r8169.c 119 KB

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