r8169.c 130 KB

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