r8169.c 131 KB

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