r8169.c 124 KB

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