r8152.c 104 KB

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