r8152.c 122 KB

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