r8169.c 150 KB

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