r8169.c 120 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050
  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(struct rtl8169_private *tp, u8 cmd)
  549. {
  550. void __iomem *ioaddr = tp->mmio_addr;
  551. int i;
  552. RTL_W8(ERIDR, cmd);
  553. RTL_W32(ERIAR, 0x800010e8);
  554. msleep(2);
  555. for (i = 0; i < 5; i++) {
  556. udelay(100);
  557. if (!(RTL_R32(ERIDR) & ERIAR_FLAG))
  558. break;
  559. }
  560. ocp_write(tp, 0x1, 0x30, 0x00000001);
  561. }
  562. #define OOB_CMD_RESET 0x00
  563. #define OOB_CMD_DRIVER_START 0x05
  564. #define OOB_CMD_DRIVER_STOP 0x06
  565. static void rtl8168_driver_start(struct rtl8169_private *tp)
  566. {
  567. int i;
  568. rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
  569. for (i = 0; i < 10; i++) {
  570. msleep(10);
  571. if (ocp_read(tp, 0x0f, 0x0010) & 0x00000800)
  572. break;
  573. }
  574. }
  575. static void rtl8168_driver_stop(struct rtl8169_private *tp)
  576. {
  577. int i;
  578. rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
  579. for (i = 0; i < 10; i++) {
  580. msleep(10);
  581. if ((ocp_read(tp, 0x0f, 0x0010) & 0x00000800) == 0)
  582. break;
  583. }
  584. }
  585. static void r8169_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
  586. {
  587. int i;
  588. RTL_W32(PHYAR, 0x80000000 | (reg_addr & 0x1f) << 16 | (value & 0xffff));
  589. for (i = 20; i > 0; i--) {
  590. /*
  591. * Check if the RTL8169 has completed writing to the specified
  592. * MII register.
  593. */
  594. if (!(RTL_R32(PHYAR) & 0x80000000))
  595. break;
  596. udelay(25);
  597. }
  598. /*
  599. * According to hardware specs a 20us delay is required after write
  600. * complete indication, but before sending next command.
  601. */
  602. udelay(20);
  603. }
  604. static int r8169_mdio_read(void __iomem *ioaddr, int reg_addr)
  605. {
  606. int i, value = -1;
  607. RTL_W32(PHYAR, 0x0 | (reg_addr & 0x1f) << 16);
  608. for (i = 20; i > 0; i--) {
  609. /*
  610. * Check if the RTL8169 has completed retrieving data from
  611. * the specified MII register.
  612. */
  613. if (RTL_R32(PHYAR) & 0x80000000) {
  614. value = RTL_R32(PHYAR) & 0xffff;
  615. break;
  616. }
  617. udelay(25);
  618. }
  619. /*
  620. * According to hardware specs a 20us delay is required after read
  621. * complete indication, but before sending next command.
  622. */
  623. udelay(20);
  624. return value;
  625. }
  626. static void r8168dp_1_mdio_access(void __iomem *ioaddr, int reg_addr, u32 data)
  627. {
  628. int i;
  629. RTL_W32(OCPDR, data |
  630. ((reg_addr & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
  631. RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
  632. RTL_W32(EPHY_RXER_NUM, 0);
  633. for (i = 0; i < 100; i++) {
  634. mdelay(1);
  635. if (!(RTL_R32(OCPAR) & OCPAR_FLAG))
  636. break;
  637. }
  638. }
  639. static void r8168dp_1_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
  640. {
  641. r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_WRITE_CMD |
  642. (value & OCPDR_DATA_MASK));
  643. }
  644. static int r8168dp_1_mdio_read(void __iomem *ioaddr, int reg_addr)
  645. {
  646. int i;
  647. r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_READ_CMD);
  648. mdelay(1);
  649. RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
  650. RTL_W32(EPHY_RXER_NUM, 0);
  651. for (i = 0; i < 100; i++) {
  652. mdelay(1);
  653. if (RTL_R32(OCPAR) & OCPAR_FLAG)
  654. break;
  655. }
  656. return RTL_R32(OCPDR) & OCPDR_DATA_MASK;
  657. }
  658. #define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
  659. static void r8168dp_2_mdio_start(void __iomem *ioaddr)
  660. {
  661. RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
  662. }
  663. static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
  664. {
  665. RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
  666. }
  667. static void r8168dp_2_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
  668. {
  669. r8168dp_2_mdio_start(ioaddr);
  670. r8169_mdio_write(ioaddr, reg_addr, value);
  671. r8168dp_2_mdio_stop(ioaddr);
  672. }
  673. static int r8168dp_2_mdio_read(void __iomem *ioaddr, int reg_addr)
  674. {
  675. int value;
  676. r8168dp_2_mdio_start(ioaddr);
  677. value = r8169_mdio_read(ioaddr, reg_addr);
  678. r8168dp_2_mdio_stop(ioaddr);
  679. return value;
  680. }
  681. static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
  682. {
  683. tp->mdio_ops.write(tp->mmio_addr, location, val);
  684. }
  685. static int rtl_readphy(struct rtl8169_private *tp, int location)
  686. {
  687. return tp->mdio_ops.read(tp->mmio_addr, location);
  688. }
  689. static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
  690. {
  691. rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
  692. }
  693. static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
  694. {
  695. int val;
  696. val = rtl_readphy(tp, reg_addr);
  697. rtl_writephy(tp, reg_addr, (val | p) & ~m);
  698. }
  699. static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
  700. int val)
  701. {
  702. struct rtl8169_private *tp = netdev_priv(dev);
  703. rtl_writephy(tp, location, val);
  704. }
  705. static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
  706. {
  707. struct rtl8169_private *tp = netdev_priv(dev);
  708. return rtl_readphy(tp, location);
  709. }
  710. static void rtl_ephy_write(void __iomem *ioaddr, int reg_addr, int value)
  711. {
  712. unsigned int i;
  713. RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
  714. (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
  715. for (i = 0; i < 100; i++) {
  716. if (!(RTL_R32(EPHYAR) & EPHYAR_FLAG))
  717. break;
  718. udelay(10);
  719. }
  720. }
  721. static u16 rtl_ephy_read(void __iomem *ioaddr, int reg_addr)
  722. {
  723. u16 value = 0xffff;
  724. unsigned int i;
  725. RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
  726. for (i = 0; i < 100; i++) {
  727. if (RTL_R32(EPHYAR) & EPHYAR_FLAG) {
  728. value = RTL_R32(EPHYAR) & EPHYAR_DATA_MASK;
  729. break;
  730. }
  731. udelay(10);
  732. }
  733. return value;
  734. }
  735. static void rtl_csi_write(void __iomem *ioaddr, int addr, int value)
  736. {
  737. unsigned int i;
  738. RTL_W32(CSIDR, value);
  739. RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
  740. CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
  741. for (i = 0; i < 100; i++) {
  742. if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
  743. break;
  744. udelay(10);
  745. }
  746. }
  747. static u32 rtl_csi_read(void __iomem *ioaddr, int addr)
  748. {
  749. u32 value = ~0x00;
  750. unsigned int i;
  751. RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
  752. CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
  753. for (i = 0; i < 100; i++) {
  754. if (RTL_R32(CSIAR) & CSIAR_FLAG) {
  755. value = RTL_R32(CSIDR);
  756. break;
  757. }
  758. udelay(10);
  759. }
  760. return value;
  761. }
  762. static u8 rtl8168d_efuse_read(void __iomem *ioaddr, int reg_addr)
  763. {
  764. u8 value = 0xff;
  765. unsigned int i;
  766. RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
  767. for (i = 0; i < 300; i++) {
  768. if (RTL_R32(EFUSEAR) & EFUSEAR_FLAG) {
  769. value = RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK;
  770. break;
  771. }
  772. udelay(100);
  773. }
  774. return value;
  775. }
  776. static void rtl8169_irq_mask_and_ack(void __iomem *ioaddr)
  777. {
  778. RTL_W16(IntrMask, 0x0000);
  779. RTL_W16(IntrStatus, 0xffff);
  780. }
  781. static void rtl8169_asic_down(void __iomem *ioaddr)
  782. {
  783. RTL_W8(ChipCmd, 0x00);
  784. rtl8169_irq_mask_and_ack(ioaddr);
  785. RTL_R16(CPlusCmd);
  786. }
  787. static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
  788. {
  789. void __iomem *ioaddr = tp->mmio_addr;
  790. return RTL_R32(TBICSR) & TBIReset;
  791. }
  792. static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
  793. {
  794. return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
  795. }
  796. static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
  797. {
  798. return RTL_R32(TBICSR) & TBILinkOk;
  799. }
  800. static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
  801. {
  802. return RTL_R8(PHYstatus) & LinkStatus;
  803. }
  804. static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
  805. {
  806. void __iomem *ioaddr = tp->mmio_addr;
  807. RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
  808. }
  809. static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
  810. {
  811. unsigned int val;
  812. val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
  813. rtl_writephy(tp, MII_BMCR, val & 0xffff);
  814. }
  815. static void __rtl8169_check_link_status(struct net_device *dev,
  816. struct rtl8169_private *tp,
  817. void __iomem *ioaddr,
  818. bool pm)
  819. {
  820. unsigned long flags;
  821. spin_lock_irqsave(&tp->lock, flags);
  822. if (tp->link_ok(ioaddr)) {
  823. /* This is to cancel a scheduled suspend if there's one. */
  824. if (pm)
  825. pm_request_resume(&tp->pci_dev->dev);
  826. netif_carrier_on(dev);
  827. if (net_ratelimit())
  828. netif_info(tp, ifup, dev, "link up\n");
  829. } else {
  830. netif_carrier_off(dev);
  831. netif_info(tp, ifdown, dev, "link down\n");
  832. if (pm)
  833. pm_schedule_suspend(&tp->pci_dev->dev, 100);
  834. }
  835. spin_unlock_irqrestore(&tp->lock, flags);
  836. }
  837. static void rtl8169_check_link_status(struct net_device *dev,
  838. struct rtl8169_private *tp,
  839. void __iomem *ioaddr)
  840. {
  841. __rtl8169_check_link_status(dev, tp, ioaddr, false);
  842. }
  843. #define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
  844. static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
  845. {
  846. void __iomem *ioaddr = tp->mmio_addr;
  847. u8 options;
  848. u32 wolopts = 0;
  849. options = RTL_R8(Config1);
  850. if (!(options & PMEnable))
  851. return 0;
  852. options = RTL_R8(Config3);
  853. if (options & LinkUp)
  854. wolopts |= WAKE_PHY;
  855. if (options & MagicPacket)
  856. wolopts |= WAKE_MAGIC;
  857. options = RTL_R8(Config5);
  858. if (options & UWF)
  859. wolopts |= WAKE_UCAST;
  860. if (options & BWF)
  861. wolopts |= WAKE_BCAST;
  862. if (options & MWF)
  863. wolopts |= WAKE_MCAST;
  864. return wolopts;
  865. }
  866. static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  867. {
  868. struct rtl8169_private *tp = netdev_priv(dev);
  869. spin_lock_irq(&tp->lock);
  870. wol->supported = WAKE_ANY;
  871. wol->wolopts = __rtl8169_get_wol(tp);
  872. spin_unlock_irq(&tp->lock);
  873. }
  874. static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
  875. {
  876. void __iomem *ioaddr = tp->mmio_addr;
  877. unsigned int i;
  878. static const struct {
  879. u32 opt;
  880. u16 reg;
  881. u8 mask;
  882. } cfg[] = {
  883. { WAKE_ANY, Config1, PMEnable },
  884. { WAKE_PHY, Config3, LinkUp },
  885. { WAKE_MAGIC, Config3, MagicPacket },
  886. { WAKE_UCAST, Config5, UWF },
  887. { WAKE_BCAST, Config5, BWF },
  888. { WAKE_MCAST, Config5, MWF },
  889. { WAKE_ANY, Config5, LanWake }
  890. };
  891. RTL_W8(Cfg9346, Cfg9346_Unlock);
  892. for (i = 0; i < ARRAY_SIZE(cfg); i++) {
  893. u8 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
  894. if (wolopts & cfg[i].opt)
  895. options |= cfg[i].mask;
  896. RTL_W8(cfg[i].reg, options);
  897. }
  898. RTL_W8(Cfg9346, Cfg9346_Lock);
  899. }
  900. static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  901. {
  902. struct rtl8169_private *tp = netdev_priv(dev);
  903. spin_lock_irq(&tp->lock);
  904. if (wol->wolopts)
  905. tp->features |= RTL_FEATURE_WOL;
  906. else
  907. tp->features &= ~RTL_FEATURE_WOL;
  908. __rtl8169_set_wol(tp, wol->wolopts);
  909. spin_unlock_irq(&tp->lock);
  910. device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
  911. return 0;
  912. }
  913. static void rtl8169_get_drvinfo(struct net_device *dev,
  914. struct ethtool_drvinfo *info)
  915. {
  916. struct rtl8169_private *tp = netdev_priv(dev);
  917. strcpy(info->driver, MODULENAME);
  918. strcpy(info->version, RTL8169_VERSION);
  919. strcpy(info->bus_info, pci_name(tp->pci_dev));
  920. }
  921. static int rtl8169_get_regs_len(struct net_device *dev)
  922. {
  923. return R8169_REGS_SIZE;
  924. }
  925. static int rtl8169_set_speed_tbi(struct net_device *dev,
  926. u8 autoneg, u16 speed, u8 duplex)
  927. {
  928. struct rtl8169_private *tp = netdev_priv(dev);
  929. void __iomem *ioaddr = tp->mmio_addr;
  930. int ret = 0;
  931. u32 reg;
  932. reg = RTL_R32(TBICSR);
  933. if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
  934. (duplex == DUPLEX_FULL)) {
  935. RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
  936. } else if (autoneg == AUTONEG_ENABLE)
  937. RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
  938. else {
  939. netif_warn(tp, link, dev,
  940. "incorrect speed setting refused in TBI mode\n");
  941. ret = -EOPNOTSUPP;
  942. }
  943. return ret;
  944. }
  945. static int rtl8169_set_speed_xmii(struct net_device *dev,
  946. u8 autoneg, u16 speed, u8 duplex)
  947. {
  948. struct rtl8169_private *tp = netdev_priv(dev);
  949. int giga_ctrl, bmcr;
  950. if (autoneg == AUTONEG_ENABLE) {
  951. int auto_nego;
  952. auto_nego = rtl_readphy(tp, MII_ADVERTISE);
  953. auto_nego |= (ADVERTISE_10HALF | ADVERTISE_10FULL |
  954. ADVERTISE_100HALF | ADVERTISE_100FULL);
  955. auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
  956. giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
  957. giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
  958. /* The 8100e/8101e/8102e do Fast Ethernet only. */
  959. if ((tp->mac_version != RTL_GIGA_MAC_VER_07) &&
  960. (tp->mac_version != RTL_GIGA_MAC_VER_08) &&
  961. (tp->mac_version != RTL_GIGA_MAC_VER_09) &&
  962. (tp->mac_version != RTL_GIGA_MAC_VER_10) &&
  963. (tp->mac_version != RTL_GIGA_MAC_VER_13) &&
  964. (tp->mac_version != RTL_GIGA_MAC_VER_14) &&
  965. (tp->mac_version != RTL_GIGA_MAC_VER_15) &&
  966. (tp->mac_version != RTL_GIGA_MAC_VER_16)) {
  967. giga_ctrl |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
  968. } else {
  969. netif_info(tp, link, dev,
  970. "PHY does not support 1000Mbps\n");
  971. }
  972. bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
  973. if ((tp->mac_version == RTL_GIGA_MAC_VER_11) ||
  974. (tp->mac_version == RTL_GIGA_MAC_VER_12) ||
  975. (tp->mac_version >= RTL_GIGA_MAC_VER_17)) {
  976. /*
  977. * Wake up the PHY.
  978. * Vendor specific (0x1f) and reserved (0x0e) MII
  979. * registers.
  980. */
  981. rtl_writephy(tp, 0x1f, 0x0000);
  982. rtl_writephy(tp, 0x0e, 0x0000);
  983. }
  984. rtl_writephy(tp, MII_ADVERTISE, auto_nego);
  985. rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
  986. } else {
  987. giga_ctrl = 0;
  988. if (speed == SPEED_10)
  989. bmcr = 0;
  990. else if (speed == SPEED_100)
  991. bmcr = BMCR_SPEED100;
  992. else
  993. return -EINVAL;
  994. if (duplex == DUPLEX_FULL)
  995. bmcr |= BMCR_FULLDPLX;
  996. rtl_writephy(tp, 0x1f, 0x0000);
  997. }
  998. tp->phy_1000_ctrl_reg = giga_ctrl;
  999. rtl_writephy(tp, MII_BMCR, bmcr);
  1000. if ((tp->mac_version == RTL_GIGA_MAC_VER_02) ||
  1001. (tp->mac_version == RTL_GIGA_MAC_VER_03)) {
  1002. if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
  1003. rtl_writephy(tp, 0x17, 0x2138);
  1004. rtl_writephy(tp, 0x0e, 0x0260);
  1005. } else {
  1006. rtl_writephy(tp, 0x17, 0x2108);
  1007. rtl_writephy(tp, 0x0e, 0x0000);
  1008. }
  1009. }
  1010. return 0;
  1011. }
  1012. static int rtl8169_set_speed(struct net_device *dev,
  1013. u8 autoneg, u16 speed, u8 duplex)
  1014. {
  1015. struct rtl8169_private *tp = netdev_priv(dev);
  1016. int ret;
  1017. ret = tp->set_speed(dev, autoneg, speed, duplex);
  1018. if (netif_running(dev) && (tp->phy_1000_ctrl_reg & ADVERTISE_1000FULL))
  1019. mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
  1020. return ret;
  1021. }
  1022. static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1023. {
  1024. struct rtl8169_private *tp = netdev_priv(dev);
  1025. unsigned long flags;
  1026. int ret;
  1027. spin_lock_irqsave(&tp->lock, flags);
  1028. ret = rtl8169_set_speed(dev, cmd->autoneg, cmd->speed, cmd->duplex);
  1029. spin_unlock_irqrestore(&tp->lock, flags);
  1030. return ret;
  1031. }
  1032. static u32 rtl8169_get_rx_csum(struct net_device *dev)
  1033. {
  1034. struct rtl8169_private *tp = netdev_priv(dev);
  1035. return tp->cp_cmd & RxChkSum;
  1036. }
  1037. static int rtl8169_set_rx_csum(struct net_device *dev, u32 data)
  1038. {
  1039. struct rtl8169_private *tp = netdev_priv(dev);
  1040. void __iomem *ioaddr = tp->mmio_addr;
  1041. unsigned long flags;
  1042. spin_lock_irqsave(&tp->lock, flags);
  1043. if (data)
  1044. tp->cp_cmd |= RxChkSum;
  1045. else
  1046. tp->cp_cmd &= ~RxChkSum;
  1047. RTL_W16(CPlusCmd, tp->cp_cmd);
  1048. RTL_R16(CPlusCmd);
  1049. spin_unlock_irqrestore(&tp->lock, flags);
  1050. return 0;
  1051. }
  1052. #ifdef CONFIG_R8169_VLAN
  1053. static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
  1054. struct sk_buff *skb)
  1055. {
  1056. return (vlan_tx_tag_present(skb)) ?
  1057. TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
  1058. }
  1059. static void rtl8169_vlan_rx_register(struct net_device *dev,
  1060. struct vlan_group *grp)
  1061. {
  1062. struct rtl8169_private *tp = netdev_priv(dev);
  1063. void __iomem *ioaddr = tp->mmio_addr;
  1064. unsigned long flags;
  1065. spin_lock_irqsave(&tp->lock, flags);
  1066. tp->vlgrp = grp;
  1067. /*
  1068. * Do not disable RxVlan on 8110SCd.
  1069. */
  1070. if (tp->vlgrp || (tp->mac_version == RTL_GIGA_MAC_VER_05))
  1071. tp->cp_cmd |= RxVlan;
  1072. else
  1073. tp->cp_cmd &= ~RxVlan;
  1074. RTL_W16(CPlusCmd, tp->cp_cmd);
  1075. RTL_R16(CPlusCmd);
  1076. spin_unlock_irqrestore(&tp->lock, flags);
  1077. }
  1078. static int rtl8169_rx_vlan_skb(struct rtl8169_private *tp, struct RxDesc *desc,
  1079. struct sk_buff *skb, int polling)
  1080. {
  1081. u32 opts2 = le32_to_cpu(desc->opts2);
  1082. struct vlan_group *vlgrp = tp->vlgrp;
  1083. int ret;
  1084. if (vlgrp && (opts2 & RxVlanTag)) {
  1085. u16 vtag = swab16(opts2 & 0xffff);
  1086. if (likely(polling))
  1087. vlan_gro_receive(&tp->napi, vlgrp, vtag, skb);
  1088. else
  1089. __vlan_hwaccel_rx(skb, vlgrp, vtag, polling);
  1090. ret = 0;
  1091. } else
  1092. ret = -1;
  1093. desc->opts2 = 0;
  1094. return ret;
  1095. }
  1096. #else /* !CONFIG_R8169_VLAN */
  1097. static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
  1098. struct sk_buff *skb)
  1099. {
  1100. return 0;
  1101. }
  1102. static int rtl8169_rx_vlan_skb(struct rtl8169_private *tp, struct RxDesc *desc,
  1103. struct sk_buff *skb, int polling)
  1104. {
  1105. return -1;
  1106. }
  1107. #endif
  1108. static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
  1109. {
  1110. struct rtl8169_private *tp = netdev_priv(dev);
  1111. void __iomem *ioaddr = tp->mmio_addr;
  1112. u32 status;
  1113. cmd->supported =
  1114. SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
  1115. cmd->port = PORT_FIBRE;
  1116. cmd->transceiver = XCVR_INTERNAL;
  1117. status = RTL_R32(TBICSR);
  1118. cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
  1119. cmd->autoneg = !!(status & TBINwEnable);
  1120. cmd->speed = SPEED_1000;
  1121. cmd->duplex = DUPLEX_FULL; /* Always set */
  1122. return 0;
  1123. }
  1124. static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
  1125. {
  1126. struct rtl8169_private *tp = netdev_priv(dev);
  1127. return mii_ethtool_gset(&tp->mii, cmd);
  1128. }
  1129. static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1130. {
  1131. struct rtl8169_private *tp = netdev_priv(dev);
  1132. unsigned long flags;
  1133. int rc;
  1134. spin_lock_irqsave(&tp->lock, flags);
  1135. rc = tp->get_settings(dev, cmd);
  1136. spin_unlock_irqrestore(&tp->lock, flags);
  1137. return rc;
  1138. }
  1139. static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
  1140. void *p)
  1141. {
  1142. struct rtl8169_private *tp = netdev_priv(dev);
  1143. unsigned long flags;
  1144. if (regs->len > R8169_REGS_SIZE)
  1145. regs->len = R8169_REGS_SIZE;
  1146. spin_lock_irqsave(&tp->lock, flags);
  1147. memcpy_fromio(p, tp->mmio_addr, regs->len);
  1148. spin_unlock_irqrestore(&tp->lock, flags);
  1149. }
  1150. static u32 rtl8169_get_msglevel(struct net_device *dev)
  1151. {
  1152. struct rtl8169_private *tp = netdev_priv(dev);
  1153. return tp->msg_enable;
  1154. }
  1155. static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
  1156. {
  1157. struct rtl8169_private *tp = netdev_priv(dev);
  1158. tp->msg_enable = value;
  1159. }
  1160. static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
  1161. "tx_packets",
  1162. "rx_packets",
  1163. "tx_errors",
  1164. "rx_errors",
  1165. "rx_missed",
  1166. "align_errors",
  1167. "tx_single_collisions",
  1168. "tx_multi_collisions",
  1169. "unicast",
  1170. "broadcast",
  1171. "multicast",
  1172. "tx_aborted",
  1173. "tx_underrun",
  1174. };
  1175. static int rtl8169_get_sset_count(struct net_device *dev, int sset)
  1176. {
  1177. switch (sset) {
  1178. case ETH_SS_STATS:
  1179. return ARRAY_SIZE(rtl8169_gstrings);
  1180. default:
  1181. return -EOPNOTSUPP;
  1182. }
  1183. }
  1184. static void rtl8169_update_counters(struct net_device *dev)
  1185. {
  1186. struct rtl8169_private *tp = netdev_priv(dev);
  1187. void __iomem *ioaddr = tp->mmio_addr;
  1188. struct rtl8169_counters *counters;
  1189. dma_addr_t paddr;
  1190. u32 cmd;
  1191. int wait = 1000;
  1192. struct device *d = &tp->pci_dev->dev;
  1193. /*
  1194. * Some chips are unable to dump tally counters when the receiver
  1195. * is disabled.
  1196. */
  1197. if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
  1198. return;
  1199. counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
  1200. if (!counters)
  1201. return;
  1202. RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
  1203. cmd = (u64)paddr & DMA_BIT_MASK(32);
  1204. RTL_W32(CounterAddrLow, cmd);
  1205. RTL_W32(CounterAddrLow, cmd | CounterDump);
  1206. while (wait--) {
  1207. if ((RTL_R32(CounterAddrLow) & CounterDump) == 0) {
  1208. /* copy updated counters */
  1209. memcpy(&tp->counters, counters, sizeof(*counters));
  1210. break;
  1211. }
  1212. udelay(10);
  1213. }
  1214. RTL_W32(CounterAddrLow, 0);
  1215. RTL_W32(CounterAddrHigh, 0);
  1216. dma_free_coherent(d, sizeof(*counters), counters, paddr);
  1217. }
  1218. static void rtl8169_get_ethtool_stats(struct net_device *dev,
  1219. struct ethtool_stats *stats, u64 *data)
  1220. {
  1221. struct rtl8169_private *tp = netdev_priv(dev);
  1222. ASSERT_RTNL();
  1223. rtl8169_update_counters(dev);
  1224. data[0] = le64_to_cpu(tp->counters.tx_packets);
  1225. data[1] = le64_to_cpu(tp->counters.rx_packets);
  1226. data[2] = le64_to_cpu(tp->counters.tx_errors);
  1227. data[3] = le32_to_cpu(tp->counters.rx_errors);
  1228. data[4] = le16_to_cpu(tp->counters.rx_missed);
  1229. data[5] = le16_to_cpu(tp->counters.align_errors);
  1230. data[6] = le32_to_cpu(tp->counters.tx_one_collision);
  1231. data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
  1232. data[8] = le64_to_cpu(tp->counters.rx_unicast);
  1233. data[9] = le64_to_cpu(tp->counters.rx_broadcast);
  1234. data[10] = le32_to_cpu(tp->counters.rx_multicast);
  1235. data[11] = le16_to_cpu(tp->counters.tx_aborted);
  1236. data[12] = le16_to_cpu(tp->counters.tx_underun);
  1237. }
  1238. static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
  1239. {
  1240. switch(stringset) {
  1241. case ETH_SS_STATS:
  1242. memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
  1243. break;
  1244. }
  1245. }
  1246. static const struct ethtool_ops rtl8169_ethtool_ops = {
  1247. .get_drvinfo = rtl8169_get_drvinfo,
  1248. .get_regs_len = rtl8169_get_regs_len,
  1249. .get_link = ethtool_op_get_link,
  1250. .get_settings = rtl8169_get_settings,
  1251. .set_settings = rtl8169_set_settings,
  1252. .get_msglevel = rtl8169_get_msglevel,
  1253. .set_msglevel = rtl8169_set_msglevel,
  1254. .get_rx_csum = rtl8169_get_rx_csum,
  1255. .set_rx_csum = rtl8169_set_rx_csum,
  1256. .set_tx_csum = ethtool_op_set_tx_csum,
  1257. .set_sg = ethtool_op_set_sg,
  1258. .set_tso = ethtool_op_set_tso,
  1259. .get_regs = rtl8169_get_regs,
  1260. .get_wol = rtl8169_get_wol,
  1261. .set_wol = rtl8169_set_wol,
  1262. .get_strings = rtl8169_get_strings,
  1263. .get_sset_count = rtl8169_get_sset_count,
  1264. .get_ethtool_stats = rtl8169_get_ethtool_stats,
  1265. };
  1266. static void rtl8169_get_mac_version(struct rtl8169_private *tp,
  1267. void __iomem *ioaddr)
  1268. {
  1269. /*
  1270. * The driver currently handles the 8168Bf and the 8168Be identically
  1271. * but they can be identified more specifically through the test below
  1272. * if needed:
  1273. *
  1274. * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
  1275. *
  1276. * Same thing for the 8101Eb and the 8101Ec:
  1277. *
  1278. * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
  1279. */
  1280. static const struct {
  1281. u32 mask;
  1282. u32 val;
  1283. int mac_version;
  1284. } mac_info[] = {
  1285. /* 8168D family. */
  1286. { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
  1287. { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
  1288. { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
  1289. /* 8168DP family. */
  1290. { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
  1291. { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
  1292. /* 8168C family. */
  1293. { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
  1294. { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
  1295. { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
  1296. { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
  1297. { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
  1298. { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
  1299. { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
  1300. { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
  1301. { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
  1302. /* 8168B family. */
  1303. { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
  1304. { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
  1305. { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
  1306. { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
  1307. /* 8101 family. */
  1308. { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
  1309. { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
  1310. { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
  1311. { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
  1312. { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
  1313. { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
  1314. { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
  1315. { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
  1316. { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
  1317. { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
  1318. { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
  1319. { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
  1320. /* FIXME: where did these entries come from ? -- FR */
  1321. { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
  1322. { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
  1323. /* 8110 family. */
  1324. { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
  1325. { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
  1326. { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
  1327. { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
  1328. { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
  1329. { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
  1330. /* Catch-all */
  1331. { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
  1332. }, *p = mac_info;
  1333. u32 reg;
  1334. reg = RTL_R32(TxConfig);
  1335. while ((reg & p->mask) != p->val)
  1336. p++;
  1337. tp->mac_version = p->mac_version;
  1338. }
  1339. static void rtl8169_print_mac_version(struct rtl8169_private *tp)
  1340. {
  1341. dprintk("mac_version = 0x%02x\n", tp->mac_version);
  1342. }
  1343. struct phy_reg {
  1344. u16 reg;
  1345. u16 val;
  1346. };
  1347. static void rtl_writephy_batch(struct rtl8169_private *tp,
  1348. const struct phy_reg *regs, int len)
  1349. {
  1350. while (len-- > 0) {
  1351. rtl_writephy(tp, regs->reg, regs->val);
  1352. regs++;
  1353. }
  1354. }
  1355. #define PHY_READ 0x00000000
  1356. #define PHY_DATA_OR 0x10000000
  1357. #define PHY_DATA_AND 0x20000000
  1358. #define PHY_BJMPN 0x30000000
  1359. #define PHY_READ_EFUSE 0x40000000
  1360. #define PHY_READ_MAC_BYTE 0x50000000
  1361. #define PHY_WRITE_MAC_BYTE 0x60000000
  1362. #define PHY_CLEAR_READCOUNT 0x70000000
  1363. #define PHY_WRITE 0x80000000
  1364. #define PHY_READCOUNT_EQ_SKIP 0x90000000
  1365. #define PHY_COMP_EQ_SKIPN 0xa0000000
  1366. #define PHY_COMP_NEQ_SKIPN 0xb0000000
  1367. #define PHY_WRITE_PREVIOUS 0xc0000000
  1368. #define PHY_SKIPN 0xd0000000
  1369. #define PHY_DELAY_MS 0xe0000000
  1370. #define PHY_WRITE_ERI_WORD 0xf0000000
  1371. static void
  1372. rtl_phy_write_fw(struct rtl8169_private *tp, const struct firmware *fw)
  1373. {
  1374. __le32 *phytable = (__le32 *)fw->data;
  1375. struct net_device *dev = tp->dev;
  1376. size_t index, fw_size = fw->size / sizeof(*phytable);
  1377. u32 predata, count;
  1378. if (fw->size % sizeof(*phytable)) {
  1379. netif_err(tp, probe, dev, "odd sized firmware %zd\n", fw->size);
  1380. return;
  1381. }
  1382. for (index = 0; index < fw_size; index++) {
  1383. u32 action = le32_to_cpu(phytable[index]);
  1384. u32 regno = (action & 0x0fff0000) >> 16;
  1385. switch(action & 0xf0000000) {
  1386. case PHY_READ:
  1387. case PHY_DATA_OR:
  1388. case PHY_DATA_AND:
  1389. case PHY_READ_EFUSE:
  1390. case PHY_CLEAR_READCOUNT:
  1391. case PHY_WRITE:
  1392. case PHY_WRITE_PREVIOUS:
  1393. case PHY_DELAY_MS:
  1394. break;
  1395. case PHY_BJMPN:
  1396. if (regno > index) {
  1397. netif_err(tp, probe, tp->dev,
  1398. "Out of range of firmware\n");
  1399. return;
  1400. }
  1401. break;
  1402. case PHY_READCOUNT_EQ_SKIP:
  1403. if (index + 2 >= fw_size) {
  1404. netif_err(tp, probe, tp->dev,
  1405. "Out of range of firmware\n");
  1406. return;
  1407. }
  1408. break;
  1409. case PHY_COMP_EQ_SKIPN:
  1410. case PHY_COMP_NEQ_SKIPN:
  1411. case PHY_SKIPN:
  1412. if (index + 1 + regno >= fw_size) {
  1413. netif_err(tp, probe, tp->dev,
  1414. "Out of range of firmware\n");
  1415. return;
  1416. }
  1417. break;
  1418. case PHY_READ_MAC_BYTE:
  1419. case PHY_WRITE_MAC_BYTE:
  1420. case PHY_WRITE_ERI_WORD:
  1421. default:
  1422. netif_err(tp, probe, tp->dev,
  1423. "Invalid action 0x%08x\n", action);
  1424. return;
  1425. }
  1426. }
  1427. predata = 0;
  1428. count = 0;
  1429. for (index = 0; index < fw_size; ) {
  1430. u32 action = le32_to_cpu(phytable[index]);
  1431. u32 data = action & 0x0000ffff;
  1432. u32 regno = (action & 0x0fff0000) >> 16;
  1433. if (!action)
  1434. break;
  1435. switch(action & 0xf0000000) {
  1436. case PHY_READ:
  1437. predata = rtl_readphy(tp, regno);
  1438. count++;
  1439. index++;
  1440. break;
  1441. case PHY_DATA_OR:
  1442. predata |= data;
  1443. index++;
  1444. break;
  1445. case PHY_DATA_AND:
  1446. predata &= data;
  1447. index++;
  1448. break;
  1449. case PHY_BJMPN:
  1450. index -= regno;
  1451. break;
  1452. case PHY_READ_EFUSE:
  1453. predata = rtl8168d_efuse_read(tp->mmio_addr, regno);
  1454. index++;
  1455. break;
  1456. case PHY_CLEAR_READCOUNT:
  1457. count = 0;
  1458. index++;
  1459. break;
  1460. case PHY_WRITE:
  1461. rtl_writephy(tp, regno, data);
  1462. index++;
  1463. break;
  1464. case PHY_READCOUNT_EQ_SKIP:
  1465. if (count == data)
  1466. index += 2;
  1467. else
  1468. index += 1;
  1469. break;
  1470. case PHY_COMP_EQ_SKIPN:
  1471. if (predata == data)
  1472. index += regno;
  1473. index++;
  1474. break;
  1475. case PHY_COMP_NEQ_SKIPN:
  1476. if (predata != data)
  1477. index += regno;
  1478. index++;
  1479. break;
  1480. case PHY_WRITE_PREVIOUS:
  1481. rtl_writephy(tp, regno, predata);
  1482. index++;
  1483. break;
  1484. case PHY_SKIPN:
  1485. index += regno + 1;
  1486. break;
  1487. case PHY_DELAY_MS:
  1488. mdelay(data);
  1489. index++;
  1490. break;
  1491. case PHY_READ_MAC_BYTE:
  1492. case PHY_WRITE_MAC_BYTE:
  1493. case PHY_WRITE_ERI_WORD:
  1494. default:
  1495. BUG();
  1496. }
  1497. }
  1498. }
  1499. static void rtl_release_firmware(struct rtl8169_private *tp)
  1500. {
  1501. release_firmware(tp->fw);
  1502. tp->fw = NULL;
  1503. }
  1504. static int rtl_apply_firmware(struct rtl8169_private *tp, const char *fw_name)
  1505. {
  1506. const struct firmware **fw = &tp->fw;
  1507. int rc = !*fw;
  1508. if (rc) {
  1509. rc = request_firmware(fw, fw_name, &tp->pci_dev->dev);
  1510. if (rc < 0)
  1511. goto out;
  1512. }
  1513. /* TODO: release firmware once rtl_phy_write_fw signals failures. */
  1514. rtl_phy_write_fw(tp, *fw);
  1515. out:
  1516. return rc;
  1517. }
  1518. static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
  1519. {
  1520. static const struct phy_reg phy_reg_init[] = {
  1521. { 0x1f, 0x0001 },
  1522. { 0x06, 0x006e },
  1523. { 0x08, 0x0708 },
  1524. { 0x15, 0x4000 },
  1525. { 0x18, 0x65c7 },
  1526. { 0x1f, 0x0001 },
  1527. { 0x03, 0x00a1 },
  1528. { 0x02, 0x0008 },
  1529. { 0x01, 0x0120 },
  1530. { 0x00, 0x1000 },
  1531. { 0x04, 0x0800 },
  1532. { 0x04, 0x0000 },
  1533. { 0x03, 0xff41 },
  1534. { 0x02, 0xdf60 },
  1535. { 0x01, 0x0140 },
  1536. { 0x00, 0x0077 },
  1537. { 0x04, 0x7800 },
  1538. { 0x04, 0x7000 },
  1539. { 0x03, 0x802f },
  1540. { 0x02, 0x4f02 },
  1541. { 0x01, 0x0409 },
  1542. { 0x00, 0xf0f9 },
  1543. { 0x04, 0x9800 },
  1544. { 0x04, 0x9000 },
  1545. { 0x03, 0xdf01 },
  1546. { 0x02, 0xdf20 },
  1547. { 0x01, 0xff95 },
  1548. { 0x00, 0xba00 },
  1549. { 0x04, 0xa800 },
  1550. { 0x04, 0xa000 },
  1551. { 0x03, 0xff41 },
  1552. { 0x02, 0xdf20 },
  1553. { 0x01, 0x0140 },
  1554. { 0x00, 0x00bb },
  1555. { 0x04, 0xb800 },
  1556. { 0x04, 0xb000 },
  1557. { 0x03, 0xdf41 },
  1558. { 0x02, 0xdc60 },
  1559. { 0x01, 0x6340 },
  1560. { 0x00, 0x007d },
  1561. { 0x04, 0xd800 },
  1562. { 0x04, 0xd000 },
  1563. { 0x03, 0xdf01 },
  1564. { 0x02, 0xdf20 },
  1565. { 0x01, 0x100a },
  1566. { 0x00, 0xa0ff },
  1567. { 0x04, 0xf800 },
  1568. { 0x04, 0xf000 },
  1569. { 0x1f, 0x0000 },
  1570. { 0x0b, 0x0000 },
  1571. { 0x00, 0x9200 }
  1572. };
  1573. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1574. }
  1575. static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
  1576. {
  1577. static const struct phy_reg phy_reg_init[] = {
  1578. { 0x1f, 0x0002 },
  1579. { 0x01, 0x90d0 },
  1580. { 0x1f, 0x0000 }
  1581. };
  1582. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1583. }
  1584. static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
  1585. {
  1586. struct pci_dev *pdev = tp->pci_dev;
  1587. u16 vendor_id, device_id;
  1588. pci_read_config_word(pdev, PCI_SUBSYSTEM_VENDOR_ID, &vendor_id);
  1589. pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &device_id);
  1590. if ((vendor_id != PCI_VENDOR_ID_GIGABYTE) || (device_id != 0xe000))
  1591. return;
  1592. rtl_writephy(tp, 0x1f, 0x0001);
  1593. rtl_writephy(tp, 0x10, 0xf01b);
  1594. rtl_writephy(tp, 0x1f, 0x0000);
  1595. }
  1596. static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
  1597. {
  1598. static const struct phy_reg phy_reg_init[] = {
  1599. { 0x1f, 0x0001 },
  1600. { 0x04, 0x0000 },
  1601. { 0x03, 0x00a1 },
  1602. { 0x02, 0x0008 },
  1603. { 0x01, 0x0120 },
  1604. { 0x00, 0x1000 },
  1605. { 0x04, 0x0800 },
  1606. { 0x04, 0x9000 },
  1607. { 0x03, 0x802f },
  1608. { 0x02, 0x4f02 },
  1609. { 0x01, 0x0409 },
  1610. { 0x00, 0xf099 },
  1611. { 0x04, 0x9800 },
  1612. { 0x04, 0xa000 },
  1613. { 0x03, 0xdf01 },
  1614. { 0x02, 0xdf20 },
  1615. { 0x01, 0xff95 },
  1616. { 0x00, 0xba00 },
  1617. { 0x04, 0xa800 },
  1618. { 0x04, 0xf000 },
  1619. { 0x03, 0xdf01 },
  1620. { 0x02, 0xdf20 },
  1621. { 0x01, 0x101a },
  1622. { 0x00, 0xa0ff },
  1623. { 0x04, 0xf800 },
  1624. { 0x04, 0x0000 },
  1625. { 0x1f, 0x0000 },
  1626. { 0x1f, 0x0001 },
  1627. { 0x10, 0xf41b },
  1628. { 0x14, 0xfb54 },
  1629. { 0x18, 0xf5c7 },
  1630. { 0x1f, 0x0000 },
  1631. { 0x1f, 0x0001 },
  1632. { 0x17, 0x0cc0 },
  1633. { 0x1f, 0x0000 }
  1634. };
  1635. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1636. rtl8169scd_hw_phy_config_quirk(tp);
  1637. }
  1638. static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
  1639. {
  1640. static const struct phy_reg phy_reg_init[] = {
  1641. { 0x1f, 0x0001 },
  1642. { 0x04, 0x0000 },
  1643. { 0x03, 0x00a1 },
  1644. { 0x02, 0x0008 },
  1645. { 0x01, 0x0120 },
  1646. { 0x00, 0x1000 },
  1647. { 0x04, 0x0800 },
  1648. { 0x04, 0x9000 },
  1649. { 0x03, 0x802f },
  1650. { 0x02, 0x4f02 },
  1651. { 0x01, 0x0409 },
  1652. { 0x00, 0xf099 },
  1653. { 0x04, 0x9800 },
  1654. { 0x04, 0xa000 },
  1655. { 0x03, 0xdf01 },
  1656. { 0x02, 0xdf20 },
  1657. { 0x01, 0xff95 },
  1658. { 0x00, 0xba00 },
  1659. { 0x04, 0xa800 },
  1660. { 0x04, 0xf000 },
  1661. { 0x03, 0xdf01 },
  1662. { 0x02, 0xdf20 },
  1663. { 0x01, 0x101a },
  1664. { 0x00, 0xa0ff },
  1665. { 0x04, 0xf800 },
  1666. { 0x04, 0x0000 },
  1667. { 0x1f, 0x0000 },
  1668. { 0x1f, 0x0001 },
  1669. { 0x0b, 0x8480 },
  1670. { 0x1f, 0x0000 },
  1671. { 0x1f, 0x0001 },
  1672. { 0x18, 0x67c7 },
  1673. { 0x04, 0x2000 },
  1674. { 0x03, 0x002f },
  1675. { 0x02, 0x4360 },
  1676. { 0x01, 0x0109 },
  1677. { 0x00, 0x3022 },
  1678. { 0x04, 0x2800 },
  1679. { 0x1f, 0x0000 },
  1680. { 0x1f, 0x0001 },
  1681. { 0x17, 0x0cc0 },
  1682. { 0x1f, 0x0000 }
  1683. };
  1684. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1685. }
  1686. static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
  1687. {
  1688. static const struct phy_reg phy_reg_init[] = {
  1689. { 0x10, 0xf41b },
  1690. { 0x1f, 0x0000 }
  1691. };
  1692. rtl_writephy(tp, 0x1f, 0x0001);
  1693. rtl_patchphy(tp, 0x16, 1 << 0);
  1694. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1695. }
  1696. static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
  1697. {
  1698. static const struct phy_reg phy_reg_init[] = {
  1699. { 0x1f, 0x0001 },
  1700. { 0x10, 0xf41b },
  1701. { 0x1f, 0x0000 }
  1702. };
  1703. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1704. }
  1705. static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
  1706. {
  1707. static const struct phy_reg phy_reg_init[] = {
  1708. { 0x1f, 0x0000 },
  1709. { 0x1d, 0x0f00 },
  1710. { 0x1f, 0x0002 },
  1711. { 0x0c, 0x1ec8 },
  1712. { 0x1f, 0x0000 }
  1713. };
  1714. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1715. }
  1716. static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
  1717. {
  1718. static const struct phy_reg phy_reg_init[] = {
  1719. { 0x1f, 0x0001 },
  1720. { 0x1d, 0x3d98 },
  1721. { 0x1f, 0x0000 }
  1722. };
  1723. rtl_writephy(tp, 0x1f, 0x0000);
  1724. rtl_patchphy(tp, 0x14, 1 << 5);
  1725. rtl_patchphy(tp, 0x0d, 1 << 5);
  1726. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1727. }
  1728. static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
  1729. {
  1730. static const struct phy_reg phy_reg_init[] = {
  1731. { 0x1f, 0x0001 },
  1732. { 0x12, 0x2300 },
  1733. { 0x1f, 0x0002 },
  1734. { 0x00, 0x88d4 },
  1735. { 0x01, 0x82b1 },
  1736. { 0x03, 0x7002 },
  1737. { 0x08, 0x9e30 },
  1738. { 0x09, 0x01f0 },
  1739. { 0x0a, 0x5500 },
  1740. { 0x0c, 0x00c8 },
  1741. { 0x1f, 0x0003 },
  1742. { 0x12, 0xc096 },
  1743. { 0x16, 0x000a },
  1744. { 0x1f, 0x0000 },
  1745. { 0x1f, 0x0000 },
  1746. { 0x09, 0x2000 },
  1747. { 0x09, 0x0000 }
  1748. };
  1749. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1750. rtl_patchphy(tp, 0x14, 1 << 5);
  1751. rtl_patchphy(tp, 0x0d, 1 << 5);
  1752. rtl_writephy(tp, 0x1f, 0x0000);
  1753. }
  1754. static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
  1755. {
  1756. static const struct phy_reg phy_reg_init[] = {
  1757. { 0x1f, 0x0001 },
  1758. { 0x12, 0x2300 },
  1759. { 0x03, 0x802f },
  1760. { 0x02, 0x4f02 },
  1761. { 0x01, 0x0409 },
  1762. { 0x00, 0xf099 },
  1763. { 0x04, 0x9800 },
  1764. { 0x04, 0x9000 },
  1765. { 0x1d, 0x3d98 },
  1766. { 0x1f, 0x0002 },
  1767. { 0x0c, 0x7eb8 },
  1768. { 0x06, 0x0761 },
  1769. { 0x1f, 0x0003 },
  1770. { 0x16, 0x0f0a },
  1771. { 0x1f, 0x0000 }
  1772. };
  1773. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1774. rtl_patchphy(tp, 0x16, 1 << 0);
  1775. rtl_patchphy(tp, 0x14, 1 << 5);
  1776. rtl_patchphy(tp, 0x0d, 1 << 5);
  1777. rtl_writephy(tp, 0x1f, 0x0000);
  1778. }
  1779. static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
  1780. {
  1781. static const struct phy_reg phy_reg_init[] = {
  1782. { 0x1f, 0x0001 },
  1783. { 0x12, 0x2300 },
  1784. { 0x1d, 0x3d98 },
  1785. { 0x1f, 0x0002 },
  1786. { 0x0c, 0x7eb8 },
  1787. { 0x06, 0x5461 },
  1788. { 0x1f, 0x0003 },
  1789. { 0x16, 0x0f0a },
  1790. { 0x1f, 0x0000 }
  1791. };
  1792. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1793. rtl_patchphy(tp, 0x16, 1 << 0);
  1794. rtl_patchphy(tp, 0x14, 1 << 5);
  1795. rtl_patchphy(tp, 0x0d, 1 << 5);
  1796. rtl_writephy(tp, 0x1f, 0x0000);
  1797. }
  1798. static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
  1799. {
  1800. rtl8168c_3_hw_phy_config(tp);
  1801. }
  1802. static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
  1803. {
  1804. static const struct phy_reg phy_reg_init_0[] = {
  1805. /* Channel Estimation */
  1806. { 0x1f, 0x0001 },
  1807. { 0x06, 0x4064 },
  1808. { 0x07, 0x2863 },
  1809. { 0x08, 0x059c },
  1810. { 0x09, 0x26b4 },
  1811. { 0x0a, 0x6a19 },
  1812. { 0x0b, 0xdcc8 },
  1813. { 0x10, 0xf06d },
  1814. { 0x14, 0x7f68 },
  1815. { 0x18, 0x7fd9 },
  1816. { 0x1c, 0xf0ff },
  1817. { 0x1d, 0x3d9c },
  1818. { 0x1f, 0x0003 },
  1819. { 0x12, 0xf49f },
  1820. { 0x13, 0x070b },
  1821. { 0x1a, 0x05ad },
  1822. { 0x14, 0x94c0 },
  1823. /*
  1824. * Tx Error Issue
  1825. * enhance line driver power
  1826. */
  1827. { 0x1f, 0x0002 },
  1828. { 0x06, 0x5561 },
  1829. { 0x1f, 0x0005 },
  1830. { 0x05, 0x8332 },
  1831. { 0x06, 0x5561 },
  1832. /*
  1833. * Can not link to 1Gbps with bad cable
  1834. * Decrease SNR threshold form 21.07dB to 19.04dB
  1835. */
  1836. { 0x1f, 0x0001 },
  1837. { 0x17, 0x0cc0 },
  1838. { 0x1f, 0x0000 },
  1839. { 0x0d, 0xf880 }
  1840. };
  1841. void __iomem *ioaddr = tp->mmio_addr;
  1842. rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
  1843. /*
  1844. * Rx Error Issue
  1845. * Fine Tune Switching regulator parameter
  1846. */
  1847. rtl_writephy(tp, 0x1f, 0x0002);
  1848. rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
  1849. rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
  1850. if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
  1851. static const struct phy_reg phy_reg_init[] = {
  1852. { 0x1f, 0x0002 },
  1853. { 0x05, 0x669a },
  1854. { 0x1f, 0x0005 },
  1855. { 0x05, 0x8330 },
  1856. { 0x06, 0x669a },
  1857. { 0x1f, 0x0002 }
  1858. };
  1859. int val;
  1860. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1861. val = rtl_readphy(tp, 0x0d);
  1862. if ((val & 0x00ff) != 0x006c) {
  1863. static const u32 set[] = {
  1864. 0x0065, 0x0066, 0x0067, 0x0068,
  1865. 0x0069, 0x006a, 0x006b, 0x006c
  1866. };
  1867. int i;
  1868. rtl_writephy(tp, 0x1f, 0x0002);
  1869. val &= 0xff00;
  1870. for (i = 0; i < ARRAY_SIZE(set); i++)
  1871. rtl_writephy(tp, 0x0d, val | set[i]);
  1872. }
  1873. } else {
  1874. static const struct phy_reg phy_reg_init[] = {
  1875. { 0x1f, 0x0002 },
  1876. { 0x05, 0x6662 },
  1877. { 0x1f, 0x0005 },
  1878. { 0x05, 0x8330 },
  1879. { 0x06, 0x6662 }
  1880. };
  1881. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1882. }
  1883. /* RSET couple improve */
  1884. rtl_writephy(tp, 0x1f, 0x0002);
  1885. rtl_patchphy(tp, 0x0d, 0x0300);
  1886. rtl_patchphy(tp, 0x0f, 0x0010);
  1887. /* Fine tune PLL performance */
  1888. rtl_writephy(tp, 0x1f, 0x0002);
  1889. rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
  1890. rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
  1891. rtl_writephy(tp, 0x1f, 0x0005);
  1892. rtl_writephy(tp, 0x05, 0x001b);
  1893. if ((rtl_readphy(tp, 0x06) != 0xbf00) ||
  1894. (rtl_apply_firmware(tp, FIRMWARE_8168D_1) < 0)) {
  1895. netif_warn(tp, probe, tp->dev, "unable to apply firmware patch\n");
  1896. }
  1897. rtl_writephy(tp, 0x1f, 0x0000);
  1898. }
  1899. static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
  1900. {
  1901. static const struct phy_reg phy_reg_init_0[] = {
  1902. /* Channel Estimation */
  1903. { 0x1f, 0x0001 },
  1904. { 0x06, 0x4064 },
  1905. { 0x07, 0x2863 },
  1906. { 0x08, 0x059c },
  1907. { 0x09, 0x26b4 },
  1908. { 0x0a, 0x6a19 },
  1909. { 0x0b, 0xdcc8 },
  1910. { 0x10, 0xf06d },
  1911. { 0x14, 0x7f68 },
  1912. { 0x18, 0x7fd9 },
  1913. { 0x1c, 0xf0ff },
  1914. { 0x1d, 0x3d9c },
  1915. { 0x1f, 0x0003 },
  1916. { 0x12, 0xf49f },
  1917. { 0x13, 0x070b },
  1918. { 0x1a, 0x05ad },
  1919. { 0x14, 0x94c0 },
  1920. /*
  1921. * Tx Error Issue
  1922. * enhance line driver power
  1923. */
  1924. { 0x1f, 0x0002 },
  1925. { 0x06, 0x5561 },
  1926. { 0x1f, 0x0005 },
  1927. { 0x05, 0x8332 },
  1928. { 0x06, 0x5561 },
  1929. /*
  1930. * Can not link to 1Gbps with bad cable
  1931. * Decrease SNR threshold form 21.07dB to 19.04dB
  1932. */
  1933. { 0x1f, 0x0001 },
  1934. { 0x17, 0x0cc0 },
  1935. { 0x1f, 0x0000 },
  1936. { 0x0d, 0xf880 }
  1937. };
  1938. void __iomem *ioaddr = tp->mmio_addr;
  1939. rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
  1940. if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
  1941. static const struct phy_reg phy_reg_init[] = {
  1942. { 0x1f, 0x0002 },
  1943. { 0x05, 0x669a },
  1944. { 0x1f, 0x0005 },
  1945. { 0x05, 0x8330 },
  1946. { 0x06, 0x669a },
  1947. { 0x1f, 0x0002 }
  1948. };
  1949. int val;
  1950. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1951. val = rtl_readphy(tp, 0x0d);
  1952. if ((val & 0x00ff) != 0x006c) {
  1953. static const u32 set[] = {
  1954. 0x0065, 0x0066, 0x0067, 0x0068,
  1955. 0x0069, 0x006a, 0x006b, 0x006c
  1956. };
  1957. int i;
  1958. rtl_writephy(tp, 0x1f, 0x0002);
  1959. val &= 0xff00;
  1960. for (i = 0; i < ARRAY_SIZE(set); i++)
  1961. rtl_writephy(tp, 0x0d, val | set[i]);
  1962. }
  1963. } else {
  1964. static const struct phy_reg phy_reg_init[] = {
  1965. { 0x1f, 0x0002 },
  1966. { 0x05, 0x2642 },
  1967. { 0x1f, 0x0005 },
  1968. { 0x05, 0x8330 },
  1969. { 0x06, 0x2642 }
  1970. };
  1971. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  1972. }
  1973. /* Fine tune PLL performance */
  1974. rtl_writephy(tp, 0x1f, 0x0002);
  1975. rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
  1976. rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
  1977. /* Switching regulator Slew rate */
  1978. rtl_writephy(tp, 0x1f, 0x0002);
  1979. rtl_patchphy(tp, 0x0f, 0x0017);
  1980. rtl_writephy(tp, 0x1f, 0x0005);
  1981. rtl_writephy(tp, 0x05, 0x001b);
  1982. if ((rtl_readphy(tp, 0x06) != 0xb300) ||
  1983. (rtl_apply_firmware(tp, FIRMWARE_8168D_2) < 0)) {
  1984. netif_warn(tp, probe, tp->dev, "unable to apply firmware patch\n");
  1985. }
  1986. rtl_writephy(tp, 0x1f, 0x0000);
  1987. }
  1988. static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
  1989. {
  1990. static const struct phy_reg phy_reg_init[] = {
  1991. { 0x1f, 0x0002 },
  1992. { 0x10, 0x0008 },
  1993. { 0x0d, 0x006c },
  1994. { 0x1f, 0x0000 },
  1995. { 0x0d, 0xf880 },
  1996. { 0x1f, 0x0001 },
  1997. { 0x17, 0x0cc0 },
  1998. { 0x1f, 0x0001 },
  1999. { 0x0b, 0xa4d8 },
  2000. { 0x09, 0x281c },
  2001. { 0x07, 0x2883 },
  2002. { 0x0a, 0x6b35 },
  2003. { 0x1d, 0x3da4 },
  2004. { 0x1c, 0xeffd },
  2005. { 0x14, 0x7f52 },
  2006. { 0x18, 0x7fc6 },
  2007. { 0x08, 0x0601 },
  2008. { 0x06, 0x4063 },
  2009. { 0x10, 0xf074 },
  2010. { 0x1f, 0x0003 },
  2011. { 0x13, 0x0789 },
  2012. { 0x12, 0xf4bd },
  2013. { 0x1a, 0x04fd },
  2014. { 0x14, 0x84b0 },
  2015. { 0x1f, 0x0000 },
  2016. { 0x00, 0x9200 },
  2017. { 0x1f, 0x0005 },
  2018. { 0x01, 0x0340 },
  2019. { 0x1f, 0x0001 },
  2020. { 0x04, 0x4000 },
  2021. { 0x03, 0x1d21 },
  2022. { 0x02, 0x0c32 },
  2023. { 0x01, 0x0200 },
  2024. { 0x00, 0x5554 },
  2025. { 0x04, 0x4800 },
  2026. { 0x04, 0x4000 },
  2027. { 0x04, 0xf000 },
  2028. { 0x03, 0xdf01 },
  2029. { 0x02, 0xdf20 },
  2030. { 0x01, 0x101a },
  2031. { 0x00, 0xa0ff },
  2032. { 0x04, 0xf800 },
  2033. { 0x04, 0xf000 },
  2034. { 0x1f, 0x0000 },
  2035. { 0x1f, 0x0007 },
  2036. { 0x1e, 0x0023 },
  2037. { 0x16, 0x0000 },
  2038. { 0x1f, 0x0000 }
  2039. };
  2040. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2041. }
  2042. static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
  2043. {
  2044. static const struct phy_reg phy_reg_init[] = {
  2045. { 0x1f, 0x0001 },
  2046. { 0x17, 0x0cc0 },
  2047. { 0x1f, 0x0007 },
  2048. { 0x1e, 0x002d },
  2049. { 0x18, 0x0040 },
  2050. { 0x1f, 0x0000 }
  2051. };
  2052. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2053. rtl_patchphy(tp, 0x0d, 1 << 5);
  2054. }
  2055. static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
  2056. {
  2057. static const struct phy_reg phy_reg_init[] = {
  2058. { 0x1f, 0x0003 },
  2059. { 0x08, 0x441d },
  2060. { 0x01, 0x9100 },
  2061. { 0x1f, 0x0000 }
  2062. };
  2063. rtl_writephy(tp, 0x1f, 0x0000);
  2064. rtl_patchphy(tp, 0x11, 1 << 12);
  2065. rtl_patchphy(tp, 0x19, 1 << 13);
  2066. rtl_patchphy(tp, 0x10, 1 << 15);
  2067. rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
  2068. }
  2069. static void rtl_hw_phy_config(struct net_device *dev)
  2070. {
  2071. struct rtl8169_private *tp = netdev_priv(dev);
  2072. rtl8169_print_mac_version(tp);
  2073. switch (tp->mac_version) {
  2074. case RTL_GIGA_MAC_VER_01:
  2075. break;
  2076. case RTL_GIGA_MAC_VER_02:
  2077. case RTL_GIGA_MAC_VER_03:
  2078. rtl8169s_hw_phy_config(tp);
  2079. break;
  2080. case RTL_GIGA_MAC_VER_04:
  2081. rtl8169sb_hw_phy_config(tp);
  2082. break;
  2083. case RTL_GIGA_MAC_VER_05:
  2084. rtl8169scd_hw_phy_config(tp);
  2085. break;
  2086. case RTL_GIGA_MAC_VER_06:
  2087. rtl8169sce_hw_phy_config(tp);
  2088. break;
  2089. case RTL_GIGA_MAC_VER_07:
  2090. case RTL_GIGA_MAC_VER_08:
  2091. case RTL_GIGA_MAC_VER_09:
  2092. rtl8102e_hw_phy_config(tp);
  2093. break;
  2094. case RTL_GIGA_MAC_VER_11:
  2095. rtl8168bb_hw_phy_config(tp);
  2096. break;
  2097. case RTL_GIGA_MAC_VER_12:
  2098. rtl8168bef_hw_phy_config(tp);
  2099. break;
  2100. case RTL_GIGA_MAC_VER_17:
  2101. rtl8168bef_hw_phy_config(tp);
  2102. break;
  2103. case RTL_GIGA_MAC_VER_18:
  2104. rtl8168cp_1_hw_phy_config(tp);
  2105. break;
  2106. case RTL_GIGA_MAC_VER_19:
  2107. rtl8168c_1_hw_phy_config(tp);
  2108. break;
  2109. case RTL_GIGA_MAC_VER_20:
  2110. rtl8168c_2_hw_phy_config(tp);
  2111. break;
  2112. case RTL_GIGA_MAC_VER_21:
  2113. rtl8168c_3_hw_phy_config(tp);
  2114. break;
  2115. case RTL_GIGA_MAC_VER_22:
  2116. rtl8168c_4_hw_phy_config(tp);
  2117. break;
  2118. case RTL_GIGA_MAC_VER_23:
  2119. case RTL_GIGA_MAC_VER_24:
  2120. rtl8168cp_2_hw_phy_config(tp);
  2121. break;
  2122. case RTL_GIGA_MAC_VER_25:
  2123. rtl8168d_1_hw_phy_config(tp);
  2124. break;
  2125. case RTL_GIGA_MAC_VER_26:
  2126. rtl8168d_2_hw_phy_config(tp);
  2127. break;
  2128. case RTL_GIGA_MAC_VER_27:
  2129. rtl8168d_3_hw_phy_config(tp);
  2130. break;
  2131. case RTL_GIGA_MAC_VER_28:
  2132. rtl8168d_4_hw_phy_config(tp);
  2133. break;
  2134. default:
  2135. break;
  2136. }
  2137. }
  2138. static void rtl8169_phy_timer(unsigned long __opaque)
  2139. {
  2140. struct net_device *dev = (struct net_device *)__opaque;
  2141. struct rtl8169_private *tp = netdev_priv(dev);
  2142. struct timer_list *timer = &tp->timer;
  2143. void __iomem *ioaddr = tp->mmio_addr;
  2144. unsigned long timeout = RTL8169_PHY_TIMEOUT;
  2145. assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
  2146. if (!(tp->phy_1000_ctrl_reg & ADVERTISE_1000FULL))
  2147. return;
  2148. spin_lock_irq(&tp->lock);
  2149. if (tp->phy_reset_pending(tp)) {
  2150. /*
  2151. * A busy loop could burn quite a few cycles on nowadays CPU.
  2152. * Let's delay the execution of the timer for a few ticks.
  2153. */
  2154. timeout = HZ/10;
  2155. goto out_mod_timer;
  2156. }
  2157. if (tp->link_ok(ioaddr))
  2158. goto out_unlock;
  2159. netif_warn(tp, link, dev, "PHY reset until link up\n");
  2160. tp->phy_reset_enable(tp);
  2161. out_mod_timer:
  2162. mod_timer(timer, jiffies + timeout);
  2163. out_unlock:
  2164. spin_unlock_irq(&tp->lock);
  2165. }
  2166. static inline void rtl8169_delete_timer(struct net_device *dev)
  2167. {
  2168. struct rtl8169_private *tp = netdev_priv(dev);
  2169. struct timer_list *timer = &tp->timer;
  2170. if (tp->mac_version <= RTL_GIGA_MAC_VER_01)
  2171. return;
  2172. del_timer_sync(timer);
  2173. }
  2174. static inline void rtl8169_request_timer(struct net_device *dev)
  2175. {
  2176. struct rtl8169_private *tp = netdev_priv(dev);
  2177. struct timer_list *timer = &tp->timer;
  2178. if (tp->mac_version <= RTL_GIGA_MAC_VER_01)
  2179. return;
  2180. mod_timer(timer, jiffies + RTL8169_PHY_TIMEOUT);
  2181. }
  2182. #ifdef CONFIG_NET_POLL_CONTROLLER
  2183. /*
  2184. * Polling 'interrupt' - used by things like netconsole to send skbs
  2185. * without having to re-enable interrupts. It's not called while
  2186. * the interrupt routine is executing.
  2187. */
  2188. static void rtl8169_netpoll(struct net_device *dev)
  2189. {
  2190. struct rtl8169_private *tp = netdev_priv(dev);
  2191. struct pci_dev *pdev = tp->pci_dev;
  2192. disable_irq(pdev->irq);
  2193. rtl8169_interrupt(pdev->irq, dev);
  2194. enable_irq(pdev->irq);
  2195. }
  2196. #endif
  2197. static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
  2198. void __iomem *ioaddr)
  2199. {
  2200. iounmap(ioaddr);
  2201. pci_release_regions(pdev);
  2202. pci_clear_mwi(pdev);
  2203. pci_disable_device(pdev);
  2204. free_netdev(dev);
  2205. }
  2206. static void rtl8169_phy_reset(struct net_device *dev,
  2207. struct rtl8169_private *tp)
  2208. {
  2209. unsigned int i;
  2210. tp->phy_reset_enable(tp);
  2211. for (i = 0; i < 100; i++) {
  2212. if (!tp->phy_reset_pending(tp))
  2213. return;
  2214. msleep(1);
  2215. }
  2216. netif_err(tp, link, dev, "PHY reset failed\n");
  2217. }
  2218. static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
  2219. {
  2220. void __iomem *ioaddr = tp->mmio_addr;
  2221. rtl_hw_phy_config(dev);
  2222. if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
  2223. dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
  2224. RTL_W8(0x82, 0x01);
  2225. }
  2226. pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
  2227. if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
  2228. pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
  2229. if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
  2230. dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
  2231. RTL_W8(0x82, 0x01);
  2232. dprintk("Set PHY Reg 0x0bh = 0x00h\n");
  2233. rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
  2234. }
  2235. rtl8169_phy_reset(dev, tp);
  2236. /*
  2237. * rtl8169_set_speed_xmii takes good care of the Fast Ethernet
  2238. * only 8101. Don't panic.
  2239. */
  2240. rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL);
  2241. if (RTL_R8(PHYstatus) & TBI_Enable)
  2242. netif_info(tp, link, dev, "TBI auto-negotiating\n");
  2243. }
  2244. static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
  2245. {
  2246. void __iomem *ioaddr = tp->mmio_addr;
  2247. u32 high;
  2248. u32 low;
  2249. low = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
  2250. high = addr[4] | (addr[5] << 8);
  2251. spin_lock_irq(&tp->lock);
  2252. RTL_W8(Cfg9346, Cfg9346_Unlock);
  2253. RTL_W32(MAC4, high);
  2254. RTL_R32(MAC4);
  2255. RTL_W32(MAC0, low);
  2256. RTL_R32(MAC0);
  2257. RTL_W8(Cfg9346, Cfg9346_Lock);
  2258. spin_unlock_irq(&tp->lock);
  2259. }
  2260. static int rtl_set_mac_address(struct net_device *dev, void *p)
  2261. {
  2262. struct rtl8169_private *tp = netdev_priv(dev);
  2263. struct sockaddr *addr = p;
  2264. if (!is_valid_ether_addr(addr->sa_data))
  2265. return -EADDRNOTAVAIL;
  2266. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  2267. rtl_rar_set(tp, dev->dev_addr);
  2268. return 0;
  2269. }
  2270. static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  2271. {
  2272. struct rtl8169_private *tp = netdev_priv(dev);
  2273. struct mii_ioctl_data *data = if_mii(ifr);
  2274. return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
  2275. }
  2276. static int rtl_xmii_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
  2277. {
  2278. switch (cmd) {
  2279. case SIOCGMIIPHY:
  2280. data->phy_id = 32; /* Internal PHY */
  2281. return 0;
  2282. case SIOCGMIIREG:
  2283. data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
  2284. return 0;
  2285. case SIOCSMIIREG:
  2286. rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
  2287. return 0;
  2288. }
  2289. return -EOPNOTSUPP;
  2290. }
  2291. static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
  2292. {
  2293. return -EOPNOTSUPP;
  2294. }
  2295. static const struct rtl_cfg_info {
  2296. void (*hw_start)(struct net_device *);
  2297. unsigned int region;
  2298. unsigned int align;
  2299. u16 intr_event;
  2300. u16 napi_event;
  2301. unsigned features;
  2302. u8 default_ver;
  2303. } rtl_cfg_infos [] = {
  2304. [RTL_CFG_0] = {
  2305. .hw_start = rtl_hw_start_8169,
  2306. .region = 1,
  2307. .align = 0,
  2308. .intr_event = SYSErr | LinkChg | RxOverflow |
  2309. RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
  2310. .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
  2311. .features = RTL_FEATURE_GMII,
  2312. .default_ver = RTL_GIGA_MAC_VER_01,
  2313. },
  2314. [RTL_CFG_1] = {
  2315. .hw_start = rtl_hw_start_8168,
  2316. .region = 2,
  2317. .align = 8,
  2318. .intr_event = SYSErr | LinkChg | RxOverflow |
  2319. TxErr | TxOK | RxOK | RxErr,
  2320. .napi_event = TxErr | TxOK | RxOK | RxOverflow,
  2321. .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
  2322. .default_ver = RTL_GIGA_MAC_VER_11,
  2323. },
  2324. [RTL_CFG_2] = {
  2325. .hw_start = rtl_hw_start_8101,
  2326. .region = 2,
  2327. .align = 8,
  2328. .intr_event = SYSErr | LinkChg | RxOverflow | PCSTimeout |
  2329. RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
  2330. .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
  2331. .features = RTL_FEATURE_MSI,
  2332. .default_ver = RTL_GIGA_MAC_VER_13,
  2333. }
  2334. };
  2335. /* Cfg9346_Unlock assumed. */
  2336. static unsigned rtl_try_msi(struct pci_dev *pdev, void __iomem *ioaddr,
  2337. const struct rtl_cfg_info *cfg)
  2338. {
  2339. unsigned msi = 0;
  2340. u8 cfg2;
  2341. cfg2 = RTL_R8(Config2) & ~MSIEnable;
  2342. if (cfg->features & RTL_FEATURE_MSI) {
  2343. if (pci_enable_msi(pdev)) {
  2344. dev_info(&pdev->dev, "no MSI. Back to INTx.\n");
  2345. } else {
  2346. cfg2 |= MSIEnable;
  2347. msi = RTL_FEATURE_MSI;
  2348. }
  2349. }
  2350. RTL_W8(Config2, cfg2);
  2351. return msi;
  2352. }
  2353. static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
  2354. {
  2355. if (tp->features & RTL_FEATURE_MSI) {
  2356. pci_disable_msi(pdev);
  2357. tp->features &= ~RTL_FEATURE_MSI;
  2358. }
  2359. }
  2360. static const struct net_device_ops rtl8169_netdev_ops = {
  2361. .ndo_open = rtl8169_open,
  2362. .ndo_stop = rtl8169_close,
  2363. .ndo_get_stats = rtl8169_get_stats,
  2364. .ndo_start_xmit = rtl8169_start_xmit,
  2365. .ndo_tx_timeout = rtl8169_tx_timeout,
  2366. .ndo_validate_addr = eth_validate_addr,
  2367. .ndo_change_mtu = rtl8169_change_mtu,
  2368. .ndo_set_mac_address = rtl_set_mac_address,
  2369. .ndo_do_ioctl = rtl8169_ioctl,
  2370. .ndo_set_multicast_list = rtl_set_rx_mode,
  2371. #ifdef CONFIG_R8169_VLAN
  2372. .ndo_vlan_rx_register = rtl8169_vlan_rx_register,
  2373. #endif
  2374. #ifdef CONFIG_NET_POLL_CONTROLLER
  2375. .ndo_poll_controller = rtl8169_netpoll,
  2376. #endif
  2377. };
  2378. static void __devinit rtl_init_mdio_ops(struct rtl8169_private *tp)
  2379. {
  2380. struct mdio_ops *ops = &tp->mdio_ops;
  2381. switch (tp->mac_version) {
  2382. case RTL_GIGA_MAC_VER_27:
  2383. ops->write = r8168dp_1_mdio_write;
  2384. ops->read = r8168dp_1_mdio_read;
  2385. break;
  2386. case RTL_GIGA_MAC_VER_28:
  2387. ops->write = r8168dp_2_mdio_write;
  2388. ops->read = r8168dp_2_mdio_read;
  2389. break;
  2390. default:
  2391. ops->write = r8169_mdio_write;
  2392. ops->read = r8169_mdio_read;
  2393. break;
  2394. }
  2395. }
  2396. static void r810x_phy_power_down(struct rtl8169_private *tp)
  2397. {
  2398. rtl_writephy(tp, 0x1f, 0x0000);
  2399. rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
  2400. }
  2401. static void r810x_phy_power_up(struct rtl8169_private *tp)
  2402. {
  2403. rtl_writephy(tp, 0x1f, 0x0000);
  2404. rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
  2405. }
  2406. static void r810x_pll_power_down(struct rtl8169_private *tp)
  2407. {
  2408. if (__rtl8169_get_wol(tp) & WAKE_ANY) {
  2409. rtl_writephy(tp, 0x1f, 0x0000);
  2410. rtl_writephy(tp, MII_BMCR, 0x0000);
  2411. return;
  2412. }
  2413. r810x_phy_power_down(tp);
  2414. }
  2415. static void r810x_pll_power_up(struct rtl8169_private *tp)
  2416. {
  2417. r810x_phy_power_up(tp);
  2418. }
  2419. static void r8168_phy_power_up(struct rtl8169_private *tp)
  2420. {
  2421. rtl_writephy(tp, 0x1f, 0x0000);
  2422. rtl_writephy(tp, 0x0e, 0x0000);
  2423. rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
  2424. }
  2425. static void r8168_phy_power_down(struct rtl8169_private *tp)
  2426. {
  2427. rtl_writephy(tp, 0x1f, 0x0000);
  2428. rtl_writephy(tp, 0x0e, 0x0200);
  2429. rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
  2430. }
  2431. static void r8168_pll_power_down(struct rtl8169_private *tp)
  2432. {
  2433. void __iomem *ioaddr = tp->mmio_addr;
  2434. if (tp->mac_version == RTL_GIGA_MAC_VER_27)
  2435. return;
  2436. if (((tp->mac_version == RTL_GIGA_MAC_VER_23) ||
  2437. (tp->mac_version == RTL_GIGA_MAC_VER_24)) &&
  2438. (RTL_R16(CPlusCmd) & ASF)) {
  2439. return;
  2440. }
  2441. if (__rtl8169_get_wol(tp) & WAKE_ANY) {
  2442. rtl_writephy(tp, 0x1f, 0x0000);
  2443. rtl_writephy(tp, MII_BMCR, 0x0000);
  2444. RTL_W32(RxConfig, RTL_R32(RxConfig) |
  2445. AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
  2446. return;
  2447. }
  2448. r8168_phy_power_down(tp);
  2449. switch (tp->mac_version) {
  2450. case RTL_GIGA_MAC_VER_25:
  2451. case RTL_GIGA_MAC_VER_26:
  2452. RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
  2453. break;
  2454. }
  2455. }
  2456. static void r8168_pll_power_up(struct rtl8169_private *tp)
  2457. {
  2458. void __iomem *ioaddr = tp->mmio_addr;
  2459. if (tp->mac_version == RTL_GIGA_MAC_VER_27)
  2460. return;
  2461. switch (tp->mac_version) {
  2462. case RTL_GIGA_MAC_VER_25:
  2463. case RTL_GIGA_MAC_VER_26:
  2464. RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
  2465. break;
  2466. }
  2467. r8168_phy_power_up(tp);
  2468. }
  2469. static void rtl_pll_power_op(struct rtl8169_private *tp,
  2470. void (*op)(struct rtl8169_private *))
  2471. {
  2472. if (op)
  2473. op(tp);
  2474. }
  2475. static void rtl_pll_power_down(struct rtl8169_private *tp)
  2476. {
  2477. rtl_pll_power_op(tp, tp->pll_power_ops.down);
  2478. }
  2479. static void rtl_pll_power_up(struct rtl8169_private *tp)
  2480. {
  2481. rtl_pll_power_op(tp, tp->pll_power_ops.up);
  2482. }
  2483. static void __devinit rtl_init_pll_power_ops(struct rtl8169_private *tp)
  2484. {
  2485. struct pll_power_ops *ops = &tp->pll_power_ops;
  2486. switch (tp->mac_version) {
  2487. case RTL_GIGA_MAC_VER_07:
  2488. case RTL_GIGA_MAC_VER_08:
  2489. case RTL_GIGA_MAC_VER_09:
  2490. case RTL_GIGA_MAC_VER_10:
  2491. case RTL_GIGA_MAC_VER_16:
  2492. ops->down = r810x_pll_power_down;
  2493. ops->up = r810x_pll_power_up;
  2494. break;
  2495. case RTL_GIGA_MAC_VER_11:
  2496. case RTL_GIGA_MAC_VER_12:
  2497. case RTL_GIGA_MAC_VER_17:
  2498. case RTL_GIGA_MAC_VER_18:
  2499. case RTL_GIGA_MAC_VER_19:
  2500. case RTL_GIGA_MAC_VER_20:
  2501. case RTL_GIGA_MAC_VER_21:
  2502. case RTL_GIGA_MAC_VER_22:
  2503. case RTL_GIGA_MAC_VER_23:
  2504. case RTL_GIGA_MAC_VER_24:
  2505. case RTL_GIGA_MAC_VER_25:
  2506. case RTL_GIGA_MAC_VER_26:
  2507. case RTL_GIGA_MAC_VER_27:
  2508. case RTL_GIGA_MAC_VER_28:
  2509. ops->down = r8168_pll_power_down;
  2510. ops->up = r8168_pll_power_up;
  2511. break;
  2512. default:
  2513. ops->down = NULL;
  2514. ops->up = NULL;
  2515. break;
  2516. }
  2517. }
  2518. static int __devinit
  2519. rtl8169_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  2520. {
  2521. const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
  2522. const unsigned int region = cfg->region;
  2523. struct rtl8169_private *tp;
  2524. struct mii_if_info *mii;
  2525. struct net_device *dev;
  2526. void __iomem *ioaddr;
  2527. unsigned int i;
  2528. int rc;
  2529. if (netif_msg_drv(&debug)) {
  2530. printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
  2531. MODULENAME, RTL8169_VERSION);
  2532. }
  2533. dev = alloc_etherdev(sizeof (*tp));
  2534. if (!dev) {
  2535. if (netif_msg_drv(&debug))
  2536. dev_err(&pdev->dev, "unable to alloc new ethernet\n");
  2537. rc = -ENOMEM;
  2538. goto out;
  2539. }
  2540. SET_NETDEV_DEV(dev, &pdev->dev);
  2541. dev->netdev_ops = &rtl8169_netdev_ops;
  2542. tp = netdev_priv(dev);
  2543. tp->dev = dev;
  2544. tp->pci_dev = pdev;
  2545. tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
  2546. mii = &tp->mii;
  2547. mii->dev = dev;
  2548. mii->mdio_read = rtl_mdio_read;
  2549. mii->mdio_write = rtl_mdio_write;
  2550. mii->phy_id_mask = 0x1f;
  2551. mii->reg_num_mask = 0x1f;
  2552. mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
  2553. /* enable device (incl. PCI PM wakeup and hotplug setup) */
  2554. rc = pci_enable_device(pdev);
  2555. if (rc < 0) {
  2556. netif_err(tp, probe, dev, "enable failure\n");
  2557. goto err_out_free_dev_1;
  2558. }
  2559. if (pci_set_mwi(pdev) < 0)
  2560. netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
  2561. /* make sure PCI base addr 1 is MMIO */
  2562. if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
  2563. netif_err(tp, probe, dev,
  2564. "region #%d not an MMIO resource, aborting\n",
  2565. region);
  2566. rc = -ENODEV;
  2567. goto err_out_mwi_2;
  2568. }
  2569. /* check for weird/broken PCI region reporting */
  2570. if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
  2571. netif_err(tp, probe, dev,
  2572. "Invalid PCI region size(s), aborting\n");
  2573. rc = -ENODEV;
  2574. goto err_out_mwi_2;
  2575. }
  2576. rc = pci_request_regions(pdev, MODULENAME);
  2577. if (rc < 0) {
  2578. netif_err(tp, probe, dev, "could not request regions\n");
  2579. goto err_out_mwi_2;
  2580. }
  2581. tp->cp_cmd = RxChkSum;
  2582. if ((sizeof(dma_addr_t) > 4) &&
  2583. !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
  2584. tp->cp_cmd |= PCIDAC;
  2585. dev->features |= NETIF_F_HIGHDMA;
  2586. } else {
  2587. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  2588. if (rc < 0) {
  2589. netif_err(tp, probe, dev, "DMA configuration failed\n");
  2590. goto err_out_free_res_3;
  2591. }
  2592. }
  2593. /* ioremap MMIO region */
  2594. ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
  2595. if (!ioaddr) {
  2596. netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
  2597. rc = -EIO;
  2598. goto err_out_free_res_3;
  2599. }
  2600. tp->pcie_cap = pci_find_capability(pdev, PCI_CAP_ID_EXP);
  2601. if (!tp->pcie_cap)
  2602. netif_info(tp, probe, dev, "no PCI Express capability\n");
  2603. RTL_W16(IntrMask, 0x0000);
  2604. /* Soft reset the chip. */
  2605. RTL_W8(ChipCmd, CmdReset);
  2606. /* Check that the chip has finished the reset. */
  2607. for (i = 0; i < 100; i++) {
  2608. if ((RTL_R8(ChipCmd) & CmdReset) == 0)
  2609. break;
  2610. msleep_interruptible(1);
  2611. }
  2612. RTL_W16(IntrStatus, 0xffff);
  2613. pci_set_master(pdev);
  2614. /* Identify chip attached to board */
  2615. rtl8169_get_mac_version(tp, ioaddr);
  2616. rtl_init_mdio_ops(tp);
  2617. rtl_init_pll_power_ops(tp);
  2618. /* Use appropriate default if unknown */
  2619. if (tp->mac_version == RTL_GIGA_MAC_NONE) {
  2620. netif_notice(tp, probe, dev,
  2621. "unknown MAC, using family default\n");
  2622. tp->mac_version = cfg->default_ver;
  2623. }
  2624. rtl8169_print_mac_version(tp);
  2625. for (i = 0; i < ARRAY_SIZE(rtl_chip_info); i++) {
  2626. if (tp->mac_version == rtl_chip_info[i].mac_version)
  2627. break;
  2628. }
  2629. if (i == ARRAY_SIZE(rtl_chip_info)) {
  2630. dev_err(&pdev->dev,
  2631. "driver bug, MAC version not found in rtl_chip_info\n");
  2632. goto err_out_msi_4;
  2633. }
  2634. tp->chipset = i;
  2635. RTL_W8(Cfg9346, Cfg9346_Unlock);
  2636. RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
  2637. RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
  2638. if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
  2639. tp->features |= RTL_FEATURE_WOL;
  2640. if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
  2641. tp->features |= RTL_FEATURE_WOL;
  2642. tp->features |= rtl_try_msi(pdev, ioaddr, cfg);
  2643. RTL_W8(Cfg9346, Cfg9346_Lock);
  2644. if ((tp->mac_version <= RTL_GIGA_MAC_VER_06) &&
  2645. (RTL_R8(PHYstatus) & TBI_Enable)) {
  2646. tp->set_speed = rtl8169_set_speed_tbi;
  2647. tp->get_settings = rtl8169_gset_tbi;
  2648. tp->phy_reset_enable = rtl8169_tbi_reset_enable;
  2649. tp->phy_reset_pending = rtl8169_tbi_reset_pending;
  2650. tp->link_ok = rtl8169_tbi_link_ok;
  2651. tp->do_ioctl = rtl_tbi_ioctl;
  2652. tp->phy_1000_ctrl_reg = ADVERTISE_1000FULL; /* Implied by TBI */
  2653. } else {
  2654. tp->set_speed = rtl8169_set_speed_xmii;
  2655. tp->get_settings = rtl8169_gset_xmii;
  2656. tp->phy_reset_enable = rtl8169_xmii_reset_enable;
  2657. tp->phy_reset_pending = rtl8169_xmii_reset_pending;
  2658. tp->link_ok = rtl8169_xmii_link_ok;
  2659. tp->do_ioctl = rtl_xmii_ioctl;
  2660. }
  2661. spin_lock_init(&tp->lock);
  2662. tp->mmio_addr = ioaddr;
  2663. /* Get MAC address */
  2664. for (i = 0; i < MAC_ADDR_LEN; i++)
  2665. dev->dev_addr[i] = RTL_R8(MAC0 + i);
  2666. memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
  2667. SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
  2668. dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
  2669. dev->irq = pdev->irq;
  2670. dev->base_addr = (unsigned long) ioaddr;
  2671. netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
  2672. #ifdef CONFIG_R8169_VLAN
  2673. dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
  2674. #endif
  2675. dev->features |= NETIF_F_GRO;
  2676. tp->intr_mask = 0xffff;
  2677. tp->hw_start = cfg->hw_start;
  2678. tp->intr_event = cfg->intr_event;
  2679. tp->napi_event = cfg->napi_event;
  2680. init_timer(&tp->timer);
  2681. tp->timer.data = (unsigned long) dev;
  2682. tp->timer.function = rtl8169_phy_timer;
  2683. rc = register_netdev(dev);
  2684. if (rc < 0)
  2685. goto err_out_msi_4;
  2686. pci_set_drvdata(pdev, dev);
  2687. netif_info(tp, probe, dev, "%s at 0x%lx, %pM, XID %08x IRQ %d\n",
  2688. rtl_chip_info[tp->chipset].name,
  2689. dev->base_addr, dev->dev_addr,
  2690. (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), dev->irq);
  2691. if ((tp->mac_version == RTL_GIGA_MAC_VER_27) ||
  2692. (tp->mac_version == RTL_GIGA_MAC_VER_28)) {
  2693. rtl8168_driver_start(tp);
  2694. }
  2695. device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
  2696. if (pci_dev_run_wake(pdev))
  2697. pm_runtime_put_noidle(&pdev->dev);
  2698. netif_carrier_off(dev);
  2699. out:
  2700. return rc;
  2701. err_out_msi_4:
  2702. rtl_disable_msi(pdev, tp);
  2703. iounmap(ioaddr);
  2704. err_out_free_res_3:
  2705. pci_release_regions(pdev);
  2706. err_out_mwi_2:
  2707. pci_clear_mwi(pdev);
  2708. pci_disable_device(pdev);
  2709. err_out_free_dev_1:
  2710. free_netdev(dev);
  2711. goto out;
  2712. }
  2713. static void __devexit rtl8169_remove_one(struct pci_dev *pdev)
  2714. {
  2715. struct net_device *dev = pci_get_drvdata(pdev);
  2716. struct rtl8169_private *tp = netdev_priv(dev);
  2717. if ((tp->mac_version == RTL_GIGA_MAC_VER_27) ||
  2718. (tp->mac_version == RTL_GIGA_MAC_VER_28)) {
  2719. rtl8168_driver_stop(tp);
  2720. }
  2721. cancel_delayed_work_sync(&tp->task);
  2722. rtl_release_firmware(tp);
  2723. unregister_netdev(dev);
  2724. if (pci_dev_run_wake(pdev))
  2725. pm_runtime_get_noresume(&pdev->dev);
  2726. /* restore original MAC address */
  2727. rtl_rar_set(tp, dev->perm_addr);
  2728. rtl_disable_msi(pdev, tp);
  2729. rtl8169_release_board(pdev, dev, tp->mmio_addr);
  2730. pci_set_drvdata(pdev, NULL);
  2731. }
  2732. static int rtl8169_open(struct net_device *dev)
  2733. {
  2734. struct rtl8169_private *tp = netdev_priv(dev);
  2735. void __iomem *ioaddr = tp->mmio_addr;
  2736. struct pci_dev *pdev = tp->pci_dev;
  2737. int retval = -ENOMEM;
  2738. pm_runtime_get_sync(&pdev->dev);
  2739. /*
  2740. * Rx and Tx desscriptors needs 256 bytes alignment.
  2741. * dma_alloc_coherent provides more.
  2742. */
  2743. tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
  2744. &tp->TxPhyAddr, GFP_KERNEL);
  2745. if (!tp->TxDescArray)
  2746. goto err_pm_runtime_put;
  2747. tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
  2748. &tp->RxPhyAddr, GFP_KERNEL);
  2749. if (!tp->RxDescArray)
  2750. goto err_free_tx_0;
  2751. retval = rtl8169_init_ring(dev);
  2752. if (retval < 0)
  2753. goto err_free_rx_1;
  2754. INIT_DELAYED_WORK(&tp->task, NULL);
  2755. smp_mb();
  2756. retval = request_irq(dev->irq, rtl8169_interrupt,
  2757. (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
  2758. dev->name, dev);
  2759. if (retval < 0)
  2760. goto err_release_ring_2;
  2761. napi_enable(&tp->napi);
  2762. rtl8169_init_phy(dev, tp);
  2763. /*
  2764. * Pretend we are using VLANs; This bypasses a nasty bug where
  2765. * Interrupts stop flowing on high load on 8110SCd controllers.
  2766. */
  2767. if (tp->mac_version == RTL_GIGA_MAC_VER_05)
  2768. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | RxVlan);
  2769. rtl_pll_power_up(tp);
  2770. rtl_hw_start(dev);
  2771. rtl8169_request_timer(dev);
  2772. tp->saved_wolopts = 0;
  2773. pm_runtime_put_noidle(&pdev->dev);
  2774. rtl8169_check_link_status(dev, tp, ioaddr);
  2775. out:
  2776. return retval;
  2777. err_release_ring_2:
  2778. rtl8169_rx_clear(tp);
  2779. err_free_rx_1:
  2780. dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
  2781. tp->RxPhyAddr);
  2782. tp->RxDescArray = NULL;
  2783. err_free_tx_0:
  2784. dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
  2785. tp->TxPhyAddr);
  2786. tp->TxDescArray = NULL;
  2787. err_pm_runtime_put:
  2788. pm_runtime_put_noidle(&pdev->dev);
  2789. goto out;
  2790. }
  2791. static void rtl8169_hw_reset(struct rtl8169_private *tp)
  2792. {
  2793. void __iomem *ioaddr = tp->mmio_addr;
  2794. /* Disable interrupts */
  2795. rtl8169_irq_mask_and_ack(ioaddr);
  2796. if (tp->mac_version == RTL_GIGA_MAC_VER_28) {
  2797. while (RTL_R8(TxPoll) & NPQ)
  2798. udelay(20);
  2799. }
  2800. /* Reset the chipset */
  2801. RTL_W8(ChipCmd, CmdReset);
  2802. /* PCI commit */
  2803. RTL_R8(ChipCmd);
  2804. }
  2805. static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
  2806. {
  2807. void __iomem *ioaddr = tp->mmio_addr;
  2808. u32 cfg = rtl8169_rx_config;
  2809. cfg |= (RTL_R32(RxConfig) & rtl_chip_info[tp->chipset].RxConfigMask);
  2810. RTL_W32(RxConfig, cfg);
  2811. /* Set DMA burst size and Interframe Gap Time */
  2812. RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
  2813. (InterFrameGap << TxInterFrameGapShift));
  2814. }
  2815. static void rtl_hw_start(struct net_device *dev)
  2816. {
  2817. struct rtl8169_private *tp = netdev_priv(dev);
  2818. void __iomem *ioaddr = tp->mmio_addr;
  2819. unsigned int i;
  2820. /* Soft reset the chip. */
  2821. RTL_W8(ChipCmd, CmdReset);
  2822. /* Check that the chip has finished the reset. */
  2823. for (i = 0; i < 100; i++) {
  2824. if ((RTL_R8(ChipCmd) & CmdReset) == 0)
  2825. break;
  2826. msleep_interruptible(1);
  2827. }
  2828. tp->hw_start(dev);
  2829. netif_start_queue(dev);
  2830. }
  2831. static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
  2832. void __iomem *ioaddr)
  2833. {
  2834. /*
  2835. * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
  2836. * register to be written before TxDescAddrLow to work.
  2837. * Switching from MMIO to I/O access fixes the issue as well.
  2838. */
  2839. RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
  2840. RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
  2841. RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
  2842. RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
  2843. }
  2844. static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
  2845. {
  2846. u16 cmd;
  2847. cmd = RTL_R16(CPlusCmd);
  2848. RTL_W16(CPlusCmd, cmd);
  2849. return cmd;
  2850. }
  2851. static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
  2852. {
  2853. /* Low hurts. Let's disable the filtering. */
  2854. RTL_W16(RxMaxSize, rx_buf_sz + 1);
  2855. }
  2856. static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
  2857. {
  2858. static const struct {
  2859. u32 mac_version;
  2860. u32 clk;
  2861. u32 val;
  2862. } cfg2_info [] = {
  2863. { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
  2864. { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
  2865. { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
  2866. { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
  2867. }, *p = cfg2_info;
  2868. unsigned int i;
  2869. u32 clk;
  2870. clk = RTL_R8(Config2) & PCI_Clock_66MHz;
  2871. for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
  2872. if ((p->mac_version == mac_version) && (p->clk == clk)) {
  2873. RTL_W32(0x7c, p->val);
  2874. break;
  2875. }
  2876. }
  2877. }
  2878. static void rtl_hw_start_8169(struct net_device *dev)
  2879. {
  2880. struct rtl8169_private *tp = netdev_priv(dev);
  2881. void __iomem *ioaddr = tp->mmio_addr;
  2882. struct pci_dev *pdev = tp->pci_dev;
  2883. if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
  2884. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
  2885. pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
  2886. }
  2887. RTL_W8(Cfg9346, Cfg9346_Unlock);
  2888. if ((tp->mac_version == RTL_GIGA_MAC_VER_01) ||
  2889. (tp->mac_version == RTL_GIGA_MAC_VER_02) ||
  2890. (tp->mac_version == RTL_GIGA_MAC_VER_03) ||
  2891. (tp->mac_version == RTL_GIGA_MAC_VER_04))
  2892. RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
  2893. RTL_W8(EarlyTxThres, NoEarlyTx);
  2894. rtl_set_rx_max_size(ioaddr, rx_buf_sz);
  2895. if ((tp->mac_version == RTL_GIGA_MAC_VER_01) ||
  2896. (tp->mac_version == RTL_GIGA_MAC_VER_02) ||
  2897. (tp->mac_version == RTL_GIGA_MAC_VER_03) ||
  2898. (tp->mac_version == RTL_GIGA_MAC_VER_04))
  2899. rtl_set_rx_tx_config_registers(tp);
  2900. tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
  2901. if ((tp->mac_version == RTL_GIGA_MAC_VER_02) ||
  2902. (tp->mac_version == RTL_GIGA_MAC_VER_03)) {
  2903. dprintk("Set MAC Reg C+CR Offset 0xE0. "
  2904. "Bit-3 and bit-14 MUST be 1\n");
  2905. tp->cp_cmd |= (1 << 14);
  2906. }
  2907. RTL_W16(CPlusCmd, tp->cp_cmd);
  2908. rtl8169_set_magic_reg(ioaddr, tp->mac_version);
  2909. /*
  2910. * Undocumented corner. Supposedly:
  2911. * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
  2912. */
  2913. RTL_W16(IntrMitigate, 0x0000);
  2914. rtl_set_rx_tx_desc_registers(tp, ioaddr);
  2915. if ((tp->mac_version != RTL_GIGA_MAC_VER_01) &&
  2916. (tp->mac_version != RTL_GIGA_MAC_VER_02) &&
  2917. (tp->mac_version != RTL_GIGA_MAC_VER_03) &&
  2918. (tp->mac_version != RTL_GIGA_MAC_VER_04)) {
  2919. RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
  2920. rtl_set_rx_tx_config_registers(tp);
  2921. }
  2922. RTL_W8(Cfg9346, Cfg9346_Lock);
  2923. /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
  2924. RTL_R8(IntrMask);
  2925. RTL_W32(RxMissed, 0);
  2926. rtl_set_rx_mode(dev);
  2927. /* no early-rx interrupts */
  2928. RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
  2929. /* Enable all known interrupts by setting the interrupt mask. */
  2930. RTL_W16(IntrMask, tp->intr_event);
  2931. }
  2932. static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
  2933. {
  2934. struct net_device *dev = pci_get_drvdata(pdev);
  2935. struct rtl8169_private *tp = netdev_priv(dev);
  2936. int cap = tp->pcie_cap;
  2937. if (cap) {
  2938. u16 ctl;
  2939. pci_read_config_word(pdev, cap + PCI_EXP_DEVCTL, &ctl);
  2940. ctl = (ctl & ~PCI_EXP_DEVCTL_READRQ) | force;
  2941. pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL, ctl);
  2942. }
  2943. }
  2944. static void rtl_csi_access_enable(void __iomem *ioaddr, u32 bits)
  2945. {
  2946. u32 csi;
  2947. csi = rtl_csi_read(ioaddr, 0x070c) & 0x00ffffff;
  2948. rtl_csi_write(ioaddr, 0x070c, csi | bits);
  2949. }
  2950. static void rtl_csi_access_enable_1(void __iomem *ioaddr)
  2951. {
  2952. rtl_csi_access_enable(ioaddr, 0x17000000);
  2953. }
  2954. static void rtl_csi_access_enable_2(void __iomem *ioaddr)
  2955. {
  2956. rtl_csi_access_enable(ioaddr, 0x27000000);
  2957. }
  2958. struct ephy_info {
  2959. unsigned int offset;
  2960. u16 mask;
  2961. u16 bits;
  2962. };
  2963. static void rtl_ephy_init(void __iomem *ioaddr, const struct ephy_info *e, int len)
  2964. {
  2965. u16 w;
  2966. while (len-- > 0) {
  2967. w = (rtl_ephy_read(ioaddr, e->offset) & ~e->mask) | e->bits;
  2968. rtl_ephy_write(ioaddr, e->offset, w);
  2969. e++;
  2970. }
  2971. }
  2972. static void rtl_disable_clock_request(struct pci_dev *pdev)
  2973. {
  2974. struct net_device *dev = pci_get_drvdata(pdev);
  2975. struct rtl8169_private *tp = netdev_priv(dev);
  2976. int cap = tp->pcie_cap;
  2977. if (cap) {
  2978. u16 ctl;
  2979. pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
  2980. ctl &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
  2981. pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
  2982. }
  2983. }
  2984. static void rtl_enable_clock_request(struct pci_dev *pdev)
  2985. {
  2986. struct net_device *dev = pci_get_drvdata(pdev);
  2987. struct rtl8169_private *tp = netdev_priv(dev);
  2988. int cap = tp->pcie_cap;
  2989. if (cap) {
  2990. u16 ctl;
  2991. pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
  2992. ctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
  2993. pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
  2994. }
  2995. }
  2996. #define R8168_CPCMD_QUIRK_MASK (\
  2997. EnableBist | \
  2998. Mac_dbgo_oe | \
  2999. Force_half_dup | \
  3000. Force_rxflow_en | \
  3001. Force_txflow_en | \
  3002. Cxpl_dbg_sel | \
  3003. ASF | \
  3004. PktCntrDisable | \
  3005. Mac_dbgo_sel)
  3006. static void rtl_hw_start_8168bb(void __iomem *ioaddr, struct pci_dev *pdev)
  3007. {
  3008. RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
  3009. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
  3010. rtl_tx_performance_tweak(pdev,
  3011. (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
  3012. }
  3013. static void rtl_hw_start_8168bef(void __iomem *ioaddr, struct pci_dev *pdev)
  3014. {
  3015. rtl_hw_start_8168bb(ioaddr, pdev);
  3016. RTL_W8(MaxTxPacketSize, TxPacketMax);
  3017. RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
  3018. }
  3019. static void __rtl_hw_start_8168cp(void __iomem *ioaddr, struct pci_dev *pdev)
  3020. {
  3021. RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
  3022. RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
  3023. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  3024. rtl_disable_clock_request(pdev);
  3025. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
  3026. }
  3027. static void rtl_hw_start_8168cp_1(void __iomem *ioaddr, struct pci_dev *pdev)
  3028. {
  3029. static const struct ephy_info e_info_8168cp[] = {
  3030. { 0x01, 0, 0x0001 },
  3031. { 0x02, 0x0800, 0x1000 },
  3032. { 0x03, 0, 0x0042 },
  3033. { 0x06, 0x0080, 0x0000 },
  3034. { 0x07, 0, 0x2000 }
  3035. };
  3036. rtl_csi_access_enable_2(ioaddr);
  3037. rtl_ephy_init(ioaddr, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
  3038. __rtl_hw_start_8168cp(ioaddr, pdev);
  3039. }
  3040. static void rtl_hw_start_8168cp_2(void __iomem *ioaddr, struct pci_dev *pdev)
  3041. {
  3042. rtl_csi_access_enable_2(ioaddr);
  3043. RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
  3044. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  3045. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
  3046. }
  3047. static void rtl_hw_start_8168cp_3(void __iomem *ioaddr, struct pci_dev *pdev)
  3048. {
  3049. rtl_csi_access_enable_2(ioaddr);
  3050. RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
  3051. /* Magic. */
  3052. RTL_W8(DBG_REG, 0x20);
  3053. RTL_W8(MaxTxPacketSize, TxPacketMax);
  3054. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  3055. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
  3056. }
  3057. static void rtl_hw_start_8168c_1(void __iomem *ioaddr, struct pci_dev *pdev)
  3058. {
  3059. static const struct ephy_info e_info_8168c_1[] = {
  3060. { 0x02, 0x0800, 0x1000 },
  3061. { 0x03, 0, 0x0002 },
  3062. { 0x06, 0x0080, 0x0000 }
  3063. };
  3064. rtl_csi_access_enable_2(ioaddr);
  3065. RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
  3066. rtl_ephy_init(ioaddr, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
  3067. __rtl_hw_start_8168cp(ioaddr, pdev);
  3068. }
  3069. static void rtl_hw_start_8168c_2(void __iomem *ioaddr, struct pci_dev *pdev)
  3070. {
  3071. static const struct ephy_info e_info_8168c_2[] = {
  3072. { 0x01, 0, 0x0001 },
  3073. { 0x03, 0x0400, 0x0220 }
  3074. };
  3075. rtl_csi_access_enable_2(ioaddr);
  3076. rtl_ephy_init(ioaddr, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
  3077. __rtl_hw_start_8168cp(ioaddr, pdev);
  3078. }
  3079. static void rtl_hw_start_8168c_3(void __iomem *ioaddr, struct pci_dev *pdev)
  3080. {
  3081. rtl_hw_start_8168c_2(ioaddr, pdev);
  3082. }
  3083. static void rtl_hw_start_8168c_4(void __iomem *ioaddr, struct pci_dev *pdev)
  3084. {
  3085. rtl_csi_access_enable_2(ioaddr);
  3086. __rtl_hw_start_8168cp(ioaddr, pdev);
  3087. }
  3088. static void rtl_hw_start_8168d(void __iomem *ioaddr, struct pci_dev *pdev)
  3089. {
  3090. rtl_csi_access_enable_2(ioaddr);
  3091. rtl_disable_clock_request(pdev);
  3092. RTL_W8(MaxTxPacketSize, TxPacketMax);
  3093. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  3094. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
  3095. }
  3096. static void rtl_hw_start_8168d_4(void __iomem *ioaddr, struct pci_dev *pdev)
  3097. {
  3098. static const struct ephy_info e_info_8168d_4[] = {
  3099. { 0x0b, ~0, 0x48 },
  3100. { 0x19, 0x20, 0x50 },
  3101. { 0x0c, ~0, 0x20 }
  3102. };
  3103. int i;
  3104. rtl_csi_access_enable_1(ioaddr);
  3105. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  3106. RTL_W8(MaxTxPacketSize, TxPacketMax);
  3107. for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
  3108. const struct ephy_info *e = e_info_8168d_4 + i;
  3109. u16 w;
  3110. w = rtl_ephy_read(ioaddr, e->offset);
  3111. rtl_ephy_write(ioaddr, 0x03, (w & e->mask) | e->bits);
  3112. }
  3113. rtl_enable_clock_request(pdev);
  3114. }
  3115. static void rtl_hw_start_8168(struct net_device *dev)
  3116. {
  3117. struct rtl8169_private *tp = netdev_priv(dev);
  3118. void __iomem *ioaddr = tp->mmio_addr;
  3119. struct pci_dev *pdev = tp->pci_dev;
  3120. RTL_W8(Cfg9346, Cfg9346_Unlock);
  3121. RTL_W8(MaxTxPacketSize, TxPacketMax);
  3122. rtl_set_rx_max_size(ioaddr, rx_buf_sz);
  3123. tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
  3124. RTL_W16(CPlusCmd, tp->cp_cmd);
  3125. RTL_W16(IntrMitigate, 0x5151);
  3126. /* Work around for RxFIFO overflow. */
  3127. if (tp->mac_version == RTL_GIGA_MAC_VER_11 ||
  3128. tp->mac_version == RTL_GIGA_MAC_VER_22) {
  3129. tp->intr_event |= RxFIFOOver | PCSTimeout;
  3130. tp->intr_event &= ~RxOverflow;
  3131. }
  3132. rtl_set_rx_tx_desc_registers(tp, ioaddr);
  3133. rtl_set_rx_mode(dev);
  3134. RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
  3135. (InterFrameGap << TxInterFrameGapShift));
  3136. RTL_R8(IntrMask);
  3137. switch (tp->mac_version) {
  3138. case RTL_GIGA_MAC_VER_11:
  3139. rtl_hw_start_8168bb(ioaddr, pdev);
  3140. break;
  3141. case RTL_GIGA_MAC_VER_12:
  3142. case RTL_GIGA_MAC_VER_17:
  3143. rtl_hw_start_8168bef(ioaddr, pdev);
  3144. break;
  3145. case RTL_GIGA_MAC_VER_18:
  3146. rtl_hw_start_8168cp_1(ioaddr, pdev);
  3147. break;
  3148. case RTL_GIGA_MAC_VER_19:
  3149. rtl_hw_start_8168c_1(ioaddr, pdev);
  3150. break;
  3151. case RTL_GIGA_MAC_VER_20:
  3152. rtl_hw_start_8168c_2(ioaddr, pdev);
  3153. break;
  3154. case RTL_GIGA_MAC_VER_21:
  3155. rtl_hw_start_8168c_3(ioaddr, pdev);
  3156. break;
  3157. case RTL_GIGA_MAC_VER_22:
  3158. rtl_hw_start_8168c_4(ioaddr, pdev);
  3159. break;
  3160. case RTL_GIGA_MAC_VER_23:
  3161. rtl_hw_start_8168cp_2(ioaddr, pdev);
  3162. break;
  3163. case RTL_GIGA_MAC_VER_24:
  3164. rtl_hw_start_8168cp_3(ioaddr, pdev);
  3165. break;
  3166. case RTL_GIGA_MAC_VER_25:
  3167. case RTL_GIGA_MAC_VER_26:
  3168. case RTL_GIGA_MAC_VER_27:
  3169. rtl_hw_start_8168d(ioaddr, pdev);
  3170. break;
  3171. case RTL_GIGA_MAC_VER_28:
  3172. rtl_hw_start_8168d_4(ioaddr, pdev);
  3173. break;
  3174. default:
  3175. printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
  3176. dev->name, tp->mac_version);
  3177. break;
  3178. }
  3179. RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
  3180. RTL_W8(Cfg9346, Cfg9346_Lock);
  3181. RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
  3182. RTL_W16(IntrMask, tp->intr_event);
  3183. }
  3184. #define R810X_CPCMD_QUIRK_MASK (\
  3185. EnableBist | \
  3186. Mac_dbgo_oe | \
  3187. Force_half_dup | \
  3188. Force_rxflow_en | \
  3189. Force_txflow_en | \
  3190. Cxpl_dbg_sel | \
  3191. ASF | \
  3192. PktCntrDisable | \
  3193. Mac_dbgo_sel)
  3194. static void rtl_hw_start_8102e_1(void __iomem *ioaddr, struct pci_dev *pdev)
  3195. {
  3196. static const struct ephy_info e_info_8102e_1[] = {
  3197. { 0x01, 0, 0x6e65 },
  3198. { 0x02, 0, 0x091f },
  3199. { 0x03, 0, 0xc2f9 },
  3200. { 0x06, 0, 0xafb5 },
  3201. { 0x07, 0, 0x0e00 },
  3202. { 0x19, 0, 0xec80 },
  3203. { 0x01, 0, 0x2e65 },
  3204. { 0x01, 0, 0x6e65 }
  3205. };
  3206. u8 cfg1;
  3207. rtl_csi_access_enable_2(ioaddr);
  3208. RTL_W8(DBG_REG, FIX_NAK_1);
  3209. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  3210. RTL_W8(Config1,
  3211. LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
  3212. RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
  3213. cfg1 = RTL_R8(Config1);
  3214. if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
  3215. RTL_W8(Config1, cfg1 & ~LEDS0);
  3216. rtl_ephy_init(ioaddr, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
  3217. }
  3218. static void rtl_hw_start_8102e_2(void __iomem *ioaddr, struct pci_dev *pdev)
  3219. {
  3220. rtl_csi_access_enable_2(ioaddr);
  3221. rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
  3222. RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
  3223. RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
  3224. }
  3225. static void rtl_hw_start_8102e_3(void __iomem *ioaddr, struct pci_dev *pdev)
  3226. {
  3227. rtl_hw_start_8102e_2(ioaddr, pdev);
  3228. rtl_ephy_write(ioaddr, 0x03, 0xc2f9);
  3229. }
  3230. static void rtl_hw_start_8101(struct net_device *dev)
  3231. {
  3232. struct rtl8169_private *tp = netdev_priv(dev);
  3233. void __iomem *ioaddr = tp->mmio_addr;
  3234. struct pci_dev *pdev = tp->pci_dev;
  3235. if ((tp->mac_version == RTL_GIGA_MAC_VER_13) ||
  3236. (tp->mac_version == RTL_GIGA_MAC_VER_16)) {
  3237. int cap = tp->pcie_cap;
  3238. if (cap) {
  3239. pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL,
  3240. PCI_EXP_DEVCTL_NOSNOOP_EN);
  3241. }
  3242. }
  3243. RTL_W8(Cfg9346, Cfg9346_Unlock);
  3244. switch (tp->mac_version) {
  3245. case RTL_GIGA_MAC_VER_07:
  3246. rtl_hw_start_8102e_1(ioaddr, pdev);
  3247. break;
  3248. case RTL_GIGA_MAC_VER_08:
  3249. rtl_hw_start_8102e_3(ioaddr, pdev);
  3250. break;
  3251. case RTL_GIGA_MAC_VER_09:
  3252. rtl_hw_start_8102e_2(ioaddr, pdev);
  3253. break;
  3254. }
  3255. RTL_W8(Cfg9346, Cfg9346_Lock);
  3256. RTL_W8(MaxTxPacketSize, TxPacketMax);
  3257. rtl_set_rx_max_size(ioaddr, rx_buf_sz);
  3258. tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
  3259. RTL_W16(CPlusCmd, tp->cp_cmd);
  3260. RTL_W16(IntrMitigate, 0x0000);
  3261. rtl_set_rx_tx_desc_registers(tp, ioaddr);
  3262. RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
  3263. rtl_set_rx_tx_config_registers(tp);
  3264. RTL_R8(IntrMask);
  3265. rtl_set_rx_mode(dev);
  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. if (unlikely(status & RxFIFOOver)) {
  3823. switch (tp->mac_version) {
  3824. /* Work around for rx fifo overflow */
  3825. case RTL_GIGA_MAC_VER_11:
  3826. case RTL_GIGA_MAC_VER_22:
  3827. case RTL_GIGA_MAC_VER_26:
  3828. netif_stop_queue(dev);
  3829. rtl8169_tx_timeout(dev);
  3830. goto done;
  3831. /* Testers needed. */
  3832. case RTL_GIGA_MAC_VER_17:
  3833. case RTL_GIGA_MAC_VER_19:
  3834. case RTL_GIGA_MAC_VER_20:
  3835. case RTL_GIGA_MAC_VER_21:
  3836. case RTL_GIGA_MAC_VER_23:
  3837. case RTL_GIGA_MAC_VER_24:
  3838. case RTL_GIGA_MAC_VER_27:
  3839. case RTL_GIGA_MAC_VER_28:
  3840. /* Experimental science. Pktgen proof. */
  3841. case RTL_GIGA_MAC_VER_12:
  3842. case RTL_GIGA_MAC_VER_25:
  3843. if (status == RxFIFOOver)
  3844. goto done;
  3845. break;
  3846. default:
  3847. break;
  3848. }
  3849. }
  3850. if (unlikely(status & SYSErr)) {
  3851. rtl8169_pcierr_interrupt(dev);
  3852. break;
  3853. }
  3854. if (status & LinkChg)
  3855. __rtl8169_check_link_status(dev, tp, ioaddr, true);
  3856. /* We need to see the lastest version of tp->intr_mask to
  3857. * avoid ignoring an MSI interrupt and having to wait for
  3858. * another event which may never come.
  3859. */
  3860. smp_rmb();
  3861. if (status & tp->intr_mask & tp->napi_event) {
  3862. RTL_W16(IntrMask, tp->intr_event & ~tp->napi_event);
  3863. tp->intr_mask = ~tp->napi_event;
  3864. if (likely(napi_schedule_prep(&tp->napi)))
  3865. __napi_schedule(&tp->napi);
  3866. else
  3867. netif_info(tp, intr, dev,
  3868. "interrupt %04x in poll\n", status);
  3869. }
  3870. /* We only get a new MSI interrupt when all active irq
  3871. * sources on the chip have been acknowledged. So, ack
  3872. * everything we've seen and check if new sources have become
  3873. * active to avoid blocking all interrupts from the chip.
  3874. */
  3875. RTL_W16(IntrStatus,
  3876. (status & RxFIFOOver) ? (status | RxOverflow) : status);
  3877. status = RTL_R16(IntrStatus);
  3878. }
  3879. done:
  3880. return IRQ_RETVAL(handled);
  3881. }
  3882. static int rtl8169_poll(struct napi_struct *napi, int budget)
  3883. {
  3884. struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
  3885. struct net_device *dev = tp->dev;
  3886. void __iomem *ioaddr = tp->mmio_addr;
  3887. int work_done;
  3888. work_done = rtl8169_rx_interrupt(dev, tp, ioaddr, (u32) budget);
  3889. rtl8169_tx_interrupt(dev, tp, ioaddr);
  3890. if (work_done < budget) {
  3891. napi_complete(napi);
  3892. /* We need for force the visibility of tp->intr_mask
  3893. * for other CPUs, as we can loose an MSI interrupt
  3894. * and potentially wait for a retransmit timeout if we don't.
  3895. * The posted write to IntrMask is safe, as it will
  3896. * eventually make it to the chip and we won't loose anything
  3897. * until it does.
  3898. */
  3899. tp->intr_mask = 0xffff;
  3900. wmb();
  3901. RTL_W16(IntrMask, tp->intr_event);
  3902. }
  3903. return work_done;
  3904. }
  3905. static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
  3906. {
  3907. struct rtl8169_private *tp = netdev_priv(dev);
  3908. if (tp->mac_version > RTL_GIGA_MAC_VER_06)
  3909. return;
  3910. dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
  3911. RTL_W32(RxMissed, 0);
  3912. }
  3913. static void rtl8169_down(struct net_device *dev)
  3914. {
  3915. struct rtl8169_private *tp = netdev_priv(dev);
  3916. void __iomem *ioaddr = tp->mmio_addr;
  3917. rtl8169_delete_timer(dev);
  3918. netif_stop_queue(dev);
  3919. napi_disable(&tp->napi);
  3920. spin_lock_irq(&tp->lock);
  3921. rtl8169_asic_down(ioaddr);
  3922. /*
  3923. * At this point device interrupts can not be enabled in any function,
  3924. * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task,
  3925. * rtl8169_reinit_task) and napi is disabled (rtl8169_poll).
  3926. */
  3927. rtl8169_rx_missed(dev, ioaddr);
  3928. spin_unlock_irq(&tp->lock);
  3929. synchronize_irq(dev->irq);
  3930. /* Give a racing hard_start_xmit a few cycles to complete. */
  3931. synchronize_sched(); /* FIXME: should this be synchronize_irq()? */
  3932. rtl8169_tx_clear(tp);
  3933. rtl8169_rx_clear(tp);
  3934. rtl_pll_power_down(tp);
  3935. }
  3936. static int rtl8169_close(struct net_device *dev)
  3937. {
  3938. struct rtl8169_private *tp = netdev_priv(dev);
  3939. struct pci_dev *pdev = tp->pci_dev;
  3940. pm_runtime_get_sync(&pdev->dev);
  3941. /* update counters before going down */
  3942. rtl8169_update_counters(dev);
  3943. rtl8169_down(dev);
  3944. free_irq(dev->irq, dev);
  3945. dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
  3946. tp->RxPhyAddr);
  3947. dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
  3948. tp->TxPhyAddr);
  3949. tp->TxDescArray = NULL;
  3950. tp->RxDescArray = NULL;
  3951. pm_runtime_put_sync(&pdev->dev);
  3952. return 0;
  3953. }
  3954. static void rtl_set_rx_mode(struct net_device *dev)
  3955. {
  3956. struct rtl8169_private *tp = netdev_priv(dev);
  3957. void __iomem *ioaddr = tp->mmio_addr;
  3958. unsigned long flags;
  3959. u32 mc_filter[2]; /* Multicast hash filter */
  3960. int rx_mode;
  3961. u32 tmp = 0;
  3962. if (dev->flags & IFF_PROMISC) {
  3963. /* Unconditionally log net taps. */
  3964. netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
  3965. rx_mode =
  3966. AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
  3967. AcceptAllPhys;
  3968. mc_filter[1] = mc_filter[0] = 0xffffffff;
  3969. } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
  3970. (dev->flags & IFF_ALLMULTI)) {
  3971. /* Too many to filter perfectly -- accept all multicasts. */
  3972. rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
  3973. mc_filter[1] = mc_filter[0] = 0xffffffff;
  3974. } else {
  3975. struct netdev_hw_addr *ha;
  3976. rx_mode = AcceptBroadcast | AcceptMyPhys;
  3977. mc_filter[1] = mc_filter[0] = 0;
  3978. netdev_for_each_mc_addr(ha, dev) {
  3979. int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
  3980. mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
  3981. rx_mode |= AcceptMulticast;
  3982. }
  3983. }
  3984. spin_lock_irqsave(&tp->lock, flags);
  3985. tmp = rtl8169_rx_config | rx_mode |
  3986. (RTL_R32(RxConfig) & rtl_chip_info[tp->chipset].RxConfigMask);
  3987. if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
  3988. u32 data = mc_filter[0];
  3989. mc_filter[0] = swab32(mc_filter[1]);
  3990. mc_filter[1] = swab32(data);
  3991. }
  3992. RTL_W32(MAR0 + 4, mc_filter[1]);
  3993. RTL_W32(MAR0 + 0, mc_filter[0]);
  3994. RTL_W32(RxConfig, tmp);
  3995. spin_unlock_irqrestore(&tp->lock, flags);
  3996. }
  3997. /**
  3998. * rtl8169_get_stats - Get rtl8169 read/write statistics
  3999. * @dev: The Ethernet Device to get statistics for
  4000. *
  4001. * Get TX/RX statistics for rtl8169
  4002. */
  4003. static struct net_device_stats *rtl8169_get_stats(struct net_device *dev)
  4004. {
  4005. struct rtl8169_private *tp = netdev_priv(dev);
  4006. void __iomem *ioaddr = tp->mmio_addr;
  4007. unsigned long flags;
  4008. if (netif_running(dev)) {
  4009. spin_lock_irqsave(&tp->lock, flags);
  4010. rtl8169_rx_missed(dev, ioaddr);
  4011. spin_unlock_irqrestore(&tp->lock, flags);
  4012. }
  4013. return &dev->stats;
  4014. }
  4015. static void rtl8169_net_suspend(struct net_device *dev)
  4016. {
  4017. struct rtl8169_private *tp = netdev_priv(dev);
  4018. if (!netif_running(dev))
  4019. return;
  4020. rtl_pll_power_down(tp);
  4021. netif_device_detach(dev);
  4022. netif_stop_queue(dev);
  4023. }
  4024. #ifdef CONFIG_PM
  4025. static int rtl8169_suspend(struct device *device)
  4026. {
  4027. struct pci_dev *pdev = to_pci_dev(device);
  4028. struct net_device *dev = pci_get_drvdata(pdev);
  4029. rtl8169_net_suspend(dev);
  4030. return 0;
  4031. }
  4032. static void __rtl8169_resume(struct net_device *dev)
  4033. {
  4034. struct rtl8169_private *tp = netdev_priv(dev);
  4035. netif_device_attach(dev);
  4036. rtl_pll_power_up(tp);
  4037. rtl8169_schedule_work(dev, rtl8169_reset_task);
  4038. }
  4039. static int rtl8169_resume(struct device *device)
  4040. {
  4041. struct pci_dev *pdev = to_pci_dev(device);
  4042. struct net_device *dev = pci_get_drvdata(pdev);
  4043. struct rtl8169_private *tp = netdev_priv(dev);
  4044. rtl8169_init_phy(dev, tp);
  4045. if (netif_running(dev))
  4046. __rtl8169_resume(dev);
  4047. return 0;
  4048. }
  4049. static int rtl8169_runtime_suspend(struct device *device)
  4050. {
  4051. struct pci_dev *pdev = to_pci_dev(device);
  4052. struct net_device *dev = pci_get_drvdata(pdev);
  4053. struct rtl8169_private *tp = netdev_priv(dev);
  4054. if (!tp->TxDescArray)
  4055. return 0;
  4056. spin_lock_irq(&tp->lock);
  4057. tp->saved_wolopts = __rtl8169_get_wol(tp);
  4058. __rtl8169_set_wol(tp, WAKE_ANY);
  4059. spin_unlock_irq(&tp->lock);
  4060. rtl8169_net_suspend(dev);
  4061. return 0;
  4062. }
  4063. static int rtl8169_runtime_resume(struct device *device)
  4064. {
  4065. struct pci_dev *pdev = to_pci_dev(device);
  4066. struct net_device *dev = pci_get_drvdata(pdev);
  4067. struct rtl8169_private *tp = netdev_priv(dev);
  4068. if (!tp->TxDescArray)
  4069. return 0;
  4070. spin_lock_irq(&tp->lock);
  4071. __rtl8169_set_wol(tp, tp->saved_wolopts);
  4072. tp->saved_wolopts = 0;
  4073. spin_unlock_irq(&tp->lock);
  4074. rtl8169_init_phy(dev, tp);
  4075. __rtl8169_resume(dev);
  4076. return 0;
  4077. }
  4078. static int rtl8169_runtime_idle(struct device *device)
  4079. {
  4080. struct pci_dev *pdev = to_pci_dev(device);
  4081. struct net_device *dev = pci_get_drvdata(pdev);
  4082. struct rtl8169_private *tp = netdev_priv(dev);
  4083. return tp->TxDescArray ? -EBUSY : 0;
  4084. }
  4085. static const struct dev_pm_ops rtl8169_pm_ops = {
  4086. .suspend = rtl8169_suspend,
  4087. .resume = rtl8169_resume,
  4088. .freeze = rtl8169_suspend,
  4089. .thaw = rtl8169_resume,
  4090. .poweroff = rtl8169_suspend,
  4091. .restore = rtl8169_resume,
  4092. .runtime_suspend = rtl8169_runtime_suspend,
  4093. .runtime_resume = rtl8169_runtime_resume,
  4094. .runtime_idle = rtl8169_runtime_idle,
  4095. };
  4096. #define RTL8169_PM_OPS (&rtl8169_pm_ops)
  4097. #else /* !CONFIG_PM */
  4098. #define RTL8169_PM_OPS NULL
  4099. #endif /* !CONFIG_PM */
  4100. static void rtl_shutdown(struct pci_dev *pdev)
  4101. {
  4102. struct net_device *dev = pci_get_drvdata(pdev);
  4103. struct rtl8169_private *tp = netdev_priv(dev);
  4104. void __iomem *ioaddr = tp->mmio_addr;
  4105. rtl8169_net_suspend(dev);
  4106. /* restore original MAC address */
  4107. rtl_rar_set(tp, dev->perm_addr);
  4108. spin_lock_irq(&tp->lock);
  4109. rtl8169_asic_down(ioaddr);
  4110. spin_unlock_irq(&tp->lock);
  4111. if (system_state == SYSTEM_POWER_OFF) {
  4112. /* WoL fails with some 8168 when the receiver is disabled. */
  4113. if (tp->features & RTL_FEATURE_WOL) {
  4114. pci_clear_master(pdev);
  4115. RTL_W8(ChipCmd, CmdRxEnb);
  4116. /* PCI commit */
  4117. RTL_R8(ChipCmd);
  4118. }
  4119. pci_wake_from_d3(pdev, true);
  4120. pci_set_power_state(pdev, PCI_D3hot);
  4121. }
  4122. }
  4123. static struct pci_driver rtl8169_pci_driver = {
  4124. .name = MODULENAME,
  4125. .id_table = rtl8169_pci_tbl,
  4126. .probe = rtl8169_init_one,
  4127. .remove = __devexit_p(rtl8169_remove_one),
  4128. .shutdown = rtl_shutdown,
  4129. .driver.pm = RTL8169_PM_OPS,
  4130. };
  4131. static int __init rtl8169_init_module(void)
  4132. {
  4133. return pci_register_driver(&rtl8169_pci_driver);
  4134. }
  4135. static void __exit rtl8169_cleanup_module(void)
  4136. {
  4137. pci_unregister_driver(&rtl8169_pci_driver);
  4138. }
  4139. module_init(rtl8169_init_module);
  4140. module_exit(rtl8169_cleanup_module);