r8169.c 168 KB

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