r8169.c 124 KB

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