r8169.c 130 KB

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