r8169.c 128 KB

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