r8169.c 168 KB

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