r8169.c 131 KB

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