r8169.c 120 KB

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