r8169.c 128 KB

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