r8152.c 103 KB

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