tg3.c 407 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564
  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-2011 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/ioport.h>
  28. #include <linux/pci.h>
  29. #include <linux/netdevice.h>
  30. #include <linux/etherdevice.h>
  31. #include <linux/skbuff.h>
  32. #include <linux/ethtool.h>
  33. #include <linux/mdio.h>
  34. #include <linux/mii.h>
  35. #include <linux/phy.h>
  36. #include <linux/brcmphy.h>
  37. #include <linux/if_vlan.h>
  38. #include <linux/ip.h>
  39. #include <linux/tcp.h>
  40. #include <linux/workqueue.h>
  41. #include <linux/prefetch.h>
  42. #include <linux/dma-mapping.h>
  43. #include <linux/firmware.h>
  44. #include <net/checksum.h>
  45. #include <net/ip.h>
  46. #include <asm/system.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 119
  79. #define DRV_MODULE_VERSION \
  80. __stringify(TG3_MAJ_NUM) "." __stringify(TG3_MIN_NUM)
  81. #define DRV_MODULE_RELDATE "May 18, 2011"
  82. #define TG3_DEF_MAC_MODE 0
  83. #define TG3_DEF_RX_MODE 0
  84. #define TG3_DEF_TX_MODE 0
  85. #define TG3_DEF_MSG_ENABLE \
  86. (NETIF_MSG_DRV | \
  87. NETIF_MSG_PROBE | \
  88. NETIF_MSG_LINK | \
  89. NETIF_MSG_TIMER | \
  90. NETIF_MSG_IFDOWN | \
  91. NETIF_MSG_IFUP | \
  92. NETIF_MSG_RX_ERR | \
  93. NETIF_MSG_TX_ERR)
  94. /* length of time before we decide the hardware is borked,
  95. * and dev->tx_timeout() should be called to fix the problem
  96. */
  97. #define TG3_TX_TIMEOUT (5 * HZ)
  98. /* hardware minimum and maximum for a single frame's data payload */
  99. #define TG3_MIN_MTU 60
  100. #define TG3_MAX_MTU(tp) \
  101. (tg3_flag(tp, JUMBO_CAPABLE) ? 9000 : 1500)
  102. /* These numbers seem to be hard coded in the NIC firmware somehow.
  103. * You can't change the ring sizes, but you can change where you place
  104. * them in the NIC onboard memory.
  105. */
  106. #define TG3_RX_STD_RING_SIZE(tp) \
  107. (tg3_flag(tp, LRG_PROD_RING_CAP) ? \
  108. TG3_RX_STD_MAX_SIZE_5717 : TG3_RX_STD_MAX_SIZE_5700)
  109. #define TG3_DEF_RX_RING_PENDING 200
  110. #define TG3_RX_JMB_RING_SIZE(tp) \
  111. (tg3_flag(tp, LRG_PROD_RING_CAP) ? \
  112. TG3_RX_JMB_MAX_SIZE_5717 : TG3_RX_JMB_MAX_SIZE_5700)
  113. #define TG3_DEF_RX_JUMBO_RING_PENDING 100
  114. #define TG3_RSS_INDIR_TBL_SIZE 128
  115. /* Do not place this n-ring entries value into the tp struct itself,
  116. * we really want to expose these constants to GCC so that modulo et
  117. * al. operations are done with shifts and masks instead of with
  118. * hw multiply/modulo instructions. Another solution would be to
  119. * replace things like '% foo' with '& (foo - 1)'.
  120. */
  121. #define TG3_TX_RING_SIZE 512
  122. #define TG3_DEF_TX_RING_PENDING (TG3_TX_RING_SIZE - 1)
  123. #define TG3_RX_STD_RING_BYTES(tp) \
  124. (sizeof(struct tg3_rx_buffer_desc) * TG3_RX_STD_RING_SIZE(tp))
  125. #define TG3_RX_JMB_RING_BYTES(tp) \
  126. (sizeof(struct tg3_ext_rx_buffer_desc) * TG3_RX_JMB_RING_SIZE(tp))
  127. #define TG3_RX_RCB_RING_BYTES(tp) \
  128. (sizeof(struct tg3_rx_buffer_desc) * (tp->rx_ret_ring_mask + 1))
  129. #define TG3_TX_RING_BYTES (sizeof(struct tg3_tx_buffer_desc) * \
  130. TG3_TX_RING_SIZE)
  131. #define NEXT_TX(N) (((N) + 1) & (TG3_TX_RING_SIZE - 1))
  132. #define TG3_DMA_BYTE_ENAB 64
  133. #define TG3_RX_STD_DMA_SZ 1536
  134. #define TG3_RX_JMB_DMA_SZ 9046
  135. #define TG3_RX_DMA_TO_MAP_SZ(x) ((x) + TG3_DMA_BYTE_ENAB)
  136. #define TG3_RX_STD_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_STD_DMA_SZ)
  137. #define TG3_RX_JMB_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_JMB_DMA_SZ)
  138. #define TG3_RX_STD_BUFF_RING_SIZE(tp) \
  139. (sizeof(struct ring_info) * TG3_RX_STD_RING_SIZE(tp))
  140. #define TG3_RX_JMB_BUFF_RING_SIZE(tp) \
  141. (sizeof(struct ring_info) * TG3_RX_JMB_RING_SIZE(tp))
  142. /* Due to a hardware bug, the 5701 can only DMA to memory addresses
  143. * that are at least dword aligned when used in PCIX mode. The driver
  144. * works around this bug by double copying the packet. This workaround
  145. * is built into the normal double copy length check for efficiency.
  146. *
  147. * However, the double copy is only necessary on those architectures
  148. * where unaligned memory accesses are inefficient. For those architectures
  149. * where unaligned memory accesses incur little penalty, we can reintegrate
  150. * the 5701 in the normal rx path. Doing so saves a device structure
  151. * dereference by hardcoding the double copy threshold in place.
  152. */
  153. #define TG3_RX_COPY_THRESHOLD 256
  154. #if NET_IP_ALIGN == 0 || defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
  155. #define TG3_RX_COPY_THRESH(tp) TG3_RX_COPY_THRESHOLD
  156. #else
  157. #define TG3_RX_COPY_THRESH(tp) ((tp)->rx_copy_thresh)
  158. #endif
  159. /* minimum number of free TX descriptors required to wake up TX process */
  160. #define TG3_TX_WAKEUP_THRESH(tnapi) ((tnapi)->tx_pending / 4)
  161. #define TG3_RAW_IP_ALIGN 2
  162. #define TG3_FW_UPDATE_TIMEOUT_SEC 5
  163. #define FIRMWARE_TG3 "tigon/tg3.bin"
  164. #define FIRMWARE_TG3TSO "tigon/tg3_tso.bin"
  165. #define FIRMWARE_TG3TSO5 "tigon/tg3_tso5.bin"
  166. static char version[] __devinitdata =
  167. DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")";
  168. MODULE_AUTHOR("David S. Miller (davem@redhat.com) and Jeff Garzik (jgarzik@pobox.com)");
  169. MODULE_DESCRIPTION("Broadcom Tigon3 ethernet driver");
  170. MODULE_LICENSE("GPL");
  171. MODULE_VERSION(DRV_MODULE_VERSION);
  172. MODULE_FIRMWARE(FIRMWARE_TG3);
  173. MODULE_FIRMWARE(FIRMWARE_TG3TSO);
  174. MODULE_FIRMWARE(FIRMWARE_TG3TSO5);
  175. static int tg3_debug = -1; /* -1 == use TG3_DEF_MSG_ENABLE as value */
  176. module_param(tg3_debug, int, 0);
  177. MODULE_PARM_DESC(tg3_debug, "Tigon3 bitmapped debugging message enable value");
  178. static DEFINE_PCI_DEVICE_TABLE(tg3_pci_tbl) = {
  179. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5700)},
  180. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5701)},
  181. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702)},
  182. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703)},
  183. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704)},
  184. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702FE)},
  185. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705)},
  186. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705_2)},
  187. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M)},
  188. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M_2)},
  189. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702X)},
  190. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703X)},
  191. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S)},
  192. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702A3)},
  193. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703A3)},
  194. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5782)},
  195. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5788)},
  196. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5789)},
  197. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901)},
  198. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901_2)},
  199. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S_2)},
  200. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705F)},
  201. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5721)},
  202. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5722)},
  203. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751)},
  204. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751M)},
  205. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751F)},
  206. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752)},
  207. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752M)},
  208. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753)},
  209. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753M)},
  210. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753F)},
  211. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754)},
  212. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754M)},
  213. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755)},
  214. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755M)},
  215. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5756)},
  216. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5786)},
  217. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787)},
  218. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787M)},
  219. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787F)},
  220. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714)},
  221. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714S)},
  222. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715)},
  223. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715S)},
  224. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780)},
  225. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780S)},
  226. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5781)},
  227. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906)},
  228. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906M)},
  229. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5784)},
  230. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5764)},
  231. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5723)},
  232. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761)},
  233. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761E)},
  234. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761S)},
  235. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761SE)},
  236. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_G)},
  237. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_F)},
  238. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780)},
  239. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57760)},
  240. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57790)},
  241. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57788)},
  242. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717)},
  243. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5718)},
  244. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57781)},
  245. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57785)},
  246. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57761)},
  247. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57765)},
  248. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57791)},
  249. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57795)},
  250. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5719)},
  251. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5720)},
  252. {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9DXX)},
  253. {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9MXX)},
  254. {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1000)},
  255. {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1001)},
  256. {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1003)},
  257. {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC9100)},
  258. {PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_TIGON3)},
  259. {PCI_DEVICE(0x10cf, 0x11a2)}, /* Fujitsu 1000base-SX with BCM5703SKHB */
  260. {}
  261. };
  262. MODULE_DEVICE_TABLE(pci, tg3_pci_tbl);
  263. static const struct {
  264. const char string[ETH_GSTRING_LEN];
  265. } ethtool_stats_keys[] = {
  266. { "rx_octets" },
  267. { "rx_fragments" },
  268. { "rx_ucast_packets" },
  269. { "rx_mcast_packets" },
  270. { "rx_bcast_packets" },
  271. { "rx_fcs_errors" },
  272. { "rx_align_errors" },
  273. { "rx_xon_pause_rcvd" },
  274. { "rx_xoff_pause_rcvd" },
  275. { "rx_mac_ctrl_rcvd" },
  276. { "rx_xoff_entered" },
  277. { "rx_frame_too_long_errors" },
  278. { "rx_jabbers" },
  279. { "rx_undersize_packets" },
  280. { "rx_in_length_errors" },
  281. { "rx_out_length_errors" },
  282. { "rx_64_or_less_octet_packets" },
  283. { "rx_65_to_127_octet_packets" },
  284. { "rx_128_to_255_octet_packets" },
  285. { "rx_256_to_511_octet_packets" },
  286. { "rx_512_to_1023_octet_packets" },
  287. { "rx_1024_to_1522_octet_packets" },
  288. { "rx_1523_to_2047_octet_packets" },
  289. { "rx_2048_to_4095_octet_packets" },
  290. { "rx_4096_to_8191_octet_packets" },
  291. { "rx_8192_to_9022_octet_packets" },
  292. { "tx_octets" },
  293. { "tx_collisions" },
  294. { "tx_xon_sent" },
  295. { "tx_xoff_sent" },
  296. { "tx_flow_control" },
  297. { "tx_mac_errors" },
  298. { "tx_single_collisions" },
  299. { "tx_mult_collisions" },
  300. { "tx_deferred" },
  301. { "tx_excessive_collisions" },
  302. { "tx_late_collisions" },
  303. { "tx_collide_2times" },
  304. { "tx_collide_3times" },
  305. { "tx_collide_4times" },
  306. { "tx_collide_5times" },
  307. { "tx_collide_6times" },
  308. { "tx_collide_7times" },
  309. { "tx_collide_8times" },
  310. { "tx_collide_9times" },
  311. { "tx_collide_10times" },
  312. { "tx_collide_11times" },
  313. { "tx_collide_12times" },
  314. { "tx_collide_13times" },
  315. { "tx_collide_14times" },
  316. { "tx_collide_15times" },
  317. { "tx_ucast_packets" },
  318. { "tx_mcast_packets" },
  319. { "tx_bcast_packets" },
  320. { "tx_carrier_sense_errors" },
  321. { "tx_discards" },
  322. { "tx_errors" },
  323. { "dma_writeq_full" },
  324. { "dma_write_prioq_full" },
  325. { "rxbds_empty" },
  326. { "rx_discards" },
  327. { "rx_errors" },
  328. { "rx_threshold_hit" },
  329. { "dma_readq_full" },
  330. { "dma_read_prioq_full" },
  331. { "tx_comp_queue_full" },
  332. { "ring_set_send_prod_index" },
  333. { "ring_status_update" },
  334. { "nic_irqs" },
  335. { "nic_avoided_irqs" },
  336. { "nic_tx_threshold_hit" },
  337. { "mbuf_lwm_thresh_hit" },
  338. };
  339. #define TG3_NUM_STATS ARRAY_SIZE(ethtool_stats_keys)
  340. static const struct {
  341. const char string[ETH_GSTRING_LEN];
  342. } ethtool_test_keys[] = {
  343. { "nvram test (online) " },
  344. { "link test (online) " },
  345. { "register test (offline)" },
  346. { "memory test (offline)" },
  347. { "loopback test (offline)" },
  348. { "interrupt test (offline)" },
  349. };
  350. #define TG3_NUM_TEST ARRAY_SIZE(ethtool_test_keys)
  351. static void tg3_write32(struct tg3 *tp, u32 off, u32 val)
  352. {
  353. writel(val, tp->regs + off);
  354. }
  355. static u32 tg3_read32(struct tg3 *tp, u32 off)
  356. {
  357. return readl(tp->regs + off);
  358. }
  359. static void tg3_ape_write32(struct tg3 *tp, u32 off, u32 val)
  360. {
  361. writel(val, tp->aperegs + off);
  362. }
  363. static u32 tg3_ape_read32(struct tg3 *tp, u32 off)
  364. {
  365. return readl(tp->aperegs + off);
  366. }
  367. static void tg3_write_indirect_reg32(struct tg3 *tp, u32 off, u32 val)
  368. {
  369. unsigned long flags;
  370. spin_lock_irqsave(&tp->indirect_lock, flags);
  371. pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
  372. pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
  373. spin_unlock_irqrestore(&tp->indirect_lock, flags);
  374. }
  375. static void tg3_write_flush_reg32(struct tg3 *tp, u32 off, u32 val)
  376. {
  377. writel(val, tp->regs + off);
  378. readl(tp->regs + off);
  379. }
  380. static u32 tg3_read_indirect_reg32(struct tg3 *tp, u32 off)
  381. {
  382. unsigned long flags;
  383. u32 val;
  384. spin_lock_irqsave(&tp->indirect_lock, flags);
  385. pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
  386. pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
  387. spin_unlock_irqrestore(&tp->indirect_lock, flags);
  388. return val;
  389. }
  390. static void tg3_write_indirect_mbox(struct tg3 *tp, u32 off, u32 val)
  391. {
  392. unsigned long flags;
  393. if (off == (MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW)) {
  394. pci_write_config_dword(tp->pdev, TG3PCI_RCV_RET_RING_CON_IDX +
  395. TG3_64BIT_REG_LOW, val);
  396. return;
  397. }
  398. if (off == TG3_RX_STD_PROD_IDX_REG) {
  399. pci_write_config_dword(tp->pdev, TG3PCI_STD_RING_PROD_IDX +
  400. TG3_64BIT_REG_LOW, val);
  401. return;
  402. }
  403. spin_lock_irqsave(&tp->indirect_lock, flags);
  404. pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
  405. pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
  406. spin_unlock_irqrestore(&tp->indirect_lock, flags);
  407. /* In indirect mode when disabling interrupts, we also need
  408. * to clear the interrupt bit in the GRC local ctrl register.
  409. */
  410. if ((off == (MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW)) &&
  411. (val == 0x1)) {
  412. pci_write_config_dword(tp->pdev, TG3PCI_MISC_LOCAL_CTRL,
  413. tp->grc_local_ctrl|GRC_LCLCTRL_CLEARINT);
  414. }
  415. }
  416. static u32 tg3_read_indirect_mbox(struct tg3 *tp, u32 off)
  417. {
  418. unsigned long flags;
  419. u32 val;
  420. spin_lock_irqsave(&tp->indirect_lock, flags);
  421. pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
  422. pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
  423. spin_unlock_irqrestore(&tp->indirect_lock, flags);
  424. return val;
  425. }
  426. /* usec_wait specifies the wait time in usec when writing to certain registers
  427. * where it is unsafe to read back the register without some delay.
  428. * GRC_LOCAL_CTRL is one example if the GPIOs are toggled to switch power.
  429. * TG3PCI_CLOCK_CTRL is another example if the clock frequencies are changed.
  430. */
  431. static void _tw32_flush(struct tg3 *tp, u32 off, u32 val, u32 usec_wait)
  432. {
  433. if (tg3_flag(tp, PCIX_TARGET_HWBUG) || tg3_flag(tp, ICH_WORKAROUND))
  434. /* Non-posted methods */
  435. tp->write32(tp, off, val);
  436. else {
  437. /* Posted method */
  438. tg3_write32(tp, off, val);
  439. if (usec_wait)
  440. udelay(usec_wait);
  441. tp->read32(tp, off);
  442. }
  443. /* Wait again after the read for the posted method to guarantee that
  444. * the wait time is met.
  445. */
  446. if (usec_wait)
  447. udelay(usec_wait);
  448. }
  449. static inline void tw32_mailbox_flush(struct tg3 *tp, u32 off, u32 val)
  450. {
  451. tp->write32_mbox(tp, off, val);
  452. if (!tg3_flag(tp, MBOX_WRITE_REORDER) && !tg3_flag(tp, ICH_WORKAROUND))
  453. tp->read32_mbox(tp, off);
  454. }
  455. static void tg3_write32_tx_mbox(struct tg3 *tp, u32 off, u32 val)
  456. {
  457. void __iomem *mbox = tp->regs + off;
  458. writel(val, mbox);
  459. if (tg3_flag(tp, TXD_MBOX_HWBUG))
  460. writel(val, mbox);
  461. if (tg3_flag(tp, MBOX_WRITE_REORDER))
  462. readl(mbox);
  463. }
  464. static u32 tg3_read32_mbox_5906(struct tg3 *tp, u32 off)
  465. {
  466. return readl(tp->regs + off + GRCMBOX_BASE);
  467. }
  468. static void tg3_write32_mbox_5906(struct tg3 *tp, u32 off, u32 val)
  469. {
  470. writel(val, tp->regs + off + GRCMBOX_BASE);
  471. }
  472. #define tw32_mailbox(reg, val) tp->write32_mbox(tp, reg, val)
  473. #define tw32_mailbox_f(reg, val) tw32_mailbox_flush(tp, (reg), (val))
  474. #define tw32_rx_mbox(reg, val) tp->write32_rx_mbox(tp, reg, val)
  475. #define tw32_tx_mbox(reg, val) tp->write32_tx_mbox(tp, reg, val)
  476. #define tr32_mailbox(reg) tp->read32_mbox(tp, reg)
  477. #define tw32(reg, val) tp->write32(tp, reg, val)
  478. #define tw32_f(reg, val) _tw32_flush(tp, (reg), (val), 0)
  479. #define tw32_wait_f(reg, val, us) _tw32_flush(tp, (reg), (val), (us))
  480. #define tr32(reg) tp->read32(tp, reg)
  481. static void tg3_write_mem(struct tg3 *tp, u32 off, u32 val)
  482. {
  483. unsigned long flags;
  484. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 &&
  485. (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC))
  486. return;
  487. spin_lock_irqsave(&tp->indirect_lock, flags);
  488. if (tg3_flag(tp, SRAM_USE_CONFIG)) {
  489. pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
  490. pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
  491. /* Always leave this as zero. */
  492. pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
  493. } else {
  494. tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
  495. tw32_f(TG3PCI_MEM_WIN_DATA, val);
  496. /* Always leave this as zero. */
  497. tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
  498. }
  499. spin_unlock_irqrestore(&tp->indirect_lock, flags);
  500. }
  501. static void tg3_read_mem(struct tg3 *tp, u32 off, u32 *val)
  502. {
  503. unsigned long flags;
  504. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 &&
  505. (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC)) {
  506. *val = 0;
  507. return;
  508. }
  509. spin_lock_irqsave(&tp->indirect_lock, flags);
  510. if (tg3_flag(tp, SRAM_USE_CONFIG)) {
  511. pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
  512. pci_read_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
  513. /* Always leave this as zero. */
  514. pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
  515. } else {
  516. tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
  517. *val = tr32(TG3PCI_MEM_WIN_DATA);
  518. /* Always leave this as zero. */
  519. tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
  520. }
  521. spin_unlock_irqrestore(&tp->indirect_lock, flags);
  522. }
  523. static void tg3_ape_lock_init(struct tg3 *tp)
  524. {
  525. int i;
  526. u32 regbase;
  527. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
  528. regbase = TG3_APE_LOCK_GRANT;
  529. else
  530. regbase = TG3_APE_PER_LOCK_GRANT;
  531. /* Make sure the driver hasn't any stale locks. */
  532. for (i = 0; i < 8; i++)
  533. tg3_ape_write32(tp, regbase + 4 * i, APE_LOCK_GRANT_DRIVER);
  534. }
  535. static int tg3_ape_lock(struct tg3 *tp, int locknum)
  536. {
  537. int i, off;
  538. int ret = 0;
  539. u32 status, req, gnt;
  540. if (!tg3_flag(tp, ENABLE_APE))
  541. return 0;
  542. switch (locknum) {
  543. case TG3_APE_LOCK_GRC:
  544. case TG3_APE_LOCK_MEM:
  545. break;
  546. default:
  547. return -EINVAL;
  548. }
  549. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
  550. req = TG3_APE_LOCK_REQ;
  551. gnt = TG3_APE_LOCK_GRANT;
  552. } else {
  553. req = TG3_APE_PER_LOCK_REQ;
  554. gnt = TG3_APE_PER_LOCK_GRANT;
  555. }
  556. off = 4 * locknum;
  557. tg3_ape_write32(tp, req + off, APE_LOCK_REQ_DRIVER);
  558. /* Wait for up to 1 millisecond to acquire lock. */
  559. for (i = 0; i < 100; i++) {
  560. status = tg3_ape_read32(tp, gnt + off);
  561. if (status == APE_LOCK_GRANT_DRIVER)
  562. break;
  563. udelay(10);
  564. }
  565. if (status != APE_LOCK_GRANT_DRIVER) {
  566. /* Revoke the lock request. */
  567. tg3_ape_write32(tp, gnt + off,
  568. APE_LOCK_GRANT_DRIVER);
  569. ret = -EBUSY;
  570. }
  571. return ret;
  572. }
  573. static void tg3_ape_unlock(struct tg3 *tp, int locknum)
  574. {
  575. u32 gnt;
  576. if (!tg3_flag(tp, ENABLE_APE))
  577. return;
  578. switch (locknum) {
  579. case TG3_APE_LOCK_GRC:
  580. case TG3_APE_LOCK_MEM:
  581. break;
  582. default:
  583. return;
  584. }
  585. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
  586. gnt = TG3_APE_LOCK_GRANT;
  587. else
  588. gnt = TG3_APE_PER_LOCK_GRANT;
  589. tg3_ape_write32(tp, gnt + 4 * locknum, APE_LOCK_GRANT_DRIVER);
  590. }
  591. static void tg3_disable_ints(struct tg3 *tp)
  592. {
  593. int i;
  594. tw32(TG3PCI_MISC_HOST_CTRL,
  595. (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
  596. for (i = 0; i < tp->irq_max; i++)
  597. tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
  598. }
  599. static void tg3_enable_ints(struct tg3 *tp)
  600. {
  601. int i;
  602. tp->irq_sync = 0;
  603. wmb();
  604. tw32(TG3PCI_MISC_HOST_CTRL,
  605. (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
  606. tp->coal_now = tp->coalesce_mode | HOSTCC_MODE_ENABLE;
  607. for (i = 0; i < tp->irq_cnt; i++) {
  608. struct tg3_napi *tnapi = &tp->napi[i];
  609. tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
  610. if (tg3_flag(tp, 1SHOT_MSI))
  611. tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
  612. tp->coal_now |= tnapi->coal_now;
  613. }
  614. /* Force an initial interrupt */
  615. if (!tg3_flag(tp, TAGGED_STATUS) &&
  616. (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
  617. tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
  618. else
  619. tw32(HOSTCC_MODE, tp->coal_now);
  620. tp->coal_now &= ~(tp->napi[0].coal_now | tp->napi[1].coal_now);
  621. }
  622. static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
  623. {
  624. struct tg3 *tp = tnapi->tp;
  625. struct tg3_hw_status *sblk = tnapi->hw_status;
  626. unsigned int work_exists = 0;
  627. /* check for phy events */
  628. if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
  629. if (sblk->status & SD_STATUS_LINK_CHG)
  630. work_exists = 1;
  631. }
  632. /* check for RX/TX work to do */
  633. if (sblk->idx[0].tx_consumer != tnapi->tx_cons ||
  634. *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
  635. work_exists = 1;
  636. return work_exists;
  637. }
  638. /* tg3_int_reenable
  639. * similar to tg3_enable_ints, but it accurately determines whether there
  640. * is new work pending and can return without flushing the PIO write
  641. * which reenables interrupts
  642. */
  643. static void tg3_int_reenable(struct tg3_napi *tnapi)
  644. {
  645. struct tg3 *tp = tnapi->tp;
  646. tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
  647. mmiowb();
  648. /* When doing tagged status, this work check is unnecessary.
  649. * The last_tag we write above tells the chip which piece of
  650. * work we've completed.
  651. */
  652. if (!tg3_flag(tp, TAGGED_STATUS) && tg3_has_work(tnapi))
  653. tw32(HOSTCC_MODE, tp->coalesce_mode |
  654. HOSTCC_MODE_ENABLE | tnapi->coal_now);
  655. }
  656. static void tg3_switch_clocks(struct tg3 *tp)
  657. {
  658. u32 clock_ctrl;
  659. u32 orig_clock_ctrl;
  660. if (tg3_flag(tp, CPMU_PRESENT) || tg3_flag(tp, 5780_CLASS))
  661. return;
  662. clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
  663. orig_clock_ctrl = clock_ctrl;
  664. clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
  665. CLOCK_CTRL_CLKRUN_OENABLE |
  666. 0x1f);
  667. tp->pci_clock_ctrl = clock_ctrl;
  668. if (tg3_flag(tp, 5705_PLUS)) {
  669. if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
  670. tw32_wait_f(TG3PCI_CLOCK_CTRL,
  671. clock_ctrl | CLOCK_CTRL_625_CORE, 40);
  672. }
  673. } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
  674. tw32_wait_f(TG3PCI_CLOCK_CTRL,
  675. clock_ctrl |
  676. (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
  677. 40);
  678. tw32_wait_f(TG3PCI_CLOCK_CTRL,
  679. clock_ctrl | (CLOCK_CTRL_ALTCLK),
  680. 40);
  681. }
  682. tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
  683. }
  684. #define PHY_BUSY_LOOPS 5000
  685. static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
  686. {
  687. u32 frame_val;
  688. unsigned int loops;
  689. int ret;
  690. if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
  691. tw32_f(MAC_MI_MODE,
  692. (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
  693. udelay(80);
  694. }
  695. *val = 0x0;
  696. frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
  697. MI_COM_PHY_ADDR_MASK);
  698. frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
  699. MI_COM_REG_ADDR_MASK);
  700. frame_val |= (MI_COM_CMD_READ | MI_COM_START);
  701. tw32_f(MAC_MI_COM, frame_val);
  702. loops = PHY_BUSY_LOOPS;
  703. while (loops != 0) {
  704. udelay(10);
  705. frame_val = tr32(MAC_MI_COM);
  706. if ((frame_val & MI_COM_BUSY) == 0) {
  707. udelay(5);
  708. frame_val = tr32(MAC_MI_COM);
  709. break;
  710. }
  711. loops -= 1;
  712. }
  713. ret = -EBUSY;
  714. if (loops != 0) {
  715. *val = frame_val & MI_COM_DATA_MASK;
  716. ret = 0;
  717. }
  718. if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
  719. tw32_f(MAC_MI_MODE, tp->mi_mode);
  720. udelay(80);
  721. }
  722. return ret;
  723. }
  724. static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
  725. {
  726. u32 frame_val;
  727. unsigned int loops;
  728. int ret;
  729. if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
  730. (reg == MII_TG3_CTRL || reg == MII_TG3_AUX_CTRL))
  731. return 0;
  732. if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
  733. tw32_f(MAC_MI_MODE,
  734. (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
  735. udelay(80);
  736. }
  737. frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
  738. MI_COM_PHY_ADDR_MASK);
  739. frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
  740. MI_COM_REG_ADDR_MASK);
  741. frame_val |= (val & MI_COM_DATA_MASK);
  742. frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
  743. tw32_f(MAC_MI_COM, frame_val);
  744. loops = PHY_BUSY_LOOPS;
  745. while (loops != 0) {
  746. udelay(10);
  747. frame_val = tr32(MAC_MI_COM);
  748. if ((frame_val & MI_COM_BUSY) == 0) {
  749. udelay(5);
  750. frame_val = tr32(MAC_MI_COM);
  751. break;
  752. }
  753. loops -= 1;
  754. }
  755. ret = -EBUSY;
  756. if (loops != 0)
  757. ret = 0;
  758. if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
  759. tw32_f(MAC_MI_MODE, tp->mi_mode);
  760. udelay(80);
  761. }
  762. return ret;
  763. }
  764. static int tg3_phy_cl45_write(struct tg3 *tp, u32 devad, u32 addr, u32 val)
  765. {
  766. int err;
  767. err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
  768. if (err)
  769. goto done;
  770. err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
  771. if (err)
  772. goto done;
  773. err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
  774. MII_TG3_MMD_CTRL_DATA_NOINC | devad);
  775. if (err)
  776. goto done;
  777. err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, val);
  778. done:
  779. return err;
  780. }
  781. static int tg3_phy_cl45_read(struct tg3 *tp, u32 devad, u32 addr, u32 *val)
  782. {
  783. int err;
  784. err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
  785. if (err)
  786. goto done;
  787. err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
  788. if (err)
  789. goto done;
  790. err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
  791. MII_TG3_MMD_CTRL_DATA_NOINC | devad);
  792. if (err)
  793. goto done;
  794. err = tg3_readphy(tp, MII_TG3_MMD_ADDRESS, val);
  795. done:
  796. return err;
  797. }
  798. static int tg3_phydsp_read(struct tg3 *tp, u32 reg, u32 *val)
  799. {
  800. int err;
  801. err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
  802. if (!err)
  803. err = tg3_readphy(tp, MII_TG3_DSP_RW_PORT, val);
  804. return err;
  805. }
  806. static int tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
  807. {
  808. int err;
  809. err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
  810. if (!err)
  811. err = tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
  812. return err;
  813. }
  814. static int tg3_phy_auxctl_read(struct tg3 *tp, int reg, u32 *val)
  815. {
  816. int err;
  817. err = tg3_writephy(tp, MII_TG3_AUX_CTRL,
  818. (reg << MII_TG3_AUXCTL_MISC_RDSEL_SHIFT) |
  819. MII_TG3_AUXCTL_SHDWSEL_MISC);
  820. if (!err)
  821. err = tg3_readphy(tp, MII_TG3_AUX_CTRL, val);
  822. return err;
  823. }
  824. static int tg3_phy_auxctl_write(struct tg3 *tp, int reg, u32 set)
  825. {
  826. if (reg == MII_TG3_AUXCTL_SHDWSEL_MISC)
  827. set |= MII_TG3_AUXCTL_MISC_WREN;
  828. return tg3_writephy(tp, MII_TG3_AUX_CTRL, set | reg);
  829. }
  830. #define TG3_PHY_AUXCTL_SMDSP_ENABLE(tp) \
  831. tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL, \
  832. MII_TG3_AUXCTL_ACTL_SMDSP_ENA | \
  833. MII_TG3_AUXCTL_ACTL_TX_6DB)
  834. #define TG3_PHY_AUXCTL_SMDSP_DISABLE(tp) \
  835. tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL, \
  836. MII_TG3_AUXCTL_ACTL_TX_6DB);
  837. static int tg3_bmcr_reset(struct tg3 *tp)
  838. {
  839. u32 phy_control;
  840. int limit, err;
  841. /* OK, reset it, and poll the BMCR_RESET bit until it
  842. * clears or we time out.
  843. */
  844. phy_control = BMCR_RESET;
  845. err = tg3_writephy(tp, MII_BMCR, phy_control);
  846. if (err != 0)
  847. return -EBUSY;
  848. limit = 5000;
  849. while (limit--) {
  850. err = tg3_readphy(tp, MII_BMCR, &phy_control);
  851. if (err != 0)
  852. return -EBUSY;
  853. if ((phy_control & BMCR_RESET) == 0) {
  854. udelay(40);
  855. break;
  856. }
  857. udelay(10);
  858. }
  859. if (limit < 0)
  860. return -EBUSY;
  861. return 0;
  862. }
  863. static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
  864. {
  865. struct tg3 *tp = bp->priv;
  866. u32 val;
  867. spin_lock_bh(&tp->lock);
  868. if (tg3_readphy(tp, reg, &val))
  869. val = -EIO;
  870. spin_unlock_bh(&tp->lock);
  871. return val;
  872. }
  873. static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
  874. {
  875. struct tg3 *tp = bp->priv;
  876. u32 ret = 0;
  877. spin_lock_bh(&tp->lock);
  878. if (tg3_writephy(tp, reg, val))
  879. ret = -EIO;
  880. spin_unlock_bh(&tp->lock);
  881. return ret;
  882. }
  883. static int tg3_mdio_reset(struct mii_bus *bp)
  884. {
  885. return 0;
  886. }
  887. static void tg3_mdio_config_5785(struct tg3 *tp)
  888. {
  889. u32 val;
  890. struct phy_device *phydev;
  891. phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
  892. switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
  893. case PHY_ID_BCM50610:
  894. case PHY_ID_BCM50610M:
  895. val = MAC_PHYCFG2_50610_LED_MODES;
  896. break;
  897. case PHY_ID_BCMAC131:
  898. val = MAC_PHYCFG2_AC131_LED_MODES;
  899. break;
  900. case PHY_ID_RTL8211C:
  901. val = MAC_PHYCFG2_RTL8211C_LED_MODES;
  902. break;
  903. case PHY_ID_RTL8201E:
  904. val = MAC_PHYCFG2_RTL8201E_LED_MODES;
  905. break;
  906. default:
  907. return;
  908. }
  909. if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
  910. tw32(MAC_PHYCFG2, val);
  911. val = tr32(MAC_PHYCFG1);
  912. val &= ~(MAC_PHYCFG1_RGMII_INT |
  913. MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
  914. val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
  915. tw32(MAC_PHYCFG1, val);
  916. return;
  917. }
  918. if (!tg3_flag(tp, RGMII_INBAND_DISABLE))
  919. val |= MAC_PHYCFG2_EMODE_MASK_MASK |
  920. MAC_PHYCFG2_FMODE_MASK_MASK |
  921. MAC_PHYCFG2_GMODE_MASK_MASK |
  922. MAC_PHYCFG2_ACT_MASK_MASK |
  923. MAC_PHYCFG2_QUAL_MASK_MASK |
  924. MAC_PHYCFG2_INBAND_ENABLE;
  925. tw32(MAC_PHYCFG2, val);
  926. val = tr32(MAC_PHYCFG1);
  927. val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
  928. MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
  929. if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
  930. if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
  931. val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
  932. if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
  933. val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
  934. }
  935. val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
  936. MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
  937. tw32(MAC_PHYCFG1, val);
  938. val = tr32(MAC_EXT_RGMII_MODE);
  939. val &= ~(MAC_RGMII_MODE_RX_INT_B |
  940. MAC_RGMII_MODE_RX_QUALITY |
  941. MAC_RGMII_MODE_RX_ACTIVITY |
  942. MAC_RGMII_MODE_RX_ENG_DET |
  943. MAC_RGMII_MODE_TX_ENABLE |
  944. MAC_RGMII_MODE_TX_LOWPWR |
  945. MAC_RGMII_MODE_TX_RESET);
  946. if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
  947. if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
  948. val |= MAC_RGMII_MODE_RX_INT_B |
  949. MAC_RGMII_MODE_RX_QUALITY |
  950. MAC_RGMII_MODE_RX_ACTIVITY |
  951. MAC_RGMII_MODE_RX_ENG_DET;
  952. if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
  953. val |= MAC_RGMII_MODE_TX_ENABLE |
  954. MAC_RGMII_MODE_TX_LOWPWR |
  955. MAC_RGMII_MODE_TX_RESET;
  956. }
  957. tw32(MAC_EXT_RGMII_MODE, val);
  958. }
  959. static void tg3_mdio_start(struct tg3 *tp)
  960. {
  961. tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
  962. tw32_f(MAC_MI_MODE, tp->mi_mode);
  963. udelay(80);
  964. if (tg3_flag(tp, MDIOBUS_INITED) &&
  965. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
  966. tg3_mdio_config_5785(tp);
  967. }
  968. static int tg3_mdio_init(struct tg3 *tp)
  969. {
  970. int i;
  971. u32 reg;
  972. struct phy_device *phydev;
  973. if (tg3_flag(tp, 5717_PLUS)) {
  974. u32 is_serdes;
  975. tp->phy_addr = PCI_FUNC(tp->pdev->devfn) + 1;
  976. if (tp->pci_chip_rev_id != CHIPREV_ID_5717_A0)
  977. is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
  978. else
  979. is_serdes = tr32(TG3_CPMU_PHY_STRAP) &
  980. TG3_CPMU_PHY_STRAP_IS_SERDES;
  981. if (is_serdes)
  982. tp->phy_addr += 7;
  983. } else
  984. tp->phy_addr = TG3_PHY_MII_ADDR;
  985. tg3_mdio_start(tp);
  986. if (!tg3_flag(tp, USE_PHYLIB) || tg3_flag(tp, MDIOBUS_INITED))
  987. return 0;
  988. tp->mdio_bus = mdiobus_alloc();
  989. if (tp->mdio_bus == NULL)
  990. return -ENOMEM;
  991. tp->mdio_bus->name = "tg3 mdio bus";
  992. snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
  993. (tp->pdev->bus->number << 8) | tp->pdev->devfn);
  994. tp->mdio_bus->priv = tp;
  995. tp->mdio_bus->parent = &tp->pdev->dev;
  996. tp->mdio_bus->read = &tg3_mdio_read;
  997. tp->mdio_bus->write = &tg3_mdio_write;
  998. tp->mdio_bus->reset = &tg3_mdio_reset;
  999. tp->mdio_bus->phy_mask = ~(1 << TG3_PHY_MII_ADDR);
  1000. tp->mdio_bus->irq = &tp->mdio_irq[0];
  1001. for (i = 0; i < PHY_MAX_ADDR; i++)
  1002. tp->mdio_bus->irq[i] = PHY_POLL;
  1003. /* The bus registration will look for all the PHYs on the mdio bus.
  1004. * Unfortunately, it does not ensure the PHY is powered up before
  1005. * accessing the PHY ID registers. A chip reset is the
  1006. * quickest way to bring the device back to an operational state..
  1007. */
  1008. if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
  1009. tg3_bmcr_reset(tp);
  1010. i = mdiobus_register(tp->mdio_bus);
  1011. if (i) {
  1012. dev_warn(&tp->pdev->dev, "mdiobus_reg failed (0x%x)\n", i);
  1013. mdiobus_free(tp->mdio_bus);
  1014. return i;
  1015. }
  1016. phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
  1017. if (!phydev || !phydev->drv) {
  1018. dev_warn(&tp->pdev->dev, "No PHY devices\n");
  1019. mdiobus_unregister(tp->mdio_bus);
  1020. mdiobus_free(tp->mdio_bus);
  1021. return -ENODEV;
  1022. }
  1023. switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
  1024. case PHY_ID_BCM57780:
  1025. phydev->interface = PHY_INTERFACE_MODE_GMII;
  1026. phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
  1027. break;
  1028. case PHY_ID_BCM50610:
  1029. case PHY_ID_BCM50610M:
  1030. phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
  1031. PHY_BRCM_RX_REFCLK_UNUSED |
  1032. PHY_BRCM_DIS_TXCRXC_NOENRGY |
  1033. PHY_BRCM_AUTO_PWRDWN_ENABLE;
  1034. if (tg3_flag(tp, RGMII_INBAND_DISABLE))
  1035. phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
  1036. if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
  1037. phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
  1038. if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
  1039. phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
  1040. /* fallthru */
  1041. case PHY_ID_RTL8211C:
  1042. phydev->interface = PHY_INTERFACE_MODE_RGMII;
  1043. break;
  1044. case PHY_ID_RTL8201E:
  1045. case PHY_ID_BCMAC131:
  1046. phydev->interface = PHY_INTERFACE_MODE_MII;
  1047. phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
  1048. tp->phy_flags |= TG3_PHYFLG_IS_FET;
  1049. break;
  1050. }
  1051. tg3_flag_set(tp, MDIOBUS_INITED);
  1052. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
  1053. tg3_mdio_config_5785(tp);
  1054. return 0;
  1055. }
  1056. static void tg3_mdio_fini(struct tg3 *tp)
  1057. {
  1058. if (tg3_flag(tp, MDIOBUS_INITED)) {
  1059. tg3_flag_clear(tp, MDIOBUS_INITED);
  1060. mdiobus_unregister(tp->mdio_bus);
  1061. mdiobus_free(tp->mdio_bus);
  1062. }
  1063. }
  1064. /* tp->lock is held. */
  1065. static inline void tg3_generate_fw_event(struct tg3 *tp)
  1066. {
  1067. u32 val;
  1068. val = tr32(GRC_RX_CPU_EVENT);
  1069. val |= GRC_RX_CPU_DRIVER_EVENT;
  1070. tw32_f(GRC_RX_CPU_EVENT, val);
  1071. tp->last_event_jiffies = jiffies;
  1072. }
  1073. #define TG3_FW_EVENT_TIMEOUT_USEC 2500
  1074. /* tp->lock is held. */
  1075. static void tg3_wait_for_event_ack(struct tg3 *tp)
  1076. {
  1077. int i;
  1078. unsigned int delay_cnt;
  1079. long time_remain;
  1080. /* If enough time has passed, no wait is necessary. */
  1081. time_remain = (long)(tp->last_event_jiffies + 1 +
  1082. usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
  1083. (long)jiffies;
  1084. if (time_remain < 0)
  1085. return;
  1086. /* Check if we can shorten the wait time. */
  1087. delay_cnt = jiffies_to_usecs(time_remain);
  1088. if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
  1089. delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
  1090. delay_cnt = (delay_cnt >> 3) + 1;
  1091. for (i = 0; i < delay_cnt; i++) {
  1092. if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
  1093. break;
  1094. udelay(8);
  1095. }
  1096. }
  1097. /* tp->lock is held. */
  1098. static void tg3_ump_link_report(struct tg3 *tp)
  1099. {
  1100. u32 reg;
  1101. u32 val;
  1102. if (!tg3_flag(tp, 5780_CLASS) || !tg3_flag(tp, ENABLE_ASF))
  1103. return;
  1104. tg3_wait_for_event_ack(tp);
  1105. tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
  1106. tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
  1107. val = 0;
  1108. if (!tg3_readphy(tp, MII_BMCR, &reg))
  1109. val = reg << 16;
  1110. if (!tg3_readphy(tp, MII_BMSR, &reg))
  1111. val |= (reg & 0xffff);
  1112. tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX, val);
  1113. val = 0;
  1114. if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
  1115. val = reg << 16;
  1116. if (!tg3_readphy(tp, MII_LPA, &reg))
  1117. val |= (reg & 0xffff);
  1118. tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 4, val);
  1119. val = 0;
  1120. if (!(tp->phy_flags & TG3_PHYFLG_MII_SERDES)) {
  1121. if (!tg3_readphy(tp, MII_CTRL1000, &reg))
  1122. val = reg << 16;
  1123. if (!tg3_readphy(tp, MII_STAT1000, &reg))
  1124. val |= (reg & 0xffff);
  1125. }
  1126. tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 8, val);
  1127. if (!tg3_readphy(tp, MII_PHYADDR, &reg))
  1128. val = reg << 16;
  1129. else
  1130. val = 0;
  1131. tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 12, val);
  1132. tg3_generate_fw_event(tp);
  1133. }
  1134. static void tg3_link_report(struct tg3 *tp)
  1135. {
  1136. if (!netif_carrier_ok(tp->dev)) {
  1137. netif_info(tp, link, tp->dev, "Link is down\n");
  1138. tg3_ump_link_report(tp);
  1139. } else if (netif_msg_link(tp)) {
  1140. netdev_info(tp->dev, "Link is up at %d Mbps, %s duplex\n",
  1141. (tp->link_config.active_speed == SPEED_1000 ?
  1142. 1000 :
  1143. (tp->link_config.active_speed == SPEED_100 ?
  1144. 100 : 10)),
  1145. (tp->link_config.active_duplex == DUPLEX_FULL ?
  1146. "full" : "half"));
  1147. netdev_info(tp->dev, "Flow control is %s for TX and %s for RX\n",
  1148. (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
  1149. "on" : "off",
  1150. (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
  1151. "on" : "off");
  1152. if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
  1153. netdev_info(tp->dev, "EEE is %s\n",
  1154. tp->setlpicnt ? "enabled" : "disabled");
  1155. tg3_ump_link_report(tp);
  1156. }
  1157. }
  1158. static u16 tg3_advert_flowctrl_1000T(u8 flow_ctrl)
  1159. {
  1160. u16 miireg;
  1161. if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
  1162. miireg = ADVERTISE_PAUSE_CAP;
  1163. else if (flow_ctrl & FLOW_CTRL_TX)
  1164. miireg = ADVERTISE_PAUSE_ASYM;
  1165. else if (flow_ctrl & FLOW_CTRL_RX)
  1166. miireg = ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
  1167. else
  1168. miireg = 0;
  1169. return miireg;
  1170. }
  1171. static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
  1172. {
  1173. u16 miireg;
  1174. if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
  1175. miireg = ADVERTISE_1000XPAUSE;
  1176. else if (flow_ctrl & FLOW_CTRL_TX)
  1177. miireg = ADVERTISE_1000XPSE_ASYM;
  1178. else if (flow_ctrl & FLOW_CTRL_RX)
  1179. miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
  1180. else
  1181. miireg = 0;
  1182. return miireg;
  1183. }
  1184. static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
  1185. {
  1186. u8 cap = 0;
  1187. if (lcladv & ADVERTISE_1000XPAUSE) {
  1188. if (lcladv & ADVERTISE_1000XPSE_ASYM) {
  1189. if (rmtadv & LPA_1000XPAUSE)
  1190. cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
  1191. else if (rmtadv & LPA_1000XPAUSE_ASYM)
  1192. cap = FLOW_CTRL_RX;
  1193. } else {
  1194. if (rmtadv & LPA_1000XPAUSE)
  1195. cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
  1196. }
  1197. } else if (lcladv & ADVERTISE_1000XPSE_ASYM) {
  1198. if ((rmtadv & LPA_1000XPAUSE) && (rmtadv & LPA_1000XPAUSE_ASYM))
  1199. cap = FLOW_CTRL_TX;
  1200. }
  1201. return cap;
  1202. }
  1203. static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
  1204. {
  1205. u8 autoneg;
  1206. u8 flowctrl = 0;
  1207. u32 old_rx_mode = tp->rx_mode;
  1208. u32 old_tx_mode = tp->tx_mode;
  1209. if (tg3_flag(tp, USE_PHYLIB))
  1210. autoneg = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]->autoneg;
  1211. else
  1212. autoneg = tp->link_config.autoneg;
  1213. if (autoneg == AUTONEG_ENABLE && tg3_flag(tp, PAUSE_AUTONEG)) {
  1214. if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
  1215. flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
  1216. else
  1217. flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
  1218. } else
  1219. flowctrl = tp->link_config.flowctrl;
  1220. tp->link_config.active_flowctrl = flowctrl;
  1221. if (flowctrl & FLOW_CTRL_RX)
  1222. tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
  1223. else
  1224. tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
  1225. if (old_rx_mode != tp->rx_mode)
  1226. tw32_f(MAC_RX_MODE, tp->rx_mode);
  1227. if (flowctrl & FLOW_CTRL_TX)
  1228. tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
  1229. else
  1230. tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
  1231. if (old_tx_mode != tp->tx_mode)
  1232. tw32_f(MAC_TX_MODE, tp->tx_mode);
  1233. }
  1234. static void tg3_adjust_link(struct net_device *dev)
  1235. {
  1236. u8 oldflowctrl, linkmesg = 0;
  1237. u32 mac_mode, lcl_adv, rmt_adv;
  1238. struct tg3 *tp = netdev_priv(dev);
  1239. struct phy_device *phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
  1240. spin_lock_bh(&tp->lock);
  1241. mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
  1242. MAC_MODE_HALF_DUPLEX);
  1243. oldflowctrl = tp->link_config.active_flowctrl;
  1244. if (phydev->link) {
  1245. lcl_adv = 0;
  1246. rmt_adv = 0;
  1247. if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
  1248. mac_mode |= MAC_MODE_PORT_MODE_MII;
  1249. else if (phydev->speed == SPEED_1000 ||
  1250. GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785)
  1251. mac_mode |= MAC_MODE_PORT_MODE_GMII;
  1252. else
  1253. mac_mode |= MAC_MODE_PORT_MODE_MII;
  1254. if (phydev->duplex == DUPLEX_HALF)
  1255. mac_mode |= MAC_MODE_HALF_DUPLEX;
  1256. else {
  1257. lcl_adv = tg3_advert_flowctrl_1000T(
  1258. tp->link_config.flowctrl);
  1259. if (phydev->pause)
  1260. rmt_adv = LPA_PAUSE_CAP;
  1261. if (phydev->asym_pause)
  1262. rmt_adv |= LPA_PAUSE_ASYM;
  1263. }
  1264. tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
  1265. } else
  1266. mac_mode |= MAC_MODE_PORT_MODE_GMII;
  1267. if (mac_mode != tp->mac_mode) {
  1268. tp->mac_mode = mac_mode;
  1269. tw32_f(MAC_MODE, tp->mac_mode);
  1270. udelay(40);
  1271. }
  1272. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
  1273. if (phydev->speed == SPEED_10)
  1274. tw32(MAC_MI_STAT,
  1275. MAC_MI_STAT_10MBPS_MODE |
  1276. MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
  1277. else
  1278. tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
  1279. }
  1280. if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
  1281. tw32(MAC_TX_LENGTHS,
  1282. ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
  1283. (6 << TX_LENGTHS_IPG_SHIFT) |
  1284. (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
  1285. else
  1286. tw32(MAC_TX_LENGTHS,
  1287. ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
  1288. (6 << TX_LENGTHS_IPG_SHIFT) |
  1289. (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
  1290. if ((phydev->link && tp->link_config.active_speed == SPEED_INVALID) ||
  1291. (!phydev->link && tp->link_config.active_speed != SPEED_INVALID) ||
  1292. phydev->speed != tp->link_config.active_speed ||
  1293. phydev->duplex != tp->link_config.active_duplex ||
  1294. oldflowctrl != tp->link_config.active_flowctrl)
  1295. linkmesg = 1;
  1296. tp->link_config.active_speed = phydev->speed;
  1297. tp->link_config.active_duplex = phydev->duplex;
  1298. spin_unlock_bh(&tp->lock);
  1299. if (linkmesg)
  1300. tg3_link_report(tp);
  1301. }
  1302. static int tg3_phy_init(struct tg3 *tp)
  1303. {
  1304. struct phy_device *phydev;
  1305. if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)
  1306. return 0;
  1307. /* Bring the PHY back to a known state. */
  1308. tg3_bmcr_reset(tp);
  1309. phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
  1310. /* Attach the MAC to the PHY. */
  1311. phydev = phy_connect(tp->dev, dev_name(&phydev->dev), tg3_adjust_link,
  1312. phydev->dev_flags, phydev->interface);
  1313. if (IS_ERR(phydev)) {
  1314. dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
  1315. return PTR_ERR(phydev);
  1316. }
  1317. /* Mask with MAC supported features. */
  1318. switch (phydev->interface) {
  1319. case PHY_INTERFACE_MODE_GMII:
  1320. case PHY_INTERFACE_MODE_RGMII:
  1321. if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
  1322. phydev->supported &= (PHY_GBIT_FEATURES |
  1323. SUPPORTED_Pause |
  1324. SUPPORTED_Asym_Pause);
  1325. break;
  1326. }
  1327. /* fallthru */
  1328. case PHY_INTERFACE_MODE_MII:
  1329. phydev->supported &= (PHY_BASIC_FEATURES |
  1330. SUPPORTED_Pause |
  1331. SUPPORTED_Asym_Pause);
  1332. break;
  1333. default:
  1334. phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
  1335. return -EINVAL;
  1336. }
  1337. tp->phy_flags |= TG3_PHYFLG_IS_CONNECTED;
  1338. phydev->advertising = phydev->supported;
  1339. return 0;
  1340. }
  1341. static void tg3_phy_start(struct tg3 *tp)
  1342. {
  1343. struct phy_device *phydev;
  1344. if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
  1345. return;
  1346. phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
  1347. if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
  1348. tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
  1349. phydev->speed = tp->link_config.orig_speed;
  1350. phydev->duplex = tp->link_config.orig_duplex;
  1351. phydev->autoneg = tp->link_config.orig_autoneg;
  1352. phydev->advertising = tp->link_config.orig_advertising;
  1353. }
  1354. phy_start(phydev);
  1355. phy_start_aneg(phydev);
  1356. }
  1357. static void tg3_phy_stop(struct tg3 *tp)
  1358. {
  1359. if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
  1360. return;
  1361. phy_stop(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
  1362. }
  1363. static void tg3_phy_fini(struct tg3 *tp)
  1364. {
  1365. if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
  1366. phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
  1367. tp->phy_flags &= ~TG3_PHYFLG_IS_CONNECTED;
  1368. }
  1369. }
  1370. static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
  1371. {
  1372. u32 phytest;
  1373. if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
  1374. u32 phy;
  1375. tg3_writephy(tp, MII_TG3_FET_TEST,
  1376. phytest | MII_TG3_FET_SHADOW_EN);
  1377. if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
  1378. if (enable)
  1379. phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
  1380. else
  1381. phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
  1382. tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
  1383. }
  1384. tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
  1385. }
  1386. }
  1387. static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
  1388. {
  1389. u32 reg;
  1390. if (!tg3_flag(tp, 5705_PLUS) ||
  1391. (tg3_flag(tp, 5717_PLUS) &&
  1392. (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
  1393. return;
  1394. if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
  1395. tg3_phy_fet_toggle_apd(tp, enable);
  1396. return;
  1397. }
  1398. reg = MII_TG3_MISC_SHDW_WREN |
  1399. MII_TG3_MISC_SHDW_SCR5_SEL |
  1400. MII_TG3_MISC_SHDW_SCR5_LPED |
  1401. MII_TG3_MISC_SHDW_SCR5_DLPTLM |
  1402. MII_TG3_MISC_SHDW_SCR5_SDTL |
  1403. MII_TG3_MISC_SHDW_SCR5_C125OE;
  1404. if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 || !enable)
  1405. reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
  1406. tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
  1407. reg = MII_TG3_MISC_SHDW_WREN |
  1408. MII_TG3_MISC_SHDW_APD_SEL |
  1409. MII_TG3_MISC_SHDW_APD_WKTM_84MS;
  1410. if (enable)
  1411. reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
  1412. tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
  1413. }
  1414. static void tg3_phy_toggle_automdix(struct tg3 *tp, int enable)
  1415. {
  1416. u32 phy;
  1417. if (!tg3_flag(tp, 5705_PLUS) ||
  1418. (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
  1419. return;
  1420. if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
  1421. u32 ephy;
  1422. if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
  1423. u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
  1424. tg3_writephy(tp, MII_TG3_FET_TEST,
  1425. ephy | MII_TG3_FET_SHADOW_EN);
  1426. if (!tg3_readphy(tp, reg, &phy)) {
  1427. if (enable)
  1428. phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
  1429. else
  1430. phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
  1431. tg3_writephy(tp, reg, phy);
  1432. }
  1433. tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
  1434. }
  1435. } else {
  1436. int ret;
  1437. ret = tg3_phy_auxctl_read(tp,
  1438. MII_TG3_AUXCTL_SHDWSEL_MISC, &phy);
  1439. if (!ret) {
  1440. if (enable)
  1441. phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
  1442. else
  1443. phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
  1444. tg3_phy_auxctl_write(tp,
  1445. MII_TG3_AUXCTL_SHDWSEL_MISC, phy);
  1446. }
  1447. }
  1448. }
  1449. static void tg3_phy_set_wirespeed(struct tg3 *tp)
  1450. {
  1451. int ret;
  1452. u32 val;
  1453. if (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED)
  1454. return;
  1455. ret = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_MISC, &val);
  1456. if (!ret)
  1457. tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_MISC,
  1458. val | MII_TG3_AUXCTL_MISC_WIRESPD_EN);
  1459. }
  1460. static void tg3_phy_apply_otp(struct tg3 *tp)
  1461. {
  1462. u32 otp, phy;
  1463. if (!tp->phy_otp)
  1464. return;
  1465. otp = tp->phy_otp;
  1466. if (TG3_PHY_AUXCTL_SMDSP_ENABLE(tp))
  1467. return;
  1468. phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
  1469. phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
  1470. tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
  1471. phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
  1472. ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
  1473. tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
  1474. phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
  1475. phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
  1476. tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
  1477. phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
  1478. tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
  1479. phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
  1480. tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
  1481. phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
  1482. ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
  1483. tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
  1484. TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
  1485. }
  1486. static void tg3_phy_eee_adjust(struct tg3 *tp, u32 current_link_up)
  1487. {
  1488. u32 val;
  1489. if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
  1490. return;
  1491. tp->setlpicnt = 0;
  1492. if (tp->link_config.autoneg == AUTONEG_ENABLE &&
  1493. current_link_up == 1 &&
  1494. tp->link_config.active_duplex == DUPLEX_FULL &&
  1495. (tp->link_config.active_speed == SPEED_100 ||
  1496. tp->link_config.active_speed == SPEED_1000)) {
  1497. u32 eeectl;
  1498. if (tp->link_config.active_speed == SPEED_1000)
  1499. eeectl = TG3_CPMU_EEE_CTRL_EXIT_16_5_US;
  1500. else
  1501. eeectl = TG3_CPMU_EEE_CTRL_EXIT_36_US;
  1502. tw32(TG3_CPMU_EEE_CTRL, eeectl);
  1503. tg3_phy_cl45_read(tp, MDIO_MMD_AN,
  1504. TG3_CL45_D7_EEERES_STAT, &val);
  1505. if (val == TG3_CL45_D7_EEERES_STAT_LP_1000T ||
  1506. val == TG3_CL45_D7_EEERES_STAT_LP_100TX)
  1507. tp->setlpicnt = 2;
  1508. }
  1509. if (!tp->setlpicnt) {
  1510. val = tr32(TG3_CPMU_EEE_MODE);
  1511. tw32(TG3_CPMU_EEE_MODE, val & ~TG3_CPMU_EEEMD_LPI_ENABLE);
  1512. }
  1513. }
  1514. static void tg3_phy_eee_enable(struct tg3 *tp)
  1515. {
  1516. u32 val;
  1517. if (tp->link_config.active_speed == SPEED_1000 &&
  1518. (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
  1519. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
  1520. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) &&
  1521. !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
  1522. tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, 0x0003);
  1523. TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
  1524. }
  1525. val = tr32(TG3_CPMU_EEE_MODE);
  1526. tw32(TG3_CPMU_EEE_MODE, val | TG3_CPMU_EEEMD_LPI_ENABLE);
  1527. }
  1528. static int tg3_wait_macro_done(struct tg3 *tp)
  1529. {
  1530. int limit = 100;
  1531. while (limit--) {
  1532. u32 tmp32;
  1533. if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
  1534. if ((tmp32 & 0x1000) == 0)
  1535. break;
  1536. }
  1537. }
  1538. if (limit < 0)
  1539. return -EBUSY;
  1540. return 0;
  1541. }
  1542. static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
  1543. {
  1544. static const u32 test_pat[4][6] = {
  1545. { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
  1546. { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
  1547. { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
  1548. { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
  1549. };
  1550. int chan;
  1551. for (chan = 0; chan < 4; chan++) {
  1552. int i;
  1553. tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
  1554. (chan * 0x2000) | 0x0200);
  1555. tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
  1556. for (i = 0; i < 6; i++)
  1557. tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
  1558. test_pat[chan][i]);
  1559. tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
  1560. if (tg3_wait_macro_done(tp)) {
  1561. *resetp = 1;
  1562. return -EBUSY;
  1563. }
  1564. tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
  1565. (chan * 0x2000) | 0x0200);
  1566. tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
  1567. if (tg3_wait_macro_done(tp)) {
  1568. *resetp = 1;
  1569. return -EBUSY;
  1570. }
  1571. tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
  1572. if (tg3_wait_macro_done(tp)) {
  1573. *resetp = 1;
  1574. return -EBUSY;
  1575. }
  1576. for (i = 0; i < 6; i += 2) {
  1577. u32 low, high;
  1578. if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
  1579. tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
  1580. tg3_wait_macro_done(tp)) {
  1581. *resetp = 1;
  1582. return -EBUSY;
  1583. }
  1584. low &= 0x7fff;
  1585. high &= 0x000f;
  1586. if (low != test_pat[chan][i] ||
  1587. high != test_pat[chan][i+1]) {
  1588. tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
  1589. tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
  1590. tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
  1591. return -EBUSY;
  1592. }
  1593. }
  1594. }
  1595. return 0;
  1596. }
  1597. static int tg3_phy_reset_chanpat(struct tg3 *tp)
  1598. {
  1599. int chan;
  1600. for (chan = 0; chan < 4; chan++) {
  1601. int i;
  1602. tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
  1603. (chan * 0x2000) | 0x0200);
  1604. tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
  1605. for (i = 0; i < 6; i++)
  1606. tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
  1607. tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
  1608. if (tg3_wait_macro_done(tp))
  1609. return -EBUSY;
  1610. }
  1611. return 0;
  1612. }
  1613. static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
  1614. {
  1615. u32 reg32, phy9_orig;
  1616. int retries, do_phy_reset, err;
  1617. retries = 10;
  1618. do_phy_reset = 1;
  1619. do {
  1620. if (do_phy_reset) {
  1621. err = tg3_bmcr_reset(tp);
  1622. if (err)
  1623. return err;
  1624. do_phy_reset = 0;
  1625. }
  1626. /* Disable transmitter and interrupt. */
  1627. if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
  1628. continue;
  1629. reg32 |= 0x3000;
  1630. tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
  1631. /* Set full-duplex, 1000 mbps. */
  1632. tg3_writephy(tp, MII_BMCR,
  1633. BMCR_FULLDPLX | TG3_BMCR_SPEED1000);
  1634. /* Set to master mode. */
  1635. if (tg3_readphy(tp, MII_TG3_CTRL, &phy9_orig))
  1636. continue;
  1637. tg3_writephy(tp, MII_TG3_CTRL,
  1638. (MII_TG3_CTRL_AS_MASTER |
  1639. MII_TG3_CTRL_ENABLE_AS_MASTER));
  1640. err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
  1641. if (err)
  1642. return err;
  1643. /* Block the PHY control access. */
  1644. tg3_phydsp_write(tp, 0x8005, 0x0800);
  1645. err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
  1646. if (!err)
  1647. break;
  1648. } while (--retries);
  1649. err = tg3_phy_reset_chanpat(tp);
  1650. if (err)
  1651. return err;
  1652. tg3_phydsp_write(tp, 0x8005, 0x0000);
  1653. tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
  1654. tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0000);
  1655. TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
  1656. tg3_writephy(tp, MII_TG3_CTRL, phy9_orig);
  1657. if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
  1658. reg32 &= ~0x3000;
  1659. tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
  1660. } else if (!err)
  1661. err = -EBUSY;
  1662. return err;
  1663. }
  1664. /* This will reset the tigon3 PHY if there is no valid
  1665. * link unless the FORCE argument is non-zero.
  1666. */
  1667. static int tg3_phy_reset(struct tg3 *tp)
  1668. {
  1669. u32 val, cpmuctrl;
  1670. int err;
  1671. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
  1672. val = tr32(GRC_MISC_CFG);
  1673. tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
  1674. udelay(40);
  1675. }
  1676. err = tg3_readphy(tp, MII_BMSR, &val);
  1677. err |= tg3_readphy(tp, MII_BMSR, &val);
  1678. if (err != 0)
  1679. return -EBUSY;
  1680. if (netif_running(tp->dev) && netif_carrier_ok(tp->dev)) {
  1681. netif_carrier_off(tp->dev);
  1682. tg3_link_report(tp);
  1683. }
  1684. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
  1685. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
  1686. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
  1687. err = tg3_phy_reset_5703_4_5(tp);
  1688. if (err)
  1689. return err;
  1690. goto out;
  1691. }
  1692. cpmuctrl = 0;
  1693. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
  1694. GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
  1695. cpmuctrl = tr32(TG3_CPMU_CTRL);
  1696. if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
  1697. tw32(TG3_CPMU_CTRL,
  1698. cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
  1699. }
  1700. err = tg3_bmcr_reset(tp);
  1701. if (err)
  1702. return err;
  1703. if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
  1704. val = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
  1705. tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, val);
  1706. tw32(TG3_CPMU_CTRL, cpmuctrl);
  1707. }
  1708. if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
  1709. GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
  1710. val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
  1711. if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
  1712. CPMU_LSPD_1000MB_MACCLK_12_5) {
  1713. val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
  1714. udelay(40);
  1715. tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
  1716. }
  1717. }
  1718. if (tg3_flag(tp, 5717_PLUS) &&
  1719. (tp->phy_flags & TG3_PHYFLG_MII_SERDES))
  1720. return 0;
  1721. tg3_phy_apply_otp(tp);
  1722. if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
  1723. tg3_phy_toggle_apd(tp, true);
  1724. else
  1725. tg3_phy_toggle_apd(tp, false);
  1726. out:
  1727. if ((tp->phy_flags & TG3_PHYFLG_ADC_BUG) &&
  1728. !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
  1729. tg3_phydsp_write(tp, 0x201f, 0x2aaa);
  1730. tg3_phydsp_write(tp, 0x000a, 0x0323);
  1731. TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
  1732. }
  1733. if (tp->phy_flags & TG3_PHYFLG_5704_A0_BUG) {
  1734. tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
  1735. tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
  1736. }
  1737. if (tp->phy_flags & TG3_PHYFLG_BER_BUG) {
  1738. if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
  1739. tg3_phydsp_write(tp, 0x000a, 0x310b);
  1740. tg3_phydsp_write(tp, 0x201f, 0x9506);
  1741. tg3_phydsp_write(tp, 0x401f, 0x14e2);
  1742. TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
  1743. }
  1744. } else if (tp->phy_flags & TG3_PHYFLG_JITTER_BUG) {
  1745. if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
  1746. tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
  1747. if (tp->phy_flags & TG3_PHYFLG_ADJUST_TRIM) {
  1748. tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
  1749. tg3_writephy(tp, MII_TG3_TEST1,
  1750. MII_TG3_TEST1_TRIM_EN | 0x4);
  1751. } else
  1752. tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
  1753. TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
  1754. }
  1755. }
  1756. /* Set Extended packet length bit (bit 14) on all chips that */
  1757. /* support jumbo frames */
  1758. if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
  1759. /* Cannot do read-modify-write on 5401 */
  1760. tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
  1761. } else if (tg3_flag(tp, JUMBO_CAPABLE)) {
  1762. /* Set bit 14 with read-modify-write to preserve other bits */
  1763. err = tg3_phy_auxctl_read(tp,
  1764. MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
  1765. if (!err)
  1766. tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
  1767. val | MII_TG3_AUXCTL_ACTL_EXTPKTLEN);
  1768. }
  1769. /* Set phy register 0x10 bit 0 to high fifo elasticity to support
  1770. * jumbo frames transmission.
  1771. */
  1772. if (tg3_flag(tp, JUMBO_CAPABLE)) {
  1773. if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &val))
  1774. tg3_writephy(tp, MII_TG3_EXT_CTRL,
  1775. val | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
  1776. }
  1777. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
  1778. /* adjust output voltage */
  1779. tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
  1780. }
  1781. tg3_phy_toggle_automdix(tp, 1);
  1782. tg3_phy_set_wirespeed(tp);
  1783. return 0;
  1784. }
  1785. static void tg3_frob_aux_power(struct tg3 *tp)
  1786. {
  1787. bool need_vaux = false;
  1788. /* The GPIOs do something completely different on 57765. */
  1789. if (!tg3_flag(tp, IS_NIC) ||
  1790. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
  1791. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
  1792. return;
  1793. if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
  1794. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
  1795. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
  1796. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) &&
  1797. tp->pdev_peer != tp->pdev) {
  1798. struct net_device *dev_peer;
  1799. dev_peer = pci_get_drvdata(tp->pdev_peer);
  1800. /* remove_one() may have been run on the peer. */
  1801. if (dev_peer) {
  1802. struct tg3 *tp_peer = netdev_priv(dev_peer);
  1803. if (tg3_flag(tp_peer, INIT_COMPLETE))
  1804. return;
  1805. if (tg3_flag(tp_peer, WOL_ENABLE) ||
  1806. tg3_flag(tp_peer, ENABLE_ASF))
  1807. need_vaux = true;
  1808. }
  1809. }
  1810. if (tg3_flag(tp, WOL_ENABLE) || tg3_flag(tp, ENABLE_ASF))
  1811. need_vaux = true;
  1812. if (need_vaux) {
  1813. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
  1814. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
  1815. tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
  1816. (GRC_LCLCTRL_GPIO_OE0 |
  1817. GRC_LCLCTRL_GPIO_OE1 |
  1818. GRC_LCLCTRL_GPIO_OE2 |
  1819. GRC_LCLCTRL_GPIO_OUTPUT0 |
  1820. GRC_LCLCTRL_GPIO_OUTPUT1),
  1821. 100);
  1822. } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
  1823. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
  1824. /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
  1825. u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
  1826. GRC_LCLCTRL_GPIO_OE1 |
  1827. GRC_LCLCTRL_GPIO_OE2 |
  1828. GRC_LCLCTRL_GPIO_OUTPUT0 |
  1829. GRC_LCLCTRL_GPIO_OUTPUT1 |
  1830. tp->grc_local_ctrl;
  1831. tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
  1832. grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
  1833. tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
  1834. grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
  1835. tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
  1836. } else {
  1837. u32 no_gpio2;
  1838. u32 grc_local_ctrl = 0;
  1839. /* Workaround to prevent overdrawing Amps. */
  1840. if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
  1841. ASIC_REV_5714) {
  1842. grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
  1843. tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
  1844. grc_local_ctrl, 100);
  1845. }
  1846. /* On 5753 and variants, GPIO2 cannot be used. */
  1847. no_gpio2 = tp->nic_sram_data_cfg &
  1848. NIC_SRAM_DATA_CFG_NO_GPIO2;
  1849. grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
  1850. GRC_LCLCTRL_GPIO_OE1 |
  1851. GRC_LCLCTRL_GPIO_OE2 |
  1852. GRC_LCLCTRL_GPIO_OUTPUT1 |
  1853. GRC_LCLCTRL_GPIO_OUTPUT2;
  1854. if (no_gpio2) {
  1855. grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
  1856. GRC_LCLCTRL_GPIO_OUTPUT2);
  1857. }
  1858. tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
  1859. grc_local_ctrl, 100);
  1860. grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
  1861. tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
  1862. grc_local_ctrl, 100);
  1863. if (!no_gpio2) {
  1864. grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
  1865. tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
  1866. grc_local_ctrl, 100);
  1867. }
  1868. }
  1869. } else {
  1870. if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
  1871. GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
  1872. tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
  1873. (GRC_LCLCTRL_GPIO_OE1 |
  1874. GRC_LCLCTRL_GPIO_OUTPUT1), 100);
  1875. tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
  1876. GRC_LCLCTRL_GPIO_OE1, 100);
  1877. tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
  1878. (GRC_LCLCTRL_GPIO_OE1 |
  1879. GRC_LCLCTRL_GPIO_OUTPUT1), 100);
  1880. }
  1881. }
  1882. }
  1883. static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
  1884. {
  1885. if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
  1886. return 1;
  1887. else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
  1888. if (speed != SPEED_10)
  1889. return 1;
  1890. } else if (speed == SPEED_10)
  1891. return 1;
  1892. return 0;
  1893. }
  1894. static int tg3_setup_phy(struct tg3 *, int);
  1895. #define RESET_KIND_SHUTDOWN 0
  1896. #define RESET_KIND_INIT 1
  1897. #define RESET_KIND_SUSPEND 2
  1898. static void tg3_write_sig_post_reset(struct tg3 *, int);
  1899. static int tg3_halt_cpu(struct tg3 *, u32);
  1900. static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
  1901. {
  1902. u32 val;
  1903. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
  1904. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
  1905. u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
  1906. u32 serdes_cfg = tr32(MAC_SERDES_CFG);
  1907. sg_dig_ctrl |=
  1908. SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
  1909. tw32(SG_DIG_CTRL, sg_dig_ctrl);
  1910. tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
  1911. }
  1912. return;
  1913. }
  1914. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
  1915. tg3_bmcr_reset(tp);
  1916. val = tr32(GRC_MISC_CFG);
  1917. tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
  1918. udelay(40);
  1919. return;
  1920. } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
  1921. u32 phytest;
  1922. if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
  1923. u32 phy;
  1924. tg3_writephy(tp, MII_ADVERTISE, 0);
  1925. tg3_writephy(tp, MII_BMCR,
  1926. BMCR_ANENABLE | BMCR_ANRESTART);
  1927. tg3_writephy(tp, MII_TG3_FET_TEST,
  1928. phytest | MII_TG3_FET_SHADOW_EN);
  1929. if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
  1930. phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
  1931. tg3_writephy(tp,
  1932. MII_TG3_FET_SHDW_AUXMODE4,
  1933. phy);
  1934. }
  1935. tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
  1936. }
  1937. return;
  1938. } else if (do_low_power) {
  1939. tg3_writephy(tp, MII_TG3_EXT_CTRL,
  1940. MII_TG3_EXT_CTRL_FORCE_LED_OFF);
  1941. val = MII_TG3_AUXCTL_PCTL_100TX_LPWR |
  1942. MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
  1943. MII_TG3_AUXCTL_PCTL_VREG_11V;
  1944. tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, val);
  1945. }
  1946. /* The PHY should not be powered down on some chips because
  1947. * of bugs.
  1948. */
  1949. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
  1950. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
  1951. (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 &&
  1952. (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
  1953. return;
  1954. if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
  1955. GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
  1956. val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
  1957. val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
  1958. val |= CPMU_LSPD_1000MB_MACCLK_12_5;
  1959. tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
  1960. }
  1961. tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
  1962. }
  1963. /* tp->lock is held. */
  1964. static int tg3_nvram_lock(struct tg3 *tp)
  1965. {
  1966. if (tg3_flag(tp, NVRAM)) {
  1967. int i;
  1968. if (tp->nvram_lock_cnt == 0) {
  1969. tw32(NVRAM_SWARB, SWARB_REQ_SET1);
  1970. for (i = 0; i < 8000; i++) {
  1971. if (tr32(NVRAM_SWARB) & SWARB_GNT1)
  1972. break;
  1973. udelay(20);
  1974. }
  1975. if (i == 8000) {
  1976. tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
  1977. return -ENODEV;
  1978. }
  1979. }
  1980. tp->nvram_lock_cnt++;
  1981. }
  1982. return 0;
  1983. }
  1984. /* tp->lock is held. */
  1985. static void tg3_nvram_unlock(struct tg3 *tp)
  1986. {
  1987. if (tg3_flag(tp, NVRAM)) {
  1988. if (tp->nvram_lock_cnt > 0)
  1989. tp->nvram_lock_cnt--;
  1990. if (tp->nvram_lock_cnt == 0)
  1991. tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
  1992. }
  1993. }
  1994. /* tp->lock is held. */
  1995. static void tg3_enable_nvram_access(struct tg3 *tp)
  1996. {
  1997. if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
  1998. u32 nvaccess = tr32(NVRAM_ACCESS);
  1999. tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
  2000. }
  2001. }
  2002. /* tp->lock is held. */
  2003. static void tg3_disable_nvram_access(struct tg3 *tp)
  2004. {
  2005. if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
  2006. u32 nvaccess = tr32(NVRAM_ACCESS);
  2007. tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
  2008. }
  2009. }
  2010. static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
  2011. u32 offset, u32 *val)
  2012. {
  2013. u32 tmp;
  2014. int i;
  2015. if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
  2016. return -EINVAL;
  2017. tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
  2018. EEPROM_ADDR_DEVID_MASK |
  2019. EEPROM_ADDR_READ);
  2020. tw32(GRC_EEPROM_ADDR,
  2021. tmp |
  2022. (0 << EEPROM_ADDR_DEVID_SHIFT) |
  2023. ((offset << EEPROM_ADDR_ADDR_SHIFT) &
  2024. EEPROM_ADDR_ADDR_MASK) |
  2025. EEPROM_ADDR_READ | EEPROM_ADDR_START);
  2026. for (i = 0; i < 1000; i++) {
  2027. tmp = tr32(GRC_EEPROM_ADDR);
  2028. if (tmp & EEPROM_ADDR_COMPLETE)
  2029. break;
  2030. msleep(1);
  2031. }
  2032. if (!(tmp & EEPROM_ADDR_COMPLETE))
  2033. return -EBUSY;
  2034. tmp = tr32(GRC_EEPROM_DATA);
  2035. /*
  2036. * The data will always be opposite the native endian
  2037. * format. Perform a blind byteswap to compensate.
  2038. */
  2039. *val = swab32(tmp);
  2040. return 0;
  2041. }
  2042. #define NVRAM_CMD_TIMEOUT 10000
  2043. static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
  2044. {
  2045. int i;
  2046. tw32(NVRAM_CMD, nvram_cmd);
  2047. for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
  2048. udelay(10);
  2049. if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
  2050. udelay(10);
  2051. break;
  2052. }
  2053. }
  2054. if (i == NVRAM_CMD_TIMEOUT)
  2055. return -EBUSY;
  2056. return 0;
  2057. }
  2058. static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
  2059. {
  2060. if (tg3_flag(tp, NVRAM) &&
  2061. tg3_flag(tp, NVRAM_BUFFERED) &&
  2062. tg3_flag(tp, FLASH) &&
  2063. !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
  2064. (tp->nvram_jedecnum == JEDEC_ATMEL))
  2065. addr = ((addr / tp->nvram_pagesize) <<
  2066. ATMEL_AT45DB0X1B_PAGE_POS) +
  2067. (addr % tp->nvram_pagesize);
  2068. return addr;
  2069. }
  2070. static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
  2071. {
  2072. if (tg3_flag(tp, NVRAM) &&
  2073. tg3_flag(tp, NVRAM_BUFFERED) &&
  2074. tg3_flag(tp, FLASH) &&
  2075. !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
  2076. (tp->nvram_jedecnum == JEDEC_ATMEL))
  2077. addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
  2078. tp->nvram_pagesize) +
  2079. (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
  2080. return addr;
  2081. }
  2082. /* NOTE: Data read in from NVRAM is byteswapped according to
  2083. * the byteswapping settings for all other register accesses.
  2084. * tg3 devices are BE devices, so on a BE machine, the data
  2085. * returned will be exactly as it is seen in NVRAM. On a LE
  2086. * machine, the 32-bit value will be byteswapped.
  2087. */
  2088. static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
  2089. {
  2090. int ret;
  2091. if (!tg3_flag(tp, NVRAM))
  2092. return tg3_nvram_read_using_eeprom(tp, offset, val);
  2093. offset = tg3_nvram_phys_addr(tp, offset);
  2094. if (offset > NVRAM_ADDR_MSK)
  2095. return -EINVAL;
  2096. ret = tg3_nvram_lock(tp);
  2097. if (ret)
  2098. return ret;
  2099. tg3_enable_nvram_access(tp);
  2100. tw32(NVRAM_ADDR, offset);
  2101. ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
  2102. NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
  2103. if (ret == 0)
  2104. *val = tr32(NVRAM_RDDATA);
  2105. tg3_disable_nvram_access(tp);
  2106. tg3_nvram_unlock(tp);
  2107. return ret;
  2108. }
  2109. /* Ensures NVRAM data is in bytestream format. */
  2110. static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
  2111. {
  2112. u32 v;
  2113. int res = tg3_nvram_read(tp, offset, &v);
  2114. if (!res)
  2115. *val = cpu_to_be32(v);
  2116. return res;
  2117. }
  2118. /* tp->lock is held. */
  2119. static void __tg3_set_mac_addr(struct tg3 *tp, int skip_mac_1)
  2120. {
  2121. u32 addr_high, addr_low;
  2122. int i;
  2123. addr_high = ((tp->dev->dev_addr[0] << 8) |
  2124. tp->dev->dev_addr[1]);
  2125. addr_low = ((tp->dev->dev_addr[2] << 24) |
  2126. (tp->dev->dev_addr[3] << 16) |
  2127. (tp->dev->dev_addr[4] << 8) |
  2128. (tp->dev->dev_addr[5] << 0));
  2129. for (i = 0; i < 4; i++) {
  2130. if (i == 1 && skip_mac_1)
  2131. continue;
  2132. tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
  2133. tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
  2134. }
  2135. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
  2136. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
  2137. for (i = 0; i < 12; i++) {
  2138. tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
  2139. tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
  2140. }
  2141. }
  2142. addr_high = (tp->dev->dev_addr[0] +
  2143. tp->dev->dev_addr[1] +
  2144. tp->dev->dev_addr[2] +
  2145. tp->dev->dev_addr[3] +
  2146. tp->dev->dev_addr[4] +
  2147. tp->dev->dev_addr[5]) &
  2148. TX_BACKOFF_SEED_MASK;
  2149. tw32(MAC_TX_BACKOFF_SEED, addr_high);
  2150. }
  2151. static void tg3_enable_register_access(struct tg3 *tp)
  2152. {
  2153. /*
  2154. * Make sure register accesses (indirect or otherwise) will function
  2155. * correctly.
  2156. */
  2157. pci_write_config_dword(tp->pdev,
  2158. TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
  2159. }
  2160. static int tg3_power_up(struct tg3 *tp)
  2161. {
  2162. tg3_enable_register_access(tp);
  2163. pci_set_power_state(tp->pdev, PCI_D0);
  2164. /* Switch out of Vaux if it is a NIC */
  2165. if (tg3_flag(tp, IS_NIC))
  2166. tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl, 100);
  2167. return 0;
  2168. }
  2169. static int tg3_power_down_prepare(struct tg3 *tp)
  2170. {
  2171. u32 misc_host_ctrl;
  2172. bool device_should_wake, do_low_power;
  2173. tg3_enable_register_access(tp);
  2174. /* Restore the CLKREQ setting. */
  2175. if (tg3_flag(tp, CLKREQ_BUG)) {
  2176. u16 lnkctl;
  2177. pci_read_config_word(tp->pdev,
  2178. tp->pcie_cap + PCI_EXP_LNKCTL,
  2179. &lnkctl);
  2180. lnkctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
  2181. pci_write_config_word(tp->pdev,
  2182. tp->pcie_cap + PCI_EXP_LNKCTL,
  2183. lnkctl);
  2184. }
  2185. misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
  2186. tw32(TG3PCI_MISC_HOST_CTRL,
  2187. misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
  2188. device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
  2189. tg3_flag(tp, WOL_ENABLE);
  2190. if (tg3_flag(tp, USE_PHYLIB)) {
  2191. do_low_power = false;
  2192. if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
  2193. !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
  2194. struct phy_device *phydev;
  2195. u32 phyid, advertising;
  2196. phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
  2197. tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
  2198. tp->link_config.orig_speed = phydev->speed;
  2199. tp->link_config.orig_duplex = phydev->duplex;
  2200. tp->link_config.orig_autoneg = phydev->autoneg;
  2201. tp->link_config.orig_advertising = phydev->advertising;
  2202. advertising = ADVERTISED_TP |
  2203. ADVERTISED_Pause |
  2204. ADVERTISED_Autoneg |
  2205. ADVERTISED_10baseT_Half;
  2206. if (tg3_flag(tp, ENABLE_ASF) || device_should_wake) {
  2207. if (tg3_flag(tp, WOL_SPEED_100MB))
  2208. advertising |=
  2209. ADVERTISED_100baseT_Half |
  2210. ADVERTISED_100baseT_Full |
  2211. ADVERTISED_10baseT_Full;
  2212. else
  2213. advertising |= ADVERTISED_10baseT_Full;
  2214. }
  2215. phydev->advertising = advertising;
  2216. phy_start_aneg(phydev);
  2217. phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
  2218. if (phyid != PHY_ID_BCMAC131) {
  2219. phyid &= PHY_BCM_OUI_MASK;
  2220. if (phyid == PHY_BCM_OUI_1 ||
  2221. phyid == PHY_BCM_OUI_2 ||
  2222. phyid == PHY_BCM_OUI_3)
  2223. do_low_power = true;
  2224. }
  2225. }
  2226. } else {
  2227. do_low_power = true;
  2228. if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
  2229. tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
  2230. tp->link_config.orig_speed = tp->link_config.speed;
  2231. tp->link_config.orig_duplex = tp->link_config.duplex;
  2232. tp->link_config.orig_autoneg = tp->link_config.autoneg;
  2233. }
  2234. if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
  2235. tp->link_config.speed = SPEED_10;
  2236. tp->link_config.duplex = DUPLEX_HALF;
  2237. tp->link_config.autoneg = AUTONEG_ENABLE;
  2238. tg3_setup_phy(tp, 0);
  2239. }
  2240. }
  2241. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
  2242. u32 val;
  2243. val = tr32(GRC_VCPU_EXT_CTRL);
  2244. tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
  2245. } else if (!tg3_flag(tp, ENABLE_ASF)) {
  2246. int i;
  2247. u32 val;
  2248. for (i = 0; i < 200; i++) {
  2249. tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
  2250. if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
  2251. break;
  2252. msleep(1);
  2253. }
  2254. }
  2255. if (tg3_flag(tp, WOL_CAP))
  2256. tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
  2257. WOL_DRV_STATE_SHUTDOWN |
  2258. WOL_DRV_WOL |
  2259. WOL_SET_MAGIC_PKT);
  2260. if (device_should_wake) {
  2261. u32 mac_mode;
  2262. if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
  2263. if (do_low_power &&
  2264. !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
  2265. tg3_phy_auxctl_write(tp,
  2266. MII_TG3_AUXCTL_SHDWSEL_PWRCTL,
  2267. MII_TG3_AUXCTL_PCTL_WOL_EN |
  2268. MII_TG3_AUXCTL_PCTL_100TX_LPWR |
  2269. MII_TG3_AUXCTL_PCTL_CL_AB_TXDAC);
  2270. udelay(40);
  2271. }
  2272. if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
  2273. mac_mode = MAC_MODE_PORT_MODE_GMII;
  2274. else
  2275. mac_mode = MAC_MODE_PORT_MODE_MII;
  2276. mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
  2277. if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
  2278. ASIC_REV_5700) {
  2279. u32 speed = tg3_flag(tp, WOL_SPEED_100MB) ?
  2280. SPEED_100 : SPEED_10;
  2281. if (tg3_5700_link_polarity(tp, speed))
  2282. mac_mode |= MAC_MODE_LINK_POLARITY;
  2283. else
  2284. mac_mode &= ~MAC_MODE_LINK_POLARITY;
  2285. }
  2286. } else {
  2287. mac_mode = MAC_MODE_PORT_MODE_TBI;
  2288. }
  2289. if (!tg3_flag(tp, 5750_PLUS))
  2290. tw32(MAC_LED_CTRL, tp->led_ctrl);
  2291. mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
  2292. if ((tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS)) &&
  2293. (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)))
  2294. mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
  2295. if (tg3_flag(tp, ENABLE_APE))
  2296. mac_mode |= MAC_MODE_APE_TX_EN |
  2297. MAC_MODE_APE_RX_EN |
  2298. MAC_MODE_TDE_ENABLE;
  2299. tw32_f(MAC_MODE, mac_mode);
  2300. udelay(100);
  2301. tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
  2302. udelay(10);
  2303. }
  2304. if (!tg3_flag(tp, WOL_SPEED_100MB) &&
  2305. (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
  2306. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
  2307. u32 base_val;
  2308. base_val = tp->pci_clock_ctrl;
  2309. base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
  2310. CLOCK_CTRL_TXCLK_DISABLE);
  2311. tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
  2312. CLOCK_CTRL_PWRDOWN_PLL133, 40);
  2313. } else if (tg3_flag(tp, 5780_CLASS) ||
  2314. tg3_flag(tp, CPMU_PRESENT) ||
  2315. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
  2316. /* do nothing */
  2317. } else if (!(tg3_flag(tp, 5750_PLUS) && tg3_flag(tp, ENABLE_ASF))) {
  2318. u32 newbits1, newbits2;
  2319. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
  2320. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
  2321. newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
  2322. CLOCK_CTRL_TXCLK_DISABLE |
  2323. CLOCK_CTRL_ALTCLK);
  2324. newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
  2325. } else if (tg3_flag(tp, 5705_PLUS)) {
  2326. newbits1 = CLOCK_CTRL_625_CORE;
  2327. newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
  2328. } else {
  2329. newbits1 = CLOCK_CTRL_ALTCLK;
  2330. newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
  2331. }
  2332. tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
  2333. 40);
  2334. tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
  2335. 40);
  2336. if (!tg3_flag(tp, 5705_PLUS)) {
  2337. u32 newbits3;
  2338. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
  2339. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
  2340. newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
  2341. CLOCK_CTRL_TXCLK_DISABLE |
  2342. CLOCK_CTRL_44MHZ_CORE);
  2343. } else {
  2344. newbits3 = CLOCK_CTRL_44MHZ_CORE;
  2345. }
  2346. tw32_wait_f(TG3PCI_CLOCK_CTRL,
  2347. tp->pci_clock_ctrl | newbits3, 40);
  2348. }
  2349. }
  2350. if (!(device_should_wake) && !tg3_flag(tp, ENABLE_ASF))
  2351. tg3_power_down_phy(tp, do_low_power);
  2352. tg3_frob_aux_power(tp);
  2353. /* Workaround for unstable PLL clock */
  2354. if ((GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX) ||
  2355. (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX)) {
  2356. u32 val = tr32(0x7d00);
  2357. val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
  2358. tw32(0x7d00, val);
  2359. if (!tg3_flag(tp, ENABLE_ASF)) {
  2360. int err;
  2361. err = tg3_nvram_lock(tp);
  2362. tg3_halt_cpu(tp, RX_CPU_BASE);
  2363. if (!err)
  2364. tg3_nvram_unlock(tp);
  2365. }
  2366. }
  2367. tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
  2368. return 0;
  2369. }
  2370. static void tg3_power_down(struct tg3 *tp)
  2371. {
  2372. tg3_power_down_prepare(tp);
  2373. pci_wake_from_d3(tp->pdev, tg3_flag(tp, WOL_ENABLE));
  2374. pci_set_power_state(tp->pdev, PCI_D3hot);
  2375. }
  2376. static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
  2377. {
  2378. switch (val & MII_TG3_AUX_STAT_SPDMASK) {
  2379. case MII_TG3_AUX_STAT_10HALF:
  2380. *speed = SPEED_10;
  2381. *duplex = DUPLEX_HALF;
  2382. break;
  2383. case MII_TG3_AUX_STAT_10FULL:
  2384. *speed = SPEED_10;
  2385. *duplex = DUPLEX_FULL;
  2386. break;
  2387. case MII_TG3_AUX_STAT_100HALF:
  2388. *speed = SPEED_100;
  2389. *duplex = DUPLEX_HALF;
  2390. break;
  2391. case MII_TG3_AUX_STAT_100FULL:
  2392. *speed = SPEED_100;
  2393. *duplex = DUPLEX_FULL;
  2394. break;
  2395. case MII_TG3_AUX_STAT_1000HALF:
  2396. *speed = SPEED_1000;
  2397. *duplex = DUPLEX_HALF;
  2398. break;
  2399. case MII_TG3_AUX_STAT_1000FULL:
  2400. *speed = SPEED_1000;
  2401. *duplex = DUPLEX_FULL;
  2402. break;
  2403. default:
  2404. if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
  2405. *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
  2406. SPEED_10;
  2407. *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
  2408. DUPLEX_HALF;
  2409. break;
  2410. }
  2411. *speed = SPEED_INVALID;
  2412. *duplex = DUPLEX_INVALID;
  2413. break;
  2414. }
  2415. }
  2416. static int tg3_phy_autoneg_cfg(struct tg3 *tp, u32 advertise, u32 flowctrl)
  2417. {
  2418. int err = 0;
  2419. u32 val, new_adv;
  2420. new_adv = ADVERTISE_CSMA;
  2421. if (advertise & ADVERTISED_10baseT_Half)
  2422. new_adv |= ADVERTISE_10HALF;
  2423. if (advertise & ADVERTISED_10baseT_Full)
  2424. new_adv |= ADVERTISE_10FULL;
  2425. if (advertise & ADVERTISED_100baseT_Half)
  2426. new_adv |= ADVERTISE_100HALF;
  2427. if (advertise & ADVERTISED_100baseT_Full)
  2428. new_adv |= ADVERTISE_100FULL;
  2429. new_adv |= tg3_advert_flowctrl_1000T(flowctrl);
  2430. err = tg3_writephy(tp, MII_ADVERTISE, new_adv);
  2431. if (err)
  2432. goto done;
  2433. if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
  2434. goto done;
  2435. new_adv = 0;
  2436. if (advertise & ADVERTISED_1000baseT_Half)
  2437. new_adv |= MII_TG3_CTRL_ADV_1000_HALF;
  2438. if (advertise & ADVERTISED_1000baseT_Full)
  2439. new_adv |= MII_TG3_CTRL_ADV_1000_FULL;
  2440. if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
  2441. tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
  2442. new_adv |= (MII_TG3_CTRL_AS_MASTER |
  2443. MII_TG3_CTRL_ENABLE_AS_MASTER);
  2444. err = tg3_writephy(tp, MII_TG3_CTRL, new_adv);
  2445. if (err)
  2446. goto done;
  2447. if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
  2448. goto done;
  2449. tw32(TG3_CPMU_EEE_MODE,
  2450. tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
  2451. err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
  2452. if (!err) {
  2453. u32 err2;
  2454. switch (GET_ASIC_REV(tp->pci_chip_rev_id)) {
  2455. case ASIC_REV_5717:
  2456. case ASIC_REV_57765:
  2457. if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
  2458. tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
  2459. MII_TG3_DSP_CH34TP2_HIBW01);
  2460. /* Fall through */
  2461. case ASIC_REV_5719:
  2462. val = MII_TG3_DSP_TAP26_ALNOKO |
  2463. MII_TG3_DSP_TAP26_RMRXSTO |
  2464. MII_TG3_DSP_TAP26_OPCSINPT;
  2465. tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
  2466. }
  2467. val = 0;
  2468. /* Advertise 100-BaseTX EEE ability */
  2469. if (advertise & ADVERTISED_100baseT_Full)
  2470. val |= MDIO_AN_EEE_ADV_100TX;
  2471. /* Advertise 1000-BaseT EEE ability */
  2472. if (advertise & ADVERTISED_1000baseT_Full)
  2473. val |= MDIO_AN_EEE_ADV_1000T;
  2474. err = tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
  2475. err2 = TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
  2476. if (!err)
  2477. err = err2;
  2478. }
  2479. done:
  2480. return err;
  2481. }
  2482. static void tg3_phy_copper_begin(struct tg3 *tp)
  2483. {
  2484. u32 new_adv;
  2485. int i;
  2486. if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
  2487. new_adv = ADVERTISED_10baseT_Half |
  2488. ADVERTISED_10baseT_Full;
  2489. if (tg3_flag(tp, WOL_SPEED_100MB))
  2490. new_adv |= ADVERTISED_100baseT_Half |
  2491. ADVERTISED_100baseT_Full;
  2492. tg3_phy_autoneg_cfg(tp, new_adv,
  2493. FLOW_CTRL_TX | FLOW_CTRL_RX);
  2494. } else if (tp->link_config.speed == SPEED_INVALID) {
  2495. if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
  2496. tp->link_config.advertising &=
  2497. ~(ADVERTISED_1000baseT_Half |
  2498. ADVERTISED_1000baseT_Full);
  2499. tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
  2500. tp->link_config.flowctrl);
  2501. } else {
  2502. /* Asking for a specific link mode. */
  2503. if (tp->link_config.speed == SPEED_1000) {
  2504. if (tp->link_config.duplex == DUPLEX_FULL)
  2505. new_adv = ADVERTISED_1000baseT_Full;
  2506. else
  2507. new_adv = ADVERTISED_1000baseT_Half;
  2508. } else if (tp->link_config.speed == SPEED_100) {
  2509. if (tp->link_config.duplex == DUPLEX_FULL)
  2510. new_adv = ADVERTISED_100baseT_Full;
  2511. else
  2512. new_adv = ADVERTISED_100baseT_Half;
  2513. } else {
  2514. if (tp->link_config.duplex == DUPLEX_FULL)
  2515. new_adv = ADVERTISED_10baseT_Full;
  2516. else
  2517. new_adv = ADVERTISED_10baseT_Half;
  2518. }
  2519. tg3_phy_autoneg_cfg(tp, new_adv,
  2520. tp->link_config.flowctrl);
  2521. }
  2522. if (tp->link_config.autoneg == AUTONEG_DISABLE &&
  2523. tp->link_config.speed != SPEED_INVALID) {
  2524. u32 bmcr, orig_bmcr;
  2525. tp->link_config.active_speed = tp->link_config.speed;
  2526. tp->link_config.active_duplex = tp->link_config.duplex;
  2527. bmcr = 0;
  2528. switch (tp->link_config.speed) {
  2529. default:
  2530. case SPEED_10:
  2531. break;
  2532. case SPEED_100:
  2533. bmcr |= BMCR_SPEED100;
  2534. break;
  2535. case SPEED_1000:
  2536. bmcr |= TG3_BMCR_SPEED1000;
  2537. break;
  2538. }
  2539. if (tp->link_config.duplex == DUPLEX_FULL)
  2540. bmcr |= BMCR_FULLDPLX;
  2541. if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
  2542. (bmcr != orig_bmcr)) {
  2543. tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
  2544. for (i = 0; i < 1500; i++) {
  2545. u32 tmp;
  2546. udelay(10);
  2547. if (tg3_readphy(tp, MII_BMSR, &tmp) ||
  2548. tg3_readphy(tp, MII_BMSR, &tmp))
  2549. continue;
  2550. if (!(tmp & BMSR_LSTATUS)) {
  2551. udelay(40);
  2552. break;
  2553. }
  2554. }
  2555. tg3_writephy(tp, MII_BMCR, bmcr);
  2556. udelay(40);
  2557. }
  2558. } else {
  2559. tg3_writephy(tp, MII_BMCR,
  2560. BMCR_ANENABLE | BMCR_ANRESTART);
  2561. }
  2562. }
  2563. static int tg3_init_5401phy_dsp(struct tg3 *tp)
  2564. {
  2565. int err;
  2566. /* Turn off tap power management. */
  2567. /* Set Extended packet length bit */
  2568. err = tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
  2569. err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
  2570. err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
  2571. err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
  2572. err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
  2573. err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
  2574. udelay(40);
  2575. return err;
  2576. }
  2577. static int tg3_copper_is_advertising_all(struct tg3 *tp, u32 mask)
  2578. {
  2579. u32 adv_reg, all_mask = 0;
  2580. if (mask & ADVERTISED_10baseT_Half)
  2581. all_mask |= ADVERTISE_10HALF;
  2582. if (mask & ADVERTISED_10baseT_Full)
  2583. all_mask |= ADVERTISE_10FULL;
  2584. if (mask & ADVERTISED_100baseT_Half)
  2585. all_mask |= ADVERTISE_100HALF;
  2586. if (mask & ADVERTISED_100baseT_Full)
  2587. all_mask |= ADVERTISE_100FULL;
  2588. if (tg3_readphy(tp, MII_ADVERTISE, &adv_reg))
  2589. return 0;
  2590. if ((adv_reg & all_mask) != all_mask)
  2591. return 0;
  2592. if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
  2593. u32 tg3_ctrl;
  2594. all_mask = 0;
  2595. if (mask & ADVERTISED_1000baseT_Half)
  2596. all_mask |= ADVERTISE_1000HALF;
  2597. if (mask & ADVERTISED_1000baseT_Full)
  2598. all_mask |= ADVERTISE_1000FULL;
  2599. if (tg3_readphy(tp, MII_TG3_CTRL, &tg3_ctrl))
  2600. return 0;
  2601. if ((tg3_ctrl & all_mask) != all_mask)
  2602. return 0;
  2603. }
  2604. return 1;
  2605. }
  2606. static int tg3_adv_1000T_flowctrl_ok(struct tg3 *tp, u32 *lcladv, u32 *rmtadv)
  2607. {
  2608. u32 curadv, reqadv;
  2609. if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
  2610. return 1;
  2611. curadv = *lcladv & (ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
  2612. reqadv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
  2613. if (tp->link_config.active_duplex == DUPLEX_FULL) {
  2614. if (curadv != reqadv)
  2615. return 0;
  2616. if (tg3_flag(tp, PAUSE_AUTONEG))
  2617. tg3_readphy(tp, MII_LPA, rmtadv);
  2618. } else {
  2619. /* Reprogram the advertisement register, even if it
  2620. * does not affect the current link. If the link
  2621. * gets renegotiated in the future, we can save an
  2622. * additional renegotiation cycle by advertising
  2623. * it correctly in the first place.
  2624. */
  2625. if (curadv != reqadv) {
  2626. *lcladv &= ~(ADVERTISE_PAUSE_CAP |
  2627. ADVERTISE_PAUSE_ASYM);
  2628. tg3_writephy(tp, MII_ADVERTISE, *lcladv | reqadv);
  2629. }
  2630. }
  2631. return 1;
  2632. }
  2633. static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
  2634. {
  2635. int current_link_up;
  2636. u32 bmsr, val;
  2637. u32 lcl_adv, rmt_adv;
  2638. u16 current_speed;
  2639. u8 current_duplex;
  2640. int i, err;
  2641. tw32(MAC_EVENT, 0);
  2642. tw32_f(MAC_STATUS,
  2643. (MAC_STATUS_SYNC_CHANGED |
  2644. MAC_STATUS_CFG_CHANGED |
  2645. MAC_STATUS_MI_COMPLETION |
  2646. MAC_STATUS_LNKSTATE_CHANGED));
  2647. udelay(40);
  2648. if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
  2649. tw32_f(MAC_MI_MODE,
  2650. (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
  2651. udelay(80);
  2652. }
  2653. tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, 0);
  2654. /* Some third-party PHYs need to be reset on link going
  2655. * down.
  2656. */
  2657. if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
  2658. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
  2659. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
  2660. netif_carrier_ok(tp->dev)) {
  2661. tg3_readphy(tp, MII_BMSR, &bmsr);
  2662. if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
  2663. !(bmsr & BMSR_LSTATUS))
  2664. force_reset = 1;
  2665. }
  2666. if (force_reset)
  2667. tg3_phy_reset(tp);
  2668. if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
  2669. tg3_readphy(tp, MII_BMSR, &bmsr);
  2670. if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
  2671. !tg3_flag(tp, INIT_COMPLETE))
  2672. bmsr = 0;
  2673. if (!(bmsr & BMSR_LSTATUS)) {
  2674. err = tg3_init_5401phy_dsp(tp);
  2675. if (err)
  2676. return err;
  2677. tg3_readphy(tp, MII_BMSR, &bmsr);
  2678. for (i = 0; i < 1000; i++) {
  2679. udelay(10);
  2680. if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
  2681. (bmsr & BMSR_LSTATUS)) {
  2682. udelay(40);
  2683. break;
  2684. }
  2685. }
  2686. if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
  2687. TG3_PHY_REV_BCM5401_B0 &&
  2688. !(bmsr & BMSR_LSTATUS) &&
  2689. tp->link_config.active_speed == SPEED_1000) {
  2690. err = tg3_phy_reset(tp);
  2691. if (!err)
  2692. err = tg3_init_5401phy_dsp(tp);
  2693. if (err)
  2694. return err;
  2695. }
  2696. }
  2697. } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
  2698. tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
  2699. /* 5701 {A0,B0} CRC bug workaround */
  2700. tg3_writephy(tp, 0x15, 0x0a75);
  2701. tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
  2702. tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
  2703. tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
  2704. }
  2705. /* Clear pending interrupts... */
  2706. tg3_readphy(tp, MII_TG3_ISTAT, &val);
  2707. tg3_readphy(tp, MII_TG3_ISTAT, &val);
  2708. if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
  2709. tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
  2710. else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
  2711. tg3_writephy(tp, MII_TG3_IMASK, ~0);
  2712. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
  2713. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
  2714. if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
  2715. tg3_writephy(tp, MII_TG3_EXT_CTRL,
  2716. MII_TG3_EXT_CTRL_LNK3_LED_MODE);
  2717. else
  2718. tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
  2719. }
  2720. current_link_up = 0;
  2721. current_speed = SPEED_INVALID;
  2722. current_duplex = DUPLEX_INVALID;
  2723. if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
  2724. err = tg3_phy_auxctl_read(tp,
  2725. MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
  2726. &val);
  2727. if (!err && !(val & (1 << 10))) {
  2728. tg3_phy_auxctl_write(tp,
  2729. MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
  2730. val | (1 << 10));
  2731. goto relink;
  2732. }
  2733. }
  2734. bmsr = 0;
  2735. for (i = 0; i < 100; i++) {
  2736. tg3_readphy(tp, MII_BMSR, &bmsr);
  2737. if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
  2738. (bmsr & BMSR_LSTATUS))
  2739. break;
  2740. udelay(40);
  2741. }
  2742. if (bmsr & BMSR_LSTATUS) {
  2743. u32 aux_stat, bmcr;
  2744. tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
  2745. for (i = 0; i < 2000; i++) {
  2746. udelay(10);
  2747. if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
  2748. aux_stat)
  2749. break;
  2750. }
  2751. tg3_aux_stat_to_speed_duplex(tp, aux_stat,
  2752. &current_speed,
  2753. &current_duplex);
  2754. bmcr = 0;
  2755. for (i = 0; i < 200; i++) {
  2756. tg3_readphy(tp, MII_BMCR, &bmcr);
  2757. if (tg3_readphy(tp, MII_BMCR, &bmcr))
  2758. continue;
  2759. if (bmcr && bmcr != 0x7fff)
  2760. break;
  2761. udelay(10);
  2762. }
  2763. lcl_adv = 0;
  2764. rmt_adv = 0;
  2765. tp->link_config.active_speed = current_speed;
  2766. tp->link_config.active_duplex = current_duplex;
  2767. if (tp->link_config.autoneg == AUTONEG_ENABLE) {
  2768. if ((bmcr & BMCR_ANENABLE) &&
  2769. tg3_copper_is_advertising_all(tp,
  2770. tp->link_config.advertising)) {
  2771. if (tg3_adv_1000T_flowctrl_ok(tp, &lcl_adv,
  2772. &rmt_adv))
  2773. current_link_up = 1;
  2774. }
  2775. } else {
  2776. if (!(bmcr & BMCR_ANENABLE) &&
  2777. tp->link_config.speed == current_speed &&
  2778. tp->link_config.duplex == current_duplex &&
  2779. tp->link_config.flowctrl ==
  2780. tp->link_config.active_flowctrl) {
  2781. current_link_up = 1;
  2782. }
  2783. }
  2784. if (current_link_up == 1 &&
  2785. tp->link_config.active_duplex == DUPLEX_FULL)
  2786. tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
  2787. }
  2788. relink:
  2789. if (current_link_up == 0 || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
  2790. tg3_phy_copper_begin(tp);
  2791. tg3_readphy(tp, MII_BMSR, &bmsr);
  2792. if ((!tg3_readphy(tp, MII_BMSR, &bmsr) && (bmsr & BMSR_LSTATUS)) ||
  2793. (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
  2794. current_link_up = 1;
  2795. }
  2796. tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
  2797. if (current_link_up == 1) {
  2798. if (tp->link_config.active_speed == SPEED_100 ||
  2799. tp->link_config.active_speed == SPEED_10)
  2800. tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
  2801. else
  2802. tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
  2803. } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
  2804. tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
  2805. else
  2806. tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
  2807. tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
  2808. if (tp->link_config.active_duplex == DUPLEX_HALF)
  2809. tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
  2810. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
  2811. if (current_link_up == 1 &&
  2812. tg3_5700_link_polarity(tp, tp->link_config.active_speed))
  2813. tp->mac_mode |= MAC_MODE_LINK_POLARITY;
  2814. else
  2815. tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
  2816. }
  2817. /* ??? Without this setting Netgear GA302T PHY does not
  2818. * ??? send/receive packets...
  2819. */
  2820. if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
  2821. tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
  2822. tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
  2823. tw32_f(MAC_MI_MODE, tp->mi_mode);
  2824. udelay(80);
  2825. }
  2826. tw32_f(MAC_MODE, tp->mac_mode);
  2827. udelay(40);
  2828. tg3_phy_eee_adjust(tp, current_link_up);
  2829. if (tg3_flag(tp, USE_LINKCHG_REG)) {
  2830. /* Polled via timer. */
  2831. tw32_f(MAC_EVENT, 0);
  2832. } else {
  2833. tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
  2834. }
  2835. udelay(40);
  2836. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
  2837. current_link_up == 1 &&
  2838. tp->link_config.active_speed == SPEED_1000 &&
  2839. (tg3_flag(tp, PCIX_MODE) || tg3_flag(tp, PCI_HIGH_SPEED))) {
  2840. udelay(120);
  2841. tw32_f(MAC_STATUS,
  2842. (MAC_STATUS_SYNC_CHANGED |
  2843. MAC_STATUS_CFG_CHANGED));
  2844. udelay(40);
  2845. tg3_write_mem(tp,
  2846. NIC_SRAM_FIRMWARE_MBOX,
  2847. NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
  2848. }
  2849. /* Prevent send BD corruption. */
  2850. if (tg3_flag(tp, CLKREQ_BUG)) {
  2851. u16 oldlnkctl, newlnkctl;
  2852. pci_read_config_word(tp->pdev,
  2853. tp->pcie_cap + PCI_EXP_LNKCTL,
  2854. &oldlnkctl);
  2855. if (tp->link_config.active_speed == SPEED_100 ||
  2856. tp->link_config.active_speed == SPEED_10)
  2857. newlnkctl = oldlnkctl & ~PCI_EXP_LNKCTL_CLKREQ_EN;
  2858. else
  2859. newlnkctl = oldlnkctl | PCI_EXP_LNKCTL_CLKREQ_EN;
  2860. if (newlnkctl != oldlnkctl)
  2861. pci_write_config_word(tp->pdev,
  2862. tp->pcie_cap + PCI_EXP_LNKCTL,
  2863. newlnkctl);
  2864. }
  2865. if (current_link_up != netif_carrier_ok(tp->dev)) {
  2866. if (current_link_up)
  2867. netif_carrier_on(tp->dev);
  2868. else
  2869. netif_carrier_off(tp->dev);
  2870. tg3_link_report(tp);
  2871. }
  2872. return 0;
  2873. }
  2874. struct tg3_fiber_aneginfo {
  2875. int state;
  2876. #define ANEG_STATE_UNKNOWN 0
  2877. #define ANEG_STATE_AN_ENABLE 1
  2878. #define ANEG_STATE_RESTART_INIT 2
  2879. #define ANEG_STATE_RESTART 3
  2880. #define ANEG_STATE_DISABLE_LINK_OK 4
  2881. #define ANEG_STATE_ABILITY_DETECT_INIT 5
  2882. #define ANEG_STATE_ABILITY_DETECT 6
  2883. #define ANEG_STATE_ACK_DETECT_INIT 7
  2884. #define ANEG_STATE_ACK_DETECT 8
  2885. #define ANEG_STATE_COMPLETE_ACK_INIT 9
  2886. #define ANEG_STATE_COMPLETE_ACK 10
  2887. #define ANEG_STATE_IDLE_DETECT_INIT 11
  2888. #define ANEG_STATE_IDLE_DETECT 12
  2889. #define ANEG_STATE_LINK_OK 13
  2890. #define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
  2891. #define ANEG_STATE_NEXT_PAGE_WAIT 15
  2892. u32 flags;
  2893. #define MR_AN_ENABLE 0x00000001
  2894. #define MR_RESTART_AN 0x00000002
  2895. #define MR_AN_COMPLETE 0x00000004
  2896. #define MR_PAGE_RX 0x00000008
  2897. #define MR_NP_LOADED 0x00000010
  2898. #define MR_TOGGLE_TX 0x00000020
  2899. #define MR_LP_ADV_FULL_DUPLEX 0x00000040
  2900. #define MR_LP_ADV_HALF_DUPLEX 0x00000080
  2901. #define MR_LP_ADV_SYM_PAUSE 0x00000100
  2902. #define MR_LP_ADV_ASYM_PAUSE 0x00000200
  2903. #define MR_LP_ADV_REMOTE_FAULT1 0x00000400
  2904. #define MR_LP_ADV_REMOTE_FAULT2 0x00000800
  2905. #define MR_LP_ADV_NEXT_PAGE 0x00001000
  2906. #define MR_TOGGLE_RX 0x00002000
  2907. #define MR_NP_RX 0x00004000
  2908. #define MR_LINK_OK 0x80000000
  2909. unsigned long link_time, cur_time;
  2910. u32 ability_match_cfg;
  2911. int ability_match_count;
  2912. char ability_match, idle_match, ack_match;
  2913. u32 txconfig, rxconfig;
  2914. #define ANEG_CFG_NP 0x00000080
  2915. #define ANEG_CFG_ACK 0x00000040
  2916. #define ANEG_CFG_RF2 0x00000020
  2917. #define ANEG_CFG_RF1 0x00000010
  2918. #define ANEG_CFG_PS2 0x00000001
  2919. #define ANEG_CFG_PS1 0x00008000
  2920. #define ANEG_CFG_HD 0x00004000
  2921. #define ANEG_CFG_FD 0x00002000
  2922. #define ANEG_CFG_INVAL 0x00001f06
  2923. };
  2924. #define ANEG_OK 0
  2925. #define ANEG_DONE 1
  2926. #define ANEG_TIMER_ENAB 2
  2927. #define ANEG_FAILED -1
  2928. #define ANEG_STATE_SETTLE_TIME 10000
  2929. static int tg3_fiber_aneg_smachine(struct tg3 *tp,
  2930. struct tg3_fiber_aneginfo *ap)
  2931. {
  2932. u16 flowctrl;
  2933. unsigned long delta;
  2934. u32 rx_cfg_reg;
  2935. int ret;
  2936. if (ap->state == ANEG_STATE_UNKNOWN) {
  2937. ap->rxconfig = 0;
  2938. ap->link_time = 0;
  2939. ap->cur_time = 0;
  2940. ap->ability_match_cfg = 0;
  2941. ap->ability_match_count = 0;
  2942. ap->ability_match = 0;
  2943. ap->idle_match = 0;
  2944. ap->ack_match = 0;
  2945. }
  2946. ap->cur_time++;
  2947. if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
  2948. rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
  2949. if (rx_cfg_reg != ap->ability_match_cfg) {
  2950. ap->ability_match_cfg = rx_cfg_reg;
  2951. ap->ability_match = 0;
  2952. ap->ability_match_count = 0;
  2953. } else {
  2954. if (++ap->ability_match_count > 1) {
  2955. ap->ability_match = 1;
  2956. ap->ability_match_cfg = rx_cfg_reg;
  2957. }
  2958. }
  2959. if (rx_cfg_reg & ANEG_CFG_ACK)
  2960. ap->ack_match = 1;
  2961. else
  2962. ap->ack_match = 0;
  2963. ap->idle_match = 0;
  2964. } else {
  2965. ap->idle_match = 1;
  2966. ap->ability_match_cfg = 0;
  2967. ap->ability_match_count = 0;
  2968. ap->ability_match = 0;
  2969. ap->ack_match = 0;
  2970. rx_cfg_reg = 0;
  2971. }
  2972. ap->rxconfig = rx_cfg_reg;
  2973. ret = ANEG_OK;
  2974. switch (ap->state) {
  2975. case ANEG_STATE_UNKNOWN:
  2976. if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
  2977. ap->state = ANEG_STATE_AN_ENABLE;
  2978. /* fallthru */
  2979. case ANEG_STATE_AN_ENABLE:
  2980. ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
  2981. if (ap->flags & MR_AN_ENABLE) {
  2982. ap->link_time = 0;
  2983. ap->cur_time = 0;
  2984. ap->ability_match_cfg = 0;
  2985. ap->ability_match_count = 0;
  2986. ap->ability_match = 0;
  2987. ap->idle_match = 0;
  2988. ap->ack_match = 0;
  2989. ap->state = ANEG_STATE_RESTART_INIT;
  2990. } else {
  2991. ap->state = ANEG_STATE_DISABLE_LINK_OK;
  2992. }
  2993. break;
  2994. case ANEG_STATE_RESTART_INIT:
  2995. ap->link_time = ap->cur_time;
  2996. ap->flags &= ~(MR_NP_LOADED);
  2997. ap->txconfig = 0;
  2998. tw32(MAC_TX_AUTO_NEG, 0);
  2999. tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
  3000. tw32_f(MAC_MODE, tp->mac_mode);
  3001. udelay(40);
  3002. ret = ANEG_TIMER_ENAB;
  3003. ap->state = ANEG_STATE_RESTART;
  3004. /* fallthru */
  3005. case ANEG_STATE_RESTART:
  3006. delta = ap->cur_time - ap->link_time;
  3007. if (delta > ANEG_STATE_SETTLE_TIME)
  3008. ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
  3009. else
  3010. ret = ANEG_TIMER_ENAB;
  3011. break;
  3012. case ANEG_STATE_DISABLE_LINK_OK:
  3013. ret = ANEG_DONE;
  3014. break;
  3015. case ANEG_STATE_ABILITY_DETECT_INIT:
  3016. ap->flags &= ~(MR_TOGGLE_TX);
  3017. ap->txconfig = ANEG_CFG_FD;
  3018. flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
  3019. if (flowctrl & ADVERTISE_1000XPAUSE)
  3020. ap->txconfig |= ANEG_CFG_PS1;
  3021. if (flowctrl & ADVERTISE_1000XPSE_ASYM)
  3022. ap->txconfig |= ANEG_CFG_PS2;
  3023. tw32(MAC_TX_AUTO_NEG, ap->txconfig);
  3024. tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
  3025. tw32_f(MAC_MODE, tp->mac_mode);
  3026. udelay(40);
  3027. ap->state = ANEG_STATE_ABILITY_DETECT;
  3028. break;
  3029. case ANEG_STATE_ABILITY_DETECT:
  3030. if (ap->ability_match != 0 && ap->rxconfig != 0)
  3031. ap->state = ANEG_STATE_ACK_DETECT_INIT;
  3032. break;
  3033. case ANEG_STATE_ACK_DETECT_INIT:
  3034. ap->txconfig |= ANEG_CFG_ACK;
  3035. tw32(MAC_TX_AUTO_NEG, ap->txconfig);
  3036. tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
  3037. tw32_f(MAC_MODE, tp->mac_mode);
  3038. udelay(40);
  3039. ap->state = ANEG_STATE_ACK_DETECT;
  3040. /* fallthru */
  3041. case ANEG_STATE_ACK_DETECT:
  3042. if (ap->ack_match != 0) {
  3043. if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
  3044. (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
  3045. ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
  3046. } else {
  3047. ap->state = ANEG_STATE_AN_ENABLE;
  3048. }
  3049. } else if (ap->ability_match != 0 &&
  3050. ap->rxconfig == 0) {
  3051. ap->state = ANEG_STATE_AN_ENABLE;
  3052. }
  3053. break;
  3054. case ANEG_STATE_COMPLETE_ACK_INIT:
  3055. if (ap->rxconfig & ANEG_CFG_INVAL) {
  3056. ret = ANEG_FAILED;
  3057. break;
  3058. }
  3059. ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
  3060. MR_LP_ADV_HALF_DUPLEX |
  3061. MR_LP_ADV_SYM_PAUSE |
  3062. MR_LP_ADV_ASYM_PAUSE |
  3063. MR_LP_ADV_REMOTE_FAULT1 |
  3064. MR_LP_ADV_REMOTE_FAULT2 |
  3065. MR_LP_ADV_NEXT_PAGE |
  3066. MR_TOGGLE_RX |
  3067. MR_NP_RX);
  3068. if (ap->rxconfig & ANEG_CFG_FD)
  3069. ap->flags |= MR_LP_ADV_FULL_DUPLEX;
  3070. if (ap->rxconfig & ANEG_CFG_HD)
  3071. ap->flags |= MR_LP_ADV_HALF_DUPLEX;
  3072. if (ap->rxconfig & ANEG_CFG_PS1)
  3073. ap->flags |= MR_LP_ADV_SYM_PAUSE;
  3074. if (ap->rxconfig & ANEG_CFG_PS2)
  3075. ap->flags |= MR_LP_ADV_ASYM_PAUSE;
  3076. if (ap->rxconfig & ANEG_CFG_RF1)
  3077. ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
  3078. if (ap->rxconfig & ANEG_CFG_RF2)
  3079. ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
  3080. if (ap->rxconfig & ANEG_CFG_NP)
  3081. ap->flags |= MR_LP_ADV_NEXT_PAGE;
  3082. ap->link_time = ap->cur_time;
  3083. ap->flags ^= (MR_TOGGLE_TX);
  3084. if (ap->rxconfig & 0x0008)
  3085. ap->flags |= MR_TOGGLE_RX;
  3086. if (ap->rxconfig & ANEG_CFG_NP)
  3087. ap->flags |= MR_NP_RX;
  3088. ap->flags |= MR_PAGE_RX;
  3089. ap->state = ANEG_STATE_COMPLETE_ACK;
  3090. ret = ANEG_TIMER_ENAB;
  3091. break;
  3092. case ANEG_STATE_COMPLETE_ACK:
  3093. if (ap->ability_match != 0 &&
  3094. ap->rxconfig == 0) {
  3095. ap->state = ANEG_STATE_AN_ENABLE;
  3096. break;
  3097. }
  3098. delta = ap->cur_time - ap->link_time;
  3099. if (delta > ANEG_STATE_SETTLE_TIME) {
  3100. if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
  3101. ap->state = ANEG_STATE_IDLE_DETECT_INIT;
  3102. } else {
  3103. if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
  3104. !(ap->flags & MR_NP_RX)) {
  3105. ap->state = ANEG_STATE_IDLE_DETECT_INIT;
  3106. } else {
  3107. ret = ANEG_FAILED;
  3108. }
  3109. }
  3110. }
  3111. break;
  3112. case ANEG_STATE_IDLE_DETECT_INIT:
  3113. ap->link_time = ap->cur_time;
  3114. tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
  3115. tw32_f(MAC_MODE, tp->mac_mode);
  3116. udelay(40);
  3117. ap->state = ANEG_STATE_IDLE_DETECT;
  3118. ret = ANEG_TIMER_ENAB;
  3119. break;
  3120. case ANEG_STATE_IDLE_DETECT:
  3121. if (ap->ability_match != 0 &&
  3122. ap->rxconfig == 0) {
  3123. ap->state = ANEG_STATE_AN_ENABLE;
  3124. break;
  3125. }
  3126. delta = ap->cur_time - ap->link_time;
  3127. if (delta > ANEG_STATE_SETTLE_TIME) {
  3128. /* XXX another gem from the Broadcom driver :( */
  3129. ap->state = ANEG_STATE_LINK_OK;
  3130. }
  3131. break;
  3132. case ANEG_STATE_LINK_OK:
  3133. ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
  3134. ret = ANEG_DONE;
  3135. break;
  3136. case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
  3137. /* ??? unimplemented */
  3138. break;
  3139. case ANEG_STATE_NEXT_PAGE_WAIT:
  3140. /* ??? unimplemented */
  3141. break;
  3142. default:
  3143. ret = ANEG_FAILED;
  3144. break;
  3145. }
  3146. return ret;
  3147. }
  3148. static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
  3149. {
  3150. int res = 0;
  3151. struct tg3_fiber_aneginfo aninfo;
  3152. int status = ANEG_FAILED;
  3153. unsigned int tick;
  3154. u32 tmp;
  3155. tw32_f(MAC_TX_AUTO_NEG, 0);
  3156. tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
  3157. tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
  3158. udelay(40);
  3159. tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
  3160. udelay(40);
  3161. memset(&aninfo, 0, sizeof(aninfo));
  3162. aninfo.flags |= MR_AN_ENABLE;
  3163. aninfo.state = ANEG_STATE_UNKNOWN;
  3164. aninfo.cur_time = 0;
  3165. tick = 0;
  3166. while (++tick < 195000) {
  3167. status = tg3_fiber_aneg_smachine(tp, &aninfo);
  3168. if (status == ANEG_DONE || status == ANEG_FAILED)
  3169. break;
  3170. udelay(1);
  3171. }
  3172. tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
  3173. tw32_f(MAC_MODE, tp->mac_mode);
  3174. udelay(40);
  3175. *txflags = aninfo.txconfig;
  3176. *rxflags = aninfo.flags;
  3177. if (status == ANEG_DONE &&
  3178. (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
  3179. MR_LP_ADV_FULL_DUPLEX)))
  3180. res = 1;
  3181. return res;
  3182. }
  3183. static void tg3_init_bcm8002(struct tg3 *tp)
  3184. {
  3185. u32 mac_status = tr32(MAC_STATUS);
  3186. int i;
  3187. /* Reset when initting first time or we have a link. */
  3188. if (tg3_flag(tp, INIT_COMPLETE) &&
  3189. !(mac_status & MAC_STATUS_PCS_SYNCED))
  3190. return;
  3191. /* Set PLL lock range. */
  3192. tg3_writephy(tp, 0x16, 0x8007);
  3193. /* SW reset */
  3194. tg3_writephy(tp, MII_BMCR, BMCR_RESET);
  3195. /* Wait for reset to complete. */
  3196. /* XXX schedule_timeout() ... */
  3197. for (i = 0; i < 500; i++)
  3198. udelay(10);
  3199. /* Config mode; select PMA/Ch 1 regs. */
  3200. tg3_writephy(tp, 0x10, 0x8411);
  3201. /* Enable auto-lock and comdet, select txclk for tx. */
  3202. tg3_writephy(tp, 0x11, 0x0a10);
  3203. tg3_writephy(tp, 0x18, 0x00a0);
  3204. tg3_writephy(tp, 0x16, 0x41ff);
  3205. /* Assert and deassert POR. */
  3206. tg3_writephy(tp, 0x13, 0x0400);
  3207. udelay(40);
  3208. tg3_writephy(tp, 0x13, 0x0000);
  3209. tg3_writephy(tp, 0x11, 0x0a50);
  3210. udelay(40);
  3211. tg3_writephy(tp, 0x11, 0x0a10);
  3212. /* Wait for signal to stabilize */
  3213. /* XXX schedule_timeout() ... */
  3214. for (i = 0; i < 15000; i++)
  3215. udelay(10);
  3216. /* Deselect the channel register so we can read the PHYID
  3217. * later.
  3218. */
  3219. tg3_writephy(tp, 0x10, 0x8011);
  3220. }
  3221. static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
  3222. {
  3223. u16 flowctrl;
  3224. u32 sg_dig_ctrl, sg_dig_status;
  3225. u32 serdes_cfg, expected_sg_dig_ctrl;
  3226. int workaround, port_a;
  3227. int current_link_up;
  3228. serdes_cfg = 0;
  3229. expected_sg_dig_ctrl = 0;
  3230. workaround = 0;
  3231. port_a = 1;
  3232. current_link_up = 0;
  3233. if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
  3234. tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
  3235. workaround = 1;
  3236. if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
  3237. port_a = 0;
  3238. /* preserve bits 0-11,13,14 for signal pre-emphasis */
  3239. /* preserve bits 20-23 for voltage regulator */
  3240. serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
  3241. }
  3242. sg_dig_ctrl = tr32(SG_DIG_CTRL);
  3243. if (tp->link_config.autoneg != AUTONEG_ENABLE) {
  3244. if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
  3245. if (workaround) {
  3246. u32 val = serdes_cfg;
  3247. if (port_a)
  3248. val |= 0xc010000;
  3249. else
  3250. val |= 0x4010000;
  3251. tw32_f(MAC_SERDES_CFG, val);
  3252. }
  3253. tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
  3254. }
  3255. if (mac_status & MAC_STATUS_PCS_SYNCED) {
  3256. tg3_setup_flow_control(tp, 0, 0);
  3257. current_link_up = 1;
  3258. }
  3259. goto out;
  3260. }
  3261. /* Want auto-negotiation. */
  3262. expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
  3263. flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
  3264. if (flowctrl & ADVERTISE_1000XPAUSE)
  3265. expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
  3266. if (flowctrl & ADVERTISE_1000XPSE_ASYM)
  3267. expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
  3268. if (sg_dig_ctrl != expected_sg_dig_ctrl) {
  3269. if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
  3270. tp->serdes_counter &&
  3271. ((mac_status & (MAC_STATUS_PCS_SYNCED |
  3272. MAC_STATUS_RCVD_CFG)) ==
  3273. MAC_STATUS_PCS_SYNCED)) {
  3274. tp->serdes_counter--;
  3275. current_link_up = 1;
  3276. goto out;
  3277. }
  3278. restart_autoneg:
  3279. if (workaround)
  3280. tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
  3281. tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
  3282. udelay(5);
  3283. tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
  3284. tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
  3285. tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
  3286. } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
  3287. MAC_STATUS_SIGNAL_DET)) {
  3288. sg_dig_status = tr32(SG_DIG_STATUS);
  3289. mac_status = tr32(MAC_STATUS);
  3290. if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
  3291. (mac_status & MAC_STATUS_PCS_SYNCED)) {
  3292. u32 local_adv = 0, remote_adv = 0;
  3293. if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
  3294. local_adv |= ADVERTISE_1000XPAUSE;
  3295. if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
  3296. local_adv |= ADVERTISE_1000XPSE_ASYM;
  3297. if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
  3298. remote_adv |= LPA_1000XPAUSE;
  3299. if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
  3300. remote_adv |= LPA_1000XPAUSE_ASYM;
  3301. tg3_setup_flow_control(tp, local_adv, remote_adv);
  3302. current_link_up = 1;
  3303. tp->serdes_counter = 0;
  3304. tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
  3305. } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
  3306. if (tp->serdes_counter)
  3307. tp->serdes_counter--;
  3308. else {
  3309. if (workaround) {
  3310. u32 val = serdes_cfg;
  3311. if (port_a)
  3312. val |= 0xc010000;
  3313. else
  3314. val |= 0x4010000;
  3315. tw32_f(MAC_SERDES_CFG, val);
  3316. }
  3317. tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
  3318. udelay(40);
  3319. /* Link parallel detection - link is up */
  3320. /* only if we have PCS_SYNC and not */
  3321. /* receiving config code words */
  3322. mac_status = tr32(MAC_STATUS);
  3323. if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
  3324. !(mac_status & MAC_STATUS_RCVD_CFG)) {
  3325. tg3_setup_flow_control(tp, 0, 0);
  3326. current_link_up = 1;
  3327. tp->phy_flags |=
  3328. TG3_PHYFLG_PARALLEL_DETECT;
  3329. tp->serdes_counter =
  3330. SERDES_PARALLEL_DET_TIMEOUT;
  3331. } else
  3332. goto restart_autoneg;
  3333. }
  3334. }
  3335. } else {
  3336. tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
  3337. tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
  3338. }
  3339. out:
  3340. return current_link_up;
  3341. }
  3342. static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
  3343. {
  3344. int current_link_up = 0;
  3345. if (!(mac_status & MAC_STATUS_PCS_SYNCED))
  3346. goto out;
  3347. if (tp->link_config.autoneg == AUTONEG_ENABLE) {
  3348. u32 txflags, rxflags;
  3349. int i;
  3350. if (fiber_autoneg(tp, &txflags, &rxflags)) {
  3351. u32 local_adv = 0, remote_adv = 0;
  3352. if (txflags & ANEG_CFG_PS1)
  3353. local_adv |= ADVERTISE_1000XPAUSE;
  3354. if (txflags & ANEG_CFG_PS2)
  3355. local_adv |= ADVERTISE_1000XPSE_ASYM;
  3356. if (rxflags & MR_LP_ADV_SYM_PAUSE)
  3357. remote_adv |= LPA_1000XPAUSE;
  3358. if (rxflags & MR_LP_ADV_ASYM_PAUSE)
  3359. remote_adv |= LPA_1000XPAUSE_ASYM;
  3360. tg3_setup_flow_control(tp, local_adv, remote_adv);
  3361. current_link_up = 1;
  3362. }
  3363. for (i = 0; i < 30; i++) {
  3364. udelay(20);
  3365. tw32_f(MAC_STATUS,
  3366. (MAC_STATUS_SYNC_CHANGED |
  3367. MAC_STATUS_CFG_CHANGED));
  3368. udelay(40);
  3369. if ((tr32(MAC_STATUS) &
  3370. (MAC_STATUS_SYNC_CHANGED |
  3371. MAC_STATUS_CFG_CHANGED)) == 0)
  3372. break;
  3373. }
  3374. mac_status = tr32(MAC_STATUS);
  3375. if (current_link_up == 0 &&
  3376. (mac_status & MAC_STATUS_PCS_SYNCED) &&
  3377. !(mac_status & MAC_STATUS_RCVD_CFG))
  3378. current_link_up = 1;
  3379. } else {
  3380. tg3_setup_flow_control(tp, 0, 0);
  3381. /* Forcing 1000FD link up. */
  3382. current_link_up = 1;
  3383. tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
  3384. udelay(40);
  3385. tw32_f(MAC_MODE, tp->mac_mode);
  3386. udelay(40);
  3387. }
  3388. out:
  3389. return current_link_up;
  3390. }
  3391. static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
  3392. {
  3393. u32 orig_pause_cfg;
  3394. u16 orig_active_speed;
  3395. u8 orig_active_duplex;
  3396. u32 mac_status;
  3397. int current_link_up;
  3398. int i;
  3399. orig_pause_cfg = tp->link_config.active_flowctrl;
  3400. orig_active_speed = tp->link_config.active_speed;
  3401. orig_active_duplex = tp->link_config.active_duplex;
  3402. if (!tg3_flag(tp, HW_AUTONEG) &&
  3403. netif_carrier_ok(tp->dev) &&
  3404. tg3_flag(tp, INIT_COMPLETE)) {
  3405. mac_status = tr32(MAC_STATUS);
  3406. mac_status &= (MAC_STATUS_PCS_SYNCED |
  3407. MAC_STATUS_SIGNAL_DET |
  3408. MAC_STATUS_CFG_CHANGED |
  3409. MAC_STATUS_RCVD_CFG);
  3410. if (mac_status == (MAC_STATUS_PCS_SYNCED |
  3411. MAC_STATUS_SIGNAL_DET)) {
  3412. tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
  3413. MAC_STATUS_CFG_CHANGED));
  3414. return 0;
  3415. }
  3416. }
  3417. tw32_f(MAC_TX_AUTO_NEG, 0);
  3418. tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
  3419. tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
  3420. tw32_f(MAC_MODE, tp->mac_mode);
  3421. udelay(40);
  3422. if (tp->phy_id == TG3_PHY_ID_BCM8002)
  3423. tg3_init_bcm8002(tp);
  3424. /* Enable link change event even when serdes polling. */
  3425. tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
  3426. udelay(40);
  3427. current_link_up = 0;
  3428. mac_status = tr32(MAC_STATUS);
  3429. if (tg3_flag(tp, HW_AUTONEG))
  3430. current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
  3431. else
  3432. current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
  3433. tp->napi[0].hw_status->status =
  3434. (SD_STATUS_UPDATED |
  3435. (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
  3436. for (i = 0; i < 100; i++) {
  3437. tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
  3438. MAC_STATUS_CFG_CHANGED));
  3439. udelay(5);
  3440. if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
  3441. MAC_STATUS_CFG_CHANGED |
  3442. MAC_STATUS_LNKSTATE_CHANGED)) == 0)
  3443. break;
  3444. }
  3445. mac_status = tr32(MAC_STATUS);
  3446. if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
  3447. current_link_up = 0;
  3448. if (tp->link_config.autoneg == AUTONEG_ENABLE &&
  3449. tp->serdes_counter == 0) {
  3450. tw32_f(MAC_MODE, (tp->mac_mode |
  3451. MAC_MODE_SEND_CONFIGS));
  3452. udelay(1);
  3453. tw32_f(MAC_MODE, tp->mac_mode);
  3454. }
  3455. }
  3456. if (current_link_up == 1) {
  3457. tp->link_config.active_speed = SPEED_1000;
  3458. tp->link_config.active_duplex = DUPLEX_FULL;
  3459. tw32(MAC_LED_CTRL, (tp->led_ctrl |
  3460. LED_CTRL_LNKLED_OVERRIDE |
  3461. LED_CTRL_1000MBPS_ON));
  3462. } else {
  3463. tp->link_config.active_speed = SPEED_INVALID;
  3464. tp->link_config.active_duplex = DUPLEX_INVALID;
  3465. tw32(MAC_LED_CTRL, (tp->led_ctrl |
  3466. LED_CTRL_LNKLED_OVERRIDE |
  3467. LED_CTRL_TRAFFIC_OVERRIDE));
  3468. }
  3469. if (current_link_up != netif_carrier_ok(tp->dev)) {
  3470. if (current_link_up)
  3471. netif_carrier_on(tp->dev);
  3472. else
  3473. netif_carrier_off(tp->dev);
  3474. tg3_link_report(tp);
  3475. } else {
  3476. u32 now_pause_cfg = tp->link_config.active_flowctrl;
  3477. if (orig_pause_cfg != now_pause_cfg ||
  3478. orig_active_speed != tp->link_config.active_speed ||
  3479. orig_active_duplex != tp->link_config.active_duplex)
  3480. tg3_link_report(tp);
  3481. }
  3482. return 0;
  3483. }
  3484. static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
  3485. {
  3486. int current_link_up, err = 0;
  3487. u32 bmsr, bmcr;
  3488. u16 current_speed;
  3489. u8 current_duplex;
  3490. u32 local_adv, remote_adv;
  3491. tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
  3492. tw32_f(MAC_MODE, tp->mac_mode);
  3493. udelay(40);
  3494. tw32(MAC_EVENT, 0);
  3495. tw32_f(MAC_STATUS,
  3496. (MAC_STATUS_SYNC_CHANGED |
  3497. MAC_STATUS_CFG_CHANGED |
  3498. MAC_STATUS_MI_COMPLETION |
  3499. MAC_STATUS_LNKSTATE_CHANGED));
  3500. udelay(40);
  3501. if (force_reset)
  3502. tg3_phy_reset(tp);
  3503. current_link_up = 0;
  3504. current_speed = SPEED_INVALID;
  3505. current_duplex = DUPLEX_INVALID;
  3506. err |= tg3_readphy(tp, MII_BMSR, &bmsr);
  3507. err |= tg3_readphy(tp, MII_BMSR, &bmsr);
  3508. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
  3509. if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
  3510. bmsr |= BMSR_LSTATUS;
  3511. else
  3512. bmsr &= ~BMSR_LSTATUS;
  3513. }
  3514. err |= tg3_readphy(tp, MII_BMCR, &bmcr);
  3515. if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
  3516. (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
  3517. /* do nothing, just check for link up at the end */
  3518. } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
  3519. u32 adv, new_adv;
  3520. err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
  3521. new_adv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
  3522. ADVERTISE_1000XPAUSE |
  3523. ADVERTISE_1000XPSE_ASYM |
  3524. ADVERTISE_SLCT);
  3525. new_adv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
  3526. if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
  3527. new_adv |= ADVERTISE_1000XHALF;
  3528. if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
  3529. new_adv |= ADVERTISE_1000XFULL;
  3530. if ((new_adv != adv) || !(bmcr & BMCR_ANENABLE)) {
  3531. tg3_writephy(tp, MII_ADVERTISE, new_adv);
  3532. bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
  3533. tg3_writephy(tp, MII_BMCR, bmcr);
  3534. tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
  3535. tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
  3536. tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
  3537. return err;
  3538. }
  3539. } else {
  3540. u32 new_bmcr;
  3541. bmcr &= ~BMCR_SPEED1000;
  3542. new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
  3543. if (tp->link_config.duplex == DUPLEX_FULL)
  3544. new_bmcr |= BMCR_FULLDPLX;
  3545. if (new_bmcr != bmcr) {
  3546. /* BMCR_SPEED1000 is a reserved bit that needs
  3547. * to be set on write.
  3548. */
  3549. new_bmcr |= BMCR_SPEED1000;
  3550. /* Force a linkdown */
  3551. if (netif_carrier_ok(tp->dev)) {
  3552. u32 adv;
  3553. err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
  3554. adv &= ~(ADVERTISE_1000XFULL |
  3555. ADVERTISE_1000XHALF |
  3556. ADVERTISE_SLCT);
  3557. tg3_writephy(tp, MII_ADVERTISE, adv);
  3558. tg3_writephy(tp, MII_BMCR, bmcr |
  3559. BMCR_ANRESTART |
  3560. BMCR_ANENABLE);
  3561. udelay(10);
  3562. netif_carrier_off(tp->dev);
  3563. }
  3564. tg3_writephy(tp, MII_BMCR, new_bmcr);
  3565. bmcr = new_bmcr;
  3566. err |= tg3_readphy(tp, MII_BMSR, &bmsr);
  3567. err |= tg3_readphy(tp, MII_BMSR, &bmsr);
  3568. if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
  3569. ASIC_REV_5714) {
  3570. if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
  3571. bmsr |= BMSR_LSTATUS;
  3572. else
  3573. bmsr &= ~BMSR_LSTATUS;
  3574. }
  3575. tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
  3576. }
  3577. }
  3578. if (bmsr & BMSR_LSTATUS) {
  3579. current_speed = SPEED_1000;
  3580. current_link_up = 1;
  3581. if (bmcr & BMCR_FULLDPLX)
  3582. current_duplex = DUPLEX_FULL;
  3583. else
  3584. current_duplex = DUPLEX_HALF;
  3585. local_adv = 0;
  3586. remote_adv = 0;
  3587. if (bmcr & BMCR_ANENABLE) {
  3588. u32 common;
  3589. err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
  3590. err |= tg3_readphy(tp, MII_LPA, &remote_adv);
  3591. common = local_adv & remote_adv;
  3592. if (common & (ADVERTISE_1000XHALF |
  3593. ADVERTISE_1000XFULL)) {
  3594. if (common & ADVERTISE_1000XFULL)
  3595. current_duplex = DUPLEX_FULL;
  3596. else
  3597. current_duplex = DUPLEX_HALF;
  3598. } else if (!tg3_flag(tp, 5780_CLASS)) {
  3599. /* Link is up via parallel detect */
  3600. } else {
  3601. current_link_up = 0;
  3602. }
  3603. }
  3604. }
  3605. if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
  3606. tg3_setup_flow_control(tp, local_adv, remote_adv);
  3607. tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
  3608. if (tp->link_config.active_duplex == DUPLEX_HALF)
  3609. tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
  3610. tw32_f(MAC_MODE, tp->mac_mode);
  3611. udelay(40);
  3612. tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
  3613. tp->link_config.active_speed = current_speed;
  3614. tp->link_config.active_duplex = current_duplex;
  3615. if (current_link_up != netif_carrier_ok(tp->dev)) {
  3616. if (current_link_up)
  3617. netif_carrier_on(tp->dev);
  3618. else {
  3619. netif_carrier_off(tp->dev);
  3620. tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
  3621. }
  3622. tg3_link_report(tp);
  3623. }
  3624. return err;
  3625. }
  3626. static void tg3_serdes_parallel_detect(struct tg3 *tp)
  3627. {
  3628. if (tp->serdes_counter) {
  3629. /* Give autoneg time to complete. */
  3630. tp->serdes_counter--;
  3631. return;
  3632. }
  3633. if (!netif_carrier_ok(tp->dev) &&
  3634. (tp->link_config.autoneg == AUTONEG_ENABLE)) {
  3635. u32 bmcr;
  3636. tg3_readphy(tp, MII_BMCR, &bmcr);
  3637. if (bmcr & BMCR_ANENABLE) {
  3638. u32 phy1, phy2;
  3639. /* Select shadow register 0x1f */
  3640. tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
  3641. tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
  3642. /* Select expansion interrupt status register */
  3643. tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
  3644. MII_TG3_DSP_EXP1_INT_STAT);
  3645. tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
  3646. tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
  3647. if ((phy1 & 0x10) && !(phy2 & 0x20)) {
  3648. /* We have signal detect and not receiving
  3649. * config code words, link is up by parallel
  3650. * detection.
  3651. */
  3652. bmcr &= ~BMCR_ANENABLE;
  3653. bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
  3654. tg3_writephy(tp, MII_BMCR, bmcr);
  3655. tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
  3656. }
  3657. }
  3658. } else if (netif_carrier_ok(tp->dev) &&
  3659. (tp->link_config.autoneg == AUTONEG_ENABLE) &&
  3660. (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
  3661. u32 phy2;
  3662. /* Select expansion interrupt status register */
  3663. tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
  3664. MII_TG3_DSP_EXP1_INT_STAT);
  3665. tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
  3666. if (phy2 & 0x20) {
  3667. u32 bmcr;
  3668. /* Config code words received, turn on autoneg. */
  3669. tg3_readphy(tp, MII_BMCR, &bmcr);
  3670. tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
  3671. tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
  3672. }
  3673. }
  3674. }
  3675. static int tg3_setup_phy(struct tg3 *tp, int force_reset)
  3676. {
  3677. u32 val;
  3678. int err;
  3679. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
  3680. err = tg3_setup_fiber_phy(tp, force_reset);
  3681. else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
  3682. err = tg3_setup_fiber_mii_phy(tp, force_reset);
  3683. else
  3684. err = tg3_setup_copper_phy(tp, force_reset);
  3685. if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
  3686. u32 scale;
  3687. val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
  3688. if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
  3689. scale = 65;
  3690. else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
  3691. scale = 6;
  3692. else
  3693. scale = 12;
  3694. val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
  3695. val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
  3696. tw32(GRC_MISC_CFG, val);
  3697. }
  3698. val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
  3699. (6 << TX_LENGTHS_IPG_SHIFT);
  3700. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
  3701. val |= tr32(MAC_TX_LENGTHS) &
  3702. (TX_LENGTHS_JMB_FRM_LEN_MSK |
  3703. TX_LENGTHS_CNT_DWN_VAL_MSK);
  3704. if (tp->link_config.active_speed == SPEED_1000 &&
  3705. tp->link_config.active_duplex == DUPLEX_HALF)
  3706. tw32(MAC_TX_LENGTHS, val |
  3707. (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
  3708. else
  3709. tw32(MAC_TX_LENGTHS, val |
  3710. (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
  3711. if (!tg3_flag(tp, 5705_PLUS)) {
  3712. if (netif_carrier_ok(tp->dev)) {
  3713. tw32(HOSTCC_STAT_COAL_TICKS,
  3714. tp->coal.stats_block_coalesce_usecs);
  3715. } else {
  3716. tw32(HOSTCC_STAT_COAL_TICKS, 0);
  3717. }
  3718. }
  3719. if (tg3_flag(tp, ASPM_WORKAROUND)) {
  3720. val = tr32(PCIE_PWR_MGMT_THRESH);
  3721. if (!netif_carrier_ok(tp->dev))
  3722. val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
  3723. tp->pwrmgmt_thresh;
  3724. else
  3725. val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
  3726. tw32(PCIE_PWR_MGMT_THRESH, val);
  3727. }
  3728. return err;
  3729. }
  3730. static inline int tg3_irq_sync(struct tg3 *tp)
  3731. {
  3732. return tp->irq_sync;
  3733. }
  3734. static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
  3735. {
  3736. int i;
  3737. dst = (u32 *)((u8 *)dst + off);
  3738. for (i = 0; i < len; i += sizeof(u32))
  3739. *dst++ = tr32(off + i);
  3740. }
  3741. static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
  3742. {
  3743. tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
  3744. tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
  3745. tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
  3746. tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
  3747. tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
  3748. tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
  3749. tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
  3750. tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
  3751. tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
  3752. tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
  3753. tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
  3754. tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
  3755. tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
  3756. tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
  3757. tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
  3758. tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
  3759. tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
  3760. tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
  3761. tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
  3762. if (tg3_flag(tp, SUPPORT_MSIX))
  3763. tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
  3764. tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
  3765. tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
  3766. tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
  3767. tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
  3768. tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
  3769. tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
  3770. tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
  3771. tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
  3772. if (!tg3_flag(tp, 5705_PLUS)) {
  3773. tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
  3774. tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
  3775. tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
  3776. }
  3777. tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
  3778. tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
  3779. tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
  3780. tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
  3781. tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
  3782. if (tg3_flag(tp, NVRAM))
  3783. tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
  3784. }
  3785. static void tg3_dump_state(struct tg3 *tp)
  3786. {
  3787. int i;
  3788. u32 *regs;
  3789. regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
  3790. if (!regs) {
  3791. netdev_err(tp->dev, "Failed allocating register dump buffer\n");
  3792. return;
  3793. }
  3794. if (tg3_flag(tp, PCI_EXPRESS)) {
  3795. /* Read up to but not including private PCI registers */
  3796. for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
  3797. regs[i / sizeof(u32)] = tr32(i);
  3798. } else
  3799. tg3_dump_legacy_regs(tp, regs);
  3800. for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
  3801. if (!regs[i + 0] && !regs[i + 1] &&
  3802. !regs[i + 2] && !regs[i + 3])
  3803. continue;
  3804. netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
  3805. i * 4,
  3806. regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
  3807. }
  3808. kfree(regs);
  3809. for (i = 0; i < tp->irq_cnt; i++) {
  3810. struct tg3_napi *tnapi = &tp->napi[i];
  3811. /* SW status block */
  3812. netdev_err(tp->dev,
  3813. "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
  3814. i,
  3815. tnapi->hw_status->status,
  3816. tnapi->hw_status->status_tag,
  3817. tnapi->hw_status->rx_jumbo_consumer,
  3818. tnapi->hw_status->rx_consumer,
  3819. tnapi->hw_status->rx_mini_consumer,
  3820. tnapi->hw_status->idx[0].rx_producer,
  3821. tnapi->hw_status->idx[0].tx_consumer);
  3822. netdev_err(tp->dev,
  3823. "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
  3824. i,
  3825. tnapi->last_tag, tnapi->last_irq_tag,
  3826. tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
  3827. tnapi->rx_rcb_ptr,
  3828. tnapi->prodring.rx_std_prod_idx,
  3829. tnapi->prodring.rx_std_cons_idx,
  3830. tnapi->prodring.rx_jmb_prod_idx,
  3831. tnapi->prodring.rx_jmb_cons_idx);
  3832. }
  3833. }
  3834. /* This is called whenever we suspect that the system chipset is re-
  3835. * ordering the sequence of MMIO to the tx send mailbox. The symptom
  3836. * is bogus tx completions. We try to recover by setting the
  3837. * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
  3838. * in the workqueue.
  3839. */
  3840. static void tg3_tx_recover(struct tg3 *tp)
  3841. {
  3842. BUG_ON(tg3_flag(tp, MBOX_WRITE_REORDER) ||
  3843. tp->write32_tx_mbox == tg3_write_indirect_mbox);
  3844. netdev_warn(tp->dev,
  3845. "The system may be re-ordering memory-mapped I/O "
  3846. "cycles to the network device, attempting to recover. "
  3847. "Please report the problem to the driver maintainer "
  3848. "and include system chipset information.\n");
  3849. spin_lock(&tp->lock);
  3850. tg3_flag_set(tp, TX_RECOVERY_PENDING);
  3851. spin_unlock(&tp->lock);
  3852. }
  3853. static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
  3854. {
  3855. /* Tell compiler to fetch tx indices from memory. */
  3856. barrier();
  3857. return tnapi->tx_pending -
  3858. ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
  3859. }
  3860. /* Tigon3 never reports partial packet sends. So we do not
  3861. * need special logic to handle SKBs that have not had all
  3862. * of their frags sent yet, like SunGEM does.
  3863. */
  3864. static void tg3_tx(struct tg3_napi *tnapi)
  3865. {
  3866. struct tg3 *tp = tnapi->tp;
  3867. u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
  3868. u32 sw_idx = tnapi->tx_cons;
  3869. struct netdev_queue *txq;
  3870. int index = tnapi - tp->napi;
  3871. if (tg3_flag(tp, ENABLE_TSS))
  3872. index--;
  3873. txq = netdev_get_tx_queue(tp->dev, index);
  3874. while (sw_idx != hw_idx) {
  3875. struct ring_info *ri = &tnapi->tx_buffers[sw_idx];
  3876. struct sk_buff *skb = ri->skb;
  3877. int i, tx_bug = 0;
  3878. if (unlikely(skb == NULL)) {
  3879. tg3_tx_recover(tp);
  3880. return;
  3881. }
  3882. pci_unmap_single(tp->pdev,
  3883. dma_unmap_addr(ri, mapping),
  3884. skb_headlen(skb),
  3885. PCI_DMA_TODEVICE);
  3886. ri->skb = NULL;
  3887. sw_idx = NEXT_TX(sw_idx);
  3888. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  3889. ri = &tnapi->tx_buffers[sw_idx];
  3890. if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
  3891. tx_bug = 1;
  3892. pci_unmap_page(tp->pdev,
  3893. dma_unmap_addr(ri, mapping),
  3894. skb_shinfo(skb)->frags[i].size,
  3895. PCI_DMA_TODEVICE);
  3896. sw_idx = NEXT_TX(sw_idx);
  3897. }
  3898. dev_kfree_skb(skb);
  3899. if (unlikely(tx_bug)) {
  3900. tg3_tx_recover(tp);
  3901. return;
  3902. }
  3903. }
  3904. tnapi->tx_cons = sw_idx;
  3905. /* Need to make the tx_cons update visible to tg3_start_xmit()
  3906. * before checking for netif_queue_stopped(). Without the
  3907. * memory barrier, there is a small possibility that tg3_start_xmit()
  3908. * will miss it and cause the queue to be stopped forever.
  3909. */
  3910. smp_mb();
  3911. if (unlikely(netif_tx_queue_stopped(txq) &&
  3912. (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
  3913. __netif_tx_lock(txq, smp_processor_id());
  3914. if (netif_tx_queue_stopped(txq) &&
  3915. (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
  3916. netif_tx_wake_queue(txq);
  3917. __netif_tx_unlock(txq);
  3918. }
  3919. }
  3920. static void tg3_rx_skb_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
  3921. {
  3922. if (!ri->skb)
  3923. return;
  3924. pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
  3925. map_sz, PCI_DMA_FROMDEVICE);
  3926. dev_kfree_skb_any(ri->skb);
  3927. ri->skb = NULL;
  3928. }
  3929. /* Returns size of skb allocated or < 0 on error.
  3930. *
  3931. * We only need to fill in the address because the other members
  3932. * of the RX descriptor are invariant, see tg3_init_rings.
  3933. *
  3934. * Note the purposeful assymetry of cpu vs. chip accesses. For
  3935. * posting buffers we only dirty the first cache line of the RX
  3936. * descriptor (containing the address). Whereas for the RX status
  3937. * buffers the cpu only reads the last cacheline of the RX descriptor
  3938. * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
  3939. */
  3940. static int tg3_alloc_rx_skb(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
  3941. u32 opaque_key, u32 dest_idx_unmasked)
  3942. {
  3943. struct tg3_rx_buffer_desc *desc;
  3944. struct ring_info *map;
  3945. struct sk_buff *skb;
  3946. dma_addr_t mapping;
  3947. int skb_size, dest_idx;
  3948. switch (opaque_key) {
  3949. case RXD_OPAQUE_RING_STD:
  3950. dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
  3951. desc = &tpr->rx_std[dest_idx];
  3952. map = &tpr->rx_std_buffers[dest_idx];
  3953. skb_size = tp->rx_pkt_map_sz;
  3954. break;
  3955. case RXD_OPAQUE_RING_JUMBO:
  3956. dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
  3957. desc = &tpr->rx_jmb[dest_idx].std;
  3958. map = &tpr->rx_jmb_buffers[dest_idx];
  3959. skb_size = TG3_RX_JMB_MAP_SZ;
  3960. break;
  3961. default:
  3962. return -EINVAL;
  3963. }
  3964. /* Do not overwrite any of the map or rp information
  3965. * until we are sure we can commit to a new buffer.
  3966. *
  3967. * Callers depend upon this behavior and assume that
  3968. * we leave everything unchanged if we fail.
  3969. */
  3970. skb = netdev_alloc_skb(tp->dev, skb_size + tp->rx_offset);
  3971. if (skb == NULL)
  3972. return -ENOMEM;
  3973. skb_reserve(skb, tp->rx_offset);
  3974. mapping = pci_map_single(tp->pdev, skb->data, skb_size,
  3975. PCI_DMA_FROMDEVICE);
  3976. if (pci_dma_mapping_error(tp->pdev, mapping)) {
  3977. dev_kfree_skb(skb);
  3978. return -EIO;
  3979. }
  3980. map->skb = skb;
  3981. dma_unmap_addr_set(map, mapping, mapping);
  3982. desc->addr_hi = ((u64)mapping >> 32);
  3983. desc->addr_lo = ((u64)mapping & 0xffffffff);
  3984. return skb_size;
  3985. }
  3986. /* We only need to move over in the address because the other
  3987. * members of the RX descriptor are invariant. See notes above
  3988. * tg3_alloc_rx_skb for full details.
  3989. */
  3990. static void tg3_recycle_rx(struct tg3_napi *tnapi,
  3991. struct tg3_rx_prodring_set *dpr,
  3992. u32 opaque_key, int src_idx,
  3993. u32 dest_idx_unmasked)
  3994. {
  3995. struct tg3 *tp = tnapi->tp;
  3996. struct tg3_rx_buffer_desc *src_desc, *dest_desc;
  3997. struct ring_info *src_map, *dest_map;
  3998. struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
  3999. int dest_idx;
  4000. switch (opaque_key) {
  4001. case RXD_OPAQUE_RING_STD:
  4002. dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
  4003. dest_desc = &dpr->rx_std[dest_idx];
  4004. dest_map = &dpr->rx_std_buffers[dest_idx];
  4005. src_desc = &spr->rx_std[src_idx];
  4006. src_map = &spr->rx_std_buffers[src_idx];
  4007. break;
  4008. case RXD_OPAQUE_RING_JUMBO:
  4009. dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
  4010. dest_desc = &dpr->rx_jmb[dest_idx].std;
  4011. dest_map = &dpr->rx_jmb_buffers[dest_idx];
  4012. src_desc = &spr->rx_jmb[src_idx].std;
  4013. src_map = &spr->rx_jmb_buffers[src_idx];
  4014. break;
  4015. default:
  4016. return;
  4017. }
  4018. dest_map->skb = src_map->skb;
  4019. dma_unmap_addr_set(dest_map, mapping,
  4020. dma_unmap_addr(src_map, mapping));
  4021. dest_desc->addr_hi = src_desc->addr_hi;
  4022. dest_desc->addr_lo = src_desc->addr_lo;
  4023. /* Ensure that the update to the skb happens after the physical
  4024. * addresses have been transferred to the new BD location.
  4025. */
  4026. smp_wmb();
  4027. src_map->skb = NULL;
  4028. }
  4029. /* The RX ring scheme is composed of multiple rings which post fresh
  4030. * buffers to the chip, and one special ring the chip uses to report
  4031. * status back to the host.
  4032. *
  4033. * The special ring reports the status of received packets to the
  4034. * host. The chip does not write into the original descriptor the
  4035. * RX buffer was obtained from. The chip simply takes the original
  4036. * descriptor as provided by the host, updates the status and length
  4037. * field, then writes this into the next status ring entry.
  4038. *
  4039. * Each ring the host uses to post buffers to the chip is described
  4040. * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
  4041. * it is first placed into the on-chip ram. When the packet's length
  4042. * is known, it walks down the TG3_BDINFO entries to select the ring.
  4043. * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
  4044. * which is within the range of the new packet's length is chosen.
  4045. *
  4046. * The "separate ring for rx status" scheme may sound queer, but it makes
  4047. * sense from a cache coherency perspective. If only the host writes
  4048. * to the buffer post rings, and only the chip writes to the rx status
  4049. * rings, then cache lines never move beyond shared-modified state.
  4050. * If both the host and chip were to write into the same ring, cache line
  4051. * eviction could occur since both entities want it in an exclusive state.
  4052. */
  4053. static int tg3_rx(struct tg3_napi *tnapi, int budget)
  4054. {
  4055. struct tg3 *tp = tnapi->tp;
  4056. u32 work_mask, rx_std_posted = 0;
  4057. u32 std_prod_idx, jmb_prod_idx;
  4058. u32 sw_idx = tnapi->rx_rcb_ptr;
  4059. u16 hw_idx;
  4060. int received;
  4061. struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
  4062. hw_idx = *(tnapi->rx_rcb_prod_idx);
  4063. /*
  4064. * We need to order the read of hw_idx and the read of
  4065. * the opaque cookie.
  4066. */
  4067. rmb();
  4068. work_mask = 0;
  4069. received = 0;
  4070. std_prod_idx = tpr->rx_std_prod_idx;
  4071. jmb_prod_idx = tpr->rx_jmb_prod_idx;
  4072. while (sw_idx != hw_idx && budget > 0) {
  4073. struct ring_info *ri;
  4074. struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
  4075. unsigned int len;
  4076. struct sk_buff *skb;
  4077. dma_addr_t dma_addr;
  4078. u32 opaque_key, desc_idx, *post_ptr;
  4079. desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
  4080. opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
  4081. if (opaque_key == RXD_OPAQUE_RING_STD) {
  4082. ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
  4083. dma_addr = dma_unmap_addr(ri, mapping);
  4084. skb = ri->skb;
  4085. post_ptr = &std_prod_idx;
  4086. rx_std_posted++;
  4087. } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
  4088. ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
  4089. dma_addr = dma_unmap_addr(ri, mapping);
  4090. skb = ri->skb;
  4091. post_ptr = &jmb_prod_idx;
  4092. } else
  4093. goto next_pkt_nopost;
  4094. work_mask |= opaque_key;
  4095. if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
  4096. (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
  4097. drop_it:
  4098. tg3_recycle_rx(tnapi, tpr, opaque_key,
  4099. desc_idx, *post_ptr);
  4100. drop_it_no_recycle:
  4101. /* Other statistics kept track of by card. */
  4102. tp->rx_dropped++;
  4103. goto next_pkt;
  4104. }
  4105. len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
  4106. ETH_FCS_LEN;
  4107. if (len > TG3_RX_COPY_THRESH(tp)) {
  4108. int skb_size;
  4109. skb_size = tg3_alloc_rx_skb(tp, tpr, opaque_key,
  4110. *post_ptr);
  4111. if (skb_size < 0)
  4112. goto drop_it;
  4113. pci_unmap_single(tp->pdev, dma_addr, skb_size,
  4114. PCI_DMA_FROMDEVICE);
  4115. /* Ensure that the update to the skb happens
  4116. * after the usage of the old DMA mapping.
  4117. */
  4118. smp_wmb();
  4119. ri->skb = NULL;
  4120. skb_put(skb, len);
  4121. } else {
  4122. struct sk_buff *copy_skb;
  4123. tg3_recycle_rx(tnapi, tpr, opaque_key,
  4124. desc_idx, *post_ptr);
  4125. copy_skb = netdev_alloc_skb(tp->dev, len +
  4126. TG3_RAW_IP_ALIGN);
  4127. if (copy_skb == NULL)
  4128. goto drop_it_no_recycle;
  4129. skb_reserve(copy_skb, TG3_RAW_IP_ALIGN);
  4130. skb_put(copy_skb, len);
  4131. pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
  4132. skb_copy_from_linear_data(skb, copy_skb->data, len);
  4133. pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
  4134. /* We'll reuse the original ring buffer. */
  4135. skb = copy_skb;
  4136. }
  4137. if ((tp->dev->features & NETIF_F_RXCSUM) &&
  4138. (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
  4139. (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
  4140. >> RXD_TCPCSUM_SHIFT) == 0xffff))
  4141. skb->ip_summed = CHECKSUM_UNNECESSARY;
  4142. else
  4143. skb_checksum_none_assert(skb);
  4144. skb->protocol = eth_type_trans(skb, tp->dev);
  4145. if (len > (tp->dev->mtu + ETH_HLEN) &&
  4146. skb->protocol != htons(ETH_P_8021Q)) {
  4147. dev_kfree_skb(skb);
  4148. goto drop_it_no_recycle;
  4149. }
  4150. if (desc->type_flags & RXD_FLAG_VLAN &&
  4151. !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
  4152. __vlan_hwaccel_put_tag(skb,
  4153. desc->err_vlan & RXD_VLAN_MASK);
  4154. napi_gro_receive(&tnapi->napi, skb);
  4155. received++;
  4156. budget--;
  4157. next_pkt:
  4158. (*post_ptr)++;
  4159. if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
  4160. tpr->rx_std_prod_idx = std_prod_idx &
  4161. tp->rx_std_ring_mask;
  4162. tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
  4163. tpr->rx_std_prod_idx);
  4164. work_mask &= ~RXD_OPAQUE_RING_STD;
  4165. rx_std_posted = 0;
  4166. }
  4167. next_pkt_nopost:
  4168. sw_idx++;
  4169. sw_idx &= tp->rx_ret_ring_mask;
  4170. /* Refresh hw_idx to see if there is new work */
  4171. if (sw_idx == hw_idx) {
  4172. hw_idx = *(tnapi->rx_rcb_prod_idx);
  4173. rmb();
  4174. }
  4175. }
  4176. /* ACK the status ring. */
  4177. tnapi->rx_rcb_ptr = sw_idx;
  4178. tw32_rx_mbox(tnapi->consmbox, sw_idx);
  4179. /* Refill RX ring(s). */
  4180. if (!tg3_flag(tp, ENABLE_RSS)) {
  4181. if (work_mask & RXD_OPAQUE_RING_STD) {
  4182. tpr->rx_std_prod_idx = std_prod_idx &
  4183. tp->rx_std_ring_mask;
  4184. tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
  4185. tpr->rx_std_prod_idx);
  4186. }
  4187. if (work_mask & RXD_OPAQUE_RING_JUMBO) {
  4188. tpr->rx_jmb_prod_idx = jmb_prod_idx &
  4189. tp->rx_jmb_ring_mask;
  4190. tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
  4191. tpr->rx_jmb_prod_idx);
  4192. }
  4193. mmiowb();
  4194. } else if (work_mask) {
  4195. /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
  4196. * updated before the producer indices can be updated.
  4197. */
  4198. smp_wmb();
  4199. tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
  4200. tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
  4201. if (tnapi != &tp->napi[1])
  4202. napi_schedule(&tp->napi[1].napi);
  4203. }
  4204. return received;
  4205. }
  4206. static void tg3_poll_link(struct tg3 *tp)
  4207. {
  4208. /* handle link change and other phy events */
  4209. if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
  4210. struct tg3_hw_status *sblk = tp->napi[0].hw_status;
  4211. if (sblk->status & SD_STATUS_LINK_CHG) {
  4212. sblk->status = SD_STATUS_UPDATED |
  4213. (sblk->status & ~SD_STATUS_LINK_CHG);
  4214. spin_lock(&tp->lock);
  4215. if (tg3_flag(tp, USE_PHYLIB)) {
  4216. tw32_f(MAC_STATUS,
  4217. (MAC_STATUS_SYNC_CHANGED |
  4218. MAC_STATUS_CFG_CHANGED |
  4219. MAC_STATUS_MI_COMPLETION |
  4220. MAC_STATUS_LNKSTATE_CHANGED));
  4221. udelay(40);
  4222. } else
  4223. tg3_setup_phy(tp, 0);
  4224. spin_unlock(&tp->lock);
  4225. }
  4226. }
  4227. }
  4228. static int tg3_rx_prodring_xfer(struct tg3 *tp,
  4229. struct tg3_rx_prodring_set *dpr,
  4230. struct tg3_rx_prodring_set *spr)
  4231. {
  4232. u32 si, di, cpycnt, src_prod_idx;
  4233. int i, err = 0;
  4234. while (1) {
  4235. src_prod_idx = spr->rx_std_prod_idx;
  4236. /* Make sure updates to the rx_std_buffers[] entries and the
  4237. * standard producer index are seen in the correct order.
  4238. */
  4239. smp_rmb();
  4240. if (spr->rx_std_cons_idx == src_prod_idx)
  4241. break;
  4242. if (spr->rx_std_cons_idx < src_prod_idx)
  4243. cpycnt = src_prod_idx - spr->rx_std_cons_idx;
  4244. else
  4245. cpycnt = tp->rx_std_ring_mask + 1 -
  4246. spr->rx_std_cons_idx;
  4247. cpycnt = min(cpycnt,
  4248. tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
  4249. si = spr->rx_std_cons_idx;
  4250. di = dpr->rx_std_prod_idx;
  4251. for (i = di; i < di + cpycnt; i++) {
  4252. if (dpr->rx_std_buffers[i].skb) {
  4253. cpycnt = i - di;
  4254. err = -ENOSPC;
  4255. break;
  4256. }
  4257. }
  4258. if (!cpycnt)
  4259. break;
  4260. /* Ensure that updates to the rx_std_buffers ring and the
  4261. * shadowed hardware producer ring from tg3_recycle_skb() are
  4262. * ordered correctly WRT the skb check above.
  4263. */
  4264. smp_rmb();
  4265. memcpy(&dpr->rx_std_buffers[di],
  4266. &spr->rx_std_buffers[si],
  4267. cpycnt * sizeof(struct ring_info));
  4268. for (i = 0; i < cpycnt; i++, di++, si++) {
  4269. struct tg3_rx_buffer_desc *sbd, *dbd;
  4270. sbd = &spr->rx_std[si];
  4271. dbd = &dpr->rx_std[di];
  4272. dbd->addr_hi = sbd->addr_hi;
  4273. dbd->addr_lo = sbd->addr_lo;
  4274. }
  4275. spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
  4276. tp->rx_std_ring_mask;
  4277. dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
  4278. tp->rx_std_ring_mask;
  4279. }
  4280. while (1) {
  4281. src_prod_idx = spr->rx_jmb_prod_idx;
  4282. /* Make sure updates to the rx_jmb_buffers[] entries and
  4283. * the jumbo producer index are seen in the correct order.
  4284. */
  4285. smp_rmb();
  4286. if (spr->rx_jmb_cons_idx == src_prod_idx)
  4287. break;
  4288. if (spr->rx_jmb_cons_idx < src_prod_idx)
  4289. cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
  4290. else
  4291. cpycnt = tp->rx_jmb_ring_mask + 1 -
  4292. spr->rx_jmb_cons_idx;
  4293. cpycnt = min(cpycnt,
  4294. tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
  4295. si = spr->rx_jmb_cons_idx;
  4296. di = dpr->rx_jmb_prod_idx;
  4297. for (i = di; i < di + cpycnt; i++) {
  4298. if (dpr->rx_jmb_buffers[i].skb) {
  4299. cpycnt = i - di;
  4300. err = -ENOSPC;
  4301. break;
  4302. }
  4303. }
  4304. if (!cpycnt)
  4305. break;
  4306. /* Ensure that updates to the rx_jmb_buffers ring and the
  4307. * shadowed hardware producer ring from tg3_recycle_skb() are
  4308. * ordered correctly WRT the skb check above.
  4309. */
  4310. smp_rmb();
  4311. memcpy(&dpr->rx_jmb_buffers[di],
  4312. &spr->rx_jmb_buffers[si],
  4313. cpycnt * sizeof(struct ring_info));
  4314. for (i = 0; i < cpycnt; i++, di++, si++) {
  4315. struct tg3_rx_buffer_desc *sbd, *dbd;
  4316. sbd = &spr->rx_jmb[si].std;
  4317. dbd = &dpr->rx_jmb[di].std;
  4318. dbd->addr_hi = sbd->addr_hi;
  4319. dbd->addr_lo = sbd->addr_lo;
  4320. }
  4321. spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
  4322. tp->rx_jmb_ring_mask;
  4323. dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
  4324. tp->rx_jmb_ring_mask;
  4325. }
  4326. return err;
  4327. }
  4328. static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
  4329. {
  4330. struct tg3 *tp = tnapi->tp;
  4331. /* run TX completion thread */
  4332. if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
  4333. tg3_tx(tnapi);
  4334. if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
  4335. return work_done;
  4336. }
  4337. /* run RX thread, within the bounds set by NAPI.
  4338. * All RX "locking" is done by ensuring outside
  4339. * code synchronizes with tg3->napi.poll()
  4340. */
  4341. if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
  4342. work_done += tg3_rx(tnapi, budget - work_done);
  4343. if (tg3_flag(tp, ENABLE_RSS) && tnapi == &tp->napi[1]) {
  4344. struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
  4345. int i, err = 0;
  4346. u32 std_prod_idx = dpr->rx_std_prod_idx;
  4347. u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
  4348. for (i = 1; i < tp->irq_cnt; i++)
  4349. err |= tg3_rx_prodring_xfer(tp, dpr,
  4350. &tp->napi[i].prodring);
  4351. wmb();
  4352. if (std_prod_idx != dpr->rx_std_prod_idx)
  4353. tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
  4354. dpr->rx_std_prod_idx);
  4355. if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
  4356. tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
  4357. dpr->rx_jmb_prod_idx);
  4358. mmiowb();
  4359. if (err)
  4360. tw32_f(HOSTCC_MODE, tp->coal_now);
  4361. }
  4362. return work_done;
  4363. }
  4364. static int tg3_poll_msix(struct napi_struct *napi, int budget)
  4365. {
  4366. struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
  4367. struct tg3 *tp = tnapi->tp;
  4368. int work_done = 0;
  4369. struct tg3_hw_status *sblk = tnapi->hw_status;
  4370. while (1) {
  4371. work_done = tg3_poll_work(tnapi, work_done, budget);
  4372. if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
  4373. goto tx_recovery;
  4374. if (unlikely(work_done >= budget))
  4375. break;
  4376. /* tp->last_tag is used in tg3_int_reenable() below
  4377. * to tell the hw how much work has been processed,
  4378. * so we must read it before checking for more work.
  4379. */
  4380. tnapi->last_tag = sblk->status_tag;
  4381. tnapi->last_irq_tag = tnapi->last_tag;
  4382. rmb();
  4383. /* check for RX/TX work to do */
  4384. if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
  4385. *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
  4386. napi_complete(napi);
  4387. /* Reenable interrupts. */
  4388. tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
  4389. mmiowb();
  4390. break;
  4391. }
  4392. }
  4393. return work_done;
  4394. tx_recovery:
  4395. /* work_done is guaranteed to be less than budget. */
  4396. napi_complete(napi);
  4397. schedule_work(&tp->reset_task);
  4398. return work_done;
  4399. }
  4400. static void tg3_process_error(struct tg3 *tp)
  4401. {
  4402. u32 val;
  4403. bool real_error = false;
  4404. if (tg3_flag(tp, ERROR_PROCESSED))
  4405. return;
  4406. /* Check Flow Attention register */
  4407. val = tr32(HOSTCC_FLOW_ATTN);
  4408. if (val & ~HOSTCC_FLOW_ATTN_MBUF_LWM) {
  4409. netdev_err(tp->dev, "FLOW Attention error. Resetting chip.\n");
  4410. real_error = true;
  4411. }
  4412. if (tr32(MSGINT_STATUS) & ~MSGINT_STATUS_MSI_REQ) {
  4413. netdev_err(tp->dev, "MSI Status error. Resetting chip.\n");
  4414. real_error = true;
  4415. }
  4416. if (tr32(RDMAC_STATUS) || tr32(WDMAC_STATUS)) {
  4417. netdev_err(tp->dev, "DMA Status error. Resetting chip.\n");
  4418. real_error = true;
  4419. }
  4420. if (!real_error)
  4421. return;
  4422. tg3_dump_state(tp);
  4423. tg3_flag_set(tp, ERROR_PROCESSED);
  4424. schedule_work(&tp->reset_task);
  4425. }
  4426. static int tg3_poll(struct napi_struct *napi, int budget)
  4427. {
  4428. struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
  4429. struct tg3 *tp = tnapi->tp;
  4430. int work_done = 0;
  4431. struct tg3_hw_status *sblk = tnapi->hw_status;
  4432. while (1) {
  4433. if (sblk->status & SD_STATUS_ERROR)
  4434. tg3_process_error(tp);
  4435. tg3_poll_link(tp);
  4436. work_done = tg3_poll_work(tnapi, work_done, budget);
  4437. if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
  4438. goto tx_recovery;
  4439. if (unlikely(work_done >= budget))
  4440. break;
  4441. if (tg3_flag(tp, TAGGED_STATUS)) {
  4442. /* tp->last_tag is used in tg3_int_reenable() below
  4443. * to tell the hw how much work has been processed,
  4444. * so we must read it before checking for more work.
  4445. */
  4446. tnapi->last_tag = sblk->status_tag;
  4447. tnapi->last_irq_tag = tnapi->last_tag;
  4448. rmb();
  4449. } else
  4450. sblk->status &= ~SD_STATUS_UPDATED;
  4451. if (likely(!tg3_has_work(tnapi))) {
  4452. napi_complete(napi);
  4453. tg3_int_reenable(tnapi);
  4454. break;
  4455. }
  4456. }
  4457. return work_done;
  4458. tx_recovery:
  4459. /* work_done is guaranteed to be less than budget. */
  4460. napi_complete(napi);
  4461. schedule_work(&tp->reset_task);
  4462. return work_done;
  4463. }
  4464. static void tg3_napi_disable(struct tg3 *tp)
  4465. {
  4466. int i;
  4467. for (i = tp->irq_cnt - 1; i >= 0; i--)
  4468. napi_disable(&tp->napi[i].napi);
  4469. }
  4470. static void tg3_napi_enable(struct tg3 *tp)
  4471. {
  4472. int i;
  4473. for (i = 0; i < tp->irq_cnt; i++)
  4474. napi_enable(&tp->napi[i].napi);
  4475. }
  4476. static void tg3_napi_init(struct tg3 *tp)
  4477. {
  4478. int i;
  4479. netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
  4480. for (i = 1; i < tp->irq_cnt; i++)
  4481. netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
  4482. }
  4483. static void tg3_napi_fini(struct tg3 *tp)
  4484. {
  4485. int i;
  4486. for (i = 0; i < tp->irq_cnt; i++)
  4487. netif_napi_del(&tp->napi[i].napi);
  4488. }
  4489. static inline void tg3_netif_stop(struct tg3 *tp)
  4490. {
  4491. tp->dev->trans_start = jiffies; /* prevent tx timeout */
  4492. tg3_napi_disable(tp);
  4493. netif_tx_disable(tp->dev);
  4494. }
  4495. static inline void tg3_netif_start(struct tg3 *tp)
  4496. {
  4497. /* NOTE: unconditional netif_tx_wake_all_queues is only
  4498. * appropriate so long as all callers are assured to
  4499. * have free tx slots (such as after tg3_init_hw)
  4500. */
  4501. netif_tx_wake_all_queues(tp->dev);
  4502. tg3_napi_enable(tp);
  4503. tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
  4504. tg3_enable_ints(tp);
  4505. }
  4506. static void tg3_irq_quiesce(struct tg3 *tp)
  4507. {
  4508. int i;
  4509. BUG_ON(tp->irq_sync);
  4510. tp->irq_sync = 1;
  4511. smp_mb();
  4512. for (i = 0; i < tp->irq_cnt; i++)
  4513. synchronize_irq(tp->napi[i].irq_vec);
  4514. }
  4515. /* Fully shutdown all tg3 driver activity elsewhere in the system.
  4516. * If irq_sync is non-zero, then the IRQ handler must be synchronized
  4517. * with as well. Most of the time, this is not necessary except when
  4518. * shutting down the device.
  4519. */
  4520. static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
  4521. {
  4522. spin_lock_bh(&tp->lock);
  4523. if (irq_sync)
  4524. tg3_irq_quiesce(tp);
  4525. }
  4526. static inline void tg3_full_unlock(struct tg3 *tp)
  4527. {
  4528. spin_unlock_bh(&tp->lock);
  4529. }
  4530. /* One-shot MSI handler - Chip automatically disables interrupt
  4531. * after sending MSI so driver doesn't have to do it.
  4532. */
  4533. static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
  4534. {
  4535. struct tg3_napi *tnapi = dev_id;
  4536. struct tg3 *tp = tnapi->tp;
  4537. prefetch(tnapi->hw_status);
  4538. if (tnapi->rx_rcb)
  4539. prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
  4540. if (likely(!tg3_irq_sync(tp)))
  4541. napi_schedule(&tnapi->napi);
  4542. return IRQ_HANDLED;
  4543. }
  4544. /* MSI ISR - No need to check for interrupt sharing and no need to
  4545. * flush status block and interrupt mailbox. PCI ordering rules
  4546. * guarantee that MSI will arrive after the status block.
  4547. */
  4548. static irqreturn_t tg3_msi(int irq, void *dev_id)
  4549. {
  4550. struct tg3_napi *tnapi = dev_id;
  4551. struct tg3 *tp = tnapi->tp;
  4552. prefetch(tnapi->hw_status);
  4553. if (tnapi->rx_rcb)
  4554. prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
  4555. /*
  4556. * Writing any value to intr-mbox-0 clears PCI INTA# and
  4557. * chip-internal interrupt pending events.
  4558. * Writing non-zero to intr-mbox-0 additional tells the
  4559. * NIC to stop sending us irqs, engaging "in-intr-handler"
  4560. * event coalescing.
  4561. */
  4562. tw32_mailbox(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
  4563. if (likely(!tg3_irq_sync(tp)))
  4564. napi_schedule(&tnapi->napi);
  4565. return IRQ_RETVAL(1);
  4566. }
  4567. static irqreturn_t tg3_interrupt(int irq, void *dev_id)
  4568. {
  4569. struct tg3_napi *tnapi = dev_id;
  4570. struct tg3 *tp = tnapi->tp;
  4571. struct tg3_hw_status *sblk = tnapi->hw_status;
  4572. unsigned int handled = 1;
  4573. /* In INTx mode, it is possible for the interrupt to arrive at
  4574. * the CPU before the status block posted prior to the interrupt.
  4575. * Reading the PCI State register will confirm whether the
  4576. * interrupt is ours and will flush the status block.
  4577. */
  4578. if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
  4579. if (tg3_flag(tp, CHIP_RESETTING) ||
  4580. (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
  4581. handled = 0;
  4582. goto out;
  4583. }
  4584. }
  4585. /*
  4586. * Writing any value to intr-mbox-0 clears PCI INTA# and
  4587. * chip-internal interrupt pending events.
  4588. * Writing non-zero to intr-mbox-0 additional tells the
  4589. * NIC to stop sending us irqs, engaging "in-intr-handler"
  4590. * event coalescing.
  4591. *
  4592. * Flush the mailbox to de-assert the IRQ immediately to prevent
  4593. * spurious interrupts. The flush impacts performance but
  4594. * excessive spurious interrupts can be worse in some cases.
  4595. */
  4596. tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
  4597. if (tg3_irq_sync(tp))
  4598. goto out;
  4599. sblk->status &= ~SD_STATUS_UPDATED;
  4600. if (likely(tg3_has_work(tnapi))) {
  4601. prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
  4602. napi_schedule(&tnapi->napi);
  4603. } else {
  4604. /* No work, shared interrupt perhaps? re-enable
  4605. * interrupts, and flush that PCI write
  4606. */
  4607. tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
  4608. 0x00000000);
  4609. }
  4610. out:
  4611. return IRQ_RETVAL(handled);
  4612. }
  4613. static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
  4614. {
  4615. struct tg3_napi *tnapi = dev_id;
  4616. struct tg3 *tp = tnapi->tp;
  4617. struct tg3_hw_status *sblk = tnapi->hw_status;
  4618. unsigned int handled = 1;
  4619. /* In INTx mode, it is possible for the interrupt to arrive at
  4620. * the CPU before the status block posted prior to the interrupt.
  4621. * Reading the PCI State register will confirm whether the
  4622. * interrupt is ours and will flush the status block.
  4623. */
  4624. if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
  4625. if (tg3_flag(tp, CHIP_RESETTING) ||
  4626. (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
  4627. handled = 0;
  4628. goto out;
  4629. }
  4630. }
  4631. /*
  4632. * writing any value to intr-mbox-0 clears PCI INTA# and
  4633. * chip-internal interrupt pending events.
  4634. * writing non-zero to intr-mbox-0 additional tells the
  4635. * NIC to stop sending us irqs, engaging "in-intr-handler"
  4636. * event coalescing.
  4637. *
  4638. * Flush the mailbox to de-assert the IRQ immediately to prevent
  4639. * spurious interrupts. The flush impacts performance but
  4640. * excessive spurious interrupts can be worse in some cases.
  4641. */
  4642. tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
  4643. /*
  4644. * In a shared interrupt configuration, sometimes other devices'
  4645. * interrupts will scream. We record the current status tag here
  4646. * so that the above check can report that the screaming interrupts
  4647. * are unhandled. Eventually they will be silenced.
  4648. */
  4649. tnapi->last_irq_tag = sblk->status_tag;
  4650. if (tg3_irq_sync(tp))
  4651. goto out;
  4652. prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
  4653. napi_schedule(&tnapi->napi);
  4654. out:
  4655. return IRQ_RETVAL(handled);
  4656. }
  4657. /* ISR for interrupt test */
  4658. static irqreturn_t tg3_test_isr(int irq, void *dev_id)
  4659. {
  4660. struct tg3_napi *tnapi = dev_id;
  4661. struct tg3 *tp = tnapi->tp;
  4662. struct tg3_hw_status *sblk = tnapi->hw_status;
  4663. if ((sblk->status & SD_STATUS_UPDATED) ||
  4664. !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
  4665. tg3_disable_ints(tp);
  4666. return IRQ_RETVAL(1);
  4667. }
  4668. return IRQ_RETVAL(0);
  4669. }
  4670. static int tg3_init_hw(struct tg3 *, int);
  4671. static int tg3_halt(struct tg3 *, int, int);
  4672. /* Restart hardware after configuration changes, self-test, etc.
  4673. * Invoked with tp->lock held.
  4674. */
  4675. static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
  4676. __releases(tp->lock)
  4677. __acquires(tp->lock)
  4678. {
  4679. int err;
  4680. err = tg3_init_hw(tp, reset_phy);
  4681. if (err) {
  4682. netdev_err(tp->dev,
  4683. "Failed to re-initialize device, aborting\n");
  4684. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  4685. tg3_full_unlock(tp);
  4686. del_timer_sync(&tp->timer);
  4687. tp->irq_sync = 0;
  4688. tg3_napi_enable(tp);
  4689. dev_close(tp->dev);
  4690. tg3_full_lock(tp, 0);
  4691. }
  4692. return err;
  4693. }
  4694. #ifdef CONFIG_NET_POLL_CONTROLLER
  4695. static void tg3_poll_controller(struct net_device *dev)
  4696. {
  4697. int i;
  4698. struct tg3 *tp = netdev_priv(dev);
  4699. for (i = 0; i < tp->irq_cnt; i++)
  4700. tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
  4701. }
  4702. #endif
  4703. static void tg3_reset_task(struct work_struct *work)
  4704. {
  4705. struct tg3 *tp = container_of(work, struct tg3, reset_task);
  4706. int err;
  4707. unsigned int restart_timer;
  4708. tg3_full_lock(tp, 0);
  4709. if (!netif_running(tp->dev)) {
  4710. tg3_full_unlock(tp);
  4711. return;
  4712. }
  4713. tg3_full_unlock(tp);
  4714. tg3_phy_stop(tp);
  4715. tg3_netif_stop(tp);
  4716. tg3_full_lock(tp, 1);
  4717. restart_timer = tg3_flag(tp, RESTART_TIMER);
  4718. tg3_flag_clear(tp, RESTART_TIMER);
  4719. if (tg3_flag(tp, TX_RECOVERY_PENDING)) {
  4720. tp->write32_tx_mbox = tg3_write32_tx_mbox;
  4721. tp->write32_rx_mbox = tg3_write_flush_reg32;
  4722. tg3_flag_set(tp, MBOX_WRITE_REORDER);
  4723. tg3_flag_clear(tp, TX_RECOVERY_PENDING);
  4724. }
  4725. tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
  4726. err = tg3_init_hw(tp, 1);
  4727. if (err)
  4728. goto out;
  4729. tg3_netif_start(tp);
  4730. if (restart_timer)
  4731. mod_timer(&tp->timer, jiffies + 1);
  4732. out:
  4733. tg3_full_unlock(tp);
  4734. if (!err)
  4735. tg3_phy_start(tp);
  4736. }
  4737. static void tg3_tx_timeout(struct net_device *dev)
  4738. {
  4739. struct tg3 *tp = netdev_priv(dev);
  4740. if (netif_msg_tx_err(tp)) {
  4741. netdev_err(dev, "transmit timed out, resetting\n");
  4742. tg3_dump_state(tp);
  4743. }
  4744. schedule_work(&tp->reset_task);
  4745. }
  4746. /* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
  4747. static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
  4748. {
  4749. u32 base = (u32) mapping & 0xffffffff;
  4750. return (base > 0xffffdcc0) && (base + len + 8 < base);
  4751. }
  4752. /* Test for DMA addresses > 40-bit */
  4753. static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
  4754. int len)
  4755. {
  4756. #if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
  4757. if (tg3_flag(tp, 40BIT_DMA_BUG))
  4758. return ((u64) mapping + len) > DMA_BIT_MASK(40);
  4759. return 0;
  4760. #else
  4761. return 0;
  4762. #endif
  4763. }
  4764. static void tg3_set_txd(struct tg3_napi *tnapi, int entry,
  4765. dma_addr_t mapping, int len, u32 flags,
  4766. u32 mss_and_is_end)
  4767. {
  4768. struct tg3_tx_buffer_desc *txd = &tnapi->tx_ring[entry];
  4769. int is_end = (mss_and_is_end & 0x1);
  4770. u32 mss = (mss_and_is_end >> 1);
  4771. u32 vlan_tag = 0;
  4772. if (is_end)
  4773. flags |= TXD_FLAG_END;
  4774. if (flags & TXD_FLAG_VLAN) {
  4775. vlan_tag = flags >> 16;
  4776. flags &= 0xffff;
  4777. }
  4778. vlan_tag |= (mss << TXD_MSS_SHIFT);
  4779. txd->addr_hi = ((u64) mapping >> 32);
  4780. txd->addr_lo = ((u64) mapping & 0xffffffff);
  4781. txd->len_flags = (len << TXD_LEN_SHIFT) | flags;
  4782. txd->vlan_tag = vlan_tag << TXD_VLAN_TAG_SHIFT;
  4783. }
  4784. static void tg3_skb_error_unmap(struct tg3_napi *tnapi,
  4785. struct sk_buff *skb, int last)
  4786. {
  4787. int i;
  4788. u32 entry = tnapi->tx_prod;
  4789. struct ring_info *txb = &tnapi->tx_buffers[entry];
  4790. pci_unmap_single(tnapi->tp->pdev,
  4791. dma_unmap_addr(txb, mapping),
  4792. skb_headlen(skb),
  4793. PCI_DMA_TODEVICE);
  4794. for (i = 0; i < last; i++) {
  4795. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  4796. entry = NEXT_TX(entry);
  4797. txb = &tnapi->tx_buffers[entry];
  4798. pci_unmap_page(tnapi->tp->pdev,
  4799. dma_unmap_addr(txb, mapping),
  4800. frag->size, PCI_DMA_TODEVICE);
  4801. }
  4802. }
  4803. /* Workaround 4GB and 40-bit hardware DMA bugs. */
  4804. static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
  4805. struct sk_buff *skb,
  4806. u32 base_flags, u32 mss)
  4807. {
  4808. struct tg3 *tp = tnapi->tp;
  4809. struct sk_buff *new_skb;
  4810. dma_addr_t new_addr = 0;
  4811. u32 entry = tnapi->tx_prod;
  4812. int ret = 0;
  4813. if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
  4814. new_skb = skb_copy(skb, GFP_ATOMIC);
  4815. else {
  4816. int more_headroom = 4 - ((unsigned long)skb->data & 3);
  4817. new_skb = skb_copy_expand(skb,
  4818. skb_headroom(skb) + more_headroom,
  4819. skb_tailroom(skb), GFP_ATOMIC);
  4820. }
  4821. if (!new_skb) {
  4822. ret = -1;
  4823. } else {
  4824. /* New SKB is guaranteed to be linear. */
  4825. new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
  4826. PCI_DMA_TODEVICE);
  4827. /* Make sure the mapping succeeded */
  4828. if (pci_dma_mapping_error(tp->pdev, new_addr)) {
  4829. ret = -1;
  4830. dev_kfree_skb(new_skb);
  4831. /* Make sure new skb does not cross any 4G boundaries.
  4832. * Drop the packet if it does.
  4833. */
  4834. } else if (tg3_flag(tp, 4G_DMA_BNDRY_BUG) &&
  4835. tg3_4g_overflow_test(new_addr, new_skb->len)) {
  4836. pci_unmap_single(tp->pdev, new_addr, new_skb->len,
  4837. PCI_DMA_TODEVICE);
  4838. ret = -1;
  4839. dev_kfree_skb(new_skb);
  4840. } else {
  4841. tnapi->tx_buffers[entry].skb = new_skb;
  4842. dma_unmap_addr_set(&tnapi->tx_buffers[entry],
  4843. mapping, new_addr);
  4844. tg3_set_txd(tnapi, entry, new_addr, new_skb->len,
  4845. base_flags, 1 | (mss << 1));
  4846. }
  4847. }
  4848. dev_kfree_skb(skb);
  4849. return ret;
  4850. }
  4851. static netdev_tx_t tg3_start_xmit(struct sk_buff *, struct net_device *);
  4852. /* Use GSO to workaround a rare TSO bug that may be triggered when the
  4853. * TSO header is greater than 80 bytes.
  4854. */
  4855. static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
  4856. {
  4857. struct sk_buff *segs, *nskb;
  4858. u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
  4859. /* Estimate the number of fragments in the worst case */
  4860. if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
  4861. netif_stop_queue(tp->dev);
  4862. /* netif_tx_stop_queue() must be done before checking
  4863. * checking tx index in tg3_tx_avail() below, because in
  4864. * tg3_tx(), we update tx index before checking for
  4865. * netif_tx_queue_stopped().
  4866. */
  4867. smp_mb();
  4868. if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
  4869. return NETDEV_TX_BUSY;
  4870. netif_wake_queue(tp->dev);
  4871. }
  4872. segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
  4873. if (IS_ERR(segs))
  4874. goto tg3_tso_bug_end;
  4875. do {
  4876. nskb = segs;
  4877. segs = segs->next;
  4878. nskb->next = NULL;
  4879. tg3_start_xmit(nskb, tp->dev);
  4880. } while (segs);
  4881. tg3_tso_bug_end:
  4882. dev_kfree_skb(skb);
  4883. return NETDEV_TX_OK;
  4884. }
  4885. /* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
  4886. * support TG3_FLAG_HW_TSO_1 or firmware TSO only.
  4887. */
  4888. static netdev_tx_t tg3_start_xmit(struct sk_buff *skb, struct net_device *dev)
  4889. {
  4890. struct tg3 *tp = netdev_priv(dev);
  4891. u32 len, entry, base_flags, mss;
  4892. int i = -1, would_hit_hwbug;
  4893. dma_addr_t mapping;
  4894. struct tg3_napi *tnapi;
  4895. struct netdev_queue *txq;
  4896. unsigned int last;
  4897. txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
  4898. tnapi = &tp->napi[skb_get_queue_mapping(skb)];
  4899. if (tg3_flag(tp, ENABLE_TSS))
  4900. tnapi++;
  4901. /* We are running in BH disabled context with netif_tx_lock
  4902. * and TX reclaim runs via tp->napi.poll inside of a software
  4903. * interrupt. Furthermore, IRQ processing runs lockless so we have
  4904. * no IRQ context deadlocks to worry about either. Rejoice!
  4905. */
  4906. if (unlikely(tg3_tx_avail(tnapi) <= (skb_shinfo(skb)->nr_frags + 1))) {
  4907. if (!netif_tx_queue_stopped(txq)) {
  4908. netif_tx_stop_queue(txq);
  4909. /* This is a hard error, log it. */
  4910. netdev_err(dev,
  4911. "BUG! Tx Ring full when queue awake!\n");
  4912. }
  4913. return NETDEV_TX_BUSY;
  4914. }
  4915. entry = tnapi->tx_prod;
  4916. base_flags = 0;
  4917. if (skb->ip_summed == CHECKSUM_PARTIAL)
  4918. base_flags |= TXD_FLAG_TCPUDP_CSUM;
  4919. mss = skb_shinfo(skb)->gso_size;
  4920. if (mss) {
  4921. struct iphdr *iph;
  4922. u32 tcp_opt_len, hdr_len;
  4923. if (skb_header_cloned(skb) &&
  4924. pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
  4925. dev_kfree_skb(skb);
  4926. goto out_unlock;
  4927. }
  4928. iph = ip_hdr(skb);
  4929. tcp_opt_len = tcp_optlen(skb);
  4930. if (skb_is_gso_v6(skb)) {
  4931. hdr_len = skb_headlen(skb) - ETH_HLEN;
  4932. } else {
  4933. u32 ip_tcp_len;
  4934. ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
  4935. hdr_len = ip_tcp_len + tcp_opt_len;
  4936. iph->check = 0;
  4937. iph->tot_len = htons(mss + hdr_len);
  4938. }
  4939. if (unlikely((ETH_HLEN + hdr_len) > 80) &&
  4940. tg3_flag(tp, TSO_BUG))
  4941. return tg3_tso_bug(tp, skb);
  4942. base_flags |= (TXD_FLAG_CPU_PRE_DMA |
  4943. TXD_FLAG_CPU_POST_DMA);
  4944. if (tg3_flag(tp, HW_TSO_1) ||
  4945. tg3_flag(tp, HW_TSO_2) ||
  4946. tg3_flag(tp, HW_TSO_3)) {
  4947. tcp_hdr(skb)->check = 0;
  4948. base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
  4949. } else
  4950. tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
  4951. iph->daddr, 0,
  4952. IPPROTO_TCP,
  4953. 0);
  4954. if (tg3_flag(tp, HW_TSO_3)) {
  4955. mss |= (hdr_len & 0xc) << 12;
  4956. if (hdr_len & 0x10)
  4957. base_flags |= 0x00000010;
  4958. base_flags |= (hdr_len & 0x3e0) << 5;
  4959. } else if (tg3_flag(tp, HW_TSO_2))
  4960. mss |= hdr_len << 9;
  4961. else if (tg3_flag(tp, HW_TSO_1) ||
  4962. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
  4963. if (tcp_opt_len || iph->ihl > 5) {
  4964. int tsflags;
  4965. tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
  4966. mss |= (tsflags << 11);
  4967. }
  4968. } else {
  4969. if (tcp_opt_len || iph->ihl > 5) {
  4970. int tsflags;
  4971. tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
  4972. base_flags |= tsflags << 12;
  4973. }
  4974. }
  4975. }
  4976. if (vlan_tx_tag_present(skb))
  4977. base_flags |= (TXD_FLAG_VLAN |
  4978. (vlan_tx_tag_get(skb) << 16));
  4979. if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
  4980. !mss && skb->len > VLAN_ETH_FRAME_LEN)
  4981. base_flags |= TXD_FLAG_JMB_PKT;
  4982. len = skb_headlen(skb);
  4983. mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
  4984. if (pci_dma_mapping_error(tp->pdev, mapping)) {
  4985. dev_kfree_skb(skb);
  4986. goto out_unlock;
  4987. }
  4988. tnapi->tx_buffers[entry].skb = skb;
  4989. dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
  4990. would_hit_hwbug = 0;
  4991. if (tg3_flag(tp, SHORT_DMA_BUG) && len <= 8)
  4992. would_hit_hwbug = 1;
  4993. if (tg3_flag(tp, 4G_DMA_BNDRY_BUG) &&
  4994. tg3_4g_overflow_test(mapping, len))
  4995. would_hit_hwbug = 1;
  4996. if (tg3_flag(tp, 40BIT_DMA_LIMIT_BUG) &&
  4997. tg3_40bit_overflow_test(tp, mapping, len))
  4998. would_hit_hwbug = 1;
  4999. if (tg3_flag(tp, 5701_DMA_BUG))
  5000. would_hit_hwbug = 1;
  5001. tg3_set_txd(tnapi, entry, mapping, len, base_flags,
  5002. (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
  5003. entry = NEXT_TX(entry);
  5004. /* Now loop through additional data fragments, and queue them. */
  5005. if (skb_shinfo(skb)->nr_frags > 0) {
  5006. last = skb_shinfo(skb)->nr_frags - 1;
  5007. for (i = 0; i <= last; i++) {
  5008. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  5009. len = frag->size;
  5010. mapping = pci_map_page(tp->pdev,
  5011. frag->page,
  5012. frag->page_offset,
  5013. len, PCI_DMA_TODEVICE);
  5014. tnapi->tx_buffers[entry].skb = NULL;
  5015. dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
  5016. mapping);
  5017. if (pci_dma_mapping_error(tp->pdev, mapping))
  5018. goto dma_error;
  5019. if (tg3_flag(tp, SHORT_DMA_BUG) &&
  5020. len <= 8)
  5021. would_hit_hwbug = 1;
  5022. if (tg3_flag(tp, 4G_DMA_BNDRY_BUG) &&
  5023. tg3_4g_overflow_test(mapping, len))
  5024. would_hit_hwbug = 1;
  5025. if (tg3_flag(tp, 40BIT_DMA_LIMIT_BUG) &&
  5026. tg3_40bit_overflow_test(tp, mapping, len))
  5027. would_hit_hwbug = 1;
  5028. if (tg3_flag(tp, HW_TSO_1) ||
  5029. tg3_flag(tp, HW_TSO_2) ||
  5030. tg3_flag(tp, HW_TSO_3))
  5031. tg3_set_txd(tnapi, entry, mapping, len,
  5032. base_flags, (i == last)|(mss << 1));
  5033. else
  5034. tg3_set_txd(tnapi, entry, mapping, len,
  5035. base_flags, (i == last));
  5036. entry = NEXT_TX(entry);
  5037. }
  5038. }
  5039. if (would_hit_hwbug) {
  5040. tg3_skb_error_unmap(tnapi, skb, i);
  5041. /* If the workaround fails due to memory/mapping
  5042. * failure, silently drop this packet.
  5043. */
  5044. if (tigon3_dma_hwbug_workaround(tnapi, skb, base_flags, mss))
  5045. goto out_unlock;
  5046. entry = NEXT_TX(tnapi->tx_prod);
  5047. }
  5048. /* Packets are ready, update Tx producer idx local and on card. */
  5049. tw32_tx_mbox(tnapi->prodmbox, entry);
  5050. tnapi->tx_prod = entry;
  5051. if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
  5052. netif_tx_stop_queue(txq);
  5053. /* netif_tx_stop_queue() must be done before checking
  5054. * checking tx index in tg3_tx_avail() below, because in
  5055. * tg3_tx(), we update tx index before checking for
  5056. * netif_tx_queue_stopped().
  5057. */
  5058. smp_mb();
  5059. if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
  5060. netif_tx_wake_queue(txq);
  5061. }
  5062. out_unlock:
  5063. mmiowb();
  5064. return NETDEV_TX_OK;
  5065. dma_error:
  5066. tg3_skb_error_unmap(tnapi, skb, i);
  5067. dev_kfree_skb(skb);
  5068. tnapi->tx_buffers[tnapi->tx_prod].skb = NULL;
  5069. return NETDEV_TX_OK;
  5070. }
  5071. static void tg3_set_loopback(struct net_device *dev, u32 features)
  5072. {
  5073. struct tg3 *tp = netdev_priv(dev);
  5074. if (features & NETIF_F_LOOPBACK) {
  5075. if (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK)
  5076. return;
  5077. /*
  5078. * Clear MAC_MODE_HALF_DUPLEX or you won't get packets back in
  5079. * loopback mode if Half-Duplex mode was negotiated earlier.
  5080. */
  5081. tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
  5082. /* Enable internal MAC loopback mode */
  5083. tp->mac_mode |= MAC_MODE_PORT_INT_LPBACK;
  5084. spin_lock_bh(&tp->lock);
  5085. tw32(MAC_MODE, tp->mac_mode);
  5086. netif_carrier_on(tp->dev);
  5087. spin_unlock_bh(&tp->lock);
  5088. netdev_info(dev, "Internal MAC loopback mode enabled.\n");
  5089. } else {
  5090. if (!(tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
  5091. return;
  5092. /* Disable internal MAC loopback mode */
  5093. tp->mac_mode &= ~MAC_MODE_PORT_INT_LPBACK;
  5094. spin_lock_bh(&tp->lock);
  5095. tw32(MAC_MODE, tp->mac_mode);
  5096. /* Force link status check */
  5097. tg3_setup_phy(tp, 1);
  5098. spin_unlock_bh(&tp->lock);
  5099. netdev_info(dev, "Internal MAC loopback mode disabled.\n");
  5100. }
  5101. }
  5102. static u32 tg3_fix_features(struct net_device *dev, u32 features)
  5103. {
  5104. struct tg3 *tp = netdev_priv(dev);
  5105. if (dev->mtu > ETH_DATA_LEN && tg3_flag(tp, 5780_CLASS))
  5106. features &= ~NETIF_F_ALL_TSO;
  5107. return features;
  5108. }
  5109. static int tg3_set_features(struct net_device *dev, u32 features)
  5110. {
  5111. u32 changed = dev->features ^ features;
  5112. if ((changed & NETIF_F_LOOPBACK) && netif_running(dev))
  5113. tg3_set_loopback(dev, features);
  5114. return 0;
  5115. }
  5116. static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
  5117. int new_mtu)
  5118. {
  5119. dev->mtu = new_mtu;
  5120. if (new_mtu > ETH_DATA_LEN) {
  5121. if (tg3_flag(tp, 5780_CLASS)) {
  5122. netdev_update_features(dev);
  5123. tg3_flag_clear(tp, TSO_CAPABLE);
  5124. } else {
  5125. tg3_flag_set(tp, JUMBO_RING_ENABLE);
  5126. }
  5127. } else {
  5128. if (tg3_flag(tp, 5780_CLASS)) {
  5129. tg3_flag_set(tp, TSO_CAPABLE);
  5130. netdev_update_features(dev);
  5131. }
  5132. tg3_flag_clear(tp, JUMBO_RING_ENABLE);
  5133. }
  5134. }
  5135. static int tg3_change_mtu(struct net_device *dev, int new_mtu)
  5136. {
  5137. struct tg3 *tp = netdev_priv(dev);
  5138. int err;
  5139. if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
  5140. return -EINVAL;
  5141. if (!netif_running(dev)) {
  5142. /* We'll just catch it later when the
  5143. * device is up'd.
  5144. */
  5145. tg3_set_mtu(dev, tp, new_mtu);
  5146. return 0;
  5147. }
  5148. tg3_phy_stop(tp);
  5149. tg3_netif_stop(tp);
  5150. tg3_full_lock(tp, 1);
  5151. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  5152. tg3_set_mtu(dev, tp, new_mtu);
  5153. err = tg3_restart_hw(tp, 0);
  5154. if (!err)
  5155. tg3_netif_start(tp);
  5156. tg3_full_unlock(tp);
  5157. if (!err)
  5158. tg3_phy_start(tp);
  5159. return err;
  5160. }
  5161. static void tg3_rx_prodring_free(struct tg3 *tp,
  5162. struct tg3_rx_prodring_set *tpr)
  5163. {
  5164. int i;
  5165. if (tpr != &tp->napi[0].prodring) {
  5166. for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
  5167. i = (i + 1) & tp->rx_std_ring_mask)
  5168. tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
  5169. tp->rx_pkt_map_sz);
  5170. if (tg3_flag(tp, JUMBO_CAPABLE)) {
  5171. for (i = tpr->rx_jmb_cons_idx;
  5172. i != tpr->rx_jmb_prod_idx;
  5173. i = (i + 1) & tp->rx_jmb_ring_mask) {
  5174. tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
  5175. TG3_RX_JMB_MAP_SZ);
  5176. }
  5177. }
  5178. return;
  5179. }
  5180. for (i = 0; i <= tp->rx_std_ring_mask; i++)
  5181. tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
  5182. tp->rx_pkt_map_sz);
  5183. if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
  5184. for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
  5185. tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
  5186. TG3_RX_JMB_MAP_SZ);
  5187. }
  5188. }
  5189. /* Initialize rx rings for packet processing.
  5190. *
  5191. * The chip has been shut down and the driver detached from
  5192. * the networking, so no interrupts or new tx packets will
  5193. * end up in the driver. tp->{tx,}lock are held and thus
  5194. * we may not sleep.
  5195. */
  5196. static int tg3_rx_prodring_alloc(struct tg3 *tp,
  5197. struct tg3_rx_prodring_set *tpr)
  5198. {
  5199. u32 i, rx_pkt_dma_sz;
  5200. tpr->rx_std_cons_idx = 0;
  5201. tpr->rx_std_prod_idx = 0;
  5202. tpr->rx_jmb_cons_idx = 0;
  5203. tpr->rx_jmb_prod_idx = 0;
  5204. if (tpr != &tp->napi[0].prodring) {
  5205. memset(&tpr->rx_std_buffers[0], 0,
  5206. TG3_RX_STD_BUFF_RING_SIZE(tp));
  5207. if (tpr->rx_jmb_buffers)
  5208. memset(&tpr->rx_jmb_buffers[0], 0,
  5209. TG3_RX_JMB_BUFF_RING_SIZE(tp));
  5210. goto done;
  5211. }
  5212. /* Zero out all descriptors. */
  5213. memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
  5214. rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
  5215. if (tg3_flag(tp, 5780_CLASS) &&
  5216. tp->dev->mtu > ETH_DATA_LEN)
  5217. rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
  5218. tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
  5219. /* Initialize invariants of the rings, we only set this
  5220. * stuff once. This works because the card does not
  5221. * write into the rx buffer posting rings.
  5222. */
  5223. for (i = 0; i <= tp->rx_std_ring_mask; i++) {
  5224. struct tg3_rx_buffer_desc *rxd;
  5225. rxd = &tpr->rx_std[i];
  5226. rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
  5227. rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
  5228. rxd->opaque = (RXD_OPAQUE_RING_STD |
  5229. (i << RXD_OPAQUE_INDEX_SHIFT));
  5230. }
  5231. /* Now allocate fresh SKBs for each rx ring. */
  5232. for (i = 0; i < tp->rx_pending; i++) {
  5233. if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_STD, i) < 0) {
  5234. netdev_warn(tp->dev,
  5235. "Using a smaller RX standard ring. Only "
  5236. "%d out of %d buffers were allocated "
  5237. "successfully\n", i, tp->rx_pending);
  5238. if (i == 0)
  5239. goto initfail;
  5240. tp->rx_pending = i;
  5241. break;
  5242. }
  5243. }
  5244. if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
  5245. goto done;
  5246. memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
  5247. if (!tg3_flag(tp, JUMBO_RING_ENABLE))
  5248. goto done;
  5249. for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
  5250. struct tg3_rx_buffer_desc *rxd;
  5251. rxd = &tpr->rx_jmb[i].std;
  5252. rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
  5253. rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
  5254. RXD_FLAG_JUMBO;
  5255. rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
  5256. (i << RXD_OPAQUE_INDEX_SHIFT));
  5257. }
  5258. for (i = 0; i < tp->rx_jumbo_pending; i++) {
  5259. if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_JUMBO, i) < 0) {
  5260. netdev_warn(tp->dev,
  5261. "Using a smaller RX jumbo ring. Only %d "
  5262. "out of %d buffers were allocated "
  5263. "successfully\n", i, tp->rx_jumbo_pending);
  5264. if (i == 0)
  5265. goto initfail;
  5266. tp->rx_jumbo_pending = i;
  5267. break;
  5268. }
  5269. }
  5270. done:
  5271. return 0;
  5272. initfail:
  5273. tg3_rx_prodring_free(tp, tpr);
  5274. return -ENOMEM;
  5275. }
  5276. static void tg3_rx_prodring_fini(struct tg3 *tp,
  5277. struct tg3_rx_prodring_set *tpr)
  5278. {
  5279. kfree(tpr->rx_std_buffers);
  5280. tpr->rx_std_buffers = NULL;
  5281. kfree(tpr->rx_jmb_buffers);
  5282. tpr->rx_jmb_buffers = NULL;
  5283. if (tpr->rx_std) {
  5284. dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
  5285. tpr->rx_std, tpr->rx_std_mapping);
  5286. tpr->rx_std = NULL;
  5287. }
  5288. if (tpr->rx_jmb) {
  5289. dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
  5290. tpr->rx_jmb, tpr->rx_jmb_mapping);
  5291. tpr->rx_jmb = NULL;
  5292. }
  5293. }
  5294. static int tg3_rx_prodring_init(struct tg3 *tp,
  5295. struct tg3_rx_prodring_set *tpr)
  5296. {
  5297. tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
  5298. GFP_KERNEL);
  5299. if (!tpr->rx_std_buffers)
  5300. return -ENOMEM;
  5301. tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
  5302. TG3_RX_STD_RING_BYTES(tp),
  5303. &tpr->rx_std_mapping,
  5304. GFP_KERNEL);
  5305. if (!tpr->rx_std)
  5306. goto err_out;
  5307. if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
  5308. tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
  5309. GFP_KERNEL);
  5310. if (!tpr->rx_jmb_buffers)
  5311. goto err_out;
  5312. tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
  5313. TG3_RX_JMB_RING_BYTES(tp),
  5314. &tpr->rx_jmb_mapping,
  5315. GFP_KERNEL);
  5316. if (!tpr->rx_jmb)
  5317. goto err_out;
  5318. }
  5319. return 0;
  5320. err_out:
  5321. tg3_rx_prodring_fini(tp, tpr);
  5322. return -ENOMEM;
  5323. }
  5324. /* Free up pending packets in all rx/tx rings.
  5325. *
  5326. * The chip has been shut down and the driver detached from
  5327. * the networking, so no interrupts or new tx packets will
  5328. * end up in the driver. tp->{tx,}lock is not held and we are not
  5329. * in an interrupt context and thus may sleep.
  5330. */
  5331. static void tg3_free_rings(struct tg3 *tp)
  5332. {
  5333. int i, j;
  5334. for (j = 0; j < tp->irq_cnt; j++) {
  5335. struct tg3_napi *tnapi = &tp->napi[j];
  5336. tg3_rx_prodring_free(tp, &tnapi->prodring);
  5337. if (!tnapi->tx_buffers)
  5338. continue;
  5339. for (i = 0; i < TG3_TX_RING_SIZE; ) {
  5340. struct ring_info *txp;
  5341. struct sk_buff *skb;
  5342. unsigned int k;
  5343. txp = &tnapi->tx_buffers[i];
  5344. skb = txp->skb;
  5345. if (skb == NULL) {
  5346. i++;
  5347. continue;
  5348. }
  5349. pci_unmap_single(tp->pdev,
  5350. dma_unmap_addr(txp, mapping),
  5351. skb_headlen(skb),
  5352. PCI_DMA_TODEVICE);
  5353. txp->skb = NULL;
  5354. i++;
  5355. for (k = 0; k < skb_shinfo(skb)->nr_frags; k++) {
  5356. txp = &tnapi->tx_buffers[i & (TG3_TX_RING_SIZE - 1)];
  5357. pci_unmap_page(tp->pdev,
  5358. dma_unmap_addr(txp, mapping),
  5359. skb_shinfo(skb)->frags[k].size,
  5360. PCI_DMA_TODEVICE);
  5361. i++;
  5362. }
  5363. dev_kfree_skb_any(skb);
  5364. }
  5365. }
  5366. }
  5367. /* Initialize tx/rx rings for packet processing.
  5368. *
  5369. * The chip has been shut down and the driver detached from
  5370. * the networking, so no interrupts or new tx packets will
  5371. * end up in the driver. tp->{tx,}lock are held and thus
  5372. * we may not sleep.
  5373. */
  5374. static int tg3_init_rings(struct tg3 *tp)
  5375. {
  5376. int i;
  5377. /* Free up all the SKBs. */
  5378. tg3_free_rings(tp);
  5379. for (i = 0; i < tp->irq_cnt; i++) {
  5380. struct tg3_napi *tnapi = &tp->napi[i];
  5381. tnapi->last_tag = 0;
  5382. tnapi->last_irq_tag = 0;
  5383. tnapi->hw_status->status = 0;
  5384. tnapi->hw_status->status_tag = 0;
  5385. memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
  5386. tnapi->tx_prod = 0;
  5387. tnapi->tx_cons = 0;
  5388. if (tnapi->tx_ring)
  5389. memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
  5390. tnapi->rx_rcb_ptr = 0;
  5391. if (tnapi->rx_rcb)
  5392. memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
  5393. if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
  5394. tg3_free_rings(tp);
  5395. return -ENOMEM;
  5396. }
  5397. }
  5398. return 0;
  5399. }
  5400. /*
  5401. * Must not be invoked with interrupt sources disabled and
  5402. * the hardware shutdown down.
  5403. */
  5404. static void tg3_free_consistent(struct tg3 *tp)
  5405. {
  5406. int i;
  5407. for (i = 0; i < tp->irq_cnt; i++) {
  5408. struct tg3_napi *tnapi = &tp->napi[i];
  5409. if (tnapi->tx_ring) {
  5410. dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
  5411. tnapi->tx_ring, tnapi->tx_desc_mapping);
  5412. tnapi->tx_ring = NULL;
  5413. }
  5414. kfree(tnapi->tx_buffers);
  5415. tnapi->tx_buffers = NULL;
  5416. if (tnapi->rx_rcb) {
  5417. dma_free_coherent(&tp->pdev->dev,
  5418. TG3_RX_RCB_RING_BYTES(tp),
  5419. tnapi->rx_rcb,
  5420. tnapi->rx_rcb_mapping);
  5421. tnapi->rx_rcb = NULL;
  5422. }
  5423. tg3_rx_prodring_fini(tp, &tnapi->prodring);
  5424. if (tnapi->hw_status) {
  5425. dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
  5426. tnapi->hw_status,
  5427. tnapi->status_mapping);
  5428. tnapi->hw_status = NULL;
  5429. }
  5430. }
  5431. if (tp->hw_stats) {
  5432. dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
  5433. tp->hw_stats, tp->stats_mapping);
  5434. tp->hw_stats = NULL;
  5435. }
  5436. }
  5437. /*
  5438. * Must not be invoked with interrupt sources disabled and
  5439. * the hardware shutdown down. Can sleep.
  5440. */
  5441. static int tg3_alloc_consistent(struct tg3 *tp)
  5442. {
  5443. int i;
  5444. tp->hw_stats = dma_alloc_coherent(&tp->pdev->dev,
  5445. sizeof(struct tg3_hw_stats),
  5446. &tp->stats_mapping,
  5447. GFP_KERNEL);
  5448. if (!tp->hw_stats)
  5449. goto err_out;
  5450. memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
  5451. for (i = 0; i < tp->irq_cnt; i++) {
  5452. struct tg3_napi *tnapi = &tp->napi[i];
  5453. struct tg3_hw_status *sblk;
  5454. tnapi->hw_status = dma_alloc_coherent(&tp->pdev->dev,
  5455. TG3_HW_STATUS_SIZE,
  5456. &tnapi->status_mapping,
  5457. GFP_KERNEL);
  5458. if (!tnapi->hw_status)
  5459. goto err_out;
  5460. memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
  5461. sblk = tnapi->hw_status;
  5462. if (tg3_rx_prodring_init(tp, &tnapi->prodring))
  5463. goto err_out;
  5464. /* If multivector TSS is enabled, vector 0 does not handle
  5465. * tx interrupts. Don't allocate any resources for it.
  5466. */
  5467. if ((!i && !tg3_flag(tp, ENABLE_TSS)) ||
  5468. (i && tg3_flag(tp, ENABLE_TSS))) {
  5469. tnapi->tx_buffers = kzalloc(sizeof(struct ring_info) *
  5470. TG3_TX_RING_SIZE,
  5471. GFP_KERNEL);
  5472. if (!tnapi->tx_buffers)
  5473. goto err_out;
  5474. tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
  5475. TG3_TX_RING_BYTES,
  5476. &tnapi->tx_desc_mapping,
  5477. GFP_KERNEL);
  5478. if (!tnapi->tx_ring)
  5479. goto err_out;
  5480. }
  5481. /*
  5482. * When RSS is enabled, the status block format changes
  5483. * slightly. The "rx_jumbo_consumer", "reserved",
  5484. * and "rx_mini_consumer" members get mapped to the
  5485. * other three rx return ring producer indexes.
  5486. */
  5487. switch (i) {
  5488. default:
  5489. tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
  5490. break;
  5491. case 2:
  5492. tnapi->rx_rcb_prod_idx = &sblk->rx_jumbo_consumer;
  5493. break;
  5494. case 3:
  5495. tnapi->rx_rcb_prod_idx = &sblk->reserved;
  5496. break;
  5497. case 4:
  5498. tnapi->rx_rcb_prod_idx = &sblk->rx_mini_consumer;
  5499. break;
  5500. }
  5501. /*
  5502. * If multivector RSS is enabled, vector 0 does not handle
  5503. * rx or tx interrupts. Don't allocate any resources for it.
  5504. */
  5505. if (!i && tg3_flag(tp, ENABLE_RSS))
  5506. continue;
  5507. tnapi->rx_rcb = dma_alloc_coherent(&tp->pdev->dev,
  5508. TG3_RX_RCB_RING_BYTES(tp),
  5509. &tnapi->rx_rcb_mapping,
  5510. GFP_KERNEL);
  5511. if (!tnapi->rx_rcb)
  5512. goto err_out;
  5513. memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
  5514. }
  5515. return 0;
  5516. err_out:
  5517. tg3_free_consistent(tp);
  5518. return -ENOMEM;
  5519. }
  5520. #define MAX_WAIT_CNT 1000
  5521. /* To stop a block, clear the enable bit and poll till it
  5522. * clears. tp->lock is held.
  5523. */
  5524. static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
  5525. {
  5526. unsigned int i;
  5527. u32 val;
  5528. if (tg3_flag(tp, 5705_PLUS)) {
  5529. switch (ofs) {
  5530. case RCVLSC_MODE:
  5531. case DMAC_MODE:
  5532. case MBFREE_MODE:
  5533. case BUFMGR_MODE:
  5534. case MEMARB_MODE:
  5535. /* We can't enable/disable these bits of the
  5536. * 5705/5750, just say success.
  5537. */
  5538. return 0;
  5539. default:
  5540. break;
  5541. }
  5542. }
  5543. val = tr32(ofs);
  5544. val &= ~enable_bit;
  5545. tw32_f(ofs, val);
  5546. for (i = 0; i < MAX_WAIT_CNT; i++) {
  5547. udelay(100);
  5548. val = tr32(ofs);
  5549. if ((val & enable_bit) == 0)
  5550. break;
  5551. }
  5552. if (i == MAX_WAIT_CNT && !silent) {
  5553. dev_err(&tp->pdev->dev,
  5554. "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
  5555. ofs, enable_bit);
  5556. return -ENODEV;
  5557. }
  5558. return 0;
  5559. }
  5560. /* tp->lock is held. */
  5561. static int tg3_abort_hw(struct tg3 *tp, int silent)
  5562. {
  5563. int i, err;
  5564. tg3_disable_ints(tp);
  5565. tp->rx_mode &= ~RX_MODE_ENABLE;
  5566. tw32_f(MAC_RX_MODE, tp->rx_mode);
  5567. udelay(10);
  5568. err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
  5569. err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
  5570. err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
  5571. err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
  5572. err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
  5573. err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
  5574. err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
  5575. err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
  5576. err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
  5577. err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
  5578. err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
  5579. err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
  5580. err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
  5581. tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
  5582. tw32_f(MAC_MODE, tp->mac_mode);
  5583. udelay(40);
  5584. tp->tx_mode &= ~TX_MODE_ENABLE;
  5585. tw32_f(MAC_TX_MODE, tp->tx_mode);
  5586. for (i = 0; i < MAX_WAIT_CNT; i++) {
  5587. udelay(100);
  5588. if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
  5589. break;
  5590. }
  5591. if (i >= MAX_WAIT_CNT) {
  5592. dev_err(&tp->pdev->dev,
  5593. "%s timed out, TX_MODE_ENABLE will not clear "
  5594. "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
  5595. err |= -ENODEV;
  5596. }
  5597. err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
  5598. err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
  5599. err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
  5600. tw32(FTQ_RESET, 0xffffffff);
  5601. tw32(FTQ_RESET, 0x00000000);
  5602. err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
  5603. err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
  5604. for (i = 0; i < tp->irq_cnt; i++) {
  5605. struct tg3_napi *tnapi = &tp->napi[i];
  5606. if (tnapi->hw_status)
  5607. memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
  5608. }
  5609. if (tp->hw_stats)
  5610. memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
  5611. return err;
  5612. }
  5613. static void tg3_ape_send_event(struct tg3 *tp, u32 event)
  5614. {
  5615. int i;
  5616. u32 apedata;
  5617. /* NCSI does not support APE events */
  5618. if (tg3_flag(tp, APE_HAS_NCSI))
  5619. return;
  5620. apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
  5621. if (apedata != APE_SEG_SIG_MAGIC)
  5622. return;
  5623. apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
  5624. if (!(apedata & APE_FW_STATUS_READY))
  5625. return;
  5626. /* Wait for up to 1 millisecond for APE to service previous event. */
  5627. for (i = 0; i < 10; i++) {
  5628. if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
  5629. return;
  5630. apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
  5631. if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
  5632. tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
  5633. event | APE_EVENT_STATUS_EVENT_PENDING);
  5634. tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
  5635. if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
  5636. break;
  5637. udelay(100);
  5638. }
  5639. if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
  5640. tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
  5641. }
  5642. static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
  5643. {
  5644. u32 event;
  5645. u32 apedata;
  5646. if (!tg3_flag(tp, ENABLE_APE))
  5647. return;
  5648. switch (kind) {
  5649. case RESET_KIND_INIT:
  5650. tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
  5651. APE_HOST_SEG_SIG_MAGIC);
  5652. tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
  5653. APE_HOST_SEG_LEN_MAGIC);
  5654. apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
  5655. tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
  5656. tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
  5657. APE_HOST_DRIVER_ID_MAGIC(TG3_MAJ_NUM, TG3_MIN_NUM));
  5658. tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
  5659. APE_HOST_BEHAV_NO_PHYLOCK);
  5660. tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE,
  5661. TG3_APE_HOST_DRVR_STATE_START);
  5662. event = APE_EVENT_STATUS_STATE_START;
  5663. break;
  5664. case RESET_KIND_SHUTDOWN:
  5665. /* With the interface we are currently using,
  5666. * APE does not track driver state. Wiping
  5667. * out the HOST SEGMENT SIGNATURE forces
  5668. * the APE to assume OS absent status.
  5669. */
  5670. tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
  5671. if (device_may_wakeup(&tp->pdev->dev) &&
  5672. tg3_flag(tp, WOL_ENABLE)) {
  5673. tg3_ape_write32(tp, TG3_APE_HOST_WOL_SPEED,
  5674. TG3_APE_HOST_WOL_SPEED_AUTO);
  5675. apedata = TG3_APE_HOST_DRVR_STATE_WOL;
  5676. } else
  5677. apedata = TG3_APE_HOST_DRVR_STATE_UNLOAD;
  5678. tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE, apedata);
  5679. event = APE_EVENT_STATUS_STATE_UNLOAD;
  5680. break;
  5681. case RESET_KIND_SUSPEND:
  5682. event = APE_EVENT_STATUS_STATE_SUSPEND;
  5683. break;
  5684. default:
  5685. return;
  5686. }
  5687. event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
  5688. tg3_ape_send_event(tp, event);
  5689. }
  5690. /* tp->lock is held. */
  5691. static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
  5692. {
  5693. tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
  5694. NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
  5695. if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
  5696. switch (kind) {
  5697. case RESET_KIND_INIT:
  5698. tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
  5699. DRV_STATE_START);
  5700. break;
  5701. case RESET_KIND_SHUTDOWN:
  5702. tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
  5703. DRV_STATE_UNLOAD);
  5704. break;
  5705. case RESET_KIND_SUSPEND:
  5706. tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
  5707. DRV_STATE_SUSPEND);
  5708. break;
  5709. default:
  5710. break;
  5711. }
  5712. }
  5713. if (kind == RESET_KIND_INIT ||
  5714. kind == RESET_KIND_SUSPEND)
  5715. tg3_ape_driver_state_change(tp, kind);
  5716. }
  5717. /* tp->lock is held. */
  5718. static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
  5719. {
  5720. if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
  5721. switch (kind) {
  5722. case RESET_KIND_INIT:
  5723. tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
  5724. DRV_STATE_START_DONE);
  5725. break;
  5726. case RESET_KIND_SHUTDOWN:
  5727. tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
  5728. DRV_STATE_UNLOAD_DONE);
  5729. break;
  5730. default:
  5731. break;
  5732. }
  5733. }
  5734. if (kind == RESET_KIND_SHUTDOWN)
  5735. tg3_ape_driver_state_change(tp, kind);
  5736. }
  5737. /* tp->lock is held. */
  5738. static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
  5739. {
  5740. if (tg3_flag(tp, ENABLE_ASF)) {
  5741. switch (kind) {
  5742. case RESET_KIND_INIT:
  5743. tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
  5744. DRV_STATE_START);
  5745. break;
  5746. case RESET_KIND_SHUTDOWN:
  5747. tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
  5748. DRV_STATE_UNLOAD);
  5749. break;
  5750. case RESET_KIND_SUSPEND:
  5751. tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
  5752. DRV_STATE_SUSPEND);
  5753. break;
  5754. default:
  5755. break;
  5756. }
  5757. }
  5758. }
  5759. static int tg3_poll_fw(struct tg3 *tp)
  5760. {
  5761. int i;
  5762. u32 val;
  5763. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
  5764. /* Wait up to 20ms for init done. */
  5765. for (i = 0; i < 200; i++) {
  5766. if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
  5767. return 0;
  5768. udelay(100);
  5769. }
  5770. return -ENODEV;
  5771. }
  5772. /* Wait for firmware initialization to complete. */
  5773. for (i = 0; i < 100000; i++) {
  5774. tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
  5775. if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
  5776. break;
  5777. udelay(10);
  5778. }
  5779. /* Chip might not be fitted with firmware. Some Sun onboard
  5780. * parts are configured like that. So don't signal the timeout
  5781. * of the above loop as an error, but do report the lack of
  5782. * running firmware once.
  5783. */
  5784. if (i >= 100000 && !tg3_flag(tp, NO_FWARE_REPORTED)) {
  5785. tg3_flag_set(tp, NO_FWARE_REPORTED);
  5786. netdev_info(tp->dev, "No firmware running\n");
  5787. }
  5788. if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
  5789. /* The 57765 A0 needs a little more
  5790. * time to do some important work.
  5791. */
  5792. mdelay(10);
  5793. }
  5794. return 0;
  5795. }
  5796. /* Save PCI command register before chip reset */
  5797. static void tg3_save_pci_state(struct tg3 *tp)
  5798. {
  5799. pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
  5800. }
  5801. /* Restore PCI state after chip reset */
  5802. static void tg3_restore_pci_state(struct tg3 *tp)
  5803. {
  5804. u32 val;
  5805. /* Re-enable indirect register accesses. */
  5806. pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
  5807. tp->misc_host_ctrl);
  5808. /* Set MAX PCI retry to zero. */
  5809. val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
  5810. if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
  5811. tg3_flag(tp, PCIX_MODE))
  5812. val |= PCISTATE_RETRY_SAME_DMA;
  5813. /* Allow reads and writes to the APE register and memory space. */
  5814. if (tg3_flag(tp, ENABLE_APE))
  5815. val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
  5816. PCISTATE_ALLOW_APE_SHMEM_WR |
  5817. PCISTATE_ALLOW_APE_PSPACE_WR;
  5818. pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
  5819. pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
  5820. if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785) {
  5821. if (tg3_flag(tp, PCI_EXPRESS))
  5822. pcie_set_readrq(tp->pdev, tp->pcie_readrq);
  5823. else {
  5824. pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
  5825. tp->pci_cacheline_sz);
  5826. pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
  5827. tp->pci_lat_timer);
  5828. }
  5829. }
  5830. /* Make sure PCI-X relaxed ordering bit is clear. */
  5831. if (tg3_flag(tp, PCIX_MODE)) {
  5832. u16 pcix_cmd;
  5833. pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
  5834. &pcix_cmd);
  5835. pcix_cmd &= ~PCI_X_CMD_ERO;
  5836. pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
  5837. pcix_cmd);
  5838. }
  5839. if (tg3_flag(tp, 5780_CLASS)) {
  5840. /* Chip reset on 5780 will reset MSI enable bit,
  5841. * so need to restore it.
  5842. */
  5843. if (tg3_flag(tp, USING_MSI)) {
  5844. u16 ctrl;
  5845. pci_read_config_word(tp->pdev,
  5846. tp->msi_cap + PCI_MSI_FLAGS,
  5847. &ctrl);
  5848. pci_write_config_word(tp->pdev,
  5849. tp->msi_cap + PCI_MSI_FLAGS,
  5850. ctrl | PCI_MSI_FLAGS_ENABLE);
  5851. val = tr32(MSGINT_MODE);
  5852. tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
  5853. }
  5854. }
  5855. }
  5856. static void tg3_stop_fw(struct tg3 *);
  5857. /* tp->lock is held. */
  5858. static int tg3_chip_reset(struct tg3 *tp)
  5859. {
  5860. u32 val;
  5861. void (*write_op)(struct tg3 *, u32, u32);
  5862. int i, err;
  5863. tg3_nvram_lock(tp);
  5864. tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
  5865. /* No matching tg3_nvram_unlock() after this because
  5866. * chip reset below will undo the nvram lock.
  5867. */
  5868. tp->nvram_lock_cnt = 0;
  5869. /* GRC_MISC_CFG core clock reset will clear the memory
  5870. * enable bit in PCI register 4 and the MSI enable bit
  5871. * on some chips, so we save relevant registers here.
  5872. */
  5873. tg3_save_pci_state(tp);
  5874. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
  5875. tg3_flag(tp, 5755_PLUS))
  5876. tw32(GRC_FASTBOOT_PC, 0);
  5877. /*
  5878. * We must avoid the readl() that normally takes place.
  5879. * It locks machines, causes machine checks, and other
  5880. * fun things. So, temporarily disable the 5701
  5881. * hardware workaround, while we do the reset.
  5882. */
  5883. write_op = tp->write32;
  5884. if (write_op == tg3_write_flush_reg32)
  5885. tp->write32 = tg3_write32;
  5886. /* Prevent the irq handler from reading or writing PCI registers
  5887. * during chip reset when the memory enable bit in the PCI command
  5888. * register may be cleared. The chip does not generate interrupt
  5889. * at this time, but the irq handler may still be called due to irq
  5890. * sharing or irqpoll.
  5891. */
  5892. tg3_flag_set(tp, CHIP_RESETTING);
  5893. for (i = 0; i < tp->irq_cnt; i++) {
  5894. struct tg3_napi *tnapi = &tp->napi[i];
  5895. if (tnapi->hw_status) {
  5896. tnapi->hw_status->status = 0;
  5897. tnapi->hw_status->status_tag = 0;
  5898. }
  5899. tnapi->last_tag = 0;
  5900. tnapi->last_irq_tag = 0;
  5901. }
  5902. smp_mb();
  5903. for (i = 0; i < tp->irq_cnt; i++)
  5904. synchronize_irq(tp->napi[i].irq_vec);
  5905. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
  5906. val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
  5907. tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
  5908. }
  5909. /* do the reset */
  5910. val = GRC_MISC_CFG_CORECLK_RESET;
  5911. if (tg3_flag(tp, PCI_EXPRESS)) {
  5912. /* Force PCIe 1.0a mode */
  5913. if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
  5914. !tg3_flag(tp, 57765_PLUS) &&
  5915. tr32(TG3_PCIE_PHY_TSTCTL) ==
  5916. (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
  5917. tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
  5918. if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
  5919. tw32(GRC_MISC_CFG, (1 << 29));
  5920. val |= (1 << 29);
  5921. }
  5922. }
  5923. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
  5924. tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
  5925. tw32(GRC_VCPU_EXT_CTRL,
  5926. tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
  5927. }
  5928. /* Manage gphy power for all CPMU absent PCIe devices. */
  5929. if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, CPMU_PRESENT))
  5930. val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
  5931. tw32(GRC_MISC_CFG, val);
  5932. /* restore 5701 hardware bug workaround write method */
  5933. tp->write32 = write_op;
  5934. /* Unfortunately, we have to delay before the PCI read back.
  5935. * Some 575X chips even will not respond to a PCI cfg access
  5936. * when the reset command is given to the chip.
  5937. *
  5938. * How do these hardware designers expect things to work
  5939. * properly if the PCI write is posted for a long period
  5940. * of time? It is always necessary to have some method by
  5941. * which a register read back can occur to push the write
  5942. * out which does the reset.
  5943. *
  5944. * For most tg3 variants the trick below was working.
  5945. * Ho hum...
  5946. */
  5947. udelay(120);
  5948. /* Flush PCI posted writes. The normal MMIO registers
  5949. * are inaccessible at this time so this is the only
  5950. * way to make this reliably (actually, this is no longer
  5951. * the case, see above). I tried to use indirect
  5952. * register read/write but this upset some 5701 variants.
  5953. */
  5954. pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
  5955. udelay(120);
  5956. if (tg3_flag(tp, PCI_EXPRESS) && tp->pcie_cap) {
  5957. u16 val16;
  5958. if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
  5959. int i;
  5960. u32 cfg_val;
  5961. /* Wait for link training to complete. */
  5962. for (i = 0; i < 5000; i++)
  5963. udelay(100);
  5964. pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
  5965. pci_write_config_dword(tp->pdev, 0xc4,
  5966. cfg_val | (1 << 15));
  5967. }
  5968. /* Clear the "no snoop" and "relaxed ordering" bits. */
  5969. pci_read_config_word(tp->pdev,
  5970. tp->pcie_cap + PCI_EXP_DEVCTL,
  5971. &val16);
  5972. val16 &= ~(PCI_EXP_DEVCTL_RELAX_EN |
  5973. PCI_EXP_DEVCTL_NOSNOOP_EN);
  5974. /*
  5975. * Older PCIe devices only support the 128 byte
  5976. * MPS setting. Enforce the restriction.
  5977. */
  5978. if (!tg3_flag(tp, CPMU_PRESENT))
  5979. val16 &= ~PCI_EXP_DEVCTL_PAYLOAD;
  5980. pci_write_config_word(tp->pdev,
  5981. tp->pcie_cap + PCI_EXP_DEVCTL,
  5982. val16);
  5983. pcie_set_readrq(tp->pdev, tp->pcie_readrq);
  5984. /* Clear error status */
  5985. pci_write_config_word(tp->pdev,
  5986. tp->pcie_cap + PCI_EXP_DEVSTA,
  5987. PCI_EXP_DEVSTA_CED |
  5988. PCI_EXP_DEVSTA_NFED |
  5989. PCI_EXP_DEVSTA_FED |
  5990. PCI_EXP_DEVSTA_URD);
  5991. }
  5992. tg3_restore_pci_state(tp);
  5993. tg3_flag_clear(tp, CHIP_RESETTING);
  5994. tg3_flag_clear(tp, ERROR_PROCESSED);
  5995. val = 0;
  5996. if (tg3_flag(tp, 5780_CLASS))
  5997. val = tr32(MEMARB_MODE);
  5998. tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
  5999. if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
  6000. tg3_stop_fw(tp);
  6001. tw32(0x5000, 0x400);
  6002. }
  6003. tw32(GRC_MODE, tp->grc_mode);
  6004. if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
  6005. val = tr32(0xc4);
  6006. tw32(0xc4, val | (1 << 15));
  6007. }
  6008. if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
  6009. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
  6010. tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
  6011. if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
  6012. tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
  6013. tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
  6014. }
  6015. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
  6016. tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
  6017. val = tp->mac_mode;
  6018. } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
  6019. tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
  6020. val = tp->mac_mode;
  6021. } else
  6022. val = 0;
  6023. tw32_f(MAC_MODE, val);
  6024. udelay(40);
  6025. tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
  6026. err = tg3_poll_fw(tp);
  6027. if (err)
  6028. return err;
  6029. tg3_mdio_start(tp);
  6030. if (tg3_flag(tp, PCI_EXPRESS) &&
  6031. tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
  6032. GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
  6033. !tg3_flag(tp, 57765_PLUS)) {
  6034. val = tr32(0x7c00);
  6035. tw32(0x7c00, val | (1 << 25));
  6036. }
  6037. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
  6038. val = tr32(TG3_CPMU_CLCK_ORIDE);
  6039. tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
  6040. }
  6041. /* Reprobe ASF enable state. */
  6042. tg3_flag_clear(tp, ENABLE_ASF);
  6043. tg3_flag_clear(tp, ASF_NEW_HANDSHAKE);
  6044. tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
  6045. if (val == NIC_SRAM_DATA_SIG_MAGIC) {
  6046. u32 nic_cfg;
  6047. tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
  6048. if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
  6049. tg3_flag_set(tp, ENABLE_ASF);
  6050. tp->last_event_jiffies = jiffies;
  6051. if (tg3_flag(tp, 5750_PLUS))
  6052. tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
  6053. }
  6054. }
  6055. return 0;
  6056. }
  6057. /* tp->lock is held. */
  6058. static void tg3_stop_fw(struct tg3 *tp)
  6059. {
  6060. if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
  6061. /* Wait for RX cpu to ACK the previous event. */
  6062. tg3_wait_for_event_ack(tp);
  6063. tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
  6064. tg3_generate_fw_event(tp);
  6065. /* Wait for RX cpu to ACK this event. */
  6066. tg3_wait_for_event_ack(tp);
  6067. }
  6068. }
  6069. /* tp->lock is held. */
  6070. static int tg3_halt(struct tg3 *tp, int kind, int silent)
  6071. {
  6072. int err;
  6073. tg3_stop_fw(tp);
  6074. tg3_write_sig_pre_reset(tp, kind);
  6075. tg3_abort_hw(tp, silent);
  6076. err = tg3_chip_reset(tp);
  6077. __tg3_set_mac_addr(tp, 0);
  6078. tg3_write_sig_legacy(tp, kind);
  6079. tg3_write_sig_post_reset(tp, kind);
  6080. if (err)
  6081. return err;
  6082. return 0;
  6083. }
  6084. #define RX_CPU_SCRATCH_BASE 0x30000
  6085. #define RX_CPU_SCRATCH_SIZE 0x04000
  6086. #define TX_CPU_SCRATCH_BASE 0x34000
  6087. #define TX_CPU_SCRATCH_SIZE 0x04000
  6088. /* tp->lock is held. */
  6089. static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
  6090. {
  6091. int i;
  6092. BUG_ON(offset == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS));
  6093. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
  6094. u32 val = tr32(GRC_VCPU_EXT_CTRL);
  6095. tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
  6096. return 0;
  6097. }
  6098. if (offset == RX_CPU_BASE) {
  6099. for (i = 0; i < 10000; i++) {
  6100. tw32(offset + CPU_STATE, 0xffffffff);
  6101. tw32(offset + CPU_MODE, CPU_MODE_HALT);
  6102. if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
  6103. break;
  6104. }
  6105. tw32(offset + CPU_STATE, 0xffffffff);
  6106. tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
  6107. udelay(10);
  6108. } else {
  6109. for (i = 0; i < 10000; i++) {
  6110. tw32(offset + CPU_STATE, 0xffffffff);
  6111. tw32(offset + CPU_MODE, CPU_MODE_HALT);
  6112. if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
  6113. break;
  6114. }
  6115. }
  6116. if (i >= 10000) {
  6117. netdev_err(tp->dev, "%s timed out, %s CPU\n",
  6118. __func__, offset == RX_CPU_BASE ? "RX" : "TX");
  6119. return -ENODEV;
  6120. }
  6121. /* Clear firmware's nvram arbitration. */
  6122. if (tg3_flag(tp, NVRAM))
  6123. tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
  6124. return 0;
  6125. }
  6126. struct fw_info {
  6127. unsigned int fw_base;
  6128. unsigned int fw_len;
  6129. const __be32 *fw_data;
  6130. };
  6131. /* tp->lock is held. */
  6132. static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base, u32 cpu_scratch_base,
  6133. int cpu_scratch_size, struct fw_info *info)
  6134. {
  6135. int err, lock_err, i;
  6136. void (*write_op)(struct tg3 *, u32, u32);
  6137. if (cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS)) {
  6138. netdev_err(tp->dev,
  6139. "%s: Trying to load TX cpu firmware which is 5705\n",
  6140. __func__);
  6141. return -EINVAL;
  6142. }
  6143. if (tg3_flag(tp, 5705_PLUS))
  6144. write_op = tg3_write_mem;
  6145. else
  6146. write_op = tg3_write_indirect_reg32;
  6147. /* It is possible that bootcode is still loading at this point.
  6148. * Get the nvram lock first before halting the cpu.
  6149. */
  6150. lock_err = tg3_nvram_lock(tp);
  6151. err = tg3_halt_cpu(tp, cpu_base);
  6152. if (!lock_err)
  6153. tg3_nvram_unlock(tp);
  6154. if (err)
  6155. goto out;
  6156. for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
  6157. write_op(tp, cpu_scratch_base + i, 0);
  6158. tw32(cpu_base + CPU_STATE, 0xffffffff);
  6159. tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
  6160. for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
  6161. write_op(tp, (cpu_scratch_base +
  6162. (info->fw_base & 0xffff) +
  6163. (i * sizeof(u32))),
  6164. be32_to_cpu(info->fw_data[i]));
  6165. err = 0;
  6166. out:
  6167. return err;
  6168. }
  6169. /* tp->lock is held. */
  6170. static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
  6171. {
  6172. struct fw_info info;
  6173. const __be32 *fw_data;
  6174. int err, i;
  6175. fw_data = (void *)tp->fw->data;
  6176. /* Firmware blob starts with version numbers, followed by
  6177. start address and length. We are setting complete length.
  6178. length = end_address_of_bss - start_address_of_text.
  6179. Remainder is the blob to be loaded contiguously
  6180. from start address. */
  6181. info.fw_base = be32_to_cpu(fw_data[1]);
  6182. info.fw_len = tp->fw->size - 12;
  6183. info.fw_data = &fw_data[3];
  6184. err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
  6185. RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
  6186. &info);
  6187. if (err)
  6188. return err;
  6189. err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
  6190. TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
  6191. &info);
  6192. if (err)
  6193. return err;
  6194. /* Now startup only the RX cpu. */
  6195. tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
  6196. tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
  6197. for (i = 0; i < 5; i++) {
  6198. if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
  6199. break;
  6200. tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
  6201. tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
  6202. tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
  6203. udelay(1000);
  6204. }
  6205. if (i >= 5) {
  6206. netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
  6207. "should be %08x\n", __func__,
  6208. tr32(RX_CPU_BASE + CPU_PC), info.fw_base);
  6209. return -ENODEV;
  6210. }
  6211. tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
  6212. tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
  6213. return 0;
  6214. }
  6215. /* tp->lock is held. */
  6216. static int tg3_load_tso_firmware(struct tg3 *tp)
  6217. {
  6218. struct fw_info info;
  6219. const __be32 *fw_data;
  6220. unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
  6221. int err, i;
  6222. if (tg3_flag(tp, HW_TSO_1) ||
  6223. tg3_flag(tp, HW_TSO_2) ||
  6224. tg3_flag(tp, HW_TSO_3))
  6225. return 0;
  6226. fw_data = (void *)tp->fw->data;
  6227. /* Firmware blob starts with version numbers, followed by
  6228. start address and length. We are setting complete length.
  6229. length = end_address_of_bss - start_address_of_text.
  6230. Remainder is the blob to be loaded contiguously
  6231. from start address. */
  6232. info.fw_base = be32_to_cpu(fw_data[1]);
  6233. cpu_scratch_size = tp->fw_len;
  6234. info.fw_len = tp->fw->size - 12;
  6235. info.fw_data = &fw_data[3];
  6236. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
  6237. cpu_base = RX_CPU_BASE;
  6238. cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
  6239. } else {
  6240. cpu_base = TX_CPU_BASE;
  6241. cpu_scratch_base = TX_CPU_SCRATCH_BASE;
  6242. cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
  6243. }
  6244. err = tg3_load_firmware_cpu(tp, cpu_base,
  6245. cpu_scratch_base, cpu_scratch_size,
  6246. &info);
  6247. if (err)
  6248. return err;
  6249. /* Now startup the cpu. */
  6250. tw32(cpu_base + CPU_STATE, 0xffffffff);
  6251. tw32_f(cpu_base + CPU_PC, info.fw_base);
  6252. for (i = 0; i < 5; i++) {
  6253. if (tr32(cpu_base + CPU_PC) == info.fw_base)
  6254. break;
  6255. tw32(cpu_base + CPU_STATE, 0xffffffff);
  6256. tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
  6257. tw32_f(cpu_base + CPU_PC, info.fw_base);
  6258. udelay(1000);
  6259. }
  6260. if (i >= 5) {
  6261. netdev_err(tp->dev,
  6262. "%s fails to set CPU PC, is %08x should be %08x\n",
  6263. __func__, tr32(cpu_base + CPU_PC), info.fw_base);
  6264. return -ENODEV;
  6265. }
  6266. tw32(cpu_base + CPU_STATE, 0xffffffff);
  6267. tw32_f(cpu_base + CPU_MODE, 0x00000000);
  6268. return 0;
  6269. }
  6270. static int tg3_set_mac_addr(struct net_device *dev, void *p)
  6271. {
  6272. struct tg3 *tp = netdev_priv(dev);
  6273. struct sockaddr *addr = p;
  6274. int err = 0, skip_mac_1 = 0;
  6275. if (!is_valid_ether_addr(addr->sa_data))
  6276. return -EINVAL;
  6277. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  6278. if (!netif_running(dev))
  6279. return 0;
  6280. if (tg3_flag(tp, ENABLE_ASF)) {
  6281. u32 addr0_high, addr0_low, addr1_high, addr1_low;
  6282. addr0_high = tr32(MAC_ADDR_0_HIGH);
  6283. addr0_low = tr32(MAC_ADDR_0_LOW);
  6284. addr1_high = tr32(MAC_ADDR_1_HIGH);
  6285. addr1_low = tr32(MAC_ADDR_1_LOW);
  6286. /* Skip MAC addr 1 if ASF is using it. */
  6287. if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
  6288. !(addr1_high == 0 && addr1_low == 0))
  6289. skip_mac_1 = 1;
  6290. }
  6291. spin_lock_bh(&tp->lock);
  6292. __tg3_set_mac_addr(tp, skip_mac_1);
  6293. spin_unlock_bh(&tp->lock);
  6294. return err;
  6295. }
  6296. /* tp->lock is held. */
  6297. static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
  6298. dma_addr_t mapping, u32 maxlen_flags,
  6299. u32 nic_addr)
  6300. {
  6301. tg3_write_mem(tp,
  6302. (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
  6303. ((u64) mapping >> 32));
  6304. tg3_write_mem(tp,
  6305. (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
  6306. ((u64) mapping & 0xffffffff));
  6307. tg3_write_mem(tp,
  6308. (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
  6309. maxlen_flags);
  6310. if (!tg3_flag(tp, 5705_PLUS))
  6311. tg3_write_mem(tp,
  6312. (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
  6313. nic_addr);
  6314. }
  6315. static void __tg3_set_rx_mode(struct net_device *);
  6316. static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
  6317. {
  6318. int i;
  6319. if (!tg3_flag(tp, ENABLE_TSS)) {
  6320. tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
  6321. tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
  6322. tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
  6323. } else {
  6324. tw32(HOSTCC_TXCOL_TICKS, 0);
  6325. tw32(HOSTCC_TXMAX_FRAMES, 0);
  6326. tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
  6327. }
  6328. if (!tg3_flag(tp, ENABLE_RSS)) {
  6329. tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
  6330. tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
  6331. tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
  6332. } else {
  6333. tw32(HOSTCC_RXCOL_TICKS, 0);
  6334. tw32(HOSTCC_RXMAX_FRAMES, 0);
  6335. tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
  6336. }
  6337. if (!tg3_flag(tp, 5705_PLUS)) {
  6338. u32 val = ec->stats_block_coalesce_usecs;
  6339. tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
  6340. tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
  6341. if (!netif_carrier_ok(tp->dev))
  6342. val = 0;
  6343. tw32(HOSTCC_STAT_COAL_TICKS, val);
  6344. }
  6345. for (i = 0; i < tp->irq_cnt - 1; i++) {
  6346. u32 reg;
  6347. reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
  6348. tw32(reg, ec->rx_coalesce_usecs);
  6349. reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
  6350. tw32(reg, ec->rx_max_coalesced_frames);
  6351. reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
  6352. tw32(reg, ec->rx_max_coalesced_frames_irq);
  6353. if (tg3_flag(tp, ENABLE_TSS)) {
  6354. reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
  6355. tw32(reg, ec->tx_coalesce_usecs);
  6356. reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
  6357. tw32(reg, ec->tx_max_coalesced_frames);
  6358. reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
  6359. tw32(reg, ec->tx_max_coalesced_frames_irq);
  6360. }
  6361. }
  6362. for (; i < tp->irq_max - 1; i++) {
  6363. tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
  6364. tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
  6365. tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
  6366. if (tg3_flag(tp, ENABLE_TSS)) {
  6367. tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
  6368. tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
  6369. tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
  6370. }
  6371. }
  6372. }
  6373. /* tp->lock is held. */
  6374. static void tg3_rings_reset(struct tg3 *tp)
  6375. {
  6376. int i;
  6377. u32 stblk, txrcb, rxrcb, limit;
  6378. struct tg3_napi *tnapi = &tp->napi[0];
  6379. /* Disable all transmit rings but the first. */
  6380. if (!tg3_flag(tp, 5705_PLUS))
  6381. limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
  6382. else if (tg3_flag(tp, 5717_PLUS))
  6383. limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
  6384. else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
  6385. limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
  6386. else
  6387. limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
  6388. for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
  6389. txrcb < limit; txrcb += TG3_BDINFO_SIZE)
  6390. tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
  6391. BDINFO_FLAGS_DISABLED);
  6392. /* Disable all receive return rings but the first. */
  6393. if (tg3_flag(tp, 5717_PLUS))
  6394. limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
  6395. else if (!tg3_flag(tp, 5705_PLUS))
  6396. limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
  6397. else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
  6398. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
  6399. limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
  6400. else
  6401. limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
  6402. for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
  6403. rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
  6404. tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
  6405. BDINFO_FLAGS_DISABLED);
  6406. /* Disable interrupts */
  6407. tw32_mailbox_f(tp->napi[0].int_mbox, 1);
  6408. /* Zero mailbox registers. */
  6409. if (tg3_flag(tp, SUPPORT_MSIX)) {
  6410. for (i = 1; i < tp->irq_max; i++) {
  6411. tp->napi[i].tx_prod = 0;
  6412. tp->napi[i].tx_cons = 0;
  6413. if (tg3_flag(tp, ENABLE_TSS))
  6414. tw32_mailbox(tp->napi[i].prodmbox, 0);
  6415. tw32_rx_mbox(tp->napi[i].consmbox, 0);
  6416. tw32_mailbox_f(tp->napi[i].int_mbox, 1);
  6417. }
  6418. if (!tg3_flag(tp, ENABLE_TSS))
  6419. tw32_mailbox(tp->napi[0].prodmbox, 0);
  6420. } else {
  6421. tp->napi[0].tx_prod = 0;
  6422. tp->napi[0].tx_cons = 0;
  6423. tw32_mailbox(tp->napi[0].prodmbox, 0);
  6424. tw32_rx_mbox(tp->napi[0].consmbox, 0);
  6425. }
  6426. /* Make sure the NIC-based send BD rings are disabled. */
  6427. if (!tg3_flag(tp, 5705_PLUS)) {
  6428. u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
  6429. for (i = 0; i < 16; i++)
  6430. tw32_tx_mbox(mbox + i * 8, 0);
  6431. }
  6432. txrcb = NIC_SRAM_SEND_RCB;
  6433. rxrcb = NIC_SRAM_RCV_RET_RCB;
  6434. /* Clear status block in ram. */
  6435. memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
  6436. /* Set status block DMA address */
  6437. tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
  6438. ((u64) tnapi->status_mapping >> 32));
  6439. tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
  6440. ((u64) tnapi->status_mapping & 0xffffffff));
  6441. if (tnapi->tx_ring) {
  6442. tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
  6443. (TG3_TX_RING_SIZE <<
  6444. BDINFO_FLAGS_MAXLEN_SHIFT),
  6445. NIC_SRAM_TX_BUFFER_DESC);
  6446. txrcb += TG3_BDINFO_SIZE;
  6447. }
  6448. if (tnapi->rx_rcb) {
  6449. tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
  6450. (tp->rx_ret_ring_mask + 1) <<
  6451. BDINFO_FLAGS_MAXLEN_SHIFT, 0);
  6452. rxrcb += TG3_BDINFO_SIZE;
  6453. }
  6454. stblk = HOSTCC_STATBLCK_RING1;
  6455. for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
  6456. u64 mapping = (u64)tnapi->status_mapping;
  6457. tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
  6458. tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
  6459. /* Clear status block in ram. */
  6460. memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
  6461. if (tnapi->tx_ring) {
  6462. tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
  6463. (TG3_TX_RING_SIZE <<
  6464. BDINFO_FLAGS_MAXLEN_SHIFT),
  6465. NIC_SRAM_TX_BUFFER_DESC);
  6466. txrcb += TG3_BDINFO_SIZE;
  6467. }
  6468. tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
  6469. ((tp->rx_ret_ring_mask + 1) <<
  6470. BDINFO_FLAGS_MAXLEN_SHIFT), 0);
  6471. stblk += 8;
  6472. rxrcb += TG3_BDINFO_SIZE;
  6473. }
  6474. }
  6475. static void tg3_setup_rxbd_thresholds(struct tg3 *tp)
  6476. {
  6477. u32 val, bdcache_maxcnt, host_rep_thresh, nic_rep_thresh;
  6478. if (!tg3_flag(tp, 5750_PLUS) ||
  6479. tg3_flag(tp, 5780_CLASS) ||
  6480. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
  6481. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
  6482. bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5700;
  6483. else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
  6484. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787)
  6485. bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5755;
  6486. else
  6487. bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5906;
  6488. nic_rep_thresh = min(bdcache_maxcnt / 2, tp->rx_std_max_post);
  6489. host_rep_thresh = max_t(u32, tp->rx_pending / 8, 1);
  6490. val = min(nic_rep_thresh, host_rep_thresh);
  6491. tw32(RCVBDI_STD_THRESH, val);
  6492. if (tg3_flag(tp, 57765_PLUS))
  6493. tw32(STD_REPLENISH_LWM, bdcache_maxcnt);
  6494. if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
  6495. return;
  6496. if (!tg3_flag(tp, 5705_PLUS))
  6497. bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5700;
  6498. else
  6499. bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5717;
  6500. host_rep_thresh = max_t(u32, tp->rx_jumbo_pending / 8, 1);
  6501. val = min(bdcache_maxcnt / 2, host_rep_thresh);
  6502. tw32(RCVBDI_JUMBO_THRESH, val);
  6503. if (tg3_flag(tp, 57765_PLUS))
  6504. tw32(JMB_REPLENISH_LWM, bdcache_maxcnt);
  6505. }
  6506. /* tp->lock is held. */
  6507. static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
  6508. {
  6509. u32 val, rdmac_mode;
  6510. int i, err, limit;
  6511. struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
  6512. tg3_disable_ints(tp);
  6513. tg3_stop_fw(tp);
  6514. tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
  6515. if (tg3_flag(tp, INIT_COMPLETE))
  6516. tg3_abort_hw(tp, 1);
  6517. /* Enable MAC control of LPI */
  6518. if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
  6519. tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL,
  6520. TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
  6521. TG3_CPMU_EEE_LNKIDL_UART_IDL);
  6522. tw32_f(TG3_CPMU_EEE_CTRL,
  6523. TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
  6524. val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
  6525. TG3_CPMU_EEEMD_LPI_IN_TX |
  6526. TG3_CPMU_EEEMD_LPI_IN_RX |
  6527. TG3_CPMU_EEEMD_EEE_ENABLE;
  6528. if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
  6529. val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
  6530. if (tg3_flag(tp, ENABLE_APE))
  6531. val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
  6532. tw32_f(TG3_CPMU_EEE_MODE, val);
  6533. tw32_f(TG3_CPMU_EEE_DBTMR1,
  6534. TG3_CPMU_DBTMR1_PCIEXIT_2047US |
  6535. TG3_CPMU_DBTMR1_LNKIDLE_2047US);
  6536. tw32_f(TG3_CPMU_EEE_DBTMR2,
  6537. TG3_CPMU_DBTMR2_APE_TX_2047US |
  6538. TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
  6539. }
  6540. if (reset_phy)
  6541. tg3_phy_reset(tp);
  6542. err = tg3_chip_reset(tp);
  6543. if (err)
  6544. return err;
  6545. tg3_write_sig_legacy(tp, RESET_KIND_INIT);
  6546. if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
  6547. val = tr32(TG3_CPMU_CTRL);
  6548. val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
  6549. tw32(TG3_CPMU_CTRL, val);
  6550. val = tr32(TG3_CPMU_LSPD_10MB_CLK);
  6551. val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
  6552. val |= CPMU_LSPD_10MB_MACCLK_6_25;
  6553. tw32(TG3_CPMU_LSPD_10MB_CLK, val);
  6554. val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
  6555. val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
  6556. val |= CPMU_LNK_AWARE_MACCLK_6_25;
  6557. tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
  6558. val = tr32(TG3_CPMU_HST_ACC);
  6559. val &= ~CPMU_HST_ACC_MACCLK_MASK;
  6560. val |= CPMU_HST_ACC_MACCLK_6_25;
  6561. tw32(TG3_CPMU_HST_ACC, val);
  6562. }
  6563. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
  6564. val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
  6565. val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
  6566. PCIE_PWR_MGMT_L1_THRESH_4MS;
  6567. tw32(PCIE_PWR_MGMT_THRESH, val);
  6568. val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
  6569. tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
  6570. tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
  6571. val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
  6572. tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
  6573. }
  6574. if (tg3_flag(tp, L1PLLPD_EN)) {
  6575. u32 grc_mode = tr32(GRC_MODE);
  6576. /* Access the lower 1K of PL PCIE block registers. */
  6577. val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
  6578. tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
  6579. val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
  6580. tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
  6581. val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
  6582. tw32(GRC_MODE, grc_mode);
  6583. }
  6584. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
  6585. if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
  6586. u32 grc_mode = tr32(GRC_MODE);
  6587. /* Access the lower 1K of PL PCIE block registers. */
  6588. val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
  6589. tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
  6590. val = tr32(TG3_PCIE_TLDLPL_PORT +
  6591. TG3_PCIE_PL_LO_PHYCTL5);
  6592. tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
  6593. val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
  6594. tw32(GRC_MODE, grc_mode);
  6595. }
  6596. if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_57765_AX) {
  6597. u32 grc_mode = tr32(GRC_MODE);
  6598. /* Access the lower 1K of DL PCIE block registers. */
  6599. val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
  6600. tw32(GRC_MODE, val | GRC_MODE_PCIE_DL_SEL);
  6601. val = tr32(TG3_PCIE_TLDLPL_PORT +
  6602. TG3_PCIE_DL_LO_FTSMAX);
  6603. val &= ~TG3_PCIE_DL_LO_FTSMAX_MSK;
  6604. tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_DL_LO_FTSMAX,
  6605. val | TG3_PCIE_DL_LO_FTSMAX_VAL);
  6606. tw32(GRC_MODE, grc_mode);
  6607. }
  6608. val = tr32(TG3_CPMU_LSPD_10MB_CLK);
  6609. val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
  6610. val |= CPMU_LSPD_10MB_MACCLK_6_25;
  6611. tw32(TG3_CPMU_LSPD_10MB_CLK, val);
  6612. }
  6613. /* This works around an issue with Athlon chipsets on
  6614. * B3 tigon3 silicon. This bit has no effect on any
  6615. * other revision. But do not set this on PCI Express
  6616. * chips and don't even touch the clocks if the CPMU is present.
  6617. */
  6618. if (!tg3_flag(tp, CPMU_PRESENT)) {
  6619. if (!tg3_flag(tp, PCI_EXPRESS))
  6620. tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
  6621. tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
  6622. }
  6623. if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
  6624. tg3_flag(tp, PCIX_MODE)) {
  6625. val = tr32(TG3PCI_PCISTATE);
  6626. val |= PCISTATE_RETRY_SAME_DMA;
  6627. tw32(TG3PCI_PCISTATE, val);
  6628. }
  6629. if (tg3_flag(tp, ENABLE_APE)) {
  6630. /* Allow reads and writes to the
  6631. * APE register and memory space.
  6632. */
  6633. val = tr32(TG3PCI_PCISTATE);
  6634. val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
  6635. PCISTATE_ALLOW_APE_SHMEM_WR |
  6636. PCISTATE_ALLOW_APE_PSPACE_WR;
  6637. tw32(TG3PCI_PCISTATE, val);
  6638. }
  6639. if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
  6640. /* Enable some hw fixes. */
  6641. val = tr32(TG3PCI_MSI_DATA);
  6642. val |= (1 << 26) | (1 << 28) | (1 << 29);
  6643. tw32(TG3PCI_MSI_DATA, val);
  6644. }
  6645. /* Descriptor ring init may make accesses to the
  6646. * NIC SRAM area to setup the TX descriptors, so we
  6647. * can only do this after the hardware has been
  6648. * successfully reset.
  6649. */
  6650. err = tg3_init_rings(tp);
  6651. if (err)
  6652. return err;
  6653. if (tg3_flag(tp, 57765_PLUS)) {
  6654. val = tr32(TG3PCI_DMA_RW_CTRL) &
  6655. ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
  6656. if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0)
  6657. val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
  6658. if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57765 &&
  6659. GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
  6660. val |= DMA_RWCTRL_TAGGED_STAT_WA;
  6661. tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
  6662. } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
  6663. GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761) {
  6664. /* This value is determined during the probe time DMA
  6665. * engine test, tg3_test_dma.
  6666. */
  6667. tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
  6668. }
  6669. tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
  6670. GRC_MODE_4X_NIC_SEND_RINGS |
  6671. GRC_MODE_NO_TX_PHDR_CSUM |
  6672. GRC_MODE_NO_RX_PHDR_CSUM);
  6673. tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
  6674. /* Pseudo-header checksum is done by hardware logic and not
  6675. * the offload processers, so make the chip do the pseudo-
  6676. * header checksums on receive. For transmit it is more
  6677. * convenient to do the pseudo-header checksum in software
  6678. * as Linux does that on transmit for us in all cases.
  6679. */
  6680. tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
  6681. tw32(GRC_MODE,
  6682. tp->grc_mode |
  6683. (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
  6684. /* Setup the timer prescalar register. Clock is always 66Mhz. */
  6685. val = tr32(GRC_MISC_CFG);
  6686. val &= ~0xff;
  6687. val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
  6688. tw32(GRC_MISC_CFG, val);
  6689. /* Initialize MBUF/DESC pool. */
  6690. if (tg3_flag(tp, 5750_PLUS)) {
  6691. /* Do nothing. */
  6692. } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
  6693. tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
  6694. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
  6695. tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
  6696. else
  6697. tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
  6698. tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
  6699. tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
  6700. } else if (tg3_flag(tp, TSO_CAPABLE)) {
  6701. int fw_len;
  6702. fw_len = tp->fw_len;
  6703. fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
  6704. tw32(BUFMGR_MB_POOL_ADDR,
  6705. NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
  6706. tw32(BUFMGR_MB_POOL_SIZE,
  6707. NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
  6708. }
  6709. if (tp->dev->mtu <= ETH_DATA_LEN) {
  6710. tw32(BUFMGR_MB_RDMA_LOW_WATER,
  6711. tp->bufmgr_config.mbuf_read_dma_low_water);
  6712. tw32(BUFMGR_MB_MACRX_LOW_WATER,
  6713. tp->bufmgr_config.mbuf_mac_rx_low_water);
  6714. tw32(BUFMGR_MB_HIGH_WATER,
  6715. tp->bufmgr_config.mbuf_high_water);
  6716. } else {
  6717. tw32(BUFMGR_MB_RDMA_LOW_WATER,
  6718. tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
  6719. tw32(BUFMGR_MB_MACRX_LOW_WATER,
  6720. tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
  6721. tw32(BUFMGR_MB_HIGH_WATER,
  6722. tp->bufmgr_config.mbuf_high_water_jumbo);
  6723. }
  6724. tw32(BUFMGR_DMA_LOW_WATER,
  6725. tp->bufmgr_config.dma_low_water);
  6726. tw32(BUFMGR_DMA_HIGH_WATER,
  6727. tp->bufmgr_config.dma_high_water);
  6728. val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
  6729. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
  6730. val |= BUFMGR_MODE_NO_TX_UNDERRUN;
  6731. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
  6732. tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
  6733. tp->pci_chip_rev_id == CHIPREV_ID_5720_A0)
  6734. val |= BUFMGR_MODE_MBLOW_ATTN_ENAB;
  6735. tw32(BUFMGR_MODE, val);
  6736. for (i = 0; i < 2000; i++) {
  6737. if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
  6738. break;
  6739. udelay(10);
  6740. }
  6741. if (i >= 2000) {
  6742. netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
  6743. return -ENODEV;
  6744. }
  6745. if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
  6746. tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
  6747. tg3_setup_rxbd_thresholds(tp);
  6748. /* Initialize TG3_BDINFO's at:
  6749. * RCVDBDI_STD_BD: standard eth size rx ring
  6750. * RCVDBDI_JUMBO_BD: jumbo frame rx ring
  6751. * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
  6752. *
  6753. * like so:
  6754. * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
  6755. * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
  6756. * ring attribute flags
  6757. * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
  6758. *
  6759. * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
  6760. * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
  6761. *
  6762. * The size of each ring is fixed in the firmware, but the location is
  6763. * configurable.
  6764. */
  6765. tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
  6766. ((u64) tpr->rx_std_mapping >> 32));
  6767. tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
  6768. ((u64) tpr->rx_std_mapping & 0xffffffff));
  6769. if (!tg3_flag(tp, 5717_PLUS))
  6770. tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
  6771. NIC_SRAM_RX_BUFFER_DESC);
  6772. /* Disable the mini ring */
  6773. if (!tg3_flag(tp, 5705_PLUS))
  6774. tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
  6775. BDINFO_FLAGS_DISABLED);
  6776. /* Program the jumbo buffer descriptor ring control
  6777. * blocks on those devices that have them.
  6778. */
  6779. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
  6780. (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))) {
  6781. if (tg3_flag(tp, JUMBO_RING_ENABLE)) {
  6782. tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
  6783. ((u64) tpr->rx_jmb_mapping >> 32));
  6784. tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
  6785. ((u64) tpr->rx_jmb_mapping & 0xffffffff));
  6786. val = TG3_RX_JMB_RING_SIZE(tp) <<
  6787. BDINFO_FLAGS_MAXLEN_SHIFT;
  6788. tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
  6789. val | BDINFO_FLAGS_USE_EXT_RECV);
  6790. if (!tg3_flag(tp, USE_JUMBO_BDFLAG) ||
  6791. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
  6792. tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
  6793. NIC_SRAM_RX_JUMBO_BUFFER_DESC);
  6794. } else {
  6795. tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
  6796. BDINFO_FLAGS_DISABLED);
  6797. }
  6798. if (tg3_flag(tp, 57765_PLUS)) {
  6799. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
  6800. val = TG3_RX_STD_MAX_SIZE_5700;
  6801. else
  6802. val = TG3_RX_STD_MAX_SIZE_5717;
  6803. val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
  6804. val |= (TG3_RX_STD_DMA_SZ << 2);
  6805. } else
  6806. val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
  6807. } else
  6808. val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
  6809. tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
  6810. tpr->rx_std_prod_idx = tp->rx_pending;
  6811. tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
  6812. tpr->rx_jmb_prod_idx =
  6813. tg3_flag(tp, JUMBO_RING_ENABLE) ? tp->rx_jumbo_pending : 0;
  6814. tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
  6815. tg3_rings_reset(tp);
  6816. /* Initialize MAC address and backoff seed. */
  6817. __tg3_set_mac_addr(tp, 0);
  6818. /* MTU + ethernet header + FCS + optional VLAN tag */
  6819. tw32(MAC_RX_MTU_SIZE,
  6820. tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
  6821. /* The slot time is changed by tg3_setup_phy if we
  6822. * run at gigabit with half duplex.
  6823. */
  6824. val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
  6825. (6 << TX_LENGTHS_IPG_SHIFT) |
  6826. (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
  6827. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
  6828. val |= tr32(MAC_TX_LENGTHS) &
  6829. (TX_LENGTHS_JMB_FRM_LEN_MSK |
  6830. TX_LENGTHS_CNT_DWN_VAL_MSK);
  6831. tw32(MAC_TX_LENGTHS, val);
  6832. /* Receive rules. */
  6833. tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
  6834. tw32(RCVLPC_CONFIG, 0x0181);
  6835. /* Calculate RDMAC_MODE setting early, we need it to determine
  6836. * the RCVLPC_STATE_ENABLE mask.
  6837. */
  6838. rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
  6839. RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
  6840. RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
  6841. RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
  6842. RDMAC_MODE_LNGREAD_ENAB);
  6843. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
  6844. rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
  6845. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
  6846. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
  6847. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
  6848. rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
  6849. RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
  6850. RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
  6851. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
  6852. tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
  6853. if (tg3_flag(tp, TSO_CAPABLE) &&
  6854. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
  6855. rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
  6856. } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
  6857. !tg3_flag(tp, IS_5788)) {
  6858. rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
  6859. }
  6860. }
  6861. if (tg3_flag(tp, PCI_EXPRESS))
  6862. rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
  6863. if (tg3_flag(tp, HW_TSO_1) ||
  6864. tg3_flag(tp, HW_TSO_2) ||
  6865. tg3_flag(tp, HW_TSO_3))
  6866. rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
  6867. if (tg3_flag(tp, 57765_PLUS) ||
  6868. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
  6869. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
  6870. rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
  6871. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
  6872. rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
  6873. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
  6874. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
  6875. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
  6876. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
  6877. tg3_flag(tp, 57765_PLUS)) {
  6878. val = tr32(TG3_RDMA_RSRVCTRL_REG);
  6879. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
  6880. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
  6881. val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
  6882. TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
  6883. TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
  6884. val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
  6885. TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
  6886. TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
  6887. }
  6888. tw32(TG3_RDMA_RSRVCTRL_REG,
  6889. val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
  6890. }
  6891. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
  6892. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
  6893. val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
  6894. tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val |
  6895. TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
  6896. TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
  6897. }
  6898. /* Receive/send statistics. */
  6899. if (tg3_flag(tp, 5750_PLUS)) {
  6900. val = tr32(RCVLPC_STATS_ENABLE);
  6901. val &= ~RCVLPC_STATSENAB_DACK_FIX;
  6902. tw32(RCVLPC_STATS_ENABLE, val);
  6903. } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
  6904. tg3_flag(tp, TSO_CAPABLE)) {
  6905. val = tr32(RCVLPC_STATS_ENABLE);
  6906. val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
  6907. tw32(RCVLPC_STATS_ENABLE, val);
  6908. } else {
  6909. tw32(RCVLPC_STATS_ENABLE, 0xffffff);
  6910. }
  6911. tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
  6912. tw32(SNDDATAI_STATSENAB, 0xffffff);
  6913. tw32(SNDDATAI_STATSCTRL,
  6914. (SNDDATAI_SCTRL_ENABLE |
  6915. SNDDATAI_SCTRL_FASTUPD));
  6916. /* Setup host coalescing engine. */
  6917. tw32(HOSTCC_MODE, 0);
  6918. for (i = 0; i < 2000; i++) {
  6919. if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
  6920. break;
  6921. udelay(10);
  6922. }
  6923. __tg3_set_coalesce(tp, &tp->coal);
  6924. if (!tg3_flag(tp, 5705_PLUS)) {
  6925. /* Status/statistics block address. See tg3_timer,
  6926. * the tg3_periodic_fetch_stats call there, and
  6927. * tg3_get_stats to see how this works for 5705/5750 chips.
  6928. */
  6929. tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
  6930. ((u64) tp->stats_mapping >> 32));
  6931. tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
  6932. ((u64) tp->stats_mapping & 0xffffffff));
  6933. tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
  6934. tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
  6935. /* Clear statistics and status block memory areas */
  6936. for (i = NIC_SRAM_STATS_BLK;
  6937. i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
  6938. i += sizeof(u32)) {
  6939. tg3_write_mem(tp, i, 0);
  6940. udelay(40);
  6941. }
  6942. }
  6943. tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
  6944. tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
  6945. tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
  6946. if (!tg3_flag(tp, 5705_PLUS))
  6947. tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
  6948. if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
  6949. tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
  6950. /* reset to prevent losing 1st rx packet intermittently */
  6951. tw32_f(MAC_RX_MODE, RX_MODE_RESET);
  6952. udelay(10);
  6953. }
  6954. tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
  6955. MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE |
  6956. MAC_MODE_FHDE_ENABLE;
  6957. if (tg3_flag(tp, ENABLE_APE))
  6958. tp->mac_mode |= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
  6959. if (!tg3_flag(tp, 5705_PLUS) &&
  6960. !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
  6961. GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
  6962. tp->mac_mode |= MAC_MODE_LINK_POLARITY;
  6963. tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
  6964. udelay(40);
  6965. /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
  6966. * If TG3_FLAG_IS_NIC is zero, we should read the
  6967. * register to preserve the GPIO settings for LOMs. The GPIOs,
  6968. * whether used as inputs or outputs, are set by boot code after
  6969. * reset.
  6970. */
  6971. if (!tg3_flag(tp, IS_NIC)) {
  6972. u32 gpio_mask;
  6973. gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
  6974. GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
  6975. GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
  6976. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
  6977. gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
  6978. GRC_LCLCTRL_GPIO_OUTPUT3;
  6979. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
  6980. gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
  6981. tp->grc_local_ctrl &= ~gpio_mask;
  6982. tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
  6983. /* GPIO1 must be driven high for eeprom write protect */
  6984. if (tg3_flag(tp, EEPROM_WRITE_PROT))
  6985. tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
  6986. GRC_LCLCTRL_GPIO_OUTPUT1);
  6987. }
  6988. tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
  6989. udelay(100);
  6990. if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1) {
  6991. val = tr32(MSGINT_MODE);
  6992. val |= MSGINT_MODE_MULTIVEC_EN | MSGINT_MODE_ENABLE;
  6993. tw32(MSGINT_MODE, val);
  6994. }
  6995. if (!tg3_flag(tp, 5705_PLUS)) {
  6996. tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
  6997. udelay(40);
  6998. }
  6999. val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
  7000. WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
  7001. WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
  7002. WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
  7003. WDMAC_MODE_LNGREAD_ENAB);
  7004. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
  7005. tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
  7006. if (tg3_flag(tp, TSO_CAPABLE) &&
  7007. (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
  7008. tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
  7009. /* nothing */
  7010. } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
  7011. !tg3_flag(tp, IS_5788)) {
  7012. val |= WDMAC_MODE_RX_ACCEL;
  7013. }
  7014. }
  7015. /* Enable host coalescing bug fix */
  7016. if (tg3_flag(tp, 5755_PLUS))
  7017. val |= WDMAC_MODE_STATUS_TAG_FIX;
  7018. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
  7019. val |= WDMAC_MODE_BURST_ALL_DATA;
  7020. tw32_f(WDMAC_MODE, val);
  7021. udelay(40);
  7022. if (tg3_flag(tp, PCIX_MODE)) {
  7023. u16 pcix_cmd;
  7024. pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
  7025. &pcix_cmd);
  7026. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
  7027. pcix_cmd &= ~PCI_X_CMD_MAX_READ;
  7028. pcix_cmd |= PCI_X_CMD_READ_2K;
  7029. } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
  7030. pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
  7031. pcix_cmd |= PCI_X_CMD_READ_2K;
  7032. }
  7033. pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
  7034. pcix_cmd);
  7035. }
  7036. tw32_f(RDMAC_MODE, rdmac_mode);
  7037. udelay(40);
  7038. tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
  7039. if (!tg3_flag(tp, 5705_PLUS))
  7040. tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
  7041. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
  7042. tw32(SNDDATAC_MODE,
  7043. SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
  7044. else
  7045. tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
  7046. tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
  7047. tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
  7048. val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
  7049. if (tg3_flag(tp, LRG_PROD_RING_CAP))
  7050. val |= RCVDBDI_MODE_LRG_RING_SZ;
  7051. tw32(RCVDBDI_MODE, val);
  7052. tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
  7053. if (tg3_flag(tp, HW_TSO_1) ||
  7054. tg3_flag(tp, HW_TSO_2) ||
  7055. tg3_flag(tp, HW_TSO_3))
  7056. tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
  7057. val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
  7058. if (tg3_flag(tp, ENABLE_TSS))
  7059. val |= SNDBDI_MODE_MULTI_TXQ_EN;
  7060. tw32(SNDBDI_MODE, val);
  7061. tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
  7062. if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
  7063. err = tg3_load_5701_a0_firmware_fix(tp);
  7064. if (err)
  7065. return err;
  7066. }
  7067. if (tg3_flag(tp, TSO_CAPABLE)) {
  7068. err = tg3_load_tso_firmware(tp);
  7069. if (err)
  7070. return err;
  7071. }
  7072. tp->tx_mode = TX_MODE_ENABLE;
  7073. if (tg3_flag(tp, 5755_PLUS) ||
  7074. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
  7075. tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
  7076. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
  7077. val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
  7078. tp->tx_mode &= ~val;
  7079. tp->tx_mode |= tr32(MAC_TX_MODE) & val;
  7080. }
  7081. tw32_f(MAC_TX_MODE, tp->tx_mode);
  7082. udelay(100);
  7083. if (tg3_flag(tp, ENABLE_RSS)) {
  7084. u32 reg = MAC_RSS_INDIR_TBL_0;
  7085. u8 *ent = (u8 *)&val;
  7086. /* Setup the indirection table */
  7087. for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
  7088. int idx = i % sizeof(val);
  7089. ent[idx] = i % (tp->irq_cnt - 1);
  7090. if (idx == sizeof(val) - 1) {
  7091. tw32(reg, val);
  7092. reg += 4;
  7093. }
  7094. }
  7095. /* Setup the "secret" hash key. */
  7096. tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
  7097. tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
  7098. tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
  7099. tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
  7100. tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
  7101. tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
  7102. tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
  7103. tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
  7104. tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
  7105. tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
  7106. }
  7107. tp->rx_mode = RX_MODE_ENABLE;
  7108. if (tg3_flag(tp, 5755_PLUS))
  7109. tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
  7110. if (tg3_flag(tp, ENABLE_RSS))
  7111. tp->rx_mode |= RX_MODE_RSS_ENABLE |
  7112. RX_MODE_RSS_ITBL_HASH_BITS_7 |
  7113. RX_MODE_RSS_IPV6_HASH_EN |
  7114. RX_MODE_RSS_TCP_IPV6_HASH_EN |
  7115. RX_MODE_RSS_IPV4_HASH_EN |
  7116. RX_MODE_RSS_TCP_IPV4_HASH_EN;
  7117. tw32_f(MAC_RX_MODE, tp->rx_mode);
  7118. udelay(10);
  7119. tw32(MAC_LED_CTRL, tp->led_ctrl);
  7120. tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
  7121. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
  7122. tw32_f(MAC_RX_MODE, RX_MODE_RESET);
  7123. udelay(10);
  7124. }
  7125. tw32_f(MAC_RX_MODE, tp->rx_mode);
  7126. udelay(10);
  7127. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
  7128. if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
  7129. !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
  7130. /* Set drive transmission level to 1.2V */
  7131. /* only if the signal pre-emphasis bit is not set */
  7132. val = tr32(MAC_SERDES_CFG);
  7133. val &= 0xfffff000;
  7134. val |= 0x880;
  7135. tw32(MAC_SERDES_CFG, val);
  7136. }
  7137. if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
  7138. tw32(MAC_SERDES_CFG, 0x616000);
  7139. }
  7140. /* Prevent chip from dropping frames when flow control
  7141. * is enabled.
  7142. */
  7143. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
  7144. val = 1;
  7145. else
  7146. val = 2;
  7147. tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
  7148. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
  7149. (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
  7150. /* Use hardware link auto-negotiation */
  7151. tg3_flag_set(tp, HW_AUTONEG);
  7152. }
  7153. if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
  7154. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
  7155. u32 tmp;
  7156. tmp = tr32(SERDES_RX_CTRL);
  7157. tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
  7158. tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
  7159. tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
  7160. tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
  7161. }
  7162. if (!tg3_flag(tp, USE_PHYLIB)) {
  7163. if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
  7164. tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
  7165. tp->link_config.speed = tp->link_config.orig_speed;
  7166. tp->link_config.duplex = tp->link_config.orig_duplex;
  7167. tp->link_config.autoneg = tp->link_config.orig_autoneg;
  7168. }
  7169. err = tg3_setup_phy(tp, 0);
  7170. if (err)
  7171. return err;
  7172. if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
  7173. !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
  7174. u32 tmp;
  7175. /* Clear CRC stats. */
  7176. if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
  7177. tg3_writephy(tp, MII_TG3_TEST1,
  7178. tmp | MII_TG3_TEST1_CRC_EN);
  7179. tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
  7180. }
  7181. }
  7182. }
  7183. __tg3_set_rx_mode(tp->dev);
  7184. /* Initialize receive rules. */
  7185. tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
  7186. tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
  7187. tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
  7188. tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
  7189. if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS))
  7190. limit = 8;
  7191. else
  7192. limit = 16;
  7193. if (tg3_flag(tp, ENABLE_ASF))
  7194. limit -= 4;
  7195. switch (limit) {
  7196. case 16:
  7197. tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
  7198. case 15:
  7199. tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
  7200. case 14:
  7201. tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
  7202. case 13:
  7203. tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
  7204. case 12:
  7205. tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
  7206. case 11:
  7207. tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
  7208. case 10:
  7209. tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
  7210. case 9:
  7211. tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
  7212. case 8:
  7213. tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
  7214. case 7:
  7215. tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
  7216. case 6:
  7217. tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
  7218. case 5:
  7219. tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
  7220. case 4:
  7221. /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
  7222. case 3:
  7223. /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
  7224. case 2:
  7225. case 1:
  7226. default:
  7227. break;
  7228. }
  7229. if (tg3_flag(tp, ENABLE_APE))
  7230. /* Write our heartbeat update interval to APE. */
  7231. tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
  7232. APE_HOST_HEARTBEAT_INT_DISABLE);
  7233. tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
  7234. return 0;
  7235. }
  7236. /* Called at device open time to get the chip ready for
  7237. * packet processing. Invoked with tp->lock held.
  7238. */
  7239. static int tg3_init_hw(struct tg3 *tp, int reset_phy)
  7240. {
  7241. tg3_switch_clocks(tp);
  7242. tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
  7243. return tg3_reset_hw(tp, reset_phy);
  7244. }
  7245. #define TG3_STAT_ADD32(PSTAT, REG) \
  7246. do { u32 __val = tr32(REG); \
  7247. (PSTAT)->low += __val; \
  7248. if ((PSTAT)->low < __val) \
  7249. (PSTAT)->high += 1; \
  7250. } while (0)
  7251. static void tg3_periodic_fetch_stats(struct tg3 *tp)
  7252. {
  7253. struct tg3_hw_stats *sp = tp->hw_stats;
  7254. if (!netif_carrier_ok(tp->dev))
  7255. return;
  7256. TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
  7257. TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
  7258. TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
  7259. TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
  7260. TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
  7261. TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
  7262. TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
  7263. TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
  7264. TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
  7265. TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
  7266. TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
  7267. TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
  7268. TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
  7269. TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
  7270. TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
  7271. TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
  7272. TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
  7273. TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
  7274. TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
  7275. TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
  7276. TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
  7277. TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
  7278. TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
  7279. TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
  7280. TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
  7281. TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
  7282. TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
  7283. TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
  7284. if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
  7285. tp->pci_chip_rev_id != CHIPREV_ID_5719_A0 &&
  7286. tp->pci_chip_rev_id != CHIPREV_ID_5720_A0) {
  7287. TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
  7288. } else {
  7289. u32 val = tr32(HOSTCC_FLOW_ATTN);
  7290. val = (val & HOSTCC_FLOW_ATTN_MBUF_LWM) ? 1 : 0;
  7291. if (val) {
  7292. tw32(HOSTCC_FLOW_ATTN, HOSTCC_FLOW_ATTN_MBUF_LWM);
  7293. sp->rx_discards.low += val;
  7294. if (sp->rx_discards.low < val)
  7295. sp->rx_discards.high += 1;
  7296. }
  7297. sp->mbuf_lwm_thresh_hit = sp->rx_discards;
  7298. }
  7299. TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
  7300. }
  7301. static void tg3_timer(unsigned long __opaque)
  7302. {
  7303. struct tg3 *tp = (struct tg3 *) __opaque;
  7304. if (tp->irq_sync)
  7305. goto restart_timer;
  7306. spin_lock(&tp->lock);
  7307. if (!tg3_flag(tp, TAGGED_STATUS)) {
  7308. /* All of this garbage is because when using non-tagged
  7309. * IRQ status the mailbox/status_block protocol the chip
  7310. * uses with the cpu is race prone.
  7311. */
  7312. if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
  7313. tw32(GRC_LOCAL_CTRL,
  7314. tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
  7315. } else {
  7316. tw32(HOSTCC_MODE, tp->coalesce_mode |
  7317. HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
  7318. }
  7319. if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
  7320. tg3_flag_set(tp, RESTART_TIMER);
  7321. spin_unlock(&tp->lock);
  7322. schedule_work(&tp->reset_task);
  7323. return;
  7324. }
  7325. }
  7326. /* This part only runs once per second. */
  7327. if (!--tp->timer_counter) {
  7328. if (tg3_flag(tp, 5705_PLUS))
  7329. tg3_periodic_fetch_stats(tp);
  7330. if (tp->setlpicnt && !--tp->setlpicnt)
  7331. tg3_phy_eee_enable(tp);
  7332. if (tg3_flag(tp, USE_LINKCHG_REG)) {
  7333. u32 mac_stat;
  7334. int phy_event;
  7335. mac_stat = tr32(MAC_STATUS);
  7336. phy_event = 0;
  7337. if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
  7338. if (mac_stat & MAC_STATUS_MI_INTERRUPT)
  7339. phy_event = 1;
  7340. } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
  7341. phy_event = 1;
  7342. if (phy_event)
  7343. tg3_setup_phy(tp, 0);
  7344. } else if (tg3_flag(tp, POLL_SERDES)) {
  7345. u32 mac_stat = tr32(MAC_STATUS);
  7346. int need_setup = 0;
  7347. if (netif_carrier_ok(tp->dev) &&
  7348. (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
  7349. need_setup = 1;
  7350. }
  7351. if (!netif_carrier_ok(tp->dev) &&
  7352. (mac_stat & (MAC_STATUS_PCS_SYNCED |
  7353. MAC_STATUS_SIGNAL_DET))) {
  7354. need_setup = 1;
  7355. }
  7356. if (need_setup) {
  7357. if (!tp->serdes_counter) {
  7358. tw32_f(MAC_MODE,
  7359. (tp->mac_mode &
  7360. ~MAC_MODE_PORT_MODE_MASK));
  7361. udelay(40);
  7362. tw32_f(MAC_MODE, tp->mac_mode);
  7363. udelay(40);
  7364. }
  7365. tg3_setup_phy(tp, 0);
  7366. }
  7367. } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
  7368. tg3_flag(tp, 5780_CLASS)) {
  7369. tg3_serdes_parallel_detect(tp);
  7370. }
  7371. tp->timer_counter = tp->timer_multiplier;
  7372. }
  7373. /* Heartbeat is only sent once every 2 seconds.
  7374. *
  7375. * The heartbeat is to tell the ASF firmware that the host
  7376. * driver is still alive. In the event that the OS crashes,
  7377. * ASF needs to reset the hardware to free up the FIFO space
  7378. * that may be filled with rx packets destined for the host.
  7379. * If the FIFO is full, ASF will no longer function properly.
  7380. *
  7381. * Unintended resets have been reported on real time kernels
  7382. * where the timer doesn't run on time. Netpoll will also have
  7383. * same problem.
  7384. *
  7385. * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
  7386. * to check the ring condition when the heartbeat is expiring
  7387. * before doing the reset. This will prevent most unintended
  7388. * resets.
  7389. */
  7390. if (!--tp->asf_counter) {
  7391. if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
  7392. tg3_wait_for_event_ack(tp);
  7393. tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
  7394. FWCMD_NICDRV_ALIVE3);
  7395. tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
  7396. tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
  7397. TG3_FW_UPDATE_TIMEOUT_SEC);
  7398. tg3_generate_fw_event(tp);
  7399. }
  7400. tp->asf_counter = tp->asf_multiplier;
  7401. }
  7402. spin_unlock(&tp->lock);
  7403. restart_timer:
  7404. tp->timer.expires = jiffies + tp->timer_offset;
  7405. add_timer(&tp->timer);
  7406. }
  7407. static int tg3_request_irq(struct tg3 *tp, int irq_num)
  7408. {
  7409. irq_handler_t fn;
  7410. unsigned long flags;
  7411. char *name;
  7412. struct tg3_napi *tnapi = &tp->napi[irq_num];
  7413. if (tp->irq_cnt == 1)
  7414. name = tp->dev->name;
  7415. else {
  7416. name = &tnapi->irq_lbl[0];
  7417. snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
  7418. name[IFNAMSIZ-1] = 0;
  7419. }
  7420. if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
  7421. fn = tg3_msi;
  7422. if (tg3_flag(tp, 1SHOT_MSI))
  7423. fn = tg3_msi_1shot;
  7424. flags = 0;
  7425. } else {
  7426. fn = tg3_interrupt;
  7427. if (tg3_flag(tp, TAGGED_STATUS))
  7428. fn = tg3_interrupt_tagged;
  7429. flags = IRQF_SHARED;
  7430. }
  7431. return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
  7432. }
  7433. static int tg3_test_interrupt(struct tg3 *tp)
  7434. {
  7435. struct tg3_napi *tnapi = &tp->napi[0];
  7436. struct net_device *dev = tp->dev;
  7437. int err, i, intr_ok = 0;
  7438. u32 val;
  7439. if (!netif_running(dev))
  7440. return -ENODEV;
  7441. tg3_disable_ints(tp);
  7442. free_irq(tnapi->irq_vec, tnapi);
  7443. /*
  7444. * Turn off MSI one shot mode. Otherwise this test has no
  7445. * observable way to know whether the interrupt was delivered.
  7446. */
  7447. if (tg3_flag(tp, 57765_PLUS)) {
  7448. val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
  7449. tw32(MSGINT_MODE, val);
  7450. }
  7451. err = request_irq(tnapi->irq_vec, tg3_test_isr,
  7452. IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, tnapi);
  7453. if (err)
  7454. return err;
  7455. tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
  7456. tg3_enable_ints(tp);
  7457. tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
  7458. tnapi->coal_now);
  7459. for (i = 0; i < 5; i++) {
  7460. u32 int_mbox, misc_host_ctrl;
  7461. int_mbox = tr32_mailbox(tnapi->int_mbox);
  7462. misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
  7463. if ((int_mbox != 0) ||
  7464. (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
  7465. intr_ok = 1;
  7466. break;
  7467. }
  7468. if (tg3_flag(tp, 57765_PLUS) &&
  7469. tnapi->hw_status->status_tag != tnapi->last_tag)
  7470. tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
  7471. msleep(10);
  7472. }
  7473. tg3_disable_ints(tp);
  7474. free_irq(tnapi->irq_vec, tnapi);
  7475. err = tg3_request_irq(tp, 0);
  7476. if (err)
  7477. return err;
  7478. if (intr_ok) {
  7479. /* Reenable MSI one shot mode. */
  7480. if (tg3_flag(tp, 57765_PLUS)) {
  7481. val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
  7482. tw32(MSGINT_MODE, val);
  7483. }
  7484. return 0;
  7485. }
  7486. return -EIO;
  7487. }
  7488. /* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
  7489. * successfully restored
  7490. */
  7491. static int tg3_test_msi(struct tg3 *tp)
  7492. {
  7493. int err;
  7494. u16 pci_cmd;
  7495. if (!tg3_flag(tp, USING_MSI))
  7496. return 0;
  7497. /* Turn off SERR reporting in case MSI terminates with Master
  7498. * Abort.
  7499. */
  7500. pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
  7501. pci_write_config_word(tp->pdev, PCI_COMMAND,
  7502. pci_cmd & ~PCI_COMMAND_SERR);
  7503. err = tg3_test_interrupt(tp);
  7504. pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
  7505. if (!err)
  7506. return 0;
  7507. /* other failures */
  7508. if (err != -EIO)
  7509. return err;
  7510. /* MSI test failed, go back to INTx mode */
  7511. netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
  7512. "to INTx mode. Please report this failure to the PCI "
  7513. "maintainer and include system chipset information\n");
  7514. free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
  7515. pci_disable_msi(tp->pdev);
  7516. tg3_flag_clear(tp, USING_MSI);
  7517. tp->napi[0].irq_vec = tp->pdev->irq;
  7518. err = tg3_request_irq(tp, 0);
  7519. if (err)
  7520. return err;
  7521. /* Need to reset the chip because the MSI cycle may have terminated
  7522. * with Master Abort.
  7523. */
  7524. tg3_full_lock(tp, 1);
  7525. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  7526. err = tg3_init_hw(tp, 1);
  7527. tg3_full_unlock(tp);
  7528. if (err)
  7529. free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
  7530. return err;
  7531. }
  7532. static int tg3_request_firmware(struct tg3 *tp)
  7533. {
  7534. const __be32 *fw_data;
  7535. if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
  7536. netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
  7537. tp->fw_needed);
  7538. return -ENOENT;
  7539. }
  7540. fw_data = (void *)tp->fw->data;
  7541. /* Firmware blob starts with version numbers, followed by
  7542. * start address and _full_ length including BSS sections
  7543. * (which must be longer than the actual data, of course
  7544. */
  7545. tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
  7546. if (tp->fw_len < (tp->fw->size - 12)) {
  7547. netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
  7548. tp->fw_len, tp->fw_needed);
  7549. release_firmware(tp->fw);
  7550. tp->fw = NULL;
  7551. return -EINVAL;
  7552. }
  7553. /* We no longer need firmware; we have it. */
  7554. tp->fw_needed = NULL;
  7555. return 0;
  7556. }
  7557. static bool tg3_enable_msix(struct tg3 *tp)
  7558. {
  7559. int i, rc, cpus = num_online_cpus();
  7560. struct msix_entry msix_ent[tp->irq_max];
  7561. if (cpus == 1)
  7562. /* Just fallback to the simpler MSI mode. */
  7563. return false;
  7564. /*
  7565. * We want as many rx rings enabled as there are cpus.
  7566. * The first MSIX vector only deals with link interrupts, etc,
  7567. * so we add one to the number of vectors we are requesting.
  7568. */
  7569. tp->irq_cnt = min_t(unsigned, cpus + 1, tp->irq_max);
  7570. for (i = 0; i < tp->irq_max; i++) {
  7571. msix_ent[i].entry = i;
  7572. msix_ent[i].vector = 0;
  7573. }
  7574. rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
  7575. if (rc < 0) {
  7576. return false;
  7577. } else if (rc != 0) {
  7578. if (pci_enable_msix(tp->pdev, msix_ent, rc))
  7579. return false;
  7580. netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
  7581. tp->irq_cnt, rc);
  7582. tp->irq_cnt = rc;
  7583. }
  7584. for (i = 0; i < tp->irq_max; i++)
  7585. tp->napi[i].irq_vec = msix_ent[i].vector;
  7586. netif_set_real_num_tx_queues(tp->dev, 1);
  7587. rc = tp->irq_cnt > 1 ? tp->irq_cnt - 1 : 1;
  7588. if (netif_set_real_num_rx_queues(tp->dev, rc)) {
  7589. pci_disable_msix(tp->pdev);
  7590. return false;
  7591. }
  7592. if (tp->irq_cnt > 1) {
  7593. tg3_flag_set(tp, ENABLE_RSS);
  7594. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
  7595. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
  7596. tg3_flag_set(tp, ENABLE_TSS);
  7597. netif_set_real_num_tx_queues(tp->dev, tp->irq_cnt - 1);
  7598. }
  7599. }
  7600. return true;
  7601. }
  7602. static void tg3_ints_init(struct tg3 *tp)
  7603. {
  7604. if ((tg3_flag(tp, SUPPORT_MSI) || tg3_flag(tp, SUPPORT_MSIX)) &&
  7605. !tg3_flag(tp, TAGGED_STATUS)) {
  7606. /* All MSI supporting chips should support tagged
  7607. * status. Assert that this is the case.
  7608. */
  7609. netdev_warn(tp->dev,
  7610. "MSI without TAGGED_STATUS? Not using MSI\n");
  7611. goto defcfg;
  7612. }
  7613. if (tg3_flag(tp, SUPPORT_MSIX) && tg3_enable_msix(tp))
  7614. tg3_flag_set(tp, USING_MSIX);
  7615. else if (tg3_flag(tp, SUPPORT_MSI) && pci_enable_msi(tp->pdev) == 0)
  7616. tg3_flag_set(tp, USING_MSI);
  7617. if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
  7618. u32 msi_mode = tr32(MSGINT_MODE);
  7619. if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1)
  7620. msi_mode |= MSGINT_MODE_MULTIVEC_EN;
  7621. tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
  7622. }
  7623. defcfg:
  7624. if (!tg3_flag(tp, USING_MSIX)) {
  7625. tp->irq_cnt = 1;
  7626. tp->napi[0].irq_vec = tp->pdev->irq;
  7627. netif_set_real_num_tx_queues(tp->dev, 1);
  7628. netif_set_real_num_rx_queues(tp->dev, 1);
  7629. }
  7630. }
  7631. static void tg3_ints_fini(struct tg3 *tp)
  7632. {
  7633. if (tg3_flag(tp, USING_MSIX))
  7634. pci_disable_msix(tp->pdev);
  7635. else if (tg3_flag(tp, USING_MSI))
  7636. pci_disable_msi(tp->pdev);
  7637. tg3_flag_clear(tp, USING_MSI);
  7638. tg3_flag_clear(tp, USING_MSIX);
  7639. tg3_flag_clear(tp, ENABLE_RSS);
  7640. tg3_flag_clear(tp, ENABLE_TSS);
  7641. }
  7642. static int tg3_open(struct net_device *dev)
  7643. {
  7644. struct tg3 *tp = netdev_priv(dev);
  7645. int i, err;
  7646. if (tp->fw_needed) {
  7647. err = tg3_request_firmware(tp);
  7648. if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
  7649. if (err)
  7650. return err;
  7651. } else if (err) {
  7652. netdev_warn(tp->dev, "TSO capability disabled\n");
  7653. tg3_flag_clear(tp, TSO_CAPABLE);
  7654. } else if (!tg3_flag(tp, TSO_CAPABLE)) {
  7655. netdev_notice(tp->dev, "TSO capability restored\n");
  7656. tg3_flag_set(tp, TSO_CAPABLE);
  7657. }
  7658. }
  7659. netif_carrier_off(tp->dev);
  7660. err = tg3_power_up(tp);
  7661. if (err)
  7662. return err;
  7663. tg3_full_lock(tp, 0);
  7664. tg3_disable_ints(tp);
  7665. tg3_flag_clear(tp, INIT_COMPLETE);
  7666. tg3_full_unlock(tp);
  7667. /*
  7668. * Setup interrupts first so we know how
  7669. * many NAPI resources to allocate
  7670. */
  7671. tg3_ints_init(tp);
  7672. /* The placement of this call is tied
  7673. * to the setup and use of Host TX descriptors.
  7674. */
  7675. err = tg3_alloc_consistent(tp);
  7676. if (err)
  7677. goto err_out1;
  7678. tg3_napi_init(tp);
  7679. tg3_napi_enable(tp);
  7680. for (i = 0; i < tp->irq_cnt; i++) {
  7681. struct tg3_napi *tnapi = &tp->napi[i];
  7682. err = tg3_request_irq(tp, i);
  7683. if (err) {
  7684. for (i--; i >= 0; i--)
  7685. free_irq(tnapi->irq_vec, tnapi);
  7686. break;
  7687. }
  7688. }
  7689. if (err)
  7690. goto err_out2;
  7691. tg3_full_lock(tp, 0);
  7692. err = tg3_init_hw(tp, 1);
  7693. if (err) {
  7694. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  7695. tg3_free_rings(tp);
  7696. } else {
  7697. if (tg3_flag(tp, TAGGED_STATUS))
  7698. tp->timer_offset = HZ;
  7699. else
  7700. tp->timer_offset = HZ / 10;
  7701. BUG_ON(tp->timer_offset > HZ);
  7702. tp->timer_counter = tp->timer_multiplier =
  7703. (HZ / tp->timer_offset);
  7704. tp->asf_counter = tp->asf_multiplier =
  7705. ((HZ / tp->timer_offset) * 2);
  7706. init_timer(&tp->timer);
  7707. tp->timer.expires = jiffies + tp->timer_offset;
  7708. tp->timer.data = (unsigned long) tp;
  7709. tp->timer.function = tg3_timer;
  7710. }
  7711. tg3_full_unlock(tp);
  7712. if (err)
  7713. goto err_out3;
  7714. if (tg3_flag(tp, USING_MSI)) {
  7715. err = tg3_test_msi(tp);
  7716. if (err) {
  7717. tg3_full_lock(tp, 0);
  7718. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  7719. tg3_free_rings(tp);
  7720. tg3_full_unlock(tp);
  7721. goto err_out2;
  7722. }
  7723. if (!tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
  7724. u32 val = tr32(PCIE_TRANSACTION_CFG);
  7725. tw32(PCIE_TRANSACTION_CFG,
  7726. val | PCIE_TRANS_CFG_1SHOT_MSI);
  7727. }
  7728. }
  7729. tg3_phy_start(tp);
  7730. tg3_full_lock(tp, 0);
  7731. add_timer(&tp->timer);
  7732. tg3_flag_set(tp, INIT_COMPLETE);
  7733. tg3_enable_ints(tp);
  7734. tg3_full_unlock(tp);
  7735. netif_tx_start_all_queues(dev);
  7736. /*
  7737. * Reset loopback feature if it was turned on while the device was down
  7738. * make sure that it's installed properly now.
  7739. */
  7740. if (dev->features & NETIF_F_LOOPBACK)
  7741. tg3_set_loopback(dev, dev->features);
  7742. return 0;
  7743. err_out3:
  7744. for (i = tp->irq_cnt - 1; i >= 0; i--) {
  7745. struct tg3_napi *tnapi = &tp->napi[i];
  7746. free_irq(tnapi->irq_vec, tnapi);
  7747. }
  7748. err_out2:
  7749. tg3_napi_disable(tp);
  7750. tg3_napi_fini(tp);
  7751. tg3_free_consistent(tp);
  7752. err_out1:
  7753. tg3_ints_fini(tp);
  7754. return err;
  7755. }
  7756. static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *,
  7757. struct rtnl_link_stats64 *);
  7758. static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *);
  7759. static int tg3_close(struct net_device *dev)
  7760. {
  7761. int i;
  7762. struct tg3 *tp = netdev_priv(dev);
  7763. tg3_napi_disable(tp);
  7764. cancel_work_sync(&tp->reset_task);
  7765. netif_tx_stop_all_queues(dev);
  7766. del_timer_sync(&tp->timer);
  7767. tg3_phy_stop(tp);
  7768. tg3_full_lock(tp, 1);
  7769. tg3_disable_ints(tp);
  7770. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  7771. tg3_free_rings(tp);
  7772. tg3_flag_clear(tp, INIT_COMPLETE);
  7773. tg3_full_unlock(tp);
  7774. for (i = tp->irq_cnt - 1; i >= 0; i--) {
  7775. struct tg3_napi *tnapi = &tp->napi[i];
  7776. free_irq(tnapi->irq_vec, tnapi);
  7777. }
  7778. tg3_ints_fini(tp);
  7779. tg3_get_stats64(tp->dev, &tp->net_stats_prev);
  7780. memcpy(&tp->estats_prev, tg3_get_estats(tp),
  7781. sizeof(tp->estats_prev));
  7782. tg3_napi_fini(tp);
  7783. tg3_free_consistent(tp);
  7784. tg3_power_down(tp);
  7785. netif_carrier_off(tp->dev);
  7786. return 0;
  7787. }
  7788. static inline u64 get_stat64(tg3_stat64_t *val)
  7789. {
  7790. return ((u64)val->high << 32) | ((u64)val->low);
  7791. }
  7792. static u64 calc_crc_errors(struct tg3 *tp)
  7793. {
  7794. struct tg3_hw_stats *hw_stats = tp->hw_stats;
  7795. if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
  7796. (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
  7797. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
  7798. u32 val;
  7799. spin_lock_bh(&tp->lock);
  7800. if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
  7801. tg3_writephy(tp, MII_TG3_TEST1,
  7802. val | MII_TG3_TEST1_CRC_EN);
  7803. tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
  7804. } else
  7805. val = 0;
  7806. spin_unlock_bh(&tp->lock);
  7807. tp->phy_crc_errors += val;
  7808. return tp->phy_crc_errors;
  7809. }
  7810. return get_stat64(&hw_stats->rx_fcs_errors);
  7811. }
  7812. #define ESTAT_ADD(member) \
  7813. estats->member = old_estats->member + \
  7814. get_stat64(&hw_stats->member)
  7815. static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *tp)
  7816. {
  7817. struct tg3_ethtool_stats *estats = &tp->estats;
  7818. struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
  7819. struct tg3_hw_stats *hw_stats = tp->hw_stats;
  7820. if (!hw_stats)
  7821. return old_estats;
  7822. ESTAT_ADD(rx_octets);
  7823. ESTAT_ADD(rx_fragments);
  7824. ESTAT_ADD(rx_ucast_packets);
  7825. ESTAT_ADD(rx_mcast_packets);
  7826. ESTAT_ADD(rx_bcast_packets);
  7827. ESTAT_ADD(rx_fcs_errors);
  7828. ESTAT_ADD(rx_align_errors);
  7829. ESTAT_ADD(rx_xon_pause_rcvd);
  7830. ESTAT_ADD(rx_xoff_pause_rcvd);
  7831. ESTAT_ADD(rx_mac_ctrl_rcvd);
  7832. ESTAT_ADD(rx_xoff_entered);
  7833. ESTAT_ADD(rx_frame_too_long_errors);
  7834. ESTAT_ADD(rx_jabbers);
  7835. ESTAT_ADD(rx_undersize_packets);
  7836. ESTAT_ADD(rx_in_length_errors);
  7837. ESTAT_ADD(rx_out_length_errors);
  7838. ESTAT_ADD(rx_64_or_less_octet_packets);
  7839. ESTAT_ADD(rx_65_to_127_octet_packets);
  7840. ESTAT_ADD(rx_128_to_255_octet_packets);
  7841. ESTAT_ADD(rx_256_to_511_octet_packets);
  7842. ESTAT_ADD(rx_512_to_1023_octet_packets);
  7843. ESTAT_ADD(rx_1024_to_1522_octet_packets);
  7844. ESTAT_ADD(rx_1523_to_2047_octet_packets);
  7845. ESTAT_ADD(rx_2048_to_4095_octet_packets);
  7846. ESTAT_ADD(rx_4096_to_8191_octet_packets);
  7847. ESTAT_ADD(rx_8192_to_9022_octet_packets);
  7848. ESTAT_ADD(tx_octets);
  7849. ESTAT_ADD(tx_collisions);
  7850. ESTAT_ADD(tx_xon_sent);
  7851. ESTAT_ADD(tx_xoff_sent);
  7852. ESTAT_ADD(tx_flow_control);
  7853. ESTAT_ADD(tx_mac_errors);
  7854. ESTAT_ADD(tx_single_collisions);
  7855. ESTAT_ADD(tx_mult_collisions);
  7856. ESTAT_ADD(tx_deferred);
  7857. ESTAT_ADD(tx_excessive_collisions);
  7858. ESTAT_ADD(tx_late_collisions);
  7859. ESTAT_ADD(tx_collide_2times);
  7860. ESTAT_ADD(tx_collide_3times);
  7861. ESTAT_ADD(tx_collide_4times);
  7862. ESTAT_ADD(tx_collide_5times);
  7863. ESTAT_ADD(tx_collide_6times);
  7864. ESTAT_ADD(tx_collide_7times);
  7865. ESTAT_ADD(tx_collide_8times);
  7866. ESTAT_ADD(tx_collide_9times);
  7867. ESTAT_ADD(tx_collide_10times);
  7868. ESTAT_ADD(tx_collide_11times);
  7869. ESTAT_ADD(tx_collide_12times);
  7870. ESTAT_ADD(tx_collide_13times);
  7871. ESTAT_ADD(tx_collide_14times);
  7872. ESTAT_ADD(tx_collide_15times);
  7873. ESTAT_ADD(tx_ucast_packets);
  7874. ESTAT_ADD(tx_mcast_packets);
  7875. ESTAT_ADD(tx_bcast_packets);
  7876. ESTAT_ADD(tx_carrier_sense_errors);
  7877. ESTAT_ADD(tx_discards);
  7878. ESTAT_ADD(tx_errors);
  7879. ESTAT_ADD(dma_writeq_full);
  7880. ESTAT_ADD(dma_write_prioq_full);
  7881. ESTAT_ADD(rxbds_empty);
  7882. ESTAT_ADD(rx_discards);
  7883. ESTAT_ADD(rx_errors);
  7884. ESTAT_ADD(rx_threshold_hit);
  7885. ESTAT_ADD(dma_readq_full);
  7886. ESTAT_ADD(dma_read_prioq_full);
  7887. ESTAT_ADD(tx_comp_queue_full);
  7888. ESTAT_ADD(ring_set_send_prod_index);
  7889. ESTAT_ADD(ring_status_update);
  7890. ESTAT_ADD(nic_irqs);
  7891. ESTAT_ADD(nic_avoided_irqs);
  7892. ESTAT_ADD(nic_tx_threshold_hit);
  7893. ESTAT_ADD(mbuf_lwm_thresh_hit);
  7894. return estats;
  7895. }
  7896. static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
  7897. struct rtnl_link_stats64 *stats)
  7898. {
  7899. struct tg3 *tp = netdev_priv(dev);
  7900. struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
  7901. struct tg3_hw_stats *hw_stats = tp->hw_stats;
  7902. if (!hw_stats)
  7903. return old_stats;
  7904. stats->rx_packets = old_stats->rx_packets +
  7905. get_stat64(&hw_stats->rx_ucast_packets) +
  7906. get_stat64(&hw_stats->rx_mcast_packets) +
  7907. get_stat64(&hw_stats->rx_bcast_packets);
  7908. stats->tx_packets = old_stats->tx_packets +
  7909. get_stat64(&hw_stats->tx_ucast_packets) +
  7910. get_stat64(&hw_stats->tx_mcast_packets) +
  7911. get_stat64(&hw_stats->tx_bcast_packets);
  7912. stats->rx_bytes = old_stats->rx_bytes +
  7913. get_stat64(&hw_stats->rx_octets);
  7914. stats->tx_bytes = old_stats->tx_bytes +
  7915. get_stat64(&hw_stats->tx_octets);
  7916. stats->rx_errors = old_stats->rx_errors +
  7917. get_stat64(&hw_stats->rx_errors);
  7918. stats->tx_errors = old_stats->tx_errors +
  7919. get_stat64(&hw_stats->tx_errors) +
  7920. get_stat64(&hw_stats->tx_mac_errors) +
  7921. get_stat64(&hw_stats->tx_carrier_sense_errors) +
  7922. get_stat64(&hw_stats->tx_discards);
  7923. stats->multicast = old_stats->multicast +
  7924. get_stat64(&hw_stats->rx_mcast_packets);
  7925. stats->collisions = old_stats->collisions +
  7926. get_stat64(&hw_stats->tx_collisions);
  7927. stats->rx_length_errors = old_stats->rx_length_errors +
  7928. get_stat64(&hw_stats->rx_frame_too_long_errors) +
  7929. get_stat64(&hw_stats->rx_undersize_packets);
  7930. stats->rx_over_errors = old_stats->rx_over_errors +
  7931. get_stat64(&hw_stats->rxbds_empty);
  7932. stats->rx_frame_errors = old_stats->rx_frame_errors +
  7933. get_stat64(&hw_stats->rx_align_errors);
  7934. stats->tx_aborted_errors = old_stats->tx_aborted_errors +
  7935. get_stat64(&hw_stats->tx_discards);
  7936. stats->tx_carrier_errors = old_stats->tx_carrier_errors +
  7937. get_stat64(&hw_stats->tx_carrier_sense_errors);
  7938. stats->rx_crc_errors = old_stats->rx_crc_errors +
  7939. calc_crc_errors(tp);
  7940. stats->rx_missed_errors = old_stats->rx_missed_errors +
  7941. get_stat64(&hw_stats->rx_discards);
  7942. stats->rx_dropped = tp->rx_dropped;
  7943. return stats;
  7944. }
  7945. static inline u32 calc_crc(unsigned char *buf, int len)
  7946. {
  7947. u32 reg;
  7948. u32 tmp;
  7949. int j, k;
  7950. reg = 0xffffffff;
  7951. for (j = 0; j < len; j++) {
  7952. reg ^= buf[j];
  7953. for (k = 0; k < 8; k++) {
  7954. tmp = reg & 0x01;
  7955. reg >>= 1;
  7956. if (tmp)
  7957. reg ^= 0xedb88320;
  7958. }
  7959. }
  7960. return ~reg;
  7961. }
  7962. static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
  7963. {
  7964. /* accept or reject all multicast frames */
  7965. tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
  7966. tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
  7967. tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
  7968. tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
  7969. }
  7970. static void __tg3_set_rx_mode(struct net_device *dev)
  7971. {
  7972. struct tg3 *tp = netdev_priv(dev);
  7973. u32 rx_mode;
  7974. rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
  7975. RX_MODE_KEEP_VLAN_TAG);
  7976. #if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
  7977. /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
  7978. * flag clear.
  7979. */
  7980. if (!tg3_flag(tp, ENABLE_ASF))
  7981. rx_mode |= RX_MODE_KEEP_VLAN_TAG;
  7982. #endif
  7983. if (dev->flags & IFF_PROMISC) {
  7984. /* Promiscuous mode. */
  7985. rx_mode |= RX_MODE_PROMISC;
  7986. } else if (dev->flags & IFF_ALLMULTI) {
  7987. /* Accept all multicast. */
  7988. tg3_set_multi(tp, 1);
  7989. } else if (netdev_mc_empty(dev)) {
  7990. /* Reject all multicast. */
  7991. tg3_set_multi(tp, 0);
  7992. } else {
  7993. /* Accept one or more multicast(s). */
  7994. struct netdev_hw_addr *ha;
  7995. u32 mc_filter[4] = { 0, };
  7996. u32 regidx;
  7997. u32 bit;
  7998. u32 crc;
  7999. netdev_for_each_mc_addr(ha, dev) {
  8000. crc = calc_crc(ha->addr, ETH_ALEN);
  8001. bit = ~crc & 0x7f;
  8002. regidx = (bit & 0x60) >> 5;
  8003. bit &= 0x1f;
  8004. mc_filter[regidx] |= (1 << bit);
  8005. }
  8006. tw32(MAC_HASH_REG_0, mc_filter[0]);
  8007. tw32(MAC_HASH_REG_1, mc_filter[1]);
  8008. tw32(MAC_HASH_REG_2, mc_filter[2]);
  8009. tw32(MAC_HASH_REG_3, mc_filter[3]);
  8010. }
  8011. if (rx_mode != tp->rx_mode) {
  8012. tp->rx_mode = rx_mode;
  8013. tw32_f(MAC_RX_MODE, rx_mode);
  8014. udelay(10);
  8015. }
  8016. }
  8017. static void tg3_set_rx_mode(struct net_device *dev)
  8018. {
  8019. struct tg3 *tp = netdev_priv(dev);
  8020. if (!netif_running(dev))
  8021. return;
  8022. tg3_full_lock(tp, 0);
  8023. __tg3_set_rx_mode(dev);
  8024. tg3_full_unlock(tp);
  8025. }
  8026. static int tg3_get_regs_len(struct net_device *dev)
  8027. {
  8028. return TG3_REG_BLK_SIZE;
  8029. }
  8030. static void tg3_get_regs(struct net_device *dev,
  8031. struct ethtool_regs *regs, void *_p)
  8032. {
  8033. struct tg3 *tp = netdev_priv(dev);
  8034. regs->version = 0;
  8035. memset(_p, 0, TG3_REG_BLK_SIZE);
  8036. if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
  8037. return;
  8038. tg3_full_lock(tp, 0);
  8039. tg3_dump_legacy_regs(tp, (u32 *)_p);
  8040. tg3_full_unlock(tp);
  8041. }
  8042. static int tg3_get_eeprom_len(struct net_device *dev)
  8043. {
  8044. struct tg3 *tp = netdev_priv(dev);
  8045. return tp->nvram_size;
  8046. }
  8047. static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
  8048. {
  8049. struct tg3 *tp = netdev_priv(dev);
  8050. int ret;
  8051. u8 *pd;
  8052. u32 i, offset, len, b_offset, b_count;
  8053. __be32 val;
  8054. if (tg3_flag(tp, NO_NVRAM))
  8055. return -EINVAL;
  8056. if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
  8057. return -EAGAIN;
  8058. offset = eeprom->offset;
  8059. len = eeprom->len;
  8060. eeprom->len = 0;
  8061. eeprom->magic = TG3_EEPROM_MAGIC;
  8062. if (offset & 3) {
  8063. /* adjustments to start on required 4 byte boundary */
  8064. b_offset = offset & 3;
  8065. b_count = 4 - b_offset;
  8066. if (b_count > len) {
  8067. /* i.e. offset=1 len=2 */
  8068. b_count = len;
  8069. }
  8070. ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
  8071. if (ret)
  8072. return ret;
  8073. memcpy(data, ((char *)&val) + b_offset, b_count);
  8074. len -= b_count;
  8075. offset += b_count;
  8076. eeprom->len += b_count;
  8077. }
  8078. /* read bytes up to the last 4 byte boundary */
  8079. pd = &data[eeprom->len];
  8080. for (i = 0; i < (len - (len & 3)); i += 4) {
  8081. ret = tg3_nvram_read_be32(tp, offset + i, &val);
  8082. if (ret) {
  8083. eeprom->len += i;
  8084. return ret;
  8085. }
  8086. memcpy(pd + i, &val, 4);
  8087. }
  8088. eeprom->len += i;
  8089. if (len & 3) {
  8090. /* read last bytes not ending on 4 byte boundary */
  8091. pd = &data[eeprom->len];
  8092. b_count = len & 3;
  8093. b_offset = offset + len - b_count;
  8094. ret = tg3_nvram_read_be32(tp, b_offset, &val);
  8095. if (ret)
  8096. return ret;
  8097. memcpy(pd, &val, b_count);
  8098. eeprom->len += b_count;
  8099. }
  8100. return 0;
  8101. }
  8102. static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf);
  8103. static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
  8104. {
  8105. struct tg3 *tp = netdev_priv(dev);
  8106. int ret;
  8107. u32 offset, len, b_offset, odd_len;
  8108. u8 *buf;
  8109. __be32 start, end;
  8110. if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
  8111. return -EAGAIN;
  8112. if (tg3_flag(tp, NO_NVRAM) ||
  8113. eeprom->magic != TG3_EEPROM_MAGIC)
  8114. return -EINVAL;
  8115. offset = eeprom->offset;
  8116. len = eeprom->len;
  8117. if ((b_offset = (offset & 3))) {
  8118. /* adjustments to start on required 4 byte boundary */
  8119. ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
  8120. if (ret)
  8121. return ret;
  8122. len += b_offset;
  8123. offset &= ~3;
  8124. if (len < 4)
  8125. len = 4;
  8126. }
  8127. odd_len = 0;
  8128. if (len & 3) {
  8129. /* adjustments to end on required 4 byte boundary */
  8130. odd_len = 1;
  8131. len = (len + 3) & ~3;
  8132. ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
  8133. if (ret)
  8134. return ret;
  8135. }
  8136. buf = data;
  8137. if (b_offset || odd_len) {
  8138. buf = kmalloc(len, GFP_KERNEL);
  8139. if (!buf)
  8140. return -ENOMEM;
  8141. if (b_offset)
  8142. memcpy(buf, &start, 4);
  8143. if (odd_len)
  8144. memcpy(buf+len-4, &end, 4);
  8145. memcpy(buf + b_offset, data, eeprom->len);
  8146. }
  8147. ret = tg3_nvram_write_block(tp, offset, len, buf);
  8148. if (buf != data)
  8149. kfree(buf);
  8150. return ret;
  8151. }
  8152. static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  8153. {
  8154. struct tg3 *tp = netdev_priv(dev);
  8155. if (tg3_flag(tp, USE_PHYLIB)) {
  8156. struct phy_device *phydev;
  8157. if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
  8158. return -EAGAIN;
  8159. phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
  8160. return phy_ethtool_gset(phydev, cmd);
  8161. }
  8162. cmd->supported = (SUPPORTED_Autoneg);
  8163. if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
  8164. cmd->supported |= (SUPPORTED_1000baseT_Half |
  8165. SUPPORTED_1000baseT_Full);
  8166. if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
  8167. cmd->supported |= (SUPPORTED_100baseT_Half |
  8168. SUPPORTED_100baseT_Full |
  8169. SUPPORTED_10baseT_Half |
  8170. SUPPORTED_10baseT_Full |
  8171. SUPPORTED_TP);
  8172. cmd->port = PORT_TP;
  8173. } else {
  8174. cmd->supported |= SUPPORTED_FIBRE;
  8175. cmd->port = PORT_FIBRE;
  8176. }
  8177. cmd->advertising = tp->link_config.advertising;
  8178. if (netif_running(dev)) {
  8179. ethtool_cmd_speed_set(cmd, tp->link_config.active_speed);
  8180. cmd->duplex = tp->link_config.active_duplex;
  8181. } else {
  8182. ethtool_cmd_speed_set(cmd, SPEED_INVALID);
  8183. cmd->duplex = DUPLEX_INVALID;
  8184. }
  8185. cmd->phy_address = tp->phy_addr;
  8186. cmd->transceiver = XCVR_INTERNAL;
  8187. cmd->autoneg = tp->link_config.autoneg;
  8188. cmd->maxtxpkt = 0;
  8189. cmd->maxrxpkt = 0;
  8190. return 0;
  8191. }
  8192. static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  8193. {
  8194. struct tg3 *tp = netdev_priv(dev);
  8195. u32 speed = ethtool_cmd_speed(cmd);
  8196. if (tg3_flag(tp, USE_PHYLIB)) {
  8197. struct phy_device *phydev;
  8198. if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
  8199. return -EAGAIN;
  8200. phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
  8201. return phy_ethtool_sset(phydev, cmd);
  8202. }
  8203. if (cmd->autoneg != AUTONEG_ENABLE &&
  8204. cmd->autoneg != AUTONEG_DISABLE)
  8205. return -EINVAL;
  8206. if (cmd->autoneg == AUTONEG_DISABLE &&
  8207. cmd->duplex != DUPLEX_FULL &&
  8208. cmd->duplex != DUPLEX_HALF)
  8209. return -EINVAL;
  8210. if (cmd->autoneg == AUTONEG_ENABLE) {
  8211. u32 mask = ADVERTISED_Autoneg |
  8212. ADVERTISED_Pause |
  8213. ADVERTISED_Asym_Pause;
  8214. if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
  8215. mask |= ADVERTISED_1000baseT_Half |
  8216. ADVERTISED_1000baseT_Full;
  8217. if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
  8218. mask |= ADVERTISED_100baseT_Half |
  8219. ADVERTISED_100baseT_Full |
  8220. ADVERTISED_10baseT_Half |
  8221. ADVERTISED_10baseT_Full |
  8222. ADVERTISED_TP;
  8223. else
  8224. mask |= ADVERTISED_FIBRE;
  8225. if (cmd->advertising & ~mask)
  8226. return -EINVAL;
  8227. mask &= (ADVERTISED_1000baseT_Half |
  8228. ADVERTISED_1000baseT_Full |
  8229. ADVERTISED_100baseT_Half |
  8230. ADVERTISED_100baseT_Full |
  8231. ADVERTISED_10baseT_Half |
  8232. ADVERTISED_10baseT_Full);
  8233. cmd->advertising &= mask;
  8234. } else {
  8235. if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
  8236. if (speed != SPEED_1000)
  8237. return -EINVAL;
  8238. if (cmd->duplex != DUPLEX_FULL)
  8239. return -EINVAL;
  8240. } else {
  8241. if (speed != SPEED_100 &&
  8242. speed != SPEED_10)
  8243. return -EINVAL;
  8244. }
  8245. }
  8246. tg3_full_lock(tp, 0);
  8247. tp->link_config.autoneg = cmd->autoneg;
  8248. if (cmd->autoneg == AUTONEG_ENABLE) {
  8249. tp->link_config.advertising = (cmd->advertising |
  8250. ADVERTISED_Autoneg);
  8251. tp->link_config.speed = SPEED_INVALID;
  8252. tp->link_config.duplex = DUPLEX_INVALID;
  8253. } else {
  8254. tp->link_config.advertising = 0;
  8255. tp->link_config.speed = speed;
  8256. tp->link_config.duplex = cmd->duplex;
  8257. }
  8258. tp->link_config.orig_speed = tp->link_config.speed;
  8259. tp->link_config.orig_duplex = tp->link_config.duplex;
  8260. tp->link_config.orig_autoneg = tp->link_config.autoneg;
  8261. if (netif_running(dev))
  8262. tg3_setup_phy(tp, 1);
  8263. tg3_full_unlock(tp);
  8264. return 0;
  8265. }
  8266. static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  8267. {
  8268. struct tg3 *tp = netdev_priv(dev);
  8269. strcpy(info->driver, DRV_MODULE_NAME);
  8270. strcpy(info->version, DRV_MODULE_VERSION);
  8271. strcpy(info->fw_version, tp->fw_ver);
  8272. strcpy(info->bus_info, pci_name(tp->pdev));
  8273. }
  8274. static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  8275. {
  8276. struct tg3 *tp = netdev_priv(dev);
  8277. if (tg3_flag(tp, WOL_CAP) && device_can_wakeup(&tp->pdev->dev))
  8278. wol->supported = WAKE_MAGIC;
  8279. else
  8280. wol->supported = 0;
  8281. wol->wolopts = 0;
  8282. if (tg3_flag(tp, WOL_ENABLE) && device_can_wakeup(&tp->pdev->dev))
  8283. wol->wolopts = WAKE_MAGIC;
  8284. memset(&wol->sopass, 0, sizeof(wol->sopass));
  8285. }
  8286. static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  8287. {
  8288. struct tg3 *tp = netdev_priv(dev);
  8289. struct device *dp = &tp->pdev->dev;
  8290. if (wol->wolopts & ~WAKE_MAGIC)
  8291. return -EINVAL;
  8292. if ((wol->wolopts & WAKE_MAGIC) &&
  8293. !(tg3_flag(tp, WOL_CAP) && device_can_wakeup(dp)))
  8294. return -EINVAL;
  8295. device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
  8296. spin_lock_bh(&tp->lock);
  8297. if (device_may_wakeup(dp))
  8298. tg3_flag_set(tp, WOL_ENABLE);
  8299. else
  8300. tg3_flag_clear(tp, WOL_ENABLE);
  8301. spin_unlock_bh(&tp->lock);
  8302. return 0;
  8303. }
  8304. static u32 tg3_get_msglevel(struct net_device *dev)
  8305. {
  8306. struct tg3 *tp = netdev_priv(dev);
  8307. return tp->msg_enable;
  8308. }
  8309. static void tg3_set_msglevel(struct net_device *dev, u32 value)
  8310. {
  8311. struct tg3 *tp = netdev_priv(dev);
  8312. tp->msg_enable = value;
  8313. }
  8314. static int tg3_nway_reset(struct net_device *dev)
  8315. {
  8316. struct tg3 *tp = netdev_priv(dev);
  8317. int r;
  8318. if (!netif_running(dev))
  8319. return -EAGAIN;
  8320. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
  8321. return -EINVAL;
  8322. if (tg3_flag(tp, USE_PHYLIB)) {
  8323. if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
  8324. return -EAGAIN;
  8325. r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
  8326. } else {
  8327. u32 bmcr;
  8328. spin_lock_bh(&tp->lock);
  8329. r = -EINVAL;
  8330. tg3_readphy(tp, MII_BMCR, &bmcr);
  8331. if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
  8332. ((bmcr & BMCR_ANENABLE) ||
  8333. (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
  8334. tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
  8335. BMCR_ANENABLE);
  8336. r = 0;
  8337. }
  8338. spin_unlock_bh(&tp->lock);
  8339. }
  8340. return r;
  8341. }
  8342. static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
  8343. {
  8344. struct tg3 *tp = netdev_priv(dev);
  8345. ering->rx_max_pending = tp->rx_std_ring_mask;
  8346. ering->rx_mini_max_pending = 0;
  8347. if (tg3_flag(tp, JUMBO_RING_ENABLE))
  8348. ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
  8349. else
  8350. ering->rx_jumbo_max_pending = 0;
  8351. ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
  8352. ering->rx_pending = tp->rx_pending;
  8353. ering->rx_mini_pending = 0;
  8354. if (tg3_flag(tp, JUMBO_RING_ENABLE))
  8355. ering->rx_jumbo_pending = tp->rx_jumbo_pending;
  8356. else
  8357. ering->rx_jumbo_pending = 0;
  8358. ering->tx_pending = tp->napi[0].tx_pending;
  8359. }
  8360. static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
  8361. {
  8362. struct tg3 *tp = netdev_priv(dev);
  8363. int i, irq_sync = 0, err = 0;
  8364. if ((ering->rx_pending > tp->rx_std_ring_mask) ||
  8365. (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
  8366. (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
  8367. (ering->tx_pending <= MAX_SKB_FRAGS) ||
  8368. (tg3_flag(tp, TSO_BUG) &&
  8369. (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
  8370. return -EINVAL;
  8371. if (netif_running(dev)) {
  8372. tg3_phy_stop(tp);
  8373. tg3_netif_stop(tp);
  8374. irq_sync = 1;
  8375. }
  8376. tg3_full_lock(tp, irq_sync);
  8377. tp->rx_pending = ering->rx_pending;
  8378. if (tg3_flag(tp, MAX_RXPEND_64) &&
  8379. tp->rx_pending > 63)
  8380. tp->rx_pending = 63;
  8381. tp->rx_jumbo_pending = ering->rx_jumbo_pending;
  8382. for (i = 0; i < tp->irq_max; i++)
  8383. tp->napi[i].tx_pending = ering->tx_pending;
  8384. if (netif_running(dev)) {
  8385. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  8386. err = tg3_restart_hw(tp, 1);
  8387. if (!err)
  8388. tg3_netif_start(tp);
  8389. }
  8390. tg3_full_unlock(tp);
  8391. if (irq_sync && !err)
  8392. tg3_phy_start(tp);
  8393. return err;
  8394. }
  8395. static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
  8396. {
  8397. struct tg3 *tp = netdev_priv(dev);
  8398. epause->autoneg = !!tg3_flag(tp, PAUSE_AUTONEG);
  8399. if (tp->link_config.active_flowctrl & FLOW_CTRL_RX)
  8400. epause->rx_pause = 1;
  8401. else
  8402. epause->rx_pause = 0;
  8403. if (tp->link_config.active_flowctrl & FLOW_CTRL_TX)
  8404. epause->tx_pause = 1;
  8405. else
  8406. epause->tx_pause = 0;
  8407. }
  8408. static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
  8409. {
  8410. struct tg3 *tp = netdev_priv(dev);
  8411. int err = 0;
  8412. if (tg3_flag(tp, USE_PHYLIB)) {
  8413. u32 newadv;
  8414. struct phy_device *phydev;
  8415. phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
  8416. if (!(phydev->supported & SUPPORTED_Pause) ||
  8417. (!(phydev->supported & SUPPORTED_Asym_Pause) &&
  8418. (epause->rx_pause != epause->tx_pause)))
  8419. return -EINVAL;
  8420. tp->link_config.flowctrl = 0;
  8421. if (epause->rx_pause) {
  8422. tp->link_config.flowctrl |= FLOW_CTRL_RX;
  8423. if (epause->tx_pause) {
  8424. tp->link_config.flowctrl |= FLOW_CTRL_TX;
  8425. newadv = ADVERTISED_Pause;
  8426. } else
  8427. newadv = ADVERTISED_Pause |
  8428. ADVERTISED_Asym_Pause;
  8429. } else if (epause->tx_pause) {
  8430. tp->link_config.flowctrl |= FLOW_CTRL_TX;
  8431. newadv = ADVERTISED_Asym_Pause;
  8432. } else
  8433. newadv = 0;
  8434. if (epause->autoneg)
  8435. tg3_flag_set(tp, PAUSE_AUTONEG);
  8436. else
  8437. tg3_flag_clear(tp, PAUSE_AUTONEG);
  8438. if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
  8439. u32 oldadv = phydev->advertising &
  8440. (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
  8441. if (oldadv != newadv) {
  8442. phydev->advertising &=
  8443. ~(ADVERTISED_Pause |
  8444. ADVERTISED_Asym_Pause);
  8445. phydev->advertising |= newadv;
  8446. if (phydev->autoneg) {
  8447. /*
  8448. * Always renegotiate the link to
  8449. * inform our link partner of our
  8450. * flow control settings, even if the
  8451. * flow control is forced. Let
  8452. * tg3_adjust_link() do the final
  8453. * flow control setup.
  8454. */
  8455. return phy_start_aneg(phydev);
  8456. }
  8457. }
  8458. if (!epause->autoneg)
  8459. tg3_setup_flow_control(tp, 0, 0);
  8460. } else {
  8461. tp->link_config.orig_advertising &=
  8462. ~(ADVERTISED_Pause |
  8463. ADVERTISED_Asym_Pause);
  8464. tp->link_config.orig_advertising |= newadv;
  8465. }
  8466. } else {
  8467. int irq_sync = 0;
  8468. if (netif_running(dev)) {
  8469. tg3_netif_stop(tp);
  8470. irq_sync = 1;
  8471. }
  8472. tg3_full_lock(tp, irq_sync);
  8473. if (epause->autoneg)
  8474. tg3_flag_set(tp, PAUSE_AUTONEG);
  8475. else
  8476. tg3_flag_clear(tp, PAUSE_AUTONEG);
  8477. if (epause->rx_pause)
  8478. tp->link_config.flowctrl |= FLOW_CTRL_RX;
  8479. else
  8480. tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
  8481. if (epause->tx_pause)
  8482. tp->link_config.flowctrl |= FLOW_CTRL_TX;
  8483. else
  8484. tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
  8485. if (netif_running(dev)) {
  8486. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  8487. err = tg3_restart_hw(tp, 1);
  8488. if (!err)
  8489. tg3_netif_start(tp);
  8490. }
  8491. tg3_full_unlock(tp);
  8492. }
  8493. return err;
  8494. }
  8495. static int tg3_get_sset_count(struct net_device *dev, int sset)
  8496. {
  8497. switch (sset) {
  8498. case ETH_SS_TEST:
  8499. return TG3_NUM_TEST;
  8500. case ETH_SS_STATS:
  8501. return TG3_NUM_STATS;
  8502. default:
  8503. return -EOPNOTSUPP;
  8504. }
  8505. }
  8506. static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
  8507. {
  8508. switch (stringset) {
  8509. case ETH_SS_STATS:
  8510. memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
  8511. break;
  8512. case ETH_SS_TEST:
  8513. memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
  8514. break;
  8515. default:
  8516. WARN_ON(1); /* we need a WARN() */
  8517. break;
  8518. }
  8519. }
  8520. static int tg3_set_phys_id(struct net_device *dev,
  8521. enum ethtool_phys_id_state state)
  8522. {
  8523. struct tg3 *tp = netdev_priv(dev);
  8524. if (!netif_running(tp->dev))
  8525. return -EAGAIN;
  8526. switch (state) {
  8527. case ETHTOOL_ID_ACTIVE:
  8528. return 1; /* cycle on/off once per second */
  8529. case ETHTOOL_ID_ON:
  8530. tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
  8531. LED_CTRL_1000MBPS_ON |
  8532. LED_CTRL_100MBPS_ON |
  8533. LED_CTRL_10MBPS_ON |
  8534. LED_CTRL_TRAFFIC_OVERRIDE |
  8535. LED_CTRL_TRAFFIC_BLINK |
  8536. LED_CTRL_TRAFFIC_LED);
  8537. break;
  8538. case ETHTOOL_ID_OFF:
  8539. tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
  8540. LED_CTRL_TRAFFIC_OVERRIDE);
  8541. break;
  8542. case ETHTOOL_ID_INACTIVE:
  8543. tw32(MAC_LED_CTRL, tp->led_ctrl);
  8544. break;
  8545. }
  8546. return 0;
  8547. }
  8548. static void tg3_get_ethtool_stats(struct net_device *dev,
  8549. struct ethtool_stats *estats, u64 *tmp_stats)
  8550. {
  8551. struct tg3 *tp = netdev_priv(dev);
  8552. memcpy(tmp_stats, tg3_get_estats(tp), sizeof(tp->estats));
  8553. }
  8554. static __be32 * tg3_vpd_readblock(struct tg3 *tp)
  8555. {
  8556. int i;
  8557. __be32 *buf;
  8558. u32 offset = 0, len = 0;
  8559. u32 magic, val;
  8560. if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &magic))
  8561. return NULL;
  8562. if (magic == TG3_EEPROM_MAGIC) {
  8563. for (offset = TG3_NVM_DIR_START;
  8564. offset < TG3_NVM_DIR_END;
  8565. offset += TG3_NVM_DIRENT_SIZE) {
  8566. if (tg3_nvram_read(tp, offset, &val))
  8567. return NULL;
  8568. if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
  8569. TG3_NVM_DIRTYPE_EXTVPD)
  8570. break;
  8571. }
  8572. if (offset != TG3_NVM_DIR_END) {
  8573. len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
  8574. if (tg3_nvram_read(tp, offset + 4, &offset))
  8575. return NULL;
  8576. offset = tg3_nvram_logical_addr(tp, offset);
  8577. }
  8578. }
  8579. if (!offset || !len) {
  8580. offset = TG3_NVM_VPD_OFF;
  8581. len = TG3_NVM_VPD_LEN;
  8582. }
  8583. buf = kmalloc(len, GFP_KERNEL);
  8584. if (buf == NULL)
  8585. return NULL;
  8586. if (magic == TG3_EEPROM_MAGIC) {
  8587. for (i = 0; i < len; i += 4) {
  8588. /* The data is in little-endian format in NVRAM.
  8589. * Use the big-endian read routines to preserve
  8590. * the byte order as it exists in NVRAM.
  8591. */
  8592. if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
  8593. goto error;
  8594. }
  8595. } else {
  8596. u8 *ptr;
  8597. ssize_t cnt;
  8598. unsigned int pos = 0;
  8599. ptr = (u8 *)&buf[0];
  8600. for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
  8601. cnt = pci_read_vpd(tp->pdev, pos,
  8602. len - pos, ptr);
  8603. if (cnt == -ETIMEDOUT || cnt == -EINTR)
  8604. cnt = 0;
  8605. else if (cnt < 0)
  8606. goto error;
  8607. }
  8608. if (pos != len)
  8609. goto error;
  8610. }
  8611. return buf;
  8612. error:
  8613. kfree(buf);
  8614. return NULL;
  8615. }
  8616. #define NVRAM_TEST_SIZE 0x100
  8617. #define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
  8618. #define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
  8619. #define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
  8620. #define NVRAM_SELFBOOT_HW_SIZE 0x20
  8621. #define NVRAM_SELFBOOT_DATA_SIZE 0x1c
  8622. static int tg3_test_nvram(struct tg3 *tp)
  8623. {
  8624. u32 csum, magic;
  8625. __be32 *buf;
  8626. int i, j, k, err = 0, size;
  8627. if (tg3_flag(tp, NO_NVRAM))
  8628. return 0;
  8629. if (tg3_nvram_read(tp, 0, &magic) != 0)
  8630. return -EIO;
  8631. if (magic == TG3_EEPROM_MAGIC)
  8632. size = NVRAM_TEST_SIZE;
  8633. else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
  8634. if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
  8635. TG3_EEPROM_SB_FORMAT_1) {
  8636. switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
  8637. case TG3_EEPROM_SB_REVISION_0:
  8638. size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
  8639. break;
  8640. case TG3_EEPROM_SB_REVISION_2:
  8641. size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
  8642. break;
  8643. case TG3_EEPROM_SB_REVISION_3:
  8644. size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
  8645. break;
  8646. default:
  8647. return 0;
  8648. }
  8649. } else
  8650. return 0;
  8651. } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
  8652. size = NVRAM_SELFBOOT_HW_SIZE;
  8653. else
  8654. return -EIO;
  8655. buf = kmalloc(size, GFP_KERNEL);
  8656. if (buf == NULL)
  8657. return -ENOMEM;
  8658. err = -EIO;
  8659. for (i = 0, j = 0; i < size; i += 4, j++) {
  8660. err = tg3_nvram_read_be32(tp, i, &buf[j]);
  8661. if (err)
  8662. break;
  8663. }
  8664. if (i < size)
  8665. goto out;
  8666. /* Selfboot format */
  8667. magic = be32_to_cpu(buf[0]);
  8668. if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
  8669. TG3_EEPROM_MAGIC_FW) {
  8670. u8 *buf8 = (u8 *) buf, csum8 = 0;
  8671. if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
  8672. TG3_EEPROM_SB_REVISION_2) {
  8673. /* For rev 2, the csum doesn't include the MBA. */
  8674. for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
  8675. csum8 += buf8[i];
  8676. for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
  8677. csum8 += buf8[i];
  8678. } else {
  8679. for (i = 0; i < size; i++)
  8680. csum8 += buf8[i];
  8681. }
  8682. if (csum8 == 0) {
  8683. err = 0;
  8684. goto out;
  8685. }
  8686. err = -EIO;
  8687. goto out;
  8688. }
  8689. if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
  8690. TG3_EEPROM_MAGIC_HW) {
  8691. u8 data[NVRAM_SELFBOOT_DATA_SIZE];
  8692. u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
  8693. u8 *buf8 = (u8 *) buf;
  8694. /* Separate the parity bits and the data bytes. */
  8695. for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
  8696. if ((i == 0) || (i == 8)) {
  8697. int l;
  8698. u8 msk;
  8699. for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
  8700. parity[k++] = buf8[i] & msk;
  8701. i++;
  8702. } else if (i == 16) {
  8703. int l;
  8704. u8 msk;
  8705. for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
  8706. parity[k++] = buf8[i] & msk;
  8707. i++;
  8708. for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
  8709. parity[k++] = buf8[i] & msk;
  8710. i++;
  8711. }
  8712. data[j++] = buf8[i];
  8713. }
  8714. err = -EIO;
  8715. for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
  8716. u8 hw8 = hweight8(data[i]);
  8717. if ((hw8 & 0x1) && parity[i])
  8718. goto out;
  8719. else if (!(hw8 & 0x1) && !parity[i])
  8720. goto out;
  8721. }
  8722. err = 0;
  8723. goto out;
  8724. }
  8725. err = -EIO;
  8726. /* Bootstrap checksum at offset 0x10 */
  8727. csum = calc_crc((unsigned char *) buf, 0x10);
  8728. if (csum != le32_to_cpu(buf[0x10/4]))
  8729. goto out;
  8730. /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
  8731. csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
  8732. if (csum != le32_to_cpu(buf[0xfc/4]))
  8733. goto out;
  8734. kfree(buf);
  8735. buf = tg3_vpd_readblock(tp);
  8736. if (!buf)
  8737. return -ENOMEM;
  8738. i = pci_vpd_find_tag((u8 *)buf, 0, TG3_NVM_VPD_LEN,
  8739. PCI_VPD_LRDT_RO_DATA);
  8740. if (i > 0) {
  8741. j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
  8742. if (j < 0)
  8743. goto out;
  8744. if (i + PCI_VPD_LRDT_TAG_SIZE + j > TG3_NVM_VPD_LEN)
  8745. goto out;
  8746. i += PCI_VPD_LRDT_TAG_SIZE;
  8747. j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
  8748. PCI_VPD_RO_KEYWORD_CHKSUM);
  8749. if (j > 0) {
  8750. u8 csum8 = 0;
  8751. j += PCI_VPD_INFO_FLD_HDR_SIZE;
  8752. for (i = 0; i <= j; i++)
  8753. csum8 += ((u8 *)buf)[i];
  8754. if (csum8)
  8755. goto out;
  8756. }
  8757. }
  8758. err = 0;
  8759. out:
  8760. kfree(buf);
  8761. return err;
  8762. }
  8763. #define TG3_SERDES_TIMEOUT_SEC 2
  8764. #define TG3_COPPER_TIMEOUT_SEC 6
  8765. static int tg3_test_link(struct tg3 *tp)
  8766. {
  8767. int i, max;
  8768. if (!netif_running(tp->dev))
  8769. return -ENODEV;
  8770. if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
  8771. max = TG3_SERDES_TIMEOUT_SEC;
  8772. else
  8773. max = TG3_COPPER_TIMEOUT_SEC;
  8774. for (i = 0; i < max; i++) {
  8775. if (netif_carrier_ok(tp->dev))
  8776. return 0;
  8777. if (msleep_interruptible(1000))
  8778. break;
  8779. }
  8780. return -EIO;
  8781. }
  8782. /* Only test the commonly used registers */
  8783. static int tg3_test_registers(struct tg3 *tp)
  8784. {
  8785. int i, is_5705, is_5750;
  8786. u32 offset, read_mask, write_mask, val, save_val, read_val;
  8787. static struct {
  8788. u16 offset;
  8789. u16 flags;
  8790. #define TG3_FL_5705 0x1
  8791. #define TG3_FL_NOT_5705 0x2
  8792. #define TG3_FL_NOT_5788 0x4
  8793. #define TG3_FL_NOT_5750 0x8
  8794. u32 read_mask;
  8795. u32 write_mask;
  8796. } reg_tbl[] = {
  8797. /* MAC Control Registers */
  8798. { MAC_MODE, TG3_FL_NOT_5705,
  8799. 0x00000000, 0x00ef6f8c },
  8800. { MAC_MODE, TG3_FL_5705,
  8801. 0x00000000, 0x01ef6b8c },
  8802. { MAC_STATUS, TG3_FL_NOT_5705,
  8803. 0x03800107, 0x00000000 },
  8804. { MAC_STATUS, TG3_FL_5705,
  8805. 0x03800100, 0x00000000 },
  8806. { MAC_ADDR_0_HIGH, 0x0000,
  8807. 0x00000000, 0x0000ffff },
  8808. { MAC_ADDR_0_LOW, 0x0000,
  8809. 0x00000000, 0xffffffff },
  8810. { MAC_RX_MTU_SIZE, 0x0000,
  8811. 0x00000000, 0x0000ffff },
  8812. { MAC_TX_MODE, 0x0000,
  8813. 0x00000000, 0x00000070 },
  8814. { MAC_TX_LENGTHS, 0x0000,
  8815. 0x00000000, 0x00003fff },
  8816. { MAC_RX_MODE, TG3_FL_NOT_5705,
  8817. 0x00000000, 0x000007fc },
  8818. { MAC_RX_MODE, TG3_FL_5705,
  8819. 0x00000000, 0x000007dc },
  8820. { MAC_HASH_REG_0, 0x0000,
  8821. 0x00000000, 0xffffffff },
  8822. { MAC_HASH_REG_1, 0x0000,
  8823. 0x00000000, 0xffffffff },
  8824. { MAC_HASH_REG_2, 0x0000,
  8825. 0x00000000, 0xffffffff },
  8826. { MAC_HASH_REG_3, 0x0000,
  8827. 0x00000000, 0xffffffff },
  8828. /* Receive Data and Receive BD Initiator Control Registers. */
  8829. { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
  8830. 0x00000000, 0xffffffff },
  8831. { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
  8832. 0x00000000, 0xffffffff },
  8833. { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
  8834. 0x00000000, 0x00000003 },
  8835. { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
  8836. 0x00000000, 0xffffffff },
  8837. { RCVDBDI_STD_BD+0, 0x0000,
  8838. 0x00000000, 0xffffffff },
  8839. { RCVDBDI_STD_BD+4, 0x0000,
  8840. 0x00000000, 0xffffffff },
  8841. { RCVDBDI_STD_BD+8, 0x0000,
  8842. 0x00000000, 0xffff0002 },
  8843. { RCVDBDI_STD_BD+0xc, 0x0000,
  8844. 0x00000000, 0xffffffff },
  8845. /* Receive BD Initiator Control Registers. */
  8846. { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
  8847. 0x00000000, 0xffffffff },
  8848. { RCVBDI_STD_THRESH, TG3_FL_5705,
  8849. 0x00000000, 0x000003ff },
  8850. { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
  8851. 0x00000000, 0xffffffff },
  8852. /* Host Coalescing Control Registers. */
  8853. { HOSTCC_MODE, TG3_FL_NOT_5705,
  8854. 0x00000000, 0x00000004 },
  8855. { HOSTCC_MODE, TG3_FL_5705,
  8856. 0x00000000, 0x000000f6 },
  8857. { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
  8858. 0x00000000, 0xffffffff },
  8859. { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
  8860. 0x00000000, 0x000003ff },
  8861. { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
  8862. 0x00000000, 0xffffffff },
  8863. { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
  8864. 0x00000000, 0x000003ff },
  8865. { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
  8866. 0x00000000, 0xffffffff },
  8867. { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
  8868. 0x00000000, 0x000000ff },
  8869. { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
  8870. 0x00000000, 0xffffffff },
  8871. { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
  8872. 0x00000000, 0x000000ff },
  8873. { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
  8874. 0x00000000, 0xffffffff },
  8875. { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
  8876. 0x00000000, 0xffffffff },
  8877. { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
  8878. 0x00000000, 0xffffffff },
  8879. { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
  8880. 0x00000000, 0x000000ff },
  8881. { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
  8882. 0x00000000, 0xffffffff },
  8883. { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
  8884. 0x00000000, 0x000000ff },
  8885. { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
  8886. 0x00000000, 0xffffffff },
  8887. { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
  8888. 0x00000000, 0xffffffff },
  8889. { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
  8890. 0x00000000, 0xffffffff },
  8891. { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
  8892. 0x00000000, 0xffffffff },
  8893. { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
  8894. 0x00000000, 0xffffffff },
  8895. { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
  8896. 0xffffffff, 0x00000000 },
  8897. { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
  8898. 0xffffffff, 0x00000000 },
  8899. /* Buffer Manager Control Registers. */
  8900. { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
  8901. 0x00000000, 0x007fff80 },
  8902. { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
  8903. 0x00000000, 0x007fffff },
  8904. { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
  8905. 0x00000000, 0x0000003f },
  8906. { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
  8907. 0x00000000, 0x000001ff },
  8908. { BUFMGR_MB_HIGH_WATER, 0x0000,
  8909. 0x00000000, 0x000001ff },
  8910. { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
  8911. 0xffffffff, 0x00000000 },
  8912. { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
  8913. 0xffffffff, 0x00000000 },
  8914. /* Mailbox Registers */
  8915. { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
  8916. 0x00000000, 0x000001ff },
  8917. { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
  8918. 0x00000000, 0x000001ff },
  8919. { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
  8920. 0x00000000, 0x000007ff },
  8921. { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
  8922. 0x00000000, 0x000001ff },
  8923. { 0xffff, 0x0000, 0x00000000, 0x00000000 },
  8924. };
  8925. is_5705 = is_5750 = 0;
  8926. if (tg3_flag(tp, 5705_PLUS)) {
  8927. is_5705 = 1;
  8928. if (tg3_flag(tp, 5750_PLUS))
  8929. is_5750 = 1;
  8930. }
  8931. for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
  8932. if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
  8933. continue;
  8934. if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
  8935. continue;
  8936. if (tg3_flag(tp, IS_5788) &&
  8937. (reg_tbl[i].flags & TG3_FL_NOT_5788))
  8938. continue;
  8939. if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
  8940. continue;
  8941. offset = (u32) reg_tbl[i].offset;
  8942. read_mask = reg_tbl[i].read_mask;
  8943. write_mask = reg_tbl[i].write_mask;
  8944. /* Save the original register content */
  8945. save_val = tr32(offset);
  8946. /* Determine the read-only value. */
  8947. read_val = save_val & read_mask;
  8948. /* Write zero to the register, then make sure the read-only bits
  8949. * are not changed and the read/write bits are all zeros.
  8950. */
  8951. tw32(offset, 0);
  8952. val = tr32(offset);
  8953. /* Test the read-only and read/write bits. */
  8954. if (((val & read_mask) != read_val) || (val & write_mask))
  8955. goto out;
  8956. /* Write ones to all the bits defined by RdMask and WrMask, then
  8957. * make sure the read-only bits are not changed and the
  8958. * read/write bits are all ones.
  8959. */
  8960. tw32(offset, read_mask | write_mask);
  8961. val = tr32(offset);
  8962. /* Test the read-only bits. */
  8963. if ((val & read_mask) != read_val)
  8964. goto out;
  8965. /* Test the read/write bits. */
  8966. if ((val & write_mask) != write_mask)
  8967. goto out;
  8968. tw32(offset, save_val);
  8969. }
  8970. return 0;
  8971. out:
  8972. if (netif_msg_hw(tp))
  8973. netdev_err(tp->dev,
  8974. "Register test failed at offset %x\n", offset);
  8975. tw32(offset, save_val);
  8976. return -EIO;
  8977. }
  8978. static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
  8979. {
  8980. static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
  8981. int i;
  8982. u32 j;
  8983. for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
  8984. for (j = 0; j < len; j += 4) {
  8985. u32 val;
  8986. tg3_write_mem(tp, offset + j, test_pattern[i]);
  8987. tg3_read_mem(tp, offset + j, &val);
  8988. if (val != test_pattern[i])
  8989. return -EIO;
  8990. }
  8991. }
  8992. return 0;
  8993. }
  8994. static int tg3_test_memory(struct tg3 *tp)
  8995. {
  8996. static struct mem_entry {
  8997. u32 offset;
  8998. u32 len;
  8999. } mem_tbl_570x[] = {
  9000. { 0x00000000, 0x00b50},
  9001. { 0x00002000, 0x1c000},
  9002. { 0xffffffff, 0x00000}
  9003. }, mem_tbl_5705[] = {
  9004. { 0x00000100, 0x0000c},
  9005. { 0x00000200, 0x00008},
  9006. { 0x00004000, 0x00800},
  9007. { 0x00006000, 0x01000},
  9008. { 0x00008000, 0x02000},
  9009. { 0x00010000, 0x0e000},
  9010. { 0xffffffff, 0x00000}
  9011. }, mem_tbl_5755[] = {
  9012. { 0x00000200, 0x00008},
  9013. { 0x00004000, 0x00800},
  9014. { 0x00006000, 0x00800},
  9015. { 0x00008000, 0x02000},
  9016. { 0x00010000, 0x0c000},
  9017. { 0xffffffff, 0x00000}
  9018. }, mem_tbl_5906[] = {
  9019. { 0x00000200, 0x00008},
  9020. { 0x00004000, 0x00400},
  9021. { 0x00006000, 0x00400},
  9022. { 0x00008000, 0x01000},
  9023. { 0x00010000, 0x01000},
  9024. { 0xffffffff, 0x00000}
  9025. }, mem_tbl_5717[] = {
  9026. { 0x00000200, 0x00008},
  9027. { 0x00010000, 0x0a000},
  9028. { 0x00020000, 0x13c00},
  9029. { 0xffffffff, 0x00000}
  9030. }, mem_tbl_57765[] = {
  9031. { 0x00000200, 0x00008},
  9032. { 0x00004000, 0x00800},
  9033. { 0x00006000, 0x09800},
  9034. { 0x00010000, 0x0a000},
  9035. { 0xffffffff, 0x00000}
  9036. };
  9037. struct mem_entry *mem_tbl;
  9038. int err = 0;
  9039. int i;
  9040. if (tg3_flag(tp, 5717_PLUS))
  9041. mem_tbl = mem_tbl_5717;
  9042. else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
  9043. mem_tbl = mem_tbl_57765;
  9044. else if (tg3_flag(tp, 5755_PLUS))
  9045. mem_tbl = mem_tbl_5755;
  9046. else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
  9047. mem_tbl = mem_tbl_5906;
  9048. else if (tg3_flag(tp, 5705_PLUS))
  9049. mem_tbl = mem_tbl_5705;
  9050. else
  9051. mem_tbl = mem_tbl_570x;
  9052. for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
  9053. err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
  9054. if (err)
  9055. break;
  9056. }
  9057. return err;
  9058. }
  9059. #define TG3_MAC_LOOPBACK 0
  9060. #define TG3_PHY_LOOPBACK 1
  9061. #define TG3_TSO_LOOPBACK 2
  9062. #define TG3_TSO_MSS 500
  9063. #define TG3_TSO_IP_HDR_LEN 20
  9064. #define TG3_TSO_TCP_HDR_LEN 20
  9065. #define TG3_TSO_TCP_OPT_LEN 12
  9066. static const u8 tg3_tso_header[] = {
  9067. 0x08, 0x00,
  9068. 0x45, 0x00, 0x00, 0x00,
  9069. 0x00, 0x00, 0x40, 0x00,
  9070. 0x40, 0x06, 0x00, 0x00,
  9071. 0x0a, 0x00, 0x00, 0x01,
  9072. 0x0a, 0x00, 0x00, 0x02,
  9073. 0x0d, 0x00, 0xe0, 0x00,
  9074. 0x00, 0x00, 0x01, 0x00,
  9075. 0x00, 0x00, 0x02, 0x00,
  9076. 0x80, 0x10, 0x10, 0x00,
  9077. 0x14, 0x09, 0x00, 0x00,
  9078. 0x01, 0x01, 0x08, 0x0a,
  9079. 0x11, 0x11, 0x11, 0x11,
  9080. 0x11, 0x11, 0x11, 0x11,
  9081. };
  9082. static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, int loopback_mode)
  9083. {
  9084. u32 mac_mode, rx_start_idx, rx_idx, tx_idx, opaque_key;
  9085. u32 base_flags = 0, mss = 0, desc_idx, coal_now, data_off, val;
  9086. struct sk_buff *skb, *rx_skb;
  9087. u8 *tx_data;
  9088. dma_addr_t map;
  9089. int num_pkts, tx_len, rx_len, i, err;
  9090. struct tg3_rx_buffer_desc *desc;
  9091. struct tg3_napi *tnapi, *rnapi;
  9092. struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
  9093. tnapi = &tp->napi[0];
  9094. rnapi = &tp->napi[0];
  9095. if (tp->irq_cnt > 1) {
  9096. if (tg3_flag(tp, ENABLE_RSS))
  9097. rnapi = &tp->napi[1];
  9098. if (tg3_flag(tp, ENABLE_TSS))
  9099. tnapi = &tp->napi[1];
  9100. }
  9101. coal_now = tnapi->coal_now | rnapi->coal_now;
  9102. if (loopback_mode == TG3_MAC_LOOPBACK) {
  9103. /* HW errata - mac loopback fails in some cases on 5780.
  9104. * Normal traffic and PHY loopback are not affected by
  9105. * errata. Also, the MAC loopback test is deprecated for
  9106. * all newer ASIC revisions.
  9107. */
  9108. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
  9109. tg3_flag(tp, CPMU_PRESENT))
  9110. return 0;
  9111. mac_mode = tp->mac_mode &
  9112. ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
  9113. mac_mode |= MAC_MODE_PORT_INT_LPBACK;
  9114. if (!tg3_flag(tp, 5705_PLUS))
  9115. mac_mode |= MAC_MODE_LINK_POLARITY;
  9116. if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
  9117. mac_mode |= MAC_MODE_PORT_MODE_MII;
  9118. else
  9119. mac_mode |= MAC_MODE_PORT_MODE_GMII;
  9120. tw32(MAC_MODE, mac_mode);
  9121. } else {
  9122. if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
  9123. tg3_phy_fet_toggle_apd(tp, false);
  9124. val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED100;
  9125. } else
  9126. val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED1000;
  9127. tg3_phy_toggle_automdix(tp, 0);
  9128. tg3_writephy(tp, MII_BMCR, val);
  9129. udelay(40);
  9130. mac_mode = tp->mac_mode &
  9131. ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
  9132. if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
  9133. tg3_writephy(tp, MII_TG3_FET_PTEST,
  9134. MII_TG3_FET_PTEST_FRC_TX_LINK |
  9135. MII_TG3_FET_PTEST_FRC_TX_LOCK);
  9136. /* The write needs to be flushed for the AC131 */
  9137. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
  9138. tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
  9139. mac_mode |= MAC_MODE_PORT_MODE_MII;
  9140. } else
  9141. mac_mode |= MAC_MODE_PORT_MODE_GMII;
  9142. /* reset to prevent losing 1st rx packet intermittently */
  9143. if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
  9144. tw32_f(MAC_RX_MODE, RX_MODE_RESET);
  9145. udelay(10);
  9146. tw32_f(MAC_RX_MODE, tp->rx_mode);
  9147. }
  9148. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
  9149. u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
  9150. if (masked_phy_id == TG3_PHY_ID_BCM5401)
  9151. mac_mode &= ~MAC_MODE_LINK_POLARITY;
  9152. else if (masked_phy_id == TG3_PHY_ID_BCM5411)
  9153. mac_mode |= MAC_MODE_LINK_POLARITY;
  9154. tg3_writephy(tp, MII_TG3_EXT_CTRL,
  9155. MII_TG3_EXT_CTRL_LNK3_LED_MODE);
  9156. }
  9157. tw32(MAC_MODE, mac_mode);
  9158. /* Wait for link */
  9159. for (i = 0; i < 100; i++) {
  9160. if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
  9161. break;
  9162. mdelay(1);
  9163. }
  9164. }
  9165. err = -EIO;
  9166. tx_len = pktsz;
  9167. skb = netdev_alloc_skb(tp->dev, tx_len);
  9168. if (!skb)
  9169. return -ENOMEM;
  9170. tx_data = skb_put(skb, tx_len);
  9171. memcpy(tx_data, tp->dev->dev_addr, 6);
  9172. memset(tx_data + 6, 0x0, 8);
  9173. tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
  9174. if (loopback_mode == TG3_TSO_LOOPBACK) {
  9175. struct iphdr *iph = (struct iphdr *)&tx_data[ETH_HLEN];
  9176. u32 hdr_len = TG3_TSO_IP_HDR_LEN + TG3_TSO_TCP_HDR_LEN +
  9177. TG3_TSO_TCP_OPT_LEN;
  9178. memcpy(tx_data + ETH_ALEN * 2, tg3_tso_header,
  9179. sizeof(tg3_tso_header));
  9180. mss = TG3_TSO_MSS;
  9181. val = tx_len - ETH_ALEN * 2 - sizeof(tg3_tso_header);
  9182. num_pkts = DIV_ROUND_UP(val, TG3_TSO_MSS);
  9183. /* Set the total length field in the IP header */
  9184. iph->tot_len = htons((u16)(mss + hdr_len));
  9185. base_flags = (TXD_FLAG_CPU_PRE_DMA |
  9186. TXD_FLAG_CPU_POST_DMA);
  9187. if (tg3_flag(tp, HW_TSO_1) ||
  9188. tg3_flag(tp, HW_TSO_2) ||
  9189. tg3_flag(tp, HW_TSO_3)) {
  9190. struct tcphdr *th;
  9191. val = ETH_HLEN + TG3_TSO_IP_HDR_LEN;
  9192. th = (struct tcphdr *)&tx_data[val];
  9193. th->check = 0;
  9194. } else
  9195. base_flags |= TXD_FLAG_TCPUDP_CSUM;
  9196. if (tg3_flag(tp, HW_TSO_3)) {
  9197. mss |= (hdr_len & 0xc) << 12;
  9198. if (hdr_len & 0x10)
  9199. base_flags |= 0x00000010;
  9200. base_flags |= (hdr_len & 0x3e0) << 5;
  9201. } else if (tg3_flag(tp, HW_TSO_2))
  9202. mss |= hdr_len << 9;
  9203. else if (tg3_flag(tp, HW_TSO_1) ||
  9204. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
  9205. mss |= (TG3_TSO_TCP_OPT_LEN << 9);
  9206. } else {
  9207. base_flags |= (TG3_TSO_TCP_OPT_LEN << 10);
  9208. }
  9209. data_off = ETH_ALEN * 2 + sizeof(tg3_tso_header);
  9210. } else {
  9211. num_pkts = 1;
  9212. data_off = ETH_HLEN;
  9213. }
  9214. for (i = data_off; i < tx_len; i++)
  9215. tx_data[i] = (u8) (i & 0xff);
  9216. map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
  9217. if (pci_dma_mapping_error(tp->pdev, map)) {
  9218. dev_kfree_skb(skb);
  9219. return -EIO;
  9220. }
  9221. tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
  9222. rnapi->coal_now);
  9223. udelay(10);
  9224. rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
  9225. tg3_set_txd(tnapi, tnapi->tx_prod, map, tx_len,
  9226. base_flags, (mss << 1) | 1);
  9227. tnapi->tx_prod++;
  9228. tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
  9229. tr32_mailbox(tnapi->prodmbox);
  9230. udelay(10);
  9231. /* 350 usec to allow enough time on some 10/100 Mbps devices. */
  9232. for (i = 0; i < 35; i++) {
  9233. tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
  9234. coal_now);
  9235. udelay(10);
  9236. tx_idx = tnapi->hw_status->idx[0].tx_consumer;
  9237. rx_idx = rnapi->hw_status->idx[0].rx_producer;
  9238. if ((tx_idx == tnapi->tx_prod) &&
  9239. (rx_idx == (rx_start_idx + num_pkts)))
  9240. break;
  9241. }
  9242. pci_unmap_single(tp->pdev, map, tx_len, PCI_DMA_TODEVICE);
  9243. dev_kfree_skb(skb);
  9244. if (tx_idx != tnapi->tx_prod)
  9245. goto out;
  9246. if (rx_idx != rx_start_idx + num_pkts)
  9247. goto out;
  9248. val = data_off;
  9249. while (rx_idx != rx_start_idx) {
  9250. desc = &rnapi->rx_rcb[rx_start_idx++];
  9251. desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
  9252. opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
  9253. if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
  9254. (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
  9255. goto out;
  9256. rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT)
  9257. - ETH_FCS_LEN;
  9258. if (loopback_mode != TG3_TSO_LOOPBACK) {
  9259. if (rx_len != tx_len)
  9260. goto out;
  9261. if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
  9262. if (opaque_key != RXD_OPAQUE_RING_STD)
  9263. goto out;
  9264. } else {
  9265. if (opaque_key != RXD_OPAQUE_RING_JUMBO)
  9266. goto out;
  9267. }
  9268. } else if ((desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
  9269. (desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
  9270. >> RXD_TCPCSUM_SHIFT != 0xffff) {
  9271. goto out;
  9272. }
  9273. if (opaque_key == RXD_OPAQUE_RING_STD) {
  9274. rx_skb = tpr->rx_std_buffers[desc_idx].skb;
  9275. map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx],
  9276. mapping);
  9277. } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
  9278. rx_skb = tpr->rx_jmb_buffers[desc_idx].skb;
  9279. map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx],
  9280. mapping);
  9281. } else
  9282. goto out;
  9283. pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len,
  9284. PCI_DMA_FROMDEVICE);
  9285. for (i = data_off; i < rx_len; i++, val++) {
  9286. if (*(rx_skb->data + i) != (u8) (val & 0xff))
  9287. goto out;
  9288. }
  9289. }
  9290. err = 0;
  9291. /* tg3_free_rings will unmap and free the rx_skb */
  9292. out:
  9293. return err;
  9294. }
  9295. #define TG3_STD_LOOPBACK_FAILED 1
  9296. #define TG3_JMB_LOOPBACK_FAILED 2
  9297. #define TG3_TSO_LOOPBACK_FAILED 4
  9298. #define TG3_MAC_LOOPBACK_SHIFT 0
  9299. #define TG3_PHY_LOOPBACK_SHIFT 4
  9300. #define TG3_LOOPBACK_FAILED 0x00000077
  9301. static int tg3_test_loopback(struct tg3 *tp)
  9302. {
  9303. int err = 0;
  9304. u32 eee_cap, cpmuctrl = 0;
  9305. if (!netif_running(tp->dev))
  9306. return TG3_LOOPBACK_FAILED;
  9307. eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
  9308. tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
  9309. err = tg3_reset_hw(tp, 1);
  9310. if (err) {
  9311. err = TG3_LOOPBACK_FAILED;
  9312. goto done;
  9313. }
  9314. if (tg3_flag(tp, ENABLE_RSS)) {
  9315. int i;
  9316. /* Reroute all rx packets to the 1st queue */
  9317. for (i = MAC_RSS_INDIR_TBL_0;
  9318. i < MAC_RSS_INDIR_TBL_0 + TG3_RSS_INDIR_TBL_SIZE; i += 4)
  9319. tw32(i, 0x0);
  9320. }
  9321. /* Turn off gphy autopowerdown. */
  9322. if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
  9323. tg3_phy_toggle_apd(tp, false);
  9324. if (tg3_flag(tp, CPMU_PRESENT)) {
  9325. int i;
  9326. u32 status;
  9327. tw32(TG3_CPMU_MUTEX_REQ, CPMU_MUTEX_REQ_DRIVER);
  9328. /* Wait for up to 40 microseconds to acquire lock. */
  9329. for (i = 0; i < 4; i++) {
  9330. status = tr32(TG3_CPMU_MUTEX_GNT);
  9331. if (status == CPMU_MUTEX_GNT_DRIVER)
  9332. break;
  9333. udelay(10);
  9334. }
  9335. if (status != CPMU_MUTEX_GNT_DRIVER) {
  9336. err = TG3_LOOPBACK_FAILED;
  9337. goto done;
  9338. }
  9339. /* Turn off link-based power management. */
  9340. cpmuctrl = tr32(TG3_CPMU_CTRL);
  9341. tw32(TG3_CPMU_CTRL,
  9342. cpmuctrl & ~(CPMU_CTRL_LINK_SPEED_MODE |
  9343. CPMU_CTRL_LINK_AWARE_MODE));
  9344. }
  9345. if (tg3_run_loopback(tp, ETH_FRAME_LEN, TG3_MAC_LOOPBACK))
  9346. err |= TG3_STD_LOOPBACK_FAILED << TG3_MAC_LOOPBACK_SHIFT;
  9347. if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
  9348. tg3_run_loopback(tp, 9000 + ETH_HLEN, TG3_MAC_LOOPBACK))
  9349. err |= TG3_JMB_LOOPBACK_FAILED << TG3_MAC_LOOPBACK_SHIFT;
  9350. if (tg3_flag(tp, CPMU_PRESENT)) {
  9351. tw32(TG3_CPMU_CTRL, cpmuctrl);
  9352. /* Release the mutex */
  9353. tw32(TG3_CPMU_MUTEX_GNT, CPMU_MUTEX_GNT_DRIVER);
  9354. }
  9355. if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
  9356. !tg3_flag(tp, USE_PHYLIB)) {
  9357. if (tg3_run_loopback(tp, ETH_FRAME_LEN, TG3_PHY_LOOPBACK))
  9358. err |= TG3_STD_LOOPBACK_FAILED <<
  9359. TG3_PHY_LOOPBACK_SHIFT;
  9360. if (tg3_flag(tp, TSO_CAPABLE) &&
  9361. tg3_run_loopback(tp, ETH_FRAME_LEN, TG3_TSO_LOOPBACK))
  9362. err |= TG3_TSO_LOOPBACK_FAILED <<
  9363. TG3_PHY_LOOPBACK_SHIFT;
  9364. if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
  9365. tg3_run_loopback(tp, 9000 + ETH_HLEN, TG3_PHY_LOOPBACK))
  9366. err |= TG3_JMB_LOOPBACK_FAILED <<
  9367. TG3_PHY_LOOPBACK_SHIFT;
  9368. }
  9369. /* Re-enable gphy autopowerdown. */
  9370. if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
  9371. tg3_phy_toggle_apd(tp, true);
  9372. done:
  9373. tp->phy_flags |= eee_cap;
  9374. return err;
  9375. }
  9376. static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
  9377. u64 *data)
  9378. {
  9379. struct tg3 *tp = netdev_priv(dev);
  9380. if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
  9381. tg3_power_up(tp);
  9382. memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
  9383. if (tg3_test_nvram(tp) != 0) {
  9384. etest->flags |= ETH_TEST_FL_FAILED;
  9385. data[0] = 1;
  9386. }
  9387. if (tg3_test_link(tp) != 0) {
  9388. etest->flags |= ETH_TEST_FL_FAILED;
  9389. data[1] = 1;
  9390. }
  9391. if (etest->flags & ETH_TEST_FL_OFFLINE) {
  9392. int err, err2 = 0, irq_sync = 0;
  9393. if (netif_running(dev)) {
  9394. tg3_phy_stop(tp);
  9395. tg3_netif_stop(tp);
  9396. irq_sync = 1;
  9397. }
  9398. tg3_full_lock(tp, irq_sync);
  9399. tg3_halt(tp, RESET_KIND_SUSPEND, 1);
  9400. err = tg3_nvram_lock(tp);
  9401. tg3_halt_cpu(tp, RX_CPU_BASE);
  9402. if (!tg3_flag(tp, 5705_PLUS))
  9403. tg3_halt_cpu(tp, TX_CPU_BASE);
  9404. if (!err)
  9405. tg3_nvram_unlock(tp);
  9406. if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
  9407. tg3_phy_reset(tp);
  9408. if (tg3_test_registers(tp) != 0) {
  9409. etest->flags |= ETH_TEST_FL_FAILED;
  9410. data[2] = 1;
  9411. }
  9412. if (tg3_test_memory(tp) != 0) {
  9413. etest->flags |= ETH_TEST_FL_FAILED;
  9414. data[3] = 1;
  9415. }
  9416. if ((data[4] = tg3_test_loopback(tp)) != 0)
  9417. etest->flags |= ETH_TEST_FL_FAILED;
  9418. tg3_full_unlock(tp);
  9419. if (tg3_test_interrupt(tp) != 0) {
  9420. etest->flags |= ETH_TEST_FL_FAILED;
  9421. data[5] = 1;
  9422. }
  9423. tg3_full_lock(tp, 0);
  9424. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  9425. if (netif_running(dev)) {
  9426. tg3_flag_set(tp, INIT_COMPLETE);
  9427. err2 = tg3_restart_hw(tp, 1);
  9428. if (!err2)
  9429. tg3_netif_start(tp);
  9430. }
  9431. tg3_full_unlock(tp);
  9432. if (irq_sync && !err2)
  9433. tg3_phy_start(tp);
  9434. }
  9435. if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
  9436. tg3_power_down(tp);
  9437. }
  9438. static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  9439. {
  9440. struct mii_ioctl_data *data = if_mii(ifr);
  9441. struct tg3 *tp = netdev_priv(dev);
  9442. int err;
  9443. if (tg3_flag(tp, USE_PHYLIB)) {
  9444. struct phy_device *phydev;
  9445. if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
  9446. return -EAGAIN;
  9447. phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
  9448. return phy_mii_ioctl(phydev, ifr, cmd);
  9449. }
  9450. switch (cmd) {
  9451. case SIOCGMIIPHY:
  9452. data->phy_id = tp->phy_addr;
  9453. /* fallthru */
  9454. case SIOCGMIIREG: {
  9455. u32 mii_regval;
  9456. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
  9457. break; /* We have no PHY */
  9458. if (!netif_running(dev))
  9459. return -EAGAIN;
  9460. spin_lock_bh(&tp->lock);
  9461. err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
  9462. spin_unlock_bh(&tp->lock);
  9463. data->val_out = mii_regval;
  9464. return err;
  9465. }
  9466. case SIOCSMIIREG:
  9467. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
  9468. break; /* We have no PHY */
  9469. if (!netif_running(dev))
  9470. return -EAGAIN;
  9471. spin_lock_bh(&tp->lock);
  9472. err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
  9473. spin_unlock_bh(&tp->lock);
  9474. return err;
  9475. default:
  9476. /* do nothing */
  9477. break;
  9478. }
  9479. return -EOPNOTSUPP;
  9480. }
  9481. static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
  9482. {
  9483. struct tg3 *tp = netdev_priv(dev);
  9484. memcpy(ec, &tp->coal, sizeof(*ec));
  9485. return 0;
  9486. }
  9487. static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
  9488. {
  9489. struct tg3 *tp = netdev_priv(dev);
  9490. u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
  9491. u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
  9492. if (!tg3_flag(tp, 5705_PLUS)) {
  9493. max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
  9494. max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
  9495. max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
  9496. min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
  9497. }
  9498. if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
  9499. (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
  9500. (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
  9501. (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
  9502. (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
  9503. (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
  9504. (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
  9505. (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
  9506. (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
  9507. (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
  9508. return -EINVAL;
  9509. /* No rx interrupts will be generated if both are zero */
  9510. if ((ec->rx_coalesce_usecs == 0) &&
  9511. (ec->rx_max_coalesced_frames == 0))
  9512. return -EINVAL;
  9513. /* No tx interrupts will be generated if both are zero */
  9514. if ((ec->tx_coalesce_usecs == 0) &&
  9515. (ec->tx_max_coalesced_frames == 0))
  9516. return -EINVAL;
  9517. /* Only copy relevant parameters, ignore all others. */
  9518. tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
  9519. tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
  9520. tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
  9521. tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
  9522. tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
  9523. tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
  9524. tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
  9525. tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
  9526. tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
  9527. if (netif_running(dev)) {
  9528. tg3_full_lock(tp, 0);
  9529. __tg3_set_coalesce(tp, &tp->coal);
  9530. tg3_full_unlock(tp);
  9531. }
  9532. return 0;
  9533. }
  9534. static const struct ethtool_ops tg3_ethtool_ops = {
  9535. .get_settings = tg3_get_settings,
  9536. .set_settings = tg3_set_settings,
  9537. .get_drvinfo = tg3_get_drvinfo,
  9538. .get_regs_len = tg3_get_regs_len,
  9539. .get_regs = tg3_get_regs,
  9540. .get_wol = tg3_get_wol,
  9541. .set_wol = tg3_set_wol,
  9542. .get_msglevel = tg3_get_msglevel,
  9543. .set_msglevel = tg3_set_msglevel,
  9544. .nway_reset = tg3_nway_reset,
  9545. .get_link = ethtool_op_get_link,
  9546. .get_eeprom_len = tg3_get_eeprom_len,
  9547. .get_eeprom = tg3_get_eeprom,
  9548. .set_eeprom = tg3_set_eeprom,
  9549. .get_ringparam = tg3_get_ringparam,
  9550. .set_ringparam = tg3_set_ringparam,
  9551. .get_pauseparam = tg3_get_pauseparam,
  9552. .set_pauseparam = tg3_set_pauseparam,
  9553. .self_test = tg3_self_test,
  9554. .get_strings = tg3_get_strings,
  9555. .set_phys_id = tg3_set_phys_id,
  9556. .get_ethtool_stats = tg3_get_ethtool_stats,
  9557. .get_coalesce = tg3_get_coalesce,
  9558. .set_coalesce = tg3_set_coalesce,
  9559. .get_sset_count = tg3_get_sset_count,
  9560. };
  9561. static void __devinit tg3_get_eeprom_size(struct tg3 *tp)
  9562. {
  9563. u32 cursize, val, magic;
  9564. tp->nvram_size = EEPROM_CHIP_SIZE;
  9565. if (tg3_nvram_read(tp, 0, &magic) != 0)
  9566. return;
  9567. if ((magic != TG3_EEPROM_MAGIC) &&
  9568. ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
  9569. ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
  9570. return;
  9571. /*
  9572. * Size the chip by reading offsets at increasing powers of two.
  9573. * When we encounter our validation signature, we know the addressing
  9574. * has wrapped around, and thus have our chip size.
  9575. */
  9576. cursize = 0x10;
  9577. while (cursize < tp->nvram_size) {
  9578. if (tg3_nvram_read(tp, cursize, &val) != 0)
  9579. return;
  9580. if (val == magic)
  9581. break;
  9582. cursize <<= 1;
  9583. }
  9584. tp->nvram_size = cursize;
  9585. }
  9586. static void __devinit tg3_get_nvram_size(struct tg3 *tp)
  9587. {
  9588. u32 val;
  9589. if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &val) != 0)
  9590. return;
  9591. /* Selfboot format */
  9592. if (val != TG3_EEPROM_MAGIC) {
  9593. tg3_get_eeprom_size(tp);
  9594. return;
  9595. }
  9596. if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
  9597. if (val != 0) {
  9598. /* This is confusing. We want to operate on the
  9599. * 16-bit value at offset 0xf2. The tg3_nvram_read()
  9600. * call will read from NVRAM and byteswap the data
  9601. * according to the byteswapping settings for all
  9602. * other register accesses. This ensures the data we
  9603. * want will always reside in the lower 16-bits.
  9604. * However, the data in NVRAM is in LE format, which
  9605. * means the data from the NVRAM read will always be
  9606. * opposite the endianness of the CPU. The 16-bit
  9607. * byteswap then brings the data to CPU endianness.
  9608. */
  9609. tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
  9610. return;
  9611. }
  9612. }
  9613. tp->nvram_size = TG3_NVRAM_SIZE_512KB;
  9614. }
  9615. static void __devinit tg3_get_nvram_info(struct tg3 *tp)
  9616. {
  9617. u32 nvcfg1;
  9618. nvcfg1 = tr32(NVRAM_CFG1);
  9619. if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
  9620. tg3_flag_set(tp, FLASH);
  9621. } else {
  9622. nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
  9623. tw32(NVRAM_CFG1, nvcfg1);
  9624. }
  9625. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
  9626. tg3_flag(tp, 5780_CLASS)) {
  9627. switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
  9628. case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
  9629. tp->nvram_jedecnum = JEDEC_ATMEL;
  9630. tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
  9631. tg3_flag_set(tp, NVRAM_BUFFERED);
  9632. break;
  9633. case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
  9634. tp->nvram_jedecnum = JEDEC_ATMEL;
  9635. tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
  9636. break;
  9637. case FLASH_VENDOR_ATMEL_EEPROM:
  9638. tp->nvram_jedecnum = JEDEC_ATMEL;
  9639. tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
  9640. tg3_flag_set(tp, NVRAM_BUFFERED);
  9641. break;
  9642. case FLASH_VENDOR_ST:
  9643. tp->nvram_jedecnum = JEDEC_ST;
  9644. tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
  9645. tg3_flag_set(tp, NVRAM_BUFFERED);
  9646. break;
  9647. case FLASH_VENDOR_SAIFUN:
  9648. tp->nvram_jedecnum = JEDEC_SAIFUN;
  9649. tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
  9650. break;
  9651. case FLASH_VENDOR_SST_SMALL:
  9652. case FLASH_VENDOR_SST_LARGE:
  9653. tp->nvram_jedecnum = JEDEC_SST;
  9654. tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
  9655. break;
  9656. }
  9657. } else {
  9658. tp->nvram_jedecnum = JEDEC_ATMEL;
  9659. tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
  9660. tg3_flag_set(tp, NVRAM_BUFFERED);
  9661. }
  9662. }
  9663. static void __devinit tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
  9664. {
  9665. switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
  9666. case FLASH_5752PAGE_SIZE_256:
  9667. tp->nvram_pagesize = 256;
  9668. break;
  9669. case FLASH_5752PAGE_SIZE_512:
  9670. tp->nvram_pagesize = 512;
  9671. break;
  9672. case FLASH_5752PAGE_SIZE_1K:
  9673. tp->nvram_pagesize = 1024;
  9674. break;
  9675. case FLASH_5752PAGE_SIZE_2K:
  9676. tp->nvram_pagesize = 2048;
  9677. break;
  9678. case FLASH_5752PAGE_SIZE_4K:
  9679. tp->nvram_pagesize = 4096;
  9680. break;
  9681. case FLASH_5752PAGE_SIZE_264:
  9682. tp->nvram_pagesize = 264;
  9683. break;
  9684. case FLASH_5752PAGE_SIZE_528:
  9685. tp->nvram_pagesize = 528;
  9686. break;
  9687. }
  9688. }
  9689. static void __devinit tg3_get_5752_nvram_info(struct tg3 *tp)
  9690. {
  9691. u32 nvcfg1;
  9692. nvcfg1 = tr32(NVRAM_CFG1);
  9693. /* NVRAM protection for TPM */
  9694. if (nvcfg1 & (1 << 27))
  9695. tg3_flag_set(tp, PROTECTED_NVRAM);
  9696. switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
  9697. case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
  9698. case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
  9699. tp->nvram_jedecnum = JEDEC_ATMEL;
  9700. tg3_flag_set(tp, NVRAM_BUFFERED);
  9701. break;
  9702. case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
  9703. tp->nvram_jedecnum = JEDEC_ATMEL;
  9704. tg3_flag_set(tp, NVRAM_BUFFERED);
  9705. tg3_flag_set(tp, FLASH);
  9706. break;
  9707. case FLASH_5752VENDOR_ST_M45PE10:
  9708. case FLASH_5752VENDOR_ST_M45PE20:
  9709. case FLASH_5752VENDOR_ST_M45PE40:
  9710. tp->nvram_jedecnum = JEDEC_ST;
  9711. tg3_flag_set(tp, NVRAM_BUFFERED);
  9712. tg3_flag_set(tp, FLASH);
  9713. break;
  9714. }
  9715. if (tg3_flag(tp, FLASH)) {
  9716. tg3_nvram_get_pagesize(tp, nvcfg1);
  9717. } else {
  9718. /* For eeprom, set pagesize to maximum eeprom size */
  9719. tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
  9720. nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
  9721. tw32(NVRAM_CFG1, nvcfg1);
  9722. }
  9723. }
  9724. static void __devinit tg3_get_5755_nvram_info(struct tg3 *tp)
  9725. {
  9726. u32 nvcfg1, protect = 0;
  9727. nvcfg1 = tr32(NVRAM_CFG1);
  9728. /* NVRAM protection for TPM */
  9729. if (nvcfg1 & (1 << 27)) {
  9730. tg3_flag_set(tp, PROTECTED_NVRAM);
  9731. protect = 1;
  9732. }
  9733. nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
  9734. switch (nvcfg1) {
  9735. case FLASH_5755VENDOR_ATMEL_FLASH_1:
  9736. case FLASH_5755VENDOR_ATMEL_FLASH_2:
  9737. case FLASH_5755VENDOR_ATMEL_FLASH_3:
  9738. case FLASH_5755VENDOR_ATMEL_FLASH_5:
  9739. tp->nvram_jedecnum = JEDEC_ATMEL;
  9740. tg3_flag_set(tp, NVRAM_BUFFERED);
  9741. tg3_flag_set(tp, FLASH);
  9742. tp->nvram_pagesize = 264;
  9743. if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
  9744. nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
  9745. tp->nvram_size = (protect ? 0x3e200 :
  9746. TG3_NVRAM_SIZE_512KB);
  9747. else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
  9748. tp->nvram_size = (protect ? 0x1f200 :
  9749. TG3_NVRAM_SIZE_256KB);
  9750. else
  9751. tp->nvram_size = (protect ? 0x1f200 :
  9752. TG3_NVRAM_SIZE_128KB);
  9753. break;
  9754. case FLASH_5752VENDOR_ST_M45PE10:
  9755. case FLASH_5752VENDOR_ST_M45PE20:
  9756. case FLASH_5752VENDOR_ST_M45PE40:
  9757. tp->nvram_jedecnum = JEDEC_ST;
  9758. tg3_flag_set(tp, NVRAM_BUFFERED);
  9759. tg3_flag_set(tp, FLASH);
  9760. tp->nvram_pagesize = 256;
  9761. if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
  9762. tp->nvram_size = (protect ?
  9763. TG3_NVRAM_SIZE_64KB :
  9764. TG3_NVRAM_SIZE_128KB);
  9765. else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
  9766. tp->nvram_size = (protect ?
  9767. TG3_NVRAM_SIZE_64KB :
  9768. TG3_NVRAM_SIZE_256KB);
  9769. else
  9770. tp->nvram_size = (protect ?
  9771. TG3_NVRAM_SIZE_128KB :
  9772. TG3_NVRAM_SIZE_512KB);
  9773. break;
  9774. }
  9775. }
  9776. static void __devinit tg3_get_5787_nvram_info(struct tg3 *tp)
  9777. {
  9778. u32 nvcfg1;
  9779. nvcfg1 = tr32(NVRAM_CFG1);
  9780. switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
  9781. case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
  9782. case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
  9783. case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
  9784. case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
  9785. tp->nvram_jedecnum = JEDEC_ATMEL;
  9786. tg3_flag_set(tp, NVRAM_BUFFERED);
  9787. tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
  9788. nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
  9789. tw32(NVRAM_CFG1, nvcfg1);
  9790. break;
  9791. case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
  9792. case FLASH_5755VENDOR_ATMEL_FLASH_1:
  9793. case FLASH_5755VENDOR_ATMEL_FLASH_2:
  9794. case FLASH_5755VENDOR_ATMEL_FLASH_3:
  9795. tp->nvram_jedecnum = JEDEC_ATMEL;
  9796. tg3_flag_set(tp, NVRAM_BUFFERED);
  9797. tg3_flag_set(tp, FLASH);
  9798. tp->nvram_pagesize = 264;
  9799. break;
  9800. case FLASH_5752VENDOR_ST_M45PE10:
  9801. case FLASH_5752VENDOR_ST_M45PE20:
  9802. case FLASH_5752VENDOR_ST_M45PE40:
  9803. tp->nvram_jedecnum = JEDEC_ST;
  9804. tg3_flag_set(tp, NVRAM_BUFFERED);
  9805. tg3_flag_set(tp, FLASH);
  9806. tp->nvram_pagesize = 256;
  9807. break;
  9808. }
  9809. }
  9810. static void __devinit tg3_get_5761_nvram_info(struct tg3 *tp)
  9811. {
  9812. u32 nvcfg1, protect = 0;
  9813. nvcfg1 = tr32(NVRAM_CFG1);
  9814. /* NVRAM protection for TPM */
  9815. if (nvcfg1 & (1 << 27)) {
  9816. tg3_flag_set(tp, PROTECTED_NVRAM);
  9817. protect = 1;
  9818. }
  9819. nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
  9820. switch (nvcfg1) {
  9821. case FLASH_5761VENDOR_ATMEL_ADB021D:
  9822. case FLASH_5761VENDOR_ATMEL_ADB041D:
  9823. case FLASH_5761VENDOR_ATMEL_ADB081D:
  9824. case FLASH_5761VENDOR_ATMEL_ADB161D:
  9825. case FLASH_5761VENDOR_ATMEL_MDB021D:
  9826. case FLASH_5761VENDOR_ATMEL_MDB041D:
  9827. case FLASH_5761VENDOR_ATMEL_MDB081D:
  9828. case FLASH_5761VENDOR_ATMEL_MDB161D:
  9829. tp->nvram_jedecnum = JEDEC_ATMEL;
  9830. tg3_flag_set(tp, NVRAM_BUFFERED);
  9831. tg3_flag_set(tp, FLASH);
  9832. tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
  9833. tp->nvram_pagesize = 256;
  9834. break;
  9835. case FLASH_5761VENDOR_ST_A_M45PE20:
  9836. case FLASH_5761VENDOR_ST_A_M45PE40:
  9837. case FLASH_5761VENDOR_ST_A_M45PE80:
  9838. case FLASH_5761VENDOR_ST_A_M45PE16:
  9839. case FLASH_5761VENDOR_ST_M_M45PE20:
  9840. case FLASH_5761VENDOR_ST_M_M45PE40:
  9841. case FLASH_5761VENDOR_ST_M_M45PE80:
  9842. case FLASH_5761VENDOR_ST_M_M45PE16:
  9843. tp->nvram_jedecnum = JEDEC_ST;
  9844. tg3_flag_set(tp, NVRAM_BUFFERED);
  9845. tg3_flag_set(tp, FLASH);
  9846. tp->nvram_pagesize = 256;
  9847. break;
  9848. }
  9849. if (protect) {
  9850. tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
  9851. } else {
  9852. switch (nvcfg1) {
  9853. case FLASH_5761VENDOR_ATMEL_ADB161D:
  9854. case FLASH_5761VENDOR_ATMEL_MDB161D:
  9855. case FLASH_5761VENDOR_ST_A_M45PE16:
  9856. case FLASH_5761VENDOR_ST_M_M45PE16:
  9857. tp->nvram_size = TG3_NVRAM_SIZE_2MB;
  9858. break;
  9859. case FLASH_5761VENDOR_ATMEL_ADB081D:
  9860. case FLASH_5761VENDOR_ATMEL_MDB081D:
  9861. case FLASH_5761VENDOR_ST_A_M45PE80:
  9862. case FLASH_5761VENDOR_ST_M_M45PE80:
  9863. tp->nvram_size = TG3_NVRAM_SIZE_1MB;
  9864. break;
  9865. case FLASH_5761VENDOR_ATMEL_ADB041D:
  9866. case FLASH_5761VENDOR_ATMEL_MDB041D:
  9867. case FLASH_5761VENDOR_ST_A_M45PE40:
  9868. case FLASH_5761VENDOR_ST_M_M45PE40:
  9869. tp->nvram_size = TG3_NVRAM_SIZE_512KB;
  9870. break;
  9871. case FLASH_5761VENDOR_ATMEL_ADB021D:
  9872. case FLASH_5761VENDOR_ATMEL_MDB021D:
  9873. case FLASH_5761VENDOR_ST_A_M45PE20:
  9874. case FLASH_5761VENDOR_ST_M_M45PE20:
  9875. tp->nvram_size = TG3_NVRAM_SIZE_256KB;
  9876. break;
  9877. }
  9878. }
  9879. }
  9880. static void __devinit tg3_get_5906_nvram_info(struct tg3 *tp)
  9881. {
  9882. tp->nvram_jedecnum = JEDEC_ATMEL;
  9883. tg3_flag_set(tp, NVRAM_BUFFERED);
  9884. tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
  9885. }
  9886. static void __devinit tg3_get_57780_nvram_info(struct tg3 *tp)
  9887. {
  9888. u32 nvcfg1;
  9889. nvcfg1 = tr32(NVRAM_CFG1);
  9890. switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
  9891. case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
  9892. case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
  9893. tp->nvram_jedecnum = JEDEC_ATMEL;
  9894. tg3_flag_set(tp, NVRAM_BUFFERED);
  9895. tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
  9896. nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
  9897. tw32(NVRAM_CFG1, nvcfg1);
  9898. return;
  9899. case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
  9900. case FLASH_57780VENDOR_ATMEL_AT45DB011D:
  9901. case FLASH_57780VENDOR_ATMEL_AT45DB011B:
  9902. case FLASH_57780VENDOR_ATMEL_AT45DB021D:
  9903. case FLASH_57780VENDOR_ATMEL_AT45DB021B:
  9904. case FLASH_57780VENDOR_ATMEL_AT45DB041D:
  9905. case FLASH_57780VENDOR_ATMEL_AT45DB041B:
  9906. tp->nvram_jedecnum = JEDEC_ATMEL;
  9907. tg3_flag_set(tp, NVRAM_BUFFERED);
  9908. tg3_flag_set(tp, FLASH);
  9909. switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
  9910. case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
  9911. case FLASH_57780VENDOR_ATMEL_AT45DB011D:
  9912. case FLASH_57780VENDOR_ATMEL_AT45DB011B:
  9913. tp->nvram_size = TG3_NVRAM_SIZE_128KB;
  9914. break;
  9915. case FLASH_57780VENDOR_ATMEL_AT45DB021D:
  9916. case FLASH_57780VENDOR_ATMEL_AT45DB021B:
  9917. tp->nvram_size = TG3_NVRAM_SIZE_256KB;
  9918. break;
  9919. case FLASH_57780VENDOR_ATMEL_AT45DB041D:
  9920. case FLASH_57780VENDOR_ATMEL_AT45DB041B:
  9921. tp->nvram_size = TG3_NVRAM_SIZE_512KB;
  9922. break;
  9923. }
  9924. break;
  9925. case FLASH_5752VENDOR_ST_M45PE10:
  9926. case FLASH_5752VENDOR_ST_M45PE20:
  9927. case FLASH_5752VENDOR_ST_M45PE40:
  9928. tp->nvram_jedecnum = JEDEC_ST;
  9929. tg3_flag_set(tp, NVRAM_BUFFERED);
  9930. tg3_flag_set(tp, FLASH);
  9931. switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
  9932. case FLASH_5752VENDOR_ST_M45PE10:
  9933. tp->nvram_size = TG3_NVRAM_SIZE_128KB;
  9934. break;
  9935. case FLASH_5752VENDOR_ST_M45PE20:
  9936. tp->nvram_size = TG3_NVRAM_SIZE_256KB;
  9937. break;
  9938. case FLASH_5752VENDOR_ST_M45PE40:
  9939. tp->nvram_size = TG3_NVRAM_SIZE_512KB;
  9940. break;
  9941. }
  9942. break;
  9943. default:
  9944. tg3_flag_set(tp, NO_NVRAM);
  9945. return;
  9946. }
  9947. tg3_nvram_get_pagesize(tp, nvcfg1);
  9948. if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
  9949. tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
  9950. }
  9951. static void __devinit tg3_get_5717_nvram_info(struct tg3 *tp)
  9952. {
  9953. u32 nvcfg1;
  9954. nvcfg1 = tr32(NVRAM_CFG1);
  9955. switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
  9956. case FLASH_5717VENDOR_ATMEL_EEPROM:
  9957. case FLASH_5717VENDOR_MICRO_EEPROM:
  9958. tp->nvram_jedecnum = JEDEC_ATMEL;
  9959. tg3_flag_set(tp, NVRAM_BUFFERED);
  9960. tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
  9961. nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
  9962. tw32(NVRAM_CFG1, nvcfg1);
  9963. return;
  9964. case FLASH_5717VENDOR_ATMEL_MDB011D:
  9965. case FLASH_5717VENDOR_ATMEL_ADB011B:
  9966. case FLASH_5717VENDOR_ATMEL_ADB011D:
  9967. case FLASH_5717VENDOR_ATMEL_MDB021D:
  9968. case FLASH_5717VENDOR_ATMEL_ADB021B:
  9969. case FLASH_5717VENDOR_ATMEL_ADB021D:
  9970. case FLASH_5717VENDOR_ATMEL_45USPT:
  9971. tp->nvram_jedecnum = JEDEC_ATMEL;
  9972. tg3_flag_set(tp, NVRAM_BUFFERED);
  9973. tg3_flag_set(tp, FLASH);
  9974. switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
  9975. case FLASH_5717VENDOR_ATMEL_MDB021D:
  9976. /* Detect size with tg3_nvram_get_size() */
  9977. break;
  9978. case FLASH_5717VENDOR_ATMEL_ADB021B:
  9979. case FLASH_5717VENDOR_ATMEL_ADB021D:
  9980. tp->nvram_size = TG3_NVRAM_SIZE_256KB;
  9981. break;
  9982. default:
  9983. tp->nvram_size = TG3_NVRAM_SIZE_128KB;
  9984. break;
  9985. }
  9986. break;
  9987. case FLASH_5717VENDOR_ST_M_M25PE10:
  9988. case FLASH_5717VENDOR_ST_A_M25PE10:
  9989. case FLASH_5717VENDOR_ST_M_M45PE10:
  9990. case FLASH_5717VENDOR_ST_A_M45PE10:
  9991. case FLASH_5717VENDOR_ST_M_M25PE20:
  9992. case FLASH_5717VENDOR_ST_A_M25PE20:
  9993. case FLASH_5717VENDOR_ST_M_M45PE20:
  9994. case FLASH_5717VENDOR_ST_A_M45PE20:
  9995. case FLASH_5717VENDOR_ST_25USPT:
  9996. case FLASH_5717VENDOR_ST_45USPT:
  9997. tp->nvram_jedecnum = JEDEC_ST;
  9998. tg3_flag_set(tp, NVRAM_BUFFERED);
  9999. tg3_flag_set(tp, FLASH);
  10000. switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
  10001. case FLASH_5717VENDOR_ST_M_M25PE20:
  10002. case FLASH_5717VENDOR_ST_M_M45PE20:
  10003. /* Detect size with tg3_nvram_get_size() */
  10004. break;
  10005. case FLASH_5717VENDOR_ST_A_M25PE20:
  10006. case FLASH_5717VENDOR_ST_A_M45PE20:
  10007. tp->nvram_size = TG3_NVRAM_SIZE_256KB;
  10008. break;
  10009. default:
  10010. tp->nvram_size = TG3_NVRAM_SIZE_128KB;
  10011. break;
  10012. }
  10013. break;
  10014. default:
  10015. tg3_flag_set(tp, NO_NVRAM);
  10016. return;
  10017. }
  10018. tg3_nvram_get_pagesize(tp, nvcfg1);
  10019. if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
  10020. tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
  10021. }
  10022. static void __devinit tg3_get_5720_nvram_info(struct tg3 *tp)
  10023. {
  10024. u32 nvcfg1, nvmpinstrp;
  10025. nvcfg1 = tr32(NVRAM_CFG1);
  10026. nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
  10027. switch (nvmpinstrp) {
  10028. case FLASH_5720_EEPROM_HD:
  10029. case FLASH_5720_EEPROM_LD:
  10030. tp->nvram_jedecnum = JEDEC_ATMEL;
  10031. tg3_flag_set(tp, NVRAM_BUFFERED);
  10032. nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
  10033. tw32(NVRAM_CFG1, nvcfg1);
  10034. if (nvmpinstrp == FLASH_5720_EEPROM_HD)
  10035. tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
  10036. else
  10037. tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
  10038. return;
  10039. case FLASH_5720VENDOR_M_ATMEL_DB011D:
  10040. case FLASH_5720VENDOR_A_ATMEL_DB011B:
  10041. case FLASH_5720VENDOR_A_ATMEL_DB011D:
  10042. case FLASH_5720VENDOR_M_ATMEL_DB021D:
  10043. case FLASH_5720VENDOR_A_ATMEL_DB021B:
  10044. case FLASH_5720VENDOR_A_ATMEL_DB021D:
  10045. case FLASH_5720VENDOR_M_ATMEL_DB041D:
  10046. case FLASH_5720VENDOR_A_ATMEL_DB041B:
  10047. case FLASH_5720VENDOR_A_ATMEL_DB041D:
  10048. case FLASH_5720VENDOR_M_ATMEL_DB081D:
  10049. case FLASH_5720VENDOR_A_ATMEL_DB081D:
  10050. case FLASH_5720VENDOR_ATMEL_45USPT:
  10051. tp->nvram_jedecnum = JEDEC_ATMEL;
  10052. tg3_flag_set(tp, NVRAM_BUFFERED);
  10053. tg3_flag_set(tp, FLASH);
  10054. switch (nvmpinstrp) {
  10055. case FLASH_5720VENDOR_M_ATMEL_DB021D:
  10056. case FLASH_5720VENDOR_A_ATMEL_DB021B:
  10057. case FLASH_5720VENDOR_A_ATMEL_DB021D:
  10058. tp->nvram_size = TG3_NVRAM_SIZE_256KB;
  10059. break;
  10060. case FLASH_5720VENDOR_M_ATMEL_DB041D:
  10061. case FLASH_5720VENDOR_A_ATMEL_DB041B:
  10062. case FLASH_5720VENDOR_A_ATMEL_DB041D:
  10063. tp->nvram_size = TG3_NVRAM_SIZE_512KB;
  10064. break;
  10065. case FLASH_5720VENDOR_M_ATMEL_DB081D:
  10066. case FLASH_5720VENDOR_A_ATMEL_DB081D:
  10067. tp->nvram_size = TG3_NVRAM_SIZE_1MB;
  10068. break;
  10069. default:
  10070. tp->nvram_size = TG3_NVRAM_SIZE_128KB;
  10071. break;
  10072. }
  10073. break;
  10074. case FLASH_5720VENDOR_M_ST_M25PE10:
  10075. case FLASH_5720VENDOR_M_ST_M45PE10:
  10076. case FLASH_5720VENDOR_A_ST_M25PE10:
  10077. case FLASH_5720VENDOR_A_ST_M45PE10:
  10078. case FLASH_5720VENDOR_M_ST_M25PE20:
  10079. case FLASH_5720VENDOR_M_ST_M45PE20:
  10080. case FLASH_5720VENDOR_A_ST_M25PE20:
  10081. case FLASH_5720VENDOR_A_ST_M45PE20:
  10082. case FLASH_5720VENDOR_M_ST_M25PE40:
  10083. case FLASH_5720VENDOR_M_ST_M45PE40:
  10084. case FLASH_5720VENDOR_A_ST_M25PE40:
  10085. case FLASH_5720VENDOR_A_ST_M45PE40:
  10086. case FLASH_5720VENDOR_M_ST_M25PE80:
  10087. case FLASH_5720VENDOR_M_ST_M45PE80:
  10088. case FLASH_5720VENDOR_A_ST_M25PE80:
  10089. case FLASH_5720VENDOR_A_ST_M45PE80:
  10090. case FLASH_5720VENDOR_ST_25USPT:
  10091. case FLASH_5720VENDOR_ST_45USPT:
  10092. tp->nvram_jedecnum = JEDEC_ST;
  10093. tg3_flag_set(tp, NVRAM_BUFFERED);
  10094. tg3_flag_set(tp, FLASH);
  10095. switch (nvmpinstrp) {
  10096. case FLASH_5720VENDOR_M_ST_M25PE20:
  10097. case FLASH_5720VENDOR_M_ST_M45PE20:
  10098. case FLASH_5720VENDOR_A_ST_M25PE20:
  10099. case FLASH_5720VENDOR_A_ST_M45PE20:
  10100. tp->nvram_size = TG3_NVRAM_SIZE_256KB;
  10101. break;
  10102. case FLASH_5720VENDOR_M_ST_M25PE40:
  10103. case FLASH_5720VENDOR_M_ST_M45PE40:
  10104. case FLASH_5720VENDOR_A_ST_M25PE40:
  10105. case FLASH_5720VENDOR_A_ST_M45PE40:
  10106. tp->nvram_size = TG3_NVRAM_SIZE_512KB;
  10107. break;
  10108. case FLASH_5720VENDOR_M_ST_M25PE80:
  10109. case FLASH_5720VENDOR_M_ST_M45PE80:
  10110. case FLASH_5720VENDOR_A_ST_M25PE80:
  10111. case FLASH_5720VENDOR_A_ST_M45PE80:
  10112. tp->nvram_size = TG3_NVRAM_SIZE_1MB;
  10113. break;
  10114. default:
  10115. tp->nvram_size = TG3_NVRAM_SIZE_128KB;
  10116. break;
  10117. }
  10118. break;
  10119. default:
  10120. tg3_flag_set(tp, NO_NVRAM);
  10121. return;
  10122. }
  10123. tg3_nvram_get_pagesize(tp, nvcfg1);
  10124. if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
  10125. tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
  10126. }
  10127. /* Chips other than 5700/5701 use the NVRAM for fetching info. */
  10128. static void __devinit tg3_nvram_init(struct tg3 *tp)
  10129. {
  10130. tw32_f(GRC_EEPROM_ADDR,
  10131. (EEPROM_ADDR_FSM_RESET |
  10132. (EEPROM_DEFAULT_CLOCK_PERIOD <<
  10133. EEPROM_ADDR_CLKPERD_SHIFT)));
  10134. msleep(1);
  10135. /* Enable seeprom accesses. */
  10136. tw32_f(GRC_LOCAL_CTRL,
  10137. tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
  10138. udelay(100);
  10139. if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
  10140. GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
  10141. tg3_flag_set(tp, NVRAM);
  10142. if (tg3_nvram_lock(tp)) {
  10143. netdev_warn(tp->dev,
  10144. "Cannot get nvram lock, %s failed\n",
  10145. __func__);
  10146. return;
  10147. }
  10148. tg3_enable_nvram_access(tp);
  10149. tp->nvram_size = 0;
  10150. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
  10151. tg3_get_5752_nvram_info(tp);
  10152. else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
  10153. tg3_get_5755_nvram_info(tp);
  10154. else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
  10155. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
  10156. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
  10157. tg3_get_5787_nvram_info(tp);
  10158. else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
  10159. tg3_get_5761_nvram_info(tp);
  10160. else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
  10161. tg3_get_5906_nvram_info(tp);
  10162. else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
  10163. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
  10164. tg3_get_57780_nvram_info(tp);
  10165. else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
  10166. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
  10167. tg3_get_5717_nvram_info(tp);
  10168. else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
  10169. tg3_get_5720_nvram_info(tp);
  10170. else
  10171. tg3_get_nvram_info(tp);
  10172. if (tp->nvram_size == 0)
  10173. tg3_get_nvram_size(tp);
  10174. tg3_disable_nvram_access(tp);
  10175. tg3_nvram_unlock(tp);
  10176. } else {
  10177. tg3_flag_clear(tp, NVRAM);
  10178. tg3_flag_clear(tp, NVRAM_BUFFERED);
  10179. tg3_get_eeprom_size(tp);
  10180. }
  10181. }
  10182. static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
  10183. u32 offset, u32 len, u8 *buf)
  10184. {
  10185. int i, j, rc = 0;
  10186. u32 val;
  10187. for (i = 0; i < len; i += 4) {
  10188. u32 addr;
  10189. __be32 data;
  10190. addr = offset + i;
  10191. memcpy(&data, buf + i, 4);
  10192. /*
  10193. * The SEEPROM interface expects the data to always be opposite
  10194. * the native endian format. We accomplish this by reversing
  10195. * all the operations that would have been performed on the
  10196. * data from a call to tg3_nvram_read_be32().
  10197. */
  10198. tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
  10199. val = tr32(GRC_EEPROM_ADDR);
  10200. tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
  10201. val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
  10202. EEPROM_ADDR_READ);
  10203. tw32(GRC_EEPROM_ADDR, val |
  10204. (0 << EEPROM_ADDR_DEVID_SHIFT) |
  10205. (addr & EEPROM_ADDR_ADDR_MASK) |
  10206. EEPROM_ADDR_START |
  10207. EEPROM_ADDR_WRITE);
  10208. for (j = 0; j < 1000; j++) {
  10209. val = tr32(GRC_EEPROM_ADDR);
  10210. if (val & EEPROM_ADDR_COMPLETE)
  10211. break;
  10212. msleep(1);
  10213. }
  10214. if (!(val & EEPROM_ADDR_COMPLETE)) {
  10215. rc = -EBUSY;
  10216. break;
  10217. }
  10218. }
  10219. return rc;
  10220. }
  10221. /* offset and length are dword aligned */
  10222. static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
  10223. u8 *buf)
  10224. {
  10225. int ret = 0;
  10226. u32 pagesize = tp->nvram_pagesize;
  10227. u32 pagemask = pagesize - 1;
  10228. u32 nvram_cmd;
  10229. u8 *tmp;
  10230. tmp = kmalloc(pagesize, GFP_KERNEL);
  10231. if (tmp == NULL)
  10232. return -ENOMEM;
  10233. while (len) {
  10234. int j;
  10235. u32 phy_addr, page_off, size;
  10236. phy_addr = offset & ~pagemask;
  10237. for (j = 0; j < pagesize; j += 4) {
  10238. ret = tg3_nvram_read_be32(tp, phy_addr + j,
  10239. (__be32 *) (tmp + j));
  10240. if (ret)
  10241. break;
  10242. }
  10243. if (ret)
  10244. break;
  10245. page_off = offset & pagemask;
  10246. size = pagesize;
  10247. if (len < size)
  10248. size = len;
  10249. len -= size;
  10250. memcpy(tmp + page_off, buf, size);
  10251. offset = offset + (pagesize - page_off);
  10252. tg3_enable_nvram_access(tp);
  10253. /*
  10254. * Before we can erase the flash page, we need
  10255. * to issue a special "write enable" command.
  10256. */
  10257. nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
  10258. if (tg3_nvram_exec_cmd(tp, nvram_cmd))
  10259. break;
  10260. /* Erase the target page */
  10261. tw32(NVRAM_ADDR, phy_addr);
  10262. nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
  10263. NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
  10264. if (tg3_nvram_exec_cmd(tp, nvram_cmd))
  10265. break;
  10266. /* Issue another write enable to start the write. */
  10267. nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
  10268. if (tg3_nvram_exec_cmd(tp, nvram_cmd))
  10269. break;
  10270. for (j = 0; j < pagesize; j += 4) {
  10271. __be32 data;
  10272. data = *((__be32 *) (tmp + j));
  10273. tw32(NVRAM_WRDATA, be32_to_cpu(data));
  10274. tw32(NVRAM_ADDR, phy_addr + j);
  10275. nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
  10276. NVRAM_CMD_WR;
  10277. if (j == 0)
  10278. nvram_cmd |= NVRAM_CMD_FIRST;
  10279. else if (j == (pagesize - 4))
  10280. nvram_cmd |= NVRAM_CMD_LAST;
  10281. if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
  10282. break;
  10283. }
  10284. if (ret)
  10285. break;
  10286. }
  10287. nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
  10288. tg3_nvram_exec_cmd(tp, nvram_cmd);
  10289. kfree(tmp);
  10290. return ret;
  10291. }
  10292. /* offset and length are dword aligned */
  10293. static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
  10294. u8 *buf)
  10295. {
  10296. int i, ret = 0;
  10297. for (i = 0; i < len; i += 4, offset += 4) {
  10298. u32 page_off, phy_addr, nvram_cmd;
  10299. __be32 data;
  10300. memcpy(&data, buf + i, 4);
  10301. tw32(NVRAM_WRDATA, be32_to_cpu(data));
  10302. page_off = offset % tp->nvram_pagesize;
  10303. phy_addr = tg3_nvram_phys_addr(tp, offset);
  10304. tw32(NVRAM_ADDR, phy_addr);
  10305. nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
  10306. if (page_off == 0 || i == 0)
  10307. nvram_cmd |= NVRAM_CMD_FIRST;
  10308. if (page_off == (tp->nvram_pagesize - 4))
  10309. nvram_cmd |= NVRAM_CMD_LAST;
  10310. if (i == (len - 4))
  10311. nvram_cmd |= NVRAM_CMD_LAST;
  10312. if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
  10313. !tg3_flag(tp, 5755_PLUS) &&
  10314. (tp->nvram_jedecnum == JEDEC_ST) &&
  10315. (nvram_cmd & NVRAM_CMD_FIRST)) {
  10316. if ((ret = tg3_nvram_exec_cmd(tp,
  10317. NVRAM_CMD_WREN | NVRAM_CMD_GO |
  10318. NVRAM_CMD_DONE)))
  10319. break;
  10320. }
  10321. if (!tg3_flag(tp, FLASH)) {
  10322. /* We always do complete word writes to eeprom. */
  10323. nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
  10324. }
  10325. if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
  10326. break;
  10327. }
  10328. return ret;
  10329. }
  10330. /* offset and length are dword aligned */
  10331. static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
  10332. {
  10333. int ret;
  10334. if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
  10335. tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
  10336. ~GRC_LCLCTRL_GPIO_OUTPUT1);
  10337. udelay(40);
  10338. }
  10339. if (!tg3_flag(tp, NVRAM)) {
  10340. ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
  10341. } else {
  10342. u32 grc_mode;
  10343. ret = tg3_nvram_lock(tp);
  10344. if (ret)
  10345. return ret;
  10346. tg3_enable_nvram_access(tp);
  10347. if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM))
  10348. tw32(NVRAM_WRITE1, 0x406);
  10349. grc_mode = tr32(GRC_MODE);
  10350. tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
  10351. if (tg3_flag(tp, NVRAM_BUFFERED) || !tg3_flag(tp, FLASH)) {
  10352. ret = tg3_nvram_write_block_buffered(tp, offset, len,
  10353. buf);
  10354. } else {
  10355. ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
  10356. buf);
  10357. }
  10358. grc_mode = tr32(GRC_MODE);
  10359. tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
  10360. tg3_disable_nvram_access(tp);
  10361. tg3_nvram_unlock(tp);
  10362. }
  10363. if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
  10364. tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
  10365. udelay(40);
  10366. }
  10367. return ret;
  10368. }
  10369. struct subsys_tbl_ent {
  10370. u16 subsys_vendor, subsys_devid;
  10371. u32 phy_id;
  10372. };
  10373. static struct subsys_tbl_ent subsys_id_to_phy_id[] __devinitdata = {
  10374. /* Broadcom boards. */
  10375. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  10376. TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
  10377. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  10378. TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
  10379. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  10380. TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
  10381. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  10382. TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
  10383. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  10384. TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
  10385. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  10386. TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
  10387. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  10388. TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
  10389. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  10390. TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
  10391. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  10392. TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
  10393. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  10394. TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
  10395. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  10396. TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
  10397. /* 3com boards. */
  10398. { TG3PCI_SUBVENDOR_ID_3COM,
  10399. TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
  10400. { TG3PCI_SUBVENDOR_ID_3COM,
  10401. TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
  10402. { TG3PCI_SUBVENDOR_ID_3COM,
  10403. TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
  10404. { TG3PCI_SUBVENDOR_ID_3COM,
  10405. TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
  10406. { TG3PCI_SUBVENDOR_ID_3COM,
  10407. TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
  10408. /* DELL boards. */
  10409. { TG3PCI_SUBVENDOR_ID_DELL,
  10410. TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
  10411. { TG3PCI_SUBVENDOR_ID_DELL,
  10412. TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
  10413. { TG3PCI_SUBVENDOR_ID_DELL,
  10414. TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
  10415. { TG3PCI_SUBVENDOR_ID_DELL,
  10416. TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
  10417. /* Compaq boards. */
  10418. { TG3PCI_SUBVENDOR_ID_COMPAQ,
  10419. TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
  10420. { TG3PCI_SUBVENDOR_ID_COMPAQ,
  10421. TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
  10422. { TG3PCI_SUBVENDOR_ID_COMPAQ,
  10423. TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
  10424. { TG3PCI_SUBVENDOR_ID_COMPAQ,
  10425. TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
  10426. { TG3PCI_SUBVENDOR_ID_COMPAQ,
  10427. TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
  10428. /* IBM boards. */
  10429. { TG3PCI_SUBVENDOR_ID_IBM,
  10430. TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
  10431. };
  10432. static struct subsys_tbl_ent * __devinit tg3_lookup_by_subsys(struct tg3 *tp)
  10433. {
  10434. int i;
  10435. for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
  10436. if ((subsys_id_to_phy_id[i].subsys_vendor ==
  10437. tp->pdev->subsystem_vendor) &&
  10438. (subsys_id_to_phy_id[i].subsys_devid ==
  10439. tp->pdev->subsystem_device))
  10440. return &subsys_id_to_phy_id[i];
  10441. }
  10442. return NULL;
  10443. }
  10444. static void __devinit tg3_get_eeprom_hw_cfg(struct tg3 *tp)
  10445. {
  10446. u32 val;
  10447. u16 pmcsr;
  10448. /* On some early chips the SRAM cannot be accessed in D3hot state,
  10449. * so need make sure we're in D0.
  10450. */
  10451. pci_read_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, &pmcsr);
  10452. pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
  10453. pci_write_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, pmcsr);
  10454. msleep(1);
  10455. /* Make sure register accesses (indirect or otherwise)
  10456. * will function correctly.
  10457. */
  10458. pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
  10459. tp->misc_host_ctrl);
  10460. /* The memory arbiter has to be enabled in order for SRAM accesses
  10461. * to succeed. Normally on powerup the tg3 chip firmware will make
  10462. * sure it is enabled, but other entities such as system netboot
  10463. * code might disable it.
  10464. */
  10465. val = tr32(MEMARB_MODE);
  10466. tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
  10467. tp->phy_id = TG3_PHY_ID_INVALID;
  10468. tp->led_ctrl = LED_CTRL_MODE_PHY_1;
  10469. /* Assume an onboard device and WOL capable by default. */
  10470. tg3_flag_set(tp, EEPROM_WRITE_PROT);
  10471. tg3_flag_set(tp, WOL_CAP);
  10472. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
  10473. if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
  10474. tg3_flag_clear(tp, EEPROM_WRITE_PROT);
  10475. tg3_flag_set(tp, IS_NIC);
  10476. }
  10477. val = tr32(VCPU_CFGSHDW);
  10478. if (val & VCPU_CFGSHDW_ASPM_DBNC)
  10479. tg3_flag_set(tp, ASPM_WORKAROUND);
  10480. if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
  10481. (val & VCPU_CFGSHDW_WOL_MAGPKT)) {
  10482. tg3_flag_set(tp, WOL_ENABLE);
  10483. device_set_wakeup_enable(&tp->pdev->dev, true);
  10484. }
  10485. goto done;
  10486. }
  10487. tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
  10488. if (val == NIC_SRAM_DATA_SIG_MAGIC) {
  10489. u32 nic_cfg, led_cfg;
  10490. u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
  10491. int eeprom_phy_serdes = 0;
  10492. tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
  10493. tp->nic_sram_data_cfg = nic_cfg;
  10494. tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
  10495. ver >>= NIC_SRAM_DATA_VER_SHIFT;
  10496. if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
  10497. GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
  10498. GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703 &&
  10499. (ver > 0) && (ver < 0x100))
  10500. tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
  10501. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
  10502. tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
  10503. if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
  10504. NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
  10505. eeprom_phy_serdes = 1;
  10506. tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
  10507. if (nic_phy_id != 0) {
  10508. u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
  10509. u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
  10510. eeprom_phy_id = (id1 >> 16) << 10;
  10511. eeprom_phy_id |= (id2 & 0xfc00) << 16;
  10512. eeprom_phy_id |= (id2 & 0x03ff) << 0;
  10513. } else
  10514. eeprom_phy_id = 0;
  10515. tp->phy_id = eeprom_phy_id;
  10516. if (eeprom_phy_serdes) {
  10517. if (!tg3_flag(tp, 5705_PLUS))
  10518. tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
  10519. else
  10520. tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
  10521. }
  10522. if (tg3_flag(tp, 5750_PLUS))
  10523. led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
  10524. SHASTA_EXT_LED_MODE_MASK);
  10525. else
  10526. led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
  10527. switch (led_cfg) {
  10528. default:
  10529. case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
  10530. tp->led_ctrl = LED_CTRL_MODE_PHY_1;
  10531. break;
  10532. case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
  10533. tp->led_ctrl = LED_CTRL_MODE_PHY_2;
  10534. break;
  10535. case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
  10536. tp->led_ctrl = LED_CTRL_MODE_MAC;
  10537. /* Default to PHY_1_MODE if 0 (MAC_MODE) is
  10538. * read on some older 5700/5701 bootcode.
  10539. */
  10540. if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
  10541. ASIC_REV_5700 ||
  10542. GET_ASIC_REV(tp->pci_chip_rev_id) ==
  10543. ASIC_REV_5701)
  10544. tp->led_ctrl = LED_CTRL_MODE_PHY_1;
  10545. break;
  10546. case SHASTA_EXT_LED_SHARED:
  10547. tp->led_ctrl = LED_CTRL_MODE_SHARED;
  10548. if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
  10549. tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
  10550. tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
  10551. LED_CTRL_MODE_PHY_2);
  10552. break;
  10553. case SHASTA_EXT_LED_MAC:
  10554. tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
  10555. break;
  10556. case SHASTA_EXT_LED_COMBO:
  10557. tp->led_ctrl = LED_CTRL_MODE_COMBO;
  10558. if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
  10559. tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
  10560. LED_CTRL_MODE_PHY_2);
  10561. break;
  10562. }
  10563. if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
  10564. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
  10565. tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
  10566. tp->led_ctrl = LED_CTRL_MODE_PHY_2;
  10567. if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
  10568. tp->led_ctrl = LED_CTRL_MODE_PHY_1;
  10569. if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
  10570. tg3_flag_set(tp, EEPROM_WRITE_PROT);
  10571. if ((tp->pdev->subsystem_vendor ==
  10572. PCI_VENDOR_ID_ARIMA) &&
  10573. (tp->pdev->subsystem_device == 0x205a ||
  10574. tp->pdev->subsystem_device == 0x2063))
  10575. tg3_flag_clear(tp, EEPROM_WRITE_PROT);
  10576. } else {
  10577. tg3_flag_clear(tp, EEPROM_WRITE_PROT);
  10578. tg3_flag_set(tp, IS_NIC);
  10579. }
  10580. if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
  10581. tg3_flag_set(tp, ENABLE_ASF);
  10582. if (tg3_flag(tp, 5750_PLUS))
  10583. tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
  10584. }
  10585. if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
  10586. tg3_flag(tp, 5750_PLUS))
  10587. tg3_flag_set(tp, ENABLE_APE);
  10588. if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
  10589. !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
  10590. tg3_flag_clear(tp, WOL_CAP);
  10591. if (tg3_flag(tp, WOL_CAP) &&
  10592. (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE)) {
  10593. tg3_flag_set(tp, WOL_ENABLE);
  10594. device_set_wakeup_enable(&tp->pdev->dev, true);
  10595. }
  10596. if (cfg2 & (1 << 17))
  10597. tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
  10598. /* serdes signal pre-emphasis in register 0x590 set by */
  10599. /* bootcode if bit 18 is set */
  10600. if (cfg2 & (1 << 18))
  10601. tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
  10602. if ((tg3_flag(tp, 57765_PLUS) ||
  10603. (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
  10604. GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX)) &&
  10605. (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
  10606. tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
  10607. if (tg3_flag(tp, PCI_EXPRESS) &&
  10608. GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
  10609. !tg3_flag(tp, 57765_PLUS)) {
  10610. u32 cfg3;
  10611. tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
  10612. if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
  10613. tg3_flag_set(tp, ASPM_WORKAROUND);
  10614. }
  10615. if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
  10616. tg3_flag_set(tp, RGMII_INBAND_DISABLE);
  10617. if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
  10618. tg3_flag_set(tp, RGMII_EXT_IBND_RX_EN);
  10619. if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
  10620. tg3_flag_set(tp, RGMII_EXT_IBND_TX_EN);
  10621. }
  10622. done:
  10623. if (tg3_flag(tp, WOL_CAP))
  10624. device_set_wakeup_enable(&tp->pdev->dev,
  10625. tg3_flag(tp, WOL_ENABLE));
  10626. else
  10627. device_set_wakeup_capable(&tp->pdev->dev, false);
  10628. }
  10629. static int __devinit tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
  10630. {
  10631. int i;
  10632. u32 val;
  10633. tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
  10634. tw32(OTP_CTRL, cmd);
  10635. /* Wait for up to 1 ms for command to execute. */
  10636. for (i = 0; i < 100; i++) {
  10637. val = tr32(OTP_STATUS);
  10638. if (val & OTP_STATUS_CMD_DONE)
  10639. break;
  10640. udelay(10);
  10641. }
  10642. return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
  10643. }
  10644. /* Read the gphy configuration from the OTP region of the chip. The gphy
  10645. * configuration is a 32-bit value that straddles the alignment boundary.
  10646. * We do two 32-bit reads and then shift and merge the results.
  10647. */
  10648. static u32 __devinit tg3_read_otp_phycfg(struct tg3 *tp)
  10649. {
  10650. u32 bhalf_otp, thalf_otp;
  10651. tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
  10652. if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
  10653. return 0;
  10654. tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
  10655. if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
  10656. return 0;
  10657. thalf_otp = tr32(OTP_READ_DATA);
  10658. tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
  10659. if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
  10660. return 0;
  10661. bhalf_otp = tr32(OTP_READ_DATA);
  10662. return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
  10663. }
  10664. static void __devinit tg3_phy_init_link_config(struct tg3 *tp)
  10665. {
  10666. u32 adv = ADVERTISED_Autoneg |
  10667. ADVERTISED_Pause;
  10668. if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
  10669. adv |= ADVERTISED_1000baseT_Half |
  10670. ADVERTISED_1000baseT_Full;
  10671. if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
  10672. adv |= ADVERTISED_100baseT_Half |
  10673. ADVERTISED_100baseT_Full |
  10674. ADVERTISED_10baseT_Half |
  10675. ADVERTISED_10baseT_Full |
  10676. ADVERTISED_TP;
  10677. else
  10678. adv |= ADVERTISED_FIBRE;
  10679. tp->link_config.advertising = adv;
  10680. tp->link_config.speed = SPEED_INVALID;
  10681. tp->link_config.duplex = DUPLEX_INVALID;
  10682. tp->link_config.autoneg = AUTONEG_ENABLE;
  10683. tp->link_config.active_speed = SPEED_INVALID;
  10684. tp->link_config.active_duplex = DUPLEX_INVALID;
  10685. tp->link_config.orig_speed = SPEED_INVALID;
  10686. tp->link_config.orig_duplex = DUPLEX_INVALID;
  10687. tp->link_config.orig_autoneg = AUTONEG_INVALID;
  10688. }
  10689. static int __devinit tg3_phy_probe(struct tg3 *tp)
  10690. {
  10691. u32 hw_phy_id_1, hw_phy_id_2;
  10692. u32 hw_phy_id, hw_phy_id_masked;
  10693. int err;
  10694. /* flow control autonegotiation is default behavior */
  10695. tg3_flag_set(tp, PAUSE_AUTONEG);
  10696. tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
  10697. if (tg3_flag(tp, USE_PHYLIB))
  10698. return tg3_phy_init(tp);
  10699. /* Reading the PHY ID register can conflict with ASF
  10700. * firmware access to the PHY hardware.
  10701. */
  10702. err = 0;
  10703. if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)) {
  10704. hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
  10705. } else {
  10706. /* Now read the physical PHY_ID from the chip and verify
  10707. * that it is sane. If it doesn't look good, we fall back
  10708. * to either the hard-coded table based PHY_ID and failing
  10709. * that the value found in the eeprom area.
  10710. */
  10711. err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
  10712. err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
  10713. hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
  10714. hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
  10715. hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
  10716. hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
  10717. }
  10718. if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
  10719. tp->phy_id = hw_phy_id;
  10720. if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
  10721. tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
  10722. else
  10723. tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
  10724. } else {
  10725. if (tp->phy_id != TG3_PHY_ID_INVALID) {
  10726. /* Do nothing, phy ID already set up in
  10727. * tg3_get_eeprom_hw_cfg().
  10728. */
  10729. } else {
  10730. struct subsys_tbl_ent *p;
  10731. /* No eeprom signature? Try the hardcoded
  10732. * subsys device table.
  10733. */
  10734. p = tg3_lookup_by_subsys(tp);
  10735. if (!p)
  10736. return -ENODEV;
  10737. tp->phy_id = p->phy_id;
  10738. if (!tp->phy_id ||
  10739. tp->phy_id == TG3_PHY_ID_BCM8002)
  10740. tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
  10741. }
  10742. }
  10743. if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
  10744. (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
  10745. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720 ||
  10746. (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 &&
  10747. tp->pci_chip_rev_id != CHIPREV_ID_5717_A0) ||
  10748. (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
  10749. tp->pci_chip_rev_id != CHIPREV_ID_57765_A0)))
  10750. tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
  10751. tg3_phy_init_link_config(tp);
  10752. if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
  10753. !tg3_flag(tp, ENABLE_APE) &&
  10754. !tg3_flag(tp, ENABLE_ASF)) {
  10755. u32 bmsr, mask;
  10756. tg3_readphy(tp, MII_BMSR, &bmsr);
  10757. if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
  10758. (bmsr & BMSR_LSTATUS))
  10759. goto skip_phy_reset;
  10760. err = tg3_phy_reset(tp);
  10761. if (err)
  10762. return err;
  10763. tg3_phy_set_wirespeed(tp);
  10764. mask = (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
  10765. ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
  10766. ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full);
  10767. if (!tg3_copper_is_advertising_all(tp, mask)) {
  10768. tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
  10769. tp->link_config.flowctrl);
  10770. tg3_writephy(tp, MII_BMCR,
  10771. BMCR_ANENABLE | BMCR_ANRESTART);
  10772. }
  10773. }
  10774. skip_phy_reset:
  10775. if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
  10776. err = tg3_init_5401phy_dsp(tp);
  10777. if (err)
  10778. return err;
  10779. err = tg3_init_5401phy_dsp(tp);
  10780. }
  10781. return err;
  10782. }
  10783. static void __devinit tg3_read_vpd(struct tg3 *tp)
  10784. {
  10785. u8 *vpd_data;
  10786. unsigned int block_end, rosize, len;
  10787. int j, i = 0;
  10788. vpd_data = (u8 *)tg3_vpd_readblock(tp);
  10789. if (!vpd_data)
  10790. goto out_no_vpd;
  10791. i = pci_vpd_find_tag(vpd_data, 0, TG3_NVM_VPD_LEN,
  10792. PCI_VPD_LRDT_RO_DATA);
  10793. if (i < 0)
  10794. goto out_not_found;
  10795. rosize = pci_vpd_lrdt_size(&vpd_data[i]);
  10796. block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
  10797. i += PCI_VPD_LRDT_TAG_SIZE;
  10798. if (block_end > TG3_NVM_VPD_LEN)
  10799. goto out_not_found;
  10800. j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
  10801. PCI_VPD_RO_KEYWORD_MFR_ID);
  10802. if (j > 0) {
  10803. len = pci_vpd_info_field_size(&vpd_data[j]);
  10804. j += PCI_VPD_INFO_FLD_HDR_SIZE;
  10805. if (j + len > block_end || len != 4 ||
  10806. memcmp(&vpd_data[j], "1028", 4))
  10807. goto partno;
  10808. j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
  10809. PCI_VPD_RO_KEYWORD_VENDOR0);
  10810. if (j < 0)
  10811. goto partno;
  10812. len = pci_vpd_info_field_size(&vpd_data[j]);
  10813. j += PCI_VPD_INFO_FLD_HDR_SIZE;
  10814. if (j + len > block_end)
  10815. goto partno;
  10816. memcpy(tp->fw_ver, &vpd_data[j], len);
  10817. strncat(tp->fw_ver, " bc ", TG3_NVM_VPD_LEN - len - 1);
  10818. }
  10819. partno:
  10820. i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
  10821. PCI_VPD_RO_KEYWORD_PARTNO);
  10822. if (i < 0)
  10823. goto out_not_found;
  10824. len = pci_vpd_info_field_size(&vpd_data[i]);
  10825. i += PCI_VPD_INFO_FLD_HDR_SIZE;
  10826. if (len > TG3_BPN_SIZE ||
  10827. (len + i) > TG3_NVM_VPD_LEN)
  10828. goto out_not_found;
  10829. memcpy(tp->board_part_number, &vpd_data[i], len);
  10830. out_not_found:
  10831. kfree(vpd_data);
  10832. if (tp->board_part_number[0])
  10833. return;
  10834. out_no_vpd:
  10835. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
  10836. if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717)
  10837. strcpy(tp->board_part_number, "BCM5717");
  10838. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
  10839. strcpy(tp->board_part_number, "BCM5718");
  10840. else
  10841. goto nomatch;
  10842. } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
  10843. if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
  10844. strcpy(tp->board_part_number, "BCM57780");
  10845. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
  10846. strcpy(tp->board_part_number, "BCM57760");
  10847. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
  10848. strcpy(tp->board_part_number, "BCM57790");
  10849. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
  10850. strcpy(tp->board_part_number, "BCM57788");
  10851. else
  10852. goto nomatch;
  10853. } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
  10854. if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
  10855. strcpy(tp->board_part_number, "BCM57761");
  10856. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
  10857. strcpy(tp->board_part_number, "BCM57765");
  10858. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
  10859. strcpy(tp->board_part_number, "BCM57781");
  10860. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
  10861. strcpy(tp->board_part_number, "BCM57785");
  10862. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
  10863. strcpy(tp->board_part_number, "BCM57791");
  10864. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
  10865. strcpy(tp->board_part_number, "BCM57795");
  10866. else
  10867. goto nomatch;
  10868. } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
  10869. strcpy(tp->board_part_number, "BCM95906");
  10870. } else {
  10871. nomatch:
  10872. strcpy(tp->board_part_number, "none");
  10873. }
  10874. }
  10875. static int __devinit tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
  10876. {
  10877. u32 val;
  10878. if (tg3_nvram_read(tp, offset, &val) ||
  10879. (val & 0xfc000000) != 0x0c000000 ||
  10880. tg3_nvram_read(tp, offset + 4, &val) ||
  10881. val != 0)
  10882. return 0;
  10883. return 1;
  10884. }
  10885. static void __devinit tg3_read_bc_ver(struct tg3 *tp)
  10886. {
  10887. u32 val, offset, start, ver_offset;
  10888. int i, dst_off;
  10889. bool newver = false;
  10890. if (tg3_nvram_read(tp, 0xc, &offset) ||
  10891. tg3_nvram_read(tp, 0x4, &start))
  10892. return;
  10893. offset = tg3_nvram_logical_addr(tp, offset);
  10894. if (tg3_nvram_read(tp, offset, &val))
  10895. return;
  10896. if ((val & 0xfc000000) == 0x0c000000) {
  10897. if (tg3_nvram_read(tp, offset + 4, &val))
  10898. return;
  10899. if (val == 0)
  10900. newver = true;
  10901. }
  10902. dst_off = strlen(tp->fw_ver);
  10903. if (newver) {
  10904. if (TG3_VER_SIZE - dst_off < 16 ||
  10905. tg3_nvram_read(tp, offset + 8, &ver_offset))
  10906. return;
  10907. offset = offset + ver_offset - start;
  10908. for (i = 0; i < 16; i += 4) {
  10909. __be32 v;
  10910. if (tg3_nvram_read_be32(tp, offset + i, &v))
  10911. return;
  10912. memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
  10913. }
  10914. } else {
  10915. u32 major, minor;
  10916. if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
  10917. return;
  10918. major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
  10919. TG3_NVM_BCVER_MAJSFT;
  10920. minor = ver_offset & TG3_NVM_BCVER_MINMSK;
  10921. snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
  10922. "v%d.%02d", major, minor);
  10923. }
  10924. }
  10925. static void __devinit tg3_read_hwsb_ver(struct tg3 *tp)
  10926. {
  10927. u32 val, major, minor;
  10928. /* Use native endian representation */
  10929. if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
  10930. return;
  10931. major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
  10932. TG3_NVM_HWSB_CFG1_MAJSFT;
  10933. minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
  10934. TG3_NVM_HWSB_CFG1_MINSFT;
  10935. snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
  10936. }
  10937. static void __devinit tg3_read_sb_ver(struct tg3 *tp, u32 val)
  10938. {
  10939. u32 offset, major, minor, build;
  10940. strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
  10941. if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
  10942. return;
  10943. switch (val & TG3_EEPROM_SB_REVISION_MASK) {
  10944. case TG3_EEPROM_SB_REVISION_0:
  10945. offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
  10946. break;
  10947. case TG3_EEPROM_SB_REVISION_2:
  10948. offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
  10949. break;
  10950. case TG3_EEPROM_SB_REVISION_3:
  10951. offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
  10952. break;
  10953. case TG3_EEPROM_SB_REVISION_4:
  10954. offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
  10955. break;
  10956. case TG3_EEPROM_SB_REVISION_5:
  10957. offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
  10958. break;
  10959. case TG3_EEPROM_SB_REVISION_6:
  10960. offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
  10961. break;
  10962. default:
  10963. return;
  10964. }
  10965. if (tg3_nvram_read(tp, offset, &val))
  10966. return;
  10967. build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
  10968. TG3_EEPROM_SB_EDH_BLD_SHFT;
  10969. major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
  10970. TG3_EEPROM_SB_EDH_MAJ_SHFT;
  10971. minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
  10972. if (minor > 99 || build > 26)
  10973. return;
  10974. offset = strlen(tp->fw_ver);
  10975. snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
  10976. " v%d.%02d", major, minor);
  10977. if (build > 0) {
  10978. offset = strlen(tp->fw_ver);
  10979. if (offset < TG3_VER_SIZE - 1)
  10980. tp->fw_ver[offset] = 'a' + build - 1;
  10981. }
  10982. }
  10983. static void __devinit tg3_read_mgmtfw_ver(struct tg3 *tp)
  10984. {
  10985. u32 val, offset, start;
  10986. int i, vlen;
  10987. for (offset = TG3_NVM_DIR_START;
  10988. offset < TG3_NVM_DIR_END;
  10989. offset += TG3_NVM_DIRENT_SIZE) {
  10990. if (tg3_nvram_read(tp, offset, &val))
  10991. return;
  10992. if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
  10993. break;
  10994. }
  10995. if (offset == TG3_NVM_DIR_END)
  10996. return;
  10997. if (!tg3_flag(tp, 5705_PLUS))
  10998. start = 0x08000000;
  10999. else if (tg3_nvram_read(tp, offset - 4, &start))
  11000. return;
  11001. if (tg3_nvram_read(tp, offset + 4, &offset) ||
  11002. !tg3_fw_img_is_valid(tp, offset) ||
  11003. tg3_nvram_read(tp, offset + 8, &val))
  11004. return;
  11005. offset += val - start;
  11006. vlen = strlen(tp->fw_ver);
  11007. tp->fw_ver[vlen++] = ',';
  11008. tp->fw_ver[vlen++] = ' ';
  11009. for (i = 0; i < 4; i++) {
  11010. __be32 v;
  11011. if (tg3_nvram_read_be32(tp, offset, &v))
  11012. return;
  11013. offset += sizeof(v);
  11014. if (vlen > TG3_VER_SIZE - sizeof(v)) {
  11015. memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
  11016. break;
  11017. }
  11018. memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
  11019. vlen += sizeof(v);
  11020. }
  11021. }
  11022. static void __devinit tg3_read_dash_ver(struct tg3 *tp)
  11023. {
  11024. int vlen;
  11025. u32 apedata;
  11026. char *fwtype;
  11027. if (!tg3_flag(tp, ENABLE_APE) || !tg3_flag(tp, ENABLE_ASF))
  11028. return;
  11029. apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
  11030. if (apedata != APE_SEG_SIG_MAGIC)
  11031. return;
  11032. apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
  11033. if (!(apedata & APE_FW_STATUS_READY))
  11034. return;
  11035. apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
  11036. if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI) {
  11037. tg3_flag_set(tp, APE_HAS_NCSI);
  11038. fwtype = "NCSI";
  11039. } else {
  11040. fwtype = "DASH";
  11041. }
  11042. vlen = strlen(tp->fw_ver);
  11043. snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
  11044. fwtype,
  11045. (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
  11046. (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
  11047. (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
  11048. (apedata & APE_FW_VERSION_BLDMSK));
  11049. }
  11050. static void __devinit tg3_read_fw_ver(struct tg3 *tp)
  11051. {
  11052. u32 val;
  11053. bool vpd_vers = false;
  11054. if (tp->fw_ver[0] != 0)
  11055. vpd_vers = true;
  11056. if (tg3_flag(tp, NO_NVRAM)) {
  11057. strcat(tp->fw_ver, "sb");
  11058. return;
  11059. }
  11060. if (tg3_nvram_read(tp, 0, &val))
  11061. return;
  11062. if (val == TG3_EEPROM_MAGIC)
  11063. tg3_read_bc_ver(tp);
  11064. else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
  11065. tg3_read_sb_ver(tp, val);
  11066. else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
  11067. tg3_read_hwsb_ver(tp);
  11068. else
  11069. return;
  11070. if (!tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE) || vpd_vers)
  11071. goto done;
  11072. tg3_read_mgmtfw_ver(tp);
  11073. done:
  11074. tp->fw_ver[TG3_VER_SIZE - 1] = 0;
  11075. }
  11076. static struct pci_dev * __devinit tg3_find_peer(struct tg3 *);
  11077. static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
  11078. {
  11079. if (tg3_flag(tp, LRG_PROD_RING_CAP))
  11080. return TG3_RX_RET_MAX_SIZE_5717;
  11081. else if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))
  11082. return TG3_RX_RET_MAX_SIZE_5700;
  11083. else
  11084. return TG3_RX_RET_MAX_SIZE_5705;
  11085. }
  11086. static DEFINE_PCI_DEVICE_TABLE(tg3_write_reorder_chipsets) = {
  11087. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
  11088. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
  11089. { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
  11090. { },
  11091. };
  11092. static int __devinit tg3_get_invariants(struct tg3 *tp)
  11093. {
  11094. u32 misc_ctrl_reg;
  11095. u32 pci_state_reg, grc_misc_cfg;
  11096. u32 val;
  11097. u16 pci_cmd;
  11098. int err;
  11099. /* Force memory write invalidate off. If we leave it on,
  11100. * then on 5700_BX chips we have to enable a workaround.
  11101. * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
  11102. * to match the cacheline size. The Broadcom driver have this
  11103. * workaround but turns MWI off all the times so never uses
  11104. * it. This seems to suggest that the workaround is insufficient.
  11105. */
  11106. pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
  11107. pci_cmd &= ~PCI_COMMAND_INVALIDATE;
  11108. pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
  11109. /* It is absolutely critical that TG3PCI_MISC_HOST_CTRL
  11110. * has the register indirect write enable bit set before
  11111. * we try to access any of the MMIO registers. It is also
  11112. * critical that the PCI-X hw workaround situation is decided
  11113. * before that as well.
  11114. */
  11115. pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
  11116. &misc_ctrl_reg);
  11117. tp->pci_chip_rev_id = (misc_ctrl_reg >>
  11118. MISC_HOST_CTRL_CHIPREV_SHIFT);
  11119. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
  11120. u32 prod_id_asic_rev;
  11121. if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
  11122. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
  11123. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
  11124. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720)
  11125. pci_read_config_dword(tp->pdev,
  11126. TG3PCI_GEN2_PRODID_ASICREV,
  11127. &prod_id_asic_rev);
  11128. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
  11129. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
  11130. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
  11131. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
  11132. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
  11133. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
  11134. pci_read_config_dword(tp->pdev,
  11135. TG3PCI_GEN15_PRODID_ASICREV,
  11136. &prod_id_asic_rev);
  11137. else
  11138. pci_read_config_dword(tp->pdev, TG3PCI_PRODID_ASICREV,
  11139. &prod_id_asic_rev);
  11140. tp->pci_chip_rev_id = prod_id_asic_rev;
  11141. }
  11142. /* Wrong chip ID in 5752 A0. This code can be removed later
  11143. * as A0 is not in production.
  11144. */
  11145. if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
  11146. tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
  11147. /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
  11148. * we need to disable memory and use config. cycles
  11149. * only to access all registers. The 5702/03 chips
  11150. * can mistakenly decode the special cycles from the
  11151. * ICH chipsets as memory write cycles, causing corruption
  11152. * of register and memory space. Only certain ICH bridges
  11153. * will drive special cycles with non-zero data during the
  11154. * address phase which can fall within the 5703's address
  11155. * range. This is not an ICH bug as the PCI spec allows
  11156. * non-zero address during special cycles. However, only
  11157. * these ICH bridges are known to drive non-zero addresses
  11158. * during special cycles.
  11159. *
  11160. * Since special cycles do not cross PCI bridges, we only
  11161. * enable this workaround if the 5703 is on the secondary
  11162. * bus of these ICH bridges.
  11163. */
  11164. if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
  11165. (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
  11166. static struct tg3_dev_id {
  11167. u32 vendor;
  11168. u32 device;
  11169. u32 rev;
  11170. } ich_chipsets[] = {
  11171. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
  11172. PCI_ANY_ID },
  11173. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
  11174. PCI_ANY_ID },
  11175. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
  11176. 0xa },
  11177. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
  11178. PCI_ANY_ID },
  11179. { },
  11180. };
  11181. struct tg3_dev_id *pci_id = &ich_chipsets[0];
  11182. struct pci_dev *bridge = NULL;
  11183. while (pci_id->vendor != 0) {
  11184. bridge = pci_get_device(pci_id->vendor, pci_id->device,
  11185. bridge);
  11186. if (!bridge) {
  11187. pci_id++;
  11188. continue;
  11189. }
  11190. if (pci_id->rev != PCI_ANY_ID) {
  11191. if (bridge->revision > pci_id->rev)
  11192. continue;
  11193. }
  11194. if (bridge->subordinate &&
  11195. (bridge->subordinate->number ==
  11196. tp->pdev->bus->number)) {
  11197. tg3_flag_set(tp, ICH_WORKAROUND);
  11198. pci_dev_put(bridge);
  11199. break;
  11200. }
  11201. }
  11202. }
  11203. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
  11204. static struct tg3_dev_id {
  11205. u32 vendor;
  11206. u32 device;
  11207. } bridge_chipsets[] = {
  11208. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
  11209. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
  11210. { },
  11211. };
  11212. struct tg3_dev_id *pci_id = &bridge_chipsets[0];
  11213. struct pci_dev *bridge = NULL;
  11214. while (pci_id->vendor != 0) {
  11215. bridge = pci_get_device(pci_id->vendor,
  11216. pci_id->device,
  11217. bridge);
  11218. if (!bridge) {
  11219. pci_id++;
  11220. continue;
  11221. }
  11222. if (bridge->subordinate &&
  11223. (bridge->subordinate->number <=
  11224. tp->pdev->bus->number) &&
  11225. (bridge->subordinate->subordinate >=
  11226. tp->pdev->bus->number)) {
  11227. tg3_flag_set(tp, 5701_DMA_BUG);
  11228. pci_dev_put(bridge);
  11229. break;
  11230. }
  11231. }
  11232. }
  11233. /* The EPB bridge inside 5714, 5715, and 5780 cannot support
  11234. * DMA addresses > 40-bit. This bridge may have other additional
  11235. * 57xx devices behind it in some 4-port NIC designs for example.
  11236. * Any tg3 device found behind the bridge will also need the 40-bit
  11237. * DMA workaround.
  11238. */
  11239. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
  11240. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
  11241. tg3_flag_set(tp, 5780_CLASS);
  11242. tg3_flag_set(tp, 40BIT_DMA_BUG);
  11243. tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
  11244. } else {
  11245. struct pci_dev *bridge = NULL;
  11246. do {
  11247. bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
  11248. PCI_DEVICE_ID_SERVERWORKS_EPB,
  11249. bridge);
  11250. if (bridge && bridge->subordinate &&
  11251. (bridge->subordinate->number <=
  11252. tp->pdev->bus->number) &&
  11253. (bridge->subordinate->subordinate >=
  11254. tp->pdev->bus->number)) {
  11255. tg3_flag_set(tp, 40BIT_DMA_BUG);
  11256. pci_dev_put(bridge);
  11257. break;
  11258. }
  11259. } while (bridge);
  11260. }
  11261. /* Initialize misc host control in PCI block. */
  11262. tp->misc_host_ctrl |= (misc_ctrl_reg &
  11263. MISC_HOST_CTRL_CHIPREV);
  11264. pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
  11265. tp->misc_host_ctrl);
  11266. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
  11267. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
  11268. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
  11269. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
  11270. tp->pdev_peer = tg3_find_peer(tp);
  11271. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
  11272. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
  11273. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
  11274. tg3_flag_set(tp, 5717_PLUS);
  11275. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 ||
  11276. tg3_flag(tp, 5717_PLUS))
  11277. tg3_flag_set(tp, 57765_PLUS);
  11278. /* Intentionally exclude ASIC_REV_5906 */
  11279. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
  11280. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
  11281. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
  11282. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
  11283. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
  11284. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
  11285. tg3_flag(tp, 57765_PLUS))
  11286. tg3_flag_set(tp, 5755_PLUS);
  11287. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
  11288. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
  11289. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
  11290. tg3_flag(tp, 5755_PLUS) ||
  11291. tg3_flag(tp, 5780_CLASS))
  11292. tg3_flag_set(tp, 5750_PLUS);
  11293. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
  11294. tg3_flag(tp, 5750_PLUS))
  11295. tg3_flag_set(tp, 5705_PLUS);
  11296. /* Determine TSO capabilities */
  11297. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
  11298. ; /* Do nothing. HW bug. */
  11299. else if (tg3_flag(tp, 57765_PLUS))
  11300. tg3_flag_set(tp, HW_TSO_3);
  11301. else if (tg3_flag(tp, 5755_PLUS) ||
  11302. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
  11303. tg3_flag_set(tp, HW_TSO_2);
  11304. else if (tg3_flag(tp, 5750_PLUS)) {
  11305. tg3_flag_set(tp, HW_TSO_1);
  11306. tg3_flag_set(tp, TSO_BUG);
  11307. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 &&
  11308. tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
  11309. tg3_flag_clear(tp, TSO_BUG);
  11310. } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
  11311. GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
  11312. tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
  11313. tg3_flag_set(tp, TSO_BUG);
  11314. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
  11315. tp->fw_needed = FIRMWARE_TG3TSO5;
  11316. else
  11317. tp->fw_needed = FIRMWARE_TG3TSO;
  11318. }
  11319. /* Selectively allow TSO based on operating conditions */
  11320. if (tg3_flag(tp, HW_TSO_1) ||
  11321. tg3_flag(tp, HW_TSO_2) ||
  11322. tg3_flag(tp, HW_TSO_3) ||
  11323. (tp->fw_needed && !tg3_flag(tp, ENABLE_ASF)))
  11324. tg3_flag_set(tp, TSO_CAPABLE);
  11325. else {
  11326. tg3_flag_clear(tp, TSO_CAPABLE);
  11327. tg3_flag_clear(tp, TSO_BUG);
  11328. tp->fw_needed = NULL;
  11329. }
  11330. if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
  11331. tp->fw_needed = FIRMWARE_TG3;
  11332. tp->irq_max = 1;
  11333. if (tg3_flag(tp, 5750_PLUS)) {
  11334. tg3_flag_set(tp, SUPPORT_MSI);
  11335. if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
  11336. GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
  11337. (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
  11338. tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
  11339. tp->pdev_peer == tp->pdev))
  11340. tg3_flag_clear(tp, SUPPORT_MSI);
  11341. if (tg3_flag(tp, 5755_PLUS) ||
  11342. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
  11343. tg3_flag_set(tp, 1SHOT_MSI);
  11344. }
  11345. if (tg3_flag(tp, 57765_PLUS)) {
  11346. tg3_flag_set(tp, SUPPORT_MSIX);
  11347. tp->irq_max = TG3_IRQ_MAX_VECS;
  11348. }
  11349. }
  11350. /* All chips can get confused if TX buffers
  11351. * straddle the 4GB address boundary.
  11352. */
  11353. tg3_flag_set(tp, 4G_DMA_BNDRY_BUG);
  11354. if (tg3_flag(tp, 5755_PLUS))
  11355. tg3_flag_set(tp, SHORT_DMA_BUG);
  11356. else
  11357. tg3_flag_set(tp, 40BIT_DMA_LIMIT_BUG);
  11358. if (tg3_flag(tp, 5717_PLUS))
  11359. tg3_flag_set(tp, LRG_PROD_RING_CAP);
  11360. if (tg3_flag(tp, 57765_PLUS) &&
  11361. GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5719)
  11362. tg3_flag_set(tp, USE_JUMBO_BDFLAG);
  11363. if (!tg3_flag(tp, 5705_PLUS) ||
  11364. tg3_flag(tp, 5780_CLASS) ||
  11365. tg3_flag(tp, USE_JUMBO_BDFLAG))
  11366. tg3_flag_set(tp, JUMBO_CAPABLE);
  11367. pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
  11368. &pci_state_reg);
  11369. tp->pcie_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_EXP);
  11370. if (tp->pcie_cap != 0) {
  11371. u16 lnkctl;
  11372. tg3_flag_set(tp, PCI_EXPRESS);
  11373. tp->pcie_readrq = 4096;
  11374. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
  11375. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
  11376. tp->pcie_readrq = 2048;
  11377. pcie_set_readrq(tp->pdev, tp->pcie_readrq);
  11378. pci_read_config_word(tp->pdev,
  11379. tp->pcie_cap + PCI_EXP_LNKCTL,
  11380. &lnkctl);
  11381. if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
  11382. if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
  11383. ASIC_REV_5906) {
  11384. tg3_flag_clear(tp, HW_TSO_2);
  11385. tg3_flag_clear(tp, TSO_CAPABLE);
  11386. }
  11387. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
  11388. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
  11389. tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
  11390. tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
  11391. tg3_flag_set(tp, CLKREQ_BUG);
  11392. } else if (tp->pci_chip_rev_id == CHIPREV_ID_5717_A0) {
  11393. tg3_flag_set(tp, L1PLLPD_EN);
  11394. }
  11395. } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
  11396. tg3_flag_set(tp, PCI_EXPRESS);
  11397. } else if (!tg3_flag(tp, 5705_PLUS) ||
  11398. tg3_flag(tp, 5780_CLASS)) {
  11399. tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
  11400. if (!tp->pcix_cap) {
  11401. dev_err(&tp->pdev->dev,
  11402. "Cannot find PCI-X capability, aborting\n");
  11403. return -EIO;
  11404. }
  11405. if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
  11406. tg3_flag_set(tp, PCIX_MODE);
  11407. }
  11408. /* If we have an AMD 762 or VIA K8T800 chipset, write
  11409. * reordering to the mailbox registers done by the host
  11410. * controller can cause major troubles. We read back from
  11411. * every mailbox register write to force the writes to be
  11412. * posted to the chip in order.
  11413. */
  11414. if (pci_dev_present(tg3_write_reorder_chipsets) &&
  11415. !tg3_flag(tp, PCI_EXPRESS))
  11416. tg3_flag_set(tp, MBOX_WRITE_REORDER);
  11417. pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
  11418. &tp->pci_cacheline_sz);
  11419. pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
  11420. &tp->pci_lat_timer);
  11421. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
  11422. tp->pci_lat_timer < 64) {
  11423. tp->pci_lat_timer = 64;
  11424. pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
  11425. tp->pci_lat_timer);
  11426. }
  11427. if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
  11428. /* 5700 BX chips need to have their TX producer index
  11429. * mailboxes written twice to workaround a bug.
  11430. */
  11431. tg3_flag_set(tp, TXD_MBOX_HWBUG);
  11432. /* If we are in PCI-X mode, enable register write workaround.
  11433. *
  11434. * The workaround is to use indirect register accesses
  11435. * for all chip writes not to mailbox registers.
  11436. */
  11437. if (tg3_flag(tp, PCIX_MODE)) {
  11438. u32 pm_reg;
  11439. tg3_flag_set(tp, PCIX_TARGET_HWBUG);
  11440. /* The chip can have it's power management PCI config
  11441. * space registers clobbered due to this bug.
  11442. * So explicitly force the chip into D0 here.
  11443. */
  11444. pci_read_config_dword(tp->pdev,
  11445. tp->pm_cap + PCI_PM_CTRL,
  11446. &pm_reg);
  11447. pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
  11448. pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
  11449. pci_write_config_dword(tp->pdev,
  11450. tp->pm_cap + PCI_PM_CTRL,
  11451. pm_reg);
  11452. /* Also, force SERR#/PERR# in PCI command. */
  11453. pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
  11454. pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
  11455. pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
  11456. }
  11457. }
  11458. if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
  11459. tg3_flag_set(tp, PCI_HIGH_SPEED);
  11460. if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
  11461. tg3_flag_set(tp, PCI_32BIT);
  11462. /* Chip-specific fixup from Broadcom driver */
  11463. if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
  11464. (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
  11465. pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
  11466. pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
  11467. }
  11468. /* Default fast path register access methods */
  11469. tp->read32 = tg3_read32;
  11470. tp->write32 = tg3_write32;
  11471. tp->read32_mbox = tg3_read32;
  11472. tp->write32_mbox = tg3_write32;
  11473. tp->write32_tx_mbox = tg3_write32;
  11474. tp->write32_rx_mbox = tg3_write32;
  11475. /* Various workaround register access methods */
  11476. if (tg3_flag(tp, PCIX_TARGET_HWBUG))
  11477. tp->write32 = tg3_write_indirect_reg32;
  11478. else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
  11479. (tg3_flag(tp, PCI_EXPRESS) &&
  11480. tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
  11481. /*
  11482. * Back to back register writes can cause problems on these
  11483. * chips, the workaround is to read back all reg writes
  11484. * except those to mailbox regs.
  11485. *
  11486. * See tg3_write_indirect_reg32().
  11487. */
  11488. tp->write32 = tg3_write_flush_reg32;
  11489. }
  11490. if (tg3_flag(tp, TXD_MBOX_HWBUG) || tg3_flag(tp, MBOX_WRITE_REORDER)) {
  11491. tp->write32_tx_mbox = tg3_write32_tx_mbox;
  11492. if (tg3_flag(tp, MBOX_WRITE_REORDER))
  11493. tp->write32_rx_mbox = tg3_write_flush_reg32;
  11494. }
  11495. if (tg3_flag(tp, ICH_WORKAROUND)) {
  11496. tp->read32 = tg3_read_indirect_reg32;
  11497. tp->write32 = tg3_write_indirect_reg32;
  11498. tp->read32_mbox = tg3_read_indirect_mbox;
  11499. tp->write32_mbox = tg3_write_indirect_mbox;
  11500. tp->write32_tx_mbox = tg3_write_indirect_mbox;
  11501. tp->write32_rx_mbox = tg3_write_indirect_mbox;
  11502. iounmap(tp->regs);
  11503. tp->regs = NULL;
  11504. pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
  11505. pci_cmd &= ~PCI_COMMAND_MEMORY;
  11506. pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
  11507. }
  11508. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
  11509. tp->read32_mbox = tg3_read32_mbox_5906;
  11510. tp->write32_mbox = tg3_write32_mbox_5906;
  11511. tp->write32_tx_mbox = tg3_write32_mbox_5906;
  11512. tp->write32_rx_mbox = tg3_write32_mbox_5906;
  11513. }
  11514. if (tp->write32 == tg3_write_indirect_reg32 ||
  11515. (tg3_flag(tp, PCIX_MODE) &&
  11516. (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
  11517. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
  11518. tg3_flag_set(tp, SRAM_USE_CONFIG);
  11519. /* Get eeprom hw config before calling tg3_set_power_state().
  11520. * In particular, the TG3_FLAG_IS_NIC flag must be
  11521. * determined before calling tg3_set_power_state() so that
  11522. * we know whether or not to switch out of Vaux power.
  11523. * When the flag is set, it means that GPIO1 is used for eeprom
  11524. * write protect and also implies that it is a LOM where GPIOs
  11525. * are not used to switch power.
  11526. */
  11527. tg3_get_eeprom_hw_cfg(tp);
  11528. if (tg3_flag(tp, ENABLE_APE)) {
  11529. /* Allow reads and writes to the
  11530. * APE register and memory space.
  11531. */
  11532. pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
  11533. PCISTATE_ALLOW_APE_SHMEM_WR |
  11534. PCISTATE_ALLOW_APE_PSPACE_WR;
  11535. pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
  11536. pci_state_reg);
  11537. }
  11538. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
  11539. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
  11540. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
  11541. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
  11542. tg3_flag(tp, 57765_PLUS))
  11543. tg3_flag_set(tp, CPMU_PRESENT);
  11544. /* Set up tp->grc_local_ctrl before calling tg3_power_up().
  11545. * GPIO1 driven high will bring 5700's external PHY out of reset.
  11546. * It is also used as eeprom write protect on LOMs.
  11547. */
  11548. tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
  11549. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
  11550. tg3_flag(tp, EEPROM_WRITE_PROT))
  11551. tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
  11552. GRC_LCLCTRL_GPIO_OUTPUT1);
  11553. /* Unused GPIO3 must be driven as output on 5752 because there
  11554. * are no pull-up resistors on unused GPIO pins.
  11555. */
  11556. else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
  11557. tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
  11558. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
  11559. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
  11560. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
  11561. tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
  11562. if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
  11563. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
  11564. /* Turn off the debug UART. */
  11565. tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
  11566. if (tg3_flag(tp, IS_NIC))
  11567. /* Keep VMain power. */
  11568. tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
  11569. GRC_LCLCTRL_GPIO_OUTPUT0;
  11570. }
  11571. /* Force the chip into D0. */
  11572. err = tg3_power_up(tp);
  11573. if (err) {
  11574. dev_err(&tp->pdev->dev, "Transition to D0 failed\n");
  11575. return err;
  11576. }
  11577. /* Derive initial jumbo mode from MTU assigned in
  11578. * ether_setup() via the alloc_etherdev() call
  11579. */
  11580. if (tp->dev->mtu > ETH_DATA_LEN && !tg3_flag(tp, 5780_CLASS))
  11581. tg3_flag_set(tp, JUMBO_RING_ENABLE);
  11582. /* Determine WakeOnLan speed to use. */
  11583. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
  11584. tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
  11585. tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
  11586. tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
  11587. tg3_flag_clear(tp, WOL_SPEED_100MB);
  11588. } else {
  11589. tg3_flag_set(tp, WOL_SPEED_100MB);
  11590. }
  11591. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
  11592. tp->phy_flags |= TG3_PHYFLG_IS_FET;
  11593. /* A few boards don't want Ethernet@WireSpeed phy feature */
  11594. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
  11595. (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
  11596. (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
  11597. (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
  11598. (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
  11599. (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
  11600. tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
  11601. if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
  11602. GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
  11603. tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
  11604. if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
  11605. tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
  11606. if (tg3_flag(tp, 5705_PLUS) &&
  11607. !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
  11608. GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
  11609. GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780 &&
  11610. !tg3_flag(tp, 57765_PLUS)) {
  11611. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
  11612. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
  11613. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
  11614. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
  11615. if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
  11616. tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
  11617. tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
  11618. if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
  11619. tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
  11620. } else
  11621. tp->phy_flags |= TG3_PHYFLG_BER_BUG;
  11622. }
  11623. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
  11624. GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
  11625. tp->phy_otp = tg3_read_otp_phycfg(tp);
  11626. if (tp->phy_otp == 0)
  11627. tp->phy_otp = TG3_OTP_DEFAULT;
  11628. }
  11629. if (tg3_flag(tp, CPMU_PRESENT))
  11630. tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
  11631. else
  11632. tp->mi_mode = MAC_MI_MODE_BASE;
  11633. tp->coalesce_mode = 0;
  11634. if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
  11635. GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
  11636. tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
  11637. /* Set these bits to enable statistics workaround. */
  11638. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
  11639. tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
  11640. tp->pci_chip_rev_id == CHIPREV_ID_5720_A0) {
  11641. tp->coalesce_mode |= HOSTCC_MODE_ATTN;
  11642. tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
  11643. }
  11644. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
  11645. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
  11646. tg3_flag_set(tp, USE_PHYLIB);
  11647. err = tg3_mdio_init(tp);
  11648. if (err)
  11649. return err;
  11650. /* Initialize data/descriptor byte/word swapping. */
  11651. val = tr32(GRC_MODE);
  11652. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
  11653. val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
  11654. GRC_MODE_WORD_SWAP_B2HRX_DATA |
  11655. GRC_MODE_B2HRX_ENABLE |
  11656. GRC_MODE_HTX2B_ENABLE |
  11657. GRC_MODE_HOST_STACKUP);
  11658. else
  11659. val &= GRC_MODE_HOST_STACKUP;
  11660. tw32(GRC_MODE, val | tp->grc_mode);
  11661. tg3_switch_clocks(tp);
  11662. /* Clear this out for sanity. */
  11663. tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
  11664. pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
  11665. &pci_state_reg);
  11666. if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
  11667. !tg3_flag(tp, PCIX_TARGET_HWBUG)) {
  11668. u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
  11669. if (chiprevid == CHIPREV_ID_5701_A0 ||
  11670. chiprevid == CHIPREV_ID_5701_B0 ||
  11671. chiprevid == CHIPREV_ID_5701_B2 ||
  11672. chiprevid == CHIPREV_ID_5701_B5) {
  11673. void __iomem *sram_base;
  11674. /* Write some dummy words into the SRAM status block
  11675. * area, see if it reads back correctly. If the return
  11676. * value is bad, force enable the PCIX workaround.
  11677. */
  11678. sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
  11679. writel(0x00000000, sram_base);
  11680. writel(0x00000000, sram_base + 4);
  11681. writel(0xffffffff, sram_base + 4);
  11682. if (readl(sram_base) != 0x00000000)
  11683. tg3_flag_set(tp, PCIX_TARGET_HWBUG);
  11684. }
  11685. }
  11686. udelay(50);
  11687. tg3_nvram_init(tp);
  11688. grc_misc_cfg = tr32(GRC_MISC_CFG);
  11689. grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
  11690. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
  11691. (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
  11692. grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
  11693. tg3_flag_set(tp, IS_5788);
  11694. if (!tg3_flag(tp, IS_5788) &&
  11695. GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
  11696. tg3_flag_set(tp, TAGGED_STATUS);
  11697. if (tg3_flag(tp, TAGGED_STATUS)) {
  11698. tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
  11699. HOSTCC_MODE_CLRTICK_TXBD);
  11700. tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
  11701. pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
  11702. tp->misc_host_ctrl);
  11703. }
  11704. /* Preserve the APE MAC_MODE bits */
  11705. if (tg3_flag(tp, ENABLE_APE))
  11706. tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
  11707. else
  11708. tp->mac_mode = TG3_DEF_MAC_MODE;
  11709. /* these are limited to 10/100 only */
  11710. if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
  11711. (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
  11712. (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
  11713. tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
  11714. (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901 ||
  11715. tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901_2 ||
  11716. tp->pdev->device == PCI_DEVICE_ID_TIGON3_5705F)) ||
  11717. (tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
  11718. (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5751F ||
  11719. tp->pdev->device == PCI_DEVICE_ID_TIGON3_5753F ||
  11720. tp->pdev->device == PCI_DEVICE_ID_TIGON3_5787F)) ||
  11721. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790 ||
  11722. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
  11723. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
  11724. (tp->phy_flags & TG3_PHYFLG_IS_FET))
  11725. tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
  11726. err = tg3_phy_probe(tp);
  11727. if (err) {
  11728. dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
  11729. /* ... but do not return immediately ... */
  11730. tg3_mdio_fini(tp);
  11731. }
  11732. tg3_read_vpd(tp);
  11733. tg3_read_fw_ver(tp);
  11734. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
  11735. tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
  11736. } else {
  11737. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
  11738. tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
  11739. else
  11740. tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
  11741. }
  11742. /* 5700 {AX,BX} chips have a broken status block link
  11743. * change bit implementation, so we must use the
  11744. * status register in those cases.
  11745. */
  11746. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
  11747. tg3_flag_set(tp, USE_LINKCHG_REG);
  11748. else
  11749. tg3_flag_clear(tp, USE_LINKCHG_REG);
  11750. /* The led_ctrl is set during tg3_phy_probe, here we might
  11751. * have to force the link status polling mechanism based
  11752. * upon subsystem IDs.
  11753. */
  11754. if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
  11755. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
  11756. !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
  11757. tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
  11758. tg3_flag_set(tp, USE_LINKCHG_REG);
  11759. }
  11760. /* For all SERDES we poll the MAC status register. */
  11761. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
  11762. tg3_flag_set(tp, POLL_SERDES);
  11763. else
  11764. tg3_flag_clear(tp, POLL_SERDES);
  11765. tp->rx_offset = NET_IP_ALIGN;
  11766. tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
  11767. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
  11768. tg3_flag(tp, PCIX_MODE)) {
  11769. tp->rx_offset = 0;
  11770. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  11771. tp->rx_copy_thresh = ~(u16)0;
  11772. #endif
  11773. }
  11774. tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
  11775. tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
  11776. tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
  11777. tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
  11778. /* Increment the rx prod index on the rx std ring by at most
  11779. * 8 for these chips to workaround hw errata.
  11780. */
  11781. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
  11782. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
  11783. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
  11784. tp->rx_std_max_post = 8;
  11785. if (tg3_flag(tp, ASPM_WORKAROUND))
  11786. tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
  11787. PCIE_PWR_MGMT_L1_THRESH_MSK;
  11788. return err;
  11789. }
  11790. #ifdef CONFIG_SPARC
  11791. static int __devinit tg3_get_macaddr_sparc(struct tg3 *tp)
  11792. {
  11793. struct net_device *dev = tp->dev;
  11794. struct pci_dev *pdev = tp->pdev;
  11795. struct device_node *dp = pci_device_to_OF_node(pdev);
  11796. const unsigned char *addr;
  11797. int len;
  11798. addr = of_get_property(dp, "local-mac-address", &len);
  11799. if (addr && len == 6) {
  11800. memcpy(dev->dev_addr, addr, 6);
  11801. memcpy(dev->perm_addr, dev->dev_addr, 6);
  11802. return 0;
  11803. }
  11804. return -ENODEV;
  11805. }
  11806. static int __devinit tg3_get_default_macaddr_sparc(struct tg3 *tp)
  11807. {
  11808. struct net_device *dev = tp->dev;
  11809. memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
  11810. memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
  11811. return 0;
  11812. }
  11813. #endif
  11814. static int __devinit tg3_get_device_address(struct tg3 *tp)
  11815. {
  11816. struct net_device *dev = tp->dev;
  11817. u32 hi, lo, mac_offset;
  11818. int addr_ok = 0;
  11819. #ifdef CONFIG_SPARC
  11820. if (!tg3_get_macaddr_sparc(tp))
  11821. return 0;
  11822. #endif
  11823. mac_offset = 0x7c;
  11824. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
  11825. tg3_flag(tp, 5780_CLASS)) {
  11826. if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
  11827. mac_offset = 0xcc;
  11828. if (tg3_nvram_lock(tp))
  11829. tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
  11830. else
  11831. tg3_nvram_unlock(tp);
  11832. } else if (tg3_flag(tp, 5717_PLUS)) {
  11833. if (PCI_FUNC(tp->pdev->devfn) & 1)
  11834. mac_offset = 0xcc;
  11835. if (PCI_FUNC(tp->pdev->devfn) > 1)
  11836. mac_offset += 0x18c;
  11837. } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
  11838. mac_offset = 0x10;
  11839. /* First try to get it from MAC address mailbox. */
  11840. tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
  11841. if ((hi >> 16) == 0x484b) {
  11842. dev->dev_addr[0] = (hi >> 8) & 0xff;
  11843. dev->dev_addr[1] = (hi >> 0) & 0xff;
  11844. tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
  11845. dev->dev_addr[2] = (lo >> 24) & 0xff;
  11846. dev->dev_addr[3] = (lo >> 16) & 0xff;
  11847. dev->dev_addr[4] = (lo >> 8) & 0xff;
  11848. dev->dev_addr[5] = (lo >> 0) & 0xff;
  11849. /* Some old bootcode may report a 0 MAC address in SRAM */
  11850. addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
  11851. }
  11852. if (!addr_ok) {
  11853. /* Next, try NVRAM. */
  11854. if (!tg3_flag(tp, NO_NVRAM) &&
  11855. !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
  11856. !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
  11857. memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
  11858. memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
  11859. }
  11860. /* Finally just fetch it out of the MAC control regs. */
  11861. else {
  11862. hi = tr32(MAC_ADDR_0_HIGH);
  11863. lo = tr32(MAC_ADDR_0_LOW);
  11864. dev->dev_addr[5] = lo & 0xff;
  11865. dev->dev_addr[4] = (lo >> 8) & 0xff;
  11866. dev->dev_addr[3] = (lo >> 16) & 0xff;
  11867. dev->dev_addr[2] = (lo >> 24) & 0xff;
  11868. dev->dev_addr[1] = hi & 0xff;
  11869. dev->dev_addr[0] = (hi >> 8) & 0xff;
  11870. }
  11871. }
  11872. if (!is_valid_ether_addr(&dev->dev_addr[0])) {
  11873. #ifdef CONFIG_SPARC
  11874. if (!tg3_get_default_macaddr_sparc(tp))
  11875. return 0;
  11876. #endif
  11877. return -EINVAL;
  11878. }
  11879. memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
  11880. return 0;
  11881. }
  11882. #define BOUNDARY_SINGLE_CACHELINE 1
  11883. #define BOUNDARY_MULTI_CACHELINE 2
  11884. static u32 __devinit tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
  11885. {
  11886. int cacheline_size;
  11887. u8 byte;
  11888. int goal;
  11889. pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
  11890. if (byte == 0)
  11891. cacheline_size = 1024;
  11892. else
  11893. cacheline_size = (int) byte * 4;
  11894. /* On 5703 and later chips, the boundary bits have no
  11895. * effect.
  11896. */
  11897. if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
  11898. GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
  11899. !tg3_flag(tp, PCI_EXPRESS))
  11900. goto out;
  11901. #if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
  11902. goal = BOUNDARY_MULTI_CACHELINE;
  11903. #else
  11904. #if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
  11905. goal = BOUNDARY_SINGLE_CACHELINE;
  11906. #else
  11907. goal = 0;
  11908. #endif
  11909. #endif
  11910. if (tg3_flag(tp, 57765_PLUS)) {
  11911. val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
  11912. goto out;
  11913. }
  11914. if (!goal)
  11915. goto out;
  11916. /* PCI controllers on most RISC systems tend to disconnect
  11917. * when a device tries to burst across a cache-line boundary.
  11918. * Therefore, letting tg3 do so just wastes PCI bandwidth.
  11919. *
  11920. * Unfortunately, for PCI-E there are only limited
  11921. * write-side controls for this, and thus for reads
  11922. * we will still get the disconnects. We'll also waste
  11923. * these PCI cycles for both read and write for chips
  11924. * other than 5700 and 5701 which do not implement the
  11925. * boundary bits.
  11926. */
  11927. if (tg3_flag(tp, PCIX_MODE) && !tg3_flag(tp, PCI_EXPRESS)) {
  11928. switch (cacheline_size) {
  11929. case 16:
  11930. case 32:
  11931. case 64:
  11932. case 128:
  11933. if (goal == BOUNDARY_SINGLE_CACHELINE) {
  11934. val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
  11935. DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
  11936. } else {
  11937. val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
  11938. DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
  11939. }
  11940. break;
  11941. case 256:
  11942. val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
  11943. DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
  11944. break;
  11945. default:
  11946. val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
  11947. DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
  11948. break;
  11949. }
  11950. } else if (tg3_flag(tp, PCI_EXPRESS)) {
  11951. switch (cacheline_size) {
  11952. case 16:
  11953. case 32:
  11954. case 64:
  11955. if (goal == BOUNDARY_SINGLE_CACHELINE) {
  11956. val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
  11957. val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
  11958. break;
  11959. }
  11960. /* fallthrough */
  11961. case 128:
  11962. default:
  11963. val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
  11964. val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
  11965. break;
  11966. }
  11967. } else {
  11968. switch (cacheline_size) {
  11969. case 16:
  11970. if (goal == BOUNDARY_SINGLE_CACHELINE) {
  11971. val |= (DMA_RWCTRL_READ_BNDRY_16 |
  11972. DMA_RWCTRL_WRITE_BNDRY_16);
  11973. break;
  11974. }
  11975. /* fallthrough */
  11976. case 32:
  11977. if (goal == BOUNDARY_SINGLE_CACHELINE) {
  11978. val |= (DMA_RWCTRL_READ_BNDRY_32 |
  11979. DMA_RWCTRL_WRITE_BNDRY_32);
  11980. break;
  11981. }
  11982. /* fallthrough */
  11983. case 64:
  11984. if (goal == BOUNDARY_SINGLE_CACHELINE) {
  11985. val |= (DMA_RWCTRL_READ_BNDRY_64 |
  11986. DMA_RWCTRL_WRITE_BNDRY_64);
  11987. break;
  11988. }
  11989. /* fallthrough */
  11990. case 128:
  11991. if (goal == BOUNDARY_SINGLE_CACHELINE) {
  11992. val |= (DMA_RWCTRL_READ_BNDRY_128 |
  11993. DMA_RWCTRL_WRITE_BNDRY_128);
  11994. break;
  11995. }
  11996. /* fallthrough */
  11997. case 256:
  11998. val |= (DMA_RWCTRL_READ_BNDRY_256 |
  11999. DMA_RWCTRL_WRITE_BNDRY_256);
  12000. break;
  12001. case 512:
  12002. val |= (DMA_RWCTRL_READ_BNDRY_512 |
  12003. DMA_RWCTRL_WRITE_BNDRY_512);
  12004. break;
  12005. case 1024:
  12006. default:
  12007. val |= (DMA_RWCTRL_READ_BNDRY_1024 |
  12008. DMA_RWCTRL_WRITE_BNDRY_1024);
  12009. break;
  12010. }
  12011. }
  12012. out:
  12013. return val;
  12014. }
  12015. static int __devinit tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma, int size, int to_device)
  12016. {
  12017. struct tg3_internal_buffer_desc test_desc;
  12018. u32 sram_dma_descs;
  12019. int i, ret;
  12020. sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
  12021. tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
  12022. tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
  12023. tw32(RDMAC_STATUS, 0);
  12024. tw32(WDMAC_STATUS, 0);
  12025. tw32(BUFMGR_MODE, 0);
  12026. tw32(FTQ_RESET, 0);
  12027. test_desc.addr_hi = ((u64) buf_dma) >> 32;
  12028. test_desc.addr_lo = buf_dma & 0xffffffff;
  12029. test_desc.nic_mbuf = 0x00002100;
  12030. test_desc.len = size;
  12031. /*
  12032. * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
  12033. * the *second* time the tg3 driver was getting loaded after an
  12034. * initial scan.
  12035. *
  12036. * Broadcom tells me:
  12037. * ...the DMA engine is connected to the GRC block and a DMA
  12038. * reset may affect the GRC block in some unpredictable way...
  12039. * The behavior of resets to individual blocks has not been tested.
  12040. *
  12041. * Broadcom noted the GRC reset will also reset all sub-components.
  12042. */
  12043. if (to_device) {
  12044. test_desc.cqid_sqid = (13 << 8) | 2;
  12045. tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
  12046. udelay(40);
  12047. } else {
  12048. test_desc.cqid_sqid = (16 << 8) | 7;
  12049. tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
  12050. udelay(40);
  12051. }
  12052. test_desc.flags = 0x00000005;
  12053. for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
  12054. u32 val;
  12055. val = *(((u32 *)&test_desc) + i);
  12056. pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
  12057. sram_dma_descs + (i * sizeof(u32)));
  12058. pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
  12059. }
  12060. pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
  12061. if (to_device)
  12062. tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
  12063. else
  12064. tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
  12065. ret = -ENODEV;
  12066. for (i = 0; i < 40; i++) {
  12067. u32 val;
  12068. if (to_device)
  12069. val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
  12070. else
  12071. val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
  12072. if ((val & 0xffff) == sram_dma_descs) {
  12073. ret = 0;
  12074. break;
  12075. }
  12076. udelay(100);
  12077. }
  12078. return ret;
  12079. }
  12080. #define TEST_BUFFER_SIZE 0x2000
  12081. static DEFINE_PCI_DEVICE_TABLE(tg3_dma_wait_state_chipsets) = {
  12082. { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
  12083. { },
  12084. };
  12085. static int __devinit tg3_test_dma(struct tg3 *tp)
  12086. {
  12087. dma_addr_t buf_dma;
  12088. u32 *buf, saved_dma_rwctrl;
  12089. int ret = 0;
  12090. buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
  12091. &buf_dma, GFP_KERNEL);
  12092. if (!buf) {
  12093. ret = -ENOMEM;
  12094. goto out_nofree;
  12095. }
  12096. tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
  12097. (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
  12098. tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
  12099. if (tg3_flag(tp, 57765_PLUS))
  12100. goto out;
  12101. if (tg3_flag(tp, PCI_EXPRESS)) {
  12102. /* DMA read watermark not used on PCIE */
  12103. tp->dma_rwctrl |= 0x00180000;
  12104. } else if (!tg3_flag(tp, PCIX_MODE)) {
  12105. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
  12106. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
  12107. tp->dma_rwctrl |= 0x003f0000;
  12108. else
  12109. tp->dma_rwctrl |= 0x003f000f;
  12110. } else {
  12111. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
  12112. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
  12113. u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
  12114. u32 read_water = 0x7;
  12115. /* If the 5704 is behind the EPB bridge, we can
  12116. * do the less restrictive ONE_DMA workaround for
  12117. * better performance.
  12118. */
  12119. if (tg3_flag(tp, 40BIT_DMA_BUG) &&
  12120. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
  12121. tp->dma_rwctrl |= 0x8000;
  12122. else if (ccval == 0x6 || ccval == 0x7)
  12123. tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
  12124. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
  12125. read_water = 4;
  12126. /* Set bit 23 to enable PCIX hw bug fix */
  12127. tp->dma_rwctrl |=
  12128. (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
  12129. (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
  12130. (1 << 23);
  12131. } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
  12132. /* 5780 always in PCIX mode */
  12133. tp->dma_rwctrl |= 0x00144000;
  12134. } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
  12135. /* 5714 always in PCIX mode */
  12136. tp->dma_rwctrl |= 0x00148000;
  12137. } else {
  12138. tp->dma_rwctrl |= 0x001b000f;
  12139. }
  12140. }
  12141. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
  12142. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
  12143. tp->dma_rwctrl &= 0xfffffff0;
  12144. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
  12145. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
  12146. /* Remove this if it causes problems for some boards. */
  12147. tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
  12148. /* On 5700/5701 chips, we need to set this bit.
  12149. * Otherwise the chip will issue cacheline transactions
  12150. * to streamable DMA memory with not all the byte
  12151. * enables turned on. This is an error on several
  12152. * RISC PCI controllers, in particular sparc64.
  12153. *
  12154. * On 5703/5704 chips, this bit has been reassigned
  12155. * a different meaning. In particular, it is used
  12156. * on those chips to enable a PCI-X workaround.
  12157. */
  12158. tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
  12159. }
  12160. tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
  12161. #if 0
  12162. /* Unneeded, already done by tg3_get_invariants. */
  12163. tg3_switch_clocks(tp);
  12164. #endif
  12165. if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
  12166. GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
  12167. goto out;
  12168. /* It is best to perform DMA test with maximum write burst size
  12169. * to expose the 5700/5701 write DMA bug.
  12170. */
  12171. saved_dma_rwctrl = tp->dma_rwctrl;
  12172. tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
  12173. tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
  12174. while (1) {
  12175. u32 *p = buf, i;
  12176. for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
  12177. p[i] = i;
  12178. /* Send the buffer to the chip. */
  12179. ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
  12180. if (ret) {
  12181. dev_err(&tp->pdev->dev,
  12182. "%s: Buffer write failed. err = %d\n",
  12183. __func__, ret);
  12184. break;
  12185. }
  12186. #if 0
  12187. /* validate data reached card RAM correctly. */
  12188. for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
  12189. u32 val;
  12190. tg3_read_mem(tp, 0x2100 + (i*4), &val);
  12191. if (le32_to_cpu(val) != p[i]) {
  12192. dev_err(&tp->pdev->dev,
  12193. "%s: Buffer corrupted on device! "
  12194. "(%d != %d)\n", __func__, val, i);
  12195. /* ret = -ENODEV here? */
  12196. }
  12197. p[i] = 0;
  12198. }
  12199. #endif
  12200. /* Now read it back. */
  12201. ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
  12202. if (ret) {
  12203. dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
  12204. "err = %d\n", __func__, ret);
  12205. break;
  12206. }
  12207. /* Verify it. */
  12208. for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
  12209. if (p[i] == i)
  12210. continue;
  12211. if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
  12212. DMA_RWCTRL_WRITE_BNDRY_16) {
  12213. tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
  12214. tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
  12215. tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
  12216. break;
  12217. } else {
  12218. dev_err(&tp->pdev->dev,
  12219. "%s: Buffer corrupted on read back! "
  12220. "(%d != %d)\n", __func__, p[i], i);
  12221. ret = -ENODEV;
  12222. goto out;
  12223. }
  12224. }
  12225. if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
  12226. /* Success. */
  12227. ret = 0;
  12228. break;
  12229. }
  12230. }
  12231. if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
  12232. DMA_RWCTRL_WRITE_BNDRY_16) {
  12233. /* DMA test passed without adjusting DMA boundary,
  12234. * now look for chipsets that are known to expose the
  12235. * DMA bug without failing the test.
  12236. */
  12237. if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
  12238. tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
  12239. tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
  12240. } else {
  12241. /* Safe to use the calculated DMA boundary. */
  12242. tp->dma_rwctrl = saved_dma_rwctrl;
  12243. }
  12244. tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
  12245. }
  12246. out:
  12247. dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
  12248. out_nofree:
  12249. return ret;
  12250. }
  12251. static void __devinit tg3_init_bufmgr_config(struct tg3 *tp)
  12252. {
  12253. if (tg3_flag(tp, 57765_PLUS)) {
  12254. tp->bufmgr_config.mbuf_read_dma_low_water =
  12255. DEFAULT_MB_RDMA_LOW_WATER_5705;
  12256. tp->bufmgr_config.mbuf_mac_rx_low_water =
  12257. DEFAULT_MB_MACRX_LOW_WATER_57765;
  12258. tp->bufmgr_config.mbuf_high_water =
  12259. DEFAULT_MB_HIGH_WATER_57765;
  12260. tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
  12261. DEFAULT_MB_RDMA_LOW_WATER_5705;
  12262. tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
  12263. DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
  12264. tp->bufmgr_config.mbuf_high_water_jumbo =
  12265. DEFAULT_MB_HIGH_WATER_JUMBO_57765;
  12266. } else if (tg3_flag(tp, 5705_PLUS)) {
  12267. tp->bufmgr_config.mbuf_read_dma_low_water =
  12268. DEFAULT_MB_RDMA_LOW_WATER_5705;
  12269. tp->bufmgr_config.mbuf_mac_rx_low_water =
  12270. DEFAULT_MB_MACRX_LOW_WATER_5705;
  12271. tp->bufmgr_config.mbuf_high_water =
  12272. DEFAULT_MB_HIGH_WATER_5705;
  12273. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
  12274. tp->bufmgr_config.mbuf_mac_rx_low_water =
  12275. DEFAULT_MB_MACRX_LOW_WATER_5906;
  12276. tp->bufmgr_config.mbuf_high_water =
  12277. DEFAULT_MB_HIGH_WATER_5906;
  12278. }
  12279. tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
  12280. DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
  12281. tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
  12282. DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
  12283. tp->bufmgr_config.mbuf_high_water_jumbo =
  12284. DEFAULT_MB_HIGH_WATER_JUMBO_5780;
  12285. } else {
  12286. tp->bufmgr_config.mbuf_read_dma_low_water =
  12287. DEFAULT_MB_RDMA_LOW_WATER;
  12288. tp->bufmgr_config.mbuf_mac_rx_low_water =
  12289. DEFAULT_MB_MACRX_LOW_WATER;
  12290. tp->bufmgr_config.mbuf_high_water =
  12291. DEFAULT_MB_HIGH_WATER;
  12292. tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
  12293. DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
  12294. tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
  12295. DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
  12296. tp->bufmgr_config.mbuf_high_water_jumbo =
  12297. DEFAULT_MB_HIGH_WATER_JUMBO;
  12298. }
  12299. tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
  12300. tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
  12301. }
  12302. static char * __devinit tg3_phy_string(struct tg3 *tp)
  12303. {
  12304. switch (tp->phy_id & TG3_PHY_ID_MASK) {
  12305. case TG3_PHY_ID_BCM5400: return "5400";
  12306. case TG3_PHY_ID_BCM5401: return "5401";
  12307. case TG3_PHY_ID_BCM5411: return "5411";
  12308. case TG3_PHY_ID_BCM5701: return "5701";
  12309. case TG3_PHY_ID_BCM5703: return "5703";
  12310. case TG3_PHY_ID_BCM5704: return "5704";
  12311. case TG3_PHY_ID_BCM5705: return "5705";
  12312. case TG3_PHY_ID_BCM5750: return "5750";
  12313. case TG3_PHY_ID_BCM5752: return "5752";
  12314. case TG3_PHY_ID_BCM5714: return "5714";
  12315. case TG3_PHY_ID_BCM5780: return "5780";
  12316. case TG3_PHY_ID_BCM5755: return "5755";
  12317. case TG3_PHY_ID_BCM5787: return "5787";
  12318. case TG3_PHY_ID_BCM5784: return "5784";
  12319. case TG3_PHY_ID_BCM5756: return "5722/5756";
  12320. case TG3_PHY_ID_BCM5906: return "5906";
  12321. case TG3_PHY_ID_BCM5761: return "5761";
  12322. case TG3_PHY_ID_BCM5718C: return "5718C";
  12323. case TG3_PHY_ID_BCM5718S: return "5718S";
  12324. case TG3_PHY_ID_BCM57765: return "57765";
  12325. case TG3_PHY_ID_BCM5719C: return "5719C";
  12326. case TG3_PHY_ID_BCM5720C: return "5720C";
  12327. case TG3_PHY_ID_BCM8002: return "8002/serdes";
  12328. case 0: return "serdes";
  12329. default: return "unknown";
  12330. }
  12331. }
  12332. static char * __devinit tg3_bus_string(struct tg3 *tp, char *str)
  12333. {
  12334. if (tg3_flag(tp, PCI_EXPRESS)) {
  12335. strcpy(str, "PCI Express");
  12336. return str;
  12337. } else if (tg3_flag(tp, PCIX_MODE)) {
  12338. u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
  12339. strcpy(str, "PCIX:");
  12340. if ((clock_ctrl == 7) ||
  12341. ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
  12342. GRC_MISC_CFG_BOARD_ID_5704CIOBE))
  12343. strcat(str, "133MHz");
  12344. else if (clock_ctrl == 0)
  12345. strcat(str, "33MHz");
  12346. else if (clock_ctrl == 2)
  12347. strcat(str, "50MHz");
  12348. else if (clock_ctrl == 4)
  12349. strcat(str, "66MHz");
  12350. else if (clock_ctrl == 6)
  12351. strcat(str, "100MHz");
  12352. } else {
  12353. strcpy(str, "PCI:");
  12354. if (tg3_flag(tp, PCI_HIGH_SPEED))
  12355. strcat(str, "66MHz");
  12356. else
  12357. strcat(str, "33MHz");
  12358. }
  12359. if (tg3_flag(tp, PCI_32BIT))
  12360. strcat(str, ":32-bit");
  12361. else
  12362. strcat(str, ":64-bit");
  12363. return str;
  12364. }
  12365. static struct pci_dev * __devinit tg3_find_peer(struct tg3 *tp)
  12366. {
  12367. struct pci_dev *peer;
  12368. unsigned int func, devnr = tp->pdev->devfn & ~7;
  12369. for (func = 0; func < 8; func++) {
  12370. peer = pci_get_slot(tp->pdev->bus, devnr | func);
  12371. if (peer && peer != tp->pdev)
  12372. break;
  12373. pci_dev_put(peer);
  12374. }
  12375. /* 5704 can be configured in single-port mode, set peer to
  12376. * tp->pdev in that case.
  12377. */
  12378. if (!peer) {
  12379. peer = tp->pdev;
  12380. return peer;
  12381. }
  12382. /*
  12383. * We don't need to keep the refcount elevated; there's no way
  12384. * to remove one half of this device without removing the other
  12385. */
  12386. pci_dev_put(peer);
  12387. return peer;
  12388. }
  12389. static void __devinit tg3_init_coal(struct tg3 *tp)
  12390. {
  12391. struct ethtool_coalesce *ec = &tp->coal;
  12392. memset(ec, 0, sizeof(*ec));
  12393. ec->cmd = ETHTOOL_GCOALESCE;
  12394. ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
  12395. ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
  12396. ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
  12397. ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
  12398. ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
  12399. ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
  12400. ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
  12401. ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
  12402. ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
  12403. if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
  12404. HOSTCC_MODE_CLRTICK_TXBD)) {
  12405. ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
  12406. ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
  12407. ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
  12408. ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
  12409. }
  12410. if (tg3_flag(tp, 5705_PLUS)) {
  12411. ec->rx_coalesce_usecs_irq = 0;
  12412. ec->tx_coalesce_usecs_irq = 0;
  12413. ec->stats_block_coalesce_usecs = 0;
  12414. }
  12415. }
  12416. static const struct net_device_ops tg3_netdev_ops = {
  12417. .ndo_open = tg3_open,
  12418. .ndo_stop = tg3_close,
  12419. .ndo_start_xmit = tg3_start_xmit,
  12420. .ndo_get_stats64 = tg3_get_stats64,
  12421. .ndo_validate_addr = eth_validate_addr,
  12422. .ndo_set_multicast_list = tg3_set_rx_mode,
  12423. .ndo_set_mac_address = tg3_set_mac_addr,
  12424. .ndo_do_ioctl = tg3_ioctl,
  12425. .ndo_tx_timeout = tg3_tx_timeout,
  12426. .ndo_change_mtu = tg3_change_mtu,
  12427. .ndo_fix_features = tg3_fix_features,
  12428. .ndo_set_features = tg3_set_features,
  12429. #ifdef CONFIG_NET_POLL_CONTROLLER
  12430. .ndo_poll_controller = tg3_poll_controller,
  12431. #endif
  12432. };
  12433. static int __devinit tg3_init_one(struct pci_dev *pdev,
  12434. const struct pci_device_id *ent)
  12435. {
  12436. struct net_device *dev;
  12437. struct tg3 *tp;
  12438. int i, err, pm_cap;
  12439. u32 sndmbx, rcvmbx, intmbx;
  12440. char str[40];
  12441. u64 dma_mask, persist_dma_mask;
  12442. u32 features = 0;
  12443. printk_once(KERN_INFO "%s\n", version);
  12444. err = pci_enable_device(pdev);
  12445. if (err) {
  12446. dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
  12447. return err;
  12448. }
  12449. err = pci_request_regions(pdev, DRV_MODULE_NAME);
  12450. if (err) {
  12451. dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
  12452. goto err_out_disable_pdev;
  12453. }
  12454. pci_set_master(pdev);
  12455. /* Find power-management capability. */
  12456. pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
  12457. if (pm_cap == 0) {
  12458. dev_err(&pdev->dev,
  12459. "Cannot find Power Management capability, aborting\n");
  12460. err = -EIO;
  12461. goto err_out_free_res;
  12462. }
  12463. dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
  12464. if (!dev) {
  12465. dev_err(&pdev->dev, "Etherdev alloc failed, aborting\n");
  12466. err = -ENOMEM;
  12467. goto err_out_free_res;
  12468. }
  12469. SET_NETDEV_DEV(dev, &pdev->dev);
  12470. tp = netdev_priv(dev);
  12471. tp->pdev = pdev;
  12472. tp->dev = dev;
  12473. tp->pm_cap = pm_cap;
  12474. tp->rx_mode = TG3_DEF_RX_MODE;
  12475. tp->tx_mode = TG3_DEF_TX_MODE;
  12476. if (tg3_debug > 0)
  12477. tp->msg_enable = tg3_debug;
  12478. else
  12479. tp->msg_enable = TG3_DEF_MSG_ENABLE;
  12480. /* The word/byte swap controls here control register access byte
  12481. * swapping. DMA data byte swapping is controlled in the GRC_MODE
  12482. * setting below.
  12483. */
  12484. tp->misc_host_ctrl =
  12485. MISC_HOST_CTRL_MASK_PCI_INT |
  12486. MISC_HOST_CTRL_WORD_SWAP |
  12487. MISC_HOST_CTRL_INDIR_ACCESS |
  12488. MISC_HOST_CTRL_PCISTATE_RW;
  12489. /* The NONFRM (non-frame) byte/word swap controls take effect
  12490. * on descriptor entries, anything which isn't packet data.
  12491. *
  12492. * The StrongARM chips on the board (one for tx, one for rx)
  12493. * are running in big-endian mode.
  12494. */
  12495. tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
  12496. GRC_MODE_WSWAP_NONFRM_DATA);
  12497. #ifdef __BIG_ENDIAN
  12498. tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
  12499. #endif
  12500. spin_lock_init(&tp->lock);
  12501. spin_lock_init(&tp->indirect_lock);
  12502. INIT_WORK(&tp->reset_task, tg3_reset_task);
  12503. tp->regs = pci_ioremap_bar(pdev, BAR_0);
  12504. if (!tp->regs) {
  12505. dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
  12506. err = -ENOMEM;
  12507. goto err_out_free_dev;
  12508. }
  12509. tp->rx_pending = TG3_DEF_RX_RING_PENDING;
  12510. tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
  12511. dev->ethtool_ops = &tg3_ethtool_ops;
  12512. dev->watchdog_timeo = TG3_TX_TIMEOUT;
  12513. dev->netdev_ops = &tg3_netdev_ops;
  12514. dev->irq = pdev->irq;
  12515. err = tg3_get_invariants(tp);
  12516. if (err) {
  12517. dev_err(&pdev->dev,
  12518. "Problem fetching invariants of chip, aborting\n");
  12519. goto err_out_iounmap;
  12520. }
  12521. /* The EPB bridge inside 5714, 5715, and 5780 and any
  12522. * device behind the EPB cannot support DMA addresses > 40-bit.
  12523. * On 64-bit systems with IOMMU, use 40-bit dma_mask.
  12524. * On 64-bit systems without IOMMU, use 64-bit dma_mask and
  12525. * do DMA address check in tg3_start_xmit().
  12526. */
  12527. if (tg3_flag(tp, IS_5788))
  12528. persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
  12529. else if (tg3_flag(tp, 40BIT_DMA_BUG)) {
  12530. persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
  12531. #ifdef CONFIG_HIGHMEM
  12532. dma_mask = DMA_BIT_MASK(64);
  12533. #endif
  12534. } else
  12535. persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
  12536. /* Configure DMA attributes. */
  12537. if (dma_mask > DMA_BIT_MASK(32)) {
  12538. err = pci_set_dma_mask(pdev, dma_mask);
  12539. if (!err) {
  12540. features |= NETIF_F_HIGHDMA;
  12541. err = pci_set_consistent_dma_mask(pdev,
  12542. persist_dma_mask);
  12543. if (err < 0) {
  12544. dev_err(&pdev->dev, "Unable to obtain 64 bit "
  12545. "DMA for consistent allocations\n");
  12546. goto err_out_iounmap;
  12547. }
  12548. }
  12549. }
  12550. if (err || dma_mask == DMA_BIT_MASK(32)) {
  12551. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  12552. if (err) {
  12553. dev_err(&pdev->dev,
  12554. "No usable DMA configuration, aborting\n");
  12555. goto err_out_iounmap;
  12556. }
  12557. }
  12558. tg3_init_bufmgr_config(tp);
  12559. features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
  12560. /* 5700 B0 chips do not support checksumming correctly due
  12561. * to hardware bugs.
  12562. */
  12563. if (tp->pci_chip_rev_id != CHIPREV_ID_5700_B0) {
  12564. features |= NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
  12565. if (tg3_flag(tp, 5755_PLUS))
  12566. features |= NETIF_F_IPV6_CSUM;
  12567. }
  12568. /* TSO is on by default on chips that support hardware TSO.
  12569. * Firmware TSO on older chips gives lower performance, so it
  12570. * is off by default, but can be enabled using ethtool.
  12571. */
  12572. if ((tg3_flag(tp, HW_TSO_1) ||
  12573. tg3_flag(tp, HW_TSO_2) ||
  12574. tg3_flag(tp, HW_TSO_3)) &&
  12575. (features & NETIF_F_IP_CSUM))
  12576. features |= NETIF_F_TSO;
  12577. if (tg3_flag(tp, HW_TSO_2) || tg3_flag(tp, HW_TSO_3)) {
  12578. if (features & NETIF_F_IPV6_CSUM)
  12579. features |= NETIF_F_TSO6;
  12580. if (tg3_flag(tp, HW_TSO_3) ||
  12581. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
  12582. (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
  12583. GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
  12584. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
  12585. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
  12586. features |= NETIF_F_TSO_ECN;
  12587. }
  12588. dev->features |= features;
  12589. dev->vlan_features |= features;
  12590. /*
  12591. * Add loopback capability only for a subset of devices that support
  12592. * MAC-LOOPBACK. Eventually this need to be enhanced to allow INT-PHY
  12593. * loopback for the remaining devices.
  12594. */
  12595. if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
  12596. !tg3_flag(tp, CPMU_PRESENT))
  12597. /* Add the loopback capability */
  12598. features |= NETIF_F_LOOPBACK;
  12599. dev->hw_features |= features;
  12600. if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
  12601. !tg3_flag(tp, TSO_CAPABLE) &&
  12602. !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
  12603. tg3_flag_set(tp, MAX_RXPEND_64);
  12604. tp->rx_pending = 63;
  12605. }
  12606. err = tg3_get_device_address(tp);
  12607. if (err) {
  12608. dev_err(&pdev->dev,
  12609. "Could not obtain valid ethernet address, aborting\n");
  12610. goto err_out_iounmap;
  12611. }
  12612. if (tg3_flag(tp, ENABLE_APE)) {
  12613. tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
  12614. if (!tp->aperegs) {
  12615. dev_err(&pdev->dev,
  12616. "Cannot map APE registers, aborting\n");
  12617. err = -ENOMEM;
  12618. goto err_out_iounmap;
  12619. }
  12620. tg3_ape_lock_init(tp);
  12621. if (tg3_flag(tp, ENABLE_ASF))
  12622. tg3_read_dash_ver(tp);
  12623. }
  12624. /*
  12625. * Reset chip in case UNDI or EFI driver did not shutdown
  12626. * DMA self test will enable WDMAC and we'll see (spurious)
  12627. * pending DMA on the PCI bus at that point.
  12628. */
  12629. if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
  12630. (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
  12631. tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
  12632. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  12633. }
  12634. err = tg3_test_dma(tp);
  12635. if (err) {
  12636. dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
  12637. goto err_out_apeunmap;
  12638. }
  12639. intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
  12640. rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
  12641. sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
  12642. for (i = 0; i < tp->irq_max; i++) {
  12643. struct tg3_napi *tnapi = &tp->napi[i];
  12644. tnapi->tp = tp;
  12645. tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
  12646. tnapi->int_mbox = intmbx;
  12647. if (i < 4)
  12648. intmbx += 0x8;
  12649. else
  12650. intmbx += 0x4;
  12651. tnapi->consmbox = rcvmbx;
  12652. tnapi->prodmbox = sndmbx;
  12653. if (i)
  12654. tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
  12655. else
  12656. tnapi->coal_now = HOSTCC_MODE_NOW;
  12657. if (!tg3_flag(tp, SUPPORT_MSIX))
  12658. break;
  12659. /*
  12660. * If we support MSIX, we'll be using RSS. If we're using
  12661. * RSS, the first vector only handles link interrupts and the
  12662. * remaining vectors handle rx and tx interrupts. Reuse the
  12663. * mailbox values for the next iteration. The values we setup
  12664. * above are still useful for the single vectored mode.
  12665. */
  12666. if (!i)
  12667. continue;
  12668. rcvmbx += 0x8;
  12669. if (sndmbx & 0x4)
  12670. sndmbx -= 0x4;
  12671. else
  12672. sndmbx += 0xc;
  12673. }
  12674. tg3_init_coal(tp);
  12675. pci_set_drvdata(pdev, dev);
  12676. err = register_netdev(dev);
  12677. if (err) {
  12678. dev_err(&pdev->dev, "Cannot register net device, aborting\n");
  12679. goto err_out_apeunmap;
  12680. }
  12681. netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
  12682. tp->board_part_number,
  12683. tp->pci_chip_rev_id,
  12684. tg3_bus_string(tp, str),
  12685. dev->dev_addr);
  12686. if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
  12687. struct phy_device *phydev;
  12688. phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
  12689. netdev_info(dev,
  12690. "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
  12691. phydev->drv->name, dev_name(&phydev->dev));
  12692. } else {
  12693. char *ethtype;
  12694. if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
  12695. ethtype = "10/100Base-TX";
  12696. else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
  12697. ethtype = "1000Base-SX";
  12698. else
  12699. ethtype = "10/100/1000Base-T";
  12700. netdev_info(dev, "attached PHY is %s (%s Ethernet) "
  12701. "(WireSpeed[%d], EEE[%d])\n",
  12702. tg3_phy_string(tp), ethtype,
  12703. (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0,
  12704. (tp->phy_flags & TG3_PHYFLG_EEE_CAP) != 0);
  12705. }
  12706. netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
  12707. (dev->features & NETIF_F_RXCSUM) != 0,
  12708. tg3_flag(tp, USE_LINKCHG_REG) != 0,
  12709. (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
  12710. tg3_flag(tp, ENABLE_ASF) != 0,
  12711. tg3_flag(tp, TSO_CAPABLE) != 0);
  12712. netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
  12713. tp->dma_rwctrl,
  12714. pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
  12715. ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
  12716. pci_save_state(pdev);
  12717. return 0;
  12718. err_out_apeunmap:
  12719. if (tp->aperegs) {
  12720. iounmap(tp->aperegs);
  12721. tp->aperegs = NULL;
  12722. }
  12723. err_out_iounmap:
  12724. if (tp->regs) {
  12725. iounmap(tp->regs);
  12726. tp->regs = NULL;
  12727. }
  12728. err_out_free_dev:
  12729. free_netdev(dev);
  12730. err_out_free_res:
  12731. pci_release_regions(pdev);
  12732. err_out_disable_pdev:
  12733. pci_disable_device(pdev);
  12734. pci_set_drvdata(pdev, NULL);
  12735. return err;
  12736. }
  12737. static void __devexit tg3_remove_one(struct pci_dev *pdev)
  12738. {
  12739. struct net_device *dev = pci_get_drvdata(pdev);
  12740. if (dev) {
  12741. struct tg3 *tp = netdev_priv(dev);
  12742. if (tp->fw)
  12743. release_firmware(tp->fw);
  12744. cancel_work_sync(&tp->reset_task);
  12745. if (tg3_flag(tp, USE_PHYLIB)) {
  12746. tg3_phy_fini(tp);
  12747. tg3_mdio_fini(tp);
  12748. }
  12749. unregister_netdev(dev);
  12750. if (tp->aperegs) {
  12751. iounmap(tp->aperegs);
  12752. tp->aperegs = NULL;
  12753. }
  12754. if (tp->regs) {
  12755. iounmap(tp->regs);
  12756. tp->regs = NULL;
  12757. }
  12758. free_netdev(dev);
  12759. pci_release_regions(pdev);
  12760. pci_disable_device(pdev);
  12761. pci_set_drvdata(pdev, NULL);
  12762. }
  12763. }
  12764. #ifdef CONFIG_PM_SLEEP
  12765. static int tg3_suspend(struct device *device)
  12766. {
  12767. struct pci_dev *pdev = to_pci_dev(device);
  12768. struct net_device *dev = pci_get_drvdata(pdev);
  12769. struct tg3 *tp = netdev_priv(dev);
  12770. int err;
  12771. if (!netif_running(dev))
  12772. return 0;
  12773. flush_work_sync(&tp->reset_task);
  12774. tg3_phy_stop(tp);
  12775. tg3_netif_stop(tp);
  12776. del_timer_sync(&tp->timer);
  12777. tg3_full_lock(tp, 1);
  12778. tg3_disable_ints(tp);
  12779. tg3_full_unlock(tp);
  12780. netif_device_detach(dev);
  12781. tg3_full_lock(tp, 0);
  12782. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  12783. tg3_flag_clear(tp, INIT_COMPLETE);
  12784. tg3_full_unlock(tp);
  12785. err = tg3_power_down_prepare(tp);
  12786. if (err) {
  12787. int err2;
  12788. tg3_full_lock(tp, 0);
  12789. tg3_flag_set(tp, INIT_COMPLETE);
  12790. err2 = tg3_restart_hw(tp, 1);
  12791. if (err2)
  12792. goto out;
  12793. tp->timer.expires = jiffies + tp->timer_offset;
  12794. add_timer(&tp->timer);
  12795. netif_device_attach(dev);
  12796. tg3_netif_start(tp);
  12797. out:
  12798. tg3_full_unlock(tp);
  12799. if (!err2)
  12800. tg3_phy_start(tp);
  12801. }
  12802. return err;
  12803. }
  12804. static int tg3_resume(struct device *device)
  12805. {
  12806. struct pci_dev *pdev = to_pci_dev(device);
  12807. struct net_device *dev = pci_get_drvdata(pdev);
  12808. struct tg3 *tp = netdev_priv(dev);
  12809. int err;
  12810. if (!netif_running(dev))
  12811. return 0;
  12812. netif_device_attach(dev);
  12813. tg3_full_lock(tp, 0);
  12814. tg3_flag_set(tp, INIT_COMPLETE);
  12815. err = tg3_restart_hw(tp, 1);
  12816. if (err)
  12817. goto out;
  12818. tp->timer.expires = jiffies + tp->timer_offset;
  12819. add_timer(&tp->timer);
  12820. tg3_netif_start(tp);
  12821. out:
  12822. tg3_full_unlock(tp);
  12823. if (!err)
  12824. tg3_phy_start(tp);
  12825. return err;
  12826. }
  12827. static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
  12828. #define TG3_PM_OPS (&tg3_pm_ops)
  12829. #else
  12830. #define TG3_PM_OPS NULL
  12831. #endif /* CONFIG_PM_SLEEP */
  12832. /**
  12833. * tg3_io_error_detected - called when PCI error is detected
  12834. * @pdev: Pointer to PCI device
  12835. * @state: The current pci connection state
  12836. *
  12837. * This function is called after a PCI bus error affecting
  12838. * this device has been detected.
  12839. */
  12840. static pci_ers_result_t tg3_io_error_detected(struct pci_dev *pdev,
  12841. pci_channel_state_t state)
  12842. {
  12843. struct net_device *netdev = pci_get_drvdata(pdev);
  12844. struct tg3 *tp = netdev_priv(netdev);
  12845. pci_ers_result_t err = PCI_ERS_RESULT_NEED_RESET;
  12846. netdev_info(netdev, "PCI I/O error detected\n");
  12847. rtnl_lock();
  12848. if (!netif_running(netdev))
  12849. goto done;
  12850. tg3_phy_stop(tp);
  12851. tg3_netif_stop(tp);
  12852. del_timer_sync(&tp->timer);
  12853. tg3_flag_clear(tp, RESTART_TIMER);
  12854. /* Want to make sure that the reset task doesn't run */
  12855. cancel_work_sync(&tp->reset_task);
  12856. tg3_flag_clear(tp, TX_RECOVERY_PENDING);
  12857. tg3_flag_clear(tp, RESTART_TIMER);
  12858. netif_device_detach(netdev);
  12859. /* Clean up software state, even if MMIO is blocked */
  12860. tg3_full_lock(tp, 0);
  12861. tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
  12862. tg3_full_unlock(tp);
  12863. done:
  12864. if (state == pci_channel_io_perm_failure)
  12865. err = PCI_ERS_RESULT_DISCONNECT;
  12866. else
  12867. pci_disable_device(pdev);
  12868. rtnl_unlock();
  12869. return err;
  12870. }
  12871. /**
  12872. * tg3_io_slot_reset - called after the pci bus has been reset.
  12873. * @pdev: Pointer to PCI device
  12874. *
  12875. * Restart the card from scratch, as if from a cold-boot.
  12876. * At this point, the card has exprienced a hard reset,
  12877. * followed by fixups by BIOS, and has its config space
  12878. * set up identically to what it was at cold boot.
  12879. */
  12880. static pci_ers_result_t tg3_io_slot_reset(struct pci_dev *pdev)
  12881. {
  12882. struct net_device *netdev = pci_get_drvdata(pdev);
  12883. struct tg3 *tp = netdev_priv(netdev);
  12884. pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
  12885. int err;
  12886. rtnl_lock();
  12887. if (pci_enable_device(pdev)) {
  12888. netdev_err(netdev, "Cannot re-enable PCI device after reset.\n");
  12889. goto done;
  12890. }
  12891. pci_set_master(pdev);
  12892. pci_restore_state(pdev);
  12893. pci_save_state(pdev);
  12894. if (!netif_running(netdev)) {
  12895. rc = PCI_ERS_RESULT_RECOVERED;
  12896. goto done;
  12897. }
  12898. err = tg3_power_up(tp);
  12899. if (err) {
  12900. netdev_err(netdev, "Failed to restore register access.\n");
  12901. goto done;
  12902. }
  12903. rc = PCI_ERS_RESULT_RECOVERED;
  12904. done:
  12905. rtnl_unlock();
  12906. return rc;
  12907. }
  12908. /**
  12909. * tg3_io_resume - called when traffic can start flowing again.
  12910. * @pdev: Pointer to PCI device
  12911. *
  12912. * This callback is called when the error recovery driver tells
  12913. * us that its OK to resume normal operation.
  12914. */
  12915. static void tg3_io_resume(struct pci_dev *pdev)
  12916. {
  12917. struct net_device *netdev = pci_get_drvdata(pdev);
  12918. struct tg3 *tp = netdev_priv(netdev);
  12919. int err;
  12920. rtnl_lock();
  12921. if (!netif_running(netdev))
  12922. goto done;
  12923. tg3_full_lock(tp, 0);
  12924. tg3_flag_set(tp, INIT_COMPLETE);
  12925. err = tg3_restart_hw(tp, 1);
  12926. tg3_full_unlock(tp);
  12927. if (err) {
  12928. netdev_err(netdev, "Cannot restart hardware after reset.\n");
  12929. goto done;
  12930. }
  12931. netif_device_attach(netdev);
  12932. tp->timer.expires = jiffies + tp->timer_offset;
  12933. add_timer(&tp->timer);
  12934. tg3_netif_start(tp);
  12935. tg3_phy_start(tp);
  12936. done:
  12937. rtnl_unlock();
  12938. }
  12939. static struct pci_error_handlers tg3_err_handler = {
  12940. .error_detected = tg3_io_error_detected,
  12941. .slot_reset = tg3_io_slot_reset,
  12942. .resume = tg3_io_resume
  12943. };
  12944. static struct pci_driver tg3_driver = {
  12945. .name = DRV_MODULE_NAME,
  12946. .id_table = tg3_pci_tbl,
  12947. .probe = tg3_init_one,
  12948. .remove = __devexit_p(tg3_remove_one),
  12949. .err_handler = &tg3_err_handler,
  12950. .driver.pm = TG3_PM_OPS,
  12951. };
  12952. static int __init tg3_init(void)
  12953. {
  12954. return pci_register_driver(&tg3_driver);
  12955. }
  12956. static void __exit tg3_cleanup(void)
  12957. {
  12958. pci_unregister_driver(&tg3_driver);
  12959. }
  12960. module_init(tg3_init);
  12961. module_exit(tg3_cleanup);