r8152.c 122 KB

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