r8169.c 149 KB

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