tg3.c 419 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166
  1. /*
  2. * tg3.c: Broadcom Tigon3 ethernet driver.
  3. *
  4. * Copyright (C) 2001, 2002, 2003, 2004 David S. Miller (davem@redhat.com)
  5. * Copyright (C) 2001, 2002, 2003 Jeff Garzik (jgarzik@pobox.com)
  6. * Copyright (C) 2004 Sun Microsystems Inc.
  7. * Copyright (C) 2005-2012 Broadcom Corporation.
  8. *
  9. * Firmware is:
  10. * Derived from proprietary unpublished source code,
  11. * Copyright (C) 2000-2003 Broadcom Corporation.
  12. *
  13. * Permission is hereby granted for the distribution of this firmware
  14. * data in hexadecimal or equivalent format, provided this copyright
  15. * notice is accompanying it.
  16. */
  17. #include <linux/module.h>
  18. #include <linux/moduleparam.h>
  19. #include <linux/stringify.h>
  20. #include <linux/kernel.h>
  21. #include <linux/types.h>
  22. #include <linux/compiler.h>
  23. #include <linux/slab.h>
  24. #include <linux/delay.h>
  25. #include <linux/in.h>
  26. #include <linux/init.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/ioport.h>
  29. #include <linux/pci.h>
  30. #include <linux/netdevice.h>
  31. #include <linux/etherdevice.h>
  32. #include <linux/skbuff.h>
  33. #include <linux/ethtool.h>
  34. #include <linux/mdio.h>
  35. #include <linux/mii.h>
  36. #include <linux/phy.h>
  37. #include <linux/brcmphy.h>
  38. #include <linux/if_vlan.h>
  39. #include <linux/ip.h>
  40. #include <linux/tcp.h>
  41. #include <linux/workqueue.h>
  42. #include <linux/prefetch.h>
  43. #include <linux/dma-mapping.h>
  44. #include <linux/firmware.h>
  45. #include <net/checksum.h>
  46. #include <net/ip.h>
  47. #include <linux/io.h>
  48. #include <asm/byteorder.h>
  49. #include <linux/uaccess.h>
  50. #ifdef CONFIG_SPARC
  51. #include <asm/idprom.h>
  52. #include <asm/prom.h>
  53. #endif
  54. #define BAR_0 0
  55. #define BAR_2 2
  56. #include "tg3.h"
  57. /* Functions & macros to verify TG3_FLAGS types */
  58. static inline int _tg3_flag(enum TG3_FLAGS flag, unsigned long *bits)
  59. {
  60. return test_bit(flag, bits);
  61. }
  62. static inline void _tg3_flag_set(enum TG3_FLAGS flag, unsigned long *bits)
  63. {
  64. set_bit(flag, bits);
  65. }
  66. static inline void _tg3_flag_clear(enum TG3_FLAGS flag, unsigned long *bits)
  67. {
  68. clear_bit(flag, bits);
  69. }
  70. #define tg3_flag(tp, flag) \
  71. _tg3_flag(TG3_FLAG_##flag, (tp)->tg3_flags)
  72. #define tg3_flag_set(tp, flag) \
  73. _tg3_flag_set(TG3_FLAG_##flag, (tp)->tg3_flags)
  74. #define tg3_flag_clear(tp, flag) \
  75. _tg3_flag_clear(TG3_FLAG_##flag, (tp)->tg3_flags)
  76. #define DRV_MODULE_NAME "tg3"
  77. #define TG3_MAJ_NUM 3
  78. #define TG3_MIN_NUM 123
  79. #define DRV_MODULE_VERSION \
  80. __stringify(TG3_MAJ_NUM) "." __stringify(TG3_MIN_NUM)
  81. #define DRV_MODULE_RELDATE "March 21, 2012"
  82. #define RESET_KIND_SHUTDOWN 0
  83. #define RESET_KIND_INIT 1
  84. #define RESET_KIND_SUSPEND 2
  85. #define TG3_DEF_RX_MODE 0
  86. #define TG3_DEF_TX_MODE 0
  87. #define TG3_DEF_MSG_ENABLE \
  88. (NETIF_MSG_DRV | \
  89. NETIF_MSG_PROBE | \
  90. NETIF_MSG_LINK | \
  91. NETIF_MSG_TIMER | \
  92. NETIF_MSG_IFDOWN | \
  93. NETIF_MSG_IFUP | \
  94. NETIF_MSG_RX_ERR | \
  95. NETIF_MSG_TX_ERR)
  96. #define TG3_GRC_LCLCTL_PWRSW_DELAY 100
  97. /* length of time before we decide the hardware is borked,
  98. * and dev->tx_timeout() should be called to fix the problem
  99. */
  100. #define TG3_TX_TIMEOUT (5 * HZ)
  101. /* hardware minimum and maximum for a single frame's data payload */
  102. #define TG3_MIN_MTU 60
  103. #define TG3_MAX_MTU(tp) \
  104. (tg3_flag(tp, JUMBO_CAPABLE) ? 9000 : 1500)
  105. /* These numbers seem to be hard coded in the NIC firmware somehow.
  106. * You can't change the ring sizes, but you can change where you place
  107. * them in the NIC onboard memory.
  108. */
  109. #define TG3_RX_STD_RING_SIZE(tp) \
  110. (tg3_flag(tp, LRG_PROD_RING_CAP) ? \
  111. TG3_RX_STD_MAX_SIZE_5717 : TG3_RX_STD_MAX_SIZE_5700)
  112. #define TG3_DEF_RX_RING_PENDING 200
  113. #define TG3_RX_JMB_RING_SIZE(tp) \
  114. (tg3_flag(tp, LRG_PROD_RING_CAP) ? \
  115. TG3_RX_JMB_MAX_SIZE_5717 : TG3_RX_JMB_MAX_SIZE_5700)
  116. #define TG3_DEF_RX_JUMBO_RING_PENDING 100
  117. /* Do not place this n-ring entries value into the tp struct itself,
  118. * we really want to expose these constants to GCC so that modulo et
  119. * al. operations are done with shifts and masks instead of with
  120. * hw multiply/modulo instructions. Another solution would be to
  121. * replace things like '% foo' with '& (foo - 1)'.
  122. */
  123. #define TG3_TX_RING_SIZE 512
  124. #define TG3_DEF_TX_RING_PENDING (TG3_TX_RING_SIZE - 1)
  125. #define TG3_RX_STD_RING_BYTES(tp) \
  126. (sizeof(struct tg3_rx_buffer_desc) * TG3_RX_STD_RING_SIZE(tp))
  127. #define TG3_RX_JMB_RING_BYTES(tp) \
  128. (sizeof(struct tg3_ext_rx_buffer_desc) * TG3_RX_JMB_RING_SIZE(tp))
  129. #define TG3_RX_RCB_RING_BYTES(tp) \
  130. (sizeof(struct tg3_rx_buffer_desc) * (tp->rx_ret_ring_mask + 1))
  131. #define TG3_TX_RING_BYTES (sizeof(struct tg3_tx_buffer_desc) * \
  132. TG3_TX_RING_SIZE)
  133. #define NEXT_TX(N) (((N) + 1) & (TG3_TX_RING_SIZE - 1))
  134. #define TG3_DMA_BYTE_ENAB 64
  135. #define TG3_RX_STD_DMA_SZ 1536
  136. #define TG3_RX_JMB_DMA_SZ 9046
  137. #define TG3_RX_DMA_TO_MAP_SZ(x) ((x) + TG3_DMA_BYTE_ENAB)
  138. #define TG3_RX_STD_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_STD_DMA_SZ)
  139. #define TG3_RX_JMB_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_JMB_DMA_SZ)
  140. #define TG3_RX_STD_BUFF_RING_SIZE(tp) \
  141. (sizeof(struct ring_info) * TG3_RX_STD_RING_SIZE(tp))
  142. #define TG3_RX_JMB_BUFF_RING_SIZE(tp) \
  143. (sizeof(struct ring_info) * TG3_RX_JMB_RING_SIZE(tp))
  144. /* Due to a hardware bug, the 5701 can only DMA to memory addresses
  145. * that are at least dword aligned when used in PCIX mode. The driver
  146. * works around this bug by double copying the packet. This workaround
  147. * is built into the normal double copy length check for efficiency.
  148. *
  149. * However, the double copy is only necessary on those architectures
  150. * where unaligned memory accesses are inefficient. For those architectures
  151. * where unaligned memory accesses incur little penalty, we can reintegrate
  152. * the 5701 in the normal rx path. Doing so saves a device structure
  153. * dereference by hardcoding the double copy threshold in place.
  154. */
  155. #define TG3_RX_COPY_THRESHOLD 256
  156. #if NET_IP_ALIGN == 0 || defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
  157. #define TG3_RX_COPY_THRESH(tp) TG3_RX_COPY_THRESHOLD
  158. #else
  159. #define TG3_RX_COPY_THRESH(tp) ((tp)->rx_copy_thresh)
  160. #endif
  161. #if (NET_IP_ALIGN != 0)
  162. #define TG3_RX_OFFSET(tp) ((tp)->rx_offset)
  163. #else
  164. #define TG3_RX_OFFSET(tp) (NET_SKB_PAD)
  165. #endif
  166. /* minimum number of free TX descriptors required to wake up TX process */
  167. #define TG3_TX_WAKEUP_THRESH(tnapi) ((tnapi)->tx_pending / 4)
  168. #define TG3_TX_BD_DMA_MAX_2K 2048
  169. #define TG3_TX_BD_DMA_MAX_4K 4096
  170. #define TG3_RAW_IP_ALIGN 2
  171. #define TG3_FW_UPDATE_TIMEOUT_SEC 5
  172. #define TG3_FW_UPDATE_FREQ_SEC (TG3_FW_UPDATE_TIMEOUT_SEC / 2)
  173. #define FIRMWARE_TG3 "tigon/tg3.bin"
  174. #define FIRMWARE_TG3TSO "tigon/tg3_tso.bin"
  175. #define FIRMWARE_TG3TSO5 "tigon/tg3_tso5.bin"
  176. static char version[] __devinitdata =
  177. DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")";
  178. MODULE_AUTHOR("David S. Miller (davem@redhat.com) and Jeff Garzik (jgarzik@pobox.com)");
  179. MODULE_DESCRIPTION("Broadcom Tigon3 ethernet driver");
  180. MODULE_LICENSE("GPL");
  181. MODULE_VERSION(DRV_MODULE_VERSION);
  182. MODULE_FIRMWARE(FIRMWARE_TG3);
  183. MODULE_FIRMWARE(FIRMWARE_TG3TSO);
  184. MODULE_FIRMWARE(FIRMWARE_TG3TSO5);
  185. static int tg3_debug = -1; /* -1 == use TG3_DEF_MSG_ENABLE as value */
  186. module_param(tg3_debug, int, 0);
  187. MODULE_PARM_DESC(tg3_debug, "Tigon3 bitmapped debugging message enable value");
  188. static DEFINE_PCI_DEVICE_TABLE(tg3_pci_tbl) = {
  189. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5700)},
  190. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5701)},
  191. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702)},
  192. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703)},
  193. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704)},
  194. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702FE)},
  195. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705)},
  196. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705_2)},
  197. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M)},
  198. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M_2)},
  199. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702X)},
  200. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703X)},
  201. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S)},
  202. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702A3)},
  203. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703A3)},
  204. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5782)},
  205. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5788)},
  206. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5789)},
  207. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901)},
  208. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901_2)},
  209. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S_2)},
  210. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705F)},
  211. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5721)},
  212. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5722)},
  213. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751)},
  214. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751M)},
  215. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751F)},
  216. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752)},
  217. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752M)},
  218. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753)},
  219. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753M)},
  220. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753F)},
  221. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754)},
  222. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754M)},
  223. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755)},
  224. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755M)},
  225. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5756)},
  226. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5786)},
  227. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787)},
  228. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787M)},
  229. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787F)},
  230. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714)},
  231. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714S)},
  232. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715)},
  233. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715S)},
  234. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780)},
  235. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780S)},
  236. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5781)},
  237. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906)},
  238. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906M)},
  239. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5784)},
  240. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5764)},
  241. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5723)},
  242. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761)},
  243. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761E)},
  244. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761S)},
  245. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761SE)},
  246. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_G)},
  247. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_F)},
  248. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780)},
  249. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57760)},
  250. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57790)},
  251. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57788)},
  252. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717)},
  253. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5718)},
  254. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57781)},
  255. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57785)},
  256. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57761)},
  257. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57765)},
  258. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57791)},
  259. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57795)},
  260. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5719)},
  261. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5720)},
  262. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57762)},
  263. {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9DXX)},
  264. {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9MXX)},
  265. {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1000)},
  266. {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1001)},
  267. {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1003)},
  268. {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC9100)},
  269. {PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_TIGON3)},
  270. {PCI_DEVICE(0x10cf, 0x11a2)}, /* Fujitsu 1000base-SX with BCM5703SKHB */
  271. {}
  272. };
  273. MODULE_DEVICE_TABLE(pci, tg3_pci_tbl);
  274. static const struct {
  275. const char string[ETH_GSTRING_LEN];
  276. } ethtool_stats_keys[] = {
  277. { "rx_octets" },
  278. { "rx_fragments" },
  279. { "rx_ucast_packets" },
  280. { "rx_mcast_packets" },
  281. { "rx_bcast_packets" },
  282. { "rx_fcs_errors" },
  283. { "rx_align_errors" },
  284. { "rx_xon_pause_rcvd" },
  285. { "rx_xoff_pause_rcvd" },
  286. { "rx_mac_ctrl_rcvd" },
  287. { "rx_xoff_entered" },
  288. { "rx_frame_too_long_errors" },
  289. { "rx_jabbers" },
  290. { "rx_undersize_packets" },
  291. { "rx_in_length_errors" },
  292. { "rx_out_length_errors" },
  293. { "rx_64_or_less_octet_packets" },
  294. { "rx_65_to_127_octet_packets" },
  295. { "rx_128_to_255_octet_packets" },
  296. { "rx_256_to_511_octet_packets" },
  297. { "rx_512_to_1023_octet_packets" },
  298. { "rx_1024_to_1522_octet_packets" },
  299. { "rx_1523_to_2047_octet_packets" },
  300. { "rx_2048_to_4095_octet_packets" },
  301. { "rx_4096_to_8191_octet_packets" },
  302. { "rx_8192_to_9022_octet_packets" },
  303. { "tx_octets" },
  304. { "tx_collisions" },
  305. { "tx_xon_sent" },
  306. { "tx_xoff_sent" },
  307. { "tx_flow_control" },
  308. { "tx_mac_errors" },
  309. { "tx_single_collisions" },
  310. { "tx_mult_collisions" },
  311. { "tx_deferred" },
  312. { "tx_excessive_collisions" },
  313. { "tx_late_collisions" },
  314. { "tx_collide_2times" },
  315. { "tx_collide_3times" },
  316. { "tx_collide_4times" },
  317. { "tx_collide_5times" },
  318. { "tx_collide_6times" },
  319. { "tx_collide_7times" },
  320. { "tx_collide_8times" },
  321. { "tx_collide_9times" },
  322. { "tx_collide_10times" },
  323. { "tx_collide_11times" },
  324. { "tx_collide_12times" },
  325. { "tx_collide_13times" },
  326. { "tx_collide_14times" },
  327. { "tx_collide_15times" },
  328. { "tx_ucast_packets" },
  329. { "tx_mcast_packets" },
  330. { "tx_bcast_packets" },
  331. { "tx_carrier_sense_errors" },
  332. { "tx_discards" },
  333. { "tx_errors" },
  334. { "dma_writeq_full" },
  335. { "dma_write_prioq_full" },
  336. { "rxbds_empty" },
  337. { "rx_discards" },
  338. { "rx_errors" },
  339. { "rx_threshold_hit" },
  340. { "dma_readq_full" },
  341. { "dma_read_prioq_full" },
  342. { "tx_comp_queue_full" },
  343. { "ring_set_send_prod_index" },
  344. { "ring_status_update" },
  345. { "nic_irqs" },
  346. { "nic_avoided_irqs" },
  347. { "nic_tx_threshold_hit" },
  348. { "mbuf_lwm_thresh_hit" },
  349. };
  350. #define TG3_NUM_STATS ARRAY_SIZE(ethtool_stats_keys)
  351. static const struct {
  352. const char string[ETH_GSTRING_LEN];
  353. } ethtool_test_keys[] = {
  354. { "nvram test (online) " },
  355. { "link test (online) " },
  356. { "register test (offline)" },
  357. { "memory test (offline)" },
  358. { "mac loopback test (offline)" },
  359. { "phy loopback test (offline)" },
  360. { "ext loopback test (offline)" },
  361. { "interrupt test (offline)" },
  362. };
  363. #define TG3_NUM_TEST ARRAY_SIZE(ethtool_test_keys)
  364. static void tg3_write32(struct tg3 *tp, u32 off, u32 val)
  365. {
  366. writel(val, tp->regs + off);
  367. }
  368. static u32 tg3_read32(struct tg3 *tp, u32 off)
  369. {
  370. return readl(tp->regs + off);
  371. }
  372. static void tg3_ape_write32(struct tg3 *tp, u32 off, u32 val)
  373. {
  374. writel(val, tp->aperegs + off);
  375. }
  376. static u32 tg3_ape_read32(struct tg3 *tp, u32 off)
  377. {
  378. return readl(tp->aperegs + off);
  379. }
  380. static void tg3_write_indirect_reg32(struct tg3 *tp, u32 off, u32 val)
  381. {
  382. unsigned long flags;
  383. spin_lock_irqsave(&tp->indirect_lock, flags);
  384. pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
  385. pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
  386. spin_unlock_irqrestore(&tp->indirect_lock, flags);
  387. }
  388. static void tg3_write_flush_reg32(struct tg3 *tp, u32 off, u32 val)
  389. {
  390. writel(val, tp->regs + off);
  391. readl(tp->regs + off);
  392. }
  393. static u32 tg3_read_indirect_reg32(struct tg3 *tp, u32 off)
  394. {
  395. unsigned long flags;
  396. u32 val;
  397. spin_lock_irqsave(&tp->indirect_lock, flags);
  398. pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
  399. pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
  400. spin_unlock_irqrestore(&tp->indirect_lock, flags);
  401. return val;
  402. }
  403. static void tg3_write_indirect_mbox(struct tg3 *tp, u32 off, u32 val)
  404. {
  405. unsigned long flags;
  406. if (off == (MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW)) {
  407. pci_write_config_dword(tp->pdev, TG3PCI_RCV_RET_RING_CON_IDX +
  408. TG3_64BIT_REG_LOW, val);
  409. return;
  410. }
  411. if (off == TG3_RX_STD_PROD_IDX_REG) {
  412. pci_write_config_dword(tp->pdev, TG3PCI_STD_RING_PROD_IDX +
  413. TG3_64BIT_REG_LOW, val);
  414. return;
  415. }
  416. spin_lock_irqsave(&tp->indirect_lock, flags);
  417. pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
  418. pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
  419. spin_unlock_irqrestore(&tp->indirect_lock, flags);
  420. /* In indirect mode when disabling interrupts, we also need
  421. * to clear the interrupt bit in the GRC local ctrl register.
  422. */
  423. if ((off == (MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW)) &&
  424. (val == 0x1)) {
  425. pci_write_config_dword(tp->pdev, TG3PCI_MISC_LOCAL_CTRL,
  426. tp->grc_local_ctrl|GRC_LCLCTRL_CLEARINT);
  427. }
  428. }
  429. static u32 tg3_read_indirect_mbox(struct tg3 *tp, u32 off)
  430. {
  431. unsigned long flags;
  432. u32 val;
  433. spin_lock_irqsave(&tp->indirect_lock, flags);
  434. pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
  435. pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
  436. spin_unlock_irqrestore(&tp->indirect_lock, flags);
  437. return val;
  438. }
  439. /* usec_wait specifies the wait time in usec when writing to certain registers
  440. * where it is unsafe to read back the register without some delay.
  441. * GRC_LOCAL_CTRL is one example if the GPIOs are toggled to switch power.
  442. * TG3PCI_CLOCK_CTRL is another example if the clock frequencies are changed.
  443. */
  444. static void _tw32_flush(struct tg3 *tp, u32 off, u32 val, u32 usec_wait)
  445. {
  446. if (tg3_flag(tp, PCIX_TARGET_HWBUG) || tg3_flag(tp, ICH_WORKAROUND))
  447. /* Non-posted methods */
  448. tp->write32(tp, off, val);
  449. else {
  450. /* Posted method */
  451. tg3_write32(tp, off, val);
  452. if (usec_wait)
  453. udelay(usec_wait);
  454. tp->read32(tp, off);
  455. }
  456. /* Wait again after the read for the posted method to guarantee that
  457. * the wait time is met.
  458. */
  459. if (usec_wait)
  460. udelay(usec_wait);
  461. }
  462. static inline void tw32_mailbox_flush(struct tg3 *tp, u32 off, u32 val)
  463. {
  464. tp->write32_mbox(tp, off, val);
  465. if (!tg3_flag(tp, MBOX_WRITE_REORDER) && !tg3_flag(tp, ICH_WORKAROUND))
  466. tp->read32_mbox(tp, off);
  467. }
  468. static void tg3_write32_tx_mbox(struct tg3 *tp, u32 off, u32 val)
  469. {
  470. void __iomem *mbox = tp->regs + off;
  471. writel(val, mbox);
  472. if (tg3_flag(tp, TXD_MBOX_HWBUG))
  473. writel(val, mbox);
  474. if (tg3_flag(tp, MBOX_WRITE_REORDER))
  475. readl(mbox);
  476. }
  477. static u32 tg3_read32_mbox_5906(struct tg3 *tp, u32 off)
  478. {
  479. return readl(tp->regs + off + GRCMBOX_BASE);
  480. }
  481. static void tg3_write32_mbox_5906(struct tg3 *tp, u32 off, u32 val)
  482. {
  483. writel(val, tp->regs + off + GRCMBOX_BASE);
  484. }
  485. #define tw32_mailbox(reg, val) tp->write32_mbox(tp, reg, val)
  486. #define tw32_mailbox_f(reg, val) tw32_mailbox_flush(tp, (reg), (val))
  487. #define tw32_rx_mbox(reg, val) tp->write32_rx_mbox(tp, reg, val)
  488. #define tw32_tx_mbox(reg, val) tp->write32_tx_mbox(tp, reg, val)
  489. #define tr32_mailbox(reg) tp->read32_mbox(tp, reg)
  490. #define tw32(reg, val) tp->write32(tp, reg, val)
  491. #define tw32_f(reg, val) _tw32_flush(tp, (reg), (val), 0)
  492. #define tw32_wait_f(reg, val, us) _tw32_flush(tp, (reg), (val), (us))
  493. #define tr32(reg) tp->read32(tp, reg)
  494. static void tg3_write_mem(struct tg3 *tp, u32 off, u32 val)
  495. {
  496. unsigned long flags;
  497. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 &&
  498. (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC))
  499. return;
  500. spin_lock_irqsave(&tp->indirect_lock, flags);
  501. if (tg3_flag(tp, SRAM_USE_CONFIG)) {
  502. pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
  503. pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
  504. /* Always leave this as zero. */
  505. pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
  506. } else {
  507. tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
  508. tw32_f(TG3PCI_MEM_WIN_DATA, val);
  509. /* Always leave this as zero. */
  510. tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
  511. }
  512. spin_unlock_irqrestore(&tp->indirect_lock, flags);
  513. }
  514. static void tg3_read_mem(struct tg3 *tp, u32 off, u32 *val)
  515. {
  516. unsigned long flags;
  517. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 &&
  518. (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC)) {
  519. *val = 0;
  520. return;
  521. }
  522. spin_lock_irqsave(&tp->indirect_lock, flags);
  523. if (tg3_flag(tp, SRAM_USE_CONFIG)) {
  524. pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
  525. pci_read_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
  526. /* Always leave this as zero. */
  527. pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
  528. } else {
  529. tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
  530. *val = tr32(TG3PCI_MEM_WIN_DATA);
  531. /* Always leave this as zero. */
  532. tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
  533. }
  534. spin_unlock_irqrestore(&tp->indirect_lock, flags);
  535. }
  536. static void tg3_ape_lock_init(struct tg3 *tp)
  537. {
  538. int i;
  539. u32 regbase, bit;
  540. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
  541. regbase = TG3_APE_LOCK_GRANT;
  542. else
  543. regbase = TG3_APE_PER_LOCK_GRANT;
  544. /* Make sure the driver hasn't any stale locks. */
  545. for (i = TG3_APE_LOCK_PHY0; i <= TG3_APE_LOCK_GPIO; i++) {
  546. switch (i) {
  547. case TG3_APE_LOCK_PHY0:
  548. case TG3_APE_LOCK_PHY1:
  549. case TG3_APE_LOCK_PHY2:
  550. case TG3_APE_LOCK_PHY3:
  551. bit = APE_LOCK_GRANT_DRIVER;
  552. break;
  553. default:
  554. if (!tp->pci_fn)
  555. bit = APE_LOCK_GRANT_DRIVER;
  556. else
  557. bit = 1 << tp->pci_fn;
  558. }
  559. tg3_ape_write32(tp, regbase + 4 * i, bit);
  560. }
  561. }
  562. static int tg3_ape_lock(struct tg3 *tp, int locknum)
  563. {
  564. int i, off;
  565. int ret = 0;
  566. u32 status, req, gnt, bit;
  567. if (!tg3_flag(tp, ENABLE_APE))
  568. return 0;
  569. switch (locknum) {
  570. case TG3_APE_LOCK_GPIO:
  571. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
  572. return 0;
  573. case TG3_APE_LOCK_GRC:
  574. case TG3_APE_LOCK_MEM:
  575. if (!tp->pci_fn)
  576. bit = APE_LOCK_REQ_DRIVER;
  577. else
  578. bit = 1 << tp->pci_fn;
  579. break;
  580. default:
  581. return -EINVAL;
  582. }
  583. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
  584. req = TG3_APE_LOCK_REQ;
  585. gnt = TG3_APE_LOCK_GRANT;
  586. } else {
  587. req = TG3_APE_PER_LOCK_REQ;
  588. gnt = TG3_APE_PER_LOCK_GRANT;
  589. }
  590. off = 4 * locknum;
  591. tg3_ape_write32(tp, req + off, bit);
  592. /* Wait for up to 1 millisecond to acquire lock. */
  593. for (i = 0; i < 100; i++) {
  594. status = tg3_ape_read32(tp, gnt + off);
  595. if (status == bit)
  596. break;
  597. udelay(10);
  598. }
  599. if (status != bit) {
  600. /* Revoke the lock request. */
  601. tg3_ape_write32(tp, gnt + off, bit);
  602. ret = -EBUSY;
  603. }
  604. return ret;
  605. }
  606. static void tg3_ape_unlock(struct tg3 *tp, int locknum)
  607. {
  608. u32 gnt, bit;
  609. if (!tg3_flag(tp, ENABLE_APE))
  610. return;
  611. switch (locknum) {
  612. case TG3_APE_LOCK_GPIO:
  613. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
  614. return;
  615. case TG3_APE_LOCK_GRC:
  616. case TG3_APE_LOCK_MEM:
  617. if (!tp->pci_fn)
  618. bit = APE_LOCK_GRANT_DRIVER;
  619. else
  620. bit = 1 << tp->pci_fn;
  621. break;
  622. default:
  623. return;
  624. }
  625. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
  626. gnt = TG3_APE_LOCK_GRANT;
  627. else
  628. gnt = TG3_APE_PER_LOCK_GRANT;
  629. tg3_ape_write32(tp, gnt + 4 * locknum, bit);
  630. }
  631. static void tg3_ape_send_event(struct tg3 *tp, u32 event)
  632. {
  633. int i;
  634. u32 apedata;
  635. /* NCSI does not support APE events */
  636. if (tg3_flag(tp, APE_HAS_NCSI))
  637. return;
  638. apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
  639. if (apedata != APE_SEG_SIG_MAGIC)
  640. return;
  641. apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
  642. if (!(apedata & APE_FW_STATUS_READY))
  643. return;
  644. /* Wait for up to 1 millisecond for APE to service previous event. */
  645. for (i = 0; i < 10; i++) {
  646. if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
  647. return;
  648. apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
  649. if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
  650. tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
  651. event | APE_EVENT_STATUS_EVENT_PENDING);
  652. tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
  653. if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
  654. break;
  655. udelay(100);
  656. }
  657. if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
  658. tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
  659. }
  660. static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
  661. {
  662. u32 event;
  663. u32 apedata;
  664. if (!tg3_flag(tp, ENABLE_APE))
  665. return;
  666. switch (kind) {
  667. case RESET_KIND_INIT:
  668. tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
  669. APE_HOST_SEG_SIG_MAGIC);
  670. tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
  671. APE_HOST_SEG_LEN_MAGIC);
  672. apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
  673. tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
  674. tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
  675. APE_HOST_DRIVER_ID_MAGIC(TG3_MAJ_NUM, TG3_MIN_NUM));
  676. tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
  677. APE_HOST_BEHAV_NO_PHYLOCK);
  678. tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE,
  679. TG3_APE_HOST_DRVR_STATE_START);
  680. event = APE_EVENT_STATUS_STATE_START;
  681. break;
  682. case RESET_KIND_SHUTDOWN:
  683. /* With the interface we are currently using,
  684. * APE does not track driver state. Wiping
  685. * out the HOST SEGMENT SIGNATURE forces
  686. * the APE to assume OS absent status.
  687. */
  688. tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
  689. if (device_may_wakeup(&tp->pdev->dev) &&
  690. tg3_flag(tp, WOL_ENABLE)) {
  691. tg3_ape_write32(tp, TG3_APE_HOST_WOL_SPEED,
  692. TG3_APE_HOST_WOL_SPEED_AUTO);
  693. apedata = TG3_APE_HOST_DRVR_STATE_WOL;
  694. } else
  695. apedata = TG3_APE_HOST_DRVR_STATE_UNLOAD;
  696. tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE, apedata);
  697. event = APE_EVENT_STATUS_STATE_UNLOAD;
  698. break;
  699. case RESET_KIND_SUSPEND:
  700. event = APE_EVENT_STATUS_STATE_SUSPEND;
  701. break;
  702. default:
  703. return;
  704. }
  705. event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
  706. tg3_ape_send_event(tp, event);
  707. }
  708. static void tg3_disable_ints(struct tg3 *tp)
  709. {
  710. int i;
  711. tw32(TG3PCI_MISC_HOST_CTRL,
  712. (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
  713. for (i = 0; i < tp->irq_max; i++)
  714. tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
  715. }
  716. static void tg3_enable_ints(struct tg3 *tp)
  717. {
  718. int i;
  719. tp->irq_sync = 0;
  720. wmb();
  721. tw32(TG3PCI_MISC_HOST_CTRL,
  722. (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
  723. tp->coal_now = tp->coalesce_mode | HOSTCC_MODE_ENABLE;
  724. for (i = 0; i < tp->irq_cnt; i++) {
  725. struct tg3_napi *tnapi = &tp->napi[i];
  726. tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
  727. if (tg3_flag(tp, 1SHOT_MSI))
  728. tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
  729. tp->coal_now |= tnapi->coal_now;
  730. }
  731. /* Force an initial interrupt */
  732. if (!tg3_flag(tp, TAGGED_STATUS) &&
  733. (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
  734. tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
  735. else
  736. tw32(HOSTCC_MODE, tp->coal_now);
  737. tp->coal_now &= ~(tp->napi[0].coal_now | tp->napi[1].coal_now);
  738. }
  739. static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
  740. {
  741. struct tg3 *tp = tnapi->tp;
  742. struct tg3_hw_status *sblk = tnapi->hw_status;
  743. unsigned int work_exists = 0;
  744. /* check for phy events */
  745. if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
  746. if (sblk->status & SD_STATUS_LINK_CHG)
  747. work_exists = 1;
  748. }
  749. /* check for TX work to do */
  750. if (sblk->idx[0].tx_consumer != tnapi->tx_cons)
  751. work_exists = 1;
  752. /* check for RX work to do */
  753. if (tnapi->rx_rcb_prod_idx &&
  754. *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
  755. work_exists = 1;
  756. return work_exists;
  757. }
  758. /* tg3_int_reenable
  759. * similar to tg3_enable_ints, but it accurately determines whether there
  760. * is new work pending and can return without flushing the PIO write
  761. * which reenables interrupts
  762. */
  763. static void tg3_int_reenable(struct tg3_napi *tnapi)
  764. {
  765. struct tg3 *tp = tnapi->tp;
  766. tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
  767. mmiowb();
  768. /* When doing tagged status, this work check is unnecessary.
  769. * The last_tag we write above tells the chip which piece of
  770. * work we've completed.
  771. */
  772. if (!tg3_flag(tp, TAGGED_STATUS) && tg3_has_work(tnapi))
  773. tw32(HOSTCC_MODE, tp->coalesce_mode |
  774. HOSTCC_MODE_ENABLE | tnapi->coal_now);
  775. }
  776. static void tg3_switch_clocks(struct tg3 *tp)
  777. {
  778. u32 clock_ctrl;
  779. u32 orig_clock_ctrl;
  780. if (tg3_flag(tp, CPMU_PRESENT) || tg3_flag(tp, 5780_CLASS))
  781. return;
  782. clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
  783. orig_clock_ctrl = clock_ctrl;
  784. clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
  785. CLOCK_CTRL_CLKRUN_OENABLE |
  786. 0x1f);
  787. tp->pci_clock_ctrl = clock_ctrl;
  788. if (tg3_flag(tp, 5705_PLUS)) {
  789. if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
  790. tw32_wait_f(TG3PCI_CLOCK_CTRL,
  791. clock_ctrl | CLOCK_CTRL_625_CORE, 40);
  792. }
  793. } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
  794. tw32_wait_f(TG3PCI_CLOCK_CTRL,
  795. clock_ctrl |
  796. (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
  797. 40);
  798. tw32_wait_f(TG3PCI_CLOCK_CTRL,
  799. clock_ctrl | (CLOCK_CTRL_ALTCLK),
  800. 40);
  801. }
  802. tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
  803. }
  804. #define PHY_BUSY_LOOPS 5000
  805. static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
  806. {
  807. u32 frame_val;
  808. unsigned int loops;
  809. int ret;
  810. if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
  811. tw32_f(MAC_MI_MODE,
  812. (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
  813. udelay(80);
  814. }
  815. *val = 0x0;
  816. frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
  817. MI_COM_PHY_ADDR_MASK);
  818. frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
  819. MI_COM_REG_ADDR_MASK);
  820. frame_val |= (MI_COM_CMD_READ | MI_COM_START);
  821. tw32_f(MAC_MI_COM, frame_val);
  822. loops = PHY_BUSY_LOOPS;
  823. while (loops != 0) {
  824. udelay(10);
  825. frame_val = tr32(MAC_MI_COM);
  826. if ((frame_val & MI_COM_BUSY) == 0) {
  827. udelay(5);
  828. frame_val = tr32(MAC_MI_COM);
  829. break;
  830. }
  831. loops -= 1;
  832. }
  833. ret = -EBUSY;
  834. if (loops != 0) {
  835. *val = frame_val & MI_COM_DATA_MASK;
  836. ret = 0;
  837. }
  838. if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
  839. tw32_f(MAC_MI_MODE, tp->mi_mode);
  840. udelay(80);
  841. }
  842. return ret;
  843. }
  844. static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
  845. {
  846. u32 frame_val;
  847. unsigned int loops;
  848. int ret;
  849. if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
  850. (reg == MII_CTRL1000 || reg == MII_TG3_AUX_CTRL))
  851. return 0;
  852. if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
  853. tw32_f(MAC_MI_MODE,
  854. (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
  855. udelay(80);
  856. }
  857. frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
  858. MI_COM_PHY_ADDR_MASK);
  859. frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
  860. MI_COM_REG_ADDR_MASK);
  861. frame_val |= (val & MI_COM_DATA_MASK);
  862. frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
  863. tw32_f(MAC_MI_COM, frame_val);
  864. loops = PHY_BUSY_LOOPS;
  865. while (loops != 0) {
  866. udelay(10);
  867. frame_val = tr32(MAC_MI_COM);
  868. if ((frame_val & MI_COM_BUSY) == 0) {
  869. udelay(5);
  870. frame_val = tr32(MAC_MI_COM);
  871. break;
  872. }
  873. loops -= 1;
  874. }
  875. ret = -EBUSY;
  876. if (loops != 0)
  877. ret = 0;
  878. if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
  879. tw32_f(MAC_MI_MODE, tp->mi_mode);
  880. udelay(80);
  881. }
  882. return ret;
  883. }
  884. static int tg3_phy_cl45_write(struct tg3 *tp, u32 devad, u32 addr, u32 val)
  885. {
  886. int err;
  887. err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
  888. if (err)
  889. goto done;
  890. err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
  891. if (err)
  892. goto done;
  893. err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
  894. MII_TG3_MMD_CTRL_DATA_NOINC | devad);
  895. if (err)
  896. goto done;
  897. err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, val);
  898. done:
  899. return err;
  900. }
  901. static int tg3_phy_cl45_read(struct tg3 *tp, u32 devad, u32 addr, u32 *val)
  902. {
  903. int err;
  904. err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
  905. if (err)
  906. goto done;
  907. err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
  908. if (err)
  909. goto done;
  910. err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
  911. MII_TG3_MMD_CTRL_DATA_NOINC | devad);
  912. if (err)
  913. goto done;
  914. err = tg3_readphy(tp, MII_TG3_MMD_ADDRESS, val);
  915. done:
  916. return err;
  917. }
  918. static int tg3_phydsp_read(struct tg3 *tp, u32 reg, u32 *val)
  919. {
  920. int err;
  921. err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
  922. if (!err)
  923. err = tg3_readphy(tp, MII_TG3_DSP_RW_PORT, val);
  924. return err;
  925. }
  926. static int tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
  927. {
  928. int err;
  929. err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
  930. if (!err)
  931. err = tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
  932. return err;
  933. }
  934. static int tg3_phy_auxctl_read(struct tg3 *tp, int reg, u32 *val)
  935. {
  936. int err;
  937. err = tg3_writephy(tp, MII_TG3_AUX_CTRL,
  938. (reg << MII_TG3_AUXCTL_MISC_RDSEL_SHIFT) |
  939. MII_TG3_AUXCTL_SHDWSEL_MISC);
  940. if (!err)
  941. err = tg3_readphy(tp, MII_TG3_AUX_CTRL, val);
  942. return err;
  943. }
  944. static int tg3_phy_auxctl_write(struct tg3 *tp, int reg, u32 set)
  945. {
  946. if (reg == MII_TG3_AUXCTL_SHDWSEL_MISC)
  947. set |= MII_TG3_AUXCTL_MISC_WREN;
  948. return tg3_writephy(tp, MII_TG3_AUX_CTRL, set | reg);
  949. }
  950. #define TG3_PHY_AUXCTL_SMDSP_ENABLE(tp) \
  951. tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL, \
  952. MII_TG3_AUXCTL_ACTL_SMDSP_ENA | \
  953. MII_TG3_AUXCTL_ACTL_TX_6DB)
  954. #define TG3_PHY_AUXCTL_SMDSP_DISABLE(tp) \
  955. tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL, \
  956. MII_TG3_AUXCTL_ACTL_TX_6DB);
  957. static int tg3_bmcr_reset(struct tg3 *tp)
  958. {
  959. u32 phy_control;
  960. int limit, err;
  961. /* OK, reset it, and poll the BMCR_RESET bit until it
  962. * clears or we time out.
  963. */
  964. phy_control = BMCR_RESET;
  965. err = tg3_writephy(tp, MII_BMCR, phy_control);
  966. if (err != 0)
  967. return -EBUSY;
  968. limit = 5000;
  969. while (limit--) {
  970. err = tg3_readphy(tp, MII_BMCR, &phy_control);
  971. if (err != 0)
  972. return -EBUSY;
  973. if ((phy_control & BMCR_RESET) == 0) {
  974. udelay(40);
  975. break;
  976. }
  977. udelay(10);
  978. }
  979. if (limit < 0)
  980. return -EBUSY;
  981. return 0;
  982. }
  983. static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
  984. {
  985. struct tg3 *tp = bp->priv;
  986. u32 val;
  987. spin_lock_bh(&tp->lock);
  988. if (tg3_readphy(tp, reg, &val))
  989. val = -EIO;
  990. spin_unlock_bh(&tp->lock);
  991. return val;
  992. }
  993. static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
  994. {
  995. struct tg3 *tp = bp->priv;
  996. u32 ret = 0;
  997. spin_lock_bh(&tp->lock);
  998. if (tg3_writephy(tp, reg, val))
  999. ret = -EIO;
  1000. spin_unlock_bh(&tp->lock);
  1001. return ret;
  1002. }
  1003. static int tg3_mdio_reset(struct mii_bus *bp)
  1004. {
  1005. return 0;
  1006. }
  1007. static void tg3_mdio_config_5785(struct tg3 *tp)
  1008. {
  1009. u32 val;
  1010. struct phy_device *phydev;
  1011. phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
  1012. switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
  1013. case PHY_ID_BCM50610:
  1014. case PHY_ID_BCM50610M:
  1015. val = MAC_PHYCFG2_50610_LED_MODES;
  1016. break;
  1017. case PHY_ID_BCMAC131:
  1018. val = MAC_PHYCFG2_AC131_LED_MODES;
  1019. break;
  1020. case PHY_ID_RTL8211C:
  1021. val = MAC_PHYCFG2_RTL8211C_LED_MODES;
  1022. break;
  1023. case PHY_ID_RTL8201E:
  1024. val = MAC_PHYCFG2_RTL8201E_LED_MODES;
  1025. break;
  1026. default:
  1027. return;
  1028. }
  1029. if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
  1030. tw32(MAC_PHYCFG2, val);
  1031. val = tr32(MAC_PHYCFG1);
  1032. val &= ~(MAC_PHYCFG1_RGMII_INT |
  1033. MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
  1034. val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
  1035. tw32(MAC_PHYCFG1, val);
  1036. return;
  1037. }
  1038. if (!tg3_flag(tp, RGMII_INBAND_DISABLE))
  1039. val |= MAC_PHYCFG2_EMODE_MASK_MASK |
  1040. MAC_PHYCFG2_FMODE_MASK_MASK |
  1041. MAC_PHYCFG2_GMODE_MASK_MASK |
  1042. MAC_PHYCFG2_ACT_MASK_MASK |
  1043. MAC_PHYCFG2_QUAL_MASK_MASK |
  1044. MAC_PHYCFG2_INBAND_ENABLE;
  1045. tw32(MAC_PHYCFG2, val);
  1046. val = tr32(MAC_PHYCFG1);
  1047. val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
  1048. MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
  1049. if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
  1050. if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
  1051. val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
  1052. if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
  1053. val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
  1054. }
  1055. val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
  1056. MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
  1057. tw32(MAC_PHYCFG1, val);
  1058. val = tr32(MAC_EXT_RGMII_MODE);
  1059. val &= ~(MAC_RGMII_MODE_RX_INT_B |
  1060. MAC_RGMII_MODE_RX_QUALITY |
  1061. MAC_RGMII_MODE_RX_ACTIVITY |
  1062. MAC_RGMII_MODE_RX_ENG_DET |
  1063. MAC_RGMII_MODE_TX_ENABLE |
  1064. MAC_RGMII_MODE_TX_LOWPWR |
  1065. MAC_RGMII_MODE_TX_RESET);
  1066. if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
  1067. if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
  1068. val |= MAC_RGMII_MODE_RX_INT_B |
  1069. MAC_RGMII_MODE_RX_QUALITY |
  1070. MAC_RGMII_MODE_RX_ACTIVITY |
  1071. MAC_RGMII_MODE_RX_ENG_DET;
  1072. if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
  1073. val |= MAC_RGMII_MODE_TX_ENABLE |
  1074. MAC_RGMII_MODE_TX_LOWPWR |
  1075. MAC_RGMII_MODE_TX_RESET;
  1076. }
  1077. tw32(MAC_EXT_RGMII_MODE, val);
  1078. }
  1079. static void tg3_mdio_start(struct tg3 *tp)
  1080. {
  1081. tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
  1082. tw32_f(MAC_MI_MODE, tp->mi_mode);
  1083. udelay(80);
  1084. if (tg3_flag(tp, MDIOBUS_INITED) &&
  1085. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
  1086. tg3_mdio_config_5785(tp);
  1087. }
  1088. static int tg3_mdio_init(struct tg3 *tp)
  1089. {
  1090. int i;
  1091. u32 reg;
  1092. struct phy_device *phydev;
  1093. if (tg3_flag(tp, 5717_PLUS)) {
  1094. u32 is_serdes;
  1095. tp->phy_addr = tp->pci_fn + 1;
  1096. if (tp->pci_chip_rev_id != CHIPREV_ID_5717_A0)
  1097. is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
  1098. else
  1099. is_serdes = tr32(TG3_CPMU_PHY_STRAP) &
  1100. TG3_CPMU_PHY_STRAP_IS_SERDES;
  1101. if (is_serdes)
  1102. tp->phy_addr += 7;
  1103. } else
  1104. tp->phy_addr = TG3_PHY_MII_ADDR;
  1105. tg3_mdio_start(tp);
  1106. if (!tg3_flag(tp, USE_PHYLIB) || tg3_flag(tp, MDIOBUS_INITED))
  1107. return 0;
  1108. tp->mdio_bus = mdiobus_alloc();
  1109. if (tp->mdio_bus == NULL)
  1110. return -ENOMEM;
  1111. tp->mdio_bus->name = "tg3 mdio bus";
  1112. snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
  1113. (tp->pdev->bus->number << 8) | tp->pdev->devfn);
  1114. tp->mdio_bus->priv = tp;
  1115. tp->mdio_bus->parent = &tp->pdev->dev;
  1116. tp->mdio_bus->read = &tg3_mdio_read;
  1117. tp->mdio_bus->write = &tg3_mdio_write;
  1118. tp->mdio_bus->reset = &tg3_mdio_reset;
  1119. tp->mdio_bus->phy_mask = ~(1 << TG3_PHY_MII_ADDR);
  1120. tp->mdio_bus->irq = &tp->mdio_irq[0];
  1121. for (i = 0; i < PHY_MAX_ADDR; i++)
  1122. tp->mdio_bus->irq[i] = PHY_POLL;
  1123. /* The bus registration will look for all the PHYs on the mdio bus.
  1124. * Unfortunately, it does not ensure the PHY is powered up before
  1125. * accessing the PHY ID registers. A chip reset is the
  1126. * quickest way to bring the device back to an operational state..
  1127. */
  1128. if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
  1129. tg3_bmcr_reset(tp);
  1130. i = mdiobus_register(tp->mdio_bus);
  1131. if (i) {
  1132. dev_warn(&tp->pdev->dev, "mdiobus_reg failed (0x%x)\n", i);
  1133. mdiobus_free(tp->mdio_bus);
  1134. return i;
  1135. }
  1136. phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
  1137. if (!phydev || !phydev->drv) {
  1138. dev_warn(&tp->pdev->dev, "No PHY devices\n");
  1139. mdiobus_unregister(tp->mdio_bus);
  1140. mdiobus_free(tp->mdio_bus);
  1141. return -ENODEV;
  1142. }
  1143. switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
  1144. case PHY_ID_BCM57780:
  1145. phydev->interface = PHY_INTERFACE_MODE_GMII;
  1146. phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
  1147. break;
  1148. case PHY_ID_BCM50610:
  1149. case PHY_ID_BCM50610M:
  1150. phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
  1151. PHY_BRCM_RX_REFCLK_UNUSED |
  1152. PHY_BRCM_DIS_TXCRXC_NOENRGY |
  1153. PHY_BRCM_AUTO_PWRDWN_ENABLE;
  1154. if (tg3_flag(tp, RGMII_INBAND_DISABLE))
  1155. phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
  1156. if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
  1157. phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
  1158. if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
  1159. phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
  1160. /* fallthru */
  1161. case PHY_ID_RTL8211C:
  1162. phydev->interface = PHY_INTERFACE_MODE_RGMII;
  1163. break;
  1164. case PHY_ID_RTL8201E:
  1165. case PHY_ID_BCMAC131:
  1166. phydev->interface = PHY_INTERFACE_MODE_MII;
  1167. phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
  1168. tp->phy_flags |= TG3_PHYFLG_IS_FET;
  1169. break;
  1170. }
  1171. tg3_flag_set(tp, MDIOBUS_INITED);
  1172. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
  1173. tg3_mdio_config_5785(tp);
  1174. return 0;
  1175. }
  1176. static void tg3_mdio_fini(struct tg3 *tp)
  1177. {
  1178. if (tg3_flag(tp, MDIOBUS_INITED)) {
  1179. tg3_flag_clear(tp, MDIOBUS_INITED);
  1180. mdiobus_unregister(tp->mdio_bus);
  1181. mdiobus_free(tp->mdio_bus);
  1182. }
  1183. }
  1184. /* tp->lock is held. */
  1185. static inline void tg3_generate_fw_event(struct tg3 *tp)
  1186. {
  1187. u32 val;
  1188. val = tr32(GRC_RX_CPU_EVENT);
  1189. val |= GRC_RX_CPU_DRIVER_EVENT;
  1190. tw32_f(GRC_RX_CPU_EVENT, val);
  1191. tp->last_event_jiffies = jiffies;
  1192. }
  1193. #define TG3_FW_EVENT_TIMEOUT_USEC 2500
  1194. /* tp->lock is held. */
  1195. static void tg3_wait_for_event_ack(struct tg3 *tp)
  1196. {
  1197. int i;
  1198. unsigned int delay_cnt;
  1199. long time_remain;
  1200. /* If enough time has passed, no wait is necessary. */
  1201. time_remain = (long)(tp->last_event_jiffies + 1 +
  1202. usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
  1203. (long)jiffies;
  1204. if (time_remain < 0)
  1205. return;
  1206. /* Check if we can shorten the wait time. */
  1207. delay_cnt = jiffies_to_usecs(time_remain);
  1208. if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
  1209. delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
  1210. delay_cnt = (delay_cnt >> 3) + 1;
  1211. for (i = 0; i < delay_cnt; i++) {
  1212. if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
  1213. break;
  1214. udelay(8);
  1215. }
  1216. }
  1217. /* tp->lock is held. */
  1218. static void tg3_phy_gather_ump_data(struct tg3 *tp, u32 *data)
  1219. {
  1220. u32 reg, val;
  1221. val = 0;
  1222. if (!tg3_readphy(tp, MII_BMCR, &reg))
  1223. val = reg << 16;
  1224. if (!tg3_readphy(tp, MII_BMSR, &reg))
  1225. val |= (reg & 0xffff);
  1226. *data++ = val;
  1227. val = 0;
  1228. if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
  1229. val = reg << 16;
  1230. if (!tg3_readphy(tp, MII_LPA, &reg))
  1231. val |= (reg & 0xffff);
  1232. *data++ = val;
  1233. val = 0;
  1234. if (!(tp->phy_flags & TG3_PHYFLG_MII_SERDES)) {
  1235. if (!tg3_readphy(tp, MII_CTRL1000, &reg))
  1236. val = reg << 16;
  1237. if (!tg3_readphy(tp, MII_STAT1000, &reg))
  1238. val |= (reg & 0xffff);
  1239. }
  1240. *data++ = val;
  1241. if (!tg3_readphy(tp, MII_PHYADDR, &reg))
  1242. val = reg << 16;
  1243. else
  1244. val = 0;
  1245. *data++ = val;
  1246. }
  1247. /* tp->lock is held. */
  1248. static void tg3_ump_link_report(struct tg3 *tp)
  1249. {
  1250. u32 data[4];
  1251. if (!tg3_flag(tp, 5780_CLASS) || !tg3_flag(tp, ENABLE_ASF))
  1252. return;
  1253. tg3_phy_gather_ump_data(tp, data);
  1254. tg3_wait_for_event_ack(tp);
  1255. tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
  1256. tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
  1257. tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x0, data[0]);
  1258. tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x4, data[1]);
  1259. tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x8, data[2]);
  1260. tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0xc, data[3]);
  1261. tg3_generate_fw_event(tp);
  1262. }
  1263. /* tp->lock is held. */
  1264. static void tg3_stop_fw(struct tg3 *tp)
  1265. {
  1266. if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
  1267. /* Wait for RX cpu to ACK the previous event. */
  1268. tg3_wait_for_event_ack(tp);
  1269. tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
  1270. tg3_generate_fw_event(tp);
  1271. /* Wait for RX cpu to ACK this event. */
  1272. tg3_wait_for_event_ack(tp);
  1273. }
  1274. }
  1275. /* tp->lock is held. */
  1276. static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
  1277. {
  1278. tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
  1279. NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
  1280. if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
  1281. switch (kind) {
  1282. case RESET_KIND_INIT:
  1283. tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
  1284. DRV_STATE_START);
  1285. break;
  1286. case RESET_KIND_SHUTDOWN:
  1287. tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
  1288. DRV_STATE_UNLOAD);
  1289. break;
  1290. case RESET_KIND_SUSPEND:
  1291. tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
  1292. DRV_STATE_SUSPEND);
  1293. break;
  1294. default:
  1295. break;
  1296. }
  1297. }
  1298. if (kind == RESET_KIND_INIT ||
  1299. kind == RESET_KIND_SUSPEND)
  1300. tg3_ape_driver_state_change(tp, kind);
  1301. }
  1302. /* tp->lock is held. */
  1303. static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
  1304. {
  1305. if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
  1306. switch (kind) {
  1307. case RESET_KIND_INIT:
  1308. tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
  1309. DRV_STATE_START_DONE);
  1310. break;
  1311. case RESET_KIND_SHUTDOWN:
  1312. tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
  1313. DRV_STATE_UNLOAD_DONE);
  1314. break;
  1315. default:
  1316. break;
  1317. }
  1318. }
  1319. if (kind == RESET_KIND_SHUTDOWN)
  1320. tg3_ape_driver_state_change(tp, kind);
  1321. }
  1322. /* tp->lock is held. */
  1323. static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
  1324. {
  1325. if (tg3_flag(tp, ENABLE_ASF)) {
  1326. switch (kind) {
  1327. case RESET_KIND_INIT:
  1328. tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
  1329. DRV_STATE_START);
  1330. break;
  1331. case RESET_KIND_SHUTDOWN:
  1332. tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
  1333. DRV_STATE_UNLOAD);
  1334. break;
  1335. case RESET_KIND_SUSPEND:
  1336. tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
  1337. DRV_STATE_SUSPEND);
  1338. break;
  1339. default:
  1340. break;
  1341. }
  1342. }
  1343. }
  1344. static int tg3_poll_fw(struct tg3 *tp)
  1345. {
  1346. int i;
  1347. u32 val;
  1348. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
  1349. /* Wait up to 20ms for init done. */
  1350. for (i = 0; i < 200; i++) {
  1351. if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
  1352. return 0;
  1353. udelay(100);
  1354. }
  1355. return -ENODEV;
  1356. }
  1357. /* Wait for firmware initialization to complete. */
  1358. for (i = 0; i < 100000; i++) {
  1359. tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
  1360. if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
  1361. break;
  1362. udelay(10);
  1363. }
  1364. /* Chip might not be fitted with firmware. Some Sun onboard
  1365. * parts are configured like that. So don't signal the timeout
  1366. * of the above loop as an error, but do report the lack of
  1367. * running firmware once.
  1368. */
  1369. if (i >= 100000 && !tg3_flag(tp, NO_FWARE_REPORTED)) {
  1370. tg3_flag_set(tp, NO_FWARE_REPORTED);
  1371. netdev_info(tp->dev, "No firmware running\n");
  1372. }
  1373. if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
  1374. /* The 57765 A0 needs a little more
  1375. * time to do some important work.
  1376. */
  1377. mdelay(10);
  1378. }
  1379. return 0;
  1380. }
  1381. static void tg3_link_report(struct tg3 *tp)
  1382. {
  1383. if (!netif_carrier_ok(tp->dev)) {
  1384. netif_info(tp, link, tp->dev, "Link is down\n");
  1385. tg3_ump_link_report(tp);
  1386. } else if (netif_msg_link(tp)) {
  1387. netdev_info(tp->dev, "Link is up at %d Mbps, %s duplex\n",
  1388. (tp->link_config.active_speed == SPEED_1000 ?
  1389. 1000 :
  1390. (tp->link_config.active_speed == SPEED_100 ?
  1391. 100 : 10)),
  1392. (tp->link_config.active_duplex == DUPLEX_FULL ?
  1393. "full" : "half"));
  1394. netdev_info(tp->dev, "Flow control is %s for TX and %s for RX\n",
  1395. (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
  1396. "on" : "off",
  1397. (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
  1398. "on" : "off");
  1399. if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
  1400. netdev_info(tp->dev, "EEE is %s\n",
  1401. tp->setlpicnt ? "enabled" : "disabled");
  1402. tg3_ump_link_report(tp);
  1403. }
  1404. }
  1405. static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
  1406. {
  1407. u16 miireg;
  1408. if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
  1409. miireg = ADVERTISE_1000XPAUSE;
  1410. else if (flow_ctrl & FLOW_CTRL_TX)
  1411. miireg = ADVERTISE_1000XPSE_ASYM;
  1412. else if (flow_ctrl & FLOW_CTRL_RX)
  1413. miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
  1414. else
  1415. miireg = 0;
  1416. return miireg;
  1417. }
  1418. static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
  1419. {
  1420. u8 cap = 0;
  1421. if (lcladv & rmtadv & ADVERTISE_1000XPAUSE) {
  1422. cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
  1423. } else if (lcladv & rmtadv & ADVERTISE_1000XPSE_ASYM) {
  1424. if (lcladv & ADVERTISE_1000XPAUSE)
  1425. cap = FLOW_CTRL_RX;
  1426. if (rmtadv & ADVERTISE_1000XPAUSE)
  1427. cap = FLOW_CTRL_TX;
  1428. }
  1429. return cap;
  1430. }
  1431. static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
  1432. {
  1433. u8 autoneg;
  1434. u8 flowctrl = 0;
  1435. u32 old_rx_mode = tp->rx_mode;
  1436. u32 old_tx_mode = tp->tx_mode;
  1437. if (tg3_flag(tp, USE_PHYLIB))
  1438. autoneg = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]->autoneg;
  1439. else
  1440. autoneg = tp->link_config.autoneg;
  1441. if (autoneg == AUTONEG_ENABLE && tg3_flag(tp, PAUSE_AUTONEG)) {
  1442. if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
  1443. flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
  1444. else
  1445. flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
  1446. } else
  1447. flowctrl = tp->link_config.flowctrl;
  1448. tp->link_config.active_flowctrl = flowctrl;
  1449. if (flowctrl & FLOW_CTRL_RX)
  1450. tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
  1451. else
  1452. tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
  1453. if (old_rx_mode != tp->rx_mode)
  1454. tw32_f(MAC_RX_MODE, tp->rx_mode);
  1455. if (flowctrl & FLOW_CTRL_TX)
  1456. tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
  1457. else
  1458. tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
  1459. if (old_tx_mode != tp->tx_mode)
  1460. tw32_f(MAC_TX_MODE, tp->tx_mode);
  1461. }
  1462. static void tg3_adjust_link(struct net_device *dev)
  1463. {
  1464. u8 oldflowctrl, linkmesg = 0;
  1465. u32 mac_mode, lcl_adv, rmt_adv;
  1466. struct tg3 *tp = netdev_priv(dev);
  1467. struct phy_device *phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
  1468. spin_lock_bh(&tp->lock);
  1469. mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
  1470. MAC_MODE_HALF_DUPLEX);
  1471. oldflowctrl = tp->link_config.active_flowctrl;
  1472. if (phydev->link) {
  1473. lcl_adv = 0;
  1474. rmt_adv = 0;
  1475. if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
  1476. mac_mode |= MAC_MODE_PORT_MODE_MII;
  1477. else if (phydev->speed == SPEED_1000 ||
  1478. GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785)
  1479. mac_mode |= MAC_MODE_PORT_MODE_GMII;
  1480. else
  1481. mac_mode |= MAC_MODE_PORT_MODE_MII;
  1482. if (phydev->duplex == DUPLEX_HALF)
  1483. mac_mode |= MAC_MODE_HALF_DUPLEX;
  1484. else {
  1485. lcl_adv = mii_advertise_flowctrl(
  1486. tp->link_config.flowctrl);
  1487. if (phydev->pause)
  1488. rmt_adv = LPA_PAUSE_CAP;
  1489. if (phydev->asym_pause)
  1490. rmt_adv |= LPA_PAUSE_ASYM;
  1491. }
  1492. tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
  1493. } else
  1494. mac_mode |= MAC_MODE_PORT_MODE_GMII;
  1495. if (mac_mode != tp->mac_mode) {
  1496. tp->mac_mode = mac_mode;
  1497. tw32_f(MAC_MODE, tp->mac_mode);
  1498. udelay(40);
  1499. }
  1500. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
  1501. if (phydev->speed == SPEED_10)
  1502. tw32(MAC_MI_STAT,
  1503. MAC_MI_STAT_10MBPS_MODE |
  1504. MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
  1505. else
  1506. tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
  1507. }
  1508. if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
  1509. tw32(MAC_TX_LENGTHS,
  1510. ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
  1511. (6 << TX_LENGTHS_IPG_SHIFT) |
  1512. (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
  1513. else
  1514. tw32(MAC_TX_LENGTHS,
  1515. ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
  1516. (6 << TX_LENGTHS_IPG_SHIFT) |
  1517. (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
  1518. if (phydev->link != tp->old_link ||
  1519. phydev->speed != tp->link_config.active_speed ||
  1520. phydev->duplex != tp->link_config.active_duplex ||
  1521. oldflowctrl != tp->link_config.active_flowctrl)
  1522. linkmesg = 1;
  1523. tp->old_link = phydev->link;
  1524. tp->link_config.active_speed = phydev->speed;
  1525. tp->link_config.active_duplex = phydev->duplex;
  1526. spin_unlock_bh(&tp->lock);
  1527. if (linkmesg)
  1528. tg3_link_report(tp);
  1529. }
  1530. static int tg3_phy_init(struct tg3 *tp)
  1531. {
  1532. struct phy_device *phydev;
  1533. if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)
  1534. return 0;
  1535. /* Bring the PHY back to a known state. */
  1536. tg3_bmcr_reset(tp);
  1537. phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
  1538. /* Attach the MAC to the PHY. */
  1539. phydev = phy_connect(tp->dev, dev_name(&phydev->dev), tg3_adjust_link,
  1540. phydev->dev_flags, phydev->interface);
  1541. if (IS_ERR(phydev)) {
  1542. dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
  1543. return PTR_ERR(phydev);
  1544. }
  1545. /* Mask with MAC supported features. */
  1546. switch (phydev->interface) {
  1547. case PHY_INTERFACE_MODE_GMII:
  1548. case PHY_INTERFACE_MODE_RGMII:
  1549. if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
  1550. phydev->supported &= (PHY_GBIT_FEATURES |
  1551. SUPPORTED_Pause |
  1552. SUPPORTED_Asym_Pause);
  1553. break;
  1554. }
  1555. /* fallthru */
  1556. case PHY_INTERFACE_MODE_MII:
  1557. phydev->supported &= (PHY_BASIC_FEATURES |
  1558. SUPPORTED_Pause |
  1559. SUPPORTED_Asym_Pause);
  1560. break;
  1561. default:
  1562. phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
  1563. return -EINVAL;
  1564. }
  1565. tp->phy_flags |= TG3_PHYFLG_IS_CONNECTED;
  1566. phydev->advertising = phydev->supported;
  1567. return 0;
  1568. }
  1569. static void tg3_phy_start(struct tg3 *tp)
  1570. {
  1571. struct phy_device *phydev;
  1572. if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
  1573. return;
  1574. phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
  1575. if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
  1576. tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
  1577. phydev->speed = tp->link_config.speed;
  1578. phydev->duplex = tp->link_config.duplex;
  1579. phydev->autoneg = tp->link_config.autoneg;
  1580. phydev->advertising = tp->link_config.advertising;
  1581. }
  1582. phy_start(phydev);
  1583. phy_start_aneg(phydev);
  1584. }
  1585. static void tg3_phy_stop(struct tg3 *tp)
  1586. {
  1587. if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
  1588. return;
  1589. phy_stop(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
  1590. }
  1591. static void tg3_phy_fini(struct tg3 *tp)
  1592. {
  1593. if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
  1594. phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
  1595. tp->phy_flags &= ~TG3_PHYFLG_IS_CONNECTED;
  1596. }
  1597. }
  1598. static int tg3_phy_set_extloopbk(struct tg3 *tp)
  1599. {
  1600. int err;
  1601. u32 val;
  1602. if (tp->phy_flags & TG3_PHYFLG_IS_FET)
  1603. return 0;
  1604. if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
  1605. /* Cannot do read-modify-write on 5401 */
  1606. err = tg3_phy_auxctl_write(tp,
  1607. MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
  1608. MII_TG3_AUXCTL_ACTL_EXTLOOPBK |
  1609. 0x4c20);
  1610. goto done;
  1611. }
  1612. err = tg3_phy_auxctl_read(tp,
  1613. MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
  1614. if (err)
  1615. return err;
  1616. val |= MII_TG3_AUXCTL_ACTL_EXTLOOPBK;
  1617. err = tg3_phy_auxctl_write(tp,
  1618. MII_TG3_AUXCTL_SHDWSEL_AUXCTL, val);
  1619. done:
  1620. return err;
  1621. }
  1622. static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
  1623. {
  1624. u32 phytest;
  1625. if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
  1626. u32 phy;
  1627. tg3_writephy(tp, MII_TG3_FET_TEST,
  1628. phytest | MII_TG3_FET_SHADOW_EN);
  1629. if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
  1630. if (enable)
  1631. phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
  1632. else
  1633. phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
  1634. tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
  1635. }
  1636. tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
  1637. }
  1638. }
  1639. static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
  1640. {
  1641. u32 reg;
  1642. if (!tg3_flag(tp, 5705_PLUS) ||
  1643. (tg3_flag(tp, 5717_PLUS) &&
  1644. (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
  1645. return;
  1646. if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
  1647. tg3_phy_fet_toggle_apd(tp, enable);
  1648. return;
  1649. }
  1650. reg = MII_TG3_MISC_SHDW_WREN |
  1651. MII_TG3_MISC_SHDW_SCR5_SEL |
  1652. MII_TG3_MISC_SHDW_SCR5_LPED |
  1653. MII_TG3_MISC_SHDW_SCR5_DLPTLM |
  1654. MII_TG3_MISC_SHDW_SCR5_SDTL |
  1655. MII_TG3_MISC_SHDW_SCR5_C125OE;
  1656. if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 || !enable)
  1657. reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
  1658. tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
  1659. reg = MII_TG3_MISC_SHDW_WREN |
  1660. MII_TG3_MISC_SHDW_APD_SEL |
  1661. MII_TG3_MISC_SHDW_APD_WKTM_84MS;
  1662. if (enable)
  1663. reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
  1664. tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
  1665. }
  1666. static void tg3_phy_toggle_automdix(struct tg3 *tp, int enable)
  1667. {
  1668. u32 phy;
  1669. if (!tg3_flag(tp, 5705_PLUS) ||
  1670. (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
  1671. return;
  1672. if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
  1673. u32 ephy;
  1674. if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
  1675. u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
  1676. tg3_writephy(tp, MII_TG3_FET_TEST,
  1677. ephy | MII_TG3_FET_SHADOW_EN);
  1678. if (!tg3_readphy(tp, reg, &phy)) {
  1679. if (enable)
  1680. phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
  1681. else
  1682. phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
  1683. tg3_writephy(tp, reg, phy);
  1684. }
  1685. tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
  1686. }
  1687. } else {
  1688. int ret;
  1689. ret = tg3_phy_auxctl_read(tp,
  1690. MII_TG3_AUXCTL_SHDWSEL_MISC, &phy);
  1691. if (!ret) {
  1692. if (enable)
  1693. phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
  1694. else
  1695. phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
  1696. tg3_phy_auxctl_write(tp,
  1697. MII_TG3_AUXCTL_SHDWSEL_MISC, phy);
  1698. }
  1699. }
  1700. }
  1701. static void tg3_phy_set_wirespeed(struct tg3 *tp)
  1702. {
  1703. int ret;
  1704. u32 val;
  1705. if (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED)
  1706. return;
  1707. ret = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_MISC, &val);
  1708. if (!ret)
  1709. tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_MISC,
  1710. val | MII_TG3_AUXCTL_MISC_WIRESPD_EN);
  1711. }
  1712. static void tg3_phy_apply_otp(struct tg3 *tp)
  1713. {
  1714. u32 otp, phy;
  1715. if (!tp->phy_otp)
  1716. return;
  1717. otp = tp->phy_otp;
  1718. if (TG3_PHY_AUXCTL_SMDSP_ENABLE(tp))
  1719. return;
  1720. phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
  1721. phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
  1722. tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
  1723. phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
  1724. ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
  1725. tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
  1726. phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
  1727. phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
  1728. tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
  1729. phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
  1730. tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
  1731. phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
  1732. tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
  1733. phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
  1734. ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
  1735. tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
  1736. TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
  1737. }
  1738. static void tg3_phy_eee_adjust(struct tg3 *tp, u32 current_link_up)
  1739. {
  1740. u32 val;
  1741. if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
  1742. return;
  1743. tp->setlpicnt = 0;
  1744. if (tp->link_config.autoneg == AUTONEG_ENABLE &&
  1745. current_link_up == 1 &&
  1746. tp->link_config.active_duplex == DUPLEX_FULL &&
  1747. (tp->link_config.active_speed == SPEED_100 ||
  1748. tp->link_config.active_speed == SPEED_1000)) {
  1749. u32 eeectl;
  1750. if (tp->link_config.active_speed == SPEED_1000)
  1751. eeectl = TG3_CPMU_EEE_CTRL_EXIT_16_5_US;
  1752. else
  1753. eeectl = TG3_CPMU_EEE_CTRL_EXIT_36_US;
  1754. tw32(TG3_CPMU_EEE_CTRL, eeectl);
  1755. tg3_phy_cl45_read(tp, MDIO_MMD_AN,
  1756. TG3_CL45_D7_EEERES_STAT, &val);
  1757. if (val == TG3_CL45_D7_EEERES_STAT_LP_1000T ||
  1758. val == TG3_CL45_D7_EEERES_STAT_LP_100TX)
  1759. tp->setlpicnt = 2;
  1760. }
  1761. if (!tp->setlpicnt) {
  1762. if (current_link_up == 1 &&
  1763. !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
  1764. tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, 0x0000);
  1765. TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
  1766. }
  1767. val = tr32(TG3_CPMU_EEE_MODE);
  1768. tw32(TG3_CPMU_EEE_MODE, val & ~TG3_CPMU_EEEMD_LPI_ENABLE);
  1769. }
  1770. }
  1771. static void tg3_phy_eee_enable(struct tg3 *tp)
  1772. {
  1773. u32 val;
  1774. if (tp->link_config.active_speed == SPEED_1000 &&
  1775. (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
  1776. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
  1777. tg3_flag(tp, 57765_CLASS)) &&
  1778. !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
  1779. val = MII_TG3_DSP_TAP26_ALNOKO |
  1780. MII_TG3_DSP_TAP26_RMRXSTO;
  1781. tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
  1782. TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
  1783. }
  1784. val = tr32(TG3_CPMU_EEE_MODE);
  1785. tw32(TG3_CPMU_EEE_MODE, val | TG3_CPMU_EEEMD_LPI_ENABLE);
  1786. }
  1787. static int tg3_wait_macro_done(struct tg3 *tp)
  1788. {
  1789. int limit = 100;
  1790. while (limit--) {
  1791. u32 tmp32;
  1792. if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
  1793. if ((tmp32 & 0x1000) == 0)
  1794. break;
  1795. }
  1796. }
  1797. if (limit < 0)
  1798. return -EBUSY;
  1799. return 0;
  1800. }
  1801. static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
  1802. {
  1803. static const u32 test_pat[4][6] = {
  1804. { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
  1805. { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
  1806. { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
  1807. { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
  1808. };
  1809. int chan;
  1810. for (chan = 0; chan < 4; chan++) {
  1811. int i;
  1812. tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
  1813. (chan * 0x2000) | 0x0200);
  1814. tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
  1815. for (i = 0; i < 6; i++)
  1816. tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
  1817. test_pat[chan][i]);
  1818. tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
  1819. if (tg3_wait_macro_done(tp)) {
  1820. *resetp = 1;
  1821. return -EBUSY;
  1822. }
  1823. tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
  1824. (chan * 0x2000) | 0x0200);
  1825. tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
  1826. if (tg3_wait_macro_done(tp)) {
  1827. *resetp = 1;
  1828. return -EBUSY;
  1829. }
  1830. tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
  1831. if (tg3_wait_macro_done(tp)) {
  1832. *resetp = 1;
  1833. return -EBUSY;
  1834. }
  1835. for (i = 0; i < 6; i += 2) {
  1836. u32 low, high;
  1837. if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
  1838. tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
  1839. tg3_wait_macro_done(tp)) {
  1840. *resetp = 1;
  1841. return -EBUSY;
  1842. }
  1843. low &= 0x7fff;
  1844. high &= 0x000f;
  1845. if (low != test_pat[chan][i] ||
  1846. high != test_pat[chan][i+1]) {
  1847. tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
  1848. tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
  1849. tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
  1850. return -EBUSY;
  1851. }
  1852. }
  1853. }
  1854. return 0;
  1855. }
  1856. static int tg3_phy_reset_chanpat(struct tg3 *tp)
  1857. {
  1858. int chan;
  1859. for (chan = 0; chan < 4; chan++) {
  1860. int i;
  1861. tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
  1862. (chan * 0x2000) | 0x0200);
  1863. tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
  1864. for (i = 0; i < 6; i++)
  1865. tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
  1866. tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
  1867. if (tg3_wait_macro_done(tp))
  1868. return -EBUSY;
  1869. }
  1870. return 0;
  1871. }
  1872. static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
  1873. {
  1874. u32 reg32, phy9_orig;
  1875. int retries, do_phy_reset, err;
  1876. retries = 10;
  1877. do_phy_reset = 1;
  1878. do {
  1879. if (do_phy_reset) {
  1880. err = tg3_bmcr_reset(tp);
  1881. if (err)
  1882. return err;
  1883. do_phy_reset = 0;
  1884. }
  1885. /* Disable transmitter and interrupt. */
  1886. if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
  1887. continue;
  1888. reg32 |= 0x3000;
  1889. tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
  1890. /* Set full-duplex, 1000 mbps. */
  1891. tg3_writephy(tp, MII_BMCR,
  1892. BMCR_FULLDPLX | BMCR_SPEED1000);
  1893. /* Set to master mode. */
  1894. if (tg3_readphy(tp, MII_CTRL1000, &phy9_orig))
  1895. continue;
  1896. tg3_writephy(tp, MII_CTRL1000,
  1897. CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
  1898. err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
  1899. if (err)
  1900. return err;
  1901. /* Block the PHY control access. */
  1902. tg3_phydsp_write(tp, 0x8005, 0x0800);
  1903. err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
  1904. if (!err)
  1905. break;
  1906. } while (--retries);
  1907. err = tg3_phy_reset_chanpat(tp);
  1908. if (err)
  1909. return err;
  1910. tg3_phydsp_write(tp, 0x8005, 0x0000);
  1911. tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
  1912. tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0000);
  1913. TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
  1914. tg3_writephy(tp, MII_CTRL1000, phy9_orig);
  1915. if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
  1916. reg32 &= ~0x3000;
  1917. tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
  1918. } else if (!err)
  1919. err = -EBUSY;
  1920. return err;
  1921. }
  1922. /* This will reset the tigon3 PHY if there is no valid
  1923. * link unless the FORCE argument is non-zero.
  1924. */
  1925. static int tg3_phy_reset(struct tg3 *tp)
  1926. {
  1927. u32 val, cpmuctrl;
  1928. int err;
  1929. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
  1930. val = tr32(GRC_MISC_CFG);
  1931. tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
  1932. udelay(40);
  1933. }
  1934. err = tg3_readphy(tp, MII_BMSR, &val);
  1935. err |= tg3_readphy(tp, MII_BMSR, &val);
  1936. if (err != 0)
  1937. return -EBUSY;
  1938. if (netif_running(tp->dev) && netif_carrier_ok(tp->dev)) {
  1939. netif_carrier_off(tp->dev);
  1940. tg3_link_report(tp);
  1941. }
  1942. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
  1943. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
  1944. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
  1945. err = tg3_phy_reset_5703_4_5(tp);
  1946. if (err)
  1947. return err;
  1948. goto out;
  1949. }
  1950. cpmuctrl = 0;
  1951. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
  1952. GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
  1953. cpmuctrl = tr32(TG3_CPMU_CTRL);
  1954. if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
  1955. tw32(TG3_CPMU_CTRL,
  1956. cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
  1957. }
  1958. err = tg3_bmcr_reset(tp);
  1959. if (err)
  1960. return err;
  1961. if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
  1962. val = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
  1963. tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, val);
  1964. tw32(TG3_CPMU_CTRL, cpmuctrl);
  1965. }
  1966. if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
  1967. GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
  1968. val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
  1969. if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
  1970. CPMU_LSPD_1000MB_MACCLK_12_5) {
  1971. val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
  1972. udelay(40);
  1973. tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
  1974. }
  1975. }
  1976. if (tg3_flag(tp, 5717_PLUS) &&
  1977. (tp->phy_flags & TG3_PHYFLG_MII_SERDES))
  1978. return 0;
  1979. tg3_phy_apply_otp(tp);
  1980. if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
  1981. tg3_phy_toggle_apd(tp, true);
  1982. else
  1983. tg3_phy_toggle_apd(tp, false);
  1984. out:
  1985. if ((tp->phy_flags & TG3_PHYFLG_ADC_BUG) &&
  1986. !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
  1987. tg3_phydsp_write(tp, 0x201f, 0x2aaa);
  1988. tg3_phydsp_write(tp, 0x000a, 0x0323);
  1989. TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
  1990. }
  1991. if (tp->phy_flags & TG3_PHYFLG_5704_A0_BUG) {
  1992. tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
  1993. tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
  1994. }
  1995. if (tp->phy_flags & TG3_PHYFLG_BER_BUG) {
  1996. if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
  1997. tg3_phydsp_write(tp, 0x000a, 0x310b);
  1998. tg3_phydsp_write(tp, 0x201f, 0x9506);
  1999. tg3_phydsp_write(tp, 0x401f, 0x14e2);
  2000. TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
  2001. }
  2002. } else if (tp->phy_flags & TG3_PHYFLG_JITTER_BUG) {
  2003. if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
  2004. tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
  2005. if (tp->phy_flags & TG3_PHYFLG_ADJUST_TRIM) {
  2006. tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
  2007. tg3_writephy(tp, MII_TG3_TEST1,
  2008. MII_TG3_TEST1_TRIM_EN | 0x4);
  2009. } else
  2010. tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
  2011. TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
  2012. }
  2013. }
  2014. /* Set Extended packet length bit (bit 14) on all chips that */
  2015. /* support jumbo frames */
  2016. if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
  2017. /* Cannot do read-modify-write on 5401 */
  2018. tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
  2019. } else if (tg3_flag(tp, JUMBO_CAPABLE)) {
  2020. /* Set bit 14 with read-modify-write to preserve other bits */
  2021. err = tg3_phy_auxctl_read(tp,
  2022. MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
  2023. if (!err)
  2024. tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
  2025. val | MII_TG3_AUXCTL_ACTL_EXTPKTLEN);
  2026. }
  2027. /* Set phy register 0x10 bit 0 to high fifo elasticity to support
  2028. * jumbo frames transmission.
  2029. */
  2030. if (tg3_flag(tp, JUMBO_CAPABLE)) {
  2031. if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &val))
  2032. tg3_writephy(tp, MII_TG3_EXT_CTRL,
  2033. val | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
  2034. }
  2035. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
  2036. /* adjust output voltage */
  2037. tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
  2038. }
  2039. tg3_phy_toggle_automdix(tp, 1);
  2040. tg3_phy_set_wirespeed(tp);
  2041. return 0;
  2042. }
  2043. #define TG3_GPIO_MSG_DRVR_PRES 0x00000001
  2044. #define TG3_GPIO_MSG_NEED_VAUX 0x00000002
  2045. #define TG3_GPIO_MSG_MASK (TG3_GPIO_MSG_DRVR_PRES | \
  2046. TG3_GPIO_MSG_NEED_VAUX)
  2047. #define TG3_GPIO_MSG_ALL_DRVR_PRES_MASK \
  2048. ((TG3_GPIO_MSG_DRVR_PRES << 0) | \
  2049. (TG3_GPIO_MSG_DRVR_PRES << 4) | \
  2050. (TG3_GPIO_MSG_DRVR_PRES << 8) | \
  2051. (TG3_GPIO_MSG_DRVR_PRES << 12))
  2052. #define TG3_GPIO_MSG_ALL_NEED_VAUX_MASK \
  2053. ((TG3_GPIO_MSG_NEED_VAUX << 0) | \
  2054. (TG3_GPIO_MSG_NEED_VAUX << 4) | \
  2055. (TG3_GPIO_MSG_NEED_VAUX << 8) | \
  2056. (TG3_GPIO_MSG_NEED_VAUX << 12))
  2057. static inline u32 tg3_set_function_status(struct tg3 *tp, u32 newstat)
  2058. {
  2059. u32 status, shift;
  2060. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
  2061. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
  2062. status = tg3_ape_read32(tp, TG3_APE_GPIO_MSG);
  2063. else
  2064. status = tr32(TG3_CPMU_DRV_STATUS);
  2065. shift = TG3_APE_GPIO_MSG_SHIFT + 4 * tp->pci_fn;
  2066. status &= ~(TG3_GPIO_MSG_MASK << shift);
  2067. status |= (newstat << shift);
  2068. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
  2069. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
  2070. tg3_ape_write32(tp, TG3_APE_GPIO_MSG, status);
  2071. else
  2072. tw32(TG3_CPMU_DRV_STATUS, status);
  2073. return status >> TG3_APE_GPIO_MSG_SHIFT;
  2074. }
  2075. static inline int tg3_pwrsrc_switch_to_vmain(struct tg3 *tp)
  2076. {
  2077. if (!tg3_flag(tp, IS_NIC))
  2078. return 0;
  2079. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
  2080. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
  2081. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
  2082. if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
  2083. return -EIO;
  2084. tg3_set_function_status(tp, TG3_GPIO_MSG_DRVR_PRES);
  2085. tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
  2086. TG3_GRC_LCLCTL_PWRSW_DELAY);
  2087. tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
  2088. } else {
  2089. tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
  2090. TG3_GRC_LCLCTL_PWRSW_DELAY);
  2091. }
  2092. return 0;
  2093. }
  2094. static void tg3_pwrsrc_die_with_vmain(struct tg3 *tp)
  2095. {
  2096. u32 grc_local_ctrl;
  2097. if (!tg3_flag(tp, IS_NIC) ||
  2098. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
  2099. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)
  2100. return;
  2101. grc_local_ctrl = tp->grc_local_ctrl | GRC_LCLCTRL_GPIO_OE1;
  2102. tw32_wait_f(GRC_LOCAL_CTRL,
  2103. grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
  2104. TG3_GRC_LCLCTL_PWRSW_DELAY);
  2105. tw32_wait_f(GRC_LOCAL_CTRL,
  2106. grc_local_ctrl,
  2107. TG3_GRC_LCLCTL_PWRSW_DELAY);
  2108. tw32_wait_f(GRC_LOCAL_CTRL,
  2109. grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
  2110. TG3_GRC_LCLCTL_PWRSW_DELAY);
  2111. }
  2112. static void tg3_pwrsrc_switch_to_vaux(struct tg3 *tp)
  2113. {
  2114. if (!tg3_flag(tp, IS_NIC))
  2115. return;
  2116. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
  2117. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
  2118. tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
  2119. (GRC_LCLCTRL_GPIO_OE0 |
  2120. GRC_LCLCTRL_GPIO_OE1 |
  2121. GRC_LCLCTRL_GPIO_OE2 |
  2122. GRC_LCLCTRL_GPIO_OUTPUT0 |
  2123. GRC_LCLCTRL_GPIO_OUTPUT1),
  2124. TG3_GRC_LCLCTL_PWRSW_DELAY);
  2125. } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
  2126. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
  2127. /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
  2128. u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
  2129. GRC_LCLCTRL_GPIO_OE1 |
  2130. GRC_LCLCTRL_GPIO_OE2 |
  2131. GRC_LCLCTRL_GPIO_OUTPUT0 |
  2132. GRC_LCLCTRL_GPIO_OUTPUT1 |
  2133. tp->grc_local_ctrl;
  2134. tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
  2135. TG3_GRC_LCLCTL_PWRSW_DELAY);
  2136. grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
  2137. tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
  2138. TG3_GRC_LCLCTL_PWRSW_DELAY);
  2139. grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
  2140. tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
  2141. TG3_GRC_LCLCTL_PWRSW_DELAY);
  2142. } else {
  2143. u32 no_gpio2;
  2144. u32 grc_local_ctrl = 0;
  2145. /* Workaround to prevent overdrawing Amps. */
  2146. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
  2147. grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
  2148. tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
  2149. grc_local_ctrl,
  2150. TG3_GRC_LCLCTL_PWRSW_DELAY);
  2151. }
  2152. /* On 5753 and variants, GPIO2 cannot be used. */
  2153. no_gpio2 = tp->nic_sram_data_cfg &
  2154. NIC_SRAM_DATA_CFG_NO_GPIO2;
  2155. grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
  2156. GRC_LCLCTRL_GPIO_OE1 |
  2157. GRC_LCLCTRL_GPIO_OE2 |
  2158. GRC_LCLCTRL_GPIO_OUTPUT1 |
  2159. GRC_LCLCTRL_GPIO_OUTPUT2;
  2160. if (no_gpio2) {
  2161. grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
  2162. GRC_LCLCTRL_GPIO_OUTPUT2);
  2163. }
  2164. tw32_wait_f(GRC_LOCAL_CTRL,
  2165. tp->grc_local_ctrl | grc_local_ctrl,
  2166. TG3_GRC_LCLCTL_PWRSW_DELAY);
  2167. grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
  2168. tw32_wait_f(GRC_LOCAL_CTRL,
  2169. tp->grc_local_ctrl | grc_local_ctrl,
  2170. TG3_GRC_LCLCTL_PWRSW_DELAY);
  2171. if (!no_gpio2) {
  2172. grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
  2173. tw32_wait_f(GRC_LOCAL_CTRL,
  2174. tp->grc_local_ctrl | grc_local_ctrl,
  2175. TG3_GRC_LCLCTL_PWRSW_DELAY);
  2176. }
  2177. }
  2178. }
  2179. static void tg3_frob_aux_power_5717(struct tg3 *tp, bool wol_enable)
  2180. {
  2181. u32 msg = 0;
  2182. /* Serialize power state transitions */
  2183. if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
  2184. return;
  2185. if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE) || wol_enable)
  2186. msg = TG3_GPIO_MSG_NEED_VAUX;
  2187. msg = tg3_set_function_status(tp, msg);
  2188. if (msg & TG3_GPIO_MSG_ALL_DRVR_PRES_MASK)
  2189. goto done;
  2190. if (msg & TG3_GPIO_MSG_ALL_NEED_VAUX_MASK)
  2191. tg3_pwrsrc_switch_to_vaux(tp);
  2192. else
  2193. tg3_pwrsrc_die_with_vmain(tp);
  2194. done:
  2195. tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
  2196. }
  2197. static void tg3_frob_aux_power(struct tg3 *tp, bool include_wol)
  2198. {
  2199. bool need_vaux = false;
  2200. /* The GPIOs do something completely different on 57765. */
  2201. if (!tg3_flag(tp, IS_NIC) || tg3_flag(tp, 57765_CLASS))
  2202. return;
  2203. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
  2204. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
  2205. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
  2206. tg3_frob_aux_power_5717(tp, include_wol ?
  2207. tg3_flag(tp, WOL_ENABLE) != 0 : 0);
  2208. return;
  2209. }
  2210. if (tp->pdev_peer && tp->pdev_peer != tp->pdev) {
  2211. struct net_device *dev_peer;
  2212. dev_peer = pci_get_drvdata(tp->pdev_peer);
  2213. /* remove_one() may have been run on the peer. */
  2214. if (dev_peer) {
  2215. struct tg3 *tp_peer = netdev_priv(dev_peer);
  2216. if (tg3_flag(tp_peer, INIT_COMPLETE))
  2217. return;
  2218. if ((include_wol && tg3_flag(tp_peer, WOL_ENABLE)) ||
  2219. tg3_flag(tp_peer, ENABLE_ASF))
  2220. need_vaux = true;
  2221. }
  2222. }
  2223. if ((include_wol && tg3_flag(tp, WOL_ENABLE)) ||
  2224. tg3_flag(tp, ENABLE_ASF))
  2225. need_vaux = true;
  2226. if (need_vaux)
  2227. tg3_pwrsrc_switch_to_vaux(tp);
  2228. else
  2229. tg3_pwrsrc_die_with_vmain(tp);
  2230. }
  2231. static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
  2232. {
  2233. if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
  2234. return 1;
  2235. else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
  2236. if (speed != SPEED_10)
  2237. return 1;
  2238. } else if (speed == SPEED_10)
  2239. return 1;
  2240. return 0;
  2241. }
  2242. static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
  2243. {
  2244. u32 val;
  2245. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
  2246. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
  2247. u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
  2248. u32 serdes_cfg = tr32(MAC_SERDES_CFG);
  2249. sg_dig_ctrl |=
  2250. SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
  2251. tw32(SG_DIG_CTRL, sg_dig_ctrl);
  2252. tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
  2253. }
  2254. return;
  2255. }
  2256. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
  2257. tg3_bmcr_reset(tp);
  2258. val = tr32(GRC_MISC_CFG);
  2259. tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
  2260. udelay(40);
  2261. return;
  2262. } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
  2263. u32 phytest;
  2264. if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
  2265. u32 phy;
  2266. tg3_writephy(tp, MII_ADVERTISE, 0);
  2267. tg3_writephy(tp, MII_BMCR,
  2268. BMCR_ANENABLE | BMCR_ANRESTART);
  2269. tg3_writephy(tp, MII_TG3_FET_TEST,
  2270. phytest | MII_TG3_FET_SHADOW_EN);
  2271. if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
  2272. phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
  2273. tg3_writephy(tp,
  2274. MII_TG3_FET_SHDW_AUXMODE4,
  2275. phy);
  2276. }
  2277. tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
  2278. }
  2279. return;
  2280. } else if (do_low_power) {
  2281. tg3_writephy(tp, MII_TG3_EXT_CTRL,
  2282. MII_TG3_EXT_CTRL_FORCE_LED_OFF);
  2283. val = MII_TG3_AUXCTL_PCTL_100TX_LPWR |
  2284. MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
  2285. MII_TG3_AUXCTL_PCTL_VREG_11V;
  2286. tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, val);
  2287. }
  2288. /* The PHY should not be powered down on some chips because
  2289. * of bugs.
  2290. */
  2291. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
  2292. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
  2293. (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 &&
  2294. (tp->phy_flags & TG3_PHYFLG_MII_SERDES)) ||
  2295. (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 &&
  2296. !tp->pci_fn))
  2297. return;
  2298. if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
  2299. GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
  2300. val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
  2301. val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
  2302. val |= CPMU_LSPD_1000MB_MACCLK_12_5;
  2303. tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
  2304. }
  2305. tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
  2306. }
  2307. /* tp->lock is held. */
  2308. static int tg3_nvram_lock(struct tg3 *tp)
  2309. {
  2310. if (tg3_flag(tp, NVRAM)) {
  2311. int i;
  2312. if (tp->nvram_lock_cnt == 0) {
  2313. tw32(NVRAM_SWARB, SWARB_REQ_SET1);
  2314. for (i = 0; i < 8000; i++) {
  2315. if (tr32(NVRAM_SWARB) & SWARB_GNT1)
  2316. break;
  2317. udelay(20);
  2318. }
  2319. if (i == 8000) {
  2320. tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
  2321. return -ENODEV;
  2322. }
  2323. }
  2324. tp->nvram_lock_cnt++;
  2325. }
  2326. return 0;
  2327. }
  2328. /* tp->lock is held. */
  2329. static void tg3_nvram_unlock(struct tg3 *tp)
  2330. {
  2331. if (tg3_flag(tp, NVRAM)) {
  2332. if (tp->nvram_lock_cnt > 0)
  2333. tp->nvram_lock_cnt--;
  2334. if (tp->nvram_lock_cnt == 0)
  2335. tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
  2336. }
  2337. }
  2338. /* tp->lock is held. */
  2339. static void tg3_enable_nvram_access(struct tg3 *tp)
  2340. {
  2341. if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
  2342. u32 nvaccess = tr32(NVRAM_ACCESS);
  2343. tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
  2344. }
  2345. }
  2346. /* tp->lock is held. */
  2347. static void tg3_disable_nvram_access(struct tg3 *tp)
  2348. {
  2349. if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
  2350. u32 nvaccess = tr32(NVRAM_ACCESS);
  2351. tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
  2352. }
  2353. }
  2354. static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
  2355. u32 offset, u32 *val)
  2356. {
  2357. u32 tmp;
  2358. int i;
  2359. if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
  2360. return -EINVAL;
  2361. tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
  2362. EEPROM_ADDR_DEVID_MASK |
  2363. EEPROM_ADDR_READ);
  2364. tw32(GRC_EEPROM_ADDR,
  2365. tmp |
  2366. (0 << EEPROM_ADDR_DEVID_SHIFT) |
  2367. ((offset << EEPROM_ADDR_ADDR_SHIFT) &
  2368. EEPROM_ADDR_ADDR_MASK) |
  2369. EEPROM_ADDR_READ | EEPROM_ADDR_START);
  2370. for (i = 0; i < 1000; i++) {
  2371. tmp = tr32(GRC_EEPROM_ADDR);
  2372. if (tmp & EEPROM_ADDR_COMPLETE)
  2373. break;
  2374. msleep(1);
  2375. }
  2376. if (!(tmp & EEPROM_ADDR_COMPLETE))
  2377. return -EBUSY;
  2378. tmp = tr32(GRC_EEPROM_DATA);
  2379. /*
  2380. * The data will always be opposite the native endian
  2381. * format. Perform a blind byteswap to compensate.
  2382. */
  2383. *val = swab32(tmp);
  2384. return 0;
  2385. }
  2386. #define NVRAM_CMD_TIMEOUT 10000
  2387. static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
  2388. {
  2389. int i;
  2390. tw32(NVRAM_CMD, nvram_cmd);
  2391. for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
  2392. udelay(10);
  2393. if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
  2394. udelay(10);
  2395. break;
  2396. }
  2397. }
  2398. if (i == NVRAM_CMD_TIMEOUT)
  2399. return -EBUSY;
  2400. return 0;
  2401. }
  2402. static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
  2403. {
  2404. if (tg3_flag(tp, NVRAM) &&
  2405. tg3_flag(tp, NVRAM_BUFFERED) &&
  2406. tg3_flag(tp, FLASH) &&
  2407. !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
  2408. (tp->nvram_jedecnum == JEDEC_ATMEL))
  2409. addr = ((addr / tp->nvram_pagesize) <<
  2410. ATMEL_AT45DB0X1B_PAGE_POS) +
  2411. (addr % tp->nvram_pagesize);
  2412. return addr;
  2413. }
  2414. static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
  2415. {
  2416. if (tg3_flag(tp, NVRAM) &&
  2417. tg3_flag(tp, NVRAM_BUFFERED) &&
  2418. tg3_flag(tp, FLASH) &&
  2419. !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
  2420. (tp->nvram_jedecnum == JEDEC_ATMEL))
  2421. addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
  2422. tp->nvram_pagesize) +
  2423. (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
  2424. return addr;
  2425. }
  2426. /* NOTE: Data read in from NVRAM is byteswapped according to
  2427. * the byteswapping settings for all other register accesses.
  2428. * tg3 devices are BE devices, so on a BE machine, the data
  2429. * returned will be exactly as it is seen in NVRAM. On a LE
  2430. * machine, the 32-bit value will be byteswapped.
  2431. */
  2432. static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
  2433. {
  2434. int ret;
  2435. if (!tg3_flag(tp, NVRAM))
  2436. return tg3_nvram_read_using_eeprom(tp, offset, val);
  2437. offset = tg3_nvram_phys_addr(tp, offset);
  2438. if (offset > NVRAM_ADDR_MSK)
  2439. return -EINVAL;
  2440. ret = tg3_nvram_lock(tp);
  2441. if (ret)
  2442. return ret;
  2443. tg3_enable_nvram_access(tp);
  2444. tw32(NVRAM_ADDR, offset);
  2445. ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
  2446. NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
  2447. if (ret == 0)
  2448. *val = tr32(NVRAM_RDDATA);
  2449. tg3_disable_nvram_access(tp);
  2450. tg3_nvram_unlock(tp);
  2451. return ret;
  2452. }
  2453. /* Ensures NVRAM data is in bytestream format. */
  2454. static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
  2455. {
  2456. u32 v;
  2457. int res = tg3_nvram_read(tp, offset, &v);
  2458. if (!res)
  2459. *val = cpu_to_be32(v);
  2460. return res;
  2461. }
  2462. static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
  2463. u32 offset, u32 len, u8 *buf)
  2464. {
  2465. int i, j, rc = 0;
  2466. u32 val;
  2467. for (i = 0; i < len; i += 4) {
  2468. u32 addr;
  2469. __be32 data;
  2470. addr = offset + i;
  2471. memcpy(&data, buf + i, 4);
  2472. /*
  2473. * The SEEPROM interface expects the data to always be opposite
  2474. * the native endian format. We accomplish this by reversing
  2475. * all the operations that would have been performed on the
  2476. * data from a call to tg3_nvram_read_be32().
  2477. */
  2478. tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
  2479. val = tr32(GRC_EEPROM_ADDR);
  2480. tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
  2481. val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
  2482. EEPROM_ADDR_READ);
  2483. tw32(GRC_EEPROM_ADDR, val |
  2484. (0 << EEPROM_ADDR_DEVID_SHIFT) |
  2485. (addr & EEPROM_ADDR_ADDR_MASK) |
  2486. EEPROM_ADDR_START |
  2487. EEPROM_ADDR_WRITE);
  2488. for (j = 0; j < 1000; j++) {
  2489. val = tr32(GRC_EEPROM_ADDR);
  2490. if (val & EEPROM_ADDR_COMPLETE)
  2491. break;
  2492. msleep(1);
  2493. }
  2494. if (!(val & EEPROM_ADDR_COMPLETE)) {
  2495. rc = -EBUSY;
  2496. break;
  2497. }
  2498. }
  2499. return rc;
  2500. }
  2501. /* offset and length are dword aligned */
  2502. static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
  2503. u8 *buf)
  2504. {
  2505. int ret = 0;
  2506. u32 pagesize = tp->nvram_pagesize;
  2507. u32 pagemask = pagesize - 1;
  2508. u32 nvram_cmd;
  2509. u8 *tmp;
  2510. tmp = kmalloc(pagesize, GFP_KERNEL);
  2511. if (tmp == NULL)
  2512. return -ENOMEM;
  2513. while (len) {
  2514. int j;
  2515. u32 phy_addr, page_off, size;
  2516. phy_addr = offset & ~pagemask;
  2517. for (j = 0; j < pagesize; j += 4) {
  2518. ret = tg3_nvram_read_be32(tp, phy_addr + j,
  2519. (__be32 *) (tmp + j));
  2520. if (ret)
  2521. break;
  2522. }
  2523. if (ret)
  2524. break;
  2525. page_off = offset & pagemask;
  2526. size = pagesize;
  2527. if (len < size)
  2528. size = len;
  2529. len -= size;
  2530. memcpy(tmp + page_off, buf, size);
  2531. offset = offset + (pagesize - page_off);
  2532. tg3_enable_nvram_access(tp);
  2533. /*
  2534. * Before we can erase the flash page, we need
  2535. * to issue a special "write enable" command.
  2536. */
  2537. nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
  2538. if (tg3_nvram_exec_cmd(tp, nvram_cmd))
  2539. break;
  2540. /* Erase the target page */
  2541. tw32(NVRAM_ADDR, phy_addr);
  2542. nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
  2543. NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
  2544. if (tg3_nvram_exec_cmd(tp, nvram_cmd))
  2545. break;
  2546. /* Issue another write enable to start the write. */
  2547. nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
  2548. if (tg3_nvram_exec_cmd(tp, nvram_cmd))
  2549. break;
  2550. for (j = 0; j < pagesize; j += 4) {
  2551. __be32 data;
  2552. data = *((__be32 *) (tmp + j));
  2553. tw32(NVRAM_WRDATA, be32_to_cpu(data));
  2554. tw32(NVRAM_ADDR, phy_addr + j);
  2555. nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
  2556. NVRAM_CMD_WR;
  2557. if (j == 0)
  2558. nvram_cmd |= NVRAM_CMD_FIRST;
  2559. else if (j == (pagesize - 4))
  2560. nvram_cmd |= NVRAM_CMD_LAST;
  2561. ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
  2562. if (ret)
  2563. break;
  2564. }
  2565. if (ret)
  2566. break;
  2567. }
  2568. nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
  2569. tg3_nvram_exec_cmd(tp, nvram_cmd);
  2570. kfree(tmp);
  2571. return ret;
  2572. }
  2573. /* offset and length are dword aligned */
  2574. static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
  2575. u8 *buf)
  2576. {
  2577. int i, ret = 0;
  2578. for (i = 0; i < len; i += 4, offset += 4) {
  2579. u32 page_off, phy_addr, nvram_cmd;
  2580. __be32 data;
  2581. memcpy(&data, buf + i, 4);
  2582. tw32(NVRAM_WRDATA, be32_to_cpu(data));
  2583. page_off = offset % tp->nvram_pagesize;
  2584. phy_addr = tg3_nvram_phys_addr(tp, offset);
  2585. nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
  2586. if (page_off == 0 || i == 0)
  2587. nvram_cmd |= NVRAM_CMD_FIRST;
  2588. if (page_off == (tp->nvram_pagesize - 4))
  2589. nvram_cmd |= NVRAM_CMD_LAST;
  2590. if (i == (len - 4))
  2591. nvram_cmd |= NVRAM_CMD_LAST;
  2592. if ((nvram_cmd & NVRAM_CMD_FIRST) ||
  2593. !tg3_flag(tp, FLASH) ||
  2594. !tg3_flag(tp, 57765_PLUS))
  2595. tw32(NVRAM_ADDR, phy_addr);
  2596. if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
  2597. !tg3_flag(tp, 5755_PLUS) &&
  2598. (tp->nvram_jedecnum == JEDEC_ST) &&
  2599. (nvram_cmd & NVRAM_CMD_FIRST)) {
  2600. u32 cmd;
  2601. cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
  2602. ret = tg3_nvram_exec_cmd(tp, cmd);
  2603. if (ret)
  2604. break;
  2605. }
  2606. if (!tg3_flag(tp, FLASH)) {
  2607. /* We always do complete word writes to eeprom. */
  2608. nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
  2609. }
  2610. ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
  2611. if (ret)
  2612. break;
  2613. }
  2614. return ret;
  2615. }
  2616. /* offset and length are dword aligned */
  2617. static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
  2618. {
  2619. int ret;
  2620. if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
  2621. tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
  2622. ~GRC_LCLCTRL_GPIO_OUTPUT1);
  2623. udelay(40);
  2624. }
  2625. if (!tg3_flag(tp, NVRAM)) {
  2626. ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
  2627. } else {
  2628. u32 grc_mode;
  2629. ret = tg3_nvram_lock(tp);
  2630. if (ret)
  2631. return ret;
  2632. tg3_enable_nvram_access(tp);
  2633. if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM))
  2634. tw32(NVRAM_WRITE1, 0x406);
  2635. grc_mode = tr32(GRC_MODE);
  2636. tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
  2637. if (tg3_flag(tp, NVRAM_BUFFERED) || !tg3_flag(tp, FLASH)) {
  2638. ret = tg3_nvram_write_block_buffered(tp, offset, len,
  2639. buf);
  2640. } else {
  2641. ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
  2642. buf);
  2643. }
  2644. grc_mode = tr32(GRC_MODE);
  2645. tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
  2646. tg3_disable_nvram_access(tp);
  2647. tg3_nvram_unlock(tp);
  2648. }
  2649. if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
  2650. tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
  2651. udelay(40);
  2652. }
  2653. return ret;
  2654. }
  2655. #define RX_CPU_SCRATCH_BASE 0x30000
  2656. #define RX_CPU_SCRATCH_SIZE 0x04000
  2657. #define TX_CPU_SCRATCH_BASE 0x34000
  2658. #define TX_CPU_SCRATCH_SIZE 0x04000
  2659. /* tp->lock is held. */
  2660. static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
  2661. {
  2662. int i;
  2663. BUG_ON(offset == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS));
  2664. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
  2665. u32 val = tr32(GRC_VCPU_EXT_CTRL);
  2666. tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
  2667. return 0;
  2668. }
  2669. if (offset == RX_CPU_BASE) {
  2670. for (i = 0; i < 10000; i++) {
  2671. tw32(offset + CPU_STATE, 0xffffffff);
  2672. tw32(offset + CPU_MODE, CPU_MODE_HALT);
  2673. if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
  2674. break;
  2675. }
  2676. tw32(offset + CPU_STATE, 0xffffffff);
  2677. tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
  2678. udelay(10);
  2679. } else {
  2680. for (i = 0; i < 10000; i++) {
  2681. tw32(offset + CPU_STATE, 0xffffffff);
  2682. tw32(offset + CPU_MODE, CPU_MODE_HALT);
  2683. if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
  2684. break;
  2685. }
  2686. }
  2687. if (i >= 10000) {
  2688. netdev_err(tp->dev, "%s timed out, %s CPU\n",
  2689. __func__, offset == RX_CPU_BASE ? "RX" : "TX");
  2690. return -ENODEV;
  2691. }
  2692. /* Clear firmware's nvram arbitration. */
  2693. if (tg3_flag(tp, NVRAM))
  2694. tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
  2695. return 0;
  2696. }
  2697. struct fw_info {
  2698. unsigned int fw_base;
  2699. unsigned int fw_len;
  2700. const __be32 *fw_data;
  2701. };
  2702. /* tp->lock is held. */
  2703. static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base,
  2704. u32 cpu_scratch_base, int cpu_scratch_size,
  2705. struct fw_info *info)
  2706. {
  2707. int err, lock_err, i;
  2708. void (*write_op)(struct tg3 *, u32, u32);
  2709. if (cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS)) {
  2710. netdev_err(tp->dev,
  2711. "%s: Trying to load TX cpu firmware which is 5705\n",
  2712. __func__);
  2713. return -EINVAL;
  2714. }
  2715. if (tg3_flag(tp, 5705_PLUS))
  2716. write_op = tg3_write_mem;
  2717. else
  2718. write_op = tg3_write_indirect_reg32;
  2719. /* It is possible that bootcode is still loading at this point.
  2720. * Get the nvram lock first before halting the cpu.
  2721. */
  2722. lock_err = tg3_nvram_lock(tp);
  2723. err = tg3_halt_cpu(tp, cpu_base);
  2724. if (!lock_err)
  2725. tg3_nvram_unlock(tp);
  2726. if (err)
  2727. goto out;
  2728. for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
  2729. write_op(tp, cpu_scratch_base + i, 0);
  2730. tw32(cpu_base + CPU_STATE, 0xffffffff);
  2731. tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
  2732. for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
  2733. write_op(tp, (cpu_scratch_base +
  2734. (info->fw_base & 0xffff) +
  2735. (i * sizeof(u32))),
  2736. be32_to_cpu(info->fw_data[i]));
  2737. err = 0;
  2738. out:
  2739. return err;
  2740. }
  2741. /* tp->lock is held. */
  2742. static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
  2743. {
  2744. struct fw_info info;
  2745. const __be32 *fw_data;
  2746. int err, i;
  2747. fw_data = (void *)tp->fw->data;
  2748. /* Firmware blob starts with version numbers, followed by
  2749. start address and length. We are setting complete length.
  2750. length = end_address_of_bss - start_address_of_text.
  2751. Remainder is the blob to be loaded contiguously
  2752. from start address. */
  2753. info.fw_base = be32_to_cpu(fw_data[1]);
  2754. info.fw_len = tp->fw->size - 12;
  2755. info.fw_data = &fw_data[3];
  2756. err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
  2757. RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
  2758. &info);
  2759. if (err)
  2760. return err;
  2761. err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
  2762. TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
  2763. &info);
  2764. if (err)
  2765. return err;
  2766. /* Now startup only the RX cpu. */
  2767. tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
  2768. tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
  2769. for (i = 0; i < 5; i++) {
  2770. if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
  2771. break;
  2772. tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
  2773. tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
  2774. tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
  2775. udelay(1000);
  2776. }
  2777. if (i >= 5) {
  2778. netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
  2779. "should be %08x\n", __func__,
  2780. tr32(RX_CPU_BASE + CPU_PC), info.fw_base);
  2781. return -ENODEV;
  2782. }
  2783. tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
  2784. tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
  2785. return 0;
  2786. }
  2787. /* tp->lock is held. */
  2788. static int tg3_load_tso_firmware(struct tg3 *tp)
  2789. {
  2790. struct fw_info info;
  2791. const __be32 *fw_data;
  2792. unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
  2793. int err, i;
  2794. if (tg3_flag(tp, HW_TSO_1) ||
  2795. tg3_flag(tp, HW_TSO_2) ||
  2796. tg3_flag(tp, HW_TSO_3))
  2797. return 0;
  2798. fw_data = (void *)tp->fw->data;
  2799. /* Firmware blob starts with version numbers, followed by
  2800. start address and length. We are setting complete length.
  2801. length = end_address_of_bss - start_address_of_text.
  2802. Remainder is the blob to be loaded contiguously
  2803. from start address. */
  2804. info.fw_base = be32_to_cpu(fw_data[1]);
  2805. cpu_scratch_size = tp->fw_len;
  2806. info.fw_len = tp->fw->size - 12;
  2807. info.fw_data = &fw_data[3];
  2808. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
  2809. cpu_base = RX_CPU_BASE;
  2810. cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
  2811. } else {
  2812. cpu_base = TX_CPU_BASE;
  2813. cpu_scratch_base = TX_CPU_SCRATCH_BASE;
  2814. cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
  2815. }
  2816. err = tg3_load_firmware_cpu(tp, cpu_base,
  2817. cpu_scratch_base, cpu_scratch_size,
  2818. &info);
  2819. if (err)
  2820. return err;
  2821. /* Now startup the cpu. */
  2822. tw32(cpu_base + CPU_STATE, 0xffffffff);
  2823. tw32_f(cpu_base + CPU_PC, info.fw_base);
  2824. for (i = 0; i < 5; i++) {
  2825. if (tr32(cpu_base + CPU_PC) == info.fw_base)
  2826. break;
  2827. tw32(cpu_base + CPU_STATE, 0xffffffff);
  2828. tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
  2829. tw32_f(cpu_base + CPU_PC, info.fw_base);
  2830. udelay(1000);
  2831. }
  2832. if (i >= 5) {
  2833. netdev_err(tp->dev,
  2834. "%s fails to set CPU PC, is %08x should be %08x\n",
  2835. __func__, tr32(cpu_base + CPU_PC), info.fw_base);
  2836. return -ENODEV;
  2837. }
  2838. tw32(cpu_base + CPU_STATE, 0xffffffff);
  2839. tw32_f(cpu_base + CPU_MODE, 0x00000000);
  2840. return 0;
  2841. }
  2842. /* tp->lock is held. */
  2843. static void __tg3_set_mac_addr(struct tg3 *tp, int skip_mac_1)
  2844. {
  2845. u32 addr_high, addr_low;
  2846. int i;
  2847. addr_high = ((tp->dev->dev_addr[0] << 8) |
  2848. tp->dev->dev_addr[1]);
  2849. addr_low = ((tp->dev->dev_addr[2] << 24) |
  2850. (tp->dev->dev_addr[3] << 16) |
  2851. (tp->dev->dev_addr[4] << 8) |
  2852. (tp->dev->dev_addr[5] << 0));
  2853. for (i = 0; i < 4; i++) {
  2854. if (i == 1 && skip_mac_1)
  2855. continue;
  2856. tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
  2857. tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
  2858. }
  2859. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
  2860. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
  2861. for (i = 0; i < 12; i++) {
  2862. tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
  2863. tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
  2864. }
  2865. }
  2866. addr_high = (tp->dev->dev_addr[0] +
  2867. tp->dev->dev_addr[1] +
  2868. tp->dev->dev_addr[2] +
  2869. tp->dev->dev_addr[3] +
  2870. tp->dev->dev_addr[4] +
  2871. tp->dev->dev_addr[5]) &
  2872. TX_BACKOFF_SEED_MASK;
  2873. tw32(MAC_TX_BACKOFF_SEED, addr_high);
  2874. }
  2875. static void tg3_enable_register_access(struct tg3 *tp)
  2876. {
  2877. /*
  2878. * Make sure register accesses (indirect or otherwise) will function
  2879. * correctly.
  2880. */
  2881. pci_write_config_dword(tp->pdev,
  2882. TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
  2883. }
  2884. static int tg3_power_up(struct tg3 *tp)
  2885. {
  2886. int err;
  2887. tg3_enable_register_access(tp);
  2888. err = pci_set_power_state(tp->pdev, PCI_D0);
  2889. if (!err) {
  2890. /* Switch out of Vaux if it is a NIC */
  2891. tg3_pwrsrc_switch_to_vmain(tp);
  2892. } else {
  2893. netdev_err(tp->dev, "Transition to D0 failed\n");
  2894. }
  2895. return err;
  2896. }
  2897. static int tg3_setup_phy(struct tg3 *, int);
  2898. static int tg3_power_down_prepare(struct tg3 *tp)
  2899. {
  2900. u32 misc_host_ctrl;
  2901. bool device_should_wake, do_low_power;
  2902. tg3_enable_register_access(tp);
  2903. /* Restore the CLKREQ setting. */
  2904. if (tg3_flag(tp, CLKREQ_BUG)) {
  2905. u16 lnkctl;
  2906. pci_read_config_word(tp->pdev,
  2907. pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
  2908. &lnkctl);
  2909. lnkctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
  2910. pci_write_config_word(tp->pdev,
  2911. pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
  2912. lnkctl);
  2913. }
  2914. misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
  2915. tw32(TG3PCI_MISC_HOST_CTRL,
  2916. misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
  2917. device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
  2918. tg3_flag(tp, WOL_ENABLE);
  2919. if (tg3_flag(tp, USE_PHYLIB)) {
  2920. do_low_power = false;
  2921. if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
  2922. !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
  2923. struct phy_device *phydev;
  2924. u32 phyid, advertising;
  2925. phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
  2926. tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
  2927. tp->link_config.speed = phydev->speed;
  2928. tp->link_config.duplex = phydev->duplex;
  2929. tp->link_config.autoneg = phydev->autoneg;
  2930. tp->link_config.advertising = phydev->advertising;
  2931. advertising = ADVERTISED_TP |
  2932. ADVERTISED_Pause |
  2933. ADVERTISED_Autoneg |
  2934. ADVERTISED_10baseT_Half;
  2935. if (tg3_flag(tp, ENABLE_ASF) || device_should_wake) {
  2936. if (tg3_flag(tp, WOL_SPEED_100MB))
  2937. advertising |=
  2938. ADVERTISED_100baseT_Half |
  2939. ADVERTISED_100baseT_Full |
  2940. ADVERTISED_10baseT_Full;
  2941. else
  2942. advertising |= ADVERTISED_10baseT_Full;
  2943. }
  2944. phydev->advertising = advertising;
  2945. phy_start_aneg(phydev);
  2946. phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
  2947. if (phyid != PHY_ID_BCMAC131) {
  2948. phyid &= PHY_BCM_OUI_MASK;
  2949. if (phyid == PHY_BCM_OUI_1 ||
  2950. phyid == PHY_BCM_OUI_2 ||
  2951. phyid == PHY_BCM_OUI_3)
  2952. do_low_power = true;
  2953. }
  2954. }
  2955. } else {
  2956. do_low_power = true;
  2957. if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER))
  2958. tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
  2959. if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
  2960. tg3_setup_phy(tp, 0);
  2961. }
  2962. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
  2963. u32 val;
  2964. val = tr32(GRC_VCPU_EXT_CTRL);
  2965. tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
  2966. } else if (!tg3_flag(tp, ENABLE_ASF)) {
  2967. int i;
  2968. u32 val;
  2969. for (i = 0; i < 200; i++) {
  2970. tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
  2971. if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
  2972. break;
  2973. msleep(1);
  2974. }
  2975. }
  2976. if (tg3_flag(tp, WOL_CAP))
  2977. tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
  2978. WOL_DRV_STATE_SHUTDOWN |
  2979. WOL_DRV_WOL |
  2980. WOL_SET_MAGIC_PKT);
  2981. if (device_should_wake) {
  2982. u32 mac_mode;
  2983. if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
  2984. if (do_low_power &&
  2985. !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
  2986. tg3_phy_auxctl_write(tp,
  2987. MII_TG3_AUXCTL_SHDWSEL_PWRCTL,
  2988. MII_TG3_AUXCTL_PCTL_WOL_EN |
  2989. MII_TG3_AUXCTL_PCTL_100TX_LPWR |
  2990. MII_TG3_AUXCTL_PCTL_CL_AB_TXDAC);
  2991. udelay(40);
  2992. }
  2993. if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
  2994. mac_mode = MAC_MODE_PORT_MODE_GMII;
  2995. else
  2996. mac_mode = MAC_MODE_PORT_MODE_MII;
  2997. mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
  2998. if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
  2999. ASIC_REV_5700) {
  3000. u32 speed = tg3_flag(tp, WOL_SPEED_100MB) ?
  3001. SPEED_100 : SPEED_10;
  3002. if (tg3_5700_link_polarity(tp, speed))
  3003. mac_mode |= MAC_MODE_LINK_POLARITY;
  3004. else
  3005. mac_mode &= ~MAC_MODE_LINK_POLARITY;
  3006. }
  3007. } else {
  3008. mac_mode = MAC_MODE_PORT_MODE_TBI;
  3009. }
  3010. if (!tg3_flag(tp, 5750_PLUS))
  3011. tw32(MAC_LED_CTRL, tp->led_ctrl);
  3012. mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
  3013. if ((tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS)) &&
  3014. (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)))
  3015. mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
  3016. if (tg3_flag(tp, ENABLE_APE))
  3017. mac_mode |= MAC_MODE_APE_TX_EN |
  3018. MAC_MODE_APE_RX_EN |
  3019. MAC_MODE_TDE_ENABLE;
  3020. tw32_f(MAC_MODE, mac_mode);
  3021. udelay(100);
  3022. tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
  3023. udelay(10);
  3024. }
  3025. if (!tg3_flag(tp, WOL_SPEED_100MB) &&
  3026. (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
  3027. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
  3028. u32 base_val;
  3029. base_val = tp->pci_clock_ctrl;
  3030. base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
  3031. CLOCK_CTRL_TXCLK_DISABLE);
  3032. tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
  3033. CLOCK_CTRL_PWRDOWN_PLL133, 40);
  3034. } else if (tg3_flag(tp, 5780_CLASS) ||
  3035. tg3_flag(tp, CPMU_PRESENT) ||
  3036. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
  3037. /* do nothing */
  3038. } else if (!(tg3_flag(tp, 5750_PLUS) && tg3_flag(tp, ENABLE_ASF))) {
  3039. u32 newbits1, newbits2;
  3040. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
  3041. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
  3042. newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
  3043. CLOCK_CTRL_TXCLK_DISABLE |
  3044. CLOCK_CTRL_ALTCLK);
  3045. newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
  3046. } else if (tg3_flag(tp, 5705_PLUS)) {
  3047. newbits1 = CLOCK_CTRL_625_CORE;
  3048. newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
  3049. } else {
  3050. newbits1 = CLOCK_CTRL_ALTCLK;
  3051. newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
  3052. }
  3053. tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
  3054. 40);
  3055. tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
  3056. 40);
  3057. if (!tg3_flag(tp, 5705_PLUS)) {
  3058. u32 newbits3;
  3059. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
  3060. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
  3061. newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
  3062. CLOCK_CTRL_TXCLK_DISABLE |
  3063. CLOCK_CTRL_44MHZ_CORE);
  3064. } else {
  3065. newbits3 = CLOCK_CTRL_44MHZ_CORE;
  3066. }
  3067. tw32_wait_f(TG3PCI_CLOCK_CTRL,
  3068. tp->pci_clock_ctrl | newbits3, 40);
  3069. }
  3070. }
  3071. if (!(device_should_wake) && !tg3_flag(tp, ENABLE_ASF))
  3072. tg3_power_down_phy(tp, do_low_power);
  3073. tg3_frob_aux_power(tp, true);
  3074. /* Workaround for unstable PLL clock */
  3075. if ((GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX) ||
  3076. (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX)) {
  3077. u32 val = tr32(0x7d00);
  3078. val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
  3079. tw32(0x7d00, val);
  3080. if (!tg3_flag(tp, ENABLE_ASF)) {
  3081. int err;
  3082. err = tg3_nvram_lock(tp);
  3083. tg3_halt_cpu(tp, RX_CPU_BASE);
  3084. if (!err)
  3085. tg3_nvram_unlock(tp);
  3086. }
  3087. }
  3088. tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
  3089. return 0;
  3090. }
  3091. static void tg3_power_down(struct tg3 *tp)
  3092. {
  3093. tg3_power_down_prepare(tp);
  3094. pci_wake_from_d3(tp->pdev, tg3_flag(tp, WOL_ENABLE));
  3095. pci_set_power_state(tp->pdev, PCI_D3hot);
  3096. }
  3097. static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
  3098. {
  3099. switch (val & MII_TG3_AUX_STAT_SPDMASK) {
  3100. case MII_TG3_AUX_STAT_10HALF:
  3101. *speed = SPEED_10;
  3102. *duplex = DUPLEX_HALF;
  3103. break;
  3104. case MII_TG3_AUX_STAT_10FULL:
  3105. *speed = SPEED_10;
  3106. *duplex = DUPLEX_FULL;
  3107. break;
  3108. case MII_TG3_AUX_STAT_100HALF:
  3109. *speed = SPEED_100;
  3110. *duplex = DUPLEX_HALF;
  3111. break;
  3112. case MII_TG3_AUX_STAT_100FULL:
  3113. *speed = SPEED_100;
  3114. *duplex = DUPLEX_FULL;
  3115. break;
  3116. case MII_TG3_AUX_STAT_1000HALF:
  3117. *speed = SPEED_1000;
  3118. *duplex = DUPLEX_HALF;
  3119. break;
  3120. case MII_TG3_AUX_STAT_1000FULL:
  3121. *speed = SPEED_1000;
  3122. *duplex = DUPLEX_FULL;
  3123. break;
  3124. default:
  3125. if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
  3126. *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
  3127. SPEED_10;
  3128. *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
  3129. DUPLEX_HALF;
  3130. break;
  3131. }
  3132. *speed = SPEED_UNKNOWN;
  3133. *duplex = DUPLEX_UNKNOWN;
  3134. break;
  3135. }
  3136. }
  3137. static int tg3_phy_autoneg_cfg(struct tg3 *tp, u32 advertise, u32 flowctrl)
  3138. {
  3139. int err = 0;
  3140. u32 val, new_adv;
  3141. new_adv = ADVERTISE_CSMA;
  3142. new_adv |= ethtool_adv_to_mii_adv_t(advertise) & ADVERTISE_ALL;
  3143. new_adv |= mii_advertise_flowctrl(flowctrl);
  3144. err = tg3_writephy(tp, MII_ADVERTISE, new_adv);
  3145. if (err)
  3146. goto done;
  3147. if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
  3148. new_adv = ethtool_adv_to_mii_ctrl1000_t(advertise);
  3149. if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
  3150. tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
  3151. new_adv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
  3152. err = tg3_writephy(tp, MII_CTRL1000, new_adv);
  3153. if (err)
  3154. goto done;
  3155. }
  3156. if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
  3157. goto done;
  3158. tw32(TG3_CPMU_EEE_MODE,
  3159. tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
  3160. err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
  3161. if (!err) {
  3162. u32 err2;
  3163. val = 0;
  3164. /* Advertise 100-BaseTX EEE ability */
  3165. if (advertise & ADVERTISED_100baseT_Full)
  3166. val |= MDIO_AN_EEE_ADV_100TX;
  3167. /* Advertise 1000-BaseT EEE ability */
  3168. if (advertise & ADVERTISED_1000baseT_Full)
  3169. val |= MDIO_AN_EEE_ADV_1000T;
  3170. err = tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
  3171. if (err)
  3172. val = 0;
  3173. switch (GET_ASIC_REV(tp->pci_chip_rev_id)) {
  3174. case ASIC_REV_5717:
  3175. case ASIC_REV_57765:
  3176. case ASIC_REV_57766:
  3177. case ASIC_REV_5719:
  3178. /* If we advertised any eee advertisements above... */
  3179. if (val)
  3180. val = MII_TG3_DSP_TAP26_ALNOKO |
  3181. MII_TG3_DSP_TAP26_RMRXSTO |
  3182. MII_TG3_DSP_TAP26_OPCSINPT;
  3183. tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
  3184. /* Fall through */
  3185. case ASIC_REV_5720:
  3186. if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
  3187. tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
  3188. MII_TG3_DSP_CH34TP2_HIBW01);
  3189. }
  3190. err2 = TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
  3191. if (!err)
  3192. err = err2;
  3193. }
  3194. done:
  3195. return err;
  3196. }
  3197. static void tg3_phy_copper_begin(struct tg3 *tp)
  3198. {
  3199. if (tp->link_config.autoneg == AUTONEG_ENABLE ||
  3200. (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
  3201. u32 adv, fc;
  3202. if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
  3203. adv = ADVERTISED_10baseT_Half |
  3204. ADVERTISED_10baseT_Full;
  3205. if (tg3_flag(tp, WOL_SPEED_100MB))
  3206. adv |= ADVERTISED_100baseT_Half |
  3207. ADVERTISED_100baseT_Full;
  3208. fc = FLOW_CTRL_TX | FLOW_CTRL_RX;
  3209. } else {
  3210. adv = tp->link_config.advertising;
  3211. if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
  3212. adv &= ~(ADVERTISED_1000baseT_Half |
  3213. ADVERTISED_1000baseT_Full);
  3214. fc = tp->link_config.flowctrl;
  3215. }
  3216. tg3_phy_autoneg_cfg(tp, adv, fc);
  3217. tg3_writephy(tp, MII_BMCR,
  3218. BMCR_ANENABLE | BMCR_ANRESTART);
  3219. } else {
  3220. int i;
  3221. u32 bmcr, orig_bmcr;
  3222. tp->link_config.active_speed = tp->link_config.speed;
  3223. tp->link_config.active_duplex = tp->link_config.duplex;
  3224. bmcr = 0;
  3225. switch (tp->link_config.speed) {
  3226. default:
  3227. case SPEED_10:
  3228. break;
  3229. case SPEED_100:
  3230. bmcr |= BMCR_SPEED100;
  3231. break;
  3232. case SPEED_1000:
  3233. bmcr |= BMCR_SPEED1000;
  3234. break;
  3235. }
  3236. if (tp->link_config.duplex == DUPLEX_FULL)
  3237. bmcr |= BMCR_FULLDPLX;
  3238. if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
  3239. (bmcr != orig_bmcr)) {
  3240. tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
  3241. for (i = 0; i < 1500; i++) {
  3242. u32 tmp;
  3243. udelay(10);
  3244. if (tg3_readphy(tp, MII_BMSR, &tmp) ||
  3245. tg3_readphy(tp, MII_BMSR, &tmp))
  3246. continue;
  3247. if (!(tmp & BMSR_LSTATUS)) {
  3248. udelay(40);
  3249. break;
  3250. }
  3251. }
  3252. tg3_writephy(tp, MII_BMCR, bmcr);
  3253. udelay(40);
  3254. }
  3255. }
  3256. }
  3257. static int tg3_init_5401phy_dsp(struct tg3 *tp)
  3258. {
  3259. int err;
  3260. /* Turn off tap power management. */
  3261. /* Set Extended packet length bit */
  3262. err = tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
  3263. err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
  3264. err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
  3265. err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
  3266. err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
  3267. err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
  3268. udelay(40);
  3269. return err;
  3270. }
  3271. static bool tg3_phy_copper_an_config_ok(struct tg3 *tp, u32 *lcladv)
  3272. {
  3273. u32 advmsk, tgtadv, advertising;
  3274. advertising = tp->link_config.advertising;
  3275. tgtadv = ethtool_adv_to_mii_adv_t(advertising) & ADVERTISE_ALL;
  3276. advmsk = ADVERTISE_ALL;
  3277. if (tp->link_config.active_duplex == DUPLEX_FULL) {
  3278. tgtadv |= mii_advertise_flowctrl(tp->link_config.flowctrl);
  3279. advmsk |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
  3280. }
  3281. if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
  3282. return false;
  3283. if ((*lcladv & advmsk) != tgtadv)
  3284. return false;
  3285. if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
  3286. u32 tg3_ctrl;
  3287. tgtadv = ethtool_adv_to_mii_ctrl1000_t(advertising);
  3288. if (tg3_readphy(tp, MII_CTRL1000, &tg3_ctrl))
  3289. return false;
  3290. if (tgtadv &&
  3291. (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
  3292. tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)) {
  3293. tgtadv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
  3294. tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL |
  3295. CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
  3296. } else {
  3297. tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL);
  3298. }
  3299. if (tg3_ctrl != tgtadv)
  3300. return false;
  3301. }
  3302. return true;
  3303. }
  3304. static bool tg3_phy_copper_fetch_rmtadv(struct tg3 *tp, u32 *rmtadv)
  3305. {
  3306. u32 lpeth = 0;
  3307. if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
  3308. u32 val;
  3309. if (tg3_readphy(tp, MII_STAT1000, &val))
  3310. return false;
  3311. lpeth = mii_stat1000_to_ethtool_lpa_t(val);
  3312. }
  3313. if (tg3_readphy(tp, MII_LPA, rmtadv))
  3314. return false;
  3315. lpeth |= mii_lpa_to_ethtool_lpa_t(*rmtadv);
  3316. tp->link_config.rmt_adv = lpeth;
  3317. return true;
  3318. }
  3319. static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
  3320. {
  3321. int current_link_up;
  3322. u32 bmsr, val;
  3323. u32 lcl_adv, rmt_adv;
  3324. u16 current_speed;
  3325. u8 current_duplex;
  3326. int i, err;
  3327. tw32(MAC_EVENT, 0);
  3328. tw32_f(MAC_STATUS,
  3329. (MAC_STATUS_SYNC_CHANGED |
  3330. MAC_STATUS_CFG_CHANGED |
  3331. MAC_STATUS_MI_COMPLETION |
  3332. MAC_STATUS_LNKSTATE_CHANGED));
  3333. udelay(40);
  3334. if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
  3335. tw32_f(MAC_MI_MODE,
  3336. (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
  3337. udelay(80);
  3338. }
  3339. tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, 0);
  3340. /* Some third-party PHYs need to be reset on link going
  3341. * down.
  3342. */
  3343. if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
  3344. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
  3345. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
  3346. netif_carrier_ok(tp->dev)) {
  3347. tg3_readphy(tp, MII_BMSR, &bmsr);
  3348. if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
  3349. !(bmsr & BMSR_LSTATUS))
  3350. force_reset = 1;
  3351. }
  3352. if (force_reset)
  3353. tg3_phy_reset(tp);
  3354. if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
  3355. tg3_readphy(tp, MII_BMSR, &bmsr);
  3356. if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
  3357. !tg3_flag(tp, INIT_COMPLETE))
  3358. bmsr = 0;
  3359. if (!(bmsr & BMSR_LSTATUS)) {
  3360. err = tg3_init_5401phy_dsp(tp);
  3361. if (err)
  3362. return err;
  3363. tg3_readphy(tp, MII_BMSR, &bmsr);
  3364. for (i = 0; i < 1000; i++) {
  3365. udelay(10);
  3366. if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
  3367. (bmsr & BMSR_LSTATUS)) {
  3368. udelay(40);
  3369. break;
  3370. }
  3371. }
  3372. if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
  3373. TG3_PHY_REV_BCM5401_B0 &&
  3374. !(bmsr & BMSR_LSTATUS) &&
  3375. tp->link_config.active_speed == SPEED_1000) {
  3376. err = tg3_phy_reset(tp);
  3377. if (!err)
  3378. err = tg3_init_5401phy_dsp(tp);
  3379. if (err)
  3380. return err;
  3381. }
  3382. }
  3383. } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
  3384. tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
  3385. /* 5701 {A0,B0} CRC bug workaround */
  3386. tg3_writephy(tp, 0x15, 0x0a75);
  3387. tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
  3388. tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
  3389. tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
  3390. }
  3391. /* Clear pending interrupts... */
  3392. tg3_readphy(tp, MII_TG3_ISTAT, &val);
  3393. tg3_readphy(tp, MII_TG3_ISTAT, &val);
  3394. if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
  3395. tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
  3396. else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
  3397. tg3_writephy(tp, MII_TG3_IMASK, ~0);
  3398. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
  3399. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
  3400. if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
  3401. tg3_writephy(tp, MII_TG3_EXT_CTRL,
  3402. MII_TG3_EXT_CTRL_LNK3_LED_MODE);
  3403. else
  3404. tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
  3405. }
  3406. current_link_up = 0;
  3407. current_speed = SPEED_UNKNOWN;
  3408. current_duplex = DUPLEX_UNKNOWN;
  3409. tp->phy_flags &= ~TG3_PHYFLG_MDIX_STATE;
  3410. tp->link_config.rmt_adv = 0;
  3411. if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
  3412. err = tg3_phy_auxctl_read(tp,
  3413. MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
  3414. &val);
  3415. if (!err && !(val & (1 << 10))) {
  3416. tg3_phy_auxctl_write(tp,
  3417. MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
  3418. val | (1 << 10));
  3419. goto relink;
  3420. }
  3421. }
  3422. bmsr = 0;
  3423. for (i = 0; i < 100; i++) {
  3424. tg3_readphy(tp, MII_BMSR, &bmsr);
  3425. if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
  3426. (bmsr & BMSR_LSTATUS))
  3427. break;
  3428. udelay(40);
  3429. }
  3430. if (bmsr & BMSR_LSTATUS) {
  3431. u32 aux_stat, bmcr;
  3432. tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
  3433. for (i = 0; i < 2000; i++) {
  3434. udelay(10);
  3435. if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
  3436. aux_stat)
  3437. break;
  3438. }
  3439. tg3_aux_stat_to_speed_duplex(tp, aux_stat,
  3440. &current_speed,
  3441. &current_duplex);
  3442. bmcr = 0;
  3443. for (i = 0; i < 200; i++) {
  3444. tg3_readphy(tp, MII_BMCR, &bmcr);
  3445. if (tg3_readphy(tp, MII_BMCR, &bmcr))
  3446. continue;
  3447. if (bmcr && bmcr != 0x7fff)
  3448. break;
  3449. udelay(10);
  3450. }
  3451. lcl_adv = 0;
  3452. rmt_adv = 0;
  3453. tp->link_config.active_speed = current_speed;
  3454. tp->link_config.active_duplex = current_duplex;
  3455. if (tp->link_config.autoneg == AUTONEG_ENABLE) {
  3456. if ((bmcr & BMCR_ANENABLE) &&
  3457. tg3_phy_copper_an_config_ok(tp, &lcl_adv) &&
  3458. tg3_phy_copper_fetch_rmtadv(tp, &rmt_adv))
  3459. current_link_up = 1;
  3460. } else {
  3461. if (!(bmcr & BMCR_ANENABLE) &&
  3462. tp->link_config.speed == current_speed &&
  3463. tp->link_config.duplex == current_duplex &&
  3464. tp->link_config.flowctrl ==
  3465. tp->link_config.active_flowctrl) {
  3466. current_link_up = 1;
  3467. }
  3468. }
  3469. if (current_link_up == 1 &&
  3470. tp->link_config.active_duplex == DUPLEX_FULL) {
  3471. u32 reg, bit;
  3472. if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
  3473. reg = MII_TG3_FET_GEN_STAT;
  3474. bit = MII_TG3_FET_GEN_STAT_MDIXSTAT;
  3475. } else {
  3476. reg = MII_TG3_EXT_STAT;
  3477. bit = MII_TG3_EXT_STAT_MDIX;
  3478. }
  3479. if (!tg3_readphy(tp, reg, &val) && (val & bit))
  3480. tp->phy_flags |= TG3_PHYFLG_MDIX_STATE;
  3481. tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
  3482. }
  3483. }
  3484. relink:
  3485. if (current_link_up == 0 || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
  3486. tg3_phy_copper_begin(tp);
  3487. tg3_readphy(tp, MII_BMSR, &bmsr);
  3488. if ((!tg3_readphy(tp, MII_BMSR, &bmsr) && (bmsr & BMSR_LSTATUS)) ||
  3489. (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
  3490. current_link_up = 1;
  3491. }
  3492. tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
  3493. if (current_link_up == 1) {
  3494. if (tp->link_config.active_speed == SPEED_100 ||
  3495. tp->link_config.active_speed == SPEED_10)
  3496. tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
  3497. else
  3498. tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
  3499. } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
  3500. tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
  3501. else
  3502. tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
  3503. tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
  3504. if (tp->link_config.active_duplex == DUPLEX_HALF)
  3505. tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
  3506. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
  3507. if (current_link_up == 1 &&
  3508. tg3_5700_link_polarity(tp, tp->link_config.active_speed))
  3509. tp->mac_mode |= MAC_MODE_LINK_POLARITY;
  3510. else
  3511. tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
  3512. }
  3513. /* ??? Without this setting Netgear GA302T PHY does not
  3514. * ??? send/receive packets...
  3515. */
  3516. if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
  3517. tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
  3518. tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
  3519. tw32_f(MAC_MI_MODE, tp->mi_mode);
  3520. udelay(80);
  3521. }
  3522. tw32_f(MAC_MODE, tp->mac_mode);
  3523. udelay(40);
  3524. tg3_phy_eee_adjust(tp, current_link_up);
  3525. if (tg3_flag(tp, USE_LINKCHG_REG)) {
  3526. /* Polled via timer. */
  3527. tw32_f(MAC_EVENT, 0);
  3528. } else {
  3529. tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
  3530. }
  3531. udelay(40);
  3532. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
  3533. current_link_up == 1 &&
  3534. tp->link_config.active_speed == SPEED_1000 &&
  3535. (tg3_flag(tp, PCIX_MODE) || tg3_flag(tp, PCI_HIGH_SPEED))) {
  3536. udelay(120);
  3537. tw32_f(MAC_STATUS,
  3538. (MAC_STATUS_SYNC_CHANGED |
  3539. MAC_STATUS_CFG_CHANGED));
  3540. udelay(40);
  3541. tg3_write_mem(tp,
  3542. NIC_SRAM_FIRMWARE_MBOX,
  3543. NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
  3544. }
  3545. /* Prevent send BD corruption. */
  3546. if (tg3_flag(tp, CLKREQ_BUG)) {
  3547. u16 oldlnkctl, newlnkctl;
  3548. pci_read_config_word(tp->pdev,
  3549. pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
  3550. &oldlnkctl);
  3551. if (tp->link_config.active_speed == SPEED_100 ||
  3552. tp->link_config.active_speed == SPEED_10)
  3553. newlnkctl = oldlnkctl & ~PCI_EXP_LNKCTL_CLKREQ_EN;
  3554. else
  3555. newlnkctl = oldlnkctl | PCI_EXP_LNKCTL_CLKREQ_EN;
  3556. if (newlnkctl != oldlnkctl)
  3557. pci_write_config_word(tp->pdev,
  3558. pci_pcie_cap(tp->pdev) +
  3559. PCI_EXP_LNKCTL, newlnkctl);
  3560. }
  3561. if (current_link_up != netif_carrier_ok(tp->dev)) {
  3562. if (current_link_up)
  3563. netif_carrier_on(tp->dev);
  3564. else
  3565. netif_carrier_off(tp->dev);
  3566. tg3_link_report(tp);
  3567. }
  3568. return 0;
  3569. }
  3570. struct tg3_fiber_aneginfo {
  3571. int state;
  3572. #define ANEG_STATE_UNKNOWN 0
  3573. #define ANEG_STATE_AN_ENABLE 1
  3574. #define ANEG_STATE_RESTART_INIT 2
  3575. #define ANEG_STATE_RESTART 3
  3576. #define ANEG_STATE_DISABLE_LINK_OK 4
  3577. #define ANEG_STATE_ABILITY_DETECT_INIT 5
  3578. #define ANEG_STATE_ABILITY_DETECT 6
  3579. #define ANEG_STATE_ACK_DETECT_INIT 7
  3580. #define ANEG_STATE_ACK_DETECT 8
  3581. #define ANEG_STATE_COMPLETE_ACK_INIT 9
  3582. #define ANEG_STATE_COMPLETE_ACK 10
  3583. #define ANEG_STATE_IDLE_DETECT_INIT 11
  3584. #define ANEG_STATE_IDLE_DETECT 12
  3585. #define ANEG_STATE_LINK_OK 13
  3586. #define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
  3587. #define ANEG_STATE_NEXT_PAGE_WAIT 15
  3588. u32 flags;
  3589. #define MR_AN_ENABLE 0x00000001
  3590. #define MR_RESTART_AN 0x00000002
  3591. #define MR_AN_COMPLETE 0x00000004
  3592. #define MR_PAGE_RX 0x00000008
  3593. #define MR_NP_LOADED 0x00000010
  3594. #define MR_TOGGLE_TX 0x00000020
  3595. #define MR_LP_ADV_FULL_DUPLEX 0x00000040
  3596. #define MR_LP_ADV_HALF_DUPLEX 0x00000080
  3597. #define MR_LP_ADV_SYM_PAUSE 0x00000100
  3598. #define MR_LP_ADV_ASYM_PAUSE 0x00000200
  3599. #define MR_LP_ADV_REMOTE_FAULT1 0x00000400
  3600. #define MR_LP_ADV_REMOTE_FAULT2 0x00000800
  3601. #define MR_LP_ADV_NEXT_PAGE 0x00001000
  3602. #define MR_TOGGLE_RX 0x00002000
  3603. #define MR_NP_RX 0x00004000
  3604. #define MR_LINK_OK 0x80000000
  3605. unsigned long link_time, cur_time;
  3606. u32 ability_match_cfg;
  3607. int ability_match_count;
  3608. char ability_match, idle_match, ack_match;
  3609. u32 txconfig, rxconfig;
  3610. #define ANEG_CFG_NP 0x00000080
  3611. #define ANEG_CFG_ACK 0x00000040
  3612. #define ANEG_CFG_RF2 0x00000020
  3613. #define ANEG_CFG_RF1 0x00000010
  3614. #define ANEG_CFG_PS2 0x00000001
  3615. #define ANEG_CFG_PS1 0x00008000
  3616. #define ANEG_CFG_HD 0x00004000
  3617. #define ANEG_CFG_FD 0x00002000
  3618. #define ANEG_CFG_INVAL 0x00001f06
  3619. };
  3620. #define ANEG_OK 0
  3621. #define ANEG_DONE 1
  3622. #define ANEG_TIMER_ENAB 2
  3623. #define ANEG_FAILED -1
  3624. #define ANEG_STATE_SETTLE_TIME 10000
  3625. static int tg3_fiber_aneg_smachine(struct tg3 *tp,
  3626. struct tg3_fiber_aneginfo *ap)
  3627. {
  3628. u16 flowctrl;
  3629. unsigned long delta;
  3630. u32 rx_cfg_reg;
  3631. int ret;
  3632. if (ap->state == ANEG_STATE_UNKNOWN) {
  3633. ap->rxconfig = 0;
  3634. ap->link_time = 0;
  3635. ap->cur_time = 0;
  3636. ap->ability_match_cfg = 0;
  3637. ap->ability_match_count = 0;
  3638. ap->ability_match = 0;
  3639. ap->idle_match = 0;
  3640. ap->ack_match = 0;
  3641. }
  3642. ap->cur_time++;
  3643. if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
  3644. rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
  3645. if (rx_cfg_reg != ap->ability_match_cfg) {
  3646. ap->ability_match_cfg = rx_cfg_reg;
  3647. ap->ability_match = 0;
  3648. ap->ability_match_count = 0;
  3649. } else {
  3650. if (++ap->ability_match_count > 1) {
  3651. ap->ability_match = 1;
  3652. ap->ability_match_cfg = rx_cfg_reg;
  3653. }
  3654. }
  3655. if (rx_cfg_reg & ANEG_CFG_ACK)
  3656. ap->ack_match = 1;
  3657. else
  3658. ap->ack_match = 0;
  3659. ap->idle_match = 0;
  3660. } else {
  3661. ap->idle_match = 1;
  3662. ap->ability_match_cfg = 0;
  3663. ap->ability_match_count = 0;
  3664. ap->ability_match = 0;
  3665. ap->ack_match = 0;
  3666. rx_cfg_reg = 0;
  3667. }
  3668. ap->rxconfig = rx_cfg_reg;
  3669. ret = ANEG_OK;
  3670. switch (ap->state) {
  3671. case ANEG_STATE_UNKNOWN:
  3672. if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
  3673. ap->state = ANEG_STATE_AN_ENABLE;
  3674. /* fallthru */
  3675. case ANEG_STATE_AN_ENABLE:
  3676. ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
  3677. if (ap->flags & MR_AN_ENABLE) {
  3678. ap->link_time = 0;
  3679. ap->cur_time = 0;
  3680. ap->ability_match_cfg = 0;
  3681. ap->ability_match_count = 0;
  3682. ap->ability_match = 0;
  3683. ap->idle_match = 0;
  3684. ap->ack_match = 0;
  3685. ap->state = ANEG_STATE_RESTART_INIT;
  3686. } else {
  3687. ap->state = ANEG_STATE_DISABLE_LINK_OK;
  3688. }
  3689. break;
  3690. case ANEG_STATE_RESTART_INIT:
  3691. ap->link_time = ap->cur_time;
  3692. ap->flags &= ~(MR_NP_LOADED);
  3693. ap->txconfig = 0;
  3694. tw32(MAC_TX_AUTO_NEG, 0);
  3695. tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
  3696. tw32_f(MAC_MODE, tp->mac_mode);
  3697. udelay(40);
  3698. ret = ANEG_TIMER_ENAB;
  3699. ap->state = ANEG_STATE_RESTART;
  3700. /* fallthru */
  3701. case ANEG_STATE_RESTART:
  3702. delta = ap->cur_time - ap->link_time;
  3703. if (delta > ANEG_STATE_SETTLE_TIME)
  3704. ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
  3705. else
  3706. ret = ANEG_TIMER_ENAB;
  3707. break;
  3708. case ANEG_STATE_DISABLE_LINK_OK:
  3709. ret = ANEG_DONE;
  3710. break;
  3711. case ANEG_STATE_ABILITY_DETECT_INIT:
  3712. ap->flags &= ~(MR_TOGGLE_TX);
  3713. ap->txconfig = ANEG_CFG_FD;
  3714. flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
  3715. if (flowctrl & ADVERTISE_1000XPAUSE)
  3716. ap->txconfig |= ANEG_CFG_PS1;
  3717. if (flowctrl & ADVERTISE_1000XPSE_ASYM)
  3718. ap->txconfig |= ANEG_CFG_PS2;
  3719. tw32(MAC_TX_AUTO_NEG, ap->txconfig);
  3720. tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
  3721. tw32_f(MAC_MODE, tp->mac_mode);
  3722. udelay(40);
  3723. ap->state = ANEG_STATE_ABILITY_DETECT;
  3724. break;
  3725. case ANEG_STATE_ABILITY_DETECT:
  3726. if (ap->ability_match != 0 && ap->rxconfig != 0)
  3727. ap->state = ANEG_STATE_ACK_DETECT_INIT;
  3728. break;
  3729. case ANEG_STATE_ACK_DETECT_INIT:
  3730. ap->txconfig |= ANEG_CFG_ACK;
  3731. tw32(MAC_TX_AUTO_NEG, ap->txconfig);
  3732. tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
  3733. tw32_f(MAC_MODE, tp->mac_mode);
  3734. udelay(40);
  3735. ap->state = ANEG_STATE_ACK_DETECT;
  3736. /* fallthru */
  3737. case ANEG_STATE_ACK_DETECT:
  3738. if (ap->ack_match != 0) {
  3739. if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
  3740. (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
  3741. ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
  3742. } else {
  3743. ap->state = ANEG_STATE_AN_ENABLE;
  3744. }
  3745. } else if (ap->ability_match != 0 &&
  3746. ap->rxconfig == 0) {
  3747. ap->state = ANEG_STATE_AN_ENABLE;
  3748. }
  3749. break;
  3750. case ANEG_STATE_COMPLETE_ACK_INIT:
  3751. if (ap->rxconfig & ANEG_CFG_INVAL) {
  3752. ret = ANEG_FAILED;
  3753. break;
  3754. }
  3755. ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
  3756. MR_LP_ADV_HALF_DUPLEX |
  3757. MR_LP_ADV_SYM_PAUSE |
  3758. MR_LP_ADV_ASYM_PAUSE |
  3759. MR_LP_ADV_REMOTE_FAULT1 |
  3760. MR_LP_ADV_REMOTE_FAULT2 |
  3761. MR_LP_ADV_NEXT_PAGE |
  3762. MR_TOGGLE_RX |
  3763. MR_NP_RX);
  3764. if (ap->rxconfig & ANEG_CFG_FD)
  3765. ap->flags |= MR_LP_ADV_FULL_DUPLEX;
  3766. if (ap->rxconfig & ANEG_CFG_HD)
  3767. ap->flags |= MR_LP_ADV_HALF_DUPLEX;
  3768. if (ap->rxconfig & ANEG_CFG_PS1)
  3769. ap->flags |= MR_LP_ADV_SYM_PAUSE;
  3770. if (ap->rxconfig & ANEG_CFG_PS2)
  3771. ap->flags |= MR_LP_ADV_ASYM_PAUSE;
  3772. if (ap->rxconfig & ANEG_CFG_RF1)
  3773. ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
  3774. if (ap->rxconfig & ANEG_CFG_RF2)
  3775. ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
  3776. if (ap->rxconfig & ANEG_CFG_NP)
  3777. ap->flags |= MR_LP_ADV_NEXT_PAGE;
  3778. ap->link_time = ap->cur_time;
  3779. ap->flags ^= (MR_TOGGLE_TX);
  3780. if (ap->rxconfig & 0x0008)
  3781. ap->flags |= MR_TOGGLE_RX;
  3782. if (ap->rxconfig & ANEG_CFG_NP)
  3783. ap->flags |= MR_NP_RX;
  3784. ap->flags |= MR_PAGE_RX;
  3785. ap->state = ANEG_STATE_COMPLETE_ACK;
  3786. ret = ANEG_TIMER_ENAB;
  3787. break;
  3788. case ANEG_STATE_COMPLETE_ACK:
  3789. if (ap->ability_match != 0 &&
  3790. ap->rxconfig == 0) {
  3791. ap->state = ANEG_STATE_AN_ENABLE;
  3792. break;
  3793. }
  3794. delta = ap->cur_time - ap->link_time;
  3795. if (delta > ANEG_STATE_SETTLE_TIME) {
  3796. if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
  3797. ap->state = ANEG_STATE_IDLE_DETECT_INIT;
  3798. } else {
  3799. if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
  3800. !(ap->flags & MR_NP_RX)) {
  3801. ap->state = ANEG_STATE_IDLE_DETECT_INIT;
  3802. } else {
  3803. ret = ANEG_FAILED;
  3804. }
  3805. }
  3806. }
  3807. break;
  3808. case ANEG_STATE_IDLE_DETECT_INIT:
  3809. ap->link_time = ap->cur_time;
  3810. tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
  3811. tw32_f(MAC_MODE, tp->mac_mode);
  3812. udelay(40);
  3813. ap->state = ANEG_STATE_IDLE_DETECT;
  3814. ret = ANEG_TIMER_ENAB;
  3815. break;
  3816. case ANEG_STATE_IDLE_DETECT:
  3817. if (ap->ability_match != 0 &&
  3818. ap->rxconfig == 0) {
  3819. ap->state = ANEG_STATE_AN_ENABLE;
  3820. break;
  3821. }
  3822. delta = ap->cur_time - ap->link_time;
  3823. if (delta > ANEG_STATE_SETTLE_TIME) {
  3824. /* XXX another gem from the Broadcom driver :( */
  3825. ap->state = ANEG_STATE_LINK_OK;
  3826. }
  3827. break;
  3828. case ANEG_STATE_LINK_OK:
  3829. ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
  3830. ret = ANEG_DONE;
  3831. break;
  3832. case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
  3833. /* ??? unimplemented */
  3834. break;
  3835. case ANEG_STATE_NEXT_PAGE_WAIT:
  3836. /* ??? unimplemented */
  3837. break;
  3838. default:
  3839. ret = ANEG_FAILED;
  3840. break;
  3841. }
  3842. return ret;
  3843. }
  3844. static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
  3845. {
  3846. int res = 0;
  3847. struct tg3_fiber_aneginfo aninfo;
  3848. int status = ANEG_FAILED;
  3849. unsigned int tick;
  3850. u32 tmp;
  3851. tw32_f(MAC_TX_AUTO_NEG, 0);
  3852. tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
  3853. tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
  3854. udelay(40);
  3855. tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
  3856. udelay(40);
  3857. memset(&aninfo, 0, sizeof(aninfo));
  3858. aninfo.flags |= MR_AN_ENABLE;
  3859. aninfo.state = ANEG_STATE_UNKNOWN;
  3860. aninfo.cur_time = 0;
  3861. tick = 0;
  3862. while (++tick < 195000) {
  3863. status = tg3_fiber_aneg_smachine(tp, &aninfo);
  3864. if (status == ANEG_DONE || status == ANEG_FAILED)
  3865. break;
  3866. udelay(1);
  3867. }
  3868. tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
  3869. tw32_f(MAC_MODE, tp->mac_mode);
  3870. udelay(40);
  3871. *txflags = aninfo.txconfig;
  3872. *rxflags = aninfo.flags;
  3873. if (status == ANEG_DONE &&
  3874. (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
  3875. MR_LP_ADV_FULL_DUPLEX)))
  3876. res = 1;
  3877. return res;
  3878. }
  3879. static void tg3_init_bcm8002(struct tg3 *tp)
  3880. {
  3881. u32 mac_status = tr32(MAC_STATUS);
  3882. int i;
  3883. /* Reset when initting first time or we have a link. */
  3884. if (tg3_flag(tp, INIT_COMPLETE) &&
  3885. !(mac_status & MAC_STATUS_PCS_SYNCED))
  3886. return;
  3887. /* Set PLL lock range. */
  3888. tg3_writephy(tp, 0x16, 0x8007);
  3889. /* SW reset */
  3890. tg3_writephy(tp, MII_BMCR, BMCR_RESET);
  3891. /* Wait for reset to complete. */
  3892. /* XXX schedule_timeout() ... */
  3893. for (i = 0; i < 500; i++)
  3894. udelay(10);
  3895. /* Config mode; select PMA/Ch 1 regs. */
  3896. tg3_writephy(tp, 0x10, 0x8411);
  3897. /* Enable auto-lock and comdet, select txclk for tx. */
  3898. tg3_writephy(tp, 0x11, 0x0a10);
  3899. tg3_writephy(tp, 0x18, 0x00a0);
  3900. tg3_writephy(tp, 0x16, 0x41ff);
  3901. /* Assert and deassert POR. */
  3902. tg3_writephy(tp, 0x13, 0x0400);
  3903. udelay(40);
  3904. tg3_writephy(tp, 0x13, 0x0000);
  3905. tg3_writephy(tp, 0x11, 0x0a50);
  3906. udelay(40);
  3907. tg3_writephy(tp, 0x11, 0x0a10);
  3908. /* Wait for signal to stabilize */
  3909. /* XXX schedule_timeout() ... */
  3910. for (i = 0; i < 15000; i++)
  3911. udelay(10);
  3912. /* Deselect the channel register so we can read the PHYID
  3913. * later.
  3914. */
  3915. tg3_writephy(tp, 0x10, 0x8011);
  3916. }
  3917. static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
  3918. {
  3919. u16 flowctrl;
  3920. u32 sg_dig_ctrl, sg_dig_status;
  3921. u32 serdes_cfg, expected_sg_dig_ctrl;
  3922. int workaround, port_a;
  3923. int current_link_up;
  3924. serdes_cfg = 0;
  3925. expected_sg_dig_ctrl = 0;
  3926. workaround = 0;
  3927. port_a = 1;
  3928. current_link_up = 0;
  3929. if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
  3930. tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
  3931. workaround = 1;
  3932. if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
  3933. port_a = 0;
  3934. /* preserve bits 0-11,13,14 for signal pre-emphasis */
  3935. /* preserve bits 20-23 for voltage regulator */
  3936. serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
  3937. }
  3938. sg_dig_ctrl = tr32(SG_DIG_CTRL);
  3939. if (tp->link_config.autoneg != AUTONEG_ENABLE) {
  3940. if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
  3941. if (workaround) {
  3942. u32 val = serdes_cfg;
  3943. if (port_a)
  3944. val |= 0xc010000;
  3945. else
  3946. val |= 0x4010000;
  3947. tw32_f(MAC_SERDES_CFG, val);
  3948. }
  3949. tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
  3950. }
  3951. if (mac_status & MAC_STATUS_PCS_SYNCED) {
  3952. tg3_setup_flow_control(tp, 0, 0);
  3953. current_link_up = 1;
  3954. }
  3955. goto out;
  3956. }
  3957. /* Want auto-negotiation. */
  3958. expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
  3959. flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
  3960. if (flowctrl & ADVERTISE_1000XPAUSE)
  3961. expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
  3962. if (flowctrl & ADVERTISE_1000XPSE_ASYM)
  3963. expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
  3964. if (sg_dig_ctrl != expected_sg_dig_ctrl) {
  3965. if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
  3966. tp->serdes_counter &&
  3967. ((mac_status & (MAC_STATUS_PCS_SYNCED |
  3968. MAC_STATUS_RCVD_CFG)) ==
  3969. MAC_STATUS_PCS_SYNCED)) {
  3970. tp->serdes_counter--;
  3971. current_link_up = 1;
  3972. goto out;
  3973. }
  3974. restart_autoneg:
  3975. if (workaround)
  3976. tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
  3977. tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
  3978. udelay(5);
  3979. tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
  3980. tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
  3981. tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
  3982. } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
  3983. MAC_STATUS_SIGNAL_DET)) {
  3984. sg_dig_status = tr32(SG_DIG_STATUS);
  3985. mac_status = tr32(MAC_STATUS);
  3986. if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
  3987. (mac_status & MAC_STATUS_PCS_SYNCED)) {
  3988. u32 local_adv = 0, remote_adv = 0;
  3989. if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
  3990. local_adv |= ADVERTISE_1000XPAUSE;
  3991. if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
  3992. local_adv |= ADVERTISE_1000XPSE_ASYM;
  3993. if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
  3994. remote_adv |= LPA_1000XPAUSE;
  3995. if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
  3996. remote_adv |= LPA_1000XPAUSE_ASYM;
  3997. tp->link_config.rmt_adv =
  3998. mii_adv_to_ethtool_adv_x(remote_adv);
  3999. tg3_setup_flow_control(tp, local_adv, remote_adv);
  4000. current_link_up = 1;
  4001. tp->serdes_counter = 0;
  4002. tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
  4003. } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
  4004. if (tp->serdes_counter)
  4005. tp->serdes_counter--;
  4006. else {
  4007. if (workaround) {
  4008. u32 val = serdes_cfg;
  4009. if (port_a)
  4010. val |= 0xc010000;
  4011. else
  4012. val |= 0x4010000;
  4013. tw32_f(MAC_SERDES_CFG, val);
  4014. }
  4015. tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
  4016. udelay(40);
  4017. /* Link parallel detection - link is up */
  4018. /* only if we have PCS_SYNC and not */
  4019. /* receiving config code words */
  4020. mac_status = tr32(MAC_STATUS);
  4021. if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
  4022. !(mac_status & MAC_STATUS_RCVD_CFG)) {
  4023. tg3_setup_flow_control(tp, 0, 0);
  4024. current_link_up = 1;
  4025. tp->phy_flags |=
  4026. TG3_PHYFLG_PARALLEL_DETECT;
  4027. tp->serdes_counter =
  4028. SERDES_PARALLEL_DET_TIMEOUT;
  4029. } else
  4030. goto restart_autoneg;
  4031. }
  4032. }
  4033. } else {
  4034. tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
  4035. tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
  4036. }
  4037. out:
  4038. return current_link_up;
  4039. }
  4040. static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
  4041. {
  4042. int current_link_up = 0;
  4043. if (!(mac_status & MAC_STATUS_PCS_SYNCED))
  4044. goto out;
  4045. if (tp->link_config.autoneg == AUTONEG_ENABLE) {
  4046. u32 txflags, rxflags;
  4047. int i;
  4048. if (fiber_autoneg(tp, &txflags, &rxflags)) {
  4049. u32 local_adv = 0, remote_adv = 0;
  4050. if (txflags & ANEG_CFG_PS1)
  4051. local_adv |= ADVERTISE_1000XPAUSE;
  4052. if (txflags & ANEG_CFG_PS2)
  4053. local_adv |= ADVERTISE_1000XPSE_ASYM;
  4054. if (rxflags & MR_LP_ADV_SYM_PAUSE)
  4055. remote_adv |= LPA_1000XPAUSE;
  4056. if (rxflags & MR_LP_ADV_ASYM_PAUSE)
  4057. remote_adv |= LPA_1000XPAUSE_ASYM;
  4058. tp->link_config.rmt_adv =
  4059. mii_adv_to_ethtool_adv_x(remote_adv);
  4060. tg3_setup_flow_control(tp, local_adv, remote_adv);
  4061. current_link_up = 1;
  4062. }
  4063. for (i = 0; i < 30; i++) {
  4064. udelay(20);
  4065. tw32_f(MAC_STATUS,
  4066. (MAC_STATUS_SYNC_CHANGED |
  4067. MAC_STATUS_CFG_CHANGED));
  4068. udelay(40);
  4069. if ((tr32(MAC_STATUS) &
  4070. (MAC_STATUS_SYNC_CHANGED |
  4071. MAC_STATUS_CFG_CHANGED)) == 0)
  4072. break;
  4073. }
  4074. mac_status = tr32(MAC_STATUS);
  4075. if (current_link_up == 0 &&
  4076. (mac_status & MAC_STATUS_PCS_SYNCED) &&
  4077. !(mac_status & MAC_STATUS_RCVD_CFG))
  4078. current_link_up = 1;
  4079. } else {
  4080. tg3_setup_flow_control(tp, 0, 0);
  4081. /* Forcing 1000FD link up. */
  4082. current_link_up = 1;
  4083. tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
  4084. udelay(40);
  4085. tw32_f(MAC_MODE, tp->mac_mode);
  4086. udelay(40);
  4087. }
  4088. out:
  4089. return current_link_up;
  4090. }
  4091. static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
  4092. {
  4093. u32 orig_pause_cfg;
  4094. u16 orig_active_speed;
  4095. u8 orig_active_duplex;
  4096. u32 mac_status;
  4097. int current_link_up;
  4098. int i;
  4099. orig_pause_cfg = tp->link_config.active_flowctrl;
  4100. orig_active_speed = tp->link_config.active_speed;
  4101. orig_active_duplex = tp->link_config.active_duplex;
  4102. if (!tg3_flag(tp, HW_AUTONEG) &&
  4103. netif_carrier_ok(tp->dev) &&
  4104. tg3_flag(tp, INIT_COMPLETE)) {
  4105. mac_status = tr32(MAC_STATUS);
  4106. mac_status &= (MAC_STATUS_PCS_SYNCED |
  4107. MAC_STATUS_SIGNAL_DET |
  4108. MAC_STATUS_CFG_CHANGED |
  4109. MAC_STATUS_RCVD_CFG);
  4110. if (mac_status == (MAC_STATUS_PCS_SYNCED |
  4111. MAC_STATUS_SIGNAL_DET)) {
  4112. tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
  4113. MAC_STATUS_CFG_CHANGED));
  4114. return 0;
  4115. }
  4116. }
  4117. tw32_f(MAC_TX_AUTO_NEG, 0);
  4118. tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
  4119. tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
  4120. tw32_f(MAC_MODE, tp->mac_mode);
  4121. udelay(40);
  4122. if (tp->phy_id == TG3_PHY_ID_BCM8002)
  4123. tg3_init_bcm8002(tp);
  4124. /* Enable link change event even when serdes polling. */
  4125. tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
  4126. udelay(40);
  4127. current_link_up = 0;
  4128. tp->link_config.rmt_adv = 0;
  4129. mac_status = tr32(MAC_STATUS);
  4130. if (tg3_flag(tp, HW_AUTONEG))
  4131. current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
  4132. else
  4133. current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
  4134. tp->napi[0].hw_status->status =
  4135. (SD_STATUS_UPDATED |
  4136. (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
  4137. for (i = 0; i < 100; i++) {
  4138. tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
  4139. MAC_STATUS_CFG_CHANGED));
  4140. udelay(5);
  4141. if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
  4142. MAC_STATUS_CFG_CHANGED |
  4143. MAC_STATUS_LNKSTATE_CHANGED)) == 0)
  4144. break;
  4145. }
  4146. mac_status = tr32(MAC_STATUS);
  4147. if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
  4148. current_link_up = 0;
  4149. if (tp->link_config.autoneg == AUTONEG_ENABLE &&
  4150. tp->serdes_counter == 0) {
  4151. tw32_f(MAC_MODE, (tp->mac_mode |
  4152. MAC_MODE_SEND_CONFIGS));
  4153. udelay(1);
  4154. tw32_f(MAC_MODE, tp->mac_mode);
  4155. }
  4156. }
  4157. if (current_link_up == 1) {
  4158. tp->link_config.active_speed = SPEED_1000;
  4159. tp->link_config.active_duplex = DUPLEX_FULL;
  4160. tw32(MAC_LED_CTRL, (tp->led_ctrl |
  4161. LED_CTRL_LNKLED_OVERRIDE |
  4162. LED_CTRL_1000MBPS_ON));
  4163. } else {
  4164. tp->link_config.active_speed = SPEED_UNKNOWN;
  4165. tp->link_config.active_duplex = DUPLEX_UNKNOWN;
  4166. tw32(MAC_LED_CTRL, (tp->led_ctrl |
  4167. LED_CTRL_LNKLED_OVERRIDE |
  4168. LED_CTRL_TRAFFIC_OVERRIDE));
  4169. }
  4170. if (current_link_up != netif_carrier_ok(tp->dev)) {
  4171. if (current_link_up)
  4172. netif_carrier_on(tp->dev);
  4173. else
  4174. netif_carrier_off(tp->dev);
  4175. tg3_link_report(tp);
  4176. } else {
  4177. u32 now_pause_cfg = tp->link_config.active_flowctrl;
  4178. if (orig_pause_cfg != now_pause_cfg ||
  4179. orig_active_speed != tp->link_config.active_speed ||
  4180. orig_active_duplex != tp->link_config.active_duplex)
  4181. tg3_link_report(tp);
  4182. }
  4183. return 0;
  4184. }
  4185. static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
  4186. {
  4187. int current_link_up, err = 0;
  4188. u32 bmsr, bmcr;
  4189. u16 current_speed;
  4190. u8 current_duplex;
  4191. u32 local_adv, remote_adv;
  4192. tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
  4193. tw32_f(MAC_MODE, tp->mac_mode);
  4194. udelay(40);
  4195. tw32(MAC_EVENT, 0);
  4196. tw32_f(MAC_STATUS,
  4197. (MAC_STATUS_SYNC_CHANGED |
  4198. MAC_STATUS_CFG_CHANGED |
  4199. MAC_STATUS_MI_COMPLETION |
  4200. MAC_STATUS_LNKSTATE_CHANGED));
  4201. udelay(40);
  4202. if (force_reset)
  4203. tg3_phy_reset(tp);
  4204. current_link_up = 0;
  4205. current_speed = SPEED_UNKNOWN;
  4206. current_duplex = DUPLEX_UNKNOWN;
  4207. tp->link_config.rmt_adv = 0;
  4208. err |= tg3_readphy(tp, MII_BMSR, &bmsr);
  4209. err |= tg3_readphy(tp, MII_BMSR, &bmsr);
  4210. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
  4211. if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
  4212. bmsr |= BMSR_LSTATUS;
  4213. else
  4214. bmsr &= ~BMSR_LSTATUS;
  4215. }
  4216. err |= tg3_readphy(tp, MII_BMCR, &bmcr);
  4217. if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
  4218. (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
  4219. /* do nothing, just check for link up at the end */
  4220. } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
  4221. u32 adv, newadv;
  4222. err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
  4223. newadv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
  4224. ADVERTISE_1000XPAUSE |
  4225. ADVERTISE_1000XPSE_ASYM |
  4226. ADVERTISE_SLCT);
  4227. newadv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
  4228. newadv |= ethtool_adv_to_mii_adv_x(tp->link_config.advertising);
  4229. if ((newadv != adv) || !(bmcr & BMCR_ANENABLE)) {
  4230. tg3_writephy(tp, MII_ADVERTISE, newadv);
  4231. bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
  4232. tg3_writephy(tp, MII_BMCR, bmcr);
  4233. tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
  4234. tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
  4235. tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
  4236. return err;
  4237. }
  4238. } else {
  4239. u32 new_bmcr;
  4240. bmcr &= ~BMCR_SPEED1000;
  4241. new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
  4242. if (tp->link_config.duplex == DUPLEX_FULL)
  4243. new_bmcr |= BMCR_FULLDPLX;
  4244. if (new_bmcr != bmcr) {
  4245. /* BMCR_SPEED1000 is a reserved bit that needs
  4246. * to be set on write.
  4247. */
  4248. new_bmcr |= BMCR_SPEED1000;
  4249. /* Force a linkdown */
  4250. if (netif_carrier_ok(tp->dev)) {
  4251. u32 adv;
  4252. err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
  4253. adv &= ~(ADVERTISE_1000XFULL |
  4254. ADVERTISE_1000XHALF |
  4255. ADVERTISE_SLCT);
  4256. tg3_writephy(tp, MII_ADVERTISE, adv);
  4257. tg3_writephy(tp, MII_BMCR, bmcr |
  4258. BMCR_ANRESTART |
  4259. BMCR_ANENABLE);
  4260. udelay(10);
  4261. netif_carrier_off(tp->dev);
  4262. }
  4263. tg3_writephy(tp, MII_BMCR, new_bmcr);
  4264. bmcr = new_bmcr;
  4265. err |= tg3_readphy(tp, MII_BMSR, &bmsr);
  4266. err |= tg3_readphy(tp, MII_BMSR, &bmsr);
  4267. if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
  4268. ASIC_REV_5714) {
  4269. if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
  4270. bmsr |= BMSR_LSTATUS;
  4271. else
  4272. bmsr &= ~BMSR_LSTATUS;
  4273. }
  4274. tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
  4275. }
  4276. }
  4277. if (bmsr & BMSR_LSTATUS) {
  4278. current_speed = SPEED_1000;
  4279. current_link_up = 1;
  4280. if (bmcr & BMCR_FULLDPLX)
  4281. current_duplex = DUPLEX_FULL;
  4282. else
  4283. current_duplex = DUPLEX_HALF;
  4284. local_adv = 0;
  4285. remote_adv = 0;
  4286. if (bmcr & BMCR_ANENABLE) {
  4287. u32 common;
  4288. err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
  4289. err |= tg3_readphy(tp, MII_LPA, &remote_adv);
  4290. common = local_adv & remote_adv;
  4291. if (common & (ADVERTISE_1000XHALF |
  4292. ADVERTISE_1000XFULL)) {
  4293. if (common & ADVERTISE_1000XFULL)
  4294. current_duplex = DUPLEX_FULL;
  4295. else
  4296. current_duplex = DUPLEX_HALF;
  4297. tp->link_config.rmt_adv =
  4298. mii_adv_to_ethtool_adv_x(remote_adv);
  4299. } else if (!tg3_flag(tp, 5780_CLASS)) {
  4300. /* Link is up via parallel detect */
  4301. } else {
  4302. current_link_up = 0;
  4303. }
  4304. }
  4305. }
  4306. if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
  4307. tg3_setup_flow_control(tp, local_adv, remote_adv);
  4308. tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
  4309. if (tp->link_config.active_duplex == DUPLEX_HALF)
  4310. tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
  4311. tw32_f(MAC_MODE, tp->mac_mode);
  4312. udelay(40);
  4313. tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
  4314. tp->link_config.active_speed = current_speed;
  4315. tp->link_config.active_duplex = current_duplex;
  4316. if (current_link_up != netif_carrier_ok(tp->dev)) {
  4317. if (current_link_up)
  4318. netif_carrier_on(tp->dev);
  4319. else {
  4320. netif_carrier_off(tp->dev);
  4321. tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
  4322. }
  4323. tg3_link_report(tp);
  4324. }
  4325. return err;
  4326. }
  4327. static void tg3_serdes_parallel_detect(struct tg3 *tp)
  4328. {
  4329. if (tp->serdes_counter) {
  4330. /* Give autoneg time to complete. */
  4331. tp->serdes_counter--;
  4332. return;
  4333. }
  4334. if (!netif_carrier_ok(tp->dev) &&
  4335. (tp->link_config.autoneg == AUTONEG_ENABLE)) {
  4336. u32 bmcr;
  4337. tg3_readphy(tp, MII_BMCR, &bmcr);
  4338. if (bmcr & BMCR_ANENABLE) {
  4339. u32 phy1, phy2;
  4340. /* Select shadow register 0x1f */
  4341. tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
  4342. tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
  4343. /* Select expansion interrupt status register */
  4344. tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
  4345. MII_TG3_DSP_EXP1_INT_STAT);
  4346. tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
  4347. tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
  4348. if ((phy1 & 0x10) && !(phy2 & 0x20)) {
  4349. /* We have signal detect and not receiving
  4350. * config code words, link is up by parallel
  4351. * detection.
  4352. */
  4353. bmcr &= ~BMCR_ANENABLE;
  4354. bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
  4355. tg3_writephy(tp, MII_BMCR, bmcr);
  4356. tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
  4357. }
  4358. }
  4359. } else if (netif_carrier_ok(tp->dev) &&
  4360. (tp->link_config.autoneg == AUTONEG_ENABLE) &&
  4361. (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
  4362. u32 phy2;
  4363. /* Select expansion interrupt status register */
  4364. tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
  4365. MII_TG3_DSP_EXP1_INT_STAT);
  4366. tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
  4367. if (phy2 & 0x20) {
  4368. u32 bmcr;
  4369. /* Config code words received, turn on autoneg. */
  4370. tg3_readphy(tp, MII_BMCR, &bmcr);
  4371. tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
  4372. tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
  4373. }
  4374. }
  4375. }
  4376. static int tg3_setup_phy(struct tg3 *tp, int force_reset)
  4377. {
  4378. u32 val;
  4379. int err;
  4380. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
  4381. err = tg3_setup_fiber_phy(tp, force_reset);
  4382. else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
  4383. err = tg3_setup_fiber_mii_phy(tp, force_reset);
  4384. else
  4385. err = tg3_setup_copper_phy(tp, force_reset);
  4386. if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
  4387. u32 scale;
  4388. val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
  4389. if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
  4390. scale = 65;
  4391. else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
  4392. scale = 6;
  4393. else
  4394. scale = 12;
  4395. val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
  4396. val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
  4397. tw32(GRC_MISC_CFG, val);
  4398. }
  4399. val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
  4400. (6 << TX_LENGTHS_IPG_SHIFT);
  4401. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
  4402. val |= tr32(MAC_TX_LENGTHS) &
  4403. (TX_LENGTHS_JMB_FRM_LEN_MSK |
  4404. TX_LENGTHS_CNT_DWN_VAL_MSK);
  4405. if (tp->link_config.active_speed == SPEED_1000 &&
  4406. tp->link_config.active_duplex == DUPLEX_HALF)
  4407. tw32(MAC_TX_LENGTHS, val |
  4408. (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
  4409. else
  4410. tw32(MAC_TX_LENGTHS, val |
  4411. (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
  4412. if (!tg3_flag(tp, 5705_PLUS)) {
  4413. if (netif_carrier_ok(tp->dev)) {
  4414. tw32(HOSTCC_STAT_COAL_TICKS,
  4415. tp->coal.stats_block_coalesce_usecs);
  4416. } else {
  4417. tw32(HOSTCC_STAT_COAL_TICKS, 0);
  4418. }
  4419. }
  4420. if (tg3_flag(tp, ASPM_WORKAROUND)) {
  4421. val = tr32(PCIE_PWR_MGMT_THRESH);
  4422. if (!netif_carrier_ok(tp->dev))
  4423. val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
  4424. tp->pwrmgmt_thresh;
  4425. else
  4426. val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
  4427. tw32(PCIE_PWR_MGMT_THRESH, val);
  4428. }
  4429. return err;
  4430. }
  4431. static inline int tg3_irq_sync(struct tg3 *tp)
  4432. {
  4433. return tp->irq_sync;
  4434. }
  4435. static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
  4436. {
  4437. int i;
  4438. dst = (u32 *)((u8 *)dst + off);
  4439. for (i = 0; i < len; i += sizeof(u32))
  4440. *dst++ = tr32(off + i);
  4441. }
  4442. static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
  4443. {
  4444. tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
  4445. tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
  4446. tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
  4447. tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
  4448. tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
  4449. tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
  4450. tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
  4451. tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
  4452. tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
  4453. tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
  4454. tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
  4455. tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
  4456. tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
  4457. tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
  4458. tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
  4459. tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
  4460. tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
  4461. tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
  4462. tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
  4463. if (tg3_flag(tp, SUPPORT_MSIX))
  4464. tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
  4465. tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
  4466. tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
  4467. tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
  4468. tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
  4469. tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
  4470. tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
  4471. tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
  4472. tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
  4473. if (!tg3_flag(tp, 5705_PLUS)) {
  4474. tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
  4475. tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
  4476. tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
  4477. }
  4478. tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
  4479. tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
  4480. tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
  4481. tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
  4482. tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
  4483. if (tg3_flag(tp, NVRAM))
  4484. tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
  4485. }
  4486. static void tg3_dump_state(struct tg3 *tp)
  4487. {
  4488. int i;
  4489. u32 *regs;
  4490. regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
  4491. if (!regs) {
  4492. netdev_err(tp->dev, "Failed allocating register dump buffer\n");
  4493. return;
  4494. }
  4495. if (tg3_flag(tp, PCI_EXPRESS)) {
  4496. /* Read up to but not including private PCI registers */
  4497. for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
  4498. regs[i / sizeof(u32)] = tr32(i);
  4499. } else
  4500. tg3_dump_legacy_regs(tp, regs);
  4501. for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
  4502. if (!regs[i + 0] && !regs[i + 1] &&
  4503. !regs[i + 2] && !regs[i + 3])
  4504. continue;
  4505. netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
  4506. i * 4,
  4507. regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
  4508. }
  4509. kfree(regs);
  4510. for (i = 0; i < tp->irq_cnt; i++) {
  4511. struct tg3_napi *tnapi = &tp->napi[i];
  4512. /* SW status block */
  4513. netdev_err(tp->dev,
  4514. "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
  4515. i,
  4516. tnapi->hw_status->status,
  4517. tnapi->hw_status->status_tag,
  4518. tnapi->hw_status->rx_jumbo_consumer,
  4519. tnapi->hw_status->rx_consumer,
  4520. tnapi->hw_status->rx_mini_consumer,
  4521. tnapi->hw_status->idx[0].rx_producer,
  4522. tnapi->hw_status->idx[0].tx_consumer);
  4523. netdev_err(tp->dev,
  4524. "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
  4525. i,
  4526. tnapi->last_tag, tnapi->last_irq_tag,
  4527. tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
  4528. tnapi->rx_rcb_ptr,
  4529. tnapi->prodring.rx_std_prod_idx,
  4530. tnapi->prodring.rx_std_cons_idx,
  4531. tnapi->prodring.rx_jmb_prod_idx,
  4532. tnapi->prodring.rx_jmb_cons_idx);
  4533. }
  4534. }
  4535. /* This is called whenever we suspect that the system chipset is re-
  4536. * ordering the sequence of MMIO to the tx send mailbox. The symptom
  4537. * is bogus tx completions. We try to recover by setting the
  4538. * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
  4539. * in the workqueue.
  4540. */
  4541. static void tg3_tx_recover(struct tg3 *tp)
  4542. {
  4543. BUG_ON(tg3_flag(tp, MBOX_WRITE_REORDER) ||
  4544. tp->write32_tx_mbox == tg3_write_indirect_mbox);
  4545. netdev_warn(tp->dev,
  4546. "The system may be re-ordering memory-mapped I/O "
  4547. "cycles to the network device, attempting to recover. "
  4548. "Please report the problem to the driver maintainer "
  4549. "and include system chipset information.\n");
  4550. spin_lock(&tp->lock);
  4551. tg3_flag_set(tp, TX_RECOVERY_PENDING);
  4552. spin_unlock(&tp->lock);
  4553. }
  4554. static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
  4555. {
  4556. /* Tell compiler to fetch tx indices from memory. */
  4557. barrier();
  4558. return tnapi->tx_pending -
  4559. ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
  4560. }
  4561. /* Tigon3 never reports partial packet sends. So we do not
  4562. * need special logic to handle SKBs that have not had all
  4563. * of their frags sent yet, like SunGEM does.
  4564. */
  4565. static void tg3_tx(struct tg3_napi *tnapi)
  4566. {
  4567. struct tg3 *tp = tnapi->tp;
  4568. u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
  4569. u32 sw_idx = tnapi->tx_cons;
  4570. struct netdev_queue *txq;
  4571. int index = tnapi - tp->napi;
  4572. unsigned int pkts_compl = 0, bytes_compl = 0;
  4573. if (tg3_flag(tp, ENABLE_TSS))
  4574. index--;
  4575. txq = netdev_get_tx_queue(tp->dev, index);
  4576. while (sw_idx != hw_idx) {
  4577. struct tg3_tx_ring_info *ri = &tnapi->tx_buffers[sw_idx];
  4578. struct sk_buff *skb = ri->skb;
  4579. int i, tx_bug = 0;
  4580. if (unlikely(skb == NULL)) {
  4581. tg3_tx_recover(tp);
  4582. return;
  4583. }
  4584. pci_unmap_single(tp->pdev,
  4585. dma_unmap_addr(ri, mapping),
  4586. skb_headlen(skb),
  4587. PCI_DMA_TODEVICE);
  4588. ri->skb = NULL;
  4589. while (ri->fragmented) {
  4590. ri->fragmented = false;
  4591. sw_idx = NEXT_TX(sw_idx);
  4592. ri = &tnapi->tx_buffers[sw_idx];
  4593. }
  4594. sw_idx = NEXT_TX(sw_idx);
  4595. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  4596. ri = &tnapi->tx_buffers[sw_idx];
  4597. if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
  4598. tx_bug = 1;
  4599. pci_unmap_page(tp->pdev,
  4600. dma_unmap_addr(ri, mapping),
  4601. skb_frag_size(&skb_shinfo(skb)->frags[i]),
  4602. PCI_DMA_TODEVICE);
  4603. while (ri->fragmented) {
  4604. ri->fragmented = false;
  4605. sw_idx = NEXT_TX(sw_idx);
  4606. ri = &tnapi->tx_buffers[sw_idx];
  4607. }
  4608. sw_idx = NEXT_TX(sw_idx);
  4609. }
  4610. pkts_compl++;
  4611. bytes_compl += skb->len;
  4612. dev_kfree_skb(skb);
  4613. if (unlikely(tx_bug)) {
  4614. tg3_tx_recover(tp);
  4615. return;
  4616. }
  4617. }
  4618. netdev_tx_completed_queue(txq, pkts_compl, bytes_compl);
  4619. tnapi->tx_cons = sw_idx;
  4620. /* Need to make the tx_cons update visible to tg3_start_xmit()
  4621. * before checking for netif_queue_stopped(). Without the
  4622. * memory barrier, there is a small possibility that tg3_start_xmit()
  4623. * will miss it and cause the queue to be stopped forever.
  4624. */
  4625. smp_mb();
  4626. if (unlikely(netif_tx_queue_stopped(txq) &&
  4627. (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
  4628. __netif_tx_lock(txq, smp_processor_id());
  4629. if (netif_tx_queue_stopped(txq) &&
  4630. (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
  4631. netif_tx_wake_queue(txq);
  4632. __netif_tx_unlock(txq);
  4633. }
  4634. }
  4635. static void tg3_frag_free(bool is_frag, void *data)
  4636. {
  4637. if (is_frag)
  4638. put_page(virt_to_head_page(data));
  4639. else
  4640. kfree(data);
  4641. }
  4642. static void tg3_rx_data_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
  4643. {
  4644. unsigned int skb_size = SKB_DATA_ALIGN(map_sz + TG3_RX_OFFSET(tp)) +
  4645. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  4646. if (!ri->data)
  4647. return;
  4648. pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
  4649. map_sz, PCI_DMA_FROMDEVICE);
  4650. tg3_frag_free(skb_size <= PAGE_SIZE, ri->data);
  4651. ri->data = NULL;
  4652. }
  4653. /* Returns size of skb allocated or < 0 on error.
  4654. *
  4655. * We only need to fill in the address because the other members
  4656. * of the RX descriptor are invariant, see tg3_init_rings.
  4657. *
  4658. * Note the purposeful assymetry of cpu vs. chip accesses. For
  4659. * posting buffers we only dirty the first cache line of the RX
  4660. * descriptor (containing the address). Whereas for the RX status
  4661. * buffers the cpu only reads the last cacheline of the RX descriptor
  4662. * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
  4663. */
  4664. static int tg3_alloc_rx_data(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
  4665. u32 opaque_key, u32 dest_idx_unmasked,
  4666. unsigned int *frag_size)
  4667. {
  4668. struct tg3_rx_buffer_desc *desc;
  4669. struct ring_info *map;
  4670. u8 *data;
  4671. dma_addr_t mapping;
  4672. int skb_size, data_size, dest_idx;
  4673. switch (opaque_key) {
  4674. case RXD_OPAQUE_RING_STD:
  4675. dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
  4676. desc = &tpr->rx_std[dest_idx];
  4677. map = &tpr->rx_std_buffers[dest_idx];
  4678. data_size = tp->rx_pkt_map_sz;
  4679. break;
  4680. case RXD_OPAQUE_RING_JUMBO:
  4681. dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
  4682. desc = &tpr->rx_jmb[dest_idx].std;
  4683. map = &tpr->rx_jmb_buffers[dest_idx];
  4684. data_size = TG3_RX_JMB_MAP_SZ;
  4685. break;
  4686. default:
  4687. return -EINVAL;
  4688. }
  4689. /* Do not overwrite any of the map or rp information
  4690. * until we are sure we can commit to a new buffer.
  4691. *
  4692. * Callers depend upon this behavior and assume that
  4693. * we leave everything unchanged if we fail.
  4694. */
  4695. skb_size = SKB_DATA_ALIGN(data_size + TG3_RX_OFFSET(tp)) +
  4696. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  4697. if (skb_size <= PAGE_SIZE) {
  4698. data = netdev_alloc_frag(skb_size);
  4699. *frag_size = skb_size;
  4700. } else {
  4701. data = kmalloc(skb_size, GFP_ATOMIC);
  4702. *frag_size = 0;
  4703. }
  4704. if (!data)
  4705. return -ENOMEM;
  4706. mapping = pci_map_single(tp->pdev,
  4707. data + TG3_RX_OFFSET(tp),
  4708. data_size,
  4709. PCI_DMA_FROMDEVICE);
  4710. if (unlikely(pci_dma_mapping_error(tp->pdev, mapping))) {
  4711. tg3_frag_free(skb_size <= PAGE_SIZE, data);
  4712. return -EIO;
  4713. }
  4714. map->data = data;
  4715. dma_unmap_addr_set(map, mapping, mapping);
  4716. desc->addr_hi = ((u64)mapping >> 32);
  4717. desc->addr_lo = ((u64)mapping & 0xffffffff);
  4718. return data_size;
  4719. }
  4720. /* We only need to move over in the address because the other
  4721. * members of the RX descriptor are invariant. See notes above
  4722. * tg3_alloc_rx_data for full details.
  4723. */
  4724. static void tg3_recycle_rx(struct tg3_napi *tnapi,
  4725. struct tg3_rx_prodring_set *dpr,
  4726. u32 opaque_key, int src_idx,
  4727. u32 dest_idx_unmasked)
  4728. {
  4729. struct tg3 *tp = tnapi->tp;
  4730. struct tg3_rx_buffer_desc *src_desc, *dest_desc;
  4731. struct ring_info *src_map, *dest_map;
  4732. struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
  4733. int dest_idx;
  4734. switch (opaque_key) {
  4735. case RXD_OPAQUE_RING_STD:
  4736. dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
  4737. dest_desc = &dpr->rx_std[dest_idx];
  4738. dest_map = &dpr->rx_std_buffers[dest_idx];
  4739. src_desc = &spr->rx_std[src_idx];
  4740. src_map = &spr->rx_std_buffers[src_idx];
  4741. break;
  4742. case RXD_OPAQUE_RING_JUMBO:
  4743. dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
  4744. dest_desc = &dpr->rx_jmb[dest_idx].std;
  4745. dest_map = &dpr->rx_jmb_buffers[dest_idx];
  4746. src_desc = &spr->rx_jmb[src_idx].std;
  4747. src_map = &spr->rx_jmb_buffers[src_idx];
  4748. break;
  4749. default:
  4750. return;
  4751. }
  4752. dest_map->data = src_map->data;
  4753. dma_unmap_addr_set(dest_map, mapping,
  4754. dma_unmap_addr(src_map, mapping));
  4755. dest_desc->addr_hi = src_desc->addr_hi;
  4756. dest_desc->addr_lo = src_desc->addr_lo;
  4757. /* Ensure that the update to the skb happens after the physical
  4758. * addresses have been transferred to the new BD location.
  4759. */
  4760. smp_wmb();
  4761. src_map->data = NULL;
  4762. }
  4763. /* The RX ring scheme is composed of multiple rings which post fresh
  4764. * buffers to the chip, and one special ring the chip uses to report
  4765. * status back to the host.
  4766. *
  4767. * The special ring reports the status of received packets to the
  4768. * host. The chip does not write into the original descriptor the
  4769. * RX buffer was obtained from. The chip simply takes the original
  4770. * descriptor as provided by the host, updates the status and length
  4771. * field, then writes this into the next status ring entry.
  4772. *
  4773. * Each ring the host uses to post buffers to the chip is described
  4774. * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
  4775. * it is first placed into the on-chip ram. When the packet's length
  4776. * is known, it walks down the TG3_BDINFO entries to select the ring.
  4777. * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
  4778. * which is within the range of the new packet's length is chosen.
  4779. *
  4780. * The "separate ring for rx status" scheme may sound queer, but it makes
  4781. * sense from a cache coherency perspective. If only the host writes
  4782. * to the buffer post rings, and only the chip writes to the rx status
  4783. * rings, then cache lines never move beyond shared-modified state.
  4784. * If both the host and chip were to write into the same ring, cache line
  4785. * eviction could occur since both entities want it in an exclusive state.
  4786. */
  4787. static int tg3_rx(struct tg3_napi *tnapi, int budget)
  4788. {
  4789. struct tg3 *tp = tnapi->tp;
  4790. u32 work_mask, rx_std_posted = 0;
  4791. u32 std_prod_idx, jmb_prod_idx;
  4792. u32 sw_idx = tnapi->rx_rcb_ptr;
  4793. u16 hw_idx;
  4794. int received;
  4795. struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
  4796. hw_idx = *(tnapi->rx_rcb_prod_idx);
  4797. /*
  4798. * We need to order the read of hw_idx and the read of
  4799. * the opaque cookie.
  4800. */
  4801. rmb();
  4802. work_mask = 0;
  4803. received = 0;
  4804. std_prod_idx = tpr->rx_std_prod_idx;
  4805. jmb_prod_idx = tpr->rx_jmb_prod_idx;
  4806. while (sw_idx != hw_idx && budget > 0) {
  4807. struct ring_info *ri;
  4808. struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
  4809. unsigned int len;
  4810. struct sk_buff *skb;
  4811. dma_addr_t dma_addr;
  4812. u32 opaque_key, desc_idx, *post_ptr;
  4813. u8 *data;
  4814. desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
  4815. opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
  4816. if (opaque_key == RXD_OPAQUE_RING_STD) {
  4817. ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
  4818. dma_addr = dma_unmap_addr(ri, mapping);
  4819. data = ri->data;
  4820. post_ptr = &std_prod_idx;
  4821. rx_std_posted++;
  4822. } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
  4823. ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
  4824. dma_addr = dma_unmap_addr(ri, mapping);
  4825. data = ri->data;
  4826. post_ptr = &jmb_prod_idx;
  4827. } else
  4828. goto next_pkt_nopost;
  4829. work_mask |= opaque_key;
  4830. if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
  4831. (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
  4832. drop_it:
  4833. tg3_recycle_rx(tnapi, tpr, opaque_key,
  4834. desc_idx, *post_ptr);
  4835. drop_it_no_recycle:
  4836. /* Other statistics kept track of by card. */
  4837. tp->rx_dropped++;
  4838. goto next_pkt;
  4839. }
  4840. prefetch(data + TG3_RX_OFFSET(tp));
  4841. len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
  4842. ETH_FCS_LEN;
  4843. if (len > TG3_RX_COPY_THRESH(tp)) {
  4844. int skb_size;
  4845. unsigned int frag_size;
  4846. skb_size = tg3_alloc_rx_data(tp, tpr, opaque_key,
  4847. *post_ptr, &frag_size);
  4848. if (skb_size < 0)
  4849. goto drop_it;
  4850. pci_unmap_single(tp->pdev, dma_addr, skb_size,
  4851. PCI_DMA_FROMDEVICE);
  4852. skb = build_skb(data, frag_size);
  4853. if (!skb) {
  4854. tg3_frag_free(frag_size != 0, data);
  4855. goto drop_it_no_recycle;
  4856. }
  4857. skb_reserve(skb, TG3_RX_OFFSET(tp));
  4858. /* Ensure that the update to the data happens
  4859. * after the usage of the old DMA mapping.
  4860. */
  4861. smp_wmb();
  4862. ri->data = NULL;
  4863. } else {
  4864. tg3_recycle_rx(tnapi, tpr, opaque_key,
  4865. desc_idx, *post_ptr);
  4866. skb = netdev_alloc_skb(tp->dev,
  4867. len + TG3_RAW_IP_ALIGN);
  4868. if (skb == NULL)
  4869. goto drop_it_no_recycle;
  4870. skb_reserve(skb, TG3_RAW_IP_ALIGN);
  4871. pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
  4872. memcpy(skb->data,
  4873. data + TG3_RX_OFFSET(tp),
  4874. len);
  4875. pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
  4876. }
  4877. skb_put(skb, len);
  4878. if ((tp->dev->features & NETIF_F_RXCSUM) &&
  4879. (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
  4880. (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
  4881. >> RXD_TCPCSUM_SHIFT) == 0xffff))
  4882. skb->ip_summed = CHECKSUM_UNNECESSARY;
  4883. else
  4884. skb_checksum_none_assert(skb);
  4885. skb->protocol = eth_type_trans(skb, tp->dev);
  4886. if (len > (tp->dev->mtu + ETH_HLEN) &&
  4887. skb->protocol != htons(ETH_P_8021Q)) {
  4888. dev_kfree_skb(skb);
  4889. goto drop_it_no_recycle;
  4890. }
  4891. if (desc->type_flags & RXD_FLAG_VLAN &&
  4892. !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
  4893. __vlan_hwaccel_put_tag(skb,
  4894. desc->err_vlan & RXD_VLAN_MASK);
  4895. napi_gro_receive(&tnapi->napi, skb);
  4896. received++;
  4897. budget--;
  4898. next_pkt:
  4899. (*post_ptr)++;
  4900. if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
  4901. tpr->rx_std_prod_idx = std_prod_idx &
  4902. tp->rx_std_ring_mask;
  4903. tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
  4904. tpr->rx_std_prod_idx);
  4905. work_mask &= ~RXD_OPAQUE_RING_STD;
  4906. rx_std_posted = 0;
  4907. }
  4908. next_pkt_nopost:
  4909. sw_idx++;
  4910. sw_idx &= tp->rx_ret_ring_mask;
  4911. /* Refresh hw_idx to see if there is new work */
  4912. if (sw_idx == hw_idx) {
  4913. hw_idx = *(tnapi->rx_rcb_prod_idx);
  4914. rmb();
  4915. }
  4916. }
  4917. /* ACK the status ring. */
  4918. tnapi->rx_rcb_ptr = sw_idx;
  4919. tw32_rx_mbox(tnapi->consmbox, sw_idx);
  4920. /* Refill RX ring(s). */
  4921. if (!tg3_flag(tp, ENABLE_RSS)) {
  4922. /* Sync BD data before updating mailbox */
  4923. wmb();
  4924. if (work_mask & RXD_OPAQUE_RING_STD) {
  4925. tpr->rx_std_prod_idx = std_prod_idx &
  4926. tp->rx_std_ring_mask;
  4927. tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
  4928. tpr->rx_std_prod_idx);
  4929. }
  4930. if (work_mask & RXD_OPAQUE_RING_JUMBO) {
  4931. tpr->rx_jmb_prod_idx = jmb_prod_idx &
  4932. tp->rx_jmb_ring_mask;
  4933. tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
  4934. tpr->rx_jmb_prod_idx);
  4935. }
  4936. mmiowb();
  4937. } else if (work_mask) {
  4938. /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
  4939. * updated before the producer indices can be updated.
  4940. */
  4941. smp_wmb();
  4942. tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
  4943. tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
  4944. if (tnapi != &tp->napi[1]) {
  4945. tp->rx_refill = true;
  4946. napi_schedule(&tp->napi[1].napi);
  4947. }
  4948. }
  4949. return received;
  4950. }
  4951. static void tg3_poll_link(struct tg3 *tp)
  4952. {
  4953. /* handle link change and other phy events */
  4954. if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
  4955. struct tg3_hw_status *sblk = tp->napi[0].hw_status;
  4956. if (sblk->status & SD_STATUS_LINK_CHG) {
  4957. sblk->status = SD_STATUS_UPDATED |
  4958. (sblk->status & ~SD_STATUS_LINK_CHG);
  4959. spin_lock(&tp->lock);
  4960. if (tg3_flag(tp, USE_PHYLIB)) {
  4961. tw32_f(MAC_STATUS,
  4962. (MAC_STATUS_SYNC_CHANGED |
  4963. MAC_STATUS_CFG_CHANGED |
  4964. MAC_STATUS_MI_COMPLETION |
  4965. MAC_STATUS_LNKSTATE_CHANGED));
  4966. udelay(40);
  4967. } else
  4968. tg3_setup_phy(tp, 0);
  4969. spin_unlock(&tp->lock);
  4970. }
  4971. }
  4972. }
  4973. static int tg3_rx_prodring_xfer(struct tg3 *tp,
  4974. struct tg3_rx_prodring_set *dpr,
  4975. struct tg3_rx_prodring_set *spr)
  4976. {
  4977. u32 si, di, cpycnt, src_prod_idx;
  4978. int i, err = 0;
  4979. while (1) {
  4980. src_prod_idx = spr->rx_std_prod_idx;
  4981. /* Make sure updates to the rx_std_buffers[] entries and the
  4982. * standard producer index are seen in the correct order.
  4983. */
  4984. smp_rmb();
  4985. if (spr->rx_std_cons_idx == src_prod_idx)
  4986. break;
  4987. if (spr->rx_std_cons_idx < src_prod_idx)
  4988. cpycnt = src_prod_idx - spr->rx_std_cons_idx;
  4989. else
  4990. cpycnt = tp->rx_std_ring_mask + 1 -
  4991. spr->rx_std_cons_idx;
  4992. cpycnt = min(cpycnt,
  4993. tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
  4994. si = spr->rx_std_cons_idx;
  4995. di = dpr->rx_std_prod_idx;
  4996. for (i = di; i < di + cpycnt; i++) {
  4997. if (dpr->rx_std_buffers[i].data) {
  4998. cpycnt = i - di;
  4999. err = -ENOSPC;
  5000. break;
  5001. }
  5002. }
  5003. if (!cpycnt)
  5004. break;
  5005. /* Ensure that updates to the rx_std_buffers ring and the
  5006. * shadowed hardware producer ring from tg3_recycle_skb() are
  5007. * ordered correctly WRT the skb check above.
  5008. */
  5009. smp_rmb();
  5010. memcpy(&dpr->rx_std_buffers[di],
  5011. &spr->rx_std_buffers[si],
  5012. cpycnt * sizeof(struct ring_info));
  5013. for (i = 0; i < cpycnt; i++, di++, si++) {
  5014. struct tg3_rx_buffer_desc *sbd, *dbd;
  5015. sbd = &spr->rx_std[si];
  5016. dbd = &dpr->rx_std[di];
  5017. dbd->addr_hi = sbd->addr_hi;
  5018. dbd->addr_lo = sbd->addr_lo;
  5019. }
  5020. spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
  5021. tp->rx_std_ring_mask;
  5022. dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
  5023. tp->rx_std_ring_mask;
  5024. }
  5025. while (1) {
  5026. src_prod_idx = spr->rx_jmb_prod_idx;
  5027. /* Make sure updates to the rx_jmb_buffers[] entries and
  5028. * the jumbo producer index are seen in the correct order.
  5029. */
  5030. smp_rmb();
  5031. if (spr->rx_jmb_cons_idx == src_prod_idx)
  5032. break;
  5033. if (spr->rx_jmb_cons_idx < src_prod_idx)
  5034. cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
  5035. else
  5036. cpycnt = tp->rx_jmb_ring_mask + 1 -
  5037. spr->rx_jmb_cons_idx;
  5038. cpycnt = min(cpycnt,
  5039. tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
  5040. si = spr->rx_jmb_cons_idx;
  5041. di = dpr->rx_jmb_prod_idx;
  5042. for (i = di; i < di + cpycnt; i++) {
  5043. if (dpr->rx_jmb_buffers[i].data) {
  5044. cpycnt = i - di;
  5045. err = -ENOSPC;
  5046. break;
  5047. }
  5048. }
  5049. if (!cpycnt)
  5050. break;
  5051. /* Ensure that updates to the rx_jmb_buffers ring and the
  5052. * shadowed hardware producer ring from tg3_recycle_skb() are
  5053. * ordered correctly WRT the skb check above.
  5054. */
  5055. smp_rmb();
  5056. memcpy(&dpr->rx_jmb_buffers[di],
  5057. &spr->rx_jmb_buffers[si],
  5058. cpycnt * sizeof(struct ring_info));
  5059. for (i = 0; i < cpycnt; i++, di++, si++) {
  5060. struct tg3_rx_buffer_desc *sbd, *dbd;
  5061. sbd = &spr->rx_jmb[si].std;
  5062. dbd = &dpr->rx_jmb[di].std;
  5063. dbd->addr_hi = sbd->addr_hi;
  5064. dbd->addr_lo = sbd->addr_lo;
  5065. }
  5066. spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
  5067. tp->rx_jmb_ring_mask;
  5068. dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
  5069. tp->rx_jmb_ring_mask;
  5070. }
  5071. return err;
  5072. }
  5073. static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
  5074. {
  5075. struct tg3 *tp = tnapi->tp;
  5076. /* run TX completion thread */
  5077. if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
  5078. tg3_tx(tnapi);
  5079. if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
  5080. return work_done;
  5081. }
  5082. if (!tnapi->rx_rcb_prod_idx)
  5083. return work_done;
  5084. /* run RX thread, within the bounds set by NAPI.
  5085. * All RX "locking" is done by ensuring outside
  5086. * code synchronizes with tg3->napi.poll()
  5087. */
  5088. if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
  5089. work_done += tg3_rx(tnapi, budget - work_done);
  5090. if (tg3_flag(tp, ENABLE_RSS) && tnapi == &tp->napi[1]) {
  5091. struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
  5092. int i, err = 0;
  5093. u32 std_prod_idx = dpr->rx_std_prod_idx;
  5094. u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
  5095. tp->rx_refill = false;
  5096. for (i = 1; i < tp->irq_cnt; i++)
  5097. err |= tg3_rx_prodring_xfer(tp, dpr,
  5098. &tp->napi[i].prodring);
  5099. wmb();
  5100. if (std_prod_idx != dpr->rx_std_prod_idx)
  5101. tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
  5102. dpr->rx_std_prod_idx);
  5103. if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
  5104. tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
  5105. dpr->rx_jmb_prod_idx);
  5106. mmiowb();
  5107. if (err)
  5108. tw32_f(HOSTCC_MODE, tp->coal_now);
  5109. }
  5110. return work_done;
  5111. }
  5112. static inline void tg3_reset_task_schedule(struct tg3 *tp)
  5113. {
  5114. if (!test_and_set_bit(TG3_FLAG_RESET_TASK_PENDING, tp->tg3_flags))
  5115. schedule_work(&tp->reset_task);
  5116. }
  5117. static inline void tg3_reset_task_cancel(struct tg3 *tp)
  5118. {
  5119. cancel_work_sync(&tp->reset_task);
  5120. tg3_flag_clear(tp, RESET_TASK_PENDING);
  5121. tg3_flag_clear(tp, TX_RECOVERY_PENDING);
  5122. }
  5123. static int tg3_poll_msix(struct napi_struct *napi, int budget)
  5124. {
  5125. struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
  5126. struct tg3 *tp = tnapi->tp;
  5127. int work_done = 0;
  5128. struct tg3_hw_status *sblk = tnapi->hw_status;
  5129. while (1) {
  5130. work_done = tg3_poll_work(tnapi, work_done, budget);
  5131. if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
  5132. goto tx_recovery;
  5133. if (unlikely(work_done >= budget))
  5134. break;
  5135. /* tp->last_tag is used in tg3_int_reenable() below
  5136. * to tell the hw how much work has been processed,
  5137. * so we must read it before checking for more work.
  5138. */
  5139. tnapi->last_tag = sblk->status_tag;
  5140. tnapi->last_irq_tag = tnapi->last_tag;
  5141. rmb();
  5142. /* check for RX/TX work to do */
  5143. if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
  5144. *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
  5145. /* This test here is not race free, but will reduce
  5146. * the number of interrupts by looping again.
  5147. */
  5148. if (tnapi == &tp->napi[1] && tp->rx_refill)
  5149. continue;
  5150. napi_complete(napi);
  5151. /* Reenable interrupts. */
  5152. tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
  5153. /* This test here is synchronized by napi_schedule()
  5154. * and napi_complete() to close the race condition.
  5155. */
  5156. if (unlikely(tnapi == &tp->napi[1] && tp->rx_refill)) {
  5157. tw32(HOSTCC_MODE, tp->coalesce_mode |
  5158. HOSTCC_MODE_ENABLE |
  5159. tnapi->coal_now);
  5160. }
  5161. mmiowb();
  5162. break;
  5163. }
  5164. }
  5165. return work_done;
  5166. tx_recovery:
  5167. /* work_done is guaranteed to be less than budget. */
  5168. napi_complete(napi);
  5169. tg3_reset_task_schedule(tp);
  5170. return work_done;
  5171. }
  5172. static void tg3_process_error(struct tg3 *tp)
  5173. {
  5174. u32 val;
  5175. bool real_error = false;
  5176. if (tg3_flag(tp, ERROR_PROCESSED))
  5177. return;
  5178. /* Check Flow Attention register */
  5179. val = tr32(HOSTCC_FLOW_ATTN);
  5180. if (val & ~HOSTCC_FLOW_ATTN_MBUF_LWM) {
  5181. netdev_err(tp->dev, "FLOW Attention error. Resetting chip.\n");
  5182. real_error = true;
  5183. }
  5184. if (tr32(MSGINT_STATUS) & ~MSGINT_STATUS_MSI_REQ) {
  5185. netdev_err(tp->dev, "MSI Status error. Resetting chip.\n");
  5186. real_error = true;
  5187. }
  5188. if (tr32(RDMAC_STATUS) || tr32(WDMAC_STATUS)) {
  5189. netdev_err(tp->dev, "DMA Status error. Resetting chip.\n");
  5190. real_error = true;
  5191. }
  5192. if (!real_error)
  5193. return;
  5194. tg3_dump_state(tp);
  5195. tg3_flag_set(tp, ERROR_PROCESSED);
  5196. tg3_reset_task_schedule(tp);
  5197. }
  5198. static int tg3_poll(struct napi_struct *napi, int budget)
  5199. {
  5200. struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
  5201. struct tg3 *tp = tnapi->tp;
  5202. int work_done = 0;
  5203. struct tg3_hw_status *sblk = tnapi->hw_status;
  5204. while (1) {
  5205. if (sblk->status & SD_STATUS_ERROR)
  5206. tg3_process_error(tp);
  5207. tg3_poll_link(tp);
  5208. work_done = tg3_poll_work(tnapi, work_done, budget);
  5209. if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
  5210. goto tx_recovery;
  5211. if (unlikely(work_done >= budget))
  5212. break;
  5213. if (tg3_flag(tp, TAGGED_STATUS)) {
  5214. /* tp->last_tag is used in tg3_int_reenable() below
  5215. * to tell the hw how much work has been processed,
  5216. * so we must read it before checking for more work.
  5217. */
  5218. tnapi->last_tag = sblk->status_tag;
  5219. tnapi->last_irq_tag = tnapi->last_tag;
  5220. rmb();
  5221. } else
  5222. sblk->status &= ~SD_STATUS_UPDATED;
  5223. if (likely(!tg3_has_work(tnapi))) {
  5224. napi_complete(napi);
  5225. tg3_int_reenable(tnapi);
  5226. break;
  5227. }
  5228. }
  5229. return work_done;
  5230. tx_recovery:
  5231. /* work_done is guaranteed to be less than budget. */
  5232. napi_complete(napi);
  5233. tg3_reset_task_schedule(tp);
  5234. return work_done;
  5235. }
  5236. static void tg3_napi_disable(struct tg3 *tp)
  5237. {
  5238. int i;
  5239. for (i = tp->irq_cnt - 1; i >= 0; i--)
  5240. napi_disable(&tp->napi[i].napi);
  5241. }
  5242. static void tg3_napi_enable(struct tg3 *tp)
  5243. {
  5244. int i;
  5245. for (i = 0; i < tp->irq_cnt; i++)
  5246. napi_enable(&tp->napi[i].napi);
  5247. }
  5248. static void tg3_napi_init(struct tg3 *tp)
  5249. {
  5250. int i;
  5251. netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
  5252. for (i = 1; i < tp->irq_cnt; i++)
  5253. netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
  5254. }
  5255. static void tg3_napi_fini(struct tg3 *tp)
  5256. {
  5257. int i;
  5258. for (i = 0; i < tp->irq_cnt; i++)
  5259. netif_napi_del(&tp->napi[i].napi);
  5260. }
  5261. static inline void tg3_netif_stop(struct tg3 *tp)
  5262. {
  5263. tp->dev->trans_start = jiffies; /* prevent tx timeout */
  5264. tg3_napi_disable(tp);
  5265. netif_tx_disable(tp->dev);
  5266. }
  5267. static inline void tg3_netif_start(struct tg3 *tp)
  5268. {
  5269. /* NOTE: unconditional netif_tx_wake_all_queues is only
  5270. * appropriate so long as all callers are assured to
  5271. * have free tx slots (such as after tg3_init_hw)
  5272. */
  5273. netif_tx_wake_all_queues(tp->dev);
  5274. tg3_napi_enable(tp);
  5275. tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
  5276. tg3_enable_ints(tp);
  5277. }
  5278. static void tg3_irq_quiesce(struct tg3 *tp)
  5279. {
  5280. int i;
  5281. BUG_ON(tp->irq_sync);
  5282. tp->irq_sync = 1;
  5283. smp_mb();
  5284. for (i = 0; i < tp->irq_cnt; i++)
  5285. synchronize_irq(tp->napi[i].irq_vec);
  5286. }
  5287. /* Fully shutdown all tg3 driver activity elsewhere in the system.
  5288. * If irq_sync is non-zero, then the IRQ handler must be synchronized
  5289. * with as well. Most of the time, this is not necessary except when
  5290. * shutting down the device.
  5291. */
  5292. static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
  5293. {
  5294. spin_lock_bh(&tp->lock);
  5295. if (irq_sync)
  5296. tg3_irq_quiesce(tp);
  5297. }
  5298. static inline void tg3_full_unlock(struct tg3 *tp)
  5299. {
  5300. spin_unlock_bh(&tp->lock);
  5301. }
  5302. /* One-shot MSI handler - Chip automatically disables interrupt
  5303. * after sending MSI so driver doesn't have to do it.
  5304. */
  5305. static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
  5306. {
  5307. struct tg3_napi *tnapi = dev_id;
  5308. struct tg3 *tp = tnapi->tp;
  5309. prefetch(tnapi->hw_status);
  5310. if (tnapi->rx_rcb)
  5311. prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
  5312. if (likely(!tg3_irq_sync(tp)))
  5313. napi_schedule(&tnapi->napi);
  5314. return IRQ_HANDLED;
  5315. }
  5316. /* MSI ISR - No need to check for interrupt sharing and no need to
  5317. * flush status block and interrupt mailbox. PCI ordering rules
  5318. * guarantee that MSI will arrive after the status block.
  5319. */
  5320. static irqreturn_t tg3_msi(int irq, void *dev_id)
  5321. {
  5322. struct tg3_napi *tnapi = dev_id;
  5323. struct tg3 *tp = tnapi->tp;
  5324. prefetch(tnapi->hw_status);
  5325. if (tnapi->rx_rcb)
  5326. prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
  5327. /*
  5328. * Writing any value to intr-mbox-0 clears PCI INTA# and
  5329. * chip-internal interrupt pending events.
  5330. * Writing non-zero to intr-mbox-0 additional tells the
  5331. * NIC to stop sending us irqs, engaging "in-intr-handler"
  5332. * event coalescing.
  5333. */
  5334. tw32_mailbox(tnapi->int_mbox, 0x00000001);
  5335. if (likely(!tg3_irq_sync(tp)))
  5336. napi_schedule(&tnapi->napi);
  5337. return IRQ_RETVAL(1);
  5338. }
  5339. static irqreturn_t tg3_interrupt(int irq, void *dev_id)
  5340. {
  5341. struct tg3_napi *tnapi = dev_id;
  5342. struct tg3 *tp = tnapi->tp;
  5343. struct tg3_hw_status *sblk = tnapi->hw_status;
  5344. unsigned int handled = 1;
  5345. /* In INTx mode, it is possible for the interrupt to arrive at
  5346. * the CPU before the status block posted prior to the interrupt.
  5347. * Reading the PCI State register will confirm whether the
  5348. * interrupt is ours and will flush the status block.
  5349. */
  5350. if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
  5351. if (tg3_flag(tp, CHIP_RESETTING) ||
  5352. (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
  5353. handled = 0;
  5354. goto out;
  5355. }
  5356. }
  5357. /*
  5358. * Writing any value to intr-mbox-0 clears PCI INTA# and
  5359. * chip-internal interrupt pending events.
  5360. * Writing non-zero to intr-mbox-0 additional tells the
  5361. * NIC to stop sending us irqs, engaging "in-intr-handler"
  5362. * event coalescing.
  5363. *
  5364. * Flush the mailbox to de-assert the IRQ immediately to prevent
  5365. * spurious interrupts. The flush impacts performance but
  5366. * excessive spurious interrupts can be worse in some cases.
  5367. */
  5368. tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
  5369. if (tg3_irq_sync(tp))
  5370. goto out;
  5371. sblk->status &= ~SD_STATUS_UPDATED;
  5372. if (likely(tg3_has_work(tnapi))) {
  5373. prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
  5374. napi_schedule(&tnapi->napi);
  5375. } else {
  5376. /* No work, shared interrupt perhaps? re-enable
  5377. * interrupts, and flush that PCI write
  5378. */
  5379. tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
  5380. 0x00000000);
  5381. }
  5382. out:
  5383. return IRQ_RETVAL(handled);
  5384. }
  5385. static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
  5386. {
  5387. struct tg3_napi *tnapi = dev_id;
  5388. struct tg3 *tp = tnapi->tp;
  5389. struct tg3_hw_status *sblk = tnapi->hw_status;
  5390. unsigned int handled = 1;
  5391. /* In INTx mode, it is possible for the interrupt to arrive at
  5392. * the CPU before the status block posted prior to the interrupt.
  5393. * Reading the PCI State register will confirm whether the
  5394. * interrupt is ours and will flush the status block.
  5395. */
  5396. if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
  5397. if (tg3_flag(tp, CHIP_RESETTING) ||
  5398. (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
  5399. handled = 0;
  5400. goto out;
  5401. }
  5402. }
  5403. /*
  5404. * writing any value to intr-mbox-0 clears PCI INTA# and
  5405. * chip-internal interrupt pending events.
  5406. * writing non-zero to intr-mbox-0 additional tells the
  5407. * NIC to stop sending us irqs, engaging "in-intr-handler"
  5408. * event coalescing.
  5409. *
  5410. * Flush the mailbox to de-assert the IRQ immediately to prevent
  5411. * spurious interrupts. The flush impacts performance but
  5412. * excessive spurious interrupts can be worse in some cases.
  5413. */
  5414. tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
  5415. /*
  5416. * In a shared interrupt configuration, sometimes other devices'
  5417. * interrupts will scream. We record the current status tag here
  5418. * so that the above check can report that the screaming interrupts
  5419. * are unhandled. Eventually they will be silenced.
  5420. */
  5421. tnapi->last_irq_tag = sblk->status_tag;
  5422. if (tg3_irq_sync(tp))
  5423. goto out;
  5424. prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
  5425. napi_schedule(&tnapi->napi);
  5426. out:
  5427. return IRQ_RETVAL(handled);
  5428. }
  5429. /* ISR for interrupt test */
  5430. static irqreturn_t tg3_test_isr(int irq, void *dev_id)
  5431. {
  5432. struct tg3_napi *tnapi = dev_id;
  5433. struct tg3 *tp = tnapi->tp;
  5434. struct tg3_hw_status *sblk = tnapi->hw_status;
  5435. if ((sblk->status & SD_STATUS_UPDATED) ||
  5436. !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
  5437. tg3_disable_ints(tp);
  5438. return IRQ_RETVAL(1);
  5439. }
  5440. return IRQ_RETVAL(0);
  5441. }
  5442. #ifdef CONFIG_NET_POLL_CONTROLLER
  5443. static void tg3_poll_controller(struct net_device *dev)
  5444. {
  5445. int i;
  5446. struct tg3 *tp = netdev_priv(dev);
  5447. for (i = 0; i < tp->irq_cnt; i++)
  5448. tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
  5449. }
  5450. #endif
  5451. static void tg3_tx_timeout(struct net_device *dev)
  5452. {
  5453. struct tg3 *tp = netdev_priv(dev);
  5454. if (netif_msg_tx_err(tp)) {
  5455. netdev_err(dev, "transmit timed out, resetting\n");
  5456. tg3_dump_state(tp);
  5457. }
  5458. tg3_reset_task_schedule(tp);
  5459. }
  5460. /* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
  5461. static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
  5462. {
  5463. u32 base = (u32) mapping & 0xffffffff;
  5464. return (base > 0xffffdcc0) && (base + len + 8 < base);
  5465. }
  5466. /* Test for DMA addresses > 40-bit */
  5467. static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
  5468. int len)
  5469. {
  5470. #if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
  5471. if (tg3_flag(tp, 40BIT_DMA_BUG))
  5472. return ((u64) mapping + len) > DMA_BIT_MASK(40);
  5473. return 0;
  5474. #else
  5475. return 0;
  5476. #endif
  5477. }
  5478. static inline void tg3_tx_set_bd(struct tg3_tx_buffer_desc *txbd,
  5479. dma_addr_t mapping, u32 len, u32 flags,
  5480. u32 mss, u32 vlan)
  5481. {
  5482. txbd->addr_hi = ((u64) mapping >> 32);
  5483. txbd->addr_lo = ((u64) mapping & 0xffffffff);
  5484. txbd->len_flags = (len << TXD_LEN_SHIFT) | (flags & 0x0000ffff);
  5485. txbd->vlan_tag = (mss << TXD_MSS_SHIFT) | (vlan << TXD_VLAN_TAG_SHIFT);
  5486. }
  5487. static bool tg3_tx_frag_set(struct tg3_napi *tnapi, u32 *entry, u32 *budget,
  5488. dma_addr_t map, u32 len, u32 flags,
  5489. u32 mss, u32 vlan)
  5490. {
  5491. struct tg3 *tp = tnapi->tp;
  5492. bool hwbug = false;
  5493. if (tg3_flag(tp, SHORT_DMA_BUG) && len <= 8)
  5494. hwbug = true;
  5495. if (tg3_4g_overflow_test(map, len))
  5496. hwbug = true;
  5497. if (tg3_40bit_overflow_test(tp, map, len))
  5498. hwbug = true;
  5499. if (tp->dma_limit) {
  5500. u32 prvidx = *entry;
  5501. u32 tmp_flag = flags & ~TXD_FLAG_END;
  5502. while (len > tp->dma_limit && *budget) {
  5503. u32 frag_len = tp->dma_limit;
  5504. len -= tp->dma_limit;
  5505. /* Avoid the 8byte DMA problem */
  5506. if (len <= 8) {
  5507. len += tp->dma_limit / 2;
  5508. frag_len = tp->dma_limit / 2;
  5509. }
  5510. tnapi->tx_buffers[*entry].fragmented = true;
  5511. tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
  5512. frag_len, tmp_flag, mss, vlan);
  5513. *budget -= 1;
  5514. prvidx = *entry;
  5515. *entry = NEXT_TX(*entry);
  5516. map += frag_len;
  5517. }
  5518. if (len) {
  5519. if (*budget) {
  5520. tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
  5521. len, flags, mss, vlan);
  5522. *budget -= 1;
  5523. *entry = NEXT_TX(*entry);
  5524. } else {
  5525. hwbug = true;
  5526. tnapi->tx_buffers[prvidx].fragmented = false;
  5527. }
  5528. }
  5529. } else {
  5530. tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
  5531. len, flags, mss, vlan);
  5532. *entry = NEXT_TX(*entry);
  5533. }
  5534. return hwbug;
  5535. }
  5536. static void tg3_tx_skb_unmap(struct tg3_napi *tnapi, u32 entry, int last)
  5537. {
  5538. int i;
  5539. struct sk_buff *skb;
  5540. struct tg3_tx_ring_info *txb = &tnapi->tx_buffers[entry];
  5541. skb = txb->skb;
  5542. txb->skb = NULL;
  5543. pci_unmap_single(tnapi->tp->pdev,
  5544. dma_unmap_addr(txb, mapping),
  5545. skb_headlen(skb),
  5546. PCI_DMA_TODEVICE);
  5547. while (txb->fragmented) {
  5548. txb->fragmented = false;
  5549. entry = NEXT_TX(entry);
  5550. txb = &tnapi->tx_buffers[entry];
  5551. }
  5552. for (i = 0; i <= last; i++) {
  5553. const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  5554. entry = NEXT_TX(entry);
  5555. txb = &tnapi->tx_buffers[entry];
  5556. pci_unmap_page(tnapi->tp->pdev,
  5557. dma_unmap_addr(txb, mapping),
  5558. skb_frag_size(frag), PCI_DMA_TODEVICE);
  5559. while (txb->fragmented) {
  5560. txb->fragmented = false;
  5561. entry = NEXT_TX(entry);
  5562. txb = &tnapi->tx_buffers[entry];
  5563. }
  5564. }
  5565. }
  5566. /* Workaround 4GB and 40-bit hardware DMA bugs. */
  5567. static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
  5568. struct sk_buff **pskb,
  5569. u32 *entry, u32 *budget,
  5570. u32 base_flags, u32 mss, u32 vlan)
  5571. {
  5572. struct tg3 *tp = tnapi->tp;
  5573. struct sk_buff *new_skb, *skb = *pskb;
  5574. dma_addr_t new_addr = 0;
  5575. int ret = 0;
  5576. if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
  5577. new_skb = skb_copy(skb, GFP_ATOMIC);
  5578. else {
  5579. int more_headroom = 4 - ((unsigned long)skb->data & 3);
  5580. new_skb = skb_copy_expand(skb,
  5581. skb_headroom(skb) + more_headroom,
  5582. skb_tailroom(skb), GFP_ATOMIC);
  5583. }
  5584. if (!new_skb) {
  5585. ret = -1;
  5586. } else {
  5587. /* New SKB is guaranteed to be linear. */
  5588. new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
  5589. PCI_DMA_TODEVICE);
  5590. /* Make sure the mapping succeeded */
  5591. if (pci_dma_mapping_error(tp->pdev, new_addr)) {
  5592. dev_kfree_skb(new_skb);
  5593. ret = -1;
  5594. } else {
  5595. u32 save_entry = *entry;
  5596. base_flags |= TXD_FLAG_END;
  5597. tnapi->tx_buffers[*entry].skb = new_skb;
  5598. dma_unmap_addr_set(&tnapi->tx_buffers[*entry],
  5599. mapping, new_addr);
  5600. if (tg3_tx_frag_set(tnapi, entry, budget, new_addr,
  5601. new_skb->len, base_flags,
  5602. mss, vlan)) {
  5603. tg3_tx_skb_unmap(tnapi, save_entry, -1);
  5604. dev_kfree_skb(new_skb);
  5605. ret = -1;
  5606. }
  5607. }
  5608. }
  5609. dev_kfree_skb(skb);
  5610. *pskb = new_skb;
  5611. return ret;
  5612. }
  5613. static netdev_tx_t tg3_start_xmit(struct sk_buff *, struct net_device *);
  5614. /* Use GSO to workaround a rare TSO bug that may be triggered when the
  5615. * TSO header is greater than 80 bytes.
  5616. */
  5617. static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
  5618. {
  5619. struct sk_buff *segs, *nskb;
  5620. u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
  5621. /* Estimate the number of fragments in the worst case */
  5622. if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
  5623. netif_stop_queue(tp->dev);
  5624. /* netif_tx_stop_queue() must be done before checking
  5625. * checking tx index in tg3_tx_avail() below, because in
  5626. * tg3_tx(), we update tx index before checking for
  5627. * netif_tx_queue_stopped().
  5628. */
  5629. smp_mb();
  5630. if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
  5631. return NETDEV_TX_BUSY;
  5632. netif_wake_queue(tp->dev);
  5633. }
  5634. segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
  5635. if (IS_ERR(segs))
  5636. goto tg3_tso_bug_end;
  5637. do {
  5638. nskb = segs;
  5639. segs = segs->next;
  5640. nskb->next = NULL;
  5641. tg3_start_xmit(nskb, tp->dev);
  5642. } while (segs);
  5643. tg3_tso_bug_end:
  5644. dev_kfree_skb(skb);
  5645. return NETDEV_TX_OK;
  5646. }
  5647. /* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
  5648. * support TG3_FLAG_HW_TSO_1 or firmware TSO only.
  5649. */
  5650. static netdev_tx_t tg3_start_xmit(struct sk_buff *skb, struct net_device *dev)
  5651. {
  5652. struct tg3 *tp = netdev_priv(dev);
  5653. u32 len, entry, base_flags, mss, vlan = 0;
  5654. u32 budget;
  5655. int i = -1, would_hit_hwbug;
  5656. dma_addr_t mapping;
  5657. struct tg3_napi *tnapi;
  5658. struct netdev_queue *txq;
  5659. unsigned int last;
  5660. txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
  5661. tnapi = &tp->napi[skb_get_queue_mapping(skb)];
  5662. if (tg3_flag(tp, ENABLE_TSS))
  5663. tnapi++;
  5664. budget = tg3_tx_avail(tnapi);
  5665. /* We are running in BH disabled context with netif_tx_lock
  5666. * and TX reclaim runs via tp->napi.poll inside of a software
  5667. * interrupt. Furthermore, IRQ processing runs lockless so we have
  5668. * no IRQ context deadlocks to worry about either. Rejoice!
  5669. */
  5670. if (unlikely(budget <= (skb_shinfo(skb)->nr_frags + 1))) {
  5671. if (!netif_tx_queue_stopped(txq)) {
  5672. netif_tx_stop_queue(txq);
  5673. /* This is a hard error, log it. */
  5674. netdev_err(dev,
  5675. "BUG! Tx Ring full when queue awake!\n");
  5676. }
  5677. return NETDEV_TX_BUSY;
  5678. }
  5679. entry = tnapi->tx_prod;
  5680. base_flags = 0;
  5681. if (skb->ip_summed == CHECKSUM_PARTIAL)
  5682. base_flags |= TXD_FLAG_TCPUDP_CSUM;
  5683. mss = skb_shinfo(skb)->gso_size;
  5684. if (mss) {
  5685. struct iphdr *iph;
  5686. u32 tcp_opt_len, hdr_len;
  5687. if (skb_header_cloned(skb) &&
  5688. pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
  5689. goto drop;
  5690. iph = ip_hdr(skb);
  5691. tcp_opt_len = tcp_optlen(skb);
  5692. hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb) - ETH_HLEN;
  5693. if (!skb_is_gso_v6(skb)) {
  5694. iph->check = 0;
  5695. iph->tot_len = htons(mss + hdr_len);
  5696. }
  5697. if (unlikely((ETH_HLEN + hdr_len) > 80) &&
  5698. tg3_flag(tp, TSO_BUG))
  5699. return tg3_tso_bug(tp, skb);
  5700. base_flags |= (TXD_FLAG_CPU_PRE_DMA |
  5701. TXD_FLAG_CPU_POST_DMA);
  5702. if (tg3_flag(tp, HW_TSO_1) ||
  5703. tg3_flag(tp, HW_TSO_2) ||
  5704. tg3_flag(tp, HW_TSO_3)) {
  5705. tcp_hdr(skb)->check = 0;
  5706. base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
  5707. } else
  5708. tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
  5709. iph->daddr, 0,
  5710. IPPROTO_TCP,
  5711. 0);
  5712. if (tg3_flag(tp, HW_TSO_3)) {
  5713. mss |= (hdr_len & 0xc) << 12;
  5714. if (hdr_len & 0x10)
  5715. base_flags |= 0x00000010;
  5716. base_flags |= (hdr_len & 0x3e0) << 5;
  5717. } else if (tg3_flag(tp, HW_TSO_2))
  5718. mss |= hdr_len << 9;
  5719. else if (tg3_flag(tp, HW_TSO_1) ||
  5720. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
  5721. if (tcp_opt_len || iph->ihl > 5) {
  5722. int tsflags;
  5723. tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
  5724. mss |= (tsflags << 11);
  5725. }
  5726. } else {
  5727. if (tcp_opt_len || iph->ihl > 5) {
  5728. int tsflags;
  5729. tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
  5730. base_flags |= tsflags << 12;
  5731. }
  5732. }
  5733. }
  5734. if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
  5735. !mss && skb->len > VLAN_ETH_FRAME_LEN)
  5736. base_flags |= TXD_FLAG_JMB_PKT;
  5737. if (vlan_tx_tag_present(skb)) {
  5738. base_flags |= TXD_FLAG_VLAN;
  5739. vlan = vlan_tx_tag_get(skb);
  5740. }
  5741. len = skb_headlen(skb);
  5742. mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
  5743. if (pci_dma_mapping_error(tp->pdev, mapping))
  5744. goto drop;
  5745. tnapi->tx_buffers[entry].skb = skb;
  5746. dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
  5747. would_hit_hwbug = 0;
  5748. if (tg3_flag(tp, 5701_DMA_BUG))
  5749. would_hit_hwbug = 1;
  5750. if (tg3_tx_frag_set(tnapi, &entry, &budget, mapping, len, base_flags |
  5751. ((skb_shinfo(skb)->nr_frags == 0) ? TXD_FLAG_END : 0),
  5752. mss, vlan)) {
  5753. would_hit_hwbug = 1;
  5754. } else if (skb_shinfo(skb)->nr_frags > 0) {
  5755. u32 tmp_mss = mss;
  5756. if (!tg3_flag(tp, HW_TSO_1) &&
  5757. !tg3_flag(tp, HW_TSO_2) &&
  5758. !tg3_flag(tp, HW_TSO_3))
  5759. tmp_mss = 0;
  5760. /* Now loop through additional data
  5761. * fragments, and queue them.
  5762. */
  5763. last = skb_shinfo(skb)->nr_frags - 1;
  5764. for (i = 0; i <= last; i++) {
  5765. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  5766. len = skb_frag_size(frag);
  5767. mapping = skb_frag_dma_map(&tp->pdev->dev, frag, 0,
  5768. len, DMA_TO_DEVICE);
  5769. tnapi->tx_buffers[entry].skb = NULL;
  5770. dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
  5771. mapping);
  5772. if (dma_mapping_error(&tp->pdev->dev, mapping))
  5773. goto dma_error;
  5774. if (!budget ||
  5775. tg3_tx_frag_set(tnapi, &entry, &budget, mapping,
  5776. len, base_flags |
  5777. ((i == last) ? TXD_FLAG_END : 0),
  5778. tmp_mss, vlan)) {
  5779. would_hit_hwbug = 1;
  5780. break;
  5781. }
  5782. }
  5783. }
  5784. if (would_hit_hwbug) {
  5785. tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, i);
  5786. /* If the workaround fails due to memory/mapping
  5787. * failure, silently drop this packet.
  5788. */
  5789. entry = tnapi->tx_prod;
  5790. budget = tg3_tx_avail(tnapi);
  5791. if (tigon3_dma_hwbug_workaround(tnapi, &skb, &entry, &budget,
  5792. base_flags, mss, vlan))
  5793. goto drop_nofree;
  5794. }
  5795. skb_tx_timestamp(skb);
  5796. netdev_tx_sent_queue(txq, skb->len);
  5797. /* Sync BD data before updating mailbox */
  5798. wmb();
  5799. /* Packets are ready, update Tx producer idx local and on card. */
  5800. tw32_tx_mbox(tnapi->prodmbox, entry);
  5801. tnapi->tx_prod = entry;
  5802. if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
  5803. netif_tx_stop_queue(txq);
  5804. /* netif_tx_stop_queue() must be done before checking
  5805. * checking tx index in tg3_tx_avail() below, because in
  5806. * tg3_tx(), we update tx index before checking for
  5807. * netif_tx_queue_stopped().
  5808. */
  5809. smp_mb();
  5810. if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
  5811. netif_tx_wake_queue(txq);
  5812. }
  5813. mmiowb();
  5814. return NETDEV_TX_OK;
  5815. dma_error:
  5816. tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, --i);
  5817. tnapi->tx_buffers[tnapi->tx_prod].skb = NULL;
  5818. drop:
  5819. dev_kfree_skb(skb);
  5820. drop_nofree:
  5821. tp->tx_dropped++;
  5822. return NETDEV_TX_OK;
  5823. }
  5824. static void tg3_mac_loopback(struct tg3 *tp, bool enable)
  5825. {
  5826. if (enable) {
  5827. tp->mac_mode &= ~(MAC_MODE_HALF_DUPLEX |
  5828. MAC_MODE_PORT_MODE_MASK);
  5829. tp->mac_mode |= MAC_MODE_PORT_INT_LPBACK;
  5830. if (!tg3_flag(tp, 5705_PLUS))
  5831. tp->mac_mode |= MAC_MODE_LINK_POLARITY;
  5832. if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
  5833. tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
  5834. else
  5835. tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
  5836. } else {
  5837. tp->mac_mode &= ~MAC_MODE_PORT_INT_LPBACK;
  5838. if (tg3_flag(tp, 5705_PLUS) ||
  5839. (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) ||
  5840. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
  5841. tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
  5842. }
  5843. tw32(MAC_MODE, tp->mac_mode);
  5844. udelay(40);
  5845. }
  5846. static int tg3_phy_lpbk_set(struct tg3 *tp, u32 speed, bool extlpbk)
  5847. {
  5848. u32 val, bmcr, mac_mode, ptest = 0;
  5849. tg3_phy_toggle_apd(tp, false);
  5850. tg3_phy_toggle_automdix(tp, 0);
  5851. if (extlpbk && tg3_phy_set_extloopbk(tp))
  5852. return -EIO;
  5853. bmcr = BMCR_FULLDPLX;
  5854. switch (speed) {
  5855. case SPEED_10:
  5856. break;
  5857. case SPEED_100:
  5858. bmcr |= BMCR_SPEED100;
  5859. break;
  5860. case SPEED_1000:
  5861. default:
  5862. if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
  5863. speed = SPEED_100;
  5864. bmcr |= BMCR_SPEED100;
  5865. } else {
  5866. speed = SPEED_1000;
  5867. bmcr |= BMCR_SPEED1000;
  5868. }
  5869. }
  5870. if (extlpbk) {
  5871. if (!(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
  5872. tg3_readphy(tp, MII_CTRL1000, &val);
  5873. val |= CTL1000_AS_MASTER |
  5874. CTL1000_ENABLE_MASTER;
  5875. tg3_writephy(tp, MII_CTRL1000, val);
  5876. } else {
  5877. ptest = MII_TG3_FET_PTEST_TRIM_SEL |
  5878. MII_TG3_FET_PTEST_TRIM_2;
  5879. tg3_writephy(tp, MII_TG3_FET_PTEST, ptest);
  5880. }
  5881. } else
  5882. bmcr |= BMCR_LOOPBACK;
  5883. tg3_writephy(tp, MII_BMCR, bmcr);
  5884. /* The write needs to be flushed for the FETs */
  5885. if (tp->phy_flags & TG3_PHYFLG_IS_FET)
  5886. tg3_readphy(tp, MII_BMCR, &bmcr);
  5887. udelay(40);
  5888. if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
  5889. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
  5890. tg3_writephy(tp, MII_TG3_FET_PTEST, ptest |
  5891. MII_TG3_FET_PTEST_FRC_TX_LINK |
  5892. MII_TG3_FET_PTEST_FRC_TX_LOCK);
  5893. /* The write needs to be flushed for the AC131 */
  5894. tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
  5895. }
  5896. /* Reset to prevent losing 1st rx packet intermittently */
  5897. if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
  5898. tg3_flag(tp, 5780_CLASS)) {
  5899. tw32_f(MAC_RX_MODE, RX_MODE_RESET);
  5900. udelay(10);
  5901. tw32_f(MAC_RX_MODE, tp->rx_mode);
  5902. }
  5903. mac_mode = tp->mac_mode &
  5904. ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
  5905. if (speed == SPEED_1000)
  5906. mac_mode |= MAC_MODE_PORT_MODE_GMII;
  5907. else
  5908. mac_mode |= MAC_MODE_PORT_MODE_MII;
  5909. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
  5910. u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
  5911. if (masked_phy_id == TG3_PHY_ID_BCM5401)
  5912. mac_mode &= ~MAC_MODE_LINK_POLARITY;
  5913. else if (masked_phy_id == TG3_PHY_ID_BCM5411)
  5914. mac_mode |= MAC_MODE_LINK_POLARITY;
  5915. tg3_writephy(tp, MII_TG3_EXT_CTRL,
  5916. MII_TG3_EXT_CTRL_LNK3_LED_MODE);
  5917. }
  5918. tw32(MAC_MODE, mac_mode);
  5919. udelay(40);
  5920. return 0;
  5921. }
  5922. static void tg3_set_loopback(struct net_device *dev, netdev_features_t features)
  5923. {
  5924. struct tg3 *tp = netdev_priv(dev);
  5925. if (features & NETIF_F_LOOPBACK) {
  5926. if (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK)
  5927. return;
  5928. spin_lock_bh(&tp->lock);
  5929. tg3_mac_loopback(tp, true);
  5930. netif_carrier_on(tp->dev);
  5931. spin_unlock_bh(&tp->lock);
  5932. netdev_info(dev, "Internal MAC loopback mode enabled.\n");
  5933. } else {
  5934. if (!(tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
  5935. return;
  5936. spin_lock_bh(&tp->lock);
  5937. tg3_mac_loopback(tp, false);
  5938. /* Force link status check */
  5939. tg3_setup_phy(tp, 1);
  5940. spin_unlock_bh(&tp->lock);
  5941. netdev_info(dev, "Internal MAC loopback mode disabled.\n");
  5942. }
  5943. }
  5944. static netdev_features_t tg3_fix_features(struct net_device *dev,
  5945. netdev_features_t features)
  5946. {
  5947. struct tg3 *tp = netdev_priv(dev);
  5948. if (dev->mtu > ETH_DATA_LEN && tg3_flag(tp, 5780_CLASS))
  5949. features &= ~NETIF_F_ALL_TSO;
  5950. return features;
  5951. }
  5952. static int tg3_set_features(struct net_device *dev, netdev_features_t features)
  5953. {
  5954. netdev_features_t changed = dev->features ^ features;
  5955. if ((changed & NETIF_F_LOOPBACK) && netif_running(dev))
  5956. tg3_set_loopback(dev, features);
  5957. return 0;
  5958. }
  5959. static void tg3_rx_prodring_free(struct tg3 *tp,
  5960. struct tg3_rx_prodring_set *tpr)
  5961. {
  5962. int i;
  5963. if (tpr != &tp->napi[0].prodring) {
  5964. for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
  5965. i = (i + 1) & tp->rx_std_ring_mask)
  5966. tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
  5967. tp->rx_pkt_map_sz);
  5968. if (tg3_flag(tp, JUMBO_CAPABLE)) {
  5969. for (i = tpr->rx_jmb_cons_idx;
  5970. i != tpr->rx_jmb_prod_idx;
  5971. i = (i + 1) & tp->rx_jmb_ring_mask) {
  5972. tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
  5973. TG3_RX_JMB_MAP_SZ);
  5974. }
  5975. }
  5976. return;
  5977. }
  5978. for (i = 0; i <= tp->rx_std_ring_mask; i++)
  5979. tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
  5980. tp->rx_pkt_map_sz);
  5981. if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
  5982. for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
  5983. tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
  5984. TG3_RX_JMB_MAP_SZ);
  5985. }
  5986. }
  5987. /* Initialize rx rings for packet processing.
  5988. *
  5989. * The chip has been shut down and the driver detached from
  5990. * the networking, so no interrupts or new tx packets will
  5991. * end up in the driver. tp->{tx,}lock are held and thus
  5992. * we may not sleep.
  5993. */
  5994. static int tg3_rx_prodring_alloc(struct tg3 *tp,
  5995. struct tg3_rx_prodring_set *tpr)
  5996. {
  5997. u32 i, rx_pkt_dma_sz;
  5998. tpr->rx_std_cons_idx = 0;
  5999. tpr->rx_std_prod_idx = 0;
  6000. tpr->rx_jmb_cons_idx = 0;
  6001. tpr->rx_jmb_prod_idx = 0;
  6002. if (tpr != &tp->napi[0].prodring) {
  6003. memset(&tpr->rx_std_buffers[0], 0,
  6004. TG3_RX_STD_BUFF_RING_SIZE(tp));
  6005. if (tpr->rx_jmb_buffers)
  6006. memset(&tpr->rx_jmb_buffers[0], 0,
  6007. TG3_RX_JMB_BUFF_RING_SIZE(tp));
  6008. goto done;
  6009. }
  6010. /* Zero out all descriptors. */
  6011. memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
  6012. rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
  6013. if (tg3_flag(tp, 5780_CLASS) &&
  6014. tp->dev->mtu > ETH_DATA_LEN)
  6015. rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
  6016. tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
  6017. /* Initialize invariants of the rings, we only set this
  6018. * stuff once. This works because the card does not
  6019. * write into the rx buffer posting rings.
  6020. */
  6021. for (i = 0; i <= tp->rx_std_ring_mask; i++) {
  6022. struct tg3_rx_buffer_desc *rxd;
  6023. rxd = &tpr->rx_std[i];
  6024. rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
  6025. rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
  6026. rxd->opaque = (RXD_OPAQUE_RING_STD |
  6027. (i << RXD_OPAQUE_INDEX_SHIFT));
  6028. }
  6029. /* Now allocate fresh SKBs for each rx ring. */
  6030. for (i = 0; i < tp->rx_pending; i++) {
  6031. unsigned int frag_size;
  6032. if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_STD, i,
  6033. &frag_size) < 0) {
  6034. netdev_warn(tp->dev,
  6035. "Using a smaller RX standard ring. Only "
  6036. "%d out of %d buffers were allocated "
  6037. "successfully\n", i, tp->rx_pending);
  6038. if (i == 0)
  6039. goto initfail;
  6040. tp->rx_pending = i;
  6041. break;
  6042. }
  6043. }
  6044. if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
  6045. goto done;
  6046. memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
  6047. if (!tg3_flag(tp, JUMBO_RING_ENABLE))
  6048. goto done;
  6049. for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
  6050. struct tg3_rx_buffer_desc *rxd;
  6051. rxd = &tpr->rx_jmb[i].std;
  6052. rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
  6053. rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
  6054. RXD_FLAG_JUMBO;
  6055. rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
  6056. (i << RXD_OPAQUE_INDEX_SHIFT));
  6057. }
  6058. for (i = 0; i < tp->rx_jumbo_pending; i++) {
  6059. unsigned int frag_size;
  6060. if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_JUMBO, i,
  6061. &frag_size) < 0) {
  6062. netdev_warn(tp->dev,
  6063. "Using a smaller RX jumbo ring. Only %d "
  6064. "out of %d buffers were allocated "
  6065. "successfully\n", i, tp->rx_jumbo_pending);
  6066. if (i == 0)
  6067. goto initfail;
  6068. tp->rx_jumbo_pending = i;
  6069. break;
  6070. }
  6071. }
  6072. done:
  6073. return 0;
  6074. initfail:
  6075. tg3_rx_prodring_free(tp, tpr);
  6076. return -ENOMEM;
  6077. }
  6078. static void tg3_rx_prodring_fini(struct tg3 *tp,
  6079. struct tg3_rx_prodring_set *tpr)
  6080. {
  6081. kfree(tpr->rx_std_buffers);
  6082. tpr->rx_std_buffers = NULL;
  6083. kfree(tpr->rx_jmb_buffers);
  6084. tpr->rx_jmb_buffers = NULL;
  6085. if (tpr->rx_std) {
  6086. dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
  6087. tpr->rx_std, tpr->rx_std_mapping);
  6088. tpr->rx_std = NULL;
  6089. }
  6090. if (tpr->rx_jmb) {
  6091. dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
  6092. tpr->rx_jmb, tpr->rx_jmb_mapping);
  6093. tpr->rx_jmb = NULL;
  6094. }
  6095. }
  6096. static int tg3_rx_prodring_init(struct tg3 *tp,
  6097. struct tg3_rx_prodring_set *tpr)
  6098. {
  6099. tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
  6100. GFP_KERNEL);
  6101. if (!tpr->rx_std_buffers)
  6102. return -ENOMEM;
  6103. tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
  6104. TG3_RX_STD_RING_BYTES(tp),
  6105. &tpr->rx_std_mapping,
  6106. GFP_KERNEL);
  6107. if (!tpr->rx_std)
  6108. goto err_out;
  6109. if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
  6110. tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
  6111. GFP_KERNEL);
  6112. if (!tpr->rx_jmb_buffers)
  6113. goto err_out;
  6114. tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
  6115. TG3_RX_JMB_RING_BYTES(tp),
  6116. &tpr->rx_jmb_mapping,
  6117. GFP_KERNEL);
  6118. if (!tpr->rx_jmb)
  6119. goto err_out;
  6120. }
  6121. return 0;
  6122. err_out:
  6123. tg3_rx_prodring_fini(tp, tpr);
  6124. return -ENOMEM;
  6125. }
  6126. /* Free up pending packets in all rx/tx rings.
  6127. *
  6128. * The chip has been shut down and the driver detached from
  6129. * the networking, so no interrupts or new tx packets will
  6130. * end up in the driver. tp->{tx,}lock is not held and we are not
  6131. * in an interrupt context and thus may sleep.
  6132. */
  6133. static void tg3_free_rings(struct tg3 *tp)
  6134. {
  6135. int i, j;
  6136. for (j = 0; j < tp->irq_cnt; j++) {
  6137. struct tg3_napi *tnapi = &tp->napi[j];
  6138. tg3_rx_prodring_free(tp, &tnapi->prodring);
  6139. if (!tnapi->tx_buffers)
  6140. continue;
  6141. for (i = 0; i < TG3_TX_RING_SIZE; i++) {
  6142. struct sk_buff *skb = tnapi->tx_buffers[i].skb;
  6143. if (!skb)
  6144. continue;
  6145. tg3_tx_skb_unmap(tnapi, i,
  6146. skb_shinfo(skb)->nr_frags - 1);
  6147. dev_kfree_skb_any(skb);
  6148. }
  6149. netdev_tx_reset_queue(netdev_get_tx_queue(tp->dev, j));
  6150. }
  6151. }
  6152. /* Initialize tx/rx rings for packet processing.
  6153. *
  6154. * The chip has been shut down and the driver detached from
  6155. * the networking, so no interrupts or new tx packets will
  6156. * end up in the driver. tp->{tx,}lock are held and thus
  6157. * we may not sleep.
  6158. */
  6159. static int tg3_init_rings(struct tg3 *tp)
  6160. {
  6161. int i;
  6162. /* Free up all the SKBs. */
  6163. tg3_free_rings(tp);
  6164. for (i = 0; i < tp->irq_cnt; i++) {
  6165. struct tg3_napi *tnapi = &tp->napi[i];
  6166. tnapi->last_tag = 0;
  6167. tnapi->last_irq_tag = 0;
  6168. tnapi->hw_status->status = 0;
  6169. tnapi->hw_status->status_tag = 0;
  6170. memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
  6171. tnapi->tx_prod = 0;
  6172. tnapi->tx_cons = 0;
  6173. if (tnapi->tx_ring)
  6174. memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
  6175. tnapi->rx_rcb_ptr = 0;
  6176. if (tnapi->rx_rcb)
  6177. memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
  6178. if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
  6179. tg3_free_rings(tp);
  6180. return -ENOMEM;
  6181. }
  6182. }
  6183. return 0;
  6184. }
  6185. /*
  6186. * Must not be invoked with interrupt sources disabled and
  6187. * the hardware shutdown down.
  6188. */
  6189. static void tg3_free_consistent(struct tg3 *tp)
  6190. {
  6191. int i;
  6192. for (i = 0; i < tp->irq_cnt; i++) {
  6193. struct tg3_napi *tnapi = &tp->napi[i];
  6194. if (tnapi->tx_ring) {
  6195. dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
  6196. tnapi->tx_ring, tnapi->tx_desc_mapping);
  6197. tnapi->tx_ring = NULL;
  6198. }
  6199. kfree(tnapi->tx_buffers);
  6200. tnapi->tx_buffers = NULL;
  6201. if (tnapi->rx_rcb) {
  6202. dma_free_coherent(&tp->pdev->dev,
  6203. TG3_RX_RCB_RING_BYTES(tp),
  6204. tnapi->rx_rcb,
  6205. tnapi->rx_rcb_mapping);
  6206. tnapi->rx_rcb = NULL;
  6207. }
  6208. tg3_rx_prodring_fini(tp, &tnapi->prodring);
  6209. if (tnapi->hw_status) {
  6210. dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
  6211. tnapi->hw_status,
  6212. tnapi->status_mapping);
  6213. tnapi->hw_status = NULL;
  6214. }
  6215. }
  6216. if (tp->hw_stats) {
  6217. dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
  6218. tp->hw_stats, tp->stats_mapping);
  6219. tp->hw_stats = NULL;
  6220. }
  6221. }
  6222. /*
  6223. * Must not be invoked with interrupt sources disabled and
  6224. * the hardware shutdown down. Can sleep.
  6225. */
  6226. static int tg3_alloc_consistent(struct tg3 *tp)
  6227. {
  6228. int i;
  6229. tp->hw_stats = dma_alloc_coherent(&tp->pdev->dev,
  6230. sizeof(struct tg3_hw_stats),
  6231. &tp->stats_mapping,
  6232. GFP_KERNEL);
  6233. if (!tp->hw_stats)
  6234. goto err_out;
  6235. memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
  6236. for (i = 0; i < tp->irq_cnt; i++) {
  6237. struct tg3_napi *tnapi = &tp->napi[i];
  6238. struct tg3_hw_status *sblk;
  6239. tnapi->hw_status = dma_alloc_coherent(&tp->pdev->dev,
  6240. TG3_HW_STATUS_SIZE,
  6241. &tnapi->status_mapping,
  6242. GFP_KERNEL);
  6243. if (!tnapi->hw_status)
  6244. goto err_out;
  6245. memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
  6246. sblk = tnapi->hw_status;
  6247. if (tg3_rx_prodring_init(tp, &tnapi->prodring))
  6248. goto err_out;
  6249. /* If multivector TSS is enabled, vector 0 does not handle
  6250. * tx interrupts. Don't allocate any resources for it.
  6251. */
  6252. if ((!i && !tg3_flag(tp, ENABLE_TSS)) ||
  6253. (i && tg3_flag(tp, ENABLE_TSS))) {
  6254. tnapi->tx_buffers = kzalloc(
  6255. sizeof(struct tg3_tx_ring_info) *
  6256. TG3_TX_RING_SIZE, GFP_KERNEL);
  6257. if (!tnapi->tx_buffers)
  6258. goto err_out;
  6259. tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
  6260. TG3_TX_RING_BYTES,
  6261. &tnapi->tx_desc_mapping,
  6262. GFP_KERNEL);
  6263. if (!tnapi->tx_ring)
  6264. goto err_out;
  6265. }
  6266. /*
  6267. * When RSS is enabled, the status block format changes
  6268. * slightly. The "rx_jumbo_consumer", "reserved",
  6269. * and "rx_mini_consumer" members get mapped to the
  6270. * other three rx return ring producer indexes.
  6271. */
  6272. switch (i) {
  6273. default:
  6274. if (tg3_flag(tp, ENABLE_RSS)) {
  6275. tnapi->rx_rcb_prod_idx = NULL;
  6276. break;
  6277. }
  6278. /* Fall through */
  6279. case 1:
  6280. tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
  6281. break;
  6282. case 2:
  6283. tnapi->rx_rcb_prod_idx = &sblk->rx_jumbo_consumer;
  6284. break;
  6285. case 3:
  6286. tnapi->rx_rcb_prod_idx = &sblk->reserved;
  6287. break;
  6288. case 4:
  6289. tnapi->rx_rcb_prod_idx = &sblk->rx_mini_consumer;
  6290. break;
  6291. }
  6292. /*
  6293. * If multivector RSS is enabled, vector 0 does not handle
  6294. * rx or tx interrupts. Don't allocate any resources for it.
  6295. */
  6296. if (!i && tg3_flag(tp, ENABLE_RSS))
  6297. continue;
  6298. tnapi->rx_rcb = dma_alloc_coherent(&tp->pdev->dev,
  6299. TG3_RX_RCB_RING_BYTES(tp),
  6300. &tnapi->rx_rcb_mapping,
  6301. GFP_KERNEL);
  6302. if (!tnapi->rx_rcb)
  6303. goto err_out;
  6304. memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
  6305. }
  6306. return 0;
  6307. err_out:
  6308. tg3_free_consistent(tp);
  6309. return -ENOMEM;
  6310. }
  6311. #define MAX_WAIT_CNT 1000
  6312. /* To stop a block, clear the enable bit and poll till it
  6313. * clears. tp->lock is held.
  6314. */
  6315. static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
  6316. {
  6317. unsigned int i;
  6318. u32 val;
  6319. if (tg3_flag(tp, 5705_PLUS)) {
  6320. switch (ofs) {
  6321. case RCVLSC_MODE:
  6322. case DMAC_MODE:
  6323. case MBFREE_MODE:
  6324. case BUFMGR_MODE:
  6325. case MEMARB_MODE:
  6326. /* We can't enable/disable these bits of the
  6327. * 5705/5750, just say success.
  6328. */
  6329. return 0;
  6330. default:
  6331. break;
  6332. }
  6333. }
  6334. val = tr32(ofs);
  6335. val &= ~enable_bit;
  6336. tw32_f(ofs, val);
  6337. for (i = 0; i < MAX_WAIT_CNT; i++) {
  6338. udelay(100);
  6339. val = tr32(ofs);
  6340. if ((val & enable_bit) == 0)
  6341. break;
  6342. }
  6343. if (i == MAX_WAIT_CNT && !silent) {
  6344. dev_err(&tp->pdev->dev,
  6345. "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
  6346. ofs, enable_bit);
  6347. return -ENODEV;
  6348. }
  6349. return 0;
  6350. }
  6351. /* tp->lock is held. */
  6352. static int tg3_abort_hw(struct tg3 *tp, int silent)
  6353. {
  6354. int i, err;
  6355. tg3_disable_ints(tp);
  6356. tp->rx_mode &= ~RX_MODE_ENABLE;
  6357. tw32_f(MAC_RX_MODE, tp->rx_mode);
  6358. udelay(10);
  6359. err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
  6360. err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
  6361. err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
  6362. err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
  6363. err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
  6364. err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
  6365. err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
  6366. err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
  6367. err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
  6368. err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
  6369. err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
  6370. err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
  6371. err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
  6372. tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
  6373. tw32_f(MAC_MODE, tp->mac_mode);
  6374. udelay(40);
  6375. tp->tx_mode &= ~TX_MODE_ENABLE;
  6376. tw32_f(MAC_TX_MODE, tp->tx_mode);
  6377. for (i = 0; i < MAX_WAIT_CNT; i++) {
  6378. udelay(100);
  6379. if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
  6380. break;
  6381. }
  6382. if (i >= MAX_WAIT_CNT) {
  6383. dev_err(&tp->pdev->dev,
  6384. "%s timed out, TX_MODE_ENABLE will not clear "
  6385. "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
  6386. err |= -ENODEV;
  6387. }
  6388. err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
  6389. err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
  6390. err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
  6391. tw32(FTQ_RESET, 0xffffffff);
  6392. tw32(FTQ_RESET, 0x00000000);
  6393. err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
  6394. err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
  6395. for (i = 0; i < tp->irq_cnt; i++) {
  6396. struct tg3_napi *tnapi = &tp->napi[i];
  6397. if (tnapi->hw_status)
  6398. memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
  6399. }
  6400. return err;
  6401. }
  6402. /* Save PCI command register before chip reset */
  6403. static void tg3_save_pci_state(struct tg3 *tp)
  6404. {
  6405. pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
  6406. }
  6407. /* Restore PCI state after chip reset */
  6408. static void tg3_restore_pci_state(struct tg3 *tp)
  6409. {
  6410. u32 val;
  6411. /* Re-enable indirect register accesses. */
  6412. pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
  6413. tp->misc_host_ctrl);
  6414. /* Set MAX PCI retry to zero. */
  6415. val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
  6416. if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
  6417. tg3_flag(tp, PCIX_MODE))
  6418. val |= PCISTATE_RETRY_SAME_DMA;
  6419. /* Allow reads and writes to the APE register and memory space. */
  6420. if (tg3_flag(tp, ENABLE_APE))
  6421. val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
  6422. PCISTATE_ALLOW_APE_SHMEM_WR |
  6423. PCISTATE_ALLOW_APE_PSPACE_WR;
  6424. pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
  6425. pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
  6426. if (!tg3_flag(tp, PCI_EXPRESS)) {
  6427. pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
  6428. tp->pci_cacheline_sz);
  6429. pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
  6430. tp->pci_lat_timer);
  6431. }
  6432. /* Make sure PCI-X relaxed ordering bit is clear. */
  6433. if (tg3_flag(tp, PCIX_MODE)) {
  6434. u16 pcix_cmd;
  6435. pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
  6436. &pcix_cmd);
  6437. pcix_cmd &= ~PCI_X_CMD_ERO;
  6438. pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
  6439. pcix_cmd);
  6440. }
  6441. if (tg3_flag(tp, 5780_CLASS)) {
  6442. /* Chip reset on 5780 will reset MSI enable bit,
  6443. * so need to restore it.
  6444. */
  6445. if (tg3_flag(tp, USING_MSI)) {
  6446. u16 ctrl;
  6447. pci_read_config_word(tp->pdev,
  6448. tp->msi_cap + PCI_MSI_FLAGS,
  6449. &ctrl);
  6450. pci_write_config_word(tp->pdev,
  6451. tp->msi_cap + PCI_MSI_FLAGS,
  6452. ctrl | PCI_MSI_FLAGS_ENABLE);
  6453. val = tr32(MSGINT_MODE);
  6454. tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
  6455. }
  6456. }
  6457. }
  6458. /* tp->lock is held. */
  6459. static int tg3_chip_reset(struct tg3 *tp)
  6460. {
  6461. u32 val;
  6462. void (*write_op)(struct tg3 *, u32, u32);
  6463. int i, err;
  6464. tg3_nvram_lock(tp);
  6465. tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
  6466. /* No matching tg3_nvram_unlock() after this because
  6467. * chip reset below will undo the nvram lock.
  6468. */
  6469. tp->nvram_lock_cnt = 0;
  6470. /* GRC_MISC_CFG core clock reset will clear the memory
  6471. * enable bit in PCI register 4 and the MSI enable bit
  6472. * on some chips, so we save relevant registers here.
  6473. */
  6474. tg3_save_pci_state(tp);
  6475. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
  6476. tg3_flag(tp, 5755_PLUS))
  6477. tw32(GRC_FASTBOOT_PC, 0);
  6478. /*
  6479. * We must avoid the readl() that normally takes place.
  6480. * It locks machines, causes machine checks, and other
  6481. * fun things. So, temporarily disable the 5701
  6482. * hardware workaround, while we do the reset.
  6483. */
  6484. write_op = tp->write32;
  6485. if (write_op == tg3_write_flush_reg32)
  6486. tp->write32 = tg3_write32;
  6487. /* Prevent the irq handler from reading or writing PCI registers
  6488. * during chip reset when the memory enable bit in the PCI command
  6489. * register may be cleared. The chip does not generate interrupt
  6490. * at this time, but the irq handler may still be called due to irq
  6491. * sharing or irqpoll.
  6492. */
  6493. tg3_flag_set(tp, CHIP_RESETTING);
  6494. for (i = 0; i < tp->irq_cnt; i++) {
  6495. struct tg3_napi *tnapi = &tp->napi[i];
  6496. if (tnapi->hw_status) {
  6497. tnapi->hw_status->status = 0;
  6498. tnapi->hw_status->status_tag = 0;
  6499. }
  6500. tnapi->last_tag = 0;
  6501. tnapi->last_irq_tag = 0;
  6502. }
  6503. smp_mb();
  6504. for (i = 0; i < tp->irq_cnt; i++)
  6505. synchronize_irq(tp->napi[i].irq_vec);
  6506. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
  6507. val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
  6508. tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
  6509. }
  6510. /* do the reset */
  6511. val = GRC_MISC_CFG_CORECLK_RESET;
  6512. if (tg3_flag(tp, PCI_EXPRESS)) {
  6513. /* Force PCIe 1.0a mode */
  6514. if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
  6515. !tg3_flag(tp, 57765_PLUS) &&
  6516. tr32(TG3_PCIE_PHY_TSTCTL) ==
  6517. (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
  6518. tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
  6519. if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
  6520. tw32(GRC_MISC_CFG, (1 << 29));
  6521. val |= (1 << 29);
  6522. }
  6523. }
  6524. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
  6525. tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
  6526. tw32(GRC_VCPU_EXT_CTRL,
  6527. tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
  6528. }
  6529. /* Manage gphy power for all CPMU absent PCIe devices. */
  6530. if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, CPMU_PRESENT))
  6531. val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
  6532. tw32(GRC_MISC_CFG, val);
  6533. /* restore 5701 hardware bug workaround write method */
  6534. tp->write32 = write_op;
  6535. /* Unfortunately, we have to delay before the PCI read back.
  6536. * Some 575X chips even will not respond to a PCI cfg access
  6537. * when the reset command is given to the chip.
  6538. *
  6539. * How do these hardware designers expect things to work
  6540. * properly if the PCI write is posted for a long period
  6541. * of time? It is always necessary to have some method by
  6542. * which a register read back can occur to push the write
  6543. * out which does the reset.
  6544. *
  6545. * For most tg3 variants the trick below was working.
  6546. * Ho hum...
  6547. */
  6548. udelay(120);
  6549. /* Flush PCI posted writes. The normal MMIO registers
  6550. * are inaccessible at this time so this is the only
  6551. * way to make this reliably (actually, this is no longer
  6552. * the case, see above). I tried to use indirect
  6553. * register read/write but this upset some 5701 variants.
  6554. */
  6555. pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
  6556. udelay(120);
  6557. if (tg3_flag(tp, PCI_EXPRESS) && pci_pcie_cap(tp->pdev)) {
  6558. u16 val16;
  6559. if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
  6560. int i;
  6561. u32 cfg_val;
  6562. /* Wait for link training to complete. */
  6563. for (i = 0; i < 5000; i++)
  6564. udelay(100);
  6565. pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
  6566. pci_write_config_dword(tp->pdev, 0xc4,
  6567. cfg_val | (1 << 15));
  6568. }
  6569. /* Clear the "no snoop" and "relaxed ordering" bits. */
  6570. pci_read_config_word(tp->pdev,
  6571. pci_pcie_cap(tp->pdev) + PCI_EXP_DEVCTL,
  6572. &val16);
  6573. val16 &= ~(PCI_EXP_DEVCTL_RELAX_EN |
  6574. PCI_EXP_DEVCTL_NOSNOOP_EN);
  6575. /*
  6576. * Older PCIe devices only support the 128 byte
  6577. * MPS setting. Enforce the restriction.
  6578. */
  6579. if (!tg3_flag(tp, CPMU_PRESENT))
  6580. val16 &= ~PCI_EXP_DEVCTL_PAYLOAD;
  6581. pci_write_config_word(tp->pdev,
  6582. pci_pcie_cap(tp->pdev) + PCI_EXP_DEVCTL,
  6583. val16);
  6584. /* Clear error status */
  6585. pci_write_config_word(tp->pdev,
  6586. pci_pcie_cap(tp->pdev) + PCI_EXP_DEVSTA,
  6587. PCI_EXP_DEVSTA_CED |
  6588. PCI_EXP_DEVSTA_NFED |
  6589. PCI_EXP_DEVSTA_FED |
  6590. PCI_EXP_DEVSTA_URD);
  6591. }
  6592. tg3_restore_pci_state(tp);
  6593. tg3_flag_clear(tp, CHIP_RESETTING);
  6594. tg3_flag_clear(tp, ERROR_PROCESSED);
  6595. val = 0;
  6596. if (tg3_flag(tp, 5780_CLASS))
  6597. val = tr32(MEMARB_MODE);
  6598. tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
  6599. if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
  6600. tg3_stop_fw(tp);
  6601. tw32(0x5000, 0x400);
  6602. }
  6603. tw32(GRC_MODE, tp->grc_mode);
  6604. if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
  6605. val = tr32(0xc4);
  6606. tw32(0xc4, val | (1 << 15));
  6607. }
  6608. if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
  6609. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
  6610. tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
  6611. if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
  6612. tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
  6613. tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
  6614. }
  6615. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
  6616. tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
  6617. val = tp->mac_mode;
  6618. } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
  6619. tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
  6620. val = tp->mac_mode;
  6621. } else
  6622. val = 0;
  6623. tw32_f(MAC_MODE, val);
  6624. udelay(40);
  6625. tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
  6626. err = tg3_poll_fw(tp);
  6627. if (err)
  6628. return err;
  6629. tg3_mdio_start(tp);
  6630. if (tg3_flag(tp, PCI_EXPRESS) &&
  6631. tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
  6632. GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
  6633. !tg3_flag(tp, 57765_PLUS)) {
  6634. val = tr32(0x7c00);
  6635. tw32(0x7c00, val | (1 << 25));
  6636. }
  6637. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
  6638. val = tr32(TG3_CPMU_CLCK_ORIDE);
  6639. tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
  6640. }
  6641. /* Reprobe ASF enable state. */
  6642. tg3_flag_clear(tp, ENABLE_ASF);
  6643. tg3_flag_clear(tp, ASF_NEW_HANDSHAKE);
  6644. tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
  6645. if (val == NIC_SRAM_DATA_SIG_MAGIC) {
  6646. u32 nic_cfg;
  6647. tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
  6648. if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
  6649. tg3_flag_set(tp, ENABLE_ASF);
  6650. tp->last_event_jiffies = jiffies;
  6651. if (tg3_flag(tp, 5750_PLUS))
  6652. tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
  6653. }
  6654. }
  6655. return 0;
  6656. }
  6657. static void tg3_get_nstats(struct tg3 *, struct rtnl_link_stats64 *);
  6658. static void tg3_get_estats(struct tg3 *, struct tg3_ethtool_stats *);
  6659. /* tp->lock is held. */
  6660. static int tg3_halt(struct tg3 *tp, int kind, int silent)
  6661. {
  6662. int err;
  6663. tg3_stop_fw(tp);
  6664. tg3_write_sig_pre_reset(tp, kind);
  6665. tg3_abort_hw(tp, silent);
  6666. err = tg3_chip_reset(tp);
  6667. __tg3_set_mac_addr(tp, 0);
  6668. tg3_write_sig_legacy(tp, kind);
  6669. tg3_write_sig_post_reset(tp, kind);
  6670. if (tp->hw_stats) {
  6671. /* Save the stats across chip resets... */
  6672. tg3_get_nstats(tp, &tp->net_stats_prev);
  6673. tg3_get_estats(tp, &tp->estats_prev);
  6674. /* And make sure the next sample is new data */
  6675. memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
  6676. }
  6677. if (err)
  6678. return err;
  6679. return 0;
  6680. }
  6681. static int tg3_set_mac_addr(struct net_device *dev, void *p)
  6682. {
  6683. struct tg3 *tp = netdev_priv(dev);
  6684. struct sockaddr *addr = p;
  6685. int err = 0, skip_mac_1 = 0;
  6686. if (!is_valid_ether_addr(addr->sa_data))
  6687. return -EADDRNOTAVAIL;
  6688. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  6689. if (!netif_running(dev))
  6690. return 0;
  6691. if (tg3_flag(tp, ENABLE_ASF)) {
  6692. u32 addr0_high, addr0_low, addr1_high, addr1_low;
  6693. addr0_high = tr32(MAC_ADDR_0_HIGH);
  6694. addr0_low = tr32(MAC_ADDR_0_LOW);
  6695. addr1_high = tr32(MAC_ADDR_1_HIGH);
  6696. addr1_low = tr32(MAC_ADDR_1_LOW);
  6697. /* Skip MAC addr 1 if ASF is using it. */
  6698. if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
  6699. !(addr1_high == 0 && addr1_low == 0))
  6700. skip_mac_1 = 1;
  6701. }
  6702. spin_lock_bh(&tp->lock);
  6703. __tg3_set_mac_addr(tp, skip_mac_1);
  6704. spin_unlock_bh(&tp->lock);
  6705. return err;
  6706. }
  6707. /* tp->lock is held. */
  6708. static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
  6709. dma_addr_t mapping, u32 maxlen_flags,
  6710. u32 nic_addr)
  6711. {
  6712. tg3_write_mem(tp,
  6713. (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
  6714. ((u64) mapping >> 32));
  6715. tg3_write_mem(tp,
  6716. (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
  6717. ((u64) mapping & 0xffffffff));
  6718. tg3_write_mem(tp,
  6719. (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
  6720. maxlen_flags);
  6721. if (!tg3_flag(tp, 5705_PLUS))
  6722. tg3_write_mem(tp,
  6723. (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
  6724. nic_addr);
  6725. }
  6726. static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
  6727. {
  6728. int i;
  6729. if (!tg3_flag(tp, ENABLE_TSS)) {
  6730. tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
  6731. tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
  6732. tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
  6733. } else {
  6734. tw32(HOSTCC_TXCOL_TICKS, 0);
  6735. tw32(HOSTCC_TXMAX_FRAMES, 0);
  6736. tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
  6737. }
  6738. if (!tg3_flag(tp, ENABLE_RSS)) {
  6739. tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
  6740. tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
  6741. tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
  6742. } else {
  6743. tw32(HOSTCC_RXCOL_TICKS, 0);
  6744. tw32(HOSTCC_RXMAX_FRAMES, 0);
  6745. tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
  6746. }
  6747. if (!tg3_flag(tp, 5705_PLUS)) {
  6748. u32 val = ec->stats_block_coalesce_usecs;
  6749. tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
  6750. tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
  6751. if (!netif_carrier_ok(tp->dev))
  6752. val = 0;
  6753. tw32(HOSTCC_STAT_COAL_TICKS, val);
  6754. }
  6755. for (i = 0; i < tp->irq_cnt - 1; i++) {
  6756. u32 reg;
  6757. reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
  6758. tw32(reg, ec->rx_coalesce_usecs);
  6759. reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
  6760. tw32(reg, ec->rx_max_coalesced_frames);
  6761. reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
  6762. tw32(reg, ec->rx_max_coalesced_frames_irq);
  6763. if (tg3_flag(tp, ENABLE_TSS)) {
  6764. reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
  6765. tw32(reg, ec->tx_coalesce_usecs);
  6766. reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
  6767. tw32(reg, ec->tx_max_coalesced_frames);
  6768. reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
  6769. tw32(reg, ec->tx_max_coalesced_frames_irq);
  6770. }
  6771. }
  6772. for (; i < tp->irq_max - 1; i++) {
  6773. tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
  6774. tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
  6775. tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
  6776. if (tg3_flag(tp, ENABLE_TSS)) {
  6777. tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
  6778. tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
  6779. tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
  6780. }
  6781. }
  6782. }
  6783. /* tp->lock is held. */
  6784. static void tg3_rings_reset(struct tg3 *tp)
  6785. {
  6786. int i;
  6787. u32 stblk, txrcb, rxrcb, limit;
  6788. struct tg3_napi *tnapi = &tp->napi[0];
  6789. /* Disable all transmit rings but the first. */
  6790. if (!tg3_flag(tp, 5705_PLUS))
  6791. limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
  6792. else if (tg3_flag(tp, 5717_PLUS))
  6793. limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
  6794. else if (tg3_flag(tp, 57765_CLASS))
  6795. limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
  6796. else
  6797. limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
  6798. for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
  6799. txrcb < limit; txrcb += TG3_BDINFO_SIZE)
  6800. tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
  6801. BDINFO_FLAGS_DISABLED);
  6802. /* Disable all receive return rings but the first. */
  6803. if (tg3_flag(tp, 5717_PLUS))
  6804. limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
  6805. else if (!tg3_flag(tp, 5705_PLUS))
  6806. limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
  6807. else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
  6808. tg3_flag(tp, 57765_CLASS))
  6809. limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
  6810. else
  6811. limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
  6812. for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
  6813. rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
  6814. tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
  6815. BDINFO_FLAGS_DISABLED);
  6816. /* Disable interrupts */
  6817. tw32_mailbox_f(tp->napi[0].int_mbox, 1);
  6818. tp->napi[0].chk_msi_cnt = 0;
  6819. tp->napi[0].last_rx_cons = 0;
  6820. tp->napi[0].last_tx_cons = 0;
  6821. /* Zero mailbox registers. */
  6822. if (tg3_flag(tp, SUPPORT_MSIX)) {
  6823. for (i = 1; i < tp->irq_max; i++) {
  6824. tp->napi[i].tx_prod = 0;
  6825. tp->napi[i].tx_cons = 0;
  6826. if (tg3_flag(tp, ENABLE_TSS))
  6827. tw32_mailbox(tp->napi[i].prodmbox, 0);
  6828. tw32_rx_mbox(tp->napi[i].consmbox, 0);
  6829. tw32_mailbox_f(tp->napi[i].int_mbox, 1);
  6830. tp->napi[i].chk_msi_cnt = 0;
  6831. tp->napi[i].last_rx_cons = 0;
  6832. tp->napi[i].last_tx_cons = 0;
  6833. }
  6834. if (!tg3_flag(tp, ENABLE_TSS))
  6835. tw32_mailbox(tp->napi[0].prodmbox, 0);
  6836. } else {
  6837. tp->napi[0].tx_prod = 0;
  6838. tp->napi[0].tx_cons = 0;
  6839. tw32_mailbox(tp->napi[0].prodmbox, 0);
  6840. tw32_rx_mbox(tp->napi[0].consmbox, 0);
  6841. }
  6842. /* Make sure the NIC-based send BD rings are disabled. */
  6843. if (!tg3_flag(tp, 5705_PLUS)) {
  6844. u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
  6845. for (i = 0; i < 16; i++)
  6846. tw32_tx_mbox(mbox + i * 8, 0);
  6847. }
  6848. txrcb = NIC_SRAM_SEND_RCB;
  6849. rxrcb = NIC_SRAM_RCV_RET_RCB;
  6850. /* Clear status block in ram. */
  6851. memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
  6852. /* Set status block DMA address */
  6853. tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
  6854. ((u64) tnapi->status_mapping >> 32));
  6855. tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
  6856. ((u64) tnapi->status_mapping & 0xffffffff));
  6857. if (tnapi->tx_ring) {
  6858. tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
  6859. (TG3_TX_RING_SIZE <<
  6860. BDINFO_FLAGS_MAXLEN_SHIFT),
  6861. NIC_SRAM_TX_BUFFER_DESC);
  6862. txrcb += TG3_BDINFO_SIZE;
  6863. }
  6864. if (tnapi->rx_rcb) {
  6865. tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
  6866. (tp->rx_ret_ring_mask + 1) <<
  6867. BDINFO_FLAGS_MAXLEN_SHIFT, 0);
  6868. rxrcb += TG3_BDINFO_SIZE;
  6869. }
  6870. stblk = HOSTCC_STATBLCK_RING1;
  6871. for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
  6872. u64 mapping = (u64)tnapi->status_mapping;
  6873. tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
  6874. tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
  6875. /* Clear status block in ram. */
  6876. memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
  6877. if (tnapi->tx_ring) {
  6878. tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
  6879. (TG3_TX_RING_SIZE <<
  6880. BDINFO_FLAGS_MAXLEN_SHIFT),
  6881. NIC_SRAM_TX_BUFFER_DESC);
  6882. txrcb += TG3_BDINFO_SIZE;
  6883. }
  6884. tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
  6885. ((tp->rx_ret_ring_mask + 1) <<
  6886. BDINFO_FLAGS_MAXLEN_SHIFT), 0);
  6887. stblk += 8;
  6888. rxrcb += TG3_BDINFO_SIZE;
  6889. }
  6890. }
  6891. static void tg3_setup_rxbd_thresholds(struct tg3 *tp)
  6892. {
  6893. u32 val, bdcache_maxcnt, host_rep_thresh, nic_rep_thresh;
  6894. if (!tg3_flag(tp, 5750_PLUS) ||
  6895. tg3_flag(tp, 5780_CLASS) ||
  6896. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
  6897. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
  6898. tg3_flag(tp, 57765_PLUS))
  6899. bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5700;
  6900. else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
  6901. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787)
  6902. bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5755;
  6903. else
  6904. bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5906;
  6905. nic_rep_thresh = min(bdcache_maxcnt / 2, tp->rx_std_max_post);
  6906. host_rep_thresh = max_t(u32, tp->rx_pending / 8, 1);
  6907. val = min(nic_rep_thresh, host_rep_thresh);
  6908. tw32(RCVBDI_STD_THRESH, val);
  6909. if (tg3_flag(tp, 57765_PLUS))
  6910. tw32(STD_REPLENISH_LWM, bdcache_maxcnt);
  6911. if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
  6912. return;
  6913. bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5700;
  6914. host_rep_thresh = max_t(u32, tp->rx_jumbo_pending / 8, 1);
  6915. val = min(bdcache_maxcnt / 2, host_rep_thresh);
  6916. tw32(RCVBDI_JUMBO_THRESH, val);
  6917. if (tg3_flag(tp, 57765_PLUS))
  6918. tw32(JMB_REPLENISH_LWM, bdcache_maxcnt);
  6919. }
  6920. static inline u32 calc_crc(unsigned char *buf, int len)
  6921. {
  6922. u32 reg;
  6923. u32 tmp;
  6924. int j, k;
  6925. reg = 0xffffffff;
  6926. for (j = 0; j < len; j++) {
  6927. reg ^= buf[j];
  6928. for (k = 0; k < 8; k++) {
  6929. tmp = reg & 0x01;
  6930. reg >>= 1;
  6931. if (tmp)
  6932. reg ^= 0xedb88320;
  6933. }
  6934. }
  6935. return ~reg;
  6936. }
  6937. static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
  6938. {
  6939. /* accept or reject all multicast frames */
  6940. tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
  6941. tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
  6942. tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
  6943. tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
  6944. }
  6945. static void __tg3_set_rx_mode(struct net_device *dev)
  6946. {
  6947. struct tg3 *tp = netdev_priv(dev);
  6948. u32 rx_mode;
  6949. rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
  6950. RX_MODE_KEEP_VLAN_TAG);
  6951. #if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
  6952. /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
  6953. * flag clear.
  6954. */
  6955. if (!tg3_flag(tp, ENABLE_ASF))
  6956. rx_mode |= RX_MODE_KEEP_VLAN_TAG;
  6957. #endif
  6958. if (dev->flags & IFF_PROMISC) {
  6959. /* Promiscuous mode. */
  6960. rx_mode |= RX_MODE_PROMISC;
  6961. } else if (dev->flags & IFF_ALLMULTI) {
  6962. /* Accept all multicast. */
  6963. tg3_set_multi(tp, 1);
  6964. } else if (netdev_mc_empty(dev)) {
  6965. /* Reject all multicast. */
  6966. tg3_set_multi(tp, 0);
  6967. } else {
  6968. /* Accept one or more multicast(s). */
  6969. struct netdev_hw_addr *ha;
  6970. u32 mc_filter[4] = { 0, };
  6971. u32 regidx;
  6972. u32 bit;
  6973. u32 crc;
  6974. netdev_for_each_mc_addr(ha, dev) {
  6975. crc = calc_crc(ha->addr, ETH_ALEN);
  6976. bit = ~crc & 0x7f;
  6977. regidx = (bit & 0x60) >> 5;
  6978. bit &= 0x1f;
  6979. mc_filter[regidx] |= (1 << bit);
  6980. }
  6981. tw32(MAC_HASH_REG_0, mc_filter[0]);
  6982. tw32(MAC_HASH_REG_1, mc_filter[1]);
  6983. tw32(MAC_HASH_REG_2, mc_filter[2]);
  6984. tw32(MAC_HASH_REG_3, mc_filter[3]);
  6985. }
  6986. if (rx_mode != tp->rx_mode) {
  6987. tp->rx_mode = rx_mode;
  6988. tw32_f(MAC_RX_MODE, rx_mode);
  6989. udelay(10);
  6990. }
  6991. }
  6992. static void tg3_rss_init_dflt_indir_tbl(struct tg3 *tp)
  6993. {
  6994. int i;
  6995. for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
  6996. tp->rss_ind_tbl[i] =
  6997. ethtool_rxfh_indir_default(i, tp->irq_cnt - 1);
  6998. }
  6999. static void tg3_rss_check_indir_tbl(struct tg3 *tp)
  7000. {
  7001. int i;
  7002. if (!tg3_flag(tp, SUPPORT_MSIX))
  7003. return;
  7004. if (tp->irq_cnt <= 2) {
  7005. memset(&tp->rss_ind_tbl[0], 0, sizeof(tp->rss_ind_tbl));
  7006. return;
  7007. }
  7008. /* Validate table against current IRQ count */
  7009. for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
  7010. if (tp->rss_ind_tbl[i] >= tp->irq_cnt - 1)
  7011. break;
  7012. }
  7013. if (i != TG3_RSS_INDIR_TBL_SIZE)
  7014. tg3_rss_init_dflt_indir_tbl(tp);
  7015. }
  7016. static void tg3_rss_write_indir_tbl(struct tg3 *tp)
  7017. {
  7018. int i = 0;
  7019. u32 reg = MAC_RSS_INDIR_TBL_0;
  7020. while (i < TG3_RSS_INDIR_TBL_SIZE) {
  7021. u32 val = tp->rss_ind_tbl[i];
  7022. i++;
  7023. for (; i % 8; i++) {
  7024. val <<= 4;
  7025. val |= tp->rss_ind_tbl[i];
  7026. }
  7027. tw32(reg, val);
  7028. reg += 4;
  7029. }
  7030. }
  7031. /* tp->lock is held. */
  7032. static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
  7033. {
  7034. u32 val, rdmac_mode;
  7035. int i, err, limit;
  7036. struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
  7037. tg3_disable_ints(tp);
  7038. tg3_stop_fw(tp);
  7039. tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
  7040. if (tg3_flag(tp, INIT_COMPLETE))
  7041. tg3_abort_hw(tp, 1);
  7042. /* Enable MAC control of LPI */
  7043. if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
  7044. tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL,
  7045. TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
  7046. TG3_CPMU_EEE_LNKIDL_UART_IDL);
  7047. tw32_f(TG3_CPMU_EEE_CTRL,
  7048. TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
  7049. val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
  7050. TG3_CPMU_EEEMD_LPI_IN_TX |
  7051. TG3_CPMU_EEEMD_LPI_IN_RX |
  7052. TG3_CPMU_EEEMD_EEE_ENABLE;
  7053. if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
  7054. val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
  7055. if (tg3_flag(tp, ENABLE_APE))
  7056. val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
  7057. tw32_f(TG3_CPMU_EEE_MODE, val);
  7058. tw32_f(TG3_CPMU_EEE_DBTMR1,
  7059. TG3_CPMU_DBTMR1_PCIEXIT_2047US |
  7060. TG3_CPMU_DBTMR1_LNKIDLE_2047US);
  7061. tw32_f(TG3_CPMU_EEE_DBTMR2,
  7062. TG3_CPMU_DBTMR2_APE_TX_2047US |
  7063. TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
  7064. }
  7065. if (reset_phy)
  7066. tg3_phy_reset(tp);
  7067. err = tg3_chip_reset(tp);
  7068. if (err)
  7069. return err;
  7070. tg3_write_sig_legacy(tp, RESET_KIND_INIT);
  7071. if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
  7072. val = tr32(TG3_CPMU_CTRL);
  7073. val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
  7074. tw32(TG3_CPMU_CTRL, val);
  7075. val = tr32(TG3_CPMU_LSPD_10MB_CLK);
  7076. val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
  7077. val |= CPMU_LSPD_10MB_MACCLK_6_25;
  7078. tw32(TG3_CPMU_LSPD_10MB_CLK, val);
  7079. val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
  7080. val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
  7081. val |= CPMU_LNK_AWARE_MACCLK_6_25;
  7082. tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
  7083. val = tr32(TG3_CPMU_HST_ACC);
  7084. val &= ~CPMU_HST_ACC_MACCLK_MASK;
  7085. val |= CPMU_HST_ACC_MACCLK_6_25;
  7086. tw32(TG3_CPMU_HST_ACC, val);
  7087. }
  7088. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
  7089. val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
  7090. val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
  7091. PCIE_PWR_MGMT_L1_THRESH_4MS;
  7092. tw32(PCIE_PWR_MGMT_THRESH, val);
  7093. val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
  7094. tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
  7095. tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
  7096. val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
  7097. tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
  7098. }
  7099. if (tg3_flag(tp, L1PLLPD_EN)) {
  7100. u32 grc_mode = tr32(GRC_MODE);
  7101. /* Access the lower 1K of PL PCIE block registers. */
  7102. val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
  7103. tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
  7104. val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
  7105. tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
  7106. val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
  7107. tw32(GRC_MODE, grc_mode);
  7108. }
  7109. if (tg3_flag(tp, 57765_CLASS)) {
  7110. if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
  7111. u32 grc_mode = tr32(GRC_MODE);
  7112. /* Access the lower 1K of PL PCIE block registers. */
  7113. val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
  7114. tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
  7115. val = tr32(TG3_PCIE_TLDLPL_PORT +
  7116. TG3_PCIE_PL_LO_PHYCTL5);
  7117. tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
  7118. val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
  7119. tw32(GRC_MODE, grc_mode);
  7120. }
  7121. if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_57765_AX) {
  7122. u32 grc_mode = tr32(GRC_MODE);
  7123. /* Access the lower 1K of DL PCIE block registers. */
  7124. val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
  7125. tw32(GRC_MODE, val | GRC_MODE_PCIE_DL_SEL);
  7126. val = tr32(TG3_PCIE_TLDLPL_PORT +
  7127. TG3_PCIE_DL_LO_FTSMAX);
  7128. val &= ~TG3_PCIE_DL_LO_FTSMAX_MSK;
  7129. tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_DL_LO_FTSMAX,
  7130. val | TG3_PCIE_DL_LO_FTSMAX_VAL);
  7131. tw32(GRC_MODE, grc_mode);
  7132. }
  7133. val = tr32(TG3_CPMU_LSPD_10MB_CLK);
  7134. val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
  7135. val |= CPMU_LSPD_10MB_MACCLK_6_25;
  7136. tw32(TG3_CPMU_LSPD_10MB_CLK, val);
  7137. }
  7138. /* This works around an issue with Athlon chipsets on
  7139. * B3 tigon3 silicon. This bit has no effect on any
  7140. * other revision. But do not set this on PCI Express
  7141. * chips and don't even touch the clocks if the CPMU is present.
  7142. */
  7143. if (!tg3_flag(tp, CPMU_PRESENT)) {
  7144. if (!tg3_flag(tp, PCI_EXPRESS))
  7145. tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
  7146. tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
  7147. }
  7148. if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
  7149. tg3_flag(tp, PCIX_MODE)) {
  7150. val = tr32(TG3PCI_PCISTATE);
  7151. val |= PCISTATE_RETRY_SAME_DMA;
  7152. tw32(TG3PCI_PCISTATE, val);
  7153. }
  7154. if (tg3_flag(tp, ENABLE_APE)) {
  7155. /* Allow reads and writes to the
  7156. * APE register and memory space.
  7157. */
  7158. val = tr32(TG3PCI_PCISTATE);
  7159. val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
  7160. PCISTATE_ALLOW_APE_SHMEM_WR |
  7161. PCISTATE_ALLOW_APE_PSPACE_WR;
  7162. tw32(TG3PCI_PCISTATE, val);
  7163. }
  7164. if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
  7165. /* Enable some hw fixes. */
  7166. val = tr32(TG3PCI_MSI_DATA);
  7167. val |= (1 << 26) | (1 << 28) | (1 << 29);
  7168. tw32(TG3PCI_MSI_DATA, val);
  7169. }
  7170. /* Descriptor ring init may make accesses to the
  7171. * NIC SRAM area to setup the TX descriptors, so we
  7172. * can only do this after the hardware has been
  7173. * successfully reset.
  7174. */
  7175. err = tg3_init_rings(tp);
  7176. if (err)
  7177. return err;
  7178. if (tg3_flag(tp, 57765_PLUS)) {
  7179. val = tr32(TG3PCI_DMA_RW_CTRL) &
  7180. ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
  7181. if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0)
  7182. val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
  7183. if (!tg3_flag(tp, 57765_CLASS) &&
  7184. GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
  7185. val |= DMA_RWCTRL_TAGGED_STAT_WA;
  7186. tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
  7187. } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
  7188. GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761) {
  7189. /* This value is determined during the probe time DMA
  7190. * engine test, tg3_test_dma.
  7191. */
  7192. tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
  7193. }
  7194. tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
  7195. GRC_MODE_4X_NIC_SEND_RINGS |
  7196. GRC_MODE_NO_TX_PHDR_CSUM |
  7197. GRC_MODE_NO_RX_PHDR_CSUM);
  7198. tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
  7199. /* Pseudo-header checksum is done by hardware logic and not
  7200. * the offload processers, so make the chip do the pseudo-
  7201. * header checksums on receive. For transmit it is more
  7202. * convenient to do the pseudo-header checksum in software
  7203. * as Linux does that on transmit for us in all cases.
  7204. */
  7205. tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
  7206. tw32(GRC_MODE,
  7207. tp->grc_mode |
  7208. (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
  7209. /* Setup the timer prescalar register. Clock is always 66Mhz. */
  7210. val = tr32(GRC_MISC_CFG);
  7211. val &= ~0xff;
  7212. val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
  7213. tw32(GRC_MISC_CFG, val);
  7214. /* Initialize MBUF/DESC pool. */
  7215. if (tg3_flag(tp, 5750_PLUS)) {
  7216. /* Do nothing. */
  7217. } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
  7218. tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
  7219. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
  7220. tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
  7221. else
  7222. tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
  7223. tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
  7224. tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
  7225. } else if (tg3_flag(tp, TSO_CAPABLE)) {
  7226. int fw_len;
  7227. fw_len = tp->fw_len;
  7228. fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
  7229. tw32(BUFMGR_MB_POOL_ADDR,
  7230. NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
  7231. tw32(BUFMGR_MB_POOL_SIZE,
  7232. NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
  7233. }
  7234. if (tp->dev->mtu <= ETH_DATA_LEN) {
  7235. tw32(BUFMGR_MB_RDMA_LOW_WATER,
  7236. tp->bufmgr_config.mbuf_read_dma_low_water);
  7237. tw32(BUFMGR_MB_MACRX_LOW_WATER,
  7238. tp->bufmgr_config.mbuf_mac_rx_low_water);
  7239. tw32(BUFMGR_MB_HIGH_WATER,
  7240. tp->bufmgr_config.mbuf_high_water);
  7241. } else {
  7242. tw32(BUFMGR_MB_RDMA_LOW_WATER,
  7243. tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
  7244. tw32(BUFMGR_MB_MACRX_LOW_WATER,
  7245. tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
  7246. tw32(BUFMGR_MB_HIGH_WATER,
  7247. tp->bufmgr_config.mbuf_high_water_jumbo);
  7248. }
  7249. tw32(BUFMGR_DMA_LOW_WATER,
  7250. tp->bufmgr_config.dma_low_water);
  7251. tw32(BUFMGR_DMA_HIGH_WATER,
  7252. tp->bufmgr_config.dma_high_water);
  7253. val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
  7254. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
  7255. val |= BUFMGR_MODE_NO_TX_UNDERRUN;
  7256. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
  7257. tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
  7258. tp->pci_chip_rev_id == CHIPREV_ID_5720_A0)
  7259. val |= BUFMGR_MODE_MBLOW_ATTN_ENAB;
  7260. tw32(BUFMGR_MODE, val);
  7261. for (i = 0; i < 2000; i++) {
  7262. if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
  7263. break;
  7264. udelay(10);
  7265. }
  7266. if (i >= 2000) {
  7267. netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
  7268. return -ENODEV;
  7269. }
  7270. if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
  7271. tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
  7272. tg3_setup_rxbd_thresholds(tp);
  7273. /* Initialize TG3_BDINFO's at:
  7274. * RCVDBDI_STD_BD: standard eth size rx ring
  7275. * RCVDBDI_JUMBO_BD: jumbo frame rx ring
  7276. * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
  7277. *
  7278. * like so:
  7279. * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
  7280. * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
  7281. * ring attribute flags
  7282. * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
  7283. *
  7284. * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
  7285. * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
  7286. *
  7287. * The size of each ring is fixed in the firmware, but the location is
  7288. * configurable.
  7289. */
  7290. tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
  7291. ((u64) tpr->rx_std_mapping >> 32));
  7292. tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
  7293. ((u64) tpr->rx_std_mapping & 0xffffffff));
  7294. if (!tg3_flag(tp, 5717_PLUS))
  7295. tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
  7296. NIC_SRAM_RX_BUFFER_DESC);
  7297. /* Disable the mini ring */
  7298. if (!tg3_flag(tp, 5705_PLUS))
  7299. tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
  7300. BDINFO_FLAGS_DISABLED);
  7301. /* Program the jumbo buffer descriptor ring control
  7302. * blocks on those devices that have them.
  7303. */
  7304. if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
  7305. (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))) {
  7306. if (tg3_flag(tp, JUMBO_RING_ENABLE)) {
  7307. tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
  7308. ((u64) tpr->rx_jmb_mapping >> 32));
  7309. tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
  7310. ((u64) tpr->rx_jmb_mapping & 0xffffffff));
  7311. val = TG3_RX_JMB_RING_SIZE(tp) <<
  7312. BDINFO_FLAGS_MAXLEN_SHIFT;
  7313. tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
  7314. val | BDINFO_FLAGS_USE_EXT_RECV);
  7315. if (!tg3_flag(tp, USE_JUMBO_BDFLAG) ||
  7316. tg3_flag(tp, 57765_CLASS))
  7317. tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
  7318. NIC_SRAM_RX_JUMBO_BUFFER_DESC);
  7319. } else {
  7320. tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
  7321. BDINFO_FLAGS_DISABLED);
  7322. }
  7323. if (tg3_flag(tp, 57765_PLUS)) {
  7324. val = TG3_RX_STD_RING_SIZE(tp);
  7325. val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
  7326. val |= (TG3_RX_STD_DMA_SZ << 2);
  7327. } else
  7328. val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
  7329. } else
  7330. val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
  7331. tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
  7332. tpr->rx_std_prod_idx = tp->rx_pending;
  7333. tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
  7334. tpr->rx_jmb_prod_idx =
  7335. tg3_flag(tp, JUMBO_RING_ENABLE) ? tp->rx_jumbo_pending : 0;
  7336. tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
  7337. tg3_rings_reset(tp);
  7338. /* Initialize MAC address and backoff seed. */
  7339. __tg3_set_mac_addr(tp, 0);
  7340. /* MTU + ethernet header + FCS + optional VLAN tag */
  7341. tw32(MAC_RX_MTU_SIZE,
  7342. tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
  7343. /* The slot time is changed by tg3_setup_phy if we
  7344. * run at gigabit with half duplex.
  7345. */
  7346. val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
  7347. (6 << TX_LENGTHS_IPG_SHIFT) |
  7348. (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
  7349. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
  7350. val |= tr32(MAC_TX_LENGTHS) &
  7351. (TX_LENGTHS_JMB_FRM_LEN_MSK |
  7352. TX_LENGTHS_CNT_DWN_VAL_MSK);
  7353. tw32(MAC_TX_LENGTHS, val);
  7354. /* Receive rules. */
  7355. tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
  7356. tw32(RCVLPC_CONFIG, 0x0181);
  7357. /* Calculate RDMAC_MODE setting early, we need it to determine
  7358. * the RCVLPC_STATE_ENABLE mask.
  7359. */
  7360. rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
  7361. RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
  7362. RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
  7363. RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
  7364. RDMAC_MODE_LNGREAD_ENAB);
  7365. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
  7366. rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
  7367. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
  7368. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
  7369. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
  7370. rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
  7371. RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
  7372. RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
  7373. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
  7374. tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
  7375. if (tg3_flag(tp, TSO_CAPABLE) &&
  7376. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
  7377. rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
  7378. } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
  7379. !tg3_flag(tp, IS_5788)) {
  7380. rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
  7381. }
  7382. }
  7383. if (tg3_flag(tp, PCI_EXPRESS))
  7384. rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
  7385. if (tg3_flag(tp, HW_TSO_1) ||
  7386. tg3_flag(tp, HW_TSO_2) ||
  7387. tg3_flag(tp, HW_TSO_3))
  7388. rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
  7389. if (tg3_flag(tp, 57765_PLUS) ||
  7390. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
  7391. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
  7392. rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
  7393. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
  7394. rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
  7395. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
  7396. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
  7397. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
  7398. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
  7399. tg3_flag(tp, 57765_PLUS)) {
  7400. val = tr32(TG3_RDMA_RSRVCTRL_REG);
  7401. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
  7402. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
  7403. val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
  7404. TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
  7405. TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
  7406. val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
  7407. TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
  7408. TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
  7409. }
  7410. tw32(TG3_RDMA_RSRVCTRL_REG,
  7411. val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
  7412. }
  7413. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
  7414. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
  7415. val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
  7416. tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val |
  7417. TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
  7418. TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
  7419. }
  7420. /* Receive/send statistics. */
  7421. if (tg3_flag(tp, 5750_PLUS)) {
  7422. val = tr32(RCVLPC_STATS_ENABLE);
  7423. val &= ~RCVLPC_STATSENAB_DACK_FIX;
  7424. tw32(RCVLPC_STATS_ENABLE, val);
  7425. } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
  7426. tg3_flag(tp, TSO_CAPABLE)) {
  7427. val = tr32(RCVLPC_STATS_ENABLE);
  7428. val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
  7429. tw32(RCVLPC_STATS_ENABLE, val);
  7430. } else {
  7431. tw32(RCVLPC_STATS_ENABLE, 0xffffff);
  7432. }
  7433. tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
  7434. tw32(SNDDATAI_STATSENAB, 0xffffff);
  7435. tw32(SNDDATAI_STATSCTRL,
  7436. (SNDDATAI_SCTRL_ENABLE |
  7437. SNDDATAI_SCTRL_FASTUPD));
  7438. /* Setup host coalescing engine. */
  7439. tw32(HOSTCC_MODE, 0);
  7440. for (i = 0; i < 2000; i++) {
  7441. if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
  7442. break;
  7443. udelay(10);
  7444. }
  7445. __tg3_set_coalesce(tp, &tp->coal);
  7446. if (!tg3_flag(tp, 5705_PLUS)) {
  7447. /* Status/statistics block address. See tg3_timer,
  7448. * the tg3_periodic_fetch_stats call there, and
  7449. * tg3_get_stats to see how this works for 5705/5750 chips.
  7450. */
  7451. tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
  7452. ((u64) tp->stats_mapping >> 32));
  7453. tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
  7454. ((u64) tp->stats_mapping & 0xffffffff));
  7455. tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
  7456. tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
  7457. /* Clear statistics and status block memory areas */
  7458. for (i = NIC_SRAM_STATS_BLK;
  7459. i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
  7460. i += sizeof(u32)) {
  7461. tg3_write_mem(tp, i, 0);
  7462. udelay(40);
  7463. }
  7464. }
  7465. tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
  7466. tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
  7467. tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
  7468. if (!tg3_flag(tp, 5705_PLUS))
  7469. tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
  7470. if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
  7471. tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
  7472. /* reset to prevent losing 1st rx packet intermittently */
  7473. tw32_f(MAC_RX_MODE, RX_MODE_RESET);
  7474. udelay(10);
  7475. }
  7476. tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
  7477. MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE |
  7478. MAC_MODE_FHDE_ENABLE;
  7479. if (tg3_flag(tp, ENABLE_APE))
  7480. tp->mac_mode |= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
  7481. if (!tg3_flag(tp, 5705_PLUS) &&
  7482. !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
  7483. GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
  7484. tp->mac_mode |= MAC_MODE_LINK_POLARITY;
  7485. tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
  7486. udelay(40);
  7487. /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
  7488. * If TG3_FLAG_IS_NIC is zero, we should read the
  7489. * register to preserve the GPIO settings for LOMs. The GPIOs,
  7490. * whether used as inputs or outputs, are set by boot code after
  7491. * reset.
  7492. */
  7493. if (!tg3_flag(tp, IS_NIC)) {
  7494. u32 gpio_mask;
  7495. gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
  7496. GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
  7497. GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
  7498. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
  7499. gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
  7500. GRC_LCLCTRL_GPIO_OUTPUT3;
  7501. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
  7502. gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
  7503. tp->grc_local_ctrl &= ~gpio_mask;
  7504. tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
  7505. /* GPIO1 must be driven high for eeprom write protect */
  7506. if (tg3_flag(tp, EEPROM_WRITE_PROT))
  7507. tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
  7508. GRC_LCLCTRL_GPIO_OUTPUT1);
  7509. }
  7510. tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
  7511. udelay(100);
  7512. if (tg3_flag(tp, USING_MSIX)) {
  7513. val = tr32(MSGINT_MODE);
  7514. val |= MSGINT_MODE_ENABLE;
  7515. if (tp->irq_cnt > 1)
  7516. val |= MSGINT_MODE_MULTIVEC_EN;
  7517. if (!tg3_flag(tp, 1SHOT_MSI))
  7518. val |= MSGINT_MODE_ONE_SHOT_DISABLE;
  7519. tw32(MSGINT_MODE, val);
  7520. }
  7521. if (!tg3_flag(tp, 5705_PLUS)) {
  7522. tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
  7523. udelay(40);
  7524. }
  7525. val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
  7526. WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
  7527. WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
  7528. WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
  7529. WDMAC_MODE_LNGREAD_ENAB);
  7530. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
  7531. tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
  7532. if (tg3_flag(tp, TSO_CAPABLE) &&
  7533. (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
  7534. tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
  7535. /* nothing */
  7536. } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
  7537. !tg3_flag(tp, IS_5788)) {
  7538. val |= WDMAC_MODE_RX_ACCEL;
  7539. }
  7540. }
  7541. /* Enable host coalescing bug fix */
  7542. if (tg3_flag(tp, 5755_PLUS))
  7543. val |= WDMAC_MODE_STATUS_TAG_FIX;
  7544. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
  7545. val |= WDMAC_MODE_BURST_ALL_DATA;
  7546. tw32_f(WDMAC_MODE, val);
  7547. udelay(40);
  7548. if (tg3_flag(tp, PCIX_MODE)) {
  7549. u16 pcix_cmd;
  7550. pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
  7551. &pcix_cmd);
  7552. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
  7553. pcix_cmd &= ~PCI_X_CMD_MAX_READ;
  7554. pcix_cmd |= PCI_X_CMD_READ_2K;
  7555. } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
  7556. pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
  7557. pcix_cmd |= PCI_X_CMD_READ_2K;
  7558. }
  7559. pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
  7560. pcix_cmd);
  7561. }
  7562. tw32_f(RDMAC_MODE, rdmac_mode);
  7563. udelay(40);
  7564. tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
  7565. if (!tg3_flag(tp, 5705_PLUS))
  7566. tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
  7567. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
  7568. tw32(SNDDATAC_MODE,
  7569. SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
  7570. else
  7571. tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
  7572. tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
  7573. tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
  7574. val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
  7575. if (tg3_flag(tp, LRG_PROD_RING_CAP))
  7576. val |= RCVDBDI_MODE_LRG_RING_SZ;
  7577. tw32(RCVDBDI_MODE, val);
  7578. tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
  7579. if (tg3_flag(tp, HW_TSO_1) ||
  7580. tg3_flag(tp, HW_TSO_2) ||
  7581. tg3_flag(tp, HW_TSO_3))
  7582. tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
  7583. val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
  7584. if (tg3_flag(tp, ENABLE_TSS))
  7585. val |= SNDBDI_MODE_MULTI_TXQ_EN;
  7586. tw32(SNDBDI_MODE, val);
  7587. tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
  7588. if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
  7589. err = tg3_load_5701_a0_firmware_fix(tp);
  7590. if (err)
  7591. return err;
  7592. }
  7593. if (tg3_flag(tp, TSO_CAPABLE)) {
  7594. err = tg3_load_tso_firmware(tp);
  7595. if (err)
  7596. return err;
  7597. }
  7598. tp->tx_mode = TX_MODE_ENABLE;
  7599. if (tg3_flag(tp, 5755_PLUS) ||
  7600. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
  7601. tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
  7602. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
  7603. val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
  7604. tp->tx_mode &= ~val;
  7605. tp->tx_mode |= tr32(MAC_TX_MODE) & val;
  7606. }
  7607. tw32_f(MAC_TX_MODE, tp->tx_mode);
  7608. udelay(100);
  7609. if (tg3_flag(tp, ENABLE_RSS)) {
  7610. tg3_rss_write_indir_tbl(tp);
  7611. /* Setup the "secret" hash key. */
  7612. tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
  7613. tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
  7614. tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
  7615. tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
  7616. tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
  7617. tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
  7618. tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
  7619. tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
  7620. tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
  7621. tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
  7622. }
  7623. tp->rx_mode = RX_MODE_ENABLE;
  7624. if (tg3_flag(tp, 5755_PLUS))
  7625. tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
  7626. if (tg3_flag(tp, ENABLE_RSS))
  7627. tp->rx_mode |= RX_MODE_RSS_ENABLE |
  7628. RX_MODE_RSS_ITBL_HASH_BITS_7 |
  7629. RX_MODE_RSS_IPV6_HASH_EN |
  7630. RX_MODE_RSS_TCP_IPV6_HASH_EN |
  7631. RX_MODE_RSS_IPV4_HASH_EN |
  7632. RX_MODE_RSS_TCP_IPV4_HASH_EN;
  7633. tw32_f(MAC_RX_MODE, tp->rx_mode);
  7634. udelay(10);
  7635. tw32(MAC_LED_CTRL, tp->led_ctrl);
  7636. tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
  7637. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
  7638. tw32_f(MAC_RX_MODE, RX_MODE_RESET);
  7639. udelay(10);
  7640. }
  7641. tw32_f(MAC_RX_MODE, tp->rx_mode);
  7642. udelay(10);
  7643. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
  7644. if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
  7645. !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
  7646. /* Set drive transmission level to 1.2V */
  7647. /* only if the signal pre-emphasis bit is not set */
  7648. val = tr32(MAC_SERDES_CFG);
  7649. val &= 0xfffff000;
  7650. val |= 0x880;
  7651. tw32(MAC_SERDES_CFG, val);
  7652. }
  7653. if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
  7654. tw32(MAC_SERDES_CFG, 0x616000);
  7655. }
  7656. /* Prevent chip from dropping frames when flow control
  7657. * is enabled.
  7658. */
  7659. if (tg3_flag(tp, 57765_CLASS))
  7660. val = 1;
  7661. else
  7662. val = 2;
  7663. tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
  7664. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
  7665. (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
  7666. /* Use hardware link auto-negotiation */
  7667. tg3_flag_set(tp, HW_AUTONEG);
  7668. }
  7669. if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
  7670. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
  7671. u32 tmp;
  7672. tmp = tr32(SERDES_RX_CTRL);
  7673. tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
  7674. tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
  7675. tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
  7676. tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
  7677. }
  7678. if (!tg3_flag(tp, USE_PHYLIB)) {
  7679. if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
  7680. tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
  7681. err = tg3_setup_phy(tp, 0);
  7682. if (err)
  7683. return err;
  7684. if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
  7685. !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
  7686. u32 tmp;
  7687. /* Clear CRC stats. */
  7688. if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
  7689. tg3_writephy(tp, MII_TG3_TEST1,
  7690. tmp | MII_TG3_TEST1_CRC_EN);
  7691. tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
  7692. }
  7693. }
  7694. }
  7695. __tg3_set_rx_mode(tp->dev);
  7696. /* Initialize receive rules. */
  7697. tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
  7698. tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
  7699. tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
  7700. tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
  7701. if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS))
  7702. limit = 8;
  7703. else
  7704. limit = 16;
  7705. if (tg3_flag(tp, ENABLE_ASF))
  7706. limit -= 4;
  7707. switch (limit) {
  7708. case 16:
  7709. tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
  7710. case 15:
  7711. tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
  7712. case 14:
  7713. tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
  7714. case 13:
  7715. tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
  7716. case 12:
  7717. tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
  7718. case 11:
  7719. tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
  7720. case 10:
  7721. tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
  7722. case 9:
  7723. tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
  7724. case 8:
  7725. tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
  7726. case 7:
  7727. tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
  7728. case 6:
  7729. tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
  7730. case 5:
  7731. tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
  7732. case 4:
  7733. /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
  7734. case 3:
  7735. /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
  7736. case 2:
  7737. case 1:
  7738. default:
  7739. break;
  7740. }
  7741. if (tg3_flag(tp, ENABLE_APE))
  7742. /* Write our heartbeat update interval to APE. */
  7743. tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
  7744. APE_HOST_HEARTBEAT_INT_DISABLE);
  7745. tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
  7746. return 0;
  7747. }
  7748. /* Called at device open time to get the chip ready for
  7749. * packet processing. Invoked with tp->lock held.
  7750. */
  7751. static int tg3_init_hw(struct tg3 *tp, int reset_phy)
  7752. {
  7753. tg3_switch_clocks(tp);
  7754. tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
  7755. return tg3_reset_hw(tp, reset_phy);
  7756. }
  7757. #define TG3_STAT_ADD32(PSTAT, REG) \
  7758. do { u32 __val = tr32(REG); \
  7759. (PSTAT)->low += __val; \
  7760. if ((PSTAT)->low < __val) \
  7761. (PSTAT)->high += 1; \
  7762. } while (0)
  7763. static void tg3_periodic_fetch_stats(struct tg3 *tp)
  7764. {
  7765. struct tg3_hw_stats *sp = tp->hw_stats;
  7766. if (!netif_carrier_ok(tp->dev))
  7767. return;
  7768. TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
  7769. TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
  7770. TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
  7771. TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
  7772. TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
  7773. TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
  7774. TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
  7775. TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
  7776. TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
  7777. TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
  7778. TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
  7779. TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
  7780. TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
  7781. TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
  7782. TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
  7783. TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
  7784. TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
  7785. TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
  7786. TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
  7787. TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
  7788. TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
  7789. TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
  7790. TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
  7791. TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
  7792. TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
  7793. TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
  7794. TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
  7795. TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
  7796. if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
  7797. tp->pci_chip_rev_id != CHIPREV_ID_5719_A0 &&
  7798. tp->pci_chip_rev_id != CHIPREV_ID_5720_A0) {
  7799. TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
  7800. } else {
  7801. u32 val = tr32(HOSTCC_FLOW_ATTN);
  7802. val = (val & HOSTCC_FLOW_ATTN_MBUF_LWM) ? 1 : 0;
  7803. if (val) {
  7804. tw32(HOSTCC_FLOW_ATTN, HOSTCC_FLOW_ATTN_MBUF_LWM);
  7805. sp->rx_discards.low += val;
  7806. if (sp->rx_discards.low < val)
  7807. sp->rx_discards.high += 1;
  7808. }
  7809. sp->mbuf_lwm_thresh_hit = sp->rx_discards;
  7810. }
  7811. TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
  7812. }
  7813. static void tg3_chk_missed_msi(struct tg3 *tp)
  7814. {
  7815. u32 i;
  7816. for (i = 0; i < tp->irq_cnt; i++) {
  7817. struct tg3_napi *tnapi = &tp->napi[i];
  7818. if (tg3_has_work(tnapi)) {
  7819. if (tnapi->last_rx_cons == tnapi->rx_rcb_ptr &&
  7820. tnapi->last_tx_cons == tnapi->tx_cons) {
  7821. if (tnapi->chk_msi_cnt < 1) {
  7822. tnapi->chk_msi_cnt++;
  7823. return;
  7824. }
  7825. tg3_msi(0, tnapi);
  7826. }
  7827. }
  7828. tnapi->chk_msi_cnt = 0;
  7829. tnapi->last_rx_cons = tnapi->rx_rcb_ptr;
  7830. tnapi->last_tx_cons = tnapi->tx_cons;
  7831. }
  7832. }
  7833. static void tg3_timer(unsigned long __opaque)
  7834. {
  7835. struct tg3 *tp = (struct tg3 *) __opaque;
  7836. if (tp->irq_sync || tg3_flag(tp, RESET_TASK_PENDING))
  7837. goto restart_timer;
  7838. spin_lock(&tp->lock);
  7839. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
  7840. tg3_flag(tp, 57765_CLASS))
  7841. tg3_chk_missed_msi(tp);
  7842. if (!tg3_flag(tp, TAGGED_STATUS)) {
  7843. /* All of this garbage is because when using non-tagged
  7844. * IRQ status the mailbox/status_block protocol the chip
  7845. * uses with the cpu is race prone.
  7846. */
  7847. if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
  7848. tw32(GRC_LOCAL_CTRL,
  7849. tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
  7850. } else {
  7851. tw32(HOSTCC_MODE, tp->coalesce_mode |
  7852. HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
  7853. }
  7854. if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
  7855. spin_unlock(&tp->lock);
  7856. tg3_reset_task_schedule(tp);
  7857. goto restart_timer;
  7858. }
  7859. }
  7860. /* This part only runs once per second. */
  7861. if (!--tp->timer_counter) {
  7862. if (tg3_flag(tp, 5705_PLUS))
  7863. tg3_periodic_fetch_stats(tp);
  7864. if (tp->setlpicnt && !--tp->setlpicnt)
  7865. tg3_phy_eee_enable(tp);
  7866. if (tg3_flag(tp, USE_LINKCHG_REG)) {
  7867. u32 mac_stat;
  7868. int phy_event;
  7869. mac_stat = tr32(MAC_STATUS);
  7870. phy_event = 0;
  7871. if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
  7872. if (mac_stat & MAC_STATUS_MI_INTERRUPT)
  7873. phy_event = 1;
  7874. } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
  7875. phy_event = 1;
  7876. if (phy_event)
  7877. tg3_setup_phy(tp, 0);
  7878. } else if (tg3_flag(tp, POLL_SERDES)) {
  7879. u32 mac_stat = tr32(MAC_STATUS);
  7880. int need_setup = 0;
  7881. if (netif_carrier_ok(tp->dev) &&
  7882. (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
  7883. need_setup = 1;
  7884. }
  7885. if (!netif_carrier_ok(tp->dev) &&
  7886. (mac_stat & (MAC_STATUS_PCS_SYNCED |
  7887. MAC_STATUS_SIGNAL_DET))) {
  7888. need_setup = 1;
  7889. }
  7890. if (need_setup) {
  7891. if (!tp->serdes_counter) {
  7892. tw32_f(MAC_MODE,
  7893. (tp->mac_mode &
  7894. ~MAC_MODE_PORT_MODE_MASK));
  7895. udelay(40);
  7896. tw32_f(MAC_MODE, tp->mac_mode);
  7897. udelay(40);
  7898. }
  7899. tg3_setup_phy(tp, 0);
  7900. }
  7901. } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
  7902. tg3_flag(tp, 5780_CLASS)) {
  7903. tg3_serdes_parallel_detect(tp);
  7904. }
  7905. tp->timer_counter = tp->timer_multiplier;
  7906. }
  7907. /* Heartbeat is only sent once every 2 seconds.
  7908. *
  7909. * The heartbeat is to tell the ASF firmware that the host
  7910. * driver is still alive. In the event that the OS crashes,
  7911. * ASF needs to reset the hardware to free up the FIFO space
  7912. * that may be filled with rx packets destined for the host.
  7913. * If the FIFO is full, ASF will no longer function properly.
  7914. *
  7915. * Unintended resets have been reported on real time kernels
  7916. * where the timer doesn't run on time. Netpoll will also have
  7917. * same problem.
  7918. *
  7919. * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
  7920. * to check the ring condition when the heartbeat is expiring
  7921. * before doing the reset. This will prevent most unintended
  7922. * resets.
  7923. */
  7924. if (!--tp->asf_counter) {
  7925. if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
  7926. tg3_wait_for_event_ack(tp);
  7927. tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
  7928. FWCMD_NICDRV_ALIVE3);
  7929. tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
  7930. tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
  7931. TG3_FW_UPDATE_TIMEOUT_SEC);
  7932. tg3_generate_fw_event(tp);
  7933. }
  7934. tp->asf_counter = tp->asf_multiplier;
  7935. }
  7936. spin_unlock(&tp->lock);
  7937. restart_timer:
  7938. tp->timer.expires = jiffies + tp->timer_offset;
  7939. add_timer(&tp->timer);
  7940. }
  7941. static void __devinit tg3_timer_init(struct tg3 *tp)
  7942. {
  7943. if (tg3_flag(tp, TAGGED_STATUS) &&
  7944. GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
  7945. !tg3_flag(tp, 57765_CLASS))
  7946. tp->timer_offset = HZ;
  7947. else
  7948. tp->timer_offset = HZ / 10;
  7949. BUG_ON(tp->timer_offset > HZ);
  7950. tp->timer_multiplier = (HZ / tp->timer_offset);
  7951. tp->asf_multiplier = (HZ / tp->timer_offset) *
  7952. TG3_FW_UPDATE_FREQ_SEC;
  7953. init_timer(&tp->timer);
  7954. tp->timer.data = (unsigned long) tp;
  7955. tp->timer.function = tg3_timer;
  7956. }
  7957. static void tg3_timer_start(struct tg3 *tp)
  7958. {
  7959. tp->asf_counter = tp->asf_multiplier;
  7960. tp->timer_counter = tp->timer_multiplier;
  7961. tp->timer.expires = jiffies + tp->timer_offset;
  7962. add_timer(&tp->timer);
  7963. }
  7964. static void tg3_timer_stop(struct tg3 *tp)
  7965. {
  7966. del_timer_sync(&tp->timer);
  7967. }
  7968. /* Restart hardware after configuration changes, self-test, etc.
  7969. * Invoked with tp->lock held.
  7970. */
  7971. static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
  7972. __releases(tp->lock)
  7973. __acquires(tp->lock)
  7974. {
  7975. int err;
  7976. err = tg3_init_hw(tp, reset_phy);
  7977. if (err) {
  7978. netdev_err(tp->dev,
  7979. "Failed to re-initialize device, aborting\n");
  7980. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  7981. tg3_full_unlock(tp);
  7982. tg3_timer_stop(tp);
  7983. tp->irq_sync = 0;
  7984. tg3_napi_enable(tp);
  7985. dev_close(tp->dev);
  7986. tg3_full_lock(tp, 0);
  7987. }
  7988. return err;
  7989. }
  7990. static void tg3_reset_task(struct work_struct *work)
  7991. {
  7992. struct tg3 *tp = container_of(work, struct tg3, reset_task);
  7993. int err;
  7994. tg3_full_lock(tp, 0);
  7995. if (!netif_running(tp->dev)) {
  7996. tg3_flag_clear(tp, RESET_TASK_PENDING);
  7997. tg3_full_unlock(tp);
  7998. return;
  7999. }
  8000. tg3_full_unlock(tp);
  8001. tg3_phy_stop(tp);
  8002. tg3_netif_stop(tp);
  8003. tg3_full_lock(tp, 1);
  8004. if (tg3_flag(tp, TX_RECOVERY_PENDING)) {
  8005. tp->write32_tx_mbox = tg3_write32_tx_mbox;
  8006. tp->write32_rx_mbox = tg3_write_flush_reg32;
  8007. tg3_flag_set(tp, MBOX_WRITE_REORDER);
  8008. tg3_flag_clear(tp, TX_RECOVERY_PENDING);
  8009. }
  8010. tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
  8011. err = tg3_init_hw(tp, 1);
  8012. if (err)
  8013. goto out;
  8014. tg3_netif_start(tp);
  8015. out:
  8016. tg3_full_unlock(tp);
  8017. if (!err)
  8018. tg3_phy_start(tp);
  8019. tg3_flag_clear(tp, RESET_TASK_PENDING);
  8020. }
  8021. static int tg3_request_irq(struct tg3 *tp, int irq_num)
  8022. {
  8023. irq_handler_t fn;
  8024. unsigned long flags;
  8025. char *name;
  8026. struct tg3_napi *tnapi = &tp->napi[irq_num];
  8027. if (tp->irq_cnt == 1)
  8028. name = tp->dev->name;
  8029. else {
  8030. name = &tnapi->irq_lbl[0];
  8031. snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
  8032. name[IFNAMSIZ-1] = 0;
  8033. }
  8034. if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
  8035. fn = tg3_msi;
  8036. if (tg3_flag(tp, 1SHOT_MSI))
  8037. fn = tg3_msi_1shot;
  8038. flags = 0;
  8039. } else {
  8040. fn = tg3_interrupt;
  8041. if (tg3_flag(tp, TAGGED_STATUS))
  8042. fn = tg3_interrupt_tagged;
  8043. flags = IRQF_SHARED;
  8044. }
  8045. return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
  8046. }
  8047. static int tg3_test_interrupt(struct tg3 *tp)
  8048. {
  8049. struct tg3_napi *tnapi = &tp->napi[0];
  8050. struct net_device *dev = tp->dev;
  8051. int err, i, intr_ok = 0;
  8052. u32 val;
  8053. if (!netif_running(dev))
  8054. return -ENODEV;
  8055. tg3_disable_ints(tp);
  8056. free_irq(tnapi->irq_vec, tnapi);
  8057. /*
  8058. * Turn off MSI one shot mode. Otherwise this test has no
  8059. * observable way to know whether the interrupt was delivered.
  8060. */
  8061. if (tg3_flag(tp, 57765_PLUS)) {
  8062. val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
  8063. tw32(MSGINT_MODE, val);
  8064. }
  8065. err = request_irq(tnapi->irq_vec, tg3_test_isr,
  8066. IRQF_SHARED, dev->name, tnapi);
  8067. if (err)
  8068. return err;
  8069. tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
  8070. tg3_enable_ints(tp);
  8071. tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
  8072. tnapi->coal_now);
  8073. for (i = 0; i < 5; i++) {
  8074. u32 int_mbox, misc_host_ctrl;
  8075. int_mbox = tr32_mailbox(tnapi->int_mbox);
  8076. misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
  8077. if ((int_mbox != 0) ||
  8078. (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
  8079. intr_ok = 1;
  8080. break;
  8081. }
  8082. if (tg3_flag(tp, 57765_PLUS) &&
  8083. tnapi->hw_status->status_tag != tnapi->last_tag)
  8084. tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
  8085. msleep(10);
  8086. }
  8087. tg3_disable_ints(tp);
  8088. free_irq(tnapi->irq_vec, tnapi);
  8089. err = tg3_request_irq(tp, 0);
  8090. if (err)
  8091. return err;
  8092. if (intr_ok) {
  8093. /* Reenable MSI one shot mode. */
  8094. if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, 1SHOT_MSI)) {
  8095. val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
  8096. tw32(MSGINT_MODE, val);
  8097. }
  8098. return 0;
  8099. }
  8100. return -EIO;
  8101. }
  8102. /* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
  8103. * successfully restored
  8104. */
  8105. static int tg3_test_msi(struct tg3 *tp)
  8106. {
  8107. int err;
  8108. u16 pci_cmd;
  8109. if (!tg3_flag(tp, USING_MSI))
  8110. return 0;
  8111. /* Turn off SERR reporting in case MSI terminates with Master
  8112. * Abort.
  8113. */
  8114. pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
  8115. pci_write_config_word(tp->pdev, PCI_COMMAND,
  8116. pci_cmd & ~PCI_COMMAND_SERR);
  8117. err = tg3_test_interrupt(tp);
  8118. pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
  8119. if (!err)
  8120. return 0;
  8121. /* other failures */
  8122. if (err != -EIO)
  8123. return err;
  8124. /* MSI test failed, go back to INTx mode */
  8125. netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
  8126. "to INTx mode. Please report this failure to the PCI "
  8127. "maintainer and include system chipset information\n");
  8128. free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
  8129. pci_disable_msi(tp->pdev);
  8130. tg3_flag_clear(tp, USING_MSI);
  8131. tp->napi[0].irq_vec = tp->pdev->irq;
  8132. err = tg3_request_irq(tp, 0);
  8133. if (err)
  8134. return err;
  8135. /* Need to reset the chip because the MSI cycle may have terminated
  8136. * with Master Abort.
  8137. */
  8138. tg3_full_lock(tp, 1);
  8139. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  8140. err = tg3_init_hw(tp, 1);
  8141. tg3_full_unlock(tp);
  8142. if (err)
  8143. free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
  8144. return err;
  8145. }
  8146. static int tg3_request_firmware(struct tg3 *tp)
  8147. {
  8148. const __be32 *fw_data;
  8149. if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
  8150. netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
  8151. tp->fw_needed);
  8152. return -ENOENT;
  8153. }
  8154. fw_data = (void *)tp->fw->data;
  8155. /* Firmware blob starts with version numbers, followed by
  8156. * start address and _full_ length including BSS sections
  8157. * (which must be longer than the actual data, of course
  8158. */
  8159. tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
  8160. if (tp->fw_len < (tp->fw->size - 12)) {
  8161. netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
  8162. tp->fw_len, tp->fw_needed);
  8163. release_firmware(tp->fw);
  8164. tp->fw = NULL;
  8165. return -EINVAL;
  8166. }
  8167. /* We no longer need firmware; we have it. */
  8168. tp->fw_needed = NULL;
  8169. return 0;
  8170. }
  8171. static bool tg3_enable_msix(struct tg3 *tp)
  8172. {
  8173. int i, rc;
  8174. struct msix_entry msix_ent[tp->irq_max];
  8175. tp->irq_cnt = netif_get_num_default_rss_queues();
  8176. if (tp->irq_cnt > 1) {
  8177. /* We want as many rx rings enabled as there are cpus.
  8178. * In multiqueue MSI-X mode, the first MSI-X vector
  8179. * only deals with link interrupts, etc, so we add
  8180. * one to the number of vectors we are requesting.
  8181. */
  8182. tp->irq_cnt = min_t(unsigned, tp->irq_cnt + 1, tp->irq_max);
  8183. }
  8184. for (i = 0; i < tp->irq_max; i++) {
  8185. msix_ent[i].entry = i;
  8186. msix_ent[i].vector = 0;
  8187. }
  8188. rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
  8189. if (rc < 0) {
  8190. return false;
  8191. } else if (rc != 0) {
  8192. if (pci_enable_msix(tp->pdev, msix_ent, rc))
  8193. return false;
  8194. netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
  8195. tp->irq_cnt, rc);
  8196. tp->irq_cnt = rc;
  8197. }
  8198. for (i = 0; i < tp->irq_max; i++)
  8199. tp->napi[i].irq_vec = msix_ent[i].vector;
  8200. netif_set_real_num_tx_queues(tp->dev, 1);
  8201. rc = tp->irq_cnt > 1 ? tp->irq_cnt - 1 : 1;
  8202. if (netif_set_real_num_rx_queues(tp->dev, rc)) {
  8203. pci_disable_msix(tp->pdev);
  8204. return false;
  8205. }
  8206. if (tp->irq_cnt > 1) {
  8207. tg3_flag_set(tp, ENABLE_RSS);
  8208. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
  8209. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
  8210. tg3_flag_set(tp, ENABLE_TSS);
  8211. netif_set_real_num_tx_queues(tp->dev, tp->irq_cnt - 1);
  8212. }
  8213. }
  8214. return true;
  8215. }
  8216. static void tg3_ints_init(struct tg3 *tp)
  8217. {
  8218. if ((tg3_flag(tp, SUPPORT_MSI) || tg3_flag(tp, SUPPORT_MSIX)) &&
  8219. !tg3_flag(tp, TAGGED_STATUS)) {
  8220. /* All MSI supporting chips should support tagged
  8221. * status. Assert that this is the case.
  8222. */
  8223. netdev_warn(tp->dev,
  8224. "MSI without TAGGED_STATUS? Not using MSI\n");
  8225. goto defcfg;
  8226. }
  8227. if (tg3_flag(tp, SUPPORT_MSIX) && tg3_enable_msix(tp))
  8228. tg3_flag_set(tp, USING_MSIX);
  8229. else if (tg3_flag(tp, SUPPORT_MSI) && pci_enable_msi(tp->pdev) == 0)
  8230. tg3_flag_set(tp, USING_MSI);
  8231. if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
  8232. u32 msi_mode = tr32(MSGINT_MODE);
  8233. if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1)
  8234. msi_mode |= MSGINT_MODE_MULTIVEC_EN;
  8235. if (!tg3_flag(tp, 1SHOT_MSI))
  8236. msi_mode |= MSGINT_MODE_ONE_SHOT_DISABLE;
  8237. tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
  8238. }
  8239. defcfg:
  8240. if (!tg3_flag(tp, USING_MSIX)) {
  8241. tp->irq_cnt = 1;
  8242. tp->napi[0].irq_vec = tp->pdev->irq;
  8243. netif_set_real_num_tx_queues(tp->dev, 1);
  8244. netif_set_real_num_rx_queues(tp->dev, 1);
  8245. }
  8246. }
  8247. static void tg3_ints_fini(struct tg3 *tp)
  8248. {
  8249. if (tg3_flag(tp, USING_MSIX))
  8250. pci_disable_msix(tp->pdev);
  8251. else if (tg3_flag(tp, USING_MSI))
  8252. pci_disable_msi(tp->pdev);
  8253. tg3_flag_clear(tp, USING_MSI);
  8254. tg3_flag_clear(tp, USING_MSIX);
  8255. tg3_flag_clear(tp, ENABLE_RSS);
  8256. tg3_flag_clear(tp, ENABLE_TSS);
  8257. }
  8258. static int tg3_open(struct net_device *dev)
  8259. {
  8260. struct tg3 *tp = netdev_priv(dev);
  8261. int i, err;
  8262. if (tp->fw_needed) {
  8263. err = tg3_request_firmware(tp);
  8264. if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
  8265. if (err)
  8266. return err;
  8267. } else if (err) {
  8268. netdev_warn(tp->dev, "TSO capability disabled\n");
  8269. tg3_flag_clear(tp, TSO_CAPABLE);
  8270. } else if (!tg3_flag(tp, TSO_CAPABLE)) {
  8271. netdev_notice(tp->dev, "TSO capability restored\n");
  8272. tg3_flag_set(tp, TSO_CAPABLE);
  8273. }
  8274. }
  8275. netif_carrier_off(tp->dev);
  8276. err = tg3_power_up(tp);
  8277. if (err)
  8278. return err;
  8279. tg3_full_lock(tp, 0);
  8280. tg3_disable_ints(tp);
  8281. tg3_flag_clear(tp, INIT_COMPLETE);
  8282. tg3_full_unlock(tp);
  8283. /*
  8284. * Setup interrupts first so we know how
  8285. * many NAPI resources to allocate
  8286. */
  8287. tg3_ints_init(tp);
  8288. tg3_rss_check_indir_tbl(tp);
  8289. /* The placement of this call is tied
  8290. * to the setup and use of Host TX descriptors.
  8291. */
  8292. err = tg3_alloc_consistent(tp);
  8293. if (err)
  8294. goto err_out1;
  8295. tg3_napi_init(tp);
  8296. tg3_napi_enable(tp);
  8297. for (i = 0; i < tp->irq_cnt; i++) {
  8298. struct tg3_napi *tnapi = &tp->napi[i];
  8299. err = tg3_request_irq(tp, i);
  8300. if (err) {
  8301. for (i--; i >= 0; i--) {
  8302. tnapi = &tp->napi[i];
  8303. free_irq(tnapi->irq_vec, tnapi);
  8304. }
  8305. goto err_out2;
  8306. }
  8307. }
  8308. tg3_full_lock(tp, 0);
  8309. err = tg3_init_hw(tp, 1);
  8310. if (err) {
  8311. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  8312. tg3_free_rings(tp);
  8313. }
  8314. tg3_full_unlock(tp);
  8315. if (err)
  8316. goto err_out3;
  8317. if (tg3_flag(tp, USING_MSI)) {
  8318. err = tg3_test_msi(tp);
  8319. if (err) {
  8320. tg3_full_lock(tp, 0);
  8321. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  8322. tg3_free_rings(tp);
  8323. tg3_full_unlock(tp);
  8324. goto err_out2;
  8325. }
  8326. if (!tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
  8327. u32 val = tr32(PCIE_TRANSACTION_CFG);
  8328. tw32(PCIE_TRANSACTION_CFG,
  8329. val | PCIE_TRANS_CFG_1SHOT_MSI);
  8330. }
  8331. }
  8332. tg3_phy_start(tp);
  8333. tg3_full_lock(tp, 0);
  8334. tg3_timer_start(tp);
  8335. tg3_flag_set(tp, INIT_COMPLETE);
  8336. tg3_enable_ints(tp);
  8337. tg3_full_unlock(tp);
  8338. netif_tx_start_all_queues(dev);
  8339. /*
  8340. * Reset loopback feature if it was turned on while the device was down
  8341. * make sure that it's installed properly now.
  8342. */
  8343. if (dev->features & NETIF_F_LOOPBACK)
  8344. tg3_set_loopback(dev, dev->features);
  8345. return 0;
  8346. err_out3:
  8347. for (i = tp->irq_cnt - 1; i >= 0; i--) {
  8348. struct tg3_napi *tnapi = &tp->napi[i];
  8349. free_irq(tnapi->irq_vec, tnapi);
  8350. }
  8351. err_out2:
  8352. tg3_napi_disable(tp);
  8353. tg3_napi_fini(tp);
  8354. tg3_free_consistent(tp);
  8355. err_out1:
  8356. tg3_ints_fini(tp);
  8357. tg3_frob_aux_power(tp, false);
  8358. pci_set_power_state(tp->pdev, PCI_D3hot);
  8359. return err;
  8360. }
  8361. static int tg3_close(struct net_device *dev)
  8362. {
  8363. int i;
  8364. struct tg3 *tp = netdev_priv(dev);
  8365. tg3_napi_disable(tp);
  8366. tg3_reset_task_cancel(tp);
  8367. netif_tx_stop_all_queues(dev);
  8368. tg3_timer_stop(tp);
  8369. tg3_phy_stop(tp);
  8370. tg3_full_lock(tp, 1);
  8371. tg3_disable_ints(tp);
  8372. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  8373. tg3_free_rings(tp);
  8374. tg3_flag_clear(tp, INIT_COMPLETE);
  8375. tg3_full_unlock(tp);
  8376. for (i = tp->irq_cnt - 1; i >= 0; i--) {
  8377. struct tg3_napi *tnapi = &tp->napi[i];
  8378. free_irq(tnapi->irq_vec, tnapi);
  8379. }
  8380. tg3_ints_fini(tp);
  8381. /* Clear stats across close / open calls */
  8382. memset(&tp->net_stats_prev, 0, sizeof(tp->net_stats_prev));
  8383. memset(&tp->estats_prev, 0, sizeof(tp->estats_prev));
  8384. tg3_napi_fini(tp);
  8385. tg3_free_consistent(tp);
  8386. tg3_power_down(tp);
  8387. netif_carrier_off(tp->dev);
  8388. return 0;
  8389. }
  8390. static inline u64 get_stat64(tg3_stat64_t *val)
  8391. {
  8392. return ((u64)val->high << 32) | ((u64)val->low);
  8393. }
  8394. static u64 tg3_calc_crc_errors(struct tg3 *tp)
  8395. {
  8396. struct tg3_hw_stats *hw_stats = tp->hw_stats;
  8397. if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
  8398. (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
  8399. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
  8400. u32 val;
  8401. if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
  8402. tg3_writephy(tp, MII_TG3_TEST1,
  8403. val | MII_TG3_TEST1_CRC_EN);
  8404. tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
  8405. } else
  8406. val = 0;
  8407. tp->phy_crc_errors += val;
  8408. return tp->phy_crc_errors;
  8409. }
  8410. return get_stat64(&hw_stats->rx_fcs_errors);
  8411. }
  8412. #define ESTAT_ADD(member) \
  8413. estats->member = old_estats->member + \
  8414. get_stat64(&hw_stats->member)
  8415. static void tg3_get_estats(struct tg3 *tp, struct tg3_ethtool_stats *estats)
  8416. {
  8417. struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
  8418. struct tg3_hw_stats *hw_stats = tp->hw_stats;
  8419. ESTAT_ADD(rx_octets);
  8420. ESTAT_ADD(rx_fragments);
  8421. ESTAT_ADD(rx_ucast_packets);
  8422. ESTAT_ADD(rx_mcast_packets);
  8423. ESTAT_ADD(rx_bcast_packets);
  8424. ESTAT_ADD(rx_fcs_errors);
  8425. ESTAT_ADD(rx_align_errors);
  8426. ESTAT_ADD(rx_xon_pause_rcvd);
  8427. ESTAT_ADD(rx_xoff_pause_rcvd);
  8428. ESTAT_ADD(rx_mac_ctrl_rcvd);
  8429. ESTAT_ADD(rx_xoff_entered);
  8430. ESTAT_ADD(rx_frame_too_long_errors);
  8431. ESTAT_ADD(rx_jabbers);
  8432. ESTAT_ADD(rx_undersize_packets);
  8433. ESTAT_ADD(rx_in_length_errors);
  8434. ESTAT_ADD(rx_out_length_errors);
  8435. ESTAT_ADD(rx_64_or_less_octet_packets);
  8436. ESTAT_ADD(rx_65_to_127_octet_packets);
  8437. ESTAT_ADD(rx_128_to_255_octet_packets);
  8438. ESTAT_ADD(rx_256_to_511_octet_packets);
  8439. ESTAT_ADD(rx_512_to_1023_octet_packets);
  8440. ESTAT_ADD(rx_1024_to_1522_octet_packets);
  8441. ESTAT_ADD(rx_1523_to_2047_octet_packets);
  8442. ESTAT_ADD(rx_2048_to_4095_octet_packets);
  8443. ESTAT_ADD(rx_4096_to_8191_octet_packets);
  8444. ESTAT_ADD(rx_8192_to_9022_octet_packets);
  8445. ESTAT_ADD(tx_octets);
  8446. ESTAT_ADD(tx_collisions);
  8447. ESTAT_ADD(tx_xon_sent);
  8448. ESTAT_ADD(tx_xoff_sent);
  8449. ESTAT_ADD(tx_flow_control);
  8450. ESTAT_ADD(tx_mac_errors);
  8451. ESTAT_ADD(tx_single_collisions);
  8452. ESTAT_ADD(tx_mult_collisions);
  8453. ESTAT_ADD(tx_deferred);
  8454. ESTAT_ADD(tx_excessive_collisions);
  8455. ESTAT_ADD(tx_late_collisions);
  8456. ESTAT_ADD(tx_collide_2times);
  8457. ESTAT_ADD(tx_collide_3times);
  8458. ESTAT_ADD(tx_collide_4times);
  8459. ESTAT_ADD(tx_collide_5times);
  8460. ESTAT_ADD(tx_collide_6times);
  8461. ESTAT_ADD(tx_collide_7times);
  8462. ESTAT_ADD(tx_collide_8times);
  8463. ESTAT_ADD(tx_collide_9times);
  8464. ESTAT_ADD(tx_collide_10times);
  8465. ESTAT_ADD(tx_collide_11times);
  8466. ESTAT_ADD(tx_collide_12times);
  8467. ESTAT_ADD(tx_collide_13times);
  8468. ESTAT_ADD(tx_collide_14times);
  8469. ESTAT_ADD(tx_collide_15times);
  8470. ESTAT_ADD(tx_ucast_packets);
  8471. ESTAT_ADD(tx_mcast_packets);
  8472. ESTAT_ADD(tx_bcast_packets);
  8473. ESTAT_ADD(tx_carrier_sense_errors);
  8474. ESTAT_ADD(tx_discards);
  8475. ESTAT_ADD(tx_errors);
  8476. ESTAT_ADD(dma_writeq_full);
  8477. ESTAT_ADD(dma_write_prioq_full);
  8478. ESTAT_ADD(rxbds_empty);
  8479. ESTAT_ADD(rx_discards);
  8480. ESTAT_ADD(rx_errors);
  8481. ESTAT_ADD(rx_threshold_hit);
  8482. ESTAT_ADD(dma_readq_full);
  8483. ESTAT_ADD(dma_read_prioq_full);
  8484. ESTAT_ADD(tx_comp_queue_full);
  8485. ESTAT_ADD(ring_set_send_prod_index);
  8486. ESTAT_ADD(ring_status_update);
  8487. ESTAT_ADD(nic_irqs);
  8488. ESTAT_ADD(nic_avoided_irqs);
  8489. ESTAT_ADD(nic_tx_threshold_hit);
  8490. ESTAT_ADD(mbuf_lwm_thresh_hit);
  8491. }
  8492. static void tg3_get_nstats(struct tg3 *tp, struct rtnl_link_stats64 *stats)
  8493. {
  8494. struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
  8495. struct tg3_hw_stats *hw_stats = tp->hw_stats;
  8496. stats->rx_packets = old_stats->rx_packets +
  8497. get_stat64(&hw_stats->rx_ucast_packets) +
  8498. get_stat64(&hw_stats->rx_mcast_packets) +
  8499. get_stat64(&hw_stats->rx_bcast_packets);
  8500. stats->tx_packets = old_stats->tx_packets +
  8501. get_stat64(&hw_stats->tx_ucast_packets) +
  8502. get_stat64(&hw_stats->tx_mcast_packets) +
  8503. get_stat64(&hw_stats->tx_bcast_packets);
  8504. stats->rx_bytes = old_stats->rx_bytes +
  8505. get_stat64(&hw_stats->rx_octets);
  8506. stats->tx_bytes = old_stats->tx_bytes +
  8507. get_stat64(&hw_stats->tx_octets);
  8508. stats->rx_errors = old_stats->rx_errors +
  8509. get_stat64(&hw_stats->rx_errors);
  8510. stats->tx_errors = old_stats->tx_errors +
  8511. get_stat64(&hw_stats->tx_errors) +
  8512. get_stat64(&hw_stats->tx_mac_errors) +
  8513. get_stat64(&hw_stats->tx_carrier_sense_errors) +
  8514. get_stat64(&hw_stats->tx_discards);
  8515. stats->multicast = old_stats->multicast +
  8516. get_stat64(&hw_stats->rx_mcast_packets);
  8517. stats->collisions = old_stats->collisions +
  8518. get_stat64(&hw_stats->tx_collisions);
  8519. stats->rx_length_errors = old_stats->rx_length_errors +
  8520. get_stat64(&hw_stats->rx_frame_too_long_errors) +
  8521. get_stat64(&hw_stats->rx_undersize_packets);
  8522. stats->rx_over_errors = old_stats->rx_over_errors +
  8523. get_stat64(&hw_stats->rxbds_empty);
  8524. stats->rx_frame_errors = old_stats->rx_frame_errors +
  8525. get_stat64(&hw_stats->rx_align_errors);
  8526. stats->tx_aborted_errors = old_stats->tx_aborted_errors +
  8527. get_stat64(&hw_stats->tx_discards);
  8528. stats->tx_carrier_errors = old_stats->tx_carrier_errors +
  8529. get_stat64(&hw_stats->tx_carrier_sense_errors);
  8530. stats->rx_crc_errors = old_stats->rx_crc_errors +
  8531. tg3_calc_crc_errors(tp);
  8532. stats->rx_missed_errors = old_stats->rx_missed_errors +
  8533. get_stat64(&hw_stats->rx_discards);
  8534. stats->rx_dropped = tp->rx_dropped;
  8535. stats->tx_dropped = tp->tx_dropped;
  8536. }
  8537. static int tg3_get_regs_len(struct net_device *dev)
  8538. {
  8539. return TG3_REG_BLK_SIZE;
  8540. }
  8541. static void tg3_get_regs(struct net_device *dev,
  8542. struct ethtool_regs *regs, void *_p)
  8543. {
  8544. struct tg3 *tp = netdev_priv(dev);
  8545. regs->version = 0;
  8546. memset(_p, 0, TG3_REG_BLK_SIZE);
  8547. if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
  8548. return;
  8549. tg3_full_lock(tp, 0);
  8550. tg3_dump_legacy_regs(tp, (u32 *)_p);
  8551. tg3_full_unlock(tp);
  8552. }
  8553. static int tg3_get_eeprom_len(struct net_device *dev)
  8554. {
  8555. struct tg3 *tp = netdev_priv(dev);
  8556. return tp->nvram_size;
  8557. }
  8558. static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
  8559. {
  8560. struct tg3 *tp = netdev_priv(dev);
  8561. int ret;
  8562. u8 *pd;
  8563. u32 i, offset, len, b_offset, b_count;
  8564. __be32 val;
  8565. if (tg3_flag(tp, NO_NVRAM))
  8566. return -EINVAL;
  8567. if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
  8568. return -EAGAIN;
  8569. offset = eeprom->offset;
  8570. len = eeprom->len;
  8571. eeprom->len = 0;
  8572. eeprom->magic = TG3_EEPROM_MAGIC;
  8573. if (offset & 3) {
  8574. /* adjustments to start on required 4 byte boundary */
  8575. b_offset = offset & 3;
  8576. b_count = 4 - b_offset;
  8577. if (b_count > len) {
  8578. /* i.e. offset=1 len=2 */
  8579. b_count = len;
  8580. }
  8581. ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
  8582. if (ret)
  8583. return ret;
  8584. memcpy(data, ((char *)&val) + b_offset, b_count);
  8585. len -= b_count;
  8586. offset += b_count;
  8587. eeprom->len += b_count;
  8588. }
  8589. /* read bytes up to the last 4 byte boundary */
  8590. pd = &data[eeprom->len];
  8591. for (i = 0; i < (len - (len & 3)); i += 4) {
  8592. ret = tg3_nvram_read_be32(tp, offset + i, &val);
  8593. if (ret) {
  8594. eeprom->len += i;
  8595. return ret;
  8596. }
  8597. memcpy(pd + i, &val, 4);
  8598. }
  8599. eeprom->len += i;
  8600. if (len & 3) {
  8601. /* read last bytes not ending on 4 byte boundary */
  8602. pd = &data[eeprom->len];
  8603. b_count = len & 3;
  8604. b_offset = offset + len - b_count;
  8605. ret = tg3_nvram_read_be32(tp, b_offset, &val);
  8606. if (ret)
  8607. return ret;
  8608. memcpy(pd, &val, b_count);
  8609. eeprom->len += b_count;
  8610. }
  8611. return 0;
  8612. }
  8613. static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
  8614. {
  8615. struct tg3 *tp = netdev_priv(dev);
  8616. int ret;
  8617. u32 offset, len, b_offset, odd_len;
  8618. u8 *buf;
  8619. __be32 start, end;
  8620. if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
  8621. return -EAGAIN;
  8622. if (tg3_flag(tp, NO_NVRAM) ||
  8623. eeprom->magic != TG3_EEPROM_MAGIC)
  8624. return -EINVAL;
  8625. offset = eeprom->offset;
  8626. len = eeprom->len;
  8627. if ((b_offset = (offset & 3))) {
  8628. /* adjustments to start on required 4 byte boundary */
  8629. ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
  8630. if (ret)
  8631. return ret;
  8632. len += b_offset;
  8633. offset &= ~3;
  8634. if (len < 4)
  8635. len = 4;
  8636. }
  8637. odd_len = 0;
  8638. if (len & 3) {
  8639. /* adjustments to end on required 4 byte boundary */
  8640. odd_len = 1;
  8641. len = (len + 3) & ~3;
  8642. ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
  8643. if (ret)
  8644. return ret;
  8645. }
  8646. buf = data;
  8647. if (b_offset || odd_len) {
  8648. buf = kmalloc(len, GFP_KERNEL);
  8649. if (!buf)
  8650. return -ENOMEM;
  8651. if (b_offset)
  8652. memcpy(buf, &start, 4);
  8653. if (odd_len)
  8654. memcpy(buf+len-4, &end, 4);
  8655. memcpy(buf + b_offset, data, eeprom->len);
  8656. }
  8657. ret = tg3_nvram_write_block(tp, offset, len, buf);
  8658. if (buf != data)
  8659. kfree(buf);
  8660. return ret;
  8661. }
  8662. static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  8663. {
  8664. struct tg3 *tp = netdev_priv(dev);
  8665. if (tg3_flag(tp, USE_PHYLIB)) {
  8666. struct phy_device *phydev;
  8667. if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
  8668. return -EAGAIN;
  8669. phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
  8670. return phy_ethtool_gset(phydev, cmd);
  8671. }
  8672. cmd->supported = (SUPPORTED_Autoneg);
  8673. if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
  8674. cmd->supported |= (SUPPORTED_1000baseT_Half |
  8675. SUPPORTED_1000baseT_Full);
  8676. if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
  8677. cmd->supported |= (SUPPORTED_100baseT_Half |
  8678. SUPPORTED_100baseT_Full |
  8679. SUPPORTED_10baseT_Half |
  8680. SUPPORTED_10baseT_Full |
  8681. SUPPORTED_TP);
  8682. cmd->port = PORT_TP;
  8683. } else {
  8684. cmd->supported |= SUPPORTED_FIBRE;
  8685. cmd->port = PORT_FIBRE;
  8686. }
  8687. cmd->advertising = tp->link_config.advertising;
  8688. if (tg3_flag(tp, PAUSE_AUTONEG)) {
  8689. if (tp->link_config.flowctrl & FLOW_CTRL_RX) {
  8690. if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
  8691. cmd->advertising |= ADVERTISED_Pause;
  8692. } else {
  8693. cmd->advertising |= ADVERTISED_Pause |
  8694. ADVERTISED_Asym_Pause;
  8695. }
  8696. } else if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
  8697. cmd->advertising |= ADVERTISED_Asym_Pause;
  8698. }
  8699. }
  8700. if (netif_running(dev) && netif_carrier_ok(dev)) {
  8701. ethtool_cmd_speed_set(cmd, tp->link_config.active_speed);
  8702. cmd->duplex = tp->link_config.active_duplex;
  8703. cmd->lp_advertising = tp->link_config.rmt_adv;
  8704. if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
  8705. if (tp->phy_flags & TG3_PHYFLG_MDIX_STATE)
  8706. cmd->eth_tp_mdix = ETH_TP_MDI_X;
  8707. else
  8708. cmd->eth_tp_mdix = ETH_TP_MDI;
  8709. }
  8710. } else {
  8711. ethtool_cmd_speed_set(cmd, SPEED_UNKNOWN);
  8712. cmd->duplex = DUPLEX_UNKNOWN;
  8713. cmd->eth_tp_mdix = ETH_TP_MDI_INVALID;
  8714. }
  8715. cmd->phy_address = tp->phy_addr;
  8716. cmd->transceiver = XCVR_INTERNAL;
  8717. cmd->autoneg = tp->link_config.autoneg;
  8718. cmd->maxtxpkt = 0;
  8719. cmd->maxrxpkt = 0;
  8720. return 0;
  8721. }
  8722. static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  8723. {
  8724. struct tg3 *tp = netdev_priv(dev);
  8725. u32 speed = ethtool_cmd_speed(cmd);
  8726. if (tg3_flag(tp, USE_PHYLIB)) {
  8727. struct phy_device *phydev;
  8728. if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
  8729. return -EAGAIN;
  8730. phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
  8731. return phy_ethtool_sset(phydev, cmd);
  8732. }
  8733. if (cmd->autoneg != AUTONEG_ENABLE &&
  8734. cmd->autoneg != AUTONEG_DISABLE)
  8735. return -EINVAL;
  8736. if (cmd->autoneg == AUTONEG_DISABLE &&
  8737. cmd->duplex != DUPLEX_FULL &&
  8738. cmd->duplex != DUPLEX_HALF)
  8739. return -EINVAL;
  8740. if (cmd->autoneg == AUTONEG_ENABLE) {
  8741. u32 mask = ADVERTISED_Autoneg |
  8742. ADVERTISED_Pause |
  8743. ADVERTISED_Asym_Pause;
  8744. if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
  8745. mask |= ADVERTISED_1000baseT_Half |
  8746. ADVERTISED_1000baseT_Full;
  8747. if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
  8748. mask |= ADVERTISED_100baseT_Half |
  8749. ADVERTISED_100baseT_Full |
  8750. ADVERTISED_10baseT_Half |
  8751. ADVERTISED_10baseT_Full |
  8752. ADVERTISED_TP;
  8753. else
  8754. mask |= ADVERTISED_FIBRE;
  8755. if (cmd->advertising & ~mask)
  8756. return -EINVAL;
  8757. mask &= (ADVERTISED_1000baseT_Half |
  8758. ADVERTISED_1000baseT_Full |
  8759. ADVERTISED_100baseT_Half |
  8760. ADVERTISED_100baseT_Full |
  8761. ADVERTISED_10baseT_Half |
  8762. ADVERTISED_10baseT_Full);
  8763. cmd->advertising &= mask;
  8764. } else {
  8765. if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
  8766. if (speed != SPEED_1000)
  8767. return -EINVAL;
  8768. if (cmd->duplex != DUPLEX_FULL)
  8769. return -EINVAL;
  8770. } else {
  8771. if (speed != SPEED_100 &&
  8772. speed != SPEED_10)
  8773. return -EINVAL;
  8774. }
  8775. }
  8776. tg3_full_lock(tp, 0);
  8777. tp->link_config.autoneg = cmd->autoneg;
  8778. if (cmd->autoneg == AUTONEG_ENABLE) {
  8779. tp->link_config.advertising = (cmd->advertising |
  8780. ADVERTISED_Autoneg);
  8781. tp->link_config.speed = SPEED_UNKNOWN;
  8782. tp->link_config.duplex = DUPLEX_UNKNOWN;
  8783. } else {
  8784. tp->link_config.advertising = 0;
  8785. tp->link_config.speed = speed;
  8786. tp->link_config.duplex = cmd->duplex;
  8787. }
  8788. if (netif_running(dev))
  8789. tg3_setup_phy(tp, 1);
  8790. tg3_full_unlock(tp);
  8791. return 0;
  8792. }
  8793. static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  8794. {
  8795. struct tg3 *tp = netdev_priv(dev);
  8796. strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
  8797. strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
  8798. strlcpy(info->fw_version, tp->fw_ver, sizeof(info->fw_version));
  8799. strlcpy(info->bus_info, pci_name(tp->pdev), sizeof(info->bus_info));
  8800. }
  8801. static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  8802. {
  8803. struct tg3 *tp = netdev_priv(dev);
  8804. if (tg3_flag(tp, WOL_CAP) && device_can_wakeup(&tp->pdev->dev))
  8805. wol->supported = WAKE_MAGIC;
  8806. else
  8807. wol->supported = 0;
  8808. wol->wolopts = 0;
  8809. if (tg3_flag(tp, WOL_ENABLE) && device_can_wakeup(&tp->pdev->dev))
  8810. wol->wolopts = WAKE_MAGIC;
  8811. memset(&wol->sopass, 0, sizeof(wol->sopass));
  8812. }
  8813. static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  8814. {
  8815. struct tg3 *tp = netdev_priv(dev);
  8816. struct device *dp = &tp->pdev->dev;
  8817. if (wol->wolopts & ~WAKE_MAGIC)
  8818. return -EINVAL;
  8819. if ((wol->wolopts & WAKE_MAGIC) &&
  8820. !(tg3_flag(tp, WOL_CAP) && device_can_wakeup(dp)))
  8821. return -EINVAL;
  8822. device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
  8823. spin_lock_bh(&tp->lock);
  8824. if (device_may_wakeup(dp))
  8825. tg3_flag_set(tp, WOL_ENABLE);
  8826. else
  8827. tg3_flag_clear(tp, WOL_ENABLE);
  8828. spin_unlock_bh(&tp->lock);
  8829. return 0;
  8830. }
  8831. static u32 tg3_get_msglevel(struct net_device *dev)
  8832. {
  8833. struct tg3 *tp = netdev_priv(dev);
  8834. return tp->msg_enable;
  8835. }
  8836. static void tg3_set_msglevel(struct net_device *dev, u32 value)
  8837. {
  8838. struct tg3 *tp = netdev_priv(dev);
  8839. tp->msg_enable = value;
  8840. }
  8841. static int tg3_nway_reset(struct net_device *dev)
  8842. {
  8843. struct tg3 *tp = netdev_priv(dev);
  8844. int r;
  8845. if (!netif_running(dev))
  8846. return -EAGAIN;
  8847. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
  8848. return -EINVAL;
  8849. if (tg3_flag(tp, USE_PHYLIB)) {
  8850. if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
  8851. return -EAGAIN;
  8852. r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
  8853. } else {
  8854. u32 bmcr;
  8855. spin_lock_bh(&tp->lock);
  8856. r = -EINVAL;
  8857. tg3_readphy(tp, MII_BMCR, &bmcr);
  8858. if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
  8859. ((bmcr & BMCR_ANENABLE) ||
  8860. (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
  8861. tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
  8862. BMCR_ANENABLE);
  8863. r = 0;
  8864. }
  8865. spin_unlock_bh(&tp->lock);
  8866. }
  8867. return r;
  8868. }
  8869. static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
  8870. {
  8871. struct tg3 *tp = netdev_priv(dev);
  8872. ering->rx_max_pending = tp->rx_std_ring_mask;
  8873. if (tg3_flag(tp, JUMBO_RING_ENABLE))
  8874. ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
  8875. else
  8876. ering->rx_jumbo_max_pending = 0;
  8877. ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
  8878. ering->rx_pending = tp->rx_pending;
  8879. if (tg3_flag(tp, JUMBO_RING_ENABLE))
  8880. ering->rx_jumbo_pending = tp->rx_jumbo_pending;
  8881. else
  8882. ering->rx_jumbo_pending = 0;
  8883. ering->tx_pending = tp->napi[0].tx_pending;
  8884. }
  8885. static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
  8886. {
  8887. struct tg3 *tp = netdev_priv(dev);
  8888. int i, irq_sync = 0, err = 0;
  8889. if ((ering->rx_pending > tp->rx_std_ring_mask) ||
  8890. (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
  8891. (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
  8892. (ering->tx_pending <= MAX_SKB_FRAGS) ||
  8893. (tg3_flag(tp, TSO_BUG) &&
  8894. (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
  8895. return -EINVAL;
  8896. if (netif_running(dev)) {
  8897. tg3_phy_stop(tp);
  8898. tg3_netif_stop(tp);
  8899. irq_sync = 1;
  8900. }
  8901. tg3_full_lock(tp, irq_sync);
  8902. tp->rx_pending = ering->rx_pending;
  8903. if (tg3_flag(tp, MAX_RXPEND_64) &&
  8904. tp->rx_pending > 63)
  8905. tp->rx_pending = 63;
  8906. tp->rx_jumbo_pending = ering->rx_jumbo_pending;
  8907. for (i = 0; i < tp->irq_max; i++)
  8908. tp->napi[i].tx_pending = ering->tx_pending;
  8909. if (netif_running(dev)) {
  8910. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  8911. err = tg3_restart_hw(tp, 1);
  8912. if (!err)
  8913. tg3_netif_start(tp);
  8914. }
  8915. tg3_full_unlock(tp);
  8916. if (irq_sync && !err)
  8917. tg3_phy_start(tp);
  8918. return err;
  8919. }
  8920. static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
  8921. {
  8922. struct tg3 *tp = netdev_priv(dev);
  8923. epause->autoneg = !!tg3_flag(tp, PAUSE_AUTONEG);
  8924. if (tp->link_config.flowctrl & FLOW_CTRL_RX)
  8925. epause->rx_pause = 1;
  8926. else
  8927. epause->rx_pause = 0;
  8928. if (tp->link_config.flowctrl & FLOW_CTRL_TX)
  8929. epause->tx_pause = 1;
  8930. else
  8931. epause->tx_pause = 0;
  8932. }
  8933. static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
  8934. {
  8935. struct tg3 *tp = netdev_priv(dev);
  8936. int err = 0;
  8937. if (tg3_flag(tp, USE_PHYLIB)) {
  8938. u32 newadv;
  8939. struct phy_device *phydev;
  8940. phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
  8941. if (!(phydev->supported & SUPPORTED_Pause) ||
  8942. (!(phydev->supported & SUPPORTED_Asym_Pause) &&
  8943. (epause->rx_pause != epause->tx_pause)))
  8944. return -EINVAL;
  8945. tp->link_config.flowctrl = 0;
  8946. if (epause->rx_pause) {
  8947. tp->link_config.flowctrl |= FLOW_CTRL_RX;
  8948. if (epause->tx_pause) {
  8949. tp->link_config.flowctrl |= FLOW_CTRL_TX;
  8950. newadv = ADVERTISED_Pause;
  8951. } else
  8952. newadv = ADVERTISED_Pause |
  8953. ADVERTISED_Asym_Pause;
  8954. } else if (epause->tx_pause) {
  8955. tp->link_config.flowctrl |= FLOW_CTRL_TX;
  8956. newadv = ADVERTISED_Asym_Pause;
  8957. } else
  8958. newadv = 0;
  8959. if (epause->autoneg)
  8960. tg3_flag_set(tp, PAUSE_AUTONEG);
  8961. else
  8962. tg3_flag_clear(tp, PAUSE_AUTONEG);
  8963. if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
  8964. u32 oldadv = phydev->advertising &
  8965. (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
  8966. if (oldadv != newadv) {
  8967. phydev->advertising &=
  8968. ~(ADVERTISED_Pause |
  8969. ADVERTISED_Asym_Pause);
  8970. phydev->advertising |= newadv;
  8971. if (phydev->autoneg) {
  8972. /*
  8973. * Always renegotiate the link to
  8974. * inform our link partner of our
  8975. * flow control settings, even if the
  8976. * flow control is forced. Let
  8977. * tg3_adjust_link() do the final
  8978. * flow control setup.
  8979. */
  8980. return phy_start_aneg(phydev);
  8981. }
  8982. }
  8983. if (!epause->autoneg)
  8984. tg3_setup_flow_control(tp, 0, 0);
  8985. } else {
  8986. tp->link_config.advertising &=
  8987. ~(ADVERTISED_Pause |
  8988. ADVERTISED_Asym_Pause);
  8989. tp->link_config.advertising |= newadv;
  8990. }
  8991. } else {
  8992. int irq_sync = 0;
  8993. if (netif_running(dev)) {
  8994. tg3_netif_stop(tp);
  8995. irq_sync = 1;
  8996. }
  8997. tg3_full_lock(tp, irq_sync);
  8998. if (epause->autoneg)
  8999. tg3_flag_set(tp, PAUSE_AUTONEG);
  9000. else
  9001. tg3_flag_clear(tp, PAUSE_AUTONEG);
  9002. if (epause->rx_pause)
  9003. tp->link_config.flowctrl |= FLOW_CTRL_RX;
  9004. else
  9005. tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
  9006. if (epause->tx_pause)
  9007. tp->link_config.flowctrl |= FLOW_CTRL_TX;
  9008. else
  9009. tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
  9010. if (netif_running(dev)) {
  9011. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  9012. err = tg3_restart_hw(tp, 1);
  9013. if (!err)
  9014. tg3_netif_start(tp);
  9015. }
  9016. tg3_full_unlock(tp);
  9017. }
  9018. return err;
  9019. }
  9020. static int tg3_get_sset_count(struct net_device *dev, int sset)
  9021. {
  9022. switch (sset) {
  9023. case ETH_SS_TEST:
  9024. return TG3_NUM_TEST;
  9025. case ETH_SS_STATS:
  9026. return TG3_NUM_STATS;
  9027. default:
  9028. return -EOPNOTSUPP;
  9029. }
  9030. }
  9031. static int tg3_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
  9032. u32 *rules __always_unused)
  9033. {
  9034. struct tg3 *tp = netdev_priv(dev);
  9035. if (!tg3_flag(tp, SUPPORT_MSIX))
  9036. return -EOPNOTSUPP;
  9037. switch (info->cmd) {
  9038. case ETHTOOL_GRXRINGS:
  9039. if (netif_running(tp->dev))
  9040. info->data = tp->irq_cnt;
  9041. else {
  9042. info->data = num_online_cpus();
  9043. if (info->data > TG3_IRQ_MAX_VECS_RSS)
  9044. info->data = TG3_IRQ_MAX_VECS_RSS;
  9045. }
  9046. /* The first interrupt vector only
  9047. * handles link interrupts.
  9048. */
  9049. info->data -= 1;
  9050. return 0;
  9051. default:
  9052. return -EOPNOTSUPP;
  9053. }
  9054. }
  9055. static u32 tg3_get_rxfh_indir_size(struct net_device *dev)
  9056. {
  9057. u32 size = 0;
  9058. struct tg3 *tp = netdev_priv(dev);
  9059. if (tg3_flag(tp, SUPPORT_MSIX))
  9060. size = TG3_RSS_INDIR_TBL_SIZE;
  9061. return size;
  9062. }
  9063. static int tg3_get_rxfh_indir(struct net_device *dev, u32 *indir)
  9064. {
  9065. struct tg3 *tp = netdev_priv(dev);
  9066. int i;
  9067. for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
  9068. indir[i] = tp->rss_ind_tbl[i];
  9069. return 0;
  9070. }
  9071. static int tg3_set_rxfh_indir(struct net_device *dev, const u32 *indir)
  9072. {
  9073. struct tg3 *tp = netdev_priv(dev);
  9074. size_t i;
  9075. for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
  9076. tp->rss_ind_tbl[i] = indir[i];
  9077. if (!netif_running(dev) || !tg3_flag(tp, ENABLE_RSS))
  9078. return 0;
  9079. /* It is legal to write the indirection
  9080. * table while the device is running.
  9081. */
  9082. tg3_full_lock(tp, 0);
  9083. tg3_rss_write_indir_tbl(tp);
  9084. tg3_full_unlock(tp);
  9085. return 0;
  9086. }
  9087. static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
  9088. {
  9089. switch (stringset) {
  9090. case ETH_SS_STATS:
  9091. memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
  9092. break;
  9093. case ETH_SS_TEST:
  9094. memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
  9095. break;
  9096. default:
  9097. WARN_ON(1); /* we need a WARN() */
  9098. break;
  9099. }
  9100. }
  9101. static int tg3_set_phys_id(struct net_device *dev,
  9102. enum ethtool_phys_id_state state)
  9103. {
  9104. struct tg3 *tp = netdev_priv(dev);
  9105. if (!netif_running(tp->dev))
  9106. return -EAGAIN;
  9107. switch (state) {
  9108. case ETHTOOL_ID_ACTIVE:
  9109. return 1; /* cycle on/off once per second */
  9110. case ETHTOOL_ID_ON:
  9111. tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
  9112. LED_CTRL_1000MBPS_ON |
  9113. LED_CTRL_100MBPS_ON |
  9114. LED_CTRL_10MBPS_ON |
  9115. LED_CTRL_TRAFFIC_OVERRIDE |
  9116. LED_CTRL_TRAFFIC_BLINK |
  9117. LED_CTRL_TRAFFIC_LED);
  9118. break;
  9119. case ETHTOOL_ID_OFF:
  9120. tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
  9121. LED_CTRL_TRAFFIC_OVERRIDE);
  9122. break;
  9123. case ETHTOOL_ID_INACTIVE:
  9124. tw32(MAC_LED_CTRL, tp->led_ctrl);
  9125. break;
  9126. }
  9127. return 0;
  9128. }
  9129. static void tg3_get_ethtool_stats(struct net_device *dev,
  9130. struct ethtool_stats *estats, u64 *tmp_stats)
  9131. {
  9132. struct tg3 *tp = netdev_priv(dev);
  9133. if (tp->hw_stats)
  9134. tg3_get_estats(tp, (struct tg3_ethtool_stats *)tmp_stats);
  9135. else
  9136. memset(tmp_stats, 0, sizeof(struct tg3_ethtool_stats));
  9137. }
  9138. static __be32 *tg3_vpd_readblock(struct tg3 *tp, u32 *vpdlen)
  9139. {
  9140. int i;
  9141. __be32 *buf;
  9142. u32 offset = 0, len = 0;
  9143. u32 magic, val;
  9144. if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &magic))
  9145. return NULL;
  9146. if (magic == TG3_EEPROM_MAGIC) {
  9147. for (offset = TG3_NVM_DIR_START;
  9148. offset < TG3_NVM_DIR_END;
  9149. offset += TG3_NVM_DIRENT_SIZE) {
  9150. if (tg3_nvram_read(tp, offset, &val))
  9151. return NULL;
  9152. if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
  9153. TG3_NVM_DIRTYPE_EXTVPD)
  9154. break;
  9155. }
  9156. if (offset != TG3_NVM_DIR_END) {
  9157. len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
  9158. if (tg3_nvram_read(tp, offset + 4, &offset))
  9159. return NULL;
  9160. offset = tg3_nvram_logical_addr(tp, offset);
  9161. }
  9162. }
  9163. if (!offset || !len) {
  9164. offset = TG3_NVM_VPD_OFF;
  9165. len = TG3_NVM_VPD_LEN;
  9166. }
  9167. buf = kmalloc(len, GFP_KERNEL);
  9168. if (buf == NULL)
  9169. return NULL;
  9170. if (magic == TG3_EEPROM_MAGIC) {
  9171. for (i = 0; i < len; i += 4) {
  9172. /* The data is in little-endian format in NVRAM.
  9173. * Use the big-endian read routines to preserve
  9174. * the byte order as it exists in NVRAM.
  9175. */
  9176. if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
  9177. goto error;
  9178. }
  9179. } else {
  9180. u8 *ptr;
  9181. ssize_t cnt;
  9182. unsigned int pos = 0;
  9183. ptr = (u8 *)&buf[0];
  9184. for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
  9185. cnt = pci_read_vpd(tp->pdev, pos,
  9186. len - pos, ptr);
  9187. if (cnt == -ETIMEDOUT || cnt == -EINTR)
  9188. cnt = 0;
  9189. else if (cnt < 0)
  9190. goto error;
  9191. }
  9192. if (pos != len)
  9193. goto error;
  9194. }
  9195. *vpdlen = len;
  9196. return buf;
  9197. error:
  9198. kfree(buf);
  9199. return NULL;
  9200. }
  9201. #define NVRAM_TEST_SIZE 0x100
  9202. #define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
  9203. #define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
  9204. #define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
  9205. #define NVRAM_SELFBOOT_FORMAT1_4_SIZE 0x20
  9206. #define NVRAM_SELFBOOT_FORMAT1_5_SIZE 0x24
  9207. #define NVRAM_SELFBOOT_FORMAT1_6_SIZE 0x50
  9208. #define NVRAM_SELFBOOT_HW_SIZE 0x20
  9209. #define NVRAM_SELFBOOT_DATA_SIZE 0x1c
  9210. static int tg3_test_nvram(struct tg3 *tp)
  9211. {
  9212. u32 csum, magic, len;
  9213. __be32 *buf;
  9214. int i, j, k, err = 0, size;
  9215. if (tg3_flag(tp, NO_NVRAM))
  9216. return 0;
  9217. if (tg3_nvram_read(tp, 0, &magic) != 0)
  9218. return -EIO;
  9219. if (magic == TG3_EEPROM_MAGIC)
  9220. size = NVRAM_TEST_SIZE;
  9221. else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
  9222. if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
  9223. TG3_EEPROM_SB_FORMAT_1) {
  9224. switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
  9225. case TG3_EEPROM_SB_REVISION_0:
  9226. size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
  9227. break;
  9228. case TG3_EEPROM_SB_REVISION_2:
  9229. size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
  9230. break;
  9231. case TG3_EEPROM_SB_REVISION_3:
  9232. size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
  9233. break;
  9234. case TG3_EEPROM_SB_REVISION_4:
  9235. size = NVRAM_SELFBOOT_FORMAT1_4_SIZE;
  9236. break;
  9237. case TG3_EEPROM_SB_REVISION_5:
  9238. size = NVRAM_SELFBOOT_FORMAT1_5_SIZE;
  9239. break;
  9240. case TG3_EEPROM_SB_REVISION_6:
  9241. size = NVRAM_SELFBOOT_FORMAT1_6_SIZE;
  9242. break;
  9243. default:
  9244. return -EIO;
  9245. }
  9246. } else
  9247. return 0;
  9248. } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
  9249. size = NVRAM_SELFBOOT_HW_SIZE;
  9250. else
  9251. return -EIO;
  9252. buf = kmalloc(size, GFP_KERNEL);
  9253. if (buf == NULL)
  9254. return -ENOMEM;
  9255. err = -EIO;
  9256. for (i = 0, j = 0; i < size; i += 4, j++) {
  9257. err = tg3_nvram_read_be32(tp, i, &buf[j]);
  9258. if (err)
  9259. break;
  9260. }
  9261. if (i < size)
  9262. goto out;
  9263. /* Selfboot format */
  9264. magic = be32_to_cpu(buf[0]);
  9265. if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
  9266. TG3_EEPROM_MAGIC_FW) {
  9267. u8 *buf8 = (u8 *) buf, csum8 = 0;
  9268. if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
  9269. TG3_EEPROM_SB_REVISION_2) {
  9270. /* For rev 2, the csum doesn't include the MBA. */
  9271. for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
  9272. csum8 += buf8[i];
  9273. for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
  9274. csum8 += buf8[i];
  9275. } else {
  9276. for (i = 0; i < size; i++)
  9277. csum8 += buf8[i];
  9278. }
  9279. if (csum8 == 0) {
  9280. err = 0;
  9281. goto out;
  9282. }
  9283. err = -EIO;
  9284. goto out;
  9285. }
  9286. if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
  9287. TG3_EEPROM_MAGIC_HW) {
  9288. u8 data[NVRAM_SELFBOOT_DATA_SIZE];
  9289. u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
  9290. u8 *buf8 = (u8 *) buf;
  9291. /* Separate the parity bits and the data bytes. */
  9292. for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
  9293. if ((i == 0) || (i == 8)) {
  9294. int l;
  9295. u8 msk;
  9296. for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
  9297. parity[k++] = buf8[i] & msk;
  9298. i++;
  9299. } else if (i == 16) {
  9300. int l;
  9301. u8 msk;
  9302. for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
  9303. parity[k++] = buf8[i] & msk;
  9304. i++;
  9305. for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
  9306. parity[k++] = buf8[i] & msk;
  9307. i++;
  9308. }
  9309. data[j++] = buf8[i];
  9310. }
  9311. err = -EIO;
  9312. for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
  9313. u8 hw8 = hweight8(data[i]);
  9314. if ((hw8 & 0x1) && parity[i])
  9315. goto out;
  9316. else if (!(hw8 & 0x1) && !parity[i])
  9317. goto out;
  9318. }
  9319. err = 0;
  9320. goto out;
  9321. }
  9322. err = -EIO;
  9323. /* Bootstrap checksum at offset 0x10 */
  9324. csum = calc_crc((unsigned char *) buf, 0x10);
  9325. if (csum != le32_to_cpu(buf[0x10/4]))
  9326. goto out;
  9327. /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
  9328. csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
  9329. if (csum != le32_to_cpu(buf[0xfc/4]))
  9330. goto out;
  9331. kfree(buf);
  9332. buf = tg3_vpd_readblock(tp, &len);
  9333. if (!buf)
  9334. return -ENOMEM;
  9335. i = pci_vpd_find_tag((u8 *)buf, 0, len, PCI_VPD_LRDT_RO_DATA);
  9336. if (i > 0) {
  9337. j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
  9338. if (j < 0)
  9339. goto out;
  9340. if (i + PCI_VPD_LRDT_TAG_SIZE + j > len)
  9341. goto out;
  9342. i += PCI_VPD_LRDT_TAG_SIZE;
  9343. j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
  9344. PCI_VPD_RO_KEYWORD_CHKSUM);
  9345. if (j > 0) {
  9346. u8 csum8 = 0;
  9347. j += PCI_VPD_INFO_FLD_HDR_SIZE;
  9348. for (i = 0; i <= j; i++)
  9349. csum8 += ((u8 *)buf)[i];
  9350. if (csum8)
  9351. goto out;
  9352. }
  9353. }
  9354. err = 0;
  9355. out:
  9356. kfree(buf);
  9357. return err;
  9358. }
  9359. #define TG3_SERDES_TIMEOUT_SEC 2
  9360. #define TG3_COPPER_TIMEOUT_SEC 6
  9361. static int tg3_test_link(struct tg3 *tp)
  9362. {
  9363. int i, max;
  9364. if (!netif_running(tp->dev))
  9365. return -ENODEV;
  9366. if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
  9367. max = TG3_SERDES_TIMEOUT_SEC;
  9368. else
  9369. max = TG3_COPPER_TIMEOUT_SEC;
  9370. for (i = 0; i < max; i++) {
  9371. if (netif_carrier_ok(tp->dev))
  9372. return 0;
  9373. if (msleep_interruptible(1000))
  9374. break;
  9375. }
  9376. return -EIO;
  9377. }
  9378. /* Only test the commonly used registers */
  9379. static int tg3_test_registers(struct tg3 *tp)
  9380. {
  9381. int i, is_5705, is_5750;
  9382. u32 offset, read_mask, write_mask, val, save_val, read_val;
  9383. static struct {
  9384. u16 offset;
  9385. u16 flags;
  9386. #define TG3_FL_5705 0x1
  9387. #define TG3_FL_NOT_5705 0x2
  9388. #define TG3_FL_NOT_5788 0x4
  9389. #define TG3_FL_NOT_5750 0x8
  9390. u32 read_mask;
  9391. u32 write_mask;
  9392. } reg_tbl[] = {
  9393. /* MAC Control Registers */
  9394. { MAC_MODE, TG3_FL_NOT_5705,
  9395. 0x00000000, 0x00ef6f8c },
  9396. { MAC_MODE, TG3_FL_5705,
  9397. 0x00000000, 0x01ef6b8c },
  9398. { MAC_STATUS, TG3_FL_NOT_5705,
  9399. 0x03800107, 0x00000000 },
  9400. { MAC_STATUS, TG3_FL_5705,
  9401. 0x03800100, 0x00000000 },
  9402. { MAC_ADDR_0_HIGH, 0x0000,
  9403. 0x00000000, 0x0000ffff },
  9404. { MAC_ADDR_0_LOW, 0x0000,
  9405. 0x00000000, 0xffffffff },
  9406. { MAC_RX_MTU_SIZE, 0x0000,
  9407. 0x00000000, 0x0000ffff },
  9408. { MAC_TX_MODE, 0x0000,
  9409. 0x00000000, 0x00000070 },
  9410. { MAC_TX_LENGTHS, 0x0000,
  9411. 0x00000000, 0x00003fff },
  9412. { MAC_RX_MODE, TG3_FL_NOT_5705,
  9413. 0x00000000, 0x000007fc },
  9414. { MAC_RX_MODE, TG3_FL_5705,
  9415. 0x00000000, 0x000007dc },
  9416. { MAC_HASH_REG_0, 0x0000,
  9417. 0x00000000, 0xffffffff },
  9418. { MAC_HASH_REG_1, 0x0000,
  9419. 0x00000000, 0xffffffff },
  9420. { MAC_HASH_REG_2, 0x0000,
  9421. 0x00000000, 0xffffffff },
  9422. { MAC_HASH_REG_3, 0x0000,
  9423. 0x00000000, 0xffffffff },
  9424. /* Receive Data and Receive BD Initiator Control Registers. */
  9425. { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
  9426. 0x00000000, 0xffffffff },
  9427. { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
  9428. 0x00000000, 0xffffffff },
  9429. { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
  9430. 0x00000000, 0x00000003 },
  9431. { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
  9432. 0x00000000, 0xffffffff },
  9433. { RCVDBDI_STD_BD+0, 0x0000,
  9434. 0x00000000, 0xffffffff },
  9435. { RCVDBDI_STD_BD+4, 0x0000,
  9436. 0x00000000, 0xffffffff },
  9437. { RCVDBDI_STD_BD+8, 0x0000,
  9438. 0x00000000, 0xffff0002 },
  9439. { RCVDBDI_STD_BD+0xc, 0x0000,
  9440. 0x00000000, 0xffffffff },
  9441. /* Receive BD Initiator Control Registers. */
  9442. { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
  9443. 0x00000000, 0xffffffff },
  9444. { RCVBDI_STD_THRESH, TG3_FL_5705,
  9445. 0x00000000, 0x000003ff },
  9446. { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
  9447. 0x00000000, 0xffffffff },
  9448. /* Host Coalescing Control Registers. */
  9449. { HOSTCC_MODE, TG3_FL_NOT_5705,
  9450. 0x00000000, 0x00000004 },
  9451. { HOSTCC_MODE, TG3_FL_5705,
  9452. 0x00000000, 0x000000f6 },
  9453. { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
  9454. 0x00000000, 0xffffffff },
  9455. { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
  9456. 0x00000000, 0x000003ff },
  9457. { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
  9458. 0x00000000, 0xffffffff },
  9459. { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
  9460. 0x00000000, 0x000003ff },
  9461. { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
  9462. 0x00000000, 0xffffffff },
  9463. { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
  9464. 0x00000000, 0x000000ff },
  9465. { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
  9466. 0x00000000, 0xffffffff },
  9467. { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
  9468. 0x00000000, 0x000000ff },
  9469. { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
  9470. 0x00000000, 0xffffffff },
  9471. { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
  9472. 0x00000000, 0xffffffff },
  9473. { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
  9474. 0x00000000, 0xffffffff },
  9475. { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
  9476. 0x00000000, 0x000000ff },
  9477. { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
  9478. 0x00000000, 0xffffffff },
  9479. { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
  9480. 0x00000000, 0x000000ff },
  9481. { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
  9482. 0x00000000, 0xffffffff },
  9483. { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
  9484. 0x00000000, 0xffffffff },
  9485. { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
  9486. 0x00000000, 0xffffffff },
  9487. { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
  9488. 0x00000000, 0xffffffff },
  9489. { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
  9490. 0x00000000, 0xffffffff },
  9491. { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
  9492. 0xffffffff, 0x00000000 },
  9493. { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
  9494. 0xffffffff, 0x00000000 },
  9495. /* Buffer Manager Control Registers. */
  9496. { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
  9497. 0x00000000, 0x007fff80 },
  9498. { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
  9499. 0x00000000, 0x007fffff },
  9500. { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
  9501. 0x00000000, 0x0000003f },
  9502. { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
  9503. 0x00000000, 0x000001ff },
  9504. { BUFMGR_MB_HIGH_WATER, 0x0000,
  9505. 0x00000000, 0x000001ff },
  9506. { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
  9507. 0xffffffff, 0x00000000 },
  9508. { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
  9509. 0xffffffff, 0x00000000 },
  9510. /* Mailbox Registers */
  9511. { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
  9512. 0x00000000, 0x000001ff },
  9513. { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
  9514. 0x00000000, 0x000001ff },
  9515. { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
  9516. 0x00000000, 0x000007ff },
  9517. { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
  9518. 0x00000000, 0x000001ff },
  9519. { 0xffff, 0x0000, 0x00000000, 0x00000000 },
  9520. };
  9521. is_5705 = is_5750 = 0;
  9522. if (tg3_flag(tp, 5705_PLUS)) {
  9523. is_5705 = 1;
  9524. if (tg3_flag(tp, 5750_PLUS))
  9525. is_5750 = 1;
  9526. }
  9527. for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
  9528. if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
  9529. continue;
  9530. if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
  9531. continue;
  9532. if (tg3_flag(tp, IS_5788) &&
  9533. (reg_tbl[i].flags & TG3_FL_NOT_5788))
  9534. continue;
  9535. if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
  9536. continue;
  9537. offset = (u32) reg_tbl[i].offset;
  9538. read_mask = reg_tbl[i].read_mask;
  9539. write_mask = reg_tbl[i].write_mask;
  9540. /* Save the original register content */
  9541. save_val = tr32(offset);
  9542. /* Determine the read-only value. */
  9543. read_val = save_val & read_mask;
  9544. /* Write zero to the register, then make sure the read-only bits
  9545. * are not changed and the read/write bits are all zeros.
  9546. */
  9547. tw32(offset, 0);
  9548. val = tr32(offset);
  9549. /* Test the read-only and read/write bits. */
  9550. if (((val & read_mask) != read_val) || (val & write_mask))
  9551. goto out;
  9552. /* Write ones to all the bits defined by RdMask and WrMask, then
  9553. * make sure the read-only bits are not changed and the
  9554. * read/write bits are all ones.
  9555. */
  9556. tw32(offset, read_mask | write_mask);
  9557. val = tr32(offset);
  9558. /* Test the read-only bits. */
  9559. if ((val & read_mask) != read_val)
  9560. goto out;
  9561. /* Test the read/write bits. */
  9562. if ((val & write_mask) != write_mask)
  9563. goto out;
  9564. tw32(offset, save_val);
  9565. }
  9566. return 0;
  9567. out:
  9568. if (netif_msg_hw(tp))
  9569. netdev_err(tp->dev,
  9570. "Register test failed at offset %x\n", offset);
  9571. tw32(offset, save_val);
  9572. return -EIO;
  9573. }
  9574. static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
  9575. {
  9576. static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
  9577. int i;
  9578. u32 j;
  9579. for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
  9580. for (j = 0; j < len; j += 4) {
  9581. u32 val;
  9582. tg3_write_mem(tp, offset + j, test_pattern[i]);
  9583. tg3_read_mem(tp, offset + j, &val);
  9584. if (val != test_pattern[i])
  9585. return -EIO;
  9586. }
  9587. }
  9588. return 0;
  9589. }
  9590. static int tg3_test_memory(struct tg3 *tp)
  9591. {
  9592. static struct mem_entry {
  9593. u32 offset;
  9594. u32 len;
  9595. } mem_tbl_570x[] = {
  9596. { 0x00000000, 0x00b50},
  9597. { 0x00002000, 0x1c000},
  9598. { 0xffffffff, 0x00000}
  9599. }, mem_tbl_5705[] = {
  9600. { 0x00000100, 0x0000c},
  9601. { 0x00000200, 0x00008},
  9602. { 0x00004000, 0x00800},
  9603. { 0x00006000, 0x01000},
  9604. { 0x00008000, 0x02000},
  9605. { 0x00010000, 0x0e000},
  9606. { 0xffffffff, 0x00000}
  9607. }, mem_tbl_5755[] = {
  9608. { 0x00000200, 0x00008},
  9609. { 0x00004000, 0x00800},
  9610. { 0x00006000, 0x00800},
  9611. { 0x00008000, 0x02000},
  9612. { 0x00010000, 0x0c000},
  9613. { 0xffffffff, 0x00000}
  9614. }, mem_tbl_5906[] = {
  9615. { 0x00000200, 0x00008},
  9616. { 0x00004000, 0x00400},
  9617. { 0x00006000, 0x00400},
  9618. { 0x00008000, 0x01000},
  9619. { 0x00010000, 0x01000},
  9620. { 0xffffffff, 0x00000}
  9621. }, mem_tbl_5717[] = {
  9622. { 0x00000200, 0x00008},
  9623. { 0x00010000, 0x0a000},
  9624. { 0x00020000, 0x13c00},
  9625. { 0xffffffff, 0x00000}
  9626. }, mem_tbl_57765[] = {
  9627. { 0x00000200, 0x00008},
  9628. { 0x00004000, 0x00800},
  9629. { 0x00006000, 0x09800},
  9630. { 0x00010000, 0x0a000},
  9631. { 0xffffffff, 0x00000}
  9632. };
  9633. struct mem_entry *mem_tbl;
  9634. int err = 0;
  9635. int i;
  9636. if (tg3_flag(tp, 5717_PLUS))
  9637. mem_tbl = mem_tbl_5717;
  9638. else if (tg3_flag(tp, 57765_CLASS))
  9639. mem_tbl = mem_tbl_57765;
  9640. else if (tg3_flag(tp, 5755_PLUS))
  9641. mem_tbl = mem_tbl_5755;
  9642. else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
  9643. mem_tbl = mem_tbl_5906;
  9644. else if (tg3_flag(tp, 5705_PLUS))
  9645. mem_tbl = mem_tbl_5705;
  9646. else
  9647. mem_tbl = mem_tbl_570x;
  9648. for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
  9649. err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
  9650. if (err)
  9651. break;
  9652. }
  9653. return err;
  9654. }
  9655. #define TG3_TSO_MSS 500
  9656. #define TG3_TSO_IP_HDR_LEN 20
  9657. #define TG3_TSO_TCP_HDR_LEN 20
  9658. #define TG3_TSO_TCP_OPT_LEN 12
  9659. static const u8 tg3_tso_header[] = {
  9660. 0x08, 0x00,
  9661. 0x45, 0x00, 0x00, 0x00,
  9662. 0x00, 0x00, 0x40, 0x00,
  9663. 0x40, 0x06, 0x00, 0x00,
  9664. 0x0a, 0x00, 0x00, 0x01,
  9665. 0x0a, 0x00, 0x00, 0x02,
  9666. 0x0d, 0x00, 0xe0, 0x00,
  9667. 0x00, 0x00, 0x01, 0x00,
  9668. 0x00, 0x00, 0x02, 0x00,
  9669. 0x80, 0x10, 0x10, 0x00,
  9670. 0x14, 0x09, 0x00, 0x00,
  9671. 0x01, 0x01, 0x08, 0x0a,
  9672. 0x11, 0x11, 0x11, 0x11,
  9673. 0x11, 0x11, 0x11, 0x11,
  9674. };
  9675. static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, bool tso_loopback)
  9676. {
  9677. u32 rx_start_idx, rx_idx, tx_idx, opaque_key;
  9678. u32 base_flags = 0, mss = 0, desc_idx, coal_now, data_off, val;
  9679. u32 budget;
  9680. struct sk_buff *skb;
  9681. u8 *tx_data, *rx_data;
  9682. dma_addr_t map;
  9683. int num_pkts, tx_len, rx_len, i, err;
  9684. struct tg3_rx_buffer_desc *desc;
  9685. struct tg3_napi *tnapi, *rnapi;
  9686. struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
  9687. tnapi = &tp->napi[0];
  9688. rnapi = &tp->napi[0];
  9689. if (tp->irq_cnt > 1) {
  9690. if (tg3_flag(tp, ENABLE_RSS))
  9691. rnapi = &tp->napi[1];
  9692. if (tg3_flag(tp, ENABLE_TSS))
  9693. tnapi = &tp->napi[1];
  9694. }
  9695. coal_now = tnapi->coal_now | rnapi->coal_now;
  9696. err = -EIO;
  9697. tx_len = pktsz;
  9698. skb = netdev_alloc_skb(tp->dev, tx_len);
  9699. if (!skb)
  9700. return -ENOMEM;
  9701. tx_data = skb_put(skb, tx_len);
  9702. memcpy(tx_data, tp->dev->dev_addr, 6);
  9703. memset(tx_data + 6, 0x0, 8);
  9704. tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
  9705. if (tso_loopback) {
  9706. struct iphdr *iph = (struct iphdr *)&tx_data[ETH_HLEN];
  9707. u32 hdr_len = TG3_TSO_IP_HDR_LEN + TG3_TSO_TCP_HDR_LEN +
  9708. TG3_TSO_TCP_OPT_LEN;
  9709. memcpy(tx_data + ETH_ALEN * 2, tg3_tso_header,
  9710. sizeof(tg3_tso_header));
  9711. mss = TG3_TSO_MSS;
  9712. val = tx_len - ETH_ALEN * 2 - sizeof(tg3_tso_header);
  9713. num_pkts = DIV_ROUND_UP(val, TG3_TSO_MSS);
  9714. /* Set the total length field in the IP header */
  9715. iph->tot_len = htons((u16)(mss + hdr_len));
  9716. base_flags = (TXD_FLAG_CPU_PRE_DMA |
  9717. TXD_FLAG_CPU_POST_DMA);
  9718. if (tg3_flag(tp, HW_TSO_1) ||
  9719. tg3_flag(tp, HW_TSO_2) ||
  9720. tg3_flag(tp, HW_TSO_3)) {
  9721. struct tcphdr *th;
  9722. val = ETH_HLEN + TG3_TSO_IP_HDR_LEN;
  9723. th = (struct tcphdr *)&tx_data[val];
  9724. th->check = 0;
  9725. } else
  9726. base_flags |= TXD_FLAG_TCPUDP_CSUM;
  9727. if (tg3_flag(tp, HW_TSO_3)) {
  9728. mss |= (hdr_len & 0xc) << 12;
  9729. if (hdr_len & 0x10)
  9730. base_flags |= 0x00000010;
  9731. base_flags |= (hdr_len & 0x3e0) << 5;
  9732. } else if (tg3_flag(tp, HW_TSO_2))
  9733. mss |= hdr_len << 9;
  9734. else if (tg3_flag(tp, HW_TSO_1) ||
  9735. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
  9736. mss |= (TG3_TSO_TCP_OPT_LEN << 9);
  9737. } else {
  9738. base_flags |= (TG3_TSO_TCP_OPT_LEN << 10);
  9739. }
  9740. data_off = ETH_ALEN * 2 + sizeof(tg3_tso_header);
  9741. } else {
  9742. num_pkts = 1;
  9743. data_off = ETH_HLEN;
  9744. if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
  9745. tx_len > VLAN_ETH_FRAME_LEN)
  9746. base_flags |= TXD_FLAG_JMB_PKT;
  9747. }
  9748. for (i = data_off; i < tx_len; i++)
  9749. tx_data[i] = (u8) (i & 0xff);
  9750. map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
  9751. if (pci_dma_mapping_error(tp->pdev, map)) {
  9752. dev_kfree_skb(skb);
  9753. return -EIO;
  9754. }
  9755. val = tnapi->tx_prod;
  9756. tnapi->tx_buffers[val].skb = skb;
  9757. dma_unmap_addr_set(&tnapi->tx_buffers[val], mapping, map);
  9758. tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
  9759. rnapi->coal_now);
  9760. udelay(10);
  9761. rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
  9762. budget = tg3_tx_avail(tnapi);
  9763. if (tg3_tx_frag_set(tnapi, &val, &budget, map, tx_len,
  9764. base_flags | TXD_FLAG_END, mss, 0)) {
  9765. tnapi->tx_buffers[val].skb = NULL;
  9766. dev_kfree_skb(skb);
  9767. return -EIO;
  9768. }
  9769. tnapi->tx_prod++;
  9770. /* Sync BD data before updating mailbox */
  9771. wmb();
  9772. tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
  9773. tr32_mailbox(tnapi->prodmbox);
  9774. udelay(10);
  9775. /* 350 usec to allow enough time on some 10/100 Mbps devices. */
  9776. for (i = 0; i < 35; i++) {
  9777. tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
  9778. coal_now);
  9779. udelay(10);
  9780. tx_idx = tnapi->hw_status->idx[0].tx_consumer;
  9781. rx_idx = rnapi->hw_status->idx[0].rx_producer;
  9782. if ((tx_idx == tnapi->tx_prod) &&
  9783. (rx_idx == (rx_start_idx + num_pkts)))
  9784. break;
  9785. }
  9786. tg3_tx_skb_unmap(tnapi, tnapi->tx_prod - 1, -1);
  9787. dev_kfree_skb(skb);
  9788. if (tx_idx != tnapi->tx_prod)
  9789. goto out;
  9790. if (rx_idx != rx_start_idx + num_pkts)
  9791. goto out;
  9792. val = data_off;
  9793. while (rx_idx != rx_start_idx) {
  9794. desc = &rnapi->rx_rcb[rx_start_idx++];
  9795. desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
  9796. opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
  9797. if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
  9798. (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
  9799. goto out;
  9800. rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT)
  9801. - ETH_FCS_LEN;
  9802. if (!tso_loopback) {
  9803. if (rx_len != tx_len)
  9804. goto out;
  9805. if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
  9806. if (opaque_key != RXD_OPAQUE_RING_STD)
  9807. goto out;
  9808. } else {
  9809. if (opaque_key != RXD_OPAQUE_RING_JUMBO)
  9810. goto out;
  9811. }
  9812. } else if ((desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
  9813. (desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
  9814. >> RXD_TCPCSUM_SHIFT != 0xffff) {
  9815. goto out;
  9816. }
  9817. if (opaque_key == RXD_OPAQUE_RING_STD) {
  9818. rx_data = tpr->rx_std_buffers[desc_idx].data;
  9819. map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx],
  9820. mapping);
  9821. } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
  9822. rx_data = tpr->rx_jmb_buffers[desc_idx].data;
  9823. map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx],
  9824. mapping);
  9825. } else
  9826. goto out;
  9827. pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len,
  9828. PCI_DMA_FROMDEVICE);
  9829. rx_data += TG3_RX_OFFSET(tp);
  9830. for (i = data_off; i < rx_len; i++, val++) {
  9831. if (*(rx_data + i) != (u8) (val & 0xff))
  9832. goto out;
  9833. }
  9834. }
  9835. err = 0;
  9836. /* tg3_free_rings will unmap and free the rx_data */
  9837. out:
  9838. return err;
  9839. }
  9840. #define TG3_STD_LOOPBACK_FAILED 1
  9841. #define TG3_JMB_LOOPBACK_FAILED 2
  9842. #define TG3_TSO_LOOPBACK_FAILED 4
  9843. #define TG3_LOOPBACK_FAILED \
  9844. (TG3_STD_LOOPBACK_FAILED | \
  9845. TG3_JMB_LOOPBACK_FAILED | \
  9846. TG3_TSO_LOOPBACK_FAILED)
  9847. static int tg3_test_loopback(struct tg3 *tp, u64 *data, bool do_extlpbk)
  9848. {
  9849. int err = -EIO;
  9850. u32 eee_cap;
  9851. u32 jmb_pkt_sz = 9000;
  9852. if (tp->dma_limit)
  9853. jmb_pkt_sz = tp->dma_limit - ETH_HLEN;
  9854. eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
  9855. tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
  9856. if (!netif_running(tp->dev)) {
  9857. data[0] = TG3_LOOPBACK_FAILED;
  9858. data[1] = TG3_LOOPBACK_FAILED;
  9859. if (do_extlpbk)
  9860. data[2] = TG3_LOOPBACK_FAILED;
  9861. goto done;
  9862. }
  9863. err = tg3_reset_hw(tp, 1);
  9864. if (err) {
  9865. data[0] = TG3_LOOPBACK_FAILED;
  9866. data[1] = TG3_LOOPBACK_FAILED;
  9867. if (do_extlpbk)
  9868. data[2] = TG3_LOOPBACK_FAILED;
  9869. goto done;
  9870. }
  9871. if (tg3_flag(tp, ENABLE_RSS)) {
  9872. int i;
  9873. /* Reroute all rx packets to the 1st queue */
  9874. for (i = MAC_RSS_INDIR_TBL_0;
  9875. i < MAC_RSS_INDIR_TBL_0 + TG3_RSS_INDIR_TBL_SIZE; i += 4)
  9876. tw32(i, 0x0);
  9877. }
  9878. /* HW errata - mac loopback fails in some cases on 5780.
  9879. * Normal traffic and PHY loopback are not affected by
  9880. * errata. Also, the MAC loopback test is deprecated for
  9881. * all newer ASIC revisions.
  9882. */
  9883. if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
  9884. !tg3_flag(tp, CPMU_PRESENT)) {
  9885. tg3_mac_loopback(tp, true);
  9886. if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
  9887. data[0] |= TG3_STD_LOOPBACK_FAILED;
  9888. if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
  9889. tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
  9890. data[0] |= TG3_JMB_LOOPBACK_FAILED;
  9891. tg3_mac_loopback(tp, false);
  9892. }
  9893. if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
  9894. !tg3_flag(tp, USE_PHYLIB)) {
  9895. int i;
  9896. tg3_phy_lpbk_set(tp, 0, false);
  9897. /* Wait for link */
  9898. for (i = 0; i < 100; i++) {
  9899. if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
  9900. break;
  9901. mdelay(1);
  9902. }
  9903. if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
  9904. data[1] |= TG3_STD_LOOPBACK_FAILED;
  9905. if (tg3_flag(tp, TSO_CAPABLE) &&
  9906. tg3_run_loopback(tp, ETH_FRAME_LEN, true))
  9907. data[1] |= TG3_TSO_LOOPBACK_FAILED;
  9908. if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
  9909. tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
  9910. data[1] |= TG3_JMB_LOOPBACK_FAILED;
  9911. if (do_extlpbk) {
  9912. tg3_phy_lpbk_set(tp, 0, true);
  9913. /* All link indications report up, but the hardware
  9914. * isn't really ready for about 20 msec. Double it
  9915. * to be sure.
  9916. */
  9917. mdelay(40);
  9918. if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
  9919. data[2] |= TG3_STD_LOOPBACK_FAILED;
  9920. if (tg3_flag(tp, TSO_CAPABLE) &&
  9921. tg3_run_loopback(tp, ETH_FRAME_LEN, true))
  9922. data[2] |= TG3_TSO_LOOPBACK_FAILED;
  9923. if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
  9924. tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
  9925. data[2] |= TG3_JMB_LOOPBACK_FAILED;
  9926. }
  9927. /* Re-enable gphy autopowerdown. */
  9928. if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
  9929. tg3_phy_toggle_apd(tp, true);
  9930. }
  9931. err = (data[0] | data[1] | data[2]) ? -EIO : 0;
  9932. done:
  9933. tp->phy_flags |= eee_cap;
  9934. return err;
  9935. }
  9936. static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
  9937. u64 *data)
  9938. {
  9939. struct tg3 *tp = netdev_priv(dev);
  9940. bool doextlpbk = etest->flags & ETH_TEST_FL_EXTERNAL_LB;
  9941. if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
  9942. tg3_power_up(tp)) {
  9943. etest->flags |= ETH_TEST_FL_FAILED;
  9944. memset(data, 1, sizeof(u64) * TG3_NUM_TEST);
  9945. return;
  9946. }
  9947. memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
  9948. if (tg3_test_nvram(tp) != 0) {
  9949. etest->flags |= ETH_TEST_FL_FAILED;
  9950. data[0] = 1;
  9951. }
  9952. if (!doextlpbk && tg3_test_link(tp)) {
  9953. etest->flags |= ETH_TEST_FL_FAILED;
  9954. data[1] = 1;
  9955. }
  9956. if (etest->flags & ETH_TEST_FL_OFFLINE) {
  9957. int err, err2 = 0, irq_sync = 0;
  9958. if (netif_running(dev)) {
  9959. tg3_phy_stop(tp);
  9960. tg3_netif_stop(tp);
  9961. irq_sync = 1;
  9962. }
  9963. tg3_full_lock(tp, irq_sync);
  9964. tg3_halt(tp, RESET_KIND_SUSPEND, 1);
  9965. err = tg3_nvram_lock(tp);
  9966. tg3_halt_cpu(tp, RX_CPU_BASE);
  9967. if (!tg3_flag(tp, 5705_PLUS))
  9968. tg3_halt_cpu(tp, TX_CPU_BASE);
  9969. if (!err)
  9970. tg3_nvram_unlock(tp);
  9971. if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
  9972. tg3_phy_reset(tp);
  9973. if (tg3_test_registers(tp) != 0) {
  9974. etest->flags |= ETH_TEST_FL_FAILED;
  9975. data[2] = 1;
  9976. }
  9977. if (tg3_test_memory(tp) != 0) {
  9978. etest->flags |= ETH_TEST_FL_FAILED;
  9979. data[3] = 1;
  9980. }
  9981. if (doextlpbk)
  9982. etest->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE;
  9983. if (tg3_test_loopback(tp, &data[4], doextlpbk))
  9984. etest->flags |= ETH_TEST_FL_FAILED;
  9985. tg3_full_unlock(tp);
  9986. if (tg3_test_interrupt(tp) != 0) {
  9987. etest->flags |= ETH_TEST_FL_FAILED;
  9988. data[7] = 1;
  9989. }
  9990. tg3_full_lock(tp, 0);
  9991. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  9992. if (netif_running(dev)) {
  9993. tg3_flag_set(tp, INIT_COMPLETE);
  9994. err2 = tg3_restart_hw(tp, 1);
  9995. if (!err2)
  9996. tg3_netif_start(tp);
  9997. }
  9998. tg3_full_unlock(tp);
  9999. if (irq_sync && !err2)
  10000. tg3_phy_start(tp);
  10001. }
  10002. if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
  10003. tg3_power_down(tp);
  10004. }
  10005. static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  10006. {
  10007. struct mii_ioctl_data *data = if_mii(ifr);
  10008. struct tg3 *tp = netdev_priv(dev);
  10009. int err;
  10010. if (tg3_flag(tp, USE_PHYLIB)) {
  10011. struct phy_device *phydev;
  10012. if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
  10013. return -EAGAIN;
  10014. phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
  10015. return phy_mii_ioctl(phydev, ifr, cmd);
  10016. }
  10017. switch (cmd) {
  10018. case SIOCGMIIPHY:
  10019. data->phy_id = tp->phy_addr;
  10020. /* fallthru */
  10021. case SIOCGMIIREG: {
  10022. u32 mii_regval;
  10023. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
  10024. break; /* We have no PHY */
  10025. if (!netif_running(dev))
  10026. return -EAGAIN;
  10027. spin_lock_bh(&tp->lock);
  10028. err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
  10029. spin_unlock_bh(&tp->lock);
  10030. data->val_out = mii_regval;
  10031. return err;
  10032. }
  10033. case SIOCSMIIREG:
  10034. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
  10035. break; /* We have no PHY */
  10036. if (!netif_running(dev))
  10037. return -EAGAIN;
  10038. spin_lock_bh(&tp->lock);
  10039. err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
  10040. spin_unlock_bh(&tp->lock);
  10041. return err;
  10042. default:
  10043. /* do nothing */
  10044. break;
  10045. }
  10046. return -EOPNOTSUPP;
  10047. }
  10048. static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
  10049. {
  10050. struct tg3 *tp = netdev_priv(dev);
  10051. memcpy(ec, &tp->coal, sizeof(*ec));
  10052. return 0;
  10053. }
  10054. static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
  10055. {
  10056. struct tg3 *tp = netdev_priv(dev);
  10057. u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
  10058. u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
  10059. if (!tg3_flag(tp, 5705_PLUS)) {
  10060. max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
  10061. max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
  10062. max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
  10063. min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
  10064. }
  10065. if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
  10066. (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
  10067. (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
  10068. (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
  10069. (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
  10070. (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
  10071. (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
  10072. (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
  10073. (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
  10074. (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
  10075. return -EINVAL;
  10076. /* No rx interrupts will be generated if both are zero */
  10077. if ((ec->rx_coalesce_usecs == 0) &&
  10078. (ec->rx_max_coalesced_frames == 0))
  10079. return -EINVAL;
  10080. /* No tx interrupts will be generated if both are zero */
  10081. if ((ec->tx_coalesce_usecs == 0) &&
  10082. (ec->tx_max_coalesced_frames == 0))
  10083. return -EINVAL;
  10084. /* Only copy relevant parameters, ignore all others. */
  10085. tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
  10086. tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
  10087. tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
  10088. tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
  10089. tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
  10090. tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
  10091. tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
  10092. tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
  10093. tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
  10094. if (netif_running(dev)) {
  10095. tg3_full_lock(tp, 0);
  10096. __tg3_set_coalesce(tp, &tp->coal);
  10097. tg3_full_unlock(tp);
  10098. }
  10099. return 0;
  10100. }
  10101. static const struct ethtool_ops tg3_ethtool_ops = {
  10102. .get_settings = tg3_get_settings,
  10103. .set_settings = tg3_set_settings,
  10104. .get_drvinfo = tg3_get_drvinfo,
  10105. .get_regs_len = tg3_get_regs_len,
  10106. .get_regs = tg3_get_regs,
  10107. .get_wol = tg3_get_wol,
  10108. .set_wol = tg3_set_wol,
  10109. .get_msglevel = tg3_get_msglevel,
  10110. .set_msglevel = tg3_set_msglevel,
  10111. .nway_reset = tg3_nway_reset,
  10112. .get_link = ethtool_op_get_link,
  10113. .get_eeprom_len = tg3_get_eeprom_len,
  10114. .get_eeprom = tg3_get_eeprom,
  10115. .set_eeprom = tg3_set_eeprom,
  10116. .get_ringparam = tg3_get_ringparam,
  10117. .set_ringparam = tg3_set_ringparam,
  10118. .get_pauseparam = tg3_get_pauseparam,
  10119. .set_pauseparam = tg3_set_pauseparam,
  10120. .self_test = tg3_self_test,
  10121. .get_strings = tg3_get_strings,
  10122. .set_phys_id = tg3_set_phys_id,
  10123. .get_ethtool_stats = tg3_get_ethtool_stats,
  10124. .get_coalesce = tg3_get_coalesce,
  10125. .set_coalesce = tg3_set_coalesce,
  10126. .get_sset_count = tg3_get_sset_count,
  10127. .get_rxnfc = tg3_get_rxnfc,
  10128. .get_rxfh_indir_size = tg3_get_rxfh_indir_size,
  10129. .get_rxfh_indir = tg3_get_rxfh_indir,
  10130. .set_rxfh_indir = tg3_set_rxfh_indir,
  10131. .get_ts_info = ethtool_op_get_ts_info,
  10132. };
  10133. static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
  10134. struct rtnl_link_stats64 *stats)
  10135. {
  10136. struct tg3 *tp = netdev_priv(dev);
  10137. if (!tp->hw_stats)
  10138. return &tp->net_stats_prev;
  10139. spin_lock_bh(&tp->lock);
  10140. tg3_get_nstats(tp, stats);
  10141. spin_unlock_bh(&tp->lock);
  10142. return stats;
  10143. }
  10144. static void tg3_set_rx_mode(struct net_device *dev)
  10145. {
  10146. struct tg3 *tp = netdev_priv(dev);
  10147. if (!netif_running(dev))
  10148. return;
  10149. tg3_full_lock(tp, 0);
  10150. __tg3_set_rx_mode(dev);
  10151. tg3_full_unlock(tp);
  10152. }
  10153. static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
  10154. int new_mtu)
  10155. {
  10156. dev->mtu = new_mtu;
  10157. if (new_mtu > ETH_DATA_LEN) {
  10158. if (tg3_flag(tp, 5780_CLASS)) {
  10159. netdev_update_features(dev);
  10160. tg3_flag_clear(tp, TSO_CAPABLE);
  10161. } else {
  10162. tg3_flag_set(tp, JUMBO_RING_ENABLE);
  10163. }
  10164. } else {
  10165. if (tg3_flag(tp, 5780_CLASS)) {
  10166. tg3_flag_set(tp, TSO_CAPABLE);
  10167. netdev_update_features(dev);
  10168. }
  10169. tg3_flag_clear(tp, JUMBO_RING_ENABLE);
  10170. }
  10171. }
  10172. static int tg3_change_mtu(struct net_device *dev, int new_mtu)
  10173. {
  10174. struct tg3 *tp = netdev_priv(dev);
  10175. int err, reset_phy = 0;
  10176. if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
  10177. return -EINVAL;
  10178. if (!netif_running(dev)) {
  10179. /* We'll just catch it later when the
  10180. * device is up'd.
  10181. */
  10182. tg3_set_mtu(dev, tp, new_mtu);
  10183. return 0;
  10184. }
  10185. tg3_phy_stop(tp);
  10186. tg3_netif_stop(tp);
  10187. tg3_full_lock(tp, 1);
  10188. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  10189. tg3_set_mtu(dev, tp, new_mtu);
  10190. /* Reset PHY, otherwise the read DMA engine will be in a mode that
  10191. * breaks all requests to 256 bytes.
  10192. */
  10193. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57766)
  10194. reset_phy = 1;
  10195. err = tg3_restart_hw(tp, reset_phy);
  10196. if (!err)
  10197. tg3_netif_start(tp);
  10198. tg3_full_unlock(tp);
  10199. if (!err)
  10200. tg3_phy_start(tp);
  10201. return err;
  10202. }
  10203. static const struct net_device_ops tg3_netdev_ops = {
  10204. .ndo_open = tg3_open,
  10205. .ndo_stop = tg3_close,
  10206. .ndo_start_xmit = tg3_start_xmit,
  10207. .ndo_get_stats64 = tg3_get_stats64,
  10208. .ndo_validate_addr = eth_validate_addr,
  10209. .ndo_set_rx_mode = tg3_set_rx_mode,
  10210. .ndo_set_mac_address = tg3_set_mac_addr,
  10211. .ndo_do_ioctl = tg3_ioctl,
  10212. .ndo_tx_timeout = tg3_tx_timeout,
  10213. .ndo_change_mtu = tg3_change_mtu,
  10214. .ndo_fix_features = tg3_fix_features,
  10215. .ndo_set_features = tg3_set_features,
  10216. #ifdef CONFIG_NET_POLL_CONTROLLER
  10217. .ndo_poll_controller = tg3_poll_controller,
  10218. #endif
  10219. };
  10220. static void __devinit tg3_get_eeprom_size(struct tg3 *tp)
  10221. {
  10222. u32 cursize, val, magic;
  10223. tp->nvram_size = EEPROM_CHIP_SIZE;
  10224. if (tg3_nvram_read(tp, 0, &magic) != 0)
  10225. return;
  10226. if ((magic != TG3_EEPROM_MAGIC) &&
  10227. ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
  10228. ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
  10229. return;
  10230. /*
  10231. * Size the chip by reading offsets at increasing powers of two.
  10232. * When we encounter our validation signature, we know the addressing
  10233. * has wrapped around, and thus have our chip size.
  10234. */
  10235. cursize = 0x10;
  10236. while (cursize < tp->nvram_size) {
  10237. if (tg3_nvram_read(tp, cursize, &val) != 0)
  10238. return;
  10239. if (val == magic)
  10240. break;
  10241. cursize <<= 1;
  10242. }
  10243. tp->nvram_size = cursize;
  10244. }
  10245. static void __devinit tg3_get_nvram_size(struct tg3 *tp)
  10246. {
  10247. u32 val;
  10248. if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &val) != 0)
  10249. return;
  10250. /* Selfboot format */
  10251. if (val != TG3_EEPROM_MAGIC) {
  10252. tg3_get_eeprom_size(tp);
  10253. return;
  10254. }
  10255. if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
  10256. if (val != 0) {
  10257. /* This is confusing. We want to operate on the
  10258. * 16-bit value at offset 0xf2. The tg3_nvram_read()
  10259. * call will read from NVRAM and byteswap the data
  10260. * according to the byteswapping settings for all
  10261. * other register accesses. This ensures the data we
  10262. * want will always reside in the lower 16-bits.
  10263. * However, the data in NVRAM is in LE format, which
  10264. * means the data from the NVRAM read will always be
  10265. * opposite the endianness of the CPU. The 16-bit
  10266. * byteswap then brings the data to CPU endianness.
  10267. */
  10268. tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
  10269. return;
  10270. }
  10271. }
  10272. tp->nvram_size = TG3_NVRAM_SIZE_512KB;
  10273. }
  10274. static void __devinit tg3_get_nvram_info(struct tg3 *tp)
  10275. {
  10276. u32 nvcfg1;
  10277. nvcfg1 = tr32(NVRAM_CFG1);
  10278. if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
  10279. tg3_flag_set(tp, FLASH);
  10280. } else {
  10281. nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
  10282. tw32(NVRAM_CFG1, nvcfg1);
  10283. }
  10284. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
  10285. tg3_flag(tp, 5780_CLASS)) {
  10286. switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
  10287. case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
  10288. tp->nvram_jedecnum = JEDEC_ATMEL;
  10289. tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
  10290. tg3_flag_set(tp, NVRAM_BUFFERED);
  10291. break;
  10292. case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
  10293. tp->nvram_jedecnum = JEDEC_ATMEL;
  10294. tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
  10295. break;
  10296. case FLASH_VENDOR_ATMEL_EEPROM:
  10297. tp->nvram_jedecnum = JEDEC_ATMEL;
  10298. tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
  10299. tg3_flag_set(tp, NVRAM_BUFFERED);
  10300. break;
  10301. case FLASH_VENDOR_ST:
  10302. tp->nvram_jedecnum = JEDEC_ST;
  10303. tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
  10304. tg3_flag_set(tp, NVRAM_BUFFERED);
  10305. break;
  10306. case FLASH_VENDOR_SAIFUN:
  10307. tp->nvram_jedecnum = JEDEC_SAIFUN;
  10308. tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
  10309. break;
  10310. case FLASH_VENDOR_SST_SMALL:
  10311. case FLASH_VENDOR_SST_LARGE:
  10312. tp->nvram_jedecnum = JEDEC_SST;
  10313. tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
  10314. break;
  10315. }
  10316. } else {
  10317. tp->nvram_jedecnum = JEDEC_ATMEL;
  10318. tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
  10319. tg3_flag_set(tp, NVRAM_BUFFERED);
  10320. }
  10321. }
  10322. static void __devinit tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
  10323. {
  10324. switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
  10325. case FLASH_5752PAGE_SIZE_256:
  10326. tp->nvram_pagesize = 256;
  10327. break;
  10328. case FLASH_5752PAGE_SIZE_512:
  10329. tp->nvram_pagesize = 512;
  10330. break;
  10331. case FLASH_5752PAGE_SIZE_1K:
  10332. tp->nvram_pagesize = 1024;
  10333. break;
  10334. case FLASH_5752PAGE_SIZE_2K:
  10335. tp->nvram_pagesize = 2048;
  10336. break;
  10337. case FLASH_5752PAGE_SIZE_4K:
  10338. tp->nvram_pagesize = 4096;
  10339. break;
  10340. case FLASH_5752PAGE_SIZE_264:
  10341. tp->nvram_pagesize = 264;
  10342. break;
  10343. case FLASH_5752PAGE_SIZE_528:
  10344. tp->nvram_pagesize = 528;
  10345. break;
  10346. }
  10347. }
  10348. static void __devinit tg3_get_5752_nvram_info(struct tg3 *tp)
  10349. {
  10350. u32 nvcfg1;
  10351. nvcfg1 = tr32(NVRAM_CFG1);
  10352. /* NVRAM protection for TPM */
  10353. if (nvcfg1 & (1 << 27))
  10354. tg3_flag_set(tp, PROTECTED_NVRAM);
  10355. switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
  10356. case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
  10357. case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
  10358. tp->nvram_jedecnum = JEDEC_ATMEL;
  10359. tg3_flag_set(tp, NVRAM_BUFFERED);
  10360. break;
  10361. case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
  10362. tp->nvram_jedecnum = JEDEC_ATMEL;
  10363. tg3_flag_set(tp, NVRAM_BUFFERED);
  10364. tg3_flag_set(tp, FLASH);
  10365. break;
  10366. case FLASH_5752VENDOR_ST_M45PE10:
  10367. case FLASH_5752VENDOR_ST_M45PE20:
  10368. case FLASH_5752VENDOR_ST_M45PE40:
  10369. tp->nvram_jedecnum = JEDEC_ST;
  10370. tg3_flag_set(tp, NVRAM_BUFFERED);
  10371. tg3_flag_set(tp, FLASH);
  10372. break;
  10373. }
  10374. if (tg3_flag(tp, FLASH)) {
  10375. tg3_nvram_get_pagesize(tp, nvcfg1);
  10376. } else {
  10377. /* For eeprom, set pagesize to maximum eeprom size */
  10378. tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
  10379. nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
  10380. tw32(NVRAM_CFG1, nvcfg1);
  10381. }
  10382. }
  10383. static void __devinit tg3_get_5755_nvram_info(struct tg3 *tp)
  10384. {
  10385. u32 nvcfg1, protect = 0;
  10386. nvcfg1 = tr32(NVRAM_CFG1);
  10387. /* NVRAM protection for TPM */
  10388. if (nvcfg1 & (1 << 27)) {
  10389. tg3_flag_set(tp, PROTECTED_NVRAM);
  10390. protect = 1;
  10391. }
  10392. nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
  10393. switch (nvcfg1) {
  10394. case FLASH_5755VENDOR_ATMEL_FLASH_1:
  10395. case FLASH_5755VENDOR_ATMEL_FLASH_2:
  10396. case FLASH_5755VENDOR_ATMEL_FLASH_3:
  10397. case FLASH_5755VENDOR_ATMEL_FLASH_5:
  10398. tp->nvram_jedecnum = JEDEC_ATMEL;
  10399. tg3_flag_set(tp, NVRAM_BUFFERED);
  10400. tg3_flag_set(tp, FLASH);
  10401. tp->nvram_pagesize = 264;
  10402. if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
  10403. nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
  10404. tp->nvram_size = (protect ? 0x3e200 :
  10405. TG3_NVRAM_SIZE_512KB);
  10406. else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
  10407. tp->nvram_size = (protect ? 0x1f200 :
  10408. TG3_NVRAM_SIZE_256KB);
  10409. else
  10410. tp->nvram_size = (protect ? 0x1f200 :
  10411. TG3_NVRAM_SIZE_128KB);
  10412. break;
  10413. case FLASH_5752VENDOR_ST_M45PE10:
  10414. case FLASH_5752VENDOR_ST_M45PE20:
  10415. case FLASH_5752VENDOR_ST_M45PE40:
  10416. tp->nvram_jedecnum = JEDEC_ST;
  10417. tg3_flag_set(tp, NVRAM_BUFFERED);
  10418. tg3_flag_set(tp, FLASH);
  10419. tp->nvram_pagesize = 256;
  10420. if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
  10421. tp->nvram_size = (protect ?
  10422. TG3_NVRAM_SIZE_64KB :
  10423. TG3_NVRAM_SIZE_128KB);
  10424. else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
  10425. tp->nvram_size = (protect ?
  10426. TG3_NVRAM_SIZE_64KB :
  10427. TG3_NVRAM_SIZE_256KB);
  10428. else
  10429. tp->nvram_size = (protect ?
  10430. TG3_NVRAM_SIZE_128KB :
  10431. TG3_NVRAM_SIZE_512KB);
  10432. break;
  10433. }
  10434. }
  10435. static void __devinit tg3_get_5787_nvram_info(struct tg3 *tp)
  10436. {
  10437. u32 nvcfg1;
  10438. nvcfg1 = tr32(NVRAM_CFG1);
  10439. switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
  10440. case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
  10441. case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
  10442. case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
  10443. case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
  10444. tp->nvram_jedecnum = JEDEC_ATMEL;
  10445. tg3_flag_set(tp, NVRAM_BUFFERED);
  10446. tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
  10447. nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
  10448. tw32(NVRAM_CFG1, nvcfg1);
  10449. break;
  10450. case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
  10451. case FLASH_5755VENDOR_ATMEL_FLASH_1:
  10452. case FLASH_5755VENDOR_ATMEL_FLASH_2:
  10453. case FLASH_5755VENDOR_ATMEL_FLASH_3:
  10454. tp->nvram_jedecnum = JEDEC_ATMEL;
  10455. tg3_flag_set(tp, NVRAM_BUFFERED);
  10456. tg3_flag_set(tp, FLASH);
  10457. tp->nvram_pagesize = 264;
  10458. break;
  10459. case FLASH_5752VENDOR_ST_M45PE10:
  10460. case FLASH_5752VENDOR_ST_M45PE20:
  10461. case FLASH_5752VENDOR_ST_M45PE40:
  10462. tp->nvram_jedecnum = JEDEC_ST;
  10463. tg3_flag_set(tp, NVRAM_BUFFERED);
  10464. tg3_flag_set(tp, FLASH);
  10465. tp->nvram_pagesize = 256;
  10466. break;
  10467. }
  10468. }
  10469. static void __devinit tg3_get_5761_nvram_info(struct tg3 *tp)
  10470. {
  10471. u32 nvcfg1, protect = 0;
  10472. nvcfg1 = tr32(NVRAM_CFG1);
  10473. /* NVRAM protection for TPM */
  10474. if (nvcfg1 & (1 << 27)) {
  10475. tg3_flag_set(tp, PROTECTED_NVRAM);
  10476. protect = 1;
  10477. }
  10478. nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
  10479. switch (nvcfg1) {
  10480. case FLASH_5761VENDOR_ATMEL_ADB021D:
  10481. case FLASH_5761VENDOR_ATMEL_ADB041D:
  10482. case FLASH_5761VENDOR_ATMEL_ADB081D:
  10483. case FLASH_5761VENDOR_ATMEL_ADB161D:
  10484. case FLASH_5761VENDOR_ATMEL_MDB021D:
  10485. case FLASH_5761VENDOR_ATMEL_MDB041D:
  10486. case FLASH_5761VENDOR_ATMEL_MDB081D:
  10487. case FLASH_5761VENDOR_ATMEL_MDB161D:
  10488. tp->nvram_jedecnum = JEDEC_ATMEL;
  10489. tg3_flag_set(tp, NVRAM_BUFFERED);
  10490. tg3_flag_set(tp, FLASH);
  10491. tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
  10492. tp->nvram_pagesize = 256;
  10493. break;
  10494. case FLASH_5761VENDOR_ST_A_M45PE20:
  10495. case FLASH_5761VENDOR_ST_A_M45PE40:
  10496. case FLASH_5761VENDOR_ST_A_M45PE80:
  10497. case FLASH_5761VENDOR_ST_A_M45PE16:
  10498. case FLASH_5761VENDOR_ST_M_M45PE20:
  10499. case FLASH_5761VENDOR_ST_M_M45PE40:
  10500. case FLASH_5761VENDOR_ST_M_M45PE80:
  10501. case FLASH_5761VENDOR_ST_M_M45PE16:
  10502. tp->nvram_jedecnum = JEDEC_ST;
  10503. tg3_flag_set(tp, NVRAM_BUFFERED);
  10504. tg3_flag_set(tp, FLASH);
  10505. tp->nvram_pagesize = 256;
  10506. break;
  10507. }
  10508. if (protect) {
  10509. tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
  10510. } else {
  10511. switch (nvcfg1) {
  10512. case FLASH_5761VENDOR_ATMEL_ADB161D:
  10513. case FLASH_5761VENDOR_ATMEL_MDB161D:
  10514. case FLASH_5761VENDOR_ST_A_M45PE16:
  10515. case FLASH_5761VENDOR_ST_M_M45PE16:
  10516. tp->nvram_size = TG3_NVRAM_SIZE_2MB;
  10517. break;
  10518. case FLASH_5761VENDOR_ATMEL_ADB081D:
  10519. case FLASH_5761VENDOR_ATMEL_MDB081D:
  10520. case FLASH_5761VENDOR_ST_A_M45PE80:
  10521. case FLASH_5761VENDOR_ST_M_M45PE80:
  10522. tp->nvram_size = TG3_NVRAM_SIZE_1MB;
  10523. break;
  10524. case FLASH_5761VENDOR_ATMEL_ADB041D:
  10525. case FLASH_5761VENDOR_ATMEL_MDB041D:
  10526. case FLASH_5761VENDOR_ST_A_M45PE40:
  10527. case FLASH_5761VENDOR_ST_M_M45PE40:
  10528. tp->nvram_size = TG3_NVRAM_SIZE_512KB;
  10529. break;
  10530. case FLASH_5761VENDOR_ATMEL_ADB021D:
  10531. case FLASH_5761VENDOR_ATMEL_MDB021D:
  10532. case FLASH_5761VENDOR_ST_A_M45PE20:
  10533. case FLASH_5761VENDOR_ST_M_M45PE20:
  10534. tp->nvram_size = TG3_NVRAM_SIZE_256KB;
  10535. break;
  10536. }
  10537. }
  10538. }
  10539. static void __devinit tg3_get_5906_nvram_info(struct tg3 *tp)
  10540. {
  10541. tp->nvram_jedecnum = JEDEC_ATMEL;
  10542. tg3_flag_set(tp, NVRAM_BUFFERED);
  10543. tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
  10544. }
  10545. static void __devinit tg3_get_57780_nvram_info(struct tg3 *tp)
  10546. {
  10547. u32 nvcfg1;
  10548. nvcfg1 = tr32(NVRAM_CFG1);
  10549. switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
  10550. case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
  10551. case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
  10552. tp->nvram_jedecnum = JEDEC_ATMEL;
  10553. tg3_flag_set(tp, NVRAM_BUFFERED);
  10554. tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
  10555. nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
  10556. tw32(NVRAM_CFG1, nvcfg1);
  10557. return;
  10558. case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
  10559. case FLASH_57780VENDOR_ATMEL_AT45DB011D:
  10560. case FLASH_57780VENDOR_ATMEL_AT45DB011B:
  10561. case FLASH_57780VENDOR_ATMEL_AT45DB021D:
  10562. case FLASH_57780VENDOR_ATMEL_AT45DB021B:
  10563. case FLASH_57780VENDOR_ATMEL_AT45DB041D:
  10564. case FLASH_57780VENDOR_ATMEL_AT45DB041B:
  10565. tp->nvram_jedecnum = JEDEC_ATMEL;
  10566. tg3_flag_set(tp, NVRAM_BUFFERED);
  10567. tg3_flag_set(tp, FLASH);
  10568. switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
  10569. case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
  10570. case FLASH_57780VENDOR_ATMEL_AT45DB011D:
  10571. case FLASH_57780VENDOR_ATMEL_AT45DB011B:
  10572. tp->nvram_size = TG3_NVRAM_SIZE_128KB;
  10573. break;
  10574. case FLASH_57780VENDOR_ATMEL_AT45DB021D:
  10575. case FLASH_57780VENDOR_ATMEL_AT45DB021B:
  10576. tp->nvram_size = TG3_NVRAM_SIZE_256KB;
  10577. break;
  10578. case FLASH_57780VENDOR_ATMEL_AT45DB041D:
  10579. case FLASH_57780VENDOR_ATMEL_AT45DB041B:
  10580. tp->nvram_size = TG3_NVRAM_SIZE_512KB;
  10581. break;
  10582. }
  10583. break;
  10584. case FLASH_5752VENDOR_ST_M45PE10:
  10585. case FLASH_5752VENDOR_ST_M45PE20:
  10586. case FLASH_5752VENDOR_ST_M45PE40:
  10587. tp->nvram_jedecnum = JEDEC_ST;
  10588. tg3_flag_set(tp, NVRAM_BUFFERED);
  10589. tg3_flag_set(tp, FLASH);
  10590. switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
  10591. case FLASH_5752VENDOR_ST_M45PE10:
  10592. tp->nvram_size = TG3_NVRAM_SIZE_128KB;
  10593. break;
  10594. case FLASH_5752VENDOR_ST_M45PE20:
  10595. tp->nvram_size = TG3_NVRAM_SIZE_256KB;
  10596. break;
  10597. case FLASH_5752VENDOR_ST_M45PE40:
  10598. tp->nvram_size = TG3_NVRAM_SIZE_512KB;
  10599. break;
  10600. }
  10601. break;
  10602. default:
  10603. tg3_flag_set(tp, NO_NVRAM);
  10604. return;
  10605. }
  10606. tg3_nvram_get_pagesize(tp, nvcfg1);
  10607. if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
  10608. tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
  10609. }
  10610. static void __devinit tg3_get_5717_nvram_info(struct tg3 *tp)
  10611. {
  10612. u32 nvcfg1;
  10613. nvcfg1 = tr32(NVRAM_CFG1);
  10614. switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
  10615. case FLASH_5717VENDOR_ATMEL_EEPROM:
  10616. case FLASH_5717VENDOR_MICRO_EEPROM:
  10617. tp->nvram_jedecnum = JEDEC_ATMEL;
  10618. tg3_flag_set(tp, NVRAM_BUFFERED);
  10619. tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
  10620. nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
  10621. tw32(NVRAM_CFG1, nvcfg1);
  10622. return;
  10623. case FLASH_5717VENDOR_ATMEL_MDB011D:
  10624. case FLASH_5717VENDOR_ATMEL_ADB011B:
  10625. case FLASH_5717VENDOR_ATMEL_ADB011D:
  10626. case FLASH_5717VENDOR_ATMEL_MDB021D:
  10627. case FLASH_5717VENDOR_ATMEL_ADB021B:
  10628. case FLASH_5717VENDOR_ATMEL_ADB021D:
  10629. case FLASH_5717VENDOR_ATMEL_45USPT:
  10630. tp->nvram_jedecnum = JEDEC_ATMEL;
  10631. tg3_flag_set(tp, NVRAM_BUFFERED);
  10632. tg3_flag_set(tp, FLASH);
  10633. switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
  10634. case FLASH_5717VENDOR_ATMEL_MDB021D:
  10635. /* Detect size with tg3_nvram_get_size() */
  10636. break;
  10637. case FLASH_5717VENDOR_ATMEL_ADB021B:
  10638. case FLASH_5717VENDOR_ATMEL_ADB021D:
  10639. tp->nvram_size = TG3_NVRAM_SIZE_256KB;
  10640. break;
  10641. default:
  10642. tp->nvram_size = TG3_NVRAM_SIZE_128KB;
  10643. break;
  10644. }
  10645. break;
  10646. case FLASH_5717VENDOR_ST_M_M25PE10:
  10647. case FLASH_5717VENDOR_ST_A_M25PE10:
  10648. case FLASH_5717VENDOR_ST_M_M45PE10:
  10649. case FLASH_5717VENDOR_ST_A_M45PE10:
  10650. case FLASH_5717VENDOR_ST_M_M25PE20:
  10651. case FLASH_5717VENDOR_ST_A_M25PE20:
  10652. case FLASH_5717VENDOR_ST_M_M45PE20:
  10653. case FLASH_5717VENDOR_ST_A_M45PE20:
  10654. case FLASH_5717VENDOR_ST_25USPT:
  10655. case FLASH_5717VENDOR_ST_45USPT:
  10656. tp->nvram_jedecnum = JEDEC_ST;
  10657. tg3_flag_set(tp, NVRAM_BUFFERED);
  10658. tg3_flag_set(tp, FLASH);
  10659. switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
  10660. case FLASH_5717VENDOR_ST_M_M25PE20:
  10661. case FLASH_5717VENDOR_ST_M_M45PE20:
  10662. /* Detect size with tg3_nvram_get_size() */
  10663. break;
  10664. case FLASH_5717VENDOR_ST_A_M25PE20:
  10665. case FLASH_5717VENDOR_ST_A_M45PE20:
  10666. tp->nvram_size = TG3_NVRAM_SIZE_256KB;
  10667. break;
  10668. default:
  10669. tp->nvram_size = TG3_NVRAM_SIZE_128KB;
  10670. break;
  10671. }
  10672. break;
  10673. default:
  10674. tg3_flag_set(tp, NO_NVRAM);
  10675. return;
  10676. }
  10677. tg3_nvram_get_pagesize(tp, nvcfg1);
  10678. if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
  10679. tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
  10680. }
  10681. static void __devinit tg3_get_5720_nvram_info(struct tg3 *tp)
  10682. {
  10683. u32 nvcfg1, nvmpinstrp;
  10684. nvcfg1 = tr32(NVRAM_CFG1);
  10685. nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
  10686. switch (nvmpinstrp) {
  10687. case FLASH_5720_EEPROM_HD:
  10688. case FLASH_5720_EEPROM_LD:
  10689. tp->nvram_jedecnum = JEDEC_ATMEL;
  10690. tg3_flag_set(tp, NVRAM_BUFFERED);
  10691. nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
  10692. tw32(NVRAM_CFG1, nvcfg1);
  10693. if (nvmpinstrp == FLASH_5720_EEPROM_HD)
  10694. tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
  10695. else
  10696. tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
  10697. return;
  10698. case FLASH_5720VENDOR_M_ATMEL_DB011D:
  10699. case FLASH_5720VENDOR_A_ATMEL_DB011B:
  10700. case FLASH_5720VENDOR_A_ATMEL_DB011D:
  10701. case FLASH_5720VENDOR_M_ATMEL_DB021D:
  10702. case FLASH_5720VENDOR_A_ATMEL_DB021B:
  10703. case FLASH_5720VENDOR_A_ATMEL_DB021D:
  10704. case FLASH_5720VENDOR_M_ATMEL_DB041D:
  10705. case FLASH_5720VENDOR_A_ATMEL_DB041B:
  10706. case FLASH_5720VENDOR_A_ATMEL_DB041D:
  10707. case FLASH_5720VENDOR_M_ATMEL_DB081D:
  10708. case FLASH_5720VENDOR_A_ATMEL_DB081D:
  10709. case FLASH_5720VENDOR_ATMEL_45USPT:
  10710. tp->nvram_jedecnum = JEDEC_ATMEL;
  10711. tg3_flag_set(tp, NVRAM_BUFFERED);
  10712. tg3_flag_set(tp, FLASH);
  10713. switch (nvmpinstrp) {
  10714. case FLASH_5720VENDOR_M_ATMEL_DB021D:
  10715. case FLASH_5720VENDOR_A_ATMEL_DB021B:
  10716. case FLASH_5720VENDOR_A_ATMEL_DB021D:
  10717. tp->nvram_size = TG3_NVRAM_SIZE_256KB;
  10718. break;
  10719. case FLASH_5720VENDOR_M_ATMEL_DB041D:
  10720. case FLASH_5720VENDOR_A_ATMEL_DB041B:
  10721. case FLASH_5720VENDOR_A_ATMEL_DB041D:
  10722. tp->nvram_size = TG3_NVRAM_SIZE_512KB;
  10723. break;
  10724. case FLASH_5720VENDOR_M_ATMEL_DB081D:
  10725. case FLASH_5720VENDOR_A_ATMEL_DB081D:
  10726. tp->nvram_size = TG3_NVRAM_SIZE_1MB;
  10727. break;
  10728. default:
  10729. tp->nvram_size = TG3_NVRAM_SIZE_128KB;
  10730. break;
  10731. }
  10732. break;
  10733. case FLASH_5720VENDOR_M_ST_M25PE10:
  10734. case FLASH_5720VENDOR_M_ST_M45PE10:
  10735. case FLASH_5720VENDOR_A_ST_M25PE10:
  10736. case FLASH_5720VENDOR_A_ST_M45PE10:
  10737. case FLASH_5720VENDOR_M_ST_M25PE20:
  10738. case FLASH_5720VENDOR_M_ST_M45PE20:
  10739. case FLASH_5720VENDOR_A_ST_M25PE20:
  10740. case FLASH_5720VENDOR_A_ST_M45PE20:
  10741. case FLASH_5720VENDOR_M_ST_M25PE40:
  10742. case FLASH_5720VENDOR_M_ST_M45PE40:
  10743. case FLASH_5720VENDOR_A_ST_M25PE40:
  10744. case FLASH_5720VENDOR_A_ST_M45PE40:
  10745. case FLASH_5720VENDOR_M_ST_M25PE80:
  10746. case FLASH_5720VENDOR_M_ST_M45PE80:
  10747. case FLASH_5720VENDOR_A_ST_M25PE80:
  10748. case FLASH_5720VENDOR_A_ST_M45PE80:
  10749. case FLASH_5720VENDOR_ST_25USPT:
  10750. case FLASH_5720VENDOR_ST_45USPT:
  10751. tp->nvram_jedecnum = JEDEC_ST;
  10752. tg3_flag_set(tp, NVRAM_BUFFERED);
  10753. tg3_flag_set(tp, FLASH);
  10754. switch (nvmpinstrp) {
  10755. case FLASH_5720VENDOR_M_ST_M25PE20:
  10756. case FLASH_5720VENDOR_M_ST_M45PE20:
  10757. case FLASH_5720VENDOR_A_ST_M25PE20:
  10758. case FLASH_5720VENDOR_A_ST_M45PE20:
  10759. tp->nvram_size = TG3_NVRAM_SIZE_256KB;
  10760. break;
  10761. case FLASH_5720VENDOR_M_ST_M25PE40:
  10762. case FLASH_5720VENDOR_M_ST_M45PE40:
  10763. case FLASH_5720VENDOR_A_ST_M25PE40:
  10764. case FLASH_5720VENDOR_A_ST_M45PE40:
  10765. tp->nvram_size = TG3_NVRAM_SIZE_512KB;
  10766. break;
  10767. case FLASH_5720VENDOR_M_ST_M25PE80:
  10768. case FLASH_5720VENDOR_M_ST_M45PE80:
  10769. case FLASH_5720VENDOR_A_ST_M25PE80:
  10770. case FLASH_5720VENDOR_A_ST_M45PE80:
  10771. tp->nvram_size = TG3_NVRAM_SIZE_1MB;
  10772. break;
  10773. default:
  10774. tp->nvram_size = TG3_NVRAM_SIZE_128KB;
  10775. break;
  10776. }
  10777. break;
  10778. default:
  10779. tg3_flag_set(tp, NO_NVRAM);
  10780. return;
  10781. }
  10782. tg3_nvram_get_pagesize(tp, nvcfg1);
  10783. if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
  10784. tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
  10785. }
  10786. /* Chips other than 5700/5701 use the NVRAM for fetching info. */
  10787. static void __devinit tg3_nvram_init(struct tg3 *tp)
  10788. {
  10789. tw32_f(GRC_EEPROM_ADDR,
  10790. (EEPROM_ADDR_FSM_RESET |
  10791. (EEPROM_DEFAULT_CLOCK_PERIOD <<
  10792. EEPROM_ADDR_CLKPERD_SHIFT)));
  10793. msleep(1);
  10794. /* Enable seeprom accesses. */
  10795. tw32_f(GRC_LOCAL_CTRL,
  10796. tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
  10797. udelay(100);
  10798. if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
  10799. GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
  10800. tg3_flag_set(tp, NVRAM);
  10801. if (tg3_nvram_lock(tp)) {
  10802. netdev_warn(tp->dev,
  10803. "Cannot get nvram lock, %s failed\n",
  10804. __func__);
  10805. return;
  10806. }
  10807. tg3_enable_nvram_access(tp);
  10808. tp->nvram_size = 0;
  10809. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
  10810. tg3_get_5752_nvram_info(tp);
  10811. else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
  10812. tg3_get_5755_nvram_info(tp);
  10813. else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
  10814. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
  10815. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
  10816. tg3_get_5787_nvram_info(tp);
  10817. else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
  10818. tg3_get_5761_nvram_info(tp);
  10819. else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
  10820. tg3_get_5906_nvram_info(tp);
  10821. else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
  10822. tg3_flag(tp, 57765_CLASS))
  10823. tg3_get_57780_nvram_info(tp);
  10824. else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
  10825. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
  10826. tg3_get_5717_nvram_info(tp);
  10827. else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
  10828. tg3_get_5720_nvram_info(tp);
  10829. else
  10830. tg3_get_nvram_info(tp);
  10831. if (tp->nvram_size == 0)
  10832. tg3_get_nvram_size(tp);
  10833. tg3_disable_nvram_access(tp);
  10834. tg3_nvram_unlock(tp);
  10835. } else {
  10836. tg3_flag_clear(tp, NVRAM);
  10837. tg3_flag_clear(tp, NVRAM_BUFFERED);
  10838. tg3_get_eeprom_size(tp);
  10839. }
  10840. }
  10841. struct subsys_tbl_ent {
  10842. u16 subsys_vendor, subsys_devid;
  10843. u32 phy_id;
  10844. };
  10845. static struct subsys_tbl_ent subsys_id_to_phy_id[] __devinitdata = {
  10846. /* Broadcom boards. */
  10847. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  10848. TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
  10849. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  10850. TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
  10851. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  10852. TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
  10853. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  10854. TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
  10855. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  10856. TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
  10857. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  10858. TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
  10859. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  10860. TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
  10861. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  10862. TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
  10863. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  10864. TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
  10865. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  10866. TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
  10867. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  10868. TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
  10869. /* 3com boards. */
  10870. { TG3PCI_SUBVENDOR_ID_3COM,
  10871. TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
  10872. { TG3PCI_SUBVENDOR_ID_3COM,
  10873. TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
  10874. { TG3PCI_SUBVENDOR_ID_3COM,
  10875. TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
  10876. { TG3PCI_SUBVENDOR_ID_3COM,
  10877. TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
  10878. { TG3PCI_SUBVENDOR_ID_3COM,
  10879. TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
  10880. /* DELL boards. */
  10881. { TG3PCI_SUBVENDOR_ID_DELL,
  10882. TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
  10883. { TG3PCI_SUBVENDOR_ID_DELL,
  10884. TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
  10885. { TG3PCI_SUBVENDOR_ID_DELL,
  10886. TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
  10887. { TG3PCI_SUBVENDOR_ID_DELL,
  10888. TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
  10889. /* Compaq boards. */
  10890. { TG3PCI_SUBVENDOR_ID_COMPAQ,
  10891. TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
  10892. { TG3PCI_SUBVENDOR_ID_COMPAQ,
  10893. TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
  10894. { TG3PCI_SUBVENDOR_ID_COMPAQ,
  10895. TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
  10896. { TG3PCI_SUBVENDOR_ID_COMPAQ,
  10897. TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
  10898. { TG3PCI_SUBVENDOR_ID_COMPAQ,
  10899. TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
  10900. /* IBM boards. */
  10901. { TG3PCI_SUBVENDOR_ID_IBM,
  10902. TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
  10903. };
  10904. static struct subsys_tbl_ent * __devinit tg3_lookup_by_subsys(struct tg3 *tp)
  10905. {
  10906. int i;
  10907. for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
  10908. if ((subsys_id_to_phy_id[i].subsys_vendor ==
  10909. tp->pdev->subsystem_vendor) &&
  10910. (subsys_id_to_phy_id[i].subsys_devid ==
  10911. tp->pdev->subsystem_device))
  10912. return &subsys_id_to_phy_id[i];
  10913. }
  10914. return NULL;
  10915. }
  10916. static void __devinit tg3_get_eeprom_hw_cfg(struct tg3 *tp)
  10917. {
  10918. u32 val;
  10919. tp->phy_id = TG3_PHY_ID_INVALID;
  10920. tp->led_ctrl = LED_CTRL_MODE_PHY_1;
  10921. /* Assume an onboard device and WOL capable by default. */
  10922. tg3_flag_set(tp, EEPROM_WRITE_PROT);
  10923. tg3_flag_set(tp, WOL_CAP);
  10924. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
  10925. if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
  10926. tg3_flag_clear(tp, EEPROM_WRITE_PROT);
  10927. tg3_flag_set(tp, IS_NIC);
  10928. }
  10929. val = tr32(VCPU_CFGSHDW);
  10930. if (val & VCPU_CFGSHDW_ASPM_DBNC)
  10931. tg3_flag_set(tp, ASPM_WORKAROUND);
  10932. if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
  10933. (val & VCPU_CFGSHDW_WOL_MAGPKT)) {
  10934. tg3_flag_set(tp, WOL_ENABLE);
  10935. device_set_wakeup_enable(&tp->pdev->dev, true);
  10936. }
  10937. goto done;
  10938. }
  10939. tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
  10940. if (val == NIC_SRAM_DATA_SIG_MAGIC) {
  10941. u32 nic_cfg, led_cfg;
  10942. u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
  10943. int eeprom_phy_serdes = 0;
  10944. tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
  10945. tp->nic_sram_data_cfg = nic_cfg;
  10946. tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
  10947. ver >>= NIC_SRAM_DATA_VER_SHIFT;
  10948. if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
  10949. GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
  10950. GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703 &&
  10951. (ver > 0) && (ver < 0x100))
  10952. tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
  10953. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
  10954. tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
  10955. if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
  10956. NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
  10957. eeprom_phy_serdes = 1;
  10958. tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
  10959. if (nic_phy_id != 0) {
  10960. u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
  10961. u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
  10962. eeprom_phy_id = (id1 >> 16) << 10;
  10963. eeprom_phy_id |= (id2 & 0xfc00) << 16;
  10964. eeprom_phy_id |= (id2 & 0x03ff) << 0;
  10965. } else
  10966. eeprom_phy_id = 0;
  10967. tp->phy_id = eeprom_phy_id;
  10968. if (eeprom_phy_serdes) {
  10969. if (!tg3_flag(tp, 5705_PLUS))
  10970. tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
  10971. else
  10972. tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
  10973. }
  10974. if (tg3_flag(tp, 5750_PLUS))
  10975. led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
  10976. SHASTA_EXT_LED_MODE_MASK);
  10977. else
  10978. led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
  10979. switch (led_cfg) {
  10980. default:
  10981. case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
  10982. tp->led_ctrl = LED_CTRL_MODE_PHY_1;
  10983. break;
  10984. case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
  10985. tp->led_ctrl = LED_CTRL_MODE_PHY_2;
  10986. break;
  10987. case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
  10988. tp->led_ctrl = LED_CTRL_MODE_MAC;
  10989. /* Default to PHY_1_MODE if 0 (MAC_MODE) is
  10990. * read on some older 5700/5701 bootcode.
  10991. */
  10992. if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
  10993. ASIC_REV_5700 ||
  10994. GET_ASIC_REV(tp->pci_chip_rev_id) ==
  10995. ASIC_REV_5701)
  10996. tp->led_ctrl = LED_CTRL_MODE_PHY_1;
  10997. break;
  10998. case SHASTA_EXT_LED_SHARED:
  10999. tp->led_ctrl = LED_CTRL_MODE_SHARED;
  11000. if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
  11001. tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
  11002. tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
  11003. LED_CTRL_MODE_PHY_2);
  11004. break;
  11005. case SHASTA_EXT_LED_MAC:
  11006. tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
  11007. break;
  11008. case SHASTA_EXT_LED_COMBO:
  11009. tp->led_ctrl = LED_CTRL_MODE_COMBO;
  11010. if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
  11011. tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
  11012. LED_CTRL_MODE_PHY_2);
  11013. break;
  11014. }
  11015. if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
  11016. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
  11017. tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
  11018. tp->led_ctrl = LED_CTRL_MODE_PHY_2;
  11019. if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
  11020. tp->led_ctrl = LED_CTRL_MODE_PHY_1;
  11021. if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
  11022. tg3_flag_set(tp, EEPROM_WRITE_PROT);
  11023. if ((tp->pdev->subsystem_vendor ==
  11024. PCI_VENDOR_ID_ARIMA) &&
  11025. (tp->pdev->subsystem_device == 0x205a ||
  11026. tp->pdev->subsystem_device == 0x2063))
  11027. tg3_flag_clear(tp, EEPROM_WRITE_PROT);
  11028. } else {
  11029. tg3_flag_clear(tp, EEPROM_WRITE_PROT);
  11030. tg3_flag_set(tp, IS_NIC);
  11031. }
  11032. if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
  11033. tg3_flag_set(tp, ENABLE_ASF);
  11034. if (tg3_flag(tp, 5750_PLUS))
  11035. tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
  11036. }
  11037. if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
  11038. tg3_flag(tp, 5750_PLUS))
  11039. tg3_flag_set(tp, ENABLE_APE);
  11040. if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
  11041. !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
  11042. tg3_flag_clear(tp, WOL_CAP);
  11043. if (tg3_flag(tp, WOL_CAP) &&
  11044. (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE)) {
  11045. tg3_flag_set(tp, WOL_ENABLE);
  11046. device_set_wakeup_enable(&tp->pdev->dev, true);
  11047. }
  11048. if (cfg2 & (1 << 17))
  11049. tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
  11050. /* serdes signal pre-emphasis in register 0x590 set by */
  11051. /* bootcode if bit 18 is set */
  11052. if (cfg2 & (1 << 18))
  11053. tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
  11054. if ((tg3_flag(tp, 57765_PLUS) ||
  11055. (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
  11056. GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX)) &&
  11057. (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
  11058. tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
  11059. if (tg3_flag(tp, PCI_EXPRESS) &&
  11060. GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
  11061. !tg3_flag(tp, 57765_PLUS)) {
  11062. u32 cfg3;
  11063. tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
  11064. if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
  11065. tg3_flag_set(tp, ASPM_WORKAROUND);
  11066. }
  11067. if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
  11068. tg3_flag_set(tp, RGMII_INBAND_DISABLE);
  11069. if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
  11070. tg3_flag_set(tp, RGMII_EXT_IBND_RX_EN);
  11071. if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
  11072. tg3_flag_set(tp, RGMII_EXT_IBND_TX_EN);
  11073. }
  11074. done:
  11075. if (tg3_flag(tp, WOL_CAP))
  11076. device_set_wakeup_enable(&tp->pdev->dev,
  11077. tg3_flag(tp, WOL_ENABLE));
  11078. else
  11079. device_set_wakeup_capable(&tp->pdev->dev, false);
  11080. }
  11081. static int __devinit tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
  11082. {
  11083. int i;
  11084. u32 val;
  11085. tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
  11086. tw32(OTP_CTRL, cmd);
  11087. /* Wait for up to 1 ms for command to execute. */
  11088. for (i = 0; i < 100; i++) {
  11089. val = tr32(OTP_STATUS);
  11090. if (val & OTP_STATUS_CMD_DONE)
  11091. break;
  11092. udelay(10);
  11093. }
  11094. return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
  11095. }
  11096. /* Read the gphy configuration from the OTP region of the chip. The gphy
  11097. * configuration is a 32-bit value that straddles the alignment boundary.
  11098. * We do two 32-bit reads and then shift and merge the results.
  11099. */
  11100. static u32 __devinit tg3_read_otp_phycfg(struct tg3 *tp)
  11101. {
  11102. u32 bhalf_otp, thalf_otp;
  11103. tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
  11104. if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
  11105. return 0;
  11106. tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
  11107. if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
  11108. return 0;
  11109. thalf_otp = tr32(OTP_READ_DATA);
  11110. tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
  11111. if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
  11112. return 0;
  11113. bhalf_otp = tr32(OTP_READ_DATA);
  11114. return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
  11115. }
  11116. static void __devinit tg3_phy_init_link_config(struct tg3 *tp)
  11117. {
  11118. u32 adv = ADVERTISED_Autoneg;
  11119. if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
  11120. adv |= ADVERTISED_1000baseT_Half |
  11121. ADVERTISED_1000baseT_Full;
  11122. if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
  11123. adv |= ADVERTISED_100baseT_Half |
  11124. ADVERTISED_100baseT_Full |
  11125. ADVERTISED_10baseT_Half |
  11126. ADVERTISED_10baseT_Full |
  11127. ADVERTISED_TP;
  11128. else
  11129. adv |= ADVERTISED_FIBRE;
  11130. tp->link_config.advertising = adv;
  11131. tp->link_config.speed = SPEED_UNKNOWN;
  11132. tp->link_config.duplex = DUPLEX_UNKNOWN;
  11133. tp->link_config.autoneg = AUTONEG_ENABLE;
  11134. tp->link_config.active_speed = SPEED_UNKNOWN;
  11135. tp->link_config.active_duplex = DUPLEX_UNKNOWN;
  11136. tp->old_link = -1;
  11137. }
  11138. static int __devinit tg3_phy_probe(struct tg3 *tp)
  11139. {
  11140. u32 hw_phy_id_1, hw_phy_id_2;
  11141. u32 hw_phy_id, hw_phy_id_masked;
  11142. int err;
  11143. /* flow control autonegotiation is default behavior */
  11144. tg3_flag_set(tp, PAUSE_AUTONEG);
  11145. tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
  11146. if (tg3_flag(tp, USE_PHYLIB))
  11147. return tg3_phy_init(tp);
  11148. /* Reading the PHY ID register can conflict with ASF
  11149. * firmware access to the PHY hardware.
  11150. */
  11151. err = 0;
  11152. if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)) {
  11153. hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
  11154. } else {
  11155. /* Now read the physical PHY_ID from the chip and verify
  11156. * that it is sane. If it doesn't look good, we fall back
  11157. * to either the hard-coded table based PHY_ID and failing
  11158. * that the value found in the eeprom area.
  11159. */
  11160. err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
  11161. err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
  11162. hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
  11163. hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
  11164. hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
  11165. hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
  11166. }
  11167. if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
  11168. tp->phy_id = hw_phy_id;
  11169. if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
  11170. tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
  11171. else
  11172. tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
  11173. } else {
  11174. if (tp->phy_id != TG3_PHY_ID_INVALID) {
  11175. /* Do nothing, phy ID already set up in
  11176. * tg3_get_eeprom_hw_cfg().
  11177. */
  11178. } else {
  11179. struct subsys_tbl_ent *p;
  11180. /* No eeprom signature? Try the hardcoded
  11181. * subsys device table.
  11182. */
  11183. p = tg3_lookup_by_subsys(tp);
  11184. if (!p)
  11185. return -ENODEV;
  11186. tp->phy_id = p->phy_id;
  11187. if (!tp->phy_id ||
  11188. tp->phy_id == TG3_PHY_ID_BCM8002)
  11189. tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
  11190. }
  11191. }
  11192. if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
  11193. (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
  11194. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720 ||
  11195. (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 &&
  11196. tp->pci_chip_rev_id != CHIPREV_ID_5717_A0) ||
  11197. (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
  11198. tp->pci_chip_rev_id != CHIPREV_ID_57765_A0)))
  11199. tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
  11200. tg3_phy_init_link_config(tp);
  11201. if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
  11202. !tg3_flag(tp, ENABLE_APE) &&
  11203. !tg3_flag(tp, ENABLE_ASF)) {
  11204. u32 bmsr, dummy;
  11205. tg3_readphy(tp, MII_BMSR, &bmsr);
  11206. if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
  11207. (bmsr & BMSR_LSTATUS))
  11208. goto skip_phy_reset;
  11209. err = tg3_phy_reset(tp);
  11210. if (err)
  11211. return err;
  11212. tg3_phy_set_wirespeed(tp);
  11213. if (!tg3_phy_copper_an_config_ok(tp, &dummy)) {
  11214. tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
  11215. tp->link_config.flowctrl);
  11216. tg3_writephy(tp, MII_BMCR,
  11217. BMCR_ANENABLE | BMCR_ANRESTART);
  11218. }
  11219. }
  11220. skip_phy_reset:
  11221. if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
  11222. err = tg3_init_5401phy_dsp(tp);
  11223. if (err)
  11224. return err;
  11225. err = tg3_init_5401phy_dsp(tp);
  11226. }
  11227. return err;
  11228. }
  11229. static void __devinit tg3_read_vpd(struct tg3 *tp)
  11230. {
  11231. u8 *vpd_data;
  11232. unsigned int block_end, rosize, len;
  11233. u32 vpdlen;
  11234. int j, i = 0;
  11235. vpd_data = (u8 *)tg3_vpd_readblock(tp, &vpdlen);
  11236. if (!vpd_data)
  11237. goto out_no_vpd;
  11238. i = pci_vpd_find_tag(vpd_data, 0, vpdlen, PCI_VPD_LRDT_RO_DATA);
  11239. if (i < 0)
  11240. goto out_not_found;
  11241. rosize = pci_vpd_lrdt_size(&vpd_data[i]);
  11242. block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
  11243. i += PCI_VPD_LRDT_TAG_SIZE;
  11244. if (block_end > vpdlen)
  11245. goto out_not_found;
  11246. j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
  11247. PCI_VPD_RO_KEYWORD_MFR_ID);
  11248. if (j > 0) {
  11249. len = pci_vpd_info_field_size(&vpd_data[j]);
  11250. j += PCI_VPD_INFO_FLD_HDR_SIZE;
  11251. if (j + len > block_end || len != 4 ||
  11252. memcmp(&vpd_data[j], "1028", 4))
  11253. goto partno;
  11254. j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
  11255. PCI_VPD_RO_KEYWORD_VENDOR0);
  11256. if (j < 0)
  11257. goto partno;
  11258. len = pci_vpd_info_field_size(&vpd_data[j]);
  11259. j += PCI_VPD_INFO_FLD_HDR_SIZE;
  11260. if (j + len > block_end)
  11261. goto partno;
  11262. memcpy(tp->fw_ver, &vpd_data[j], len);
  11263. strncat(tp->fw_ver, " bc ", vpdlen - len - 1);
  11264. }
  11265. partno:
  11266. i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
  11267. PCI_VPD_RO_KEYWORD_PARTNO);
  11268. if (i < 0)
  11269. goto out_not_found;
  11270. len = pci_vpd_info_field_size(&vpd_data[i]);
  11271. i += PCI_VPD_INFO_FLD_HDR_SIZE;
  11272. if (len > TG3_BPN_SIZE ||
  11273. (len + i) > vpdlen)
  11274. goto out_not_found;
  11275. memcpy(tp->board_part_number, &vpd_data[i], len);
  11276. out_not_found:
  11277. kfree(vpd_data);
  11278. if (tp->board_part_number[0])
  11279. return;
  11280. out_no_vpd:
  11281. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
  11282. if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717)
  11283. strcpy(tp->board_part_number, "BCM5717");
  11284. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
  11285. strcpy(tp->board_part_number, "BCM5718");
  11286. else
  11287. goto nomatch;
  11288. } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
  11289. if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
  11290. strcpy(tp->board_part_number, "BCM57780");
  11291. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
  11292. strcpy(tp->board_part_number, "BCM57760");
  11293. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
  11294. strcpy(tp->board_part_number, "BCM57790");
  11295. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
  11296. strcpy(tp->board_part_number, "BCM57788");
  11297. else
  11298. goto nomatch;
  11299. } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
  11300. if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
  11301. strcpy(tp->board_part_number, "BCM57761");
  11302. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
  11303. strcpy(tp->board_part_number, "BCM57765");
  11304. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
  11305. strcpy(tp->board_part_number, "BCM57781");
  11306. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
  11307. strcpy(tp->board_part_number, "BCM57785");
  11308. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
  11309. strcpy(tp->board_part_number, "BCM57791");
  11310. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
  11311. strcpy(tp->board_part_number, "BCM57795");
  11312. else
  11313. goto nomatch;
  11314. } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57766) {
  11315. if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762)
  11316. strcpy(tp->board_part_number, "BCM57762");
  11317. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766)
  11318. strcpy(tp->board_part_number, "BCM57766");
  11319. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782)
  11320. strcpy(tp->board_part_number, "BCM57782");
  11321. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
  11322. strcpy(tp->board_part_number, "BCM57786");
  11323. else
  11324. goto nomatch;
  11325. } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
  11326. strcpy(tp->board_part_number, "BCM95906");
  11327. } else {
  11328. nomatch:
  11329. strcpy(tp->board_part_number, "none");
  11330. }
  11331. }
  11332. static int __devinit tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
  11333. {
  11334. u32 val;
  11335. if (tg3_nvram_read(tp, offset, &val) ||
  11336. (val & 0xfc000000) != 0x0c000000 ||
  11337. tg3_nvram_read(tp, offset + 4, &val) ||
  11338. val != 0)
  11339. return 0;
  11340. return 1;
  11341. }
  11342. static void __devinit tg3_read_bc_ver(struct tg3 *tp)
  11343. {
  11344. u32 val, offset, start, ver_offset;
  11345. int i, dst_off;
  11346. bool newver = false;
  11347. if (tg3_nvram_read(tp, 0xc, &offset) ||
  11348. tg3_nvram_read(tp, 0x4, &start))
  11349. return;
  11350. offset = tg3_nvram_logical_addr(tp, offset);
  11351. if (tg3_nvram_read(tp, offset, &val))
  11352. return;
  11353. if ((val & 0xfc000000) == 0x0c000000) {
  11354. if (tg3_nvram_read(tp, offset + 4, &val))
  11355. return;
  11356. if (val == 0)
  11357. newver = true;
  11358. }
  11359. dst_off = strlen(tp->fw_ver);
  11360. if (newver) {
  11361. if (TG3_VER_SIZE - dst_off < 16 ||
  11362. tg3_nvram_read(tp, offset + 8, &ver_offset))
  11363. return;
  11364. offset = offset + ver_offset - start;
  11365. for (i = 0; i < 16; i += 4) {
  11366. __be32 v;
  11367. if (tg3_nvram_read_be32(tp, offset + i, &v))
  11368. return;
  11369. memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
  11370. }
  11371. } else {
  11372. u32 major, minor;
  11373. if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
  11374. return;
  11375. major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
  11376. TG3_NVM_BCVER_MAJSFT;
  11377. minor = ver_offset & TG3_NVM_BCVER_MINMSK;
  11378. snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
  11379. "v%d.%02d", major, minor);
  11380. }
  11381. }
  11382. static void __devinit tg3_read_hwsb_ver(struct tg3 *tp)
  11383. {
  11384. u32 val, major, minor;
  11385. /* Use native endian representation */
  11386. if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
  11387. return;
  11388. major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
  11389. TG3_NVM_HWSB_CFG1_MAJSFT;
  11390. minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
  11391. TG3_NVM_HWSB_CFG1_MINSFT;
  11392. snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
  11393. }
  11394. static void __devinit tg3_read_sb_ver(struct tg3 *tp, u32 val)
  11395. {
  11396. u32 offset, major, minor, build;
  11397. strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
  11398. if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
  11399. return;
  11400. switch (val & TG3_EEPROM_SB_REVISION_MASK) {
  11401. case TG3_EEPROM_SB_REVISION_0:
  11402. offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
  11403. break;
  11404. case TG3_EEPROM_SB_REVISION_2:
  11405. offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
  11406. break;
  11407. case TG3_EEPROM_SB_REVISION_3:
  11408. offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
  11409. break;
  11410. case TG3_EEPROM_SB_REVISION_4:
  11411. offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
  11412. break;
  11413. case TG3_EEPROM_SB_REVISION_5:
  11414. offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
  11415. break;
  11416. case TG3_EEPROM_SB_REVISION_6:
  11417. offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
  11418. break;
  11419. default:
  11420. return;
  11421. }
  11422. if (tg3_nvram_read(tp, offset, &val))
  11423. return;
  11424. build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
  11425. TG3_EEPROM_SB_EDH_BLD_SHFT;
  11426. major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
  11427. TG3_EEPROM_SB_EDH_MAJ_SHFT;
  11428. minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
  11429. if (minor > 99 || build > 26)
  11430. return;
  11431. offset = strlen(tp->fw_ver);
  11432. snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
  11433. " v%d.%02d", major, minor);
  11434. if (build > 0) {
  11435. offset = strlen(tp->fw_ver);
  11436. if (offset < TG3_VER_SIZE - 1)
  11437. tp->fw_ver[offset] = 'a' + build - 1;
  11438. }
  11439. }
  11440. static void __devinit tg3_read_mgmtfw_ver(struct tg3 *tp)
  11441. {
  11442. u32 val, offset, start;
  11443. int i, vlen;
  11444. for (offset = TG3_NVM_DIR_START;
  11445. offset < TG3_NVM_DIR_END;
  11446. offset += TG3_NVM_DIRENT_SIZE) {
  11447. if (tg3_nvram_read(tp, offset, &val))
  11448. return;
  11449. if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
  11450. break;
  11451. }
  11452. if (offset == TG3_NVM_DIR_END)
  11453. return;
  11454. if (!tg3_flag(tp, 5705_PLUS))
  11455. start = 0x08000000;
  11456. else if (tg3_nvram_read(tp, offset - 4, &start))
  11457. return;
  11458. if (tg3_nvram_read(tp, offset + 4, &offset) ||
  11459. !tg3_fw_img_is_valid(tp, offset) ||
  11460. tg3_nvram_read(tp, offset + 8, &val))
  11461. return;
  11462. offset += val - start;
  11463. vlen = strlen(tp->fw_ver);
  11464. tp->fw_ver[vlen++] = ',';
  11465. tp->fw_ver[vlen++] = ' ';
  11466. for (i = 0; i < 4; i++) {
  11467. __be32 v;
  11468. if (tg3_nvram_read_be32(tp, offset, &v))
  11469. return;
  11470. offset += sizeof(v);
  11471. if (vlen > TG3_VER_SIZE - sizeof(v)) {
  11472. memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
  11473. break;
  11474. }
  11475. memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
  11476. vlen += sizeof(v);
  11477. }
  11478. }
  11479. static void __devinit tg3_read_dash_ver(struct tg3 *tp)
  11480. {
  11481. int vlen;
  11482. u32 apedata;
  11483. char *fwtype;
  11484. if (!tg3_flag(tp, ENABLE_APE) || !tg3_flag(tp, ENABLE_ASF))
  11485. return;
  11486. apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
  11487. if (apedata != APE_SEG_SIG_MAGIC)
  11488. return;
  11489. apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
  11490. if (!(apedata & APE_FW_STATUS_READY))
  11491. return;
  11492. apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
  11493. if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI) {
  11494. tg3_flag_set(tp, APE_HAS_NCSI);
  11495. fwtype = "NCSI";
  11496. } else {
  11497. fwtype = "DASH";
  11498. }
  11499. vlen = strlen(tp->fw_ver);
  11500. snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
  11501. fwtype,
  11502. (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
  11503. (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
  11504. (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
  11505. (apedata & APE_FW_VERSION_BLDMSK));
  11506. }
  11507. static void __devinit tg3_read_fw_ver(struct tg3 *tp)
  11508. {
  11509. u32 val;
  11510. bool vpd_vers = false;
  11511. if (tp->fw_ver[0] != 0)
  11512. vpd_vers = true;
  11513. if (tg3_flag(tp, NO_NVRAM)) {
  11514. strcat(tp->fw_ver, "sb");
  11515. return;
  11516. }
  11517. if (tg3_nvram_read(tp, 0, &val))
  11518. return;
  11519. if (val == TG3_EEPROM_MAGIC)
  11520. tg3_read_bc_ver(tp);
  11521. else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
  11522. tg3_read_sb_ver(tp, val);
  11523. else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
  11524. tg3_read_hwsb_ver(tp);
  11525. else
  11526. return;
  11527. if (vpd_vers)
  11528. goto done;
  11529. if (tg3_flag(tp, ENABLE_APE)) {
  11530. if (tg3_flag(tp, ENABLE_ASF))
  11531. tg3_read_dash_ver(tp);
  11532. } else if (tg3_flag(tp, ENABLE_ASF)) {
  11533. tg3_read_mgmtfw_ver(tp);
  11534. }
  11535. done:
  11536. tp->fw_ver[TG3_VER_SIZE - 1] = 0;
  11537. }
  11538. static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
  11539. {
  11540. if (tg3_flag(tp, LRG_PROD_RING_CAP))
  11541. return TG3_RX_RET_MAX_SIZE_5717;
  11542. else if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))
  11543. return TG3_RX_RET_MAX_SIZE_5700;
  11544. else
  11545. return TG3_RX_RET_MAX_SIZE_5705;
  11546. }
  11547. static DEFINE_PCI_DEVICE_TABLE(tg3_write_reorder_chipsets) = {
  11548. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
  11549. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
  11550. { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
  11551. { },
  11552. };
  11553. static struct pci_dev * __devinit tg3_find_peer(struct tg3 *tp)
  11554. {
  11555. struct pci_dev *peer;
  11556. unsigned int func, devnr = tp->pdev->devfn & ~7;
  11557. for (func = 0; func < 8; func++) {
  11558. peer = pci_get_slot(tp->pdev->bus, devnr | func);
  11559. if (peer && peer != tp->pdev)
  11560. break;
  11561. pci_dev_put(peer);
  11562. }
  11563. /* 5704 can be configured in single-port mode, set peer to
  11564. * tp->pdev in that case.
  11565. */
  11566. if (!peer) {
  11567. peer = tp->pdev;
  11568. return peer;
  11569. }
  11570. /*
  11571. * We don't need to keep the refcount elevated; there's no way
  11572. * to remove one half of this device without removing the other
  11573. */
  11574. pci_dev_put(peer);
  11575. return peer;
  11576. }
  11577. static void __devinit tg3_detect_asic_rev(struct tg3 *tp, u32 misc_ctrl_reg)
  11578. {
  11579. tp->pci_chip_rev_id = misc_ctrl_reg >> MISC_HOST_CTRL_CHIPREV_SHIFT;
  11580. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
  11581. u32 reg;
  11582. /* All devices that use the alternate
  11583. * ASIC REV location have a CPMU.
  11584. */
  11585. tg3_flag_set(tp, CPMU_PRESENT);
  11586. if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
  11587. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
  11588. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
  11589. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720)
  11590. reg = TG3PCI_GEN2_PRODID_ASICREV;
  11591. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
  11592. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
  11593. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
  11594. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
  11595. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
  11596. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
  11597. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762 ||
  11598. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766 ||
  11599. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782 ||
  11600. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
  11601. reg = TG3PCI_GEN15_PRODID_ASICREV;
  11602. else
  11603. reg = TG3PCI_PRODID_ASICREV;
  11604. pci_read_config_dword(tp->pdev, reg, &tp->pci_chip_rev_id);
  11605. }
  11606. /* Wrong chip ID in 5752 A0. This code can be removed later
  11607. * as A0 is not in production.
  11608. */
  11609. if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
  11610. tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
  11611. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
  11612. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
  11613. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
  11614. tg3_flag_set(tp, 5717_PLUS);
  11615. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 ||
  11616. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57766)
  11617. tg3_flag_set(tp, 57765_CLASS);
  11618. if (tg3_flag(tp, 57765_CLASS) || tg3_flag(tp, 5717_PLUS))
  11619. tg3_flag_set(tp, 57765_PLUS);
  11620. /* Intentionally exclude ASIC_REV_5906 */
  11621. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
  11622. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
  11623. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
  11624. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
  11625. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
  11626. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
  11627. tg3_flag(tp, 57765_PLUS))
  11628. tg3_flag_set(tp, 5755_PLUS);
  11629. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
  11630. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)
  11631. tg3_flag_set(tp, 5780_CLASS);
  11632. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
  11633. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
  11634. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
  11635. tg3_flag(tp, 5755_PLUS) ||
  11636. tg3_flag(tp, 5780_CLASS))
  11637. tg3_flag_set(tp, 5750_PLUS);
  11638. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
  11639. tg3_flag(tp, 5750_PLUS))
  11640. tg3_flag_set(tp, 5705_PLUS);
  11641. }
  11642. static int __devinit tg3_get_invariants(struct tg3 *tp)
  11643. {
  11644. u32 misc_ctrl_reg;
  11645. u32 pci_state_reg, grc_misc_cfg;
  11646. u32 val;
  11647. u16 pci_cmd;
  11648. int err;
  11649. /* Force memory write invalidate off. If we leave it on,
  11650. * then on 5700_BX chips we have to enable a workaround.
  11651. * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
  11652. * to match the cacheline size. The Broadcom driver have this
  11653. * workaround but turns MWI off all the times so never uses
  11654. * it. This seems to suggest that the workaround is insufficient.
  11655. */
  11656. pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
  11657. pci_cmd &= ~PCI_COMMAND_INVALIDATE;
  11658. pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
  11659. /* Important! -- Make sure register accesses are byteswapped
  11660. * correctly. Also, for those chips that require it, make
  11661. * sure that indirect register accesses are enabled before
  11662. * the first operation.
  11663. */
  11664. pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
  11665. &misc_ctrl_reg);
  11666. tp->misc_host_ctrl |= (misc_ctrl_reg &
  11667. MISC_HOST_CTRL_CHIPREV);
  11668. pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
  11669. tp->misc_host_ctrl);
  11670. tg3_detect_asic_rev(tp, misc_ctrl_reg);
  11671. /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
  11672. * we need to disable memory and use config. cycles
  11673. * only to access all registers. The 5702/03 chips
  11674. * can mistakenly decode the special cycles from the
  11675. * ICH chipsets as memory write cycles, causing corruption
  11676. * of register and memory space. Only certain ICH bridges
  11677. * will drive special cycles with non-zero data during the
  11678. * address phase which can fall within the 5703's address
  11679. * range. This is not an ICH bug as the PCI spec allows
  11680. * non-zero address during special cycles. However, only
  11681. * these ICH bridges are known to drive non-zero addresses
  11682. * during special cycles.
  11683. *
  11684. * Since special cycles do not cross PCI bridges, we only
  11685. * enable this workaround if the 5703 is on the secondary
  11686. * bus of these ICH bridges.
  11687. */
  11688. if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
  11689. (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
  11690. static struct tg3_dev_id {
  11691. u32 vendor;
  11692. u32 device;
  11693. u32 rev;
  11694. } ich_chipsets[] = {
  11695. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
  11696. PCI_ANY_ID },
  11697. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
  11698. PCI_ANY_ID },
  11699. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
  11700. 0xa },
  11701. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
  11702. PCI_ANY_ID },
  11703. { },
  11704. };
  11705. struct tg3_dev_id *pci_id = &ich_chipsets[0];
  11706. struct pci_dev *bridge = NULL;
  11707. while (pci_id->vendor != 0) {
  11708. bridge = pci_get_device(pci_id->vendor, pci_id->device,
  11709. bridge);
  11710. if (!bridge) {
  11711. pci_id++;
  11712. continue;
  11713. }
  11714. if (pci_id->rev != PCI_ANY_ID) {
  11715. if (bridge->revision > pci_id->rev)
  11716. continue;
  11717. }
  11718. if (bridge->subordinate &&
  11719. (bridge->subordinate->number ==
  11720. tp->pdev->bus->number)) {
  11721. tg3_flag_set(tp, ICH_WORKAROUND);
  11722. pci_dev_put(bridge);
  11723. break;
  11724. }
  11725. }
  11726. }
  11727. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
  11728. static struct tg3_dev_id {
  11729. u32 vendor;
  11730. u32 device;
  11731. } bridge_chipsets[] = {
  11732. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
  11733. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
  11734. { },
  11735. };
  11736. struct tg3_dev_id *pci_id = &bridge_chipsets[0];
  11737. struct pci_dev *bridge = NULL;
  11738. while (pci_id->vendor != 0) {
  11739. bridge = pci_get_device(pci_id->vendor,
  11740. pci_id->device,
  11741. bridge);
  11742. if (!bridge) {
  11743. pci_id++;
  11744. continue;
  11745. }
  11746. if (bridge->subordinate &&
  11747. (bridge->subordinate->number <=
  11748. tp->pdev->bus->number) &&
  11749. (bridge->subordinate->subordinate >=
  11750. tp->pdev->bus->number)) {
  11751. tg3_flag_set(tp, 5701_DMA_BUG);
  11752. pci_dev_put(bridge);
  11753. break;
  11754. }
  11755. }
  11756. }
  11757. /* The EPB bridge inside 5714, 5715, and 5780 cannot support
  11758. * DMA addresses > 40-bit. This bridge may have other additional
  11759. * 57xx devices behind it in some 4-port NIC designs for example.
  11760. * Any tg3 device found behind the bridge will also need the 40-bit
  11761. * DMA workaround.
  11762. */
  11763. if (tg3_flag(tp, 5780_CLASS)) {
  11764. tg3_flag_set(tp, 40BIT_DMA_BUG);
  11765. tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
  11766. } else {
  11767. struct pci_dev *bridge = NULL;
  11768. do {
  11769. bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
  11770. PCI_DEVICE_ID_SERVERWORKS_EPB,
  11771. bridge);
  11772. if (bridge && bridge->subordinate &&
  11773. (bridge->subordinate->number <=
  11774. tp->pdev->bus->number) &&
  11775. (bridge->subordinate->subordinate >=
  11776. tp->pdev->bus->number)) {
  11777. tg3_flag_set(tp, 40BIT_DMA_BUG);
  11778. pci_dev_put(bridge);
  11779. break;
  11780. }
  11781. } while (bridge);
  11782. }
  11783. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
  11784. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)
  11785. tp->pdev_peer = tg3_find_peer(tp);
  11786. /* Determine TSO capabilities */
  11787. if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0)
  11788. ; /* Do nothing. HW bug. */
  11789. else if (tg3_flag(tp, 57765_PLUS))
  11790. tg3_flag_set(tp, HW_TSO_3);
  11791. else if (tg3_flag(tp, 5755_PLUS) ||
  11792. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
  11793. tg3_flag_set(tp, HW_TSO_2);
  11794. else if (tg3_flag(tp, 5750_PLUS)) {
  11795. tg3_flag_set(tp, HW_TSO_1);
  11796. tg3_flag_set(tp, TSO_BUG);
  11797. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 &&
  11798. tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
  11799. tg3_flag_clear(tp, TSO_BUG);
  11800. } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
  11801. GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
  11802. tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
  11803. tg3_flag_set(tp, TSO_BUG);
  11804. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
  11805. tp->fw_needed = FIRMWARE_TG3TSO5;
  11806. else
  11807. tp->fw_needed = FIRMWARE_TG3TSO;
  11808. }
  11809. /* Selectively allow TSO based on operating conditions */
  11810. if (tg3_flag(tp, HW_TSO_1) ||
  11811. tg3_flag(tp, HW_TSO_2) ||
  11812. tg3_flag(tp, HW_TSO_3) ||
  11813. tp->fw_needed) {
  11814. /* For firmware TSO, assume ASF is disabled.
  11815. * We'll disable TSO later if we discover ASF
  11816. * is enabled in tg3_get_eeprom_hw_cfg().
  11817. */
  11818. tg3_flag_set(tp, TSO_CAPABLE);
  11819. } else {
  11820. tg3_flag_clear(tp, TSO_CAPABLE);
  11821. tg3_flag_clear(tp, TSO_BUG);
  11822. tp->fw_needed = NULL;
  11823. }
  11824. if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
  11825. tp->fw_needed = FIRMWARE_TG3;
  11826. tp->irq_max = 1;
  11827. if (tg3_flag(tp, 5750_PLUS)) {
  11828. tg3_flag_set(tp, SUPPORT_MSI);
  11829. if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
  11830. GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
  11831. (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
  11832. tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
  11833. tp->pdev_peer == tp->pdev))
  11834. tg3_flag_clear(tp, SUPPORT_MSI);
  11835. if (tg3_flag(tp, 5755_PLUS) ||
  11836. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
  11837. tg3_flag_set(tp, 1SHOT_MSI);
  11838. }
  11839. if (tg3_flag(tp, 57765_PLUS)) {
  11840. tg3_flag_set(tp, SUPPORT_MSIX);
  11841. tp->irq_max = TG3_IRQ_MAX_VECS;
  11842. tg3_rss_init_dflt_indir_tbl(tp);
  11843. }
  11844. }
  11845. if (tg3_flag(tp, 5755_PLUS) ||
  11846. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
  11847. tg3_flag_set(tp, SHORT_DMA_BUG);
  11848. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
  11849. tp->dma_limit = TG3_TX_BD_DMA_MAX_4K;
  11850. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
  11851. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
  11852. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
  11853. tg3_flag_set(tp, LRG_PROD_RING_CAP);
  11854. if (tg3_flag(tp, 57765_PLUS) &&
  11855. tp->pci_chip_rev_id != CHIPREV_ID_5719_A0)
  11856. tg3_flag_set(tp, USE_JUMBO_BDFLAG);
  11857. if (!tg3_flag(tp, 5705_PLUS) ||
  11858. tg3_flag(tp, 5780_CLASS) ||
  11859. tg3_flag(tp, USE_JUMBO_BDFLAG))
  11860. tg3_flag_set(tp, JUMBO_CAPABLE);
  11861. pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
  11862. &pci_state_reg);
  11863. if (pci_is_pcie(tp->pdev)) {
  11864. u16 lnkctl;
  11865. tg3_flag_set(tp, PCI_EXPRESS);
  11866. pci_read_config_word(tp->pdev,
  11867. pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
  11868. &lnkctl);
  11869. if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
  11870. if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
  11871. ASIC_REV_5906) {
  11872. tg3_flag_clear(tp, HW_TSO_2);
  11873. tg3_flag_clear(tp, TSO_CAPABLE);
  11874. }
  11875. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
  11876. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
  11877. tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
  11878. tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
  11879. tg3_flag_set(tp, CLKREQ_BUG);
  11880. } else if (tp->pci_chip_rev_id == CHIPREV_ID_5717_A0) {
  11881. tg3_flag_set(tp, L1PLLPD_EN);
  11882. }
  11883. } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
  11884. /* BCM5785 devices are effectively PCIe devices, and should
  11885. * follow PCIe codepaths, but do not have a PCIe capabilities
  11886. * section.
  11887. */
  11888. tg3_flag_set(tp, PCI_EXPRESS);
  11889. } else if (!tg3_flag(tp, 5705_PLUS) ||
  11890. tg3_flag(tp, 5780_CLASS)) {
  11891. tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
  11892. if (!tp->pcix_cap) {
  11893. dev_err(&tp->pdev->dev,
  11894. "Cannot find PCI-X capability, aborting\n");
  11895. return -EIO;
  11896. }
  11897. if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
  11898. tg3_flag_set(tp, PCIX_MODE);
  11899. }
  11900. /* If we have an AMD 762 or VIA K8T800 chipset, write
  11901. * reordering to the mailbox registers done by the host
  11902. * controller can cause major troubles. We read back from
  11903. * every mailbox register write to force the writes to be
  11904. * posted to the chip in order.
  11905. */
  11906. if (pci_dev_present(tg3_write_reorder_chipsets) &&
  11907. !tg3_flag(tp, PCI_EXPRESS))
  11908. tg3_flag_set(tp, MBOX_WRITE_REORDER);
  11909. pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
  11910. &tp->pci_cacheline_sz);
  11911. pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
  11912. &tp->pci_lat_timer);
  11913. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
  11914. tp->pci_lat_timer < 64) {
  11915. tp->pci_lat_timer = 64;
  11916. pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
  11917. tp->pci_lat_timer);
  11918. }
  11919. /* Important! -- It is critical that the PCI-X hw workaround
  11920. * situation is decided before the first MMIO register access.
  11921. */
  11922. if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
  11923. /* 5700 BX chips need to have their TX producer index
  11924. * mailboxes written twice to workaround a bug.
  11925. */
  11926. tg3_flag_set(tp, TXD_MBOX_HWBUG);
  11927. /* If we are in PCI-X mode, enable register write workaround.
  11928. *
  11929. * The workaround is to use indirect register accesses
  11930. * for all chip writes not to mailbox registers.
  11931. */
  11932. if (tg3_flag(tp, PCIX_MODE)) {
  11933. u32 pm_reg;
  11934. tg3_flag_set(tp, PCIX_TARGET_HWBUG);
  11935. /* The chip can have it's power management PCI config
  11936. * space registers clobbered due to this bug.
  11937. * So explicitly force the chip into D0 here.
  11938. */
  11939. pci_read_config_dword(tp->pdev,
  11940. tp->pm_cap + PCI_PM_CTRL,
  11941. &pm_reg);
  11942. pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
  11943. pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
  11944. pci_write_config_dword(tp->pdev,
  11945. tp->pm_cap + PCI_PM_CTRL,
  11946. pm_reg);
  11947. /* Also, force SERR#/PERR# in PCI command. */
  11948. pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
  11949. pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
  11950. pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
  11951. }
  11952. }
  11953. if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
  11954. tg3_flag_set(tp, PCI_HIGH_SPEED);
  11955. if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
  11956. tg3_flag_set(tp, PCI_32BIT);
  11957. /* Chip-specific fixup from Broadcom driver */
  11958. if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
  11959. (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
  11960. pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
  11961. pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
  11962. }
  11963. /* Default fast path register access methods */
  11964. tp->read32 = tg3_read32;
  11965. tp->write32 = tg3_write32;
  11966. tp->read32_mbox = tg3_read32;
  11967. tp->write32_mbox = tg3_write32;
  11968. tp->write32_tx_mbox = tg3_write32;
  11969. tp->write32_rx_mbox = tg3_write32;
  11970. /* Various workaround register access methods */
  11971. if (tg3_flag(tp, PCIX_TARGET_HWBUG))
  11972. tp->write32 = tg3_write_indirect_reg32;
  11973. else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
  11974. (tg3_flag(tp, PCI_EXPRESS) &&
  11975. tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
  11976. /*
  11977. * Back to back register writes can cause problems on these
  11978. * chips, the workaround is to read back all reg writes
  11979. * except those to mailbox regs.
  11980. *
  11981. * See tg3_write_indirect_reg32().
  11982. */
  11983. tp->write32 = tg3_write_flush_reg32;
  11984. }
  11985. if (tg3_flag(tp, TXD_MBOX_HWBUG) || tg3_flag(tp, MBOX_WRITE_REORDER)) {
  11986. tp->write32_tx_mbox = tg3_write32_tx_mbox;
  11987. if (tg3_flag(tp, MBOX_WRITE_REORDER))
  11988. tp->write32_rx_mbox = tg3_write_flush_reg32;
  11989. }
  11990. if (tg3_flag(tp, ICH_WORKAROUND)) {
  11991. tp->read32 = tg3_read_indirect_reg32;
  11992. tp->write32 = tg3_write_indirect_reg32;
  11993. tp->read32_mbox = tg3_read_indirect_mbox;
  11994. tp->write32_mbox = tg3_write_indirect_mbox;
  11995. tp->write32_tx_mbox = tg3_write_indirect_mbox;
  11996. tp->write32_rx_mbox = tg3_write_indirect_mbox;
  11997. iounmap(tp->regs);
  11998. tp->regs = NULL;
  11999. pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
  12000. pci_cmd &= ~PCI_COMMAND_MEMORY;
  12001. pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
  12002. }
  12003. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
  12004. tp->read32_mbox = tg3_read32_mbox_5906;
  12005. tp->write32_mbox = tg3_write32_mbox_5906;
  12006. tp->write32_tx_mbox = tg3_write32_mbox_5906;
  12007. tp->write32_rx_mbox = tg3_write32_mbox_5906;
  12008. }
  12009. if (tp->write32 == tg3_write_indirect_reg32 ||
  12010. (tg3_flag(tp, PCIX_MODE) &&
  12011. (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
  12012. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
  12013. tg3_flag_set(tp, SRAM_USE_CONFIG);
  12014. /* The memory arbiter has to be enabled in order for SRAM accesses
  12015. * to succeed. Normally on powerup the tg3 chip firmware will make
  12016. * sure it is enabled, but other entities such as system netboot
  12017. * code might disable it.
  12018. */
  12019. val = tr32(MEMARB_MODE);
  12020. tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
  12021. tp->pci_fn = PCI_FUNC(tp->pdev->devfn) & 3;
  12022. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
  12023. tg3_flag(tp, 5780_CLASS)) {
  12024. if (tg3_flag(tp, PCIX_MODE)) {
  12025. pci_read_config_dword(tp->pdev,
  12026. tp->pcix_cap + PCI_X_STATUS,
  12027. &val);
  12028. tp->pci_fn = val & 0x7;
  12029. }
  12030. } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
  12031. tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
  12032. if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) ==
  12033. NIC_SRAM_CPMUSTAT_SIG) {
  12034. tp->pci_fn = val & TG3_CPMU_STATUS_FMSK_5717;
  12035. tp->pci_fn = tp->pci_fn ? 1 : 0;
  12036. }
  12037. } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
  12038. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
  12039. tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
  12040. if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) ==
  12041. NIC_SRAM_CPMUSTAT_SIG) {
  12042. tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5719) >>
  12043. TG3_CPMU_STATUS_FSHFT_5719;
  12044. }
  12045. }
  12046. /* Get eeprom hw config before calling tg3_set_power_state().
  12047. * In particular, the TG3_FLAG_IS_NIC flag must be
  12048. * determined before calling tg3_set_power_state() so that
  12049. * we know whether or not to switch out of Vaux power.
  12050. * When the flag is set, it means that GPIO1 is used for eeprom
  12051. * write protect and also implies that it is a LOM where GPIOs
  12052. * are not used to switch power.
  12053. */
  12054. tg3_get_eeprom_hw_cfg(tp);
  12055. if (tp->fw_needed && tg3_flag(tp, ENABLE_ASF)) {
  12056. tg3_flag_clear(tp, TSO_CAPABLE);
  12057. tg3_flag_clear(tp, TSO_BUG);
  12058. tp->fw_needed = NULL;
  12059. }
  12060. if (tg3_flag(tp, ENABLE_APE)) {
  12061. /* Allow reads and writes to the
  12062. * APE register and memory space.
  12063. */
  12064. pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
  12065. PCISTATE_ALLOW_APE_SHMEM_WR |
  12066. PCISTATE_ALLOW_APE_PSPACE_WR;
  12067. pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
  12068. pci_state_reg);
  12069. tg3_ape_lock_init(tp);
  12070. }
  12071. /* Set up tp->grc_local_ctrl before calling
  12072. * tg3_pwrsrc_switch_to_vmain(). GPIO1 driven high
  12073. * will bring 5700's external PHY out of reset.
  12074. * It is also used as eeprom write protect on LOMs.
  12075. */
  12076. tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
  12077. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
  12078. tg3_flag(tp, EEPROM_WRITE_PROT))
  12079. tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
  12080. GRC_LCLCTRL_GPIO_OUTPUT1);
  12081. /* Unused GPIO3 must be driven as output on 5752 because there
  12082. * are no pull-up resistors on unused GPIO pins.
  12083. */
  12084. else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
  12085. tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
  12086. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
  12087. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
  12088. tg3_flag(tp, 57765_CLASS))
  12089. tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
  12090. if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
  12091. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
  12092. /* Turn off the debug UART. */
  12093. tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
  12094. if (tg3_flag(tp, IS_NIC))
  12095. /* Keep VMain power. */
  12096. tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
  12097. GRC_LCLCTRL_GPIO_OUTPUT0;
  12098. }
  12099. /* Switch out of Vaux if it is a NIC */
  12100. tg3_pwrsrc_switch_to_vmain(tp);
  12101. /* Derive initial jumbo mode from MTU assigned in
  12102. * ether_setup() via the alloc_etherdev() call
  12103. */
  12104. if (tp->dev->mtu > ETH_DATA_LEN && !tg3_flag(tp, 5780_CLASS))
  12105. tg3_flag_set(tp, JUMBO_RING_ENABLE);
  12106. /* Determine WakeOnLan speed to use. */
  12107. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
  12108. tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
  12109. tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
  12110. tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
  12111. tg3_flag_clear(tp, WOL_SPEED_100MB);
  12112. } else {
  12113. tg3_flag_set(tp, WOL_SPEED_100MB);
  12114. }
  12115. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
  12116. tp->phy_flags |= TG3_PHYFLG_IS_FET;
  12117. /* A few boards don't want Ethernet@WireSpeed phy feature */
  12118. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
  12119. (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
  12120. (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
  12121. (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
  12122. (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
  12123. (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
  12124. tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
  12125. if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
  12126. GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
  12127. tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
  12128. if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
  12129. tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
  12130. if (tg3_flag(tp, 5705_PLUS) &&
  12131. !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
  12132. GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
  12133. GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780 &&
  12134. !tg3_flag(tp, 57765_PLUS)) {
  12135. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
  12136. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
  12137. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
  12138. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
  12139. if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
  12140. tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
  12141. tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
  12142. if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
  12143. tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
  12144. } else
  12145. tp->phy_flags |= TG3_PHYFLG_BER_BUG;
  12146. }
  12147. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
  12148. GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
  12149. tp->phy_otp = tg3_read_otp_phycfg(tp);
  12150. if (tp->phy_otp == 0)
  12151. tp->phy_otp = TG3_OTP_DEFAULT;
  12152. }
  12153. if (tg3_flag(tp, CPMU_PRESENT))
  12154. tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
  12155. else
  12156. tp->mi_mode = MAC_MI_MODE_BASE;
  12157. tp->coalesce_mode = 0;
  12158. if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
  12159. GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
  12160. tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
  12161. /* Set these bits to enable statistics workaround. */
  12162. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
  12163. tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
  12164. tp->pci_chip_rev_id == CHIPREV_ID_5720_A0) {
  12165. tp->coalesce_mode |= HOSTCC_MODE_ATTN;
  12166. tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
  12167. }
  12168. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
  12169. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
  12170. tg3_flag_set(tp, USE_PHYLIB);
  12171. err = tg3_mdio_init(tp);
  12172. if (err)
  12173. return err;
  12174. /* Initialize data/descriptor byte/word swapping. */
  12175. val = tr32(GRC_MODE);
  12176. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
  12177. val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
  12178. GRC_MODE_WORD_SWAP_B2HRX_DATA |
  12179. GRC_MODE_B2HRX_ENABLE |
  12180. GRC_MODE_HTX2B_ENABLE |
  12181. GRC_MODE_HOST_STACKUP);
  12182. else
  12183. val &= GRC_MODE_HOST_STACKUP;
  12184. tw32(GRC_MODE, val | tp->grc_mode);
  12185. tg3_switch_clocks(tp);
  12186. /* Clear this out for sanity. */
  12187. tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
  12188. pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
  12189. &pci_state_reg);
  12190. if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
  12191. !tg3_flag(tp, PCIX_TARGET_HWBUG)) {
  12192. u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
  12193. if (chiprevid == CHIPREV_ID_5701_A0 ||
  12194. chiprevid == CHIPREV_ID_5701_B0 ||
  12195. chiprevid == CHIPREV_ID_5701_B2 ||
  12196. chiprevid == CHIPREV_ID_5701_B5) {
  12197. void __iomem *sram_base;
  12198. /* Write some dummy words into the SRAM status block
  12199. * area, see if it reads back correctly. If the return
  12200. * value is bad, force enable the PCIX workaround.
  12201. */
  12202. sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
  12203. writel(0x00000000, sram_base);
  12204. writel(0x00000000, sram_base + 4);
  12205. writel(0xffffffff, sram_base + 4);
  12206. if (readl(sram_base) != 0x00000000)
  12207. tg3_flag_set(tp, PCIX_TARGET_HWBUG);
  12208. }
  12209. }
  12210. udelay(50);
  12211. tg3_nvram_init(tp);
  12212. grc_misc_cfg = tr32(GRC_MISC_CFG);
  12213. grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
  12214. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
  12215. (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
  12216. grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
  12217. tg3_flag_set(tp, IS_5788);
  12218. if (!tg3_flag(tp, IS_5788) &&
  12219. GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
  12220. tg3_flag_set(tp, TAGGED_STATUS);
  12221. if (tg3_flag(tp, TAGGED_STATUS)) {
  12222. tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
  12223. HOSTCC_MODE_CLRTICK_TXBD);
  12224. tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
  12225. pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
  12226. tp->misc_host_ctrl);
  12227. }
  12228. /* Preserve the APE MAC_MODE bits */
  12229. if (tg3_flag(tp, ENABLE_APE))
  12230. tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
  12231. else
  12232. tp->mac_mode = 0;
  12233. /* these are limited to 10/100 only */
  12234. if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
  12235. (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
  12236. (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
  12237. tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
  12238. (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901 ||
  12239. tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901_2 ||
  12240. tp->pdev->device == PCI_DEVICE_ID_TIGON3_5705F)) ||
  12241. (tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
  12242. (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5751F ||
  12243. tp->pdev->device == PCI_DEVICE_ID_TIGON3_5753F ||
  12244. tp->pdev->device == PCI_DEVICE_ID_TIGON3_5787F)) ||
  12245. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790 ||
  12246. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
  12247. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
  12248. (tp->phy_flags & TG3_PHYFLG_IS_FET))
  12249. tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
  12250. err = tg3_phy_probe(tp);
  12251. if (err) {
  12252. dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
  12253. /* ... but do not return immediately ... */
  12254. tg3_mdio_fini(tp);
  12255. }
  12256. tg3_read_vpd(tp);
  12257. tg3_read_fw_ver(tp);
  12258. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
  12259. tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
  12260. } else {
  12261. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
  12262. tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
  12263. else
  12264. tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
  12265. }
  12266. /* 5700 {AX,BX} chips have a broken status block link
  12267. * change bit implementation, so we must use the
  12268. * status register in those cases.
  12269. */
  12270. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
  12271. tg3_flag_set(tp, USE_LINKCHG_REG);
  12272. else
  12273. tg3_flag_clear(tp, USE_LINKCHG_REG);
  12274. /* The led_ctrl is set during tg3_phy_probe, here we might
  12275. * have to force the link status polling mechanism based
  12276. * upon subsystem IDs.
  12277. */
  12278. if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
  12279. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
  12280. !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
  12281. tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
  12282. tg3_flag_set(tp, USE_LINKCHG_REG);
  12283. }
  12284. /* For all SERDES we poll the MAC status register. */
  12285. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
  12286. tg3_flag_set(tp, POLL_SERDES);
  12287. else
  12288. tg3_flag_clear(tp, POLL_SERDES);
  12289. tp->rx_offset = NET_SKB_PAD + NET_IP_ALIGN;
  12290. tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
  12291. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
  12292. tg3_flag(tp, PCIX_MODE)) {
  12293. tp->rx_offset = NET_SKB_PAD;
  12294. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  12295. tp->rx_copy_thresh = ~(u16)0;
  12296. #endif
  12297. }
  12298. tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
  12299. tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
  12300. tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
  12301. tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
  12302. /* Increment the rx prod index on the rx std ring by at most
  12303. * 8 for these chips to workaround hw errata.
  12304. */
  12305. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
  12306. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
  12307. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
  12308. tp->rx_std_max_post = 8;
  12309. if (tg3_flag(tp, ASPM_WORKAROUND))
  12310. tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
  12311. PCIE_PWR_MGMT_L1_THRESH_MSK;
  12312. return err;
  12313. }
  12314. #ifdef CONFIG_SPARC
  12315. static int __devinit tg3_get_macaddr_sparc(struct tg3 *tp)
  12316. {
  12317. struct net_device *dev = tp->dev;
  12318. struct pci_dev *pdev = tp->pdev;
  12319. struct device_node *dp = pci_device_to_OF_node(pdev);
  12320. const unsigned char *addr;
  12321. int len;
  12322. addr = of_get_property(dp, "local-mac-address", &len);
  12323. if (addr && len == 6) {
  12324. memcpy(dev->dev_addr, addr, 6);
  12325. memcpy(dev->perm_addr, dev->dev_addr, 6);
  12326. return 0;
  12327. }
  12328. return -ENODEV;
  12329. }
  12330. static int __devinit tg3_get_default_macaddr_sparc(struct tg3 *tp)
  12331. {
  12332. struct net_device *dev = tp->dev;
  12333. memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
  12334. memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
  12335. return 0;
  12336. }
  12337. #endif
  12338. static int __devinit tg3_get_device_address(struct tg3 *tp)
  12339. {
  12340. struct net_device *dev = tp->dev;
  12341. u32 hi, lo, mac_offset;
  12342. int addr_ok = 0;
  12343. #ifdef CONFIG_SPARC
  12344. if (!tg3_get_macaddr_sparc(tp))
  12345. return 0;
  12346. #endif
  12347. mac_offset = 0x7c;
  12348. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
  12349. tg3_flag(tp, 5780_CLASS)) {
  12350. if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
  12351. mac_offset = 0xcc;
  12352. if (tg3_nvram_lock(tp))
  12353. tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
  12354. else
  12355. tg3_nvram_unlock(tp);
  12356. } else if (tg3_flag(tp, 5717_PLUS)) {
  12357. if (tp->pci_fn & 1)
  12358. mac_offset = 0xcc;
  12359. if (tp->pci_fn > 1)
  12360. mac_offset += 0x18c;
  12361. } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
  12362. mac_offset = 0x10;
  12363. /* First try to get it from MAC address mailbox. */
  12364. tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
  12365. if ((hi >> 16) == 0x484b) {
  12366. dev->dev_addr[0] = (hi >> 8) & 0xff;
  12367. dev->dev_addr[1] = (hi >> 0) & 0xff;
  12368. tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
  12369. dev->dev_addr[2] = (lo >> 24) & 0xff;
  12370. dev->dev_addr[3] = (lo >> 16) & 0xff;
  12371. dev->dev_addr[4] = (lo >> 8) & 0xff;
  12372. dev->dev_addr[5] = (lo >> 0) & 0xff;
  12373. /* Some old bootcode may report a 0 MAC address in SRAM */
  12374. addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
  12375. }
  12376. if (!addr_ok) {
  12377. /* Next, try NVRAM. */
  12378. if (!tg3_flag(tp, NO_NVRAM) &&
  12379. !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
  12380. !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
  12381. memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
  12382. memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
  12383. }
  12384. /* Finally just fetch it out of the MAC control regs. */
  12385. else {
  12386. hi = tr32(MAC_ADDR_0_HIGH);
  12387. lo = tr32(MAC_ADDR_0_LOW);
  12388. dev->dev_addr[5] = lo & 0xff;
  12389. dev->dev_addr[4] = (lo >> 8) & 0xff;
  12390. dev->dev_addr[3] = (lo >> 16) & 0xff;
  12391. dev->dev_addr[2] = (lo >> 24) & 0xff;
  12392. dev->dev_addr[1] = hi & 0xff;
  12393. dev->dev_addr[0] = (hi >> 8) & 0xff;
  12394. }
  12395. }
  12396. if (!is_valid_ether_addr(&dev->dev_addr[0])) {
  12397. #ifdef CONFIG_SPARC
  12398. if (!tg3_get_default_macaddr_sparc(tp))
  12399. return 0;
  12400. #endif
  12401. return -EINVAL;
  12402. }
  12403. memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
  12404. return 0;
  12405. }
  12406. #define BOUNDARY_SINGLE_CACHELINE 1
  12407. #define BOUNDARY_MULTI_CACHELINE 2
  12408. static u32 __devinit tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
  12409. {
  12410. int cacheline_size;
  12411. u8 byte;
  12412. int goal;
  12413. pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
  12414. if (byte == 0)
  12415. cacheline_size = 1024;
  12416. else
  12417. cacheline_size = (int) byte * 4;
  12418. /* On 5703 and later chips, the boundary bits have no
  12419. * effect.
  12420. */
  12421. if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
  12422. GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
  12423. !tg3_flag(tp, PCI_EXPRESS))
  12424. goto out;
  12425. #if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
  12426. goal = BOUNDARY_MULTI_CACHELINE;
  12427. #else
  12428. #if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
  12429. goal = BOUNDARY_SINGLE_CACHELINE;
  12430. #else
  12431. goal = 0;
  12432. #endif
  12433. #endif
  12434. if (tg3_flag(tp, 57765_PLUS)) {
  12435. val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
  12436. goto out;
  12437. }
  12438. if (!goal)
  12439. goto out;
  12440. /* PCI controllers on most RISC systems tend to disconnect
  12441. * when a device tries to burst across a cache-line boundary.
  12442. * Therefore, letting tg3 do so just wastes PCI bandwidth.
  12443. *
  12444. * Unfortunately, for PCI-E there are only limited
  12445. * write-side controls for this, and thus for reads
  12446. * we will still get the disconnects. We'll also waste
  12447. * these PCI cycles for both read and write for chips
  12448. * other than 5700 and 5701 which do not implement the
  12449. * boundary bits.
  12450. */
  12451. if (tg3_flag(tp, PCIX_MODE) && !tg3_flag(tp, PCI_EXPRESS)) {
  12452. switch (cacheline_size) {
  12453. case 16:
  12454. case 32:
  12455. case 64:
  12456. case 128:
  12457. if (goal == BOUNDARY_SINGLE_CACHELINE) {
  12458. val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
  12459. DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
  12460. } else {
  12461. val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
  12462. DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
  12463. }
  12464. break;
  12465. case 256:
  12466. val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
  12467. DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
  12468. break;
  12469. default:
  12470. val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
  12471. DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
  12472. break;
  12473. }
  12474. } else if (tg3_flag(tp, PCI_EXPRESS)) {
  12475. switch (cacheline_size) {
  12476. case 16:
  12477. case 32:
  12478. case 64:
  12479. if (goal == BOUNDARY_SINGLE_CACHELINE) {
  12480. val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
  12481. val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
  12482. break;
  12483. }
  12484. /* fallthrough */
  12485. case 128:
  12486. default:
  12487. val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
  12488. val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
  12489. break;
  12490. }
  12491. } else {
  12492. switch (cacheline_size) {
  12493. case 16:
  12494. if (goal == BOUNDARY_SINGLE_CACHELINE) {
  12495. val |= (DMA_RWCTRL_READ_BNDRY_16 |
  12496. DMA_RWCTRL_WRITE_BNDRY_16);
  12497. break;
  12498. }
  12499. /* fallthrough */
  12500. case 32:
  12501. if (goal == BOUNDARY_SINGLE_CACHELINE) {
  12502. val |= (DMA_RWCTRL_READ_BNDRY_32 |
  12503. DMA_RWCTRL_WRITE_BNDRY_32);
  12504. break;
  12505. }
  12506. /* fallthrough */
  12507. case 64:
  12508. if (goal == BOUNDARY_SINGLE_CACHELINE) {
  12509. val |= (DMA_RWCTRL_READ_BNDRY_64 |
  12510. DMA_RWCTRL_WRITE_BNDRY_64);
  12511. break;
  12512. }
  12513. /* fallthrough */
  12514. case 128:
  12515. if (goal == BOUNDARY_SINGLE_CACHELINE) {
  12516. val |= (DMA_RWCTRL_READ_BNDRY_128 |
  12517. DMA_RWCTRL_WRITE_BNDRY_128);
  12518. break;
  12519. }
  12520. /* fallthrough */
  12521. case 256:
  12522. val |= (DMA_RWCTRL_READ_BNDRY_256 |
  12523. DMA_RWCTRL_WRITE_BNDRY_256);
  12524. break;
  12525. case 512:
  12526. val |= (DMA_RWCTRL_READ_BNDRY_512 |
  12527. DMA_RWCTRL_WRITE_BNDRY_512);
  12528. break;
  12529. case 1024:
  12530. default:
  12531. val |= (DMA_RWCTRL_READ_BNDRY_1024 |
  12532. DMA_RWCTRL_WRITE_BNDRY_1024);
  12533. break;
  12534. }
  12535. }
  12536. out:
  12537. return val;
  12538. }
  12539. static int __devinit tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma, int size, int to_device)
  12540. {
  12541. struct tg3_internal_buffer_desc test_desc;
  12542. u32 sram_dma_descs;
  12543. int i, ret;
  12544. sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
  12545. tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
  12546. tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
  12547. tw32(RDMAC_STATUS, 0);
  12548. tw32(WDMAC_STATUS, 0);
  12549. tw32(BUFMGR_MODE, 0);
  12550. tw32(FTQ_RESET, 0);
  12551. test_desc.addr_hi = ((u64) buf_dma) >> 32;
  12552. test_desc.addr_lo = buf_dma & 0xffffffff;
  12553. test_desc.nic_mbuf = 0x00002100;
  12554. test_desc.len = size;
  12555. /*
  12556. * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
  12557. * the *second* time the tg3 driver was getting loaded after an
  12558. * initial scan.
  12559. *
  12560. * Broadcom tells me:
  12561. * ...the DMA engine is connected to the GRC block and a DMA
  12562. * reset may affect the GRC block in some unpredictable way...
  12563. * The behavior of resets to individual blocks has not been tested.
  12564. *
  12565. * Broadcom noted the GRC reset will also reset all sub-components.
  12566. */
  12567. if (to_device) {
  12568. test_desc.cqid_sqid = (13 << 8) | 2;
  12569. tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
  12570. udelay(40);
  12571. } else {
  12572. test_desc.cqid_sqid = (16 << 8) | 7;
  12573. tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
  12574. udelay(40);
  12575. }
  12576. test_desc.flags = 0x00000005;
  12577. for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
  12578. u32 val;
  12579. val = *(((u32 *)&test_desc) + i);
  12580. pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
  12581. sram_dma_descs + (i * sizeof(u32)));
  12582. pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
  12583. }
  12584. pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
  12585. if (to_device)
  12586. tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
  12587. else
  12588. tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
  12589. ret = -ENODEV;
  12590. for (i = 0; i < 40; i++) {
  12591. u32 val;
  12592. if (to_device)
  12593. val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
  12594. else
  12595. val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
  12596. if ((val & 0xffff) == sram_dma_descs) {
  12597. ret = 0;
  12598. break;
  12599. }
  12600. udelay(100);
  12601. }
  12602. return ret;
  12603. }
  12604. #define TEST_BUFFER_SIZE 0x2000
  12605. static DEFINE_PCI_DEVICE_TABLE(tg3_dma_wait_state_chipsets) = {
  12606. { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
  12607. { },
  12608. };
  12609. static int __devinit tg3_test_dma(struct tg3 *tp)
  12610. {
  12611. dma_addr_t buf_dma;
  12612. u32 *buf, saved_dma_rwctrl;
  12613. int ret = 0;
  12614. buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
  12615. &buf_dma, GFP_KERNEL);
  12616. if (!buf) {
  12617. ret = -ENOMEM;
  12618. goto out_nofree;
  12619. }
  12620. tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
  12621. (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
  12622. tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
  12623. if (tg3_flag(tp, 57765_PLUS))
  12624. goto out;
  12625. if (tg3_flag(tp, PCI_EXPRESS)) {
  12626. /* DMA read watermark not used on PCIE */
  12627. tp->dma_rwctrl |= 0x00180000;
  12628. } else if (!tg3_flag(tp, PCIX_MODE)) {
  12629. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
  12630. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
  12631. tp->dma_rwctrl |= 0x003f0000;
  12632. else
  12633. tp->dma_rwctrl |= 0x003f000f;
  12634. } else {
  12635. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
  12636. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
  12637. u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
  12638. u32 read_water = 0x7;
  12639. /* If the 5704 is behind the EPB bridge, we can
  12640. * do the less restrictive ONE_DMA workaround for
  12641. * better performance.
  12642. */
  12643. if (tg3_flag(tp, 40BIT_DMA_BUG) &&
  12644. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
  12645. tp->dma_rwctrl |= 0x8000;
  12646. else if (ccval == 0x6 || ccval == 0x7)
  12647. tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
  12648. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
  12649. read_water = 4;
  12650. /* Set bit 23 to enable PCIX hw bug fix */
  12651. tp->dma_rwctrl |=
  12652. (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
  12653. (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
  12654. (1 << 23);
  12655. } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
  12656. /* 5780 always in PCIX mode */
  12657. tp->dma_rwctrl |= 0x00144000;
  12658. } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
  12659. /* 5714 always in PCIX mode */
  12660. tp->dma_rwctrl |= 0x00148000;
  12661. } else {
  12662. tp->dma_rwctrl |= 0x001b000f;
  12663. }
  12664. }
  12665. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
  12666. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
  12667. tp->dma_rwctrl &= 0xfffffff0;
  12668. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
  12669. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
  12670. /* Remove this if it causes problems for some boards. */
  12671. tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
  12672. /* On 5700/5701 chips, we need to set this bit.
  12673. * Otherwise the chip will issue cacheline transactions
  12674. * to streamable DMA memory with not all the byte
  12675. * enables turned on. This is an error on several
  12676. * RISC PCI controllers, in particular sparc64.
  12677. *
  12678. * On 5703/5704 chips, this bit has been reassigned
  12679. * a different meaning. In particular, it is used
  12680. * on those chips to enable a PCI-X workaround.
  12681. */
  12682. tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
  12683. }
  12684. tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
  12685. #if 0
  12686. /* Unneeded, already done by tg3_get_invariants. */
  12687. tg3_switch_clocks(tp);
  12688. #endif
  12689. if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
  12690. GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
  12691. goto out;
  12692. /* It is best to perform DMA test with maximum write burst size
  12693. * to expose the 5700/5701 write DMA bug.
  12694. */
  12695. saved_dma_rwctrl = tp->dma_rwctrl;
  12696. tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
  12697. tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
  12698. while (1) {
  12699. u32 *p = buf, i;
  12700. for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
  12701. p[i] = i;
  12702. /* Send the buffer to the chip. */
  12703. ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
  12704. if (ret) {
  12705. dev_err(&tp->pdev->dev,
  12706. "%s: Buffer write failed. err = %d\n",
  12707. __func__, ret);
  12708. break;
  12709. }
  12710. #if 0
  12711. /* validate data reached card RAM correctly. */
  12712. for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
  12713. u32 val;
  12714. tg3_read_mem(tp, 0x2100 + (i*4), &val);
  12715. if (le32_to_cpu(val) != p[i]) {
  12716. dev_err(&tp->pdev->dev,
  12717. "%s: Buffer corrupted on device! "
  12718. "(%d != %d)\n", __func__, val, i);
  12719. /* ret = -ENODEV here? */
  12720. }
  12721. p[i] = 0;
  12722. }
  12723. #endif
  12724. /* Now read it back. */
  12725. ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
  12726. if (ret) {
  12727. dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
  12728. "err = %d\n", __func__, ret);
  12729. break;
  12730. }
  12731. /* Verify it. */
  12732. for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
  12733. if (p[i] == i)
  12734. continue;
  12735. if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
  12736. DMA_RWCTRL_WRITE_BNDRY_16) {
  12737. tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
  12738. tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
  12739. tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
  12740. break;
  12741. } else {
  12742. dev_err(&tp->pdev->dev,
  12743. "%s: Buffer corrupted on read back! "
  12744. "(%d != %d)\n", __func__, p[i], i);
  12745. ret = -ENODEV;
  12746. goto out;
  12747. }
  12748. }
  12749. if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
  12750. /* Success. */
  12751. ret = 0;
  12752. break;
  12753. }
  12754. }
  12755. if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
  12756. DMA_RWCTRL_WRITE_BNDRY_16) {
  12757. /* DMA test passed without adjusting DMA boundary,
  12758. * now look for chipsets that are known to expose the
  12759. * DMA bug without failing the test.
  12760. */
  12761. if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
  12762. tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
  12763. tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
  12764. } else {
  12765. /* Safe to use the calculated DMA boundary. */
  12766. tp->dma_rwctrl = saved_dma_rwctrl;
  12767. }
  12768. tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
  12769. }
  12770. out:
  12771. dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
  12772. out_nofree:
  12773. return ret;
  12774. }
  12775. static void __devinit tg3_init_bufmgr_config(struct tg3 *tp)
  12776. {
  12777. if (tg3_flag(tp, 57765_PLUS)) {
  12778. tp->bufmgr_config.mbuf_read_dma_low_water =
  12779. DEFAULT_MB_RDMA_LOW_WATER_5705;
  12780. tp->bufmgr_config.mbuf_mac_rx_low_water =
  12781. DEFAULT_MB_MACRX_LOW_WATER_57765;
  12782. tp->bufmgr_config.mbuf_high_water =
  12783. DEFAULT_MB_HIGH_WATER_57765;
  12784. tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
  12785. DEFAULT_MB_RDMA_LOW_WATER_5705;
  12786. tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
  12787. DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
  12788. tp->bufmgr_config.mbuf_high_water_jumbo =
  12789. DEFAULT_MB_HIGH_WATER_JUMBO_57765;
  12790. } else if (tg3_flag(tp, 5705_PLUS)) {
  12791. tp->bufmgr_config.mbuf_read_dma_low_water =
  12792. DEFAULT_MB_RDMA_LOW_WATER_5705;
  12793. tp->bufmgr_config.mbuf_mac_rx_low_water =
  12794. DEFAULT_MB_MACRX_LOW_WATER_5705;
  12795. tp->bufmgr_config.mbuf_high_water =
  12796. DEFAULT_MB_HIGH_WATER_5705;
  12797. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
  12798. tp->bufmgr_config.mbuf_mac_rx_low_water =
  12799. DEFAULT_MB_MACRX_LOW_WATER_5906;
  12800. tp->bufmgr_config.mbuf_high_water =
  12801. DEFAULT_MB_HIGH_WATER_5906;
  12802. }
  12803. tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
  12804. DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
  12805. tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
  12806. DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
  12807. tp->bufmgr_config.mbuf_high_water_jumbo =
  12808. DEFAULT_MB_HIGH_WATER_JUMBO_5780;
  12809. } else {
  12810. tp->bufmgr_config.mbuf_read_dma_low_water =
  12811. DEFAULT_MB_RDMA_LOW_WATER;
  12812. tp->bufmgr_config.mbuf_mac_rx_low_water =
  12813. DEFAULT_MB_MACRX_LOW_WATER;
  12814. tp->bufmgr_config.mbuf_high_water =
  12815. DEFAULT_MB_HIGH_WATER;
  12816. tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
  12817. DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
  12818. tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
  12819. DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
  12820. tp->bufmgr_config.mbuf_high_water_jumbo =
  12821. DEFAULT_MB_HIGH_WATER_JUMBO;
  12822. }
  12823. tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
  12824. tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
  12825. }
  12826. static char * __devinit tg3_phy_string(struct tg3 *tp)
  12827. {
  12828. switch (tp->phy_id & TG3_PHY_ID_MASK) {
  12829. case TG3_PHY_ID_BCM5400: return "5400";
  12830. case TG3_PHY_ID_BCM5401: return "5401";
  12831. case TG3_PHY_ID_BCM5411: return "5411";
  12832. case TG3_PHY_ID_BCM5701: return "5701";
  12833. case TG3_PHY_ID_BCM5703: return "5703";
  12834. case TG3_PHY_ID_BCM5704: return "5704";
  12835. case TG3_PHY_ID_BCM5705: return "5705";
  12836. case TG3_PHY_ID_BCM5750: return "5750";
  12837. case TG3_PHY_ID_BCM5752: return "5752";
  12838. case TG3_PHY_ID_BCM5714: return "5714";
  12839. case TG3_PHY_ID_BCM5780: return "5780";
  12840. case TG3_PHY_ID_BCM5755: return "5755";
  12841. case TG3_PHY_ID_BCM5787: return "5787";
  12842. case TG3_PHY_ID_BCM5784: return "5784";
  12843. case TG3_PHY_ID_BCM5756: return "5722/5756";
  12844. case TG3_PHY_ID_BCM5906: return "5906";
  12845. case TG3_PHY_ID_BCM5761: return "5761";
  12846. case TG3_PHY_ID_BCM5718C: return "5718C";
  12847. case TG3_PHY_ID_BCM5718S: return "5718S";
  12848. case TG3_PHY_ID_BCM57765: return "57765";
  12849. case TG3_PHY_ID_BCM5719C: return "5719C";
  12850. case TG3_PHY_ID_BCM5720C: return "5720C";
  12851. case TG3_PHY_ID_BCM8002: return "8002/serdes";
  12852. case 0: return "serdes";
  12853. default: return "unknown";
  12854. }
  12855. }
  12856. static char * __devinit tg3_bus_string(struct tg3 *tp, char *str)
  12857. {
  12858. if (tg3_flag(tp, PCI_EXPRESS)) {
  12859. strcpy(str, "PCI Express");
  12860. return str;
  12861. } else if (tg3_flag(tp, PCIX_MODE)) {
  12862. u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
  12863. strcpy(str, "PCIX:");
  12864. if ((clock_ctrl == 7) ||
  12865. ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
  12866. GRC_MISC_CFG_BOARD_ID_5704CIOBE))
  12867. strcat(str, "133MHz");
  12868. else if (clock_ctrl == 0)
  12869. strcat(str, "33MHz");
  12870. else if (clock_ctrl == 2)
  12871. strcat(str, "50MHz");
  12872. else if (clock_ctrl == 4)
  12873. strcat(str, "66MHz");
  12874. else if (clock_ctrl == 6)
  12875. strcat(str, "100MHz");
  12876. } else {
  12877. strcpy(str, "PCI:");
  12878. if (tg3_flag(tp, PCI_HIGH_SPEED))
  12879. strcat(str, "66MHz");
  12880. else
  12881. strcat(str, "33MHz");
  12882. }
  12883. if (tg3_flag(tp, PCI_32BIT))
  12884. strcat(str, ":32-bit");
  12885. else
  12886. strcat(str, ":64-bit");
  12887. return str;
  12888. }
  12889. static void __devinit tg3_init_coal(struct tg3 *tp)
  12890. {
  12891. struct ethtool_coalesce *ec = &tp->coal;
  12892. memset(ec, 0, sizeof(*ec));
  12893. ec->cmd = ETHTOOL_GCOALESCE;
  12894. ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
  12895. ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
  12896. ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
  12897. ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
  12898. ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
  12899. ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
  12900. ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
  12901. ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
  12902. ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
  12903. if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
  12904. HOSTCC_MODE_CLRTICK_TXBD)) {
  12905. ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
  12906. ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
  12907. ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
  12908. ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
  12909. }
  12910. if (tg3_flag(tp, 5705_PLUS)) {
  12911. ec->rx_coalesce_usecs_irq = 0;
  12912. ec->tx_coalesce_usecs_irq = 0;
  12913. ec->stats_block_coalesce_usecs = 0;
  12914. }
  12915. }
  12916. static int __devinit tg3_init_one(struct pci_dev *pdev,
  12917. const struct pci_device_id *ent)
  12918. {
  12919. struct net_device *dev;
  12920. struct tg3 *tp;
  12921. int i, err, pm_cap;
  12922. u32 sndmbx, rcvmbx, intmbx;
  12923. char str[40];
  12924. u64 dma_mask, persist_dma_mask;
  12925. netdev_features_t features = 0;
  12926. printk_once(KERN_INFO "%s\n", version);
  12927. err = pci_enable_device(pdev);
  12928. if (err) {
  12929. dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
  12930. return err;
  12931. }
  12932. err = pci_request_regions(pdev, DRV_MODULE_NAME);
  12933. if (err) {
  12934. dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
  12935. goto err_out_disable_pdev;
  12936. }
  12937. pci_set_master(pdev);
  12938. /* Find power-management capability. */
  12939. pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
  12940. if (pm_cap == 0) {
  12941. dev_err(&pdev->dev,
  12942. "Cannot find Power Management capability, aborting\n");
  12943. err = -EIO;
  12944. goto err_out_free_res;
  12945. }
  12946. err = pci_set_power_state(pdev, PCI_D0);
  12947. if (err) {
  12948. dev_err(&pdev->dev, "Transition to D0 failed, aborting\n");
  12949. goto err_out_free_res;
  12950. }
  12951. dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
  12952. if (!dev) {
  12953. err = -ENOMEM;
  12954. goto err_out_power_down;
  12955. }
  12956. SET_NETDEV_DEV(dev, &pdev->dev);
  12957. tp = netdev_priv(dev);
  12958. tp->pdev = pdev;
  12959. tp->dev = dev;
  12960. tp->pm_cap = pm_cap;
  12961. tp->rx_mode = TG3_DEF_RX_MODE;
  12962. tp->tx_mode = TG3_DEF_TX_MODE;
  12963. if (tg3_debug > 0)
  12964. tp->msg_enable = tg3_debug;
  12965. else
  12966. tp->msg_enable = TG3_DEF_MSG_ENABLE;
  12967. /* The word/byte swap controls here control register access byte
  12968. * swapping. DMA data byte swapping is controlled in the GRC_MODE
  12969. * setting below.
  12970. */
  12971. tp->misc_host_ctrl =
  12972. MISC_HOST_CTRL_MASK_PCI_INT |
  12973. MISC_HOST_CTRL_WORD_SWAP |
  12974. MISC_HOST_CTRL_INDIR_ACCESS |
  12975. MISC_HOST_CTRL_PCISTATE_RW;
  12976. /* The NONFRM (non-frame) byte/word swap controls take effect
  12977. * on descriptor entries, anything which isn't packet data.
  12978. *
  12979. * The StrongARM chips on the board (one for tx, one for rx)
  12980. * are running in big-endian mode.
  12981. */
  12982. tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
  12983. GRC_MODE_WSWAP_NONFRM_DATA);
  12984. #ifdef __BIG_ENDIAN
  12985. tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
  12986. #endif
  12987. spin_lock_init(&tp->lock);
  12988. spin_lock_init(&tp->indirect_lock);
  12989. INIT_WORK(&tp->reset_task, tg3_reset_task);
  12990. tp->regs = pci_ioremap_bar(pdev, BAR_0);
  12991. if (!tp->regs) {
  12992. dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
  12993. err = -ENOMEM;
  12994. goto err_out_free_dev;
  12995. }
  12996. if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
  12997. tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761E ||
  12998. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S ||
  12999. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761SE ||
  13000. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
  13001. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
  13002. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
  13003. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720) {
  13004. tg3_flag_set(tp, ENABLE_APE);
  13005. tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
  13006. if (!tp->aperegs) {
  13007. dev_err(&pdev->dev,
  13008. "Cannot map APE registers, aborting\n");
  13009. err = -ENOMEM;
  13010. goto err_out_iounmap;
  13011. }
  13012. }
  13013. tp->rx_pending = TG3_DEF_RX_RING_PENDING;
  13014. tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
  13015. dev->ethtool_ops = &tg3_ethtool_ops;
  13016. dev->watchdog_timeo = TG3_TX_TIMEOUT;
  13017. dev->netdev_ops = &tg3_netdev_ops;
  13018. dev->irq = pdev->irq;
  13019. err = tg3_get_invariants(tp);
  13020. if (err) {
  13021. dev_err(&pdev->dev,
  13022. "Problem fetching invariants of chip, aborting\n");
  13023. goto err_out_apeunmap;
  13024. }
  13025. /* The EPB bridge inside 5714, 5715, and 5780 and any
  13026. * device behind the EPB cannot support DMA addresses > 40-bit.
  13027. * On 64-bit systems with IOMMU, use 40-bit dma_mask.
  13028. * On 64-bit systems without IOMMU, use 64-bit dma_mask and
  13029. * do DMA address check in tg3_start_xmit().
  13030. */
  13031. if (tg3_flag(tp, IS_5788))
  13032. persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
  13033. else if (tg3_flag(tp, 40BIT_DMA_BUG)) {
  13034. persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
  13035. #ifdef CONFIG_HIGHMEM
  13036. dma_mask = DMA_BIT_MASK(64);
  13037. #endif
  13038. } else
  13039. persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
  13040. /* Configure DMA attributes. */
  13041. if (dma_mask > DMA_BIT_MASK(32)) {
  13042. err = pci_set_dma_mask(pdev, dma_mask);
  13043. if (!err) {
  13044. features |= NETIF_F_HIGHDMA;
  13045. err = pci_set_consistent_dma_mask(pdev,
  13046. persist_dma_mask);
  13047. if (err < 0) {
  13048. dev_err(&pdev->dev, "Unable to obtain 64 bit "
  13049. "DMA for consistent allocations\n");
  13050. goto err_out_apeunmap;
  13051. }
  13052. }
  13053. }
  13054. if (err || dma_mask == DMA_BIT_MASK(32)) {
  13055. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  13056. if (err) {
  13057. dev_err(&pdev->dev,
  13058. "No usable DMA configuration, aborting\n");
  13059. goto err_out_apeunmap;
  13060. }
  13061. }
  13062. tg3_init_bufmgr_config(tp);
  13063. features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
  13064. /* 5700 B0 chips do not support checksumming correctly due
  13065. * to hardware bugs.
  13066. */
  13067. if (tp->pci_chip_rev_id != CHIPREV_ID_5700_B0) {
  13068. features |= NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
  13069. if (tg3_flag(tp, 5755_PLUS))
  13070. features |= NETIF_F_IPV6_CSUM;
  13071. }
  13072. /* TSO is on by default on chips that support hardware TSO.
  13073. * Firmware TSO on older chips gives lower performance, so it
  13074. * is off by default, but can be enabled using ethtool.
  13075. */
  13076. if ((tg3_flag(tp, HW_TSO_1) ||
  13077. tg3_flag(tp, HW_TSO_2) ||
  13078. tg3_flag(tp, HW_TSO_3)) &&
  13079. (features & NETIF_F_IP_CSUM))
  13080. features |= NETIF_F_TSO;
  13081. if (tg3_flag(tp, HW_TSO_2) || tg3_flag(tp, HW_TSO_3)) {
  13082. if (features & NETIF_F_IPV6_CSUM)
  13083. features |= NETIF_F_TSO6;
  13084. if (tg3_flag(tp, HW_TSO_3) ||
  13085. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
  13086. (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
  13087. GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
  13088. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
  13089. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
  13090. features |= NETIF_F_TSO_ECN;
  13091. }
  13092. dev->features |= features;
  13093. dev->vlan_features |= features;
  13094. /*
  13095. * Add loopback capability only for a subset of devices that support
  13096. * MAC-LOOPBACK. Eventually this need to be enhanced to allow INT-PHY
  13097. * loopback for the remaining devices.
  13098. */
  13099. if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
  13100. !tg3_flag(tp, CPMU_PRESENT))
  13101. /* Add the loopback capability */
  13102. features |= NETIF_F_LOOPBACK;
  13103. dev->hw_features |= features;
  13104. if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
  13105. !tg3_flag(tp, TSO_CAPABLE) &&
  13106. !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
  13107. tg3_flag_set(tp, MAX_RXPEND_64);
  13108. tp->rx_pending = 63;
  13109. }
  13110. err = tg3_get_device_address(tp);
  13111. if (err) {
  13112. dev_err(&pdev->dev,
  13113. "Could not obtain valid ethernet address, aborting\n");
  13114. goto err_out_apeunmap;
  13115. }
  13116. /*
  13117. * Reset chip in case UNDI or EFI driver did not shutdown
  13118. * DMA self test will enable WDMAC and we'll see (spurious)
  13119. * pending DMA on the PCI bus at that point.
  13120. */
  13121. if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
  13122. (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
  13123. tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
  13124. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  13125. }
  13126. err = tg3_test_dma(tp);
  13127. if (err) {
  13128. dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
  13129. goto err_out_apeunmap;
  13130. }
  13131. intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
  13132. rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
  13133. sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
  13134. for (i = 0; i < tp->irq_max; i++) {
  13135. struct tg3_napi *tnapi = &tp->napi[i];
  13136. tnapi->tp = tp;
  13137. tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
  13138. tnapi->int_mbox = intmbx;
  13139. if (i <= 4)
  13140. intmbx += 0x8;
  13141. else
  13142. intmbx += 0x4;
  13143. tnapi->consmbox = rcvmbx;
  13144. tnapi->prodmbox = sndmbx;
  13145. if (i)
  13146. tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
  13147. else
  13148. tnapi->coal_now = HOSTCC_MODE_NOW;
  13149. if (!tg3_flag(tp, SUPPORT_MSIX))
  13150. break;
  13151. /*
  13152. * If we support MSIX, we'll be using RSS. If we're using
  13153. * RSS, the first vector only handles link interrupts and the
  13154. * remaining vectors handle rx and tx interrupts. Reuse the
  13155. * mailbox values for the next iteration. The values we setup
  13156. * above are still useful for the single vectored mode.
  13157. */
  13158. if (!i)
  13159. continue;
  13160. rcvmbx += 0x8;
  13161. if (sndmbx & 0x4)
  13162. sndmbx -= 0x4;
  13163. else
  13164. sndmbx += 0xc;
  13165. }
  13166. tg3_init_coal(tp);
  13167. pci_set_drvdata(pdev, dev);
  13168. if (tg3_flag(tp, 5717_PLUS)) {
  13169. /* Resume a low-power mode */
  13170. tg3_frob_aux_power(tp, false);
  13171. }
  13172. tg3_timer_init(tp);
  13173. err = register_netdev(dev);
  13174. if (err) {
  13175. dev_err(&pdev->dev, "Cannot register net device, aborting\n");
  13176. goto err_out_apeunmap;
  13177. }
  13178. netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
  13179. tp->board_part_number,
  13180. tp->pci_chip_rev_id,
  13181. tg3_bus_string(tp, str),
  13182. dev->dev_addr);
  13183. if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
  13184. struct phy_device *phydev;
  13185. phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
  13186. netdev_info(dev,
  13187. "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
  13188. phydev->drv->name, dev_name(&phydev->dev));
  13189. } else {
  13190. char *ethtype;
  13191. if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
  13192. ethtype = "10/100Base-TX";
  13193. else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
  13194. ethtype = "1000Base-SX";
  13195. else
  13196. ethtype = "10/100/1000Base-T";
  13197. netdev_info(dev, "attached PHY is %s (%s Ethernet) "
  13198. "(WireSpeed[%d], EEE[%d])\n",
  13199. tg3_phy_string(tp), ethtype,
  13200. (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0,
  13201. (tp->phy_flags & TG3_PHYFLG_EEE_CAP) != 0);
  13202. }
  13203. netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
  13204. (dev->features & NETIF_F_RXCSUM) != 0,
  13205. tg3_flag(tp, USE_LINKCHG_REG) != 0,
  13206. (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
  13207. tg3_flag(tp, ENABLE_ASF) != 0,
  13208. tg3_flag(tp, TSO_CAPABLE) != 0);
  13209. netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
  13210. tp->dma_rwctrl,
  13211. pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
  13212. ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
  13213. pci_save_state(pdev);
  13214. return 0;
  13215. err_out_apeunmap:
  13216. if (tp->aperegs) {
  13217. iounmap(tp->aperegs);
  13218. tp->aperegs = NULL;
  13219. }
  13220. err_out_iounmap:
  13221. if (tp->regs) {
  13222. iounmap(tp->regs);
  13223. tp->regs = NULL;
  13224. }
  13225. err_out_free_dev:
  13226. free_netdev(dev);
  13227. err_out_power_down:
  13228. pci_set_power_state(pdev, PCI_D3hot);
  13229. err_out_free_res:
  13230. pci_release_regions(pdev);
  13231. err_out_disable_pdev:
  13232. pci_disable_device(pdev);
  13233. pci_set_drvdata(pdev, NULL);
  13234. return err;
  13235. }
  13236. static void __devexit tg3_remove_one(struct pci_dev *pdev)
  13237. {
  13238. struct net_device *dev = pci_get_drvdata(pdev);
  13239. if (dev) {
  13240. struct tg3 *tp = netdev_priv(dev);
  13241. release_firmware(tp->fw);
  13242. tg3_reset_task_cancel(tp);
  13243. if (tg3_flag(tp, USE_PHYLIB)) {
  13244. tg3_phy_fini(tp);
  13245. tg3_mdio_fini(tp);
  13246. }
  13247. unregister_netdev(dev);
  13248. if (tp->aperegs) {
  13249. iounmap(tp->aperegs);
  13250. tp->aperegs = NULL;
  13251. }
  13252. if (tp->regs) {
  13253. iounmap(tp->regs);
  13254. tp->regs = NULL;
  13255. }
  13256. free_netdev(dev);
  13257. pci_release_regions(pdev);
  13258. pci_disable_device(pdev);
  13259. pci_set_drvdata(pdev, NULL);
  13260. }
  13261. }
  13262. #ifdef CONFIG_PM_SLEEP
  13263. static int tg3_suspend(struct device *device)
  13264. {
  13265. struct pci_dev *pdev = to_pci_dev(device);
  13266. struct net_device *dev = pci_get_drvdata(pdev);
  13267. struct tg3 *tp = netdev_priv(dev);
  13268. int err;
  13269. if (!netif_running(dev))
  13270. return 0;
  13271. tg3_reset_task_cancel(tp);
  13272. tg3_phy_stop(tp);
  13273. tg3_netif_stop(tp);
  13274. tg3_timer_stop(tp);
  13275. tg3_full_lock(tp, 1);
  13276. tg3_disable_ints(tp);
  13277. tg3_full_unlock(tp);
  13278. netif_device_detach(dev);
  13279. tg3_full_lock(tp, 0);
  13280. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  13281. tg3_flag_clear(tp, INIT_COMPLETE);
  13282. tg3_full_unlock(tp);
  13283. err = tg3_power_down_prepare(tp);
  13284. if (err) {
  13285. int err2;
  13286. tg3_full_lock(tp, 0);
  13287. tg3_flag_set(tp, INIT_COMPLETE);
  13288. err2 = tg3_restart_hw(tp, 1);
  13289. if (err2)
  13290. goto out;
  13291. tg3_timer_start(tp);
  13292. netif_device_attach(dev);
  13293. tg3_netif_start(tp);
  13294. out:
  13295. tg3_full_unlock(tp);
  13296. if (!err2)
  13297. tg3_phy_start(tp);
  13298. }
  13299. return err;
  13300. }
  13301. static int tg3_resume(struct device *device)
  13302. {
  13303. struct pci_dev *pdev = to_pci_dev(device);
  13304. struct net_device *dev = pci_get_drvdata(pdev);
  13305. struct tg3 *tp = netdev_priv(dev);
  13306. int err;
  13307. if (!netif_running(dev))
  13308. return 0;
  13309. netif_device_attach(dev);
  13310. tg3_full_lock(tp, 0);
  13311. tg3_flag_set(tp, INIT_COMPLETE);
  13312. err = tg3_restart_hw(tp, 1);
  13313. if (err)
  13314. goto out;
  13315. tg3_timer_start(tp);
  13316. tg3_netif_start(tp);
  13317. out:
  13318. tg3_full_unlock(tp);
  13319. if (!err)
  13320. tg3_phy_start(tp);
  13321. return err;
  13322. }
  13323. static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
  13324. #define TG3_PM_OPS (&tg3_pm_ops)
  13325. #else
  13326. #define TG3_PM_OPS NULL
  13327. #endif /* CONFIG_PM_SLEEP */
  13328. /**
  13329. * tg3_io_error_detected - called when PCI error is detected
  13330. * @pdev: Pointer to PCI device
  13331. * @state: The current pci connection state
  13332. *
  13333. * This function is called after a PCI bus error affecting
  13334. * this device has been detected.
  13335. */
  13336. static pci_ers_result_t tg3_io_error_detected(struct pci_dev *pdev,
  13337. pci_channel_state_t state)
  13338. {
  13339. struct net_device *netdev = pci_get_drvdata(pdev);
  13340. struct tg3 *tp = netdev_priv(netdev);
  13341. pci_ers_result_t err = PCI_ERS_RESULT_NEED_RESET;
  13342. netdev_info(netdev, "PCI I/O error detected\n");
  13343. rtnl_lock();
  13344. if (!netif_running(netdev))
  13345. goto done;
  13346. tg3_phy_stop(tp);
  13347. tg3_netif_stop(tp);
  13348. tg3_timer_stop(tp);
  13349. /* Want to make sure that the reset task doesn't run */
  13350. tg3_reset_task_cancel(tp);
  13351. netif_device_detach(netdev);
  13352. /* Clean up software state, even if MMIO is blocked */
  13353. tg3_full_lock(tp, 0);
  13354. tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
  13355. tg3_full_unlock(tp);
  13356. done:
  13357. if (state == pci_channel_io_perm_failure)
  13358. err = PCI_ERS_RESULT_DISCONNECT;
  13359. else
  13360. pci_disable_device(pdev);
  13361. rtnl_unlock();
  13362. return err;
  13363. }
  13364. /**
  13365. * tg3_io_slot_reset - called after the pci bus has been reset.
  13366. * @pdev: Pointer to PCI device
  13367. *
  13368. * Restart the card from scratch, as if from a cold-boot.
  13369. * At this point, the card has exprienced a hard reset,
  13370. * followed by fixups by BIOS, and has its config space
  13371. * set up identically to what it was at cold boot.
  13372. */
  13373. static pci_ers_result_t tg3_io_slot_reset(struct pci_dev *pdev)
  13374. {
  13375. struct net_device *netdev = pci_get_drvdata(pdev);
  13376. struct tg3 *tp = netdev_priv(netdev);
  13377. pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
  13378. int err;
  13379. rtnl_lock();
  13380. if (pci_enable_device(pdev)) {
  13381. netdev_err(netdev, "Cannot re-enable PCI device after reset.\n");
  13382. goto done;
  13383. }
  13384. pci_set_master(pdev);
  13385. pci_restore_state(pdev);
  13386. pci_save_state(pdev);
  13387. if (!netif_running(netdev)) {
  13388. rc = PCI_ERS_RESULT_RECOVERED;
  13389. goto done;
  13390. }
  13391. err = tg3_power_up(tp);
  13392. if (err)
  13393. goto done;
  13394. rc = PCI_ERS_RESULT_RECOVERED;
  13395. done:
  13396. rtnl_unlock();
  13397. return rc;
  13398. }
  13399. /**
  13400. * tg3_io_resume - called when traffic can start flowing again.
  13401. * @pdev: Pointer to PCI device
  13402. *
  13403. * This callback is called when the error recovery driver tells
  13404. * us that its OK to resume normal operation.
  13405. */
  13406. static void tg3_io_resume(struct pci_dev *pdev)
  13407. {
  13408. struct net_device *netdev = pci_get_drvdata(pdev);
  13409. struct tg3 *tp = netdev_priv(netdev);
  13410. int err;
  13411. rtnl_lock();
  13412. if (!netif_running(netdev))
  13413. goto done;
  13414. tg3_full_lock(tp, 0);
  13415. tg3_flag_set(tp, INIT_COMPLETE);
  13416. err = tg3_restart_hw(tp, 1);
  13417. tg3_full_unlock(tp);
  13418. if (err) {
  13419. netdev_err(netdev, "Cannot restart hardware after reset.\n");
  13420. goto done;
  13421. }
  13422. netif_device_attach(netdev);
  13423. tg3_timer_start(tp);
  13424. tg3_netif_start(tp);
  13425. tg3_phy_start(tp);
  13426. done:
  13427. rtnl_unlock();
  13428. }
  13429. static struct pci_error_handlers tg3_err_handler = {
  13430. .error_detected = tg3_io_error_detected,
  13431. .slot_reset = tg3_io_slot_reset,
  13432. .resume = tg3_io_resume
  13433. };
  13434. static struct pci_driver tg3_driver = {
  13435. .name = DRV_MODULE_NAME,
  13436. .id_table = tg3_pci_tbl,
  13437. .probe = tg3_init_one,
  13438. .remove = __devexit_p(tg3_remove_one),
  13439. .err_handler = &tg3_err_handler,
  13440. .driver.pm = TG3_PM_OPS,
  13441. };
  13442. static int __init tg3_init(void)
  13443. {
  13444. return pci_register_driver(&tg3_driver);
  13445. }
  13446. static void __exit tg3_cleanup(void)
  13447. {
  13448. pci_unregister_driver(&tg3_driver);
  13449. }
  13450. module_init(tg3_init);
  13451. module_exit(tg3_cleanup);