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