r8152.c 123 KB

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