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. mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
  1708. }
  1709. out:
  1710. return ret;
  1711. }
  1712. static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1713. {
  1714. struct rtl8169_private *tp = netdev_priv(dev);
  1715. int ret;
  1716. del_timer_sync(&tp->timer);
  1717. rtl_lock_work(tp);
  1718. ret = rtl8169_set_speed(dev, cmd->autoneg, ethtool_cmd_speed(cmd),
  1719. cmd->duplex, cmd->advertising);
  1720. rtl_unlock_work(tp);
  1721. return ret;
  1722. }
  1723. static netdev_features_t rtl8169_fix_features(struct net_device *dev,
  1724. netdev_features_t features)
  1725. {
  1726. struct rtl8169_private *tp = netdev_priv(dev);
  1727. if (dev->mtu > TD_MSS_MAX)
  1728. features &= ~NETIF_F_ALL_TSO;
  1729. if (dev->mtu > JUMBO_1K &&
  1730. !rtl_chip_infos[tp->mac_version].jumbo_tx_csum)
  1731. features &= ~NETIF_F_IP_CSUM;
  1732. return features;
  1733. }
  1734. static void __rtl8169_set_features(struct net_device *dev,
  1735. netdev_features_t features)
  1736. {
  1737. struct rtl8169_private *tp = netdev_priv(dev);
  1738. void __iomem *ioaddr = tp->mmio_addr;
  1739. u32 rx_config;
  1740. rx_config = RTL_R32(RxConfig);
  1741. if (features & NETIF_F_RXALL)
  1742. rx_config |= (AcceptErr | AcceptRunt);
  1743. else
  1744. rx_config &= ~(AcceptErr | AcceptRunt);
  1745. RTL_W32(RxConfig, rx_config);
  1746. if (features & NETIF_F_RXCSUM)
  1747. tp->cp_cmd |= RxChkSum;
  1748. else
  1749. tp->cp_cmd &= ~RxChkSum;
  1750. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  1751. tp->cp_cmd |= RxVlan;
  1752. else
  1753. tp->cp_cmd &= ~RxVlan;
  1754. tp->cp_cmd |= RTL_R16(CPlusCmd) & ~(RxVlan | RxChkSum);
  1755. RTL_W16(CPlusCmd, tp->cp_cmd);
  1756. RTL_R16(CPlusCmd);
  1757. }
  1758. static int rtl8169_set_features(struct net_device *dev,
  1759. netdev_features_t features)
  1760. {
  1761. struct rtl8169_private *tp = netdev_priv(dev);
  1762. features &= NETIF_F_RXALL | NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_RX;
  1763. rtl_lock_work(tp);
  1764. if (features ^ dev->features)
  1765. __rtl8169_set_features(dev, features);
  1766. rtl_unlock_work(tp);
  1767. return 0;
  1768. }
  1769. static inline u32 rtl8169_tx_vlan_tag(struct sk_buff *skb)
  1770. {
  1771. return (skb_vlan_tag_present(skb)) ?
  1772. TxVlanTag | swab16(skb_vlan_tag_get(skb)) : 0x00;
  1773. }
  1774. static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
  1775. {
  1776. u32 opts2 = le32_to_cpu(desc->opts2);
  1777. if (opts2 & RxVlanTag)
  1778. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), swab16(opts2 & 0xffff));
  1779. }
  1780. static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
  1781. {
  1782. struct rtl8169_private *tp = netdev_priv(dev);
  1783. void __iomem *ioaddr = tp->mmio_addr;
  1784. u32 status;
  1785. cmd->supported =
  1786. SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
  1787. cmd->port = PORT_FIBRE;
  1788. cmd->transceiver = XCVR_INTERNAL;
  1789. status = RTL_R32(TBICSR);
  1790. cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
  1791. cmd->autoneg = !!(status & TBINwEnable);
  1792. ethtool_cmd_speed_set(cmd, SPEED_1000);
  1793. cmd->duplex = DUPLEX_FULL; /* Always set */
  1794. return 0;
  1795. }
  1796. static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
  1797. {
  1798. struct rtl8169_private *tp = netdev_priv(dev);
  1799. return mii_ethtool_gset(&tp->mii, cmd);
  1800. }
  1801. static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1802. {
  1803. struct rtl8169_private *tp = netdev_priv(dev);
  1804. int rc;
  1805. rtl_lock_work(tp);
  1806. rc = tp->get_settings(dev, cmd);
  1807. rtl_unlock_work(tp);
  1808. return rc;
  1809. }
  1810. static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
  1811. void *p)
  1812. {
  1813. struct rtl8169_private *tp = netdev_priv(dev);
  1814. u32 __iomem *data = tp->mmio_addr;
  1815. u32 *dw = p;
  1816. int i;
  1817. rtl_lock_work(tp);
  1818. for (i = 0; i < R8169_REGS_SIZE; i += 4)
  1819. memcpy_fromio(dw++, data++, 4);
  1820. rtl_unlock_work(tp);
  1821. }
  1822. static u32 rtl8169_get_msglevel(struct net_device *dev)
  1823. {
  1824. struct rtl8169_private *tp = netdev_priv(dev);
  1825. return tp->msg_enable;
  1826. }
  1827. static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
  1828. {
  1829. struct rtl8169_private *tp = netdev_priv(dev);
  1830. tp->msg_enable = value;
  1831. }
  1832. static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
  1833. "tx_packets",
  1834. "rx_packets",
  1835. "tx_errors",
  1836. "rx_errors",
  1837. "rx_missed",
  1838. "align_errors",
  1839. "tx_single_collisions",
  1840. "tx_multi_collisions",
  1841. "unicast",
  1842. "broadcast",
  1843. "multicast",
  1844. "tx_aborted",
  1845. "tx_underrun",
  1846. };
  1847. static int rtl8169_get_sset_count(struct net_device *dev, int sset)
  1848. {
  1849. switch (sset) {
  1850. case ETH_SS_STATS:
  1851. return ARRAY_SIZE(rtl8169_gstrings);
  1852. default:
  1853. return -EOPNOTSUPP;
  1854. }
  1855. }
  1856. DECLARE_RTL_COND(rtl_counters_cond)
  1857. {
  1858. void __iomem *ioaddr = tp->mmio_addr;
  1859. return RTL_R32(CounterAddrLow) & (CounterReset | CounterDump);
  1860. }
  1861. static bool rtl8169_do_counters(struct net_device *dev, u32 counter_cmd)
  1862. {
  1863. struct rtl8169_private *tp = netdev_priv(dev);
  1864. void __iomem *ioaddr = tp->mmio_addr;
  1865. dma_addr_t paddr = tp->counters_phys_addr;
  1866. u32 cmd;
  1867. bool ret;
  1868. RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
  1869. cmd = (u64)paddr & DMA_BIT_MASK(32);
  1870. RTL_W32(CounterAddrLow, cmd);
  1871. RTL_W32(CounterAddrLow, cmd | counter_cmd);
  1872. ret = rtl_udelay_loop_wait_low(tp, &rtl_counters_cond, 10, 1000);
  1873. RTL_W32(CounterAddrLow, 0);
  1874. RTL_W32(CounterAddrHigh, 0);
  1875. return ret;
  1876. }
  1877. static bool rtl8169_reset_counters(struct net_device *dev)
  1878. {
  1879. struct rtl8169_private *tp = netdev_priv(dev);
  1880. /*
  1881. * Versions prior to RTL_GIGA_MAC_VER_19 don't support resetting the
  1882. * tally counters.
  1883. */
  1884. if (tp->mac_version < RTL_GIGA_MAC_VER_19)
  1885. return true;
  1886. return rtl8169_do_counters(dev, CounterReset);
  1887. }
  1888. static bool rtl8169_update_counters(struct net_device *dev)
  1889. {
  1890. struct rtl8169_private *tp = netdev_priv(dev);
  1891. void __iomem *ioaddr = tp->mmio_addr;
  1892. /*
  1893. * Some chips are unable to dump tally counters when the receiver
  1894. * is disabled.
  1895. */
  1896. if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
  1897. return true;
  1898. return rtl8169_do_counters(dev, CounterDump);
  1899. }
  1900. static bool rtl8169_init_counter_offsets(struct net_device *dev)
  1901. {
  1902. struct rtl8169_private *tp = netdev_priv(dev);
  1903. struct rtl8169_counters *counters = tp->counters;
  1904. bool ret = false;
  1905. /*
  1906. * rtl8169_init_counter_offsets is called from rtl_open. On chip
  1907. * versions prior to RTL_GIGA_MAC_VER_19 the tally counters are only
  1908. * reset by a power cycle, while the counter values collected by the
  1909. * driver are reset at every driver unload/load cycle.
  1910. *
  1911. * To make sure the HW values returned by @get_stats64 match the SW
  1912. * values, we collect the initial values at first open(*) and use them
  1913. * as offsets to normalize the values returned by @get_stats64.
  1914. *
  1915. * (*) We can't call rtl8169_init_counter_offsets from rtl_init_one
  1916. * for the reason stated in rtl8169_update_counters; CmdRxEnb is only
  1917. * set at open time by rtl_hw_start.
  1918. */
  1919. if (tp->tc_offset.inited)
  1920. return true;
  1921. /* If both, reset and update fail, propagate to caller. */
  1922. if (rtl8169_reset_counters(dev))
  1923. ret = true;
  1924. if (rtl8169_update_counters(dev))
  1925. ret = true;
  1926. tp->tc_offset.tx_errors = counters->tx_errors;
  1927. tp->tc_offset.tx_multi_collision = counters->tx_multi_collision;
  1928. tp->tc_offset.tx_aborted = counters->tx_aborted;
  1929. tp->tc_offset.inited = true;
  1930. return ret;
  1931. }
  1932. static void rtl8169_get_ethtool_stats(struct net_device *dev,
  1933. struct ethtool_stats *stats, u64 *data)
  1934. {
  1935. struct rtl8169_private *tp = netdev_priv(dev);
  1936. struct rtl8169_counters *counters = tp->counters;
  1937. ASSERT_RTNL();
  1938. rtl8169_update_counters(dev);
  1939. data[0] = le64_to_cpu(counters->tx_packets);
  1940. data[1] = le64_to_cpu(counters->rx_packets);
  1941. data[2] = le64_to_cpu(counters->tx_errors);
  1942. data[3] = le32_to_cpu(counters->rx_errors);
  1943. data[4] = le16_to_cpu(counters->rx_missed);
  1944. data[5] = le16_to_cpu(counters->align_errors);
  1945. data[6] = le32_to_cpu(counters->tx_one_collision);
  1946. data[7] = le32_to_cpu(counters->tx_multi_collision);
  1947. data[8] = le64_to_cpu(counters->rx_unicast);
  1948. data[9] = le64_to_cpu(counters->rx_broadcast);
  1949. data[10] = le32_to_cpu(counters->rx_multicast);
  1950. data[11] = le16_to_cpu(counters->tx_aborted);
  1951. data[12] = le16_to_cpu(counters->tx_underun);
  1952. }
  1953. static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
  1954. {
  1955. switch(stringset) {
  1956. case ETH_SS_STATS:
  1957. memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
  1958. break;
  1959. }
  1960. }
  1961. static const struct ethtool_ops rtl8169_ethtool_ops = {
  1962. .get_drvinfo = rtl8169_get_drvinfo,
  1963. .get_regs_len = rtl8169_get_regs_len,
  1964. .get_link = ethtool_op_get_link,
  1965. .get_settings = rtl8169_get_settings,
  1966. .set_settings = rtl8169_set_settings,
  1967. .get_msglevel = rtl8169_get_msglevel,
  1968. .set_msglevel = rtl8169_set_msglevel,
  1969. .get_regs = rtl8169_get_regs,
  1970. .get_wol = rtl8169_get_wol,
  1971. .set_wol = rtl8169_set_wol,
  1972. .get_strings = rtl8169_get_strings,
  1973. .get_sset_count = rtl8169_get_sset_count,
  1974. .get_ethtool_stats = rtl8169_get_ethtool_stats,
  1975. .get_ts_info = ethtool_op_get_ts_info,
  1976. };
  1977. static void rtl8169_get_mac_version(struct rtl8169_private *tp,
  1978. struct net_device *dev, u8 default_version)
  1979. {
  1980. void __iomem *ioaddr = tp->mmio_addr;
  1981. /*
  1982. * The driver currently handles the 8168Bf and the 8168Be identically
  1983. * but they can be identified more specifically through the test below
  1984. * if needed:
  1985. *
  1986. * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
  1987. *
  1988. * Same thing for the 8101Eb and the 8101Ec:
  1989. *
  1990. * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
  1991. */
  1992. static const struct rtl_mac_info {
  1993. u32 mask;
  1994. u32 val;
  1995. int mac_version;
  1996. } mac_info[] = {
  1997. /* 8168EP family. */
  1998. { 0x7cf00000, 0x50200000, RTL_GIGA_MAC_VER_51 },
  1999. { 0x7cf00000, 0x50100000, RTL_GIGA_MAC_VER_50 },
  2000. { 0x7cf00000, 0x50000000, RTL_GIGA_MAC_VER_49 },
  2001. /* 8168H family. */
  2002. { 0x7cf00000, 0x54100000, RTL_GIGA_MAC_VER_46 },
  2003. { 0x7cf00000, 0x54000000, RTL_GIGA_MAC_VER_45 },
  2004. /* 8168G family. */
  2005. { 0x7cf00000, 0x5c800000, RTL_GIGA_MAC_VER_44 },
  2006. { 0x7cf00000, 0x50900000, RTL_GIGA_MAC_VER_42 },
  2007. { 0x7cf00000, 0x4c100000, RTL_GIGA_MAC_VER_41 },
  2008. { 0x7cf00000, 0x4c000000, RTL_GIGA_MAC_VER_40 },
  2009. /* 8168F family. */
  2010. { 0x7c800000, 0x48800000, RTL_GIGA_MAC_VER_38 },
  2011. { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
  2012. { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
  2013. /* 8168E family. */
  2014. { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
  2015. { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
  2016. { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
  2017. { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
  2018. /* 8168D family. */
  2019. { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
  2020. { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
  2021. { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
  2022. /* 8168DP family. */
  2023. { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
  2024. { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
  2025. { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
  2026. /* 8168C family. */
  2027. { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
  2028. { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
  2029. { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
  2030. { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
  2031. { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
  2032. { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
  2033. { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
  2034. { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
  2035. { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
  2036. /* 8168B family. */
  2037. { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
  2038. { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
  2039. { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
  2040. { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
  2041. /* 8101 family. */
  2042. { 0x7cf00000, 0x44900000, RTL_GIGA_MAC_VER_39 },
  2043. { 0x7c800000, 0x44800000, RTL_GIGA_MAC_VER_39 },
  2044. { 0x7c800000, 0x44000000, RTL_GIGA_MAC_VER_37 },
  2045. { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
  2046. { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
  2047. { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
  2048. { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
  2049. { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
  2050. { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
  2051. { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
  2052. { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
  2053. { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
  2054. { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
  2055. { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
  2056. { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
  2057. { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
  2058. { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
  2059. { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
  2060. { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
  2061. /* FIXME: where did these entries come from ? -- FR */
  2062. { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
  2063. { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
  2064. /* 8110 family. */
  2065. { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
  2066. { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
  2067. { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
  2068. { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
  2069. { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
  2070. { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
  2071. /* Catch-all */
  2072. { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
  2073. };
  2074. const struct rtl_mac_info *p = mac_info;
  2075. u32 reg;
  2076. reg = RTL_R32(TxConfig);
  2077. while ((reg & p->mask) != p->val)
  2078. p++;
  2079. tp->mac_version = p->mac_version;
  2080. if (tp->mac_version == RTL_GIGA_MAC_NONE) {
  2081. netif_notice(tp, probe, dev,
  2082. "unknown MAC, using family default\n");
  2083. tp->mac_version = default_version;
  2084. } else if (tp->mac_version == RTL_GIGA_MAC_VER_42) {
  2085. tp->mac_version = tp->mii.supports_gmii ?
  2086. RTL_GIGA_MAC_VER_42 :
  2087. RTL_GIGA_MAC_VER_43;
  2088. } else if (tp->mac_version == RTL_GIGA_MAC_VER_45) {
  2089. tp->mac_version = tp->mii.supports_gmii ?
  2090. RTL_GIGA_MAC_VER_45 :
  2091. RTL_GIGA_MAC_VER_47;
  2092. } else if (tp->mac_version == RTL_GIGA_MAC_VER_46) {
  2093. tp->mac_version = tp->mii.supports_gmii ?
  2094. RTL_GIGA_MAC_VER_46 :
  2095. RTL_GIGA_MAC_VER_48;
  2096. }
  2097. }
  2098. static void rtl8169_print_mac_version(struct rtl8169_private *tp)
  2099. {
  2100. dprintk("mac_version = 0x%02x\n", tp->mac_version);
  2101. }
  2102. struct phy_reg {
  2103. u16 reg;
  2104. u16 val;
  2105. };
  2106. static void rtl_writephy_batch(struct rtl8169_private *tp,
  2107. const struct phy_reg *regs, int len)
  2108. {
  2109. while (len-- > 0) {
  2110. rtl_writephy(tp, regs->reg, regs->val);
  2111. regs++;
  2112. }
  2113. }
  2114. #define PHY_READ 0x00000000
  2115. #define PHY_DATA_OR 0x10000000
  2116. #define PHY_DATA_AND 0x20000000
  2117. #define PHY_BJMPN 0x30000000
  2118. #define PHY_MDIO_CHG 0x40000000
  2119. #define PHY_CLEAR_READCOUNT 0x70000000
  2120. #define PHY_WRITE 0x80000000
  2121. #define PHY_READCOUNT_EQ_SKIP 0x90000000
  2122. #define PHY_COMP_EQ_SKIPN 0xa0000000
  2123. #define PHY_COMP_NEQ_SKIPN 0xb0000000
  2124. #define PHY_WRITE_PREVIOUS 0xc0000000
  2125. #define PHY_SKIPN 0xd0000000
  2126. #define PHY_DELAY_MS 0xe0000000
  2127. struct fw_info {
  2128. u32 magic;
  2129. char version[RTL_VER_SIZE];
  2130. __le32 fw_start;
  2131. __le32 fw_len;
  2132. u8 chksum;
  2133. } __packed;
  2134. #define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
  2135. static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
  2136. {
  2137. const struct firmware *fw = rtl_fw->fw;
  2138. struct fw_info *fw_info = (struct fw_info *)fw->data;
  2139. struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
  2140. char *version = rtl_fw->version;
  2141. bool rc = false;
  2142. if (fw->size < FW_OPCODE_SIZE)
  2143. goto out;
  2144. if (!fw_info->magic) {
  2145. size_t i, size, start;
  2146. u8 checksum = 0;
  2147. if (fw->size < sizeof(*fw_info))
  2148. goto out;
  2149. for (i = 0; i < fw->size; i++)
  2150. checksum += fw->data[i];
  2151. if (checksum != 0)
  2152. goto out;
  2153. start = le32_to_cpu(fw_info->fw_start);
  2154. if (start > fw->size)
  2155. goto out;
  2156. size = le32_to_cpu(fw_info->fw_len);
  2157. if (size > (fw->size - start) / FW_OPCODE_SIZE)
  2158. goto out;
  2159. memcpy(version, fw_info->version, RTL_VER_SIZE);
  2160. pa->code = (__le32 *)(fw->data + start);
  2161. pa->size = size;
  2162. } else {
  2163. if (fw->size % FW_OPCODE_SIZE)
  2164. goto out;
  2165. strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
  2166. pa->code = (__le32 *)fw->data;
  2167. pa->size = fw->size / FW_OPCODE_SIZE;
  2168. }
  2169. version[RTL_VER_SIZE - 1] = 0;
  2170. rc = true;
  2171. out:
  2172. return rc;
  2173. }
  2174. static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
  2175. struct rtl_fw_phy_action *pa)
  2176. {
  2177. bool rc = false;
  2178. size_t index;
  2179. for (index = 0; index < pa->size; index++) {
  2180. u32 action = le32_to_cpu(pa->code[index]);
  2181. u32 regno = (action & 0x0fff0000) >> 16;
  2182. switch(action & 0xf0000000) {
  2183. case PHY_READ:
  2184. case PHY_DATA_OR:
  2185. case PHY_DATA_AND:
  2186. case PHY_MDIO_CHG:
  2187. case PHY_CLEAR_READCOUNT:
  2188. case PHY_WRITE:
  2189. case PHY_WRITE_PREVIOUS:
  2190. case PHY_DELAY_MS:
  2191. break;
  2192. case PHY_BJMPN:
  2193. if (regno > index) {
  2194. netif_err(tp, ifup, tp->dev,
  2195. "Out of range of firmware\n");
  2196. goto out;
  2197. }
  2198. break;
  2199. case PHY_READCOUNT_EQ_SKIP:
  2200. if (index + 2 >= pa->size) {
  2201. netif_err(tp, ifup, tp->dev,
  2202. "Out of range of firmware\n");
  2203. goto out;
  2204. }
  2205. break;
  2206. case PHY_COMP_EQ_SKIPN:
  2207. case PHY_COMP_NEQ_SKIPN:
  2208. case PHY_SKIPN:
  2209. if (index + 1 + regno >= pa->size) {
  2210. netif_err(tp, ifup, tp->dev,
  2211. "Out of range of firmware\n");
  2212. goto out;
  2213. }
  2214. break;
  2215. default:
  2216. netif_err(tp, ifup, tp->dev,
  2217. "Invalid action 0x%08x\n", action);
  2218. goto out;
  2219. }
  2220. }
  2221. rc = true;
  2222. out:
  2223. return rc;
  2224. }
  2225. static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
  2226. {
  2227. struct net_device *dev = tp->dev;
  2228. int rc = -EINVAL;
  2229. if (!rtl_fw_format_ok(tp, rtl_fw)) {
  2230. netif_err(tp, ifup, dev, "invalid firmware\n");
  2231. goto out;
  2232. }
  2233. if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
  2234. rc = 0;
  2235. out:
  2236. return rc;
  2237. }
  2238. static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
  2239. {
  2240. struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
  2241. struct mdio_ops org, *ops = &tp->mdio_ops;
  2242. u32 predata, count;
  2243. size_t index;
  2244. predata = count = 0;
  2245. org.write = ops->write;
  2246. org.read = ops->read;
  2247. for (index = 0; index < pa->size; ) {
  2248. u32 action = le32_to_cpu(pa->code[index]);
  2249. u32 data = action & 0x0000ffff;
  2250. u32 regno = (action & 0x0fff0000) >> 16;
  2251. if (!action)
  2252. break;
  2253. switch(action & 0xf0000000) {
  2254. case PHY_READ:
  2255. predata = rtl_readphy(tp, regno);
  2256. count++;
  2257. index++;
  2258. break;
  2259. case PHY_DATA_OR:
  2260. predata |= data;
  2261. index++;
  2262. break;
  2263. case PHY_DATA_AND:
  2264. predata &= data;
  2265. index++;
  2266. break;
  2267. case PHY_BJMPN:
  2268. index -= regno;
  2269. break;
  2270. case PHY_MDIO_CHG:
  2271. if (data == 0) {
  2272. ops->write = org.write;
  2273. ops->read = org.read;
  2274. } else if (data == 1) {
  2275. ops->write = mac_mcu_write;
  2276. ops->read = mac_mcu_read;
  2277. }
  2278. index++;
  2279. break;
  2280. case PHY_CLEAR_READCOUNT:
  2281. count = 0;
  2282. index++;
  2283. break;
  2284. case PHY_WRITE:
  2285. rtl_writephy(tp, regno, data);
  2286. index++;
  2287. break;
  2288. case PHY_READCOUNT_EQ_SKIP:
  2289. index += (count == data) ? 2 : 1;
  2290. break;
  2291. case PHY_COMP_EQ_SKIPN:
  2292. if (predata == data)
  2293. index += regno;
  2294. index++;
  2295. break;
  2296. case PHY_COMP_NEQ_SKIPN:
  2297. if (predata != data)
  2298. index += regno;
  2299. index++;
  2300. break;
  2301. case PHY_WRITE_PREVIOUS:
  2302. rtl_writephy(tp, regno, predata);
  2303. index++;
  2304. break;
  2305. case PHY_SKIPN:
  2306. index += regno + 1;
  2307. break;
  2308. case PHY_DELAY_MS:
  2309. mdelay(data);
  2310. index++;
  2311. break;
  2312. default:
  2313. BUG();
  2314. }
  2315. }
  2316. ops->write = org.write;
  2317. ops->read = org.read;
  2318. }
  2319. static void rtl_release_firmware(struct rtl8169_private *tp)
  2320. {
  2321. if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
  2322. release_firmware(tp->rtl_fw->fw);
  2323. kfree(tp->rtl_fw);
  2324. }
  2325. tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
  2326. }
  2327. static void rtl_apply_firmware(struct rtl8169_private *tp)
  2328. {
  2329. struct rtl_fw *rtl_fw = tp->rtl_fw;
  2330. /* TODO: release firmware once rtl_phy_write_fw signals failures. */
  2331. if (!IS_ERR_OR_NULL(rtl_fw))
  2332. rtl_phy_write_fw(tp, rtl_fw);
  2333. }
  2334. static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
  2335. {
  2336. if (rtl_readphy(tp, reg) != val)
  2337. netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
  2338. else
  2339. rtl_apply_firmware(tp);
  2340. }
  2341. static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
  2342. {
  2343. static const struct phy_reg phy_reg_init[] = {
  2344. { 0x1f, 0x0001 },
  2345. { 0x06, 0x006e },
  2346. { 0x08, 0x0708 },
  2347. { 0x15, 0x4000 },
  2348. { 0x18, 0x65c7 },
  2349. { 0x1f, 0x0001 },
  2350. { 0x03, 0x00a1 },
  2351. { 0x02, 0x0008 },
  2352. { 0x01, 0x0120 },
  2353. { 0x00, 0x1000 },
  2354. { 0x04, 0x0800 },
  2355. { 0x04, 0x0000 },
  2356. { 0x03, 0xff41 },
  2357. { 0x02, 0xdf60 },
  2358. { 0x01, 0x0140 },
  2359. { 0x00, 0x0077 },
  2360. { 0x04, 0x7800 },
  2361. { 0x04, 0x7000 },
  2362. { 0x03, 0x802f },
  2363. { 0x02, 0x4f02 },
  2364. { 0x01, 0x0409 },
  2365. { 0x00, 0xf0f9 },
  2366. { 0x04, 0x9800 },
  2367. { 0x04, 0x9000 },
  2368. { 0x03, 0xdf01 },
  2369. { 0x02, 0xdf20 },
  2370. { 0x01, 0xff95 },
  2371. { 0x00, 0xba00 },
  2372. { 0x04, 0xa800 },
  2373. { 0x04, 0xa000 },
  2374. { 0x03, 0xff41 },
  2375. { 0x02, 0xdf20 },
  2376. { 0x01, 0x0140 },
  2377. { 0x00, 0x00bb },
  2378. { 0x04, 0xb800 },
  2379. { 0x04, 0xb000 },
  2380. { 0x03, 0xdf41 },
  2381. { 0x02, 0xdc60 },
  2382. { 0x01, 0x6340 },
  2383. { 0x00, 0x007d },
  2384. { 0x04, 0xd800 },
  2385. { 0x04, 0xd000 },
  2386. { 0x03, 0xdf01 },
  2387. { 0x02, 0xdf20 },
  2388. { 0x01, 0x100a },
  2389. { 0x00, 0xa0ff },
  2390. { 0x04, 0xf800 },
  2391. { 0x04, 0xf000 },
  2392. { 0x1f, 0x0000 },
  2393. { 0x0b, 0x0000 },
  2394. { 0x00, 0x9200 }
  2395. };
  2396. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2397. }
  2398. static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
  2399. {
  2400. static const struct phy_reg phy_reg_init[] = {
  2401. { 0x1f, 0x0002 },
  2402. { 0x01, 0x90d0 },
  2403. { 0x1f, 0x0000 }
  2404. };
  2405. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2406. }
  2407. static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
  2408. {
  2409. struct pci_dev *pdev = tp->pci_dev;
  2410. if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
  2411. (pdev->subsystem_device != 0xe000))
  2412. return;
  2413. rtl_writephy(tp, 0x1f, 0x0001);
  2414. rtl_writephy(tp, 0x10, 0xf01b);
  2415. rtl_writephy(tp, 0x1f, 0x0000);
  2416. }
  2417. static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
  2418. {
  2419. static const struct phy_reg phy_reg_init[] = {
  2420. { 0x1f, 0x0001 },
  2421. { 0x04, 0x0000 },
  2422. { 0x03, 0x00a1 },
  2423. { 0x02, 0x0008 },
  2424. { 0x01, 0x0120 },
  2425. { 0x00, 0x1000 },
  2426. { 0x04, 0x0800 },
  2427. { 0x04, 0x9000 },
  2428. { 0x03, 0x802f },
  2429. { 0x02, 0x4f02 },
  2430. { 0x01, 0x0409 },
  2431. { 0x00, 0xf099 },
  2432. { 0x04, 0x9800 },
  2433. { 0x04, 0xa000 },
  2434. { 0x03, 0xdf01 },
  2435. { 0x02, 0xdf20 },
  2436. { 0x01, 0xff95 },
  2437. { 0x00, 0xba00 },
  2438. { 0x04, 0xa800 },
  2439. { 0x04, 0xf000 },
  2440. { 0x03, 0xdf01 },
  2441. { 0x02, 0xdf20 },
  2442. { 0x01, 0x101a },
  2443. { 0x00, 0xa0ff },
  2444. { 0x04, 0xf800 },
  2445. { 0x04, 0x0000 },
  2446. { 0x1f, 0x0000 },
  2447. { 0x1f, 0x0001 },
  2448. { 0x10, 0xf41b },
  2449. { 0x14, 0xfb54 },
  2450. { 0x18, 0xf5c7 },
  2451. { 0x1f, 0x0000 },
  2452. { 0x1f, 0x0001 },
  2453. { 0x17, 0x0cc0 },
  2454. { 0x1f, 0x0000 }
  2455. };
  2456. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2457. rtl8169scd_hw_phy_config_quirk(tp);
  2458. }
  2459. static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
  2460. {
  2461. static const struct phy_reg phy_reg_init[] = {
  2462. { 0x1f, 0x0001 },
  2463. { 0x04, 0x0000 },
  2464. { 0x03, 0x00a1 },
  2465. { 0x02, 0x0008 },
  2466. { 0x01, 0x0120 },
  2467. { 0x00, 0x1000 },
  2468. { 0x04, 0x0800 },
  2469. { 0x04, 0x9000 },
  2470. { 0x03, 0x802f },
  2471. { 0x02, 0x4f02 },
  2472. { 0x01, 0x0409 },
  2473. { 0x00, 0xf099 },
  2474. { 0x04, 0x9800 },
  2475. { 0x04, 0xa000 },
  2476. { 0x03, 0xdf01 },
  2477. { 0x02, 0xdf20 },
  2478. { 0x01, 0xff95 },
  2479. { 0x00, 0xba00 },
  2480. { 0x04, 0xa800 },
  2481. { 0x04, 0xf000 },
  2482. { 0x03, 0xdf01 },
  2483. { 0x02, 0xdf20 },
  2484. { 0x01, 0x101a },
  2485. { 0x00, 0xa0ff },
  2486. { 0x04, 0xf800 },
  2487. { 0x04, 0x0000 },
  2488. { 0x1f, 0x0000 },
  2489. { 0x1f, 0x0001 },
  2490. { 0x0b, 0x8480 },
  2491. { 0x1f, 0x0000 },
  2492. { 0x1f, 0x0001 },
  2493. { 0x18, 0x67c7 },
  2494. { 0x04, 0x2000 },
  2495. { 0x03, 0x002f },
  2496. { 0x02, 0x4360 },
  2497. { 0x01, 0x0109 },
  2498. { 0x00, 0x3022 },
  2499. { 0x04, 0x2800 },
  2500. { 0x1f, 0x0000 },
  2501. { 0x1f, 0x0001 },
  2502. { 0x17, 0x0cc0 },
  2503. { 0x1f, 0x0000 }
  2504. };
  2505. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2506. }
  2507. static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
  2508. {
  2509. static const struct phy_reg phy_reg_init[] = {
  2510. { 0x10, 0xf41b },
  2511. { 0x1f, 0x0000 }
  2512. };
  2513. rtl_writephy(tp, 0x1f, 0x0001);
  2514. rtl_patchphy(tp, 0x16, 1 << 0);
  2515. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2516. }
  2517. static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
  2518. {
  2519. static const struct phy_reg phy_reg_init[] = {
  2520. { 0x1f, 0x0001 },
  2521. { 0x10, 0xf41b },
  2522. { 0x1f, 0x0000 }
  2523. };
  2524. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2525. }
  2526. static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
  2527. {
  2528. static const struct phy_reg phy_reg_init[] = {
  2529. { 0x1f, 0x0000 },
  2530. { 0x1d, 0x0f00 },
  2531. { 0x1f, 0x0002 },
  2532. { 0x0c, 0x1ec8 },
  2533. { 0x1f, 0x0000 }
  2534. };
  2535. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2536. }
  2537. static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
  2538. {
  2539. static const struct phy_reg phy_reg_init[] = {
  2540. { 0x1f, 0x0001 },
  2541. { 0x1d, 0x3d98 },
  2542. { 0x1f, 0x0000 }
  2543. };
  2544. rtl_writephy(tp, 0x1f, 0x0000);
  2545. rtl_patchphy(tp, 0x14, 1 << 5);
  2546. rtl_patchphy(tp, 0x0d, 1 << 5);
  2547. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2548. }
  2549. static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
  2550. {
  2551. static const struct phy_reg phy_reg_init[] = {
  2552. { 0x1f, 0x0001 },
  2553. { 0x12, 0x2300 },
  2554. { 0x1f, 0x0002 },
  2555. { 0x00, 0x88d4 },
  2556. { 0x01, 0x82b1 },
  2557. { 0x03, 0x7002 },
  2558. { 0x08, 0x9e30 },
  2559. { 0x09, 0x01f0 },
  2560. { 0x0a, 0x5500 },
  2561. { 0x0c, 0x00c8 },
  2562. { 0x1f, 0x0003 },
  2563. { 0x12, 0xc096 },
  2564. { 0x16, 0x000a },
  2565. { 0x1f, 0x0000 },
  2566. { 0x1f, 0x0000 },
  2567. { 0x09, 0x2000 },
  2568. { 0x09, 0x0000 }
  2569. };
  2570. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2571. rtl_patchphy(tp, 0x14, 1 << 5);
  2572. rtl_patchphy(tp, 0x0d, 1 << 5);
  2573. rtl_writephy(tp, 0x1f, 0x0000);
  2574. }
  2575. static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
  2576. {
  2577. static const struct phy_reg phy_reg_init[] = {
  2578. { 0x1f, 0x0001 },
  2579. { 0x12, 0x2300 },
  2580. { 0x03, 0x802f },
  2581. { 0x02, 0x4f02 },
  2582. { 0x01, 0x0409 },
  2583. { 0x00, 0xf099 },
  2584. { 0x04, 0x9800 },
  2585. { 0x04, 0x9000 },
  2586. { 0x1d, 0x3d98 },
  2587. { 0x1f, 0x0002 },
  2588. { 0x0c, 0x7eb8 },
  2589. { 0x06, 0x0761 },
  2590. { 0x1f, 0x0003 },
  2591. { 0x16, 0x0f0a },
  2592. { 0x1f, 0x0000 }
  2593. };
  2594. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2595. rtl_patchphy(tp, 0x16, 1 << 0);
  2596. rtl_patchphy(tp, 0x14, 1 << 5);
  2597. rtl_patchphy(tp, 0x0d, 1 << 5);
  2598. rtl_writephy(tp, 0x1f, 0x0000);
  2599. }
  2600. static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
  2601. {
  2602. static const struct phy_reg phy_reg_init[] = {
  2603. { 0x1f, 0x0001 },
  2604. { 0x12, 0x2300 },
  2605. { 0x1d, 0x3d98 },
  2606. { 0x1f, 0x0002 },
  2607. { 0x0c, 0x7eb8 },
  2608. { 0x06, 0x5461 },
  2609. { 0x1f, 0x0003 },
  2610. { 0x16, 0x0f0a },
  2611. { 0x1f, 0x0000 }
  2612. };
  2613. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2614. rtl_patchphy(tp, 0x16, 1 << 0);
  2615. rtl_patchphy(tp, 0x14, 1 << 5);
  2616. rtl_patchphy(tp, 0x0d, 1 << 5);
  2617. rtl_writephy(tp, 0x1f, 0x0000);
  2618. }
  2619. static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
  2620. {
  2621. rtl8168c_3_hw_phy_config(tp);
  2622. }
  2623. static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
  2624. {
  2625. static const struct phy_reg phy_reg_init_0[] = {
  2626. /* Channel Estimation */
  2627. { 0x1f, 0x0001 },
  2628. { 0x06, 0x4064 },
  2629. { 0x07, 0x2863 },
  2630. { 0x08, 0x059c },
  2631. { 0x09, 0x26b4 },
  2632. { 0x0a, 0x6a19 },
  2633. { 0x0b, 0xdcc8 },
  2634. { 0x10, 0xf06d },
  2635. { 0x14, 0x7f68 },
  2636. { 0x18, 0x7fd9 },
  2637. { 0x1c, 0xf0ff },
  2638. { 0x1d, 0x3d9c },
  2639. { 0x1f, 0x0003 },
  2640. { 0x12, 0xf49f },
  2641. { 0x13, 0x070b },
  2642. { 0x1a, 0x05ad },
  2643. { 0x14, 0x94c0 },
  2644. /*
  2645. * Tx Error Issue
  2646. * Enhance line driver power
  2647. */
  2648. { 0x1f, 0x0002 },
  2649. { 0x06, 0x5561 },
  2650. { 0x1f, 0x0005 },
  2651. { 0x05, 0x8332 },
  2652. { 0x06, 0x5561 },
  2653. /*
  2654. * Can not link to 1Gbps with bad cable
  2655. * Decrease SNR threshold form 21.07dB to 19.04dB
  2656. */
  2657. { 0x1f, 0x0001 },
  2658. { 0x17, 0x0cc0 },
  2659. { 0x1f, 0x0000 },
  2660. { 0x0d, 0xf880 }
  2661. };
  2662. rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
  2663. /*
  2664. * Rx Error Issue
  2665. * Fine Tune Switching regulator parameter
  2666. */
  2667. rtl_writephy(tp, 0x1f, 0x0002);
  2668. rtl_w0w1_phy(tp, 0x0b, 0x0010, 0x00ef);
  2669. rtl_w0w1_phy(tp, 0x0c, 0xa200, 0x5d00);
  2670. if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
  2671. static const struct phy_reg phy_reg_init[] = {
  2672. { 0x1f, 0x0002 },
  2673. { 0x05, 0x669a },
  2674. { 0x1f, 0x0005 },
  2675. { 0x05, 0x8330 },
  2676. { 0x06, 0x669a },
  2677. { 0x1f, 0x0002 }
  2678. };
  2679. int val;
  2680. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2681. val = rtl_readphy(tp, 0x0d);
  2682. if ((val & 0x00ff) != 0x006c) {
  2683. static const u32 set[] = {
  2684. 0x0065, 0x0066, 0x0067, 0x0068,
  2685. 0x0069, 0x006a, 0x006b, 0x006c
  2686. };
  2687. int i;
  2688. rtl_writephy(tp, 0x1f, 0x0002);
  2689. val &= 0xff00;
  2690. for (i = 0; i < ARRAY_SIZE(set); i++)
  2691. rtl_writephy(tp, 0x0d, val | set[i]);
  2692. }
  2693. } else {
  2694. static const struct phy_reg phy_reg_init[] = {
  2695. { 0x1f, 0x0002 },
  2696. { 0x05, 0x6662 },
  2697. { 0x1f, 0x0005 },
  2698. { 0x05, 0x8330 },
  2699. { 0x06, 0x6662 }
  2700. };
  2701. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2702. }
  2703. /* RSET couple improve */
  2704. rtl_writephy(tp, 0x1f, 0x0002);
  2705. rtl_patchphy(tp, 0x0d, 0x0300);
  2706. rtl_patchphy(tp, 0x0f, 0x0010);
  2707. /* Fine tune PLL performance */
  2708. rtl_writephy(tp, 0x1f, 0x0002);
  2709. rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
  2710. rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
  2711. rtl_writephy(tp, 0x1f, 0x0005);
  2712. rtl_writephy(tp, 0x05, 0x001b);
  2713. rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
  2714. rtl_writephy(tp, 0x1f, 0x0000);
  2715. }
  2716. static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
  2717. {
  2718. static const struct phy_reg phy_reg_init_0[] = {
  2719. /* Channel Estimation */
  2720. { 0x1f, 0x0001 },
  2721. { 0x06, 0x4064 },
  2722. { 0x07, 0x2863 },
  2723. { 0x08, 0x059c },
  2724. { 0x09, 0x26b4 },
  2725. { 0x0a, 0x6a19 },
  2726. { 0x0b, 0xdcc8 },
  2727. { 0x10, 0xf06d },
  2728. { 0x14, 0x7f68 },
  2729. { 0x18, 0x7fd9 },
  2730. { 0x1c, 0xf0ff },
  2731. { 0x1d, 0x3d9c },
  2732. { 0x1f, 0x0003 },
  2733. { 0x12, 0xf49f },
  2734. { 0x13, 0x070b },
  2735. { 0x1a, 0x05ad },
  2736. { 0x14, 0x94c0 },
  2737. /*
  2738. * Tx Error Issue
  2739. * Enhance line driver power
  2740. */
  2741. { 0x1f, 0x0002 },
  2742. { 0x06, 0x5561 },
  2743. { 0x1f, 0x0005 },
  2744. { 0x05, 0x8332 },
  2745. { 0x06, 0x5561 },
  2746. /*
  2747. * Can not link to 1Gbps with bad cable
  2748. * Decrease SNR threshold form 21.07dB to 19.04dB
  2749. */
  2750. { 0x1f, 0x0001 },
  2751. { 0x17, 0x0cc0 },
  2752. { 0x1f, 0x0000 },
  2753. { 0x0d, 0xf880 }
  2754. };
  2755. rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
  2756. if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
  2757. static const struct phy_reg phy_reg_init[] = {
  2758. { 0x1f, 0x0002 },
  2759. { 0x05, 0x669a },
  2760. { 0x1f, 0x0005 },
  2761. { 0x05, 0x8330 },
  2762. { 0x06, 0x669a },
  2763. { 0x1f, 0x0002 }
  2764. };
  2765. int val;
  2766. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2767. val = rtl_readphy(tp, 0x0d);
  2768. if ((val & 0x00ff) != 0x006c) {
  2769. static const u32 set[] = {
  2770. 0x0065, 0x0066, 0x0067, 0x0068,
  2771. 0x0069, 0x006a, 0x006b, 0x006c
  2772. };
  2773. int i;
  2774. rtl_writephy(tp, 0x1f, 0x0002);
  2775. val &= 0xff00;
  2776. for (i = 0; i < ARRAY_SIZE(set); i++)
  2777. rtl_writephy(tp, 0x0d, val | set[i]);
  2778. }
  2779. } else {
  2780. static const struct phy_reg phy_reg_init[] = {
  2781. { 0x1f, 0x0002 },
  2782. { 0x05, 0x2642 },
  2783. { 0x1f, 0x0005 },
  2784. { 0x05, 0x8330 },
  2785. { 0x06, 0x2642 }
  2786. };
  2787. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2788. }
  2789. /* Fine tune PLL performance */
  2790. rtl_writephy(tp, 0x1f, 0x0002);
  2791. rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
  2792. rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
  2793. /* Switching regulator Slew rate */
  2794. rtl_writephy(tp, 0x1f, 0x0002);
  2795. rtl_patchphy(tp, 0x0f, 0x0017);
  2796. rtl_writephy(tp, 0x1f, 0x0005);
  2797. rtl_writephy(tp, 0x05, 0x001b);
  2798. rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
  2799. rtl_writephy(tp, 0x1f, 0x0000);
  2800. }
  2801. static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
  2802. {
  2803. static const struct phy_reg phy_reg_init[] = {
  2804. { 0x1f, 0x0002 },
  2805. { 0x10, 0x0008 },
  2806. { 0x0d, 0x006c },
  2807. { 0x1f, 0x0000 },
  2808. { 0x0d, 0xf880 },
  2809. { 0x1f, 0x0001 },
  2810. { 0x17, 0x0cc0 },
  2811. { 0x1f, 0x0001 },
  2812. { 0x0b, 0xa4d8 },
  2813. { 0x09, 0x281c },
  2814. { 0x07, 0x2883 },
  2815. { 0x0a, 0x6b35 },
  2816. { 0x1d, 0x3da4 },
  2817. { 0x1c, 0xeffd },
  2818. { 0x14, 0x7f52 },
  2819. { 0x18, 0x7fc6 },
  2820. { 0x08, 0x0601 },
  2821. { 0x06, 0x4063 },
  2822. { 0x10, 0xf074 },
  2823. { 0x1f, 0x0003 },
  2824. { 0x13, 0x0789 },
  2825. { 0x12, 0xf4bd },
  2826. { 0x1a, 0x04fd },
  2827. { 0x14, 0x84b0 },
  2828. { 0x1f, 0x0000 },
  2829. { 0x00, 0x9200 },
  2830. { 0x1f, 0x0005 },
  2831. { 0x01, 0x0340 },
  2832. { 0x1f, 0x0001 },
  2833. { 0x04, 0x4000 },
  2834. { 0x03, 0x1d21 },
  2835. { 0x02, 0x0c32 },
  2836. { 0x01, 0x0200 },
  2837. { 0x00, 0x5554 },
  2838. { 0x04, 0x4800 },
  2839. { 0x04, 0x4000 },
  2840. { 0x04, 0xf000 },
  2841. { 0x03, 0xdf01 },
  2842. { 0x02, 0xdf20 },
  2843. { 0x01, 0x101a },
  2844. { 0x00, 0xa0ff },
  2845. { 0x04, 0xf800 },
  2846. { 0x04, 0xf000 },
  2847. { 0x1f, 0x0000 },
  2848. { 0x1f, 0x0007 },
  2849. { 0x1e, 0x0023 },
  2850. { 0x16, 0x0000 },
  2851. { 0x1f, 0x0000 }
  2852. };
  2853. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2854. }
  2855. static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
  2856. {
  2857. static const struct phy_reg phy_reg_init[] = {
  2858. { 0x1f, 0x0001 },
  2859. { 0x17, 0x0cc0 },
  2860. { 0x1f, 0x0007 },
  2861. { 0x1e, 0x002d },
  2862. { 0x18, 0x0040 },
  2863. { 0x1f, 0x0000 }
  2864. };
  2865. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2866. rtl_patchphy(tp, 0x0d, 1 << 5);
  2867. }
  2868. static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
  2869. {
  2870. static const struct phy_reg phy_reg_init[] = {
  2871. /* Enable Delay cap */
  2872. { 0x1f, 0x0005 },
  2873. { 0x05, 0x8b80 },
  2874. { 0x06, 0xc896 },
  2875. { 0x1f, 0x0000 },
  2876. /* Channel estimation fine tune */
  2877. { 0x1f, 0x0001 },
  2878. { 0x0b, 0x6c20 },
  2879. { 0x07, 0x2872 },
  2880. { 0x1c, 0xefff },
  2881. { 0x1f, 0x0003 },
  2882. { 0x14, 0x6420 },
  2883. { 0x1f, 0x0000 },
  2884. /* Update PFM & 10M TX idle timer */
  2885. { 0x1f, 0x0007 },
  2886. { 0x1e, 0x002f },
  2887. { 0x15, 0x1919 },
  2888. { 0x1f, 0x0000 },
  2889. { 0x1f, 0x0007 },
  2890. { 0x1e, 0x00ac },
  2891. { 0x18, 0x0006 },
  2892. { 0x1f, 0x0000 }
  2893. };
  2894. rtl_apply_firmware(tp);
  2895. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2896. /* DCO enable for 10M IDLE Power */
  2897. rtl_writephy(tp, 0x1f, 0x0007);
  2898. rtl_writephy(tp, 0x1e, 0x0023);
  2899. rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
  2900. rtl_writephy(tp, 0x1f, 0x0000);
  2901. /* For impedance matching */
  2902. rtl_writephy(tp, 0x1f, 0x0002);
  2903. rtl_w0w1_phy(tp, 0x08, 0x8000, 0x7f00);
  2904. rtl_writephy(tp, 0x1f, 0x0000);
  2905. /* PHY auto speed down */
  2906. rtl_writephy(tp, 0x1f, 0x0007);
  2907. rtl_writephy(tp, 0x1e, 0x002d);
  2908. rtl_w0w1_phy(tp, 0x18, 0x0050, 0x0000);
  2909. rtl_writephy(tp, 0x1f, 0x0000);
  2910. rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
  2911. rtl_writephy(tp, 0x1f, 0x0005);
  2912. rtl_writephy(tp, 0x05, 0x8b86);
  2913. rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
  2914. rtl_writephy(tp, 0x1f, 0x0000);
  2915. rtl_writephy(tp, 0x1f, 0x0005);
  2916. rtl_writephy(tp, 0x05, 0x8b85);
  2917. rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
  2918. rtl_writephy(tp, 0x1f, 0x0007);
  2919. rtl_writephy(tp, 0x1e, 0x0020);
  2920. rtl_w0w1_phy(tp, 0x15, 0x0000, 0x1100);
  2921. rtl_writephy(tp, 0x1f, 0x0006);
  2922. rtl_writephy(tp, 0x00, 0x5a00);
  2923. rtl_writephy(tp, 0x1f, 0x0000);
  2924. rtl_writephy(tp, 0x0d, 0x0007);
  2925. rtl_writephy(tp, 0x0e, 0x003c);
  2926. rtl_writephy(tp, 0x0d, 0x4007);
  2927. rtl_writephy(tp, 0x0e, 0x0000);
  2928. rtl_writephy(tp, 0x0d, 0x0000);
  2929. }
  2930. static void rtl_rar_exgmac_set(struct rtl8169_private *tp, u8 *addr)
  2931. {
  2932. const u16 w[] = {
  2933. addr[0] | (addr[1] << 8),
  2934. addr[2] | (addr[3] << 8),
  2935. addr[4] | (addr[5] << 8)
  2936. };
  2937. const struct exgmac_reg e[] = {
  2938. { .addr = 0xe0, ERIAR_MASK_1111, .val = w[0] | (w[1] << 16) },
  2939. { .addr = 0xe4, ERIAR_MASK_1111, .val = w[2] },
  2940. { .addr = 0xf0, ERIAR_MASK_1111, .val = w[0] << 16 },
  2941. { .addr = 0xf4, ERIAR_MASK_1111, .val = w[1] | (w[2] << 16) }
  2942. };
  2943. rtl_write_exgmac_batch(tp, e, ARRAY_SIZE(e));
  2944. }
  2945. static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
  2946. {
  2947. static const struct phy_reg phy_reg_init[] = {
  2948. /* Enable Delay cap */
  2949. { 0x1f, 0x0004 },
  2950. { 0x1f, 0x0007 },
  2951. { 0x1e, 0x00ac },
  2952. { 0x18, 0x0006 },
  2953. { 0x1f, 0x0002 },
  2954. { 0x1f, 0x0000 },
  2955. { 0x1f, 0x0000 },
  2956. /* Channel estimation fine tune */
  2957. { 0x1f, 0x0003 },
  2958. { 0x09, 0xa20f },
  2959. { 0x1f, 0x0000 },
  2960. { 0x1f, 0x0000 },
  2961. /* Green Setting */
  2962. { 0x1f, 0x0005 },
  2963. { 0x05, 0x8b5b },
  2964. { 0x06, 0x9222 },
  2965. { 0x05, 0x8b6d },
  2966. { 0x06, 0x8000 },
  2967. { 0x05, 0x8b76 },
  2968. { 0x06, 0x8000 },
  2969. { 0x1f, 0x0000 }
  2970. };
  2971. rtl_apply_firmware(tp);
  2972. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2973. /* For 4-corner performance improve */
  2974. rtl_writephy(tp, 0x1f, 0x0005);
  2975. rtl_writephy(tp, 0x05, 0x8b80);
  2976. rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
  2977. rtl_writephy(tp, 0x1f, 0x0000);
  2978. /* PHY auto speed down */
  2979. rtl_writephy(tp, 0x1f, 0x0004);
  2980. rtl_writephy(tp, 0x1f, 0x0007);
  2981. rtl_writephy(tp, 0x1e, 0x002d);
  2982. rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
  2983. rtl_writephy(tp, 0x1f, 0x0002);
  2984. rtl_writephy(tp, 0x1f, 0x0000);
  2985. rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
  2986. /* improve 10M EEE waveform */
  2987. rtl_writephy(tp, 0x1f, 0x0005);
  2988. rtl_writephy(tp, 0x05, 0x8b86);
  2989. rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
  2990. rtl_writephy(tp, 0x1f, 0x0000);
  2991. /* Improve 2-pair detection performance */
  2992. rtl_writephy(tp, 0x1f, 0x0005);
  2993. rtl_writephy(tp, 0x05, 0x8b85);
  2994. rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
  2995. rtl_writephy(tp, 0x1f, 0x0000);
  2996. /* EEE setting */
  2997. rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_1111, 0x0000, 0x0003, ERIAR_EXGMAC);
  2998. rtl_writephy(tp, 0x1f, 0x0005);
  2999. rtl_writephy(tp, 0x05, 0x8b85);
  3000. rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
  3001. rtl_writephy(tp, 0x1f, 0x0004);
  3002. rtl_writephy(tp, 0x1f, 0x0007);
  3003. rtl_writephy(tp, 0x1e, 0x0020);
  3004. rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0100);
  3005. rtl_writephy(tp, 0x1f, 0x0002);
  3006. rtl_writephy(tp, 0x1f, 0x0000);
  3007. rtl_writephy(tp, 0x0d, 0x0007);
  3008. rtl_writephy(tp, 0x0e, 0x003c);
  3009. rtl_writephy(tp, 0x0d, 0x4007);
  3010. rtl_writephy(tp, 0x0e, 0x0000);
  3011. rtl_writephy(tp, 0x0d, 0x0000);
  3012. /* Green feature */
  3013. rtl_writephy(tp, 0x1f, 0x0003);
  3014. rtl_w0w1_phy(tp, 0x19, 0x0000, 0x0001);
  3015. rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0400);
  3016. rtl_writephy(tp, 0x1f, 0x0000);
  3017. /* Broken BIOS workaround: feed GigaMAC registers with MAC address. */
  3018. rtl_rar_exgmac_set(tp, tp->dev->dev_addr);
  3019. }
  3020. static void rtl8168f_hw_phy_config(struct rtl8169_private *tp)
  3021. {
  3022. /* For 4-corner performance improve */
  3023. rtl_writephy(tp, 0x1f, 0x0005);
  3024. rtl_writephy(tp, 0x05, 0x8b80);
  3025. rtl_w0w1_phy(tp, 0x06, 0x0006, 0x0000);
  3026. rtl_writephy(tp, 0x1f, 0x0000);
  3027. /* PHY auto speed down */
  3028. rtl_writephy(tp, 0x1f, 0x0007);
  3029. rtl_writephy(tp, 0x1e, 0x002d);
  3030. rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
  3031. rtl_writephy(tp, 0x1f, 0x0000);
  3032. rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
  3033. /* Improve 10M EEE waveform */
  3034. rtl_writephy(tp, 0x1f, 0x0005);
  3035. rtl_writephy(tp, 0x05, 0x8b86);
  3036. rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
  3037. rtl_writephy(tp, 0x1f, 0x0000);
  3038. }
  3039. static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
  3040. {
  3041. static const struct phy_reg phy_reg_init[] = {
  3042. /* Channel estimation fine tune */
  3043. { 0x1f, 0x0003 },
  3044. { 0x09, 0xa20f },
  3045. { 0x1f, 0x0000 },
  3046. /* Modify green table for giga & fnet */
  3047. { 0x1f, 0x0005 },
  3048. { 0x05, 0x8b55 },
  3049. { 0x06, 0x0000 },
  3050. { 0x05, 0x8b5e },
  3051. { 0x06, 0x0000 },
  3052. { 0x05, 0x8b67 },
  3053. { 0x06, 0x0000 },
  3054. { 0x05, 0x8b70 },
  3055. { 0x06, 0x0000 },
  3056. { 0x1f, 0x0000 },
  3057. { 0x1f, 0x0007 },
  3058. { 0x1e, 0x0078 },
  3059. { 0x17, 0x0000 },
  3060. { 0x19, 0x00fb },
  3061. { 0x1f, 0x0000 },
  3062. /* Modify green table for 10M */
  3063. { 0x1f, 0x0005 },
  3064. { 0x05, 0x8b79 },
  3065. { 0x06, 0xaa00 },
  3066. { 0x1f, 0x0000 },
  3067. /* Disable hiimpedance detection (RTCT) */
  3068. { 0x1f, 0x0003 },
  3069. { 0x01, 0x328a },
  3070. { 0x1f, 0x0000 }
  3071. };
  3072. rtl_apply_firmware(tp);
  3073. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  3074. rtl8168f_hw_phy_config(tp);
  3075. /* Improve 2-pair detection performance */
  3076. rtl_writephy(tp, 0x1f, 0x0005);
  3077. rtl_writephy(tp, 0x05, 0x8b85);
  3078. rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
  3079. rtl_writephy(tp, 0x1f, 0x0000);
  3080. }
  3081. static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
  3082. {
  3083. rtl_apply_firmware(tp);
  3084. rtl8168f_hw_phy_config(tp);
  3085. }
  3086. static void rtl8411_hw_phy_config(struct rtl8169_private *tp)
  3087. {
  3088. static const struct phy_reg phy_reg_init[] = {
  3089. /* Channel estimation fine tune */
  3090. { 0x1f, 0x0003 },
  3091. { 0x09, 0xa20f },
  3092. { 0x1f, 0x0000 },
  3093. /* Modify green table for giga & fnet */
  3094. { 0x1f, 0x0005 },
  3095. { 0x05, 0x8b55 },
  3096. { 0x06, 0x0000 },
  3097. { 0x05, 0x8b5e },
  3098. { 0x06, 0x0000 },
  3099. { 0x05, 0x8b67 },
  3100. { 0x06, 0x0000 },
  3101. { 0x05, 0x8b70 },
  3102. { 0x06, 0x0000 },
  3103. { 0x1f, 0x0000 },
  3104. { 0x1f, 0x0007 },
  3105. { 0x1e, 0x0078 },
  3106. { 0x17, 0x0000 },
  3107. { 0x19, 0x00aa },
  3108. { 0x1f, 0x0000 },
  3109. /* Modify green table for 10M */
  3110. { 0x1f, 0x0005 },
  3111. { 0x05, 0x8b79 },
  3112. { 0x06, 0xaa00 },
  3113. { 0x1f, 0x0000 },
  3114. /* Disable hiimpedance detection (RTCT) */
  3115. { 0x1f, 0x0003 },
  3116. { 0x01, 0x328a },
  3117. { 0x1f, 0x0000 }
  3118. };
  3119. rtl_apply_firmware(tp);
  3120. rtl8168f_hw_phy_config(tp);
  3121. /* Improve 2-pair detection performance */
  3122. rtl_writephy(tp, 0x1f, 0x0005);
  3123. rtl_writephy(tp, 0x05, 0x8b85);
  3124. rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
  3125. rtl_writephy(tp, 0x1f, 0x0000);
  3126. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  3127. /* Modify green table for giga */
  3128. rtl_writephy(tp, 0x1f, 0x0005);
  3129. rtl_writephy(tp, 0x05, 0x8b54);
  3130. rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
  3131. rtl_writephy(tp, 0x05, 0x8b5d);
  3132. rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
  3133. rtl_writephy(tp, 0x05, 0x8a7c);
  3134. rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
  3135. rtl_writephy(tp, 0x05, 0x8a7f);
  3136. rtl_w0w1_phy(tp, 0x06, 0x0100, 0x0000);
  3137. rtl_writephy(tp, 0x05, 0x8a82);
  3138. rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
  3139. rtl_writephy(tp, 0x05, 0x8a85);
  3140. rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
  3141. rtl_writephy(tp, 0x05, 0x8a88);
  3142. rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
  3143. rtl_writephy(tp, 0x1f, 0x0000);
  3144. /* uc same-seed solution */
  3145. rtl_writephy(tp, 0x1f, 0x0005);
  3146. rtl_writephy(tp, 0x05, 0x8b85);
  3147. rtl_w0w1_phy(tp, 0x06, 0x8000, 0x0000);
  3148. rtl_writephy(tp, 0x1f, 0x0000);
  3149. /* eee setting */
  3150. rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x00, 0x03, ERIAR_EXGMAC);
  3151. rtl_writephy(tp, 0x1f, 0x0005);
  3152. rtl_writephy(tp, 0x05, 0x8b85);
  3153. rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
  3154. rtl_writephy(tp, 0x1f, 0x0004);
  3155. rtl_writephy(tp, 0x1f, 0x0007);
  3156. rtl_writephy(tp, 0x1e, 0x0020);
  3157. rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0100);
  3158. rtl_writephy(tp, 0x1f, 0x0000);
  3159. rtl_writephy(tp, 0x0d, 0x0007);
  3160. rtl_writephy(tp, 0x0e, 0x003c);
  3161. rtl_writephy(tp, 0x0d, 0x4007);
  3162. rtl_writephy(tp, 0x0e, 0x0000);
  3163. rtl_writephy(tp, 0x0d, 0x0000);
  3164. /* Green feature */
  3165. rtl_writephy(tp, 0x1f, 0x0003);
  3166. rtl_w0w1_phy(tp, 0x19, 0x0000, 0x0001);
  3167. rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0400);
  3168. rtl_writephy(tp, 0x1f, 0x0000);
  3169. }
  3170. static void rtl8168g_1_hw_phy_config(struct rtl8169_private *tp)
  3171. {
  3172. rtl_apply_firmware(tp);
  3173. rtl_writephy(tp, 0x1f, 0x0a46);
  3174. if (rtl_readphy(tp, 0x10) & 0x0100) {
  3175. rtl_writephy(tp, 0x1f, 0x0bcc);
  3176. rtl_w0w1_phy(tp, 0x12, 0x0000, 0x8000);
  3177. } else {
  3178. rtl_writephy(tp, 0x1f, 0x0bcc);
  3179. rtl_w0w1_phy(tp, 0x12, 0x8000, 0x0000);
  3180. }
  3181. rtl_writephy(tp, 0x1f, 0x0a46);
  3182. if (rtl_readphy(tp, 0x13) & 0x0100) {
  3183. rtl_writephy(tp, 0x1f, 0x0c41);
  3184. rtl_w0w1_phy(tp, 0x15, 0x0002, 0x0000);
  3185. } else {
  3186. rtl_writephy(tp, 0x1f, 0x0c41);
  3187. rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0002);
  3188. }
  3189. /* Enable PHY auto speed down */
  3190. rtl_writephy(tp, 0x1f, 0x0a44);
  3191. rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
  3192. rtl_writephy(tp, 0x1f, 0x0bcc);
  3193. rtl_w0w1_phy(tp, 0x14, 0x0100, 0x0000);
  3194. rtl_writephy(tp, 0x1f, 0x0a44);
  3195. rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
  3196. rtl_writephy(tp, 0x1f, 0x0a43);
  3197. rtl_writephy(tp, 0x13, 0x8084);
  3198. rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
  3199. rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
  3200. /* EEE auto-fallback function */
  3201. rtl_writephy(tp, 0x1f, 0x0a4b);
  3202. rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
  3203. /* Enable UC LPF tune function */
  3204. rtl_writephy(tp, 0x1f, 0x0a43);
  3205. rtl_writephy(tp, 0x13, 0x8012);
  3206. rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
  3207. rtl_writephy(tp, 0x1f, 0x0c42);
  3208. rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
  3209. /* Improve SWR Efficiency */
  3210. rtl_writephy(tp, 0x1f, 0x0bcd);
  3211. rtl_writephy(tp, 0x14, 0x5065);
  3212. rtl_writephy(tp, 0x14, 0xd065);
  3213. rtl_writephy(tp, 0x1f, 0x0bc8);
  3214. rtl_writephy(tp, 0x11, 0x5655);
  3215. rtl_writephy(tp, 0x1f, 0x0bcd);
  3216. rtl_writephy(tp, 0x14, 0x1065);
  3217. rtl_writephy(tp, 0x14, 0x9065);
  3218. rtl_writephy(tp, 0x14, 0x1065);
  3219. /* Check ALDPS bit, disable it if enabled */
  3220. rtl_writephy(tp, 0x1f, 0x0a43);
  3221. if (rtl_readphy(tp, 0x10) & 0x0004)
  3222. rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
  3223. rtl_writephy(tp, 0x1f, 0x0000);
  3224. }
  3225. static void rtl8168g_2_hw_phy_config(struct rtl8169_private *tp)
  3226. {
  3227. rtl_apply_firmware(tp);
  3228. }
  3229. static void rtl8168h_1_hw_phy_config(struct rtl8169_private *tp)
  3230. {
  3231. u16 dout_tapbin;
  3232. u32 data;
  3233. rtl_apply_firmware(tp);
  3234. /* CHN EST parameters adjust - giga master */
  3235. rtl_writephy(tp, 0x1f, 0x0a43);
  3236. rtl_writephy(tp, 0x13, 0x809b);
  3237. rtl_w0w1_phy(tp, 0x14, 0x8000, 0xf800);
  3238. rtl_writephy(tp, 0x13, 0x80a2);
  3239. rtl_w0w1_phy(tp, 0x14, 0x8000, 0xff00);
  3240. rtl_writephy(tp, 0x13, 0x80a4);
  3241. rtl_w0w1_phy(tp, 0x14, 0x8500, 0xff00);
  3242. rtl_writephy(tp, 0x13, 0x809c);
  3243. rtl_w0w1_phy(tp, 0x14, 0xbd00, 0xff00);
  3244. rtl_writephy(tp, 0x1f, 0x0000);
  3245. /* CHN EST parameters adjust - giga slave */
  3246. rtl_writephy(tp, 0x1f, 0x0a43);
  3247. rtl_writephy(tp, 0x13, 0x80ad);
  3248. rtl_w0w1_phy(tp, 0x14, 0x7000, 0xf800);
  3249. rtl_writephy(tp, 0x13, 0x80b4);
  3250. rtl_w0w1_phy(tp, 0x14, 0x5000, 0xff00);
  3251. rtl_writephy(tp, 0x13, 0x80ac);
  3252. rtl_w0w1_phy(tp, 0x14, 0x4000, 0xff00);
  3253. rtl_writephy(tp, 0x1f, 0x0000);
  3254. /* CHN EST parameters adjust - fnet */
  3255. rtl_writephy(tp, 0x1f, 0x0a43);
  3256. rtl_writephy(tp, 0x13, 0x808e);
  3257. rtl_w0w1_phy(tp, 0x14, 0x1200, 0xff00);
  3258. rtl_writephy(tp, 0x13, 0x8090);
  3259. rtl_w0w1_phy(tp, 0x14, 0xe500, 0xff00);
  3260. rtl_writephy(tp, 0x13, 0x8092);
  3261. rtl_w0w1_phy(tp, 0x14, 0x9f00, 0xff00);
  3262. rtl_writephy(tp, 0x1f, 0x0000);
  3263. /* enable R-tune & PGA-retune function */
  3264. dout_tapbin = 0;
  3265. rtl_writephy(tp, 0x1f, 0x0a46);
  3266. data = rtl_readphy(tp, 0x13);
  3267. data &= 3;
  3268. data <<= 2;
  3269. dout_tapbin |= data;
  3270. data = rtl_readphy(tp, 0x12);
  3271. data &= 0xc000;
  3272. data >>= 14;
  3273. dout_tapbin |= data;
  3274. dout_tapbin = ~(dout_tapbin^0x08);
  3275. dout_tapbin <<= 12;
  3276. dout_tapbin &= 0xf000;
  3277. rtl_writephy(tp, 0x1f, 0x0a43);
  3278. rtl_writephy(tp, 0x13, 0x827a);
  3279. rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
  3280. rtl_writephy(tp, 0x13, 0x827b);
  3281. rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
  3282. rtl_writephy(tp, 0x13, 0x827c);
  3283. rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
  3284. rtl_writephy(tp, 0x13, 0x827d);
  3285. rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
  3286. rtl_writephy(tp, 0x1f, 0x0a43);
  3287. rtl_writephy(tp, 0x13, 0x0811);
  3288. rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
  3289. rtl_writephy(tp, 0x1f, 0x0a42);
  3290. rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
  3291. rtl_writephy(tp, 0x1f, 0x0000);
  3292. /* enable GPHY 10M */
  3293. rtl_writephy(tp, 0x1f, 0x0a44);
  3294. rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
  3295. rtl_writephy(tp, 0x1f, 0x0000);
  3296. /* SAR ADC performance */
  3297. rtl_writephy(tp, 0x1f, 0x0bca);
  3298. rtl_w0w1_phy(tp, 0x17, 0x4000, 0x3000);
  3299. rtl_writephy(tp, 0x1f, 0x0000);
  3300. rtl_writephy(tp, 0x1f, 0x0a43);
  3301. rtl_writephy(tp, 0x13, 0x803f);
  3302. rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
  3303. rtl_writephy(tp, 0x13, 0x8047);
  3304. rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
  3305. rtl_writephy(tp, 0x13, 0x804f);
  3306. rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
  3307. rtl_writephy(tp, 0x13, 0x8057);
  3308. rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
  3309. rtl_writephy(tp, 0x13, 0x805f);
  3310. rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
  3311. rtl_writephy(tp, 0x13, 0x8067);
  3312. rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
  3313. rtl_writephy(tp, 0x13, 0x806f);
  3314. rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
  3315. rtl_writephy(tp, 0x1f, 0x0000);
  3316. /* disable phy pfm mode */
  3317. rtl_writephy(tp, 0x1f, 0x0a44);
  3318. rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0080);
  3319. rtl_writephy(tp, 0x1f, 0x0000);
  3320. /* Check ALDPS bit, disable it if enabled */
  3321. rtl_writephy(tp, 0x1f, 0x0a43);
  3322. if (rtl_readphy(tp, 0x10) & 0x0004)
  3323. rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
  3324. rtl_writephy(tp, 0x1f, 0x0000);
  3325. }
  3326. static void rtl8168h_2_hw_phy_config(struct rtl8169_private *tp)
  3327. {
  3328. u16 ioffset_p3, ioffset_p2, ioffset_p1, ioffset_p0;
  3329. u16 rlen;
  3330. u32 data;
  3331. rtl_apply_firmware(tp);
  3332. /* CHIN EST parameter update */
  3333. rtl_writephy(tp, 0x1f, 0x0a43);
  3334. rtl_writephy(tp, 0x13, 0x808a);
  3335. rtl_w0w1_phy(tp, 0x14, 0x000a, 0x003f);
  3336. rtl_writephy(tp, 0x1f, 0x0000);
  3337. /* enable R-tune & PGA-retune function */
  3338. rtl_writephy(tp, 0x1f, 0x0a43);
  3339. rtl_writephy(tp, 0x13, 0x0811);
  3340. rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
  3341. rtl_writephy(tp, 0x1f, 0x0a42);
  3342. rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
  3343. rtl_writephy(tp, 0x1f, 0x0000);
  3344. /* enable GPHY 10M */
  3345. rtl_writephy(tp, 0x1f, 0x0a44);
  3346. rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
  3347. rtl_writephy(tp, 0x1f, 0x0000);
  3348. r8168_mac_ocp_write(tp, 0xdd02, 0x807d);
  3349. data = r8168_mac_ocp_read(tp, 0xdd02);
  3350. ioffset_p3 = ((data & 0x80)>>7);
  3351. ioffset_p3 <<= 3;
  3352. data = r8168_mac_ocp_read(tp, 0xdd00);
  3353. ioffset_p3 |= ((data & (0xe000))>>13);
  3354. ioffset_p2 = ((data & (0x1e00))>>9);
  3355. ioffset_p1 = ((data & (0x01e0))>>5);
  3356. ioffset_p0 = ((data & 0x0010)>>4);
  3357. ioffset_p0 <<= 3;
  3358. ioffset_p0 |= (data & (0x07));
  3359. data = (ioffset_p3<<12)|(ioffset_p2<<8)|(ioffset_p1<<4)|(ioffset_p0);
  3360. if ((ioffset_p3 != 0x0f) || (ioffset_p2 != 0x0f) ||
  3361. (ioffset_p1 != 0x0f) || (ioffset_p0 == 0x0f)) {
  3362. rtl_writephy(tp, 0x1f, 0x0bcf);
  3363. rtl_writephy(tp, 0x16, data);
  3364. rtl_writephy(tp, 0x1f, 0x0000);
  3365. }
  3366. /* Modify rlen (TX LPF corner frequency) level */
  3367. rtl_writephy(tp, 0x1f, 0x0bcd);
  3368. data = rtl_readphy(tp, 0x16);
  3369. data &= 0x000f;
  3370. rlen = 0;
  3371. if (data > 3)
  3372. rlen = data - 3;
  3373. data = rlen | (rlen<<4) | (rlen<<8) | (rlen<<12);
  3374. rtl_writephy(tp, 0x17, data);
  3375. rtl_writephy(tp, 0x1f, 0x0bcd);
  3376. rtl_writephy(tp, 0x1f, 0x0000);
  3377. /* disable phy pfm mode */
  3378. rtl_writephy(tp, 0x1f, 0x0a44);
  3379. rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0080);
  3380. rtl_writephy(tp, 0x1f, 0x0000);
  3381. /* Check ALDPS bit, disable it if enabled */
  3382. rtl_writephy(tp, 0x1f, 0x0a43);
  3383. if (rtl_readphy(tp, 0x10) & 0x0004)
  3384. rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
  3385. rtl_writephy(tp, 0x1f, 0x0000);
  3386. }
  3387. static void rtl8168ep_1_hw_phy_config(struct rtl8169_private *tp)
  3388. {
  3389. /* Enable PHY auto speed down */
  3390. rtl_writephy(tp, 0x1f, 0x0a44);
  3391. rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
  3392. rtl_writephy(tp, 0x1f, 0x0000);
  3393. /* patch 10M & ALDPS */
  3394. rtl_writephy(tp, 0x1f, 0x0bcc);
  3395. rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
  3396. rtl_writephy(tp, 0x1f, 0x0a44);
  3397. rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
  3398. rtl_writephy(tp, 0x1f, 0x0a43);
  3399. rtl_writephy(tp, 0x13, 0x8084);
  3400. rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
  3401. rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
  3402. rtl_writephy(tp, 0x1f, 0x0000);
  3403. /* Enable EEE auto-fallback function */
  3404. rtl_writephy(tp, 0x1f, 0x0a4b);
  3405. rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
  3406. rtl_writephy(tp, 0x1f, 0x0000);
  3407. /* Enable UC LPF tune function */
  3408. rtl_writephy(tp, 0x1f, 0x0a43);
  3409. rtl_writephy(tp, 0x13, 0x8012);
  3410. rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
  3411. rtl_writephy(tp, 0x1f, 0x0000);
  3412. /* set rg_sel_sdm_rate */
  3413. rtl_writephy(tp, 0x1f, 0x0c42);
  3414. rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
  3415. rtl_writephy(tp, 0x1f, 0x0000);
  3416. /* Check ALDPS bit, disable it if enabled */
  3417. rtl_writephy(tp, 0x1f, 0x0a43);
  3418. if (rtl_readphy(tp, 0x10) & 0x0004)
  3419. rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
  3420. rtl_writephy(tp, 0x1f, 0x0000);
  3421. }
  3422. static void rtl8168ep_2_hw_phy_config(struct rtl8169_private *tp)
  3423. {
  3424. /* patch 10M & ALDPS */
  3425. rtl_writephy(tp, 0x1f, 0x0bcc);
  3426. rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
  3427. rtl_writephy(tp, 0x1f, 0x0a44);
  3428. rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
  3429. rtl_writephy(tp, 0x1f, 0x0a43);
  3430. rtl_writephy(tp, 0x13, 0x8084);
  3431. rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
  3432. rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
  3433. rtl_writephy(tp, 0x1f, 0x0000);
  3434. /* Enable UC LPF tune function */
  3435. rtl_writephy(tp, 0x1f, 0x0a43);
  3436. rtl_writephy(tp, 0x13, 0x8012);
  3437. rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
  3438. rtl_writephy(tp, 0x1f, 0x0000);
  3439. /* Set rg_sel_sdm_rate */
  3440. rtl_writephy(tp, 0x1f, 0x0c42);
  3441. rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
  3442. rtl_writephy(tp, 0x1f, 0x0000);
  3443. /* Channel estimation parameters */
  3444. rtl_writephy(tp, 0x1f, 0x0a43);
  3445. rtl_writephy(tp, 0x13, 0x80f3);
  3446. rtl_w0w1_phy(tp, 0x14, 0x8b00, ~0x8bff);
  3447. rtl_writephy(tp, 0x13, 0x80f0);
  3448. rtl_w0w1_phy(tp, 0x14, 0x3a00, ~0x3aff);
  3449. rtl_writephy(tp, 0x13, 0x80ef);
  3450. rtl_w0w1_phy(tp, 0x14, 0x0500, ~0x05ff);
  3451. rtl_writephy(tp, 0x13, 0x80f6);
  3452. rtl_w0w1_phy(tp, 0x14, 0x6e00, ~0x6eff);
  3453. rtl_writephy(tp, 0x13, 0x80ec);
  3454. rtl_w0w1_phy(tp, 0x14, 0x6800, ~0x68ff);
  3455. rtl_writephy(tp, 0x13, 0x80ed);
  3456. rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
  3457. rtl_writephy(tp, 0x13, 0x80f2);
  3458. rtl_w0w1_phy(tp, 0x14, 0xf400, ~0xf4ff);
  3459. rtl_writephy(tp, 0x13, 0x80f4);
  3460. rtl_w0w1_phy(tp, 0x14, 0x8500, ~0x85ff);
  3461. rtl_writephy(tp, 0x1f, 0x0a43);
  3462. rtl_writephy(tp, 0x13, 0x8110);
  3463. rtl_w0w1_phy(tp, 0x14, 0xa800, ~0xa8ff);
  3464. rtl_writephy(tp, 0x13, 0x810f);
  3465. rtl_w0w1_phy(tp, 0x14, 0x1d00, ~0x1dff);
  3466. rtl_writephy(tp, 0x13, 0x8111);
  3467. rtl_w0w1_phy(tp, 0x14, 0xf500, ~0xf5ff);
  3468. rtl_writephy(tp, 0x13, 0x8113);
  3469. rtl_w0w1_phy(tp, 0x14, 0x6100, ~0x61ff);
  3470. rtl_writephy(tp, 0x13, 0x8115);
  3471. rtl_w0w1_phy(tp, 0x14, 0x9200, ~0x92ff);
  3472. rtl_writephy(tp, 0x13, 0x810e);
  3473. rtl_w0w1_phy(tp, 0x14, 0x0400, ~0x04ff);
  3474. rtl_writephy(tp, 0x13, 0x810c);
  3475. rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
  3476. rtl_writephy(tp, 0x13, 0x810b);
  3477. rtl_w0w1_phy(tp, 0x14, 0x5a00, ~0x5aff);
  3478. rtl_writephy(tp, 0x1f, 0x0a43);
  3479. rtl_writephy(tp, 0x13, 0x80d1);
  3480. rtl_w0w1_phy(tp, 0x14, 0xff00, ~0xffff);
  3481. rtl_writephy(tp, 0x13, 0x80cd);
  3482. rtl_w0w1_phy(tp, 0x14, 0x9e00, ~0x9eff);
  3483. rtl_writephy(tp, 0x13, 0x80d3);
  3484. rtl_w0w1_phy(tp, 0x14, 0x0e00, ~0x0eff);
  3485. rtl_writephy(tp, 0x13, 0x80d5);
  3486. rtl_w0w1_phy(tp, 0x14, 0xca00, ~0xcaff);
  3487. rtl_writephy(tp, 0x13, 0x80d7);
  3488. rtl_w0w1_phy(tp, 0x14, 0x8400, ~0x84ff);
  3489. /* Force PWM-mode */
  3490. rtl_writephy(tp, 0x1f, 0x0bcd);
  3491. rtl_writephy(tp, 0x14, 0x5065);
  3492. rtl_writephy(tp, 0x14, 0xd065);
  3493. rtl_writephy(tp, 0x1f, 0x0bc8);
  3494. rtl_writephy(tp, 0x12, 0x00ed);
  3495. rtl_writephy(tp, 0x1f, 0x0bcd);
  3496. rtl_writephy(tp, 0x14, 0x1065);
  3497. rtl_writephy(tp, 0x14, 0x9065);
  3498. rtl_writephy(tp, 0x14, 0x1065);
  3499. rtl_writephy(tp, 0x1f, 0x0000);
  3500. /* Check ALDPS bit, disable it if enabled */
  3501. rtl_writephy(tp, 0x1f, 0x0a43);
  3502. if (rtl_readphy(tp, 0x10) & 0x0004)
  3503. rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
  3504. rtl_writephy(tp, 0x1f, 0x0000);
  3505. }
  3506. static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
  3507. {
  3508. static const struct phy_reg phy_reg_init[] = {
  3509. { 0x1f, 0x0003 },
  3510. { 0x08, 0x441d },
  3511. { 0x01, 0x9100 },
  3512. { 0x1f, 0x0000 }
  3513. };
  3514. rtl_writephy(tp, 0x1f, 0x0000);
  3515. rtl_patchphy(tp, 0x11, 1 << 12);
  3516. rtl_patchphy(tp, 0x19, 1 << 13);
  3517. rtl_patchphy(tp, 0x10, 1 << 15);
  3518. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  3519. }
  3520. static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
  3521. {
  3522. static const struct phy_reg phy_reg_init[] = {
  3523. { 0x1f, 0x0005 },
  3524. { 0x1a, 0x0000 },
  3525. { 0x1f, 0x0000 },
  3526. { 0x1f, 0x0004 },
  3527. { 0x1c, 0x0000 },
  3528. { 0x1f, 0x0000 },
  3529. { 0x1f, 0x0001 },
  3530. { 0x15, 0x7701 },
  3531. { 0x1f, 0x0000 }
  3532. };
  3533. /* Disable ALDPS before ram code */
  3534. rtl_writephy(tp, 0x1f, 0x0000);
  3535. rtl_writephy(tp, 0x18, 0x0310);
  3536. msleep(100);
  3537. rtl_apply_firmware(tp);
  3538. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  3539. }
  3540. static void rtl8402_hw_phy_config(struct rtl8169_private *tp)
  3541. {
  3542. /* Disable ALDPS before setting firmware */
  3543. rtl_writephy(tp, 0x1f, 0x0000);
  3544. rtl_writephy(tp, 0x18, 0x0310);
  3545. msleep(20);
  3546. rtl_apply_firmware(tp);
  3547. /* EEE setting */
  3548. rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
  3549. rtl_writephy(tp, 0x1f, 0x0004);
  3550. rtl_writephy(tp, 0x10, 0x401f);
  3551. rtl_writephy(tp, 0x19, 0x7030);
  3552. rtl_writephy(tp, 0x1f, 0x0000);
  3553. }
  3554. static void rtl8106e_hw_phy_config(struct rtl8169_private *tp)
  3555. {
  3556. static const struct phy_reg phy_reg_init[] = {
  3557. { 0x1f, 0x0004 },
  3558. { 0x10, 0xc07f },
  3559. { 0x19, 0x7030 },
  3560. { 0x1f, 0x0000 }
  3561. };
  3562. /* Disable ALDPS before ram code */
  3563. rtl_writephy(tp, 0x1f, 0x0000);
  3564. rtl_writephy(tp, 0x18, 0x0310);
  3565. msleep(100);
  3566. rtl_apply_firmware(tp);
  3567. rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
  3568. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  3569. rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
  3570. }
  3571. static void rtl_hw_phy_config(struct net_device *dev)
  3572. {
  3573. struct rtl8169_private *tp = netdev_priv(dev);
  3574. rtl8169_print_mac_version(tp);
  3575. switch (tp->mac_version) {
  3576. case RTL_GIGA_MAC_VER_01:
  3577. break;
  3578. case RTL_GIGA_MAC_VER_02:
  3579. case RTL_GIGA_MAC_VER_03:
  3580. rtl8169s_hw_phy_config(tp);
  3581. break;
  3582. case RTL_GIGA_MAC_VER_04:
  3583. rtl8169sb_hw_phy_config(tp);
  3584. break;
  3585. case RTL_GIGA_MAC_VER_05:
  3586. rtl8169scd_hw_phy_config(tp);
  3587. break;
  3588. case RTL_GIGA_MAC_VER_06:
  3589. rtl8169sce_hw_phy_config(tp);
  3590. break;
  3591. case RTL_GIGA_MAC_VER_07:
  3592. case RTL_GIGA_MAC_VER_08:
  3593. case RTL_GIGA_MAC_VER_09:
  3594. rtl8102e_hw_phy_config(tp);
  3595. break;
  3596. case RTL_GIGA_MAC_VER_11:
  3597. rtl8168bb_hw_phy_config(tp);
  3598. break;
  3599. case RTL_GIGA_MAC_VER_12:
  3600. rtl8168bef_hw_phy_config(tp);
  3601. break;
  3602. case RTL_GIGA_MAC_VER_17:
  3603. rtl8168bef_hw_phy_config(tp);
  3604. break;
  3605. case RTL_GIGA_MAC_VER_18:
  3606. rtl8168cp_1_hw_phy_config(tp);
  3607. break;
  3608. case RTL_GIGA_MAC_VER_19:
  3609. rtl8168c_1_hw_phy_config(tp);
  3610. break;
  3611. case RTL_GIGA_MAC_VER_20:
  3612. rtl8168c_2_hw_phy_config(tp);
  3613. break;
  3614. case RTL_GIGA_MAC_VER_21:
  3615. rtl8168c_3_hw_phy_config(tp);
  3616. break;
  3617. case RTL_GIGA_MAC_VER_22:
  3618. rtl8168c_4_hw_phy_config(tp);
  3619. break;
  3620. case RTL_GIGA_MAC_VER_23:
  3621. case RTL_GIGA_MAC_VER_24:
  3622. rtl8168cp_2_hw_phy_config(tp);
  3623. break;
  3624. case RTL_GIGA_MAC_VER_25:
  3625. rtl8168d_1_hw_phy_config(tp);
  3626. break;
  3627. case RTL_GIGA_MAC_VER_26:
  3628. rtl8168d_2_hw_phy_config(tp);
  3629. break;
  3630. case RTL_GIGA_MAC_VER_27:
  3631. rtl8168d_3_hw_phy_config(tp);
  3632. break;
  3633. case RTL_GIGA_MAC_VER_28:
  3634. rtl8168d_4_hw_phy_config(tp);
  3635. break;
  3636. case RTL_GIGA_MAC_VER_29:
  3637. case RTL_GIGA_MAC_VER_30:
  3638. rtl8105e_hw_phy_config(tp);
  3639. break;
  3640. case RTL_GIGA_MAC_VER_31:
  3641. /* None. */
  3642. break;
  3643. case RTL_GIGA_MAC_VER_32:
  3644. case RTL_GIGA_MAC_VER_33:
  3645. rtl8168e_1_hw_phy_config(tp);
  3646. break;
  3647. case RTL_GIGA_MAC_VER_34:
  3648. rtl8168e_2_hw_phy_config(tp);
  3649. break;
  3650. case RTL_GIGA_MAC_VER_35:
  3651. rtl8168f_1_hw_phy_config(tp);
  3652. break;
  3653. case RTL_GIGA_MAC_VER_36:
  3654. rtl8168f_2_hw_phy_config(tp);
  3655. break;
  3656. case RTL_GIGA_MAC_VER_37:
  3657. rtl8402_hw_phy_config(tp);
  3658. break;
  3659. case RTL_GIGA_MAC_VER_38:
  3660. rtl8411_hw_phy_config(tp);
  3661. break;
  3662. case RTL_GIGA_MAC_VER_39:
  3663. rtl8106e_hw_phy_config(tp);
  3664. break;
  3665. case RTL_GIGA_MAC_VER_40:
  3666. rtl8168g_1_hw_phy_config(tp);
  3667. break;
  3668. case RTL_GIGA_MAC_VER_42:
  3669. case RTL_GIGA_MAC_VER_43:
  3670. case RTL_GIGA_MAC_VER_44:
  3671. rtl8168g_2_hw_phy_config(tp);
  3672. break;
  3673. case RTL_GIGA_MAC_VER_45:
  3674. case RTL_GIGA_MAC_VER_47:
  3675. rtl8168h_1_hw_phy_config(tp);
  3676. break;
  3677. case RTL_GIGA_MAC_VER_46:
  3678. case RTL_GIGA_MAC_VER_48:
  3679. rtl8168h_2_hw_phy_config(tp);
  3680. break;
  3681. case RTL_GIGA_MAC_VER_49:
  3682. rtl8168ep_1_hw_phy_config(tp);
  3683. break;
  3684. case RTL_GIGA_MAC_VER_50:
  3685. case RTL_GIGA_MAC_VER_51:
  3686. rtl8168ep_2_hw_phy_config(tp);
  3687. break;
  3688. case RTL_GIGA_MAC_VER_41:
  3689. default:
  3690. break;
  3691. }
  3692. }
  3693. static void rtl_phy_work(struct rtl8169_private *tp)
  3694. {
  3695. struct timer_list *timer = &tp->timer;
  3696. void __iomem *ioaddr = tp->mmio_addr;
  3697. unsigned long timeout = RTL8169_PHY_TIMEOUT;
  3698. assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
  3699. if (tp->phy_reset_pending(tp)) {
  3700. /*
  3701. * A busy loop could burn quite a few cycles on nowadays CPU.
  3702. * Let's delay the execution of the timer for a few ticks.
  3703. */
  3704. timeout = HZ/10;
  3705. goto out_mod_timer;
  3706. }
  3707. if (tp->link_ok(ioaddr))
  3708. return;
  3709. netif_dbg(tp, link, tp->dev, "PHY reset until link up\n");
  3710. tp->phy_reset_enable(tp);
  3711. out_mod_timer:
  3712. mod_timer(timer, jiffies + timeout);
  3713. }
  3714. static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
  3715. {
  3716. if (!test_and_set_bit(flag, tp->wk.flags))
  3717. schedule_work(&tp->wk.work);
  3718. }
  3719. static void rtl8169_phy_timer(unsigned long __opaque)
  3720. {
  3721. struct net_device *dev = (struct net_device *)__opaque;
  3722. struct rtl8169_private *tp = netdev_priv(dev);
  3723. rtl_schedule_task(tp, RTL_FLAG_TASK_PHY_PENDING);
  3724. }
  3725. static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
  3726. void __iomem *ioaddr)
  3727. {
  3728. iounmap(ioaddr);
  3729. pci_release_regions(pdev);
  3730. pci_clear_mwi(pdev);
  3731. pci_disable_device(pdev);
  3732. free_netdev(dev);
  3733. }
  3734. DECLARE_RTL_COND(rtl_phy_reset_cond)
  3735. {
  3736. return tp->phy_reset_pending(tp);
  3737. }
  3738. static void rtl8169_phy_reset(struct net_device *dev,
  3739. struct rtl8169_private *tp)
  3740. {
  3741. tp->phy_reset_enable(tp);
  3742. rtl_msleep_loop_wait_low(tp, &rtl_phy_reset_cond, 1, 100);
  3743. }
  3744. static bool rtl_tbi_enabled(struct rtl8169_private *tp)
  3745. {
  3746. void __iomem *ioaddr = tp->mmio_addr;
  3747. return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
  3748. (RTL_R8(PHYstatus) & TBI_Enable);
  3749. }
  3750. static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
  3751. {
  3752. void __iomem *ioaddr = tp->mmio_addr;
  3753. rtl_hw_phy_config(dev);
  3754. if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
  3755. dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
  3756. RTL_W8(0x82, 0x01);
  3757. }
  3758. pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
  3759. if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
  3760. pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
  3761. if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
  3762. dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
  3763. RTL_W8(0x82, 0x01);
  3764. dprintk("Set PHY Reg 0x0bh = 0x00h\n");
  3765. rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
  3766. }
  3767. rtl8169_phy_reset(dev, tp);
  3768. rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
  3769. ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
  3770. ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
  3771. (tp->mii.supports_gmii ?
  3772. ADVERTISED_1000baseT_Half |
  3773. ADVERTISED_1000baseT_Full : 0));
  3774. if (rtl_tbi_enabled(tp))
  3775. netif_info(tp, link, dev, "TBI auto-negotiating\n");
  3776. }
  3777. static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
  3778. {
  3779. void __iomem *ioaddr = tp->mmio_addr;
  3780. rtl_lock_work(tp);
  3781. RTL_W8(Cfg9346, Cfg9346_Unlock);
  3782. RTL_W32(MAC4, addr[4] | addr[5] << 8);
  3783. RTL_R32(MAC4);
  3784. RTL_W32(MAC0, addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
  3785. RTL_R32(MAC0);
  3786. if (tp->mac_version == RTL_GIGA_MAC_VER_34)
  3787. rtl_rar_exgmac_set(tp, addr);
  3788. RTL_W8(Cfg9346, Cfg9346_Lock);
  3789. rtl_unlock_work(tp);
  3790. }
  3791. static int rtl_set_mac_address(struct net_device *dev, void *p)
  3792. {
  3793. struct rtl8169_private *tp = netdev_priv(dev);
  3794. struct sockaddr *addr = p;
  3795. if (!is_valid_ether_addr(addr->sa_data))
  3796. return -EADDRNOTAVAIL;
  3797. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  3798. rtl_rar_set(tp, dev->dev_addr);
  3799. return 0;
  3800. }
  3801. static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  3802. {
  3803. struct rtl8169_private *tp = netdev_priv(dev);
  3804. struct mii_ioctl_data *data = if_mii(ifr);
  3805. return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
  3806. }
  3807. static int rtl_xmii_ioctl(struct rtl8169_private *tp,
  3808. struct mii_ioctl_data *data, int cmd)
  3809. {
  3810. switch (cmd) {
  3811. case SIOCGMIIPHY:
  3812. data->phy_id = 32; /* Internal PHY */
  3813. return 0;
  3814. case SIOCGMIIREG:
  3815. data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
  3816. return 0;
  3817. case SIOCSMIIREG:
  3818. rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
  3819. return 0;
  3820. }
  3821. return -EOPNOTSUPP;
  3822. }
  3823. static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
  3824. {
  3825. return -EOPNOTSUPP;
  3826. }
  3827. static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
  3828. {
  3829. if (tp->features & RTL_FEATURE_MSI) {
  3830. pci_disable_msi(pdev);
  3831. tp->features &= ~RTL_FEATURE_MSI;
  3832. }
  3833. }
  3834. static void rtl_init_mdio_ops(struct rtl8169_private *tp)
  3835. {
  3836. struct mdio_ops *ops = &tp->mdio_ops;
  3837. switch (tp->mac_version) {
  3838. case RTL_GIGA_MAC_VER_27:
  3839. ops->write = r8168dp_1_mdio_write;
  3840. ops->read = r8168dp_1_mdio_read;
  3841. break;
  3842. case RTL_GIGA_MAC_VER_28:
  3843. case RTL_GIGA_MAC_VER_31:
  3844. ops->write = r8168dp_2_mdio_write;
  3845. ops->read = r8168dp_2_mdio_read;
  3846. break;
  3847. case RTL_GIGA_MAC_VER_40:
  3848. case RTL_GIGA_MAC_VER_41:
  3849. case RTL_GIGA_MAC_VER_42:
  3850. case RTL_GIGA_MAC_VER_43:
  3851. case RTL_GIGA_MAC_VER_44:
  3852. case RTL_GIGA_MAC_VER_45:
  3853. case RTL_GIGA_MAC_VER_46:
  3854. case RTL_GIGA_MAC_VER_47:
  3855. case RTL_GIGA_MAC_VER_48:
  3856. case RTL_GIGA_MAC_VER_49:
  3857. case RTL_GIGA_MAC_VER_50:
  3858. case RTL_GIGA_MAC_VER_51:
  3859. ops->write = r8168g_mdio_write;
  3860. ops->read = r8168g_mdio_read;
  3861. break;
  3862. default:
  3863. ops->write = r8169_mdio_write;
  3864. ops->read = r8169_mdio_read;
  3865. break;
  3866. }
  3867. }
  3868. static void rtl_speed_down(struct rtl8169_private *tp)
  3869. {
  3870. u32 adv;
  3871. int lpa;
  3872. rtl_writephy(tp, 0x1f, 0x0000);
  3873. lpa = rtl_readphy(tp, MII_LPA);
  3874. if (lpa & (LPA_10HALF | LPA_10FULL))
  3875. adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full;
  3876. else if (lpa & (LPA_100HALF | LPA_100FULL))
  3877. adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
  3878. ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full;
  3879. else
  3880. adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
  3881. ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
  3882. (tp->mii.supports_gmii ?
  3883. ADVERTISED_1000baseT_Half |
  3884. ADVERTISED_1000baseT_Full : 0);
  3885. rtl8169_set_speed(tp->dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
  3886. adv);
  3887. }
  3888. static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
  3889. {
  3890. void __iomem *ioaddr = tp->mmio_addr;
  3891. switch (tp->mac_version) {
  3892. case RTL_GIGA_MAC_VER_25:
  3893. case RTL_GIGA_MAC_VER_26:
  3894. case RTL_GIGA_MAC_VER_29:
  3895. case RTL_GIGA_MAC_VER_30:
  3896. case RTL_GIGA_MAC_VER_32:
  3897. case RTL_GIGA_MAC_VER_33:
  3898. case RTL_GIGA_MAC_VER_34:
  3899. case RTL_GIGA_MAC_VER_37:
  3900. case RTL_GIGA_MAC_VER_38:
  3901. case RTL_GIGA_MAC_VER_39:
  3902. case RTL_GIGA_MAC_VER_40:
  3903. case RTL_GIGA_MAC_VER_41:
  3904. case RTL_GIGA_MAC_VER_42:
  3905. case RTL_GIGA_MAC_VER_43:
  3906. case RTL_GIGA_MAC_VER_44:
  3907. case RTL_GIGA_MAC_VER_45:
  3908. case RTL_GIGA_MAC_VER_46:
  3909. case RTL_GIGA_MAC_VER_47:
  3910. case RTL_GIGA_MAC_VER_48:
  3911. case RTL_GIGA_MAC_VER_49:
  3912. case RTL_GIGA_MAC_VER_50:
  3913. case RTL_GIGA_MAC_VER_51:
  3914. RTL_W32(RxConfig, RTL_R32(RxConfig) |
  3915. AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
  3916. break;
  3917. default:
  3918. break;
  3919. }
  3920. }
  3921. static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
  3922. {
  3923. if (!(__rtl8169_get_wol(tp) & WAKE_ANY))
  3924. return false;
  3925. rtl_speed_down(tp);
  3926. rtl_wol_suspend_quirk(tp);
  3927. return true;
  3928. }
  3929. static void r810x_phy_power_down(struct rtl8169_private *tp)
  3930. {
  3931. rtl_writephy(tp, 0x1f, 0x0000);
  3932. rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
  3933. }
  3934. static void r810x_phy_power_up(struct rtl8169_private *tp)
  3935. {
  3936. rtl_writephy(tp, 0x1f, 0x0000);
  3937. rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
  3938. }
  3939. static void r810x_pll_power_down(struct rtl8169_private *tp)
  3940. {
  3941. void __iomem *ioaddr = tp->mmio_addr;
  3942. if (rtl_wol_pll_power_down(tp))
  3943. return;
  3944. r810x_phy_power_down(tp);
  3945. switch (tp->mac_version) {
  3946. case RTL_GIGA_MAC_VER_07:
  3947. case RTL_GIGA_MAC_VER_08:
  3948. case RTL_GIGA_MAC_VER_09:
  3949. case RTL_GIGA_MAC_VER_10:
  3950. case RTL_GIGA_MAC_VER_13:
  3951. case RTL_GIGA_MAC_VER_16:
  3952. break;
  3953. default:
  3954. RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
  3955. break;
  3956. }
  3957. }
  3958. static void r810x_pll_power_up(struct rtl8169_private *tp)
  3959. {
  3960. void __iomem *ioaddr = tp->mmio_addr;
  3961. r810x_phy_power_up(tp);
  3962. switch (tp->mac_version) {
  3963. case RTL_GIGA_MAC_VER_07:
  3964. case RTL_GIGA_MAC_VER_08:
  3965. case RTL_GIGA_MAC_VER_09:
  3966. case RTL_GIGA_MAC_VER_10:
  3967. case RTL_GIGA_MAC_VER_13:
  3968. case RTL_GIGA_MAC_VER_16:
  3969. break;
  3970. case RTL_GIGA_MAC_VER_47:
  3971. case RTL_GIGA_MAC_VER_48:
  3972. RTL_W8(PMCH, RTL_R8(PMCH) | 0xc0);
  3973. break;
  3974. default:
  3975. RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
  3976. break;
  3977. }
  3978. }
  3979. static void r8168_phy_power_up(struct rtl8169_private *tp)
  3980. {
  3981. rtl_writephy(tp, 0x1f, 0x0000);
  3982. switch (tp->mac_version) {
  3983. case RTL_GIGA_MAC_VER_11:
  3984. case RTL_GIGA_MAC_VER_12:
  3985. case RTL_GIGA_MAC_VER_17:
  3986. case RTL_GIGA_MAC_VER_18:
  3987. case RTL_GIGA_MAC_VER_19:
  3988. case RTL_GIGA_MAC_VER_20:
  3989. case RTL_GIGA_MAC_VER_21:
  3990. case RTL_GIGA_MAC_VER_22:
  3991. case RTL_GIGA_MAC_VER_23:
  3992. case RTL_GIGA_MAC_VER_24:
  3993. case RTL_GIGA_MAC_VER_25:
  3994. case RTL_GIGA_MAC_VER_26:
  3995. case RTL_GIGA_MAC_VER_27:
  3996. case RTL_GIGA_MAC_VER_28:
  3997. case RTL_GIGA_MAC_VER_31:
  3998. rtl_writephy(tp, 0x0e, 0x0000);
  3999. break;
  4000. default:
  4001. break;
  4002. }
  4003. rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
  4004. }
  4005. static void r8168_phy_power_down(struct rtl8169_private *tp)
  4006. {
  4007. rtl_writephy(tp, 0x1f, 0x0000);
  4008. switch (tp->mac_version) {
  4009. case RTL_GIGA_MAC_VER_32:
  4010. case RTL_GIGA_MAC_VER_33:
  4011. case RTL_GIGA_MAC_VER_40:
  4012. case RTL_GIGA_MAC_VER_41:
  4013. rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
  4014. break;
  4015. case RTL_GIGA_MAC_VER_11:
  4016. case RTL_GIGA_MAC_VER_12:
  4017. case RTL_GIGA_MAC_VER_17:
  4018. case RTL_GIGA_MAC_VER_18:
  4019. case RTL_GIGA_MAC_VER_19:
  4020. case RTL_GIGA_MAC_VER_20:
  4021. case RTL_GIGA_MAC_VER_21:
  4022. case RTL_GIGA_MAC_VER_22:
  4023. case RTL_GIGA_MAC_VER_23:
  4024. case RTL_GIGA_MAC_VER_24:
  4025. case RTL_GIGA_MAC_VER_25:
  4026. case RTL_GIGA_MAC_VER_26:
  4027. case RTL_GIGA_MAC_VER_27:
  4028. case RTL_GIGA_MAC_VER_28:
  4029. case RTL_GIGA_MAC_VER_31:
  4030. rtl_writephy(tp, 0x0e, 0x0200);
  4031. default:
  4032. rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
  4033. break;
  4034. }
  4035. }
  4036. static void r8168_pll_power_down(struct rtl8169_private *tp)
  4037. {
  4038. void __iomem *ioaddr = tp->mmio_addr;
  4039. if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
  4040. tp->mac_version == RTL_GIGA_MAC_VER_28 ||
  4041. tp->mac_version == RTL_GIGA_MAC_VER_31 ||
  4042. tp->mac_version == RTL_GIGA_MAC_VER_49 ||
  4043. tp->mac_version == RTL_GIGA_MAC_VER_50 ||
  4044. tp->mac_version == RTL_GIGA_MAC_VER_51) &&
  4045. r8168_check_dash(tp)) {
  4046. return;
  4047. }
  4048. if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
  4049. tp->mac_version == RTL_GIGA_MAC_VER_24) &&
  4050. (RTL_R16(CPlusCmd) & ASF)) {
  4051. return;
  4052. }
  4053. if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
  4054. tp->mac_version == RTL_GIGA_MAC_VER_33)
  4055. rtl_ephy_write(tp, 0x19, 0xff64);
  4056. if (rtl_wol_pll_power_down(tp))
  4057. return;
  4058. r8168_phy_power_down(tp);
  4059. switch (tp->mac_version) {
  4060. case RTL_GIGA_MAC_VER_25:
  4061. case RTL_GIGA_MAC_VER_26:
  4062. case RTL_GIGA_MAC_VER_27:
  4063. case RTL_GIGA_MAC_VER_28:
  4064. case RTL_GIGA_MAC_VER_31:
  4065. case RTL_GIGA_MAC_VER_32:
  4066. case RTL_GIGA_MAC_VER_33:
  4067. case RTL_GIGA_MAC_VER_44:
  4068. case RTL_GIGA_MAC_VER_45:
  4069. case RTL_GIGA_MAC_VER_46:
  4070. case RTL_GIGA_MAC_VER_50:
  4071. case RTL_GIGA_MAC_VER_51:
  4072. RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
  4073. break;
  4074. case RTL_GIGA_MAC_VER_40:
  4075. case RTL_GIGA_MAC_VER_41:
  4076. case RTL_GIGA_MAC_VER_49:
  4077. rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0x00000000,
  4078. 0xfc000000, ERIAR_EXGMAC);
  4079. RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
  4080. break;
  4081. }
  4082. }
  4083. static void r8168_pll_power_up(struct rtl8169_private *tp)
  4084. {
  4085. void __iomem *ioaddr = tp->mmio_addr;
  4086. switch (tp->mac_version) {
  4087. case RTL_GIGA_MAC_VER_25:
  4088. case RTL_GIGA_MAC_VER_26:
  4089. case RTL_GIGA_MAC_VER_27:
  4090. case RTL_GIGA_MAC_VER_28:
  4091. case RTL_GIGA_MAC_VER_31:
  4092. case RTL_GIGA_MAC_VER_32:
  4093. case RTL_GIGA_MAC_VER_33:
  4094. RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
  4095. break;
  4096. case RTL_GIGA_MAC_VER_44:
  4097. case RTL_GIGA_MAC_VER_45:
  4098. case RTL_GIGA_MAC_VER_46:
  4099. case RTL_GIGA_MAC_VER_50:
  4100. case RTL_GIGA_MAC_VER_51:
  4101. RTL_W8(PMCH, RTL_R8(PMCH) | 0xc0);
  4102. break;
  4103. case RTL_GIGA_MAC_VER_40:
  4104. case RTL_GIGA_MAC_VER_41:
  4105. case RTL_GIGA_MAC_VER_49:
  4106. RTL_W8(PMCH, RTL_R8(PMCH) | 0xc0);
  4107. rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0xfc000000,
  4108. 0x00000000, ERIAR_EXGMAC);
  4109. break;
  4110. }
  4111. r8168_phy_power_up(tp);
  4112. }
  4113. static void rtl_generic_op(struct rtl8169_private *tp,
  4114. void (*op)(struct rtl8169_private *))
  4115. {
  4116. if (op)
  4117. op(tp);
  4118. }
  4119. static void rtl_pll_power_down(struct rtl8169_private *tp)
  4120. {
  4121. rtl_generic_op(tp, tp->pll_power_ops.down);
  4122. }
  4123. static void rtl_pll_power_up(struct rtl8169_private *tp)
  4124. {
  4125. rtl_generic_op(tp, tp->pll_power_ops.up);
  4126. }
  4127. static void rtl_init_pll_power_ops(struct rtl8169_private *tp)
  4128. {
  4129. struct pll_power_ops *ops = &tp->pll_power_ops;
  4130. switch (tp->mac_version) {
  4131. case RTL_GIGA_MAC_VER_07:
  4132. case RTL_GIGA_MAC_VER_08:
  4133. case RTL_GIGA_MAC_VER_09:
  4134. case RTL_GIGA_MAC_VER_10:
  4135. case RTL_GIGA_MAC_VER_16:
  4136. case RTL_GIGA_MAC_VER_29:
  4137. case RTL_GIGA_MAC_VER_30:
  4138. case RTL_GIGA_MAC_VER_37:
  4139. case RTL_GIGA_MAC_VER_39:
  4140. case RTL_GIGA_MAC_VER_43:
  4141. case RTL_GIGA_MAC_VER_47:
  4142. case RTL_GIGA_MAC_VER_48:
  4143. ops->down = r810x_pll_power_down;
  4144. ops->up = r810x_pll_power_up;
  4145. break;
  4146. case RTL_GIGA_MAC_VER_11:
  4147. case RTL_GIGA_MAC_VER_12:
  4148. case RTL_GIGA_MAC_VER_17:
  4149. case RTL_GIGA_MAC_VER_18:
  4150. case RTL_GIGA_MAC_VER_19:
  4151. case RTL_GIGA_MAC_VER_20:
  4152. case RTL_GIGA_MAC_VER_21:
  4153. case RTL_GIGA_MAC_VER_22:
  4154. case RTL_GIGA_MAC_VER_23:
  4155. case RTL_GIGA_MAC_VER_24:
  4156. case RTL_GIGA_MAC_VER_25:
  4157. case RTL_GIGA_MAC_VER_26:
  4158. case RTL_GIGA_MAC_VER_27:
  4159. case RTL_GIGA_MAC_VER_28:
  4160. case RTL_GIGA_MAC_VER_31:
  4161. case RTL_GIGA_MAC_VER_32:
  4162. case RTL_GIGA_MAC_VER_33:
  4163. case RTL_GIGA_MAC_VER_34:
  4164. case RTL_GIGA_MAC_VER_35:
  4165. case RTL_GIGA_MAC_VER_36:
  4166. case RTL_GIGA_MAC_VER_38:
  4167. case RTL_GIGA_MAC_VER_40:
  4168. case RTL_GIGA_MAC_VER_41:
  4169. case RTL_GIGA_MAC_VER_42:
  4170. case RTL_GIGA_MAC_VER_44:
  4171. case RTL_GIGA_MAC_VER_45:
  4172. case RTL_GIGA_MAC_VER_46:
  4173. case RTL_GIGA_MAC_VER_49:
  4174. case RTL_GIGA_MAC_VER_50:
  4175. case RTL_GIGA_MAC_VER_51:
  4176. ops->down = r8168_pll_power_down;
  4177. ops->up = r8168_pll_power_up;
  4178. break;
  4179. default:
  4180. ops->down = NULL;
  4181. ops->up = NULL;
  4182. break;
  4183. }
  4184. }
  4185. static void rtl_init_rxcfg(struct rtl8169_private *tp)
  4186. {
  4187. void __iomem *ioaddr = tp->mmio_addr;
  4188. switch (tp->mac_version) {
  4189. case RTL_GIGA_MAC_VER_01:
  4190. case RTL_GIGA_MAC_VER_02:
  4191. case RTL_GIGA_MAC_VER_03:
  4192. case RTL_GIGA_MAC_VER_04:
  4193. case RTL_GIGA_MAC_VER_05:
  4194. case RTL_GIGA_MAC_VER_06:
  4195. case RTL_GIGA_MAC_VER_10:
  4196. case RTL_GIGA_MAC_VER_11:
  4197. case RTL_GIGA_MAC_VER_12:
  4198. case RTL_GIGA_MAC_VER_13:
  4199. case RTL_GIGA_MAC_VER_14:
  4200. case RTL_GIGA_MAC_VER_15:
  4201. case RTL_GIGA_MAC_VER_16:
  4202. case RTL_GIGA_MAC_VER_17:
  4203. RTL_W32(RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
  4204. break;
  4205. case RTL_GIGA_MAC_VER_18:
  4206. case RTL_GIGA_MAC_VER_19:
  4207. case RTL_GIGA_MAC_VER_20:
  4208. case RTL_GIGA_MAC_VER_21:
  4209. case RTL_GIGA_MAC_VER_22:
  4210. case RTL_GIGA_MAC_VER_23:
  4211. case RTL_GIGA_MAC_VER_24:
  4212. case RTL_GIGA_MAC_VER_34:
  4213. case RTL_GIGA_MAC_VER_35:
  4214. RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
  4215. break;
  4216. case RTL_GIGA_MAC_VER_40:
  4217. RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST | RX_EARLY_OFF);
  4218. break;
  4219. case RTL_GIGA_MAC_VER_41:
  4220. case RTL_GIGA_MAC_VER_42:
  4221. case RTL_GIGA_MAC_VER_43:
  4222. case RTL_GIGA_MAC_VER_44:
  4223. case RTL_GIGA_MAC_VER_45:
  4224. case RTL_GIGA_MAC_VER_46:
  4225. case RTL_GIGA_MAC_VER_47:
  4226. case RTL_GIGA_MAC_VER_48:
  4227. RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST | RX_EARLY_OFF);
  4228. break;
  4229. case RTL_GIGA_MAC_VER_49:
  4230. case RTL_GIGA_MAC_VER_50:
  4231. case RTL_GIGA_MAC_VER_51:
  4232. RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST | RX_EARLY_OFF);
  4233. break;
  4234. default:
  4235. RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST);
  4236. break;
  4237. }
  4238. }
  4239. static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
  4240. {
  4241. tp->dirty_tx = tp->cur_tx = tp->cur_rx = 0;
  4242. }
  4243. static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
  4244. {
  4245. void __iomem *ioaddr = tp->mmio_addr;
  4246. RTL_W8(Cfg9346, Cfg9346_Unlock);
  4247. rtl_generic_op(tp, tp->jumbo_ops.enable);
  4248. RTL_W8(Cfg9346, Cfg9346_Lock);
  4249. }
  4250. static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
  4251. {
  4252. void __iomem *ioaddr = tp->mmio_addr;
  4253. RTL_W8(Cfg9346, Cfg9346_Unlock);
  4254. rtl_generic_op(tp, tp->jumbo_ops.disable);
  4255. RTL_W8(Cfg9346, Cfg9346_Lock);
  4256. }
  4257. static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
  4258. {
  4259. void __iomem *ioaddr = tp->mmio_addr;
  4260. RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
  4261. RTL_W8(Config4, RTL_R8(Config4) | Jumbo_En1);
  4262. rtl_tx_performance_tweak(tp->pci_dev, PCI_EXP_DEVCTL_READRQ_512B);
  4263. }
  4264. static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
  4265. {
  4266. void __iomem *ioaddr = tp->mmio_addr;
  4267. RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
  4268. RTL_W8(Config4, RTL_R8(Config4) & ~Jumbo_En1);
  4269. rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
  4270. }
  4271. static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
  4272. {
  4273. void __iomem *ioaddr = tp->mmio_addr;
  4274. RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
  4275. }
  4276. static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
  4277. {
  4278. void __iomem *ioaddr = tp->mmio_addr;
  4279. RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
  4280. }
  4281. static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
  4282. {
  4283. void __iomem *ioaddr = tp->mmio_addr;
  4284. RTL_W8(MaxTxPacketSize, 0x3f);
  4285. RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
  4286. RTL_W8(Config4, RTL_R8(Config4) | 0x01);
  4287. rtl_tx_performance_tweak(tp->pci_dev, PCI_EXP_DEVCTL_READRQ_512B);
  4288. }
  4289. static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
  4290. {
  4291. void __iomem *ioaddr = tp->mmio_addr;
  4292. RTL_W8(MaxTxPacketSize, 0x0c);
  4293. RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
  4294. RTL_W8(Config4, RTL_R8(Config4) & ~0x01);
  4295. rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
  4296. }
  4297. static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
  4298. {
  4299. rtl_tx_performance_tweak(tp->pci_dev,
  4300. PCI_EXP_DEVCTL_READRQ_512B | PCI_EXP_DEVCTL_NOSNOOP_EN);
  4301. }
  4302. static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
  4303. {
  4304. rtl_tx_performance_tweak(tp->pci_dev,
  4305. (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
  4306. }
  4307. static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
  4308. {
  4309. void __iomem *ioaddr = tp->mmio_addr;
  4310. r8168b_0_hw_jumbo_enable(tp);
  4311. RTL_W8(Config4, RTL_R8(Config4) | (1 << 0));
  4312. }
  4313. static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
  4314. {
  4315. void __iomem *ioaddr = tp->mmio_addr;
  4316. r8168b_0_hw_jumbo_disable(tp);
  4317. RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
  4318. }
  4319. static void rtl_init_jumbo_ops(struct rtl8169_private *tp)
  4320. {
  4321. struct jumbo_ops *ops = &tp->jumbo_ops;
  4322. switch (tp->mac_version) {
  4323. case RTL_GIGA_MAC_VER_11:
  4324. ops->disable = r8168b_0_hw_jumbo_disable;
  4325. ops->enable = r8168b_0_hw_jumbo_enable;
  4326. break;
  4327. case RTL_GIGA_MAC_VER_12:
  4328. case RTL_GIGA_MAC_VER_17:
  4329. ops->disable = r8168b_1_hw_jumbo_disable;
  4330. ops->enable = r8168b_1_hw_jumbo_enable;
  4331. break;
  4332. case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
  4333. case RTL_GIGA_MAC_VER_19:
  4334. case RTL_GIGA_MAC_VER_20:
  4335. case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
  4336. case RTL_GIGA_MAC_VER_22:
  4337. case RTL_GIGA_MAC_VER_23:
  4338. case RTL_GIGA_MAC_VER_24:
  4339. case RTL_GIGA_MAC_VER_25:
  4340. case RTL_GIGA_MAC_VER_26:
  4341. ops->disable = r8168c_hw_jumbo_disable;
  4342. ops->enable = r8168c_hw_jumbo_enable;
  4343. break;
  4344. case RTL_GIGA_MAC_VER_27:
  4345. case RTL_GIGA_MAC_VER_28:
  4346. ops->disable = r8168dp_hw_jumbo_disable;
  4347. ops->enable = r8168dp_hw_jumbo_enable;
  4348. break;
  4349. case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
  4350. case RTL_GIGA_MAC_VER_32:
  4351. case RTL_GIGA_MAC_VER_33:
  4352. case RTL_GIGA_MAC_VER_34:
  4353. ops->disable = r8168e_hw_jumbo_disable;
  4354. ops->enable = r8168e_hw_jumbo_enable;
  4355. break;
  4356. /*
  4357. * No action needed for jumbo frames with 8169.
  4358. * No jumbo for 810x at all.
  4359. */
  4360. case RTL_GIGA_MAC_VER_40:
  4361. case RTL_GIGA_MAC_VER_41:
  4362. case RTL_GIGA_MAC_VER_42:
  4363. case RTL_GIGA_MAC_VER_43:
  4364. case RTL_GIGA_MAC_VER_44:
  4365. case RTL_GIGA_MAC_VER_45:
  4366. case RTL_GIGA_MAC_VER_46:
  4367. case RTL_GIGA_MAC_VER_47:
  4368. case RTL_GIGA_MAC_VER_48:
  4369. case RTL_GIGA_MAC_VER_49:
  4370. case RTL_GIGA_MAC_VER_50:
  4371. case RTL_GIGA_MAC_VER_51:
  4372. default:
  4373. ops->disable = NULL;
  4374. ops->enable = NULL;
  4375. break;
  4376. }
  4377. }
  4378. DECLARE_RTL_COND(rtl_chipcmd_cond)
  4379. {
  4380. void __iomem *ioaddr = tp->mmio_addr;
  4381. return RTL_R8(ChipCmd) & CmdReset;
  4382. }
  4383. static void rtl_hw_reset(struct rtl8169_private *tp)
  4384. {
  4385. void __iomem *ioaddr = tp->mmio_addr;
  4386. RTL_W8(ChipCmd, CmdReset);
  4387. rtl_udelay_loop_wait_low(tp, &rtl_chipcmd_cond, 100, 100);
  4388. }
  4389. static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
  4390. {
  4391. struct rtl_fw *rtl_fw;
  4392. const char *name;
  4393. int rc = -ENOMEM;
  4394. name = rtl_lookup_firmware_name(tp);
  4395. if (!name)
  4396. goto out_no_firmware;
  4397. rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
  4398. if (!rtl_fw)
  4399. goto err_warn;
  4400. rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
  4401. if (rc < 0)
  4402. goto err_free;
  4403. rc = rtl_check_firmware(tp, rtl_fw);
  4404. if (rc < 0)
  4405. goto err_release_firmware;
  4406. tp->rtl_fw = rtl_fw;
  4407. out:
  4408. return;
  4409. err_release_firmware:
  4410. release_firmware(rtl_fw->fw);
  4411. err_free:
  4412. kfree(rtl_fw);
  4413. err_warn:
  4414. netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
  4415. name, rc);
  4416. out_no_firmware:
  4417. tp->rtl_fw = NULL;
  4418. goto out;
  4419. }
  4420. static void rtl_request_firmware(struct rtl8169_private *tp)
  4421. {
  4422. if (IS_ERR(tp->rtl_fw))
  4423. rtl_request_uncached_firmware(tp);
  4424. }
  4425. static void rtl_rx_close(struct rtl8169_private *tp)
  4426. {
  4427. void __iomem *ioaddr = tp->mmio_addr;
  4428. RTL_W32(RxConfig, RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
  4429. }
  4430. DECLARE_RTL_COND(rtl_npq_cond)
  4431. {
  4432. void __iomem *ioaddr = tp->mmio_addr;
  4433. return RTL_R8(TxPoll) & NPQ;
  4434. }
  4435. DECLARE_RTL_COND(rtl_txcfg_empty_cond)
  4436. {
  4437. void __iomem *ioaddr = tp->mmio_addr;
  4438. return RTL_R32(TxConfig) & TXCFG_EMPTY;
  4439. }
  4440. static void rtl8169_hw_reset(struct rtl8169_private *tp)
  4441. {
  4442. void __iomem *ioaddr = tp->mmio_addr;
  4443. /* Disable interrupts */
  4444. rtl8169_irq_mask_and_ack(tp);
  4445. rtl_rx_close(tp);
  4446. if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
  4447. tp->mac_version == RTL_GIGA_MAC_VER_28 ||
  4448. tp->mac_version == RTL_GIGA_MAC_VER_31) {
  4449. rtl_udelay_loop_wait_low(tp, &rtl_npq_cond, 20, 42*42);
  4450. } else if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
  4451. tp->mac_version == RTL_GIGA_MAC_VER_35 ||
  4452. tp->mac_version == RTL_GIGA_MAC_VER_36 ||
  4453. tp->mac_version == RTL_GIGA_MAC_VER_37 ||
  4454. tp->mac_version == RTL_GIGA_MAC_VER_38 ||
  4455. tp->mac_version == RTL_GIGA_MAC_VER_40 ||
  4456. tp->mac_version == RTL_GIGA_MAC_VER_41 ||
  4457. tp->mac_version == RTL_GIGA_MAC_VER_42 ||
  4458. tp->mac_version == RTL_GIGA_MAC_VER_43 ||
  4459. tp->mac_version == RTL_GIGA_MAC_VER_44 ||
  4460. tp->mac_version == RTL_GIGA_MAC_VER_45 ||
  4461. tp->mac_version == RTL_GIGA_MAC_VER_46 ||
  4462. tp->mac_version == RTL_GIGA_MAC_VER_47 ||
  4463. tp->mac_version == RTL_GIGA_MAC_VER_48 ||
  4464. tp->mac_version == RTL_GIGA_MAC_VER_49 ||
  4465. tp->mac_version == RTL_GIGA_MAC_VER_50 ||
  4466. tp->mac_version == RTL_GIGA_MAC_VER_51) {
  4467. RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
  4468. rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 666);
  4469. } else {
  4470. RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
  4471. udelay(100);
  4472. }
  4473. rtl_hw_reset(tp);
  4474. }
  4475. static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
  4476. {
  4477. void __iomem *ioaddr = tp->mmio_addr;
  4478. /* Set DMA burst size and Interframe Gap Time */
  4479. RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
  4480. (InterFrameGap << TxInterFrameGapShift));
  4481. }
  4482. static void rtl_hw_start(struct net_device *dev)
  4483. {
  4484. struct rtl8169_private *tp = netdev_priv(dev);
  4485. tp->hw_start(dev);
  4486. rtl_irq_enable_all(tp);
  4487. }
  4488. static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
  4489. void __iomem *ioaddr)
  4490. {
  4491. /*
  4492. * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
  4493. * register to be written before TxDescAddrLow to work.
  4494. * Switching from MMIO to I/O access fixes the issue as well.
  4495. */
  4496. RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
  4497. RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
  4498. RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
  4499. RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
  4500. }
  4501. static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
  4502. {
  4503. u16 cmd;
  4504. cmd = RTL_R16(CPlusCmd);
  4505. RTL_W16(CPlusCmd, cmd);
  4506. return cmd;
  4507. }
  4508. static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
  4509. {
  4510. /* Low hurts. Let's disable the filtering. */
  4511. RTL_W16(RxMaxSize, rx_buf_sz + 1);
  4512. }
  4513. static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
  4514. {
  4515. static const struct rtl_cfg2_info {
  4516. u32 mac_version;
  4517. u32 clk;
  4518. u32 val;
  4519. } cfg2_info [] = {
  4520. { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
  4521. { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
  4522. { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
  4523. { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
  4524. };
  4525. const struct rtl_cfg2_info *p = cfg2_info;
  4526. unsigned int i;
  4527. u32 clk;
  4528. clk = RTL_R8(Config2) & PCI_Clock_66MHz;
  4529. for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
  4530. if ((p->mac_version == mac_version) && (p->clk == clk)) {
  4531. RTL_W32(0x7c, p->val);
  4532. break;
  4533. }
  4534. }
  4535. }
  4536. static void rtl_set_rx_mode(struct net_device *dev)
  4537. {
  4538. struct rtl8169_private *tp = netdev_priv(dev);
  4539. void __iomem *ioaddr = tp->mmio_addr;
  4540. u32 mc_filter[2]; /* Multicast hash filter */
  4541. int rx_mode;
  4542. u32 tmp = 0;
  4543. if (dev->flags & IFF_PROMISC) {
  4544. /* Unconditionally log net taps. */
  4545. netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
  4546. rx_mode =
  4547. AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
  4548. AcceptAllPhys;
  4549. mc_filter[1] = mc_filter[0] = 0xffffffff;
  4550. } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
  4551. (dev->flags & IFF_ALLMULTI)) {
  4552. /* Too many to filter perfectly -- accept all multicasts. */
  4553. rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
  4554. mc_filter[1] = mc_filter[0] = 0xffffffff;
  4555. } else {
  4556. struct netdev_hw_addr *ha;
  4557. rx_mode = AcceptBroadcast | AcceptMyPhys;
  4558. mc_filter[1] = mc_filter[0] = 0;
  4559. netdev_for_each_mc_addr(ha, dev) {
  4560. int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
  4561. mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
  4562. rx_mode |= AcceptMulticast;
  4563. }
  4564. }
  4565. if (dev->features & NETIF_F_RXALL)
  4566. rx_mode |= (AcceptErr | AcceptRunt);
  4567. tmp = (RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
  4568. if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
  4569. u32 data = mc_filter[0];
  4570. mc_filter[0] = swab32(mc_filter[1]);
  4571. mc_filter[1] = swab32(data);
  4572. }
  4573. if (tp->mac_version == RTL_GIGA_MAC_VER_35)
  4574. mc_filter[1] = mc_filter[0] = 0xffffffff;
  4575. RTL_W32(MAR0 + 4, mc_filter[1]);
  4576. RTL_W32(MAR0 + 0, mc_filter[0]);
  4577. RTL_W32(RxConfig, tmp);
  4578. }
  4579. static void rtl_hw_start_8169(struct net_device *dev)
  4580. {
  4581. struct rtl8169_private *tp = netdev_priv(dev);
  4582. void __iomem *ioaddr = tp->mmio_addr;
  4583. struct pci_dev *pdev = tp->pci_dev;
  4584. if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
  4585. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
  4586. pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
  4587. }
  4588. RTL_W8(Cfg9346, Cfg9346_Unlock);
  4589. if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
  4590. tp->mac_version == RTL_GIGA_MAC_VER_02 ||
  4591. tp->mac_version == RTL_GIGA_MAC_VER_03 ||
  4592. tp->mac_version == RTL_GIGA_MAC_VER_04)
  4593. RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
  4594. rtl_init_rxcfg(tp);
  4595. RTL_W8(EarlyTxThres, NoEarlyTx);
  4596. rtl_set_rx_max_size(ioaddr, rx_buf_sz);
  4597. if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
  4598. tp->mac_version == RTL_GIGA_MAC_VER_02 ||
  4599. tp->mac_version == RTL_GIGA_MAC_VER_03 ||
  4600. tp->mac_version == RTL_GIGA_MAC_VER_04)
  4601. rtl_set_rx_tx_config_registers(tp);
  4602. tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
  4603. if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
  4604. tp->mac_version == RTL_GIGA_MAC_VER_03) {
  4605. dprintk("Set MAC Reg C+CR Offset 0xe0. "
  4606. "Bit-3 and bit-14 MUST be 1\n");
  4607. tp->cp_cmd |= (1 << 14);
  4608. }
  4609. RTL_W16(CPlusCmd, tp->cp_cmd);
  4610. rtl8169_set_magic_reg(ioaddr, tp->mac_version);
  4611. /*
  4612. * Undocumented corner. Supposedly:
  4613. * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
  4614. */
  4615. RTL_W16(IntrMitigate, 0x0000);
  4616. rtl_set_rx_tx_desc_registers(tp, ioaddr);
  4617. if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
  4618. tp->mac_version != RTL_GIGA_MAC_VER_02 &&
  4619. tp->mac_version != RTL_GIGA_MAC_VER_03 &&
  4620. tp->mac_version != RTL_GIGA_MAC_VER_04) {
  4621. RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
  4622. rtl_set_rx_tx_config_registers(tp);
  4623. }
  4624. RTL_W8(Cfg9346, Cfg9346_Lock);
  4625. /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
  4626. RTL_R8(IntrMask);
  4627. RTL_W32(RxMissed, 0);
  4628. rtl_set_rx_mode(dev);
  4629. /* no early-rx interrupts */
  4630. RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
  4631. }
  4632. static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value)
  4633. {
  4634. if (tp->csi_ops.write)
  4635. tp->csi_ops.write(tp, addr, value);
  4636. }
  4637. static u32 rtl_csi_read(struct rtl8169_private *tp, int addr)
  4638. {
  4639. return tp->csi_ops.read ? tp->csi_ops.read(tp, addr) : ~0;
  4640. }
  4641. static void rtl_csi_access_enable(struct rtl8169_private *tp, u32 bits)
  4642. {
  4643. u32 csi;
  4644. csi = rtl_csi_read(tp, 0x070c) & 0x00ffffff;
  4645. rtl_csi_write(tp, 0x070c, csi | bits);
  4646. }
  4647. static void rtl_csi_access_enable_1(struct rtl8169_private *tp)
  4648. {
  4649. rtl_csi_access_enable(tp, 0x17000000);
  4650. }
  4651. static void rtl_csi_access_enable_2(struct rtl8169_private *tp)
  4652. {
  4653. rtl_csi_access_enable(tp, 0x27000000);
  4654. }
  4655. DECLARE_RTL_COND(rtl_csiar_cond)
  4656. {
  4657. void __iomem *ioaddr = tp->mmio_addr;
  4658. return RTL_R32(CSIAR) & CSIAR_FLAG;
  4659. }
  4660. static void r8169_csi_write(struct rtl8169_private *tp, int addr, int value)
  4661. {
  4662. void __iomem *ioaddr = tp->mmio_addr;
  4663. RTL_W32(CSIDR, value);
  4664. RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
  4665. CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
  4666. rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
  4667. }
  4668. static u32 r8169_csi_read(struct rtl8169_private *tp, int addr)
  4669. {
  4670. void __iomem *ioaddr = tp->mmio_addr;
  4671. RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
  4672. CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
  4673. return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
  4674. RTL_R32(CSIDR) : ~0;
  4675. }
  4676. static void r8402_csi_write(struct rtl8169_private *tp, int addr, int value)
  4677. {
  4678. void __iomem *ioaddr = tp->mmio_addr;
  4679. RTL_W32(CSIDR, value);
  4680. RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
  4681. CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
  4682. CSIAR_FUNC_NIC);
  4683. rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
  4684. }
  4685. static u32 r8402_csi_read(struct rtl8169_private *tp, int addr)
  4686. {
  4687. void __iomem *ioaddr = tp->mmio_addr;
  4688. RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC |
  4689. CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
  4690. return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
  4691. RTL_R32(CSIDR) : ~0;
  4692. }
  4693. static void r8411_csi_write(struct rtl8169_private *tp, int addr, int value)
  4694. {
  4695. void __iomem *ioaddr = tp->mmio_addr;
  4696. RTL_W32(CSIDR, value);
  4697. RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
  4698. CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
  4699. CSIAR_FUNC_NIC2);
  4700. rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
  4701. }
  4702. static u32 r8411_csi_read(struct rtl8169_private *tp, int addr)
  4703. {
  4704. void __iomem *ioaddr = tp->mmio_addr;
  4705. RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC2 |
  4706. CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
  4707. return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
  4708. RTL_R32(CSIDR) : ~0;
  4709. }
  4710. static void rtl_init_csi_ops(struct rtl8169_private *tp)
  4711. {
  4712. struct csi_ops *ops = &tp->csi_ops;
  4713. switch (tp->mac_version) {
  4714. case RTL_GIGA_MAC_VER_01:
  4715. case RTL_GIGA_MAC_VER_02:
  4716. case RTL_GIGA_MAC_VER_03:
  4717. case RTL_GIGA_MAC_VER_04:
  4718. case RTL_GIGA_MAC_VER_05:
  4719. case RTL_GIGA_MAC_VER_06:
  4720. case RTL_GIGA_MAC_VER_10:
  4721. case RTL_GIGA_MAC_VER_11:
  4722. case RTL_GIGA_MAC_VER_12:
  4723. case RTL_GIGA_MAC_VER_13:
  4724. case RTL_GIGA_MAC_VER_14:
  4725. case RTL_GIGA_MAC_VER_15:
  4726. case RTL_GIGA_MAC_VER_16:
  4727. case RTL_GIGA_MAC_VER_17:
  4728. ops->write = NULL;
  4729. ops->read = NULL;
  4730. break;
  4731. case RTL_GIGA_MAC_VER_37:
  4732. case RTL_GIGA_MAC_VER_38:
  4733. ops->write = r8402_csi_write;
  4734. ops->read = r8402_csi_read;
  4735. break;
  4736. case RTL_GIGA_MAC_VER_44:
  4737. ops->write = r8411_csi_write;
  4738. ops->read = r8411_csi_read;
  4739. break;
  4740. default:
  4741. ops->write = r8169_csi_write;
  4742. ops->read = r8169_csi_read;
  4743. break;
  4744. }
  4745. }
  4746. struct ephy_info {
  4747. unsigned int offset;
  4748. u16 mask;
  4749. u16 bits;
  4750. };
  4751. static void rtl_ephy_init(struct rtl8169_private *tp, const struct ephy_info *e,
  4752. int len)
  4753. {
  4754. u16 w;
  4755. while (len-- > 0) {
  4756. w = (rtl_ephy_read(tp, e->offset) & ~e->mask) | e->bits;
  4757. rtl_ephy_write(tp, e->offset, w);
  4758. e++;
  4759. }
  4760. }
  4761. static void rtl_disable_clock_request(struct pci_dev *pdev)
  4762. {
  4763. pcie_capability_clear_word(pdev, PCI_EXP_LNKCTL,
  4764. PCI_EXP_LNKCTL_CLKREQ_EN);
  4765. }
  4766. static void rtl_enable_clock_request(struct pci_dev *pdev)
  4767. {
  4768. pcie_capability_set_word(pdev, PCI_EXP_LNKCTL,
  4769. PCI_EXP_LNKCTL_CLKREQ_EN);
  4770. }
  4771. static void rtl_pcie_state_l2l3_enable(struct rtl8169_private *tp, bool enable)
  4772. {
  4773. void __iomem *ioaddr = tp->mmio_addr;
  4774. u8 data;
  4775. data = RTL_R8(Config3);
  4776. if (enable)
  4777. data |= Rdy_to_L23;
  4778. else
  4779. data &= ~Rdy_to_L23;
  4780. RTL_W8(Config3, data);
  4781. }
  4782. #define R8168_CPCMD_QUIRK_MASK (\
  4783. EnableBist | \
  4784. Mac_dbgo_oe | \
  4785. Force_half_dup | \
  4786. Force_rxflow_en | \
  4787. Force_txflow_en | \
  4788. Cxpl_dbg_sel | \
  4789. ASF | \
  4790. PktCntrDisable | \
  4791. Mac_dbgo_sel)
  4792. static void rtl_hw_start_8168bb(struct rtl8169_private *tp)
  4793. {
  4794. void __iomem *ioaddr = tp->mmio_addr;
  4795. struct pci_dev *pdev = tp->pci_dev;
  4796. RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
  4797. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
  4798. if (tp->dev->mtu <= ETH_DATA_LEN) {
  4799. rtl_tx_performance_tweak(pdev, (0x5 << MAX_READ_REQUEST_SHIFT) |
  4800. PCI_EXP_DEVCTL_NOSNOOP_EN);
  4801. }
  4802. }
  4803. static void rtl_hw_start_8168bef(struct rtl8169_private *tp)
  4804. {
  4805. void __iomem *ioaddr = tp->mmio_addr;
  4806. rtl_hw_start_8168bb(tp);
  4807. RTL_W8(MaxTxPacketSize, TxPacketMax);
  4808. RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
  4809. }
  4810. static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
  4811. {
  4812. void __iomem *ioaddr = tp->mmio_addr;
  4813. struct pci_dev *pdev = tp->pci_dev;
  4814. RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
  4815. RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
  4816. if (tp->dev->mtu <= ETH_DATA_LEN)
  4817. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  4818. rtl_disable_clock_request(pdev);
  4819. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
  4820. }
  4821. static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
  4822. {
  4823. static const struct ephy_info e_info_8168cp[] = {
  4824. { 0x01, 0, 0x0001 },
  4825. { 0x02, 0x0800, 0x1000 },
  4826. { 0x03, 0, 0x0042 },
  4827. { 0x06, 0x0080, 0x0000 },
  4828. { 0x07, 0, 0x2000 }
  4829. };
  4830. rtl_csi_access_enable_2(tp);
  4831. rtl_ephy_init(tp, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
  4832. __rtl_hw_start_8168cp(tp);
  4833. }
  4834. static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp)
  4835. {
  4836. void __iomem *ioaddr = tp->mmio_addr;
  4837. struct pci_dev *pdev = tp->pci_dev;
  4838. rtl_csi_access_enable_2(tp);
  4839. RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
  4840. if (tp->dev->mtu <= ETH_DATA_LEN)
  4841. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  4842. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
  4843. }
  4844. static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
  4845. {
  4846. void __iomem *ioaddr = tp->mmio_addr;
  4847. struct pci_dev *pdev = tp->pci_dev;
  4848. rtl_csi_access_enable_2(tp);
  4849. RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
  4850. /* Magic. */
  4851. RTL_W8(DBG_REG, 0x20);
  4852. RTL_W8(MaxTxPacketSize, TxPacketMax);
  4853. if (tp->dev->mtu <= ETH_DATA_LEN)
  4854. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  4855. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
  4856. }
  4857. static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
  4858. {
  4859. void __iomem *ioaddr = tp->mmio_addr;
  4860. static const struct ephy_info e_info_8168c_1[] = {
  4861. { 0x02, 0x0800, 0x1000 },
  4862. { 0x03, 0, 0x0002 },
  4863. { 0x06, 0x0080, 0x0000 }
  4864. };
  4865. rtl_csi_access_enable_2(tp);
  4866. RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
  4867. rtl_ephy_init(tp, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
  4868. __rtl_hw_start_8168cp(tp);
  4869. }
  4870. static void rtl_hw_start_8168c_2(struct rtl8169_private *tp)
  4871. {
  4872. static const struct ephy_info e_info_8168c_2[] = {
  4873. { 0x01, 0, 0x0001 },
  4874. { 0x03, 0x0400, 0x0220 }
  4875. };
  4876. rtl_csi_access_enable_2(tp);
  4877. rtl_ephy_init(tp, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
  4878. __rtl_hw_start_8168cp(tp);
  4879. }
  4880. static void rtl_hw_start_8168c_3(struct rtl8169_private *tp)
  4881. {
  4882. rtl_hw_start_8168c_2(tp);
  4883. }
  4884. static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
  4885. {
  4886. rtl_csi_access_enable_2(tp);
  4887. __rtl_hw_start_8168cp(tp);
  4888. }
  4889. static void rtl_hw_start_8168d(struct rtl8169_private *tp)
  4890. {
  4891. void __iomem *ioaddr = tp->mmio_addr;
  4892. struct pci_dev *pdev = tp->pci_dev;
  4893. rtl_csi_access_enable_2(tp);
  4894. rtl_disable_clock_request(pdev);
  4895. RTL_W8(MaxTxPacketSize, TxPacketMax);
  4896. if (tp->dev->mtu <= ETH_DATA_LEN)
  4897. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  4898. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
  4899. }
  4900. static void rtl_hw_start_8168dp(struct rtl8169_private *tp)
  4901. {
  4902. void __iomem *ioaddr = tp->mmio_addr;
  4903. struct pci_dev *pdev = tp->pci_dev;
  4904. rtl_csi_access_enable_1(tp);
  4905. if (tp->dev->mtu <= ETH_DATA_LEN)
  4906. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  4907. RTL_W8(MaxTxPacketSize, TxPacketMax);
  4908. rtl_disable_clock_request(pdev);
  4909. }
  4910. static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
  4911. {
  4912. void __iomem *ioaddr = tp->mmio_addr;
  4913. struct pci_dev *pdev = tp->pci_dev;
  4914. static const struct ephy_info e_info_8168d_4[] = {
  4915. { 0x0b, ~0, 0x48 },
  4916. { 0x19, 0x20, 0x50 },
  4917. { 0x0c, ~0, 0x20 }
  4918. };
  4919. int i;
  4920. rtl_csi_access_enable_1(tp);
  4921. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  4922. RTL_W8(MaxTxPacketSize, TxPacketMax);
  4923. for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
  4924. const struct ephy_info *e = e_info_8168d_4 + i;
  4925. u16 w;
  4926. w = rtl_ephy_read(tp, e->offset);
  4927. rtl_ephy_write(tp, 0x03, (w & e->mask) | e->bits);
  4928. }
  4929. rtl_enable_clock_request(pdev);
  4930. }
  4931. static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
  4932. {
  4933. void __iomem *ioaddr = tp->mmio_addr;
  4934. struct pci_dev *pdev = tp->pci_dev;
  4935. static const struct ephy_info e_info_8168e_1[] = {
  4936. { 0x00, 0x0200, 0x0100 },
  4937. { 0x00, 0x0000, 0x0004 },
  4938. { 0x06, 0x0002, 0x0001 },
  4939. { 0x06, 0x0000, 0x0030 },
  4940. { 0x07, 0x0000, 0x2000 },
  4941. { 0x00, 0x0000, 0x0020 },
  4942. { 0x03, 0x5800, 0x2000 },
  4943. { 0x03, 0x0000, 0x0001 },
  4944. { 0x01, 0x0800, 0x1000 },
  4945. { 0x07, 0x0000, 0x4000 },
  4946. { 0x1e, 0x0000, 0x2000 },
  4947. { 0x19, 0xffff, 0xfe6c },
  4948. { 0x0a, 0x0000, 0x0040 }
  4949. };
  4950. rtl_csi_access_enable_2(tp);
  4951. rtl_ephy_init(tp, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
  4952. if (tp->dev->mtu <= ETH_DATA_LEN)
  4953. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  4954. RTL_W8(MaxTxPacketSize, TxPacketMax);
  4955. rtl_disable_clock_request(pdev);
  4956. /* Reset tx FIFO pointer */
  4957. RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
  4958. RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
  4959. RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
  4960. }
  4961. static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
  4962. {
  4963. void __iomem *ioaddr = tp->mmio_addr;
  4964. struct pci_dev *pdev = tp->pci_dev;
  4965. static const struct ephy_info e_info_8168e_2[] = {
  4966. { 0x09, 0x0000, 0x0080 },
  4967. { 0x19, 0x0000, 0x0224 }
  4968. };
  4969. rtl_csi_access_enable_1(tp);
  4970. rtl_ephy_init(tp, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
  4971. if (tp->dev->mtu <= ETH_DATA_LEN)
  4972. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  4973. rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
  4974. rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
  4975. rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
  4976. rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
  4977. rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
  4978. rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
  4979. rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
  4980. rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
  4981. RTL_W8(MaxTxPacketSize, EarlySize);
  4982. rtl_disable_clock_request(pdev);
  4983. RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
  4984. RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
  4985. /* Adjust EEE LED frequency */
  4986. RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
  4987. RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
  4988. RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
  4989. RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
  4990. }
  4991. static void rtl_hw_start_8168f(struct rtl8169_private *tp)
  4992. {
  4993. void __iomem *ioaddr = tp->mmio_addr;
  4994. struct pci_dev *pdev = tp->pci_dev;
  4995. rtl_csi_access_enable_2(tp);
  4996. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  4997. rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
  4998. rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
  4999. rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
  5000. rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
  5001. rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
  5002. rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
  5003. rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
  5004. rtl_w0w1_eri(tp, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
  5005. rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
  5006. rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
  5007. RTL_W8(MaxTxPacketSize, EarlySize);
  5008. rtl_disable_clock_request(pdev);
  5009. RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
  5010. RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
  5011. RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
  5012. RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
  5013. RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
  5014. }
  5015. static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
  5016. {
  5017. void __iomem *ioaddr = tp->mmio_addr;
  5018. static const struct ephy_info e_info_8168f_1[] = {
  5019. { 0x06, 0x00c0, 0x0020 },
  5020. { 0x08, 0x0001, 0x0002 },
  5021. { 0x09, 0x0000, 0x0080 },
  5022. { 0x19, 0x0000, 0x0224 }
  5023. };
  5024. rtl_hw_start_8168f(tp);
  5025. rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
  5026. rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
  5027. /* Adjust EEE LED frequency */
  5028. RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
  5029. }
  5030. static void rtl_hw_start_8411(struct rtl8169_private *tp)
  5031. {
  5032. static const struct ephy_info e_info_8168f_1[] = {
  5033. { 0x06, 0x00c0, 0x0020 },
  5034. { 0x0f, 0xffff, 0x5200 },
  5035. { 0x1e, 0x0000, 0x4000 },
  5036. { 0x19, 0x0000, 0x0224 }
  5037. };
  5038. rtl_hw_start_8168f(tp);
  5039. rtl_pcie_state_l2l3_enable(tp, false);
  5040. rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
  5041. rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0x0000, ERIAR_EXGMAC);
  5042. }
  5043. static void rtl_hw_start_8168g(struct rtl8169_private *tp)
  5044. {
  5045. void __iomem *ioaddr = tp->mmio_addr;
  5046. struct pci_dev *pdev = tp->pci_dev;
  5047. RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
  5048. rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x080002, ERIAR_EXGMAC);
  5049. rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
  5050. rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
  5051. rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
  5052. rtl_csi_access_enable_1(tp);
  5053. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  5054. rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
  5055. rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
  5056. rtl_eri_write(tp, 0x2f8, ERIAR_MASK_0011, 0x1d8f, ERIAR_EXGMAC);
  5057. RTL_W32(MISC, RTL_R32(MISC) & ~RXDV_GATED_EN);
  5058. RTL_W8(MaxTxPacketSize, EarlySize);
  5059. rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
  5060. rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
  5061. /* Adjust EEE LED frequency */
  5062. RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
  5063. rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
  5064. rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
  5065. rtl_pcie_state_l2l3_enable(tp, false);
  5066. }
  5067. static void rtl_hw_start_8168g_1(struct rtl8169_private *tp)
  5068. {
  5069. void __iomem *ioaddr = tp->mmio_addr;
  5070. static const struct ephy_info e_info_8168g_1[] = {
  5071. { 0x00, 0x0000, 0x0008 },
  5072. { 0x0c, 0x37d0, 0x0820 },
  5073. { 0x1e, 0x0000, 0x0001 },
  5074. { 0x19, 0x8000, 0x0000 }
  5075. };
  5076. rtl_hw_start_8168g(tp);
  5077. /* disable aspm and clock request before access ephy */
  5078. RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
  5079. RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
  5080. rtl_ephy_init(tp, e_info_8168g_1, ARRAY_SIZE(e_info_8168g_1));
  5081. }
  5082. static void rtl_hw_start_8168g_2(struct rtl8169_private *tp)
  5083. {
  5084. void __iomem *ioaddr = tp->mmio_addr;
  5085. static const struct ephy_info e_info_8168g_2[] = {
  5086. { 0x00, 0x0000, 0x0008 },
  5087. { 0x0c, 0x3df0, 0x0200 },
  5088. { 0x19, 0xffff, 0xfc00 },
  5089. { 0x1e, 0xffff, 0x20eb }
  5090. };
  5091. rtl_hw_start_8168g(tp);
  5092. /* disable aspm and clock request before access ephy */
  5093. RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
  5094. RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
  5095. rtl_ephy_init(tp, e_info_8168g_2, ARRAY_SIZE(e_info_8168g_2));
  5096. }
  5097. static void rtl_hw_start_8411_2(struct rtl8169_private *tp)
  5098. {
  5099. void __iomem *ioaddr = tp->mmio_addr;
  5100. static const struct ephy_info e_info_8411_2[] = {
  5101. { 0x00, 0x0000, 0x0008 },
  5102. { 0x0c, 0x3df0, 0x0200 },
  5103. { 0x0f, 0xffff, 0x5200 },
  5104. { 0x19, 0x0020, 0x0000 },
  5105. { 0x1e, 0x0000, 0x2000 }
  5106. };
  5107. rtl_hw_start_8168g(tp);
  5108. /* disable aspm and clock request before access ephy */
  5109. RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
  5110. RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
  5111. rtl_ephy_init(tp, e_info_8411_2, ARRAY_SIZE(e_info_8411_2));
  5112. }
  5113. static void rtl_hw_start_8168h_1(struct rtl8169_private *tp)
  5114. {
  5115. void __iomem *ioaddr = tp->mmio_addr;
  5116. struct pci_dev *pdev = tp->pci_dev;
  5117. int rg_saw_cnt;
  5118. u32 data;
  5119. static const struct ephy_info e_info_8168h_1[] = {
  5120. { 0x1e, 0x0800, 0x0001 },
  5121. { 0x1d, 0x0000, 0x0800 },
  5122. { 0x05, 0xffff, 0x2089 },
  5123. { 0x06, 0xffff, 0x5881 },
  5124. { 0x04, 0xffff, 0x154a },
  5125. { 0x01, 0xffff, 0x068b }
  5126. };
  5127. /* disable aspm and clock request before access ephy */
  5128. RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
  5129. RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
  5130. rtl_ephy_init(tp, e_info_8168h_1, ARRAY_SIZE(e_info_8168h_1));
  5131. RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
  5132. rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
  5133. rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
  5134. rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
  5135. rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
  5136. rtl_csi_access_enable_1(tp);
  5137. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  5138. rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
  5139. rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
  5140. rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_1111, 0x0010, 0x00, ERIAR_EXGMAC);
  5141. rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f00, 0x00, ERIAR_EXGMAC);
  5142. rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
  5143. RTL_W32(MISC, RTL_R32(MISC) & ~RXDV_GATED_EN);
  5144. RTL_W8(MaxTxPacketSize, EarlySize);
  5145. rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
  5146. rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
  5147. /* Adjust EEE LED frequency */
  5148. RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
  5149. RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
  5150. RTL_W8(DLLPR, RTL_R8(MISC_1) & ~PFM_D3COLD_EN);
  5151. RTL_W8(DLLPR, RTL_R8(DLLPR) & ~TX_10M_PS_EN);
  5152. rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
  5153. rtl_pcie_state_l2l3_enable(tp, false);
  5154. rtl_writephy(tp, 0x1f, 0x0c42);
  5155. rg_saw_cnt = rtl_readphy(tp, 0x13);
  5156. rtl_writephy(tp, 0x1f, 0x0000);
  5157. if (rg_saw_cnt > 0) {
  5158. u16 sw_cnt_1ms_ini;
  5159. sw_cnt_1ms_ini = 16000000/rg_saw_cnt;
  5160. sw_cnt_1ms_ini &= 0x0fff;
  5161. data = r8168_mac_ocp_read(tp, 0xd412);
  5162. data &= 0x0fff;
  5163. data |= sw_cnt_1ms_ini;
  5164. r8168_mac_ocp_write(tp, 0xd412, data);
  5165. }
  5166. data = r8168_mac_ocp_read(tp, 0xe056);
  5167. data &= 0xf0;
  5168. data |= 0x07;
  5169. r8168_mac_ocp_write(tp, 0xe056, data);
  5170. data = r8168_mac_ocp_read(tp, 0xe052);
  5171. data &= 0x8008;
  5172. data |= 0x6000;
  5173. r8168_mac_ocp_write(tp, 0xe052, data);
  5174. data = r8168_mac_ocp_read(tp, 0xe0d6);
  5175. data &= 0x01ff;
  5176. data |= 0x017f;
  5177. r8168_mac_ocp_write(tp, 0xe0d6, data);
  5178. data = r8168_mac_ocp_read(tp, 0xd420);
  5179. data &= 0x0fff;
  5180. data |= 0x047f;
  5181. r8168_mac_ocp_write(tp, 0xd420, data);
  5182. r8168_mac_ocp_write(tp, 0xe63e, 0x0001);
  5183. r8168_mac_ocp_write(tp, 0xe63e, 0x0000);
  5184. r8168_mac_ocp_write(tp, 0xc094, 0x0000);
  5185. r8168_mac_ocp_write(tp, 0xc09e, 0x0000);
  5186. }
  5187. static void rtl_hw_start_8168ep(struct rtl8169_private *tp)
  5188. {
  5189. void __iomem *ioaddr = tp->mmio_addr;
  5190. struct pci_dev *pdev = tp->pci_dev;
  5191. rtl8168ep_stop_cmac(tp);
  5192. RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
  5193. rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
  5194. rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x2f, ERIAR_EXGMAC);
  5195. rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x5f, ERIAR_EXGMAC);
  5196. rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
  5197. rtl_csi_access_enable_1(tp);
  5198. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  5199. rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
  5200. rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
  5201. rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f80, 0x00, ERIAR_EXGMAC);
  5202. rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
  5203. RTL_W32(MISC, RTL_R32(MISC) & ~RXDV_GATED_EN);
  5204. RTL_W8(MaxTxPacketSize, EarlySize);
  5205. rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
  5206. rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
  5207. /* Adjust EEE LED frequency */
  5208. RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
  5209. rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
  5210. RTL_W8(DLLPR, RTL_R8(DLLPR) & ~TX_10M_PS_EN);
  5211. rtl_pcie_state_l2l3_enable(tp, false);
  5212. }
  5213. static void rtl_hw_start_8168ep_1(struct rtl8169_private *tp)
  5214. {
  5215. void __iomem *ioaddr = tp->mmio_addr;
  5216. static const struct ephy_info e_info_8168ep_1[] = {
  5217. { 0x00, 0xffff, 0x10ab },
  5218. { 0x06, 0xffff, 0xf030 },
  5219. { 0x08, 0xffff, 0x2006 },
  5220. { 0x0d, 0xffff, 0x1666 },
  5221. { 0x0c, 0x3ff0, 0x0000 }
  5222. };
  5223. /* disable aspm and clock request before access ephy */
  5224. RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
  5225. RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
  5226. rtl_ephy_init(tp, e_info_8168ep_1, ARRAY_SIZE(e_info_8168ep_1));
  5227. rtl_hw_start_8168ep(tp);
  5228. }
  5229. static void rtl_hw_start_8168ep_2(struct rtl8169_private *tp)
  5230. {
  5231. void __iomem *ioaddr = tp->mmio_addr;
  5232. static const struct ephy_info e_info_8168ep_2[] = {
  5233. { 0x00, 0xffff, 0x10a3 },
  5234. { 0x19, 0xffff, 0xfc00 },
  5235. { 0x1e, 0xffff, 0x20ea }
  5236. };
  5237. /* disable aspm and clock request before access ephy */
  5238. RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
  5239. RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
  5240. rtl_ephy_init(tp, e_info_8168ep_2, ARRAY_SIZE(e_info_8168ep_2));
  5241. rtl_hw_start_8168ep(tp);
  5242. RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
  5243. RTL_W8(DLLPR, RTL_R8(MISC_1) & ~PFM_D3COLD_EN);
  5244. }
  5245. static void rtl_hw_start_8168ep_3(struct rtl8169_private *tp)
  5246. {
  5247. void __iomem *ioaddr = tp->mmio_addr;
  5248. u32 data;
  5249. static const struct ephy_info e_info_8168ep_3[] = {
  5250. { 0x00, 0xffff, 0x10a3 },
  5251. { 0x19, 0xffff, 0x7c00 },
  5252. { 0x1e, 0xffff, 0x20eb },
  5253. { 0x0d, 0xffff, 0x1666 }
  5254. };
  5255. /* disable aspm and clock request before access ephy */
  5256. RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
  5257. RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
  5258. rtl_ephy_init(tp, e_info_8168ep_3, ARRAY_SIZE(e_info_8168ep_3));
  5259. rtl_hw_start_8168ep(tp);
  5260. RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
  5261. RTL_W8(DLLPR, RTL_R8(MISC_1) & ~PFM_D3COLD_EN);
  5262. data = r8168_mac_ocp_read(tp, 0xd3e2);
  5263. data &= 0xf000;
  5264. data |= 0x0271;
  5265. r8168_mac_ocp_write(tp, 0xd3e2, data);
  5266. data = r8168_mac_ocp_read(tp, 0xd3e4);
  5267. data &= 0xff00;
  5268. r8168_mac_ocp_write(tp, 0xd3e4, data);
  5269. data = r8168_mac_ocp_read(tp, 0xe860);
  5270. data |= 0x0080;
  5271. r8168_mac_ocp_write(tp, 0xe860, data);
  5272. }
  5273. static void rtl_hw_start_8168(struct net_device *dev)
  5274. {
  5275. struct rtl8169_private *tp = netdev_priv(dev);
  5276. void __iomem *ioaddr = tp->mmio_addr;
  5277. RTL_W8(Cfg9346, Cfg9346_Unlock);
  5278. RTL_W8(MaxTxPacketSize, TxPacketMax);
  5279. rtl_set_rx_max_size(ioaddr, rx_buf_sz);
  5280. tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
  5281. RTL_W16(CPlusCmd, tp->cp_cmd);
  5282. RTL_W16(IntrMitigate, 0x5151);
  5283. /* Work around for RxFIFO overflow. */
  5284. if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
  5285. tp->event_slow |= RxFIFOOver | PCSTimeout;
  5286. tp->event_slow &= ~RxOverflow;
  5287. }
  5288. rtl_set_rx_tx_desc_registers(tp, ioaddr);
  5289. rtl_set_rx_tx_config_registers(tp);
  5290. RTL_R8(IntrMask);
  5291. switch (tp->mac_version) {
  5292. case RTL_GIGA_MAC_VER_11:
  5293. rtl_hw_start_8168bb(tp);
  5294. break;
  5295. case RTL_GIGA_MAC_VER_12:
  5296. case RTL_GIGA_MAC_VER_17:
  5297. rtl_hw_start_8168bef(tp);
  5298. break;
  5299. case RTL_GIGA_MAC_VER_18:
  5300. rtl_hw_start_8168cp_1(tp);
  5301. break;
  5302. case RTL_GIGA_MAC_VER_19:
  5303. rtl_hw_start_8168c_1(tp);
  5304. break;
  5305. case RTL_GIGA_MAC_VER_20:
  5306. rtl_hw_start_8168c_2(tp);
  5307. break;
  5308. case RTL_GIGA_MAC_VER_21:
  5309. rtl_hw_start_8168c_3(tp);
  5310. break;
  5311. case RTL_GIGA_MAC_VER_22:
  5312. rtl_hw_start_8168c_4(tp);
  5313. break;
  5314. case RTL_GIGA_MAC_VER_23:
  5315. rtl_hw_start_8168cp_2(tp);
  5316. break;
  5317. case RTL_GIGA_MAC_VER_24:
  5318. rtl_hw_start_8168cp_3(tp);
  5319. break;
  5320. case RTL_GIGA_MAC_VER_25:
  5321. case RTL_GIGA_MAC_VER_26:
  5322. case RTL_GIGA_MAC_VER_27:
  5323. rtl_hw_start_8168d(tp);
  5324. break;
  5325. case RTL_GIGA_MAC_VER_28:
  5326. rtl_hw_start_8168d_4(tp);
  5327. break;
  5328. case RTL_GIGA_MAC_VER_31:
  5329. rtl_hw_start_8168dp(tp);
  5330. break;
  5331. case RTL_GIGA_MAC_VER_32:
  5332. case RTL_GIGA_MAC_VER_33:
  5333. rtl_hw_start_8168e_1(tp);
  5334. break;
  5335. case RTL_GIGA_MAC_VER_34:
  5336. rtl_hw_start_8168e_2(tp);
  5337. break;
  5338. case RTL_GIGA_MAC_VER_35:
  5339. case RTL_GIGA_MAC_VER_36:
  5340. rtl_hw_start_8168f_1(tp);
  5341. break;
  5342. case RTL_GIGA_MAC_VER_38:
  5343. rtl_hw_start_8411(tp);
  5344. break;
  5345. case RTL_GIGA_MAC_VER_40:
  5346. case RTL_GIGA_MAC_VER_41:
  5347. rtl_hw_start_8168g_1(tp);
  5348. break;
  5349. case RTL_GIGA_MAC_VER_42:
  5350. rtl_hw_start_8168g_2(tp);
  5351. break;
  5352. case RTL_GIGA_MAC_VER_44:
  5353. rtl_hw_start_8411_2(tp);
  5354. break;
  5355. case RTL_GIGA_MAC_VER_45:
  5356. case RTL_GIGA_MAC_VER_46:
  5357. rtl_hw_start_8168h_1(tp);
  5358. break;
  5359. case RTL_GIGA_MAC_VER_49:
  5360. rtl_hw_start_8168ep_1(tp);
  5361. break;
  5362. case RTL_GIGA_MAC_VER_50:
  5363. rtl_hw_start_8168ep_2(tp);
  5364. break;
  5365. case RTL_GIGA_MAC_VER_51:
  5366. rtl_hw_start_8168ep_3(tp);
  5367. break;
  5368. default:
  5369. printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
  5370. dev->name, tp->mac_version);
  5371. break;
  5372. }
  5373. RTL_W8(Cfg9346, Cfg9346_Lock);
  5374. RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
  5375. rtl_set_rx_mode(dev);
  5376. RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
  5377. }
  5378. #define R810X_CPCMD_QUIRK_MASK (\
  5379. EnableBist | \
  5380. Mac_dbgo_oe | \
  5381. Force_half_dup | \
  5382. Force_rxflow_en | \
  5383. Force_txflow_en | \
  5384. Cxpl_dbg_sel | \
  5385. ASF | \
  5386. PktCntrDisable | \
  5387. Mac_dbgo_sel)
  5388. static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
  5389. {
  5390. void __iomem *ioaddr = tp->mmio_addr;
  5391. struct pci_dev *pdev = tp->pci_dev;
  5392. static const struct ephy_info e_info_8102e_1[] = {
  5393. { 0x01, 0, 0x6e65 },
  5394. { 0x02, 0, 0x091f },
  5395. { 0x03, 0, 0xc2f9 },
  5396. { 0x06, 0, 0xafb5 },
  5397. { 0x07, 0, 0x0e00 },
  5398. { 0x19, 0, 0xec80 },
  5399. { 0x01, 0, 0x2e65 },
  5400. { 0x01, 0, 0x6e65 }
  5401. };
  5402. u8 cfg1;
  5403. rtl_csi_access_enable_2(tp);
  5404. RTL_W8(DBG_REG, FIX_NAK_1);
  5405. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  5406. RTL_W8(Config1,
  5407. LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
  5408. RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
  5409. cfg1 = RTL_R8(Config1);
  5410. if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
  5411. RTL_W8(Config1, cfg1 & ~LEDS0);
  5412. rtl_ephy_init(tp, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
  5413. }
  5414. static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
  5415. {
  5416. void __iomem *ioaddr = tp->mmio_addr;
  5417. struct pci_dev *pdev = tp->pci_dev;
  5418. rtl_csi_access_enable_2(tp);
  5419. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  5420. RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
  5421. RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
  5422. }
  5423. static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
  5424. {
  5425. rtl_hw_start_8102e_2(tp);
  5426. rtl_ephy_write(tp, 0x03, 0xc2f9);
  5427. }
  5428. static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
  5429. {
  5430. void __iomem *ioaddr = tp->mmio_addr;
  5431. static const struct ephy_info e_info_8105e_1[] = {
  5432. { 0x07, 0, 0x4000 },
  5433. { 0x19, 0, 0x0200 },
  5434. { 0x19, 0, 0x0020 },
  5435. { 0x1e, 0, 0x2000 },
  5436. { 0x03, 0, 0x0001 },
  5437. { 0x19, 0, 0x0100 },
  5438. { 0x19, 0, 0x0004 },
  5439. { 0x0a, 0, 0x0020 }
  5440. };
  5441. /* Force LAN exit from ASPM if Rx/Tx are not idle */
  5442. RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
  5443. /* Disable Early Tally Counter */
  5444. RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
  5445. RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
  5446. RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
  5447. rtl_ephy_init(tp, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
  5448. rtl_pcie_state_l2l3_enable(tp, false);
  5449. }
  5450. static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
  5451. {
  5452. rtl_hw_start_8105e_1(tp);
  5453. rtl_ephy_write(tp, 0x1e, rtl_ephy_read(tp, 0x1e) | 0x8000);
  5454. }
  5455. static void rtl_hw_start_8402(struct rtl8169_private *tp)
  5456. {
  5457. void __iomem *ioaddr = tp->mmio_addr;
  5458. static const struct ephy_info e_info_8402[] = {
  5459. { 0x19, 0xffff, 0xff64 },
  5460. { 0x1e, 0, 0x4000 }
  5461. };
  5462. rtl_csi_access_enable_2(tp);
  5463. /* Force LAN exit from ASPM if Rx/Tx are not idle */
  5464. RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
  5465. RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
  5466. RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
  5467. rtl_ephy_init(tp, e_info_8402, ARRAY_SIZE(e_info_8402));
  5468. rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
  5469. rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00000002, ERIAR_EXGMAC);
  5470. rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00000006, ERIAR_EXGMAC);
  5471. rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
  5472. rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
  5473. rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
  5474. rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
  5475. rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0e00, 0xff00, ERIAR_EXGMAC);
  5476. rtl_pcie_state_l2l3_enable(tp, false);
  5477. }
  5478. static void rtl_hw_start_8106(struct rtl8169_private *tp)
  5479. {
  5480. void __iomem *ioaddr = tp->mmio_addr;
  5481. /* Force LAN exit from ASPM if Rx/Tx are not idle */
  5482. RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
  5483. RTL_W32(MISC, (RTL_R32(MISC) | DISABLE_LAN_EN) & ~EARLY_TALLY_EN);
  5484. RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
  5485. RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
  5486. rtl_pcie_state_l2l3_enable(tp, false);
  5487. }
  5488. static void rtl_hw_start_8101(struct net_device *dev)
  5489. {
  5490. struct rtl8169_private *tp = netdev_priv(dev);
  5491. void __iomem *ioaddr = tp->mmio_addr;
  5492. struct pci_dev *pdev = tp->pci_dev;
  5493. if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
  5494. tp->event_slow &= ~RxFIFOOver;
  5495. if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
  5496. tp->mac_version == RTL_GIGA_MAC_VER_16)
  5497. pcie_capability_set_word(pdev, PCI_EXP_DEVCTL,
  5498. PCI_EXP_DEVCTL_NOSNOOP_EN);
  5499. RTL_W8(Cfg9346, Cfg9346_Unlock);
  5500. RTL_W8(MaxTxPacketSize, TxPacketMax);
  5501. rtl_set_rx_max_size(ioaddr, rx_buf_sz);
  5502. tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
  5503. RTL_W16(CPlusCmd, tp->cp_cmd);
  5504. rtl_set_rx_tx_desc_registers(tp, ioaddr);
  5505. rtl_set_rx_tx_config_registers(tp);
  5506. switch (tp->mac_version) {
  5507. case RTL_GIGA_MAC_VER_07:
  5508. rtl_hw_start_8102e_1(tp);
  5509. break;
  5510. case RTL_GIGA_MAC_VER_08:
  5511. rtl_hw_start_8102e_3(tp);
  5512. break;
  5513. case RTL_GIGA_MAC_VER_09:
  5514. rtl_hw_start_8102e_2(tp);
  5515. break;
  5516. case RTL_GIGA_MAC_VER_29:
  5517. rtl_hw_start_8105e_1(tp);
  5518. break;
  5519. case RTL_GIGA_MAC_VER_30:
  5520. rtl_hw_start_8105e_2(tp);
  5521. break;
  5522. case RTL_GIGA_MAC_VER_37:
  5523. rtl_hw_start_8402(tp);
  5524. break;
  5525. case RTL_GIGA_MAC_VER_39:
  5526. rtl_hw_start_8106(tp);
  5527. break;
  5528. case RTL_GIGA_MAC_VER_43:
  5529. rtl_hw_start_8168g_2(tp);
  5530. break;
  5531. case RTL_GIGA_MAC_VER_47:
  5532. case RTL_GIGA_MAC_VER_48:
  5533. rtl_hw_start_8168h_1(tp);
  5534. break;
  5535. }
  5536. RTL_W8(Cfg9346, Cfg9346_Lock);
  5537. RTL_W16(IntrMitigate, 0x0000);
  5538. RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
  5539. rtl_set_rx_mode(dev);
  5540. RTL_R8(IntrMask);
  5541. RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
  5542. }
  5543. static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
  5544. {
  5545. struct rtl8169_private *tp = netdev_priv(dev);
  5546. if (new_mtu < ETH_ZLEN ||
  5547. new_mtu > rtl_chip_infos[tp->mac_version].jumbo_max)
  5548. return -EINVAL;
  5549. if (new_mtu > ETH_DATA_LEN)
  5550. rtl_hw_jumbo_enable(tp);
  5551. else
  5552. rtl_hw_jumbo_disable(tp);
  5553. dev->mtu = new_mtu;
  5554. netdev_update_features(dev);
  5555. return 0;
  5556. }
  5557. static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
  5558. {
  5559. desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
  5560. desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
  5561. }
  5562. static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
  5563. void **data_buff, struct RxDesc *desc)
  5564. {
  5565. dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
  5566. DMA_FROM_DEVICE);
  5567. kfree(*data_buff);
  5568. *data_buff = NULL;
  5569. rtl8169_make_unusable_by_asic(desc);
  5570. }
  5571. static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
  5572. {
  5573. u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
  5574. /* Force memory writes to complete before releasing descriptor */
  5575. dma_wmb();
  5576. desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
  5577. }
  5578. static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
  5579. u32 rx_buf_sz)
  5580. {
  5581. desc->addr = cpu_to_le64(mapping);
  5582. rtl8169_mark_to_asic(desc, rx_buf_sz);
  5583. }
  5584. static inline void *rtl8169_align(void *data)
  5585. {
  5586. return (void *)ALIGN((long)data, 16);
  5587. }
  5588. static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
  5589. struct RxDesc *desc)
  5590. {
  5591. void *data;
  5592. dma_addr_t mapping;
  5593. struct device *d = &tp->pci_dev->dev;
  5594. struct net_device *dev = tp->dev;
  5595. int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
  5596. data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
  5597. if (!data)
  5598. return NULL;
  5599. if (rtl8169_align(data) != data) {
  5600. kfree(data);
  5601. data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
  5602. if (!data)
  5603. return NULL;
  5604. }
  5605. mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
  5606. DMA_FROM_DEVICE);
  5607. if (unlikely(dma_mapping_error(d, mapping))) {
  5608. if (net_ratelimit())
  5609. netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
  5610. goto err_out;
  5611. }
  5612. rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
  5613. return data;
  5614. err_out:
  5615. kfree(data);
  5616. return NULL;
  5617. }
  5618. static void rtl8169_rx_clear(struct rtl8169_private *tp)
  5619. {
  5620. unsigned int i;
  5621. for (i = 0; i < NUM_RX_DESC; i++) {
  5622. if (tp->Rx_databuff[i]) {
  5623. rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
  5624. tp->RxDescArray + i);
  5625. }
  5626. }
  5627. }
  5628. static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
  5629. {
  5630. desc->opts1 |= cpu_to_le32(RingEnd);
  5631. }
  5632. static int rtl8169_rx_fill(struct rtl8169_private *tp)
  5633. {
  5634. unsigned int i;
  5635. for (i = 0; i < NUM_RX_DESC; i++) {
  5636. void *data;
  5637. if (tp->Rx_databuff[i])
  5638. continue;
  5639. data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
  5640. if (!data) {
  5641. rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
  5642. goto err_out;
  5643. }
  5644. tp->Rx_databuff[i] = data;
  5645. }
  5646. rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
  5647. return 0;
  5648. err_out:
  5649. rtl8169_rx_clear(tp);
  5650. return -ENOMEM;
  5651. }
  5652. static int rtl8169_init_ring(struct net_device *dev)
  5653. {
  5654. struct rtl8169_private *tp = netdev_priv(dev);
  5655. rtl8169_init_ring_indexes(tp);
  5656. memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
  5657. memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
  5658. return rtl8169_rx_fill(tp);
  5659. }
  5660. static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
  5661. struct TxDesc *desc)
  5662. {
  5663. unsigned int len = tx_skb->len;
  5664. dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
  5665. desc->opts1 = 0x00;
  5666. desc->opts2 = 0x00;
  5667. desc->addr = 0x00;
  5668. tx_skb->len = 0;
  5669. }
  5670. static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
  5671. unsigned int n)
  5672. {
  5673. unsigned int i;
  5674. for (i = 0; i < n; i++) {
  5675. unsigned int entry = (start + i) % NUM_TX_DESC;
  5676. struct ring_info *tx_skb = tp->tx_skb + entry;
  5677. unsigned int len = tx_skb->len;
  5678. if (len) {
  5679. struct sk_buff *skb = tx_skb->skb;
  5680. rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
  5681. tp->TxDescArray + entry);
  5682. if (skb) {
  5683. tp->dev->stats.tx_dropped++;
  5684. dev_kfree_skb_any(skb);
  5685. tx_skb->skb = NULL;
  5686. }
  5687. }
  5688. }
  5689. }
  5690. static void rtl8169_tx_clear(struct rtl8169_private *tp)
  5691. {
  5692. rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
  5693. tp->cur_tx = tp->dirty_tx = 0;
  5694. }
  5695. static void rtl_reset_work(struct rtl8169_private *tp)
  5696. {
  5697. struct net_device *dev = tp->dev;
  5698. int i;
  5699. napi_disable(&tp->napi);
  5700. netif_stop_queue(dev);
  5701. synchronize_sched();
  5702. rtl8169_hw_reset(tp);
  5703. for (i = 0; i < NUM_RX_DESC; i++)
  5704. rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
  5705. rtl8169_tx_clear(tp);
  5706. rtl8169_init_ring_indexes(tp);
  5707. napi_enable(&tp->napi);
  5708. rtl_hw_start(dev);
  5709. netif_wake_queue(dev);
  5710. rtl8169_check_link_status(dev, tp, tp->mmio_addr);
  5711. }
  5712. static void rtl8169_tx_timeout(struct net_device *dev)
  5713. {
  5714. struct rtl8169_private *tp = netdev_priv(dev);
  5715. rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
  5716. }
  5717. static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
  5718. u32 *opts)
  5719. {
  5720. struct skb_shared_info *info = skb_shinfo(skb);
  5721. unsigned int cur_frag, entry;
  5722. struct TxDesc *uninitialized_var(txd);
  5723. struct device *d = &tp->pci_dev->dev;
  5724. entry = tp->cur_tx;
  5725. for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
  5726. const skb_frag_t *frag = info->frags + cur_frag;
  5727. dma_addr_t mapping;
  5728. u32 status, len;
  5729. void *addr;
  5730. entry = (entry + 1) % NUM_TX_DESC;
  5731. txd = tp->TxDescArray + entry;
  5732. len = skb_frag_size(frag);
  5733. addr = skb_frag_address(frag);
  5734. mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
  5735. if (unlikely(dma_mapping_error(d, mapping))) {
  5736. if (net_ratelimit())
  5737. netif_err(tp, drv, tp->dev,
  5738. "Failed to map TX fragments DMA!\n");
  5739. goto err_out;
  5740. }
  5741. /* Anti gcc 2.95.3 bugware (sic) */
  5742. status = opts[0] | len |
  5743. (RingEnd * !((entry + 1) % NUM_TX_DESC));
  5744. txd->opts1 = cpu_to_le32(status);
  5745. txd->opts2 = cpu_to_le32(opts[1]);
  5746. txd->addr = cpu_to_le64(mapping);
  5747. tp->tx_skb[entry].len = len;
  5748. }
  5749. if (cur_frag) {
  5750. tp->tx_skb[entry].skb = skb;
  5751. txd->opts1 |= cpu_to_le32(LastFrag);
  5752. }
  5753. return cur_frag;
  5754. err_out:
  5755. rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
  5756. return -EIO;
  5757. }
  5758. static bool rtl_test_hw_pad_bug(struct rtl8169_private *tp, struct sk_buff *skb)
  5759. {
  5760. return skb->len < ETH_ZLEN && tp->mac_version == RTL_GIGA_MAC_VER_34;
  5761. }
  5762. static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
  5763. struct net_device *dev);
  5764. /* r8169_csum_workaround()
  5765. * The hw limites the value the transport offset. When the offset is out of the
  5766. * range, calculate the checksum by sw.
  5767. */
  5768. static void r8169_csum_workaround(struct rtl8169_private *tp,
  5769. struct sk_buff *skb)
  5770. {
  5771. if (skb_shinfo(skb)->gso_size) {
  5772. netdev_features_t features = tp->dev->features;
  5773. struct sk_buff *segs, *nskb;
  5774. features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
  5775. segs = skb_gso_segment(skb, features);
  5776. if (IS_ERR(segs) || !segs)
  5777. goto drop;
  5778. do {
  5779. nskb = segs;
  5780. segs = segs->next;
  5781. nskb->next = NULL;
  5782. rtl8169_start_xmit(nskb, tp->dev);
  5783. } while (segs);
  5784. dev_consume_skb_any(skb);
  5785. } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
  5786. if (skb_checksum_help(skb) < 0)
  5787. goto drop;
  5788. rtl8169_start_xmit(skb, tp->dev);
  5789. } else {
  5790. struct net_device_stats *stats;
  5791. drop:
  5792. stats = &tp->dev->stats;
  5793. stats->tx_dropped++;
  5794. dev_kfree_skb_any(skb);
  5795. }
  5796. }
  5797. /* msdn_giant_send_check()
  5798. * According to the document of microsoft, the TCP Pseudo Header excludes the
  5799. * packet length for IPv6 TCP large packets.
  5800. */
  5801. static int msdn_giant_send_check(struct sk_buff *skb)
  5802. {
  5803. const struct ipv6hdr *ipv6h;
  5804. struct tcphdr *th;
  5805. int ret;
  5806. ret = skb_cow_head(skb, 0);
  5807. if (ret)
  5808. return ret;
  5809. ipv6h = ipv6_hdr(skb);
  5810. th = tcp_hdr(skb);
  5811. th->check = 0;
  5812. th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
  5813. return ret;
  5814. }
  5815. static inline __be16 get_protocol(struct sk_buff *skb)
  5816. {
  5817. __be16 protocol;
  5818. if (skb->protocol == htons(ETH_P_8021Q))
  5819. protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
  5820. else
  5821. protocol = skb->protocol;
  5822. return protocol;
  5823. }
  5824. static bool rtl8169_tso_csum_v1(struct rtl8169_private *tp,
  5825. struct sk_buff *skb, u32 *opts)
  5826. {
  5827. u32 mss = skb_shinfo(skb)->gso_size;
  5828. if (mss) {
  5829. opts[0] |= TD_LSO;
  5830. opts[0] |= min(mss, TD_MSS_MAX) << TD0_MSS_SHIFT;
  5831. } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
  5832. const struct iphdr *ip = ip_hdr(skb);
  5833. if (ip->protocol == IPPROTO_TCP)
  5834. opts[0] |= TD0_IP_CS | TD0_TCP_CS;
  5835. else if (ip->protocol == IPPROTO_UDP)
  5836. opts[0] |= TD0_IP_CS | TD0_UDP_CS;
  5837. else
  5838. WARN_ON_ONCE(1);
  5839. }
  5840. return true;
  5841. }
  5842. static bool rtl8169_tso_csum_v2(struct rtl8169_private *tp,
  5843. struct sk_buff *skb, u32 *opts)
  5844. {
  5845. u32 transport_offset = (u32)skb_transport_offset(skb);
  5846. u32 mss = skb_shinfo(skb)->gso_size;
  5847. if (mss) {
  5848. if (transport_offset > GTTCPHO_MAX) {
  5849. netif_warn(tp, tx_err, tp->dev,
  5850. "Invalid transport offset 0x%x for TSO\n",
  5851. transport_offset);
  5852. return false;
  5853. }
  5854. switch (get_protocol(skb)) {
  5855. case htons(ETH_P_IP):
  5856. opts[0] |= TD1_GTSENV4;
  5857. break;
  5858. case htons(ETH_P_IPV6):
  5859. if (msdn_giant_send_check(skb))
  5860. return false;
  5861. opts[0] |= TD1_GTSENV6;
  5862. break;
  5863. default:
  5864. WARN_ON_ONCE(1);
  5865. break;
  5866. }
  5867. opts[0] |= transport_offset << GTTCPHO_SHIFT;
  5868. opts[1] |= min(mss, TD_MSS_MAX) << TD1_MSS_SHIFT;
  5869. } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
  5870. u8 ip_protocol;
  5871. if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
  5872. return !(skb_checksum_help(skb) || eth_skb_pad(skb));
  5873. if (transport_offset > TCPHO_MAX) {
  5874. netif_warn(tp, tx_err, tp->dev,
  5875. "Invalid transport offset 0x%x\n",
  5876. transport_offset);
  5877. return false;
  5878. }
  5879. switch (get_protocol(skb)) {
  5880. case htons(ETH_P_IP):
  5881. opts[1] |= TD1_IPv4_CS;
  5882. ip_protocol = ip_hdr(skb)->protocol;
  5883. break;
  5884. case htons(ETH_P_IPV6):
  5885. opts[1] |= TD1_IPv6_CS;
  5886. ip_protocol = ipv6_hdr(skb)->nexthdr;
  5887. break;
  5888. default:
  5889. ip_protocol = IPPROTO_RAW;
  5890. break;
  5891. }
  5892. if (ip_protocol == IPPROTO_TCP)
  5893. opts[1] |= TD1_TCP_CS;
  5894. else if (ip_protocol == IPPROTO_UDP)
  5895. opts[1] |= TD1_UDP_CS;
  5896. else
  5897. WARN_ON_ONCE(1);
  5898. opts[1] |= transport_offset << TCPHO_SHIFT;
  5899. } else {
  5900. if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
  5901. return !eth_skb_pad(skb);
  5902. }
  5903. return true;
  5904. }
  5905. static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
  5906. struct net_device *dev)
  5907. {
  5908. struct rtl8169_private *tp = netdev_priv(dev);
  5909. unsigned int entry = tp->cur_tx % NUM_TX_DESC;
  5910. struct TxDesc *txd = tp->TxDescArray + entry;
  5911. void __iomem *ioaddr = tp->mmio_addr;
  5912. struct device *d = &tp->pci_dev->dev;
  5913. dma_addr_t mapping;
  5914. u32 status, len;
  5915. u32 opts[2];
  5916. int frags;
  5917. if (unlikely(!TX_FRAGS_READY_FOR(tp, skb_shinfo(skb)->nr_frags))) {
  5918. netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
  5919. goto err_stop_0;
  5920. }
  5921. if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
  5922. goto err_stop_0;
  5923. opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(skb));
  5924. opts[0] = DescOwn;
  5925. if (!tp->tso_csum(tp, skb, opts)) {
  5926. r8169_csum_workaround(tp, skb);
  5927. return NETDEV_TX_OK;
  5928. }
  5929. len = skb_headlen(skb);
  5930. mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
  5931. if (unlikely(dma_mapping_error(d, mapping))) {
  5932. if (net_ratelimit())
  5933. netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
  5934. goto err_dma_0;
  5935. }
  5936. tp->tx_skb[entry].len = len;
  5937. txd->addr = cpu_to_le64(mapping);
  5938. frags = rtl8169_xmit_frags(tp, skb, opts);
  5939. if (frags < 0)
  5940. goto err_dma_1;
  5941. else if (frags)
  5942. opts[0] |= FirstFrag;
  5943. else {
  5944. opts[0] |= FirstFrag | LastFrag;
  5945. tp->tx_skb[entry].skb = skb;
  5946. }
  5947. txd->opts2 = cpu_to_le32(opts[1]);
  5948. skb_tx_timestamp(skb);
  5949. /* Force memory writes to complete before releasing descriptor */
  5950. dma_wmb();
  5951. /* Anti gcc 2.95.3 bugware (sic) */
  5952. status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
  5953. txd->opts1 = cpu_to_le32(status);
  5954. /* Force all memory writes to complete before notifying device */
  5955. wmb();
  5956. tp->cur_tx += frags + 1;
  5957. RTL_W8(TxPoll, NPQ);
  5958. mmiowb();
  5959. if (!TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
  5960. /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
  5961. * not miss a ring update when it notices a stopped queue.
  5962. */
  5963. smp_wmb();
  5964. netif_stop_queue(dev);
  5965. /* Sync with rtl_tx:
  5966. * - publish queue status and cur_tx ring index (write barrier)
  5967. * - refresh dirty_tx ring index (read barrier).
  5968. * May the current thread have a pessimistic view of the ring
  5969. * status and forget to wake up queue, a racing rtl_tx thread
  5970. * can't.
  5971. */
  5972. smp_mb();
  5973. if (TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS))
  5974. netif_wake_queue(dev);
  5975. }
  5976. return NETDEV_TX_OK;
  5977. err_dma_1:
  5978. rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
  5979. err_dma_0:
  5980. dev_kfree_skb_any(skb);
  5981. dev->stats.tx_dropped++;
  5982. return NETDEV_TX_OK;
  5983. err_stop_0:
  5984. netif_stop_queue(dev);
  5985. dev->stats.tx_dropped++;
  5986. return NETDEV_TX_BUSY;
  5987. }
  5988. static void rtl8169_pcierr_interrupt(struct net_device *dev)
  5989. {
  5990. struct rtl8169_private *tp = netdev_priv(dev);
  5991. struct pci_dev *pdev = tp->pci_dev;
  5992. u16 pci_status, pci_cmd;
  5993. pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
  5994. pci_read_config_word(pdev, PCI_STATUS, &pci_status);
  5995. netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
  5996. pci_cmd, pci_status);
  5997. /*
  5998. * The recovery sequence below admits a very elaborated explanation:
  5999. * - it seems to work;
  6000. * - I did not see what else could be done;
  6001. * - it makes iop3xx happy.
  6002. *
  6003. * Feel free to adjust to your needs.
  6004. */
  6005. if (pdev->broken_parity_status)
  6006. pci_cmd &= ~PCI_COMMAND_PARITY;
  6007. else
  6008. pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
  6009. pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
  6010. pci_write_config_word(pdev, PCI_STATUS,
  6011. pci_status & (PCI_STATUS_DETECTED_PARITY |
  6012. PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
  6013. PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
  6014. /* The infamous DAC f*ckup only happens at boot time */
  6015. if ((tp->cp_cmd & PCIDAC) && !tp->cur_rx) {
  6016. void __iomem *ioaddr = tp->mmio_addr;
  6017. netif_info(tp, intr, dev, "disabling PCI DAC\n");
  6018. tp->cp_cmd &= ~PCIDAC;
  6019. RTL_W16(CPlusCmd, tp->cp_cmd);
  6020. dev->features &= ~NETIF_F_HIGHDMA;
  6021. }
  6022. rtl8169_hw_reset(tp);
  6023. rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
  6024. }
  6025. static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
  6026. {
  6027. unsigned int dirty_tx, tx_left;
  6028. dirty_tx = tp->dirty_tx;
  6029. smp_rmb();
  6030. tx_left = tp->cur_tx - dirty_tx;
  6031. while (tx_left > 0) {
  6032. unsigned int entry = dirty_tx % NUM_TX_DESC;
  6033. struct ring_info *tx_skb = tp->tx_skb + entry;
  6034. u32 status;
  6035. status = le32_to_cpu(tp->TxDescArray[entry].opts1);
  6036. if (status & DescOwn)
  6037. break;
  6038. /* This barrier is needed to keep us from reading
  6039. * any other fields out of the Tx descriptor until
  6040. * we know the status of DescOwn
  6041. */
  6042. dma_rmb();
  6043. rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
  6044. tp->TxDescArray + entry);
  6045. if (status & LastFrag) {
  6046. u64_stats_update_begin(&tp->tx_stats.syncp);
  6047. tp->tx_stats.packets++;
  6048. tp->tx_stats.bytes += tx_skb->skb->len;
  6049. u64_stats_update_end(&tp->tx_stats.syncp);
  6050. dev_kfree_skb_any(tx_skb->skb);
  6051. tx_skb->skb = NULL;
  6052. }
  6053. dirty_tx++;
  6054. tx_left--;
  6055. }
  6056. if (tp->dirty_tx != dirty_tx) {
  6057. tp->dirty_tx = dirty_tx;
  6058. /* Sync with rtl8169_start_xmit:
  6059. * - publish dirty_tx ring index (write barrier)
  6060. * - refresh cur_tx ring index and queue status (read barrier)
  6061. * May the current thread miss the stopped queue condition,
  6062. * a racing xmit thread can only have a right view of the
  6063. * ring status.
  6064. */
  6065. smp_mb();
  6066. if (netif_queue_stopped(dev) &&
  6067. TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
  6068. netif_wake_queue(dev);
  6069. }
  6070. /*
  6071. * 8168 hack: TxPoll requests are lost when the Tx packets are
  6072. * too close. Let's kick an extra TxPoll request when a burst
  6073. * of start_xmit activity is detected (if it is not detected,
  6074. * it is slow enough). -- FR
  6075. */
  6076. if (tp->cur_tx != dirty_tx) {
  6077. void __iomem *ioaddr = tp->mmio_addr;
  6078. RTL_W8(TxPoll, NPQ);
  6079. }
  6080. }
  6081. }
  6082. static inline int rtl8169_fragmented_frame(u32 status)
  6083. {
  6084. return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
  6085. }
  6086. static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
  6087. {
  6088. u32 status = opts1 & RxProtoMask;
  6089. if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
  6090. ((status == RxProtoUDP) && !(opts1 & UDPFail)))
  6091. skb->ip_summed = CHECKSUM_UNNECESSARY;
  6092. else
  6093. skb_checksum_none_assert(skb);
  6094. }
  6095. static struct sk_buff *rtl8169_try_rx_copy(void *data,
  6096. struct rtl8169_private *tp,
  6097. int pkt_size,
  6098. dma_addr_t addr)
  6099. {
  6100. struct sk_buff *skb;
  6101. struct device *d = &tp->pci_dev->dev;
  6102. data = rtl8169_align(data);
  6103. dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
  6104. prefetch(data);
  6105. skb = napi_alloc_skb(&tp->napi, pkt_size);
  6106. if (skb)
  6107. memcpy(skb->data, data, pkt_size);
  6108. dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
  6109. return skb;
  6110. }
  6111. static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
  6112. {
  6113. unsigned int cur_rx, rx_left;
  6114. unsigned int count;
  6115. cur_rx = tp->cur_rx;
  6116. for (rx_left = min(budget, NUM_RX_DESC); rx_left > 0; rx_left--, cur_rx++) {
  6117. unsigned int entry = cur_rx % NUM_RX_DESC;
  6118. struct RxDesc *desc = tp->RxDescArray + entry;
  6119. u32 status;
  6120. status = le32_to_cpu(desc->opts1) & tp->opts1_mask;
  6121. if (status & DescOwn)
  6122. break;
  6123. /* This barrier is needed to keep us from reading
  6124. * any other fields out of the Rx descriptor until
  6125. * we know the status of DescOwn
  6126. */
  6127. dma_rmb();
  6128. if (unlikely(status & RxRES)) {
  6129. netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
  6130. status);
  6131. dev->stats.rx_errors++;
  6132. if (status & (RxRWT | RxRUNT))
  6133. dev->stats.rx_length_errors++;
  6134. if (status & RxCRC)
  6135. dev->stats.rx_crc_errors++;
  6136. if (status & RxFOVF) {
  6137. rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
  6138. dev->stats.rx_fifo_errors++;
  6139. }
  6140. if ((status & (RxRUNT | RxCRC)) &&
  6141. !(status & (RxRWT | RxFOVF)) &&
  6142. (dev->features & NETIF_F_RXALL))
  6143. goto process_pkt;
  6144. } else {
  6145. struct sk_buff *skb;
  6146. dma_addr_t addr;
  6147. int pkt_size;
  6148. process_pkt:
  6149. addr = le64_to_cpu(desc->addr);
  6150. if (likely(!(dev->features & NETIF_F_RXFCS)))
  6151. pkt_size = (status & 0x00003fff) - 4;
  6152. else
  6153. pkt_size = status & 0x00003fff;
  6154. /*
  6155. * The driver does not support incoming fragmented
  6156. * frames. They are seen as a symptom of over-mtu
  6157. * sized frames.
  6158. */
  6159. if (unlikely(rtl8169_fragmented_frame(status))) {
  6160. dev->stats.rx_dropped++;
  6161. dev->stats.rx_length_errors++;
  6162. goto release_descriptor;
  6163. }
  6164. skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
  6165. tp, pkt_size, addr);
  6166. if (!skb) {
  6167. dev->stats.rx_dropped++;
  6168. goto release_descriptor;
  6169. }
  6170. rtl8169_rx_csum(skb, status);
  6171. skb_put(skb, pkt_size);
  6172. skb->protocol = eth_type_trans(skb, dev);
  6173. rtl8169_rx_vlan_tag(desc, skb);
  6174. napi_gro_receive(&tp->napi, skb);
  6175. u64_stats_update_begin(&tp->rx_stats.syncp);
  6176. tp->rx_stats.packets++;
  6177. tp->rx_stats.bytes += pkt_size;
  6178. u64_stats_update_end(&tp->rx_stats.syncp);
  6179. if (skb->pkt_type == PACKET_MULTICAST)
  6180. dev->stats.multicast++;
  6181. }
  6182. release_descriptor:
  6183. desc->opts2 = 0;
  6184. rtl8169_mark_to_asic(desc, rx_buf_sz);
  6185. }
  6186. count = cur_rx - tp->cur_rx;
  6187. tp->cur_rx = cur_rx;
  6188. return count;
  6189. }
  6190. static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
  6191. {
  6192. struct net_device *dev = dev_instance;
  6193. struct rtl8169_private *tp = netdev_priv(dev);
  6194. int handled = 0;
  6195. u16 status;
  6196. status = rtl_get_events(tp);
  6197. if (status && status != 0xffff) {
  6198. status &= RTL_EVENT_NAPI | tp->event_slow;
  6199. if (status) {
  6200. handled = 1;
  6201. rtl_irq_disable(tp);
  6202. napi_schedule(&tp->napi);
  6203. }
  6204. }
  6205. return IRQ_RETVAL(handled);
  6206. }
  6207. /*
  6208. * Workqueue context.
  6209. */
  6210. static void rtl_slow_event_work(struct rtl8169_private *tp)
  6211. {
  6212. struct net_device *dev = tp->dev;
  6213. u16 status;
  6214. status = rtl_get_events(tp) & tp->event_slow;
  6215. rtl_ack_events(tp, status);
  6216. if (unlikely(status & RxFIFOOver)) {
  6217. switch (tp->mac_version) {
  6218. /* Work around for rx fifo overflow */
  6219. case RTL_GIGA_MAC_VER_11:
  6220. netif_stop_queue(dev);
  6221. /* XXX - Hack alert. See rtl_task(). */
  6222. set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
  6223. default:
  6224. break;
  6225. }
  6226. }
  6227. if (unlikely(status & SYSErr))
  6228. rtl8169_pcierr_interrupt(dev);
  6229. if (status & LinkChg)
  6230. __rtl8169_check_link_status(dev, tp, tp->mmio_addr, true);
  6231. rtl_irq_enable_all(tp);
  6232. }
  6233. static void rtl_task(struct work_struct *work)
  6234. {
  6235. static const struct {
  6236. int bitnr;
  6237. void (*action)(struct rtl8169_private *);
  6238. } rtl_work[] = {
  6239. /* XXX - keep rtl_slow_event_work() as first element. */
  6240. { RTL_FLAG_TASK_SLOW_PENDING, rtl_slow_event_work },
  6241. { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
  6242. { RTL_FLAG_TASK_PHY_PENDING, rtl_phy_work }
  6243. };
  6244. struct rtl8169_private *tp =
  6245. container_of(work, struct rtl8169_private, wk.work);
  6246. struct net_device *dev = tp->dev;
  6247. int i;
  6248. rtl_lock_work(tp);
  6249. if (!netif_running(dev) ||
  6250. !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
  6251. goto out_unlock;
  6252. for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
  6253. bool pending;
  6254. pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
  6255. if (pending)
  6256. rtl_work[i].action(tp);
  6257. }
  6258. out_unlock:
  6259. rtl_unlock_work(tp);
  6260. }
  6261. static int rtl8169_poll(struct napi_struct *napi, int budget)
  6262. {
  6263. struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
  6264. struct net_device *dev = tp->dev;
  6265. u16 enable_mask = RTL_EVENT_NAPI | tp->event_slow;
  6266. int work_done= 0;
  6267. u16 status;
  6268. status = rtl_get_events(tp);
  6269. rtl_ack_events(tp, status & ~tp->event_slow);
  6270. if (status & RTL_EVENT_NAPI_RX)
  6271. work_done = rtl_rx(dev, tp, (u32) budget);
  6272. if (status & RTL_EVENT_NAPI_TX)
  6273. rtl_tx(dev, tp);
  6274. if (status & tp->event_slow) {
  6275. enable_mask &= ~tp->event_slow;
  6276. rtl_schedule_task(tp, RTL_FLAG_TASK_SLOW_PENDING);
  6277. }
  6278. if (work_done < budget) {
  6279. napi_complete(napi);
  6280. rtl_irq_enable(tp, enable_mask);
  6281. mmiowb();
  6282. }
  6283. return work_done;
  6284. }
  6285. static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
  6286. {
  6287. struct rtl8169_private *tp = netdev_priv(dev);
  6288. if (tp->mac_version > RTL_GIGA_MAC_VER_06)
  6289. return;
  6290. dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
  6291. RTL_W32(RxMissed, 0);
  6292. }
  6293. static void rtl8169_down(struct net_device *dev)
  6294. {
  6295. struct rtl8169_private *tp = netdev_priv(dev);
  6296. void __iomem *ioaddr = tp->mmio_addr;
  6297. del_timer_sync(&tp->timer);
  6298. napi_disable(&tp->napi);
  6299. netif_stop_queue(dev);
  6300. rtl8169_hw_reset(tp);
  6301. /*
  6302. * At this point device interrupts can not be enabled in any function,
  6303. * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
  6304. * and napi is disabled (rtl8169_poll).
  6305. */
  6306. rtl8169_rx_missed(dev, ioaddr);
  6307. /* Give a racing hard_start_xmit a few cycles to complete. */
  6308. synchronize_sched();
  6309. rtl8169_tx_clear(tp);
  6310. rtl8169_rx_clear(tp);
  6311. rtl_pll_power_down(tp);
  6312. }
  6313. static int rtl8169_close(struct net_device *dev)
  6314. {
  6315. struct rtl8169_private *tp = netdev_priv(dev);
  6316. struct pci_dev *pdev = tp->pci_dev;
  6317. pm_runtime_get_sync(&pdev->dev);
  6318. /* Update counters before going down */
  6319. rtl8169_update_counters(dev);
  6320. rtl_lock_work(tp);
  6321. clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
  6322. rtl8169_down(dev);
  6323. rtl_unlock_work(tp);
  6324. cancel_work_sync(&tp->wk.work);
  6325. free_irq(pdev->irq, dev);
  6326. dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
  6327. tp->RxPhyAddr);
  6328. dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
  6329. tp->TxPhyAddr);
  6330. tp->TxDescArray = NULL;
  6331. tp->RxDescArray = NULL;
  6332. pm_runtime_put_sync(&pdev->dev);
  6333. return 0;
  6334. }
  6335. #ifdef CONFIG_NET_POLL_CONTROLLER
  6336. static void rtl8169_netpoll(struct net_device *dev)
  6337. {
  6338. struct rtl8169_private *tp = netdev_priv(dev);
  6339. rtl8169_interrupt(tp->pci_dev->irq, dev);
  6340. }
  6341. #endif
  6342. static int rtl_open(struct net_device *dev)
  6343. {
  6344. struct rtl8169_private *tp = netdev_priv(dev);
  6345. void __iomem *ioaddr = tp->mmio_addr;
  6346. struct pci_dev *pdev = tp->pci_dev;
  6347. int retval = -ENOMEM;
  6348. pm_runtime_get_sync(&pdev->dev);
  6349. /*
  6350. * Rx and Tx descriptors needs 256 bytes alignment.
  6351. * dma_alloc_coherent provides more.
  6352. */
  6353. tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
  6354. &tp->TxPhyAddr, GFP_KERNEL);
  6355. if (!tp->TxDescArray)
  6356. goto err_pm_runtime_put;
  6357. tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
  6358. &tp->RxPhyAddr, GFP_KERNEL);
  6359. if (!tp->RxDescArray)
  6360. goto err_free_tx_0;
  6361. retval = rtl8169_init_ring(dev);
  6362. if (retval < 0)
  6363. goto err_free_rx_1;
  6364. INIT_WORK(&tp->wk.work, rtl_task);
  6365. smp_mb();
  6366. rtl_request_firmware(tp);
  6367. retval = request_irq(pdev->irq, rtl8169_interrupt,
  6368. (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
  6369. dev->name, dev);
  6370. if (retval < 0)
  6371. goto err_release_fw_2;
  6372. rtl_lock_work(tp);
  6373. set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
  6374. napi_enable(&tp->napi);
  6375. rtl8169_init_phy(dev, tp);
  6376. __rtl8169_set_features(dev, dev->features);
  6377. rtl_pll_power_up(tp);
  6378. rtl_hw_start(dev);
  6379. if (!rtl8169_init_counter_offsets(dev))
  6380. netif_warn(tp, hw, dev, "counter reset/update failed\n");
  6381. netif_start_queue(dev);
  6382. rtl_unlock_work(tp);
  6383. tp->saved_wolopts = 0;
  6384. pm_runtime_put_noidle(&pdev->dev);
  6385. rtl8169_check_link_status(dev, tp, ioaddr);
  6386. out:
  6387. return retval;
  6388. err_release_fw_2:
  6389. rtl_release_firmware(tp);
  6390. rtl8169_rx_clear(tp);
  6391. err_free_rx_1:
  6392. dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
  6393. tp->RxPhyAddr);
  6394. tp->RxDescArray = NULL;
  6395. err_free_tx_0:
  6396. dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
  6397. tp->TxPhyAddr);
  6398. tp->TxDescArray = NULL;
  6399. err_pm_runtime_put:
  6400. pm_runtime_put_noidle(&pdev->dev);
  6401. goto out;
  6402. }
  6403. static struct rtnl_link_stats64 *
  6404. rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
  6405. {
  6406. struct rtl8169_private *tp = netdev_priv(dev);
  6407. void __iomem *ioaddr = tp->mmio_addr;
  6408. struct rtl8169_counters *counters = tp->counters;
  6409. unsigned int start;
  6410. if (netif_running(dev))
  6411. rtl8169_rx_missed(dev, ioaddr);
  6412. do {
  6413. start = u64_stats_fetch_begin_irq(&tp->rx_stats.syncp);
  6414. stats->rx_packets = tp->rx_stats.packets;
  6415. stats->rx_bytes = tp->rx_stats.bytes;
  6416. } while (u64_stats_fetch_retry_irq(&tp->rx_stats.syncp, start));
  6417. do {
  6418. start = u64_stats_fetch_begin_irq(&tp->tx_stats.syncp);
  6419. stats->tx_packets = tp->tx_stats.packets;
  6420. stats->tx_bytes = tp->tx_stats.bytes;
  6421. } while (u64_stats_fetch_retry_irq(&tp->tx_stats.syncp, start));
  6422. stats->rx_dropped = dev->stats.rx_dropped;
  6423. stats->tx_dropped = dev->stats.tx_dropped;
  6424. stats->rx_length_errors = dev->stats.rx_length_errors;
  6425. stats->rx_errors = dev->stats.rx_errors;
  6426. stats->rx_crc_errors = dev->stats.rx_crc_errors;
  6427. stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
  6428. stats->rx_missed_errors = dev->stats.rx_missed_errors;
  6429. stats->multicast = dev->stats.multicast;
  6430. /*
  6431. * Fetch additonal counter values missing in stats collected by driver
  6432. * from tally counters.
  6433. */
  6434. rtl8169_update_counters(dev);
  6435. /*
  6436. * Subtract values fetched during initalization.
  6437. * See rtl8169_init_counter_offsets for a description why we do that.
  6438. */
  6439. stats->tx_errors = le64_to_cpu(counters->tx_errors) -
  6440. le64_to_cpu(tp->tc_offset.tx_errors);
  6441. stats->collisions = le32_to_cpu(counters->tx_multi_collision) -
  6442. le32_to_cpu(tp->tc_offset.tx_multi_collision);
  6443. stats->tx_aborted_errors = le16_to_cpu(counters->tx_aborted) -
  6444. le16_to_cpu(tp->tc_offset.tx_aborted);
  6445. return stats;
  6446. }
  6447. static void rtl8169_net_suspend(struct net_device *dev)
  6448. {
  6449. struct rtl8169_private *tp = netdev_priv(dev);
  6450. if (!netif_running(dev))
  6451. return;
  6452. netif_device_detach(dev);
  6453. netif_stop_queue(dev);
  6454. rtl_lock_work(tp);
  6455. napi_disable(&tp->napi);
  6456. clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
  6457. rtl_unlock_work(tp);
  6458. rtl_pll_power_down(tp);
  6459. }
  6460. #ifdef CONFIG_PM
  6461. static int rtl8169_suspend(struct device *device)
  6462. {
  6463. struct pci_dev *pdev = to_pci_dev(device);
  6464. struct net_device *dev = pci_get_drvdata(pdev);
  6465. rtl8169_net_suspend(dev);
  6466. return 0;
  6467. }
  6468. static void __rtl8169_resume(struct net_device *dev)
  6469. {
  6470. struct rtl8169_private *tp = netdev_priv(dev);
  6471. netif_device_attach(dev);
  6472. rtl_pll_power_up(tp);
  6473. rtl_lock_work(tp);
  6474. napi_enable(&tp->napi);
  6475. set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
  6476. rtl_unlock_work(tp);
  6477. rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
  6478. }
  6479. static int rtl8169_resume(struct device *device)
  6480. {
  6481. struct pci_dev *pdev = to_pci_dev(device);
  6482. struct net_device *dev = pci_get_drvdata(pdev);
  6483. struct rtl8169_private *tp = netdev_priv(dev);
  6484. rtl8169_init_phy(dev, tp);
  6485. if (netif_running(dev))
  6486. __rtl8169_resume(dev);
  6487. return 0;
  6488. }
  6489. static int rtl8169_runtime_suspend(struct device *device)
  6490. {
  6491. struct pci_dev *pdev = to_pci_dev(device);
  6492. struct net_device *dev = pci_get_drvdata(pdev);
  6493. struct rtl8169_private *tp = netdev_priv(dev);
  6494. if (!tp->TxDescArray)
  6495. return 0;
  6496. rtl_lock_work(tp);
  6497. tp->saved_wolopts = __rtl8169_get_wol(tp);
  6498. __rtl8169_set_wol(tp, WAKE_ANY);
  6499. rtl_unlock_work(tp);
  6500. rtl8169_net_suspend(dev);
  6501. return 0;
  6502. }
  6503. static int rtl8169_runtime_resume(struct device *device)
  6504. {
  6505. struct pci_dev *pdev = to_pci_dev(device);
  6506. struct net_device *dev = pci_get_drvdata(pdev);
  6507. struct rtl8169_private *tp = netdev_priv(dev);
  6508. if (!tp->TxDescArray)
  6509. return 0;
  6510. rtl_lock_work(tp);
  6511. __rtl8169_set_wol(tp, tp->saved_wolopts);
  6512. tp->saved_wolopts = 0;
  6513. rtl_unlock_work(tp);
  6514. rtl8169_init_phy(dev, tp);
  6515. __rtl8169_resume(dev);
  6516. return 0;
  6517. }
  6518. static int rtl8169_runtime_idle(struct device *device)
  6519. {
  6520. struct pci_dev *pdev = to_pci_dev(device);
  6521. struct net_device *dev = pci_get_drvdata(pdev);
  6522. struct rtl8169_private *tp = netdev_priv(dev);
  6523. return tp->TxDescArray ? -EBUSY : 0;
  6524. }
  6525. static const struct dev_pm_ops rtl8169_pm_ops = {
  6526. .suspend = rtl8169_suspend,
  6527. .resume = rtl8169_resume,
  6528. .freeze = rtl8169_suspend,
  6529. .thaw = rtl8169_resume,
  6530. .poweroff = rtl8169_suspend,
  6531. .restore = rtl8169_resume,
  6532. .runtime_suspend = rtl8169_runtime_suspend,
  6533. .runtime_resume = rtl8169_runtime_resume,
  6534. .runtime_idle = rtl8169_runtime_idle,
  6535. };
  6536. #define RTL8169_PM_OPS (&rtl8169_pm_ops)
  6537. #else /* !CONFIG_PM */
  6538. #define RTL8169_PM_OPS NULL
  6539. #endif /* !CONFIG_PM */
  6540. static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
  6541. {
  6542. void __iomem *ioaddr = tp->mmio_addr;
  6543. /* WoL fails with 8168b when the receiver is disabled. */
  6544. switch (tp->mac_version) {
  6545. case RTL_GIGA_MAC_VER_11:
  6546. case RTL_GIGA_MAC_VER_12:
  6547. case RTL_GIGA_MAC_VER_17:
  6548. pci_clear_master(tp->pci_dev);
  6549. RTL_W8(ChipCmd, CmdRxEnb);
  6550. /* PCI commit */
  6551. RTL_R8(ChipCmd);
  6552. break;
  6553. default:
  6554. break;
  6555. }
  6556. }
  6557. static void rtl_shutdown(struct pci_dev *pdev)
  6558. {
  6559. struct net_device *dev = pci_get_drvdata(pdev);
  6560. struct rtl8169_private *tp = netdev_priv(dev);
  6561. struct device *d = &pdev->dev;
  6562. pm_runtime_get_sync(d);
  6563. rtl8169_net_suspend(dev);
  6564. /* Restore original MAC address */
  6565. rtl_rar_set(tp, dev->perm_addr);
  6566. rtl8169_hw_reset(tp);
  6567. if (system_state == SYSTEM_POWER_OFF) {
  6568. if (__rtl8169_get_wol(tp) & WAKE_ANY) {
  6569. rtl_wol_suspend_quirk(tp);
  6570. rtl_wol_shutdown_quirk(tp);
  6571. }
  6572. pci_wake_from_d3(pdev, true);
  6573. pci_set_power_state(pdev, PCI_D3hot);
  6574. }
  6575. pm_runtime_put_noidle(d);
  6576. }
  6577. static void rtl_remove_one(struct pci_dev *pdev)
  6578. {
  6579. struct net_device *dev = pci_get_drvdata(pdev);
  6580. struct rtl8169_private *tp = netdev_priv(dev);
  6581. if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
  6582. tp->mac_version == RTL_GIGA_MAC_VER_28 ||
  6583. tp->mac_version == RTL_GIGA_MAC_VER_31 ||
  6584. tp->mac_version == RTL_GIGA_MAC_VER_49 ||
  6585. tp->mac_version == RTL_GIGA_MAC_VER_50 ||
  6586. tp->mac_version == RTL_GIGA_MAC_VER_51) &&
  6587. r8168_check_dash(tp)) {
  6588. rtl8168_driver_stop(tp);
  6589. }
  6590. netif_napi_del(&tp->napi);
  6591. unregister_netdev(dev);
  6592. dma_free_coherent(&tp->pci_dev->dev, sizeof(*tp->counters),
  6593. tp->counters, tp->counters_phys_addr);
  6594. rtl_release_firmware(tp);
  6595. if (pci_dev_run_wake(pdev))
  6596. pm_runtime_get_noresume(&pdev->dev);
  6597. /* restore original MAC address */
  6598. rtl_rar_set(tp, dev->perm_addr);
  6599. rtl_disable_msi(pdev, tp);
  6600. rtl8169_release_board(pdev, dev, tp->mmio_addr);
  6601. }
  6602. static const struct net_device_ops rtl_netdev_ops = {
  6603. .ndo_open = rtl_open,
  6604. .ndo_stop = rtl8169_close,
  6605. .ndo_get_stats64 = rtl8169_get_stats64,
  6606. .ndo_start_xmit = rtl8169_start_xmit,
  6607. .ndo_tx_timeout = rtl8169_tx_timeout,
  6608. .ndo_validate_addr = eth_validate_addr,
  6609. .ndo_change_mtu = rtl8169_change_mtu,
  6610. .ndo_fix_features = rtl8169_fix_features,
  6611. .ndo_set_features = rtl8169_set_features,
  6612. .ndo_set_mac_address = rtl_set_mac_address,
  6613. .ndo_do_ioctl = rtl8169_ioctl,
  6614. .ndo_set_rx_mode = rtl_set_rx_mode,
  6615. #ifdef CONFIG_NET_POLL_CONTROLLER
  6616. .ndo_poll_controller = rtl8169_netpoll,
  6617. #endif
  6618. };
  6619. static const struct rtl_cfg_info {
  6620. void (*hw_start)(struct net_device *);
  6621. unsigned int region;
  6622. unsigned int align;
  6623. u16 event_slow;
  6624. unsigned features;
  6625. u8 default_ver;
  6626. } rtl_cfg_infos [] = {
  6627. [RTL_CFG_0] = {
  6628. .hw_start = rtl_hw_start_8169,
  6629. .region = 1,
  6630. .align = 0,
  6631. .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
  6632. .features = RTL_FEATURE_GMII,
  6633. .default_ver = RTL_GIGA_MAC_VER_01,
  6634. },
  6635. [RTL_CFG_1] = {
  6636. .hw_start = rtl_hw_start_8168,
  6637. .region = 2,
  6638. .align = 8,
  6639. .event_slow = SYSErr | LinkChg | RxOverflow,
  6640. .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
  6641. .default_ver = RTL_GIGA_MAC_VER_11,
  6642. },
  6643. [RTL_CFG_2] = {
  6644. .hw_start = rtl_hw_start_8101,
  6645. .region = 2,
  6646. .align = 8,
  6647. .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver |
  6648. PCSTimeout,
  6649. .features = RTL_FEATURE_MSI,
  6650. .default_ver = RTL_GIGA_MAC_VER_13,
  6651. }
  6652. };
  6653. /* Cfg9346_Unlock assumed. */
  6654. static unsigned rtl_try_msi(struct rtl8169_private *tp,
  6655. const struct rtl_cfg_info *cfg)
  6656. {
  6657. void __iomem *ioaddr = tp->mmio_addr;
  6658. unsigned msi = 0;
  6659. u8 cfg2;
  6660. cfg2 = RTL_R8(Config2) & ~MSIEnable;
  6661. if (cfg->features & RTL_FEATURE_MSI) {
  6662. if (pci_enable_msi(tp->pci_dev)) {
  6663. netif_info(tp, hw, tp->dev, "no MSI. Back to INTx.\n");
  6664. } else {
  6665. cfg2 |= MSIEnable;
  6666. msi = RTL_FEATURE_MSI;
  6667. }
  6668. }
  6669. if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
  6670. RTL_W8(Config2, cfg2);
  6671. return msi;
  6672. }
  6673. DECLARE_RTL_COND(rtl_link_list_ready_cond)
  6674. {
  6675. void __iomem *ioaddr = tp->mmio_addr;
  6676. return RTL_R8(MCU) & LINK_LIST_RDY;
  6677. }
  6678. DECLARE_RTL_COND(rtl_rxtx_empty_cond)
  6679. {
  6680. void __iomem *ioaddr = tp->mmio_addr;
  6681. return (RTL_R8(MCU) & RXTX_EMPTY) == RXTX_EMPTY;
  6682. }
  6683. static void rtl_hw_init_8168g(struct rtl8169_private *tp)
  6684. {
  6685. void __iomem *ioaddr = tp->mmio_addr;
  6686. u32 data;
  6687. tp->ocp_base = OCP_STD_PHY_BASE;
  6688. RTL_W32(MISC, RTL_R32(MISC) | RXDV_GATED_EN);
  6689. if (!rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 42))
  6690. return;
  6691. if (!rtl_udelay_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42))
  6692. return;
  6693. RTL_W8(ChipCmd, RTL_R8(ChipCmd) & ~(CmdTxEnb | CmdRxEnb));
  6694. msleep(1);
  6695. RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
  6696. data = r8168_mac_ocp_read(tp, 0xe8de);
  6697. data &= ~(1 << 14);
  6698. r8168_mac_ocp_write(tp, 0xe8de, data);
  6699. if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
  6700. return;
  6701. data = r8168_mac_ocp_read(tp, 0xe8de);
  6702. data |= (1 << 15);
  6703. r8168_mac_ocp_write(tp, 0xe8de, data);
  6704. if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
  6705. return;
  6706. }
  6707. static void rtl_hw_init_8168ep(struct rtl8169_private *tp)
  6708. {
  6709. rtl8168ep_stop_cmac(tp);
  6710. rtl_hw_init_8168g(tp);
  6711. }
  6712. static void rtl_hw_initialize(struct rtl8169_private *tp)
  6713. {
  6714. switch (tp->mac_version) {
  6715. case RTL_GIGA_MAC_VER_40:
  6716. case RTL_GIGA_MAC_VER_41:
  6717. case RTL_GIGA_MAC_VER_42:
  6718. case RTL_GIGA_MAC_VER_43:
  6719. case RTL_GIGA_MAC_VER_44:
  6720. case RTL_GIGA_MAC_VER_45:
  6721. case RTL_GIGA_MAC_VER_46:
  6722. case RTL_GIGA_MAC_VER_47:
  6723. case RTL_GIGA_MAC_VER_48:
  6724. rtl_hw_init_8168g(tp);
  6725. break;
  6726. case RTL_GIGA_MAC_VER_49:
  6727. case RTL_GIGA_MAC_VER_50:
  6728. case RTL_GIGA_MAC_VER_51:
  6729. rtl_hw_init_8168ep(tp);
  6730. break;
  6731. default:
  6732. break;
  6733. }
  6734. }
  6735. static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  6736. {
  6737. const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
  6738. const unsigned int region = cfg->region;
  6739. struct rtl8169_private *tp;
  6740. struct mii_if_info *mii;
  6741. struct net_device *dev;
  6742. void __iomem *ioaddr;
  6743. int chipset, i;
  6744. int rc;
  6745. if (netif_msg_drv(&debug)) {
  6746. printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
  6747. MODULENAME, RTL8169_VERSION);
  6748. }
  6749. dev = alloc_etherdev(sizeof (*tp));
  6750. if (!dev) {
  6751. rc = -ENOMEM;
  6752. goto out;
  6753. }
  6754. SET_NETDEV_DEV(dev, &pdev->dev);
  6755. dev->netdev_ops = &rtl_netdev_ops;
  6756. tp = netdev_priv(dev);
  6757. tp->dev = dev;
  6758. tp->pci_dev = pdev;
  6759. tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
  6760. mii = &tp->mii;
  6761. mii->dev = dev;
  6762. mii->mdio_read = rtl_mdio_read;
  6763. mii->mdio_write = rtl_mdio_write;
  6764. mii->phy_id_mask = 0x1f;
  6765. mii->reg_num_mask = 0x1f;
  6766. mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
  6767. /* disable ASPM completely as that cause random device stop working
  6768. * problems as well as full system hangs for some PCIe devices users */
  6769. pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
  6770. PCIE_LINK_STATE_CLKPM);
  6771. /* enable device (incl. PCI PM wakeup and hotplug setup) */
  6772. rc = pci_enable_device(pdev);
  6773. if (rc < 0) {
  6774. netif_err(tp, probe, dev, "enable failure\n");
  6775. goto err_out_free_dev_1;
  6776. }
  6777. if (pci_set_mwi(pdev) < 0)
  6778. netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
  6779. /* make sure PCI base addr 1 is MMIO */
  6780. if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
  6781. netif_err(tp, probe, dev,
  6782. "region #%d not an MMIO resource, aborting\n",
  6783. region);
  6784. rc = -ENODEV;
  6785. goto err_out_mwi_2;
  6786. }
  6787. /* check for weird/broken PCI region reporting */
  6788. if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
  6789. netif_err(tp, probe, dev,
  6790. "Invalid PCI region size(s), aborting\n");
  6791. rc = -ENODEV;
  6792. goto err_out_mwi_2;
  6793. }
  6794. rc = pci_request_regions(pdev, MODULENAME);
  6795. if (rc < 0) {
  6796. netif_err(tp, probe, dev, "could not request regions\n");
  6797. goto err_out_mwi_2;
  6798. }
  6799. tp->cp_cmd = 0;
  6800. if ((sizeof(dma_addr_t) > 4) &&
  6801. !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
  6802. tp->cp_cmd |= PCIDAC;
  6803. dev->features |= NETIF_F_HIGHDMA;
  6804. } else {
  6805. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  6806. if (rc < 0) {
  6807. netif_err(tp, probe, dev, "DMA configuration failed\n");
  6808. goto err_out_free_res_3;
  6809. }
  6810. }
  6811. /* ioremap MMIO region */
  6812. ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
  6813. if (!ioaddr) {
  6814. netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
  6815. rc = -EIO;
  6816. goto err_out_free_res_3;
  6817. }
  6818. tp->mmio_addr = ioaddr;
  6819. if (!pci_is_pcie(pdev))
  6820. netif_info(tp, probe, dev, "not PCI Express\n");
  6821. /* Identify chip attached to board */
  6822. rtl8169_get_mac_version(tp, dev, cfg->default_ver);
  6823. rtl_init_rxcfg(tp);
  6824. rtl_irq_disable(tp);
  6825. rtl_hw_initialize(tp);
  6826. rtl_hw_reset(tp);
  6827. rtl_ack_events(tp, 0xffff);
  6828. pci_set_master(pdev);
  6829. rtl_init_mdio_ops(tp);
  6830. rtl_init_pll_power_ops(tp);
  6831. rtl_init_jumbo_ops(tp);
  6832. rtl_init_csi_ops(tp);
  6833. rtl8169_print_mac_version(tp);
  6834. chipset = tp->mac_version;
  6835. tp->txd_version = rtl_chip_infos[chipset].txd_version;
  6836. RTL_W8(Cfg9346, Cfg9346_Unlock);
  6837. RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
  6838. RTL_W8(Config5, RTL_R8(Config5) & (BWF | MWF | UWF | LanWake | PMEStatus));
  6839. switch (tp->mac_version) {
  6840. case RTL_GIGA_MAC_VER_34:
  6841. case RTL_GIGA_MAC_VER_35:
  6842. case RTL_GIGA_MAC_VER_36:
  6843. case RTL_GIGA_MAC_VER_37:
  6844. case RTL_GIGA_MAC_VER_38:
  6845. case RTL_GIGA_MAC_VER_40:
  6846. case RTL_GIGA_MAC_VER_41:
  6847. case RTL_GIGA_MAC_VER_42:
  6848. case RTL_GIGA_MAC_VER_43:
  6849. case RTL_GIGA_MAC_VER_44:
  6850. case RTL_GIGA_MAC_VER_45:
  6851. case RTL_GIGA_MAC_VER_46:
  6852. case RTL_GIGA_MAC_VER_47:
  6853. case RTL_GIGA_MAC_VER_48:
  6854. case RTL_GIGA_MAC_VER_49:
  6855. case RTL_GIGA_MAC_VER_50:
  6856. case RTL_GIGA_MAC_VER_51:
  6857. if (rtl_eri_read(tp, 0xdc, ERIAR_EXGMAC) & MagicPacket_v2)
  6858. tp->features |= RTL_FEATURE_WOL;
  6859. if ((RTL_R8(Config3) & LinkUp) != 0)
  6860. tp->features |= RTL_FEATURE_WOL;
  6861. break;
  6862. default:
  6863. if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
  6864. tp->features |= RTL_FEATURE_WOL;
  6865. break;
  6866. }
  6867. if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
  6868. tp->features |= RTL_FEATURE_WOL;
  6869. tp->features |= rtl_try_msi(tp, cfg);
  6870. RTL_W8(Cfg9346, Cfg9346_Lock);
  6871. if (rtl_tbi_enabled(tp)) {
  6872. tp->set_speed = rtl8169_set_speed_tbi;
  6873. tp->get_settings = rtl8169_gset_tbi;
  6874. tp->phy_reset_enable = rtl8169_tbi_reset_enable;
  6875. tp->phy_reset_pending = rtl8169_tbi_reset_pending;
  6876. tp->link_ok = rtl8169_tbi_link_ok;
  6877. tp->do_ioctl = rtl_tbi_ioctl;
  6878. } else {
  6879. tp->set_speed = rtl8169_set_speed_xmii;
  6880. tp->get_settings = rtl8169_gset_xmii;
  6881. tp->phy_reset_enable = rtl8169_xmii_reset_enable;
  6882. tp->phy_reset_pending = rtl8169_xmii_reset_pending;
  6883. tp->link_ok = rtl8169_xmii_link_ok;
  6884. tp->do_ioctl = rtl_xmii_ioctl;
  6885. }
  6886. mutex_init(&tp->wk.mutex);
  6887. u64_stats_init(&tp->rx_stats.syncp);
  6888. u64_stats_init(&tp->tx_stats.syncp);
  6889. /* Get MAC address */
  6890. if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
  6891. tp->mac_version == RTL_GIGA_MAC_VER_36 ||
  6892. tp->mac_version == RTL_GIGA_MAC_VER_37 ||
  6893. tp->mac_version == RTL_GIGA_MAC_VER_38 ||
  6894. tp->mac_version == RTL_GIGA_MAC_VER_40 ||
  6895. tp->mac_version == RTL_GIGA_MAC_VER_41 ||
  6896. tp->mac_version == RTL_GIGA_MAC_VER_42 ||
  6897. tp->mac_version == RTL_GIGA_MAC_VER_43 ||
  6898. tp->mac_version == RTL_GIGA_MAC_VER_44 ||
  6899. tp->mac_version == RTL_GIGA_MAC_VER_45 ||
  6900. tp->mac_version == RTL_GIGA_MAC_VER_46 ||
  6901. tp->mac_version == RTL_GIGA_MAC_VER_47 ||
  6902. tp->mac_version == RTL_GIGA_MAC_VER_48 ||
  6903. tp->mac_version == RTL_GIGA_MAC_VER_49 ||
  6904. tp->mac_version == RTL_GIGA_MAC_VER_50 ||
  6905. tp->mac_version == RTL_GIGA_MAC_VER_51) {
  6906. u16 mac_addr[3];
  6907. *(u32 *)&mac_addr[0] = rtl_eri_read(tp, 0xe0, ERIAR_EXGMAC);
  6908. *(u16 *)&mac_addr[2] = rtl_eri_read(tp, 0xe4, ERIAR_EXGMAC);
  6909. if (is_valid_ether_addr((u8 *)mac_addr))
  6910. rtl_rar_set(tp, (u8 *)mac_addr);
  6911. }
  6912. for (i = 0; i < ETH_ALEN; i++)
  6913. dev->dev_addr[i] = RTL_R8(MAC0 + i);
  6914. dev->ethtool_ops = &rtl8169_ethtool_ops;
  6915. dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
  6916. netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
  6917. /* don't enable SG, IP_CSUM and TSO by default - it might not work
  6918. * properly for all devices */
  6919. dev->features |= NETIF_F_RXCSUM |
  6920. NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
  6921. dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
  6922. NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_TX |
  6923. NETIF_F_HW_VLAN_CTAG_RX;
  6924. dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
  6925. NETIF_F_HIGHDMA;
  6926. tp->cp_cmd |= RxChkSum | RxVlan;
  6927. /*
  6928. * Pretend we are using VLANs; This bypasses a nasty bug where
  6929. * Interrupts stop flowing on high load on 8110SCd controllers.
  6930. */
  6931. if (tp->mac_version == RTL_GIGA_MAC_VER_05)
  6932. /* Disallow toggling */
  6933. dev->hw_features &= ~NETIF_F_HW_VLAN_CTAG_RX;
  6934. if (tp->txd_version == RTL_TD_0)
  6935. tp->tso_csum = rtl8169_tso_csum_v1;
  6936. else if (tp->txd_version == RTL_TD_1) {
  6937. tp->tso_csum = rtl8169_tso_csum_v2;
  6938. dev->hw_features |= NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
  6939. } else
  6940. WARN_ON_ONCE(1);
  6941. dev->hw_features |= NETIF_F_RXALL;
  6942. dev->hw_features |= NETIF_F_RXFCS;
  6943. tp->hw_start = cfg->hw_start;
  6944. tp->event_slow = cfg->event_slow;
  6945. tp->opts1_mask = (tp->mac_version != RTL_GIGA_MAC_VER_01) ?
  6946. ~(RxBOVF | RxFOVF) : ~0;
  6947. init_timer(&tp->timer);
  6948. tp->timer.data = (unsigned long) dev;
  6949. tp->timer.function = rtl8169_phy_timer;
  6950. tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
  6951. tp->counters = dma_alloc_coherent (&pdev->dev, sizeof(*tp->counters),
  6952. &tp->counters_phys_addr, GFP_KERNEL);
  6953. if (!tp->counters) {
  6954. rc = -ENOMEM;
  6955. goto err_out_msi_4;
  6956. }
  6957. rc = register_netdev(dev);
  6958. if (rc < 0)
  6959. goto err_out_cnt_5;
  6960. pci_set_drvdata(pdev, dev);
  6961. netif_info(tp, probe, dev, "%s at 0x%p, %pM, XID %08x IRQ %d\n",
  6962. rtl_chip_infos[chipset].name, ioaddr, dev->dev_addr,
  6963. (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), pdev->irq);
  6964. if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
  6965. netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
  6966. "tx checksumming: %s]\n",
  6967. rtl_chip_infos[chipset].jumbo_max,
  6968. rtl_chip_infos[chipset].jumbo_tx_csum ? "ok" : "ko");
  6969. }
  6970. if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
  6971. tp->mac_version == RTL_GIGA_MAC_VER_28 ||
  6972. tp->mac_version == RTL_GIGA_MAC_VER_31 ||
  6973. tp->mac_version == RTL_GIGA_MAC_VER_49 ||
  6974. tp->mac_version == RTL_GIGA_MAC_VER_50 ||
  6975. tp->mac_version == RTL_GIGA_MAC_VER_51) &&
  6976. r8168_check_dash(tp)) {
  6977. rtl8168_driver_start(tp);
  6978. }
  6979. device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
  6980. if (pci_dev_run_wake(pdev))
  6981. pm_runtime_put_noidle(&pdev->dev);
  6982. netif_carrier_off(dev);
  6983. out:
  6984. return rc;
  6985. err_out_cnt_5:
  6986. dma_free_coherent(&pdev->dev, sizeof(*tp->counters), tp->counters,
  6987. tp->counters_phys_addr);
  6988. err_out_msi_4:
  6989. netif_napi_del(&tp->napi);
  6990. rtl_disable_msi(pdev, tp);
  6991. iounmap(ioaddr);
  6992. err_out_free_res_3:
  6993. pci_release_regions(pdev);
  6994. err_out_mwi_2:
  6995. pci_clear_mwi(pdev);
  6996. pci_disable_device(pdev);
  6997. err_out_free_dev_1:
  6998. free_netdev(dev);
  6999. goto out;
  7000. }
  7001. static struct pci_driver rtl8169_pci_driver = {
  7002. .name = MODULENAME,
  7003. .id_table = rtl8169_pci_tbl,
  7004. .probe = rtl_init_one,
  7005. .remove = rtl_remove_one,
  7006. .shutdown = rtl_shutdown,
  7007. .driver.pm = RTL8169_PM_OPS,
  7008. };
  7009. module_pci_driver(rtl8169_pci_driver);