r8152.c 122 KB

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