r8169.c 129 KB

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