tg3.c 428 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586
  1. /*
  2. * tg3.c: Broadcom Tigon3 ethernet driver.
  3. *
  4. * Copyright (C) 2001, 2002, 2003, 2004 David S. Miller (davem@redhat.com)
  5. * Copyright (C) 2001, 2002, 2003 Jeff Garzik (jgarzik@pobox.com)
  6. * Copyright (C) 2004 Sun Microsystems Inc.
  7. * Copyright (C) 2005-2012 Broadcom Corporation.
  8. *
  9. * Firmware is:
  10. * Derived from proprietary unpublished source code,
  11. * Copyright (C) 2000-2003 Broadcom Corporation.
  12. *
  13. * Permission is hereby granted for the distribution of this firmware
  14. * data in hexadecimal or equivalent format, provided this copyright
  15. * notice is accompanying it.
  16. */
  17. #include <linux/module.h>
  18. #include <linux/moduleparam.h>
  19. #include <linux/stringify.h>
  20. #include <linux/kernel.h>
  21. #include <linux/types.h>
  22. #include <linux/compiler.h>
  23. #include <linux/slab.h>
  24. #include <linux/delay.h>
  25. #include <linux/in.h>
  26. #include <linux/init.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/ioport.h>
  29. #include <linux/pci.h>
  30. #include <linux/netdevice.h>
  31. #include <linux/etherdevice.h>
  32. #include <linux/skbuff.h>
  33. #include <linux/ethtool.h>
  34. #include <linux/mdio.h>
  35. #include <linux/mii.h>
  36. #include <linux/phy.h>
  37. #include <linux/brcmphy.h>
  38. #include <linux/if_vlan.h>
  39. #include <linux/ip.h>
  40. #include <linux/tcp.h>
  41. #include <linux/workqueue.h>
  42. #include <linux/prefetch.h>
  43. #include <linux/dma-mapping.h>
  44. #include <linux/firmware.h>
  45. #include <linux/hwmon.h>
  46. #include <linux/hwmon-sysfs.h>
  47. #include <net/checksum.h>
  48. #include <net/ip.h>
  49. #include <linux/io.h>
  50. #include <asm/byteorder.h>
  51. #include <linux/uaccess.h>
  52. #ifdef CONFIG_SPARC
  53. #include <asm/idprom.h>
  54. #include <asm/prom.h>
  55. #endif
  56. #define BAR_0 0
  57. #define BAR_2 2
  58. #include "tg3.h"
  59. /* Functions & macros to verify TG3_FLAGS types */
  60. static inline int _tg3_flag(enum TG3_FLAGS flag, unsigned long *bits)
  61. {
  62. return test_bit(flag, bits);
  63. }
  64. static inline void _tg3_flag_set(enum TG3_FLAGS flag, unsigned long *bits)
  65. {
  66. set_bit(flag, bits);
  67. }
  68. static inline void _tg3_flag_clear(enum TG3_FLAGS flag, unsigned long *bits)
  69. {
  70. clear_bit(flag, bits);
  71. }
  72. #define tg3_flag(tp, flag) \
  73. _tg3_flag(TG3_FLAG_##flag, (tp)->tg3_flags)
  74. #define tg3_flag_set(tp, flag) \
  75. _tg3_flag_set(TG3_FLAG_##flag, (tp)->tg3_flags)
  76. #define tg3_flag_clear(tp, flag) \
  77. _tg3_flag_clear(TG3_FLAG_##flag, (tp)->tg3_flags)
  78. #define DRV_MODULE_NAME "tg3"
  79. #define TG3_MAJ_NUM 3
  80. #define TG3_MIN_NUM 126
  81. #define DRV_MODULE_VERSION \
  82. __stringify(TG3_MAJ_NUM) "." __stringify(TG3_MIN_NUM)
  83. #define DRV_MODULE_RELDATE "November 05, 2012"
  84. #define RESET_KIND_SHUTDOWN 0
  85. #define RESET_KIND_INIT 1
  86. #define RESET_KIND_SUSPEND 2
  87. #define TG3_DEF_RX_MODE 0
  88. #define TG3_DEF_TX_MODE 0
  89. #define TG3_DEF_MSG_ENABLE \
  90. (NETIF_MSG_DRV | \
  91. NETIF_MSG_PROBE | \
  92. NETIF_MSG_LINK | \
  93. NETIF_MSG_TIMER | \
  94. NETIF_MSG_IFDOWN | \
  95. NETIF_MSG_IFUP | \
  96. NETIF_MSG_RX_ERR | \
  97. NETIF_MSG_TX_ERR)
  98. #define TG3_GRC_LCLCTL_PWRSW_DELAY 100
  99. /* length of time before we decide the hardware is borked,
  100. * and dev->tx_timeout() should be called to fix the problem
  101. */
  102. #define TG3_TX_TIMEOUT (5 * HZ)
  103. /* hardware minimum and maximum for a single frame's data payload */
  104. #define TG3_MIN_MTU 60
  105. #define TG3_MAX_MTU(tp) \
  106. (tg3_flag(tp, JUMBO_CAPABLE) ? 9000 : 1500)
  107. /* These numbers seem to be hard coded in the NIC firmware somehow.
  108. * You can't change the ring sizes, but you can change where you place
  109. * them in the NIC onboard memory.
  110. */
  111. #define TG3_RX_STD_RING_SIZE(tp) \
  112. (tg3_flag(tp, LRG_PROD_RING_CAP) ? \
  113. TG3_RX_STD_MAX_SIZE_5717 : TG3_RX_STD_MAX_SIZE_5700)
  114. #define TG3_DEF_RX_RING_PENDING 200
  115. #define TG3_RX_JMB_RING_SIZE(tp) \
  116. (tg3_flag(tp, LRG_PROD_RING_CAP) ? \
  117. TG3_RX_JMB_MAX_SIZE_5717 : TG3_RX_JMB_MAX_SIZE_5700)
  118. #define TG3_DEF_RX_JUMBO_RING_PENDING 100
  119. /* Do not place this n-ring entries value into the tp struct itself,
  120. * we really want to expose these constants to GCC so that modulo et
  121. * al. operations are done with shifts and masks instead of with
  122. * hw multiply/modulo instructions. Another solution would be to
  123. * replace things like '% foo' with '& (foo - 1)'.
  124. */
  125. #define TG3_TX_RING_SIZE 512
  126. #define TG3_DEF_TX_RING_PENDING (TG3_TX_RING_SIZE - 1)
  127. #define TG3_RX_STD_RING_BYTES(tp) \
  128. (sizeof(struct tg3_rx_buffer_desc) * TG3_RX_STD_RING_SIZE(tp))
  129. #define TG3_RX_JMB_RING_BYTES(tp) \
  130. (sizeof(struct tg3_ext_rx_buffer_desc) * TG3_RX_JMB_RING_SIZE(tp))
  131. #define TG3_RX_RCB_RING_BYTES(tp) \
  132. (sizeof(struct tg3_rx_buffer_desc) * (tp->rx_ret_ring_mask + 1))
  133. #define TG3_TX_RING_BYTES (sizeof(struct tg3_tx_buffer_desc) * \
  134. TG3_TX_RING_SIZE)
  135. #define NEXT_TX(N) (((N) + 1) & (TG3_TX_RING_SIZE - 1))
  136. #define TG3_DMA_BYTE_ENAB 64
  137. #define TG3_RX_STD_DMA_SZ 1536
  138. #define TG3_RX_JMB_DMA_SZ 9046
  139. #define TG3_RX_DMA_TO_MAP_SZ(x) ((x) + TG3_DMA_BYTE_ENAB)
  140. #define TG3_RX_STD_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_STD_DMA_SZ)
  141. #define TG3_RX_JMB_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_JMB_DMA_SZ)
  142. #define TG3_RX_STD_BUFF_RING_SIZE(tp) \
  143. (sizeof(struct ring_info) * TG3_RX_STD_RING_SIZE(tp))
  144. #define TG3_RX_JMB_BUFF_RING_SIZE(tp) \
  145. (sizeof(struct ring_info) * TG3_RX_JMB_RING_SIZE(tp))
  146. /* Due to a hardware bug, the 5701 can only DMA to memory addresses
  147. * that are at least dword aligned when used in PCIX mode. The driver
  148. * works around this bug by double copying the packet. This workaround
  149. * is built into the normal double copy length check for efficiency.
  150. *
  151. * However, the double copy is only necessary on those architectures
  152. * where unaligned memory accesses are inefficient. For those architectures
  153. * where unaligned memory accesses incur little penalty, we can reintegrate
  154. * the 5701 in the normal rx path. Doing so saves a device structure
  155. * dereference by hardcoding the double copy threshold in place.
  156. */
  157. #define TG3_RX_COPY_THRESHOLD 256
  158. #if NET_IP_ALIGN == 0 || defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
  159. #define TG3_RX_COPY_THRESH(tp) TG3_RX_COPY_THRESHOLD
  160. #else
  161. #define TG3_RX_COPY_THRESH(tp) ((tp)->rx_copy_thresh)
  162. #endif
  163. #if (NET_IP_ALIGN != 0)
  164. #define TG3_RX_OFFSET(tp) ((tp)->rx_offset)
  165. #else
  166. #define TG3_RX_OFFSET(tp) (NET_SKB_PAD)
  167. #endif
  168. /* minimum number of free TX descriptors required to wake up TX process */
  169. #define TG3_TX_WAKEUP_THRESH(tnapi) ((tnapi)->tx_pending / 4)
  170. #define TG3_TX_BD_DMA_MAX_2K 2048
  171. #define TG3_TX_BD_DMA_MAX_4K 4096
  172. #define TG3_RAW_IP_ALIGN 2
  173. #define TG3_FW_UPDATE_TIMEOUT_SEC 5
  174. #define TG3_FW_UPDATE_FREQ_SEC (TG3_FW_UPDATE_TIMEOUT_SEC / 2)
  175. #define FIRMWARE_TG3 "tigon/tg3.bin"
  176. #define FIRMWARE_TG3TSO "tigon/tg3_tso.bin"
  177. #define FIRMWARE_TG3TSO5 "tigon/tg3_tso5.bin"
  178. static char version[] __devinitdata =
  179. DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")";
  180. MODULE_AUTHOR("David S. Miller (davem@redhat.com) and Jeff Garzik (jgarzik@pobox.com)");
  181. MODULE_DESCRIPTION("Broadcom Tigon3 ethernet driver");
  182. MODULE_LICENSE("GPL");
  183. MODULE_VERSION(DRV_MODULE_VERSION);
  184. MODULE_FIRMWARE(FIRMWARE_TG3);
  185. MODULE_FIRMWARE(FIRMWARE_TG3TSO);
  186. MODULE_FIRMWARE(FIRMWARE_TG3TSO5);
  187. static int tg3_debug = -1; /* -1 == use TG3_DEF_MSG_ENABLE as value */
  188. module_param(tg3_debug, int, 0);
  189. MODULE_PARM_DESC(tg3_debug, "Tigon3 bitmapped debugging message enable value");
  190. static DEFINE_PCI_DEVICE_TABLE(tg3_pci_tbl) = {
  191. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5700)},
  192. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5701)},
  193. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702)},
  194. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703)},
  195. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704)},
  196. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702FE)},
  197. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705)},
  198. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705_2)},
  199. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M)},
  200. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M_2)},
  201. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702X)},
  202. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703X)},
  203. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S)},
  204. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702A3)},
  205. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703A3)},
  206. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5782)},
  207. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5788)},
  208. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5789)},
  209. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901)},
  210. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901_2)},
  211. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S_2)},
  212. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705F)},
  213. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5721)},
  214. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5722)},
  215. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751)},
  216. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751M)},
  217. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751F)},
  218. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752)},
  219. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752M)},
  220. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753)},
  221. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753M)},
  222. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753F)},
  223. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754)},
  224. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754M)},
  225. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755)},
  226. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755M)},
  227. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5756)},
  228. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5786)},
  229. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787)},
  230. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787M)},
  231. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787F)},
  232. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714)},
  233. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714S)},
  234. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715)},
  235. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715S)},
  236. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780)},
  237. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780S)},
  238. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5781)},
  239. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906)},
  240. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906M)},
  241. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5784)},
  242. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5764)},
  243. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5723)},
  244. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761)},
  245. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761E)},
  246. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761S)},
  247. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761SE)},
  248. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_G)},
  249. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_F)},
  250. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780)},
  251. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57760)},
  252. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57790)},
  253. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57788)},
  254. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717)},
  255. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717_C)},
  256. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5718)},
  257. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57781)},
  258. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57785)},
  259. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57761)},
  260. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57765)},
  261. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57791)},
  262. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57795)},
  263. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5719)},
  264. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5720)},
  265. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57762)},
  266. {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9DXX)},
  267. {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9MXX)},
  268. {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1000)},
  269. {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1001)},
  270. {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1003)},
  271. {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC9100)},
  272. {PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_TIGON3)},
  273. {PCI_DEVICE(0x10cf, 0x11a2)}, /* Fujitsu 1000base-SX with BCM5703SKHB */
  274. {}
  275. };
  276. MODULE_DEVICE_TABLE(pci, tg3_pci_tbl);
  277. static const struct {
  278. const char string[ETH_GSTRING_LEN];
  279. } ethtool_stats_keys[] = {
  280. { "rx_octets" },
  281. { "rx_fragments" },
  282. { "rx_ucast_packets" },
  283. { "rx_mcast_packets" },
  284. { "rx_bcast_packets" },
  285. { "rx_fcs_errors" },
  286. { "rx_align_errors" },
  287. { "rx_xon_pause_rcvd" },
  288. { "rx_xoff_pause_rcvd" },
  289. { "rx_mac_ctrl_rcvd" },
  290. { "rx_xoff_entered" },
  291. { "rx_frame_too_long_errors" },
  292. { "rx_jabbers" },
  293. { "rx_undersize_packets" },
  294. { "rx_in_length_errors" },
  295. { "rx_out_length_errors" },
  296. { "rx_64_or_less_octet_packets" },
  297. { "rx_65_to_127_octet_packets" },
  298. { "rx_128_to_255_octet_packets" },
  299. { "rx_256_to_511_octet_packets" },
  300. { "rx_512_to_1023_octet_packets" },
  301. { "rx_1024_to_1522_octet_packets" },
  302. { "rx_1523_to_2047_octet_packets" },
  303. { "rx_2048_to_4095_octet_packets" },
  304. { "rx_4096_to_8191_octet_packets" },
  305. { "rx_8192_to_9022_octet_packets" },
  306. { "tx_octets" },
  307. { "tx_collisions" },
  308. { "tx_xon_sent" },
  309. { "tx_xoff_sent" },
  310. { "tx_flow_control" },
  311. { "tx_mac_errors" },
  312. { "tx_single_collisions" },
  313. { "tx_mult_collisions" },
  314. { "tx_deferred" },
  315. { "tx_excessive_collisions" },
  316. { "tx_late_collisions" },
  317. { "tx_collide_2times" },
  318. { "tx_collide_3times" },
  319. { "tx_collide_4times" },
  320. { "tx_collide_5times" },
  321. { "tx_collide_6times" },
  322. { "tx_collide_7times" },
  323. { "tx_collide_8times" },
  324. { "tx_collide_9times" },
  325. { "tx_collide_10times" },
  326. { "tx_collide_11times" },
  327. { "tx_collide_12times" },
  328. { "tx_collide_13times" },
  329. { "tx_collide_14times" },
  330. { "tx_collide_15times" },
  331. { "tx_ucast_packets" },
  332. { "tx_mcast_packets" },
  333. { "tx_bcast_packets" },
  334. { "tx_carrier_sense_errors" },
  335. { "tx_discards" },
  336. { "tx_errors" },
  337. { "dma_writeq_full" },
  338. { "dma_write_prioq_full" },
  339. { "rxbds_empty" },
  340. { "rx_discards" },
  341. { "rx_errors" },
  342. { "rx_threshold_hit" },
  343. { "dma_readq_full" },
  344. { "dma_read_prioq_full" },
  345. { "tx_comp_queue_full" },
  346. { "ring_set_send_prod_index" },
  347. { "ring_status_update" },
  348. { "nic_irqs" },
  349. { "nic_avoided_irqs" },
  350. { "nic_tx_threshold_hit" },
  351. { "mbuf_lwm_thresh_hit" },
  352. };
  353. #define TG3_NUM_STATS ARRAY_SIZE(ethtool_stats_keys)
  354. static const struct {
  355. const char string[ETH_GSTRING_LEN];
  356. } ethtool_test_keys[] = {
  357. { "nvram test (online) " },
  358. { "link test (online) " },
  359. { "register test (offline)" },
  360. { "memory test (offline)" },
  361. { "mac loopback test (offline)" },
  362. { "phy loopback test (offline)" },
  363. { "ext loopback test (offline)" },
  364. { "interrupt test (offline)" },
  365. };
  366. #define TG3_NUM_TEST ARRAY_SIZE(ethtool_test_keys)
  367. static void tg3_write32(struct tg3 *tp, u32 off, u32 val)
  368. {
  369. writel(val, tp->regs + off);
  370. }
  371. static u32 tg3_read32(struct tg3 *tp, u32 off)
  372. {
  373. return readl(tp->regs + off);
  374. }
  375. static void tg3_ape_write32(struct tg3 *tp, u32 off, u32 val)
  376. {
  377. writel(val, tp->aperegs + off);
  378. }
  379. static u32 tg3_ape_read32(struct tg3 *tp, u32 off)
  380. {
  381. return readl(tp->aperegs + off);
  382. }
  383. static void tg3_write_indirect_reg32(struct tg3 *tp, u32 off, u32 val)
  384. {
  385. unsigned long flags;
  386. spin_lock_irqsave(&tp->indirect_lock, flags);
  387. pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
  388. pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
  389. spin_unlock_irqrestore(&tp->indirect_lock, flags);
  390. }
  391. static void tg3_write_flush_reg32(struct tg3 *tp, u32 off, u32 val)
  392. {
  393. writel(val, tp->regs + off);
  394. readl(tp->regs + off);
  395. }
  396. static u32 tg3_read_indirect_reg32(struct tg3 *tp, u32 off)
  397. {
  398. unsigned long flags;
  399. u32 val;
  400. spin_lock_irqsave(&tp->indirect_lock, flags);
  401. pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
  402. pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
  403. spin_unlock_irqrestore(&tp->indirect_lock, flags);
  404. return val;
  405. }
  406. static void tg3_write_indirect_mbox(struct tg3 *tp, u32 off, u32 val)
  407. {
  408. unsigned long flags;
  409. if (off == (MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW)) {
  410. pci_write_config_dword(tp->pdev, TG3PCI_RCV_RET_RING_CON_IDX +
  411. TG3_64BIT_REG_LOW, val);
  412. return;
  413. }
  414. if (off == TG3_RX_STD_PROD_IDX_REG) {
  415. pci_write_config_dword(tp->pdev, TG3PCI_STD_RING_PROD_IDX +
  416. TG3_64BIT_REG_LOW, val);
  417. return;
  418. }
  419. spin_lock_irqsave(&tp->indirect_lock, flags);
  420. pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
  421. pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
  422. spin_unlock_irqrestore(&tp->indirect_lock, flags);
  423. /* In indirect mode when disabling interrupts, we also need
  424. * to clear the interrupt bit in the GRC local ctrl register.
  425. */
  426. if ((off == (MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW)) &&
  427. (val == 0x1)) {
  428. pci_write_config_dword(tp->pdev, TG3PCI_MISC_LOCAL_CTRL,
  429. tp->grc_local_ctrl|GRC_LCLCTRL_CLEARINT);
  430. }
  431. }
  432. static u32 tg3_read_indirect_mbox(struct tg3 *tp, u32 off)
  433. {
  434. unsigned long flags;
  435. u32 val;
  436. spin_lock_irqsave(&tp->indirect_lock, flags);
  437. pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
  438. pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
  439. spin_unlock_irqrestore(&tp->indirect_lock, flags);
  440. return val;
  441. }
  442. /* usec_wait specifies the wait time in usec when writing to certain registers
  443. * where it is unsafe to read back the register without some delay.
  444. * GRC_LOCAL_CTRL is one example if the GPIOs are toggled to switch power.
  445. * TG3PCI_CLOCK_CTRL is another example if the clock frequencies are changed.
  446. */
  447. static void _tw32_flush(struct tg3 *tp, u32 off, u32 val, u32 usec_wait)
  448. {
  449. if (tg3_flag(tp, PCIX_TARGET_HWBUG) || tg3_flag(tp, ICH_WORKAROUND))
  450. /* Non-posted methods */
  451. tp->write32(tp, off, val);
  452. else {
  453. /* Posted method */
  454. tg3_write32(tp, off, val);
  455. if (usec_wait)
  456. udelay(usec_wait);
  457. tp->read32(tp, off);
  458. }
  459. /* Wait again after the read for the posted method to guarantee that
  460. * the wait time is met.
  461. */
  462. if (usec_wait)
  463. udelay(usec_wait);
  464. }
  465. static inline void tw32_mailbox_flush(struct tg3 *tp, u32 off, u32 val)
  466. {
  467. tp->write32_mbox(tp, off, val);
  468. if (!tg3_flag(tp, MBOX_WRITE_REORDER) && !tg3_flag(tp, ICH_WORKAROUND))
  469. tp->read32_mbox(tp, off);
  470. }
  471. static void tg3_write32_tx_mbox(struct tg3 *tp, u32 off, u32 val)
  472. {
  473. void __iomem *mbox = tp->regs + off;
  474. writel(val, mbox);
  475. if (tg3_flag(tp, TXD_MBOX_HWBUG))
  476. writel(val, mbox);
  477. if (tg3_flag(tp, MBOX_WRITE_REORDER))
  478. readl(mbox);
  479. }
  480. static u32 tg3_read32_mbox_5906(struct tg3 *tp, u32 off)
  481. {
  482. return readl(tp->regs + off + GRCMBOX_BASE);
  483. }
  484. static void tg3_write32_mbox_5906(struct tg3 *tp, u32 off, u32 val)
  485. {
  486. writel(val, tp->regs + off + GRCMBOX_BASE);
  487. }
  488. #define tw32_mailbox(reg, val) tp->write32_mbox(tp, reg, val)
  489. #define tw32_mailbox_f(reg, val) tw32_mailbox_flush(tp, (reg), (val))
  490. #define tw32_rx_mbox(reg, val) tp->write32_rx_mbox(tp, reg, val)
  491. #define tw32_tx_mbox(reg, val) tp->write32_tx_mbox(tp, reg, val)
  492. #define tr32_mailbox(reg) tp->read32_mbox(tp, reg)
  493. #define tw32(reg, val) tp->write32(tp, reg, val)
  494. #define tw32_f(reg, val) _tw32_flush(tp, (reg), (val), 0)
  495. #define tw32_wait_f(reg, val, us) _tw32_flush(tp, (reg), (val), (us))
  496. #define tr32(reg) tp->read32(tp, reg)
  497. static void tg3_write_mem(struct tg3 *tp, u32 off, u32 val)
  498. {
  499. unsigned long flags;
  500. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 &&
  501. (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC))
  502. return;
  503. spin_lock_irqsave(&tp->indirect_lock, flags);
  504. if (tg3_flag(tp, SRAM_USE_CONFIG)) {
  505. pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
  506. pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
  507. /* Always leave this as zero. */
  508. pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
  509. } else {
  510. tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
  511. tw32_f(TG3PCI_MEM_WIN_DATA, val);
  512. /* Always leave this as zero. */
  513. tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
  514. }
  515. spin_unlock_irqrestore(&tp->indirect_lock, flags);
  516. }
  517. static void tg3_read_mem(struct tg3 *tp, u32 off, u32 *val)
  518. {
  519. unsigned long flags;
  520. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 &&
  521. (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC)) {
  522. *val = 0;
  523. return;
  524. }
  525. spin_lock_irqsave(&tp->indirect_lock, flags);
  526. if (tg3_flag(tp, SRAM_USE_CONFIG)) {
  527. pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
  528. pci_read_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
  529. /* Always leave this as zero. */
  530. pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
  531. } else {
  532. tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
  533. *val = tr32(TG3PCI_MEM_WIN_DATA);
  534. /* Always leave this as zero. */
  535. tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
  536. }
  537. spin_unlock_irqrestore(&tp->indirect_lock, flags);
  538. }
  539. static void tg3_ape_lock_init(struct tg3 *tp)
  540. {
  541. int i;
  542. u32 regbase, bit;
  543. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
  544. regbase = TG3_APE_LOCK_GRANT;
  545. else
  546. regbase = TG3_APE_PER_LOCK_GRANT;
  547. /* Make sure the driver hasn't any stale locks. */
  548. for (i = TG3_APE_LOCK_PHY0; i <= TG3_APE_LOCK_GPIO; i++) {
  549. switch (i) {
  550. case TG3_APE_LOCK_PHY0:
  551. case TG3_APE_LOCK_PHY1:
  552. case TG3_APE_LOCK_PHY2:
  553. case TG3_APE_LOCK_PHY3:
  554. bit = APE_LOCK_GRANT_DRIVER;
  555. break;
  556. default:
  557. if (!tp->pci_fn)
  558. bit = APE_LOCK_GRANT_DRIVER;
  559. else
  560. bit = 1 << tp->pci_fn;
  561. }
  562. tg3_ape_write32(tp, regbase + 4 * i, bit);
  563. }
  564. }
  565. static int tg3_ape_lock(struct tg3 *tp, int locknum)
  566. {
  567. int i, off;
  568. int ret = 0;
  569. u32 status, req, gnt, bit;
  570. if (!tg3_flag(tp, ENABLE_APE))
  571. return 0;
  572. switch (locknum) {
  573. case TG3_APE_LOCK_GPIO:
  574. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
  575. return 0;
  576. case TG3_APE_LOCK_GRC:
  577. case TG3_APE_LOCK_MEM:
  578. if (!tp->pci_fn)
  579. bit = APE_LOCK_REQ_DRIVER;
  580. else
  581. bit = 1 << tp->pci_fn;
  582. break;
  583. case TG3_APE_LOCK_PHY0:
  584. case TG3_APE_LOCK_PHY1:
  585. case TG3_APE_LOCK_PHY2:
  586. case TG3_APE_LOCK_PHY3:
  587. bit = APE_LOCK_REQ_DRIVER;
  588. break;
  589. default:
  590. return -EINVAL;
  591. }
  592. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
  593. req = TG3_APE_LOCK_REQ;
  594. gnt = TG3_APE_LOCK_GRANT;
  595. } else {
  596. req = TG3_APE_PER_LOCK_REQ;
  597. gnt = TG3_APE_PER_LOCK_GRANT;
  598. }
  599. off = 4 * locknum;
  600. tg3_ape_write32(tp, req + off, bit);
  601. /* Wait for up to 1 millisecond to acquire lock. */
  602. for (i = 0; i < 100; i++) {
  603. status = tg3_ape_read32(tp, gnt + off);
  604. if (status == bit)
  605. break;
  606. udelay(10);
  607. }
  608. if (status != bit) {
  609. /* Revoke the lock request. */
  610. tg3_ape_write32(tp, gnt + off, bit);
  611. ret = -EBUSY;
  612. }
  613. return ret;
  614. }
  615. static void tg3_ape_unlock(struct tg3 *tp, int locknum)
  616. {
  617. u32 gnt, bit;
  618. if (!tg3_flag(tp, ENABLE_APE))
  619. return;
  620. switch (locknum) {
  621. case TG3_APE_LOCK_GPIO:
  622. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
  623. return;
  624. case TG3_APE_LOCK_GRC:
  625. case TG3_APE_LOCK_MEM:
  626. if (!tp->pci_fn)
  627. bit = APE_LOCK_GRANT_DRIVER;
  628. else
  629. bit = 1 << tp->pci_fn;
  630. break;
  631. case TG3_APE_LOCK_PHY0:
  632. case TG3_APE_LOCK_PHY1:
  633. case TG3_APE_LOCK_PHY2:
  634. case TG3_APE_LOCK_PHY3:
  635. bit = APE_LOCK_GRANT_DRIVER;
  636. break;
  637. default:
  638. return;
  639. }
  640. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
  641. gnt = TG3_APE_LOCK_GRANT;
  642. else
  643. gnt = TG3_APE_PER_LOCK_GRANT;
  644. tg3_ape_write32(tp, gnt + 4 * locknum, bit);
  645. }
  646. static int tg3_ape_event_lock(struct tg3 *tp, u32 timeout_us)
  647. {
  648. u32 apedata;
  649. while (timeout_us) {
  650. if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
  651. return -EBUSY;
  652. apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
  653. if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
  654. break;
  655. tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
  656. udelay(10);
  657. timeout_us -= (timeout_us > 10) ? 10 : timeout_us;
  658. }
  659. return timeout_us ? 0 : -EBUSY;
  660. }
  661. static int tg3_ape_wait_for_event(struct tg3 *tp, u32 timeout_us)
  662. {
  663. u32 i, apedata;
  664. for (i = 0; i < timeout_us / 10; i++) {
  665. apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
  666. if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
  667. break;
  668. udelay(10);
  669. }
  670. return i == timeout_us / 10;
  671. }
  672. static int tg3_ape_scratchpad_read(struct tg3 *tp, u32 *data, u32 base_off,
  673. u32 len)
  674. {
  675. int err;
  676. u32 i, bufoff, msgoff, maxlen, apedata;
  677. if (!tg3_flag(tp, APE_HAS_NCSI))
  678. return 0;
  679. apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
  680. if (apedata != APE_SEG_SIG_MAGIC)
  681. return -ENODEV;
  682. apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
  683. if (!(apedata & APE_FW_STATUS_READY))
  684. return -EAGAIN;
  685. bufoff = tg3_ape_read32(tp, TG3_APE_SEG_MSG_BUF_OFF) +
  686. TG3_APE_SHMEM_BASE;
  687. msgoff = bufoff + 2 * sizeof(u32);
  688. maxlen = tg3_ape_read32(tp, TG3_APE_SEG_MSG_BUF_LEN);
  689. while (len) {
  690. u32 length;
  691. /* Cap xfer sizes to scratchpad limits. */
  692. length = (len > maxlen) ? maxlen : len;
  693. len -= length;
  694. apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
  695. if (!(apedata & APE_FW_STATUS_READY))
  696. return -EAGAIN;
  697. /* Wait for up to 1 msec for APE to service previous event. */
  698. err = tg3_ape_event_lock(tp, 1000);
  699. if (err)
  700. return err;
  701. apedata = APE_EVENT_STATUS_DRIVER_EVNT |
  702. APE_EVENT_STATUS_SCRTCHPD_READ |
  703. APE_EVENT_STATUS_EVENT_PENDING;
  704. tg3_ape_write32(tp, TG3_APE_EVENT_STATUS, apedata);
  705. tg3_ape_write32(tp, bufoff, base_off);
  706. tg3_ape_write32(tp, bufoff + sizeof(u32), length);
  707. tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
  708. tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
  709. base_off += length;
  710. if (tg3_ape_wait_for_event(tp, 30000))
  711. return -EAGAIN;
  712. for (i = 0; length; i += 4, length -= 4) {
  713. u32 val = tg3_ape_read32(tp, msgoff + i);
  714. memcpy(data, &val, sizeof(u32));
  715. data++;
  716. }
  717. }
  718. return 0;
  719. }
  720. static int tg3_ape_send_event(struct tg3 *tp, u32 event)
  721. {
  722. int err;
  723. u32 apedata;
  724. apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
  725. if (apedata != APE_SEG_SIG_MAGIC)
  726. return -EAGAIN;
  727. apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
  728. if (!(apedata & APE_FW_STATUS_READY))
  729. return -EAGAIN;
  730. /* Wait for up to 1 millisecond for APE to service previous event. */
  731. err = tg3_ape_event_lock(tp, 1000);
  732. if (err)
  733. return err;
  734. tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
  735. event | APE_EVENT_STATUS_EVENT_PENDING);
  736. tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
  737. tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
  738. return 0;
  739. }
  740. static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
  741. {
  742. u32 event;
  743. u32 apedata;
  744. if (!tg3_flag(tp, ENABLE_APE))
  745. return;
  746. switch (kind) {
  747. case RESET_KIND_INIT:
  748. tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
  749. APE_HOST_SEG_SIG_MAGIC);
  750. tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
  751. APE_HOST_SEG_LEN_MAGIC);
  752. apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
  753. tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
  754. tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
  755. APE_HOST_DRIVER_ID_MAGIC(TG3_MAJ_NUM, TG3_MIN_NUM));
  756. tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
  757. APE_HOST_BEHAV_NO_PHYLOCK);
  758. tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE,
  759. TG3_APE_HOST_DRVR_STATE_START);
  760. event = APE_EVENT_STATUS_STATE_START;
  761. break;
  762. case RESET_KIND_SHUTDOWN:
  763. /* With the interface we are currently using,
  764. * APE does not track driver state. Wiping
  765. * out the HOST SEGMENT SIGNATURE forces
  766. * the APE to assume OS absent status.
  767. */
  768. tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
  769. if (device_may_wakeup(&tp->pdev->dev) &&
  770. tg3_flag(tp, WOL_ENABLE)) {
  771. tg3_ape_write32(tp, TG3_APE_HOST_WOL_SPEED,
  772. TG3_APE_HOST_WOL_SPEED_AUTO);
  773. apedata = TG3_APE_HOST_DRVR_STATE_WOL;
  774. } else
  775. apedata = TG3_APE_HOST_DRVR_STATE_UNLOAD;
  776. tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE, apedata);
  777. event = APE_EVENT_STATUS_STATE_UNLOAD;
  778. break;
  779. case RESET_KIND_SUSPEND:
  780. event = APE_EVENT_STATUS_STATE_SUSPEND;
  781. break;
  782. default:
  783. return;
  784. }
  785. event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
  786. tg3_ape_send_event(tp, event);
  787. }
  788. static void tg3_disable_ints(struct tg3 *tp)
  789. {
  790. int i;
  791. tw32(TG3PCI_MISC_HOST_CTRL,
  792. (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
  793. for (i = 0; i < tp->irq_max; i++)
  794. tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
  795. }
  796. static void tg3_enable_ints(struct tg3 *tp)
  797. {
  798. int i;
  799. tp->irq_sync = 0;
  800. wmb();
  801. tw32(TG3PCI_MISC_HOST_CTRL,
  802. (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
  803. tp->coal_now = tp->coalesce_mode | HOSTCC_MODE_ENABLE;
  804. for (i = 0; i < tp->irq_cnt; i++) {
  805. struct tg3_napi *tnapi = &tp->napi[i];
  806. tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
  807. if (tg3_flag(tp, 1SHOT_MSI))
  808. tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
  809. tp->coal_now |= tnapi->coal_now;
  810. }
  811. /* Force an initial interrupt */
  812. if (!tg3_flag(tp, TAGGED_STATUS) &&
  813. (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
  814. tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
  815. else
  816. tw32(HOSTCC_MODE, tp->coal_now);
  817. tp->coal_now &= ~(tp->napi[0].coal_now | tp->napi[1].coal_now);
  818. }
  819. static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
  820. {
  821. struct tg3 *tp = tnapi->tp;
  822. struct tg3_hw_status *sblk = tnapi->hw_status;
  823. unsigned int work_exists = 0;
  824. /* check for phy events */
  825. if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
  826. if (sblk->status & SD_STATUS_LINK_CHG)
  827. work_exists = 1;
  828. }
  829. /* check for TX work to do */
  830. if (sblk->idx[0].tx_consumer != tnapi->tx_cons)
  831. work_exists = 1;
  832. /* check for RX work to do */
  833. if (tnapi->rx_rcb_prod_idx &&
  834. *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
  835. work_exists = 1;
  836. return work_exists;
  837. }
  838. /* tg3_int_reenable
  839. * similar to tg3_enable_ints, but it accurately determines whether there
  840. * is new work pending and can return without flushing the PIO write
  841. * which reenables interrupts
  842. */
  843. static void tg3_int_reenable(struct tg3_napi *tnapi)
  844. {
  845. struct tg3 *tp = tnapi->tp;
  846. tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
  847. mmiowb();
  848. /* When doing tagged status, this work check is unnecessary.
  849. * The last_tag we write above tells the chip which piece of
  850. * work we've completed.
  851. */
  852. if (!tg3_flag(tp, TAGGED_STATUS) && tg3_has_work(tnapi))
  853. tw32(HOSTCC_MODE, tp->coalesce_mode |
  854. HOSTCC_MODE_ENABLE | tnapi->coal_now);
  855. }
  856. static void tg3_switch_clocks(struct tg3 *tp)
  857. {
  858. u32 clock_ctrl;
  859. u32 orig_clock_ctrl;
  860. if (tg3_flag(tp, CPMU_PRESENT) || tg3_flag(tp, 5780_CLASS))
  861. return;
  862. clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
  863. orig_clock_ctrl = clock_ctrl;
  864. clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
  865. CLOCK_CTRL_CLKRUN_OENABLE |
  866. 0x1f);
  867. tp->pci_clock_ctrl = clock_ctrl;
  868. if (tg3_flag(tp, 5705_PLUS)) {
  869. if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
  870. tw32_wait_f(TG3PCI_CLOCK_CTRL,
  871. clock_ctrl | CLOCK_CTRL_625_CORE, 40);
  872. }
  873. } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
  874. tw32_wait_f(TG3PCI_CLOCK_CTRL,
  875. clock_ctrl |
  876. (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
  877. 40);
  878. tw32_wait_f(TG3PCI_CLOCK_CTRL,
  879. clock_ctrl | (CLOCK_CTRL_ALTCLK),
  880. 40);
  881. }
  882. tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
  883. }
  884. #define PHY_BUSY_LOOPS 5000
  885. static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
  886. {
  887. u32 frame_val;
  888. unsigned int loops;
  889. int ret;
  890. if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
  891. tw32_f(MAC_MI_MODE,
  892. (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
  893. udelay(80);
  894. }
  895. tg3_ape_lock(tp, tp->phy_ape_lock);
  896. *val = 0x0;
  897. frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
  898. MI_COM_PHY_ADDR_MASK);
  899. frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
  900. MI_COM_REG_ADDR_MASK);
  901. frame_val |= (MI_COM_CMD_READ | MI_COM_START);
  902. tw32_f(MAC_MI_COM, frame_val);
  903. loops = PHY_BUSY_LOOPS;
  904. while (loops != 0) {
  905. udelay(10);
  906. frame_val = tr32(MAC_MI_COM);
  907. if ((frame_val & MI_COM_BUSY) == 0) {
  908. udelay(5);
  909. frame_val = tr32(MAC_MI_COM);
  910. break;
  911. }
  912. loops -= 1;
  913. }
  914. ret = -EBUSY;
  915. if (loops != 0) {
  916. *val = frame_val & MI_COM_DATA_MASK;
  917. ret = 0;
  918. }
  919. if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
  920. tw32_f(MAC_MI_MODE, tp->mi_mode);
  921. udelay(80);
  922. }
  923. tg3_ape_unlock(tp, tp->phy_ape_lock);
  924. return ret;
  925. }
  926. static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
  927. {
  928. u32 frame_val;
  929. unsigned int loops;
  930. int ret;
  931. if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
  932. (reg == MII_CTRL1000 || reg == MII_TG3_AUX_CTRL))
  933. return 0;
  934. if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
  935. tw32_f(MAC_MI_MODE,
  936. (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
  937. udelay(80);
  938. }
  939. tg3_ape_lock(tp, tp->phy_ape_lock);
  940. frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
  941. MI_COM_PHY_ADDR_MASK);
  942. frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
  943. MI_COM_REG_ADDR_MASK);
  944. frame_val |= (val & MI_COM_DATA_MASK);
  945. frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
  946. tw32_f(MAC_MI_COM, frame_val);
  947. loops = PHY_BUSY_LOOPS;
  948. while (loops != 0) {
  949. udelay(10);
  950. frame_val = tr32(MAC_MI_COM);
  951. if ((frame_val & MI_COM_BUSY) == 0) {
  952. udelay(5);
  953. frame_val = tr32(MAC_MI_COM);
  954. break;
  955. }
  956. loops -= 1;
  957. }
  958. ret = -EBUSY;
  959. if (loops != 0)
  960. ret = 0;
  961. if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
  962. tw32_f(MAC_MI_MODE, tp->mi_mode);
  963. udelay(80);
  964. }
  965. tg3_ape_unlock(tp, tp->phy_ape_lock);
  966. return ret;
  967. }
  968. static int tg3_phy_cl45_write(struct tg3 *tp, u32 devad, u32 addr, u32 val)
  969. {
  970. int err;
  971. err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
  972. if (err)
  973. goto done;
  974. err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
  975. if (err)
  976. goto done;
  977. err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
  978. MII_TG3_MMD_CTRL_DATA_NOINC | devad);
  979. if (err)
  980. goto done;
  981. err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, val);
  982. done:
  983. return err;
  984. }
  985. static int tg3_phy_cl45_read(struct tg3 *tp, u32 devad, u32 addr, u32 *val)
  986. {
  987. int err;
  988. err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
  989. if (err)
  990. goto done;
  991. err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
  992. if (err)
  993. goto done;
  994. err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
  995. MII_TG3_MMD_CTRL_DATA_NOINC | devad);
  996. if (err)
  997. goto done;
  998. err = tg3_readphy(tp, MII_TG3_MMD_ADDRESS, val);
  999. done:
  1000. return err;
  1001. }
  1002. static int tg3_phydsp_read(struct tg3 *tp, u32 reg, u32 *val)
  1003. {
  1004. int err;
  1005. err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
  1006. if (!err)
  1007. err = tg3_readphy(tp, MII_TG3_DSP_RW_PORT, val);
  1008. return err;
  1009. }
  1010. static int tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
  1011. {
  1012. int err;
  1013. err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
  1014. if (!err)
  1015. err = tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
  1016. return err;
  1017. }
  1018. static int tg3_phy_auxctl_read(struct tg3 *tp, int reg, u32 *val)
  1019. {
  1020. int err;
  1021. err = tg3_writephy(tp, MII_TG3_AUX_CTRL,
  1022. (reg << MII_TG3_AUXCTL_MISC_RDSEL_SHIFT) |
  1023. MII_TG3_AUXCTL_SHDWSEL_MISC);
  1024. if (!err)
  1025. err = tg3_readphy(tp, MII_TG3_AUX_CTRL, val);
  1026. return err;
  1027. }
  1028. static int tg3_phy_auxctl_write(struct tg3 *tp, int reg, u32 set)
  1029. {
  1030. if (reg == MII_TG3_AUXCTL_SHDWSEL_MISC)
  1031. set |= MII_TG3_AUXCTL_MISC_WREN;
  1032. return tg3_writephy(tp, MII_TG3_AUX_CTRL, set | reg);
  1033. }
  1034. #define TG3_PHY_AUXCTL_SMDSP_ENABLE(tp) \
  1035. tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL, \
  1036. MII_TG3_AUXCTL_ACTL_SMDSP_ENA | \
  1037. MII_TG3_AUXCTL_ACTL_TX_6DB)
  1038. #define TG3_PHY_AUXCTL_SMDSP_DISABLE(tp) \
  1039. tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL, \
  1040. MII_TG3_AUXCTL_ACTL_TX_6DB);
  1041. static int tg3_bmcr_reset(struct tg3 *tp)
  1042. {
  1043. u32 phy_control;
  1044. int limit, err;
  1045. /* OK, reset it, and poll the BMCR_RESET bit until it
  1046. * clears or we time out.
  1047. */
  1048. phy_control = BMCR_RESET;
  1049. err = tg3_writephy(tp, MII_BMCR, phy_control);
  1050. if (err != 0)
  1051. return -EBUSY;
  1052. limit = 5000;
  1053. while (limit--) {
  1054. err = tg3_readphy(tp, MII_BMCR, &phy_control);
  1055. if (err != 0)
  1056. return -EBUSY;
  1057. if ((phy_control & BMCR_RESET) == 0) {
  1058. udelay(40);
  1059. break;
  1060. }
  1061. udelay(10);
  1062. }
  1063. if (limit < 0)
  1064. return -EBUSY;
  1065. return 0;
  1066. }
  1067. static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
  1068. {
  1069. struct tg3 *tp = bp->priv;
  1070. u32 val;
  1071. spin_lock_bh(&tp->lock);
  1072. if (tg3_readphy(tp, reg, &val))
  1073. val = -EIO;
  1074. spin_unlock_bh(&tp->lock);
  1075. return val;
  1076. }
  1077. static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
  1078. {
  1079. struct tg3 *tp = bp->priv;
  1080. u32 ret = 0;
  1081. spin_lock_bh(&tp->lock);
  1082. if (tg3_writephy(tp, reg, val))
  1083. ret = -EIO;
  1084. spin_unlock_bh(&tp->lock);
  1085. return ret;
  1086. }
  1087. static int tg3_mdio_reset(struct mii_bus *bp)
  1088. {
  1089. return 0;
  1090. }
  1091. static void tg3_mdio_config_5785(struct tg3 *tp)
  1092. {
  1093. u32 val;
  1094. struct phy_device *phydev;
  1095. phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
  1096. switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
  1097. case PHY_ID_BCM50610:
  1098. case PHY_ID_BCM50610M:
  1099. val = MAC_PHYCFG2_50610_LED_MODES;
  1100. break;
  1101. case PHY_ID_BCMAC131:
  1102. val = MAC_PHYCFG2_AC131_LED_MODES;
  1103. break;
  1104. case PHY_ID_RTL8211C:
  1105. val = MAC_PHYCFG2_RTL8211C_LED_MODES;
  1106. break;
  1107. case PHY_ID_RTL8201E:
  1108. val = MAC_PHYCFG2_RTL8201E_LED_MODES;
  1109. break;
  1110. default:
  1111. return;
  1112. }
  1113. if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
  1114. tw32(MAC_PHYCFG2, val);
  1115. val = tr32(MAC_PHYCFG1);
  1116. val &= ~(MAC_PHYCFG1_RGMII_INT |
  1117. MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
  1118. val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
  1119. tw32(MAC_PHYCFG1, val);
  1120. return;
  1121. }
  1122. if (!tg3_flag(tp, RGMII_INBAND_DISABLE))
  1123. val |= MAC_PHYCFG2_EMODE_MASK_MASK |
  1124. MAC_PHYCFG2_FMODE_MASK_MASK |
  1125. MAC_PHYCFG2_GMODE_MASK_MASK |
  1126. MAC_PHYCFG2_ACT_MASK_MASK |
  1127. MAC_PHYCFG2_QUAL_MASK_MASK |
  1128. MAC_PHYCFG2_INBAND_ENABLE;
  1129. tw32(MAC_PHYCFG2, val);
  1130. val = tr32(MAC_PHYCFG1);
  1131. val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
  1132. MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
  1133. if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
  1134. if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
  1135. val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
  1136. if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
  1137. val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
  1138. }
  1139. val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
  1140. MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
  1141. tw32(MAC_PHYCFG1, val);
  1142. val = tr32(MAC_EXT_RGMII_MODE);
  1143. val &= ~(MAC_RGMII_MODE_RX_INT_B |
  1144. MAC_RGMII_MODE_RX_QUALITY |
  1145. MAC_RGMII_MODE_RX_ACTIVITY |
  1146. MAC_RGMII_MODE_RX_ENG_DET |
  1147. MAC_RGMII_MODE_TX_ENABLE |
  1148. MAC_RGMII_MODE_TX_LOWPWR |
  1149. MAC_RGMII_MODE_TX_RESET);
  1150. if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
  1151. if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
  1152. val |= MAC_RGMII_MODE_RX_INT_B |
  1153. MAC_RGMII_MODE_RX_QUALITY |
  1154. MAC_RGMII_MODE_RX_ACTIVITY |
  1155. MAC_RGMII_MODE_RX_ENG_DET;
  1156. if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
  1157. val |= MAC_RGMII_MODE_TX_ENABLE |
  1158. MAC_RGMII_MODE_TX_LOWPWR |
  1159. MAC_RGMII_MODE_TX_RESET;
  1160. }
  1161. tw32(MAC_EXT_RGMII_MODE, val);
  1162. }
  1163. static void tg3_mdio_start(struct tg3 *tp)
  1164. {
  1165. tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
  1166. tw32_f(MAC_MI_MODE, tp->mi_mode);
  1167. udelay(80);
  1168. if (tg3_flag(tp, MDIOBUS_INITED) &&
  1169. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
  1170. tg3_mdio_config_5785(tp);
  1171. }
  1172. static int tg3_mdio_init(struct tg3 *tp)
  1173. {
  1174. int i;
  1175. u32 reg;
  1176. struct phy_device *phydev;
  1177. if (tg3_flag(tp, 5717_PLUS)) {
  1178. u32 is_serdes;
  1179. tp->phy_addr = tp->pci_fn + 1;
  1180. if (tp->pci_chip_rev_id != CHIPREV_ID_5717_A0)
  1181. is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
  1182. else
  1183. is_serdes = tr32(TG3_CPMU_PHY_STRAP) &
  1184. TG3_CPMU_PHY_STRAP_IS_SERDES;
  1185. if (is_serdes)
  1186. tp->phy_addr += 7;
  1187. } else
  1188. tp->phy_addr = TG3_PHY_MII_ADDR;
  1189. tg3_mdio_start(tp);
  1190. if (!tg3_flag(tp, USE_PHYLIB) || tg3_flag(tp, MDIOBUS_INITED))
  1191. return 0;
  1192. tp->mdio_bus = mdiobus_alloc();
  1193. if (tp->mdio_bus == NULL)
  1194. return -ENOMEM;
  1195. tp->mdio_bus->name = "tg3 mdio bus";
  1196. snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
  1197. (tp->pdev->bus->number << 8) | tp->pdev->devfn);
  1198. tp->mdio_bus->priv = tp;
  1199. tp->mdio_bus->parent = &tp->pdev->dev;
  1200. tp->mdio_bus->read = &tg3_mdio_read;
  1201. tp->mdio_bus->write = &tg3_mdio_write;
  1202. tp->mdio_bus->reset = &tg3_mdio_reset;
  1203. tp->mdio_bus->phy_mask = ~(1 << TG3_PHY_MII_ADDR);
  1204. tp->mdio_bus->irq = &tp->mdio_irq[0];
  1205. for (i = 0; i < PHY_MAX_ADDR; i++)
  1206. tp->mdio_bus->irq[i] = PHY_POLL;
  1207. /* The bus registration will look for all the PHYs on the mdio bus.
  1208. * Unfortunately, it does not ensure the PHY is powered up before
  1209. * accessing the PHY ID registers. A chip reset is the
  1210. * quickest way to bring the device back to an operational state..
  1211. */
  1212. if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
  1213. tg3_bmcr_reset(tp);
  1214. i = mdiobus_register(tp->mdio_bus);
  1215. if (i) {
  1216. dev_warn(&tp->pdev->dev, "mdiobus_reg failed (0x%x)\n", i);
  1217. mdiobus_free(tp->mdio_bus);
  1218. return i;
  1219. }
  1220. phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
  1221. if (!phydev || !phydev->drv) {
  1222. dev_warn(&tp->pdev->dev, "No PHY devices\n");
  1223. mdiobus_unregister(tp->mdio_bus);
  1224. mdiobus_free(tp->mdio_bus);
  1225. return -ENODEV;
  1226. }
  1227. switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
  1228. case PHY_ID_BCM57780:
  1229. phydev->interface = PHY_INTERFACE_MODE_GMII;
  1230. phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
  1231. break;
  1232. case PHY_ID_BCM50610:
  1233. case PHY_ID_BCM50610M:
  1234. phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
  1235. PHY_BRCM_RX_REFCLK_UNUSED |
  1236. PHY_BRCM_DIS_TXCRXC_NOENRGY |
  1237. PHY_BRCM_AUTO_PWRDWN_ENABLE;
  1238. if (tg3_flag(tp, RGMII_INBAND_DISABLE))
  1239. phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
  1240. if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
  1241. phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
  1242. if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
  1243. phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
  1244. /* fallthru */
  1245. case PHY_ID_RTL8211C:
  1246. phydev->interface = PHY_INTERFACE_MODE_RGMII;
  1247. break;
  1248. case PHY_ID_RTL8201E:
  1249. case PHY_ID_BCMAC131:
  1250. phydev->interface = PHY_INTERFACE_MODE_MII;
  1251. phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
  1252. tp->phy_flags |= TG3_PHYFLG_IS_FET;
  1253. break;
  1254. }
  1255. tg3_flag_set(tp, MDIOBUS_INITED);
  1256. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
  1257. tg3_mdio_config_5785(tp);
  1258. return 0;
  1259. }
  1260. static void tg3_mdio_fini(struct tg3 *tp)
  1261. {
  1262. if (tg3_flag(tp, MDIOBUS_INITED)) {
  1263. tg3_flag_clear(tp, MDIOBUS_INITED);
  1264. mdiobus_unregister(tp->mdio_bus);
  1265. mdiobus_free(tp->mdio_bus);
  1266. }
  1267. }
  1268. /* tp->lock is held. */
  1269. static inline void tg3_generate_fw_event(struct tg3 *tp)
  1270. {
  1271. u32 val;
  1272. val = tr32(GRC_RX_CPU_EVENT);
  1273. val |= GRC_RX_CPU_DRIVER_EVENT;
  1274. tw32_f(GRC_RX_CPU_EVENT, val);
  1275. tp->last_event_jiffies = jiffies;
  1276. }
  1277. #define TG3_FW_EVENT_TIMEOUT_USEC 2500
  1278. /* tp->lock is held. */
  1279. static void tg3_wait_for_event_ack(struct tg3 *tp)
  1280. {
  1281. int i;
  1282. unsigned int delay_cnt;
  1283. long time_remain;
  1284. /* If enough time has passed, no wait is necessary. */
  1285. time_remain = (long)(tp->last_event_jiffies + 1 +
  1286. usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
  1287. (long)jiffies;
  1288. if (time_remain < 0)
  1289. return;
  1290. /* Check if we can shorten the wait time. */
  1291. delay_cnt = jiffies_to_usecs(time_remain);
  1292. if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
  1293. delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
  1294. delay_cnt = (delay_cnt >> 3) + 1;
  1295. for (i = 0; i < delay_cnt; i++) {
  1296. if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
  1297. break;
  1298. udelay(8);
  1299. }
  1300. }
  1301. /* tp->lock is held. */
  1302. static void tg3_phy_gather_ump_data(struct tg3 *tp, u32 *data)
  1303. {
  1304. u32 reg, val;
  1305. val = 0;
  1306. if (!tg3_readphy(tp, MII_BMCR, &reg))
  1307. val = reg << 16;
  1308. if (!tg3_readphy(tp, MII_BMSR, &reg))
  1309. val |= (reg & 0xffff);
  1310. *data++ = val;
  1311. val = 0;
  1312. if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
  1313. val = reg << 16;
  1314. if (!tg3_readphy(tp, MII_LPA, &reg))
  1315. val |= (reg & 0xffff);
  1316. *data++ = val;
  1317. val = 0;
  1318. if (!(tp->phy_flags & TG3_PHYFLG_MII_SERDES)) {
  1319. if (!tg3_readphy(tp, MII_CTRL1000, &reg))
  1320. val = reg << 16;
  1321. if (!tg3_readphy(tp, MII_STAT1000, &reg))
  1322. val |= (reg & 0xffff);
  1323. }
  1324. *data++ = val;
  1325. if (!tg3_readphy(tp, MII_PHYADDR, &reg))
  1326. val = reg << 16;
  1327. else
  1328. val = 0;
  1329. *data++ = val;
  1330. }
  1331. /* tp->lock is held. */
  1332. static void tg3_ump_link_report(struct tg3 *tp)
  1333. {
  1334. u32 data[4];
  1335. if (!tg3_flag(tp, 5780_CLASS) || !tg3_flag(tp, ENABLE_ASF))
  1336. return;
  1337. tg3_phy_gather_ump_data(tp, data);
  1338. tg3_wait_for_event_ack(tp);
  1339. tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
  1340. tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
  1341. tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x0, data[0]);
  1342. tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x4, data[1]);
  1343. tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x8, data[2]);
  1344. tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0xc, data[3]);
  1345. tg3_generate_fw_event(tp);
  1346. }
  1347. /* tp->lock is held. */
  1348. static void tg3_stop_fw(struct tg3 *tp)
  1349. {
  1350. if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
  1351. /* Wait for RX cpu to ACK the previous event. */
  1352. tg3_wait_for_event_ack(tp);
  1353. tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
  1354. tg3_generate_fw_event(tp);
  1355. /* Wait for RX cpu to ACK this event. */
  1356. tg3_wait_for_event_ack(tp);
  1357. }
  1358. }
  1359. /* tp->lock is held. */
  1360. static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
  1361. {
  1362. tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
  1363. NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
  1364. if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
  1365. switch (kind) {
  1366. case RESET_KIND_INIT:
  1367. tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
  1368. DRV_STATE_START);
  1369. break;
  1370. case RESET_KIND_SHUTDOWN:
  1371. tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
  1372. DRV_STATE_UNLOAD);
  1373. break;
  1374. case RESET_KIND_SUSPEND:
  1375. tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
  1376. DRV_STATE_SUSPEND);
  1377. break;
  1378. default:
  1379. break;
  1380. }
  1381. }
  1382. if (kind == RESET_KIND_INIT ||
  1383. kind == RESET_KIND_SUSPEND)
  1384. tg3_ape_driver_state_change(tp, kind);
  1385. }
  1386. /* tp->lock is held. */
  1387. static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
  1388. {
  1389. if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
  1390. switch (kind) {
  1391. case RESET_KIND_INIT:
  1392. tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
  1393. DRV_STATE_START_DONE);
  1394. break;
  1395. case RESET_KIND_SHUTDOWN:
  1396. tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
  1397. DRV_STATE_UNLOAD_DONE);
  1398. break;
  1399. default:
  1400. break;
  1401. }
  1402. }
  1403. if (kind == RESET_KIND_SHUTDOWN)
  1404. tg3_ape_driver_state_change(tp, kind);
  1405. }
  1406. /* tp->lock is held. */
  1407. static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
  1408. {
  1409. if (tg3_flag(tp, ENABLE_ASF)) {
  1410. switch (kind) {
  1411. case RESET_KIND_INIT:
  1412. tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
  1413. DRV_STATE_START);
  1414. break;
  1415. case RESET_KIND_SHUTDOWN:
  1416. tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
  1417. DRV_STATE_UNLOAD);
  1418. break;
  1419. case RESET_KIND_SUSPEND:
  1420. tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
  1421. DRV_STATE_SUSPEND);
  1422. break;
  1423. default:
  1424. break;
  1425. }
  1426. }
  1427. }
  1428. static int tg3_poll_fw(struct tg3 *tp)
  1429. {
  1430. int i;
  1431. u32 val;
  1432. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
  1433. /* Wait up to 20ms for init done. */
  1434. for (i = 0; i < 200; i++) {
  1435. if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
  1436. return 0;
  1437. udelay(100);
  1438. }
  1439. return -ENODEV;
  1440. }
  1441. /* Wait for firmware initialization to complete. */
  1442. for (i = 0; i < 100000; i++) {
  1443. tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
  1444. if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
  1445. break;
  1446. udelay(10);
  1447. }
  1448. /* Chip might not be fitted with firmware. Some Sun onboard
  1449. * parts are configured like that. So don't signal the timeout
  1450. * of the above loop as an error, but do report the lack of
  1451. * running firmware once.
  1452. */
  1453. if (i >= 100000 && !tg3_flag(tp, NO_FWARE_REPORTED)) {
  1454. tg3_flag_set(tp, NO_FWARE_REPORTED);
  1455. netdev_info(tp->dev, "No firmware running\n");
  1456. }
  1457. if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
  1458. /* The 57765 A0 needs a little more
  1459. * time to do some important work.
  1460. */
  1461. mdelay(10);
  1462. }
  1463. return 0;
  1464. }
  1465. static void tg3_link_report(struct tg3 *tp)
  1466. {
  1467. if (!netif_carrier_ok(tp->dev)) {
  1468. netif_info(tp, link, tp->dev, "Link is down\n");
  1469. tg3_ump_link_report(tp);
  1470. } else if (netif_msg_link(tp)) {
  1471. netdev_info(tp->dev, "Link is up at %d Mbps, %s duplex\n",
  1472. (tp->link_config.active_speed == SPEED_1000 ?
  1473. 1000 :
  1474. (tp->link_config.active_speed == SPEED_100 ?
  1475. 100 : 10)),
  1476. (tp->link_config.active_duplex == DUPLEX_FULL ?
  1477. "full" : "half"));
  1478. netdev_info(tp->dev, "Flow control is %s for TX and %s for RX\n",
  1479. (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
  1480. "on" : "off",
  1481. (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
  1482. "on" : "off");
  1483. if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
  1484. netdev_info(tp->dev, "EEE is %s\n",
  1485. tp->setlpicnt ? "enabled" : "disabled");
  1486. tg3_ump_link_report(tp);
  1487. }
  1488. }
  1489. static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
  1490. {
  1491. u16 miireg;
  1492. if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
  1493. miireg = ADVERTISE_1000XPAUSE;
  1494. else if (flow_ctrl & FLOW_CTRL_TX)
  1495. miireg = ADVERTISE_1000XPSE_ASYM;
  1496. else if (flow_ctrl & FLOW_CTRL_RX)
  1497. miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
  1498. else
  1499. miireg = 0;
  1500. return miireg;
  1501. }
  1502. static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
  1503. {
  1504. u8 cap = 0;
  1505. if (lcladv & rmtadv & ADVERTISE_1000XPAUSE) {
  1506. cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
  1507. } else if (lcladv & rmtadv & ADVERTISE_1000XPSE_ASYM) {
  1508. if (lcladv & ADVERTISE_1000XPAUSE)
  1509. cap = FLOW_CTRL_RX;
  1510. if (rmtadv & ADVERTISE_1000XPAUSE)
  1511. cap = FLOW_CTRL_TX;
  1512. }
  1513. return cap;
  1514. }
  1515. static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
  1516. {
  1517. u8 autoneg;
  1518. u8 flowctrl = 0;
  1519. u32 old_rx_mode = tp->rx_mode;
  1520. u32 old_tx_mode = tp->tx_mode;
  1521. if (tg3_flag(tp, USE_PHYLIB))
  1522. autoneg = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]->autoneg;
  1523. else
  1524. autoneg = tp->link_config.autoneg;
  1525. if (autoneg == AUTONEG_ENABLE && tg3_flag(tp, PAUSE_AUTONEG)) {
  1526. if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
  1527. flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
  1528. else
  1529. flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
  1530. } else
  1531. flowctrl = tp->link_config.flowctrl;
  1532. tp->link_config.active_flowctrl = flowctrl;
  1533. if (flowctrl & FLOW_CTRL_RX)
  1534. tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
  1535. else
  1536. tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
  1537. if (old_rx_mode != tp->rx_mode)
  1538. tw32_f(MAC_RX_MODE, tp->rx_mode);
  1539. if (flowctrl & FLOW_CTRL_TX)
  1540. tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
  1541. else
  1542. tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
  1543. if (old_tx_mode != tp->tx_mode)
  1544. tw32_f(MAC_TX_MODE, tp->tx_mode);
  1545. }
  1546. static void tg3_adjust_link(struct net_device *dev)
  1547. {
  1548. u8 oldflowctrl, linkmesg = 0;
  1549. u32 mac_mode, lcl_adv, rmt_adv;
  1550. struct tg3 *tp = netdev_priv(dev);
  1551. struct phy_device *phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
  1552. spin_lock_bh(&tp->lock);
  1553. mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
  1554. MAC_MODE_HALF_DUPLEX);
  1555. oldflowctrl = tp->link_config.active_flowctrl;
  1556. if (phydev->link) {
  1557. lcl_adv = 0;
  1558. rmt_adv = 0;
  1559. if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
  1560. mac_mode |= MAC_MODE_PORT_MODE_MII;
  1561. else if (phydev->speed == SPEED_1000 ||
  1562. GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785)
  1563. mac_mode |= MAC_MODE_PORT_MODE_GMII;
  1564. else
  1565. mac_mode |= MAC_MODE_PORT_MODE_MII;
  1566. if (phydev->duplex == DUPLEX_HALF)
  1567. mac_mode |= MAC_MODE_HALF_DUPLEX;
  1568. else {
  1569. lcl_adv = mii_advertise_flowctrl(
  1570. tp->link_config.flowctrl);
  1571. if (phydev->pause)
  1572. rmt_adv = LPA_PAUSE_CAP;
  1573. if (phydev->asym_pause)
  1574. rmt_adv |= LPA_PAUSE_ASYM;
  1575. }
  1576. tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
  1577. } else
  1578. mac_mode |= MAC_MODE_PORT_MODE_GMII;
  1579. if (mac_mode != tp->mac_mode) {
  1580. tp->mac_mode = mac_mode;
  1581. tw32_f(MAC_MODE, tp->mac_mode);
  1582. udelay(40);
  1583. }
  1584. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
  1585. if (phydev->speed == SPEED_10)
  1586. tw32(MAC_MI_STAT,
  1587. MAC_MI_STAT_10MBPS_MODE |
  1588. MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
  1589. else
  1590. tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
  1591. }
  1592. if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
  1593. tw32(MAC_TX_LENGTHS,
  1594. ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
  1595. (6 << TX_LENGTHS_IPG_SHIFT) |
  1596. (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
  1597. else
  1598. tw32(MAC_TX_LENGTHS,
  1599. ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
  1600. (6 << TX_LENGTHS_IPG_SHIFT) |
  1601. (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
  1602. if (phydev->link != tp->old_link ||
  1603. phydev->speed != tp->link_config.active_speed ||
  1604. phydev->duplex != tp->link_config.active_duplex ||
  1605. oldflowctrl != tp->link_config.active_flowctrl)
  1606. linkmesg = 1;
  1607. tp->old_link = phydev->link;
  1608. tp->link_config.active_speed = phydev->speed;
  1609. tp->link_config.active_duplex = phydev->duplex;
  1610. spin_unlock_bh(&tp->lock);
  1611. if (linkmesg)
  1612. tg3_link_report(tp);
  1613. }
  1614. static int tg3_phy_init(struct tg3 *tp)
  1615. {
  1616. struct phy_device *phydev;
  1617. if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)
  1618. return 0;
  1619. /* Bring the PHY back to a known state. */
  1620. tg3_bmcr_reset(tp);
  1621. phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
  1622. /* Attach the MAC to the PHY. */
  1623. phydev = phy_connect(tp->dev, dev_name(&phydev->dev), tg3_adjust_link,
  1624. phydev->dev_flags, phydev->interface);
  1625. if (IS_ERR(phydev)) {
  1626. dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
  1627. return PTR_ERR(phydev);
  1628. }
  1629. /* Mask with MAC supported features. */
  1630. switch (phydev->interface) {
  1631. case PHY_INTERFACE_MODE_GMII:
  1632. case PHY_INTERFACE_MODE_RGMII:
  1633. if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
  1634. phydev->supported &= (PHY_GBIT_FEATURES |
  1635. SUPPORTED_Pause |
  1636. SUPPORTED_Asym_Pause);
  1637. break;
  1638. }
  1639. /* fallthru */
  1640. case PHY_INTERFACE_MODE_MII:
  1641. phydev->supported &= (PHY_BASIC_FEATURES |
  1642. SUPPORTED_Pause |
  1643. SUPPORTED_Asym_Pause);
  1644. break;
  1645. default:
  1646. phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
  1647. return -EINVAL;
  1648. }
  1649. tp->phy_flags |= TG3_PHYFLG_IS_CONNECTED;
  1650. phydev->advertising = phydev->supported;
  1651. return 0;
  1652. }
  1653. static void tg3_phy_start(struct tg3 *tp)
  1654. {
  1655. struct phy_device *phydev;
  1656. if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
  1657. return;
  1658. phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
  1659. if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
  1660. tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
  1661. phydev->speed = tp->link_config.speed;
  1662. phydev->duplex = tp->link_config.duplex;
  1663. phydev->autoneg = tp->link_config.autoneg;
  1664. phydev->advertising = tp->link_config.advertising;
  1665. }
  1666. phy_start(phydev);
  1667. phy_start_aneg(phydev);
  1668. }
  1669. static void tg3_phy_stop(struct tg3 *tp)
  1670. {
  1671. if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
  1672. return;
  1673. phy_stop(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
  1674. }
  1675. static void tg3_phy_fini(struct tg3 *tp)
  1676. {
  1677. if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
  1678. phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
  1679. tp->phy_flags &= ~TG3_PHYFLG_IS_CONNECTED;
  1680. }
  1681. }
  1682. static int tg3_phy_set_extloopbk(struct tg3 *tp)
  1683. {
  1684. int err;
  1685. u32 val;
  1686. if (tp->phy_flags & TG3_PHYFLG_IS_FET)
  1687. return 0;
  1688. if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
  1689. /* Cannot do read-modify-write on 5401 */
  1690. err = tg3_phy_auxctl_write(tp,
  1691. MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
  1692. MII_TG3_AUXCTL_ACTL_EXTLOOPBK |
  1693. 0x4c20);
  1694. goto done;
  1695. }
  1696. err = tg3_phy_auxctl_read(tp,
  1697. MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
  1698. if (err)
  1699. return err;
  1700. val |= MII_TG3_AUXCTL_ACTL_EXTLOOPBK;
  1701. err = tg3_phy_auxctl_write(tp,
  1702. MII_TG3_AUXCTL_SHDWSEL_AUXCTL, val);
  1703. done:
  1704. return err;
  1705. }
  1706. static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
  1707. {
  1708. u32 phytest;
  1709. if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
  1710. u32 phy;
  1711. tg3_writephy(tp, MII_TG3_FET_TEST,
  1712. phytest | MII_TG3_FET_SHADOW_EN);
  1713. if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
  1714. if (enable)
  1715. phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
  1716. else
  1717. phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
  1718. tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
  1719. }
  1720. tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
  1721. }
  1722. }
  1723. static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
  1724. {
  1725. u32 reg;
  1726. if (!tg3_flag(tp, 5705_PLUS) ||
  1727. (tg3_flag(tp, 5717_PLUS) &&
  1728. (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
  1729. return;
  1730. if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
  1731. tg3_phy_fet_toggle_apd(tp, enable);
  1732. return;
  1733. }
  1734. reg = MII_TG3_MISC_SHDW_WREN |
  1735. MII_TG3_MISC_SHDW_SCR5_SEL |
  1736. MII_TG3_MISC_SHDW_SCR5_LPED |
  1737. MII_TG3_MISC_SHDW_SCR5_DLPTLM |
  1738. MII_TG3_MISC_SHDW_SCR5_SDTL |
  1739. MII_TG3_MISC_SHDW_SCR5_C125OE;
  1740. if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 || !enable)
  1741. reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
  1742. tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
  1743. reg = MII_TG3_MISC_SHDW_WREN |
  1744. MII_TG3_MISC_SHDW_APD_SEL |
  1745. MII_TG3_MISC_SHDW_APD_WKTM_84MS;
  1746. if (enable)
  1747. reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
  1748. tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
  1749. }
  1750. static void tg3_phy_toggle_automdix(struct tg3 *tp, int enable)
  1751. {
  1752. u32 phy;
  1753. if (!tg3_flag(tp, 5705_PLUS) ||
  1754. (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
  1755. return;
  1756. if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
  1757. u32 ephy;
  1758. if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
  1759. u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
  1760. tg3_writephy(tp, MII_TG3_FET_TEST,
  1761. ephy | MII_TG3_FET_SHADOW_EN);
  1762. if (!tg3_readphy(tp, reg, &phy)) {
  1763. if (enable)
  1764. phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
  1765. else
  1766. phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
  1767. tg3_writephy(tp, reg, phy);
  1768. }
  1769. tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
  1770. }
  1771. } else {
  1772. int ret;
  1773. ret = tg3_phy_auxctl_read(tp,
  1774. MII_TG3_AUXCTL_SHDWSEL_MISC, &phy);
  1775. if (!ret) {
  1776. if (enable)
  1777. phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
  1778. else
  1779. phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
  1780. tg3_phy_auxctl_write(tp,
  1781. MII_TG3_AUXCTL_SHDWSEL_MISC, phy);
  1782. }
  1783. }
  1784. }
  1785. static void tg3_phy_set_wirespeed(struct tg3 *tp)
  1786. {
  1787. int ret;
  1788. u32 val;
  1789. if (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED)
  1790. return;
  1791. ret = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_MISC, &val);
  1792. if (!ret)
  1793. tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_MISC,
  1794. val | MII_TG3_AUXCTL_MISC_WIRESPD_EN);
  1795. }
  1796. static void tg3_phy_apply_otp(struct tg3 *tp)
  1797. {
  1798. u32 otp, phy;
  1799. if (!tp->phy_otp)
  1800. return;
  1801. otp = tp->phy_otp;
  1802. if (TG3_PHY_AUXCTL_SMDSP_ENABLE(tp))
  1803. return;
  1804. phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
  1805. phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
  1806. tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
  1807. phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
  1808. ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
  1809. tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
  1810. phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
  1811. phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
  1812. tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
  1813. phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
  1814. tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
  1815. phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
  1816. tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
  1817. phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
  1818. ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
  1819. tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
  1820. TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
  1821. }
  1822. static void tg3_phy_eee_adjust(struct tg3 *tp, u32 current_link_up)
  1823. {
  1824. u32 val;
  1825. if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
  1826. return;
  1827. tp->setlpicnt = 0;
  1828. if (tp->link_config.autoneg == AUTONEG_ENABLE &&
  1829. current_link_up == 1 &&
  1830. tp->link_config.active_duplex == DUPLEX_FULL &&
  1831. (tp->link_config.active_speed == SPEED_100 ||
  1832. tp->link_config.active_speed == SPEED_1000)) {
  1833. u32 eeectl;
  1834. if (tp->link_config.active_speed == SPEED_1000)
  1835. eeectl = TG3_CPMU_EEE_CTRL_EXIT_16_5_US;
  1836. else
  1837. eeectl = TG3_CPMU_EEE_CTRL_EXIT_36_US;
  1838. tw32(TG3_CPMU_EEE_CTRL, eeectl);
  1839. tg3_phy_cl45_read(tp, MDIO_MMD_AN,
  1840. TG3_CL45_D7_EEERES_STAT, &val);
  1841. if (val == TG3_CL45_D7_EEERES_STAT_LP_1000T ||
  1842. val == TG3_CL45_D7_EEERES_STAT_LP_100TX)
  1843. tp->setlpicnt = 2;
  1844. }
  1845. if (!tp->setlpicnt) {
  1846. if (current_link_up == 1 &&
  1847. !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
  1848. tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, 0x0000);
  1849. TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
  1850. }
  1851. val = tr32(TG3_CPMU_EEE_MODE);
  1852. tw32(TG3_CPMU_EEE_MODE, val & ~TG3_CPMU_EEEMD_LPI_ENABLE);
  1853. }
  1854. }
  1855. static void tg3_phy_eee_enable(struct tg3 *tp)
  1856. {
  1857. u32 val;
  1858. if (tp->link_config.active_speed == SPEED_1000 &&
  1859. (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
  1860. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
  1861. tg3_flag(tp, 57765_CLASS)) &&
  1862. !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
  1863. val = MII_TG3_DSP_TAP26_ALNOKO |
  1864. MII_TG3_DSP_TAP26_RMRXSTO;
  1865. tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
  1866. TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
  1867. }
  1868. val = tr32(TG3_CPMU_EEE_MODE);
  1869. tw32(TG3_CPMU_EEE_MODE, val | TG3_CPMU_EEEMD_LPI_ENABLE);
  1870. }
  1871. static int tg3_wait_macro_done(struct tg3 *tp)
  1872. {
  1873. int limit = 100;
  1874. while (limit--) {
  1875. u32 tmp32;
  1876. if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
  1877. if ((tmp32 & 0x1000) == 0)
  1878. break;
  1879. }
  1880. }
  1881. if (limit < 0)
  1882. return -EBUSY;
  1883. return 0;
  1884. }
  1885. static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
  1886. {
  1887. static const u32 test_pat[4][6] = {
  1888. { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
  1889. { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
  1890. { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
  1891. { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
  1892. };
  1893. int chan;
  1894. for (chan = 0; chan < 4; chan++) {
  1895. int i;
  1896. tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
  1897. (chan * 0x2000) | 0x0200);
  1898. tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
  1899. for (i = 0; i < 6; i++)
  1900. tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
  1901. test_pat[chan][i]);
  1902. tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
  1903. if (tg3_wait_macro_done(tp)) {
  1904. *resetp = 1;
  1905. return -EBUSY;
  1906. }
  1907. tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
  1908. (chan * 0x2000) | 0x0200);
  1909. tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
  1910. if (tg3_wait_macro_done(tp)) {
  1911. *resetp = 1;
  1912. return -EBUSY;
  1913. }
  1914. tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
  1915. if (tg3_wait_macro_done(tp)) {
  1916. *resetp = 1;
  1917. return -EBUSY;
  1918. }
  1919. for (i = 0; i < 6; i += 2) {
  1920. u32 low, high;
  1921. if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
  1922. tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
  1923. tg3_wait_macro_done(tp)) {
  1924. *resetp = 1;
  1925. return -EBUSY;
  1926. }
  1927. low &= 0x7fff;
  1928. high &= 0x000f;
  1929. if (low != test_pat[chan][i] ||
  1930. high != test_pat[chan][i+1]) {
  1931. tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
  1932. tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
  1933. tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
  1934. return -EBUSY;
  1935. }
  1936. }
  1937. }
  1938. return 0;
  1939. }
  1940. static int tg3_phy_reset_chanpat(struct tg3 *tp)
  1941. {
  1942. int chan;
  1943. for (chan = 0; chan < 4; chan++) {
  1944. int i;
  1945. tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
  1946. (chan * 0x2000) | 0x0200);
  1947. tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
  1948. for (i = 0; i < 6; i++)
  1949. tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
  1950. tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
  1951. if (tg3_wait_macro_done(tp))
  1952. return -EBUSY;
  1953. }
  1954. return 0;
  1955. }
  1956. static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
  1957. {
  1958. u32 reg32, phy9_orig;
  1959. int retries, do_phy_reset, err;
  1960. retries = 10;
  1961. do_phy_reset = 1;
  1962. do {
  1963. if (do_phy_reset) {
  1964. err = tg3_bmcr_reset(tp);
  1965. if (err)
  1966. return err;
  1967. do_phy_reset = 0;
  1968. }
  1969. /* Disable transmitter and interrupt. */
  1970. if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
  1971. continue;
  1972. reg32 |= 0x3000;
  1973. tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
  1974. /* Set full-duplex, 1000 mbps. */
  1975. tg3_writephy(tp, MII_BMCR,
  1976. BMCR_FULLDPLX | BMCR_SPEED1000);
  1977. /* Set to master mode. */
  1978. if (tg3_readphy(tp, MII_CTRL1000, &phy9_orig))
  1979. continue;
  1980. tg3_writephy(tp, MII_CTRL1000,
  1981. CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
  1982. err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
  1983. if (err)
  1984. return err;
  1985. /* Block the PHY control access. */
  1986. tg3_phydsp_write(tp, 0x8005, 0x0800);
  1987. err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
  1988. if (!err)
  1989. break;
  1990. } while (--retries);
  1991. err = tg3_phy_reset_chanpat(tp);
  1992. if (err)
  1993. return err;
  1994. tg3_phydsp_write(tp, 0x8005, 0x0000);
  1995. tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
  1996. tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0000);
  1997. TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
  1998. tg3_writephy(tp, MII_CTRL1000, phy9_orig);
  1999. if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
  2000. reg32 &= ~0x3000;
  2001. tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
  2002. } else if (!err)
  2003. err = -EBUSY;
  2004. return err;
  2005. }
  2006. /* This will reset the tigon3 PHY if there is no valid
  2007. * link unless the FORCE argument is non-zero.
  2008. */
  2009. static int tg3_phy_reset(struct tg3 *tp)
  2010. {
  2011. u32 val, cpmuctrl;
  2012. int err;
  2013. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
  2014. val = tr32(GRC_MISC_CFG);
  2015. tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
  2016. udelay(40);
  2017. }
  2018. err = tg3_readphy(tp, MII_BMSR, &val);
  2019. err |= tg3_readphy(tp, MII_BMSR, &val);
  2020. if (err != 0)
  2021. return -EBUSY;
  2022. if (netif_running(tp->dev) && netif_carrier_ok(tp->dev)) {
  2023. netif_carrier_off(tp->dev);
  2024. tg3_link_report(tp);
  2025. }
  2026. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
  2027. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
  2028. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
  2029. err = tg3_phy_reset_5703_4_5(tp);
  2030. if (err)
  2031. return err;
  2032. goto out;
  2033. }
  2034. cpmuctrl = 0;
  2035. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
  2036. GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
  2037. cpmuctrl = tr32(TG3_CPMU_CTRL);
  2038. if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
  2039. tw32(TG3_CPMU_CTRL,
  2040. cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
  2041. }
  2042. err = tg3_bmcr_reset(tp);
  2043. if (err)
  2044. return err;
  2045. if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
  2046. val = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
  2047. tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, val);
  2048. tw32(TG3_CPMU_CTRL, cpmuctrl);
  2049. }
  2050. if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
  2051. GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
  2052. val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
  2053. if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
  2054. CPMU_LSPD_1000MB_MACCLK_12_5) {
  2055. val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
  2056. udelay(40);
  2057. tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
  2058. }
  2059. }
  2060. if (tg3_flag(tp, 5717_PLUS) &&
  2061. (tp->phy_flags & TG3_PHYFLG_MII_SERDES))
  2062. return 0;
  2063. tg3_phy_apply_otp(tp);
  2064. if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
  2065. tg3_phy_toggle_apd(tp, true);
  2066. else
  2067. tg3_phy_toggle_apd(tp, false);
  2068. out:
  2069. if ((tp->phy_flags & TG3_PHYFLG_ADC_BUG) &&
  2070. !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
  2071. tg3_phydsp_write(tp, 0x201f, 0x2aaa);
  2072. tg3_phydsp_write(tp, 0x000a, 0x0323);
  2073. TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
  2074. }
  2075. if (tp->phy_flags & TG3_PHYFLG_5704_A0_BUG) {
  2076. tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
  2077. tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
  2078. }
  2079. if (tp->phy_flags & TG3_PHYFLG_BER_BUG) {
  2080. if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
  2081. tg3_phydsp_write(tp, 0x000a, 0x310b);
  2082. tg3_phydsp_write(tp, 0x201f, 0x9506);
  2083. tg3_phydsp_write(tp, 0x401f, 0x14e2);
  2084. TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
  2085. }
  2086. } else if (tp->phy_flags & TG3_PHYFLG_JITTER_BUG) {
  2087. if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
  2088. tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
  2089. if (tp->phy_flags & TG3_PHYFLG_ADJUST_TRIM) {
  2090. tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
  2091. tg3_writephy(tp, MII_TG3_TEST1,
  2092. MII_TG3_TEST1_TRIM_EN | 0x4);
  2093. } else
  2094. tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
  2095. TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
  2096. }
  2097. }
  2098. /* Set Extended packet length bit (bit 14) on all chips that */
  2099. /* support jumbo frames */
  2100. if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
  2101. /* Cannot do read-modify-write on 5401 */
  2102. tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
  2103. } else if (tg3_flag(tp, JUMBO_CAPABLE)) {
  2104. /* Set bit 14 with read-modify-write to preserve other bits */
  2105. err = tg3_phy_auxctl_read(tp,
  2106. MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
  2107. if (!err)
  2108. tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
  2109. val | MII_TG3_AUXCTL_ACTL_EXTPKTLEN);
  2110. }
  2111. /* Set phy register 0x10 bit 0 to high fifo elasticity to support
  2112. * jumbo frames transmission.
  2113. */
  2114. if (tg3_flag(tp, JUMBO_CAPABLE)) {
  2115. if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &val))
  2116. tg3_writephy(tp, MII_TG3_EXT_CTRL,
  2117. val | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
  2118. }
  2119. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
  2120. /* adjust output voltage */
  2121. tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
  2122. }
  2123. tg3_phy_toggle_automdix(tp, 1);
  2124. tg3_phy_set_wirespeed(tp);
  2125. return 0;
  2126. }
  2127. #define TG3_GPIO_MSG_DRVR_PRES 0x00000001
  2128. #define TG3_GPIO_MSG_NEED_VAUX 0x00000002
  2129. #define TG3_GPIO_MSG_MASK (TG3_GPIO_MSG_DRVR_PRES | \
  2130. TG3_GPIO_MSG_NEED_VAUX)
  2131. #define TG3_GPIO_MSG_ALL_DRVR_PRES_MASK \
  2132. ((TG3_GPIO_MSG_DRVR_PRES << 0) | \
  2133. (TG3_GPIO_MSG_DRVR_PRES << 4) | \
  2134. (TG3_GPIO_MSG_DRVR_PRES << 8) | \
  2135. (TG3_GPIO_MSG_DRVR_PRES << 12))
  2136. #define TG3_GPIO_MSG_ALL_NEED_VAUX_MASK \
  2137. ((TG3_GPIO_MSG_NEED_VAUX << 0) | \
  2138. (TG3_GPIO_MSG_NEED_VAUX << 4) | \
  2139. (TG3_GPIO_MSG_NEED_VAUX << 8) | \
  2140. (TG3_GPIO_MSG_NEED_VAUX << 12))
  2141. static inline u32 tg3_set_function_status(struct tg3 *tp, u32 newstat)
  2142. {
  2143. u32 status, shift;
  2144. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
  2145. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
  2146. status = tg3_ape_read32(tp, TG3_APE_GPIO_MSG);
  2147. else
  2148. status = tr32(TG3_CPMU_DRV_STATUS);
  2149. shift = TG3_APE_GPIO_MSG_SHIFT + 4 * tp->pci_fn;
  2150. status &= ~(TG3_GPIO_MSG_MASK << shift);
  2151. status |= (newstat << shift);
  2152. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
  2153. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
  2154. tg3_ape_write32(tp, TG3_APE_GPIO_MSG, status);
  2155. else
  2156. tw32(TG3_CPMU_DRV_STATUS, status);
  2157. return status >> TG3_APE_GPIO_MSG_SHIFT;
  2158. }
  2159. static inline int tg3_pwrsrc_switch_to_vmain(struct tg3 *tp)
  2160. {
  2161. if (!tg3_flag(tp, IS_NIC))
  2162. return 0;
  2163. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
  2164. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
  2165. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
  2166. if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
  2167. return -EIO;
  2168. tg3_set_function_status(tp, TG3_GPIO_MSG_DRVR_PRES);
  2169. tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
  2170. TG3_GRC_LCLCTL_PWRSW_DELAY);
  2171. tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
  2172. } else {
  2173. tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
  2174. TG3_GRC_LCLCTL_PWRSW_DELAY);
  2175. }
  2176. return 0;
  2177. }
  2178. static void tg3_pwrsrc_die_with_vmain(struct tg3 *tp)
  2179. {
  2180. u32 grc_local_ctrl;
  2181. if (!tg3_flag(tp, IS_NIC) ||
  2182. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
  2183. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)
  2184. return;
  2185. grc_local_ctrl = tp->grc_local_ctrl | GRC_LCLCTRL_GPIO_OE1;
  2186. tw32_wait_f(GRC_LOCAL_CTRL,
  2187. grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
  2188. TG3_GRC_LCLCTL_PWRSW_DELAY);
  2189. tw32_wait_f(GRC_LOCAL_CTRL,
  2190. grc_local_ctrl,
  2191. TG3_GRC_LCLCTL_PWRSW_DELAY);
  2192. tw32_wait_f(GRC_LOCAL_CTRL,
  2193. grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
  2194. TG3_GRC_LCLCTL_PWRSW_DELAY);
  2195. }
  2196. static void tg3_pwrsrc_switch_to_vaux(struct tg3 *tp)
  2197. {
  2198. if (!tg3_flag(tp, IS_NIC))
  2199. return;
  2200. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
  2201. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
  2202. tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
  2203. (GRC_LCLCTRL_GPIO_OE0 |
  2204. GRC_LCLCTRL_GPIO_OE1 |
  2205. GRC_LCLCTRL_GPIO_OE2 |
  2206. GRC_LCLCTRL_GPIO_OUTPUT0 |
  2207. GRC_LCLCTRL_GPIO_OUTPUT1),
  2208. TG3_GRC_LCLCTL_PWRSW_DELAY);
  2209. } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
  2210. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
  2211. /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
  2212. u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
  2213. GRC_LCLCTRL_GPIO_OE1 |
  2214. GRC_LCLCTRL_GPIO_OE2 |
  2215. GRC_LCLCTRL_GPIO_OUTPUT0 |
  2216. GRC_LCLCTRL_GPIO_OUTPUT1 |
  2217. tp->grc_local_ctrl;
  2218. tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
  2219. TG3_GRC_LCLCTL_PWRSW_DELAY);
  2220. grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
  2221. tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
  2222. TG3_GRC_LCLCTL_PWRSW_DELAY);
  2223. grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
  2224. tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
  2225. TG3_GRC_LCLCTL_PWRSW_DELAY);
  2226. } else {
  2227. u32 no_gpio2;
  2228. u32 grc_local_ctrl = 0;
  2229. /* Workaround to prevent overdrawing Amps. */
  2230. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
  2231. grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
  2232. tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
  2233. grc_local_ctrl,
  2234. TG3_GRC_LCLCTL_PWRSW_DELAY);
  2235. }
  2236. /* On 5753 and variants, GPIO2 cannot be used. */
  2237. no_gpio2 = tp->nic_sram_data_cfg &
  2238. NIC_SRAM_DATA_CFG_NO_GPIO2;
  2239. grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
  2240. GRC_LCLCTRL_GPIO_OE1 |
  2241. GRC_LCLCTRL_GPIO_OE2 |
  2242. GRC_LCLCTRL_GPIO_OUTPUT1 |
  2243. GRC_LCLCTRL_GPIO_OUTPUT2;
  2244. if (no_gpio2) {
  2245. grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
  2246. GRC_LCLCTRL_GPIO_OUTPUT2);
  2247. }
  2248. tw32_wait_f(GRC_LOCAL_CTRL,
  2249. tp->grc_local_ctrl | grc_local_ctrl,
  2250. TG3_GRC_LCLCTL_PWRSW_DELAY);
  2251. grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
  2252. tw32_wait_f(GRC_LOCAL_CTRL,
  2253. tp->grc_local_ctrl | grc_local_ctrl,
  2254. TG3_GRC_LCLCTL_PWRSW_DELAY);
  2255. if (!no_gpio2) {
  2256. grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
  2257. tw32_wait_f(GRC_LOCAL_CTRL,
  2258. tp->grc_local_ctrl | grc_local_ctrl,
  2259. TG3_GRC_LCLCTL_PWRSW_DELAY);
  2260. }
  2261. }
  2262. }
  2263. static void tg3_frob_aux_power_5717(struct tg3 *tp, bool wol_enable)
  2264. {
  2265. u32 msg = 0;
  2266. /* Serialize power state transitions */
  2267. if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
  2268. return;
  2269. if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE) || wol_enable)
  2270. msg = TG3_GPIO_MSG_NEED_VAUX;
  2271. msg = tg3_set_function_status(tp, msg);
  2272. if (msg & TG3_GPIO_MSG_ALL_DRVR_PRES_MASK)
  2273. goto done;
  2274. if (msg & TG3_GPIO_MSG_ALL_NEED_VAUX_MASK)
  2275. tg3_pwrsrc_switch_to_vaux(tp);
  2276. else
  2277. tg3_pwrsrc_die_with_vmain(tp);
  2278. done:
  2279. tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
  2280. }
  2281. static void tg3_frob_aux_power(struct tg3 *tp, bool include_wol)
  2282. {
  2283. bool need_vaux = false;
  2284. /* The GPIOs do something completely different on 57765. */
  2285. if (!tg3_flag(tp, IS_NIC) || tg3_flag(tp, 57765_CLASS))
  2286. return;
  2287. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
  2288. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
  2289. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
  2290. tg3_frob_aux_power_5717(tp, include_wol ?
  2291. tg3_flag(tp, WOL_ENABLE) != 0 : 0);
  2292. return;
  2293. }
  2294. if (tp->pdev_peer && tp->pdev_peer != tp->pdev) {
  2295. struct net_device *dev_peer;
  2296. dev_peer = pci_get_drvdata(tp->pdev_peer);
  2297. /* remove_one() may have been run on the peer. */
  2298. if (dev_peer) {
  2299. struct tg3 *tp_peer = netdev_priv(dev_peer);
  2300. if (tg3_flag(tp_peer, INIT_COMPLETE))
  2301. return;
  2302. if ((include_wol && tg3_flag(tp_peer, WOL_ENABLE)) ||
  2303. tg3_flag(tp_peer, ENABLE_ASF))
  2304. need_vaux = true;
  2305. }
  2306. }
  2307. if ((include_wol && tg3_flag(tp, WOL_ENABLE)) ||
  2308. tg3_flag(tp, ENABLE_ASF))
  2309. need_vaux = true;
  2310. if (need_vaux)
  2311. tg3_pwrsrc_switch_to_vaux(tp);
  2312. else
  2313. tg3_pwrsrc_die_with_vmain(tp);
  2314. }
  2315. static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
  2316. {
  2317. if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
  2318. return 1;
  2319. else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
  2320. if (speed != SPEED_10)
  2321. return 1;
  2322. } else if (speed == SPEED_10)
  2323. return 1;
  2324. return 0;
  2325. }
  2326. static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
  2327. {
  2328. u32 val;
  2329. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
  2330. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
  2331. u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
  2332. u32 serdes_cfg = tr32(MAC_SERDES_CFG);
  2333. sg_dig_ctrl |=
  2334. SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
  2335. tw32(SG_DIG_CTRL, sg_dig_ctrl);
  2336. tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
  2337. }
  2338. return;
  2339. }
  2340. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
  2341. tg3_bmcr_reset(tp);
  2342. val = tr32(GRC_MISC_CFG);
  2343. tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
  2344. udelay(40);
  2345. return;
  2346. } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
  2347. u32 phytest;
  2348. if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
  2349. u32 phy;
  2350. tg3_writephy(tp, MII_ADVERTISE, 0);
  2351. tg3_writephy(tp, MII_BMCR,
  2352. BMCR_ANENABLE | BMCR_ANRESTART);
  2353. tg3_writephy(tp, MII_TG3_FET_TEST,
  2354. phytest | MII_TG3_FET_SHADOW_EN);
  2355. if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
  2356. phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
  2357. tg3_writephy(tp,
  2358. MII_TG3_FET_SHDW_AUXMODE4,
  2359. phy);
  2360. }
  2361. tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
  2362. }
  2363. return;
  2364. } else if (do_low_power) {
  2365. tg3_writephy(tp, MII_TG3_EXT_CTRL,
  2366. MII_TG3_EXT_CTRL_FORCE_LED_OFF);
  2367. val = MII_TG3_AUXCTL_PCTL_100TX_LPWR |
  2368. MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
  2369. MII_TG3_AUXCTL_PCTL_VREG_11V;
  2370. tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, val);
  2371. }
  2372. /* The PHY should not be powered down on some chips because
  2373. * of bugs.
  2374. */
  2375. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
  2376. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
  2377. (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 &&
  2378. (tp->phy_flags & TG3_PHYFLG_MII_SERDES)) ||
  2379. (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 &&
  2380. !tp->pci_fn))
  2381. return;
  2382. if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
  2383. GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
  2384. val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
  2385. val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
  2386. val |= CPMU_LSPD_1000MB_MACCLK_12_5;
  2387. tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
  2388. }
  2389. tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
  2390. }
  2391. /* tp->lock is held. */
  2392. static int tg3_nvram_lock(struct tg3 *tp)
  2393. {
  2394. if (tg3_flag(tp, NVRAM)) {
  2395. int i;
  2396. if (tp->nvram_lock_cnt == 0) {
  2397. tw32(NVRAM_SWARB, SWARB_REQ_SET1);
  2398. for (i = 0; i < 8000; i++) {
  2399. if (tr32(NVRAM_SWARB) & SWARB_GNT1)
  2400. break;
  2401. udelay(20);
  2402. }
  2403. if (i == 8000) {
  2404. tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
  2405. return -ENODEV;
  2406. }
  2407. }
  2408. tp->nvram_lock_cnt++;
  2409. }
  2410. return 0;
  2411. }
  2412. /* tp->lock is held. */
  2413. static void tg3_nvram_unlock(struct tg3 *tp)
  2414. {
  2415. if (tg3_flag(tp, NVRAM)) {
  2416. if (tp->nvram_lock_cnt > 0)
  2417. tp->nvram_lock_cnt--;
  2418. if (tp->nvram_lock_cnt == 0)
  2419. tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
  2420. }
  2421. }
  2422. /* tp->lock is held. */
  2423. static void tg3_enable_nvram_access(struct tg3 *tp)
  2424. {
  2425. if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
  2426. u32 nvaccess = tr32(NVRAM_ACCESS);
  2427. tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
  2428. }
  2429. }
  2430. /* tp->lock is held. */
  2431. static void tg3_disable_nvram_access(struct tg3 *tp)
  2432. {
  2433. if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
  2434. u32 nvaccess = tr32(NVRAM_ACCESS);
  2435. tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
  2436. }
  2437. }
  2438. static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
  2439. u32 offset, u32 *val)
  2440. {
  2441. u32 tmp;
  2442. int i;
  2443. if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
  2444. return -EINVAL;
  2445. tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
  2446. EEPROM_ADDR_DEVID_MASK |
  2447. EEPROM_ADDR_READ);
  2448. tw32(GRC_EEPROM_ADDR,
  2449. tmp |
  2450. (0 << EEPROM_ADDR_DEVID_SHIFT) |
  2451. ((offset << EEPROM_ADDR_ADDR_SHIFT) &
  2452. EEPROM_ADDR_ADDR_MASK) |
  2453. EEPROM_ADDR_READ | EEPROM_ADDR_START);
  2454. for (i = 0; i < 1000; i++) {
  2455. tmp = tr32(GRC_EEPROM_ADDR);
  2456. if (tmp & EEPROM_ADDR_COMPLETE)
  2457. break;
  2458. msleep(1);
  2459. }
  2460. if (!(tmp & EEPROM_ADDR_COMPLETE))
  2461. return -EBUSY;
  2462. tmp = tr32(GRC_EEPROM_DATA);
  2463. /*
  2464. * The data will always be opposite the native endian
  2465. * format. Perform a blind byteswap to compensate.
  2466. */
  2467. *val = swab32(tmp);
  2468. return 0;
  2469. }
  2470. #define NVRAM_CMD_TIMEOUT 10000
  2471. static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
  2472. {
  2473. int i;
  2474. tw32(NVRAM_CMD, nvram_cmd);
  2475. for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
  2476. udelay(10);
  2477. if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
  2478. udelay(10);
  2479. break;
  2480. }
  2481. }
  2482. if (i == NVRAM_CMD_TIMEOUT)
  2483. return -EBUSY;
  2484. return 0;
  2485. }
  2486. static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
  2487. {
  2488. if (tg3_flag(tp, NVRAM) &&
  2489. tg3_flag(tp, NVRAM_BUFFERED) &&
  2490. tg3_flag(tp, FLASH) &&
  2491. !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
  2492. (tp->nvram_jedecnum == JEDEC_ATMEL))
  2493. addr = ((addr / tp->nvram_pagesize) <<
  2494. ATMEL_AT45DB0X1B_PAGE_POS) +
  2495. (addr % tp->nvram_pagesize);
  2496. return addr;
  2497. }
  2498. static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
  2499. {
  2500. if (tg3_flag(tp, NVRAM) &&
  2501. tg3_flag(tp, NVRAM_BUFFERED) &&
  2502. tg3_flag(tp, FLASH) &&
  2503. !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
  2504. (tp->nvram_jedecnum == JEDEC_ATMEL))
  2505. addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
  2506. tp->nvram_pagesize) +
  2507. (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
  2508. return addr;
  2509. }
  2510. /* NOTE: Data read in from NVRAM is byteswapped according to
  2511. * the byteswapping settings for all other register accesses.
  2512. * tg3 devices are BE devices, so on a BE machine, the data
  2513. * returned will be exactly as it is seen in NVRAM. On a LE
  2514. * machine, the 32-bit value will be byteswapped.
  2515. */
  2516. static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
  2517. {
  2518. int ret;
  2519. if (!tg3_flag(tp, NVRAM))
  2520. return tg3_nvram_read_using_eeprom(tp, offset, val);
  2521. offset = tg3_nvram_phys_addr(tp, offset);
  2522. if (offset > NVRAM_ADDR_MSK)
  2523. return -EINVAL;
  2524. ret = tg3_nvram_lock(tp);
  2525. if (ret)
  2526. return ret;
  2527. tg3_enable_nvram_access(tp);
  2528. tw32(NVRAM_ADDR, offset);
  2529. ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
  2530. NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
  2531. if (ret == 0)
  2532. *val = tr32(NVRAM_RDDATA);
  2533. tg3_disable_nvram_access(tp);
  2534. tg3_nvram_unlock(tp);
  2535. return ret;
  2536. }
  2537. /* Ensures NVRAM data is in bytestream format. */
  2538. static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
  2539. {
  2540. u32 v;
  2541. int res = tg3_nvram_read(tp, offset, &v);
  2542. if (!res)
  2543. *val = cpu_to_be32(v);
  2544. return res;
  2545. }
  2546. static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
  2547. u32 offset, u32 len, u8 *buf)
  2548. {
  2549. int i, j, rc = 0;
  2550. u32 val;
  2551. for (i = 0; i < len; i += 4) {
  2552. u32 addr;
  2553. __be32 data;
  2554. addr = offset + i;
  2555. memcpy(&data, buf + i, 4);
  2556. /*
  2557. * The SEEPROM interface expects the data to always be opposite
  2558. * the native endian format. We accomplish this by reversing
  2559. * all the operations that would have been performed on the
  2560. * data from a call to tg3_nvram_read_be32().
  2561. */
  2562. tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
  2563. val = tr32(GRC_EEPROM_ADDR);
  2564. tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
  2565. val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
  2566. EEPROM_ADDR_READ);
  2567. tw32(GRC_EEPROM_ADDR, val |
  2568. (0 << EEPROM_ADDR_DEVID_SHIFT) |
  2569. (addr & EEPROM_ADDR_ADDR_MASK) |
  2570. EEPROM_ADDR_START |
  2571. EEPROM_ADDR_WRITE);
  2572. for (j = 0; j < 1000; j++) {
  2573. val = tr32(GRC_EEPROM_ADDR);
  2574. if (val & EEPROM_ADDR_COMPLETE)
  2575. break;
  2576. msleep(1);
  2577. }
  2578. if (!(val & EEPROM_ADDR_COMPLETE)) {
  2579. rc = -EBUSY;
  2580. break;
  2581. }
  2582. }
  2583. return rc;
  2584. }
  2585. /* offset and length are dword aligned */
  2586. static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
  2587. u8 *buf)
  2588. {
  2589. int ret = 0;
  2590. u32 pagesize = tp->nvram_pagesize;
  2591. u32 pagemask = pagesize - 1;
  2592. u32 nvram_cmd;
  2593. u8 *tmp;
  2594. tmp = kmalloc(pagesize, GFP_KERNEL);
  2595. if (tmp == NULL)
  2596. return -ENOMEM;
  2597. while (len) {
  2598. int j;
  2599. u32 phy_addr, page_off, size;
  2600. phy_addr = offset & ~pagemask;
  2601. for (j = 0; j < pagesize; j += 4) {
  2602. ret = tg3_nvram_read_be32(tp, phy_addr + j,
  2603. (__be32 *) (tmp + j));
  2604. if (ret)
  2605. break;
  2606. }
  2607. if (ret)
  2608. break;
  2609. page_off = offset & pagemask;
  2610. size = pagesize;
  2611. if (len < size)
  2612. size = len;
  2613. len -= size;
  2614. memcpy(tmp + page_off, buf, size);
  2615. offset = offset + (pagesize - page_off);
  2616. tg3_enable_nvram_access(tp);
  2617. /*
  2618. * Before we can erase the flash page, we need
  2619. * to issue a special "write enable" command.
  2620. */
  2621. nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
  2622. if (tg3_nvram_exec_cmd(tp, nvram_cmd))
  2623. break;
  2624. /* Erase the target page */
  2625. tw32(NVRAM_ADDR, phy_addr);
  2626. nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
  2627. NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
  2628. if (tg3_nvram_exec_cmd(tp, nvram_cmd))
  2629. break;
  2630. /* Issue another write enable to start the write. */
  2631. nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
  2632. if (tg3_nvram_exec_cmd(tp, nvram_cmd))
  2633. break;
  2634. for (j = 0; j < pagesize; j += 4) {
  2635. __be32 data;
  2636. data = *((__be32 *) (tmp + j));
  2637. tw32(NVRAM_WRDATA, be32_to_cpu(data));
  2638. tw32(NVRAM_ADDR, phy_addr + j);
  2639. nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
  2640. NVRAM_CMD_WR;
  2641. if (j == 0)
  2642. nvram_cmd |= NVRAM_CMD_FIRST;
  2643. else if (j == (pagesize - 4))
  2644. nvram_cmd |= NVRAM_CMD_LAST;
  2645. ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
  2646. if (ret)
  2647. break;
  2648. }
  2649. if (ret)
  2650. break;
  2651. }
  2652. nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
  2653. tg3_nvram_exec_cmd(tp, nvram_cmd);
  2654. kfree(tmp);
  2655. return ret;
  2656. }
  2657. /* offset and length are dword aligned */
  2658. static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
  2659. u8 *buf)
  2660. {
  2661. int i, ret = 0;
  2662. for (i = 0; i < len; i += 4, offset += 4) {
  2663. u32 page_off, phy_addr, nvram_cmd;
  2664. __be32 data;
  2665. memcpy(&data, buf + i, 4);
  2666. tw32(NVRAM_WRDATA, be32_to_cpu(data));
  2667. page_off = offset % tp->nvram_pagesize;
  2668. phy_addr = tg3_nvram_phys_addr(tp, offset);
  2669. nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
  2670. if (page_off == 0 || i == 0)
  2671. nvram_cmd |= NVRAM_CMD_FIRST;
  2672. if (page_off == (tp->nvram_pagesize - 4))
  2673. nvram_cmd |= NVRAM_CMD_LAST;
  2674. if (i == (len - 4))
  2675. nvram_cmd |= NVRAM_CMD_LAST;
  2676. if ((nvram_cmd & NVRAM_CMD_FIRST) ||
  2677. !tg3_flag(tp, FLASH) ||
  2678. !tg3_flag(tp, 57765_PLUS))
  2679. tw32(NVRAM_ADDR, phy_addr);
  2680. if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
  2681. !tg3_flag(tp, 5755_PLUS) &&
  2682. (tp->nvram_jedecnum == JEDEC_ST) &&
  2683. (nvram_cmd & NVRAM_CMD_FIRST)) {
  2684. u32 cmd;
  2685. cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
  2686. ret = tg3_nvram_exec_cmd(tp, cmd);
  2687. if (ret)
  2688. break;
  2689. }
  2690. if (!tg3_flag(tp, FLASH)) {
  2691. /* We always do complete word writes to eeprom. */
  2692. nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
  2693. }
  2694. ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
  2695. if (ret)
  2696. break;
  2697. }
  2698. return ret;
  2699. }
  2700. /* offset and length are dword aligned */
  2701. static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
  2702. {
  2703. int ret;
  2704. if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
  2705. tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
  2706. ~GRC_LCLCTRL_GPIO_OUTPUT1);
  2707. udelay(40);
  2708. }
  2709. if (!tg3_flag(tp, NVRAM)) {
  2710. ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
  2711. } else {
  2712. u32 grc_mode;
  2713. ret = tg3_nvram_lock(tp);
  2714. if (ret)
  2715. return ret;
  2716. tg3_enable_nvram_access(tp);
  2717. if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM))
  2718. tw32(NVRAM_WRITE1, 0x406);
  2719. grc_mode = tr32(GRC_MODE);
  2720. tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
  2721. if (tg3_flag(tp, NVRAM_BUFFERED) || !tg3_flag(tp, FLASH)) {
  2722. ret = tg3_nvram_write_block_buffered(tp, offset, len,
  2723. buf);
  2724. } else {
  2725. ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
  2726. buf);
  2727. }
  2728. grc_mode = tr32(GRC_MODE);
  2729. tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
  2730. tg3_disable_nvram_access(tp);
  2731. tg3_nvram_unlock(tp);
  2732. }
  2733. if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
  2734. tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
  2735. udelay(40);
  2736. }
  2737. return ret;
  2738. }
  2739. #define RX_CPU_SCRATCH_BASE 0x30000
  2740. #define RX_CPU_SCRATCH_SIZE 0x04000
  2741. #define TX_CPU_SCRATCH_BASE 0x34000
  2742. #define TX_CPU_SCRATCH_SIZE 0x04000
  2743. /* tp->lock is held. */
  2744. static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
  2745. {
  2746. int i;
  2747. BUG_ON(offset == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS));
  2748. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
  2749. u32 val = tr32(GRC_VCPU_EXT_CTRL);
  2750. tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
  2751. return 0;
  2752. }
  2753. if (offset == RX_CPU_BASE) {
  2754. for (i = 0; i < 10000; i++) {
  2755. tw32(offset + CPU_STATE, 0xffffffff);
  2756. tw32(offset + CPU_MODE, CPU_MODE_HALT);
  2757. if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
  2758. break;
  2759. }
  2760. tw32(offset + CPU_STATE, 0xffffffff);
  2761. tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
  2762. udelay(10);
  2763. } else {
  2764. for (i = 0; i < 10000; i++) {
  2765. tw32(offset + CPU_STATE, 0xffffffff);
  2766. tw32(offset + CPU_MODE, CPU_MODE_HALT);
  2767. if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
  2768. break;
  2769. }
  2770. }
  2771. if (i >= 10000) {
  2772. netdev_err(tp->dev, "%s timed out, %s CPU\n",
  2773. __func__, offset == RX_CPU_BASE ? "RX" : "TX");
  2774. return -ENODEV;
  2775. }
  2776. /* Clear firmware's nvram arbitration. */
  2777. if (tg3_flag(tp, NVRAM))
  2778. tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
  2779. return 0;
  2780. }
  2781. struct fw_info {
  2782. unsigned int fw_base;
  2783. unsigned int fw_len;
  2784. const __be32 *fw_data;
  2785. };
  2786. /* tp->lock is held. */
  2787. static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base,
  2788. u32 cpu_scratch_base, int cpu_scratch_size,
  2789. struct fw_info *info)
  2790. {
  2791. int err, lock_err, i;
  2792. void (*write_op)(struct tg3 *, u32, u32);
  2793. if (cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS)) {
  2794. netdev_err(tp->dev,
  2795. "%s: Trying to load TX cpu firmware which is 5705\n",
  2796. __func__);
  2797. return -EINVAL;
  2798. }
  2799. if (tg3_flag(tp, 5705_PLUS))
  2800. write_op = tg3_write_mem;
  2801. else
  2802. write_op = tg3_write_indirect_reg32;
  2803. /* It is possible that bootcode is still loading at this point.
  2804. * Get the nvram lock first before halting the cpu.
  2805. */
  2806. lock_err = tg3_nvram_lock(tp);
  2807. err = tg3_halt_cpu(tp, cpu_base);
  2808. if (!lock_err)
  2809. tg3_nvram_unlock(tp);
  2810. if (err)
  2811. goto out;
  2812. for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
  2813. write_op(tp, cpu_scratch_base + i, 0);
  2814. tw32(cpu_base + CPU_STATE, 0xffffffff);
  2815. tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
  2816. for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
  2817. write_op(tp, (cpu_scratch_base +
  2818. (info->fw_base & 0xffff) +
  2819. (i * sizeof(u32))),
  2820. be32_to_cpu(info->fw_data[i]));
  2821. err = 0;
  2822. out:
  2823. return err;
  2824. }
  2825. /* tp->lock is held. */
  2826. static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
  2827. {
  2828. struct fw_info info;
  2829. const __be32 *fw_data;
  2830. int err, i;
  2831. fw_data = (void *)tp->fw->data;
  2832. /* Firmware blob starts with version numbers, followed by
  2833. start address and length. We are setting complete length.
  2834. length = end_address_of_bss - start_address_of_text.
  2835. Remainder is the blob to be loaded contiguously
  2836. from start address. */
  2837. info.fw_base = be32_to_cpu(fw_data[1]);
  2838. info.fw_len = tp->fw->size - 12;
  2839. info.fw_data = &fw_data[3];
  2840. err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
  2841. RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
  2842. &info);
  2843. if (err)
  2844. return err;
  2845. err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
  2846. TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
  2847. &info);
  2848. if (err)
  2849. return err;
  2850. /* Now startup only the RX cpu. */
  2851. tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
  2852. tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
  2853. for (i = 0; i < 5; i++) {
  2854. if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
  2855. break;
  2856. tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
  2857. tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
  2858. tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
  2859. udelay(1000);
  2860. }
  2861. if (i >= 5) {
  2862. netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
  2863. "should be %08x\n", __func__,
  2864. tr32(RX_CPU_BASE + CPU_PC), info.fw_base);
  2865. return -ENODEV;
  2866. }
  2867. tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
  2868. tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
  2869. return 0;
  2870. }
  2871. /* tp->lock is held. */
  2872. static int tg3_load_tso_firmware(struct tg3 *tp)
  2873. {
  2874. struct fw_info info;
  2875. const __be32 *fw_data;
  2876. unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
  2877. int err, i;
  2878. if (tg3_flag(tp, HW_TSO_1) ||
  2879. tg3_flag(tp, HW_TSO_2) ||
  2880. tg3_flag(tp, HW_TSO_3))
  2881. return 0;
  2882. fw_data = (void *)tp->fw->data;
  2883. /* Firmware blob starts with version numbers, followed by
  2884. start address and length. We are setting complete length.
  2885. length = end_address_of_bss - start_address_of_text.
  2886. Remainder is the blob to be loaded contiguously
  2887. from start address. */
  2888. info.fw_base = be32_to_cpu(fw_data[1]);
  2889. cpu_scratch_size = tp->fw_len;
  2890. info.fw_len = tp->fw->size - 12;
  2891. info.fw_data = &fw_data[3];
  2892. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
  2893. cpu_base = RX_CPU_BASE;
  2894. cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
  2895. } else {
  2896. cpu_base = TX_CPU_BASE;
  2897. cpu_scratch_base = TX_CPU_SCRATCH_BASE;
  2898. cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
  2899. }
  2900. err = tg3_load_firmware_cpu(tp, cpu_base,
  2901. cpu_scratch_base, cpu_scratch_size,
  2902. &info);
  2903. if (err)
  2904. return err;
  2905. /* Now startup the cpu. */
  2906. tw32(cpu_base + CPU_STATE, 0xffffffff);
  2907. tw32_f(cpu_base + CPU_PC, info.fw_base);
  2908. for (i = 0; i < 5; i++) {
  2909. if (tr32(cpu_base + CPU_PC) == info.fw_base)
  2910. break;
  2911. tw32(cpu_base + CPU_STATE, 0xffffffff);
  2912. tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
  2913. tw32_f(cpu_base + CPU_PC, info.fw_base);
  2914. udelay(1000);
  2915. }
  2916. if (i >= 5) {
  2917. netdev_err(tp->dev,
  2918. "%s fails to set CPU PC, is %08x should be %08x\n",
  2919. __func__, tr32(cpu_base + CPU_PC), info.fw_base);
  2920. return -ENODEV;
  2921. }
  2922. tw32(cpu_base + CPU_STATE, 0xffffffff);
  2923. tw32_f(cpu_base + CPU_MODE, 0x00000000);
  2924. return 0;
  2925. }
  2926. /* tp->lock is held. */
  2927. static void __tg3_set_mac_addr(struct tg3 *tp, int skip_mac_1)
  2928. {
  2929. u32 addr_high, addr_low;
  2930. int i;
  2931. addr_high = ((tp->dev->dev_addr[0] << 8) |
  2932. tp->dev->dev_addr[1]);
  2933. addr_low = ((tp->dev->dev_addr[2] << 24) |
  2934. (tp->dev->dev_addr[3] << 16) |
  2935. (tp->dev->dev_addr[4] << 8) |
  2936. (tp->dev->dev_addr[5] << 0));
  2937. for (i = 0; i < 4; i++) {
  2938. if (i == 1 && skip_mac_1)
  2939. continue;
  2940. tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
  2941. tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
  2942. }
  2943. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
  2944. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
  2945. for (i = 0; i < 12; i++) {
  2946. tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
  2947. tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
  2948. }
  2949. }
  2950. addr_high = (tp->dev->dev_addr[0] +
  2951. tp->dev->dev_addr[1] +
  2952. tp->dev->dev_addr[2] +
  2953. tp->dev->dev_addr[3] +
  2954. tp->dev->dev_addr[4] +
  2955. tp->dev->dev_addr[5]) &
  2956. TX_BACKOFF_SEED_MASK;
  2957. tw32(MAC_TX_BACKOFF_SEED, addr_high);
  2958. }
  2959. static void tg3_enable_register_access(struct tg3 *tp)
  2960. {
  2961. /*
  2962. * Make sure register accesses (indirect or otherwise) will function
  2963. * correctly.
  2964. */
  2965. pci_write_config_dword(tp->pdev,
  2966. TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
  2967. }
  2968. static int tg3_power_up(struct tg3 *tp)
  2969. {
  2970. int err;
  2971. tg3_enable_register_access(tp);
  2972. err = pci_set_power_state(tp->pdev, PCI_D0);
  2973. if (!err) {
  2974. /* Switch out of Vaux if it is a NIC */
  2975. tg3_pwrsrc_switch_to_vmain(tp);
  2976. } else {
  2977. netdev_err(tp->dev, "Transition to D0 failed\n");
  2978. }
  2979. return err;
  2980. }
  2981. static int tg3_setup_phy(struct tg3 *, int);
  2982. static int tg3_power_down_prepare(struct tg3 *tp)
  2983. {
  2984. u32 misc_host_ctrl;
  2985. bool device_should_wake, do_low_power;
  2986. tg3_enable_register_access(tp);
  2987. /* Restore the CLKREQ setting. */
  2988. if (tg3_flag(tp, CLKREQ_BUG))
  2989. pcie_capability_set_word(tp->pdev, PCI_EXP_LNKCTL,
  2990. PCI_EXP_LNKCTL_CLKREQ_EN);
  2991. misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
  2992. tw32(TG3PCI_MISC_HOST_CTRL,
  2993. misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
  2994. device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
  2995. tg3_flag(tp, WOL_ENABLE);
  2996. if (tg3_flag(tp, USE_PHYLIB)) {
  2997. do_low_power = false;
  2998. if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
  2999. !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
  3000. struct phy_device *phydev;
  3001. u32 phyid, advertising;
  3002. phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
  3003. tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
  3004. tp->link_config.speed = phydev->speed;
  3005. tp->link_config.duplex = phydev->duplex;
  3006. tp->link_config.autoneg = phydev->autoneg;
  3007. tp->link_config.advertising = phydev->advertising;
  3008. advertising = ADVERTISED_TP |
  3009. ADVERTISED_Pause |
  3010. ADVERTISED_Autoneg |
  3011. ADVERTISED_10baseT_Half;
  3012. if (tg3_flag(tp, ENABLE_ASF) || device_should_wake) {
  3013. if (tg3_flag(tp, WOL_SPEED_100MB))
  3014. advertising |=
  3015. ADVERTISED_100baseT_Half |
  3016. ADVERTISED_100baseT_Full |
  3017. ADVERTISED_10baseT_Full;
  3018. else
  3019. advertising |= ADVERTISED_10baseT_Full;
  3020. }
  3021. phydev->advertising = advertising;
  3022. phy_start_aneg(phydev);
  3023. phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
  3024. if (phyid != PHY_ID_BCMAC131) {
  3025. phyid &= PHY_BCM_OUI_MASK;
  3026. if (phyid == PHY_BCM_OUI_1 ||
  3027. phyid == PHY_BCM_OUI_2 ||
  3028. phyid == PHY_BCM_OUI_3)
  3029. do_low_power = true;
  3030. }
  3031. }
  3032. } else {
  3033. do_low_power = true;
  3034. if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER))
  3035. tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
  3036. if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
  3037. tg3_setup_phy(tp, 0);
  3038. }
  3039. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
  3040. u32 val;
  3041. val = tr32(GRC_VCPU_EXT_CTRL);
  3042. tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
  3043. } else if (!tg3_flag(tp, ENABLE_ASF)) {
  3044. int i;
  3045. u32 val;
  3046. for (i = 0; i < 200; i++) {
  3047. tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
  3048. if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
  3049. break;
  3050. msleep(1);
  3051. }
  3052. }
  3053. if (tg3_flag(tp, WOL_CAP))
  3054. tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
  3055. WOL_DRV_STATE_SHUTDOWN |
  3056. WOL_DRV_WOL |
  3057. WOL_SET_MAGIC_PKT);
  3058. if (device_should_wake) {
  3059. u32 mac_mode;
  3060. if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
  3061. if (do_low_power &&
  3062. !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
  3063. tg3_phy_auxctl_write(tp,
  3064. MII_TG3_AUXCTL_SHDWSEL_PWRCTL,
  3065. MII_TG3_AUXCTL_PCTL_WOL_EN |
  3066. MII_TG3_AUXCTL_PCTL_100TX_LPWR |
  3067. MII_TG3_AUXCTL_PCTL_CL_AB_TXDAC);
  3068. udelay(40);
  3069. }
  3070. if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
  3071. mac_mode = MAC_MODE_PORT_MODE_GMII;
  3072. else
  3073. mac_mode = MAC_MODE_PORT_MODE_MII;
  3074. mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
  3075. if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
  3076. ASIC_REV_5700) {
  3077. u32 speed = tg3_flag(tp, WOL_SPEED_100MB) ?
  3078. SPEED_100 : SPEED_10;
  3079. if (tg3_5700_link_polarity(tp, speed))
  3080. mac_mode |= MAC_MODE_LINK_POLARITY;
  3081. else
  3082. mac_mode &= ~MAC_MODE_LINK_POLARITY;
  3083. }
  3084. } else {
  3085. mac_mode = MAC_MODE_PORT_MODE_TBI;
  3086. }
  3087. if (!tg3_flag(tp, 5750_PLUS))
  3088. tw32(MAC_LED_CTRL, tp->led_ctrl);
  3089. mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
  3090. if ((tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS)) &&
  3091. (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)))
  3092. mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
  3093. if (tg3_flag(tp, ENABLE_APE))
  3094. mac_mode |= MAC_MODE_APE_TX_EN |
  3095. MAC_MODE_APE_RX_EN |
  3096. MAC_MODE_TDE_ENABLE;
  3097. tw32_f(MAC_MODE, mac_mode);
  3098. udelay(100);
  3099. tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
  3100. udelay(10);
  3101. }
  3102. if (!tg3_flag(tp, WOL_SPEED_100MB) &&
  3103. (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
  3104. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
  3105. u32 base_val;
  3106. base_val = tp->pci_clock_ctrl;
  3107. base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
  3108. CLOCK_CTRL_TXCLK_DISABLE);
  3109. tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
  3110. CLOCK_CTRL_PWRDOWN_PLL133, 40);
  3111. } else if (tg3_flag(tp, 5780_CLASS) ||
  3112. tg3_flag(tp, CPMU_PRESENT) ||
  3113. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
  3114. /* do nothing */
  3115. } else if (!(tg3_flag(tp, 5750_PLUS) && tg3_flag(tp, ENABLE_ASF))) {
  3116. u32 newbits1, newbits2;
  3117. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
  3118. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
  3119. newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
  3120. CLOCK_CTRL_TXCLK_DISABLE |
  3121. CLOCK_CTRL_ALTCLK);
  3122. newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
  3123. } else if (tg3_flag(tp, 5705_PLUS)) {
  3124. newbits1 = CLOCK_CTRL_625_CORE;
  3125. newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
  3126. } else {
  3127. newbits1 = CLOCK_CTRL_ALTCLK;
  3128. newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
  3129. }
  3130. tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
  3131. 40);
  3132. tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
  3133. 40);
  3134. if (!tg3_flag(tp, 5705_PLUS)) {
  3135. u32 newbits3;
  3136. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
  3137. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
  3138. newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
  3139. CLOCK_CTRL_TXCLK_DISABLE |
  3140. CLOCK_CTRL_44MHZ_CORE);
  3141. } else {
  3142. newbits3 = CLOCK_CTRL_44MHZ_CORE;
  3143. }
  3144. tw32_wait_f(TG3PCI_CLOCK_CTRL,
  3145. tp->pci_clock_ctrl | newbits3, 40);
  3146. }
  3147. }
  3148. if (!(device_should_wake) && !tg3_flag(tp, ENABLE_ASF))
  3149. tg3_power_down_phy(tp, do_low_power);
  3150. tg3_frob_aux_power(tp, true);
  3151. /* Workaround for unstable PLL clock */
  3152. if ((GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX) ||
  3153. (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX)) {
  3154. u32 val = tr32(0x7d00);
  3155. val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
  3156. tw32(0x7d00, val);
  3157. if (!tg3_flag(tp, ENABLE_ASF)) {
  3158. int err;
  3159. err = tg3_nvram_lock(tp);
  3160. tg3_halt_cpu(tp, RX_CPU_BASE);
  3161. if (!err)
  3162. tg3_nvram_unlock(tp);
  3163. }
  3164. }
  3165. tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
  3166. return 0;
  3167. }
  3168. static void tg3_power_down(struct tg3 *tp)
  3169. {
  3170. tg3_power_down_prepare(tp);
  3171. pci_wake_from_d3(tp->pdev, tg3_flag(tp, WOL_ENABLE));
  3172. pci_set_power_state(tp->pdev, PCI_D3hot);
  3173. }
  3174. static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
  3175. {
  3176. switch (val & MII_TG3_AUX_STAT_SPDMASK) {
  3177. case MII_TG3_AUX_STAT_10HALF:
  3178. *speed = SPEED_10;
  3179. *duplex = DUPLEX_HALF;
  3180. break;
  3181. case MII_TG3_AUX_STAT_10FULL:
  3182. *speed = SPEED_10;
  3183. *duplex = DUPLEX_FULL;
  3184. break;
  3185. case MII_TG3_AUX_STAT_100HALF:
  3186. *speed = SPEED_100;
  3187. *duplex = DUPLEX_HALF;
  3188. break;
  3189. case MII_TG3_AUX_STAT_100FULL:
  3190. *speed = SPEED_100;
  3191. *duplex = DUPLEX_FULL;
  3192. break;
  3193. case MII_TG3_AUX_STAT_1000HALF:
  3194. *speed = SPEED_1000;
  3195. *duplex = DUPLEX_HALF;
  3196. break;
  3197. case MII_TG3_AUX_STAT_1000FULL:
  3198. *speed = SPEED_1000;
  3199. *duplex = DUPLEX_FULL;
  3200. break;
  3201. default:
  3202. if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
  3203. *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
  3204. SPEED_10;
  3205. *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
  3206. DUPLEX_HALF;
  3207. break;
  3208. }
  3209. *speed = SPEED_UNKNOWN;
  3210. *duplex = DUPLEX_UNKNOWN;
  3211. break;
  3212. }
  3213. }
  3214. static int tg3_phy_autoneg_cfg(struct tg3 *tp, u32 advertise, u32 flowctrl)
  3215. {
  3216. int err = 0;
  3217. u32 val, new_adv;
  3218. new_adv = ADVERTISE_CSMA;
  3219. new_adv |= ethtool_adv_to_mii_adv_t(advertise) & ADVERTISE_ALL;
  3220. new_adv |= mii_advertise_flowctrl(flowctrl);
  3221. err = tg3_writephy(tp, MII_ADVERTISE, new_adv);
  3222. if (err)
  3223. goto done;
  3224. if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
  3225. new_adv = ethtool_adv_to_mii_ctrl1000_t(advertise);
  3226. if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
  3227. tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
  3228. new_adv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
  3229. err = tg3_writephy(tp, MII_CTRL1000, new_adv);
  3230. if (err)
  3231. goto done;
  3232. }
  3233. if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
  3234. goto done;
  3235. tw32(TG3_CPMU_EEE_MODE,
  3236. tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
  3237. err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
  3238. if (!err) {
  3239. u32 err2;
  3240. val = 0;
  3241. /* Advertise 100-BaseTX EEE ability */
  3242. if (advertise & ADVERTISED_100baseT_Full)
  3243. val |= MDIO_AN_EEE_ADV_100TX;
  3244. /* Advertise 1000-BaseT EEE ability */
  3245. if (advertise & ADVERTISED_1000baseT_Full)
  3246. val |= MDIO_AN_EEE_ADV_1000T;
  3247. err = tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
  3248. if (err)
  3249. val = 0;
  3250. switch (GET_ASIC_REV(tp->pci_chip_rev_id)) {
  3251. case ASIC_REV_5717:
  3252. case ASIC_REV_57765:
  3253. case ASIC_REV_57766:
  3254. case ASIC_REV_5719:
  3255. /* If we advertised any eee advertisements above... */
  3256. if (val)
  3257. val = MII_TG3_DSP_TAP26_ALNOKO |
  3258. MII_TG3_DSP_TAP26_RMRXSTO |
  3259. MII_TG3_DSP_TAP26_OPCSINPT;
  3260. tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
  3261. /* Fall through */
  3262. case ASIC_REV_5720:
  3263. if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
  3264. tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
  3265. MII_TG3_DSP_CH34TP2_HIBW01);
  3266. }
  3267. err2 = TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
  3268. if (!err)
  3269. err = err2;
  3270. }
  3271. done:
  3272. return err;
  3273. }
  3274. static void tg3_phy_copper_begin(struct tg3 *tp)
  3275. {
  3276. if (tp->link_config.autoneg == AUTONEG_ENABLE ||
  3277. (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
  3278. u32 adv, fc;
  3279. if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
  3280. adv = ADVERTISED_10baseT_Half |
  3281. ADVERTISED_10baseT_Full;
  3282. if (tg3_flag(tp, WOL_SPEED_100MB))
  3283. adv |= ADVERTISED_100baseT_Half |
  3284. ADVERTISED_100baseT_Full;
  3285. fc = FLOW_CTRL_TX | FLOW_CTRL_RX;
  3286. } else {
  3287. adv = tp->link_config.advertising;
  3288. if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
  3289. adv &= ~(ADVERTISED_1000baseT_Half |
  3290. ADVERTISED_1000baseT_Full);
  3291. fc = tp->link_config.flowctrl;
  3292. }
  3293. tg3_phy_autoneg_cfg(tp, adv, fc);
  3294. tg3_writephy(tp, MII_BMCR,
  3295. BMCR_ANENABLE | BMCR_ANRESTART);
  3296. } else {
  3297. int i;
  3298. u32 bmcr, orig_bmcr;
  3299. tp->link_config.active_speed = tp->link_config.speed;
  3300. tp->link_config.active_duplex = tp->link_config.duplex;
  3301. bmcr = 0;
  3302. switch (tp->link_config.speed) {
  3303. default:
  3304. case SPEED_10:
  3305. break;
  3306. case SPEED_100:
  3307. bmcr |= BMCR_SPEED100;
  3308. break;
  3309. case SPEED_1000:
  3310. bmcr |= BMCR_SPEED1000;
  3311. break;
  3312. }
  3313. if (tp->link_config.duplex == DUPLEX_FULL)
  3314. bmcr |= BMCR_FULLDPLX;
  3315. if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
  3316. (bmcr != orig_bmcr)) {
  3317. tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
  3318. for (i = 0; i < 1500; i++) {
  3319. u32 tmp;
  3320. udelay(10);
  3321. if (tg3_readphy(tp, MII_BMSR, &tmp) ||
  3322. tg3_readphy(tp, MII_BMSR, &tmp))
  3323. continue;
  3324. if (!(tmp & BMSR_LSTATUS)) {
  3325. udelay(40);
  3326. break;
  3327. }
  3328. }
  3329. tg3_writephy(tp, MII_BMCR, bmcr);
  3330. udelay(40);
  3331. }
  3332. }
  3333. }
  3334. static int tg3_init_5401phy_dsp(struct tg3 *tp)
  3335. {
  3336. int err;
  3337. /* Turn off tap power management. */
  3338. /* Set Extended packet length bit */
  3339. err = tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
  3340. err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
  3341. err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
  3342. err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
  3343. err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
  3344. err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
  3345. udelay(40);
  3346. return err;
  3347. }
  3348. static bool tg3_phy_copper_an_config_ok(struct tg3 *tp, u32 *lcladv)
  3349. {
  3350. u32 advmsk, tgtadv, advertising;
  3351. advertising = tp->link_config.advertising;
  3352. tgtadv = ethtool_adv_to_mii_adv_t(advertising) & ADVERTISE_ALL;
  3353. advmsk = ADVERTISE_ALL;
  3354. if (tp->link_config.active_duplex == DUPLEX_FULL) {
  3355. tgtadv |= mii_advertise_flowctrl(tp->link_config.flowctrl);
  3356. advmsk |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
  3357. }
  3358. if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
  3359. return false;
  3360. if ((*lcladv & advmsk) != tgtadv)
  3361. return false;
  3362. if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
  3363. u32 tg3_ctrl;
  3364. tgtadv = ethtool_adv_to_mii_ctrl1000_t(advertising);
  3365. if (tg3_readphy(tp, MII_CTRL1000, &tg3_ctrl))
  3366. return false;
  3367. if (tgtadv &&
  3368. (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
  3369. tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)) {
  3370. tgtadv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
  3371. tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL |
  3372. CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
  3373. } else {
  3374. tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL);
  3375. }
  3376. if (tg3_ctrl != tgtadv)
  3377. return false;
  3378. }
  3379. return true;
  3380. }
  3381. static bool tg3_phy_copper_fetch_rmtadv(struct tg3 *tp, u32 *rmtadv)
  3382. {
  3383. u32 lpeth = 0;
  3384. if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
  3385. u32 val;
  3386. if (tg3_readphy(tp, MII_STAT1000, &val))
  3387. return false;
  3388. lpeth = mii_stat1000_to_ethtool_lpa_t(val);
  3389. }
  3390. if (tg3_readphy(tp, MII_LPA, rmtadv))
  3391. return false;
  3392. lpeth |= mii_lpa_to_ethtool_lpa_t(*rmtadv);
  3393. tp->link_config.rmt_adv = lpeth;
  3394. return true;
  3395. }
  3396. static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
  3397. {
  3398. int current_link_up;
  3399. u32 bmsr, val;
  3400. u32 lcl_adv, rmt_adv;
  3401. u16 current_speed;
  3402. u8 current_duplex;
  3403. int i, err;
  3404. tw32(MAC_EVENT, 0);
  3405. tw32_f(MAC_STATUS,
  3406. (MAC_STATUS_SYNC_CHANGED |
  3407. MAC_STATUS_CFG_CHANGED |
  3408. MAC_STATUS_MI_COMPLETION |
  3409. MAC_STATUS_LNKSTATE_CHANGED));
  3410. udelay(40);
  3411. if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
  3412. tw32_f(MAC_MI_MODE,
  3413. (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
  3414. udelay(80);
  3415. }
  3416. tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, 0);
  3417. /* Some third-party PHYs need to be reset on link going
  3418. * down.
  3419. */
  3420. if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
  3421. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
  3422. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
  3423. netif_carrier_ok(tp->dev)) {
  3424. tg3_readphy(tp, MII_BMSR, &bmsr);
  3425. if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
  3426. !(bmsr & BMSR_LSTATUS))
  3427. force_reset = 1;
  3428. }
  3429. if (force_reset)
  3430. tg3_phy_reset(tp);
  3431. if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
  3432. tg3_readphy(tp, MII_BMSR, &bmsr);
  3433. if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
  3434. !tg3_flag(tp, INIT_COMPLETE))
  3435. bmsr = 0;
  3436. if (!(bmsr & BMSR_LSTATUS)) {
  3437. err = tg3_init_5401phy_dsp(tp);
  3438. if (err)
  3439. return err;
  3440. tg3_readphy(tp, MII_BMSR, &bmsr);
  3441. for (i = 0; i < 1000; i++) {
  3442. udelay(10);
  3443. if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
  3444. (bmsr & BMSR_LSTATUS)) {
  3445. udelay(40);
  3446. break;
  3447. }
  3448. }
  3449. if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
  3450. TG3_PHY_REV_BCM5401_B0 &&
  3451. !(bmsr & BMSR_LSTATUS) &&
  3452. tp->link_config.active_speed == SPEED_1000) {
  3453. err = tg3_phy_reset(tp);
  3454. if (!err)
  3455. err = tg3_init_5401phy_dsp(tp);
  3456. if (err)
  3457. return err;
  3458. }
  3459. }
  3460. } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
  3461. tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
  3462. /* 5701 {A0,B0} CRC bug workaround */
  3463. tg3_writephy(tp, 0x15, 0x0a75);
  3464. tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
  3465. tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
  3466. tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
  3467. }
  3468. /* Clear pending interrupts... */
  3469. tg3_readphy(tp, MII_TG3_ISTAT, &val);
  3470. tg3_readphy(tp, MII_TG3_ISTAT, &val);
  3471. if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
  3472. tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
  3473. else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
  3474. tg3_writephy(tp, MII_TG3_IMASK, ~0);
  3475. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
  3476. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
  3477. if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
  3478. tg3_writephy(tp, MII_TG3_EXT_CTRL,
  3479. MII_TG3_EXT_CTRL_LNK3_LED_MODE);
  3480. else
  3481. tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
  3482. }
  3483. current_link_up = 0;
  3484. current_speed = SPEED_UNKNOWN;
  3485. current_duplex = DUPLEX_UNKNOWN;
  3486. tp->phy_flags &= ~TG3_PHYFLG_MDIX_STATE;
  3487. tp->link_config.rmt_adv = 0;
  3488. if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
  3489. err = tg3_phy_auxctl_read(tp,
  3490. MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
  3491. &val);
  3492. if (!err && !(val & (1 << 10))) {
  3493. tg3_phy_auxctl_write(tp,
  3494. MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
  3495. val | (1 << 10));
  3496. goto relink;
  3497. }
  3498. }
  3499. bmsr = 0;
  3500. for (i = 0; i < 100; i++) {
  3501. tg3_readphy(tp, MII_BMSR, &bmsr);
  3502. if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
  3503. (bmsr & BMSR_LSTATUS))
  3504. break;
  3505. udelay(40);
  3506. }
  3507. if (bmsr & BMSR_LSTATUS) {
  3508. u32 aux_stat, bmcr;
  3509. tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
  3510. for (i = 0; i < 2000; i++) {
  3511. udelay(10);
  3512. if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
  3513. aux_stat)
  3514. break;
  3515. }
  3516. tg3_aux_stat_to_speed_duplex(tp, aux_stat,
  3517. &current_speed,
  3518. &current_duplex);
  3519. bmcr = 0;
  3520. for (i = 0; i < 200; i++) {
  3521. tg3_readphy(tp, MII_BMCR, &bmcr);
  3522. if (tg3_readphy(tp, MII_BMCR, &bmcr))
  3523. continue;
  3524. if (bmcr && bmcr != 0x7fff)
  3525. break;
  3526. udelay(10);
  3527. }
  3528. lcl_adv = 0;
  3529. rmt_adv = 0;
  3530. tp->link_config.active_speed = current_speed;
  3531. tp->link_config.active_duplex = current_duplex;
  3532. if (tp->link_config.autoneg == AUTONEG_ENABLE) {
  3533. if ((bmcr & BMCR_ANENABLE) &&
  3534. tg3_phy_copper_an_config_ok(tp, &lcl_adv) &&
  3535. tg3_phy_copper_fetch_rmtadv(tp, &rmt_adv))
  3536. current_link_up = 1;
  3537. } else {
  3538. if (!(bmcr & BMCR_ANENABLE) &&
  3539. tp->link_config.speed == current_speed &&
  3540. tp->link_config.duplex == current_duplex &&
  3541. tp->link_config.flowctrl ==
  3542. tp->link_config.active_flowctrl) {
  3543. current_link_up = 1;
  3544. }
  3545. }
  3546. if (current_link_up == 1 &&
  3547. tp->link_config.active_duplex == DUPLEX_FULL) {
  3548. u32 reg, bit;
  3549. if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
  3550. reg = MII_TG3_FET_GEN_STAT;
  3551. bit = MII_TG3_FET_GEN_STAT_MDIXSTAT;
  3552. } else {
  3553. reg = MII_TG3_EXT_STAT;
  3554. bit = MII_TG3_EXT_STAT_MDIX;
  3555. }
  3556. if (!tg3_readphy(tp, reg, &val) && (val & bit))
  3557. tp->phy_flags |= TG3_PHYFLG_MDIX_STATE;
  3558. tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
  3559. }
  3560. }
  3561. relink:
  3562. if (current_link_up == 0 || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
  3563. tg3_phy_copper_begin(tp);
  3564. tg3_readphy(tp, MII_BMSR, &bmsr);
  3565. if ((!tg3_readphy(tp, MII_BMSR, &bmsr) && (bmsr & BMSR_LSTATUS)) ||
  3566. (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
  3567. current_link_up = 1;
  3568. }
  3569. tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
  3570. if (current_link_up == 1) {
  3571. if (tp->link_config.active_speed == SPEED_100 ||
  3572. tp->link_config.active_speed == SPEED_10)
  3573. tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
  3574. else
  3575. tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
  3576. } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
  3577. tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
  3578. else
  3579. tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
  3580. tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
  3581. if (tp->link_config.active_duplex == DUPLEX_HALF)
  3582. tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
  3583. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
  3584. if (current_link_up == 1 &&
  3585. tg3_5700_link_polarity(tp, tp->link_config.active_speed))
  3586. tp->mac_mode |= MAC_MODE_LINK_POLARITY;
  3587. else
  3588. tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
  3589. }
  3590. /* ??? Without this setting Netgear GA302T PHY does not
  3591. * ??? send/receive packets...
  3592. */
  3593. if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
  3594. tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
  3595. tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
  3596. tw32_f(MAC_MI_MODE, tp->mi_mode);
  3597. udelay(80);
  3598. }
  3599. tw32_f(MAC_MODE, tp->mac_mode);
  3600. udelay(40);
  3601. tg3_phy_eee_adjust(tp, current_link_up);
  3602. if (tg3_flag(tp, USE_LINKCHG_REG)) {
  3603. /* Polled via timer. */
  3604. tw32_f(MAC_EVENT, 0);
  3605. } else {
  3606. tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
  3607. }
  3608. udelay(40);
  3609. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
  3610. current_link_up == 1 &&
  3611. tp->link_config.active_speed == SPEED_1000 &&
  3612. (tg3_flag(tp, PCIX_MODE) || tg3_flag(tp, PCI_HIGH_SPEED))) {
  3613. udelay(120);
  3614. tw32_f(MAC_STATUS,
  3615. (MAC_STATUS_SYNC_CHANGED |
  3616. MAC_STATUS_CFG_CHANGED));
  3617. udelay(40);
  3618. tg3_write_mem(tp,
  3619. NIC_SRAM_FIRMWARE_MBOX,
  3620. NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
  3621. }
  3622. /* Prevent send BD corruption. */
  3623. if (tg3_flag(tp, CLKREQ_BUG)) {
  3624. if (tp->link_config.active_speed == SPEED_100 ||
  3625. tp->link_config.active_speed == SPEED_10)
  3626. pcie_capability_clear_word(tp->pdev, PCI_EXP_LNKCTL,
  3627. PCI_EXP_LNKCTL_CLKREQ_EN);
  3628. else
  3629. pcie_capability_set_word(tp->pdev, PCI_EXP_LNKCTL,
  3630. PCI_EXP_LNKCTL_CLKREQ_EN);
  3631. }
  3632. if (current_link_up != netif_carrier_ok(tp->dev)) {
  3633. if (current_link_up)
  3634. netif_carrier_on(tp->dev);
  3635. else
  3636. netif_carrier_off(tp->dev);
  3637. tg3_link_report(tp);
  3638. }
  3639. return 0;
  3640. }
  3641. struct tg3_fiber_aneginfo {
  3642. int state;
  3643. #define ANEG_STATE_UNKNOWN 0
  3644. #define ANEG_STATE_AN_ENABLE 1
  3645. #define ANEG_STATE_RESTART_INIT 2
  3646. #define ANEG_STATE_RESTART 3
  3647. #define ANEG_STATE_DISABLE_LINK_OK 4
  3648. #define ANEG_STATE_ABILITY_DETECT_INIT 5
  3649. #define ANEG_STATE_ABILITY_DETECT 6
  3650. #define ANEG_STATE_ACK_DETECT_INIT 7
  3651. #define ANEG_STATE_ACK_DETECT 8
  3652. #define ANEG_STATE_COMPLETE_ACK_INIT 9
  3653. #define ANEG_STATE_COMPLETE_ACK 10
  3654. #define ANEG_STATE_IDLE_DETECT_INIT 11
  3655. #define ANEG_STATE_IDLE_DETECT 12
  3656. #define ANEG_STATE_LINK_OK 13
  3657. #define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
  3658. #define ANEG_STATE_NEXT_PAGE_WAIT 15
  3659. u32 flags;
  3660. #define MR_AN_ENABLE 0x00000001
  3661. #define MR_RESTART_AN 0x00000002
  3662. #define MR_AN_COMPLETE 0x00000004
  3663. #define MR_PAGE_RX 0x00000008
  3664. #define MR_NP_LOADED 0x00000010
  3665. #define MR_TOGGLE_TX 0x00000020
  3666. #define MR_LP_ADV_FULL_DUPLEX 0x00000040
  3667. #define MR_LP_ADV_HALF_DUPLEX 0x00000080
  3668. #define MR_LP_ADV_SYM_PAUSE 0x00000100
  3669. #define MR_LP_ADV_ASYM_PAUSE 0x00000200
  3670. #define MR_LP_ADV_REMOTE_FAULT1 0x00000400
  3671. #define MR_LP_ADV_REMOTE_FAULT2 0x00000800
  3672. #define MR_LP_ADV_NEXT_PAGE 0x00001000
  3673. #define MR_TOGGLE_RX 0x00002000
  3674. #define MR_NP_RX 0x00004000
  3675. #define MR_LINK_OK 0x80000000
  3676. unsigned long link_time, cur_time;
  3677. u32 ability_match_cfg;
  3678. int ability_match_count;
  3679. char ability_match, idle_match, ack_match;
  3680. u32 txconfig, rxconfig;
  3681. #define ANEG_CFG_NP 0x00000080
  3682. #define ANEG_CFG_ACK 0x00000040
  3683. #define ANEG_CFG_RF2 0x00000020
  3684. #define ANEG_CFG_RF1 0x00000010
  3685. #define ANEG_CFG_PS2 0x00000001
  3686. #define ANEG_CFG_PS1 0x00008000
  3687. #define ANEG_CFG_HD 0x00004000
  3688. #define ANEG_CFG_FD 0x00002000
  3689. #define ANEG_CFG_INVAL 0x00001f06
  3690. };
  3691. #define ANEG_OK 0
  3692. #define ANEG_DONE 1
  3693. #define ANEG_TIMER_ENAB 2
  3694. #define ANEG_FAILED -1
  3695. #define ANEG_STATE_SETTLE_TIME 10000
  3696. static int tg3_fiber_aneg_smachine(struct tg3 *tp,
  3697. struct tg3_fiber_aneginfo *ap)
  3698. {
  3699. u16 flowctrl;
  3700. unsigned long delta;
  3701. u32 rx_cfg_reg;
  3702. int ret;
  3703. if (ap->state == ANEG_STATE_UNKNOWN) {
  3704. ap->rxconfig = 0;
  3705. ap->link_time = 0;
  3706. ap->cur_time = 0;
  3707. ap->ability_match_cfg = 0;
  3708. ap->ability_match_count = 0;
  3709. ap->ability_match = 0;
  3710. ap->idle_match = 0;
  3711. ap->ack_match = 0;
  3712. }
  3713. ap->cur_time++;
  3714. if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
  3715. rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
  3716. if (rx_cfg_reg != ap->ability_match_cfg) {
  3717. ap->ability_match_cfg = rx_cfg_reg;
  3718. ap->ability_match = 0;
  3719. ap->ability_match_count = 0;
  3720. } else {
  3721. if (++ap->ability_match_count > 1) {
  3722. ap->ability_match = 1;
  3723. ap->ability_match_cfg = rx_cfg_reg;
  3724. }
  3725. }
  3726. if (rx_cfg_reg & ANEG_CFG_ACK)
  3727. ap->ack_match = 1;
  3728. else
  3729. ap->ack_match = 0;
  3730. ap->idle_match = 0;
  3731. } else {
  3732. ap->idle_match = 1;
  3733. ap->ability_match_cfg = 0;
  3734. ap->ability_match_count = 0;
  3735. ap->ability_match = 0;
  3736. ap->ack_match = 0;
  3737. rx_cfg_reg = 0;
  3738. }
  3739. ap->rxconfig = rx_cfg_reg;
  3740. ret = ANEG_OK;
  3741. switch (ap->state) {
  3742. case ANEG_STATE_UNKNOWN:
  3743. if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
  3744. ap->state = ANEG_STATE_AN_ENABLE;
  3745. /* fallthru */
  3746. case ANEG_STATE_AN_ENABLE:
  3747. ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
  3748. if (ap->flags & MR_AN_ENABLE) {
  3749. ap->link_time = 0;
  3750. ap->cur_time = 0;
  3751. ap->ability_match_cfg = 0;
  3752. ap->ability_match_count = 0;
  3753. ap->ability_match = 0;
  3754. ap->idle_match = 0;
  3755. ap->ack_match = 0;
  3756. ap->state = ANEG_STATE_RESTART_INIT;
  3757. } else {
  3758. ap->state = ANEG_STATE_DISABLE_LINK_OK;
  3759. }
  3760. break;
  3761. case ANEG_STATE_RESTART_INIT:
  3762. ap->link_time = ap->cur_time;
  3763. ap->flags &= ~(MR_NP_LOADED);
  3764. ap->txconfig = 0;
  3765. tw32(MAC_TX_AUTO_NEG, 0);
  3766. tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
  3767. tw32_f(MAC_MODE, tp->mac_mode);
  3768. udelay(40);
  3769. ret = ANEG_TIMER_ENAB;
  3770. ap->state = ANEG_STATE_RESTART;
  3771. /* fallthru */
  3772. case ANEG_STATE_RESTART:
  3773. delta = ap->cur_time - ap->link_time;
  3774. if (delta > ANEG_STATE_SETTLE_TIME)
  3775. ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
  3776. else
  3777. ret = ANEG_TIMER_ENAB;
  3778. break;
  3779. case ANEG_STATE_DISABLE_LINK_OK:
  3780. ret = ANEG_DONE;
  3781. break;
  3782. case ANEG_STATE_ABILITY_DETECT_INIT:
  3783. ap->flags &= ~(MR_TOGGLE_TX);
  3784. ap->txconfig = ANEG_CFG_FD;
  3785. flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
  3786. if (flowctrl & ADVERTISE_1000XPAUSE)
  3787. ap->txconfig |= ANEG_CFG_PS1;
  3788. if (flowctrl & ADVERTISE_1000XPSE_ASYM)
  3789. ap->txconfig |= ANEG_CFG_PS2;
  3790. tw32(MAC_TX_AUTO_NEG, ap->txconfig);
  3791. tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
  3792. tw32_f(MAC_MODE, tp->mac_mode);
  3793. udelay(40);
  3794. ap->state = ANEG_STATE_ABILITY_DETECT;
  3795. break;
  3796. case ANEG_STATE_ABILITY_DETECT:
  3797. if (ap->ability_match != 0 && ap->rxconfig != 0)
  3798. ap->state = ANEG_STATE_ACK_DETECT_INIT;
  3799. break;
  3800. case ANEG_STATE_ACK_DETECT_INIT:
  3801. ap->txconfig |= ANEG_CFG_ACK;
  3802. tw32(MAC_TX_AUTO_NEG, ap->txconfig);
  3803. tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
  3804. tw32_f(MAC_MODE, tp->mac_mode);
  3805. udelay(40);
  3806. ap->state = ANEG_STATE_ACK_DETECT;
  3807. /* fallthru */
  3808. case ANEG_STATE_ACK_DETECT:
  3809. if (ap->ack_match != 0) {
  3810. if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
  3811. (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
  3812. ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
  3813. } else {
  3814. ap->state = ANEG_STATE_AN_ENABLE;
  3815. }
  3816. } else if (ap->ability_match != 0 &&
  3817. ap->rxconfig == 0) {
  3818. ap->state = ANEG_STATE_AN_ENABLE;
  3819. }
  3820. break;
  3821. case ANEG_STATE_COMPLETE_ACK_INIT:
  3822. if (ap->rxconfig & ANEG_CFG_INVAL) {
  3823. ret = ANEG_FAILED;
  3824. break;
  3825. }
  3826. ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
  3827. MR_LP_ADV_HALF_DUPLEX |
  3828. MR_LP_ADV_SYM_PAUSE |
  3829. MR_LP_ADV_ASYM_PAUSE |
  3830. MR_LP_ADV_REMOTE_FAULT1 |
  3831. MR_LP_ADV_REMOTE_FAULT2 |
  3832. MR_LP_ADV_NEXT_PAGE |
  3833. MR_TOGGLE_RX |
  3834. MR_NP_RX);
  3835. if (ap->rxconfig & ANEG_CFG_FD)
  3836. ap->flags |= MR_LP_ADV_FULL_DUPLEX;
  3837. if (ap->rxconfig & ANEG_CFG_HD)
  3838. ap->flags |= MR_LP_ADV_HALF_DUPLEX;
  3839. if (ap->rxconfig & ANEG_CFG_PS1)
  3840. ap->flags |= MR_LP_ADV_SYM_PAUSE;
  3841. if (ap->rxconfig & ANEG_CFG_PS2)
  3842. ap->flags |= MR_LP_ADV_ASYM_PAUSE;
  3843. if (ap->rxconfig & ANEG_CFG_RF1)
  3844. ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
  3845. if (ap->rxconfig & ANEG_CFG_RF2)
  3846. ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
  3847. if (ap->rxconfig & ANEG_CFG_NP)
  3848. ap->flags |= MR_LP_ADV_NEXT_PAGE;
  3849. ap->link_time = ap->cur_time;
  3850. ap->flags ^= (MR_TOGGLE_TX);
  3851. if (ap->rxconfig & 0x0008)
  3852. ap->flags |= MR_TOGGLE_RX;
  3853. if (ap->rxconfig & ANEG_CFG_NP)
  3854. ap->flags |= MR_NP_RX;
  3855. ap->flags |= MR_PAGE_RX;
  3856. ap->state = ANEG_STATE_COMPLETE_ACK;
  3857. ret = ANEG_TIMER_ENAB;
  3858. break;
  3859. case ANEG_STATE_COMPLETE_ACK:
  3860. if (ap->ability_match != 0 &&
  3861. ap->rxconfig == 0) {
  3862. ap->state = ANEG_STATE_AN_ENABLE;
  3863. break;
  3864. }
  3865. delta = ap->cur_time - ap->link_time;
  3866. if (delta > ANEG_STATE_SETTLE_TIME) {
  3867. if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
  3868. ap->state = ANEG_STATE_IDLE_DETECT_INIT;
  3869. } else {
  3870. if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
  3871. !(ap->flags & MR_NP_RX)) {
  3872. ap->state = ANEG_STATE_IDLE_DETECT_INIT;
  3873. } else {
  3874. ret = ANEG_FAILED;
  3875. }
  3876. }
  3877. }
  3878. break;
  3879. case ANEG_STATE_IDLE_DETECT_INIT:
  3880. ap->link_time = ap->cur_time;
  3881. tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
  3882. tw32_f(MAC_MODE, tp->mac_mode);
  3883. udelay(40);
  3884. ap->state = ANEG_STATE_IDLE_DETECT;
  3885. ret = ANEG_TIMER_ENAB;
  3886. break;
  3887. case ANEG_STATE_IDLE_DETECT:
  3888. if (ap->ability_match != 0 &&
  3889. ap->rxconfig == 0) {
  3890. ap->state = ANEG_STATE_AN_ENABLE;
  3891. break;
  3892. }
  3893. delta = ap->cur_time - ap->link_time;
  3894. if (delta > ANEG_STATE_SETTLE_TIME) {
  3895. /* XXX another gem from the Broadcom driver :( */
  3896. ap->state = ANEG_STATE_LINK_OK;
  3897. }
  3898. break;
  3899. case ANEG_STATE_LINK_OK:
  3900. ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
  3901. ret = ANEG_DONE;
  3902. break;
  3903. case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
  3904. /* ??? unimplemented */
  3905. break;
  3906. case ANEG_STATE_NEXT_PAGE_WAIT:
  3907. /* ??? unimplemented */
  3908. break;
  3909. default:
  3910. ret = ANEG_FAILED;
  3911. break;
  3912. }
  3913. return ret;
  3914. }
  3915. static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
  3916. {
  3917. int res = 0;
  3918. struct tg3_fiber_aneginfo aninfo;
  3919. int status = ANEG_FAILED;
  3920. unsigned int tick;
  3921. u32 tmp;
  3922. tw32_f(MAC_TX_AUTO_NEG, 0);
  3923. tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
  3924. tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
  3925. udelay(40);
  3926. tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
  3927. udelay(40);
  3928. memset(&aninfo, 0, sizeof(aninfo));
  3929. aninfo.flags |= MR_AN_ENABLE;
  3930. aninfo.state = ANEG_STATE_UNKNOWN;
  3931. aninfo.cur_time = 0;
  3932. tick = 0;
  3933. while (++tick < 195000) {
  3934. status = tg3_fiber_aneg_smachine(tp, &aninfo);
  3935. if (status == ANEG_DONE || status == ANEG_FAILED)
  3936. break;
  3937. udelay(1);
  3938. }
  3939. tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
  3940. tw32_f(MAC_MODE, tp->mac_mode);
  3941. udelay(40);
  3942. *txflags = aninfo.txconfig;
  3943. *rxflags = aninfo.flags;
  3944. if (status == ANEG_DONE &&
  3945. (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
  3946. MR_LP_ADV_FULL_DUPLEX)))
  3947. res = 1;
  3948. return res;
  3949. }
  3950. static void tg3_init_bcm8002(struct tg3 *tp)
  3951. {
  3952. u32 mac_status = tr32(MAC_STATUS);
  3953. int i;
  3954. /* Reset when initting first time or we have a link. */
  3955. if (tg3_flag(tp, INIT_COMPLETE) &&
  3956. !(mac_status & MAC_STATUS_PCS_SYNCED))
  3957. return;
  3958. /* Set PLL lock range. */
  3959. tg3_writephy(tp, 0x16, 0x8007);
  3960. /* SW reset */
  3961. tg3_writephy(tp, MII_BMCR, BMCR_RESET);
  3962. /* Wait for reset to complete. */
  3963. /* XXX schedule_timeout() ... */
  3964. for (i = 0; i < 500; i++)
  3965. udelay(10);
  3966. /* Config mode; select PMA/Ch 1 regs. */
  3967. tg3_writephy(tp, 0x10, 0x8411);
  3968. /* Enable auto-lock and comdet, select txclk for tx. */
  3969. tg3_writephy(tp, 0x11, 0x0a10);
  3970. tg3_writephy(tp, 0x18, 0x00a0);
  3971. tg3_writephy(tp, 0x16, 0x41ff);
  3972. /* Assert and deassert POR. */
  3973. tg3_writephy(tp, 0x13, 0x0400);
  3974. udelay(40);
  3975. tg3_writephy(tp, 0x13, 0x0000);
  3976. tg3_writephy(tp, 0x11, 0x0a50);
  3977. udelay(40);
  3978. tg3_writephy(tp, 0x11, 0x0a10);
  3979. /* Wait for signal to stabilize */
  3980. /* XXX schedule_timeout() ... */
  3981. for (i = 0; i < 15000; i++)
  3982. udelay(10);
  3983. /* Deselect the channel register so we can read the PHYID
  3984. * later.
  3985. */
  3986. tg3_writephy(tp, 0x10, 0x8011);
  3987. }
  3988. static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
  3989. {
  3990. u16 flowctrl;
  3991. u32 sg_dig_ctrl, sg_dig_status;
  3992. u32 serdes_cfg, expected_sg_dig_ctrl;
  3993. int workaround, port_a;
  3994. int current_link_up;
  3995. serdes_cfg = 0;
  3996. expected_sg_dig_ctrl = 0;
  3997. workaround = 0;
  3998. port_a = 1;
  3999. current_link_up = 0;
  4000. if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
  4001. tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
  4002. workaround = 1;
  4003. if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
  4004. port_a = 0;
  4005. /* preserve bits 0-11,13,14 for signal pre-emphasis */
  4006. /* preserve bits 20-23 for voltage regulator */
  4007. serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
  4008. }
  4009. sg_dig_ctrl = tr32(SG_DIG_CTRL);
  4010. if (tp->link_config.autoneg != AUTONEG_ENABLE) {
  4011. if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
  4012. if (workaround) {
  4013. u32 val = serdes_cfg;
  4014. if (port_a)
  4015. val |= 0xc010000;
  4016. else
  4017. val |= 0x4010000;
  4018. tw32_f(MAC_SERDES_CFG, val);
  4019. }
  4020. tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
  4021. }
  4022. if (mac_status & MAC_STATUS_PCS_SYNCED) {
  4023. tg3_setup_flow_control(tp, 0, 0);
  4024. current_link_up = 1;
  4025. }
  4026. goto out;
  4027. }
  4028. /* Want auto-negotiation. */
  4029. expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
  4030. flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
  4031. if (flowctrl & ADVERTISE_1000XPAUSE)
  4032. expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
  4033. if (flowctrl & ADVERTISE_1000XPSE_ASYM)
  4034. expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
  4035. if (sg_dig_ctrl != expected_sg_dig_ctrl) {
  4036. if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
  4037. tp->serdes_counter &&
  4038. ((mac_status & (MAC_STATUS_PCS_SYNCED |
  4039. MAC_STATUS_RCVD_CFG)) ==
  4040. MAC_STATUS_PCS_SYNCED)) {
  4041. tp->serdes_counter--;
  4042. current_link_up = 1;
  4043. goto out;
  4044. }
  4045. restart_autoneg:
  4046. if (workaround)
  4047. tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
  4048. tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
  4049. udelay(5);
  4050. tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
  4051. tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
  4052. tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
  4053. } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
  4054. MAC_STATUS_SIGNAL_DET)) {
  4055. sg_dig_status = tr32(SG_DIG_STATUS);
  4056. mac_status = tr32(MAC_STATUS);
  4057. if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
  4058. (mac_status & MAC_STATUS_PCS_SYNCED)) {
  4059. u32 local_adv = 0, remote_adv = 0;
  4060. if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
  4061. local_adv |= ADVERTISE_1000XPAUSE;
  4062. if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
  4063. local_adv |= ADVERTISE_1000XPSE_ASYM;
  4064. if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
  4065. remote_adv |= LPA_1000XPAUSE;
  4066. if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
  4067. remote_adv |= LPA_1000XPAUSE_ASYM;
  4068. tp->link_config.rmt_adv =
  4069. mii_adv_to_ethtool_adv_x(remote_adv);
  4070. tg3_setup_flow_control(tp, local_adv, remote_adv);
  4071. current_link_up = 1;
  4072. tp->serdes_counter = 0;
  4073. tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
  4074. } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
  4075. if (tp->serdes_counter)
  4076. tp->serdes_counter--;
  4077. else {
  4078. if (workaround) {
  4079. u32 val = serdes_cfg;
  4080. if (port_a)
  4081. val |= 0xc010000;
  4082. else
  4083. val |= 0x4010000;
  4084. tw32_f(MAC_SERDES_CFG, val);
  4085. }
  4086. tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
  4087. udelay(40);
  4088. /* Link parallel detection - link is up */
  4089. /* only if we have PCS_SYNC and not */
  4090. /* receiving config code words */
  4091. mac_status = tr32(MAC_STATUS);
  4092. if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
  4093. !(mac_status & MAC_STATUS_RCVD_CFG)) {
  4094. tg3_setup_flow_control(tp, 0, 0);
  4095. current_link_up = 1;
  4096. tp->phy_flags |=
  4097. TG3_PHYFLG_PARALLEL_DETECT;
  4098. tp->serdes_counter =
  4099. SERDES_PARALLEL_DET_TIMEOUT;
  4100. } else
  4101. goto restart_autoneg;
  4102. }
  4103. }
  4104. } else {
  4105. tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
  4106. tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
  4107. }
  4108. out:
  4109. return current_link_up;
  4110. }
  4111. static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
  4112. {
  4113. int current_link_up = 0;
  4114. if (!(mac_status & MAC_STATUS_PCS_SYNCED))
  4115. goto out;
  4116. if (tp->link_config.autoneg == AUTONEG_ENABLE) {
  4117. u32 txflags, rxflags;
  4118. int i;
  4119. if (fiber_autoneg(tp, &txflags, &rxflags)) {
  4120. u32 local_adv = 0, remote_adv = 0;
  4121. if (txflags & ANEG_CFG_PS1)
  4122. local_adv |= ADVERTISE_1000XPAUSE;
  4123. if (txflags & ANEG_CFG_PS2)
  4124. local_adv |= ADVERTISE_1000XPSE_ASYM;
  4125. if (rxflags & MR_LP_ADV_SYM_PAUSE)
  4126. remote_adv |= LPA_1000XPAUSE;
  4127. if (rxflags & MR_LP_ADV_ASYM_PAUSE)
  4128. remote_adv |= LPA_1000XPAUSE_ASYM;
  4129. tp->link_config.rmt_adv =
  4130. mii_adv_to_ethtool_adv_x(remote_adv);
  4131. tg3_setup_flow_control(tp, local_adv, remote_adv);
  4132. current_link_up = 1;
  4133. }
  4134. for (i = 0; i < 30; i++) {
  4135. udelay(20);
  4136. tw32_f(MAC_STATUS,
  4137. (MAC_STATUS_SYNC_CHANGED |
  4138. MAC_STATUS_CFG_CHANGED));
  4139. udelay(40);
  4140. if ((tr32(MAC_STATUS) &
  4141. (MAC_STATUS_SYNC_CHANGED |
  4142. MAC_STATUS_CFG_CHANGED)) == 0)
  4143. break;
  4144. }
  4145. mac_status = tr32(MAC_STATUS);
  4146. if (current_link_up == 0 &&
  4147. (mac_status & MAC_STATUS_PCS_SYNCED) &&
  4148. !(mac_status & MAC_STATUS_RCVD_CFG))
  4149. current_link_up = 1;
  4150. } else {
  4151. tg3_setup_flow_control(tp, 0, 0);
  4152. /* Forcing 1000FD link up. */
  4153. current_link_up = 1;
  4154. tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
  4155. udelay(40);
  4156. tw32_f(MAC_MODE, tp->mac_mode);
  4157. udelay(40);
  4158. }
  4159. out:
  4160. return current_link_up;
  4161. }
  4162. static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
  4163. {
  4164. u32 orig_pause_cfg;
  4165. u16 orig_active_speed;
  4166. u8 orig_active_duplex;
  4167. u32 mac_status;
  4168. int current_link_up;
  4169. int i;
  4170. orig_pause_cfg = tp->link_config.active_flowctrl;
  4171. orig_active_speed = tp->link_config.active_speed;
  4172. orig_active_duplex = tp->link_config.active_duplex;
  4173. if (!tg3_flag(tp, HW_AUTONEG) &&
  4174. netif_carrier_ok(tp->dev) &&
  4175. tg3_flag(tp, INIT_COMPLETE)) {
  4176. mac_status = tr32(MAC_STATUS);
  4177. mac_status &= (MAC_STATUS_PCS_SYNCED |
  4178. MAC_STATUS_SIGNAL_DET |
  4179. MAC_STATUS_CFG_CHANGED |
  4180. MAC_STATUS_RCVD_CFG);
  4181. if (mac_status == (MAC_STATUS_PCS_SYNCED |
  4182. MAC_STATUS_SIGNAL_DET)) {
  4183. tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
  4184. MAC_STATUS_CFG_CHANGED));
  4185. return 0;
  4186. }
  4187. }
  4188. tw32_f(MAC_TX_AUTO_NEG, 0);
  4189. tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
  4190. tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
  4191. tw32_f(MAC_MODE, tp->mac_mode);
  4192. udelay(40);
  4193. if (tp->phy_id == TG3_PHY_ID_BCM8002)
  4194. tg3_init_bcm8002(tp);
  4195. /* Enable link change event even when serdes polling. */
  4196. tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
  4197. udelay(40);
  4198. current_link_up = 0;
  4199. tp->link_config.rmt_adv = 0;
  4200. mac_status = tr32(MAC_STATUS);
  4201. if (tg3_flag(tp, HW_AUTONEG))
  4202. current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
  4203. else
  4204. current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
  4205. tp->napi[0].hw_status->status =
  4206. (SD_STATUS_UPDATED |
  4207. (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
  4208. for (i = 0; i < 100; i++) {
  4209. tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
  4210. MAC_STATUS_CFG_CHANGED));
  4211. udelay(5);
  4212. if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
  4213. MAC_STATUS_CFG_CHANGED |
  4214. MAC_STATUS_LNKSTATE_CHANGED)) == 0)
  4215. break;
  4216. }
  4217. mac_status = tr32(MAC_STATUS);
  4218. if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
  4219. current_link_up = 0;
  4220. if (tp->link_config.autoneg == AUTONEG_ENABLE &&
  4221. tp->serdes_counter == 0) {
  4222. tw32_f(MAC_MODE, (tp->mac_mode |
  4223. MAC_MODE_SEND_CONFIGS));
  4224. udelay(1);
  4225. tw32_f(MAC_MODE, tp->mac_mode);
  4226. }
  4227. }
  4228. if (current_link_up == 1) {
  4229. tp->link_config.active_speed = SPEED_1000;
  4230. tp->link_config.active_duplex = DUPLEX_FULL;
  4231. tw32(MAC_LED_CTRL, (tp->led_ctrl |
  4232. LED_CTRL_LNKLED_OVERRIDE |
  4233. LED_CTRL_1000MBPS_ON));
  4234. } else {
  4235. tp->link_config.active_speed = SPEED_UNKNOWN;
  4236. tp->link_config.active_duplex = DUPLEX_UNKNOWN;
  4237. tw32(MAC_LED_CTRL, (tp->led_ctrl |
  4238. LED_CTRL_LNKLED_OVERRIDE |
  4239. LED_CTRL_TRAFFIC_OVERRIDE));
  4240. }
  4241. if (current_link_up != netif_carrier_ok(tp->dev)) {
  4242. if (current_link_up)
  4243. netif_carrier_on(tp->dev);
  4244. else
  4245. netif_carrier_off(tp->dev);
  4246. tg3_link_report(tp);
  4247. } else {
  4248. u32 now_pause_cfg = tp->link_config.active_flowctrl;
  4249. if (orig_pause_cfg != now_pause_cfg ||
  4250. orig_active_speed != tp->link_config.active_speed ||
  4251. orig_active_duplex != tp->link_config.active_duplex)
  4252. tg3_link_report(tp);
  4253. }
  4254. return 0;
  4255. }
  4256. static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
  4257. {
  4258. int current_link_up, err = 0;
  4259. u32 bmsr, bmcr;
  4260. u16 current_speed;
  4261. u8 current_duplex;
  4262. u32 local_adv, remote_adv;
  4263. tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
  4264. tw32_f(MAC_MODE, tp->mac_mode);
  4265. udelay(40);
  4266. tw32(MAC_EVENT, 0);
  4267. tw32_f(MAC_STATUS,
  4268. (MAC_STATUS_SYNC_CHANGED |
  4269. MAC_STATUS_CFG_CHANGED |
  4270. MAC_STATUS_MI_COMPLETION |
  4271. MAC_STATUS_LNKSTATE_CHANGED));
  4272. udelay(40);
  4273. if (force_reset)
  4274. tg3_phy_reset(tp);
  4275. current_link_up = 0;
  4276. current_speed = SPEED_UNKNOWN;
  4277. current_duplex = DUPLEX_UNKNOWN;
  4278. tp->link_config.rmt_adv = 0;
  4279. err |= tg3_readphy(tp, MII_BMSR, &bmsr);
  4280. err |= tg3_readphy(tp, MII_BMSR, &bmsr);
  4281. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
  4282. if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
  4283. bmsr |= BMSR_LSTATUS;
  4284. else
  4285. bmsr &= ~BMSR_LSTATUS;
  4286. }
  4287. err |= tg3_readphy(tp, MII_BMCR, &bmcr);
  4288. if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
  4289. (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
  4290. /* do nothing, just check for link up at the end */
  4291. } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
  4292. u32 adv, newadv;
  4293. err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
  4294. newadv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
  4295. ADVERTISE_1000XPAUSE |
  4296. ADVERTISE_1000XPSE_ASYM |
  4297. ADVERTISE_SLCT);
  4298. newadv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
  4299. newadv |= ethtool_adv_to_mii_adv_x(tp->link_config.advertising);
  4300. if ((newadv != adv) || !(bmcr & BMCR_ANENABLE)) {
  4301. tg3_writephy(tp, MII_ADVERTISE, newadv);
  4302. bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
  4303. tg3_writephy(tp, MII_BMCR, bmcr);
  4304. tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
  4305. tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
  4306. tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
  4307. return err;
  4308. }
  4309. } else {
  4310. u32 new_bmcr;
  4311. bmcr &= ~BMCR_SPEED1000;
  4312. new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
  4313. if (tp->link_config.duplex == DUPLEX_FULL)
  4314. new_bmcr |= BMCR_FULLDPLX;
  4315. if (new_bmcr != bmcr) {
  4316. /* BMCR_SPEED1000 is a reserved bit that needs
  4317. * to be set on write.
  4318. */
  4319. new_bmcr |= BMCR_SPEED1000;
  4320. /* Force a linkdown */
  4321. if (netif_carrier_ok(tp->dev)) {
  4322. u32 adv;
  4323. err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
  4324. adv &= ~(ADVERTISE_1000XFULL |
  4325. ADVERTISE_1000XHALF |
  4326. ADVERTISE_SLCT);
  4327. tg3_writephy(tp, MII_ADVERTISE, adv);
  4328. tg3_writephy(tp, MII_BMCR, bmcr |
  4329. BMCR_ANRESTART |
  4330. BMCR_ANENABLE);
  4331. udelay(10);
  4332. netif_carrier_off(tp->dev);
  4333. }
  4334. tg3_writephy(tp, MII_BMCR, new_bmcr);
  4335. bmcr = new_bmcr;
  4336. err |= tg3_readphy(tp, MII_BMSR, &bmsr);
  4337. err |= tg3_readphy(tp, MII_BMSR, &bmsr);
  4338. if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
  4339. ASIC_REV_5714) {
  4340. if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
  4341. bmsr |= BMSR_LSTATUS;
  4342. else
  4343. bmsr &= ~BMSR_LSTATUS;
  4344. }
  4345. tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
  4346. }
  4347. }
  4348. if (bmsr & BMSR_LSTATUS) {
  4349. current_speed = SPEED_1000;
  4350. current_link_up = 1;
  4351. if (bmcr & BMCR_FULLDPLX)
  4352. current_duplex = DUPLEX_FULL;
  4353. else
  4354. current_duplex = DUPLEX_HALF;
  4355. local_adv = 0;
  4356. remote_adv = 0;
  4357. if (bmcr & BMCR_ANENABLE) {
  4358. u32 common;
  4359. err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
  4360. err |= tg3_readphy(tp, MII_LPA, &remote_adv);
  4361. common = local_adv & remote_adv;
  4362. if (common & (ADVERTISE_1000XHALF |
  4363. ADVERTISE_1000XFULL)) {
  4364. if (common & ADVERTISE_1000XFULL)
  4365. current_duplex = DUPLEX_FULL;
  4366. else
  4367. current_duplex = DUPLEX_HALF;
  4368. tp->link_config.rmt_adv =
  4369. mii_adv_to_ethtool_adv_x(remote_adv);
  4370. } else if (!tg3_flag(tp, 5780_CLASS)) {
  4371. /* Link is up via parallel detect */
  4372. } else {
  4373. current_link_up = 0;
  4374. }
  4375. }
  4376. }
  4377. if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
  4378. tg3_setup_flow_control(tp, local_adv, remote_adv);
  4379. tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
  4380. if (tp->link_config.active_duplex == DUPLEX_HALF)
  4381. tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
  4382. tw32_f(MAC_MODE, tp->mac_mode);
  4383. udelay(40);
  4384. tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
  4385. tp->link_config.active_speed = current_speed;
  4386. tp->link_config.active_duplex = current_duplex;
  4387. if (current_link_up != netif_carrier_ok(tp->dev)) {
  4388. if (current_link_up)
  4389. netif_carrier_on(tp->dev);
  4390. else {
  4391. netif_carrier_off(tp->dev);
  4392. tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
  4393. }
  4394. tg3_link_report(tp);
  4395. }
  4396. return err;
  4397. }
  4398. static void tg3_serdes_parallel_detect(struct tg3 *tp)
  4399. {
  4400. if (tp->serdes_counter) {
  4401. /* Give autoneg time to complete. */
  4402. tp->serdes_counter--;
  4403. return;
  4404. }
  4405. if (!netif_carrier_ok(tp->dev) &&
  4406. (tp->link_config.autoneg == AUTONEG_ENABLE)) {
  4407. u32 bmcr;
  4408. tg3_readphy(tp, MII_BMCR, &bmcr);
  4409. if (bmcr & BMCR_ANENABLE) {
  4410. u32 phy1, phy2;
  4411. /* Select shadow register 0x1f */
  4412. tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
  4413. tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
  4414. /* Select expansion interrupt status register */
  4415. tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
  4416. MII_TG3_DSP_EXP1_INT_STAT);
  4417. tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
  4418. tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
  4419. if ((phy1 & 0x10) && !(phy2 & 0x20)) {
  4420. /* We have signal detect and not receiving
  4421. * config code words, link is up by parallel
  4422. * detection.
  4423. */
  4424. bmcr &= ~BMCR_ANENABLE;
  4425. bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
  4426. tg3_writephy(tp, MII_BMCR, bmcr);
  4427. tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
  4428. }
  4429. }
  4430. } else if (netif_carrier_ok(tp->dev) &&
  4431. (tp->link_config.autoneg == AUTONEG_ENABLE) &&
  4432. (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
  4433. u32 phy2;
  4434. /* Select expansion interrupt status register */
  4435. tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
  4436. MII_TG3_DSP_EXP1_INT_STAT);
  4437. tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
  4438. if (phy2 & 0x20) {
  4439. u32 bmcr;
  4440. /* Config code words received, turn on autoneg. */
  4441. tg3_readphy(tp, MII_BMCR, &bmcr);
  4442. tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
  4443. tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
  4444. }
  4445. }
  4446. }
  4447. static int tg3_setup_phy(struct tg3 *tp, int force_reset)
  4448. {
  4449. u32 val;
  4450. int err;
  4451. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
  4452. err = tg3_setup_fiber_phy(tp, force_reset);
  4453. else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
  4454. err = tg3_setup_fiber_mii_phy(tp, force_reset);
  4455. else
  4456. err = tg3_setup_copper_phy(tp, force_reset);
  4457. if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
  4458. u32 scale;
  4459. val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
  4460. if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
  4461. scale = 65;
  4462. else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
  4463. scale = 6;
  4464. else
  4465. scale = 12;
  4466. val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
  4467. val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
  4468. tw32(GRC_MISC_CFG, val);
  4469. }
  4470. val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
  4471. (6 << TX_LENGTHS_IPG_SHIFT);
  4472. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
  4473. val |= tr32(MAC_TX_LENGTHS) &
  4474. (TX_LENGTHS_JMB_FRM_LEN_MSK |
  4475. TX_LENGTHS_CNT_DWN_VAL_MSK);
  4476. if (tp->link_config.active_speed == SPEED_1000 &&
  4477. tp->link_config.active_duplex == DUPLEX_HALF)
  4478. tw32(MAC_TX_LENGTHS, val |
  4479. (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
  4480. else
  4481. tw32(MAC_TX_LENGTHS, val |
  4482. (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
  4483. if (!tg3_flag(tp, 5705_PLUS)) {
  4484. if (netif_carrier_ok(tp->dev)) {
  4485. tw32(HOSTCC_STAT_COAL_TICKS,
  4486. tp->coal.stats_block_coalesce_usecs);
  4487. } else {
  4488. tw32(HOSTCC_STAT_COAL_TICKS, 0);
  4489. }
  4490. }
  4491. if (tg3_flag(tp, ASPM_WORKAROUND)) {
  4492. val = tr32(PCIE_PWR_MGMT_THRESH);
  4493. if (!netif_carrier_ok(tp->dev))
  4494. val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
  4495. tp->pwrmgmt_thresh;
  4496. else
  4497. val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
  4498. tw32(PCIE_PWR_MGMT_THRESH, val);
  4499. }
  4500. return err;
  4501. }
  4502. static inline int tg3_irq_sync(struct tg3 *tp)
  4503. {
  4504. return tp->irq_sync;
  4505. }
  4506. static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
  4507. {
  4508. int i;
  4509. dst = (u32 *)((u8 *)dst + off);
  4510. for (i = 0; i < len; i += sizeof(u32))
  4511. *dst++ = tr32(off + i);
  4512. }
  4513. static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
  4514. {
  4515. tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
  4516. tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
  4517. tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
  4518. tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
  4519. tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
  4520. tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
  4521. tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
  4522. tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
  4523. tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
  4524. tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
  4525. tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
  4526. tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
  4527. tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
  4528. tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
  4529. tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
  4530. tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
  4531. tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
  4532. tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
  4533. tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
  4534. if (tg3_flag(tp, SUPPORT_MSIX))
  4535. tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
  4536. tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
  4537. tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
  4538. tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
  4539. tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
  4540. tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
  4541. tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
  4542. tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
  4543. tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
  4544. if (!tg3_flag(tp, 5705_PLUS)) {
  4545. tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
  4546. tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
  4547. tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
  4548. }
  4549. tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
  4550. tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
  4551. tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
  4552. tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
  4553. tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
  4554. if (tg3_flag(tp, NVRAM))
  4555. tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
  4556. }
  4557. static void tg3_dump_state(struct tg3 *tp)
  4558. {
  4559. int i;
  4560. u32 *regs;
  4561. regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
  4562. if (!regs) {
  4563. netdev_err(tp->dev, "Failed allocating register dump buffer\n");
  4564. return;
  4565. }
  4566. if (tg3_flag(tp, PCI_EXPRESS)) {
  4567. /* Read up to but not including private PCI registers */
  4568. for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
  4569. regs[i / sizeof(u32)] = tr32(i);
  4570. } else
  4571. tg3_dump_legacy_regs(tp, regs);
  4572. for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
  4573. if (!regs[i + 0] && !regs[i + 1] &&
  4574. !regs[i + 2] && !regs[i + 3])
  4575. continue;
  4576. netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
  4577. i * 4,
  4578. regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
  4579. }
  4580. kfree(regs);
  4581. for (i = 0; i < tp->irq_cnt; i++) {
  4582. struct tg3_napi *tnapi = &tp->napi[i];
  4583. /* SW status block */
  4584. netdev_err(tp->dev,
  4585. "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
  4586. i,
  4587. tnapi->hw_status->status,
  4588. tnapi->hw_status->status_tag,
  4589. tnapi->hw_status->rx_jumbo_consumer,
  4590. tnapi->hw_status->rx_consumer,
  4591. tnapi->hw_status->rx_mini_consumer,
  4592. tnapi->hw_status->idx[0].rx_producer,
  4593. tnapi->hw_status->idx[0].tx_consumer);
  4594. netdev_err(tp->dev,
  4595. "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
  4596. i,
  4597. tnapi->last_tag, tnapi->last_irq_tag,
  4598. tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
  4599. tnapi->rx_rcb_ptr,
  4600. tnapi->prodring.rx_std_prod_idx,
  4601. tnapi->prodring.rx_std_cons_idx,
  4602. tnapi->prodring.rx_jmb_prod_idx,
  4603. tnapi->prodring.rx_jmb_cons_idx);
  4604. }
  4605. }
  4606. /* This is called whenever we suspect that the system chipset is re-
  4607. * ordering the sequence of MMIO to the tx send mailbox. The symptom
  4608. * is bogus tx completions. We try to recover by setting the
  4609. * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
  4610. * in the workqueue.
  4611. */
  4612. static void tg3_tx_recover(struct tg3 *tp)
  4613. {
  4614. BUG_ON(tg3_flag(tp, MBOX_WRITE_REORDER) ||
  4615. tp->write32_tx_mbox == tg3_write_indirect_mbox);
  4616. netdev_warn(tp->dev,
  4617. "The system may be re-ordering memory-mapped I/O "
  4618. "cycles to the network device, attempting to recover. "
  4619. "Please report the problem to the driver maintainer "
  4620. "and include system chipset information.\n");
  4621. spin_lock(&tp->lock);
  4622. tg3_flag_set(tp, TX_RECOVERY_PENDING);
  4623. spin_unlock(&tp->lock);
  4624. }
  4625. static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
  4626. {
  4627. /* Tell compiler to fetch tx indices from memory. */
  4628. barrier();
  4629. return tnapi->tx_pending -
  4630. ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
  4631. }
  4632. /* Tigon3 never reports partial packet sends. So we do not
  4633. * need special logic to handle SKBs that have not had all
  4634. * of their frags sent yet, like SunGEM does.
  4635. */
  4636. static void tg3_tx(struct tg3_napi *tnapi)
  4637. {
  4638. struct tg3 *tp = tnapi->tp;
  4639. u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
  4640. u32 sw_idx = tnapi->tx_cons;
  4641. struct netdev_queue *txq;
  4642. int index = tnapi - tp->napi;
  4643. unsigned int pkts_compl = 0, bytes_compl = 0;
  4644. if (tg3_flag(tp, ENABLE_TSS))
  4645. index--;
  4646. txq = netdev_get_tx_queue(tp->dev, index);
  4647. while (sw_idx != hw_idx) {
  4648. struct tg3_tx_ring_info *ri = &tnapi->tx_buffers[sw_idx];
  4649. struct sk_buff *skb = ri->skb;
  4650. int i, tx_bug = 0;
  4651. if (unlikely(skb == NULL)) {
  4652. tg3_tx_recover(tp);
  4653. return;
  4654. }
  4655. pci_unmap_single(tp->pdev,
  4656. dma_unmap_addr(ri, mapping),
  4657. skb_headlen(skb),
  4658. PCI_DMA_TODEVICE);
  4659. ri->skb = NULL;
  4660. while (ri->fragmented) {
  4661. ri->fragmented = false;
  4662. sw_idx = NEXT_TX(sw_idx);
  4663. ri = &tnapi->tx_buffers[sw_idx];
  4664. }
  4665. sw_idx = NEXT_TX(sw_idx);
  4666. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  4667. ri = &tnapi->tx_buffers[sw_idx];
  4668. if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
  4669. tx_bug = 1;
  4670. pci_unmap_page(tp->pdev,
  4671. dma_unmap_addr(ri, mapping),
  4672. skb_frag_size(&skb_shinfo(skb)->frags[i]),
  4673. PCI_DMA_TODEVICE);
  4674. while (ri->fragmented) {
  4675. ri->fragmented = false;
  4676. sw_idx = NEXT_TX(sw_idx);
  4677. ri = &tnapi->tx_buffers[sw_idx];
  4678. }
  4679. sw_idx = NEXT_TX(sw_idx);
  4680. }
  4681. pkts_compl++;
  4682. bytes_compl += skb->len;
  4683. dev_kfree_skb(skb);
  4684. if (unlikely(tx_bug)) {
  4685. tg3_tx_recover(tp);
  4686. return;
  4687. }
  4688. }
  4689. netdev_tx_completed_queue(txq, pkts_compl, bytes_compl);
  4690. tnapi->tx_cons = sw_idx;
  4691. /* Need to make the tx_cons update visible to tg3_start_xmit()
  4692. * before checking for netif_queue_stopped(). Without the
  4693. * memory barrier, there is a small possibility that tg3_start_xmit()
  4694. * will miss it and cause the queue to be stopped forever.
  4695. */
  4696. smp_mb();
  4697. if (unlikely(netif_tx_queue_stopped(txq) &&
  4698. (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
  4699. __netif_tx_lock(txq, smp_processor_id());
  4700. if (netif_tx_queue_stopped(txq) &&
  4701. (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
  4702. netif_tx_wake_queue(txq);
  4703. __netif_tx_unlock(txq);
  4704. }
  4705. }
  4706. static void tg3_frag_free(bool is_frag, void *data)
  4707. {
  4708. if (is_frag)
  4709. put_page(virt_to_head_page(data));
  4710. else
  4711. kfree(data);
  4712. }
  4713. static void tg3_rx_data_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
  4714. {
  4715. unsigned int skb_size = SKB_DATA_ALIGN(map_sz + TG3_RX_OFFSET(tp)) +
  4716. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  4717. if (!ri->data)
  4718. return;
  4719. pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
  4720. map_sz, PCI_DMA_FROMDEVICE);
  4721. tg3_frag_free(skb_size <= PAGE_SIZE, ri->data);
  4722. ri->data = NULL;
  4723. }
  4724. /* Returns size of skb allocated or < 0 on error.
  4725. *
  4726. * We only need to fill in the address because the other members
  4727. * of the RX descriptor are invariant, see tg3_init_rings.
  4728. *
  4729. * Note the purposeful assymetry of cpu vs. chip accesses. For
  4730. * posting buffers we only dirty the first cache line of the RX
  4731. * descriptor (containing the address). Whereas for the RX status
  4732. * buffers the cpu only reads the last cacheline of the RX descriptor
  4733. * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
  4734. */
  4735. static int tg3_alloc_rx_data(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
  4736. u32 opaque_key, u32 dest_idx_unmasked,
  4737. unsigned int *frag_size)
  4738. {
  4739. struct tg3_rx_buffer_desc *desc;
  4740. struct ring_info *map;
  4741. u8 *data;
  4742. dma_addr_t mapping;
  4743. int skb_size, data_size, dest_idx;
  4744. switch (opaque_key) {
  4745. case RXD_OPAQUE_RING_STD:
  4746. dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
  4747. desc = &tpr->rx_std[dest_idx];
  4748. map = &tpr->rx_std_buffers[dest_idx];
  4749. data_size = tp->rx_pkt_map_sz;
  4750. break;
  4751. case RXD_OPAQUE_RING_JUMBO:
  4752. dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
  4753. desc = &tpr->rx_jmb[dest_idx].std;
  4754. map = &tpr->rx_jmb_buffers[dest_idx];
  4755. data_size = TG3_RX_JMB_MAP_SZ;
  4756. break;
  4757. default:
  4758. return -EINVAL;
  4759. }
  4760. /* Do not overwrite any of the map or rp information
  4761. * until we are sure we can commit to a new buffer.
  4762. *
  4763. * Callers depend upon this behavior and assume that
  4764. * we leave everything unchanged if we fail.
  4765. */
  4766. skb_size = SKB_DATA_ALIGN(data_size + TG3_RX_OFFSET(tp)) +
  4767. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  4768. if (skb_size <= PAGE_SIZE) {
  4769. data = netdev_alloc_frag(skb_size);
  4770. *frag_size = skb_size;
  4771. } else {
  4772. data = kmalloc(skb_size, GFP_ATOMIC);
  4773. *frag_size = 0;
  4774. }
  4775. if (!data)
  4776. return -ENOMEM;
  4777. mapping = pci_map_single(tp->pdev,
  4778. data + TG3_RX_OFFSET(tp),
  4779. data_size,
  4780. PCI_DMA_FROMDEVICE);
  4781. if (unlikely(pci_dma_mapping_error(tp->pdev, mapping))) {
  4782. tg3_frag_free(skb_size <= PAGE_SIZE, data);
  4783. return -EIO;
  4784. }
  4785. map->data = data;
  4786. dma_unmap_addr_set(map, mapping, mapping);
  4787. desc->addr_hi = ((u64)mapping >> 32);
  4788. desc->addr_lo = ((u64)mapping & 0xffffffff);
  4789. return data_size;
  4790. }
  4791. /* We only need to move over in the address because the other
  4792. * members of the RX descriptor are invariant. See notes above
  4793. * tg3_alloc_rx_data for full details.
  4794. */
  4795. static void tg3_recycle_rx(struct tg3_napi *tnapi,
  4796. struct tg3_rx_prodring_set *dpr,
  4797. u32 opaque_key, int src_idx,
  4798. u32 dest_idx_unmasked)
  4799. {
  4800. struct tg3 *tp = tnapi->tp;
  4801. struct tg3_rx_buffer_desc *src_desc, *dest_desc;
  4802. struct ring_info *src_map, *dest_map;
  4803. struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
  4804. int dest_idx;
  4805. switch (opaque_key) {
  4806. case RXD_OPAQUE_RING_STD:
  4807. dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
  4808. dest_desc = &dpr->rx_std[dest_idx];
  4809. dest_map = &dpr->rx_std_buffers[dest_idx];
  4810. src_desc = &spr->rx_std[src_idx];
  4811. src_map = &spr->rx_std_buffers[src_idx];
  4812. break;
  4813. case RXD_OPAQUE_RING_JUMBO:
  4814. dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
  4815. dest_desc = &dpr->rx_jmb[dest_idx].std;
  4816. dest_map = &dpr->rx_jmb_buffers[dest_idx];
  4817. src_desc = &spr->rx_jmb[src_idx].std;
  4818. src_map = &spr->rx_jmb_buffers[src_idx];
  4819. break;
  4820. default:
  4821. return;
  4822. }
  4823. dest_map->data = src_map->data;
  4824. dma_unmap_addr_set(dest_map, mapping,
  4825. dma_unmap_addr(src_map, mapping));
  4826. dest_desc->addr_hi = src_desc->addr_hi;
  4827. dest_desc->addr_lo = src_desc->addr_lo;
  4828. /* Ensure that the update to the skb happens after the physical
  4829. * addresses have been transferred to the new BD location.
  4830. */
  4831. smp_wmb();
  4832. src_map->data = NULL;
  4833. }
  4834. /* The RX ring scheme is composed of multiple rings which post fresh
  4835. * buffers to the chip, and one special ring the chip uses to report
  4836. * status back to the host.
  4837. *
  4838. * The special ring reports the status of received packets to the
  4839. * host. The chip does not write into the original descriptor the
  4840. * RX buffer was obtained from. The chip simply takes the original
  4841. * descriptor as provided by the host, updates the status and length
  4842. * field, then writes this into the next status ring entry.
  4843. *
  4844. * Each ring the host uses to post buffers to the chip is described
  4845. * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
  4846. * it is first placed into the on-chip ram. When the packet's length
  4847. * is known, it walks down the TG3_BDINFO entries to select the ring.
  4848. * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
  4849. * which is within the range of the new packet's length is chosen.
  4850. *
  4851. * The "separate ring for rx status" scheme may sound queer, but it makes
  4852. * sense from a cache coherency perspective. If only the host writes
  4853. * to the buffer post rings, and only the chip writes to the rx status
  4854. * rings, then cache lines never move beyond shared-modified state.
  4855. * If both the host and chip were to write into the same ring, cache line
  4856. * eviction could occur since both entities want it in an exclusive state.
  4857. */
  4858. static int tg3_rx(struct tg3_napi *tnapi, int budget)
  4859. {
  4860. struct tg3 *tp = tnapi->tp;
  4861. u32 work_mask, rx_std_posted = 0;
  4862. u32 std_prod_idx, jmb_prod_idx;
  4863. u32 sw_idx = tnapi->rx_rcb_ptr;
  4864. u16 hw_idx;
  4865. int received;
  4866. struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
  4867. hw_idx = *(tnapi->rx_rcb_prod_idx);
  4868. /*
  4869. * We need to order the read of hw_idx and the read of
  4870. * the opaque cookie.
  4871. */
  4872. rmb();
  4873. work_mask = 0;
  4874. received = 0;
  4875. std_prod_idx = tpr->rx_std_prod_idx;
  4876. jmb_prod_idx = tpr->rx_jmb_prod_idx;
  4877. while (sw_idx != hw_idx && budget > 0) {
  4878. struct ring_info *ri;
  4879. struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
  4880. unsigned int len;
  4881. struct sk_buff *skb;
  4882. dma_addr_t dma_addr;
  4883. u32 opaque_key, desc_idx, *post_ptr;
  4884. u8 *data;
  4885. desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
  4886. opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
  4887. if (opaque_key == RXD_OPAQUE_RING_STD) {
  4888. ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
  4889. dma_addr = dma_unmap_addr(ri, mapping);
  4890. data = ri->data;
  4891. post_ptr = &std_prod_idx;
  4892. rx_std_posted++;
  4893. } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
  4894. ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
  4895. dma_addr = dma_unmap_addr(ri, mapping);
  4896. data = ri->data;
  4897. post_ptr = &jmb_prod_idx;
  4898. } else
  4899. goto next_pkt_nopost;
  4900. work_mask |= opaque_key;
  4901. if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
  4902. (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
  4903. drop_it:
  4904. tg3_recycle_rx(tnapi, tpr, opaque_key,
  4905. desc_idx, *post_ptr);
  4906. drop_it_no_recycle:
  4907. /* Other statistics kept track of by card. */
  4908. tp->rx_dropped++;
  4909. goto next_pkt;
  4910. }
  4911. prefetch(data + TG3_RX_OFFSET(tp));
  4912. len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
  4913. ETH_FCS_LEN;
  4914. if (len > TG3_RX_COPY_THRESH(tp)) {
  4915. int skb_size;
  4916. unsigned int frag_size;
  4917. skb_size = tg3_alloc_rx_data(tp, tpr, opaque_key,
  4918. *post_ptr, &frag_size);
  4919. if (skb_size < 0)
  4920. goto drop_it;
  4921. pci_unmap_single(tp->pdev, dma_addr, skb_size,
  4922. PCI_DMA_FROMDEVICE);
  4923. skb = build_skb(data, frag_size);
  4924. if (!skb) {
  4925. tg3_frag_free(frag_size != 0, data);
  4926. goto drop_it_no_recycle;
  4927. }
  4928. skb_reserve(skb, TG3_RX_OFFSET(tp));
  4929. /* Ensure that the update to the data happens
  4930. * after the usage of the old DMA mapping.
  4931. */
  4932. smp_wmb();
  4933. ri->data = NULL;
  4934. } else {
  4935. tg3_recycle_rx(tnapi, tpr, opaque_key,
  4936. desc_idx, *post_ptr);
  4937. skb = netdev_alloc_skb(tp->dev,
  4938. len + TG3_RAW_IP_ALIGN);
  4939. if (skb == NULL)
  4940. goto drop_it_no_recycle;
  4941. skb_reserve(skb, TG3_RAW_IP_ALIGN);
  4942. pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
  4943. memcpy(skb->data,
  4944. data + TG3_RX_OFFSET(tp),
  4945. len);
  4946. pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
  4947. }
  4948. skb_put(skb, len);
  4949. if ((tp->dev->features & NETIF_F_RXCSUM) &&
  4950. (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
  4951. (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
  4952. >> RXD_TCPCSUM_SHIFT) == 0xffff))
  4953. skb->ip_summed = CHECKSUM_UNNECESSARY;
  4954. else
  4955. skb_checksum_none_assert(skb);
  4956. skb->protocol = eth_type_trans(skb, tp->dev);
  4957. if (len > (tp->dev->mtu + ETH_HLEN) &&
  4958. skb->protocol != htons(ETH_P_8021Q)) {
  4959. dev_kfree_skb(skb);
  4960. goto drop_it_no_recycle;
  4961. }
  4962. if (desc->type_flags & RXD_FLAG_VLAN &&
  4963. !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
  4964. __vlan_hwaccel_put_tag(skb,
  4965. desc->err_vlan & RXD_VLAN_MASK);
  4966. napi_gro_receive(&tnapi->napi, skb);
  4967. received++;
  4968. budget--;
  4969. next_pkt:
  4970. (*post_ptr)++;
  4971. if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
  4972. tpr->rx_std_prod_idx = std_prod_idx &
  4973. tp->rx_std_ring_mask;
  4974. tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
  4975. tpr->rx_std_prod_idx);
  4976. work_mask &= ~RXD_OPAQUE_RING_STD;
  4977. rx_std_posted = 0;
  4978. }
  4979. next_pkt_nopost:
  4980. sw_idx++;
  4981. sw_idx &= tp->rx_ret_ring_mask;
  4982. /* Refresh hw_idx to see if there is new work */
  4983. if (sw_idx == hw_idx) {
  4984. hw_idx = *(tnapi->rx_rcb_prod_idx);
  4985. rmb();
  4986. }
  4987. }
  4988. /* ACK the status ring. */
  4989. tnapi->rx_rcb_ptr = sw_idx;
  4990. tw32_rx_mbox(tnapi->consmbox, sw_idx);
  4991. /* Refill RX ring(s). */
  4992. if (!tg3_flag(tp, ENABLE_RSS)) {
  4993. /* Sync BD data before updating mailbox */
  4994. wmb();
  4995. if (work_mask & RXD_OPAQUE_RING_STD) {
  4996. tpr->rx_std_prod_idx = std_prod_idx &
  4997. tp->rx_std_ring_mask;
  4998. tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
  4999. tpr->rx_std_prod_idx);
  5000. }
  5001. if (work_mask & RXD_OPAQUE_RING_JUMBO) {
  5002. tpr->rx_jmb_prod_idx = jmb_prod_idx &
  5003. tp->rx_jmb_ring_mask;
  5004. tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
  5005. tpr->rx_jmb_prod_idx);
  5006. }
  5007. mmiowb();
  5008. } else if (work_mask) {
  5009. /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
  5010. * updated before the producer indices can be updated.
  5011. */
  5012. smp_wmb();
  5013. tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
  5014. tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
  5015. if (tnapi != &tp->napi[1]) {
  5016. tp->rx_refill = true;
  5017. napi_schedule(&tp->napi[1].napi);
  5018. }
  5019. }
  5020. return received;
  5021. }
  5022. static void tg3_poll_link(struct tg3 *tp)
  5023. {
  5024. /* handle link change and other phy events */
  5025. if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
  5026. struct tg3_hw_status *sblk = tp->napi[0].hw_status;
  5027. if (sblk->status & SD_STATUS_LINK_CHG) {
  5028. sblk->status = SD_STATUS_UPDATED |
  5029. (sblk->status & ~SD_STATUS_LINK_CHG);
  5030. spin_lock(&tp->lock);
  5031. if (tg3_flag(tp, USE_PHYLIB)) {
  5032. tw32_f(MAC_STATUS,
  5033. (MAC_STATUS_SYNC_CHANGED |
  5034. MAC_STATUS_CFG_CHANGED |
  5035. MAC_STATUS_MI_COMPLETION |
  5036. MAC_STATUS_LNKSTATE_CHANGED));
  5037. udelay(40);
  5038. } else
  5039. tg3_setup_phy(tp, 0);
  5040. spin_unlock(&tp->lock);
  5041. }
  5042. }
  5043. }
  5044. static int tg3_rx_prodring_xfer(struct tg3 *tp,
  5045. struct tg3_rx_prodring_set *dpr,
  5046. struct tg3_rx_prodring_set *spr)
  5047. {
  5048. u32 si, di, cpycnt, src_prod_idx;
  5049. int i, err = 0;
  5050. while (1) {
  5051. src_prod_idx = spr->rx_std_prod_idx;
  5052. /* Make sure updates to the rx_std_buffers[] entries and the
  5053. * standard producer index are seen in the correct order.
  5054. */
  5055. smp_rmb();
  5056. if (spr->rx_std_cons_idx == src_prod_idx)
  5057. break;
  5058. if (spr->rx_std_cons_idx < src_prod_idx)
  5059. cpycnt = src_prod_idx - spr->rx_std_cons_idx;
  5060. else
  5061. cpycnt = tp->rx_std_ring_mask + 1 -
  5062. spr->rx_std_cons_idx;
  5063. cpycnt = min(cpycnt,
  5064. tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
  5065. si = spr->rx_std_cons_idx;
  5066. di = dpr->rx_std_prod_idx;
  5067. for (i = di; i < di + cpycnt; i++) {
  5068. if (dpr->rx_std_buffers[i].data) {
  5069. cpycnt = i - di;
  5070. err = -ENOSPC;
  5071. break;
  5072. }
  5073. }
  5074. if (!cpycnt)
  5075. break;
  5076. /* Ensure that updates to the rx_std_buffers ring and the
  5077. * shadowed hardware producer ring from tg3_recycle_skb() are
  5078. * ordered correctly WRT the skb check above.
  5079. */
  5080. smp_rmb();
  5081. memcpy(&dpr->rx_std_buffers[di],
  5082. &spr->rx_std_buffers[si],
  5083. cpycnt * sizeof(struct ring_info));
  5084. for (i = 0; i < cpycnt; i++, di++, si++) {
  5085. struct tg3_rx_buffer_desc *sbd, *dbd;
  5086. sbd = &spr->rx_std[si];
  5087. dbd = &dpr->rx_std[di];
  5088. dbd->addr_hi = sbd->addr_hi;
  5089. dbd->addr_lo = sbd->addr_lo;
  5090. }
  5091. spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
  5092. tp->rx_std_ring_mask;
  5093. dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
  5094. tp->rx_std_ring_mask;
  5095. }
  5096. while (1) {
  5097. src_prod_idx = spr->rx_jmb_prod_idx;
  5098. /* Make sure updates to the rx_jmb_buffers[] entries and
  5099. * the jumbo producer index are seen in the correct order.
  5100. */
  5101. smp_rmb();
  5102. if (spr->rx_jmb_cons_idx == src_prod_idx)
  5103. break;
  5104. if (spr->rx_jmb_cons_idx < src_prod_idx)
  5105. cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
  5106. else
  5107. cpycnt = tp->rx_jmb_ring_mask + 1 -
  5108. spr->rx_jmb_cons_idx;
  5109. cpycnt = min(cpycnt,
  5110. tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
  5111. si = spr->rx_jmb_cons_idx;
  5112. di = dpr->rx_jmb_prod_idx;
  5113. for (i = di; i < di + cpycnt; i++) {
  5114. if (dpr->rx_jmb_buffers[i].data) {
  5115. cpycnt = i - di;
  5116. err = -ENOSPC;
  5117. break;
  5118. }
  5119. }
  5120. if (!cpycnt)
  5121. break;
  5122. /* Ensure that updates to the rx_jmb_buffers ring and the
  5123. * shadowed hardware producer ring from tg3_recycle_skb() are
  5124. * ordered correctly WRT the skb check above.
  5125. */
  5126. smp_rmb();
  5127. memcpy(&dpr->rx_jmb_buffers[di],
  5128. &spr->rx_jmb_buffers[si],
  5129. cpycnt * sizeof(struct ring_info));
  5130. for (i = 0; i < cpycnt; i++, di++, si++) {
  5131. struct tg3_rx_buffer_desc *sbd, *dbd;
  5132. sbd = &spr->rx_jmb[si].std;
  5133. dbd = &dpr->rx_jmb[di].std;
  5134. dbd->addr_hi = sbd->addr_hi;
  5135. dbd->addr_lo = sbd->addr_lo;
  5136. }
  5137. spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
  5138. tp->rx_jmb_ring_mask;
  5139. dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
  5140. tp->rx_jmb_ring_mask;
  5141. }
  5142. return err;
  5143. }
  5144. static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
  5145. {
  5146. struct tg3 *tp = tnapi->tp;
  5147. /* run TX completion thread */
  5148. if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
  5149. tg3_tx(tnapi);
  5150. if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
  5151. return work_done;
  5152. }
  5153. if (!tnapi->rx_rcb_prod_idx)
  5154. return work_done;
  5155. /* run RX thread, within the bounds set by NAPI.
  5156. * All RX "locking" is done by ensuring outside
  5157. * code synchronizes with tg3->napi.poll()
  5158. */
  5159. if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
  5160. work_done += tg3_rx(tnapi, budget - work_done);
  5161. if (tg3_flag(tp, ENABLE_RSS) && tnapi == &tp->napi[1]) {
  5162. struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
  5163. int i, err = 0;
  5164. u32 std_prod_idx = dpr->rx_std_prod_idx;
  5165. u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
  5166. tp->rx_refill = false;
  5167. for (i = 1; i <= tp->rxq_cnt; i++)
  5168. err |= tg3_rx_prodring_xfer(tp, dpr,
  5169. &tp->napi[i].prodring);
  5170. wmb();
  5171. if (std_prod_idx != dpr->rx_std_prod_idx)
  5172. tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
  5173. dpr->rx_std_prod_idx);
  5174. if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
  5175. tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
  5176. dpr->rx_jmb_prod_idx);
  5177. mmiowb();
  5178. if (err)
  5179. tw32_f(HOSTCC_MODE, tp->coal_now);
  5180. }
  5181. return work_done;
  5182. }
  5183. static inline void tg3_reset_task_schedule(struct tg3 *tp)
  5184. {
  5185. if (!test_and_set_bit(TG3_FLAG_RESET_TASK_PENDING, tp->tg3_flags))
  5186. schedule_work(&tp->reset_task);
  5187. }
  5188. static inline void tg3_reset_task_cancel(struct tg3 *tp)
  5189. {
  5190. cancel_work_sync(&tp->reset_task);
  5191. tg3_flag_clear(tp, RESET_TASK_PENDING);
  5192. tg3_flag_clear(tp, TX_RECOVERY_PENDING);
  5193. }
  5194. static int tg3_poll_msix(struct napi_struct *napi, int budget)
  5195. {
  5196. struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
  5197. struct tg3 *tp = tnapi->tp;
  5198. int work_done = 0;
  5199. struct tg3_hw_status *sblk = tnapi->hw_status;
  5200. while (1) {
  5201. work_done = tg3_poll_work(tnapi, work_done, budget);
  5202. if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
  5203. goto tx_recovery;
  5204. if (unlikely(work_done >= budget))
  5205. break;
  5206. /* tp->last_tag is used in tg3_int_reenable() below
  5207. * to tell the hw how much work has been processed,
  5208. * so we must read it before checking for more work.
  5209. */
  5210. tnapi->last_tag = sblk->status_tag;
  5211. tnapi->last_irq_tag = tnapi->last_tag;
  5212. rmb();
  5213. /* check for RX/TX work to do */
  5214. if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
  5215. *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
  5216. /* This test here is not race free, but will reduce
  5217. * the number of interrupts by looping again.
  5218. */
  5219. if (tnapi == &tp->napi[1] && tp->rx_refill)
  5220. continue;
  5221. napi_complete(napi);
  5222. /* Reenable interrupts. */
  5223. tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
  5224. /* This test here is synchronized by napi_schedule()
  5225. * and napi_complete() to close the race condition.
  5226. */
  5227. if (unlikely(tnapi == &tp->napi[1] && tp->rx_refill)) {
  5228. tw32(HOSTCC_MODE, tp->coalesce_mode |
  5229. HOSTCC_MODE_ENABLE |
  5230. tnapi->coal_now);
  5231. }
  5232. mmiowb();
  5233. break;
  5234. }
  5235. }
  5236. return work_done;
  5237. tx_recovery:
  5238. /* work_done is guaranteed to be less than budget. */
  5239. napi_complete(napi);
  5240. tg3_reset_task_schedule(tp);
  5241. return work_done;
  5242. }
  5243. static void tg3_process_error(struct tg3 *tp)
  5244. {
  5245. u32 val;
  5246. bool real_error = false;
  5247. if (tg3_flag(tp, ERROR_PROCESSED))
  5248. return;
  5249. /* Check Flow Attention register */
  5250. val = tr32(HOSTCC_FLOW_ATTN);
  5251. if (val & ~HOSTCC_FLOW_ATTN_MBUF_LWM) {
  5252. netdev_err(tp->dev, "FLOW Attention error. Resetting chip.\n");
  5253. real_error = true;
  5254. }
  5255. if (tr32(MSGINT_STATUS) & ~MSGINT_STATUS_MSI_REQ) {
  5256. netdev_err(tp->dev, "MSI Status error. Resetting chip.\n");
  5257. real_error = true;
  5258. }
  5259. if (tr32(RDMAC_STATUS) || tr32(WDMAC_STATUS)) {
  5260. netdev_err(tp->dev, "DMA Status error. Resetting chip.\n");
  5261. real_error = true;
  5262. }
  5263. if (!real_error)
  5264. return;
  5265. tg3_dump_state(tp);
  5266. tg3_flag_set(tp, ERROR_PROCESSED);
  5267. tg3_reset_task_schedule(tp);
  5268. }
  5269. static int tg3_poll(struct napi_struct *napi, int budget)
  5270. {
  5271. struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
  5272. struct tg3 *tp = tnapi->tp;
  5273. int work_done = 0;
  5274. struct tg3_hw_status *sblk = tnapi->hw_status;
  5275. while (1) {
  5276. if (sblk->status & SD_STATUS_ERROR)
  5277. tg3_process_error(tp);
  5278. tg3_poll_link(tp);
  5279. work_done = tg3_poll_work(tnapi, work_done, budget);
  5280. if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
  5281. goto tx_recovery;
  5282. if (unlikely(work_done >= budget))
  5283. break;
  5284. if (tg3_flag(tp, TAGGED_STATUS)) {
  5285. /* tp->last_tag is used in tg3_int_reenable() below
  5286. * to tell the hw how much work has been processed,
  5287. * so we must read it before checking for more work.
  5288. */
  5289. tnapi->last_tag = sblk->status_tag;
  5290. tnapi->last_irq_tag = tnapi->last_tag;
  5291. rmb();
  5292. } else
  5293. sblk->status &= ~SD_STATUS_UPDATED;
  5294. if (likely(!tg3_has_work(tnapi))) {
  5295. napi_complete(napi);
  5296. tg3_int_reenable(tnapi);
  5297. break;
  5298. }
  5299. }
  5300. return work_done;
  5301. tx_recovery:
  5302. /* work_done is guaranteed to be less than budget. */
  5303. napi_complete(napi);
  5304. tg3_reset_task_schedule(tp);
  5305. return work_done;
  5306. }
  5307. static void tg3_napi_disable(struct tg3 *tp)
  5308. {
  5309. int i;
  5310. for (i = tp->irq_cnt - 1; i >= 0; i--)
  5311. napi_disable(&tp->napi[i].napi);
  5312. }
  5313. static void tg3_napi_enable(struct tg3 *tp)
  5314. {
  5315. int i;
  5316. for (i = 0; i < tp->irq_cnt; i++)
  5317. napi_enable(&tp->napi[i].napi);
  5318. }
  5319. static void tg3_napi_init(struct tg3 *tp)
  5320. {
  5321. int i;
  5322. netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
  5323. for (i = 1; i < tp->irq_cnt; i++)
  5324. netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
  5325. }
  5326. static void tg3_napi_fini(struct tg3 *tp)
  5327. {
  5328. int i;
  5329. for (i = 0; i < tp->irq_cnt; i++)
  5330. netif_napi_del(&tp->napi[i].napi);
  5331. }
  5332. static inline void tg3_netif_stop(struct tg3 *tp)
  5333. {
  5334. tp->dev->trans_start = jiffies; /* prevent tx timeout */
  5335. tg3_napi_disable(tp);
  5336. netif_tx_disable(tp->dev);
  5337. }
  5338. static inline void tg3_netif_start(struct tg3 *tp)
  5339. {
  5340. /* NOTE: unconditional netif_tx_wake_all_queues is only
  5341. * appropriate so long as all callers are assured to
  5342. * have free tx slots (such as after tg3_init_hw)
  5343. */
  5344. netif_tx_wake_all_queues(tp->dev);
  5345. tg3_napi_enable(tp);
  5346. tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
  5347. tg3_enable_ints(tp);
  5348. }
  5349. static void tg3_irq_quiesce(struct tg3 *tp)
  5350. {
  5351. int i;
  5352. BUG_ON(tp->irq_sync);
  5353. tp->irq_sync = 1;
  5354. smp_mb();
  5355. for (i = 0; i < tp->irq_cnt; i++)
  5356. synchronize_irq(tp->napi[i].irq_vec);
  5357. }
  5358. /* Fully shutdown all tg3 driver activity elsewhere in the system.
  5359. * If irq_sync is non-zero, then the IRQ handler must be synchronized
  5360. * with as well. Most of the time, this is not necessary except when
  5361. * shutting down the device.
  5362. */
  5363. static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
  5364. {
  5365. spin_lock_bh(&tp->lock);
  5366. if (irq_sync)
  5367. tg3_irq_quiesce(tp);
  5368. }
  5369. static inline void tg3_full_unlock(struct tg3 *tp)
  5370. {
  5371. spin_unlock_bh(&tp->lock);
  5372. }
  5373. /* One-shot MSI handler - Chip automatically disables interrupt
  5374. * after sending MSI so driver doesn't have to do it.
  5375. */
  5376. static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
  5377. {
  5378. struct tg3_napi *tnapi = dev_id;
  5379. struct tg3 *tp = tnapi->tp;
  5380. prefetch(tnapi->hw_status);
  5381. if (tnapi->rx_rcb)
  5382. prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
  5383. if (likely(!tg3_irq_sync(tp)))
  5384. napi_schedule(&tnapi->napi);
  5385. return IRQ_HANDLED;
  5386. }
  5387. /* MSI ISR - No need to check for interrupt sharing and no need to
  5388. * flush status block and interrupt mailbox. PCI ordering rules
  5389. * guarantee that MSI will arrive after the status block.
  5390. */
  5391. static irqreturn_t tg3_msi(int irq, void *dev_id)
  5392. {
  5393. struct tg3_napi *tnapi = dev_id;
  5394. struct tg3 *tp = tnapi->tp;
  5395. prefetch(tnapi->hw_status);
  5396. if (tnapi->rx_rcb)
  5397. prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
  5398. /*
  5399. * Writing any value to intr-mbox-0 clears PCI INTA# and
  5400. * chip-internal interrupt pending events.
  5401. * Writing non-zero to intr-mbox-0 additional tells the
  5402. * NIC to stop sending us irqs, engaging "in-intr-handler"
  5403. * event coalescing.
  5404. */
  5405. tw32_mailbox(tnapi->int_mbox, 0x00000001);
  5406. if (likely(!tg3_irq_sync(tp)))
  5407. napi_schedule(&tnapi->napi);
  5408. return IRQ_RETVAL(1);
  5409. }
  5410. static irqreturn_t tg3_interrupt(int irq, void *dev_id)
  5411. {
  5412. struct tg3_napi *tnapi = dev_id;
  5413. struct tg3 *tp = tnapi->tp;
  5414. struct tg3_hw_status *sblk = tnapi->hw_status;
  5415. unsigned int handled = 1;
  5416. /* In INTx mode, it is possible for the interrupt to arrive at
  5417. * the CPU before the status block posted prior to the interrupt.
  5418. * Reading the PCI State register will confirm whether the
  5419. * interrupt is ours and will flush the status block.
  5420. */
  5421. if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
  5422. if (tg3_flag(tp, CHIP_RESETTING) ||
  5423. (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
  5424. handled = 0;
  5425. goto out;
  5426. }
  5427. }
  5428. /*
  5429. * Writing any value to intr-mbox-0 clears PCI INTA# and
  5430. * chip-internal interrupt pending events.
  5431. * Writing non-zero to intr-mbox-0 additional tells the
  5432. * NIC to stop sending us irqs, engaging "in-intr-handler"
  5433. * event coalescing.
  5434. *
  5435. * Flush the mailbox to de-assert the IRQ immediately to prevent
  5436. * spurious interrupts. The flush impacts performance but
  5437. * excessive spurious interrupts can be worse in some cases.
  5438. */
  5439. tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
  5440. if (tg3_irq_sync(tp))
  5441. goto out;
  5442. sblk->status &= ~SD_STATUS_UPDATED;
  5443. if (likely(tg3_has_work(tnapi))) {
  5444. prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
  5445. napi_schedule(&tnapi->napi);
  5446. } else {
  5447. /* No work, shared interrupt perhaps? re-enable
  5448. * interrupts, and flush that PCI write
  5449. */
  5450. tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
  5451. 0x00000000);
  5452. }
  5453. out:
  5454. return IRQ_RETVAL(handled);
  5455. }
  5456. static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
  5457. {
  5458. struct tg3_napi *tnapi = dev_id;
  5459. struct tg3 *tp = tnapi->tp;
  5460. struct tg3_hw_status *sblk = tnapi->hw_status;
  5461. unsigned int handled = 1;
  5462. /* In INTx mode, it is possible for the interrupt to arrive at
  5463. * the CPU before the status block posted prior to the interrupt.
  5464. * Reading the PCI State register will confirm whether the
  5465. * interrupt is ours and will flush the status block.
  5466. */
  5467. if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
  5468. if (tg3_flag(tp, CHIP_RESETTING) ||
  5469. (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
  5470. handled = 0;
  5471. goto out;
  5472. }
  5473. }
  5474. /*
  5475. * writing any value to intr-mbox-0 clears PCI INTA# and
  5476. * chip-internal interrupt pending events.
  5477. * writing non-zero to intr-mbox-0 additional tells the
  5478. * NIC to stop sending us irqs, engaging "in-intr-handler"
  5479. * event coalescing.
  5480. *
  5481. * Flush the mailbox to de-assert the IRQ immediately to prevent
  5482. * spurious interrupts. The flush impacts performance but
  5483. * excessive spurious interrupts can be worse in some cases.
  5484. */
  5485. tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
  5486. /*
  5487. * In a shared interrupt configuration, sometimes other devices'
  5488. * interrupts will scream. We record the current status tag here
  5489. * so that the above check can report that the screaming interrupts
  5490. * are unhandled. Eventually they will be silenced.
  5491. */
  5492. tnapi->last_irq_tag = sblk->status_tag;
  5493. if (tg3_irq_sync(tp))
  5494. goto out;
  5495. prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
  5496. napi_schedule(&tnapi->napi);
  5497. out:
  5498. return IRQ_RETVAL(handled);
  5499. }
  5500. /* ISR for interrupt test */
  5501. static irqreturn_t tg3_test_isr(int irq, void *dev_id)
  5502. {
  5503. struct tg3_napi *tnapi = dev_id;
  5504. struct tg3 *tp = tnapi->tp;
  5505. struct tg3_hw_status *sblk = tnapi->hw_status;
  5506. if ((sblk->status & SD_STATUS_UPDATED) ||
  5507. !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
  5508. tg3_disable_ints(tp);
  5509. return IRQ_RETVAL(1);
  5510. }
  5511. return IRQ_RETVAL(0);
  5512. }
  5513. #ifdef CONFIG_NET_POLL_CONTROLLER
  5514. static void tg3_poll_controller(struct net_device *dev)
  5515. {
  5516. int i;
  5517. struct tg3 *tp = netdev_priv(dev);
  5518. for (i = 0; i < tp->irq_cnt; i++)
  5519. tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
  5520. }
  5521. #endif
  5522. static void tg3_tx_timeout(struct net_device *dev)
  5523. {
  5524. struct tg3 *tp = netdev_priv(dev);
  5525. if (netif_msg_tx_err(tp)) {
  5526. netdev_err(dev, "transmit timed out, resetting\n");
  5527. tg3_dump_state(tp);
  5528. }
  5529. tg3_reset_task_schedule(tp);
  5530. }
  5531. /* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
  5532. static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
  5533. {
  5534. u32 base = (u32) mapping & 0xffffffff;
  5535. return (base > 0xffffdcc0) && (base + len + 8 < base);
  5536. }
  5537. /* Test for DMA addresses > 40-bit */
  5538. static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
  5539. int len)
  5540. {
  5541. #if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
  5542. if (tg3_flag(tp, 40BIT_DMA_BUG))
  5543. return ((u64) mapping + len) > DMA_BIT_MASK(40);
  5544. return 0;
  5545. #else
  5546. return 0;
  5547. #endif
  5548. }
  5549. static inline void tg3_tx_set_bd(struct tg3_tx_buffer_desc *txbd,
  5550. dma_addr_t mapping, u32 len, u32 flags,
  5551. u32 mss, u32 vlan)
  5552. {
  5553. txbd->addr_hi = ((u64) mapping >> 32);
  5554. txbd->addr_lo = ((u64) mapping & 0xffffffff);
  5555. txbd->len_flags = (len << TXD_LEN_SHIFT) | (flags & 0x0000ffff);
  5556. txbd->vlan_tag = (mss << TXD_MSS_SHIFT) | (vlan << TXD_VLAN_TAG_SHIFT);
  5557. }
  5558. static bool tg3_tx_frag_set(struct tg3_napi *tnapi, u32 *entry, u32 *budget,
  5559. dma_addr_t map, u32 len, u32 flags,
  5560. u32 mss, u32 vlan)
  5561. {
  5562. struct tg3 *tp = tnapi->tp;
  5563. bool hwbug = false;
  5564. if (tg3_flag(tp, SHORT_DMA_BUG) && len <= 8)
  5565. hwbug = true;
  5566. if (tg3_4g_overflow_test(map, len))
  5567. hwbug = true;
  5568. if (tg3_40bit_overflow_test(tp, map, len))
  5569. hwbug = true;
  5570. if (tp->dma_limit) {
  5571. u32 prvidx = *entry;
  5572. u32 tmp_flag = flags & ~TXD_FLAG_END;
  5573. while (len > tp->dma_limit && *budget) {
  5574. u32 frag_len = tp->dma_limit;
  5575. len -= tp->dma_limit;
  5576. /* Avoid the 8byte DMA problem */
  5577. if (len <= 8) {
  5578. len += tp->dma_limit / 2;
  5579. frag_len = tp->dma_limit / 2;
  5580. }
  5581. tnapi->tx_buffers[*entry].fragmented = true;
  5582. tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
  5583. frag_len, tmp_flag, mss, vlan);
  5584. *budget -= 1;
  5585. prvidx = *entry;
  5586. *entry = NEXT_TX(*entry);
  5587. map += frag_len;
  5588. }
  5589. if (len) {
  5590. if (*budget) {
  5591. tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
  5592. len, flags, mss, vlan);
  5593. *budget -= 1;
  5594. *entry = NEXT_TX(*entry);
  5595. } else {
  5596. hwbug = true;
  5597. tnapi->tx_buffers[prvidx].fragmented = false;
  5598. }
  5599. }
  5600. } else {
  5601. tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
  5602. len, flags, mss, vlan);
  5603. *entry = NEXT_TX(*entry);
  5604. }
  5605. return hwbug;
  5606. }
  5607. static void tg3_tx_skb_unmap(struct tg3_napi *tnapi, u32 entry, int last)
  5608. {
  5609. int i;
  5610. struct sk_buff *skb;
  5611. struct tg3_tx_ring_info *txb = &tnapi->tx_buffers[entry];
  5612. skb = txb->skb;
  5613. txb->skb = NULL;
  5614. pci_unmap_single(tnapi->tp->pdev,
  5615. dma_unmap_addr(txb, mapping),
  5616. skb_headlen(skb),
  5617. PCI_DMA_TODEVICE);
  5618. while (txb->fragmented) {
  5619. txb->fragmented = false;
  5620. entry = NEXT_TX(entry);
  5621. txb = &tnapi->tx_buffers[entry];
  5622. }
  5623. for (i = 0; i <= last; i++) {
  5624. const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  5625. entry = NEXT_TX(entry);
  5626. txb = &tnapi->tx_buffers[entry];
  5627. pci_unmap_page(tnapi->tp->pdev,
  5628. dma_unmap_addr(txb, mapping),
  5629. skb_frag_size(frag), PCI_DMA_TODEVICE);
  5630. while (txb->fragmented) {
  5631. txb->fragmented = false;
  5632. entry = NEXT_TX(entry);
  5633. txb = &tnapi->tx_buffers[entry];
  5634. }
  5635. }
  5636. }
  5637. /* Workaround 4GB and 40-bit hardware DMA bugs. */
  5638. static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
  5639. struct sk_buff **pskb,
  5640. u32 *entry, u32 *budget,
  5641. u32 base_flags, u32 mss, u32 vlan)
  5642. {
  5643. struct tg3 *tp = tnapi->tp;
  5644. struct sk_buff *new_skb, *skb = *pskb;
  5645. dma_addr_t new_addr = 0;
  5646. int ret = 0;
  5647. if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
  5648. new_skb = skb_copy(skb, GFP_ATOMIC);
  5649. else {
  5650. int more_headroom = 4 - ((unsigned long)skb->data & 3);
  5651. new_skb = skb_copy_expand(skb,
  5652. skb_headroom(skb) + more_headroom,
  5653. skb_tailroom(skb), GFP_ATOMIC);
  5654. }
  5655. if (!new_skb) {
  5656. ret = -1;
  5657. } else {
  5658. /* New SKB is guaranteed to be linear. */
  5659. new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
  5660. PCI_DMA_TODEVICE);
  5661. /* Make sure the mapping succeeded */
  5662. if (pci_dma_mapping_error(tp->pdev, new_addr)) {
  5663. dev_kfree_skb(new_skb);
  5664. ret = -1;
  5665. } else {
  5666. u32 save_entry = *entry;
  5667. base_flags |= TXD_FLAG_END;
  5668. tnapi->tx_buffers[*entry].skb = new_skb;
  5669. dma_unmap_addr_set(&tnapi->tx_buffers[*entry],
  5670. mapping, new_addr);
  5671. if (tg3_tx_frag_set(tnapi, entry, budget, new_addr,
  5672. new_skb->len, base_flags,
  5673. mss, vlan)) {
  5674. tg3_tx_skb_unmap(tnapi, save_entry, -1);
  5675. dev_kfree_skb(new_skb);
  5676. ret = -1;
  5677. }
  5678. }
  5679. }
  5680. dev_kfree_skb(skb);
  5681. *pskb = new_skb;
  5682. return ret;
  5683. }
  5684. static netdev_tx_t tg3_start_xmit(struct sk_buff *, struct net_device *);
  5685. /* Use GSO to workaround a rare TSO bug that may be triggered when the
  5686. * TSO header is greater than 80 bytes.
  5687. */
  5688. static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
  5689. {
  5690. struct sk_buff *segs, *nskb;
  5691. u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
  5692. /* Estimate the number of fragments in the worst case */
  5693. if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
  5694. netif_stop_queue(tp->dev);
  5695. /* netif_tx_stop_queue() must be done before checking
  5696. * checking tx index in tg3_tx_avail() below, because in
  5697. * tg3_tx(), we update tx index before checking for
  5698. * netif_tx_queue_stopped().
  5699. */
  5700. smp_mb();
  5701. if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
  5702. return NETDEV_TX_BUSY;
  5703. netif_wake_queue(tp->dev);
  5704. }
  5705. segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
  5706. if (IS_ERR(segs))
  5707. goto tg3_tso_bug_end;
  5708. do {
  5709. nskb = segs;
  5710. segs = segs->next;
  5711. nskb->next = NULL;
  5712. tg3_start_xmit(nskb, tp->dev);
  5713. } while (segs);
  5714. tg3_tso_bug_end:
  5715. dev_kfree_skb(skb);
  5716. return NETDEV_TX_OK;
  5717. }
  5718. /* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
  5719. * support TG3_FLAG_HW_TSO_1 or firmware TSO only.
  5720. */
  5721. static netdev_tx_t tg3_start_xmit(struct sk_buff *skb, struct net_device *dev)
  5722. {
  5723. struct tg3 *tp = netdev_priv(dev);
  5724. u32 len, entry, base_flags, mss, vlan = 0;
  5725. u32 budget;
  5726. int i = -1, would_hit_hwbug;
  5727. dma_addr_t mapping;
  5728. struct tg3_napi *tnapi;
  5729. struct netdev_queue *txq;
  5730. unsigned int last;
  5731. txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
  5732. tnapi = &tp->napi[skb_get_queue_mapping(skb)];
  5733. if (tg3_flag(tp, ENABLE_TSS))
  5734. tnapi++;
  5735. budget = tg3_tx_avail(tnapi);
  5736. /* We are running in BH disabled context with netif_tx_lock
  5737. * and TX reclaim runs via tp->napi.poll inside of a software
  5738. * interrupt. Furthermore, IRQ processing runs lockless so we have
  5739. * no IRQ context deadlocks to worry about either. Rejoice!
  5740. */
  5741. if (unlikely(budget <= (skb_shinfo(skb)->nr_frags + 1))) {
  5742. if (!netif_tx_queue_stopped(txq)) {
  5743. netif_tx_stop_queue(txq);
  5744. /* This is a hard error, log it. */
  5745. netdev_err(dev,
  5746. "BUG! Tx Ring full when queue awake!\n");
  5747. }
  5748. return NETDEV_TX_BUSY;
  5749. }
  5750. entry = tnapi->tx_prod;
  5751. base_flags = 0;
  5752. if (skb->ip_summed == CHECKSUM_PARTIAL)
  5753. base_flags |= TXD_FLAG_TCPUDP_CSUM;
  5754. mss = skb_shinfo(skb)->gso_size;
  5755. if (mss) {
  5756. struct iphdr *iph;
  5757. u32 tcp_opt_len, hdr_len;
  5758. if (skb_header_cloned(skb) &&
  5759. pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
  5760. goto drop;
  5761. iph = ip_hdr(skb);
  5762. tcp_opt_len = tcp_optlen(skb);
  5763. hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb) - ETH_HLEN;
  5764. if (!skb_is_gso_v6(skb)) {
  5765. iph->check = 0;
  5766. iph->tot_len = htons(mss + hdr_len);
  5767. }
  5768. if (unlikely((ETH_HLEN + hdr_len) > 80) &&
  5769. tg3_flag(tp, TSO_BUG))
  5770. return tg3_tso_bug(tp, skb);
  5771. base_flags |= (TXD_FLAG_CPU_PRE_DMA |
  5772. TXD_FLAG_CPU_POST_DMA);
  5773. if (tg3_flag(tp, HW_TSO_1) ||
  5774. tg3_flag(tp, HW_TSO_2) ||
  5775. tg3_flag(tp, HW_TSO_3)) {
  5776. tcp_hdr(skb)->check = 0;
  5777. base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
  5778. } else
  5779. tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
  5780. iph->daddr, 0,
  5781. IPPROTO_TCP,
  5782. 0);
  5783. if (tg3_flag(tp, HW_TSO_3)) {
  5784. mss |= (hdr_len & 0xc) << 12;
  5785. if (hdr_len & 0x10)
  5786. base_flags |= 0x00000010;
  5787. base_flags |= (hdr_len & 0x3e0) << 5;
  5788. } else if (tg3_flag(tp, HW_TSO_2))
  5789. mss |= hdr_len << 9;
  5790. else if (tg3_flag(tp, HW_TSO_1) ||
  5791. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
  5792. if (tcp_opt_len || iph->ihl > 5) {
  5793. int tsflags;
  5794. tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
  5795. mss |= (tsflags << 11);
  5796. }
  5797. } else {
  5798. if (tcp_opt_len || iph->ihl > 5) {
  5799. int tsflags;
  5800. tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
  5801. base_flags |= tsflags << 12;
  5802. }
  5803. }
  5804. }
  5805. if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
  5806. !mss && skb->len > VLAN_ETH_FRAME_LEN)
  5807. base_flags |= TXD_FLAG_JMB_PKT;
  5808. if (vlan_tx_tag_present(skb)) {
  5809. base_flags |= TXD_FLAG_VLAN;
  5810. vlan = vlan_tx_tag_get(skb);
  5811. }
  5812. len = skb_headlen(skb);
  5813. mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
  5814. if (pci_dma_mapping_error(tp->pdev, mapping))
  5815. goto drop;
  5816. tnapi->tx_buffers[entry].skb = skb;
  5817. dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
  5818. would_hit_hwbug = 0;
  5819. if (tg3_flag(tp, 5701_DMA_BUG))
  5820. would_hit_hwbug = 1;
  5821. if (tg3_tx_frag_set(tnapi, &entry, &budget, mapping, len, base_flags |
  5822. ((skb_shinfo(skb)->nr_frags == 0) ? TXD_FLAG_END : 0),
  5823. mss, vlan)) {
  5824. would_hit_hwbug = 1;
  5825. } else if (skb_shinfo(skb)->nr_frags > 0) {
  5826. u32 tmp_mss = mss;
  5827. if (!tg3_flag(tp, HW_TSO_1) &&
  5828. !tg3_flag(tp, HW_TSO_2) &&
  5829. !tg3_flag(tp, HW_TSO_3))
  5830. tmp_mss = 0;
  5831. /* Now loop through additional data
  5832. * fragments, and queue them.
  5833. */
  5834. last = skb_shinfo(skb)->nr_frags - 1;
  5835. for (i = 0; i <= last; i++) {
  5836. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  5837. len = skb_frag_size(frag);
  5838. mapping = skb_frag_dma_map(&tp->pdev->dev, frag, 0,
  5839. len, DMA_TO_DEVICE);
  5840. tnapi->tx_buffers[entry].skb = NULL;
  5841. dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
  5842. mapping);
  5843. if (dma_mapping_error(&tp->pdev->dev, mapping))
  5844. goto dma_error;
  5845. if (!budget ||
  5846. tg3_tx_frag_set(tnapi, &entry, &budget, mapping,
  5847. len, base_flags |
  5848. ((i == last) ? TXD_FLAG_END : 0),
  5849. tmp_mss, vlan)) {
  5850. would_hit_hwbug = 1;
  5851. break;
  5852. }
  5853. }
  5854. }
  5855. if (would_hit_hwbug) {
  5856. tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, i);
  5857. /* If the workaround fails due to memory/mapping
  5858. * failure, silently drop this packet.
  5859. */
  5860. entry = tnapi->tx_prod;
  5861. budget = tg3_tx_avail(tnapi);
  5862. if (tigon3_dma_hwbug_workaround(tnapi, &skb, &entry, &budget,
  5863. base_flags, mss, vlan))
  5864. goto drop_nofree;
  5865. }
  5866. skb_tx_timestamp(skb);
  5867. netdev_tx_sent_queue(txq, skb->len);
  5868. /* Sync BD data before updating mailbox */
  5869. wmb();
  5870. /* Packets are ready, update Tx producer idx local and on card. */
  5871. tw32_tx_mbox(tnapi->prodmbox, entry);
  5872. tnapi->tx_prod = entry;
  5873. if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
  5874. netif_tx_stop_queue(txq);
  5875. /* netif_tx_stop_queue() must be done before checking
  5876. * checking tx index in tg3_tx_avail() below, because in
  5877. * tg3_tx(), we update tx index before checking for
  5878. * netif_tx_queue_stopped().
  5879. */
  5880. smp_mb();
  5881. if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
  5882. netif_tx_wake_queue(txq);
  5883. }
  5884. mmiowb();
  5885. return NETDEV_TX_OK;
  5886. dma_error:
  5887. tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, --i);
  5888. tnapi->tx_buffers[tnapi->tx_prod].skb = NULL;
  5889. drop:
  5890. dev_kfree_skb(skb);
  5891. drop_nofree:
  5892. tp->tx_dropped++;
  5893. return NETDEV_TX_OK;
  5894. }
  5895. static void tg3_mac_loopback(struct tg3 *tp, bool enable)
  5896. {
  5897. if (enable) {
  5898. tp->mac_mode &= ~(MAC_MODE_HALF_DUPLEX |
  5899. MAC_MODE_PORT_MODE_MASK);
  5900. tp->mac_mode |= MAC_MODE_PORT_INT_LPBACK;
  5901. if (!tg3_flag(tp, 5705_PLUS))
  5902. tp->mac_mode |= MAC_MODE_LINK_POLARITY;
  5903. if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
  5904. tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
  5905. else
  5906. tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
  5907. } else {
  5908. tp->mac_mode &= ~MAC_MODE_PORT_INT_LPBACK;
  5909. if (tg3_flag(tp, 5705_PLUS) ||
  5910. (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) ||
  5911. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
  5912. tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
  5913. }
  5914. tw32(MAC_MODE, tp->mac_mode);
  5915. udelay(40);
  5916. }
  5917. static int tg3_phy_lpbk_set(struct tg3 *tp, u32 speed, bool extlpbk)
  5918. {
  5919. u32 val, bmcr, mac_mode, ptest = 0;
  5920. tg3_phy_toggle_apd(tp, false);
  5921. tg3_phy_toggle_automdix(tp, 0);
  5922. if (extlpbk && tg3_phy_set_extloopbk(tp))
  5923. return -EIO;
  5924. bmcr = BMCR_FULLDPLX;
  5925. switch (speed) {
  5926. case SPEED_10:
  5927. break;
  5928. case SPEED_100:
  5929. bmcr |= BMCR_SPEED100;
  5930. break;
  5931. case SPEED_1000:
  5932. default:
  5933. if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
  5934. speed = SPEED_100;
  5935. bmcr |= BMCR_SPEED100;
  5936. } else {
  5937. speed = SPEED_1000;
  5938. bmcr |= BMCR_SPEED1000;
  5939. }
  5940. }
  5941. if (extlpbk) {
  5942. if (!(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
  5943. tg3_readphy(tp, MII_CTRL1000, &val);
  5944. val |= CTL1000_AS_MASTER |
  5945. CTL1000_ENABLE_MASTER;
  5946. tg3_writephy(tp, MII_CTRL1000, val);
  5947. } else {
  5948. ptest = MII_TG3_FET_PTEST_TRIM_SEL |
  5949. MII_TG3_FET_PTEST_TRIM_2;
  5950. tg3_writephy(tp, MII_TG3_FET_PTEST, ptest);
  5951. }
  5952. } else
  5953. bmcr |= BMCR_LOOPBACK;
  5954. tg3_writephy(tp, MII_BMCR, bmcr);
  5955. /* The write needs to be flushed for the FETs */
  5956. if (tp->phy_flags & TG3_PHYFLG_IS_FET)
  5957. tg3_readphy(tp, MII_BMCR, &bmcr);
  5958. udelay(40);
  5959. if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
  5960. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
  5961. tg3_writephy(tp, MII_TG3_FET_PTEST, ptest |
  5962. MII_TG3_FET_PTEST_FRC_TX_LINK |
  5963. MII_TG3_FET_PTEST_FRC_TX_LOCK);
  5964. /* The write needs to be flushed for the AC131 */
  5965. tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
  5966. }
  5967. /* Reset to prevent losing 1st rx packet intermittently */
  5968. if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
  5969. tg3_flag(tp, 5780_CLASS)) {
  5970. tw32_f(MAC_RX_MODE, RX_MODE_RESET);
  5971. udelay(10);
  5972. tw32_f(MAC_RX_MODE, tp->rx_mode);
  5973. }
  5974. mac_mode = tp->mac_mode &
  5975. ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
  5976. if (speed == SPEED_1000)
  5977. mac_mode |= MAC_MODE_PORT_MODE_GMII;
  5978. else
  5979. mac_mode |= MAC_MODE_PORT_MODE_MII;
  5980. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
  5981. u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
  5982. if (masked_phy_id == TG3_PHY_ID_BCM5401)
  5983. mac_mode &= ~MAC_MODE_LINK_POLARITY;
  5984. else if (masked_phy_id == TG3_PHY_ID_BCM5411)
  5985. mac_mode |= MAC_MODE_LINK_POLARITY;
  5986. tg3_writephy(tp, MII_TG3_EXT_CTRL,
  5987. MII_TG3_EXT_CTRL_LNK3_LED_MODE);
  5988. }
  5989. tw32(MAC_MODE, mac_mode);
  5990. udelay(40);
  5991. return 0;
  5992. }
  5993. static void tg3_set_loopback(struct net_device *dev, netdev_features_t features)
  5994. {
  5995. struct tg3 *tp = netdev_priv(dev);
  5996. if (features & NETIF_F_LOOPBACK) {
  5997. if (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK)
  5998. return;
  5999. spin_lock_bh(&tp->lock);
  6000. tg3_mac_loopback(tp, true);
  6001. netif_carrier_on(tp->dev);
  6002. spin_unlock_bh(&tp->lock);
  6003. netdev_info(dev, "Internal MAC loopback mode enabled.\n");
  6004. } else {
  6005. if (!(tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
  6006. return;
  6007. spin_lock_bh(&tp->lock);
  6008. tg3_mac_loopback(tp, false);
  6009. /* Force link status check */
  6010. tg3_setup_phy(tp, 1);
  6011. spin_unlock_bh(&tp->lock);
  6012. netdev_info(dev, "Internal MAC loopback mode disabled.\n");
  6013. }
  6014. }
  6015. static netdev_features_t tg3_fix_features(struct net_device *dev,
  6016. netdev_features_t features)
  6017. {
  6018. struct tg3 *tp = netdev_priv(dev);
  6019. if (dev->mtu > ETH_DATA_LEN && tg3_flag(tp, 5780_CLASS))
  6020. features &= ~NETIF_F_ALL_TSO;
  6021. return features;
  6022. }
  6023. static int tg3_set_features(struct net_device *dev, netdev_features_t features)
  6024. {
  6025. netdev_features_t changed = dev->features ^ features;
  6026. if ((changed & NETIF_F_LOOPBACK) && netif_running(dev))
  6027. tg3_set_loopback(dev, features);
  6028. return 0;
  6029. }
  6030. static void tg3_rx_prodring_free(struct tg3 *tp,
  6031. struct tg3_rx_prodring_set *tpr)
  6032. {
  6033. int i;
  6034. if (tpr != &tp->napi[0].prodring) {
  6035. for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
  6036. i = (i + 1) & tp->rx_std_ring_mask)
  6037. tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
  6038. tp->rx_pkt_map_sz);
  6039. if (tg3_flag(tp, JUMBO_CAPABLE)) {
  6040. for (i = tpr->rx_jmb_cons_idx;
  6041. i != tpr->rx_jmb_prod_idx;
  6042. i = (i + 1) & tp->rx_jmb_ring_mask) {
  6043. tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
  6044. TG3_RX_JMB_MAP_SZ);
  6045. }
  6046. }
  6047. return;
  6048. }
  6049. for (i = 0; i <= tp->rx_std_ring_mask; i++)
  6050. tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
  6051. tp->rx_pkt_map_sz);
  6052. if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
  6053. for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
  6054. tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
  6055. TG3_RX_JMB_MAP_SZ);
  6056. }
  6057. }
  6058. /* Initialize rx rings for packet processing.
  6059. *
  6060. * The chip has been shut down and the driver detached from
  6061. * the networking, so no interrupts or new tx packets will
  6062. * end up in the driver. tp->{tx,}lock are held and thus
  6063. * we may not sleep.
  6064. */
  6065. static int tg3_rx_prodring_alloc(struct tg3 *tp,
  6066. struct tg3_rx_prodring_set *tpr)
  6067. {
  6068. u32 i, rx_pkt_dma_sz;
  6069. tpr->rx_std_cons_idx = 0;
  6070. tpr->rx_std_prod_idx = 0;
  6071. tpr->rx_jmb_cons_idx = 0;
  6072. tpr->rx_jmb_prod_idx = 0;
  6073. if (tpr != &tp->napi[0].prodring) {
  6074. memset(&tpr->rx_std_buffers[0], 0,
  6075. TG3_RX_STD_BUFF_RING_SIZE(tp));
  6076. if (tpr->rx_jmb_buffers)
  6077. memset(&tpr->rx_jmb_buffers[0], 0,
  6078. TG3_RX_JMB_BUFF_RING_SIZE(tp));
  6079. goto done;
  6080. }
  6081. /* Zero out all descriptors. */
  6082. memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
  6083. rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
  6084. if (tg3_flag(tp, 5780_CLASS) &&
  6085. tp->dev->mtu > ETH_DATA_LEN)
  6086. rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
  6087. tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
  6088. /* Initialize invariants of the rings, we only set this
  6089. * stuff once. This works because the card does not
  6090. * write into the rx buffer posting rings.
  6091. */
  6092. for (i = 0; i <= tp->rx_std_ring_mask; i++) {
  6093. struct tg3_rx_buffer_desc *rxd;
  6094. rxd = &tpr->rx_std[i];
  6095. rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
  6096. rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
  6097. rxd->opaque = (RXD_OPAQUE_RING_STD |
  6098. (i << RXD_OPAQUE_INDEX_SHIFT));
  6099. }
  6100. /* Now allocate fresh SKBs for each rx ring. */
  6101. for (i = 0; i < tp->rx_pending; i++) {
  6102. unsigned int frag_size;
  6103. if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_STD, i,
  6104. &frag_size) < 0) {
  6105. netdev_warn(tp->dev,
  6106. "Using a smaller RX standard ring. Only "
  6107. "%d out of %d buffers were allocated "
  6108. "successfully\n", i, tp->rx_pending);
  6109. if (i == 0)
  6110. goto initfail;
  6111. tp->rx_pending = i;
  6112. break;
  6113. }
  6114. }
  6115. if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
  6116. goto done;
  6117. memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
  6118. if (!tg3_flag(tp, JUMBO_RING_ENABLE))
  6119. goto done;
  6120. for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
  6121. struct tg3_rx_buffer_desc *rxd;
  6122. rxd = &tpr->rx_jmb[i].std;
  6123. rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
  6124. rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
  6125. RXD_FLAG_JUMBO;
  6126. rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
  6127. (i << RXD_OPAQUE_INDEX_SHIFT));
  6128. }
  6129. for (i = 0; i < tp->rx_jumbo_pending; i++) {
  6130. unsigned int frag_size;
  6131. if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_JUMBO, i,
  6132. &frag_size) < 0) {
  6133. netdev_warn(tp->dev,
  6134. "Using a smaller RX jumbo ring. Only %d "
  6135. "out of %d buffers were allocated "
  6136. "successfully\n", i, tp->rx_jumbo_pending);
  6137. if (i == 0)
  6138. goto initfail;
  6139. tp->rx_jumbo_pending = i;
  6140. break;
  6141. }
  6142. }
  6143. done:
  6144. return 0;
  6145. initfail:
  6146. tg3_rx_prodring_free(tp, tpr);
  6147. return -ENOMEM;
  6148. }
  6149. static void tg3_rx_prodring_fini(struct tg3 *tp,
  6150. struct tg3_rx_prodring_set *tpr)
  6151. {
  6152. kfree(tpr->rx_std_buffers);
  6153. tpr->rx_std_buffers = NULL;
  6154. kfree(tpr->rx_jmb_buffers);
  6155. tpr->rx_jmb_buffers = NULL;
  6156. if (tpr->rx_std) {
  6157. dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
  6158. tpr->rx_std, tpr->rx_std_mapping);
  6159. tpr->rx_std = NULL;
  6160. }
  6161. if (tpr->rx_jmb) {
  6162. dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
  6163. tpr->rx_jmb, tpr->rx_jmb_mapping);
  6164. tpr->rx_jmb = NULL;
  6165. }
  6166. }
  6167. static int tg3_rx_prodring_init(struct tg3 *tp,
  6168. struct tg3_rx_prodring_set *tpr)
  6169. {
  6170. tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
  6171. GFP_KERNEL);
  6172. if (!tpr->rx_std_buffers)
  6173. return -ENOMEM;
  6174. tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
  6175. TG3_RX_STD_RING_BYTES(tp),
  6176. &tpr->rx_std_mapping,
  6177. GFP_KERNEL);
  6178. if (!tpr->rx_std)
  6179. goto err_out;
  6180. if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
  6181. tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
  6182. GFP_KERNEL);
  6183. if (!tpr->rx_jmb_buffers)
  6184. goto err_out;
  6185. tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
  6186. TG3_RX_JMB_RING_BYTES(tp),
  6187. &tpr->rx_jmb_mapping,
  6188. GFP_KERNEL);
  6189. if (!tpr->rx_jmb)
  6190. goto err_out;
  6191. }
  6192. return 0;
  6193. err_out:
  6194. tg3_rx_prodring_fini(tp, tpr);
  6195. return -ENOMEM;
  6196. }
  6197. /* Free up pending packets in all rx/tx rings.
  6198. *
  6199. * The chip has been shut down and the driver detached from
  6200. * the networking, so no interrupts or new tx packets will
  6201. * end up in the driver. tp->{tx,}lock is not held and we are not
  6202. * in an interrupt context and thus may sleep.
  6203. */
  6204. static void tg3_free_rings(struct tg3 *tp)
  6205. {
  6206. int i, j;
  6207. for (j = 0; j < tp->irq_cnt; j++) {
  6208. struct tg3_napi *tnapi = &tp->napi[j];
  6209. tg3_rx_prodring_free(tp, &tnapi->prodring);
  6210. if (!tnapi->tx_buffers)
  6211. continue;
  6212. for (i = 0; i < TG3_TX_RING_SIZE; i++) {
  6213. struct sk_buff *skb = tnapi->tx_buffers[i].skb;
  6214. if (!skb)
  6215. continue;
  6216. tg3_tx_skb_unmap(tnapi, i,
  6217. skb_shinfo(skb)->nr_frags - 1);
  6218. dev_kfree_skb_any(skb);
  6219. }
  6220. netdev_tx_reset_queue(netdev_get_tx_queue(tp->dev, j));
  6221. }
  6222. }
  6223. /* Initialize tx/rx rings for packet processing.
  6224. *
  6225. * The chip has been shut down and the driver detached from
  6226. * the networking, so no interrupts or new tx packets will
  6227. * end up in the driver. tp->{tx,}lock are held and thus
  6228. * we may not sleep.
  6229. */
  6230. static int tg3_init_rings(struct tg3 *tp)
  6231. {
  6232. int i;
  6233. /* Free up all the SKBs. */
  6234. tg3_free_rings(tp);
  6235. for (i = 0; i < tp->irq_cnt; i++) {
  6236. struct tg3_napi *tnapi = &tp->napi[i];
  6237. tnapi->last_tag = 0;
  6238. tnapi->last_irq_tag = 0;
  6239. tnapi->hw_status->status = 0;
  6240. tnapi->hw_status->status_tag = 0;
  6241. memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
  6242. tnapi->tx_prod = 0;
  6243. tnapi->tx_cons = 0;
  6244. if (tnapi->tx_ring)
  6245. memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
  6246. tnapi->rx_rcb_ptr = 0;
  6247. if (tnapi->rx_rcb)
  6248. memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
  6249. if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
  6250. tg3_free_rings(tp);
  6251. return -ENOMEM;
  6252. }
  6253. }
  6254. return 0;
  6255. }
  6256. static void tg3_mem_tx_release(struct tg3 *tp)
  6257. {
  6258. int i;
  6259. for (i = 0; i < tp->irq_max; i++) {
  6260. struct tg3_napi *tnapi = &tp->napi[i];
  6261. if (tnapi->tx_ring) {
  6262. dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
  6263. tnapi->tx_ring, tnapi->tx_desc_mapping);
  6264. tnapi->tx_ring = NULL;
  6265. }
  6266. kfree(tnapi->tx_buffers);
  6267. tnapi->tx_buffers = NULL;
  6268. }
  6269. }
  6270. static int tg3_mem_tx_acquire(struct tg3 *tp)
  6271. {
  6272. int i;
  6273. struct tg3_napi *tnapi = &tp->napi[0];
  6274. /* If multivector TSS is enabled, vector 0 does not handle
  6275. * tx interrupts. Don't allocate any resources for it.
  6276. */
  6277. if (tg3_flag(tp, ENABLE_TSS))
  6278. tnapi++;
  6279. for (i = 0; i < tp->txq_cnt; i++, tnapi++) {
  6280. tnapi->tx_buffers = kzalloc(sizeof(struct tg3_tx_ring_info) *
  6281. TG3_TX_RING_SIZE, GFP_KERNEL);
  6282. if (!tnapi->tx_buffers)
  6283. goto err_out;
  6284. tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
  6285. TG3_TX_RING_BYTES,
  6286. &tnapi->tx_desc_mapping,
  6287. GFP_KERNEL);
  6288. if (!tnapi->tx_ring)
  6289. goto err_out;
  6290. }
  6291. return 0;
  6292. err_out:
  6293. tg3_mem_tx_release(tp);
  6294. return -ENOMEM;
  6295. }
  6296. static void tg3_mem_rx_release(struct tg3 *tp)
  6297. {
  6298. int i;
  6299. for (i = 0; i < tp->irq_max; i++) {
  6300. struct tg3_napi *tnapi = &tp->napi[i];
  6301. tg3_rx_prodring_fini(tp, &tnapi->prodring);
  6302. if (!tnapi->rx_rcb)
  6303. continue;
  6304. dma_free_coherent(&tp->pdev->dev,
  6305. TG3_RX_RCB_RING_BYTES(tp),
  6306. tnapi->rx_rcb,
  6307. tnapi->rx_rcb_mapping);
  6308. tnapi->rx_rcb = NULL;
  6309. }
  6310. }
  6311. static int tg3_mem_rx_acquire(struct tg3 *tp)
  6312. {
  6313. unsigned int i, limit;
  6314. limit = tp->rxq_cnt;
  6315. /* If RSS is enabled, we need a (dummy) producer ring
  6316. * set on vector zero. This is the true hw prodring.
  6317. */
  6318. if (tg3_flag(tp, ENABLE_RSS))
  6319. limit++;
  6320. for (i = 0; i < limit; i++) {
  6321. struct tg3_napi *tnapi = &tp->napi[i];
  6322. if (tg3_rx_prodring_init(tp, &tnapi->prodring))
  6323. goto err_out;
  6324. /* If multivector RSS is enabled, vector 0
  6325. * does not handle rx or tx interrupts.
  6326. * Don't allocate any resources for it.
  6327. */
  6328. if (!i && tg3_flag(tp, ENABLE_RSS))
  6329. continue;
  6330. tnapi->rx_rcb = dma_alloc_coherent(&tp->pdev->dev,
  6331. TG3_RX_RCB_RING_BYTES(tp),
  6332. &tnapi->rx_rcb_mapping,
  6333. GFP_KERNEL);
  6334. if (!tnapi->rx_rcb)
  6335. goto err_out;
  6336. memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
  6337. }
  6338. return 0;
  6339. err_out:
  6340. tg3_mem_rx_release(tp);
  6341. return -ENOMEM;
  6342. }
  6343. /*
  6344. * Must not be invoked with interrupt sources disabled and
  6345. * the hardware shutdown down.
  6346. */
  6347. static void tg3_free_consistent(struct tg3 *tp)
  6348. {
  6349. int i;
  6350. for (i = 0; i < tp->irq_cnt; i++) {
  6351. struct tg3_napi *tnapi = &tp->napi[i];
  6352. if (tnapi->hw_status) {
  6353. dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
  6354. tnapi->hw_status,
  6355. tnapi->status_mapping);
  6356. tnapi->hw_status = NULL;
  6357. }
  6358. }
  6359. tg3_mem_rx_release(tp);
  6360. tg3_mem_tx_release(tp);
  6361. if (tp->hw_stats) {
  6362. dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
  6363. tp->hw_stats, tp->stats_mapping);
  6364. tp->hw_stats = NULL;
  6365. }
  6366. }
  6367. /*
  6368. * Must not be invoked with interrupt sources disabled and
  6369. * the hardware shutdown down. Can sleep.
  6370. */
  6371. static int tg3_alloc_consistent(struct tg3 *tp)
  6372. {
  6373. int i;
  6374. tp->hw_stats = dma_alloc_coherent(&tp->pdev->dev,
  6375. sizeof(struct tg3_hw_stats),
  6376. &tp->stats_mapping,
  6377. GFP_KERNEL);
  6378. if (!tp->hw_stats)
  6379. goto err_out;
  6380. memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
  6381. for (i = 0; i < tp->irq_cnt; i++) {
  6382. struct tg3_napi *tnapi = &tp->napi[i];
  6383. struct tg3_hw_status *sblk;
  6384. tnapi->hw_status = dma_alloc_coherent(&tp->pdev->dev,
  6385. TG3_HW_STATUS_SIZE,
  6386. &tnapi->status_mapping,
  6387. GFP_KERNEL);
  6388. if (!tnapi->hw_status)
  6389. goto err_out;
  6390. memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
  6391. sblk = tnapi->hw_status;
  6392. if (tg3_flag(tp, ENABLE_RSS)) {
  6393. u16 *prodptr = NULL;
  6394. /*
  6395. * When RSS is enabled, the status block format changes
  6396. * slightly. The "rx_jumbo_consumer", "reserved",
  6397. * and "rx_mini_consumer" members get mapped to the
  6398. * other three rx return ring producer indexes.
  6399. */
  6400. switch (i) {
  6401. case 1:
  6402. prodptr = &sblk->idx[0].rx_producer;
  6403. break;
  6404. case 2:
  6405. prodptr = &sblk->rx_jumbo_consumer;
  6406. break;
  6407. case 3:
  6408. prodptr = &sblk->reserved;
  6409. break;
  6410. case 4:
  6411. prodptr = &sblk->rx_mini_consumer;
  6412. break;
  6413. }
  6414. tnapi->rx_rcb_prod_idx = prodptr;
  6415. } else {
  6416. tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
  6417. }
  6418. }
  6419. if (tg3_mem_tx_acquire(tp) || tg3_mem_rx_acquire(tp))
  6420. goto err_out;
  6421. return 0;
  6422. err_out:
  6423. tg3_free_consistent(tp);
  6424. return -ENOMEM;
  6425. }
  6426. #define MAX_WAIT_CNT 1000
  6427. /* To stop a block, clear the enable bit and poll till it
  6428. * clears. tp->lock is held.
  6429. */
  6430. static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
  6431. {
  6432. unsigned int i;
  6433. u32 val;
  6434. if (tg3_flag(tp, 5705_PLUS)) {
  6435. switch (ofs) {
  6436. case RCVLSC_MODE:
  6437. case DMAC_MODE:
  6438. case MBFREE_MODE:
  6439. case BUFMGR_MODE:
  6440. case MEMARB_MODE:
  6441. /* We can't enable/disable these bits of the
  6442. * 5705/5750, just say success.
  6443. */
  6444. return 0;
  6445. default:
  6446. break;
  6447. }
  6448. }
  6449. val = tr32(ofs);
  6450. val &= ~enable_bit;
  6451. tw32_f(ofs, val);
  6452. for (i = 0; i < MAX_WAIT_CNT; i++) {
  6453. udelay(100);
  6454. val = tr32(ofs);
  6455. if ((val & enable_bit) == 0)
  6456. break;
  6457. }
  6458. if (i == MAX_WAIT_CNT && !silent) {
  6459. dev_err(&tp->pdev->dev,
  6460. "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
  6461. ofs, enable_bit);
  6462. return -ENODEV;
  6463. }
  6464. return 0;
  6465. }
  6466. /* tp->lock is held. */
  6467. static int tg3_abort_hw(struct tg3 *tp, int silent)
  6468. {
  6469. int i, err;
  6470. tg3_disable_ints(tp);
  6471. tp->rx_mode &= ~RX_MODE_ENABLE;
  6472. tw32_f(MAC_RX_MODE, tp->rx_mode);
  6473. udelay(10);
  6474. err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
  6475. err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
  6476. err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
  6477. err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
  6478. err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
  6479. err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
  6480. err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
  6481. err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
  6482. err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
  6483. err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
  6484. err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
  6485. err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
  6486. err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
  6487. tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
  6488. tw32_f(MAC_MODE, tp->mac_mode);
  6489. udelay(40);
  6490. tp->tx_mode &= ~TX_MODE_ENABLE;
  6491. tw32_f(MAC_TX_MODE, tp->tx_mode);
  6492. for (i = 0; i < MAX_WAIT_CNT; i++) {
  6493. udelay(100);
  6494. if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
  6495. break;
  6496. }
  6497. if (i >= MAX_WAIT_CNT) {
  6498. dev_err(&tp->pdev->dev,
  6499. "%s timed out, TX_MODE_ENABLE will not clear "
  6500. "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
  6501. err |= -ENODEV;
  6502. }
  6503. err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
  6504. err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
  6505. err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
  6506. tw32(FTQ_RESET, 0xffffffff);
  6507. tw32(FTQ_RESET, 0x00000000);
  6508. err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
  6509. err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
  6510. for (i = 0; i < tp->irq_cnt; i++) {
  6511. struct tg3_napi *tnapi = &tp->napi[i];
  6512. if (tnapi->hw_status)
  6513. memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
  6514. }
  6515. return err;
  6516. }
  6517. /* Save PCI command register before chip reset */
  6518. static void tg3_save_pci_state(struct tg3 *tp)
  6519. {
  6520. pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
  6521. }
  6522. /* Restore PCI state after chip reset */
  6523. static void tg3_restore_pci_state(struct tg3 *tp)
  6524. {
  6525. u32 val;
  6526. /* Re-enable indirect register accesses. */
  6527. pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
  6528. tp->misc_host_ctrl);
  6529. /* Set MAX PCI retry to zero. */
  6530. val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
  6531. if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
  6532. tg3_flag(tp, PCIX_MODE))
  6533. val |= PCISTATE_RETRY_SAME_DMA;
  6534. /* Allow reads and writes to the APE register and memory space. */
  6535. if (tg3_flag(tp, ENABLE_APE))
  6536. val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
  6537. PCISTATE_ALLOW_APE_SHMEM_WR |
  6538. PCISTATE_ALLOW_APE_PSPACE_WR;
  6539. pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
  6540. pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
  6541. if (!tg3_flag(tp, PCI_EXPRESS)) {
  6542. pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
  6543. tp->pci_cacheline_sz);
  6544. pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
  6545. tp->pci_lat_timer);
  6546. }
  6547. /* Make sure PCI-X relaxed ordering bit is clear. */
  6548. if (tg3_flag(tp, PCIX_MODE)) {
  6549. u16 pcix_cmd;
  6550. pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
  6551. &pcix_cmd);
  6552. pcix_cmd &= ~PCI_X_CMD_ERO;
  6553. pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
  6554. pcix_cmd);
  6555. }
  6556. if (tg3_flag(tp, 5780_CLASS)) {
  6557. /* Chip reset on 5780 will reset MSI enable bit,
  6558. * so need to restore it.
  6559. */
  6560. if (tg3_flag(tp, USING_MSI)) {
  6561. u16 ctrl;
  6562. pci_read_config_word(tp->pdev,
  6563. tp->msi_cap + PCI_MSI_FLAGS,
  6564. &ctrl);
  6565. pci_write_config_word(tp->pdev,
  6566. tp->msi_cap + PCI_MSI_FLAGS,
  6567. ctrl | PCI_MSI_FLAGS_ENABLE);
  6568. val = tr32(MSGINT_MODE);
  6569. tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
  6570. }
  6571. }
  6572. }
  6573. /* tp->lock is held. */
  6574. static int tg3_chip_reset(struct tg3 *tp)
  6575. {
  6576. u32 val;
  6577. void (*write_op)(struct tg3 *, u32, u32);
  6578. int i, err;
  6579. tg3_nvram_lock(tp);
  6580. tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
  6581. /* No matching tg3_nvram_unlock() after this because
  6582. * chip reset below will undo the nvram lock.
  6583. */
  6584. tp->nvram_lock_cnt = 0;
  6585. /* GRC_MISC_CFG core clock reset will clear the memory
  6586. * enable bit in PCI register 4 and the MSI enable bit
  6587. * on some chips, so we save relevant registers here.
  6588. */
  6589. tg3_save_pci_state(tp);
  6590. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
  6591. tg3_flag(tp, 5755_PLUS))
  6592. tw32(GRC_FASTBOOT_PC, 0);
  6593. /*
  6594. * We must avoid the readl() that normally takes place.
  6595. * It locks machines, causes machine checks, and other
  6596. * fun things. So, temporarily disable the 5701
  6597. * hardware workaround, while we do the reset.
  6598. */
  6599. write_op = tp->write32;
  6600. if (write_op == tg3_write_flush_reg32)
  6601. tp->write32 = tg3_write32;
  6602. /* Prevent the irq handler from reading or writing PCI registers
  6603. * during chip reset when the memory enable bit in the PCI command
  6604. * register may be cleared. The chip does not generate interrupt
  6605. * at this time, but the irq handler may still be called due to irq
  6606. * sharing or irqpoll.
  6607. */
  6608. tg3_flag_set(tp, CHIP_RESETTING);
  6609. for (i = 0; i < tp->irq_cnt; i++) {
  6610. struct tg3_napi *tnapi = &tp->napi[i];
  6611. if (tnapi->hw_status) {
  6612. tnapi->hw_status->status = 0;
  6613. tnapi->hw_status->status_tag = 0;
  6614. }
  6615. tnapi->last_tag = 0;
  6616. tnapi->last_irq_tag = 0;
  6617. }
  6618. smp_mb();
  6619. for (i = 0; i < tp->irq_cnt; i++)
  6620. synchronize_irq(tp->napi[i].irq_vec);
  6621. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
  6622. val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
  6623. tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
  6624. }
  6625. /* do the reset */
  6626. val = GRC_MISC_CFG_CORECLK_RESET;
  6627. if (tg3_flag(tp, PCI_EXPRESS)) {
  6628. /* Force PCIe 1.0a mode */
  6629. if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
  6630. !tg3_flag(tp, 57765_PLUS) &&
  6631. tr32(TG3_PCIE_PHY_TSTCTL) ==
  6632. (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
  6633. tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
  6634. if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
  6635. tw32(GRC_MISC_CFG, (1 << 29));
  6636. val |= (1 << 29);
  6637. }
  6638. }
  6639. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
  6640. tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
  6641. tw32(GRC_VCPU_EXT_CTRL,
  6642. tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
  6643. }
  6644. /* Manage gphy power for all CPMU absent PCIe devices. */
  6645. if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, CPMU_PRESENT))
  6646. val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
  6647. tw32(GRC_MISC_CFG, val);
  6648. /* restore 5701 hardware bug workaround write method */
  6649. tp->write32 = write_op;
  6650. /* Unfortunately, we have to delay before the PCI read back.
  6651. * Some 575X chips even will not respond to a PCI cfg access
  6652. * when the reset command is given to the chip.
  6653. *
  6654. * How do these hardware designers expect things to work
  6655. * properly if the PCI write is posted for a long period
  6656. * of time? It is always necessary to have some method by
  6657. * which a register read back can occur to push the write
  6658. * out which does the reset.
  6659. *
  6660. * For most tg3 variants the trick below was working.
  6661. * Ho hum...
  6662. */
  6663. udelay(120);
  6664. /* Flush PCI posted writes. The normal MMIO registers
  6665. * are inaccessible at this time so this is the only
  6666. * way to make this reliably (actually, this is no longer
  6667. * the case, see above). I tried to use indirect
  6668. * register read/write but this upset some 5701 variants.
  6669. */
  6670. pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
  6671. udelay(120);
  6672. if (tg3_flag(tp, PCI_EXPRESS) && pci_is_pcie(tp->pdev)) {
  6673. u16 val16;
  6674. if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
  6675. int j;
  6676. u32 cfg_val;
  6677. /* Wait for link training to complete. */
  6678. for (j = 0; j < 5000; j++)
  6679. udelay(100);
  6680. pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
  6681. pci_write_config_dword(tp->pdev, 0xc4,
  6682. cfg_val | (1 << 15));
  6683. }
  6684. /* Clear the "no snoop" and "relaxed ordering" bits. */
  6685. val16 = PCI_EXP_DEVCTL_RELAX_EN | PCI_EXP_DEVCTL_NOSNOOP_EN;
  6686. /*
  6687. * Older PCIe devices only support the 128 byte
  6688. * MPS setting. Enforce the restriction.
  6689. */
  6690. if (!tg3_flag(tp, CPMU_PRESENT))
  6691. val16 |= PCI_EXP_DEVCTL_PAYLOAD;
  6692. pcie_capability_clear_word(tp->pdev, PCI_EXP_DEVCTL, val16);
  6693. /* Clear error status */
  6694. pcie_capability_write_word(tp->pdev, PCI_EXP_DEVSTA,
  6695. PCI_EXP_DEVSTA_CED |
  6696. PCI_EXP_DEVSTA_NFED |
  6697. PCI_EXP_DEVSTA_FED |
  6698. PCI_EXP_DEVSTA_URD);
  6699. }
  6700. tg3_restore_pci_state(tp);
  6701. tg3_flag_clear(tp, CHIP_RESETTING);
  6702. tg3_flag_clear(tp, ERROR_PROCESSED);
  6703. val = 0;
  6704. if (tg3_flag(tp, 5780_CLASS))
  6705. val = tr32(MEMARB_MODE);
  6706. tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
  6707. if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
  6708. tg3_stop_fw(tp);
  6709. tw32(0x5000, 0x400);
  6710. }
  6711. tw32(GRC_MODE, tp->grc_mode);
  6712. if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
  6713. val = tr32(0xc4);
  6714. tw32(0xc4, val | (1 << 15));
  6715. }
  6716. if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
  6717. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
  6718. tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
  6719. if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
  6720. tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
  6721. tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
  6722. }
  6723. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
  6724. tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
  6725. val = tp->mac_mode;
  6726. } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
  6727. tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
  6728. val = tp->mac_mode;
  6729. } else
  6730. val = 0;
  6731. tw32_f(MAC_MODE, val);
  6732. udelay(40);
  6733. tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
  6734. err = tg3_poll_fw(tp);
  6735. if (err)
  6736. return err;
  6737. tg3_mdio_start(tp);
  6738. if (tg3_flag(tp, PCI_EXPRESS) &&
  6739. tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
  6740. GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
  6741. !tg3_flag(tp, 57765_PLUS)) {
  6742. val = tr32(0x7c00);
  6743. tw32(0x7c00, val | (1 << 25));
  6744. }
  6745. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
  6746. val = tr32(TG3_CPMU_CLCK_ORIDE);
  6747. tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
  6748. }
  6749. /* Reprobe ASF enable state. */
  6750. tg3_flag_clear(tp, ENABLE_ASF);
  6751. tg3_flag_clear(tp, ASF_NEW_HANDSHAKE);
  6752. tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
  6753. if (val == NIC_SRAM_DATA_SIG_MAGIC) {
  6754. u32 nic_cfg;
  6755. tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
  6756. if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
  6757. tg3_flag_set(tp, ENABLE_ASF);
  6758. tp->last_event_jiffies = jiffies;
  6759. if (tg3_flag(tp, 5750_PLUS))
  6760. tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
  6761. }
  6762. }
  6763. return 0;
  6764. }
  6765. static void tg3_get_nstats(struct tg3 *, struct rtnl_link_stats64 *);
  6766. static void tg3_get_estats(struct tg3 *, struct tg3_ethtool_stats *);
  6767. /* tp->lock is held. */
  6768. static int tg3_halt(struct tg3 *tp, int kind, int silent)
  6769. {
  6770. int err;
  6771. tg3_stop_fw(tp);
  6772. tg3_write_sig_pre_reset(tp, kind);
  6773. tg3_abort_hw(tp, silent);
  6774. err = tg3_chip_reset(tp);
  6775. __tg3_set_mac_addr(tp, 0);
  6776. tg3_write_sig_legacy(tp, kind);
  6777. tg3_write_sig_post_reset(tp, kind);
  6778. if (tp->hw_stats) {
  6779. /* Save the stats across chip resets... */
  6780. tg3_get_nstats(tp, &tp->net_stats_prev);
  6781. tg3_get_estats(tp, &tp->estats_prev);
  6782. /* And make sure the next sample is new data */
  6783. memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
  6784. }
  6785. if (err)
  6786. return err;
  6787. return 0;
  6788. }
  6789. static int tg3_set_mac_addr(struct net_device *dev, void *p)
  6790. {
  6791. struct tg3 *tp = netdev_priv(dev);
  6792. struct sockaddr *addr = p;
  6793. int err = 0, skip_mac_1 = 0;
  6794. if (!is_valid_ether_addr(addr->sa_data))
  6795. return -EADDRNOTAVAIL;
  6796. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  6797. if (!netif_running(dev))
  6798. return 0;
  6799. if (tg3_flag(tp, ENABLE_ASF)) {
  6800. u32 addr0_high, addr0_low, addr1_high, addr1_low;
  6801. addr0_high = tr32(MAC_ADDR_0_HIGH);
  6802. addr0_low = tr32(MAC_ADDR_0_LOW);
  6803. addr1_high = tr32(MAC_ADDR_1_HIGH);
  6804. addr1_low = tr32(MAC_ADDR_1_LOW);
  6805. /* Skip MAC addr 1 if ASF is using it. */
  6806. if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
  6807. !(addr1_high == 0 && addr1_low == 0))
  6808. skip_mac_1 = 1;
  6809. }
  6810. spin_lock_bh(&tp->lock);
  6811. __tg3_set_mac_addr(tp, skip_mac_1);
  6812. spin_unlock_bh(&tp->lock);
  6813. return err;
  6814. }
  6815. /* tp->lock is held. */
  6816. static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
  6817. dma_addr_t mapping, u32 maxlen_flags,
  6818. u32 nic_addr)
  6819. {
  6820. tg3_write_mem(tp,
  6821. (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
  6822. ((u64) mapping >> 32));
  6823. tg3_write_mem(tp,
  6824. (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
  6825. ((u64) mapping & 0xffffffff));
  6826. tg3_write_mem(tp,
  6827. (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
  6828. maxlen_flags);
  6829. if (!tg3_flag(tp, 5705_PLUS))
  6830. tg3_write_mem(tp,
  6831. (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
  6832. nic_addr);
  6833. }
  6834. static void tg3_coal_tx_init(struct tg3 *tp, struct ethtool_coalesce *ec)
  6835. {
  6836. int i = 0;
  6837. if (!tg3_flag(tp, ENABLE_TSS)) {
  6838. tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
  6839. tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
  6840. tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
  6841. } else {
  6842. tw32(HOSTCC_TXCOL_TICKS, 0);
  6843. tw32(HOSTCC_TXMAX_FRAMES, 0);
  6844. tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
  6845. for (; i < tp->txq_cnt; i++) {
  6846. u32 reg;
  6847. reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
  6848. tw32(reg, ec->tx_coalesce_usecs);
  6849. reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
  6850. tw32(reg, ec->tx_max_coalesced_frames);
  6851. reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
  6852. tw32(reg, ec->tx_max_coalesced_frames_irq);
  6853. }
  6854. }
  6855. for (; i < tp->irq_max - 1; i++) {
  6856. tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
  6857. tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
  6858. tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
  6859. }
  6860. }
  6861. static void tg3_coal_rx_init(struct tg3 *tp, struct ethtool_coalesce *ec)
  6862. {
  6863. int i = 0;
  6864. u32 limit = tp->rxq_cnt;
  6865. if (!tg3_flag(tp, ENABLE_RSS)) {
  6866. tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
  6867. tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
  6868. tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
  6869. limit--;
  6870. } else {
  6871. tw32(HOSTCC_RXCOL_TICKS, 0);
  6872. tw32(HOSTCC_RXMAX_FRAMES, 0);
  6873. tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
  6874. }
  6875. for (; i < limit; i++) {
  6876. u32 reg;
  6877. reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
  6878. tw32(reg, ec->rx_coalesce_usecs);
  6879. reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
  6880. tw32(reg, ec->rx_max_coalesced_frames);
  6881. reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
  6882. tw32(reg, ec->rx_max_coalesced_frames_irq);
  6883. }
  6884. for (; i < tp->irq_max - 1; i++) {
  6885. tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
  6886. tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
  6887. tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
  6888. }
  6889. }
  6890. static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
  6891. {
  6892. tg3_coal_tx_init(tp, ec);
  6893. tg3_coal_rx_init(tp, ec);
  6894. if (!tg3_flag(tp, 5705_PLUS)) {
  6895. u32 val = ec->stats_block_coalesce_usecs;
  6896. tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
  6897. tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
  6898. if (!netif_carrier_ok(tp->dev))
  6899. val = 0;
  6900. tw32(HOSTCC_STAT_COAL_TICKS, val);
  6901. }
  6902. }
  6903. /* tp->lock is held. */
  6904. static void tg3_rings_reset(struct tg3 *tp)
  6905. {
  6906. int i;
  6907. u32 stblk, txrcb, rxrcb, limit;
  6908. struct tg3_napi *tnapi = &tp->napi[0];
  6909. /* Disable all transmit rings but the first. */
  6910. if (!tg3_flag(tp, 5705_PLUS))
  6911. limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
  6912. else if (tg3_flag(tp, 5717_PLUS))
  6913. limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
  6914. else if (tg3_flag(tp, 57765_CLASS))
  6915. limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
  6916. else
  6917. limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
  6918. for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
  6919. txrcb < limit; txrcb += TG3_BDINFO_SIZE)
  6920. tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
  6921. BDINFO_FLAGS_DISABLED);
  6922. /* Disable all receive return rings but the first. */
  6923. if (tg3_flag(tp, 5717_PLUS))
  6924. limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
  6925. else if (!tg3_flag(tp, 5705_PLUS))
  6926. limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
  6927. else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
  6928. tg3_flag(tp, 57765_CLASS))
  6929. limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
  6930. else
  6931. limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
  6932. for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
  6933. rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
  6934. tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
  6935. BDINFO_FLAGS_DISABLED);
  6936. /* Disable interrupts */
  6937. tw32_mailbox_f(tp->napi[0].int_mbox, 1);
  6938. tp->napi[0].chk_msi_cnt = 0;
  6939. tp->napi[0].last_rx_cons = 0;
  6940. tp->napi[0].last_tx_cons = 0;
  6941. /* Zero mailbox registers. */
  6942. if (tg3_flag(tp, SUPPORT_MSIX)) {
  6943. for (i = 1; i < tp->irq_max; i++) {
  6944. tp->napi[i].tx_prod = 0;
  6945. tp->napi[i].tx_cons = 0;
  6946. if (tg3_flag(tp, ENABLE_TSS))
  6947. tw32_mailbox(tp->napi[i].prodmbox, 0);
  6948. tw32_rx_mbox(tp->napi[i].consmbox, 0);
  6949. tw32_mailbox_f(tp->napi[i].int_mbox, 1);
  6950. tp->napi[i].chk_msi_cnt = 0;
  6951. tp->napi[i].last_rx_cons = 0;
  6952. tp->napi[i].last_tx_cons = 0;
  6953. }
  6954. if (!tg3_flag(tp, ENABLE_TSS))
  6955. tw32_mailbox(tp->napi[0].prodmbox, 0);
  6956. } else {
  6957. tp->napi[0].tx_prod = 0;
  6958. tp->napi[0].tx_cons = 0;
  6959. tw32_mailbox(tp->napi[0].prodmbox, 0);
  6960. tw32_rx_mbox(tp->napi[0].consmbox, 0);
  6961. }
  6962. /* Make sure the NIC-based send BD rings are disabled. */
  6963. if (!tg3_flag(tp, 5705_PLUS)) {
  6964. u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
  6965. for (i = 0; i < 16; i++)
  6966. tw32_tx_mbox(mbox + i * 8, 0);
  6967. }
  6968. txrcb = NIC_SRAM_SEND_RCB;
  6969. rxrcb = NIC_SRAM_RCV_RET_RCB;
  6970. /* Clear status block in ram. */
  6971. memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
  6972. /* Set status block DMA address */
  6973. tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
  6974. ((u64) tnapi->status_mapping >> 32));
  6975. tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
  6976. ((u64) tnapi->status_mapping & 0xffffffff));
  6977. if (tnapi->tx_ring) {
  6978. tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
  6979. (TG3_TX_RING_SIZE <<
  6980. BDINFO_FLAGS_MAXLEN_SHIFT),
  6981. NIC_SRAM_TX_BUFFER_DESC);
  6982. txrcb += TG3_BDINFO_SIZE;
  6983. }
  6984. if (tnapi->rx_rcb) {
  6985. tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
  6986. (tp->rx_ret_ring_mask + 1) <<
  6987. BDINFO_FLAGS_MAXLEN_SHIFT, 0);
  6988. rxrcb += TG3_BDINFO_SIZE;
  6989. }
  6990. stblk = HOSTCC_STATBLCK_RING1;
  6991. for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
  6992. u64 mapping = (u64)tnapi->status_mapping;
  6993. tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
  6994. tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
  6995. /* Clear status block in ram. */
  6996. memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
  6997. if (tnapi->tx_ring) {
  6998. tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
  6999. (TG3_TX_RING_SIZE <<
  7000. BDINFO_FLAGS_MAXLEN_SHIFT),
  7001. NIC_SRAM_TX_BUFFER_DESC);
  7002. txrcb += TG3_BDINFO_SIZE;
  7003. }
  7004. tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
  7005. ((tp->rx_ret_ring_mask + 1) <<
  7006. BDINFO_FLAGS_MAXLEN_SHIFT), 0);
  7007. stblk += 8;
  7008. rxrcb += TG3_BDINFO_SIZE;
  7009. }
  7010. }
  7011. static void tg3_setup_rxbd_thresholds(struct tg3 *tp)
  7012. {
  7013. u32 val, bdcache_maxcnt, host_rep_thresh, nic_rep_thresh;
  7014. if (!tg3_flag(tp, 5750_PLUS) ||
  7015. tg3_flag(tp, 5780_CLASS) ||
  7016. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
  7017. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
  7018. tg3_flag(tp, 57765_PLUS))
  7019. bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5700;
  7020. else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
  7021. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787)
  7022. bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5755;
  7023. else
  7024. bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5906;
  7025. nic_rep_thresh = min(bdcache_maxcnt / 2, tp->rx_std_max_post);
  7026. host_rep_thresh = max_t(u32, tp->rx_pending / 8, 1);
  7027. val = min(nic_rep_thresh, host_rep_thresh);
  7028. tw32(RCVBDI_STD_THRESH, val);
  7029. if (tg3_flag(tp, 57765_PLUS))
  7030. tw32(STD_REPLENISH_LWM, bdcache_maxcnt);
  7031. if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
  7032. return;
  7033. bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5700;
  7034. host_rep_thresh = max_t(u32, tp->rx_jumbo_pending / 8, 1);
  7035. val = min(bdcache_maxcnt / 2, host_rep_thresh);
  7036. tw32(RCVBDI_JUMBO_THRESH, val);
  7037. if (tg3_flag(tp, 57765_PLUS))
  7038. tw32(JMB_REPLENISH_LWM, bdcache_maxcnt);
  7039. }
  7040. static inline u32 calc_crc(unsigned char *buf, int len)
  7041. {
  7042. u32 reg;
  7043. u32 tmp;
  7044. int j, k;
  7045. reg = 0xffffffff;
  7046. for (j = 0; j < len; j++) {
  7047. reg ^= buf[j];
  7048. for (k = 0; k < 8; k++) {
  7049. tmp = reg & 0x01;
  7050. reg >>= 1;
  7051. if (tmp)
  7052. reg ^= 0xedb88320;
  7053. }
  7054. }
  7055. return ~reg;
  7056. }
  7057. static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
  7058. {
  7059. /* accept or reject all multicast frames */
  7060. tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
  7061. tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
  7062. tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
  7063. tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
  7064. }
  7065. static void __tg3_set_rx_mode(struct net_device *dev)
  7066. {
  7067. struct tg3 *tp = netdev_priv(dev);
  7068. u32 rx_mode;
  7069. rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
  7070. RX_MODE_KEEP_VLAN_TAG);
  7071. #if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
  7072. /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
  7073. * flag clear.
  7074. */
  7075. if (!tg3_flag(tp, ENABLE_ASF))
  7076. rx_mode |= RX_MODE_KEEP_VLAN_TAG;
  7077. #endif
  7078. if (dev->flags & IFF_PROMISC) {
  7079. /* Promiscuous mode. */
  7080. rx_mode |= RX_MODE_PROMISC;
  7081. } else if (dev->flags & IFF_ALLMULTI) {
  7082. /* Accept all multicast. */
  7083. tg3_set_multi(tp, 1);
  7084. } else if (netdev_mc_empty(dev)) {
  7085. /* Reject all multicast. */
  7086. tg3_set_multi(tp, 0);
  7087. } else {
  7088. /* Accept one or more multicast(s). */
  7089. struct netdev_hw_addr *ha;
  7090. u32 mc_filter[4] = { 0, };
  7091. u32 regidx;
  7092. u32 bit;
  7093. u32 crc;
  7094. netdev_for_each_mc_addr(ha, dev) {
  7095. crc = calc_crc(ha->addr, ETH_ALEN);
  7096. bit = ~crc & 0x7f;
  7097. regidx = (bit & 0x60) >> 5;
  7098. bit &= 0x1f;
  7099. mc_filter[regidx] |= (1 << bit);
  7100. }
  7101. tw32(MAC_HASH_REG_0, mc_filter[0]);
  7102. tw32(MAC_HASH_REG_1, mc_filter[1]);
  7103. tw32(MAC_HASH_REG_2, mc_filter[2]);
  7104. tw32(MAC_HASH_REG_3, mc_filter[3]);
  7105. }
  7106. if (rx_mode != tp->rx_mode) {
  7107. tp->rx_mode = rx_mode;
  7108. tw32_f(MAC_RX_MODE, rx_mode);
  7109. udelay(10);
  7110. }
  7111. }
  7112. static void tg3_rss_init_dflt_indir_tbl(struct tg3 *tp, u32 qcnt)
  7113. {
  7114. int i;
  7115. for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
  7116. tp->rss_ind_tbl[i] = ethtool_rxfh_indir_default(i, qcnt);
  7117. }
  7118. static void tg3_rss_check_indir_tbl(struct tg3 *tp)
  7119. {
  7120. int i;
  7121. if (!tg3_flag(tp, SUPPORT_MSIX))
  7122. return;
  7123. if (tp->irq_cnt <= 2) {
  7124. memset(&tp->rss_ind_tbl[0], 0, sizeof(tp->rss_ind_tbl));
  7125. return;
  7126. }
  7127. /* Validate table against current IRQ count */
  7128. for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
  7129. if (tp->rss_ind_tbl[i] >= tp->irq_cnt - 1)
  7130. break;
  7131. }
  7132. if (i != TG3_RSS_INDIR_TBL_SIZE)
  7133. tg3_rss_init_dflt_indir_tbl(tp, tp->rxq_cnt);
  7134. }
  7135. static void tg3_rss_write_indir_tbl(struct tg3 *tp)
  7136. {
  7137. int i = 0;
  7138. u32 reg = MAC_RSS_INDIR_TBL_0;
  7139. while (i < TG3_RSS_INDIR_TBL_SIZE) {
  7140. u32 val = tp->rss_ind_tbl[i];
  7141. i++;
  7142. for (; i % 8; i++) {
  7143. val <<= 4;
  7144. val |= tp->rss_ind_tbl[i];
  7145. }
  7146. tw32(reg, val);
  7147. reg += 4;
  7148. }
  7149. }
  7150. /* tp->lock is held. */
  7151. static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
  7152. {
  7153. u32 val, rdmac_mode;
  7154. int i, err, limit;
  7155. struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
  7156. tg3_disable_ints(tp);
  7157. tg3_stop_fw(tp);
  7158. tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
  7159. if (tg3_flag(tp, INIT_COMPLETE))
  7160. tg3_abort_hw(tp, 1);
  7161. /* Enable MAC control of LPI */
  7162. if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
  7163. tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL,
  7164. TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
  7165. TG3_CPMU_EEE_LNKIDL_UART_IDL);
  7166. tw32_f(TG3_CPMU_EEE_CTRL,
  7167. TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
  7168. val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
  7169. TG3_CPMU_EEEMD_LPI_IN_TX |
  7170. TG3_CPMU_EEEMD_LPI_IN_RX |
  7171. TG3_CPMU_EEEMD_EEE_ENABLE;
  7172. if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
  7173. val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
  7174. if (tg3_flag(tp, ENABLE_APE))
  7175. val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
  7176. tw32_f(TG3_CPMU_EEE_MODE, val);
  7177. tw32_f(TG3_CPMU_EEE_DBTMR1,
  7178. TG3_CPMU_DBTMR1_PCIEXIT_2047US |
  7179. TG3_CPMU_DBTMR1_LNKIDLE_2047US);
  7180. tw32_f(TG3_CPMU_EEE_DBTMR2,
  7181. TG3_CPMU_DBTMR2_APE_TX_2047US |
  7182. TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
  7183. }
  7184. if (reset_phy)
  7185. tg3_phy_reset(tp);
  7186. err = tg3_chip_reset(tp);
  7187. if (err)
  7188. return err;
  7189. tg3_write_sig_legacy(tp, RESET_KIND_INIT);
  7190. if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
  7191. val = tr32(TG3_CPMU_CTRL);
  7192. val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
  7193. tw32(TG3_CPMU_CTRL, val);
  7194. val = tr32(TG3_CPMU_LSPD_10MB_CLK);
  7195. val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
  7196. val |= CPMU_LSPD_10MB_MACCLK_6_25;
  7197. tw32(TG3_CPMU_LSPD_10MB_CLK, val);
  7198. val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
  7199. val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
  7200. val |= CPMU_LNK_AWARE_MACCLK_6_25;
  7201. tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
  7202. val = tr32(TG3_CPMU_HST_ACC);
  7203. val &= ~CPMU_HST_ACC_MACCLK_MASK;
  7204. val |= CPMU_HST_ACC_MACCLK_6_25;
  7205. tw32(TG3_CPMU_HST_ACC, val);
  7206. }
  7207. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
  7208. val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
  7209. val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
  7210. PCIE_PWR_MGMT_L1_THRESH_4MS;
  7211. tw32(PCIE_PWR_MGMT_THRESH, val);
  7212. val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
  7213. tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
  7214. tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
  7215. val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
  7216. tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
  7217. }
  7218. if (tg3_flag(tp, L1PLLPD_EN)) {
  7219. u32 grc_mode = tr32(GRC_MODE);
  7220. /* Access the lower 1K of PL PCIE block registers. */
  7221. val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
  7222. tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
  7223. val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
  7224. tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
  7225. val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
  7226. tw32(GRC_MODE, grc_mode);
  7227. }
  7228. if (tg3_flag(tp, 57765_CLASS)) {
  7229. if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
  7230. u32 grc_mode = tr32(GRC_MODE);
  7231. /* Access the lower 1K of PL PCIE block registers. */
  7232. val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
  7233. tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
  7234. val = tr32(TG3_PCIE_TLDLPL_PORT +
  7235. TG3_PCIE_PL_LO_PHYCTL5);
  7236. tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
  7237. val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
  7238. tw32(GRC_MODE, grc_mode);
  7239. }
  7240. if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_57765_AX) {
  7241. u32 grc_mode = tr32(GRC_MODE);
  7242. /* Access the lower 1K of DL PCIE block registers. */
  7243. val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
  7244. tw32(GRC_MODE, val | GRC_MODE_PCIE_DL_SEL);
  7245. val = tr32(TG3_PCIE_TLDLPL_PORT +
  7246. TG3_PCIE_DL_LO_FTSMAX);
  7247. val &= ~TG3_PCIE_DL_LO_FTSMAX_MSK;
  7248. tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_DL_LO_FTSMAX,
  7249. val | TG3_PCIE_DL_LO_FTSMAX_VAL);
  7250. tw32(GRC_MODE, grc_mode);
  7251. }
  7252. val = tr32(TG3_CPMU_LSPD_10MB_CLK);
  7253. val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
  7254. val |= CPMU_LSPD_10MB_MACCLK_6_25;
  7255. tw32(TG3_CPMU_LSPD_10MB_CLK, val);
  7256. }
  7257. /* This works around an issue with Athlon chipsets on
  7258. * B3 tigon3 silicon. This bit has no effect on any
  7259. * other revision. But do not set this on PCI Express
  7260. * chips and don't even touch the clocks if the CPMU is present.
  7261. */
  7262. if (!tg3_flag(tp, CPMU_PRESENT)) {
  7263. if (!tg3_flag(tp, PCI_EXPRESS))
  7264. tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
  7265. tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
  7266. }
  7267. if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
  7268. tg3_flag(tp, PCIX_MODE)) {
  7269. val = tr32(TG3PCI_PCISTATE);
  7270. val |= PCISTATE_RETRY_SAME_DMA;
  7271. tw32(TG3PCI_PCISTATE, val);
  7272. }
  7273. if (tg3_flag(tp, ENABLE_APE)) {
  7274. /* Allow reads and writes to the
  7275. * APE register and memory space.
  7276. */
  7277. val = tr32(TG3PCI_PCISTATE);
  7278. val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
  7279. PCISTATE_ALLOW_APE_SHMEM_WR |
  7280. PCISTATE_ALLOW_APE_PSPACE_WR;
  7281. tw32(TG3PCI_PCISTATE, val);
  7282. }
  7283. if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
  7284. /* Enable some hw fixes. */
  7285. val = tr32(TG3PCI_MSI_DATA);
  7286. val |= (1 << 26) | (1 << 28) | (1 << 29);
  7287. tw32(TG3PCI_MSI_DATA, val);
  7288. }
  7289. /* Descriptor ring init may make accesses to the
  7290. * NIC SRAM area to setup the TX descriptors, so we
  7291. * can only do this after the hardware has been
  7292. * successfully reset.
  7293. */
  7294. err = tg3_init_rings(tp);
  7295. if (err)
  7296. return err;
  7297. if (tg3_flag(tp, 57765_PLUS)) {
  7298. val = tr32(TG3PCI_DMA_RW_CTRL) &
  7299. ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
  7300. if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0)
  7301. val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
  7302. if (!tg3_flag(tp, 57765_CLASS) &&
  7303. GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
  7304. val |= DMA_RWCTRL_TAGGED_STAT_WA;
  7305. tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
  7306. } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
  7307. GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761) {
  7308. /* This value is determined during the probe time DMA
  7309. * engine test, tg3_test_dma.
  7310. */
  7311. tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
  7312. }
  7313. tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
  7314. GRC_MODE_4X_NIC_SEND_RINGS |
  7315. GRC_MODE_NO_TX_PHDR_CSUM |
  7316. GRC_MODE_NO_RX_PHDR_CSUM);
  7317. tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
  7318. /* Pseudo-header checksum is done by hardware logic and not
  7319. * the offload processers, so make the chip do the pseudo-
  7320. * header checksums on receive. For transmit it is more
  7321. * convenient to do the pseudo-header checksum in software
  7322. * as Linux does that on transmit for us in all cases.
  7323. */
  7324. tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
  7325. tw32(GRC_MODE,
  7326. tp->grc_mode |
  7327. (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
  7328. /* Setup the timer prescalar register. Clock is always 66Mhz. */
  7329. val = tr32(GRC_MISC_CFG);
  7330. val &= ~0xff;
  7331. val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
  7332. tw32(GRC_MISC_CFG, val);
  7333. /* Initialize MBUF/DESC pool. */
  7334. if (tg3_flag(tp, 5750_PLUS)) {
  7335. /* Do nothing. */
  7336. } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
  7337. tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
  7338. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
  7339. tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
  7340. else
  7341. tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
  7342. tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
  7343. tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
  7344. } else if (tg3_flag(tp, TSO_CAPABLE)) {
  7345. int fw_len;
  7346. fw_len = tp->fw_len;
  7347. fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
  7348. tw32(BUFMGR_MB_POOL_ADDR,
  7349. NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
  7350. tw32(BUFMGR_MB_POOL_SIZE,
  7351. NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
  7352. }
  7353. if (tp->dev->mtu <= ETH_DATA_LEN) {
  7354. tw32(BUFMGR_MB_RDMA_LOW_WATER,
  7355. tp->bufmgr_config.mbuf_read_dma_low_water);
  7356. tw32(BUFMGR_MB_MACRX_LOW_WATER,
  7357. tp->bufmgr_config.mbuf_mac_rx_low_water);
  7358. tw32(BUFMGR_MB_HIGH_WATER,
  7359. tp->bufmgr_config.mbuf_high_water);
  7360. } else {
  7361. tw32(BUFMGR_MB_RDMA_LOW_WATER,
  7362. tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
  7363. tw32(BUFMGR_MB_MACRX_LOW_WATER,
  7364. tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
  7365. tw32(BUFMGR_MB_HIGH_WATER,
  7366. tp->bufmgr_config.mbuf_high_water_jumbo);
  7367. }
  7368. tw32(BUFMGR_DMA_LOW_WATER,
  7369. tp->bufmgr_config.dma_low_water);
  7370. tw32(BUFMGR_DMA_HIGH_WATER,
  7371. tp->bufmgr_config.dma_high_water);
  7372. val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
  7373. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
  7374. val |= BUFMGR_MODE_NO_TX_UNDERRUN;
  7375. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
  7376. tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
  7377. tp->pci_chip_rev_id == CHIPREV_ID_5720_A0)
  7378. val |= BUFMGR_MODE_MBLOW_ATTN_ENAB;
  7379. tw32(BUFMGR_MODE, val);
  7380. for (i = 0; i < 2000; i++) {
  7381. if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
  7382. break;
  7383. udelay(10);
  7384. }
  7385. if (i >= 2000) {
  7386. netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
  7387. return -ENODEV;
  7388. }
  7389. if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
  7390. tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
  7391. tg3_setup_rxbd_thresholds(tp);
  7392. /* Initialize TG3_BDINFO's at:
  7393. * RCVDBDI_STD_BD: standard eth size rx ring
  7394. * RCVDBDI_JUMBO_BD: jumbo frame rx ring
  7395. * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
  7396. *
  7397. * like so:
  7398. * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
  7399. * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
  7400. * ring attribute flags
  7401. * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
  7402. *
  7403. * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
  7404. * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
  7405. *
  7406. * The size of each ring is fixed in the firmware, but the location is
  7407. * configurable.
  7408. */
  7409. tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
  7410. ((u64) tpr->rx_std_mapping >> 32));
  7411. tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
  7412. ((u64) tpr->rx_std_mapping & 0xffffffff));
  7413. if (!tg3_flag(tp, 5717_PLUS))
  7414. tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
  7415. NIC_SRAM_RX_BUFFER_DESC);
  7416. /* Disable the mini ring */
  7417. if (!tg3_flag(tp, 5705_PLUS))
  7418. tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
  7419. BDINFO_FLAGS_DISABLED);
  7420. /* Program the jumbo buffer descriptor ring control
  7421. * blocks on those devices that have them.
  7422. */
  7423. if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
  7424. (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))) {
  7425. if (tg3_flag(tp, JUMBO_RING_ENABLE)) {
  7426. tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
  7427. ((u64) tpr->rx_jmb_mapping >> 32));
  7428. tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
  7429. ((u64) tpr->rx_jmb_mapping & 0xffffffff));
  7430. val = TG3_RX_JMB_RING_SIZE(tp) <<
  7431. BDINFO_FLAGS_MAXLEN_SHIFT;
  7432. tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
  7433. val | BDINFO_FLAGS_USE_EXT_RECV);
  7434. if (!tg3_flag(tp, USE_JUMBO_BDFLAG) ||
  7435. tg3_flag(tp, 57765_CLASS))
  7436. tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
  7437. NIC_SRAM_RX_JUMBO_BUFFER_DESC);
  7438. } else {
  7439. tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
  7440. BDINFO_FLAGS_DISABLED);
  7441. }
  7442. if (tg3_flag(tp, 57765_PLUS)) {
  7443. val = TG3_RX_STD_RING_SIZE(tp);
  7444. val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
  7445. val |= (TG3_RX_STD_DMA_SZ << 2);
  7446. } else
  7447. val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
  7448. } else
  7449. val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
  7450. tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
  7451. tpr->rx_std_prod_idx = tp->rx_pending;
  7452. tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
  7453. tpr->rx_jmb_prod_idx =
  7454. tg3_flag(tp, JUMBO_RING_ENABLE) ? tp->rx_jumbo_pending : 0;
  7455. tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
  7456. tg3_rings_reset(tp);
  7457. /* Initialize MAC address and backoff seed. */
  7458. __tg3_set_mac_addr(tp, 0);
  7459. /* MTU + ethernet header + FCS + optional VLAN tag */
  7460. tw32(MAC_RX_MTU_SIZE,
  7461. tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
  7462. /* The slot time is changed by tg3_setup_phy if we
  7463. * run at gigabit with half duplex.
  7464. */
  7465. val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
  7466. (6 << TX_LENGTHS_IPG_SHIFT) |
  7467. (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
  7468. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
  7469. val |= tr32(MAC_TX_LENGTHS) &
  7470. (TX_LENGTHS_JMB_FRM_LEN_MSK |
  7471. TX_LENGTHS_CNT_DWN_VAL_MSK);
  7472. tw32(MAC_TX_LENGTHS, val);
  7473. /* Receive rules. */
  7474. tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
  7475. tw32(RCVLPC_CONFIG, 0x0181);
  7476. /* Calculate RDMAC_MODE setting early, we need it to determine
  7477. * the RCVLPC_STATE_ENABLE mask.
  7478. */
  7479. rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
  7480. RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
  7481. RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
  7482. RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
  7483. RDMAC_MODE_LNGREAD_ENAB);
  7484. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
  7485. rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
  7486. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
  7487. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
  7488. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
  7489. rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
  7490. RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
  7491. RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
  7492. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
  7493. tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
  7494. if (tg3_flag(tp, TSO_CAPABLE) &&
  7495. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
  7496. rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
  7497. } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
  7498. !tg3_flag(tp, IS_5788)) {
  7499. rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
  7500. }
  7501. }
  7502. if (tg3_flag(tp, PCI_EXPRESS))
  7503. rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
  7504. if (tg3_flag(tp, HW_TSO_1) ||
  7505. tg3_flag(tp, HW_TSO_2) ||
  7506. tg3_flag(tp, HW_TSO_3))
  7507. rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
  7508. if (tg3_flag(tp, 57765_PLUS) ||
  7509. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
  7510. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
  7511. rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
  7512. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
  7513. rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
  7514. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
  7515. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
  7516. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
  7517. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
  7518. tg3_flag(tp, 57765_PLUS)) {
  7519. val = tr32(TG3_RDMA_RSRVCTRL_REG);
  7520. if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0) {
  7521. val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
  7522. TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
  7523. TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
  7524. val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
  7525. TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
  7526. TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
  7527. }
  7528. tw32(TG3_RDMA_RSRVCTRL_REG,
  7529. val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
  7530. }
  7531. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
  7532. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
  7533. val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
  7534. tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val |
  7535. TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
  7536. TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
  7537. }
  7538. /* Receive/send statistics. */
  7539. if (tg3_flag(tp, 5750_PLUS)) {
  7540. val = tr32(RCVLPC_STATS_ENABLE);
  7541. val &= ~RCVLPC_STATSENAB_DACK_FIX;
  7542. tw32(RCVLPC_STATS_ENABLE, val);
  7543. } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
  7544. tg3_flag(tp, TSO_CAPABLE)) {
  7545. val = tr32(RCVLPC_STATS_ENABLE);
  7546. val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
  7547. tw32(RCVLPC_STATS_ENABLE, val);
  7548. } else {
  7549. tw32(RCVLPC_STATS_ENABLE, 0xffffff);
  7550. }
  7551. tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
  7552. tw32(SNDDATAI_STATSENAB, 0xffffff);
  7553. tw32(SNDDATAI_STATSCTRL,
  7554. (SNDDATAI_SCTRL_ENABLE |
  7555. SNDDATAI_SCTRL_FASTUPD));
  7556. /* Setup host coalescing engine. */
  7557. tw32(HOSTCC_MODE, 0);
  7558. for (i = 0; i < 2000; i++) {
  7559. if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
  7560. break;
  7561. udelay(10);
  7562. }
  7563. __tg3_set_coalesce(tp, &tp->coal);
  7564. if (!tg3_flag(tp, 5705_PLUS)) {
  7565. /* Status/statistics block address. See tg3_timer,
  7566. * the tg3_periodic_fetch_stats call there, and
  7567. * tg3_get_stats to see how this works for 5705/5750 chips.
  7568. */
  7569. tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
  7570. ((u64) tp->stats_mapping >> 32));
  7571. tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
  7572. ((u64) tp->stats_mapping & 0xffffffff));
  7573. tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
  7574. tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
  7575. /* Clear statistics and status block memory areas */
  7576. for (i = NIC_SRAM_STATS_BLK;
  7577. i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
  7578. i += sizeof(u32)) {
  7579. tg3_write_mem(tp, i, 0);
  7580. udelay(40);
  7581. }
  7582. }
  7583. tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
  7584. tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
  7585. tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
  7586. if (!tg3_flag(tp, 5705_PLUS))
  7587. tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
  7588. if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
  7589. tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
  7590. /* reset to prevent losing 1st rx packet intermittently */
  7591. tw32_f(MAC_RX_MODE, RX_MODE_RESET);
  7592. udelay(10);
  7593. }
  7594. tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
  7595. MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE |
  7596. MAC_MODE_FHDE_ENABLE;
  7597. if (tg3_flag(tp, ENABLE_APE))
  7598. tp->mac_mode |= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
  7599. if (!tg3_flag(tp, 5705_PLUS) &&
  7600. !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
  7601. GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
  7602. tp->mac_mode |= MAC_MODE_LINK_POLARITY;
  7603. tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
  7604. udelay(40);
  7605. /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
  7606. * If TG3_FLAG_IS_NIC is zero, we should read the
  7607. * register to preserve the GPIO settings for LOMs. The GPIOs,
  7608. * whether used as inputs or outputs, are set by boot code after
  7609. * reset.
  7610. */
  7611. if (!tg3_flag(tp, IS_NIC)) {
  7612. u32 gpio_mask;
  7613. gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
  7614. GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
  7615. GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
  7616. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
  7617. gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
  7618. GRC_LCLCTRL_GPIO_OUTPUT3;
  7619. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
  7620. gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
  7621. tp->grc_local_ctrl &= ~gpio_mask;
  7622. tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
  7623. /* GPIO1 must be driven high for eeprom write protect */
  7624. if (tg3_flag(tp, EEPROM_WRITE_PROT))
  7625. tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
  7626. GRC_LCLCTRL_GPIO_OUTPUT1);
  7627. }
  7628. tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
  7629. udelay(100);
  7630. if (tg3_flag(tp, USING_MSIX)) {
  7631. val = tr32(MSGINT_MODE);
  7632. val |= MSGINT_MODE_ENABLE;
  7633. if (tp->irq_cnt > 1)
  7634. val |= MSGINT_MODE_MULTIVEC_EN;
  7635. if (!tg3_flag(tp, 1SHOT_MSI))
  7636. val |= MSGINT_MODE_ONE_SHOT_DISABLE;
  7637. tw32(MSGINT_MODE, val);
  7638. }
  7639. if (!tg3_flag(tp, 5705_PLUS)) {
  7640. tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
  7641. udelay(40);
  7642. }
  7643. val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
  7644. WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
  7645. WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
  7646. WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
  7647. WDMAC_MODE_LNGREAD_ENAB);
  7648. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
  7649. tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
  7650. if (tg3_flag(tp, TSO_CAPABLE) &&
  7651. (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
  7652. tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
  7653. /* nothing */
  7654. } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
  7655. !tg3_flag(tp, IS_5788)) {
  7656. val |= WDMAC_MODE_RX_ACCEL;
  7657. }
  7658. }
  7659. /* Enable host coalescing bug fix */
  7660. if (tg3_flag(tp, 5755_PLUS))
  7661. val |= WDMAC_MODE_STATUS_TAG_FIX;
  7662. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
  7663. val |= WDMAC_MODE_BURST_ALL_DATA;
  7664. tw32_f(WDMAC_MODE, val);
  7665. udelay(40);
  7666. if (tg3_flag(tp, PCIX_MODE)) {
  7667. u16 pcix_cmd;
  7668. pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
  7669. &pcix_cmd);
  7670. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
  7671. pcix_cmd &= ~PCI_X_CMD_MAX_READ;
  7672. pcix_cmd |= PCI_X_CMD_READ_2K;
  7673. } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
  7674. pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
  7675. pcix_cmd |= PCI_X_CMD_READ_2K;
  7676. }
  7677. pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
  7678. pcix_cmd);
  7679. }
  7680. tw32_f(RDMAC_MODE, rdmac_mode);
  7681. udelay(40);
  7682. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719) {
  7683. for (i = 0; i < TG3_NUM_RDMA_CHANNELS; i++) {
  7684. if (tr32(TG3_RDMA_LENGTH + (i << 2)) > TG3_MAX_MTU(tp))
  7685. break;
  7686. }
  7687. if (i < TG3_NUM_RDMA_CHANNELS) {
  7688. val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
  7689. val |= TG3_LSO_RD_DMA_TX_LENGTH_WA;
  7690. tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val);
  7691. tg3_flag_set(tp, 5719_RDMA_BUG);
  7692. }
  7693. }
  7694. tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
  7695. if (!tg3_flag(tp, 5705_PLUS))
  7696. tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
  7697. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
  7698. tw32(SNDDATAC_MODE,
  7699. SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
  7700. else
  7701. tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
  7702. tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
  7703. tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
  7704. val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
  7705. if (tg3_flag(tp, LRG_PROD_RING_CAP))
  7706. val |= RCVDBDI_MODE_LRG_RING_SZ;
  7707. tw32(RCVDBDI_MODE, val);
  7708. tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
  7709. if (tg3_flag(tp, HW_TSO_1) ||
  7710. tg3_flag(tp, HW_TSO_2) ||
  7711. tg3_flag(tp, HW_TSO_3))
  7712. tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
  7713. val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
  7714. if (tg3_flag(tp, ENABLE_TSS))
  7715. val |= SNDBDI_MODE_MULTI_TXQ_EN;
  7716. tw32(SNDBDI_MODE, val);
  7717. tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
  7718. if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
  7719. err = tg3_load_5701_a0_firmware_fix(tp);
  7720. if (err)
  7721. return err;
  7722. }
  7723. if (tg3_flag(tp, TSO_CAPABLE)) {
  7724. err = tg3_load_tso_firmware(tp);
  7725. if (err)
  7726. return err;
  7727. }
  7728. tp->tx_mode = TX_MODE_ENABLE;
  7729. if (tg3_flag(tp, 5755_PLUS) ||
  7730. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
  7731. tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
  7732. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
  7733. val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
  7734. tp->tx_mode &= ~val;
  7735. tp->tx_mode |= tr32(MAC_TX_MODE) & val;
  7736. }
  7737. tw32_f(MAC_TX_MODE, tp->tx_mode);
  7738. udelay(100);
  7739. if (tg3_flag(tp, ENABLE_RSS)) {
  7740. tg3_rss_write_indir_tbl(tp);
  7741. /* Setup the "secret" hash key. */
  7742. tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
  7743. tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
  7744. tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
  7745. tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
  7746. tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
  7747. tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
  7748. tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
  7749. tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
  7750. tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
  7751. tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
  7752. }
  7753. tp->rx_mode = RX_MODE_ENABLE;
  7754. if (tg3_flag(tp, 5755_PLUS))
  7755. tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
  7756. if (tg3_flag(tp, ENABLE_RSS))
  7757. tp->rx_mode |= RX_MODE_RSS_ENABLE |
  7758. RX_MODE_RSS_ITBL_HASH_BITS_7 |
  7759. RX_MODE_RSS_IPV6_HASH_EN |
  7760. RX_MODE_RSS_TCP_IPV6_HASH_EN |
  7761. RX_MODE_RSS_IPV4_HASH_EN |
  7762. RX_MODE_RSS_TCP_IPV4_HASH_EN;
  7763. tw32_f(MAC_RX_MODE, tp->rx_mode);
  7764. udelay(10);
  7765. tw32(MAC_LED_CTRL, tp->led_ctrl);
  7766. tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
  7767. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
  7768. tw32_f(MAC_RX_MODE, RX_MODE_RESET);
  7769. udelay(10);
  7770. }
  7771. tw32_f(MAC_RX_MODE, tp->rx_mode);
  7772. udelay(10);
  7773. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
  7774. if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
  7775. !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
  7776. /* Set drive transmission level to 1.2V */
  7777. /* only if the signal pre-emphasis bit is not set */
  7778. val = tr32(MAC_SERDES_CFG);
  7779. val &= 0xfffff000;
  7780. val |= 0x880;
  7781. tw32(MAC_SERDES_CFG, val);
  7782. }
  7783. if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
  7784. tw32(MAC_SERDES_CFG, 0x616000);
  7785. }
  7786. /* Prevent chip from dropping frames when flow control
  7787. * is enabled.
  7788. */
  7789. if (tg3_flag(tp, 57765_CLASS))
  7790. val = 1;
  7791. else
  7792. val = 2;
  7793. tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
  7794. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
  7795. (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
  7796. /* Use hardware link auto-negotiation */
  7797. tg3_flag_set(tp, HW_AUTONEG);
  7798. }
  7799. if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
  7800. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
  7801. u32 tmp;
  7802. tmp = tr32(SERDES_RX_CTRL);
  7803. tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
  7804. tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
  7805. tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
  7806. tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
  7807. }
  7808. if (!tg3_flag(tp, USE_PHYLIB)) {
  7809. if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
  7810. tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
  7811. err = tg3_setup_phy(tp, 0);
  7812. if (err)
  7813. return err;
  7814. if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
  7815. !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
  7816. u32 tmp;
  7817. /* Clear CRC stats. */
  7818. if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
  7819. tg3_writephy(tp, MII_TG3_TEST1,
  7820. tmp | MII_TG3_TEST1_CRC_EN);
  7821. tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
  7822. }
  7823. }
  7824. }
  7825. __tg3_set_rx_mode(tp->dev);
  7826. /* Initialize receive rules. */
  7827. tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
  7828. tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
  7829. tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
  7830. tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
  7831. if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS))
  7832. limit = 8;
  7833. else
  7834. limit = 16;
  7835. if (tg3_flag(tp, ENABLE_ASF))
  7836. limit -= 4;
  7837. switch (limit) {
  7838. case 16:
  7839. tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
  7840. case 15:
  7841. tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
  7842. case 14:
  7843. tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
  7844. case 13:
  7845. tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
  7846. case 12:
  7847. tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
  7848. case 11:
  7849. tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
  7850. case 10:
  7851. tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
  7852. case 9:
  7853. tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
  7854. case 8:
  7855. tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
  7856. case 7:
  7857. tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
  7858. case 6:
  7859. tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
  7860. case 5:
  7861. tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
  7862. case 4:
  7863. /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
  7864. case 3:
  7865. /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
  7866. case 2:
  7867. case 1:
  7868. default:
  7869. break;
  7870. }
  7871. if (tg3_flag(tp, ENABLE_APE))
  7872. /* Write our heartbeat update interval to APE. */
  7873. tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
  7874. APE_HOST_HEARTBEAT_INT_DISABLE);
  7875. tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
  7876. return 0;
  7877. }
  7878. /* Called at device open time to get the chip ready for
  7879. * packet processing. Invoked with tp->lock held.
  7880. */
  7881. static int tg3_init_hw(struct tg3 *tp, int reset_phy)
  7882. {
  7883. tg3_switch_clocks(tp);
  7884. tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
  7885. return tg3_reset_hw(tp, reset_phy);
  7886. }
  7887. static void tg3_sd_scan_scratchpad(struct tg3 *tp, struct tg3_ocir *ocir)
  7888. {
  7889. int i;
  7890. for (i = 0; i < TG3_SD_NUM_RECS; i++, ocir++) {
  7891. u32 off = i * TG3_OCIR_LEN, len = TG3_OCIR_LEN;
  7892. tg3_ape_scratchpad_read(tp, (u32 *) ocir, off, len);
  7893. off += len;
  7894. if (ocir->signature != TG3_OCIR_SIG_MAGIC ||
  7895. !(ocir->version_flags & TG3_OCIR_FLAG_ACTIVE))
  7896. memset(ocir, 0, TG3_OCIR_LEN);
  7897. }
  7898. }
  7899. /* sysfs attributes for hwmon */
  7900. static ssize_t tg3_show_temp(struct device *dev,
  7901. struct device_attribute *devattr, char *buf)
  7902. {
  7903. struct pci_dev *pdev = to_pci_dev(dev);
  7904. struct net_device *netdev = pci_get_drvdata(pdev);
  7905. struct tg3 *tp = netdev_priv(netdev);
  7906. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  7907. u32 temperature;
  7908. spin_lock_bh(&tp->lock);
  7909. tg3_ape_scratchpad_read(tp, &temperature, attr->index,
  7910. sizeof(temperature));
  7911. spin_unlock_bh(&tp->lock);
  7912. return sprintf(buf, "%u\n", temperature);
  7913. }
  7914. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, tg3_show_temp, NULL,
  7915. TG3_TEMP_SENSOR_OFFSET);
  7916. static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, tg3_show_temp, NULL,
  7917. TG3_TEMP_CAUTION_OFFSET);
  7918. static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, tg3_show_temp, NULL,
  7919. TG3_TEMP_MAX_OFFSET);
  7920. static struct attribute *tg3_attributes[] = {
  7921. &sensor_dev_attr_temp1_input.dev_attr.attr,
  7922. &sensor_dev_attr_temp1_crit.dev_attr.attr,
  7923. &sensor_dev_attr_temp1_max.dev_attr.attr,
  7924. NULL
  7925. };
  7926. static const struct attribute_group tg3_group = {
  7927. .attrs = tg3_attributes,
  7928. };
  7929. static void tg3_hwmon_close(struct tg3 *tp)
  7930. {
  7931. if (tp->hwmon_dev) {
  7932. hwmon_device_unregister(tp->hwmon_dev);
  7933. tp->hwmon_dev = NULL;
  7934. sysfs_remove_group(&tp->pdev->dev.kobj, &tg3_group);
  7935. }
  7936. }
  7937. static void tg3_hwmon_open(struct tg3 *tp)
  7938. {
  7939. int i, err;
  7940. u32 size = 0;
  7941. struct pci_dev *pdev = tp->pdev;
  7942. struct tg3_ocir ocirs[TG3_SD_NUM_RECS];
  7943. tg3_sd_scan_scratchpad(tp, ocirs);
  7944. for (i = 0; i < TG3_SD_NUM_RECS; i++) {
  7945. if (!ocirs[i].src_data_length)
  7946. continue;
  7947. size += ocirs[i].src_hdr_length;
  7948. size += ocirs[i].src_data_length;
  7949. }
  7950. if (!size)
  7951. return;
  7952. /* Register hwmon sysfs hooks */
  7953. err = sysfs_create_group(&pdev->dev.kobj, &tg3_group);
  7954. if (err) {
  7955. dev_err(&pdev->dev, "Cannot create sysfs group, aborting\n");
  7956. return;
  7957. }
  7958. tp->hwmon_dev = hwmon_device_register(&pdev->dev);
  7959. if (IS_ERR(tp->hwmon_dev)) {
  7960. tp->hwmon_dev = NULL;
  7961. dev_err(&pdev->dev, "Cannot register hwmon device, aborting\n");
  7962. sysfs_remove_group(&pdev->dev.kobj, &tg3_group);
  7963. }
  7964. }
  7965. #define TG3_STAT_ADD32(PSTAT, REG) \
  7966. do { u32 __val = tr32(REG); \
  7967. (PSTAT)->low += __val; \
  7968. if ((PSTAT)->low < __val) \
  7969. (PSTAT)->high += 1; \
  7970. } while (0)
  7971. static void tg3_periodic_fetch_stats(struct tg3 *tp)
  7972. {
  7973. struct tg3_hw_stats *sp = tp->hw_stats;
  7974. if (!netif_carrier_ok(tp->dev))
  7975. return;
  7976. TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
  7977. TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
  7978. TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
  7979. TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
  7980. TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
  7981. TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
  7982. TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
  7983. TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
  7984. TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
  7985. TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
  7986. TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
  7987. TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
  7988. TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
  7989. if (unlikely(tg3_flag(tp, 5719_RDMA_BUG) &&
  7990. (sp->tx_ucast_packets.low + sp->tx_mcast_packets.low +
  7991. sp->tx_bcast_packets.low) > TG3_NUM_RDMA_CHANNELS)) {
  7992. u32 val;
  7993. val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
  7994. val &= ~TG3_LSO_RD_DMA_TX_LENGTH_WA;
  7995. tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val);
  7996. tg3_flag_clear(tp, 5719_RDMA_BUG);
  7997. }
  7998. TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
  7999. TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
  8000. TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
  8001. TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
  8002. TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
  8003. TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
  8004. TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
  8005. TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
  8006. TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
  8007. TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
  8008. TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
  8009. TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
  8010. TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
  8011. TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
  8012. TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
  8013. if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
  8014. tp->pci_chip_rev_id != CHIPREV_ID_5719_A0 &&
  8015. tp->pci_chip_rev_id != CHIPREV_ID_5720_A0) {
  8016. TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
  8017. } else {
  8018. u32 val = tr32(HOSTCC_FLOW_ATTN);
  8019. val = (val & HOSTCC_FLOW_ATTN_MBUF_LWM) ? 1 : 0;
  8020. if (val) {
  8021. tw32(HOSTCC_FLOW_ATTN, HOSTCC_FLOW_ATTN_MBUF_LWM);
  8022. sp->rx_discards.low += val;
  8023. if (sp->rx_discards.low < val)
  8024. sp->rx_discards.high += 1;
  8025. }
  8026. sp->mbuf_lwm_thresh_hit = sp->rx_discards;
  8027. }
  8028. TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
  8029. }
  8030. static void tg3_chk_missed_msi(struct tg3 *tp)
  8031. {
  8032. u32 i;
  8033. for (i = 0; i < tp->irq_cnt; i++) {
  8034. struct tg3_napi *tnapi = &tp->napi[i];
  8035. if (tg3_has_work(tnapi)) {
  8036. if (tnapi->last_rx_cons == tnapi->rx_rcb_ptr &&
  8037. tnapi->last_tx_cons == tnapi->tx_cons) {
  8038. if (tnapi->chk_msi_cnt < 1) {
  8039. tnapi->chk_msi_cnt++;
  8040. return;
  8041. }
  8042. tg3_msi(0, tnapi);
  8043. }
  8044. }
  8045. tnapi->chk_msi_cnt = 0;
  8046. tnapi->last_rx_cons = tnapi->rx_rcb_ptr;
  8047. tnapi->last_tx_cons = tnapi->tx_cons;
  8048. }
  8049. }
  8050. static void tg3_timer(unsigned long __opaque)
  8051. {
  8052. struct tg3 *tp = (struct tg3 *) __opaque;
  8053. if (tp->irq_sync || tg3_flag(tp, RESET_TASK_PENDING))
  8054. goto restart_timer;
  8055. spin_lock(&tp->lock);
  8056. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
  8057. tg3_flag(tp, 57765_CLASS))
  8058. tg3_chk_missed_msi(tp);
  8059. if (!tg3_flag(tp, TAGGED_STATUS)) {
  8060. /* All of this garbage is because when using non-tagged
  8061. * IRQ status the mailbox/status_block protocol the chip
  8062. * uses with the cpu is race prone.
  8063. */
  8064. if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
  8065. tw32(GRC_LOCAL_CTRL,
  8066. tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
  8067. } else {
  8068. tw32(HOSTCC_MODE, tp->coalesce_mode |
  8069. HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
  8070. }
  8071. if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
  8072. spin_unlock(&tp->lock);
  8073. tg3_reset_task_schedule(tp);
  8074. goto restart_timer;
  8075. }
  8076. }
  8077. /* This part only runs once per second. */
  8078. if (!--tp->timer_counter) {
  8079. if (tg3_flag(tp, 5705_PLUS))
  8080. tg3_periodic_fetch_stats(tp);
  8081. if (tp->setlpicnt && !--tp->setlpicnt)
  8082. tg3_phy_eee_enable(tp);
  8083. if (tg3_flag(tp, USE_LINKCHG_REG)) {
  8084. u32 mac_stat;
  8085. int phy_event;
  8086. mac_stat = tr32(MAC_STATUS);
  8087. phy_event = 0;
  8088. if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
  8089. if (mac_stat & MAC_STATUS_MI_INTERRUPT)
  8090. phy_event = 1;
  8091. } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
  8092. phy_event = 1;
  8093. if (phy_event)
  8094. tg3_setup_phy(tp, 0);
  8095. } else if (tg3_flag(tp, POLL_SERDES)) {
  8096. u32 mac_stat = tr32(MAC_STATUS);
  8097. int need_setup = 0;
  8098. if (netif_carrier_ok(tp->dev) &&
  8099. (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
  8100. need_setup = 1;
  8101. }
  8102. if (!netif_carrier_ok(tp->dev) &&
  8103. (mac_stat & (MAC_STATUS_PCS_SYNCED |
  8104. MAC_STATUS_SIGNAL_DET))) {
  8105. need_setup = 1;
  8106. }
  8107. if (need_setup) {
  8108. if (!tp->serdes_counter) {
  8109. tw32_f(MAC_MODE,
  8110. (tp->mac_mode &
  8111. ~MAC_MODE_PORT_MODE_MASK));
  8112. udelay(40);
  8113. tw32_f(MAC_MODE, tp->mac_mode);
  8114. udelay(40);
  8115. }
  8116. tg3_setup_phy(tp, 0);
  8117. }
  8118. } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
  8119. tg3_flag(tp, 5780_CLASS)) {
  8120. tg3_serdes_parallel_detect(tp);
  8121. }
  8122. tp->timer_counter = tp->timer_multiplier;
  8123. }
  8124. /* Heartbeat is only sent once every 2 seconds.
  8125. *
  8126. * The heartbeat is to tell the ASF firmware that the host
  8127. * driver is still alive. In the event that the OS crashes,
  8128. * ASF needs to reset the hardware to free up the FIFO space
  8129. * that may be filled with rx packets destined for the host.
  8130. * If the FIFO is full, ASF will no longer function properly.
  8131. *
  8132. * Unintended resets have been reported on real time kernels
  8133. * where the timer doesn't run on time. Netpoll will also have
  8134. * same problem.
  8135. *
  8136. * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
  8137. * to check the ring condition when the heartbeat is expiring
  8138. * before doing the reset. This will prevent most unintended
  8139. * resets.
  8140. */
  8141. if (!--tp->asf_counter) {
  8142. if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
  8143. tg3_wait_for_event_ack(tp);
  8144. tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
  8145. FWCMD_NICDRV_ALIVE3);
  8146. tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
  8147. tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
  8148. TG3_FW_UPDATE_TIMEOUT_SEC);
  8149. tg3_generate_fw_event(tp);
  8150. }
  8151. tp->asf_counter = tp->asf_multiplier;
  8152. }
  8153. spin_unlock(&tp->lock);
  8154. restart_timer:
  8155. tp->timer.expires = jiffies + tp->timer_offset;
  8156. add_timer(&tp->timer);
  8157. }
  8158. static void __devinit tg3_timer_init(struct tg3 *tp)
  8159. {
  8160. if (tg3_flag(tp, TAGGED_STATUS) &&
  8161. GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
  8162. !tg3_flag(tp, 57765_CLASS))
  8163. tp->timer_offset = HZ;
  8164. else
  8165. tp->timer_offset = HZ / 10;
  8166. BUG_ON(tp->timer_offset > HZ);
  8167. tp->timer_multiplier = (HZ / tp->timer_offset);
  8168. tp->asf_multiplier = (HZ / tp->timer_offset) *
  8169. TG3_FW_UPDATE_FREQ_SEC;
  8170. init_timer(&tp->timer);
  8171. tp->timer.data = (unsigned long) tp;
  8172. tp->timer.function = tg3_timer;
  8173. }
  8174. static void tg3_timer_start(struct tg3 *tp)
  8175. {
  8176. tp->asf_counter = tp->asf_multiplier;
  8177. tp->timer_counter = tp->timer_multiplier;
  8178. tp->timer.expires = jiffies + tp->timer_offset;
  8179. add_timer(&tp->timer);
  8180. }
  8181. static void tg3_timer_stop(struct tg3 *tp)
  8182. {
  8183. del_timer_sync(&tp->timer);
  8184. }
  8185. /* Restart hardware after configuration changes, self-test, etc.
  8186. * Invoked with tp->lock held.
  8187. */
  8188. static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
  8189. __releases(tp->lock)
  8190. __acquires(tp->lock)
  8191. {
  8192. int err;
  8193. err = tg3_init_hw(tp, reset_phy);
  8194. if (err) {
  8195. netdev_err(tp->dev,
  8196. "Failed to re-initialize device, aborting\n");
  8197. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  8198. tg3_full_unlock(tp);
  8199. tg3_timer_stop(tp);
  8200. tp->irq_sync = 0;
  8201. tg3_napi_enable(tp);
  8202. dev_close(tp->dev);
  8203. tg3_full_lock(tp, 0);
  8204. }
  8205. return err;
  8206. }
  8207. static void tg3_reset_task(struct work_struct *work)
  8208. {
  8209. struct tg3 *tp = container_of(work, struct tg3, reset_task);
  8210. int err;
  8211. tg3_full_lock(tp, 0);
  8212. if (!netif_running(tp->dev)) {
  8213. tg3_flag_clear(tp, RESET_TASK_PENDING);
  8214. tg3_full_unlock(tp);
  8215. return;
  8216. }
  8217. tg3_full_unlock(tp);
  8218. tg3_phy_stop(tp);
  8219. tg3_netif_stop(tp);
  8220. tg3_full_lock(tp, 1);
  8221. if (tg3_flag(tp, TX_RECOVERY_PENDING)) {
  8222. tp->write32_tx_mbox = tg3_write32_tx_mbox;
  8223. tp->write32_rx_mbox = tg3_write_flush_reg32;
  8224. tg3_flag_set(tp, MBOX_WRITE_REORDER);
  8225. tg3_flag_clear(tp, TX_RECOVERY_PENDING);
  8226. }
  8227. tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
  8228. err = tg3_init_hw(tp, 1);
  8229. if (err)
  8230. goto out;
  8231. tg3_netif_start(tp);
  8232. out:
  8233. tg3_full_unlock(tp);
  8234. if (!err)
  8235. tg3_phy_start(tp);
  8236. tg3_flag_clear(tp, RESET_TASK_PENDING);
  8237. }
  8238. static int tg3_request_irq(struct tg3 *tp, int irq_num)
  8239. {
  8240. irq_handler_t fn;
  8241. unsigned long flags;
  8242. char *name;
  8243. struct tg3_napi *tnapi = &tp->napi[irq_num];
  8244. if (tp->irq_cnt == 1)
  8245. name = tp->dev->name;
  8246. else {
  8247. name = &tnapi->irq_lbl[0];
  8248. snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
  8249. name[IFNAMSIZ-1] = 0;
  8250. }
  8251. if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
  8252. fn = tg3_msi;
  8253. if (tg3_flag(tp, 1SHOT_MSI))
  8254. fn = tg3_msi_1shot;
  8255. flags = 0;
  8256. } else {
  8257. fn = tg3_interrupt;
  8258. if (tg3_flag(tp, TAGGED_STATUS))
  8259. fn = tg3_interrupt_tagged;
  8260. flags = IRQF_SHARED;
  8261. }
  8262. return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
  8263. }
  8264. static int tg3_test_interrupt(struct tg3 *tp)
  8265. {
  8266. struct tg3_napi *tnapi = &tp->napi[0];
  8267. struct net_device *dev = tp->dev;
  8268. int err, i, intr_ok = 0;
  8269. u32 val;
  8270. if (!netif_running(dev))
  8271. return -ENODEV;
  8272. tg3_disable_ints(tp);
  8273. free_irq(tnapi->irq_vec, tnapi);
  8274. /*
  8275. * Turn off MSI one shot mode. Otherwise this test has no
  8276. * observable way to know whether the interrupt was delivered.
  8277. */
  8278. if (tg3_flag(tp, 57765_PLUS)) {
  8279. val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
  8280. tw32(MSGINT_MODE, val);
  8281. }
  8282. err = request_irq(tnapi->irq_vec, tg3_test_isr,
  8283. IRQF_SHARED, dev->name, tnapi);
  8284. if (err)
  8285. return err;
  8286. tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
  8287. tg3_enable_ints(tp);
  8288. tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
  8289. tnapi->coal_now);
  8290. for (i = 0; i < 5; i++) {
  8291. u32 int_mbox, misc_host_ctrl;
  8292. int_mbox = tr32_mailbox(tnapi->int_mbox);
  8293. misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
  8294. if ((int_mbox != 0) ||
  8295. (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
  8296. intr_ok = 1;
  8297. break;
  8298. }
  8299. if (tg3_flag(tp, 57765_PLUS) &&
  8300. tnapi->hw_status->status_tag != tnapi->last_tag)
  8301. tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
  8302. msleep(10);
  8303. }
  8304. tg3_disable_ints(tp);
  8305. free_irq(tnapi->irq_vec, tnapi);
  8306. err = tg3_request_irq(tp, 0);
  8307. if (err)
  8308. return err;
  8309. if (intr_ok) {
  8310. /* Reenable MSI one shot mode. */
  8311. if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, 1SHOT_MSI)) {
  8312. val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
  8313. tw32(MSGINT_MODE, val);
  8314. }
  8315. return 0;
  8316. }
  8317. return -EIO;
  8318. }
  8319. /* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
  8320. * successfully restored
  8321. */
  8322. static int tg3_test_msi(struct tg3 *tp)
  8323. {
  8324. int err;
  8325. u16 pci_cmd;
  8326. if (!tg3_flag(tp, USING_MSI))
  8327. return 0;
  8328. /* Turn off SERR reporting in case MSI terminates with Master
  8329. * Abort.
  8330. */
  8331. pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
  8332. pci_write_config_word(tp->pdev, PCI_COMMAND,
  8333. pci_cmd & ~PCI_COMMAND_SERR);
  8334. err = tg3_test_interrupt(tp);
  8335. pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
  8336. if (!err)
  8337. return 0;
  8338. /* other failures */
  8339. if (err != -EIO)
  8340. return err;
  8341. /* MSI test failed, go back to INTx mode */
  8342. netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
  8343. "to INTx mode. Please report this failure to the PCI "
  8344. "maintainer and include system chipset information\n");
  8345. free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
  8346. pci_disable_msi(tp->pdev);
  8347. tg3_flag_clear(tp, USING_MSI);
  8348. tp->napi[0].irq_vec = tp->pdev->irq;
  8349. err = tg3_request_irq(tp, 0);
  8350. if (err)
  8351. return err;
  8352. /* Need to reset the chip because the MSI cycle may have terminated
  8353. * with Master Abort.
  8354. */
  8355. tg3_full_lock(tp, 1);
  8356. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  8357. err = tg3_init_hw(tp, 1);
  8358. tg3_full_unlock(tp);
  8359. if (err)
  8360. free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
  8361. return err;
  8362. }
  8363. static int tg3_request_firmware(struct tg3 *tp)
  8364. {
  8365. const __be32 *fw_data;
  8366. if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
  8367. netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
  8368. tp->fw_needed);
  8369. return -ENOENT;
  8370. }
  8371. fw_data = (void *)tp->fw->data;
  8372. /* Firmware blob starts with version numbers, followed by
  8373. * start address and _full_ length including BSS sections
  8374. * (which must be longer than the actual data, of course
  8375. */
  8376. tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
  8377. if (tp->fw_len < (tp->fw->size - 12)) {
  8378. netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
  8379. tp->fw_len, tp->fw_needed);
  8380. release_firmware(tp->fw);
  8381. tp->fw = NULL;
  8382. return -EINVAL;
  8383. }
  8384. /* We no longer need firmware; we have it. */
  8385. tp->fw_needed = NULL;
  8386. return 0;
  8387. }
  8388. static u32 tg3_irq_count(struct tg3 *tp)
  8389. {
  8390. u32 irq_cnt = max(tp->rxq_cnt, tp->txq_cnt);
  8391. if (irq_cnt > 1) {
  8392. /* We want as many rx rings enabled as there are cpus.
  8393. * In multiqueue MSI-X mode, the first MSI-X vector
  8394. * only deals with link interrupts, etc, so we add
  8395. * one to the number of vectors we are requesting.
  8396. */
  8397. irq_cnt = min_t(unsigned, irq_cnt + 1, tp->irq_max);
  8398. }
  8399. return irq_cnt;
  8400. }
  8401. static bool tg3_enable_msix(struct tg3 *tp)
  8402. {
  8403. int i, rc;
  8404. struct msix_entry msix_ent[TG3_IRQ_MAX_VECS];
  8405. tp->txq_cnt = tp->txq_req;
  8406. tp->rxq_cnt = tp->rxq_req;
  8407. if (!tp->rxq_cnt)
  8408. tp->rxq_cnt = netif_get_num_default_rss_queues();
  8409. if (tp->rxq_cnt > tp->rxq_max)
  8410. tp->rxq_cnt = tp->rxq_max;
  8411. /* Disable multiple TX rings by default. Simple round-robin hardware
  8412. * scheduling of the TX rings can cause starvation of rings with
  8413. * small packets when other rings have TSO or jumbo packets.
  8414. */
  8415. if (!tp->txq_req)
  8416. tp->txq_cnt = 1;
  8417. tp->irq_cnt = tg3_irq_count(tp);
  8418. for (i = 0; i < tp->irq_max; i++) {
  8419. msix_ent[i].entry = i;
  8420. msix_ent[i].vector = 0;
  8421. }
  8422. rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
  8423. if (rc < 0) {
  8424. return false;
  8425. } else if (rc != 0) {
  8426. if (pci_enable_msix(tp->pdev, msix_ent, rc))
  8427. return false;
  8428. netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
  8429. tp->irq_cnt, rc);
  8430. tp->irq_cnt = rc;
  8431. tp->rxq_cnt = max(rc - 1, 1);
  8432. if (tp->txq_cnt)
  8433. tp->txq_cnt = min(tp->rxq_cnt, tp->txq_max);
  8434. }
  8435. for (i = 0; i < tp->irq_max; i++)
  8436. tp->napi[i].irq_vec = msix_ent[i].vector;
  8437. if (netif_set_real_num_rx_queues(tp->dev, tp->rxq_cnt)) {
  8438. pci_disable_msix(tp->pdev);
  8439. return false;
  8440. }
  8441. if (tp->irq_cnt == 1)
  8442. return true;
  8443. tg3_flag_set(tp, ENABLE_RSS);
  8444. if (tp->txq_cnt > 1)
  8445. tg3_flag_set(tp, ENABLE_TSS);
  8446. netif_set_real_num_tx_queues(tp->dev, tp->txq_cnt);
  8447. return true;
  8448. }
  8449. static void tg3_ints_init(struct tg3 *tp)
  8450. {
  8451. if ((tg3_flag(tp, SUPPORT_MSI) || tg3_flag(tp, SUPPORT_MSIX)) &&
  8452. !tg3_flag(tp, TAGGED_STATUS)) {
  8453. /* All MSI supporting chips should support tagged
  8454. * status. Assert that this is the case.
  8455. */
  8456. netdev_warn(tp->dev,
  8457. "MSI without TAGGED_STATUS? Not using MSI\n");
  8458. goto defcfg;
  8459. }
  8460. if (tg3_flag(tp, SUPPORT_MSIX) && tg3_enable_msix(tp))
  8461. tg3_flag_set(tp, USING_MSIX);
  8462. else if (tg3_flag(tp, SUPPORT_MSI) && pci_enable_msi(tp->pdev) == 0)
  8463. tg3_flag_set(tp, USING_MSI);
  8464. if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
  8465. u32 msi_mode = tr32(MSGINT_MODE);
  8466. if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1)
  8467. msi_mode |= MSGINT_MODE_MULTIVEC_EN;
  8468. if (!tg3_flag(tp, 1SHOT_MSI))
  8469. msi_mode |= MSGINT_MODE_ONE_SHOT_DISABLE;
  8470. tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
  8471. }
  8472. defcfg:
  8473. if (!tg3_flag(tp, USING_MSIX)) {
  8474. tp->irq_cnt = 1;
  8475. tp->napi[0].irq_vec = tp->pdev->irq;
  8476. }
  8477. if (tp->irq_cnt == 1) {
  8478. tp->txq_cnt = 1;
  8479. tp->rxq_cnt = 1;
  8480. netif_set_real_num_tx_queues(tp->dev, 1);
  8481. netif_set_real_num_rx_queues(tp->dev, 1);
  8482. }
  8483. }
  8484. static void tg3_ints_fini(struct tg3 *tp)
  8485. {
  8486. if (tg3_flag(tp, USING_MSIX))
  8487. pci_disable_msix(tp->pdev);
  8488. else if (tg3_flag(tp, USING_MSI))
  8489. pci_disable_msi(tp->pdev);
  8490. tg3_flag_clear(tp, USING_MSI);
  8491. tg3_flag_clear(tp, USING_MSIX);
  8492. tg3_flag_clear(tp, ENABLE_RSS);
  8493. tg3_flag_clear(tp, ENABLE_TSS);
  8494. }
  8495. static int tg3_start(struct tg3 *tp, bool reset_phy, bool test_irq)
  8496. {
  8497. struct net_device *dev = tp->dev;
  8498. int i, err;
  8499. /*
  8500. * Setup interrupts first so we know how
  8501. * many NAPI resources to allocate
  8502. */
  8503. tg3_ints_init(tp);
  8504. tg3_rss_check_indir_tbl(tp);
  8505. /* The placement of this call is tied
  8506. * to the setup and use of Host TX descriptors.
  8507. */
  8508. err = tg3_alloc_consistent(tp);
  8509. if (err)
  8510. goto err_out1;
  8511. tg3_napi_init(tp);
  8512. tg3_napi_enable(tp);
  8513. for (i = 0; i < tp->irq_cnt; i++) {
  8514. struct tg3_napi *tnapi = &tp->napi[i];
  8515. err = tg3_request_irq(tp, i);
  8516. if (err) {
  8517. for (i--; i >= 0; i--) {
  8518. tnapi = &tp->napi[i];
  8519. free_irq(tnapi->irq_vec, tnapi);
  8520. }
  8521. goto err_out2;
  8522. }
  8523. }
  8524. tg3_full_lock(tp, 0);
  8525. err = tg3_init_hw(tp, reset_phy);
  8526. if (err) {
  8527. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  8528. tg3_free_rings(tp);
  8529. }
  8530. tg3_full_unlock(tp);
  8531. if (err)
  8532. goto err_out3;
  8533. if (test_irq && tg3_flag(tp, USING_MSI)) {
  8534. err = tg3_test_msi(tp);
  8535. if (err) {
  8536. tg3_full_lock(tp, 0);
  8537. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  8538. tg3_free_rings(tp);
  8539. tg3_full_unlock(tp);
  8540. goto err_out2;
  8541. }
  8542. if (!tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
  8543. u32 val = tr32(PCIE_TRANSACTION_CFG);
  8544. tw32(PCIE_TRANSACTION_CFG,
  8545. val | PCIE_TRANS_CFG_1SHOT_MSI);
  8546. }
  8547. }
  8548. tg3_phy_start(tp);
  8549. tg3_hwmon_open(tp);
  8550. tg3_full_lock(tp, 0);
  8551. tg3_timer_start(tp);
  8552. tg3_flag_set(tp, INIT_COMPLETE);
  8553. tg3_enable_ints(tp);
  8554. tg3_full_unlock(tp);
  8555. netif_tx_start_all_queues(dev);
  8556. /*
  8557. * Reset loopback feature if it was turned on while the device was down
  8558. * make sure that it's installed properly now.
  8559. */
  8560. if (dev->features & NETIF_F_LOOPBACK)
  8561. tg3_set_loopback(dev, dev->features);
  8562. return 0;
  8563. err_out3:
  8564. for (i = tp->irq_cnt - 1; i >= 0; i--) {
  8565. struct tg3_napi *tnapi = &tp->napi[i];
  8566. free_irq(tnapi->irq_vec, tnapi);
  8567. }
  8568. err_out2:
  8569. tg3_napi_disable(tp);
  8570. tg3_napi_fini(tp);
  8571. tg3_free_consistent(tp);
  8572. err_out1:
  8573. tg3_ints_fini(tp);
  8574. return err;
  8575. }
  8576. static void tg3_stop(struct tg3 *tp)
  8577. {
  8578. int i;
  8579. tg3_reset_task_cancel(tp);
  8580. tg3_netif_stop(tp);
  8581. tg3_timer_stop(tp);
  8582. tg3_hwmon_close(tp);
  8583. tg3_phy_stop(tp);
  8584. tg3_full_lock(tp, 1);
  8585. tg3_disable_ints(tp);
  8586. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  8587. tg3_free_rings(tp);
  8588. tg3_flag_clear(tp, INIT_COMPLETE);
  8589. tg3_full_unlock(tp);
  8590. for (i = tp->irq_cnt - 1; i >= 0; i--) {
  8591. struct tg3_napi *tnapi = &tp->napi[i];
  8592. free_irq(tnapi->irq_vec, tnapi);
  8593. }
  8594. tg3_ints_fini(tp);
  8595. tg3_napi_fini(tp);
  8596. tg3_free_consistent(tp);
  8597. }
  8598. static int tg3_open(struct net_device *dev)
  8599. {
  8600. struct tg3 *tp = netdev_priv(dev);
  8601. int err;
  8602. if (tp->fw_needed) {
  8603. err = tg3_request_firmware(tp);
  8604. if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
  8605. if (err)
  8606. return err;
  8607. } else if (err) {
  8608. netdev_warn(tp->dev, "TSO capability disabled\n");
  8609. tg3_flag_clear(tp, TSO_CAPABLE);
  8610. } else if (!tg3_flag(tp, TSO_CAPABLE)) {
  8611. netdev_notice(tp->dev, "TSO capability restored\n");
  8612. tg3_flag_set(tp, TSO_CAPABLE);
  8613. }
  8614. }
  8615. netif_carrier_off(tp->dev);
  8616. err = tg3_power_up(tp);
  8617. if (err)
  8618. return err;
  8619. tg3_full_lock(tp, 0);
  8620. tg3_disable_ints(tp);
  8621. tg3_flag_clear(tp, INIT_COMPLETE);
  8622. tg3_full_unlock(tp);
  8623. err = tg3_start(tp, true, true);
  8624. if (err) {
  8625. tg3_frob_aux_power(tp, false);
  8626. pci_set_power_state(tp->pdev, PCI_D3hot);
  8627. }
  8628. return err;
  8629. }
  8630. static int tg3_close(struct net_device *dev)
  8631. {
  8632. struct tg3 *tp = netdev_priv(dev);
  8633. tg3_stop(tp);
  8634. /* Clear stats across close / open calls */
  8635. memset(&tp->net_stats_prev, 0, sizeof(tp->net_stats_prev));
  8636. memset(&tp->estats_prev, 0, sizeof(tp->estats_prev));
  8637. tg3_power_down(tp);
  8638. netif_carrier_off(tp->dev);
  8639. return 0;
  8640. }
  8641. static inline u64 get_stat64(tg3_stat64_t *val)
  8642. {
  8643. return ((u64)val->high << 32) | ((u64)val->low);
  8644. }
  8645. static u64 tg3_calc_crc_errors(struct tg3 *tp)
  8646. {
  8647. struct tg3_hw_stats *hw_stats = tp->hw_stats;
  8648. if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
  8649. (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
  8650. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
  8651. u32 val;
  8652. if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
  8653. tg3_writephy(tp, MII_TG3_TEST1,
  8654. val | MII_TG3_TEST1_CRC_EN);
  8655. tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
  8656. } else
  8657. val = 0;
  8658. tp->phy_crc_errors += val;
  8659. return tp->phy_crc_errors;
  8660. }
  8661. return get_stat64(&hw_stats->rx_fcs_errors);
  8662. }
  8663. #define ESTAT_ADD(member) \
  8664. estats->member = old_estats->member + \
  8665. get_stat64(&hw_stats->member)
  8666. static void tg3_get_estats(struct tg3 *tp, struct tg3_ethtool_stats *estats)
  8667. {
  8668. struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
  8669. struct tg3_hw_stats *hw_stats = tp->hw_stats;
  8670. ESTAT_ADD(rx_octets);
  8671. ESTAT_ADD(rx_fragments);
  8672. ESTAT_ADD(rx_ucast_packets);
  8673. ESTAT_ADD(rx_mcast_packets);
  8674. ESTAT_ADD(rx_bcast_packets);
  8675. ESTAT_ADD(rx_fcs_errors);
  8676. ESTAT_ADD(rx_align_errors);
  8677. ESTAT_ADD(rx_xon_pause_rcvd);
  8678. ESTAT_ADD(rx_xoff_pause_rcvd);
  8679. ESTAT_ADD(rx_mac_ctrl_rcvd);
  8680. ESTAT_ADD(rx_xoff_entered);
  8681. ESTAT_ADD(rx_frame_too_long_errors);
  8682. ESTAT_ADD(rx_jabbers);
  8683. ESTAT_ADD(rx_undersize_packets);
  8684. ESTAT_ADD(rx_in_length_errors);
  8685. ESTAT_ADD(rx_out_length_errors);
  8686. ESTAT_ADD(rx_64_or_less_octet_packets);
  8687. ESTAT_ADD(rx_65_to_127_octet_packets);
  8688. ESTAT_ADD(rx_128_to_255_octet_packets);
  8689. ESTAT_ADD(rx_256_to_511_octet_packets);
  8690. ESTAT_ADD(rx_512_to_1023_octet_packets);
  8691. ESTAT_ADD(rx_1024_to_1522_octet_packets);
  8692. ESTAT_ADD(rx_1523_to_2047_octet_packets);
  8693. ESTAT_ADD(rx_2048_to_4095_octet_packets);
  8694. ESTAT_ADD(rx_4096_to_8191_octet_packets);
  8695. ESTAT_ADD(rx_8192_to_9022_octet_packets);
  8696. ESTAT_ADD(tx_octets);
  8697. ESTAT_ADD(tx_collisions);
  8698. ESTAT_ADD(tx_xon_sent);
  8699. ESTAT_ADD(tx_xoff_sent);
  8700. ESTAT_ADD(tx_flow_control);
  8701. ESTAT_ADD(tx_mac_errors);
  8702. ESTAT_ADD(tx_single_collisions);
  8703. ESTAT_ADD(tx_mult_collisions);
  8704. ESTAT_ADD(tx_deferred);
  8705. ESTAT_ADD(tx_excessive_collisions);
  8706. ESTAT_ADD(tx_late_collisions);
  8707. ESTAT_ADD(tx_collide_2times);
  8708. ESTAT_ADD(tx_collide_3times);
  8709. ESTAT_ADD(tx_collide_4times);
  8710. ESTAT_ADD(tx_collide_5times);
  8711. ESTAT_ADD(tx_collide_6times);
  8712. ESTAT_ADD(tx_collide_7times);
  8713. ESTAT_ADD(tx_collide_8times);
  8714. ESTAT_ADD(tx_collide_9times);
  8715. ESTAT_ADD(tx_collide_10times);
  8716. ESTAT_ADD(tx_collide_11times);
  8717. ESTAT_ADD(tx_collide_12times);
  8718. ESTAT_ADD(tx_collide_13times);
  8719. ESTAT_ADD(tx_collide_14times);
  8720. ESTAT_ADD(tx_collide_15times);
  8721. ESTAT_ADD(tx_ucast_packets);
  8722. ESTAT_ADD(tx_mcast_packets);
  8723. ESTAT_ADD(tx_bcast_packets);
  8724. ESTAT_ADD(tx_carrier_sense_errors);
  8725. ESTAT_ADD(tx_discards);
  8726. ESTAT_ADD(tx_errors);
  8727. ESTAT_ADD(dma_writeq_full);
  8728. ESTAT_ADD(dma_write_prioq_full);
  8729. ESTAT_ADD(rxbds_empty);
  8730. ESTAT_ADD(rx_discards);
  8731. ESTAT_ADD(rx_errors);
  8732. ESTAT_ADD(rx_threshold_hit);
  8733. ESTAT_ADD(dma_readq_full);
  8734. ESTAT_ADD(dma_read_prioq_full);
  8735. ESTAT_ADD(tx_comp_queue_full);
  8736. ESTAT_ADD(ring_set_send_prod_index);
  8737. ESTAT_ADD(ring_status_update);
  8738. ESTAT_ADD(nic_irqs);
  8739. ESTAT_ADD(nic_avoided_irqs);
  8740. ESTAT_ADD(nic_tx_threshold_hit);
  8741. ESTAT_ADD(mbuf_lwm_thresh_hit);
  8742. }
  8743. static void tg3_get_nstats(struct tg3 *tp, struct rtnl_link_stats64 *stats)
  8744. {
  8745. struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
  8746. struct tg3_hw_stats *hw_stats = tp->hw_stats;
  8747. stats->rx_packets = old_stats->rx_packets +
  8748. get_stat64(&hw_stats->rx_ucast_packets) +
  8749. get_stat64(&hw_stats->rx_mcast_packets) +
  8750. get_stat64(&hw_stats->rx_bcast_packets);
  8751. stats->tx_packets = old_stats->tx_packets +
  8752. get_stat64(&hw_stats->tx_ucast_packets) +
  8753. get_stat64(&hw_stats->tx_mcast_packets) +
  8754. get_stat64(&hw_stats->tx_bcast_packets);
  8755. stats->rx_bytes = old_stats->rx_bytes +
  8756. get_stat64(&hw_stats->rx_octets);
  8757. stats->tx_bytes = old_stats->tx_bytes +
  8758. get_stat64(&hw_stats->tx_octets);
  8759. stats->rx_errors = old_stats->rx_errors +
  8760. get_stat64(&hw_stats->rx_errors);
  8761. stats->tx_errors = old_stats->tx_errors +
  8762. get_stat64(&hw_stats->tx_errors) +
  8763. get_stat64(&hw_stats->tx_mac_errors) +
  8764. get_stat64(&hw_stats->tx_carrier_sense_errors) +
  8765. get_stat64(&hw_stats->tx_discards);
  8766. stats->multicast = old_stats->multicast +
  8767. get_stat64(&hw_stats->rx_mcast_packets);
  8768. stats->collisions = old_stats->collisions +
  8769. get_stat64(&hw_stats->tx_collisions);
  8770. stats->rx_length_errors = old_stats->rx_length_errors +
  8771. get_stat64(&hw_stats->rx_frame_too_long_errors) +
  8772. get_stat64(&hw_stats->rx_undersize_packets);
  8773. stats->rx_over_errors = old_stats->rx_over_errors +
  8774. get_stat64(&hw_stats->rxbds_empty);
  8775. stats->rx_frame_errors = old_stats->rx_frame_errors +
  8776. get_stat64(&hw_stats->rx_align_errors);
  8777. stats->tx_aborted_errors = old_stats->tx_aborted_errors +
  8778. get_stat64(&hw_stats->tx_discards);
  8779. stats->tx_carrier_errors = old_stats->tx_carrier_errors +
  8780. get_stat64(&hw_stats->tx_carrier_sense_errors);
  8781. stats->rx_crc_errors = old_stats->rx_crc_errors +
  8782. tg3_calc_crc_errors(tp);
  8783. stats->rx_missed_errors = old_stats->rx_missed_errors +
  8784. get_stat64(&hw_stats->rx_discards);
  8785. stats->rx_dropped = tp->rx_dropped;
  8786. stats->tx_dropped = tp->tx_dropped;
  8787. }
  8788. static int tg3_get_regs_len(struct net_device *dev)
  8789. {
  8790. return TG3_REG_BLK_SIZE;
  8791. }
  8792. static void tg3_get_regs(struct net_device *dev,
  8793. struct ethtool_regs *regs, void *_p)
  8794. {
  8795. struct tg3 *tp = netdev_priv(dev);
  8796. regs->version = 0;
  8797. memset(_p, 0, TG3_REG_BLK_SIZE);
  8798. if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
  8799. return;
  8800. tg3_full_lock(tp, 0);
  8801. tg3_dump_legacy_regs(tp, (u32 *)_p);
  8802. tg3_full_unlock(tp);
  8803. }
  8804. static int tg3_get_eeprom_len(struct net_device *dev)
  8805. {
  8806. struct tg3 *tp = netdev_priv(dev);
  8807. return tp->nvram_size;
  8808. }
  8809. static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
  8810. {
  8811. struct tg3 *tp = netdev_priv(dev);
  8812. int ret;
  8813. u8 *pd;
  8814. u32 i, offset, len, b_offset, b_count;
  8815. __be32 val;
  8816. if (tg3_flag(tp, NO_NVRAM))
  8817. return -EINVAL;
  8818. if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
  8819. return -EAGAIN;
  8820. offset = eeprom->offset;
  8821. len = eeprom->len;
  8822. eeprom->len = 0;
  8823. eeprom->magic = TG3_EEPROM_MAGIC;
  8824. if (offset & 3) {
  8825. /* adjustments to start on required 4 byte boundary */
  8826. b_offset = offset & 3;
  8827. b_count = 4 - b_offset;
  8828. if (b_count > len) {
  8829. /* i.e. offset=1 len=2 */
  8830. b_count = len;
  8831. }
  8832. ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
  8833. if (ret)
  8834. return ret;
  8835. memcpy(data, ((char *)&val) + b_offset, b_count);
  8836. len -= b_count;
  8837. offset += b_count;
  8838. eeprom->len += b_count;
  8839. }
  8840. /* read bytes up to the last 4 byte boundary */
  8841. pd = &data[eeprom->len];
  8842. for (i = 0; i < (len - (len & 3)); i += 4) {
  8843. ret = tg3_nvram_read_be32(tp, offset + i, &val);
  8844. if (ret) {
  8845. eeprom->len += i;
  8846. return ret;
  8847. }
  8848. memcpy(pd + i, &val, 4);
  8849. }
  8850. eeprom->len += i;
  8851. if (len & 3) {
  8852. /* read last bytes not ending on 4 byte boundary */
  8853. pd = &data[eeprom->len];
  8854. b_count = len & 3;
  8855. b_offset = offset + len - b_count;
  8856. ret = tg3_nvram_read_be32(tp, b_offset, &val);
  8857. if (ret)
  8858. return ret;
  8859. memcpy(pd, &val, b_count);
  8860. eeprom->len += b_count;
  8861. }
  8862. return 0;
  8863. }
  8864. static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
  8865. {
  8866. struct tg3 *tp = netdev_priv(dev);
  8867. int ret;
  8868. u32 offset, len, b_offset, odd_len;
  8869. u8 *buf;
  8870. __be32 start, end;
  8871. if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
  8872. return -EAGAIN;
  8873. if (tg3_flag(tp, NO_NVRAM) ||
  8874. eeprom->magic != TG3_EEPROM_MAGIC)
  8875. return -EINVAL;
  8876. offset = eeprom->offset;
  8877. len = eeprom->len;
  8878. if ((b_offset = (offset & 3))) {
  8879. /* adjustments to start on required 4 byte boundary */
  8880. ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
  8881. if (ret)
  8882. return ret;
  8883. len += b_offset;
  8884. offset &= ~3;
  8885. if (len < 4)
  8886. len = 4;
  8887. }
  8888. odd_len = 0;
  8889. if (len & 3) {
  8890. /* adjustments to end on required 4 byte boundary */
  8891. odd_len = 1;
  8892. len = (len + 3) & ~3;
  8893. ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
  8894. if (ret)
  8895. return ret;
  8896. }
  8897. buf = data;
  8898. if (b_offset || odd_len) {
  8899. buf = kmalloc(len, GFP_KERNEL);
  8900. if (!buf)
  8901. return -ENOMEM;
  8902. if (b_offset)
  8903. memcpy(buf, &start, 4);
  8904. if (odd_len)
  8905. memcpy(buf+len-4, &end, 4);
  8906. memcpy(buf + b_offset, data, eeprom->len);
  8907. }
  8908. ret = tg3_nvram_write_block(tp, offset, len, buf);
  8909. if (buf != data)
  8910. kfree(buf);
  8911. return ret;
  8912. }
  8913. static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  8914. {
  8915. struct tg3 *tp = netdev_priv(dev);
  8916. if (tg3_flag(tp, USE_PHYLIB)) {
  8917. struct phy_device *phydev;
  8918. if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
  8919. return -EAGAIN;
  8920. phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
  8921. return phy_ethtool_gset(phydev, cmd);
  8922. }
  8923. cmd->supported = (SUPPORTED_Autoneg);
  8924. if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
  8925. cmd->supported |= (SUPPORTED_1000baseT_Half |
  8926. SUPPORTED_1000baseT_Full);
  8927. if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
  8928. cmd->supported |= (SUPPORTED_100baseT_Half |
  8929. SUPPORTED_100baseT_Full |
  8930. SUPPORTED_10baseT_Half |
  8931. SUPPORTED_10baseT_Full |
  8932. SUPPORTED_TP);
  8933. cmd->port = PORT_TP;
  8934. } else {
  8935. cmd->supported |= SUPPORTED_FIBRE;
  8936. cmd->port = PORT_FIBRE;
  8937. }
  8938. cmd->advertising = tp->link_config.advertising;
  8939. if (tg3_flag(tp, PAUSE_AUTONEG)) {
  8940. if (tp->link_config.flowctrl & FLOW_CTRL_RX) {
  8941. if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
  8942. cmd->advertising |= ADVERTISED_Pause;
  8943. } else {
  8944. cmd->advertising |= ADVERTISED_Pause |
  8945. ADVERTISED_Asym_Pause;
  8946. }
  8947. } else if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
  8948. cmd->advertising |= ADVERTISED_Asym_Pause;
  8949. }
  8950. }
  8951. if (netif_running(dev) && netif_carrier_ok(dev)) {
  8952. ethtool_cmd_speed_set(cmd, tp->link_config.active_speed);
  8953. cmd->duplex = tp->link_config.active_duplex;
  8954. cmd->lp_advertising = tp->link_config.rmt_adv;
  8955. if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
  8956. if (tp->phy_flags & TG3_PHYFLG_MDIX_STATE)
  8957. cmd->eth_tp_mdix = ETH_TP_MDI_X;
  8958. else
  8959. cmd->eth_tp_mdix = ETH_TP_MDI;
  8960. }
  8961. } else {
  8962. ethtool_cmd_speed_set(cmd, SPEED_UNKNOWN);
  8963. cmd->duplex = DUPLEX_UNKNOWN;
  8964. cmd->eth_tp_mdix = ETH_TP_MDI_INVALID;
  8965. }
  8966. cmd->phy_address = tp->phy_addr;
  8967. cmd->transceiver = XCVR_INTERNAL;
  8968. cmd->autoneg = tp->link_config.autoneg;
  8969. cmd->maxtxpkt = 0;
  8970. cmd->maxrxpkt = 0;
  8971. return 0;
  8972. }
  8973. static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  8974. {
  8975. struct tg3 *tp = netdev_priv(dev);
  8976. u32 speed = ethtool_cmd_speed(cmd);
  8977. if (tg3_flag(tp, USE_PHYLIB)) {
  8978. struct phy_device *phydev;
  8979. if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
  8980. return -EAGAIN;
  8981. phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
  8982. return phy_ethtool_sset(phydev, cmd);
  8983. }
  8984. if (cmd->autoneg != AUTONEG_ENABLE &&
  8985. cmd->autoneg != AUTONEG_DISABLE)
  8986. return -EINVAL;
  8987. if (cmd->autoneg == AUTONEG_DISABLE &&
  8988. cmd->duplex != DUPLEX_FULL &&
  8989. cmd->duplex != DUPLEX_HALF)
  8990. return -EINVAL;
  8991. if (cmd->autoneg == AUTONEG_ENABLE) {
  8992. u32 mask = ADVERTISED_Autoneg |
  8993. ADVERTISED_Pause |
  8994. ADVERTISED_Asym_Pause;
  8995. if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
  8996. mask |= ADVERTISED_1000baseT_Half |
  8997. ADVERTISED_1000baseT_Full;
  8998. if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
  8999. mask |= ADVERTISED_100baseT_Half |
  9000. ADVERTISED_100baseT_Full |
  9001. ADVERTISED_10baseT_Half |
  9002. ADVERTISED_10baseT_Full |
  9003. ADVERTISED_TP;
  9004. else
  9005. mask |= ADVERTISED_FIBRE;
  9006. if (cmd->advertising & ~mask)
  9007. return -EINVAL;
  9008. mask &= (ADVERTISED_1000baseT_Half |
  9009. ADVERTISED_1000baseT_Full |
  9010. ADVERTISED_100baseT_Half |
  9011. ADVERTISED_100baseT_Full |
  9012. ADVERTISED_10baseT_Half |
  9013. ADVERTISED_10baseT_Full);
  9014. cmd->advertising &= mask;
  9015. } else {
  9016. if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
  9017. if (speed != SPEED_1000)
  9018. return -EINVAL;
  9019. if (cmd->duplex != DUPLEX_FULL)
  9020. return -EINVAL;
  9021. } else {
  9022. if (speed != SPEED_100 &&
  9023. speed != SPEED_10)
  9024. return -EINVAL;
  9025. }
  9026. }
  9027. tg3_full_lock(tp, 0);
  9028. tp->link_config.autoneg = cmd->autoneg;
  9029. if (cmd->autoneg == AUTONEG_ENABLE) {
  9030. tp->link_config.advertising = (cmd->advertising |
  9031. ADVERTISED_Autoneg);
  9032. tp->link_config.speed = SPEED_UNKNOWN;
  9033. tp->link_config.duplex = DUPLEX_UNKNOWN;
  9034. } else {
  9035. tp->link_config.advertising = 0;
  9036. tp->link_config.speed = speed;
  9037. tp->link_config.duplex = cmd->duplex;
  9038. }
  9039. if (netif_running(dev))
  9040. tg3_setup_phy(tp, 1);
  9041. tg3_full_unlock(tp);
  9042. return 0;
  9043. }
  9044. static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  9045. {
  9046. struct tg3 *tp = netdev_priv(dev);
  9047. strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
  9048. strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
  9049. strlcpy(info->fw_version, tp->fw_ver, sizeof(info->fw_version));
  9050. strlcpy(info->bus_info, pci_name(tp->pdev), sizeof(info->bus_info));
  9051. }
  9052. static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  9053. {
  9054. struct tg3 *tp = netdev_priv(dev);
  9055. if (tg3_flag(tp, WOL_CAP) && device_can_wakeup(&tp->pdev->dev))
  9056. wol->supported = WAKE_MAGIC;
  9057. else
  9058. wol->supported = 0;
  9059. wol->wolopts = 0;
  9060. if (tg3_flag(tp, WOL_ENABLE) && device_can_wakeup(&tp->pdev->dev))
  9061. wol->wolopts = WAKE_MAGIC;
  9062. memset(&wol->sopass, 0, sizeof(wol->sopass));
  9063. }
  9064. static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  9065. {
  9066. struct tg3 *tp = netdev_priv(dev);
  9067. struct device *dp = &tp->pdev->dev;
  9068. if (wol->wolopts & ~WAKE_MAGIC)
  9069. return -EINVAL;
  9070. if ((wol->wolopts & WAKE_MAGIC) &&
  9071. !(tg3_flag(tp, WOL_CAP) && device_can_wakeup(dp)))
  9072. return -EINVAL;
  9073. device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
  9074. spin_lock_bh(&tp->lock);
  9075. if (device_may_wakeup(dp))
  9076. tg3_flag_set(tp, WOL_ENABLE);
  9077. else
  9078. tg3_flag_clear(tp, WOL_ENABLE);
  9079. spin_unlock_bh(&tp->lock);
  9080. return 0;
  9081. }
  9082. static u32 tg3_get_msglevel(struct net_device *dev)
  9083. {
  9084. struct tg3 *tp = netdev_priv(dev);
  9085. return tp->msg_enable;
  9086. }
  9087. static void tg3_set_msglevel(struct net_device *dev, u32 value)
  9088. {
  9089. struct tg3 *tp = netdev_priv(dev);
  9090. tp->msg_enable = value;
  9091. }
  9092. static int tg3_nway_reset(struct net_device *dev)
  9093. {
  9094. struct tg3 *tp = netdev_priv(dev);
  9095. int r;
  9096. if (!netif_running(dev))
  9097. return -EAGAIN;
  9098. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
  9099. return -EINVAL;
  9100. if (tg3_flag(tp, USE_PHYLIB)) {
  9101. if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
  9102. return -EAGAIN;
  9103. r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
  9104. } else {
  9105. u32 bmcr;
  9106. spin_lock_bh(&tp->lock);
  9107. r = -EINVAL;
  9108. tg3_readphy(tp, MII_BMCR, &bmcr);
  9109. if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
  9110. ((bmcr & BMCR_ANENABLE) ||
  9111. (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
  9112. tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
  9113. BMCR_ANENABLE);
  9114. r = 0;
  9115. }
  9116. spin_unlock_bh(&tp->lock);
  9117. }
  9118. return r;
  9119. }
  9120. static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
  9121. {
  9122. struct tg3 *tp = netdev_priv(dev);
  9123. ering->rx_max_pending = tp->rx_std_ring_mask;
  9124. if (tg3_flag(tp, JUMBO_RING_ENABLE))
  9125. ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
  9126. else
  9127. ering->rx_jumbo_max_pending = 0;
  9128. ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
  9129. ering->rx_pending = tp->rx_pending;
  9130. if (tg3_flag(tp, JUMBO_RING_ENABLE))
  9131. ering->rx_jumbo_pending = tp->rx_jumbo_pending;
  9132. else
  9133. ering->rx_jumbo_pending = 0;
  9134. ering->tx_pending = tp->napi[0].tx_pending;
  9135. }
  9136. static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
  9137. {
  9138. struct tg3 *tp = netdev_priv(dev);
  9139. int i, irq_sync = 0, err = 0;
  9140. if ((ering->rx_pending > tp->rx_std_ring_mask) ||
  9141. (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
  9142. (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
  9143. (ering->tx_pending <= MAX_SKB_FRAGS) ||
  9144. (tg3_flag(tp, TSO_BUG) &&
  9145. (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
  9146. return -EINVAL;
  9147. if (netif_running(dev)) {
  9148. tg3_phy_stop(tp);
  9149. tg3_netif_stop(tp);
  9150. irq_sync = 1;
  9151. }
  9152. tg3_full_lock(tp, irq_sync);
  9153. tp->rx_pending = ering->rx_pending;
  9154. if (tg3_flag(tp, MAX_RXPEND_64) &&
  9155. tp->rx_pending > 63)
  9156. tp->rx_pending = 63;
  9157. tp->rx_jumbo_pending = ering->rx_jumbo_pending;
  9158. for (i = 0; i < tp->irq_max; i++)
  9159. tp->napi[i].tx_pending = ering->tx_pending;
  9160. if (netif_running(dev)) {
  9161. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  9162. err = tg3_restart_hw(tp, 1);
  9163. if (!err)
  9164. tg3_netif_start(tp);
  9165. }
  9166. tg3_full_unlock(tp);
  9167. if (irq_sync && !err)
  9168. tg3_phy_start(tp);
  9169. return err;
  9170. }
  9171. static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
  9172. {
  9173. struct tg3 *tp = netdev_priv(dev);
  9174. epause->autoneg = !!tg3_flag(tp, PAUSE_AUTONEG);
  9175. if (tp->link_config.flowctrl & FLOW_CTRL_RX)
  9176. epause->rx_pause = 1;
  9177. else
  9178. epause->rx_pause = 0;
  9179. if (tp->link_config.flowctrl & FLOW_CTRL_TX)
  9180. epause->tx_pause = 1;
  9181. else
  9182. epause->tx_pause = 0;
  9183. }
  9184. static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
  9185. {
  9186. struct tg3 *tp = netdev_priv(dev);
  9187. int err = 0;
  9188. if (tg3_flag(tp, USE_PHYLIB)) {
  9189. u32 newadv;
  9190. struct phy_device *phydev;
  9191. phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
  9192. if (!(phydev->supported & SUPPORTED_Pause) ||
  9193. (!(phydev->supported & SUPPORTED_Asym_Pause) &&
  9194. (epause->rx_pause != epause->tx_pause)))
  9195. return -EINVAL;
  9196. tp->link_config.flowctrl = 0;
  9197. if (epause->rx_pause) {
  9198. tp->link_config.flowctrl |= FLOW_CTRL_RX;
  9199. if (epause->tx_pause) {
  9200. tp->link_config.flowctrl |= FLOW_CTRL_TX;
  9201. newadv = ADVERTISED_Pause;
  9202. } else
  9203. newadv = ADVERTISED_Pause |
  9204. ADVERTISED_Asym_Pause;
  9205. } else if (epause->tx_pause) {
  9206. tp->link_config.flowctrl |= FLOW_CTRL_TX;
  9207. newadv = ADVERTISED_Asym_Pause;
  9208. } else
  9209. newadv = 0;
  9210. if (epause->autoneg)
  9211. tg3_flag_set(tp, PAUSE_AUTONEG);
  9212. else
  9213. tg3_flag_clear(tp, PAUSE_AUTONEG);
  9214. if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
  9215. u32 oldadv = phydev->advertising &
  9216. (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
  9217. if (oldadv != newadv) {
  9218. phydev->advertising &=
  9219. ~(ADVERTISED_Pause |
  9220. ADVERTISED_Asym_Pause);
  9221. phydev->advertising |= newadv;
  9222. if (phydev->autoneg) {
  9223. /*
  9224. * Always renegotiate the link to
  9225. * inform our link partner of our
  9226. * flow control settings, even if the
  9227. * flow control is forced. Let
  9228. * tg3_adjust_link() do the final
  9229. * flow control setup.
  9230. */
  9231. return phy_start_aneg(phydev);
  9232. }
  9233. }
  9234. if (!epause->autoneg)
  9235. tg3_setup_flow_control(tp, 0, 0);
  9236. } else {
  9237. tp->link_config.advertising &=
  9238. ~(ADVERTISED_Pause |
  9239. ADVERTISED_Asym_Pause);
  9240. tp->link_config.advertising |= newadv;
  9241. }
  9242. } else {
  9243. int irq_sync = 0;
  9244. if (netif_running(dev)) {
  9245. tg3_netif_stop(tp);
  9246. irq_sync = 1;
  9247. }
  9248. tg3_full_lock(tp, irq_sync);
  9249. if (epause->autoneg)
  9250. tg3_flag_set(tp, PAUSE_AUTONEG);
  9251. else
  9252. tg3_flag_clear(tp, PAUSE_AUTONEG);
  9253. if (epause->rx_pause)
  9254. tp->link_config.flowctrl |= FLOW_CTRL_RX;
  9255. else
  9256. tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
  9257. if (epause->tx_pause)
  9258. tp->link_config.flowctrl |= FLOW_CTRL_TX;
  9259. else
  9260. tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
  9261. if (netif_running(dev)) {
  9262. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  9263. err = tg3_restart_hw(tp, 1);
  9264. if (!err)
  9265. tg3_netif_start(tp);
  9266. }
  9267. tg3_full_unlock(tp);
  9268. }
  9269. return err;
  9270. }
  9271. static int tg3_get_sset_count(struct net_device *dev, int sset)
  9272. {
  9273. switch (sset) {
  9274. case ETH_SS_TEST:
  9275. return TG3_NUM_TEST;
  9276. case ETH_SS_STATS:
  9277. return TG3_NUM_STATS;
  9278. default:
  9279. return -EOPNOTSUPP;
  9280. }
  9281. }
  9282. static int tg3_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
  9283. u32 *rules __always_unused)
  9284. {
  9285. struct tg3 *tp = netdev_priv(dev);
  9286. if (!tg3_flag(tp, SUPPORT_MSIX))
  9287. return -EOPNOTSUPP;
  9288. switch (info->cmd) {
  9289. case ETHTOOL_GRXRINGS:
  9290. if (netif_running(tp->dev))
  9291. info->data = tp->rxq_cnt;
  9292. else {
  9293. info->data = num_online_cpus();
  9294. if (info->data > TG3_RSS_MAX_NUM_QS)
  9295. info->data = TG3_RSS_MAX_NUM_QS;
  9296. }
  9297. /* The first interrupt vector only
  9298. * handles link interrupts.
  9299. */
  9300. info->data -= 1;
  9301. return 0;
  9302. default:
  9303. return -EOPNOTSUPP;
  9304. }
  9305. }
  9306. static u32 tg3_get_rxfh_indir_size(struct net_device *dev)
  9307. {
  9308. u32 size = 0;
  9309. struct tg3 *tp = netdev_priv(dev);
  9310. if (tg3_flag(tp, SUPPORT_MSIX))
  9311. size = TG3_RSS_INDIR_TBL_SIZE;
  9312. return size;
  9313. }
  9314. static int tg3_get_rxfh_indir(struct net_device *dev, u32 *indir)
  9315. {
  9316. struct tg3 *tp = netdev_priv(dev);
  9317. int i;
  9318. for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
  9319. indir[i] = tp->rss_ind_tbl[i];
  9320. return 0;
  9321. }
  9322. static int tg3_set_rxfh_indir(struct net_device *dev, const u32 *indir)
  9323. {
  9324. struct tg3 *tp = netdev_priv(dev);
  9325. size_t i;
  9326. for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
  9327. tp->rss_ind_tbl[i] = indir[i];
  9328. if (!netif_running(dev) || !tg3_flag(tp, ENABLE_RSS))
  9329. return 0;
  9330. /* It is legal to write the indirection
  9331. * table while the device is running.
  9332. */
  9333. tg3_full_lock(tp, 0);
  9334. tg3_rss_write_indir_tbl(tp);
  9335. tg3_full_unlock(tp);
  9336. return 0;
  9337. }
  9338. static void tg3_get_channels(struct net_device *dev,
  9339. struct ethtool_channels *channel)
  9340. {
  9341. struct tg3 *tp = netdev_priv(dev);
  9342. u32 deflt_qs = netif_get_num_default_rss_queues();
  9343. channel->max_rx = tp->rxq_max;
  9344. channel->max_tx = tp->txq_max;
  9345. if (netif_running(dev)) {
  9346. channel->rx_count = tp->rxq_cnt;
  9347. channel->tx_count = tp->txq_cnt;
  9348. } else {
  9349. if (tp->rxq_req)
  9350. channel->rx_count = tp->rxq_req;
  9351. else
  9352. channel->rx_count = min(deflt_qs, tp->rxq_max);
  9353. if (tp->txq_req)
  9354. channel->tx_count = tp->txq_req;
  9355. else
  9356. channel->tx_count = min(deflt_qs, tp->txq_max);
  9357. }
  9358. }
  9359. static int tg3_set_channels(struct net_device *dev,
  9360. struct ethtool_channels *channel)
  9361. {
  9362. struct tg3 *tp = netdev_priv(dev);
  9363. if (!tg3_flag(tp, SUPPORT_MSIX))
  9364. return -EOPNOTSUPP;
  9365. if (channel->rx_count > tp->rxq_max ||
  9366. channel->tx_count > tp->txq_max)
  9367. return -EINVAL;
  9368. tp->rxq_req = channel->rx_count;
  9369. tp->txq_req = channel->tx_count;
  9370. if (!netif_running(dev))
  9371. return 0;
  9372. tg3_stop(tp);
  9373. netif_carrier_off(dev);
  9374. tg3_start(tp, true, false);
  9375. return 0;
  9376. }
  9377. static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
  9378. {
  9379. switch (stringset) {
  9380. case ETH_SS_STATS:
  9381. memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
  9382. break;
  9383. case ETH_SS_TEST:
  9384. memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
  9385. break;
  9386. default:
  9387. WARN_ON(1); /* we need a WARN() */
  9388. break;
  9389. }
  9390. }
  9391. static int tg3_set_phys_id(struct net_device *dev,
  9392. enum ethtool_phys_id_state state)
  9393. {
  9394. struct tg3 *tp = netdev_priv(dev);
  9395. if (!netif_running(tp->dev))
  9396. return -EAGAIN;
  9397. switch (state) {
  9398. case ETHTOOL_ID_ACTIVE:
  9399. return 1; /* cycle on/off once per second */
  9400. case ETHTOOL_ID_ON:
  9401. tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
  9402. LED_CTRL_1000MBPS_ON |
  9403. LED_CTRL_100MBPS_ON |
  9404. LED_CTRL_10MBPS_ON |
  9405. LED_CTRL_TRAFFIC_OVERRIDE |
  9406. LED_CTRL_TRAFFIC_BLINK |
  9407. LED_CTRL_TRAFFIC_LED);
  9408. break;
  9409. case ETHTOOL_ID_OFF:
  9410. tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
  9411. LED_CTRL_TRAFFIC_OVERRIDE);
  9412. break;
  9413. case ETHTOOL_ID_INACTIVE:
  9414. tw32(MAC_LED_CTRL, tp->led_ctrl);
  9415. break;
  9416. }
  9417. return 0;
  9418. }
  9419. static void tg3_get_ethtool_stats(struct net_device *dev,
  9420. struct ethtool_stats *estats, u64 *tmp_stats)
  9421. {
  9422. struct tg3 *tp = netdev_priv(dev);
  9423. if (tp->hw_stats)
  9424. tg3_get_estats(tp, (struct tg3_ethtool_stats *)tmp_stats);
  9425. else
  9426. memset(tmp_stats, 0, sizeof(struct tg3_ethtool_stats));
  9427. }
  9428. static __be32 *tg3_vpd_readblock(struct tg3 *tp, u32 *vpdlen)
  9429. {
  9430. int i;
  9431. __be32 *buf;
  9432. u32 offset = 0, len = 0;
  9433. u32 magic, val;
  9434. if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &magic))
  9435. return NULL;
  9436. if (magic == TG3_EEPROM_MAGIC) {
  9437. for (offset = TG3_NVM_DIR_START;
  9438. offset < TG3_NVM_DIR_END;
  9439. offset += TG3_NVM_DIRENT_SIZE) {
  9440. if (tg3_nvram_read(tp, offset, &val))
  9441. return NULL;
  9442. if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
  9443. TG3_NVM_DIRTYPE_EXTVPD)
  9444. break;
  9445. }
  9446. if (offset != TG3_NVM_DIR_END) {
  9447. len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
  9448. if (tg3_nvram_read(tp, offset + 4, &offset))
  9449. return NULL;
  9450. offset = tg3_nvram_logical_addr(tp, offset);
  9451. }
  9452. }
  9453. if (!offset || !len) {
  9454. offset = TG3_NVM_VPD_OFF;
  9455. len = TG3_NVM_VPD_LEN;
  9456. }
  9457. buf = kmalloc(len, GFP_KERNEL);
  9458. if (buf == NULL)
  9459. return NULL;
  9460. if (magic == TG3_EEPROM_MAGIC) {
  9461. for (i = 0; i < len; i += 4) {
  9462. /* The data is in little-endian format in NVRAM.
  9463. * Use the big-endian read routines to preserve
  9464. * the byte order as it exists in NVRAM.
  9465. */
  9466. if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
  9467. goto error;
  9468. }
  9469. } else {
  9470. u8 *ptr;
  9471. ssize_t cnt;
  9472. unsigned int pos = 0;
  9473. ptr = (u8 *)&buf[0];
  9474. for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
  9475. cnt = pci_read_vpd(tp->pdev, pos,
  9476. len - pos, ptr);
  9477. if (cnt == -ETIMEDOUT || cnt == -EINTR)
  9478. cnt = 0;
  9479. else if (cnt < 0)
  9480. goto error;
  9481. }
  9482. if (pos != len)
  9483. goto error;
  9484. }
  9485. *vpdlen = len;
  9486. return buf;
  9487. error:
  9488. kfree(buf);
  9489. return NULL;
  9490. }
  9491. #define NVRAM_TEST_SIZE 0x100
  9492. #define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
  9493. #define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
  9494. #define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
  9495. #define NVRAM_SELFBOOT_FORMAT1_4_SIZE 0x20
  9496. #define NVRAM_SELFBOOT_FORMAT1_5_SIZE 0x24
  9497. #define NVRAM_SELFBOOT_FORMAT1_6_SIZE 0x50
  9498. #define NVRAM_SELFBOOT_HW_SIZE 0x20
  9499. #define NVRAM_SELFBOOT_DATA_SIZE 0x1c
  9500. static int tg3_test_nvram(struct tg3 *tp)
  9501. {
  9502. u32 csum, magic, len;
  9503. __be32 *buf;
  9504. int i, j, k, err = 0, size;
  9505. if (tg3_flag(tp, NO_NVRAM))
  9506. return 0;
  9507. if (tg3_nvram_read(tp, 0, &magic) != 0)
  9508. return -EIO;
  9509. if (magic == TG3_EEPROM_MAGIC)
  9510. size = NVRAM_TEST_SIZE;
  9511. else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
  9512. if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
  9513. TG3_EEPROM_SB_FORMAT_1) {
  9514. switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
  9515. case TG3_EEPROM_SB_REVISION_0:
  9516. size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
  9517. break;
  9518. case TG3_EEPROM_SB_REVISION_2:
  9519. size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
  9520. break;
  9521. case TG3_EEPROM_SB_REVISION_3:
  9522. size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
  9523. break;
  9524. case TG3_EEPROM_SB_REVISION_4:
  9525. size = NVRAM_SELFBOOT_FORMAT1_4_SIZE;
  9526. break;
  9527. case TG3_EEPROM_SB_REVISION_5:
  9528. size = NVRAM_SELFBOOT_FORMAT1_5_SIZE;
  9529. break;
  9530. case TG3_EEPROM_SB_REVISION_6:
  9531. size = NVRAM_SELFBOOT_FORMAT1_6_SIZE;
  9532. break;
  9533. default:
  9534. return -EIO;
  9535. }
  9536. } else
  9537. return 0;
  9538. } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
  9539. size = NVRAM_SELFBOOT_HW_SIZE;
  9540. else
  9541. return -EIO;
  9542. buf = kmalloc(size, GFP_KERNEL);
  9543. if (buf == NULL)
  9544. return -ENOMEM;
  9545. err = -EIO;
  9546. for (i = 0, j = 0; i < size; i += 4, j++) {
  9547. err = tg3_nvram_read_be32(tp, i, &buf[j]);
  9548. if (err)
  9549. break;
  9550. }
  9551. if (i < size)
  9552. goto out;
  9553. /* Selfboot format */
  9554. magic = be32_to_cpu(buf[0]);
  9555. if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
  9556. TG3_EEPROM_MAGIC_FW) {
  9557. u8 *buf8 = (u8 *) buf, csum8 = 0;
  9558. if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
  9559. TG3_EEPROM_SB_REVISION_2) {
  9560. /* For rev 2, the csum doesn't include the MBA. */
  9561. for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
  9562. csum8 += buf8[i];
  9563. for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
  9564. csum8 += buf8[i];
  9565. } else {
  9566. for (i = 0; i < size; i++)
  9567. csum8 += buf8[i];
  9568. }
  9569. if (csum8 == 0) {
  9570. err = 0;
  9571. goto out;
  9572. }
  9573. err = -EIO;
  9574. goto out;
  9575. }
  9576. if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
  9577. TG3_EEPROM_MAGIC_HW) {
  9578. u8 data[NVRAM_SELFBOOT_DATA_SIZE];
  9579. u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
  9580. u8 *buf8 = (u8 *) buf;
  9581. /* Separate the parity bits and the data bytes. */
  9582. for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
  9583. if ((i == 0) || (i == 8)) {
  9584. int l;
  9585. u8 msk;
  9586. for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
  9587. parity[k++] = buf8[i] & msk;
  9588. i++;
  9589. } else if (i == 16) {
  9590. int l;
  9591. u8 msk;
  9592. for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
  9593. parity[k++] = buf8[i] & msk;
  9594. i++;
  9595. for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
  9596. parity[k++] = buf8[i] & msk;
  9597. i++;
  9598. }
  9599. data[j++] = buf8[i];
  9600. }
  9601. err = -EIO;
  9602. for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
  9603. u8 hw8 = hweight8(data[i]);
  9604. if ((hw8 & 0x1) && parity[i])
  9605. goto out;
  9606. else if (!(hw8 & 0x1) && !parity[i])
  9607. goto out;
  9608. }
  9609. err = 0;
  9610. goto out;
  9611. }
  9612. err = -EIO;
  9613. /* Bootstrap checksum at offset 0x10 */
  9614. csum = calc_crc((unsigned char *) buf, 0x10);
  9615. if (csum != le32_to_cpu(buf[0x10/4]))
  9616. goto out;
  9617. /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
  9618. csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
  9619. if (csum != le32_to_cpu(buf[0xfc/4]))
  9620. goto out;
  9621. kfree(buf);
  9622. buf = tg3_vpd_readblock(tp, &len);
  9623. if (!buf)
  9624. return -ENOMEM;
  9625. i = pci_vpd_find_tag((u8 *)buf, 0, len, PCI_VPD_LRDT_RO_DATA);
  9626. if (i > 0) {
  9627. j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
  9628. if (j < 0)
  9629. goto out;
  9630. if (i + PCI_VPD_LRDT_TAG_SIZE + j > len)
  9631. goto out;
  9632. i += PCI_VPD_LRDT_TAG_SIZE;
  9633. j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
  9634. PCI_VPD_RO_KEYWORD_CHKSUM);
  9635. if (j > 0) {
  9636. u8 csum8 = 0;
  9637. j += PCI_VPD_INFO_FLD_HDR_SIZE;
  9638. for (i = 0; i <= j; i++)
  9639. csum8 += ((u8 *)buf)[i];
  9640. if (csum8)
  9641. goto out;
  9642. }
  9643. }
  9644. err = 0;
  9645. out:
  9646. kfree(buf);
  9647. return err;
  9648. }
  9649. #define TG3_SERDES_TIMEOUT_SEC 2
  9650. #define TG3_COPPER_TIMEOUT_SEC 6
  9651. static int tg3_test_link(struct tg3 *tp)
  9652. {
  9653. int i, max;
  9654. if (!netif_running(tp->dev))
  9655. return -ENODEV;
  9656. if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
  9657. max = TG3_SERDES_TIMEOUT_SEC;
  9658. else
  9659. max = TG3_COPPER_TIMEOUT_SEC;
  9660. for (i = 0; i < max; i++) {
  9661. if (netif_carrier_ok(tp->dev))
  9662. return 0;
  9663. if (msleep_interruptible(1000))
  9664. break;
  9665. }
  9666. return -EIO;
  9667. }
  9668. /* Only test the commonly used registers */
  9669. static int tg3_test_registers(struct tg3 *tp)
  9670. {
  9671. int i, is_5705, is_5750;
  9672. u32 offset, read_mask, write_mask, val, save_val, read_val;
  9673. static struct {
  9674. u16 offset;
  9675. u16 flags;
  9676. #define TG3_FL_5705 0x1
  9677. #define TG3_FL_NOT_5705 0x2
  9678. #define TG3_FL_NOT_5788 0x4
  9679. #define TG3_FL_NOT_5750 0x8
  9680. u32 read_mask;
  9681. u32 write_mask;
  9682. } reg_tbl[] = {
  9683. /* MAC Control Registers */
  9684. { MAC_MODE, TG3_FL_NOT_5705,
  9685. 0x00000000, 0x00ef6f8c },
  9686. { MAC_MODE, TG3_FL_5705,
  9687. 0x00000000, 0x01ef6b8c },
  9688. { MAC_STATUS, TG3_FL_NOT_5705,
  9689. 0x03800107, 0x00000000 },
  9690. { MAC_STATUS, TG3_FL_5705,
  9691. 0x03800100, 0x00000000 },
  9692. { MAC_ADDR_0_HIGH, 0x0000,
  9693. 0x00000000, 0x0000ffff },
  9694. { MAC_ADDR_0_LOW, 0x0000,
  9695. 0x00000000, 0xffffffff },
  9696. { MAC_RX_MTU_SIZE, 0x0000,
  9697. 0x00000000, 0x0000ffff },
  9698. { MAC_TX_MODE, 0x0000,
  9699. 0x00000000, 0x00000070 },
  9700. { MAC_TX_LENGTHS, 0x0000,
  9701. 0x00000000, 0x00003fff },
  9702. { MAC_RX_MODE, TG3_FL_NOT_5705,
  9703. 0x00000000, 0x000007fc },
  9704. { MAC_RX_MODE, TG3_FL_5705,
  9705. 0x00000000, 0x000007dc },
  9706. { MAC_HASH_REG_0, 0x0000,
  9707. 0x00000000, 0xffffffff },
  9708. { MAC_HASH_REG_1, 0x0000,
  9709. 0x00000000, 0xffffffff },
  9710. { MAC_HASH_REG_2, 0x0000,
  9711. 0x00000000, 0xffffffff },
  9712. { MAC_HASH_REG_3, 0x0000,
  9713. 0x00000000, 0xffffffff },
  9714. /* Receive Data and Receive BD Initiator Control Registers. */
  9715. { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
  9716. 0x00000000, 0xffffffff },
  9717. { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
  9718. 0x00000000, 0xffffffff },
  9719. { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
  9720. 0x00000000, 0x00000003 },
  9721. { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
  9722. 0x00000000, 0xffffffff },
  9723. { RCVDBDI_STD_BD+0, 0x0000,
  9724. 0x00000000, 0xffffffff },
  9725. { RCVDBDI_STD_BD+4, 0x0000,
  9726. 0x00000000, 0xffffffff },
  9727. { RCVDBDI_STD_BD+8, 0x0000,
  9728. 0x00000000, 0xffff0002 },
  9729. { RCVDBDI_STD_BD+0xc, 0x0000,
  9730. 0x00000000, 0xffffffff },
  9731. /* Receive BD Initiator Control Registers. */
  9732. { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
  9733. 0x00000000, 0xffffffff },
  9734. { RCVBDI_STD_THRESH, TG3_FL_5705,
  9735. 0x00000000, 0x000003ff },
  9736. { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
  9737. 0x00000000, 0xffffffff },
  9738. /* Host Coalescing Control Registers. */
  9739. { HOSTCC_MODE, TG3_FL_NOT_5705,
  9740. 0x00000000, 0x00000004 },
  9741. { HOSTCC_MODE, TG3_FL_5705,
  9742. 0x00000000, 0x000000f6 },
  9743. { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
  9744. 0x00000000, 0xffffffff },
  9745. { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
  9746. 0x00000000, 0x000003ff },
  9747. { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
  9748. 0x00000000, 0xffffffff },
  9749. { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
  9750. 0x00000000, 0x000003ff },
  9751. { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
  9752. 0x00000000, 0xffffffff },
  9753. { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
  9754. 0x00000000, 0x000000ff },
  9755. { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
  9756. 0x00000000, 0xffffffff },
  9757. { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
  9758. 0x00000000, 0x000000ff },
  9759. { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
  9760. 0x00000000, 0xffffffff },
  9761. { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
  9762. 0x00000000, 0xffffffff },
  9763. { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
  9764. 0x00000000, 0xffffffff },
  9765. { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
  9766. 0x00000000, 0x000000ff },
  9767. { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
  9768. 0x00000000, 0xffffffff },
  9769. { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
  9770. 0x00000000, 0x000000ff },
  9771. { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
  9772. 0x00000000, 0xffffffff },
  9773. { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
  9774. 0x00000000, 0xffffffff },
  9775. { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
  9776. 0x00000000, 0xffffffff },
  9777. { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
  9778. 0x00000000, 0xffffffff },
  9779. { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
  9780. 0x00000000, 0xffffffff },
  9781. { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
  9782. 0xffffffff, 0x00000000 },
  9783. { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
  9784. 0xffffffff, 0x00000000 },
  9785. /* Buffer Manager Control Registers. */
  9786. { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
  9787. 0x00000000, 0x007fff80 },
  9788. { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
  9789. 0x00000000, 0x007fffff },
  9790. { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
  9791. 0x00000000, 0x0000003f },
  9792. { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
  9793. 0x00000000, 0x000001ff },
  9794. { BUFMGR_MB_HIGH_WATER, 0x0000,
  9795. 0x00000000, 0x000001ff },
  9796. { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
  9797. 0xffffffff, 0x00000000 },
  9798. { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
  9799. 0xffffffff, 0x00000000 },
  9800. /* Mailbox Registers */
  9801. { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
  9802. 0x00000000, 0x000001ff },
  9803. { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
  9804. 0x00000000, 0x000001ff },
  9805. { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
  9806. 0x00000000, 0x000007ff },
  9807. { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
  9808. 0x00000000, 0x000001ff },
  9809. { 0xffff, 0x0000, 0x00000000, 0x00000000 },
  9810. };
  9811. is_5705 = is_5750 = 0;
  9812. if (tg3_flag(tp, 5705_PLUS)) {
  9813. is_5705 = 1;
  9814. if (tg3_flag(tp, 5750_PLUS))
  9815. is_5750 = 1;
  9816. }
  9817. for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
  9818. if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
  9819. continue;
  9820. if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
  9821. continue;
  9822. if (tg3_flag(tp, IS_5788) &&
  9823. (reg_tbl[i].flags & TG3_FL_NOT_5788))
  9824. continue;
  9825. if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
  9826. continue;
  9827. offset = (u32) reg_tbl[i].offset;
  9828. read_mask = reg_tbl[i].read_mask;
  9829. write_mask = reg_tbl[i].write_mask;
  9830. /* Save the original register content */
  9831. save_val = tr32(offset);
  9832. /* Determine the read-only value. */
  9833. read_val = save_val & read_mask;
  9834. /* Write zero to the register, then make sure the read-only bits
  9835. * are not changed and the read/write bits are all zeros.
  9836. */
  9837. tw32(offset, 0);
  9838. val = tr32(offset);
  9839. /* Test the read-only and read/write bits. */
  9840. if (((val & read_mask) != read_val) || (val & write_mask))
  9841. goto out;
  9842. /* Write ones to all the bits defined by RdMask and WrMask, then
  9843. * make sure the read-only bits are not changed and the
  9844. * read/write bits are all ones.
  9845. */
  9846. tw32(offset, read_mask | write_mask);
  9847. val = tr32(offset);
  9848. /* Test the read-only bits. */
  9849. if ((val & read_mask) != read_val)
  9850. goto out;
  9851. /* Test the read/write bits. */
  9852. if ((val & write_mask) != write_mask)
  9853. goto out;
  9854. tw32(offset, save_val);
  9855. }
  9856. return 0;
  9857. out:
  9858. if (netif_msg_hw(tp))
  9859. netdev_err(tp->dev,
  9860. "Register test failed at offset %x\n", offset);
  9861. tw32(offset, save_val);
  9862. return -EIO;
  9863. }
  9864. static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
  9865. {
  9866. static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
  9867. int i;
  9868. u32 j;
  9869. for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
  9870. for (j = 0; j < len; j += 4) {
  9871. u32 val;
  9872. tg3_write_mem(tp, offset + j, test_pattern[i]);
  9873. tg3_read_mem(tp, offset + j, &val);
  9874. if (val != test_pattern[i])
  9875. return -EIO;
  9876. }
  9877. }
  9878. return 0;
  9879. }
  9880. static int tg3_test_memory(struct tg3 *tp)
  9881. {
  9882. static struct mem_entry {
  9883. u32 offset;
  9884. u32 len;
  9885. } mem_tbl_570x[] = {
  9886. { 0x00000000, 0x00b50},
  9887. { 0x00002000, 0x1c000},
  9888. { 0xffffffff, 0x00000}
  9889. }, mem_tbl_5705[] = {
  9890. { 0x00000100, 0x0000c},
  9891. { 0x00000200, 0x00008},
  9892. { 0x00004000, 0x00800},
  9893. { 0x00006000, 0x01000},
  9894. { 0x00008000, 0x02000},
  9895. { 0x00010000, 0x0e000},
  9896. { 0xffffffff, 0x00000}
  9897. }, mem_tbl_5755[] = {
  9898. { 0x00000200, 0x00008},
  9899. { 0x00004000, 0x00800},
  9900. { 0x00006000, 0x00800},
  9901. { 0x00008000, 0x02000},
  9902. { 0x00010000, 0x0c000},
  9903. { 0xffffffff, 0x00000}
  9904. }, mem_tbl_5906[] = {
  9905. { 0x00000200, 0x00008},
  9906. { 0x00004000, 0x00400},
  9907. { 0x00006000, 0x00400},
  9908. { 0x00008000, 0x01000},
  9909. { 0x00010000, 0x01000},
  9910. { 0xffffffff, 0x00000}
  9911. }, mem_tbl_5717[] = {
  9912. { 0x00000200, 0x00008},
  9913. { 0x00010000, 0x0a000},
  9914. { 0x00020000, 0x13c00},
  9915. { 0xffffffff, 0x00000}
  9916. }, mem_tbl_57765[] = {
  9917. { 0x00000200, 0x00008},
  9918. { 0x00004000, 0x00800},
  9919. { 0x00006000, 0x09800},
  9920. { 0x00010000, 0x0a000},
  9921. { 0xffffffff, 0x00000}
  9922. };
  9923. struct mem_entry *mem_tbl;
  9924. int err = 0;
  9925. int i;
  9926. if (tg3_flag(tp, 5717_PLUS))
  9927. mem_tbl = mem_tbl_5717;
  9928. else if (tg3_flag(tp, 57765_CLASS))
  9929. mem_tbl = mem_tbl_57765;
  9930. else if (tg3_flag(tp, 5755_PLUS))
  9931. mem_tbl = mem_tbl_5755;
  9932. else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
  9933. mem_tbl = mem_tbl_5906;
  9934. else if (tg3_flag(tp, 5705_PLUS))
  9935. mem_tbl = mem_tbl_5705;
  9936. else
  9937. mem_tbl = mem_tbl_570x;
  9938. for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
  9939. err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
  9940. if (err)
  9941. break;
  9942. }
  9943. return err;
  9944. }
  9945. #define TG3_TSO_MSS 500
  9946. #define TG3_TSO_IP_HDR_LEN 20
  9947. #define TG3_TSO_TCP_HDR_LEN 20
  9948. #define TG3_TSO_TCP_OPT_LEN 12
  9949. static const u8 tg3_tso_header[] = {
  9950. 0x08, 0x00,
  9951. 0x45, 0x00, 0x00, 0x00,
  9952. 0x00, 0x00, 0x40, 0x00,
  9953. 0x40, 0x06, 0x00, 0x00,
  9954. 0x0a, 0x00, 0x00, 0x01,
  9955. 0x0a, 0x00, 0x00, 0x02,
  9956. 0x0d, 0x00, 0xe0, 0x00,
  9957. 0x00, 0x00, 0x01, 0x00,
  9958. 0x00, 0x00, 0x02, 0x00,
  9959. 0x80, 0x10, 0x10, 0x00,
  9960. 0x14, 0x09, 0x00, 0x00,
  9961. 0x01, 0x01, 0x08, 0x0a,
  9962. 0x11, 0x11, 0x11, 0x11,
  9963. 0x11, 0x11, 0x11, 0x11,
  9964. };
  9965. static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, bool tso_loopback)
  9966. {
  9967. u32 rx_start_idx, rx_idx, tx_idx, opaque_key;
  9968. u32 base_flags = 0, mss = 0, desc_idx, coal_now, data_off, val;
  9969. u32 budget;
  9970. struct sk_buff *skb;
  9971. u8 *tx_data, *rx_data;
  9972. dma_addr_t map;
  9973. int num_pkts, tx_len, rx_len, i, err;
  9974. struct tg3_rx_buffer_desc *desc;
  9975. struct tg3_napi *tnapi, *rnapi;
  9976. struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
  9977. tnapi = &tp->napi[0];
  9978. rnapi = &tp->napi[0];
  9979. if (tp->irq_cnt > 1) {
  9980. if (tg3_flag(tp, ENABLE_RSS))
  9981. rnapi = &tp->napi[1];
  9982. if (tg3_flag(tp, ENABLE_TSS))
  9983. tnapi = &tp->napi[1];
  9984. }
  9985. coal_now = tnapi->coal_now | rnapi->coal_now;
  9986. err = -EIO;
  9987. tx_len = pktsz;
  9988. skb = netdev_alloc_skb(tp->dev, tx_len);
  9989. if (!skb)
  9990. return -ENOMEM;
  9991. tx_data = skb_put(skb, tx_len);
  9992. memcpy(tx_data, tp->dev->dev_addr, 6);
  9993. memset(tx_data + 6, 0x0, 8);
  9994. tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
  9995. if (tso_loopback) {
  9996. struct iphdr *iph = (struct iphdr *)&tx_data[ETH_HLEN];
  9997. u32 hdr_len = TG3_TSO_IP_HDR_LEN + TG3_TSO_TCP_HDR_LEN +
  9998. TG3_TSO_TCP_OPT_LEN;
  9999. memcpy(tx_data + ETH_ALEN * 2, tg3_tso_header,
  10000. sizeof(tg3_tso_header));
  10001. mss = TG3_TSO_MSS;
  10002. val = tx_len - ETH_ALEN * 2 - sizeof(tg3_tso_header);
  10003. num_pkts = DIV_ROUND_UP(val, TG3_TSO_MSS);
  10004. /* Set the total length field in the IP header */
  10005. iph->tot_len = htons((u16)(mss + hdr_len));
  10006. base_flags = (TXD_FLAG_CPU_PRE_DMA |
  10007. TXD_FLAG_CPU_POST_DMA);
  10008. if (tg3_flag(tp, HW_TSO_1) ||
  10009. tg3_flag(tp, HW_TSO_2) ||
  10010. tg3_flag(tp, HW_TSO_3)) {
  10011. struct tcphdr *th;
  10012. val = ETH_HLEN + TG3_TSO_IP_HDR_LEN;
  10013. th = (struct tcphdr *)&tx_data[val];
  10014. th->check = 0;
  10015. } else
  10016. base_flags |= TXD_FLAG_TCPUDP_CSUM;
  10017. if (tg3_flag(tp, HW_TSO_3)) {
  10018. mss |= (hdr_len & 0xc) << 12;
  10019. if (hdr_len & 0x10)
  10020. base_flags |= 0x00000010;
  10021. base_flags |= (hdr_len & 0x3e0) << 5;
  10022. } else if (tg3_flag(tp, HW_TSO_2))
  10023. mss |= hdr_len << 9;
  10024. else if (tg3_flag(tp, HW_TSO_1) ||
  10025. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
  10026. mss |= (TG3_TSO_TCP_OPT_LEN << 9);
  10027. } else {
  10028. base_flags |= (TG3_TSO_TCP_OPT_LEN << 10);
  10029. }
  10030. data_off = ETH_ALEN * 2 + sizeof(tg3_tso_header);
  10031. } else {
  10032. num_pkts = 1;
  10033. data_off = ETH_HLEN;
  10034. if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
  10035. tx_len > VLAN_ETH_FRAME_LEN)
  10036. base_flags |= TXD_FLAG_JMB_PKT;
  10037. }
  10038. for (i = data_off; i < tx_len; i++)
  10039. tx_data[i] = (u8) (i & 0xff);
  10040. map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
  10041. if (pci_dma_mapping_error(tp->pdev, map)) {
  10042. dev_kfree_skb(skb);
  10043. return -EIO;
  10044. }
  10045. val = tnapi->tx_prod;
  10046. tnapi->tx_buffers[val].skb = skb;
  10047. dma_unmap_addr_set(&tnapi->tx_buffers[val], mapping, map);
  10048. tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
  10049. rnapi->coal_now);
  10050. udelay(10);
  10051. rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
  10052. budget = tg3_tx_avail(tnapi);
  10053. if (tg3_tx_frag_set(tnapi, &val, &budget, map, tx_len,
  10054. base_flags | TXD_FLAG_END, mss, 0)) {
  10055. tnapi->tx_buffers[val].skb = NULL;
  10056. dev_kfree_skb(skb);
  10057. return -EIO;
  10058. }
  10059. tnapi->tx_prod++;
  10060. /* Sync BD data before updating mailbox */
  10061. wmb();
  10062. tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
  10063. tr32_mailbox(tnapi->prodmbox);
  10064. udelay(10);
  10065. /* 350 usec to allow enough time on some 10/100 Mbps devices. */
  10066. for (i = 0; i < 35; i++) {
  10067. tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
  10068. coal_now);
  10069. udelay(10);
  10070. tx_idx = tnapi->hw_status->idx[0].tx_consumer;
  10071. rx_idx = rnapi->hw_status->idx[0].rx_producer;
  10072. if ((tx_idx == tnapi->tx_prod) &&
  10073. (rx_idx == (rx_start_idx + num_pkts)))
  10074. break;
  10075. }
  10076. tg3_tx_skb_unmap(tnapi, tnapi->tx_prod - 1, -1);
  10077. dev_kfree_skb(skb);
  10078. if (tx_idx != tnapi->tx_prod)
  10079. goto out;
  10080. if (rx_idx != rx_start_idx + num_pkts)
  10081. goto out;
  10082. val = data_off;
  10083. while (rx_idx != rx_start_idx) {
  10084. desc = &rnapi->rx_rcb[rx_start_idx++];
  10085. desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
  10086. opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
  10087. if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
  10088. (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
  10089. goto out;
  10090. rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT)
  10091. - ETH_FCS_LEN;
  10092. if (!tso_loopback) {
  10093. if (rx_len != tx_len)
  10094. goto out;
  10095. if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
  10096. if (opaque_key != RXD_OPAQUE_RING_STD)
  10097. goto out;
  10098. } else {
  10099. if (opaque_key != RXD_OPAQUE_RING_JUMBO)
  10100. goto out;
  10101. }
  10102. } else if ((desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
  10103. (desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
  10104. >> RXD_TCPCSUM_SHIFT != 0xffff) {
  10105. goto out;
  10106. }
  10107. if (opaque_key == RXD_OPAQUE_RING_STD) {
  10108. rx_data = tpr->rx_std_buffers[desc_idx].data;
  10109. map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx],
  10110. mapping);
  10111. } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
  10112. rx_data = tpr->rx_jmb_buffers[desc_idx].data;
  10113. map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx],
  10114. mapping);
  10115. } else
  10116. goto out;
  10117. pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len,
  10118. PCI_DMA_FROMDEVICE);
  10119. rx_data += TG3_RX_OFFSET(tp);
  10120. for (i = data_off; i < rx_len; i++, val++) {
  10121. if (*(rx_data + i) != (u8) (val & 0xff))
  10122. goto out;
  10123. }
  10124. }
  10125. err = 0;
  10126. /* tg3_free_rings will unmap and free the rx_data */
  10127. out:
  10128. return err;
  10129. }
  10130. #define TG3_STD_LOOPBACK_FAILED 1
  10131. #define TG3_JMB_LOOPBACK_FAILED 2
  10132. #define TG3_TSO_LOOPBACK_FAILED 4
  10133. #define TG3_LOOPBACK_FAILED \
  10134. (TG3_STD_LOOPBACK_FAILED | \
  10135. TG3_JMB_LOOPBACK_FAILED | \
  10136. TG3_TSO_LOOPBACK_FAILED)
  10137. static int tg3_test_loopback(struct tg3 *tp, u64 *data, bool do_extlpbk)
  10138. {
  10139. int err = -EIO;
  10140. u32 eee_cap;
  10141. u32 jmb_pkt_sz = 9000;
  10142. if (tp->dma_limit)
  10143. jmb_pkt_sz = tp->dma_limit - ETH_HLEN;
  10144. eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
  10145. tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
  10146. if (!netif_running(tp->dev)) {
  10147. data[0] = TG3_LOOPBACK_FAILED;
  10148. data[1] = TG3_LOOPBACK_FAILED;
  10149. if (do_extlpbk)
  10150. data[2] = TG3_LOOPBACK_FAILED;
  10151. goto done;
  10152. }
  10153. err = tg3_reset_hw(tp, 1);
  10154. if (err) {
  10155. data[0] = TG3_LOOPBACK_FAILED;
  10156. data[1] = TG3_LOOPBACK_FAILED;
  10157. if (do_extlpbk)
  10158. data[2] = TG3_LOOPBACK_FAILED;
  10159. goto done;
  10160. }
  10161. if (tg3_flag(tp, ENABLE_RSS)) {
  10162. int i;
  10163. /* Reroute all rx packets to the 1st queue */
  10164. for (i = MAC_RSS_INDIR_TBL_0;
  10165. i < MAC_RSS_INDIR_TBL_0 + TG3_RSS_INDIR_TBL_SIZE; i += 4)
  10166. tw32(i, 0x0);
  10167. }
  10168. /* HW errata - mac loopback fails in some cases on 5780.
  10169. * Normal traffic and PHY loopback are not affected by
  10170. * errata. Also, the MAC loopback test is deprecated for
  10171. * all newer ASIC revisions.
  10172. */
  10173. if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
  10174. !tg3_flag(tp, CPMU_PRESENT)) {
  10175. tg3_mac_loopback(tp, true);
  10176. if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
  10177. data[0] |= TG3_STD_LOOPBACK_FAILED;
  10178. if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
  10179. tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
  10180. data[0] |= TG3_JMB_LOOPBACK_FAILED;
  10181. tg3_mac_loopback(tp, false);
  10182. }
  10183. if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
  10184. !tg3_flag(tp, USE_PHYLIB)) {
  10185. int i;
  10186. tg3_phy_lpbk_set(tp, 0, false);
  10187. /* Wait for link */
  10188. for (i = 0; i < 100; i++) {
  10189. if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
  10190. break;
  10191. mdelay(1);
  10192. }
  10193. if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
  10194. data[1] |= TG3_STD_LOOPBACK_FAILED;
  10195. if (tg3_flag(tp, TSO_CAPABLE) &&
  10196. tg3_run_loopback(tp, ETH_FRAME_LEN, true))
  10197. data[1] |= TG3_TSO_LOOPBACK_FAILED;
  10198. if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
  10199. tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
  10200. data[1] |= TG3_JMB_LOOPBACK_FAILED;
  10201. if (do_extlpbk) {
  10202. tg3_phy_lpbk_set(tp, 0, true);
  10203. /* All link indications report up, but the hardware
  10204. * isn't really ready for about 20 msec. Double it
  10205. * to be sure.
  10206. */
  10207. mdelay(40);
  10208. if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
  10209. data[2] |= TG3_STD_LOOPBACK_FAILED;
  10210. if (tg3_flag(tp, TSO_CAPABLE) &&
  10211. tg3_run_loopback(tp, ETH_FRAME_LEN, true))
  10212. data[2] |= TG3_TSO_LOOPBACK_FAILED;
  10213. if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
  10214. tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
  10215. data[2] |= TG3_JMB_LOOPBACK_FAILED;
  10216. }
  10217. /* Re-enable gphy autopowerdown. */
  10218. if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
  10219. tg3_phy_toggle_apd(tp, true);
  10220. }
  10221. err = (data[0] | data[1] | data[2]) ? -EIO : 0;
  10222. done:
  10223. tp->phy_flags |= eee_cap;
  10224. return err;
  10225. }
  10226. static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
  10227. u64 *data)
  10228. {
  10229. struct tg3 *tp = netdev_priv(dev);
  10230. bool doextlpbk = etest->flags & ETH_TEST_FL_EXTERNAL_LB;
  10231. if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
  10232. tg3_power_up(tp)) {
  10233. etest->flags |= ETH_TEST_FL_FAILED;
  10234. memset(data, 1, sizeof(u64) * TG3_NUM_TEST);
  10235. return;
  10236. }
  10237. memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
  10238. if (tg3_test_nvram(tp) != 0) {
  10239. etest->flags |= ETH_TEST_FL_FAILED;
  10240. data[0] = 1;
  10241. }
  10242. if (!doextlpbk && tg3_test_link(tp)) {
  10243. etest->flags |= ETH_TEST_FL_FAILED;
  10244. data[1] = 1;
  10245. }
  10246. if (etest->flags & ETH_TEST_FL_OFFLINE) {
  10247. int err, err2 = 0, irq_sync = 0;
  10248. if (netif_running(dev)) {
  10249. tg3_phy_stop(tp);
  10250. tg3_netif_stop(tp);
  10251. irq_sync = 1;
  10252. }
  10253. tg3_full_lock(tp, irq_sync);
  10254. tg3_halt(tp, RESET_KIND_SUSPEND, 1);
  10255. err = tg3_nvram_lock(tp);
  10256. tg3_halt_cpu(tp, RX_CPU_BASE);
  10257. if (!tg3_flag(tp, 5705_PLUS))
  10258. tg3_halt_cpu(tp, TX_CPU_BASE);
  10259. if (!err)
  10260. tg3_nvram_unlock(tp);
  10261. if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
  10262. tg3_phy_reset(tp);
  10263. if (tg3_test_registers(tp) != 0) {
  10264. etest->flags |= ETH_TEST_FL_FAILED;
  10265. data[2] = 1;
  10266. }
  10267. if (tg3_test_memory(tp) != 0) {
  10268. etest->flags |= ETH_TEST_FL_FAILED;
  10269. data[3] = 1;
  10270. }
  10271. if (doextlpbk)
  10272. etest->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE;
  10273. if (tg3_test_loopback(tp, &data[4], doextlpbk))
  10274. etest->flags |= ETH_TEST_FL_FAILED;
  10275. tg3_full_unlock(tp);
  10276. if (tg3_test_interrupt(tp) != 0) {
  10277. etest->flags |= ETH_TEST_FL_FAILED;
  10278. data[7] = 1;
  10279. }
  10280. tg3_full_lock(tp, 0);
  10281. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  10282. if (netif_running(dev)) {
  10283. tg3_flag_set(tp, INIT_COMPLETE);
  10284. err2 = tg3_restart_hw(tp, 1);
  10285. if (!err2)
  10286. tg3_netif_start(tp);
  10287. }
  10288. tg3_full_unlock(tp);
  10289. if (irq_sync && !err2)
  10290. tg3_phy_start(tp);
  10291. }
  10292. if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
  10293. tg3_power_down(tp);
  10294. }
  10295. static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  10296. {
  10297. struct mii_ioctl_data *data = if_mii(ifr);
  10298. struct tg3 *tp = netdev_priv(dev);
  10299. int err;
  10300. if (tg3_flag(tp, USE_PHYLIB)) {
  10301. struct phy_device *phydev;
  10302. if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
  10303. return -EAGAIN;
  10304. phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
  10305. return phy_mii_ioctl(phydev, ifr, cmd);
  10306. }
  10307. switch (cmd) {
  10308. case SIOCGMIIPHY:
  10309. data->phy_id = tp->phy_addr;
  10310. /* fallthru */
  10311. case SIOCGMIIREG: {
  10312. u32 mii_regval;
  10313. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
  10314. break; /* We have no PHY */
  10315. if (!netif_running(dev))
  10316. return -EAGAIN;
  10317. spin_lock_bh(&tp->lock);
  10318. err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
  10319. spin_unlock_bh(&tp->lock);
  10320. data->val_out = mii_regval;
  10321. return err;
  10322. }
  10323. case SIOCSMIIREG:
  10324. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
  10325. break; /* We have no PHY */
  10326. if (!netif_running(dev))
  10327. return -EAGAIN;
  10328. spin_lock_bh(&tp->lock);
  10329. err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
  10330. spin_unlock_bh(&tp->lock);
  10331. return err;
  10332. default:
  10333. /* do nothing */
  10334. break;
  10335. }
  10336. return -EOPNOTSUPP;
  10337. }
  10338. static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
  10339. {
  10340. struct tg3 *tp = netdev_priv(dev);
  10341. memcpy(ec, &tp->coal, sizeof(*ec));
  10342. return 0;
  10343. }
  10344. static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
  10345. {
  10346. struct tg3 *tp = netdev_priv(dev);
  10347. u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
  10348. u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
  10349. if (!tg3_flag(tp, 5705_PLUS)) {
  10350. max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
  10351. max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
  10352. max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
  10353. min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
  10354. }
  10355. if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
  10356. (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
  10357. (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
  10358. (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
  10359. (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
  10360. (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
  10361. (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
  10362. (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
  10363. (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
  10364. (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
  10365. return -EINVAL;
  10366. /* No rx interrupts will be generated if both are zero */
  10367. if ((ec->rx_coalesce_usecs == 0) &&
  10368. (ec->rx_max_coalesced_frames == 0))
  10369. return -EINVAL;
  10370. /* No tx interrupts will be generated if both are zero */
  10371. if ((ec->tx_coalesce_usecs == 0) &&
  10372. (ec->tx_max_coalesced_frames == 0))
  10373. return -EINVAL;
  10374. /* Only copy relevant parameters, ignore all others. */
  10375. tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
  10376. tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
  10377. tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
  10378. tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
  10379. tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
  10380. tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
  10381. tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
  10382. tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
  10383. tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
  10384. if (netif_running(dev)) {
  10385. tg3_full_lock(tp, 0);
  10386. __tg3_set_coalesce(tp, &tp->coal);
  10387. tg3_full_unlock(tp);
  10388. }
  10389. return 0;
  10390. }
  10391. static const struct ethtool_ops tg3_ethtool_ops = {
  10392. .get_settings = tg3_get_settings,
  10393. .set_settings = tg3_set_settings,
  10394. .get_drvinfo = tg3_get_drvinfo,
  10395. .get_regs_len = tg3_get_regs_len,
  10396. .get_regs = tg3_get_regs,
  10397. .get_wol = tg3_get_wol,
  10398. .set_wol = tg3_set_wol,
  10399. .get_msglevel = tg3_get_msglevel,
  10400. .set_msglevel = tg3_set_msglevel,
  10401. .nway_reset = tg3_nway_reset,
  10402. .get_link = ethtool_op_get_link,
  10403. .get_eeprom_len = tg3_get_eeprom_len,
  10404. .get_eeprom = tg3_get_eeprom,
  10405. .set_eeprom = tg3_set_eeprom,
  10406. .get_ringparam = tg3_get_ringparam,
  10407. .set_ringparam = tg3_set_ringparam,
  10408. .get_pauseparam = tg3_get_pauseparam,
  10409. .set_pauseparam = tg3_set_pauseparam,
  10410. .self_test = tg3_self_test,
  10411. .get_strings = tg3_get_strings,
  10412. .set_phys_id = tg3_set_phys_id,
  10413. .get_ethtool_stats = tg3_get_ethtool_stats,
  10414. .get_coalesce = tg3_get_coalesce,
  10415. .set_coalesce = tg3_set_coalesce,
  10416. .get_sset_count = tg3_get_sset_count,
  10417. .get_rxnfc = tg3_get_rxnfc,
  10418. .get_rxfh_indir_size = tg3_get_rxfh_indir_size,
  10419. .get_rxfh_indir = tg3_get_rxfh_indir,
  10420. .set_rxfh_indir = tg3_set_rxfh_indir,
  10421. .get_channels = tg3_get_channels,
  10422. .set_channels = tg3_set_channels,
  10423. .get_ts_info = ethtool_op_get_ts_info,
  10424. };
  10425. static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
  10426. struct rtnl_link_stats64 *stats)
  10427. {
  10428. struct tg3 *tp = netdev_priv(dev);
  10429. spin_lock_bh(&tp->lock);
  10430. if (!tp->hw_stats) {
  10431. spin_unlock_bh(&tp->lock);
  10432. return &tp->net_stats_prev;
  10433. }
  10434. tg3_get_nstats(tp, stats);
  10435. spin_unlock_bh(&tp->lock);
  10436. return stats;
  10437. }
  10438. static void tg3_set_rx_mode(struct net_device *dev)
  10439. {
  10440. struct tg3 *tp = netdev_priv(dev);
  10441. if (!netif_running(dev))
  10442. return;
  10443. tg3_full_lock(tp, 0);
  10444. __tg3_set_rx_mode(dev);
  10445. tg3_full_unlock(tp);
  10446. }
  10447. static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
  10448. int new_mtu)
  10449. {
  10450. dev->mtu = new_mtu;
  10451. if (new_mtu > ETH_DATA_LEN) {
  10452. if (tg3_flag(tp, 5780_CLASS)) {
  10453. netdev_update_features(dev);
  10454. tg3_flag_clear(tp, TSO_CAPABLE);
  10455. } else {
  10456. tg3_flag_set(tp, JUMBO_RING_ENABLE);
  10457. }
  10458. } else {
  10459. if (tg3_flag(tp, 5780_CLASS)) {
  10460. tg3_flag_set(tp, TSO_CAPABLE);
  10461. netdev_update_features(dev);
  10462. }
  10463. tg3_flag_clear(tp, JUMBO_RING_ENABLE);
  10464. }
  10465. }
  10466. static int tg3_change_mtu(struct net_device *dev, int new_mtu)
  10467. {
  10468. struct tg3 *tp = netdev_priv(dev);
  10469. int err, reset_phy = 0;
  10470. if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
  10471. return -EINVAL;
  10472. if (!netif_running(dev)) {
  10473. /* We'll just catch it later when the
  10474. * device is up'd.
  10475. */
  10476. tg3_set_mtu(dev, tp, new_mtu);
  10477. return 0;
  10478. }
  10479. tg3_phy_stop(tp);
  10480. tg3_netif_stop(tp);
  10481. tg3_full_lock(tp, 1);
  10482. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  10483. tg3_set_mtu(dev, tp, new_mtu);
  10484. /* Reset PHY, otherwise the read DMA engine will be in a mode that
  10485. * breaks all requests to 256 bytes.
  10486. */
  10487. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57766)
  10488. reset_phy = 1;
  10489. err = tg3_restart_hw(tp, reset_phy);
  10490. if (!err)
  10491. tg3_netif_start(tp);
  10492. tg3_full_unlock(tp);
  10493. if (!err)
  10494. tg3_phy_start(tp);
  10495. return err;
  10496. }
  10497. static const struct net_device_ops tg3_netdev_ops = {
  10498. .ndo_open = tg3_open,
  10499. .ndo_stop = tg3_close,
  10500. .ndo_start_xmit = tg3_start_xmit,
  10501. .ndo_get_stats64 = tg3_get_stats64,
  10502. .ndo_validate_addr = eth_validate_addr,
  10503. .ndo_set_rx_mode = tg3_set_rx_mode,
  10504. .ndo_set_mac_address = tg3_set_mac_addr,
  10505. .ndo_do_ioctl = tg3_ioctl,
  10506. .ndo_tx_timeout = tg3_tx_timeout,
  10507. .ndo_change_mtu = tg3_change_mtu,
  10508. .ndo_fix_features = tg3_fix_features,
  10509. .ndo_set_features = tg3_set_features,
  10510. #ifdef CONFIG_NET_POLL_CONTROLLER
  10511. .ndo_poll_controller = tg3_poll_controller,
  10512. #endif
  10513. };
  10514. static void __devinit tg3_get_eeprom_size(struct tg3 *tp)
  10515. {
  10516. u32 cursize, val, magic;
  10517. tp->nvram_size = EEPROM_CHIP_SIZE;
  10518. if (tg3_nvram_read(tp, 0, &magic) != 0)
  10519. return;
  10520. if ((magic != TG3_EEPROM_MAGIC) &&
  10521. ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
  10522. ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
  10523. return;
  10524. /*
  10525. * Size the chip by reading offsets at increasing powers of two.
  10526. * When we encounter our validation signature, we know the addressing
  10527. * has wrapped around, and thus have our chip size.
  10528. */
  10529. cursize = 0x10;
  10530. while (cursize < tp->nvram_size) {
  10531. if (tg3_nvram_read(tp, cursize, &val) != 0)
  10532. return;
  10533. if (val == magic)
  10534. break;
  10535. cursize <<= 1;
  10536. }
  10537. tp->nvram_size = cursize;
  10538. }
  10539. static void __devinit tg3_get_nvram_size(struct tg3 *tp)
  10540. {
  10541. u32 val;
  10542. if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &val) != 0)
  10543. return;
  10544. /* Selfboot format */
  10545. if (val != TG3_EEPROM_MAGIC) {
  10546. tg3_get_eeprom_size(tp);
  10547. return;
  10548. }
  10549. if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
  10550. if (val != 0) {
  10551. /* This is confusing. We want to operate on the
  10552. * 16-bit value at offset 0xf2. The tg3_nvram_read()
  10553. * call will read from NVRAM and byteswap the data
  10554. * according to the byteswapping settings for all
  10555. * other register accesses. This ensures the data we
  10556. * want will always reside in the lower 16-bits.
  10557. * However, the data in NVRAM is in LE format, which
  10558. * means the data from the NVRAM read will always be
  10559. * opposite the endianness of the CPU. The 16-bit
  10560. * byteswap then brings the data to CPU endianness.
  10561. */
  10562. tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
  10563. return;
  10564. }
  10565. }
  10566. tp->nvram_size = TG3_NVRAM_SIZE_512KB;
  10567. }
  10568. static void __devinit tg3_get_nvram_info(struct tg3 *tp)
  10569. {
  10570. u32 nvcfg1;
  10571. nvcfg1 = tr32(NVRAM_CFG1);
  10572. if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
  10573. tg3_flag_set(tp, FLASH);
  10574. } else {
  10575. nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
  10576. tw32(NVRAM_CFG1, nvcfg1);
  10577. }
  10578. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
  10579. tg3_flag(tp, 5780_CLASS)) {
  10580. switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
  10581. case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
  10582. tp->nvram_jedecnum = JEDEC_ATMEL;
  10583. tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
  10584. tg3_flag_set(tp, NVRAM_BUFFERED);
  10585. break;
  10586. case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
  10587. tp->nvram_jedecnum = JEDEC_ATMEL;
  10588. tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
  10589. break;
  10590. case FLASH_VENDOR_ATMEL_EEPROM:
  10591. tp->nvram_jedecnum = JEDEC_ATMEL;
  10592. tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
  10593. tg3_flag_set(tp, NVRAM_BUFFERED);
  10594. break;
  10595. case FLASH_VENDOR_ST:
  10596. tp->nvram_jedecnum = JEDEC_ST;
  10597. tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
  10598. tg3_flag_set(tp, NVRAM_BUFFERED);
  10599. break;
  10600. case FLASH_VENDOR_SAIFUN:
  10601. tp->nvram_jedecnum = JEDEC_SAIFUN;
  10602. tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
  10603. break;
  10604. case FLASH_VENDOR_SST_SMALL:
  10605. case FLASH_VENDOR_SST_LARGE:
  10606. tp->nvram_jedecnum = JEDEC_SST;
  10607. tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
  10608. break;
  10609. }
  10610. } else {
  10611. tp->nvram_jedecnum = JEDEC_ATMEL;
  10612. tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
  10613. tg3_flag_set(tp, NVRAM_BUFFERED);
  10614. }
  10615. }
  10616. static void __devinit tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
  10617. {
  10618. switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
  10619. case FLASH_5752PAGE_SIZE_256:
  10620. tp->nvram_pagesize = 256;
  10621. break;
  10622. case FLASH_5752PAGE_SIZE_512:
  10623. tp->nvram_pagesize = 512;
  10624. break;
  10625. case FLASH_5752PAGE_SIZE_1K:
  10626. tp->nvram_pagesize = 1024;
  10627. break;
  10628. case FLASH_5752PAGE_SIZE_2K:
  10629. tp->nvram_pagesize = 2048;
  10630. break;
  10631. case FLASH_5752PAGE_SIZE_4K:
  10632. tp->nvram_pagesize = 4096;
  10633. break;
  10634. case FLASH_5752PAGE_SIZE_264:
  10635. tp->nvram_pagesize = 264;
  10636. break;
  10637. case FLASH_5752PAGE_SIZE_528:
  10638. tp->nvram_pagesize = 528;
  10639. break;
  10640. }
  10641. }
  10642. static void __devinit tg3_get_5752_nvram_info(struct tg3 *tp)
  10643. {
  10644. u32 nvcfg1;
  10645. nvcfg1 = tr32(NVRAM_CFG1);
  10646. /* NVRAM protection for TPM */
  10647. if (nvcfg1 & (1 << 27))
  10648. tg3_flag_set(tp, PROTECTED_NVRAM);
  10649. switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
  10650. case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
  10651. case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
  10652. tp->nvram_jedecnum = JEDEC_ATMEL;
  10653. tg3_flag_set(tp, NVRAM_BUFFERED);
  10654. break;
  10655. case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
  10656. tp->nvram_jedecnum = JEDEC_ATMEL;
  10657. tg3_flag_set(tp, NVRAM_BUFFERED);
  10658. tg3_flag_set(tp, FLASH);
  10659. break;
  10660. case FLASH_5752VENDOR_ST_M45PE10:
  10661. case FLASH_5752VENDOR_ST_M45PE20:
  10662. case FLASH_5752VENDOR_ST_M45PE40:
  10663. tp->nvram_jedecnum = JEDEC_ST;
  10664. tg3_flag_set(tp, NVRAM_BUFFERED);
  10665. tg3_flag_set(tp, FLASH);
  10666. break;
  10667. }
  10668. if (tg3_flag(tp, FLASH)) {
  10669. tg3_nvram_get_pagesize(tp, nvcfg1);
  10670. } else {
  10671. /* For eeprom, set pagesize to maximum eeprom size */
  10672. tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
  10673. nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
  10674. tw32(NVRAM_CFG1, nvcfg1);
  10675. }
  10676. }
  10677. static void __devinit tg3_get_5755_nvram_info(struct tg3 *tp)
  10678. {
  10679. u32 nvcfg1, protect = 0;
  10680. nvcfg1 = tr32(NVRAM_CFG1);
  10681. /* NVRAM protection for TPM */
  10682. if (nvcfg1 & (1 << 27)) {
  10683. tg3_flag_set(tp, PROTECTED_NVRAM);
  10684. protect = 1;
  10685. }
  10686. nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
  10687. switch (nvcfg1) {
  10688. case FLASH_5755VENDOR_ATMEL_FLASH_1:
  10689. case FLASH_5755VENDOR_ATMEL_FLASH_2:
  10690. case FLASH_5755VENDOR_ATMEL_FLASH_3:
  10691. case FLASH_5755VENDOR_ATMEL_FLASH_5:
  10692. tp->nvram_jedecnum = JEDEC_ATMEL;
  10693. tg3_flag_set(tp, NVRAM_BUFFERED);
  10694. tg3_flag_set(tp, FLASH);
  10695. tp->nvram_pagesize = 264;
  10696. if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
  10697. nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
  10698. tp->nvram_size = (protect ? 0x3e200 :
  10699. TG3_NVRAM_SIZE_512KB);
  10700. else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
  10701. tp->nvram_size = (protect ? 0x1f200 :
  10702. TG3_NVRAM_SIZE_256KB);
  10703. else
  10704. tp->nvram_size = (protect ? 0x1f200 :
  10705. TG3_NVRAM_SIZE_128KB);
  10706. break;
  10707. case FLASH_5752VENDOR_ST_M45PE10:
  10708. case FLASH_5752VENDOR_ST_M45PE20:
  10709. case FLASH_5752VENDOR_ST_M45PE40:
  10710. tp->nvram_jedecnum = JEDEC_ST;
  10711. tg3_flag_set(tp, NVRAM_BUFFERED);
  10712. tg3_flag_set(tp, FLASH);
  10713. tp->nvram_pagesize = 256;
  10714. if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
  10715. tp->nvram_size = (protect ?
  10716. TG3_NVRAM_SIZE_64KB :
  10717. TG3_NVRAM_SIZE_128KB);
  10718. else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
  10719. tp->nvram_size = (protect ?
  10720. TG3_NVRAM_SIZE_64KB :
  10721. TG3_NVRAM_SIZE_256KB);
  10722. else
  10723. tp->nvram_size = (protect ?
  10724. TG3_NVRAM_SIZE_128KB :
  10725. TG3_NVRAM_SIZE_512KB);
  10726. break;
  10727. }
  10728. }
  10729. static void __devinit tg3_get_5787_nvram_info(struct tg3 *tp)
  10730. {
  10731. u32 nvcfg1;
  10732. nvcfg1 = tr32(NVRAM_CFG1);
  10733. switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
  10734. case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
  10735. case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
  10736. case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
  10737. case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
  10738. tp->nvram_jedecnum = JEDEC_ATMEL;
  10739. tg3_flag_set(tp, NVRAM_BUFFERED);
  10740. tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
  10741. nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
  10742. tw32(NVRAM_CFG1, nvcfg1);
  10743. break;
  10744. case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
  10745. case FLASH_5755VENDOR_ATMEL_FLASH_1:
  10746. case FLASH_5755VENDOR_ATMEL_FLASH_2:
  10747. case FLASH_5755VENDOR_ATMEL_FLASH_3:
  10748. tp->nvram_jedecnum = JEDEC_ATMEL;
  10749. tg3_flag_set(tp, NVRAM_BUFFERED);
  10750. tg3_flag_set(tp, FLASH);
  10751. tp->nvram_pagesize = 264;
  10752. break;
  10753. case FLASH_5752VENDOR_ST_M45PE10:
  10754. case FLASH_5752VENDOR_ST_M45PE20:
  10755. case FLASH_5752VENDOR_ST_M45PE40:
  10756. tp->nvram_jedecnum = JEDEC_ST;
  10757. tg3_flag_set(tp, NVRAM_BUFFERED);
  10758. tg3_flag_set(tp, FLASH);
  10759. tp->nvram_pagesize = 256;
  10760. break;
  10761. }
  10762. }
  10763. static void __devinit tg3_get_5761_nvram_info(struct tg3 *tp)
  10764. {
  10765. u32 nvcfg1, protect = 0;
  10766. nvcfg1 = tr32(NVRAM_CFG1);
  10767. /* NVRAM protection for TPM */
  10768. if (nvcfg1 & (1 << 27)) {
  10769. tg3_flag_set(tp, PROTECTED_NVRAM);
  10770. protect = 1;
  10771. }
  10772. nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
  10773. switch (nvcfg1) {
  10774. case FLASH_5761VENDOR_ATMEL_ADB021D:
  10775. case FLASH_5761VENDOR_ATMEL_ADB041D:
  10776. case FLASH_5761VENDOR_ATMEL_ADB081D:
  10777. case FLASH_5761VENDOR_ATMEL_ADB161D:
  10778. case FLASH_5761VENDOR_ATMEL_MDB021D:
  10779. case FLASH_5761VENDOR_ATMEL_MDB041D:
  10780. case FLASH_5761VENDOR_ATMEL_MDB081D:
  10781. case FLASH_5761VENDOR_ATMEL_MDB161D:
  10782. tp->nvram_jedecnum = JEDEC_ATMEL;
  10783. tg3_flag_set(tp, NVRAM_BUFFERED);
  10784. tg3_flag_set(tp, FLASH);
  10785. tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
  10786. tp->nvram_pagesize = 256;
  10787. break;
  10788. case FLASH_5761VENDOR_ST_A_M45PE20:
  10789. case FLASH_5761VENDOR_ST_A_M45PE40:
  10790. case FLASH_5761VENDOR_ST_A_M45PE80:
  10791. case FLASH_5761VENDOR_ST_A_M45PE16:
  10792. case FLASH_5761VENDOR_ST_M_M45PE20:
  10793. case FLASH_5761VENDOR_ST_M_M45PE40:
  10794. case FLASH_5761VENDOR_ST_M_M45PE80:
  10795. case FLASH_5761VENDOR_ST_M_M45PE16:
  10796. tp->nvram_jedecnum = JEDEC_ST;
  10797. tg3_flag_set(tp, NVRAM_BUFFERED);
  10798. tg3_flag_set(tp, FLASH);
  10799. tp->nvram_pagesize = 256;
  10800. break;
  10801. }
  10802. if (protect) {
  10803. tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
  10804. } else {
  10805. switch (nvcfg1) {
  10806. case FLASH_5761VENDOR_ATMEL_ADB161D:
  10807. case FLASH_5761VENDOR_ATMEL_MDB161D:
  10808. case FLASH_5761VENDOR_ST_A_M45PE16:
  10809. case FLASH_5761VENDOR_ST_M_M45PE16:
  10810. tp->nvram_size = TG3_NVRAM_SIZE_2MB;
  10811. break;
  10812. case FLASH_5761VENDOR_ATMEL_ADB081D:
  10813. case FLASH_5761VENDOR_ATMEL_MDB081D:
  10814. case FLASH_5761VENDOR_ST_A_M45PE80:
  10815. case FLASH_5761VENDOR_ST_M_M45PE80:
  10816. tp->nvram_size = TG3_NVRAM_SIZE_1MB;
  10817. break;
  10818. case FLASH_5761VENDOR_ATMEL_ADB041D:
  10819. case FLASH_5761VENDOR_ATMEL_MDB041D:
  10820. case FLASH_5761VENDOR_ST_A_M45PE40:
  10821. case FLASH_5761VENDOR_ST_M_M45PE40:
  10822. tp->nvram_size = TG3_NVRAM_SIZE_512KB;
  10823. break;
  10824. case FLASH_5761VENDOR_ATMEL_ADB021D:
  10825. case FLASH_5761VENDOR_ATMEL_MDB021D:
  10826. case FLASH_5761VENDOR_ST_A_M45PE20:
  10827. case FLASH_5761VENDOR_ST_M_M45PE20:
  10828. tp->nvram_size = TG3_NVRAM_SIZE_256KB;
  10829. break;
  10830. }
  10831. }
  10832. }
  10833. static void __devinit tg3_get_5906_nvram_info(struct tg3 *tp)
  10834. {
  10835. tp->nvram_jedecnum = JEDEC_ATMEL;
  10836. tg3_flag_set(tp, NVRAM_BUFFERED);
  10837. tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
  10838. }
  10839. static void __devinit tg3_get_57780_nvram_info(struct tg3 *tp)
  10840. {
  10841. u32 nvcfg1;
  10842. nvcfg1 = tr32(NVRAM_CFG1);
  10843. switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
  10844. case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
  10845. case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
  10846. tp->nvram_jedecnum = JEDEC_ATMEL;
  10847. tg3_flag_set(tp, NVRAM_BUFFERED);
  10848. tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
  10849. nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
  10850. tw32(NVRAM_CFG1, nvcfg1);
  10851. return;
  10852. case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
  10853. case FLASH_57780VENDOR_ATMEL_AT45DB011D:
  10854. case FLASH_57780VENDOR_ATMEL_AT45DB011B:
  10855. case FLASH_57780VENDOR_ATMEL_AT45DB021D:
  10856. case FLASH_57780VENDOR_ATMEL_AT45DB021B:
  10857. case FLASH_57780VENDOR_ATMEL_AT45DB041D:
  10858. case FLASH_57780VENDOR_ATMEL_AT45DB041B:
  10859. tp->nvram_jedecnum = JEDEC_ATMEL;
  10860. tg3_flag_set(tp, NVRAM_BUFFERED);
  10861. tg3_flag_set(tp, FLASH);
  10862. switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
  10863. case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
  10864. case FLASH_57780VENDOR_ATMEL_AT45DB011D:
  10865. case FLASH_57780VENDOR_ATMEL_AT45DB011B:
  10866. tp->nvram_size = TG3_NVRAM_SIZE_128KB;
  10867. break;
  10868. case FLASH_57780VENDOR_ATMEL_AT45DB021D:
  10869. case FLASH_57780VENDOR_ATMEL_AT45DB021B:
  10870. tp->nvram_size = TG3_NVRAM_SIZE_256KB;
  10871. break;
  10872. case FLASH_57780VENDOR_ATMEL_AT45DB041D:
  10873. case FLASH_57780VENDOR_ATMEL_AT45DB041B:
  10874. tp->nvram_size = TG3_NVRAM_SIZE_512KB;
  10875. break;
  10876. }
  10877. break;
  10878. case FLASH_5752VENDOR_ST_M45PE10:
  10879. case FLASH_5752VENDOR_ST_M45PE20:
  10880. case FLASH_5752VENDOR_ST_M45PE40:
  10881. tp->nvram_jedecnum = JEDEC_ST;
  10882. tg3_flag_set(tp, NVRAM_BUFFERED);
  10883. tg3_flag_set(tp, FLASH);
  10884. switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
  10885. case FLASH_5752VENDOR_ST_M45PE10:
  10886. tp->nvram_size = TG3_NVRAM_SIZE_128KB;
  10887. break;
  10888. case FLASH_5752VENDOR_ST_M45PE20:
  10889. tp->nvram_size = TG3_NVRAM_SIZE_256KB;
  10890. break;
  10891. case FLASH_5752VENDOR_ST_M45PE40:
  10892. tp->nvram_size = TG3_NVRAM_SIZE_512KB;
  10893. break;
  10894. }
  10895. break;
  10896. default:
  10897. tg3_flag_set(tp, NO_NVRAM);
  10898. return;
  10899. }
  10900. tg3_nvram_get_pagesize(tp, nvcfg1);
  10901. if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
  10902. tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
  10903. }
  10904. static void __devinit tg3_get_5717_nvram_info(struct tg3 *tp)
  10905. {
  10906. u32 nvcfg1;
  10907. nvcfg1 = tr32(NVRAM_CFG1);
  10908. switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
  10909. case FLASH_5717VENDOR_ATMEL_EEPROM:
  10910. case FLASH_5717VENDOR_MICRO_EEPROM:
  10911. tp->nvram_jedecnum = JEDEC_ATMEL;
  10912. tg3_flag_set(tp, NVRAM_BUFFERED);
  10913. tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
  10914. nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
  10915. tw32(NVRAM_CFG1, nvcfg1);
  10916. return;
  10917. case FLASH_5717VENDOR_ATMEL_MDB011D:
  10918. case FLASH_5717VENDOR_ATMEL_ADB011B:
  10919. case FLASH_5717VENDOR_ATMEL_ADB011D:
  10920. case FLASH_5717VENDOR_ATMEL_MDB021D:
  10921. case FLASH_5717VENDOR_ATMEL_ADB021B:
  10922. case FLASH_5717VENDOR_ATMEL_ADB021D:
  10923. case FLASH_5717VENDOR_ATMEL_45USPT:
  10924. tp->nvram_jedecnum = JEDEC_ATMEL;
  10925. tg3_flag_set(tp, NVRAM_BUFFERED);
  10926. tg3_flag_set(tp, FLASH);
  10927. switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
  10928. case FLASH_5717VENDOR_ATMEL_MDB021D:
  10929. /* Detect size with tg3_nvram_get_size() */
  10930. break;
  10931. case FLASH_5717VENDOR_ATMEL_ADB021B:
  10932. case FLASH_5717VENDOR_ATMEL_ADB021D:
  10933. tp->nvram_size = TG3_NVRAM_SIZE_256KB;
  10934. break;
  10935. default:
  10936. tp->nvram_size = TG3_NVRAM_SIZE_128KB;
  10937. break;
  10938. }
  10939. break;
  10940. case FLASH_5717VENDOR_ST_M_M25PE10:
  10941. case FLASH_5717VENDOR_ST_A_M25PE10:
  10942. case FLASH_5717VENDOR_ST_M_M45PE10:
  10943. case FLASH_5717VENDOR_ST_A_M45PE10:
  10944. case FLASH_5717VENDOR_ST_M_M25PE20:
  10945. case FLASH_5717VENDOR_ST_A_M25PE20:
  10946. case FLASH_5717VENDOR_ST_M_M45PE20:
  10947. case FLASH_5717VENDOR_ST_A_M45PE20:
  10948. case FLASH_5717VENDOR_ST_25USPT:
  10949. case FLASH_5717VENDOR_ST_45USPT:
  10950. tp->nvram_jedecnum = JEDEC_ST;
  10951. tg3_flag_set(tp, NVRAM_BUFFERED);
  10952. tg3_flag_set(tp, FLASH);
  10953. switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
  10954. case FLASH_5717VENDOR_ST_M_M25PE20:
  10955. case FLASH_5717VENDOR_ST_M_M45PE20:
  10956. /* Detect size with tg3_nvram_get_size() */
  10957. break;
  10958. case FLASH_5717VENDOR_ST_A_M25PE20:
  10959. case FLASH_5717VENDOR_ST_A_M45PE20:
  10960. tp->nvram_size = TG3_NVRAM_SIZE_256KB;
  10961. break;
  10962. default:
  10963. tp->nvram_size = TG3_NVRAM_SIZE_128KB;
  10964. break;
  10965. }
  10966. break;
  10967. default:
  10968. tg3_flag_set(tp, NO_NVRAM);
  10969. return;
  10970. }
  10971. tg3_nvram_get_pagesize(tp, nvcfg1);
  10972. if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
  10973. tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
  10974. }
  10975. static void __devinit tg3_get_5720_nvram_info(struct tg3 *tp)
  10976. {
  10977. u32 nvcfg1, nvmpinstrp;
  10978. nvcfg1 = tr32(NVRAM_CFG1);
  10979. nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
  10980. switch (nvmpinstrp) {
  10981. case FLASH_5720_EEPROM_HD:
  10982. case FLASH_5720_EEPROM_LD:
  10983. tp->nvram_jedecnum = JEDEC_ATMEL;
  10984. tg3_flag_set(tp, NVRAM_BUFFERED);
  10985. nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
  10986. tw32(NVRAM_CFG1, nvcfg1);
  10987. if (nvmpinstrp == FLASH_5720_EEPROM_HD)
  10988. tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
  10989. else
  10990. tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
  10991. return;
  10992. case FLASH_5720VENDOR_M_ATMEL_DB011D:
  10993. case FLASH_5720VENDOR_A_ATMEL_DB011B:
  10994. case FLASH_5720VENDOR_A_ATMEL_DB011D:
  10995. case FLASH_5720VENDOR_M_ATMEL_DB021D:
  10996. case FLASH_5720VENDOR_A_ATMEL_DB021B:
  10997. case FLASH_5720VENDOR_A_ATMEL_DB021D:
  10998. case FLASH_5720VENDOR_M_ATMEL_DB041D:
  10999. case FLASH_5720VENDOR_A_ATMEL_DB041B:
  11000. case FLASH_5720VENDOR_A_ATMEL_DB041D:
  11001. case FLASH_5720VENDOR_M_ATMEL_DB081D:
  11002. case FLASH_5720VENDOR_A_ATMEL_DB081D:
  11003. case FLASH_5720VENDOR_ATMEL_45USPT:
  11004. tp->nvram_jedecnum = JEDEC_ATMEL;
  11005. tg3_flag_set(tp, NVRAM_BUFFERED);
  11006. tg3_flag_set(tp, FLASH);
  11007. switch (nvmpinstrp) {
  11008. case FLASH_5720VENDOR_M_ATMEL_DB021D:
  11009. case FLASH_5720VENDOR_A_ATMEL_DB021B:
  11010. case FLASH_5720VENDOR_A_ATMEL_DB021D:
  11011. tp->nvram_size = TG3_NVRAM_SIZE_256KB;
  11012. break;
  11013. case FLASH_5720VENDOR_M_ATMEL_DB041D:
  11014. case FLASH_5720VENDOR_A_ATMEL_DB041B:
  11015. case FLASH_5720VENDOR_A_ATMEL_DB041D:
  11016. tp->nvram_size = TG3_NVRAM_SIZE_512KB;
  11017. break;
  11018. case FLASH_5720VENDOR_M_ATMEL_DB081D:
  11019. case FLASH_5720VENDOR_A_ATMEL_DB081D:
  11020. tp->nvram_size = TG3_NVRAM_SIZE_1MB;
  11021. break;
  11022. default:
  11023. tp->nvram_size = TG3_NVRAM_SIZE_128KB;
  11024. break;
  11025. }
  11026. break;
  11027. case FLASH_5720VENDOR_M_ST_M25PE10:
  11028. case FLASH_5720VENDOR_M_ST_M45PE10:
  11029. case FLASH_5720VENDOR_A_ST_M25PE10:
  11030. case FLASH_5720VENDOR_A_ST_M45PE10:
  11031. case FLASH_5720VENDOR_M_ST_M25PE20:
  11032. case FLASH_5720VENDOR_M_ST_M45PE20:
  11033. case FLASH_5720VENDOR_A_ST_M25PE20:
  11034. case FLASH_5720VENDOR_A_ST_M45PE20:
  11035. case FLASH_5720VENDOR_M_ST_M25PE40:
  11036. case FLASH_5720VENDOR_M_ST_M45PE40:
  11037. case FLASH_5720VENDOR_A_ST_M25PE40:
  11038. case FLASH_5720VENDOR_A_ST_M45PE40:
  11039. case FLASH_5720VENDOR_M_ST_M25PE80:
  11040. case FLASH_5720VENDOR_M_ST_M45PE80:
  11041. case FLASH_5720VENDOR_A_ST_M25PE80:
  11042. case FLASH_5720VENDOR_A_ST_M45PE80:
  11043. case FLASH_5720VENDOR_ST_25USPT:
  11044. case FLASH_5720VENDOR_ST_45USPT:
  11045. tp->nvram_jedecnum = JEDEC_ST;
  11046. tg3_flag_set(tp, NVRAM_BUFFERED);
  11047. tg3_flag_set(tp, FLASH);
  11048. switch (nvmpinstrp) {
  11049. case FLASH_5720VENDOR_M_ST_M25PE20:
  11050. case FLASH_5720VENDOR_M_ST_M45PE20:
  11051. case FLASH_5720VENDOR_A_ST_M25PE20:
  11052. case FLASH_5720VENDOR_A_ST_M45PE20:
  11053. tp->nvram_size = TG3_NVRAM_SIZE_256KB;
  11054. break;
  11055. case FLASH_5720VENDOR_M_ST_M25PE40:
  11056. case FLASH_5720VENDOR_M_ST_M45PE40:
  11057. case FLASH_5720VENDOR_A_ST_M25PE40:
  11058. case FLASH_5720VENDOR_A_ST_M45PE40:
  11059. tp->nvram_size = TG3_NVRAM_SIZE_512KB;
  11060. break;
  11061. case FLASH_5720VENDOR_M_ST_M25PE80:
  11062. case FLASH_5720VENDOR_M_ST_M45PE80:
  11063. case FLASH_5720VENDOR_A_ST_M25PE80:
  11064. case FLASH_5720VENDOR_A_ST_M45PE80:
  11065. tp->nvram_size = TG3_NVRAM_SIZE_1MB;
  11066. break;
  11067. default:
  11068. tp->nvram_size = TG3_NVRAM_SIZE_128KB;
  11069. break;
  11070. }
  11071. break;
  11072. default:
  11073. tg3_flag_set(tp, NO_NVRAM);
  11074. return;
  11075. }
  11076. tg3_nvram_get_pagesize(tp, nvcfg1);
  11077. if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
  11078. tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
  11079. }
  11080. /* Chips other than 5700/5701 use the NVRAM for fetching info. */
  11081. static void __devinit tg3_nvram_init(struct tg3 *tp)
  11082. {
  11083. tw32_f(GRC_EEPROM_ADDR,
  11084. (EEPROM_ADDR_FSM_RESET |
  11085. (EEPROM_DEFAULT_CLOCK_PERIOD <<
  11086. EEPROM_ADDR_CLKPERD_SHIFT)));
  11087. msleep(1);
  11088. /* Enable seeprom accesses. */
  11089. tw32_f(GRC_LOCAL_CTRL,
  11090. tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
  11091. udelay(100);
  11092. if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
  11093. GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
  11094. tg3_flag_set(tp, NVRAM);
  11095. if (tg3_nvram_lock(tp)) {
  11096. netdev_warn(tp->dev,
  11097. "Cannot get nvram lock, %s failed\n",
  11098. __func__);
  11099. return;
  11100. }
  11101. tg3_enable_nvram_access(tp);
  11102. tp->nvram_size = 0;
  11103. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
  11104. tg3_get_5752_nvram_info(tp);
  11105. else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
  11106. tg3_get_5755_nvram_info(tp);
  11107. else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
  11108. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
  11109. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
  11110. tg3_get_5787_nvram_info(tp);
  11111. else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
  11112. tg3_get_5761_nvram_info(tp);
  11113. else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
  11114. tg3_get_5906_nvram_info(tp);
  11115. else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
  11116. tg3_flag(tp, 57765_CLASS))
  11117. tg3_get_57780_nvram_info(tp);
  11118. else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
  11119. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
  11120. tg3_get_5717_nvram_info(tp);
  11121. else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
  11122. tg3_get_5720_nvram_info(tp);
  11123. else
  11124. tg3_get_nvram_info(tp);
  11125. if (tp->nvram_size == 0)
  11126. tg3_get_nvram_size(tp);
  11127. tg3_disable_nvram_access(tp);
  11128. tg3_nvram_unlock(tp);
  11129. } else {
  11130. tg3_flag_clear(tp, NVRAM);
  11131. tg3_flag_clear(tp, NVRAM_BUFFERED);
  11132. tg3_get_eeprom_size(tp);
  11133. }
  11134. }
  11135. struct subsys_tbl_ent {
  11136. u16 subsys_vendor, subsys_devid;
  11137. u32 phy_id;
  11138. };
  11139. static struct subsys_tbl_ent subsys_id_to_phy_id[] __devinitdata = {
  11140. /* Broadcom boards. */
  11141. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  11142. TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
  11143. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  11144. TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
  11145. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  11146. TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
  11147. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  11148. TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
  11149. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  11150. TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
  11151. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  11152. TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
  11153. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  11154. TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
  11155. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  11156. TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
  11157. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  11158. TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
  11159. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  11160. TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
  11161. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  11162. TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
  11163. /* 3com boards. */
  11164. { TG3PCI_SUBVENDOR_ID_3COM,
  11165. TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
  11166. { TG3PCI_SUBVENDOR_ID_3COM,
  11167. TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
  11168. { TG3PCI_SUBVENDOR_ID_3COM,
  11169. TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
  11170. { TG3PCI_SUBVENDOR_ID_3COM,
  11171. TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
  11172. { TG3PCI_SUBVENDOR_ID_3COM,
  11173. TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
  11174. /* DELL boards. */
  11175. { TG3PCI_SUBVENDOR_ID_DELL,
  11176. TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
  11177. { TG3PCI_SUBVENDOR_ID_DELL,
  11178. TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
  11179. { TG3PCI_SUBVENDOR_ID_DELL,
  11180. TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
  11181. { TG3PCI_SUBVENDOR_ID_DELL,
  11182. TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
  11183. /* Compaq boards. */
  11184. { TG3PCI_SUBVENDOR_ID_COMPAQ,
  11185. TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
  11186. { TG3PCI_SUBVENDOR_ID_COMPAQ,
  11187. TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
  11188. { TG3PCI_SUBVENDOR_ID_COMPAQ,
  11189. TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
  11190. { TG3PCI_SUBVENDOR_ID_COMPAQ,
  11191. TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
  11192. { TG3PCI_SUBVENDOR_ID_COMPAQ,
  11193. TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
  11194. /* IBM boards. */
  11195. { TG3PCI_SUBVENDOR_ID_IBM,
  11196. TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
  11197. };
  11198. static struct subsys_tbl_ent * __devinit tg3_lookup_by_subsys(struct tg3 *tp)
  11199. {
  11200. int i;
  11201. for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
  11202. if ((subsys_id_to_phy_id[i].subsys_vendor ==
  11203. tp->pdev->subsystem_vendor) &&
  11204. (subsys_id_to_phy_id[i].subsys_devid ==
  11205. tp->pdev->subsystem_device))
  11206. return &subsys_id_to_phy_id[i];
  11207. }
  11208. return NULL;
  11209. }
  11210. static void __devinit tg3_get_eeprom_hw_cfg(struct tg3 *tp)
  11211. {
  11212. u32 val;
  11213. tp->phy_id = TG3_PHY_ID_INVALID;
  11214. tp->led_ctrl = LED_CTRL_MODE_PHY_1;
  11215. /* Assume an onboard device and WOL capable by default. */
  11216. tg3_flag_set(tp, EEPROM_WRITE_PROT);
  11217. tg3_flag_set(tp, WOL_CAP);
  11218. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
  11219. if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
  11220. tg3_flag_clear(tp, EEPROM_WRITE_PROT);
  11221. tg3_flag_set(tp, IS_NIC);
  11222. }
  11223. val = tr32(VCPU_CFGSHDW);
  11224. if (val & VCPU_CFGSHDW_ASPM_DBNC)
  11225. tg3_flag_set(tp, ASPM_WORKAROUND);
  11226. if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
  11227. (val & VCPU_CFGSHDW_WOL_MAGPKT)) {
  11228. tg3_flag_set(tp, WOL_ENABLE);
  11229. device_set_wakeup_enable(&tp->pdev->dev, true);
  11230. }
  11231. goto done;
  11232. }
  11233. tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
  11234. if (val == NIC_SRAM_DATA_SIG_MAGIC) {
  11235. u32 nic_cfg, led_cfg;
  11236. u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
  11237. int eeprom_phy_serdes = 0;
  11238. tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
  11239. tp->nic_sram_data_cfg = nic_cfg;
  11240. tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
  11241. ver >>= NIC_SRAM_DATA_VER_SHIFT;
  11242. if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
  11243. GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
  11244. GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703 &&
  11245. (ver > 0) && (ver < 0x100))
  11246. tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
  11247. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
  11248. tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
  11249. if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
  11250. NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
  11251. eeprom_phy_serdes = 1;
  11252. tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
  11253. if (nic_phy_id != 0) {
  11254. u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
  11255. u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
  11256. eeprom_phy_id = (id1 >> 16) << 10;
  11257. eeprom_phy_id |= (id2 & 0xfc00) << 16;
  11258. eeprom_phy_id |= (id2 & 0x03ff) << 0;
  11259. } else
  11260. eeprom_phy_id = 0;
  11261. tp->phy_id = eeprom_phy_id;
  11262. if (eeprom_phy_serdes) {
  11263. if (!tg3_flag(tp, 5705_PLUS))
  11264. tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
  11265. else
  11266. tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
  11267. }
  11268. if (tg3_flag(tp, 5750_PLUS))
  11269. led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
  11270. SHASTA_EXT_LED_MODE_MASK);
  11271. else
  11272. led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
  11273. switch (led_cfg) {
  11274. default:
  11275. case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
  11276. tp->led_ctrl = LED_CTRL_MODE_PHY_1;
  11277. break;
  11278. case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
  11279. tp->led_ctrl = LED_CTRL_MODE_PHY_2;
  11280. break;
  11281. case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
  11282. tp->led_ctrl = LED_CTRL_MODE_MAC;
  11283. /* Default to PHY_1_MODE if 0 (MAC_MODE) is
  11284. * read on some older 5700/5701 bootcode.
  11285. */
  11286. if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
  11287. ASIC_REV_5700 ||
  11288. GET_ASIC_REV(tp->pci_chip_rev_id) ==
  11289. ASIC_REV_5701)
  11290. tp->led_ctrl = LED_CTRL_MODE_PHY_1;
  11291. break;
  11292. case SHASTA_EXT_LED_SHARED:
  11293. tp->led_ctrl = LED_CTRL_MODE_SHARED;
  11294. if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
  11295. tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
  11296. tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
  11297. LED_CTRL_MODE_PHY_2);
  11298. break;
  11299. case SHASTA_EXT_LED_MAC:
  11300. tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
  11301. break;
  11302. case SHASTA_EXT_LED_COMBO:
  11303. tp->led_ctrl = LED_CTRL_MODE_COMBO;
  11304. if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
  11305. tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
  11306. LED_CTRL_MODE_PHY_2);
  11307. break;
  11308. }
  11309. if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
  11310. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
  11311. tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
  11312. tp->led_ctrl = LED_CTRL_MODE_PHY_2;
  11313. if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
  11314. tp->led_ctrl = LED_CTRL_MODE_PHY_1;
  11315. if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
  11316. tg3_flag_set(tp, EEPROM_WRITE_PROT);
  11317. if ((tp->pdev->subsystem_vendor ==
  11318. PCI_VENDOR_ID_ARIMA) &&
  11319. (tp->pdev->subsystem_device == 0x205a ||
  11320. tp->pdev->subsystem_device == 0x2063))
  11321. tg3_flag_clear(tp, EEPROM_WRITE_PROT);
  11322. } else {
  11323. tg3_flag_clear(tp, EEPROM_WRITE_PROT);
  11324. tg3_flag_set(tp, IS_NIC);
  11325. }
  11326. if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
  11327. tg3_flag_set(tp, ENABLE_ASF);
  11328. if (tg3_flag(tp, 5750_PLUS))
  11329. tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
  11330. }
  11331. if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
  11332. tg3_flag(tp, 5750_PLUS))
  11333. tg3_flag_set(tp, ENABLE_APE);
  11334. if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
  11335. !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
  11336. tg3_flag_clear(tp, WOL_CAP);
  11337. if (tg3_flag(tp, WOL_CAP) &&
  11338. (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE)) {
  11339. tg3_flag_set(tp, WOL_ENABLE);
  11340. device_set_wakeup_enable(&tp->pdev->dev, true);
  11341. }
  11342. if (cfg2 & (1 << 17))
  11343. tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
  11344. /* serdes signal pre-emphasis in register 0x590 set by */
  11345. /* bootcode if bit 18 is set */
  11346. if (cfg2 & (1 << 18))
  11347. tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
  11348. if ((tg3_flag(tp, 57765_PLUS) ||
  11349. (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
  11350. GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX)) &&
  11351. (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
  11352. tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
  11353. if (tg3_flag(tp, PCI_EXPRESS) &&
  11354. GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
  11355. !tg3_flag(tp, 57765_PLUS)) {
  11356. u32 cfg3;
  11357. tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
  11358. if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
  11359. tg3_flag_set(tp, ASPM_WORKAROUND);
  11360. }
  11361. if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
  11362. tg3_flag_set(tp, RGMII_INBAND_DISABLE);
  11363. if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
  11364. tg3_flag_set(tp, RGMII_EXT_IBND_RX_EN);
  11365. if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
  11366. tg3_flag_set(tp, RGMII_EXT_IBND_TX_EN);
  11367. }
  11368. done:
  11369. if (tg3_flag(tp, WOL_CAP))
  11370. device_set_wakeup_enable(&tp->pdev->dev,
  11371. tg3_flag(tp, WOL_ENABLE));
  11372. else
  11373. device_set_wakeup_capable(&tp->pdev->dev, false);
  11374. }
  11375. static int __devinit tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
  11376. {
  11377. int i;
  11378. u32 val;
  11379. tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
  11380. tw32(OTP_CTRL, cmd);
  11381. /* Wait for up to 1 ms for command to execute. */
  11382. for (i = 0; i < 100; i++) {
  11383. val = tr32(OTP_STATUS);
  11384. if (val & OTP_STATUS_CMD_DONE)
  11385. break;
  11386. udelay(10);
  11387. }
  11388. return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
  11389. }
  11390. /* Read the gphy configuration from the OTP region of the chip. The gphy
  11391. * configuration is a 32-bit value that straddles the alignment boundary.
  11392. * We do two 32-bit reads and then shift and merge the results.
  11393. */
  11394. static u32 __devinit tg3_read_otp_phycfg(struct tg3 *tp)
  11395. {
  11396. u32 bhalf_otp, thalf_otp;
  11397. tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
  11398. if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
  11399. return 0;
  11400. tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
  11401. if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
  11402. return 0;
  11403. thalf_otp = tr32(OTP_READ_DATA);
  11404. tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
  11405. if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
  11406. return 0;
  11407. bhalf_otp = tr32(OTP_READ_DATA);
  11408. return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
  11409. }
  11410. static void __devinit tg3_phy_init_link_config(struct tg3 *tp)
  11411. {
  11412. u32 adv = ADVERTISED_Autoneg;
  11413. if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
  11414. adv |= ADVERTISED_1000baseT_Half |
  11415. ADVERTISED_1000baseT_Full;
  11416. if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
  11417. adv |= ADVERTISED_100baseT_Half |
  11418. ADVERTISED_100baseT_Full |
  11419. ADVERTISED_10baseT_Half |
  11420. ADVERTISED_10baseT_Full |
  11421. ADVERTISED_TP;
  11422. else
  11423. adv |= ADVERTISED_FIBRE;
  11424. tp->link_config.advertising = adv;
  11425. tp->link_config.speed = SPEED_UNKNOWN;
  11426. tp->link_config.duplex = DUPLEX_UNKNOWN;
  11427. tp->link_config.autoneg = AUTONEG_ENABLE;
  11428. tp->link_config.active_speed = SPEED_UNKNOWN;
  11429. tp->link_config.active_duplex = DUPLEX_UNKNOWN;
  11430. tp->old_link = -1;
  11431. }
  11432. static int __devinit tg3_phy_probe(struct tg3 *tp)
  11433. {
  11434. u32 hw_phy_id_1, hw_phy_id_2;
  11435. u32 hw_phy_id, hw_phy_id_masked;
  11436. int err;
  11437. /* flow control autonegotiation is default behavior */
  11438. tg3_flag_set(tp, PAUSE_AUTONEG);
  11439. tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
  11440. if (tg3_flag(tp, ENABLE_APE)) {
  11441. switch (tp->pci_fn) {
  11442. case 0:
  11443. tp->phy_ape_lock = TG3_APE_LOCK_PHY0;
  11444. break;
  11445. case 1:
  11446. tp->phy_ape_lock = TG3_APE_LOCK_PHY1;
  11447. break;
  11448. case 2:
  11449. tp->phy_ape_lock = TG3_APE_LOCK_PHY2;
  11450. break;
  11451. case 3:
  11452. tp->phy_ape_lock = TG3_APE_LOCK_PHY3;
  11453. break;
  11454. }
  11455. }
  11456. if (tg3_flag(tp, USE_PHYLIB))
  11457. return tg3_phy_init(tp);
  11458. /* Reading the PHY ID register can conflict with ASF
  11459. * firmware access to the PHY hardware.
  11460. */
  11461. err = 0;
  11462. if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)) {
  11463. hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
  11464. } else {
  11465. /* Now read the physical PHY_ID from the chip and verify
  11466. * that it is sane. If it doesn't look good, we fall back
  11467. * to either the hard-coded table based PHY_ID and failing
  11468. * that the value found in the eeprom area.
  11469. */
  11470. err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
  11471. err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
  11472. hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
  11473. hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
  11474. hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
  11475. hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
  11476. }
  11477. if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
  11478. tp->phy_id = hw_phy_id;
  11479. if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
  11480. tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
  11481. else
  11482. tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
  11483. } else {
  11484. if (tp->phy_id != TG3_PHY_ID_INVALID) {
  11485. /* Do nothing, phy ID already set up in
  11486. * tg3_get_eeprom_hw_cfg().
  11487. */
  11488. } else {
  11489. struct subsys_tbl_ent *p;
  11490. /* No eeprom signature? Try the hardcoded
  11491. * subsys device table.
  11492. */
  11493. p = tg3_lookup_by_subsys(tp);
  11494. if (!p)
  11495. return -ENODEV;
  11496. tp->phy_id = p->phy_id;
  11497. if (!tp->phy_id ||
  11498. tp->phy_id == TG3_PHY_ID_BCM8002)
  11499. tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
  11500. }
  11501. }
  11502. if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
  11503. (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
  11504. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720 ||
  11505. (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 &&
  11506. tp->pci_chip_rev_id != CHIPREV_ID_5717_A0) ||
  11507. (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
  11508. tp->pci_chip_rev_id != CHIPREV_ID_57765_A0)))
  11509. tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
  11510. tg3_phy_init_link_config(tp);
  11511. if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
  11512. !tg3_flag(tp, ENABLE_APE) &&
  11513. !tg3_flag(tp, ENABLE_ASF)) {
  11514. u32 bmsr, dummy;
  11515. tg3_readphy(tp, MII_BMSR, &bmsr);
  11516. if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
  11517. (bmsr & BMSR_LSTATUS))
  11518. goto skip_phy_reset;
  11519. err = tg3_phy_reset(tp);
  11520. if (err)
  11521. return err;
  11522. tg3_phy_set_wirespeed(tp);
  11523. if (!tg3_phy_copper_an_config_ok(tp, &dummy)) {
  11524. tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
  11525. tp->link_config.flowctrl);
  11526. tg3_writephy(tp, MII_BMCR,
  11527. BMCR_ANENABLE | BMCR_ANRESTART);
  11528. }
  11529. }
  11530. skip_phy_reset:
  11531. if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
  11532. err = tg3_init_5401phy_dsp(tp);
  11533. if (err)
  11534. return err;
  11535. err = tg3_init_5401phy_dsp(tp);
  11536. }
  11537. return err;
  11538. }
  11539. static void __devinit tg3_read_vpd(struct tg3 *tp)
  11540. {
  11541. u8 *vpd_data;
  11542. unsigned int block_end, rosize, len;
  11543. u32 vpdlen;
  11544. int j, i = 0;
  11545. vpd_data = (u8 *)tg3_vpd_readblock(tp, &vpdlen);
  11546. if (!vpd_data)
  11547. goto out_no_vpd;
  11548. i = pci_vpd_find_tag(vpd_data, 0, vpdlen, PCI_VPD_LRDT_RO_DATA);
  11549. if (i < 0)
  11550. goto out_not_found;
  11551. rosize = pci_vpd_lrdt_size(&vpd_data[i]);
  11552. block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
  11553. i += PCI_VPD_LRDT_TAG_SIZE;
  11554. if (block_end > vpdlen)
  11555. goto out_not_found;
  11556. j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
  11557. PCI_VPD_RO_KEYWORD_MFR_ID);
  11558. if (j > 0) {
  11559. len = pci_vpd_info_field_size(&vpd_data[j]);
  11560. j += PCI_VPD_INFO_FLD_HDR_SIZE;
  11561. if (j + len > block_end || len != 4 ||
  11562. memcmp(&vpd_data[j], "1028", 4))
  11563. goto partno;
  11564. j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
  11565. PCI_VPD_RO_KEYWORD_VENDOR0);
  11566. if (j < 0)
  11567. goto partno;
  11568. len = pci_vpd_info_field_size(&vpd_data[j]);
  11569. j += PCI_VPD_INFO_FLD_HDR_SIZE;
  11570. if (j + len > block_end)
  11571. goto partno;
  11572. memcpy(tp->fw_ver, &vpd_data[j], len);
  11573. strncat(tp->fw_ver, " bc ", vpdlen - len - 1);
  11574. }
  11575. partno:
  11576. i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
  11577. PCI_VPD_RO_KEYWORD_PARTNO);
  11578. if (i < 0)
  11579. goto out_not_found;
  11580. len = pci_vpd_info_field_size(&vpd_data[i]);
  11581. i += PCI_VPD_INFO_FLD_HDR_SIZE;
  11582. if (len > TG3_BPN_SIZE ||
  11583. (len + i) > vpdlen)
  11584. goto out_not_found;
  11585. memcpy(tp->board_part_number, &vpd_data[i], len);
  11586. out_not_found:
  11587. kfree(vpd_data);
  11588. if (tp->board_part_number[0])
  11589. return;
  11590. out_no_vpd:
  11591. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
  11592. if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
  11593. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C)
  11594. strcpy(tp->board_part_number, "BCM5717");
  11595. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
  11596. strcpy(tp->board_part_number, "BCM5718");
  11597. else
  11598. goto nomatch;
  11599. } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
  11600. if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
  11601. strcpy(tp->board_part_number, "BCM57780");
  11602. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
  11603. strcpy(tp->board_part_number, "BCM57760");
  11604. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
  11605. strcpy(tp->board_part_number, "BCM57790");
  11606. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
  11607. strcpy(tp->board_part_number, "BCM57788");
  11608. else
  11609. goto nomatch;
  11610. } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
  11611. if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
  11612. strcpy(tp->board_part_number, "BCM57761");
  11613. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
  11614. strcpy(tp->board_part_number, "BCM57765");
  11615. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
  11616. strcpy(tp->board_part_number, "BCM57781");
  11617. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
  11618. strcpy(tp->board_part_number, "BCM57785");
  11619. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
  11620. strcpy(tp->board_part_number, "BCM57791");
  11621. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
  11622. strcpy(tp->board_part_number, "BCM57795");
  11623. else
  11624. goto nomatch;
  11625. } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57766) {
  11626. if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762)
  11627. strcpy(tp->board_part_number, "BCM57762");
  11628. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766)
  11629. strcpy(tp->board_part_number, "BCM57766");
  11630. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782)
  11631. strcpy(tp->board_part_number, "BCM57782");
  11632. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
  11633. strcpy(tp->board_part_number, "BCM57786");
  11634. else
  11635. goto nomatch;
  11636. } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
  11637. strcpy(tp->board_part_number, "BCM95906");
  11638. } else {
  11639. nomatch:
  11640. strcpy(tp->board_part_number, "none");
  11641. }
  11642. }
  11643. static int __devinit tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
  11644. {
  11645. u32 val;
  11646. if (tg3_nvram_read(tp, offset, &val) ||
  11647. (val & 0xfc000000) != 0x0c000000 ||
  11648. tg3_nvram_read(tp, offset + 4, &val) ||
  11649. val != 0)
  11650. return 0;
  11651. return 1;
  11652. }
  11653. static void __devinit tg3_read_bc_ver(struct tg3 *tp)
  11654. {
  11655. u32 val, offset, start, ver_offset;
  11656. int i, dst_off;
  11657. bool newver = false;
  11658. if (tg3_nvram_read(tp, 0xc, &offset) ||
  11659. tg3_nvram_read(tp, 0x4, &start))
  11660. return;
  11661. offset = tg3_nvram_logical_addr(tp, offset);
  11662. if (tg3_nvram_read(tp, offset, &val))
  11663. return;
  11664. if ((val & 0xfc000000) == 0x0c000000) {
  11665. if (tg3_nvram_read(tp, offset + 4, &val))
  11666. return;
  11667. if (val == 0)
  11668. newver = true;
  11669. }
  11670. dst_off = strlen(tp->fw_ver);
  11671. if (newver) {
  11672. if (TG3_VER_SIZE - dst_off < 16 ||
  11673. tg3_nvram_read(tp, offset + 8, &ver_offset))
  11674. return;
  11675. offset = offset + ver_offset - start;
  11676. for (i = 0; i < 16; i += 4) {
  11677. __be32 v;
  11678. if (tg3_nvram_read_be32(tp, offset + i, &v))
  11679. return;
  11680. memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
  11681. }
  11682. } else {
  11683. u32 major, minor;
  11684. if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
  11685. return;
  11686. major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
  11687. TG3_NVM_BCVER_MAJSFT;
  11688. minor = ver_offset & TG3_NVM_BCVER_MINMSK;
  11689. snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
  11690. "v%d.%02d", major, minor);
  11691. }
  11692. }
  11693. static void __devinit tg3_read_hwsb_ver(struct tg3 *tp)
  11694. {
  11695. u32 val, major, minor;
  11696. /* Use native endian representation */
  11697. if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
  11698. return;
  11699. major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
  11700. TG3_NVM_HWSB_CFG1_MAJSFT;
  11701. minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
  11702. TG3_NVM_HWSB_CFG1_MINSFT;
  11703. snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
  11704. }
  11705. static void __devinit tg3_read_sb_ver(struct tg3 *tp, u32 val)
  11706. {
  11707. u32 offset, major, minor, build;
  11708. strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
  11709. if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
  11710. return;
  11711. switch (val & TG3_EEPROM_SB_REVISION_MASK) {
  11712. case TG3_EEPROM_SB_REVISION_0:
  11713. offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
  11714. break;
  11715. case TG3_EEPROM_SB_REVISION_2:
  11716. offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
  11717. break;
  11718. case TG3_EEPROM_SB_REVISION_3:
  11719. offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
  11720. break;
  11721. case TG3_EEPROM_SB_REVISION_4:
  11722. offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
  11723. break;
  11724. case TG3_EEPROM_SB_REVISION_5:
  11725. offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
  11726. break;
  11727. case TG3_EEPROM_SB_REVISION_6:
  11728. offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
  11729. break;
  11730. default:
  11731. return;
  11732. }
  11733. if (tg3_nvram_read(tp, offset, &val))
  11734. return;
  11735. build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
  11736. TG3_EEPROM_SB_EDH_BLD_SHFT;
  11737. major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
  11738. TG3_EEPROM_SB_EDH_MAJ_SHFT;
  11739. minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
  11740. if (minor > 99 || build > 26)
  11741. return;
  11742. offset = strlen(tp->fw_ver);
  11743. snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
  11744. " v%d.%02d", major, minor);
  11745. if (build > 0) {
  11746. offset = strlen(tp->fw_ver);
  11747. if (offset < TG3_VER_SIZE - 1)
  11748. tp->fw_ver[offset] = 'a' + build - 1;
  11749. }
  11750. }
  11751. static void __devinit tg3_read_mgmtfw_ver(struct tg3 *tp)
  11752. {
  11753. u32 val, offset, start;
  11754. int i, vlen;
  11755. for (offset = TG3_NVM_DIR_START;
  11756. offset < TG3_NVM_DIR_END;
  11757. offset += TG3_NVM_DIRENT_SIZE) {
  11758. if (tg3_nvram_read(tp, offset, &val))
  11759. return;
  11760. if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
  11761. break;
  11762. }
  11763. if (offset == TG3_NVM_DIR_END)
  11764. return;
  11765. if (!tg3_flag(tp, 5705_PLUS))
  11766. start = 0x08000000;
  11767. else if (tg3_nvram_read(tp, offset - 4, &start))
  11768. return;
  11769. if (tg3_nvram_read(tp, offset + 4, &offset) ||
  11770. !tg3_fw_img_is_valid(tp, offset) ||
  11771. tg3_nvram_read(tp, offset + 8, &val))
  11772. return;
  11773. offset += val - start;
  11774. vlen = strlen(tp->fw_ver);
  11775. tp->fw_ver[vlen++] = ',';
  11776. tp->fw_ver[vlen++] = ' ';
  11777. for (i = 0; i < 4; i++) {
  11778. __be32 v;
  11779. if (tg3_nvram_read_be32(tp, offset, &v))
  11780. return;
  11781. offset += sizeof(v);
  11782. if (vlen > TG3_VER_SIZE - sizeof(v)) {
  11783. memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
  11784. break;
  11785. }
  11786. memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
  11787. vlen += sizeof(v);
  11788. }
  11789. }
  11790. static void __devinit tg3_probe_ncsi(struct tg3 *tp)
  11791. {
  11792. u32 apedata;
  11793. apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
  11794. if (apedata != APE_SEG_SIG_MAGIC)
  11795. return;
  11796. apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
  11797. if (!(apedata & APE_FW_STATUS_READY))
  11798. return;
  11799. if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI)
  11800. tg3_flag_set(tp, APE_HAS_NCSI);
  11801. }
  11802. static void __devinit tg3_read_dash_ver(struct tg3 *tp)
  11803. {
  11804. int vlen;
  11805. u32 apedata;
  11806. char *fwtype;
  11807. apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
  11808. if (tg3_flag(tp, APE_HAS_NCSI))
  11809. fwtype = "NCSI";
  11810. else
  11811. fwtype = "DASH";
  11812. vlen = strlen(tp->fw_ver);
  11813. snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
  11814. fwtype,
  11815. (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
  11816. (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
  11817. (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
  11818. (apedata & APE_FW_VERSION_BLDMSK));
  11819. }
  11820. static void __devinit tg3_read_fw_ver(struct tg3 *tp)
  11821. {
  11822. u32 val;
  11823. bool vpd_vers = false;
  11824. if (tp->fw_ver[0] != 0)
  11825. vpd_vers = true;
  11826. if (tg3_flag(tp, NO_NVRAM)) {
  11827. strcat(tp->fw_ver, "sb");
  11828. return;
  11829. }
  11830. if (tg3_nvram_read(tp, 0, &val))
  11831. return;
  11832. if (val == TG3_EEPROM_MAGIC)
  11833. tg3_read_bc_ver(tp);
  11834. else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
  11835. tg3_read_sb_ver(tp, val);
  11836. else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
  11837. tg3_read_hwsb_ver(tp);
  11838. if (tg3_flag(tp, ENABLE_ASF)) {
  11839. if (tg3_flag(tp, ENABLE_APE)) {
  11840. tg3_probe_ncsi(tp);
  11841. if (!vpd_vers)
  11842. tg3_read_dash_ver(tp);
  11843. } else if (!vpd_vers) {
  11844. tg3_read_mgmtfw_ver(tp);
  11845. }
  11846. }
  11847. tp->fw_ver[TG3_VER_SIZE - 1] = 0;
  11848. }
  11849. static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
  11850. {
  11851. if (tg3_flag(tp, LRG_PROD_RING_CAP))
  11852. return TG3_RX_RET_MAX_SIZE_5717;
  11853. else if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))
  11854. return TG3_RX_RET_MAX_SIZE_5700;
  11855. else
  11856. return TG3_RX_RET_MAX_SIZE_5705;
  11857. }
  11858. static DEFINE_PCI_DEVICE_TABLE(tg3_write_reorder_chipsets) = {
  11859. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
  11860. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
  11861. { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
  11862. { },
  11863. };
  11864. static struct pci_dev * __devinit tg3_find_peer(struct tg3 *tp)
  11865. {
  11866. struct pci_dev *peer;
  11867. unsigned int func, devnr = tp->pdev->devfn & ~7;
  11868. for (func = 0; func < 8; func++) {
  11869. peer = pci_get_slot(tp->pdev->bus, devnr | func);
  11870. if (peer && peer != tp->pdev)
  11871. break;
  11872. pci_dev_put(peer);
  11873. }
  11874. /* 5704 can be configured in single-port mode, set peer to
  11875. * tp->pdev in that case.
  11876. */
  11877. if (!peer) {
  11878. peer = tp->pdev;
  11879. return peer;
  11880. }
  11881. /*
  11882. * We don't need to keep the refcount elevated; there's no way
  11883. * to remove one half of this device without removing the other
  11884. */
  11885. pci_dev_put(peer);
  11886. return peer;
  11887. }
  11888. static void __devinit tg3_detect_asic_rev(struct tg3 *tp, u32 misc_ctrl_reg)
  11889. {
  11890. tp->pci_chip_rev_id = misc_ctrl_reg >> MISC_HOST_CTRL_CHIPREV_SHIFT;
  11891. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
  11892. u32 reg;
  11893. /* All devices that use the alternate
  11894. * ASIC REV location have a CPMU.
  11895. */
  11896. tg3_flag_set(tp, CPMU_PRESENT);
  11897. if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
  11898. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C ||
  11899. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
  11900. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
  11901. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720)
  11902. reg = TG3PCI_GEN2_PRODID_ASICREV;
  11903. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
  11904. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
  11905. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
  11906. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
  11907. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
  11908. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
  11909. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762 ||
  11910. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766 ||
  11911. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782 ||
  11912. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
  11913. reg = TG3PCI_GEN15_PRODID_ASICREV;
  11914. else
  11915. reg = TG3PCI_PRODID_ASICREV;
  11916. pci_read_config_dword(tp->pdev, reg, &tp->pci_chip_rev_id);
  11917. }
  11918. /* Wrong chip ID in 5752 A0. This code can be removed later
  11919. * as A0 is not in production.
  11920. */
  11921. if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
  11922. tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
  11923. if (tp->pci_chip_rev_id == CHIPREV_ID_5717_C0)
  11924. tp->pci_chip_rev_id = CHIPREV_ID_5720_A0;
  11925. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
  11926. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
  11927. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
  11928. tg3_flag_set(tp, 5717_PLUS);
  11929. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 ||
  11930. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57766)
  11931. tg3_flag_set(tp, 57765_CLASS);
  11932. if (tg3_flag(tp, 57765_CLASS) || tg3_flag(tp, 5717_PLUS))
  11933. tg3_flag_set(tp, 57765_PLUS);
  11934. /* Intentionally exclude ASIC_REV_5906 */
  11935. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
  11936. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
  11937. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
  11938. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
  11939. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
  11940. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
  11941. tg3_flag(tp, 57765_PLUS))
  11942. tg3_flag_set(tp, 5755_PLUS);
  11943. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
  11944. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)
  11945. tg3_flag_set(tp, 5780_CLASS);
  11946. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
  11947. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
  11948. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
  11949. tg3_flag(tp, 5755_PLUS) ||
  11950. tg3_flag(tp, 5780_CLASS))
  11951. tg3_flag_set(tp, 5750_PLUS);
  11952. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
  11953. tg3_flag(tp, 5750_PLUS))
  11954. tg3_flag_set(tp, 5705_PLUS);
  11955. }
  11956. static int __devinit tg3_get_invariants(struct tg3 *tp)
  11957. {
  11958. u32 misc_ctrl_reg;
  11959. u32 pci_state_reg, grc_misc_cfg;
  11960. u32 val;
  11961. u16 pci_cmd;
  11962. int err;
  11963. /* Force memory write invalidate off. If we leave it on,
  11964. * then on 5700_BX chips we have to enable a workaround.
  11965. * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
  11966. * to match the cacheline size. The Broadcom driver have this
  11967. * workaround but turns MWI off all the times so never uses
  11968. * it. This seems to suggest that the workaround is insufficient.
  11969. */
  11970. pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
  11971. pci_cmd &= ~PCI_COMMAND_INVALIDATE;
  11972. pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
  11973. /* Important! -- Make sure register accesses are byteswapped
  11974. * correctly. Also, for those chips that require it, make
  11975. * sure that indirect register accesses are enabled before
  11976. * the first operation.
  11977. */
  11978. pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
  11979. &misc_ctrl_reg);
  11980. tp->misc_host_ctrl |= (misc_ctrl_reg &
  11981. MISC_HOST_CTRL_CHIPREV);
  11982. pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
  11983. tp->misc_host_ctrl);
  11984. tg3_detect_asic_rev(tp, misc_ctrl_reg);
  11985. /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
  11986. * we need to disable memory and use config. cycles
  11987. * only to access all registers. The 5702/03 chips
  11988. * can mistakenly decode the special cycles from the
  11989. * ICH chipsets as memory write cycles, causing corruption
  11990. * of register and memory space. Only certain ICH bridges
  11991. * will drive special cycles with non-zero data during the
  11992. * address phase which can fall within the 5703's address
  11993. * range. This is not an ICH bug as the PCI spec allows
  11994. * non-zero address during special cycles. However, only
  11995. * these ICH bridges are known to drive non-zero addresses
  11996. * during special cycles.
  11997. *
  11998. * Since special cycles do not cross PCI bridges, we only
  11999. * enable this workaround if the 5703 is on the secondary
  12000. * bus of these ICH bridges.
  12001. */
  12002. if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
  12003. (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
  12004. static struct tg3_dev_id {
  12005. u32 vendor;
  12006. u32 device;
  12007. u32 rev;
  12008. } ich_chipsets[] = {
  12009. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
  12010. PCI_ANY_ID },
  12011. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
  12012. PCI_ANY_ID },
  12013. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
  12014. 0xa },
  12015. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
  12016. PCI_ANY_ID },
  12017. { },
  12018. };
  12019. struct tg3_dev_id *pci_id = &ich_chipsets[0];
  12020. struct pci_dev *bridge = NULL;
  12021. while (pci_id->vendor != 0) {
  12022. bridge = pci_get_device(pci_id->vendor, pci_id->device,
  12023. bridge);
  12024. if (!bridge) {
  12025. pci_id++;
  12026. continue;
  12027. }
  12028. if (pci_id->rev != PCI_ANY_ID) {
  12029. if (bridge->revision > pci_id->rev)
  12030. continue;
  12031. }
  12032. if (bridge->subordinate &&
  12033. (bridge->subordinate->number ==
  12034. tp->pdev->bus->number)) {
  12035. tg3_flag_set(tp, ICH_WORKAROUND);
  12036. pci_dev_put(bridge);
  12037. break;
  12038. }
  12039. }
  12040. }
  12041. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
  12042. static struct tg3_dev_id {
  12043. u32 vendor;
  12044. u32 device;
  12045. } bridge_chipsets[] = {
  12046. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
  12047. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
  12048. { },
  12049. };
  12050. struct tg3_dev_id *pci_id = &bridge_chipsets[0];
  12051. struct pci_dev *bridge = NULL;
  12052. while (pci_id->vendor != 0) {
  12053. bridge = pci_get_device(pci_id->vendor,
  12054. pci_id->device,
  12055. bridge);
  12056. if (!bridge) {
  12057. pci_id++;
  12058. continue;
  12059. }
  12060. if (bridge->subordinate &&
  12061. (bridge->subordinate->number <=
  12062. tp->pdev->bus->number) &&
  12063. (bridge->subordinate->busn_res.end >=
  12064. tp->pdev->bus->number)) {
  12065. tg3_flag_set(tp, 5701_DMA_BUG);
  12066. pci_dev_put(bridge);
  12067. break;
  12068. }
  12069. }
  12070. }
  12071. /* The EPB bridge inside 5714, 5715, and 5780 cannot support
  12072. * DMA addresses > 40-bit. This bridge may have other additional
  12073. * 57xx devices behind it in some 4-port NIC designs for example.
  12074. * Any tg3 device found behind the bridge will also need the 40-bit
  12075. * DMA workaround.
  12076. */
  12077. if (tg3_flag(tp, 5780_CLASS)) {
  12078. tg3_flag_set(tp, 40BIT_DMA_BUG);
  12079. tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
  12080. } else {
  12081. struct pci_dev *bridge = NULL;
  12082. do {
  12083. bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
  12084. PCI_DEVICE_ID_SERVERWORKS_EPB,
  12085. bridge);
  12086. if (bridge && bridge->subordinate &&
  12087. (bridge->subordinate->number <=
  12088. tp->pdev->bus->number) &&
  12089. (bridge->subordinate->busn_res.end >=
  12090. tp->pdev->bus->number)) {
  12091. tg3_flag_set(tp, 40BIT_DMA_BUG);
  12092. pci_dev_put(bridge);
  12093. break;
  12094. }
  12095. } while (bridge);
  12096. }
  12097. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
  12098. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)
  12099. tp->pdev_peer = tg3_find_peer(tp);
  12100. /* Determine TSO capabilities */
  12101. if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0)
  12102. ; /* Do nothing. HW bug. */
  12103. else if (tg3_flag(tp, 57765_PLUS))
  12104. tg3_flag_set(tp, HW_TSO_3);
  12105. else if (tg3_flag(tp, 5755_PLUS) ||
  12106. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
  12107. tg3_flag_set(tp, HW_TSO_2);
  12108. else if (tg3_flag(tp, 5750_PLUS)) {
  12109. tg3_flag_set(tp, HW_TSO_1);
  12110. tg3_flag_set(tp, TSO_BUG);
  12111. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 &&
  12112. tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
  12113. tg3_flag_clear(tp, TSO_BUG);
  12114. } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
  12115. GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
  12116. tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
  12117. tg3_flag_set(tp, TSO_BUG);
  12118. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
  12119. tp->fw_needed = FIRMWARE_TG3TSO5;
  12120. else
  12121. tp->fw_needed = FIRMWARE_TG3TSO;
  12122. }
  12123. /* Selectively allow TSO based on operating conditions */
  12124. if (tg3_flag(tp, HW_TSO_1) ||
  12125. tg3_flag(tp, HW_TSO_2) ||
  12126. tg3_flag(tp, HW_TSO_3) ||
  12127. tp->fw_needed) {
  12128. /* For firmware TSO, assume ASF is disabled.
  12129. * We'll disable TSO later if we discover ASF
  12130. * is enabled in tg3_get_eeprom_hw_cfg().
  12131. */
  12132. tg3_flag_set(tp, TSO_CAPABLE);
  12133. } else {
  12134. tg3_flag_clear(tp, TSO_CAPABLE);
  12135. tg3_flag_clear(tp, TSO_BUG);
  12136. tp->fw_needed = NULL;
  12137. }
  12138. if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
  12139. tp->fw_needed = FIRMWARE_TG3;
  12140. tp->irq_max = 1;
  12141. if (tg3_flag(tp, 5750_PLUS)) {
  12142. tg3_flag_set(tp, SUPPORT_MSI);
  12143. if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
  12144. GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
  12145. (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
  12146. tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
  12147. tp->pdev_peer == tp->pdev))
  12148. tg3_flag_clear(tp, SUPPORT_MSI);
  12149. if (tg3_flag(tp, 5755_PLUS) ||
  12150. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
  12151. tg3_flag_set(tp, 1SHOT_MSI);
  12152. }
  12153. if (tg3_flag(tp, 57765_PLUS)) {
  12154. tg3_flag_set(tp, SUPPORT_MSIX);
  12155. tp->irq_max = TG3_IRQ_MAX_VECS;
  12156. }
  12157. }
  12158. tp->txq_max = 1;
  12159. tp->rxq_max = 1;
  12160. if (tp->irq_max > 1) {
  12161. tp->rxq_max = TG3_RSS_MAX_NUM_QS;
  12162. tg3_rss_init_dflt_indir_tbl(tp, TG3_RSS_MAX_NUM_QS);
  12163. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
  12164. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
  12165. tp->txq_max = tp->irq_max - 1;
  12166. }
  12167. if (tg3_flag(tp, 5755_PLUS) ||
  12168. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
  12169. tg3_flag_set(tp, SHORT_DMA_BUG);
  12170. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
  12171. tp->dma_limit = TG3_TX_BD_DMA_MAX_4K;
  12172. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
  12173. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
  12174. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
  12175. tg3_flag_set(tp, LRG_PROD_RING_CAP);
  12176. if (tg3_flag(tp, 57765_PLUS) &&
  12177. tp->pci_chip_rev_id != CHIPREV_ID_5719_A0)
  12178. tg3_flag_set(tp, USE_JUMBO_BDFLAG);
  12179. if (!tg3_flag(tp, 5705_PLUS) ||
  12180. tg3_flag(tp, 5780_CLASS) ||
  12181. tg3_flag(tp, USE_JUMBO_BDFLAG))
  12182. tg3_flag_set(tp, JUMBO_CAPABLE);
  12183. pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
  12184. &pci_state_reg);
  12185. if (pci_is_pcie(tp->pdev)) {
  12186. u16 lnkctl;
  12187. tg3_flag_set(tp, PCI_EXPRESS);
  12188. pcie_capability_read_word(tp->pdev, PCI_EXP_LNKCTL, &lnkctl);
  12189. if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
  12190. if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
  12191. ASIC_REV_5906) {
  12192. tg3_flag_clear(tp, HW_TSO_2);
  12193. tg3_flag_clear(tp, TSO_CAPABLE);
  12194. }
  12195. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
  12196. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
  12197. tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
  12198. tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
  12199. tg3_flag_set(tp, CLKREQ_BUG);
  12200. } else if (tp->pci_chip_rev_id == CHIPREV_ID_5717_A0) {
  12201. tg3_flag_set(tp, L1PLLPD_EN);
  12202. }
  12203. } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
  12204. /* BCM5785 devices are effectively PCIe devices, and should
  12205. * follow PCIe codepaths, but do not have a PCIe capabilities
  12206. * section.
  12207. */
  12208. tg3_flag_set(tp, PCI_EXPRESS);
  12209. } else if (!tg3_flag(tp, 5705_PLUS) ||
  12210. tg3_flag(tp, 5780_CLASS)) {
  12211. tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
  12212. if (!tp->pcix_cap) {
  12213. dev_err(&tp->pdev->dev,
  12214. "Cannot find PCI-X capability, aborting\n");
  12215. return -EIO;
  12216. }
  12217. if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
  12218. tg3_flag_set(tp, PCIX_MODE);
  12219. }
  12220. /* If we have an AMD 762 or VIA K8T800 chipset, write
  12221. * reordering to the mailbox registers done by the host
  12222. * controller can cause major troubles. We read back from
  12223. * every mailbox register write to force the writes to be
  12224. * posted to the chip in order.
  12225. */
  12226. if (pci_dev_present(tg3_write_reorder_chipsets) &&
  12227. !tg3_flag(tp, PCI_EXPRESS))
  12228. tg3_flag_set(tp, MBOX_WRITE_REORDER);
  12229. pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
  12230. &tp->pci_cacheline_sz);
  12231. pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
  12232. &tp->pci_lat_timer);
  12233. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
  12234. tp->pci_lat_timer < 64) {
  12235. tp->pci_lat_timer = 64;
  12236. pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
  12237. tp->pci_lat_timer);
  12238. }
  12239. /* Important! -- It is critical that the PCI-X hw workaround
  12240. * situation is decided before the first MMIO register access.
  12241. */
  12242. if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
  12243. /* 5700 BX chips need to have their TX producer index
  12244. * mailboxes written twice to workaround a bug.
  12245. */
  12246. tg3_flag_set(tp, TXD_MBOX_HWBUG);
  12247. /* If we are in PCI-X mode, enable register write workaround.
  12248. *
  12249. * The workaround is to use indirect register accesses
  12250. * for all chip writes not to mailbox registers.
  12251. */
  12252. if (tg3_flag(tp, PCIX_MODE)) {
  12253. u32 pm_reg;
  12254. tg3_flag_set(tp, PCIX_TARGET_HWBUG);
  12255. /* The chip can have it's power management PCI config
  12256. * space registers clobbered due to this bug.
  12257. * So explicitly force the chip into D0 here.
  12258. */
  12259. pci_read_config_dword(tp->pdev,
  12260. tp->pm_cap + PCI_PM_CTRL,
  12261. &pm_reg);
  12262. pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
  12263. pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
  12264. pci_write_config_dword(tp->pdev,
  12265. tp->pm_cap + PCI_PM_CTRL,
  12266. pm_reg);
  12267. /* Also, force SERR#/PERR# in PCI command. */
  12268. pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
  12269. pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
  12270. pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
  12271. }
  12272. }
  12273. if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
  12274. tg3_flag_set(tp, PCI_HIGH_SPEED);
  12275. if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
  12276. tg3_flag_set(tp, PCI_32BIT);
  12277. /* Chip-specific fixup from Broadcom driver */
  12278. if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
  12279. (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
  12280. pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
  12281. pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
  12282. }
  12283. /* Default fast path register access methods */
  12284. tp->read32 = tg3_read32;
  12285. tp->write32 = tg3_write32;
  12286. tp->read32_mbox = tg3_read32;
  12287. tp->write32_mbox = tg3_write32;
  12288. tp->write32_tx_mbox = tg3_write32;
  12289. tp->write32_rx_mbox = tg3_write32;
  12290. /* Various workaround register access methods */
  12291. if (tg3_flag(tp, PCIX_TARGET_HWBUG))
  12292. tp->write32 = tg3_write_indirect_reg32;
  12293. else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
  12294. (tg3_flag(tp, PCI_EXPRESS) &&
  12295. tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
  12296. /*
  12297. * Back to back register writes can cause problems on these
  12298. * chips, the workaround is to read back all reg writes
  12299. * except those to mailbox regs.
  12300. *
  12301. * See tg3_write_indirect_reg32().
  12302. */
  12303. tp->write32 = tg3_write_flush_reg32;
  12304. }
  12305. if (tg3_flag(tp, TXD_MBOX_HWBUG) || tg3_flag(tp, MBOX_WRITE_REORDER)) {
  12306. tp->write32_tx_mbox = tg3_write32_tx_mbox;
  12307. if (tg3_flag(tp, MBOX_WRITE_REORDER))
  12308. tp->write32_rx_mbox = tg3_write_flush_reg32;
  12309. }
  12310. if (tg3_flag(tp, ICH_WORKAROUND)) {
  12311. tp->read32 = tg3_read_indirect_reg32;
  12312. tp->write32 = tg3_write_indirect_reg32;
  12313. tp->read32_mbox = tg3_read_indirect_mbox;
  12314. tp->write32_mbox = tg3_write_indirect_mbox;
  12315. tp->write32_tx_mbox = tg3_write_indirect_mbox;
  12316. tp->write32_rx_mbox = tg3_write_indirect_mbox;
  12317. iounmap(tp->regs);
  12318. tp->regs = NULL;
  12319. pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
  12320. pci_cmd &= ~PCI_COMMAND_MEMORY;
  12321. pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
  12322. }
  12323. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
  12324. tp->read32_mbox = tg3_read32_mbox_5906;
  12325. tp->write32_mbox = tg3_write32_mbox_5906;
  12326. tp->write32_tx_mbox = tg3_write32_mbox_5906;
  12327. tp->write32_rx_mbox = tg3_write32_mbox_5906;
  12328. }
  12329. if (tp->write32 == tg3_write_indirect_reg32 ||
  12330. (tg3_flag(tp, PCIX_MODE) &&
  12331. (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
  12332. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
  12333. tg3_flag_set(tp, SRAM_USE_CONFIG);
  12334. /* The memory arbiter has to be enabled in order for SRAM accesses
  12335. * to succeed. Normally on powerup the tg3 chip firmware will make
  12336. * sure it is enabled, but other entities such as system netboot
  12337. * code might disable it.
  12338. */
  12339. val = tr32(MEMARB_MODE);
  12340. tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
  12341. tp->pci_fn = PCI_FUNC(tp->pdev->devfn) & 3;
  12342. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
  12343. tg3_flag(tp, 5780_CLASS)) {
  12344. if (tg3_flag(tp, PCIX_MODE)) {
  12345. pci_read_config_dword(tp->pdev,
  12346. tp->pcix_cap + PCI_X_STATUS,
  12347. &val);
  12348. tp->pci_fn = val & 0x7;
  12349. }
  12350. } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
  12351. tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
  12352. if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) ==
  12353. NIC_SRAM_CPMUSTAT_SIG) {
  12354. tp->pci_fn = val & TG3_CPMU_STATUS_FMSK_5717;
  12355. tp->pci_fn = tp->pci_fn ? 1 : 0;
  12356. }
  12357. } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
  12358. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
  12359. tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
  12360. if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) ==
  12361. NIC_SRAM_CPMUSTAT_SIG) {
  12362. tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5719) >>
  12363. TG3_CPMU_STATUS_FSHFT_5719;
  12364. }
  12365. }
  12366. /* Get eeprom hw config before calling tg3_set_power_state().
  12367. * In particular, the TG3_FLAG_IS_NIC flag must be
  12368. * determined before calling tg3_set_power_state() so that
  12369. * we know whether or not to switch out of Vaux power.
  12370. * When the flag is set, it means that GPIO1 is used for eeprom
  12371. * write protect and also implies that it is a LOM where GPIOs
  12372. * are not used to switch power.
  12373. */
  12374. tg3_get_eeprom_hw_cfg(tp);
  12375. if (tp->fw_needed && tg3_flag(tp, ENABLE_ASF)) {
  12376. tg3_flag_clear(tp, TSO_CAPABLE);
  12377. tg3_flag_clear(tp, TSO_BUG);
  12378. tp->fw_needed = NULL;
  12379. }
  12380. if (tg3_flag(tp, ENABLE_APE)) {
  12381. /* Allow reads and writes to the
  12382. * APE register and memory space.
  12383. */
  12384. pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
  12385. PCISTATE_ALLOW_APE_SHMEM_WR |
  12386. PCISTATE_ALLOW_APE_PSPACE_WR;
  12387. pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
  12388. pci_state_reg);
  12389. tg3_ape_lock_init(tp);
  12390. }
  12391. /* Set up tp->grc_local_ctrl before calling
  12392. * tg3_pwrsrc_switch_to_vmain(). GPIO1 driven high
  12393. * will bring 5700's external PHY out of reset.
  12394. * It is also used as eeprom write protect on LOMs.
  12395. */
  12396. tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
  12397. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
  12398. tg3_flag(tp, EEPROM_WRITE_PROT))
  12399. tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
  12400. GRC_LCLCTRL_GPIO_OUTPUT1);
  12401. /* Unused GPIO3 must be driven as output on 5752 because there
  12402. * are no pull-up resistors on unused GPIO pins.
  12403. */
  12404. else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
  12405. tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
  12406. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
  12407. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
  12408. tg3_flag(tp, 57765_CLASS))
  12409. tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
  12410. if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
  12411. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
  12412. /* Turn off the debug UART. */
  12413. tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
  12414. if (tg3_flag(tp, IS_NIC))
  12415. /* Keep VMain power. */
  12416. tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
  12417. GRC_LCLCTRL_GPIO_OUTPUT0;
  12418. }
  12419. /* Switch out of Vaux if it is a NIC */
  12420. tg3_pwrsrc_switch_to_vmain(tp);
  12421. /* Derive initial jumbo mode from MTU assigned in
  12422. * ether_setup() via the alloc_etherdev() call
  12423. */
  12424. if (tp->dev->mtu > ETH_DATA_LEN && !tg3_flag(tp, 5780_CLASS))
  12425. tg3_flag_set(tp, JUMBO_RING_ENABLE);
  12426. /* Determine WakeOnLan speed to use. */
  12427. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
  12428. tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
  12429. tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
  12430. tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
  12431. tg3_flag_clear(tp, WOL_SPEED_100MB);
  12432. } else {
  12433. tg3_flag_set(tp, WOL_SPEED_100MB);
  12434. }
  12435. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
  12436. tp->phy_flags |= TG3_PHYFLG_IS_FET;
  12437. /* A few boards don't want Ethernet@WireSpeed phy feature */
  12438. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
  12439. (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
  12440. (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
  12441. (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
  12442. (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
  12443. (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
  12444. tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
  12445. if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
  12446. GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
  12447. tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
  12448. if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
  12449. tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
  12450. if (tg3_flag(tp, 5705_PLUS) &&
  12451. !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
  12452. GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
  12453. GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780 &&
  12454. !tg3_flag(tp, 57765_PLUS)) {
  12455. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
  12456. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
  12457. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
  12458. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
  12459. if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
  12460. tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
  12461. tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
  12462. if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
  12463. tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
  12464. } else
  12465. tp->phy_flags |= TG3_PHYFLG_BER_BUG;
  12466. }
  12467. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
  12468. GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
  12469. tp->phy_otp = tg3_read_otp_phycfg(tp);
  12470. if (tp->phy_otp == 0)
  12471. tp->phy_otp = TG3_OTP_DEFAULT;
  12472. }
  12473. if (tg3_flag(tp, CPMU_PRESENT))
  12474. tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
  12475. else
  12476. tp->mi_mode = MAC_MI_MODE_BASE;
  12477. tp->coalesce_mode = 0;
  12478. if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
  12479. GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
  12480. tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
  12481. /* Set these bits to enable statistics workaround. */
  12482. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
  12483. tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
  12484. tp->pci_chip_rev_id == CHIPREV_ID_5720_A0) {
  12485. tp->coalesce_mode |= HOSTCC_MODE_ATTN;
  12486. tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
  12487. }
  12488. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
  12489. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
  12490. tg3_flag_set(tp, USE_PHYLIB);
  12491. err = tg3_mdio_init(tp);
  12492. if (err)
  12493. return err;
  12494. /* Initialize data/descriptor byte/word swapping. */
  12495. val = tr32(GRC_MODE);
  12496. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
  12497. val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
  12498. GRC_MODE_WORD_SWAP_B2HRX_DATA |
  12499. GRC_MODE_B2HRX_ENABLE |
  12500. GRC_MODE_HTX2B_ENABLE |
  12501. GRC_MODE_HOST_STACKUP);
  12502. else
  12503. val &= GRC_MODE_HOST_STACKUP;
  12504. tw32(GRC_MODE, val | tp->grc_mode);
  12505. tg3_switch_clocks(tp);
  12506. /* Clear this out for sanity. */
  12507. tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
  12508. pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
  12509. &pci_state_reg);
  12510. if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
  12511. !tg3_flag(tp, PCIX_TARGET_HWBUG)) {
  12512. u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
  12513. if (chiprevid == CHIPREV_ID_5701_A0 ||
  12514. chiprevid == CHIPREV_ID_5701_B0 ||
  12515. chiprevid == CHIPREV_ID_5701_B2 ||
  12516. chiprevid == CHIPREV_ID_5701_B5) {
  12517. void __iomem *sram_base;
  12518. /* Write some dummy words into the SRAM status block
  12519. * area, see if it reads back correctly. If the return
  12520. * value is bad, force enable the PCIX workaround.
  12521. */
  12522. sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
  12523. writel(0x00000000, sram_base);
  12524. writel(0x00000000, sram_base + 4);
  12525. writel(0xffffffff, sram_base + 4);
  12526. if (readl(sram_base) != 0x00000000)
  12527. tg3_flag_set(tp, PCIX_TARGET_HWBUG);
  12528. }
  12529. }
  12530. udelay(50);
  12531. tg3_nvram_init(tp);
  12532. grc_misc_cfg = tr32(GRC_MISC_CFG);
  12533. grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
  12534. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
  12535. (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
  12536. grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
  12537. tg3_flag_set(tp, IS_5788);
  12538. if (!tg3_flag(tp, IS_5788) &&
  12539. GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
  12540. tg3_flag_set(tp, TAGGED_STATUS);
  12541. if (tg3_flag(tp, TAGGED_STATUS)) {
  12542. tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
  12543. HOSTCC_MODE_CLRTICK_TXBD);
  12544. tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
  12545. pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
  12546. tp->misc_host_ctrl);
  12547. }
  12548. /* Preserve the APE MAC_MODE bits */
  12549. if (tg3_flag(tp, ENABLE_APE))
  12550. tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
  12551. else
  12552. tp->mac_mode = 0;
  12553. /* these are limited to 10/100 only */
  12554. if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
  12555. (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
  12556. (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
  12557. tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
  12558. (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901 ||
  12559. tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901_2 ||
  12560. tp->pdev->device == PCI_DEVICE_ID_TIGON3_5705F)) ||
  12561. (tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
  12562. (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5751F ||
  12563. tp->pdev->device == PCI_DEVICE_ID_TIGON3_5753F ||
  12564. tp->pdev->device == PCI_DEVICE_ID_TIGON3_5787F)) ||
  12565. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790 ||
  12566. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
  12567. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
  12568. (tp->phy_flags & TG3_PHYFLG_IS_FET))
  12569. tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
  12570. err = tg3_phy_probe(tp);
  12571. if (err) {
  12572. dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
  12573. /* ... but do not return immediately ... */
  12574. tg3_mdio_fini(tp);
  12575. }
  12576. tg3_read_vpd(tp);
  12577. tg3_read_fw_ver(tp);
  12578. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
  12579. tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
  12580. } else {
  12581. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
  12582. tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
  12583. else
  12584. tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
  12585. }
  12586. /* 5700 {AX,BX} chips have a broken status block link
  12587. * change bit implementation, so we must use the
  12588. * status register in those cases.
  12589. */
  12590. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
  12591. tg3_flag_set(tp, USE_LINKCHG_REG);
  12592. else
  12593. tg3_flag_clear(tp, USE_LINKCHG_REG);
  12594. /* The led_ctrl is set during tg3_phy_probe, here we might
  12595. * have to force the link status polling mechanism based
  12596. * upon subsystem IDs.
  12597. */
  12598. if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
  12599. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
  12600. !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
  12601. tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
  12602. tg3_flag_set(tp, USE_LINKCHG_REG);
  12603. }
  12604. /* For all SERDES we poll the MAC status register. */
  12605. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
  12606. tg3_flag_set(tp, POLL_SERDES);
  12607. else
  12608. tg3_flag_clear(tp, POLL_SERDES);
  12609. tp->rx_offset = NET_SKB_PAD + NET_IP_ALIGN;
  12610. tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
  12611. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
  12612. tg3_flag(tp, PCIX_MODE)) {
  12613. tp->rx_offset = NET_SKB_PAD;
  12614. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  12615. tp->rx_copy_thresh = ~(u16)0;
  12616. #endif
  12617. }
  12618. tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
  12619. tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
  12620. tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
  12621. tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
  12622. /* Increment the rx prod index on the rx std ring by at most
  12623. * 8 for these chips to workaround hw errata.
  12624. */
  12625. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
  12626. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
  12627. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
  12628. tp->rx_std_max_post = 8;
  12629. if (tg3_flag(tp, ASPM_WORKAROUND))
  12630. tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
  12631. PCIE_PWR_MGMT_L1_THRESH_MSK;
  12632. return err;
  12633. }
  12634. #ifdef CONFIG_SPARC
  12635. static int __devinit tg3_get_macaddr_sparc(struct tg3 *tp)
  12636. {
  12637. struct net_device *dev = tp->dev;
  12638. struct pci_dev *pdev = tp->pdev;
  12639. struct device_node *dp = pci_device_to_OF_node(pdev);
  12640. const unsigned char *addr;
  12641. int len;
  12642. addr = of_get_property(dp, "local-mac-address", &len);
  12643. if (addr && len == 6) {
  12644. memcpy(dev->dev_addr, addr, 6);
  12645. memcpy(dev->perm_addr, dev->dev_addr, 6);
  12646. return 0;
  12647. }
  12648. return -ENODEV;
  12649. }
  12650. static int __devinit tg3_get_default_macaddr_sparc(struct tg3 *tp)
  12651. {
  12652. struct net_device *dev = tp->dev;
  12653. memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
  12654. memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
  12655. return 0;
  12656. }
  12657. #endif
  12658. static int __devinit tg3_get_device_address(struct tg3 *tp)
  12659. {
  12660. struct net_device *dev = tp->dev;
  12661. u32 hi, lo, mac_offset;
  12662. int addr_ok = 0;
  12663. #ifdef CONFIG_SPARC
  12664. if (!tg3_get_macaddr_sparc(tp))
  12665. return 0;
  12666. #endif
  12667. mac_offset = 0x7c;
  12668. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
  12669. tg3_flag(tp, 5780_CLASS)) {
  12670. if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
  12671. mac_offset = 0xcc;
  12672. if (tg3_nvram_lock(tp))
  12673. tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
  12674. else
  12675. tg3_nvram_unlock(tp);
  12676. } else if (tg3_flag(tp, 5717_PLUS)) {
  12677. if (tp->pci_fn & 1)
  12678. mac_offset = 0xcc;
  12679. if (tp->pci_fn > 1)
  12680. mac_offset += 0x18c;
  12681. } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
  12682. mac_offset = 0x10;
  12683. /* First try to get it from MAC address mailbox. */
  12684. tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
  12685. if ((hi >> 16) == 0x484b) {
  12686. dev->dev_addr[0] = (hi >> 8) & 0xff;
  12687. dev->dev_addr[1] = (hi >> 0) & 0xff;
  12688. tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
  12689. dev->dev_addr[2] = (lo >> 24) & 0xff;
  12690. dev->dev_addr[3] = (lo >> 16) & 0xff;
  12691. dev->dev_addr[4] = (lo >> 8) & 0xff;
  12692. dev->dev_addr[5] = (lo >> 0) & 0xff;
  12693. /* Some old bootcode may report a 0 MAC address in SRAM */
  12694. addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
  12695. }
  12696. if (!addr_ok) {
  12697. /* Next, try NVRAM. */
  12698. if (!tg3_flag(tp, NO_NVRAM) &&
  12699. !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
  12700. !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
  12701. memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
  12702. memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
  12703. }
  12704. /* Finally just fetch it out of the MAC control regs. */
  12705. else {
  12706. hi = tr32(MAC_ADDR_0_HIGH);
  12707. lo = tr32(MAC_ADDR_0_LOW);
  12708. dev->dev_addr[5] = lo & 0xff;
  12709. dev->dev_addr[4] = (lo >> 8) & 0xff;
  12710. dev->dev_addr[3] = (lo >> 16) & 0xff;
  12711. dev->dev_addr[2] = (lo >> 24) & 0xff;
  12712. dev->dev_addr[1] = hi & 0xff;
  12713. dev->dev_addr[0] = (hi >> 8) & 0xff;
  12714. }
  12715. }
  12716. if (!is_valid_ether_addr(&dev->dev_addr[0])) {
  12717. #ifdef CONFIG_SPARC
  12718. if (!tg3_get_default_macaddr_sparc(tp))
  12719. return 0;
  12720. #endif
  12721. return -EINVAL;
  12722. }
  12723. memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
  12724. return 0;
  12725. }
  12726. #define BOUNDARY_SINGLE_CACHELINE 1
  12727. #define BOUNDARY_MULTI_CACHELINE 2
  12728. static u32 __devinit tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
  12729. {
  12730. int cacheline_size;
  12731. u8 byte;
  12732. int goal;
  12733. pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
  12734. if (byte == 0)
  12735. cacheline_size = 1024;
  12736. else
  12737. cacheline_size = (int) byte * 4;
  12738. /* On 5703 and later chips, the boundary bits have no
  12739. * effect.
  12740. */
  12741. if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
  12742. GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
  12743. !tg3_flag(tp, PCI_EXPRESS))
  12744. goto out;
  12745. #if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
  12746. goal = BOUNDARY_MULTI_CACHELINE;
  12747. #else
  12748. #if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
  12749. goal = BOUNDARY_SINGLE_CACHELINE;
  12750. #else
  12751. goal = 0;
  12752. #endif
  12753. #endif
  12754. if (tg3_flag(tp, 57765_PLUS)) {
  12755. val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
  12756. goto out;
  12757. }
  12758. if (!goal)
  12759. goto out;
  12760. /* PCI controllers on most RISC systems tend to disconnect
  12761. * when a device tries to burst across a cache-line boundary.
  12762. * Therefore, letting tg3 do so just wastes PCI bandwidth.
  12763. *
  12764. * Unfortunately, for PCI-E there are only limited
  12765. * write-side controls for this, and thus for reads
  12766. * we will still get the disconnects. We'll also waste
  12767. * these PCI cycles for both read and write for chips
  12768. * other than 5700 and 5701 which do not implement the
  12769. * boundary bits.
  12770. */
  12771. if (tg3_flag(tp, PCIX_MODE) && !tg3_flag(tp, PCI_EXPRESS)) {
  12772. switch (cacheline_size) {
  12773. case 16:
  12774. case 32:
  12775. case 64:
  12776. case 128:
  12777. if (goal == BOUNDARY_SINGLE_CACHELINE) {
  12778. val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
  12779. DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
  12780. } else {
  12781. val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
  12782. DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
  12783. }
  12784. break;
  12785. case 256:
  12786. val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
  12787. DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
  12788. break;
  12789. default:
  12790. val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
  12791. DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
  12792. break;
  12793. }
  12794. } else if (tg3_flag(tp, PCI_EXPRESS)) {
  12795. switch (cacheline_size) {
  12796. case 16:
  12797. case 32:
  12798. case 64:
  12799. if (goal == BOUNDARY_SINGLE_CACHELINE) {
  12800. val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
  12801. val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
  12802. break;
  12803. }
  12804. /* fallthrough */
  12805. case 128:
  12806. default:
  12807. val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
  12808. val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
  12809. break;
  12810. }
  12811. } else {
  12812. switch (cacheline_size) {
  12813. case 16:
  12814. if (goal == BOUNDARY_SINGLE_CACHELINE) {
  12815. val |= (DMA_RWCTRL_READ_BNDRY_16 |
  12816. DMA_RWCTRL_WRITE_BNDRY_16);
  12817. break;
  12818. }
  12819. /* fallthrough */
  12820. case 32:
  12821. if (goal == BOUNDARY_SINGLE_CACHELINE) {
  12822. val |= (DMA_RWCTRL_READ_BNDRY_32 |
  12823. DMA_RWCTRL_WRITE_BNDRY_32);
  12824. break;
  12825. }
  12826. /* fallthrough */
  12827. case 64:
  12828. if (goal == BOUNDARY_SINGLE_CACHELINE) {
  12829. val |= (DMA_RWCTRL_READ_BNDRY_64 |
  12830. DMA_RWCTRL_WRITE_BNDRY_64);
  12831. break;
  12832. }
  12833. /* fallthrough */
  12834. case 128:
  12835. if (goal == BOUNDARY_SINGLE_CACHELINE) {
  12836. val |= (DMA_RWCTRL_READ_BNDRY_128 |
  12837. DMA_RWCTRL_WRITE_BNDRY_128);
  12838. break;
  12839. }
  12840. /* fallthrough */
  12841. case 256:
  12842. val |= (DMA_RWCTRL_READ_BNDRY_256 |
  12843. DMA_RWCTRL_WRITE_BNDRY_256);
  12844. break;
  12845. case 512:
  12846. val |= (DMA_RWCTRL_READ_BNDRY_512 |
  12847. DMA_RWCTRL_WRITE_BNDRY_512);
  12848. break;
  12849. case 1024:
  12850. default:
  12851. val |= (DMA_RWCTRL_READ_BNDRY_1024 |
  12852. DMA_RWCTRL_WRITE_BNDRY_1024);
  12853. break;
  12854. }
  12855. }
  12856. out:
  12857. return val;
  12858. }
  12859. static int __devinit tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma, int size, int to_device)
  12860. {
  12861. struct tg3_internal_buffer_desc test_desc;
  12862. u32 sram_dma_descs;
  12863. int i, ret;
  12864. sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
  12865. tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
  12866. tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
  12867. tw32(RDMAC_STATUS, 0);
  12868. tw32(WDMAC_STATUS, 0);
  12869. tw32(BUFMGR_MODE, 0);
  12870. tw32(FTQ_RESET, 0);
  12871. test_desc.addr_hi = ((u64) buf_dma) >> 32;
  12872. test_desc.addr_lo = buf_dma & 0xffffffff;
  12873. test_desc.nic_mbuf = 0x00002100;
  12874. test_desc.len = size;
  12875. /*
  12876. * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
  12877. * the *second* time the tg3 driver was getting loaded after an
  12878. * initial scan.
  12879. *
  12880. * Broadcom tells me:
  12881. * ...the DMA engine is connected to the GRC block and a DMA
  12882. * reset may affect the GRC block in some unpredictable way...
  12883. * The behavior of resets to individual blocks has not been tested.
  12884. *
  12885. * Broadcom noted the GRC reset will also reset all sub-components.
  12886. */
  12887. if (to_device) {
  12888. test_desc.cqid_sqid = (13 << 8) | 2;
  12889. tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
  12890. udelay(40);
  12891. } else {
  12892. test_desc.cqid_sqid = (16 << 8) | 7;
  12893. tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
  12894. udelay(40);
  12895. }
  12896. test_desc.flags = 0x00000005;
  12897. for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
  12898. u32 val;
  12899. val = *(((u32 *)&test_desc) + i);
  12900. pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
  12901. sram_dma_descs + (i * sizeof(u32)));
  12902. pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
  12903. }
  12904. pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
  12905. if (to_device)
  12906. tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
  12907. else
  12908. tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
  12909. ret = -ENODEV;
  12910. for (i = 0; i < 40; i++) {
  12911. u32 val;
  12912. if (to_device)
  12913. val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
  12914. else
  12915. val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
  12916. if ((val & 0xffff) == sram_dma_descs) {
  12917. ret = 0;
  12918. break;
  12919. }
  12920. udelay(100);
  12921. }
  12922. return ret;
  12923. }
  12924. #define TEST_BUFFER_SIZE 0x2000
  12925. static DEFINE_PCI_DEVICE_TABLE(tg3_dma_wait_state_chipsets) = {
  12926. { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
  12927. { },
  12928. };
  12929. static int __devinit tg3_test_dma(struct tg3 *tp)
  12930. {
  12931. dma_addr_t buf_dma;
  12932. u32 *buf, saved_dma_rwctrl;
  12933. int ret = 0;
  12934. buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
  12935. &buf_dma, GFP_KERNEL);
  12936. if (!buf) {
  12937. ret = -ENOMEM;
  12938. goto out_nofree;
  12939. }
  12940. tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
  12941. (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
  12942. tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
  12943. if (tg3_flag(tp, 57765_PLUS))
  12944. goto out;
  12945. if (tg3_flag(tp, PCI_EXPRESS)) {
  12946. /* DMA read watermark not used on PCIE */
  12947. tp->dma_rwctrl |= 0x00180000;
  12948. } else if (!tg3_flag(tp, PCIX_MODE)) {
  12949. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
  12950. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
  12951. tp->dma_rwctrl |= 0x003f0000;
  12952. else
  12953. tp->dma_rwctrl |= 0x003f000f;
  12954. } else {
  12955. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
  12956. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
  12957. u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
  12958. u32 read_water = 0x7;
  12959. /* If the 5704 is behind the EPB bridge, we can
  12960. * do the less restrictive ONE_DMA workaround for
  12961. * better performance.
  12962. */
  12963. if (tg3_flag(tp, 40BIT_DMA_BUG) &&
  12964. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
  12965. tp->dma_rwctrl |= 0x8000;
  12966. else if (ccval == 0x6 || ccval == 0x7)
  12967. tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
  12968. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
  12969. read_water = 4;
  12970. /* Set bit 23 to enable PCIX hw bug fix */
  12971. tp->dma_rwctrl |=
  12972. (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
  12973. (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
  12974. (1 << 23);
  12975. } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
  12976. /* 5780 always in PCIX mode */
  12977. tp->dma_rwctrl |= 0x00144000;
  12978. } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
  12979. /* 5714 always in PCIX mode */
  12980. tp->dma_rwctrl |= 0x00148000;
  12981. } else {
  12982. tp->dma_rwctrl |= 0x001b000f;
  12983. }
  12984. }
  12985. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
  12986. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
  12987. tp->dma_rwctrl &= 0xfffffff0;
  12988. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
  12989. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
  12990. /* Remove this if it causes problems for some boards. */
  12991. tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
  12992. /* On 5700/5701 chips, we need to set this bit.
  12993. * Otherwise the chip will issue cacheline transactions
  12994. * to streamable DMA memory with not all the byte
  12995. * enables turned on. This is an error on several
  12996. * RISC PCI controllers, in particular sparc64.
  12997. *
  12998. * On 5703/5704 chips, this bit has been reassigned
  12999. * a different meaning. In particular, it is used
  13000. * on those chips to enable a PCI-X workaround.
  13001. */
  13002. tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
  13003. }
  13004. tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
  13005. #if 0
  13006. /* Unneeded, already done by tg3_get_invariants. */
  13007. tg3_switch_clocks(tp);
  13008. #endif
  13009. if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
  13010. GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
  13011. goto out;
  13012. /* It is best to perform DMA test with maximum write burst size
  13013. * to expose the 5700/5701 write DMA bug.
  13014. */
  13015. saved_dma_rwctrl = tp->dma_rwctrl;
  13016. tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
  13017. tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
  13018. while (1) {
  13019. u32 *p = buf, i;
  13020. for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
  13021. p[i] = i;
  13022. /* Send the buffer to the chip. */
  13023. ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
  13024. if (ret) {
  13025. dev_err(&tp->pdev->dev,
  13026. "%s: Buffer write failed. err = %d\n",
  13027. __func__, ret);
  13028. break;
  13029. }
  13030. #if 0
  13031. /* validate data reached card RAM correctly. */
  13032. for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
  13033. u32 val;
  13034. tg3_read_mem(tp, 0x2100 + (i*4), &val);
  13035. if (le32_to_cpu(val) != p[i]) {
  13036. dev_err(&tp->pdev->dev,
  13037. "%s: Buffer corrupted on device! "
  13038. "(%d != %d)\n", __func__, val, i);
  13039. /* ret = -ENODEV here? */
  13040. }
  13041. p[i] = 0;
  13042. }
  13043. #endif
  13044. /* Now read it back. */
  13045. ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
  13046. if (ret) {
  13047. dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
  13048. "err = %d\n", __func__, ret);
  13049. break;
  13050. }
  13051. /* Verify it. */
  13052. for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
  13053. if (p[i] == i)
  13054. continue;
  13055. if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
  13056. DMA_RWCTRL_WRITE_BNDRY_16) {
  13057. tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
  13058. tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
  13059. tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
  13060. break;
  13061. } else {
  13062. dev_err(&tp->pdev->dev,
  13063. "%s: Buffer corrupted on read back! "
  13064. "(%d != %d)\n", __func__, p[i], i);
  13065. ret = -ENODEV;
  13066. goto out;
  13067. }
  13068. }
  13069. if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
  13070. /* Success. */
  13071. ret = 0;
  13072. break;
  13073. }
  13074. }
  13075. if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
  13076. DMA_RWCTRL_WRITE_BNDRY_16) {
  13077. /* DMA test passed without adjusting DMA boundary,
  13078. * now look for chipsets that are known to expose the
  13079. * DMA bug without failing the test.
  13080. */
  13081. if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
  13082. tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
  13083. tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
  13084. } else {
  13085. /* Safe to use the calculated DMA boundary. */
  13086. tp->dma_rwctrl = saved_dma_rwctrl;
  13087. }
  13088. tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
  13089. }
  13090. out:
  13091. dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
  13092. out_nofree:
  13093. return ret;
  13094. }
  13095. static void __devinit tg3_init_bufmgr_config(struct tg3 *tp)
  13096. {
  13097. if (tg3_flag(tp, 57765_PLUS)) {
  13098. tp->bufmgr_config.mbuf_read_dma_low_water =
  13099. DEFAULT_MB_RDMA_LOW_WATER_5705;
  13100. tp->bufmgr_config.mbuf_mac_rx_low_water =
  13101. DEFAULT_MB_MACRX_LOW_WATER_57765;
  13102. tp->bufmgr_config.mbuf_high_water =
  13103. DEFAULT_MB_HIGH_WATER_57765;
  13104. tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
  13105. DEFAULT_MB_RDMA_LOW_WATER_5705;
  13106. tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
  13107. DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
  13108. tp->bufmgr_config.mbuf_high_water_jumbo =
  13109. DEFAULT_MB_HIGH_WATER_JUMBO_57765;
  13110. } else if (tg3_flag(tp, 5705_PLUS)) {
  13111. tp->bufmgr_config.mbuf_read_dma_low_water =
  13112. DEFAULT_MB_RDMA_LOW_WATER_5705;
  13113. tp->bufmgr_config.mbuf_mac_rx_low_water =
  13114. DEFAULT_MB_MACRX_LOW_WATER_5705;
  13115. tp->bufmgr_config.mbuf_high_water =
  13116. DEFAULT_MB_HIGH_WATER_5705;
  13117. if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
  13118. tp->bufmgr_config.mbuf_mac_rx_low_water =
  13119. DEFAULT_MB_MACRX_LOW_WATER_5906;
  13120. tp->bufmgr_config.mbuf_high_water =
  13121. DEFAULT_MB_HIGH_WATER_5906;
  13122. }
  13123. tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
  13124. DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
  13125. tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
  13126. DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
  13127. tp->bufmgr_config.mbuf_high_water_jumbo =
  13128. DEFAULT_MB_HIGH_WATER_JUMBO_5780;
  13129. } else {
  13130. tp->bufmgr_config.mbuf_read_dma_low_water =
  13131. DEFAULT_MB_RDMA_LOW_WATER;
  13132. tp->bufmgr_config.mbuf_mac_rx_low_water =
  13133. DEFAULT_MB_MACRX_LOW_WATER;
  13134. tp->bufmgr_config.mbuf_high_water =
  13135. DEFAULT_MB_HIGH_WATER;
  13136. tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
  13137. DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
  13138. tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
  13139. DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
  13140. tp->bufmgr_config.mbuf_high_water_jumbo =
  13141. DEFAULT_MB_HIGH_WATER_JUMBO;
  13142. }
  13143. tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
  13144. tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
  13145. }
  13146. static char * __devinit tg3_phy_string(struct tg3 *tp)
  13147. {
  13148. switch (tp->phy_id & TG3_PHY_ID_MASK) {
  13149. case TG3_PHY_ID_BCM5400: return "5400";
  13150. case TG3_PHY_ID_BCM5401: return "5401";
  13151. case TG3_PHY_ID_BCM5411: return "5411";
  13152. case TG3_PHY_ID_BCM5701: return "5701";
  13153. case TG3_PHY_ID_BCM5703: return "5703";
  13154. case TG3_PHY_ID_BCM5704: return "5704";
  13155. case TG3_PHY_ID_BCM5705: return "5705";
  13156. case TG3_PHY_ID_BCM5750: return "5750";
  13157. case TG3_PHY_ID_BCM5752: return "5752";
  13158. case TG3_PHY_ID_BCM5714: return "5714";
  13159. case TG3_PHY_ID_BCM5780: return "5780";
  13160. case TG3_PHY_ID_BCM5755: return "5755";
  13161. case TG3_PHY_ID_BCM5787: return "5787";
  13162. case TG3_PHY_ID_BCM5784: return "5784";
  13163. case TG3_PHY_ID_BCM5756: return "5722/5756";
  13164. case TG3_PHY_ID_BCM5906: return "5906";
  13165. case TG3_PHY_ID_BCM5761: return "5761";
  13166. case TG3_PHY_ID_BCM5718C: return "5718C";
  13167. case TG3_PHY_ID_BCM5718S: return "5718S";
  13168. case TG3_PHY_ID_BCM57765: return "57765";
  13169. case TG3_PHY_ID_BCM5719C: return "5719C";
  13170. case TG3_PHY_ID_BCM5720C: return "5720C";
  13171. case TG3_PHY_ID_BCM8002: return "8002/serdes";
  13172. case 0: return "serdes";
  13173. default: return "unknown";
  13174. }
  13175. }
  13176. static char * __devinit tg3_bus_string(struct tg3 *tp, char *str)
  13177. {
  13178. if (tg3_flag(tp, PCI_EXPRESS)) {
  13179. strcpy(str, "PCI Express");
  13180. return str;
  13181. } else if (tg3_flag(tp, PCIX_MODE)) {
  13182. u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
  13183. strcpy(str, "PCIX:");
  13184. if ((clock_ctrl == 7) ||
  13185. ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
  13186. GRC_MISC_CFG_BOARD_ID_5704CIOBE))
  13187. strcat(str, "133MHz");
  13188. else if (clock_ctrl == 0)
  13189. strcat(str, "33MHz");
  13190. else if (clock_ctrl == 2)
  13191. strcat(str, "50MHz");
  13192. else if (clock_ctrl == 4)
  13193. strcat(str, "66MHz");
  13194. else if (clock_ctrl == 6)
  13195. strcat(str, "100MHz");
  13196. } else {
  13197. strcpy(str, "PCI:");
  13198. if (tg3_flag(tp, PCI_HIGH_SPEED))
  13199. strcat(str, "66MHz");
  13200. else
  13201. strcat(str, "33MHz");
  13202. }
  13203. if (tg3_flag(tp, PCI_32BIT))
  13204. strcat(str, ":32-bit");
  13205. else
  13206. strcat(str, ":64-bit");
  13207. return str;
  13208. }
  13209. static void __devinit tg3_init_coal(struct tg3 *tp)
  13210. {
  13211. struct ethtool_coalesce *ec = &tp->coal;
  13212. memset(ec, 0, sizeof(*ec));
  13213. ec->cmd = ETHTOOL_GCOALESCE;
  13214. ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
  13215. ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
  13216. ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
  13217. ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
  13218. ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
  13219. ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
  13220. ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
  13221. ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
  13222. ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
  13223. if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
  13224. HOSTCC_MODE_CLRTICK_TXBD)) {
  13225. ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
  13226. ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
  13227. ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
  13228. ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
  13229. }
  13230. if (tg3_flag(tp, 5705_PLUS)) {
  13231. ec->rx_coalesce_usecs_irq = 0;
  13232. ec->tx_coalesce_usecs_irq = 0;
  13233. ec->stats_block_coalesce_usecs = 0;
  13234. }
  13235. }
  13236. static int __devinit tg3_init_one(struct pci_dev *pdev,
  13237. const struct pci_device_id *ent)
  13238. {
  13239. struct net_device *dev;
  13240. struct tg3 *tp;
  13241. int i, err, pm_cap;
  13242. u32 sndmbx, rcvmbx, intmbx;
  13243. char str[40];
  13244. u64 dma_mask, persist_dma_mask;
  13245. netdev_features_t features = 0;
  13246. printk_once(KERN_INFO "%s\n", version);
  13247. err = pci_enable_device(pdev);
  13248. if (err) {
  13249. dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
  13250. return err;
  13251. }
  13252. err = pci_request_regions(pdev, DRV_MODULE_NAME);
  13253. if (err) {
  13254. dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
  13255. goto err_out_disable_pdev;
  13256. }
  13257. pci_set_master(pdev);
  13258. /* Find power-management capability. */
  13259. pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
  13260. if (pm_cap == 0) {
  13261. dev_err(&pdev->dev,
  13262. "Cannot find Power Management capability, aborting\n");
  13263. err = -EIO;
  13264. goto err_out_free_res;
  13265. }
  13266. err = pci_set_power_state(pdev, PCI_D0);
  13267. if (err) {
  13268. dev_err(&pdev->dev, "Transition to D0 failed, aborting\n");
  13269. goto err_out_free_res;
  13270. }
  13271. dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
  13272. if (!dev) {
  13273. err = -ENOMEM;
  13274. goto err_out_power_down;
  13275. }
  13276. SET_NETDEV_DEV(dev, &pdev->dev);
  13277. tp = netdev_priv(dev);
  13278. tp->pdev = pdev;
  13279. tp->dev = dev;
  13280. tp->pm_cap = pm_cap;
  13281. tp->rx_mode = TG3_DEF_RX_MODE;
  13282. tp->tx_mode = TG3_DEF_TX_MODE;
  13283. if (tg3_debug > 0)
  13284. tp->msg_enable = tg3_debug;
  13285. else
  13286. tp->msg_enable = TG3_DEF_MSG_ENABLE;
  13287. /* The word/byte swap controls here control register access byte
  13288. * swapping. DMA data byte swapping is controlled in the GRC_MODE
  13289. * setting below.
  13290. */
  13291. tp->misc_host_ctrl =
  13292. MISC_HOST_CTRL_MASK_PCI_INT |
  13293. MISC_HOST_CTRL_WORD_SWAP |
  13294. MISC_HOST_CTRL_INDIR_ACCESS |
  13295. MISC_HOST_CTRL_PCISTATE_RW;
  13296. /* The NONFRM (non-frame) byte/word swap controls take effect
  13297. * on descriptor entries, anything which isn't packet data.
  13298. *
  13299. * The StrongARM chips on the board (one for tx, one for rx)
  13300. * are running in big-endian mode.
  13301. */
  13302. tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
  13303. GRC_MODE_WSWAP_NONFRM_DATA);
  13304. #ifdef __BIG_ENDIAN
  13305. tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
  13306. #endif
  13307. spin_lock_init(&tp->lock);
  13308. spin_lock_init(&tp->indirect_lock);
  13309. INIT_WORK(&tp->reset_task, tg3_reset_task);
  13310. tp->regs = pci_ioremap_bar(pdev, BAR_0);
  13311. if (!tp->regs) {
  13312. dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
  13313. err = -ENOMEM;
  13314. goto err_out_free_dev;
  13315. }
  13316. if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
  13317. tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761E ||
  13318. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S ||
  13319. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761SE ||
  13320. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
  13321. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C ||
  13322. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
  13323. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
  13324. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720) {
  13325. tg3_flag_set(tp, ENABLE_APE);
  13326. tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
  13327. if (!tp->aperegs) {
  13328. dev_err(&pdev->dev,
  13329. "Cannot map APE registers, aborting\n");
  13330. err = -ENOMEM;
  13331. goto err_out_iounmap;
  13332. }
  13333. }
  13334. tp->rx_pending = TG3_DEF_RX_RING_PENDING;
  13335. tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
  13336. dev->ethtool_ops = &tg3_ethtool_ops;
  13337. dev->watchdog_timeo = TG3_TX_TIMEOUT;
  13338. dev->netdev_ops = &tg3_netdev_ops;
  13339. dev->irq = pdev->irq;
  13340. err = tg3_get_invariants(tp);
  13341. if (err) {
  13342. dev_err(&pdev->dev,
  13343. "Problem fetching invariants of chip, aborting\n");
  13344. goto err_out_apeunmap;
  13345. }
  13346. /* The EPB bridge inside 5714, 5715, and 5780 and any
  13347. * device behind the EPB cannot support DMA addresses > 40-bit.
  13348. * On 64-bit systems with IOMMU, use 40-bit dma_mask.
  13349. * On 64-bit systems without IOMMU, use 64-bit dma_mask and
  13350. * do DMA address check in tg3_start_xmit().
  13351. */
  13352. if (tg3_flag(tp, IS_5788))
  13353. persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
  13354. else if (tg3_flag(tp, 40BIT_DMA_BUG)) {
  13355. persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
  13356. #ifdef CONFIG_HIGHMEM
  13357. dma_mask = DMA_BIT_MASK(64);
  13358. #endif
  13359. } else
  13360. persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
  13361. /* Configure DMA attributes. */
  13362. if (dma_mask > DMA_BIT_MASK(32)) {
  13363. err = pci_set_dma_mask(pdev, dma_mask);
  13364. if (!err) {
  13365. features |= NETIF_F_HIGHDMA;
  13366. err = pci_set_consistent_dma_mask(pdev,
  13367. persist_dma_mask);
  13368. if (err < 0) {
  13369. dev_err(&pdev->dev, "Unable to obtain 64 bit "
  13370. "DMA for consistent allocations\n");
  13371. goto err_out_apeunmap;
  13372. }
  13373. }
  13374. }
  13375. if (err || dma_mask == DMA_BIT_MASK(32)) {
  13376. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  13377. if (err) {
  13378. dev_err(&pdev->dev,
  13379. "No usable DMA configuration, aborting\n");
  13380. goto err_out_apeunmap;
  13381. }
  13382. }
  13383. tg3_init_bufmgr_config(tp);
  13384. features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
  13385. /* 5700 B0 chips do not support checksumming correctly due
  13386. * to hardware bugs.
  13387. */
  13388. if (tp->pci_chip_rev_id != CHIPREV_ID_5700_B0) {
  13389. features |= NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
  13390. if (tg3_flag(tp, 5755_PLUS))
  13391. features |= NETIF_F_IPV6_CSUM;
  13392. }
  13393. /* TSO is on by default on chips that support hardware TSO.
  13394. * Firmware TSO on older chips gives lower performance, so it
  13395. * is off by default, but can be enabled using ethtool.
  13396. */
  13397. if ((tg3_flag(tp, HW_TSO_1) ||
  13398. tg3_flag(tp, HW_TSO_2) ||
  13399. tg3_flag(tp, HW_TSO_3)) &&
  13400. (features & NETIF_F_IP_CSUM))
  13401. features |= NETIF_F_TSO;
  13402. if (tg3_flag(tp, HW_TSO_2) || tg3_flag(tp, HW_TSO_3)) {
  13403. if (features & NETIF_F_IPV6_CSUM)
  13404. features |= NETIF_F_TSO6;
  13405. if (tg3_flag(tp, HW_TSO_3) ||
  13406. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
  13407. (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
  13408. GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
  13409. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
  13410. GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
  13411. features |= NETIF_F_TSO_ECN;
  13412. }
  13413. dev->features |= features;
  13414. dev->vlan_features |= features;
  13415. /*
  13416. * Add loopback capability only for a subset of devices that support
  13417. * MAC-LOOPBACK. Eventually this need to be enhanced to allow INT-PHY
  13418. * loopback for the remaining devices.
  13419. */
  13420. if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
  13421. !tg3_flag(tp, CPMU_PRESENT))
  13422. /* Add the loopback capability */
  13423. features |= NETIF_F_LOOPBACK;
  13424. dev->hw_features |= features;
  13425. if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
  13426. !tg3_flag(tp, TSO_CAPABLE) &&
  13427. !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
  13428. tg3_flag_set(tp, MAX_RXPEND_64);
  13429. tp->rx_pending = 63;
  13430. }
  13431. err = tg3_get_device_address(tp);
  13432. if (err) {
  13433. dev_err(&pdev->dev,
  13434. "Could not obtain valid ethernet address, aborting\n");
  13435. goto err_out_apeunmap;
  13436. }
  13437. /*
  13438. * Reset chip in case UNDI or EFI driver did not shutdown
  13439. * DMA self test will enable WDMAC and we'll see (spurious)
  13440. * pending DMA on the PCI bus at that point.
  13441. */
  13442. if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
  13443. (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
  13444. tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
  13445. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  13446. }
  13447. err = tg3_test_dma(tp);
  13448. if (err) {
  13449. dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
  13450. goto err_out_apeunmap;
  13451. }
  13452. intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
  13453. rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
  13454. sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
  13455. for (i = 0; i < tp->irq_max; i++) {
  13456. struct tg3_napi *tnapi = &tp->napi[i];
  13457. tnapi->tp = tp;
  13458. tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
  13459. tnapi->int_mbox = intmbx;
  13460. if (i <= 4)
  13461. intmbx += 0x8;
  13462. else
  13463. intmbx += 0x4;
  13464. tnapi->consmbox = rcvmbx;
  13465. tnapi->prodmbox = sndmbx;
  13466. if (i)
  13467. tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
  13468. else
  13469. tnapi->coal_now = HOSTCC_MODE_NOW;
  13470. if (!tg3_flag(tp, SUPPORT_MSIX))
  13471. break;
  13472. /*
  13473. * If we support MSIX, we'll be using RSS. If we're using
  13474. * RSS, the first vector only handles link interrupts and the
  13475. * remaining vectors handle rx and tx interrupts. Reuse the
  13476. * mailbox values for the next iteration. The values we setup
  13477. * above are still useful for the single vectored mode.
  13478. */
  13479. if (!i)
  13480. continue;
  13481. rcvmbx += 0x8;
  13482. if (sndmbx & 0x4)
  13483. sndmbx -= 0x4;
  13484. else
  13485. sndmbx += 0xc;
  13486. }
  13487. tg3_init_coal(tp);
  13488. pci_set_drvdata(pdev, dev);
  13489. if (tg3_flag(tp, 5717_PLUS)) {
  13490. /* Resume a low-power mode */
  13491. tg3_frob_aux_power(tp, false);
  13492. }
  13493. tg3_timer_init(tp);
  13494. err = register_netdev(dev);
  13495. if (err) {
  13496. dev_err(&pdev->dev, "Cannot register net device, aborting\n");
  13497. goto err_out_apeunmap;
  13498. }
  13499. netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
  13500. tp->board_part_number,
  13501. tp->pci_chip_rev_id,
  13502. tg3_bus_string(tp, str),
  13503. dev->dev_addr);
  13504. if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
  13505. struct phy_device *phydev;
  13506. phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
  13507. netdev_info(dev,
  13508. "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
  13509. phydev->drv->name, dev_name(&phydev->dev));
  13510. } else {
  13511. char *ethtype;
  13512. if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
  13513. ethtype = "10/100Base-TX";
  13514. else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
  13515. ethtype = "1000Base-SX";
  13516. else
  13517. ethtype = "10/100/1000Base-T";
  13518. netdev_info(dev, "attached PHY is %s (%s Ethernet) "
  13519. "(WireSpeed[%d], EEE[%d])\n",
  13520. tg3_phy_string(tp), ethtype,
  13521. (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0,
  13522. (tp->phy_flags & TG3_PHYFLG_EEE_CAP) != 0);
  13523. }
  13524. netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
  13525. (dev->features & NETIF_F_RXCSUM) != 0,
  13526. tg3_flag(tp, USE_LINKCHG_REG) != 0,
  13527. (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
  13528. tg3_flag(tp, ENABLE_ASF) != 0,
  13529. tg3_flag(tp, TSO_CAPABLE) != 0);
  13530. netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
  13531. tp->dma_rwctrl,
  13532. pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
  13533. ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
  13534. pci_save_state(pdev);
  13535. return 0;
  13536. err_out_apeunmap:
  13537. if (tp->aperegs) {
  13538. iounmap(tp->aperegs);
  13539. tp->aperegs = NULL;
  13540. }
  13541. err_out_iounmap:
  13542. if (tp->regs) {
  13543. iounmap(tp->regs);
  13544. tp->regs = NULL;
  13545. }
  13546. err_out_free_dev:
  13547. free_netdev(dev);
  13548. err_out_power_down:
  13549. pci_set_power_state(pdev, PCI_D3hot);
  13550. err_out_free_res:
  13551. pci_release_regions(pdev);
  13552. err_out_disable_pdev:
  13553. pci_disable_device(pdev);
  13554. pci_set_drvdata(pdev, NULL);
  13555. return err;
  13556. }
  13557. static void __devexit tg3_remove_one(struct pci_dev *pdev)
  13558. {
  13559. struct net_device *dev = pci_get_drvdata(pdev);
  13560. if (dev) {
  13561. struct tg3 *tp = netdev_priv(dev);
  13562. release_firmware(tp->fw);
  13563. tg3_reset_task_cancel(tp);
  13564. if (tg3_flag(tp, USE_PHYLIB)) {
  13565. tg3_phy_fini(tp);
  13566. tg3_mdio_fini(tp);
  13567. }
  13568. unregister_netdev(dev);
  13569. if (tp->aperegs) {
  13570. iounmap(tp->aperegs);
  13571. tp->aperegs = NULL;
  13572. }
  13573. if (tp->regs) {
  13574. iounmap(tp->regs);
  13575. tp->regs = NULL;
  13576. }
  13577. free_netdev(dev);
  13578. pci_release_regions(pdev);
  13579. pci_disable_device(pdev);
  13580. pci_set_drvdata(pdev, NULL);
  13581. }
  13582. }
  13583. #ifdef CONFIG_PM_SLEEP
  13584. static int tg3_suspend(struct device *device)
  13585. {
  13586. struct pci_dev *pdev = to_pci_dev(device);
  13587. struct net_device *dev = pci_get_drvdata(pdev);
  13588. struct tg3 *tp = netdev_priv(dev);
  13589. int err;
  13590. if (!netif_running(dev))
  13591. return 0;
  13592. tg3_reset_task_cancel(tp);
  13593. tg3_phy_stop(tp);
  13594. tg3_netif_stop(tp);
  13595. tg3_timer_stop(tp);
  13596. tg3_full_lock(tp, 1);
  13597. tg3_disable_ints(tp);
  13598. tg3_full_unlock(tp);
  13599. netif_device_detach(dev);
  13600. tg3_full_lock(tp, 0);
  13601. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  13602. tg3_flag_clear(tp, INIT_COMPLETE);
  13603. tg3_full_unlock(tp);
  13604. err = tg3_power_down_prepare(tp);
  13605. if (err) {
  13606. int err2;
  13607. tg3_full_lock(tp, 0);
  13608. tg3_flag_set(tp, INIT_COMPLETE);
  13609. err2 = tg3_restart_hw(tp, 1);
  13610. if (err2)
  13611. goto out;
  13612. tg3_timer_start(tp);
  13613. netif_device_attach(dev);
  13614. tg3_netif_start(tp);
  13615. out:
  13616. tg3_full_unlock(tp);
  13617. if (!err2)
  13618. tg3_phy_start(tp);
  13619. }
  13620. return err;
  13621. }
  13622. static int tg3_resume(struct device *device)
  13623. {
  13624. struct pci_dev *pdev = to_pci_dev(device);
  13625. struct net_device *dev = pci_get_drvdata(pdev);
  13626. struct tg3 *tp = netdev_priv(dev);
  13627. int err;
  13628. if (!netif_running(dev))
  13629. return 0;
  13630. netif_device_attach(dev);
  13631. tg3_full_lock(tp, 0);
  13632. tg3_flag_set(tp, INIT_COMPLETE);
  13633. err = tg3_restart_hw(tp, 1);
  13634. if (err)
  13635. goto out;
  13636. tg3_timer_start(tp);
  13637. tg3_netif_start(tp);
  13638. out:
  13639. tg3_full_unlock(tp);
  13640. if (!err)
  13641. tg3_phy_start(tp);
  13642. return err;
  13643. }
  13644. static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
  13645. #define TG3_PM_OPS (&tg3_pm_ops)
  13646. #else
  13647. #define TG3_PM_OPS NULL
  13648. #endif /* CONFIG_PM_SLEEP */
  13649. /**
  13650. * tg3_io_error_detected - called when PCI error is detected
  13651. * @pdev: Pointer to PCI device
  13652. * @state: The current pci connection state
  13653. *
  13654. * This function is called after a PCI bus error affecting
  13655. * this device has been detected.
  13656. */
  13657. static pci_ers_result_t tg3_io_error_detected(struct pci_dev *pdev,
  13658. pci_channel_state_t state)
  13659. {
  13660. struct net_device *netdev = pci_get_drvdata(pdev);
  13661. struct tg3 *tp = netdev_priv(netdev);
  13662. pci_ers_result_t err = PCI_ERS_RESULT_NEED_RESET;
  13663. netdev_info(netdev, "PCI I/O error detected\n");
  13664. rtnl_lock();
  13665. if (!netif_running(netdev))
  13666. goto done;
  13667. tg3_phy_stop(tp);
  13668. tg3_netif_stop(tp);
  13669. tg3_timer_stop(tp);
  13670. /* Want to make sure that the reset task doesn't run */
  13671. tg3_reset_task_cancel(tp);
  13672. netif_device_detach(netdev);
  13673. /* Clean up software state, even if MMIO is blocked */
  13674. tg3_full_lock(tp, 0);
  13675. tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
  13676. tg3_full_unlock(tp);
  13677. done:
  13678. if (state == pci_channel_io_perm_failure)
  13679. err = PCI_ERS_RESULT_DISCONNECT;
  13680. else
  13681. pci_disable_device(pdev);
  13682. rtnl_unlock();
  13683. return err;
  13684. }
  13685. /**
  13686. * tg3_io_slot_reset - called after the pci bus has been reset.
  13687. * @pdev: Pointer to PCI device
  13688. *
  13689. * Restart the card from scratch, as if from a cold-boot.
  13690. * At this point, the card has exprienced a hard reset,
  13691. * followed by fixups by BIOS, and has its config space
  13692. * set up identically to what it was at cold boot.
  13693. */
  13694. static pci_ers_result_t tg3_io_slot_reset(struct pci_dev *pdev)
  13695. {
  13696. struct net_device *netdev = pci_get_drvdata(pdev);
  13697. struct tg3 *tp = netdev_priv(netdev);
  13698. pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
  13699. int err;
  13700. rtnl_lock();
  13701. if (pci_enable_device(pdev)) {
  13702. netdev_err(netdev, "Cannot re-enable PCI device after reset.\n");
  13703. goto done;
  13704. }
  13705. pci_set_master(pdev);
  13706. pci_restore_state(pdev);
  13707. pci_save_state(pdev);
  13708. if (!netif_running(netdev)) {
  13709. rc = PCI_ERS_RESULT_RECOVERED;
  13710. goto done;
  13711. }
  13712. err = tg3_power_up(tp);
  13713. if (err)
  13714. goto done;
  13715. rc = PCI_ERS_RESULT_RECOVERED;
  13716. done:
  13717. rtnl_unlock();
  13718. return rc;
  13719. }
  13720. /**
  13721. * tg3_io_resume - called when traffic can start flowing again.
  13722. * @pdev: Pointer to PCI device
  13723. *
  13724. * This callback is called when the error recovery driver tells
  13725. * us that its OK to resume normal operation.
  13726. */
  13727. static void tg3_io_resume(struct pci_dev *pdev)
  13728. {
  13729. struct net_device *netdev = pci_get_drvdata(pdev);
  13730. struct tg3 *tp = netdev_priv(netdev);
  13731. int err;
  13732. rtnl_lock();
  13733. if (!netif_running(netdev))
  13734. goto done;
  13735. tg3_full_lock(tp, 0);
  13736. tg3_flag_set(tp, INIT_COMPLETE);
  13737. err = tg3_restart_hw(tp, 1);
  13738. tg3_full_unlock(tp);
  13739. if (err) {
  13740. netdev_err(netdev, "Cannot restart hardware after reset.\n");
  13741. goto done;
  13742. }
  13743. netif_device_attach(netdev);
  13744. tg3_timer_start(tp);
  13745. tg3_netif_start(tp);
  13746. tg3_phy_start(tp);
  13747. done:
  13748. rtnl_unlock();
  13749. }
  13750. static const struct pci_error_handlers tg3_err_handler = {
  13751. .error_detected = tg3_io_error_detected,
  13752. .slot_reset = tg3_io_slot_reset,
  13753. .resume = tg3_io_resume
  13754. };
  13755. static struct pci_driver tg3_driver = {
  13756. .name = DRV_MODULE_NAME,
  13757. .id_table = tg3_pci_tbl,
  13758. .probe = tg3_init_one,
  13759. .remove = __devexit_p(tg3_remove_one),
  13760. .err_handler = &tg3_err_handler,
  13761. .driver.pm = TG3_PM_OPS,
  13762. };
  13763. static int __init tg3_init(void)
  13764. {
  13765. return pci_register_driver(&tg3_driver);
  13766. }
  13767. static void __exit tg3_cleanup(void)
  13768. {
  13769. pci_unregister_driver(&tg3_driver);
  13770. }
  13771. module_init(tg3_init);
  13772. module_exit(tg3_cleanup);