r8169.c 131 KB

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