r8152.c 122 KB

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