r8169.c 210 KB

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