tg3.c 396 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890
  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-2010 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/mii.h>
  34. #include <linux/phy.h>
  35. #include <linux/brcmphy.h>
  36. #include <linux/if_vlan.h>
  37. #include <linux/ip.h>
  38. #include <linux/tcp.h>
  39. #include <linux/workqueue.h>
  40. #include <linux/prefetch.h>
  41. #include <linux/dma-mapping.h>
  42. #include <linux/firmware.h>
  43. #include <net/checksum.h>
  44. #include <net/ip.h>
  45. #include <asm/system.h>
  46. #include <asm/io.h>
  47. #include <asm/byteorder.h>
  48. #include <asm/uaccess.h>
  49. #ifdef CONFIG_SPARC
  50. #include <asm/idprom.h>
  51. #include <asm/prom.h>
  52. #endif
  53. #define BAR_0 0
  54. #define BAR_2 2
  55. #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
  56. #define TG3_VLAN_TAG_USED 1
  57. #else
  58. #define TG3_VLAN_TAG_USED 0
  59. #endif
  60. #include "tg3.h"
  61. #define DRV_MODULE_NAME "tg3"
  62. #define TG3_MAJ_NUM 3
  63. #define TG3_MIN_NUM 113
  64. #define DRV_MODULE_VERSION \
  65. __stringify(TG3_MAJ_NUM) "." __stringify(TG3_MIN_NUM)
  66. #define DRV_MODULE_RELDATE "August 2, 2010"
  67. #define TG3_DEF_MAC_MODE 0
  68. #define TG3_DEF_RX_MODE 0
  69. #define TG3_DEF_TX_MODE 0
  70. #define TG3_DEF_MSG_ENABLE \
  71. (NETIF_MSG_DRV | \
  72. NETIF_MSG_PROBE | \
  73. NETIF_MSG_LINK | \
  74. NETIF_MSG_TIMER | \
  75. NETIF_MSG_IFDOWN | \
  76. NETIF_MSG_IFUP | \
  77. NETIF_MSG_RX_ERR | \
  78. NETIF_MSG_TX_ERR)
  79. /* length of time before we decide the hardware is borked,
  80. * and dev->tx_timeout() should be called to fix the problem
  81. */
  82. #define TG3_TX_TIMEOUT (5 * HZ)
  83. /* hardware minimum and maximum for a single frame's data payload */
  84. #define TG3_MIN_MTU 60
  85. #define TG3_MAX_MTU(tp) \
  86. ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) ? 9000 : 1500)
  87. /* These numbers seem to be hard coded in the NIC firmware somehow.
  88. * You can't change the ring sizes, but you can change where you place
  89. * them in the NIC onboard memory.
  90. */
  91. #define TG3_RX_RING_SIZE 512
  92. #define TG3_DEF_RX_RING_PENDING 200
  93. #define TG3_RX_JUMBO_RING_SIZE 256
  94. #define TG3_DEF_RX_JUMBO_RING_PENDING 100
  95. #define TG3_RSS_INDIR_TBL_SIZE 128
  96. /* Do not place this n-ring entries value into the tp struct itself,
  97. * we really want to expose these constants to GCC so that modulo et
  98. * al. operations are done with shifts and masks instead of with
  99. * hw multiply/modulo instructions. Another solution would be to
  100. * replace things like '% foo' with '& (foo - 1)'.
  101. */
  102. #define TG3_RX_RCB_RING_SIZE(tp) \
  103. (((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) && \
  104. !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) ? 1024 : 512)
  105. #define TG3_TX_RING_SIZE 512
  106. #define TG3_DEF_TX_RING_PENDING (TG3_TX_RING_SIZE - 1)
  107. #define TG3_RX_RING_BYTES (sizeof(struct tg3_rx_buffer_desc) * \
  108. TG3_RX_RING_SIZE)
  109. #define TG3_RX_JUMBO_RING_BYTES (sizeof(struct tg3_ext_rx_buffer_desc) * \
  110. TG3_RX_JUMBO_RING_SIZE)
  111. #define TG3_RX_RCB_RING_BYTES(tp) (sizeof(struct tg3_rx_buffer_desc) * \
  112. TG3_RX_RCB_RING_SIZE(tp))
  113. #define TG3_TX_RING_BYTES (sizeof(struct tg3_tx_buffer_desc) * \
  114. TG3_TX_RING_SIZE)
  115. #define NEXT_TX(N) (((N) + 1) & (TG3_TX_RING_SIZE - 1))
  116. #define TG3_RX_DMA_ALIGN 16
  117. #define TG3_RX_HEADROOM ALIGN(VLAN_HLEN, TG3_RX_DMA_ALIGN)
  118. #define TG3_DMA_BYTE_ENAB 64
  119. #define TG3_RX_STD_DMA_SZ 1536
  120. #define TG3_RX_JMB_DMA_SZ 9046
  121. #define TG3_RX_DMA_TO_MAP_SZ(x) ((x) + TG3_DMA_BYTE_ENAB)
  122. #define TG3_RX_STD_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_STD_DMA_SZ)
  123. #define TG3_RX_JMB_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_JMB_DMA_SZ)
  124. #define TG3_RX_STD_BUFF_RING_SIZE \
  125. (sizeof(struct ring_info) * TG3_RX_RING_SIZE)
  126. #define TG3_RX_JMB_BUFF_RING_SIZE \
  127. (sizeof(struct ring_info) * TG3_RX_JUMBO_RING_SIZE)
  128. /* Due to a hardware bug, the 5701 can only DMA to memory addresses
  129. * that are at least dword aligned when used in PCIX mode. The driver
  130. * works around this bug by double copying the packet. This workaround
  131. * is built into the normal double copy length check for efficiency.
  132. *
  133. * However, the double copy is only necessary on those architectures
  134. * where unaligned memory accesses are inefficient. For those architectures
  135. * where unaligned memory accesses incur little penalty, we can reintegrate
  136. * the 5701 in the normal rx path. Doing so saves a device structure
  137. * dereference by hardcoding the double copy threshold in place.
  138. */
  139. #define TG3_RX_COPY_THRESHOLD 256
  140. #if NET_IP_ALIGN == 0 || defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
  141. #define TG3_RX_COPY_THRESH(tp) TG3_RX_COPY_THRESHOLD
  142. #else
  143. #define TG3_RX_COPY_THRESH(tp) ((tp)->rx_copy_thresh)
  144. #endif
  145. /* minimum number of free TX descriptors required to wake up TX process */
  146. #define TG3_TX_WAKEUP_THRESH(tnapi) ((tnapi)->tx_pending / 4)
  147. #define TG3_RAW_IP_ALIGN 2
  148. /* number of ETHTOOL_GSTATS u64's */
  149. #define TG3_NUM_STATS (sizeof(struct tg3_ethtool_stats)/sizeof(u64))
  150. #define TG3_NUM_TEST 6
  151. #define TG3_FW_UPDATE_TIMEOUT_SEC 5
  152. #define FIRMWARE_TG3 "tigon/tg3.bin"
  153. #define FIRMWARE_TG3TSO "tigon/tg3_tso.bin"
  154. #define FIRMWARE_TG3TSO5 "tigon/tg3_tso5.bin"
  155. static char version[] __devinitdata =
  156. DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")";
  157. MODULE_AUTHOR("David S. Miller (davem@redhat.com) and Jeff Garzik (jgarzik@pobox.com)");
  158. MODULE_DESCRIPTION("Broadcom Tigon3 ethernet driver");
  159. MODULE_LICENSE("GPL");
  160. MODULE_VERSION(DRV_MODULE_VERSION);
  161. MODULE_FIRMWARE(FIRMWARE_TG3);
  162. MODULE_FIRMWARE(FIRMWARE_TG3TSO);
  163. MODULE_FIRMWARE(FIRMWARE_TG3TSO5);
  164. static int tg3_debug = -1; /* -1 == use TG3_DEF_MSG_ENABLE as value */
  165. module_param(tg3_debug, int, 0);
  166. MODULE_PARM_DESC(tg3_debug, "Tigon3 bitmapped debugging message enable value");
  167. static DEFINE_PCI_DEVICE_TABLE(tg3_pci_tbl) = {
  168. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5700)},
  169. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5701)},
  170. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702)},
  171. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703)},
  172. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704)},
  173. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702FE)},
  174. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705)},
  175. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705_2)},
  176. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M)},
  177. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M_2)},
  178. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702X)},
  179. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703X)},
  180. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S)},
  181. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702A3)},
  182. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703A3)},
  183. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5782)},
  184. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5788)},
  185. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5789)},
  186. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901)},
  187. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901_2)},
  188. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S_2)},
  189. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705F)},
  190. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5721)},
  191. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5722)},
  192. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751)},
  193. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751M)},
  194. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751F)},
  195. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752)},
  196. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752M)},
  197. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753)},
  198. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753M)},
  199. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753F)},
  200. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754)},
  201. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754M)},
  202. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755)},
  203. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755M)},
  204. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5756)},
  205. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5786)},
  206. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787)},
  207. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787M)},
  208. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787F)},
  209. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714)},
  210. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714S)},
  211. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715)},
  212. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715S)},
  213. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780)},
  214. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780S)},
  215. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5781)},
  216. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906)},
  217. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906M)},
  218. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5784)},
  219. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5764)},
  220. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5723)},
  221. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761)},
  222. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761E)},
  223. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761S)},
  224. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761SE)},
  225. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_G)},
  226. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_F)},
  227. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780)},
  228. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57760)},
  229. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57790)},
  230. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57788)},
  231. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717)},
  232. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5718)},
  233. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57781)},
  234. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57785)},
  235. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57761)},
  236. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57765)},
  237. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57791)},
  238. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57795)},
  239. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5719)},
  240. {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9DXX)},
  241. {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9MXX)},
  242. {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1000)},
  243. {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1001)},
  244. {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1003)},
  245. {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC9100)},
  246. {PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_TIGON3)},
  247. {}
  248. };
  249. MODULE_DEVICE_TABLE(pci, tg3_pci_tbl);
  250. static const struct {
  251. const char string[ETH_GSTRING_LEN];
  252. } ethtool_stats_keys[TG3_NUM_STATS] = {
  253. { "rx_octets" },
  254. { "rx_fragments" },
  255. { "rx_ucast_packets" },
  256. { "rx_mcast_packets" },
  257. { "rx_bcast_packets" },
  258. { "rx_fcs_errors" },
  259. { "rx_align_errors" },
  260. { "rx_xon_pause_rcvd" },
  261. { "rx_xoff_pause_rcvd" },
  262. { "rx_mac_ctrl_rcvd" },
  263. { "rx_xoff_entered" },
  264. { "rx_frame_too_long_errors" },
  265. { "rx_jabbers" },
  266. { "rx_undersize_packets" },
  267. { "rx_in_length_errors" },
  268. { "rx_out_length_errors" },
  269. { "rx_64_or_less_octet_packets" },
  270. { "rx_65_to_127_octet_packets" },
  271. { "rx_128_to_255_octet_packets" },
  272. { "rx_256_to_511_octet_packets" },
  273. { "rx_512_to_1023_octet_packets" },
  274. { "rx_1024_to_1522_octet_packets" },
  275. { "rx_1523_to_2047_octet_packets" },
  276. { "rx_2048_to_4095_octet_packets" },
  277. { "rx_4096_to_8191_octet_packets" },
  278. { "rx_8192_to_9022_octet_packets" },
  279. { "tx_octets" },
  280. { "tx_collisions" },
  281. { "tx_xon_sent" },
  282. { "tx_xoff_sent" },
  283. { "tx_flow_control" },
  284. { "tx_mac_errors" },
  285. { "tx_single_collisions" },
  286. { "tx_mult_collisions" },
  287. { "tx_deferred" },
  288. { "tx_excessive_collisions" },
  289. { "tx_late_collisions" },
  290. { "tx_collide_2times" },
  291. { "tx_collide_3times" },
  292. { "tx_collide_4times" },
  293. { "tx_collide_5times" },
  294. { "tx_collide_6times" },
  295. { "tx_collide_7times" },
  296. { "tx_collide_8times" },
  297. { "tx_collide_9times" },
  298. { "tx_collide_10times" },
  299. { "tx_collide_11times" },
  300. { "tx_collide_12times" },
  301. { "tx_collide_13times" },
  302. { "tx_collide_14times" },
  303. { "tx_collide_15times" },
  304. { "tx_ucast_packets" },
  305. { "tx_mcast_packets" },
  306. { "tx_bcast_packets" },
  307. { "tx_carrier_sense_errors" },
  308. { "tx_discards" },
  309. { "tx_errors" },
  310. { "dma_writeq_full" },
  311. { "dma_write_prioq_full" },
  312. { "rxbds_empty" },
  313. { "rx_discards" },
  314. { "rx_errors" },
  315. { "rx_threshold_hit" },
  316. { "dma_readq_full" },
  317. { "dma_read_prioq_full" },
  318. { "tx_comp_queue_full" },
  319. { "ring_set_send_prod_index" },
  320. { "ring_status_update" },
  321. { "nic_irqs" },
  322. { "nic_avoided_irqs" },
  323. { "nic_tx_threshold_hit" }
  324. };
  325. static const struct {
  326. const char string[ETH_GSTRING_LEN];
  327. } ethtool_test_keys[TG3_NUM_TEST] = {
  328. { "nvram test (online) " },
  329. { "link test (online) " },
  330. { "register test (offline)" },
  331. { "memory test (offline)" },
  332. { "loopback test (offline)" },
  333. { "interrupt test (offline)" },
  334. };
  335. static void tg3_write32(struct tg3 *tp, u32 off, u32 val)
  336. {
  337. writel(val, tp->regs + off);
  338. }
  339. static u32 tg3_read32(struct tg3 *tp, u32 off)
  340. {
  341. return readl(tp->regs + off);
  342. }
  343. static void tg3_ape_write32(struct tg3 *tp, u32 off, u32 val)
  344. {
  345. writel(val, tp->aperegs + off);
  346. }
  347. static u32 tg3_ape_read32(struct tg3 *tp, u32 off)
  348. {
  349. return readl(tp->aperegs + off);
  350. }
  351. static void tg3_write_indirect_reg32(struct tg3 *tp, u32 off, u32 val)
  352. {
  353. unsigned long flags;
  354. spin_lock_irqsave(&tp->indirect_lock, flags);
  355. pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
  356. pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
  357. spin_unlock_irqrestore(&tp->indirect_lock, flags);
  358. }
  359. static void tg3_write_flush_reg32(struct tg3 *tp, u32 off, u32 val)
  360. {
  361. writel(val, tp->regs + off);
  362. readl(tp->regs + off);
  363. }
  364. static u32 tg3_read_indirect_reg32(struct tg3 *tp, u32 off)
  365. {
  366. unsigned long flags;
  367. u32 val;
  368. spin_lock_irqsave(&tp->indirect_lock, flags);
  369. pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
  370. pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
  371. spin_unlock_irqrestore(&tp->indirect_lock, flags);
  372. return val;
  373. }
  374. static void tg3_write_indirect_mbox(struct tg3 *tp, u32 off, u32 val)
  375. {
  376. unsigned long flags;
  377. if (off == (MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW)) {
  378. pci_write_config_dword(tp->pdev, TG3PCI_RCV_RET_RING_CON_IDX +
  379. TG3_64BIT_REG_LOW, val);
  380. return;
  381. }
  382. if (off == TG3_RX_STD_PROD_IDX_REG) {
  383. pci_write_config_dword(tp->pdev, TG3PCI_STD_RING_PROD_IDX +
  384. TG3_64BIT_REG_LOW, val);
  385. return;
  386. }
  387. spin_lock_irqsave(&tp->indirect_lock, flags);
  388. pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
  389. pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
  390. spin_unlock_irqrestore(&tp->indirect_lock, flags);
  391. /* In indirect mode when disabling interrupts, we also need
  392. * to clear the interrupt bit in the GRC local ctrl register.
  393. */
  394. if ((off == (MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW)) &&
  395. (val == 0x1)) {
  396. pci_write_config_dword(tp->pdev, TG3PCI_MISC_LOCAL_CTRL,
  397. tp->grc_local_ctrl|GRC_LCLCTRL_CLEARINT);
  398. }
  399. }
  400. static u32 tg3_read_indirect_mbox(struct tg3 *tp, u32 off)
  401. {
  402. unsigned long flags;
  403. u32 val;
  404. spin_lock_irqsave(&tp->indirect_lock, flags);
  405. pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
  406. pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
  407. spin_unlock_irqrestore(&tp->indirect_lock, flags);
  408. return val;
  409. }
  410. /* usec_wait specifies the wait time in usec when writing to certain registers
  411. * where it is unsafe to read back the register without some delay.
  412. * GRC_LOCAL_CTRL is one example if the GPIOs are toggled to switch power.
  413. * TG3PCI_CLOCK_CTRL is another example if the clock frequencies are changed.
  414. */
  415. static void _tw32_flush(struct tg3 *tp, u32 off, u32 val, u32 usec_wait)
  416. {
  417. if ((tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG) ||
  418. (tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND))
  419. /* Non-posted methods */
  420. tp->write32(tp, off, val);
  421. else {
  422. /* Posted method */
  423. tg3_write32(tp, off, val);
  424. if (usec_wait)
  425. udelay(usec_wait);
  426. tp->read32(tp, off);
  427. }
  428. /* Wait again after the read for the posted method to guarantee that
  429. * the wait time is met.
  430. */
  431. if (usec_wait)
  432. udelay(usec_wait);
  433. }
  434. static inline void tw32_mailbox_flush(struct tg3 *tp, u32 off, u32 val)
  435. {
  436. tp->write32_mbox(tp, off, val);
  437. if (!(tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER) &&
  438. !(tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND))
  439. tp->read32_mbox(tp, off);
  440. }
  441. static void tg3_write32_tx_mbox(struct tg3 *tp, u32 off, u32 val)
  442. {
  443. void __iomem *mbox = tp->regs + off;
  444. writel(val, mbox);
  445. if (tp->tg3_flags & TG3_FLAG_TXD_MBOX_HWBUG)
  446. writel(val, mbox);
  447. if (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)
  448. readl(mbox);
  449. }
  450. static u32 tg3_read32_mbox_5906(struct tg3 *tp, u32 off)
  451. {
  452. return readl(tp->regs + off + GRCMBOX_BASE);
  453. }
  454. static void tg3_write32_mbox_5906(struct tg3 *tp, u32 off, u32 val)
  455. {
  456. writel(val, tp->regs + off + GRCMBOX_BASE);
  457. }
  458. #define tw32_mailbox(reg, val) tp->write32_mbox(tp, reg, val)
  459. #define tw32_mailbox_f(reg, val) tw32_mailbox_flush(tp, (reg), (val))
  460. #define tw32_rx_mbox(reg, val) tp->write32_rx_mbox(tp, reg, val)
  461. #define tw32_tx_mbox(reg, val) tp->write32_tx_mbox(tp, reg, val)
  462. #define tr32_mailbox(reg) tp->read32_mbox(tp, reg)
  463. #define tw32(reg, val) tp->write32(tp, reg, val)
  464. #define tw32_f(reg, val) _tw32_flush(tp, (reg), (val), 0)
  465. #define tw32_wait_f(reg, val, us) _tw32_flush(tp, (reg), (val), (us))
  466. #define tr32(reg) tp->read32(tp, reg)
  467. static void tg3_write_mem(struct tg3 *tp, u32 off, u32 val)
  468. {
  469. unsigned long flags;
  470. if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) &&
  471. (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC))
  472. return;
  473. spin_lock_irqsave(&tp->indirect_lock, flags);
  474. if (tp->tg3_flags & TG3_FLAG_SRAM_USE_CONFIG) {
  475. pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
  476. pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
  477. /* Always leave this as zero. */
  478. pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
  479. } else {
  480. tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
  481. tw32_f(TG3PCI_MEM_WIN_DATA, val);
  482. /* Always leave this as zero. */
  483. tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
  484. }
  485. spin_unlock_irqrestore(&tp->indirect_lock, flags);
  486. }
  487. static void tg3_read_mem(struct tg3 *tp, u32 off, u32 *val)
  488. {
  489. unsigned long flags;
  490. if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) &&
  491. (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC)) {
  492. *val = 0;
  493. return;
  494. }
  495. spin_lock_irqsave(&tp->indirect_lock, flags);
  496. if (tp->tg3_flags & TG3_FLAG_SRAM_USE_CONFIG) {
  497. pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
  498. pci_read_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
  499. /* Always leave this as zero. */
  500. pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
  501. } else {
  502. tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
  503. *val = tr32(TG3PCI_MEM_WIN_DATA);
  504. /* Always leave this as zero. */
  505. tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
  506. }
  507. spin_unlock_irqrestore(&tp->indirect_lock, flags);
  508. }
  509. static void tg3_ape_lock_init(struct tg3 *tp)
  510. {
  511. int i;
  512. u32 regbase;
  513. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
  514. regbase = TG3_APE_LOCK_GRANT;
  515. else
  516. regbase = TG3_APE_PER_LOCK_GRANT;
  517. /* Make sure the driver hasn't any stale locks. */
  518. for (i = 0; i < 8; i++)
  519. tg3_ape_write32(tp, regbase + 4 * i, APE_LOCK_GRANT_DRIVER);
  520. }
  521. static int tg3_ape_lock(struct tg3 *tp, int locknum)
  522. {
  523. int i, off;
  524. int ret = 0;
  525. u32 status, req, gnt;
  526. if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
  527. return 0;
  528. switch (locknum) {
  529. case TG3_APE_LOCK_GRC:
  530. case TG3_APE_LOCK_MEM:
  531. break;
  532. default:
  533. return -EINVAL;
  534. }
  535. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
  536. req = TG3_APE_LOCK_REQ;
  537. gnt = TG3_APE_LOCK_GRANT;
  538. } else {
  539. req = TG3_APE_PER_LOCK_REQ;
  540. gnt = TG3_APE_PER_LOCK_GRANT;
  541. }
  542. off = 4 * locknum;
  543. tg3_ape_write32(tp, req + off, APE_LOCK_REQ_DRIVER);
  544. /* Wait for up to 1 millisecond to acquire lock. */
  545. for (i = 0; i < 100; i++) {
  546. status = tg3_ape_read32(tp, gnt + off);
  547. if (status == APE_LOCK_GRANT_DRIVER)
  548. break;
  549. udelay(10);
  550. }
  551. if (status != APE_LOCK_GRANT_DRIVER) {
  552. /* Revoke the lock request. */
  553. tg3_ape_write32(tp, gnt + off,
  554. APE_LOCK_GRANT_DRIVER);
  555. ret = -EBUSY;
  556. }
  557. return ret;
  558. }
  559. static void tg3_ape_unlock(struct tg3 *tp, int locknum)
  560. {
  561. u32 gnt;
  562. if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
  563. return;
  564. switch (locknum) {
  565. case TG3_APE_LOCK_GRC:
  566. case TG3_APE_LOCK_MEM:
  567. break;
  568. default:
  569. return;
  570. }
  571. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
  572. gnt = TG3_APE_LOCK_GRANT;
  573. else
  574. gnt = TG3_APE_PER_LOCK_GRANT;
  575. tg3_ape_write32(tp, gnt + 4 * locknum, APE_LOCK_GRANT_DRIVER);
  576. }
  577. static void tg3_disable_ints(struct tg3 *tp)
  578. {
  579. int i;
  580. tw32(TG3PCI_MISC_HOST_CTRL,
  581. (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
  582. for (i = 0; i < tp->irq_max; i++)
  583. tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
  584. }
  585. static void tg3_enable_ints(struct tg3 *tp)
  586. {
  587. int i;
  588. tp->irq_sync = 0;
  589. wmb();
  590. tw32(TG3PCI_MISC_HOST_CTRL,
  591. (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
  592. tp->coal_now = tp->coalesce_mode | HOSTCC_MODE_ENABLE;
  593. for (i = 0; i < tp->irq_cnt; i++) {
  594. struct tg3_napi *tnapi = &tp->napi[i];
  595. tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
  596. if (tp->tg3_flags2 & TG3_FLG2_1SHOT_MSI)
  597. tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
  598. tp->coal_now |= tnapi->coal_now;
  599. }
  600. /* Force an initial interrupt */
  601. if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) &&
  602. (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
  603. tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
  604. else
  605. tw32(HOSTCC_MODE, tp->coal_now);
  606. tp->coal_now &= ~(tp->napi[0].coal_now | tp->napi[1].coal_now);
  607. }
  608. static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
  609. {
  610. struct tg3 *tp = tnapi->tp;
  611. struct tg3_hw_status *sblk = tnapi->hw_status;
  612. unsigned int work_exists = 0;
  613. /* check for phy events */
  614. if (!(tp->tg3_flags &
  615. (TG3_FLAG_USE_LINKCHG_REG |
  616. TG3_FLAG_POLL_SERDES))) {
  617. if (sblk->status & SD_STATUS_LINK_CHG)
  618. work_exists = 1;
  619. }
  620. /* check for RX/TX work to do */
  621. if (sblk->idx[0].tx_consumer != tnapi->tx_cons ||
  622. *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
  623. work_exists = 1;
  624. return work_exists;
  625. }
  626. /* tg3_int_reenable
  627. * similar to tg3_enable_ints, but it accurately determines whether there
  628. * is new work pending and can return without flushing the PIO write
  629. * which reenables interrupts
  630. */
  631. static void tg3_int_reenable(struct tg3_napi *tnapi)
  632. {
  633. struct tg3 *tp = tnapi->tp;
  634. tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
  635. mmiowb();
  636. /* When doing tagged status, this work check is unnecessary.
  637. * The last_tag we write above tells the chip which piece of
  638. * work we've completed.
  639. */
  640. if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) &&
  641. tg3_has_work(tnapi))
  642. tw32(HOSTCC_MODE, tp->coalesce_mode |
  643. HOSTCC_MODE_ENABLE | tnapi->coal_now);
  644. }
  645. static void tg3_switch_clocks(struct tg3 *tp)
  646. {
  647. u32 clock_ctrl;
  648. u32 orig_clock_ctrl;
  649. if ((tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) ||
  650. (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
  651. return;
  652. clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
  653. orig_clock_ctrl = clock_ctrl;
  654. clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
  655. CLOCK_CTRL_CLKRUN_OENABLE |
  656. 0x1f);
  657. tp->pci_clock_ctrl = clock_ctrl;
  658. if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
  659. if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
  660. tw32_wait_f(TG3PCI_CLOCK_CTRL,
  661. clock_ctrl | CLOCK_CTRL_625_CORE, 40);
  662. }
  663. } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
  664. tw32_wait_f(TG3PCI_CLOCK_CTRL,
  665. clock_ctrl |
  666. (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
  667. 40);
  668. tw32_wait_f(TG3PCI_CLOCK_CTRL,
  669. clock_ctrl | (CLOCK_CTRL_ALTCLK),
  670. 40);
  671. }
  672. tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
  673. }
  674. #define PHY_BUSY_LOOPS 5000
  675. static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
  676. {
  677. u32 frame_val;
  678. unsigned int loops;
  679. int ret;
  680. if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
  681. tw32_f(MAC_MI_MODE,
  682. (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
  683. udelay(80);
  684. }
  685. *val = 0x0;
  686. frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
  687. MI_COM_PHY_ADDR_MASK);
  688. frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
  689. MI_COM_REG_ADDR_MASK);
  690. frame_val |= (MI_COM_CMD_READ | MI_COM_START);
  691. tw32_f(MAC_MI_COM, frame_val);
  692. loops = PHY_BUSY_LOOPS;
  693. while (loops != 0) {
  694. udelay(10);
  695. frame_val = tr32(MAC_MI_COM);
  696. if ((frame_val & MI_COM_BUSY) == 0) {
  697. udelay(5);
  698. frame_val = tr32(MAC_MI_COM);
  699. break;
  700. }
  701. loops -= 1;
  702. }
  703. ret = -EBUSY;
  704. if (loops != 0) {
  705. *val = frame_val & MI_COM_DATA_MASK;
  706. ret = 0;
  707. }
  708. if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
  709. tw32_f(MAC_MI_MODE, tp->mi_mode);
  710. udelay(80);
  711. }
  712. return ret;
  713. }
  714. static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
  715. {
  716. u32 frame_val;
  717. unsigned int loops;
  718. int ret;
  719. if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
  720. (reg == MII_TG3_CTRL || reg == MII_TG3_AUX_CTRL))
  721. return 0;
  722. if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
  723. tw32_f(MAC_MI_MODE,
  724. (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
  725. udelay(80);
  726. }
  727. frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
  728. MI_COM_PHY_ADDR_MASK);
  729. frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
  730. MI_COM_REG_ADDR_MASK);
  731. frame_val |= (val & MI_COM_DATA_MASK);
  732. frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
  733. tw32_f(MAC_MI_COM, frame_val);
  734. loops = PHY_BUSY_LOOPS;
  735. while (loops != 0) {
  736. udelay(10);
  737. frame_val = tr32(MAC_MI_COM);
  738. if ((frame_val & MI_COM_BUSY) == 0) {
  739. udelay(5);
  740. frame_val = tr32(MAC_MI_COM);
  741. break;
  742. }
  743. loops -= 1;
  744. }
  745. ret = -EBUSY;
  746. if (loops != 0)
  747. ret = 0;
  748. if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
  749. tw32_f(MAC_MI_MODE, tp->mi_mode);
  750. udelay(80);
  751. }
  752. return ret;
  753. }
  754. static int tg3_bmcr_reset(struct tg3 *tp)
  755. {
  756. u32 phy_control;
  757. int limit, err;
  758. /* OK, reset it, and poll the BMCR_RESET bit until it
  759. * clears or we time out.
  760. */
  761. phy_control = BMCR_RESET;
  762. err = tg3_writephy(tp, MII_BMCR, phy_control);
  763. if (err != 0)
  764. return -EBUSY;
  765. limit = 5000;
  766. while (limit--) {
  767. err = tg3_readphy(tp, MII_BMCR, &phy_control);
  768. if (err != 0)
  769. return -EBUSY;
  770. if ((phy_control & BMCR_RESET) == 0) {
  771. udelay(40);
  772. break;
  773. }
  774. udelay(10);
  775. }
  776. if (limit < 0)
  777. return -EBUSY;
  778. return 0;
  779. }
  780. static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
  781. {
  782. struct tg3 *tp = bp->priv;
  783. u32 val;
  784. spin_lock_bh(&tp->lock);
  785. if (tg3_readphy(tp, reg, &val))
  786. val = -EIO;
  787. spin_unlock_bh(&tp->lock);
  788. return val;
  789. }
  790. static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
  791. {
  792. struct tg3 *tp = bp->priv;
  793. u32 ret = 0;
  794. spin_lock_bh(&tp->lock);
  795. if (tg3_writephy(tp, reg, val))
  796. ret = -EIO;
  797. spin_unlock_bh(&tp->lock);
  798. return ret;
  799. }
  800. static int tg3_mdio_reset(struct mii_bus *bp)
  801. {
  802. return 0;
  803. }
  804. static void tg3_mdio_config_5785(struct tg3 *tp)
  805. {
  806. u32 val;
  807. struct phy_device *phydev;
  808. phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
  809. switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
  810. case PHY_ID_BCM50610:
  811. case PHY_ID_BCM50610M:
  812. val = MAC_PHYCFG2_50610_LED_MODES;
  813. break;
  814. case PHY_ID_BCMAC131:
  815. val = MAC_PHYCFG2_AC131_LED_MODES;
  816. break;
  817. case PHY_ID_RTL8211C:
  818. val = MAC_PHYCFG2_RTL8211C_LED_MODES;
  819. break;
  820. case PHY_ID_RTL8201E:
  821. val = MAC_PHYCFG2_RTL8201E_LED_MODES;
  822. break;
  823. default:
  824. return;
  825. }
  826. if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
  827. tw32(MAC_PHYCFG2, val);
  828. val = tr32(MAC_PHYCFG1);
  829. val &= ~(MAC_PHYCFG1_RGMII_INT |
  830. MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
  831. val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
  832. tw32(MAC_PHYCFG1, val);
  833. return;
  834. }
  835. if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE))
  836. val |= MAC_PHYCFG2_EMODE_MASK_MASK |
  837. MAC_PHYCFG2_FMODE_MASK_MASK |
  838. MAC_PHYCFG2_GMODE_MASK_MASK |
  839. MAC_PHYCFG2_ACT_MASK_MASK |
  840. MAC_PHYCFG2_QUAL_MASK_MASK |
  841. MAC_PHYCFG2_INBAND_ENABLE;
  842. tw32(MAC_PHYCFG2, val);
  843. val = tr32(MAC_PHYCFG1);
  844. val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
  845. MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
  846. if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE)) {
  847. if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
  848. val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
  849. if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
  850. val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
  851. }
  852. val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
  853. MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
  854. tw32(MAC_PHYCFG1, val);
  855. val = tr32(MAC_EXT_RGMII_MODE);
  856. val &= ~(MAC_RGMII_MODE_RX_INT_B |
  857. MAC_RGMII_MODE_RX_QUALITY |
  858. MAC_RGMII_MODE_RX_ACTIVITY |
  859. MAC_RGMII_MODE_RX_ENG_DET |
  860. MAC_RGMII_MODE_TX_ENABLE |
  861. MAC_RGMII_MODE_TX_LOWPWR |
  862. MAC_RGMII_MODE_TX_RESET);
  863. if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE)) {
  864. if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
  865. val |= MAC_RGMII_MODE_RX_INT_B |
  866. MAC_RGMII_MODE_RX_QUALITY |
  867. MAC_RGMII_MODE_RX_ACTIVITY |
  868. MAC_RGMII_MODE_RX_ENG_DET;
  869. if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
  870. val |= MAC_RGMII_MODE_TX_ENABLE |
  871. MAC_RGMII_MODE_TX_LOWPWR |
  872. MAC_RGMII_MODE_TX_RESET;
  873. }
  874. tw32(MAC_EXT_RGMII_MODE, val);
  875. }
  876. static void tg3_mdio_start(struct tg3 *tp)
  877. {
  878. tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
  879. tw32_f(MAC_MI_MODE, tp->mi_mode);
  880. udelay(80);
  881. if ((tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED) &&
  882. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
  883. tg3_mdio_config_5785(tp);
  884. }
  885. static int tg3_mdio_init(struct tg3 *tp)
  886. {
  887. int i;
  888. u32 reg;
  889. struct phy_device *phydev;
  890. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
  891. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719) {
  892. u32 is_serdes;
  893. tp->phy_addr = PCI_FUNC(tp->pdev->devfn) + 1;
  894. if (tp->pci_chip_rev_id != CHIPREV_ID_5717_A0)
  895. is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
  896. else
  897. is_serdes = tr32(TG3_CPMU_PHY_STRAP) &
  898. TG3_CPMU_PHY_STRAP_IS_SERDES;
  899. if (is_serdes)
  900. tp->phy_addr += 7;
  901. } else
  902. tp->phy_addr = TG3_PHY_MII_ADDR;
  903. tg3_mdio_start(tp);
  904. if (!(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) ||
  905. (tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED))
  906. return 0;
  907. tp->mdio_bus = mdiobus_alloc();
  908. if (tp->mdio_bus == NULL)
  909. return -ENOMEM;
  910. tp->mdio_bus->name = "tg3 mdio bus";
  911. snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
  912. (tp->pdev->bus->number << 8) | tp->pdev->devfn);
  913. tp->mdio_bus->priv = tp;
  914. tp->mdio_bus->parent = &tp->pdev->dev;
  915. tp->mdio_bus->read = &tg3_mdio_read;
  916. tp->mdio_bus->write = &tg3_mdio_write;
  917. tp->mdio_bus->reset = &tg3_mdio_reset;
  918. tp->mdio_bus->phy_mask = ~(1 << TG3_PHY_MII_ADDR);
  919. tp->mdio_bus->irq = &tp->mdio_irq[0];
  920. for (i = 0; i < PHY_MAX_ADDR; i++)
  921. tp->mdio_bus->irq[i] = PHY_POLL;
  922. /* The bus registration will look for all the PHYs on the mdio bus.
  923. * Unfortunately, it does not ensure the PHY is powered up before
  924. * accessing the PHY ID registers. A chip reset is the
  925. * quickest way to bring the device back to an operational state..
  926. */
  927. if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
  928. tg3_bmcr_reset(tp);
  929. i = mdiobus_register(tp->mdio_bus);
  930. if (i) {
  931. dev_warn(&tp->pdev->dev, "mdiobus_reg failed (0x%x)\n", i);
  932. mdiobus_free(tp->mdio_bus);
  933. return i;
  934. }
  935. phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
  936. if (!phydev || !phydev->drv) {
  937. dev_warn(&tp->pdev->dev, "No PHY devices\n");
  938. mdiobus_unregister(tp->mdio_bus);
  939. mdiobus_free(tp->mdio_bus);
  940. return -ENODEV;
  941. }
  942. switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
  943. case PHY_ID_BCM57780:
  944. phydev->interface = PHY_INTERFACE_MODE_GMII;
  945. phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
  946. break;
  947. case PHY_ID_BCM50610:
  948. case PHY_ID_BCM50610M:
  949. phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
  950. PHY_BRCM_RX_REFCLK_UNUSED |
  951. PHY_BRCM_DIS_TXCRXC_NOENRGY |
  952. PHY_BRCM_AUTO_PWRDWN_ENABLE;
  953. if (tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE)
  954. phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
  955. if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
  956. phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
  957. if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
  958. phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
  959. /* fallthru */
  960. case PHY_ID_RTL8211C:
  961. phydev->interface = PHY_INTERFACE_MODE_RGMII;
  962. break;
  963. case PHY_ID_RTL8201E:
  964. case PHY_ID_BCMAC131:
  965. phydev->interface = PHY_INTERFACE_MODE_MII;
  966. phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
  967. tp->phy_flags |= TG3_PHYFLG_IS_FET;
  968. break;
  969. }
  970. tp->tg3_flags3 |= TG3_FLG3_MDIOBUS_INITED;
  971. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
  972. tg3_mdio_config_5785(tp);
  973. return 0;
  974. }
  975. static void tg3_mdio_fini(struct tg3 *tp)
  976. {
  977. if (tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED) {
  978. tp->tg3_flags3 &= ~TG3_FLG3_MDIOBUS_INITED;
  979. mdiobus_unregister(tp->mdio_bus);
  980. mdiobus_free(tp->mdio_bus);
  981. }
  982. }
  983. /* tp->lock is held. */
  984. static inline void tg3_generate_fw_event(struct tg3 *tp)
  985. {
  986. u32 val;
  987. val = tr32(GRC_RX_CPU_EVENT);
  988. val |= GRC_RX_CPU_DRIVER_EVENT;
  989. tw32_f(GRC_RX_CPU_EVENT, val);
  990. tp->last_event_jiffies = jiffies;
  991. }
  992. #define TG3_FW_EVENT_TIMEOUT_USEC 2500
  993. /* tp->lock is held. */
  994. static void tg3_wait_for_event_ack(struct tg3 *tp)
  995. {
  996. int i;
  997. unsigned int delay_cnt;
  998. long time_remain;
  999. /* If enough time has passed, no wait is necessary. */
  1000. time_remain = (long)(tp->last_event_jiffies + 1 +
  1001. usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
  1002. (long)jiffies;
  1003. if (time_remain < 0)
  1004. return;
  1005. /* Check if we can shorten the wait time. */
  1006. delay_cnt = jiffies_to_usecs(time_remain);
  1007. if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
  1008. delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
  1009. delay_cnt = (delay_cnt >> 3) + 1;
  1010. for (i = 0; i < delay_cnt; i++) {
  1011. if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
  1012. break;
  1013. udelay(8);
  1014. }
  1015. }
  1016. /* tp->lock is held. */
  1017. static void tg3_ump_link_report(struct tg3 *tp)
  1018. {
  1019. u32 reg;
  1020. u32 val;
  1021. if (!(tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
  1022. !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
  1023. return;
  1024. tg3_wait_for_event_ack(tp);
  1025. tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
  1026. tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
  1027. val = 0;
  1028. if (!tg3_readphy(tp, MII_BMCR, &reg))
  1029. val = reg << 16;
  1030. if (!tg3_readphy(tp, MII_BMSR, &reg))
  1031. val |= (reg & 0xffff);
  1032. tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX, val);
  1033. val = 0;
  1034. if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
  1035. val = reg << 16;
  1036. if (!tg3_readphy(tp, MII_LPA, &reg))
  1037. val |= (reg & 0xffff);
  1038. tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 4, val);
  1039. val = 0;
  1040. if (!(tp->phy_flags & TG3_PHYFLG_MII_SERDES)) {
  1041. if (!tg3_readphy(tp, MII_CTRL1000, &reg))
  1042. val = reg << 16;
  1043. if (!tg3_readphy(tp, MII_STAT1000, &reg))
  1044. val |= (reg & 0xffff);
  1045. }
  1046. tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 8, val);
  1047. if (!tg3_readphy(tp, MII_PHYADDR, &reg))
  1048. val = reg << 16;
  1049. else
  1050. val = 0;
  1051. tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 12, val);
  1052. tg3_generate_fw_event(tp);
  1053. }
  1054. static void tg3_link_report(struct tg3 *tp)
  1055. {
  1056. if (!netif_carrier_ok(tp->dev)) {
  1057. netif_info(tp, link, tp->dev, "Link is down\n");
  1058. tg3_ump_link_report(tp);
  1059. } else if (netif_msg_link(tp)) {
  1060. netdev_info(tp->dev, "Link is up at %d Mbps, %s duplex\n",
  1061. (tp->link_config.active_speed == SPEED_1000 ?
  1062. 1000 :
  1063. (tp->link_config.active_speed == SPEED_100 ?
  1064. 100 : 10)),
  1065. (tp->link_config.active_duplex == DUPLEX_FULL ?
  1066. "full" : "half"));
  1067. netdev_info(tp->dev, "Flow control is %s for TX and %s for RX\n",
  1068. (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
  1069. "on" : "off",
  1070. (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
  1071. "on" : "off");
  1072. tg3_ump_link_report(tp);
  1073. }
  1074. }
  1075. static u16 tg3_advert_flowctrl_1000T(u8 flow_ctrl)
  1076. {
  1077. u16 miireg;
  1078. if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
  1079. miireg = ADVERTISE_PAUSE_CAP;
  1080. else if (flow_ctrl & FLOW_CTRL_TX)
  1081. miireg = ADVERTISE_PAUSE_ASYM;
  1082. else if (flow_ctrl & FLOW_CTRL_RX)
  1083. miireg = ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
  1084. else
  1085. miireg = 0;
  1086. return miireg;
  1087. }
  1088. static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
  1089. {
  1090. u16 miireg;
  1091. if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
  1092. miireg = ADVERTISE_1000XPAUSE;
  1093. else if (flow_ctrl & FLOW_CTRL_TX)
  1094. miireg = ADVERTISE_1000XPSE_ASYM;
  1095. else if (flow_ctrl & FLOW_CTRL_RX)
  1096. miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
  1097. else
  1098. miireg = 0;
  1099. return miireg;
  1100. }
  1101. static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
  1102. {
  1103. u8 cap = 0;
  1104. if (lcladv & ADVERTISE_1000XPAUSE) {
  1105. if (lcladv & ADVERTISE_1000XPSE_ASYM) {
  1106. if (rmtadv & LPA_1000XPAUSE)
  1107. cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
  1108. else if (rmtadv & LPA_1000XPAUSE_ASYM)
  1109. cap = FLOW_CTRL_RX;
  1110. } else {
  1111. if (rmtadv & LPA_1000XPAUSE)
  1112. cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
  1113. }
  1114. } else if (lcladv & ADVERTISE_1000XPSE_ASYM) {
  1115. if ((rmtadv & LPA_1000XPAUSE) && (rmtadv & LPA_1000XPAUSE_ASYM))
  1116. cap = FLOW_CTRL_TX;
  1117. }
  1118. return cap;
  1119. }
  1120. static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
  1121. {
  1122. u8 autoneg;
  1123. u8 flowctrl = 0;
  1124. u32 old_rx_mode = tp->rx_mode;
  1125. u32 old_tx_mode = tp->tx_mode;
  1126. if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)
  1127. autoneg = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]->autoneg;
  1128. else
  1129. autoneg = tp->link_config.autoneg;
  1130. if (autoneg == AUTONEG_ENABLE &&
  1131. (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG)) {
  1132. if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
  1133. flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
  1134. else
  1135. flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
  1136. } else
  1137. flowctrl = tp->link_config.flowctrl;
  1138. tp->link_config.active_flowctrl = flowctrl;
  1139. if (flowctrl & FLOW_CTRL_RX)
  1140. tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
  1141. else
  1142. tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
  1143. if (old_rx_mode != tp->rx_mode)
  1144. tw32_f(MAC_RX_MODE, tp->rx_mode);
  1145. if (flowctrl & FLOW_CTRL_TX)
  1146. tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
  1147. else
  1148. tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
  1149. if (old_tx_mode != tp->tx_mode)
  1150. tw32_f(MAC_TX_MODE, tp->tx_mode);
  1151. }
  1152. static void tg3_adjust_link(struct net_device *dev)
  1153. {
  1154. u8 oldflowctrl, linkmesg = 0;
  1155. u32 mac_mode, lcl_adv, rmt_adv;
  1156. struct tg3 *tp = netdev_priv(dev);
  1157. struct phy_device *phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
  1158. spin_lock_bh(&tp->lock);
  1159. mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
  1160. MAC_MODE_HALF_DUPLEX);
  1161. oldflowctrl = tp->link_config.active_flowctrl;
  1162. if (phydev->link) {
  1163. lcl_adv = 0;
  1164. rmt_adv = 0;
  1165. if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
  1166. mac_mode |= MAC_MODE_PORT_MODE_MII;
  1167. else if (phydev->speed == SPEED_1000 ||
  1168. GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785)
  1169. mac_mode |= MAC_MODE_PORT_MODE_GMII;
  1170. else
  1171. mac_mode |= MAC_MODE_PORT_MODE_MII;
  1172. if (phydev->duplex == DUPLEX_HALF)
  1173. mac_mode |= MAC_MODE_HALF_DUPLEX;
  1174. else {
  1175. lcl_adv = tg3_advert_flowctrl_1000T(
  1176. tp->link_config.flowctrl);
  1177. if (phydev->pause)
  1178. rmt_adv = LPA_PAUSE_CAP;
  1179. if (phydev->asym_pause)
  1180. rmt_adv |= LPA_PAUSE_ASYM;
  1181. }
  1182. tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
  1183. } else
  1184. mac_mode |= MAC_MODE_PORT_MODE_GMII;
  1185. if (mac_mode != tp->mac_mode) {
  1186. tp->mac_mode = mac_mode;
  1187. tw32_f(MAC_MODE, tp->mac_mode);
  1188. udelay(40);
  1189. }
  1190. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
  1191. if (phydev->speed == SPEED_10)
  1192. tw32(MAC_MI_STAT,
  1193. MAC_MI_STAT_10MBPS_MODE |
  1194. MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
  1195. else
  1196. tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
  1197. }
  1198. if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
  1199. tw32(MAC_TX_LENGTHS,
  1200. ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
  1201. (6 << TX_LENGTHS_IPG_SHIFT) |
  1202. (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
  1203. else
  1204. tw32(MAC_TX_LENGTHS,
  1205. ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
  1206. (6 << TX_LENGTHS_IPG_SHIFT) |
  1207. (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
  1208. if ((phydev->link && tp->link_config.active_speed == SPEED_INVALID) ||
  1209. (!phydev->link && tp->link_config.active_speed != SPEED_INVALID) ||
  1210. phydev->speed != tp->link_config.active_speed ||
  1211. phydev->duplex != tp->link_config.active_duplex ||
  1212. oldflowctrl != tp->link_config.active_flowctrl)
  1213. linkmesg = 1;
  1214. tp->link_config.active_speed = phydev->speed;
  1215. tp->link_config.active_duplex = phydev->duplex;
  1216. spin_unlock_bh(&tp->lock);
  1217. if (linkmesg)
  1218. tg3_link_report(tp);
  1219. }
  1220. static int tg3_phy_init(struct tg3 *tp)
  1221. {
  1222. struct phy_device *phydev;
  1223. if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)
  1224. return 0;
  1225. /* Bring the PHY back to a known state. */
  1226. tg3_bmcr_reset(tp);
  1227. phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
  1228. /* Attach the MAC to the PHY. */
  1229. phydev = phy_connect(tp->dev, dev_name(&phydev->dev), tg3_adjust_link,
  1230. phydev->dev_flags, phydev->interface);
  1231. if (IS_ERR(phydev)) {
  1232. dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
  1233. return PTR_ERR(phydev);
  1234. }
  1235. /* Mask with MAC supported features. */
  1236. switch (phydev->interface) {
  1237. case PHY_INTERFACE_MODE_GMII:
  1238. case PHY_INTERFACE_MODE_RGMII:
  1239. if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
  1240. phydev->supported &= (PHY_GBIT_FEATURES |
  1241. SUPPORTED_Pause |
  1242. SUPPORTED_Asym_Pause);
  1243. break;
  1244. }
  1245. /* fallthru */
  1246. case PHY_INTERFACE_MODE_MII:
  1247. phydev->supported &= (PHY_BASIC_FEATURES |
  1248. SUPPORTED_Pause |
  1249. SUPPORTED_Asym_Pause);
  1250. break;
  1251. default:
  1252. phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
  1253. return -EINVAL;
  1254. }
  1255. tp->phy_flags |= TG3_PHYFLG_IS_CONNECTED;
  1256. phydev->advertising = phydev->supported;
  1257. return 0;
  1258. }
  1259. static void tg3_phy_start(struct tg3 *tp)
  1260. {
  1261. struct phy_device *phydev;
  1262. if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
  1263. return;
  1264. phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
  1265. if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
  1266. tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
  1267. phydev->speed = tp->link_config.orig_speed;
  1268. phydev->duplex = tp->link_config.orig_duplex;
  1269. phydev->autoneg = tp->link_config.orig_autoneg;
  1270. phydev->advertising = tp->link_config.orig_advertising;
  1271. }
  1272. phy_start(phydev);
  1273. phy_start_aneg(phydev);
  1274. }
  1275. static void tg3_phy_stop(struct tg3 *tp)
  1276. {
  1277. if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
  1278. return;
  1279. phy_stop(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
  1280. }
  1281. static void tg3_phy_fini(struct tg3 *tp)
  1282. {
  1283. if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
  1284. phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
  1285. tp->phy_flags &= ~TG3_PHYFLG_IS_CONNECTED;
  1286. }
  1287. }
  1288. static int tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
  1289. {
  1290. int err;
  1291. err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
  1292. if (!err)
  1293. err = tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
  1294. return err;
  1295. }
  1296. static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
  1297. {
  1298. u32 phytest;
  1299. if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
  1300. u32 phy;
  1301. tg3_writephy(tp, MII_TG3_FET_TEST,
  1302. phytest | MII_TG3_FET_SHADOW_EN);
  1303. if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
  1304. if (enable)
  1305. phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
  1306. else
  1307. phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
  1308. tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
  1309. }
  1310. tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
  1311. }
  1312. }
  1313. static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
  1314. {
  1315. u32 reg;
  1316. if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
  1317. ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
  1318. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719) &&
  1319. (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
  1320. return;
  1321. if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
  1322. tg3_phy_fet_toggle_apd(tp, enable);
  1323. return;
  1324. }
  1325. reg = MII_TG3_MISC_SHDW_WREN |
  1326. MII_TG3_MISC_SHDW_SCR5_SEL |
  1327. MII_TG3_MISC_SHDW_SCR5_LPED |
  1328. MII_TG3_MISC_SHDW_SCR5_DLPTLM |
  1329. MII_TG3_MISC_SHDW_SCR5_SDTL |
  1330. MII_TG3_MISC_SHDW_SCR5_C125OE;
  1331. if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 || !enable)
  1332. reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
  1333. tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
  1334. reg = MII_TG3_MISC_SHDW_WREN |
  1335. MII_TG3_MISC_SHDW_APD_SEL |
  1336. MII_TG3_MISC_SHDW_APD_WKTM_84MS;
  1337. if (enable)
  1338. reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
  1339. tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
  1340. }
  1341. static void tg3_phy_toggle_automdix(struct tg3 *tp, int enable)
  1342. {
  1343. u32 phy;
  1344. if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
  1345. (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
  1346. return;
  1347. if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
  1348. u32 ephy;
  1349. if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
  1350. u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
  1351. tg3_writephy(tp, MII_TG3_FET_TEST,
  1352. ephy | MII_TG3_FET_SHADOW_EN);
  1353. if (!tg3_readphy(tp, reg, &phy)) {
  1354. if (enable)
  1355. phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
  1356. else
  1357. phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
  1358. tg3_writephy(tp, reg, phy);
  1359. }
  1360. tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
  1361. }
  1362. } else {
  1363. phy = MII_TG3_AUXCTL_MISC_RDSEL_MISC |
  1364. MII_TG3_AUXCTL_SHDWSEL_MISC;
  1365. if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, phy) &&
  1366. !tg3_readphy(tp, MII_TG3_AUX_CTRL, &phy)) {
  1367. if (enable)
  1368. phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
  1369. else
  1370. phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
  1371. phy |= MII_TG3_AUXCTL_MISC_WREN;
  1372. tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
  1373. }
  1374. }
  1375. }
  1376. static void tg3_phy_set_wirespeed(struct tg3 *tp)
  1377. {
  1378. u32 val;
  1379. if (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED)
  1380. return;
  1381. if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x7007) &&
  1382. !tg3_readphy(tp, MII_TG3_AUX_CTRL, &val))
  1383. tg3_writephy(tp, MII_TG3_AUX_CTRL,
  1384. (val | (1 << 15) | (1 << 4)));
  1385. }
  1386. static void tg3_phy_apply_otp(struct tg3 *tp)
  1387. {
  1388. u32 otp, phy;
  1389. if (!tp->phy_otp)
  1390. return;
  1391. otp = tp->phy_otp;
  1392. /* Enable SM_DSP clock and tx 6dB coding. */
  1393. phy = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
  1394. MII_TG3_AUXCTL_ACTL_SMDSP_ENA |
  1395. MII_TG3_AUXCTL_ACTL_TX_6DB;
  1396. tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
  1397. phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
  1398. phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
  1399. tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
  1400. phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
  1401. ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
  1402. tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
  1403. phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
  1404. phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
  1405. tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
  1406. phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
  1407. tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
  1408. phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
  1409. tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
  1410. phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
  1411. ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
  1412. tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
  1413. /* Turn off SM_DSP clock. */
  1414. phy = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
  1415. MII_TG3_AUXCTL_ACTL_TX_6DB;
  1416. tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
  1417. }
  1418. static int tg3_wait_macro_done(struct tg3 *tp)
  1419. {
  1420. int limit = 100;
  1421. while (limit--) {
  1422. u32 tmp32;
  1423. if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
  1424. if ((tmp32 & 0x1000) == 0)
  1425. break;
  1426. }
  1427. }
  1428. if (limit < 0)
  1429. return -EBUSY;
  1430. return 0;
  1431. }
  1432. static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
  1433. {
  1434. static const u32 test_pat[4][6] = {
  1435. { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
  1436. { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
  1437. { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
  1438. { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
  1439. };
  1440. int chan;
  1441. for (chan = 0; chan < 4; chan++) {
  1442. int i;
  1443. tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
  1444. (chan * 0x2000) | 0x0200);
  1445. tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
  1446. for (i = 0; i < 6; i++)
  1447. tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
  1448. test_pat[chan][i]);
  1449. tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
  1450. if (tg3_wait_macro_done(tp)) {
  1451. *resetp = 1;
  1452. return -EBUSY;
  1453. }
  1454. tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
  1455. (chan * 0x2000) | 0x0200);
  1456. tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
  1457. if (tg3_wait_macro_done(tp)) {
  1458. *resetp = 1;
  1459. return -EBUSY;
  1460. }
  1461. tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
  1462. if (tg3_wait_macro_done(tp)) {
  1463. *resetp = 1;
  1464. return -EBUSY;
  1465. }
  1466. for (i = 0; i < 6; i += 2) {
  1467. u32 low, high;
  1468. if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
  1469. tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
  1470. tg3_wait_macro_done(tp)) {
  1471. *resetp = 1;
  1472. return -EBUSY;
  1473. }
  1474. low &= 0x7fff;
  1475. high &= 0x000f;
  1476. if (low != test_pat[chan][i] ||
  1477. high != test_pat[chan][i+1]) {
  1478. tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
  1479. tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
  1480. tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
  1481. return -EBUSY;
  1482. }
  1483. }
  1484. }
  1485. return 0;
  1486. }
  1487. static int tg3_phy_reset_chanpat(struct tg3 *tp)
  1488. {
  1489. int chan;
  1490. for (chan = 0; chan < 4; chan++) {
  1491. int i;
  1492. tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
  1493. (chan * 0x2000) | 0x0200);
  1494. tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
  1495. for (i = 0; i < 6; i++)
  1496. tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
  1497. tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
  1498. if (tg3_wait_macro_done(tp))
  1499. return -EBUSY;
  1500. }
  1501. return 0;
  1502. }
  1503. static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
  1504. {
  1505. u32 reg32, phy9_orig;
  1506. int retries, do_phy_reset, err;
  1507. retries = 10;
  1508. do_phy_reset = 1;
  1509. do {
  1510. if (do_phy_reset) {
  1511. err = tg3_bmcr_reset(tp);
  1512. if (err)
  1513. return err;
  1514. do_phy_reset = 0;
  1515. }
  1516. /* Disable transmitter and interrupt. */
  1517. if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
  1518. continue;
  1519. reg32 |= 0x3000;
  1520. tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
  1521. /* Set full-duplex, 1000 mbps. */
  1522. tg3_writephy(tp, MII_BMCR,
  1523. BMCR_FULLDPLX | TG3_BMCR_SPEED1000);
  1524. /* Set to master mode. */
  1525. if (tg3_readphy(tp, MII_TG3_CTRL, &phy9_orig))
  1526. continue;
  1527. tg3_writephy(tp, MII_TG3_CTRL,
  1528. (MII_TG3_CTRL_AS_MASTER |
  1529. MII_TG3_CTRL_ENABLE_AS_MASTER));
  1530. /* Enable SM_DSP_CLOCK and 6dB. */
  1531. tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
  1532. /* Block the PHY control access. */
  1533. tg3_phydsp_write(tp, 0x8005, 0x0800);
  1534. err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
  1535. if (!err)
  1536. break;
  1537. } while (--retries);
  1538. err = tg3_phy_reset_chanpat(tp);
  1539. if (err)
  1540. return err;
  1541. tg3_phydsp_write(tp, 0x8005, 0x0000);
  1542. tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
  1543. tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0000);
  1544. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
  1545. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
  1546. /* Set Extended packet length bit for jumbo frames */
  1547. tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4400);
  1548. } else {
  1549. tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
  1550. }
  1551. tg3_writephy(tp, MII_TG3_CTRL, phy9_orig);
  1552. if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
  1553. reg32 &= ~0x3000;
  1554. tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
  1555. } else if (!err)
  1556. err = -EBUSY;
  1557. return err;
  1558. }
  1559. /* This will reset the tigon3 PHY if there is no valid
  1560. * link unless the FORCE argument is non-zero.
  1561. */
  1562. static int tg3_phy_reset(struct tg3 *tp)
  1563. {
  1564. u32 val, cpmuctrl;
  1565. int err;
  1566. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
  1567. val = tr32(GRC_MISC_CFG);
  1568. tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
  1569. udelay(40);
  1570. }
  1571. err = tg3_readphy(tp, MII_BMSR, &val);
  1572. err |= tg3_readphy(tp, MII_BMSR, &val);
  1573. if (err != 0)
  1574. return -EBUSY;
  1575. if (netif_running(tp->dev) && netif_carrier_ok(tp->dev)) {
  1576. netif_carrier_off(tp->dev);
  1577. tg3_link_report(tp);
  1578. }
  1579. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
  1580. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
  1581. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
  1582. err = tg3_phy_reset_5703_4_5(tp);
  1583. if (err)
  1584. return err;
  1585. goto out;
  1586. }
  1587. cpmuctrl = 0;
  1588. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
  1589. GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
  1590. cpmuctrl = tr32(TG3_CPMU_CTRL);
  1591. if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
  1592. tw32(TG3_CPMU_CTRL,
  1593. cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
  1594. }
  1595. err = tg3_bmcr_reset(tp);
  1596. if (err)
  1597. return err;
  1598. if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
  1599. val = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
  1600. tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, val);
  1601. tw32(TG3_CPMU_CTRL, cpmuctrl);
  1602. }
  1603. if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
  1604. GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
  1605. val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
  1606. if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
  1607. CPMU_LSPD_1000MB_MACCLK_12_5) {
  1608. val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
  1609. udelay(40);
  1610. tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
  1611. }
  1612. }
  1613. if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
  1614. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719) &&
  1615. (tp->phy_flags & TG3_PHYFLG_MII_SERDES))
  1616. return 0;
  1617. tg3_phy_apply_otp(tp);
  1618. if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
  1619. tg3_phy_toggle_apd(tp, true);
  1620. else
  1621. tg3_phy_toggle_apd(tp, false);
  1622. out:
  1623. if (tp->phy_flags & TG3_PHYFLG_ADC_BUG) {
  1624. tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
  1625. tg3_phydsp_write(tp, 0x201f, 0x2aaa);
  1626. tg3_phydsp_write(tp, 0x000a, 0x0323);
  1627. tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
  1628. }
  1629. if (tp->phy_flags & TG3_PHYFLG_5704_A0_BUG) {
  1630. tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
  1631. tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
  1632. }
  1633. if (tp->phy_flags & TG3_PHYFLG_BER_BUG) {
  1634. tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
  1635. tg3_phydsp_write(tp, 0x000a, 0x310b);
  1636. tg3_phydsp_write(tp, 0x201f, 0x9506);
  1637. tg3_phydsp_write(tp, 0x401f, 0x14e2);
  1638. tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
  1639. } else if (tp->phy_flags & TG3_PHYFLG_JITTER_BUG) {
  1640. tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
  1641. tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
  1642. if (tp->phy_flags & TG3_PHYFLG_ADJUST_TRIM) {
  1643. tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
  1644. tg3_writephy(tp, MII_TG3_TEST1,
  1645. MII_TG3_TEST1_TRIM_EN | 0x4);
  1646. } else
  1647. tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
  1648. tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
  1649. }
  1650. /* Set Extended packet length bit (bit 14) on all chips that */
  1651. /* support jumbo frames */
  1652. if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
  1653. /* Cannot do read-modify-write on 5401 */
  1654. tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4c20);
  1655. } else if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
  1656. /* Set bit 14 with read-modify-write to preserve other bits */
  1657. if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0007) &&
  1658. !tg3_readphy(tp, MII_TG3_AUX_CTRL, &val))
  1659. tg3_writephy(tp, MII_TG3_AUX_CTRL, val | 0x4000);
  1660. }
  1661. /* Set phy register 0x10 bit 0 to high fifo elasticity to support
  1662. * jumbo frames transmission.
  1663. */
  1664. if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
  1665. if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &val))
  1666. tg3_writephy(tp, MII_TG3_EXT_CTRL,
  1667. val | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
  1668. }
  1669. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
  1670. /* adjust output voltage */
  1671. tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
  1672. }
  1673. tg3_phy_toggle_automdix(tp, 1);
  1674. tg3_phy_set_wirespeed(tp);
  1675. return 0;
  1676. }
  1677. static void tg3_frob_aux_power(struct tg3 *tp)
  1678. {
  1679. struct tg3 *tp_peer = tp;
  1680. /* The GPIOs do something completely different on 57765. */
  1681. if ((tp->tg3_flags2 & TG3_FLG2_IS_NIC) == 0 ||
  1682. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
  1683. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
  1684. return;
  1685. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
  1686. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
  1687. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
  1688. struct net_device *dev_peer;
  1689. dev_peer = pci_get_drvdata(tp->pdev_peer);
  1690. /* remove_one() may have been run on the peer. */
  1691. if (!dev_peer)
  1692. tp_peer = tp;
  1693. else
  1694. tp_peer = netdev_priv(dev_peer);
  1695. }
  1696. if ((tp->tg3_flags & TG3_FLAG_WOL_ENABLE) != 0 ||
  1697. (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0 ||
  1698. (tp_peer->tg3_flags & TG3_FLAG_WOL_ENABLE) != 0 ||
  1699. (tp_peer->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0) {
  1700. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
  1701. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
  1702. tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
  1703. (GRC_LCLCTRL_GPIO_OE0 |
  1704. GRC_LCLCTRL_GPIO_OE1 |
  1705. GRC_LCLCTRL_GPIO_OE2 |
  1706. GRC_LCLCTRL_GPIO_OUTPUT0 |
  1707. GRC_LCLCTRL_GPIO_OUTPUT1),
  1708. 100);
  1709. } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
  1710. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
  1711. /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
  1712. u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
  1713. GRC_LCLCTRL_GPIO_OE1 |
  1714. GRC_LCLCTRL_GPIO_OE2 |
  1715. GRC_LCLCTRL_GPIO_OUTPUT0 |
  1716. GRC_LCLCTRL_GPIO_OUTPUT1 |
  1717. tp->grc_local_ctrl;
  1718. tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
  1719. grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
  1720. tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
  1721. grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
  1722. tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
  1723. } else {
  1724. u32 no_gpio2;
  1725. u32 grc_local_ctrl = 0;
  1726. if (tp_peer != tp &&
  1727. (tp_peer->tg3_flags & TG3_FLAG_INIT_COMPLETE) != 0)
  1728. return;
  1729. /* Workaround to prevent overdrawing Amps. */
  1730. if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
  1731. ASIC_REV_5714) {
  1732. grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
  1733. tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
  1734. grc_local_ctrl, 100);
  1735. }
  1736. /* On 5753 and variants, GPIO2 cannot be used. */
  1737. no_gpio2 = tp->nic_sram_data_cfg &
  1738. NIC_SRAM_DATA_CFG_NO_GPIO2;
  1739. grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
  1740. GRC_LCLCTRL_GPIO_OE1 |
  1741. GRC_LCLCTRL_GPIO_OE2 |
  1742. GRC_LCLCTRL_GPIO_OUTPUT1 |
  1743. GRC_LCLCTRL_GPIO_OUTPUT2;
  1744. if (no_gpio2) {
  1745. grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
  1746. GRC_LCLCTRL_GPIO_OUTPUT2);
  1747. }
  1748. tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
  1749. grc_local_ctrl, 100);
  1750. grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
  1751. tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
  1752. grc_local_ctrl, 100);
  1753. if (!no_gpio2) {
  1754. grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
  1755. tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
  1756. grc_local_ctrl, 100);
  1757. }
  1758. }
  1759. } else {
  1760. if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
  1761. GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
  1762. if (tp_peer != tp &&
  1763. (tp_peer->tg3_flags & TG3_FLAG_INIT_COMPLETE) != 0)
  1764. return;
  1765. tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
  1766. (GRC_LCLCTRL_GPIO_OE1 |
  1767. GRC_LCLCTRL_GPIO_OUTPUT1), 100);
  1768. tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
  1769. GRC_LCLCTRL_GPIO_OE1, 100);
  1770. tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
  1771. (GRC_LCLCTRL_GPIO_OE1 |
  1772. GRC_LCLCTRL_GPIO_OUTPUT1), 100);
  1773. }
  1774. }
  1775. }
  1776. static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
  1777. {
  1778. if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
  1779. return 1;
  1780. else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
  1781. if (speed != SPEED_10)
  1782. return 1;
  1783. } else if (speed == SPEED_10)
  1784. return 1;
  1785. return 0;
  1786. }
  1787. static int tg3_setup_phy(struct tg3 *, int);
  1788. #define RESET_KIND_SHUTDOWN 0
  1789. #define RESET_KIND_INIT 1
  1790. #define RESET_KIND_SUSPEND 2
  1791. static void tg3_write_sig_post_reset(struct tg3 *, int);
  1792. static int tg3_halt_cpu(struct tg3 *, u32);
  1793. static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
  1794. {
  1795. u32 val;
  1796. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
  1797. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
  1798. u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
  1799. u32 serdes_cfg = tr32(MAC_SERDES_CFG);
  1800. sg_dig_ctrl |=
  1801. SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
  1802. tw32(SG_DIG_CTRL, sg_dig_ctrl);
  1803. tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
  1804. }
  1805. return;
  1806. }
  1807. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
  1808. tg3_bmcr_reset(tp);
  1809. val = tr32(GRC_MISC_CFG);
  1810. tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
  1811. udelay(40);
  1812. return;
  1813. } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
  1814. u32 phytest;
  1815. if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
  1816. u32 phy;
  1817. tg3_writephy(tp, MII_ADVERTISE, 0);
  1818. tg3_writephy(tp, MII_BMCR,
  1819. BMCR_ANENABLE | BMCR_ANRESTART);
  1820. tg3_writephy(tp, MII_TG3_FET_TEST,
  1821. phytest | MII_TG3_FET_SHADOW_EN);
  1822. if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
  1823. phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
  1824. tg3_writephy(tp,
  1825. MII_TG3_FET_SHDW_AUXMODE4,
  1826. phy);
  1827. }
  1828. tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
  1829. }
  1830. return;
  1831. } else if (do_low_power) {
  1832. tg3_writephy(tp, MII_TG3_EXT_CTRL,
  1833. MII_TG3_EXT_CTRL_FORCE_LED_OFF);
  1834. tg3_writephy(tp, MII_TG3_AUX_CTRL,
  1835. MII_TG3_AUXCTL_SHDWSEL_PWRCTL |
  1836. MII_TG3_AUXCTL_PCTL_100TX_LPWR |
  1837. MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
  1838. MII_TG3_AUXCTL_PCTL_VREG_11V);
  1839. }
  1840. /* The PHY should not be powered down on some chips because
  1841. * of bugs.
  1842. */
  1843. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
  1844. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
  1845. (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 &&
  1846. (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
  1847. return;
  1848. if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
  1849. GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
  1850. val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
  1851. val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
  1852. val |= CPMU_LSPD_1000MB_MACCLK_12_5;
  1853. tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
  1854. }
  1855. tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
  1856. }
  1857. /* tp->lock is held. */
  1858. static int tg3_nvram_lock(struct tg3 *tp)
  1859. {
  1860. if (tp->tg3_flags & TG3_FLAG_NVRAM) {
  1861. int i;
  1862. if (tp->nvram_lock_cnt == 0) {
  1863. tw32(NVRAM_SWARB, SWARB_REQ_SET1);
  1864. for (i = 0; i < 8000; i++) {
  1865. if (tr32(NVRAM_SWARB) & SWARB_GNT1)
  1866. break;
  1867. udelay(20);
  1868. }
  1869. if (i == 8000) {
  1870. tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
  1871. return -ENODEV;
  1872. }
  1873. }
  1874. tp->nvram_lock_cnt++;
  1875. }
  1876. return 0;
  1877. }
  1878. /* tp->lock is held. */
  1879. static void tg3_nvram_unlock(struct tg3 *tp)
  1880. {
  1881. if (tp->tg3_flags & TG3_FLAG_NVRAM) {
  1882. if (tp->nvram_lock_cnt > 0)
  1883. tp->nvram_lock_cnt--;
  1884. if (tp->nvram_lock_cnt == 0)
  1885. tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
  1886. }
  1887. }
  1888. /* tp->lock is held. */
  1889. static void tg3_enable_nvram_access(struct tg3 *tp)
  1890. {
  1891. if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
  1892. !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM)) {
  1893. u32 nvaccess = tr32(NVRAM_ACCESS);
  1894. tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
  1895. }
  1896. }
  1897. /* tp->lock is held. */
  1898. static void tg3_disable_nvram_access(struct tg3 *tp)
  1899. {
  1900. if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
  1901. !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM)) {
  1902. u32 nvaccess = tr32(NVRAM_ACCESS);
  1903. tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
  1904. }
  1905. }
  1906. static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
  1907. u32 offset, u32 *val)
  1908. {
  1909. u32 tmp;
  1910. int i;
  1911. if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
  1912. return -EINVAL;
  1913. tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
  1914. EEPROM_ADDR_DEVID_MASK |
  1915. EEPROM_ADDR_READ);
  1916. tw32(GRC_EEPROM_ADDR,
  1917. tmp |
  1918. (0 << EEPROM_ADDR_DEVID_SHIFT) |
  1919. ((offset << EEPROM_ADDR_ADDR_SHIFT) &
  1920. EEPROM_ADDR_ADDR_MASK) |
  1921. EEPROM_ADDR_READ | EEPROM_ADDR_START);
  1922. for (i = 0; i < 1000; i++) {
  1923. tmp = tr32(GRC_EEPROM_ADDR);
  1924. if (tmp & EEPROM_ADDR_COMPLETE)
  1925. break;
  1926. msleep(1);
  1927. }
  1928. if (!(tmp & EEPROM_ADDR_COMPLETE))
  1929. return -EBUSY;
  1930. tmp = tr32(GRC_EEPROM_DATA);
  1931. /*
  1932. * The data will always be opposite the native endian
  1933. * format. Perform a blind byteswap to compensate.
  1934. */
  1935. *val = swab32(tmp);
  1936. return 0;
  1937. }
  1938. #define NVRAM_CMD_TIMEOUT 10000
  1939. static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
  1940. {
  1941. int i;
  1942. tw32(NVRAM_CMD, nvram_cmd);
  1943. for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
  1944. udelay(10);
  1945. if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
  1946. udelay(10);
  1947. break;
  1948. }
  1949. }
  1950. if (i == NVRAM_CMD_TIMEOUT)
  1951. return -EBUSY;
  1952. return 0;
  1953. }
  1954. static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
  1955. {
  1956. if ((tp->tg3_flags & TG3_FLAG_NVRAM) &&
  1957. (tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) &&
  1958. (tp->tg3_flags2 & TG3_FLG2_FLASH) &&
  1959. !(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM_ADDR_TRANS) &&
  1960. (tp->nvram_jedecnum == JEDEC_ATMEL))
  1961. addr = ((addr / tp->nvram_pagesize) <<
  1962. ATMEL_AT45DB0X1B_PAGE_POS) +
  1963. (addr % tp->nvram_pagesize);
  1964. return addr;
  1965. }
  1966. static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
  1967. {
  1968. if ((tp->tg3_flags & TG3_FLAG_NVRAM) &&
  1969. (tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) &&
  1970. (tp->tg3_flags2 & TG3_FLG2_FLASH) &&
  1971. !(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM_ADDR_TRANS) &&
  1972. (tp->nvram_jedecnum == JEDEC_ATMEL))
  1973. addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
  1974. tp->nvram_pagesize) +
  1975. (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
  1976. return addr;
  1977. }
  1978. /* NOTE: Data read in from NVRAM is byteswapped according to
  1979. * the byteswapping settings for all other register accesses.
  1980. * tg3 devices are BE devices, so on a BE machine, the data
  1981. * returned will be exactly as it is seen in NVRAM. On a LE
  1982. * machine, the 32-bit value will be byteswapped.
  1983. */
  1984. static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
  1985. {
  1986. int ret;
  1987. if (!(tp->tg3_flags & TG3_FLAG_NVRAM))
  1988. return tg3_nvram_read_using_eeprom(tp, offset, val);
  1989. offset = tg3_nvram_phys_addr(tp, offset);
  1990. if (offset > NVRAM_ADDR_MSK)
  1991. return -EINVAL;
  1992. ret = tg3_nvram_lock(tp);
  1993. if (ret)
  1994. return ret;
  1995. tg3_enable_nvram_access(tp);
  1996. tw32(NVRAM_ADDR, offset);
  1997. ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
  1998. NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
  1999. if (ret == 0)
  2000. *val = tr32(NVRAM_RDDATA);
  2001. tg3_disable_nvram_access(tp);
  2002. tg3_nvram_unlock(tp);
  2003. return ret;
  2004. }
  2005. /* Ensures NVRAM data is in bytestream format. */
  2006. static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
  2007. {
  2008. u32 v;
  2009. int res = tg3_nvram_read(tp, offset, &v);
  2010. if (!res)
  2011. *val = cpu_to_be32(v);
  2012. return res;
  2013. }
  2014. /* tp->lock is held. */
  2015. static void __tg3_set_mac_addr(struct tg3 *tp, int skip_mac_1)
  2016. {
  2017. u32 addr_high, addr_low;
  2018. int i;
  2019. addr_high = ((tp->dev->dev_addr[0] << 8) |
  2020. tp->dev->dev_addr[1]);
  2021. addr_low = ((tp->dev->dev_addr[2] << 24) |
  2022. (tp->dev->dev_addr[3] << 16) |
  2023. (tp->dev->dev_addr[4] << 8) |
  2024. (tp->dev->dev_addr[5] << 0));
  2025. for (i = 0; i < 4; i++) {
  2026. if (i == 1 && skip_mac_1)
  2027. continue;
  2028. tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
  2029. tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
  2030. }
  2031. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
  2032. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
  2033. for (i = 0; i < 12; i++) {
  2034. tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
  2035. tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
  2036. }
  2037. }
  2038. addr_high = (tp->dev->dev_addr[0] +
  2039. tp->dev->dev_addr[1] +
  2040. tp->dev->dev_addr[2] +
  2041. tp->dev->dev_addr[3] +
  2042. tp->dev->dev_addr[4] +
  2043. tp->dev->dev_addr[5]) &
  2044. TX_BACKOFF_SEED_MASK;
  2045. tw32(MAC_TX_BACKOFF_SEED, addr_high);
  2046. }
  2047. static int tg3_set_power_state(struct tg3 *tp, pci_power_t state)
  2048. {
  2049. u32 misc_host_ctrl;
  2050. bool device_should_wake, do_low_power;
  2051. /* Make sure register accesses (indirect or otherwise)
  2052. * will function correctly.
  2053. */
  2054. pci_write_config_dword(tp->pdev,
  2055. TG3PCI_MISC_HOST_CTRL,
  2056. tp->misc_host_ctrl);
  2057. switch (state) {
  2058. case PCI_D0:
  2059. pci_enable_wake(tp->pdev, state, false);
  2060. pci_set_power_state(tp->pdev, PCI_D0);
  2061. /* Switch out of Vaux if it is a NIC */
  2062. if (tp->tg3_flags2 & TG3_FLG2_IS_NIC)
  2063. tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl, 100);
  2064. return 0;
  2065. case PCI_D1:
  2066. case PCI_D2:
  2067. case PCI_D3hot:
  2068. break;
  2069. default:
  2070. netdev_err(tp->dev, "Invalid power state (D%d) requested\n",
  2071. state);
  2072. return -EINVAL;
  2073. }
  2074. /* Restore the CLKREQ setting. */
  2075. if (tp->tg3_flags3 & TG3_FLG3_CLKREQ_BUG) {
  2076. u16 lnkctl;
  2077. pci_read_config_word(tp->pdev,
  2078. tp->pcie_cap + PCI_EXP_LNKCTL,
  2079. &lnkctl);
  2080. lnkctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
  2081. pci_write_config_word(tp->pdev,
  2082. tp->pcie_cap + PCI_EXP_LNKCTL,
  2083. lnkctl);
  2084. }
  2085. misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
  2086. tw32(TG3PCI_MISC_HOST_CTRL,
  2087. misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
  2088. device_should_wake = pci_pme_capable(tp->pdev, state) &&
  2089. device_may_wakeup(&tp->pdev->dev) &&
  2090. (tp->tg3_flags & TG3_FLAG_WOL_ENABLE);
  2091. if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
  2092. do_low_power = false;
  2093. if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
  2094. !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
  2095. struct phy_device *phydev;
  2096. u32 phyid, advertising;
  2097. phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
  2098. tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
  2099. tp->link_config.orig_speed = phydev->speed;
  2100. tp->link_config.orig_duplex = phydev->duplex;
  2101. tp->link_config.orig_autoneg = phydev->autoneg;
  2102. tp->link_config.orig_advertising = phydev->advertising;
  2103. advertising = ADVERTISED_TP |
  2104. ADVERTISED_Pause |
  2105. ADVERTISED_Autoneg |
  2106. ADVERTISED_10baseT_Half;
  2107. if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
  2108. device_should_wake) {
  2109. if (tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB)
  2110. advertising |=
  2111. ADVERTISED_100baseT_Half |
  2112. ADVERTISED_100baseT_Full |
  2113. ADVERTISED_10baseT_Full;
  2114. else
  2115. advertising |= ADVERTISED_10baseT_Full;
  2116. }
  2117. phydev->advertising = advertising;
  2118. phy_start_aneg(phydev);
  2119. phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
  2120. if (phyid != PHY_ID_BCMAC131) {
  2121. phyid &= PHY_BCM_OUI_MASK;
  2122. if (phyid == PHY_BCM_OUI_1 ||
  2123. phyid == PHY_BCM_OUI_2 ||
  2124. phyid == PHY_BCM_OUI_3)
  2125. do_low_power = true;
  2126. }
  2127. }
  2128. } else {
  2129. do_low_power = true;
  2130. if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
  2131. tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
  2132. tp->link_config.orig_speed = tp->link_config.speed;
  2133. tp->link_config.orig_duplex = tp->link_config.duplex;
  2134. tp->link_config.orig_autoneg = tp->link_config.autoneg;
  2135. }
  2136. if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
  2137. tp->link_config.speed = SPEED_10;
  2138. tp->link_config.duplex = DUPLEX_HALF;
  2139. tp->link_config.autoneg = AUTONEG_ENABLE;
  2140. tg3_setup_phy(tp, 0);
  2141. }
  2142. }
  2143. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
  2144. u32 val;
  2145. val = tr32(GRC_VCPU_EXT_CTRL);
  2146. tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
  2147. } else if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
  2148. int i;
  2149. u32 val;
  2150. for (i = 0; i < 200; i++) {
  2151. tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
  2152. if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
  2153. break;
  2154. msleep(1);
  2155. }
  2156. }
  2157. if (tp->tg3_flags & TG3_FLAG_WOL_CAP)
  2158. tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
  2159. WOL_DRV_STATE_SHUTDOWN |
  2160. WOL_DRV_WOL |
  2161. WOL_SET_MAGIC_PKT);
  2162. if (device_should_wake) {
  2163. u32 mac_mode;
  2164. if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
  2165. if (do_low_power) {
  2166. tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x5a);
  2167. udelay(40);
  2168. }
  2169. if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
  2170. mac_mode = MAC_MODE_PORT_MODE_GMII;
  2171. else
  2172. mac_mode = MAC_MODE_PORT_MODE_MII;
  2173. mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
  2174. if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
  2175. ASIC_REV_5700) {
  2176. u32 speed = (tp->tg3_flags &
  2177. TG3_FLAG_WOL_SPEED_100MB) ?
  2178. SPEED_100 : SPEED_10;
  2179. if (tg3_5700_link_polarity(tp, speed))
  2180. mac_mode |= MAC_MODE_LINK_POLARITY;
  2181. else
  2182. mac_mode &= ~MAC_MODE_LINK_POLARITY;
  2183. }
  2184. } else {
  2185. mac_mode = MAC_MODE_PORT_MODE_TBI;
  2186. }
  2187. if (!(tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
  2188. tw32(MAC_LED_CTRL, tp->led_ctrl);
  2189. mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
  2190. if (((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
  2191. !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) &&
  2192. ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
  2193. (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)))
  2194. mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
  2195. if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
  2196. mac_mode |= tp->mac_mode &
  2197. (MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN);
  2198. if (mac_mode & MAC_MODE_APE_TX_EN)
  2199. mac_mode |= MAC_MODE_TDE_ENABLE;
  2200. }
  2201. tw32_f(MAC_MODE, mac_mode);
  2202. udelay(100);
  2203. tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
  2204. udelay(10);
  2205. }
  2206. if (!(tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB) &&
  2207. (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
  2208. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
  2209. u32 base_val;
  2210. base_val = tp->pci_clock_ctrl;
  2211. base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
  2212. CLOCK_CTRL_TXCLK_DISABLE);
  2213. tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
  2214. CLOCK_CTRL_PWRDOWN_PLL133, 40);
  2215. } else if ((tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
  2216. (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) ||
  2217. (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)) {
  2218. /* do nothing */
  2219. } else if (!((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
  2220. (tp->tg3_flags & TG3_FLAG_ENABLE_ASF))) {
  2221. u32 newbits1, newbits2;
  2222. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
  2223. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
  2224. newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
  2225. CLOCK_CTRL_TXCLK_DISABLE |
  2226. CLOCK_CTRL_ALTCLK);
  2227. newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
  2228. } else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
  2229. newbits1 = CLOCK_CTRL_625_CORE;
  2230. newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
  2231. } else {
  2232. newbits1 = CLOCK_CTRL_ALTCLK;
  2233. newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
  2234. }
  2235. tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
  2236. 40);
  2237. tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
  2238. 40);
  2239. if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
  2240. u32 newbits3;
  2241. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
  2242. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
  2243. newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
  2244. CLOCK_CTRL_TXCLK_DISABLE |
  2245. CLOCK_CTRL_44MHZ_CORE);
  2246. } else {
  2247. newbits3 = CLOCK_CTRL_44MHZ_CORE;
  2248. }
  2249. tw32_wait_f(TG3PCI_CLOCK_CTRL,
  2250. tp->pci_clock_ctrl | newbits3, 40);
  2251. }
  2252. }
  2253. if (!(device_should_wake) &&
  2254. !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
  2255. tg3_power_down_phy(tp, do_low_power);
  2256. tg3_frob_aux_power(tp);
  2257. /* Workaround for unstable PLL clock */
  2258. if ((GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX) ||
  2259. (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX)) {
  2260. u32 val = tr32(0x7d00);
  2261. val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
  2262. tw32(0x7d00, val);
  2263. if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
  2264. int err;
  2265. err = tg3_nvram_lock(tp);
  2266. tg3_halt_cpu(tp, RX_CPU_BASE);
  2267. if (!err)
  2268. tg3_nvram_unlock(tp);
  2269. }
  2270. }
  2271. tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
  2272. if (device_should_wake)
  2273. pci_enable_wake(tp->pdev, state, true);
  2274. /* Finally, set the new power state. */
  2275. pci_set_power_state(tp->pdev, state);
  2276. return 0;
  2277. }
  2278. static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
  2279. {
  2280. switch (val & MII_TG3_AUX_STAT_SPDMASK) {
  2281. case MII_TG3_AUX_STAT_10HALF:
  2282. *speed = SPEED_10;
  2283. *duplex = DUPLEX_HALF;
  2284. break;
  2285. case MII_TG3_AUX_STAT_10FULL:
  2286. *speed = SPEED_10;
  2287. *duplex = DUPLEX_FULL;
  2288. break;
  2289. case MII_TG3_AUX_STAT_100HALF:
  2290. *speed = SPEED_100;
  2291. *duplex = DUPLEX_HALF;
  2292. break;
  2293. case MII_TG3_AUX_STAT_100FULL:
  2294. *speed = SPEED_100;
  2295. *duplex = DUPLEX_FULL;
  2296. break;
  2297. case MII_TG3_AUX_STAT_1000HALF:
  2298. *speed = SPEED_1000;
  2299. *duplex = DUPLEX_HALF;
  2300. break;
  2301. case MII_TG3_AUX_STAT_1000FULL:
  2302. *speed = SPEED_1000;
  2303. *duplex = DUPLEX_FULL;
  2304. break;
  2305. default:
  2306. if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
  2307. *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
  2308. SPEED_10;
  2309. *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
  2310. DUPLEX_HALF;
  2311. break;
  2312. }
  2313. *speed = SPEED_INVALID;
  2314. *duplex = DUPLEX_INVALID;
  2315. break;
  2316. }
  2317. }
  2318. static void tg3_phy_copper_begin(struct tg3 *tp)
  2319. {
  2320. u32 new_adv;
  2321. int i;
  2322. if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
  2323. /* Entering low power mode. Disable gigabit and
  2324. * 100baseT advertisements.
  2325. */
  2326. tg3_writephy(tp, MII_TG3_CTRL, 0);
  2327. new_adv = (ADVERTISE_10HALF | ADVERTISE_10FULL |
  2328. ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
  2329. if (tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB)
  2330. new_adv |= (ADVERTISE_100HALF | ADVERTISE_100FULL);
  2331. tg3_writephy(tp, MII_ADVERTISE, new_adv);
  2332. } else if (tp->link_config.speed == SPEED_INVALID) {
  2333. if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
  2334. tp->link_config.advertising &=
  2335. ~(ADVERTISED_1000baseT_Half |
  2336. ADVERTISED_1000baseT_Full);
  2337. new_adv = ADVERTISE_CSMA;
  2338. if (tp->link_config.advertising & ADVERTISED_10baseT_Half)
  2339. new_adv |= ADVERTISE_10HALF;
  2340. if (tp->link_config.advertising & ADVERTISED_10baseT_Full)
  2341. new_adv |= ADVERTISE_10FULL;
  2342. if (tp->link_config.advertising & ADVERTISED_100baseT_Half)
  2343. new_adv |= ADVERTISE_100HALF;
  2344. if (tp->link_config.advertising & ADVERTISED_100baseT_Full)
  2345. new_adv |= ADVERTISE_100FULL;
  2346. new_adv |= tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
  2347. tg3_writephy(tp, MII_ADVERTISE, new_adv);
  2348. if (tp->link_config.advertising &
  2349. (ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full)) {
  2350. new_adv = 0;
  2351. if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
  2352. new_adv |= MII_TG3_CTRL_ADV_1000_HALF;
  2353. if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
  2354. new_adv |= MII_TG3_CTRL_ADV_1000_FULL;
  2355. if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY) &&
  2356. (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
  2357. tp->pci_chip_rev_id == CHIPREV_ID_5701_B0))
  2358. new_adv |= (MII_TG3_CTRL_AS_MASTER |
  2359. MII_TG3_CTRL_ENABLE_AS_MASTER);
  2360. tg3_writephy(tp, MII_TG3_CTRL, new_adv);
  2361. } else {
  2362. tg3_writephy(tp, MII_TG3_CTRL, 0);
  2363. }
  2364. } else {
  2365. new_adv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
  2366. new_adv |= ADVERTISE_CSMA;
  2367. /* Asking for a specific link mode. */
  2368. if (tp->link_config.speed == SPEED_1000) {
  2369. tg3_writephy(tp, MII_ADVERTISE, new_adv);
  2370. if (tp->link_config.duplex == DUPLEX_FULL)
  2371. new_adv = MII_TG3_CTRL_ADV_1000_FULL;
  2372. else
  2373. new_adv = MII_TG3_CTRL_ADV_1000_HALF;
  2374. if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
  2375. tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
  2376. new_adv |= (MII_TG3_CTRL_AS_MASTER |
  2377. MII_TG3_CTRL_ENABLE_AS_MASTER);
  2378. } else {
  2379. if (tp->link_config.speed == SPEED_100) {
  2380. if (tp->link_config.duplex == DUPLEX_FULL)
  2381. new_adv |= ADVERTISE_100FULL;
  2382. else
  2383. new_adv |= ADVERTISE_100HALF;
  2384. } else {
  2385. if (tp->link_config.duplex == DUPLEX_FULL)
  2386. new_adv |= ADVERTISE_10FULL;
  2387. else
  2388. new_adv |= ADVERTISE_10HALF;
  2389. }
  2390. tg3_writephy(tp, MII_ADVERTISE, new_adv);
  2391. new_adv = 0;
  2392. }
  2393. tg3_writephy(tp, MII_TG3_CTRL, new_adv);
  2394. }
  2395. if (tp->link_config.autoneg == AUTONEG_DISABLE &&
  2396. tp->link_config.speed != SPEED_INVALID) {
  2397. u32 bmcr, orig_bmcr;
  2398. tp->link_config.active_speed = tp->link_config.speed;
  2399. tp->link_config.active_duplex = tp->link_config.duplex;
  2400. bmcr = 0;
  2401. switch (tp->link_config.speed) {
  2402. default:
  2403. case SPEED_10:
  2404. break;
  2405. case SPEED_100:
  2406. bmcr |= BMCR_SPEED100;
  2407. break;
  2408. case SPEED_1000:
  2409. bmcr |= TG3_BMCR_SPEED1000;
  2410. break;
  2411. }
  2412. if (tp->link_config.duplex == DUPLEX_FULL)
  2413. bmcr |= BMCR_FULLDPLX;
  2414. if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
  2415. (bmcr != orig_bmcr)) {
  2416. tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
  2417. for (i = 0; i < 1500; i++) {
  2418. u32 tmp;
  2419. udelay(10);
  2420. if (tg3_readphy(tp, MII_BMSR, &tmp) ||
  2421. tg3_readphy(tp, MII_BMSR, &tmp))
  2422. continue;
  2423. if (!(tmp & BMSR_LSTATUS)) {
  2424. udelay(40);
  2425. break;
  2426. }
  2427. }
  2428. tg3_writephy(tp, MII_BMCR, bmcr);
  2429. udelay(40);
  2430. }
  2431. } else {
  2432. tg3_writephy(tp, MII_BMCR,
  2433. BMCR_ANENABLE | BMCR_ANRESTART);
  2434. }
  2435. }
  2436. static int tg3_init_5401phy_dsp(struct tg3 *tp)
  2437. {
  2438. int err;
  2439. /* Turn off tap power management. */
  2440. /* Set Extended packet length bit */
  2441. err = tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4c20);
  2442. err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
  2443. err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
  2444. err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
  2445. err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
  2446. err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
  2447. udelay(40);
  2448. return err;
  2449. }
  2450. static int tg3_copper_is_advertising_all(struct tg3 *tp, u32 mask)
  2451. {
  2452. u32 adv_reg, all_mask = 0;
  2453. if (mask & ADVERTISED_10baseT_Half)
  2454. all_mask |= ADVERTISE_10HALF;
  2455. if (mask & ADVERTISED_10baseT_Full)
  2456. all_mask |= ADVERTISE_10FULL;
  2457. if (mask & ADVERTISED_100baseT_Half)
  2458. all_mask |= ADVERTISE_100HALF;
  2459. if (mask & ADVERTISED_100baseT_Full)
  2460. all_mask |= ADVERTISE_100FULL;
  2461. if (tg3_readphy(tp, MII_ADVERTISE, &adv_reg))
  2462. return 0;
  2463. if ((adv_reg & all_mask) != all_mask)
  2464. return 0;
  2465. if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
  2466. u32 tg3_ctrl;
  2467. all_mask = 0;
  2468. if (mask & ADVERTISED_1000baseT_Half)
  2469. all_mask |= ADVERTISE_1000HALF;
  2470. if (mask & ADVERTISED_1000baseT_Full)
  2471. all_mask |= ADVERTISE_1000FULL;
  2472. if (tg3_readphy(tp, MII_TG3_CTRL, &tg3_ctrl))
  2473. return 0;
  2474. if ((tg3_ctrl & all_mask) != all_mask)
  2475. return 0;
  2476. }
  2477. return 1;
  2478. }
  2479. static int tg3_adv_1000T_flowctrl_ok(struct tg3 *tp, u32 *lcladv, u32 *rmtadv)
  2480. {
  2481. u32 curadv, reqadv;
  2482. if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
  2483. return 1;
  2484. curadv = *lcladv & (ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
  2485. reqadv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
  2486. if (tp->link_config.active_duplex == DUPLEX_FULL) {
  2487. if (curadv != reqadv)
  2488. return 0;
  2489. if (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG)
  2490. tg3_readphy(tp, MII_LPA, rmtadv);
  2491. } else {
  2492. /* Reprogram the advertisement register, even if it
  2493. * does not affect the current link. If the link
  2494. * gets renegotiated in the future, we can save an
  2495. * additional renegotiation cycle by advertising
  2496. * it correctly in the first place.
  2497. */
  2498. if (curadv != reqadv) {
  2499. *lcladv &= ~(ADVERTISE_PAUSE_CAP |
  2500. ADVERTISE_PAUSE_ASYM);
  2501. tg3_writephy(tp, MII_ADVERTISE, *lcladv | reqadv);
  2502. }
  2503. }
  2504. return 1;
  2505. }
  2506. static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
  2507. {
  2508. int current_link_up;
  2509. u32 bmsr, val;
  2510. u32 lcl_adv, rmt_adv;
  2511. u16 current_speed;
  2512. u8 current_duplex;
  2513. int i, err;
  2514. tw32(MAC_EVENT, 0);
  2515. tw32_f(MAC_STATUS,
  2516. (MAC_STATUS_SYNC_CHANGED |
  2517. MAC_STATUS_CFG_CHANGED |
  2518. MAC_STATUS_MI_COMPLETION |
  2519. MAC_STATUS_LNKSTATE_CHANGED));
  2520. udelay(40);
  2521. if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
  2522. tw32_f(MAC_MI_MODE,
  2523. (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
  2524. udelay(80);
  2525. }
  2526. tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x02);
  2527. /* Some third-party PHYs need to be reset on link going
  2528. * down.
  2529. */
  2530. if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
  2531. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
  2532. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
  2533. netif_carrier_ok(tp->dev)) {
  2534. tg3_readphy(tp, MII_BMSR, &bmsr);
  2535. if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
  2536. !(bmsr & BMSR_LSTATUS))
  2537. force_reset = 1;
  2538. }
  2539. if (force_reset)
  2540. tg3_phy_reset(tp);
  2541. if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
  2542. tg3_readphy(tp, MII_BMSR, &bmsr);
  2543. if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
  2544. !(tp->tg3_flags & TG3_FLAG_INIT_COMPLETE))
  2545. bmsr = 0;
  2546. if (!(bmsr & BMSR_LSTATUS)) {
  2547. err = tg3_init_5401phy_dsp(tp);
  2548. if (err)
  2549. return err;
  2550. tg3_readphy(tp, MII_BMSR, &bmsr);
  2551. for (i = 0; i < 1000; i++) {
  2552. udelay(10);
  2553. if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
  2554. (bmsr & BMSR_LSTATUS)) {
  2555. udelay(40);
  2556. break;
  2557. }
  2558. }
  2559. if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
  2560. TG3_PHY_REV_BCM5401_B0 &&
  2561. !(bmsr & BMSR_LSTATUS) &&
  2562. tp->link_config.active_speed == SPEED_1000) {
  2563. err = tg3_phy_reset(tp);
  2564. if (!err)
  2565. err = tg3_init_5401phy_dsp(tp);
  2566. if (err)
  2567. return err;
  2568. }
  2569. }
  2570. } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
  2571. tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
  2572. /* 5701 {A0,B0} CRC bug workaround */
  2573. tg3_writephy(tp, 0x15, 0x0a75);
  2574. tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
  2575. tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
  2576. tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
  2577. }
  2578. /* Clear pending interrupts... */
  2579. tg3_readphy(tp, MII_TG3_ISTAT, &val);
  2580. tg3_readphy(tp, MII_TG3_ISTAT, &val);
  2581. if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
  2582. tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
  2583. else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
  2584. tg3_writephy(tp, MII_TG3_IMASK, ~0);
  2585. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
  2586. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
  2587. if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
  2588. tg3_writephy(tp, MII_TG3_EXT_CTRL,
  2589. MII_TG3_EXT_CTRL_LNK3_LED_MODE);
  2590. else
  2591. tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
  2592. }
  2593. current_link_up = 0;
  2594. current_speed = SPEED_INVALID;
  2595. current_duplex = DUPLEX_INVALID;
  2596. if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
  2597. tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4007);
  2598. tg3_readphy(tp, MII_TG3_AUX_CTRL, &val);
  2599. if (!(val & (1 << 10))) {
  2600. val |= (1 << 10);
  2601. tg3_writephy(tp, MII_TG3_AUX_CTRL, val);
  2602. goto relink;
  2603. }
  2604. }
  2605. bmsr = 0;
  2606. for (i = 0; i < 100; i++) {
  2607. tg3_readphy(tp, MII_BMSR, &bmsr);
  2608. if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
  2609. (bmsr & BMSR_LSTATUS))
  2610. break;
  2611. udelay(40);
  2612. }
  2613. if (bmsr & BMSR_LSTATUS) {
  2614. u32 aux_stat, bmcr;
  2615. tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
  2616. for (i = 0; i < 2000; i++) {
  2617. udelay(10);
  2618. if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
  2619. aux_stat)
  2620. break;
  2621. }
  2622. tg3_aux_stat_to_speed_duplex(tp, aux_stat,
  2623. &current_speed,
  2624. &current_duplex);
  2625. bmcr = 0;
  2626. for (i = 0; i < 200; i++) {
  2627. tg3_readphy(tp, MII_BMCR, &bmcr);
  2628. if (tg3_readphy(tp, MII_BMCR, &bmcr))
  2629. continue;
  2630. if (bmcr && bmcr != 0x7fff)
  2631. break;
  2632. udelay(10);
  2633. }
  2634. lcl_adv = 0;
  2635. rmt_adv = 0;
  2636. tp->link_config.active_speed = current_speed;
  2637. tp->link_config.active_duplex = current_duplex;
  2638. if (tp->link_config.autoneg == AUTONEG_ENABLE) {
  2639. if ((bmcr & BMCR_ANENABLE) &&
  2640. tg3_copper_is_advertising_all(tp,
  2641. tp->link_config.advertising)) {
  2642. if (tg3_adv_1000T_flowctrl_ok(tp, &lcl_adv,
  2643. &rmt_adv))
  2644. current_link_up = 1;
  2645. }
  2646. } else {
  2647. if (!(bmcr & BMCR_ANENABLE) &&
  2648. tp->link_config.speed == current_speed &&
  2649. tp->link_config.duplex == current_duplex &&
  2650. tp->link_config.flowctrl ==
  2651. tp->link_config.active_flowctrl) {
  2652. current_link_up = 1;
  2653. }
  2654. }
  2655. if (current_link_up == 1 &&
  2656. tp->link_config.active_duplex == DUPLEX_FULL)
  2657. tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
  2658. }
  2659. relink:
  2660. if (current_link_up == 0 || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
  2661. tg3_phy_copper_begin(tp);
  2662. tg3_readphy(tp, MII_BMSR, &bmsr);
  2663. if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
  2664. (bmsr & BMSR_LSTATUS))
  2665. current_link_up = 1;
  2666. }
  2667. tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
  2668. if (current_link_up == 1) {
  2669. if (tp->link_config.active_speed == SPEED_100 ||
  2670. tp->link_config.active_speed == SPEED_10)
  2671. tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
  2672. else
  2673. tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
  2674. } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
  2675. tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
  2676. else
  2677. tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
  2678. tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
  2679. if (tp->link_config.active_duplex == DUPLEX_HALF)
  2680. tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
  2681. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
  2682. if (current_link_up == 1 &&
  2683. tg3_5700_link_polarity(tp, tp->link_config.active_speed))
  2684. tp->mac_mode |= MAC_MODE_LINK_POLARITY;
  2685. else
  2686. tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
  2687. }
  2688. /* ??? Without this setting Netgear GA302T PHY does not
  2689. * ??? send/receive packets...
  2690. */
  2691. if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
  2692. tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
  2693. tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
  2694. tw32_f(MAC_MI_MODE, tp->mi_mode);
  2695. udelay(80);
  2696. }
  2697. tw32_f(MAC_MODE, tp->mac_mode);
  2698. udelay(40);
  2699. if (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) {
  2700. /* Polled via timer. */
  2701. tw32_f(MAC_EVENT, 0);
  2702. } else {
  2703. tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
  2704. }
  2705. udelay(40);
  2706. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
  2707. current_link_up == 1 &&
  2708. tp->link_config.active_speed == SPEED_1000 &&
  2709. ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) ||
  2710. (tp->tg3_flags & TG3_FLAG_PCI_HIGH_SPEED))) {
  2711. udelay(120);
  2712. tw32_f(MAC_STATUS,
  2713. (MAC_STATUS_SYNC_CHANGED |
  2714. MAC_STATUS_CFG_CHANGED));
  2715. udelay(40);
  2716. tg3_write_mem(tp,
  2717. NIC_SRAM_FIRMWARE_MBOX,
  2718. NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
  2719. }
  2720. /* Prevent send BD corruption. */
  2721. if (tp->tg3_flags3 & TG3_FLG3_CLKREQ_BUG) {
  2722. u16 oldlnkctl, newlnkctl;
  2723. pci_read_config_word(tp->pdev,
  2724. tp->pcie_cap + PCI_EXP_LNKCTL,
  2725. &oldlnkctl);
  2726. if (tp->link_config.active_speed == SPEED_100 ||
  2727. tp->link_config.active_speed == SPEED_10)
  2728. newlnkctl = oldlnkctl & ~PCI_EXP_LNKCTL_CLKREQ_EN;
  2729. else
  2730. newlnkctl = oldlnkctl | PCI_EXP_LNKCTL_CLKREQ_EN;
  2731. if (newlnkctl != oldlnkctl)
  2732. pci_write_config_word(tp->pdev,
  2733. tp->pcie_cap + PCI_EXP_LNKCTL,
  2734. newlnkctl);
  2735. }
  2736. if (current_link_up != netif_carrier_ok(tp->dev)) {
  2737. if (current_link_up)
  2738. netif_carrier_on(tp->dev);
  2739. else
  2740. netif_carrier_off(tp->dev);
  2741. tg3_link_report(tp);
  2742. }
  2743. return 0;
  2744. }
  2745. struct tg3_fiber_aneginfo {
  2746. int state;
  2747. #define ANEG_STATE_UNKNOWN 0
  2748. #define ANEG_STATE_AN_ENABLE 1
  2749. #define ANEG_STATE_RESTART_INIT 2
  2750. #define ANEG_STATE_RESTART 3
  2751. #define ANEG_STATE_DISABLE_LINK_OK 4
  2752. #define ANEG_STATE_ABILITY_DETECT_INIT 5
  2753. #define ANEG_STATE_ABILITY_DETECT 6
  2754. #define ANEG_STATE_ACK_DETECT_INIT 7
  2755. #define ANEG_STATE_ACK_DETECT 8
  2756. #define ANEG_STATE_COMPLETE_ACK_INIT 9
  2757. #define ANEG_STATE_COMPLETE_ACK 10
  2758. #define ANEG_STATE_IDLE_DETECT_INIT 11
  2759. #define ANEG_STATE_IDLE_DETECT 12
  2760. #define ANEG_STATE_LINK_OK 13
  2761. #define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
  2762. #define ANEG_STATE_NEXT_PAGE_WAIT 15
  2763. u32 flags;
  2764. #define MR_AN_ENABLE 0x00000001
  2765. #define MR_RESTART_AN 0x00000002
  2766. #define MR_AN_COMPLETE 0x00000004
  2767. #define MR_PAGE_RX 0x00000008
  2768. #define MR_NP_LOADED 0x00000010
  2769. #define MR_TOGGLE_TX 0x00000020
  2770. #define MR_LP_ADV_FULL_DUPLEX 0x00000040
  2771. #define MR_LP_ADV_HALF_DUPLEX 0x00000080
  2772. #define MR_LP_ADV_SYM_PAUSE 0x00000100
  2773. #define MR_LP_ADV_ASYM_PAUSE 0x00000200
  2774. #define MR_LP_ADV_REMOTE_FAULT1 0x00000400
  2775. #define MR_LP_ADV_REMOTE_FAULT2 0x00000800
  2776. #define MR_LP_ADV_NEXT_PAGE 0x00001000
  2777. #define MR_TOGGLE_RX 0x00002000
  2778. #define MR_NP_RX 0x00004000
  2779. #define MR_LINK_OK 0x80000000
  2780. unsigned long link_time, cur_time;
  2781. u32 ability_match_cfg;
  2782. int ability_match_count;
  2783. char ability_match, idle_match, ack_match;
  2784. u32 txconfig, rxconfig;
  2785. #define ANEG_CFG_NP 0x00000080
  2786. #define ANEG_CFG_ACK 0x00000040
  2787. #define ANEG_CFG_RF2 0x00000020
  2788. #define ANEG_CFG_RF1 0x00000010
  2789. #define ANEG_CFG_PS2 0x00000001
  2790. #define ANEG_CFG_PS1 0x00008000
  2791. #define ANEG_CFG_HD 0x00004000
  2792. #define ANEG_CFG_FD 0x00002000
  2793. #define ANEG_CFG_INVAL 0x00001f06
  2794. };
  2795. #define ANEG_OK 0
  2796. #define ANEG_DONE 1
  2797. #define ANEG_TIMER_ENAB 2
  2798. #define ANEG_FAILED -1
  2799. #define ANEG_STATE_SETTLE_TIME 10000
  2800. static int tg3_fiber_aneg_smachine(struct tg3 *tp,
  2801. struct tg3_fiber_aneginfo *ap)
  2802. {
  2803. u16 flowctrl;
  2804. unsigned long delta;
  2805. u32 rx_cfg_reg;
  2806. int ret;
  2807. if (ap->state == ANEG_STATE_UNKNOWN) {
  2808. ap->rxconfig = 0;
  2809. ap->link_time = 0;
  2810. ap->cur_time = 0;
  2811. ap->ability_match_cfg = 0;
  2812. ap->ability_match_count = 0;
  2813. ap->ability_match = 0;
  2814. ap->idle_match = 0;
  2815. ap->ack_match = 0;
  2816. }
  2817. ap->cur_time++;
  2818. if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
  2819. rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
  2820. if (rx_cfg_reg != ap->ability_match_cfg) {
  2821. ap->ability_match_cfg = rx_cfg_reg;
  2822. ap->ability_match = 0;
  2823. ap->ability_match_count = 0;
  2824. } else {
  2825. if (++ap->ability_match_count > 1) {
  2826. ap->ability_match = 1;
  2827. ap->ability_match_cfg = rx_cfg_reg;
  2828. }
  2829. }
  2830. if (rx_cfg_reg & ANEG_CFG_ACK)
  2831. ap->ack_match = 1;
  2832. else
  2833. ap->ack_match = 0;
  2834. ap->idle_match = 0;
  2835. } else {
  2836. ap->idle_match = 1;
  2837. ap->ability_match_cfg = 0;
  2838. ap->ability_match_count = 0;
  2839. ap->ability_match = 0;
  2840. ap->ack_match = 0;
  2841. rx_cfg_reg = 0;
  2842. }
  2843. ap->rxconfig = rx_cfg_reg;
  2844. ret = ANEG_OK;
  2845. switch (ap->state) {
  2846. case ANEG_STATE_UNKNOWN:
  2847. if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
  2848. ap->state = ANEG_STATE_AN_ENABLE;
  2849. /* fallthru */
  2850. case ANEG_STATE_AN_ENABLE:
  2851. ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
  2852. if (ap->flags & MR_AN_ENABLE) {
  2853. ap->link_time = 0;
  2854. ap->cur_time = 0;
  2855. ap->ability_match_cfg = 0;
  2856. ap->ability_match_count = 0;
  2857. ap->ability_match = 0;
  2858. ap->idle_match = 0;
  2859. ap->ack_match = 0;
  2860. ap->state = ANEG_STATE_RESTART_INIT;
  2861. } else {
  2862. ap->state = ANEG_STATE_DISABLE_LINK_OK;
  2863. }
  2864. break;
  2865. case ANEG_STATE_RESTART_INIT:
  2866. ap->link_time = ap->cur_time;
  2867. ap->flags &= ~(MR_NP_LOADED);
  2868. ap->txconfig = 0;
  2869. tw32(MAC_TX_AUTO_NEG, 0);
  2870. tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
  2871. tw32_f(MAC_MODE, tp->mac_mode);
  2872. udelay(40);
  2873. ret = ANEG_TIMER_ENAB;
  2874. ap->state = ANEG_STATE_RESTART;
  2875. /* fallthru */
  2876. case ANEG_STATE_RESTART:
  2877. delta = ap->cur_time - ap->link_time;
  2878. if (delta > ANEG_STATE_SETTLE_TIME)
  2879. ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
  2880. else
  2881. ret = ANEG_TIMER_ENAB;
  2882. break;
  2883. case ANEG_STATE_DISABLE_LINK_OK:
  2884. ret = ANEG_DONE;
  2885. break;
  2886. case ANEG_STATE_ABILITY_DETECT_INIT:
  2887. ap->flags &= ~(MR_TOGGLE_TX);
  2888. ap->txconfig = ANEG_CFG_FD;
  2889. flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
  2890. if (flowctrl & ADVERTISE_1000XPAUSE)
  2891. ap->txconfig |= ANEG_CFG_PS1;
  2892. if (flowctrl & ADVERTISE_1000XPSE_ASYM)
  2893. ap->txconfig |= ANEG_CFG_PS2;
  2894. tw32(MAC_TX_AUTO_NEG, ap->txconfig);
  2895. tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
  2896. tw32_f(MAC_MODE, tp->mac_mode);
  2897. udelay(40);
  2898. ap->state = ANEG_STATE_ABILITY_DETECT;
  2899. break;
  2900. case ANEG_STATE_ABILITY_DETECT:
  2901. if (ap->ability_match != 0 && ap->rxconfig != 0)
  2902. ap->state = ANEG_STATE_ACK_DETECT_INIT;
  2903. break;
  2904. case ANEG_STATE_ACK_DETECT_INIT:
  2905. ap->txconfig |= ANEG_CFG_ACK;
  2906. tw32(MAC_TX_AUTO_NEG, ap->txconfig);
  2907. tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
  2908. tw32_f(MAC_MODE, tp->mac_mode);
  2909. udelay(40);
  2910. ap->state = ANEG_STATE_ACK_DETECT;
  2911. /* fallthru */
  2912. case ANEG_STATE_ACK_DETECT:
  2913. if (ap->ack_match != 0) {
  2914. if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
  2915. (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
  2916. ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
  2917. } else {
  2918. ap->state = ANEG_STATE_AN_ENABLE;
  2919. }
  2920. } else if (ap->ability_match != 0 &&
  2921. ap->rxconfig == 0) {
  2922. ap->state = ANEG_STATE_AN_ENABLE;
  2923. }
  2924. break;
  2925. case ANEG_STATE_COMPLETE_ACK_INIT:
  2926. if (ap->rxconfig & ANEG_CFG_INVAL) {
  2927. ret = ANEG_FAILED;
  2928. break;
  2929. }
  2930. ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
  2931. MR_LP_ADV_HALF_DUPLEX |
  2932. MR_LP_ADV_SYM_PAUSE |
  2933. MR_LP_ADV_ASYM_PAUSE |
  2934. MR_LP_ADV_REMOTE_FAULT1 |
  2935. MR_LP_ADV_REMOTE_FAULT2 |
  2936. MR_LP_ADV_NEXT_PAGE |
  2937. MR_TOGGLE_RX |
  2938. MR_NP_RX);
  2939. if (ap->rxconfig & ANEG_CFG_FD)
  2940. ap->flags |= MR_LP_ADV_FULL_DUPLEX;
  2941. if (ap->rxconfig & ANEG_CFG_HD)
  2942. ap->flags |= MR_LP_ADV_HALF_DUPLEX;
  2943. if (ap->rxconfig & ANEG_CFG_PS1)
  2944. ap->flags |= MR_LP_ADV_SYM_PAUSE;
  2945. if (ap->rxconfig & ANEG_CFG_PS2)
  2946. ap->flags |= MR_LP_ADV_ASYM_PAUSE;
  2947. if (ap->rxconfig & ANEG_CFG_RF1)
  2948. ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
  2949. if (ap->rxconfig & ANEG_CFG_RF2)
  2950. ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
  2951. if (ap->rxconfig & ANEG_CFG_NP)
  2952. ap->flags |= MR_LP_ADV_NEXT_PAGE;
  2953. ap->link_time = ap->cur_time;
  2954. ap->flags ^= (MR_TOGGLE_TX);
  2955. if (ap->rxconfig & 0x0008)
  2956. ap->flags |= MR_TOGGLE_RX;
  2957. if (ap->rxconfig & ANEG_CFG_NP)
  2958. ap->flags |= MR_NP_RX;
  2959. ap->flags |= MR_PAGE_RX;
  2960. ap->state = ANEG_STATE_COMPLETE_ACK;
  2961. ret = ANEG_TIMER_ENAB;
  2962. break;
  2963. case ANEG_STATE_COMPLETE_ACK:
  2964. if (ap->ability_match != 0 &&
  2965. ap->rxconfig == 0) {
  2966. ap->state = ANEG_STATE_AN_ENABLE;
  2967. break;
  2968. }
  2969. delta = ap->cur_time - ap->link_time;
  2970. if (delta > ANEG_STATE_SETTLE_TIME) {
  2971. if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
  2972. ap->state = ANEG_STATE_IDLE_DETECT_INIT;
  2973. } else {
  2974. if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
  2975. !(ap->flags & MR_NP_RX)) {
  2976. ap->state = ANEG_STATE_IDLE_DETECT_INIT;
  2977. } else {
  2978. ret = ANEG_FAILED;
  2979. }
  2980. }
  2981. }
  2982. break;
  2983. case ANEG_STATE_IDLE_DETECT_INIT:
  2984. ap->link_time = ap->cur_time;
  2985. tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
  2986. tw32_f(MAC_MODE, tp->mac_mode);
  2987. udelay(40);
  2988. ap->state = ANEG_STATE_IDLE_DETECT;
  2989. ret = ANEG_TIMER_ENAB;
  2990. break;
  2991. case ANEG_STATE_IDLE_DETECT:
  2992. if (ap->ability_match != 0 &&
  2993. ap->rxconfig == 0) {
  2994. ap->state = ANEG_STATE_AN_ENABLE;
  2995. break;
  2996. }
  2997. delta = ap->cur_time - ap->link_time;
  2998. if (delta > ANEG_STATE_SETTLE_TIME) {
  2999. /* XXX another gem from the Broadcom driver :( */
  3000. ap->state = ANEG_STATE_LINK_OK;
  3001. }
  3002. break;
  3003. case ANEG_STATE_LINK_OK:
  3004. ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
  3005. ret = ANEG_DONE;
  3006. break;
  3007. case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
  3008. /* ??? unimplemented */
  3009. break;
  3010. case ANEG_STATE_NEXT_PAGE_WAIT:
  3011. /* ??? unimplemented */
  3012. break;
  3013. default:
  3014. ret = ANEG_FAILED;
  3015. break;
  3016. }
  3017. return ret;
  3018. }
  3019. static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
  3020. {
  3021. int res = 0;
  3022. struct tg3_fiber_aneginfo aninfo;
  3023. int status = ANEG_FAILED;
  3024. unsigned int tick;
  3025. u32 tmp;
  3026. tw32_f(MAC_TX_AUTO_NEG, 0);
  3027. tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
  3028. tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
  3029. udelay(40);
  3030. tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
  3031. udelay(40);
  3032. memset(&aninfo, 0, sizeof(aninfo));
  3033. aninfo.flags |= MR_AN_ENABLE;
  3034. aninfo.state = ANEG_STATE_UNKNOWN;
  3035. aninfo.cur_time = 0;
  3036. tick = 0;
  3037. while (++tick < 195000) {
  3038. status = tg3_fiber_aneg_smachine(tp, &aninfo);
  3039. if (status == ANEG_DONE || status == ANEG_FAILED)
  3040. break;
  3041. udelay(1);
  3042. }
  3043. tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
  3044. tw32_f(MAC_MODE, tp->mac_mode);
  3045. udelay(40);
  3046. *txflags = aninfo.txconfig;
  3047. *rxflags = aninfo.flags;
  3048. if (status == ANEG_DONE &&
  3049. (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
  3050. MR_LP_ADV_FULL_DUPLEX)))
  3051. res = 1;
  3052. return res;
  3053. }
  3054. static void tg3_init_bcm8002(struct tg3 *tp)
  3055. {
  3056. u32 mac_status = tr32(MAC_STATUS);
  3057. int i;
  3058. /* Reset when initting first time or we have a link. */
  3059. if ((tp->tg3_flags & TG3_FLAG_INIT_COMPLETE) &&
  3060. !(mac_status & MAC_STATUS_PCS_SYNCED))
  3061. return;
  3062. /* Set PLL lock range. */
  3063. tg3_writephy(tp, 0x16, 0x8007);
  3064. /* SW reset */
  3065. tg3_writephy(tp, MII_BMCR, BMCR_RESET);
  3066. /* Wait for reset to complete. */
  3067. /* XXX schedule_timeout() ... */
  3068. for (i = 0; i < 500; i++)
  3069. udelay(10);
  3070. /* Config mode; select PMA/Ch 1 regs. */
  3071. tg3_writephy(tp, 0x10, 0x8411);
  3072. /* Enable auto-lock and comdet, select txclk for tx. */
  3073. tg3_writephy(tp, 0x11, 0x0a10);
  3074. tg3_writephy(tp, 0x18, 0x00a0);
  3075. tg3_writephy(tp, 0x16, 0x41ff);
  3076. /* Assert and deassert POR. */
  3077. tg3_writephy(tp, 0x13, 0x0400);
  3078. udelay(40);
  3079. tg3_writephy(tp, 0x13, 0x0000);
  3080. tg3_writephy(tp, 0x11, 0x0a50);
  3081. udelay(40);
  3082. tg3_writephy(tp, 0x11, 0x0a10);
  3083. /* Wait for signal to stabilize */
  3084. /* XXX schedule_timeout() ... */
  3085. for (i = 0; i < 15000; i++)
  3086. udelay(10);
  3087. /* Deselect the channel register so we can read the PHYID
  3088. * later.
  3089. */
  3090. tg3_writephy(tp, 0x10, 0x8011);
  3091. }
  3092. static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
  3093. {
  3094. u16 flowctrl;
  3095. u32 sg_dig_ctrl, sg_dig_status;
  3096. u32 serdes_cfg, expected_sg_dig_ctrl;
  3097. int workaround, port_a;
  3098. int current_link_up;
  3099. serdes_cfg = 0;
  3100. expected_sg_dig_ctrl = 0;
  3101. workaround = 0;
  3102. port_a = 1;
  3103. current_link_up = 0;
  3104. if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
  3105. tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
  3106. workaround = 1;
  3107. if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
  3108. port_a = 0;
  3109. /* preserve bits 0-11,13,14 for signal pre-emphasis */
  3110. /* preserve bits 20-23 for voltage regulator */
  3111. serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
  3112. }
  3113. sg_dig_ctrl = tr32(SG_DIG_CTRL);
  3114. if (tp->link_config.autoneg != AUTONEG_ENABLE) {
  3115. if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
  3116. if (workaround) {
  3117. u32 val = serdes_cfg;
  3118. if (port_a)
  3119. val |= 0xc010000;
  3120. else
  3121. val |= 0x4010000;
  3122. tw32_f(MAC_SERDES_CFG, val);
  3123. }
  3124. tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
  3125. }
  3126. if (mac_status & MAC_STATUS_PCS_SYNCED) {
  3127. tg3_setup_flow_control(tp, 0, 0);
  3128. current_link_up = 1;
  3129. }
  3130. goto out;
  3131. }
  3132. /* Want auto-negotiation. */
  3133. expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
  3134. flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
  3135. if (flowctrl & ADVERTISE_1000XPAUSE)
  3136. expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
  3137. if (flowctrl & ADVERTISE_1000XPSE_ASYM)
  3138. expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
  3139. if (sg_dig_ctrl != expected_sg_dig_ctrl) {
  3140. if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
  3141. tp->serdes_counter &&
  3142. ((mac_status & (MAC_STATUS_PCS_SYNCED |
  3143. MAC_STATUS_RCVD_CFG)) ==
  3144. MAC_STATUS_PCS_SYNCED)) {
  3145. tp->serdes_counter--;
  3146. current_link_up = 1;
  3147. goto out;
  3148. }
  3149. restart_autoneg:
  3150. if (workaround)
  3151. tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
  3152. tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
  3153. udelay(5);
  3154. tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
  3155. tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
  3156. tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
  3157. } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
  3158. MAC_STATUS_SIGNAL_DET)) {
  3159. sg_dig_status = tr32(SG_DIG_STATUS);
  3160. mac_status = tr32(MAC_STATUS);
  3161. if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
  3162. (mac_status & MAC_STATUS_PCS_SYNCED)) {
  3163. u32 local_adv = 0, remote_adv = 0;
  3164. if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
  3165. local_adv |= ADVERTISE_1000XPAUSE;
  3166. if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
  3167. local_adv |= ADVERTISE_1000XPSE_ASYM;
  3168. if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
  3169. remote_adv |= LPA_1000XPAUSE;
  3170. if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
  3171. remote_adv |= LPA_1000XPAUSE_ASYM;
  3172. tg3_setup_flow_control(tp, local_adv, remote_adv);
  3173. current_link_up = 1;
  3174. tp->serdes_counter = 0;
  3175. tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
  3176. } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
  3177. if (tp->serdes_counter)
  3178. tp->serdes_counter--;
  3179. else {
  3180. if (workaround) {
  3181. u32 val = serdes_cfg;
  3182. if (port_a)
  3183. val |= 0xc010000;
  3184. else
  3185. val |= 0x4010000;
  3186. tw32_f(MAC_SERDES_CFG, val);
  3187. }
  3188. tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
  3189. udelay(40);
  3190. /* Link parallel detection - link is up */
  3191. /* only if we have PCS_SYNC and not */
  3192. /* receiving config code words */
  3193. mac_status = tr32(MAC_STATUS);
  3194. if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
  3195. !(mac_status & MAC_STATUS_RCVD_CFG)) {
  3196. tg3_setup_flow_control(tp, 0, 0);
  3197. current_link_up = 1;
  3198. tp->phy_flags |=
  3199. TG3_PHYFLG_PARALLEL_DETECT;
  3200. tp->serdes_counter =
  3201. SERDES_PARALLEL_DET_TIMEOUT;
  3202. } else
  3203. goto restart_autoneg;
  3204. }
  3205. }
  3206. } else {
  3207. tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
  3208. tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
  3209. }
  3210. out:
  3211. return current_link_up;
  3212. }
  3213. static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
  3214. {
  3215. int current_link_up = 0;
  3216. if (!(mac_status & MAC_STATUS_PCS_SYNCED))
  3217. goto out;
  3218. if (tp->link_config.autoneg == AUTONEG_ENABLE) {
  3219. u32 txflags, rxflags;
  3220. int i;
  3221. if (fiber_autoneg(tp, &txflags, &rxflags)) {
  3222. u32 local_adv = 0, remote_adv = 0;
  3223. if (txflags & ANEG_CFG_PS1)
  3224. local_adv |= ADVERTISE_1000XPAUSE;
  3225. if (txflags & ANEG_CFG_PS2)
  3226. local_adv |= ADVERTISE_1000XPSE_ASYM;
  3227. if (rxflags & MR_LP_ADV_SYM_PAUSE)
  3228. remote_adv |= LPA_1000XPAUSE;
  3229. if (rxflags & MR_LP_ADV_ASYM_PAUSE)
  3230. remote_adv |= LPA_1000XPAUSE_ASYM;
  3231. tg3_setup_flow_control(tp, local_adv, remote_adv);
  3232. current_link_up = 1;
  3233. }
  3234. for (i = 0; i < 30; i++) {
  3235. udelay(20);
  3236. tw32_f(MAC_STATUS,
  3237. (MAC_STATUS_SYNC_CHANGED |
  3238. MAC_STATUS_CFG_CHANGED));
  3239. udelay(40);
  3240. if ((tr32(MAC_STATUS) &
  3241. (MAC_STATUS_SYNC_CHANGED |
  3242. MAC_STATUS_CFG_CHANGED)) == 0)
  3243. break;
  3244. }
  3245. mac_status = tr32(MAC_STATUS);
  3246. if (current_link_up == 0 &&
  3247. (mac_status & MAC_STATUS_PCS_SYNCED) &&
  3248. !(mac_status & MAC_STATUS_RCVD_CFG))
  3249. current_link_up = 1;
  3250. } else {
  3251. tg3_setup_flow_control(tp, 0, 0);
  3252. /* Forcing 1000FD link up. */
  3253. current_link_up = 1;
  3254. tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
  3255. udelay(40);
  3256. tw32_f(MAC_MODE, tp->mac_mode);
  3257. udelay(40);
  3258. }
  3259. out:
  3260. return current_link_up;
  3261. }
  3262. static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
  3263. {
  3264. u32 orig_pause_cfg;
  3265. u16 orig_active_speed;
  3266. u8 orig_active_duplex;
  3267. u32 mac_status;
  3268. int current_link_up;
  3269. int i;
  3270. orig_pause_cfg = tp->link_config.active_flowctrl;
  3271. orig_active_speed = tp->link_config.active_speed;
  3272. orig_active_duplex = tp->link_config.active_duplex;
  3273. if (!(tp->tg3_flags2 & TG3_FLG2_HW_AUTONEG) &&
  3274. netif_carrier_ok(tp->dev) &&
  3275. (tp->tg3_flags & TG3_FLAG_INIT_COMPLETE)) {
  3276. mac_status = tr32(MAC_STATUS);
  3277. mac_status &= (MAC_STATUS_PCS_SYNCED |
  3278. MAC_STATUS_SIGNAL_DET |
  3279. MAC_STATUS_CFG_CHANGED |
  3280. MAC_STATUS_RCVD_CFG);
  3281. if (mac_status == (MAC_STATUS_PCS_SYNCED |
  3282. MAC_STATUS_SIGNAL_DET)) {
  3283. tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
  3284. MAC_STATUS_CFG_CHANGED));
  3285. return 0;
  3286. }
  3287. }
  3288. tw32_f(MAC_TX_AUTO_NEG, 0);
  3289. tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
  3290. tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
  3291. tw32_f(MAC_MODE, tp->mac_mode);
  3292. udelay(40);
  3293. if (tp->phy_id == TG3_PHY_ID_BCM8002)
  3294. tg3_init_bcm8002(tp);
  3295. /* Enable link change event even when serdes polling. */
  3296. tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
  3297. udelay(40);
  3298. current_link_up = 0;
  3299. mac_status = tr32(MAC_STATUS);
  3300. if (tp->tg3_flags2 & TG3_FLG2_HW_AUTONEG)
  3301. current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
  3302. else
  3303. current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
  3304. tp->napi[0].hw_status->status =
  3305. (SD_STATUS_UPDATED |
  3306. (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
  3307. for (i = 0; i < 100; i++) {
  3308. tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
  3309. MAC_STATUS_CFG_CHANGED));
  3310. udelay(5);
  3311. if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
  3312. MAC_STATUS_CFG_CHANGED |
  3313. MAC_STATUS_LNKSTATE_CHANGED)) == 0)
  3314. break;
  3315. }
  3316. mac_status = tr32(MAC_STATUS);
  3317. if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
  3318. current_link_up = 0;
  3319. if (tp->link_config.autoneg == AUTONEG_ENABLE &&
  3320. tp->serdes_counter == 0) {
  3321. tw32_f(MAC_MODE, (tp->mac_mode |
  3322. MAC_MODE_SEND_CONFIGS));
  3323. udelay(1);
  3324. tw32_f(MAC_MODE, tp->mac_mode);
  3325. }
  3326. }
  3327. if (current_link_up == 1) {
  3328. tp->link_config.active_speed = SPEED_1000;
  3329. tp->link_config.active_duplex = DUPLEX_FULL;
  3330. tw32(MAC_LED_CTRL, (tp->led_ctrl |
  3331. LED_CTRL_LNKLED_OVERRIDE |
  3332. LED_CTRL_1000MBPS_ON));
  3333. } else {
  3334. tp->link_config.active_speed = SPEED_INVALID;
  3335. tp->link_config.active_duplex = DUPLEX_INVALID;
  3336. tw32(MAC_LED_CTRL, (tp->led_ctrl |
  3337. LED_CTRL_LNKLED_OVERRIDE |
  3338. LED_CTRL_TRAFFIC_OVERRIDE));
  3339. }
  3340. if (current_link_up != netif_carrier_ok(tp->dev)) {
  3341. if (current_link_up)
  3342. netif_carrier_on(tp->dev);
  3343. else
  3344. netif_carrier_off(tp->dev);
  3345. tg3_link_report(tp);
  3346. } else {
  3347. u32 now_pause_cfg = tp->link_config.active_flowctrl;
  3348. if (orig_pause_cfg != now_pause_cfg ||
  3349. orig_active_speed != tp->link_config.active_speed ||
  3350. orig_active_duplex != tp->link_config.active_duplex)
  3351. tg3_link_report(tp);
  3352. }
  3353. return 0;
  3354. }
  3355. static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
  3356. {
  3357. int current_link_up, err = 0;
  3358. u32 bmsr, bmcr;
  3359. u16 current_speed;
  3360. u8 current_duplex;
  3361. u32 local_adv, remote_adv;
  3362. tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
  3363. tw32_f(MAC_MODE, tp->mac_mode);
  3364. udelay(40);
  3365. tw32(MAC_EVENT, 0);
  3366. tw32_f(MAC_STATUS,
  3367. (MAC_STATUS_SYNC_CHANGED |
  3368. MAC_STATUS_CFG_CHANGED |
  3369. MAC_STATUS_MI_COMPLETION |
  3370. MAC_STATUS_LNKSTATE_CHANGED));
  3371. udelay(40);
  3372. if (force_reset)
  3373. tg3_phy_reset(tp);
  3374. current_link_up = 0;
  3375. current_speed = SPEED_INVALID;
  3376. current_duplex = DUPLEX_INVALID;
  3377. err |= tg3_readphy(tp, MII_BMSR, &bmsr);
  3378. err |= tg3_readphy(tp, MII_BMSR, &bmsr);
  3379. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
  3380. if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
  3381. bmsr |= BMSR_LSTATUS;
  3382. else
  3383. bmsr &= ~BMSR_LSTATUS;
  3384. }
  3385. err |= tg3_readphy(tp, MII_BMCR, &bmcr);
  3386. if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
  3387. (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
  3388. /* do nothing, just check for link up at the end */
  3389. } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
  3390. u32 adv, new_adv;
  3391. err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
  3392. new_adv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
  3393. ADVERTISE_1000XPAUSE |
  3394. ADVERTISE_1000XPSE_ASYM |
  3395. ADVERTISE_SLCT);
  3396. new_adv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
  3397. if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
  3398. new_adv |= ADVERTISE_1000XHALF;
  3399. if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
  3400. new_adv |= ADVERTISE_1000XFULL;
  3401. if ((new_adv != adv) || !(bmcr & BMCR_ANENABLE)) {
  3402. tg3_writephy(tp, MII_ADVERTISE, new_adv);
  3403. bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
  3404. tg3_writephy(tp, MII_BMCR, bmcr);
  3405. tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
  3406. tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
  3407. tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
  3408. return err;
  3409. }
  3410. } else {
  3411. u32 new_bmcr;
  3412. bmcr &= ~BMCR_SPEED1000;
  3413. new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
  3414. if (tp->link_config.duplex == DUPLEX_FULL)
  3415. new_bmcr |= BMCR_FULLDPLX;
  3416. if (new_bmcr != bmcr) {
  3417. /* BMCR_SPEED1000 is a reserved bit that needs
  3418. * to be set on write.
  3419. */
  3420. new_bmcr |= BMCR_SPEED1000;
  3421. /* Force a linkdown */
  3422. if (netif_carrier_ok(tp->dev)) {
  3423. u32 adv;
  3424. err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
  3425. adv &= ~(ADVERTISE_1000XFULL |
  3426. ADVERTISE_1000XHALF |
  3427. ADVERTISE_SLCT);
  3428. tg3_writephy(tp, MII_ADVERTISE, adv);
  3429. tg3_writephy(tp, MII_BMCR, bmcr |
  3430. BMCR_ANRESTART |
  3431. BMCR_ANENABLE);
  3432. udelay(10);
  3433. netif_carrier_off(tp->dev);
  3434. }
  3435. tg3_writephy(tp, MII_BMCR, new_bmcr);
  3436. bmcr = new_bmcr;
  3437. err |= tg3_readphy(tp, MII_BMSR, &bmsr);
  3438. err |= tg3_readphy(tp, MII_BMSR, &bmsr);
  3439. if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
  3440. ASIC_REV_5714) {
  3441. if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
  3442. bmsr |= BMSR_LSTATUS;
  3443. else
  3444. bmsr &= ~BMSR_LSTATUS;
  3445. }
  3446. tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
  3447. }
  3448. }
  3449. if (bmsr & BMSR_LSTATUS) {
  3450. current_speed = SPEED_1000;
  3451. current_link_up = 1;
  3452. if (bmcr & BMCR_FULLDPLX)
  3453. current_duplex = DUPLEX_FULL;
  3454. else
  3455. current_duplex = DUPLEX_HALF;
  3456. local_adv = 0;
  3457. remote_adv = 0;
  3458. if (bmcr & BMCR_ANENABLE) {
  3459. u32 common;
  3460. err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
  3461. err |= tg3_readphy(tp, MII_LPA, &remote_adv);
  3462. common = local_adv & remote_adv;
  3463. if (common & (ADVERTISE_1000XHALF |
  3464. ADVERTISE_1000XFULL)) {
  3465. if (common & ADVERTISE_1000XFULL)
  3466. current_duplex = DUPLEX_FULL;
  3467. else
  3468. current_duplex = DUPLEX_HALF;
  3469. } else if (!(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
  3470. /* Link is up via parallel detect */
  3471. } else {
  3472. current_link_up = 0;
  3473. }
  3474. }
  3475. }
  3476. if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
  3477. tg3_setup_flow_control(tp, local_adv, remote_adv);
  3478. tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
  3479. if (tp->link_config.active_duplex == DUPLEX_HALF)
  3480. tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
  3481. tw32_f(MAC_MODE, tp->mac_mode);
  3482. udelay(40);
  3483. tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
  3484. tp->link_config.active_speed = current_speed;
  3485. tp->link_config.active_duplex = current_duplex;
  3486. if (current_link_up != netif_carrier_ok(tp->dev)) {
  3487. if (current_link_up)
  3488. netif_carrier_on(tp->dev);
  3489. else {
  3490. netif_carrier_off(tp->dev);
  3491. tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
  3492. }
  3493. tg3_link_report(tp);
  3494. }
  3495. return err;
  3496. }
  3497. static void tg3_serdes_parallel_detect(struct tg3 *tp)
  3498. {
  3499. if (tp->serdes_counter) {
  3500. /* Give autoneg time to complete. */
  3501. tp->serdes_counter--;
  3502. return;
  3503. }
  3504. if (!netif_carrier_ok(tp->dev) &&
  3505. (tp->link_config.autoneg == AUTONEG_ENABLE)) {
  3506. u32 bmcr;
  3507. tg3_readphy(tp, MII_BMCR, &bmcr);
  3508. if (bmcr & BMCR_ANENABLE) {
  3509. u32 phy1, phy2;
  3510. /* Select shadow register 0x1f */
  3511. tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
  3512. tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
  3513. /* Select expansion interrupt status register */
  3514. tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
  3515. MII_TG3_DSP_EXP1_INT_STAT);
  3516. tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
  3517. tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
  3518. if ((phy1 & 0x10) && !(phy2 & 0x20)) {
  3519. /* We have signal detect and not receiving
  3520. * config code words, link is up by parallel
  3521. * detection.
  3522. */
  3523. bmcr &= ~BMCR_ANENABLE;
  3524. bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
  3525. tg3_writephy(tp, MII_BMCR, bmcr);
  3526. tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
  3527. }
  3528. }
  3529. } else if (netif_carrier_ok(tp->dev) &&
  3530. (tp->link_config.autoneg == AUTONEG_ENABLE) &&
  3531. (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
  3532. u32 phy2;
  3533. /* Select expansion interrupt status register */
  3534. tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
  3535. MII_TG3_DSP_EXP1_INT_STAT);
  3536. tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
  3537. if (phy2 & 0x20) {
  3538. u32 bmcr;
  3539. /* Config code words received, turn on autoneg. */
  3540. tg3_readphy(tp, MII_BMCR, &bmcr);
  3541. tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
  3542. tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
  3543. }
  3544. }
  3545. }
  3546. static int tg3_setup_phy(struct tg3 *tp, int force_reset)
  3547. {
  3548. int err;
  3549. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
  3550. err = tg3_setup_fiber_phy(tp, force_reset);
  3551. else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
  3552. err = tg3_setup_fiber_mii_phy(tp, force_reset);
  3553. else
  3554. err = tg3_setup_copper_phy(tp, force_reset);
  3555. if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
  3556. u32 val, scale;
  3557. val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
  3558. if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
  3559. scale = 65;
  3560. else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
  3561. scale = 6;
  3562. else
  3563. scale = 12;
  3564. val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
  3565. val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
  3566. tw32(GRC_MISC_CFG, val);
  3567. }
  3568. if (tp->link_config.active_speed == SPEED_1000 &&
  3569. tp->link_config.active_duplex == DUPLEX_HALF)
  3570. tw32(MAC_TX_LENGTHS,
  3571. ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
  3572. (6 << TX_LENGTHS_IPG_SHIFT) |
  3573. (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
  3574. else
  3575. tw32(MAC_TX_LENGTHS,
  3576. ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
  3577. (6 << TX_LENGTHS_IPG_SHIFT) |
  3578. (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
  3579. if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
  3580. if (netif_carrier_ok(tp->dev)) {
  3581. tw32(HOSTCC_STAT_COAL_TICKS,
  3582. tp->coal.stats_block_coalesce_usecs);
  3583. } else {
  3584. tw32(HOSTCC_STAT_COAL_TICKS, 0);
  3585. }
  3586. }
  3587. if (tp->tg3_flags & TG3_FLAG_ASPM_WORKAROUND) {
  3588. u32 val = tr32(PCIE_PWR_MGMT_THRESH);
  3589. if (!netif_carrier_ok(tp->dev))
  3590. val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
  3591. tp->pwrmgmt_thresh;
  3592. else
  3593. val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
  3594. tw32(PCIE_PWR_MGMT_THRESH, val);
  3595. }
  3596. return err;
  3597. }
  3598. static inline int tg3_irq_sync(struct tg3 *tp)
  3599. {
  3600. return tp->irq_sync;
  3601. }
  3602. /* This is called whenever we suspect that the system chipset is re-
  3603. * ordering the sequence of MMIO to the tx send mailbox. The symptom
  3604. * is bogus tx completions. We try to recover by setting the
  3605. * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
  3606. * in the workqueue.
  3607. */
  3608. static void tg3_tx_recover(struct tg3 *tp)
  3609. {
  3610. BUG_ON((tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER) ||
  3611. tp->write32_tx_mbox == tg3_write_indirect_mbox);
  3612. netdev_warn(tp->dev,
  3613. "The system may be re-ordering memory-mapped I/O "
  3614. "cycles to the network device, attempting to recover. "
  3615. "Please report the problem to the driver maintainer "
  3616. "and include system chipset information.\n");
  3617. spin_lock(&tp->lock);
  3618. tp->tg3_flags |= TG3_FLAG_TX_RECOVERY_PENDING;
  3619. spin_unlock(&tp->lock);
  3620. }
  3621. static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
  3622. {
  3623. /* Tell compiler to fetch tx indices from memory. */
  3624. barrier();
  3625. return tnapi->tx_pending -
  3626. ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
  3627. }
  3628. /* Tigon3 never reports partial packet sends. So we do not
  3629. * need special logic to handle SKBs that have not had all
  3630. * of their frags sent yet, like SunGEM does.
  3631. */
  3632. static void tg3_tx(struct tg3_napi *tnapi)
  3633. {
  3634. struct tg3 *tp = tnapi->tp;
  3635. u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
  3636. u32 sw_idx = tnapi->tx_cons;
  3637. struct netdev_queue *txq;
  3638. int index = tnapi - tp->napi;
  3639. if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
  3640. index--;
  3641. txq = netdev_get_tx_queue(tp->dev, index);
  3642. while (sw_idx != hw_idx) {
  3643. struct ring_info *ri = &tnapi->tx_buffers[sw_idx];
  3644. struct sk_buff *skb = ri->skb;
  3645. int i, tx_bug = 0;
  3646. if (unlikely(skb == NULL)) {
  3647. tg3_tx_recover(tp);
  3648. return;
  3649. }
  3650. pci_unmap_single(tp->pdev,
  3651. dma_unmap_addr(ri, mapping),
  3652. skb_headlen(skb),
  3653. PCI_DMA_TODEVICE);
  3654. ri->skb = NULL;
  3655. sw_idx = NEXT_TX(sw_idx);
  3656. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  3657. ri = &tnapi->tx_buffers[sw_idx];
  3658. if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
  3659. tx_bug = 1;
  3660. pci_unmap_page(tp->pdev,
  3661. dma_unmap_addr(ri, mapping),
  3662. skb_shinfo(skb)->frags[i].size,
  3663. PCI_DMA_TODEVICE);
  3664. sw_idx = NEXT_TX(sw_idx);
  3665. }
  3666. dev_kfree_skb(skb);
  3667. if (unlikely(tx_bug)) {
  3668. tg3_tx_recover(tp);
  3669. return;
  3670. }
  3671. }
  3672. tnapi->tx_cons = sw_idx;
  3673. /* Need to make the tx_cons update visible to tg3_start_xmit()
  3674. * before checking for netif_queue_stopped(). Without the
  3675. * memory barrier, there is a small possibility that tg3_start_xmit()
  3676. * will miss it and cause the queue to be stopped forever.
  3677. */
  3678. smp_mb();
  3679. if (unlikely(netif_tx_queue_stopped(txq) &&
  3680. (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
  3681. __netif_tx_lock(txq, smp_processor_id());
  3682. if (netif_tx_queue_stopped(txq) &&
  3683. (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
  3684. netif_tx_wake_queue(txq);
  3685. __netif_tx_unlock(txq);
  3686. }
  3687. }
  3688. static void tg3_rx_skb_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
  3689. {
  3690. if (!ri->skb)
  3691. return;
  3692. pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
  3693. map_sz, PCI_DMA_FROMDEVICE);
  3694. dev_kfree_skb_any(ri->skb);
  3695. ri->skb = NULL;
  3696. }
  3697. /* Returns size of skb allocated or < 0 on error.
  3698. *
  3699. * We only need to fill in the address because the other members
  3700. * of the RX descriptor are invariant, see tg3_init_rings.
  3701. *
  3702. * Note the purposeful assymetry of cpu vs. chip accesses. For
  3703. * posting buffers we only dirty the first cache line of the RX
  3704. * descriptor (containing the address). Whereas for the RX status
  3705. * buffers the cpu only reads the last cacheline of the RX descriptor
  3706. * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
  3707. */
  3708. static int tg3_alloc_rx_skb(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
  3709. u32 opaque_key, u32 dest_idx_unmasked)
  3710. {
  3711. struct tg3_rx_buffer_desc *desc;
  3712. struct ring_info *map, *src_map;
  3713. struct sk_buff *skb;
  3714. dma_addr_t mapping;
  3715. int skb_size, dest_idx;
  3716. src_map = NULL;
  3717. switch (opaque_key) {
  3718. case RXD_OPAQUE_RING_STD:
  3719. dest_idx = dest_idx_unmasked % TG3_RX_RING_SIZE;
  3720. desc = &tpr->rx_std[dest_idx];
  3721. map = &tpr->rx_std_buffers[dest_idx];
  3722. skb_size = tp->rx_pkt_map_sz;
  3723. break;
  3724. case RXD_OPAQUE_RING_JUMBO:
  3725. dest_idx = dest_idx_unmasked % TG3_RX_JUMBO_RING_SIZE;
  3726. desc = &tpr->rx_jmb[dest_idx].std;
  3727. map = &tpr->rx_jmb_buffers[dest_idx];
  3728. skb_size = TG3_RX_JMB_MAP_SZ;
  3729. break;
  3730. default:
  3731. return -EINVAL;
  3732. }
  3733. /* Do not overwrite any of the map or rp information
  3734. * until we are sure we can commit to a new buffer.
  3735. *
  3736. * Callers depend upon this behavior and assume that
  3737. * we leave everything unchanged if we fail.
  3738. */
  3739. skb = netdev_alloc_skb(tp->dev, skb_size + tp->rx_offset);
  3740. if (skb == NULL)
  3741. return -ENOMEM;
  3742. skb_reserve(skb, tp->rx_offset);
  3743. mapping = pci_map_single(tp->pdev, skb->data, skb_size,
  3744. PCI_DMA_FROMDEVICE);
  3745. if (pci_dma_mapping_error(tp->pdev, mapping)) {
  3746. dev_kfree_skb(skb);
  3747. return -EIO;
  3748. }
  3749. map->skb = skb;
  3750. dma_unmap_addr_set(map, mapping, mapping);
  3751. desc->addr_hi = ((u64)mapping >> 32);
  3752. desc->addr_lo = ((u64)mapping & 0xffffffff);
  3753. return skb_size;
  3754. }
  3755. /* We only need to move over in the address because the other
  3756. * members of the RX descriptor are invariant. See notes above
  3757. * tg3_alloc_rx_skb for full details.
  3758. */
  3759. static void tg3_recycle_rx(struct tg3_napi *tnapi,
  3760. struct tg3_rx_prodring_set *dpr,
  3761. u32 opaque_key, int src_idx,
  3762. u32 dest_idx_unmasked)
  3763. {
  3764. struct tg3 *tp = tnapi->tp;
  3765. struct tg3_rx_buffer_desc *src_desc, *dest_desc;
  3766. struct ring_info *src_map, *dest_map;
  3767. struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
  3768. int dest_idx;
  3769. switch (opaque_key) {
  3770. case RXD_OPAQUE_RING_STD:
  3771. dest_idx = dest_idx_unmasked % TG3_RX_RING_SIZE;
  3772. dest_desc = &dpr->rx_std[dest_idx];
  3773. dest_map = &dpr->rx_std_buffers[dest_idx];
  3774. src_desc = &spr->rx_std[src_idx];
  3775. src_map = &spr->rx_std_buffers[src_idx];
  3776. break;
  3777. case RXD_OPAQUE_RING_JUMBO:
  3778. dest_idx = dest_idx_unmasked % TG3_RX_JUMBO_RING_SIZE;
  3779. dest_desc = &dpr->rx_jmb[dest_idx].std;
  3780. dest_map = &dpr->rx_jmb_buffers[dest_idx];
  3781. src_desc = &spr->rx_jmb[src_idx].std;
  3782. src_map = &spr->rx_jmb_buffers[src_idx];
  3783. break;
  3784. default:
  3785. return;
  3786. }
  3787. dest_map->skb = src_map->skb;
  3788. dma_unmap_addr_set(dest_map, mapping,
  3789. dma_unmap_addr(src_map, mapping));
  3790. dest_desc->addr_hi = src_desc->addr_hi;
  3791. dest_desc->addr_lo = src_desc->addr_lo;
  3792. /* Ensure that the update to the skb happens after the physical
  3793. * addresses have been transferred to the new BD location.
  3794. */
  3795. smp_wmb();
  3796. src_map->skb = NULL;
  3797. }
  3798. /* The RX ring scheme is composed of multiple rings which post fresh
  3799. * buffers to the chip, and one special ring the chip uses to report
  3800. * status back to the host.
  3801. *
  3802. * The special ring reports the status of received packets to the
  3803. * host. The chip does not write into the original descriptor the
  3804. * RX buffer was obtained from. The chip simply takes the original
  3805. * descriptor as provided by the host, updates the status and length
  3806. * field, then writes this into the next status ring entry.
  3807. *
  3808. * Each ring the host uses to post buffers to the chip is described
  3809. * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
  3810. * it is first placed into the on-chip ram. When the packet's length
  3811. * is known, it walks down the TG3_BDINFO entries to select the ring.
  3812. * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
  3813. * which is within the range of the new packet's length is chosen.
  3814. *
  3815. * The "separate ring for rx status" scheme may sound queer, but it makes
  3816. * sense from a cache coherency perspective. If only the host writes
  3817. * to the buffer post rings, and only the chip writes to the rx status
  3818. * rings, then cache lines never move beyond shared-modified state.
  3819. * If both the host and chip were to write into the same ring, cache line
  3820. * eviction could occur since both entities want it in an exclusive state.
  3821. */
  3822. static int tg3_rx(struct tg3_napi *tnapi, int budget)
  3823. {
  3824. struct tg3 *tp = tnapi->tp;
  3825. u32 work_mask, rx_std_posted = 0;
  3826. u32 std_prod_idx, jmb_prod_idx;
  3827. u32 sw_idx = tnapi->rx_rcb_ptr;
  3828. u16 hw_idx;
  3829. int received;
  3830. struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
  3831. hw_idx = *(tnapi->rx_rcb_prod_idx);
  3832. /*
  3833. * We need to order the read of hw_idx and the read of
  3834. * the opaque cookie.
  3835. */
  3836. rmb();
  3837. work_mask = 0;
  3838. received = 0;
  3839. std_prod_idx = tpr->rx_std_prod_idx;
  3840. jmb_prod_idx = tpr->rx_jmb_prod_idx;
  3841. while (sw_idx != hw_idx && budget > 0) {
  3842. struct ring_info *ri;
  3843. struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
  3844. unsigned int len;
  3845. struct sk_buff *skb;
  3846. dma_addr_t dma_addr;
  3847. u32 opaque_key, desc_idx, *post_ptr;
  3848. bool hw_vlan __maybe_unused = false;
  3849. u16 vtag __maybe_unused = 0;
  3850. desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
  3851. opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
  3852. if (opaque_key == RXD_OPAQUE_RING_STD) {
  3853. ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
  3854. dma_addr = dma_unmap_addr(ri, mapping);
  3855. skb = ri->skb;
  3856. post_ptr = &std_prod_idx;
  3857. rx_std_posted++;
  3858. } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
  3859. ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
  3860. dma_addr = dma_unmap_addr(ri, mapping);
  3861. skb = ri->skb;
  3862. post_ptr = &jmb_prod_idx;
  3863. } else
  3864. goto next_pkt_nopost;
  3865. work_mask |= opaque_key;
  3866. if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
  3867. (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
  3868. drop_it:
  3869. tg3_recycle_rx(tnapi, tpr, opaque_key,
  3870. desc_idx, *post_ptr);
  3871. drop_it_no_recycle:
  3872. /* Other statistics kept track of by card. */
  3873. tp->net_stats.rx_dropped++;
  3874. goto next_pkt;
  3875. }
  3876. len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
  3877. ETH_FCS_LEN;
  3878. if (len > TG3_RX_COPY_THRESH(tp)) {
  3879. int skb_size;
  3880. skb_size = tg3_alloc_rx_skb(tp, tpr, opaque_key,
  3881. *post_ptr);
  3882. if (skb_size < 0)
  3883. goto drop_it;
  3884. pci_unmap_single(tp->pdev, dma_addr, skb_size,
  3885. PCI_DMA_FROMDEVICE);
  3886. /* Ensure that the update to the skb happens
  3887. * after the usage of the old DMA mapping.
  3888. */
  3889. smp_wmb();
  3890. ri->skb = NULL;
  3891. skb_put(skb, len);
  3892. } else {
  3893. struct sk_buff *copy_skb;
  3894. tg3_recycle_rx(tnapi, tpr, opaque_key,
  3895. desc_idx, *post_ptr);
  3896. copy_skb = netdev_alloc_skb(tp->dev, len + VLAN_HLEN +
  3897. TG3_RAW_IP_ALIGN);
  3898. if (copy_skb == NULL)
  3899. goto drop_it_no_recycle;
  3900. skb_reserve(copy_skb, TG3_RAW_IP_ALIGN + VLAN_HLEN);
  3901. skb_put(copy_skb, len);
  3902. pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
  3903. skb_copy_from_linear_data(skb, copy_skb->data, len);
  3904. pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
  3905. /* We'll reuse the original ring buffer. */
  3906. skb = copy_skb;
  3907. }
  3908. if ((tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) &&
  3909. (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
  3910. (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
  3911. >> RXD_TCPCSUM_SHIFT) == 0xffff))
  3912. skb->ip_summed = CHECKSUM_UNNECESSARY;
  3913. else
  3914. skb_checksum_none_assert(skb);
  3915. skb->protocol = eth_type_trans(skb, tp->dev);
  3916. if (len > (tp->dev->mtu + ETH_HLEN) &&
  3917. skb->protocol != htons(ETH_P_8021Q)) {
  3918. dev_kfree_skb(skb);
  3919. goto next_pkt;
  3920. }
  3921. if (desc->type_flags & RXD_FLAG_VLAN &&
  3922. !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG)) {
  3923. vtag = desc->err_vlan & RXD_VLAN_MASK;
  3924. #if TG3_VLAN_TAG_USED
  3925. if (tp->vlgrp)
  3926. hw_vlan = true;
  3927. else
  3928. #endif
  3929. {
  3930. struct vlan_ethhdr *ve = (struct vlan_ethhdr *)
  3931. __skb_push(skb, VLAN_HLEN);
  3932. memmove(ve, skb->data + VLAN_HLEN,
  3933. ETH_ALEN * 2);
  3934. ve->h_vlan_proto = htons(ETH_P_8021Q);
  3935. ve->h_vlan_TCI = htons(vtag);
  3936. }
  3937. }
  3938. #if TG3_VLAN_TAG_USED
  3939. if (hw_vlan)
  3940. vlan_gro_receive(&tnapi->napi, tp->vlgrp, vtag, skb);
  3941. else
  3942. #endif
  3943. napi_gro_receive(&tnapi->napi, skb);
  3944. received++;
  3945. budget--;
  3946. next_pkt:
  3947. (*post_ptr)++;
  3948. if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
  3949. tpr->rx_std_prod_idx = std_prod_idx % TG3_RX_RING_SIZE;
  3950. tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
  3951. tpr->rx_std_prod_idx);
  3952. work_mask &= ~RXD_OPAQUE_RING_STD;
  3953. rx_std_posted = 0;
  3954. }
  3955. next_pkt_nopost:
  3956. sw_idx++;
  3957. sw_idx &= (TG3_RX_RCB_RING_SIZE(tp) - 1);
  3958. /* Refresh hw_idx to see if there is new work */
  3959. if (sw_idx == hw_idx) {
  3960. hw_idx = *(tnapi->rx_rcb_prod_idx);
  3961. rmb();
  3962. }
  3963. }
  3964. /* ACK the status ring. */
  3965. tnapi->rx_rcb_ptr = sw_idx;
  3966. tw32_rx_mbox(tnapi->consmbox, sw_idx);
  3967. /* Refill RX ring(s). */
  3968. if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)) {
  3969. if (work_mask & RXD_OPAQUE_RING_STD) {
  3970. tpr->rx_std_prod_idx = std_prod_idx % TG3_RX_RING_SIZE;
  3971. tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
  3972. tpr->rx_std_prod_idx);
  3973. }
  3974. if (work_mask & RXD_OPAQUE_RING_JUMBO) {
  3975. tpr->rx_jmb_prod_idx = jmb_prod_idx %
  3976. TG3_RX_JUMBO_RING_SIZE;
  3977. tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
  3978. tpr->rx_jmb_prod_idx);
  3979. }
  3980. mmiowb();
  3981. } else if (work_mask) {
  3982. /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
  3983. * updated before the producer indices can be updated.
  3984. */
  3985. smp_wmb();
  3986. tpr->rx_std_prod_idx = std_prod_idx % TG3_RX_RING_SIZE;
  3987. tpr->rx_jmb_prod_idx = jmb_prod_idx % TG3_RX_JUMBO_RING_SIZE;
  3988. if (tnapi != &tp->napi[1])
  3989. napi_schedule(&tp->napi[1].napi);
  3990. }
  3991. return received;
  3992. }
  3993. static void tg3_poll_link(struct tg3 *tp)
  3994. {
  3995. /* handle link change and other phy events */
  3996. if (!(tp->tg3_flags &
  3997. (TG3_FLAG_USE_LINKCHG_REG |
  3998. TG3_FLAG_POLL_SERDES))) {
  3999. struct tg3_hw_status *sblk = tp->napi[0].hw_status;
  4000. if (sblk->status & SD_STATUS_LINK_CHG) {
  4001. sblk->status = SD_STATUS_UPDATED |
  4002. (sblk->status & ~SD_STATUS_LINK_CHG);
  4003. spin_lock(&tp->lock);
  4004. if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
  4005. tw32_f(MAC_STATUS,
  4006. (MAC_STATUS_SYNC_CHANGED |
  4007. MAC_STATUS_CFG_CHANGED |
  4008. MAC_STATUS_MI_COMPLETION |
  4009. MAC_STATUS_LNKSTATE_CHANGED));
  4010. udelay(40);
  4011. } else
  4012. tg3_setup_phy(tp, 0);
  4013. spin_unlock(&tp->lock);
  4014. }
  4015. }
  4016. }
  4017. static int tg3_rx_prodring_xfer(struct tg3 *tp,
  4018. struct tg3_rx_prodring_set *dpr,
  4019. struct tg3_rx_prodring_set *spr)
  4020. {
  4021. u32 si, di, cpycnt, src_prod_idx;
  4022. int i, err = 0;
  4023. while (1) {
  4024. src_prod_idx = spr->rx_std_prod_idx;
  4025. /* Make sure updates to the rx_std_buffers[] entries and the
  4026. * standard producer index are seen in the correct order.
  4027. */
  4028. smp_rmb();
  4029. if (spr->rx_std_cons_idx == src_prod_idx)
  4030. break;
  4031. if (spr->rx_std_cons_idx < src_prod_idx)
  4032. cpycnt = src_prod_idx - spr->rx_std_cons_idx;
  4033. else
  4034. cpycnt = TG3_RX_RING_SIZE - spr->rx_std_cons_idx;
  4035. cpycnt = min(cpycnt, TG3_RX_RING_SIZE - dpr->rx_std_prod_idx);
  4036. si = spr->rx_std_cons_idx;
  4037. di = dpr->rx_std_prod_idx;
  4038. for (i = di; i < di + cpycnt; i++) {
  4039. if (dpr->rx_std_buffers[i].skb) {
  4040. cpycnt = i - di;
  4041. err = -ENOSPC;
  4042. break;
  4043. }
  4044. }
  4045. if (!cpycnt)
  4046. break;
  4047. /* Ensure that updates to the rx_std_buffers ring and the
  4048. * shadowed hardware producer ring from tg3_recycle_skb() are
  4049. * ordered correctly WRT the skb check above.
  4050. */
  4051. smp_rmb();
  4052. memcpy(&dpr->rx_std_buffers[di],
  4053. &spr->rx_std_buffers[si],
  4054. cpycnt * sizeof(struct ring_info));
  4055. for (i = 0; i < cpycnt; i++, di++, si++) {
  4056. struct tg3_rx_buffer_desc *sbd, *dbd;
  4057. sbd = &spr->rx_std[si];
  4058. dbd = &dpr->rx_std[di];
  4059. dbd->addr_hi = sbd->addr_hi;
  4060. dbd->addr_lo = sbd->addr_lo;
  4061. }
  4062. spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) %
  4063. TG3_RX_RING_SIZE;
  4064. dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) %
  4065. TG3_RX_RING_SIZE;
  4066. }
  4067. while (1) {
  4068. src_prod_idx = spr->rx_jmb_prod_idx;
  4069. /* Make sure updates to the rx_jmb_buffers[] entries and
  4070. * the jumbo producer index are seen in the correct order.
  4071. */
  4072. smp_rmb();
  4073. if (spr->rx_jmb_cons_idx == src_prod_idx)
  4074. break;
  4075. if (spr->rx_jmb_cons_idx < src_prod_idx)
  4076. cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
  4077. else
  4078. cpycnt = TG3_RX_JUMBO_RING_SIZE - spr->rx_jmb_cons_idx;
  4079. cpycnt = min(cpycnt,
  4080. TG3_RX_JUMBO_RING_SIZE - dpr->rx_jmb_prod_idx);
  4081. si = spr->rx_jmb_cons_idx;
  4082. di = dpr->rx_jmb_prod_idx;
  4083. for (i = di; i < di + cpycnt; i++) {
  4084. if (dpr->rx_jmb_buffers[i].skb) {
  4085. cpycnt = i - di;
  4086. err = -ENOSPC;
  4087. break;
  4088. }
  4089. }
  4090. if (!cpycnt)
  4091. break;
  4092. /* Ensure that updates to the rx_jmb_buffers ring and the
  4093. * shadowed hardware producer ring from tg3_recycle_skb() are
  4094. * ordered correctly WRT the skb check above.
  4095. */
  4096. smp_rmb();
  4097. memcpy(&dpr->rx_jmb_buffers[di],
  4098. &spr->rx_jmb_buffers[si],
  4099. cpycnt * sizeof(struct ring_info));
  4100. for (i = 0; i < cpycnt; i++, di++, si++) {
  4101. struct tg3_rx_buffer_desc *sbd, *dbd;
  4102. sbd = &spr->rx_jmb[si].std;
  4103. dbd = &dpr->rx_jmb[di].std;
  4104. dbd->addr_hi = sbd->addr_hi;
  4105. dbd->addr_lo = sbd->addr_lo;
  4106. }
  4107. spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) %
  4108. TG3_RX_JUMBO_RING_SIZE;
  4109. dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) %
  4110. TG3_RX_JUMBO_RING_SIZE;
  4111. }
  4112. return err;
  4113. }
  4114. static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
  4115. {
  4116. struct tg3 *tp = tnapi->tp;
  4117. /* run TX completion thread */
  4118. if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
  4119. tg3_tx(tnapi);
  4120. if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
  4121. return work_done;
  4122. }
  4123. /* run RX thread, within the bounds set by NAPI.
  4124. * All RX "locking" is done by ensuring outside
  4125. * code synchronizes with tg3->napi.poll()
  4126. */
  4127. if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
  4128. work_done += tg3_rx(tnapi, budget - work_done);
  4129. if ((tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS) && tnapi == &tp->napi[1]) {
  4130. struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
  4131. int i, err = 0;
  4132. u32 std_prod_idx = dpr->rx_std_prod_idx;
  4133. u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
  4134. for (i = 1; i < tp->irq_cnt; i++)
  4135. err |= tg3_rx_prodring_xfer(tp, dpr,
  4136. &tp->napi[i].prodring);
  4137. wmb();
  4138. if (std_prod_idx != dpr->rx_std_prod_idx)
  4139. tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
  4140. dpr->rx_std_prod_idx);
  4141. if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
  4142. tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
  4143. dpr->rx_jmb_prod_idx);
  4144. mmiowb();
  4145. if (err)
  4146. tw32_f(HOSTCC_MODE, tp->coal_now);
  4147. }
  4148. return work_done;
  4149. }
  4150. static int tg3_poll_msix(struct napi_struct *napi, int budget)
  4151. {
  4152. struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
  4153. struct tg3 *tp = tnapi->tp;
  4154. int work_done = 0;
  4155. struct tg3_hw_status *sblk = tnapi->hw_status;
  4156. while (1) {
  4157. work_done = tg3_poll_work(tnapi, work_done, budget);
  4158. if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
  4159. goto tx_recovery;
  4160. if (unlikely(work_done >= budget))
  4161. break;
  4162. /* tp->last_tag is used in tg3_int_reenable() below
  4163. * to tell the hw how much work has been processed,
  4164. * so we must read it before checking for more work.
  4165. */
  4166. tnapi->last_tag = sblk->status_tag;
  4167. tnapi->last_irq_tag = tnapi->last_tag;
  4168. rmb();
  4169. /* check for RX/TX work to do */
  4170. if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
  4171. *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
  4172. napi_complete(napi);
  4173. /* Reenable interrupts. */
  4174. tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
  4175. mmiowb();
  4176. break;
  4177. }
  4178. }
  4179. return work_done;
  4180. tx_recovery:
  4181. /* work_done is guaranteed to be less than budget. */
  4182. napi_complete(napi);
  4183. schedule_work(&tp->reset_task);
  4184. return work_done;
  4185. }
  4186. static int tg3_poll(struct napi_struct *napi, int budget)
  4187. {
  4188. struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
  4189. struct tg3 *tp = tnapi->tp;
  4190. int work_done = 0;
  4191. struct tg3_hw_status *sblk = tnapi->hw_status;
  4192. while (1) {
  4193. tg3_poll_link(tp);
  4194. work_done = tg3_poll_work(tnapi, work_done, budget);
  4195. if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
  4196. goto tx_recovery;
  4197. if (unlikely(work_done >= budget))
  4198. break;
  4199. if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) {
  4200. /* tp->last_tag is used in tg3_int_reenable() below
  4201. * to tell the hw how much work has been processed,
  4202. * so we must read it before checking for more work.
  4203. */
  4204. tnapi->last_tag = sblk->status_tag;
  4205. tnapi->last_irq_tag = tnapi->last_tag;
  4206. rmb();
  4207. } else
  4208. sblk->status &= ~SD_STATUS_UPDATED;
  4209. if (likely(!tg3_has_work(tnapi))) {
  4210. napi_complete(napi);
  4211. tg3_int_reenable(tnapi);
  4212. break;
  4213. }
  4214. }
  4215. return work_done;
  4216. tx_recovery:
  4217. /* work_done is guaranteed to be less than budget. */
  4218. napi_complete(napi);
  4219. schedule_work(&tp->reset_task);
  4220. return work_done;
  4221. }
  4222. static void tg3_napi_disable(struct tg3 *tp)
  4223. {
  4224. int i;
  4225. for (i = tp->irq_cnt - 1; i >= 0; i--)
  4226. napi_disable(&tp->napi[i].napi);
  4227. }
  4228. static void tg3_napi_enable(struct tg3 *tp)
  4229. {
  4230. int i;
  4231. for (i = 0; i < tp->irq_cnt; i++)
  4232. napi_enable(&tp->napi[i].napi);
  4233. }
  4234. static void tg3_napi_init(struct tg3 *tp)
  4235. {
  4236. int i;
  4237. netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
  4238. for (i = 1; i < tp->irq_cnt; i++)
  4239. netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
  4240. }
  4241. static void tg3_napi_fini(struct tg3 *tp)
  4242. {
  4243. int i;
  4244. for (i = 0; i < tp->irq_cnt; i++)
  4245. netif_napi_del(&tp->napi[i].napi);
  4246. }
  4247. static inline void tg3_netif_stop(struct tg3 *tp)
  4248. {
  4249. tp->dev->trans_start = jiffies; /* prevent tx timeout */
  4250. tg3_napi_disable(tp);
  4251. netif_tx_disable(tp->dev);
  4252. }
  4253. static inline void tg3_netif_start(struct tg3 *tp)
  4254. {
  4255. /* NOTE: unconditional netif_tx_wake_all_queues is only
  4256. * appropriate so long as all callers are assured to
  4257. * have free tx slots (such as after tg3_init_hw)
  4258. */
  4259. netif_tx_wake_all_queues(tp->dev);
  4260. tg3_napi_enable(tp);
  4261. tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
  4262. tg3_enable_ints(tp);
  4263. }
  4264. static void tg3_irq_quiesce(struct tg3 *tp)
  4265. {
  4266. int i;
  4267. BUG_ON(tp->irq_sync);
  4268. tp->irq_sync = 1;
  4269. smp_mb();
  4270. for (i = 0; i < tp->irq_cnt; i++)
  4271. synchronize_irq(tp->napi[i].irq_vec);
  4272. }
  4273. /* Fully shutdown all tg3 driver activity elsewhere in the system.
  4274. * If irq_sync is non-zero, then the IRQ handler must be synchronized
  4275. * with as well. Most of the time, this is not necessary except when
  4276. * shutting down the device.
  4277. */
  4278. static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
  4279. {
  4280. spin_lock_bh(&tp->lock);
  4281. if (irq_sync)
  4282. tg3_irq_quiesce(tp);
  4283. }
  4284. static inline void tg3_full_unlock(struct tg3 *tp)
  4285. {
  4286. spin_unlock_bh(&tp->lock);
  4287. }
  4288. /* One-shot MSI handler - Chip automatically disables interrupt
  4289. * after sending MSI so driver doesn't have to do it.
  4290. */
  4291. static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
  4292. {
  4293. struct tg3_napi *tnapi = dev_id;
  4294. struct tg3 *tp = tnapi->tp;
  4295. prefetch(tnapi->hw_status);
  4296. if (tnapi->rx_rcb)
  4297. prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
  4298. if (likely(!tg3_irq_sync(tp)))
  4299. napi_schedule(&tnapi->napi);
  4300. return IRQ_HANDLED;
  4301. }
  4302. /* MSI ISR - No need to check for interrupt sharing and no need to
  4303. * flush status block and interrupt mailbox. PCI ordering rules
  4304. * guarantee that MSI will arrive after the status block.
  4305. */
  4306. static irqreturn_t tg3_msi(int irq, void *dev_id)
  4307. {
  4308. struct tg3_napi *tnapi = dev_id;
  4309. struct tg3 *tp = tnapi->tp;
  4310. prefetch(tnapi->hw_status);
  4311. if (tnapi->rx_rcb)
  4312. prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
  4313. /*
  4314. * Writing any value to intr-mbox-0 clears PCI INTA# and
  4315. * chip-internal interrupt pending events.
  4316. * Writing non-zero to intr-mbox-0 additional tells the
  4317. * NIC to stop sending us irqs, engaging "in-intr-handler"
  4318. * event coalescing.
  4319. */
  4320. tw32_mailbox(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
  4321. if (likely(!tg3_irq_sync(tp)))
  4322. napi_schedule(&tnapi->napi);
  4323. return IRQ_RETVAL(1);
  4324. }
  4325. static irqreturn_t tg3_interrupt(int irq, void *dev_id)
  4326. {
  4327. struct tg3_napi *tnapi = dev_id;
  4328. struct tg3 *tp = tnapi->tp;
  4329. struct tg3_hw_status *sblk = tnapi->hw_status;
  4330. unsigned int handled = 1;
  4331. /* In INTx mode, it is possible for the interrupt to arrive at
  4332. * the CPU before the status block posted prior to the interrupt.
  4333. * Reading the PCI State register will confirm whether the
  4334. * interrupt is ours and will flush the status block.
  4335. */
  4336. if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
  4337. if ((tp->tg3_flags & TG3_FLAG_CHIP_RESETTING) ||
  4338. (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
  4339. handled = 0;
  4340. goto out;
  4341. }
  4342. }
  4343. /*
  4344. * Writing any value to intr-mbox-0 clears PCI INTA# and
  4345. * chip-internal interrupt pending events.
  4346. * Writing non-zero to intr-mbox-0 additional tells the
  4347. * NIC to stop sending us irqs, engaging "in-intr-handler"
  4348. * event coalescing.
  4349. *
  4350. * Flush the mailbox to de-assert the IRQ immediately to prevent
  4351. * spurious interrupts. The flush impacts performance but
  4352. * excessive spurious interrupts can be worse in some cases.
  4353. */
  4354. tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
  4355. if (tg3_irq_sync(tp))
  4356. goto out;
  4357. sblk->status &= ~SD_STATUS_UPDATED;
  4358. if (likely(tg3_has_work(tnapi))) {
  4359. prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
  4360. napi_schedule(&tnapi->napi);
  4361. } else {
  4362. /* No work, shared interrupt perhaps? re-enable
  4363. * interrupts, and flush that PCI write
  4364. */
  4365. tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
  4366. 0x00000000);
  4367. }
  4368. out:
  4369. return IRQ_RETVAL(handled);
  4370. }
  4371. static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
  4372. {
  4373. struct tg3_napi *tnapi = dev_id;
  4374. struct tg3 *tp = tnapi->tp;
  4375. struct tg3_hw_status *sblk = tnapi->hw_status;
  4376. unsigned int handled = 1;
  4377. /* In INTx mode, it is possible for the interrupt to arrive at
  4378. * the CPU before the status block posted prior to the interrupt.
  4379. * Reading the PCI State register will confirm whether the
  4380. * interrupt is ours and will flush the status block.
  4381. */
  4382. if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
  4383. if ((tp->tg3_flags & TG3_FLAG_CHIP_RESETTING) ||
  4384. (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
  4385. handled = 0;
  4386. goto out;
  4387. }
  4388. }
  4389. /*
  4390. * writing any value to intr-mbox-0 clears PCI INTA# and
  4391. * chip-internal interrupt pending events.
  4392. * writing non-zero to intr-mbox-0 additional tells the
  4393. * NIC to stop sending us irqs, engaging "in-intr-handler"
  4394. * event coalescing.
  4395. *
  4396. * Flush the mailbox to de-assert the IRQ immediately to prevent
  4397. * spurious interrupts. The flush impacts performance but
  4398. * excessive spurious interrupts can be worse in some cases.
  4399. */
  4400. tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
  4401. /*
  4402. * In a shared interrupt configuration, sometimes other devices'
  4403. * interrupts will scream. We record the current status tag here
  4404. * so that the above check can report that the screaming interrupts
  4405. * are unhandled. Eventually they will be silenced.
  4406. */
  4407. tnapi->last_irq_tag = sblk->status_tag;
  4408. if (tg3_irq_sync(tp))
  4409. goto out;
  4410. prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
  4411. napi_schedule(&tnapi->napi);
  4412. out:
  4413. return IRQ_RETVAL(handled);
  4414. }
  4415. /* ISR for interrupt test */
  4416. static irqreturn_t tg3_test_isr(int irq, void *dev_id)
  4417. {
  4418. struct tg3_napi *tnapi = dev_id;
  4419. struct tg3 *tp = tnapi->tp;
  4420. struct tg3_hw_status *sblk = tnapi->hw_status;
  4421. if ((sblk->status & SD_STATUS_UPDATED) ||
  4422. !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
  4423. tg3_disable_ints(tp);
  4424. return IRQ_RETVAL(1);
  4425. }
  4426. return IRQ_RETVAL(0);
  4427. }
  4428. static int tg3_init_hw(struct tg3 *, int);
  4429. static int tg3_halt(struct tg3 *, int, int);
  4430. /* Restart hardware after configuration changes, self-test, etc.
  4431. * Invoked with tp->lock held.
  4432. */
  4433. static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
  4434. __releases(tp->lock)
  4435. __acquires(tp->lock)
  4436. {
  4437. int err;
  4438. err = tg3_init_hw(tp, reset_phy);
  4439. if (err) {
  4440. netdev_err(tp->dev,
  4441. "Failed to re-initialize device, aborting\n");
  4442. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  4443. tg3_full_unlock(tp);
  4444. del_timer_sync(&tp->timer);
  4445. tp->irq_sync = 0;
  4446. tg3_napi_enable(tp);
  4447. dev_close(tp->dev);
  4448. tg3_full_lock(tp, 0);
  4449. }
  4450. return err;
  4451. }
  4452. #ifdef CONFIG_NET_POLL_CONTROLLER
  4453. static void tg3_poll_controller(struct net_device *dev)
  4454. {
  4455. int i;
  4456. struct tg3 *tp = netdev_priv(dev);
  4457. for (i = 0; i < tp->irq_cnt; i++)
  4458. tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
  4459. }
  4460. #endif
  4461. static void tg3_reset_task(struct work_struct *work)
  4462. {
  4463. struct tg3 *tp = container_of(work, struct tg3, reset_task);
  4464. int err;
  4465. unsigned int restart_timer;
  4466. tg3_full_lock(tp, 0);
  4467. if (!netif_running(tp->dev)) {
  4468. tg3_full_unlock(tp);
  4469. return;
  4470. }
  4471. tg3_full_unlock(tp);
  4472. tg3_phy_stop(tp);
  4473. tg3_netif_stop(tp);
  4474. tg3_full_lock(tp, 1);
  4475. restart_timer = tp->tg3_flags2 & TG3_FLG2_RESTART_TIMER;
  4476. tp->tg3_flags2 &= ~TG3_FLG2_RESTART_TIMER;
  4477. if (tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING) {
  4478. tp->write32_tx_mbox = tg3_write32_tx_mbox;
  4479. tp->write32_rx_mbox = tg3_write_flush_reg32;
  4480. tp->tg3_flags |= TG3_FLAG_MBOX_WRITE_REORDER;
  4481. tp->tg3_flags &= ~TG3_FLAG_TX_RECOVERY_PENDING;
  4482. }
  4483. tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
  4484. err = tg3_init_hw(tp, 1);
  4485. if (err)
  4486. goto out;
  4487. tg3_netif_start(tp);
  4488. if (restart_timer)
  4489. mod_timer(&tp->timer, jiffies + 1);
  4490. out:
  4491. tg3_full_unlock(tp);
  4492. if (!err)
  4493. tg3_phy_start(tp);
  4494. }
  4495. static void tg3_dump_short_state(struct tg3 *tp)
  4496. {
  4497. netdev_err(tp->dev, "DEBUG: MAC_TX_STATUS[%08x] MAC_RX_STATUS[%08x]\n",
  4498. tr32(MAC_TX_STATUS), tr32(MAC_RX_STATUS));
  4499. netdev_err(tp->dev, "DEBUG: RDMAC_STATUS[%08x] WDMAC_STATUS[%08x]\n",
  4500. tr32(RDMAC_STATUS), tr32(WDMAC_STATUS));
  4501. }
  4502. static void tg3_tx_timeout(struct net_device *dev)
  4503. {
  4504. struct tg3 *tp = netdev_priv(dev);
  4505. if (netif_msg_tx_err(tp)) {
  4506. netdev_err(dev, "transmit timed out, resetting\n");
  4507. tg3_dump_short_state(tp);
  4508. }
  4509. schedule_work(&tp->reset_task);
  4510. }
  4511. /* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
  4512. static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
  4513. {
  4514. u32 base = (u32) mapping & 0xffffffff;
  4515. return (base > 0xffffdcc0) && (base + len + 8 < base);
  4516. }
  4517. /* Test for DMA addresses > 40-bit */
  4518. static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
  4519. int len)
  4520. {
  4521. #if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
  4522. if (tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG)
  4523. return ((u64) mapping + len) > DMA_BIT_MASK(40);
  4524. return 0;
  4525. #else
  4526. return 0;
  4527. #endif
  4528. }
  4529. static void tg3_set_txd(struct tg3_napi *, int, dma_addr_t, int, u32, u32);
  4530. /* Workaround 4GB and 40-bit hardware DMA bugs. */
  4531. static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
  4532. struct sk_buff *skb, u32 last_plus_one,
  4533. u32 *start, u32 base_flags, u32 mss)
  4534. {
  4535. struct tg3 *tp = tnapi->tp;
  4536. struct sk_buff *new_skb;
  4537. dma_addr_t new_addr = 0;
  4538. u32 entry = *start;
  4539. int i, ret = 0;
  4540. if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
  4541. new_skb = skb_copy(skb, GFP_ATOMIC);
  4542. else {
  4543. int more_headroom = 4 - ((unsigned long)skb->data & 3);
  4544. new_skb = skb_copy_expand(skb,
  4545. skb_headroom(skb) + more_headroom,
  4546. skb_tailroom(skb), GFP_ATOMIC);
  4547. }
  4548. if (!new_skb) {
  4549. ret = -1;
  4550. } else {
  4551. /* New SKB is guaranteed to be linear. */
  4552. entry = *start;
  4553. new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
  4554. PCI_DMA_TODEVICE);
  4555. /* Make sure the mapping succeeded */
  4556. if (pci_dma_mapping_error(tp->pdev, new_addr)) {
  4557. ret = -1;
  4558. dev_kfree_skb(new_skb);
  4559. new_skb = NULL;
  4560. /* Make sure new skb does not cross any 4G boundaries.
  4561. * Drop the packet if it does.
  4562. */
  4563. } else if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
  4564. tg3_4g_overflow_test(new_addr, new_skb->len)) {
  4565. pci_unmap_single(tp->pdev, new_addr, new_skb->len,
  4566. PCI_DMA_TODEVICE);
  4567. ret = -1;
  4568. dev_kfree_skb(new_skb);
  4569. new_skb = NULL;
  4570. } else {
  4571. tg3_set_txd(tnapi, entry, new_addr, new_skb->len,
  4572. base_flags, 1 | (mss << 1));
  4573. *start = NEXT_TX(entry);
  4574. }
  4575. }
  4576. /* Now clean up the sw ring entries. */
  4577. i = 0;
  4578. while (entry != last_plus_one) {
  4579. int len;
  4580. if (i == 0)
  4581. len = skb_headlen(skb);
  4582. else
  4583. len = skb_shinfo(skb)->frags[i-1].size;
  4584. pci_unmap_single(tp->pdev,
  4585. dma_unmap_addr(&tnapi->tx_buffers[entry],
  4586. mapping),
  4587. len, PCI_DMA_TODEVICE);
  4588. if (i == 0) {
  4589. tnapi->tx_buffers[entry].skb = new_skb;
  4590. dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
  4591. new_addr);
  4592. } else {
  4593. tnapi->tx_buffers[entry].skb = NULL;
  4594. }
  4595. entry = NEXT_TX(entry);
  4596. i++;
  4597. }
  4598. dev_kfree_skb(skb);
  4599. return ret;
  4600. }
  4601. static void tg3_set_txd(struct tg3_napi *tnapi, int entry,
  4602. dma_addr_t mapping, int len, u32 flags,
  4603. u32 mss_and_is_end)
  4604. {
  4605. struct tg3_tx_buffer_desc *txd = &tnapi->tx_ring[entry];
  4606. int is_end = (mss_and_is_end & 0x1);
  4607. u32 mss = (mss_and_is_end >> 1);
  4608. u32 vlan_tag = 0;
  4609. if (is_end)
  4610. flags |= TXD_FLAG_END;
  4611. if (flags & TXD_FLAG_VLAN) {
  4612. vlan_tag = flags >> 16;
  4613. flags &= 0xffff;
  4614. }
  4615. vlan_tag |= (mss << TXD_MSS_SHIFT);
  4616. txd->addr_hi = ((u64) mapping >> 32);
  4617. txd->addr_lo = ((u64) mapping & 0xffffffff);
  4618. txd->len_flags = (len << TXD_LEN_SHIFT) | flags;
  4619. txd->vlan_tag = vlan_tag << TXD_VLAN_TAG_SHIFT;
  4620. }
  4621. /* hard_start_xmit for devices that don't have any bugs and
  4622. * support TG3_FLG2_HW_TSO_2 and TG3_FLG2_HW_TSO_3 only.
  4623. */
  4624. static netdev_tx_t tg3_start_xmit(struct sk_buff *skb,
  4625. struct net_device *dev)
  4626. {
  4627. struct tg3 *tp = netdev_priv(dev);
  4628. u32 len, entry, base_flags, mss;
  4629. dma_addr_t mapping;
  4630. struct tg3_napi *tnapi;
  4631. struct netdev_queue *txq;
  4632. unsigned int i, last;
  4633. txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
  4634. tnapi = &tp->napi[skb_get_queue_mapping(skb)];
  4635. if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
  4636. tnapi++;
  4637. /* We are running in BH disabled context with netif_tx_lock
  4638. * and TX reclaim runs via tp->napi.poll inside of a software
  4639. * interrupt. Furthermore, IRQ processing runs lockless so we have
  4640. * no IRQ context deadlocks to worry about either. Rejoice!
  4641. */
  4642. if (unlikely(tg3_tx_avail(tnapi) <= (skb_shinfo(skb)->nr_frags + 1))) {
  4643. if (!netif_tx_queue_stopped(txq)) {
  4644. netif_tx_stop_queue(txq);
  4645. /* This is a hard error, log it. */
  4646. netdev_err(dev,
  4647. "BUG! Tx Ring full when queue awake!\n");
  4648. }
  4649. return NETDEV_TX_BUSY;
  4650. }
  4651. entry = tnapi->tx_prod;
  4652. base_flags = 0;
  4653. mss = skb_shinfo(skb)->gso_size;
  4654. if (mss) {
  4655. int tcp_opt_len, ip_tcp_len;
  4656. u32 hdrlen;
  4657. if (skb_header_cloned(skb) &&
  4658. pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
  4659. dev_kfree_skb(skb);
  4660. goto out_unlock;
  4661. }
  4662. if (skb_is_gso_v6(skb)) {
  4663. hdrlen = skb_headlen(skb) - ETH_HLEN;
  4664. } else {
  4665. struct iphdr *iph = ip_hdr(skb);
  4666. tcp_opt_len = tcp_optlen(skb);
  4667. ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
  4668. iph->check = 0;
  4669. iph->tot_len = htons(mss + ip_tcp_len + tcp_opt_len);
  4670. hdrlen = ip_tcp_len + tcp_opt_len;
  4671. }
  4672. if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) {
  4673. mss |= (hdrlen & 0xc) << 12;
  4674. if (hdrlen & 0x10)
  4675. base_flags |= 0x00000010;
  4676. base_flags |= (hdrlen & 0x3e0) << 5;
  4677. } else
  4678. mss |= hdrlen << 9;
  4679. base_flags |= (TXD_FLAG_CPU_PRE_DMA |
  4680. TXD_FLAG_CPU_POST_DMA);
  4681. tcp_hdr(skb)->check = 0;
  4682. } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
  4683. base_flags |= TXD_FLAG_TCPUDP_CSUM;
  4684. }
  4685. #if TG3_VLAN_TAG_USED
  4686. if (tp->vlgrp != NULL && vlan_tx_tag_present(skb))
  4687. base_flags |= (TXD_FLAG_VLAN |
  4688. (vlan_tx_tag_get(skb) << 16));
  4689. #endif
  4690. len = skb_headlen(skb);
  4691. /* Queue skb data, a.k.a. the main skb fragment. */
  4692. mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
  4693. if (pci_dma_mapping_error(tp->pdev, mapping)) {
  4694. dev_kfree_skb(skb);
  4695. goto out_unlock;
  4696. }
  4697. tnapi->tx_buffers[entry].skb = skb;
  4698. dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
  4699. if ((tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG) &&
  4700. !mss && skb->len > ETH_DATA_LEN)
  4701. base_flags |= TXD_FLAG_JMB_PKT;
  4702. tg3_set_txd(tnapi, entry, mapping, len, base_flags,
  4703. (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
  4704. entry = NEXT_TX(entry);
  4705. /* Now loop through additional data fragments, and queue them. */
  4706. if (skb_shinfo(skb)->nr_frags > 0) {
  4707. last = skb_shinfo(skb)->nr_frags - 1;
  4708. for (i = 0; i <= last; i++) {
  4709. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  4710. len = frag->size;
  4711. mapping = pci_map_page(tp->pdev,
  4712. frag->page,
  4713. frag->page_offset,
  4714. len, PCI_DMA_TODEVICE);
  4715. if (pci_dma_mapping_error(tp->pdev, mapping))
  4716. goto dma_error;
  4717. tnapi->tx_buffers[entry].skb = NULL;
  4718. dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
  4719. mapping);
  4720. tg3_set_txd(tnapi, entry, mapping, len,
  4721. base_flags, (i == last) | (mss << 1));
  4722. entry = NEXT_TX(entry);
  4723. }
  4724. }
  4725. /* Packets are ready, update Tx producer idx local and on card. */
  4726. tw32_tx_mbox(tnapi->prodmbox, entry);
  4727. tnapi->tx_prod = entry;
  4728. if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
  4729. netif_tx_stop_queue(txq);
  4730. /* netif_tx_stop_queue() must be done before checking
  4731. * checking tx index in tg3_tx_avail() below, because in
  4732. * tg3_tx(), we update tx index before checking for
  4733. * netif_tx_queue_stopped().
  4734. */
  4735. smp_mb();
  4736. if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
  4737. netif_tx_wake_queue(txq);
  4738. }
  4739. out_unlock:
  4740. mmiowb();
  4741. return NETDEV_TX_OK;
  4742. dma_error:
  4743. last = i;
  4744. entry = tnapi->tx_prod;
  4745. tnapi->tx_buffers[entry].skb = NULL;
  4746. pci_unmap_single(tp->pdev,
  4747. dma_unmap_addr(&tnapi->tx_buffers[entry], mapping),
  4748. skb_headlen(skb),
  4749. PCI_DMA_TODEVICE);
  4750. for (i = 0; i <= last; i++) {
  4751. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  4752. entry = NEXT_TX(entry);
  4753. pci_unmap_page(tp->pdev,
  4754. dma_unmap_addr(&tnapi->tx_buffers[entry],
  4755. mapping),
  4756. frag->size, PCI_DMA_TODEVICE);
  4757. }
  4758. dev_kfree_skb(skb);
  4759. return NETDEV_TX_OK;
  4760. }
  4761. static netdev_tx_t tg3_start_xmit_dma_bug(struct sk_buff *,
  4762. struct net_device *);
  4763. /* Use GSO to workaround a rare TSO bug that may be triggered when the
  4764. * TSO header is greater than 80 bytes.
  4765. */
  4766. static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
  4767. {
  4768. struct sk_buff *segs, *nskb;
  4769. u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
  4770. /* Estimate the number of fragments in the worst case */
  4771. if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
  4772. netif_stop_queue(tp->dev);
  4773. /* netif_tx_stop_queue() must be done before checking
  4774. * checking tx index in tg3_tx_avail() below, because in
  4775. * tg3_tx(), we update tx index before checking for
  4776. * netif_tx_queue_stopped().
  4777. */
  4778. smp_mb();
  4779. if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
  4780. return NETDEV_TX_BUSY;
  4781. netif_wake_queue(tp->dev);
  4782. }
  4783. segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
  4784. if (IS_ERR(segs))
  4785. goto tg3_tso_bug_end;
  4786. do {
  4787. nskb = segs;
  4788. segs = segs->next;
  4789. nskb->next = NULL;
  4790. tg3_start_xmit_dma_bug(nskb, tp->dev);
  4791. } while (segs);
  4792. tg3_tso_bug_end:
  4793. dev_kfree_skb(skb);
  4794. return NETDEV_TX_OK;
  4795. }
  4796. /* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
  4797. * support TG3_FLG2_HW_TSO_1 or firmware TSO only.
  4798. */
  4799. static netdev_tx_t tg3_start_xmit_dma_bug(struct sk_buff *skb,
  4800. struct net_device *dev)
  4801. {
  4802. struct tg3 *tp = netdev_priv(dev);
  4803. u32 len, entry, base_flags, mss;
  4804. int would_hit_hwbug;
  4805. dma_addr_t mapping;
  4806. struct tg3_napi *tnapi;
  4807. struct netdev_queue *txq;
  4808. unsigned int i, last;
  4809. txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
  4810. tnapi = &tp->napi[skb_get_queue_mapping(skb)];
  4811. if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
  4812. tnapi++;
  4813. /* We are running in BH disabled context with netif_tx_lock
  4814. * and TX reclaim runs via tp->napi.poll inside of a software
  4815. * interrupt. Furthermore, IRQ processing runs lockless so we have
  4816. * no IRQ context deadlocks to worry about either. Rejoice!
  4817. */
  4818. if (unlikely(tg3_tx_avail(tnapi) <= (skb_shinfo(skb)->nr_frags + 1))) {
  4819. if (!netif_tx_queue_stopped(txq)) {
  4820. netif_tx_stop_queue(txq);
  4821. /* This is a hard error, log it. */
  4822. netdev_err(dev,
  4823. "BUG! Tx Ring full when queue awake!\n");
  4824. }
  4825. return NETDEV_TX_BUSY;
  4826. }
  4827. entry = tnapi->tx_prod;
  4828. base_flags = 0;
  4829. if (skb->ip_summed == CHECKSUM_PARTIAL)
  4830. base_flags |= TXD_FLAG_TCPUDP_CSUM;
  4831. mss = skb_shinfo(skb)->gso_size;
  4832. if (mss) {
  4833. struct iphdr *iph;
  4834. u32 tcp_opt_len, hdr_len;
  4835. if (skb_header_cloned(skb) &&
  4836. pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
  4837. dev_kfree_skb(skb);
  4838. goto out_unlock;
  4839. }
  4840. iph = ip_hdr(skb);
  4841. tcp_opt_len = tcp_optlen(skb);
  4842. if (skb_is_gso_v6(skb)) {
  4843. hdr_len = skb_headlen(skb) - ETH_HLEN;
  4844. } else {
  4845. u32 ip_tcp_len;
  4846. ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
  4847. hdr_len = ip_tcp_len + tcp_opt_len;
  4848. iph->check = 0;
  4849. iph->tot_len = htons(mss + hdr_len);
  4850. }
  4851. if (unlikely((ETH_HLEN + hdr_len) > 80) &&
  4852. (tp->tg3_flags2 & TG3_FLG2_TSO_BUG))
  4853. return tg3_tso_bug(tp, skb);
  4854. base_flags |= (TXD_FLAG_CPU_PRE_DMA |
  4855. TXD_FLAG_CPU_POST_DMA);
  4856. if (tp->tg3_flags2 & TG3_FLG2_HW_TSO) {
  4857. tcp_hdr(skb)->check = 0;
  4858. base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
  4859. } else
  4860. tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
  4861. iph->daddr, 0,
  4862. IPPROTO_TCP,
  4863. 0);
  4864. if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) {
  4865. mss |= (hdr_len & 0xc) << 12;
  4866. if (hdr_len & 0x10)
  4867. base_flags |= 0x00000010;
  4868. base_flags |= (hdr_len & 0x3e0) << 5;
  4869. } else if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_2)
  4870. mss |= hdr_len << 9;
  4871. else if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_1) ||
  4872. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
  4873. if (tcp_opt_len || iph->ihl > 5) {
  4874. int tsflags;
  4875. tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
  4876. mss |= (tsflags << 11);
  4877. }
  4878. } else {
  4879. if (tcp_opt_len || iph->ihl > 5) {
  4880. int tsflags;
  4881. tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
  4882. base_flags |= tsflags << 12;
  4883. }
  4884. }
  4885. }
  4886. #if TG3_VLAN_TAG_USED
  4887. if (tp->vlgrp != NULL && vlan_tx_tag_present(skb))
  4888. base_flags |= (TXD_FLAG_VLAN |
  4889. (vlan_tx_tag_get(skb) << 16));
  4890. #endif
  4891. if ((tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG) &&
  4892. !mss && skb->len > ETH_DATA_LEN)
  4893. base_flags |= TXD_FLAG_JMB_PKT;
  4894. len = skb_headlen(skb);
  4895. mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
  4896. if (pci_dma_mapping_error(tp->pdev, mapping)) {
  4897. dev_kfree_skb(skb);
  4898. goto out_unlock;
  4899. }
  4900. tnapi->tx_buffers[entry].skb = skb;
  4901. dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
  4902. would_hit_hwbug = 0;
  4903. if ((tp->tg3_flags3 & TG3_FLG3_SHORT_DMA_BUG) && len <= 8)
  4904. would_hit_hwbug = 1;
  4905. if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
  4906. tg3_4g_overflow_test(mapping, len))
  4907. would_hit_hwbug = 1;
  4908. if ((tp->tg3_flags3 & TG3_FLG3_40BIT_DMA_LIMIT_BUG) &&
  4909. tg3_40bit_overflow_test(tp, mapping, len))
  4910. would_hit_hwbug = 1;
  4911. if (tp->tg3_flags3 & TG3_FLG3_5701_DMA_BUG)
  4912. would_hit_hwbug = 1;
  4913. tg3_set_txd(tnapi, entry, mapping, len, base_flags,
  4914. (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
  4915. entry = NEXT_TX(entry);
  4916. /* Now loop through additional data fragments, and queue them. */
  4917. if (skb_shinfo(skb)->nr_frags > 0) {
  4918. last = skb_shinfo(skb)->nr_frags - 1;
  4919. for (i = 0; i <= last; i++) {
  4920. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  4921. len = frag->size;
  4922. mapping = pci_map_page(tp->pdev,
  4923. frag->page,
  4924. frag->page_offset,
  4925. len, PCI_DMA_TODEVICE);
  4926. tnapi->tx_buffers[entry].skb = NULL;
  4927. dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
  4928. mapping);
  4929. if (pci_dma_mapping_error(tp->pdev, mapping))
  4930. goto dma_error;
  4931. if ((tp->tg3_flags3 & TG3_FLG3_SHORT_DMA_BUG) &&
  4932. len <= 8)
  4933. would_hit_hwbug = 1;
  4934. if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
  4935. tg3_4g_overflow_test(mapping, len))
  4936. would_hit_hwbug = 1;
  4937. if ((tp->tg3_flags3 & TG3_FLG3_40BIT_DMA_LIMIT_BUG) &&
  4938. tg3_40bit_overflow_test(tp, mapping, len))
  4939. would_hit_hwbug = 1;
  4940. if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
  4941. tg3_set_txd(tnapi, entry, mapping, len,
  4942. base_flags, (i == last)|(mss << 1));
  4943. else
  4944. tg3_set_txd(tnapi, entry, mapping, len,
  4945. base_flags, (i == last));
  4946. entry = NEXT_TX(entry);
  4947. }
  4948. }
  4949. if (would_hit_hwbug) {
  4950. u32 last_plus_one = entry;
  4951. u32 start;
  4952. start = entry - 1 - skb_shinfo(skb)->nr_frags;
  4953. start &= (TG3_TX_RING_SIZE - 1);
  4954. /* If the workaround fails due to memory/mapping
  4955. * failure, silently drop this packet.
  4956. */
  4957. if (tigon3_dma_hwbug_workaround(tnapi, skb, last_plus_one,
  4958. &start, base_flags, mss))
  4959. goto out_unlock;
  4960. entry = start;
  4961. }
  4962. /* Packets are ready, update Tx producer idx local and on card. */
  4963. tw32_tx_mbox(tnapi->prodmbox, entry);
  4964. tnapi->tx_prod = entry;
  4965. if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
  4966. netif_tx_stop_queue(txq);
  4967. /* netif_tx_stop_queue() must be done before checking
  4968. * checking tx index in tg3_tx_avail() below, because in
  4969. * tg3_tx(), we update tx index before checking for
  4970. * netif_tx_queue_stopped().
  4971. */
  4972. smp_mb();
  4973. if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
  4974. netif_tx_wake_queue(txq);
  4975. }
  4976. out_unlock:
  4977. mmiowb();
  4978. return NETDEV_TX_OK;
  4979. dma_error:
  4980. last = i;
  4981. entry = tnapi->tx_prod;
  4982. tnapi->tx_buffers[entry].skb = NULL;
  4983. pci_unmap_single(tp->pdev,
  4984. dma_unmap_addr(&tnapi->tx_buffers[entry], mapping),
  4985. skb_headlen(skb),
  4986. PCI_DMA_TODEVICE);
  4987. for (i = 0; i <= last; i++) {
  4988. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  4989. entry = NEXT_TX(entry);
  4990. pci_unmap_page(tp->pdev,
  4991. dma_unmap_addr(&tnapi->tx_buffers[entry],
  4992. mapping),
  4993. frag->size, PCI_DMA_TODEVICE);
  4994. }
  4995. dev_kfree_skb(skb);
  4996. return NETDEV_TX_OK;
  4997. }
  4998. static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
  4999. int new_mtu)
  5000. {
  5001. dev->mtu = new_mtu;
  5002. if (new_mtu > ETH_DATA_LEN) {
  5003. if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) {
  5004. tp->tg3_flags2 &= ~TG3_FLG2_TSO_CAPABLE;
  5005. ethtool_op_set_tso(dev, 0);
  5006. } else {
  5007. tp->tg3_flags |= TG3_FLAG_JUMBO_RING_ENABLE;
  5008. }
  5009. } else {
  5010. if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)
  5011. tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
  5012. tp->tg3_flags &= ~TG3_FLAG_JUMBO_RING_ENABLE;
  5013. }
  5014. }
  5015. static int tg3_change_mtu(struct net_device *dev, int new_mtu)
  5016. {
  5017. struct tg3 *tp = netdev_priv(dev);
  5018. int err;
  5019. if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
  5020. return -EINVAL;
  5021. if (!netif_running(dev)) {
  5022. /* We'll just catch it later when the
  5023. * device is up'd.
  5024. */
  5025. tg3_set_mtu(dev, tp, new_mtu);
  5026. return 0;
  5027. }
  5028. tg3_phy_stop(tp);
  5029. tg3_netif_stop(tp);
  5030. tg3_full_lock(tp, 1);
  5031. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  5032. tg3_set_mtu(dev, tp, new_mtu);
  5033. err = tg3_restart_hw(tp, 0);
  5034. if (!err)
  5035. tg3_netif_start(tp);
  5036. tg3_full_unlock(tp);
  5037. if (!err)
  5038. tg3_phy_start(tp);
  5039. return err;
  5040. }
  5041. static void tg3_rx_prodring_free(struct tg3 *tp,
  5042. struct tg3_rx_prodring_set *tpr)
  5043. {
  5044. int i;
  5045. if (tpr != &tp->napi[0].prodring) {
  5046. for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
  5047. i = (i + 1) % TG3_RX_RING_SIZE)
  5048. tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
  5049. tp->rx_pkt_map_sz);
  5050. if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
  5051. for (i = tpr->rx_jmb_cons_idx;
  5052. i != tpr->rx_jmb_prod_idx;
  5053. i = (i + 1) % TG3_RX_JUMBO_RING_SIZE) {
  5054. tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
  5055. TG3_RX_JMB_MAP_SZ);
  5056. }
  5057. }
  5058. return;
  5059. }
  5060. for (i = 0; i < TG3_RX_RING_SIZE; i++)
  5061. tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
  5062. tp->rx_pkt_map_sz);
  5063. if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
  5064. for (i = 0; i < TG3_RX_JUMBO_RING_SIZE; i++)
  5065. tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
  5066. TG3_RX_JMB_MAP_SZ);
  5067. }
  5068. }
  5069. /* Initialize rx rings for packet processing.
  5070. *
  5071. * The chip has been shut down and the driver detached from
  5072. * the networking, so no interrupts or new tx packets will
  5073. * end up in the driver. tp->{tx,}lock are held and thus
  5074. * we may not sleep.
  5075. */
  5076. static int tg3_rx_prodring_alloc(struct tg3 *tp,
  5077. struct tg3_rx_prodring_set *tpr)
  5078. {
  5079. u32 i, rx_pkt_dma_sz;
  5080. tpr->rx_std_cons_idx = 0;
  5081. tpr->rx_std_prod_idx = 0;
  5082. tpr->rx_jmb_cons_idx = 0;
  5083. tpr->rx_jmb_prod_idx = 0;
  5084. if (tpr != &tp->napi[0].prodring) {
  5085. memset(&tpr->rx_std_buffers[0], 0, TG3_RX_STD_BUFF_RING_SIZE);
  5086. if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE)
  5087. memset(&tpr->rx_jmb_buffers[0], 0,
  5088. TG3_RX_JMB_BUFF_RING_SIZE);
  5089. goto done;
  5090. }
  5091. /* Zero out all descriptors. */
  5092. memset(tpr->rx_std, 0, TG3_RX_RING_BYTES);
  5093. rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
  5094. if ((tp->tg3_flags2 & TG3_FLG2_5780_CLASS) &&
  5095. tp->dev->mtu > ETH_DATA_LEN)
  5096. rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
  5097. tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
  5098. /* Initialize invariants of the rings, we only set this
  5099. * stuff once. This works because the card does not
  5100. * write into the rx buffer posting rings.
  5101. */
  5102. for (i = 0; i < TG3_RX_RING_SIZE; i++) {
  5103. struct tg3_rx_buffer_desc *rxd;
  5104. rxd = &tpr->rx_std[i];
  5105. rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
  5106. rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
  5107. rxd->opaque = (RXD_OPAQUE_RING_STD |
  5108. (i << RXD_OPAQUE_INDEX_SHIFT));
  5109. }
  5110. /* Now allocate fresh SKBs for each rx ring. */
  5111. for (i = 0; i < tp->rx_pending; i++) {
  5112. if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_STD, i) < 0) {
  5113. netdev_warn(tp->dev,
  5114. "Using a smaller RX standard ring. Only "
  5115. "%d out of %d buffers were allocated "
  5116. "successfully\n", i, tp->rx_pending);
  5117. if (i == 0)
  5118. goto initfail;
  5119. tp->rx_pending = i;
  5120. break;
  5121. }
  5122. }
  5123. if (!(tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE))
  5124. goto done;
  5125. memset(tpr->rx_jmb, 0, TG3_RX_JUMBO_RING_BYTES);
  5126. if (!(tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE))
  5127. goto done;
  5128. for (i = 0; i < TG3_RX_JUMBO_RING_SIZE; i++) {
  5129. struct tg3_rx_buffer_desc *rxd;
  5130. rxd = &tpr->rx_jmb[i].std;
  5131. rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
  5132. rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
  5133. RXD_FLAG_JUMBO;
  5134. rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
  5135. (i << RXD_OPAQUE_INDEX_SHIFT));
  5136. }
  5137. for (i = 0; i < tp->rx_jumbo_pending; i++) {
  5138. if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_JUMBO, i) < 0) {
  5139. netdev_warn(tp->dev,
  5140. "Using a smaller RX jumbo ring. Only %d "
  5141. "out of %d buffers were allocated "
  5142. "successfully\n", i, tp->rx_jumbo_pending);
  5143. if (i == 0)
  5144. goto initfail;
  5145. tp->rx_jumbo_pending = i;
  5146. break;
  5147. }
  5148. }
  5149. done:
  5150. return 0;
  5151. initfail:
  5152. tg3_rx_prodring_free(tp, tpr);
  5153. return -ENOMEM;
  5154. }
  5155. static void tg3_rx_prodring_fini(struct tg3 *tp,
  5156. struct tg3_rx_prodring_set *tpr)
  5157. {
  5158. kfree(tpr->rx_std_buffers);
  5159. tpr->rx_std_buffers = NULL;
  5160. kfree(tpr->rx_jmb_buffers);
  5161. tpr->rx_jmb_buffers = NULL;
  5162. if (tpr->rx_std) {
  5163. pci_free_consistent(tp->pdev, TG3_RX_RING_BYTES,
  5164. tpr->rx_std, tpr->rx_std_mapping);
  5165. tpr->rx_std = NULL;
  5166. }
  5167. if (tpr->rx_jmb) {
  5168. pci_free_consistent(tp->pdev, TG3_RX_JUMBO_RING_BYTES,
  5169. tpr->rx_jmb, tpr->rx_jmb_mapping);
  5170. tpr->rx_jmb = NULL;
  5171. }
  5172. }
  5173. static int tg3_rx_prodring_init(struct tg3 *tp,
  5174. struct tg3_rx_prodring_set *tpr)
  5175. {
  5176. tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE, GFP_KERNEL);
  5177. if (!tpr->rx_std_buffers)
  5178. return -ENOMEM;
  5179. tpr->rx_std = pci_alloc_consistent(tp->pdev, TG3_RX_RING_BYTES,
  5180. &tpr->rx_std_mapping);
  5181. if (!tpr->rx_std)
  5182. goto err_out;
  5183. if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
  5184. tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE,
  5185. GFP_KERNEL);
  5186. if (!tpr->rx_jmb_buffers)
  5187. goto err_out;
  5188. tpr->rx_jmb = pci_alloc_consistent(tp->pdev,
  5189. TG3_RX_JUMBO_RING_BYTES,
  5190. &tpr->rx_jmb_mapping);
  5191. if (!tpr->rx_jmb)
  5192. goto err_out;
  5193. }
  5194. return 0;
  5195. err_out:
  5196. tg3_rx_prodring_fini(tp, tpr);
  5197. return -ENOMEM;
  5198. }
  5199. /* Free up pending packets in all rx/tx rings.
  5200. *
  5201. * The chip has been shut down and the driver detached from
  5202. * the networking, so no interrupts or new tx packets will
  5203. * end up in the driver. tp->{tx,}lock is not held and we are not
  5204. * in an interrupt context and thus may sleep.
  5205. */
  5206. static void tg3_free_rings(struct tg3 *tp)
  5207. {
  5208. int i, j;
  5209. for (j = 0; j < tp->irq_cnt; j++) {
  5210. struct tg3_napi *tnapi = &tp->napi[j];
  5211. tg3_rx_prodring_free(tp, &tnapi->prodring);
  5212. if (!tnapi->tx_buffers)
  5213. continue;
  5214. for (i = 0; i < TG3_TX_RING_SIZE; ) {
  5215. struct ring_info *txp;
  5216. struct sk_buff *skb;
  5217. unsigned int k;
  5218. txp = &tnapi->tx_buffers[i];
  5219. skb = txp->skb;
  5220. if (skb == NULL) {
  5221. i++;
  5222. continue;
  5223. }
  5224. pci_unmap_single(tp->pdev,
  5225. dma_unmap_addr(txp, mapping),
  5226. skb_headlen(skb),
  5227. PCI_DMA_TODEVICE);
  5228. txp->skb = NULL;
  5229. i++;
  5230. for (k = 0; k < skb_shinfo(skb)->nr_frags; k++) {
  5231. txp = &tnapi->tx_buffers[i & (TG3_TX_RING_SIZE - 1)];
  5232. pci_unmap_page(tp->pdev,
  5233. dma_unmap_addr(txp, mapping),
  5234. skb_shinfo(skb)->frags[k].size,
  5235. PCI_DMA_TODEVICE);
  5236. i++;
  5237. }
  5238. dev_kfree_skb_any(skb);
  5239. }
  5240. }
  5241. }
  5242. /* Initialize tx/rx rings for packet processing.
  5243. *
  5244. * The chip has been shut down and the driver detached from
  5245. * the networking, so no interrupts or new tx packets will
  5246. * end up in the driver. tp->{tx,}lock are held and thus
  5247. * we may not sleep.
  5248. */
  5249. static int tg3_init_rings(struct tg3 *tp)
  5250. {
  5251. int i;
  5252. /* Free up all the SKBs. */
  5253. tg3_free_rings(tp);
  5254. for (i = 0; i < tp->irq_cnt; i++) {
  5255. struct tg3_napi *tnapi = &tp->napi[i];
  5256. tnapi->last_tag = 0;
  5257. tnapi->last_irq_tag = 0;
  5258. tnapi->hw_status->status = 0;
  5259. tnapi->hw_status->status_tag = 0;
  5260. memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
  5261. tnapi->tx_prod = 0;
  5262. tnapi->tx_cons = 0;
  5263. if (tnapi->tx_ring)
  5264. memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
  5265. tnapi->rx_rcb_ptr = 0;
  5266. if (tnapi->rx_rcb)
  5267. memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
  5268. if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
  5269. tg3_free_rings(tp);
  5270. return -ENOMEM;
  5271. }
  5272. }
  5273. return 0;
  5274. }
  5275. /*
  5276. * Must not be invoked with interrupt sources disabled and
  5277. * the hardware shutdown down.
  5278. */
  5279. static void tg3_free_consistent(struct tg3 *tp)
  5280. {
  5281. int i;
  5282. for (i = 0; i < tp->irq_cnt; i++) {
  5283. struct tg3_napi *tnapi = &tp->napi[i];
  5284. if (tnapi->tx_ring) {
  5285. pci_free_consistent(tp->pdev, TG3_TX_RING_BYTES,
  5286. tnapi->tx_ring, tnapi->tx_desc_mapping);
  5287. tnapi->tx_ring = NULL;
  5288. }
  5289. kfree(tnapi->tx_buffers);
  5290. tnapi->tx_buffers = NULL;
  5291. if (tnapi->rx_rcb) {
  5292. pci_free_consistent(tp->pdev, TG3_RX_RCB_RING_BYTES(tp),
  5293. tnapi->rx_rcb,
  5294. tnapi->rx_rcb_mapping);
  5295. tnapi->rx_rcb = NULL;
  5296. }
  5297. tg3_rx_prodring_fini(tp, &tnapi->prodring);
  5298. if (tnapi->hw_status) {
  5299. pci_free_consistent(tp->pdev, TG3_HW_STATUS_SIZE,
  5300. tnapi->hw_status,
  5301. tnapi->status_mapping);
  5302. tnapi->hw_status = NULL;
  5303. }
  5304. }
  5305. if (tp->hw_stats) {
  5306. pci_free_consistent(tp->pdev, sizeof(struct tg3_hw_stats),
  5307. tp->hw_stats, tp->stats_mapping);
  5308. tp->hw_stats = NULL;
  5309. }
  5310. }
  5311. /*
  5312. * Must not be invoked with interrupt sources disabled and
  5313. * the hardware shutdown down. Can sleep.
  5314. */
  5315. static int tg3_alloc_consistent(struct tg3 *tp)
  5316. {
  5317. int i;
  5318. tp->hw_stats = pci_alloc_consistent(tp->pdev,
  5319. sizeof(struct tg3_hw_stats),
  5320. &tp->stats_mapping);
  5321. if (!tp->hw_stats)
  5322. goto err_out;
  5323. memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
  5324. for (i = 0; i < tp->irq_cnt; i++) {
  5325. struct tg3_napi *tnapi = &tp->napi[i];
  5326. struct tg3_hw_status *sblk;
  5327. tnapi->hw_status = pci_alloc_consistent(tp->pdev,
  5328. TG3_HW_STATUS_SIZE,
  5329. &tnapi->status_mapping);
  5330. if (!tnapi->hw_status)
  5331. goto err_out;
  5332. memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
  5333. sblk = tnapi->hw_status;
  5334. if (tg3_rx_prodring_init(tp, &tnapi->prodring))
  5335. goto err_out;
  5336. /* If multivector TSS is enabled, vector 0 does not handle
  5337. * tx interrupts. Don't allocate any resources for it.
  5338. */
  5339. if ((!i && !(tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)) ||
  5340. (i && (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS))) {
  5341. tnapi->tx_buffers = kzalloc(sizeof(struct ring_info) *
  5342. TG3_TX_RING_SIZE,
  5343. GFP_KERNEL);
  5344. if (!tnapi->tx_buffers)
  5345. goto err_out;
  5346. tnapi->tx_ring = pci_alloc_consistent(tp->pdev,
  5347. TG3_TX_RING_BYTES,
  5348. &tnapi->tx_desc_mapping);
  5349. if (!tnapi->tx_ring)
  5350. goto err_out;
  5351. }
  5352. /*
  5353. * When RSS is enabled, the status block format changes
  5354. * slightly. The "rx_jumbo_consumer", "reserved",
  5355. * and "rx_mini_consumer" members get mapped to the
  5356. * other three rx return ring producer indexes.
  5357. */
  5358. switch (i) {
  5359. default:
  5360. tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
  5361. break;
  5362. case 2:
  5363. tnapi->rx_rcb_prod_idx = &sblk->rx_jumbo_consumer;
  5364. break;
  5365. case 3:
  5366. tnapi->rx_rcb_prod_idx = &sblk->reserved;
  5367. break;
  5368. case 4:
  5369. tnapi->rx_rcb_prod_idx = &sblk->rx_mini_consumer;
  5370. break;
  5371. }
  5372. /*
  5373. * If multivector RSS is enabled, vector 0 does not handle
  5374. * rx or tx interrupts. Don't allocate any resources for it.
  5375. */
  5376. if (!i && (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS))
  5377. continue;
  5378. tnapi->rx_rcb = pci_alloc_consistent(tp->pdev,
  5379. TG3_RX_RCB_RING_BYTES(tp),
  5380. &tnapi->rx_rcb_mapping);
  5381. if (!tnapi->rx_rcb)
  5382. goto err_out;
  5383. memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
  5384. }
  5385. return 0;
  5386. err_out:
  5387. tg3_free_consistent(tp);
  5388. return -ENOMEM;
  5389. }
  5390. #define MAX_WAIT_CNT 1000
  5391. /* To stop a block, clear the enable bit and poll till it
  5392. * clears. tp->lock is held.
  5393. */
  5394. static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
  5395. {
  5396. unsigned int i;
  5397. u32 val;
  5398. if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
  5399. switch (ofs) {
  5400. case RCVLSC_MODE:
  5401. case DMAC_MODE:
  5402. case MBFREE_MODE:
  5403. case BUFMGR_MODE:
  5404. case MEMARB_MODE:
  5405. /* We can't enable/disable these bits of the
  5406. * 5705/5750, just say success.
  5407. */
  5408. return 0;
  5409. default:
  5410. break;
  5411. }
  5412. }
  5413. val = tr32(ofs);
  5414. val &= ~enable_bit;
  5415. tw32_f(ofs, val);
  5416. for (i = 0; i < MAX_WAIT_CNT; i++) {
  5417. udelay(100);
  5418. val = tr32(ofs);
  5419. if ((val & enable_bit) == 0)
  5420. break;
  5421. }
  5422. if (i == MAX_WAIT_CNT && !silent) {
  5423. dev_err(&tp->pdev->dev,
  5424. "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
  5425. ofs, enable_bit);
  5426. return -ENODEV;
  5427. }
  5428. return 0;
  5429. }
  5430. /* tp->lock is held. */
  5431. static int tg3_abort_hw(struct tg3 *tp, int silent)
  5432. {
  5433. int i, err;
  5434. tg3_disable_ints(tp);
  5435. tp->rx_mode &= ~RX_MODE_ENABLE;
  5436. tw32_f(MAC_RX_MODE, tp->rx_mode);
  5437. udelay(10);
  5438. err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
  5439. err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
  5440. err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
  5441. err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
  5442. err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
  5443. err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
  5444. err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
  5445. err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
  5446. err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
  5447. err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
  5448. err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
  5449. err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
  5450. err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
  5451. tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
  5452. tw32_f(MAC_MODE, tp->mac_mode);
  5453. udelay(40);
  5454. tp->tx_mode &= ~TX_MODE_ENABLE;
  5455. tw32_f(MAC_TX_MODE, tp->tx_mode);
  5456. for (i = 0; i < MAX_WAIT_CNT; i++) {
  5457. udelay(100);
  5458. if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
  5459. break;
  5460. }
  5461. if (i >= MAX_WAIT_CNT) {
  5462. dev_err(&tp->pdev->dev,
  5463. "%s timed out, TX_MODE_ENABLE will not clear "
  5464. "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
  5465. err |= -ENODEV;
  5466. }
  5467. err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
  5468. err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
  5469. err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
  5470. tw32(FTQ_RESET, 0xffffffff);
  5471. tw32(FTQ_RESET, 0x00000000);
  5472. err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
  5473. err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
  5474. for (i = 0; i < tp->irq_cnt; i++) {
  5475. struct tg3_napi *tnapi = &tp->napi[i];
  5476. if (tnapi->hw_status)
  5477. memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
  5478. }
  5479. if (tp->hw_stats)
  5480. memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
  5481. return err;
  5482. }
  5483. static void tg3_ape_send_event(struct tg3 *tp, u32 event)
  5484. {
  5485. int i;
  5486. u32 apedata;
  5487. /* NCSI does not support APE events */
  5488. if (tp->tg3_flags3 & TG3_FLG3_APE_HAS_NCSI)
  5489. return;
  5490. apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
  5491. if (apedata != APE_SEG_SIG_MAGIC)
  5492. return;
  5493. apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
  5494. if (!(apedata & APE_FW_STATUS_READY))
  5495. return;
  5496. /* Wait for up to 1 millisecond for APE to service previous event. */
  5497. for (i = 0; i < 10; i++) {
  5498. if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
  5499. return;
  5500. apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
  5501. if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
  5502. tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
  5503. event | APE_EVENT_STATUS_EVENT_PENDING);
  5504. tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
  5505. if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
  5506. break;
  5507. udelay(100);
  5508. }
  5509. if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
  5510. tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
  5511. }
  5512. static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
  5513. {
  5514. u32 event;
  5515. u32 apedata;
  5516. if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
  5517. return;
  5518. switch (kind) {
  5519. case RESET_KIND_INIT:
  5520. tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
  5521. APE_HOST_SEG_SIG_MAGIC);
  5522. tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
  5523. APE_HOST_SEG_LEN_MAGIC);
  5524. apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
  5525. tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
  5526. tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
  5527. APE_HOST_DRIVER_ID_MAGIC(TG3_MAJ_NUM, TG3_MIN_NUM));
  5528. tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
  5529. APE_HOST_BEHAV_NO_PHYLOCK);
  5530. tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE,
  5531. TG3_APE_HOST_DRVR_STATE_START);
  5532. event = APE_EVENT_STATUS_STATE_START;
  5533. break;
  5534. case RESET_KIND_SHUTDOWN:
  5535. /* With the interface we are currently using,
  5536. * APE does not track driver state. Wiping
  5537. * out the HOST SEGMENT SIGNATURE forces
  5538. * the APE to assume OS absent status.
  5539. */
  5540. tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
  5541. if (device_may_wakeup(&tp->pdev->dev) &&
  5542. (tp->tg3_flags & TG3_FLAG_WOL_ENABLE)) {
  5543. tg3_ape_write32(tp, TG3_APE_HOST_WOL_SPEED,
  5544. TG3_APE_HOST_WOL_SPEED_AUTO);
  5545. apedata = TG3_APE_HOST_DRVR_STATE_WOL;
  5546. } else
  5547. apedata = TG3_APE_HOST_DRVR_STATE_UNLOAD;
  5548. tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE, apedata);
  5549. event = APE_EVENT_STATUS_STATE_UNLOAD;
  5550. break;
  5551. case RESET_KIND_SUSPEND:
  5552. event = APE_EVENT_STATUS_STATE_SUSPEND;
  5553. break;
  5554. default:
  5555. return;
  5556. }
  5557. event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
  5558. tg3_ape_send_event(tp, event);
  5559. }
  5560. /* tp->lock is held. */
  5561. static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
  5562. {
  5563. tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
  5564. NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
  5565. if (tp->tg3_flags2 & TG3_FLG2_ASF_NEW_HANDSHAKE) {
  5566. switch (kind) {
  5567. case RESET_KIND_INIT:
  5568. tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
  5569. DRV_STATE_START);
  5570. break;
  5571. case RESET_KIND_SHUTDOWN:
  5572. tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
  5573. DRV_STATE_UNLOAD);
  5574. break;
  5575. case RESET_KIND_SUSPEND:
  5576. tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
  5577. DRV_STATE_SUSPEND);
  5578. break;
  5579. default:
  5580. break;
  5581. }
  5582. }
  5583. if (kind == RESET_KIND_INIT ||
  5584. kind == RESET_KIND_SUSPEND)
  5585. tg3_ape_driver_state_change(tp, kind);
  5586. }
  5587. /* tp->lock is held. */
  5588. static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
  5589. {
  5590. if (tp->tg3_flags2 & TG3_FLG2_ASF_NEW_HANDSHAKE) {
  5591. switch (kind) {
  5592. case RESET_KIND_INIT:
  5593. tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
  5594. DRV_STATE_START_DONE);
  5595. break;
  5596. case RESET_KIND_SHUTDOWN:
  5597. tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
  5598. DRV_STATE_UNLOAD_DONE);
  5599. break;
  5600. default:
  5601. break;
  5602. }
  5603. }
  5604. if (kind == RESET_KIND_SHUTDOWN)
  5605. tg3_ape_driver_state_change(tp, kind);
  5606. }
  5607. /* tp->lock is held. */
  5608. static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
  5609. {
  5610. if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) {
  5611. switch (kind) {
  5612. case RESET_KIND_INIT:
  5613. tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
  5614. DRV_STATE_START);
  5615. break;
  5616. case RESET_KIND_SHUTDOWN:
  5617. tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
  5618. DRV_STATE_UNLOAD);
  5619. break;
  5620. case RESET_KIND_SUSPEND:
  5621. tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
  5622. DRV_STATE_SUSPEND);
  5623. break;
  5624. default:
  5625. break;
  5626. }
  5627. }
  5628. }
  5629. static int tg3_poll_fw(struct tg3 *tp)
  5630. {
  5631. int i;
  5632. u32 val;
  5633. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
  5634. /* Wait up to 20ms for init done. */
  5635. for (i = 0; i < 200; i++) {
  5636. if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
  5637. return 0;
  5638. udelay(100);
  5639. }
  5640. return -ENODEV;
  5641. }
  5642. /* Wait for firmware initialization to complete. */
  5643. for (i = 0; i < 100000; i++) {
  5644. tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
  5645. if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
  5646. break;
  5647. udelay(10);
  5648. }
  5649. /* Chip might not be fitted with firmware. Some Sun onboard
  5650. * parts are configured like that. So don't signal the timeout
  5651. * of the above loop as an error, but do report the lack of
  5652. * running firmware once.
  5653. */
  5654. if (i >= 100000 &&
  5655. !(tp->tg3_flags2 & TG3_FLG2_NO_FWARE_REPORTED)) {
  5656. tp->tg3_flags2 |= TG3_FLG2_NO_FWARE_REPORTED;
  5657. netdev_info(tp->dev, "No firmware running\n");
  5658. }
  5659. if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
  5660. /* The 57765 A0 needs a little more
  5661. * time to do some important work.
  5662. */
  5663. mdelay(10);
  5664. }
  5665. return 0;
  5666. }
  5667. /* Save PCI command register before chip reset */
  5668. static void tg3_save_pci_state(struct tg3 *tp)
  5669. {
  5670. pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
  5671. }
  5672. /* Restore PCI state after chip reset */
  5673. static void tg3_restore_pci_state(struct tg3 *tp)
  5674. {
  5675. u32 val;
  5676. /* Re-enable indirect register accesses. */
  5677. pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
  5678. tp->misc_host_ctrl);
  5679. /* Set MAX PCI retry to zero. */
  5680. val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
  5681. if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
  5682. (tp->tg3_flags & TG3_FLAG_PCIX_MODE))
  5683. val |= PCISTATE_RETRY_SAME_DMA;
  5684. /* Allow reads and writes to the APE register and memory space. */
  5685. if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
  5686. val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
  5687. PCISTATE_ALLOW_APE_SHMEM_WR |
  5688. PCISTATE_ALLOW_APE_PSPACE_WR;
  5689. pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
  5690. pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
  5691. if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785) {
  5692. if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)
  5693. pcie_set_readrq(tp->pdev, 4096);
  5694. else {
  5695. pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
  5696. tp->pci_cacheline_sz);
  5697. pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
  5698. tp->pci_lat_timer);
  5699. }
  5700. }
  5701. /* Make sure PCI-X relaxed ordering bit is clear. */
  5702. if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
  5703. u16 pcix_cmd;
  5704. pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
  5705. &pcix_cmd);
  5706. pcix_cmd &= ~PCI_X_CMD_ERO;
  5707. pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
  5708. pcix_cmd);
  5709. }
  5710. if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) {
  5711. /* Chip reset on 5780 will reset MSI enable bit,
  5712. * so need to restore it.
  5713. */
  5714. if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
  5715. u16 ctrl;
  5716. pci_read_config_word(tp->pdev,
  5717. tp->msi_cap + PCI_MSI_FLAGS,
  5718. &ctrl);
  5719. pci_write_config_word(tp->pdev,
  5720. tp->msi_cap + PCI_MSI_FLAGS,
  5721. ctrl | PCI_MSI_FLAGS_ENABLE);
  5722. val = tr32(MSGINT_MODE);
  5723. tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
  5724. }
  5725. }
  5726. }
  5727. static void tg3_stop_fw(struct tg3 *);
  5728. /* tp->lock is held. */
  5729. static int tg3_chip_reset(struct tg3 *tp)
  5730. {
  5731. u32 val;
  5732. void (*write_op)(struct tg3 *, u32, u32);
  5733. int i, err;
  5734. tg3_nvram_lock(tp);
  5735. tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
  5736. /* No matching tg3_nvram_unlock() after this because
  5737. * chip reset below will undo the nvram lock.
  5738. */
  5739. tp->nvram_lock_cnt = 0;
  5740. /* GRC_MISC_CFG core clock reset will clear the memory
  5741. * enable bit in PCI register 4 and the MSI enable bit
  5742. * on some chips, so we save relevant registers here.
  5743. */
  5744. tg3_save_pci_state(tp);
  5745. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
  5746. (tp->tg3_flags3 & TG3_FLG3_5755_PLUS))
  5747. tw32(GRC_FASTBOOT_PC, 0);
  5748. /*
  5749. * We must avoid the readl() that normally takes place.
  5750. * It locks machines, causes machine checks, and other
  5751. * fun things. So, temporarily disable the 5701
  5752. * hardware workaround, while we do the reset.
  5753. */
  5754. write_op = tp->write32;
  5755. if (write_op == tg3_write_flush_reg32)
  5756. tp->write32 = tg3_write32;
  5757. /* Prevent the irq handler from reading or writing PCI registers
  5758. * during chip reset when the memory enable bit in the PCI command
  5759. * register may be cleared. The chip does not generate interrupt
  5760. * at this time, but the irq handler may still be called due to irq
  5761. * sharing or irqpoll.
  5762. */
  5763. tp->tg3_flags |= TG3_FLAG_CHIP_RESETTING;
  5764. for (i = 0; i < tp->irq_cnt; i++) {
  5765. struct tg3_napi *tnapi = &tp->napi[i];
  5766. if (tnapi->hw_status) {
  5767. tnapi->hw_status->status = 0;
  5768. tnapi->hw_status->status_tag = 0;
  5769. }
  5770. tnapi->last_tag = 0;
  5771. tnapi->last_irq_tag = 0;
  5772. }
  5773. smp_mb();
  5774. for (i = 0; i < tp->irq_cnt; i++)
  5775. synchronize_irq(tp->napi[i].irq_vec);
  5776. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
  5777. val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
  5778. tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
  5779. }
  5780. /* do the reset */
  5781. val = GRC_MISC_CFG_CORECLK_RESET;
  5782. if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
  5783. /* Force PCIe 1.0a mode */
  5784. if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
  5785. !(tp->tg3_flags3 & TG3_FLG3_5717_PLUS) &&
  5786. tr32(TG3_PCIE_PHY_TSTCTL) ==
  5787. (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
  5788. tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
  5789. if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
  5790. tw32(GRC_MISC_CFG, (1 << 29));
  5791. val |= (1 << 29);
  5792. }
  5793. }
  5794. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
  5795. tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
  5796. tw32(GRC_VCPU_EXT_CTRL,
  5797. tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
  5798. }
  5799. /* Manage gphy power for all CPMU absent PCIe devices. */
  5800. if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
  5801. !(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT))
  5802. val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
  5803. tw32(GRC_MISC_CFG, val);
  5804. /* restore 5701 hardware bug workaround write method */
  5805. tp->write32 = write_op;
  5806. /* Unfortunately, we have to delay before the PCI read back.
  5807. * Some 575X chips even will not respond to a PCI cfg access
  5808. * when the reset command is given to the chip.
  5809. *
  5810. * How do these hardware designers expect things to work
  5811. * properly if the PCI write is posted for a long period
  5812. * of time? It is always necessary to have some method by
  5813. * which a register read back can occur to push the write
  5814. * out which does the reset.
  5815. *
  5816. * For most tg3 variants the trick below was working.
  5817. * Ho hum...
  5818. */
  5819. udelay(120);
  5820. /* Flush PCI posted writes. The normal MMIO registers
  5821. * are inaccessible at this time so this is the only
  5822. * way to make this reliably (actually, this is no longer
  5823. * the case, see above). I tried to use indirect
  5824. * register read/write but this upset some 5701 variants.
  5825. */
  5826. pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
  5827. udelay(120);
  5828. if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) && tp->pcie_cap) {
  5829. u16 val16;
  5830. if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
  5831. int i;
  5832. u32 cfg_val;
  5833. /* Wait for link training to complete. */
  5834. for (i = 0; i < 5000; i++)
  5835. udelay(100);
  5836. pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
  5837. pci_write_config_dword(tp->pdev, 0xc4,
  5838. cfg_val | (1 << 15));
  5839. }
  5840. /* Clear the "no snoop" and "relaxed ordering" bits. */
  5841. pci_read_config_word(tp->pdev,
  5842. tp->pcie_cap + PCI_EXP_DEVCTL,
  5843. &val16);
  5844. val16 &= ~(PCI_EXP_DEVCTL_RELAX_EN |
  5845. PCI_EXP_DEVCTL_NOSNOOP_EN);
  5846. /*
  5847. * Older PCIe devices only support the 128 byte
  5848. * MPS setting. Enforce the restriction.
  5849. */
  5850. if (!(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT))
  5851. val16 &= ~PCI_EXP_DEVCTL_PAYLOAD;
  5852. pci_write_config_word(tp->pdev,
  5853. tp->pcie_cap + PCI_EXP_DEVCTL,
  5854. val16);
  5855. pcie_set_readrq(tp->pdev, 4096);
  5856. /* Clear error status */
  5857. pci_write_config_word(tp->pdev,
  5858. tp->pcie_cap + PCI_EXP_DEVSTA,
  5859. PCI_EXP_DEVSTA_CED |
  5860. PCI_EXP_DEVSTA_NFED |
  5861. PCI_EXP_DEVSTA_FED |
  5862. PCI_EXP_DEVSTA_URD);
  5863. }
  5864. tg3_restore_pci_state(tp);
  5865. tp->tg3_flags &= ~TG3_FLAG_CHIP_RESETTING;
  5866. val = 0;
  5867. if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)
  5868. val = tr32(MEMARB_MODE);
  5869. tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
  5870. if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
  5871. tg3_stop_fw(tp);
  5872. tw32(0x5000, 0x400);
  5873. }
  5874. tw32(GRC_MODE, tp->grc_mode);
  5875. if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
  5876. val = tr32(0xc4);
  5877. tw32(0xc4, val | (1 << 15));
  5878. }
  5879. if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
  5880. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
  5881. tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
  5882. if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
  5883. tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
  5884. tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
  5885. }
  5886. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
  5887. tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
  5888. tw32_f(MAC_MODE, tp->mac_mode);
  5889. } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
  5890. tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
  5891. tw32_f(MAC_MODE, tp->mac_mode);
  5892. } else if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
  5893. tp->mac_mode &= (MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN);
  5894. if (tp->mac_mode & MAC_MODE_APE_TX_EN)
  5895. tp->mac_mode |= MAC_MODE_TDE_ENABLE;
  5896. tw32_f(MAC_MODE, tp->mac_mode);
  5897. } else
  5898. tw32_f(MAC_MODE, 0);
  5899. udelay(40);
  5900. tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
  5901. err = tg3_poll_fw(tp);
  5902. if (err)
  5903. return err;
  5904. tg3_mdio_start(tp);
  5905. if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
  5906. tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
  5907. GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
  5908. !(tp->tg3_flags3 & TG3_FLG3_5717_PLUS)) {
  5909. val = tr32(0x7c00);
  5910. tw32(0x7c00, val | (1 << 25));
  5911. }
  5912. /* Reprobe ASF enable state. */
  5913. tp->tg3_flags &= ~TG3_FLAG_ENABLE_ASF;
  5914. tp->tg3_flags2 &= ~TG3_FLG2_ASF_NEW_HANDSHAKE;
  5915. tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
  5916. if (val == NIC_SRAM_DATA_SIG_MAGIC) {
  5917. u32 nic_cfg;
  5918. tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
  5919. if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
  5920. tp->tg3_flags |= TG3_FLAG_ENABLE_ASF;
  5921. tp->last_event_jiffies = jiffies;
  5922. if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
  5923. tp->tg3_flags2 |= TG3_FLG2_ASF_NEW_HANDSHAKE;
  5924. }
  5925. }
  5926. return 0;
  5927. }
  5928. /* tp->lock is held. */
  5929. static void tg3_stop_fw(struct tg3 *tp)
  5930. {
  5931. if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
  5932. !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
  5933. /* Wait for RX cpu to ACK the previous event. */
  5934. tg3_wait_for_event_ack(tp);
  5935. tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
  5936. tg3_generate_fw_event(tp);
  5937. /* Wait for RX cpu to ACK this event. */
  5938. tg3_wait_for_event_ack(tp);
  5939. }
  5940. }
  5941. /* tp->lock is held. */
  5942. static int tg3_halt(struct tg3 *tp, int kind, int silent)
  5943. {
  5944. int err;
  5945. tg3_stop_fw(tp);
  5946. tg3_write_sig_pre_reset(tp, kind);
  5947. tg3_abort_hw(tp, silent);
  5948. err = tg3_chip_reset(tp);
  5949. __tg3_set_mac_addr(tp, 0);
  5950. tg3_write_sig_legacy(tp, kind);
  5951. tg3_write_sig_post_reset(tp, kind);
  5952. if (err)
  5953. return err;
  5954. return 0;
  5955. }
  5956. #define RX_CPU_SCRATCH_BASE 0x30000
  5957. #define RX_CPU_SCRATCH_SIZE 0x04000
  5958. #define TX_CPU_SCRATCH_BASE 0x34000
  5959. #define TX_CPU_SCRATCH_SIZE 0x04000
  5960. /* tp->lock is held. */
  5961. static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
  5962. {
  5963. int i;
  5964. BUG_ON(offset == TX_CPU_BASE &&
  5965. (tp->tg3_flags2 & TG3_FLG2_5705_PLUS));
  5966. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
  5967. u32 val = tr32(GRC_VCPU_EXT_CTRL);
  5968. tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
  5969. return 0;
  5970. }
  5971. if (offset == RX_CPU_BASE) {
  5972. for (i = 0; i < 10000; i++) {
  5973. tw32(offset + CPU_STATE, 0xffffffff);
  5974. tw32(offset + CPU_MODE, CPU_MODE_HALT);
  5975. if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
  5976. break;
  5977. }
  5978. tw32(offset + CPU_STATE, 0xffffffff);
  5979. tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
  5980. udelay(10);
  5981. } else {
  5982. for (i = 0; i < 10000; i++) {
  5983. tw32(offset + CPU_STATE, 0xffffffff);
  5984. tw32(offset + CPU_MODE, CPU_MODE_HALT);
  5985. if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
  5986. break;
  5987. }
  5988. }
  5989. if (i >= 10000) {
  5990. netdev_err(tp->dev, "%s timed out, %s CPU\n",
  5991. __func__, offset == RX_CPU_BASE ? "RX" : "TX");
  5992. return -ENODEV;
  5993. }
  5994. /* Clear firmware's nvram arbitration. */
  5995. if (tp->tg3_flags & TG3_FLAG_NVRAM)
  5996. tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
  5997. return 0;
  5998. }
  5999. struct fw_info {
  6000. unsigned int fw_base;
  6001. unsigned int fw_len;
  6002. const __be32 *fw_data;
  6003. };
  6004. /* tp->lock is held. */
  6005. static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base, u32 cpu_scratch_base,
  6006. int cpu_scratch_size, struct fw_info *info)
  6007. {
  6008. int err, lock_err, i;
  6009. void (*write_op)(struct tg3 *, u32, u32);
  6010. if (cpu_base == TX_CPU_BASE &&
  6011. (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
  6012. netdev_err(tp->dev,
  6013. "%s: Trying to load TX cpu firmware which is 5705\n",
  6014. __func__);
  6015. return -EINVAL;
  6016. }
  6017. if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
  6018. write_op = tg3_write_mem;
  6019. else
  6020. write_op = tg3_write_indirect_reg32;
  6021. /* It is possible that bootcode is still loading at this point.
  6022. * Get the nvram lock first before halting the cpu.
  6023. */
  6024. lock_err = tg3_nvram_lock(tp);
  6025. err = tg3_halt_cpu(tp, cpu_base);
  6026. if (!lock_err)
  6027. tg3_nvram_unlock(tp);
  6028. if (err)
  6029. goto out;
  6030. for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
  6031. write_op(tp, cpu_scratch_base + i, 0);
  6032. tw32(cpu_base + CPU_STATE, 0xffffffff);
  6033. tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
  6034. for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
  6035. write_op(tp, (cpu_scratch_base +
  6036. (info->fw_base & 0xffff) +
  6037. (i * sizeof(u32))),
  6038. be32_to_cpu(info->fw_data[i]));
  6039. err = 0;
  6040. out:
  6041. return err;
  6042. }
  6043. /* tp->lock is held. */
  6044. static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
  6045. {
  6046. struct fw_info info;
  6047. const __be32 *fw_data;
  6048. int err, i;
  6049. fw_data = (void *)tp->fw->data;
  6050. /* Firmware blob starts with version numbers, followed by
  6051. start address and length. We are setting complete length.
  6052. length = end_address_of_bss - start_address_of_text.
  6053. Remainder is the blob to be loaded contiguously
  6054. from start address. */
  6055. info.fw_base = be32_to_cpu(fw_data[1]);
  6056. info.fw_len = tp->fw->size - 12;
  6057. info.fw_data = &fw_data[3];
  6058. err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
  6059. RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
  6060. &info);
  6061. if (err)
  6062. return err;
  6063. err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
  6064. TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
  6065. &info);
  6066. if (err)
  6067. return err;
  6068. /* Now startup only the RX cpu. */
  6069. tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
  6070. tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
  6071. for (i = 0; i < 5; i++) {
  6072. if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
  6073. break;
  6074. tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
  6075. tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
  6076. tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
  6077. udelay(1000);
  6078. }
  6079. if (i >= 5) {
  6080. netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
  6081. "should be %08x\n", __func__,
  6082. tr32(RX_CPU_BASE + CPU_PC), info.fw_base);
  6083. return -ENODEV;
  6084. }
  6085. tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
  6086. tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
  6087. return 0;
  6088. }
  6089. /* 5705 needs a special version of the TSO firmware. */
  6090. /* tp->lock is held. */
  6091. static int tg3_load_tso_firmware(struct tg3 *tp)
  6092. {
  6093. struct fw_info info;
  6094. const __be32 *fw_data;
  6095. unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
  6096. int err, i;
  6097. if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
  6098. return 0;
  6099. fw_data = (void *)tp->fw->data;
  6100. /* Firmware blob starts with version numbers, followed by
  6101. start address and length. We are setting complete length.
  6102. length = end_address_of_bss - start_address_of_text.
  6103. Remainder is the blob to be loaded contiguously
  6104. from start address. */
  6105. info.fw_base = be32_to_cpu(fw_data[1]);
  6106. cpu_scratch_size = tp->fw_len;
  6107. info.fw_len = tp->fw->size - 12;
  6108. info.fw_data = &fw_data[3];
  6109. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
  6110. cpu_base = RX_CPU_BASE;
  6111. cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
  6112. } else {
  6113. cpu_base = TX_CPU_BASE;
  6114. cpu_scratch_base = TX_CPU_SCRATCH_BASE;
  6115. cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
  6116. }
  6117. err = tg3_load_firmware_cpu(tp, cpu_base,
  6118. cpu_scratch_base, cpu_scratch_size,
  6119. &info);
  6120. if (err)
  6121. return err;
  6122. /* Now startup the cpu. */
  6123. tw32(cpu_base + CPU_STATE, 0xffffffff);
  6124. tw32_f(cpu_base + CPU_PC, info.fw_base);
  6125. for (i = 0; i < 5; i++) {
  6126. if (tr32(cpu_base + CPU_PC) == info.fw_base)
  6127. break;
  6128. tw32(cpu_base + CPU_STATE, 0xffffffff);
  6129. tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
  6130. tw32_f(cpu_base + CPU_PC, info.fw_base);
  6131. udelay(1000);
  6132. }
  6133. if (i >= 5) {
  6134. netdev_err(tp->dev,
  6135. "%s fails to set CPU PC, is %08x should be %08x\n",
  6136. __func__, tr32(cpu_base + CPU_PC), info.fw_base);
  6137. return -ENODEV;
  6138. }
  6139. tw32(cpu_base + CPU_STATE, 0xffffffff);
  6140. tw32_f(cpu_base + CPU_MODE, 0x00000000);
  6141. return 0;
  6142. }
  6143. static int tg3_set_mac_addr(struct net_device *dev, void *p)
  6144. {
  6145. struct tg3 *tp = netdev_priv(dev);
  6146. struct sockaddr *addr = p;
  6147. int err = 0, skip_mac_1 = 0;
  6148. if (!is_valid_ether_addr(addr->sa_data))
  6149. return -EINVAL;
  6150. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  6151. if (!netif_running(dev))
  6152. return 0;
  6153. if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) {
  6154. u32 addr0_high, addr0_low, addr1_high, addr1_low;
  6155. addr0_high = tr32(MAC_ADDR_0_HIGH);
  6156. addr0_low = tr32(MAC_ADDR_0_LOW);
  6157. addr1_high = tr32(MAC_ADDR_1_HIGH);
  6158. addr1_low = tr32(MAC_ADDR_1_LOW);
  6159. /* Skip MAC addr 1 if ASF is using it. */
  6160. if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
  6161. !(addr1_high == 0 && addr1_low == 0))
  6162. skip_mac_1 = 1;
  6163. }
  6164. spin_lock_bh(&tp->lock);
  6165. __tg3_set_mac_addr(tp, skip_mac_1);
  6166. spin_unlock_bh(&tp->lock);
  6167. return err;
  6168. }
  6169. /* tp->lock is held. */
  6170. static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
  6171. dma_addr_t mapping, u32 maxlen_flags,
  6172. u32 nic_addr)
  6173. {
  6174. tg3_write_mem(tp,
  6175. (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
  6176. ((u64) mapping >> 32));
  6177. tg3_write_mem(tp,
  6178. (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
  6179. ((u64) mapping & 0xffffffff));
  6180. tg3_write_mem(tp,
  6181. (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
  6182. maxlen_flags);
  6183. if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
  6184. tg3_write_mem(tp,
  6185. (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
  6186. nic_addr);
  6187. }
  6188. static void __tg3_set_rx_mode(struct net_device *);
  6189. static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
  6190. {
  6191. int i;
  6192. if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)) {
  6193. tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
  6194. tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
  6195. tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
  6196. } else {
  6197. tw32(HOSTCC_TXCOL_TICKS, 0);
  6198. tw32(HOSTCC_TXMAX_FRAMES, 0);
  6199. tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
  6200. }
  6201. if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)) {
  6202. tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
  6203. tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
  6204. tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
  6205. } else {
  6206. tw32(HOSTCC_RXCOL_TICKS, 0);
  6207. tw32(HOSTCC_RXMAX_FRAMES, 0);
  6208. tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
  6209. }
  6210. if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
  6211. u32 val = ec->stats_block_coalesce_usecs;
  6212. tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
  6213. tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
  6214. if (!netif_carrier_ok(tp->dev))
  6215. val = 0;
  6216. tw32(HOSTCC_STAT_COAL_TICKS, val);
  6217. }
  6218. for (i = 0; i < tp->irq_cnt - 1; i++) {
  6219. u32 reg;
  6220. reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
  6221. tw32(reg, ec->rx_coalesce_usecs);
  6222. reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
  6223. tw32(reg, ec->rx_max_coalesced_frames);
  6224. reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
  6225. tw32(reg, ec->rx_max_coalesced_frames_irq);
  6226. if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS) {
  6227. reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
  6228. tw32(reg, ec->tx_coalesce_usecs);
  6229. reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
  6230. tw32(reg, ec->tx_max_coalesced_frames);
  6231. reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
  6232. tw32(reg, ec->tx_max_coalesced_frames_irq);
  6233. }
  6234. }
  6235. for (; i < tp->irq_max - 1; i++) {
  6236. tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
  6237. tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
  6238. tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
  6239. if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS) {
  6240. tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
  6241. tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
  6242. tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
  6243. }
  6244. }
  6245. }
  6246. /* tp->lock is held. */
  6247. static void tg3_rings_reset(struct tg3 *tp)
  6248. {
  6249. int i;
  6250. u32 stblk, txrcb, rxrcb, limit;
  6251. struct tg3_napi *tnapi = &tp->napi[0];
  6252. /* Disable all transmit rings but the first. */
  6253. if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
  6254. limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
  6255. else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
  6256. limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
  6257. else
  6258. limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
  6259. for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
  6260. txrcb < limit; txrcb += TG3_BDINFO_SIZE)
  6261. tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
  6262. BDINFO_FLAGS_DISABLED);
  6263. /* Disable all receive return rings but the first. */
  6264. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
  6265. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
  6266. limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
  6267. else if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
  6268. limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
  6269. else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
  6270. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
  6271. limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
  6272. else
  6273. limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
  6274. for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
  6275. rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
  6276. tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
  6277. BDINFO_FLAGS_DISABLED);
  6278. /* Disable interrupts */
  6279. tw32_mailbox_f(tp->napi[0].int_mbox, 1);
  6280. /* Zero mailbox registers. */
  6281. if (tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX) {
  6282. for (i = 1; i < tp->irq_max; i++) {
  6283. tp->napi[i].tx_prod = 0;
  6284. tp->napi[i].tx_cons = 0;
  6285. if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
  6286. tw32_mailbox(tp->napi[i].prodmbox, 0);
  6287. tw32_rx_mbox(tp->napi[i].consmbox, 0);
  6288. tw32_mailbox_f(tp->napi[i].int_mbox, 1);
  6289. }
  6290. if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS))
  6291. tw32_mailbox(tp->napi[0].prodmbox, 0);
  6292. } else {
  6293. tp->napi[0].tx_prod = 0;
  6294. tp->napi[0].tx_cons = 0;
  6295. tw32_mailbox(tp->napi[0].prodmbox, 0);
  6296. tw32_rx_mbox(tp->napi[0].consmbox, 0);
  6297. }
  6298. /* Make sure the NIC-based send BD rings are disabled. */
  6299. if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
  6300. u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
  6301. for (i = 0; i < 16; i++)
  6302. tw32_tx_mbox(mbox + i * 8, 0);
  6303. }
  6304. txrcb = NIC_SRAM_SEND_RCB;
  6305. rxrcb = NIC_SRAM_RCV_RET_RCB;
  6306. /* Clear status block in ram. */
  6307. memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
  6308. /* Set status block DMA address */
  6309. tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
  6310. ((u64) tnapi->status_mapping >> 32));
  6311. tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
  6312. ((u64) tnapi->status_mapping & 0xffffffff));
  6313. if (tnapi->tx_ring) {
  6314. tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
  6315. (TG3_TX_RING_SIZE <<
  6316. BDINFO_FLAGS_MAXLEN_SHIFT),
  6317. NIC_SRAM_TX_BUFFER_DESC);
  6318. txrcb += TG3_BDINFO_SIZE;
  6319. }
  6320. if (tnapi->rx_rcb) {
  6321. tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
  6322. (TG3_RX_RCB_RING_SIZE(tp) <<
  6323. BDINFO_FLAGS_MAXLEN_SHIFT), 0);
  6324. rxrcb += TG3_BDINFO_SIZE;
  6325. }
  6326. stblk = HOSTCC_STATBLCK_RING1;
  6327. for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
  6328. u64 mapping = (u64)tnapi->status_mapping;
  6329. tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
  6330. tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
  6331. /* Clear status block in ram. */
  6332. memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
  6333. if (tnapi->tx_ring) {
  6334. tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
  6335. (TG3_TX_RING_SIZE <<
  6336. BDINFO_FLAGS_MAXLEN_SHIFT),
  6337. NIC_SRAM_TX_BUFFER_DESC);
  6338. txrcb += TG3_BDINFO_SIZE;
  6339. }
  6340. tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
  6341. (TG3_RX_RCB_RING_SIZE(tp) <<
  6342. BDINFO_FLAGS_MAXLEN_SHIFT), 0);
  6343. stblk += 8;
  6344. rxrcb += TG3_BDINFO_SIZE;
  6345. }
  6346. }
  6347. /* tp->lock is held. */
  6348. static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
  6349. {
  6350. u32 val, rdmac_mode;
  6351. int i, err, limit;
  6352. struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
  6353. tg3_disable_ints(tp);
  6354. tg3_stop_fw(tp);
  6355. tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
  6356. if (tp->tg3_flags & TG3_FLAG_INIT_COMPLETE)
  6357. tg3_abort_hw(tp, 1);
  6358. if (reset_phy)
  6359. tg3_phy_reset(tp);
  6360. err = tg3_chip_reset(tp);
  6361. if (err)
  6362. return err;
  6363. tg3_write_sig_legacy(tp, RESET_KIND_INIT);
  6364. if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
  6365. val = tr32(TG3_CPMU_CTRL);
  6366. val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
  6367. tw32(TG3_CPMU_CTRL, val);
  6368. val = tr32(TG3_CPMU_LSPD_10MB_CLK);
  6369. val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
  6370. val |= CPMU_LSPD_10MB_MACCLK_6_25;
  6371. tw32(TG3_CPMU_LSPD_10MB_CLK, val);
  6372. val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
  6373. val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
  6374. val |= CPMU_LNK_AWARE_MACCLK_6_25;
  6375. tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
  6376. val = tr32(TG3_CPMU_HST_ACC);
  6377. val &= ~CPMU_HST_ACC_MACCLK_MASK;
  6378. val |= CPMU_HST_ACC_MACCLK_6_25;
  6379. tw32(TG3_CPMU_HST_ACC, val);
  6380. }
  6381. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
  6382. val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
  6383. val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
  6384. PCIE_PWR_MGMT_L1_THRESH_4MS;
  6385. tw32(PCIE_PWR_MGMT_THRESH, val);
  6386. val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
  6387. tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
  6388. tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
  6389. val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
  6390. tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
  6391. }
  6392. if (tp->tg3_flags3 & TG3_FLG3_L1PLLPD_EN) {
  6393. u32 grc_mode = tr32(GRC_MODE);
  6394. /* Access the lower 1K of PL PCIE block registers. */
  6395. val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
  6396. tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
  6397. val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
  6398. tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
  6399. val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
  6400. tw32(GRC_MODE, grc_mode);
  6401. }
  6402. if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
  6403. u32 grc_mode = tr32(GRC_MODE);
  6404. /* Access the lower 1K of PL PCIE block registers. */
  6405. val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
  6406. tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
  6407. val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5);
  6408. tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
  6409. val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
  6410. tw32(GRC_MODE, grc_mode);
  6411. val = tr32(TG3_CPMU_LSPD_10MB_CLK);
  6412. val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
  6413. val |= CPMU_LSPD_10MB_MACCLK_6_25;
  6414. tw32(TG3_CPMU_LSPD_10MB_CLK, val);
  6415. }
  6416. /* This works around an issue with Athlon chipsets on
  6417. * B3 tigon3 silicon. This bit has no effect on any
  6418. * other revision. But do not set this on PCI Express
  6419. * chips and don't even touch the clocks if the CPMU is present.
  6420. */
  6421. if (!(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT)) {
  6422. if (!(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
  6423. tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
  6424. tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
  6425. }
  6426. if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
  6427. (tp->tg3_flags & TG3_FLAG_PCIX_MODE)) {
  6428. val = tr32(TG3PCI_PCISTATE);
  6429. val |= PCISTATE_RETRY_SAME_DMA;
  6430. tw32(TG3PCI_PCISTATE, val);
  6431. }
  6432. if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
  6433. /* Allow reads and writes to the
  6434. * APE register and memory space.
  6435. */
  6436. val = tr32(TG3PCI_PCISTATE);
  6437. val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
  6438. PCISTATE_ALLOW_APE_SHMEM_WR |
  6439. PCISTATE_ALLOW_APE_PSPACE_WR;
  6440. tw32(TG3PCI_PCISTATE, val);
  6441. }
  6442. if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
  6443. /* Enable some hw fixes. */
  6444. val = tr32(TG3PCI_MSI_DATA);
  6445. val |= (1 << 26) | (1 << 28) | (1 << 29);
  6446. tw32(TG3PCI_MSI_DATA, val);
  6447. }
  6448. /* Descriptor ring init may make accesses to the
  6449. * NIC SRAM area to setup the TX descriptors, so we
  6450. * can only do this after the hardware has been
  6451. * successfully reset.
  6452. */
  6453. err = tg3_init_rings(tp);
  6454. if (err)
  6455. return err;
  6456. if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS) {
  6457. val = tr32(TG3PCI_DMA_RW_CTRL) &
  6458. ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
  6459. if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0)
  6460. val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
  6461. tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
  6462. } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
  6463. GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761) {
  6464. /* This value is determined during the probe time DMA
  6465. * engine test, tg3_test_dma.
  6466. */
  6467. tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
  6468. }
  6469. tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
  6470. GRC_MODE_4X_NIC_SEND_RINGS |
  6471. GRC_MODE_NO_TX_PHDR_CSUM |
  6472. GRC_MODE_NO_RX_PHDR_CSUM);
  6473. tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
  6474. /* Pseudo-header checksum is done by hardware logic and not
  6475. * the offload processers, so make the chip do the pseudo-
  6476. * header checksums on receive. For transmit it is more
  6477. * convenient to do the pseudo-header checksum in software
  6478. * as Linux does that on transmit for us in all cases.
  6479. */
  6480. tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
  6481. tw32(GRC_MODE,
  6482. tp->grc_mode |
  6483. (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
  6484. /* Setup the timer prescalar register. Clock is always 66Mhz. */
  6485. val = tr32(GRC_MISC_CFG);
  6486. val &= ~0xff;
  6487. val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
  6488. tw32(GRC_MISC_CFG, val);
  6489. /* Initialize MBUF/DESC pool. */
  6490. if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
  6491. /* Do nothing. */
  6492. } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
  6493. tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
  6494. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
  6495. tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
  6496. else
  6497. tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
  6498. tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
  6499. tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
  6500. } else if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) {
  6501. int fw_len;
  6502. fw_len = tp->fw_len;
  6503. fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
  6504. tw32(BUFMGR_MB_POOL_ADDR,
  6505. NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
  6506. tw32(BUFMGR_MB_POOL_SIZE,
  6507. NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
  6508. }
  6509. if (tp->dev->mtu <= ETH_DATA_LEN) {
  6510. tw32(BUFMGR_MB_RDMA_LOW_WATER,
  6511. tp->bufmgr_config.mbuf_read_dma_low_water);
  6512. tw32(BUFMGR_MB_MACRX_LOW_WATER,
  6513. tp->bufmgr_config.mbuf_mac_rx_low_water);
  6514. tw32(BUFMGR_MB_HIGH_WATER,
  6515. tp->bufmgr_config.mbuf_high_water);
  6516. } else {
  6517. tw32(BUFMGR_MB_RDMA_LOW_WATER,
  6518. tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
  6519. tw32(BUFMGR_MB_MACRX_LOW_WATER,
  6520. tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
  6521. tw32(BUFMGR_MB_HIGH_WATER,
  6522. tp->bufmgr_config.mbuf_high_water_jumbo);
  6523. }
  6524. tw32(BUFMGR_DMA_LOW_WATER,
  6525. tp->bufmgr_config.dma_low_water);
  6526. tw32(BUFMGR_DMA_HIGH_WATER,
  6527. tp->bufmgr_config.dma_high_water);
  6528. val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
  6529. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
  6530. val |= BUFMGR_MODE_NO_TX_UNDERRUN;
  6531. tw32(BUFMGR_MODE, val);
  6532. for (i = 0; i < 2000; i++) {
  6533. if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
  6534. break;
  6535. udelay(10);
  6536. }
  6537. if (i >= 2000) {
  6538. netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
  6539. return -ENODEV;
  6540. }
  6541. /* Setup replenish threshold. */
  6542. val = tp->rx_pending / 8;
  6543. if (val == 0)
  6544. val = 1;
  6545. else if (val > tp->rx_std_max_post)
  6546. val = tp->rx_std_max_post;
  6547. else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
  6548. if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
  6549. tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
  6550. if (val > (TG3_RX_INTERNAL_RING_SZ_5906 / 2))
  6551. val = TG3_RX_INTERNAL_RING_SZ_5906 / 2;
  6552. }
  6553. tw32(RCVBDI_STD_THRESH, val);
  6554. /* Initialize TG3_BDINFO's at:
  6555. * RCVDBDI_STD_BD: standard eth size rx ring
  6556. * RCVDBDI_JUMBO_BD: jumbo frame rx ring
  6557. * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
  6558. *
  6559. * like so:
  6560. * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
  6561. * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
  6562. * ring attribute flags
  6563. * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
  6564. *
  6565. * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
  6566. * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
  6567. *
  6568. * The size of each ring is fixed in the firmware, but the location is
  6569. * configurable.
  6570. */
  6571. tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
  6572. ((u64) tpr->rx_std_mapping >> 32));
  6573. tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
  6574. ((u64) tpr->rx_std_mapping & 0xffffffff));
  6575. if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
  6576. GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5719)
  6577. tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
  6578. NIC_SRAM_RX_BUFFER_DESC);
  6579. /* Disable the mini ring */
  6580. if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
  6581. tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
  6582. BDINFO_FLAGS_DISABLED);
  6583. /* Program the jumbo buffer descriptor ring control
  6584. * blocks on those devices that have them.
  6585. */
  6586. if ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) &&
  6587. !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
  6588. /* Setup replenish threshold. */
  6589. tw32(RCVBDI_JUMBO_THRESH, tp->rx_jumbo_pending / 8);
  6590. if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) {
  6591. tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
  6592. ((u64) tpr->rx_jmb_mapping >> 32));
  6593. tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
  6594. ((u64) tpr->rx_jmb_mapping & 0xffffffff));
  6595. tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
  6596. (RX_JUMBO_MAX_SIZE << BDINFO_FLAGS_MAXLEN_SHIFT) |
  6597. BDINFO_FLAGS_USE_EXT_RECV);
  6598. if (!(tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG) ||
  6599. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
  6600. tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
  6601. NIC_SRAM_RX_JUMBO_BUFFER_DESC);
  6602. } else {
  6603. tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
  6604. BDINFO_FLAGS_DISABLED);
  6605. }
  6606. if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS)
  6607. val = (RX_STD_MAX_SIZE_5705 << BDINFO_FLAGS_MAXLEN_SHIFT) |
  6608. (TG3_RX_STD_DMA_SZ << 2);
  6609. else
  6610. val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
  6611. } else
  6612. val = RX_STD_MAX_SIZE_5705 << BDINFO_FLAGS_MAXLEN_SHIFT;
  6613. tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
  6614. tpr->rx_std_prod_idx = tp->rx_pending;
  6615. tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
  6616. tpr->rx_jmb_prod_idx = (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) ?
  6617. tp->rx_jumbo_pending : 0;
  6618. tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
  6619. if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS) {
  6620. tw32(STD_REPLENISH_LWM, 32);
  6621. tw32(JMB_REPLENISH_LWM, 16);
  6622. }
  6623. tg3_rings_reset(tp);
  6624. /* Initialize MAC address and backoff seed. */
  6625. __tg3_set_mac_addr(tp, 0);
  6626. /* MTU + ethernet header + FCS + optional VLAN tag */
  6627. tw32(MAC_RX_MTU_SIZE,
  6628. tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
  6629. /* The slot time is changed by tg3_setup_phy if we
  6630. * run at gigabit with half duplex.
  6631. */
  6632. tw32(MAC_TX_LENGTHS,
  6633. (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
  6634. (6 << TX_LENGTHS_IPG_SHIFT) |
  6635. (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
  6636. /* Receive rules. */
  6637. tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
  6638. tw32(RCVLPC_CONFIG, 0x0181);
  6639. /* Calculate RDMAC_MODE setting early, we need it to determine
  6640. * the RCVLPC_STATE_ENABLE mask.
  6641. */
  6642. rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
  6643. RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
  6644. RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
  6645. RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
  6646. RDMAC_MODE_LNGREAD_ENAB);
  6647. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
  6648. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
  6649. rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
  6650. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
  6651. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
  6652. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
  6653. rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
  6654. RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
  6655. RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
  6656. /* If statement applies to 5705 and 5750 PCI devices only */
  6657. if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
  6658. tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) ||
  6659. (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)) {
  6660. if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE &&
  6661. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
  6662. rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
  6663. } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
  6664. !(tp->tg3_flags2 & TG3_FLG2_IS_5788)) {
  6665. rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
  6666. }
  6667. }
  6668. if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)
  6669. rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
  6670. if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
  6671. rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
  6672. if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) ||
  6673. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
  6674. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
  6675. rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
  6676. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
  6677. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
  6678. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
  6679. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
  6680. (tp->tg3_flags3 & TG3_FLG3_5717_PLUS)) {
  6681. val = tr32(TG3_RDMA_RSRVCTRL_REG);
  6682. tw32(TG3_RDMA_RSRVCTRL_REG,
  6683. val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
  6684. }
  6685. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719) {
  6686. val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
  6687. tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val |
  6688. TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
  6689. TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
  6690. }
  6691. /* Receive/send statistics. */
  6692. if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
  6693. val = tr32(RCVLPC_STATS_ENABLE);
  6694. val &= ~RCVLPC_STATSENAB_DACK_FIX;
  6695. tw32(RCVLPC_STATS_ENABLE, val);
  6696. } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
  6697. (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
  6698. val = tr32(RCVLPC_STATS_ENABLE);
  6699. val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
  6700. tw32(RCVLPC_STATS_ENABLE, val);
  6701. } else {
  6702. tw32(RCVLPC_STATS_ENABLE, 0xffffff);
  6703. }
  6704. tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
  6705. tw32(SNDDATAI_STATSENAB, 0xffffff);
  6706. tw32(SNDDATAI_STATSCTRL,
  6707. (SNDDATAI_SCTRL_ENABLE |
  6708. SNDDATAI_SCTRL_FASTUPD));
  6709. /* Setup host coalescing engine. */
  6710. tw32(HOSTCC_MODE, 0);
  6711. for (i = 0; i < 2000; i++) {
  6712. if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
  6713. break;
  6714. udelay(10);
  6715. }
  6716. __tg3_set_coalesce(tp, &tp->coal);
  6717. if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
  6718. /* Status/statistics block address. See tg3_timer,
  6719. * the tg3_periodic_fetch_stats call there, and
  6720. * tg3_get_stats to see how this works for 5705/5750 chips.
  6721. */
  6722. tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
  6723. ((u64) tp->stats_mapping >> 32));
  6724. tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
  6725. ((u64) tp->stats_mapping & 0xffffffff));
  6726. tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
  6727. tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
  6728. /* Clear statistics and status block memory areas */
  6729. for (i = NIC_SRAM_STATS_BLK;
  6730. i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
  6731. i += sizeof(u32)) {
  6732. tg3_write_mem(tp, i, 0);
  6733. udelay(40);
  6734. }
  6735. }
  6736. tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
  6737. tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
  6738. tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
  6739. if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
  6740. tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
  6741. if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
  6742. tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
  6743. /* reset to prevent losing 1st rx packet intermittently */
  6744. tw32_f(MAC_RX_MODE, RX_MODE_RESET);
  6745. udelay(10);
  6746. }
  6747. if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
  6748. tp->mac_mode &= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
  6749. else
  6750. tp->mac_mode = 0;
  6751. tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
  6752. MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE | MAC_MODE_FHDE_ENABLE;
  6753. if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
  6754. !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
  6755. GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
  6756. tp->mac_mode |= MAC_MODE_LINK_POLARITY;
  6757. tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
  6758. udelay(40);
  6759. /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
  6760. * If TG3_FLG2_IS_NIC is zero, we should read the
  6761. * register to preserve the GPIO settings for LOMs. The GPIOs,
  6762. * whether used as inputs or outputs, are set by boot code after
  6763. * reset.
  6764. */
  6765. if (!(tp->tg3_flags2 & TG3_FLG2_IS_NIC)) {
  6766. u32 gpio_mask;
  6767. gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
  6768. GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
  6769. GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
  6770. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
  6771. gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
  6772. GRC_LCLCTRL_GPIO_OUTPUT3;
  6773. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
  6774. gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
  6775. tp->grc_local_ctrl &= ~gpio_mask;
  6776. tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
  6777. /* GPIO1 must be driven high for eeprom write protect */
  6778. if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT)
  6779. tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
  6780. GRC_LCLCTRL_GPIO_OUTPUT1);
  6781. }
  6782. tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
  6783. udelay(100);
  6784. if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX) {
  6785. val = tr32(MSGINT_MODE);
  6786. val |= MSGINT_MODE_MULTIVEC_EN | MSGINT_MODE_ENABLE;
  6787. tw32(MSGINT_MODE, val);
  6788. }
  6789. if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
  6790. tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
  6791. udelay(40);
  6792. }
  6793. val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
  6794. WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
  6795. WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
  6796. WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
  6797. WDMAC_MODE_LNGREAD_ENAB);
  6798. /* If statement applies to 5705 and 5750 PCI devices only */
  6799. if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
  6800. tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) ||
  6801. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750) {
  6802. if ((tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) &&
  6803. (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
  6804. tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
  6805. /* nothing */
  6806. } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
  6807. !(tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
  6808. !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)) {
  6809. val |= WDMAC_MODE_RX_ACCEL;
  6810. }
  6811. }
  6812. /* Enable host coalescing bug fix */
  6813. if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
  6814. val |= WDMAC_MODE_STATUS_TAG_FIX;
  6815. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
  6816. val |= WDMAC_MODE_BURST_ALL_DATA;
  6817. tw32_f(WDMAC_MODE, val);
  6818. udelay(40);
  6819. if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
  6820. u16 pcix_cmd;
  6821. pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
  6822. &pcix_cmd);
  6823. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
  6824. pcix_cmd &= ~PCI_X_CMD_MAX_READ;
  6825. pcix_cmd |= PCI_X_CMD_READ_2K;
  6826. } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
  6827. pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
  6828. pcix_cmd |= PCI_X_CMD_READ_2K;
  6829. }
  6830. pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
  6831. pcix_cmd);
  6832. }
  6833. tw32_f(RDMAC_MODE, rdmac_mode);
  6834. udelay(40);
  6835. tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
  6836. if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
  6837. tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
  6838. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
  6839. tw32(SNDDATAC_MODE,
  6840. SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
  6841. else
  6842. tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
  6843. tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
  6844. tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
  6845. tw32(RCVDBDI_MODE, RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ);
  6846. tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
  6847. if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
  6848. tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
  6849. val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
  6850. if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
  6851. val |= SNDBDI_MODE_MULTI_TXQ_EN;
  6852. tw32(SNDBDI_MODE, val);
  6853. tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
  6854. if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
  6855. err = tg3_load_5701_a0_firmware_fix(tp);
  6856. if (err)
  6857. return err;
  6858. }
  6859. if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) {
  6860. err = tg3_load_tso_firmware(tp);
  6861. if (err)
  6862. return err;
  6863. }
  6864. tp->tx_mode = TX_MODE_ENABLE;
  6865. if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
  6866. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
  6867. tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
  6868. tw32_f(MAC_TX_MODE, tp->tx_mode);
  6869. udelay(100);
  6870. if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS) {
  6871. u32 reg = MAC_RSS_INDIR_TBL_0;
  6872. u8 *ent = (u8 *)&val;
  6873. /* Setup the indirection table */
  6874. for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
  6875. int idx = i % sizeof(val);
  6876. ent[idx] = i % (tp->irq_cnt - 1);
  6877. if (idx == sizeof(val) - 1) {
  6878. tw32(reg, val);
  6879. reg += 4;
  6880. }
  6881. }
  6882. /* Setup the "secret" hash key. */
  6883. tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
  6884. tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
  6885. tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
  6886. tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
  6887. tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
  6888. tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
  6889. tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
  6890. tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
  6891. tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
  6892. tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
  6893. }
  6894. tp->rx_mode = RX_MODE_ENABLE;
  6895. if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
  6896. tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
  6897. if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)
  6898. tp->rx_mode |= RX_MODE_RSS_ENABLE |
  6899. RX_MODE_RSS_ITBL_HASH_BITS_7 |
  6900. RX_MODE_RSS_IPV6_HASH_EN |
  6901. RX_MODE_RSS_TCP_IPV6_HASH_EN |
  6902. RX_MODE_RSS_IPV4_HASH_EN |
  6903. RX_MODE_RSS_TCP_IPV4_HASH_EN;
  6904. tw32_f(MAC_RX_MODE, tp->rx_mode);
  6905. udelay(10);
  6906. tw32(MAC_LED_CTRL, tp->led_ctrl);
  6907. tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
  6908. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
  6909. tw32_f(MAC_RX_MODE, RX_MODE_RESET);
  6910. udelay(10);
  6911. }
  6912. tw32_f(MAC_RX_MODE, tp->rx_mode);
  6913. udelay(10);
  6914. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
  6915. if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
  6916. !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
  6917. /* Set drive transmission level to 1.2V */
  6918. /* only if the signal pre-emphasis bit is not set */
  6919. val = tr32(MAC_SERDES_CFG);
  6920. val &= 0xfffff000;
  6921. val |= 0x880;
  6922. tw32(MAC_SERDES_CFG, val);
  6923. }
  6924. if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
  6925. tw32(MAC_SERDES_CFG, 0x616000);
  6926. }
  6927. /* Prevent chip from dropping frames when flow control
  6928. * is enabled.
  6929. */
  6930. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
  6931. val = 1;
  6932. else
  6933. val = 2;
  6934. tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
  6935. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
  6936. (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
  6937. /* Use hardware link auto-negotiation */
  6938. tp->tg3_flags2 |= TG3_FLG2_HW_AUTONEG;
  6939. }
  6940. if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
  6941. (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)) {
  6942. u32 tmp;
  6943. tmp = tr32(SERDES_RX_CTRL);
  6944. tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
  6945. tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
  6946. tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
  6947. tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
  6948. }
  6949. if (!(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)) {
  6950. if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
  6951. tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
  6952. tp->link_config.speed = tp->link_config.orig_speed;
  6953. tp->link_config.duplex = tp->link_config.orig_duplex;
  6954. tp->link_config.autoneg = tp->link_config.orig_autoneg;
  6955. }
  6956. err = tg3_setup_phy(tp, 0);
  6957. if (err)
  6958. return err;
  6959. if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
  6960. !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
  6961. u32 tmp;
  6962. /* Clear CRC stats. */
  6963. if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
  6964. tg3_writephy(tp, MII_TG3_TEST1,
  6965. tmp | MII_TG3_TEST1_CRC_EN);
  6966. tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
  6967. }
  6968. }
  6969. }
  6970. __tg3_set_rx_mode(tp->dev);
  6971. /* Initialize receive rules. */
  6972. tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
  6973. tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
  6974. tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
  6975. tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
  6976. if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
  6977. !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
  6978. limit = 8;
  6979. else
  6980. limit = 16;
  6981. if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF)
  6982. limit -= 4;
  6983. switch (limit) {
  6984. case 16:
  6985. tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
  6986. case 15:
  6987. tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
  6988. case 14:
  6989. tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
  6990. case 13:
  6991. tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
  6992. case 12:
  6993. tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
  6994. case 11:
  6995. tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
  6996. case 10:
  6997. tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
  6998. case 9:
  6999. tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
  7000. case 8:
  7001. tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
  7002. case 7:
  7003. tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
  7004. case 6:
  7005. tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
  7006. case 5:
  7007. tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
  7008. case 4:
  7009. /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
  7010. case 3:
  7011. /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
  7012. case 2:
  7013. case 1:
  7014. default:
  7015. break;
  7016. }
  7017. if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
  7018. /* Write our heartbeat update interval to APE. */
  7019. tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
  7020. APE_HOST_HEARTBEAT_INT_DISABLE);
  7021. tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
  7022. return 0;
  7023. }
  7024. /* Called at device open time to get the chip ready for
  7025. * packet processing. Invoked with tp->lock held.
  7026. */
  7027. static int tg3_init_hw(struct tg3 *tp, int reset_phy)
  7028. {
  7029. tg3_switch_clocks(tp);
  7030. tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
  7031. return tg3_reset_hw(tp, reset_phy);
  7032. }
  7033. #define TG3_STAT_ADD32(PSTAT, REG) \
  7034. do { u32 __val = tr32(REG); \
  7035. (PSTAT)->low += __val; \
  7036. if ((PSTAT)->low < __val) \
  7037. (PSTAT)->high += 1; \
  7038. } while (0)
  7039. static void tg3_periodic_fetch_stats(struct tg3 *tp)
  7040. {
  7041. struct tg3_hw_stats *sp = tp->hw_stats;
  7042. if (!netif_carrier_ok(tp->dev))
  7043. return;
  7044. TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
  7045. TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
  7046. TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
  7047. TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
  7048. TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
  7049. TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
  7050. TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
  7051. TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
  7052. TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
  7053. TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
  7054. TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
  7055. TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
  7056. TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
  7057. TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
  7058. TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
  7059. TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
  7060. TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
  7061. TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
  7062. TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
  7063. TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
  7064. TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
  7065. TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
  7066. TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
  7067. TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
  7068. TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
  7069. TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
  7070. TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
  7071. TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
  7072. TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
  7073. TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
  7074. }
  7075. static void tg3_timer(unsigned long __opaque)
  7076. {
  7077. struct tg3 *tp = (struct tg3 *) __opaque;
  7078. if (tp->irq_sync)
  7079. goto restart_timer;
  7080. spin_lock(&tp->lock);
  7081. if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)) {
  7082. /* All of this garbage is because when using non-tagged
  7083. * IRQ status the mailbox/status_block protocol the chip
  7084. * uses with the cpu is race prone.
  7085. */
  7086. if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
  7087. tw32(GRC_LOCAL_CTRL,
  7088. tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
  7089. } else {
  7090. tw32(HOSTCC_MODE, tp->coalesce_mode |
  7091. HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
  7092. }
  7093. if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
  7094. tp->tg3_flags2 |= TG3_FLG2_RESTART_TIMER;
  7095. spin_unlock(&tp->lock);
  7096. schedule_work(&tp->reset_task);
  7097. return;
  7098. }
  7099. }
  7100. /* This part only runs once per second. */
  7101. if (!--tp->timer_counter) {
  7102. if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
  7103. tg3_periodic_fetch_stats(tp);
  7104. if (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) {
  7105. u32 mac_stat;
  7106. int phy_event;
  7107. mac_stat = tr32(MAC_STATUS);
  7108. phy_event = 0;
  7109. if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
  7110. if (mac_stat & MAC_STATUS_MI_INTERRUPT)
  7111. phy_event = 1;
  7112. } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
  7113. phy_event = 1;
  7114. if (phy_event)
  7115. tg3_setup_phy(tp, 0);
  7116. } else if (tp->tg3_flags & TG3_FLAG_POLL_SERDES) {
  7117. u32 mac_stat = tr32(MAC_STATUS);
  7118. int need_setup = 0;
  7119. if (netif_carrier_ok(tp->dev) &&
  7120. (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
  7121. need_setup = 1;
  7122. }
  7123. if (!netif_carrier_ok(tp->dev) &&
  7124. (mac_stat & (MAC_STATUS_PCS_SYNCED |
  7125. MAC_STATUS_SIGNAL_DET))) {
  7126. need_setup = 1;
  7127. }
  7128. if (need_setup) {
  7129. if (!tp->serdes_counter) {
  7130. tw32_f(MAC_MODE,
  7131. (tp->mac_mode &
  7132. ~MAC_MODE_PORT_MODE_MASK));
  7133. udelay(40);
  7134. tw32_f(MAC_MODE, tp->mac_mode);
  7135. udelay(40);
  7136. }
  7137. tg3_setup_phy(tp, 0);
  7138. }
  7139. } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
  7140. (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
  7141. tg3_serdes_parallel_detect(tp);
  7142. }
  7143. tp->timer_counter = tp->timer_multiplier;
  7144. }
  7145. /* Heartbeat is only sent once every 2 seconds.
  7146. *
  7147. * The heartbeat is to tell the ASF firmware that the host
  7148. * driver is still alive. In the event that the OS crashes,
  7149. * ASF needs to reset the hardware to free up the FIFO space
  7150. * that may be filled with rx packets destined for the host.
  7151. * If the FIFO is full, ASF will no longer function properly.
  7152. *
  7153. * Unintended resets have been reported on real time kernels
  7154. * where the timer doesn't run on time. Netpoll will also have
  7155. * same problem.
  7156. *
  7157. * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
  7158. * to check the ring condition when the heartbeat is expiring
  7159. * before doing the reset. This will prevent most unintended
  7160. * resets.
  7161. */
  7162. if (!--tp->asf_counter) {
  7163. if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
  7164. !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
  7165. tg3_wait_for_event_ack(tp);
  7166. tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
  7167. FWCMD_NICDRV_ALIVE3);
  7168. tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
  7169. tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
  7170. TG3_FW_UPDATE_TIMEOUT_SEC);
  7171. tg3_generate_fw_event(tp);
  7172. }
  7173. tp->asf_counter = tp->asf_multiplier;
  7174. }
  7175. spin_unlock(&tp->lock);
  7176. restart_timer:
  7177. tp->timer.expires = jiffies + tp->timer_offset;
  7178. add_timer(&tp->timer);
  7179. }
  7180. static int tg3_request_irq(struct tg3 *tp, int irq_num)
  7181. {
  7182. irq_handler_t fn;
  7183. unsigned long flags;
  7184. char *name;
  7185. struct tg3_napi *tnapi = &tp->napi[irq_num];
  7186. if (tp->irq_cnt == 1)
  7187. name = tp->dev->name;
  7188. else {
  7189. name = &tnapi->irq_lbl[0];
  7190. snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
  7191. name[IFNAMSIZ-1] = 0;
  7192. }
  7193. if (tp->tg3_flags2 & TG3_FLG2_USING_MSI_OR_MSIX) {
  7194. fn = tg3_msi;
  7195. if (tp->tg3_flags2 & TG3_FLG2_1SHOT_MSI)
  7196. fn = tg3_msi_1shot;
  7197. flags = IRQF_SAMPLE_RANDOM;
  7198. } else {
  7199. fn = tg3_interrupt;
  7200. if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)
  7201. fn = tg3_interrupt_tagged;
  7202. flags = IRQF_SHARED | IRQF_SAMPLE_RANDOM;
  7203. }
  7204. return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
  7205. }
  7206. static int tg3_test_interrupt(struct tg3 *tp)
  7207. {
  7208. struct tg3_napi *tnapi = &tp->napi[0];
  7209. struct net_device *dev = tp->dev;
  7210. int err, i, intr_ok = 0;
  7211. u32 val;
  7212. if (!netif_running(dev))
  7213. return -ENODEV;
  7214. tg3_disable_ints(tp);
  7215. free_irq(tnapi->irq_vec, tnapi);
  7216. /*
  7217. * Turn off MSI one shot mode. Otherwise this test has no
  7218. * observable way to know whether the interrupt was delivered.
  7219. */
  7220. if ((tp->tg3_flags3 & TG3_FLG3_5717_PLUS) &&
  7221. (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
  7222. val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
  7223. tw32(MSGINT_MODE, val);
  7224. }
  7225. err = request_irq(tnapi->irq_vec, tg3_test_isr,
  7226. IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, tnapi);
  7227. if (err)
  7228. return err;
  7229. tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
  7230. tg3_enable_ints(tp);
  7231. tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
  7232. tnapi->coal_now);
  7233. for (i = 0; i < 5; i++) {
  7234. u32 int_mbox, misc_host_ctrl;
  7235. int_mbox = tr32_mailbox(tnapi->int_mbox);
  7236. misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
  7237. if ((int_mbox != 0) ||
  7238. (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
  7239. intr_ok = 1;
  7240. break;
  7241. }
  7242. msleep(10);
  7243. }
  7244. tg3_disable_ints(tp);
  7245. free_irq(tnapi->irq_vec, tnapi);
  7246. err = tg3_request_irq(tp, 0);
  7247. if (err)
  7248. return err;
  7249. if (intr_ok) {
  7250. /* Reenable MSI one shot mode. */
  7251. if ((tp->tg3_flags3 & TG3_FLG3_5717_PLUS) &&
  7252. (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
  7253. val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
  7254. tw32(MSGINT_MODE, val);
  7255. }
  7256. return 0;
  7257. }
  7258. return -EIO;
  7259. }
  7260. /* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
  7261. * successfully restored
  7262. */
  7263. static int tg3_test_msi(struct tg3 *tp)
  7264. {
  7265. int err;
  7266. u16 pci_cmd;
  7267. if (!(tp->tg3_flags2 & TG3_FLG2_USING_MSI))
  7268. return 0;
  7269. /* Turn off SERR reporting in case MSI terminates with Master
  7270. * Abort.
  7271. */
  7272. pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
  7273. pci_write_config_word(tp->pdev, PCI_COMMAND,
  7274. pci_cmd & ~PCI_COMMAND_SERR);
  7275. err = tg3_test_interrupt(tp);
  7276. pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
  7277. if (!err)
  7278. return 0;
  7279. /* other failures */
  7280. if (err != -EIO)
  7281. return err;
  7282. /* MSI test failed, go back to INTx mode */
  7283. netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
  7284. "to INTx mode. Please report this failure to the PCI "
  7285. "maintainer and include system chipset information\n");
  7286. free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
  7287. pci_disable_msi(tp->pdev);
  7288. tp->tg3_flags2 &= ~TG3_FLG2_USING_MSI;
  7289. tp->napi[0].irq_vec = tp->pdev->irq;
  7290. err = tg3_request_irq(tp, 0);
  7291. if (err)
  7292. return err;
  7293. /* Need to reset the chip because the MSI cycle may have terminated
  7294. * with Master Abort.
  7295. */
  7296. tg3_full_lock(tp, 1);
  7297. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  7298. err = tg3_init_hw(tp, 1);
  7299. tg3_full_unlock(tp);
  7300. if (err)
  7301. free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
  7302. return err;
  7303. }
  7304. static int tg3_request_firmware(struct tg3 *tp)
  7305. {
  7306. const __be32 *fw_data;
  7307. if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
  7308. netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
  7309. tp->fw_needed);
  7310. return -ENOENT;
  7311. }
  7312. fw_data = (void *)tp->fw->data;
  7313. /* Firmware blob starts with version numbers, followed by
  7314. * start address and _full_ length including BSS sections
  7315. * (which must be longer than the actual data, of course
  7316. */
  7317. tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
  7318. if (tp->fw_len < (tp->fw->size - 12)) {
  7319. netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
  7320. tp->fw_len, tp->fw_needed);
  7321. release_firmware(tp->fw);
  7322. tp->fw = NULL;
  7323. return -EINVAL;
  7324. }
  7325. /* We no longer need firmware; we have it. */
  7326. tp->fw_needed = NULL;
  7327. return 0;
  7328. }
  7329. static bool tg3_enable_msix(struct tg3 *tp)
  7330. {
  7331. int i, rc, cpus = num_online_cpus();
  7332. struct msix_entry msix_ent[tp->irq_max];
  7333. if (cpus == 1)
  7334. /* Just fallback to the simpler MSI mode. */
  7335. return false;
  7336. /*
  7337. * We want as many rx rings enabled as there are cpus.
  7338. * The first MSIX vector only deals with link interrupts, etc,
  7339. * so we add one to the number of vectors we are requesting.
  7340. */
  7341. tp->irq_cnt = min_t(unsigned, cpus + 1, tp->irq_max);
  7342. for (i = 0; i < tp->irq_max; i++) {
  7343. msix_ent[i].entry = i;
  7344. msix_ent[i].vector = 0;
  7345. }
  7346. rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
  7347. if (rc < 0) {
  7348. return false;
  7349. } else if (rc != 0) {
  7350. if (pci_enable_msix(tp->pdev, msix_ent, rc))
  7351. return false;
  7352. netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
  7353. tp->irq_cnt, rc);
  7354. tp->irq_cnt = rc;
  7355. }
  7356. for (i = 0; i < tp->irq_max; i++)
  7357. tp->napi[i].irq_vec = msix_ent[i].vector;
  7358. netif_set_real_num_tx_queues(tp->dev, 1);
  7359. rc = tp->irq_cnt > 1 ? tp->irq_cnt - 1 : 1;
  7360. if (netif_set_real_num_rx_queues(tp->dev, rc)) {
  7361. pci_disable_msix(tp->pdev);
  7362. return false;
  7363. }
  7364. if (tp->irq_cnt > 1)
  7365. tp->tg3_flags3 |= TG3_FLG3_ENABLE_RSS;
  7366. return true;
  7367. }
  7368. static void tg3_ints_init(struct tg3 *tp)
  7369. {
  7370. if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSI_OR_MSIX) &&
  7371. !(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)) {
  7372. /* All MSI supporting chips should support tagged
  7373. * status. Assert that this is the case.
  7374. */
  7375. netdev_warn(tp->dev,
  7376. "MSI without TAGGED_STATUS? Not using MSI\n");
  7377. goto defcfg;
  7378. }
  7379. if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX) && tg3_enable_msix(tp))
  7380. tp->tg3_flags2 |= TG3_FLG2_USING_MSIX;
  7381. else if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSI) &&
  7382. pci_enable_msi(tp->pdev) == 0)
  7383. tp->tg3_flags2 |= TG3_FLG2_USING_MSI;
  7384. if (tp->tg3_flags2 & TG3_FLG2_USING_MSI_OR_MSIX) {
  7385. u32 msi_mode = tr32(MSGINT_MODE);
  7386. if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX)
  7387. msi_mode |= MSGINT_MODE_MULTIVEC_EN;
  7388. tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
  7389. }
  7390. defcfg:
  7391. if (!(tp->tg3_flags2 & TG3_FLG2_USING_MSIX)) {
  7392. tp->irq_cnt = 1;
  7393. tp->napi[0].irq_vec = tp->pdev->irq;
  7394. netif_set_real_num_tx_queues(tp->dev, 1);
  7395. }
  7396. }
  7397. static void tg3_ints_fini(struct tg3 *tp)
  7398. {
  7399. if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX)
  7400. pci_disable_msix(tp->pdev);
  7401. else if (tp->tg3_flags2 & TG3_FLG2_USING_MSI)
  7402. pci_disable_msi(tp->pdev);
  7403. tp->tg3_flags2 &= ~TG3_FLG2_USING_MSI_OR_MSIX;
  7404. tp->tg3_flags3 &= ~(TG3_FLG3_ENABLE_RSS | TG3_FLG3_ENABLE_TSS);
  7405. }
  7406. static int tg3_open(struct net_device *dev)
  7407. {
  7408. struct tg3 *tp = netdev_priv(dev);
  7409. int i, err;
  7410. if (tp->fw_needed) {
  7411. err = tg3_request_firmware(tp);
  7412. if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
  7413. if (err)
  7414. return err;
  7415. } else if (err) {
  7416. netdev_warn(tp->dev, "TSO capability disabled\n");
  7417. tp->tg3_flags2 &= ~TG3_FLG2_TSO_CAPABLE;
  7418. } else if (!(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
  7419. netdev_notice(tp->dev, "TSO capability restored\n");
  7420. tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
  7421. }
  7422. }
  7423. netif_carrier_off(tp->dev);
  7424. err = tg3_set_power_state(tp, PCI_D0);
  7425. if (err)
  7426. return err;
  7427. tg3_full_lock(tp, 0);
  7428. tg3_disable_ints(tp);
  7429. tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
  7430. tg3_full_unlock(tp);
  7431. /*
  7432. * Setup interrupts first so we know how
  7433. * many NAPI resources to allocate
  7434. */
  7435. tg3_ints_init(tp);
  7436. /* The placement of this call is tied
  7437. * to the setup and use of Host TX descriptors.
  7438. */
  7439. err = tg3_alloc_consistent(tp);
  7440. if (err)
  7441. goto err_out1;
  7442. tg3_napi_init(tp);
  7443. tg3_napi_enable(tp);
  7444. for (i = 0; i < tp->irq_cnt; i++) {
  7445. struct tg3_napi *tnapi = &tp->napi[i];
  7446. err = tg3_request_irq(tp, i);
  7447. if (err) {
  7448. for (i--; i >= 0; i--)
  7449. free_irq(tnapi->irq_vec, tnapi);
  7450. break;
  7451. }
  7452. }
  7453. if (err)
  7454. goto err_out2;
  7455. tg3_full_lock(tp, 0);
  7456. err = tg3_init_hw(tp, 1);
  7457. if (err) {
  7458. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  7459. tg3_free_rings(tp);
  7460. } else {
  7461. if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)
  7462. tp->timer_offset = HZ;
  7463. else
  7464. tp->timer_offset = HZ / 10;
  7465. BUG_ON(tp->timer_offset > HZ);
  7466. tp->timer_counter = tp->timer_multiplier =
  7467. (HZ / tp->timer_offset);
  7468. tp->asf_counter = tp->asf_multiplier =
  7469. ((HZ / tp->timer_offset) * 2);
  7470. init_timer(&tp->timer);
  7471. tp->timer.expires = jiffies + tp->timer_offset;
  7472. tp->timer.data = (unsigned long) tp;
  7473. tp->timer.function = tg3_timer;
  7474. }
  7475. tg3_full_unlock(tp);
  7476. if (err)
  7477. goto err_out3;
  7478. if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
  7479. err = tg3_test_msi(tp);
  7480. if (err) {
  7481. tg3_full_lock(tp, 0);
  7482. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  7483. tg3_free_rings(tp);
  7484. tg3_full_unlock(tp);
  7485. goto err_out2;
  7486. }
  7487. if (!(tp->tg3_flags3 & TG3_FLG3_5717_PLUS) &&
  7488. (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
  7489. u32 val = tr32(PCIE_TRANSACTION_CFG);
  7490. tw32(PCIE_TRANSACTION_CFG,
  7491. val | PCIE_TRANS_CFG_1SHOT_MSI);
  7492. }
  7493. }
  7494. tg3_phy_start(tp);
  7495. tg3_full_lock(tp, 0);
  7496. add_timer(&tp->timer);
  7497. tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
  7498. tg3_enable_ints(tp);
  7499. tg3_full_unlock(tp);
  7500. netif_tx_start_all_queues(dev);
  7501. return 0;
  7502. err_out3:
  7503. for (i = tp->irq_cnt - 1; i >= 0; i--) {
  7504. struct tg3_napi *tnapi = &tp->napi[i];
  7505. free_irq(tnapi->irq_vec, tnapi);
  7506. }
  7507. err_out2:
  7508. tg3_napi_disable(tp);
  7509. tg3_napi_fini(tp);
  7510. tg3_free_consistent(tp);
  7511. err_out1:
  7512. tg3_ints_fini(tp);
  7513. return err;
  7514. }
  7515. static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *,
  7516. struct rtnl_link_stats64 *);
  7517. static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *);
  7518. static int tg3_close(struct net_device *dev)
  7519. {
  7520. int i;
  7521. struct tg3 *tp = netdev_priv(dev);
  7522. tg3_napi_disable(tp);
  7523. cancel_work_sync(&tp->reset_task);
  7524. netif_tx_stop_all_queues(dev);
  7525. del_timer_sync(&tp->timer);
  7526. tg3_phy_stop(tp);
  7527. tg3_full_lock(tp, 1);
  7528. tg3_disable_ints(tp);
  7529. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  7530. tg3_free_rings(tp);
  7531. tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
  7532. tg3_full_unlock(tp);
  7533. for (i = tp->irq_cnt - 1; i >= 0; i--) {
  7534. struct tg3_napi *tnapi = &tp->napi[i];
  7535. free_irq(tnapi->irq_vec, tnapi);
  7536. }
  7537. tg3_ints_fini(tp);
  7538. tg3_get_stats64(tp->dev, &tp->net_stats_prev);
  7539. memcpy(&tp->estats_prev, tg3_get_estats(tp),
  7540. sizeof(tp->estats_prev));
  7541. tg3_napi_fini(tp);
  7542. tg3_free_consistent(tp);
  7543. tg3_set_power_state(tp, PCI_D3hot);
  7544. netif_carrier_off(tp->dev);
  7545. return 0;
  7546. }
  7547. static inline u64 get_stat64(tg3_stat64_t *val)
  7548. {
  7549. return ((u64)val->high << 32) | ((u64)val->low);
  7550. }
  7551. static u64 calc_crc_errors(struct tg3 *tp)
  7552. {
  7553. struct tg3_hw_stats *hw_stats = tp->hw_stats;
  7554. if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
  7555. (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
  7556. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
  7557. u32 val;
  7558. spin_lock_bh(&tp->lock);
  7559. if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
  7560. tg3_writephy(tp, MII_TG3_TEST1,
  7561. val | MII_TG3_TEST1_CRC_EN);
  7562. tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
  7563. } else
  7564. val = 0;
  7565. spin_unlock_bh(&tp->lock);
  7566. tp->phy_crc_errors += val;
  7567. return tp->phy_crc_errors;
  7568. }
  7569. return get_stat64(&hw_stats->rx_fcs_errors);
  7570. }
  7571. #define ESTAT_ADD(member) \
  7572. estats->member = old_estats->member + \
  7573. get_stat64(&hw_stats->member)
  7574. static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *tp)
  7575. {
  7576. struct tg3_ethtool_stats *estats = &tp->estats;
  7577. struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
  7578. struct tg3_hw_stats *hw_stats = tp->hw_stats;
  7579. if (!hw_stats)
  7580. return old_estats;
  7581. ESTAT_ADD(rx_octets);
  7582. ESTAT_ADD(rx_fragments);
  7583. ESTAT_ADD(rx_ucast_packets);
  7584. ESTAT_ADD(rx_mcast_packets);
  7585. ESTAT_ADD(rx_bcast_packets);
  7586. ESTAT_ADD(rx_fcs_errors);
  7587. ESTAT_ADD(rx_align_errors);
  7588. ESTAT_ADD(rx_xon_pause_rcvd);
  7589. ESTAT_ADD(rx_xoff_pause_rcvd);
  7590. ESTAT_ADD(rx_mac_ctrl_rcvd);
  7591. ESTAT_ADD(rx_xoff_entered);
  7592. ESTAT_ADD(rx_frame_too_long_errors);
  7593. ESTAT_ADD(rx_jabbers);
  7594. ESTAT_ADD(rx_undersize_packets);
  7595. ESTAT_ADD(rx_in_length_errors);
  7596. ESTAT_ADD(rx_out_length_errors);
  7597. ESTAT_ADD(rx_64_or_less_octet_packets);
  7598. ESTAT_ADD(rx_65_to_127_octet_packets);
  7599. ESTAT_ADD(rx_128_to_255_octet_packets);
  7600. ESTAT_ADD(rx_256_to_511_octet_packets);
  7601. ESTAT_ADD(rx_512_to_1023_octet_packets);
  7602. ESTAT_ADD(rx_1024_to_1522_octet_packets);
  7603. ESTAT_ADD(rx_1523_to_2047_octet_packets);
  7604. ESTAT_ADD(rx_2048_to_4095_octet_packets);
  7605. ESTAT_ADD(rx_4096_to_8191_octet_packets);
  7606. ESTAT_ADD(rx_8192_to_9022_octet_packets);
  7607. ESTAT_ADD(tx_octets);
  7608. ESTAT_ADD(tx_collisions);
  7609. ESTAT_ADD(tx_xon_sent);
  7610. ESTAT_ADD(tx_xoff_sent);
  7611. ESTAT_ADD(tx_flow_control);
  7612. ESTAT_ADD(tx_mac_errors);
  7613. ESTAT_ADD(tx_single_collisions);
  7614. ESTAT_ADD(tx_mult_collisions);
  7615. ESTAT_ADD(tx_deferred);
  7616. ESTAT_ADD(tx_excessive_collisions);
  7617. ESTAT_ADD(tx_late_collisions);
  7618. ESTAT_ADD(tx_collide_2times);
  7619. ESTAT_ADD(tx_collide_3times);
  7620. ESTAT_ADD(tx_collide_4times);
  7621. ESTAT_ADD(tx_collide_5times);
  7622. ESTAT_ADD(tx_collide_6times);
  7623. ESTAT_ADD(tx_collide_7times);
  7624. ESTAT_ADD(tx_collide_8times);
  7625. ESTAT_ADD(tx_collide_9times);
  7626. ESTAT_ADD(tx_collide_10times);
  7627. ESTAT_ADD(tx_collide_11times);
  7628. ESTAT_ADD(tx_collide_12times);
  7629. ESTAT_ADD(tx_collide_13times);
  7630. ESTAT_ADD(tx_collide_14times);
  7631. ESTAT_ADD(tx_collide_15times);
  7632. ESTAT_ADD(tx_ucast_packets);
  7633. ESTAT_ADD(tx_mcast_packets);
  7634. ESTAT_ADD(tx_bcast_packets);
  7635. ESTAT_ADD(tx_carrier_sense_errors);
  7636. ESTAT_ADD(tx_discards);
  7637. ESTAT_ADD(tx_errors);
  7638. ESTAT_ADD(dma_writeq_full);
  7639. ESTAT_ADD(dma_write_prioq_full);
  7640. ESTAT_ADD(rxbds_empty);
  7641. ESTAT_ADD(rx_discards);
  7642. ESTAT_ADD(rx_errors);
  7643. ESTAT_ADD(rx_threshold_hit);
  7644. ESTAT_ADD(dma_readq_full);
  7645. ESTAT_ADD(dma_read_prioq_full);
  7646. ESTAT_ADD(tx_comp_queue_full);
  7647. ESTAT_ADD(ring_set_send_prod_index);
  7648. ESTAT_ADD(ring_status_update);
  7649. ESTAT_ADD(nic_irqs);
  7650. ESTAT_ADD(nic_avoided_irqs);
  7651. ESTAT_ADD(nic_tx_threshold_hit);
  7652. return estats;
  7653. }
  7654. static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
  7655. struct rtnl_link_stats64 *stats)
  7656. {
  7657. struct tg3 *tp = netdev_priv(dev);
  7658. struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
  7659. struct tg3_hw_stats *hw_stats = tp->hw_stats;
  7660. if (!hw_stats)
  7661. return old_stats;
  7662. stats->rx_packets = old_stats->rx_packets +
  7663. get_stat64(&hw_stats->rx_ucast_packets) +
  7664. get_stat64(&hw_stats->rx_mcast_packets) +
  7665. get_stat64(&hw_stats->rx_bcast_packets);
  7666. stats->tx_packets = old_stats->tx_packets +
  7667. get_stat64(&hw_stats->tx_ucast_packets) +
  7668. get_stat64(&hw_stats->tx_mcast_packets) +
  7669. get_stat64(&hw_stats->tx_bcast_packets);
  7670. stats->rx_bytes = old_stats->rx_bytes +
  7671. get_stat64(&hw_stats->rx_octets);
  7672. stats->tx_bytes = old_stats->tx_bytes +
  7673. get_stat64(&hw_stats->tx_octets);
  7674. stats->rx_errors = old_stats->rx_errors +
  7675. get_stat64(&hw_stats->rx_errors);
  7676. stats->tx_errors = old_stats->tx_errors +
  7677. get_stat64(&hw_stats->tx_errors) +
  7678. get_stat64(&hw_stats->tx_mac_errors) +
  7679. get_stat64(&hw_stats->tx_carrier_sense_errors) +
  7680. get_stat64(&hw_stats->tx_discards);
  7681. stats->multicast = old_stats->multicast +
  7682. get_stat64(&hw_stats->rx_mcast_packets);
  7683. stats->collisions = old_stats->collisions +
  7684. get_stat64(&hw_stats->tx_collisions);
  7685. stats->rx_length_errors = old_stats->rx_length_errors +
  7686. get_stat64(&hw_stats->rx_frame_too_long_errors) +
  7687. get_stat64(&hw_stats->rx_undersize_packets);
  7688. stats->rx_over_errors = old_stats->rx_over_errors +
  7689. get_stat64(&hw_stats->rxbds_empty);
  7690. stats->rx_frame_errors = old_stats->rx_frame_errors +
  7691. get_stat64(&hw_stats->rx_align_errors);
  7692. stats->tx_aborted_errors = old_stats->tx_aborted_errors +
  7693. get_stat64(&hw_stats->tx_discards);
  7694. stats->tx_carrier_errors = old_stats->tx_carrier_errors +
  7695. get_stat64(&hw_stats->tx_carrier_sense_errors);
  7696. stats->rx_crc_errors = old_stats->rx_crc_errors +
  7697. calc_crc_errors(tp);
  7698. stats->rx_missed_errors = old_stats->rx_missed_errors +
  7699. get_stat64(&hw_stats->rx_discards);
  7700. return stats;
  7701. }
  7702. static inline u32 calc_crc(unsigned char *buf, int len)
  7703. {
  7704. u32 reg;
  7705. u32 tmp;
  7706. int j, k;
  7707. reg = 0xffffffff;
  7708. for (j = 0; j < len; j++) {
  7709. reg ^= buf[j];
  7710. for (k = 0; k < 8; k++) {
  7711. tmp = reg & 0x01;
  7712. reg >>= 1;
  7713. if (tmp)
  7714. reg ^= 0xedb88320;
  7715. }
  7716. }
  7717. return ~reg;
  7718. }
  7719. static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
  7720. {
  7721. /* accept or reject all multicast frames */
  7722. tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
  7723. tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
  7724. tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
  7725. tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
  7726. }
  7727. static void __tg3_set_rx_mode(struct net_device *dev)
  7728. {
  7729. struct tg3 *tp = netdev_priv(dev);
  7730. u32 rx_mode;
  7731. rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
  7732. RX_MODE_KEEP_VLAN_TAG);
  7733. /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
  7734. * flag clear.
  7735. */
  7736. #if TG3_VLAN_TAG_USED
  7737. if (!tp->vlgrp &&
  7738. !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
  7739. rx_mode |= RX_MODE_KEEP_VLAN_TAG;
  7740. #else
  7741. /* By definition, VLAN is disabled always in this
  7742. * case.
  7743. */
  7744. if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
  7745. rx_mode |= RX_MODE_KEEP_VLAN_TAG;
  7746. #endif
  7747. if (dev->flags & IFF_PROMISC) {
  7748. /* Promiscuous mode. */
  7749. rx_mode |= RX_MODE_PROMISC;
  7750. } else if (dev->flags & IFF_ALLMULTI) {
  7751. /* Accept all multicast. */
  7752. tg3_set_multi(tp, 1);
  7753. } else if (netdev_mc_empty(dev)) {
  7754. /* Reject all multicast. */
  7755. tg3_set_multi(tp, 0);
  7756. } else {
  7757. /* Accept one or more multicast(s). */
  7758. struct netdev_hw_addr *ha;
  7759. u32 mc_filter[4] = { 0, };
  7760. u32 regidx;
  7761. u32 bit;
  7762. u32 crc;
  7763. netdev_for_each_mc_addr(ha, dev) {
  7764. crc = calc_crc(ha->addr, ETH_ALEN);
  7765. bit = ~crc & 0x7f;
  7766. regidx = (bit & 0x60) >> 5;
  7767. bit &= 0x1f;
  7768. mc_filter[regidx] |= (1 << bit);
  7769. }
  7770. tw32(MAC_HASH_REG_0, mc_filter[0]);
  7771. tw32(MAC_HASH_REG_1, mc_filter[1]);
  7772. tw32(MAC_HASH_REG_2, mc_filter[2]);
  7773. tw32(MAC_HASH_REG_3, mc_filter[3]);
  7774. }
  7775. if (rx_mode != tp->rx_mode) {
  7776. tp->rx_mode = rx_mode;
  7777. tw32_f(MAC_RX_MODE, rx_mode);
  7778. udelay(10);
  7779. }
  7780. }
  7781. static void tg3_set_rx_mode(struct net_device *dev)
  7782. {
  7783. struct tg3 *tp = netdev_priv(dev);
  7784. if (!netif_running(dev))
  7785. return;
  7786. tg3_full_lock(tp, 0);
  7787. __tg3_set_rx_mode(dev);
  7788. tg3_full_unlock(tp);
  7789. }
  7790. #define TG3_REGDUMP_LEN (32 * 1024)
  7791. static int tg3_get_regs_len(struct net_device *dev)
  7792. {
  7793. return TG3_REGDUMP_LEN;
  7794. }
  7795. static void tg3_get_regs(struct net_device *dev,
  7796. struct ethtool_regs *regs, void *_p)
  7797. {
  7798. u32 *p = _p;
  7799. struct tg3 *tp = netdev_priv(dev);
  7800. u8 *orig_p = _p;
  7801. int i;
  7802. regs->version = 0;
  7803. memset(p, 0, TG3_REGDUMP_LEN);
  7804. if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
  7805. return;
  7806. tg3_full_lock(tp, 0);
  7807. #define __GET_REG32(reg) (*(p)++ = tr32(reg))
  7808. #define GET_REG32_LOOP(base, len) \
  7809. do { p = (u32 *)(orig_p + (base)); \
  7810. for (i = 0; i < len; i += 4) \
  7811. __GET_REG32((base) + i); \
  7812. } while (0)
  7813. #define GET_REG32_1(reg) \
  7814. do { p = (u32 *)(orig_p + (reg)); \
  7815. __GET_REG32((reg)); \
  7816. } while (0)
  7817. GET_REG32_LOOP(TG3PCI_VENDOR, 0xb0);
  7818. GET_REG32_LOOP(MAILBOX_INTERRUPT_0, 0x200);
  7819. GET_REG32_LOOP(MAC_MODE, 0x4f0);
  7820. GET_REG32_LOOP(SNDDATAI_MODE, 0xe0);
  7821. GET_REG32_1(SNDDATAC_MODE);
  7822. GET_REG32_LOOP(SNDBDS_MODE, 0x80);
  7823. GET_REG32_LOOP(SNDBDI_MODE, 0x48);
  7824. GET_REG32_1(SNDBDC_MODE);
  7825. GET_REG32_LOOP(RCVLPC_MODE, 0x20);
  7826. GET_REG32_LOOP(RCVLPC_SELLST_BASE, 0x15c);
  7827. GET_REG32_LOOP(RCVDBDI_MODE, 0x0c);
  7828. GET_REG32_LOOP(RCVDBDI_JUMBO_BD, 0x3c);
  7829. GET_REG32_LOOP(RCVDBDI_BD_PROD_IDX_0, 0x44);
  7830. GET_REG32_1(RCVDCC_MODE);
  7831. GET_REG32_LOOP(RCVBDI_MODE, 0x20);
  7832. GET_REG32_LOOP(RCVCC_MODE, 0x14);
  7833. GET_REG32_LOOP(RCVLSC_MODE, 0x08);
  7834. GET_REG32_1(MBFREE_MODE);
  7835. GET_REG32_LOOP(HOSTCC_MODE, 0x100);
  7836. GET_REG32_LOOP(MEMARB_MODE, 0x10);
  7837. GET_REG32_LOOP(BUFMGR_MODE, 0x58);
  7838. GET_REG32_LOOP(RDMAC_MODE, 0x08);
  7839. GET_REG32_LOOP(WDMAC_MODE, 0x08);
  7840. GET_REG32_1(RX_CPU_MODE);
  7841. GET_REG32_1(RX_CPU_STATE);
  7842. GET_REG32_1(RX_CPU_PGMCTR);
  7843. GET_REG32_1(RX_CPU_HWBKPT);
  7844. GET_REG32_1(TX_CPU_MODE);
  7845. GET_REG32_1(TX_CPU_STATE);
  7846. GET_REG32_1(TX_CPU_PGMCTR);
  7847. GET_REG32_LOOP(GRCMBOX_INTERRUPT_0, 0x110);
  7848. GET_REG32_LOOP(FTQ_RESET, 0x120);
  7849. GET_REG32_LOOP(MSGINT_MODE, 0x0c);
  7850. GET_REG32_1(DMAC_MODE);
  7851. GET_REG32_LOOP(GRC_MODE, 0x4c);
  7852. if (tp->tg3_flags & TG3_FLAG_NVRAM)
  7853. GET_REG32_LOOP(NVRAM_CMD, 0x24);
  7854. #undef __GET_REG32
  7855. #undef GET_REG32_LOOP
  7856. #undef GET_REG32_1
  7857. tg3_full_unlock(tp);
  7858. }
  7859. static int tg3_get_eeprom_len(struct net_device *dev)
  7860. {
  7861. struct tg3 *tp = netdev_priv(dev);
  7862. return tp->nvram_size;
  7863. }
  7864. static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
  7865. {
  7866. struct tg3 *tp = netdev_priv(dev);
  7867. int ret;
  7868. u8 *pd;
  7869. u32 i, offset, len, b_offset, b_count;
  7870. __be32 val;
  7871. if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM)
  7872. return -EINVAL;
  7873. if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
  7874. return -EAGAIN;
  7875. offset = eeprom->offset;
  7876. len = eeprom->len;
  7877. eeprom->len = 0;
  7878. eeprom->magic = TG3_EEPROM_MAGIC;
  7879. if (offset & 3) {
  7880. /* adjustments to start on required 4 byte boundary */
  7881. b_offset = offset & 3;
  7882. b_count = 4 - b_offset;
  7883. if (b_count > len) {
  7884. /* i.e. offset=1 len=2 */
  7885. b_count = len;
  7886. }
  7887. ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
  7888. if (ret)
  7889. return ret;
  7890. memcpy(data, ((char *)&val) + b_offset, b_count);
  7891. len -= b_count;
  7892. offset += b_count;
  7893. eeprom->len += b_count;
  7894. }
  7895. /* read bytes upto the last 4 byte boundary */
  7896. pd = &data[eeprom->len];
  7897. for (i = 0; i < (len - (len & 3)); i += 4) {
  7898. ret = tg3_nvram_read_be32(tp, offset + i, &val);
  7899. if (ret) {
  7900. eeprom->len += i;
  7901. return ret;
  7902. }
  7903. memcpy(pd + i, &val, 4);
  7904. }
  7905. eeprom->len += i;
  7906. if (len & 3) {
  7907. /* read last bytes not ending on 4 byte boundary */
  7908. pd = &data[eeprom->len];
  7909. b_count = len & 3;
  7910. b_offset = offset + len - b_count;
  7911. ret = tg3_nvram_read_be32(tp, b_offset, &val);
  7912. if (ret)
  7913. return ret;
  7914. memcpy(pd, &val, b_count);
  7915. eeprom->len += b_count;
  7916. }
  7917. return 0;
  7918. }
  7919. static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf);
  7920. static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
  7921. {
  7922. struct tg3 *tp = netdev_priv(dev);
  7923. int ret;
  7924. u32 offset, len, b_offset, odd_len;
  7925. u8 *buf;
  7926. __be32 start, end;
  7927. if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
  7928. return -EAGAIN;
  7929. if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
  7930. eeprom->magic != TG3_EEPROM_MAGIC)
  7931. return -EINVAL;
  7932. offset = eeprom->offset;
  7933. len = eeprom->len;
  7934. if ((b_offset = (offset & 3))) {
  7935. /* adjustments to start on required 4 byte boundary */
  7936. ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
  7937. if (ret)
  7938. return ret;
  7939. len += b_offset;
  7940. offset &= ~3;
  7941. if (len < 4)
  7942. len = 4;
  7943. }
  7944. odd_len = 0;
  7945. if (len & 3) {
  7946. /* adjustments to end on required 4 byte boundary */
  7947. odd_len = 1;
  7948. len = (len + 3) & ~3;
  7949. ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
  7950. if (ret)
  7951. return ret;
  7952. }
  7953. buf = data;
  7954. if (b_offset || odd_len) {
  7955. buf = kmalloc(len, GFP_KERNEL);
  7956. if (!buf)
  7957. return -ENOMEM;
  7958. if (b_offset)
  7959. memcpy(buf, &start, 4);
  7960. if (odd_len)
  7961. memcpy(buf+len-4, &end, 4);
  7962. memcpy(buf + b_offset, data, eeprom->len);
  7963. }
  7964. ret = tg3_nvram_write_block(tp, offset, len, buf);
  7965. if (buf != data)
  7966. kfree(buf);
  7967. return ret;
  7968. }
  7969. static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  7970. {
  7971. struct tg3 *tp = netdev_priv(dev);
  7972. if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
  7973. struct phy_device *phydev;
  7974. if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
  7975. return -EAGAIN;
  7976. phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
  7977. return phy_ethtool_gset(phydev, cmd);
  7978. }
  7979. cmd->supported = (SUPPORTED_Autoneg);
  7980. if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
  7981. cmd->supported |= (SUPPORTED_1000baseT_Half |
  7982. SUPPORTED_1000baseT_Full);
  7983. if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
  7984. cmd->supported |= (SUPPORTED_100baseT_Half |
  7985. SUPPORTED_100baseT_Full |
  7986. SUPPORTED_10baseT_Half |
  7987. SUPPORTED_10baseT_Full |
  7988. SUPPORTED_TP);
  7989. cmd->port = PORT_TP;
  7990. } else {
  7991. cmd->supported |= SUPPORTED_FIBRE;
  7992. cmd->port = PORT_FIBRE;
  7993. }
  7994. cmd->advertising = tp->link_config.advertising;
  7995. if (netif_running(dev)) {
  7996. cmd->speed = tp->link_config.active_speed;
  7997. cmd->duplex = tp->link_config.active_duplex;
  7998. }
  7999. cmd->phy_address = tp->phy_addr;
  8000. cmd->transceiver = XCVR_INTERNAL;
  8001. cmd->autoneg = tp->link_config.autoneg;
  8002. cmd->maxtxpkt = 0;
  8003. cmd->maxrxpkt = 0;
  8004. return 0;
  8005. }
  8006. static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  8007. {
  8008. struct tg3 *tp = netdev_priv(dev);
  8009. if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
  8010. struct phy_device *phydev;
  8011. if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
  8012. return -EAGAIN;
  8013. phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
  8014. return phy_ethtool_sset(phydev, cmd);
  8015. }
  8016. if (cmd->autoneg != AUTONEG_ENABLE &&
  8017. cmd->autoneg != AUTONEG_DISABLE)
  8018. return -EINVAL;
  8019. if (cmd->autoneg == AUTONEG_DISABLE &&
  8020. cmd->duplex != DUPLEX_FULL &&
  8021. cmd->duplex != DUPLEX_HALF)
  8022. return -EINVAL;
  8023. if (cmd->autoneg == AUTONEG_ENABLE) {
  8024. u32 mask = ADVERTISED_Autoneg |
  8025. ADVERTISED_Pause |
  8026. ADVERTISED_Asym_Pause;
  8027. if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
  8028. mask |= ADVERTISED_1000baseT_Half |
  8029. ADVERTISED_1000baseT_Full;
  8030. if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
  8031. mask |= ADVERTISED_100baseT_Half |
  8032. ADVERTISED_100baseT_Full |
  8033. ADVERTISED_10baseT_Half |
  8034. ADVERTISED_10baseT_Full |
  8035. ADVERTISED_TP;
  8036. else
  8037. mask |= ADVERTISED_FIBRE;
  8038. if (cmd->advertising & ~mask)
  8039. return -EINVAL;
  8040. mask &= (ADVERTISED_1000baseT_Half |
  8041. ADVERTISED_1000baseT_Full |
  8042. ADVERTISED_100baseT_Half |
  8043. ADVERTISED_100baseT_Full |
  8044. ADVERTISED_10baseT_Half |
  8045. ADVERTISED_10baseT_Full);
  8046. cmd->advertising &= mask;
  8047. } else {
  8048. if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
  8049. if (cmd->speed != SPEED_1000)
  8050. return -EINVAL;
  8051. if (cmd->duplex != DUPLEX_FULL)
  8052. return -EINVAL;
  8053. } else {
  8054. if (cmd->speed != SPEED_100 &&
  8055. cmd->speed != SPEED_10)
  8056. return -EINVAL;
  8057. }
  8058. }
  8059. tg3_full_lock(tp, 0);
  8060. tp->link_config.autoneg = cmd->autoneg;
  8061. if (cmd->autoneg == AUTONEG_ENABLE) {
  8062. tp->link_config.advertising = (cmd->advertising |
  8063. ADVERTISED_Autoneg);
  8064. tp->link_config.speed = SPEED_INVALID;
  8065. tp->link_config.duplex = DUPLEX_INVALID;
  8066. } else {
  8067. tp->link_config.advertising = 0;
  8068. tp->link_config.speed = cmd->speed;
  8069. tp->link_config.duplex = cmd->duplex;
  8070. }
  8071. tp->link_config.orig_speed = tp->link_config.speed;
  8072. tp->link_config.orig_duplex = tp->link_config.duplex;
  8073. tp->link_config.orig_autoneg = tp->link_config.autoneg;
  8074. if (netif_running(dev))
  8075. tg3_setup_phy(tp, 1);
  8076. tg3_full_unlock(tp);
  8077. return 0;
  8078. }
  8079. static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  8080. {
  8081. struct tg3 *tp = netdev_priv(dev);
  8082. strcpy(info->driver, DRV_MODULE_NAME);
  8083. strcpy(info->version, DRV_MODULE_VERSION);
  8084. strcpy(info->fw_version, tp->fw_ver);
  8085. strcpy(info->bus_info, pci_name(tp->pdev));
  8086. }
  8087. static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  8088. {
  8089. struct tg3 *tp = netdev_priv(dev);
  8090. if ((tp->tg3_flags & TG3_FLAG_WOL_CAP) &&
  8091. device_can_wakeup(&tp->pdev->dev))
  8092. wol->supported = WAKE_MAGIC;
  8093. else
  8094. wol->supported = 0;
  8095. wol->wolopts = 0;
  8096. if ((tp->tg3_flags & TG3_FLAG_WOL_ENABLE) &&
  8097. device_can_wakeup(&tp->pdev->dev))
  8098. wol->wolopts = WAKE_MAGIC;
  8099. memset(&wol->sopass, 0, sizeof(wol->sopass));
  8100. }
  8101. static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  8102. {
  8103. struct tg3 *tp = netdev_priv(dev);
  8104. struct device *dp = &tp->pdev->dev;
  8105. if (wol->wolopts & ~WAKE_MAGIC)
  8106. return -EINVAL;
  8107. if ((wol->wolopts & WAKE_MAGIC) &&
  8108. !((tp->tg3_flags & TG3_FLAG_WOL_CAP) && device_can_wakeup(dp)))
  8109. return -EINVAL;
  8110. spin_lock_bh(&tp->lock);
  8111. if (wol->wolopts & WAKE_MAGIC) {
  8112. tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
  8113. device_set_wakeup_enable(dp, true);
  8114. } else {
  8115. tp->tg3_flags &= ~TG3_FLAG_WOL_ENABLE;
  8116. device_set_wakeup_enable(dp, false);
  8117. }
  8118. spin_unlock_bh(&tp->lock);
  8119. return 0;
  8120. }
  8121. static u32 tg3_get_msglevel(struct net_device *dev)
  8122. {
  8123. struct tg3 *tp = netdev_priv(dev);
  8124. return tp->msg_enable;
  8125. }
  8126. static void tg3_set_msglevel(struct net_device *dev, u32 value)
  8127. {
  8128. struct tg3 *tp = netdev_priv(dev);
  8129. tp->msg_enable = value;
  8130. }
  8131. static int tg3_set_tso(struct net_device *dev, u32 value)
  8132. {
  8133. struct tg3 *tp = netdev_priv(dev);
  8134. if (!(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
  8135. if (value)
  8136. return -EINVAL;
  8137. return 0;
  8138. }
  8139. if ((dev->features & NETIF_F_IPV6_CSUM) &&
  8140. ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_2) ||
  8141. (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3))) {
  8142. if (value) {
  8143. dev->features |= NETIF_F_TSO6;
  8144. if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) ||
  8145. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
  8146. (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
  8147. GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
  8148. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
  8149. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
  8150. dev->features |= NETIF_F_TSO_ECN;
  8151. } else
  8152. dev->features &= ~(NETIF_F_TSO6 | NETIF_F_TSO_ECN);
  8153. }
  8154. return ethtool_op_set_tso(dev, value);
  8155. }
  8156. static int tg3_nway_reset(struct net_device *dev)
  8157. {
  8158. struct tg3 *tp = netdev_priv(dev);
  8159. int r;
  8160. if (!netif_running(dev))
  8161. return -EAGAIN;
  8162. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
  8163. return -EINVAL;
  8164. if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
  8165. if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
  8166. return -EAGAIN;
  8167. r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
  8168. } else {
  8169. u32 bmcr;
  8170. spin_lock_bh(&tp->lock);
  8171. r = -EINVAL;
  8172. tg3_readphy(tp, MII_BMCR, &bmcr);
  8173. if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
  8174. ((bmcr & BMCR_ANENABLE) ||
  8175. (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
  8176. tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
  8177. BMCR_ANENABLE);
  8178. r = 0;
  8179. }
  8180. spin_unlock_bh(&tp->lock);
  8181. }
  8182. return r;
  8183. }
  8184. static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
  8185. {
  8186. struct tg3 *tp = netdev_priv(dev);
  8187. ering->rx_max_pending = TG3_RX_RING_SIZE - 1;
  8188. ering->rx_mini_max_pending = 0;
  8189. if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE)
  8190. ering->rx_jumbo_max_pending = TG3_RX_JUMBO_RING_SIZE - 1;
  8191. else
  8192. ering->rx_jumbo_max_pending = 0;
  8193. ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
  8194. ering->rx_pending = tp->rx_pending;
  8195. ering->rx_mini_pending = 0;
  8196. if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE)
  8197. ering->rx_jumbo_pending = tp->rx_jumbo_pending;
  8198. else
  8199. ering->rx_jumbo_pending = 0;
  8200. ering->tx_pending = tp->napi[0].tx_pending;
  8201. }
  8202. static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
  8203. {
  8204. struct tg3 *tp = netdev_priv(dev);
  8205. int i, irq_sync = 0, err = 0;
  8206. if ((ering->rx_pending > TG3_RX_RING_SIZE - 1) ||
  8207. (ering->rx_jumbo_pending > TG3_RX_JUMBO_RING_SIZE - 1) ||
  8208. (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
  8209. (ering->tx_pending <= MAX_SKB_FRAGS) ||
  8210. ((tp->tg3_flags2 & TG3_FLG2_TSO_BUG) &&
  8211. (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
  8212. return -EINVAL;
  8213. if (netif_running(dev)) {
  8214. tg3_phy_stop(tp);
  8215. tg3_netif_stop(tp);
  8216. irq_sync = 1;
  8217. }
  8218. tg3_full_lock(tp, irq_sync);
  8219. tp->rx_pending = ering->rx_pending;
  8220. if ((tp->tg3_flags2 & TG3_FLG2_MAX_RXPEND_64) &&
  8221. tp->rx_pending > 63)
  8222. tp->rx_pending = 63;
  8223. tp->rx_jumbo_pending = ering->rx_jumbo_pending;
  8224. for (i = 0; i < tp->irq_max; i++)
  8225. tp->napi[i].tx_pending = ering->tx_pending;
  8226. if (netif_running(dev)) {
  8227. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  8228. err = tg3_restart_hw(tp, 1);
  8229. if (!err)
  8230. tg3_netif_start(tp);
  8231. }
  8232. tg3_full_unlock(tp);
  8233. if (irq_sync && !err)
  8234. tg3_phy_start(tp);
  8235. return err;
  8236. }
  8237. static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
  8238. {
  8239. struct tg3 *tp = netdev_priv(dev);
  8240. epause->autoneg = (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG) != 0;
  8241. if (tp->link_config.active_flowctrl & FLOW_CTRL_RX)
  8242. epause->rx_pause = 1;
  8243. else
  8244. epause->rx_pause = 0;
  8245. if (tp->link_config.active_flowctrl & FLOW_CTRL_TX)
  8246. epause->tx_pause = 1;
  8247. else
  8248. epause->tx_pause = 0;
  8249. }
  8250. static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
  8251. {
  8252. struct tg3 *tp = netdev_priv(dev);
  8253. int err = 0;
  8254. if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
  8255. u32 newadv;
  8256. struct phy_device *phydev;
  8257. phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
  8258. if (!(phydev->supported & SUPPORTED_Pause) ||
  8259. (!(phydev->supported & SUPPORTED_Asym_Pause) &&
  8260. ((epause->rx_pause && !epause->tx_pause) ||
  8261. (!epause->rx_pause && epause->tx_pause))))
  8262. return -EINVAL;
  8263. tp->link_config.flowctrl = 0;
  8264. if (epause->rx_pause) {
  8265. tp->link_config.flowctrl |= FLOW_CTRL_RX;
  8266. if (epause->tx_pause) {
  8267. tp->link_config.flowctrl |= FLOW_CTRL_TX;
  8268. newadv = ADVERTISED_Pause;
  8269. } else
  8270. newadv = ADVERTISED_Pause |
  8271. ADVERTISED_Asym_Pause;
  8272. } else if (epause->tx_pause) {
  8273. tp->link_config.flowctrl |= FLOW_CTRL_TX;
  8274. newadv = ADVERTISED_Asym_Pause;
  8275. } else
  8276. newadv = 0;
  8277. if (epause->autoneg)
  8278. tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
  8279. else
  8280. tp->tg3_flags &= ~TG3_FLAG_PAUSE_AUTONEG;
  8281. if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
  8282. u32 oldadv = phydev->advertising &
  8283. (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
  8284. if (oldadv != newadv) {
  8285. phydev->advertising &=
  8286. ~(ADVERTISED_Pause |
  8287. ADVERTISED_Asym_Pause);
  8288. phydev->advertising |= newadv;
  8289. if (phydev->autoneg) {
  8290. /*
  8291. * Always renegotiate the link to
  8292. * inform our link partner of our
  8293. * flow control settings, even if the
  8294. * flow control is forced. Let
  8295. * tg3_adjust_link() do the final
  8296. * flow control setup.
  8297. */
  8298. return phy_start_aneg(phydev);
  8299. }
  8300. }
  8301. if (!epause->autoneg)
  8302. tg3_setup_flow_control(tp, 0, 0);
  8303. } else {
  8304. tp->link_config.orig_advertising &=
  8305. ~(ADVERTISED_Pause |
  8306. ADVERTISED_Asym_Pause);
  8307. tp->link_config.orig_advertising |= newadv;
  8308. }
  8309. } else {
  8310. int irq_sync = 0;
  8311. if (netif_running(dev)) {
  8312. tg3_netif_stop(tp);
  8313. irq_sync = 1;
  8314. }
  8315. tg3_full_lock(tp, irq_sync);
  8316. if (epause->autoneg)
  8317. tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
  8318. else
  8319. tp->tg3_flags &= ~TG3_FLAG_PAUSE_AUTONEG;
  8320. if (epause->rx_pause)
  8321. tp->link_config.flowctrl |= FLOW_CTRL_RX;
  8322. else
  8323. tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
  8324. if (epause->tx_pause)
  8325. tp->link_config.flowctrl |= FLOW_CTRL_TX;
  8326. else
  8327. tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
  8328. if (netif_running(dev)) {
  8329. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  8330. err = tg3_restart_hw(tp, 1);
  8331. if (!err)
  8332. tg3_netif_start(tp);
  8333. }
  8334. tg3_full_unlock(tp);
  8335. }
  8336. return err;
  8337. }
  8338. static u32 tg3_get_rx_csum(struct net_device *dev)
  8339. {
  8340. struct tg3 *tp = netdev_priv(dev);
  8341. return (tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) != 0;
  8342. }
  8343. static int tg3_set_rx_csum(struct net_device *dev, u32 data)
  8344. {
  8345. struct tg3 *tp = netdev_priv(dev);
  8346. if (tp->tg3_flags & TG3_FLAG_BROKEN_CHECKSUMS) {
  8347. if (data != 0)
  8348. return -EINVAL;
  8349. return 0;
  8350. }
  8351. spin_lock_bh(&tp->lock);
  8352. if (data)
  8353. tp->tg3_flags |= TG3_FLAG_RX_CHECKSUMS;
  8354. else
  8355. tp->tg3_flags &= ~TG3_FLAG_RX_CHECKSUMS;
  8356. spin_unlock_bh(&tp->lock);
  8357. return 0;
  8358. }
  8359. static int tg3_set_tx_csum(struct net_device *dev, u32 data)
  8360. {
  8361. struct tg3 *tp = netdev_priv(dev);
  8362. if (tp->tg3_flags & TG3_FLAG_BROKEN_CHECKSUMS) {
  8363. if (data != 0)
  8364. return -EINVAL;
  8365. return 0;
  8366. }
  8367. if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
  8368. ethtool_op_set_tx_ipv6_csum(dev, data);
  8369. else
  8370. ethtool_op_set_tx_csum(dev, data);
  8371. return 0;
  8372. }
  8373. static int tg3_get_sset_count(struct net_device *dev, int sset)
  8374. {
  8375. switch (sset) {
  8376. case ETH_SS_TEST:
  8377. return TG3_NUM_TEST;
  8378. case ETH_SS_STATS:
  8379. return TG3_NUM_STATS;
  8380. default:
  8381. return -EOPNOTSUPP;
  8382. }
  8383. }
  8384. static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
  8385. {
  8386. switch (stringset) {
  8387. case ETH_SS_STATS:
  8388. memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
  8389. break;
  8390. case ETH_SS_TEST:
  8391. memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
  8392. break;
  8393. default:
  8394. WARN_ON(1); /* we need a WARN() */
  8395. break;
  8396. }
  8397. }
  8398. static int tg3_phys_id(struct net_device *dev, u32 data)
  8399. {
  8400. struct tg3 *tp = netdev_priv(dev);
  8401. int i;
  8402. if (!netif_running(tp->dev))
  8403. return -EAGAIN;
  8404. if (data == 0)
  8405. data = UINT_MAX / 2;
  8406. for (i = 0; i < (data * 2); i++) {
  8407. if ((i % 2) == 0)
  8408. tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
  8409. LED_CTRL_1000MBPS_ON |
  8410. LED_CTRL_100MBPS_ON |
  8411. LED_CTRL_10MBPS_ON |
  8412. LED_CTRL_TRAFFIC_OVERRIDE |
  8413. LED_CTRL_TRAFFIC_BLINK |
  8414. LED_CTRL_TRAFFIC_LED);
  8415. else
  8416. tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
  8417. LED_CTRL_TRAFFIC_OVERRIDE);
  8418. if (msleep_interruptible(500))
  8419. break;
  8420. }
  8421. tw32(MAC_LED_CTRL, tp->led_ctrl);
  8422. return 0;
  8423. }
  8424. static void tg3_get_ethtool_stats(struct net_device *dev,
  8425. struct ethtool_stats *estats, u64 *tmp_stats)
  8426. {
  8427. struct tg3 *tp = netdev_priv(dev);
  8428. memcpy(tmp_stats, tg3_get_estats(tp), sizeof(tp->estats));
  8429. }
  8430. #define NVRAM_TEST_SIZE 0x100
  8431. #define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
  8432. #define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
  8433. #define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
  8434. #define NVRAM_SELFBOOT_HW_SIZE 0x20
  8435. #define NVRAM_SELFBOOT_DATA_SIZE 0x1c
  8436. static int tg3_test_nvram(struct tg3 *tp)
  8437. {
  8438. u32 csum, magic;
  8439. __be32 *buf;
  8440. int i, j, k, err = 0, size;
  8441. if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM)
  8442. return 0;
  8443. if (tg3_nvram_read(tp, 0, &magic) != 0)
  8444. return -EIO;
  8445. if (magic == TG3_EEPROM_MAGIC)
  8446. size = NVRAM_TEST_SIZE;
  8447. else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
  8448. if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
  8449. TG3_EEPROM_SB_FORMAT_1) {
  8450. switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
  8451. case TG3_EEPROM_SB_REVISION_0:
  8452. size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
  8453. break;
  8454. case TG3_EEPROM_SB_REVISION_2:
  8455. size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
  8456. break;
  8457. case TG3_EEPROM_SB_REVISION_3:
  8458. size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
  8459. break;
  8460. default:
  8461. return 0;
  8462. }
  8463. } else
  8464. return 0;
  8465. } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
  8466. size = NVRAM_SELFBOOT_HW_SIZE;
  8467. else
  8468. return -EIO;
  8469. buf = kmalloc(size, GFP_KERNEL);
  8470. if (buf == NULL)
  8471. return -ENOMEM;
  8472. err = -EIO;
  8473. for (i = 0, j = 0; i < size; i += 4, j++) {
  8474. err = tg3_nvram_read_be32(tp, i, &buf[j]);
  8475. if (err)
  8476. break;
  8477. }
  8478. if (i < size)
  8479. goto out;
  8480. /* Selfboot format */
  8481. magic = be32_to_cpu(buf[0]);
  8482. if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
  8483. TG3_EEPROM_MAGIC_FW) {
  8484. u8 *buf8 = (u8 *) buf, csum8 = 0;
  8485. if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
  8486. TG3_EEPROM_SB_REVISION_2) {
  8487. /* For rev 2, the csum doesn't include the MBA. */
  8488. for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
  8489. csum8 += buf8[i];
  8490. for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
  8491. csum8 += buf8[i];
  8492. } else {
  8493. for (i = 0; i < size; i++)
  8494. csum8 += buf8[i];
  8495. }
  8496. if (csum8 == 0) {
  8497. err = 0;
  8498. goto out;
  8499. }
  8500. err = -EIO;
  8501. goto out;
  8502. }
  8503. if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
  8504. TG3_EEPROM_MAGIC_HW) {
  8505. u8 data[NVRAM_SELFBOOT_DATA_SIZE];
  8506. u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
  8507. u8 *buf8 = (u8 *) buf;
  8508. /* Separate the parity bits and the data bytes. */
  8509. for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
  8510. if ((i == 0) || (i == 8)) {
  8511. int l;
  8512. u8 msk;
  8513. for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
  8514. parity[k++] = buf8[i] & msk;
  8515. i++;
  8516. } else if (i == 16) {
  8517. int l;
  8518. u8 msk;
  8519. for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
  8520. parity[k++] = buf8[i] & msk;
  8521. i++;
  8522. for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
  8523. parity[k++] = buf8[i] & msk;
  8524. i++;
  8525. }
  8526. data[j++] = buf8[i];
  8527. }
  8528. err = -EIO;
  8529. for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
  8530. u8 hw8 = hweight8(data[i]);
  8531. if ((hw8 & 0x1) && parity[i])
  8532. goto out;
  8533. else if (!(hw8 & 0x1) && !parity[i])
  8534. goto out;
  8535. }
  8536. err = 0;
  8537. goto out;
  8538. }
  8539. /* Bootstrap checksum at offset 0x10 */
  8540. csum = calc_crc((unsigned char *) buf, 0x10);
  8541. if (csum != be32_to_cpu(buf[0x10/4]))
  8542. goto out;
  8543. /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
  8544. csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
  8545. if (csum != be32_to_cpu(buf[0xfc/4]))
  8546. goto out;
  8547. err = 0;
  8548. out:
  8549. kfree(buf);
  8550. return err;
  8551. }
  8552. #define TG3_SERDES_TIMEOUT_SEC 2
  8553. #define TG3_COPPER_TIMEOUT_SEC 6
  8554. static int tg3_test_link(struct tg3 *tp)
  8555. {
  8556. int i, max;
  8557. if (!netif_running(tp->dev))
  8558. return -ENODEV;
  8559. if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
  8560. max = TG3_SERDES_TIMEOUT_SEC;
  8561. else
  8562. max = TG3_COPPER_TIMEOUT_SEC;
  8563. for (i = 0; i < max; i++) {
  8564. if (netif_carrier_ok(tp->dev))
  8565. return 0;
  8566. if (msleep_interruptible(1000))
  8567. break;
  8568. }
  8569. return -EIO;
  8570. }
  8571. /* Only test the commonly used registers */
  8572. static int tg3_test_registers(struct tg3 *tp)
  8573. {
  8574. int i, is_5705, is_5750;
  8575. u32 offset, read_mask, write_mask, val, save_val, read_val;
  8576. static struct {
  8577. u16 offset;
  8578. u16 flags;
  8579. #define TG3_FL_5705 0x1
  8580. #define TG3_FL_NOT_5705 0x2
  8581. #define TG3_FL_NOT_5788 0x4
  8582. #define TG3_FL_NOT_5750 0x8
  8583. u32 read_mask;
  8584. u32 write_mask;
  8585. } reg_tbl[] = {
  8586. /* MAC Control Registers */
  8587. { MAC_MODE, TG3_FL_NOT_5705,
  8588. 0x00000000, 0x00ef6f8c },
  8589. { MAC_MODE, TG3_FL_5705,
  8590. 0x00000000, 0x01ef6b8c },
  8591. { MAC_STATUS, TG3_FL_NOT_5705,
  8592. 0x03800107, 0x00000000 },
  8593. { MAC_STATUS, TG3_FL_5705,
  8594. 0x03800100, 0x00000000 },
  8595. { MAC_ADDR_0_HIGH, 0x0000,
  8596. 0x00000000, 0x0000ffff },
  8597. { MAC_ADDR_0_LOW, 0x0000,
  8598. 0x00000000, 0xffffffff },
  8599. { MAC_RX_MTU_SIZE, 0x0000,
  8600. 0x00000000, 0x0000ffff },
  8601. { MAC_TX_MODE, 0x0000,
  8602. 0x00000000, 0x00000070 },
  8603. { MAC_TX_LENGTHS, 0x0000,
  8604. 0x00000000, 0x00003fff },
  8605. { MAC_RX_MODE, TG3_FL_NOT_5705,
  8606. 0x00000000, 0x000007fc },
  8607. { MAC_RX_MODE, TG3_FL_5705,
  8608. 0x00000000, 0x000007dc },
  8609. { MAC_HASH_REG_0, 0x0000,
  8610. 0x00000000, 0xffffffff },
  8611. { MAC_HASH_REG_1, 0x0000,
  8612. 0x00000000, 0xffffffff },
  8613. { MAC_HASH_REG_2, 0x0000,
  8614. 0x00000000, 0xffffffff },
  8615. { MAC_HASH_REG_3, 0x0000,
  8616. 0x00000000, 0xffffffff },
  8617. /* Receive Data and Receive BD Initiator Control Registers. */
  8618. { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
  8619. 0x00000000, 0xffffffff },
  8620. { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
  8621. 0x00000000, 0xffffffff },
  8622. { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
  8623. 0x00000000, 0x00000003 },
  8624. { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
  8625. 0x00000000, 0xffffffff },
  8626. { RCVDBDI_STD_BD+0, 0x0000,
  8627. 0x00000000, 0xffffffff },
  8628. { RCVDBDI_STD_BD+4, 0x0000,
  8629. 0x00000000, 0xffffffff },
  8630. { RCVDBDI_STD_BD+8, 0x0000,
  8631. 0x00000000, 0xffff0002 },
  8632. { RCVDBDI_STD_BD+0xc, 0x0000,
  8633. 0x00000000, 0xffffffff },
  8634. /* Receive BD Initiator Control Registers. */
  8635. { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
  8636. 0x00000000, 0xffffffff },
  8637. { RCVBDI_STD_THRESH, TG3_FL_5705,
  8638. 0x00000000, 0x000003ff },
  8639. { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
  8640. 0x00000000, 0xffffffff },
  8641. /* Host Coalescing Control Registers. */
  8642. { HOSTCC_MODE, TG3_FL_NOT_5705,
  8643. 0x00000000, 0x00000004 },
  8644. { HOSTCC_MODE, TG3_FL_5705,
  8645. 0x00000000, 0x000000f6 },
  8646. { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
  8647. 0x00000000, 0xffffffff },
  8648. { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
  8649. 0x00000000, 0x000003ff },
  8650. { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
  8651. 0x00000000, 0xffffffff },
  8652. { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
  8653. 0x00000000, 0x000003ff },
  8654. { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
  8655. 0x00000000, 0xffffffff },
  8656. { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
  8657. 0x00000000, 0x000000ff },
  8658. { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
  8659. 0x00000000, 0xffffffff },
  8660. { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
  8661. 0x00000000, 0x000000ff },
  8662. { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
  8663. 0x00000000, 0xffffffff },
  8664. { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
  8665. 0x00000000, 0xffffffff },
  8666. { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
  8667. 0x00000000, 0xffffffff },
  8668. { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
  8669. 0x00000000, 0x000000ff },
  8670. { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
  8671. 0x00000000, 0xffffffff },
  8672. { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
  8673. 0x00000000, 0x000000ff },
  8674. { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
  8675. 0x00000000, 0xffffffff },
  8676. { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
  8677. 0x00000000, 0xffffffff },
  8678. { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
  8679. 0x00000000, 0xffffffff },
  8680. { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
  8681. 0x00000000, 0xffffffff },
  8682. { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
  8683. 0x00000000, 0xffffffff },
  8684. { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
  8685. 0xffffffff, 0x00000000 },
  8686. { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
  8687. 0xffffffff, 0x00000000 },
  8688. /* Buffer Manager Control Registers. */
  8689. { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
  8690. 0x00000000, 0x007fff80 },
  8691. { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
  8692. 0x00000000, 0x007fffff },
  8693. { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
  8694. 0x00000000, 0x0000003f },
  8695. { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
  8696. 0x00000000, 0x000001ff },
  8697. { BUFMGR_MB_HIGH_WATER, 0x0000,
  8698. 0x00000000, 0x000001ff },
  8699. { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
  8700. 0xffffffff, 0x00000000 },
  8701. { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
  8702. 0xffffffff, 0x00000000 },
  8703. /* Mailbox Registers */
  8704. { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
  8705. 0x00000000, 0x000001ff },
  8706. { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
  8707. 0x00000000, 0x000001ff },
  8708. { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
  8709. 0x00000000, 0x000007ff },
  8710. { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
  8711. 0x00000000, 0x000001ff },
  8712. { 0xffff, 0x0000, 0x00000000, 0x00000000 },
  8713. };
  8714. is_5705 = is_5750 = 0;
  8715. if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
  8716. is_5705 = 1;
  8717. if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
  8718. is_5750 = 1;
  8719. }
  8720. for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
  8721. if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
  8722. continue;
  8723. if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
  8724. continue;
  8725. if ((tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
  8726. (reg_tbl[i].flags & TG3_FL_NOT_5788))
  8727. continue;
  8728. if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
  8729. continue;
  8730. offset = (u32) reg_tbl[i].offset;
  8731. read_mask = reg_tbl[i].read_mask;
  8732. write_mask = reg_tbl[i].write_mask;
  8733. /* Save the original register content */
  8734. save_val = tr32(offset);
  8735. /* Determine the read-only value. */
  8736. read_val = save_val & read_mask;
  8737. /* Write zero to the register, then make sure the read-only bits
  8738. * are not changed and the read/write bits are all zeros.
  8739. */
  8740. tw32(offset, 0);
  8741. val = tr32(offset);
  8742. /* Test the read-only and read/write bits. */
  8743. if (((val & read_mask) != read_val) || (val & write_mask))
  8744. goto out;
  8745. /* Write ones to all the bits defined by RdMask and WrMask, then
  8746. * make sure the read-only bits are not changed and the
  8747. * read/write bits are all ones.
  8748. */
  8749. tw32(offset, read_mask | write_mask);
  8750. val = tr32(offset);
  8751. /* Test the read-only bits. */
  8752. if ((val & read_mask) != read_val)
  8753. goto out;
  8754. /* Test the read/write bits. */
  8755. if ((val & write_mask) != write_mask)
  8756. goto out;
  8757. tw32(offset, save_val);
  8758. }
  8759. return 0;
  8760. out:
  8761. if (netif_msg_hw(tp))
  8762. netdev_err(tp->dev,
  8763. "Register test failed at offset %x\n", offset);
  8764. tw32(offset, save_val);
  8765. return -EIO;
  8766. }
  8767. static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
  8768. {
  8769. static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
  8770. int i;
  8771. u32 j;
  8772. for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
  8773. for (j = 0; j < len; j += 4) {
  8774. u32 val;
  8775. tg3_write_mem(tp, offset + j, test_pattern[i]);
  8776. tg3_read_mem(tp, offset + j, &val);
  8777. if (val != test_pattern[i])
  8778. return -EIO;
  8779. }
  8780. }
  8781. return 0;
  8782. }
  8783. static int tg3_test_memory(struct tg3 *tp)
  8784. {
  8785. static struct mem_entry {
  8786. u32 offset;
  8787. u32 len;
  8788. } mem_tbl_570x[] = {
  8789. { 0x00000000, 0x00b50},
  8790. { 0x00002000, 0x1c000},
  8791. { 0xffffffff, 0x00000}
  8792. }, mem_tbl_5705[] = {
  8793. { 0x00000100, 0x0000c},
  8794. { 0x00000200, 0x00008},
  8795. { 0x00004000, 0x00800},
  8796. { 0x00006000, 0x01000},
  8797. { 0x00008000, 0x02000},
  8798. { 0x00010000, 0x0e000},
  8799. { 0xffffffff, 0x00000}
  8800. }, mem_tbl_5755[] = {
  8801. { 0x00000200, 0x00008},
  8802. { 0x00004000, 0x00800},
  8803. { 0x00006000, 0x00800},
  8804. { 0x00008000, 0x02000},
  8805. { 0x00010000, 0x0c000},
  8806. { 0xffffffff, 0x00000}
  8807. }, mem_tbl_5906[] = {
  8808. { 0x00000200, 0x00008},
  8809. { 0x00004000, 0x00400},
  8810. { 0x00006000, 0x00400},
  8811. { 0x00008000, 0x01000},
  8812. { 0x00010000, 0x01000},
  8813. { 0xffffffff, 0x00000}
  8814. }, mem_tbl_5717[] = {
  8815. { 0x00000200, 0x00008},
  8816. { 0x00010000, 0x0a000},
  8817. { 0x00020000, 0x13c00},
  8818. { 0xffffffff, 0x00000}
  8819. }, mem_tbl_57765[] = {
  8820. { 0x00000200, 0x00008},
  8821. { 0x00004000, 0x00800},
  8822. { 0x00006000, 0x09800},
  8823. { 0x00010000, 0x0a000},
  8824. { 0xffffffff, 0x00000}
  8825. };
  8826. struct mem_entry *mem_tbl;
  8827. int err = 0;
  8828. int i;
  8829. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
  8830. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
  8831. mem_tbl = mem_tbl_5717;
  8832. else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
  8833. mem_tbl = mem_tbl_57765;
  8834. else if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
  8835. mem_tbl = mem_tbl_5755;
  8836. else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
  8837. mem_tbl = mem_tbl_5906;
  8838. else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
  8839. mem_tbl = mem_tbl_5705;
  8840. else
  8841. mem_tbl = mem_tbl_570x;
  8842. for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
  8843. err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
  8844. if (err)
  8845. break;
  8846. }
  8847. return err;
  8848. }
  8849. #define TG3_MAC_LOOPBACK 0
  8850. #define TG3_PHY_LOOPBACK 1
  8851. static int tg3_run_loopback(struct tg3 *tp, int loopback_mode)
  8852. {
  8853. u32 mac_mode, rx_start_idx, rx_idx, tx_idx, opaque_key;
  8854. u32 desc_idx, coal_now;
  8855. struct sk_buff *skb, *rx_skb;
  8856. u8 *tx_data;
  8857. dma_addr_t map;
  8858. int num_pkts, tx_len, rx_len, i, err;
  8859. struct tg3_rx_buffer_desc *desc;
  8860. struct tg3_napi *tnapi, *rnapi;
  8861. struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
  8862. tnapi = &tp->napi[0];
  8863. rnapi = &tp->napi[0];
  8864. if (tp->irq_cnt > 1) {
  8865. rnapi = &tp->napi[1];
  8866. if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
  8867. tnapi = &tp->napi[1];
  8868. }
  8869. coal_now = tnapi->coal_now | rnapi->coal_now;
  8870. if (loopback_mode == TG3_MAC_LOOPBACK) {
  8871. /* HW errata - mac loopback fails in some cases on 5780.
  8872. * Normal traffic and PHY loopback are not affected by
  8873. * errata.
  8874. */
  8875. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780)
  8876. return 0;
  8877. mac_mode = (tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK) |
  8878. MAC_MODE_PORT_INT_LPBACK;
  8879. if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
  8880. mac_mode |= MAC_MODE_LINK_POLARITY;
  8881. if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
  8882. mac_mode |= MAC_MODE_PORT_MODE_MII;
  8883. else
  8884. mac_mode |= MAC_MODE_PORT_MODE_GMII;
  8885. tw32(MAC_MODE, mac_mode);
  8886. } else if (loopback_mode == TG3_PHY_LOOPBACK) {
  8887. u32 val;
  8888. if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
  8889. tg3_phy_fet_toggle_apd(tp, false);
  8890. val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED100;
  8891. } else
  8892. val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED1000;
  8893. tg3_phy_toggle_automdix(tp, 0);
  8894. tg3_writephy(tp, MII_BMCR, val);
  8895. udelay(40);
  8896. mac_mode = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
  8897. if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
  8898. tg3_writephy(tp, MII_TG3_FET_PTEST,
  8899. MII_TG3_FET_PTEST_FRC_TX_LINK |
  8900. MII_TG3_FET_PTEST_FRC_TX_LOCK);
  8901. /* The write needs to be flushed for the AC131 */
  8902. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
  8903. tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
  8904. mac_mode |= MAC_MODE_PORT_MODE_MII;
  8905. } else
  8906. mac_mode |= MAC_MODE_PORT_MODE_GMII;
  8907. /* reset to prevent losing 1st rx packet intermittently */
  8908. if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
  8909. tw32_f(MAC_RX_MODE, RX_MODE_RESET);
  8910. udelay(10);
  8911. tw32_f(MAC_RX_MODE, tp->rx_mode);
  8912. }
  8913. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
  8914. u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
  8915. if (masked_phy_id == TG3_PHY_ID_BCM5401)
  8916. mac_mode &= ~MAC_MODE_LINK_POLARITY;
  8917. else if (masked_phy_id == TG3_PHY_ID_BCM5411)
  8918. mac_mode |= MAC_MODE_LINK_POLARITY;
  8919. tg3_writephy(tp, MII_TG3_EXT_CTRL,
  8920. MII_TG3_EXT_CTRL_LNK3_LED_MODE);
  8921. }
  8922. tw32(MAC_MODE, mac_mode);
  8923. } else {
  8924. return -EINVAL;
  8925. }
  8926. err = -EIO;
  8927. tx_len = 1514;
  8928. skb = netdev_alloc_skb(tp->dev, tx_len);
  8929. if (!skb)
  8930. return -ENOMEM;
  8931. tx_data = skb_put(skb, tx_len);
  8932. memcpy(tx_data, tp->dev->dev_addr, 6);
  8933. memset(tx_data + 6, 0x0, 8);
  8934. tw32(MAC_RX_MTU_SIZE, tx_len + 4);
  8935. for (i = 14; i < tx_len; i++)
  8936. tx_data[i] = (u8) (i & 0xff);
  8937. map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
  8938. if (pci_dma_mapping_error(tp->pdev, map)) {
  8939. dev_kfree_skb(skb);
  8940. return -EIO;
  8941. }
  8942. tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
  8943. rnapi->coal_now);
  8944. udelay(10);
  8945. rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
  8946. num_pkts = 0;
  8947. tg3_set_txd(tnapi, tnapi->tx_prod, map, tx_len, 0, 1);
  8948. tnapi->tx_prod++;
  8949. num_pkts++;
  8950. tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
  8951. tr32_mailbox(tnapi->prodmbox);
  8952. udelay(10);
  8953. /* 350 usec to allow enough time on some 10/100 Mbps devices. */
  8954. for (i = 0; i < 35; i++) {
  8955. tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
  8956. coal_now);
  8957. udelay(10);
  8958. tx_idx = tnapi->hw_status->idx[0].tx_consumer;
  8959. rx_idx = rnapi->hw_status->idx[0].rx_producer;
  8960. if ((tx_idx == tnapi->tx_prod) &&
  8961. (rx_idx == (rx_start_idx + num_pkts)))
  8962. break;
  8963. }
  8964. pci_unmap_single(tp->pdev, map, tx_len, PCI_DMA_TODEVICE);
  8965. dev_kfree_skb(skb);
  8966. if (tx_idx != tnapi->tx_prod)
  8967. goto out;
  8968. if (rx_idx != rx_start_idx + num_pkts)
  8969. goto out;
  8970. desc = &rnapi->rx_rcb[rx_start_idx];
  8971. desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
  8972. opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
  8973. if (opaque_key != RXD_OPAQUE_RING_STD)
  8974. goto out;
  8975. if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
  8976. (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
  8977. goto out;
  8978. rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) - 4;
  8979. if (rx_len != tx_len)
  8980. goto out;
  8981. rx_skb = tpr->rx_std_buffers[desc_idx].skb;
  8982. map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx], mapping);
  8983. pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len, PCI_DMA_FROMDEVICE);
  8984. for (i = 14; i < tx_len; i++) {
  8985. if (*(rx_skb->data + i) != (u8) (i & 0xff))
  8986. goto out;
  8987. }
  8988. err = 0;
  8989. /* tg3_free_rings will unmap and free the rx_skb */
  8990. out:
  8991. return err;
  8992. }
  8993. #define TG3_MAC_LOOPBACK_FAILED 1
  8994. #define TG3_PHY_LOOPBACK_FAILED 2
  8995. #define TG3_LOOPBACK_FAILED (TG3_MAC_LOOPBACK_FAILED | \
  8996. TG3_PHY_LOOPBACK_FAILED)
  8997. static int tg3_test_loopback(struct tg3 *tp)
  8998. {
  8999. int err = 0;
  9000. u32 cpmuctrl = 0;
  9001. if (!netif_running(tp->dev))
  9002. return TG3_LOOPBACK_FAILED;
  9003. err = tg3_reset_hw(tp, 1);
  9004. if (err)
  9005. return TG3_LOOPBACK_FAILED;
  9006. /* Turn off gphy autopowerdown. */
  9007. if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
  9008. tg3_phy_toggle_apd(tp, false);
  9009. if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) {
  9010. int i;
  9011. u32 status;
  9012. tw32(TG3_CPMU_MUTEX_REQ, CPMU_MUTEX_REQ_DRIVER);
  9013. /* Wait for up to 40 microseconds to acquire lock. */
  9014. for (i = 0; i < 4; i++) {
  9015. status = tr32(TG3_CPMU_MUTEX_GNT);
  9016. if (status == CPMU_MUTEX_GNT_DRIVER)
  9017. break;
  9018. udelay(10);
  9019. }
  9020. if (status != CPMU_MUTEX_GNT_DRIVER)
  9021. return TG3_LOOPBACK_FAILED;
  9022. /* Turn off link-based power management. */
  9023. cpmuctrl = tr32(TG3_CPMU_CTRL);
  9024. tw32(TG3_CPMU_CTRL,
  9025. cpmuctrl & ~(CPMU_CTRL_LINK_SPEED_MODE |
  9026. CPMU_CTRL_LINK_AWARE_MODE));
  9027. }
  9028. if (tg3_run_loopback(tp, TG3_MAC_LOOPBACK))
  9029. err |= TG3_MAC_LOOPBACK_FAILED;
  9030. if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) {
  9031. tw32(TG3_CPMU_CTRL, cpmuctrl);
  9032. /* Release the mutex */
  9033. tw32(TG3_CPMU_MUTEX_GNT, CPMU_MUTEX_GNT_DRIVER);
  9034. }
  9035. if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
  9036. !(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)) {
  9037. if (tg3_run_loopback(tp, TG3_PHY_LOOPBACK))
  9038. err |= TG3_PHY_LOOPBACK_FAILED;
  9039. }
  9040. /* Re-enable gphy autopowerdown. */
  9041. if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
  9042. tg3_phy_toggle_apd(tp, true);
  9043. return err;
  9044. }
  9045. static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
  9046. u64 *data)
  9047. {
  9048. struct tg3 *tp = netdev_priv(dev);
  9049. if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
  9050. tg3_set_power_state(tp, PCI_D0);
  9051. memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
  9052. if (tg3_test_nvram(tp) != 0) {
  9053. etest->flags |= ETH_TEST_FL_FAILED;
  9054. data[0] = 1;
  9055. }
  9056. if (tg3_test_link(tp) != 0) {
  9057. etest->flags |= ETH_TEST_FL_FAILED;
  9058. data[1] = 1;
  9059. }
  9060. if (etest->flags & ETH_TEST_FL_OFFLINE) {
  9061. int err, err2 = 0, irq_sync = 0;
  9062. if (netif_running(dev)) {
  9063. tg3_phy_stop(tp);
  9064. tg3_netif_stop(tp);
  9065. irq_sync = 1;
  9066. }
  9067. tg3_full_lock(tp, irq_sync);
  9068. tg3_halt(tp, RESET_KIND_SUSPEND, 1);
  9069. err = tg3_nvram_lock(tp);
  9070. tg3_halt_cpu(tp, RX_CPU_BASE);
  9071. if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
  9072. tg3_halt_cpu(tp, TX_CPU_BASE);
  9073. if (!err)
  9074. tg3_nvram_unlock(tp);
  9075. if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
  9076. tg3_phy_reset(tp);
  9077. if (tg3_test_registers(tp) != 0) {
  9078. etest->flags |= ETH_TEST_FL_FAILED;
  9079. data[2] = 1;
  9080. }
  9081. if (tg3_test_memory(tp) != 0) {
  9082. etest->flags |= ETH_TEST_FL_FAILED;
  9083. data[3] = 1;
  9084. }
  9085. if ((data[4] = tg3_test_loopback(tp)) != 0)
  9086. etest->flags |= ETH_TEST_FL_FAILED;
  9087. tg3_full_unlock(tp);
  9088. if (tg3_test_interrupt(tp) != 0) {
  9089. etest->flags |= ETH_TEST_FL_FAILED;
  9090. data[5] = 1;
  9091. }
  9092. tg3_full_lock(tp, 0);
  9093. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  9094. if (netif_running(dev)) {
  9095. tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
  9096. err2 = tg3_restart_hw(tp, 1);
  9097. if (!err2)
  9098. tg3_netif_start(tp);
  9099. }
  9100. tg3_full_unlock(tp);
  9101. if (irq_sync && !err2)
  9102. tg3_phy_start(tp);
  9103. }
  9104. if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
  9105. tg3_set_power_state(tp, PCI_D3hot);
  9106. }
  9107. static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  9108. {
  9109. struct mii_ioctl_data *data = if_mii(ifr);
  9110. struct tg3 *tp = netdev_priv(dev);
  9111. int err;
  9112. if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
  9113. struct phy_device *phydev;
  9114. if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
  9115. return -EAGAIN;
  9116. phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
  9117. return phy_mii_ioctl(phydev, ifr, cmd);
  9118. }
  9119. switch (cmd) {
  9120. case SIOCGMIIPHY:
  9121. data->phy_id = tp->phy_addr;
  9122. /* fallthru */
  9123. case SIOCGMIIREG: {
  9124. u32 mii_regval;
  9125. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
  9126. break; /* We have no PHY */
  9127. if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
  9128. return -EAGAIN;
  9129. spin_lock_bh(&tp->lock);
  9130. err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
  9131. spin_unlock_bh(&tp->lock);
  9132. data->val_out = mii_regval;
  9133. return err;
  9134. }
  9135. case SIOCSMIIREG:
  9136. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
  9137. break; /* We have no PHY */
  9138. if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
  9139. return -EAGAIN;
  9140. spin_lock_bh(&tp->lock);
  9141. err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
  9142. spin_unlock_bh(&tp->lock);
  9143. return err;
  9144. default:
  9145. /* do nothing */
  9146. break;
  9147. }
  9148. return -EOPNOTSUPP;
  9149. }
  9150. #if TG3_VLAN_TAG_USED
  9151. static void tg3_vlan_rx_register(struct net_device *dev, struct vlan_group *grp)
  9152. {
  9153. struct tg3 *tp = netdev_priv(dev);
  9154. if (!netif_running(dev)) {
  9155. tp->vlgrp = grp;
  9156. return;
  9157. }
  9158. tg3_netif_stop(tp);
  9159. tg3_full_lock(tp, 0);
  9160. tp->vlgrp = grp;
  9161. /* Update RX_MODE_KEEP_VLAN_TAG bit in RX_MODE register. */
  9162. __tg3_set_rx_mode(dev);
  9163. tg3_netif_start(tp);
  9164. tg3_full_unlock(tp);
  9165. }
  9166. #endif
  9167. static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
  9168. {
  9169. struct tg3 *tp = netdev_priv(dev);
  9170. memcpy(ec, &tp->coal, sizeof(*ec));
  9171. return 0;
  9172. }
  9173. static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
  9174. {
  9175. struct tg3 *tp = netdev_priv(dev);
  9176. u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
  9177. u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
  9178. if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
  9179. max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
  9180. max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
  9181. max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
  9182. min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
  9183. }
  9184. if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
  9185. (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
  9186. (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
  9187. (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
  9188. (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
  9189. (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
  9190. (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
  9191. (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
  9192. (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
  9193. (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
  9194. return -EINVAL;
  9195. /* No rx interrupts will be generated if both are zero */
  9196. if ((ec->rx_coalesce_usecs == 0) &&
  9197. (ec->rx_max_coalesced_frames == 0))
  9198. return -EINVAL;
  9199. /* No tx interrupts will be generated if both are zero */
  9200. if ((ec->tx_coalesce_usecs == 0) &&
  9201. (ec->tx_max_coalesced_frames == 0))
  9202. return -EINVAL;
  9203. /* Only copy relevant parameters, ignore all others. */
  9204. tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
  9205. tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
  9206. tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
  9207. tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
  9208. tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
  9209. tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
  9210. tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
  9211. tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
  9212. tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
  9213. if (netif_running(dev)) {
  9214. tg3_full_lock(tp, 0);
  9215. __tg3_set_coalesce(tp, &tp->coal);
  9216. tg3_full_unlock(tp);
  9217. }
  9218. return 0;
  9219. }
  9220. static const struct ethtool_ops tg3_ethtool_ops = {
  9221. .get_settings = tg3_get_settings,
  9222. .set_settings = tg3_set_settings,
  9223. .get_drvinfo = tg3_get_drvinfo,
  9224. .get_regs_len = tg3_get_regs_len,
  9225. .get_regs = tg3_get_regs,
  9226. .get_wol = tg3_get_wol,
  9227. .set_wol = tg3_set_wol,
  9228. .get_msglevel = tg3_get_msglevel,
  9229. .set_msglevel = tg3_set_msglevel,
  9230. .nway_reset = tg3_nway_reset,
  9231. .get_link = ethtool_op_get_link,
  9232. .get_eeprom_len = tg3_get_eeprom_len,
  9233. .get_eeprom = tg3_get_eeprom,
  9234. .set_eeprom = tg3_set_eeprom,
  9235. .get_ringparam = tg3_get_ringparam,
  9236. .set_ringparam = tg3_set_ringparam,
  9237. .get_pauseparam = tg3_get_pauseparam,
  9238. .set_pauseparam = tg3_set_pauseparam,
  9239. .get_rx_csum = tg3_get_rx_csum,
  9240. .set_rx_csum = tg3_set_rx_csum,
  9241. .set_tx_csum = tg3_set_tx_csum,
  9242. .set_sg = ethtool_op_set_sg,
  9243. .set_tso = tg3_set_tso,
  9244. .self_test = tg3_self_test,
  9245. .get_strings = tg3_get_strings,
  9246. .phys_id = tg3_phys_id,
  9247. .get_ethtool_stats = tg3_get_ethtool_stats,
  9248. .get_coalesce = tg3_get_coalesce,
  9249. .set_coalesce = tg3_set_coalesce,
  9250. .get_sset_count = tg3_get_sset_count,
  9251. };
  9252. static void __devinit tg3_get_eeprom_size(struct tg3 *tp)
  9253. {
  9254. u32 cursize, val, magic;
  9255. tp->nvram_size = EEPROM_CHIP_SIZE;
  9256. if (tg3_nvram_read(tp, 0, &magic) != 0)
  9257. return;
  9258. if ((magic != TG3_EEPROM_MAGIC) &&
  9259. ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
  9260. ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
  9261. return;
  9262. /*
  9263. * Size the chip by reading offsets at increasing powers of two.
  9264. * When we encounter our validation signature, we know the addressing
  9265. * has wrapped around, and thus have our chip size.
  9266. */
  9267. cursize = 0x10;
  9268. while (cursize < tp->nvram_size) {
  9269. if (tg3_nvram_read(tp, cursize, &val) != 0)
  9270. return;
  9271. if (val == magic)
  9272. break;
  9273. cursize <<= 1;
  9274. }
  9275. tp->nvram_size = cursize;
  9276. }
  9277. static void __devinit tg3_get_nvram_size(struct tg3 *tp)
  9278. {
  9279. u32 val;
  9280. if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
  9281. tg3_nvram_read(tp, 0, &val) != 0)
  9282. return;
  9283. /* Selfboot format */
  9284. if (val != TG3_EEPROM_MAGIC) {
  9285. tg3_get_eeprom_size(tp);
  9286. return;
  9287. }
  9288. if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
  9289. if (val != 0) {
  9290. /* This is confusing. We want to operate on the
  9291. * 16-bit value at offset 0xf2. The tg3_nvram_read()
  9292. * call will read from NVRAM and byteswap the data
  9293. * according to the byteswapping settings for all
  9294. * other register accesses. This ensures the data we
  9295. * want will always reside in the lower 16-bits.
  9296. * However, the data in NVRAM is in LE format, which
  9297. * means the data from the NVRAM read will always be
  9298. * opposite the endianness of the CPU. The 16-bit
  9299. * byteswap then brings the data to CPU endianness.
  9300. */
  9301. tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
  9302. return;
  9303. }
  9304. }
  9305. tp->nvram_size = TG3_NVRAM_SIZE_512KB;
  9306. }
  9307. static void __devinit tg3_get_nvram_info(struct tg3 *tp)
  9308. {
  9309. u32 nvcfg1;
  9310. nvcfg1 = tr32(NVRAM_CFG1);
  9311. if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
  9312. tp->tg3_flags2 |= TG3_FLG2_FLASH;
  9313. } else {
  9314. nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
  9315. tw32(NVRAM_CFG1, nvcfg1);
  9316. }
  9317. if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750) ||
  9318. (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
  9319. switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
  9320. case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
  9321. tp->nvram_jedecnum = JEDEC_ATMEL;
  9322. tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
  9323. tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
  9324. break;
  9325. case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
  9326. tp->nvram_jedecnum = JEDEC_ATMEL;
  9327. tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
  9328. break;
  9329. case FLASH_VENDOR_ATMEL_EEPROM:
  9330. tp->nvram_jedecnum = JEDEC_ATMEL;
  9331. tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
  9332. tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
  9333. break;
  9334. case FLASH_VENDOR_ST:
  9335. tp->nvram_jedecnum = JEDEC_ST;
  9336. tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
  9337. tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
  9338. break;
  9339. case FLASH_VENDOR_SAIFUN:
  9340. tp->nvram_jedecnum = JEDEC_SAIFUN;
  9341. tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
  9342. break;
  9343. case FLASH_VENDOR_SST_SMALL:
  9344. case FLASH_VENDOR_SST_LARGE:
  9345. tp->nvram_jedecnum = JEDEC_SST;
  9346. tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
  9347. break;
  9348. }
  9349. } else {
  9350. tp->nvram_jedecnum = JEDEC_ATMEL;
  9351. tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
  9352. tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
  9353. }
  9354. }
  9355. static void __devinit tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
  9356. {
  9357. switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
  9358. case FLASH_5752PAGE_SIZE_256:
  9359. tp->nvram_pagesize = 256;
  9360. break;
  9361. case FLASH_5752PAGE_SIZE_512:
  9362. tp->nvram_pagesize = 512;
  9363. break;
  9364. case FLASH_5752PAGE_SIZE_1K:
  9365. tp->nvram_pagesize = 1024;
  9366. break;
  9367. case FLASH_5752PAGE_SIZE_2K:
  9368. tp->nvram_pagesize = 2048;
  9369. break;
  9370. case FLASH_5752PAGE_SIZE_4K:
  9371. tp->nvram_pagesize = 4096;
  9372. break;
  9373. case FLASH_5752PAGE_SIZE_264:
  9374. tp->nvram_pagesize = 264;
  9375. break;
  9376. case FLASH_5752PAGE_SIZE_528:
  9377. tp->nvram_pagesize = 528;
  9378. break;
  9379. }
  9380. }
  9381. static void __devinit tg3_get_5752_nvram_info(struct tg3 *tp)
  9382. {
  9383. u32 nvcfg1;
  9384. nvcfg1 = tr32(NVRAM_CFG1);
  9385. /* NVRAM protection for TPM */
  9386. if (nvcfg1 & (1 << 27))
  9387. tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
  9388. switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
  9389. case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
  9390. case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
  9391. tp->nvram_jedecnum = JEDEC_ATMEL;
  9392. tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
  9393. break;
  9394. case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
  9395. tp->nvram_jedecnum = JEDEC_ATMEL;
  9396. tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
  9397. tp->tg3_flags2 |= TG3_FLG2_FLASH;
  9398. break;
  9399. case FLASH_5752VENDOR_ST_M45PE10:
  9400. case FLASH_5752VENDOR_ST_M45PE20:
  9401. case FLASH_5752VENDOR_ST_M45PE40:
  9402. tp->nvram_jedecnum = JEDEC_ST;
  9403. tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
  9404. tp->tg3_flags2 |= TG3_FLG2_FLASH;
  9405. break;
  9406. }
  9407. if (tp->tg3_flags2 & TG3_FLG2_FLASH) {
  9408. tg3_nvram_get_pagesize(tp, nvcfg1);
  9409. } else {
  9410. /* For eeprom, set pagesize to maximum eeprom size */
  9411. tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
  9412. nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
  9413. tw32(NVRAM_CFG1, nvcfg1);
  9414. }
  9415. }
  9416. static void __devinit tg3_get_5755_nvram_info(struct tg3 *tp)
  9417. {
  9418. u32 nvcfg1, protect = 0;
  9419. nvcfg1 = tr32(NVRAM_CFG1);
  9420. /* NVRAM protection for TPM */
  9421. if (nvcfg1 & (1 << 27)) {
  9422. tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
  9423. protect = 1;
  9424. }
  9425. nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
  9426. switch (nvcfg1) {
  9427. case FLASH_5755VENDOR_ATMEL_FLASH_1:
  9428. case FLASH_5755VENDOR_ATMEL_FLASH_2:
  9429. case FLASH_5755VENDOR_ATMEL_FLASH_3:
  9430. case FLASH_5755VENDOR_ATMEL_FLASH_5:
  9431. tp->nvram_jedecnum = JEDEC_ATMEL;
  9432. tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
  9433. tp->tg3_flags2 |= TG3_FLG2_FLASH;
  9434. tp->nvram_pagesize = 264;
  9435. if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
  9436. nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
  9437. tp->nvram_size = (protect ? 0x3e200 :
  9438. TG3_NVRAM_SIZE_512KB);
  9439. else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
  9440. tp->nvram_size = (protect ? 0x1f200 :
  9441. TG3_NVRAM_SIZE_256KB);
  9442. else
  9443. tp->nvram_size = (protect ? 0x1f200 :
  9444. TG3_NVRAM_SIZE_128KB);
  9445. break;
  9446. case FLASH_5752VENDOR_ST_M45PE10:
  9447. case FLASH_5752VENDOR_ST_M45PE20:
  9448. case FLASH_5752VENDOR_ST_M45PE40:
  9449. tp->nvram_jedecnum = JEDEC_ST;
  9450. tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
  9451. tp->tg3_flags2 |= TG3_FLG2_FLASH;
  9452. tp->nvram_pagesize = 256;
  9453. if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
  9454. tp->nvram_size = (protect ?
  9455. TG3_NVRAM_SIZE_64KB :
  9456. TG3_NVRAM_SIZE_128KB);
  9457. else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
  9458. tp->nvram_size = (protect ?
  9459. TG3_NVRAM_SIZE_64KB :
  9460. TG3_NVRAM_SIZE_256KB);
  9461. else
  9462. tp->nvram_size = (protect ?
  9463. TG3_NVRAM_SIZE_128KB :
  9464. TG3_NVRAM_SIZE_512KB);
  9465. break;
  9466. }
  9467. }
  9468. static void __devinit tg3_get_5787_nvram_info(struct tg3 *tp)
  9469. {
  9470. u32 nvcfg1;
  9471. nvcfg1 = tr32(NVRAM_CFG1);
  9472. switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
  9473. case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
  9474. case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
  9475. case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
  9476. case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
  9477. tp->nvram_jedecnum = JEDEC_ATMEL;
  9478. tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
  9479. tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
  9480. nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
  9481. tw32(NVRAM_CFG1, nvcfg1);
  9482. break;
  9483. case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
  9484. case FLASH_5755VENDOR_ATMEL_FLASH_1:
  9485. case FLASH_5755VENDOR_ATMEL_FLASH_2:
  9486. case FLASH_5755VENDOR_ATMEL_FLASH_3:
  9487. tp->nvram_jedecnum = JEDEC_ATMEL;
  9488. tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
  9489. tp->tg3_flags2 |= TG3_FLG2_FLASH;
  9490. tp->nvram_pagesize = 264;
  9491. break;
  9492. case FLASH_5752VENDOR_ST_M45PE10:
  9493. case FLASH_5752VENDOR_ST_M45PE20:
  9494. case FLASH_5752VENDOR_ST_M45PE40:
  9495. tp->nvram_jedecnum = JEDEC_ST;
  9496. tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
  9497. tp->tg3_flags2 |= TG3_FLG2_FLASH;
  9498. tp->nvram_pagesize = 256;
  9499. break;
  9500. }
  9501. }
  9502. static void __devinit tg3_get_5761_nvram_info(struct tg3 *tp)
  9503. {
  9504. u32 nvcfg1, protect = 0;
  9505. nvcfg1 = tr32(NVRAM_CFG1);
  9506. /* NVRAM protection for TPM */
  9507. if (nvcfg1 & (1 << 27)) {
  9508. tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
  9509. protect = 1;
  9510. }
  9511. nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
  9512. switch (nvcfg1) {
  9513. case FLASH_5761VENDOR_ATMEL_ADB021D:
  9514. case FLASH_5761VENDOR_ATMEL_ADB041D:
  9515. case FLASH_5761VENDOR_ATMEL_ADB081D:
  9516. case FLASH_5761VENDOR_ATMEL_ADB161D:
  9517. case FLASH_5761VENDOR_ATMEL_MDB021D:
  9518. case FLASH_5761VENDOR_ATMEL_MDB041D:
  9519. case FLASH_5761VENDOR_ATMEL_MDB081D:
  9520. case FLASH_5761VENDOR_ATMEL_MDB161D:
  9521. tp->nvram_jedecnum = JEDEC_ATMEL;
  9522. tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
  9523. tp->tg3_flags2 |= TG3_FLG2_FLASH;
  9524. tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
  9525. tp->nvram_pagesize = 256;
  9526. break;
  9527. case FLASH_5761VENDOR_ST_A_M45PE20:
  9528. case FLASH_5761VENDOR_ST_A_M45PE40:
  9529. case FLASH_5761VENDOR_ST_A_M45PE80:
  9530. case FLASH_5761VENDOR_ST_A_M45PE16:
  9531. case FLASH_5761VENDOR_ST_M_M45PE20:
  9532. case FLASH_5761VENDOR_ST_M_M45PE40:
  9533. case FLASH_5761VENDOR_ST_M_M45PE80:
  9534. case FLASH_5761VENDOR_ST_M_M45PE16:
  9535. tp->nvram_jedecnum = JEDEC_ST;
  9536. tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
  9537. tp->tg3_flags2 |= TG3_FLG2_FLASH;
  9538. tp->nvram_pagesize = 256;
  9539. break;
  9540. }
  9541. if (protect) {
  9542. tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
  9543. } else {
  9544. switch (nvcfg1) {
  9545. case FLASH_5761VENDOR_ATMEL_ADB161D:
  9546. case FLASH_5761VENDOR_ATMEL_MDB161D:
  9547. case FLASH_5761VENDOR_ST_A_M45PE16:
  9548. case FLASH_5761VENDOR_ST_M_M45PE16:
  9549. tp->nvram_size = TG3_NVRAM_SIZE_2MB;
  9550. break;
  9551. case FLASH_5761VENDOR_ATMEL_ADB081D:
  9552. case FLASH_5761VENDOR_ATMEL_MDB081D:
  9553. case FLASH_5761VENDOR_ST_A_M45PE80:
  9554. case FLASH_5761VENDOR_ST_M_M45PE80:
  9555. tp->nvram_size = TG3_NVRAM_SIZE_1MB;
  9556. break;
  9557. case FLASH_5761VENDOR_ATMEL_ADB041D:
  9558. case FLASH_5761VENDOR_ATMEL_MDB041D:
  9559. case FLASH_5761VENDOR_ST_A_M45PE40:
  9560. case FLASH_5761VENDOR_ST_M_M45PE40:
  9561. tp->nvram_size = TG3_NVRAM_SIZE_512KB;
  9562. break;
  9563. case FLASH_5761VENDOR_ATMEL_ADB021D:
  9564. case FLASH_5761VENDOR_ATMEL_MDB021D:
  9565. case FLASH_5761VENDOR_ST_A_M45PE20:
  9566. case FLASH_5761VENDOR_ST_M_M45PE20:
  9567. tp->nvram_size = TG3_NVRAM_SIZE_256KB;
  9568. break;
  9569. }
  9570. }
  9571. }
  9572. static void __devinit tg3_get_5906_nvram_info(struct tg3 *tp)
  9573. {
  9574. tp->nvram_jedecnum = JEDEC_ATMEL;
  9575. tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
  9576. tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
  9577. }
  9578. static void __devinit tg3_get_57780_nvram_info(struct tg3 *tp)
  9579. {
  9580. u32 nvcfg1;
  9581. nvcfg1 = tr32(NVRAM_CFG1);
  9582. switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
  9583. case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
  9584. case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
  9585. tp->nvram_jedecnum = JEDEC_ATMEL;
  9586. tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
  9587. tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
  9588. nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
  9589. tw32(NVRAM_CFG1, nvcfg1);
  9590. return;
  9591. case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
  9592. case FLASH_57780VENDOR_ATMEL_AT45DB011D:
  9593. case FLASH_57780VENDOR_ATMEL_AT45DB011B:
  9594. case FLASH_57780VENDOR_ATMEL_AT45DB021D:
  9595. case FLASH_57780VENDOR_ATMEL_AT45DB021B:
  9596. case FLASH_57780VENDOR_ATMEL_AT45DB041D:
  9597. case FLASH_57780VENDOR_ATMEL_AT45DB041B:
  9598. tp->nvram_jedecnum = JEDEC_ATMEL;
  9599. tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
  9600. tp->tg3_flags2 |= TG3_FLG2_FLASH;
  9601. switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
  9602. case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
  9603. case FLASH_57780VENDOR_ATMEL_AT45DB011D:
  9604. case FLASH_57780VENDOR_ATMEL_AT45DB011B:
  9605. tp->nvram_size = TG3_NVRAM_SIZE_128KB;
  9606. break;
  9607. case FLASH_57780VENDOR_ATMEL_AT45DB021D:
  9608. case FLASH_57780VENDOR_ATMEL_AT45DB021B:
  9609. tp->nvram_size = TG3_NVRAM_SIZE_256KB;
  9610. break;
  9611. case FLASH_57780VENDOR_ATMEL_AT45DB041D:
  9612. case FLASH_57780VENDOR_ATMEL_AT45DB041B:
  9613. tp->nvram_size = TG3_NVRAM_SIZE_512KB;
  9614. break;
  9615. }
  9616. break;
  9617. case FLASH_5752VENDOR_ST_M45PE10:
  9618. case FLASH_5752VENDOR_ST_M45PE20:
  9619. case FLASH_5752VENDOR_ST_M45PE40:
  9620. tp->nvram_jedecnum = JEDEC_ST;
  9621. tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
  9622. tp->tg3_flags2 |= TG3_FLG2_FLASH;
  9623. switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
  9624. case FLASH_5752VENDOR_ST_M45PE10:
  9625. tp->nvram_size = TG3_NVRAM_SIZE_128KB;
  9626. break;
  9627. case FLASH_5752VENDOR_ST_M45PE20:
  9628. tp->nvram_size = TG3_NVRAM_SIZE_256KB;
  9629. break;
  9630. case FLASH_5752VENDOR_ST_M45PE40:
  9631. tp->nvram_size = TG3_NVRAM_SIZE_512KB;
  9632. break;
  9633. }
  9634. break;
  9635. default:
  9636. tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM;
  9637. return;
  9638. }
  9639. tg3_nvram_get_pagesize(tp, nvcfg1);
  9640. if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
  9641. tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
  9642. }
  9643. static void __devinit tg3_get_5717_nvram_info(struct tg3 *tp)
  9644. {
  9645. u32 nvcfg1;
  9646. nvcfg1 = tr32(NVRAM_CFG1);
  9647. switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
  9648. case FLASH_5717VENDOR_ATMEL_EEPROM:
  9649. case FLASH_5717VENDOR_MICRO_EEPROM:
  9650. tp->nvram_jedecnum = JEDEC_ATMEL;
  9651. tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
  9652. tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
  9653. nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
  9654. tw32(NVRAM_CFG1, nvcfg1);
  9655. return;
  9656. case FLASH_5717VENDOR_ATMEL_MDB011D:
  9657. case FLASH_5717VENDOR_ATMEL_ADB011B:
  9658. case FLASH_5717VENDOR_ATMEL_ADB011D:
  9659. case FLASH_5717VENDOR_ATMEL_MDB021D:
  9660. case FLASH_5717VENDOR_ATMEL_ADB021B:
  9661. case FLASH_5717VENDOR_ATMEL_ADB021D:
  9662. case FLASH_5717VENDOR_ATMEL_45USPT:
  9663. tp->nvram_jedecnum = JEDEC_ATMEL;
  9664. tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
  9665. tp->tg3_flags2 |= TG3_FLG2_FLASH;
  9666. switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
  9667. case FLASH_5717VENDOR_ATMEL_MDB021D:
  9668. case FLASH_5717VENDOR_ATMEL_ADB021B:
  9669. case FLASH_5717VENDOR_ATMEL_ADB021D:
  9670. tp->nvram_size = TG3_NVRAM_SIZE_256KB;
  9671. break;
  9672. default:
  9673. tp->nvram_size = TG3_NVRAM_SIZE_128KB;
  9674. break;
  9675. }
  9676. break;
  9677. case FLASH_5717VENDOR_ST_M_M25PE10:
  9678. case FLASH_5717VENDOR_ST_A_M25PE10:
  9679. case FLASH_5717VENDOR_ST_M_M45PE10:
  9680. case FLASH_5717VENDOR_ST_A_M45PE10:
  9681. case FLASH_5717VENDOR_ST_M_M25PE20:
  9682. case FLASH_5717VENDOR_ST_A_M25PE20:
  9683. case FLASH_5717VENDOR_ST_M_M45PE20:
  9684. case FLASH_5717VENDOR_ST_A_M45PE20:
  9685. case FLASH_5717VENDOR_ST_25USPT:
  9686. case FLASH_5717VENDOR_ST_45USPT:
  9687. tp->nvram_jedecnum = JEDEC_ST;
  9688. tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
  9689. tp->tg3_flags2 |= TG3_FLG2_FLASH;
  9690. switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
  9691. case FLASH_5717VENDOR_ST_M_M25PE20:
  9692. case FLASH_5717VENDOR_ST_A_M25PE20:
  9693. case FLASH_5717VENDOR_ST_M_M45PE20:
  9694. case FLASH_5717VENDOR_ST_A_M45PE20:
  9695. tp->nvram_size = TG3_NVRAM_SIZE_256KB;
  9696. break;
  9697. default:
  9698. tp->nvram_size = TG3_NVRAM_SIZE_128KB;
  9699. break;
  9700. }
  9701. break;
  9702. default:
  9703. tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM;
  9704. return;
  9705. }
  9706. tg3_nvram_get_pagesize(tp, nvcfg1);
  9707. if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
  9708. tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
  9709. }
  9710. /* Chips other than 5700/5701 use the NVRAM for fetching info. */
  9711. static void __devinit tg3_nvram_init(struct tg3 *tp)
  9712. {
  9713. tw32_f(GRC_EEPROM_ADDR,
  9714. (EEPROM_ADDR_FSM_RESET |
  9715. (EEPROM_DEFAULT_CLOCK_PERIOD <<
  9716. EEPROM_ADDR_CLKPERD_SHIFT)));
  9717. msleep(1);
  9718. /* Enable seeprom accesses. */
  9719. tw32_f(GRC_LOCAL_CTRL,
  9720. tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
  9721. udelay(100);
  9722. if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
  9723. GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
  9724. tp->tg3_flags |= TG3_FLAG_NVRAM;
  9725. if (tg3_nvram_lock(tp)) {
  9726. netdev_warn(tp->dev,
  9727. "Cannot get nvram lock, %s failed\n",
  9728. __func__);
  9729. return;
  9730. }
  9731. tg3_enable_nvram_access(tp);
  9732. tp->nvram_size = 0;
  9733. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
  9734. tg3_get_5752_nvram_info(tp);
  9735. else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
  9736. tg3_get_5755_nvram_info(tp);
  9737. else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
  9738. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
  9739. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
  9740. tg3_get_5787_nvram_info(tp);
  9741. else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
  9742. tg3_get_5761_nvram_info(tp);
  9743. else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
  9744. tg3_get_5906_nvram_info(tp);
  9745. else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
  9746. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
  9747. tg3_get_57780_nvram_info(tp);
  9748. else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
  9749. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
  9750. tg3_get_5717_nvram_info(tp);
  9751. else
  9752. tg3_get_nvram_info(tp);
  9753. if (tp->nvram_size == 0)
  9754. tg3_get_nvram_size(tp);
  9755. tg3_disable_nvram_access(tp);
  9756. tg3_nvram_unlock(tp);
  9757. } else {
  9758. tp->tg3_flags &= ~(TG3_FLAG_NVRAM | TG3_FLAG_NVRAM_BUFFERED);
  9759. tg3_get_eeprom_size(tp);
  9760. }
  9761. }
  9762. static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
  9763. u32 offset, u32 len, u8 *buf)
  9764. {
  9765. int i, j, rc = 0;
  9766. u32 val;
  9767. for (i = 0; i < len; i += 4) {
  9768. u32 addr;
  9769. __be32 data;
  9770. addr = offset + i;
  9771. memcpy(&data, buf + i, 4);
  9772. /*
  9773. * The SEEPROM interface expects the data to always be opposite
  9774. * the native endian format. We accomplish this by reversing
  9775. * all the operations that would have been performed on the
  9776. * data from a call to tg3_nvram_read_be32().
  9777. */
  9778. tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
  9779. val = tr32(GRC_EEPROM_ADDR);
  9780. tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
  9781. val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
  9782. EEPROM_ADDR_READ);
  9783. tw32(GRC_EEPROM_ADDR, val |
  9784. (0 << EEPROM_ADDR_DEVID_SHIFT) |
  9785. (addr & EEPROM_ADDR_ADDR_MASK) |
  9786. EEPROM_ADDR_START |
  9787. EEPROM_ADDR_WRITE);
  9788. for (j = 0; j < 1000; j++) {
  9789. val = tr32(GRC_EEPROM_ADDR);
  9790. if (val & EEPROM_ADDR_COMPLETE)
  9791. break;
  9792. msleep(1);
  9793. }
  9794. if (!(val & EEPROM_ADDR_COMPLETE)) {
  9795. rc = -EBUSY;
  9796. break;
  9797. }
  9798. }
  9799. return rc;
  9800. }
  9801. /* offset and length are dword aligned */
  9802. static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
  9803. u8 *buf)
  9804. {
  9805. int ret = 0;
  9806. u32 pagesize = tp->nvram_pagesize;
  9807. u32 pagemask = pagesize - 1;
  9808. u32 nvram_cmd;
  9809. u8 *tmp;
  9810. tmp = kmalloc(pagesize, GFP_KERNEL);
  9811. if (tmp == NULL)
  9812. return -ENOMEM;
  9813. while (len) {
  9814. int j;
  9815. u32 phy_addr, page_off, size;
  9816. phy_addr = offset & ~pagemask;
  9817. for (j = 0; j < pagesize; j += 4) {
  9818. ret = tg3_nvram_read_be32(tp, phy_addr + j,
  9819. (__be32 *) (tmp + j));
  9820. if (ret)
  9821. break;
  9822. }
  9823. if (ret)
  9824. break;
  9825. page_off = offset & pagemask;
  9826. size = pagesize;
  9827. if (len < size)
  9828. size = len;
  9829. len -= size;
  9830. memcpy(tmp + page_off, buf, size);
  9831. offset = offset + (pagesize - page_off);
  9832. tg3_enable_nvram_access(tp);
  9833. /*
  9834. * Before we can erase the flash page, we need
  9835. * to issue a special "write enable" command.
  9836. */
  9837. nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
  9838. if (tg3_nvram_exec_cmd(tp, nvram_cmd))
  9839. break;
  9840. /* Erase the target page */
  9841. tw32(NVRAM_ADDR, phy_addr);
  9842. nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
  9843. NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
  9844. if (tg3_nvram_exec_cmd(tp, nvram_cmd))
  9845. break;
  9846. /* Issue another write enable to start the write. */
  9847. nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
  9848. if (tg3_nvram_exec_cmd(tp, nvram_cmd))
  9849. break;
  9850. for (j = 0; j < pagesize; j += 4) {
  9851. __be32 data;
  9852. data = *((__be32 *) (tmp + j));
  9853. tw32(NVRAM_WRDATA, be32_to_cpu(data));
  9854. tw32(NVRAM_ADDR, phy_addr + j);
  9855. nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
  9856. NVRAM_CMD_WR;
  9857. if (j == 0)
  9858. nvram_cmd |= NVRAM_CMD_FIRST;
  9859. else if (j == (pagesize - 4))
  9860. nvram_cmd |= NVRAM_CMD_LAST;
  9861. if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
  9862. break;
  9863. }
  9864. if (ret)
  9865. break;
  9866. }
  9867. nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
  9868. tg3_nvram_exec_cmd(tp, nvram_cmd);
  9869. kfree(tmp);
  9870. return ret;
  9871. }
  9872. /* offset and length are dword aligned */
  9873. static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
  9874. u8 *buf)
  9875. {
  9876. int i, ret = 0;
  9877. for (i = 0; i < len; i += 4, offset += 4) {
  9878. u32 page_off, phy_addr, nvram_cmd;
  9879. __be32 data;
  9880. memcpy(&data, buf + i, 4);
  9881. tw32(NVRAM_WRDATA, be32_to_cpu(data));
  9882. page_off = offset % tp->nvram_pagesize;
  9883. phy_addr = tg3_nvram_phys_addr(tp, offset);
  9884. tw32(NVRAM_ADDR, phy_addr);
  9885. nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
  9886. if (page_off == 0 || i == 0)
  9887. nvram_cmd |= NVRAM_CMD_FIRST;
  9888. if (page_off == (tp->nvram_pagesize - 4))
  9889. nvram_cmd |= NVRAM_CMD_LAST;
  9890. if (i == (len - 4))
  9891. nvram_cmd |= NVRAM_CMD_LAST;
  9892. if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
  9893. !(tp->tg3_flags3 & TG3_FLG3_5755_PLUS) &&
  9894. (tp->nvram_jedecnum == JEDEC_ST) &&
  9895. (nvram_cmd & NVRAM_CMD_FIRST)) {
  9896. if ((ret = tg3_nvram_exec_cmd(tp,
  9897. NVRAM_CMD_WREN | NVRAM_CMD_GO |
  9898. NVRAM_CMD_DONE)))
  9899. break;
  9900. }
  9901. if (!(tp->tg3_flags2 & TG3_FLG2_FLASH)) {
  9902. /* We always do complete word writes to eeprom. */
  9903. nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
  9904. }
  9905. if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
  9906. break;
  9907. }
  9908. return ret;
  9909. }
  9910. /* offset and length are dword aligned */
  9911. static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
  9912. {
  9913. int ret;
  9914. if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT) {
  9915. tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
  9916. ~GRC_LCLCTRL_GPIO_OUTPUT1);
  9917. udelay(40);
  9918. }
  9919. if (!(tp->tg3_flags & TG3_FLAG_NVRAM)) {
  9920. ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
  9921. } else {
  9922. u32 grc_mode;
  9923. ret = tg3_nvram_lock(tp);
  9924. if (ret)
  9925. return ret;
  9926. tg3_enable_nvram_access(tp);
  9927. if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
  9928. !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM))
  9929. tw32(NVRAM_WRITE1, 0x406);
  9930. grc_mode = tr32(GRC_MODE);
  9931. tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
  9932. if ((tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) ||
  9933. !(tp->tg3_flags2 & TG3_FLG2_FLASH)) {
  9934. ret = tg3_nvram_write_block_buffered(tp, offset, len,
  9935. buf);
  9936. } else {
  9937. ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
  9938. buf);
  9939. }
  9940. grc_mode = tr32(GRC_MODE);
  9941. tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
  9942. tg3_disable_nvram_access(tp);
  9943. tg3_nvram_unlock(tp);
  9944. }
  9945. if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT) {
  9946. tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
  9947. udelay(40);
  9948. }
  9949. return ret;
  9950. }
  9951. struct subsys_tbl_ent {
  9952. u16 subsys_vendor, subsys_devid;
  9953. u32 phy_id;
  9954. };
  9955. static struct subsys_tbl_ent subsys_id_to_phy_id[] __devinitdata = {
  9956. /* Broadcom boards. */
  9957. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  9958. TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
  9959. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  9960. TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
  9961. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  9962. TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
  9963. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  9964. TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
  9965. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  9966. TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
  9967. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  9968. TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
  9969. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  9970. TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
  9971. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  9972. TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
  9973. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  9974. TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
  9975. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  9976. TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
  9977. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  9978. TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
  9979. /* 3com boards. */
  9980. { TG3PCI_SUBVENDOR_ID_3COM,
  9981. TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
  9982. { TG3PCI_SUBVENDOR_ID_3COM,
  9983. TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
  9984. { TG3PCI_SUBVENDOR_ID_3COM,
  9985. TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
  9986. { TG3PCI_SUBVENDOR_ID_3COM,
  9987. TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
  9988. { TG3PCI_SUBVENDOR_ID_3COM,
  9989. TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
  9990. /* DELL boards. */
  9991. { TG3PCI_SUBVENDOR_ID_DELL,
  9992. TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
  9993. { TG3PCI_SUBVENDOR_ID_DELL,
  9994. TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
  9995. { TG3PCI_SUBVENDOR_ID_DELL,
  9996. TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
  9997. { TG3PCI_SUBVENDOR_ID_DELL,
  9998. TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
  9999. /* Compaq boards. */
  10000. { TG3PCI_SUBVENDOR_ID_COMPAQ,
  10001. TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
  10002. { TG3PCI_SUBVENDOR_ID_COMPAQ,
  10003. TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
  10004. { TG3PCI_SUBVENDOR_ID_COMPAQ,
  10005. TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
  10006. { TG3PCI_SUBVENDOR_ID_COMPAQ,
  10007. TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
  10008. { TG3PCI_SUBVENDOR_ID_COMPAQ,
  10009. TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
  10010. /* IBM boards. */
  10011. { TG3PCI_SUBVENDOR_ID_IBM,
  10012. TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
  10013. };
  10014. static struct subsys_tbl_ent * __devinit tg3_lookup_by_subsys(struct tg3 *tp)
  10015. {
  10016. int i;
  10017. for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
  10018. if ((subsys_id_to_phy_id[i].subsys_vendor ==
  10019. tp->pdev->subsystem_vendor) &&
  10020. (subsys_id_to_phy_id[i].subsys_devid ==
  10021. tp->pdev->subsystem_device))
  10022. return &subsys_id_to_phy_id[i];
  10023. }
  10024. return NULL;
  10025. }
  10026. static void __devinit tg3_get_eeprom_hw_cfg(struct tg3 *tp)
  10027. {
  10028. u32 val;
  10029. u16 pmcsr;
  10030. /* On some early chips the SRAM cannot be accessed in D3hot state,
  10031. * so need make sure we're in D0.
  10032. */
  10033. pci_read_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, &pmcsr);
  10034. pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
  10035. pci_write_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, pmcsr);
  10036. msleep(1);
  10037. /* Make sure register accesses (indirect or otherwise)
  10038. * will function correctly.
  10039. */
  10040. pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
  10041. tp->misc_host_ctrl);
  10042. /* The memory arbiter has to be enabled in order for SRAM accesses
  10043. * to succeed. Normally on powerup the tg3 chip firmware will make
  10044. * sure it is enabled, but other entities such as system netboot
  10045. * code might disable it.
  10046. */
  10047. val = tr32(MEMARB_MODE);
  10048. tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
  10049. tp->phy_id = TG3_PHY_ID_INVALID;
  10050. tp->led_ctrl = LED_CTRL_MODE_PHY_1;
  10051. /* Assume an onboard device and WOL capable by default. */
  10052. tp->tg3_flags |= TG3_FLAG_EEPROM_WRITE_PROT | TG3_FLAG_WOL_CAP;
  10053. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
  10054. if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
  10055. tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
  10056. tp->tg3_flags2 |= TG3_FLG2_IS_NIC;
  10057. }
  10058. val = tr32(VCPU_CFGSHDW);
  10059. if (val & VCPU_CFGSHDW_ASPM_DBNC)
  10060. tp->tg3_flags |= TG3_FLAG_ASPM_WORKAROUND;
  10061. if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
  10062. (val & VCPU_CFGSHDW_WOL_MAGPKT))
  10063. tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
  10064. goto done;
  10065. }
  10066. tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
  10067. if (val == NIC_SRAM_DATA_SIG_MAGIC) {
  10068. u32 nic_cfg, led_cfg;
  10069. u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
  10070. int eeprom_phy_serdes = 0;
  10071. tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
  10072. tp->nic_sram_data_cfg = nic_cfg;
  10073. tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
  10074. ver >>= NIC_SRAM_DATA_VER_SHIFT;
  10075. if ((GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700) &&
  10076. (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) &&
  10077. (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703) &&
  10078. (ver > 0) && (ver < 0x100))
  10079. tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
  10080. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
  10081. tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
  10082. if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
  10083. NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
  10084. eeprom_phy_serdes = 1;
  10085. tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
  10086. if (nic_phy_id != 0) {
  10087. u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
  10088. u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
  10089. eeprom_phy_id = (id1 >> 16) << 10;
  10090. eeprom_phy_id |= (id2 & 0xfc00) << 16;
  10091. eeprom_phy_id |= (id2 & 0x03ff) << 0;
  10092. } else
  10093. eeprom_phy_id = 0;
  10094. tp->phy_id = eeprom_phy_id;
  10095. if (eeprom_phy_serdes) {
  10096. if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
  10097. tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
  10098. else
  10099. tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
  10100. }
  10101. if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
  10102. led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
  10103. SHASTA_EXT_LED_MODE_MASK);
  10104. else
  10105. led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
  10106. switch (led_cfg) {
  10107. default:
  10108. case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
  10109. tp->led_ctrl = LED_CTRL_MODE_PHY_1;
  10110. break;
  10111. case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
  10112. tp->led_ctrl = LED_CTRL_MODE_PHY_2;
  10113. break;
  10114. case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
  10115. tp->led_ctrl = LED_CTRL_MODE_MAC;
  10116. /* Default to PHY_1_MODE if 0 (MAC_MODE) is
  10117. * read on some older 5700/5701 bootcode.
  10118. */
  10119. if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
  10120. ASIC_REV_5700 ||
  10121. GET_ASIC_REV(tp->pci_chip_rev_id) ==
  10122. ASIC_REV_5701)
  10123. tp->led_ctrl = LED_CTRL_MODE_PHY_1;
  10124. break;
  10125. case SHASTA_EXT_LED_SHARED:
  10126. tp->led_ctrl = LED_CTRL_MODE_SHARED;
  10127. if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
  10128. tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
  10129. tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
  10130. LED_CTRL_MODE_PHY_2);
  10131. break;
  10132. case SHASTA_EXT_LED_MAC:
  10133. tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
  10134. break;
  10135. case SHASTA_EXT_LED_COMBO:
  10136. tp->led_ctrl = LED_CTRL_MODE_COMBO;
  10137. if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
  10138. tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
  10139. LED_CTRL_MODE_PHY_2);
  10140. break;
  10141. }
  10142. if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
  10143. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
  10144. tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
  10145. tp->led_ctrl = LED_CTRL_MODE_PHY_2;
  10146. if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
  10147. tp->led_ctrl = LED_CTRL_MODE_PHY_1;
  10148. if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
  10149. tp->tg3_flags |= TG3_FLAG_EEPROM_WRITE_PROT;
  10150. if ((tp->pdev->subsystem_vendor ==
  10151. PCI_VENDOR_ID_ARIMA) &&
  10152. (tp->pdev->subsystem_device == 0x205a ||
  10153. tp->pdev->subsystem_device == 0x2063))
  10154. tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
  10155. } else {
  10156. tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
  10157. tp->tg3_flags2 |= TG3_FLG2_IS_NIC;
  10158. }
  10159. if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
  10160. tp->tg3_flags |= TG3_FLAG_ENABLE_ASF;
  10161. if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
  10162. tp->tg3_flags2 |= TG3_FLG2_ASF_NEW_HANDSHAKE;
  10163. }
  10164. if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
  10165. (tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
  10166. tp->tg3_flags3 |= TG3_FLG3_ENABLE_APE;
  10167. if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
  10168. !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
  10169. tp->tg3_flags &= ~TG3_FLAG_WOL_CAP;
  10170. if ((tp->tg3_flags & TG3_FLAG_WOL_CAP) &&
  10171. (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE))
  10172. tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
  10173. if (cfg2 & (1 << 17))
  10174. tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
  10175. /* serdes signal pre-emphasis in register 0x590 set by */
  10176. /* bootcode if bit 18 is set */
  10177. if (cfg2 & (1 << 18))
  10178. tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
  10179. if (((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
  10180. GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX)) &&
  10181. (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
  10182. tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
  10183. if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
  10184. GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
  10185. !(tp->tg3_flags3 & TG3_FLG3_5717_PLUS)) {
  10186. u32 cfg3;
  10187. tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
  10188. if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
  10189. tp->tg3_flags |= TG3_FLAG_ASPM_WORKAROUND;
  10190. }
  10191. if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
  10192. tp->tg3_flags3 |= TG3_FLG3_RGMII_INBAND_DISABLE;
  10193. if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
  10194. tp->tg3_flags3 |= TG3_FLG3_RGMII_EXT_IBND_RX_EN;
  10195. if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
  10196. tp->tg3_flags3 |= TG3_FLG3_RGMII_EXT_IBND_TX_EN;
  10197. }
  10198. done:
  10199. device_init_wakeup(&tp->pdev->dev, tp->tg3_flags & TG3_FLAG_WOL_CAP);
  10200. device_set_wakeup_enable(&tp->pdev->dev,
  10201. tp->tg3_flags & TG3_FLAG_WOL_ENABLE);
  10202. }
  10203. static int __devinit tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
  10204. {
  10205. int i;
  10206. u32 val;
  10207. tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
  10208. tw32(OTP_CTRL, cmd);
  10209. /* Wait for up to 1 ms for command to execute. */
  10210. for (i = 0; i < 100; i++) {
  10211. val = tr32(OTP_STATUS);
  10212. if (val & OTP_STATUS_CMD_DONE)
  10213. break;
  10214. udelay(10);
  10215. }
  10216. return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
  10217. }
  10218. /* Read the gphy configuration from the OTP region of the chip. The gphy
  10219. * configuration is a 32-bit value that straddles the alignment boundary.
  10220. * We do two 32-bit reads and then shift and merge the results.
  10221. */
  10222. static u32 __devinit tg3_read_otp_phycfg(struct tg3 *tp)
  10223. {
  10224. u32 bhalf_otp, thalf_otp;
  10225. tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
  10226. if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
  10227. return 0;
  10228. tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
  10229. if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
  10230. return 0;
  10231. thalf_otp = tr32(OTP_READ_DATA);
  10232. tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
  10233. if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
  10234. return 0;
  10235. bhalf_otp = tr32(OTP_READ_DATA);
  10236. return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
  10237. }
  10238. static int __devinit tg3_phy_probe(struct tg3 *tp)
  10239. {
  10240. u32 hw_phy_id_1, hw_phy_id_2;
  10241. u32 hw_phy_id, hw_phy_id_masked;
  10242. int err;
  10243. if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)
  10244. return tg3_phy_init(tp);
  10245. /* Reading the PHY ID register can conflict with ASF
  10246. * firmware access to the PHY hardware.
  10247. */
  10248. err = 0;
  10249. if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
  10250. (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
  10251. hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
  10252. } else {
  10253. /* Now read the physical PHY_ID from the chip and verify
  10254. * that it is sane. If it doesn't look good, we fall back
  10255. * to either the hard-coded table based PHY_ID and failing
  10256. * that the value found in the eeprom area.
  10257. */
  10258. err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
  10259. err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
  10260. hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
  10261. hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
  10262. hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
  10263. hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
  10264. }
  10265. if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
  10266. tp->phy_id = hw_phy_id;
  10267. if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
  10268. tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
  10269. else
  10270. tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
  10271. } else {
  10272. if (tp->phy_id != TG3_PHY_ID_INVALID) {
  10273. /* Do nothing, phy ID already set up in
  10274. * tg3_get_eeprom_hw_cfg().
  10275. */
  10276. } else {
  10277. struct subsys_tbl_ent *p;
  10278. /* No eeprom signature? Try the hardcoded
  10279. * subsys device table.
  10280. */
  10281. p = tg3_lookup_by_subsys(tp);
  10282. if (!p)
  10283. return -ENODEV;
  10284. tp->phy_id = p->phy_id;
  10285. if (!tp->phy_id ||
  10286. tp->phy_id == TG3_PHY_ID_BCM8002)
  10287. tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
  10288. }
  10289. }
  10290. if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
  10291. !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) &&
  10292. !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
  10293. u32 bmsr, adv_reg, tg3_ctrl, mask;
  10294. tg3_readphy(tp, MII_BMSR, &bmsr);
  10295. if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
  10296. (bmsr & BMSR_LSTATUS))
  10297. goto skip_phy_reset;
  10298. err = tg3_phy_reset(tp);
  10299. if (err)
  10300. return err;
  10301. adv_reg = (ADVERTISE_10HALF | ADVERTISE_10FULL |
  10302. ADVERTISE_100HALF | ADVERTISE_100FULL |
  10303. ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
  10304. tg3_ctrl = 0;
  10305. if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
  10306. tg3_ctrl = (MII_TG3_CTRL_ADV_1000_HALF |
  10307. MII_TG3_CTRL_ADV_1000_FULL);
  10308. if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
  10309. tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
  10310. tg3_ctrl |= (MII_TG3_CTRL_AS_MASTER |
  10311. MII_TG3_CTRL_ENABLE_AS_MASTER);
  10312. }
  10313. mask = (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
  10314. ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
  10315. ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full);
  10316. if (!tg3_copper_is_advertising_all(tp, mask)) {
  10317. tg3_writephy(tp, MII_ADVERTISE, adv_reg);
  10318. if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
  10319. tg3_writephy(tp, MII_TG3_CTRL, tg3_ctrl);
  10320. tg3_writephy(tp, MII_BMCR,
  10321. BMCR_ANENABLE | BMCR_ANRESTART);
  10322. }
  10323. tg3_phy_set_wirespeed(tp);
  10324. tg3_writephy(tp, MII_ADVERTISE, adv_reg);
  10325. if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
  10326. tg3_writephy(tp, MII_TG3_CTRL, tg3_ctrl);
  10327. }
  10328. skip_phy_reset:
  10329. if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
  10330. err = tg3_init_5401phy_dsp(tp);
  10331. if (err)
  10332. return err;
  10333. err = tg3_init_5401phy_dsp(tp);
  10334. }
  10335. if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
  10336. tp->link_config.advertising =
  10337. (ADVERTISED_1000baseT_Half |
  10338. ADVERTISED_1000baseT_Full |
  10339. ADVERTISED_Autoneg |
  10340. ADVERTISED_FIBRE);
  10341. if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
  10342. tp->link_config.advertising &=
  10343. ~(ADVERTISED_1000baseT_Half |
  10344. ADVERTISED_1000baseT_Full);
  10345. return err;
  10346. }
  10347. static void __devinit tg3_read_vpd(struct tg3 *tp)
  10348. {
  10349. u8 *vpd_data;
  10350. unsigned int block_end, rosize, len;
  10351. int j, i = 0;
  10352. u32 magic;
  10353. if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
  10354. tg3_nvram_read(tp, 0x0, &magic))
  10355. goto out_no_vpd;
  10356. vpd_data = kmalloc(TG3_NVM_VPD_LEN, GFP_KERNEL);
  10357. if (!vpd_data)
  10358. goto out_no_vpd;
  10359. if (magic == TG3_EEPROM_MAGIC) {
  10360. for (i = 0; i < TG3_NVM_VPD_LEN; i += 4) {
  10361. u32 tmp;
  10362. /* The data is in little-endian format in NVRAM.
  10363. * Use the big-endian read routines to preserve
  10364. * the byte order as it exists in NVRAM.
  10365. */
  10366. if (tg3_nvram_read_be32(tp, TG3_NVM_VPD_OFF + i, &tmp))
  10367. goto out_not_found;
  10368. memcpy(&vpd_data[i], &tmp, sizeof(tmp));
  10369. }
  10370. } else {
  10371. ssize_t cnt;
  10372. unsigned int pos = 0;
  10373. for (; pos < TG3_NVM_VPD_LEN && i < 3; i++, pos += cnt) {
  10374. cnt = pci_read_vpd(tp->pdev, pos,
  10375. TG3_NVM_VPD_LEN - pos,
  10376. &vpd_data[pos]);
  10377. if (cnt == -ETIMEDOUT || -EINTR)
  10378. cnt = 0;
  10379. else if (cnt < 0)
  10380. goto out_not_found;
  10381. }
  10382. if (pos != TG3_NVM_VPD_LEN)
  10383. goto out_not_found;
  10384. }
  10385. i = pci_vpd_find_tag(vpd_data, 0, TG3_NVM_VPD_LEN,
  10386. PCI_VPD_LRDT_RO_DATA);
  10387. if (i < 0)
  10388. goto out_not_found;
  10389. rosize = pci_vpd_lrdt_size(&vpd_data[i]);
  10390. block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
  10391. i += PCI_VPD_LRDT_TAG_SIZE;
  10392. if (block_end > TG3_NVM_VPD_LEN)
  10393. goto out_not_found;
  10394. j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
  10395. PCI_VPD_RO_KEYWORD_MFR_ID);
  10396. if (j > 0) {
  10397. len = pci_vpd_info_field_size(&vpd_data[j]);
  10398. j += PCI_VPD_INFO_FLD_HDR_SIZE;
  10399. if (j + len > block_end || len != 4 ||
  10400. memcmp(&vpd_data[j], "1028", 4))
  10401. goto partno;
  10402. j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
  10403. PCI_VPD_RO_KEYWORD_VENDOR0);
  10404. if (j < 0)
  10405. goto partno;
  10406. len = pci_vpd_info_field_size(&vpd_data[j]);
  10407. j += PCI_VPD_INFO_FLD_HDR_SIZE;
  10408. if (j + len > block_end)
  10409. goto partno;
  10410. memcpy(tp->fw_ver, &vpd_data[j], len);
  10411. strncat(tp->fw_ver, " bc ", TG3_NVM_VPD_LEN - len - 1);
  10412. }
  10413. partno:
  10414. i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
  10415. PCI_VPD_RO_KEYWORD_PARTNO);
  10416. if (i < 0)
  10417. goto out_not_found;
  10418. len = pci_vpd_info_field_size(&vpd_data[i]);
  10419. i += PCI_VPD_INFO_FLD_HDR_SIZE;
  10420. if (len > TG3_BPN_SIZE ||
  10421. (len + i) > TG3_NVM_VPD_LEN)
  10422. goto out_not_found;
  10423. memcpy(tp->board_part_number, &vpd_data[i], len);
  10424. out_not_found:
  10425. kfree(vpd_data);
  10426. if (!tp->board_part_number[0])
  10427. return;
  10428. out_no_vpd:
  10429. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
  10430. strcpy(tp->board_part_number, "BCM95906");
  10431. else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
  10432. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
  10433. strcpy(tp->board_part_number, "BCM57780");
  10434. else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
  10435. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
  10436. strcpy(tp->board_part_number, "BCM57760");
  10437. else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
  10438. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
  10439. strcpy(tp->board_part_number, "BCM57790");
  10440. else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 &&
  10441. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
  10442. strcpy(tp->board_part_number, "BCM57788");
  10443. else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
  10444. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
  10445. strcpy(tp->board_part_number, "BCM57761");
  10446. else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
  10447. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
  10448. strcpy(tp->board_part_number, "BCM57765");
  10449. else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
  10450. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
  10451. strcpy(tp->board_part_number, "BCM57781");
  10452. else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
  10453. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
  10454. strcpy(tp->board_part_number, "BCM57785");
  10455. else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
  10456. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
  10457. strcpy(tp->board_part_number, "BCM57791");
  10458. else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
  10459. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
  10460. strcpy(tp->board_part_number, "BCM57795");
  10461. else
  10462. strcpy(tp->board_part_number, "none");
  10463. }
  10464. static int __devinit tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
  10465. {
  10466. u32 val;
  10467. if (tg3_nvram_read(tp, offset, &val) ||
  10468. (val & 0xfc000000) != 0x0c000000 ||
  10469. tg3_nvram_read(tp, offset + 4, &val) ||
  10470. val != 0)
  10471. return 0;
  10472. return 1;
  10473. }
  10474. static void __devinit tg3_read_bc_ver(struct tg3 *tp)
  10475. {
  10476. u32 val, offset, start, ver_offset;
  10477. int i, dst_off;
  10478. bool newver = false;
  10479. if (tg3_nvram_read(tp, 0xc, &offset) ||
  10480. tg3_nvram_read(tp, 0x4, &start))
  10481. return;
  10482. offset = tg3_nvram_logical_addr(tp, offset);
  10483. if (tg3_nvram_read(tp, offset, &val))
  10484. return;
  10485. if ((val & 0xfc000000) == 0x0c000000) {
  10486. if (tg3_nvram_read(tp, offset + 4, &val))
  10487. return;
  10488. if (val == 0)
  10489. newver = true;
  10490. }
  10491. dst_off = strlen(tp->fw_ver);
  10492. if (newver) {
  10493. if (TG3_VER_SIZE - dst_off < 16 ||
  10494. tg3_nvram_read(tp, offset + 8, &ver_offset))
  10495. return;
  10496. offset = offset + ver_offset - start;
  10497. for (i = 0; i < 16; i += 4) {
  10498. __be32 v;
  10499. if (tg3_nvram_read_be32(tp, offset + i, &v))
  10500. return;
  10501. memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
  10502. }
  10503. } else {
  10504. u32 major, minor;
  10505. if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
  10506. return;
  10507. major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
  10508. TG3_NVM_BCVER_MAJSFT;
  10509. minor = ver_offset & TG3_NVM_BCVER_MINMSK;
  10510. snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
  10511. "v%d.%02d", major, minor);
  10512. }
  10513. }
  10514. static void __devinit tg3_read_hwsb_ver(struct tg3 *tp)
  10515. {
  10516. u32 val, major, minor;
  10517. /* Use native endian representation */
  10518. if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
  10519. return;
  10520. major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
  10521. TG3_NVM_HWSB_CFG1_MAJSFT;
  10522. minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
  10523. TG3_NVM_HWSB_CFG1_MINSFT;
  10524. snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
  10525. }
  10526. static void __devinit tg3_read_sb_ver(struct tg3 *tp, u32 val)
  10527. {
  10528. u32 offset, major, minor, build;
  10529. strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
  10530. if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
  10531. return;
  10532. switch (val & TG3_EEPROM_SB_REVISION_MASK) {
  10533. case TG3_EEPROM_SB_REVISION_0:
  10534. offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
  10535. break;
  10536. case TG3_EEPROM_SB_REVISION_2:
  10537. offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
  10538. break;
  10539. case TG3_EEPROM_SB_REVISION_3:
  10540. offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
  10541. break;
  10542. case TG3_EEPROM_SB_REVISION_4:
  10543. offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
  10544. break;
  10545. case TG3_EEPROM_SB_REVISION_5:
  10546. offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
  10547. break;
  10548. default:
  10549. return;
  10550. }
  10551. if (tg3_nvram_read(tp, offset, &val))
  10552. return;
  10553. build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
  10554. TG3_EEPROM_SB_EDH_BLD_SHFT;
  10555. major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
  10556. TG3_EEPROM_SB_EDH_MAJ_SHFT;
  10557. minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
  10558. if (minor > 99 || build > 26)
  10559. return;
  10560. offset = strlen(tp->fw_ver);
  10561. snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
  10562. " v%d.%02d", major, minor);
  10563. if (build > 0) {
  10564. offset = strlen(tp->fw_ver);
  10565. if (offset < TG3_VER_SIZE - 1)
  10566. tp->fw_ver[offset] = 'a' + build - 1;
  10567. }
  10568. }
  10569. static void __devinit tg3_read_mgmtfw_ver(struct tg3 *tp)
  10570. {
  10571. u32 val, offset, start;
  10572. int i, vlen;
  10573. for (offset = TG3_NVM_DIR_START;
  10574. offset < TG3_NVM_DIR_END;
  10575. offset += TG3_NVM_DIRENT_SIZE) {
  10576. if (tg3_nvram_read(tp, offset, &val))
  10577. return;
  10578. if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
  10579. break;
  10580. }
  10581. if (offset == TG3_NVM_DIR_END)
  10582. return;
  10583. if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
  10584. start = 0x08000000;
  10585. else if (tg3_nvram_read(tp, offset - 4, &start))
  10586. return;
  10587. if (tg3_nvram_read(tp, offset + 4, &offset) ||
  10588. !tg3_fw_img_is_valid(tp, offset) ||
  10589. tg3_nvram_read(tp, offset + 8, &val))
  10590. return;
  10591. offset += val - start;
  10592. vlen = strlen(tp->fw_ver);
  10593. tp->fw_ver[vlen++] = ',';
  10594. tp->fw_ver[vlen++] = ' ';
  10595. for (i = 0; i < 4; i++) {
  10596. __be32 v;
  10597. if (tg3_nvram_read_be32(tp, offset, &v))
  10598. return;
  10599. offset += sizeof(v);
  10600. if (vlen > TG3_VER_SIZE - sizeof(v)) {
  10601. memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
  10602. break;
  10603. }
  10604. memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
  10605. vlen += sizeof(v);
  10606. }
  10607. }
  10608. static void __devinit tg3_read_dash_ver(struct tg3 *tp)
  10609. {
  10610. int vlen;
  10611. u32 apedata;
  10612. char *fwtype;
  10613. if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) ||
  10614. !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
  10615. return;
  10616. apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
  10617. if (apedata != APE_SEG_SIG_MAGIC)
  10618. return;
  10619. apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
  10620. if (!(apedata & APE_FW_STATUS_READY))
  10621. return;
  10622. apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
  10623. if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI) {
  10624. tp->tg3_flags3 |= TG3_FLG3_APE_HAS_NCSI;
  10625. fwtype = "NCSI";
  10626. } else {
  10627. fwtype = "DASH";
  10628. }
  10629. vlen = strlen(tp->fw_ver);
  10630. snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
  10631. fwtype,
  10632. (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
  10633. (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
  10634. (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
  10635. (apedata & APE_FW_VERSION_BLDMSK));
  10636. }
  10637. static void __devinit tg3_read_fw_ver(struct tg3 *tp)
  10638. {
  10639. u32 val;
  10640. bool vpd_vers = false;
  10641. if (tp->fw_ver[0] != 0)
  10642. vpd_vers = true;
  10643. if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) {
  10644. strcat(tp->fw_ver, "sb");
  10645. return;
  10646. }
  10647. if (tg3_nvram_read(tp, 0, &val))
  10648. return;
  10649. if (val == TG3_EEPROM_MAGIC)
  10650. tg3_read_bc_ver(tp);
  10651. else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
  10652. tg3_read_sb_ver(tp, val);
  10653. else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
  10654. tg3_read_hwsb_ver(tp);
  10655. else
  10656. return;
  10657. if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
  10658. (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) || vpd_vers)
  10659. goto done;
  10660. tg3_read_mgmtfw_ver(tp);
  10661. done:
  10662. tp->fw_ver[TG3_VER_SIZE - 1] = 0;
  10663. }
  10664. static struct pci_dev * __devinit tg3_find_peer(struct tg3 *);
  10665. static void inline vlan_features_add(struct net_device *dev, unsigned long flags)
  10666. {
  10667. #if TG3_VLAN_TAG_USED
  10668. dev->vlan_features |= flags;
  10669. #endif
  10670. }
  10671. static int __devinit tg3_get_invariants(struct tg3 *tp)
  10672. {
  10673. static struct pci_device_id write_reorder_chipsets[] = {
  10674. { PCI_DEVICE(PCI_VENDOR_ID_AMD,
  10675. PCI_DEVICE_ID_AMD_FE_GATE_700C) },
  10676. { PCI_DEVICE(PCI_VENDOR_ID_AMD,
  10677. PCI_DEVICE_ID_AMD_8131_BRIDGE) },
  10678. { PCI_DEVICE(PCI_VENDOR_ID_VIA,
  10679. PCI_DEVICE_ID_VIA_8385_0) },
  10680. { },
  10681. };
  10682. u32 misc_ctrl_reg;
  10683. u32 pci_state_reg, grc_misc_cfg;
  10684. u32 val;
  10685. u16 pci_cmd;
  10686. int err;
  10687. /* Force memory write invalidate off. If we leave it on,
  10688. * then on 5700_BX chips we have to enable a workaround.
  10689. * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
  10690. * to match the cacheline size. The Broadcom driver have this
  10691. * workaround but turns MWI off all the times so never uses
  10692. * it. This seems to suggest that the workaround is insufficient.
  10693. */
  10694. pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
  10695. pci_cmd &= ~PCI_COMMAND_INVALIDATE;
  10696. pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
  10697. /* It is absolutely critical that TG3PCI_MISC_HOST_CTRL
  10698. * has the register indirect write enable bit set before
  10699. * we try to access any of the MMIO registers. It is also
  10700. * critical that the PCI-X hw workaround situation is decided
  10701. * before that as well.
  10702. */
  10703. pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
  10704. &misc_ctrl_reg);
  10705. tp->pci_chip_rev_id = (misc_ctrl_reg >>
  10706. MISC_HOST_CTRL_CHIPREV_SHIFT);
  10707. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
  10708. u32 prod_id_asic_rev;
  10709. if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
  10710. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
  10711. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719)
  10712. pci_read_config_dword(tp->pdev,
  10713. TG3PCI_GEN2_PRODID_ASICREV,
  10714. &prod_id_asic_rev);
  10715. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
  10716. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
  10717. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
  10718. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
  10719. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
  10720. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
  10721. pci_read_config_dword(tp->pdev,
  10722. TG3PCI_GEN15_PRODID_ASICREV,
  10723. &prod_id_asic_rev);
  10724. else
  10725. pci_read_config_dword(tp->pdev, TG3PCI_PRODID_ASICREV,
  10726. &prod_id_asic_rev);
  10727. tp->pci_chip_rev_id = prod_id_asic_rev;
  10728. }
  10729. /* Wrong chip ID in 5752 A0. This code can be removed later
  10730. * as A0 is not in production.
  10731. */
  10732. if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
  10733. tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
  10734. /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
  10735. * we need to disable memory and use config. cycles
  10736. * only to access all registers. The 5702/03 chips
  10737. * can mistakenly decode the special cycles from the
  10738. * ICH chipsets as memory write cycles, causing corruption
  10739. * of register and memory space. Only certain ICH bridges
  10740. * will drive special cycles with non-zero data during the
  10741. * address phase which can fall within the 5703's address
  10742. * range. This is not an ICH bug as the PCI spec allows
  10743. * non-zero address during special cycles. However, only
  10744. * these ICH bridges are known to drive non-zero addresses
  10745. * during special cycles.
  10746. *
  10747. * Since special cycles do not cross PCI bridges, we only
  10748. * enable this workaround if the 5703 is on the secondary
  10749. * bus of these ICH bridges.
  10750. */
  10751. if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
  10752. (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
  10753. static struct tg3_dev_id {
  10754. u32 vendor;
  10755. u32 device;
  10756. u32 rev;
  10757. } ich_chipsets[] = {
  10758. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
  10759. PCI_ANY_ID },
  10760. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
  10761. PCI_ANY_ID },
  10762. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
  10763. 0xa },
  10764. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
  10765. PCI_ANY_ID },
  10766. { },
  10767. };
  10768. struct tg3_dev_id *pci_id = &ich_chipsets[0];
  10769. struct pci_dev *bridge = NULL;
  10770. while (pci_id->vendor != 0) {
  10771. bridge = pci_get_device(pci_id->vendor, pci_id->device,
  10772. bridge);
  10773. if (!bridge) {
  10774. pci_id++;
  10775. continue;
  10776. }
  10777. if (pci_id->rev != PCI_ANY_ID) {
  10778. if (bridge->revision > pci_id->rev)
  10779. continue;
  10780. }
  10781. if (bridge->subordinate &&
  10782. (bridge->subordinate->number ==
  10783. tp->pdev->bus->number)) {
  10784. tp->tg3_flags2 |= TG3_FLG2_ICH_WORKAROUND;
  10785. pci_dev_put(bridge);
  10786. break;
  10787. }
  10788. }
  10789. }
  10790. if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
  10791. static struct tg3_dev_id {
  10792. u32 vendor;
  10793. u32 device;
  10794. } bridge_chipsets[] = {
  10795. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
  10796. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
  10797. { },
  10798. };
  10799. struct tg3_dev_id *pci_id = &bridge_chipsets[0];
  10800. struct pci_dev *bridge = NULL;
  10801. while (pci_id->vendor != 0) {
  10802. bridge = pci_get_device(pci_id->vendor,
  10803. pci_id->device,
  10804. bridge);
  10805. if (!bridge) {
  10806. pci_id++;
  10807. continue;
  10808. }
  10809. if (bridge->subordinate &&
  10810. (bridge->subordinate->number <=
  10811. tp->pdev->bus->number) &&
  10812. (bridge->subordinate->subordinate >=
  10813. tp->pdev->bus->number)) {
  10814. tp->tg3_flags3 |= TG3_FLG3_5701_DMA_BUG;
  10815. pci_dev_put(bridge);
  10816. break;
  10817. }
  10818. }
  10819. }
  10820. /* The EPB bridge inside 5714, 5715, and 5780 cannot support
  10821. * DMA addresses > 40-bit. This bridge may have other additional
  10822. * 57xx devices behind it in some 4-port NIC designs for example.
  10823. * Any tg3 device found behind the bridge will also need the 40-bit
  10824. * DMA workaround.
  10825. */
  10826. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
  10827. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
  10828. tp->tg3_flags2 |= TG3_FLG2_5780_CLASS;
  10829. tp->tg3_flags |= TG3_FLAG_40BIT_DMA_BUG;
  10830. tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
  10831. } else {
  10832. struct pci_dev *bridge = NULL;
  10833. do {
  10834. bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
  10835. PCI_DEVICE_ID_SERVERWORKS_EPB,
  10836. bridge);
  10837. if (bridge && bridge->subordinate &&
  10838. (bridge->subordinate->number <=
  10839. tp->pdev->bus->number) &&
  10840. (bridge->subordinate->subordinate >=
  10841. tp->pdev->bus->number)) {
  10842. tp->tg3_flags |= TG3_FLAG_40BIT_DMA_BUG;
  10843. pci_dev_put(bridge);
  10844. break;
  10845. }
  10846. } while (bridge);
  10847. }
  10848. /* Initialize misc host control in PCI block. */
  10849. tp->misc_host_ctrl |= (misc_ctrl_reg &
  10850. MISC_HOST_CTRL_CHIPREV);
  10851. pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
  10852. tp->misc_host_ctrl);
  10853. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
  10854. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
  10855. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
  10856. tp->pdev_peer = tg3_find_peer(tp);
  10857. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
  10858. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
  10859. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
  10860. tp->tg3_flags3 |= TG3_FLG3_5717_PLUS;
  10861. /* Intentionally exclude ASIC_REV_5906 */
  10862. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
  10863. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
  10864. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
  10865. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
  10866. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
  10867. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
  10868. (tp->tg3_flags3 & TG3_FLG3_5717_PLUS))
  10869. tp->tg3_flags3 |= TG3_FLG3_5755_PLUS;
  10870. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
  10871. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
  10872. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
  10873. (tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
  10874. (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
  10875. tp->tg3_flags2 |= TG3_FLG2_5750_PLUS;
  10876. if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) ||
  10877. (tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
  10878. tp->tg3_flags2 |= TG3_FLG2_5705_PLUS;
  10879. /* 5700 B0 chips do not support checksumming correctly due
  10880. * to hardware bugs.
  10881. */
  10882. if (tp->pci_chip_rev_id == CHIPREV_ID_5700_B0)
  10883. tp->tg3_flags |= TG3_FLAG_BROKEN_CHECKSUMS;
  10884. else {
  10885. unsigned long features = NETIF_F_IP_CSUM | NETIF_F_SG | NETIF_F_GRO;
  10886. tp->tg3_flags |= TG3_FLAG_RX_CHECKSUMS;
  10887. if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
  10888. features |= NETIF_F_IPV6_CSUM;
  10889. tp->dev->features |= features;
  10890. vlan_features_add(tp->dev, features);
  10891. }
  10892. /* Determine TSO capabilities */
  10893. if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS)
  10894. tp->tg3_flags2 |= TG3_FLG2_HW_TSO_3;
  10895. else if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
  10896. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
  10897. tp->tg3_flags2 |= TG3_FLG2_HW_TSO_2;
  10898. else if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
  10899. tp->tg3_flags2 |= TG3_FLG2_HW_TSO_1 | TG3_FLG2_TSO_BUG;
  10900. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 &&
  10901. tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
  10902. tp->tg3_flags2 &= ~TG3_FLG2_TSO_BUG;
  10903. } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
  10904. GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
  10905. tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
  10906. tp->tg3_flags2 |= TG3_FLG2_TSO_BUG;
  10907. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
  10908. tp->fw_needed = FIRMWARE_TG3TSO5;
  10909. else
  10910. tp->fw_needed = FIRMWARE_TG3TSO;
  10911. }
  10912. tp->irq_max = 1;
  10913. if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
  10914. tp->tg3_flags |= TG3_FLAG_SUPPORT_MSI;
  10915. if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
  10916. GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
  10917. (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
  10918. tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
  10919. tp->pdev_peer == tp->pdev))
  10920. tp->tg3_flags &= ~TG3_FLAG_SUPPORT_MSI;
  10921. if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
  10922. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
  10923. tp->tg3_flags2 |= TG3_FLG2_1SHOT_MSI;
  10924. }
  10925. if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS) {
  10926. tp->tg3_flags |= TG3_FLAG_SUPPORT_MSIX;
  10927. tp->irq_max = TG3_IRQ_MAX_VECS;
  10928. }
  10929. }
  10930. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
  10931. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
  10932. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
  10933. tp->tg3_flags3 |= TG3_FLG3_SHORT_DMA_BUG;
  10934. else if (!(tp->tg3_flags3 & TG3_FLG3_5755_PLUS)) {
  10935. tp->tg3_flags3 |= TG3_FLG3_4G_DMA_BNDRY_BUG;
  10936. tp->tg3_flags3 |= TG3_FLG3_40BIT_DMA_LIMIT_BUG;
  10937. }
  10938. if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS)
  10939. tp->tg3_flags3 |= TG3_FLG3_USE_JUMBO_BDFLAG;
  10940. if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
  10941. (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
  10942. (tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG))
  10943. tp->tg3_flags |= TG3_FLAG_JUMBO_CAPABLE;
  10944. pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
  10945. &pci_state_reg);
  10946. tp->pcie_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_EXP);
  10947. if (tp->pcie_cap != 0) {
  10948. u16 lnkctl;
  10949. tp->tg3_flags2 |= TG3_FLG2_PCI_EXPRESS;
  10950. pcie_set_readrq(tp->pdev, 4096);
  10951. pci_read_config_word(tp->pdev,
  10952. tp->pcie_cap + PCI_EXP_LNKCTL,
  10953. &lnkctl);
  10954. if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
  10955. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
  10956. tp->tg3_flags2 &= ~TG3_FLG2_HW_TSO_2;
  10957. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
  10958. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
  10959. tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
  10960. tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
  10961. tp->tg3_flags3 |= TG3_FLG3_CLKREQ_BUG;
  10962. } else if (tp->pci_chip_rev_id == CHIPREV_ID_5717_A0) {
  10963. tp->tg3_flags3 |= TG3_FLG3_L1PLLPD_EN;
  10964. }
  10965. } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
  10966. tp->tg3_flags2 |= TG3_FLG2_PCI_EXPRESS;
  10967. } else if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
  10968. (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
  10969. tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
  10970. if (!tp->pcix_cap) {
  10971. dev_err(&tp->pdev->dev,
  10972. "Cannot find PCI-X capability, aborting\n");
  10973. return -EIO;
  10974. }
  10975. if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
  10976. tp->tg3_flags |= TG3_FLAG_PCIX_MODE;
  10977. }
  10978. /* If we have an AMD 762 or VIA K8T800 chipset, write
  10979. * reordering to the mailbox registers done by the host
  10980. * controller can cause major troubles. We read back from
  10981. * every mailbox register write to force the writes to be
  10982. * posted to the chip in order.
  10983. */
  10984. if (pci_dev_present(write_reorder_chipsets) &&
  10985. !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
  10986. tp->tg3_flags |= TG3_FLAG_MBOX_WRITE_REORDER;
  10987. pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
  10988. &tp->pci_cacheline_sz);
  10989. pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
  10990. &tp->pci_lat_timer);
  10991. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
  10992. tp->pci_lat_timer < 64) {
  10993. tp->pci_lat_timer = 64;
  10994. pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
  10995. tp->pci_lat_timer);
  10996. }
  10997. if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
  10998. /* 5700 BX chips need to have their TX producer index
  10999. * mailboxes written twice to workaround a bug.
  11000. */
  11001. tp->tg3_flags |= TG3_FLAG_TXD_MBOX_HWBUG;
  11002. /* If we are in PCI-X mode, enable register write workaround.
  11003. *
  11004. * The workaround is to use indirect register accesses
  11005. * for all chip writes not to mailbox registers.
  11006. */
  11007. if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
  11008. u32 pm_reg;
  11009. tp->tg3_flags |= TG3_FLAG_PCIX_TARGET_HWBUG;
  11010. /* The chip can have it's power management PCI config
  11011. * space registers clobbered due to this bug.
  11012. * So explicitly force the chip into D0 here.
  11013. */
  11014. pci_read_config_dword(tp->pdev,
  11015. tp->pm_cap + PCI_PM_CTRL,
  11016. &pm_reg);
  11017. pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
  11018. pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
  11019. pci_write_config_dword(tp->pdev,
  11020. tp->pm_cap + PCI_PM_CTRL,
  11021. pm_reg);
  11022. /* Also, force SERR#/PERR# in PCI command. */
  11023. pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
  11024. pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
  11025. pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
  11026. }
  11027. }
  11028. if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
  11029. tp->tg3_flags |= TG3_FLAG_PCI_HIGH_SPEED;
  11030. if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
  11031. tp->tg3_flags |= TG3_FLAG_PCI_32BIT;
  11032. /* Chip-specific fixup from Broadcom driver */
  11033. if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
  11034. (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
  11035. pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
  11036. pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
  11037. }
  11038. /* Default fast path register access methods */
  11039. tp->read32 = tg3_read32;
  11040. tp->write32 = tg3_write32;
  11041. tp->read32_mbox = tg3_read32;
  11042. tp->write32_mbox = tg3_write32;
  11043. tp->write32_tx_mbox = tg3_write32;
  11044. tp->write32_rx_mbox = tg3_write32;
  11045. /* Various workaround register access methods */
  11046. if (tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG)
  11047. tp->write32 = tg3_write_indirect_reg32;
  11048. else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
  11049. ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
  11050. tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
  11051. /*
  11052. * Back to back register writes can cause problems on these
  11053. * chips, the workaround is to read back all reg writes
  11054. * except those to mailbox regs.
  11055. *
  11056. * See tg3_write_indirect_reg32().
  11057. */
  11058. tp->write32 = tg3_write_flush_reg32;
  11059. }
  11060. if ((tp->tg3_flags & TG3_FLAG_TXD_MBOX_HWBUG) ||
  11061. (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)) {
  11062. tp->write32_tx_mbox = tg3_write32_tx_mbox;
  11063. if (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)
  11064. tp->write32_rx_mbox = tg3_write_flush_reg32;
  11065. }
  11066. if (tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND) {
  11067. tp->read32 = tg3_read_indirect_reg32;
  11068. tp->write32 = tg3_write_indirect_reg32;
  11069. tp->read32_mbox = tg3_read_indirect_mbox;
  11070. tp->write32_mbox = tg3_write_indirect_mbox;
  11071. tp->write32_tx_mbox = tg3_write_indirect_mbox;
  11072. tp->write32_rx_mbox = tg3_write_indirect_mbox;
  11073. iounmap(tp->regs);
  11074. tp->regs = NULL;
  11075. pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
  11076. pci_cmd &= ~PCI_COMMAND_MEMORY;
  11077. pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
  11078. }
  11079. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
  11080. tp->read32_mbox = tg3_read32_mbox_5906;
  11081. tp->write32_mbox = tg3_write32_mbox_5906;
  11082. tp->write32_tx_mbox = tg3_write32_mbox_5906;
  11083. tp->write32_rx_mbox = tg3_write32_mbox_5906;
  11084. }
  11085. if (tp->write32 == tg3_write_indirect_reg32 ||
  11086. ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) &&
  11087. (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
  11088. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
  11089. tp->tg3_flags |= TG3_FLAG_SRAM_USE_CONFIG;
  11090. /* Get eeprom hw config before calling tg3_set_power_state().
  11091. * In particular, the TG3_FLG2_IS_NIC flag must be
  11092. * determined before calling tg3_set_power_state() so that
  11093. * we know whether or not to switch out of Vaux power.
  11094. * When the flag is set, it means that GPIO1 is used for eeprom
  11095. * write protect and also implies that it is a LOM where GPIOs
  11096. * are not used to switch power.
  11097. */
  11098. tg3_get_eeprom_hw_cfg(tp);
  11099. if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
  11100. /* Allow reads and writes to the
  11101. * APE register and memory space.
  11102. */
  11103. pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
  11104. PCISTATE_ALLOW_APE_SHMEM_WR |
  11105. PCISTATE_ALLOW_APE_PSPACE_WR;
  11106. pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
  11107. pci_state_reg);
  11108. }
  11109. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
  11110. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
  11111. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
  11112. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
  11113. (tp->tg3_flags3 & TG3_FLG3_5717_PLUS))
  11114. tp->tg3_flags |= TG3_FLAG_CPMU_PRESENT;
  11115. /* Set up tp->grc_local_ctrl before calling tg3_set_power_state().
  11116. * GPIO1 driven high will bring 5700's external PHY out of reset.
  11117. * It is also used as eeprom write protect on LOMs.
  11118. */
  11119. tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
  11120. if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) ||
  11121. (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT))
  11122. tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
  11123. GRC_LCLCTRL_GPIO_OUTPUT1);
  11124. /* Unused GPIO3 must be driven as output on 5752 because there
  11125. * are no pull-up resistors on unused GPIO pins.
  11126. */
  11127. else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
  11128. tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
  11129. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
  11130. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
  11131. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
  11132. tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
  11133. if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
  11134. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
  11135. /* Turn off the debug UART. */
  11136. tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
  11137. if (tp->tg3_flags2 & TG3_FLG2_IS_NIC)
  11138. /* Keep VMain power. */
  11139. tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
  11140. GRC_LCLCTRL_GPIO_OUTPUT0;
  11141. }
  11142. /* Force the chip into D0. */
  11143. err = tg3_set_power_state(tp, PCI_D0);
  11144. if (err) {
  11145. dev_err(&tp->pdev->dev, "Transition to D0 failed\n");
  11146. return err;
  11147. }
  11148. /* Derive initial jumbo mode from MTU assigned in
  11149. * ether_setup() via the alloc_etherdev() call
  11150. */
  11151. if (tp->dev->mtu > ETH_DATA_LEN &&
  11152. !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
  11153. tp->tg3_flags |= TG3_FLAG_JUMBO_RING_ENABLE;
  11154. /* Determine WakeOnLan speed to use. */
  11155. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
  11156. tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
  11157. tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
  11158. tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
  11159. tp->tg3_flags &= ~(TG3_FLAG_WOL_SPEED_100MB);
  11160. } else {
  11161. tp->tg3_flags |= TG3_FLAG_WOL_SPEED_100MB;
  11162. }
  11163. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
  11164. tp->phy_flags |= TG3_PHYFLG_IS_FET;
  11165. /* A few boards don't want Ethernet@WireSpeed phy feature */
  11166. if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) ||
  11167. ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
  11168. (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
  11169. (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
  11170. (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
  11171. (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
  11172. tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
  11173. if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
  11174. GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
  11175. tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
  11176. if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
  11177. tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
  11178. if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
  11179. !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
  11180. GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
  11181. GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780 &&
  11182. !(tp->tg3_flags3 & TG3_FLG3_5717_PLUS)) {
  11183. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
  11184. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
  11185. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
  11186. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
  11187. if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
  11188. tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
  11189. tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
  11190. if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
  11191. tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
  11192. } else
  11193. tp->phy_flags |= TG3_PHYFLG_BER_BUG;
  11194. }
  11195. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
  11196. GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
  11197. tp->phy_otp = tg3_read_otp_phycfg(tp);
  11198. if (tp->phy_otp == 0)
  11199. tp->phy_otp = TG3_OTP_DEFAULT;
  11200. }
  11201. if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT)
  11202. tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
  11203. else
  11204. tp->mi_mode = MAC_MI_MODE_BASE;
  11205. tp->coalesce_mode = 0;
  11206. if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
  11207. GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
  11208. tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
  11209. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
  11210. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
  11211. tp->tg3_flags3 |= TG3_FLG3_USE_PHYLIB;
  11212. err = tg3_mdio_init(tp);
  11213. if (err)
  11214. return err;
  11215. /* Initialize data/descriptor byte/word swapping. */
  11216. val = tr32(GRC_MODE);
  11217. val &= GRC_MODE_HOST_STACKUP;
  11218. tw32(GRC_MODE, val | tp->grc_mode);
  11219. tg3_switch_clocks(tp);
  11220. /* Clear this out for sanity. */
  11221. tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
  11222. pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
  11223. &pci_state_reg);
  11224. if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
  11225. (tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG) == 0) {
  11226. u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
  11227. if (chiprevid == CHIPREV_ID_5701_A0 ||
  11228. chiprevid == CHIPREV_ID_5701_B0 ||
  11229. chiprevid == CHIPREV_ID_5701_B2 ||
  11230. chiprevid == CHIPREV_ID_5701_B5) {
  11231. void __iomem *sram_base;
  11232. /* Write some dummy words into the SRAM status block
  11233. * area, see if it reads back correctly. If the return
  11234. * value is bad, force enable the PCIX workaround.
  11235. */
  11236. sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
  11237. writel(0x00000000, sram_base);
  11238. writel(0x00000000, sram_base + 4);
  11239. writel(0xffffffff, sram_base + 4);
  11240. if (readl(sram_base) != 0x00000000)
  11241. tp->tg3_flags |= TG3_FLAG_PCIX_TARGET_HWBUG;
  11242. }
  11243. }
  11244. udelay(50);
  11245. tg3_nvram_init(tp);
  11246. grc_misc_cfg = tr32(GRC_MISC_CFG);
  11247. grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
  11248. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
  11249. (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
  11250. grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
  11251. tp->tg3_flags2 |= TG3_FLG2_IS_5788;
  11252. if (!(tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
  11253. (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700))
  11254. tp->tg3_flags |= TG3_FLAG_TAGGED_STATUS;
  11255. if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) {
  11256. tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
  11257. HOSTCC_MODE_CLRTICK_TXBD);
  11258. tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
  11259. pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
  11260. tp->misc_host_ctrl);
  11261. }
  11262. /* Preserve the APE MAC_MODE bits */
  11263. if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
  11264. tp->mac_mode = tr32(MAC_MODE) |
  11265. MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
  11266. else
  11267. tp->mac_mode = TG3_DEF_MAC_MODE;
  11268. /* these are limited to 10/100 only */
  11269. if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
  11270. (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
  11271. (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
  11272. tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
  11273. (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901 ||
  11274. tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901_2 ||
  11275. tp->pdev->device == PCI_DEVICE_ID_TIGON3_5705F)) ||
  11276. (tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
  11277. (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5751F ||
  11278. tp->pdev->device == PCI_DEVICE_ID_TIGON3_5753F ||
  11279. tp->pdev->device == PCI_DEVICE_ID_TIGON3_5787F)) ||
  11280. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790 ||
  11281. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
  11282. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
  11283. (tp->phy_flags & TG3_PHYFLG_IS_FET))
  11284. tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
  11285. err = tg3_phy_probe(tp);
  11286. if (err) {
  11287. dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
  11288. /* ... but do not return immediately ... */
  11289. tg3_mdio_fini(tp);
  11290. }
  11291. tg3_read_vpd(tp);
  11292. tg3_read_fw_ver(tp);
  11293. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
  11294. tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
  11295. } else {
  11296. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
  11297. tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
  11298. else
  11299. tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
  11300. }
  11301. /* 5700 {AX,BX} chips have a broken status block link
  11302. * change bit implementation, so we must use the
  11303. * status register in those cases.
  11304. */
  11305. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
  11306. tp->tg3_flags |= TG3_FLAG_USE_LINKCHG_REG;
  11307. else
  11308. tp->tg3_flags &= ~TG3_FLAG_USE_LINKCHG_REG;
  11309. /* The led_ctrl is set during tg3_phy_probe, here we might
  11310. * have to force the link status polling mechanism based
  11311. * upon subsystem IDs.
  11312. */
  11313. if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
  11314. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
  11315. !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
  11316. tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
  11317. tp->tg3_flags |= TG3_FLAG_USE_LINKCHG_REG;
  11318. }
  11319. /* For all SERDES we poll the MAC status register. */
  11320. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
  11321. tp->tg3_flags |= TG3_FLAG_POLL_SERDES;
  11322. else
  11323. tp->tg3_flags &= ~TG3_FLAG_POLL_SERDES;
  11324. tp->rx_offset = NET_IP_ALIGN + TG3_RX_HEADROOM;
  11325. tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
  11326. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
  11327. (tp->tg3_flags & TG3_FLAG_PCIX_MODE) != 0) {
  11328. tp->rx_offset -= NET_IP_ALIGN;
  11329. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  11330. tp->rx_copy_thresh = ~(u16)0;
  11331. #endif
  11332. }
  11333. tp->rx_std_max_post = TG3_RX_RING_SIZE;
  11334. /* Increment the rx prod index on the rx std ring by at most
  11335. * 8 for these chips to workaround hw errata.
  11336. */
  11337. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
  11338. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
  11339. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
  11340. tp->rx_std_max_post = 8;
  11341. if (tp->tg3_flags & TG3_FLAG_ASPM_WORKAROUND)
  11342. tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
  11343. PCIE_PWR_MGMT_L1_THRESH_MSK;
  11344. return err;
  11345. }
  11346. #ifdef CONFIG_SPARC
  11347. static int __devinit tg3_get_macaddr_sparc(struct tg3 *tp)
  11348. {
  11349. struct net_device *dev = tp->dev;
  11350. struct pci_dev *pdev = tp->pdev;
  11351. struct device_node *dp = pci_device_to_OF_node(pdev);
  11352. const unsigned char *addr;
  11353. int len;
  11354. addr = of_get_property(dp, "local-mac-address", &len);
  11355. if (addr && len == 6) {
  11356. memcpy(dev->dev_addr, addr, 6);
  11357. memcpy(dev->perm_addr, dev->dev_addr, 6);
  11358. return 0;
  11359. }
  11360. return -ENODEV;
  11361. }
  11362. static int __devinit tg3_get_default_macaddr_sparc(struct tg3 *tp)
  11363. {
  11364. struct net_device *dev = tp->dev;
  11365. memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
  11366. memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
  11367. return 0;
  11368. }
  11369. #endif
  11370. static int __devinit tg3_get_device_address(struct tg3 *tp)
  11371. {
  11372. struct net_device *dev = tp->dev;
  11373. u32 hi, lo, mac_offset;
  11374. int addr_ok = 0;
  11375. #ifdef CONFIG_SPARC
  11376. if (!tg3_get_macaddr_sparc(tp))
  11377. return 0;
  11378. #endif
  11379. mac_offset = 0x7c;
  11380. if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) ||
  11381. (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
  11382. if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
  11383. mac_offset = 0xcc;
  11384. if (tg3_nvram_lock(tp))
  11385. tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
  11386. else
  11387. tg3_nvram_unlock(tp);
  11388. } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
  11389. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719) {
  11390. if (PCI_FUNC(tp->pdev->devfn) & 1)
  11391. mac_offset = 0xcc;
  11392. if (PCI_FUNC(tp->pdev->devfn) > 1)
  11393. mac_offset += 0x18c;
  11394. } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
  11395. mac_offset = 0x10;
  11396. /* First try to get it from MAC address mailbox. */
  11397. tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
  11398. if ((hi >> 16) == 0x484b) {
  11399. dev->dev_addr[0] = (hi >> 8) & 0xff;
  11400. dev->dev_addr[1] = (hi >> 0) & 0xff;
  11401. tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
  11402. dev->dev_addr[2] = (lo >> 24) & 0xff;
  11403. dev->dev_addr[3] = (lo >> 16) & 0xff;
  11404. dev->dev_addr[4] = (lo >> 8) & 0xff;
  11405. dev->dev_addr[5] = (lo >> 0) & 0xff;
  11406. /* Some old bootcode may report a 0 MAC address in SRAM */
  11407. addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
  11408. }
  11409. if (!addr_ok) {
  11410. /* Next, try NVRAM. */
  11411. if (!(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) &&
  11412. !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
  11413. !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
  11414. memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
  11415. memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
  11416. }
  11417. /* Finally just fetch it out of the MAC control regs. */
  11418. else {
  11419. hi = tr32(MAC_ADDR_0_HIGH);
  11420. lo = tr32(MAC_ADDR_0_LOW);
  11421. dev->dev_addr[5] = lo & 0xff;
  11422. dev->dev_addr[4] = (lo >> 8) & 0xff;
  11423. dev->dev_addr[3] = (lo >> 16) & 0xff;
  11424. dev->dev_addr[2] = (lo >> 24) & 0xff;
  11425. dev->dev_addr[1] = hi & 0xff;
  11426. dev->dev_addr[0] = (hi >> 8) & 0xff;
  11427. }
  11428. }
  11429. if (!is_valid_ether_addr(&dev->dev_addr[0])) {
  11430. #ifdef CONFIG_SPARC
  11431. if (!tg3_get_default_macaddr_sparc(tp))
  11432. return 0;
  11433. #endif
  11434. return -EINVAL;
  11435. }
  11436. memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
  11437. return 0;
  11438. }
  11439. #define BOUNDARY_SINGLE_CACHELINE 1
  11440. #define BOUNDARY_MULTI_CACHELINE 2
  11441. static u32 __devinit tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
  11442. {
  11443. int cacheline_size;
  11444. u8 byte;
  11445. int goal;
  11446. pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
  11447. if (byte == 0)
  11448. cacheline_size = 1024;
  11449. else
  11450. cacheline_size = (int) byte * 4;
  11451. /* On 5703 and later chips, the boundary bits have no
  11452. * effect.
  11453. */
  11454. if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
  11455. GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
  11456. !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
  11457. goto out;
  11458. #if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
  11459. goal = BOUNDARY_MULTI_CACHELINE;
  11460. #else
  11461. #if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
  11462. goal = BOUNDARY_SINGLE_CACHELINE;
  11463. #else
  11464. goal = 0;
  11465. #endif
  11466. #endif
  11467. if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS) {
  11468. val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
  11469. goto out;
  11470. }
  11471. if (!goal)
  11472. goto out;
  11473. /* PCI controllers on most RISC systems tend to disconnect
  11474. * when a device tries to burst across a cache-line boundary.
  11475. * Therefore, letting tg3 do so just wastes PCI bandwidth.
  11476. *
  11477. * Unfortunately, for PCI-E there are only limited
  11478. * write-side controls for this, and thus for reads
  11479. * we will still get the disconnects. We'll also waste
  11480. * these PCI cycles for both read and write for chips
  11481. * other than 5700 and 5701 which do not implement the
  11482. * boundary bits.
  11483. */
  11484. if ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) &&
  11485. !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)) {
  11486. switch (cacheline_size) {
  11487. case 16:
  11488. case 32:
  11489. case 64:
  11490. case 128:
  11491. if (goal == BOUNDARY_SINGLE_CACHELINE) {
  11492. val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
  11493. DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
  11494. } else {
  11495. val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
  11496. DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
  11497. }
  11498. break;
  11499. case 256:
  11500. val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
  11501. DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
  11502. break;
  11503. default:
  11504. val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
  11505. DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
  11506. break;
  11507. }
  11508. } else if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
  11509. switch (cacheline_size) {
  11510. case 16:
  11511. case 32:
  11512. case 64:
  11513. if (goal == BOUNDARY_SINGLE_CACHELINE) {
  11514. val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
  11515. val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
  11516. break;
  11517. }
  11518. /* fallthrough */
  11519. case 128:
  11520. default:
  11521. val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
  11522. val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
  11523. break;
  11524. }
  11525. } else {
  11526. switch (cacheline_size) {
  11527. case 16:
  11528. if (goal == BOUNDARY_SINGLE_CACHELINE) {
  11529. val |= (DMA_RWCTRL_READ_BNDRY_16 |
  11530. DMA_RWCTRL_WRITE_BNDRY_16);
  11531. break;
  11532. }
  11533. /* fallthrough */
  11534. case 32:
  11535. if (goal == BOUNDARY_SINGLE_CACHELINE) {
  11536. val |= (DMA_RWCTRL_READ_BNDRY_32 |
  11537. DMA_RWCTRL_WRITE_BNDRY_32);
  11538. break;
  11539. }
  11540. /* fallthrough */
  11541. case 64:
  11542. if (goal == BOUNDARY_SINGLE_CACHELINE) {
  11543. val |= (DMA_RWCTRL_READ_BNDRY_64 |
  11544. DMA_RWCTRL_WRITE_BNDRY_64);
  11545. break;
  11546. }
  11547. /* fallthrough */
  11548. case 128:
  11549. if (goal == BOUNDARY_SINGLE_CACHELINE) {
  11550. val |= (DMA_RWCTRL_READ_BNDRY_128 |
  11551. DMA_RWCTRL_WRITE_BNDRY_128);
  11552. break;
  11553. }
  11554. /* fallthrough */
  11555. case 256:
  11556. val |= (DMA_RWCTRL_READ_BNDRY_256 |
  11557. DMA_RWCTRL_WRITE_BNDRY_256);
  11558. break;
  11559. case 512:
  11560. val |= (DMA_RWCTRL_READ_BNDRY_512 |
  11561. DMA_RWCTRL_WRITE_BNDRY_512);
  11562. break;
  11563. case 1024:
  11564. default:
  11565. val |= (DMA_RWCTRL_READ_BNDRY_1024 |
  11566. DMA_RWCTRL_WRITE_BNDRY_1024);
  11567. break;
  11568. }
  11569. }
  11570. out:
  11571. return val;
  11572. }
  11573. static int __devinit tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma, int size, int to_device)
  11574. {
  11575. struct tg3_internal_buffer_desc test_desc;
  11576. u32 sram_dma_descs;
  11577. int i, ret;
  11578. sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
  11579. tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
  11580. tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
  11581. tw32(RDMAC_STATUS, 0);
  11582. tw32(WDMAC_STATUS, 0);
  11583. tw32(BUFMGR_MODE, 0);
  11584. tw32(FTQ_RESET, 0);
  11585. test_desc.addr_hi = ((u64) buf_dma) >> 32;
  11586. test_desc.addr_lo = buf_dma & 0xffffffff;
  11587. test_desc.nic_mbuf = 0x00002100;
  11588. test_desc.len = size;
  11589. /*
  11590. * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
  11591. * the *second* time the tg3 driver was getting loaded after an
  11592. * initial scan.
  11593. *
  11594. * Broadcom tells me:
  11595. * ...the DMA engine is connected to the GRC block and a DMA
  11596. * reset may affect the GRC block in some unpredictable way...
  11597. * The behavior of resets to individual blocks has not been tested.
  11598. *
  11599. * Broadcom noted the GRC reset will also reset all sub-components.
  11600. */
  11601. if (to_device) {
  11602. test_desc.cqid_sqid = (13 << 8) | 2;
  11603. tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
  11604. udelay(40);
  11605. } else {
  11606. test_desc.cqid_sqid = (16 << 8) | 7;
  11607. tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
  11608. udelay(40);
  11609. }
  11610. test_desc.flags = 0x00000005;
  11611. for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
  11612. u32 val;
  11613. val = *(((u32 *)&test_desc) + i);
  11614. pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
  11615. sram_dma_descs + (i * sizeof(u32)));
  11616. pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
  11617. }
  11618. pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
  11619. if (to_device)
  11620. tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
  11621. else
  11622. tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
  11623. ret = -ENODEV;
  11624. for (i = 0; i < 40; i++) {
  11625. u32 val;
  11626. if (to_device)
  11627. val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
  11628. else
  11629. val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
  11630. if ((val & 0xffff) == sram_dma_descs) {
  11631. ret = 0;
  11632. break;
  11633. }
  11634. udelay(100);
  11635. }
  11636. return ret;
  11637. }
  11638. #define TEST_BUFFER_SIZE 0x2000
  11639. static int __devinit tg3_test_dma(struct tg3 *tp)
  11640. {
  11641. dma_addr_t buf_dma;
  11642. u32 *buf, saved_dma_rwctrl;
  11643. int ret = 0;
  11644. buf = pci_alloc_consistent(tp->pdev, TEST_BUFFER_SIZE, &buf_dma);
  11645. if (!buf) {
  11646. ret = -ENOMEM;
  11647. goto out_nofree;
  11648. }
  11649. tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
  11650. (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
  11651. tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
  11652. if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS)
  11653. goto out;
  11654. if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
  11655. /* DMA read watermark not used on PCIE */
  11656. tp->dma_rwctrl |= 0x00180000;
  11657. } else if (!(tp->tg3_flags & TG3_FLAG_PCIX_MODE)) {
  11658. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
  11659. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
  11660. tp->dma_rwctrl |= 0x003f0000;
  11661. else
  11662. tp->dma_rwctrl |= 0x003f000f;
  11663. } else {
  11664. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
  11665. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
  11666. u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
  11667. u32 read_water = 0x7;
  11668. /* If the 5704 is behind the EPB bridge, we can
  11669. * do the less restrictive ONE_DMA workaround for
  11670. * better performance.
  11671. */
  11672. if ((tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG) &&
  11673. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
  11674. tp->dma_rwctrl |= 0x8000;
  11675. else if (ccval == 0x6 || ccval == 0x7)
  11676. tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
  11677. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
  11678. read_water = 4;
  11679. /* Set bit 23 to enable PCIX hw bug fix */
  11680. tp->dma_rwctrl |=
  11681. (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
  11682. (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
  11683. (1 << 23);
  11684. } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
  11685. /* 5780 always in PCIX mode */
  11686. tp->dma_rwctrl |= 0x00144000;
  11687. } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
  11688. /* 5714 always in PCIX mode */
  11689. tp->dma_rwctrl |= 0x00148000;
  11690. } else {
  11691. tp->dma_rwctrl |= 0x001b000f;
  11692. }
  11693. }
  11694. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
  11695. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
  11696. tp->dma_rwctrl &= 0xfffffff0;
  11697. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
  11698. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
  11699. /* Remove this if it causes problems for some boards. */
  11700. tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
  11701. /* On 5700/5701 chips, we need to set this bit.
  11702. * Otherwise the chip will issue cacheline transactions
  11703. * to streamable DMA memory with not all the byte
  11704. * enables turned on. This is an error on several
  11705. * RISC PCI controllers, in particular sparc64.
  11706. *
  11707. * On 5703/5704 chips, this bit has been reassigned
  11708. * a different meaning. In particular, it is used
  11709. * on those chips to enable a PCI-X workaround.
  11710. */
  11711. tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
  11712. }
  11713. tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
  11714. #if 0
  11715. /* Unneeded, already done by tg3_get_invariants. */
  11716. tg3_switch_clocks(tp);
  11717. #endif
  11718. if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
  11719. GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
  11720. goto out;
  11721. /* It is best to perform DMA test with maximum write burst size
  11722. * to expose the 5700/5701 write DMA bug.
  11723. */
  11724. saved_dma_rwctrl = tp->dma_rwctrl;
  11725. tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
  11726. tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
  11727. while (1) {
  11728. u32 *p = buf, i;
  11729. for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
  11730. p[i] = i;
  11731. /* Send the buffer to the chip. */
  11732. ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
  11733. if (ret) {
  11734. dev_err(&tp->pdev->dev,
  11735. "%s: Buffer write failed. err = %d\n",
  11736. __func__, ret);
  11737. break;
  11738. }
  11739. #if 0
  11740. /* validate data reached card RAM correctly. */
  11741. for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
  11742. u32 val;
  11743. tg3_read_mem(tp, 0x2100 + (i*4), &val);
  11744. if (le32_to_cpu(val) != p[i]) {
  11745. dev_err(&tp->pdev->dev,
  11746. "%s: Buffer corrupted on device! "
  11747. "(%d != %d)\n", __func__, val, i);
  11748. /* ret = -ENODEV here? */
  11749. }
  11750. p[i] = 0;
  11751. }
  11752. #endif
  11753. /* Now read it back. */
  11754. ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
  11755. if (ret) {
  11756. dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
  11757. "err = %d\n", __func__, ret);
  11758. break;
  11759. }
  11760. /* Verify it. */
  11761. for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
  11762. if (p[i] == i)
  11763. continue;
  11764. if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
  11765. DMA_RWCTRL_WRITE_BNDRY_16) {
  11766. tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
  11767. tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
  11768. tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
  11769. break;
  11770. } else {
  11771. dev_err(&tp->pdev->dev,
  11772. "%s: Buffer corrupted on read back! "
  11773. "(%d != %d)\n", __func__, p[i], i);
  11774. ret = -ENODEV;
  11775. goto out;
  11776. }
  11777. }
  11778. if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
  11779. /* Success. */
  11780. ret = 0;
  11781. break;
  11782. }
  11783. }
  11784. if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
  11785. DMA_RWCTRL_WRITE_BNDRY_16) {
  11786. static struct pci_device_id dma_wait_state_chipsets[] = {
  11787. { PCI_DEVICE(PCI_VENDOR_ID_APPLE,
  11788. PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
  11789. { },
  11790. };
  11791. /* DMA test passed without adjusting DMA boundary,
  11792. * now look for chipsets that are known to expose the
  11793. * DMA bug without failing the test.
  11794. */
  11795. if (pci_dev_present(dma_wait_state_chipsets)) {
  11796. tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
  11797. tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
  11798. } else {
  11799. /* Safe to use the calculated DMA boundary. */
  11800. tp->dma_rwctrl = saved_dma_rwctrl;
  11801. }
  11802. tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
  11803. }
  11804. out:
  11805. pci_free_consistent(tp->pdev, TEST_BUFFER_SIZE, buf, buf_dma);
  11806. out_nofree:
  11807. return ret;
  11808. }
  11809. static void __devinit tg3_init_link_config(struct tg3 *tp)
  11810. {
  11811. tp->link_config.advertising =
  11812. (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
  11813. ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
  11814. ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full |
  11815. ADVERTISED_Autoneg | ADVERTISED_MII);
  11816. tp->link_config.speed = SPEED_INVALID;
  11817. tp->link_config.duplex = DUPLEX_INVALID;
  11818. tp->link_config.autoneg = AUTONEG_ENABLE;
  11819. tp->link_config.active_speed = SPEED_INVALID;
  11820. tp->link_config.active_duplex = DUPLEX_INVALID;
  11821. tp->link_config.orig_speed = SPEED_INVALID;
  11822. tp->link_config.orig_duplex = DUPLEX_INVALID;
  11823. tp->link_config.orig_autoneg = AUTONEG_INVALID;
  11824. }
  11825. static void __devinit tg3_init_bufmgr_config(struct tg3 *tp)
  11826. {
  11827. if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS) {
  11828. tp->bufmgr_config.mbuf_read_dma_low_water =
  11829. DEFAULT_MB_RDMA_LOW_WATER_5705;
  11830. tp->bufmgr_config.mbuf_mac_rx_low_water =
  11831. DEFAULT_MB_MACRX_LOW_WATER_57765;
  11832. tp->bufmgr_config.mbuf_high_water =
  11833. DEFAULT_MB_HIGH_WATER_57765;
  11834. tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
  11835. DEFAULT_MB_RDMA_LOW_WATER_5705;
  11836. tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
  11837. DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
  11838. tp->bufmgr_config.mbuf_high_water_jumbo =
  11839. DEFAULT_MB_HIGH_WATER_JUMBO_57765;
  11840. } else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
  11841. tp->bufmgr_config.mbuf_read_dma_low_water =
  11842. DEFAULT_MB_RDMA_LOW_WATER_5705;
  11843. tp->bufmgr_config.mbuf_mac_rx_low_water =
  11844. DEFAULT_MB_MACRX_LOW_WATER_5705;
  11845. tp->bufmgr_config.mbuf_high_water =
  11846. DEFAULT_MB_HIGH_WATER_5705;
  11847. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
  11848. tp->bufmgr_config.mbuf_mac_rx_low_water =
  11849. DEFAULT_MB_MACRX_LOW_WATER_5906;
  11850. tp->bufmgr_config.mbuf_high_water =
  11851. DEFAULT_MB_HIGH_WATER_5906;
  11852. }
  11853. tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
  11854. DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
  11855. tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
  11856. DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
  11857. tp->bufmgr_config.mbuf_high_water_jumbo =
  11858. DEFAULT_MB_HIGH_WATER_JUMBO_5780;
  11859. } else {
  11860. tp->bufmgr_config.mbuf_read_dma_low_water =
  11861. DEFAULT_MB_RDMA_LOW_WATER;
  11862. tp->bufmgr_config.mbuf_mac_rx_low_water =
  11863. DEFAULT_MB_MACRX_LOW_WATER;
  11864. tp->bufmgr_config.mbuf_high_water =
  11865. DEFAULT_MB_HIGH_WATER;
  11866. tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
  11867. DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
  11868. tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
  11869. DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
  11870. tp->bufmgr_config.mbuf_high_water_jumbo =
  11871. DEFAULT_MB_HIGH_WATER_JUMBO;
  11872. }
  11873. tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
  11874. tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
  11875. }
  11876. static char * __devinit tg3_phy_string(struct tg3 *tp)
  11877. {
  11878. switch (tp->phy_id & TG3_PHY_ID_MASK) {
  11879. case TG3_PHY_ID_BCM5400: return "5400";
  11880. case TG3_PHY_ID_BCM5401: return "5401";
  11881. case TG3_PHY_ID_BCM5411: return "5411";
  11882. case TG3_PHY_ID_BCM5701: return "5701";
  11883. case TG3_PHY_ID_BCM5703: return "5703";
  11884. case TG3_PHY_ID_BCM5704: return "5704";
  11885. case TG3_PHY_ID_BCM5705: return "5705";
  11886. case TG3_PHY_ID_BCM5750: return "5750";
  11887. case TG3_PHY_ID_BCM5752: return "5752";
  11888. case TG3_PHY_ID_BCM5714: return "5714";
  11889. case TG3_PHY_ID_BCM5780: return "5780";
  11890. case TG3_PHY_ID_BCM5755: return "5755";
  11891. case TG3_PHY_ID_BCM5787: return "5787";
  11892. case TG3_PHY_ID_BCM5784: return "5784";
  11893. case TG3_PHY_ID_BCM5756: return "5722/5756";
  11894. case TG3_PHY_ID_BCM5906: return "5906";
  11895. case TG3_PHY_ID_BCM5761: return "5761";
  11896. case TG3_PHY_ID_BCM5718C: return "5718C";
  11897. case TG3_PHY_ID_BCM5718S: return "5718S";
  11898. case TG3_PHY_ID_BCM57765: return "57765";
  11899. case TG3_PHY_ID_BCM5719C: return "5719C";
  11900. case TG3_PHY_ID_BCM8002: return "8002/serdes";
  11901. case 0: return "serdes";
  11902. default: return "unknown";
  11903. }
  11904. }
  11905. static char * __devinit tg3_bus_string(struct tg3 *tp, char *str)
  11906. {
  11907. if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
  11908. strcpy(str, "PCI Express");
  11909. return str;
  11910. } else if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
  11911. u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
  11912. strcpy(str, "PCIX:");
  11913. if ((clock_ctrl == 7) ||
  11914. ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
  11915. GRC_MISC_CFG_BOARD_ID_5704CIOBE))
  11916. strcat(str, "133MHz");
  11917. else if (clock_ctrl == 0)
  11918. strcat(str, "33MHz");
  11919. else if (clock_ctrl == 2)
  11920. strcat(str, "50MHz");
  11921. else if (clock_ctrl == 4)
  11922. strcat(str, "66MHz");
  11923. else if (clock_ctrl == 6)
  11924. strcat(str, "100MHz");
  11925. } else {
  11926. strcpy(str, "PCI:");
  11927. if (tp->tg3_flags & TG3_FLAG_PCI_HIGH_SPEED)
  11928. strcat(str, "66MHz");
  11929. else
  11930. strcat(str, "33MHz");
  11931. }
  11932. if (tp->tg3_flags & TG3_FLAG_PCI_32BIT)
  11933. strcat(str, ":32-bit");
  11934. else
  11935. strcat(str, ":64-bit");
  11936. return str;
  11937. }
  11938. static struct pci_dev * __devinit tg3_find_peer(struct tg3 *tp)
  11939. {
  11940. struct pci_dev *peer;
  11941. unsigned int func, devnr = tp->pdev->devfn & ~7;
  11942. for (func = 0; func < 8; func++) {
  11943. peer = pci_get_slot(tp->pdev->bus, devnr | func);
  11944. if (peer && peer != tp->pdev)
  11945. break;
  11946. pci_dev_put(peer);
  11947. }
  11948. /* 5704 can be configured in single-port mode, set peer to
  11949. * tp->pdev in that case.
  11950. */
  11951. if (!peer) {
  11952. peer = tp->pdev;
  11953. return peer;
  11954. }
  11955. /*
  11956. * We don't need to keep the refcount elevated; there's no way
  11957. * to remove one half of this device without removing the other
  11958. */
  11959. pci_dev_put(peer);
  11960. return peer;
  11961. }
  11962. static void __devinit tg3_init_coal(struct tg3 *tp)
  11963. {
  11964. struct ethtool_coalesce *ec = &tp->coal;
  11965. memset(ec, 0, sizeof(*ec));
  11966. ec->cmd = ETHTOOL_GCOALESCE;
  11967. ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
  11968. ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
  11969. ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
  11970. ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
  11971. ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
  11972. ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
  11973. ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
  11974. ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
  11975. ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
  11976. if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
  11977. HOSTCC_MODE_CLRTICK_TXBD)) {
  11978. ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
  11979. ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
  11980. ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
  11981. ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
  11982. }
  11983. if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
  11984. ec->rx_coalesce_usecs_irq = 0;
  11985. ec->tx_coalesce_usecs_irq = 0;
  11986. ec->stats_block_coalesce_usecs = 0;
  11987. }
  11988. }
  11989. static const struct net_device_ops tg3_netdev_ops = {
  11990. .ndo_open = tg3_open,
  11991. .ndo_stop = tg3_close,
  11992. .ndo_start_xmit = tg3_start_xmit,
  11993. .ndo_get_stats64 = tg3_get_stats64,
  11994. .ndo_validate_addr = eth_validate_addr,
  11995. .ndo_set_multicast_list = tg3_set_rx_mode,
  11996. .ndo_set_mac_address = tg3_set_mac_addr,
  11997. .ndo_do_ioctl = tg3_ioctl,
  11998. .ndo_tx_timeout = tg3_tx_timeout,
  11999. .ndo_change_mtu = tg3_change_mtu,
  12000. #if TG3_VLAN_TAG_USED
  12001. .ndo_vlan_rx_register = tg3_vlan_rx_register,
  12002. #endif
  12003. #ifdef CONFIG_NET_POLL_CONTROLLER
  12004. .ndo_poll_controller = tg3_poll_controller,
  12005. #endif
  12006. };
  12007. static const struct net_device_ops tg3_netdev_ops_dma_bug = {
  12008. .ndo_open = tg3_open,
  12009. .ndo_stop = tg3_close,
  12010. .ndo_start_xmit = tg3_start_xmit_dma_bug,
  12011. .ndo_get_stats64 = tg3_get_stats64,
  12012. .ndo_validate_addr = eth_validate_addr,
  12013. .ndo_set_multicast_list = tg3_set_rx_mode,
  12014. .ndo_set_mac_address = tg3_set_mac_addr,
  12015. .ndo_do_ioctl = tg3_ioctl,
  12016. .ndo_tx_timeout = tg3_tx_timeout,
  12017. .ndo_change_mtu = tg3_change_mtu,
  12018. #if TG3_VLAN_TAG_USED
  12019. .ndo_vlan_rx_register = tg3_vlan_rx_register,
  12020. #endif
  12021. #ifdef CONFIG_NET_POLL_CONTROLLER
  12022. .ndo_poll_controller = tg3_poll_controller,
  12023. #endif
  12024. };
  12025. static int __devinit tg3_init_one(struct pci_dev *pdev,
  12026. const struct pci_device_id *ent)
  12027. {
  12028. struct net_device *dev;
  12029. struct tg3 *tp;
  12030. int i, err, pm_cap;
  12031. u32 sndmbx, rcvmbx, intmbx;
  12032. char str[40];
  12033. u64 dma_mask, persist_dma_mask;
  12034. printk_once(KERN_INFO "%s\n", version);
  12035. err = pci_enable_device(pdev);
  12036. if (err) {
  12037. dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
  12038. return err;
  12039. }
  12040. err = pci_request_regions(pdev, DRV_MODULE_NAME);
  12041. if (err) {
  12042. dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
  12043. goto err_out_disable_pdev;
  12044. }
  12045. pci_set_master(pdev);
  12046. /* Find power-management capability. */
  12047. pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
  12048. if (pm_cap == 0) {
  12049. dev_err(&pdev->dev,
  12050. "Cannot find Power Management capability, aborting\n");
  12051. err = -EIO;
  12052. goto err_out_free_res;
  12053. }
  12054. dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
  12055. if (!dev) {
  12056. dev_err(&pdev->dev, "Etherdev alloc failed, aborting\n");
  12057. err = -ENOMEM;
  12058. goto err_out_free_res;
  12059. }
  12060. SET_NETDEV_DEV(dev, &pdev->dev);
  12061. #if TG3_VLAN_TAG_USED
  12062. dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
  12063. #endif
  12064. tp = netdev_priv(dev);
  12065. tp->pdev = pdev;
  12066. tp->dev = dev;
  12067. tp->pm_cap = pm_cap;
  12068. tp->rx_mode = TG3_DEF_RX_MODE;
  12069. tp->tx_mode = TG3_DEF_TX_MODE;
  12070. if (tg3_debug > 0)
  12071. tp->msg_enable = tg3_debug;
  12072. else
  12073. tp->msg_enable = TG3_DEF_MSG_ENABLE;
  12074. /* The word/byte swap controls here control register access byte
  12075. * swapping. DMA data byte swapping is controlled in the GRC_MODE
  12076. * setting below.
  12077. */
  12078. tp->misc_host_ctrl =
  12079. MISC_HOST_CTRL_MASK_PCI_INT |
  12080. MISC_HOST_CTRL_WORD_SWAP |
  12081. MISC_HOST_CTRL_INDIR_ACCESS |
  12082. MISC_HOST_CTRL_PCISTATE_RW;
  12083. /* The NONFRM (non-frame) byte/word swap controls take effect
  12084. * on descriptor entries, anything which isn't packet data.
  12085. *
  12086. * The StrongARM chips on the board (one for tx, one for rx)
  12087. * are running in big-endian mode.
  12088. */
  12089. tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
  12090. GRC_MODE_WSWAP_NONFRM_DATA);
  12091. #ifdef __BIG_ENDIAN
  12092. tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
  12093. #endif
  12094. spin_lock_init(&tp->lock);
  12095. spin_lock_init(&tp->indirect_lock);
  12096. INIT_WORK(&tp->reset_task, tg3_reset_task);
  12097. tp->regs = pci_ioremap_bar(pdev, BAR_0);
  12098. if (!tp->regs) {
  12099. dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
  12100. err = -ENOMEM;
  12101. goto err_out_free_dev;
  12102. }
  12103. tg3_init_link_config(tp);
  12104. tp->rx_pending = TG3_DEF_RX_RING_PENDING;
  12105. tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
  12106. dev->ethtool_ops = &tg3_ethtool_ops;
  12107. dev->watchdog_timeo = TG3_TX_TIMEOUT;
  12108. dev->irq = pdev->irq;
  12109. err = tg3_get_invariants(tp);
  12110. if (err) {
  12111. dev_err(&pdev->dev,
  12112. "Problem fetching invariants of chip, aborting\n");
  12113. goto err_out_iounmap;
  12114. }
  12115. if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) &&
  12116. GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
  12117. GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5719)
  12118. dev->netdev_ops = &tg3_netdev_ops;
  12119. else
  12120. dev->netdev_ops = &tg3_netdev_ops_dma_bug;
  12121. /* The EPB bridge inside 5714, 5715, and 5780 and any
  12122. * device behind the EPB cannot support DMA addresses > 40-bit.
  12123. * On 64-bit systems with IOMMU, use 40-bit dma_mask.
  12124. * On 64-bit systems without IOMMU, use 64-bit dma_mask and
  12125. * do DMA address check in tg3_start_xmit().
  12126. */
  12127. if (tp->tg3_flags2 & TG3_FLG2_IS_5788)
  12128. persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
  12129. else if (tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG) {
  12130. persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
  12131. #ifdef CONFIG_HIGHMEM
  12132. dma_mask = DMA_BIT_MASK(64);
  12133. #endif
  12134. } else
  12135. persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
  12136. /* Configure DMA attributes. */
  12137. if (dma_mask > DMA_BIT_MASK(32)) {
  12138. err = pci_set_dma_mask(pdev, dma_mask);
  12139. if (!err) {
  12140. dev->features |= NETIF_F_HIGHDMA;
  12141. err = pci_set_consistent_dma_mask(pdev,
  12142. persist_dma_mask);
  12143. if (err < 0) {
  12144. dev_err(&pdev->dev, "Unable to obtain 64 bit "
  12145. "DMA for consistent allocations\n");
  12146. goto err_out_iounmap;
  12147. }
  12148. }
  12149. }
  12150. if (err || dma_mask == DMA_BIT_MASK(32)) {
  12151. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  12152. if (err) {
  12153. dev_err(&pdev->dev,
  12154. "No usable DMA configuration, aborting\n");
  12155. goto err_out_iounmap;
  12156. }
  12157. }
  12158. tg3_init_bufmgr_config(tp);
  12159. /* Selectively allow TSO based on operating conditions */
  12160. if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO) ||
  12161. (tp->fw_needed && !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)))
  12162. tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
  12163. else {
  12164. tp->tg3_flags2 &= ~(TG3_FLG2_TSO_CAPABLE | TG3_FLG2_TSO_BUG);
  12165. tp->fw_needed = NULL;
  12166. }
  12167. if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
  12168. tp->fw_needed = FIRMWARE_TG3;
  12169. /* TSO is on by default on chips that support hardware TSO.
  12170. * Firmware TSO on older chips gives lower performance, so it
  12171. * is off by default, but can be enabled using ethtool.
  12172. */
  12173. if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO) &&
  12174. (dev->features & NETIF_F_IP_CSUM)) {
  12175. dev->features |= NETIF_F_TSO;
  12176. vlan_features_add(dev, NETIF_F_TSO);
  12177. }
  12178. if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_2) ||
  12179. (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3)) {
  12180. if (dev->features & NETIF_F_IPV6_CSUM) {
  12181. dev->features |= NETIF_F_TSO6;
  12182. vlan_features_add(dev, NETIF_F_TSO6);
  12183. }
  12184. if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) ||
  12185. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
  12186. (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
  12187. GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
  12188. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
  12189. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
  12190. dev->features |= NETIF_F_TSO_ECN;
  12191. vlan_features_add(dev, NETIF_F_TSO_ECN);
  12192. }
  12193. }
  12194. if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
  12195. !(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) &&
  12196. !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
  12197. tp->tg3_flags2 |= TG3_FLG2_MAX_RXPEND_64;
  12198. tp->rx_pending = 63;
  12199. }
  12200. err = tg3_get_device_address(tp);
  12201. if (err) {
  12202. dev_err(&pdev->dev,
  12203. "Could not obtain valid ethernet address, aborting\n");
  12204. goto err_out_iounmap;
  12205. }
  12206. if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
  12207. tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
  12208. if (!tp->aperegs) {
  12209. dev_err(&pdev->dev,
  12210. "Cannot map APE registers, aborting\n");
  12211. err = -ENOMEM;
  12212. goto err_out_iounmap;
  12213. }
  12214. tg3_ape_lock_init(tp);
  12215. if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF)
  12216. tg3_read_dash_ver(tp);
  12217. }
  12218. /*
  12219. * Reset chip in case UNDI or EFI driver did not shutdown
  12220. * DMA self test will enable WDMAC and we'll see (spurious)
  12221. * pending DMA on the PCI bus at that point.
  12222. */
  12223. if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
  12224. (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
  12225. tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
  12226. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  12227. }
  12228. err = tg3_test_dma(tp);
  12229. if (err) {
  12230. dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
  12231. goto err_out_apeunmap;
  12232. }
  12233. /* flow control autonegotiation is default behavior */
  12234. tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
  12235. tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
  12236. intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
  12237. rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
  12238. sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
  12239. for (i = 0; i < tp->irq_max; i++) {
  12240. struct tg3_napi *tnapi = &tp->napi[i];
  12241. tnapi->tp = tp;
  12242. tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
  12243. tnapi->int_mbox = intmbx;
  12244. if (i < 4)
  12245. intmbx += 0x8;
  12246. else
  12247. intmbx += 0x4;
  12248. tnapi->consmbox = rcvmbx;
  12249. tnapi->prodmbox = sndmbx;
  12250. if (i)
  12251. tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
  12252. else
  12253. tnapi->coal_now = HOSTCC_MODE_NOW;
  12254. if (!(tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX))
  12255. break;
  12256. /*
  12257. * If we support MSIX, we'll be using RSS. If we're using
  12258. * RSS, the first vector only handles link interrupts and the
  12259. * remaining vectors handle rx and tx interrupts. Reuse the
  12260. * mailbox values for the next iteration. The values we setup
  12261. * above are still useful for the single vectored mode.
  12262. */
  12263. if (!i)
  12264. continue;
  12265. rcvmbx += 0x8;
  12266. if (sndmbx & 0x4)
  12267. sndmbx -= 0x4;
  12268. else
  12269. sndmbx += 0xc;
  12270. }
  12271. tg3_init_coal(tp);
  12272. pci_set_drvdata(pdev, dev);
  12273. err = register_netdev(dev);
  12274. if (err) {
  12275. dev_err(&pdev->dev, "Cannot register net device, aborting\n");
  12276. goto err_out_apeunmap;
  12277. }
  12278. netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
  12279. tp->board_part_number,
  12280. tp->pci_chip_rev_id,
  12281. tg3_bus_string(tp, str),
  12282. dev->dev_addr);
  12283. if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
  12284. struct phy_device *phydev;
  12285. phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
  12286. netdev_info(dev,
  12287. "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
  12288. phydev->drv->name, dev_name(&phydev->dev));
  12289. } else {
  12290. char *ethtype;
  12291. if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
  12292. ethtype = "10/100Base-TX";
  12293. else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
  12294. ethtype = "1000Base-SX";
  12295. else
  12296. ethtype = "10/100/1000Base-T";
  12297. netdev_info(dev, "attached PHY is %s (%s Ethernet) "
  12298. "(WireSpeed[%d])\n", tg3_phy_string(tp), ethtype,
  12299. (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0);
  12300. }
  12301. netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
  12302. (tp->tg3_flags & TG3_FLAG_RX_CHECKSUMS) != 0,
  12303. (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) != 0,
  12304. (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
  12305. (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0,
  12306. (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) != 0);
  12307. netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
  12308. tp->dma_rwctrl,
  12309. pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
  12310. ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
  12311. return 0;
  12312. err_out_apeunmap:
  12313. if (tp->aperegs) {
  12314. iounmap(tp->aperegs);
  12315. tp->aperegs = NULL;
  12316. }
  12317. err_out_iounmap:
  12318. if (tp->regs) {
  12319. iounmap(tp->regs);
  12320. tp->regs = NULL;
  12321. }
  12322. err_out_free_dev:
  12323. free_netdev(dev);
  12324. err_out_free_res:
  12325. pci_release_regions(pdev);
  12326. err_out_disable_pdev:
  12327. pci_disable_device(pdev);
  12328. pci_set_drvdata(pdev, NULL);
  12329. return err;
  12330. }
  12331. static void __devexit tg3_remove_one(struct pci_dev *pdev)
  12332. {
  12333. struct net_device *dev = pci_get_drvdata(pdev);
  12334. if (dev) {
  12335. struct tg3 *tp = netdev_priv(dev);
  12336. if (tp->fw)
  12337. release_firmware(tp->fw);
  12338. flush_scheduled_work();
  12339. if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
  12340. tg3_phy_fini(tp);
  12341. tg3_mdio_fini(tp);
  12342. }
  12343. unregister_netdev(dev);
  12344. if (tp->aperegs) {
  12345. iounmap(tp->aperegs);
  12346. tp->aperegs = NULL;
  12347. }
  12348. if (tp->regs) {
  12349. iounmap(tp->regs);
  12350. tp->regs = NULL;
  12351. }
  12352. free_netdev(dev);
  12353. pci_release_regions(pdev);
  12354. pci_disable_device(pdev);
  12355. pci_set_drvdata(pdev, NULL);
  12356. }
  12357. }
  12358. static int tg3_suspend(struct pci_dev *pdev, pm_message_t state)
  12359. {
  12360. struct net_device *dev = pci_get_drvdata(pdev);
  12361. struct tg3 *tp = netdev_priv(dev);
  12362. pci_power_t target_state;
  12363. int err;
  12364. /* PCI register 4 needs to be saved whether netif_running() or not.
  12365. * MSI address and data need to be saved if using MSI and
  12366. * netif_running().
  12367. */
  12368. pci_save_state(pdev);
  12369. if (!netif_running(dev))
  12370. return 0;
  12371. flush_scheduled_work();
  12372. tg3_phy_stop(tp);
  12373. tg3_netif_stop(tp);
  12374. del_timer_sync(&tp->timer);
  12375. tg3_full_lock(tp, 1);
  12376. tg3_disable_ints(tp);
  12377. tg3_full_unlock(tp);
  12378. netif_device_detach(dev);
  12379. tg3_full_lock(tp, 0);
  12380. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  12381. tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
  12382. tg3_full_unlock(tp);
  12383. target_state = pdev->pm_cap ? pci_target_state(pdev) : PCI_D3hot;
  12384. err = tg3_set_power_state(tp, target_state);
  12385. if (err) {
  12386. int err2;
  12387. tg3_full_lock(tp, 0);
  12388. tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
  12389. err2 = tg3_restart_hw(tp, 1);
  12390. if (err2)
  12391. goto out;
  12392. tp->timer.expires = jiffies + tp->timer_offset;
  12393. add_timer(&tp->timer);
  12394. netif_device_attach(dev);
  12395. tg3_netif_start(tp);
  12396. out:
  12397. tg3_full_unlock(tp);
  12398. if (!err2)
  12399. tg3_phy_start(tp);
  12400. }
  12401. return err;
  12402. }
  12403. static int tg3_resume(struct pci_dev *pdev)
  12404. {
  12405. struct net_device *dev = pci_get_drvdata(pdev);
  12406. struct tg3 *tp = netdev_priv(dev);
  12407. int err;
  12408. pci_restore_state(tp->pdev);
  12409. if (!netif_running(dev))
  12410. return 0;
  12411. err = tg3_set_power_state(tp, PCI_D0);
  12412. if (err)
  12413. return err;
  12414. netif_device_attach(dev);
  12415. tg3_full_lock(tp, 0);
  12416. tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
  12417. err = tg3_restart_hw(tp, 1);
  12418. if (err)
  12419. goto out;
  12420. tp->timer.expires = jiffies + tp->timer_offset;
  12421. add_timer(&tp->timer);
  12422. tg3_netif_start(tp);
  12423. out:
  12424. tg3_full_unlock(tp);
  12425. if (!err)
  12426. tg3_phy_start(tp);
  12427. return err;
  12428. }
  12429. static struct pci_driver tg3_driver = {
  12430. .name = DRV_MODULE_NAME,
  12431. .id_table = tg3_pci_tbl,
  12432. .probe = tg3_init_one,
  12433. .remove = __devexit_p(tg3_remove_one),
  12434. .suspend = tg3_suspend,
  12435. .resume = tg3_resume
  12436. };
  12437. static int __init tg3_init(void)
  12438. {
  12439. return pci_register_driver(&tg3_driver);
  12440. }
  12441. static void __exit tg3_cleanup(void)
  12442. {
  12443. pci_unregister_driver(&tg3_driver);
  12444. }
  12445. module_init(tg3_init);
  12446. module_exit(tg3_cleanup);