r8169.c 174 KB

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