r8169.c 210 KB

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