r8169.c 130 KB

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