r8152.c 123 KB

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