r8152.c 101 KB

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