r8152.c 99 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361
  1. /*
  2. * Copyright (c) 2014 Realtek Semiconductor Corp. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * version 2 as published by the Free Software Foundation.
  7. *
  8. */
  9. #include <linux/signal.h>
  10. #include <linux/slab.h>
  11. #include <linux/module.h>
  12. #include <linux/netdevice.h>
  13. #include <linux/etherdevice.h>
  14. #include <linux/mii.h>
  15. #include <linux/ethtool.h>
  16. #include <linux/usb.h>
  17. #include <linux/crc32.h>
  18. #include <linux/if_vlan.h>
  19. #include <linux/uaccess.h>
  20. #include <linux/list.h>
  21. #include <linux/ip.h>
  22. #include <linux/ipv6.h>
  23. #include <net/ip6_checksum.h>
  24. #include <uapi/linux/mdio.h>
  25. #include <linux/mdio.h>
  26. #include <linux/usb/cdc.h>
  27. #include <linux/suspend.h>
  28. /* Information for net-next */
  29. #define NETNEXT_VERSION "08"
  30. /* Information for net */
  31. #define NET_VERSION "5"
  32. #define DRIVER_VERSION "v1." NETNEXT_VERSION "." NET_VERSION
  33. #define DRIVER_AUTHOR "Realtek linux nic maintainers <nic_swsd@realtek.com>"
  34. #define DRIVER_DESC "Realtek RTL8152/RTL8153 Based USB Ethernet Adapters"
  35. #define MODULENAME "r8152"
  36. #define R8152_PHY_ID 32
  37. #define PLA_IDR 0xc000
  38. #define PLA_RCR 0xc010
  39. #define PLA_RMS 0xc016
  40. #define PLA_RXFIFO_CTRL0 0xc0a0
  41. #define PLA_RXFIFO_CTRL1 0xc0a4
  42. #define PLA_RXFIFO_CTRL2 0xc0a8
  43. #define PLA_DMY_REG0 0xc0b0
  44. #define PLA_FMC 0xc0b4
  45. #define PLA_CFG_WOL 0xc0b6
  46. #define PLA_TEREDO_CFG 0xc0bc
  47. #define PLA_MAR 0xcd00
  48. #define PLA_BACKUP 0xd000
  49. #define PAL_BDC_CR 0xd1a0
  50. #define PLA_TEREDO_TIMER 0xd2cc
  51. #define PLA_REALWOW_TIMER 0xd2e8
  52. #define PLA_LEDSEL 0xdd90
  53. #define PLA_LED_FEATURE 0xdd92
  54. #define PLA_PHYAR 0xde00
  55. #define PLA_BOOT_CTRL 0xe004
  56. #define PLA_GPHY_INTR_IMR 0xe022
  57. #define PLA_EEE_CR 0xe040
  58. #define PLA_EEEP_CR 0xe080
  59. #define PLA_MAC_PWR_CTRL 0xe0c0
  60. #define PLA_MAC_PWR_CTRL2 0xe0ca
  61. #define PLA_MAC_PWR_CTRL3 0xe0cc
  62. #define PLA_MAC_PWR_CTRL4 0xe0ce
  63. #define PLA_WDT6_CTRL 0xe428
  64. #define PLA_TCR0 0xe610
  65. #define PLA_TCR1 0xe612
  66. #define PLA_MTPS 0xe615
  67. #define PLA_TXFIFO_CTRL 0xe618
  68. #define PLA_RSTTALLY 0xe800
  69. #define PLA_CR 0xe813
  70. #define PLA_CRWECR 0xe81c
  71. #define PLA_CONFIG12 0xe81e /* CONFIG1, CONFIG2 */
  72. #define PLA_CONFIG34 0xe820 /* CONFIG3, CONFIG4 */
  73. #define PLA_CONFIG5 0xe822
  74. #define PLA_PHY_PWR 0xe84c
  75. #define PLA_OOB_CTRL 0xe84f
  76. #define PLA_CPCR 0xe854
  77. #define PLA_MISC_0 0xe858
  78. #define PLA_MISC_1 0xe85a
  79. #define PLA_OCP_GPHY_BASE 0xe86c
  80. #define PLA_TALLYCNT 0xe890
  81. #define PLA_SFF_STS_7 0xe8de
  82. #define PLA_PHYSTATUS 0xe908
  83. #define PLA_BP_BA 0xfc26
  84. #define PLA_BP_0 0xfc28
  85. #define PLA_BP_1 0xfc2a
  86. #define PLA_BP_2 0xfc2c
  87. #define PLA_BP_3 0xfc2e
  88. #define PLA_BP_4 0xfc30
  89. #define PLA_BP_5 0xfc32
  90. #define PLA_BP_6 0xfc34
  91. #define PLA_BP_7 0xfc36
  92. #define PLA_BP_EN 0xfc38
  93. #define USB_USB2PHY 0xb41e
  94. #define USB_SSPHYLINK2 0xb428
  95. #define USB_U2P3_CTRL 0xb460
  96. #define USB_CSR_DUMMY1 0xb464
  97. #define USB_CSR_DUMMY2 0xb466
  98. #define USB_DEV_STAT 0xb808
  99. #define USB_CONNECT_TIMER 0xcbf8
  100. #define USB_BURST_SIZE 0xcfc0
  101. #define USB_USB_CTRL 0xd406
  102. #define USB_PHY_CTRL 0xd408
  103. #define USB_TX_AGG 0xd40a
  104. #define USB_RX_BUF_TH 0xd40c
  105. #define USB_USB_TIMER 0xd428
  106. #define USB_RX_EARLY_TIMEOUT 0xd42c
  107. #define USB_RX_EARLY_SIZE 0xd42e
  108. #define USB_PM_CTRL_STATUS 0xd432
  109. #define USB_TX_DMA 0xd434
  110. #define USB_TOLERANCE 0xd490
  111. #define USB_LPM_CTRL 0xd41a
  112. #define USB_BMU_RESET 0xd4b0
  113. #define USB_UPS_CTRL 0xd800
  114. #define USB_MISC_0 0xd81a
  115. #define USB_POWER_CUT 0xd80a
  116. #define USB_AFE_CTRL2 0xd824
  117. #define USB_WDT11_CTRL 0xe43c
  118. #define USB_BP_BA 0xfc26
  119. #define USB_BP_0 0xfc28
  120. #define USB_BP_1 0xfc2a
  121. #define USB_BP_2 0xfc2c
  122. #define USB_BP_3 0xfc2e
  123. #define USB_BP_4 0xfc30
  124. #define USB_BP_5 0xfc32
  125. #define USB_BP_6 0xfc34
  126. #define USB_BP_7 0xfc36
  127. #define USB_BP_EN 0xfc38
  128. /* OCP Registers */
  129. #define OCP_ALDPS_CONFIG 0x2010
  130. #define OCP_EEE_CONFIG1 0x2080
  131. #define OCP_EEE_CONFIG2 0x2092
  132. #define OCP_EEE_CONFIG3 0x2094
  133. #define OCP_BASE_MII 0xa400
  134. #define OCP_EEE_AR 0xa41a
  135. #define OCP_EEE_DATA 0xa41c
  136. #define OCP_PHY_STATUS 0xa420
  137. #define OCP_POWER_CFG 0xa430
  138. #define OCP_EEE_CFG 0xa432
  139. #define OCP_SRAM_ADDR 0xa436
  140. #define OCP_SRAM_DATA 0xa438
  141. #define OCP_DOWN_SPEED 0xa442
  142. #define OCP_EEE_ABLE 0xa5c4
  143. #define OCP_EEE_ADV 0xa5d0
  144. #define OCP_EEE_LPABLE 0xa5d2
  145. #define OCP_PHY_STATE 0xa708 /* nway state for 8153 */
  146. #define OCP_ADC_CFG 0xbc06
  147. /* SRAM Register */
  148. #define SRAM_LPF_CFG 0x8012
  149. #define SRAM_10M_AMP1 0x8080
  150. #define SRAM_10M_AMP2 0x8082
  151. #define SRAM_IMPEDANCE 0x8084
  152. /* PLA_RCR */
  153. #define RCR_AAP 0x00000001
  154. #define RCR_APM 0x00000002
  155. #define RCR_AM 0x00000004
  156. #define RCR_AB 0x00000008
  157. #define RCR_ACPT_ALL (RCR_AAP | RCR_APM | RCR_AM | RCR_AB)
  158. /* PLA_RXFIFO_CTRL0 */
  159. #define RXFIFO_THR1_NORMAL 0x00080002
  160. #define RXFIFO_THR1_OOB 0x01800003
  161. /* PLA_RXFIFO_CTRL1 */
  162. #define RXFIFO_THR2_FULL 0x00000060
  163. #define RXFIFO_THR2_HIGH 0x00000038
  164. #define RXFIFO_THR2_OOB 0x0000004a
  165. #define RXFIFO_THR2_NORMAL 0x00a0
  166. /* PLA_RXFIFO_CTRL2 */
  167. #define RXFIFO_THR3_FULL 0x00000078
  168. #define RXFIFO_THR3_HIGH 0x00000048
  169. #define RXFIFO_THR3_OOB 0x0000005a
  170. #define RXFIFO_THR3_NORMAL 0x0110
  171. /* PLA_TXFIFO_CTRL */
  172. #define TXFIFO_THR_NORMAL 0x00400008
  173. #define TXFIFO_THR_NORMAL2 0x01000008
  174. /* PLA_DMY_REG0 */
  175. #define ECM_ALDPS 0x0002
  176. /* PLA_FMC */
  177. #define FMC_FCR_MCU_EN 0x0001
  178. /* PLA_EEEP_CR */
  179. #define EEEP_CR_EEEP_TX 0x0002
  180. /* PLA_WDT6_CTRL */
  181. #define WDT6_SET_MODE 0x0010
  182. /* PLA_TCR0 */
  183. #define TCR0_TX_EMPTY 0x0800
  184. #define TCR0_AUTO_FIFO 0x0080
  185. /* PLA_TCR1 */
  186. #define VERSION_MASK 0x7cf0
  187. /* PLA_MTPS */
  188. #define MTPS_JUMBO (12 * 1024 / 64)
  189. #define MTPS_DEFAULT (6 * 1024 / 64)
  190. /* PLA_RSTTALLY */
  191. #define TALLY_RESET 0x0001
  192. /* PLA_CR */
  193. #define CR_RST 0x10
  194. #define CR_RE 0x08
  195. #define CR_TE 0x04
  196. /* PLA_CRWECR */
  197. #define CRWECR_NORAML 0x00
  198. #define CRWECR_CONFIG 0xc0
  199. /* PLA_OOB_CTRL */
  200. #define NOW_IS_OOB 0x80
  201. #define TXFIFO_EMPTY 0x20
  202. #define RXFIFO_EMPTY 0x10
  203. #define LINK_LIST_READY 0x02
  204. #define DIS_MCU_CLROOB 0x01
  205. #define FIFO_EMPTY (TXFIFO_EMPTY | RXFIFO_EMPTY)
  206. /* PLA_MISC_1 */
  207. #define RXDY_GATED_EN 0x0008
  208. /* PLA_SFF_STS_7 */
  209. #define RE_INIT_LL 0x8000
  210. #define MCU_BORW_EN 0x4000
  211. /* PLA_CPCR */
  212. #define CPCR_RX_VLAN 0x0040
  213. /* PLA_CFG_WOL */
  214. #define MAGIC_EN 0x0001
  215. /* PLA_TEREDO_CFG */
  216. #define TEREDO_SEL 0x8000
  217. #define TEREDO_WAKE_MASK 0x7f00
  218. #define TEREDO_RS_EVENT_MASK 0x00fe
  219. #define OOB_TEREDO_EN 0x0001
  220. /* PAL_BDC_CR */
  221. #define ALDPS_PROXY_MODE 0x0001
  222. /* PLA_CONFIG34 */
  223. #define LINK_ON_WAKE_EN 0x0010
  224. #define LINK_OFF_WAKE_EN 0x0008
  225. /* PLA_CONFIG5 */
  226. #define BWF_EN 0x0040
  227. #define MWF_EN 0x0020
  228. #define UWF_EN 0x0010
  229. #define LAN_WAKE_EN 0x0002
  230. /* PLA_LED_FEATURE */
  231. #define LED_MODE_MASK 0x0700
  232. /* PLA_PHY_PWR */
  233. #define TX_10M_IDLE_EN 0x0080
  234. #define PFM_PWM_SWITCH 0x0040
  235. /* PLA_MAC_PWR_CTRL */
  236. #define D3_CLK_GATED_EN 0x00004000
  237. #define MCU_CLK_RATIO 0x07010f07
  238. #define MCU_CLK_RATIO_MASK 0x0f0f0f0f
  239. #define ALDPS_SPDWN_RATIO 0x0f87
  240. /* PLA_MAC_PWR_CTRL2 */
  241. #define EEE_SPDWN_RATIO 0x8007
  242. /* PLA_MAC_PWR_CTRL3 */
  243. #define PKT_AVAIL_SPDWN_EN 0x0100
  244. #define SUSPEND_SPDWN_EN 0x0004
  245. #define U1U2_SPDWN_EN 0x0002
  246. #define L1_SPDWN_EN 0x0001
  247. /* PLA_MAC_PWR_CTRL4 */
  248. #define PWRSAVE_SPDWN_EN 0x1000
  249. #define RXDV_SPDWN_EN 0x0800
  250. #define TX10MIDLE_EN 0x0100
  251. #define TP100_SPDWN_EN 0x0020
  252. #define TP500_SPDWN_EN 0x0010
  253. #define TP1000_SPDWN_EN 0x0008
  254. #define EEE_SPDWN_EN 0x0001
  255. /* PLA_GPHY_INTR_IMR */
  256. #define GPHY_STS_MSK 0x0001
  257. #define SPEED_DOWN_MSK 0x0002
  258. #define SPDWN_RXDV_MSK 0x0004
  259. #define SPDWN_LINKCHG_MSK 0x0008
  260. /* PLA_PHYAR */
  261. #define PHYAR_FLAG 0x80000000
  262. /* PLA_EEE_CR */
  263. #define EEE_RX_EN 0x0001
  264. #define EEE_TX_EN 0x0002
  265. /* PLA_BOOT_CTRL */
  266. #define AUTOLOAD_DONE 0x0002
  267. /* USB_USB2PHY */
  268. #define USB2PHY_SUSPEND 0x0001
  269. #define USB2PHY_L1 0x0002
  270. /* USB_SSPHYLINK2 */
  271. #define pwd_dn_scale_mask 0x3ffe
  272. #define pwd_dn_scale(x) ((x) << 1)
  273. /* USB_CSR_DUMMY1 */
  274. #define DYNAMIC_BURST 0x0001
  275. /* USB_CSR_DUMMY2 */
  276. #define EP4_FULL_FC 0x0001
  277. /* USB_DEV_STAT */
  278. #define STAT_SPEED_MASK 0x0006
  279. #define STAT_SPEED_HIGH 0x0000
  280. #define STAT_SPEED_FULL 0x0002
  281. /* USB_TX_AGG */
  282. #define TX_AGG_MAX_THRESHOLD 0x03
  283. /* USB_RX_BUF_TH */
  284. #define RX_THR_SUPPER 0x0c350180
  285. #define RX_THR_HIGH 0x7a120180
  286. #define RX_THR_SLOW 0xffff0180
  287. /* USB_TX_DMA */
  288. #define TEST_MODE_DISABLE 0x00000001
  289. #define TX_SIZE_ADJUST1 0x00000100
  290. /* USB_BMU_RESET */
  291. #define BMU_RESET_EP_IN 0x01
  292. #define BMU_RESET_EP_OUT 0x02
  293. /* USB_UPS_CTRL */
  294. #define POWER_CUT 0x0100
  295. /* USB_PM_CTRL_STATUS */
  296. #define RESUME_INDICATE 0x0001
  297. /* USB_USB_CTRL */
  298. #define RX_AGG_DISABLE 0x0010
  299. #define RX_ZERO_EN 0x0080
  300. /* USB_U2P3_CTRL */
  301. #define U2P3_ENABLE 0x0001
  302. /* USB_POWER_CUT */
  303. #define PWR_EN 0x0001
  304. #define PHASE2_EN 0x0008
  305. /* USB_MISC_0 */
  306. #define PCUT_STATUS 0x0001
  307. /* USB_RX_EARLY_TIMEOUT */
  308. #define COALESCE_SUPER 85000U
  309. #define COALESCE_HIGH 250000U
  310. #define COALESCE_SLOW 524280U
  311. /* USB_WDT11_CTRL */
  312. #define TIMER11_EN 0x0001
  313. /* USB_LPM_CTRL */
  314. /* bit 4 ~ 5: fifo empty boundary */
  315. #define FIFO_EMPTY_1FB 0x30 /* 0x1fb * 64 = 32448 bytes */
  316. /* bit 2 ~ 3: LMP timer */
  317. #define LPM_TIMER_MASK 0x0c
  318. #define LPM_TIMER_500MS 0x04 /* 500 ms */
  319. #define LPM_TIMER_500US 0x0c /* 500 us */
  320. #define ROK_EXIT_LPM 0x02
  321. /* USB_AFE_CTRL2 */
  322. #define SEN_VAL_MASK 0xf800
  323. #define SEN_VAL_NORMAL 0xa000
  324. #define SEL_RXIDLE 0x0100
  325. /* OCP_ALDPS_CONFIG */
  326. #define ENPWRSAVE 0x8000
  327. #define ENPDNPS 0x0200
  328. #define LINKENA 0x0100
  329. #define DIS_SDSAVE 0x0010
  330. /* OCP_PHY_STATUS */
  331. #define PHY_STAT_MASK 0x0007
  332. #define PHY_STAT_LAN_ON 3
  333. #define PHY_STAT_PWRDN 5
  334. /* OCP_POWER_CFG */
  335. #define EEE_CLKDIV_EN 0x8000
  336. #define EN_ALDPS 0x0004
  337. #define EN_10M_PLLOFF 0x0001
  338. /* OCP_EEE_CONFIG1 */
  339. #define RG_TXLPI_MSK_HFDUP 0x8000
  340. #define RG_MATCLR_EN 0x4000
  341. #define EEE_10_CAP 0x2000
  342. #define EEE_NWAY_EN 0x1000
  343. #define TX_QUIET_EN 0x0200
  344. #define RX_QUIET_EN 0x0100
  345. #define sd_rise_time_mask 0x0070
  346. #define sd_rise_time(x) (min(x, 7) << 4) /* bit 4 ~ 6 */
  347. #define RG_RXLPI_MSK_HFDUP 0x0008
  348. #define SDFALLTIME 0x0007 /* bit 0 ~ 2 */
  349. /* OCP_EEE_CONFIG2 */
  350. #define RG_LPIHYS_NUM 0x7000 /* bit 12 ~ 15 */
  351. #define RG_DACQUIET_EN 0x0400
  352. #define RG_LDVQUIET_EN 0x0200
  353. #define RG_CKRSEL 0x0020
  354. #define RG_EEEPRG_EN 0x0010
  355. /* OCP_EEE_CONFIG3 */
  356. #define fast_snr_mask 0xff80
  357. #define fast_snr(x) (min(x, 0x1ff) << 7) /* bit 7 ~ 15 */
  358. #define RG_LFS_SEL 0x0060 /* bit 6 ~ 5 */
  359. #define MSK_PH 0x0006 /* bit 0 ~ 3 */
  360. /* OCP_EEE_AR */
  361. /* bit[15:14] function */
  362. #define FUN_ADDR 0x0000
  363. #define FUN_DATA 0x4000
  364. /* bit[4:0] device addr */
  365. /* OCP_EEE_CFG */
  366. #define CTAP_SHORT_EN 0x0040
  367. #define EEE10_EN 0x0010
  368. /* OCP_DOWN_SPEED */
  369. #define EN_10M_BGOFF 0x0080
  370. /* OCP_PHY_STATE */
  371. #define TXDIS_STATE 0x01
  372. #define ABD_STATE 0x02
  373. /* OCP_ADC_CFG */
  374. #define CKADSEL_L 0x0100
  375. #define ADC_EN 0x0080
  376. #define EN_EMI_L 0x0040
  377. /* SRAM_LPF_CFG */
  378. #define LPF_AUTO_TUNE 0x8000
  379. /* SRAM_10M_AMP1 */
  380. #define GDAC_IB_UPALL 0x0008
  381. /* SRAM_10M_AMP2 */
  382. #define AMP_DN 0x0200
  383. /* SRAM_IMPEDANCE */
  384. #define RX_DRIVING_MASK 0x6000
  385. enum rtl_register_content {
  386. _1000bps = 0x10,
  387. _100bps = 0x08,
  388. _10bps = 0x04,
  389. LINK_STATUS = 0x02,
  390. FULL_DUP = 0x01,
  391. };
  392. #define RTL8152_MAX_TX 4
  393. #define RTL8152_MAX_RX 10
  394. #define INTBUFSIZE 2
  395. #define CRC_SIZE 4
  396. #define TX_ALIGN 4
  397. #define RX_ALIGN 8
  398. #define INTR_LINK 0x0004
  399. #define RTL8152_REQT_READ 0xc0
  400. #define RTL8152_REQT_WRITE 0x40
  401. #define RTL8152_REQ_GET_REGS 0x05
  402. #define RTL8152_REQ_SET_REGS 0x05
  403. #define BYTE_EN_DWORD 0xff
  404. #define BYTE_EN_WORD 0x33
  405. #define BYTE_EN_BYTE 0x11
  406. #define BYTE_EN_SIX_BYTES 0x3f
  407. #define BYTE_EN_START_MASK 0x0f
  408. #define BYTE_EN_END_MASK 0xf0
  409. #define RTL8153_MAX_PACKET 9216 /* 9K */
  410. #define RTL8153_MAX_MTU (RTL8153_MAX_PACKET - VLAN_ETH_HLEN - VLAN_HLEN)
  411. #define RTL8152_RMS (VLAN_ETH_FRAME_LEN + VLAN_HLEN)
  412. #define RTL8153_RMS RTL8153_MAX_PACKET
  413. #define RTL8152_TX_TIMEOUT (5 * HZ)
  414. #define RTL8152_NAPI_WEIGHT 64
  415. /* rtl8152 flags */
  416. enum rtl8152_flags {
  417. RTL8152_UNPLUG = 0,
  418. RTL8152_SET_RX_MODE,
  419. WORK_ENABLE,
  420. RTL8152_LINK_CHG,
  421. SELECTIVE_SUSPEND,
  422. PHY_RESET,
  423. SCHEDULE_NAPI,
  424. };
  425. /* Define these values to match your device */
  426. #define VENDOR_ID_REALTEK 0x0bda
  427. #define VENDOR_ID_SAMSUNG 0x04e8
  428. #define VENDOR_ID_LENOVO 0x17ef
  429. #define VENDOR_ID_NVIDIA 0x0955
  430. #define MCU_TYPE_PLA 0x0100
  431. #define MCU_TYPE_USB 0x0000
  432. struct tally_counter {
  433. __le64 tx_packets;
  434. __le64 rx_packets;
  435. __le64 tx_errors;
  436. __le32 rx_errors;
  437. __le16 rx_missed;
  438. __le16 align_errors;
  439. __le32 tx_one_collision;
  440. __le32 tx_multi_collision;
  441. __le64 rx_unicast;
  442. __le64 rx_broadcast;
  443. __le32 rx_multicast;
  444. __le16 tx_aborted;
  445. __le16 tx_underrun;
  446. };
  447. struct rx_desc {
  448. __le32 opts1;
  449. #define RX_LEN_MASK 0x7fff
  450. __le32 opts2;
  451. #define RD_UDP_CS BIT(23)
  452. #define RD_TCP_CS BIT(22)
  453. #define RD_IPV6_CS BIT(20)
  454. #define RD_IPV4_CS BIT(19)
  455. __le32 opts3;
  456. #define IPF BIT(23) /* IP checksum fail */
  457. #define UDPF BIT(22) /* UDP checksum fail */
  458. #define TCPF BIT(21) /* TCP checksum fail */
  459. #define RX_VLAN_TAG BIT(16)
  460. __le32 opts4;
  461. __le32 opts5;
  462. __le32 opts6;
  463. };
  464. struct tx_desc {
  465. __le32 opts1;
  466. #define TX_FS BIT(31) /* First segment of a packet */
  467. #define TX_LS BIT(30) /* Final segment of a packet */
  468. #define GTSENDV4 BIT(28)
  469. #define GTSENDV6 BIT(27)
  470. #define GTTCPHO_SHIFT 18
  471. #define GTTCPHO_MAX 0x7fU
  472. #define TX_LEN_MAX 0x3ffffU
  473. __le32 opts2;
  474. #define UDP_CS BIT(31) /* Calculate UDP/IP checksum */
  475. #define TCP_CS BIT(30) /* Calculate TCP/IP checksum */
  476. #define IPV4_CS BIT(29) /* Calculate IPv4 checksum */
  477. #define IPV6_CS BIT(28) /* Calculate IPv6 checksum */
  478. #define MSS_SHIFT 17
  479. #define MSS_MAX 0x7ffU
  480. #define TCPHO_SHIFT 17
  481. #define TCPHO_MAX 0x7ffU
  482. #define TX_VLAN_TAG BIT(16)
  483. };
  484. struct r8152;
  485. struct rx_agg {
  486. struct list_head list;
  487. struct urb *urb;
  488. struct r8152 *context;
  489. void *buffer;
  490. void *head;
  491. };
  492. struct tx_agg {
  493. struct list_head list;
  494. struct urb *urb;
  495. struct r8152 *context;
  496. void *buffer;
  497. void *head;
  498. u32 skb_num;
  499. u32 skb_len;
  500. };
  501. struct r8152 {
  502. unsigned long flags;
  503. struct usb_device *udev;
  504. struct napi_struct napi;
  505. struct usb_interface *intf;
  506. struct net_device *netdev;
  507. struct urb *intr_urb;
  508. struct tx_agg tx_info[RTL8152_MAX_TX];
  509. struct rx_agg rx_info[RTL8152_MAX_RX];
  510. struct list_head rx_done, tx_free;
  511. struct sk_buff_head tx_queue, rx_queue;
  512. spinlock_t rx_lock, tx_lock;
  513. struct delayed_work schedule;
  514. struct mii_if_info mii;
  515. struct mutex control; /* use for hw setting */
  516. #ifdef CONFIG_PM_SLEEP
  517. struct notifier_block pm_notifier;
  518. #endif
  519. struct rtl_ops {
  520. void (*init)(struct r8152 *);
  521. int (*enable)(struct r8152 *);
  522. void (*disable)(struct r8152 *);
  523. void (*up)(struct r8152 *);
  524. void (*down)(struct r8152 *);
  525. void (*unload)(struct r8152 *);
  526. int (*eee_get)(struct r8152 *, struct ethtool_eee *);
  527. int (*eee_set)(struct r8152 *, struct ethtool_eee *);
  528. bool (*in_nway)(struct r8152 *);
  529. void (*autosuspend_en)(struct r8152 *tp, bool enable);
  530. } rtl_ops;
  531. int intr_interval;
  532. u32 saved_wolopts;
  533. u32 msg_enable;
  534. u32 tx_qlen;
  535. u32 coalesce;
  536. u16 ocp_base;
  537. u8 *intr_buff;
  538. u8 version;
  539. };
  540. enum rtl_version {
  541. RTL_VER_UNKNOWN = 0,
  542. RTL_VER_01,
  543. RTL_VER_02,
  544. RTL_VER_03,
  545. RTL_VER_04,
  546. RTL_VER_05,
  547. RTL_VER_06,
  548. RTL_VER_MAX
  549. };
  550. enum tx_csum_stat {
  551. TX_CSUM_SUCCESS = 0,
  552. TX_CSUM_TSO,
  553. TX_CSUM_NONE
  554. };
  555. /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
  556. * The RTL chips use a 64 element hash table based on the Ethernet CRC.
  557. */
  558. static const int multicast_filter_limit = 32;
  559. static unsigned int agg_buf_sz = 16384;
  560. #define RTL_LIMITED_TSO_SIZE (agg_buf_sz - sizeof(struct tx_desc) - \
  561. VLAN_ETH_HLEN - VLAN_HLEN)
  562. static
  563. int get_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
  564. {
  565. int ret;
  566. void *tmp;
  567. tmp = kmalloc(size, GFP_KERNEL);
  568. if (!tmp)
  569. return -ENOMEM;
  570. ret = usb_control_msg(tp->udev, usb_rcvctrlpipe(tp->udev, 0),
  571. RTL8152_REQ_GET_REGS, RTL8152_REQT_READ,
  572. value, index, tmp, size, 500);
  573. memcpy(data, tmp, size);
  574. kfree(tmp);
  575. return ret;
  576. }
  577. static
  578. int set_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
  579. {
  580. int ret;
  581. void *tmp;
  582. tmp = kmemdup(data, size, GFP_KERNEL);
  583. if (!tmp)
  584. return -ENOMEM;
  585. ret = usb_control_msg(tp->udev, usb_sndctrlpipe(tp->udev, 0),
  586. RTL8152_REQ_SET_REGS, RTL8152_REQT_WRITE,
  587. value, index, tmp, size, 500);
  588. kfree(tmp);
  589. return ret;
  590. }
  591. static int generic_ocp_read(struct r8152 *tp, u16 index, u16 size,
  592. void *data, u16 type)
  593. {
  594. u16 limit = 64;
  595. int ret = 0;
  596. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  597. return -ENODEV;
  598. /* both size and indix must be 4 bytes align */
  599. if ((size & 3) || !size || (index & 3) || !data)
  600. return -EPERM;
  601. if ((u32)index + (u32)size > 0xffff)
  602. return -EPERM;
  603. while (size) {
  604. if (size > limit) {
  605. ret = get_registers(tp, index, type, limit, data);
  606. if (ret < 0)
  607. break;
  608. index += limit;
  609. data += limit;
  610. size -= limit;
  611. } else {
  612. ret = get_registers(tp, index, type, size, data);
  613. if (ret < 0)
  614. break;
  615. index += size;
  616. data += size;
  617. size = 0;
  618. break;
  619. }
  620. }
  621. if (ret == -ENODEV)
  622. set_bit(RTL8152_UNPLUG, &tp->flags);
  623. return ret;
  624. }
  625. static int generic_ocp_write(struct r8152 *tp, u16 index, u16 byteen,
  626. u16 size, void *data, u16 type)
  627. {
  628. int ret;
  629. u16 byteen_start, byteen_end, byen;
  630. u16 limit = 512;
  631. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  632. return -ENODEV;
  633. /* both size and indix must be 4 bytes align */
  634. if ((size & 3) || !size || (index & 3) || !data)
  635. return -EPERM;
  636. if ((u32)index + (u32)size > 0xffff)
  637. return -EPERM;
  638. byteen_start = byteen & BYTE_EN_START_MASK;
  639. byteen_end = byteen & BYTE_EN_END_MASK;
  640. byen = byteen_start | (byteen_start << 4);
  641. ret = set_registers(tp, index, type | byen, 4, data);
  642. if (ret < 0)
  643. goto error1;
  644. index += 4;
  645. data += 4;
  646. size -= 4;
  647. if (size) {
  648. size -= 4;
  649. while (size) {
  650. if (size > limit) {
  651. ret = set_registers(tp, index,
  652. type | BYTE_EN_DWORD,
  653. limit, data);
  654. if (ret < 0)
  655. goto error1;
  656. index += limit;
  657. data += limit;
  658. size -= limit;
  659. } else {
  660. ret = set_registers(tp, index,
  661. type | BYTE_EN_DWORD,
  662. size, data);
  663. if (ret < 0)
  664. goto error1;
  665. index += size;
  666. data += size;
  667. size = 0;
  668. break;
  669. }
  670. }
  671. byen = byteen_end | (byteen_end >> 4);
  672. ret = set_registers(tp, index, type | byen, 4, data);
  673. if (ret < 0)
  674. goto error1;
  675. }
  676. error1:
  677. if (ret == -ENODEV)
  678. set_bit(RTL8152_UNPLUG, &tp->flags);
  679. return ret;
  680. }
  681. static inline
  682. int pla_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
  683. {
  684. return generic_ocp_read(tp, index, size, data, MCU_TYPE_PLA);
  685. }
  686. static inline
  687. int pla_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
  688. {
  689. return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_PLA);
  690. }
  691. static inline
  692. int usb_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
  693. {
  694. return generic_ocp_read(tp, index, size, data, MCU_TYPE_USB);
  695. }
  696. static inline
  697. int usb_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
  698. {
  699. return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_USB);
  700. }
  701. static u32 ocp_read_dword(struct r8152 *tp, u16 type, u16 index)
  702. {
  703. __le32 data;
  704. generic_ocp_read(tp, index, sizeof(data), &data, type);
  705. return __le32_to_cpu(data);
  706. }
  707. static void ocp_write_dword(struct r8152 *tp, u16 type, u16 index, u32 data)
  708. {
  709. __le32 tmp = __cpu_to_le32(data);
  710. generic_ocp_write(tp, index, BYTE_EN_DWORD, sizeof(tmp), &tmp, type);
  711. }
  712. static u16 ocp_read_word(struct r8152 *tp, u16 type, u16 index)
  713. {
  714. u32 data;
  715. __le32 tmp;
  716. u8 shift = index & 2;
  717. index &= ~3;
  718. generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
  719. data = __le32_to_cpu(tmp);
  720. data >>= (shift * 8);
  721. data &= 0xffff;
  722. return (u16)data;
  723. }
  724. static void ocp_write_word(struct r8152 *tp, u16 type, u16 index, u32 data)
  725. {
  726. u32 mask = 0xffff;
  727. __le32 tmp;
  728. u16 byen = BYTE_EN_WORD;
  729. u8 shift = index & 2;
  730. data &= mask;
  731. if (index & 2) {
  732. byen <<= shift;
  733. mask <<= (shift * 8);
  734. data <<= (shift * 8);
  735. index &= ~3;
  736. }
  737. tmp = __cpu_to_le32(data);
  738. generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
  739. }
  740. static u8 ocp_read_byte(struct r8152 *tp, u16 type, u16 index)
  741. {
  742. u32 data;
  743. __le32 tmp;
  744. u8 shift = index & 3;
  745. index &= ~3;
  746. generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
  747. data = __le32_to_cpu(tmp);
  748. data >>= (shift * 8);
  749. data &= 0xff;
  750. return (u8)data;
  751. }
  752. static void ocp_write_byte(struct r8152 *tp, u16 type, u16 index, u32 data)
  753. {
  754. u32 mask = 0xff;
  755. __le32 tmp;
  756. u16 byen = BYTE_EN_BYTE;
  757. u8 shift = index & 3;
  758. data &= mask;
  759. if (index & 3) {
  760. byen <<= shift;
  761. mask <<= (shift * 8);
  762. data <<= (shift * 8);
  763. index &= ~3;
  764. }
  765. tmp = __cpu_to_le32(data);
  766. generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
  767. }
  768. static u16 ocp_reg_read(struct r8152 *tp, u16 addr)
  769. {
  770. u16 ocp_base, ocp_index;
  771. ocp_base = addr & 0xf000;
  772. if (ocp_base != tp->ocp_base) {
  773. ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
  774. tp->ocp_base = ocp_base;
  775. }
  776. ocp_index = (addr & 0x0fff) | 0xb000;
  777. return ocp_read_word(tp, MCU_TYPE_PLA, ocp_index);
  778. }
  779. static void ocp_reg_write(struct r8152 *tp, u16 addr, u16 data)
  780. {
  781. u16 ocp_base, ocp_index;
  782. ocp_base = addr & 0xf000;
  783. if (ocp_base != tp->ocp_base) {
  784. ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
  785. tp->ocp_base = ocp_base;
  786. }
  787. ocp_index = (addr & 0x0fff) | 0xb000;
  788. ocp_write_word(tp, MCU_TYPE_PLA, ocp_index, data);
  789. }
  790. static inline void r8152_mdio_write(struct r8152 *tp, u32 reg_addr, u32 value)
  791. {
  792. ocp_reg_write(tp, OCP_BASE_MII + reg_addr * 2, value);
  793. }
  794. static inline int r8152_mdio_read(struct r8152 *tp, u32 reg_addr)
  795. {
  796. return ocp_reg_read(tp, OCP_BASE_MII + reg_addr * 2);
  797. }
  798. static void sram_write(struct r8152 *tp, u16 addr, u16 data)
  799. {
  800. ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
  801. ocp_reg_write(tp, OCP_SRAM_DATA, data);
  802. }
  803. static int read_mii_word(struct net_device *netdev, int phy_id, int reg)
  804. {
  805. struct r8152 *tp = netdev_priv(netdev);
  806. int ret;
  807. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  808. return -ENODEV;
  809. if (phy_id != R8152_PHY_ID)
  810. return -EINVAL;
  811. ret = r8152_mdio_read(tp, reg);
  812. return ret;
  813. }
  814. static
  815. void write_mii_word(struct net_device *netdev, int phy_id, int reg, int val)
  816. {
  817. struct r8152 *tp = netdev_priv(netdev);
  818. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  819. return;
  820. if (phy_id != R8152_PHY_ID)
  821. return;
  822. r8152_mdio_write(tp, reg, val);
  823. }
  824. static int
  825. r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags);
  826. static int rtl8152_set_mac_address(struct net_device *netdev, void *p)
  827. {
  828. struct r8152 *tp = netdev_priv(netdev);
  829. struct sockaddr *addr = p;
  830. int ret = -EADDRNOTAVAIL;
  831. if (!is_valid_ether_addr(addr->sa_data))
  832. goto out1;
  833. ret = usb_autopm_get_interface(tp->intf);
  834. if (ret < 0)
  835. goto out1;
  836. mutex_lock(&tp->control);
  837. memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
  838. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
  839. pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES, 8, addr->sa_data);
  840. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
  841. mutex_unlock(&tp->control);
  842. usb_autopm_put_interface(tp->intf);
  843. out1:
  844. return ret;
  845. }
  846. static int set_ethernet_addr(struct r8152 *tp)
  847. {
  848. struct net_device *dev = tp->netdev;
  849. struct sockaddr sa;
  850. int ret;
  851. if (tp->version == RTL_VER_01)
  852. ret = pla_ocp_read(tp, PLA_IDR, 8, sa.sa_data);
  853. else
  854. ret = pla_ocp_read(tp, PLA_BACKUP, 8, sa.sa_data);
  855. if (ret < 0) {
  856. netif_err(tp, probe, dev, "Get ether addr fail\n");
  857. } else if (!is_valid_ether_addr(sa.sa_data)) {
  858. netif_err(tp, probe, dev, "Invalid ether addr %pM\n",
  859. sa.sa_data);
  860. eth_hw_addr_random(dev);
  861. ether_addr_copy(sa.sa_data, dev->dev_addr);
  862. ret = rtl8152_set_mac_address(dev, &sa);
  863. netif_info(tp, probe, dev, "Random ether addr %pM\n",
  864. sa.sa_data);
  865. } else {
  866. if (tp->version == RTL_VER_01)
  867. ether_addr_copy(dev->dev_addr, sa.sa_data);
  868. else
  869. ret = rtl8152_set_mac_address(dev, &sa);
  870. }
  871. return ret;
  872. }
  873. static void read_bulk_callback(struct urb *urb)
  874. {
  875. struct net_device *netdev;
  876. int status = urb->status;
  877. struct rx_agg *agg;
  878. struct r8152 *tp;
  879. agg = urb->context;
  880. if (!agg)
  881. return;
  882. tp = agg->context;
  883. if (!tp)
  884. return;
  885. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  886. return;
  887. if (!test_bit(WORK_ENABLE, &tp->flags))
  888. return;
  889. netdev = tp->netdev;
  890. /* When link down, the driver would cancel all bulks. */
  891. /* This avoid the re-submitting bulk */
  892. if (!netif_carrier_ok(netdev))
  893. return;
  894. usb_mark_last_busy(tp->udev);
  895. switch (status) {
  896. case 0:
  897. if (urb->actual_length < ETH_ZLEN)
  898. break;
  899. spin_lock(&tp->rx_lock);
  900. list_add_tail(&agg->list, &tp->rx_done);
  901. spin_unlock(&tp->rx_lock);
  902. napi_schedule(&tp->napi);
  903. return;
  904. case -ESHUTDOWN:
  905. set_bit(RTL8152_UNPLUG, &tp->flags);
  906. netif_device_detach(tp->netdev);
  907. return;
  908. case -ENOENT:
  909. return; /* the urb is in unlink state */
  910. case -ETIME:
  911. if (net_ratelimit())
  912. netdev_warn(netdev, "maybe reset is needed?\n");
  913. break;
  914. default:
  915. if (net_ratelimit())
  916. netdev_warn(netdev, "Rx status %d\n", status);
  917. break;
  918. }
  919. r8152_submit_rx(tp, agg, GFP_ATOMIC);
  920. }
  921. static void write_bulk_callback(struct urb *urb)
  922. {
  923. struct net_device_stats *stats;
  924. struct net_device *netdev;
  925. struct tx_agg *agg;
  926. struct r8152 *tp;
  927. int status = urb->status;
  928. agg = urb->context;
  929. if (!agg)
  930. return;
  931. tp = agg->context;
  932. if (!tp)
  933. return;
  934. netdev = tp->netdev;
  935. stats = &netdev->stats;
  936. if (status) {
  937. if (net_ratelimit())
  938. netdev_warn(netdev, "Tx status %d\n", status);
  939. stats->tx_errors += agg->skb_num;
  940. } else {
  941. stats->tx_packets += agg->skb_num;
  942. stats->tx_bytes += agg->skb_len;
  943. }
  944. spin_lock(&tp->tx_lock);
  945. list_add_tail(&agg->list, &tp->tx_free);
  946. spin_unlock(&tp->tx_lock);
  947. usb_autopm_put_interface_async(tp->intf);
  948. if (!netif_carrier_ok(netdev))
  949. return;
  950. if (!test_bit(WORK_ENABLE, &tp->flags))
  951. return;
  952. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  953. return;
  954. if (!skb_queue_empty(&tp->tx_queue))
  955. napi_schedule(&tp->napi);
  956. }
  957. static void intr_callback(struct urb *urb)
  958. {
  959. struct r8152 *tp;
  960. __le16 *d;
  961. int status = urb->status;
  962. int res;
  963. tp = urb->context;
  964. if (!tp)
  965. return;
  966. if (!test_bit(WORK_ENABLE, &tp->flags))
  967. return;
  968. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  969. return;
  970. switch (status) {
  971. case 0: /* success */
  972. break;
  973. case -ECONNRESET: /* unlink */
  974. case -ESHUTDOWN:
  975. netif_device_detach(tp->netdev);
  976. case -ENOENT:
  977. case -EPROTO:
  978. netif_info(tp, intr, tp->netdev,
  979. "Stop submitting intr, status %d\n", status);
  980. return;
  981. case -EOVERFLOW:
  982. netif_info(tp, intr, tp->netdev, "intr status -EOVERFLOW\n");
  983. goto resubmit;
  984. /* -EPIPE: should clear the halt */
  985. default:
  986. netif_info(tp, intr, tp->netdev, "intr status %d\n", status);
  987. goto resubmit;
  988. }
  989. d = urb->transfer_buffer;
  990. if (INTR_LINK & __le16_to_cpu(d[0])) {
  991. if (!netif_carrier_ok(tp->netdev)) {
  992. set_bit(RTL8152_LINK_CHG, &tp->flags);
  993. schedule_delayed_work(&tp->schedule, 0);
  994. }
  995. } else {
  996. if (netif_carrier_ok(tp->netdev)) {
  997. set_bit(RTL8152_LINK_CHG, &tp->flags);
  998. schedule_delayed_work(&tp->schedule, 0);
  999. }
  1000. }
  1001. resubmit:
  1002. res = usb_submit_urb(urb, GFP_ATOMIC);
  1003. if (res == -ENODEV) {
  1004. set_bit(RTL8152_UNPLUG, &tp->flags);
  1005. netif_device_detach(tp->netdev);
  1006. } else if (res) {
  1007. netif_err(tp, intr, tp->netdev,
  1008. "can't resubmit intr, status %d\n", res);
  1009. }
  1010. }
  1011. static inline void *rx_agg_align(void *data)
  1012. {
  1013. return (void *)ALIGN((uintptr_t)data, RX_ALIGN);
  1014. }
  1015. static inline void *tx_agg_align(void *data)
  1016. {
  1017. return (void *)ALIGN((uintptr_t)data, TX_ALIGN);
  1018. }
  1019. static void free_all_mem(struct r8152 *tp)
  1020. {
  1021. int i;
  1022. for (i = 0; i < RTL8152_MAX_RX; i++) {
  1023. usb_free_urb(tp->rx_info[i].urb);
  1024. tp->rx_info[i].urb = NULL;
  1025. kfree(tp->rx_info[i].buffer);
  1026. tp->rx_info[i].buffer = NULL;
  1027. tp->rx_info[i].head = NULL;
  1028. }
  1029. for (i = 0; i < RTL8152_MAX_TX; i++) {
  1030. usb_free_urb(tp->tx_info[i].urb);
  1031. tp->tx_info[i].urb = NULL;
  1032. kfree(tp->tx_info[i].buffer);
  1033. tp->tx_info[i].buffer = NULL;
  1034. tp->tx_info[i].head = NULL;
  1035. }
  1036. usb_free_urb(tp->intr_urb);
  1037. tp->intr_urb = NULL;
  1038. kfree(tp->intr_buff);
  1039. tp->intr_buff = NULL;
  1040. }
  1041. static int alloc_all_mem(struct r8152 *tp)
  1042. {
  1043. struct net_device *netdev = tp->netdev;
  1044. struct usb_interface *intf = tp->intf;
  1045. struct usb_host_interface *alt = intf->cur_altsetting;
  1046. struct usb_host_endpoint *ep_intr = alt->endpoint + 2;
  1047. struct urb *urb;
  1048. int node, i;
  1049. u8 *buf;
  1050. node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1;
  1051. spin_lock_init(&tp->rx_lock);
  1052. spin_lock_init(&tp->tx_lock);
  1053. INIT_LIST_HEAD(&tp->tx_free);
  1054. skb_queue_head_init(&tp->tx_queue);
  1055. skb_queue_head_init(&tp->rx_queue);
  1056. for (i = 0; i < RTL8152_MAX_RX; i++) {
  1057. buf = kmalloc_node(agg_buf_sz, GFP_KERNEL, node);
  1058. if (!buf)
  1059. goto err1;
  1060. if (buf != rx_agg_align(buf)) {
  1061. kfree(buf);
  1062. buf = kmalloc_node(agg_buf_sz + RX_ALIGN, GFP_KERNEL,
  1063. node);
  1064. if (!buf)
  1065. goto err1;
  1066. }
  1067. urb = usb_alloc_urb(0, GFP_KERNEL);
  1068. if (!urb) {
  1069. kfree(buf);
  1070. goto err1;
  1071. }
  1072. INIT_LIST_HEAD(&tp->rx_info[i].list);
  1073. tp->rx_info[i].context = tp;
  1074. tp->rx_info[i].urb = urb;
  1075. tp->rx_info[i].buffer = buf;
  1076. tp->rx_info[i].head = rx_agg_align(buf);
  1077. }
  1078. for (i = 0; i < RTL8152_MAX_TX; i++) {
  1079. buf = kmalloc_node(agg_buf_sz, GFP_KERNEL, node);
  1080. if (!buf)
  1081. goto err1;
  1082. if (buf != tx_agg_align(buf)) {
  1083. kfree(buf);
  1084. buf = kmalloc_node(agg_buf_sz + TX_ALIGN, GFP_KERNEL,
  1085. node);
  1086. if (!buf)
  1087. goto err1;
  1088. }
  1089. urb = usb_alloc_urb(0, GFP_KERNEL);
  1090. if (!urb) {
  1091. kfree(buf);
  1092. goto err1;
  1093. }
  1094. INIT_LIST_HEAD(&tp->tx_info[i].list);
  1095. tp->tx_info[i].context = tp;
  1096. tp->tx_info[i].urb = urb;
  1097. tp->tx_info[i].buffer = buf;
  1098. tp->tx_info[i].head = tx_agg_align(buf);
  1099. list_add_tail(&tp->tx_info[i].list, &tp->tx_free);
  1100. }
  1101. tp->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
  1102. if (!tp->intr_urb)
  1103. goto err1;
  1104. tp->intr_buff = kmalloc(INTBUFSIZE, GFP_KERNEL);
  1105. if (!tp->intr_buff)
  1106. goto err1;
  1107. tp->intr_interval = (int)ep_intr->desc.bInterval;
  1108. usb_fill_int_urb(tp->intr_urb, tp->udev, usb_rcvintpipe(tp->udev, 3),
  1109. tp->intr_buff, INTBUFSIZE, intr_callback,
  1110. tp, tp->intr_interval);
  1111. return 0;
  1112. err1:
  1113. free_all_mem(tp);
  1114. return -ENOMEM;
  1115. }
  1116. static struct tx_agg *r8152_get_tx_agg(struct r8152 *tp)
  1117. {
  1118. struct tx_agg *agg = NULL;
  1119. unsigned long flags;
  1120. if (list_empty(&tp->tx_free))
  1121. return NULL;
  1122. spin_lock_irqsave(&tp->tx_lock, flags);
  1123. if (!list_empty(&tp->tx_free)) {
  1124. struct list_head *cursor;
  1125. cursor = tp->tx_free.next;
  1126. list_del_init(cursor);
  1127. agg = list_entry(cursor, struct tx_agg, list);
  1128. }
  1129. spin_unlock_irqrestore(&tp->tx_lock, flags);
  1130. return agg;
  1131. }
  1132. /* r8152_csum_workaround()
  1133. * The hw limites the value the transport offset. When the offset is out of the
  1134. * range, calculate the checksum by sw.
  1135. */
  1136. static void r8152_csum_workaround(struct r8152 *tp, struct sk_buff *skb,
  1137. struct sk_buff_head *list)
  1138. {
  1139. if (skb_shinfo(skb)->gso_size) {
  1140. netdev_features_t features = tp->netdev->features;
  1141. struct sk_buff_head seg_list;
  1142. struct sk_buff *segs, *nskb;
  1143. features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
  1144. segs = skb_gso_segment(skb, features);
  1145. if (IS_ERR(segs) || !segs)
  1146. goto drop;
  1147. __skb_queue_head_init(&seg_list);
  1148. do {
  1149. nskb = segs;
  1150. segs = segs->next;
  1151. nskb->next = NULL;
  1152. __skb_queue_tail(&seg_list, nskb);
  1153. } while (segs);
  1154. skb_queue_splice(&seg_list, list);
  1155. dev_kfree_skb(skb);
  1156. } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
  1157. if (skb_checksum_help(skb) < 0)
  1158. goto drop;
  1159. __skb_queue_head(list, skb);
  1160. } else {
  1161. struct net_device_stats *stats;
  1162. drop:
  1163. stats = &tp->netdev->stats;
  1164. stats->tx_dropped++;
  1165. dev_kfree_skb(skb);
  1166. }
  1167. }
  1168. /* msdn_giant_send_check()
  1169. * According to the document of microsoft, the TCP Pseudo Header excludes the
  1170. * packet length for IPv6 TCP large packets.
  1171. */
  1172. static int msdn_giant_send_check(struct sk_buff *skb)
  1173. {
  1174. const struct ipv6hdr *ipv6h;
  1175. struct tcphdr *th;
  1176. int ret;
  1177. ret = skb_cow_head(skb, 0);
  1178. if (ret)
  1179. return ret;
  1180. ipv6h = ipv6_hdr(skb);
  1181. th = tcp_hdr(skb);
  1182. th->check = 0;
  1183. th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
  1184. return ret;
  1185. }
  1186. static inline void rtl_tx_vlan_tag(struct tx_desc *desc, struct sk_buff *skb)
  1187. {
  1188. if (skb_vlan_tag_present(skb)) {
  1189. u32 opts2;
  1190. opts2 = TX_VLAN_TAG | swab16(skb_vlan_tag_get(skb));
  1191. desc->opts2 |= cpu_to_le32(opts2);
  1192. }
  1193. }
  1194. static inline void rtl_rx_vlan_tag(struct rx_desc *desc, struct sk_buff *skb)
  1195. {
  1196. u32 opts2 = le32_to_cpu(desc->opts2);
  1197. if (opts2 & RX_VLAN_TAG)
  1198. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
  1199. swab16(opts2 & 0xffff));
  1200. }
  1201. static int r8152_tx_csum(struct r8152 *tp, struct tx_desc *desc,
  1202. struct sk_buff *skb, u32 len, u32 transport_offset)
  1203. {
  1204. u32 mss = skb_shinfo(skb)->gso_size;
  1205. u32 opts1, opts2 = 0;
  1206. int ret = TX_CSUM_SUCCESS;
  1207. WARN_ON_ONCE(len > TX_LEN_MAX);
  1208. opts1 = len | TX_FS | TX_LS;
  1209. if (mss) {
  1210. if (transport_offset > GTTCPHO_MAX) {
  1211. netif_warn(tp, tx_err, tp->netdev,
  1212. "Invalid transport offset 0x%x for TSO\n",
  1213. transport_offset);
  1214. ret = TX_CSUM_TSO;
  1215. goto unavailable;
  1216. }
  1217. switch (vlan_get_protocol(skb)) {
  1218. case htons(ETH_P_IP):
  1219. opts1 |= GTSENDV4;
  1220. break;
  1221. case htons(ETH_P_IPV6):
  1222. if (msdn_giant_send_check(skb)) {
  1223. ret = TX_CSUM_TSO;
  1224. goto unavailable;
  1225. }
  1226. opts1 |= GTSENDV6;
  1227. break;
  1228. default:
  1229. WARN_ON_ONCE(1);
  1230. break;
  1231. }
  1232. opts1 |= transport_offset << GTTCPHO_SHIFT;
  1233. opts2 |= min(mss, MSS_MAX) << MSS_SHIFT;
  1234. } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
  1235. u8 ip_protocol;
  1236. if (transport_offset > TCPHO_MAX) {
  1237. netif_warn(tp, tx_err, tp->netdev,
  1238. "Invalid transport offset 0x%x\n",
  1239. transport_offset);
  1240. ret = TX_CSUM_NONE;
  1241. goto unavailable;
  1242. }
  1243. switch (vlan_get_protocol(skb)) {
  1244. case htons(ETH_P_IP):
  1245. opts2 |= IPV4_CS;
  1246. ip_protocol = ip_hdr(skb)->protocol;
  1247. break;
  1248. case htons(ETH_P_IPV6):
  1249. opts2 |= IPV6_CS;
  1250. ip_protocol = ipv6_hdr(skb)->nexthdr;
  1251. break;
  1252. default:
  1253. ip_protocol = IPPROTO_RAW;
  1254. break;
  1255. }
  1256. if (ip_protocol == IPPROTO_TCP)
  1257. opts2 |= TCP_CS;
  1258. else if (ip_protocol == IPPROTO_UDP)
  1259. opts2 |= UDP_CS;
  1260. else
  1261. WARN_ON_ONCE(1);
  1262. opts2 |= transport_offset << TCPHO_SHIFT;
  1263. }
  1264. desc->opts2 = cpu_to_le32(opts2);
  1265. desc->opts1 = cpu_to_le32(opts1);
  1266. unavailable:
  1267. return ret;
  1268. }
  1269. static int r8152_tx_agg_fill(struct r8152 *tp, struct tx_agg *agg)
  1270. {
  1271. struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
  1272. int remain, ret;
  1273. u8 *tx_data;
  1274. __skb_queue_head_init(&skb_head);
  1275. spin_lock(&tx_queue->lock);
  1276. skb_queue_splice_init(tx_queue, &skb_head);
  1277. spin_unlock(&tx_queue->lock);
  1278. tx_data = agg->head;
  1279. agg->skb_num = 0;
  1280. agg->skb_len = 0;
  1281. remain = agg_buf_sz;
  1282. while (remain >= ETH_ZLEN + sizeof(struct tx_desc)) {
  1283. struct tx_desc *tx_desc;
  1284. struct sk_buff *skb;
  1285. unsigned int len;
  1286. u32 offset;
  1287. skb = __skb_dequeue(&skb_head);
  1288. if (!skb)
  1289. break;
  1290. len = skb->len + sizeof(*tx_desc);
  1291. if (len > remain) {
  1292. __skb_queue_head(&skb_head, skb);
  1293. break;
  1294. }
  1295. tx_data = tx_agg_align(tx_data);
  1296. tx_desc = (struct tx_desc *)tx_data;
  1297. offset = (u32)skb_transport_offset(skb);
  1298. if (r8152_tx_csum(tp, tx_desc, skb, skb->len, offset)) {
  1299. r8152_csum_workaround(tp, skb, &skb_head);
  1300. continue;
  1301. }
  1302. rtl_tx_vlan_tag(tx_desc, skb);
  1303. tx_data += sizeof(*tx_desc);
  1304. len = skb->len;
  1305. if (skb_copy_bits(skb, 0, tx_data, len) < 0) {
  1306. struct net_device_stats *stats = &tp->netdev->stats;
  1307. stats->tx_dropped++;
  1308. dev_kfree_skb_any(skb);
  1309. tx_data -= sizeof(*tx_desc);
  1310. continue;
  1311. }
  1312. tx_data += len;
  1313. agg->skb_len += len;
  1314. agg->skb_num++;
  1315. dev_kfree_skb_any(skb);
  1316. remain = agg_buf_sz - (int)(tx_agg_align(tx_data) - agg->head);
  1317. }
  1318. if (!skb_queue_empty(&skb_head)) {
  1319. spin_lock(&tx_queue->lock);
  1320. skb_queue_splice(&skb_head, tx_queue);
  1321. spin_unlock(&tx_queue->lock);
  1322. }
  1323. netif_tx_lock(tp->netdev);
  1324. if (netif_queue_stopped(tp->netdev) &&
  1325. skb_queue_len(&tp->tx_queue) < tp->tx_qlen)
  1326. netif_wake_queue(tp->netdev);
  1327. netif_tx_unlock(tp->netdev);
  1328. ret = usb_autopm_get_interface_async(tp->intf);
  1329. if (ret < 0)
  1330. goto out_tx_fill;
  1331. usb_fill_bulk_urb(agg->urb, tp->udev, usb_sndbulkpipe(tp->udev, 2),
  1332. agg->head, (int)(tx_data - (u8 *)agg->head),
  1333. (usb_complete_t)write_bulk_callback, agg);
  1334. ret = usb_submit_urb(agg->urb, GFP_ATOMIC);
  1335. if (ret < 0)
  1336. usb_autopm_put_interface_async(tp->intf);
  1337. out_tx_fill:
  1338. return ret;
  1339. }
  1340. static u8 r8152_rx_csum(struct r8152 *tp, struct rx_desc *rx_desc)
  1341. {
  1342. u8 checksum = CHECKSUM_NONE;
  1343. u32 opts2, opts3;
  1344. if (tp->version == RTL_VER_01)
  1345. goto return_result;
  1346. opts2 = le32_to_cpu(rx_desc->opts2);
  1347. opts3 = le32_to_cpu(rx_desc->opts3);
  1348. if (opts2 & RD_IPV4_CS) {
  1349. if (opts3 & IPF)
  1350. checksum = CHECKSUM_NONE;
  1351. else if ((opts2 & RD_UDP_CS) && (opts3 & UDPF))
  1352. checksum = CHECKSUM_NONE;
  1353. else if ((opts2 & RD_TCP_CS) && (opts3 & TCPF))
  1354. checksum = CHECKSUM_NONE;
  1355. else
  1356. checksum = CHECKSUM_UNNECESSARY;
  1357. } else if (RD_IPV6_CS) {
  1358. if ((opts2 & RD_UDP_CS) && !(opts3 & UDPF))
  1359. checksum = CHECKSUM_UNNECESSARY;
  1360. else if ((opts2 & RD_TCP_CS) && !(opts3 & TCPF))
  1361. checksum = CHECKSUM_UNNECESSARY;
  1362. }
  1363. return_result:
  1364. return checksum;
  1365. }
  1366. static int rx_bottom(struct r8152 *tp, int budget)
  1367. {
  1368. unsigned long flags;
  1369. struct list_head *cursor, *next, rx_queue;
  1370. int ret = 0, work_done = 0;
  1371. if (!skb_queue_empty(&tp->rx_queue)) {
  1372. while (work_done < budget) {
  1373. struct sk_buff *skb = __skb_dequeue(&tp->rx_queue);
  1374. struct net_device *netdev = tp->netdev;
  1375. struct net_device_stats *stats = &netdev->stats;
  1376. unsigned int pkt_len;
  1377. if (!skb)
  1378. break;
  1379. pkt_len = skb->len;
  1380. napi_gro_receive(&tp->napi, skb);
  1381. work_done++;
  1382. stats->rx_packets++;
  1383. stats->rx_bytes += pkt_len;
  1384. }
  1385. }
  1386. if (list_empty(&tp->rx_done))
  1387. goto out1;
  1388. INIT_LIST_HEAD(&rx_queue);
  1389. spin_lock_irqsave(&tp->rx_lock, flags);
  1390. list_splice_init(&tp->rx_done, &rx_queue);
  1391. spin_unlock_irqrestore(&tp->rx_lock, flags);
  1392. list_for_each_safe(cursor, next, &rx_queue) {
  1393. struct rx_desc *rx_desc;
  1394. struct rx_agg *agg;
  1395. int len_used = 0;
  1396. struct urb *urb;
  1397. u8 *rx_data;
  1398. list_del_init(cursor);
  1399. agg = list_entry(cursor, struct rx_agg, list);
  1400. urb = agg->urb;
  1401. if (urb->actual_length < ETH_ZLEN)
  1402. goto submit;
  1403. rx_desc = agg->head;
  1404. rx_data = agg->head;
  1405. len_used += sizeof(struct rx_desc);
  1406. while (urb->actual_length > len_used) {
  1407. struct net_device *netdev = tp->netdev;
  1408. struct net_device_stats *stats = &netdev->stats;
  1409. unsigned int pkt_len;
  1410. struct sk_buff *skb;
  1411. pkt_len = le32_to_cpu(rx_desc->opts1) & RX_LEN_MASK;
  1412. if (pkt_len < ETH_ZLEN)
  1413. break;
  1414. len_used += pkt_len;
  1415. if (urb->actual_length < len_used)
  1416. break;
  1417. pkt_len -= CRC_SIZE;
  1418. rx_data += sizeof(struct rx_desc);
  1419. skb = netdev_alloc_skb_ip_align(netdev, pkt_len);
  1420. if (!skb) {
  1421. stats->rx_dropped++;
  1422. goto find_next_rx;
  1423. }
  1424. skb->ip_summed = r8152_rx_csum(tp, rx_desc);
  1425. memcpy(skb->data, rx_data, pkt_len);
  1426. skb_put(skb, pkt_len);
  1427. skb->protocol = eth_type_trans(skb, netdev);
  1428. rtl_rx_vlan_tag(rx_desc, skb);
  1429. if (work_done < budget) {
  1430. napi_gro_receive(&tp->napi, skb);
  1431. work_done++;
  1432. stats->rx_packets++;
  1433. stats->rx_bytes += pkt_len;
  1434. } else {
  1435. __skb_queue_tail(&tp->rx_queue, skb);
  1436. }
  1437. find_next_rx:
  1438. rx_data = rx_agg_align(rx_data + pkt_len + CRC_SIZE);
  1439. rx_desc = (struct rx_desc *)rx_data;
  1440. len_used = (int)(rx_data - (u8 *)agg->head);
  1441. len_used += sizeof(struct rx_desc);
  1442. }
  1443. submit:
  1444. if (!ret) {
  1445. ret = r8152_submit_rx(tp, agg, GFP_ATOMIC);
  1446. } else {
  1447. urb->actual_length = 0;
  1448. list_add_tail(&agg->list, next);
  1449. }
  1450. }
  1451. if (!list_empty(&rx_queue)) {
  1452. spin_lock_irqsave(&tp->rx_lock, flags);
  1453. list_splice_tail(&rx_queue, &tp->rx_done);
  1454. spin_unlock_irqrestore(&tp->rx_lock, flags);
  1455. }
  1456. out1:
  1457. return work_done;
  1458. }
  1459. static void tx_bottom(struct r8152 *tp)
  1460. {
  1461. int res;
  1462. do {
  1463. struct tx_agg *agg;
  1464. if (skb_queue_empty(&tp->tx_queue))
  1465. break;
  1466. agg = r8152_get_tx_agg(tp);
  1467. if (!agg)
  1468. break;
  1469. res = r8152_tx_agg_fill(tp, agg);
  1470. if (res) {
  1471. struct net_device *netdev = tp->netdev;
  1472. if (res == -ENODEV) {
  1473. set_bit(RTL8152_UNPLUG, &tp->flags);
  1474. netif_device_detach(netdev);
  1475. } else {
  1476. struct net_device_stats *stats = &netdev->stats;
  1477. unsigned long flags;
  1478. netif_warn(tp, tx_err, netdev,
  1479. "failed tx_urb %d\n", res);
  1480. stats->tx_dropped += agg->skb_num;
  1481. spin_lock_irqsave(&tp->tx_lock, flags);
  1482. list_add_tail(&agg->list, &tp->tx_free);
  1483. spin_unlock_irqrestore(&tp->tx_lock, flags);
  1484. }
  1485. }
  1486. } while (res == 0);
  1487. }
  1488. static void bottom_half(struct r8152 *tp)
  1489. {
  1490. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  1491. return;
  1492. if (!test_bit(WORK_ENABLE, &tp->flags))
  1493. return;
  1494. /* When link down, the driver would cancel all bulks. */
  1495. /* This avoid the re-submitting bulk */
  1496. if (!netif_carrier_ok(tp->netdev))
  1497. return;
  1498. clear_bit(SCHEDULE_NAPI, &tp->flags);
  1499. tx_bottom(tp);
  1500. }
  1501. static int r8152_poll(struct napi_struct *napi, int budget)
  1502. {
  1503. struct r8152 *tp = container_of(napi, struct r8152, napi);
  1504. int work_done;
  1505. work_done = rx_bottom(tp, budget);
  1506. bottom_half(tp);
  1507. if (work_done < budget) {
  1508. napi_complete(napi);
  1509. if (!list_empty(&tp->rx_done))
  1510. napi_schedule(napi);
  1511. }
  1512. return work_done;
  1513. }
  1514. static
  1515. int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags)
  1516. {
  1517. int ret;
  1518. /* The rx would be stopped, so skip submitting */
  1519. if (test_bit(RTL8152_UNPLUG, &tp->flags) ||
  1520. !test_bit(WORK_ENABLE, &tp->flags) || !netif_carrier_ok(tp->netdev))
  1521. return 0;
  1522. usb_fill_bulk_urb(agg->urb, tp->udev, usb_rcvbulkpipe(tp->udev, 1),
  1523. agg->head, agg_buf_sz,
  1524. (usb_complete_t)read_bulk_callback, agg);
  1525. ret = usb_submit_urb(agg->urb, mem_flags);
  1526. if (ret == -ENODEV) {
  1527. set_bit(RTL8152_UNPLUG, &tp->flags);
  1528. netif_device_detach(tp->netdev);
  1529. } else if (ret) {
  1530. struct urb *urb = agg->urb;
  1531. unsigned long flags;
  1532. urb->actual_length = 0;
  1533. spin_lock_irqsave(&tp->rx_lock, flags);
  1534. list_add_tail(&agg->list, &tp->rx_done);
  1535. spin_unlock_irqrestore(&tp->rx_lock, flags);
  1536. netif_err(tp, rx_err, tp->netdev,
  1537. "Couldn't submit rx[%p], ret = %d\n", agg, ret);
  1538. napi_schedule(&tp->napi);
  1539. }
  1540. return ret;
  1541. }
  1542. static void rtl_drop_queued_tx(struct r8152 *tp)
  1543. {
  1544. struct net_device_stats *stats = &tp->netdev->stats;
  1545. struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
  1546. struct sk_buff *skb;
  1547. if (skb_queue_empty(tx_queue))
  1548. return;
  1549. __skb_queue_head_init(&skb_head);
  1550. spin_lock_bh(&tx_queue->lock);
  1551. skb_queue_splice_init(tx_queue, &skb_head);
  1552. spin_unlock_bh(&tx_queue->lock);
  1553. while ((skb = __skb_dequeue(&skb_head))) {
  1554. dev_kfree_skb(skb);
  1555. stats->tx_dropped++;
  1556. }
  1557. }
  1558. static void rtl8152_tx_timeout(struct net_device *netdev)
  1559. {
  1560. struct r8152 *tp = netdev_priv(netdev);
  1561. netif_warn(tp, tx_err, netdev, "Tx timeout\n");
  1562. usb_queue_reset_device(tp->intf);
  1563. }
  1564. static void rtl8152_set_rx_mode(struct net_device *netdev)
  1565. {
  1566. struct r8152 *tp = netdev_priv(netdev);
  1567. if (netif_carrier_ok(netdev)) {
  1568. set_bit(RTL8152_SET_RX_MODE, &tp->flags);
  1569. schedule_delayed_work(&tp->schedule, 0);
  1570. }
  1571. }
  1572. static void _rtl8152_set_rx_mode(struct net_device *netdev)
  1573. {
  1574. struct r8152 *tp = netdev_priv(netdev);
  1575. u32 mc_filter[2]; /* Multicast hash filter */
  1576. __le32 tmp[2];
  1577. u32 ocp_data;
  1578. netif_stop_queue(netdev);
  1579. ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
  1580. ocp_data &= ~RCR_ACPT_ALL;
  1581. ocp_data |= RCR_AB | RCR_APM;
  1582. if (netdev->flags & IFF_PROMISC) {
  1583. /* Unconditionally log net taps. */
  1584. netif_notice(tp, link, netdev, "Promiscuous mode enabled\n");
  1585. ocp_data |= RCR_AM | RCR_AAP;
  1586. mc_filter[1] = 0xffffffff;
  1587. mc_filter[0] = 0xffffffff;
  1588. } else if ((netdev_mc_count(netdev) > multicast_filter_limit) ||
  1589. (netdev->flags & IFF_ALLMULTI)) {
  1590. /* Too many to filter perfectly -- accept all multicasts. */
  1591. ocp_data |= RCR_AM;
  1592. mc_filter[1] = 0xffffffff;
  1593. mc_filter[0] = 0xffffffff;
  1594. } else {
  1595. struct netdev_hw_addr *ha;
  1596. mc_filter[1] = 0;
  1597. mc_filter[0] = 0;
  1598. netdev_for_each_mc_addr(ha, netdev) {
  1599. int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
  1600. mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
  1601. ocp_data |= RCR_AM;
  1602. }
  1603. }
  1604. tmp[0] = __cpu_to_le32(swab32(mc_filter[1]));
  1605. tmp[1] = __cpu_to_le32(swab32(mc_filter[0]));
  1606. pla_ocp_write(tp, PLA_MAR, BYTE_EN_DWORD, sizeof(tmp), tmp);
  1607. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
  1608. netif_wake_queue(netdev);
  1609. }
  1610. static netdev_features_t
  1611. rtl8152_features_check(struct sk_buff *skb, struct net_device *dev,
  1612. netdev_features_t features)
  1613. {
  1614. u32 mss = skb_shinfo(skb)->gso_size;
  1615. int max_offset = mss ? GTTCPHO_MAX : TCPHO_MAX;
  1616. int offset = skb_transport_offset(skb);
  1617. if ((mss || skb->ip_summed == CHECKSUM_PARTIAL) && offset > max_offset)
  1618. features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
  1619. else if ((skb->len + sizeof(struct tx_desc)) > agg_buf_sz)
  1620. features &= ~NETIF_F_GSO_MASK;
  1621. return features;
  1622. }
  1623. static netdev_tx_t rtl8152_start_xmit(struct sk_buff *skb,
  1624. struct net_device *netdev)
  1625. {
  1626. struct r8152 *tp = netdev_priv(netdev);
  1627. skb_tx_timestamp(skb);
  1628. skb_queue_tail(&tp->tx_queue, skb);
  1629. if (!list_empty(&tp->tx_free)) {
  1630. if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
  1631. set_bit(SCHEDULE_NAPI, &tp->flags);
  1632. schedule_delayed_work(&tp->schedule, 0);
  1633. } else {
  1634. usb_mark_last_busy(tp->udev);
  1635. napi_schedule(&tp->napi);
  1636. }
  1637. } else if (skb_queue_len(&tp->tx_queue) > tp->tx_qlen) {
  1638. netif_stop_queue(netdev);
  1639. }
  1640. return NETDEV_TX_OK;
  1641. }
  1642. static void r8152b_reset_packet_filter(struct r8152 *tp)
  1643. {
  1644. u32 ocp_data;
  1645. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_FMC);
  1646. ocp_data &= ~FMC_FCR_MCU_EN;
  1647. ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
  1648. ocp_data |= FMC_FCR_MCU_EN;
  1649. ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
  1650. }
  1651. static void rtl8152_nic_reset(struct r8152 *tp)
  1652. {
  1653. int i;
  1654. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, CR_RST);
  1655. for (i = 0; i < 1000; i++) {
  1656. if (!(ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR) & CR_RST))
  1657. break;
  1658. usleep_range(100, 400);
  1659. }
  1660. }
  1661. static void set_tx_qlen(struct r8152 *tp)
  1662. {
  1663. struct net_device *netdev = tp->netdev;
  1664. tp->tx_qlen = agg_buf_sz / (netdev->mtu + VLAN_ETH_HLEN + VLAN_HLEN +
  1665. sizeof(struct tx_desc));
  1666. }
  1667. static inline u8 rtl8152_get_speed(struct r8152 *tp)
  1668. {
  1669. return ocp_read_byte(tp, MCU_TYPE_PLA, PLA_PHYSTATUS);
  1670. }
  1671. static void rtl_set_eee_plus(struct r8152 *tp)
  1672. {
  1673. u32 ocp_data;
  1674. u8 speed;
  1675. speed = rtl8152_get_speed(tp);
  1676. if (speed & _10bps) {
  1677. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
  1678. ocp_data |= EEEP_CR_EEEP_TX;
  1679. ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
  1680. } else {
  1681. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
  1682. ocp_data &= ~EEEP_CR_EEEP_TX;
  1683. ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
  1684. }
  1685. }
  1686. static void rxdy_gated_en(struct r8152 *tp, bool enable)
  1687. {
  1688. u32 ocp_data;
  1689. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
  1690. if (enable)
  1691. ocp_data |= RXDY_GATED_EN;
  1692. else
  1693. ocp_data &= ~RXDY_GATED_EN;
  1694. ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
  1695. }
  1696. static int rtl_start_rx(struct r8152 *tp)
  1697. {
  1698. int i, ret = 0;
  1699. INIT_LIST_HEAD(&tp->rx_done);
  1700. for (i = 0; i < RTL8152_MAX_RX; i++) {
  1701. INIT_LIST_HEAD(&tp->rx_info[i].list);
  1702. ret = r8152_submit_rx(tp, &tp->rx_info[i], GFP_KERNEL);
  1703. if (ret)
  1704. break;
  1705. }
  1706. if (ret && ++i < RTL8152_MAX_RX) {
  1707. struct list_head rx_queue;
  1708. unsigned long flags;
  1709. INIT_LIST_HEAD(&rx_queue);
  1710. do {
  1711. struct rx_agg *agg = &tp->rx_info[i++];
  1712. struct urb *urb = agg->urb;
  1713. urb->actual_length = 0;
  1714. list_add_tail(&agg->list, &rx_queue);
  1715. } while (i < RTL8152_MAX_RX);
  1716. spin_lock_irqsave(&tp->rx_lock, flags);
  1717. list_splice_tail(&rx_queue, &tp->rx_done);
  1718. spin_unlock_irqrestore(&tp->rx_lock, flags);
  1719. }
  1720. return ret;
  1721. }
  1722. static int rtl_stop_rx(struct r8152 *tp)
  1723. {
  1724. int i;
  1725. for (i = 0; i < RTL8152_MAX_RX; i++)
  1726. usb_kill_urb(tp->rx_info[i].urb);
  1727. while (!skb_queue_empty(&tp->rx_queue))
  1728. dev_kfree_skb(__skb_dequeue(&tp->rx_queue));
  1729. return 0;
  1730. }
  1731. static int rtl_enable(struct r8152 *tp)
  1732. {
  1733. u32 ocp_data;
  1734. r8152b_reset_packet_filter(tp);
  1735. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR);
  1736. ocp_data |= CR_RE | CR_TE;
  1737. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data);
  1738. rxdy_gated_en(tp, false);
  1739. return 0;
  1740. }
  1741. static int rtl8152_enable(struct r8152 *tp)
  1742. {
  1743. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  1744. return -ENODEV;
  1745. set_tx_qlen(tp);
  1746. rtl_set_eee_plus(tp);
  1747. return rtl_enable(tp);
  1748. }
  1749. static void r8153_set_rx_early_timeout(struct r8152 *tp)
  1750. {
  1751. u32 ocp_data = tp->coalesce / 8;
  1752. ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT, ocp_data);
  1753. }
  1754. static void r8153_set_rx_early_size(struct r8152 *tp)
  1755. {
  1756. u32 mtu = tp->netdev->mtu;
  1757. u32 ocp_data = (agg_buf_sz - mtu - VLAN_ETH_HLEN - VLAN_HLEN) / 8;
  1758. ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE, ocp_data);
  1759. }
  1760. static int rtl8153_enable(struct r8152 *tp)
  1761. {
  1762. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  1763. return -ENODEV;
  1764. usb_disable_lpm(tp->udev);
  1765. set_tx_qlen(tp);
  1766. rtl_set_eee_plus(tp);
  1767. r8153_set_rx_early_timeout(tp);
  1768. r8153_set_rx_early_size(tp);
  1769. return rtl_enable(tp);
  1770. }
  1771. static void rtl_disable(struct r8152 *tp)
  1772. {
  1773. u32 ocp_data;
  1774. int i;
  1775. if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
  1776. rtl_drop_queued_tx(tp);
  1777. return;
  1778. }
  1779. ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
  1780. ocp_data &= ~RCR_ACPT_ALL;
  1781. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
  1782. rtl_drop_queued_tx(tp);
  1783. for (i = 0; i < RTL8152_MAX_TX; i++)
  1784. usb_kill_urb(tp->tx_info[i].urb);
  1785. rxdy_gated_en(tp, true);
  1786. for (i = 0; i < 1000; i++) {
  1787. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  1788. if ((ocp_data & FIFO_EMPTY) == FIFO_EMPTY)
  1789. break;
  1790. usleep_range(1000, 2000);
  1791. }
  1792. for (i = 0; i < 1000; i++) {
  1793. if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0) & TCR0_TX_EMPTY)
  1794. break;
  1795. usleep_range(1000, 2000);
  1796. }
  1797. rtl_stop_rx(tp);
  1798. rtl8152_nic_reset(tp);
  1799. }
  1800. static void r8152_power_cut_en(struct r8152 *tp, bool enable)
  1801. {
  1802. u32 ocp_data;
  1803. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
  1804. if (enable)
  1805. ocp_data |= POWER_CUT;
  1806. else
  1807. ocp_data &= ~POWER_CUT;
  1808. ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
  1809. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS);
  1810. ocp_data &= ~RESUME_INDICATE;
  1811. ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data);
  1812. }
  1813. static void rtl_rx_vlan_en(struct r8152 *tp, bool enable)
  1814. {
  1815. u32 ocp_data;
  1816. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
  1817. if (enable)
  1818. ocp_data |= CPCR_RX_VLAN;
  1819. else
  1820. ocp_data &= ~CPCR_RX_VLAN;
  1821. ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
  1822. }
  1823. static int rtl8152_set_features(struct net_device *dev,
  1824. netdev_features_t features)
  1825. {
  1826. netdev_features_t changed = features ^ dev->features;
  1827. struct r8152 *tp = netdev_priv(dev);
  1828. int ret;
  1829. ret = usb_autopm_get_interface(tp->intf);
  1830. if (ret < 0)
  1831. goto out;
  1832. mutex_lock(&tp->control);
  1833. if (changed & NETIF_F_HW_VLAN_CTAG_RX) {
  1834. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  1835. rtl_rx_vlan_en(tp, true);
  1836. else
  1837. rtl_rx_vlan_en(tp, false);
  1838. }
  1839. mutex_unlock(&tp->control);
  1840. usb_autopm_put_interface(tp->intf);
  1841. out:
  1842. return ret;
  1843. }
  1844. #define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
  1845. static u32 __rtl_get_wol(struct r8152 *tp)
  1846. {
  1847. u32 ocp_data;
  1848. u32 wolopts = 0;
  1849. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CONFIG5);
  1850. if (!(ocp_data & LAN_WAKE_EN))
  1851. return 0;
  1852. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
  1853. if (ocp_data & LINK_ON_WAKE_EN)
  1854. wolopts |= WAKE_PHY;
  1855. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
  1856. if (ocp_data & UWF_EN)
  1857. wolopts |= WAKE_UCAST;
  1858. if (ocp_data & BWF_EN)
  1859. wolopts |= WAKE_BCAST;
  1860. if (ocp_data & MWF_EN)
  1861. wolopts |= WAKE_MCAST;
  1862. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
  1863. if (ocp_data & MAGIC_EN)
  1864. wolopts |= WAKE_MAGIC;
  1865. return wolopts;
  1866. }
  1867. static void __rtl_set_wol(struct r8152 *tp, u32 wolopts)
  1868. {
  1869. u32 ocp_data;
  1870. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
  1871. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
  1872. ocp_data &= ~LINK_ON_WAKE_EN;
  1873. if (wolopts & WAKE_PHY)
  1874. ocp_data |= LINK_ON_WAKE_EN;
  1875. ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
  1876. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
  1877. ocp_data &= ~(UWF_EN | BWF_EN | MWF_EN | LAN_WAKE_EN);
  1878. if (wolopts & WAKE_UCAST)
  1879. ocp_data |= UWF_EN;
  1880. if (wolopts & WAKE_BCAST)
  1881. ocp_data |= BWF_EN;
  1882. if (wolopts & WAKE_MCAST)
  1883. ocp_data |= MWF_EN;
  1884. if (wolopts & WAKE_ANY)
  1885. ocp_data |= LAN_WAKE_EN;
  1886. ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG5, ocp_data);
  1887. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
  1888. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
  1889. ocp_data &= ~MAGIC_EN;
  1890. if (wolopts & WAKE_MAGIC)
  1891. ocp_data |= MAGIC_EN;
  1892. ocp_write_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL, ocp_data);
  1893. if (wolopts & WAKE_ANY)
  1894. device_set_wakeup_enable(&tp->udev->dev, true);
  1895. else
  1896. device_set_wakeup_enable(&tp->udev->dev, false);
  1897. }
  1898. static void r8153_u1u2en(struct r8152 *tp, bool enable)
  1899. {
  1900. u8 u1u2[8];
  1901. if (enable)
  1902. memset(u1u2, 0xff, sizeof(u1u2));
  1903. else
  1904. memset(u1u2, 0x00, sizeof(u1u2));
  1905. usb_ocp_write(tp, USB_TOLERANCE, BYTE_EN_SIX_BYTES, sizeof(u1u2), u1u2);
  1906. }
  1907. static void r8153_u2p3en(struct r8152 *tp, bool enable)
  1908. {
  1909. u32 ocp_data;
  1910. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL);
  1911. if (enable && tp->version != RTL_VER_03 && tp->version != RTL_VER_04)
  1912. ocp_data |= U2P3_ENABLE;
  1913. else
  1914. ocp_data &= ~U2P3_ENABLE;
  1915. ocp_write_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL, ocp_data);
  1916. }
  1917. static void r8153_power_cut_en(struct r8152 *tp, bool enable)
  1918. {
  1919. u32 ocp_data;
  1920. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT);
  1921. if (enable)
  1922. ocp_data |= PWR_EN | PHASE2_EN;
  1923. else
  1924. ocp_data &= ~(PWR_EN | PHASE2_EN);
  1925. ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
  1926. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
  1927. ocp_data &= ~PCUT_STATUS;
  1928. ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
  1929. }
  1930. static bool rtl_can_wakeup(struct r8152 *tp)
  1931. {
  1932. struct usb_device *udev = tp->udev;
  1933. return (udev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_WAKEUP);
  1934. }
  1935. static void rtl_runtime_suspend_enable(struct r8152 *tp, bool enable)
  1936. {
  1937. if (enable) {
  1938. u32 ocp_data;
  1939. __rtl_set_wol(tp, WAKE_ANY);
  1940. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
  1941. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
  1942. ocp_data |= LINK_OFF_WAKE_EN;
  1943. ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
  1944. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
  1945. } else {
  1946. u32 ocp_data;
  1947. __rtl_set_wol(tp, tp->saved_wolopts);
  1948. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
  1949. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
  1950. ocp_data &= ~LINK_OFF_WAKE_EN;
  1951. ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
  1952. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
  1953. }
  1954. }
  1955. static void rtl8153_runtime_enable(struct r8152 *tp, bool enable)
  1956. {
  1957. rtl_runtime_suspend_enable(tp, enable);
  1958. if (enable) {
  1959. r8153_u1u2en(tp, false);
  1960. r8153_u2p3en(tp, false);
  1961. } else {
  1962. r8153_u2p3en(tp, true);
  1963. r8153_u1u2en(tp, true);
  1964. }
  1965. }
  1966. static void rtl_phy_reset(struct r8152 *tp)
  1967. {
  1968. u16 data;
  1969. int i;
  1970. data = r8152_mdio_read(tp, MII_BMCR);
  1971. /* don't reset again before the previous one complete */
  1972. if (data & BMCR_RESET)
  1973. return;
  1974. data |= BMCR_RESET;
  1975. r8152_mdio_write(tp, MII_BMCR, data);
  1976. for (i = 0; i < 50; i++) {
  1977. msleep(20);
  1978. if ((r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET) == 0)
  1979. break;
  1980. }
  1981. }
  1982. static void r8153_teredo_off(struct r8152 *tp)
  1983. {
  1984. u32 ocp_data;
  1985. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
  1986. ocp_data &= ~(TEREDO_SEL | TEREDO_RS_EVENT_MASK | OOB_TEREDO_EN);
  1987. ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
  1988. ocp_write_word(tp, MCU_TYPE_PLA, PLA_WDT6_CTRL, WDT6_SET_MODE);
  1989. ocp_write_word(tp, MCU_TYPE_PLA, PLA_REALWOW_TIMER, 0);
  1990. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TEREDO_TIMER, 0);
  1991. }
  1992. static void rtl_reset_bmu(struct r8152 *tp)
  1993. {
  1994. u32 ocp_data;
  1995. ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_BMU_RESET);
  1996. ocp_data &= ~(BMU_RESET_EP_IN | BMU_RESET_EP_OUT);
  1997. ocp_write_byte(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data);
  1998. ocp_data |= BMU_RESET_EP_IN | BMU_RESET_EP_OUT;
  1999. ocp_write_byte(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data);
  2000. }
  2001. static void r8152_aldps_en(struct r8152 *tp, bool enable)
  2002. {
  2003. if (enable) {
  2004. ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPWRSAVE | ENPDNPS |
  2005. LINKENA | DIS_SDSAVE);
  2006. } else {
  2007. ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPDNPS | LINKENA |
  2008. DIS_SDSAVE);
  2009. msleep(20);
  2010. }
  2011. }
  2012. static void rtl8152_disable(struct r8152 *tp)
  2013. {
  2014. r8152_aldps_en(tp, false);
  2015. rtl_disable(tp);
  2016. r8152_aldps_en(tp, true);
  2017. }
  2018. static void r8152b_hw_phy_cfg(struct r8152 *tp)
  2019. {
  2020. u16 data;
  2021. data = r8152_mdio_read(tp, MII_BMCR);
  2022. if (data & BMCR_PDOWN) {
  2023. data &= ~BMCR_PDOWN;
  2024. r8152_mdio_write(tp, MII_BMCR, data);
  2025. }
  2026. set_bit(PHY_RESET, &tp->flags);
  2027. }
  2028. static void r8152b_exit_oob(struct r8152 *tp)
  2029. {
  2030. u32 ocp_data;
  2031. int i;
  2032. ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
  2033. ocp_data &= ~RCR_ACPT_ALL;
  2034. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
  2035. rxdy_gated_en(tp, true);
  2036. r8153_teredo_off(tp);
  2037. r8152b_hw_phy_cfg(tp);
  2038. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
  2039. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, 0x00);
  2040. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  2041. ocp_data &= ~NOW_IS_OOB;
  2042. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
  2043. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
  2044. ocp_data &= ~MCU_BORW_EN;
  2045. ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
  2046. for (i = 0; i < 1000; i++) {
  2047. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  2048. if (ocp_data & LINK_LIST_READY)
  2049. break;
  2050. usleep_range(1000, 2000);
  2051. }
  2052. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
  2053. ocp_data |= RE_INIT_LL;
  2054. ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
  2055. for (i = 0; i < 1000; i++) {
  2056. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  2057. if (ocp_data & LINK_LIST_READY)
  2058. break;
  2059. usleep_range(1000, 2000);
  2060. }
  2061. rtl8152_nic_reset(tp);
  2062. /* rx share fifo credit full threshold */
  2063. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
  2064. if (tp->udev->speed == USB_SPEED_FULL ||
  2065. tp->udev->speed == USB_SPEED_LOW) {
  2066. /* rx share fifo credit near full threshold */
  2067. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
  2068. RXFIFO_THR2_FULL);
  2069. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
  2070. RXFIFO_THR3_FULL);
  2071. } else {
  2072. /* rx share fifo credit near full threshold */
  2073. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
  2074. RXFIFO_THR2_HIGH);
  2075. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
  2076. RXFIFO_THR3_HIGH);
  2077. }
  2078. /* TX share fifo free credit full threshold */
  2079. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL);
  2080. ocp_write_byte(tp, MCU_TYPE_USB, USB_TX_AGG, TX_AGG_MAX_THRESHOLD);
  2081. ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_HIGH);
  2082. ocp_write_dword(tp, MCU_TYPE_USB, USB_TX_DMA,
  2083. TEST_MODE_DISABLE | TX_SIZE_ADJUST1);
  2084. rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
  2085. ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
  2086. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
  2087. ocp_data |= TCR0_AUTO_FIFO;
  2088. ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
  2089. }
  2090. static void r8152b_enter_oob(struct r8152 *tp)
  2091. {
  2092. u32 ocp_data;
  2093. int i;
  2094. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  2095. ocp_data &= ~NOW_IS_OOB;
  2096. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
  2097. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_OOB);
  2098. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_OOB);
  2099. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_OOB);
  2100. rtl_disable(tp);
  2101. for (i = 0; i < 1000; i++) {
  2102. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  2103. if (ocp_data & LINK_LIST_READY)
  2104. break;
  2105. usleep_range(1000, 2000);
  2106. }
  2107. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
  2108. ocp_data |= RE_INIT_LL;
  2109. ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
  2110. for (i = 0; i < 1000; i++) {
  2111. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  2112. if (ocp_data & LINK_LIST_READY)
  2113. break;
  2114. usleep_range(1000, 2000);
  2115. }
  2116. ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
  2117. rtl_rx_vlan_en(tp, true);
  2118. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PAL_BDC_CR);
  2119. ocp_data |= ALDPS_PROXY_MODE;
  2120. ocp_write_word(tp, MCU_TYPE_PLA, PAL_BDC_CR, ocp_data);
  2121. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  2122. ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
  2123. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
  2124. rxdy_gated_en(tp, false);
  2125. ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
  2126. ocp_data |= RCR_APM | RCR_AM | RCR_AB;
  2127. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
  2128. }
  2129. static void r8153_hw_phy_cfg(struct r8152 *tp)
  2130. {
  2131. u32 ocp_data;
  2132. u16 data;
  2133. if (tp->version == RTL_VER_03 || tp->version == RTL_VER_04 ||
  2134. tp->version == RTL_VER_05)
  2135. ocp_reg_write(tp, OCP_ADC_CFG, CKADSEL_L | ADC_EN | EN_EMI_L);
  2136. data = r8152_mdio_read(tp, MII_BMCR);
  2137. if (data & BMCR_PDOWN) {
  2138. data &= ~BMCR_PDOWN;
  2139. r8152_mdio_write(tp, MII_BMCR, data);
  2140. }
  2141. if (tp->version == RTL_VER_03) {
  2142. data = ocp_reg_read(tp, OCP_EEE_CFG);
  2143. data &= ~CTAP_SHORT_EN;
  2144. ocp_reg_write(tp, OCP_EEE_CFG, data);
  2145. }
  2146. data = ocp_reg_read(tp, OCP_POWER_CFG);
  2147. data |= EEE_CLKDIV_EN;
  2148. ocp_reg_write(tp, OCP_POWER_CFG, data);
  2149. data = ocp_reg_read(tp, OCP_DOWN_SPEED);
  2150. data |= EN_10M_BGOFF;
  2151. ocp_reg_write(tp, OCP_DOWN_SPEED, data);
  2152. data = ocp_reg_read(tp, OCP_POWER_CFG);
  2153. data |= EN_10M_PLLOFF;
  2154. ocp_reg_write(tp, OCP_POWER_CFG, data);
  2155. sram_write(tp, SRAM_IMPEDANCE, 0x0b13);
  2156. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
  2157. ocp_data |= PFM_PWM_SWITCH;
  2158. ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
  2159. /* Enable LPF corner auto tune */
  2160. sram_write(tp, SRAM_LPF_CFG, 0xf70f);
  2161. /* Adjust 10M Amplitude */
  2162. sram_write(tp, SRAM_10M_AMP1, 0x00af);
  2163. sram_write(tp, SRAM_10M_AMP2, 0x0208);
  2164. set_bit(PHY_RESET, &tp->flags);
  2165. }
  2166. static void r8153_first_init(struct r8152 *tp)
  2167. {
  2168. u32 ocp_data;
  2169. int i;
  2170. rxdy_gated_en(tp, true);
  2171. r8153_teredo_off(tp);
  2172. ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
  2173. ocp_data &= ~RCR_ACPT_ALL;
  2174. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
  2175. r8153_hw_phy_cfg(tp);
  2176. rtl8152_nic_reset(tp);
  2177. rtl_reset_bmu(tp);
  2178. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  2179. ocp_data &= ~NOW_IS_OOB;
  2180. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
  2181. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
  2182. ocp_data &= ~MCU_BORW_EN;
  2183. ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
  2184. for (i = 0; i < 1000; i++) {
  2185. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  2186. if (ocp_data & LINK_LIST_READY)
  2187. break;
  2188. usleep_range(1000, 2000);
  2189. }
  2190. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
  2191. ocp_data |= RE_INIT_LL;
  2192. ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
  2193. for (i = 0; i < 1000; i++) {
  2194. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  2195. if (ocp_data & LINK_LIST_READY)
  2196. break;
  2197. usleep_range(1000, 2000);
  2198. }
  2199. rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
  2200. ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8153_RMS);
  2201. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, MTPS_JUMBO);
  2202. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
  2203. ocp_data |= TCR0_AUTO_FIFO;
  2204. ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
  2205. rtl8152_nic_reset(tp);
  2206. /* rx share fifo credit full threshold */
  2207. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
  2208. ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_NORMAL);
  2209. ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_NORMAL);
  2210. /* TX share fifo free credit full threshold */
  2211. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL2);
  2212. /* rx aggregation */
  2213. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
  2214. ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
  2215. ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
  2216. }
  2217. static void r8153_enter_oob(struct r8152 *tp)
  2218. {
  2219. u32 ocp_data;
  2220. int i;
  2221. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  2222. ocp_data &= ~NOW_IS_OOB;
  2223. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
  2224. rtl_disable(tp);
  2225. rtl_reset_bmu(tp);
  2226. for (i = 0; i < 1000; i++) {
  2227. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  2228. if (ocp_data & LINK_LIST_READY)
  2229. break;
  2230. usleep_range(1000, 2000);
  2231. }
  2232. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
  2233. ocp_data |= RE_INIT_LL;
  2234. ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
  2235. for (i = 0; i < 1000; i++) {
  2236. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  2237. if (ocp_data & LINK_LIST_READY)
  2238. break;
  2239. usleep_range(1000, 2000);
  2240. }
  2241. ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8153_RMS);
  2242. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
  2243. ocp_data &= ~TEREDO_WAKE_MASK;
  2244. ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
  2245. rtl_rx_vlan_en(tp, true);
  2246. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PAL_BDC_CR);
  2247. ocp_data |= ALDPS_PROXY_MODE;
  2248. ocp_write_word(tp, MCU_TYPE_PLA, PAL_BDC_CR, ocp_data);
  2249. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
  2250. ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
  2251. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
  2252. rxdy_gated_en(tp, false);
  2253. ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
  2254. ocp_data |= RCR_APM | RCR_AM | RCR_AB;
  2255. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
  2256. }
  2257. static void r8153_aldps_en(struct r8152 *tp, bool enable)
  2258. {
  2259. u16 data;
  2260. data = ocp_reg_read(tp, OCP_POWER_CFG);
  2261. if (enable) {
  2262. data |= EN_ALDPS;
  2263. ocp_reg_write(tp, OCP_POWER_CFG, data);
  2264. } else {
  2265. data &= ~EN_ALDPS;
  2266. ocp_reg_write(tp, OCP_POWER_CFG, data);
  2267. msleep(20);
  2268. }
  2269. }
  2270. static void rtl8153_disable(struct r8152 *tp)
  2271. {
  2272. r8153_aldps_en(tp, false);
  2273. rtl_disable(tp);
  2274. rtl_reset_bmu(tp);
  2275. r8153_aldps_en(tp, true);
  2276. usb_enable_lpm(tp->udev);
  2277. }
  2278. static int rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u16 speed, u8 duplex)
  2279. {
  2280. u16 bmcr, anar, gbcr;
  2281. int ret = 0;
  2282. cancel_delayed_work_sync(&tp->schedule);
  2283. anar = r8152_mdio_read(tp, MII_ADVERTISE);
  2284. anar &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
  2285. ADVERTISE_100HALF | ADVERTISE_100FULL);
  2286. if (tp->mii.supports_gmii) {
  2287. gbcr = r8152_mdio_read(tp, MII_CTRL1000);
  2288. gbcr &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
  2289. } else {
  2290. gbcr = 0;
  2291. }
  2292. if (autoneg == AUTONEG_DISABLE) {
  2293. if (speed == SPEED_10) {
  2294. bmcr = 0;
  2295. anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
  2296. } else if (speed == SPEED_100) {
  2297. bmcr = BMCR_SPEED100;
  2298. anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
  2299. } else if (speed == SPEED_1000 && tp->mii.supports_gmii) {
  2300. bmcr = BMCR_SPEED1000;
  2301. gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
  2302. } else {
  2303. ret = -EINVAL;
  2304. goto out;
  2305. }
  2306. if (duplex == DUPLEX_FULL)
  2307. bmcr |= BMCR_FULLDPLX;
  2308. } else {
  2309. if (speed == SPEED_10) {
  2310. if (duplex == DUPLEX_FULL)
  2311. anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
  2312. else
  2313. anar |= ADVERTISE_10HALF;
  2314. } else if (speed == SPEED_100) {
  2315. if (duplex == DUPLEX_FULL) {
  2316. anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
  2317. anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
  2318. } else {
  2319. anar |= ADVERTISE_10HALF;
  2320. anar |= ADVERTISE_100HALF;
  2321. }
  2322. } else if (speed == SPEED_1000 && tp->mii.supports_gmii) {
  2323. if (duplex == DUPLEX_FULL) {
  2324. anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
  2325. anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
  2326. gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
  2327. } else {
  2328. anar |= ADVERTISE_10HALF;
  2329. anar |= ADVERTISE_100HALF;
  2330. gbcr |= ADVERTISE_1000HALF;
  2331. }
  2332. } else {
  2333. ret = -EINVAL;
  2334. goto out;
  2335. }
  2336. bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
  2337. }
  2338. if (test_bit(PHY_RESET, &tp->flags))
  2339. bmcr |= BMCR_RESET;
  2340. if (tp->mii.supports_gmii)
  2341. r8152_mdio_write(tp, MII_CTRL1000, gbcr);
  2342. r8152_mdio_write(tp, MII_ADVERTISE, anar);
  2343. r8152_mdio_write(tp, MII_BMCR, bmcr);
  2344. if (test_and_clear_bit(PHY_RESET, &tp->flags)) {
  2345. int i;
  2346. for (i = 0; i < 50; i++) {
  2347. msleep(20);
  2348. if ((r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET) == 0)
  2349. break;
  2350. }
  2351. }
  2352. out:
  2353. return ret;
  2354. }
  2355. static void rtl8152_up(struct r8152 *tp)
  2356. {
  2357. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  2358. return;
  2359. r8152_aldps_en(tp, false);
  2360. r8152b_exit_oob(tp);
  2361. r8152_aldps_en(tp, true);
  2362. }
  2363. static void rtl8152_down(struct r8152 *tp)
  2364. {
  2365. if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
  2366. rtl_drop_queued_tx(tp);
  2367. return;
  2368. }
  2369. r8152_power_cut_en(tp, false);
  2370. r8152_aldps_en(tp, false);
  2371. r8152b_enter_oob(tp);
  2372. r8152_aldps_en(tp, true);
  2373. }
  2374. static void rtl8153_up(struct r8152 *tp)
  2375. {
  2376. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  2377. return;
  2378. r8153_u1u2en(tp, false);
  2379. r8153_aldps_en(tp, false);
  2380. r8153_first_init(tp);
  2381. r8153_aldps_en(tp, true);
  2382. r8153_u2p3en(tp, true);
  2383. r8153_u1u2en(tp, true);
  2384. usb_enable_lpm(tp->udev);
  2385. }
  2386. static void rtl8153_down(struct r8152 *tp)
  2387. {
  2388. if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
  2389. rtl_drop_queued_tx(tp);
  2390. return;
  2391. }
  2392. r8153_u1u2en(tp, false);
  2393. r8153_u2p3en(tp, false);
  2394. r8153_power_cut_en(tp, false);
  2395. r8153_aldps_en(tp, false);
  2396. r8153_enter_oob(tp);
  2397. r8153_aldps_en(tp, true);
  2398. }
  2399. static bool rtl8152_in_nway(struct r8152 *tp)
  2400. {
  2401. u16 nway_state;
  2402. ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, 0x2000);
  2403. tp->ocp_base = 0x2000;
  2404. ocp_write_byte(tp, MCU_TYPE_PLA, 0xb014, 0x4c); /* phy state */
  2405. nway_state = ocp_read_word(tp, MCU_TYPE_PLA, 0xb01a);
  2406. /* bit 15: TXDIS_STATE, bit 14: ABD_STATE */
  2407. if (nway_state & 0xc000)
  2408. return false;
  2409. else
  2410. return true;
  2411. }
  2412. static bool rtl8153_in_nway(struct r8152 *tp)
  2413. {
  2414. u16 phy_state = ocp_reg_read(tp, OCP_PHY_STATE) & 0xff;
  2415. if (phy_state == TXDIS_STATE || phy_state == ABD_STATE)
  2416. return false;
  2417. else
  2418. return true;
  2419. }
  2420. static void set_carrier(struct r8152 *tp)
  2421. {
  2422. struct net_device *netdev = tp->netdev;
  2423. u8 speed;
  2424. speed = rtl8152_get_speed(tp);
  2425. if (speed & LINK_STATUS) {
  2426. if (!netif_carrier_ok(netdev)) {
  2427. tp->rtl_ops.enable(tp);
  2428. set_bit(RTL8152_SET_RX_MODE, &tp->flags);
  2429. napi_disable(&tp->napi);
  2430. netif_carrier_on(netdev);
  2431. rtl_start_rx(tp);
  2432. napi_enable(&tp->napi);
  2433. }
  2434. } else {
  2435. if (netif_carrier_ok(netdev)) {
  2436. netif_carrier_off(netdev);
  2437. napi_disable(&tp->napi);
  2438. tp->rtl_ops.disable(tp);
  2439. napi_enable(&tp->napi);
  2440. }
  2441. }
  2442. }
  2443. static void rtl_work_func_t(struct work_struct *work)
  2444. {
  2445. struct r8152 *tp = container_of(work, struct r8152, schedule.work);
  2446. /* If the device is unplugged or !netif_running(), the workqueue
  2447. * doesn't need to wake the device, and could return directly.
  2448. */
  2449. if (test_bit(RTL8152_UNPLUG, &tp->flags) || !netif_running(tp->netdev))
  2450. return;
  2451. if (usb_autopm_get_interface(tp->intf) < 0)
  2452. return;
  2453. if (!test_bit(WORK_ENABLE, &tp->flags))
  2454. goto out1;
  2455. if (!mutex_trylock(&tp->control)) {
  2456. schedule_delayed_work(&tp->schedule, 0);
  2457. goto out1;
  2458. }
  2459. if (test_and_clear_bit(RTL8152_LINK_CHG, &tp->flags))
  2460. set_carrier(tp);
  2461. if (test_and_clear_bit(RTL8152_SET_RX_MODE, &tp->flags))
  2462. _rtl8152_set_rx_mode(tp->netdev);
  2463. /* don't schedule napi before linking */
  2464. if (test_and_clear_bit(SCHEDULE_NAPI, &tp->flags) &&
  2465. netif_carrier_ok(tp->netdev))
  2466. napi_schedule(&tp->napi);
  2467. if (test_and_clear_bit(PHY_RESET, &tp->flags))
  2468. rtl_phy_reset(tp);
  2469. mutex_unlock(&tp->control);
  2470. out1:
  2471. usb_autopm_put_interface(tp->intf);
  2472. }
  2473. #ifdef CONFIG_PM_SLEEP
  2474. static int rtl_notifier(struct notifier_block *nb, unsigned long action,
  2475. void *data)
  2476. {
  2477. struct r8152 *tp = container_of(nb, struct r8152, pm_notifier);
  2478. switch (action) {
  2479. case PM_HIBERNATION_PREPARE:
  2480. case PM_SUSPEND_PREPARE:
  2481. usb_autopm_get_interface(tp->intf);
  2482. break;
  2483. case PM_POST_HIBERNATION:
  2484. case PM_POST_SUSPEND:
  2485. usb_autopm_put_interface(tp->intf);
  2486. break;
  2487. case PM_POST_RESTORE:
  2488. case PM_RESTORE_PREPARE:
  2489. default:
  2490. break;
  2491. }
  2492. return NOTIFY_DONE;
  2493. }
  2494. #endif
  2495. static int rtl8152_open(struct net_device *netdev)
  2496. {
  2497. struct r8152 *tp = netdev_priv(netdev);
  2498. int res = 0;
  2499. res = alloc_all_mem(tp);
  2500. if (res)
  2501. goto out;
  2502. netif_carrier_off(netdev);
  2503. res = usb_autopm_get_interface(tp->intf);
  2504. if (res < 0) {
  2505. free_all_mem(tp);
  2506. goto out;
  2507. }
  2508. mutex_lock(&tp->control);
  2509. tp->rtl_ops.up(tp);
  2510. rtl8152_set_speed(tp, AUTONEG_ENABLE,
  2511. tp->mii.supports_gmii ? SPEED_1000 : SPEED_100,
  2512. DUPLEX_FULL);
  2513. netif_carrier_off(netdev);
  2514. netif_start_queue(netdev);
  2515. set_bit(WORK_ENABLE, &tp->flags);
  2516. res = usb_submit_urb(tp->intr_urb, GFP_KERNEL);
  2517. if (res) {
  2518. if (res == -ENODEV)
  2519. netif_device_detach(tp->netdev);
  2520. netif_warn(tp, ifup, netdev, "intr_urb submit failed: %d\n",
  2521. res);
  2522. free_all_mem(tp);
  2523. } else {
  2524. napi_enable(&tp->napi);
  2525. }
  2526. mutex_unlock(&tp->control);
  2527. usb_autopm_put_interface(tp->intf);
  2528. #ifdef CONFIG_PM_SLEEP
  2529. tp->pm_notifier.notifier_call = rtl_notifier;
  2530. register_pm_notifier(&tp->pm_notifier);
  2531. #endif
  2532. out:
  2533. return res;
  2534. }
  2535. static int rtl8152_close(struct net_device *netdev)
  2536. {
  2537. struct r8152 *tp = netdev_priv(netdev);
  2538. int res = 0;
  2539. #ifdef CONFIG_PM_SLEEP
  2540. unregister_pm_notifier(&tp->pm_notifier);
  2541. #endif
  2542. napi_disable(&tp->napi);
  2543. clear_bit(WORK_ENABLE, &tp->flags);
  2544. usb_kill_urb(tp->intr_urb);
  2545. cancel_delayed_work_sync(&tp->schedule);
  2546. netif_stop_queue(netdev);
  2547. res = usb_autopm_get_interface(tp->intf);
  2548. if (res < 0 || test_bit(RTL8152_UNPLUG, &tp->flags)) {
  2549. rtl_drop_queued_tx(tp);
  2550. rtl_stop_rx(tp);
  2551. } else {
  2552. mutex_lock(&tp->control);
  2553. tp->rtl_ops.down(tp);
  2554. mutex_unlock(&tp->control);
  2555. usb_autopm_put_interface(tp->intf);
  2556. }
  2557. free_all_mem(tp);
  2558. return res;
  2559. }
  2560. static inline void r8152_mmd_indirect(struct r8152 *tp, u16 dev, u16 reg)
  2561. {
  2562. ocp_reg_write(tp, OCP_EEE_AR, FUN_ADDR | dev);
  2563. ocp_reg_write(tp, OCP_EEE_DATA, reg);
  2564. ocp_reg_write(tp, OCP_EEE_AR, FUN_DATA | dev);
  2565. }
  2566. static u16 r8152_mmd_read(struct r8152 *tp, u16 dev, u16 reg)
  2567. {
  2568. u16 data;
  2569. r8152_mmd_indirect(tp, dev, reg);
  2570. data = ocp_reg_read(tp, OCP_EEE_DATA);
  2571. ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
  2572. return data;
  2573. }
  2574. static void r8152_mmd_write(struct r8152 *tp, u16 dev, u16 reg, u16 data)
  2575. {
  2576. r8152_mmd_indirect(tp, dev, reg);
  2577. ocp_reg_write(tp, OCP_EEE_DATA, data);
  2578. ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
  2579. }
  2580. static void r8152_eee_en(struct r8152 *tp, bool enable)
  2581. {
  2582. u16 config1, config2, config3;
  2583. u32 ocp_data;
  2584. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
  2585. config1 = ocp_reg_read(tp, OCP_EEE_CONFIG1) & ~sd_rise_time_mask;
  2586. config2 = ocp_reg_read(tp, OCP_EEE_CONFIG2);
  2587. config3 = ocp_reg_read(tp, OCP_EEE_CONFIG3) & ~fast_snr_mask;
  2588. if (enable) {
  2589. ocp_data |= EEE_RX_EN | EEE_TX_EN;
  2590. config1 |= EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN | RX_QUIET_EN;
  2591. config1 |= sd_rise_time(1);
  2592. config2 |= RG_DACQUIET_EN | RG_LDVQUIET_EN;
  2593. config3 |= fast_snr(42);
  2594. } else {
  2595. ocp_data &= ~(EEE_RX_EN | EEE_TX_EN);
  2596. config1 &= ~(EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN |
  2597. RX_QUIET_EN);
  2598. config1 |= sd_rise_time(7);
  2599. config2 &= ~(RG_DACQUIET_EN | RG_LDVQUIET_EN);
  2600. config3 |= fast_snr(511);
  2601. }
  2602. ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
  2603. ocp_reg_write(tp, OCP_EEE_CONFIG1, config1);
  2604. ocp_reg_write(tp, OCP_EEE_CONFIG2, config2);
  2605. ocp_reg_write(tp, OCP_EEE_CONFIG3, config3);
  2606. }
  2607. static void r8152b_enable_eee(struct r8152 *tp)
  2608. {
  2609. r8152_eee_en(tp, true);
  2610. r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, MDIO_EEE_100TX);
  2611. }
  2612. static void r8153_eee_en(struct r8152 *tp, bool enable)
  2613. {
  2614. u32 ocp_data;
  2615. u16 config;
  2616. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
  2617. config = ocp_reg_read(tp, OCP_EEE_CFG);
  2618. if (enable) {
  2619. ocp_data |= EEE_RX_EN | EEE_TX_EN;
  2620. config |= EEE10_EN;
  2621. } else {
  2622. ocp_data &= ~(EEE_RX_EN | EEE_TX_EN);
  2623. config &= ~EEE10_EN;
  2624. }
  2625. ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
  2626. ocp_reg_write(tp, OCP_EEE_CFG, config);
  2627. }
  2628. static void r8153_enable_eee(struct r8152 *tp)
  2629. {
  2630. r8153_eee_en(tp, true);
  2631. ocp_reg_write(tp, OCP_EEE_ADV, MDIO_EEE_1000T | MDIO_EEE_100TX);
  2632. }
  2633. static void r8152b_enable_fc(struct r8152 *tp)
  2634. {
  2635. u16 anar;
  2636. anar = r8152_mdio_read(tp, MII_ADVERTISE);
  2637. anar |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
  2638. r8152_mdio_write(tp, MII_ADVERTISE, anar);
  2639. }
  2640. static void rtl_tally_reset(struct r8152 *tp)
  2641. {
  2642. u32 ocp_data;
  2643. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY);
  2644. ocp_data |= TALLY_RESET;
  2645. ocp_write_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY, ocp_data);
  2646. }
  2647. static void r8152b_init(struct r8152 *tp)
  2648. {
  2649. u32 ocp_data;
  2650. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  2651. return;
  2652. r8152_aldps_en(tp, false);
  2653. if (tp->version == RTL_VER_01) {
  2654. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
  2655. ocp_data &= ~LED_MODE_MASK;
  2656. ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
  2657. }
  2658. r8152_power_cut_en(tp, false);
  2659. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
  2660. ocp_data |= TX_10M_IDLE_EN | PFM_PWM_SWITCH;
  2661. ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
  2662. ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL);
  2663. ocp_data &= ~MCU_CLK_RATIO_MASK;
  2664. ocp_data |= MCU_CLK_RATIO | D3_CLK_GATED_EN;
  2665. ocp_write_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ocp_data);
  2666. ocp_data = GPHY_STS_MSK | SPEED_DOWN_MSK |
  2667. SPDWN_RXDV_MSK | SPDWN_LINKCHG_MSK;
  2668. ocp_write_word(tp, MCU_TYPE_PLA, PLA_GPHY_INTR_IMR, ocp_data);
  2669. r8152b_enable_eee(tp);
  2670. r8152_aldps_en(tp, true);
  2671. r8152b_enable_fc(tp);
  2672. rtl_tally_reset(tp);
  2673. /* enable rx aggregation */
  2674. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
  2675. ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
  2676. ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
  2677. }
  2678. static void r8153_init(struct r8152 *tp)
  2679. {
  2680. u32 ocp_data;
  2681. int i;
  2682. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  2683. return;
  2684. r8153_aldps_en(tp, false);
  2685. r8153_u1u2en(tp, false);
  2686. for (i = 0; i < 500; i++) {
  2687. if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
  2688. AUTOLOAD_DONE)
  2689. break;
  2690. msleep(20);
  2691. }
  2692. for (i = 0; i < 500; i++) {
  2693. ocp_data = ocp_reg_read(tp, OCP_PHY_STATUS) & PHY_STAT_MASK;
  2694. if (ocp_data == PHY_STAT_LAN_ON || ocp_data == PHY_STAT_PWRDN)
  2695. break;
  2696. msleep(20);
  2697. }
  2698. usb_disable_lpm(tp->udev);
  2699. r8153_u2p3en(tp, false);
  2700. if (tp->version == RTL_VER_04) {
  2701. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2);
  2702. ocp_data &= ~pwd_dn_scale_mask;
  2703. ocp_data |= pwd_dn_scale(96);
  2704. ocp_write_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2, ocp_data);
  2705. ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_USB2PHY);
  2706. ocp_data |= USB2PHY_L1 | USB2PHY_SUSPEND;
  2707. ocp_write_byte(tp, MCU_TYPE_USB, USB_USB2PHY, ocp_data);
  2708. } else if (tp->version == RTL_VER_05) {
  2709. ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0);
  2710. ocp_data &= ~ECM_ALDPS;
  2711. ocp_write_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0, ocp_data);
  2712. ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1);
  2713. if (ocp_read_word(tp, MCU_TYPE_USB, USB_BURST_SIZE) == 0)
  2714. ocp_data &= ~DYNAMIC_BURST;
  2715. else
  2716. ocp_data |= DYNAMIC_BURST;
  2717. ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, ocp_data);
  2718. } else if (tp->version == RTL_VER_06) {
  2719. ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1);
  2720. if (ocp_read_word(tp, MCU_TYPE_USB, USB_BURST_SIZE) == 0)
  2721. ocp_data &= ~DYNAMIC_BURST;
  2722. else
  2723. ocp_data |= DYNAMIC_BURST;
  2724. ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, ocp_data);
  2725. }
  2726. ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2);
  2727. ocp_data |= EP4_FULL_FC;
  2728. ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2, ocp_data);
  2729. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL);
  2730. ocp_data &= ~TIMER11_EN;
  2731. ocp_write_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL, ocp_data);
  2732. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
  2733. ocp_data &= ~LED_MODE_MASK;
  2734. ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
  2735. ocp_data = FIFO_EMPTY_1FB | ROK_EXIT_LPM;
  2736. if (tp->version == RTL_VER_04 && tp->udev->speed < USB_SPEED_SUPER)
  2737. ocp_data |= LPM_TIMER_500MS;
  2738. else
  2739. ocp_data |= LPM_TIMER_500US;
  2740. ocp_write_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL, ocp_data);
  2741. ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2);
  2742. ocp_data &= ~SEN_VAL_MASK;
  2743. ocp_data |= SEN_VAL_NORMAL | SEL_RXIDLE;
  2744. ocp_write_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2, ocp_data);
  2745. ocp_write_word(tp, MCU_TYPE_USB, USB_CONNECT_TIMER, 0x0001);
  2746. r8153_power_cut_en(tp, false);
  2747. r8153_u1u2en(tp, true);
  2748. /* MAC clock speed down */
  2749. ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, 0);
  2750. ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, 0);
  2751. ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, 0);
  2752. ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, 0);
  2753. r8153_enable_eee(tp);
  2754. r8153_aldps_en(tp, true);
  2755. r8152b_enable_fc(tp);
  2756. rtl_tally_reset(tp);
  2757. r8153_u2p3en(tp, true);
  2758. }
  2759. static int rtl8152_pre_reset(struct usb_interface *intf)
  2760. {
  2761. struct r8152 *tp = usb_get_intfdata(intf);
  2762. struct net_device *netdev;
  2763. if (!tp)
  2764. return 0;
  2765. netdev = tp->netdev;
  2766. if (!netif_running(netdev))
  2767. return 0;
  2768. napi_disable(&tp->napi);
  2769. clear_bit(WORK_ENABLE, &tp->flags);
  2770. usb_kill_urb(tp->intr_urb);
  2771. cancel_delayed_work_sync(&tp->schedule);
  2772. if (netif_carrier_ok(netdev)) {
  2773. netif_stop_queue(netdev);
  2774. mutex_lock(&tp->control);
  2775. tp->rtl_ops.disable(tp);
  2776. mutex_unlock(&tp->control);
  2777. }
  2778. return 0;
  2779. }
  2780. static int rtl8152_post_reset(struct usb_interface *intf)
  2781. {
  2782. struct r8152 *tp = usb_get_intfdata(intf);
  2783. struct net_device *netdev;
  2784. if (!tp)
  2785. return 0;
  2786. netdev = tp->netdev;
  2787. if (!netif_running(netdev))
  2788. return 0;
  2789. set_bit(WORK_ENABLE, &tp->flags);
  2790. if (netif_carrier_ok(netdev)) {
  2791. mutex_lock(&tp->control);
  2792. tp->rtl_ops.enable(tp);
  2793. rtl8152_set_rx_mode(netdev);
  2794. mutex_unlock(&tp->control);
  2795. netif_wake_queue(netdev);
  2796. }
  2797. napi_enable(&tp->napi);
  2798. return 0;
  2799. }
  2800. static bool delay_autosuspend(struct r8152 *tp)
  2801. {
  2802. bool sw_linking = !!netif_carrier_ok(tp->netdev);
  2803. bool hw_linking = !!(rtl8152_get_speed(tp) & LINK_STATUS);
  2804. /* This means a linking change occurs and the driver doesn't detect it,
  2805. * yet. If the driver has disabled tx/rx and hw is linking on, the
  2806. * device wouldn't wake up by receiving any packet.
  2807. */
  2808. if (work_busy(&tp->schedule.work) || sw_linking != hw_linking)
  2809. return true;
  2810. /* If the linking down is occurred by nway, the device may miss the
  2811. * linking change event. And it wouldn't wake when linking on.
  2812. */
  2813. if (!sw_linking && tp->rtl_ops.in_nway(tp))
  2814. return true;
  2815. else
  2816. return false;
  2817. }
  2818. static int rtl8152_suspend(struct usb_interface *intf, pm_message_t message)
  2819. {
  2820. struct r8152 *tp = usb_get_intfdata(intf);
  2821. struct net_device *netdev = tp->netdev;
  2822. int ret = 0;
  2823. mutex_lock(&tp->control);
  2824. if (PMSG_IS_AUTO(message)) {
  2825. if (netif_running(netdev) && delay_autosuspend(tp)) {
  2826. ret = -EBUSY;
  2827. goto out1;
  2828. }
  2829. set_bit(SELECTIVE_SUSPEND, &tp->flags);
  2830. } else {
  2831. netif_device_detach(netdev);
  2832. }
  2833. if (netif_running(netdev) && test_bit(WORK_ENABLE, &tp->flags)) {
  2834. clear_bit(WORK_ENABLE, &tp->flags);
  2835. usb_kill_urb(tp->intr_urb);
  2836. napi_disable(&tp->napi);
  2837. if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
  2838. rtl_stop_rx(tp);
  2839. tp->rtl_ops.autosuspend_en(tp, true);
  2840. } else {
  2841. cancel_delayed_work_sync(&tp->schedule);
  2842. tp->rtl_ops.down(tp);
  2843. }
  2844. napi_enable(&tp->napi);
  2845. }
  2846. out1:
  2847. mutex_unlock(&tp->control);
  2848. return ret;
  2849. }
  2850. static int rtl8152_resume(struct usb_interface *intf)
  2851. {
  2852. struct r8152 *tp = usb_get_intfdata(intf);
  2853. mutex_lock(&tp->control);
  2854. if (!test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
  2855. tp->rtl_ops.init(tp);
  2856. netif_device_attach(tp->netdev);
  2857. }
  2858. if (netif_running(tp->netdev) && tp->netdev->flags & IFF_UP) {
  2859. if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
  2860. tp->rtl_ops.autosuspend_en(tp, false);
  2861. clear_bit(SELECTIVE_SUSPEND, &tp->flags);
  2862. napi_disable(&tp->napi);
  2863. set_bit(WORK_ENABLE, &tp->flags);
  2864. if (netif_carrier_ok(tp->netdev))
  2865. rtl_start_rx(tp);
  2866. napi_enable(&tp->napi);
  2867. } else {
  2868. tp->rtl_ops.up(tp);
  2869. rtl8152_set_speed(tp, AUTONEG_ENABLE,
  2870. tp->mii.supports_gmii ?
  2871. SPEED_1000 : SPEED_100,
  2872. DUPLEX_FULL);
  2873. netif_carrier_off(tp->netdev);
  2874. set_bit(WORK_ENABLE, &tp->flags);
  2875. }
  2876. usb_submit_urb(tp->intr_urb, GFP_KERNEL);
  2877. } else if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
  2878. if (tp->netdev->flags & IFF_UP)
  2879. tp->rtl_ops.autosuspend_en(tp, false);
  2880. clear_bit(SELECTIVE_SUSPEND, &tp->flags);
  2881. }
  2882. mutex_unlock(&tp->control);
  2883. return 0;
  2884. }
  2885. static int rtl8152_reset_resume(struct usb_interface *intf)
  2886. {
  2887. struct r8152 *tp = usb_get_intfdata(intf);
  2888. clear_bit(SELECTIVE_SUSPEND, &tp->flags);
  2889. return rtl8152_resume(intf);
  2890. }
  2891. static void rtl8152_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  2892. {
  2893. struct r8152 *tp = netdev_priv(dev);
  2894. if (usb_autopm_get_interface(tp->intf) < 0)
  2895. return;
  2896. if (!rtl_can_wakeup(tp)) {
  2897. wol->supported = 0;
  2898. wol->wolopts = 0;
  2899. } else {
  2900. mutex_lock(&tp->control);
  2901. wol->supported = WAKE_ANY;
  2902. wol->wolopts = __rtl_get_wol(tp);
  2903. mutex_unlock(&tp->control);
  2904. }
  2905. usb_autopm_put_interface(tp->intf);
  2906. }
  2907. static int rtl8152_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  2908. {
  2909. struct r8152 *tp = netdev_priv(dev);
  2910. int ret;
  2911. if (!rtl_can_wakeup(tp))
  2912. return -EOPNOTSUPP;
  2913. ret = usb_autopm_get_interface(tp->intf);
  2914. if (ret < 0)
  2915. goto out_set_wol;
  2916. mutex_lock(&tp->control);
  2917. __rtl_set_wol(tp, wol->wolopts);
  2918. tp->saved_wolopts = wol->wolopts & WAKE_ANY;
  2919. mutex_unlock(&tp->control);
  2920. usb_autopm_put_interface(tp->intf);
  2921. out_set_wol:
  2922. return ret;
  2923. }
  2924. static u32 rtl8152_get_msglevel(struct net_device *dev)
  2925. {
  2926. struct r8152 *tp = netdev_priv(dev);
  2927. return tp->msg_enable;
  2928. }
  2929. static void rtl8152_set_msglevel(struct net_device *dev, u32 value)
  2930. {
  2931. struct r8152 *tp = netdev_priv(dev);
  2932. tp->msg_enable = value;
  2933. }
  2934. static void rtl8152_get_drvinfo(struct net_device *netdev,
  2935. struct ethtool_drvinfo *info)
  2936. {
  2937. struct r8152 *tp = netdev_priv(netdev);
  2938. strlcpy(info->driver, MODULENAME, sizeof(info->driver));
  2939. strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
  2940. usb_make_path(tp->udev, info->bus_info, sizeof(info->bus_info));
  2941. }
  2942. static
  2943. int rtl8152_get_settings(struct net_device *netdev, struct ethtool_cmd *cmd)
  2944. {
  2945. struct r8152 *tp = netdev_priv(netdev);
  2946. int ret;
  2947. if (!tp->mii.mdio_read)
  2948. return -EOPNOTSUPP;
  2949. ret = usb_autopm_get_interface(tp->intf);
  2950. if (ret < 0)
  2951. goto out;
  2952. mutex_lock(&tp->control);
  2953. ret = mii_ethtool_gset(&tp->mii, cmd);
  2954. mutex_unlock(&tp->control);
  2955. usb_autopm_put_interface(tp->intf);
  2956. out:
  2957. return ret;
  2958. }
  2959. static int rtl8152_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  2960. {
  2961. struct r8152 *tp = netdev_priv(dev);
  2962. int ret;
  2963. ret = usb_autopm_get_interface(tp->intf);
  2964. if (ret < 0)
  2965. goto out;
  2966. mutex_lock(&tp->control);
  2967. ret = rtl8152_set_speed(tp, cmd->autoneg, cmd->speed, cmd->duplex);
  2968. mutex_unlock(&tp->control);
  2969. usb_autopm_put_interface(tp->intf);
  2970. out:
  2971. return ret;
  2972. }
  2973. static const char rtl8152_gstrings[][ETH_GSTRING_LEN] = {
  2974. "tx_packets",
  2975. "rx_packets",
  2976. "tx_errors",
  2977. "rx_errors",
  2978. "rx_missed",
  2979. "align_errors",
  2980. "tx_single_collisions",
  2981. "tx_multi_collisions",
  2982. "rx_unicast",
  2983. "rx_broadcast",
  2984. "rx_multicast",
  2985. "tx_aborted",
  2986. "tx_underrun",
  2987. };
  2988. static int rtl8152_get_sset_count(struct net_device *dev, int sset)
  2989. {
  2990. switch (sset) {
  2991. case ETH_SS_STATS:
  2992. return ARRAY_SIZE(rtl8152_gstrings);
  2993. default:
  2994. return -EOPNOTSUPP;
  2995. }
  2996. }
  2997. static void rtl8152_get_ethtool_stats(struct net_device *dev,
  2998. struct ethtool_stats *stats, u64 *data)
  2999. {
  3000. struct r8152 *tp = netdev_priv(dev);
  3001. struct tally_counter tally;
  3002. if (usb_autopm_get_interface(tp->intf) < 0)
  3003. return;
  3004. generic_ocp_read(tp, PLA_TALLYCNT, sizeof(tally), &tally, MCU_TYPE_PLA);
  3005. usb_autopm_put_interface(tp->intf);
  3006. data[0] = le64_to_cpu(tally.tx_packets);
  3007. data[1] = le64_to_cpu(tally.rx_packets);
  3008. data[2] = le64_to_cpu(tally.tx_errors);
  3009. data[3] = le32_to_cpu(tally.rx_errors);
  3010. data[4] = le16_to_cpu(tally.rx_missed);
  3011. data[5] = le16_to_cpu(tally.align_errors);
  3012. data[6] = le32_to_cpu(tally.tx_one_collision);
  3013. data[7] = le32_to_cpu(tally.tx_multi_collision);
  3014. data[8] = le64_to_cpu(tally.rx_unicast);
  3015. data[9] = le64_to_cpu(tally.rx_broadcast);
  3016. data[10] = le32_to_cpu(tally.rx_multicast);
  3017. data[11] = le16_to_cpu(tally.tx_aborted);
  3018. data[12] = le16_to_cpu(tally.tx_underrun);
  3019. }
  3020. static void rtl8152_get_strings(struct net_device *dev, u32 stringset, u8 *data)
  3021. {
  3022. switch (stringset) {
  3023. case ETH_SS_STATS:
  3024. memcpy(data, *rtl8152_gstrings, sizeof(rtl8152_gstrings));
  3025. break;
  3026. }
  3027. }
  3028. static int r8152_get_eee(struct r8152 *tp, struct ethtool_eee *eee)
  3029. {
  3030. u32 ocp_data, lp, adv, supported = 0;
  3031. u16 val;
  3032. val = r8152_mmd_read(tp, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
  3033. supported = mmd_eee_cap_to_ethtool_sup_t(val);
  3034. val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
  3035. adv = mmd_eee_adv_to_ethtool_adv_t(val);
  3036. val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
  3037. lp = mmd_eee_adv_to_ethtool_adv_t(val);
  3038. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
  3039. ocp_data &= EEE_RX_EN | EEE_TX_EN;
  3040. eee->eee_enabled = !!ocp_data;
  3041. eee->eee_active = !!(supported & adv & lp);
  3042. eee->supported = supported;
  3043. eee->advertised = adv;
  3044. eee->lp_advertised = lp;
  3045. return 0;
  3046. }
  3047. static int r8152_set_eee(struct r8152 *tp, struct ethtool_eee *eee)
  3048. {
  3049. u16 val = ethtool_adv_to_mmd_eee_adv_t(eee->advertised);
  3050. r8152_eee_en(tp, eee->eee_enabled);
  3051. if (!eee->eee_enabled)
  3052. val = 0;
  3053. r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
  3054. return 0;
  3055. }
  3056. static int r8153_get_eee(struct r8152 *tp, struct ethtool_eee *eee)
  3057. {
  3058. u32 ocp_data, lp, adv, supported = 0;
  3059. u16 val;
  3060. val = ocp_reg_read(tp, OCP_EEE_ABLE);
  3061. supported = mmd_eee_cap_to_ethtool_sup_t(val);
  3062. val = ocp_reg_read(tp, OCP_EEE_ADV);
  3063. adv = mmd_eee_adv_to_ethtool_adv_t(val);
  3064. val = ocp_reg_read(tp, OCP_EEE_LPABLE);
  3065. lp = mmd_eee_adv_to_ethtool_adv_t(val);
  3066. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
  3067. ocp_data &= EEE_RX_EN | EEE_TX_EN;
  3068. eee->eee_enabled = !!ocp_data;
  3069. eee->eee_active = !!(supported & adv & lp);
  3070. eee->supported = supported;
  3071. eee->advertised = adv;
  3072. eee->lp_advertised = lp;
  3073. return 0;
  3074. }
  3075. static int r8153_set_eee(struct r8152 *tp, struct ethtool_eee *eee)
  3076. {
  3077. u16 val = ethtool_adv_to_mmd_eee_adv_t(eee->advertised);
  3078. r8153_eee_en(tp, eee->eee_enabled);
  3079. if (!eee->eee_enabled)
  3080. val = 0;
  3081. ocp_reg_write(tp, OCP_EEE_ADV, val);
  3082. return 0;
  3083. }
  3084. static int
  3085. rtl_ethtool_get_eee(struct net_device *net, struct ethtool_eee *edata)
  3086. {
  3087. struct r8152 *tp = netdev_priv(net);
  3088. int ret;
  3089. ret = usb_autopm_get_interface(tp->intf);
  3090. if (ret < 0)
  3091. goto out;
  3092. mutex_lock(&tp->control);
  3093. ret = tp->rtl_ops.eee_get(tp, edata);
  3094. mutex_unlock(&tp->control);
  3095. usb_autopm_put_interface(tp->intf);
  3096. out:
  3097. return ret;
  3098. }
  3099. static int
  3100. rtl_ethtool_set_eee(struct net_device *net, struct ethtool_eee *edata)
  3101. {
  3102. struct r8152 *tp = netdev_priv(net);
  3103. int ret;
  3104. ret = usb_autopm_get_interface(tp->intf);
  3105. if (ret < 0)
  3106. goto out;
  3107. mutex_lock(&tp->control);
  3108. ret = tp->rtl_ops.eee_set(tp, edata);
  3109. if (!ret)
  3110. ret = mii_nway_restart(&tp->mii);
  3111. mutex_unlock(&tp->control);
  3112. usb_autopm_put_interface(tp->intf);
  3113. out:
  3114. return ret;
  3115. }
  3116. static int rtl8152_nway_reset(struct net_device *dev)
  3117. {
  3118. struct r8152 *tp = netdev_priv(dev);
  3119. int ret;
  3120. ret = usb_autopm_get_interface(tp->intf);
  3121. if (ret < 0)
  3122. goto out;
  3123. mutex_lock(&tp->control);
  3124. ret = mii_nway_restart(&tp->mii);
  3125. mutex_unlock(&tp->control);
  3126. usb_autopm_put_interface(tp->intf);
  3127. out:
  3128. return ret;
  3129. }
  3130. static int rtl8152_get_coalesce(struct net_device *netdev,
  3131. struct ethtool_coalesce *coalesce)
  3132. {
  3133. struct r8152 *tp = netdev_priv(netdev);
  3134. switch (tp->version) {
  3135. case RTL_VER_01:
  3136. case RTL_VER_02:
  3137. return -EOPNOTSUPP;
  3138. default:
  3139. break;
  3140. }
  3141. coalesce->rx_coalesce_usecs = tp->coalesce;
  3142. return 0;
  3143. }
  3144. static int rtl8152_set_coalesce(struct net_device *netdev,
  3145. struct ethtool_coalesce *coalesce)
  3146. {
  3147. struct r8152 *tp = netdev_priv(netdev);
  3148. int ret;
  3149. switch (tp->version) {
  3150. case RTL_VER_01:
  3151. case RTL_VER_02:
  3152. return -EOPNOTSUPP;
  3153. default:
  3154. break;
  3155. }
  3156. if (coalesce->rx_coalesce_usecs > COALESCE_SLOW)
  3157. return -EINVAL;
  3158. ret = usb_autopm_get_interface(tp->intf);
  3159. if (ret < 0)
  3160. return ret;
  3161. mutex_lock(&tp->control);
  3162. if (tp->coalesce != coalesce->rx_coalesce_usecs) {
  3163. tp->coalesce = coalesce->rx_coalesce_usecs;
  3164. if (netif_running(tp->netdev) && netif_carrier_ok(netdev))
  3165. r8153_set_rx_early_timeout(tp);
  3166. }
  3167. mutex_unlock(&tp->control);
  3168. usb_autopm_put_interface(tp->intf);
  3169. return ret;
  3170. }
  3171. static struct ethtool_ops ops = {
  3172. .get_drvinfo = rtl8152_get_drvinfo,
  3173. .get_settings = rtl8152_get_settings,
  3174. .set_settings = rtl8152_set_settings,
  3175. .get_link = ethtool_op_get_link,
  3176. .nway_reset = rtl8152_nway_reset,
  3177. .get_msglevel = rtl8152_get_msglevel,
  3178. .set_msglevel = rtl8152_set_msglevel,
  3179. .get_wol = rtl8152_get_wol,
  3180. .set_wol = rtl8152_set_wol,
  3181. .get_strings = rtl8152_get_strings,
  3182. .get_sset_count = rtl8152_get_sset_count,
  3183. .get_ethtool_stats = rtl8152_get_ethtool_stats,
  3184. .get_coalesce = rtl8152_get_coalesce,
  3185. .set_coalesce = rtl8152_set_coalesce,
  3186. .get_eee = rtl_ethtool_get_eee,
  3187. .set_eee = rtl_ethtool_set_eee,
  3188. };
  3189. static int rtl8152_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
  3190. {
  3191. struct r8152 *tp = netdev_priv(netdev);
  3192. struct mii_ioctl_data *data = if_mii(rq);
  3193. int res;
  3194. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  3195. return -ENODEV;
  3196. res = usb_autopm_get_interface(tp->intf);
  3197. if (res < 0)
  3198. goto out;
  3199. switch (cmd) {
  3200. case SIOCGMIIPHY:
  3201. data->phy_id = R8152_PHY_ID; /* Internal PHY */
  3202. break;
  3203. case SIOCGMIIREG:
  3204. mutex_lock(&tp->control);
  3205. data->val_out = r8152_mdio_read(tp, data->reg_num);
  3206. mutex_unlock(&tp->control);
  3207. break;
  3208. case SIOCSMIIREG:
  3209. if (!capable(CAP_NET_ADMIN)) {
  3210. res = -EPERM;
  3211. break;
  3212. }
  3213. mutex_lock(&tp->control);
  3214. r8152_mdio_write(tp, data->reg_num, data->val_in);
  3215. mutex_unlock(&tp->control);
  3216. break;
  3217. default:
  3218. res = -EOPNOTSUPP;
  3219. }
  3220. usb_autopm_put_interface(tp->intf);
  3221. out:
  3222. return res;
  3223. }
  3224. static int rtl8152_change_mtu(struct net_device *dev, int new_mtu)
  3225. {
  3226. struct r8152 *tp = netdev_priv(dev);
  3227. int ret;
  3228. switch (tp->version) {
  3229. case RTL_VER_01:
  3230. case RTL_VER_02:
  3231. return eth_change_mtu(dev, new_mtu);
  3232. default:
  3233. break;
  3234. }
  3235. if (new_mtu < 68 || new_mtu > RTL8153_MAX_MTU)
  3236. return -EINVAL;
  3237. ret = usb_autopm_get_interface(tp->intf);
  3238. if (ret < 0)
  3239. return ret;
  3240. mutex_lock(&tp->control);
  3241. dev->mtu = new_mtu;
  3242. if (netif_running(dev) && netif_carrier_ok(dev))
  3243. r8153_set_rx_early_size(tp);
  3244. mutex_unlock(&tp->control);
  3245. usb_autopm_put_interface(tp->intf);
  3246. return ret;
  3247. }
  3248. static const struct net_device_ops rtl8152_netdev_ops = {
  3249. .ndo_open = rtl8152_open,
  3250. .ndo_stop = rtl8152_close,
  3251. .ndo_do_ioctl = rtl8152_ioctl,
  3252. .ndo_start_xmit = rtl8152_start_xmit,
  3253. .ndo_tx_timeout = rtl8152_tx_timeout,
  3254. .ndo_set_features = rtl8152_set_features,
  3255. .ndo_set_rx_mode = rtl8152_set_rx_mode,
  3256. .ndo_set_mac_address = rtl8152_set_mac_address,
  3257. .ndo_change_mtu = rtl8152_change_mtu,
  3258. .ndo_validate_addr = eth_validate_addr,
  3259. .ndo_features_check = rtl8152_features_check,
  3260. };
  3261. static void r8152b_get_version(struct r8152 *tp)
  3262. {
  3263. u32 ocp_data;
  3264. u16 version;
  3265. ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR1);
  3266. version = (u16)(ocp_data & VERSION_MASK);
  3267. switch (version) {
  3268. case 0x4c00:
  3269. tp->version = RTL_VER_01;
  3270. break;
  3271. case 0x4c10:
  3272. tp->version = RTL_VER_02;
  3273. break;
  3274. case 0x5c00:
  3275. tp->version = RTL_VER_03;
  3276. tp->mii.supports_gmii = 1;
  3277. break;
  3278. case 0x5c10:
  3279. tp->version = RTL_VER_04;
  3280. tp->mii.supports_gmii = 1;
  3281. break;
  3282. case 0x5c20:
  3283. tp->version = RTL_VER_05;
  3284. tp->mii.supports_gmii = 1;
  3285. break;
  3286. case 0x5c30:
  3287. tp->version = RTL_VER_06;
  3288. tp->mii.supports_gmii = 1;
  3289. break;
  3290. default:
  3291. netif_info(tp, probe, tp->netdev,
  3292. "Unknown version 0x%04x\n", version);
  3293. break;
  3294. }
  3295. }
  3296. static void rtl8152_unload(struct r8152 *tp)
  3297. {
  3298. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  3299. return;
  3300. if (tp->version != RTL_VER_01)
  3301. r8152_power_cut_en(tp, true);
  3302. }
  3303. static void rtl8153_unload(struct r8152 *tp)
  3304. {
  3305. if (test_bit(RTL8152_UNPLUG, &tp->flags))
  3306. return;
  3307. r8153_power_cut_en(tp, false);
  3308. }
  3309. static int rtl_ops_init(struct r8152 *tp)
  3310. {
  3311. struct rtl_ops *ops = &tp->rtl_ops;
  3312. int ret = 0;
  3313. switch (tp->version) {
  3314. case RTL_VER_01:
  3315. case RTL_VER_02:
  3316. ops->init = r8152b_init;
  3317. ops->enable = rtl8152_enable;
  3318. ops->disable = rtl8152_disable;
  3319. ops->up = rtl8152_up;
  3320. ops->down = rtl8152_down;
  3321. ops->unload = rtl8152_unload;
  3322. ops->eee_get = r8152_get_eee;
  3323. ops->eee_set = r8152_set_eee;
  3324. ops->in_nway = rtl8152_in_nway;
  3325. ops->autosuspend_en = rtl_runtime_suspend_enable;
  3326. break;
  3327. case RTL_VER_03:
  3328. case RTL_VER_04:
  3329. case RTL_VER_05:
  3330. case RTL_VER_06:
  3331. ops->init = r8153_init;
  3332. ops->enable = rtl8153_enable;
  3333. ops->disable = rtl8153_disable;
  3334. ops->up = rtl8153_up;
  3335. ops->down = rtl8153_down;
  3336. ops->unload = rtl8153_unload;
  3337. ops->eee_get = r8153_get_eee;
  3338. ops->eee_set = r8153_set_eee;
  3339. ops->in_nway = rtl8153_in_nway;
  3340. ops->autosuspend_en = rtl8153_runtime_enable;
  3341. break;
  3342. default:
  3343. ret = -ENODEV;
  3344. netif_err(tp, probe, tp->netdev, "Unknown Device\n");
  3345. break;
  3346. }
  3347. return ret;
  3348. }
  3349. static int rtl8152_probe(struct usb_interface *intf,
  3350. const struct usb_device_id *id)
  3351. {
  3352. struct usb_device *udev = interface_to_usbdev(intf);
  3353. struct r8152 *tp;
  3354. struct net_device *netdev;
  3355. int ret;
  3356. if (udev->actconfig->desc.bConfigurationValue != 1) {
  3357. usb_driver_set_configuration(udev, 1);
  3358. return -ENODEV;
  3359. }
  3360. usb_reset_device(udev);
  3361. netdev = alloc_etherdev(sizeof(struct r8152));
  3362. if (!netdev) {
  3363. dev_err(&intf->dev, "Out of memory\n");
  3364. return -ENOMEM;
  3365. }
  3366. SET_NETDEV_DEV(netdev, &intf->dev);
  3367. tp = netdev_priv(netdev);
  3368. tp->msg_enable = 0x7FFF;
  3369. tp->udev = udev;
  3370. tp->netdev = netdev;
  3371. tp->intf = intf;
  3372. r8152b_get_version(tp);
  3373. ret = rtl_ops_init(tp);
  3374. if (ret)
  3375. goto out;
  3376. mutex_init(&tp->control);
  3377. INIT_DELAYED_WORK(&tp->schedule, rtl_work_func_t);
  3378. netdev->netdev_ops = &rtl8152_netdev_ops;
  3379. netdev->watchdog_timeo = RTL8152_TX_TIMEOUT;
  3380. netdev->features |= NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG |
  3381. NETIF_F_TSO | NETIF_F_FRAGLIST | NETIF_F_IPV6_CSUM |
  3382. NETIF_F_TSO6 | NETIF_F_HW_VLAN_CTAG_RX |
  3383. NETIF_F_HW_VLAN_CTAG_TX;
  3384. netdev->hw_features = NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG |
  3385. NETIF_F_TSO | NETIF_F_FRAGLIST |
  3386. NETIF_F_IPV6_CSUM | NETIF_F_TSO6 |
  3387. NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX;
  3388. netdev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
  3389. NETIF_F_HIGHDMA | NETIF_F_FRAGLIST |
  3390. NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
  3391. netdev->ethtool_ops = &ops;
  3392. netif_set_gso_max_size(netdev, RTL_LIMITED_TSO_SIZE);
  3393. tp->mii.dev = netdev;
  3394. tp->mii.mdio_read = read_mii_word;
  3395. tp->mii.mdio_write = write_mii_word;
  3396. tp->mii.phy_id_mask = 0x3f;
  3397. tp->mii.reg_num_mask = 0x1f;
  3398. tp->mii.phy_id = R8152_PHY_ID;
  3399. switch (udev->speed) {
  3400. case USB_SPEED_SUPER:
  3401. case USB_SPEED_SUPER_PLUS:
  3402. tp->coalesce = COALESCE_SUPER;
  3403. break;
  3404. case USB_SPEED_HIGH:
  3405. tp->coalesce = COALESCE_HIGH;
  3406. break;
  3407. default:
  3408. tp->coalesce = COALESCE_SLOW;
  3409. break;
  3410. }
  3411. intf->needs_remote_wakeup = 1;
  3412. tp->rtl_ops.init(tp);
  3413. set_ethernet_addr(tp);
  3414. usb_set_intfdata(intf, tp);
  3415. netif_napi_add(netdev, &tp->napi, r8152_poll, RTL8152_NAPI_WEIGHT);
  3416. ret = register_netdev(netdev);
  3417. if (ret != 0) {
  3418. netif_err(tp, probe, netdev, "couldn't register the device\n");
  3419. goto out1;
  3420. }
  3421. if (!rtl_can_wakeup(tp))
  3422. __rtl_set_wol(tp, 0);
  3423. tp->saved_wolopts = __rtl_get_wol(tp);
  3424. if (tp->saved_wolopts)
  3425. device_set_wakeup_enable(&udev->dev, true);
  3426. else
  3427. device_set_wakeup_enable(&udev->dev, false);
  3428. netif_info(tp, probe, netdev, "%s\n", DRIVER_VERSION);
  3429. return 0;
  3430. out1:
  3431. netif_napi_del(&tp->napi);
  3432. usb_set_intfdata(intf, NULL);
  3433. out:
  3434. free_netdev(netdev);
  3435. return ret;
  3436. }
  3437. static void rtl8152_disconnect(struct usb_interface *intf)
  3438. {
  3439. struct r8152 *tp = usb_get_intfdata(intf);
  3440. usb_set_intfdata(intf, NULL);
  3441. if (tp) {
  3442. struct usb_device *udev = tp->udev;
  3443. if (udev->state == USB_STATE_NOTATTACHED)
  3444. set_bit(RTL8152_UNPLUG, &tp->flags);
  3445. netif_napi_del(&tp->napi);
  3446. unregister_netdev(tp->netdev);
  3447. tp->rtl_ops.unload(tp);
  3448. free_netdev(tp->netdev);
  3449. }
  3450. }
  3451. #define REALTEK_USB_DEVICE(vend, prod) \
  3452. .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \
  3453. USB_DEVICE_ID_MATCH_INT_CLASS, \
  3454. .idVendor = (vend), \
  3455. .idProduct = (prod), \
  3456. .bInterfaceClass = USB_CLASS_VENDOR_SPEC \
  3457. }, \
  3458. { \
  3459. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO | \
  3460. USB_DEVICE_ID_MATCH_DEVICE, \
  3461. .idVendor = (vend), \
  3462. .idProduct = (prod), \
  3463. .bInterfaceClass = USB_CLASS_COMM, \
  3464. .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET, \
  3465. .bInterfaceProtocol = USB_CDC_PROTO_NONE
  3466. /* table of devices that work with this driver */
  3467. static struct usb_device_id rtl8152_table[] = {
  3468. {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8152)},
  3469. {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8153)},
  3470. {REALTEK_USB_DEVICE(VENDOR_ID_SAMSUNG, 0xa101)},
  3471. {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x7205)},
  3472. {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x304f)},
  3473. {REALTEK_USB_DEVICE(VENDOR_ID_NVIDIA, 0x09ff)},
  3474. {}
  3475. };
  3476. MODULE_DEVICE_TABLE(usb, rtl8152_table);
  3477. static struct usb_driver rtl8152_driver = {
  3478. .name = MODULENAME,
  3479. .id_table = rtl8152_table,
  3480. .probe = rtl8152_probe,
  3481. .disconnect = rtl8152_disconnect,
  3482. .suspend = rtl8152_suspend,
  3483. .resume = rtl8152_resume,
  3484. .reset_resume = rtl8152_reset_resume,
  3485. .pre_reset = rtl8152_pre_reset,
  3486. .post_reset = rtl8152_post_reset,
  3487. .supports_autosuspend = 1,
  3488. .disable_hub_initiated_lpm = 1,
  3489. };
  3490. module_usb_driver(rtl8152_driver);
  3491. MODULE_AUTHOR(DRIVER_AUTHOR);
  3492. MODULE_DESCRIPTION(DRIVER_DESC);
  3493. MODULE_LICENSE("GPL");