r8169.c 131 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488
  1. /*
  2. * r8169.c: RealTek 8169/8168/8101 ethernet driver.
  3. *
  4. * Copyright (c) 2002 ShuChen <shuchen@realtek.com.tw>
  5. * Copyright (c) 2003 - 2007 Francois Romieu <romieu@fr.zoreil.com>
  6. * Copyright (c) a lot of people too. Please respect their work.
  7. *
  8. * See MAINTAINERS file for support contact information.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/moduleparam.h>
  12. #include <linux/pci.h>
  13. #include <linux/netdevice.h>
  14. #include <linux/etherdevice.h>
  15. #include <linux/delay.h>
  16. #include <linux/ethtool.h>
  17. #include <linux/mii.h>
  18. #include <linux/if_vlan.h>
  19. #include <linux/crc32.h>
  20. #include <linux/in.h>
  21. #include <linux/ip.h>
  22. #include <linux/tcp.h>
  23. #include <linux/init.h>
  24. #include <linux/dma-mapping.h>
  25. #include <linux/pm_runtime.h>
  26. #include <linux/firmware.h>
  27. #include <linux/pci-aspm.h>
  28. #include <asm/system.h>
  29. #include <asm/io.h>
  30. #include <asm/irq.h>
  31. #define RTL8169_VERSION "2.3LK-NAPI"
  32. #define MODULENAME "r8169"
  33. #define PFX MODULENAME ": "
  34. #define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
  35. #define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
  36. #define FIRMWARE_8168E_1 "rtl_nic/rtl8168e-1.fw"
  37. #define FIRMWARE_8168E_2 "rtl_nic/rtl8168e-2.fw"
  38. #define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
  39. #ifdef RTL8169_DEBUG
  40. #define assert(expr) \
  41. if (!(expr)) { \
  42. printk( "Assertion failed! %s,%s,%s,line=%d\n", \
  43. #expr,__FILE__,__func__,__LINE__); \
  44. }
  45. #define dprintk(fmt, args...) \
  46. do { printk(KERN_DEBUG PFX fmt, ## args); } while (0)
  47. #else
  48. #define assert(expr) do {} while (0)
  49. #define dprintk(fmt, args...) do {} while (0)
  50. #endif /* RTL8169_DEBUG */
  51. #define R8169_MSG_DEFAULT \
  52. (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
  53. #define TX_BUFFS_AVAIL(tp) \
  54. (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx - 1)
  55. /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
  56. The RTL chips use a 64 element hash table based on the Ethernet CRC. */
  57. static const int multicast_filter_limit = 32;
  58. /* MAC address length */
  59. #define MAC_ADDR_LEN 6
  60. #define MAX_READ_REQUEST_SHIFT 12
  61. #define RX_FIFO_THRESH 7 /* 7 means NO threshold, Rx buffer level before first PCI xfer. */
  62. #define RX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
  63. #define TX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
  64. #define SafeMtu 0x1c20 /* ... actually life sucks beyond ~7k */
  65. #define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
  66. #define R8169_REGS_SIZE 256
  67. #define R8169_NAPI_WEIGHT 64
  68. #define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
  69. #define NUM_RX_DESC 256 /* Number of Rx descriptor registers */
  70. #define RX_BUF_SIZE 1536 /* Rx Buffer size */
  71. #define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
  72. #define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
  73. #define RTL8169_TX_TIMEOUT (6*HZ)
  74. #define RTL8169_PHY_TIMEOUT (10*HZ)
  75. #define RTL_EEPROM_SIG cpu_to_le32(0x8129)
  76. #define RTL_EEPROM_SIG_MASK cpu_to_le32(0xffff)
  77. #define RTL_EEPROM_SIG_ADDR 0x0000
  78. /* write/read MMIO register */
  79. #define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
  80. #define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
  81. #define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
  82. #define RTL_R8(reg) readb (ioaddr + (reg))
  83. #define RTL_R16(reg) readw (ioaddr + (reg))
  84. #define RTL_R32(reg) readl (ioaddr + (reg))
  85. enum mac_version {
  86. RTL_GIGA_MAC_NONE = 0x00,
  87. RTL_GIGA_MAC_VER_01 = 0x01, // 8169
  88. RTL_GIGA_MAC_VER_02 = 0x02, // 8169S
  89. RTL_GIGA_MAC_VER_03 = 0x03, // 8110S
  90. RTL_GIGA_MAC_VER_04 = 0x04, // 8169SB
  91. RTL_GIGA_MAC_VER_05 = 0x05, // 8110SCd
  92. RTL_GIGA_MAC_VER_06 = 0x06, // 8110SCe
  93. RTL_GIGA_MAC_VER_07 = 0x07, // 8102e
  94. RTL_GIGA_MAC_VER_08 = 0x08, // 8102e
  95. RTL_GIGA_MAC_VER_09 = 0x09, // 8102e
  96. RTL_GIGA_MAC_VER_10 = 0x0a, // 8101e
  97. RTL_GIGA_MAC_VER_11 = 0x0b, // 8168Bb
  98. RTL_GIGA_MAC_VER_12 = 0x0c, // 8168Be
  99. RTL_GIGA_MAC_VER_13 = 0x0d, // 8101Eb
  100. RTL_GIGA_MAC_VER_14 = 0x0e, // 8101 ?
  101. RTL_GIGA_MAC_VER_15 = 0x0f, // 8101 ?
  102. RTL_GIGA_MAC_VER_16 = 0x11, // 8101Ec
  103. RTL_GIGA_MAC_VER_17 = 0x10, // 8168Bf
  104. RTL_GIGA_MAC_VER_18 = 0x12, // 8168CP
  105. RTL_GIGA_MAC_VER_19 = 0x13, // 8168C
  106. RTL_GIGA_MAC_VER_20 = 0x14, // 8168C
  107. RTL_GIGA_MAC_VER_21 = 0x15, // 8168C
  108. RTL_GIGA_MAC_VER_22 = 0x16, // 8168C
  109. RTL_GIGA_MAC_VER_23 = 0x17, // 8168CP
  110. RTL_GIGA_MAC_VER_24 = 0x18, // 8168CP
  111. RTL_GIGA_MAC_VER_25 = 0x19, // 8168D
  112. RTL_GIGA_MAC_VER_26 = 0x1a, // 8168D
  113. RTL_GIGA_MAC_VER_27 = 0x1b, // 8168DP
  114. RTL_GIGA_MAC_VER_28 = 0x1c, // 8168DP
  115. RTL_GIGA_MAC_VER_29 = 0x1d, // 8105E
  116. RTL_GIGA_MAC_VER_30 = 0x1e, // 8105E
  117. RTL_GIGA_MAC_VER_31 = 0x1f, // 8168DP
  118. RTL_GIGA_MAC_VER_32 = 0x20, // 8168E
  119. RTL_GIGA_MAC_VER_33 = 0x21, // 8168E
  120. };
  121. enum rtl_tx_desc_version {
  122. RTL_TD_0 = 0,
  123. RTL_TD_1 = 1,
  124. };
  125. #define _R(NAME,MAC,TD) \
  126. { .name = NAME, .mac_version = MAC, .txd_version = TD }
  127. static const struct {
  128. const char *name;
  129. u8 mac_version;
  130. enum rtl_tx_desc_version txd_version;
  131. } rtl_chip_info[] = {
  132. _R("RTL8169", RTL_GIGA_MAC_VER_01, RTL_TD_0), // 8169
  133. _R("RTL8169s", RTL_GIGA_MAC_VER_02, RTL_TD_0), // 8169S
  134. _R("RTL8110s", RTL_GIGA_MAC_VER_03, RTL_TD_0), // 8110S
  135. _R("RTL8169sb/8110sb", RTL_GIGA_MAC_VER_04, RTL_TD_0), // 8169SB
  136. _R("RTL8169sc/8110sc", RTL_GIGA_MAC_VER_05, RTL_TD_0), // 8110SCd
  137. _R("RTL8169sc/8110sc", RTL_GIGA_MAC_VER_06, RTL_TD_0), // 8110SCe
  138. _R("RTL8102e", RTL_GIGA_MAC_VER_07, RTL_TD_1), // PCI-E
  139. _R("RTL8102e", RTL_GIGA_MAC_VER_08, RTL_TD_1), // PCI-E
  140. _R("RTL8102e", RTL_GIGA_MAC_VER_09, RTL_TD_1), // PCI-E
  141. _R("RTL8101e", RTL_GIGA_MAC_VER_10, RTL_TD_0), // PCI-E
  142. _R("RTL8168b/8111b", RTL_GIGA_MAC_VER_11, RTL_TD_0), // PCI-E
  143. _R("RTL8168b/8111b", RTL_GIGA_MAC_VER_12, RTL_TD_0), // PCI-E
  144. _R("RTL8101e", RTL_GIGA_MAC_VER_13, RTL_TD_0), // PCI-E 8139
  145. _R("RTL8100e", RTL_GIGA_MAC_VER_14, RTL_TD_0), // PCI-E 8139
  146. _R("RTL8100e", RTL_GIGA_MAC_VER_15, RTL_TD_0), // PCI-E 8139
  147. _R("RTL8168b/8111b", RTL_GIGA_MAC_VER_17, RTL_TD_0), // PCI-E
  148. _R("RTL8101e", RTL_GIGA_MAC_VER_16, RTL_TD_0), // PCI-E
  149. _R("RTL8168cp/8111cp", RTL_GIGA_MAC_VER_18, RTL_TD_1), // PCI-E
  150. _R("RTL8168c/8111c", RTL_GIGA_MAC_VER_19, RTL_TD_1), // PCI-E
  151. _R("RTL8168c/8111c", RTL_GIGA_MAC_VER_20, RTL_TD_1), // PCI-E
  152. _R("RTL8168c/8111c", RTL_GIGA_MAC_VER_21, RTL_TD_1), // PCI-E
  153. _R("RTL8168c/8111c", RTL_GIGA_MAC_VER_22, RTL_TD_1), // PCI-E
  154. _R("RTL8168cp/8111cp", RTL_GIGA_MAC_VER_23, RTL_TD_1), // PCI-E
  155. _R("RTL8168cp/8111cp", RTL_GIGA_MAC_VER_24, RTL_TD_1), // PCI-E
  156. _R("RTL8168d/8111d", RTL_GIGA_MAC_VER_25, RTL_TD_1), // PCI-E
  157. _R("RTL8168d/8111d", RTL_GIGA_MAC_VER_26, RTL_TD_1), // PCI-E
  158. _R("RTL8168dp/8111dp", RTL_GIGA_MAC_VER_27, RTL_TD_1), // PCI-E
  159. _R("RTL8168dp/8111dp", RTL_GIGA_MAC_VER_28, RTL_TD_1), // PCI-E
  160. _R("RTL8105e", RTL_GIGA_MAC_VER_29, RTL_TD_1), // PCI-E
  161. _R("RTL8105e", RTL_GIGA_MAC_VER_30, RTL_TD_1), // PCI-E
  162. _R("RTL8168dp/8111dp", RTL_GIGA_MAC_VER_31, RTL_TD_1), // PCI-E
  163. _R("RTL8168e/8111e", RTL_GIGA_MAC_VER_32, RTL_TD_1), // PCI-E
  164. _R("RTL8168e/8111e", RTL_GIGA_MAC_VER_33, RTL_TD_1) // PCI-E
  165. };
  166. #undef _R
  167. static const struct rtl_firmware_info {
  168. int mac_version;
  169. const char *fw_name;
  170. } rtl_firmware_infos[] = {
  171. { .mac_version = RTL_GIGA_MAC_VER_25, .fw_name = FIRMWARE_8168D_1 },
  172. { .mac_version = RTL_GIGA_MAC_VER_26, .fw_name = FIRMWARE_8168D_2 },
  173. { .mac_version = RTL_GIGA_MAC_VER_29, .fw_name = FIRMWARE_8105E_1 },
  174. { .mac_version = RTL_GIGA_MAC_VER_30, .fw_name = FIRMWARE_8105E_1 },
  175. { .mac_version = RTL_GIGA_MAC_VER_30, .fw_name = FIRMWARE_8105E_1 },
  176. { .mac_version = RTL_GIGA_MAC_VER_31, .fw_name = FIRMWARE_8168E_1 },
  177. { .mac_version = RTL_GIGA_MAC_VER_32, .fw_name = FIRMWARE_8168E_2 }
  178. };
  179. enum cfg_version {
  180. RTL_CFG_0 = 0x00,
  181. RTL_CFG_1,
  182. RTL_CFG_2
  183. };
  184. static void rtl_hw_start_8169(struct net_device *);
  185. static void rtl_hw_start_8168(struct net_device *);
  186. static void rtl_hw_start_8101(struct net_device *);
  187. static DEFINE_PCI_DEVICE_TABLE(rtl8169_pci_tbl) = {
  188. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
  189. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
  190. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
  191. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
  192. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
  193. { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
  194. { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
  195. { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
  196. { PCI_VENDOR_ID_LINKSYS, 0x1032,
  197. PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
  198. { 0x0001, 0x8168,
  199. PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
  200. {0,},
  201. };
  202. MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
  203. static int rx_buf_sz = 16383;
  204. static int use_dac;
  205. static struct {
  206. u32 msg_enable;
  207. } debug = { -1 };
  208. enum rtl_registers {
  209. MAC0 = 0, /* Ethernet hardware address. */
  210. MAC4 = 4,
  211. MAR0 = 8, /* Multicast filter. */
  212. CounterAddrLow = 0x10,
  213. CounterAddrHigh = 0x14,
  214. TxDescStartAddrLow = 0x20,
  215. TxDescStartAddrHigh = 0x24,
  216. TxHDescStartAddrLow = 0x28,
  217. TxHDescStartAddrHigh = 0x2c,
  218. FLASH = 0x30,
  219. ERSR = 0x36,
  220. ChipCmd = 0x37,
  221. TxPoll = 0x38,
  222. IntrMask = 0x3c,
  223. IntrStatus = 0x3e,
  224. TxConfig = 0x40,
  225. RxConfig = 0x44,
  226. #define RTL_RX_CONFIG_MASK 0xff7e1880u
  227. RxMissed = 0x4c,
  228. Cfg9346 = 0x50,
  229. Config0 = 0x51,
  230. Config1 = 0x52,
  231. Config2 = 0x53,
  232. Config3 = 0x54,
  233. Config4 = 0x55,
  234. Config5 = 0x56,
  235. MultiIntr = 0x5c,
  236. PHYAR = 0x60,
  237. PHYstatus = 0x6c,
  238. RxMaxSize = 0xda,
  239. CPlusCmd = 0xe0,
  240. IntrMitigate = 0xe2,
  241. RxDescAddrLow = 0xe4,
  242. RxDescAddrHigh = 0xe8,
  243. EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
  244. #define NoEarlyTx 0x3f /* Max value : no early transmit. */
  245. MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
  246. #define TxPacketMax (8064 >> 7)
  247. FuncEvent = 0xf0,
  248. FuncEventMask = 0xf4,
  249. FuncPresetState = 0xf8,
  250. FuncForceEvent = 0xfc,
  251. };
  252. enum rtl8110_registers {
  253. TBICSR = 0x64,
  254. TBI_ANAR = 0x68,
  255. TBI_LPAR = 0x6a,
  256. };
  257. enum rtl8168_8101_registers {
  258. CSIDR = 0x64,
  259. CSIAR = 0x68,
  260. #define CSIAR_FLAG 0x80000000
  261. #define CSIAR_WRITE_CMD 0x80000000
  262. #define CSIAR_BYTE_ENABLE 0x0f
  263. #define CSIAR_BYTE_ENABLE_SHIFT 12
  264. #define CSIAR_ADDR_MASK 0x0fff
  265. PMCH = 0x6f,
  266. EPHYAR = 0x80,
  267. #define EPHYAR_FLAG 0x80000000
  268. #define EPHYAR_WRITE_CMD 0x80000000
  269. #define EPHYAR_REG_MASK 0x1f
  270. #define EPHYAR_REG_SHIFT 16
  271. #define EPHYAR_DATA_MASK 0xffff
  272. DLLPR = 0xd0,
  273. #define PM_SWITCH (1 << 6)
  274. DBG_REG = 0xd1,
  275. #define FIX_NAK_1 (1 << 4)
  276. #define FIX_NAK_2 (1 << 3)
  277. TWSI = 0xd2,
  278. MCU = 0xd3,
  279. #define EN_NDP (1 << 3)
  280. #define EN_OOB_RESET (1 << 2)
  281. EFUSEAR = 0xdc,
  282. #define EFUSEAR_FLAG 0x80000000
  283. #define EFUSEAR_WRITE_CMD 0x80000000
  284. #define EFUSEAR_READ_CMD 0x00000000
  285. #define EFUSEAR_REG_MASK 0x03ff
  286. #define EFUSEAR_REG_SHIFT 8
  287. #define EFUSEAR_DATA_MASK 0xff
  288. };
  289. enum rtl8168_registers {
  290. ERIDR = 0x70,
  291. ERIAR = 0x74,
  292. #define ERIAR_FLAG 0x80000000
  293. #define ERIAR_WRITE_CMD 0x80000000
  294. #define ERIAR_READ_CMD 0x00000000
  295. #define ERIAR_ADDR_BYTE_ALIGN 4
  296. #define ERIAR_EXGMAC 0
  297. #define ERIAR_MSIX 1
  298. #define ERIAR_ASF 2
  299. #define ERIAR_TYPE_SHIFT 16
  300. #define ERIAR_BYTEEN 0x0f
  301. #define ERIAR_BYTEEN_SHIFT 12
  302. EPHY_RXER_NUM = 0x7c,
  303. OCPDR = 0xb0, /* OCP GPHY access */
  304. #define OCPDR_WRITE_CMD 0x80000000
  305. #define OCPDR_READ_CMD 0x00000000
  306. #define OCPDR_REG_MASK 0x7f
  307. #define OCPDR_GPHY_REG_SHIFT 16
  308. #define OCPDR_DATA_MASK 0xffff
  309. OCPAR = 0xb4,
  310. #define OCPAR_FLAG 0x80000000
  311. #define OCPAR_GPHY_WRITE_CMD 0x8000f060
  312. #define OCPAR_GPHY_READ_CMD 0x0000f060
  313. RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
  314. MISC = 0xf0, /* 8168e only. */
  315. txpla_rst = (1 << 29)
  316. };
  317. enum rtl_register_content {
  318. /* InterruptStatusBits */
  319. SYSErr = 0x8000,
  320. PCSTimeout = 0x4000,
  321. SWInt = 0x0100,
  322. TxDescUnavail = 0x0080,
  323. RxFIFOOver = 0x0040,
  324. LinkChg = 0x0020,
  325. RxOverflow = 0x0010,
  326. TxErr = 0x0008,
  327. TxOK = 0x0004,
  328. RxErr = 0x0002,
  329. RxOK = 0x0001,
  330. /* RxStatusDesc */
  331. RxFOVF = (1 << 23),
  332. RxRWT = (1 << 22),
  333. RxRES = (1 << 21),
  334. RxRUNT = (1 << 20),
  335. RxCRC = (1 << 19),
  336. /* ChipCmdBits */
  337. CmdReset = 0x10,
  338. CmdRxEnb = 0x08,
  339. CmdTxEnb = 0x04,
  340. RxBufEmpty = 0x01,
  341. /* TXPoll register p.5 */
  342. HPQ = 0x80, /* Poll cmd on the high prio queue */
  343. NPQ = 0x40, /* Poll cmd on the low prio queue */
  344. FSWInt = 0x01, /* Forced software interrupt */
  345. /* Cfg9346Bits */
  346. Cfg9346_Lock = 0x00,
  347. Cfg9346_Unlock = 0xc0,
  348. /* rx_mode_bits */
  349. AcceptErr = 0x20,
  350. AcceptRunt = 0x10,
  351. AcceptBroadcast = 0x08,
  352. AcceptMulticast = 0x04,
  353. AcceptMyPhys = 0x02,
  354. AcceptAllPhys = 0x01,
  355. /* RxConfigBits */
  356. RxCfgFIFOShift = 13,
  357. RxCfgDMAShift = 8,
  358. /* TxConfigBits */
  359. TxInterFrameGapShift = 24,
  360. TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
  361. /* Config1 register p.24 */
  362. LEDS1 = (1 << 7),
  363. LEDS0 = (1 << 6),
  364. MSIEnable = (1 << 5), /* Enable Message Signaled Interrupt */
  365. Speed_down = (1 << 4),
  366. MEMMAP = (1 << 3),
  367. IOMAP = (1 << 2),
  368. VPD = (1 << 1),
  369. PMEnable = (1 << 0), /* Power Management Enable */
  370. /* Config2 register p. 25 */
  371. PCI_Clock_66MHz = 0x01,
  372. PCI_Clock_33MHz = 0x00,
  373. /* Config3 register p.25 */
  374. MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
  375. LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
  376. Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
  377. /* Config5 register p.27 */
  378. BWF = (1 << 6), /* Accept Broadcast wakeup frame */
  379. MWF = (1 << 5), /* Accept Multicast wakeup frame */
  380. UWF = (1 << 4), /* Accept Unicast wakeup frame */
  381. spi_en = (1 << 3),
  382. LanWake = (1 << 1), /* LanWake enable/disable */
  383. PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
  384. /* TBICSR p.28 */
  385. TBIReset = 0x80000000,
  386. TBILoopback = 0x40000000,
  387. TBINwEnable = 0x20000000,
  388. TBINwRestart = 0x10000000,
  389. TBILinkOk = 0x02000000,
  390. TBINwComplete = 0x01000000,
  391. /* CPlusCmd p.31 */
  392. EnableBist = (1 << 15), // 8168 8101
  393. Mac_dbgo_oe = (1 << 14), // 8168 8101
  394. Normal_mode = (1 << 13), // unused
  395. Force_half_dup = (1 << 12), // 8168 8101
  396. Force_rxflow_en = (1 << 11), // 8168 8101
  397. Force_txflow_en = (1 << 10), // 8168 8101
  398. Cxpl_dbg_sel = (1 << 9), // 8168 8101
  399. ASF = (1 << 8), // 8168 8101
  400. PktCntrDisable = (1 << 7), // 8168 8101
  401. Mac_dbgo_sel = 0x001c, // 8168
  402. RxVlan = (1 << 6),
  403. RxChkSum = (1 << 5),
  404. PCIDAC = (1 << 4),
  405. PCIMulRW = (1 << 3),
  406. INTT_0 = 0x0000, // 8168
  407. INTT_1 = 0x0001, // 8168
  408. INTT_2 = 0x0002, // 8168
  409. INTT_3 = 0x0003, // 8168
  410. /* rtl8169_PHYstatus */
  411. TBI_Enable = 0x80,
  412. TxFlowCtrl = 0x40,
  413. RxFlowCtrl = 0x20,
  414. _1000bpsF = 0x10,
  415. _100bps = 0x08,
  416. _10bps = 0x04,
  417. LinkStatus = 0x02,
  418. FullDup = 0x01,
  419. /* _TBICSRBit */
  420. TBILinkOK = 0x02000000,
  421. /* DumpCounterCommand */
  422. CounterDump = 0x8,
  423. };
  424. enum rtl_desc_bit {
  425. /* First doubleword. */
  426. DescOwn = (1 << 31), /* Descriptor is owned by NIC */
  427. RingEnd = (1 << 30), /* End of descriptor ring */
  428. FirstFrag = (1 << 29), /* First segment of a packet */
  429. LastFrag = (1 << 28), /* Final segment of a packet */
  430. };
  431. /* Generic case. */
  432. enum rtl_tx_desc_bit {
  433. /* First doubleword. */
  434. TD_LSO = (1 << 27), /* Large Send Offload */
  435. #define TD_MSS_MAX 0x07ffu /* MSS value */
  436. /* Second doubleword. */
  437. TxVlanTag = (1 << 17), /* Add VLAN tag */
  438. };
  439. /* 8169, 8168b and 810x except 8102e. */
  440. enum rtl_tx_desc_bit_0 {
  441. /* First doubleword. */
  442. #define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
  443. TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
  444. TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
  445. TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
  446. };
  447. /* 8102e, 8168c and beyond. */
  448. enum rtl_tx_desc_bit_1 {
  449. /* Second doubleword. */
  450. #define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
  451. TD1_IP_CS = (1 << 29), /* Calculate IP checksum */
  452. TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
  453. TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
  454. };
  455. static const struct rtl_tx_desc_info {
  456. struct {
  457. u32 udp;
  458. u32 tcp;
  459. } checksum;
  460. u16 mss_shift;
  461. u16 opts_offset;
  462. } tx_desc_info [] = {
  463. [RTL_TD_0] = {
  464. .checksum = {
  465. .udp = TD0_IP_CS | TD0_UDP_CS,
  466. .tcp = TD0_IP_CS | TD0_TCP_CS
  467. },
  468. .mss_shift = TD0_MSS_SHIFT,
  469. .opts_offset = 0
  470. },
  471. [RTL_TD_1] = {
  472. .checksum = {
  473. .udp = TD1_IP_CS | TD1_UDP_CS,
  474. .tcp = TD1_IP_CS | TD1_TCP_CS
  475. },
  476. .mss_shift = TD1_MSS_SHIFT,
  477. .opts_offset = 1
  478. }
  479. };
  480. enum rtl_rx_desc_bit {
  481. /* Rx private */
  482. PID1 = (1 << 18), /* Protocol ID bit 1/2 */
  483. PID0 = (1 << 17), /* Protocol ID bit 2/2 */
  484. #define RxProtoUDP (PID1)
  485. #define RxProtoTCP (PID0)
  486. #define RxProtoIP (PID1 | PID0)
  487. #define RxProtoMask RxProtoIP
  488. IPFail = (1 << 16), /* IP checksum failed */
  489. UDPFail = (1 << 15), /* UDP/IP checksum failed */
  490. TCPFail = (1 << 14), /* TCP/IP checksum failed */
  491. RxVlanTag = (1 << 16), /* VLAN tag available */
  492. };
  493. #define RsvdMask 0x3fffc000
  494. struct TxDesc {
  495. __le32 opts1;
  496. __le32 opts2;
  497. __le64 addr;
  498. };
  499. struct RxDesc {
  500. __le32 opts1;
  501. __le32 opts2;
  502. __le64 addr;
  503. };
  504. struct ring_info {
  505. struct sk_buff *skb;
  506. u32 len;
  507. u8 __pad[sizeof(void *) - sizeof(u32)];
  508. };
  509. enum features {
  510. RTL_FEATURE_WOL = (1 << 0),
  511. RTL_FEATURE_MSI = (1 << 1),
  512. RTL_FEATURE_GMII = (1 << 2),
  513. };
  514. struct rtl8169_counters {
  515. __le64 tx_packets;
  516. __le64 rx_packets;
  517. __le64 tx_errors;
  518. __le32 rx_errors;
  519. __le16 rx_missed;
  520. __le16 align_errors;
  521. __le32 tx_one_collision;
  522. __le32 tx_multi_collision;
  523. __le64 rx_unicast;
  524. __le64 rx_broadcast;
  525. __le32 rx_multicast;
  526. __le16 tx_aborted;
  527. __le16 tx_underun;
  528. };
  529. struct rtl8169_private {
  530. void __iomem *mmio_addr; /* memory map physical address */
  531. struct pci_dev *pci_dev; /* Index of PCI device */
  532. struct net_device *dev;
  533. struct napi_struct napi;
  534. spinlock_t lock; /* spin lock flag */
  535. u32 msg_enable;
  536. u16 txd_version;
  537. u16 mac_version;
  538. u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
  539. u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
  540. u32 dirty_rx;
  541. u32 dirty_tx;
  542. struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
  543. struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
  544. dma_addr_t TxPhyAddr;
  545. dma_addr_t RxPhyAddr;
  546. void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
  547. struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
  548. struct timer_list timer;
  549. u16 cp_cmd;
  550. u16 intr_event;
  551. u16 napi_event;
  552. u16 intr_mask;
  553. int phy_1000_ctrl_reg;
  554. struct mdio_ops {
  555. void (*write)(void __iomem *, int, int);
  556. int (*read)(void __iomem *, int);
  557. } mdio_ops;
  558. struct pll_power_ops {
  559. void (*down)(struct rtl8169_private *);
  560. void (*up)(struct rtl8169_private *);
  561. } pll_power_ops;
  562. int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
  563. int (*get_settings)(struct net_device *, struct ethtool_cmd *);
  564. void (*phy_reset_enable)(struct rtl8169_private *tp);
  565. void (*hw_start)(struct net_device *);
  566. unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
  567. unsigned int (*link_ok)(void __iomem *);
  568. int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
  569. int pcie_cap;
  570. struct delayed_work task;
  571. unsigned features;
  572. struct mii_if_info mii;
  573. struct rtl8169_counters counters;
  574. u32 saved_wolopts;
  575. const struct firmware *fw;
  576. #define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN);
  577. };
  578. MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
  579. MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
  580. module_param(use_dac, int, 0);
  581. MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
  582. module_param_named(debug, debug.msg_enable, int, 0);
  583. MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
  584. MODULE_LICENSE("GPL");
  585. MODULE_VERSION(RTL8169_VERSION);
  586. MODULE_FIRMWARE(FIRMWARE_8168D_1);
  587. MODULE_FIRMWARE(FIRMWARE_8168D_2);
  588. MODULE_FIRMWARE(FIRMWARE_8168E_1);
  589. MODULE_FIRMWARE(FIRMWARE_8168E_2);
  590. MODULE_FIRMWARE(FIRMWARE_8105E_1);
  591. static int rtl8169_open(struct net_device *dev);
  592. static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
  593. struct net_device *dev);
  594. static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance);
  595. static int rtl8169_init_ring(struct net_device *dev);
  596. static void rtl_hw_start(struct net_device *dev);
  597. static int rtl8169_close(struct net_device *dev);
  598. static void rtl_set_rx_mode(struct net_device *dev);
  599. static void rtl8169_tx_timeout(struct net_device *dev);
  600. static struct net_device_stats *rtl8169_get_stats(struct net_device *dev);
  601. static int rtl8169_rx_interrupt(struct net_device *, struct rtl8169_private *,
  602. void __iomem *, u32 budget);
  603. static int rtl8169_change_mtu(struct net_device *dev, int new_mtu);
  604. static void rtl8169_down(struct net_device *dev);
  605. static void rtl8169_rx_clear(struct rtl8169_private *tp);
  606. static int rtl8169_poll(struct napi_struct *napi, int budget);
  607. static const unsigned int rtl8169_rx_config =
  608. (RX_FIFO_THRESH << RxCfgFIFOShift) | (RX_DMA_BURST << RxCfgDMAShift);
  609. static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
  610. {
  611. void __iomem *ioaddr = tp->mmio_addr;
  612. int i;
  613. RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
  614. for (i = 0; i < 20; i++) {
  615. udelay(100);
  616. if (RTL_R32(OCPAR) & OCPAR_FLAG)
  617. break;
  618. }
  619. return RTL_R32(OCPDR);
  620. }
  621. static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
  622. {
  623. void __iomem *ioaddr = tp->mmio_addr;
  624. int i;
  625. RTL_W32(OCPDR, data);
  626. RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
  627. for (i = 0; i < 20; i++) {
  628. udelay(100);
  629. if ((RTL_R32(OCPAR) & OCPAR_FLAG) == 0)
  630. break;
  631. }
  632. }
  633. static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
  634. {
  635. void __iomem *ioaddr = tp->mmio_addr;
  636. int i;
  637. RTL_W8(ERIDR, cmd);
  638. RTL_W32(ERIAR, 0x800010e8);
  639. msleep(2);
  640. for (i = 0; i < 5; i++) {
  641. udelay(100);
  642. if (!(RTL_R32(ERIDR) & ERIAR_FLAG))
  643. break;
  644. }
  645. ocp_write(tp, 0x1, 0x30, 0x00000001);
  646. }
  647. #define OOB_CMD_RESET 0x00
  648. #define OOB_CMD_DRIVER_START 0x05
  649. #define OOB_CMD_DRIVER_STOP 0x06
  650. static void rtl8168_driver_start(struct rtl8169_private *tp)
  651. {
  652. int i;
  653. u32 reg;
  654. rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
  655. if (tp->mac_version == RTL_GIGA_MAC_VER_31)
  656. reg = 0xb8;
  657. else
  658. reg = 0x10;
  659. for (i = 0; i < 10; i++) {
  660. msleep(10);
  661. if (ocp_read(tp, 0x0f, reg) & 0x00000800)
  662. break;
  663. }
  664. }
  665. static void rtl8168_driver_stop(struct rtl8169_private *tp)
  666. {
  667. int i;
  668. u32 reg;
  669. rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
  670. if (tp->mac_version == RTL_GIGA_MAC_VER_31)
  671. reg = 0xb8;
  672. else
  673. reg = 0x10;
  674. for (i = 0; i < 10; i++) {
  675. msleep(10);
  676. if ((ocp_read(tp, 0x0f, reg) & 0x00000800) == 0)
  677. break;
  678. }
  679. }
  680. static int r8168dp_check_dash(struct rtl8169_private *tp)
  681. {
  682. u32 reg;
  683. if (tp->mac_version == RTL_GIGA_MAC_VER_31)
  684. reg = 0xb8;
  685. else
  686. reg = 0x10;
  687. if (ocp_read(tp, 0xF, reg) & 0x00008000)
  688. return 1;
  689. else
  690. return 0;
  691. }
  692. static void r8169_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
  693. {
  694. int i;
  695. RTL_W32(PHYAR, 0x80000000 | (reg_addr & 0x1f) << 16 | (value & 0xffff));
  696. for (i = 20; i > 0; i--) {
  697. /*
  698. * Check if the RTL8169 has completed writing to the specified
  699. * MII register.
  700. */
  701. if (!(RTL_R32(PHYAR) & 0x80000000))
  702. break;
  703. udelay(25);
  704. }
  705. /*
  706. * According to hardware specs a 20us delay is required after write
  707. * complete indication, but before sending next command.
  708. */
  709. udelay(20);
  710. }
  711. static int r8169_mdio_read(void __iomem *ioaddr, int reg_addr)
  712. {
  713. int i, value = -1;
  714. RTL_W32(PHYAR, 0x0 | (reg_addr & 0x1f) << 16);
  715. for (i = 20; i > 0; i--) {
  716. /*
  717. * Check if the RTL8169 has completed retrieving data from
  718. * the specified MII register.
  719. */
  720. if (RTL_R32(PHYAR) & 0x80000000) {
  721. value = RTL_R32(PHYAR) & 0xffff;
  722. break;
  723. }
  724. udelay(25);
  725. }
  726. /*
  727. * According to hardware specs a 20us delay is required after read
  728. * complete indication, but before sending next command.
  729. */
  730. udelay(20);
  731. return value;
  732. }
  733. static void r8168dp_1_mdio_access(void __iomem *ioaddr, int reg_addr, u32 data)
  734. {
  735. int i;
  736. RTL_W32(OCPDR, data |
  737. ((reg_addr & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
  738. RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
  739. RTL_W32(EPHY_RXER_NUM, 0);
  740. for (i = 0; i < 100; i++) {
  741. mdelay(1);
  742. if (!(RTL_R32(OCPAR) & OCPAR_FLAG))
  743. break;
  744. }
  745. }
  746. static void r8168dp_1_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
  747. {
  748. r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_WRITE_CMD |
  749. (value & OCPDR_DATA_MASK));
  750. }
  751. static int r8168dp_1_mdio_read(void __iomem *ioaddr, int reg_addr)
  752. {
  753. int i;
  754. r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_READ_CMD);
  755. mdelay(1);
  756. RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
  757. RTL_W32(EPHY_RXER_NUM, 0);
  758. for (i = 0; i < 100; i++) {
  759. mdelay(1);
  760. if (RTL_R32(OCPAR) & OCPAR_FLAG)
  761. break;
  762. }
  763. return RTL_R32(OCPDR) & OCPDR_DATA_MASK;
  764. }
  765. #define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
  766. static void r8168dp_2_mdio_start(void __iomem *ioaddr)
  767. {
  768. RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
  769. }
  770. static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
  771. {
  772. RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
  773. }
  774. static void r8168dp_2_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
  775. {
  776. r8168dp_2_mdio_start(ioaddr);
  777. r8169_mdio_write(ioaddr, reg_addr, value);
  778. r8168dp_2_mdio_stop(ioaddr);
  779. }
  780. static int r8168dp_2_mdio_read(void __iomem *ioaddr, int reg_addr)
  781. {
  782. int value;
  783. r8168dp_2_mdio_start(ioaddr);
  784. value = r8169_mdio_read(ioaddr, reg_addr);
  785. r8168dp_2_mdio_stop(ioaddr);
  786. return value;
  787. }
  788. static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
  789. {
  790. tp->mdio_ops.write(tp->mmio_addr, location, val);
  791. }
  792. static int rtl_readphy(struct rtl8169_private *tp, int location)
  793. {
  794. return tp->mdio_ops.read(tp->mmio_addr, location);
  795. }
  796. static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
  797. {
  798. rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
  799. }
  800. static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
  801. {
  802. int val;
  803. val = rtl_readphy(tp, reg_addr);
  804. rtl_writephy(tp, reg_addr, (val | p) & ~m);
  805. }
  806. static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
  807. int val)
  808. {
  809. struct rtl8169_private *tp = netdev_priv(dev);
  810. rtl_writephy(tp, location, val);
  811. }
  812. static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
  813. {
  814. struct rtl8169_private *tp = netdev_priv(dev);
  815. return rtl_readphy(tp, location);
  816. }
  817. static void rtl_ephy_write(void __iomem *ioaddr, int reg_addr, int value)
  818. {
  819. unsigned int i;
  820. RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
  821. (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
  822. for (i = 0; i < 100; i++) {
  823. if (!(RTL_R32(EPHYAR) & EPHYAR_FLAG))
  824. break;
  825. udelay(10);
  826. }
  827. }
  828. static u16 rtl_ephy_read(void __iomem *ioaddr, int reg_addr)
  829. {
  830. u16 value = 0xffff;
  831. unsigned int i;
  832. RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
  833. for (i = 0; i < 100; i++) {
  834. if (RTL_R32(EPHYAR) & EPHYAR_FLAG) {
  835. value = RTL_R32(EPHYAR) & EPHYAR_DATA_MASK;
  836. break;
  837. }
  838. udelay(10);
  839. }
  840. return value;
  841. }
  842. static void rtl_csi_write(void __iomem *ioaddr, int addr, int value)
  843. {
  844. unsigned int i;
  845. RTL_W32(CSIDR, value);
  846. RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
  847. CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
  848. for (i = 0; i < 100; i++) {
  849. if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
  850. break;
  851. udelay(10);
  852. }
  853. }
  854. static u32 rtl_csi_read(void __iomem *ioaddr, int addr)
  855. {
  856. u32 value = ~0x00;
  857. unsigned int i;
  858. RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
  859. CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
  860. for (i = 0; i < 100; i++) {
  861. if (RTL_R32(CSIAR) & CSIAR_FLAG) {
  862. value = RTL_R32(CSIDR);
  863. break;
  864. }
  865. udelay(10);
  866. }
  867. return value;
  868. }
  869. static u8 rtl8168d_efuse_read(void __iomem *ioaddr, int reg_addr)
  870. {
  871. u8 value = 0xff;
  872. unsigned int i;
  873. RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
  874. for (i = 0; i < 300; i++) {
  875. if (RTL_R32(EFUSEAR) & EFUSEAR_FLAG) {
  876. value = RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK;
  877. break;
  878. }
  879. udelay(100);
  880. }
  881. return value;
  882. }
  883. static void rtl8169_irq_mask_and_ack(void __iomem *ioaddr)
  884. {
  885. RTL_W16(IntrMask, 0x0000);
  886. RTL_W16(IntrStatus, 0xffff);
  887. }
  888. static void rtl8169_asic_down(void __iomem *ioaddr)
  889. {
  890. RTL_W8(ChipCmd, 0x00);
  891. rtl8169_irq_mask_and_ack(ioaddr);
  892. RTL_R16(CPlusCmd);
  893. }
  894. static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
  895. {
  896. void __iomem *ioaddr = tp->mmio_addr;
  897. return RTL_R32(TBICSR) & TBIReset;
  898. }
  899. static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
  900. {
  901. return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
  902. }
  903. static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
  904. {
  905. return RTL_R32(TBICSR) & TBILinkOk;
  906. }
  907. static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
  908. {
  909. return RTL_R8(PHYstatus) & LinkStatus;
  910. }
  911. static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
  912. {
  913. void __iomem *ioaddr = tp->mmio_addr;
  914. RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
  915. }
  916. static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
  917. {
  918. unsigned int val;
  919. val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
  920. rtl_writephy(tp, MII_BMCR, val & 0xffff);
  921. }
  922. static void __rtl8169_check_link_status(struct net_device *dev,
  923. struct rtl8169_private *tp,
  924. void __iomem *ioaddr,
  925. bool pm)
  926. {
  927. unsigned long flags;
  928. spin_lock_irqsave(&tp->lock, flags);
  929. if (tp->link_ok(ioaddr)) {
  930. /* This is to cancel a scheduled suspend if there's one. */
  931. if (pm)
  932. pm_request_resume(&tp->pci_dev->dev);
  933. netif_carrier_on(dev);
  934. if (net_ratelimit())
  935. netif_info(tp, ifup, dev, "link up\n");
  936. } else {
  937. netif_carrier_off(dev);
  938. netif_info(tp, ifdown, dev, "link down\n");
  939. if (pm)
  940. pm_schedule_suspend(&tp->pci_dev->dev, 100);
  941. }
  942. spin_unlock_irqrestore(&tp->lock, flags);
  943. }
  944. static void rtl8169_check_link_status(struct net_device *dev,
  945. struct rtl8169_private *tp,
  946. void __iomem *ioaddr)
  947. {
  948. __rtl8169_check_link_status(dev, tp, ioaddr, false);
  949. }
  950. #define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
  951. static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
  952. {
  953. void __iomem *ioaddr = tp->mmio_addr;
  954. u8 options;
  955. u32 wolopts = 0;
  956. options = RTL_R8(Config1);
  957. if (!(options & PMEnable))
  958. return 0;
  959. options = RTL_R8(Config3);
  960. if (options & LinkUp)
  961. wolopts |= WAKE_PHY;
  962. if (options & MagicPacket)
  963. wolopts |= WAKE_MAGIC;
  964. options = RTL_R8(Config5);
  965. if (options & UWF)
  966. wolopts |= WAKE_UCAST;
  967. if (options & BWF)
  968. wolopts |= WAKE_BCAST;
  969. if (options & MWF)
  970. wolopts |= WAKE_MCAST;
  971. return wolopts;
  972. }
  973. static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  974. {
  975. struct rtl8169_private *tp = netdev_priv(dev);
  976. spin_lock_irq(&tp->lock);
  977. wol->supported = WAKE_ANY;
  978. wol->wolopts = __rtl8169_get_wol(tp);
  979. spin_unlock_irq(&tp->lock);
  980. }
  981. static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
  982. {
  983. void __iomem *ioaddr = tp->mmio_addr;
  984. unsigned int i;
  985. static const struct {
  986. u32 opt;
  987. u16 reg;
  988. u8 mask;
  989. } cfg[] = {
  990. { WAKE_ANY, Config1, PMEnable },
  991. { WAKE_PHY, Config3, LinkUp },
  992. { WAKE_MAGIC, Config3, MagicPacket },
  993. { WAKE_UCAST, Config5, UWF },
  994. { WAKE_BCAST, Config5, BWF },
  995. { WAKE_MCAST, Config5, MWF },
  996. { WAKE_ANY, Config5, LanWake }
  997. };
  998. RTL_W8(Cfg9346, Cfg9346_Unlock);
  999. for (i = 0; i < ARRAY_SIZE(cfg); i++) {
  1000. u8 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
  1001. if (wolopts & cfg[i].opt)
  1002. options |= cfg[i].mask;
  1003. RTL_W8(cfg[i].reg, options);
  1004. }
  1005. RTL_W8(Cfg9346, Cfg9346_Lock);
  1006. }
  1007. static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  1008. {
  1009. struct rtl8169_private *tp = netdev_priv(dev);
  1010. spin_lock_irq(&tp->lock);
  1011. if (wol->wolopts)
  1012. tp->features |= RTL_FEATURE_WOL;
  1013. else
  1014. tp->features &= ~RTL_FEATURE_WOL;
  1015. __rtl8169_set_wol(tp, wol->wolopts);
  1016. spin_unlock_irq(&tp->lock);
  1017. device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
  1018. return 0;
  1019. }
  1020. static void rtl8169_get_drvinfo(struct net_device *dev,
  1021. struct ethtool_drvinfo *info)
  1022. {
  1023. struct rtl8169_private *tp = netdev_priv(dev);
  1024. strcpy(info->driver, MODULENAME);
  1025. strcpy(info->version, RTL8169_VERSION);
  1026. strcpy(info->bus_info, pci_name(tp->pci_dev));
  1027. }
  1028. static int rtl8169_get_regs_len(struct net_device *dev)
  1029. {
  1030. return R8169_REGS_SIZE;
  1031. }
  1032. static int rtl8169_set_speed_tbi(struct net_device *dev,
  1033. u8 autoneg, u16 speed, u8 duplex, u32 ignored)
  1034. {
  1035. struct rtl8169_private *tp = netdev_priv(dev);
  1036. void __iomem *ioaddr = tp->mmio_addr;
  1037. int ret = 0;
  1038. u32 reg;
  1039. reg = RTL_R32(TBICSR);
  1040. if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
  1041. (duplex == DUPLEX_FULL)) {
  1042. RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
  1043. } else if (autoneg == AUTONEG_ENABLE)
  1044. RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
  1045. else {
  1046. netif_warn(tp, link, dev,
  1047. "incorrect speed setting refused in TBI mode\n");
  1048. ret = -EOPNOTSUPP;
  1049. }
  1050. return ret;
  1051. }
  1052. static int rtl8169_set_speed_xmii(struct net_device *dev,
  1053. u8 autoneg, u16 speed, u8 duplex, u32 adv)
  1054. {
  1055. struct rtl8169_private *tp = netdev_priv(dev);
  1056. int giga_ctrl, bmcr;
  1057. int rc = -EINVAL;
  1058. rtl_writephy(tp, 0x1f, 0x0000);
  1059. if (autoneg == AUTONEG_ENABLE) {
  1060. int auto_nego;
  1061. auto_nego = rtl_readphy(tp, MII_ADVERTISE);
  1062. auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
  1063. ADVERTISE_100HALF | ADVERTISE_100FULL);
  1064. if (adv & ADVERTISED_10baseT_Half)
  1065. auto_nego |= ADVERTISE_10HALF;
  1066. if (adv & ADVERTISED_10baseT_Full)
  1067. auto_nego |= ADVERTISE_10FULL;
  1068. if (adv & ADVERTISED_100baseT_Half)
  1069. auto_nego |= ADVERTISE_100HALF;
  1070. if (adv & ADVERTISED_100baseT_Full)
  1071. auto_nego |= ADVERTISE_100FULL;
  1072. auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
  1073. giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
  1074. giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
  1075. /* The 8100e/8101e/8102e do Fast Ethernet only. */
  1076. if ((tp->mac_version != RTL_GIGA_MAC_VER_07) &&
  1077. (tp->mac_version != RTL_GIGA_MAC_VER_08) &&
  1078. (tp->mac_version != RTL_GIGA_MAC_VER_09) &&
  1079. (tp->mac_version != RTL_GIGA_MAC_VER_10) &&
  1080. (tp->mac_version != RTL_GIGA_MAC_VER_13) &&
  1081. (tp->mac_version != RTL_GIGA_MAC_VER_14) &&
  1082. (tp->mac_version != RTL_GIGA_MAC_VER_15) &&
  1083. (tp->mac_version != RTL_GIGA_MAC_VER_16) &&
  1084. (tp->mac_version != RTL_GIGA_MAC_VER_29) &&
  1085. (tp->mac_version != RTL_GIGA_MAC_VER_30)) {
  1086. if (adv & ADVERTISED_1000baseT_Half)
  1087. giga_ctrl |= ADVERTISE_1000HALF;
  1088. if (adv & ADVERTISED_1000baseT_Full)
  1089. giga_ctrl |= ADVERTISE_1000FULL;
  1090. } else if (adv & (ADVERTISED_1000baseT_Half |
  1091. ADVERTISED_1000baseT_Full)) {
  1092. netif_info(tp, link, dev,
  1093. "PHY does not support 1000Mbps\n");
  1094. goto out;
  1095. }
  1096. bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
  1097. rtl_writephy(tp, MII_ADVERTISE, auto_nego);
  1098. rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
  1099. } else {
  1100. giga_ctrl = 0;
  1101. if (speed == SPEED_10)
  1102. bmcr = 0;
  1103. else if (speed == SPEED_100)
  1104. bmcr = BMCR_SPEED100;
  1105. else
  1106. goto out;
  1107. if (duplex == DUPLEX_FULL)
  1108. bmcr |= BMCR_FULLDPLX;
  1109. }
  1110. tp->phy_1000_ctrl_reg = giga_ctrl;
  1111. rtl_writephy(tp, MII_BMCR, bmcr);
  1112. if ((tp->mac_version == RTL_GIGA_MAC_VER_02) ||
  1113. (tp->mac_version == RTL_GIGA_MAC_VER_03)) {
  1114. if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
  1115. rtl_writephy(tp, 0x17, 0x2138);
  1116. rtl_writephy(tp, 0x0e, 0x0260);
  1117. } else {
  1118. rtl_writephy(tp, 0x17, 0x2108);
  1119. rtl_writephy(tp, 0x0e, 0x0000);
  1120. }
  1121. }
  1122. rc = 0;
  1123. out:
  1124. return rc;
  1125. }
  1126. static int rtl8169_set_speed(struct net_device *dev,
  1127. u8 autoneg, u16 speed, u8 duplex, u32 advertising)
  1128. {
  1129. struct rtl8169_private *tp = netdev_priv(dev);
  1130. int ret;
  1131. ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
  1132. if (netif_running(dev) && (tp->phy_1000_ctrl_reg & ADVERTISE_1000FULL))
  1133. mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
  1134. return ret;
  1135. }
  1136. static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1137. {
  1138. struct rtl8169_private *tp = netdev_priv(dev);
  1139. unsigned long flags;
  1140. int ret;
  1141. spin_lock_irqsave(&tp->lock, flags);
  1142. ret = rtl8169_set_speed(dev,
  1143. cmd->autoneg, cmd->speed, cmd->duplex, cmd->advertising);
  1144. spin_unlock_irqrestore(&tp->lock, flags);
  1145. return ret;
  1146. }
  1147. static u32 rtl8169_fix_features(struct net_device *dev, u32 features)
  1148. {
  1149. if (dev->mtu > TD_MSS_MAX)
  1150. features &= ~NETIF_F_ALL_TSO;
  1151. return features;
  1152. }
  1153. static int rtl8169_set_features(struct net_device *dev, u32 features)
  1154. {
  1155. struct rtl8169_private *tp = netdev_priv(dev);
  1156. void __iomem *ioaddr = tp->mmio_addr;
  1157. unsigned long flags;
  1158. spin_lock_irqsave(&tp->lock, flags);
  1159. if (features & NETIF_F_RXCSUM)
  1160. tp->cp_cmd |= RxChkSum;
  1161. else
  1162. tp->cp_cmd &= ~RxChkSum;
  1163. if (dev->features & NETIF_F_HW_VLAN_RX)
  1164. tp->cp_cmd |= RxVlan;
  1165. else
  1166. tp->cp_cmd &= ~RxVlan;
  1167. RTL_W16(CPlusCmd, tp->cp_cmd);
  1168. RTL_R16(CPlusCmd);
  1169. spin_unlock_irqrestore(&tp->lock, flags);
  1170. return 0;
  1171. }
  1172. static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
  1173. struct sk_buff *skb)
  1174. {
  1175. return (vlan_tx_tag_present(skb)) ?
  1176. TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
  1177. }
  1178. static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
  1179. {
  1180. u32 opts2 = le32_to_cpu(desc->opts2);
  1181. if (opts2 & RxVlanTag)
  1182. __vlan_hwaccel_put_tag(skb, swab16(opts2 & 0xffff));
  1183. desc->opts2 = 0;
  1184. }
  1185. static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
  1186. {
  1187. struct rtl8169_private *tp = netdev_priv(dev);
  1188. void __iomem *ioaddr = tp->mmio_addr;
  1189. u32 status;
  1190. cmd->supported =
  1191. SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
  1192. cmd->port = PORT_FIBRE;
  1193. cmd->transceiver = XCVR_INTERNAL;
  1194. status = RTL_R32(TBICSR);
  1195. cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
  1196. cmd->autoneg = !!(status & TBINwEnable);
  1197. cmd->speed = SPEED_1000;
  1198. cmd->duplex = DUPLEX_FULL; /* Always set */
  1199. return 0;
  1200. }
  1201. static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
  1202. {
  1203. struct rtl8169_private *tp = netdev_priv(dev);
  1204. return mii_ethtool_gset(&tp->mii, cmd);
  1205. }
  1206. static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1207. {
  1208. struct rtl8169_private *tp = netdev_priv(dev);
  1209. unsigned long flags;
  1210. int rc;
  1211. spin_lock_irqsave(&tp->lock, flags);
  1212. rc = tp->get_settings(dev, cmd);
  1213. spin_unlock_irqrestore(&tp->lock, flags);
  1214. return rc;
  1215. }
  1216. static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
  1217. void *p)
  1218. {
  1219. struct rtl8169_private *tp = netdev_priv(dev);
  1220. unsigned long flags;
  1221. if (regs->len > R8169_REGS_SIZE)
  1222. regs->len = R8169_REGS_SIZE;
  1223. spin_lock_irqsave(&tp->lock, flags);
  1224. memcpy_fromio(p, tp->mmio_addr, regs->len);
  1225. spin_unlock_irqrestore(&tp->lock, flags);
  1226. }
  1227. static u32 rtl8169_get_msglevel(struct net_device *dev)
  1228. {
  1229. struct rtl8169_private *tp = netdev_priv(dev);
  1230. return tp->msg_enable;
  1231. }
  1232. static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
  1233. {
  1234. struct rtl8169_private *tp = netdev_priv(dev);
  1235. tp->msg_enable = value;
  1236. }
  1237. static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
  1238. "tx_packets",
  1239. "rx_packets",
  1240. "tx_errors",
  1241. "rx_errors",
  1242. "rx_missed",
  1243. "align_errors",
  1244. "tx_single_collisions",
  1245. "tx_multi_collisions",
  1246. "unicast",
  1247. "broadcast",
  1248. "multicast",
  1249. "tx_aborted",
  1250. "tx_underrun",
  1251. };
  1252. static int rtl8169_get_sset_count(struct net_device *dev, int sset)
  1253. {
  1254. switch (sset) {
  1255. case ETH_SS_STATS:
  1256. return ARRAY_SIZE(rtl8169_gstrings);
  1257. default:
  1258. return -EOPNOTSUPP;
  1259. }
  1260. }
  1261. static void rtl8169_update_counters(struct net_device *dev)
  1262. {
  1263. struct rtl8169_private *tp = netdev_priv(dev);
  1264. void __iomem *ioaddr = tp->mmio_addr;
  1265. struct rtl8169_counters *counters;
  1266. dma_addr_t paddr;
  1267. u32 cmd;
  1268. int wait = 1000;
  1269. struct device *d = &tp->pci_dev->dev;
  1270. /*
  1271. * Some chips are unable to dump tally counters when the receiver
  1272. * is disabled.
  1273. */
  1274. if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
  1275. return;
  1276. counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
  1277. if (!counters)
  1278. return;
  1279. RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
  1280. cmd = (u64)paddr & DMA_BIT_MASK(32);
  1281. RTL_W32(CounterAddrLow, cmd);
  1282. RTL_W32(CounterAddrLow, cmd | CounterDump);
  1283. while (wait--) {
  1284. if ((RTL_R32(CounterAddrLow) & CounterDump) == 0) {
  1285. /* copy updated counters */
  1286. memcpy(&tp->counters, counters, sizeof(*counters));
  1287. break;
  1288. }
  1289. udelay(10);
  1290. }
  1291. RTL_W32(CounterAddrLow, 0);
  1292. RTL_W32(CounterAddrHigh, 0);
  1293. dma_free_coherent(d, sizeof(*counters), counters, paddr);
  1294. }
  1295. static void rtl8169_get_ethtool_stats(struct net_device *dev,
  1296. struct ethtool_stats *stats, u64 *data)
  1297. {
  1298. struct rtl8169_private *tp = netdev_priv(dev);
  1299. ASSERT_RTNL();
  1300. rtl8169_update_counters(dev);
  1301. data[0] = le64_to_cpu(tp->counters.tx_packets);
  1302. data[1] = le64_to_cpu(tp->counters.rx_packets);
  1303. data[2] = le64_to_cpu(tp->counters.tx_errors);
  1304. data[3] = le32_to_cpu(tp->counters.rx_errors);
  1305. data[4] = le16_to_cpu(tp->counters.rx_missed);
  1306. data[5] = le16_to_cpu(tp->counters.align_errors);
  1307. data[6] = le32_to_cpu(tp->counters.tx_one_collision);
  1308. data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
  1309. data[8] = le64_to_cpu(tp->counters.rx_unicast);
  1310. data[9] = le64_to_cpu(tp->counters.rx_broadcast);
  1311. data[10] = le32_to_cpu(tp->counters.rx_multicast);
  1312. data[11] = le16_to_cpu(tp->counters.tx_aborted);
  1313. data[12] = le16_to_cpu(tp->counters.tx_underun);
  1314. }
  1315. static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
  1316. {
  1317. switch(stringset) {
  1318. case ETH_SS_STATS:
  1319. memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
  1320. break;
  1321. }
  1322. }
  1323. static const struct ethtool_ops rtl8169_ethtool_ops = {
  1324. .get_drvinfo = rtl8169_get_drvinfo,
  1325. .get_regs_len = rtl8169_get_regs_len,
  1326. .get_link = ethtool_op_get_link,
  1327. .get_settings = rtl8169_get_settings,
  1328. .set_settings = rtl8169_set_settings,
  1329. .get_msglevel = rtl8169_get_msglevel,
  1330. .set_msglevel = rtl8169_set_msglevel,
  1331. .get_regs = rtl8169_get_regs,
  1332. .get_wol = rtl8169_get_wol,
  1333. .set_wol = rtl8169_set_wol,
  1334. .get_strings = rtl8169_get_strings,
  1335. .get_sset_count = rtl8169_get_sset_count,
  1336. .get_ethtool_stats = rtl8169_get_ethtool_stats,
  1337. };
  1338. static void rtl8169_get_mac_version(struct rtl8169_private *tp,
  1339. void __iomem *ioaddr)
  1340. {
  1341. /*
  1342. * The driver currently handles the 8168Bf and the 8168Be identically
  1343. * but they can be identified more specifically through the test below
  1344. * if needed:
  1345. *
  1346. * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
  1347. *
  1348. * Same thing for the 8101Eb and the 8101Ec:
  1349. *
  1350. * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
  1351. */
  1352. static const struct {
  1353. u32 mask;
  1354. u32 val;
  1355. int mac_version;
  1356. } mac_info[] = {
  1357. /* 8168E family. */
  1358. { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
  1359. { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
  1360. { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
  1361. /* 8168D family. */
  1362. { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
  1363. { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
  1364. { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
  1365. /* 8168DP family. */
  1366. { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
  1367. { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
  1368. { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
  1369. /* 8168C family. */
  1370. { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
  1371. { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
  1372. { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
  1373. { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
  1374. { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
  1375. { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
  1376. { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
  1377. { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
  1378. { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
  1379. /* 8168B family. */
  1380. { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
  1381. { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
  1382. { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
  1383. { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
  1384. /* 8101 family. */
  1385. { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
  1386. { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
  1387. { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
  1388. { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
  1389. { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
  1390. { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
  1391. { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
  1392. { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
  1393. { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
  1394. { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
  1395. { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
  1396. { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
  1397. { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
  1398. { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
  1399. { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
  1400. { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
  1401. /* FIXME: where did these entries come from ? -- FR */
  1402. { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
  1403. { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
  1404. /* 8110 family. */
  1405. { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
  1406. { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
  1407. { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
  1408. { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
  1409. { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
  1410. { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
  1411. /* Catch-all */
  1412. { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
  1413. }, *p = mac_info;
  1414. u32 reg;
  1415. reg = RTL_R32(TxConfig);
  1416. while ((reg & p->mask) != p->val)
  1417. p++;
  1418. tp->mac_version = p->mac_version;
  1419. }
  1420. static void rtl8169_print_mac_version(struct rtl8169_private *tp)
  1421. {
  1422. dprintk("mac_version = 0x%02x\n", tp->mac_version);
  1423. }
  1424. struct phy_reg {
  1425. u16 reg;
  1426. u16 val;
  1427. };
  1428. static void rtl_writephy_batch(struct rtl8169_private *tp,
  1429. const struct phy_reg *regs, int len)
  1430. {
  1431. while (len-- > 0) {
  1432. rtl_writephy(tp, regs->reg, regs->val);
  1433. regs++;
  1434. }
  1435. }
  1436. #define PHY_READ 0x00000000
  1437. #define PHY_DATA_OR 0x10000000
  1438. #define PHY_DATA_AND 0x20000000
  1439. #define PHY_BJMPN 0x30000000
  1440. #define PHY_READ_EFUSE 0x40000000
  1441. #define PHY_READ_MAC_BYTE 0x50000000
  1442. #define PHY_WRITE_MAC_BYTE 0x60000000
  1443. #define PHY_CLEAR_READCOUNT 0x70000000
  1444. #define PHY_WRITE 0x80000000
  1445. #define PHY_READCOUNT_EQ_SKIP 0x90000000
  1446. #define PHY_COMP_EQ_SKIPN 0xa0000000
  1447. #define PHY_COMP_NEQ_SKIPN 0xb0000000
  1448. #define PHY_WRITE_PREVIOUS 0xc0000000
  1449. #define PHY_SKIPN 0xd0000000
  1450. #define PHY_DELAY_MS 0xe0000000
  1451. #define PHY_WRITE_ERI_WORD 0xf0000000
  1452. static void
  1453. rtl_phy_write_fw(struct rtl8169_private *tp, const struct firmware *fw)
  1454. {
  1455. __le32 *phytable = (__le32 *)fw->data;
  1456. struct net_device *dev = tp->dev;
  1457. size_t index, fw_size = fw->size / sizeof(*phytable);
  1458. u32 predata, count;
  1459. if (fw->size % sizeof(*phytable)) {
  1460. netif_err(tp, probe, dev, "odd sized firmware %zd\n", fw->size);
  1461. return;
  1462. }
  1463. for (index = 0; index < fw_size; index++) {
  1464. u32 action = le32_to_cpu(phytable[index]);
  1465. u32 regno = (action & 0x0fff0000) >> 16;
  1466. switch(action & 0xf0000000) {
  1467. case PHY_READ:
  1468. case PHY_DATA_OR:
  1469. case PHY_DATA_AND:
  1470. case PHY_READ_EFUSE:
  1471. case PHY_CLEAR_READCOUNT:
  1472. case PHY_WRITE:
  1473. case PHY_WRITE_PREVIOUS:
  1474. case PHY_DELAY_MS:
  1475. break;
  1476. case PHY_BJMPN:
  1477. if (regno > index) {
  1478. netif_err(tp, probe, tp->dev,
  1479. "Out of range of firmware\n");
  1480. return;
  1481. }
  1482. break;
  1483. case PHY_READCOUNT_EQ_SKIP:
  1484. if (index + 2 >= fw_size) {
  1485. netif_err(tp, probe, tp->dev,
  1486. "Out of range of firmware\n");
  1487. return;
  1488. }
  1489. break;
  1490. case PHY_COMP_EQ_SKIPN:
  1491. case PHY_COMP_NEQ_SKIPN:
  1492. case PHY_SKIPN:
  1493. if (index + 1 + regno >= fw_size) {
  1494. netif_err(tp, probe, tp->dev,
  1495. "Out of range of firmware\n");
  1496. return;
  1497. }
  1498. break;
  1499. case PHY_READ_MAC_BYTE:
  1500. case PHY_WRITE_MAC_BYTE:
  1501. case PHY_WRITE_ERI_WORD:
  1502. default:
  1503. netif_err(tp, probe, tp->dev,
  1504. "Invalid action 0x%08x\n", action);
  1505. return;
  1506. }
  1507. }
  1508. predata = 0;
  1509. count = 0;
  1510. for (index = 0; index < fw_size; ) {
  1511. u32 action = le32_to_cpu(phytable[index]);
  1512. u32 data = action & 0x0000ffff;
  1513. u32 regno = (action & 0x0fff0000) >> 16;
  1514. if (!action)
  1515. break;
  1516. switch(action & 0xf0000000) {
  1517. case PHY_READ:
  1518. predata = rtl_readphy(tp, regno);
  1519. count++;
  1520. index++;
  1521. break;
  1522. case PHY_DATA_OR:
  1523. predata |= data;
  1524. index++;
  1525. break;
  1526. case PHY_DATA_AND:
  1527. predata &= data;
  1528. index++;
  1529. break;
  1530. case PHY_BJMPN:
  1531. index -= regno;
  1532. break;
  1533. case PHY_READ_EFUSE:
  1534. predata = rtl8168d_efuse_read(tp->mmio_addr, regno);
  1535. index++;
  1536. break;
  1537. case PHY_CLEAR_READCOUNT:
  1538. count = 0;
  1539. index++;
  1540. break;
  1541. case PHY_WRITE:
  1542. rtl_writephy(tp, regno, data);
  1543. index++;
  1544. break;
  1545. case PHY_READCOUNT_EQ_SKIP:
  1546. if (count == data)
  1547. index += 2;
  1548. else
  1549. index += 1;
  1550. break;
  1551. case PHY_COMP_EQ_SKIPN:
  1552. if (predata == data)
  1553. index += regno;
  1554. index++;
  1555. break;
  1556. case PHY_COMP_NEQ_SKIPN:
  1557. if (predata != data)
  1558. index += regno;
  1559. index++;
  1560. break;
  1561. case PHY_WRITE_PREVIOUS:
  1562. rtl_writephy(tp, regno, predata);
  1563. index++;
  1564. break;
  1565. case PHY_SKIPN:
  1566. index += regno + 1;
  1567. break;
  1568. case PHY_DELAY_MS:
  1569. mdelay(data);
  1570. index++;
  1571. break;
  1572. case PHY_READ_MAC_BYTE:
  1573. case PHY_WRITE_MAC_BYTE:
  1574. case PHY_WRITE_ERI_WORD:
  1575. default:
  1576. BUG();
  1577. }
  1578. }
  1579. }
  1580. static void rtl_release_firmware(struct rtl8169_private *tp)
  1581. {
  1582. if (!IS_ERR_OR_NULL(tp->fw))
  1583. release_firmware(tp->fw);
  1584. tp->fw = RTL_FIRMWARE_UNKNOWN;
  1585. }
  1586. static void rtl_apply_firmware(struct rtl8169_private *tp)
  1587. {
  1588. const struct firmware *fw = tp->fw;
  1589. /* TODO: release firmware once rtl_phy_write_fw signals failures. */
  1590. if (!IS_ERR_OR_NULL(fw))
  1591. rtl_phy_write_fw(tp, fw);
  1592. }
  1593. static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
  1594. {
  1595. if (rtl_readphy(tp, reg) != val)
  1596. netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
  1597. else
  1598. rtl_apply_firmware(tp);
  1599. }
  1600. static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
  1601. {
  1602. static const struct phy_reg phy_reg_init[] = {
  1603. { 0x1f, 0x0001 },
  1604. { 0x06, 0x006e },
  1605. { 0x08, 0x0708 },
  1606. { 0x15, 0x4000 },
  1607. { 0x18, 0x65c7 },
  1608. { 0x1f, 0x0001 },
  1609. { 0x03, 0x00a1 },
  1610. { 0x02, 0x0008 },
  1611. { 0x01, 0x0120 },
  1612. { 0x00, 0x1000 },
  1613. { 0x04, 0x0800 },
  1614. { 0x04, 0x0000 },
  1615. { 0x03, 0xff41 },
  1616. { 0x02, 0xdf60 },
  1617. { 0x01, 0x0140 },
  1618. { 0x00, 0x0077 },
  1619. { 0x04, 0x7800 },
  1620. { 0x04, 0x7000 },
  1621. { 0x03, 0x802f },
  1622. { 0x02, 0x4f02 },
  1623. { 0x01, 0x0409 },
  1624. { 0x00, 0xf0f9 },
  1625. { 0x04, 0x9800 },
  1626. { 0x04, 0x9000 },
  1627. { 0x03, 0xdf01 },
  1628. { 0x02, 0xdf20 },
  1629. { 0x01, 0xff95 },
  1630. { 0x00, 0xba00 },
  1631. { 0x04, 0xa800 },
  1632. { 0x04, 0xa000 },
  1633. { 0x03, 0xff41 },
  1634. { 0x02, 0xdf20 },
  1635. { 0x01, 0x0140 },
  1636. { 0x00, 0x00bb },
  1637. { 0x04, 0xb800 },
  1638. { 0x04, 0xb000 },
  1639. { 0x03, 0xdf41 },
  1640. { 0x02, 0xdc60 },
  1641. { 0x01, 0x6340 },
  1642. { 0x00, 0x007d },
  1643. { 0x04, 0xd800 },
  1644. { 0x04, 0xd000 },
  1645. { 0x03, 0xdf01 },
  1646. { 0x02, 0xdf20 },
  1647. { 0x01, 0x100a },
  1648. { 0x00, 0xa0ff },
  1649. { 0x04, 0xf800 },
  1650. { 0x04, 0xf000 },
  1651. { 0x1f, 0x0000 },
  1652. { 0x0b, 0x0000 },
  1653. { 0x00, 0x9200 }
  1654. };
  1655. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1656. }
  1657. static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
  1658. {
  1659. static const struct phy_reg phy_reg_init[] = {
  1660. { 0x1f, 0x0002 },
  1661. { 0x01, 0x90d0 },
  1662. { 0x1f, 0x0000 }
  1663. };
  1664. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1665. }
  1666. static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
  1667. {
  1668. struct pci_dev *pdev = tp->pci_dev;
  1669. u16 vendor_id, device_id;
  1670. pci_read_config_word(pdev, PCI_SUBSYSTEM_VENDOR_ID, &vendor_id);
  1671. pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &device_id);
  1672. if ((vendor_id != PCI_VENDOR_ID_GIGABYTE) || (device_id != 0xe000))
  1673. return;
  1674. rtl_writephy(tp, 0x1f, 0x0001);
  1675. rtl_writephy(tp, 0x10, 0xf01b);
  1676. rtl_writephy(tp, 0x1f, 0x0000);
  1677. }
  1678. static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
  1679. {
  1680. static const struct phy_reg phy_reg_init[] = {
  1681. { 0x1f, 0x0001 },
  1682. { 0x04, 0x0000 },
  1683. { 0x03, 0x00a1 },
  1684. { 0x02, 0x0008 },
  1685. { 0x01, 0x0120 },
  1686. { 0x00, 0x1000 },
  1687. { 0x04, 0x0800 },
  1688. { 0x04, 0x9000 },
  1689. { 0x03, 0x802f },
  1690. { 0x02, 0x4f02 },
  1691. { 0x01, 0x0409 },
  1692. { 0x00, 0xf099 },
  1693. { 0x04, 0x9800 },
  1694. { 0x04, 0xa000 },
  1695. { 0x03, 0xdf01 },
  1696. { 0x02, 0xdf20 },
  1697. { 0x01, 0xff95 },
  1698. { 0x00, 0xba00 },
  1699. { 0x04, 0xa800 },
  1700. { 0x04, 0xf000 },
  1701. { 0x03, 0xdf01 },
  1702. { 0x02, 0xdf20 },
  1703. { 0x01, 0x101a },
  1704. { 0x00, 0xa0ff },
  1705. { 0x04, 0xf800 },
  1706. { 0x04, 0x0000 },
  1707. { 0x1f, 0x0000 },
  1708. { 0x1f, 0x0001 },
  1709. { 0x10, 0xf41b },
  1710. { 0x14, 0xfb54 },
  1711. { 0x18, 0xf5c7 },
  1712. { 0x1f, 0x0000 },
  1713. { 0x1f, 0x0001 },
  1714. { 0x17, 0x0cc0 },
  1715. { 0x1f, 0x0000 }
  1716. };
  1717. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1718. rtl8169scd_hw_phy_config_quirk(tp);
  1719. }
  1720. static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
  1721. {
  1722. static const struct phy_reg phy_reg_init[] = {
  1723. { 0x1f, 0x0001 },
  1724. { 0x04, 0x0000 },
  1725. { 0x03, 0x00a1 },
  1726. { 0x02, 0x0008 },
  1727. { 0x01, 0x0120 },
  1728. { 0x00, 0x1000 },
  1729. { 0x04, 0x0800 },
  1730. { 0x04, 0x9000 },
  1731. { 0x03, 0x802f },
  1732. { 0x02, 0x4f02 },
  1733. { 0x01, 0x0409 },
  1734. { 0x00, 0xf099 },
  1735. { 0x04, 0x9800 },
  1736. { 0x04, 0xa000 },
  1737. { 0x03, 0xdf01 },
  1738. { 0x02, 0xdf20 },
  1739. { 0x01, 0xff95 },
  1740. { 0x00, 0xba00 },
  1741. { 0x04, 0xa800 },
  1742. { 0x04, 0xf000 },
  1743. { 0x03, 0xdf01 },
  1744. { 0x02, 0xdf20 },
  1745. { 0x01, 0x101a },
  1746. { 0x00, 0xa0ff },
  1747. { 0x04, 0xf800 },
  1748. { 0x04, 0x0000 },
  1749. { 0x1f, 0x0000 },
  1750. { 0x1f, 0x0001 },
  1751. { 0x0b, 0x8480 },
  1752. { 0x1f, 0x0000 },
  1753. { 0x1f, 0x0001 },
  1754. { 0x18, 0x67c7 },
  1755. { 0x04, 0x2000 },
  1756. { 0x03, 0x002f },
  1757. { 0x02, 0x4360 },
  1758. { 0x01, 0x0109 },
  1759. { 0x00, 0x3022 },
  1760. { 0x04, 0x2800 },
  1761. { 0x1f, 0x0000 },
  1762. { 0x1f, 0x0001 },
  1763. { 0x17, 0x0cc0 },
  1764. { 0x1f, 0x0000 }
  1765. };
  1766. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1767. }
  1768. static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
  1769. {
  1770. static const struct phy_reg phy_reg_init[] = {
  1771. { 0x10, 0xf41b },
  1772. { 0x1f, 0x0000 }
  1773. };
  1774. rtl_writephy(tp, 0x1f, 0x0001);
  1775. rtl_patchphy(tp, 0x16, 1 << 0);
  1776. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1777. }
  1778. static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
  1779. {
  1780. static const struct phy_reg phy_reg_init[] = {
  1781. { 0x1f, 0x0001 },
  1782. { 0x10, 0xf41b },
  1783. { 0x1f, 0x0000 }
  1784. };
  1785. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1786. }
  1787. static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
  1788. {
  1789. static const struct phy_reg phy_reg_init[] = {
  1790. { 0x1f, 0x0000 },
  1791. { 0x1d, 0x0f00 },
  1792. { 0x1f, 0x0002 },
  1793. { 0x0c, 0x1ec8 },
  1794. { 0x1f, 0x0000 }
  1795. };
  1796. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1797. }
  1798. static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
  1799. {
  1800. static const struct phy_reg phy_reg_init[] = {
  1801. { 0x1f, 0x0001 },
  1802. { 0x1d, 0x3d98 },
  1803. { 0x1f, 0x0000 }
  1804. };
  1805. rtl_writephy(tp, 0x1f, 0x0000);
  1806. rtl_patchphy(tp, 0x14, 1 << 5);
  1807. rtl_patchphy(tp, 0x0d, 1 << 5);
  1808. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1809. }
  1810. static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
  1811. {
  1812. static const struct phy_reg phy_reg_init[] = {
  1813. { 0x1f, 0x0001 },
  1814. { 0x12, 0x2300 },
  1815. { 0x1f, 0x0002 },
  1816. { 0x00, 0x88d4 },
  1817. { 0x01, 0x82b1 },
  1818. { 0x03, 0x7002 },
  1819. { 0x08, 0x9e30 },
  1820. { 0x09, 0x01f0 },
  1821. { 0x0a, 0x5500 },
  1822. { 0x0c, 0x00c8 },
  1823. { 0x1f, 0x0003 },
  1824. { 0x12, 0xc096 },
  1825. { 0x16, 0x000a },
  1826. { 0x1f, 0x0000 },
  1827. { 0x1f, 0x0000 },
  1828. { 0x09, 0x2000 },
  1829. { 0x09, 0x0000 }
  1830. };
  1831. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1832. rtl_patchphy(tp, 0x14, 1 << 5);
  1833. rtl_patchphy(tp, 0x0d, 1 << 5);
  1834. rtl_writephy(tp, 0x1f, 0x0000);
  1835. }
  1836. static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
  1837. {
  1838. static const struct phy_reg phy_reg_init[] = {
  1839. { 0x1f, 0x0001 },
  1840. { 0x12, 0x2300 },
  1841. { 0x03, 0x802f },
  1842. { 0x02, 0x4f02 },
  1843. { 0x01, 0x0409 },
  1844. { 0x00, 0xf099 },
  1845. { 0x04, 0x9800 },
  1846. { 0x04, 0x9000 },
  1847. { 0x1d, 0x3d98 },
  1848. { 0x1f, 0x0002 },
  1849. { 0x0c, 0x7eb8 },
  1850. { 0x06, 0x0761 },
  1851. { 0x1f, 0x0003 },
  1852. { 0x16, 0x0f0a },
  1853. { 0x1f, 0x0000 }
  1854. };
  1855. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1856. rtl_patchphy(tp, 0x16, 1 << 0);
  1857. rtl_patchphy(tp, 0x14, 1 << 5);
  1858. rtl_patchphy(tp, 0x0d, 1 << 5);
  1859. rtl_writephy(tp, 0x1f, 0x0000);
  1860. }
  1861. static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
  1862. {
  1863. static const struct phy_reg phy_reg_init[] = {
  1864. { 0x1f, 0x0001 },
  1865. { 0x12, 0x2300 },
  1866. { 0x1d, 0x3d98 },
  1867. { 0x1f, 0x0002 },
  1868. { 0x0c, 0x7eb8 },
  1869. { 0x06, 0x5461 },
  1870. { 0x1f, 0x0003 },
  1871. { 0x16, 0x0f0a },
  1872. { 0x1f, 0x0000 }
  1873. };
  1874. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1875. rtl_patchphy(tp, 0x16, 1 << 0);
  1876. rtl_patchphy(tp, 0x14, 1 << 5);
  1877. rtl_patchphy(tp, 0x0d, 1 << 5);
  1878. rtl_writephy(tp, 0x1f, 0x0000);
  1879. }
  1880. static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
  1881. {
  1882. rtl8168c_3_hw_phy_config(tp);
  1883. }
  1884. static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
  1885. {
  1886. static const struct phy_reg phy_reg_init_0[] = {
  1887. /* Channel Estimation */
  1888. { 0x1f, 0x0001 },
  1889. { 0x06, 0x4064 },
  1890. { 0x07, 0x2863 },
  1891. { 0x08, 0x059c },
  1892. { 0x09, 0x26b4 },
  1893. { 0x0a, 0x6a19 },
  1894. { 0x0b, 0xdcc8 },
  1895. { 0x10, 0xf06d },
  1896. { 0x14, 0x7f68 },
  1897. { 0x18, 0x7fd9 },
  1898. { 0x1c, 0xf0ff },
  1899. { 0x1d, 0x3d9c },
  1900. { 0x1f, 0x0003 },
  1901. { 0x12, 0xf49f },
  1902. { 0x13, 0x070b },
  1903. { 0x1a, 0x05ad },
  1904. { 0x14, 0x94c0 },
  1905. /*
  1906. * Tx Error Issue
  1907. * enhance line driver power
  1908. */
  1909. { 0x1f, 0x0002 },
  1910. { 0x06, 0x5561 },
  1911. { 0x1f, 0x0005 },
  1912. { 0x05, 0x8332 },
  1913. { 0x06, 0x5561 },
  1914. /*
  1915. * Can not link to 1Gbps with bad cable
  1916. * Decrease SNR threshold form 21.07dB to 19.04dB
  1917. */
  1918. { 0x1f, 0x0001 },
  1919. { 0x17, 0x0cc0 },
  1920. { 0x1f, 0x0000 },
  1921. { 0x0d, 0xf880 }
  1922. };
  1923. void __iomem *ioaddr = tp->mmio_addr;
  1924. rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
  1925. /*
  1926. * Rx Error Issue
  1927. * Fine Tune Switching regulator parameter
  1928. */
  1929. rtl_writephy(tp, 0x1f, 0x0002);
  1930. rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
  1931. rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
  1932. if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
  1933. static const struct phy_reg phy_reg_init[] = {
  1934. { 0x1f, 0x0002 },
  1935. { 0x05, 0x669a },
  1936. { 0x1f, 0x0005 },
  1937. { 0x05, 0x8330 },
  1938. { 0x06, 0x669a },
  1939. { 0x1f, 0x0002 }
  1940. };
  1941. int val;
  1942. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1943. val = rtl_readphy(tp, 0x0d);
  1944. if ((val & 0x00ff) != 0x006c) {
  1945. static const u32 set[] = {
  1946. 0x0065, 0x0066, 0x0067, 0x0068,
  1947. 0x0069, 0x006a, 0x006b, 0x006c
  1948. };
  1949. int i;
  1950. rtl_writephy(tp, 0x1f, 0x0002);
  1951. val &= 0xff00;
  1952. for (i = 0; i < ARRAY_SIZE(set); i++)
  1953. rtl_writephy(tp, 0x0d, val | set[i]);
  1954. }
  1955. } else {
  1956. static const struct phy_reg phy_reg_init[] = {
  1957. { 0x1f, 0x0002 },
  1958. { 0x05, 0x6662 },
  1959. { 0x1f, 0x0005 },
  1960. { 0x05, 0x8330 },
  1961. { 0x06, 0x6662 }
  1962. };
  1963. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1964. }
  1965. /* RSET couple improve */
  1966. rtl_writephy(tp, 0x1f, 0x0002);
  1967. rtl_patchphy(tp, 0x0d, 0x0300);
  1968. rtl_patchphy(tp, 0x0f, 0x0010);
  1969. /* Fine tune PLL performance */
  1970. rtl_writephy(tp, 0x1f, 0x0002);
  1971. rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
  1972. rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
  1973. rtl_writephy(tp, 0x1f, 0x0005);
  1974. rtl_writephy(tp, 0x05, 0x001b);
  1975. rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
  1976. rtl_writephy(tp, 0x1f, 0x0000);
  1977. }
  1978. static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
  1979. {
  1980. static const struct phy_reg phy_reg_init_0[] = {
  1981. /* Channel Estimation */
  1982. { 0x1f, 0x0001 },
  1983. { 0x06, 0x4064 },
  1984. { 0x07, 0x2863 },
  1985. { 0x08, 0x059c },
  1986. { 0x09, 0x26b4 },
  1987. { 0x0a, 0x6a19 },
  1988. { 0x0b, 0xdcc8 },
  1989. { 0x10, 0xf06d },
  1990. { 0x14, 0x7f68 },
  1991. { 0x18, 0x7fd9 },
  1992. { 0x1c, 0xf0ff },
  1993. { 0x1d, 0x3d9c },
  1994. { 0x1f, 0x0003 },
  1995. { 0x12, 0xf49f },
  1996. { 0x13, 0x070b },
  1997. { 0x1a, 0x05ad },
  1998. { 0x14, 0x94c0 },
  1999. /*
  2000. * Tx Error Issue
  2001. * enhance line driver power
  2002. */
  2003. { 0x1f, 0x0002 },
  2004. { 0x06, 0x5561 },
  2005. { 0x1f, 0x0005 },
  2006. { 0x05, 0x8332 },
  2007. { 0x06, 0x5561 },
  2008. /*
  2009. * Can not link to 1Gbps with bad cable
  2010. * Decrease SNR threshold form 21.07dB to 19.04dB
  2011. */
  2012. { 0x1f, 0x0001 },
  2013. { 0x17, 0x0cc0 },
  2014. { 0x1f, 0x0000 },
  2015. { 0x0d, 0xf880 }
  2016. };
  2017. void __iomem *ioaddr = tp->mmio_addr;
  2018. rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
  2019. if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
  2020. static const struct phy_reg phy_reg_init[] = {
  2021. { 0x1f, 0x0002 },
  2022. { 0x05, 0x669a },
  2023. { 0x1f, 0x0005 },
  2024. { 0x05, 0x8330 },
  2025. { 0x06, 0x669a },
  2026. { 0x1f, 0x0002 }
  2027. };
  2028. int val;
  2029. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2030. val = rtl_readphy(tp, 0x0d);
  2031. if ((val & 0x00ff) != 0x006c) {
  2032. static const u32 set[] = {
  2033. 0x0065, 0x0066, 0x0067, 0x0068,
  2034. 0x0069, 0x006a, 0x006b, 0x006c
  2035. };
  2036. int i;
  2037. rtl_writephy(tp, 0x1f, 0x0002);
  2038. val &= 0xff00;
  2039. for (i = 0; i < ARRAY_SIZE(set); i++)
  2040. rtl_writephy(tp, 0x0d, val | set[i]);
  2041. }
  2042. } else {
  2043. static const struct phy_reg phy_reg_init[] = {
  2044. { 0x1f, 0x0002 },
  2045. { 0x05, 0x2642 },
  2046. { 0x1f, 0x0005 },
  2047. { 0x05, 0x8330 },
  2048. { 0x06, 0x2642 }
  2049. };
  2050. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2051. }
  2052. /* Fine tune PLL performance */
  2053. rtl_writephy(tp, 0x1f, 0x0002);
  2054. rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
  2055. rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
  2056. /* Switching regulator Slew rate */
  2057. rtl_writephy(tp, 0x1f, 0x0002);
  2058. rtl_patchphy(tp, 0x0f, 0x0017);
  2059. rtl_writephy(tp, 0x1f, 0x0005);
  2060. rtl_writephy(tp, 0x05, 0x001b);
  2061. rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
  2062. rtl_writephy(tp, 0x1f, 0x0000);
  2063. }
  2064. static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
  2065. {
  2066. static const struct phy_reg phy_reg_init[] = {
  2067. { 0x1f, 0x0002 },
  2068. { 0x10, 0x0008 },
  2069. { 0x0d, 0x006c },
  2070. { 0x1f, 0x0000 },
  2071. { 0x0d, 0xf880 },
  2072. { 0x1f, 0x0001 },
  2073. { 0x17, 0x0cc0 },
  2074. { 0x1f, 0x0001 },
  2075. { 0x0b, 0xa4d8 },
  2076. { 0x09, 0x281c },
  2077. { 0x07, 0x2883 },
  2078. { 0x0a, 0x6b35 },
  2079. { 0x1d, 0x3da4 },
  2080. { 0x1c, 0xeffd },
  2081. { 0x14, 0x7f52 },
  2082. { 0x18, 0x7fc6 },
  2083. { 0x08, 0x0601 },
  2084. { 0x06, 0x4063 },
  2085. { 0x10, 0xf074 },
  2086. { 0x1f, 0x0003 },
  2087. { 0x13, 0x0789 },
  2088. { 0x12, 0xf4bd },
  2089. { 0x1a, 0x04fd },
  2090. { 0x14, 0x84b0 },
  2091. { 0x1f, 0x0000 },
  2092. { 0x00, 0x9200 },
  2093. { 0x1f, 0x0005 },
  2094. { 0x01, 0x0340 },
  2095. { 0x1f, 0x0001 },
  2096. { 0x04, 0x4000 },
  2097. { 0x03, 0x1d21 },
  2098. { 0x02, 0x0c32 },
  2099. { 0x01, 0x0200 },
  2100. { 0x00, 0x5554 },
  2101. { 0x04, 0x4800 },
  2102. { 0x04, 0x4000 },
  2103. { 0x04, 0xf000 },
  2104. { 0x03, 0xdf01 },
  2105. { 0x02, 0xdf20 },
  2106. { 0x01, 0x101a },
  2107. { 0x00, 0xa0ff },
  2108. { 0x04, 0xf800 },
  2109. { 0x04, 0xf000 },
  2110. { 0x1f, 0x0000 },
  2111. { 0x1f, 0x0007 },
  2112. { 0x1e, 0x0023 },
  2113. { 0x16, 0x0000 },
  2114. { 0x1f, 0x0000 }
  2115. };
  2116. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2117. }
  2118. static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
  2119. {
  2120. static const struct phy_reg phy_reg_init[] = {
  2121. { 0x1f, 0x0001 },
  2122. { 0x17, 0x0cc0 },
  2123. { 0x1f, 0x0007 },
  2124. { 0x1e, 0x002d },
  2125. { 0x18, 0x0040 },
  2126. { 0x1f, 0x0000 }
  2127. };
  2128. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2129. rtl_patchphy(tp, 0x0d, 1 << 5);
  2130. }
  2131. static void rtl8168e_hw_phy_config(struct rtl8169_private *tp)
  2132. {
  2133. static const struct phy_reg phy_reg_init[] = {
  2134. /* Enable Delay cap */
  2135. { 0x1f, 0x0005 },
  2136. { 0x05, 0x8b80 },
  2137. { 0x06, 0xc896 },
  2138. { 0x1f, 0x0000 },
  2139. /* Channel estimation fine tune */
  2140. { 0x1f, 0x0001 },
  2141. { 0x0b, 0x6c20 },
  2142. { 0x07, 0x2872 },
  2143. { 0x1c, 0xefff },
  2144. { 0x1f, 0x0003 },
  2145. { 0x14, 0x6420 },
  2146. { 0x1f, 0x0000 },
  2147. /* Update PFM & 10M TX idle timer */
  2148. { 0x1f, 0x0007 },
  2149. { 0x1e, 0x002f },
  2150. { 0x15, 0x1919 },
  2151. { 0x1f, 0x0000 },
  2152. { 0x1f, 0x0007 },
  2153. { 0x1e, 0x00ac },
  2154. { 0x18, 0x0006 },
  2155. { 0x1f, 0x0000 }
  2156. };
  2157. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2158. /* DCO enable for 10M IDLE Power */
  2159. rtl_writephy(tp, 0x1f, 0x0007);
  2160. rtl_writephy(tp, 0x1e, 0x0023);
  2161. rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
  2162. rtl_writephy(tp, 0x1f, 0x0000);
  2163. /* For impedance matching */
  2164. rtl_writephy(tp, 0x1f, 0x0002);
  2165. rtl_w1w0_phy(tp, 0x08, 0x8000, 0x7f00);
  2166. rtl_writephy(tp, 0x1F, 0x0000);
  2167. /* PHY auto speed down */
  2168. rtl_writephy(tp, 0x1f, 0x0007);
  2169. rtl_writephy(tp, 0x1e, 0x002d);
  2170. rtl_w1w0_phy(tp, 0x18, 0x0050, 0x0000);
  2171. rtl_writephy(tp, 0x1f, 0x0000);
  2172. rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
  2173. rtl_writephy(tp, 0x1f, 0x0005);
  2174. rtl_writephy(tp, 0x05, 0x8b86);
  2175. rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
  2176. rtl_writephy(tp, 0x1f, 0x0000);
  2177. rtl_writephy(tp, 0x1f, 0x0005);
  2178. rtl_writephy(tp, 0x05, 0x8b85);
  2179. rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
  2180. rtl_writephy(tp, 0x1f, 0x0007);
  2181. rtl_writephy(tp, 0x1e, 0x0020);
  2182. rtl_w1w0_phy(tp, 0x15, 0x0000, 0x1100);
  2183. rtl_writephy(tp, 0x1f, 0x0006);
  2184. rtl_writephy(tp, 0x00, 0x5a00);
  2185. rtl_writephy(tp, 0x1f, 0x0000);
  2186. rtl_writephy(tp, 0x0d, 0x0007);
  2187. rtl_writephy(tp, 0x0e, 0x003c);
  2188. rtl_writephy(tp, 0x0d, 0x4007);
  2189. rtl_writephy(tp, 0x0e, 0x0000);
  2190. rtl_writephy(tp, 0x0d, 0x0000);
  2191. }
  2192. static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
  2193. {
  2194. rtl_apply_firmware(tp);
  2195. rtl8168e_hw_phy_config(tp);
  2196. }
  2197. static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
  2198. {
  2199. rtl_apply_firmware(tp);
  2200. rtl8168e_hw_phy_config(tp);
  2201. }
  2202. static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
  2203. {
  2204. static const struct phy_reg phy_reg_init[] = {
  2205. { 0x1f, 0x0003 },
  2206. { 0x08, 0x441d },
  2207. { 0x01, 0x9100 },
  2208. { 0x1f, 0x0000 }
  2209. };
  2210. rtl_writephy(tp, 0x1f, 0x0000);
  2211. rtl_patchphy(tp, 0x11, 1 << 12);
  2212. rtl_patchphy(tp, 0x19, 1 << 13);
  2213. rtl_patchphy(tp, 0x10, 1 << 15);
  2214. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2215. }
  2216. static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
  2217. {
  2218. static const struct phy_reg phy_reg_init[] = {
  2219. { 0x1f, 0x0005 },
  2220. { 0x1a, 0x0000 },
  2221. { 0x1f, 0x0000 },
  2222. { 0x1f, 0x0004 },
  2223. { 0x1c, 0x0000 },
  2224. { 0x1f, 0x0000 },
  2225. { 0x1f, 0x0001 },
  2226. { 0x15, 0x7701 },
  2227. { 0x1f, 0x0000 }
  2228. };
  2229. /* Disable ALDPS before ram code */
  2230. rtl_writephy(tp, 0x1f, 0x0000);
  2231. rtl_writephy(tp, 0x18, 0x0310);
  2232. msleep(100);
  2233. rtl_apply_firmware(tp);
  2234. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2235. }
  2236. static void rtl_hw_phy_config(struct net_device *dev)
  2237. {
  2238. struct rtl8169_private *tp = netdev_priv(dev);
  2239. rtl8169_print_mac_version(tp);
  2240. switch (tp->mac_version) {
  2241. case RTL_GIGA_MAC_VER_01:
  2242. break;
  2243. case RTL_GIGA_MAC_VER_02:
  2244. case RTL_GIGA_MAC_VER_03:
  2245. rtl8169s_hw_phy_config(tp);
  2246. break;
  2247. case RTL_GIGA_MAC_VER_04:
  2248. rtl8169sb_hw_phy_config(tp);
  2249. break;
  2250. case RTL_GIGA_MAC_VER_05:
  2251. rtl8169scd_hw_phy_config(tp);
  2252. break;
  2253. case RTL_GIGA_MAC_VER_06:
  2254. rtl8169sce_hw_phy_config(tp);
  2255. break;
  2256. case RTL_GIGA_MAC_VER_07:
  2257. case RTL_GIGA_MAC_VER_08:
  2258. case RTL_GIGA_MAC_VER_09:
  2259. rtl8102e_hw_phy_config(tp);
  2260. break;
  2261. case RTL_GIGA_MAC_VER_11:
  2262. rtl8168bb_hw_phy_config(tp);
  2263. break;
  2264. case RTL_GIGA_MAC_VER_12:
  2265. rtl8168bef_hw_phy_config(tp);
  2266. break;
  2267. case RTL_GIGA_MAC_VER_17:
  2268. rtl8168bef_hw_phy_config(tp);
  2269. break;
  2270. case RTL_GIGA_MAC_VER_18:
  2271. rtl8168cp_1_hw_phy_config(tp);
  2272. break;
  2273. case RTL_GIGA_MAC_VER_19:
  2274. rtl8168c_1_hw_phy_config(tp);
  2275. break;
  2276. case RTL_GIGA_MAC_VER_20:
  2277. rtl8168c_2_hw_phy_config(tp);
  2278. break;
  2279. case RTL_GIGA_MAC_VER_21:
  2280. rtl8168c_3_hw_phy_config(tp);
  2281. break;
  2282. case RTL_GIGA_MAC_VER_22:
  2283. rtl8168c_4_hw_phy_config(tp);
  2284. break;
  2285. case RTL_GIGA_MAC_VER_23:
  2286. case RTL_GIGA_MAC_VER_24:
  2287. rtl8168cp_2_hw_phy_config(tp);
  2288. break;
  2289. case RTL_GIGA_MAC_VER_25:
  2290. rtl8168d_1_hw_phy_config(tp);
  2291. break;
  2292. case RTL_GIGA_MAC_VER_26:
  2293. rtl8168d_2_hw_phy_config(tp);
  2294. break;
  2295. case RTL_GIGA_MAC_VER_27:
  2296. rtl8168d_3_hw_phy_config(tp);
  2297. break;
  2298. case RTL_GIGA_MAC_VER_28:
  2299. rtl8168d_4_hw_phy_config(tp);
  2300. break;
  2301. case RTL_GIGA_MAC_VER_29:
  2302. case RTL_GIGA_MAC_VER_30:
  2303. rtl8105e_hw_phy_config(tp);
  2304. break;
  2305. case RTL_GIGA_MAC_VER_32:
  2306. rtl8168e_1_hw_phy_config(tp);
  2307. break;
  2308. case RTL_GIGA_MAC_VER_33:
  2309. rtl8168e_2_hw_phy_config(tp);
  2310. break;
  2311. default:
  2312. break;
  2313. }
  2314. }
  2315. static void rtl8169_phy_timer(unsigned long __opaque)
  2316. {
  2317. struct net_device *dev = (struct net_device *)__opaque;
  2318. struct rtl8169_private *tp = netdev_priv(dev);
  2319. struct timer_list *timer = &tp->timer;
  2320. void __iomem *ioaddr = tp->mmio_addr;
  2321. unsigned long timeout = RTL8169_PHY_TIMEOUT;
  2322. assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
  2323. if (!(tp->phy_1000_ctrl_reg & ADVERTISE_1000FULL))
  2324. return;
  2325. spin_lock_irq(&tp->lock);
  2326. if (tp->phy_reset_pending(tp)) {
  2327. /*
  2328. * A busy loop could burn quite a few cycles on nowadays CPU.
  2329. * Let's delay the execution of the timer for a few ticks.
  2330. */
  2331. timeout = HZ/10;
  2332. goto out_mod_timer;
  2333. }
  2334. if (tp->link_ok(ioaddr))
  2335. goto out_unlock;
  2336. netif_warn(tp, link, dev, "PHY reset until link up\n");
  2337. tp->phy_reset_enable(tp);
  2338. out_mod_timer:
  2339. mod_timer(timer, jiffies + timeout);
  2340. out_unlock:
  2341. spin_unlock_irq(&tp->lock);
  2342. }
  2343. static inline void rtl8169_delete_timer(struct net_device *dev)
  2344. {
  2345. struct rtl8169_private *tp = netdev_priv(dev);
  2346. struct timer_list *timer = &tp->timer;
  2347. if (tp->mac_version <= RTL_GIGA_MAC_VER_01)
  2348. return;
  2349. del_timer_sync(timer);
  2350. }
  2351. static inline void rtl8169_request_timer(struct net_device *dev)
  2352. {
  2353. struct rtl8169_private *tp = netdev_priv(dev);
  2354. struct timer_list *timer = &tp->timer;
  2355. if (tp->mac_version <= RTL_GIGA_MAC_VER_01)
  2356. return;
  2357. mod_timer(timer, jiffies + RTL8169_PHY_TIMEOUT);
  2358. }
  2359. #ifdef CONFIG_NET_POLL_CONTROLLER
  2360. /*
  2361. * Polling 'interrupt' - used by things like netconsole to send skbs
  2362. * without having to re-enable interrupts. It's not called while
  2363. * the interrupt routine is executing.
  2364. */
  2365. static void rtl8169_netpoll(struct net_device *dev)
  2366. {
  2367. struct rtl8169_private *tp = netdev_priv(dev);
  2368. struct pci_dev *pdev = tp->pci_dev;
  2369. disable_irq(pdev->irq);
  2370. rtl8169_interrupt(pdev->irq, dev);
  2371. enable_irq(pdev->irq);
  2372. }
  2373. #endif
  2374. static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
  2375. void __iomem *ioaddr)
  2376. {
  2377. iounmap(ioaddr);
  2378. pci_release_regions(pdev);
  2379. pci_clear_mwi(pdev);
  2380. pci_disable_device(pdev);
  2381. free_netdev(dev);
  2382. }
  2383. static void rtl8169_phy_reset(struct net_device *dev,
  2384. struct rtl8169_private *tp)
  2385. {
  2386. unsigned int i;
  2387. tp->phy_reset_enable(tp);
  2388. for (i = 0; i < 100; i++) {
  2389. if (!tp->phy_reset_pending(tp))
  2390. return;
  2391. msleep(1);
  2392. }
  2393. netif_err(tp, link, dev, "PHY reset failed\n");
  2394. }
  2395. static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
  2396. {
  2397. void __iomem *ioaddr = tp->mmio_addr;
  2398. rtl_hw_phy_config(dev);
  2399. if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
  2400. dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
  2401. RTL_W8(0x82, 0x01);
  2402. }
  2403. pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
  2404. if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
  2405. pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
  2406. if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
  2407. dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
  2408. RTL_W8(0x82, 0x01);
  2409. dprintk("Set PHY Reg 0x0bh = 0x00h\n");
  2410. rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
  2411. }
  2412. rtl8169_phy_reset(dev, tp);
  2413. rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
  2414. ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
  2415. ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
  2416. (tp->mii.supports_gmii ?
  2417. ADVERTISED_1000baseT_Half |
  2418. ADVERTISED_1000baseT_Full : 0));
  2419. if (RTL_R8(PHYstatus) & TBI_Enable)
  2420. netif_info(tp, link, dev, "TBI auto-negotiating\n");
  2421. }
  2422. static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
  2423. {
  2424. void __iomem *ioaddr = tp->mmio_addr;
  2425. u32 high;
  2426. u32 low;
  2427. low = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
  2428. high = addr[4] | (addr[5] << 8);
  2429. spin_lock_irq(&tp->lock);
  2430. RTL_W8(Cfg9346, Cfg9346_Unlock);
  2431. RTL_W32(MAC4, high);
  2432. RTL_R32(MAC4);
  2433. RTL_W32(MAC0, low);
  2434. RTL_R32(MAC0);
  2435. RTL_W8(Cfg9346, Cfg9346_Lock);
  2436. spin_unlock_irq(&tp->lock);
  2437. }
  2438. static int rtl_set_mac_address(struct net_device *dev, void *p)
  2439. {
  2440. struct rtl8169_private *tp = netdev_priv(dev);
  2441. struct sockaddr *addr = p;
  2442. if (!is_valid_ether_addr(addr->sa_data))
  2443. return -EADDRNOTAVAIL;
  2444. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  2445. rtl_rar_set(tp, dev->dev_addr);
  2446. return 0;
  2447. }
  2448. static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  2449. {
  2450. struct rtl8169_private *tp = netdev_priv(dev);
  2451. struct mii_ioctl_data *data = if_mii(ifr);
  2452. return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
  2453. }
  2454. static int rtl_xmii_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
  2455. {
  2456. switch (cmd) {
  2457. case SIOCGMIIPHY:
  2458. data->phy_id = 32; /* Internal PHY */
  2459. return 0;
  2460. case SIOCGMIIREG:
  2461. data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
  2462. return 0;
  2463. case SIOCSMIIREG:
  2464. rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
  2465. return 0;
  2466. }
  2467. return -EOPNOTSUPP;
  2468. }
  2469. static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
  2470. {
  2471. return -EOPNOTSUPP;
  2472. }
  2473. static const struct rtl_cfg_info {
  2474. void (*hw_start)(struct net_device *);
  2475. unsigned int region;
  2476. unsigned int align;
  2477. u16 intr_event;
  2478. u16 napi_event;
  2479. unsigned features;
  2480. u8 default_ver;
  2481. } rtl_cfg_infos [] = {
  2482. [RTL_CFG_0] = {
  2483. .hw_start = rtl_hw_start_8169,
  2484. .region = 1,
  2485. .align = 0,
  2486. .intr_event = SYSErr | LinkChg | RxOverflow |
  2487. RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
  2488. .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
  2489. .features = RTL_FEATURE_GMII,
  2490. .default_ver = RTL_GIGA_MAC_VER_01,
  2491. },
  2492. [RTL_CFG_1] = {
  2493. .hw_start = rtl_hw_start_8168,
  2494. .region = 2,
  2495. .align = 8,
  2496. .intr_event = SYSErr | LinkChg | RxOverflow |
  2497. TxErr | TxOK | RxOK | RxErr,
  2498. .napi_event = TxErr | TxOK | RxOK | RxOverflow,
  2499. .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
  2500. .default_ver = RTL_GIGA_MAC_VER_11,
  2501. },
  2502. [RTL_CFG_2] = {
  2503. .hw_start = rtl_hw_start_8101,
  2504. .region = 2,
  2505. .align = 8,
  2506. .intr_event = SYSErr | LinkChg | RxOverflow | PCSTimeout |
  2507. RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
  2508. .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
  2509. .features = RTL_FEATURE_MSI,
  2510. .default_ver = RTL_GIGA_MAC_VER_13,
  2511. }
  2512. };
  2513. /* Cfg9346_Unlock assumed. */
  2514. static unsigned rtl_try_msi(struct pci_dev *pdev, void __iomem *ioaddr,
  2515. const struct rtl_cfg_info *cfg)
  2516. {
  2517. unsigned msi = 0;
  2518. u8 cfg2;
  2519. cfg2 = RTL_R8(Config2) & ~MSIEnable;
  2520. if (cfg->features & RTL_FEATURE_MSI) {
  2521. if (pci_enable_msi(pdev)) {
  2522. dev_info(&pdev->dev, "no MSI. Back to INTx.\n");
  2523. } else {
  2524. cfg2 |= MSIEnable;
  2525. msi = RTL_FEATURE_MSI;
  2526. }
  2527. }
  2528. RTL_W8(Config2, cfg2);
  2529. return msi;
  2530. }
  2531. static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
  2532. {
  2533. if (tp->features & RTL_FEATURE_MSI) {
  2534. pci_disable_msi(pdev);
  2535. tp->features &= ~RTL_FEATURE_MSI;
  2536. }
  2537. }
  2538. static const struct net_device_ops rtl8169_netdev_ops = {
  2539. .ndo_open = rtl8169_open,
  2540. .ndo_stop = rtl8169_close,
  2541. .ndo_get_stats = rtl8169_get_stats,
  2542. .ndo_start_xmit = rtl8169_start_xmit,
  2543. .ndo_tx_timeout = rtl8169_tx_timeout,
  2544. .ndo_validate_addr = eth_validate_addr,
  2545. .ndo_change_mtu = rtl8169_change_mtu,
  2546. .ndo_fix_features = rtl8169_fix_features,
  2547. .ndo_set_features = rtl8169_set_features,
  2548. .ndo_set_mac_address = rtl_set_mac_address,
  2549. .ndo_do_ioctl = rtl8169_ioctl,
  2550. .ndo_set_multicast_list = rtl_set_rx_mode,
  2551. #ifdef CONFIG_NET_POLL_CONTROLLER
  2552. .ndo_poll_controller = rtl8169_netpoll,
  2553. #endif
  2554. };
  2555. static void __devinit rtl_init_mdio_ops(struct rtl8169_private *tp)
  2556. {
  2557. struct mdio_ops *ops = &tp->mdio_ops;
  2558. switch (tp->mac_version) {
  2559. case RTL_GIGA_MAC_VER_27:
  2560. ops->write = r8168dp_1_mdio_write;
  2561. ops->read = r8168dp_1_mdio_read;
  2562. break;
  2563. case RTL_GIGA_MAC_VER_28:
  2564. case RTL_GIGA_MAC_VER_31:
  2565. ops->write = r8168dp_2_mdio_write;
  2566. ops->read = r8168dp_2_mdio_read;
  2567. break;
  2568. default:
  2569. ops->write = r8169_mdio_write;
  2570. ops->read = r8169_mdio_read;
  2571. break;
  2572. }
  2573. }
  2574. static void r810x_phy_power_down(struct rtl8169_private *tp)
  2575. {
  2576. rtl_writephy(tp, 0x1f, 0x0000);
  2577. rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
  2578. }
  2579. static void r810x_phy_power_up(struct rtl8169_private *tp)
  2580. {
  2581. rtl_writephy(tp, 0x1f, 0x0000);
  2582. rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
  2583. }
  2584. static void r810x_pll_power_down(struct rtl8169_private *tp)
  2585. {
  2586. if (__rtl8169_get_wol(tp) & WAKE_ANY) {
  2587. rtl_writephy(tp, 0x1f, 0x0000);
  2588. rtl_writephy(tp, MII_BMCR, 0x0000);
  2589. return;
  2590. }
  2591. r810x_phy_power_down(tp);
  2592. }
  2593. static void r810x_pll_power_up(struct rtl8169_private *tp)
  2594. {
  2595. r810x_phy_power_up(tp);
  2596. }
  2597. static void r8168_phy_power_up(struct rtl8169_private *tp)
  2598. {
  2599. rtl_writephy(tp, 0x1f, 0x0000);
  2600. switch (tp->mac_version) {
  2601. case RTL_GIGA_MAC_VER_11:
  2602. case RTL_GIGA_MAC_VER_12:
  2603. case RTL_GIGA_MAC_VER_17:
  2604. case RTL_GIGA_MAC_VER_18:
  2605. case RTL_GIGA_MAC_VER_19:
  2606. case RTL_GIGA_MAC_VER_20:
  2607. case RTL_GIGA_MAC_VER_21:
  2608. case RTL_GIGA_MAC_VER_22:
  2609. case RTL_GIGA_MAC_VER_23:
  2610. case RTL_GIGA_MAC_VER_24:
  2611. case RTL_GIGA_MAC_VER_25:
  2612. case RTL_GIGA_MAC_VER_26:
  2613. case RTL_GIGA_MAC_VER_27:
  2614. case RTL_GIGA_MAC_VER_28:
  2615. case RTL_GIGA_MAC_VER_31:
  2616. rtl_writephy(tp, 0x0e, 0x0000);
  2617. break;
  2618. default:
  2619. break;
  2620. }
  2621. rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
  2622. }
  2623. static void r8168_phy_power_down(struct rtl8169_private *tp)
  2624. {
  2625. rtl_writephy(tp, 0x1f, 0x0000);
  2626. switch (tp->mac_version) {
  2627. case RTL_GIGA_MAC_VER_32:
  2628. case RTL_GIGA_MAC_VER_33:
  2629. rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
  2630. break;
  2631. case RTL_GIGA_MAC_VER_11:
  2632. case RTL_GIGA_MAC_VER_12:
  2633. case RTL_GIGA_MAC_VER_17:
  2634. case RTL_GIGA_MAC_VER_18:
  2635. case RTL_GIGA_MAC_VER_19:
  2636. case RTL_GIGA_MAC_VER_20:
  2637. case RTL_GIGA_MAC_VER_21:
  2638. case RTL_GIGA_MAC_VER_22:
  2639. case RTL_GIGA_MAC_VER_23:
  2640. case RTL_GIGA_MAC_VER_24:
  2641. case RTL_GIGA_MAC_VER_25:
  2642. case RTL_GIGA_MAC_VER_26:
  2643. case RTL_GIGA_MAC_VER_27:
  2644. case RTL_GIGA_MAC_VER_28:
  2645. case RTL_GIGA_MAC_VER_31:
  2646. rtl_writephy(tp, 0x0e, 0x0200);
  2647. default:
  2648. rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
  2649. break;
  2650. }
  2651. }
  2652. static void r8168_pll_power_down(struct rtl8169_private *tp)
  2653. {
  2654. void __iomem *ioaddr = tp->mmio_addr;
  2655. if (((tp->mac_version == RTL_GIGA_MAC_VER_27) ||
  2656. (tp->mac_version == RTL_GIGA_MAC_VER_28) ||
  2657. (tp->mac_version == RTL_GIGA_MAC_VER_31)) &&
  2658. r8168dp_check_dash(tp)) {
  2659. return;
  2660. }
  2661. if (((tp->mac_version == RTL_GIGA_MAC_VER_23) ||
  2662. (tp->mac_version == RTL_GIGA_MAC_VER_24)) &&
  2663. (RTL_R16(CPlusCmd) & ASF)) {
  2664. return;
  2665. }
  2666. if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
  2667. tp->mac_version == RTL_GIGA_MAC_VER_33)
  2668. rtl_ephy_write(ioaddr, 0x19, 0xff64);
  2669. if (__rtl8169_get_wol(tp) & WAKE_ANY) {
  2670. rtl_writephy(tp, 0x1f, 0x0000);
  2671. rtl_writephy(tp, MII_BMCR, 0x0000);
  2672. RTL_W32(RxConfig, RTL_R32(RxConfig) |
  2673. AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
  2674. return;
  2675. }
  2676. r8168_phy_power_down(tp);
  2677. switch (tp->mac_version) {
  2678. case RTL_GIGA_MAC_VER_25:
  2679. case RTL_GIGA_MAC_VER_26:
  2680. case RTL_GIGA_MAC_VER_27:
  2681. case RTL_GIGA_MAC_VER_28:
  2682. case RTL_GIGA_MAC_VER_31:
  2683. case RTL_GIGA_MAC_VER_32:
  2684. case RTL_GIGA_MAC_VER_33:
  2685. RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
  2686. break;
  2687. }
  2688. }
  2689. static void r8168_pll_power_up(struct rtl8169_private *tp)
  2690. {
  2691. void __iomem *ioaddr = tp->mmio_addr;
  2692. if (((tp->mac_version == RTL_GIGA_MAC_VER_27) ||
  2693. (tp->mac_version == RTL_GIGA_MAC_VER_28) ||
  2694. (tp->mac_version == RTL_GIGA_MAC_VER_31)) &&
  2695. r8168dp_check_dash(tp)) {
  2696. return;
  2697. }
  2698. switch (tp->mac_version) {
  2699. case RTL_GIGA_MAC_VER_25:
  2700. case RTL_GIGA_MAC_VER_26:
  2701. case RTL_GIGA_MAC_VER_27:
  2702. case RTL_GIGA_MAC_VER_28:
  2703. case RTL_GIGA_MAC_VER_31:
  2704. case RTL_GIGA_MAC_VER_32:
  2705. case RTL_GIGA_MAC_VER_33:
  2706. RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
  2707. break;
  2708. }
  2709. r8168_phy_power_up(tp);
  2710. }
  2711. static void rtl_pll_power_op(struct rtl8169_private *tp,
  2712. void (*op)(struct rtl8169_private *))
  2713. {
  2714. if (op)
  2715. op(tp);
  2716. }
  2717. static void rtl_pll_power_down(struct rtl8169_private *tp)
  2718. {
  2719. rtl_pll_power_op(tp, tp->pll_power_ops.down);
  2720. }
  2721. static void rtl_pll_power_up(struct rtl8169_private *tp)
  2722. {
  2723. rtl_pll_power_op(tp, tp->pll_power_ops.up);
  2724. }
  2725. static void __devinit rtl_init_pll_power_ops(struct rtl8169_private *tp)
  2726. {
  2727. struct pll_power_ops *ops = &tp->pll_power_ops;
  2728. switch (tp->mac_version) {
  2729. case RTL_GIGA_MAC_VER_07:
  2730. case RTL_GIGA_MAC_VER_08:
  2731. case RTL_GIGA_MAC_VER_09:
  2732. case RTL_GIGA_MAC_VER_10:
  2733. case RTL_GIGA_MAC_VER_16:
  2734. case RTL_GIGA_MAC_VER_29:
  2735. case RTL_GIGA_MAC_VER_30:
  2736. ops->down = r810x_pll_power_down;
  2737. ops->up = r810x_pll_power_up;
  2738. break;
  2739. case RTL_GIGA_MAC_VER_11:
  2740. case RTL_GIGA_MAC_VER_12:
  2741. case RTL_GIGA_MAC_VER_17:
  2742. case RTL_GIGA_MAC_VER_18:
  2743. case RTL_GIGA_MAC_VER_19:
  2744. case RTL_GIGA_MAC_VER_20:
  2745. case RTL_GIGA_MAC_VER_21:
  2746. case RTL_GIGA_MAC_VER_22:
  2747. case RTL_GIGA_MAC_VER_23:
  2748. case RTL_GIGA_MAC_VER_24:
  2749. case RTL_GIGA_MAC_VER_25:
  2750. case RTL_GIGA_MAC_VER_26:
  2751. case RTL_GIGA_MAC_VER_27:
  2752. case RTL_GIGA_MAC_VER_28:
  2753. case RTL_GIGA_MAC_VER_31:
  2754. case RTL_GIGA_MAC_VER_32:
  2755. case RTL_GIGA_MAC_VER_33:
  2756. ops->down = r8168_pll_power_down;
  2757. ops->up = r8168_pll_power_up;
  2758. break;
  2759. default:
  2760. ops->down = NULL;
  2761. ops->up = NULL;
  2762. break;
  2763. }
  2764. }
  2765. static int __devinit
  2766. rtl8169_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  2767. {
  2768. const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
  2769. const unsigned int region = cfg->region;
  2770. struct rtl8169_private *tp;
  2771. struct mii_if_info *mii;
  2772. struct net_device *dev;
  2773. void __iomem *ioaddr;
  2774. int chipset, i;
  2775. int rc;
  2776. if (netif_msg_drv(&debug)) {
  2777. printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
  2778. MODULENAME, RTL8169_VERSION);
  2779. }
  2780. dev = alloc_etherdev(sizeof (*tp));
  2781. if (!dev) {
  2782. if (netif_msg_drv(&debug))
  2783. dev_err(&pdev->dev, "unable to alloc new ethernet\n");
  2784. rc = -ENOMEM;
  2785. goto out;
  2786. }
  2787. SET_NETDEV_DEV(dev, &pdev->dev);
  2788. dev->netdev_ops = &rtl8169_netdev_ops;
  2789. tp = netdev_priv(dev);
  2790. tp->dev = dev;
  2791. tp->pci_dev = pdev;
  2792. tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
  2793. mii = &tp->mii;
  2794. mii->dev = dev;
  2795. mii->mdio_read = rtl_mdio_read;
  2796. mii->mdio_write = rtl_mdio_write;
  2797. mii->phy_id_mask = 0x1f;
  2798. mii->reg_num_mask = 0x1f;
  2799. mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
  2800. /* disable ASPM completely as that cause random device stop working
  2801. * problems as well as full system hangs for some PCIe devices users */
  2802. pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
  2803. PCIE_LINK_STATE_CLKPM);
  2804. /* enable device (incl. PCI PM wakeup and hotplug setup) */
  2805. rc = pci_enable_device(pdev);
  2806. if (rc < 0) {
  2807. netif_err(tp, probe, dev, "enable failure\n");
  2808. goto err_out_free_dev_1;
  2809. }
  2810. if (pci_set_mwi(pdev) < 0)
  2811. netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
  2812. /* make sure PCI base addr 1 is MMIO */
  2813. if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
  2814. netif_err(tp, probe, dev,
  2815. "region #%d not an MMIO resource, aborting\n",
  2816. region);
  2817. rc = -ENODEV;
  2818. goto err_out_mwi_2;
  2819. }
  2820. /* check for weird/broken PCI region reporting */
  2821. if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
  2822. netif_err(tp, probe, dev,
  2823. "Invalid PCI region size(s), aborting\n");
  2824. rc = -ENODEV;
  2825. goto err_out_mwi_2;
  2826. }
  2827. rc = pci_request_regions(pdev, MODULENAME);
  2828. if (rc < 0) {
  2829. netif_err(tp, probe, dev, "could not request regions\n");
  2830. goto err_out_mwi_2;
  2831. }
  2832. tp->cp_cmd = RxChkSum;
  2833. if ((sizeof(dma_addr_t) > 4) &&
  2834. !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
  2835. tp->cp_cmd |= PCIDAC;
  2836. dev->features |= NETIF_F_HIGHDMA;
  2837. } else {
  2838. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  2839. if (rc < 0) {
  2840. netif_err(tp, probe, dev, "DMA configuration failed\n");
  2841. goto err_out_free_res_3;
  2842. }
  2843. }
  2844. /* ioremap MMIO region */
  2845. ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
  2846. if (!ioaddr) {
  2847. netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
  2848. rc = -EIO;
  2849. goto err_out_free_res_3;
  2850. }
  2851. tp->pcie_cap = pci_find_capability(pdev, PCI_CAP_ID_EXP);
  2852. if (!tp->pcie_cap)
  2853. netif_info(tp, probe, dev, "no PCI Express capability\n");
  2854. RTL_W16(IntrMask, 0x0000);
  2855. /* Soft reset the chip. */
  2856. RTL_W8(ChipCmd, CmdReset);
  2857. /* Check that the chip has finished the reset. */
  2858. for (i = 0; i < 100; i++) {
  2859. if ((RTL_R8(ChipCmd) & CmdReset) == 0)
  2860. break;
  2861. msleep_interruptible(1);
  2862. }
  2863. RTL_W16(IntrStatus, 0xffff);
  2864. pci_set_master(pdev);
  2865. /* Identify chip attached to board */
  2866. rtl8169_get_mac_version(tp, ioaddr);
  2867. /*
  2868. * Pretend we are using VLANs; This bypasses a nasty bug where
  2869. * Interrupts stop flowing on high load on 8110SCd controllers.
  2870. */
  2871. if (tp->mac_version == RTL_GIGA_MAC_VER_05)
  2872. tp->cp_cmd |= RxVlan;
  2873. rtl_init_mdio_ops(tp);
  2874. rtl_init_pll_power_ops(tp);
  2875. /* Use appropriate default if unknown */
  2876. if (tp->mac_version == RTL_GIGA_MAC_NONE) {
  2877. netif_notice(tp, probe, dev,
  2878. "unknown MAC, using family default\n");
  2879. tp->mac_version = cfg->default_ver;
  2880. }
  2881. rtl8169_print_mac_version(tp);
  2882. for (i = 0; i < ARRAY_SIZE(rtl_chip_info); i++) {
  2883. if (tp->mac_version == rtl_chip_info[i].mac_version)
  2884. break;
  2885. }
  2886. if (i == ARRAY_SIZE(rtl_chip_info)) {
  2887. dev_err(&pdev->dev,
  2888. "driver bug, MAC version not found in rtl_chip_info\n");
  2889. goto err_out_msi_4;
  2890. }
  2891. chipset = i;
  2892. tp->txd_version = rtl_chip_info[chipset].txd_version;
  2893. RTL_W8(Cfg9346, Cfg9346_Unlock);
  2894. RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
  2895. RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
  2896. if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
  2897. tp->features |= RTL_FEATURE_WOL;
  2898. if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
  2899. tp->features |= RTL_FEATURE_WOL;
  2900. tp->features |= rtl_try_msi(pdev, ioaddr, cfg);
  2901. RTL_W8(Cfg9346, Cfg9346_Lock);
  2902. if ((tp->mac_version <= RTL_GIGA_MAC_VER_06) &&
  2903. (RTL_R8(PHYstatus) & TBI_Enable)) {
  2904. tp->set_speed = rtl8169_set_speed_tbi;
  2905. tp->get_settings = rtl8169_gset_tbi;
  2906. tp->phy_reset_enable = rtl8169_tbi_reset_enable;
  2907. tp->phy_reset_pending = rtl8169_tbi_reset_pending;
  2908. tp->link_ok = rtl8169_tbi_link_ok;
  2909. tp->do_ioctl = rtl_tbi_ioctl;
  2910. tp->phy_1000_ctrl_reg = ADVERTISE_1000FULL; /* Implied by TBI */
  2911. } else {
  2912. tp->set_speed = rtl8169_set_speed_xmii;
  2913. tp->get_settings = rtl8169_gset_xmii;
  2914. tp->phy_reset_enable = rtl8169_xmii_reset_enable;
  2915. tp->phy_reset_pending = rtl8169_xmii_reset_pending;
  2916. tp->link_ok = rtl8169_xmii_link_ok;
  2917. tp->do_ioctl = rtl_xmii_ioctl;
  2918. }
  2919. spin_lock_init(&tp->lock);
  2920. tp->mmio_addr = ioaddr;
  2921. /* Get MAC address */
  2922. for (i = 0; i < MAC_ADDR_LEN; i++)
  2923. dev->dev_addr[i] = RTL_R8(MAC0 + i);
  2924. memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
  2925. SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
  2926. dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
  2927. dev->irq = pdev->irq;
  2928. dev->base_addr = (unsigned long) ioaddr;
  2929. netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
  2930. /* don't enable SG, IP_CSUM and TSO by default - it might not work
  2931. * properly for all devices */
  2932. dev->features |= NETIF_F_RXCSUM |
  2933. NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
  2934. dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
  2935. NETIF_F_RXCSUM | NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
  2936. dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
  2937. NETIF_F_HIGHDMA;
  2938. if (tp->mac_version == RTL_GIGA_MAC_VER_05)
  2939. /* 8110SCd requires hardware Rx VLAN - disallow toggling */
  2940. dev->hw_features &= ~NETIF_F_HW_VLAN_RX;
  2941. tp->intr_mask = 0xffff;
  2942. tp->hw_start = cfg->hw_start;
  2943. tp->intr_event = cfg->intr_event;
  2944. tp->napi_event = cfg->napi_event;
  2945. init_timer(&tp->timer);
  2946. tp->timer.data = (unsigned long) dev;
  2947. tp->timer.function = rtl8169_phy_timer;
  2948. tp->fw = RTL_FIRMWARE_UNKNOWN;
  2949. rc = register_netdev(dev);
  2950. if (rc < 0)
  2951. goto err_out_msi_4;
  2952. pci_set_drvdata(pdev, dev);
  2953. netif_info(tp, probe, dev, "%s at 0x%lx, %pM, XID %08x IRQ %d\n",
  2954. rtl_chip_info[chipset].name, dev->base_addr, dev->dev_addr,
  2955. (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), dev->irq);
  2956. if ((tp->mac_version == RTL_GIGA_MAC_VER_27) ||
  2957. (tp->mac_version == RTL_GIGA_MAC_VER_28) ||
  2958. (tp->mac_version == RTL_GIGA_MAC_VER_31)) {
  2959. rtl8168_driver_start(tp);
  2960. }
  2961. device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
  2962. if (pci_dev_run_wake(pdev))
  2963. pm_runtime_put_noidle(&pdev->dev);
  2964. netif_carrier_off(dev);
  2965. out:
  2966. return rc;
  2967. err_out_msi_4:
  2968. rtl_disable_msi(pdev, tp);
  2969. iounmap(ioaddr);
  2970. err_out_free_res_3:
  2971. pci_release_regions(pdev);
  2972. err_out_mwi_2:
  2973. pci_clear_mwi(pdev);
  2974. pci_disable_device(pdev);
  2975. err_out_free_dev_1:
  2976. free_netdev(dev);
  2977. goto out;
  2978. }
  2979. static void __devexit rtl8169_remove_one(struct pci_dev *pdev)
  2980. {
  2981. struct net_device *dev = pci_get_drvdata(pdev);
  2982. struct rtl8169_private *tp = netdev_priv(dev);
  2983. if ((tp->mac_version == RTL_GIGA_MAC_VER_27) ||
  2984. (tp->mac_version == RTL_GIGA_MAC_VER_28) ||
  2985. (tp->mac_version == RTL_GIGA_MAC_VER_31)) {
  2986. rtl8168_driver_stop(tp);
  2987. }
  2988. cancel_delayed_work_sync(&tp->task);
  2989. unregister_netdev(dev);
  2990. rtl_release_firmware(tp);
  2991. if (pci_dev_run_wake(pdev))
  2992. pm_runtime_get_noresume(&pdev->dev);
  2993. /* restore original MAC address */
  2994. rtl_rar_set(tp, dev->perm_addr);
  2995. rtl_disable_msi(pdev, tp);
  2996. rtl8169_release_board(pdev, dev, tp->mmio_addr);
  2997. pci_set_drvdata(pdev, NULL);
  2998. }
  2999. static void rtl_request_firmware(struct rtl8169_private *tp)
  3000. {
  3001. int i;
  3002. /* Return early if the firmware is already loaded / cached. */
  3003. if (!IS_ERR(tp->fw))
  3004. goto out;
  3005. for (i = 0; i < ARRAY_SIZE(rtl_firmware_infos); i++) {
  3006. const struct rtl_firmware_info *info = rtl_firmware_infos + i;
  3007. if (info->mac_version == tp->mac_version) {
  3008. const char *name = info->fw_name;
  3009. int rc;
  3010. rc = request_firmware(&tp->fw, name, &tp->pci_dev->dev);
  3011. if (rc < 0) {
  3012. netif_warn(tp, ifup, tp->dev, "unable to load "
  3013. "firmware patch %s (%d)\n", name, rc);
  3014. goto out_disable_request_firmware;
  3015. }
  3016. goto out;
  3017. }
  3018. }
  3019. out_disable_request_firmware:
  3020. tp->fw = NULL;
  3021. out:
  3022. return;
  3023. }
  3024. static int rtl8169_open(struct net_device *dev)
  3025. {
  3026. struct rtl8169_private *tp = netdev_priv(dev);
  3027. void __iomem *ioaddr = tp->mmio_addr;
  3028. struct pci_dev *pdev = tp->pci_dev;
  3029. int retval = -ENOMEM;
  3030. pm_runtime_get_sync(&pdev->dev);
  3031. /*
  3032. * Rx and Tx desscriptors needs 256 bytes alignment.
  3033. * dma_alloc_coherent provides more.
  3034. */
  3035. tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
  3036. &tp->TxPhyAddr, GFP_KERNEL);
  3037. if (!tp->TxDescArray)
  3038. goto err_pm_runtime_put;
  3039. tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
  3040. &tp->RxPhyAddr, GFP_KERNEL);
  3041. if (!tp->RxDescArray)
  3042. goto err_free_tx_0;
  3043. retval = rtl8169_init_ring(dev);
  3044. if (retval < 0)
  3045. goto err_free_rx_1;
  3046. INIT_DELAYED_WORK(&tp->task, NULL);
  3047. smp_mb();
  3048. rtl_request_firmware(tp);
  3049. retval = request_irq(dev->irq, rtl8169_interrupt,
  3050. (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
  3051. dev->name, dev);
  3052. if (retval < 0)
  3053. goto err_release_fw_2;
  3054. napi_enable(&tp->napi);
  3055. rtl8169_init_phy(dev, tp);
  3056. rtl8169_set_features(dev, dev->features);
  3057. rtl_pll_power_up(tp);
  3058. rtl_hw_start(dev);
  3059. rtl8169_request_timer(dev);
  3060. tp->saved_wolopts = 0;
  3061. pm_runtime_put_noidle(&pdev->dev);
  3062. rtl8169_check_link_status(dev, tp, ioaddr);
  3063. out:
  3064. return retval;
  3065. err_release_fw_2:
  3066. rtl_release_firmware(tp);
  3067. rtl8169_rx_clear(tp);
  3068. err_free_rx_1:
  3069. dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
  3070. tp->RxPhyAddr);
  3071. tp->RxDescArray = NULL;
  3072. err_free_tx_0:
  3073. dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
  3074. tp->TxPhyAddr);
  3075. tp->TxDescArray = NULL;
  3076. err_pm_runtime_put:
  3077. pm_runtime_put_noidle(&pdev->dev);
  3078. goto out;
  3079. }
  3080. static void rtl8169_hw_reset(struct rtl8169_private *tp)
  3081. {
  3082. void __iomem *ioaddr = tp->mmio_addr;
  3083. /* Disable interrupts */
  3084. rtl8169_irq_mask_and_ack(ioaddr);
  3085. if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
  3086. tp->mac_version == RTL_GIGA_MAC_VER_28 ||
  3087. tp->mac_version == RTL_GIGA_MAC_VER_31) {
  3088. while (RTL_R8(TxPoll) & NPQ)
  3089. udelay(20);
  3090. }
  3091. /* Reset the chipset */
  3092. RTL_W8(ChipCmd, CmdReset);
  3093. /* PCI commit */
  3094. RTL_R8(ChipCmd);
  3095. }
  3096. static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
  3097. {
  3098. void __iomem *ioaddr = tp->mmio_addr;
  3099. u32 cfg = rtl8169_rx_config;
  3100. cfg |= (RTL_R32(RxConfig) & RTL_RX_CONFIG_MASK);
  3101. RTL_W32(RxConfig, cfg);
  3102. /* Set DMA burst size and Interframe Gap Time */
  3103. RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
  3104. (InterFrameGap << TxInterFrameGapShift));
  3105. }
  3106. static void rtl_hw_start(struct net_device *dev)
  3107. {
  3108. struct rtl8169_private *tp = netdev_priv(dev);
  3109. void __iomem *ioaddr = tp->mmio_addr;
  3110. unsigned int i;
  3111. /* Soft reset the chip. */
  3112. RTL_W8(ChipCmd, CmdReset);
  3113. /* Check that the chip has finished the reset. */
  3114. for (i = 0; i < 100; i++) {
  3115. if ((RTL_R8(ChipCmd) & CmdReset) == 0)
  3116. break;
  3117. msleep_interruptible(1);
  3118. }
  3119. tp->hw_start(dev);
  3120. netif_start_queue(dev);
  3121. }
  3122. static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
  3123. void __iomem *ioaddr)
  3124. {
  3125. /*
  3126. * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
  3127. * register to be written before TxDescAddrLow to work.
  3128. * Switching from MMIO to I/O access fixes the issue as well.
  3129. */
  3130. RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
  3131. RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
  3132. RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
  3133. RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
  3134. }
  3135. static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
  3136. {
  3137. u16 cmd;
  3138. cmd = RTL_R16(CPlusCmd);
  3139. RTL_W16(CPlusCmd, cmd);
  3140. return cmd;
  3141. }
  3142. static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
  3143. {
  3144. /* Low hurts. Let's disable the filtering. */
  3145. RTL_W16(RxMaxSize, rx_buf_sz + 1);
  3146. }
  3147. static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
  3148. {
  3149. static const struct {
  3150. u32 mac_version;
  3151. u32 clk;
  3152. u32 val;
  3153. } cfg2_info [] = {
  3154. { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
  3155. { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
  3156. { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
  3157. { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
  3158. }, *p = cfg2_info;
  3159. unsigned int i;
  3160. u32 clk;
  3161. clk = RTL_R8(Config2) & PCI_Clock_66MHz;
  3162. for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
  3163. if ((p->mac_version == mac_version) && (p->clk == clk)) {
  3164. RTL_W32(0x7c, p->val);
  3165. break;
  3166. }
  3167. }
  3168. }
  3169. static void rtl_hw_start_8169(struct net_device *dev)
  3170. {
  3171. struct rtl8169_private *tp = netdev_priv(dev);
  3172. void __iomem *ioaddr = tp->mmio_addr;
  3173. struct pci_dev *pdev = tp->pci_dev;
  3174. if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
  3175. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
  3176. pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
  3177. }
  3178. RTL_W8(Cfg9346, Cfg9346_Unlock);
  3179. if ((tp->mac_version == RTL_GIGA_MAC_VER_01) ||
  3180. (tp->mac_version == RTL_GIGA_MAC_VER_02) ||
  3181. (tp->mac_version == RTL_GIGA_MAC_VER_03) ||
  3182. (tp->mac_version == RTL_GIGA_MAC_VER_04))
  3183. RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
  3184. RTL_W8(EarlyTxThres, NoEarlyTx);
  3185. rtl_set_rx_max_size(ioaddr, rx_buf_sz);
  3186. if ((tp->mac_version == RTL_GIGA_MAC_VER_01) ||
  3187. (tp->mac_version == RTL_GIGA_MAC_VER_02) ||
  3188. (tp->mac_version == RTL_GIGA_MAC_VER_03) ||
  3189. (tp->mac_version == RTL_GIGA_MAC_VER_04))
  3190. rtl_set_rx_tx_config_registers(tp);
  3191. tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
  3192. if ((tp->mac_version == RTL_GIGA_MAC_VER_02) ||
  3193. (tp->mac_version == RTL_GIGA_MAC_VER_03)) {
  3194. dprintk("Set MAC Reg C+CR Offset 0xE0. "
  3195. "Bit-3 and bit-14 MUST be 1\n");
  3196. tp->cp_cmd |= (1 << 14);
  3197. }
  3198. RTL_W16(CPlusCmd, tp->cp_cmd);
  3199. rtl8169_set_magic_reg(ioaddr, tp->mac_version);
  3200. /*
  3201. * Undocumented corner. Supposedly:
  3202. * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
  3203. */
  3204. RTL_W16(IntrMitigate, 0x0000);
  3205. rtl_set_rx_tx_desc_registers(tp, ioaddr);
  3206. if ((tp->mac_version != RTL_GIGA_MAC_VER_01) &&
  3207. (tp->mac_version != RTL_GIGA_MAC_VER_02) &&
  3208. (tp->mac_version != RTL_GIGA_MAC_VER_03) &&
  3209. (tp->mac_version != RTL_GIGA_MAC_VER_04)) {
  3210. RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
  3211. rtl_set_rx_tx_config_registers(tp);
  3212. }
  3213. RTL_W8(Cfg9346, Cfg9346_Lock);
  3214. /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
  3215. RTL_R8(IntrMask);
  3216. RTL_W32(RxMissed, 0);
  3217. rtl_set_rx_mode(dev);
  3218. /* no early-rx interrupts */
  3219. RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
  3220. /* Enable all known interrupts by setting the interrupt mask. */
  3221. RTL_W16(IntrMask, tp->intr_event);
  3222. }
  3223. static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
  3224. {
  3225. struct net_device *dev = pci_get_drvdata(pdev);
  3226. struct rtl8169_private *tp = netdev_priv(dev);
  3227. int cap = tp->pcie_cap;
  3228. if (cap) {
  3229. u16 ctl;
  3230. pci_read_config_word(pdev, cap + PCI_EXP_DEVCTL, &ctl);
  3231. ctl = (ctl & ~PCI_EXP_DEVCTL_READRQ) | force;
  3232. pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL, ctl);
  3233. }
  3234. }
  3235. static void rtl_csi_access_enable(void __iomem *ioaddr, u32 bits)
  3236. {
  3237. u32 csi;
  3238. csi = rtl_csi_read(ioaddr, 0x070c) & 0x00ffffff;
  3239. rtl_csi_write(ioaddr, 0x070c, csi | bits);
  3240. }
  3241. static void rtl_csi_access_enable_1(void __iomem *ioaddr)
  3242. {
  3243. rtl_csi_access_enable(ioaddr, 0x17000000);
  3244. }
  3245. static void rtl_csi_access_enable_2(void __iomem *ioaddr)
  3246. {
  3247. rtl_csi_access_enable(ioaddr, 0x27000000);
  3248. }
  3249. struct ephy_info {
  3250. unsigned int offset;
  3251. u16 mask;
  3252. u16 bits;
  3253. };
  3254. static void rtl_ephy_init(void __iomem *ioaddr, const struct ephy_info *e, int len)
  3255. {
  3256. u16 w;
  3257. while (len-- > 0) {
  3258. w = (rtl_ephy_read(ioaddr, e->offset) & ~e->mask) | e->bits;
  3259. rtl_ephy_write(ioaddr, e->offset, w);
  3260. e++;
  3261. }
  3262. }
  3263. static void rtl_disable_clock_request(struct pci_dev *pdev)
  3264. {
  3265. struct net_device *dev = pci_get_drvdata(pdev);
  3266. struct rtl8169_private *tp = netdev_priv(dev);
  3267. int cap = tp->pcie_cap;
  3268. if (cap) {
  3269. u16 ctl;
  3270. pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
  3271. ctl &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
  3272. pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
  3273. }
  3274. }
  3275. static void rtl_enable_clock_request(struct pci_dev *pdev)
  3276. {
  3277. struct net_device *dev = pci_get_drvdata(pdev);
  3278. struct rtl8169_private *tp = netdev_priv(dev);
  3279. int cap = tp->pcie_cap;
  3280. if (cap) {
  3281. u16 ctl;
  3282. pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
  3283. ctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
  3284. pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
  3285. }
  3286. }
  3287. #define R8168_CPCMD_QUIRK_MASK (\
  3288. EnableBist | \
  3289. Mac_dbgo_oe | \
  3290. Force_half_dup | \
  3291. Force_rxflow_en | \
  3292. Force_txflow_en | \
  3293. Cxpl_dbg_sel | \
  3294. ASF | \
  3295. PktCntrDisable | \
  3296. Mac_dbgo_sel)
  3297. static void rtl_hw_start_8168bb(void __iomem *ioaddr, struct pci_dev *pdev)
  3298. {
  3299. RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
  3300. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
  3301. rtl_tx_performance_tweak(pdev,
  3302. (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
  3303. }
  3304. static void rtl_hw_start_8168bef(void __iomem *ioaddr, struct pci_dev *pdev)
  3305. {
  3306. rtl_hw_start_8168bb(ioaddr, pdev);
  3307. RTL_W8(MaxTxPacketSize, TxPacketMax);
  3308. RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
  3309. }
  3310. static void __rtl_hw_start_8168cp(void __iomem *ioaddr, struct pci_dev *pdev)
  3311. {
  3312. RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
  3313. RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
  3314. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  3315. rtl_disable_clock_request(pdev);
  3316. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
  3317. }
  3318. static void rtl_hw_start_8168cp_1(void __iomem *ioaddr, struct pci_dev *pdev)
  3319. {
  3320. static const struct ephy_info e_info_8168cp[] = {
  3321. { 0x01, 0, 0x0001 },
  3322. { 0x02, 0x0800, 0x1000 },
  3323. { 0x03, 0, 0x0042 },
  3324. { 0x06, 0x0080, 0x0000 },
  3325. { 0x07, 0, 0x2000 }
  3326. };
  3327. rtl_csi_access_enable_2(ioaddr);
  3328. rtl_ephy_init(ioaddr, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
  3329. __rtl_hw_start_8168cp(ioaddr, pdev);
  3330. }
  3331. static void rtl_hw_start_8168cp_2(void __iomem *ioaddr, struct pci_dev *pdev)
  3332. {
  3333. rtl_csi_access_enable_2(ioaddr);
  3334. RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
  3335. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  3336. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
  3337. }
  3338. static void rtl_hw_start_8168cp_3(void __iomem *ioaddr, struct pci_dev *pdev)
  3339. {
  3340. rtl_csi_access_enable_2(ioaddr);
  3341. RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
  3342. /* Magic. */
  3343. RTL_W8(DBG_REG, 0x20);
  3344. RTL_W8(MaxTxPacketSize, TxPacketMax);
  3345. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  3346. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
  3347. }
  3348. static void rtl_hw_start_8168c_1(void __iomem *ioaddr, struct pci_dev *pdev)
  3349. {
  3350. static const struct ephy_info e_info_8168c_1[] = {
  3351. { 0x02, 0x0800, 0x1000 },
  3352. { 0x03, 0, 0x0002 },
  3353. { 0x06, 0x0080, 0x0000 }
  3354. };
  3355. rtl_csi_access_enable_2(ioaddr);
  3356. RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
  3357. rtl_ephy_init(ioaddr, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
  3358. __rtl_hw_start_8168cp(ioaddr, pdev);
  3359. }
  3360. static void rtl_hw_start_8168c_2(void __iomem *ioaddr, struct pci_dev *pdev)
  3361. {
  3362. static const struct ephy_info e_info_8168c_2[] = {
  3363. { 0x01, 0, 0x0001 },
  3364. { 0x03, 0x0400, 0x0220 }
  3365. };
  3366. rtl_csi_access_enable_2(ioaddr);
  3367. rtl_ephy_init(ioaddr, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
  3368. __rtl_hw_start_8168cp(ioaddr, pdev);
  3369. }
  3370. static void rtl_hw_start_8168c_3(void __iomem *ioaddr, struct pci_dev *pdev)
  3371. {
  3372. rtl_hw_start_8168c_2(ioaddr, pdev);
  3373. }
  3374. static void rtl_hw_start_8168c_4(void __iomem *ioaddr, struct pci_dev *pdev)
  3375. {
  3376. rtl_csi_access_enable_2(ioaddr);
  3377. __rtl_hw_start_8168cp(ioaddr, pdev);
  3378. }
  3379. static void rtl_hw_start_8168d(void __iomem *ioaddr, struct pci_dev *pdev)
  3380. {
  3381. rtl_csi_access_enable_2(ioaddr);
  3382. rtl_disable_clock_request(pdev);
  3383. RTL_W8(MaxTxPacketSize, TxPacketMax);
  3384. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  3385. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
  3386. }
  3387. static void rtl_hw_start_8168dp(void __iomem *ioaddr, struct pci_dev *pdev)
  3388. {
  3389. rtl_csi_access_enable_1(ioaddr);
  3390. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  3391. RTL_W8(MaxTxPacketSize, TxPacketMax);
  3392. rtl_disable_clock_request(pdev);
  3393. }
  3394. static void rtl_hw_start_8168d_4(void __iomem *ioaddr, struct pci_dev *pdev)
  3395. {
  3396. static const struct ephy_info e_info_8168d_4[] = {
  3397. { 0x0b, ~0, 0x48 },
  3398. { 0x19, 0x20, 0x50 },
  3399. { 0x0c, ~0, 0x20 }
  3400. };
  3401. int i;
  3402. rtl_csi_access_enable_1(ioaddr);
  3403. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  3404. RTL_W8(MaxTxPacketSize, TxPacketMax);
  3405. for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
  3406. const struct ephy_info *e = e_info_8168d_4 + i;
  3407. u16 w;
  3408. w = rtl_ephy_read(ioaddr, e->offset);
  3409. rtl_ephy_write(ioaddr, 0x03, (w & e->mask) | e->bits);
  3410. }
  3411. rtl_enable_clock_request(pdev);
  3412. }
  3413. static void rtl_hw_start_8168e(void __iomem *ioaddr, struct pci_dev *pdev)
  3414. {
  3415. static const struct ephy_info e_info_8168e[] = {
  3416. { 0x00, 0x0200, 0x0100 },
  3417. { 0x00, 0x0000, 0x0004 },
  3418. { 0x06, 0x0002, 0x0001 },
  3419. { 0x06, 0x0000, 0x0030 },
  3420. { 0x07, 0x0000, 0x2000 },
  3421. { 0x00, 0x0000, 0x0020 },
  3422. { 0x03, 0x5800, 0x2000 },
  3423. { 0x03, 0x0000, 0x0001 },
  3424. { 0x01, 0x0800, 0x1000 },
  3425. { 0x07, 0x0000, 0x4000 },
  3426. { 0x1e, 0x0000, 0x2000 },
  3427. { 0x19, 0xffff, 0xfe6c },
  3428. { 0x0a, 0x0000, 0x0040 }
  3429. };
  3430. rtl_csi_access_enable_2(ioaddr);
  3431. rtl_ephy_init(ioaddr, e_info_8168e, ARRAY_SIZE(e_info_8168e));
  3432. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  3433. RTL_W8(MaxTxPacketSize, TxPacketMax);
  3434. rtl_disable_clock_request(pdev);
  3435. /* Reset tx FIFO pointer */
  3436. RTL_W32(MISC, RTL_R32(MISC) | txpla_rst);
  3437. RTL_W32(MISC, RTL_R32(MISC) & ~txpla_rst);
  3438. RTL_W8(Config5, RTL_R8(Config5) & ~spi_en);
  3439. }
  3440. static void rtl_hw_start_8168(struct net_device *dev)
  3441. {
  3442. struct rtl8169_private *tp = netdev_priv(dev);
  3443. void __iomem *ioaddr = tp->mmio_addr;
  3444. struct pci_dev *pdev = tp->pci_dev;
  3445. RTL_W8(Cfg9346, Cfg9346_Unlock);
  3446. RTL_W8(MaxTxPacketSize, TxPacketMax);
  3447. rtl_set_rx_max_size(ioaddr, rx_buf_sz);
  3448. tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
  3449. RTL_W16(CPlusCmd, tp->cp_cmd);
  3450. RTL_W16(IntrMitigate, 0x5151);
  3451. /* Work around for RxFIFO overflow. */
  3452. if (tp->mac_version == RTL_GIGA_MAC_VER_11 ||
  3453. tp->mac_version == RTL_GIGA_MAC_VER_22) {
  3454. tp->intr_event |= RxFIFOOver | PCSTimeout;
  3455. tp->intr_event &= ~RxOverflow;
  3456. }
  3457. rtl_set_rx_tx_desc_registers(tp, ioaddr);
  3458. rtl_set_rx_mode(dev);
  3459. RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
  3460. (InterFrameGap << TxInterFrameGapShift));
  3461. RTL_R8(IntrMask);
  3462. switch (tp->mac_version) {
  3463. case RTL_GIGA_MAC_VER_11:
  3464. rtl_hw_start_8168bb(ioaddr, pdev);
  3465. break;
  3466. case RTL_GIGA_MAC_VER_12:
  3467. case RTL_GIGA_MAC_VER_17:
  3468. rtl_hw_start_8168bef(ioaddr, pdev);
  3469. break;
  3470. case RTL_GIGA_MAC_VER_18:
  3471. rtl_hw_start_8168cp_1(ioaddr, pdev);
  3472. break;
  3473. case RTL_GIGA_MAC_VER_19:
  3474. rtl_hw_start_8168c_1(ioaddr, pdev);
  3475. break;
  3476. case RTL_GIGA_MAC_VER_20:
  3477. rtl_hw_start_8168c_2(ioaddr, pdev);
  3478. break;
  3479. case RTL_GIGA_MAC_VER_21:
  3480. rtl_hw_start_8168c_3(ioaddr, pdev);
  3481. break;
  3482. case RTL_GIGA_MAC_VER_22:
  3483. rtl_hw_start_8168c_4(ioaddr, pdev);
  3484. break;
  3485. case RTL_GIGA_MAC_VER_23:
  3486. rtl_hw_start_8168cp_2(ioaddr, pdev);
  3487. break;
  3488. case RTL_GIGA_MAC_VER_24:
  3489. rtl_hw_start_8168cp_3(ioaddr, pdev);
  3490. break;
  3491. case RTL_GIGA_MAC_VER_25:
  3492. case RTL_GIGA_MAC_VER_26:
  3493. case RTL_GIGA_MAC_VER_27:
  3494. rtl_hw_start_8168d(ioaddr, pdev);
  3495. break;
  3496. case RTL_GIGA_MAC_VER_28:
  3497. rtl_hw_start_8168d_4(ioaddr, pdev);
  3498. break;
  3499. case RTL_GIGA_MAC_VER_31:
  3500. rtl_hw_start_8168dp(ioaddr, pdev);
  3501. break;
  3502. case RTL_GIGA_MAC_VER_32:
  3503. case RTL_GIGA_MAC_VER_33:
  3504. rtl_hw_start_8168e(ioaddr, pdev);
  3505. break;
  3506. default:
  3507. printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
  3508. dev->name, tp->mac_version);
  3509. break;
  3510. }
  3511. RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
  3512. RTL_W8(Cfg9346, Cfg9346_Lock);
  3513. RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
  3514. RTL_W16(IntrMask, tp->intr_event);
  3515. }
  3516. #define R810X_CPCMD_QUIRK_MASK (\
  3517. EnableBist | \
  3518. Mac_dbgo_oe | \
  3519. Force_half_dup | \
  3520. Force_rxflow_en | \
  3521. Force_txflow_en | \
  3522. Cxpl_dbg_sel | \
  3523. ASF | \
  3524. PktCntrDisable | \
  3525. Mac_dbgo_sel)
  3526. static void rtl_hw_start_8102e_1(void __iomem *ioaddr, struct pci_dev *pdev)
  3527. {
  3528. static const struct ephy_info e_info_8102e_1[] = {
  3529. { 0x01, 0, 0x6e65 },
  3530. { 0x02, 0, 0x091f },
  3531. { 0x03, 0, 0xc2f9 },
  3532. { 0x06, 0, 0xafb5 },
  3533. { 0x07, 0, 0x0e00 },
  3534. { 0x19, 0, 0xec80 },
  3535. { 0x01, 0, 0x2e65 },
  3536. { 0x01, 0, 0x6e65 }
  3537. };
  3538. u8 cfg1;
  3539. rtl_csi_access_enable_2(ioaddr);
  3540. RTL_W8(DBG_REG, FIX_NAK_1);
  3541. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  3542. RTL_W8(Config1,
  3543. LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
  3544. RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
  3545. cfg1 = RTL_R8(Config1);
  3546. if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
  3547. RTL_W8(Config1, cfg1 & ~LEDS0);
  3548. rtl_ephy_init(ioaddr, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
  3549. }
  3550. static void rtl_hw_start_8102e_2(void __iomem *ioaddr, struct pci_dev *pdev)
  3551. {
  3552. rtl_csi_access_enable_2(ioaddr);
  3553. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  3554. RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
  3555. RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
  3556. }
  3557. static void rtl_hw_start_8102e_3(void __iomem *ioaddr, struct pci_dev *pdev)
  3558. {
  3559. rtl_hw_start_8102e_2(ioaddr, pdev);
  3560. rtl_ephy_write(ioaddr, 0x03, 0xc2f9);
  3561. }
  3562. static void rtl_hw_start_8105e_1(void __iomem *ioaddr, struct pci_dev *pdev)
  3563. {
  3564. static const struct ephy_info e_info_8105e_1[] = {
  3565. { 0x07, 0, 0x4000 },
  3566. { 0x19, 0, 0x0200 },
  3567. { 0x19, 0, 0x0020 },
  3568. { 0x1e, 0, 0x2000 },
  3569. { 0x03, 0, 0x0001 },
  3570. { 0x19, 0, 0x0100 },
  3571. { 0x19, 0, 0x0004 },
  3572. { 0x0a, 0, 0x0020 }
  3573. };
  3574. /* Force LAN exit from ASPM if Rx/Tx are not idel */
  3575. RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
  3576. /* disable Early Tally Counter */
  3577. RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
  3578. RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
  3579. RTL_W8(DLLPR, RTL_R8(DLLPR) | PM_SWITCH);
  3580. rtl_ephy_init(ioaddr, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
  3581. }
  3582. static void rtl_hw_start_8105e_2(void __iomem *ioaddr, struct pci_dev *pdev)
  3583. {
  3584. rtl_hw_start_8105e_1(ioaddr, pdev);
  3585. rtl_ephy_write(ioaddr, 0x1e, rtl_ephy_read(ioaddr, 0x1e) | 0x8000);
  3586. }
  3587. static void rtl_hw_start_8101(struct net_device *dev)
  3588. {
  3589. struct rtl8169_private *tp = netdev_priv(dev);
  3590. void __iomem *ioaddr = tp->mmio_addr;
  3591. struct pci_dev *pdev = tp->pci_dev;
  3592. if ((tp->mac_version == RTL_GIGA_MAC_VER_13) ||
  3593. (tp->mac_version == RTL_GIGA_MAC_VER_16)) {
  3594. int cap = tp->pcie_cap;
  3595. if (cap) {
  3596. pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL,
  3597. PCI_EXP_DEVCTL_NOSNOOP_EN);
  3598. }
  3599. }
  3600. RTL_W8(Cfg9346, Cfg9346_Unlock);
  3601. switch (tp->mac_version) {
  3602. case RTL_GIGA_MAC_VER_07:
  3603. rtl_hw_start_8102e_1(ioaddr, pdev);
  3604. break;
  3605. case RTL_GIGA_MAC_VER_08:
  3606. rtl_hw_start_8102e_3(ioaddr, pdev);
  3607. break;
  3608. case RTL_GIGA_MAC_VER_09:
  3609. rtl_hw_start_8102e_2(ioaddr, pdev);
  3610. break;
  3611. case RTL_GIGA_MAC_VER_29:
  3612. rtl_hw_start_8105e_1(ioaddr, pdev);
  3613. break;
  3614. case RTL_GIGA_MAC_VER_30:
  3615. rtl_hw_start_8105e_2(ioaddr, pdev);
  3616. break;
  3617. }
  3618. RTL_W8(Cfg9346, Cfg9346_Lock);
  3619. RTL_W8(MaxTxPacketSize, TxPacketMax);
  3620. rtl_set_rx_max_size(ioaddr, rx_buf_sz);
  3621. tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
  3622. RTL_W16(CPlusCmd, tp->cp_cmd);
  3623. RTL_W16(IntrMitigate, 0x0000);
  3624. rtl_set_rx_tx_desc_registers(tp, ioaddr);
  3625. RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
  3626. rtl_set_rx_tx_config_registers(tp);
  3627. RTL_R8(IntrMask);
  3628. rtl_set_rx_mode(dev);
  3629. RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
  3630. RTL_W16(IntrMask, tp->intr_event);
  3631. }
  3632. static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
  3633. {
  3634. if (new_mtu < ETH_ZLEN || new_mtu > SafeMtu)
  3635. return -EINVAL;
  3636. dev->mtu = new_mtu;
  3637. netdev_update_features(dev);
  3638. return 0;
  3639. }
  3640. static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
  3641. {
  3642. desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
  3643. desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
  3644. }
  3645. static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
  3646. void **data_buff, struct RxDesc *desc)
  3647. {
  3648. dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
  3649. DMA_FROM_DEVICE);
  3650. kfree(*data_buff);
  3651. *data_buff = NULL;
  3652. rtl8169_make_unusable_by_asic(desc);
  3653. }
  3654. static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
  3655. {
  3656. u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
  3657. desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
  3658. }
  3659. static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
  3660. u32 rx_buf_sz)
  3661. {
  3662. desc->addr = cpu_to_le64(mapping);
  3663. wmb();
  3664. rtl8169_mark_to_asic(desc, rx_buf_sz);
  3665. }
  3666. static inline void *rtl8169_align(void *data)
  3667. {
  3668. return (void *)ALIGN((long)data, 16);
  3669. }
  3670. static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
  3671. struct RxDesc *desc)
  3672. {
  3673. void *data;
  3674. dma_addr_t mapping;
  3675. struct device *d = &tp->pci_dev->dev;
  3676. struct net_device *dev = tp->dev;
  3677. int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
  3678. data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
  3679. if (!data)
  3680. return NULL;
  3681. if (rtl8169_align(data) != data) {
  3682. kfree(data);
  3683. data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
  3684. if (!data)
  3685. return NULL;
  3686. }
  3687. mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
  3688. DMA_FROM_DEVICE);
  3689. if (unlikely(dma_mapping_error(d, mapping))) {
  3690. if (net_ratelimit())
  3691. netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
  3692. goto err_out;
  3693. }
  3694. rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
  3695. return data;
  3696. err_out:
  3697. kfree(data);
  3698. return NULL;
  3699. }
  3700. static void rtl8169_rx_clear(struct rtl8169_private *tp)
  3701. {
  3702. unsigned int i;
  3703. for (i = 0; i < NUM_RX_DESC; i++) {
  3704. if (tp->Rx_databuff[i]) {
  3705. rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
  3706. tp->RxDescArray + i);
  3707. }
  3708. }
  3709. }
  3710. static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
  3711. {
  3712. desc->opts1 |= cpu_to_le32(RingEnd);
  3713. }
  3714. static int rtl8169_rx_fill(struct rtl8169_private *tp)
  3715. {
  3716. unsigned int i;
  3717. for (i = 0; i < NUM_RX_DESC; i++) {
  3718. void *data;
  3719. if (tp->Rx_databuff[i])
  3720. continue;
  3721. data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
  3722. if (!data) {
  3723. rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
  3724. goto err_out;
  3725. }
  3726. tp->Rx_databuff[i] = data;
  3727. }
  3728. rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
  3729. return 0;
  3730. err_out:
  3731. rtl8169_rx_clear(tp);
  3732. return -ENOMEM;
  3733. }
  3734. static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
  3735. {
  3736. tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
  3737. }
  3738. static int rtl8169_init_ring(struct net_device *dev)
  3739. {
  3740. struct rtl8169_private *tp = netdev_priv(dev);
  3741. rtl8169_init_ring_indexes(tp);
  3742. memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
  3743. memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
  3744. return rtl8169_rx_fill(tp);
  3745. }
  3746. static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
  3747. struct TxDesc *desc)
  3748. {
  3749. unsigned int len = tx_skb->len;
  3750. dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
  3751. desc->opts1 = 0x00;
  3752. desc->opts2 = 0x00;
  3753. desc->addr = 0x00;
  3754. tx_skb->len = 0;
  3755. }
  3756. static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
  3757. unsigned int n)
  3758. {
  3759. unsigned int i;
  3760. for (i = 0; i < n; i++) {
  3761. unsigned int entry = (start + i) % NUM_TX_DESC;
  3762. struct ring_info *tx_skb = tp->tx_skb + entry;
  3763. unsigned int len = tx_skb->len;
  3764. if (len) {
  3765. struct sk_buff *skb = tx_skb->skb;
  3766. rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
  3767. tp->TxDescArray + entry);
  3768. if (skb) {
  3769. tp->dev->stats.tx_dropped++;
  3770. dev_kfree_skb(skb);
  3771. tx_skb->skb = NULL;
  3772. }
  3773. }
  3774. }
  3775. }
  3776. static void rtl8169_tx_clear(struct rtl8169_private *tp)
  3777. {
  3778. rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
  3779. tp->cur_tx = tp->dirty_tx = 0;
  3780. }
  3781. static void rtl8169_schedule_work(struct net_device *dev, work_func_t task)
  3782. {
  3783. struct rtl8169_private *tp = netdev_priv(dev);
  3784. PREPARE_DELAYED_WORK(&tp->task, task);
  3785. schedule_delayed_work(&tp->task, 4);
  3786. }
  3787. static void rtl8169_wait_for_quiescence(struct net_device *dev)
  3788. {
  3789. struct rtl8169_private *tp = netdev_priv(dev);
  3790. void __iomem *ioaddr = tp->mmio_addr;
  3791. synchronize_irq(dev->irq);
  3792. /* Wait for any pending NAPI task to complete */
  3793. napi_disable(&tp->napi);
  3794. rtl8169_irq_mask_and_ack(ioaddr);
  3795. tp->intr_mask = 0xffff;
  3796. RTL_W16(IntrMask, tp->intr_event);
  3797. napi_enable(&tp->napi);
  3798. }
  3799. static void rtl8169_reinit_task(struct work_struct *work)
  3800. {
  3801. struct rtl8169_private *tp =
  3802. container_of(work, struct rtl8169_private, task.work);
  3803. struct net_device *dev = tp->dev;
  3804. int ret;
  3805. rtnl_lock();
  3806. if (!netif_running(dev))
  3807. goto out_unlock;
  3808. rtl8169_wait_for_quiescence(dev);
  3809. rtl8169_close(dev);
  3810. ret = rtl8169_open(dev);
  3811. if (unlikely(ret < 0)) {
  3812. if (net_ratelimit())
  3813. netif_err(tp, drv, dev,
  3814. "reinit failure (status = %d). Rescheduling\n",
  3815. ret);
  3816. rtl8169_schedule_work(dev, rtl8169_reinit_task);
  3817. }
  3818. out_unlock:
  3819. rtnl_unlock();
  3820. }
  3821. static void rtl8169_reset_task(struct work_struct *work)
  3822. {
  3823. struct rtl8169_private *tp =
  3824. container_of(work, struct rtl8169_private, task.work);
  3825. struct net_device *dev = tp->dev;
  3826. rtnl_lock();
  3827. if (!netif_running(dev))
  3828. goto out_unlock;
  3829. rtl8169_wait_for_quiescence(dev);
  3830. rtl8169_rx_interrupt(dev, tp, tp->mmio_addr, ~(u32)0);
  3831. rtl8169_tx_clear(tp);
  3832. if (tp->dirty_rx == tp->cur_rx) {
  3833. rtl8169_init_ring_indexes(tp);
  3834. rtl_hw_start(dev);
  3835. netif_wake_queue(dev);
  3836. rtl8169_check_link_status(dev, tp, tp->mmio_addr);
  3837. } else {
  3838. if (net_ratelimit())
  3839. netif_emerg(tp, intr, dev, "Rx buffers shortage\n");
  3840. rtl8169_schedule_work(dev, rtl8169_reset_task);
  3841. }
  3842. out_unlock:
  3843. rtnl_unlock();
  3844. }
  3845. static void rtl8169_tx_timeout(struct net_device *dev)
  3846. {
  3847. struct rtl8169_private *tp = netdev_priv(dev);
  3848. rtl8169_hw_reset(tp);
  3849. /* Let's wait a bit while any (async) irq lands on */
  3850. rtl8169_schedule_work(dev, rtl8169_reset_task);
  3851. }
  3852. static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
  3853. u32 *opts)
  3854. {
  3855. struct skb_shared_info *info = skb_shinfo(skb);
  3856. unsigned int cur_frag, entry;
  3857. struct TxDesc * uninitialized_var(txd);
  3858. struct device *d = &tp->pci_dev->dev;
  3859. entry = tp->cur_tx;
  3860. for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
  3861. skb_frag_t *frag = info->frags + cur_frag;
  3862. dma_addr_t mapping;
  3863. u32 status, len;
  3864. void *addr;
  3865. entry = (entry + 1) % NUM_TX_DESC;
  3866. txd = tp->TxDescArray + entry;
  3867. len = frag->size;
  3868. addr = ((void *) page_address(frag->page)) + frag->page_offset;
  3869. mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
  3870. if (unlikely(dma_mapping_error(d, mapping))) {
  3871. if (net_ratelimit())
  3872. netif_err(tp, drv, tp->dev,
  3873. "Failed to map TX fragments DMA!\n");
  3874. goto err_out;
  3875. }
  3876. /* anti gcc 2.95.3 bugware (sic) */
  3877. status = opts[0] | len |
  3878. (RingEnd * !((entry + 1) % NUM_TX_DESC));
  3879. txd->opts1 = cpu_to_le32(status);
  3880. txd->opts2 = cpu_to_le32(opts[1]);
  3881. txd->addr = cpu_to_le64(mapping);
  3882. tp->tx_skb[entry].len = len;
  3883. }
  3884. if (cur_frag) {
  3885. tp->tx_skb[entry].skb = skb;
  3886. txd->opts1 |= cpu_to_le32(LastFrag);
  3887. }
  3888. return cur_frag;
  3889. err_out:
  3890. rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
  3891. return -EIO;
  3892. }
  3893. static inline void rtl8169_tso_csum(struct rtl8169_private *tp,
  3894. struct sk_buff *skb, u32 *opts)
  3895. {
  3896. const struct rtl_tx_desc_info *info = tx_desc_info + tp->txd_version;
  3897. u32 mss = skb_shinfo(skb)->gso_size;
  3898. int offset = info->opts_offset;
  3899. if (mss) {
  3900. opts[0] |= TD_LSO;
  3901. opts[offset] |= min(mss, TD_MSS_MAX) << info->mss_shift;
  3902. } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
  3903. const struct iphdr *ip = ip_hdr(skb);
  3904. if (ip->protocol == IPPROTO_TCP)
  3905. opts[offset] |= info->checksum.tcp;
  3906. else if (ip->protocol == IPPROTO_UDP)
  3907. opts[offset] |= info->checksum.udp;
  3908. else
  3909. WARN_ON_ONCE(1);
  3910. }
  3911. }
  3912. static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
  3913. struct net_device *dev)
  3914. {
  3915. struct rtl8169_private *tp = netdev_priv(dev);
  3916. unsigned int entry = tp->cur_tx % NUM_TX_DESC;
  3917. struct TxDesc *txd = tp->TxDescArray + entry;
  3918. void __iomem *ioaddr = tp->mmio_addr;
  3919. struct device *d = &tp->pci_dev->dev;
  3920. dma_addr_t mapping;
  3921. u32 status, len;
  3922. u32 opts[2];
  3923. int frags;
  3924. if (unlikely(TX_BUFFS_AVAIL(tp) < skb_shinfo(skb)->nr_frags)) {
  3925. netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
  3926. goto err_stop_0;
  3927. }
  3928. if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
  3929. goto err_stop_0;
  3930. len = skb_headlen(skb);
  3931. mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
  3932. if (unlikely(dma_mapping_error(d, mapping))) {
  3933. if (net_ratelimit())
  3934. netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
  3935. goto err_dma_0;
  3936. }
  3937. tp->tx_skb[entry].len = len;
  3938. txd->addr = cpu_to_le64(mapping);
  3939. opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
  3940. opts[0] = DescOwn;
  3941. rtl8169_tso_csum(tp, skb, opts);
  3942. frags = rtl8169_xmit_frags(tp, skb, opts);
  3943. if (frags < 0)
  3944. goto err_dma_1;
  3945. else if (frags)
  3946. opts[0] |= FirstFrag;
  3947. else {
  3948. opts[0] |= FirstFrag | LastFrag;
  3949. tp->tx_skb[entry].skb = skb;
  3950. }
  3951. txd->opts2 = cpu_to_le32(opts[1]);
  3952. wmb();
  3953. /* anti gcc 2.95.3 bugware (sic) */
  3954. status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
  3955. txd->opts1 = cpu_to_le32(status);
  3956. tp->cur_tx += frags + 1;
  3957. wmb();
  3958. RTL_W8(TxPoll, NPQ); /* set polling bit */
  3959. if (TX_BUFFS_AVAIL(tp) < MAX_SKB_FRAGS) {
  3960. netif_stop_queue(dev);
  3961. smp_rmb();
  3962. if (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)
  3963. netif_wake_queue(dev);
  3964. }
  3965. return NETDEV_TX_OK;
  3966. err_dma_1:
  3967. rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
  3968. err_dma_0:
  3969. dev_kfree_skb(skb);
  3970. dev->stats.tx_dropped++;
  3971. return NETDEV_TX_OK;
  3972. err_stop_0:
  3973. netif_stop_queue(dev);
  3974. dev->stats.tx_dropped++;
  3975. return NETDEV_TX_BUSY;
  3976. }
  3977. static void rtl8169_pcierr_interrupt(struct net_device *dev)
  3978. {
  3979. struct rtl8169_private *tp = netdev_priv(dev);
  3980. struct pci_dev *pdev = tp->pci_dev;
  3981. u16 pci_status, pci_cmd;
  3982. pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
  3983. pci_read_config_word(pdev, PCI_STATUS, &pci_status);
  3984. netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
  3985. pci_cmd, pci_status);
  3986. /*
  3987. * The recovery sequence below admits a very elaborated explanation:
  3988. * - it seems to work;
  3989. * - I did not see what else could be done;
  3990. * - it makes iop3xx happy.
  3991. *
  3992. * Feel free to adjust to your needs.
  3993. */
  3994. if (pdev->broken_parity_status)
  3995. pci_cmd &= ~PCI_COMMAND_PARITY;
  3996. else
  3997. pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
  3998. pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
  3999. pci_write_config_word(pdev, PCI_STATUS,
  4000. pci_status & (PCI_STATUS_DETECTED_PARITY |
  4001. PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
  4002. PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
  4003. /* The infamous DAC f*ckup only happens at boot time */
  4004. if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
  4005. void __iomem *ioaddr = tp->mmio_addr;
  4006. netif_info(tp, intr, dev, "disabling PCI DAC\n");
  4007. tp->cp_cmd &= ~PCIDAC;
  4008. RTL_W16(CPlusCmd, tp->cp_cmd);
  4009. dev->features &= ~NETIF_F_HIGHDMA;
  4010. }
  4011. rtl8169_hw_reset(tp);
  4012. rtl8169_schedule_work(dev, rtl8169_reinit_task);
  4013. }
  4014. static void rtl8169_tx_interrupt(struct net_device *dev,
  4015. struct rtl8169_private *tp,
  4016. void __iomem *ioaddr)
  4017. {
  4018. unsigned int dirty_tx, tx_left;
  4019. dirty_tx = tp->dirty_tx;
  4020. smp_rmb();
  4021. tx_left = tp->cur_tx - dirty_tx;
  4022. while (tx_left > 0) {
  4023. unsigned int entry = dirty_tx % NUM_TX_DESC;
  4024. struct ring_info *tx_skb = tp->tx_skb + entry;
  4025. u32 status;
  4026. rmb();
  4027. status = le32_to_cpu(tp->TxDescArray[entry].opts1);
  4028. if (status & DescOwn)
  4029. break;
  4030. rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
  4031. tp->TxDescArray + entry);
  4032. if (status & LastFrag) {
  4033. dev->stats.tx_packets++;
  4034. dev->stats.tx_bytes += tx_skb->skb->len;
  4035. dev_kfree_skb(tx_skb->skb);
  4036. tx_skb->skb = NULL;
  4037. }
  4038. dirty_tx++;
  4039. tx_left--;
  4040. }
  4041. if (tp->dirty_tx != dirty_tx) {
  4042. tp->dirty_tx = dirty_tx;
  4043. smp_wmb();
  4044. if (netif_queue_stopped(dev) &&
  4045. (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)) {
  4046. netif_wake_queue(dev);
  4047. }
  4048. /*
  4049. * 8168 hack: TxPoll requests are lost when the Tx packets are
  4050. * too close. Let's kick an extra TxPoll request when a burst
  4051. * of start_xmit activity is detected (if it is not detected,
  4052. * it is slow enough). -- FR
  4053. */
  4054. smp_rmb();
  4055. if (tp->cur_tx != dirty_tx)
  4056. RTL_W8(TxPoll, NPQ);
  4057. }
  4058. }
  4059. static inline int rtl8169_fragmented_frame(u32 status)
  4060. {
  4061. return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
  4062. }
  4063. static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
  4064. {
  4065. u32 status = opts1 & RxProtoMask;
  4066. if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
  4067. ((status == RxProtoUDP) && !(opts1 & UDPFail)))
  4068. skb->ip_summed = CHECKSUM_UNNECESSARY;
  4069. else
  4070. skb_checksum_none_assert(skb);
  4071. }
  4072. static struct sk_buff *rtl8169_try_rx_copy(void *data,
  4073. struct rtl8169_private *tp,
  4074. int pkt_size,
  4075. dma_addr_t addr)
  4076. {
  4077. struct sk_buff *skb;
  4078. struct device *d = &tp->pci_dev->dev;
  4079. data = rtl8169_align(data);
  4080. dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
  4081. prefetch(data);
  4082. skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
  4083. if (skb)
  4084. memcpy(skb->data, data, pkt_size);
  4085. dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
  4086. return skb;
  4087. }
  4088. /*
  4089. * Warning : rtl8169_rx_interrupt() might be called :
  4090. * 1) from NAPI (softirq) context
  4091. * (polling = 1 : we should call netif_receive_skb())
  4092. * 2) from process context (rtl8169_reset_task())
  4093. * (polling = 0 : we must call netif_rx() instead)
  4094. */
  4095. static int rtl8169_rx_interrupt(struct net_device *dev,
  4096. struct rtl8169_private *tp,
  4097. void __iomem *ioaddr, u32 budget)
  4098. {
  4099. unsigned int cur_rx, rx_left;
  4100. unsigned int count;
  4101. int polling = (budget != ~(u32)0) ? 1 : 0;
  4102. cur_rx = tp->cur_rx;
  4103. rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
  4104. rx_left = min(rx_left, budget);
  4105. for (; rx_left > 0; rx_left--, cur_rx++) {
  4106. unsigned int entry = cur_rx % NUM_RX_DESC;
  4107. struct RxDesc *desc = tp->RxDescArray + entry;
  4108. u32 status;
  4109. rmb();
  4110. status = le32_to_cpu(desc->opts1);
  4111. if (status & DescOwn)
  4112. break;
  4113. if (unlikely(status & RxRES)) {
  4114. netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
  4115. status);
  4116. dev->stats.rx_errors++;
  4117. if (status & (RxRWT | RxRUNT))
  4118. dev->stats.rx_length_errors++;
  4119. if (status & RxCRC)
  4120. dev->stats.rx_crc_errors++;
  4121. if (status & RxFOVF) {
  4122. rtl8169_schedule_work(dev, rtl8169_reset_task);
  4123. dev->stats.rx_fifo_errors++;
  4124. }
  4125. rtl8169_mark_to_asic(desc, rx_buf_sz);
  4126. } else {
  4127. struct sk_buff *skb;
  4128. dma_addr_t addr = le64_to_cpu(desc->addr);
  4129. int pkt_size = (status & 0x00001FFF) - 4;
  4130. /*
  4131. * The driver does not support incoming fragmented
  4132. * frames. They are seen as a symptom of over-mtu
  4133. * sized frames.
  4134. */
  4135. if (unlikely(rtl8169_fragmented_frame(status))) {
  4136. dev->stats.rx_dropped++;
  4137. dev->stats.rx_length_errors++;
  4138. rtl8169_mark_to_asic(desc, rx_buf_sz);
  4139. continue;
  4140. }
  4141. skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
  4142. tp, pkt_size, addr);
  4143. rtl8169_mark_to_asic(desc, rx_buf_sz);
  4144. if (!skb) {
  4145. dev->stats.rx_dropped++;
  4146. continue;
  4147. }
  4148. rtl8169_rx_csum(skb, status);
  4149. skb_put(skb, pkt_size);
  4150. skb->protocol = eth_type_trans(skb, dev);
  4151. rtl8169_rx_vlan_tag(desc, skb);
  4152. if (likely(polling))
  4153. napi_gro_receive(&tp->napi, skb);
  4154. else
  4155. netif_rx(skb);
  4156. dev->stats.rx_bytes += pkt_size;
  4157. dev->stats.rx_packets++;
  4158. }
  4159. /* Work around for AMD plateform. */
  4160. if ((desc->opts2 & cpu_to_le32(0xfffe000)) &&
  4161. (tp->mac_version == RTL_GIGA_MAC_VER_05)) {
  4162. desc->opts2 = 0;
  4163. cur_rx++;
  4164. }
  4165. }
  4166. count = cur_rx - tp->cur_rx;
  4167. tp->cur_rx = cur_rx;
  4168. tp->dirty_rx += count;
  4169. return count;
  4170. }
  4171. static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
  4172. {
  4173. struct net_device *dev = dev_instance;
  4174. struct rtl8169_private *tp = netdev_priv(dev);
  4175. void __iomem *ioaddr = tp->mmio_addr;
  4176. int handled = 0;
  4177. int status;
  4178. /* loop handling interrupts until we have no new ones or
  4179. * we hit a invalid/hotplug case.
  4180. */
  4181. status = RTL_R16(IntrStatus);
  4182. while (status && status != 0xffff) {
  4183. handled = 1;
  4184. /* Handle all of the error cases first. These will reset
  4185. * the chip, so just exit the loop.
  4186. */
  4187. if (unlikely(!netif_running(dev))) {
  4188. rtl8169_asic_down(ioaddr);
  4189. break;
  4190. }
  4191. if (unlikely(status & RxFIFOOver)) {
  4192. switch (tp->mac_version) {
  4193. /* Work around for rx fifo overflow */
  4194. case RTL_GIGA_MAC_VER_11:
  4195. case RTL_GIGA_MAC_VER_22:
  4196. case RTL_GIGA_MAC_VER_26:
  4197. netif_stop_queue(dev);
  4198. rtl8169_tx_timeout(dev);
  4199. goto done;
  4200. /* Testers needed. */
  4201. case RTL_GIGA_MAC_VER_17:
  4202. case RTL_GIGA_MAC_VER_19:
  4203. case RTL_GIGA_MAC_VER_20:
  4204. case RTL_GIGA_MAC_VER_21:
  4205. case RTL_GIGA_MAC_VER_23:
  4206. case RTL_GIGA_MAC_VER_24:
  4207. case RTL_GIGA_MAC_VER_27:
  4208. case RTL_GIGA_MAC_VER_28:
  4209. case RTL_GIGA_MAC_VER_31:
  4210. /* Experimental science. Pktgen proof. */
  4211. case RTL_GIGA_MAC_VER_12:
  4212. case RTL_GIGA_MAC_VER_25:
  4213. if (status == RxFIFOOver)
  4214. goto done;
  4215. break;
  4216. default:
  4217. break;
  4218. }
  4219. }
  4220. if (unlikely(status & SYSErr)) {
  4221. rtl8169_pcierr_interrupt(dev);
  4222. break;
  4223. }
  4224. if (status & LinkChg)
  4225. __rtl8169_check_link_status(dev, tp, ioaddr, true);
  4226. /* We need to see the lastest version of tp->intr_mask to
  4227. * avoid ignoring an MSI interrupt and having to wait for
  4228. * another event which may never come.
  4229. */
  4230. smp_rmb();
  4231. if (status & tp->intr_mask & tp->napi_event) {
  4232. RTL_W16(IntrMask, tp->intr_event & ~tp->napi_event);
  4233. tp->intr_mask = ~tp->napi_event;
  4234. if (likely(napi_schedule_prep(&tp->napi)))
  4235. __napi_schedule(&tp->napi);
  4236. else
  4237. netif_info(tp, intr, dev,
  4238. "interrupt %04x in poll\n", status);
  4239. }
  4240. /* We only get a new MSI interrupt when all active irq
  4241. * sources on the chip have been acknowledged. So, ack
  4242. * everything we've seen and check if new sources have become
  4243. * active to avoid blocking all interrupts from the chip.
  4244. */
  4245. RTL_W16(IntrStatus,
  4246. (status & RxFIFOOver) ? (status | RxOverflow) : status);
  4247. status = RTL_R16(IntrStatus);
  4248. }
  4249. done:
  4250. return IRQ_RETVAL(handled);
  4251. }
  4252. static int rtl8169_poll(struct napi_struct *napi, int budget)
  4253. {
  4254. struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
  4255. struct net_device *dev = tp->dev;
  4256. void __iomem *ioaddr = tp->mmio_addr;
  4257. int work_done;
  4258. work_done = rtl8169_rx_interrupt(dev, tp, ioaddr, (u32) budget);
  4259. rtl8169_tx_interrupt(dev, tp, ioaddr);
  4260. if (work_done < budget) {
  4261. napi_complete(napi);
  4262. /* We need for force the visibility of tp->intr_mask
  4263. * for other CPUs, as we can loose an MSI interrupt
  4264. * and potentially wait for a retransmit timeout if we don't.
  4265. * The posted write to IntrMask is safe, as it will
  4266. * eventually make it to the chip and we won't loose anything
  4267. * until it does.
  4268. */
  4269. tp->intr_mask = 0xffff;
  4270. wmb();
  4271. RTL_W16(IntrMask, tp->intr_event);
  4272. }
  4273. return work_done;
  4274. }
  4275. static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
  4276. {
  4277. struct rtl8169_private *tp = netdev_priv(dev);
  4278. if (tp->mac_version > RTL_GIGA_MAC_VER_06)
  4279. return;
  4280. dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
  4281. RTL_W32(RxMissed, 0);
  4282. }
  4283. static void rtl8169_down(struct net_device *dev)
  4284. {
  4285. struct rtl8169_private *tp = netdev_priv(dev);
  4286. void __iomem *ioaddr = tp->mmio_addr;
  4287. rtl8169_delete_timer(dev);
  4288. netif_stop_queue(dev);
  4289. napi_disable(&tp->napi);
  4290. spin_lock_irq(&tp->lock);
  4291. rtl8169_asic_down(ioaddr);
  4292. /*
  4293. * At this point device interrupts can not be enabled in any function,
  4294. * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task,
  4295. * rtl8169_reinit_task) and napi is disabled (rtl8169_poll).
  4296. */
  4297. rtl8169_rx_missed(dev, ioaddr);
  4298. spin_unlock_irq(&tp->lock);
  4299. synchronize_irq(dev->irq);
  4300. /* Give a racing hard_start_xmit a few cycles to complete. */
  4301. synchronize_sched(); /* FIXME: should this be synchronize_irq()? */
  4302. rtl8169_tx_clear(tp);
  4303. rtl8169_rx_clear(tp);
  4304. rtl_pll_power_down(tp);
  4305. }
  4306. static int rtl8169_close(struct net_device *dev)
  4307. {
  4308. struct rtl8169_private *tp = netdev_priv(dev);
  4309. struct pci_dev *pdev = tp->pci_dev;
  4310. pm_runtime_get_sync(&pdev->dev);
  4311. /* update counters before going down */
  4312. rtl8169_update_counters(dev);
  4313. rtl8169_down(dev);
  4314. free_irq(dev->irq, dev);
  4315. dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
  4316. tp->RxPhyAddr);
  4317. dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
  4318. tp->TxPhyAddr);
  4319. tp->TxDescArray = NULL;
  4320. tp->RxDescArray = NULL;
  4321. pm_runtime_put_sync(&pdev->dev);
  4322. return 0;
  4323. }
  4324. static void rtl_set_rx_mode(struct net_device *dev)
  4325. {
  4326. struct rtl8169_private *tp = netdev_priv(dev);
  4327. void __iomem *ioaddr = tp->mmio_addr;
  4328. unsigned long flags;
  4329. u32 mc_filter[2]; /* Multicast hash filter */
  4330. int rx_mode;
  4331. u32 tmp = 0;
  4332. if (dev->flags & IFF_PROMISC) {
  4333. /* Unconditionally log net taps. */
  4334. netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
  4335. rx_mode =
  4336. AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
  4337. AcceptAllPhys;
  4338. mc_filter[1] = mc_filter[0] = 0xffffffff;
  4339. } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
  4340. (dev->flags & IFF_ALLMULTI)) {
  4341. /* Too many to filter perfectly -- accept all multicasts. */
  4342. rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
  4343. mc_filter[1] = mc_filter[0] = 0xffffffff;
  4344. } else {
  4345. struct netdev_hw_addr *ha;
  4346. rx_mode = AcceptBroadcast | AcceptMyPhys;
  4347. mc_filter[1] = mc_filter[0] = 0;
  4348. netdev_for_each_mc_addr(ha, dev) {
  4349. int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
  4350. mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
  4351. rx_mode |= AcceptMulticast;
  4352. }
  4353. }
  4354. spin_lock_irqsave(&tp->lock, flags);
  4355. tmp = rtl8169_rx_config | rx_mode |
  4356. (RTL_R32(RxConfig) & RTL_RX_CONFIG_MASK);
  4357. if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
  4358. u32 data = mc_filter[0];
  4359. mc_filter[0] = swab32(mc_filter[1]);
  4360. mc_filter[1] = swab32(data);
  4361. }
  4362. RTL_W32(MAR0 + 4, mc_filter[1]);
  4363. RTL_W32(MAR0 + 0, mc_filter[0]);
  4364. RTL_W32(RxConfig, tmp);
  4365. spin_unlock_irqrestore(&tp->lock, flags);
  4366. }
  4367. /**
  4368. * rtl8169_get_stats - Get rtl8169 read/write statistics
  4369. * @dev: The Ethernet Device to get statistics for
  4370. *
  4371. * Get TX/RX statistics for rtl8169
  4372. */
  4373. static struct net_device_stats *rtl8169_get_stats(struct net_device *dev)
  4374. {
  4375. struct rtl8169_private *tp = netdev_priv(dev);
  4376. void __iomem *ioaddr = tp->mmio_addr;
  4377. unsigned long flags;
  4378. if (netif_running(dev)) {
  4379. spin_lock_irqsave(&tp->lock, flags);
  4380. rtl8169_rx_missed(dev, ioaddr);
  4381. spin_unlock_irqrestore(&tp->lock, flags);
  4382. }
  4383. return &dev->stats;
  4384. }
  4385. static void rtl8169_net_suspend(struct net_device *dev)
  4386. {
  4387. struct rtl8169_private *tp = netdev_priv(dev);
  4388. if (!netif_running(dev))
  4389. return;
  4390. rtl_pll_power_down(tp);
  4391. netif_device_detach(dev);
  4392. netif_stop_queue(dev);
  4393. }
  4394. #ifdef CONFIG_PM
  4395. static int rtl8169_suspend(struct device *device)
  4396. {
  4397. struct pci_dev *pdev = to_pci_dev(device);
  4398. struct net_device *dev = pci_get_drvdata(pdev);
  4399. rtl8169_net_suspend(dev);
  4400. return 0;
  4401. }
  4402. static void __rtl8169_resume(struct net_device *dev)
  4403. {
  4404. struct rtl8169_private *tp = netdev_priv(dev);
  4405. netif_device_attach(dev);
  4406. rtl_pll_power_up(tp);
  4407. rtl8169_schedule_work(dev, rtl8169_reset_task);
  4408. }
  4409. static int rtl8169_resume(struct device *device)
  4410. {
  4411. struct pci_dev *pdev = to_pci_dev(device);
  4412. struct net_device *dev = pci_get_drvdata(pdev);
  4413. struct rtl8169_private *tp = netdev_priv(dev);
  4414. rtl8169_init_phy(dev, tp);
  4415. if (netif_running(dev))
  4416. __rtl8169_resume(dev);
  4417. return 0;
  4418. }
  4419. static int rtl8169_runtime_suspend(struct device *device)
  4420. {
  4421. struct pci_dev *pdev = to_pci_dev(device);
  4422. struct net_device *dev = pci_get_drvdata(pdev);
  4423. struct rtl8169_private *tp = netdev_priv(dev);
  4424. if (!tp->TxDescArray)
  4425. return 0;
  4426. spin_lock_irq(&tp->lock);
  4427. tp->saved_wolopts = __rtl8169_get_wol(tp);
  4428. __rtl8169_set_wol(tp, WAKE_ANY);
  4429. spin_unlock_irq(&tp->lock);
  4430. rtl8169_net_suspend(dev);
  4431. return 0;
  4432. }
  4433. static int rtl8169_runtime_resume(struct device *device)
  4434. {
  4435. struct pci_dev *pdev = to_pci_dev(device);
  4436. struct net_device *dev = pci_get_drvdata(pdev);
  4437. struct rtl8169_private *tp = netdev_priv(dev);
  4438. if (!tp->TxDescArray)
  4439. return 0;
  4440. spin_lock_irq(&tp->lock);
  4441. __rtl8169_set_wol(tp, tp->saved_wolopts);
  4442. tp->saved_wolopts = 0;
  4443. spin_unlock_irq(&tp->lock);
  4444. rtl8169_init_phy(dev, tp);
  4445. __rtl8169_resume(dev);
  4446. return 0;
  4447. }
  4448. static int rtl8169_runtime_idle(struct device *device)
  4449. {
  4450. struct pci_dev *pdev = to_pci_dev(device);
  4451. struct net_device *dev = pci_get_drvdata(pdev);
  4452. struct rtl8169_private *tp = netdev_priv(dev);
  4453. return tp->TxDescArray ? -EBUSY : 0;
  4454. }
  4455. static const struct dev_pm_ops rtl8169_pm_ops = {
  4456. .suspend = rtl8169_suspend,
  4457. .resume = rtl8169_resume,
  4458. .freeze = rtl8169_suspend,
  4459. .thaw = rtl8169_resume,
  4460. .poweroff = rtl8169_suspend,
  4461. .restore = rtl8169_resume,
  4462. .runtime_suspend = rtl8169_runtime_suspend,
  4463. .runtime_resume = rtl8169_runtime_resume,
  4464. .runtime_idle = rtl8169_runtime_idle,
  4465. };
  4466. #define RTL8169_PM_OPS (&rtl8169_pm_ops)
  4467. #else /* !CONFIG_PM */
  4468. #define RTL8169_PM_OPS NULL
  4469. #endif /* !CONFIG_PM */
  4470. static void rtl_shutdown(struct pci_dev *pdev)
  4471. {
  4472. struct net_device *dev = pci_get_drvdata(pdev);
  4473. struct rtl8169_private *tp = netdev_priv(dev);
  4474. void __iomem *ioaddr = tp->mmio_addr;
  4475. rtl8169_net_suspend(dev);
  4476. /* restore original MAC address */
  4477. rtl_rar_set(tp, dev->perm_addr);
  4478. spin_lock_irq(&tp->lock);
  4479. rtl8169_asic_down(ioaddr);
  4480. spin_unlock_irq(&tp->lock);
  4481. if (system_state == SYSTEM_POWER_OFF) {
  4482. /* WoL fails with some 8168 when the receiver is disabled. */
  4483. if (tp->features & RTL_FEATURE_WOL) {
  4484. pci_clear_master(pdev);
  4485. RTL_W8(ChipCmd, CmdRxEnb);
  4486. /* PCI commit */
  4487. RTL_R8(ChipCmd);
  4488. }
  4489. pci_wake_from_d3(pdev, true);
  4490. pci_set_power_state(pdev, PCI_D3hot);
  4491. }
  4492. }
  4493. static struct pci_driver rtl8169_pci_driver = {
  4494. .name = MODULENAME,
  4495. .id_table = rtl8169_pci_tbl,
  4496. .probe = rtl8169_init_one,
  4497. .remove = __devexit_p(rtl8169_remove_one),
  4498. .shutdown = rtl_shutdown,
  4499. .driver.pm = RTL8169_PM_OPS,
  4500. };
  4501. static int __init rtl8169_init_module(void)
  4502. {
  4503. return pci_register_driver(&rtl8169_pci_driver);
  4504. }
  4505. static void __exit rtl8169_cleanup_module(void)
  4506. {
  4507. pci_unregister_driver(&rtl8169_pci_driver);
  4508. }
  4509. module_init(rtl8169_init_module);
  4510. module_exit(rtl8169_cleanup_module);