r8152.c 98 KB

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