r8152.c 87 KB

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