r8169.c 130 KB

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