r8152.c 89 KB

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