r8169.c 150 KB

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