r8152.c 88 KB

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