r8169.c 137 KB

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