r8169.c 174 KB

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