tg3.c 469 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389
  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-2016 Broadcom Corporation.
  8. * Copyright (C) 2016-2017 Broadcom Limited.
  9. * Copyright (C) 2018 Broadcom. All Rights Reserved. The term "Broadcom"
  10. * refers to Broadcom Inc. and/or its subsidiaries.
  11. *
  12. * Firmware is:
  13. * Derived from proprietary unpublished source code,
  14. * Copyright (C) 2000-2016 Broadcom Corporation.
  15. * Copyright (C) 2016-2017 Broadcom Ltd.
  16. * Copyright (C) 2018 Broadcom. All Rights Reserved. The term "Broadcom"
  17. * refers to Broadcom Inc. and/or its subsidiaries.
  18. *
  19. * Permission is hereby granted for the distribution of this firmware
  20. * data in hexadecimal or equivalent format, provided this copyright
  21. * notice is accompanying it.
  22. */
  23. #include <linux/module.h>
  24. #include <linux/moduleparam.h>
  25. #include <linux/stringify.h>
  26. #include <linux/kernel.h>
  27. #include <linux/sched/signal.h>
  28. #include <linux/types.h>
  29. #include <linux/compiler.h>
  30. #include <linux/slab.h>
  31. #include <linux/delay.h>
  32. #include <linux/in.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/ioport.h>
  35. #include <linux/pci.h>
  36. #include <linux/netdevice.h>
  37. #include <linux/etherdevice.h>
  38. #include <linux/skbuff.h>
  39. #include <linux/ethtool.h>
  40. #include <linux/mdio.h>
  41. #include <linux/mii.h>
  42. #include <linux/phy.h>
  43. #include <linux/brcmphy.h>
  44. #include <linux/if.h>
  45. #include <linux/if_vlan.h>
  46. #include <linux/ip.h>
  47. #include <linux/tcp.h>
  48. #include <linux/workqueue.h>
  49. #include <linux/prefetch.h>
  50. #include <linux/dma-mapping.h>
  51. #include <linux/firmware.h>
  52. #include <linux/ssb/ssb_driver_gige.h>
  53. #include <linux/hwmon.h>
  54. #include <linux/hwmon-sysfs.h>
  55. #include <linux/crc32poly.h>
  56. #include <net/checksum.h>
  57. #include <net/ip.h>
  58. #include <linux/io.h>
  59. #include <asm/byteorder.h>
  60. #include <linux/uaccess.h>
  61. #include <uapi/linux/net_tstamp.h>
  62. #include <linux/ptp_clock_kernel.h>
  63. #ifdef CONFIG_SPARC
  64. #include <asm/idprom.h>
  65. #include <asm/prom.h>
  66. #endif
  67. #define BAR_0 0
  68. #define BAR_2 2
  69. #include "tg3.h"
  70. /* Functions & macros to verify TG3_FLAGS types */
  71. static inline int _tg3_flag(enum TG3_FLAGS flag, unsigned long *bits)
  72. {
  73. return test_bit(flag, bits);
  74. }
  75. static inline void _tg3_flag_set(enum TG3_FLAGS flag, unsigned long *bits)
  76. {
  77. set_bit(flag, bits);
  78. }
  79. static inline void _tg3_flag_clear(enum TG3_FLAGS flag, unsigned long *bits)
  80. {
  81. clear_bit(flag, bits);
  82. }
  83. #define tg3_flag(tp, flag) \
  84. _tg3_flag(TG3_FLAG_##flag, (tp)->tg3_flags)
  85. #define tg3_flag_set(tp, flag) \
  86. _tg3_flag_set(TG3_FLAG_##flag, (tp)->tg3_flags)
  87. #define tg3_flag_clear(tp, flag) \
  88. _tg3_flag_clear(TG3_FLAG_##flag, (tp)->tg3_flags)
  89. #define DRV_MODULE_NAME "tg3"
  90. #define TG3_MAJ_NUM 3
  91. #define TG3_MIN_NUM 137
  92. #define DRV_MODULE_VERSION \
  93. __stringify(TG3_MAJ_NUM) "." __stringify(TG3_MIN_NUM)
  94. #define DRV_MODULE_RELDATE "May 11, 2014"
  95. #define RESET_KIND_SHUTDOWN 0
  96. #define RESET_KIND_INIT 1
  97. #define RESET_KIND_SUSPEND 2
  98. #define TG3_DEF_RX_MODE 0
  99. #define TG3_DEF_TX_MODE 0
  100. #define TG3_DEF_MSG_ENABLE \
  101. (NETIF_MSG_DRV | \
  102. NETIF_MSG_PROBE | \
  103. NETIF_MSG_LINK | \
  104. NETIF_MSG_TIMER | \
  105. NETIF_MSG_IFDOWN | \
  106. NETIF_MSG_IFUP | \
  107. NETIF_MSG_RX_ERR | \
  108. NETIF_MSG_TX_ERR)
  109. #define TG3_GRC_LCLCTL_PWRSW_DELAY 100
  110. /* length of time before we decide the hardware is borked,
  111. * and dev->tx_timeout() should be called to fix the problem
  112. */
  113. #define TG3_TX_TIMEOUT (5 * HZ)
  114. /* hardware minimum and maximum for a single frame's data payload */
  115. #define TG3_MIN_MTU ETH_ZLEN
  116. #define TG3_MAX_MTU(tp) \
  117. (tg3_flag(tp, JUMBO_CAPABLE) ? 9000 : 1500)
  118. /* These numbers seem to be hard coded in the NIC firmware somehow.
  119. * You can't change the ring sizes, but you can change where you place
  120. * them in the NIC onboard memory.
  121. */
  122. #define TG3_RX_STD_RING_SIZE(tp) \
  123. (tg3_flag(tp, LRG_PROD_RING_CAP) ? \
  124. TG3_RX_STD_MAX_SIZE_5717 : TG3_RX_STD_MAX_SIZE_5700)
  125. #define TG3_DEF_RX_RING_PENDING 200
  126. #define TG3_RX_JMB_RING_SIZE(tp) \
  127. (tg3_flag(tp, LRG_PROD_RING_CAP) ? \
  128. TG3_RX_JMB_MAX_SIZE_5717 : TG3_RX_JMB_MAX_SIZE_5700)
  129. #define TG3_DEF_RX_JUMBO_RING_PENDING 100
  130. /* Do not place this n-ring entries value into the tp struct itself,
  131. * we really want to expose these constants to GCC so that modulo et
  132. * al. operations are done with shifts and masks instead of with
  133. * hw multiply/modulo instructions. Another solution would be to
  134. * replace things like '% foo' with '& (foo - 1)'.
  135. */
  136. #define TG3_TX_RING_SIZE 512
  137. #define TG3_DEF_TX_RING_PENDING (TG3_TX_RING_SIZE - 1)
  138. #define TG3_RX_STD_RING_BYTES(tp) \
  139. (sizeof(struct tg3_rx_buffer_desc) * TG3_RX_STD_RING_SIZE(tp))
  140. #define TG3_RX_JMB_RING_BYTES(tp) \
  141. (sizeof(struct tg3_ext_rx_buffer_desc) * TG3_RX_JMB_RING_SIZE(tp))
  142. #define TG3_RX_RCB_RING_BYTES(tp) \
  143. (sizeof(struct tg3_rx_buffer_desc) * (tp->rx_ret_ring_mask + 1))
  144. #define TG3_TX_RING_BYTES (sizeof(struct tg3_tx_buffer_desc) * \
  145. TG3_TX_RING_SIZE)
  146. #define NEXT_TX(N) (((N) + 1) & (TG3_TX_RING_SIZE - 1))
  147. #define TG3_DMA_BYTE_ENAB 64
  148. #define TG3_RX_STD_DMA_SZ 1536
  149. #define TG3_RX_JMB_DMA_SZ 9046
  150. #define TG3_RX_DMA_TO_MAP_SZ(x) ((x) + TG3_DMA_BYTE_ENAB)
  151. #define TG3_RX_STD_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_STD_DMA_SZ)
  152. #define TG3_RX_JMB_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_JMB_DMA_SZ)
  153. #define TG3_RX_STD_BUFF_RING_SIZE(tp) \
  154. (sizeof(struct ring_info) * TG3_RX_STD_RING_SIZE(tp))
  155. #define TG3_RX_JMB_BUFF_RING_SIZE(tp) \
  156. (sizeof(struct ring_info) * TG3_RX_JMB_RING_SIZE(tp))
  157. /* Due to a hardware bug, the 5701 can only DMA to memory addresses
  158. * that are at least dword aligned when used in PCIX mode. The driver
  159. * works around this bug by double copying the packet. This workaround
  160. * is built into the normal double copy length check for efficiency.
  161. *
  162. * However, the double copy is only necessary on those architectures
  163. * where unaligned memory accesses are inefficient. For those architectures
  164. * where unaligned memory accesses incur little penalty, we can reintegrate
  165. * the 5701 in the normal rx path. Doing so saves a device structure
  166. * dereference by hardcoding the double copy threshold in place.
  167. */
  168. #define TG3_RX_COPY_THRESHOLD 256
  169. #if NET_IP_ALIGN == 0 || defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
  170. #define TG3_RX_COPY_THRESH(tp) TG3_RX_COPY_THRESHOLD
  171. #else
  172. #define TG3_RX_COPY_THRESH(tp) ((tp)->rx_copy_thresh)
  173. #endif
  174. #if (NET_IP_ALIGN != 0)
  175. #define TG3_RX_OFFSET(tp) ((tp)->rx_offset)
  176. #else
  177. #define TG3_RX_OFFSET(tp) (NET_SKB_PAD)
  178. #endif
  179. /* minimum number of free TX descriptors required to wake up TX process */
  180. #define TG3_TX_WAKEUP_THRESH(tnapi) ((tnapi)->tx_pending / 4)
  181. #define TG3_TX_BD_DMA_MAX_2K 2048
  182. #define TG3_TX_BD_DMA_MAX_4K 4096
  183. #define TG3_RAW_IP_ALIGN 2
  184. #define TG3_MAX_UCAST_ADDR(tp) (tg3_flag((tp), ENABLE_ASF) ? 2 : 3)
  185. #define TG3_UCAST_ADDR_IDX(tp) (tg3_flag((tp), ENABLE_ASF) ? 2 : 1)
  186. #define TG3_FW_UPDATE_TIMEOUT_SEC 5
  187. #define TG3_FW_UPDATE_FREQ_SEC (TG3_FW_UPDATE_TIMEOUT_SEC / 2)
  188. #define FIRMWARE_TG3 "tigon/tg3.bin"
  189. #define FIRMWARE_TG357766 "tigon/tg357766.bin"
  190. #define FIRMWARE_TG3TSO "tigon/tg3_tso.bin"
  191. #define FIRMWARE_TG3TSO5 "tigon/tg3_tso5.bin"
  192. static char version[] =
  193. DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")";
  194. MODULE_AUTHOR("David S. Miller (davem@redhat.com) and Jeff Garzik (jgarzik@pobox.com)");
  195. MODULE_DESCRIPTION("Broadcom Tigon3 ethernet driver");
  196. MODULE_LICENSE("GPL");
  197. MODULE_VERSION(DRV_MODULE_VERSION);
  198. MODULE_FIRMWARE(FIRMWARE_TG3);
  199. MODULE_FIRMWARE(FIRMWARE_TG3TSO);
  200. MODULE_FIRMWARE(FIRMWARE_TG3TSO5);
  201. static int tg3_debug = -1; /* -1 == use TG3_DEF_MSG_ENABLE as value */
  202. module_param(tg3_debug, int, 0);
  203. MODULE_PARM_DESC(tg3_debug, "Tigon3 bitmapped debugging message enable value");
  204. #define TG3_DRV_DATA_FLAG_10_100_ONLY 0x0001
  205. #define TG3_DRV_DATA_FLAG_5705_10_100 0x0002
  206. static const struct pci_device_id tg3_pci_tbl[] = {
  207. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5700)},
  208. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5701)},
  209. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702)},
  210. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703)},
  211. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704)},
  212. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702FE)},
  213. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705)},
  214. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705_2)},
  215. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M)},
  216. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M_2)},
  217. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702X)},
  218. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703X)},
  219. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S)},
  220. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702A3)},
  221. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703A3)},
  222. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5782)},
  223. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5788)},
  224. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5789)},
  225. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901),
  226. .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY |
  227. TG3_DRV_DATA_FLAG_5705_10_100},
  228. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901_2),
  229. .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY |
  230. TG3_DRV_DATA_FLAG_5705_10_100},
  231. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S_2)},
  232. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705F),
  233. .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY |
  234. TG3_DRV_DATA_FLAG_5705_10_100},
  235. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5721)},
  236. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5722)},
  237. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5750)},
  238. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751)},
  239. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751M)},
  240. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751F),
  241. .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
  242. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752)},
  243. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752M)},
  244. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753)},
  245. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753M)},
  246. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753F),
  247. .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
  248. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754)},
  249. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754M)},
  250. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755)},
  251. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755M)},
  252. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5756)},
  253. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5786)},
  254. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787)},
  255. {PCI_DEVICE_SUB(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5787M,
  256. PCI_VENDOR_ID_LENOVO,
  257. TG3PCI_SUBDEVICE_ID_LENOVO_5787M),
  258. .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
  259. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787M)},
  260. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787F),
  261. .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
  262. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714)},
  263. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714S)},
  264. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715)},
  265. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715S)},
  266. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780)},
  267. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780S)},
  268. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5781)},
  269. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906)},
  270. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906M)},
  271. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5784)},
  272. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5764)},
  273. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5723)},
  274. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761)},
  275. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761E)},
  276. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761S)},
  277. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761SE)},
  278. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_G)},
  279. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_F)},
  280. {PCI_DEVICE_SUB(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780,
  281. PCI_VENDOR_ID_AI, TG3PCI_SUBDEVICE_ID_ACER_57780_A),
  282. .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
  283. {PCI_DEVICE_SUB(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780,
  284. PCI_VENDOR_ID_AI, TG3PCI_SUBDEVICE_ID_ACER_57780_B),
  285. .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
  286. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780)},
  287. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57760)},
  288. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57790),
  289. .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
  290. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57788)},
  291. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717)},
  292. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717_C)},
  293. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5718)},
  294. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57781)},
  295. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57785)},
  296. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57761)},
  297. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57765)},
  298. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57791),
  299. .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
  300. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57795),
  301. .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
  302. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5719)},
  303. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5720)},
  304. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57762)},
  305. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57766)},
  306. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5762)},
  307. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5725)},
  308. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5727)},
  309. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57764)},
  310. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57767)},
  311. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57787)},
  312. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57782)},
  313. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57786)},
  314. {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9DXX)},
  315. {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9MXX)},
  316. {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1000)},
  317. {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1001)},
  318. {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1003)},
  319. {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC9100)},
  320. {PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_TIGON3)},
  321. {PCI_DEVICE(0x10cf, 0x11a2)}, /* Fujitsu 1000base-SX with BCM5703SKHB */
  322. {}
  323. };
  324. MODULE_DEVICE_TABLE(pci, tg3_pci_tbl);
  325. static const struct {
  326. const char string[ETH_GSTRING_LEN];
  327. } ethtool_stats_keys[] = {
  328. { "rx_octets" },
  329. { "rx_fragments" },
  330. { "rx_ucast_packets" },
  331. { "rx_mcast_packets" },
  332. { "rx_bcast_packets" },
  333. { "rx_fcs_errors" },
  334. { "rx_align_errors" },
  335. { "rx_xon_pause_rcvd" },
  336. { "rx_xoff_pause_rcvd" },
  337. { "rx_mac_ctrl_rcvd" },
  338. { "rx_xoff_entered" },
  339. { "rx_frame_too_long_errors" },
  340. { "rx_jabbers" },
  341. { "rx_undersize_packets" },
  342. { "rx_in_length_errors" },
  343. { "rx_out_length_errors" },
  344. { "rx_64_or_less_octet_packets" },
  345. { "rx_65_to_127_octet_packets" },
  346. { "rx_128_to_255_octet_packets" },
  347. { "rx_256_to_511_octet_packets" },
  348. { "rx_512_to_1023_octet_packets" },
  349. { "rx_1024_to_1522_octet_packets" },
  350. { "rx_1523_to_2047_octet_packets" },
  351. { "rx_2048_to_4095_octet_packets" },
  352. { "rx_4096_to_8191_octet_packets" },
  353. { "rx_8192_to_9022_octet_packets" },
  354. { "tx_octets" },
  355. { "tx_collisions" },
  356. { "tx_xon_sent" },
  357. { "tx_xoff_sent" },
  358. { "tx_flow_control" },
  359. { "tx_mac_errors" },
  360. { "tx_single_collisions" },
  361. { "tx_mult_collisions" },
  362. { "tx_deferred" },
  363. { "tx_excessive_collisions" },
  364. { "tx_late_collisions" },
  365. { "tx_collide_2times" },
  366. { "tx_collide_3times" },
  367. { "tx_collide_4times" },
  368. { "tx_collide_5times" },
  369. { "tx_collide_6times" },
  370. { "tx_collide_7times" },
  371. { "tx_collide_8times" },
  372. { "tx_collide_9times" },
  373. { "tx_collide_10times" },
  374. { "tx_collide_11times" },
  375. { "tx_collide_12times" },
  376. { "tx_collide_13times" },
  377. { "tx_collide_14times" },
  378. { "tx_collide_15times" },
  379. { "tx_ucast_packets" },
  380. { "tx_mcast_packets" },
  381. { "tx_bcast_packets" },
  382. { "tx_carrier_sense_errors" },
  383. { "tx_discards" },
  384. { "tx_errors" },
  385. { "dma_writeq_full" },
  386. { "dma_write_prioq_full" },
  387. { "rxbds_empty" },
  388. { "rx_discards" },
  389. { "rx_errors" },
  390. { "rx_threshold_hit" },
  391. { "dma_readq_full" },
  392. { "dma_read_prioq_full" },
  393. { "tx_comp_queue_full" },
  394. { "ring_set_send_prod_index" },
  395. { "ring_status_update" },
  396. { "nic_irqs" },
  397. { "nic_avoided_irqs" },
  398. { "nic_tx_threshold_hit" },
  399. { "mbuf_lwm_thresh_hit" },
  400. };
  401. #define TG3_NUM_STATS ARRAY_SIZE(ethtool_stats_keys)
  402. #define TG3_NVRAM_TEST 0
  403. #define TG3_LINK_TEST 1
  404. #define TG3_REGISTER_TEST 2
  405. #define TG3_MEMORY_TEST 3
  406. #define TG3_MAC_LOOPB_TEST 4
  407. #define TG3_PHY_LOOPB_TEST 5
  408. #define TG3_EXT_LOOPB_TEST 6
  409. #define TG3_INTERRUPT_TEST 7
  410. static const struct {
  411. const char string[ETH_GSTRING_LEN];
  412. } ethtool_test_keys[] = {
  413. [TG3_NVRAM_TEST] = { "nvram test (online) " },
  414. [TG3_LINK_TEST] = { "link test (online) " },
  415. [TG3_REGISTER_TEST] = { "register test (offline)" },
  416. [TG3_MEMORY_TEST] = { "memory test (offline)" },
  417. [TG3_MAC_LOOPB_TEST] = { "mac loopback test (offline)" },
  418. [TG3_PHY_LOOPB_TEST] = { "phy loopback test (offline)" },
  419. [TG3_EXT_LOOPB_TEST] = { "ext loopback test (offline)" },
  420. [TG3_INTERRUPT_TEST] = { "interrupt test (offline)" },
  421. };
  422. #define TG3_NUM_TEST ARRAY_SIZE(ethtool_test_keys)
  423. static void tg3_write32(struct tg3 *tp, u32 off, u32 val)
  424. {
  425. writel(val, tp->regs + off);
  426. }
  427. static u32 tg3_read32(struct tg3 *tp, u32 off)
  428. {
  429. return readl(tp->regs + off);
  430. }
  431. static void tg3_ape_write32(struct tg3 *tp, u32 off, u32 val)
  432. {
  433. writel(val, tp->aperegs + off);
  434. }
  435. static u32 tg3_ape_read32(struct tg3 *tp, u32 off)
  436. {
  437. return readl(tp->aperegs + off);
  438. }
  439. static void tg3_write_indirect_reg32(struct tg3 *tp, u32 off, u32 val)
  440. {
  441. unsigned long flags;
  442. spin_lock_irqsave(&tp->indirect_lock, flags);
  443. pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
  444. pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
  445. spin_unlock_irqrestore(&tp->indirect_lock, flags);
  446. }
  447. static void tg3_write_flush_reg32(struct tg3 *tp, u32 off, u32 val)
  448. {
  449. writel(val, tp->regs + off);
  450. readl(tp->regs + off);
  451. }
  452. static u32 tg3_read_indirect_reg32(struct tg3 *tp, u32 off)
  453. {
  454. unsigned long flags;
  455. u32 val;
  456. spin_lock_irqsave(&tp->indirect_lock, flags);
  457. pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
  458. pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
  459. spin_unlock_irqrestore(&tp->indirect_lock, flags);
  460. return val;
  461. }
  462. static void tg3_write_indirect_mbox(struct tg3 *tp, u32 off, u32 val)
  463. {
  464. unsigned long flags;
  465. if (off == (MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW)) {
  466. pci_write_config_dword(tp->pdev, TG3PCI_RCV_RET_RING_CON_IDX +
  467. TG3_64BIT_REG_LOW, val);
  468. return;
  469. }
  470. if (off == TG3_RX_STD_PROD_IDX_REG) {
  471. pci_write_config_dword(tp->pdev, TG3PCI_STD_RING_PROD_IDX +
  472. TG3_64BIT_REG_LOW, val);
  473. return;
  474. }
  475. spin_lock_irqsave(&tp->indirect_lock, flags);
  476. pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
  477. pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
  478. spin_unlock_irqrestore(&tp->indirect_lock, flags);
  479. /* In indirect mode when disabling interrupts, we also need
  480. * to clear the interrupt bit in the GRC local ctrl register.
  481. */
  482. if ((off == (MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW)) &&
  483. (val == 0x1)) {
  484. pci_write_config_dword(tp->pdev, TG3PCI_MISC_LOCAL_CTRL,
  485. tp->grc_local_ctrl|GRC_LCLCTRL_CLEARINT);
  486. }
  487. }
  488. static u32 tg3_read_indirect_mbox(struct tg3 *tp, u32 off)
  489. {
  490. unsigned long flags;
  491. u32 val;
  492. spin_lock_irqsave(&tp->indirect_lock, flags);
  493. pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
  494. pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
  495. spin_unlock_irqrestore(&tp->indirect_lock, flags);
  496. return val;
  497. }
  498. /* usec_wait specifies the wait time in usec when writing to certain registers
  499. * where it is unsafe to read back the register without some delay.
  500. * GRC_LOCAL_CTRL is one example if the GPIOs are toggled to switch power.
  501. * TG3PCI_CLOCK_CTRL is another example if the clock frequencies are changed.
  502. */
  503. static void _tw32_flush(struct tg3 *tp, u32 off, u32 val, u32 usec_wait)
  504. {
  505. if (tg3_flag(tp, PCIX_TARGET_HWBUG) || tg3_flag(tp, ICH_WORKAROUND))
  506. /* Non-posted methods */
  507. tp->write32(tp, off, val);
  508. else {
  509. /* Posted method */
  510. tg3_write32(tp, off, val);
  511. if (usec_wait)
  512. udelay(usec_wait);
  513. tp->read32(tp, off);
  514. }
  515. /* Wait again after the read for the posted method to guarantee that
  516. * the wait time is met.
  517. */
  518. if (usec_wait)
  519. udelay(usec_wait);
  520. }
  521. static inline void tw32_mailbox_flush(struct tg3 *tp, u32 off, u32 val)
  522. {
  523. tp->write32_mbox(tp, off, val);
  524. if (tg3_flag(tp, FLUSH_POSTED_WRITES) ||
  525. (!tg3_flag(tp, MBOX_WRITE_REORDER) &&
  526. !tg3_flag(tp, ICH_WORKAROUND)))
  527. tp->read32_mbox(tp, off);
  528. }
  529. static void tg3_write32_tx_mbox(struct tg3 *tp, u32 off, u32 val)
  530. {
  531. void __iomem *mbox = tp->regs + off;
  532. writel(val, mbox);
  533. if (tg3_flag(tp, TXD_MBOX_HWBUG))
  534. writel(val, mbox);
  535. if (tg3_flag(tp, MBOX_WRITE_REORDER) ||
  536. tg3_flag(tp, FLUSH_POSTED_WRITES))
  537. readl(mbox);
  538. }
  539. static u32 tg3_read32_mbox_5906(struct tg3 *tp, u32 off)
  540. {
  541. return readl(tp->regs + off + GRCMBOX_BASE);
  542. }
  543. static void tg3_write32_mbox_5906(struct tg3 *tp, u32 off, u32 val)
  544. {
  545. writel(val, tp->regs + off + GRCMBOX_BASE);
  546. }
  547. #define tw32_mailbox(reg, val) tp->write32_mbox(tp, reg, val)
  548. #define tw32_mailbox_f(reg, val) tw32_mailbox_flush(tp, (reg), (val))
  549. #define tw32_rx_mbox(reg, val) tp->write32_rx_mbox(tp, reg, val)
  550. #define tw32_tx_mbox(reg, val) tp->write32_tx_mbox(tp, reg, val)
  551. #define tr32_mailbox(reg) tp->read32_mbox(tp, reg)
  552. #define tw32(reg, val) tp->write32(tp, reg, val)
  553. #define tw32_f(reg, val) _tw32_flush(tp, (reg), (val), 0)
  554. #define tw32_wait_f(reg, val, us) _tw32_flush(tp, (reg), (val), (us))
  555. #define tr32(reg) tp->read32(tp, reg)
  556. static void tg3_write_mem(struct tg3 *tp, u32 off, u32 val)
  557. {
  558. unsigned long flags;
  559. if (tg3_asic_rev(tp) == ASIC_REV_5906 &&
  560. (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC))
  561. return;
  562. spin_lock_irqsave(&tp->indirect_lock, flags);
  563. if (tg3_flag(tp, SRAM_USE_CONFIG)) {
  564. pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
  565. pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
  566. /* Always leave this as zero. */
  567. pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
  568. } else {
  569. tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
  570. tw32_f(TG3PCI_MEM_WIN_DATA, val);
  571. /* Always leave this as zero. */
  572. tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
  573. }
  574. spin_unlock_irqrestore(&tp->indirect_lock, flags);
  575. }
  576. static void tg3_read_mem(struct tg3 *tp, u32 off, u32 *val)
  577. {
  578. unsigned long flags;
  579. if (tg3_asic_rev(tp) == ASIC_REV_5906 &&
  580. (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC)) {
  581. *val = 0;
  582. return;
  583. }
  584. spin_lock_irqsave(&tp->indirect_lock, flags);
  585. if (tg3_flag(tp, SRAM_USE_CONFIG)) {
  586. pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
  587. pci_read_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
  588. /* Always leave this as zero. */
  589. pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
  590. } else {
  591. tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
  592. *val = tr32(TG3PCI_MEM_WIN_DATA);
  593. /* Always leave this as zero. */
  594. tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
  595. }
  596. spin_unlock_irqrestore(&tp->indirect_lock, flags);
  597. }
  598. static void tg3_ape_lock_init(struct tg3 *tp)
  599. {
  600. int i;
  601. u32 regbase, bit;
  602. if (tg3_asic_rev(tp) == ASIC_REV_5761)
  603. regbase = TG3_APE_LOCK_GRANT;
  604. else
  605. regbase = TG3_APE_PER_LOCK_GRANT;
  606. /* Make sure the driver hasn't any stale locks. */
  607. for (i = TG3_APE_LOCK_PHY0; i <= TG3_APE_LOCK_GPIO; i++) {
  608. switch (i) {
  609. case TG3_APE_LOCK_PHY0:
  610. case TG3_APE_LOCK_PHY1:
  611. case TG3_APE_LOCK_PHY2:
  612. case TG3_APE_LOCK_PHY3:
  613. bit = APE_LOCK_GRANT_DRIVER;
  614. break;
  615. default:
  616. if (!tp->pci_fn)
  617. bit = APE_LOCK_GRANT_DRIVER;
  618. else
  619. bit = 1 << tp->pci_fn;
  620. }
  621. tg3_ape_write32(tp, regbase + 4 * i, bit);
  622. }
  623. }
  624. static int tg3_ape_lock(struct tg3 *tp, int locknum)
  625. {
  626. int i, off;
  627. int ret = 0;
  628. u32 status, req, gnt, bit;
  629. if (!tg3_flag(tp, ENABLE_APE))
  630. return 0;
  631. switch (locknum) {
  632. case TG3_APE_LOCK_GPIO:
  633. if (tg3_asic_rev(tp) == ASIC_REV_5761)
  634. return 0;
  635. /* else: fall through */
  636. case TG3_APE_LOCK_GRC:
  637. case TG3_APE_LOCK_MEM:
  638. if (!tp->pci_fn)
  639. bit = APE_LOCK_REQ_DRIVER;
  640. else
  641. bit = 1 << tp->pci_fn;
  642. break;
  643. case TG3_APE_LOCK_PHY0:
  644. case TG3_APE_LOCK_PHY1:
  645. case TG3_APE_LOCK_PHY2:
  646. case TG3_APE_LOCK_PHY3:
  647. bit = APE_LOCK_REQ_DRIVER;
  648. break;
  649. default:
  650. return -EINVAL;
  651. }
  652. if (tg3_asic_rev(tp) == ASIC_REV_5761) {
  653. req = TG3_APE_LOCK_REQ;
  654. gnt = TG3_APE_LOCK_GRANT;
  655. } else {
  656. req = TG3_APE_PER_LOCK_REQ;
  657. gnt = TG3_APE_PER_LOCK_GRANT;
  658. }
  659. off = 4 * locknum;
  660. tg3_ape_write32(tp, req + off, bit);
  661. /* Wait for up to 1 millisecond to acquire lock. */
  662. for (i = 0; i < 100; i++) {
  663. status = tg3_ape_read32(tp, gnt + off);
  664. if (status == bit)
  665. break;
  666. if (pci_channel_offline(tp->pdev))
  667. break;
  668. udelay(10);
  669. }
  670. if (status != bit) {
  671. /* Revoke the lock request. */
  672. tg3_ape_write32(tp, gnt + off, bit);
  673. ret = -EBUSY;
  674. }
  675. return ret;
  676. }
  677. static void tg3_ape_unlock(struct tg3 *tp, int locknum)
  678. {
  679. u32 gnt, bit;
  680. if (!tg3_flag(tp, ENABLE_APE))
  681. return;
  682. switch (locknum) {
  683. case TG3_APE_LOCK_GPIO:
  684. if (tg3_asic_rev(tp) == ASIC_REV_5761)
  685. return;
  686. /* else: fall through */
  687. case TG3_APE_LOCK_GRC:
  688. case TG3_APE_LOCK_MEM:
  689. if (!tp->pci_fn)
  690. bit = APE_LOCK_GRANT_DRIVER;
  691. else
  692. bit = 1 << tp->pci_fn;
  693. break;
  694. case TG3_APE_LOCK_PHY0:
  695. case TG3_APE_LOCK_PHY1:
  696. case TG3_APE_LOCK_PHY2:
  697. case TG3_APE_LOCK_PHY3:
  698. bit = APE_LOCK_GRANT_DRIVER;
  699. break;
  700. default:
  701. return;
  702. }
  703. if (tg3_asic_rev(tp) == ASIC_REV_5761)
  704. gnt = TG3_APE_LOCK_GRANT;
  705. else
  706. gnt = TG3_APE_PER_LOCK_GRANT;
  707. tg3_ape_write32(tp, gnt + 4 * locknum, bit);
  708. }
  709. static int tg3_ape_event_lock(struct tg3 *tp, u32 timeout_us)
  710. {
  711. u32 apedata;
  712. while (timeout_us) {
  713. if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
  714. return -EBUSY;
  715. apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
  716. if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
  717. break;
  718. tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
  719. udelay(10);
  720. timeout_us -= (timeout_us > 10) ? 10 : timeout_us;
  721. }
  722. return timeout_us ? 0 : -EBUSY;
  723. }
  724. #ifdef CONFIG_TIGON3_HWMON
  725. static int tg3_ape_wait_for_event(struct tg3 *tp, u32 timeout_us)
  726. {
  727. u32 i, apedata;
  728. for (i = 0; i < timeout_us / 10; i++) {
  729. apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
  730. if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
  731. break;
  732. udelay(10);
  733. }
  734. return i == timeout_us / 10;
  735. }
  736. static int tg3_ape_scratchpad_read(struct tg3 *tp, u32 *data, u32 base_off,
  737. u32 len)
  738. {
  739. int err;
  740. u32 i, bufoff, msgoff, maxlen, apedata;
  741. if (!tg3_flag(tp, APE_HAS_NCSI))
  742. return 0;
  743. apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
  744. if (apedata != APE_SEG_SIG_MAGIC)
  745. return -ENODEV;
  746. apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
  747. if (!(apedata & APE_FW_STATUS_READY))
  748. return -EAGAIN;
  749. bufoff = tg3_ape_read32(tp, TG3_APE_SEG_MSG_BUF_OFF) +
  750. TG3_APE_SHMEM_BASE;
  751. msgoff = bufoff + 2 * sizeof(u32);
  752. maxlen = tg3_ape_read32(tp, TG3_APE_SEG_MSG_BUF_LEN);
  753. while (len) {
  754. u32 length;
  755. /* Cap xfer sizes to scratchpad limits. */
  756. length = (len > maxlen) ? maxlen : len;
  757. len -= length;
  758. apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
  759. if (!(apedata & APE_FW_STATUS_READY))
  760. return -EAGAIN;
  761. /* Wait for up to 1 msec for APE to service previous event. */
  762. err = tg3_ape_event_lock(tp, 1000);
  763. if (err)
  764. return err;
  765. apedata = APE_EVENT_STATUS_DRIVER_EVNT |
  766. APE_EVENT_STATUS_SCRTCHPD_READ |
  767. APE_EVENT_STATUS_EVENT_PENDING;
  768. tg3_ape_write32(tp, TG3_APE_EVENT_STATUS, apedata);
  769. tg3_ape_write32(tp, bufoff, base_off);
  770. tg3_ape_write32(tp, bufoff + sizeof(u32), length);
  771. tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
  772. tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
  773. base_off += length;
  774. if (tg3_ape_wait_for_event(tp, 30000))
  775. return -EAGAIN;
  776. for (i = 0; length; i += 4, length -= 4) {
  777. u32 val = tg3_ape_read32(tp, msgoff + i);
  778. memcpy(data, &val, sizeof(u32));
  779. data++;
  780. }
  781. }
  782. return 0;
  783. }
  784. #endif
  785. static int tg3_ape_send_event(struct tg3 *tp, u32 event)
  786. {
  787. int err;
  788. u32 apedata;
  789. apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
  790. if (apedata != APE_SEG_SIG_MAGIC)
  791. return -EAGAIN;
  792. apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
  793. if (!(apedata & APE_FW_STATUS_READY))
  794. return -EAGAIN;
  795. /* Wait for up to 20 millisecond for APE to service previous event. */
  796. err = tg3_ape_event_lock(tp, 20000);
  797. if (err)
  798. return err;
  799. tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
  800. event | APE_EVENT_STATUS_EVENT_PENDING);
  801. tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
  802. tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
  803. return 0;
  804. }
  805. static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
  806. {
  807. u32 event;
  808. u32 apedata;
  809. if (!tg3_flag(tp, ENABLE_APE))
  810. return;
  811. switch (kind) {
  812. case RESET_KIND_INIT:
  813. tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_COUNT, tp->ape_hb++);
  814. tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
  815. APE_HOST_SEG_SIG_MAGIC);
  816. tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
  817. APE_HOST_SEG_LEN_MAGIC);
  818. apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
  819. tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
  820. tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
  821. APE_HOST_DRIVER_ID_MAGIC(TG3_MAJ_NUM, TG3_MIN_NUM));
  822. tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
  823. APE_HOST_BEHAV_NO_PHYLOCK);
  824. tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE,
  825. TG3_APE_HOST_DRVR_STATE_START);
  826. event = APE_EVENT_STATUS_STATE_START;
  827. break;
  828. case RESET_KIND_SHUTDOWN:
  829. if (device_may_wakeup(&tp->pdev->dev) &&
  830. tg3_flag(tp, WOL_ENABLE)) {
  831. tg3_ape_write32(tp, TG3_APE_HOST_WOL_SPEED,
  832. TG3_APE_HOST_WOL_SPEED_AUTO);
  833. apedata = TG3_APE_HOST_DRVR_STATE_WOL;
  834. } else
  835. apedata = TG3_APE_HOST_DRVR_STATE_UNLOAD;
  836. tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE, apedata);
  837. event = APE_EVENT_STATUS_STATE_UNLOAD;
  838. break;
  839. default:
  840. return;
  841. }
  842. event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
  843. tg3_ape_send_event(tp, event);
  844. }
  845. static void tg3_send_ape_heartbeat(struct tg3 *tp,
  846. unsigned long interval)
  847. {
  848. /* Check if hb interval has exceeded */
  849. if (!tg3_flag(tp, ENABLE_APE) ||
  850. time_before(jiffies, tp->ape_hb_jiffies + interval))
  851. return;
  852. tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_COUNT, tp->ape_hb++);
  853. tp->ape_hb_jiffies = jiffies;
  854. }
  855. static void tg3_disable_ints(struct tg3 *tp)
  856. {
  857. int i;
  858. tw32(TG3PCI_MISC_HOST_CTRL,
  859. (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
  860. for (i = 0; i < tp->irq_max; i++)
  861. tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
  862. }
  863. static void tg3_enable_ints(struct tg3 *tp)
  864. {
  865. int i;
  866. tp->irq_sync = 0;
  867. wmb();
  868. tw32(TG3PCI_MISC_HOST_CTRL,
  869. (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
  870. tp->coal_now = tp->coalesce_mode | HOSTCC_MODE_ENABLE;
  871. for (i = 0; i < tp->irq_cnt; i++) {
  872. struct tg3_napi *tnapi = &tp->napi[i];
  873. tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
  874. if (tg3_flag(tp, 1SHOT_MSI))
  875. tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
  876. tp->coal_now |= tnapi->coal_now;
  877. }
  878. /* Force an initial interrupt */
  879. if (!tg3_flag(tp, TAGGED_STATUS) &&
  880. (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
  881. tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
  882. else
  883. tw32(HOSTCC_MODE, tp->coal_now);
  884. tp->coal_now &= ~(tp->napi[0].coal_now | tp->napi[1].coal_now);
  885. }
  886. static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
  887. {
  888. struct tg3 *tp = tnapi->tp;
  889. struct tg3_hw_status *sblk = tnapi->hw_status;
  890. unsigned int work_exists = 0;
  891. /* check for phy events */
  892. if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
  893. if (sblk->status & SD_STATUS_LINK_CHG)
  894. work_exists = 1;
  895. }
  896. /* check for TX work to do */
  897. if (sblk->idx[0].tx_consumer != tnapi->tx_cons)
  898. work_exists = 1;
  899. /* check for RX work to do */
  900. if (tnapi->rx_rcb_prod_idx &&
  901. *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
  902. work_exists = 1;
  903. return work_exists;
  904. }
  905. /* tg3_int_reenable
  906. * similar to tg3_enable_ints, but it accurately determines whether there
  907. * is new work pending and can return without flushing the PIO write
  908. * which reenables interrupts
  909. */
  910. static void tg3_int_reenable(struct tg3_napi *tnapi)
  911. {
  912. struct tg3 *tp = tnapi->tp;
  913. tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
  914. mmiowb();
  915. /* When doing tagged status, this work check is unnecessary.
  916. * The last_tag we write above tells the chip which piece of
  917. * work we've completed.
  918. */
  919. if (!tg3_flag(tp, TAGGED_STATUS) && tg3_has_work(tnapi))
  920. tw32(HOSTCC_MODE, tp->coalesce_mode |
  921. HOSTCC_MODE_ENABLE | tnapi->coal_now);
  922. }
  923. static void tg3_switch_clocks(struct tg3 *tp)
  924. {
  925. u32 clock_ctrl;
  926. u32 orig_clock_ctrl;
  927. if (tg3_flag(tp, CPMU_PRESENT) || tg3_flag(tp, 5780_CLASS))
  928. return;
  929. clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
  930. orig_clock_ctrl = clock_ctrl;
  931. clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
  932. CLOCK_CTRL_CLKRUN_OENABLE |
  933. 0x1f);
  934. tp->pci_clock_ctrl = clock_ctrl;
  935. if (tg3_flag(tp, 5705_PLUS)) {
  936. if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
  937. tw32_wait_f(TG3PCI_CLOCK_CTRL,
  938. clock_ctrl | CLOCK_CTRL_625_CORE, 40);
  939. }
  940. } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
  941. tw32_wait_f(TG3PCI_CLOCK_CTRL,
  942. clock_ctrl |
  943. (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
  944. 40);
  945. tw32_wait_f(TG3PCI_CLOCK_CTRL,
  946. clock_ctrl | (CLOCK_CTRL_ALTCLK),
  947. 40);
  948. }
  949. tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
  950. }
  951. #define PHY_BUSY_LOOPS 5000
  952. static int __tg3_readphy(struct tg3 *tp, unsigned int phy_addr, int reg,
  953. u32 *val)
  954. {
  955. u32 frame_val;
  956. unsigned int loops;
  957. int ret;
  958. if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
  959. tw32_f(MAC_MI_MODE,
  960. (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
  961. udelay(80);
  962. }
  963. tg3_ape_lock(tp, tp->phy_ape_lock);
  964. *val = 0x0;
  965. frame_val = ((phy_addr << MI_COM_PHY_ADDR_SHIFT) &
  966. MI_COM_PHY_ADDR_MASK);
  967. frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
  968. MI_COM_REG_ADDR_MASK);
  969. frame_val |= (MI_COM_CMD_READ | MI_COM_START);
  970. tw32_f(MAC_MI_COM, frame_val);
  971. loops = PHY_BUSY_LOOPS;
  972. while (loops != 0) {
  973. udelay(10);
  974. frame_val = tr32(MAC_MI_COM);
  975. if ((frame_val & MI_COM_BUSY) == 0) {
  976. udelay(5);
  977. frame_val = tr32(MAC_MI_COM);
  978. break;
  979. }
  980. loops -= 1;
  981. }
  982. ret = -EBUSY;
  983. if (loops != 0) {
  984. *val = frame_val & MI_COM_DATA_MASK;
  985. ret = 0;
  986. }
  987. if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
  988. tw32_f(MAC_MI_MODE, tp->mi_mode);
  989. udelay(80);
  990. }
  991. tg3_ape_unlock(tp, tp->phy_ape_lock);
  992. return ret;
  993. }
  994. static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
  995. {
  996. return __tg3_readphy(tp, tp->phy_addr, reg, val);
  997. }
  998. static int __tg3_writephy(struct tg3 *tp, unsigned int phy_addr, int reg,
  999. u32 val)
  1000. {
  1001. u32 frame_val;
  1002. unsigned int loops;
  1003. int ret;
  1004. if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
  1005. (reg == MII_CTRL1000 || reg == MII_TG3_AUX_CTRL))
  1006. return 0;
  1007. if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
  1008. tw32_f(MAC_MI_MODE,
  1009. (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
  1010. udelay(80);
  1011. }
  1012. tg3_ape_lock(tp, tp->phy_ape_lock);
  1013. frame_val = ((phy_addr << MI_COM_PHY_ADDR_SHIFT) &
  1014. MI_COM_PHY_ADDR_MASK);
  1015. frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
  1016. MI_COM_REG_ADDR_MASK);
  1017. frame_val |= (val & MI_COM_DATA_MASK);
  1018. frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
  1019. tw32_f(MAC_MI_COM, frame_val);
  1020. loops = PHY_BUSY_LOOPS;
  1021. while (loops != 0) {
  1022. udelay(10);
  1023. frame_val = tr32(MAC_MI_COM);
  1024. if ((frame_val & MI_COM_BUSY) == 0) {
  1025. udelay(5);
  1026. frame_val = tr32(MAC_MI_COM);
  1027. break;
  1028. }
  1029. loops -= 1;
  1030. }
  1031. ret = -EBUSY;
  1032. if (loops != 0)
  1033. ret = 0;
  1034. if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
  1035. tw32_f(MAC_MI_MODE, tp->mi_mode);
  1036. udelay(80);
  1037. }
  1038. tg3_ape_unlock(tp, tp->phy_ape_lock);
  1039. return ret;
  1040. }
  1041. static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
  1042. {
  1043. return __tg3_writephy(tp, tp->phy_addr, reg, val);
  1044. }
  1045. static int tg3_phy_cl45_write(struct tg3 *tp, u32 devad, u32 addr, u32 val)
  1046. {
  1047. int err;
  1048. err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
  1049. if (err)
  1050. goto done;
  1051. err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
  1052. if (err)
  1053. goto done;
  1054. err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
  1055. MII_TG3_MMD_CTRL_DATA_NOINC | devad);
  1056. if (err)
  1057. goto done;
  1058. err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, val);
  1059. done:
  1060. return err;
  1061. }
  1062. static int tg3_phy_cl45_read(struct tg3 *tp, u32 devad, u32 addr, u32 *val)
  1063. {
  1064. int err;
  1065. err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
  1066. if (err)
  1067. goto done;
  1068. err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
  1069. if (err)
  1070. goto done;
  1071. err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
  1072. MII_TG3_MMD_CTRL_DATA_NOINC | devad);
  1073. if (err)
  1074. goto done;
  1075. err = tg3_readphy(tp, MII_TG3_MMD_ADDRESS, val);
  1076. done:
  1077. return err;
  1078. }
  1079. static int tg3_phydsp_read(struct tg3 *tp, u32 reg, u32 *val)
  1080. {
  1081. int err;
  1082. err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
  1083. if (!err)
  1084. err = tg3_readphy(tp, MII_TG3_DSP_RW_PORT, val);
  1085. return err;
  1086. }
  1087. static int tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
  1088. {
  1089. int err;
  1090. err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
  1091. if (!err)
  1092. err = tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
  1093. return err;
  1094. }
  1095. static int tg3_phy_auxctl_read(struct tg3 *tp, int reg, u32 *val)
  1096. {
  1097. int err;
  1098. err = tg3_writephy(tp, MII_TG3_AUX_CTRL,
  1099. (reg << MII_TG3_AUXCTL_MISC_RDSEL_SHIFT) |
  1100. MII_TG3_AUXCTL_SHDWSEL_MISC);
  1101. if (!err)
  1102. err = tg3_readphy(tp, MII_TG3_AUX_CTRL, val);
  1103. return err;
  1104. }
  1105. static int tg3_phy_auxctl_write(struct tg3 *tp, int reg, u32 set)
  1106. {
  1107. if (reg == MII_TG3_AUXCTL_SHDWSEL_MISC)
  1108. set |= MII_TG3_AUXCTL_MISC_WREN;
  1109. return tg3_writephy(tp, MII_TG3_AUX_CTRL, set | reg);
  1110. }
  1111. static int tg3_phy_toggle_auxctl_smdsp(struct tg3 *tp, bool enable)
  1112. {
  1113. u32 val;
  1114. int err;
  1115. err = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
  1116. if (err)
  1117. return err;
  1118. if (enable)
  1119. val |= MII_TG3_AUXCTL_ACTL_SMDSP_ENA;
  1120. else
  1121. val &= ~MII_TG3_AUXCTL_ACTL_SMDSP_ENA;
  1122. err = tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
  1123. val | MII_TG3_AUXCTL_ACTL_TX_6DB);
  1124. return err;
  1125. }
  1126. static int tg3_phy_shdw_write(struct tg3 *tp, int reg, u32 val)
  1127. {
  1128. return tg3_writephy(tp, MII_TG3_MISC_SHDW,
  1129. reg | val | MII_TG3_MISC_SHDW_WREN);
  1130. }
  1131. static int tg3_bmcr_reset(struct tg3 *tp)
  1132. {
  1133. u32 phy_control;
  1134. int limit, err;
  1135. /* OK, reset it, and poll the BMCR_RESET bit until it
  1136. * clears or we time out.
  1137. */
  1138. phy_control = BMCR_RESET;
  1139. err = tg3_writephy(tp, MII_BMCR, phy_control);
  1140. if (err != 0)
  1141. return -EBUSY;
  1142. limit = 5000;
  1143. while (limit--) {
  1144. err = tg3_readphy(tp, MII_BMCR, &phy_control);
  1145. if (err != 0)
  1146. return -EBUSY;
  1147. if ((phy_control & BMCR_RESET) == 0) {
  1148. udelay(40);
  1149. break;
  1150. }
  1151. udelay(10);
  1152. }
  1153. if (limit < 0)
  1154. return -EBUSY;
  1155. return 0;
  1156. }
  1157. static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
  1158. {
  1159. struct tg3 *tp = bp->priv;
  1160. u32 val;
  1161. spin_lock_bh(&tp->lock);
  1162. if (__tg3_readphy(tp, mii_id, reg, &val))
  1163. val = -EIO;
  1164. spin_unlock_bh(&tp->lock);
  1165. return val;
  1166. }
  1167. static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
  1168. {
  1169. struct tg3 *tp = bp->priv;
  1170. u32 ret = 0;
  1171. spin_lock_bh(&tp->lock);
  1172. if (__tg3_writephy(tp, mii_id, reg, val))
  1173. ret = -EIO;
  1174. spin_unlock_bh(&tp->lock);
  1175. return ret;
  1176. }
  1177. static void tg3_mdio_config_5785(struct tg3 *tp)
  1178. {
  1179. u32 val;
  1180. struct phy_device *phydev;
  1181. phydev = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr);
  1182. switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
  1183. case PHY_ID_BCM50610:
  1184. case PHY_ID_BCM50610M:
  1185. val = MAC_PHYCFG2_50610_LED_MODES;
  1186. break;
  1187. case PHY_ID_BCMAC131:
  1188. val = MAC_PHYCFG2_AC131_LED_MODES;
  1189. break;
  1190. case PHY_ID_RTL8211C:
  1191. val = MAC_PHYCFG2_RTL8211C_LED_MODES;
  1192. break;
  1193. case PHY_ID_RTL8201E:
  1194. val = MAC_PHYCFG2_RTL8201E_LED_MODES;
  1195. break;
  1196. default:
  1197. return;
  1198. }
  1199. if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
  1200. tw32(MAC_PHYCFG2, val);
  1201. val = tr32(MAC_PHYCFG1);
  1202. val &= ~(MAC_PHYCFG1_RGMII_INT |
  1203. MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
  1204. val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
  1205. tw32(MAC_PHYCFG1, val);
  1206. return;
  1207. }
  1208. if (!tg3_flag(tp, RGMII_INBAND_DISABLE))
  1209. val |= MAC_PHYCFG2_EMODE_MASK_MASK |
  1210. MAC_PHYCFG2_FMODE_MASK_MASK |
  1211. MAC_PHYCFG2_GMODE_MASK_MASK |
  1212. MAC_PHYCFG2_ACT_MASK_MASK |
  1213. MAC_PHYCFG2_QUAL_MASK_MASK |
  1214. MAC_PHYCFG2_INBAND_ENABLE;
  1215. tw32(MAC_PHYCFG2, val);
  1216. val = tr32(MAC_PHYCFG1);
  1217. val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
  1218. MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
  1219. if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
  1220. if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
  1221. val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
  1222. if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
  1223. val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
  1224. }
  1225. val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
  1226. MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
  1227. tw32(MAC_PHYCFG1, val);
  1228. val = tr32(MAC_EXT_RGMII_MODE);
  1229. val &= ~(MAC_RGMII_MODE_RX_INT_B |
  1230. MAC_RGMII_MODE_RX_QUALITY |
  1231. MAC_RGMII_MODE_RX_ACTIVITY |
  1232. MAC_RGMII_MODE_RX_ENG_DET |
  1233. MAC_RGMII_MODE_TX_ENABLE |
  1234. MAC_RGMII_MODE_TX_LOWPWR |
  1235. MAC_RGMII_MODE_TX_RESET);
  1236. if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
  1237. if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
  1238. val |= MAC_RGMII_MODE_RX_INT_B |
  1239. MAC_RGMII_MODE_RX_QUALITY |
  1240. MAC_RGMII_MODE_RX_ACTIVITY |
  1241. MAC_RGMII_MODE_RX_ENG_DET;
  1242. if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
  1243. val |= MAC_RGMII_MODE_TX_ENABLE |
  1244. MAC_RGMII_MODE_TX_LOWPWR |
  1245. MAC_RGMII_MODE_TX_RESET;
  1246. }
  1247. tw32(MAC_EXT_RGMII_MODE, val);
  1248. }
  1249. static void tg3_mdio_start(struct tg3 *tp)
  1250. {
  1251. tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
  1252. tw32_f(MAC_MI_MODE, tp->mi_mode);
  1253. udelay(80);
  1254. if (tg3_flag(tp, MDIOBUS_INITED) &&
  1255. tg3_asic_rev(tp) == ASIC_REV_5785)
  1256. tg3_mdio_config_5785(tp);
  1257. }
  1258. static int tg3_mdio_init(struct tg3 *tp)
  1259. {
  1260. int i;
  1261. u32 reg;
  1262. struct phy_device *phydev;
  1263. if (tg3_flag(tp, 5717_PLUS)) {
  1264. u32 is_serdes;
  1265. tp->phy_addr = tp->pci_fn + 1;
  1266. if (tg3_chip_rev_id(tp) != CHIPREV_ID_5717_A0)
  1267. is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
  1268. else
  1269. is_serdes = tr32(TG3_CPMU_PHY_STRAP) &
  1270. TG3_CPMU_PHY_STRAP_IS_SERDES;
  1271. if (is_serdes)
  1272. tp->phy_addr += 7;
  1273. } else if (tg3_flag(tp, IS_SSB_CORE) && tg3_flag(tp, ROBOSWITCH)) {
  1274. int addr;
  1275. addr = ssb_gige_get_phyaddr(tp->pdev);
  1276. if (addr < 0)
  1277. return addr;
  1278. tp->phy_addr = addr;
  1279. } else
  1280. tp->phy_addr = TG3_PHY_MII_ADDR;
  1281. tg3_mdio_start(tp);
  1282. if (!tg3_flag(tp, USE_PHYLIB) || tg3_flag(tp, MDIOBUS_INITED))
  1283. return 0;
  1284. tp->mdio_bus = mdiobus_alloc();
  1285. if (tp->mdio_bus == NULL)
  1286. return -ENOMEM;
  1287. tp->mdio_bus->name = "tg3 mdio bus";
  1288. snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
  1289. (tp->pdev->bus->number << 8) | tp->pdev->devfn);
  1290. tp->mdio_bus->priv = tp;
  1291. tp->mdio_bus->parent = &tp->pdev->dev;
  1292. tp->mdio_bus->read = &tg3_mdio_read;
  1293. tp->mdio_bus->write = &tg3_mdio_write;
  1294. tp->mdio_bus->phy_mask = ~(1 << tp->phy_addr);
  1295. /* The bus registration will look for all the PHYs on the mdio bus.
  1296. * Unfortunately, it does not ensure the PHY is powered up before
  1297. * accessing the PHY ID registers. A chip reset is the
  1298. * quickest way to bring the device back to an operational state..
  1299. */
  1300. if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
  1301. tg3_bmcr_reset(tp);
  1302. i = mdiobus_register(tp->mdio_bus);
  1303. if (i) {
  1304. dev_warn(&tp->pdev->dev, "mdiobus_reg failed (0x%x)\n", i);
  1305. mdiobus_free(tp->mdio_bus);
  1306. return i;
  1307. }
  1308. phydev = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr);
  1309. if (!phydev || !phydev->drv) {
  1310. dev_warn(&tp->pdev->dev, "No PHY devices\n");
  1311. mdiobus_unregister(tp->mdio_bus);
  1312. mdiobus_free(tp->mdio_bus);
  1313. return -ENODEV;
  1314. }
  1315. switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
  1316. case PHY_ID_BCM57780:
  1317. phydev->interface = PHY_INTERFACE_MODE_GMII;
  1318. phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
  1319. break;
  1320. case PHY_ID_BCM50610:
  1321. case PHY_ID_BCM50610M:
  1322. phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
  1323. PHY_BRCM_RX_REFCLK_UNUSED |
  1324. PHY_BRCM_DIS_TXCRXC_NOENRGY |
  1325. PHY_BRCM_AUTO_PWRDWN_ENABLE;
  1326. if (tg3_flag(tp, RGMII_INBAND_DISABLE))
  1327. phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
  1328. if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
  1329. phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
  1330. if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
  1331. phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
  1332. /* fallthru */
  1333. case PHY_ID_RTL8211C:
  1334. phydev->interface = PHY_INTERFACE_MODE_RGMII;
  1335. break;
  1336. case PHY_ID_RTL8201E:
  1337. case PHY_ID_BCMAC131:
  1338. phydev->interface = PHY_INTERFACE_MODE_MII;
  1339. phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
  1340. tp->phy_flags |= TG3_PHYFLG_IS_FET;
  1341. break;
  1342. }
  1343. tg3_flag_set(tp, MDIOBUS_INITED);
  1344. if (tg3_asic_rev(tp) == ASIC_REV_5785)
  1345. tg3_mdio_config_5785(tp);
  1346. return 0;
  1347. }
  1348. static void tg3_mdio_fini(struct tg3 *tp)
  1349. {
  1350. if (tg3_flag(tp, MDIOBUS_INITED)) {
  1351. tg3_flag_clear(tp, MDIOBUS_INITED);
  1352. mdiobus_unregister(tp->mdio_bus);
  1353. mdiobus_free(tp->mdio_bus);
  1354. }
  1355. }
  1356. /* tp->lock is held. */
  1357. static inline void tg3_generate_fw_event(struct tg3 *tp)
  1358. {
  1359. u32 val;
  1360. val = tr32(GRC_RX_CPU_EVENT);
  1361. val |= GRC_RX_CPU_DRIVER_EVENT;
  1362. tw32_f(GRC_RX_CPU_EVENT, val);
  1363. tp->last_event_jiffies = jiffies;
  1364. }
  1365. #define TG3_FW_EVENT_TIMEOUT_USEC 2500
  1366. /* tp->lock is held. */
  1367. static void tg3_wait_for_event_ack(struct tg3 *tp)
  1368. {
  1369. int i;
  1370. unsigned int delay_cnt;
  1371. long time_remain;
  1372. /* If enough time has passed, no wait is necessary. */
  1373. time_remain = (long)(tp->last_event_jiffies + 1 +
  1374. usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
  1375. (long)jiffies;
  1376. if (time_remain < 0)
  1377. return;
  1378. /* Check if we can shorten the wait time. */
  1379. delay_cnt = jiffies_to_usecs(time_remain);
  1380. if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
  1381. delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
  1382. delay_cnt = (delay_cnt >> 3) + 1;
  1383. for (i = 0; i < delay_cnt; i++) {
  1384. if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
  1385. break;
  1386. if (pci_channel_offline(tp->pdev))
  1387. break;
  1388. udelay(8);
  1389. }
  1390. }
  1391. /* tp->lock is held. */
  1392. static void tg3_phy_gather_ump_data(struct tg3 *tp, u32 *data)
  1393. {
  1394. u32 reg, val;
  1395. val = 0;
  1396. if (!tg3_readphy(tp, MII_BMCR, &reg))
  1397. val = reg << 16;
  1398. if (!tg3_readphy(tp, MII_BMSR, &reg))
  1399. val |= (reg & 0xffff);
  1400. *data++ = val;
  1401. val = 0;
  1402. if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
  1403. val = reg << 16;
  1404. if (!tg3_readphy(tp, MII_LPA, &reg))
  1405. val |= (reg & 0xffff);
  1406. *data++ = val;
  1407. val = 0;
  1408. if (!(tp->phy_flags & TG3_PHYFLG_MII_SERDES)) {
  1409. if (!tg3_readphy(tp, MII_CTRL1000, &reg))
  1410. val = reg << 16;
  1411. if (!tg3_readphy(tp, MII_STAT1000, &reg))
  1412. val |= (reg & 0xffff);
  1413. }
  1414. *data++ = val;
  1415. if (!tg3_readphy(tp, MII_PHYADDR, &reg))
  1416. val = reg << 16;
  1417. else
  1418. val = 0;
  1419. *data++ = val;
  1420. }
  1421. /* tp->lock is held. */
  1422. static void tg3_ump_link_report(struct tg3 *tp)
  1423. {
  1424. u32 data[4];
  1425. if (!tg3_flag(tp, 5780_CLASS) || !tg3_flag(tp, ENABLE_ASF))
  1426. return;
  1427. tg3_phy_gather_ump_data(tp, data);
  1428. tg3_wait_for_event_ack(tp);
  1429. tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
  1430. tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
  1431. tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x0, data[0]);
  1432. tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x4, data[1]);
  1433. tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x8, data[2]);
  1434. tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0xc, data[3]);
  1435. tg3_generate_fw_event(tp);
  1436. }
  1437. /* tp->lock is held. */
  1438. static void tg3_stop_fw(struct tg3 *tp)
  1439. {
  1440. if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
  1441. /* Wait for RX cpu to ACK the previous event. */
  1442. tg3_wait_for_event_ack(tp);
  1443. tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
  1444. tg3_generate_fw_event(tp);
  1445. /* Wait for RX cpu to ACK this event. */
  1446. tg3_wait_for_event_ack(tp);
  1447. }
  1448. }
  1449. /* tp->lock is held. */
  1450. static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
  1451. {
  1452. tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
  1453. NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
  1454. if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
  1455. switch (kind) {
  1456. case RESET_KIND_INIT:
  1457. tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
  1458. DRV_STATE_START);
  1459. break;
  1460. case RESET_KIND_SHUTDOWN:
  1461. tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
  1462. DRV_STATE_UNLOAD);
  1463. break;
  1464. case RESET_KIND_SUSPEND:
  1465. tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
  1466. DRV_STATE_SUSPEND);
  1467. break;
  1468. default:
  1469. break;
  1470. }
  1471. }
  1472. }
  1473. /* tp->lock is held. */
  1474. static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
  1475. {
  1476. if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
  1477. switch (kind) {
  1478. case RESET_KIND_INIT:
  1479. tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
  1480. DRV_STATE_START_DONE);
  1481. break;
  1482. case RESET_KIND_SHUTDOWN:
  1483. tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
  1484. DRV_STATE_UNLOAD_DONE);
  1485. break;
  1486. default:
  1487. break;
  1488. }
  1489. }
  1490. }
  1491. /* tp->lock is held. */
  1492. static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
  1493. {
  1494. if (tg3_flag(tp, ENABLE_ASF)) {
  1495. switch (kind) {
  1496. case RESET_KIND_INIT:
  1497. tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
  1498. DRV_STATE_START);
  1499. break;
  1500. case RESET_KIND_SHUTDOWN:
  1501. tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
  1502. DRV_STATE_UNLOAD);
  1503. break;
  1504. case RESET_KIND_SUSPEND:
  1505. tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
  1506. DRV_STATE_SUSPEND);
  1507. break;
  1508. default:
  1509. break;
  1510. }
  1511. }
  1512. }
  1513. static int tg3_poll_fw(struct tg3 *tp)
  1514. {
  1515. int i;
  1516. u32 val;
  1517. if (tg3_flag(tp, NO_FWARE_REPORTED))
  1518. return 0;
  1519. if (tg3_flag(tp, IS_SSB_CORE)) {
  1520. /* We don't use firmware. */
  1521. return 0;
  1522. }
  1523. if (tg3_asic_rev(tp) == ASIC_REV_5906) {
  1524. /* Wait up to 20ms for init done. */
  1525. for (i = 0; i < 200; i++) {
  1526. if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
  1527. return 0;
  1528. if (pci_channel_offline(tp->pdev))
  1529. return -ENODEV;
  1530. udelay(100);
  1531. }
  1532. return -ENODEV;
  1533. }
  1534. /* Wait for firmware initialization to complete. */
  1535. for (i = 0; i < 100000; i++) {
  1536. tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
  1537. if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
  1538. break;
  1539. if (pci_channel_offline(tp->pdev)) {
  1540. if (!tg3_flag(tp, NO_FWARE_REPORTED)) {
  1541. tg3_flag_set(tp, NO_FWARE_REPORTED);
  1542. netdev_info(tp->dev, "No firmware running\n");
  1543. }
  1544. break;
  1545. }
  1546. udelay(10);
  1547. }
  1548. /* Chip might not be fitted with firmware. Some Sun onboard
  1549. * parts are configured like that. So don't signal the timeout
  1550. * of the above loop as an error, but do report the lack of
  1551. * running firmware once.
  1552. */
  1553. if (i >= 100000 && !tg3_flag(tp, NO_FWARE_REPORTED)) {
  1554. tg3_flag_set(tp, NO_FWARE_REPORTED);
  1555. netdev_info(tp->dev, "No firmware running\n");
  1556. }
  1557. if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0) {
  1558. /* The 57765 A0 needs a little more
  1559. * time to do some important work.
  1560. */
  1561. mdelay(10);
  1562. }
  1563. return 0;
  1564. }
  1565. static void tg3_link_report(struct tg3 *tp)
  1566. {
  1567. if (!netif_carrier_ok(tp->dev)) {
  1568. netif_info(tp, link, tp->dev, "Link is down\n");
  1569. tg3_ump_link_report(tp);
  1570. } else if (netif_msg_link(tp)) {
  1571. netdev_info(tp->dev, "Link is up at %d Mbps, %s duplex\n",
  1572. (tp->link_config.active_speed == SPEED_1000 ?
  1573. 1000 :
  1574. (tp->link_config.active_speed == SPEED_100 ?
  1575. 100 : 10)),
  1576. (tp->link_config.active_duplex == DUPLEX_FULL ?
  1577. "full" : "half"));
  1578. netdev_info(tp->dev, "Flow control is %s for TX and %s for RX\n",
  1579. (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
  1580. "on" : "off",
  1581. (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
  1582. "on" : "off");
  1583. if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
  1584. netdev_info(tp->dev, "EEE is %s\n",
  1585. tp->setlpicnt ? "enabled" : "disabled");
  1586. tg3_ump_link_report(tp);
  1587. }
  1588. tp->link_up = netif_carrier_ok(tp->dev);
  1589. }
  1590. static u32 tg3_decode_flowctrl_1000T(u32 adv)
  1591. {
  1592. u32 flowctrl = 0;
  1593. if (adv & ADVERTISE_PAUSE_CAP) {
  1594. flowctrl |= FLOW_CTRL_RX;
  1595. if (!(adv & ADVERTISE_PAUSE_ASYM))
  1596. flowctrl |= FLOW_CTRL_TX;
  1597. } else if (adv & ADVERTISE_PAUSE_ASYM)
  1598. flowctrl |= FLOW_CTRL_TX;
  1599. return flowctrl;
  1600. }
  1601. static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
  1602. {
  1603. u16 miireg;
  1604. if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
  1605. miireg = ADVERTISE_1000XPAUSE;
  1606. else if (flow_ctrl & FLOW_CTRL_TX)
  1607. miireg = ADVERTISE_1000XPSE_ASYM;
  1608. else if (flow_ctrl & FLOW_CTRL_RX)
  1609. miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
  1610. else
  1611. miireg = 0;
  1612. return miireg;
  1613. }
  1614. static u32 tg3_decode_flowctrl_1000X(u32 adv)
  1615. {
  1616. u32 flowctrl = 0;
  1617. if (adv & ADVERTISE_1000XPAUSE) {
  1618. flowctrl |= FLOW_CTRL_RX;
  1619. if (!(adv & ADVERTISE_1000XPSE_ASYM))
  1620. flowctrl |= FLOW_CTRL_TX;
  1621. } else if (adv & ADVERTISE_1000XPSE_ASYM)
  1622. flowctrl |= FLOW_CTRL_TX;
  1623. return flowctrl;
  1624. }
  1625. static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
  1626. {
  1627. u8 cap = 0;
  1628. if (lcladv & rmtadv & ADVERTISE_1000XPAUSE) {
  1629. cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
  1630. } else if (lcladv & rmtadv & ADVERTISE_1000XPSE_ASYM) {
  1631. if (lcladv & ADVERTISE_1000XPAUSE)
  1632. cap = FLOW_CTRL_RX;
  1633. if (rmtadv & ADVERTISE_1000XPAUSE)
  1634. cap = FLOW_CTRL_TX;
  1635. }
  1636. return cap;
  1637. }
  1638. static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
  1639. {
  1640. u8 autoneg;
  1641. u8 flowctrl = 0;
  1642. u32 old_rx_mode = tp->rx_mode;
  1643. u32 old_tx_mode = tp->tx_mode;
  1644. if (tg3_flag(tp, USE_PHYLIB))
  1645. autoneg = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr)->autoneg;
  1646. else
  1647. autoneg = tp->link_config.autoneg;
  1648. if (autoneg == AUTONEG_ENABLE && tg3_flag(tp, PAUSE_AUTONEG)) {
  1649. if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
  1650. flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
  1651. else
  1652. flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
  1653. } else
  1654. flowctrl = tp->link_config.flowctrl;
  1655. tp->link_config.active_flowctrl = flowctrl;
  1656. if (flowctrl & FLOW_CTRL_RX)
  1657. tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
  1658. else
  1659. tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
  1660. if (old_rx_mode != tp->rx_mode)
  1661. tw32_f(MAC_RX_MODE, tp->rx_mode);
  1662. if (flowctrl & FLOW_CTRL_TX)
  1663. tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
  1664. else
  1665. tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
  1666. if (old_tx_mode != tp->tx_mode)
  1667. tw32_f(MAC_TX_MODE, tp->tx_mode);
  1668. }
  1669. static void tg3_adjust_link(struct net_device *dev)
  1670. {
  1671. u8 oldflowctrl, linkmesg = 0;
  1672. u32 mac_mode, lcl_adv, rmt_adv;
  1673. struct tg3 *tp = netdev_priv(dev);
  1674. struct phy_device *phydev = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr);
  1675. spin_lock_bh(&tp->lock);
  1676. mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
  1677. MAC_MODE_HALF_DUPLEX);
  1678. oldflowctrl = tp->link_config.active_flowctrl;
  1679. if (phydev->link) {
  1680. lcl_adv = 0;
  1681. rmt_adv = 0;
  1682. if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
  1683. mac_mode |= MAC_MODE_PORT_MODE_MII;
  1684. else if (phydev->speed == SPEED_1000 ||
  1685. tg3_asic_rev(tp) != ASIC_REV_5785)
  1686. mac_mode |= MAC_MODE_PORT_MODE_GMII;
  1687. else
  1688. mac_mode |= MAC_MODE_PORT_MODE_MII;
  1689. if (phydev->duplex == DUPLEX_HALF)
  1690. mac_mode |= MAC_MODE_HALF_DUPLEX;
  1691. else {
  1692. lcl_adv = mii_advertise_flowctrl(
  1693. tp->link_config.flowctrl);
  1694. if (phydev->pause)
  1695. rmt_adv = LPA_PAUSE_CAP;
  1696. if (phydev->asym_pause)
  1697. rmt_adv |= LPA_PAUSE_ASYM;
  1698. }
  1699. tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
  1700. } else
  1701. mac_mode |= MAC_MODE_PORT_MODE_GMII;
  1702. if (mac_mode != tp->mac_mode) {
  1703. tp->mac_mode = mac_mode;
  1704. tw32_f(MAC_MODE, tp->mac_mode);
  1705. udelay(40);
  1706. }
  1707. if (tg3_asic_rev(tp) == ASIC_REV_5785) {
  1708. if (phydev->speed == SPEED_10)
  1709. tw32(MAC_MI_STAT,
  1710. MAC_MI_STAT_10MBPS_MODE |
  1711. MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
  1712. else
  1713. tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
  1714. }
  1715. if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
  1716. tw32(MAC_TX_LENGTHS,
  1717. ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
  1718. (6 << TX_LENGTHS_IPG_SHIFT) |
  1719. (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
  1720. else
  1721. tw32(MAC_TX_LENGTHS,
  1722. ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
  1723. (6 << TX_LENGTHS_IPG_SHIFT) |
  1724. (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
  1725. if (phydev->link != tp->old_link ||
  1726. phydev->speed != tp->link_config.active_speed ||
  1727. phydev->duplex != tp->link_config.active_duplex ||
  1728. oldflowctrl != tp->link_config.active_flowctrl)
  1729. linkmesg = 1;
  1730. tp->old_link = phydev->link;
  1731. tp->link_config.active_speed = phydev->speed;
  1732. tp->link_config.active_duplex = phydev->duplex;
  1733. spin_unlock_bh(&tp->lock);
  1734. if (linkmesg)
  1735. tg3_link_report(tp);
  1736. }
  1737. static int tg3_phy_init(struct tg3 *tp)
  1738. {
  1739. struct phy_device *phydev;
  1740. if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)
  1741. return 0;
  1742. /* Bring the PHY back to a known state. */
  1743. tg3_bmcr_reset(tp);
  1744. phydev = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr);
  1745. /* Attach the MAC to the PHY. */
  1746. phydev = phy_connect(tp->dev, phydev_name(phydev),
  1747. tg3_adjust_link, phydev->interface);
  1748. if (IS_ERR(phydev)) {
  1749. dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
  1750. return PTR_ERR(phydev);
  1751. }
  1752. /* Mask with MAC supported features. */
  1753. switch (phydev->interface) {
  1754. case PHY_INTERFACE_MODE_GMII:
  1755. case PHY_INTERFACE_MODE_RGMII:
  1756. if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
  1757. phydev->supported &= (PHY_GBIT_FEATURES |
  1758. SUPPORTED_Pause |
  1759. SUPPORTED_Asym_Pause);
  1760. break;
  1761. }
  1762. /* fallthru */
  1763. case PHY_INTERFACE_MODE_MII:
  1764. phydev->supported &= (PHY_BASIC_FEATURES |
  1765. SUPPORTED_Pause |
  1766. SUPPORTED_Asym_Pause);
  1767. break;
  1768. default:
  1769. phy_disconnect(mdiobus_get_phy(tp->mdio_bus, tp->phy_addr));
  1770. return -EINVAL;
  1771. }
  1772. tp->phy_flags |= TG3_PHYFLG_IS_CONNECTED;
  1773. phydev->advertising = phydev->supported;
  1774. phy_attached_info(phydev);
  1775. return 0;
  1776. }
  1777. static void tg3_phy_start(struct tg3 *tp)
  1778. {
  1779. struct phy_device *phydev;
  1780. if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
  1781. return;
  1782. phydev = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr);
  1783. if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
  1784. tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
  1785. phydev->speed = tp->link_config.speed;
  1786. phydev->duplex = tp->link_config.duplex;
  1787. phydev->autoneg = tp->link_config.autoneg;
  1788. phydev->advertising = tp->link_config.advertising;
  1789. }
  1790. phy_start(phydev);
  1791. phy_start_aneg(phydev);
  1792. }
  1793. static void tg3_phy_stop(struct tg3 *tp)
  1794. {
  1795. if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
  1796. return;
  1797. phy_stop(mdiobus_get_phy(tp->mdio_bus, tp->phy_addr));
  1798. }
  1799. static void tg3_phy_fini(struct tg3 *tp)
  1800. {
  1801. if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
  1802. phy_disconnect(mdiobus_get_phy(tp->mdio_bus, tp->phy_addr));
  1803. tp->phy_flags &= ~TG3_PHYFLG_IS_CONNECTED;
  1804. }
  1805. }
  1806. static int tg3_phy_set_extloopbk(struct tg3 *tp)
  1807. {
  1808. int err;
  1809. u32 val;
  1810. if (tp->phy_flags & TG3_PHYFLG_IS_FET)
  1811. return 0;
  1812. if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
  1813. /* Cannot do read-modify-write on 5401 */
  1814. err = tg3_phy_auxctl_write(tp,
  1815. MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
  1816. MII_TG3_AUXCTL_ACTL_EXTLOOPBK |
  1817. 0x4c20);
  1818. goto done;
  1819. }
  1820. err = tg3_phy_auxctl_read(tp,
  1821. MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
  1822. if (err)
  1823. return err;
  1824. val |= MII_TG3_AUXCTL_ACTL_EXTLOOPBK;
  1825. err = tg3_phy_auxctl_write(tp,
  1826. MII_TG3_AUXCTL_SHDWSEL_AUXCTL, val);
  1827. done:
  1828. return err;
  1829. }
  1830. static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
  1831. {
  1832. u32 phytest;
  1833. if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
  1834. u32 phy;
  1835. tg3_writephy(tp, MII_TG3_FET_TEST,
  1836. phytest | MII_TG3_FET_SHADOW_EN);
  1837. if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
  1838. if (enable)
  1839. phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
  1840. else
  1841. phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
  1842. tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
  1843. }
  1844. tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
  1845. }
  1846. }
  1847. static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
  1848. {
  1849. u32 reg;
  1850. if (!tg3_flag(tp, 5705_PLUS) ||
  1851. (tg3_flag(tp, 5717_PLUS) &&
  1852. (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
  1853. return;
  1854. if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
  1855. tg3_phy_fet_toggle_apd(tp, enable);
  1856. return;
  1857. }
  1858. reg = MII_TG3_MISC_SHDW_SCR5_LPED |
  1859. MII_TG3_MISC_SHDW_SCR5_DLPTLM |
  1860. MII_TG3_MISC_SHDW_SCR5_SDTL |
  1861. MII_TG3_MISC_SHDW_SCR5_C125OE;
  1862. if (tg3_asic_rev(tp) != ASIC_REV_5784 || !enable)
  1863. reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
  1864. tg3_phy_shdw_write(tp, MII_TG3_MISC_SHDW_SCR5_SEL, reg);
  1865. reg = MII_TG3_MISC_SHDW_APD_WKTM_84MS;
  1866. if (enable)
  1867. reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
  1868. tg3_phy_shdw_write(tp, MII_TG3_MISC_SHDW_APD_SEL, reg);
  1869. }
  1870. static void tg3_phy_toggle_automdix(struct tg3 *tp, bool enable)
  1871. {
  1872. u32 phy;
  1873. if (!tg3_flag(tp, 5705_PLUS) ||
  1874. (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
  1875. return;
  1876. if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
  1877. u32 ephy;
  1878. if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
  1879. u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
  1880. tg3_writephy(tp, MII_TG3_FET_TEST,
  1881. ephy | MII_TG3_FET_SHADOW_EN);
  1882. if (!tg3_readphy(tp, reg, &phy)) {
  1883. if (enable)
  1884. phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
  1885. else
  1886. phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
  1887. tg3_writephy(tp, reg, phy);
  1888. }
  1889. tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
  1890. }
  1891. } else {
  1892. int ret;
  1893. ret = tg3_phy_auxctl_read(tp,
  1894. MII_TG3_AUXCTL_SHDWSEL_MISC, &phy);
  1895. if (!ret) {
  1896. if (enable)
  1897. phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
  1898. else
  1899. phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
  1900. tg3_phy_auxctl_write(tp,
  1901. MII_TG3_AUXCTL_SHDWSEL_MISC, phy);
  1902. }
  1903. }
  1904. }
  1905. static void tg3_phy_set_wirespeed(struct tg3 *tp)
  1906. {
  1907. int ret;
  1908. u32 val;
  1909. if (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED)
  1910. return;
  1911. ret = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_MISC, &val);
  1912. if (!ret)
  1913. tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_MISC,
  1914. val | MII_TG3_AUXCTL_MISC_WIRESPD_EN);
  1915. }
  1916. static void tg3_phy_apply_otp(struct tg3 *tp)
  1917. {
  1918. u32 otp, phy;
  1919. if (!tp->phy_otp)
  1920. return;
  1921. otp = tp->phy_otp;
  1922. if (tg3_phy_toggle_auxctl_smdsp(tp, true))
  1923. return;
  1924. phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
  1925. phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
  1926. tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
  1927. phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
  1928. ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
  1929. tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
  1930. phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
  1931. phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
  1932. tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
  1933. phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
  1934. tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
  1935. phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
  1936. tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
  1937. phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
  1938. ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
  1939. tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
  1940. tg3_phy_toggle_auxctl_smdsp(tp, false);
  1941. }
  1942. static void tg3_eee_pull_config(struct tg3 *tp, struct ethtool_eee *eee)
  1943. {
  1944. u32 val;
  1945. struct ethtool_eee *dest = &tp->eee;
  1946. if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
  1947. return;
  1948. if (eee)
  1949. dest = eee;
  1950. if (tg3_phy_cl45_read(tp, MDIO_MMD_AN, TG3_CL45_D7_EEERES_STAT, &val))
  1951. return;
  1952. /* Pull eee_active */
  1953. if (val == TG3_CL45_D7_EEERES_STAT_LP_1000T ||
  1954. val == TG3_CL45_D7_EEERES_STAT_LP_100TX) {
  1955. dest->eee_active = 1;
  1956. } else
  1957. dest->eee_active = 0;
  1958. /* Pull lp advertised settings */
  1959. if (tg3_phy_cl45_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE, &val))
  1960. return;
  1961. dest->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(val);
  1962. /* Pull advertised and eee_enabled settings */
  1963. if (tg3_phy_cl45_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, &val))
  1964. return;
  1965. dest->eee_enabled = !!val;
  1966. dest->advertised = mmd_eee_adv_to_ethtool_adv_t(val);
  1967. /* Pull tx_lpi_enabled */
  1968. val = tr32(TG3_CPMU_EEE_MODE);
  1969. dest->tx_lpi_enabled = !!(val & TG3_CPMU_EEEMD_LPI_IN_TX);
  1970. /* Pull lpi timer value */
  1971. dest->tx_lpi_timer = tr32(TG3_CPMU_EEE_DBTMR1) & 0xffff;
  1972. }
  1973. static void tg3_phy_eee_adjust(struct tg3 *tp, bool current_link_up)
  1974. {
  1975. u32 val;
  1976. if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
  1977. return;
  1978. tp->setlpicnt = 0;
  1979. if (tp->link_config.autoneg == AUTONEG_ENABLE &&
  1980. current_link_up &&
  1981. tp->link_config.active_duplex == DUPLEX_FULL &&
  1982. (tp->link_config.active_speed == SPEED_100 ||
  1983. tp->link_config.active_speed == SPEED_1000)) {
  1984. u32 eeectl;
  1985. if (tp->link_config.active_speed == SPEED_1000)
  1986. eeectl = TG3_CPMU_EEE_CTRL_EXIT_16_5_US;
  1987. else
  1988. eeectl = TG3_CPMU_EEE_CTRL_EXIT_36_US;
  1989. tw32(TG3_CPMU_EEE_CTRL, eeectl);
  1990. tg3_eee_pull_config(tp, NULL);
  1991. if (tp->eee.eee_active)
  1992. tp->setlpicnt = 2;
  1993. }
  1994. if (!tp->setlpicnt) {
  1995. if (current_link_up &&
  1996. !tg3_phy_toggle_auxctl_smdsp(tp, true)) {
  1997. tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, 0x0000);
  1998. tg3_phy_toggle_auxctl_smdsp(tp, false);
  1999. }
  2000. val = tr32(TG3_CPMU_EEE_MODE);
  2001. tw32(TG3_CPMU_EEE_MODE, val & ~TG3_CPMU_EEEMD_LPI_ENABLE);
  2002. }
  2003. }
  2004. static void tg3_phy_eee_enable(struct tg3 *tp)
  2005. {
  2006. u32 val;
  2007. if (tp->link_config.active_speed == SPEED_1000 &&
  2008. (tg3_asic_rev(tp) == ASIC_REV_5717 ||
  2009. tg3_asic_rev(tp) == ASIC_REV_5719 ||
  2010. tg3_flag(tp, 57765_CLASS)) &&
  2011. !tg3_phy_toggle_auxctl_smdsp(tp, true)) {
  2012. val = MII_TG3_DSP_TAP26_ALNOKO |
  2013. MII_TG3_DSP_TAP26_RMRXSTO;
  2014. tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
  2015. tg3_phy_toggle_auxctl_smdsp(tp, false);
  2016. }
  2017. val = tr32(TG3_CPMU_EEE_MODE);
  2018. tw32(TG3_CPMU_EEE_MODE, val | TG3_CPMU_EEEMD_LPI_ENABLE);
  2019. }
  2020. static int tg3_wait_macro_done(struct tg3 *tp)
  2021. {
  2022. int limit = 100;
  2023. while (limit--) {
  2024. u32 tmp32;
  2025. if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
  2026. if ((tmp32 & 0x1000) == 0)
  2027. break;
  2028. }
  2029. }
  2030. if (limit < 0)
  2031. return -EBUSY;
  2032. return 0;
  2033. }
  2034. static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
  2035. {
  2036. static const u32 test_pat[4][6] = {
  2037. { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
  2038. { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
  2039. { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
  2040. { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
  2041. };
  2042. int chan;
  2043. for (chan = 0; chan < 4; chan++) {
  2044. int i;
  2045. tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
  2046. (chan * 0x2000) | 0x0200);
  2047. tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
  2048. for (i = 0; i < 6; i++)
  2049. tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
  2050. test_pat[chan][i]);
  2051. tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
  2052. if (tg3_wait_macro_done(tp)) {
  2053. *resetp = 1;
  2054. return -EBUSY;
  2055. }
  2056. tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
  2057. (chan * 0x2000) | 0x0200);
  2058. tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
  2059. if (tg3_wait_macro_done(tp)) {
  2060. *resetp = 1;
  2061. return -EBUSY;
  2062. }
  2063. tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
  2064. if (tg3_wait_macro_done(tp)) {
  2065. *resetp = 1;
  2066. return -EBUSY;
  2067. }
  2068. for (i = 0; i < 6; i += 2) {
  2069. u32 low, high;
  2070. if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
  2071. tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
  2072. tg3_wait_macro_done(tp)) {
  2073. *resetp = 1;
  2074. return -EBUSY;
  2075. }
  2076. low &= 0x7fff;
  2077. high &= 0x000f;
  2078. if (low != test_pat[chan][i] ||
  2079. high != test_pat[chan][i+1]) {
  2080. tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
  2081. tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
  2082. tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
  2083. return -EBUSY;
  2084. }
  2085. }
  2086. }
  2087. return 0;
  2088. }
  2089. static int tg3_phy_reset_chanpat(struct tg3 *tp)
  2090. {
  2091. int chan;
  2092. for (chan = 0; chan < 4; chan++) {
  2093. int i;
  2094. tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
  2095. (chan * 0x2000) | 0x0200);
  2096. tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
  2097. for (i = 0; i < 6; i++)
  2098. tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
  2099. tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
  2100. if (tg3_wait_macro_done(tp))
  2101. return -EBUSY;
  2102. }
  2103. return 0;
  2104. }
  2105. static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
  2106. {
  2107. u32 reg32, phy9_orig;
  2108. int retries, do_phy_reset, err;
  2109. retries = 10;
  2110. do_phy_reset = 1;
  2111. do {
  2112. if (do_phy_reset) {
  2113. err = tg3_bmcr_reset(tp);
  2114. if (err)
  2115. return err;
  2116. do_phy_reset = 0;
  2117. }
  2118. /* Disable transmitter and interrupt. */
  2119. if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
  2120. continue;
  2121. reg32 |= 0x3000;
  2122. tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
  2123. /* Set full-duplex, 1000 mbps. */
  2124. tg3_writephy(tp, MII_BMCR,
  2125. BMCR_FULLDPLX | BMCR_SPEED1000);
  2126. /* Set to master mode. */
  2127. if (tg3_readphy(tp, MII_CTRL1000, &phy9_orig))
  2128. continue;
  2129. tg3_writephy(tp, MII_CTRL1000,
  2130. CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
  2131. err = tg3_phy_toggle_auxctl_smdsp(tp, true);
  2132. if (err)
  2133. return err;
  2134. /* Block the PHY control access. */
  2135. tg3_phydsp_write(tp, 0x8005, 0x0800);
  2136. err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
  2137. if (!err)
  2138. break;
  2139. } while (--retries);
  2140. err = tg3_phy_reset_chanpat(tp);
  2141. if (err)
  2142. return err;
  2143. tg3_phydsp_write(tp, 0x8005, 0x0000);
  2144. tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
  2145. tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0000);
  2146. tg3_phy_toggle_auxctl_smdsp(tp, false);
  2147. tg3_writephy(tp, MII_CTRL1000, phy9_orig);
  2148. err = tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32);
  2149. if (err)
  2150. return err;
  2151. reg32 &= ~0x3000;
  2152. tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
  2153. return 0;
  2154. }
  2155. static void tg3_carrier_off(struct tg3 *tp)
  2156. {
  2157. netif_carrier_off(tp->dev);
  2158. tp->link_up = false;
  2159. }
  2160. static void tg3_warn_mgmt_link_flap(struct tg3 *tp)
  2161. {
  2162. if (tg3_flag(tp, ENABLE_ASF))
  2163. netdev_warn(tp->dev,
  2164. "Management side-band traffic will be interrupted during phy settings change\n");
  2165. }
  2166. /* This will reset the tigon3 PHY if there is no valid
  2167. * link unless the FORCE argument is non-zero.
  2168. */
  2169. static int tg3_phy_reset(struct tg3 *tp)
  2170. {
  2171. u32 val, cpmuctrl;
  2172. int err;
  2173. if (tg3_asic_rev(tp) == ASIC_REV_5906) {
  2174. val = tr32(GRC_MISC_CFG);
  2175. tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
  2176. udelay(40);
  2177. }
  2178. err = tg3_readphy(tp, MII_BMSR, &val);
  2179. err |= tg3_readphy(tp, MII_BMSR, &val);
  2180. if (err != 0)
  2181. return -EBUSY;
  2182. if (netif_running(tp->dev) && tp->link_up) {
  2183. netif_carrier_off(tp->dev);
  2184. tg3_link_report(tp);
  2185. }
  2186. if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
  2187. tg3_asic_rev(tp) == ASIC_REV_5704 ||
  2188. tg3_asic_rev(tp) == ASIC_REV_5705) {
  2189. err = tg3_phy_reset_5703_4_5(tp);
  2190. if (err)
  2191. return err;
  2192. goto out;
  2193. }
  2194. cpmuctrl = 0;
  2195. if (tg3_asic_rev(tp) == ASIC_REV_5784 &&
  2196. tg3_chip_rev(tp) != CHIPREV_5784_AX) {
  2197. cpmuctrl = tr32(TG3_CPMU_CTRL);
  2198. if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
  2199. tw32(TG3_CPMU_CTRL,
  2200. cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
  2201. }
  2202. err = tg3_bmcr_reset(tp);
  2203. if (err)
  2204. return err;
  2205. if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
  2206. val = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
  2207. tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, val);
  2208. tw32(TG3_CPMU_CTRL, cpmuctrl);
  2209. }
  2210. if (tg3_chip_rev(tp) == CHIPREV_5784_AX ||
  2211. tg3_chip_rev(tp) == CHIPREV_5761_AX) {
  2212. val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
  2213. if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
  2214. CPMU_LSPD_1000MB_MACCLK_12_5) {
  2215. val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
  2216. udelay(40);
  2217. tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
  2218. }
  2219. }
  2220. if (tg3_flag(tp, 5717_PLUS) &&
  2221. (tp->phy_flags & TG3_PHYFLG_MII_SERDES))
  2222. return 0;
  2223. tg3_phy_apply_otp(tp);
  2224. if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
  2225. tg3_phy_toggle_apd(tp, true);
  2226. else
  2227. tg3_phy_toggle_apd(tp, false);
  2228. out:
  2229. if ((tp->phy_flags & TG3_PHYFLG_ADC_BUG) &&
  2230. !tg3_phy_toggle_auxctl_smdsp(tp, true)) {
  2231. tg3_phydsp_write(tp, 0x201f, 0x2aaa);
  2232. tg3_phydsp_write(tp, 0x000a, 0x0323);
  2233. tg3_phy_toggle_auxctl_smdsp(tp, false);
  2234. }
  2235. if (tp->phy_flags & TG3_PHYFLG_5704_A0_BUG) {
  2236. tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
  2237. tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
  2238. }
  2239. if (tp->phy_flags & TG3_PHYFLG_BER_BUG) {
  2240. if (!tg3_phy_toggle_auxctl_smdsp(tp, true)) {
  2241. tg3_phydsp_write(tp, 0x000a, 0x310b);
  2242. tg3_phydsp_write(tp, 0x201f, 0x9506);
  2243. tg3_phydsp_write(tp, 0x401f, 0x14e2);
  2244. tg3_phy_toggle_auxctl_smdsp(tp, false);
  2245. }
  2246. } else if (tp->phy_flags & TG3_PHYFLG_JITTER_BUG) {
  2247. if (!tg3_phy_toggle_auxctl_smdsp(tp, true)) {
  2248. tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
  2249. if (tp->phy_flags & TG3_PHYFLG_ADJUST_TRIM) {
  2250. tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
  2251. tg3_writephy(tp, MII_TG3_TEST1,
  2252. MII_TG3_TEST1_TRIM_EN | 0x4);
  2253. } else
  2254. tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
  2255. tg3_phy_toggle_auxctl_smdsp(tp, false);
  2256. }
  2257. }
  2258. /* Set Extended packet length bit (bit 14) on all chips that */
  2259. /* support jumbo frames */
  2260. if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
  2261. /* Cannot do read-modify-write on 5401 */
  2262. tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
  2263. } else if (tg3_flag(tp, JUMBO_CAPABLE)) {
  2264. /* Set bit 14 with read-modify-write to preserve other bits */
  2265. err = tg3_phy_auxctl_read(tp,
  2266. MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
  2267. if (!err)
  2268. tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
  2269. val | MII_TG3_AUXCTL_ACTL_EXTPKTLEN);
  2270. }
  2271. /* Set phy register 0x10 bit 0 to high fifo elasticity to support
  2272. * jumbo frames transmission.
  2273. */
  2274. if (tg3_flag(tp, JUMBO_CAPABLE)) {
  2275. if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &val))
  2276. tg3_writephy(tp, MII_TG3_EXT_CTRL,
  2277. val | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
  2278. }
  2279. if (tg3_asic_rev(tp) == ASIC_REV_5906) {
  2280. /* adjust output voltage */
  2281. tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
  2282. }
  2283. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5762_A0)
  2284. tg3_phydsp_write(tp, 0xffb, 0x4000);
  2285. tg3_phy_toggle_automdix(tp, true);
  2286. tg3_phy_set_wirespeed(tp);
  2287. return 0;
  2288. }
  2289. #define TG3_GPIO_MSG_DRVR_PRES 0x00000001
  2290. #define TG3_GPIO_MSG_NEED_VAUX 0x00000002
  2291. #define TG3_GPIO_MSG_MASK (TG3_GPIO_MSG_DRVR_PRES | \
  2292. TG3_GPIO_MSG_NEED_VAUX)
  2293. #define TG3_GPIO_MSG_ALL_DRVR_PRES_MASK \
  2294. ((TG3_GPIO_MSG_DRVR_PRES << 0) | \
  2295. (TG3_GPIO_MSG_DRVR_PRES << 4) | \
  2296. (TG3_GPIO_MSG_DRVR_PRES << 8) | \
  2297. (TG3_GPIO_MSG_DRVR_PRES << 12))
  2298. #define TG3_GPIO_MSG_ALL_NEED_VAUX_MASK \
  2299. ((TG3_GPIO_MSG_NEED_VAUX << 0) | \
  2300. (TG3_GPIO_MSG_NEED_VAUX << 4) | \
  2301. (TG3_GPIO_MSG_NEED_VAUX << 8) | \
  2302. (TG3_GPIO_MSG_NEED_VAUX << 12))
  2303. static inline u32 tg3_set_function_status(struct tg3 *tp, u32 newstat)
  2304. {
  2305. u32 status, shift;
  2306. if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
  2307. tg3_asic_rev(tp) == ASIC_REV_5719)
  2308. status = tg3_ape_read32(tp, TG3_APE_GPIO_MSG);
  2309. else
  2310. status = tr32(TG3_CPMU_DRV_STATUS);
  2311. shift = TG3_APE_GPIO_MSG_SHIFT + 4 * tp->pci_fn;
  2312. status &= ~(TG3_GPIO_MSG_MASK << shift);
  2313. status |= (newstat << shift);
  2314. if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
  2315. tg3_asic_rev(tp) == ASIC_REV_5719)
  2316. tg3_ape_write32(tp, TG3_APE_GPIO_MSG, status);
  2317. else
  2318. tw32(TG3_CPMU_DRV_STATUS, status);
  2319. return status >> TG3_APE_GPIO_MSG_SHIFT;
  2320. }
  2321. static inline int tg3_pwrsrc_switch_to_vmain(struct tg3 *tp)
  2322. {
  2323. if (!tg3_flag(tp, IS_NIC))
  2324. return 0;
  2325. if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
  2326. tg3_asic_rev(tp) == ASIC_REV_5719 ||
  2327. tg3_asic_rev(tp) == ASIC_REV_5720) {
  2328. if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
  2329. return -EIO;
  2330. tg3_set_function_status(tp, TG3_GPIO_MSG_DRVR_PRES);
  2331. tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
  2332. TG3_GRC_LCLCTL_PWRSW_DELAY);
  2333. tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
  2334. } else {
  2335. tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
  2336. TG3_GRC_LCLCTL_PWRSW_DELAY);
  2337. }
  2338. return 0;
  2339. }
  2340. static void tg3_pwrsrc_die_with_vmain(struct tg3 *tp)
  2341. {
  2342. u32 grc_local_ctrl;
  2343. if (!tg3_flag(tp, IS_NIC) ||
  2344. tg3_asic_rev(tp) == ASIC_REV_5700 ||
  2345. tg3_asic_rev(tp) == ASIC_REV_5701)
  2346. return;
  2347. grc_local_ctrl = tp->grc_local_ctrl | GRC_LCLCTRL_GPIO_OE1;
  2348. tw32_wait_f(GRC_LOCAL_CTRL,
  2349. grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
  2350. TG3_GRC_LCLCTL_PWRSW_DELAY);
  2351. tw32_wait_f(GRC_LOCAL_CTRL,
  2352. grc_local_ctrl,
  2353. TG3_GRC_LCLCTL_PWRSW_DELAY);
  2354. tw32_wait_f(GRC_LOCAL_CTRL,
  2355. grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
  2356. TG3_GRC_LCLCTL_PWRSW_DELAY);
  2357. }
  2358. static void tg3_pwrsrc_switch_to_vaux(struct tg3 *tp)
  2359. {
  2360. if (!tg3_flag(tp, IS_NIC))
  2361. return;
  2362. if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
  2363. tg3_asic_rev(tp) == ASIC_REV_5701) {
  2364. tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
  2365. (GRC_LCLCTRL_GPIO_OE0 |
  2366. GRC_LCLCTRL_GPIO_OE1 |
  2367. GRC_LCLCTRL_GPIO_OE2 |
  2368. GRC_LCLCTRL_GPIO_OUTPUT0 |
  2369. GRC_LCLCTRL_GPIO_OUTPUT1),
  2370. TG3_GRC_LCLCTL_PWRSW_DELAY);
  2371. } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
  2372. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
  2373. /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
  2374. u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
  2375. GRC_LCLCTRL_GPIO_OE1 |
  2376. GRC_LCLCTRL_GPIO_OE2 |
  2377. GRC_LCLCTRL_GPIO_OUTPUT0 |
  2378. GRC_LCLCTRL_GPIO_OUTPUT1 |
  2379. tp->grc_local_ctrl;
  2380. tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
  2381. TG3_GRC_LCLCTL_PWRSW_DELAY);
  2382. grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
  2383. tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
  2384. TG3_GRC_LCLCTL_PWRSW_DELAY);
  2385. grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
  2386. tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
  2387. TG3_GRC_LCLCTL_PWRSW_DELAY);
  2388. } else {
  2389. u32 no_gpio2;
  2390. u32 grc_local_ctrl = 0;
  2391. /* Workaround to prevent overdrawing Amps. */
  2392. if (tg3_asic_rev(tp) == ASIC_REV_5714) {
  2393. grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
  2394. tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
  2395. grc_local_ctrl,
  2396. TG3_GRC_LCLCTL_PWRSW_DELAY);
  2397. }
  2398. /* On 5753 and variants, GPIO2 cannot be used. */
  2399. no_gpio2 = tp->nic_sram_data_cfg &
  2400. NIC_SRAM_DATA_CFG_NO_GPIO2;
  2401. grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
  2402. GRC_LCLCTRL_GPIO_OE1 |
  2403. GRC_LCLCTRL_GPIO_OE2 |
  2404. GRC_LCLCTRL_GPIO_OUTPUT1 |
  2405. GRC_LCLCTRL_GPIO_OUTPUT2;
  2406. if (no_gpio2) {
  2407. grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
  2408. GRC_LCLCTRL_GPIO_OUTPUT2);
  2409. }
  2410. tw32_wait_f(GRC_LOCAL_CTRL,
  2411. tp->grc_local_ctrl | grc_local_ctrl,
  2412. TG3_GRC_LCLCTL_PWRSW_DELAY);
  2413. grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
  2414. tw32_wait_f(GRC_LOCAL_CTRL,
  2415. tp->grc_local_ctrl | grc_local_ctrl,
  2416. TG3_GRC_LCLCTL_PWRSW_DELAY);
  2417. if (!no_gpio2) {
  2418. grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
  2419. tw32_wait_f(GRC_LOCAL_CTRL,
  2420. tp->grc_local_ctrl | grc_local_ctrl,
  2421. TG3_GRC_LCLCTL_PWRSW_DELAY);
  2422. }
  2423. }
  2424. }
  2425. static void tg3_frob_aux_power_5717(struct tg3 *tp, bool wol_enable)
  2426. {
  2427. u32 msg = 0;
  2428. /* Serialize power state transitions */
  2429. if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
  2430. return;
  2431. if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE) || wol_enable)
  2432. msg = TG3_GPIO_MSG_NEED_VAUX;
  2433. msg = tg3_set_function_status(tp, msg);
  2434. if (msg & TG3_GPIO_MSG_ALL_DRVR_PRES_MASK)
  2435. goto done;
  2436. if (msg & TG3_GPIO_MSG_ALL_NEED_VAUX_MASK)
  2437. tg3_pwrsrc_switch_to_vaux(tp);
  2438. else
  2439. tg3_pwrsrc_die_with_vmain(tp);
  2440. done:
  2441. tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
  2442. }
  2443. static void tg3_frob_aux_power(struct tg3 *tp, bool include_wol)
  2444. {
  2445. bool need_vaux = false;
  2446. /* The GPIOs do something completely different on 57765. */
  2447. if (!tg3_flag(tp, IS_NIC) || tg3_flag(tp, 57765_CLASS))
  2448. return;
  2449. if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
  2450. tg3_asic_rev(tp) == ASIC_REV_5719 ||
  2451. tg3_asic_rev(tp) == ASIC_REV_5720) {
  2452. tg3_frob_aux_power_5717(tp, include_wol ?
  2453. tg3_flag(tp, WOL_ENABLE) != 0 : 0);
  2454. return;
  2455. }
  2456. if (tp->pdev_peer && tp->pdev_peer != tp->pdev) {
  2457. struct net_device *dev_peer;
  2458. dev_peer = pci_get_drvdata(tp->pdev_peer);
  2459. /* remove_one() may have been run on the peer. */
  2460. if (dev_peer) {
  2461. struct tg3 *tp_peer = netdev_priv(dev_peer);
  2462. if (tg3_flag(tp_peer, INIT_COMPLETE))
  2463. return;
  2464. if ((include_wol && tg3_flag(tp_peer, WOL_ENABLE)) ||
  2465. tg3_flag(tp_peer, ENABLE_ASF))
  2466. need_vaux = true;
  2467. }
  2468. }
  2469. if ((include_wol && tg3_flag(tp, WOL_ENABLE)) ||
  2470. tg3_flag(tp, ENABLE_ASF))
  2471. need_vaux = true;
  2472. if (need_vaux)
  2473. tg3_pwrsrc_switch_to_vaux(tp);
  2474. else
  2475. tg3_pwrsrc_die_with_vmain(tp);
  2476. }
  2477. static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
  2478. {
  2479. if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
  2480. return 1;
  2481. else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
  2482. if (speed != SPEED_10)
  2483. return 1;
  2484. } else if (speed == SPEED_10)
  2485. return 1;
  2486. return 0;
  2487. }
  2488. static bool tg3_phy_power_bug(struct tg3 *tp)
  2489. {
  2490. switch (tg3_asic_rev(tp)) {
  2491. case ASIC_REV_5700:
  2492. case ASIC_REV_5704:
  2493. return true;
  2494. case ASIC_REV_5780:
  2495. if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
  2496. return true;
  2497. return false;
  2498. case ASIC_REV_5717:
  2499. if (!tp->pci_fn)
  2500. return true;
  2501. return false;
  2502. case ASIC_REV_5719:
  2503. case ASIC_REV_5720:
  2504. if ((tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
  2505. !tp->pci_fn)
  2506. return true;
  2507. return false;
  2508. }
  2509. return false;
  2510. }
  2511. static bool tg3_phy_led_bug(struct tg3 *tp)
  2512. {
  2513. switch (tg3_asic_rev(tp)) {
  2514. case ASIC_REV_5719:
  2515. case ASIC_REV_5720:
  2516. if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
  2517. !tp->pci_fn)
  2518. return true;
  2519. return false;
  2520. }
  2521. return false;
  2522. }
  2523. static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
  2524. {
  2525. u32 val;
  2526. if (tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN)
  2527. return;
  2528. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
  2529. if (tg3_asic_rev(tp) == ASIC_REV_5704) {
  2530. u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
  2531. u32 serdes_cfg = tr32(MAC_SERDES_CFG);
  2532. sg_dig_ctrl |=
  2533. SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
  2534. tw32(SG_DIG_CTRL, sg_dig_ctrl);
  2535. tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
  2536. }
  2537. return;
  2538. }
  2539. if (tg3_asic_rev(tp) == ASIC_REV_5906) {
  2540. tg3_bmcr_reset(tp);
  2541. val = tr32(GRC_MISC_CFG);
  2542. tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
  2543. udelay(40);
  2544. return;
  2545. } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
  2546. u32 phytest;
  2547. if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
  2548. u32 phy;
  2549. tg3_writephy(tp, MII_ADVERTISE, 0);
  2550. tg3_writephy(tp, MII_BMCR,
  2551. BMCR_ANENABLE | BMCR_ANRESTART);
  2552. tg3_writephy(tp, MII_TG3_FET_TEST,
  2553. phytest | MII_TG3_FET_SHADOW_EN);
  2554. if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
  2555. phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
  2556. tg3_writephy(tp,
  2557. MII_TG3_FET_SHDW_AUXMODE4,
  2558. phy);
  2559. }
  2560. tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
  2561. }
  2562. return;
  2563. } else if (do_low_power) {
  2564. if (!tg3_phy_led_bug(tp))
  2565. tg3_writephy(tp, MII_TG3_EXT_CTRL,
  2566. MII_TG3_EXT_CTRL_FORCE_LED_OFF);
  2567. val = MII_TG3_AUXCTL_PCTL_100TX_LPWR |
  2568. MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
  2569. MII_TG3_AUXCTL_PCTL_VREG_11V;
  2570. tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, val);
  2571. }
  2572. /* The PHY should not be powered down on some chips because
  2573. * of bugs.
  2574. */
  2575. if (tg3_phy_power_bug(tp))
  2576. return;
  2577. if (tg3_chip_rev(tp) == CHIPREV_5784_AX ||
  2578. tg3_chip_rev(tp) == CHIPREV_5761_AX) {
  2579. val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
  2580. val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
  2581. val |= CPMU_LSPD_1000MB_MACCLK_12_5;
  2582. tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
  2583. }
  2584. tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
  2585. }
  2586. /* tp->lock is held. */
  2587. static int tg3_nvram_lock(struct tg3 *tp)
  2588. {
  2589. if (tg3_flag(tp, NVRAM)) {
  2590. int i;
  2591. if (tp->nvram_lock_cnt == 0) {
  2592. tw32(NVRAM_SWARB, SWARB_REQ_SET1);
  2593. for (i = 0; i < 8000; i++) {
  2594. if (tr32(NVRAM_SWARB) & SWARB_GNT1)
  2595. break;
  2596. udelay(20);
  2597. }
  2598. if (i == 8000) {
  2599. tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
  2600. return -ENODEV;
  2601. }
  2602. }
  2603. tp->nvram_lock_cnt++;
  2604. }
  2605. return 0;
  2606. }
  2607. /* tp->lock is held. */
  2608. static void tg3_nvram_unlock(struct tg3 *tp)
  2609. {
  2610. if (tg3_flag(tp, NVRAM)) {
  2611. if (tp->nvram_lock_cnt > 0)
  2612. tp->nvram_lock_cnt--;
  2613. if (tp->nvram_lock_cnt == 0)
  2614. tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
  2615. }
  2616. }
  2617. /* tp->lock is held. */
  2618. static void tg3_enable_nvram_access(struct tg3 *tp)
  2619. {
  2620. if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
  2621. u32 nvaccess = tr32(NVRAM_ACCESS);
  2622. tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
  2623. }
  2624. }
  2625. /* tp->lock is held. */
  2626. static void tg3_disable_nvram_access(struct tg3 *tp)
  2627. {
  2628. if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
  2629. u32 nvaccess = tr32(NVRAM_ACCESS);
  2630. tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
  2631. }
  2632. }
  2633. static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
  2634. u32 offset, u32 *val)
  2635. {
  2636. u32 tmp;
  2637. int i;
  2638. if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
  2639. return -EINVAL;
  2640. tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
  2641. EEPROM_ADDR_DEVID_MASK |
  2642. EEPROM_ADDR_READ);
  2643. tw32(GRC_EEPROM_ADDR,
  2644. tmp |
  2645. (0 << EEPROM_ADDR_DEVID_SHIFT) |
  2646. ((offset << EEPROM_ADDR_ADDR_SHIFT) &
  2647. EEPROM_ADDR_ADDR_MASK) |
  2648. EEPROM_ADDR_READ | EEPROM_ADDR_START);
  2649. for (i = 0; i < 1000; i++) {
  2650. tmp = tr32(GRC_EEPROM_ADDR);
  2651. if (tmp & EEPROM_ADDR_COMPLETE)
  2652. break;
  2653. msleep(1);
  2654. }
  2655. if (!(tmp & EEPROM_ADDR_COMPLETE))
  2656. return -EBUSY;
  2657. tmp = tr32(GRC_EEPROM_DATA);
  2658. /*
  2659. * The data will always be opposite the native endian
  2660. * format. Perform a blind byteswap to compensate.
  2661. */
  2662. *val = swab32(tmp);
  2663. return 0;
  2664. }
  2665. #define NVRAM_CMD_TIMEOUT 10000
  2666. static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
  2667. {
  2668. int i;
  2669. tw32(NVRAM_CMD, nvram_cmd);
  2670. for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
  2671. usleep_range(10, 40);
  2672. if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
  2673. udelay(10);
  2674. break;
  2675. }
  2676. }
  2677. if (i == NVRAM_CMD_TIMEOUT)
  2678. return -EBUSY;
  2679. return 0;
  2680. }
  2681. static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
  2682. {
  2683. if (tg3_flag(tp, NVRAM) &&
  2684. tg3_flag(tp, NVRAM_BUFFERED) &&
  2685. tg3_flag(tp, FLASH) &&
  2686. !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
  2687. (tp->nvram_jedecnum == JEDEC_ATMEL))
  2688. addr = ((addr / tp->nvram_pagesize) <<
  2689. ATMEL_AT45DB0X1B_PAGE_POS) +
  2690. (addr % tp->nvram_pagesize);
  2691. return addr;
  2692. }
  2693. static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
  2694. {
  2695. if (tg3_flag(tp, NVRAM) &&
  2696. tg3_flag(tp, NVRAM_BUFFERED) &&
  2697. tg3_flag(tp, FLASH) &&
  2698. !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
  2699. (tp->nvram_jedecnum == JEDEC_ATMEL))
  2700. addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
  2701. tp->nvram_pagesize) +
  2702. (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
  2703. return addr;
  2704. }
  2705. /* NOTE: Data read in from NVRAM is byteswapped according to
  2706. * the byteswapping settings for all other register accesses.
  2707. * tg3 devices are BE devices, so on a BE machine, the data
  2708. * returned will be exactly as it is seen in NVRAM. On a LE
  2709. * machine, the 32-bit value will be byteswapped.
  2710. */
  2711. static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
  2712. {
  2713. int ret;
  2714. if (!tg3_flag(tp, NVRAM))
  2715. return tg3_nvram_read_using_eeprom(tp, offset, val);
  2716. offset = tg3_nvram_phys_addr(tp, offset);
  2717. if (offset > NVRAM_ADDR_MSK)
  2718. return -EINVAL;
  2719. ret = tg3_nvram_lock(tp);
  2720. if (ret)
  2721. return ret;
  2722. tg3_enable_nvram_access(tp);
  2723. tw32(NVRAM_ADDR, offset);
  2724. ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
  2725. NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
  2726. if (ret == 0)
  2727. *val = tr32(NVRAM_RDDATA);
  2728. tg3_disable_nvram_access(tp);
  2729. tg3_nvram_unlock(tp);
  2730. return ret;
  2731. }
  2732. /* Ensures NVRAM data is in bytestream format. */
  2733. static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
  2734. {
  2735. u32 v;
  2736. int res = tg3_nvram_read(tp, offset, &v);
  2737. if (!res)
  2738. *val = cpu_to_be32(v);
  2739. return res;
  2740. }
  2741. static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
  2742. u32 offset, u32 len, u8 *buf)
  2743. {
  2744. int i, j, rc = 0;
  2745. u32 val;
  2746. for (i = 0; i < len; i += 4) {
  2747. u32 addr;
  2748. __be32 data;
  2749. addr = offset + i;
  2750. memcpy(&data, buf + i, 4);
  2751. /*
  2752. * The SEEPROM interface expects the data to always be opposite
  2753. * the native endian format. We accomplish this by reversing
  2754. * all the operations that would have been performed on the
  2755. * data from a call to tg3_nvram_read_be32().
  2756. */
  2757. tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
  2758. val = tr32(GRC_EEPROM_ADDR);
  2759. tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
  2760. val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
  2761. EEPROM_ADDR_READ);
  2762. tw32(GRC_EEPROM_ADDR, val |
  2763. (0 << EEPROM_ADDR_DEVID_SHIFT) |
  2764. (addr & EEPROM_ADDR_ADDR_MASK) |
  2765. EEPROM_ADDR_START |
  2766. EEPROM_ADDR_WRITE);
  2767. for (j = 0; j < 1000; j++) {
  2768. val = tr32(GRC_EEPROM_ADDR);
  2769. if (val & EEPROM_ADDR_COMPLETE)
  2770. break;
  2771. msleep(1);
  2772. }
  2773. if (!(val & EEPROM_ADDR_COMPLETE)) {
  2774. rc = -EBUSY;
  2775. break;
  2776. }
  2777. }
  2778. return rc;
  2779. }
  2780. /* offset and length are dword aligned */
  2781. static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
  2782. u8 *buf)
  2783. {
  2784. int ret = 0;
  2785. u32 pagesize = tp->nvram_pagesize;
  2786. u32 pagemask = pagesize - 1;
  2787. u32 nvram_cmd;
  2788. u8 *tmp;
  2789. tmp = kmalloc(pagesize, GFP_KERNEL);
  2790. if (tmp == NULL)
  2791. return -ENOMEM;
  2792. while (len) {
  2793. int j;
  2794. u32 phy_addr, page_off, size;
  2795. phy_addr = offset & ~pagemask;
  2796. for (j = 0; j < pagesize; j += 4) {
  2797. ret = tg3_nvram_read_be32(tp, phy_addr + j,
  2798. (__be32 *) (tmp + j));
  2799. if (ret)
  2800. break;
  2801. }
  2802. if (ret)
  2803. break;
  2804. page_off = offset & pagemask;
  2805. size = pagesize;
  2806. if (len < size)
  2807. size = len;
  2808. len -= size;
  2809. memcpy(tmp + page_off, buf, size);
  2810. offset = offset + (pagesize - page_off);
  2811. tg3_enable_nvram_access(tp);
  2812. /*
  2813. * Before we can erase the flash page, we need
  2814. * to issue a special "write enable" command.
  2815. */
  2816. nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
  2817. if (tg3_nvram_exec_cmd(tp, nvram_cmd))
  2818. break;
  2819. /* Erase the target page */
  2820. tw32(NVRAM_ADDR, phy_addr);
  2821. nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
  2822. NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
  2823. if (tg3_nvram_exec_cmd(tp, nvram_cmd))
  2824. break;
  2825. /* Issue another write enable to start the write. */
  2826. nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
  2827. if (tg3_nvram_exec_cmd(tp, nvram_cmd))
  2828. break;
  2829. for (j = 0; j < pagesize; j += 4) {
  2830. __be32 data;
  2831. data = *((__be32 *) (tmp + j));
  2832. tw32(NVRAM_WRDATA, be32_to_cpu(data));
  2833. tw32(NVRAM_ADDR, phy_addr + j);
  2834. nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
  2835. NVRAM_CMD_WR;
  2836. if (j == 0)
  2837. nvram_cmd |= NVRAM_CMD_FIRST;
  2838. else if (j == (pagesize - 4))
  2839. nvram_cmd |= NVRAM_CMD_LAST;
  2840. ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
  2841. if (ret)
  2842. break;
  2843. }
  2844. if (ret)
  2845. break;
  2846. }
  2847. nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
  2848. tg3_nvram_exec_cmd(tp, nvram_cmd);
  2849. kfree(tmp);
  2850. return ret;
  2851. }
  2852. /* offset and length are dword aligned */
  2853. static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
  2854. u8 *buf)
  2855. {
  2856. int i, ret = 0;
  2857. for (i = 0; i < len; i += 4, offset += 4) {
  2858. u32 page_off, phy_addr, nvram_cmd;
  2859. __be32 data;
  2860. memcpy(&data, buf + i, 4);
  2861. tw32(NVRAM_WRDATA, be32_to_cpu(data));
  2862. page_off = offset % tp->nvram_pagesize;
  2863. phy_addr = tg3_nvram_phys_addr(tp, offset);
  2864. nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
  2865. if (page_off == 0 || i == 0)
  2866. nvram_cmd |= NVRAM_CMD_FIRST;
  2867. if (page_off == (tp->nvram_pagesize - 4))
  2868. nvram_cmd |= NVRAM_CMD_LAST;
  2869. if (i == (len - 4))
  2870. nvram_cmd |= NVRAM_CMD_LAST;
  2871. if ((nvram_cmd & NVRAM_CMD_FIRST) ||
  2872. !tg3_flag(tp, FLASH) ||
  2873. !tg3_flag(tp, 57765_PLUS))
  2874. tw32(NVRAM_ADDR, phy_addr);
  2875. if (tg3_asic_rev(tp) != ASIC_REV_5752 &&
  2876. !tg3_flag(tp, 5755_PLUS) &&
  2877. (tp->nvram_jedecnum == JEDEC_ST) &&
  2878. (nvram_cmd & NVRAM_CMD_FIRST)) {
  2879. u32 cmd;
  2880. cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
  2881. ret = tg3_nvram_exec_cmd(tp, cmd);
  2882. if (ret)
  2883. break;
  2884. }
  2885. if (!tg3_flag(tp, FLASH)) {
  2886. /* We always do complete word writes to eeprom. */
  2887. nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
  2888. }
  2889. ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
  2890. if (ret)
  2891. break;
  2892. }
  2893. return ret;
  2894. }
  2895. /* offset and length are dword aligned */
  2896. static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
  2897. {
  2898. int ret;
  2899. if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
  2900. tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
  2901. ~GRC_LCLCTRL_GPIO_OUTPUT1);
  2902. udelay(40);
  2903. }
  2904. if (!tg3_flag(tp, NVRAM)) {
  2905. ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
  2906. } else {
  2907. u32 grc_mode;
  2908. ret = tg3_nvram_lock(tp);
  2909. if (ret)
  2910. return ret;
  2911. tg3_enable_nvram_access(tp);
  2912. if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM))
  2913. tw32(NVRAM_WRITE1, 0x406);
  2914. grc_mode = tr32(GRC_MODE);
  2915. tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
  2916. if (tg3_flag(tp, NVRAM_BUFFERED) || !tg3_flag(tp, FLASH)) {
  2917. ret = tg3_nvram_write_block_buffered(tp, offset, len,
  2918. buf);
  2919. } else {
  2920. ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
  2921. buf);
  2922. }
  2923. grc_mode = tr32(GRC_MODE);
  2924. tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
  2925. tg3_disable_nvram_access(tp);
  2926. tg3_nvram_unlock(tp);
  2927. }
  2928. if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
  2929. tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
  2930. udelay(40);
  2931. }
  2932. return ret;
  2933. }
  2934. #define RX_CPU_SCRATCH_BASE 0x30000
  2935. #define RX_CPU_SCRATCH_SIZE 0x04000
  2936. #define TX_CPU_SCRATCH_BASE 0x34000
  2937. #define TX_CPU_SCRATCH_SIZE 0x04000
  2938. /* tp->lock is held. */
  2939. static int tg3_pause_cpu(struct tg3 *tp, u32 cpu_base)
  2940. {
  2941. int i;
  2942. const int iters = 10000;
  2943. for (i = 0; i < iters; i++) {
  2944. tw32(cpu_base + CPU_STATE, 0xffffffff);
  2945. tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
  2946. if (tr32(cpu_base + CPU_MODE) & CPU_MODE_HALT)
  2947. break;
  2948. if (pci_channel_offline(tp->pdev))
  2949. return -EBUSY;
  2950. }
  2951. return (i == iters) ? -EBUSY : 0;
  2952. }
  2953. /* tp->lock is held. */
  2954. static int tg3_rxcpu_pause(struct tg3 *tp)
  2955. {
  2956. int rc = tg3_pause_cpu(tp, RX_CPU_BASE);
  2957. tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
  2958. tw32_f(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
  2959. udelay(10);
  2960. return rc;
  2961. }
  2962. /* tp->lock is held. */
  2963. static int tg3_txcpu_pause(struct tg3 *tp)
  2964. {
  2965. return tg3_pause_cpu(tp, TX_CPU_BASE);
  2966. }
  2967. /* tp->lock is held. */
  2968. static void tg3_resume_cpu(struct tg3 *tp, u32 cpu_base)
  2969. {
  2970. tw32(cpu_base + CPU_STATE, 0xffffffff);
  2971. tw32_f(cpu_base + CPU_MODE, 0x00000000);
  2972. }
  2973. /* tp->lock is held. */
  2974. static void tg3_rxcpu_resume(struct tg3 *tp)
  2975. {
  2976. tg3_resume_cpu(tp, RX_CPU_BASE);
  2977. }
  2978. /* tp->lock is held. */
  2979. static int tg3_halt_cpu(struct tg3 *tp, u32 cpu_base)
  2980. {
  2981. int rc;
  2982. BUG_ON(cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS));
  2983. if (tg3_asic_rev(tp) == ASIC_REV_5906) {
  2984. u32 val = tr32(GRC_VCPU_EXT_CTRL);
  2985. tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
  2986. return 0;
  2987. }
  2988. if (cpu_base == RX_CPU_BASE) {
  2989. rc = tg3_rxcpu_pause(tp);
  2990. } else {
  2991. /*
  2992. * There is only an Rx CPU for the 5750 derivative in the
  2993. * BCM4785.
  2994. */
  2995. if (tg3_flag(tp, IS_SSB_CORE))
  2996. return 0;
  2997. rc = tg3_txcpu_pause(tp);
  2998. }
  2999. if (rc) {
  3000. netdev_err(tp->dev, "%s timed out, %s CPU\n",
  3001. __func__, cpu_base == RX_CPU_BASE ? "RX" : "TX");
  3002. return -ENODEV;
  3003. }
  3004. /* Clear firmware's nvram arbitration. */
  3005. if (tg3_flag(tp, NVRAM))
  3006. tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
  3007. return 0;
  3008. }
  3009. static int tg3_fw_data_len(struct tg3 *tp,
  3010. const struct tg3_firmware_hdr *fw_hdr)
  3011. {
  3012. int fw_len;
  3013. /* Non fragmented firmware have one firmware header followed by a
  3014. * contiguous chunk of data to be written. The length field in that
  3015. * header is not the length of data to be written but the complete
  3016. * length of the bss. The data length is determined based on
  3017. * tp->fw->size minus headers.
  3018. *
  3019. * Fragmented firmware have a main header followed by multiple
  3020. * fragments. Each fragment is identical to non fragmented firmware
  3021. * with a firmware header followed by a contiguous chunk of data. In
  3022. * the main header, the length field is unused and set to 0xffffffff.
  3023. * In each fragment header the length is the entire size of that
  3024. * fragment i.e. fragment data + header length. Data length is
  3025. * therefore length field in the header minus TG3_FW_HDR_LEN.
  3026. */
  3027. if (tp->fw_len == 0xffffffff)
  3028. fw_len = be32_to_cpu(fw_hdr->len);
  3029. else
  3030. fw_len = tp->fw->size;
  3031. return (fw_len - TG3_FW_HDR_LEN) / sizeof(u32);
  3032. }
  3033. /* tp->lock is held. */
  3034. static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base,
  3035. u32 cpu_scratch_base, int cpu_scratch_size,
  3036. const struct tg3_firmware_hdr *fw_hdr)
  3037. {
  3038. int err, i;
  3039. void (*write_op)(struct tg3 *, u32, u32);
  3040. int total_len = tp->fw->size;
  3041. if (cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS)) {
  3042. netdev_err(tp->dev,
  3043. "%s: Trying to load TX cpu firmware which is 5705\n",
  3044. __func__);
  3045. return -EINVAL;
  3046. }
  3047. if (tg3_flag(tp, 5705_PLUS) && tg3_asic_rev(tp) != ASIC_REV_57766)
  3048. write_op = tg3_write_mem;
  3049. else
  3050. write_op = tg3_write_indirect_reg32;
  3051. if (tg3_asic_rev(tp) != ASIC_REV_57766) {
  3052. /* It is possible that bootcode is still loading at this point.
  3053. * Get the nvram lock first before halting the cpu.
  3054. */
  3055. int lock_err = tg3_nvram_lock(tp);
  3056. err = tg3_halt_cpu(tp, cpu_base);
  3057. if (!lock_err)
  3058. tg3_nvram_unlock(tp);
  3059. if (err)
  3060. goto out;
  3061. for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
  3062. write_op(tp, cpu_scratch_base + i, 0);
  3063. tw32(cpu_base + CPU_STATE, 0xffffffff);
  3064. tw32(cpu_base + CPU_MODE,
  3065. tr32(cpu_base + CPU_MODE) | CPU_MODE_HALT);
  3066. } else {
  3067. /* Subtract additional main header for fragmented firmware and
  3068. * advance to the first fragment
  3069. */
  3070. total_len -= TG3_FW_HDR_LEN;
  3071. fw_hdr++;
  3072. }
  3073. do {
  3074. u32 *fw_data = (u32 *)(fw_hdr + 1);
  3075. for (i = 0; i < tg3_fw_data_len(tp, fw_hdr); i++)
  3076. write_op(tp, cpu_scratch_base +
  3077. (be32_to_cpu(fw_hdr->base_addr) & 0xffff) +
  3078. (i * sizeof(u32)),
  3079. be32_to_cpu(fw_data[i]));
  3080. total_len -= be32_to_cpu(fw_hdr->len);
  3081. /* Advance to next fragment */
  3082. fw_hdr = (struct tg3_firmware_hdr *)
  3083. ((void *)fw_hdr + be32_to_cpu(fw_hdr->len));
  3084. } while (total_len > 0);
  3085. err = 0;
  3086. out:
  3087. return err;
  3088. }
  3089. /* tp->lock is held. */
  3090. static int tg3_pause_cpu_and_set_pc(struct tg3 *tp, u32 cpu_base, u32 pc)
  3091. {
  3092. int i;
  3093. const int iters = 5;
  3094. tw32(cpu_base + CPU_STATE, 0xffffffff);
  3095. tw32_f(cpu_base + CPU_PC, pc);
  3096. for (i = 0; i < iters; i++) {
  3097. if (tr32(cpu_base + CPU_PC) == pc)
  3098. break;
  3099. tw32(cpu_base + CPU_STATE, 0xffffffff);
  3100. tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
  3101. tw32_f(cpu_base + CPU_PC, pc);
  3102. udelay(1000);
  3103. }
  3104. return (i == iters) ? -EBUSY : 0;
  3105. }
  3106. /* tp->lock is held. */
  3107. static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
  3108. {
  3109. const struct tg3_firmware_hdr *fw_hdr;
  3110. int err;
  3111. fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
  3112. /* Firmware blob starts with version numbers, followed by
  3113. start address and length. We are setting complete length.
  3114. length = end_address_of_bss - start_address_of_text.
  3115. Remainder is the blob to be loaded contiguously
  3116. from start address. */
  3117. err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
  3118. RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
  3119. fw_hdr);
  3120. if (err)
  3121. return err;
  3122. err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
  3123. TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
  3124. fw_hdr);
  3125. if (err)
  3126. return err;
  3127. /* Now startup only the RX cpu. */
  3128. err = tg3_pause_cpu_and_set_pc(tp, RX_CPU_BASE,
  3129. be32_to_cpu(fw_hdr->base_addr));
  3130. if (err) {
  3131. netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
  3132. "should be %08x\n", __func__,
  3133. tr32(RX_CPU_BASE + CPU_PC),
  3134. be32_to_cpu(fw_hdr->base_addr));
  3135. return -ENODEV;
  3136. }
  3137. tg3_rxcpu_resume(tp);
  3138. return 0;
  3139. }
  3140. static int tg3_validate_rxcpu_state(struct tg3 *tp)
  3141. {
  3142. const int iters = 1000;
  3143. int i;
  3144. u32 val;
  3145. /* Wait for boot code to complete initialization and enter service
  3146. * loop. It is then safe to download service patches
  3147. */
  3148. for (i = 0; i < iters; i++) {
  3149. if (tr32(RX_CPU_HWBKPT) == TG3_SBROM_IN_SERVICE_LOOP)
  3150. break;
  3151. udelay(10);
  3152. }
  3153. if (i == iters) {
  3154. netdev_err(tp->dev, "Boot code not ready for service patches\n");
  3155. return -EBUSY;
  3156. }
  3157. val = tg3_read_indirect_reg32(tp, TG3_57766_FW_HANDSHAKE);
  3158. if (val & 0xff) {
  3159. netdev_warn(tp->dev,
  3160. "Other patches exist. Not downloading EEE patch\n");
  3161. return -EEXIST;
  3162. }
  3163. return 0;
  3164. }
  3165. /* tp->lock is held. */
  3166. static void tg3_load_57766_firmware(struct tg3 *tp)
  3167. {
  3168. struct tg3_firmware_hdr *fw_hdr;
  3169. if (!tg3_flag(tp, NO_NVRAM))
  3170. return;
  3171. if (tg3_validate_rxcpu_state(tp))
  3172. return;
  3173. if (!tp->fw)
  3174. return;
  3175. /* This firmware blob has a different format than older firmware
  3176. * releases as given below. The main difference is we have fragmented
  3177. * data to be written to non-contiguous locations.
  3178. *
  3179. * In the beginning we have a firmware header identical to other
  3180. * firmware which consists of version, base addr and length. The length
  3181. * here is unused and set to 0xffffffff.
  3182. *
  3183. * This is followed by a series of firmware fragments which are
  3184. * individually identical to previous firmware. i.e. they have the
  3185. * firmware header and followed by data for that fragment. The version
  3186. * field of the individual fragment header is unused.
  3187. */
  3188. fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
  3189. if (be32_to_cpu(fw_hdr->base_addr) != TG3_57766_FW_BASE_ADDR)
  3190. return;
  3191. if (tg3_rxcpu_pause(tp))
  3192. return;
  3193. /* tg3_load_firmware_cpu() will always succeed for the 57766 */
  3194. tg3_load_firmware_cpu(tp, 0, TG3_57766_FW_BASE_ADDR, 0, fw_hdr);
  3195. tg3_rxcpu_resume(tp);
  3196. }
  3197. /* tp->lock is held. */
  3198. static int tg3_load_tso_firmware(struct tg3 *tp)
  3199. {
  3200. const struct tg3_firmware_hdr *fw_hdr;
  3201. unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
  3202. int err;
  3203. if (!tg3_flag(tp, FW_TSO))
  3204. return 0;
  3205. fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
  3206. /* Firmware blob starts with version numbers, followed by
  3207. start address and length. We are setting complete length.
  3208. length = end_address_of_bss - start_address_of_text.
  3209. Remainder is the blob to be loaded contiguously
  3210. from start address. */
  3211. cpu_scratch_size = tp->fw_len;
  3212. if (tg3_asic_rev(tp) == ASIC_REV_5705) {
  3213. cpu_base = RX_CPU_BASE;
  3214. cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
  3215. } else {
  3216. cpu_base = TX_CPU_BASE;
  3217. cpu_scratch_base = TX_CPU_SCRATCH_BASE;
  3218. cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
  3219. }
  3220. err = tg3_load_firmware_cpu(tp, cpu_base,
  3221. cpu_scratch_base, cpu_scratch_size,
  3222. fw_hdr);
  3223. if (err)
  3224. return err;
  3225. /* Now startup the cpu. */
  3226. err = tg3_pause_cpu_and_set_pc(tp, cpu_base,
  3227. be32_to_cpu(fw_hdr->base_addr));
  3228. if (err) {
  3229. netdev_err(tp->dev,
  3230. "%s fails to set CPU PC, is %08x should be %08x\n",
  3231. __func__, tr32(cpu_base + CPU_PC),
  3232. be32_to_cpu(fw_hdr->base_addr));
  3233. return -ENODEV;
  3234. }
  3235. tg3_resume_cpu(tp, cpu_base);
  3236. return 0;
  3237. }
  3238. /* tp->lock is held. */
  3239. static void __tg3_set_one_mac_addr(struct tg3 *tp, u8 *mac_addr, int index)
  3240. {
  3241. u32 addr_high, addr_low;
  3242. addr_high = ((mac_addr[0] << 8) | mac_addr[1]);
  3243. addr_low = ((mac_addr[2] << 24) | (mac_addr[3] << 16) |
  3244. (mac_addr[4] << 8) | mac_addr[5]);
  3245. if (index < 4) {
  3246. tw32(MAC_ADDR_0_HIGH + (index * 8), addr_high);
  3247. tw32(MAC_ADDR_0_LOW + (index * 8), addr_low);
  3248. } else {
  3249. index -= 4;
  3250. tw32(MAC_EXTADDR_0_HIGH + (index * 8), addr_high);
  3251. tw32(MAC_EXTADDR_0_LOW + (index * 8), addr_low);
  3252. }
  3253. }
  3254. /* tp->lock is held. */
  3255. static void __tg3_set_mac_addr(struct tg3 *tp, bool skip_mac_1)
  3256. {
  3257. u32 addr_high;
  3258. int i;
  3259. for (i = 0; i < 4; i++) {
  3260. if (i == 1 && skip_mac_1)
  3261. continue;
  3262. __tg3_set_one_mac_addr(tp, tp->dev->dev_addr, i);
  3263. }
  3264. if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
  3265. tg3_asic_rev(tp) == ASIC_REV_5704) {
  3266. for (i = 4; i < 16; i++)
  3267. __tg3_set_one_mac_addr(tp, tp->dev->dev_addr, i);
  3268. }
  3269. addr_high = (tp->dev->dev_addr[0] +
  3270. tp->dev->dev_addr[1] +
  3271. tp->dev->dev_addr[2] +
  3272. tp->dev->dev_addr[3] +
  3273. tp->dev->dev_addr[4] +
  3274. tp->dev->dev_addr[5]) &
  3275. TX_BACKOFF_SEED_MASK;
  3276. tw32(MAC_TX_BACKOFF_SEED, addr_high);
  3277. }
  3278. static void tg3_enable_register_access(struct tg3 *tp)
  3279. {
  3280. /*
  3281. * Make sure register accesses (indirect or otherwise) will function
  3282. * correctly.
  3283. */
  3284. pci_write_config_dword(tp->pdev,
  3285. TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
  3286. }
  3287. static int tg3_power_up(struct tg3 *tp)
  3288. {
  3289. int err;
  3290. tg3_enable_register_access(tp);
  3291. err = pci_set_power_state(tp->pdev, PCI_D0);
  3292. if (!err) {
  3293. /* Switch out of Vaux if it is a NIC */
  3294. tg3_pwrsrc_switch_to_vmain(tp);
  3295. } else {
  3296. netdev_err(tp->dev, "Transition to D0 failed\n");
  3297. }
  3298. return err;
  3299. }
  3300. static int tg3_setup_phy(struct tg3 *, bool);
  3301. static int tg3_power_down_prepare(struct tg3 *tp)
  3302. {
  3303. u32 misc_host_ctrl;
  3304. bool device_should_wake, do_low_power;
  3305. tg3_enable_register_access(tp);
  3306. /* Restore the CLKREQ setting. */
  3307. if (tg3_flag(tp, CLKREQ_BUG))
  3308. pcie_capability_set_word(tp->pdev, PCI_EXP_LNKCTL,
  3309. PCI_EXP_LNKCTL_CLKREQ_EN);
  3310. misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
  3311. tw32(TG3PCI_MISC_HOST_CTRL,
  3312. misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
  3313. device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
  3314. tg3_flag(tp, WOL_ENABLE);
  3315. if (tg3_flag(tp, USE_PHYLIB)) {
  3316. do_low_power = false;
  3317. if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
  3318. !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
  3319. struct phy_device *phydev;
  3320. u32 phyid, advertising;
  3321. phydev = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr);
  3322. tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
  3323. tp->link_config.speed = phydev->speed;
  3324. tp->link_config.duplex = phydev->duplex;
  3325. tp->link_config.autoneg = phydev->autoneg;
  3326. tp->link_config.advertising = phydev->advertising;
  3327. advertising = ADVERTISED_TP |
  3328. ADVERTISED_Pause |
  3329. ADVERTISED_Autoneg |
  3330. ADVERTISED_10baseT_Half;
  3331. if (tg3_flag(tp, ENABLE_ASF) || device_should_wake) {
  3332. if (tg3_flag(tp, WOL_SPEED_100MB))
  3333. advertising |=
  3334. ADVERTISED_100baseT_Half |
  3335. ADVERTISED_100baseT_Full |
  3336. ADVERTISED_10baseT_Full;
  3337. else
  3338. advertising |= ADVERTISED_10baseT_Full;
  3339. }
  3340. phydev->advertising = advertising;
  3341. phy_start_aneg(phydev);
  3342. phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
  3343. if (phyid != PHY_ID_BCMAC131) {
  3344. phyid &= PHY_BCM_OUI_MASK;
  3345. if (phyid == PHY_BCM_OUI_1 ||
  3346. phyid == PHY_BCM_OUI_2 ||
  3347. phyid == PHY_BCM_OUI_3)
  3348. do_low_power = true;
  3349. }
  3350. }
  3351. } else {
  3352. do_low_power = true;
  3353. if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER))
  3354. tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
  3355. if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
  3356. tg3_setup_phy(tp, false);
  3357. }
  3358. if (tg3_asic_rev(tp) == ASIC_REV_5906) {
  3359. u32 val;
  3360. val = tr32(GRC_VCPU_EXT_CTRL);
  3361. tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
  3362. } else if (!tg3_flag(tp, ENABLE_ASF)) {
  3363. int i;
  3364. u32 val;
  3365. for (i = 0; i < 200; i++) {
  3366. tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
  3367. if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
  3368. break;
  3369. msleep(1);
  3370. }
  3371. }
  3372. if (tg3_flag(tp, WOL_CAP))
  3373. tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
  3374. WOL_DRV_STATE_SHUTDOWN |
  3375. WOL_DRV_WOL |
  3376. WOL_SET_MAGIC_PKT);
  3377. if (device_should_wake) {
  3378. u32 mac_mode;
  3379. if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
  3380. if (do_low_power &&
  3381. !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
  3382. tg3_phy_auxctl_write(tp,
  3383. MII_TG3_AUXCTL_SHDWSEL_PWRCTL,
  3384. MII_TG3_AUXCTL_PCTL_WOL_EN |
  3385. MII_TG3_AUXCTL_PCTL_100TX_LPWR |
  3386. MII_TG3_AUXCTL_PCTL_CL_AB_TXDAC);
  3387. udelay(40);
  3388. }
  3389. if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
  3390. mac_mode = MAC_MODE_PORT_MODE_GMII;
  3391. else if (tp->phy_flags &
  3392. TG3_PHYFLG_KEEP_LINK_ON_PWRDN) {
  3393. if (tp->link_config.active_speed == SPEED_1000)
  3394. mac_mode = MAC_MODE_PORT_MODE_GMII;
  3395. else
  3396. mac_mode = MAC_MODE_PORT_MODE_MII;
  3397. } else
  3398. mac_mode = MAC_MODE_PORT_MODE_MII;
  3399. mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
  3400. if (tg3_asic_rev(tp) == ASIC_REV_5700) {
  3401. u32 speed = tg3_flag(tp, WOL_SPEED_100MB) ?
  3402. SPEED_100 : SPEED_10;
  3403. if (tg3_5700_link_polarity(tp, speed))
  3404. mac_mode |= MAC_MODE_LINK_POLARITY;
  3405. else
  3406. mac_mode &= ~MAC_MODE_LINK_POLARITY;
  3407. }
  3408. } else {
  3409. mac_mode = MAC_MODE_PORT_MODE_TBI;
  3410. }
  3411. if (!tg3_flag(tp, 5750_PLUS))
  3412. tw32(MAC_LED_CTRL, tp->led_ctrl);
  3413. mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
  3414. if ((tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS)) &&
  3415. (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)))
  3416. mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
  3417. if (tg3_flag(tp, ENABLE_APE))
  3418. mac_mode |= MAC_MODE_APE_TX_EN |
  3419. MAC_MODE_APE_RX_EN |
  3420. MAC_MODE_TDE_ENABLE;
  3421. tw32_f(MAC_MODE, mac_mode);
  3422. udelay(100);
  3423. tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
  3424. udelay(10);
  3425. }
  3426. if (!tg3_flag(tp, WOL_SPEED_100MB) &&
  3427. (tg3_asic_rev(tp) == ASIC_REV_5700 ||
  3428. tg3_asic_rev(tp) == ASIC_REV_5701)) {
  3429. u32 base_val;
  3430. base_val = tp->pci_clock_ctrl;
  3431. base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
  3432. CLOCK_CTRL_TXCLK_DISABLE);
  3433. tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
  3434. CLOCK_CTRL_PWRDOWN_PLL133, 40);
  3435. } else if (tg3_flag(tp, 5780_CLASS) ||
  3436. tg3_flag(tp, CPMU_PRESENT) ||
  3437. tg3_asic_rev(tp) == ASIC_REV_5906) {
  3438. /* do nothing */
  3439. } else if (!(tg3_flag(tp, 5750_PLUS) && tg3_flag(tp, ENABLE_ASF))) {
  3440. u32 newbits1, newbits2;
  3441. if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
  3442. tg3_asic_rev(tp) == ASIC_REV_5701) {
  3443. newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
  3444. CLOCK_CTRL_TXCLK_DISABLE |
  3445. CLOCK_CTRL_ALTCLK);
  3446. newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
  3447. } else if (tg3_flag(tp, 5705_PLUS)) {
  3448. newbits1 = CLOCK_CTRL_625_CORE;
  3449. newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
  3450. } else {
  3451. newbits1 = CLOCK_CTRL_ALTCLK;
  3452. newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
  3453. }
  3454. tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
  3455. 40);
  3456. tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
  3457. 40);
  3458. if (!tg3_flag(tp, 5705_PLUS)) {
  3459. u32 newbits3;
  3460. if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
  3461. tg3_asic_rev(tp) == ASIC_REV_5701) {
  3462. newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
  3463. CLOCK_CTRL_TXCLK_DISABLE |
  3464. CLOCK_CTRL_44MHZ_CORE);
  3465. } else {
  3466. newbits3 = CLOCK_CTRL_44MHZ_CORE;
  3467. }
  3468. tw32_wait_f(TG3PCI_CLOCK_CTRL,
  3469. tp->pci_clock_ctrl | newbits3, 40);
  3470. }
  3471. }
  3472. if (!(device_should_wake) && !tg3_flag(tp, ENABLE_ASF))
  3473. tg3_power_down_phy(tp, do_low_power);
  3474. tg3_frob_aux_power(tp, true);
  3475. /* Workaround for unstable PLL clock */
  3476. if ((!tg3_flag(tp, IS_SSB_CORE)) &&
  3477. ((tg3_chip_rev(tp) == CHIPREV_5750_AX) ||
  3478. (tg3_chip_rev(tp) == CHIPREV_5750_BX))) {
  3479. u32 val = tr32(0x7d00);
  3480. val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
  3481. tw32(0x7d00, val);
  3482. if (!tg3_flag(tp, ENABLE_ASF)) {
  3483. int err;
  3484. err = tg3_nvram_lock(tp);
  3485. tg3_halt_cpu(tp, RX_CPU_BASE);
  3486. if (!err)
  3487. tg3_nvram_unlock(tp);
  3488. }
  3489. }
  3490. tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
  3491. tg3_ape_driver_state_change(tp, RESET_KIND_SHUTDOWN);
  3492. return 0;
  3493. }
  3494. static void tg3_power_down(struct tg3 *tp)
  3495. {
  3496. pci_wake_from_d3(tp->pdev, tg3_flag(tp, WOL_ENABLE));
  3497. pci_set_power_state(tp->pdev, PCI_D3hot);
  3498. }
  3499. static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
  3500. {
  3501. switch (val & MII_TG3_AUX_STAT_SPDMASK) {
  3502. case MII_TG3_AUX_STAT_10HALF:
  3503. *speed = SPEED_10;
  3504. *duplex = DUPLEX_HALF;
  3505. break;
  3506. case MII_TG3_AUX_STAT_10FULL:
  3507. *speed = SPEED_10;
  3508. *duplex = DUPLEX_FULL;
  3509. break;
  3510. case MII_TG3_AUX_STAT_100HALF:
  3511. *speed = SPEED_100;
  3512. *duplex = DUPLEX_HALF;
  3513. break;
  3514. case MII_TG3_AUX_STAT_100FULL:
  3515. *speed = SPEED_100;
  3516. *duplex = DUPLEX_FULL;
  3517. break;
  3518. case MII_TG3_AUX_STAT_1000HALF:
  3519. *speed = SPEED_1000;
  3520. *duplex = DUPLEX_HALF;
  3521. break;
  3522. case MII_TG3_AUX_STAT_1000FULL:
  3523. *speed = SPEED_1000;
  3524. *duplex = DUPLEX_FULL;
  3525. break;
  3526. default:
  3527. if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
  3528. *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
  3529. SPEED_10;
  3530. *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
  3531. DUPLEX_HALF;
  3532. break;
  3533. }
  3534. *speed = SPEED_UNKNOWN;
  3535. *duplex = DUPLEX_UNKNOWN;
  3536. break;
  3537. }
  3538. }
  3539. static int tg3_phy_autoneg_cfg(struct tg3 *tp, u32 advertise, u32 flowctrl)
  3540. {
  3541. int err = 0;
  3542. u32 val, new_adv;
  3543. new_adv = ADVERTISE_CSMA;
  3544. new_adv |= ethtool_adv_to_mii_adv_t(advertise) & ADVERTISE_ALL;
  3545. new_adv |= mii_advertise_flowctrl(flowctrl);
  3546. err = tg3_writephy(tp, MII_ADVERTISE, new_adv);
  3547. if (err)
  3548. goto done;
  3549. if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
  3550. new_adv = ethtool_adv_to_mii_ctrl1000_t(advertise);
  3551. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
  3552. tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0)
  3553. new_adv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
  3554. err = tg3_writephy(tp, MII_CTRL1000, new_adv);
  3555. if (err)
  3556. goto done;
  3557. }
  3558. if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
  3559. goto done;
  3560. tw32(TG3_CPMU_EEE_MODE,
  3561. tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
  3562. err = tg3_phy_toggle_auxctl_smdsp(tp, true);
  3563. if (!err) {
  3564. u32 err2;
  3565. val = 0;
  3566. /* Advertise 100-BaseTX EEE ability */
  3567. if (advertise & ADVERTISED_100baseT_Full)
  3568. val |= MDIO_AN_EEE_ADV_100TX;
  3569. /* Advertise 1000-BaseT EEE ability */
  3570. if (advertise & ADVERTISED_1000baseT_Full)
  3571. val |= MDIO_AN_EEE_ADV_1000T;
  3572. if (!tp->eee.eee_enabled) {
  3573. val = 0;
  3574. tp->eee.advertised = 0;
  3575. } else {
  3576. tp->eee.advertised = advertise &
  3577. (ADVERTISED_100baseT_Full |
  3578. ADVERTISED_1000baseT_Full);
  3579. }
  3580. err = tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
  3581. if (err)
  3582. val = 0;
  3583. switch (tg3_asic_rev(tp)) {
  3584. case ASIC_REV_5717:
  3585. case ASIC_REV_57765:
  3586. case ASIC_REV_57766:
  3587. case ASIC_REV_5719:
  3588. /* If we advertised any eee advertisements above... */
  3589. if (val)
  3590. val = MII_TG3_DSP_TAP26_ALNOKO |
  3591. MII_TG3_DSP_TAP26_RMRXSTO |
  3592. MII_TG3_DSP_TAP26_OPCSINPT;
  3593. tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
  3594. /* Fall through */
  3595. case ASIC_REV_5720:
  3596. case ASIC_REV_5762:
  3597. if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
  3598. tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
  3599. MII_TG3_DSP_CH34TP2_HIBW01);
  3600. }
  3601. err2 = tg3_phy_toggle_auxctl_smdsp(tp, false);
  3602. if (!err)
  3603. err = err2;
  3604. }
  3605. done:
  3606. return err;
  3607. }
  3608. static void tg3_phy_copper_begin(struct tg3 *tp)
  3609. {
  3610. if (tp->link_config.autoneg == AUTONEG_ENABLE ||
  3611. (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
  3612. u32 adv, fc;
  3613. if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
  3614. !(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN)) {
  3615. adv = ADVERTISED_10baseT_Half |
  3616. ADVERTISED_10baseT_Full;
  3617. if (tg3_flag(tp, WOL_SPEED_100MB))
  3618. adv |= ADVERTISED_100baseT_Half |
  3619. ADVERTISED_100baseT_Full;
  3620. if (tp->phy_flags & TG3_PHYFLG_1G_ON_VAUX_OK) {
  3621. if (!(tp->phy_flags &
  3622. TG3_PHYFLG_DISABLE_1G_HD_ADV))
  3623. adv |= ADVERTISED_1000baseT_Half;
  3624. adv |= ADVERTISED_1000baseT_Full;
  3625. }
  3626. fc = FLOW_CTRL_TX | FLOW_CTRL_RX;
  3627. } else {
  3628. adv = tp->link_config.advertising;
  3629. if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
  3630. adv &= ~(ADVERTISED_1000baseT_Half |
  3631. ADVERTISED_1000baseT_Full);
  3632. fc = tp->link_config.flowctrl;
  3633. }
  3634. tg3_phy_autoneg_cfg(tp, adv, fc);
  3635. if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
  3636. (tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN)) {
  3637. /* Normally during power down we want to autonegotiate
  3638. * the lowest possible speed for WOL. However, to avoid
  3639. * link flap, we leave it untouched.
  3640. */
  3641. return;
  3642. }
  3643. tg3_writephy(tp, MII_BMCR,
  3644. BMCR_ANENABLE | BMCR_ANRESTART);
  3645. } else {
  3646. int i;
  3647. u32 bmcr, orig_bmcr;
  3648. tp->link_config.active_speed = tp->link_config.speed;
  3649. tp->link_config.active_duplex = tp->link_config.duplex;
  3650. if (tg3_asic_rev(tp) == ASIC_REV_5714) {
  3651. /* With autoneg disabled, 5715 only links up when the
  3652. * advertisement register has the configured speed
  3653. * enabled.
  3654. */
  3655. tg3_writephy(tp, MII_ADVERTISE, ADVERTISE_ALL);
  3656. }
  3657. bmcr = 0;
  3658. switch (tp->link_config.speed) {
  3659. default:
  3660. case SPEED_10:
  3661. break;
  3662. case SPEED_100:
  3663. bmcr |= BMCR_SPEED100;
  3664. break;
  3665. case SPEED_1000:
  3666. bmcr |= BMCR_SPEED1000;
  3667. break;
  3668. }
  3669. if (tp->link_config.duplex == DUPLEX_FULL)
  3670. bmcr |= BMCR_FULLDPLX;
  3671. if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
  3672. (bmcr != orig_bmcr)) {
  3673. tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
  3674. for (i = 0; i < 1500; i++) {
  3675. u32 tmp;
  3676. udelay(10);
  3677. if (tg3_readphy(tp, MII_BMSR, &tmp) ||
  3678. tg3_readphy(tp, MII_BMSR, &tmp))
  3679. continue;
  3680. if (!(tmp & BMSR_LSTATUS)) {
  3681. udelay(40);
  3682. break;
  3683. }
  3684. }
  3685. tg3_writephy(tp, MII_BMCR, bmcr);
  3686. udelay(40);
  3687. }
  3688. }
  3689. }
  3690. static int tg3_phy_pull_config(struct tg3 *tp)
  3691. {
  3692. int err;
  3693. u32 val;
  3694. err = tg3_readphy(tp, MII_BMCR, &val);
  3695. if (err)
  3696. goto done;
  3697. if (!(val & BMCR_ANENABLE)) {
  3698. tp->link_config.autoneg = AUTONEG_DISABLE;
  3699. tp->link_config.advertising = 0;
  3700. tg3_flag_clear(tp, PAUSE_AUTONEG);
  3701. err = -EIO;
  3702. switch (val & (BMCR_SPEED1000 | BMCR_SPEED100)) {
  3703. case 0:
  3704. if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
  3705. goto done;
  3706. tp->link_config.speed = SPEED_10;
  3707. break;
  3708. case BMCR_SPEED100:
  3709. if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
  3710. goto done;
  3711. tp->link_config.speed = SPEED_100;
  3712. break;
  3713. case BMCR_SPEED1000:
  3714. if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
  3715. tp->link_config.speed = SPEED_1000;
  3716. break;
  3717. }
  3718. /* Fall through */
  3719. default:
  3720. goto done;
  3721. }
  3722. if (val & BMCR_FULLDPLX)
  3723. tp->link_config.duplex = DUPLEX_FULL;
  3724. else
  3725. tp->link_config.duplex = DUPLEX_HALF;
  3726. tp->link_config.flowctrl = FLOW_CTRL_RX | FLOW_CTRL_TX;
  3727. err = 0;
  3728. goto done;
  3729. }
  3730. tp->link_config.autoneg = AUTONEG_ENABLE;
  3731. tp->link_config.advertising = ADVERTISED_Autoneg;
  3732. tg3_flag_set(tp, PAUSE_AUTONEG);
  3733. if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
  3734. u32 adv;
  3735. err = tg3_readphy(tp, MII_ADVERTISE, &val);
  3736. if (err)
  3737. goto done;
  3738. adv = mii_adv_to_ethtool_adv_t(val & ADVERTISE_ALL);
  3739. tp->link_config.advertising |= adv | ADVERTISED_TP;
  3740. tp->link_config.flowctrl = tg3_decode_flowctrl_1000T(val);
  3741. } else {
  3742. tp->link_config.advertising |= ADVERTISED_FIBRE;
  3743. }
  3744. if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
  3745. u32 adv;
  3746. if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
  3747. err = tg3_readphy(tp, MII_CTRL1000, &val);
  3748. if (err)
  3749. goto done;
  3750. adv = mii_ctrl1000_to_ethtool_adv_t(val);
  3751. } else {
  3752. err = tg3_readphy(tp, MII_ADVERTISE, &val);
  3753. if (err)
  3754. goto done;
  3755. adv = tg3_decode_flowctrl_1000X(val);
  3756. tp->link_config.flowctrl = adv;
  3757. val &= (ADVERTISE_1000XHALF | ADVERTISE_1000XFULL);
  3758. adv = mii_adv_to_ethtool_adv_x(val);
  3759. }
  3760. tp->link_config.advertising |= adv;
  3761. }
  3762. done:
  3763. return err;
  3764. }
  3765. static int tg3_init_5401phy_dsp(struct tg3 *tp)
  3766. {
  3767. int err;
  3768. /* Turn off tap power management. */
  3769. /* Set Extended packet length bit */
  3770. err = tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
  3771. err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
  3772. err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
  3773. err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
  3774. err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
  3775. err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
  3776. udelay(40);
  3777. return err;
  3778. }
  3779. static bool tg3_phy_eee_config_ok(struct tg3 *tp)
  3780. {
  3781. struct ethtool_eee eee;
  3782. if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
  3783. return true;
  3784. tg3_eee_pull_config(tp, &eee);
  3785. if (tp->eee.eee_enabled) {
  3786. if (tp->eee.advertised != eee.advertised ||
  3787. tp->eee.tx_lpi_timer != eee.tx_lpi_timer ||
  3788. tp->eee.tx_lpi_enabled != eee.tx_lpi_enabled)
  3789. return false;
  3790. } else {
  3791. /* EEE is disabled but we're advertising */
  3792. if (eee.advertised)
  3793. return false;
  3794. }
  3795. return true;
  3796. }
  3797. static bool tg3_phy_copper_an_config_ok(struct tg3 *tp, u32 *lcladv)
  3798. {
  3799. u32 advmsk, tgtadv, advertising;
  3800. advertising = tp->link_config.advertising;
  3801. tgtadv = ethtool_adv_to_mii_adv_t(advertising) & ADVERTISE_ALL;
  3802. advmsk = ADVERTISE_ALL;
  3803. if (tp->link_config.active_duplex == DUPLEX_FULL) {
  3804. tgtadv |= mii_advertise_flowctrl(tp->link_config.flowctrl);
  3805. advmsk |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
  3806. }
  3807. if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
  3808. return false;
  3809. if ((*lcladv & advmsk) != tgtadv)
  3810. return false;
  3811. if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
  3812. u32 tg3_ctrl;
  3813. tgtadv = ethtool_adv_to_mii_ctrl1000_t(advertising);
  3814. if (tg3_readphy(tp, MII_CTRL1000, &tg3_ctrl))
  3815. return false;
  3816. if (tgtadv &&
  3817. (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
  3818. tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0)) {
  3819. tgtadv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
  3820. tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL |
  3821. CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
  3822. } else {
  3823. tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL);
  3824. }
  3825. if (tg3_ctrl != tgtadv)
  3826. return false;
  3827. }
  3828. return true;
  3829. }
  3830. static bool tg3_phy_copper_fetch_rmtadv(struct tg3 *tp, u32 *rmtadv)
  3831. {
  3832. u32 lpeth = 0;
  3833. if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
  3834. u32 val;
  3835. if (tg3_readphy(tp, MII_STAT1000, &val))
  3836. return false;
  3837. lpeth = mii_stat1000_to_ethtool_lpa_t(val);
  3838. }
  3839. if (tg3_readphy(tp, MII_LPA, rmtadv))
  3840. return false;
  3841. lpeth |= mii_lpa_to_ethtool_lpa_t(*rmtadv);
  3842. tp->link_config.rmt_adv = lpeth;
  3843. return true;
  3844. }
  3845. static bool tg3_test_and_report_link_chg(struct tg3 *tp, bool curr_link_up)
  3846. {
  3847. if (curr_link_up != tp->link_up) {
  3848. if (curr_link_up) {
  3849. netif_carrier_on(tp->dev);
  3850. } else {
  3851. netif_carrier_off(tp->dev);
  3852. if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
  3853. tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
  3854. }
  3855. tg3_link_report(tp);
  3856. return true;
  3857. }
  3858. return false;
  3859. }
  3860. static void tg3_clear_mac_status(struct tg3 *tp)
  3861. {
  3862. tw32(MAC_EVENT, 0);
  3863. tw32_f(MAC_STATUS,
  3864. MAC_STATUS_SYNC_CHANGED |
  3865. MAC_STATUS_CFG_CHANGED |
  3866. MAC_STATUS_MI_COMPLETION |
  3867. MAC_STATUS_LNKSTATE_CHANGED);
  3868. udelay(40);
  3869. }
  3870. static void tg3_setup_eee(struct tg3 *tp)
  3871. {
  3872. u32 val;
  3873. val = TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
  3874. TG3_CPMU_EEE_LNKIDL_UART_IDL;
  3875. if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0)
  3876. val |= TG3_CPMU_EEE_LNKIDL_APE_TX_MT;
  3877. tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL, val);
  3878. tw32_f(TG3_CPMU_EEE_CTRL,
  3879. TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
  3880. val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
  3881. (tp->eee.tx_lpi_enabled ? TG3_CPMU_EEEMD_LPI_IN_TX : 0) |
  3882. TG3_CPMU_EEEMD_LPI_IN_RX |
  3883. TG3_CPMU_EEEMD_EEE_ENABLE;
  3884. if (tg3_asic_rev(tp) != ASIC_REV_5717)
  3885. val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
  3886. if (tg3_flag(tp, ENABLE_APE))
  3887. val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
  3888. tw32_f(TG3_CPMU_EEE_MODE, tp->eee.eee_enabled ? val : 0);
  3889. tw32_f(TG3_CPMU_EEE_DBTMR1,
  3890. TG3_CPMU_DBTMR1_PCIEXIT_2047US |
  3891. (tp->eee.tx_lpi_timer & 0xffff));
  3892. tw32_f(TG3_CPMU_EEE_DBTMR2,
  3893. TG3_CPMU_DBTMR2_APE_TX_2047US |
  3894. TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
  3895. }
  3896. static int tg3_setup_copper_phy(struct tg3 *tp, bool force_reset)
  3897. {
  3898. bool current_link_up;
  3899. u32 bmsr, val;
  3900. u32 lcl_adv, rmt_adv;
  3901. u16 current_speed;
  3902. u8 current_duplex;
  3903. int i, err;
  3904. tg3_clear_mac_status(tp);
  3905. if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
  3906. tw32_f(MAC_MI_MODE,
  3907. (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
  3908. udelay(80);
  3909. }
  3910. tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, 0);
  3911. /* Some third-party PHYs need to be reset on link going
  3912. * down.
  3913. */
  3914. if ((tg3_asic_rev(tp) == ASIC_REV_5703 ||
  3915. tg3_asic_rev(tp) == ASIC_REV_5704 ||
  3916. tg3_asic_rev(tp) == ASIC_REV_5705) &&
  3917. tp->link_up) {
  3918. tg3_readphy(tp, MII_BMSR, &bmsr);
  3919. if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
  3920. !(bmsr & BMSR_LSTATUS))
  3921. force_reset = true;
  3922. }
  3923. if (force_reset)
  3924. tg3_phy_reset(tp);
  3925. if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
  3926. tg3_readphy(tp, MII_BMSR, &bmsr);
  3927. if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
  3928. !tg3_flag(tp, INIT_COMPLETE))
  3929. bmsr = 0;
  3930. if (!(bmsr & BMSR_LSTATUS)) {
  3931. err = tg3_init_5401phy_dsp(tp);
  3932. if (err)
  3933. return err;
  3934. tg3_readphy(tp, MII_BMSR, &bmsr);
  3935. for (i = 0; i < 1000; i++) {
  3936. udelay(10);
  3937. if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
  3938. (bmsr & BMSR_LSTATUS)) {
  3939. udelay(40);
  3940. break;
  3941. }
  3942. }
  3943. if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
  3944. TG3_PHY_REV_BCM5401_B0 &&
  3945. !(bmsr & BMSR_LSTATUS) &&
  3946. tp->link_config.active_speed == SPEED_1000) {
  3947. err = tg3_phy_reset(tp);
  3948. if (!err)
  3949. err = tg3_init_5401phy_dsp(tp);
  3950. if (err)
  3951. return err;
  3952. }
  3953. }
  3954. } else if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
  3955. tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0) {
  3956. /* 5701 {A0,B0} CRC bug workaround */
  3957. tg3_writephy(tp, 0x15, 0x0a75);
  3958. tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
  3959. tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
  3960. tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
  3961. }
  3962. /* Clear pending interrupts... */
  3963. tg3_readphy(tp, MII_TG3_ISTAT, &val);
  3964. tg3_readphy(tp, MII_TG3_ISTAT, &val);
  3965. if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
  3966. tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
  3967. else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
  3968. tg3_writephy(tp, MII_TG3_IMASK, ~0);
  3969. if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
  3970. tg3_asic_rev(tp) == ASIC_REV_5701) {
  3971. if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
  3972. tg3_writephy(tp, MII_TG3_EXT_CTRL,
  3973. MII_TG3_EXT_CTRL_LNK3_LED_MODE);
  3974. else
  3975. tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
  3976. }
  3977. current_link_up = false;
  3978. current_speed = SPEED_UNKNOWN;
  3979. current_duplex = DUPLEX_UNKNOWN;
  3980. tp->phy_flags &= ~TG3_PHYFLG_MDIX_STATE;
  3981. tp->link_config.rmt_adv = 0;
  3982. if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
  3983. err = tg3_phy_auxctl_read(tp,
  3984. MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
  3985. &val);
  3986. if (!err && !(val & (1 << 10))) {
  3987. tg3_phy_auxctl_write(tp,
  3988. MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
  3989. val | (1 << 10));
  3990. goto relink;
  3991. }
  3992. }
  3993. bmsr = 0;
  3994. for (i = 0; i < 100; i++) {
  3995. tg3_readphy(tp, MII_BMSR, &bmsr);
  3996. if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
  3997. (bmsr & BMSR_LSTATUS))
  3998. break;
  3999. udelay(40);
  4000. }
  4001. if (bmsr & BMSR_LSTATUS) {
  4002. u32 aux_stat, bmcr;
  4003. tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
  4004. for (i = 0; i < 2000; i++) {
  4005. udelay(10);
  4006. if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
  4007. aux_stat)
  4008. break;
  4009. }
  4010. tg3_aux_stat_to_speed_duplex(tp, aux_stat,
  4011. &current_speed,
  4012. &current_duplex);
  4013. bmcr = 0;
  4014. for (i = 0; i < 200; i++) {
  4015. tg3_readphy(tp, MII_BMCR, &bmcr);
  4016. if (tg3_readphy(tp, MII_BMCR, &bmcr))
  4017. continue;
  4018. if (bmcr && bmcr != 0x7fff)
  4019. break;
  4020. udelay(10);
  4021. }
  4022. lcl_adv = 0;
  4023. rmt_adv = 0;
  4024. tp->link_config.active_speed = current_speed;
  4025. tp->link_config.active_duplex = current_duplex;
  4026. if (tp->link_config.autoneg == AUTONEG_ENABLE) {
  4027. bool eee_config_ok = tg3_phy_eee_config_ok(tp);
  4028. if ((bmcr & BMCR_ANENABLE) &&
  4029. eee_config_ok &&
  4030. tg3_phy_copper_an_config_ok(tp, &lcl_adv) &&
  4031. tg3_phy_copper_fetch_rmtadv(tp, &rmt_adv))
  4032. current_link_up = true;
  4033. /* EEE settings changes take effect only after a phy
  4034. * reset. If we have skipped a reset due to Link Flap
  4035. * Avoidance being enabled, do it now.
  4036. */
  4037. if (!eee_config_ok &&
  4038. (tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN) &&
  4039. !force_reset) {
  4040. tg3_setup_eee(tp);
  4041. tg3_phy_reset(tp);
  4042. }
  4043. } else {
  4044. if (!(bmcr & BMCR_ANENABLE) &&
  4045. tp->link_config.speed == current_speed &&
  4046. tp->link_config.duplex == current_duplex) {
  4047. current_link_up = true;
  4048. }
  4049. }
  4050. if (current_link_up &&
  4051. tp->link_config.active_duplex == DUPLEX_FULL) {
  4052. u32 reg, bit;
  4053. if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
  4054. reg = MII_TG3_FET_GEN_STAT;
  4055. bit = MII_TG3_FET_GEN_STAT_MDIXSTAT;
  4056. } else {
  4057. reg = MII_TG3_EXT_STAT;
  4058. bit = MII_TG3_EXT_STAT_MDIX;
  4059. }
  4060. if (!tg3_readphy(tp, reg, &val) && (val & bit))
  4061. tp->phy_flags |= TG3_PHYFLG_MDIX_STATE;
  4062. tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
  4063. }
  4064. }
  4065. relink:
  4066. if (!current_link_up || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
  4067. tg3_phy_copper_begin(tp);
  4068. if (tg3_flag(tp, ROBOSWITCH)) {
  4069. current_link_up = true;
  4070. /* FIXME: when BCM5325 switch is used use 100 MBit/s */
  4071. current_speed = SPEED_1000;
  4072. current_duplex = DUPLEX_FULL;
  4073. tp->link_config.active_speed = current_speed;
  4074. tp->link_config.active_duplex = current_duplex;
  4075. }
  4076. tg3_readphy(tp, MII_BMSR, &bmsr);
  4077. if ((!tg3_readphy(tp, MII_BMSR, &bmsr) && (bmsr & BMSR_LSTATUS)) ||
  4078. (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
  4079. current_link_up = true;
  4080. }
  4081. tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
  4082. if (current_link_up) {
  4083. if (tp->link_config.active_speed == SPEED_100 ||
  4084. tp->link_config.active_speed == SPEED_10)
  4085. tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
  4086. else
  4087. tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
  4088. } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
  4089. tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
  4090. else
  4091. tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
  4092. /* In order for the 5750 core in BCM4785 chip to work properly
  4093. * in RGMII mode, the Led Control Register must be set up.
  4094. */
  4095. if (tg3_flag(tp, RGMII_MODE)) {
  4096. u32 led_ctrl = tr32(MAC_LED_CTRL);
  4097. led_ctrl &= ~(LED_CTRL_1000MBPS_ON | LED_CTRL_100MBPS_ON);
  4098. if (tp->link_config.active_speed == SPEED_10)
  4099. led_ctrl |= LED_CTRL_LNKLED_OVERRIDE;
  4100. else if (tp->link_config.active_speed == SPEED_100)
  4101. led_ctrl |= (LED_CTRL_LNKLED_OVERRIDE |
  4102. LED_CTRL_100MBPS_ON);
  4103. else if (tp->link_config.active_speed == SPEED_1000)
  4104. led_ctrl |= (LED_CTRL_LNKLED_OVERRIDE |
  4105. LED_CTRL_1000MBPS_ON);
  4106. tw32(MAC_LED_CTRL, led_ctrl);
  4107. udelay(40);
  4108. }
  4109. tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
  4110. if (tp->link_config.active_duplex == DUPLEX_HALF)
  4111. tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
  4112. if (tg3_asic_rev(tp) == ASIC_REV_5700) {
  4113. if (current_link_up &&
  4114. tg3_5700_link_polarity(tp, tp->link_config.active_speed))
  4115. tp->mac_mode |= MAC_MODE_LINK_POLARITY;
  4116. else
  4117. tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
  4118. }
  4119. /* ??? Without this setting Netgear GA302T PHY does not
  4120. * ??? send/receive packets...
  4121. */
  4122. if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
  4123. tg3_chip_rev_id(tp) == CHIPREV_ID_5700_ALTIMA) {
  4124. tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
  4125. tw32_f(MAC_MI_MODE, tp->mi_mode);
  4126. udelay(80);
  4127. }
  4128. tw32_f(MAC_MODE, tp->mac_mode);
  4129. udelay(40);
  4130. tg3_phy_eee_adjust(tp, current_link_up);
  4131. if (tg3_flag(tp, USE_LINKCHG_REG)) {
  4132. /* Polled via timer. */
  4133. tw32_f(MAC_EVENT, 0);
  4134. } else {
  4135. tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
  4136. }
  4137. udelay(40);
  4138. if (tg3_asic_rev(tp) == ASIC_REV_5700 &&
  4139. current_link_up &&
  4140. tp->link_config.active_speed == SPEED_1000 &&
  4141. (tg3_flag(tp, PCIX_MODE) || tg3_flag(tp, PCI_HIGH_SPEED))) {
  4142. udelay(120);
  4143. tw32_f(MAC_STATUS,
  4144. (MAC_STATUS_SYNC_CHANGED |
  4145. MAC_STATUS_CFG_CHANGED));
  4146. udelay(40);
  4147. tg3_write_mem(tp,
  4148. NIC_SRAM_FIRMWARE_MBOX,
  4149. NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
  4150. }
  4151. /* Prevent send BD corruption. */
  4152. if (tg3_flag(tp, CLKREQ_BUG)) {
  4153. if (tp->link_config.active_speed == SPEED_100 ||
  4154. tp->link_config.active_speed == SPEED_10)
  4155. pcie_capability_clear_word(tp->pdev, PCI_EXP_LNKCTL,
  4156. PCI_EXP_LNKCTL_CLKREQ_EN);
  4157. else
  4158. pcie_capability_set_word(tp->pdev, PCI_EXP_LNKCTL,
  4159. PCI_EXP_LNKCTL_CLKREQ_EN);
  4160. }
  4161. tg3_test_and_report_link_chg(tp, current_link_up);
  4162. return 0;
  4163. }
  4164. struct tg3_fiber_aneginfo {
  4165. int state;
  4166. #define ANEG_STATE_UNKNOWN 0
  4167. #define ANEG_STATE_AN_ENABLE 1
  4168. #define ANEG_STATE_RESTART_INIT 2
  4169. #define ANEG_STATE_RESTART 3
  4170. #define ANEG_STATE_DISABLE_LINK_OK 4
  4171. #define ANEG_STATE_ABILITY_DETECT_INIT 5
  4172. #define ANEG_STATE_ABILITY_DETECT 6
  4173. #define ANEG_STATE_ACK_DETECT_INIT 7
  4174. #define ANEG_STATE_ACK_DETECT 8
  4175. #define ANEG_STATE_COMPLETE_ACK_INIT 9
  4176. #define ANEG_STATE_COMPLETE_ACK 10
  4177. #define ANEG_STATE_IDLE_DETECT_INIT 11
  4178. #define ANEG_STATE_IDLE_DETECT 12
  4179. #define ANEG_STATE_LINK_OK 13
  4180. #define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
  4181. #define ANEG_STATE_NEXT_PAGE_WAIT 15
  4182. u32 flags;
  4183. #define MR_AN_ENABLE 0x00000001
  4184. #define MR_RESTART_AN 0x00000002
  4185. #define MR_AN_COMPLETE 0x00000004
  4186. #define MR_PAGE_RX 0x00000008
  4187. #define MR_NP_LOADED 0x00000010
  4188. #define MR_TOGGLE_TX 0x00000020
  4189. #define MR_LP_ADV_FULL_DUPLEX 0x00000040
  4190. #define MR_LP_ADV_HALF_DUPLEX 0x00000080
  4191. #define MR_LP_ADV_SYM_PAUSE 0x00000100
  4192. #define MR_LP_ADV_ASYM_PAUSE 0x00000200
  4193. #define MR_LP_ADV_REMOTE_FAULT1 0x00000400
  4194. #define MR_LP_ADV_REMOTE_FAULT2 0x00000800
  4195. #define MR_LP_ADV_NEXT_PAGE 0x00001000
  4196. #define MR_TOGGLE_RX 0x00002000
  4197. #define MR_NP_RX 0x00004000
  4198. #define MR_LINK_OK 0x80000000
  4199. unsigned long link_time, cur_time;
  4200. u32 ability_match_cfg;
  4201. int ability_match_count;
  4202. char ability_match, idle_match, ack_match;
  4203. u32 txconfig, rxconfig;
  4204. #define ANEG_CFG_NP 0x00000080
  4205. #define ANEG_CFG_ACK 0x00000040
  4206. #define ANEG_CFG_RF2 0x00000020
  4207. #define ANEG_CFG_RF1 0x00000010
  4208. #define ANEG_CFG_PS2 0x00000001
  4209. #define ANEG_CFG_PS1 0x00008000
  4210. #define ANEG_CFG_HD 0x00004000
  4211. #define ANEG_CFG_FD 0x00002000
  4212. #define ANEG_CFG_INVAL 0x00001f06
  4213. };
  4214. #define ANEG_OK 0
  4215. #define ANEG_DONE 1
  4216. #define ANEG_TIMER_ENAB 2
  4217. #define ANEG_FAILED -1
  4218. #define ANEG_STATE_SETTLE_TIME 10000
  4219. static int tg3_fiber_aneg_smachine(struct tg3 *tp,
  4220. struct tg3_fiber_aneginfo *ap)
  4221. {
  4222. u16 flowctrl;
  4223. unsigned long delta;
  4224. u32 rx_cfg_reg;
  4225. int ret;
  4226. if (ap->state == ANEG_STATE_UNKNOWN) {
  4227. ap->rxconfig = 0;
  4228. ap->link_time = 0;
  4229. ap->cur_time = 0;
  4230. ap->ability_match_cfg = 0;
  4231. ap->ability_match_count = 0;
  4232. ap->ability_match = 0;
  4233. ap->idle_match = 0;
  4234. ap->ack_match = 0;
  4235. }
  4236. ap->cur_time++;
  4237. if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
  4238. rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
  4239. if (rx_cfg_reg != ap->ability_match_cfg) {
  4240. ap->ability_match_cfg = rx_cfg_reg;
  4241. ap->ability_match = 0;
  4242. ap->ability_match_count = 0;
  4243. } else {
  4244. if (++ap->ability_match_count > 1) {
  4245. ap->ability_match = 1;
  4246. ap->ability_match_cfg = rx_cfg_reg;
  4247. }
  4248. }
  4249. if (rx_cfg_reg & ANEG_CFG_ACK)
  4250. ap->ack_match = 1;
  4251. else
  4252. ap->ack_match = 0;
  4253. ap->idle_match = 0;
  4254. } else {
  4255. ap->idle_match = 1;
  4256. ap->ability_match_cfg = 0;
  4257. ap->ability_match_count = 0;
  4258. ap->ability_match = 0;
  4259. ap->ack_match = 0;
  4260. rx_cfg_reg = 0;
  4261. }
  4262. ap->rxconfig = rx_cfg_reg;
  4263. ret = ANEG_OK;
  4264. switch (ap->state) {
  4265. case ANEG_STATE_UNKNOWN:
  4266. if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
  4267. ap->state = ANEG_STATE_AN_ENABLE;
  4268. /* fallthru */
  4269. case ANEG_STATE_AN_ENABLE:
  4270. ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
  4271. if (ap->flags & MR_AN_ENABLE) {
  4272. ap->link_time = 0;
  4273. ap->cur_time = 0;
  4274. ap->ability_match_cfg = 0;
  4275. ap->ability_match_count = 0;
  4276. ap->ability_match = 0;
  4277. ap->idle_match = 0;
  4278. ap->ack_match = 0;
  4279. ap->state = ANEG_STATE_RESTART_INIT;
  4280. } else {
  4281. ap->state = ANEG_STATE_DISABLE_LINK_OK;
  4282. }
  4283. break;
  4284. case ANEG_STATE_RESTART_INIT:
  4285. ap->link_time = ap->cur_time;
  4286. ap->flags &= ~(MR_NP_LOADED);
  4287. ap->txconfig = 0;
  4288. tw32(MAC_TX_AUTO_NEG, 0);
  4289. tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
  4290. tw32_f(MAC_MODE, tp->mac_mode);
  4291. udelay(40);
  4292. ret = ANEG_TIMER_ENAB;
  4293. ap->state = ANEG_STATE_RESTART;
  4294. /* fallthru */
  4295. case ANEG_STATE_RESTART:
  4296. delta = ap->cur_time - ap->link_time;
  4297. if (delta > ANEG_STATE_SETTLE_TIME)
  4298. ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
  4299. else
  4300. ret = ANEG_TIMER_ENAB;
  4301. break;
  4302. case ANEG_STATE_DISABLE_LINK_OK:
  4303. ret = ANEG_DONE;
  4304. break;
  4305. case ANEG_STATE_ABILITY_DETECT_INIT:
  4306. ap->flags &= ~(MR_TOGGLE_TX);
  4307. ap->txconfig = ANEG_CFG_FD;
  4308. flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
  4309. if (flowctrl & ADVERTISE_1000XPAUSE)
  4310. ap->txconfig |= ANEG_CFG_PS1;
  4311. if (flowctrl & ADVERTISE_1000XPSE_ASYM)
  4312. ap->txconfig |= ANEG_CFG_PS2;
  4313. tw32(MAC_TX_AUTO_NEG, ap->txconfig);
  4314. tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
  4315. tw32_f(MAC_MODE, tp->mac_mode);
  4316. udelay(40);
  4317. ap->state = ANEG_STATE_ABILITY_DETECT;
  4318. break;
  4319. case ANEG_STATE_ABILITY_DETECT:
  4320. if (ap->ability_match != 0 && ap->rxconfig != 0)
  4321. ap->state = ANEG_STATE_ACK_DETECT_INIT;
  4322. break;
  4323. case ANEG_STATE_ACK_DETECT_INIT:
  4324. ap->txconfig |= ANEG_CFG_ACK;
  4325. tw32(MAC_TX_AUTO_NEG, ap->txconfig);
  4326. tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
  4327. tw32_f(MAC_MODE, tp->mac_mode);
  4328. udelay(40);
  4329. ap->state = ANEG_STATE_ACK_DETECT;
  4330. /* fallthru */
  4331. case ANEG_STATE_ACK_DETECT:
  4332. if (ap->ack_match != 0) {
  4333. if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
  4334. (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
  4335. ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
  4336. } else {
  4337. ap->state = ANEG_STATE_AN_ENABLE;
  4338. }
  4339. } else if (ap->ability_match != 0 &&
  4340. ap->rxconfig == 0) {
  4341. ap->state = ANEG_STATE_AN_ENABLE;
  4342. }
  4343. break;
  4344. case ANEG_STATE_COMPLETE_ACK_INIT:
  4345. if (ap->rxconfig & ANEG_CFG_INVAL) {
  4346. ret = ANEG_FAILED;
  4347. break;
  4348. }
  4349. ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
  4350. MR_LP_ADV_HALF_DUPLEX |
  4351. MR_LP_ADV_SYM_PAUSE |
  4352. MR_LP_ADV_ASYM_PAUSE |
  4353. MR_LP_ADV_REMOTE_FAULT1 |
  4354. MR_LP_ADV_REMOTE_FAULT2 |
  4355. MR_LP_ADV_NEXT_PAGE |
  4356. MR_TOGGLE_RX |
  4357. MR_NP_RX);
  4358. if (ap->rxconfig & ANEG_CFG_FD)
  4359. ap->flags |= MR_LP_ADV_FULL_DUPLEX;
  4360. if (ap->rxconfig & ANEG_CFG_HD)
  4361. ap->flags |= MR_LP_ADV_HALF_DUPLEX;
  4362. if (ap->rxconfig & ANEG_CFG_PS1)
  4363. ap->flags |= MR_LP_ADV_SYM_PAUSE;
  4364. if (ap->rxconfig & ANEG_CFG_PS2)
  4365. ap->flags |= MR_LP_ADV_ASYM_PAUSE;
  4366. if (ap->rxconfig & ANEG_CFG_RF1)
  4367. ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
  4368. if (ap->rxconfig & ANEG_CFG_RF2)
  4369. ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
  4370. if (ap->rxconfig & ANEG_CFG_NP)
  4371. ap->flags |= MR_LP_ADV_NEXT_PAGE;
  4372. ap->link_time = ap->cur_time;
  4373. ap->flags ^= (MR_TOGGLE_TX);
  4374. if (ap->rxconfig & 0x0008)
  4375. ap->flags |= MR_TOGGLE_RX;
  4376. if (ap->rxconfig & ANEG_CFG_NP)
  4377. ap->flags |= MR_NP_RX;
  4378. ap->flags |= MR_PAGE_RX;
  4379. ap->state = ANEG_STATE_COMPLETE_ACK;
  4380. ret = ANEG_TIMER_ENAB;
  4381. break;
  4382. case ANEG_STATE_COMPLETE_ACK:
  4383. if (ap->ability_match != 0 &&
  4384. ap->rxconfig == 0) {
  4385. ap->state = ANEG_STATE_AN_ENABLE;
  4386. break;
  4387. }
  4388. delta = ap->cur_time - ap->link_time;
  4389. if (delta > ANEG_STATE_SETTLE_TIME) {
  4390. if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
  4391. ap->state = ANEG_STATE_IDLE_DETECT_INIT;
  4392. } else {
  4393. if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
  4394. !(ap->flags & MR_NP_RX)) {
  4395. ap->state = ANEG_STATE_IDLE_DETECT_INIT;
  4396. } else {
  4397. ret = ANEG_FAILED;
  4398. }
  4399. }
  4400. }
  4401. break;
  4402. case ANEG_STATE_IDLE_DETECT_INIT:
  4403. ap->link_time = ap->cur_time;
  4404. tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
  4405. tw32_f(MAC_MODE, tp->mac_mode);
  4406. udelay(40);
  4407. ap->state = ANEG_STATE_IDLE_DETECT;
  4408. ret = ANEG_TIMER_ENAB;
  4409. break;
  4410. case ANEG_STATE_IDLE_DETECT:
  4411. if (ap->ability_match != 0 &&
  4412. ap->rxconfig == 0) {
  4413. ap->state = ANEG_STATE_AN_ENABLE;
  4414. break;
  4415. }
  4416. delta = ap->cur_time - ap->link_time;
  4417. if (delta > ANEG_STATE_SETTLE_TIME) {
  4418. /* XXX another gem from the Broadcom driver :( */
  4419. ap->state = ANEG_STATE_LINK_OK;
  4420. }
  4421. break;
  4422. case ANEG_STATE_LINK_OK:
  4423. ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
  4424. ret = ANEG_DONE;
  4425. break;
  4426. case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
  4427. /* ??? unimplemented */
  4428. break;
  4429. case ANEG_STATE_NEXT_PAGE_WAIT:
  4430. /* ??? unimplemented */
  4431. break;
  4432. default:
  4433. ret = ANEG_FAILED;
  4434. break;
  4435. }
  4436. return ret;
  4437. }
  4438. static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
  4439. {
  4440. int res = 0;
  4441. struct tg3_fiber_aneginfo aninfo;
  4442. int status = ANEG_FAILED;
  4443. unsigned int tick;
  4444. u32 tmp;
  4445. tw32_f(MAC_TX_AUTO_NEG, 0);
  4446. tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
  4447. tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
  4448. udelay(40);
  4449. tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
  4450. udelay(40);
  4451. memset(&aninfo, 0, sizeof(aninfo));
  4452. aninfo.flags |= MR_AN_ENABLE;
  4453. aninfo.state = ANEG_STATE_UNKNOWN;
  4454. aninfo.cur_time = 0;
  4455. tick = 0;
  4456. while (++tick < 195000) {
  4457. status = tg3_fiber_aneg_smachine(tp, &aninfo);
  4458. if (status == ANEG_DONE || status == ANEG_FAILED)
  4459. break;
  4460. udelay(1);
  4461. }
  4462. tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
  4463. tw32_f(MAC_MODE, tp->mac_mode);
  4464. udelay(40);
  4465. *txflags = aninfo.txconfig;
  4466. *rxflags = aninfo.flags;
  4467. if (status == ANEG_DONE &&
  4468. (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
  4469. MR_LP_ADV_FULL_DUPLEX)))
  4470. res = 1;
  4471. return res;
  4472. }
  4473. static void tg3_init_bcm8002(struct tg3 *tp)
  4474. {
  4475. u32 mac_status = tr32(MAC_STATUS);
  4476. int i;
  4477. /* Reset when initting first time or we have a link. */
  4478. if (tg3_flag(tp, INIT_COMPLETE) &&
  4479. !(mac_status & MAC_STATUS_PCS_SYNCED))
  4480. return;
  4481. /* Set PLL lock range. */
  4482. tg3_writephy(tp, 0x16, 0x8007);
  4483. /* SW reset */
  4484. tg3_writephy(tp, MII_BMCR, BMCR_RESET);
  4485. /* Wait for reset to complete. */
  4486. /* XXX schedule_timeout() ... */
  4487. for (i = 0; i < 500; i++)
  4488. udelay(10);
  4489. /* Config mode; select PMA/Ch 1 regs. */
  4490. tg3_writephy(tp, 0x10, 0x8411);
  4491. /* Enable auto-lock and comdet, select txclk for tx. */
  4492. tg3_writephy(tp, 0x11, 0x0a10);
  4493. tg3_writephy(tp, 0x18, 0x00a0);
  4494. tg3_writephy(tp, 0x16, 0x41ff);
  4495. /* Assert and deassert POR. */
  4496. tg3_writephy(tp, 0x13, 0x0400);
  4497. udelay(40);
  4498. tg3_writephy(tp, 0x13, 0x0000);
  4499. tg3_writephy(tp, 0x11, 0x0a50);
  4500. udelay(40);
  4501. tg3_writephy(tp, 0x11, 0x0a10);
  4502. /* Wait for signal to stabilize */
  4503. /* XXX schedule_timeout() ... */
  4504. for (i = 0; i < 15000; i++)
  4505. udelay(10);
  4506. /* Deselect the channel register so we can read the PHYID
  4507. * later.
  4508. */
  4509. tg3_writephy(tp, 0x10, 0x8011);
  4510. }
  4511. static bool tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
  4512. {
  4513. u16 flowctrl;
  4514. bool current_link_up;
  4515. u32 sg_dig_ctrl, sg_dig_status;
  4516. u32 serdes_cfg, expected_sg_dig_ctrl;
  4517. int workaround, port_a;
  4518. serdes_cfg = 0;
  4519. expected_sg_dig_ctrl = 0;
  4520. workaround = 0;
  4521. port_a = 1;
  4522. current_link_up = false;
  4523. if (tg3_chip_rev_id(tp) != CHIPREV_ID_5704_A0 &&
  4524. tg3_chip_rev_id(tp) != CHIPREV_ID_5704_A1) {
  4525. workaround = 1;
  4526. if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
  4527. port_a = 0;
  4528. /* preserve bits 0-11,13,14 for signal pre-emphasis */
  4529. /* preserve bits 20-23 for voltage regulator */
  4530. serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
  4531. }
  4532. sg_dig_ctrl = tr32(SG_DIG_CTRL);
  4533. if (tp->link_config.autoneg != AUTONEG_ENABLE) {
  4534. if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
  4535. if (workaround) {
  4536. u32 val = serdes_cfg;
  4537. if (port_a)
  4538. val |= 0xc010000;
  4539. else
  4540. val |= 0x4010000;
  4541. tw32_f(MAC_SERDES_CFG, val);
  4542. }
  4543. tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
  4544. }
  4545. if (mac_status & MAC_STATUS_PCS_SYNCED) {
  4546. tg3_setup_flow_control(tp, 0, 0);
  4547. current_link_up = true;
  4548. }
  4549. goto out;
  4550. }
  4551. /* Want auto-negotiation. */
  4552. expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
  4553. flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
  4554. if (flowctrl & ADVERTISE_1000XPAUSE)
  4555. expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
  4556. if (flowctrl & ADVERTISE_1000XPSE_ASYM)
  4557. expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
  4558. if (sg_dig_ctrl != expected_sg_dig_ctrl) {
  4559. if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
  4560. tp->serdes_counter &&
  4561. ((mac_status & (MAC_STATUS_PCS_SYNCED |
  4562. MAC_STATUS_RCVD_CFG)) ==
  4563. MAC_STATUS_PCS_SYNCED)) {
  4564. tp->serdes_counter--;
  4565. current_link_up = true;
  4566. goto out;
  4567. }
  4568. restart_autoneg:
  4569. if (workaround)
  4570. tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
  4571. tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
  4572. udelay(5);
  4573. tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
  4574. tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
  4575. tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
  4576. } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
  4577. MAC_STATUS_SIGNAL_DET)) {
  4578. sg_dig_status = tr32(SG_DIG_STATUS);
  4579. mac_status = tr32(MAC_STATUS);
  4580. if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
  4581. (mac_status & MAC_STATUS_PCS_SYNCED)) {
  4582. u32 local_adv = 0, remote_adv = 0;
  4583. if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
  4584. local_adv |= ADVERTISE_1000XPAUSE;
  4585. if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
  4586. local_adv |= ADVERTISE_1000XPSE_ASYM;
  4587. if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
  4588. remote_adv |= LPA_1000XPAUSE;
  4589. if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
  4590. remote_adv |= LPA_1000XPAUSE_ASYM;
  4591. tp->link_config.rmt_adv =
  4592. mii_adv_to_ethtool_adv_x(remote_adv);
  4593. tg3_setup_flow_control(tp, local_adv, remote_adv);
  4594. current_link_up = true;
  4595. tp->serdes_counter = 0;
  4596. tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
  4597. } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
  4598. if (tp->serdes_counter)
  4599. tp->serdes_counter--;
  4600. else {
  4601. if (workaround) {
  4602. u32 val = serdes_cfg;
  4603. if (port_a)
  4604. val |= 0xc010000;
  4605. else
  4606. val |= 0x4010000;
  4607. tw32_f(MAC_SERDES_CFG, val);
  4608. }
  4609. tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
  4610. udelay(40);
  4611. /* Link parallel detection - link is up */
  4612. /* only if we have PCS_SYNC and not */
  4613. /* receiving config code words */
  4614. mac_status = tr32(MAC_STATUS);
  4615. if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
  4616. !(mac_status & MAC_STATUS_RCVD_CFG)) {
  4617. tg3_setup_flow_control(tp, 0, 0);
  4618. current_link_up = true;
  4619. tp->phy_flags |=
  4620. TG3_PHYFLG_PARALLEL_DETECT;
  4621. tp->serdes_counter =
  4622. SERDES_PARALLEL_DET_TIMEOUT;
  4623. } else
  4624. goto restart_autoneg;
  4625. }
  4626. }
  4627. } else {
  4628. tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
  4629. tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
  4630. }
  4631. out:
  4632. return current_link_up;
  4633. }
  4634. static bool tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
  4635. {
  4636. bool current_link_up = false;
  4637. if (!(mac_status & MAC_STATUS_PCS_SYNCED))
  4638. goto out;
  4639. if (tp->link_config.autoneg == AUTONEG_ENABLE) {
  4640. u32 txflags, rxflags;
  4641. int i;
  4642. if (fiber_autoneg(tp, &txflags, &rxflags)) {
  4643. u32 local_adv = 0, remote_adv = 0;
  4644. if (txflags & ANEG_CFG_PS1)
  4645. local_adv |= ADVERTISE_1000XPAUSE;
  4646. if (txflags & ANEG_CFG_PS2)
  4647. local_adv |= ADVERTISE_1000XPSE_ASYM;
  4648. if (rxflags & MR_LP_ADV_SYM_PAUSE)
  4649. remote_adv |= LPA_1000XPAUSE;
  4650. if (rxflags & MR_LP_ADV_ASYM_PAUSE)
  4651. remote_adv |= LPA_1000XPAUSE_ASYM;
  4652. tp->link_config.rmt_adv =
  4653. mii_adv_to_ethtool_adv_x(remote_adv);
  4654. tg3_setup_flow_control(tp, local_adv, remote_adv);
  4655. current_link_up = true;
  4656. }
  4657. for (i = 0; i < 30; i++) {
  4658. udelay(20);
  4659. tw32_f(MAC_STATUS,
  4660. (MAC_STATUS_SYNC_CHANGED |
  4661. MAC_STATUS_CFG_CHANGED));
  4662. udelay(40);
  4663. if ((tr32(MAC_STATUS) &
  4664. (MAC_STATUS_SYNC_CHANGED |
  4665. MAC_STATUS_CFG_CHANGED)) == 0)
  4666. break;
  4667. }
  4668. mac_status = tr32(MAC_STATUS);
  4669. if (!current_link_up &&
  4670. (mac_status & MAC_STATUS_PCS_SYNCED) &&
  4671. !(mac_status & MAC_STATUS_RCVD_CFG))
  4672. current_link_up = true;
  4673. } else {
  4674. tg3_setup_flow_control(tp, 0, 0);
  4675. /* Forcing 1000FD link up. */
  4676. current_link_up = true;
  4677. tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
  4678. udelay(40);
  4679. tw32_f(MAC_MODE, tp->mac_mode);
  4680. udelay(40);
  4681. }
  4682. out:
  4683. return current_link_up;
  4684. }
  4685. static int tg3_setup_fiber_phy(struct tg3 *tp, bool force_reset)
  4686. {
  4687. u32 orig_pause_cfg;
  4688. u16 orig_active_speed;
  4689. u8 orig_active_duplex;
  4690. u32 mac_status;
  4691. bool current_link_up;
  4692. int i;
  4693. orig_pause_cfg = tp->link_config.active_flowctrl;
  4694. orig_active_speed = tp->link_config.active_speed;
  4695. orig_active_duplex = tp->link_config.active_duplex;
  4696. if (!tg3_flag(tp, HW_AUTONEG) &&
  4697. tp->link_up &&
  4698. tg3_flag(tp, INIT_COMPLETE)) {
  4699. mac_status = tr32(MAC_STATUS);
  4700. mac_status &= (MAC_STATUS_PCS_SYNCED |
  4701. MAC_STATUS_SIGNAL_DET |
  4702. MAC_STATUS_CFG_CHANGED |
  4703. MAC_STATUS_RCVD_CFG);
  4704. if (mac_status == (MAC_STATUS_PCS_SYNCED |
  4705. MAC_STATUS_SIGNAL_DET)) {
  4706. tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
  4707. MAC_STATUS_CFG_CHANGED));
  4708. return 0;
  4709. }
  4710. }
  4711. tw32_f(MAC_TX_AUTO_NEG, 0);
  4712. tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
  4713. tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
  4714. tw32_f(MAC_MODE, tp->mac_mode);
  4715. udelay(40);
  4716. if (tp->phy_id == TG3_PHY_ID_BCM8002)
  4717. tg3_init_bcm8002(tp);
  4718. /* Enable link change event even when serdes polling. */
  4719. tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
  4720. udelay(40);
  4721. current_link_up = false;
  4722. tp->link_config.rmt_adv = 0;
  4723. mac_status = tr32(MAC_STATUS);
  4724. if (tg3_flag(tp, HW_AUTONEG))
  4725. current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
  4726. else
  4727. current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
  4728. tp->napi[0].hw_status->status =
  4729. (SD_STATUS_UPDATED |
  4730. (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
  4731. for (i = 0; i < 100; i++) {
  4732. tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
  4733. MAC_STATUS_CFG_CHANGED));
  4734. udelay(5);
  4735. if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
  4736. MAC_STATUS_CFG_CHANGED |
  4737. MAC_STATUS_LNKSTATE_CHANGED)) == 0)
  4738. break;
  4739. }
  4740. mac_status = tr32(MAC_STATUS);
  4741. if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
  4742. current_link_up = false;
  4743. if (tp->link_config.autoneg == AUTONEG_ENABLE &&
  4744. tp->serdes_counter == 0) {
  4745. tw32_f(MAC_MODE, (tp->mac_mode |
  4746. MAC_MODE_SEND_CONFIGS));
  4747. udelay(1);
  4748. tw32_f(MAC_MODE, tp->mac_mode);
  4749. }
  4750. }
  4751. if (current_link_up) {
  4752. tp->link_config.active_speed = SPEED_1000;
  4753. tp->link_config.active_duplex = DUPLEX_FULL;
  4754. tw32(MAC_LED_CTRL, (tp->led_ctrl |
  4755. LED_CTRL_LNKLED_OVERRIDE |
  4756. LED_CTRL_1000MBPS_ON));
  4757. } else {
  4758. tp->link_config.active_speed = SPEED_UNKNOWN;
  4759. tp->link_config.active_duplex = DUPLEX_UNKNOWN;
  4760. tw32(MAC_LED_CTRL, (tp->led_ctrl |
  4761. LED_CTRL_LNKLED_OVERRIDE |
  4762. LED_CTRL_TRAFFIC_OVERRIDE));
  4763. }
  4764. if (!tg3_test_and_report_link_chg(tp, current_link_up)) {
  4765. u32 now_pause_cfg = tp->link_config.active_flowctrl;
  4766. if (orig_pause_cfg != now_pause_cfg ||
  4767. orig_active_speed != tp->link_config.active_speed ||
  4768. orig_active_duplex != tp->link_config.active_duplex)
  4769. tg3_link_report(tp);
  4770. }
  4771. return 0;
  4772. }
  4773. static int tg3_setup_fiber_mii_phy(struct tg3 *tp, bool force_reset)
  4774. {
  4775. int err = 0;
  4776. u32 bmsr, bmcr;
  4777. u16 current_speed = SPEED_UNKNOWN;
  4778. u8 current_duplex = DUPLEX_UNKNOWN;
  4779. bool current_link_up = false;
  4780. u32 local_adv, remote_adv, sgsr;
  4781. if ((tg3_asic_rev(tp) == ASIC_REV_5719 ||
  4782. tg3_asic_rev(tp) == ASIC_REV_5720) &&
  4783. !tg3_readphy(tp, SERDES_TG3_1000X_STATUS, &sgsr) &&
  4784. (sgsr & SERDES_TG3_SGMII_MODE)) {
  4785. if (force_reset)
  4786. tg3_phy_reset(tp);
  4787. tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
  4788. if (!(sgsr & SERDES_TG3_LINK_UP)) {
  4789. tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
  4790. } else {
  4791. current_link_up = true;
  4792. if (sgsr & SERDES_TG3_SPEED_1000) {
  4793. current_speed = SPEED_1000;
  4794. tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
  4795. } else if (sgsr & SERDES_TG3_SPEED_100) {
  4796. current_speed = SPEED_100;
  4797. tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
  4798. } else {
  4799. current_speed = SPEED_10;
  4800. tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
  4801. }
  4802. if (sgsr & SERDES_TG3_FULL_DUPLEX)
  4803. current_duplex = DUPLEX_FULL;
  4804. else
  4805. current_duplex = DUPLEX_HALF;
  4806. }
  4807. tw32_f(MAC_MODE, tp->mac_mode);
  4808. udelay(40);
  4809. tg3_clear_mac_status(tp);
  4810. goto fiber_setup_done;
  4811. }
  4812. tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
  4813. tw32_f(MAC_MODE, tp->mac_mode);
  4814. udelay(40);
  4815. tg3_clear_mac_status(tp);
  4816. if (force_reset)
  4817. tg3_phy_reset(tp);
  4818. tp->link_config.rmt_adv = 0;
  4819. err |= tg3_readphy(tp, MII_BMSR, &bmsr);
  4820. err |= tg3_readphy(tp, MII_BMSR, &bmsr);
  4821. if (tg3_asic_rev(tp) == ASIC_REV_5714) {
  4822. if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
  4823. bmsr |= BMSR_LSTATUS;
  4824. else
  4825. bmsr &= ~BMSR_LSTATUS;
  4826. }
  4827. err |= tg3_readphy(tp, MII_BMCR, &bmcr);
  4828. if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
  4829. (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
  4830. /* do nothing, just check for link up at the end */
  4831. } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
  4832. u32 adv, newadv;
  4833. err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
  4834. newadv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
  4835. ADVERTISE_1000XPAUSE |
  4836. ADVERTISE_1000XPSE_ASYM |
  4837. ADVERTISE_SLCT);
  4838. newadv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
  4839. newadv |= ethtool_adv_to_mii_adv_x(tp->link_config.advertising);
  4840. if ((newadv != adv) || !(bmcr & BMCR_ANENABLE)) {
  4841. tg3_writephy(tp, MII_ADVERTISE, newadv);
  4842. bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
  4843. tg3_writephy(tp, MII_BMCR, bmcr);
  4844. tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
  4845. tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
  4846. tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
  4847. return err;
  4848. }
  4849. } else {
  4850. u32 new_bmcr;
  4851. bmcr &= ~BMCR_SPEED1000;
  4852. new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
  4853. if (tp->link_config.duplex == DUPLEX_FULL)
  4854. new_bmcr |= BMCR_FULLDPLX;
  4855. if (new_bmcr != bmcr) {
  4856. /* BMCR_SPEED1000 is a reserved bit that needs
  4857. * to be set on write.
  4858. */
  4859. new_bmcr |= BMCR_SPEED1000;
  4860. /* Force a linkdown */
  4861. if (tp->link_up) {
  4862. u32 adv;
  4863. err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
  4864. adv &= ~(ADVERTISE_1000XFULL |
  4865. ADVERTISE_1000XHALF |
  4866. ADVERTISE_SLCT);
  4867. tg3_writephy(tp, MII_ADVERTISE, adv);
  4868. tg3_writephy(tp, MII_BMCR, bmcr |
  4869. BMCR_ANRESTART |
  4870. BMCR_ANENABLE);
  4871. udelay(10);
  4872. tg3_carrier_off(tp);
  4873. }
  4874. tg3_writephy(tp, MII_BMCR, new_bmcr);
  4875. bmcr = new_bmcr;
  4876. err |= tg3_readphy(tp, MII_BMSR, &bmsr);
  4877. err |= tg3_readphy(tp, MII_BMSR, &bmsr);
  4878. if (tg3_asic_rev(tp) == ASIC_REV_5714) {
  4879. if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
  4880. bmsr |= BMSR_LSTATUS;
  4881. else
  4882. bmsr &= ~BMSR_LSTATUS;
  4883. }
  4884. tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
  4885. }
  4886. }
  4887. if (bmsr & BMSR_LSTATUS) {
  4888. current_speed = SPEED_1000;
  4889. current_link_up = true;
  4890. if (bmcr & BMCR_FULLDPLX)
  4891. current_duplex = DUPLEX_FULL;
  4892. else
  4893. current_duplex = DUPLEX_HALF;
  4894. local_adv = 0;
  4895. remote_adv = 0;
  4896. if (bmcr & BMCR_ANENABLE) {
  4897. u32 common;
  4898. err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
  4899. err |= tg3_readphy(tp, MII_LPA, &remote_adv);
  4900. common = local_adv & remote_adv;
  4901. if (common & (ADVERTISE_1000XHALF |
  4902. ADVERTISE_1000XFULL)) {
  4903. if (common & ADVERTISE_1000XFULL)
  4904. current_duplex = DUPLEX_FULL;
  4905. else
  4906. current_duplex = DUPLEX_HALF;
  4907. tp->link_config.rmt_adv =
  4908. mii_adv_to_ethtool_adv_x(remote_adv);
  4909. } else if (!tg3_flag(tp, 5780_CLASS)) {
  4910. /* Link is up via parallel detect */
  4911. } else {
  4912. current_link_up = false;
  4913. }
  4914. }
  4915. }
  4916. fiber_setup_done:
  4917. if (current_link_up && current_duplex == DUPLEX_FULL)
  4918. tg3_setup_flow_control(tp, local_adv, remote_adv);
  4919. tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
  4920. if (tp->link_config.active_duplex == DUPLEX_HALF)
  4921. tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
  4922. tw32_f(MAC_MODE, tp->mac_mode);
  4923. udelay(40);
  4924. tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
  4925. tp->link_config.active_speed = current_speed;
  4926. tp->link_config.active_duplex = current_duplex;
  4927. tg3_test_and_report_link_chg(tp, current_link_up);
  4928. return err;
  4929. }
  4930. static void tg3_serdes_parallel_detect(struct tg3 *tp)
  4931. {
  4932. if (tp->serdes_counter) {
  4933. /* Give autoneg time to complete. */
  4934. tp->serdes_counter--;
  4935. return;
  4936. }
  4937. if (!tp->link_up &&
  4938. (tp->link_config.autoneg == AUTONEG_ENABLE)) {
  4939. u32 bmcr;
  4940. tg3_readphy(tp, MII_BMCR, &bmcr);
  4941. if (bmcr & BMCR_ANENABLE) {
  4942. u32 phy1, phy2;
  4943. /* Select shadow register 0x1f */
  4944. tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
  4945. tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
  4946. /* Select expansion interrupt status register */
  4947. tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
  4948. MII_TG3_DSP_EXP1_INT_STAT);
  4949. tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
  4950. tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
  4951. if ((phy1 & 0x10) && !(phy2 & 0x20)) {
  4952. /* We have signal detect and not receiving
  4953. * config code words, link is up by parallel
  4954. * detection.
  4955. */
  4956. bmcr &= ~BMCR_ANENABLE;
  4957. bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
  4958. tg3_writephy(tp, MII_BMCR, bmcr);
  4959. tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
  4960. }
  4961. }
  4962. } else if (tp->link_up &&
  4963. (tp->link_config.autoneg == AUTONEG_ENABLE) &&
  4964. (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
  4965. u32 phy2;
  4966. /* Select expansion interrupt status register */
  4967. tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
  4968. MII_TG3_DSP_EXP1_INT_STAT);
  4969. tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
  4970. if (phy2 & 0x20) {
  4971. u32 bmcr;
  4972. /* Config code words received, turn on autoneg. */
  4973. tg3_readphy(tp, MII_BMCR, &bmcr);
  4974. tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
  4975. tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
  4976. }
  4977. }
  4978. }
  4979. static int tg3_setup_phy(struct tg3 *tp, bool force_reset)
  4980. {
  4981. u32 val;
  4982. int err;
  4983. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
  4984. err = tg3_setup_fiber_phy(tp, force_reset);
  4985. else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
  4986. err = tg3_setup_fiber_mii_phy(tp, force_reset);
  4987. else
  4988. err = tg3_setup_copper_phy(tp, force_reset);
  4989. if (tg3_chip_rev(tp) == CHIPREV_5784_AX) {
  4990. u32 scale;
  4991. val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
  4992. if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
  4993. scale = 65;
  4994. else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
  4995. scale = 6;
  4996. else
  4997. scale = 12;
  4998. val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
  4999. val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
  5000. tw32(GRC_MISC_CFG, val);
  5001. }
  5002. val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
  5003. (6 << TX_LENGTHS_IPG_SHIFT);
  5004. if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
  5005. tg3_asic_rev(tp) == ASIC_REV_5762)
  5006. val |= tr32(MAC_TX_LENGTHS) &
  5007. (TX_LENGTHS_JMB_FRM_LEN_MSK |
  5008. TX_LENGTHS_CNT_DWN_VAL_MSK);
  5009. if (tp->link_config.active_speed == SPEED_1000 &&
  5010. tp->link_config.active_duplex == DUPLEX_HALF)
  5011. tw32(MAC_TX_LENGTHS, val |
  5012. (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
  5013. else
  5014. tw32(MAC_TX_LENGTHS, val |
  5015. (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
  5016. if (!tg3_flag(tp, 5705_PLUS)) {
  5017. if (tp->link_up) {
  5018. tw32(HOSTCC_STAT_COAL_TICKS,
  5019. tp->coal.stats_block_coalesce_usecs);
  5020. } else {
  5021. tw32(HOSTCC_STAT_COAL_TICKS, 0);
  5022. }
  5023. }
  5024. if (tg3_flag(tp, ASPM_WORKAROUND)) {
  5025. val = tr32(PCIE_PWR_MGMT_THRESH);
  5026. if (!tp->link_up)
  5027. val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
  5028. tp->pwrmgmt_thresh;
  5029. else
  5030. val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
  5031. tw32(PCIE_PWR_MGMT_THRESH, val);
  5032. }
  5033. return err;
  5034. }
  5035. /* tp->lock must be held */
  5036. static u64 tg3_refclk_read(struct tg3 *tp)
  5037. {
  5038. u64 stamp = tr32(TG3_EAV_REF_CLCK_LSB);
  5039. return stamp | (u64)tr32(TG3_EAV_REF_CLCK_MSB) << 32;
  5040. }
  5041. /* tp->lock must be held */
  5042. static void tg3_refclk_write(struct tg3 *tp, u64 newval)
  5043. {
  5044. u32 clock_ctl = tr32(TG3_EAV_REF_CLCK_CTL);
  5045. tw32(TG3_EAV_REF_CLCK_CTL, clock_ctl | TG3_EAV_REF_CLCK_CTL_STOP);
  5046. tw32(TG3_EAV_REF_CLCK_LSB, newval & 0xffffffff);
  5047. tw32(TG3_EAV_REF_CLCK_MSB, newval >> 32);
  5048. tw32_f(TG3_EAV_REF_CLCK_CTL, clock_ctl | TG3_EAV_REF_CLCK_CTL_RESUME);
  5049. }
  5050. static inline void tg3_full_lock(struct tg3 *tp, int irq_sync);
  5051. static inline void tg3_full_unlock(struct tg3 *tp);
  5052. static int tg3_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info)
  5053. {
  5054. struct tg3 *tp = netdev_priv(dev);
  5055. info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
  5056. SOF_TIMESTAMPING_RX_SOFTWARE |
  5057. SOF_TIMESTAMPING_SOFTWARE;
  5058. if (tg3_flag(tp, PTP_CAPABLE)) {
  5059. info->so_timestamping |= SOF_TIMESTAMPING_TX_HARDWARE |
  5060. SOF_TIMESTAMPING_RX_HARDWARE |
  5061. SOF_TIMESTAMPING_RAW_HARDWARE;
  5062. }
  5063. if (tp->ptp_clock)
  5064. info->phc_index = ptp_clock_index(tp->ptp_clock);
  5065. else
  5066. info->phc_index = -1;
  5067. info->tx_types = (1 << HWTSTAMP_TX_OFF) | (1 << HWTSTAMP_TX_ON);
  5068. info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
  5069. (1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT) |
  5070. (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
  5071. (1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT);
  5072. return 0;
  5073. }
  5074. static int tg3_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
  5075. {
  5076. struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
  5077. bool neg_adj = false;
  5078. u32 correction = 0;
  5079. if (ppb < 0) {
  5080. neg_adj = true;
  5081. ppb = -ppb;
  5082. }
  5083. /* Frequency adjustment is performed using hardware with a 24 bit
  5084. * accumulator and a programmable correction value. On each clk, the
  5085. * correction value gets added to the accumulator and when it
  5086. * overflows, the time counter is incremented/decremented.
  5087. *
  5088. * So conversion from ppb to correction value is
  5089. * ppb * (1 << 24) / 1000000000
  5090. */
  5091. correction = div_u64((u64)ppb * (1 << 24), 1000000000ULL) &
  5092. TG3_EAV_REF_CLK_CORRECT_MASK;
  5093. tg3_full_lock(tp, 0);
  5094. if (correction)
  5095. tw32(TG3_EAV_REF_CLK_CORRECT_CTL,
  5096. TG3_EAV_REF_CLK_CORRECT_EN |
  5097. (neg_adj ? TG3_EAV_REF_CLK_CORRECT_NEG : 0) | correction);
  5098. else
  5099. tw32(TG3_EAV_REF_CLK_CORRECT_CTL, 0);
  5100. tg3_full_unlock(tp);
  5101. return 0;
  5102. }
  5103. static int tg3_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
  5104. {
  5105. struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
  5106. tg3_full_lock(tp, 0);
  5107. tp->ptp_adjust += delta;
  5108. tg3_full_unlock(tp);
  5109. return 0;
  5110. }
  5111. static int tg3_ptp_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
  5112. {
  5113. u64 ns;
  5114. struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
  5115. tg3_full_lock(tp, 0);
  5116. ns = tg3_refclk_read(tp);
  5117. ns += tp->ptp_adjust;
  5118. tg3_full_unlock(tp);
  5119. *ts = ns_to_timespec64(ns);
  5120. return 0;
  5121. }
  5122. static int tg3_ptp_settime(struct ptp_clock_info *ptp,
  5123. const struct timespec64 *ts)
  5124. {
  5125. u64 ns;
  5126. struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
  5127. ns = timespec64_to_ns(ts);
  5128. tg3_full_lock(tp, 0);
  5129. tg3_refclk_write(tp, ns);
  5130. tp->ptp_adjust = 0;
  5131. tg3_full_unlock(tp);
  5132. return 0;
  5133. }
  5134. static int tg3_ptp_enable(struct ptp_clock_info *ptp,
  5135. struct ptp_clock_request *rq, int on)
  5136. {
  5137. struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
  5138. u32 clock_ctl;
  5139. int rval = 0;
  5140. switch (rq->type) {
  5141. case PTP_CLK_REQ_PEROUT:
  5142. if (rq->perout.index != 0)
  5143. return -EINVAL;
  5144. tg3_full_lock(tp, 0);
  5145. clock_ctl = tr32(TG3_EAV_REF_CLCK_CTL);
  5146. clock_ctl &= ~TG3_EAV_CTL_TSYNC_GPIO_MASK;
  5147. if (on) {
  5148. u64 nsec;
  5149. nsec = rq->perout.start.sec * 1000000000ULL +
  5150. rq->perout.start.nsec;
  5151. if (rq->perout.period.sec || rq->perout.period.nsec) {
  5152. netdev_warn(tp->dev,
  5153. "Device supports only a one-shot timesync output, period must be 0\n");
  5154. rval = -EINVAL;
  5155. goto err_out;
  5156. }
  5157. if (nsec & (1ULL << 63)) {
  5158. netdev_warn(tp->dev,
  5159. "Start value (nsec) is over limit. Maximum size of start is only 63 bits\n");
  5160. rval = -EINVAL;
  5161. goto err_out;
  5162. }
  5163. tw32(TG3_EAV_WATCHDOG0_LSB, (nsec & 0xffffffff));
  5164. tw32(TG3_EAV_WATCHDOG0_MSB,
  5165. TG3_EAV_WATCHDOG0_EN |
  5166. ((nsec >> 32) & TG3_EAV_WATCHDOG_MSB_MASK));
  5167. tw32(TG3_EAV_REF_CLCK_CTL,
  5168. clock_ctl | TG3_EAV_CTL_TSYNC_WDOG0);
  5169. } else {
  5170. tw32(TG3_EAV_WATCHDOG0_MSB, 0);
  5171. tw32(TG3_EAV_REF_CLCK_CTL, clock_ctl);
  5172. }
  5173. err_out:
  5174. tg3_full_unlock(tp);
  5175. return rval;
  5176. default:
  5177. break;
  5178. }
  5179. return -EOPNOTSUPP;
  5180. }
  5181. static const struct ptp_clock_info tg3_ptp_caps = {
  5182. .owner = THIS_MODULE,
  5183. .name = "tg3 clock",
  5184. .max_adj = 250000000,
  5185. .n_alarm = 0,
  5186. .n_ext_ts = 0,
  5187. .n_per_out = 1,
  5188. .n_pins = 0,
  5189. .pps = 0,
  5190. .adjfreq = tg3_ptp_adjfreq,
  5191. .adjtime = tg3_ptp_adjtime,
  5192. .gettime64 = tg3_ptp_gettime,
  5193. .settime64 = tg3_ptp_settime,
  5194. .enable = tg3_ptp_enable,
  5195. };
  5196. static void tg3_hwclock_to_timestamp(struct tg3 *tp, u64 hwclock,
  5197. struct skb_shared_hwtstamps *timestamp)
  5198. {
  5199. memset(timestamp, 0, sizeof(struct skb_shared_hwtstamps));
  5200. timestamp->hwtstamp = ns_to_ktime((hwclock & TG3_TSTAMP_MASK) +
  5201. tp->ptp_adjust);
  5202. }
  5203. /* tp->lock must be held */
  5204. static void tg3_ptp_init(struct tg3 *tp)
  5205. {
  5206. if (!tg3_flag(tp, PTP_CAPABLE))
  5207. return;
  5208. /* Initialize the hardware clock to the system time. */
  5209. tg3_refclk_write(tp, ktime_to_ns(ktime_get_real()));
  5210. tp->ptp_adjust = 0;
  5211. tp->ptp_info = tg3_ptp_caps;
  5212. }
  5213. /* tp->lock must be held */
  5214. static void tg3_ptp_resume(struct tg3 *tp)
  5215. {
  5216. if (!tg3_flag(tp, PTP_CAPABLE))
  5217. return;
  5218. tg3_refclk_write(tp, ktime_to_ns(ktime_get_real()) + tp->ptp_adjust);
  5219. tp->ptp_adjust = 0;
  5220. }
  5221. static void tg3_ptp_fini(struct tg3 *tp)
  5222. {
  5223. if (!tg3_flag(tp, PTP_CAPABLE) || !tp->ptp_clock)
  5224. return;
  5225. ptp_clock_unregister(tp->ptp_clock);
  5226. tp->ptp_clock = NULL;
  5227. tp->ptp_adjust = 0;
  5228. }
  5229. static inline int tg3_irq_sync(struct tg3 *tp)
  5230. {
  5231. return tp->irq_sync;
  5232. }
  5233. static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
  5234. {
  5235. int i;
  5236. dst = (u32 *)((u8 *)dst + off);
  5237. for (i = 0; i < len; i += sizeof(u32))
  5238. *dst++ = tr32(off + i);
  5239. }
  5240. static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
  5241. {
  5242. tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
  5243. tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
  5244. tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
  5245. tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
  5246. tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
  5247. tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
  5248. tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
  5249. tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
  5250. tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
  5251. tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
  5252. tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
  5253. tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
  5254. tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
  5255. tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
  5256. tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
  5257. tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
  5258. tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
  5259. tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
  5260. tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
  5261. if (tg3_flag(tp, SUPPORT_MSIX))
  5262. tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
  5263. tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
  5264. tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
  5265. tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
  5266. tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
  5267. tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
  5268. tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
  5269. tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
  5270. tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
  5271. if (!tg3_flag(tp, 5705_PLUS)) {
  5272. tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
  5273. tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
  5274. tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
  5275. }
  5276. tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
  5277. tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
  5278. tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
  5279. tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
  5280. tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
  5281. if (tg3_flag(tp, NVRAM))
  5282. tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
  5283. }
  5284. static void tg3_dump_state(struct tg3 *tp)
  5285. {
  5286. int i;
  5287. u32 *regs;
  5288. regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
  5289. if (!regs)
  5290. return;
  5291. if (tg3_flag(tp, PCI_EXPRESS)) {
  5292. /* Read up to but not including private PCI registers */
  5293. for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
  5294. regs[i / sizeof(u32)] = tr32(i);
  5295. } else
  5296. tg3_dump_legacy_regs(tp, regs);
  5297. for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
  5298. if (!regs[i + 0] && !regs[i + 1] &&
  5299. !regs[i + 2] && !regs[i + 3])
  5300. continue;
  5301. netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
  5302. i * 4,
  5303. regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
  5304. }
  5305. kfree(regs);
  5306. for (i = 0; i < tp->irq_cnt; i++) {
  5307. struct tg3_napi *tnapi = &tp->napi[i];
  5308. /* SW status block */
  5309. netdev_err(tp->dev,
  5310. "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
  5311. i,
  5312. tnapi->hw_status->status,
  5313. tnapi->hw_status->status_tag,
  5314. tnapi->hw_status->rx_jumbo_consumer,
  5315. tnapi->hw_status->rx_consumer,
  5316. tnapi->hw_status->rx_mini_consumer,
  5317. tnapi->hw_status->idx[0].rx_producer,
  5318. tnapi->hw_status->idx[0].tx_consumer);
  5319. netdev_err(tp->dev,
  5320. "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
  5321. i,
  5322. tnapi->last_tag, tnapi->last_irq_tag,
  5323. tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
  5324. tnapi->rx_rcb_ptr,
  5325. tnapi->prodring.rx_std_prod_idx,
  5326. tnapi->prodring.rx_std_cons_idx,
  5327. tnapi->prodring.rx_jmb_prod_idx,
  5328. tnapi->prodring.rx_jmb_cons_idx);
  5329. }
  5330. }
  5331. /* This is called whenever we suspect that the system chipset is re-
  5332. * ordering the sequence of MMIO to the tx send mailbox. The symptom
  5333. * is bogus tx completions. We try to recover by setting the
  5334. * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
  5335. * in the workqueue.
  5336. */
  5337. static void tg3_tx_recover(struct tg3 *tp)
  5338. {
  5339. BUG_ON(tg3_flag(tp, MBOX_WRITE_REORDER) ||
  5340. tp->write32_tx_mbox == tg3_write_indirect_mbox);
  5341. netdev_warn(tp->dev,
  5342. "The system may be re-ordering memory-mapped I/O "
  5343. "cycles to the network device, attempting to recover. "
  5344. "Please report the problem to the driver maintainer "
  5345. "and include system chipset information.\n");
  5346. tg3_flag_set(tp, TX_RECOVERY_PENDING);
  5347. }
  5348. static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
  5349. {
  5350. /* Tell compiler to fetch tx indices from memory. */
  5351. barrier();
  5352. return tnapi->tx_pending -
  5353. ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
  5354. }
  5355. /* Tigon3 never reports partial packet sends. So we do not
  5356. * need special logic to handle SKBs that have not had all
  5357. * of their frags sent yet, like SunGEM does.
  5358. */
  5359. static void tg3_tx(struct tg3_napi *tnapi)
  5360. {
  5361. struct tg3 *tp = tnapi->tp;
  5362. u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
  5363. u32 sw_idx = tnapi->tx_cons;
  5364. struct netdev_queue *txq;
  5365. int index = tnapi - tp->napi;
  5366. unsigned int pkts_compl = 0, bytes_compl = 0;
  5367. if (tg3_flag(tp, ENABLE_TSS))
  5368. index--;
  5369. txq = netdev_get_tx_queue(tp->dev, index);
  5370. while (sw_idx != hw_idx) {
  5371. struct tg3_tx_ring_info *ri = &tnapi->tx_buffers[sw_idx];
  5372. struct sk_buff *skb = ri->skb;
  5373. int i, tx_bug = 0;
  5374. if (unlikely(skb == NULL)) {
  5375. tg3_tx_recover(tp);
  5376. return;
  5377. }
  5378. if (tnapi->tx_ring[sw_idx].len_flags & TXD_FLAG_HWTSTAMP) {
  5379. struct skb_shared_hwtstamps timestamp;
  5380. u64 hwclock = tr32(TG3_TX_TSTAMP_LSB);
  5381. hwclock |= (u64)tr32(TG3_TX_TSTAMP_MSB) << 32;
  5382. tg3_hwclock_to_timestamp(tp, hwclock, &timestamp);
  5383. skb_tstamp_tx(skb, &timestamp);
  5384. }
  5385. pci_unmap_single(tp->pdev,
  5386. dma_unmap_addr(ri, mapping),
  5387. skb_headlen(skb),
  5388. PCI_DMA_TODEVICE);
  5389. ri->skb = NULL;
  5390. while (ri->fragmented) {
  5391. ri->fragmented = false;
  5392. sw_idx = NEXT_TX(sw_idx);
  5393. ri = &tnapi->tx_buffers[sw_idx];
  5394. }
  5395. sw_idx = NEXT_TX(sw_idx);
  5396. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  5397. ri = &tnapi->tx_buffers[sw_idx];
  5398. if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
  5399. tx_bug = 1;
  5400. pci_unmap_page(tp->pdev,
  5401. dma_unmap_addr(ri, mapping),
  5402. skb_frag_size(&skb_shinfo(skb)->frags[i]),
  5403. PCI_DMA_TODEVICE);
  5404. while (ri->fragmented) {
  5405. ri->fragmented = false;
  5406. sw_idx = NEXT_TX(sw_idx);
  5407. ri = &tnapi->tx_buffers[sw_idx];
  5408. }
  5409. sw_idx = NEXT_TX(sw_idx);
  5410. }
  5411. pkts_compl++;
  5412. bytes_compl += skb->len;
  5413. dev_consume_skb_any(skb);
  5414. if (unlikely(tx_bug)) {
  5415. tg3_tx_recover(tp);
  5416. return;
  5417. }
  5418. }
  5419. netdev_tx_completed_queue(txq, pkts_compl, bytes_compl);
  5420. tnapi->tx_cons = sw_idx;
  5421. /* Need to make the tx_cons update visible to tg3_start_xmit()
  5422. * before checking for netif_queue_stopped(). Without the
  5423. * memory barrier, there is a small possibility that tg3_start_xmit()
  5424. * will miss it and cause the queue to be stopped forever.
  5425. */
  5426. smp_mb();
  5427. if (unlikely(netif_tx_queue_stopped(txq) &&
  5428. (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
  5429. __netif_tx_lock(txq, smp_processor_id());
  5430. if (netif_tx_queue_stopped(txq) &&
  5431. (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
  5432. netif_tx_wake_queue(txq);
  5433. __netif_tx_unlock(txq);
  5434. }
  5435. }
  5436. static void tg3_frag_free(bool is_frag, void *data)
  5437. {
  5438. if (is_frag)
  5439. skb_free_frag(data);
  5440. else
  5441. kfree(data);
  5442. }
  5443. static void tg3_rx_data_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
  5444. {
  5445. unsigned int skb_size = SKB_DATA_ALIGN(map_sz + TG3_RX_OFFSET(tp)) +
  5446. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  5447. if (!ri->data)
  5448. return;
  5449. pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
  5450. map_sz, PCI_DMA_FROMDEVICE);
  5451. tg3_frag_free(skb_size <= PAGE_SIZE, ri->data);
  5452. ri->data = NULL;
  5453. }
  5454. /* Returns size of skb allocated or < 0 on error.
  5455. *
  5456. * We only need to fill in the address because the other members
  5457. * of the RX descriptor are invariant, see tg3_init_rings.
  5458. *
  5459. * Note the purposeful assymetry of cpu vs. chip accesses. For
  5460. * posting buffers we only dirty the first cache line of the RX
  5461. * descriptor (containing the address). Whereas for the RX status
  5462. * buffers the cpu only reads the last cacheline of the RX descriptor
  5463. * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
  5464. */
  5465. static int tg3_alloc_rx_data(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
  5466. u32 opaque_key, u32 dest_idx_unmasked,
  5467. unsigned int *frag_size)
  5468. {
  5469. struct tg3_rx_buffer_desc *desc;
  5470. struct ring_info *map;
  5471. u8 *data;
  5472. dma_addr_t mapping;
  5473. int skb_size, data_size, dest_idx;
  5474. switch (opaque_key) {
  5475. case RXD_OPAQUE_RING_STD:
  5476. dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
  5477. desc = &tpr->rx_std[dest_idx];
  5478. map = &tpr->rx_std_buffers[dest_idx];
  5479. data_size = tp->rx_pkt_map_sz;
  5480. break;
  5481. case RXD_OPAQUE_RING_JUMBO:
  5482. dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
  5483. desc = &tpr->rx_jmb[dest_idx].std;
  5484. map = &tpr->rx_jmb_buffers[dest_idx];
  5485. data_size = TG3_RX_JMB_MAP_SZ;
  5486. break;
  5487. default:
  5488. return -EINVAL;
  5489. }
  5490. /* Do not overwrite any of the map or rp information
  5491. * until we are sure we can commit to a new buffer.
  5492. *
  5493. * Callers depend upon this behavior and assume that
  5494. * we leave everything unchanged if we fail.
  5495. */
  5496. skb_size = SKB_DATA_ALIGN(data_size + TG3_RX_OFFSET(tp)) +
  5497. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  5498. if (skb_size <= PAGE_SIZE) {
  5499. data = netdev_alloc_frag(skb_size);
  5500. *frag_size = skb_size;
  5501. } else {
  5502. data = kmalloc(skb_size, GFP_ATOMIC);
  5503. *frag_size = 0;
  5504. }
  5505. if (!data)
  5506. return -ENOMEM;
  5507. mapping = pci_map_single(tp->pdev,
  5508. data + TG3_RX_OFFSET(tp),
  5509. data_size,
  5510. PCI_DMA_FROMDEVICE);
  5511. if (unlikely(pci_dma_mapping_error(tp->pdev, mapping))) {
  5512. tg3_frag_free(skb_size <= PAGE_SIZE, data);
  5513. return -EIO;
  5514. }
  5515. map->data = data;
  5516. dma_unmap_addr_set(map, mapping, mapping);
  5517. desc->addr_hi = ((u64)mapping >> 32);
  5518. desc->addr_lo = ((u64)mapping & 0xffffffff);
  5519. return data_size;
  5520. }
  5521. /* We only need to move over in the address because the other
  5522. * members of the RX descriptor are invariant. See notes above
  5523. * tg3_alloc_rx_data for full details.
  5524. */
  5525. static void tg3_recycle_rx(struct tg3_napi *tnapi,
  5526. struct tg3_rx_prodring_set *dpr,
  5527. u32 opaque_key, int src_idx,
  5528. u32 dest_idx_unmasked)
  5529. {
  5530. struct tg3 *tp = tnapi->tp;
  5531. struct tg3_rx_buffer_desc *src_desc, *dest_desc;
  5532. struct ring_info *src_map, *dest_map;
  5533. struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
  5534. int dest_idx;
  5535. switch (opaque_key) {
  5536. case RXD_OPAQUE_RING_STD:
  5537. dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
  5538. dest_desc = &dpr->rx_std[dest_idx];
  5539. dest_map = &dpr->rx_std_buffers[dest_idx];
  5540. src_desc = &spr->rx_std[src_idx];
  5541. src_map = &spr->rx_std_buffers[src_idx];
  5542. break;
  5543. case RXD_OPAQUE_RING_JUMBO:
  5544. dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
  5545. dest_desc = &dpr->rx_jmb[dest_idx].std;
  5546. dest_map = &dpr->rx_jmb_buffers[dest_idx];
  5547. src_desc = &spr->rx_jmb[src_idx].std;
  5548. src_map = &spr->rx_jmb_buffers[src_idx];
  5549. break;
  5550. default:
  5551. return;
  5552. }
  5553. dest_map->data = src_map->data;
  5554. dma_unmap_addr_set(dest_map, mapping,
  5555. dma_unmap_addr(src_map, mapping));
  5556. dest_desc->addr_hi = src_desc->addr_hi;
  5557. dest_desc->addr_lo = src_desc->addr_lo;
  5558. /* Ensure that the update to the skb happens after the physical
  5559. * addresses have been transferred to the new BD location.
  5560. */
  5561. smp_wmb();
  5562. src_map->data = NULL;
  5563. }
  5564. /* The RX ring scheme is composed of multiple rings which post fresh
  5565. * buffers to the chip, and one special ring the chip uses to report
  5566. * status back to the host.
  5567. *
  5568. * The special ring reports the status of received packets to the
  5569. * host. The chip does not write into the original descriptor the
  5570. * RX buffer was obtained from. The chip simply takes the original
  5571. * descriptor as provided by the host, updates the status and length
  5572. * field, then writes this into the next status ring entry.
  5573. *
  5574. * Each ring the host uses to post buffers to the chip is described
  5575. * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
  5576. * it is first placed into the on-chip ram. When the packet's length
  5577. * is known, it walks down the TG3_BDINFO entries to select the ring.
  5578. * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
  5579. * which is within the range of the new packet's length is chosen.
  5580. *
  5581. * The "separate ring for rx status" scheme may sound queer, but it makes
  5582. * sense from a cache coherency perspective. If only the host writes
  5583. * to the buffer post rings, and only the chip writes to the rx status
  5584. * rings, then cache lines never move beyond shared-modified state.
  5585. * If both the host and chip were to write into the same ring, cache line
  5586. * eviction could occur since both entities want it in an exclusive state.
  5587. */
  5588. static int tg3_rx(struct tg3_napi *tnapi, int budget)
  5589. {
  5590. struct tg3 *tp = tnapi->tp;
  5591. u32 work_mask, rx_std_posted = 0;
  5592. u32 std_prod_idx, jmb_prod_idx;
  5593. u32 sw_idx = tnapi->rx_rcb_ptr;
  5594. u16 hw_idx;
  5595. int received;
  5596. struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
  5597. hw_idx = *(tnapi->rx_rcb_prod_idx);
  5598. /*
  5599. * We need to order the read of hw_idx and the read of
  5600. * the opaque cookie.
  5601. */
  5602. rmb();
  5603. work_mask = 0;
  5604. received = 0;
  5605. std_prod_idx = tpr->rx_std_prod_idx;
  5606. jmb_prod_idx = tpr->rx_jmb_prod_idx;
  5607. while (sw_idx != hw_idx && budget > 0) {
  5608. struct ring_info *ri;
  5609. struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
  5610. unsigned int len;
  5611. struct sk_buff *skb;
  5612. dma_addr_t dma_addr;
  5613. u32 opaque_key, desc_idx, *post_ptr;
  5614. u8 *data;
  5615. u64 tstamp = 0;
  5616. desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
  5617. opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
  5618. if (opaque_key == RXD_OPAQUE_RING_STD) {
  5619. ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
  5620. dma_addr = dma_unmap_addr(ri, mapping);
  5621. data = ri->data;
  5622. post_ptr = &std_prod_idx;
  5623. rx_std_posted++;
  5624. } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
  5625. ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
  5626. dma_addr = dma_unmap_addr(ri, mapping);
  5627. data = ri->data;
  5628. post_ptr = &jmb_prod_idx;
  5629. } else
  5630. goto next_pkt_nopost;
  5631. work_mask |= opaque_key;
  5632. if (desc->err_vlan & RXD_ERR_MASK) {
  5633. drop_it:
  5634. tg3_recycle_rx(tnapi, tpr, opaque_key,
  5635. desc_idx, *post_ptr);
  5636. drop_it_no_recycle:
  5637. /* Other statistics kept track of by card. */
  5638. tp->rx_dropped++;
  5639. goto next_pkt;
  5640. }
  5641. prefetch(data + TG3_RX_OFFSET(tp));
  5642. len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
  5643. ETH_FCS_LEN;
  5644. if ((desc->type_flags & RXD_FLAG_PTPSTAT_MASK) ==
  5645. RXD_FLAG_PTPSTAT_PTPV1 ||
  5646. (desc->type_flags & RXD_FLAG_PTPSTAT_MASK) ==
  5647. RXD_FLAG_PTPSTAT_PTPV2) {
  5648. tstamp = tr32(TG3_RX_TSTAMP_LSB);
  5649. tstamp |= (u64)tr32(TG3_RX_TSTAMP_MSB) << 32;
  5650. }
  5651. if (len > TG3_RX_COPY_THRESH(tp)) {
  5652. int skb_size;
  5653. unsigned int frag_size;
  5654. skb_size = tg3_alloc_rx_data(tp, tpr, opaque_key,
  5655. *post_ptr, &frag_size);
  5656. if (skb_size < 0)
  5657. goto drop_it;
  5658. pci_unmap_single(tp->pdev, dma_addr, skb_size,
  5659. PCI_DMA_FROMDEVICE);
  5660. /* Ensure that the update to the data happens
  5661. * after the usage of the old DMA mapping.
  5662. */
  5663. smp_wmb();
  5664. ri->data = NULL;
  5665. skb = build_skb(data, frag_size);
  5666. if (!skb) {
  5667. tg3_frag_free(frag_size != 0, data);
  5668. goto drop_it_no_recycle;
  5669. }
  5670. skb_reserve(skb, TG3_RX_OFFSET(tp));
  5671. } else {
  5672. tg3_recycle_rx(tnapi, tpr, opaque_key,
  5673. desc_idx, *post_ptr);
  5674. skb = netdev_alloc_skb(tp->dev,
  5675. len + TG3_RAW_IP_ALIGN);
  5676. if (skb == NULL)
  5677. goto drop_it_no_recycle;
  5678. skb_reserve(skb, TG3_RAW_IP_ALIGN);
  5679. pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
  5680. memcpy(skb->data,
  5681. data + TG3_RX_OFFSET(tp),
  5682. len);
  5683. pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
  5684. }
  5685. skb_put(skb, len);
  5686. if (tstamp)
  5687. tg3_hwclock_to_timestamp(tp, tstamp,
  5688. skb_hwtstamps(skb));
  5689. if ((tp->dev->features & NETIF_F_RXCSUM) &&
  5690. (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
  5691. (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
  5692. >> RXD_TCPCSUM_SHIFT) == 0xffff))
  5693. skb->ip_summed = CHECKSUM_UNNECESSARY;
  5694. else
  5695. skb_checksum_none_assert(skb);
  5696. skb->protocol = eth_type_trans(skb, tp->dev);
  5697. if (len > (tp->dev->mtu + ETH_HLEN) &&
  5698. skb->protocol != htons(ETH_P_8021Q) &&
  5699. skb->protocol != htons(ETH_P_8021AD)) {
  5700. dev_kfree_skb_any(skb);
  5701. goto drop_it_no_recycle;
  5702. }
  5703. if (desc->type_flags & RXD_FLAG_VLAN &&
  5704. !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
  5705. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
  5706. desc->err_vlan & RXD_VLAN_MASK);
  5707. napi_gro_receive(&tnapi->napi, skb);
  5708. received++;
  5709. budget--;
  5710. next_pkt:
  5711. (*post_ptr)++;
  5712. if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
  5713. tpr->rx_std_prod_idx = std_prod_idx &
  5714. tp->rx_std_ring_mask;
  5715. tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
  5716. tpr->rx_std_prod_idx);
  5717. work_mask &= ~RXD_OPAQUE_RING_STD;
  5718. rx_std_posted = 0;
  5719. }
  5720. next_pkt_nopost:
  5721. sw_idx++;
  5722. sw_idx &= tp->rx_ret_ring_mask;
  5723. /* Refresh hw_idx to see if there is new work */
  5724. if (sw_idx == hw_idx) {
  5725. hw_idx = *(tnapi->rx_rcb_prod_idx);
  5726. rmb();
  5727. }
  5728. }
  5729. /* ACK the status ring. */
  5730. tnapi->rx_rcb_ptr = sw_idx;
  5731. tw32_rx_mbox(tnapi->consmbox, sw_idx);
  5732. /* Refill RX ring(s). */
  5733. if (!tg3_flag(tp, ENABLE_RSS)) {
  5734. /* Sync BD data before updating mailbox */
  5735. wmb();
  5736. if (work_mask & RXD_OPAQUE_RING_STD) {
  5737. tpr->rx_std_prod_idx = std_prod_idx &
  5738. tp->rx_std_ring_mask;
  5739. tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
  5740. tpr->rx_std_prod_idx);
  5741. }
  5742. if (work_mask & RXD_OPAQUE_RING_JUMBO) {
  5743. tpr->rx_jmb_prod_idx = jmb_prod_idx &
  5744. tp->rx_jmb_ring_mask;
  5745. tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
  5746. tpr->rx_jmb_prod_idx);
  5747. }
  5748. mmiowb();
  5749. } else if (work_mask) {
  5750. /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
  5751. * updated before the producer indices can be updated.
  5752. */
  5753. smp_wmb();
  5754. tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
  5755. tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
  5756. if (tnapi != &tp->napi[1]) {
  5757. tp->rx_refill = true;
  5758. napi_schedule(&tp->napi[1].napi);
  5759. }
  5760. }
  5761. return received;
  5762. }
  5763. static void tg3_poll_link(struct tg3 *tp)
  5764. {
  5765. /* handle link change and other phy events */
  5766. if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
  5767. struct tg3_hw_status *sblk = tp->napi[0].hw_status;
  5768. if (sblk->status & SD_STATUS_LINK_CHG) {
  5769. sblk->status = SD_STATUS_UPDATED |
  5770. (sblk->status & ~SD_STATUS_LINK_CHG);
  5771. spin_lock(&tp->lock);
  5772. if (tg3_flag(tp, USE_PHYLIB)) {
  5773. tw32_f(MAC_STATUS,
  5774. (MAC_STATUS_SYNC_CHANGED |
  5775. MAC_STATUS_CFG_CHANGED |
  5776. MAC_STATUS_MI_COMPLETION |
  5777. MAC_STATUS_LNKSTATE_CHANGED));
  5778. udelay(40);
  5779. } else
  5780. tg3_setup_phy(tp, false);
  5781. spin_unlock(&tp->lock);
  5782. }
  5783. }
  5784. }
  5785. static int tg3_rx_prodring_xfer(struct tg3 *tp,
  5786. struct tg3_rx_prodring_set *dpr,
  5787. struct tg3_rx_prodring_set *spr)
  5788. {
  5789. u32 si, di, cpycnt, src_prod_idx;
  5790. int i, err = 0;
  5791. while (1) {
  5792. src_prod_idx = spr->rx_std_prod_idx;
  5793. /* Make sure updates to the rx_std_buffers[] entries and the
  5794. * standard producer index are seen in the correct order.
  5795. */
  5796. smp_rmb();
  5797. if (spr->rx_std_cons_idx == src_prod_idx)
  5798. break;
  5799. if (spr->rx_std_cons_idx < src_prod_idx)
  5800. cpycnt = src_prod_idx - spr->rx_std_cons_idx;
  5801. else
  5802. cpycnt = tp->rx_std_ring_mask + 1 -
  5803. spr->rx_std_cons_idx;
  5804. cpycnt = min(cpycnt,
  5805. tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
  5806. si = spr->rx_std_cons_idx;
  5807. di = dpr->rx_std_prod_idx;
  5808. for (i = di; i < di + cpycnt; i++) {
  5809. if (dpr->rx_std_buffers[i].data) {
  5810. cpycnt = i - di;
  5811. err = -ENOSPC;
  5812. break;
  5813. }
  5814. }
  5815. if (!cpycnt)
  5816. break;
  5817. /* Ensure that updates to the rx_std_buffers ring and the
  5818. * shadowed hardware producer ring from tg3_recycle_skb() are
  5819. * ordered correctly WRT the skb check above.
  5820. */
  5821. smp_rmb();
  5822. memcpy(&dpr->rx_std_buffers[di],
  5823. &spr->rx_std_buffers[si],
  5824. cpycnt * sizeof(struct ring_info));
  5825. for (i = 0; i < cpycnt; i++, di++, si++) {
  5826. struct tg3_rx_buffer_desc *sbd, *dbd;
  5827. sbd = &spr->rx_std[si];
  5828. dbd = &dpr->rx_std[di];
  5829. dbd->addr_hi = sbd->addr_hi;
  5830. dbd->addr_lo = sbd->addr_lo;
  5831. }
  5832. spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
  5833. tp->rx_std_ring_mask;
  5834. dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
  5835. tp->rx_std_ring_mask;
  5836. }
  5837. while (1) {
  5838. src_prod_idx = spr->rx_jmb_prod_idx;
  5839. /* Make sure updates to the rx_jmb_buffers[] entries and
  5840. * the jumbo producer index are seen in the correct order.
  5841. */
  5842. smp_rmb();
  5843. if (spr->rx_jmb_cons_idx == src_prod_idx)
  5844. break;
  5845. if (spr->rx_jmb_cons_idx < src_prod_idx)
  5846. cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
  5847. else
  5848. cpycnt = tp->rx_jmb_ring_mask + 1 -
  5849. spr->rx_jmb_cons_idx;
  5850. cpycnt = min(cpycnt,
  5851. tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
  5852. si = spr->rx_jmb_cons_idx;
  5853. di = dpr->rx_jmb_prod_idx;
  5854. for (i = di; i < di + cpycnt; i++) {
  5855. if (dpr->rx_jmb_buffers[i].data) {
  5856. cpycnt = i - di;
  5857. err = -ENOSPC;
  5858. break;
  5859. }
  5860. }
  5861. if (!cpycnt)
  5862. break;
  5863. /* Ensure that updates to the rx_jmb_buffers ring and the
  5864. * shadowed hardware producer ring from tg3_recycle_skb() are
  5865. * ordered correctly WRT the skb check above.
  5866. */
  5867. smp_rmb();
  5868. memcpy(&dpr->rx_jmb_buffers[di],
  5869. &spr->rx_jmb_buffers[si],
  5870. cpycnt * sizeof(struct ring_info));
  5871. for (i = 0; i < cpycnt; i++, di++, si++) {
  5872. struct tg3_rx_buffer_desc *sbd, *dbd;
  5873. sbd = &spr->rx_jmb[si].std;
  5874. dbd = &dpr->rx_jmb[di].std;
  5875. dbd->addr_hi = sbd->addr_hi;
  5876. dbd->addr_lo = sbd->addr_lo;
  5877. }
  5878. spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
  5879. tp->rx_jmb_ring_mask;
  5880. dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
  5881. tp->rx_jmb_ring_mask;
  5882. }
  5883. return err;
  5884. }
  5885. static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
  5886. {
  5887. struct tg3 *tp = tnapi->tp;
  5888. /* run TX completion thread */
  5889. if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
  5890. tg3_tx(tnapi);
  5891. if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
  5892. return work_done;
  5893. }
  5894. if (!tnapi->rx_rcb_prod_idx)
  5895. return work_done;
  5896. /* run RX thread, within the bounds set by NAPI.
  5897. * All RX "locking" is done by ensuring outside
  5898. * code synchronizes with tg3->napi.poll()
  5899. */
  5900. if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
  5901. work_done += tg3_rx(tnapi, budget - work_done);
  5902. if (tg3_flag(tp, ENABLE_RSS) && tnapi == &tp->napi[1]) {
  5903. struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
  5904. int i, err = 0;
  5905. u32 std_prod_idx = dpr->rx_std_prod_idx;
  5906. u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
  5907. tp->rx_refill = false;
  5908. for (i = 1; i <= tp->rxq_cnt; i++)
  5909. err |= tg3_rx_prodring_xfer(tp, dpr,
  5910. &tp->napi[i].prodring);
  5911. wmb();
  5912. if (std_prod_idx != dpr->rx_std_prod_idx)
  5913. tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
  5914. dpr->rx_std_prod_idx);
  5915. if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
  5916. tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
  5917. dpr->rx_jmb_prod_idx);
  5918. mmiowb();
  5919. if (err)
  5920. tw32_f(HOSTCC_MODE, tp->coal_now);
  5921. }
  5922. return work_done;
  5923. }
  5924. static inline void tg3_reset_task_schedule(struct tg3 *tp)
  5925. {
  5926. if (!test_and_set_bit(TG3_FLAG_RESET_TASK_PENDING, tp->tg3_flags))
  5927. schedule_work(&tp->reset_task);
  5928. }
  5929. static inline void tg3_reset_task_cancel(struct tg3 *tp)
  5930. {
  5931. if (test_and_clear_bit(TG3_FLAG_RESET_TASK_PENDING, tp->tg3_flags))
  5932. cancel_work_sync(&tp->reset_task);
  5933. tg3_flag_clear(tp, TX_RECOVERY_PENDING);
  5934. }
  5935. static int tg3_poll_msix(struct napi_struct *napi, int budget)
  5936. {
  5937. struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
  5938. struct tg3 *tp = tnapi->tp;
  5939. int work_done = 0;
  5940. struct tg3_hw_status *sblk = tnapi->hw_status;
  5941. while (1) {
  5942. work_done = tg3_poll_work(tnapi, work_done, budget);
  5943. if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
  5944. goto tx_recovery;
  5945. if (unlikely(work_done >= budget))
  5946. break;
  5947. /* tp->last_tag is used in tg3_int_reenable() below
  5948. * to tell the hw how much work has been processed,
  5949. * so we must read it before checking for more work.
  5950. */
  5951. tnapi->last_tag = sblk->status_tag;
  5952. tnapi->last_irq_tag = tnapi->last_tag;
  5953. rmb();
  5954. /* check for RX/TX work to do */
  5955. if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
  5956. *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
  5957. /* This test here is not race free, but will reduce
  5958. * the number of interrupts by looping again.
  5959. */
  5960. if (tnapi == &tp->napi[1] && tp->rx_refill)
  5961. continue;
  5962. napi_complete_done(napi, work_done);
  5963. /* Reenable interrupts. */
  5964. tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
  5965. /* This test here is synchronized by napi_schedule()
  5966. * and napi_complete() to close the race condition.
  5967. */
  5968. if (unlikely(tnapi == &tp->napi[1] && tp->rx_refill)) {
  5969. tw32(HOSTCC_MODE, tp->coalesce_mode |
  5970. HOSTCC_MODE_ENABLE |
  5971. tnapi->coal_now);
  5972. }
  5973. mmiowb();
  5974. break;
  5975. }
  5976. }
  5977. tg3_send_ape_heartbeat(tp, TG3_APE_HB_INTERVAL << 1);
  5978. return work_done;
  5979. tx_recovery:
  5980. /* work_done is guaranteed to be less than budget. */
  5981. napi_complete(napi);
  5982. tg3_reset_task_schedule(tp);
  5983. return work_done;
  5984. }
  5985. static void tg3_process_error(struct tg3 *tp)
  5986. {
  5987. u32 val;
  5988. bool real_error = false;
  5989. if (tg3_flag(tp, ERROR_PROCESSED))
  5990. return;
  5991. /* Check Flow Attention register */
  5992. val = tr32(HOSTCC_FLOW_ATTN);
  5993. if (val & ~HOSTCC_FLOW_ATTN_MBUF_LWM) {
  5994. netdev_err(tp->dev, "FLOW Attention error. Resetting chip.\n");
  5995. real_error = true;
  5996. }
  5997. if (tr32(MSGINT_STATUS) & ~MSGINT_STATUS_MSI_REQ) {
  5998. netdev_err(tp->dev, "MSI Status error. Resetting chip.\n");
  5999. real_error = true;
  6000. }
  6001. if (tr32(RDMAC_STATUS) || tr32(WDMAC_STATUS)) {
  6002. netdev_err(tp->dev, "DMA Status error. Resetting chip.\n");
  6003. real_error = true;
  6004. }
  6005. if (!real_error)
  6006. return;
  6007. tg3_dump_state(tp);
  6008. tg3_flag_set(tp, ERROR_PROCESSED);
  6009. tg3_reset_task_schedule(tp);
  6010. }
  6011. static int tg3_poll(struct napi_struct *napi, int budget)
  6012. {
  6013. struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
  6014. struct tg3 *tp = tnapi->tp;
  6015. int work_done = 0;
  6016. struct tg3_hw_status *sblk = tnapi->hw_status;
  6017. while (1) {
  6018. if (sblk->status & SD_STATUS_ERROR)
  6019. tg3_process_error(tp);
  6020. tg3_poll_link(tp);
  6021. work_done = tg3_poll_work(tnapi, work_done, budget);
  6022. if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
  6023. goto tx_recovery;
  6024. if (unlikely(work_done >= budget))
  6025. break;
  6026. if (tg3_flag(tp, TAGGED_STATUS)) {
  6027. /* tp->last_tag is used in tg3_int_reenable() below
  6028. * to tell the hw how much work has been processed,
  6029. * so we must read it before checking for more work.
  6030. */
  6031. tnapi->last_tag = sblk->status_tag;
  6032. tnapi->last_irq_tag = tnapi->last_tag;
  6033. rmb();
  6034. } else
  6035. sblk->status &= ~SD_STATUS_UPDATED;
  6036. if (likely(!tg3_has_work(tnapi))) {
  6037. napi_complete_done(napi, work_done);
  6038. tg3_int_reenable(tnapi);
  6039. break;
  6040. }
  6041. }
  6042. tg3_send_ape_heartbeat(tp, TG3_APE_HB_INTERVAL << 1);
  6043. return work_done;
  6044. tx_recovery:
  6045. /* work_done is guaranteed to be less than budget. */
  6046. napi_complete(napi);
  6047. tg3_reset_task_schedule(tp);
  6048. return work_done;
  6049. }
  6050. static void tg3_napi_disable(struct tg3 *tp)
  6051. {
  6052. int i;
  6053. for (i = tp->irq_cnt - 1; i >= 0; i--)
  6054. napi_disable(&tp->napi[i].napi);
  6055. }
  6056. static void tg3_napi_enable(struct tg3 *tp)
  6057. {
  6058. int i;
  6059. for (i = 0; i < tp->irq_cnt; i++)
  6060. napi_enable(&tp->napi[i].napi);
  6061. }
  6062. static void tg3_napi_init(struct tg3 *tp)
  6063. {
  6064. int i;
  6065. netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
  6066. for (i = 1; i < tp->irq_cnt; i++)
  6067. netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
  6068. }
  6069. static void tg3_napi_fini(struct tg3 *tp)
  6070. {
  6071. int i;
  6072. for (i = 0; i < tp->irq_cnt; i++)
  6073. netif_napi_del(&tp->napi[i].napi);
  6074. }
  6075. static inline void tg3_netif_stop(struct tg3 *tp)
  6076. {
  6077. netif_trans_update(tp->dev); /* prevent tx timeout */
  6078. tg3_napi_disable(tp);
  6079. netif_carrier_off(tp->dev);
  6080. netif_tx_disable(tp->dev);
  6081. }
  6082. /* tp->lock must be held */
  6083. static inline void tg3_netif_start(struct tg3 *tp)
  6084. {
  6085. tg3_ptp_resume(tp);
  6086. /* NOTE: unconditional netif_tx_wake_all_queues is only
  6087. * appropriate so long as all callers are assured to
  6088. * have free tx slots (such as after tg3_init_hw)
  6089. */
  6090. netif_tx_wake_all_queues(tp->dev);
  6091. if (tp->link_up)
  6092. netif_carrier_on(tp->dev);
  6093. tg3_napi_enable(tp);
  6094. tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
  6095. tg3_enable_ints(tp);
  6096. }
  6097. static void tg3_irq_quiesce(struct tg3 *tp)
  6098. __releases(tp->lock)
  6099. __acquires(tp->lock)
  6100. {
  6101. int i;
  6102. BUG_ON(tp->irq_sync);
  6103. tp->irq_sync = 1;
  6104. smp_mb();
  6105. spin_unlock_bh(&tp->lock);
  6106. for (i = 0; i < tp->irq_cnt; i++)
  6107. synchronize_irq(tp->napi[i].irq_vec);
  6108. spin_lock_bh(&tp->lock);
  6109. }
  6110. /* Fully shutdown all tg3 driver activity elsewhere in the system.
  6111. * If irq_sync is non-zero, then the IRQ handler must be synchronized
  6112. * with as well. Most of the time, this is not necessary except when
  6113. * shutting down the device.
  6114. */
  6115. static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
  6116. {
  6117. spin_lock_bh(&tp->lock);
  6118. if (irq_sync)
  6119. tg3_irq_quiesce(tp);
  6120. }
  6121. static inline void tg3_full_unlock(struct tg3 *tp)
  6122. {
  6123. spin_unlock_bh(&tp->lock);
  6124. }
  6125. /* One-shot MSI handler - Chip automatically disables interrupt
  6126. * after sending MSI so driver doesn't have to do it.
  6127. */
  6128. static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
  6129. {
  6130. struct tg3_napi *tnapi = dev_id;
  6131. struct tg3 *tp = tnapi->tp;
  6132. prefetch(tnapi->hw_status);
  6133. if (tnapi->rx_rcb)
  6134. prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
  6135. if (likely(!tg3_irq_sync(tp)))
  6136. napi_schedule(&tnapi->napi);
  6137. return IRQ_HANDLED;
  6138. }
  6139. /* MSI ISR - No need to check for interrupt sharing and no need to
  6140. * flush status block and interrupt mailbox. PCI ordering rules
  6141. * guarantee that MSI will arrive after the status block.
  6142. */
  6143. static irqreturn_t tg3_msi(int irq, void *dev_id)
  6144. {
  6145. struct tg3_napi *tnapi = dev_id;
  6146. struct tg3 *tp = tnapi->tp;
  6147. prefetch(tnapi->hw_status);
  6148. if (tnapi->rx_rcb)
  6149. prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
  6150. /*
  6151. * Writing any value to intr-mbox-0 clears PCI INTA# and
  6152. * chip-internal interrupt pending events.
  6153. * Writing non-zero to intr-mbox-0 additional tells the
  6154. * NIC to stop sending us irqs, engaging "in-intr-handler"
  6155. * event coalescing.
  6156. */
  6157. tw32_mailbox(tnapi->int_mbox, 0x00000001);
  6158. if (likely(!tg3_irq_sync(tp)))
  6159. napi_schedule(&tnapi->napi);
  6160. return IRQ_RETVAL(1);
  6161. }
  6162. static irqreturn_t tg3_interrupt(int irq, void *dev_id)
  6163. {
  6164. struct tg3_napi *tnapi = dev_id;
  6165. struct tg3 *tp = tnapi->tp;
  6166. struct tg3_hw_status *sblk = tnapi->hw_status;
  6167. unsigned int handled = 1;
  6168. /* In INTx mode, it is possible for the interrupt to arrive at
  6169. * the CPU before the status block posted prior to the interrupt.
  6170. * Reading the PCI State register will confirm whether the
  6171. * interrupt is ours and will flush the status block.
  6172. */
  6173. if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
  6174. if (tg3_flag(tp, CHIP_RESETTING) ||
  6175. (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
  6176. handled = 0;
  6177. goto out;
  6178. }
  6179. }
  6180. /*
  6181. * Writing any value to intr-mbox-0 clears PCI INTA# and
  6182. * chip-internal interrupt pending events.
  6183. * Writing non-zero to intr-mbox-0 additional tells the
  6184. * NIC to stop sending us irqs, engaging "in-intr-handler"
  6185. * event coalescing.
  6186. *
  6187. * Flush the mailbox to de-assert the IRQ immediately to prevent
  6188. * spurious interrupts. The flush impacts performance but
  6189. * excessive spurious interrupts can be worse in some cases.
  6190. */
  6191. tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
  6192. if (tg3_irq_sync(tp))
  6193. goto out;
  6194. sblk->status &= ~SD_STATUS_UPDATED;
  6195. if (likely(tg3_has_work(tnapi))) {
  6196. prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
  6197. napi_schedule(&tnapi->napi);
  6198. } else {
  6199. /* No work, shared interrupt perhaps? re-enable
  6200. * interrupts, and flush that PCI write
  6201. */
  6202. tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
  6203. 0x00000000);
  6204. }
  6205. out:
  6206. return IRQ_RETVAL(handled);
  6207. }
  6208. static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
  6209. {
  6210. struct tg3_napi *tnapi = dev_id;
  6211. struct tg3 *tp = tnapi->tp;
  6212. struct tg3_hw_status *sblk = tnapi->hw_status;
  6213. unsigned int handled = 1;
  6214. /* In INTx mode, it is possible for the interrupt to arrive at
  6215. * the CPU before the status block posted prior to the interrupt.
  6216. * Reading the PCI State register will confirm whether the
  6217. * interrupt is ours and will flush the status block.
  6218. */
  6219. if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
  6220. if (tg3_flag(tp, CHIP_RESETTING) ||
  6221. (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
  6222. handled = 0;
  6223. goto out;
  6224. }
  6225. }
  6226. /*
  6227. * writing any value to intr-mbox-0 clears PCI INTA# and
  6228. * chip-internal interrupt pending events.
  6229. * writing non-zero to intr-mbox-0 additional tells the
  6230. * NIC to stop sending us irqs, engaging "in-intr-handler"
  6231. * event coalescing.
  6232. *
  6233. * Flush the mailbox to de-assert the IRQ immediately to prevent
  6234. * spurious interrupts. The flush impacts performance but
  6235. * excessive spurious interrupts can be worse in some cases.
  6236. */
  6237. tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
  6238. /*
  6239. * In a shared interrupt configuration, sometimes other devices'
  6240. * interrupts will scream. We record the current status tag here
  6241. * so that the above check can report that the screaming interrupts
  6242. * are unhandled. Eventually they will be silenced.
  6243. */
  6244. tnapi->last_irq_tag = sblk->status_tag;
  6245. if (tg3_irq_sync(tp))
  6246. goto out;
  6247. prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
  6248. napi_schedule(&tnapi->napi);
  6249. out:
  6250. return IRQ_RETVAL(handled);
  6251. }
  6252. /* ISR for interrupt test */
  6253. static irqreturn_t tg3_test_isr(int irq, void *dev_id)
  6254. {
  6255. struct tg3_napi *tnapi = dev_id;
  6256. struct tg3 *tp = tnapi->tp;
  6257. struct tg3_hw_status *sblk = tnapi->hw_status;
  6258. if ((sblk->status & SD_STATUS_UPDATED) ||
  6259. !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
  6260. tg3_disable_ints(tp);
  6261. return IRQ_RETVAL(1);
  6262. }
  6263. return IRQ_RETVAL(0);
  6264. }
  6265. #ifdef CONFIG_NET_POLL_CONTROLLER
  6266. static void tg3_poll_controller(struct net_device *dev)
  6267. {
  6268. int i;
  6269. struct tg3 *tp = netdev_priv(dev);
  6270. if (tg3_irq_sync(tp))
  6271. return;
  6272. for (i = 0; i < tp->irq_cnt; i++)
  6273. tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
  6274. }
  6275. #endif
  6276. static void tg3_tx_timeout(struct net_device *dev)
  6277. {
  6278. struct tg3 *tp = netdev_priv(dev);
  6279. if (netif_msg_tx_err(tp)) {
  6280. netdev_err(dev, "transmit timed out, resetting\n");
  6281. tg3_dump_state(tp);
  6282. }
  6283. tg3_reset_task_schedule(tp);
  6284. }
  6285. /* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
  6286. static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
  6287. {
  6288. u32 base = (u32) mapping & 0xffffffff;
  6289. return base + len + 8 < base;
  6290. }
  6291. /* Test for TSO DMA buffers that cross into regions which are within MSS bytes
  6292. * of any 4GB boundaries: 4G, 8G, etc
  6293. */
  6294. static inline int tg3_4g_tso_overflow_test(struct tg3 *tp, dma_addr_t mapping,
  6295. u32 len, u32 mss)
  6296. {
  6297. if (tg3_asic_rev(tp) == ASIC_REV_5762 && mss) {
  6298. u32 base = (u32) mapping & 0xffffffff;
  6299. return ((base + len + (mss & 0x3fff)) < base);
  6300. }
  6301. return 0;
  6302. }
  6303. /* Test for DMA addresses > 40-bit */
  6304. static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
  6305. int len)
  6306. {
  6307. #if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
  6308. if (tg3_flag(tp, 40BIT_DMA_BUG))
  6309. return ((u64) mapping + len) > DMA_BIT_MASK(40);
  6310. return 0;
  6311. #else
  6312. return 0;
  6313. #endif
  6314. }
  6315. static inline void tg3_tx_set_bd(struct tg3_tx_buffer_desc *txbd,
  6316. dma_addr_t mapping, u32 len, u32 flags,
  6317. u32 mss, u32 vlan)
  6318. {
  6319. txbd->addr_hi = ((u64) mapping >> 32);
  6320. txbd->addr_lo = ((u64) mapping & 0xffffffff);
  6321. txbd->len_flags = (len << TXD_LEN_SHIFT) | (flags & 0x0000ffff);
  6322. txbd->vlan_tag = (mss << TXD_MSS_SHIFT) | (vlan << TXD_VLAN_TAG_SHIFT);
  6323. }
  6324. static bool tg3_tx_frag_set(struct tg3_napi *tnapi, u32 *entry, u32 *budget,
  6325. dma_addr_t map, u32 len, u32 flags,
  6326. u32 mss, u32 vlan)
  6327. {
  6328. struct tg3 *tp = tnapi->tp;
  6329. bool hwbug = false;
  6330. if (tg3_flag(tp, SHORT_DMA_BUG) && len <= 8)
  6331. hwbug = true;
  6332. if (tg3_4g_overflow_test(map, len))
  6333. hwbug = true;
  6334. if (tg3_4g_tso_overflow_test(tp, map, len, mss))
  6335. hwbug = true;
  6336. if (tg3_40bit_overflow_test(tp, map, len))
  6337. hwbug = true;
  6338. if (tp->dma_limit) {
  6339. u32 prvidx = *entry;
  6340. u32 tmp_flag = flags & ~TXD_FLAG_END;
  6341. while (len > tp->dma_limit && *budget) {
  6342. u32 frag_len = tp->dma_limit;
  6343. len -= tp->dma_limit;
  6344. /* Avoid the 8byte DMA problem */
  6345. if (len <= 8) {
  6346. len += tp->dma_limit / 2;
  6347. frag_len = tp->dma_limit / 2;
  6348. }
  6349. tnapi->tx_buffers[*entry].fragmented = true;
  6350. tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
  6351. frag_len, tmp_flag, mss, vlan);
  6352. *budget -= 1;
  6353. prvidx = *entry;
  6354. *entry = NEXT_TX(*entry);
  6355. map += frag_len;
  6356. }
  6357. if (len) {
  6358. if (*budget) {
  6359. tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
  6360. len, flags, mss, vlan);
  6361. *budget -= 1;
  6362. *entry = NEXT_TX(*entry);
  6363. } else {
  6364. hwbug = true;
  6365. tnapi->tx_buffers[prvidx].fragmented = false;
  6366. }
  6367. }
  6368. } else {
  6369. tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
  6370. len, flags, mss, vlan);
  6371. *entry = NEXT_TX(*entry);
  6372. }
  6373. return hwbug;
  6374. }
  6375. static void tg3_tx_skb_unmap(struct tg3_napi *tnapi, u32 entry, int last)
  6376. {
  6377. int i;
  6378. struct sk_buff *skb;
  6379. struct tg3_tx_ring_info *txb = &tnapi->tx_buffers[entry];
  6380. skb = txb->skb;
  6381. txb->skb = NULL;
  6382. pci_unmap_single(tnapi->tp->pdev,
  6383. dma_unmap_addr(txb, mapping),
  6384. skb_headlen(skb),
  6385. PCI_DMA_TODEVICE);
  6386. while (txb->fragmented) {
  6387. txb->fragmented = false;
  6388. entry = NEXT_TX(entry);
  6389. txb = &tnapi->tx_buffers[entry];
  6390. }
  6391. for (i = 0; i <= last; i++) {
  6392. const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  6393. entry = NEXT_TX(entry);
  6394. txb = &tnapi->tx_buffers[entry];
  6395. pci_unmap_page(tnapi->tp->pdev,
  6396. dma_unmap_addr(txb, mapping),
  6397. skb_frag_size(frag), PCI_DMA_TODEVICE);
  6398. while (txb->fragmented) {
  6399. txb->fragmented = false;
  6400. entry = NEXT_TX(entry);
  6401. txb = &tnapi->tx_buffers[entry];
  6402. }
  6403. }
  6404. }
  6405. /* Workaround 4GB and 40-bit hardware DMA bugs. */
  6406. static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
  6407. struct sk_buff **pskb,
  6408. u32 *entry, u32 *budget,
  6409. u32 base_flags, u32 mss, u32 vlan)
  6410. {
  6411. struct tg3 *tp = tnapi->tp;
  6412. struct sk_buff *new_skb, *skb = *pskb;
  6413. dma_addr_t new_addr = 0;
  6414. int ret = 0;
  6415. if (tg3_asic_rev(tp) != ASIC_REV_5701)
  6416. new_skb = skb_copy(skb, GFP_ATOMIC);
  6417. else {
  6418. int more_headroom = 4 - ((unsigned long)skb->data & 3);
  6419. new_skb = skb_copy_expand(skb,
  6420. skb_headroom(skb) + more_headroom,
  6421. skb_tailroom(skb), GFP_ATOMIC);
  6422. }
  6423. if (!new_skb) {
  6424. ret = -1;
  6425. } else {
  6426. /* New SKB is guaranteed to be linear. */
  6427. new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
  6428. PCI_DMA_TODEVICE);
  6429. /* Make sure the mapping succeeded */
  6430. if (pci_dma_mapping_error(tp->pdev, new_addr)) {
  6431. dev_kfree_skb_any(new_skb);
  6432. ret = -1;
  6433. } else {
  6434. u32 save_entry = *entry;
  6435. base_flags |= TXD_FLAG_END;
  6436. tnapi->tx_buffers[*entry].skb = new_skb;
  6437. dma_unmap_addr_set(&tnapi->tx_buffers[*entry],
  6438. mapping, new_addr);
  6439. if (tg3_tx_frag_set(tnapi, entry, budget, new_addr,
  6440. new_skb->len, base_flags,
  6441. mss, vlan)) {
  6442. tg3_tx_skb_unmap(tnapi, save_entry, -1);
  6443. dev_kfree_skb_any(new_skb);
  6444. ret = -1;
  6445. }
  6446. }
  6447. }
  6448. dev_consume_skb_any(skb);
  6449. *pskb = new_skb;
  6450. return ret;
  6451. }
  6452. static bool tg3_tso_bug_gso_check(struct tg3_napi *tnapi, struct sk_buff *skb)
  6453. {
  6454. /* Check if we will never have enough descriptors,
  6455. * as gso_segs can be more than current ring size
  6456. */
  6457. return skb_shinfo(skb)->gso_segs < tnapi->tx_pending / 3;
  6458. }
  6459. static netdev_tx_t tg3_start_xmit(struct sk_buff *, struct net_device *);
  6460. /* Use GSO to workaround all TSO packets that meet HW bug conditions
  6461. * indicated in tg3_tx_frag_set()
  6462. */
  6463. static int tg3_tso_bug(struct tg3 *tp, struct tg3_napi *tnapi,
  6464. struct netdev_queue *txq, struct sk_buff *skb)
  6465. {
  6466. struct sk_buff *segs, *nskb;
  6467. u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
  6468. /* Estimate the number of fragments in the worst case */
  6469. if (unlikely(tg3_tx_avail(tnapi) <= frag_cnt_est)) {
  6470. netif_tx_stop_queue(txq);
  6471. /* netif_tx_stop_queue() must be done before checking
  6472. * checking tx index in tg3_tx_avail() below, because in
  6473. * tg3_tx(), we update tx index before checking for
  6474. * netif_tx_queue_stopped().
  6475. */
  6476. smp_mb();
  6477. if (tg3_tx_avail(tnapi) <= frag_cnt_est)
  6478. return NETDEV_TX_BUSY;
  6479. netif_tx_wake_queue(txq);
  6480. }
  6481. segs = skb_gso_segment(skb, tp->dev->features &
  6482. ~(NETIF_F_TSO | NETIF_F_TSO6));
  6483. if (IS_ERR(segs) || !segs)
  6484. goto tg3_tso_bug_end;
  6485. do {
  6486. nskb = segs;
  6487. segs = segs->next;
  6488. nskb->next = NULL;
  6489. tg3_start_xmit(nskb, tp->dev);
  6490. } while (segs);
  6491. tg3_tso_bug_end:
  6492. dev_consume_skb_any(skb);
  6493. return NETDEV_TX_OK;
  6494. }
  6495. /* hard_start_xmit for all devices */
  6496. static netdev_tx_t tg3_start_xmit(struct sk_buff *skb, struct net_device *dev)
  6497. {
  6498. struct tg3 *tp = netdev_priv(dev);
  6499. u32 len, entry, base_flags, mss, vlan = 0;
  6500. u32 budget;
  6501. int i = -1, would_hit_hwbug;
  6502. dma_addr_t mapping;
  6503. struct tg3_napi *tnapi;
  6504. struct netdev_queue *txq;
  6505. unsigned int last;
  6506. struct iphdr *iph = NULL;
  6507. struct tcphdr *tcph = NULL;
  6508. __sum16 tcp_csum = 0, ip_csum = 0;
  6509. __be16 ip_tot_len = 0;
  6510. txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
  6511. tnapi = &tp->napi[skb_get_queue_mapping(skb)];
  6512. if (tg3_flag(tp, ENABLE_TSS))
  6513. tnapi++;
  6514. budget = tg3_tx_avail(tnapi);
  6515. /* We are running in BH disabled context with netif_tx_lock
  6516. * and TX reclaim runs via tp->napi.poll inside of a software
  6517. * interrupt. Furthermore, IRQ processing runs lockless so we have
  6518. * no IRQ context deadlocks to worry about either. Rejoice!
  6519. */
  6520. if (unlikely(budget <= (skb_shinfo(skb)->nr_frags + 1))) {
  6521. if (!netif_tx_queue_stopped(txq)) {
  6522. netif_tx_stop_queue(txq);
  6523. /* This is a hard error, log it. */
  6524. netdev_err(dev,
  6525. "BUG! Tx Ring full when queue awake!\n");
  6526. }
  6527. return NETDEV_TX_BUSY;
  6528. }
  6529. entry = tnapi->tx_prod;
  6530. base_flags = 0;
  6531. mss = skb_shinfo(skb)->gso_size;
  6532. if (mss) {
  6533. u32 tcp_opt_len, hdr_len;
  6534. if (skb_cow_head(skb, 0))
  6535. goto drop;
  6536. iph = ip_hdr(skb);
  6537. tcp_opt_len = tcp_optlen(skb);
  6538. hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb) - ETH_HLEN;
  6539. /* HW/FW can not correctly segment packets that have been
  6540. * vlan encapsulated.
  6541. */
  6542. if (skb->protocol == htons(ETH_P_8021Q) ||
  6543. skb->protocol == htons(ETH_P_8021AD)) {
  6544. if (tg3_tso_bug_gso_check(tnapi, skb))
  6545. return tg3_tso_bug(tp, tnapi, txq, skb);
  6546. goto drop;
  6547. }
  6548. if (!skb_is_gso_v6(skb)) {
  6549. if (unlikely((ETH_HLEN + hdr_len) > 80) &&
  6550. tg3_flag(tp, TSO_BUG)) {
  6551. if (tg3_tso_bug_gso_check(tnapi, skb))
  6552. return tg3_tso_bug(tp, tnapi, txq, skb);
  6553. goto drop;
  6554. }
  6555. ip_csum = iph->check;
  6556. ip_tot_len = iph->tot_len;
  6557. iph->check = 0;
  6558. iph->tot_len = htons(mss + hdr_len);
  6559. }
  6560. base_flags |= (TXD_FLAG_CPU_PRE_DMA |
  6561. TXD_FLAG_CPU_POST_DMA);
  6562. tcph = tcp_hdr(skb);
  6563. tcp_csum = tcph->check;
  6564. if (tg3_flag(tp, HW_TSO_1) ||
  6565. tg3_flag(tp, HW_TSO_2) ||
  6566. tg3_flag(tp, HW_TSO_3)) {
  6567. tcph->check = 0;
  6568. base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
  6569. } else {
  6570. tcph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
  6571. 0, IPPROTO_TCP, 0);
  6572. }
  6573. if (tg3_flag(tp, HW_TSO_3)) {
  6574. mss |= (hdr_len & 0xc) << 12;
  6575. if (hdr_len & 0x10)
  6576. base_flags |= 0x00000010;
  6577. base_flags |= (hdr_len & 0x3e0) << 5;
  6578. } else if (tg3_flag(tp, HW_TSO_2))
  6579. mss |= hdr_len << 9;
  6580. else if (tg3_flag(tp, HW_TSO_1) ||
  6581. tg3_asic_rev(tp) == ASIC_REV_5705) {
  6582. if (tcp_opt_len || iph->ihl > 5) {
  6583. int tsflags;
  6584. tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
  6585. mss |= (tsflags << 11);
  6586. }
  6587. } else {
  6588. if (tcp_opt_len || iph->ihl > 5) {
  6589. int tsflags;
  6590. tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
  6591. base_flags |= tsflags << 12;
  6592. }
  6593. }
  6594. } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
  6595. /* HW/FW can not correctly checksum packets that have been
  6596. * vlan encapsulated.
  6597. */
  6598. if (skb->protocol == htons(ETH_P_8021Q) ||
  6599. skb->protocol == htons(ETH_P_8021AD)) {
  6600. if (skb_checksum_help(skb))
  6601. goto drop;
  6602. } else {
  6603. base_flags |= TXD_FLAG_TCPUDP_CSUM;
  6604. }
  6605. }
  6606. if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
  6607. !mss && skb->len > VLAN_ETH_FRAME_LEN)
  6608. base_flags |= TXD_FLAG_JMB_PKT;
  6609. if (skb_vlan_tag_present(skb)) {
  6610. base_flags |= TXD_FLAG_VLAN;
  6611. vlan = skb_vlan_tag_get(skb);
  6612. }
  6613. if ((unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) &&
  6614. tg3_flag(tp, TX_TSTAMP_EN)) {
  6615. skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
  6616. base_flags |= TXD_FLAG_HWTSTAMP;
  6617. }
  6618. len = skb_headlen(skb);
  6619. mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
  6620. if (pci_dma_mapping_error(tp->pdev, mapping))
  6621. goto drop;
  6622. tnapi->tx_buffers[entry].skb = skb;
  6623. dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
  6624. would_hit_hwbug = 0;
  6625. if (tg3_flag(tp, 5701_DMA_BUG))
  6626. would_hit_hwbug = 1;
  6627. if (tg3_tx_frag_set(tnapi, &entry, &budget, mapping, len, base_flags |
  6628. ((skb_shinfo(skb)->nr_frags == 0) ? TXD_FLAG_END : 0),
  6629. mss, vlan)) {
  6630. would_hit_hwbug = 1;
  6631. } else if (skb_shinfo(skb)->nr_frags > 0) {
  6632. u32 tmp_mss = mss;
  6633. if (!tg3_flag(tp, HW_TSO_1) &&
  6634. !tg3_flag(tp, HW_TSO_2) &&
  6635. !tg3_flag(tp, HW_TSO_3))
  6636. tmp_mss = 0;
  6637. /* Now loop through additional data
  6638. * fragments, and queue them.
  6639. */
  6640. last = skb_shinfo(skb)->nr_frags - 1;
  6641. for (i = 0; i <= last; i++) {
  6642. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  6643. len = skb_frag_size(frag);
  6644. mapping = skb_frag_dma_map(&tp->pdev->dev, frag, 0,
  6645. len, DMA_TO_DEVICE);
  6646. tnapi->tx_buffers[entry].skb = NULL;
  6647. dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
  6648. mapping);
  6649. if (dma_mapping_error(&tp->pdev->dev, mapping))
  6650. goto dma_error;
  6651. if (!budget ||
  6652. tg3_tx_frag_set(tnapi, &entry, &budget, mapping,
  6653. len, base_flags |
  6654. ((i == last) ? TXD_FLAG_END : 0),
  6655. tmp_mss, vlan)) {
  6656. would_hit_hwbug = 1;
  6657. break;
  6658. }
  6659. }
  6660. }
  6661. if (would_hit_hwbug) {
  6662. tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, i);
  6663. if (mss && tg3_tso_bug_gso_check(tnapi, skb)) {
  6664. /* If it's a TSO packet, do GSO instead of
  6665. * allocating and copying to a large linear SKB
  6666. */
  6667. if (ip_tot_len) {
  6668. iph->check = ip_csum;
  6669. iph->tot_len = ip_tot_len;
  6670. }
  6671. tcph->check = tcp_csum;
  6672. return tg3_tso_bug(tp, tnapi, txq, skb);
  6673. }
  6674. /* If the workaround fails due to memory/mapping
  6675. * failure, silently drop this packet.
  6676. */
  6677. entry = tnapi->tx_prod;
  6678. budget = tg3_tx_avail(tnapi);
  6679. if (tigon3_dma_hwbug_workaround(tnapi, &skb, &entry, &budget,
  6680. base_flags, mss, vlan))
  6681. goto drop_nofree;
  6682. }
  6683. skb_tx_timestamp(skb);
  6684. netdev_tx_sent_queue(txq, skb->len);
  6685. /* Sync BD data before updating mailbox */
  6686. wmb();
  6687. tnapi->tx_prod = entry;
  6688. if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
  6689. netif_tx_stop_queue(txq);
  6690. /* netif_tx_stop_queue() must be done before checking
  6691. * checking tx index in tg3_tx_avail() below, because in
  6692. * tg3_tx(), we update tx index before checking for
  6693. * netif_tx_queue_stopped().
  6694. */
  6695. smp_mb();
  6696. if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
  6697. netif_tx_wake_queue(txq);
  6698. }
  6699. if (!skb->xmit_more || netif_xmit_stopped(txq)) {
  6700. /* Packets are ready, update Tx producer idx on card. */
  6701. tw32_tx_mbox(tnapi->prodmbox, entry);
  6702. mmiowb();
  6703. }
  6704. return NETDEV_TX_OK;
  6705. dma_error:
  6706. tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, --i);
  6707. tnapi->tx_buffers[tnapi->tx_prod].skb = NULL;
  6708. drop:
  6709. dev_kfree_skb_any(skb);
  6710. drop_nofree:
  6711. tp->tx_dropped++;
  6712. return NETDEV_TX_OK;
  6713. }
  6714. static void tg3_mac_loopback(struct tg3 *tp, bool enable)
  6715. {
  6716. if (enable) {
  6717. tp->mac_mode &= ~(MAC_MODE_HALF_DUPLEX |
  6718. MAC_MODE_PORT_MODE_MASK);
  6719. tp->mac_mode |= MAC_MODE_PORT_INT_LPBACK;
  6720. if (!tg3_flag(tp, 5705_PLUS))
  6721. tp->mac_mode |= MAC_MODE_LINK_POLARITY;
  6722. if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
  6723. tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
  6724. else
  6725. tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
  6726. } else {
  6727. tp->mac_mode &= ~MAC_MODE_PORT_INT_LPBACK;
  6728. if (tg3_flag(tp, 5705_PLUS) ||
  6729. (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) ||
  6730. tg3_asic_rev(tp) == ASIC_REV_5700)
  6731. tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
  6732. }
  6733. tw32(MAC_MODE, tp->mac_mode);
  6734. udelay(40);
  6735. }
  6736. static int tg3_phy_lpbk_set(struct tg3 *tp, u32 speed, bool extlpbk)
  6737. {
  6738. u32 val, bmcr, mac_mode, ptest = 0;
  6739. tg3_phy_toggle_apd(tp, false);
  6740. tg3_phy_toggle_automdix(tp, false);
  6741. if (extlpbk && tg3_phy_set_extloopbk(tp))
  6742. return -EIO;
  6743. bmcr = BMCR_FULLDPLX;
  6744. switch (speed) {
  6745. case SPEED_10:
  6746. break;
  6747. case SPEED_100:
  6748. bmcr |= BMCR_SPEED100;
  6749. break;
  6750. case SPEED_1000:
  6751. default:
  6752. if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
  6753. speed = SPEED_100;
  6754. bmcr |= BMCR_SPEED100;
  6755. } else {
  6756. speed = SPEED_1000;
  6757. bmcr |= BMCR_SPEED1000;
  6758. }
  6759. }
  6760. if (extlpbk) {
  6761. if (!(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
  6762. tg3_readphy(tp, MII_CTRL1000, &val);
  6763. val |= CTL1000_AS_MASTER |
  6764. CTL1000_ENABLE_MASTER;
  6765. tg3_writephy(tp, MII_CTRL1000, val);
  6766. } else {
  6767. ptest = MII_TG3_FET_PTEST_TRIM_SEL |
  6768. MII_TG3_FET_PTEST_TRIM_2;
  6769. tg3_writephy(tp, MII_TG3_FET_PTEST, ptest);
  6770. }
  6771. } else
  6772. bmcr |= BMCR_LOOPBACK;
  6773. tg3_writephy(tp, MII_BMCR, bmcr);
  6774. /* The write needs to be flushed for the FETs */
  6775. if (tp->phy_flags & TG3_PHYFLG_IS_FET)
  6776. tg3_readphy(tp, MII_BMCR, &bmcr);
  6777. udelay(40);
  6778. if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
  6779. tg3_asic_rev(tp) == ASIC_REV_5785) {
  6780. tg3_writephy(tp, MII_TG3_FET_PTEST, ptest |
  6781. MII_TG3_FET_PTEST_FRC_TX_LINK |
  6782. MII_TG3_FET_PTEST_FRC_TX_LOCK);
  6783. /* The write needs to be flushed for the AC131 */
  6784. tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
  6785. }
  6786. /* Reset to prevent losing 1st rx packet intermittently */
  6787. if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
  6788. tg3_flag(tp, 5780_CLASS)) {
  6789. tw32_f(MAC_RX_MODE, RX_MODE_RESET);
  6790. udelay(10);
  6791. tw32_f(MAC_RX_MODE, tp->rx_mode);
  6792. }
  6793. mac_mode = tp->mac_mode &
  6794. ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
  6795. if (speed == SPEED_1000)
  6796. mac_mode |= MAC_MODE_PORT_MODE_GMII;
  6797. else
  6798. mac_mode |= MAC_MODE_PORT_MODE_MII;
  6799. if (tg3_asic_rev(tp) == ASIC_REV_5700) {
  6800. u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
  6801. if (masked_phy_id == TG3_PHY_ID_BCM5401)
  6802. mac_mode &= ~MAC_MODE_LINK_POLARITY;
  6803. else if (masked_phy_id == TG3_PHY_ID_BCM5411)
  6804. mac_mode |= MAC_MODE_LINK_POLARITY;
  6805. tg3_writephy(tp, MII_TG3_EXT_CTRL,
  6806. MII_TG3_EXT_CTRL_LNK3_LED_MODE);
  6807. }
  6808. tw32(MAC_MODE, mac_mode);
  6809. udelay(40);
  6810. return 0;
  6811. }
  6812. static void tg3_set_loopback(struct net_device *dev, netdev_features_t features)
  6813. {
  6814. struct tg3 *tp = netdev_priv(dev);
  6815. if (features & NETIF_F_LOOPBACK) {
  6816. if (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK)
  6817. return;
  6818. spin_lock_bh(&tp->lock);
  6819. tg3_mac_loopback(tp, true);
  6820. netif_carrier_on(tp->dev);
  6821. spin_unlock_bh(&tp->lock);
  6822. netdev_info(dev, "Internal MAC loopback mode enabled.\n");
  6823. } else {
  6824. if (!(tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
  6825. return;
  6826. spin_lock_bh(&tp->lock);
  6827. tg3_mac_loopback(tp, false);
  6828. /* Force link status check */
  6829. tg3_setup_phy(tp, true);
  6830. spin_unlock_bh(&tp->lock);
  6831. netdev_info(dev, "Internal MAC loopback mode disabled.\n");
  6832. }
  6833. }
  6834. static netdev_features_t tg3_fix_features(struct net_device *dev,
  6835. netdev_features_t features)
  6836. {
  6837. struct tg3 *tp = netdev_priv(dev);
  6838. if (dev->mtu > ETH_DATA_LEN && tg3_flag(tp, 5780_CLASS))
  6839. features &= ~NETIF_F_ALL_TSO;
  6840. return features;
  6841. }
  6842. static int tg3_set_features(struct net_device *dev, netdev_features_t features)
  6843. {
  6844. netdev_features_t changed = dev->features ^ features;
  6845. if ((changed & NETIF_F_LOOPBACK) && netif_running(dev))
  6846. tg3_set_loopback(dev, features);
  6847. return 0;
  6848. }
  6849. static void tg3_rx_prodring_free(struct tg3 *tp,
  6850. struct tg3_rx_prodring_set *tpr)
  6851. {
  6852. int i;
  6853. if (tpr != &tp->napi[0].prodring) {
  6854. for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
  6855. i = (i + 1) & tp->rx_std_ring_mask)
  6856. tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
  6857. tp->rx_pkt_map_sz);
  6858. if (tg3_flag(tp, JUMBO_CAPABLE)) {
  6859. for (i = tpr->rx_jmb_cons_idx;
  6860. i != tpr->rx_jmb_prod_idx;
  6861. i = (i + 1) & tp->rx_jmb_ring_mask) {
  6862. tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
  6863. TG3_RX_JMB_MAP_SZ);
  6864. }
  6865. }
  6866. return;
  6867. }
  6868. for (i = 0; i <= tp->rx_std_ring_mask; i++)
  6869. tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
  6870. tp->rx_pkt_map_sz);
  6871. if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
  6872. for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
  6873. tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
  6874. TG3_RX_JMB_MAP_SZ);
  6875. }
  6876. }
  6877. /* Initialize rx rings for packet processing.
  6878. *
  6879. * The chip has been shut down and the driver detached from
  6880. * the networking, so no interrupts or new tx packets will
  6881. * end up in the driver. tp->{tx,}lock are held and thus
  6882. * we may not sleep.
  6883. */
  6884. static int tg3_rx_prodring_alloc(struct tg3 *tp,
  6885. struct tg3_rx_prodring_set *tpr)
  6886. {
  6887. u32 i, rx_pkt_dma_sz;
  6888. tpr->rx_std_cons_idx = 0;
  6889. tpr->rx_std_prod_idx = 0;
  6890. tpr->rx_jmb_cons_idx = 0;
  6891. tpr->rx_jmb_prod_idx = 0;
  6892. if (tpr != &tp->napi[0].prodring) {
  6893. memset(&tpr->rx_std_buffers[0], 0,
  6894. TG3_RX_STD_BUFF_RING_SIZE(tp));
  6895. if (tpr->rx_jmb_buffers)
  6896. memset(&tpr->rx_jmb_buffers[0], 0,
  6897. TG3_RX_JMB_BUFF_RING_SIZE(tp));
  6898. goto done;
  6899. }
  6900. /* Zero out all descriptors. */
  6901. memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
  6902. rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
  6903. if (tg3_flag(tp, 5780_CLASS) &&
  6904. tp->dev->mtu > ETH_DATA_LEN)
  6905. rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
  6906. tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
  6907. /* Initialize invariants of the rings, we only set this
  6908. * stuff once. This works because the card does not
  6909. * write into the rx buffer posting rings.
  6910. */
  6911. for (i = 0; i <= tp->rx_std_ring_mask; i++) {
  6912. struct tg3_rx_buffer_desc *rxd;
  6913. rxd = &tpr->rx_std[i];
  6914. rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
  6915. rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
  6916. rxd->opaque = (RXD_OPAQUE_RING_STD |
  6917. (i << RXD_OPAQUE_INDEX_SHIFT));
  6918. }
  6919. /* Now allocate fresh SKBs for each rx ring. */
  6920. for (i = 0; i < tp->rx_pending; i++) {
  6921. unsigned int frag_size;
  6922. if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_STD, i,
  6923. &frag_size) < 0) {
  6924. netdev_warn(tp->dev,
  6925. "Using a smaller RX standard ring. Only "
  6926. "%d out of %d buffers were allocated "
  6927. "successfully\n", i, tp->rx_pending);
  6928. if (i == 0)
  6929. goto initfail;
  6930. tp->rx_pending = i;
  6931. break;
  6932. }
  6933. }
  6934. if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
  6935. goto done;
  6936. memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
  6937. if (!tg3_flag(tp, JUMBO_RING_ENABLE))
  6938. goto done;
  6939. for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
  6940. struct tg3_rx_buffer_desc *rxd;
  6941. rxd = &tpr->rx_jmb[i].std;
  6942. rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
  6943. rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
  6944. RXD_FLAG_JUMBO;
  6945. rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
  6946. (i << RXD_OPAQUE_INDEX_SHIFT));
  6947. }
  6948. for (i = 0; i < tp->rx_jumbo_pending; i++) {
  6949. unsigned int frag_size;
  6950. if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_JUMBO, i,
  6951. &frag_size) < 0) {
  6952. netdev_warn(tp->dev,
  6953. "Using a smaller RX jumbo ring. Only %d "
  6954. "out of %d buffers were allocated "
  6955. "successfully\n", i, tp->rx_jumbo_pending);
  6956. if (i == 0)
  6957. goto initfail;
  6958. tp->rx_jumbo_pending = i;
  6959. break;
  6960. }
  6961. }
  6962. done:
  6963. return 0;
  6964. initfail:
  6965. tg3_rx_prodring_free(tp, tpr);
  6966. return -ENOMEM;
  6967. }
  6968. static void tg3_rx_prodring_fini(struct tg3 *tp,
  6969. struct tg3_rx_prodring_set *tpr)
  6970. {
  6971. kfree(tpr->rx_std_buffers);
  6972. tpr->rx_std_buffers = NULL;
  6973. kfree(tpr->rx_jmb_buffers);
  6974. tpr->rx_jmb_buffers = NULL;
  6975. if (tpr->rx_std) {
  6976. dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
  6977. tpr->rx_std, tpr->rx_std_mapping);
  6978. tpr->rx_std = NULL;
  6979. }
  6980. if (tpr->rx_jmb) {
  6981. dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
  6982. tpr->rx_jmb, tpr->rx_jmb_mapping);
  6983. tpr->rx_jmb = NULL;
  6984. }
  6985. }
  6986. static int tg3_rx_prodring_init(struct tg3 *tp,
  6987. struct tg3_rx_prodring_set *tpr)
  6988. {
  6989. tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
  6990. GFP_KERNEL);
  6991. if (!tpr->rx_std_buffers)
  6992. return -ENOMEM;
  6993. tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
  6994. TG3_RX_STD_RING_BYTES(tp),
  6995. &tpr->rx_std_mapping,
  6996. GFP_KERNEL);
  6997. if (!tpr->rx_std)
  6998. goto err_out;
  6999. if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
  7000. tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
  7001. GFP_KERNEL);
  7002. if (!tpr->rx_jmb_buffers)
  7003. goto err_out;
  7004. tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
  7005. TG3_RX_JMB_RING_BYTES(tp),
  7006. &tpr->rx_jmb_mapping,
  7007. GFP_KERNEL);
  7008. if (!tpr->rx_jmb)
  7009. goto err_out;
  7010. }
  7011. return 0;
  7012. err_out:
  7013. tg3_rx_prodring_fini(tp, tpr);
  7014. return -ENOMEM;
  7015. }
  7016. /* Free up pending packets in all rx/tx rings.
  7017. *
  7018. * The chip has been shut down and the driver detached from
  7019. * the networking, so no interrupts or new tx packets will
  7020. * end up in the driver. tp->{tx,}lock is not held and we are not
  7021. * in an interrupt context and thus may sleep.
  7022. */
  7023. static void tg3_free_rings(struct tg3 *tp)
  7024. {
  7025. int i, j;
  7026. for (j = 0; j < tp->irq_cnt; j++) {
  7027. struct tg3_napi *tnapi = &tp->napi[j];
  7028. tg3_rx_prodring_free(tp, &tnapi->prodring);
  7029. if (!tnapi->tx_buffers)
  7030. continue;
  7031. for (i = 0; i < TG3_TX_RING_SIZE; i++) {
  7032. struct sk_buff *skb = tnapi->tx_buffers[i].skb;
  7033. if (!skb)
  7034. continue;
  7035. tg3_tx_skb_unmap(tnapi, i,
  7036. skb_shinfo(skb)->nr_frags - 1);
  7037. dev_consume_skb_any(skb);
  7038. }
  7039. netdev_tx_reset_queue(netdev_get_tx_queue(tp->dev, j));
  7040. }
  7041. }
  7042. /* Initialize tx/rx rings for packet processing.
  7043. *
  7044. * The chip has been shut down and the driver detached from
  7045. * the networking, so no interrupts or new tx packets will
  7046. * end up in the driver. tp->{tx,}lock are held and thus
  7047. * we may not sleep.
  7048. */
  7049. static int tg3_init_rings(struct tg3 *tp)
  7050. {
  7051. int i;
  7052. /* Free up all the SKBs. */
  7053. tg3_free_rings(tp);
  7054. for (i = 0; i < tp->irq_cnt; i++) {
  7055. struct tg3_napi *tnapi = &tp->napi[i];
  7056. tnapi->last_tag = 0;
  7057. tnapi->last_irq_tag = 0;
  7058. tnapi->hw_status->status = 0;
  7059. tnapi->hw_status->status_tag = 0;
  7060. memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
  7061. tnapi->tx_prod = 0;
  7062. tnapi->tx_cons = 0;
  7063. if (tnapi->tx_ring)
  7064. memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
  7065. tnapi->rx_rcb_ptr = 0;
  7066. if (tnapi->rx_rcb)
  7067. memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
  7068. if (tnapi->prodring.rx_std &&
  7069. tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
  7070. tg3_free_rings(tp);
  7071. return -ENOMEM;
  7072. }
  7073. }
  7074. return 0;
  7075. }
  7076. static void tg3_mem_tx_release(struct tg3 *tp)
  7077. {
  7078. int i;
  7079. for (i = 0; i < tp->irq_max; i++) {
  7080. struct tg3_napi *tnapi = &tp->napi[i];
  7081. if (tnapi->tx_ring) {
  7082. dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
  7083. tnapi->tx_ring, tnapi->tx_desc_mapping);
  7084. tnapi->tx_ring = NULL;
  7085. }
  7086. kfree(tnapi->tx_buffers);
  7087. tnapi->tx_buffers = NULL;
  7088. }
  7089. }
  7090. static int tg3_mem_tx_acquire(struct tg3 *tp)
  7091. {
  7092. int i;
  7093. struct tg3_napi *tnapi = &tp->napi[0];
  7094. /* If multivector TSS is enabled, vector 0 does not handle
  7095. * tx interrupts. Don't allocate any resources for it.
  7096. */
  7097. if (tg3_flag(tp, ENABLE_TSS))
  7098. tnapi++;
  7099. for (i = 0; i < tp->txq_cnt; i++, tnapi++) {
  7100. tnapi->tx_buffers = kcalloc(TG3_TX_RING_SIZE,
  7101. sizeof(struct tg3_tx_ring_info),
  7102. GFP_KERNEL);
  7103. if (!tnapi->tx_buffers)
  7104. goto err_out;
  7105. tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
  7106. TG3_TX_RING_BYTES,
  7107. &tnapi->tx_desc_mapping,
  7108. GFP_KERNEL);
  7109. if (!tnapi->tx_ring)
  7110. goto err_out;
  7111. }
  7112. return 0;
  7113. err_out:
  7114. tg3_mem_tx_release(tp);
  7115. return -ENOMEM;
  7116. }
  7117. static void tg3_mem_rx_release(struct tg3 *tp)
  7118. {
  7119. int i;
  7120. for (i = 0; i < tp->irq_max; i++) {
  7121. struct tg3_napi *tnapi = &tp->napi[i];
  7122. tg3_rx_prodring_fini(tp, &tnapi->prodring);
  7123. if (!tnapi->rx_rcb)
  7124. continue;
  7125. dma_free_coherent(&tp->pdev->dev,
  7126. TG3_RX_RCB_RING_BYTES(tp),
  7127. tnapi->rx_rcb,
  7128. tnapi->rx_rcb_mapping);
  7129. tnapi->rx_rcb = NULL;
  7130. }
  7131. }
  7132. static int tg3_mem_rx_acquire(struct tg3 *tp)
  7133. {
  7134. unsigned int i, limit;
  7135. limit = tp->rxq_cnt;
  7136. /* If RSS is enabled, we need a (dummy) producer ring
  7137. * set on vector zero. This is the true hw prodring.
  7138. */
  7139. if (tg3_flag(tp, ENABLE_RSS))
  7140. limit++;
  7141. for (i = 0; i < limit; i++) {
  7142. struct tg3_napi *tnapi = &tp->napi[i];
  7143. if (tg3_rx_prodring_init(tp, &tnapi->prodring))
  7144. goto err_out;
  7145. /* If multivector RSS is enabled, vector 0
  7146. * does not handle rx or tx interrupts.
  7147. * Don't allocate any resources for it.
  7148. */
  7149. if (!i && tg3_flag(tp, ENABLE_RSS))
  7150. continue;
  7151. tnapi->rx_rcb = dma_zalloc_coherent(&tp->pdev->dev,
  7152. TG3_RX_RCB_RING_BYTES(tp),
  7153. &tnapi->rx_rcb_mapping,
  7154. GFP_KERNEL);
  7155. if (!tnapi->rx_rcb)
  7156. goto err_out;
  7157. }
  7158. return 0;
  7159. err_out:
  7160. tg3_mem_rx_release(tp);
  7161. return -ENOMEM;
  7162. }
  7163. /*
  7164. * Must not be invoked with interrupt sources disabled and
  7165. * the hardware shutdown down.
  7166. */
  7167. static void tg3_free_consistent(struct tg3 *tp)
  7168. {
  7169. int i;
  7170. for (i = 0; i < tp->irq_cnt; i++) {
  7171. struct tg3_napi *tnapi = &tp->napi[i];
  7172. if (tnapi->hw_status) {
  7173. dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
  7174. tnapi->hw_status,
  7175. tnapi->status_mapping);
  7176. tnapi->hw_status = NULL;
  7177. }
  7178. }
  7179. tg3_mem_rx_release(tp);
  7180. tg3_mem_tx_release(tp);
  7181. /* tp->hw_stats can be referenced safely:
  7182. * 1. under rtnl_lock
  7183. * 2. or under tp->lock if TG3_FLAG_INIT_COMPLETE is set.
  7184. */
  7185. if (tp->hw_stats) {
  7186. dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
  7187. tp->hw_stats, tp->stats_mapping);
  7188. tp->hw_stats = NULL;
  7189. }
  7190. }
  7191. /*
  7192. * Must not be invoked with interrupt sources disabled and
  7193. * the hardware shutdown down. Can sleep.
  7194. */
  7195. static int tg3_alloc_consistent(struct tg3 *tp)
  7196. {
  7197. int i;
  7198. tp->hw_stats = dma_zalloc_coherent(&tp->pdev->dev,
  7199. sizeof(struct tg3_hw_stats),
  7200. &tp->stats_mapping, GFP_KERNEL);
  7201. if (!tp->hw_stats)
  7202. goto err_out;
  7203. for (i = 0; i < tp->irq_cnt; i++) {
  7204. struct tg3_napi *tnapi = &tp->napi[i];
  7205. struct tg3_hw_status *sblk;
  7206. tnapi->hw_status = dma_zalloc_coherent(&tp->pdev->dev,
  7207. TG3_HW_STATUS_SIZE,
  7208. &tnapi->status_mapping,
  7209. GFP_KERNEL);
  7210. if (!tnapi->hw_status)
  7211. goto err_out;
  7212. sblk = tnapi->hw_status;
  7213. if (tg3_flag(tp, ENABLE_RSS)) {
  7214. u16 *prodptr = NULL;
  7215. /*
  7216. * When RSS is enabled, the status block format changes
  7217. * slightly. The "rx_jumbo_consumer", "reserved",
  7218. * and "rx_mini_consumer" members get mapped to the
  7219. * other three rx return ring producer indexes.
  7220. */
  7221. switch (i) {
  7222. case 1:
  7223. prodptr = &sblk->idx[0].rx_producer;
  7224. break;
  7225. case 2:
  7226. prodptr = &sblk->rx_jumbo_consumer;
  7227. break;
  7228. case 3:
  7229. prodptr = &sblk->reserved;
  7230. break;
  7231. case 4:
  7232. prodptr = &sblk->rx_mini_consumer;
  7233. break;
  7234. }
  7235. tnapi->rx_rcb_prod_idx = prodptr;
  7236. } else {
  7237. tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
  7238. }
  7239. }
  7240. if (tg3_mem_tx_acquire(tp) || tg3_mem_rx_acquire(tp))
  7241. goto err_out;
  7242. return 0;
  7243. err_out:
  7244. tg3_free_consistent(tp);
  7245. return -ENOMEM;
  7246. }
  7247. #define MAX_WAIT_CNT 1000
  7248. /* To stop a block, clear the enable bit and poll till it
  7249. * clears. tp->lock is held.
  7250. */
  7251. static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, bool silent)
  7252. {
  7253. unsigned int i;
  7254. u32 val;
  7255. if (tg3_flag(tp, 5705_PLUS)) {
  7256. switch (ofs) {
  7257. case RCVLSC_MODE:
  7258. case DMAC_MODE:
  7259. case MBFREE_MODE:
  7260. case BUFMGR_MODE:
  7261. case MEMARB_MODE:
  7262. /* We can't enable/disable these bits of the
  7263. * 5705/5750, just say success.
  7264. */
  7265. return 0;
  7266. default:
  7267. break;
  7268. }
  7269. }
  7270. val = tr32(ofs);
  7271. val &= ~enable_bit;
  7272. tw32_f(ofs, val);
  7273. for (i = 0; i < MAX_WAIT_CNT; i++) {
  7274. if (pci_channel_offline(tp->pdev)) {
  7275. dev_err(&tp->pdev->dev,
  7276. "tg3_stop_block device offline, "
  7277. "ofs=%lx enable_bit=%x\n",
  7278. ofs, enable_bit);
  7279. return -ENODEV;
  7280. }
  7281. udelay(100);
  7282. val = tr32(ofs);
  7283. if ((val & enable_bit) == 0)
  7284. break;
  7285. }
  7286. if (i == MAX_WAIT_CNT && !silent) {
  7287. dev_err(&tp->pdev->dev,
  7288. "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
  7289. ofs, enable_bit);
  7290. return -ENODEV;
  7291. }
  7292. return 0;
  7293. }
  7294. /* tp->lock is held. */
  7295. static int tg3_abort_hw(struct tg3 *tp, bool silent)
  7296. {
  7297. int i, err;
  7298. tg3_disable_ints(tp);
  7299. if (pci_channel_offline(tp->pdev)) {
  7300. tp->rx_mode &= ~(RX_MODE_ENABLE | TX_MODE_ENABLE);
  7301. tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
  7302. err = -ENODEV;
  7303. goto err_no_dev;
  7304. }
  7305. tp->rx_mode &= ~RX_MODE_ENABLE;
  7306. tw32_f(MAC_RX_MODE, tp->rx_mode);
  7307. udelay(10);
  7308. err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
  7309. err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
  7310. err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
  7311. err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
  7312. err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
  7313. err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
  7314. err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
  7315. err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
  7316. err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
  7317. err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
  7318. err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
  7319. err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
  7320. err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
  7321. tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
  7322. tw32_f(MAC_MODE, tp->mac_mode);
  7323. udelay(40);
  7324. tp->tx_mode &= ~TX_MODE_ENABLE;
  7325. tw32_f(MAC_TX_MODE, tp->tx_mode);
  7326. for (i = 0; i < MAX_WAIT_CNT; i++) {
  7327. udelay(100);
  7328. if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
  7329. break;
  7330. }
  7331. if (i >= MAX_WAIT_CNT) {
  7332. dev_err(&tp->pdev->dev,
  7333. "%s timed out, TX_MODE_ENABLE will not clear "
  7334. "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
  7335. err |= -ENODEV;
  7336. }
  7337. err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
  7338. err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
  7339. err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
  7340. tw32(FTQ_RESET, 0xffffffff);
  7341. tw32(FTQ_RESET, 0x00000000);
  7342. err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
  7343. err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
  7344. err_no_dev:
  7345. for (i = 0; i < tp->irq_cnt; i++) {
  7346. struct tg3_napi *tnapi = &tp->napi[i];
  7347. if (tnapi->hw_status)
  7348. memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
  7349. }
  7350. return err;
  7351. }
  7352. /* Save PCI command register before chip reset */
  7353. static void tg3_save_pci_state(struct tg3 *tp)
  7354. {
  7355. pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
  7356. }
  7357. /* Restore PCI state after chip reset */
  7358. static void tg3_restore_pci_state(struct tg3 *tp)
  7359. {
  7360. u32 val;
  7361. /* Re-enable indirect register accesses. */
  7362. pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
  7363. tp->misc_host_ctrl);
  7364. /* Set MAX PCI retry to zero. */
  7365. val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
  7366. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0 &&
  7367. tg3_flag(tp, PCIX_MODE))
  7368. val |= PCISTATE_RETRY_SAME_DMA;
  7369. /* Allow reads and writes to the APE register and memory space. */
  7370. if (tg3_flag(tp, ENABLE_APE))
  7371. val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
  7372. PCISTATE_ALLOW_APE_SHMEM_WR |
  7373. PCISTATE_ALLOW_APE_PSPACE_WR;
  7374. pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
  7375. pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
  7376. if (!tg3_flag(tp, PCI_EXPRESS)) {
  7377. pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
  7378. tp->pci_cacheline_sz);
  7379. pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
  7380. tp->pci_lat_timer);
  7381. }
  7382. /* Make sure PCI-X relaxed ordering bit is clear. */
  7383. if (tg3_flag(tp, PCIX_MODE)) {
  7384. u16 pcix_cmd;
  7385. pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
  7386. &pcix_cmd);
  7387. pcix_cmd &= ~PCI_X_CMD_ERO;
  7388. pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
  7389. pcix_cmd);
  7390. }
  7391. if (tg3_flag(tp, 5780_CLASS)) {
  7392. /* Chip reset on 5780 will reset MSI enable bit,
  7393. * so need to restore it.
  7394. */
  7395. if (tg3_flag(tp, USING_MSI)) {
  7396. u16 ctrl;
  7397. pci_read_config_word(tp->pdev,
  7398. tp->msi_cap + PCI_MSI_FLAGS,
  7399. &ctrl);
  7400. pci_write_config_word(tp->pdev,
  7401. tp->msi_cap + PCI_MSI_FLAGS,
  7402. ctrl | PCI_MSI_FLAGS_ENABLE);
  7403. val = tr32(MSGINT_MODE);
  7404. tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
  7405. }
  7406. }
  7407. }
  7408. static void tg3_override_clk(struct tg3 *tp)
  7409. {
  7410. u32 val;
  7411. switch (tg3_asic_rev(tp)) {
  7412. case ASIC_REV_5717:
  7413. val = tr32(TG3_CPMU_CLCK_ORIDE_ENABLE);
  7414. tw32(TG3_CPMU_CLCK_ORIDE_ENABLE, val |
  7415. TG3_CPMU_MAC_ORIDE_ENABLE);
  7416. break;
  7417. case ASIC_REV_5719:
  7418. case ASIC_REV_5720:
  7419. tw32(TG3_CPMU_CLCK_ORIDE, CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
  7420. break;
  7421. default:
  7422. return;
  7423. }
  7424. }
  7425. static void tg3_restore_clk(struct tg3 *tp)
  7426. {
  7427. u32 val;
  7428. switch (tg3_asic_rev(tp)) {
  7429. case ASIC_REV_5717:
  7430. val = tr32(TG3_CPMU_CLCK_ORIDE_ENABLE);
  7431. tw32(TG3_CPMU_CLCK_ORIDE_ENABLE,
  7432. val & ~TG3_CPMU_MAC_ORIDE_ENABLE);
  7433. break;
  7434. case ASIC_REV_5719:
  7435. case ASIC_REV_5720:
  7436. val = tr32(TG3_CPMU_CLCK_ORIDE);
  7437. tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
  7438. break;
  7439. default:
  7440. return;
  7441. }
  7442. }
  7443. /* tp->lock is held. */
  7444. static int tg3_chip_reset(struct tg3 *tp)
  7445. __releases(tp->lock)
  7446. __acquires(tp->lock)
  7447. {
  7448. u32 val;
  7449. void (*write_op)(struct tg3 *, u32, u32);
  7450. int i, err;
  7451. if (!pci_device_is_present(tp->pdev))
  7452. return -ENODEV;
  7453. tg3_nvram_lock(tp);
  7454. tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
  7455. /* No matching tg3_nvram_unlock() after this because
  7456. * chip reset below will undo the nvram lock.
  7457. */
  7458. tp->nvram_lock_cnt = 0;
  7459. /* GRC_MISC_CFG core clock reset will clear the memory
  7460. * enable bit in PCI register 4 and the MSI enable bit
  7461. * on some chips, so we save relevant registers here.
  7462. */
  7463. tg3_save_pci_state(tp);
  7464. if (tg3_asic_rev(tp) == ASIC_REV_5752 ||
  7465. tg3_flag(tp, 5755_PLUS))
  7466. tw32(GRC_FASTBOOT_PC, 0);
  7467. /*
  7468. * We must avoid the readl() that normally takes place.
  7469. * It locks machines, causes machine checks, and other
  7470. * fun things. So, temporarily disable the 5701
  7471. * hardware workaround, while we do the reset.
  7472. */
  7473. write_op = tp->write32;
  7474. if (write_op == tg3_write_flush_reg32)
  7475. tp->write32 = tg3_write32;
  7476. /* Prevent the irq handler from reading or writing PCI registers
  7477. * during chip reset when the memory enable bit in the PCI command
  7478. * register may be cleared. The chip does not generate interrupt
  7479. * at this time, but the irq handler may still be called due to irq
  7480. * sharing or irqpoll.
  7481. */
  7482. tg3_flag_set(tp, CHIP_RESETTING);
  7483. for (i = 0; i < tp->irq_cnt; i++) {
  7484. struct tg3_napi *tnapi = &tp->napi[i];
  7485. if (tnapi->hw_status) {
  7486. tnapi->hw_status->status = 0;
  7487. tnapi->hw_status->status_tag = 0;
  7488. }
  7489. tnapi->last_tag = 0;
  7490. tnapi->last_irq_tag = 0;
  7491. }
  7492. smp_mb();
  7493. tg3_full_unlock(tp);
  7494. for (i = 0; i < tp->irq_cnt; i++)
  7495. synchronize_irq(tp->napi[i].irq_vec);
  7496. tg3_full_lock(tp, 0);
  7497. if (tg3_asic_rev(tp) == ASIC_REV_57780) {
  7498. val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
  7499. tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
  7500. }
  7501. /* do the reset */
  7502. val = GRC_MISC_CFG_CORECLK_RESET;
  7503. if (tg3_flag(tp, PCI_EXPRESS)) {
  7504. /* Force PCIe 1.0a mode */
  7505. if (tg3_asic_rev(tp) != ASIC_REV_5785 &&
  7506. !tg3_flag(tp, 57765_PLUS) &&
  7507. tr32(TG3_PCIE_PHY_TSTCTL) ==
  7508. (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
  7509. tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
  7510. if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0) {
  7511. tw32(GRC_MISC_CFG, (1 << 29));
  7512. val |= (1 << 29);
  7513. }
  7514. }
  7515. if (tg3_asic_rev(tp) == ASIC_REV_5906) {
  7516. tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
  7517. tw32(GRC_VCPU_EXT_CTRL,
  7518. tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
  7519. }
  7520. /* Set the clock to the highest frequency to avoid timeouts. With link
  7521. * aware mode, the clock speed could be slow and bootcode does not
  7522. * complete within the expected time. Override the clock to allow the
  7523. * bootcode to finish sooner and then restore it.
  7524. */
  7525. tg3_override_clk(tp);
  7526. /* Manage gphy power for all CPMU absent PCIe devices. */
  7527. if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, CPMU_PRESENT))
  7528. val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
  7529. tw32(GRC_MISC_CFG, val);
  7530. /* restore 5701 hardware bug workaround write method */
  7531. tp->write32 = write_op;
  7532. /* Unfortunately, we have to delay before the PCI read back.
  7533. * Some 575X chips even will not respond to a PCI cfg access
  7534. * when the reset command is given to the chip.
  7535. *
  7536. * How do these hardware designers expect things to work
  7537. * properly if the PCI write is posted for a long period
  7538. * of time? It is always necessary to have some method by
  7539. * which a register read back can occur to push the write
  7540. * out which does the reset.
  7541. *
  7542. * For most tg3 variants the trick below was working.
  7543. * Ho hum...
  7544. */
  7545. udelay(120);
  7546. /* Flush PCI posted writes. The normal MMIO registers
  7547. * are inaccessible at this time so this is the only
  7548. * way to make this reliably (actually, this is no longer
  7549. * the case, see above). I tried to use indirect
  7550. * register read/write but this upset some 5701 variants.
  7551. */
  7552. pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
  7553. udelay(120);
  7554. if (tg3_flag(tp, PCI_EXPRESS) && pci_is_pcie(tp->pdev)) {
  7555. u16 val16;
  7556. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A0) {
  7557. int j;
  7558. u32 cfg_val;
  7559. /* Wait for link training to complete. */
  7560. for (j = 0; j < 5000; j++)
  7561. udelay(100);
  7562. pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
  7563. pci_write_config_dword(tp->pdev, 0xc4,
  7564. cfg_val | (1 << 15));
  7565. }
  7566. /* Clear the "no snoop" and "relaxed ordering" bits. */
  7567. val16 = PCI_EXP_DEVCTL_RELAX_EN | PCI_EXP_DEVCTL_NOSNOOP_EN;
  7568. /*
  7569. * Older PCIe devices only support the 128 byte
  7570. * MPS setting. Enforce the restriction.
  7571. */
  7572. if (!tg3_flag(tp, CPMU_PRESENT))
  7573. val16 |= PCI_EXP_DEVCTL_PAYLOAD;
  7574. pcie_capability_clear_word(tp->pdev, PCI_EXP_DEVCTL, val16);
  7575. /* Clear error status */
  7576. pcie_capability_write_word(tp->pdev, PCI_EXP_DEVSTA,
  7577. PCI_EXP_DEVSTA_CED |
  7578. PCI_EXP_DEVSTA_NFED |
  7579. PCI_EXP_DEVSTA_FED |
  7580. PCI_EXP_DEVSTA_URD);
  7581. }
  7582. tg3_restore_pci_state(tp);
  7583. tg3_flag_clear(tp, CHIP_RESETTING);
  7584. tg3_flag_clear(tp, ERROR_PROCESSED);
  7585. val = 0;
  7586. if (tg3_flag(tp, 5780_CLASS))
  7587. val = tr32(MEMARB_MODE);
  7588. tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
  7589. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A3) {
  7590. tg3_stop_fw(tp);
  7591. tw32(0x5000, 0x400);
  7592. }
  7593. if (tg3_flag(tp, IS_SSB_CORE)) {
  7594. /*
  7595. * BCM4785: In order to avoid repercussions from using
  7596. * potentially defective internal ROM, stop the Rx RISC CPU,
  7597. * which is not required.
  7598. */
  7599. tg3_stop_fw(tp);
  7600. tg3_halt_cpu(tp, RX_CPU_BASE);
  7601. }
  7602. err = tg3_poll_fw(tp);
  7603. if (err)
  7604. return err;
  7605. tw32(GRC_MODE, tp->grc_mode);
  7606. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A0) {
  7607. val = tr32(0xc4);
  7608. tw32(0xc4, val | (1 << 15));
  7609. }
  7610. if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
  7611. tg3_asic_rev(tp) == ASIC_REV_5705) {
  7612. tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
  7613. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A0)
  7614. tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
  7615. tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
  7616. }
  7617. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
  7618. tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
  7619. val = tp->mac_mode;
  7620. } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
  7621. tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
  7622. val = tp->mac_mode;
  7623. } else
  7624. val = 0;
  7625. tw32_f(MAC_MODE, val);
  7626. udelay(40);
  7627. tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
  7628. tg3_mdio_start(tp);
  7629. if (tg3_flag(tp, PCI_EXPRESS) &&
  7630. tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0 &&
  7631. tg3_asic_rev(tp) != ASIC_REV_5785 &&
  7632. !tg3_flag(tp, 57765_PLUS)) {
  7633. val = tr32(0x7c00);
  7634. tw32(0x7c00, val | (1 << 25));
  7635. }
  7636. tg3_restore_clk(tp);
  7637. /* Increase the core clock speed to fix tx timeout issue for 5762
  7638. * with 100Mbps link speed.
  7639. */
  7640. if (tg3_asic_rev(tp) == ASIC_REV_5762) {
  7641. val = tr32(TG3_CPMU_CLCK_ORIDE_ENABLE);
  7642. tw32(TG3_CPMU_CLCK_ORIDE_ENABLE, val |
  7643. TG3_CPMU_MAC_ORIDE_ENABLE);
  7644. }
  7645. /* Reprobe ASF enable state. */
  7646. tg3_flag_clear(tp, ENABLE_ASF);
  7647. tp->phy_flags &= ~(TG3_PHYFLG_1G_ON_VAUX_OK |
  7648. TG3_PHYFLG_KEEP_LINK_ON_PWRDN);
  7649. tg3_flag_clear(tp, ASF_NEW_HANDSHAKE);
  7650. tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
  7651. if (val == NIC_SRAM_DATA_SIG_MAGIC) {
  7652. u32 nic_cfg;
  7653. tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
  7654. if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
  7655. tg3_flag_set(tp, ENABLE_ASF);
  7656. tp->last_event_jiffies = jiffies;
  7657. if (tg3_flag(tp, 5750_PLUS))
  7658. tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
  7659. tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &nic_cfg);
  7660. if (nic_cfg & NIC_SRAM_1G_ON_VAUX_OK)
  7661. tp->phy_flags |= TG3_PHYFLG_1G_ON_VAUX_OK;
  7662. if (nic_cfg & NIC_SRAM_LNK_FLAP_AVOID)
  7663. tp->phy_flags |= TG3_PHYFLG_KEEP_LINK_ON_PWRDN;
  7664. }
  7665. }
  7666. return 0;
  7667. }
  7668. static void tg3_get_nstats(struct tg3 *, struct rtnl_link_stats64 *);
  7669. static void tg3_get_estats(struct tg3 *, struct tg3_ethtool_stats *);
  7670. static void __tg3_set_rx_mode(struct net_device *);
  7671. /* tp->lock is held. */
  7672. static int tg3_halt(struct tg3 *tp, int kind, bool silent)
  7673. {
  7674. int err;
  7675. tg3_stop_fw(tp);
  7676. tg3_write_sig_pre_reset(tp, kind);
  7677. tg3_abort_hw(tp, silent);
  7678. err = tg3_chip_reset(tp);
  7679. __tg3_set_mac_addr(tp, false);
  7680. tg3_write_sig_legacy(tp, kind);
  7681. tg3_write_sig_post_reset(tp, kind);
  7682. if (tp->hw_stats) {
  7683. /* Save the stats across chip resets... */
  7684. tg3_get_nstats(tp, &tp->net_stats_prev);
  7685. tg3_get_estats(tp, &tp->estats_prev);
  7686. /* And make sure the next sample is new data */
  7687. memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
  7688. }
  7689. return err;
  7690. }
  7691. static int tg3_set_mac_addr(struct net_device *dev, void *p)
  7692. {
  7693. struct tg3 *tp = netdev_priv(dev);
  7694. struct sockaddr *addr = p;
  7695. int err = 0;
  7696. bool skip_mac_1 = false;
  7697. if (!is_valid_ether_addr(addr->sa_data))
  7698. return -EADDRNOTAVAIL;
  7699. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  7700. if (!netif_running(dev))
  7701. return 0;
  7702. if (tg3_flag(tp, ENABLE_ASF)) {
  7703. u32 addr0_high, addr0_low, addr1_high, addr1_low;
  7704. addr0_high = tr32(MAC_ADDR_0_HIGH);
  7705. addr0_low = tr32(MAC_ADDR_0_LOW);
  7706. addr1_high = tr32(MAC_ADDR_1_HIGH);
  7707. addr1_low = tr32(MAC_ADDR_1_LOW);
  7708. /* Skip MAC addr 1 if ASF is using it. */
  7709. if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
  7710. !(addr1_high == 0 && addr1_low == 0))
  7711. skip_mac_1 = true;
  7712. }
  7713. spin_lock_bh(&tp->lock);
  7714. __tg3_set_mac_addr(tp, skip_mac_1);
  7715. __tg3_set_rx_mode(dev);
  7716. spin_unlock_bh(&tp->lock);
  7717. return err;
  7718. }
  7719. /* tp->lock is held. */
  7720. static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
  7721. dma_addr_t mapping, u32 maxlen_flags,
  7722. u32 nic_addr)
  7723. {
  7724. tg3_write_mem(tp,
  7725. (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
  7726. ((u64) mapping >> 32));
  7727. tg3_write_mem(tp,
  7728. (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
  7729. ((u64) mapping & 0xffffffff));
  7730. tg3_write_mem(tp,
  7731. (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
  7732. maxlen_flags);
  7733. if (!tg3_flag(tp, 5705_PLUS))
  7734. tg3_write_mem(tp,
  7735. (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
  7736. nic_addr);
  7737. }
  7738. static void tg3_coal_tx_init(struct tg3 *tp, struct ethtool_coalesce *ec)
  7739. {
  7740. int i = 0;
  7741. if (!tg3_flag(tp, ENABLE_TSS)) {
  7742. tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
  7743. tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
  7744. tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
  7745. } else {
  7746. tw32(HOSTCC_TXCOL_TICKS, 0);
  7747. tw32(HOSTCC_TXMAX_FRAMES, 0);
  7748. tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
  7749. for (; i < tp->txq_cnt; i++) {
  7750. u32 reg;
  7751. reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
  7752. tw32(reg, ec->tx_coalesce_usecs);
  7753. reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
  7754. tw32(reg, ec->tx_max_coalesced_frames);
  7755. reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
  7756. tw32(reg, ec->tx_max_coalesced_frames_irq);
  7757. }
  7758. }
  7759. for (; i < tp->irq_max - 1; i++) {
  7760. tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
  7761. tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
  7762. tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
  7763. }
  7764. }
  7765. static void tg3_coal_rx_init(struct tg3 *tp, struct ethtool_coalesce *ec)
  7766. {
  7767. int i = 0;
  7768. u32 limit = tp->rxq_cnt;
  7769. if (!tg3_flag(tp, ENABLE_RSS)) {
  7770. tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
  7771. tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
  7772. tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
  7773. limit--;
  7774. } else {
  7775. tw32(HOSTCC_RXCOL_TICKS, 0);
  7776. tw32(HOSTCC_RXMAX_FRAMES, 0);
  7777. tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
  7778. }
  7779. for (; i < limit; i++) {
  7780. u32 reg;
  7781. reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
  7782. tw32(reg, ec->rx_coalesce_usecs);
  7783. reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
  7784. tw32(reg, ec->rx_max_coalesced_frames);
  7785. reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
  7786. tw32(reg, ec->rx_max_coalesced_frames_irq);
  7787. }
  7788. for (; i < tp->irq_max - 1; i++) {
  7789. tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
  7790. tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
  7791. tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
  7792. }
  7793. }
  7794. static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
  7795. {
  7796. tg3_coal_tx_init(tp, ec);
  7797. tg3_coal_rx_init(tp, ec);
  7798. if (!tg3_flag(tp, 5705_PLUS)) {
  7799. u32 val = ec->stats_block_coalesce_usecs;
  7800. tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
  7801. tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
  7802. if (!tp->link_up)
  7803. val = 0;
  7804. tw32(HOSTCC_STAT_COAL_TICKS, val);
  7805. }
  7806. }
  7807. /* tp->lock is held. */
  7808. static void tg3_tx_rcbs_disable(struct tg3 *tp)
  7809. {
  7810. u32 txrcb, limit;
  7811. /* Disable all transmit rings but the first. */
  7812. if (!tg3_flag(tp, 5705_PLUS))
  7813. limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
  7814. else if (tg3_flag(tp, 5717_PLUS))
  7815. limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
  7816. else if (tg3_flag(tp, 57765_CLASS) ||
  7817. tg3_asic_rev(tp) == ASIC_REV_5762)
  7818. limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
  7819. else
  7820. limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
  7821. for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
  7822. txrcb < limit; txrcb += TG3_BDINFO_SIZE)
  7823. tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
  7824. BDINFO_FLAGS_DISABLED);
  7825. }
  7826. /* tp->lock is held. */
  7827. static void tg3_tx_rcbs_init(struct tg3 *tp)
  7828. {
  7829. int i = 0;
  7830. u32 txrcb = NIC_SRAM_SEND_RCB;
  7831. if (tg3_flag(tp, ENABLE_TSS))
  7832. i++;
  7833. for (; i < tp->irq_max; i++, txrcb += TG3_BDINFO_SIZE) {
  7834. struct tg3_napi *tnapi = &tp->napi[i];
  7835. if (!tnapi->tx_ring)
  7836. continue;
  7837. tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
  7838. (TG3_TX_RING_SIZE << BDINFO_FLAGS_MAXLEN_SHIFT),
  7839. NIC_SRAM_TX_BUFFER_DESC);
  7840. }
  7841. }
  7842. /* tp->lock is held. */
  7843. static void tg3_rx_ret_rcbs_disable(struct tg3 *tp)
  7844. {
  7845. u32 rxrcb, limit;
  7846. /* Disable all receive return rings but the first. */
  7847. if (tg3_flag(tp, 5717_PLUS))
  7848. limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
  7849. else if (!tg3_flag(tp, 5705_PLUS))
  7850. limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
  7851. else if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
  7852. tg3_asic_rev(tp) == ASIC_REV_5762 ||
  7853. tg3_flag(tp, 57765_CLASS))
  7854. limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
  7855. else
  7856. limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
  7857. for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
  7858. rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
  7859. tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
  7860. BDINFO_FLAGS_DISABLED);
  7861. }
  7862. /* tp->lock is held. */
  7863. static void tg3_rx_ret_rcbs_init(struct tg3 *tp)
  7864. {
  7865. int i = 0;
  7866. u32 rxrcb = NIC_SRAM_RCV_RET_RCB;
  7867. if (tg3_flag(tp, ENABLE_RSS))
  7868. i++;
  7869. for (; i < tp->irq_max; i++, rxrcb += TG3_BDINFO_SIZE) {
  7870. struct tg3_napi *tnapi = &tp->napi[i];
  7871. if (!tnapi->rx_rcb)
  7872. continue;
  7873. tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
  7874. (tp->rx_ret_ring_mask + 1) <<
  7875. BDINFO_FLAGS_MAXLEN_SHIFT, 0);
  7876. }
  7877. }
  7878. /* tp->lock is held. */
  7879. static void tg3_rings_reset(struct tg3 *tp)
  7880. {
  7881. int i;
  7882. u32 stblk;
  7883. struct tg3_napi *tnapi = &tp->napi[0];
  7884. tg3_tx_rcbs_disable(tp);
  7885. tg3_rx_ret_rcbs_disable(tp);
  7886. /* Disable interrupts */
  7887. tw32_mailbox_f(tp->napi[0].int_mbox, 1);
  7888. tp->napi[0].chk_msi_cnt = 0;
  7889. tp->napi[0].last_rx_cons = 0;
  7890. tp->napi[0].last_tx_cons = 0;
  7891. /* Zero mailbox registers. */
  7892. if (tg3_flag(tp, SUPPORT_MSIX)) {
  7893. for (i = 1; i < tp->irq_max; i++) {
  7894. tp->napi[i].tx_prod = 0;
  7895. tp->napi[i].tx_cons = 0;
  7896. if (tg3_flag(tp, ENABLE_TSS))
  7897. tw32_mailbox(tp->napi[i].prodmbox, 0);
  7898. tw32_rx_mbox(tp->napi[i].consmbox, 0);
  7899. tw32_mailbox_f(tp->napi[i].int_mbox, 1);
  7900. tp->napi[i].chk_msi_cnt = 0;
  7901. tp->napi[i].last_rx_cons = 0;
  7902. tp->napi[i].last_tx_cons = 0;
  7903. }
  7904. if (!tg3_flag(tp, ENABLE_TSS))
  7905. tw32_mailbox(tp->napi[0].prodmbox, 0);
  7906. } else {
  7907. tp->napi[0].tx_prod = 0;
  7908. tp->napi[0].tx_cons = 0;
  7909. tw32_mailbox(tp->napi[0].prodmbox, 0);
  7910. tw32_rx_mbox(tp->napi[0].consmbox, 0);
  7911. }
  7912. /* Make sure the NIC-based send BD rings are disabled. */
  7913. if (!tg3_flag(tp, 5705_PLUS)) {
  7914. u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
  7915. for (i = 0; i < 16; i++)
  7916. tw32_tx_mbox(mbox + i * 8, 0);
  7917. }
  7918. /* Clear status block in ram. */
  7919. memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
  7920. /* Set status block DMA address */
  7921. tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
  7922. ((u64) tnapi->status_mapping >> 32));
  7923. tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
  7924. ((u64) tnapi->status_mapping & 0xffffffff));
  7925. stblk = HOSTCC_STATBLCK_RING1;
  7926. for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
  7927. u64 mapping = (u64)tnapi->status_mapping;
  7928. tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
  7929. tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
  7930. stblk += 8;
  7931. /* Clear status block in ram. */
  7932. memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
  7933. }
  7934. tg3_tx_rcbs_init(tp);
  7935. tg3_rx_ret_rcbs_init(tp);
  7936. }
  7937. static void tg3_setup_rxbd_thresholds(struct tg3 *tp)
  7938. {
  7939. u32 val, bdcache_maxcnt, host_rep_thresh, nic_rep_thresh;
  7940. if (!tg3_flag(tp, 5750_PLUS) ||
  7941. tg3_flag(tp, 5780_CLASS) ||
  7942. tg3_asic_rev(tp) == ASIC_REV_5750 ||
  7943. tg3_asic_rev(tp) == ASIC_REV_5752 ||
  7944. tg3_flag(tp, 57765_PLUS))
  7945. bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5700;
  7946. else if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
  7947. tg3_asic_rev(tp) == ASIC_REV_5787)
  7948. bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5755;
  7949. else
  7950. bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5906;
  7951. nic_rep_thresh = min(bdcache_maxcnt / 2, tp->rx_std_max_post);
  7952. host_rep_thresh = max_t(u32, tp->rx_pending / 8, 1);
  7953. val = min(nic_rep_thresh, host_rep_thresh);
  7954. tw32(RCVBDI_STD_THRESH, val);
  7955. if (tg3_flag(tp, 57765_PLUS))
  7956. tw32(STD_REPLENISH_LWM, bdcache_maxcnt);
  7957. if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
  7958. return;
  7959. bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5700;
  7960. host_rep_thresh = max_t(u32, tp->rx_jumbo_pending / 8, 1);
  7961. val = min(bdcache_maxcnt / 2, host_rep_thresh);
  7962. tw32(RCVBDI_JUMBO_THRESH, val);
  7963. if (tg3_flag(tp, 57765_PLUS))
  7964. tw32(JMB_REPLENISH_LWM, bdcache_maxcnt);
  7965. }
  7966. static inline u32 calc_crc(unsigned char *buf, int len)
  7967. {
  7968. u32 reg;
  7969. u32 tmp;
  7970. int j, k;
  7971. reg = 0xffffffff;
  7972. for (j = 0; j < len; j++) {
  7973. reg ^= buf[j];
  7974. for (k = 0; k < 8; k++) {
  7975. tmp = reg & 0x01;
  7976. reg >>= 1;
  7977. if (tmp)
  7978. reg ^= CRC32_POLY_LE;
  7979. }
  7980. }
  7981. return ~reg;
  7982. }
  7983. static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
  7984. {
  7985. /* accept or reject all multicast frames */
  7986. tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
  7987. tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
  7988. tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
  7989. tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
  7990. }
  7991. static void __tg3_set_rx_mode(struct net_device *dev)
  7992. {
  7993. struct tg3 *tp = netdev_priv(dev);
  7994. u32 rx_mode;
  7995. rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
  7996. RX_MODE_KEEP_VLAN_TAG);
  7997. #if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
  7998. /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
  7999. * flag clear.
  8000. */
  8001. if (!tg3_flag(tp, ENABLE_ASF))
  8002. rx_mode |= RX_MODE_KEEP_VLAN_TAG;
  8003. #endif
  8004. if (dev->flags & IFF_PROMISC) {
  8005. /* Promiscuous mode. */
  8006. rx_mode |= RX_MODE_PROMISC;
  8007. } else if (dev->flags & IFF_ALLMULTI) {
  8008. /* Accept all multicast. */
  8009. tg3_set_multi(tp, 1);
  8010. } else if (netdev_mc_empty(dev)) {
  8011. /* Reject all multicast. */
  8012. tg3_set_multi(tp, 0);
  8013. } else {
  8014. /* Accept one or more multicast(s). */
  8015. struct netdev_hw_addr *ha;
  8016. u32 mc_filter[4] = { 0, };
  8017. u32 regidx;
  8018. u32 bit;
  8019. u32 crc;
  8020. netdev_for_each_mc_addr(ha, dev) {
  8021. crc = calc_crc(ha->addr, ETH_ALEN);
  8022. bit = ~crc & 0x7f;
  8023. regidx = (bit & 0x60) >> 5;
  8024. bit &= 0x1f;
  8025. mc_filter[regidx] |= (1 << bit);
  8026. }
  8027. tw32(MAC_HASH_REG_0, mc_filter[0]);
  8028. tw32(MAC_HASH_REG_1, mc_filter[1]);
  8029. tw32(MAC_HASH_REG_2, mc_filter[2]);
  8030. tw32(MAC_HASH_REG_3, mc_filter[3]);
  8031. }
  8032. if (netdev_uc_count(dev) > TG3_MAX_UCAST_ADDR(tp)) {
  8033. rx_mode |= RX_MODE_PROMISC;
  8034. } else if (!(dev->flags & IFF_PROMISC)) {
  8035. /* Add all entries into to the mac addr filter list */
  8036. int i = 0;
  8037. struct netdev_hw_addr *ha;
  8038. netdev_for_each_uc_addr(ha, dev) {
  8039. __tg3_set_one_mac_addr(tp, ha->addr,
  8040. i + TG3_UCAST_ADDR_IDX(tp));
  8041. i++;
  8042. }
  8043. }
  8044. if (rx_mode != tp->rx_mode) {
  8045. tp->rx_mode = rx_mode;
  8046. tw32_f(MAC_RX_MODE, rx_mode);
  8047. udelay(10);
  8048. }
  8049. }
  8050. static void tg3_rss_init_dflt_indir_tbl(struct tg3 *tp, u32 qcnt)
  8051. {
  8052. int i;
  8053. for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
  8054. tp->rss_ind_tbl[i] = ethtool_rxfh_indir_default(i, qcnt);
  8055. }
  8056. static void tg3_rss_check_indir_tbl(struct tg3 *tp)
  8057. {
  8058. int i;
  8059. if (!tg3_flag(tp, SUPPORT_MSIX))
  8060. return;
  8061. if (tp->rxq_cnt == 1) {
  8062. memset(&tp->rss_ind_tbl[0], 0, sizeof(tp->rss_ind_tbl));
  8063. return;
  8064. }
  8065. /* Validate table against current IRQ count */
  8066. for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
  8067. if (tp->rss_ind_tbl[i] >= tp->rxq_cnt)
  8068. break;
  8069. }
  8070. if (i != TG3_RSS_INDIR_TBL_SIZE)
  8071. tg3_rss_init_dflt_indir_tbl(tp, tp->rxq_cnt);
  8072. }
  8073. static void tg3_rss_write_indir_tbl(struct tg3 *tp)
  8074. {
  8075. int i = 0;
  8076. u32 reg = MAC_RSS_INDIR_TBL_0;
  8077. while (i < TG3_RSS_INDIR_TBL_SIZE) {
  8078. u32 val = tp->rss_ind_tbl[i];
  8079. i++;
  8080. for (; i % 8; i++) {
  8081. val <<= 4;
  8082. val |= tp->rss_ind_tbl[i];
  8083. }
  8084. tw32(reg, val);
  8085. reg += 4;
  8086. }
  8087. }
  8088. static inline u32 tg3_lso_rd_dma_workaround_bit(struct tg3 *tp)
  8089. {
  8090. if (tg3_asic_rev(tp) == ASIC_REV_5719)
  8091. return TG3_LSO_RD_DMA_TX_LENGTH_WA_5719;
  8092. else
  8093. return TG3_LSO_RD_DMA_TX_LENGTH_WA_5720;
  8094. }
  8095. /* tp->lock is held. */
  8096. static int tg3_reset_hw(struct tg3 *tp, bool reset_phy)
  8097. {
  8098. u32 val, rdmac_mode;
  8099. int i, err, limit;
  8100. struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
  8101. tg3_disable_ints(tp);
  8102. tg3_stop_fw(tp);
  8103. tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
  8104. if (tg3_flag(tp, INIT_COMPLETE))
  8105. tg3_abort_hw(tp, 1);
  8106. if ((tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN) &&
  8107. !(tp->phy_flags & TG3_PHYFLG_USER_CONFIGURED)) {
  8108. tg3_phy_pull_config(tp);
  8109. tg3_eee_pull_config(tp, NULL);
  8110. tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
  8111. }
  8112. /* Enable MAC control of LPI */
  8113. if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
  8114. tg3_setup_eee(tp);
  8115. if (reset_phy)
  8116. tg3_phy_reset(tp);
  8117. err = tg3_chip_reset(tp);
  8118. if (err)
  8119. return err;
  8120. tg3_write_sig_legacy(tp, RESET_KIND_INIT);
  8121. if (tg3_chip_rev(tp) == CHIPREV_5784_AX) {
  8122. val = tr32(TG3_CPMU_CTRL);
  8123. val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
  8124. tw32(TG3_CPMU_CTRL, val);
  8125. val = tr32(TG3_CPMU_LSPD_10MB_CLK);
  8126. val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
  8127. val |= CPMU_LSPD_10MB_MACCLK_6_25;
  8128. tw32(TG3_CPMU_LSPD_10MB_CLK, val);
  8129. val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
  8130. val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
  8131. val |= CPMU_LNK_AWARE_MACCLK_6_25;
  8132. tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
  8133. val = tr32(TG3_CPMU_HST_ACC);
  8134. val &= ~CPMU_HST_ACC_MACCLK_MASK;
  8135. val |= CPMU_HST_ACC_MACCLK_6_25;
  8136. tw32(TG3_CPMU_HST_ACC, val);
  8137. }
  8138. if (tg3_asic_rev(tp) == ASIC_REV_57780) {
  8139. val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
  8140. val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
  8141. PCIE_PWR_MGMT_L1_THRESH_4MS;
  8142. tw32(PCIE_PWR_MGMT_THRESH, val);
  8143. val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
  8144. tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
  8145. tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
  8146. val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
  8147. tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
  8148. }
  8149. if (tg3_flag(tp, L1PLLPD_EN)) {
  8150. u32 grc_mode = tr32(GRC_MODE);
  8151. /* Access the lower 1K of PL PCIE block registers. */
  8152. val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
  8153. tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
  8154. val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
  8155. tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
  8156. val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
  8157. tw32(GRC_MODE, grc_mode);
  8158. }
  8159. if (tg3_flag(tp, 57765_CLASS)) {
  8160. if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0) {
  8161. u32 grc_mode = tr32(GRC_MODE);
  8162. /* Access the lower 1K of PL PCIE block registers. */
  8163. val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
  8164. tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
  8165. val = tr32(TG3_PCIE_TLDLPL_PORT +
  8166. TG3_PCIE_PL_LO_PHYCTL5);
  8167. tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
  8168. val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
  8169. tw32(GRC_MODE, grc_mode);
  8170. }
  8171. if (tg3_chip_rev(tp) != CHIPREV_57765_AX) {
  8172. u32 grc_mode;
  8173. /* Fix transmit hangs */
  8174. val = tr32(TG3_CPMU_PADRNG_CTL);
  8175. val |= TG3_CPMU_PADRNG_CTL_RDIV2;
  8176. tw32(TG3_CPMU_PADRNG_CTL, val);
  8177. grc_mode = tr32(GRC_MODE);
  8178. /* Access the lower 1K of DL PCIE block registers. */
  8179. val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
  8180. tw32(GRC_MODE, val | GRC_MODE_PCIE_DL_SEL);
  8181. val = tr32(TG3_PCIE_TLDLPL_PORT +
  8182. TG3_PCIE_DL_LO_FTSMAX);
  8183. val &= ~TG3_PCIE_DL_LO_FTSMAX_MSK;
  8184. tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_DL_LO_FTSMAX,
  8185. val | TG3_PCIE_DL_LO_FTSMAX_VAL);
  8186. tw32(GRC_MODE, grc_mode);
  8187. }
  8188. val = tr32(TG3_CPMU_LSPD_10MB_CLK);
  8189. val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
  8190. val |= CPMU_LSPD_10MB_MACCLK_6_25;
  8191. tw32(TG3_CPMU_LSPD_10MB_CLK, val);
  8192. }
  8193. /* This works around an issue with Athlon chipsets on
  8194. * B3 tigon3 silicon. This bit has no effect on any
  8195. * other revision. But do not set this on PCI Express
  8196. * chips and don't even touch the clocks if the CPMU is present.
  8197. */
  8198. if (!tg3_flag(tp, CPMU_PRESENT)) {
  8199. if (!tg3_flag(tp, PCI_EXPRESS))
  8200. tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
  8201. tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
  8202. }
  8203. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0 &&
  8204. tg3_flag(tp, PCIX_MODE)) {
  8205. val = tr32(TG3PCI_PCISTATE);
  8206. val |= PCISTATE_RETRY_SAME_DMA;
  8207. tw32(TG3PCI_PCISTATE, val);
  8208. }
  8209. if (tg3_flag(tp, ENABLE_APE)) {
  8210. /* Allow reads and writes to the
  8211. * APE register and memory space.
  8212. */
  8213. val = tr32(TG3PCI_PCISTATE);
  8214. val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
  8215. PCISTATE_ALLOW_APE_SHMEM_WR |
  8216. PCISTATE_ALLOW_APE_PSPACE_WR;
  8217. tw32(TG3PCI_PCISTATE, val);
  8218. }
  8219. if (tg3_chip_rev(tp) == CHIPREV_5704_BX) {
  8220. /* Enable some hw fixes. */
  8221. val = tr32(TG3PCI_MSI_DATA);
  8222. val |= (1 << 26) | (1 << 28) | (1 << 29);
  8223. tw32(TG3PCI_MSI_DATA, val);
  8224. }
  8225. /* Descriptor ring init may make accesses to the
  8226. * NIC SRAM area to setup the TX descriptors, so we
  8227. * can only do this after the hardware has been
  8228. * successfully reset.
  8229. */
  8230. err = tg3_init_rings(tp);
  8231. if (err)
  8232. return err;
  8233. if (tg3_flag(tp, 57765_PLUS)) {
  8234. val = tr32(TG3PCI_DMA_RW_CTRL) &
  8235. ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
  8236. if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0)
  8237. val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
  8238. if (!tg3_flag(tp, 57765_CLASS) &&
  8239. tg3_asic_rev(tp) != ASIC_REV_5717 &&
  8240. tg3_asic_rev(tp) != ASIC_REV_5762)
  8241. val |= DMA_RWCTRL_TAGGED_STAT_WA;
  8242. tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
  8243. } else if (tg3_asic_rev(tp) != ASIC_REV_5784 &&
  8244. tg3_asic_rev(tp) != ASIC_REV_5761) {
  8245. /* This value is determined during the probe time DMA
  8246. * engine test, tg3_test_dma.
  8247. */
  8248. tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
  8249. }
  8250. tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
  8251. GRC_MODE_4X_NIC_SEND_RINGS |
  8252. GRC_MODE_NO_TX_PHDR_CSUM |
  8253. GRC_MODE_NO_RX_PHDR_CSUM);
  8254. tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
  8255. /* Pseudo-header checksum is done by hardware logic and not
  8256. * the offload processers, so make the chip do the pseudo-
  8257. * header checksums on receive. For transmit it is more
  8258. * convenient to do the pseudo-header checksum in software
  8259. * as Linux does that on transmit for us in all cases.
  8260. */
  8261. tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
  8262. val = GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP;
  8263. if (tp->rxptpctl)
  8264. tw32(TG3_RX_PTP_CTL,
  8265. tp->rxptpctl | TG3_RX_PTP_CTL_HWTS_INTERLOCK);
  8266. if (tg3_flag(tp, PTP_CAPABLE))
  8267. val |= GRC_MODE_TIME_SYNC_ENABLE;
  8268. tw32(GRC_MODE, tp->grc_mode | val);
  8269. /* On one of the AMD platform, MRRS is restricted to 4000 because of
  8270. * south bridge limitation. As a workaround, Driver is setting MRRS
  8271. * to 2048 instead of default 4096.
  8272. */
  8273. if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
  8274. tp->pdev->subsystem_device == TG3PCI_SUBDEVICE_ID_DELL_5762) {
  8275. val = tr32(TG3PCI_DEV_STATUS_CTRL) & ~MAX_READ_REQ_MASK;
  8276. tw32(TG3PCI_DEV_STATUS_CTRL, val | MAX_READ_REQ_SIZE_2048);
  8277. }
  8278. /* Setup the timer prescalar register. Clock is always 66Mhz. */
  8279. val = tr32(GRC_MISC_CFG);
  8280. val &= ~0xff;
  8281. val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
  8282. tw32(GRC_MISC_CFG, val);
  8283. /* Initialize MBUF/DESC pool. */
  8284. if (tg3_flag(tp, 5750_PLUS)) {
  8285. /* Do nothing. */
  8286. } else if (tg3_asic_rev(tp) != ASIC_REV_5705) {
  8287. tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
  8288. if (tg3_asic_rev(tp) == ASIC_REV_5704)
  8289. tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
  8290. else
  8291. tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
  8292. tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
  8293. tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
  8294. } else if (tg3_flag(tp, TSO_CAPABLE)) {
  8295. int fw_len;
  8296. fw_len = tp->fw_len;
  8297. fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
  8298. tw32(BUFMGR_MB_POOL_ADDR,
  8299. NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
  8300. tw32(BUFMGR_MB_POOL_SIZE,
  8301. NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
  8302. }
  8303. if (tp->dev->mtu <= ETH_DATA_LEN) {
  8304. tw32(BUFMGR_MB_RDMA_LOW_WATER,
  8305. tp->bufmgr_config.mbuf_read_dma_low_water);
  8306. tw32(BUFMGR_MB_MACRX_LOW_WATER,
  8307. tp->bufmgr_config.mbuf_mac_rx_low_water);
  8308. tw32(BUFMGR_MB_HIGH_WATER,
  8309. tp->bufmgr_config.mbuf_high_water);
  8310. } else {
  8311. tw32(BUFMGR_MB_RDMA_LOW_WATER,
  8312. tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
  8313. tw32(BUFMGR_MB_MACRX_LOW_WATER,
  8314. tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
  8315. tw32(BUFMGR_MB_HIGH_WATER,
  8316. tp->bufmgr_config.mbuf_high_water_jumbo);
  8317. }
  8318. tw32(BUFMGR_DMA_LOW_WATER,
  8319. tp->bufmgr_config.dma_low_water);
  8320. tw32(BUFMGR_DMA_HIGH_WATER,
  8321. tp->bufmgr_config.dma_high_water);
  8322. val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
  8323. if (tg3_asic_rev(tp) == ASIC_REV_5719)
  8324. val |= BUFMGR_MODE_NO_TX_UNDERRUN;
  8325. if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
  8326. tg3_asic_rev(tp) == ASIC_REV_5762 ||
  8327. tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
  8328. tg3_chip_rev_id(tp) == CHIPREV_ID_5720_A0)
  8329. val |= BUFMGR_MODE_MBLOW_ATTN_ENAB;
  8330. tw32(BUFMGR_MODE, val);
  8331. for (i = 0; i < 2000; i++) {
  8332. if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
  8333. break;
  8334. udelay(10);
  8335. }
  8336. if (i >= 2000) {
  8337. netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
  8338. return -ENODEV;
  8339. }
  8340. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5906_A1)
  8341. tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
  8342. tg3_setup_rxbd_thresholds(tp);
  8343. /* Initialize TG3_BDINFO's at:
  8344. * RCVDBDI_STD_BD: standard eth size rx ring
  8345. * RCVDBDI_JUMBO_BD: jumbo frame rx ring
  8346. * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
  8347. *
  8348. * like so:
  8349. * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
  8350. * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
  8351. * ring attribute flags
  8352. * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
  8353. *
  8354. * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
  8355. * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
  8356. *
  8357. * The size of each ring is fixed in the firmware, but the location is
  8358. * configurable.
  8359. */
  8360. tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
  8361. ((u64) tpr->rx_std_mapping >> 32));
  8362. tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
  8363. ((u64) tpr->rx_std_mapping & 0xffffffff));
  8364. if (!tg3_flag(tp, 5717_PLUS))
  8365. tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
  8366. NIC_SRAM_RX_BUFFER_DESC);
  8367. /* Disable the mini ring */
  8368. if (!tg3_flag(tp, 5705_PLUS))
  8369. tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
  8370. BDINFO_FLAGS_DISABLED);
  8371. /* Program the jumbo buffer descriptor ring control
  8372. * blocks on those devices that have them.
  8373. */
  8374. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
  8375. (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))) {
  8376. if (tg3_flag(tp, JUMBO_RING_ENABLE)) {
  8377. tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
  8378. ((u64) tpr->rx_jmb_mapping >> 32));
  8379. tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
  8380. ((u64) tpr->rx_jmb_mapping & 0xffffffff));
  8381. val = TG3_RX_JMB_RING_SIZE(tp) <<
  8382. BDINFO_FLAGS_MAXLEN_SHIFT;
  8383. tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
  8384. val | BDINFO_FLAGS_USE_EXT_RECV);
  8385. if (!tg3_flag(tp, USE_JUMBO_BDFLAG) ||
  8386. tg3_flag(tp, 57765_CLASS) ||
  8387. tg3_asic_rev(tp) == ASIC_REV_5762)
  8388. tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
  8389. NIC_SRAM_RX_JUMBO_BUFFER_DESC);
  8390. } else {
  8391. tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
  8392. BDINFO_FLAGS_DISABLED);
  8393. }
  8394. if (tg3_flag(tp, 57765_PLUS)) {
  8395. val = TG3_RX_STD_RING_SIZE(tp);
  8396. val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
  8397. val |= (TG3_RX_STD_DMA_SZ << 2);
  8398. } else
  8399. val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
  8400. } else
  8401. val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
  8402. tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
  8403. tpr->rx_std_prod_idx = tp->rx_pending;
  8404. tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
  8405. tpr->rx_jmb_prod_idx =
  8406. tg3_flag(tp, JUMBO_RING_ENABLE) ? tp->rx_jumbo_pending : 0;
  8407. tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
  8408. tg3_rings_reset(tp);
  8409. /* Initialize MAC address and backoff seed. */
  8410. __tg3_set_mac_addr(tp, false);
  8411. /* MTU + ethernet header + FCS + optional VLAN tag */
  8412. tw32(MAC_RX_MTU_SIZE,
  8413. tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
  8414. /* The slot time is changed by tg3_setup_phy if we
  8415. * run at gigabit with half duplex.
  8416. */
  8417. val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
  8418. (6 << TX_LENGTHS_IPG_SHIFT) |
  8419. (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
  8420. if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
  8421. tg3_asic_rev(tp) == ASIC_REV_5762)
  8422. val |= tr32(MAC_TX_LENGTHS) &
  8423. (TX_LENGTHS_JMB_FRM_LEN_MSK |
  8424. TX_LENGTHS_CNT_DWN_VAL_MSK);
  8425. tw32(MAC_TX_LENGTHS, val);
  8426. /* Receive rules. */
  8427. tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
  8428. tw32(RCVLPC_CONFIG, 0x0181);
  8429. /* Calculate RDMAC_MODE setting early, we need it to determine
  8430. * the RCVLPC_STATE_ENABLE mask.
  8431. */
  8432. rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
  8433. RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
  8434. RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
  8435. RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
  8436. RDMAC_MODE_LNGREAD_ENAB);
  8437. if (tg3_asic_rev(tp) == ASIC_REV_5717)
  8438. rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
  8439. if (tg3_asic_rev(tp) == ASIC_REV_5784 ||
  8440. tg3_asic_rev(tp) == ASIC_REV_5785 ||
  8441. tg3_asic_rev(tp) == ASIC_REV_57780)
  8442. rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
  8443. RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
  8444. RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
  8445. if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
  8446. tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
  8447. if (tg3_flag(tp, TSO_CAPABLE) &&
  8448. tg3_asic_rev(tp) == ASIC_REV_5705) {
  8449. rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
  8450. } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
  8451. !tg3_flag(tp, IS_5788)) {
  8452. rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
  8453. }
  8454. }
  8455. if (tg3_flag(tp, PCI_EXPRESS))
  8456. rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
  8457. if (tg3_asic_rev(tp) == ASIC_REV_57766) {
  8458. tp->dma_limit = 0;
  8459. if (tp->dev->mtu <= ETH_DATA_LEN) {
  8460. rdmac_mode |= RDMAC_MODE_JMB_2K_MMRR;
  8461. tp->dma_limit = TG3_TX_BD_DMA_MAX_2K;
  8462. }
  8463. }
  8464. if (tg3_flag(tp, HW_TSO_1) ||
  8465. tg3_flag(tp, HW_TSO_2) ||
  8466. tg3_flag(tp, HW_TSO_3))
  8467. rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
  8468. if (tg3_flag(tp, 57765_PLUS) ||
  8469. tg3_asic_rev(tp) == ASIC_REV_5785 ||
  8470. tg3_asic_rev(tp) == ASIC_REV_57780)
  8471. rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
  8472. if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
  8473. tg3_asic_rev(tp) == ASIC_REV_5762)
  8474. rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
  8475. if (tg3_asic_rev(tp) == ASIC_REV_5761 ||
  8476. tg3_asic_rev(tp) == ASIC_REV_5784 ||
  8477. tg3_asic_rev(tp) == ASIC_REV_5785 ||
  8478. tg3_asic_rev(tp) == ASIC_REV_57780 ||
  8479. tg3_flag(tp, 57765_PLUS)) {
  8480. u32 tgtreg;
  8481. if (tg3_asic_rev(tp) == ASIC_REV_5762)
  8482. tgtreg = TG3_RDMA_RSRVCTRL_REG2;
  8483. else
  8484. tgtreg = TG3_RDMA_RSRVCTRL_REG;
  8485. val = tr32(tgtreg);
  8486. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
  8487. tg3_asic_rev(tp) == ASIC_REV_5762) {
  8488. val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
  8489. TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
  8490. TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
  8491. val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
  8492. TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
  8493. TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
  8494. }
  8495. tw32(tgtreg, val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
  8496. }
  8497. if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
  8498. tg3_asic_rev(tp) == ASIC_REV_5720 ||
  8499. tg3_asic_rev(tp) == ASIC_REV_5762) {
  8500. u32 tgtreg;
  8501. if (tg3_asic_rev(tp) == ASIC_REV_5762)
  8502. tgtreg = TG3_LSO_RD_DMA_CRPTEN_CTRL2;
  8503. else
  8504. tgtreg = TG3_LSO_RD_DMA_CRPTEN_CTRL;
  8505. val = tr32(tgtreg);
  8506. tw32(tgtreg, val |
  8507. TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
  8508. TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
  8509. }
  8510. /* Receive/send statistics. */
  8511. if (tg3_flag(tp, 5750_PLUS)) {
  8512. val = tr32(RCVLPC_STATS_ENABLE);
  8513. val &= ~RCVLPC_STATSENAB_DACK_FIX;
  8514. tw32(RCVLPC_STATS_ENABLE, val);
  8515. } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
  8516. tg3_flag(tp, TSO_CAPABLE)) {
  8517. val = tr32(RCVLPC_STATS_ENABLE);
  8518. val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
  8519. tw32(RCVLPC_STATS_ENABLE, val);
  8520. } else {
  8521. tw32(RCVLPC_STATS_ENABLE, 0xffffff);
  8522. }
  8523. tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
  8524. tw32(SNDDATAI_STATSENAB, 0xffffff);
  8525. tw32(SNDDATAI_STATSCTRL,
  8526. (SNDDATAI_SCTRL_ENABLE |
  8527. SNDDATAI_SCTRL_FASTUPD));
  8528. /* Setup host coalescing engine. */
  8529. tw32(HOSTCC_MODE, 0);
  8530. for (i = 0; i < 2000; i++) {
  8531. if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
  8532. break;
  8533. udelay(10);
  8534. }
  8535. __tg3_set_coalesce(tp, &tp->coal);
  8536. if (!tg3_flag(tp, 5705_PLUS)) {
  8537. /* Status/statistics block address. See tg3_timer,
  8538. * the tg3_periodic_fetch_stats call there, and
  8539. * tg3_get_stats to see how this works for 5705/5750 chips.
  8540. */
  8541. tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
  8542. ((u64) tp->stats_mapping >> 32));
  8543. tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
  8544. ((u64) tp->stats_mapping & 0xffffffff));
  8545. tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
  8546. tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
  8547. /* Clear statistics and status block memory areas */
  8548. for (i = NIC_SRAM_STATS_BLK;
  8549. i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
  8550. i += sizeof(u32)) {
  8551. tg3_write_mem(tp, i, 0);
  8552. udelay(40);
  8553. }
  8554. }
  8555. tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
  8556. tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
  8557. tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
  8558. if (!tg3_flag(tp, 5705_PLUS))
  8559. tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
  8560. if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
  8561. tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
  8562. /* reset to prevent losing 1st rx packet intermittently */
  8563. tw32_f(MAC_RX_MODE, RX_MODE_RESET);
  8564. udelay(10);
  8565. }
  8566. tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
  8567. MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE |
  8568. MAC_MODE_FHDE_ENABLE;
  8569. if (tg3_flag(tp, ENABLE_APE))
  8570. tp->mac_mode |= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
  8571. if (!tg3_flag(tp, 5705_PLUS) &&
  8572. !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
  8573. tg3_asic_rev(tp) != ASIC_REV_5700)
  8574. tp->mac_mode |= MAC_MODE_LINK_POLARITY;
  8575. tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
  8576. udelay(40);
  8577. /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
  8578. * If TG3_FLAG_IS_NIC is zero, we should read the
  8579. * register to preserve the GPIO settings for LOMs. The GPIOs,
  8580. * whether used as inputs or outputs, are set by boot code after
  8581. * reset.
  8582. */
  8583. if (!tg3_flag(tp, IS_NIC)) {
  8584. u32 gpio_mask;
  8585. gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
  8586. GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
  8587. GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
  8588. if (tg3_asic_rev(tp) == ASIC_REV_5752)
  8589. gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
  8590. GRC_LCLCTRL_GPIO_OUTPUT3;
  8591. if (tg3_asic_rev(tp) == ASIC_REV_5755)
  8592. gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
  8593. tp->grc_local_ctrl &= ~gpio_mask;
  8594. tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
  8595. /* GPIO1 must be driven high for eeprom write protect */
  8596. if (tg3_flag(tp, EEPROM_WRITE_PROT))
  8597. tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
  8598. GRC_LCLCTRL_GPIO_OUTPUT1);
  8599. }
  8600. tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
  8601. udelay(100);
  8602. if (tg3_flag(tp, USING_MSIX)) {
  8603. val = tr32(MSGINT_MODE);
  8604. val |= MSGINT_MODE_ENABLE;
  8605. if (tp->irq_cnt > 1)
  8606. val |= MSGINT_MODE_MULTIVEC_EN;
  8607. if (!tg3_flag(tp, 1SHOT_MSI))
  8608. val |= MSGINT_MODE_ONE_SHOT_DISABLE;
  8609. tw32(MSGINT_MODE, val);
  8610. }
  8611. if (!tg3_flag(tp, 5705_PLUS)) {
  8612. tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
  8613. udelay(40);
  8614. }
  8615. val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
  8616. WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
  8617. WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
  8618. WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
  8619. WDMAC_MODE_LNGREAD_ENAB);
  8620. if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
  8621. tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
  8622. if (tg3_flag(tp, TSO_CAPABLE) &&
  8623. (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A1 ||
  8624. tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A2)) {
  8625. /* nothing */
  8626. } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
  8627. !tg3_flag(tp, IS_5788)) {
  8628. val |= WDMAC_MODE_RX_ACCEL;
  8629. }
  8630. }
  8631. /* Enable host coalescing bug fix */
  8632. if (tg3_flag(tp, 5755_PLUS))
  8633. val |= WDMAC_MODE_STATUS_TAG_FIX;
  8634. if (tg3_asic_rev(tp) == ASIC_REV_5785)
  8635. val |= WDMAC_MODE_BURST_ALL_DATA;
  8636. tw32_f(WDMAC_MODE, val);
  8637. udelay(40);
  8638. if (tg3_flag(tp, PCIX_MODE)) {
  8639. u16 pcix_cmd;
  8640. pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
  8641. &pcix_cmd);
  8642. if (tg3_asic_rev(tp) == ASIC_REV_5703) {
  8643. pcix_cmd &= ~PCI_X_CMD_MAX_READ;
  8644. pcix_cmd |= PCI_X_CMD_READ_2K;
  8645. } else if (tg3_asic_rev(tp) == ASIC_REV_5704) {
  8646. pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
  8647. pcix_cmd |= PCI_X_CMD_READ_2K;
  8648. }
  8649. pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
  8650. pcix_cmd);
  8651. }
  8652. tw32_f(RDMAC_MODE, rdmac_mode);
  8653. udelay(40);
  8654. if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
  8655. tg3_asic_rev(tp) == ASIC_REV_5720) {
  8656. for (i = 0; i < TG3_NUM_RDMA_CHANNELS; i++) {
  8657. if (tr32(TG3_RDMA_LENGTH + (i << 2)) > TG3_MAX_MTU(tp))
  8658. break;
  8659. }
  8660. if (i < TG3_NUM_RDMA_CHANNELS) {
  8661. val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
  8662. val |= tg3_lso_rd_dma_workaround_bit(tp);
  8663. tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val);
  8664. tg3_flag_set(tp, 5719_5720_RDMA_BUG);
  8665. }
  8666. }
  8667. tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
  8668. if (!tg3_flag(tp, 5705_PLUS))
  8669. tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
  8670. if (tg3_asic_rev(tp) == ASIC_REV_5761)
  8671. tw32(SNDDATAC_MODE,
  8672. SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
  8673. else
  8674. tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
  8675. tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
  8676. tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
  8677. val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
  8678. if (tg3_flag(tp, LRG_PROD_RING_CAP))
  8679. val |= RCVDBDI_MODE_LRG_RING_SZ;
  8680. tw32(RCVDBDI_MODE, val);
  8681. tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
  8682. if (tg3_flag(tp, HW_TSO_1) ||
  8683. tg3_flag(tp, HW_TSO_2) ||
  8684. tg3_flag(tp, HW_TSO_3))
  8685. tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
  8686. val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
  8687. if (tg3_flag(tp, ENABLE_TSS))
  8688. val |= SNDBDI_MODE_MULTI_TXQ_EN;
  8689. tw32(SNDBDI_MODE, val);
  8690. tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
  8691. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0) {
  8692. err = tg3_load_5701_a0_firmware_fix(tp);
  8693. if (err)
  8694. return err;
  8695. }
  8696. if (tg3_asic_rev(tp) == ASIC_REV_57766) {
  8697. /* Ignore any errors for the firmware download. If download
  8698. * fails, the device will operate with EEE disabled
  8699. */
  8700. tg3_load_57766_firmware(tp);
  8701. }
  8702. if (tg3_flag(tp, TSO_CAPABLE)) {
  8703. err = tg3_load_tso_firmware(tp);
  8704. if (err)
  8705. return err;
  8706. }
  8707. tp->tx_mode = TX_MODE_ENABLE;
  8708. if (tg3_flag(tp, 5755_PLUS) ||
  8709. tg3_asic_rev(tp) == ASIC_REV_5906)
  8710. tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
  8711. if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
  8712. tg3_asic_rev(tp) == ASIC_REV_5762) {
  8713. val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
  8714. tp->tx_mode &= ~val;
  8715. tp->tx_mode |= tr32(MAC_TX_MODE) & val;
  8716. }
  8717. tw32_f(MAC_TX_MODE, tp->tx_mode);
  8718. udelay(100);
  8719. if (tg3_flag(tp, ENABLE_RSS)) {
  8720. u32 rss_key[10];
  8721. tg3_rss_write_indir_tbl(tp);
  8722. netdev_rss_key_fill(rss_key, 10 * sizeof(u32));
  8723. for (i = 0; i < 10 ; i++)
  8724. tw32(MAC_RSS_HASH_KEY_0 + i*4, rss_key[i]);
  8725. }
  8726. tp->rx_mode = RX_MODE_ENABLE;
  8727. if (tg3_flag(tp, 5755_PLUS))
  8728. tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
  8729. if (tg3_asic_rev(tp) == ASIC_REV_5762)
  8730. tp->rx_mode |= RX_MODE_IPV4_FRAG_FIX;
  8731. if (tg3_flag(tp, ENABLE_RSS))
  8732. tp->rx_mode |= RX_MODE_RSS_ENABLE |
  8733. RX_MODE_RSS_ITBL_HASH_BITS_7 |
  8734. RX_MODE_RSS_IPV6_HASH_EN |
  8735. RX_MODE_RSS_TCP_IPV6_HASH_EN |
  8736. RX_MODE_RSS_IPV4_HASH_EN |
  8737. RX_MODE_RSS_TCP_IPV4_HASH_EN;
  8738. tw32_f(MAC_RX_MODE, tp->rx_mode);
  8739. udelay(10);
  8740. tw32(MAC_LED_CTRL, tp->led_ctrl);
  8741. tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
  8742. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
  8743. tw32_f(MAC_RX_MODE, RX_MODE_RESET);
  8744. udelay(10);
  8745. }
  8746. tw32_f(MAC_RX_MODE, tp->rx_mode);
  8747. udelay(10);
  8748. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
  8749. if ((tg3_asic_rev(tp) == ASIC_REV_5704) &&
  8750. !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
  8751. /* Set drive transmission level to 1.2V */
  8752. /* only if the signal pre-emphasis bit is not set */
  8753. val = tr32(MAC_SERDES_CFG);
  8754. val &= 0xfffff000;
  8755. val |= 0x880;
  8756. tw32(MAC_SERDES_CFG, val);
  8757. }
  8758. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A1)
  8759. tw32(MAC_SERDES_CFG, 0x616000);
  8760. }
  8761. /* Prevent chip from dropping frames when flow control
  8762. * is enabled.
  8763. */
  8764. if (tg3_flag(tp, 57765_CLASS))
  8765. val = 1;
  8766. else
  8767. val = 2;
  8768. tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
  8769. if (tg3_asic_rev(tp) == ASIC_REV_5704 &&
  8770. (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
  8771. /* Use hardware link auto-negotiation */
  8772. tg3_flag_set(tp, HW_AUTONEG);
  8773. }
  8774. if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
  8775. tg3_asic_rev(tp) == ASIC_REV_5714) {
  8776. u32 tmp;
  8777. tmp = tr32(SERDES_RX_CTRL);
  8778. tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
  8779. tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
  8780. tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
  8781. tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
  8782. }
  8783. if (!tg3_flag(tp, USE_PHYLIB)) {
  8784. if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
  8785. tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
  8786. err = tg3_setup_phy(tp, false);
  8787. if (err)
  8788. return err;
  8789. if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
  8790. !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
  8791. u32 tmp;
  8792. /* Clear CRC stats. */
  8793. if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
  8794. tg3_writephy(tp, MII_TG3_TEST1,
  8795. tmp | MII_TG3_TEST1_CRC_EN);
  8796. tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
  8797. }
  8798. }
  8799. }
  8800. __tg3_set_rx_mode(tp->dev);
  8801. /* Initialize receive rules. */
  8802. tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
  8803. tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
  8804. tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
  8805. tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
  8806. if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS))
  8807. limit = 8;
  8808. else
  8809. limit = 16;
  8810. if (tg3_flag(tp, ENABLE_ASF))
  8811. limit -= 4;
  8812. switch (limit) {
  8813. case 16:
  8814. tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
  8815. /* fall through */
  8816. case 15:
  8817. tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
  8818. /* fall through */
  8819. case 14:
  8820. tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
  8821. /* fall through */
  8822. case 13:
  8823. tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
  8824. /* fall through */
  8825. case 12:
  8826. tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
  8827. /* fall through */
  8828. case 11:
  8829. tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
  8830. /* fall through */
  8831. case 10:
  8832. tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
  8833. /* fall through */
  8834. case 9:
  8835. tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
  8836. /* fall through */
  8837. case 8:
  8838. tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
  8839. /* fall through */
  8840. case 7:
  8841. tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
  8842. /* fall through */
  8843. case 6:
  8844. tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
  8845. /* fall through */
  8846. case 5:
  8847. tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
  8848. /* fall through */
  8849. case 4:
  8850. /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
  8851. case 3:
  8852. /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
  8853. case 2:
  8854. case 1:
  8855. default:
  8856. break;
  8857. }
  8858. if (tg3_flag(tp, ENABLE_APE))
  8859. /* Write our heartbeat update interval to APE. */
  8860. tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
  8861. APE_HOST_HEARTBEAT_INT_5SEC);
  8862. tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
  8863. return 0;
  8864. }
  8865. /* Called at device open time to get the chip ready for
  8866. * packet processing. Invoked with tp->lock held.
  8867. */
  8868. static int tg3_init_hw(struct tg3 *tp, bool reset_phy)
  8869. {
  8870. /* Chip may have been just powered on. If so, the boot code may still
  8871. * be running initialization. Wait for it to finish to avoid races in
  8872. * accessing the hardware.
  8873. */
  8874. tg3_enable_register_access(tp);
  8875. tg3_poll_fw(tp);
  8876. tg3_switch_clocks(tp);
  8877. tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
  8878. return tg3_reset_hw(tp, reset_phy);
  8879. }
  8880. #ifdef CONFIG_TIGON3_HWMON
  8881. static void tg3_sd_scan_scratchpad(struct tg3 *tp, struct tg3_ocir *ocir)
  8882. {
  8883. int i;
  8884. for (i = 0; i < TG3_SD_NUM_RECS; i++, ocir++) {
  8885. u32 off = i * TG3_OCIR_LEN, len = TG3_OCIR_LEN;
  8886. tg3_ape_scratchpad_read(tp, (u32 *) ocir, off, len);
  8887. off += len;
  8888. if (ocir->signature != TG3_OCIR_SIG_MAGIC ||
  8889. !(ocir->version_flags & TG3_OCIR_FLAG_ACTIVE))
  8890. memset(ocir, 0, TG3_OCIR_LEN);
  8891. }
  8892. }
  8893. /* sysfs attributes for hwmon */
  8894. static ssize_t tg3_show_temp(struct device *dev,
  8895. struct device_attribute *devattr, char *buf)
  8896. {
  8897. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  8898. struct tg3 *tp = dev_get_drvdata(dev);
  8899. u32 temperature;
  8900. spin_lock_bh(&tp->lock);
  8901. tg3_ape_scratchpad_read(tp, &temperature, attr->index,
  8902. sizeof(temperature));
  8903. spin_unlock_bh(&tp->lock);
  8904. return sprintf(buf, "%u\n", temperature * 1000);
  8905. }
  8906. static SENSOR_DEVICE_ATTR(temp1_input, 0444, tg3_show_temp, NULL,
  8907. TG3_TEMP_SENSOR_OFFSET);
  8908. static SENSOR_DEVICE_ATTR(temp1_crit, 0444, tg3_show_temp, NULL,
  8909. TG3_TEMP_CAUTION_OFFSET);
  8910. static SENSOR_DEVICE_ATTR(temp1_max, 0444, tg3_show_temp, NULL,
  8911. TG3_TEMP_MAX_OFFSET);
  8912. static struct attribute *tg3_attrs[] = {
  8913. &sensor_dev_attr_temp1_input.dev_attr.attr,
  8914. &sensor_dev_attr_temp1_crit.dev_attr.attr,
  8915. &sensor_dev_attr_temp1_max.dev_attr.attr,
  8916. NULL
  8917. };
  8918. ATTRIBUTE_GROUPS(tg3);
  8919. static void tg3_hwmon_close(struct tg3 *tp)
  8920. {
  8921. if (tp->hwmon_dev) {
  8922. hwmon_device_unregister(tp->hwmon_dev);
  8923. tp->hwmon_dev = NULL;
  8924. }
  8925. }
  8926. static void tg3_hwmon_open(struct tg3 *tp)
  8927. {
  8928. int i;
  8929. u32 size = 0;
  8930. struct pci_dev *pdev = tp->pdev;
  8931. struct tg3_ocir ocirs[TG3_SD_NUM_RECS];
  8932. tg3_sd_scan_scratchpad(tp, ocirs);
  8933. for (i = 0; i < TG3_SD_NUM_RECS; i++) {
  8934. if (!ocirs[i].src_data_length)
  8935. continue;
  8936. size += ocirs[i].src_hdr_length;
  8937. size += ocirs[i].src_data_length;
  8938. }
  8939. if (!size)
  8940. return;
  8941. tp->hwmon_dev = hwmon_device_register_with_groups(&pdev->dev, "tg3",
  8942. tp, tg3_groups);
  8943. if (IS_ERR(tp->hwmon_dev)) {
  8944. tp->hwmon_dev = NULL;
  8945. dev_err(&pdev->dev, "Cannot register hwmon device, aborting\n");
  8946. }
  8947. }
  8948. #else
  8949. static inline void tg3_hwmon_close(struct tg3 *tp) { }
  8950. static inline void tg3_hwmon_open(struct tg3 *tp) { }
  8951. #endif /* CONFIG_TIGON3_HWMON */
  8952. #define TG3_STAT_ADD32(PSTAT, REG) \
  8953. do { u32 __val = tr32(REG); \
  8954. (PSTAT)->low += __val; \
  8955. if ((PSTAT)->low < __val) \
  8956. (PSTAT)->high += 1; \
  8957. } while (0)
  8958. static void tg3_periodic_fetch_stats(struct tg3 *tp)
  8959. {
  8960. struct tg3_hw_stats *sp = tp->hw_stats;
  8961. if (!tp->link_up)
  8962. return;
  8963. TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
  8964. TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
  8965. TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
  8966. TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
  8967. TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
  8968. TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
  8969. TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
  8970. TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
  8971. TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
  8972. TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
  8973. TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
  8974. TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
  8975. TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
  8976. if (unlikely(tg3_flag(tp, 5719_5720_RDMA_BUG) &&
  8977. (sp->tx_ucast_packets.low + sp->tx_mcast_packets.low +
  8978. sp->tx_bcast_packets.low) > TG3_NUM_RDMA_CHANNELS)) {
  8979. u32 val;
  8980. val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
  8981. val &= ~tg3_lso_rd_dma_workaround_bit(tp);
  8982. tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val);
  8983. tg3_flag_clear(tp, 5719_5720_RDMA_BUG);
  8984. }
  8985. TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
  8986. TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
  8987. TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
  8988. TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
  8989. TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
  8990. TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
  8991. TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
  8992. TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
  8993. TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
  8994. TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
  8995. TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
  8996. TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
  8997. TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
  8998. TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
  8999. TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
  9000. if (tg3_asic_rev(tp) != ASIC_REV_5717 &&
  9001. tg3_asic_rev(tp) != ASIC_REV_5762 &&
  9002. tg3_chip_rev_id(tp) != CHIPREV_ID_5719_A0 &&
  9003. tg3_chip_rev_id(tp) != CHIPREV_ID_5720_A0) {
  9004. TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
  9005. } else {
  9006. u32 val = tr32(HOSTCC_FLOW_ATTN);
  9007. val = (val & HOSTCC_FLOW_ATTN_MBUF_LWM) ? 1 : 0;
  9008. if (val) {
  9009. tw32(HOSTCC_FLOW_ATTN, HOSTCC_FLOW_ATTN_MBUF_LWM);
  9010. sp->rx_discards.low += val;
  9011. if (sp->rx_discards.low < val)
  9012. sp->rx_discards.high += 1;
  9013. }
  9014. sp->mbuf_lwm_thresh_hit = sp->rx_discards;
  9015. }
  9016. TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
  9017. }
  9018. static void tg3_chk_missed_msi(struct tg3 *tp)
  9019. {
  9020. u32 i;
  9021. for (i = 0; i < tp->irq_cnt; i++) {
  9022. struct tg3_napi *tnapi = &tp->napi[i];
  9023. if (tg3_has_work(tnapi)) {
  9024. if (tnapi->last_rx_cons == tnapi->rx_rcb_ptr &&
  9025. tnapi->last_tx_cons == tnapi->tx_cons) {
  9026. if (tnapi->chk_msi_cnt < 1) {
  9027. tnapi->chk_msi_cnt++;
  9028. return;
  9029. }
  9030. tg3_msi(0, tnapi);
  9031. }
  9032. }
  9033. tnapi->chk_msi_cnt = 0;
  9034. tnapi->last_rx_cons = tnapi->rx_rcb_ptr;
  9035. tnapi->last_tx_cons = tnapi->tx_cons;
  9036. }
  9037. }
  9038. static void tg3_timer(struct timer_list *t)
  9039. {
  9040. struct tg3 *tp = from_timer(tp, t, timer);
  9041. spin_lock(&tp->lock);
  9042. if (tp->irq_sync || tg3_flag(tp, RESET_TASK_PENDING)) {
  9043. spin_unlock(&tp->lock);
  9044. goto restart_timer;
  9045. }
  9046. if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
  9047. tg3_flag(tp, 57765_CLASS))
  9048. tg3_chk_missed_msi(tp);
  9049. if (tg3_flag(tp, FLUSH_POSTED_WRITES)) {
  9050. /* BCM4785: Flush posted writes from GbE to host memory. */
  9051. tr32(HOSTCC_MODE);
  9052. }
  9053. if (!tg3_flag(tp, TAGGED_STATUS)) {
  9054. /* All of this garbage is because when using non-tagged
  9055. * IRQ status the mailbox/status_block protocol the chip
  9056. * uses with the cpu is race prone.
  9057. */
  9058. if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
  9059. tw32(GRC_LOCAL_CTRL,
  9060. tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
  9061. } else {
  9062. tw32(HOSTCC_MODE, tp->coalesce_mode |
  9063. HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
  9064. }
  9065. if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
  9066. spin_unlock(&tp->lock);
  9067. tg3_reset_task_schedule(tp);
  9068. goto restart_timer;
  9069. }
  9070. }
  9071. /* This part only runs once per second. */
  9072. if (!--tp->timer_counter) {
  9073. if (tg3_flag(tp, 5705_PLUS))
  9074. tg3_periodic_fetch_stats(tp);
  9075. if (tp->setlpicnt && !--tp->setlpicnt)
  9076. tg3_phy_eee_enable(tp);
  9077. if (tg3_flag(tp, USE_LINKCHG_REG)) {
  9078. u32 mac_stat;
  9079. int phy_event;
  9080. mac_stat = tr32(MAC_STATUS);
  9081. phy_event = 0;
  9082. if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
  9083. if (mac_stat & MAC_STATUS_MI_INTERRUPT)
  9084. phy_event = 1;
  9085. } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
  9086. phy_event = 1;
  9087. if (phy_event)
  9088. tg3_setup_phy(tp, false);
  9089. } else if (tg3_flag(tp, POLL_SERDES)) {
  9090. u32 mac_stat = tr32(MAC_STATUS);
  9091. int need_setup = 0;
  9092. if (tp->link_up &&
  9093. (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
  9094. need_setup = 1;
  9095. }
  9096. if (!tp->link_up &&
  9097. (mac_stat & (MAC_STATUS_PCS_SYNCED |
  9098. MAC_STATUS_SIGNAL_DET))) {
  9099. need_setup = 1;
  9100. }
  9101. if (need_setup) {
  9102. if (!tp->serdes_counter) {
  9103. tw32_f(MAC_MODE,
  9104. (tp->mac_mode &
  9105. ~MAC_MODE_PORT_MODE_MASK));
  9106. udelay(40);
  9107. tw32_f(MAC_MODE, tp->mac_mode);
  9108. udelay(40);
  9109. }
  9110. tg3_setup_phy(tp, false);
  9111. }
  9112. } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
  9113. tg3_flag(tp, 5780_CLASS)) {
  9114. tg3_serdes_parallel_detect(tp);
  9115. } else if (tg3_flag(tp, POLL_CPMU_LINK)) {
  9116. u32 cpmu = tr32(TG3_CPMU_STATUS);
  9117. bool link_up = !((cpmu & TG3_CPMU_STATUS_LINK_MASK) ==
  9118. TG3_CPMU_STATUS_LINK_MASK);
  9119. if (link_up != tp->link_up)
  9120. tg3_setup_phy(tp, false);
  9121. }
  9122. tp->timer_counter = tp->timer_multiplier;
  9123. }
  9124. /* Heartbeat is only sent once every 2 seconds.
  9125. *
  9126. * The heartbeat is to tell the ASF firmware that the host
  9127. * driver is still alive. In the event that the OS crashes,
  9128. * ASF needs to reset the hardware to free up the FIFO space
  9129. * that may be filled with rx packets destined for the host.
  9130. * If the FIFO is full, ASF will no longer function properly.
  9131. *
  9132. * Unintended resets have been reported on real time kernels
  9133. * where the timer doesn't run on time. Netpoll will also have
  9134. * same problem.
  9135. *
  9136. * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
  9137. * to check the ring condition when the heartbeat is expiring
  9138. * before doing the reset. This will prevent most unintended
  9139. * resets.
  9140. */
  9141. if (!--tp->asf_counter) {
  9142. if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
  9143. tg3_wait_for_event_ack(tp);
  9144. tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
  9145. FWCMD_NICDRV_ALIVE3);
  9146. tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
  9147. tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
  9148. TG3_FW_UPDATE_TIMEOUT_SEC);
  9149. tg3_generate_fw_event(tp);
  9150. }
  9151. tp->asf_counter = tp->asf_multiplier;
  9152. }
  9153. /* Update the APE heartbeat every 5 seconds.*/
  9154. tg3_send_ape_heartbeat(tp, TG3_APE_HB_INTERVAL);
  9155. spin_unlock(&tp->lock);
  9156. restart_timer:
  9157. tp->timer.expires = jiffies + tp->timer_offset;
  9158. add_timer(&tp->timer);
  9159. }
  9160. static void tg3_timer_init(struct tg3 *tp)
  9161. {
  9162. if (tg3_flag(tp, TAGGED_STATUS) &&
  9163. tg3_asic_rev(tp) != ASIC_REV_5717 &&
  9164. !tg3_flag(tp, 57765_CLASS))
  9165. tp->timer_offset = HZ;
  9166. else
  9167. tp->timer_offset = HZ / 10;
  9168. BUG_ON(tp->timer_offset > HZ);
  9169. tp->timer_multiplier = (HZ / tp->timer_offset);
  9170. tp->asf_multiplier = (HZ / tp->timer_offset) *
  9171. TG3_FW_UPDATE_FREQ_SEC;
  9172. timer_setup(&tp->timer, tg3_timer, 0);
  9173. }
  9174. static void tg3_timer_start(struct tg3 *tp)
  9175. {
  9176. tp->asf_counter = tp->asf_multiplier;
  9177. tp->timer_counter = tp->timer_multiplier;
  9178. tp->timer.expires = jiffies + tp->timer_offset;
  9179. add_timer(&tp->timer);
  9180. }
  9181. static void tg3_timer_stop(struct tg3 *tp)
  9182. {
  9183. del_timer_sync(&tp->timer);
  9184. }
  9185. /* Restart hardware after configuration changes, self-test, etc.
  9186. * Invoked with tp->lock held.
  9187. */
  9188. static int tg3_restart_hw(struct tg3 *tp, bool reset_phy)
  9189. __releases(tp->lock)
  9190. __acquires(tp->lock)
  9191. {
  9192. int err;
  9193. err = tg3_init_hw(tp, reset_phy);
  9194. if (err) {
  9195. netdev_err(tp->dev,
  9196. "Failed to re-initialize device, aborting\n");
  9197. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  9198. tg3_full_unlock(tp);
  9199. tg3_timer_stop(tp);
  9200. tp->irq_sync = 0;
  9201. tg3_napi_enable(tp);
  9202. dev_close(tp->dev);
  9203. tg3_full_lock(tp, 0);
  9204. }
  9205. return err;
  9206. }
  9207. static void tg3_reset_task(struct work_struct *work)
  9208. {
  9209. struct tg3 *tp = container_of(work, struct tg3, reset_task);
  9210. int err;
  9211. rtnl_lock();
  9212. tg3_full_lock(tp, 0);
  9213. if (!netif_running(tp->dev)) {
  9214. tg3_flag_clear(tp, RESET_TASK_PENDING);
  9215. tg3_full_unlock(tp);
  9216. rtnl_unlock();
  9217. return;
  9218. }
  9219. tg3_full_unlock(tp);
  9220. tg3_phy_stop(tp);
  9221. tg3_netif_stop(tp);
  9222. tg3_full_lock(tp, 1);
  9223. if (tg3_flag(tp, TX_RECOVERY_PENDING)) {
  9224. tp->write32_tx_mbox = tg3_write32_tx_mbox;
  9225. tp->write32_rx_mbox = tg3_write_flush_reg32;
  9226. tg3_flag_set(tp, MBOX_WRITE_REORDER);
  9227. tg3_flag_clear(tp, TX_RECOVERY_PENDING);
  9228. }
  9229. tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
  9230. err = tg3_init_hw(tp, true);
  9231. if (err) {
  9232. tg3_full_unlock(tp);
  9233. tp->irq_sync = 0;
  9234. tg3_napi_enable(tp);
  9235. /* Clear this flag so that tg3_reset_task_cancel() will not
  9236. * call cancel_work_sync() and wait forever.
  9237. */
  9238. tg3_flag_clear(tp, RESET_TASK_PENDING);
  9239. dev_close(tp->dev);
  9240. goto out;
  9241. }
  9242. tg3_netif_start(tp);
  9243. tg3_full_unlock(tp);
  9244. if (!err)
  9245. tg3_phy_start(tp);
  9246. tg3_flag_clear(tp, RESET_TASK_PENDING);
  9247. out:
  9248. rtnl_unlock();
  9249. }
  9250. static int tg3_request_irq(struct tg3 *tp, int irq_num)
  9251. {
  9252. irq_handler_t fn;
  9253. unsigned long flags;
  9254. char *name;
  9255. struct tg3_napi *tnapi = &tp->napi[irq_num];
  9256. if (tp->irq_cnt == 1)
  9257. name = tp->dev->name;
  9258. else {
  9259. name = &tnapi->irq_lbl[0];
  9260. if (tnapi->tx_buffers && tnapi->rx_rcb)
  9261. snprintf(name, IFNAMSIZ,
  9262. "%s-txrx-%d", tp->dev->name, irq_num);
  9263. else if (tnapi->tx_buffers)
  9264. snprintf(name, IFNAMSIZ,
  9265. "%s-tx-%d", tp->dev->name, irq_num);
  9266. else if (tnapi->rx_rcb)
  9267. snprintf(name, IFNAMSIZ,
  9268. "%s-rx-%d", tp->dev->name, irq_num);
  9269. else
  9270. snprintf(name, IFNAMSIZ,
  9271. "%s-%d", tp->dev->name, irq_num);
  9272. name[IFNAMSIZ-1] = 0;
  9273. }
  9274. if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
  9275. fn = tg3_msi;
  9276. if (tg3_flag(tp, 1SHOT_MSI))
  9277. fn = tg3_msi_1shot;
  9278. flags = 0;
  9279. } else {
  9280. fn = tg3_interrupt;
  9281. if (tg3_flag(tp, TAGGED_STATUS))
  9282. fn = tg3_interrupt_tagged;
  9283. flags = IRQF_SHARED;
  9284. }
  9285. return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
  9286. }
  9287. static int tg3_test_interrupt(struct tg3 *tp)
  9288. {
  9289. struct tg3_napi *tnapi = &tp->napi[0];
  9290. struct net_device *dev = tp->dev;
  9291. int err, i, intr_ok = 0;
  9292. u32 val;
  9293. if (!netif_running(dev))
  9294. return -ENODEV;
  9295. tg3_disable_ints(tp);
  9296. free_irq(tnapi->irq_vec, tnapi);
  9297. /*
  9298. * Turn off MSI one shot mode. Otherwise this test has no
  9299. * observable way to know whether the interrupt was delivered.
  9300. */
  9301. if (tg3_flag(tp, 57765_PLUS)) {
  9302. val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
  9303. tw32(MSGINT_MODE, val);
  9304. }
  9305. err = request_irq(tnapi->irq_vec, tg3_test_isr,
  9306. IRQF_SHARED, dev->name, tnapi);
  9307. if (err)
  9308. return err;
  9309. tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
  9310. tg3_enable_ints(tp);
  9311. tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
  9312. tnapi->coal_now);
  9313. for (i = 0; i < 5; i++) {
  9314. u32 int_mbox, misc_host_ctrl;
  9315. int_mbox = tr32_mailbox(tnapi->int_mbox);
  9316. misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
  9317. if ((int_mbox != 0) ||
  9318. (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
  9319. intr_ok = 1;
  9320. break;
  9321. }
  9322. if (tg3_flag(tp, 57765_PLUS) &&
  9323. tnapi->hw_status->status_tag != tnapi->last_tag)
  9324. tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
  9325. msleep(10);
  9326. }
  9327. tg3_disable_ints(tp);
  9328. free_irq(tnapi->irq_vec, tnapi);
  9329. err = tg3_request_irq(tp, 0);
  9330. if (err)
  9331. return err;
  9332. if (intr_ok) {
  9333. /* Reenable MSI one shot mode. */
  9334. if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, 1SHOT_MSI)) {
  9335. val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
  9336. tw32(MSGINT_MODE, val);
  9337. }
  9338. return 0;
  9339. }
  9340. return -EIO;
  9341. }
  9342. /* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
  9343. * successfully restored
  9344. */
  9345. static int tg3_test_msi(struct tg3 *tp)
  9346. {
  9347. int err;
  9348. u16 pci_cmd;
  9349. if (!tg3_flag(tp, USING_MSI))
  9350. return 0;
  9351. /* Turn off SERR reporting in case MSI terminates with Master
  9352. * Abort.
  9353. */
  9354. pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
  9355. pci_write_config_word(tp->pdev, PCI_COMMAND,
  9356. pci_cmd & ~PCI_COMMAND_SERR);
  9357. err = tg3_test_interrupt(tp);
  9358. pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
  9359. if (!err)
  9360. return 0;
  9361. /* other failures */
  9362. if (err != -EIO)
  9363. return err;
  9364. /* MSI test failed, go back to INTx mode */
  9365. netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
  9366. "to INTx mode. Please report this failure to the PCI "
  9367. "maintainer and include system chipset information\n");
  9368. free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
  9369. pci_disable_msi(tp->pdev);
  9370. tg3_flag_clear(tp, USING_MSI);
  9371. tp->napi[0].irq_vec = tp->pdev->irq;
  9372. err = tg3_request_irq(tp, 0);
  9373. if (err)
  9374. return err;
  9375. /* Need to reset the chip because the MSI cycle may have terminated
  9376. * with Master Abort.
  9377. */
  9378. tg3_full_lock(tp, 1);
  9379. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  9380. err = tg3_init_hw(tp, true);
  9381. tg3_full_unlock(tp);
  9382. if (err)
  9383. free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
  9384. return err;
  9385. }
  9386. static int tg3_request_firmware(struct tg3 *tp)
  9387. {
  9388. const struct tg3_firmware_hdr *fw_hdr;
  9389. if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
  9390. netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
  9391. tp->fw_needed);
  9392. return -ENOENT;
  9393. }
  9394. fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
  9395. /* Firmware blob starts with version numbers, followed by
  9396. * start address and _full_ length including BSS sections
  9397. * (which must be longer than the actual data, of course
  9398. */
  9399. tp->fw_len = be32_to_cpu(fw_hdr->len); /* includes bss */
  9400. if (tp->fw_len < (tp->fw->size - TG3_FW_HDR_LEN)) {
  9401. netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
  9402. tp->fw_len, tp->fw_needed);
  9403. release_firmware(tp->fw);
  9404. tp->fw = NULL;
  9405. return -EINVAL;
  9406. }
  9407. /* We no longer need firmware; we have it. */
  9408. tp->fw_needed = NULL;
  9409. return 0;
  9410. }
  9411. static u32 tg3_irq_count(struct tg3 *tp)
  9412. {
  9413. u32 irq_cnt = max(tp->rxq_cnt, tp->txq_cnt);
  9414. if (irq_cnt > 1) {
  9415. /* We want as many rx rings enabled as there are cpus.
  9416. * In multiqueue MSI-X mode, the first MSI-X vector
  9417. * only deals with link interrupts, etc, so we add
  9418. * one to the number of vectors we are requesting.
  9419. */
  9420. irq_cnt = min_t(unsigned, irq_cnt + 1, tp->irq_max);
  9421. }
  9422. return irq_cnt;
  9423. }
  9424. static bool tg3_enable_msix(struct tg3 *tp)
  9425. {
  9426. int i, rc;
  9427. struct msix_entry msix_ent[TG3_IRQ_MAX_VECS];
  9428. tp->txq_cnt = tp->txq_req;
  9429. tp->rxq_cnt = tp->rxq_req;
  9430. if (!tp->rxq_cnt)
  9431. tp->rxq_cnt = netif_get_num_default_rss_queues();
  9432. if (tp->rxq_cnt > tp->rxq_max)
  9433. tp->rxq_cnt = tp->rxq_max;
  9434. /* Disable multiple TX rings by default. Simple round-robin hardware
  9435. * scheduling of the TX rings can cause starvation of rings with
  9436. * small packets when other rings have TSO or jumbo packets.
  9437. */
  9438. if (!tp->txq_req)
  9439. tp->txq_cnt = 1;
  9440. tp->irq_cnt = tg3_irq_count(tp);
  9441. for (i = 0; i < tp->irq_max; i++) {
  9442. msix_ent[i].entry = i;
  9443. msix_ent[i].vector = 0;
  9444. }
  9445. rc = pci_enable_msix_range(tp->pdev, msix_ent, 1, tp->irq_cnt);
  9446. if (rc < 0) {
  9447. return false;
  9448. } else if (rc < tp->irq_cnt) {
  9449. netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
  9450. tp->irq_cnt, rc);
  9451. tp->irq_cnt = rc;
  9452. tp->rxq_cnt = max(rc - 1, 1);
  9453. if (tp->txq_cnt)
  9454. tp->txq_cnt = min(tp->rxq_cnt, tp->txq_max);
  9455. }
  9456. for (i = 0; i < tp->irq_max; i++)
  9457. tp->napi[i].irq_vec = msix_ent[i].vector;
  9458. if (netif_set_real_num_rx_queues(tp->dev, tp->rxq_cnt)) {
  9459. pci_disable_msix(tp->pdev);
  9460. return false;
  9461. }
  9462. if (tp->irq_cnt == 1)
  9463. return true;
  9464. tg3_flag_set(tp, ENABLE_RSS);
  9465. if (tp->txq_cnt > 1)
  9466. tg3_flag_set(tp, ENABLE_TSS);
  9467. netif_set_real_num_tx_queues(tp->dev, tp->txq_cnt);
  9468. return true;
  9469. }
  9470. static void tg3_ints_init(struct tg3 *tp)
  9471. {
  9472. if ((tg3_flag(tp, SUPPORT_MSI) || tg3_flag(tp, SUPPORT_MSIX)) &&
  9473. !tg3_flag(tp, TAGGED_STATUS)) {
  9474. /* All MSI supporting chips should support tagged
  9475. * status. Assert that this is the case.
  9476. */
  9477. netdev_warn(tp->dev,
  9478. "MSI without TAGGED_STATUS? Not using MSI\n");
  9479. goto defcfg;
  9480. }
  9481. if (tg3_flag(tp, SUPPORT_MSIX) && tg3_enable_msix(tp))
  9482. tg3_flag_set(tp, USING_MSIX);
  9483. else if (tg3_flag(tp, SUPPORT_MSI) && pci_enable_msi(tp->pdev) == 0)
  9484. tg3_flag_set(tp, USING_MSI);
  9485. if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
  9486. u32 msi_mode = tr32(MSGINT_MODE);
  9487. if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1)
  9488. msi_mode |= MSGINT_MODE_MULTIVEC_EN;
  9489. if (!tg3_flag(tp, 1SHOT_MSI))
  9490. msi_mode |= MSGINT_MODE_ONE_SHOT_DISABLE;
  9491. tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
  9492. }
  9493. defcfg:
  9494. if (!tg3_flag(tp, USING_MSIX)) {
  9495. tp->irq_cnt = 1;
  9496. tp->napi[0].irq_vec = tp->pdev->irq;
  9497. }
  9498. if (tp->irq_cnt == 1) {
  9499. tp->txq_cnt = 1;
  9500. tp->rxq_cnt = 1;
  9501. netif_set_real_num_tx_queues(tp->dev, 1);
  9502. netif_set_real_num_rx_queues(tp->dev, 1);
  9503. }
  9504. }
  9505. static void tg3_ints_fini(struct tg3 *tp)
  9506. {
  9507. if (tg3_flag(tp, USING_MSIX))
  9508. pci_disable_msix(tp->pdev);
  9509. else if (tg3_flag(tp, USING_MSI))
  9510. pci_disable_msi(tp->pdev);
  9511. tg3_flag_clear(tp, USING_MSI);
  9512. tg3_flag_clear(tp, USING_MSIX);
  9513. tg3_flag_clear(tp, ENABLE_RSS);
  9514. tg3_flag_clear(tp, ENABLE_TSS);
  9515. }
  9516. static int tg3_start(struct tg3 *tp, bool reset_phy, bool test_irq,
  9517. bool init)
  9518. {
  9519. struct net_device *dev = tp->dev;
  9520. int i, err;
  9521. /*
  9522. * Setup interrupts first so we know how
  9523. * many NAPI resources to allocate
  9524. */
  9525. tg3_ints_init(tp);
  9526. tg3_rss_check_indir_tbl(tp);
  9527. /* The placement of this call is tied
  9528. * to the setup and use of Host TX descriptors.
  9529. */
  9530. err = tg3_alloc_consistent(tp);
  9531. if (err)
  9532. goto out_ints_fini;
  9533. tg3_napi_init(tp);
  9534. tg3_napi_enable(tp);
  9535. for (i = 0; i < tp->irq_cnt; i++) {
  9536. err = tg3_request_irq(tp, i);
  9537. if (err) {
  9538. for (i--; i >= 0; i--) {
  9539. struct tg3_napi *tnapi = &tp->napi[i];
  9540. free_irq(tnapi->irq_vec, tnapi);
  9541. }
  9542. goto out_napi_fini;
  9543. }
  9544. }
  9545. tg3_full_lock(tp, 0);
  9546. if (init)
  9547. tg3_ape_driver_state_change(tp, RESET_KIND_INIT);
  9548. err = tg3_init_hw(tp, reset_phy);
  9549. if (err) {
  9550. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  9551. tg3_free_rings(tp);
  9552. }
  9553. tg3_full_unlock(tp);
  9554. if (err)
  9555. goto out_free_irq;
  9556. if (test_irq && tg3_flag(tp, USING_MSI)) {
  9557. err = tg3_test_msi(tp);
  9558. if (err) {
  9559. tg3_full_lock(tp, 0);
  9560. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  9561. tg3_free_rings(tp);
  9562. tg3_full_unlock(tp);
  9563. goto out_napi_fini;
  9564. }
  9565. if (!tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
  9566. u32 val = tr32(PCIE_TRANSACTION_CFG);
  9567. tw32(PCIE_TRANSACTION_CFG,
  9568. val | PCIE_TRANS_CFG_1SHOT_MSI);
  9569. }
  9570. }
  9571. tg3_phy_start(tp);
  9572. tg3_hwmon_open(tp);
  9573. tg3_full_lock(tp, 0);
  9574. tg3_timer_start(tp);
  9575. tg3_flag_set(tp, INIT_COMPLETE);
  9576. tg3_enable_ints(tp);
  9577. tg3_ptp_resume(tp);
  9578. tg3_full_unlock(tp);
  9579. netif_tx_start_all_queues(dev);
  9580. /*
  9581. * Reset loopback feature if it was turned on while the device was down
  9582. * make sure that it's installed properly now.
  9583. */
  9584. if (dev->features & NETIF_F_LOOPBACK)
  9585. tg3_set_loopback(dev, dev->features);
  9586. return 0;
  9587. out_free_irq:
  9588. for (i = tp->irq_cnt - 1; i >= 0; i--) {
  9589. struct tg3_napi *tnapi = &tp->napi[i];
  9590. free_irq(tnapi->irq_vec, tnapi);
  9591. }
  9592. out_napi_fini:
  9593. tg3_napi_disable(tp);
  9594. tg3_napi_fini(tp);
  9595. tg3_free_consistent(tp);
  9596. out_ints_fini:
  9597. tg3_ints_fini(tp);
  9598. return err;
  9599. }
  9600. static void tg3_stop(struct tg3 *tp)
  9601. {
  9602. int i;
  9603. tg3_reset_task_cancel(tp);
  9604. tg3_netif_stop(tp);
  9605. tg3_timer_stop(tp);
  9606. tg3_hwmon_close(tp);
  9607. tg3_phy_stop(tp);
  9608. tg3_full_lock(tp, 1);
  9609. tg3_disable_ints(tp);
  9610. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  9611. tg3_free_rings(tp);
  9612. tg3_flag_clear(tp, INIT_COMPLETE);
  9613. tg3_full_unlock(tp);
  9614. for (i = tp->irq_cnt - 1; i >= 0; i--) {
  9615. struct tg3_napi *tnapi = &tp->napi[i];
  9616. free_irq(tnapi->irq_vec, tnapi);
  9617. }
  9618. tg3_ints_fini(tp);
  9619. tg3_napi_fini(tp);
  9620. tg3_free_consistent(tp);
  9621. }
  9622. static int tg3_open(struct net_device *dev)
  9623. {
  9624. struct tg3 *tp = netdev_priv(dev);
  9625. int err;
  9626. if (tp->pcierr_recovery) {
  9627. netdev_err(dev, "Failed to open device. PCI error recovery "
  9628. "in progress\n");
  9629. return -EAGAIN;
  9630. }
  9631. if (tp->fw_needed) {
  9632. err = tg3_request_firmware(tp);
  9633. if (tg3_asic_rev(tp) == ASIC_REV_57766) {
  9634. if (err) {
  9635. netdev_warn(tp->dev, "EEE capability disabled\n");
  9636. tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
  9637. } else if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP)) {
  9638. netdev_warn(tp->dev, "EEE capability restored\n");
  9639. tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
  9640. }
  9641. } else if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0) {
  9642. if (err)
  9643. return err;
  9644. } else if (err) {
  9645. netdev_warn(tp->dev, "TSO capability disabled\n");
  9646. tg3_flag_clear(tp, TSO_CAPABLE);
  9647. } else if (!tg3_flag(tp, TSO_CAPABLE)) {
  9648. netdev_notice(tp->dev, "TSO capability restored\n");
  9649. tg3_flag_set(tp, TSO_CAPABLE);
  9650. }
  9651. }
  9652. tg3_carrier_off(tp);
  9653. err = tg3_power_up(tp);
  9654. if (err)
  9655. return err;
  9656. tg3_full_lock(tp, 0);
  9657. tg3_disable_ints(tp);
  9658. tg3_flag_clear(tp, INIT_COMPLETE);
  9659. tg3_full_unlock(tp);
  9660. err = tg3_start(tp,
  9661. !(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN),
  9662. true, true);
  9663. if (err) {
  9664. tg3_frob_aux_power(tp, false);
  9665. pci_set_power_state(tp->pdev, PCI_D3hot);
  9666. }
  9667. return err;
  9668. }
  9669. static int tg3_close(struct net_device *dev)
  9670. {
  9671. struct tg3 *tp = netdev_priv(dev);
  9672. if (tp->pcierr_recovery) {
  9673. netdev_err(dev, "Failed to close device. PCI error recovery "
  9674. "in progress\n");
  9675. return -EAGAIN;
  9676. }
  9677. tg3_stop(tp);
  9678. if (pci_device_is_present(tp->pdev)) {
  9679. tg3_power_down_prepare(tp);
  9680. tg3_carrier_off(tp);
  9681. }
  9682. return 0;
  9683. }
  9684. static inline u64 get_stat64(tg3_stat64_t *val)
  9685. {
  9686. return ((u64)val->high << 32) | ((u64)val->low);
  9687. }
  9688. static u64 tg3_calc_crc_errors(struct tg3 *tp)
  9689. {
  9690. struct tg3_hw_stats *hw_stats = tp->hw_stats;
  9691. if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
  9692. (tg3_asic_rev(tp) == ASIC_REV_5700 ||
  9693. tg3_asic_rev(tp) == ASIC_REV_5701)) {
  9694. u32 val;
  9695. if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
  9696. tg3_writephy(tp, MII_TG3_TEST1,
  9697. val | MII_TG3_TEST1_CRC_EN);
  9698. tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
  9699. } else
  9700. val = 0;
  9701. tp->phy_crc_errors += val;
  9702. return tp->phy_crc_errors;
  9703. }
  9704. return get_stat64(&hw_stats->rx_fcs_errors);
  9705. }
  9706. #define ESTAT_ADD(member) \
  9707. estats->member = old_estats->member + \
  9708. get_stat64(&hw_stats->member)
  9709. static void tg3_get_estats(struct tg3 *tp, struct tg3_ethtool_stats *estats)
  9710. {
  9711. struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
  9712. struct tg3_hw_stats *hw_stats = tp->hw_stats;
  9713. ESTAT_ADD(rx_octets);
  9714. ESTAT_ADD(rx_fragments);
  9715. ESTAT_ADD(rx_ucast_packets);
  9716. ESTAT_ADD(rx_mcast_packets);
  9717. ESTAT_ADD(rx_bcast_packets);
  9718. ESTAT_ADD(rx_fcs_errors);
  9719. ESTAT_ADD(rx_align_errors);
  9720. ESTAT_ADD(rx_xon_pause_rcvd);
  9721. ESTAT_ADD(rx_xoff_pause_rcvd);
  9722. ESTAT_ADD(rx_mac_ctrl_rcvd);
  9723. ESTAT_ADD(rx_xoff_entered);
  9724. ESTAT_ADD(rx_frame_too_long_errors);
  9725. ESTAT_ADD(rx_jabbers);
  9726. ESTAT_ADD(rx_undersize_packets);
  9727. ESTAT_ADD(rx_in_length_errors);
  9728. ESTAT_ADD(rx_out_length_errors);
  9729. ESTAT_ADD(rx_64_or_less_octet_packets);
  9730. ESTAT_ADD(rx_65_to_127_octet_packets);
  9731. ESTAT_ADD(rx_128_to_255_octet_packets);
  9732. ESTAT_ADD(rx_256_to_511_octet_packets);
  9733. ESTAT_ADD(rx_512_to_1023_octet_packets);
  9734. ESTAT_ADD(rx_1024_to_1522_octet_packets);
  9735. ESTAT_ADD(rx_1523_to_2047_octet_packets);
  9736. ESTAT_ADD(rx_2048_to_4095_octet_packets);
  9737. ESTAT_ADD(rx_4096_to_8191_octet_packets);
  9738. ESTAT_ADD(rx_8192_to_9022_octet_packets);
  9739. ESTAT_ADD(tx_octets);
  9740. ESTAT_ADD(tx_collisions);
  9741. ESTAT_ADD(tx_xon_sent);
  9742. ESTAT_ADD(tx_xoff_sent);
  9743. ESTAT_ADD(tx_flow_control);
  9744. ESTAT_ADD(tx_mac_errors);
  9745. ESTAT_ADD(tx_single_collisions);
  9746. ESTAT_ADD(tx_mult_collisions);
  9747. ESTAT_ADD(tx_deferred);
  9748. ESTAT_ADD(tx_excessive_collisions);
  9749. ESTAT_ADD(tx_late_collisions);
  9750. ESTAT_ADD(tx_collide_2times);
  9751. ESTAT_ADD(tx_collide_3times);
  9752. ESTAT_ADD(tx_collide_4times);
  9753. ESTAT_ADD(tx_collide_5times);
  9754. ESTAT_ADD(tx_collide_6times);
  9755. ESTAT_ADD(tx_collide_7times);
  9756. ESTAT_ADD(tx_collide_8times);
  9757. ESTAT_ADD(tx_collide_9times);
  9758. ESTAT_ADD(tx_collide_10times);
  9759. ESTAT_ADD(tx_collide_11times);
  9760. ESTAT_ADD(tx_collide_12times);
  9761. ESTAT_ADD(tx_collide_13times);
  9762. ESTAT_ADD(tx_collide_14times);
  9763. ESTAT_ADD(tx_collide_15times);
  9764. ESTAT_ADD(tx_ucast_packets);
  9765. ESTAT_ADD(tx_mcast_packets);
  9766. ESTAT_ADD(tx_bcast_packets);
  9767. ESTAT_ADD(tx_carrier_sense_errors);
  9768. ESTAT_ADD(tx_discards);
  9769. ESTAT_ADD(tx_errors);
  9770. ESTAT_ADD(dma_writeq_full);
  9771. ESTAT_ADD(dma_write_prioq_full);
  9772. ESTAT_ADD(rxbds_empty);
  9773. ESTAT_ADD(rx_discards);
  9774. ESTAT_ADD(rx_errors);
  9775. ESTAT_ADD(rx_threshold_hit);
  9776. ESTAT_ADD(dma_readq_full);
  9777. ESTAT_ADD(dma_read_prioq_full);
  9778. ESTAT_ADD(tx_comp_queue_full);
  9779. ESTAT_ADD(ring_set_send_prod_index);
  9780. ESTAT_ADD(ring_status_update);
  9781. ESTAT_ADD(nic_irqs);
  9782. ESTAT_ADD(nic_avoided_irqs);
  9783. ESTAT_ADD(nic_tx_threshold_hit);
  9784. ESTAT_ADD(mbuf_lwm_thresh_hit);
  9785. }
  9786. static void tg3_get_nstats(struct tg3 *tp, struct rtnl_link_stats64 *stats)
  9787. {
  9788. struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
  9789. struct tg3_hw_stats *hw_stats = tp->hw_stats;
  9790. stats->rx_packets = old_stats->rx_packets +
  9791. get_stat64(&hw_stats->rx_ucast_packets) +
  9792. get_stat64(&hw_stats->rx_mcast_packets) +
  9793. get_stat64(&hw_stats->rx_bcast_packets);
  9794. stats->tx_packets = old_stats->tx_packets +
  9795. get_stat64(&hw_stats->tx_ucast_packets) +
  9796. get_stat64(&hw_stats->tx_mcast_packets) +
  9797. get_stat64(&hw_stats->tx_bcast_packets);
  9798. stats->rx_bytes = old_stats->rx_bytes +
  9799. get_stat64(&hw_stats->rx_octets);
  9800. stats->tx_bytes = old_stats->tx_bytes +
  9801. get_stat64(&hw_stats->tx_octets);
  9802. stats->rx_errors = old_stats->rx_errors +
  9803. get_stat64(&hw_stats->rx_errors);
  9804. stats->tx_errors = old_stats->tx_errors +
  9805. get_stat64(&hw_stats->tx_errors) +
  9806. get_stat64(&hw_stats->tx_mac_errors) +
  9807. get_stat64(&hw_stats->tx_carrier_sense_errors) +
  9808. get_stat64(&hw_stats->tx_discards);
  9809. stats->multicast = old_stats->multicast +
  9810. get_stat64(&hw_stats->rx_mcast_packets);
  9811. stats->collisions = old_stats->collisions +
  9812. get_stat64(&hw_stats->tx_collisions);
  9813. stats->rx_length_errors = old_stats->rx_length_errors +
  9814. get_stat64(&hw_stats->rx_frame_too_long_errors) +
  9815. get_stat64(&hw_stats->rx_undersize_packets);
  9816. stats->rx_frame_errors = old_stats->rx_frame_errors +
  9817. get_stat64(&hw_stats->rx_align_errors);
  9818. stats->tx_aborted_errors = old_stats->tx_aborted_errors +
  9819. get_stat64(&hw_stats->tx_discards);
  9820. stats->tx_carrier_errors = old_stats->tx_carrier_errors +
  9821. get_stat64(&hw_stats->tx_carrier_sense_errors);
  9822. stats->rx_crc_errors = old_stats->rx_crc_errors +
  9823. tg3_calc_crc_errors(tp);
  9824. stats->rx_missed_errors = old_stats->rx_missed_errors +
  9825. get_stat64(&hw_stats->rx_discards);
  9826. stats->rx_dropped = tp->rx_dropped;
  9827. stats->tx_dropped = tp->tx_dropped;
  9828. }
  9829. static int tg3_get_regs_len(struct net_device *dev)
  9830. {
  9831. return TG3_REG_BLK_SIZE;
  9832. }
  9833. static void tg3_get_regs(struct net_device *dev,
  9834. struct ethtool_regs *regs, void *_p)
  9835. {
  9836. struct tg3 *tp = netdev_priv(dev);
  9837. regs->version = 0;
  9838. memset(_p, 0, TG3_REG_BLK_SIZE);
  9839. if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
  9840. return;
  9841. tg3_full_lock(tp, 0);
  9842. tg3_dump_legacy_regs(tp, (u32 *)_p);
  9843. tg3_full_unlock(tp);
  9844. }
  9845. static int tg3_get_eeprom_len(struct net_device *dev)
  9846. {
  9847. struct tg3 *tp = netdev_priv(dev);
  9848. return tp->nvram_size;
  9849. }
  9850. static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
  9851. {
  9852. struct tg3 *tp = netdev_priv(dev);
  9853. int ret, cpmu_restore = 0;
  9854. u8 *pd;
  9855. u32 i, offset, len, b_offset, b_count, cpmu_val = 0;
  9856. __be32 val;
  9857. if (tg3_flag(tp, NO_NVRAM))
  9858. return -EINVAL;
  9859. offset = eeprom->offset;
  9860. len = eeprom->len;
  9861. eeprom->len = 0;
  9862. eeprom->magic = TG3_EEPROM_MAGIC;
  9863. /* Override clock, link aware and link idle modes */
  9864. if (tg3_flag(tp, CPMU_PRESENT)) {
  9865. cpmu_val = tr32(TG3_CPMU_CTRL);
  9866. if (cpmu_val & (CPMU_CTRL_LINK_AWARE_MODE |
  9867. CPMU_CTRL_LINK_IDLE_MODE)) {
  9868. tw32(TG3_CPMU_CTRL, cpmu_val &
  9869. ~(CPMU_CTRL_LINK_AWARE_MODE |
  9870. CPMU_CTRL_LINK_IDLE_MODE));
  9871. cpmu_restore = 1;
  9872. }
  9873. }
  9874. tg3_override_clk(tp);
  9875. if (offset & 3) {
  9876. /* adjustments to start on required 4 byte boundary */
  9877. b_offset = offset & 3;
  9878. b_count = 4 - b_offset;
  9879. if (b_count > len) {
  9880. /* i.e. offset=1 len=2 */
  9881. b_count = len;
  9882. }
  9883. ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
  9884. if (ret)
  9885. goto eeprom_done;
  9886. memcpy(data, ((char *)&val) + b_offset, b_count);
  9887. len -= b_count;
  9888. offset += b_count;
  9889. eeprom->len += b_count;
  9890. }
  9891. /* read bytes up to the last 4 byte boundary */
  9892. pd = &data[eeprom->len];
  9893. for (i = 0; i < (len - (len & 3)); i += 4) {
  9894. ret = tg3_nvram_read_be32(tp, offset + i, &val);
  9895. if (ret) {
  9896. if (i)
  9897. i -= 4;
  9898. eeprom->len += i;
  9899. goto eeprom_done;
  9900. }
  9901. memcpy(pd + i, &val, 4);
  9902. if (need_resched()) {
  9903. if (signal_pending(current)) {
  9904. eeprom->len += i;
  9905. ret = -EINTR;
  9906. goto eeprom_done;
  9907. }
  9908. cond_resched();
  9909. }
  9910. }
  9911. eeprom->len += i;
  9912. if (len & 3) {
  9913. /* read last bytes not ending on 4 byte boundary */
  9914. pd = &data[eeprom->len];
  9915. b_count = len & 3;
  9916. b_offset = offset + len - b_count;
  9917. ret = tg3_nvram_read_be32(tp, b_offset, &val);
  9918. if (ret)
  9919. goto eeprom_done;
  9920. memcpy(pd, &val, b_count);
  9921. eeprom->len += b_count;
  9922. }
  9923. ret = 0;
  9924. eeprom_done:
  9925. /* Restore clock, link aware and link idle modes */
  9926. tg3_restore_clk(tp);
  9927. if (cpmu_restore)
  9928. tw32(TG3_CPMU_CTRL, cpmu_val);
  9929. return ret;
  9930. }
  9931. static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
  9932. {
  9933. struct tg3 *tp = netdev_priv(dev);
  9934. int ret;
  9935. u32 offset, len, b_offset, odd_len;
  9936. u8 *buf;
  9937. __be32 start = 0, end;
  9938. if (tg3_flag(tp, NO_NVRAM) ||
  9939. eeprom->magic != TG3_EEPROM_MAGIC)
  9940. return -EINVAL;
  9941. offset = eeprom->offset;
  9942. len = eeprom->len;
  9943. if ((b_offset = (offset & 3))) {
  9944. /* adjustments to start on required 4 byte boundary */
  9945. ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
  9946. if (ret)
  9947. return ret;
  9948. len += b_offset;
  9949. offset &= ~3;
  9950. if (len < 4)
  9951. len = 4;
  9952. }
  9953. odd_len = 0;
  9954. if (len & 3) {
  9955. /* adjustments to end on required 4 byte boundary */
  9956. odd_len = 1;
  9957. len = (len + 3) & ~3;
  9958. ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
  9959. if (ret)
  9960. return ret;
  9961. }
  9962. buf = data;
  9963. if (b_offset || odd_len) {
  9964. buf = kmalloc(len, GFP_KERNEL);
  9965. if (!buf)
  9966. return -ENOMEM;
  9967. if (b_offset)
  9968. memcpy(buf, &start, 4);
  9969. if (odd_len)
  9970. memcpy(buf+len-4, &end, 4);
  9971. memcpy(buf + b_offset, data, eeprom->len);
  9972. }
  9973. ret = tg3_nvram_write_block(tp, offset, len, buf);
  9974. if (buf != data)
  9975. kfree(buf);
  9976. return ret;
  9977. }
  9978. static int tg3_get_link_ksettings(struct net_device *dev,
  9979. struct ethtool_link_ksettings *cmd)
  9980. {
  9981. struct tg3 *tp = netdev_priv(dev);
  9982. u32 supported, advertising;
  9983. if (tg3_flag(tp, USE_PHYLIB)) {
  9984. struct phy_device *phydev;
  9985. if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
  9986. return -EAGAIN;
  9987. phydev = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr);
  9988. phy_ethtool_ksettings_get(phydev, cmd);
  9989. return 0;
  9990. }
  9991. supported = (SUPPORTED_Autoneg);
  9992. if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
  9993. supported |= (SUPPORTED_1000baseT_Half |
  9994. SUPPORTED_1000baseT_Full);
  9995. if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
  9996. supported |= (SUPPORTED_100baseT_Half |
  9997. SUPPORTED_100baseT_Full |
  9998. SUPPORTED_10baseT_Half |
  9999. SUPPORTED_10baseT_Full |
  10000. SUPPORTED_TP);
  10001. cmd->base.port = PORT_TP;
  10002. } else {
  10003. supported |= SUPPORTED_FIBRE;
  10004. cmd->base.port = PORT_FIBRE;
  10005. }
  10006. ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
  10007. supported);
  10008. advertising = tp->link_config.advertising;
  10009. if (tg3_flag(tp, PAUSE_AUTONEG)) {
  10010. if (tp->link_config.flowctrl & FLOW_CTRL_RX) {
  10011. if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
  10012. advertising |= ADVERTISED_Pause;
  10013. } else {
  10014. advertising |= ADVERTISED_Pause |
  10015. ADVERTISED_Asym_Pause;
  10016. }
  10017. } else if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
  10018. advertising |= ADVERTISED_Asym_Pause;
  10019. }
  10020. }
  10021. ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
  10022. advertising);
  10023. if (netif_running(dev) && tp->link_up) {
  10024. cmd->base.speed = tp->link_config.active_speed;
  10025. cmd->base.duplex = tp->link_config.active_duplex;
  10026. ethtool_convert_legacy_u32_to_link_mode(
  10027. cmd->link_modes.lp_advertising,
  10028. tp->link_config.rmt_adv);
  10029. if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
  10030. if (tp->phy_flags & TG3_PHYFLG_MDIX_STATE)
  10031. cmd->base.eth_tp_mdix = ETH_TP_MDI_X;
  10032. else
  10033. cmd->base.eth_tp_mdix = ETH_TP_MDI;
  10034. }
  10035. } else {
  10036. cmd->base.speed = SPEED_UNKNOWN;
  10037. cmd->base.duplex = DUPLEX_UNKNOWN;
  10038. cmd->base.eth_tp_mdix = ETH_TP_MDI_INVALID;
  10039. }
  10040. cmd->base.phy_address = tp->phy_addr;
  10041. cmd->base.autoneg = tp->link_config.autoneg;
  10042. return 0;
  10043. }
  10044. static int tg3_set_link_ksettings(struct net_device *dev,
  10045. const struct ethtool_link_ksettings *cmd)
  10046. {
  10047. struct tg3 *tp = netdev_priv(dev);
  10048. u32 speed = cmd->base.speed;
  10049. u32 advertising;
  10050. if (tg3_flag(tp, USE_PHYLIB)) {
  10051. struct phy_device *phydev;
  10052. if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
  10053. return -EAGAIN;
  10054. phydev = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr);
  10055. return phy_ethtool_ksettings_set(phydev, cmd);
  10056. }
  10057. if (cmd->base.autoneg != AUTONEG_ENABLE &&
  10058. cmd->base.autoneg != AUTONEG_DISABLE)
  10059. return -EINVAL;
  10060. if (cmd->base.autoneg == AUTONEG_DISABLE &&
  10061. cmd->base.duplex != DUPLEX_FULL &&
  10062. cmd->base.duplex != DUPLEX_HALF)
  10063. return -EINVAL;
  10064. ethtool_convert_link_mode_to_legacy_u32(&advertising,
  10065. cmd->link_modes.advertising);
  10066. if (cmd->base.autoneg == AUTONEG_ENABLE) {
  10067. u32 mask = ADVERTISED_Autoneg |
  10068. ADVERTISED_Pause |
  10069. ADVERTISED_Asym_Pause;
  10070. if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
  10071. mask |= ADVERTISED_1000baseT_Half |
  10072. ADVERTISED_1000baseT_Full;
  10073. if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
  10074. mask |= ADVERTISED_100baseT_Half |
  10075. ADVERTISED_100baseT_Full |
  10076. ADVERTISED_10baseT_Half |
  10077. ADVERTISED_10baseT_Full |
  10078. ADVERTISED_TP;
  10079. else
  10080. mask |= ADVERTISED_FIBRE;
  10081. if (advertising & ~mask)
  10082. return -EINVAL;
  10083. mask &= (ADVERTISED_1000baseT_Half |
  10084. ADVERTISED_1000baseT_Full |
  10085. ADVERTISED_100baseT_Half |
  10086. ADVERTISED_100baseT_Full |
  10087. ADVERTISED_10baseT_Half |
  10088. ADVERTISED_10baseT_Full);
  10089. advertising &= mask;
  10090. } else {
  10091. if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
  10092. if (speed != SPEED_1000)
  10093. return -EINVAL;
  10094. if (cmd->base.duplex != DUPLEX_FULL)
  10095. return -EINVAL;
  10096. } else {
  10097. if (speed != SPEED_100 &&
  10098. speed != SPEED_10)
  10099. return -EINVAL;
  10100. }
  10101. }
  10102. tg3_full_lock(tp, 0);
  10103. tp->link_config.autoneg = cmd->base.autoneg;
  10104. if (cmd->base.autoneg == AUTONEG_ENABLE) {
  10105. tp->link_config.advertising = (advertising |
  10106. ADVERTISED_Autoneg);
  10107. tp->link_config.speed = SPEED_UNKNOWN;
  10108. tp->link_config.duplex = DUPLEX_UNKNOWN;
  10109. } else {
  10110. tp->link_config.advertising = 0;
  10111. tp->link_config.speed = speed;
  10112. tp->link_config.duplex = cmd->base.duplex;
  10113. }
  10114. tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
  10115. tg3_warn_mgmt_link_flap(tp);
  10116. if (netif_running(dev))
  10117. tg3_setup_phy(tp, true);
  10118. tg3_full_unlock(tp);
  10119. return 0;
  10120. }
  10121. static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  10122. {
  10123. struct tg3 *tp = netdev_priv(dev);
  10124. strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
  10125. strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
  10126. strlcpy(info->fw_version, tp->fw_ver, sizeof(info->fw_version));
  10127. strlcpy(info->bus_info, pci_name(tp->pdev), sizeof(info->bus_info));
  10128. }
  10129. static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  10130. {
  10131. struct tg3 *tp = netdev_priv(dev);
  10132. if (tg3_flag(tp, WOL_CAP) && device_can_wakeup(&tp->pdev->dev))
  10133. wol->supported = WAKE_MAGIC;
  10134. else
  10135. wol->supported = 0;
  10136. wol->wolopts = 0;
  10137. if (tg3_flag(tp, WOL_ENABLE) && device_can_wakeup(&tp->pdev->dev))
  10138. wol->wolopts = WAKE_MAGIC;
  10139. memset(&wol->sopass, 0, sizeof(wol->sopass));
  10140. }
  10141. static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  10142. {
  10143. struct tg3 *tp = netdev_priv(dev);
  10144. struct device *dp = &tp->pdev->dev;
  10145. if (wol->wolopts & ~WAKE_MAGIC)
  10146. return -EINVAL;
  10147. if ((wol->wolopts & WAKE_MAGIC) &&
  10148. !(tg3_flag(tp, WOL_CAP) && device_can_wakeup(dp)))
  10149. return -EINVAL;
  10150. device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
  10151. if (device_may_wakeup(dp))
  10152. tg3_flag_set(tp, WOL_ENABLE);
  10153. else
  10154. tg3_flag_clear(tp, WOL_ENABLE);
  10155. return 0;
  10156. }
  10157. static u32 tg3_get_msglevel(struct net_device *dev)
  10158. {
  10159. struct tg3 *tp = netdev_priv(dev);
  10160. return tp->msg_enable;
  10161. }
  10162. static void tg3_set_msglevel(struct net_device *dev, u32 value)
  10163. {
  10164. struct tg3 *tp = netdev_priv(dev);
  10165. tp->msg_enable = value;
  10166. }
  10167. static int tg3_nway_reset(struct net_device *dev)
  10168. {
  10169. struct tg3 *tp = netdev_priv(dev);
  10170. int r;
  10171. if (!netif_running(dev))
  10172. return -EAGAIN;
  10173. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
  10174. return -EINVAL;
  10175. tg3_warn_mgmt_link_flap(tp);
  10176. if (tg3_flag(tp, USE_PHYLIB)) {
  10177. if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
  10178. return -EAGAIN;
  10179. r = phy_start_aneg(mdiobus_get_phy(tp->mdio_bus, tp->phy_addr));
  10180. } else {
  10181. u32 bmcr;
  10182. spin_lock_bh(&tp->lock);
  10183. r = -EINVAL;
  10184. tg3_readphy(tp, MII_BMCR, &bmcr);
  10185. if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
  10186. ((bmcr & BMCR_ANENABLE) ||
  10187. (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
  10188. tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
  10189. BMCR_ANENABLE);
  10190. r = 0;
  10191. }
  10192. spin_unlock_bh(&tp->lock);
  10193. }
  10194. return r;
  10195. }
  10196. static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
  10197. {
  10198. struct tg3 *tp = netdev_priv(dev);
  10199. ering->rx_max_pending = tp->rx_std_ring_mask;
  10200. if (tg3_flag(tp, JUMBO_RING_ENABLE))
  10201. ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
  10202. else
  10203. ering->rx_jumbo_max_pending = 0;
  10204. ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
  10205. ering->rx_pending = tp->rx_pending;
  10206. if (tg3_flag(tp, JUMBO_RING_ENABLE))
  10207. ering->rx_jumbo_pending = tp->rx_jumbo_pending;
  10208. else
  10209. ering->rx_jumbo_pending = 0;
  10210. ering->tx_pending = tp->napi[0].tx_pending;
  10211. }
  10212. static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
  10213. {
  10214. struct tg3 *tp = netdev_priv(dev);
  10215. int i, irq_sync = 0, err = 0;
  10216. bool reset_phy = false;
  10217. if ((ering->rx_pending > tp->rx_std_ring_mask) ||
  10218. (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
  10219. (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
  10220. (ering->tx_pending <= MAX_SKB_FRAGS) ||
  10221. (tg3_flag(tp, TSO_BUG) &&
  10222. (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
  10223. return -EINVAL;
  10224. if (netif_running(dev)) {
  10225. tg3_phy_stop(tp);
  10226. tg3_netif_stop(tp);
  10227. irq_sync = 1;
  10228. }
  10229. tg3_full_lock(tp, irq_sync);
  10230. tp->rx_pending = ering->rx_pending;
  10231. if (tg3_flag(tp, MAX_RXPEND_64) &&
  10232. tp->rx_pending > 63)
  10233. tp->rx_pending = 63;
  10234. if (tg3_flag(tp, JUMBO_RING_ENABLE))
  10235. tp->rx_jumbo_pending = ering->rx_jumbo_pending;
  10236. for (i = 0; i < tp->irq_max; i++)
  10237. tp->napi[i].tx_pending = ering->tx_pending;
  10238. if (netif_running(dev)) {
  10239. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  10240. /* Reset PHY to avoid PHY lock up */
  10241. if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
  10242. tg3_asic_rev(tp) == ASIC_REV_5719 ||
  10243. tg3_asic_rev(tp) == ASIC_REV_5720)
  10244. reset_phy = true;
  10245. err = tg3_restart_hw(tp, reset_phy);
  10246. if (!err)
  10247. tg3_netif_start(tp);
  10248. }
  10249. tg3_full_unlock(tp);
  10250. if (irq_sync && !err)
  10251. tg3_phy_start(tp);
  10252. return err;
  10253. }
  10254. static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
  10255. {
  10256. struct tg3 *tp = netdev_priv(dev);
  10257. epause->autoneg = !!tg3_flag(tp, PAUSE_AUTONEG);
  10258. if (tp->link_config.flowctrl & FLOW_CTRL_RX)
  10259. epause->rx_pause = 1;
  10260. else
  10261. epause->rx_pause = 0;
  10262. if (tp->link_config.flowctrl & FLOW_CTRL_TX)
  10263. epause->tx_pause = 1;
  10264. else
  10265. epause->tx_pause = 0;
  10266. }
  10267. static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
  10268. {
  10269. struct tg3 *tp = netdev_priv(dev);
  10270. int err = 0;
  10271. bool reset_phy = false;
  10272. if (tp->link_config.autoneg == AUTONEG_ENABLE)
  10273. tg3_warn_mgmt_link_flap(tp);
  10274. if (tg3_flag(tp, USE_PHYLIB)) {
  10275. u32 newadv;
  10276. struct phy_device *phydev;
  10277. phydev = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr);
  10278. if (!(phydev->supported & SUPPORTED_Pause) ||
  10279. (!(phydev->supported & SUPPORTED_Asym_Pause) &&
  10280. (epause->rx_pause != epause->tx_pause)))
  10281. return -EINVAL;
  10282. tp->link_config.flowctrl = 0;
  10283. if (epause->rx_pause) {
  10284. tp->link_config.flowctrl |= FLOW_CTRL_RX;
  10285. if (epause->tx_pause) {
  10286. tp->link_config.flowctrl |= FLOW_CTRL_TX;
  10287. newadv = ADVERTISED_Pause;
  10288. } else
  10289. newadv = ADVERTISED_Pause |
  10290. ADVERTISED_Asym_Pause;
  10291. } else if (epause->tx_pause) {
  10292. tp->link_config.flowctrl |= FLOW_CTRL_TX;
  10293. newadv = ADVERTISED_Asym_Pause;
  10294. } else
  10295. newadv = 0;
  10296. if (epause->autoneg)
  10297. tg3_flag_set(tp, PAUSE_AUTONEG);
  10298. else
  10299. tg3_flag_clear(tp, PAUSE_AUTONEG);
  10300. if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
  10301. u32 oldadv = phydev->advertising &
  10302. (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
  10303. if (oldadv != newadv) {
  10304. phydev->advertising &=
  10305. ~(ADVERTISED_Pause |
  10306. ADVERTISED_Asym_Pause);
  10307. phydev->advertising |= newadv;
  10308. if (phydev->autoneg) {
  10309. /*
  10310. * Always renegotiate the link to
  10311. * inform our link partner of our
  10312. * flow control settings, even if the
  10313. * flow control is forced. Let
  10314. * tg3_adjust_link() do the final
  10315. * flow control setup.
  10316. */
  10317. return phy_start_aneg(phydev);
  10318. }
  10319. }
  10320. if (!epause->autoneg)
  10321. tg3_setup_flow_control(tp, 0, 0);
  10322. } else {
  10323. tp->link_config.advertising &=
  10324. ~(ADVERTISED_Pause |
  10325. ADVERTISED_Asym_Pause);
  10326. tp->link_config.advertising |= newadv;
  10327. }
  10328. } else {
  10329. int irq_sync = 0;
  10330. if (netif_running(dev)) {
  10331. tg3_netif_stop(tp);
  10332. irq_sync = 1;
  10333. }
  10334. tg3_full_lock(tp, irq_sync);
  10335. if (epause->autoneg)
  10336. tg3_flag_set(tp, PAUSE_AUTONEG);
  10337. else
  10338. tg3_flag_clear(tp, PAUSE_AUTONEG);
  10339. if (epause->rx_pause)
  10340. tp->link_config.flowctrl |= FLOW_CTRL_RX;
  10341. else
  10342. tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
  10343. if (epause->tx_pause)
  10344. tp->link_config.flowctrl |= FLOW_CTRL_TX;
  10345. else
  10346. tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
  10347. if (netif_running(dev)) {
  10348. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  10349. /* Reset PHY to avoid PHY lock up */
  10350. if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
  10351. tg3_asic_rev(tp) == ASIC_REV_5719 ||
  10352. tg3_asic_rev(tp) == ASIC_REV_5720)
  10353. reset_phy = true;
  10354. err = tg3_restart_hw(tp, reset_phy);
  10355. if (!err)
  10356. tg3_netif_start(tp);
  10357. }
  10358. tg3_full_unlock(tp);
  10359. }
  10360. tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
  10361. return err;
  10362. }
  10363. static int tg3_get_sset_count(struct net_device *dev, int sset)
  10364. {
  10365. switch (sset) {
  10366. case ETH_SS_TEST:
  10367. return TG3_NUM_TEST;
  10368. case ETH_SS_STATS:
  10369. return TG3_NUM_STATS;
  10370. default:
  10371. return -EOPNOTSUPP;
  10372. }
  10373. }
  10374. static int tg3_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
  10375. u32 *rules __always_unused)
  10376. {
  10377. struct tg3 *tp = netdev_priv(dev);
  10378. if (!tg3_flag(tp, SUPPORT_MSIX))
  10379. return -EOPNOTSUPP;
  10380. switch (info->cmd) {
  10381. case ETHTOOL_GRXRINGS:
  10382. if (netif_running(tp->dev))
  10383. info->data = tp->rxq_cnt;
  10384. else {
  10385. info->data = num_online_cpus();
  10386. if (info->data > TG3_RSS_MAX_NUM_QS)
  10387. info->data = TG3_RSS_MAX_NUM_QS;
  10388. }
  10389. return 0;
  10390. default:
  10391. return -EOPNOTSUPP;
  10392. }
  10393. }
  10394. static u32 tg3_get_rxfh_indir_size(struct net_device *dev)
  10395. {
  10396. u32 size = 0;
  10397. struct tg3 *tp = netdev_priv(dev);
  10398. if (tg3_flag(tp, SUPPORT_MSIX))
  10399. size = TG3_RSS_INDIR_TBL_SIZE;
  10400. return size;
  10401. }
  10402. static int tg3_get_rxfh(struct net_device *dev, u32 *indir, u8 *key, u8 *hfunc)
  10403. {
  10404. struct tg3 *tp = netdev_priv(dev);
  10405. int i;
  10406. if (hfunc)
  10407. *hfunc = ETH_RSS_HASH_TOP;
  10408. if (!indir)
  10409. return 0;
  10410. for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
  10411. indir[i] = tp->rss_ind_tbl[i];
  10412. return 0;
  10413. }
  10414. static int tg3_set_rxfh(struct net_device *dev, const u32 *indir, const u8 *key,
  10415. const u8 hfunc)
  10416. {
  10417. struct tg3 *tp = netdev_priv(dev);
  10418. size_t i;
  10419. /* We require at least one supported parameter to be changed and no
  10420. * change in any of the unsupported parameters
  10421. */
  10422. if (key ||
  10423. (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP))
  10424. return -EOPNOTSUPP;
  10425. if (!indir)
  10426. return 0;
  10427. for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
  10428. tp->rss_ind_tbl[i] = indir[i];
  10429. if (!netif_running(dev) || !tg3_flag(tp, ENABLE_RSS))
  10430. return 0;
  10431. /* It is legal to write the indirection
  10432. * table while the device is running.
  10433. */
  10434. tg3_full_lock(tp, 0);
  10435. tg3_rss_write_indir_tbl(tp);
  10436. tg3_full_unlock(tp);
  10437. return 0;
  10438. }
  10439. static void tg3_get_channels(struct net_device *dev,
  10440. struct ethtool_channels *channel)
  10441. {
  10442. struct tg3 *tp = netdev_priv(dev);
  10443. u32 deflt_qs = netif_get_num_default_rss_queues();
  10444. channel->max_rx = tp->rxq_max;
  10445. channel->max_tx = tp->txq_max;
  10446. if (netif_running(dev)) {
  10447. channel->rx_count = tp->rxq_cnt;
  10448. channel->tx_count = tp->txq_cnt;
  10449. } else {
  10450. if (tp->rxq_req)
  10451. channel->rx_count = tp->rxq_req;
  10452. else
  10453. channel->rx_count = min(deflt_qs, tp->rxq_max);
  10454. if (tp->txq_req)
  10455. channel->tx_count = tp->txq_req;
  10456. else
  10457. channel->tx_count = min(deflt_qs, tp->txq_max);
  10458. }
  10459. }
  10460. static int tg3_set_channels(struct net_device *dev,
  10461. struct ethtool_channels *channel)
  10462. {
  10463. struct tg3 *tp = netdev_priv(dev);
  10464. if (!tg3_flag(tp, SUPPORT_MSIX))
  10465. return -EOPNOTSUPP;
  10466. if (channel->rx_count > tp->rxq_max ||
  10467. channel->tx_count > tp->txq_max)
  10468. return -EINVAL;
  10469. tp->rxq_req = channel->rx_count;
  10470. tp->txq_req = channel->tx_count;
  10471. if (!netif_running(dev))
  10472. return 0;
  10473. tg3_stop(tp);
  10474. tg3_carrier_off(tp);
  10475. tg3_start(tp, true, false, false);
  10476. return 0;
  10477. }
  10478. static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
  10479. {
  10480. switch (stringset) {
  10481. case ETH_SS_STATS:
  10482. memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
  10483. break;
  10484. case ETH_SS_TEST:
  10485. memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
  10486. break;
  10487. default:
  10488. WARN_ON(1); /* we need a WARN() */
  10489. break;
  10490. }
  10491. }
  10492. static int tg3_set_phys_id(struct net_device *dev,
  10493. enum ethtool_phys_id_state state)
  10494. {
  10495. struct tg3 *tp = netdev_priv(dev);
  10496. if (!netif_running(tp->dev))
  10497. return -EAGAIN;
  10498. switch (state) {
  10499. case ETHTOOL_ID_ACTIVE:
  10500. return 1; /* cycle on/off once per second */
  10501. case ETHTOOL_ID_ON:
  10502. tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
  10503. LED_CTRL_1000MBPS_ON |
  10504. LED_CTRL_100MBPS_ON |
  10505. LED_CTRL_10MBPS_ON |
  10506. LED_CTRL_TRAFFIC_OVERRIDE |
  10507. LED_CTRL_TRAFFIC_BLINK |
  10508. LED_CTRL_TRAFFIC_LED);
  10509. break;
  10510. case ETHTOOL_ID_OFF:
  10511. tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
  10512. LED_CTRL_TRAFFIC_OVERRIDE);
  10513. break;
  10514. case ETHTOOL_ID_INACTIVE:
  10515. tw32(MAC_LED_CTRL, tp->led_ctrl);
  10516. break;
  10517. }
  10518. return 0;
  10519. }
  10520. static void tg3_get_ethtool_stats(struct net_device *dev,
  10521. struct ethtool_stats *estats, u64 *tmp_stats)
  10522. {
  10523. struct tg3 *tp = netdev_priv(dev);
  10524. if (tp->hw_stats)
  10525. tg3_get_estats(tp, (struct tg3_ethtool_stats *)tmp_stats);
  10526. else
  10527. memset(tmp_stats, 0, sizeof(struct tg3_ethtool_stats));
  10528. }
  10529. static __be32 *tg3_vpd_readblock(struct tg3 *tp, u32 *vpdlen)
  10530. {
  10531. int i;
  10532. __be32 *buf;
  10533. u32 offset = 0, len = 0;
  10534. u32 magic, val;
  10535. if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &magic))
  10536. return NULL;
  10537. if (magic == TG3_EEPROM_MAGIC) {
  10538. for (offset = TG3_NVM_DIR_START;
  10539. offset < TG3_NVM_DIR_END;
  10540. offset += TG3_NVM_DIRENT_SIZE) {
  10541. if (tg3_nvram_read(tp, offset, &val))
  10542. return NULL;
  10543. if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
  10544. TG3_NVM_DIRTYPE_EXTVPD)
  10545. break;
  10546. }
  10547. if (offset != TG3_NVM_DIR_END) {
  10548. len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
  10549. if (tg3_nvram_read(tp, offset + 4, &offset))
  10550. return NULL;
  10551. offset = tg3_nvram_logical_addr(tp, offset);
  10552. }
  10553. }
  10554. if (!offset || !len) {
  10555. offset = TG3_NVM_VPD_OFF;
  10556. len = TG3_NVM_VPD_LEN;
  10557. }
  10558. buf = kmalloc(len, GFP_KERNEL);
  10559. if (buf == NULL)
  10560. return NULL;
  10561. if (magic == TG3_EEPROM_MAGIC) {
  10562. for (i = 0; i < len; i += 4) {
  10563. /* The data is in little-endian format in NVRAM.
  10564. * Use the big-endian read routines to preserve
  10565. * the byte order as it exists in NVRAM.
  10566. */
  10567. if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
  10568. goto error;
  10569. }
  10570. } else {
  10571. u8 *ptr;
  10572. ssize_t cnt;
  10573. unsigned int pos = 0;
  10574. ptr = (u8 *)&buf[0];
  10575. for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
  10576. cnt = pci_read_vpd(tp->pdev, pos,
  10577. len - pos, ptr);
  10578. if (cnt == -ETIMEDOUT || cnt == -EINTR)
  10579. cnt = 0;
  10580. else if (cnt < 0)
  10581. goto error;
  10582. }
  10583. if (pos != len)
  10584. goto error;
  10585. }
  10586. *vpdlen = len;
  10587. return buf;
  10588. error:
  10589. kfree(buf);
  10590. return NULL;
  10591. }
  10592. #define NVRAM_TEST_SIZE 0x100
  10593. #define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
  10594. #define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
  10595. #define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
  10596. #define NVRAM_SELFBOOT_FORMAT1_4_SIZE 0x20
  10597. #define NVRAM_SELFBOOT_FORMAT1_5_SIZE 0x24
  10598. #define NVRAM_SELFBOOT_FORMAT1_6_SIZE 0x50
  10599. #define NVRAM_SELFBOOT_HW_SIZE 0x20
  10600. #define NVRAM_SELFBOOT_DATA_SIZE 0x1c
  10601. static int tg3_test_nvram(struct tg3 *tp)
  10602. {
  10603. u32 csum, magic, len;
  10604. __be32 *buf;
  10605. int i, j, k, err = 0, size;
  10606. if (tg3_flag(tp, NO_NVRAM))
  10607. return 0;
  10608. if (tg3_nvram_read(tp, 0, &magic) != 0)
  10609. return -EIO;
  10610. if (magic == TG3_EEPROM_MAGIC)
  10611. size = NVRAM_TEST_SIZE;
  10612. else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
  10613. if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
  10614. TG3_EEPROM_SB_FORMAT_1) {
  10615. switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
  10616. case TG3_EEPROM_SB_REVISION_0:
  10617. size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
  10618. break;
  10619. case TG3_EEPROM_SB_REVISION_2:
  10620. size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
  10621. break;
  10622. case TG3_EEPROM_SB_REVISION_3:
  10623. size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
  10624. break;
  10625. case TG3_EEPROM_SB_REVISION_4:
  10626. size = NVRAM_SELFBOOT_FORMAT1_4_SIZE;
  10627. break;
  10628. case TG3_EEPROM_SB_REVISION_5:
  10629. size = NVRAM_SELFBOOT_FORMAT1_5_SIZE;
  10630. break;
  10631. case TG3_EEPROM_SB_REVISION_6:
  10632. size = NVRAM_SELFBOOT_FORMAT1_6_SIZE;
  10633. break;
  10634. default:
  10635. return -EIO;
  10636. }
  10637. } else
  10638. return 0;
  10639. } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
  10640. size = NVRAM_SELFBOOT_HW_SIZE;
  10641. else
  10642. return -EIO;
  10643. buf = kmalloc(size, GFP_KERNEL);
  10644. if (buf == NULL)
  10645. return -ENOMEM;
  10646. err = -EIO;
  10647. for (i = 0, j = 0; i < size; i += 4, j++) {
  10648. err = tg3_nvram_read_be32(tp, i, &buf[j]);
  10649. if (err)
  10650. break;
  10651. }
  10652. if (i < size)
  10653. goto out;
  10654. /* Selfboot format */
  10655. magic = be32_to_cpu(buf[0]);
  10656. if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
  10657. TG3_EEPROM_MAGIC_FW) {
  10658. u8 *buf8 = (u8 *) buf, csum8 = 0;
  10659. if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
  10660. TG3_EEPROM_SB_REVISION_2) {
  10661. /* For rev 2, the csum doesn't include the MBA. */
  10662. for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
  10663. csum8 += buf8[i];
  10664. for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
  10665. csum8 += buf8[i];
  10666. } else {
  10667. for (i = 0; i < size; i++)
  10668. csum8 += buf8[i];
  10669. }
  10670. if (csum8 == 0) {
  10671. err = 0;
  10672. goto out;
  10673. }
  10674. err = -EIO;
  10675. goto out;
  10676. }
  10677. if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
  10678. TG3_EEPROM_MAGIC_HW) {
  10679. u8 data[NVRAM_SELFBOOT_DATA_SIZE];
  10680. u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
  10681. u8 *buf8 = (u8 *) buf;
  10682. /* Separate the parity bits and the data bytes. */
  10683. for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
  10684. if ((i == 0) || (i == 8)) {
  10685. int l;
  10686. u8 msk;
  10687. for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
  10688. parity[k++] = buf8[i] & msk;
  10689. i++;
  10690. } else if (i == 16) {
  10691. int l;
  10692. u8 msk;
  10693. for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
  10694. parity[k++] = buf8[i] & msk;
  10695. i++;
  10696. for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
  10697. parity[k++] = buf8[i] & msk;
  10698. i++;
  10699. }
  10700. data[j++] = buf8[i];
  10701. }
  10702. err = -EIO;
  10703. for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
  10704. u8 hw8 = hweight8(data[i]);
  10705. if ((hw8 & 0x1) && parity[i])
  10706. goto out;
  10707. else if (!(hw8 & 0x1) && !parity[i])
  10708. goto out;
  10709. }
  10710. err = 0;
  10711. goto out;
  10712. }
  10713. err = -EIO;
  10714. /* Bootstrap checksum at offset 0x10 */
  10715. csum = calc_crc((unsigned char *) buf, 0x10);
  10716. if (csum != le32_to_cpu(buf[0x10/4]))
  10717. goto out;
  10718. /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
  10719. csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
  10720. if (csum != le32_to_cpu(buf[0xfc/4]))
  10721. goto out;
  10722. kfree(buf);
  10723. buf = tg3_vpd_readblock(tp, &len);
  10724. if (!buf)
  10725. return -ENOMEM;
  10726. i = pci_vpd_find_tag((u8 *)buf, 0, len, PCI_VPD_LRDT_RO_DATA);
  10727. if (i > 0) {
  10728. j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
  10729. if (j < 0)
  10730. goto out;
  10731. if (i + PCI_VPD_LRDT_TAG_SIZE + j > len)
  10732. goto out;
  10733. i += PCI_VPD_LRDT_TAG_SIZE;
  10734. j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
  10735. PCI_VPD_RO_KEYWORD_CHKSUM);
  10736. if (j > 0) {
  10737. u8 csum8 = 0;
  10738. j += PCI_VPD_INFO_FLD_HDR_SIZE;
  10739. for (i = 0; i <= j; i++)
  10740. csum8 += ((u8 *)buf)[i];
  10741. if (csum8)
  10742. goto out;
  10743. }
  10744. }
  10745. err = 0;
  10746. out:
  10747. kfree(buf);
  10748. return err;
  10749. }
  10750. #define TG3_SERDES_TIMEOUT_SEC 2
  10751. #define TG3_COPPER_TIMEOUT_SEC 6
  10752. static int tg3_test_link(struct tg3 *tp)
  10753. {
  10754. int i, max;
  10755. if (!netif_running(tp->dev))
  10756. return -ENODEV;
  10757. if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
  10758. max = TG3_SERDES_TIMEOUT_SEC;
  10759. else
  10760. max = TG3_COPPER_TIMEOUT_SEC;
  10761. for (i = 0; i < max; i++) {
  10762. if (tp->link_up)
  10763. return 0;
  10764. if (msleep_interruptible(1000))
  10765. break;
  10766. }
  10767. return -EIO;
  10768. }
  10769. /* Only test the commonly used registers */
  10770. static int tg3_test_registers(struct tg3 *tp)
  10771. {
  10772. int i, is_5705, is_5750;
  10773. u32 offset, read_mask, write_mask, val, save_val, read_val;
  10774. static struct {
  10775. u16 offset;
  10776. u16 flags;
  10777. #define TG3_FL_5705 0x1
  10778. #define TG3_FL_NOT_5705 0x2
  10779. #define TG3_FL_NOT_5788 0x4
  10780. #define TG3_FL_NOT_5750 0x8
  10781. u32 read_mask;
  10782. u32 write_mask;
  10783. } reg_tbl[] = {
  10784. /* MAC Control Registers */
  10785. { MAC_MODE, TG3_FL_NOT_5705,
  10786. 0x00000000, 0x00ef6f8c },
  10787. { MAC_MODE, TG3_FL_5705,
  10788. 0x00000000, 0x01ef6b8c },
  10789. { MAC_STATUS, TG3_FL_NOT_5705,
  10790. 0x03800107, 0x00000000 },
  10791. { MAC_STATUS, TG3_FL_5705,
  10792. 0x03800100, 0x00000000 },
  10793. { MAC_ADDR_0_HIGH, 0x0000,
  10794. 0x00000000, 0x0000ffff },
  10795. { MAC_ADDR_0_LOW, 0x0000,
  10796. 0x00000000, 0xffffffff },
  10797. { MAC_RX_MTU_SIZE, 0x0000,
  10798. 0x00000000, 0x0000ffff },
  10799. { MAC_TX_MODE, 0x0000,
  10800. 0x00000000, 0x00000070 },
  10801. { MAC_TX_LENGTHS, 0x0000,
  10802. 0x00000000, 0x00003fff },
  10803. { MAC_RX_MODE, TG3_FL_NOT_5705,
  10804. 0x00000000, 0x000007fc },
  10805. { MAC_RX_MODE, TG3_FL_5705,
  10806. 0x00000000, 0x000007dc },
  10807. { MAC_HASH_REG_0, 0x0000,
  10808. 0x00000000, 0xffffffff },
  10809. { MAC_HASH_REG_1, 0x0000,
  10810. 0x00000000, 0xffffffff },
  10811. { MAC_HASH_REG_2, 0x0000,
  10812. 0x00000000, 0xffffffff },
  10813. { MAC_HASH_REG_3, 0x0000,
  10814. 0x00000000, 0xffffffff },
  10815. /* Receive Data and Receive BD Initiator Control Registers. */
  10816. { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
  10817. 0x00000000, 0xffffffff },
  10818. { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
  10819. 0x00000000, 0xffffffff },
  10820. { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
  10821. 0x00000000, 0x00000003 },
  10822. { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
  10823. 0x00000000, 0xffffffff },
  10824. { RCVDBDI_STD_BD+0, 0x0000,
  10825. 0x00000000, 0xffffffff },
  10826. { RCVDBDI_STD_BD+4, 0x0000,
  10827. 0x00000000, 0xffffffff },
  10828. { RCVDBDI_STD_BD+8, 0x0000,
  10829. 0x00000000, 0xffff0002 },
  10830. { RCVDBDI_STD_BD+0xc, 0x0000,
  10831. 0x00000000, 0xffffffff },
  10832. /* Receive BD Initiator Control Registers. */
  10833. { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
  10834. 0x00000000, 0xffffffff },
  10835. { RCVBDI_STD_THRESH, TG3_FL_5705,
  10836. 0x00000000, 0x000003ff },
  10837. { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
  10838. 0x00000000, 0xffffffff },
  10839. /* Host Coalescing Control Registers. */
  10840. { HOSTCC_MODE, TG3_FL_NOT_5705,
  10841. 0x00000000, 0x00000004 },
  10842. { HOSTCC_MODE, TG3_FL_5705,
  10843. 0x00000000, 0x000000f6 },
  10844. { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
  10845. 0x00000000, 0xffffffff },
  10846. { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
  10847. 0x00000000, 0x000003ff },
  10848. { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
  10849. 0x00000000, 0xffffffff },
  10850. { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
  10851. 0x00000000, 0x000003ff },
  10852. { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
  10853. 0x00000000, 0xffffffff },
  10854. { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
  10855. 0x00000000, 0x000000ff },
  10856. { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
  10857. 0x00000000, 0xffffffff },
  10858. { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
  10859. 0x00000000, 0x000000ff },
  10860. { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
  10861. 0x00000000, 0xffffffff },
  10862. { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
  10863. 0x00000000, 0xffffffff },
  10864. { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
  10865. 0x00000000, 0xffffffff },
  10866. { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
  10867. 0x00000000, 0x000000ff },
  10868. { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
  10869. 0x00000000, 0xffffffff },
  10870. { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
  10871. 0x00000000, 0x000000ff },
  10872. { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
  10873. 0x00000000, 0xffffffff },
  10874. { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
  10875. 0x00000000, 0xffffffff },
  10876. { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
  10877. 0x00000000, 0xffffffff },
  10878. { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
  10879. 0x00000000, 0xffffffff },
  10880. { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
  10881. 0x00000000, 0xffffffff },
  10882. { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
  10883. 0xffffffff, 0x00000000 },
  10884. { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
  10885. 0xffffffff, 0x00000000 },
  10886. /* Buffer Manager Control Registers. */
  10887. { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
  10888. 0x00000000, 0x007fff80 },
  10889. { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
  10890. 0x00000000, 0x007fffff },
  10891. { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
  10892. 0x00000000, 0x0000003f },
  10893. { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
  10894. 0x00000000, 0x000001ff },
  10895. { BUFMGR_MB_HIGH_WATER, 0x0000,
  10896. 0x00000000, 0x000001ff },
  10897. { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
  10898. 0xffffffff, 0x00000000 },
  10899. { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
  10900. 0xffffffff, 0x00000000 },
  10901. /* Mailbox Registers */
  10902. { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
  10903. 0x00000000, 0x000001ff },
  10904. { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
  10905. 0x00000000, 0x000001ff },
  10906. { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
  10907. 0x00000000, 0x000007ff },
  10908. { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
  10909. 0x00000000, 0x000001ff },
  10910. { 0xffff, 0x0000, 0x00000000, 0x00000000 },
  10911. };
  10912. is_5705 = is_5750 = 0;
  10913. if (tg3_flag(tp, 5705_PLUS)) {
  10914. is_5705 = 1;
  10915. if (tg3_flag(tp, 5750_PLUS))
  10916. is_5750 = 1;
  10917. }
  10918. for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
  10919. if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
  10920. continue;
  10921. if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
  10922. continue;
  10923. if (tg3_flag(tp, IS_5788) &&
  10924. (reg_tbl[i].flags & TG3_FL_NOT_5788))
  10925. continue;
  10926. if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
  10927. continue;
  10928. offset = (u32) reg_tbl[i].offset;
  10929. read_mask = reg_tbl[i].read_mask;
  10930. write_mask = reg_tbl[i].write_mask;
  10931. /* Save the original register content */
  10932. save_val = tr32(offset);
  10933. /* Determine the read-only value. */
  10934. read_val = save_val & read_mask;
  10935. /* Write zero to the register, then make sure the read-only bits
  10936. * are not changed and the read/write bits are all zeros.
  10937. */
  10938. tw32(offset, 0);
  10939. val = tr32(offset);
  10940. /* Test the read-only and read/write bits. */
  10941. if (((val & read_mask) != read_val) || (val & write_mask))
  10942. goto out;
  10943. /* Write ones to all the bits defined by RdMask and WrMask, then
  10944. * make sure the read-only bits are not changed and the
  10945. * read/write bits are all ones.
  10946. */
  10947. tw32(offset, read_mask | write_mask);
  10948. val = tr32(offset);
  10949. /* Test the read-only bits. */
  10950. if ((val & read_mask) != read_val)
  10951. goto out;
  10952. /* Test the read/write bits. */
  10953. if ((val & write_mask) != write_mask)
  10954. goto out;
  10955. tw32(offset, save_val);
  10956. }
  10957. return 0;
  10958. out:
  10959. if (netif_msg_hw(tp))
  10960. netdev_err(tp->dev,
  10961. "Register test failed at offset %x\n", offset);
  10962. tw32(offset, save_val);
  10963. return -EIO;
  10964. }
  10965. static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
  10966. {
  10967. static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
  10968. int i;
  10969. u32 j;
  10970. for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
  10971. for (j = 0; j < len; j += 4) {
  10972. u32 val;
  10973. tg3_write_mem(tp, offset + j, test_pattern[i]);
  10974. tg3_read_mem(tp, offset + j, &val);
  10975. if (val != test_pattern[i])
  10976. return -EIO;
  10977. }
  10978. }
  10979. return 0;
  10980. }
  10981. static int tg3_test_memory(struct tg3 *tp)
  10982. {
  10983. static struct mem_entry {
  10984. u32 offset;
  10985. u32 len;
  10986. } mem_tbl_570x[] = {
  10987. { 0x00000000, 0x00b50},
  10988. { 0x00002000, 0x1c000},
  10989. { 0xffffffff, 0x00000}
  10990. }, mem_tbl_5705[] = {
  10991. { 0x00000100, 0x0000c},
  10992. { 0x00000200, 0x00008},
  10993. { 0x00004000, 0x00800},
  10994. { 0x00006000, 0x01000},
  10995. { 0x00008000, 0x02000},
  10996. { 0x00010000, 0x0e000},
  10997. { 0xffffffff, 0x00000}
  10998. }, mem_tbl_5755[] = {
  10999. { 0x00000200, 0x00008},
  11000. { 0x00004000, 0x00800},
  11001. { 0x00006000, 0x00800},
  11002. { 0x00008000, 0x02000},
  11003. { 0x00010000, 0x0c000},
  11004. { 0xffffffff, 0x00000}
  11005. }, mem_tbl_5906[] = {
  11006. { 0x00000200, 0x00008},
  11007. { 0x00004000, 0x00400},
  11008. { 0x00006000, 0x00400},
  11009. { 0x00008000, 0x01000},
  11010. { 0x00010000, 0x01000},
  11011. { 0xffffffff, 0x00000}
  11012. }, mem_tbl_5717[] = {
  11013. { 0x00000200, 0x00008},
  11014. { 0x00010000, 0x0a000},
  11015. { 0x00020000, 0x13c00},
  11016. { 0xffffffff, 0x00000}
  11017. }, mem_tbl_57765[] = {
  11018. { 0x00000200, 0x00008},
  11019. { 0x00004000, 0x00800},
  11020. { 0x00006000, 0x09800},
  11021. { 0x00010000, 0x0a000},
  11022. { 0xffffffff, 0x00000}
  11023. };
  11024. struct mem_entry *mem_tbl;
  11025. int err = 0;
  11026. int i;
  11027. if (tg3_flag(tp, 5717_PLUS))
  11028. mem_tbl = mem_tbl_5717;
  11029. else if (tg3_flag(tp, 57765_CLASS) ||
  11030. tg3_asic_rev(tp) == ASIC_REV_5762)
  11031. mem_tbl = mem_tbl_57765;
  11032. else if (tg3_flag(tp, 5755_PLUS))
  11033. mem_tbl = mem_tbl_5755;
  11034. else if (tg3_asic_rev(tp) == ASIC_REV_5906)
  11035. mem_tbl = mem_tbl_5906;
  11036. else if (tg3_flag(tp, 5705_PLUS))
  11037. mem_tbl = mem_tbl_5705;
  11038. else
  11039. mem_tbl = mem_tbl_570x;
  11040. for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
  11041. err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
  11042. if (err)
  11043. break;
  11044. }
  11045. return err;
  11046. }
  11047. #define TG3_TSO_MSS 500
  11048. #define TG3_TSO_IP_HDR_LEN 20
  11049. #define TG3_TSO_TCP_HDR_LEN 20
  11050. #define TG3_TSO_TCP_OPT_LEN 12
  11051. static const u8 tg3_tso_header[] = {
  11052. 0x08, 0x00,
  11053. 0x45, 0x00, 0x00, 0x00,
  11054. 0x00, 0x00, 0x40, 0x00,
  11055. 0x40, 0x06, 0x00, 0x00,
  11056. 0x0a, 0x00, 0x00, 0x01,
  11057. 0x0a, 0x00, 0x00, 0x02,
  11058. 0x0d, 0x00, 0xe0, 0x00,
  11059. 0x00, 0x00, 0x01, 0x00,
  11060. 0x00, 0x00, 0x02, 0x00,
  11061. 0x80, 0x10, 0x10, 0x00,
  11062. 0x14, 0x09, 0x00, 0x00,
  11063. 0x01, 0x01, 0x08, 0x0a,
  11064. 0x11, 0x11, 0x11, 0x11,
  11065. 0x11, 0x11, 0x11, 0x11,
  11066. };
  11067. static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, bool tso_loopback)
  11068. {
  11069. u32 rx_start_idx, rx_idx, tx_idx, opaque_key;
  11070. u32 base_flags = 0, mss = 0, desc_idx, coal_now, data_off, val;
  11071. u32 budget;
  11072. struct sk_buff *skb;
  11073. u8 *tx_data, *rx_data;
  11074. dma_addr_t map;
  11075. int num_pkts, tx_len, rx_len, i, err;
  11076. struct tg3_rx_buffer_desc *desc;
  11077. struct tg3_napi *tnapi, *rnapi;
  11078. struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
  11079. tnapi = &tp->napi[0];
  11080. rnapi = &tp->napi[0];
  11081. if (tp->irq_cnt > 1) {
  11082. if (tg3_flag(tp, ENABLE_RSS))
  11083. rnapi = &tp->napi[1];
  11084. if (tg3_flag(tp, ENABLE_TSS))
  11085. tnapi = &tp->napi[1];
  11086. }
  11087. coal_now = tnapi->coal_now | rnapi->coal_now;
  11088. err = -EIO;
  11089. tx_len = pktsz;
  11090. skb = netdev_alloc_skb(tp->dev, tx_len);
  11091. if (!skb)
  11092. return -ENOMEM;
  11093. tx_data = skb_put(skb, tx_len);
  11094. memcpy(tx_data, tp->dev->dev_addr, ETH_ALEN);
  11095. memset(tx_data + ETH_ALEN, 0x0, 8);
  11096. tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
  11097. if (tso_loopback) {
  11098. struct iphdr *iph = (struct iphdr *)&tx_data[ETH_HLEN];
  11099. u32 hdr_len = TG3_TSO_IP_HDR_LEN + TG3_TSO_TCP_HDR_LEN +
  11100. TG3_TSO_TCP_OPT_LEN;
  11101. memcpy(tx_data + ETH_ALEN * 2, tg3_tso_header,
  11102. sizeof(tg3_tso_header));
  11103. mss = TG3_TSO_MSS;
  11104. val = tx_len - ETH_ALEN * 2 - sizeof(tg3_tso_header);
  11105. num_pkts = DIV_ROUND_UP(val, TG3_TSO_MSS);
  11106. /* Set the total length field in the IP header */
  11107. iph->tot_len = htons((u16)(mss + hdr_len));
  11108. base_flags = (TXD_FLAG_CPU_PRE_DMA |
  11109. TXD_FLAG_CPU_POST_DMA);
  11110. if (tg3_flag(tp, HW_TSO_1) ||
  11111. tg3_flag(tp, HW_TSO_2) ||
  11112. tg3_flag(tp, HW_TSO_3)) {
  11113. struct tcphdr *th;
  11114. val = ETH_HLEN + TG3_TSO_IP_HDR_LEN;
  11115. th = (struct tcphdr *)&tx_data[val];
  11116. th->check = 0;
  11117. } else
  11118. base_flags |= TXD_FLAG_TCPUDP_CSUM;
  11119. if (tg3_flag(tp, HW_TSO_3)) {
  11120. mss |= (hdr_len & 0xc) << 12;
  11121. if (hdr_len & 0x10)
  11122. base_flags |= 0x00000010;
  11123. base_flags |= (hdr_len & 0x3e0) << 5;
  11124. } else if (tg3_flag(tp, HW_TSO_2))
  11125. mss |= hdr_len << 9;
  11126. else if (tg3_flag(tp, HW_TSO_1) ||
  11127. tg3_asic_rev(tp) == ASIC_REV_5705) {
  11128. mss |= (TG3_TSO_TCP_OPT_LEN << 9);
  11129. } else {
  11130. base_flags |= (TG3_TSO_TCP_OPT_LEN << 10);
  11131. }
  11132. data_off = ETH_ALEN * 2 + sizeof(tg3_tso_header);
  11133. } else {
  11134. num_pkts = 1;
  11135. data_off = ETH_HLEN;
  11136. if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
  11137. tx_len > VLAN_ETH_FRAME_LEN)
  11138. base_flags |= TXD_FLAG_JMB_PKT;
  11139. }
  11140. for (i = data_off; i < tx_len; i++)
  11141. tx_data[i] = (u8) (i & 0xff);
  11142. map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
  11143. if (pci_dma_mapping_error(tp->pdev, map)) {
  11144. dev_kfree_skb(skb);
  11145. return -EIO;
  11146. }
  11147. val = tnapi->tx_prod;
  11148. tnapi->tx_buffers[val].skb = skb;
  11149. dma_unmap_addr_set(&tnapi->tx_buffers[val], mapping, map);
  11150. tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
  11151. rnapi->coal_now);
  11152. udelay(10);
  11153. rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
  11154. budget = tg3_tx_avail(tnapi);
  11155. if (tg3_tx_frag_set(tnapi, &val, &budget, map, tx_len,
  11156. base_flags | TXD_FLAG_END, mss, 0)) {
  11157. tnapi->tx_buffers[val].skb = NULL;
  11158. dev_kfree_skb(skb);
  11159. return -EIO;
  11160. }
  11161. tnapi->tx_prod++;
  11162. /* Sync BD data before updating mailbox */
  11163. wmb();
  11164. tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
  11165. tr32_mailbox(tnapi->prodmbox);
  11166. udelay(10);
  11167. /* 350 usec to allow enough time on some 10/100 Mbps devices. */
  11168. for (i = 0; i < 35; i++) {
  11169. tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
  11170. coal_now);
  11171. udelay(10);
  11172. tx_idx = tnapi->hw_status->idx[0].tx_consumer;
  11173. rx_idx = rnapi->hw_status->idx[0].rx_producer;
  11174. if ((tx_idx == tnapi->tx_prod) &&
  11175. (rx_idx == (rx_start_idx + num_pkts)))
  11176. break;
  11177. }
  11178. tg3_tx_skb_unmap(tnapi, tnapi->tx_prod - 1, -1);
  11179. dev_kfree_skb(skb);
  11180. if (tx_idx != tnapi->tx_prod)
  11181. goto out;
  11182. if (rx_idx != rx_start_idx + num_pkts)
  11183. goto out;
  11184. val = data_off;
  11185. while (rx_idx != rx_start_idx) {
  11186. desc = &rnapi->rx_rcb[rx_start_idx++];
  11187. desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
  11188. opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
  11189. if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
  11190. (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
  11191. goto out;
  11192. rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT)
  11193. - ETH_FCS_LEN;
  11194. if (!tso_loopback) {
  11195. if (rx_len != tx_len)
  11196. goto out;
  11197. if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
  11198. if (opaque_key != RXD_OPAQUE_RING_STD)
  11199. goto out;
  11200. } else {
  11201. if (opaque_key != RXD_OPAQUE_RING_JUMBO)
  11202. goto out;
  11203. }
  11204. } else if ((desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
  11205. (desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
  11206. >> RXD_TCPCSUM_SHIFT != 0xffff) {
  11207. goto out;
  11208. }
  11209. if (opaque_key == RXD_OPAQUE_RING_STD) {
  11210. rx_data = tpr->rx_std_buffers[desc_idx].data;
  11211. map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx],
  11212. mapping);
  11213. } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
  11214. rx_data = tpr->rx_jmb_buffers[desc_idx].data;
  11215. map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx],
  11216. mapping);
  11217. } else
  11218. goto out;
  11219. pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len,
  11220. PCI_DMA_FROMDEVICE);
  11221. rx_data += TG3_RX_OFFSET(tp);
  11222. for (i = data_off; i < rx_len; i++, val++) {
  11223. if (*(rx_data + i) != (u8) (val & 0xff))
  11224. goto out;
  11225. }
  11226. }
  11227. err = 0;
  11228. /* tg3_free_rings will unmap and free the rx_data */
  11229. out:
  11230. return err;
  11231. }
  11232. #define TG3_STD_LOOPBACK_FAILED 1
  11233. #define TG3_JMB_LOOPBACK_FAILED 2
  11234. #define TG3_TSO_LOOPBACK_FAILED 4
  11235. #define TG3_LOOPBACK_FAILED \
  11236. (TG3_STD_LOOPBACK_FAILED | \
  11237. TG3_JMB_LOOPBACK_FAILED | \
  11238. TG3_TSO_LOOPBACK_FAILED)
  11239. static int tg3_test_loopback(struct tg3 *tp, u64 *data, bool do_extlpbk)
  11240. {
  11241. int err = -EIO;
  11242. u32 eee_cap;
  11243. u32 jmb_pkt_sz = 9000;
  11244. if (tp->dma_limit)
  11245. jmb_pkt_sz = tp->dma_limit - ETH_HLEN;
  11246. eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
  11247. tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
  11248. if (!netif_running(tp->dev)) {
  11249. data[TG3_MAC_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
  11250. data[TG3_PHY_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
  11251. if (do_extlpbk)
  11252. data[TG3_EXT_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
  11253. goto done;
  11254. }
  11255. err = tg3_reset_hw(tp, true);
  11256. if (err) {
  11257. data[TG3_MAC_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
  11258. data[TG3_PHY_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
  11259. if (do_extlpbk)
  11260. data[TG3_EXT_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
  11261. goto done;
  11262. }
  11263. if (tg3_flag(tp, ENABLE_RSS)) {
  11264. int i;
  11265. /* Reroute all rx packets to the 1st queue */
  11266. for (i = MAC_RSS_INDIR_TBL_0;
  11267. i < MAC_RSS_INDIR_TBL_0 + TG3_RSS_INDIR_TBL_SIZE; i += 4)
  11268. tw32(i, 0x0);
  11269. }
  11270. /* HW errata - mac loopback fails in some cases on 5780.
  11271. * Normal traffic and PHY loopback are not affected by
  11272. * errata. Also, the MAC loopback test is deprecated for
  11273. * all newer ASIC revisions.
  11274. */
  11275. if (tg3_asic_rev(tp) != ASIC_REV_5780 &&
  11276. !tg3_flag(tp, CPMU_PRESENT)) {
  11277. tg3_mac_loopback(tp, true);
  11278. if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
  11279. data[TG3_MAC_LOOPB_TEST] |= TG3_STD_LOOPBACK_FAILED;
  11280. if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
  11281. tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
  11282. data[TG3_MAC_LOOPB_TEST] |= TG3_JMB_LOOPBACK_FAILED;
  11283. tg3_mac_loopback(tp, false);
  11284. }
  11285. if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
  11286. !tg3_flag(tp, USE_PHYLIB)) {
  11287. int i;
  11288. tg3_phy_lpbk_set(tp, 0, false);
  11289. /* Wait for link */
  11290. for (i = 0; i < 100; i++) {
  11291. if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
  11292. break;
  11293. mdelay(1);
  11294. }
  11295. if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
  11296. data[TG3_PHY_LOOPB_TEST] |= TG3_STD_LOOPBACK_FAILED;
  11297. if (tg3_flag(tp, TSO_CAPABLE) &&
  11298. tg3_run_loopback(tp, ETH_FRAME_LEN, true))
  11299. data[TG3_PHY_LOOPB_TEST] |= TG3_TSO_LOOPBACK_FAILED;
  11300. if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
  11301. tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
  11302. data[TG3_PHY_LOOPB_TEST] |= TG3_JMB_LOOPBACK_FAILED;
  11303. if (do_extlpbk) {
  11304. tg3_phy_lpbk_set(tp, 0, true);
  11305. /* All link indications report up, but the hardware
  11306. * isn't really ready for about 20 msec. Double it
  11307. * to be sure.
  11308. */
  11309. mdelay(40);
  11310. if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
  11311. data[TG3_EXT_LOOPB_TEST] |=
  11312. TG3_STD_LOOPBACK_FAILED;
  11313. if (tg3_flag(tp, TSO_CAPABLE) &&
  11314. tg3_run_loopback(tp, ETH_FRAME_LEN, true))
  11315. data[TG3_EXT_LOOPB_TEST] |=
  11316. TG3_TSO_LOOPBACK_FAILED;
  11317. if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
  11318. tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
  11319. data[TG3_EXT_LOOPB_TEST] |=
  11320. TG3_JMB_LOOPBACK_FAILED;
  11321. }
  11322. /* Re-enable gphy autopowerdown. */
  11323. if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
  11324. tg3_phy_toggle_apd(tp, true);
  11325. }
  11326. err = (data[TG3_MAC_LOOPB_TEST] | data[TG3_PHY_LOOPB_TEST] |
  11327. data[TG3_EXT_LOOPB_TEST]) ? -EIO : 0;
  11328. done:
  11329. tp->phy_flags |= eee_cap;
  11330. return err;
  11331. }
  11332. static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
  11333. u64 *data)
  11334. {
  11335. struct tg3 *tp = netdev_priv(dev);
  11336. bool doextlpbk = etest->flags & ETH_TEST_FL_EXTERNAL_LB;
  11337. if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
  11338. if (tg3_power_up(tp)) {
  11339. etest->flags |= ETH_TEST_FL_FAILED;
  11340. memset(data, 1, sizeof(u64) * TG3_NUM_TEST);
  11341. return;
  11342. }
  11343. tg3_ape_driver_state_change(tp, RESET_KIND_INIT);
  11344. }
  11345. memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
  11346. if (tg3_test_nvram(tp) != 0) {
  11347. etest->flags |= ETH_TEST_FL_FAILED;
  11348. data[TG3_NVRAM_TEST] = 1;
  11349. }
  11350. if (!doextlpbk && tg3_test_link(tp)) {
  11351. etest->flags |= ETH_TEST_FL_FAILED;
  11352. data[TG3_LINK_TEST] = 1;
  11353. }
  11354. if (etest->flags & ETH_TEST_FL_OFFLINE) {
  11355. int err, err2 = 0, irq_sync = 0;
  11356. if (netif_running(dev)) {
  11357. tg3_phy_stop(tp);
  11358. tg3_netif_stop(tp);
  11359. irq_sync = 1;
  11360. }
  11361. tg3_full_lock(tp, irq_sync);
  11362. tg3_halt(tp, RESET_KIND_SUSPEND, 1);
  11363. err = tg3_nvram_lock(tp);
  11364. tg3_halt_cpu(tp, RX_CPU_BASE);
  11365. if (!tg3_flag(tp, 5705_PLUS))
  11366. tg3_halt_cpu(tp, TX_CPU_BASE);
  11367. if (!err)
  11368. tg3_nvram_unlock(tp);
  11369. if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
  11370. tg3_phy_reset(tp);
  11371. if (tg3_test_registers(tp) != 0) {
  11372. etest->flags |= ETH_TEST_FL_FAILED;
  11373. data[TG3_REGISTER_TEST] = 1;
  11374. }
  11375. if (tg3_test_memory(tp) != 0) {
  11376. etest->flags |= ETH_TEST_FL_FAILED;
  11377. data[TG3_MEMORY_TEST] = 1;
  11378. }
  11379. if (doextlpbk)
  11380. etest->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE;
  11381. if (tg3_test_loopback(tp, data, doextlpbk))
  11382. etest->flags |= ETH_TEST_FL_FAILED;
  11383. tg3_full_unlock(tp);
  11384. if (tg3_test_interrupt(tp) != 0) {
  11385. etest->flags |= ETH_TEST_FL_FAILED;
  11386. data[TG3_INTERRUPT_TEST] = 1;
  11387. }
  11388. tg3_full_lock(tp, 0);
  11389. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  11390. if (netif_running(dev)) {
  11391. tg3_flag_set(tp, INIT_COMPLETE);
  11392. err2 = tg3_restart_hw(tp, true);
  11393. if (!err2)
  11394. tg3_netif_start(tp);
  11395. }
  11396. tg3_full_unlock(tp);
  11397. if (irq_sync && !err2)
  11398. tg3_phy_start(tp);
  11399. }
  11400. if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
  11401. tg3_power_down_prepare(tp);
  11402. }
  11403. static int tg3_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
  11404. {
  11405. struct tg3 *tp = netdev_priv(dev);
  11406. struct hwtstamp_config stmpconf;
  11407. if (!tg3_flag(tp, PTP_CAPABLE))
  11408. return -EOPNOTSUPP;
  11409. if (copy_from_user(&stmpconf, ifr->ifr_data, sizeof(stmpconf)))
  11410. return -EFAULT;
  11411. if (stmpconf.flags)
  11412. return -EINVAL;
  11413. if (stmpconf.tx_type != HWTSTAMP_TX_ON &&
  11414. stmpconf.tx_type != HWTSTAMP_TX_OFF)
  11415. return -ERANGE;
  11416. switch (stmpconf.rx_filter) {
  11417. case HWTSTAMP_FILTER_NONE:
  11418. tp->rxptpctl = 0;
  11419. break;
  11420. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  11421. tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
  11422. TG3_RX_PTP_CTL_ALL_V1_EVENTS;
  11423. break;
  11424. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  11425. tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
  11426. TG3_RX_PTP_CTL_SYNC_EVNT;
  11427. break;
  11428. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  11429. tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
  11430. TG3_RX_PTP_CTL_DELAY_REQ;
  11431. break;
  11432. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  11433. tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
  11434. TG3_RX_PTP_CTL_ALL_V2_EVENTS;
  11435. break;
  11436. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  11437. tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
  11438. TG3_RX_PTP_CTL_ALL_V2_EVENTS;
  11439. break;
  11440. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  11441. tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
  11442. TG3_RX_PTP_CTL_ALL_V2_EVENTS;
  11443. break;
  11444. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  11445. tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
  11446. TG3_RX_PTP_CTL_SYNC_EVNT;
  11447. break;
  11448. case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  11449. tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
  11450. TG3_RX_PTP_CTL_SYNC_EVNT;
  11451. break;
  11452. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  11453. tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
  11454. TG3_RX_PTP_CTL_SYNC_EVNT;
  11455. break;
  11456. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  11457. tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
  11458. TG3_RX_PTP_CTL_DELAY_REQ;
  11459. break;
  11460. case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
  11461. tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
  11462. TG3_RX_PTP_CTL_DELAY_REQ;
  11463. break;
  11464. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  11465. tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
  11466. TG3_RX_PTP_CTL_DELAY_REQ;
  11467. break;
  11468. default:
  11469. return -ERANGE;
  11470. }
  11471. if (netif_running(dev) && tp->rxptpctl)
  11472. tw32(TG3_RX_PTP_CTL,
  11473. tp->rxptpctl | TG3_RX_PTP_CTL_HWTS_INTERLOCK);
  11474. if (stmpconf.tx_type == HWTSTAMP_TX_ON)
  11475. tg3_flag_set(tp, TX_TSTAMP_EN);
  11476. else
  11477. tg3_flag_clear(tp, TX_TSTAMP_EN);
  11478. return copy_to_user(ifr->ifr_data, &stmpconf, sizeof(stmpconf)) ?
  11479. -EFAULT : 0;
  11480. }
  11481. static int tg3_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
  11482. {
  11483. struct tg3 *tp = netdev_priv(dev);
  11484. struct hwtstamp_config stmpconf;
  11485. if (!tg3_flag(tp, PTP_CAPABLE))
  11486. return -EOPNOTSUPP;
  11487. stmpconf.flags = 0;
  11488. stmpconf.tx_type = (tg3_flag(tp, TX_TSTAMP_EN) ?
  11489. HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF);
  11490. switch (tp->rxptpctl) {
  11491. case 0:
  11492. stmpconf.rx_filter = HWTSTAMP_FILTER_NONE;
  11493. break;
  11494. case TG3_RX_PTP_CTL_RX_PTP_V1_EN | TG3_RX_PTP_CTL_ALL_V1_EVENTS:
  11495. stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
  11496. break;
  11497. case TG3_RX_PTP_CTL_RX_PTP_V1_EN | TG3_RX_PTP_CTL_SYNC_EVNT:
  11498. stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_SYNC;
  11499. break;
  11500. case TG3_RX_PTP_CTL_RX_PTP_V1_EN | TG3_RX_PTP_CTL_DELAY_REQ:
  11501. stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ;
  11502. break;
  11503. case TG3_RX_PTP_CTL_RX_PTP_V2_EN | TG3_RX_PTP_CTL_ALL_V2_EVENTS:
  11504. stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
  11505. break;
  11506. case TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN | TG3_RX_PTP_CTL_ALL_V2_EVENTS:
  11507. stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
  11508. break;
  11509. case TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN | TG3_RX_PTP_CTL_ALL_V2_EVENTS:
  11510. stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
  11511. break;
  11512. case TG3_RX_PTP_CTL_RX_PTP_V2_EN | TG3_RX_PTP_CTL_SYNC_EVNT:
  11513. stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_SYNC;
  11514. break;
  11515. case TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN | TG3_RX_PTP_CTL_SYNC_EVNT:
  11516. stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_SYNC;
  11517. break;
  11518. case TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN | TG3_RX_PTP_CTL_SYNC_EVNT:
  11519. stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_SYNC;
  11520. break;
  11521. case TG3_RX_PTP_CTL_RX_PTP_V2_EN | TG3_RX_PTP_CTL_DELAY_REQ:
  11522. stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_DELAY_REQ;
  11523. break;
  11524. case TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN | TG3_RX_PTP_CTL_DELAY_REQ:
  11525. stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ;
  11526. break;
  11527. case TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN | TG3_RX_PTP_CTL_DELAY_REQ:
  11528. stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ;
  11529. break;
  11530. default:
  11531. WARN_ON_ONCE(1);
  11532. return -ERANGE;
  11533. }
  11534. return copy_to_user(ifr->ifr_data, &stmpconf, sizeof(stmpconf)) ?
  11535. -EFAULT : 0;
  11536. }
  11537. static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  11538. {
  11539. struct mii_ioctl_data *data = if_mii(ifr);
  11540. struct tg3 *tp = netdev_priv(dev);
  11541. int err;
  11542. if (tg3_flag(tp, USE_PHYLIB)) {
  11543. struct phy_device *phydev;
  11544. if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
  11545. return -EAGAIN;
  11546. phydev = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr);
  11547. return phy_mii_ioctl(phydev, ifr, cmd);
  11548. }
  11549. switch (cmd) {
  11550. case SIOCGMIIPHY:
  11551. data->phy_id = tp->phy_addr;
  11552. /* fallthru */
  11553. case SIOCGMIIREG: {
  11554. u32 mii_regval;
  11555. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
  11556. break; /* We have no PHY */
  11557. if (!netif_running(dev))
  11558. return -EAGAIN;
  11559. spin_lock_bh(&tp->lock);
  11560. err = __tg3_readphy(tp, data->phy_id & 0x1f,
  11561. data->reg_num & 0x1f, &mii_regval);
  11562. spin_unlock_bh(&tp->lock);
  11563. data->val_out = mii_regval;
  11564. return err;
  11565. }
  11566. case SIOCSMIIREG:
  11567. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
  11568. break; /* We have no PHY */
  11569. if (!netif_running(dev))
  11570. return -EAGAIN;
  11571. spin_lock_bh(&tp->lock);
  11572. err = __tg3_writephy(tp, data->phy_id & 0x1f,
  11573. data->reg_num & 0x1f, data->val_in);
  11574. spin_unlock_bh(&tp->lock);
  11575. return err;
  11576. case SIOCSHWTSTAMP:
  11577. return tg3_hwtstamp_set(dev, ifr);
  11578. case SIOCGHWTSTAMP:
  11579. return tg3_hwtstamp_get(dev, ifr);
  11580. default:
  11581. /* do nothing */
  11582. break;
  11583. }
  11584. return -EOPNOTSUPP;
  11585. }
  11586. static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
  11587. {
  11588. struct tg3 *tp = netdev_priv(dev);
  11589. memcpy(ec, &tp->coal, sizeof(*ec));
  11590. return 0;
  11591. }
  11592. static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
  11593. {
  11594. struct tg3 *tp = netdev_priv(dev);
  11595. u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
  11596. u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
  11597. if (!tg3_flag(tp, 5705_PLUS)) {
  11598. max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
  11599. max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
  11600. max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
  11601. min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
  11602. }
  11603. if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
  11604. (!ec->rx_coalesce_usecs) ||
  11605. (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
  11606. (!ec->tx_coalesce_usecs) ||
  11607. (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
  11608. (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
  11609. (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
  11610. (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
  11611. (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
  11612. (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
  11613. (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
  11614. (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
  11615. return -EINVAL;
  11616. /* Only copy relevant parameters, ignore all others. */
  11617. tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
  11618. tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
  11619. tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
  11620. tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
  11621. tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
  11622. tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
  11623. tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
  11624. tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
  11625. tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
  11626. if (netif_running(dev)) {
  11627. tg3_full_lock(tp, 0);
  11628. __tg3_set_coalesce(tp, &tp->coal);
  11629. tg3_full_unlock(tp);
  11630. }
  11631. return 0;
  11632. }
  11633. static int tg3_set_eee(struct net_device *dev, struct ethtool_eee *edata)
  11634. {
  11635. struct tg3 *tp = netdev_priv(dev);
  11636. if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP)) {
  11637. netdev_warn(tp->dev, "Board does not support EEE!\n");
  11638. return -EOPNOTSUPP;
  11639. }
  11640. if (edata->advertised != tp->eee.advertised) {
  11641. netdev_warn(tp->dev,
  11642. "Direct manipulation of EEE advertisement is not supported\n");
  11643. return -EINVAL;
  11644. }
  11645. if (edata->tx_lpi_timer > TG3_CPMU_DBTMR1_LNKIDLE_MAX) {
  11646. netdev_warn(tp->dev,
  11647. "Maximal Tx Lpi timer supported is %#x(u)\n",
  11648. TG3_CPMU_DBTMR1_LNKIDLE_MAX);
  11649. return -EINVAL;
  11650. }
  11651. tp->eee = *edata;
  11652. tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
  11653. tg3_warn_mgmt_link_flap(tp);
  11654. if (netif_running(tp->dev)) {
  11655. tg3_full_lock(tp, 0);
  11656. tg3_setup_eee(tp);
  11657. tg3_phy_reset(tp);
  11658. tg3_full_unlock(tp);
  11659. }
  11660. return 0;
  11661. }
  11662. static int tg3_get_eee(struct net_device *dev, struct ethtool_eee *edata)
  11663. {
  11664. struct tg3 *tp = netdev_priv(dev);
  11665. if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP)) {
  11666. netdev_warn(tp->dev,
  11667. "Board does not support EEE!\n");
  11668. return -EOPNOTSUPP;
  11669. }
  11670. *edata = tp->eee;
  11671. return 0;
  11672. }
  11673. static const struct ethtool_ops tg3_ethtool_ops = {
  11674. .get_drvinfo = tg3_get_drvinfo,
  11675. .get_regs_len = tg3_get_regs_len,
  11676. .get_regs = tg3_get_regs,
  11677. .get_wol = tg3_get_wol,
  11678. .set_wol = tg3_set_wol,
  11679. .get_msglevel = tg3_get_msglevel,
  11680. .set_msglevel = tg3_set_msglevel,
  11681. .nway_reset = tg3_nway_reset,
  11682. .get_link = ethtool_op_get_link,
  11683. .get_eeprom_len = tg3_get_eeprom_len,
  11684. .get_eeprom = tg3_get_eeprom,
  11685. .set_eeprom = tg3_set_eeprom,
  11686. .get_ringparam = tg3_get_ringparam,
  11687. .set_ringparam = tg3_set_ringparam,
  11688. .get_pauseparam = tg3_get_pauseparam,
  11689. .set_pauseparam = tg3_set_pauseparam,
  11690. .self_test = tg3_self_test,
  11691. .get_strings = tg3_get_strings,
  11692. .set_phys_id = tg3_set_phys_id,
  11693. .get_ethtool_stats = tg3_get_ethtool_stats,
  11694. .get_coalesce = tg3_get_coalesce,
  11695. .set_coalesce = tg3_set_coalesce,
  11696. .get_sset_count = tg3_get_sset_count,
  11697. .get_rxnfc = tg3_get_rxnfc,
  11698. .get_rxfh_indir_size = tg3_get_rxfh_indir_size,
  11699. .get_rxfh = tg3_get_rxfh,
  11700. .set_rxfh = tg3_set_rxfh,
  11701. .get_channels = tg3_get_channels,
  11702. .set_channels = tg3_set_channels,
  11703. .get_ts_info = tg3_get_ts_info,
  11704. .get_eee = tg3_get_eee,
  11705. .set_eee = tg3_set_eee,
  11706. .get_link_ksettings = tg3_get_link_ksettings,
  11707. .set_link_ksettings = tg3_set_link_ksettings,
  11708. };
  11709. static void tg3_get_stats64(struct net_device *dev,
  11710. struct rtnl_link_stats64 *stats)
  11711. {
  11712. struct tg3 *tp = netdev_priv(dev);
  11713. spin_lock_bh(&tp->lock);
  11714. if (!tp->hw_stats || !tg3_flag(tp, INIT_COMPLETE)) {
  11715. *stats = tp->net_stats_prev;
  11716. spin_unlock_bh(&tp->lock);
  11717. return;
  11718. }
  11719. tg3_get_nstats(tp, stats);
  11720. spin_unlock_bh(&tp->lock);
  11721. }
  11722. static void tg3_set_rx_mode(struct net_device *dev)
  11723. {
  11724. struct tg3 *tp = netdev_priv(dev);
  11725. if (!netif_running(dev))
  11726. return;
  11727. tg3_full_lock(tp, 0);
  11728. __tg3_set_rx_mode(dev);
  11729. tg3_full_unlock(tp);
  11730. }
  11731. static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
  11732. int new_mtu)
  11733. {
  11734. dev->mtu = new_mtu;
  11735. if (new_mtu > ETH_DATA_LEN) {
  11736. if (tg3_flag(tp, 5780_CLASS)) {
  11737. netdev_update_features(dev);
  11738. tg3_flag_clear(tp, TSO_CAPABLE);
  11739. } else {
  11740. tg3_flag_set(tp, JUMBO_RING_ENABLE);
  11741. }
  11742. } else {
  11743. if (tg3_flag(tp, 5780_CLASS)) {
  11744. tg3_flag_set(tp, TSO_CAPABLE);
  11745. netdev_update_features(dev);
  11746. }
  11747. tg3_flag_clear(tp, JUMBO_RING_ENABLE);
  11748. }
  11749. }
  11750. static int tg3_change_mtu(struct net_device *dev, int new_mtu)
  11751. {
  11752. struct tg3 *tp = netdev_priv(dev);
  11753. int err;
  11754. bool reset_phy = false;
  11755. if (!netif_running(dev)) {
  11756. /* We'll just catch it later when the
  11757. * device is up'd.
  11758. */
  11759. tg3_set_mtu(dev, tp, new_mtu);
  11760. return 0;
  11761. }
  11762. tg3_phy_stop(tp);
  11763. tg3_netif_stop(tp);
  11764. tg3_set_mtu(dev, tp, new_mtu);
  11765. tg3_full_lock(tp, 1);
  11766. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  11767. /* Reset PHY, otherwise the read DMA engine will be in a mode that
  11768. * breaks all requests to 256 bytes.
  11769. */
  11770. if (tg3_asic_rev(tp) == ASIC_REV_57766 ||
  11771. tg3_asic_rev(tp) == ASIC_REV_5717 ||
  11772. tg3_asic_rev(tp) == ASIC_REV_5719 ||
  11773. tg3_asic_rev(tp) == ASIC_REV_5720)
  11774. reset_phy = true;
  11775. err = tg3_restart_hw(tp, reset_phy);
  11776. if (!err)
  11777. tg3_netif_start(tp);
  11778. tg3_full_unlock(tp);
  11779. if (!err)
  11780. tg3_phy_start(tp);
  11781. return err;
  11782. }
  11783. static const struct net_device_ops tg3_netdev_ops = {
  11784. .ndo_open = tg3_open,
  11785. .ndo_stop = tg3_close,
  11786. .ndo_start_xmit = tg3_start_xmit,
  11787. .ndo_get_stats64 = tg3_get_stats64,
  11788. .ndo_validate_addr = eth_validate_addr,
  11789. .ndo_set_rx_mode = tg3_set_rx_mode,
  11790. .ndo_set_mac_address = tg3_set_mac_addr,
  11791. .ndo_do_ioctl = tg3_ioctl,
  11792. .ndo_tx_timeout = tg3_tx_timeout,
  11793. .ndo_change_mtu = tg3_change_mtu,
  11794. .ndo_fix_features = tg3_fix_features,
  11795. .ndo_set_features = tg3_set_features,
  11796. #ifdef CONFIG_NET_POLL_CONTROLLER
  11797. .ndo_poll_controller = tg3_poll_controller,
  11798. #endif
  11799. };
  11800. static void tg3_get_eeprom_size(struct tg3 *tp)
  11801. {
  11802. u32 cursize, val, magic;
  11803. tp->nvram_size = EEPROM_CHIP_SIZE;
  11804. if (tg3_nvram_read(tp, 0, &magic) != 0)
  11805. return;
  11806. if ((magic != TG3_EEPROM_MAGIC) &&
  11807. ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
  11808. ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
  11809. return;
  11810. /*
  11811. * Size the chip by reading offsets at increasing powers of two.
  11812. * When we encounter our validation signature, we know the addressing
  11813. * has wrapped around, and thus have our chip size.
  11814. */
  11815. cursize = 0x10;
  11816. while (cursize < tp->nvram_size) {
  11817. if (tg3_nvram_read(tp, cursize, &val) != 0)
  11818. return;
  11819. if (val == magic)
  11820. break;
  11821. cursize <<= 1;
  11822. }
  11823. tp->nvram_size = cursize;
  11824. }
  11825. static void tg3_get_nvram_size(struct tg3 *tp)
  11826. {
  11827. u32 val;
  11828. if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &val) != 0)
  11829. return;
  11830. /* Selfboot format */
  11831. if (val != TG3_EEPROM_MAGIC) {
  11832. tg3_get_eeprom_size(tp);
  11833. return;
  11834. }
  11835. if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
  11836. if (val != 0) {
  11837. /* This is confusing. We want to operate on the
  11838. * 16-bit value at offset 0xf2. The tg3_nvram_read()
  11839. * call will read from NVRAM and byteswap the data
  11840. * according to the byteswapping settings for all
  11841. * other register accesses. This ensures the data we
  11842. * want will always reside in the lower 16-bits.
  11843. * However, the data in NVRAM is in LE format, which
  11844. * means the data from the NVRAM read will always be
  11845. * opposite the endianness of the CPU. The 16-bit
  11846. * byteswap then brings the data to CPU endianness.
  11847. */
  11848. tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
  11849. return;
  11850. }
  11851. }
  11852. tp->nvram_size = TG3_NVRAM_SIZE_512KB;
  11853. }
  11854. static void tg3_get_nvram_info(struct tg3 *tp)
  11855. {
  11856. u32 nvcfg1;
  11857. nvcfg1 = tr32(NVRAM_CFG1);
  11858. if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
  11859. tg3_flag_set(tp, FLASH);
  11860. } else {
  11861. nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
  11862. tw32(NVRAM_CFG1, nvcfg1);
  11863. }
  11864. if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
  11865. tg3_flag(tp, 5780_CLASS)) {
  11866. switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
  11867. case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
  11868. tp->nvram_jedecnum = JEDEC_ATMEL;
  11869. tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
  11870. tg3_flag_set(tp, NVRAM_BUFFERED);
  11871. break;
  11872. case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
  11873. tp->nvram_jedecnum = JEDEC_ATMEL;
  11874. tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
  11875. break;
  11876. case FLASH_VENDOR_ATMEL_EEPROM:
  11877. tp->nvram_jedecnum = JEDEC_ATMEL;
  11878. tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
  11879. tg3_flag_set(tp, NVRAM_BUFFERED);
  11880. break;
  11881. case FLASH_VENDOR_ST:
  11882. tp->nvram_jedecnum = JEDEC_ST;
  11883. tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
  11884. tg3_flag_set(tp, NVRAM_BUFFERED);
  11885. break;
  11886. case FLASH_VENDOR_SAIFUN:
  11887. tp->nvram_jedecnum = JEDEC_SAIFUN;
  11888. tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
  11889. break;
  11890. case FLASH_VENDOR_SST_SMALL:
  11891. case FLASH_VENDOR_SST_LARGE:
  11892. tp->nvram_jedecnum = JEDEC_SST;
  11893. tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
  11894. break;
  11895. }
  11896. } else {
  11897. tp->nvram_jedecnum = JEDEC_ATMEL;
  11898. tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
  11899. tg3_flag_set(tp, NVRAM_BUFFERED);
  11900. }
  11901. }
  11902. static void tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
  11903. {
  11904. switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
  11905. case FLASH_5752PAGE_SIZE_256:
  11906. tp->nvram_pagesize = 256;
  11907. break;
  11908. case FLASH_5752PAGE_SIZE_512:
  11909. tp->nvram_pagesize = 512;
  11910. break;
  11911. case FLASH_5752PAGE_SIZE_1K:
  11912. tp->nvram_pagesize = 1024;
  11913. break;
  11914. case FLASH_5752PAGE_SIZE_2K:
  11915. tp->nvram_pagesize = 2048;
  11916. break;
  11917. case FLASH_5752PAGE_SIZE_4K:
  11918. tp->nvram_pagesize = 4096;
  11919. break;
  11920. case FLASH_5752PAGE_SIZE_264:
  11921. tp->nvram_pagesize = 264;
  11922. break;
  11923. case FLASH_5752PAGE_SIZE_528:
  11924. tp->nvram_pagesize = 528;
  11925. break;
  11926. }
  11927. }
  11928. static void tg3_get_5752_nvram_info(struct tg3 *tp)
  11929. {
  11930. u32 nvcfg1;
  11931. nvcfg1 = tr32(NVRAM_CFG1);
  11932. /* NVRAM protection for TPM */
  11933. if (nvcfg1 & (1 << 27))
  11934. tg3_flag_set(tp, PROTECTED_NVRAM);
  11935. switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
  11936. case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
  11937. case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
  11938. tp->nvram_jedecnum = JEDEC_ATMEL;
  11939. tg3_flag_set(tp, NVRAM_BUFFERED);
  11940. break;
  11941. case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
  11942. tp->nvram_jedecnum = JEDEC_ATMEL;
  11943. tg3_flag_set(tp, NVRAM_BUFFERED);
  11944. tg3_flag_set(tp, FLASH);
  11945. break;
  11946. case FLASH_5752VENDOR_ST_M45PE10:
  11947. case FLASH_5752VENDOR_ST_M45PE20:
  11948. case FLASH_5752VENDOR_ST_M45PE40:
  11949. tp->nvram_jedecnum = JEDEC_ST;
  11950. tg3_flag_set(tp, NVRAM_BUFFERED);
  11951. tg3_flag_set(tp, FLASH);
  11952. break;
  11953. }
  11954. if (tg3_flag(tp, FLASH)) {
  11955. tg3_nvram_get_pagesize(tp, nvcfg1);
  11956. } else {
  11957. /* For eeprom, set pagesize to maximum eeprom size */
  11958. tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
  11959. nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
  11960. tw32(NVRAM_CFG1, nvcfg1);
  11961. }
  11962. }
  11963. static void tg3_get_5755_nvram_info(struct tg3 *tp)
  11964. {
  11965. u32 nvcfg1, protect = 0;
  11966. nvcfg1 = tr32(NVRAM_CFG1);
  11967. /* NVRAM protection for TPM */
  11968. if (nvcfg1 & (1 << 27)) {
  11969. tg3_flag_set(tp, PROTECTED_NVRAM);
  11970. protect = 1;
  11971. }
  11972. nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
  11973. switch (nvcfg1) {
  11974. case FLASH_5755VENDOR_ATMEL_FLASH_1:
  11975. case FLASH_5755VENDOR_ATMEL_FLASH_2:
  11976. case FLASH_5755VENDOR_ATMEL_FLASH_3:
  11977. case FLASH_5755VENDOR_ATMEL_FLASH_5:
  11978. tp->nvram_jedecnum = JEDEC_ATMEL;
  11979. tg3_flag_set(tp, NVRAM_BUFFERED);
  11980. tg3_flag_set(tp, FLASH);
  11981. tp->nvram_pagesize = 264;
  11982. if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
  11983. nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
  11984. tp->nvram_size = (protect ? 0x3e200 :
  11985. TG3_NVRAM_SIZE_512KB);
  11986. else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
  11987. tp->nvram_size = (protect ? 0x1f200 :
  11988. TG3_NVRAM_SIZE_256KB);
  11989. else
  11990. tp->nvram_size = (protect ? 0x1f200 :
  11991. TG3_NVRAM_SIZE_128KB);
  11992. break;
  11993. case FLASH_5752VENDOR_ST_M45PE10:
  11994. case FLASH_5752VENDOR_ST_M45PE20:
  11995. case FLASH_5752VENDOR_ST_M45PE40:
  11996. tp->nvram_jedecnum = JEDEC_ST;
  11997. tg3_flag_set(tp, NVRAM_BUFFERED);
  11998. tg3_flag_set(tp, FLASH);
  11999. tp->nvram_pagesize = 256;
  12000. if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
  12001. tp->nvram_size = (protect ?
  12002. TG3_NVRAM_SIZE_64KB :
  12003. TG3_NVRAM_SIZE_128KB);
  12004. else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
  12005. tp->nvram_size = (protect ?
  12006. TG3_NVRAM_SIZE_64KB :
  12007. TG3_NVRAM_SIZE_256KB);
  12008. else
  12009. tp->nvram_size = (protect ?
  12010. TG3_NVRAM_SIZE_128KB :
  12011. TG3_NVRAM_SIZE_512KB);
  12012. break;
  12013. }
  12014. }
  12015. static void tg3_get_5787_nvram_info(struct tg3 *tp)
  12016. {
  12017. u32 nvcfg1;
  12018. nvcfg1 = tr32(NVRAM_CFG1);
  12019. switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
  12020. case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
  12021. case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
  12022. case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
  12023. case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
  12024. tp->nvram_jedecnum = JEDEC_ATMEL;
  12025. tg3_flag_set(tp, NVRAM_BUFFERED);
  12026. tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
  12027. nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
  12028. tw32(NVRAM_CFG1, nvcfg1);
  12029. break;
  12030. case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
  12031. case FLASH_5755VENDOR_ATMEL_FLASH_1:
  12032. case FLASH_5755VENDOR_ATMEL_FLASH_2:
  12033. case FLASH_5755VENDOR_ATMEL_FLASH_3:
  12034. tp->nvram_jedecnum = JEDEC_ATMEL;
  12035. tg3_flag_set(tp, NVRAM_BUFFERED);
  12036. tg3_flag_set(tp, FLASH);
  12037. tp->nvram_pagesize = 264;
  12038. break;
  12039. case FLASH_5752VENDOR_ST_M45PE10:
  12040. case FLASH_5752VENDOR_ST_M45PE20:
  12041. case FLASH_5752VENDOR_ST_M45PE40:
  12042. tp->nvram_jedecnum = JEDEC_ST;
  12043. tg3_flag_set(tp, NVRAM_BUFFERED);
  12044. tg3_flag_set(tp, FLASH);
  12045. tp->nvram_pagesize = 256;
  12046. break;
  12047. }
  12048. }
  12049. static void tg3_get_5761_nvram_info(struct tg3 *tp)
  12050. {
  12051. u32 nvcfg1, protect = 0;
  12052. nvcfg1 = tr32(NVRAM_CFG1);
  12053. /* NVRAM protection for TPM */
  12054. if (nvcfg1 & (1 << 27)) {
  12055. tg3_flag_set(tp, PROTECTED_NVRAM);
  12056. protect = 1;
  12057. }
  12058. nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
  12059. switch (nvcfg1) {
  12060. case FLASH_5761VENDOR_ATMEL_ADB021D:
  12061. case FLASH_5761VENDOR_ATMEL_ADB041D:
  12062. case FLASH_5761VENDOR_ATMEL_ADB081D:
  12063. case FLASH_5761VENDOR_ATMEL_ADB161D:
  12064. case FLASH_5761VENDOR_ATMEL_MDB021D:
  12065. case FLASH_5761VENDOR_ATMEL_MDB041D:
  12066. case FLASH_5761VENDOR_ATMEL_MDB081D:
  12067. case FLASH_5761VENDOR_ATMEL_MDB161D:
  12068. tp->nvram_jedecnum = JEDEC_ATMEL;
  12069. tg3_flag_set(tp, NVRAM_BUFFERED);
  12070. tg3_flag_set(tp, FLASH);
  12071. tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
  12072. tp->nvram_pagesize = 256;
  12073. break;
  12074. case FLASH_5761VENDOR_ST_A_M45PE20:
  12075. case FLASH_5761VENDOR_ST_A_M45PE40:
  12076. case FLASH_5761VENDOR_ST_A_M45PE80:
  12077. case FLASH_5761VENDOR_ST_A_M45PE16:
  12078. case FLASH_5761VENDOR_ST_M_M45PE20:
  12079. case FLASH_5761VENDOR_ST_M_M45PE40:
  12080. case FLASH_5761VENDOR_ST_M_M45PE80:
  12081. case FLASH_5761VENDOR_ST_M_M45PE16:
  12082. tp->nvram_jedecnum = JEDEC_ST;
  12083. tg3_flag_set(tp, NVRAM_BUFFERED);
  12084. tg3_flag_set(tp, FLASH);
  12085. tp->nvram_pagesize = 256;
  12086. break;
  12087. }
  12088. if (protect) {
  12089. tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
  12090. } else {
  12091. switch (nvcfg1) {
  12092. case FLASH_5761VENDOR_ATMEL_ADB161D:
  12093. case FLASH_5761VENDOR_ATMEL_MDB161D:
  12094. case FLASH_5761VENDOR_ST_A_M45PE16:
  12095. case FLASH_5761VENDOR_ST_M_M45PE16:
  12096. tp->nvram_size = TG3_NVRAM_SIZE_2MB;
  12097. break;
  12098. case FLASH_5761VENDOR_ATMEL_ADB081D:
  12099. case FLASH_5761VENDOR_ATMEL_MDB081D:
  12100. case FLASH_5761VENDOR_ST_A_M45PE80:
  12101. case FLASH_5761VENDOR_ST_M_M45PE80:
  12102. tp->nvram_size = TG3_NVRAM_SIZE_1MB;
  12103. break;
  12104. case FLASH_5761VENDOR_ATMEL_ADB041D:
  12105. case FLASH_5761VENDOR_ATMEL_MDB041D:
  12106. case FLASH_5761VENDOR_ST_A_M45PE40:
  12107. case FLASH_5761VENDOR_ST_M_M45PE40:
  12108. tp->nvram_size = TG3_NVRAM_SIZE_512KB;
  12109. break;
  12110. case FLASH_5761VENDOR_ATMEL_ADB021D:
  12111. case FLASH_5761VENDOR_ATMEL_MDB021D:
  12112. case FLASH_5761VENDOR_ST_A_M45PE20:
  12113. case FLASH_5761VENDOR_ST_M_M45PE20:
  12114. tp->nvram_size = TG3_NVRAM_SIZE_256KB;
  12115. break;
  12116. }
  12117. }
  12118. }
  12119. static void tg3_get_5906_nvram_info(struct tg3 *tp)
  12120. {
  12121. tp->nvram_jedecnum = JEDEC_ATMEL;
  12122. tg3_flag_set(tp, NVRAM_BUFFERED);
  12123. tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
  12124. }
  12125. static void tg3_get_57780_nvram_info(struct tg3 *tp)
  12126. {
  12127. u32 nvcfg1;
  12128. nvcfg1 = tr32(NVRAM_CFG1);
  12129. switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
  12130. case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
  12131. case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
  12132. tp->nvram_jedecnum = JEDEC_ATMEL;
  12133. tg3_flag_set(tp, NVRAM_BUFFERED);
  12134. tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
  12135. nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
  12136. tw32(NVRAM_CFG1, nvcfg1);
  12137. return;
  12138. case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
  12139. case FLASH_57780VENDOR_ATMEL_AT45DB011D:
  12140. case FLASH_57780VENDOR_ATMEL_AT45DB011B:
  12141. case FLASH_57780VENDOR_ATMEL_AT45DB021D:
  12142. case FLASH_57780VENDOR_ATMEL_AT45DB021B:
  12143. case FLASH_57780VENDOR_ATMEL_AT45DB041D:
  12144. case FLASH_57780VENDOR_ATMEL_AT45DB041B:
  12145. tp->nvram_jedecnum = JEDEC_ATMEL;
  12146. tg3_flag_set(tp, NVRAM_BUFFERED);
  12147. tg3_flag_set(tp, FLASH);
  12148. switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
  12149. case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
  12150. case FLASH_57780VENDOR_ATMEL_AT45DB011D:
  12151. case FLASH_57780VENDOR_ATMEL_AT45DB011B:
  12152. tp->nvram_size = TG3_NVRAM_SIZE_128KB;
  12153. break;
  12154. case FLASH_57780VENDOR_ATMEL_AT45DB021D:
  12155. case FLASH_57780VENDOR_ATMEL_AT45DB021B:
  12156. tp->nvram_size = TG3_NVRAM_SIZE_256KB;
  12157. break;
  12158. case FLASH_57780VENDOR_ATMEL_AT45DB041D:
  12159. case FLASH_57780VENDOR_ATMEL_AT45DB041B:
  12160. tp->nvram_size = TG3_NVRAM_SIZE_512KB;
  12161. break;
  12162. }
  12163. break;
  12164. case FLASH_5752VENDOR_ST_M45PE10:
  12165. case FLASH_5752VENDOR_ST_M45PE20:
  12166. case FLASH_5752VENDOR_ST_M45PE40:
  12167. tp->nvram_jedecnum = JEDEC_ST;
  12168. tg3_flag_set(tp, NVRAM_BUFFERED);
  12169. tg3_flag_set(tp, FLASH);
  12170. switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
  12171. case FLASH_5752VENDOR_ST_M45PE10:
  12172. tp->nvram_size = TG3_NVRAM_SIZE_128KB;
  12173. break;
  12174. case FLASH_5752VENDOR_ST_M45PE20:
  12175. tp->nvram_size = TG3_NVRAM_SIZE_256KB;
  12176. break;
  12177. case FLASH_5752VENDOR_ST_M45PE40:
  12178. tp->nvram_size = TG3_NVRAM_SIZE_512KB;
  12179. break;
  12180. }
  12181. break;
  12182. default:
  12183. tg3_flag_set(tp, NO_NVRAM);
  12184. return;
  12185. }
  12186. tg3_nvram_get_pagesize(tp, nvcfg1);
  12187. if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
  12188. tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
  12189. }
  12190. static void tg3_get_5717_nvram_info(struct tg3 *tp)
  12191. {
  12192. u32 nvcfg1;
  12193. nvcfg1 = tr32(NVRAM_CFG1);
  12194. switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
  12195. case FLASH_5717VENDOR_ATMEL_EEPROM:
  12196. case FLASH_5717VENDOR_MICRO_EEPROM:
  12197. tp->nvram_jedecnum = JEDEC_ATMEL;
  12198. tg3_flag_set(tp, NVRAM_BUFFERED);
  12199. tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
  12200. nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
  12201. tw32(NVRAM_CFG1, nvcfg1);
  12202. return;
  12203. case FLASH_5717VENDOR_ATMEL_MDB011D:
  12204. case FLASH_5717VENDOR_ATMEL_ADB011B:
  12205. case FLASH_5717VENDOR_ATMEL_ADB011D:
  12206. case FLASH_5717VENDOR_ATMEL_MDB021D:
  12207. case FLASH_5717VENDOR_ATMEL_ADB021B:
  12208. case FLASH_5717VENDOR_ATMEL_ADB021D:
  12209. case FLASH_5717VENDOR_ATMEL_45USPT:
  12210. tp->nvram_jedecnum = JEDEC_ATMEL;
  12211. tg3_flag_set(tp, NVRAM_BUFFERED);
  12212. tg3_flag_set(tp, FLASH);
  12213. switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
  12214. case FLASH_5717VENDOR_ATMEL_MDB021D:
  12215. /* Detect size with tg3_nvram_get_size() */
  12216. break;
  12217. case FLASH_5717VENDOR_ATMEL_ADB021B:
  12218. case FLASH_5717VENDOR_ATMEL_ADB021D:
  12219. tp->nvram_size = TG3_NVRAM_SIZE_256KB;
  12220. break;
  12221. default:
  12222. tp->nvram_size = TG3_NVRAM_SIZE_128KB;
  12223. break;
  12224. }
  12225. break;
  12226. case FLASH_5717VENDOR_ST_M_M25PE10:
  12227. case FLASH_5717VENDOR_ST_A_M25PE10:
  12228. case FLASH_5717VENDOR_ST_M_M45PE10:
  12229. case FLASH_5717VENDOR_ST_A_M45PE10:
  12230. case FLASH_5717VENDOR_ST_M_M25PE20:
  12231. case FLASH_5717VENDOR_ST_A_M25PE20:
  12232. case FLASH_5717VENDOR_ST_M_M45PE20:
  12233. case FLASH_5717VENDOR_ST_A_M45PE20:
  12234. case FLASH_5717VENDOR_ST_25USPT:
  12235. case FLASH_5717VENDOR_ST_45USPT:
  12236. tp->nvram_jedecnum = JEDEC_ST;
  12237. tg3_flag_set(tp, NVRAM_BUFFERED);
  12238. tg3_flag_set(tp, FLASH);
  12239. switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
  12240. case FLASH_5717VENDOR_ST_M_M25PE20:
  12241. case FLASH_5717VENDOR_ST_M_M45PE20:
  12242. /* Detect size with tg3_nvram_get_size() */
  12243. break;
  12244. case FLASH_5717VENDOR_ST_A_M25PE20:
  12245. case FLASH_5717VENDOR_ST_A_M45PE20:
  12246. tp->nvram_size = TG3_NVRAM_SIZE_256KB;
  12247. break;
  12248. default:
  12249. tp->nvram_size = TG3_NVRAM_SIZE_128KB;
  12250. break;
  12251. }
  12252. break;
  12253. default:
  12254. tg3_flag_set(tp, NO_NVRAM);
  12255. return;
  12256. }
  12257. tg3_nvram_get_pagesize(tp, nvcfg1);
  12258. if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
  12259. tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
  12260. }
  12261. static void tg3_get_5720_nvram_info(struct tg3 *tp)
  12262. {
  12263. u32 nvcfg1, nvmpinstrp, nv_status;
  12264. nvcfg1 = tr32(NVRAM_CFG1);
  12265. nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
  12266. if (tg3_asic_rev(tp) == ASIC_REV_5762) {
  12267. if (!(nvcfg1 & NVRAM_CFG1_5762VENDOR_MASK)) {
  12268. tg3_flag_set(tp, NO_NVRAM);
  12269. return;
  12270. }
  12271. switch (nvmpinstrp) {
  12272. case FLASH_5762_MX25L_100:
  12273. case FLASH_5762_MX25L_200:
  12274. case FLASH_5762_MX25L_400:
  12275. case FLASH_5762_MX25L_800:
  12276. case FLASH_5762_MX25L_160_320:
  12277. tp->nvram_pagesize = 4096;
  12278. tp->nvram_jedecnum = JEDEC_MACRONIX;
  12279. tg3_flag_set(tp, NVRAM_BUFFERED);
  12280. tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
  12281. tg3_flag_set(tp, FLASH);
  12282. nv_status = tr32(NVRAM_AUTOSENSE_STATUS);
  12283. tp->nvram_size =
  12284. (1 << (nv_status >> AUTOSENSE_DEVID &
  12285. AUTOSENSE_DEVID_MASK)
  12286. << AUTOSENSE_SIZE_IN_MB);
  12287. return;
  12288. case FLASH_5762_EEPROM_HD:
  12289. nvmpinstrp = FLASH_5720_EEPROM_HD;
  12290. break;
  12291. case FLASH_5762_EEPROM_LD:
  12292. nvmpinstrp = FLASH_5720_EEPROM_LD;
  12293. break;
  12294. case FLASH_5720VENDOR_M_ST_M45PE20:
  12295. /* This pinstrap supports multiple sizes, so force it
  12296. * to read the actual size from location 0xf0.
  12297. */
  12298. nvmpinstrp = FLASH_5720VENDOR_ST_45USPT;
  12299. break;
  12300. }
  12301. }
  12302. switch (nvmpinstrp) {
  12303. case FLASH_5720_EEPROM_HD:
  12304. case FLASH_5720_EEPROM_LD:
  12305. tp->nvram_jedecnum = JEDEC_ATMEL;
  12306. tg3_flag_set(tp, NVRAM_BUFFERED);
  12307. nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
  12308. tw32(NVRAM_CFG1, nvcfg1);
  12309. if (nvmpinstrp == FLASH_5720_EEPROM_HD)
  12310. tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
  12311. else
  12312. tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
  12313. return;
  12314. case FLASH_5720VENDOR_M_ATMEL_DB011D:
  12315. case FLASH_5720VENDOR_A_ATMEL_DB011B:
  12316. case FLASH_5720VENDOR_A_ATMEL_DB011D:
  12317. case FLASH_5720VENDOR_M_ATMEL_DB021D:
  12318. case FLASH_5720VENDOR_A_ATMEL_DB021B:
  12319. case FLASH_5720VENDOR_A_ATMEL_DB021D:
  12320. case FLASH_5720VENDOR_M_ATMEL_DB041D:
  12321. case FLASH_5720VENDOR_A_ATMEL_DB041B:
  12322. case FLASH_5720VENDOR_A_ATMEL_DB041D:
  12323. case FLASH_5720VENDOR_M_ATMEL_DB081D:
  12324. case FLASH_5720VENDOR_A_ATMEL_DB081D:
  12325. case FLASH_5720VENDOR_ATMEL_45USPT:
  12326. tp->nvram_jedecnum = JEDEC_ATMEL;
  12327. tg3_flag_set(tp, NVRAM_BUFFERED);
  12328. tg3_flag_set(tp, FLASH);
  12329. switch (nvmpinstrp) {
  12330. case FLASH_5720VENDOR_M_ATMEL_DB021D:
  12331. case FLASH_5720VENDOR_A_ATMEL_DB021B:
  12332. case FLASH_5720VENDOR_A_ATMEL_DB021D:
  12333. tp->nvram_size = TG3_NVRAM_SIZE_256KB;
  12334. break;
  12335. case FLASH_5720VENDOR_M_ATMEL_DB041D:
  12336. case FLASH_5720VENDOR_A_ATMEL_DB041B:
  12337. case FLASH_5720VENDOR_A_ATMEL_DB041D:
  12338. tp->nvram_size = TG3_NVRAM_SIZE_512KB;
  12339. break;
  12340. case FLASH_5720VENDOR_M_ATMEL_DB081D:
  12341. case FLASH_5720VENDOR_A_ATMEL_DB081D:
  12342. tp->nvram_size = TG3_NVRAM_SIZE_1MB;
  12343. break;
  12344. default:
  12345. if (tg3_asic_rev(tp) != ASIC_REV_5762)
  12346. tp->nvram_size = TG3_NVRAM_SIZE_128KB;
  12347. break;
  12348. }
  12349. break;
  12350. case FLASH_5720VENDOR_M_ST_M25PE10:
  12351. case FLASH_5720VENDOR_M_ST_M45PE10:
  12352. case FLASH_5720VENDOR_A_ST_M25PE10:
  12353. case FLASH_5720VENDOR_A_ST_M45PE10:
  12354. case FLASH_5720VENDOR_M_ST_M25PE20:
  12355. case FLASH_5720VENDOR_M_ST_M45PE20:
  12356. case FLASH_5720VENDOR_A_ST_M25PE20:
  12357. case FLASH_5720VENDOR_A_ST_M45PE20:
  12358. case FLASH_5720VENDOR_M_ST_M25PE40:
  12359. case FLASH_5720VENDOR_M_ST_M45PE40:
  12360. case FLASH_5720VENDOR_A_ST_M25PE40:
  12361. case FLASH_5720VENDOR_A_ST_M45PE40:
  12362. case FLASH_5720VENDOR_M_ST_M25PE80:
  12363. case FLASH_5720VENDOR_M_ST_M45PE80:
  12364. case FLASH_5720VENDOR_A_ST_M25PE80:
  12365. case FLASH_5720VENDOR_A_ST_M45PE80:
  12366. case FLASH_5720VENDOR_ST_25USPT:
  12367. case FLASH_5720VENDOR_ST_45USPT:
  12368. tp->nvram_jedecnum = JEDEC_ST;
  12369. tg3_flag_set(tp, NVRAM_BUFFERED);
  12370. tg3_flag_set(tp, FLASH);
  12371. switch (nvmpinstrp) {
  12372. case FLASH_5720VENDOR_M_ST_M25PE20:
  12373. case FLASH_5720VENDOR_M_ST_M45PE20:
  12374. case FLASH_5720VENDOR_A_ST_M25PE20:
  12375. case FLASH_5720VENDOR_A_ST_M45PE20:
  12376. tp->nvram_size = TG3_NVRAM_SIZE_256KB;
  12377. break;
  12378. case FLASH_5720VENDOR_M_ST_M25PE40:
  12379. case FLASH_5720VENDOR_M_ST_M45PE40:
  12380. case FLASH_5720VENDOR_A_ST_M25PE40:
  12381. case FLASH_5720VENDOR_A_ST_M45PE40:
  12382. tp->nvram_size = TG3_NVRAM_SIZE_512KB;
  12383. break;
  12384. case FLASH_5720VENDOR_M_ST_M25PE80:
  12385. case FLASH_5720VENDOR_M_ST_M45PE80:
  12386. case FLASH_5720VENDOR_A_ST_M25PE80:
  12387. case FLASH_5720VENDOR_A_ST_M45PE80:
  12388. tp->nvram_size = TG3_NVRAM_SIZE_1MB;
  12389. break;
  12390. default:
  12391. if (tg3_asic_rev(tp) != ASIC_REV_5762)
  12392. tp->nvram_size = TG3_NVRAM_SIZE_128KB;
  12393. break;
  12394. }
  12395. break;
  12396. default:
  12397. tg3_flag_set(tp, NO_NVRAM);
  12398. return;
  12399. }
  12400. tg3_nvram_get_pagesize(tp, nvcfg1);
  12401. if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
  12402. tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
  12403. if (tg3_asic_rev(tp) == ASIC_REV_5762) {
  12404. u32 val;
  12405. if (tg3_nvram_read(tp, 0, &val))
  12406. return;
  12407. if (val != TG3_EEPROM_MAGIC &&
  12408. (val & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW)
  12409. tg3_flag_set(tp, NO_NVRAM);
  12410. }
  12411. }
  12412. /* Chips other than 5700/5701 use the NVRAM for fetching info. */
  12413. static void tg3_nvram_init(struct tg3 *tp)
  12414. {
  12415. if (tg3_flag(tp, IS_SSB_CORE)) {
  12416. /* No NVRAM and EEPROM on the SSB Broadcom GigE core. */
  12417. tg3_flag_clear(tp, NVRAM);
  12418. tg3_flag_clear(tp, NVRAM_BUFFERED);
  12419. tg3_flag_set(tp, NO_NVRAM);
  12420. return;
  12421. }
  12422. tw32_f(GRC_EEPROM_ADDR,
  12423. (EEPROM_ADDR_FSM_RESET |
  12424. (EEPROM_DEFAULT_CLOCK_PERIOD <<
  12425. EEPROM_ADDR_CLKPERD_SHIFT)));
  12426. msleep(1);
  12427. /* Enable seeprom accesses. */
  12428. tw32_f(GRC_LOCAL_CTRL,
  12429. tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
  12430. udelay(100);
  12431. if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
  12432. tg3_asic_rev(tp) != ASIC_REV_5701) {
  12433. tg3_flag_set(tp, NVRAM);
  12434. if (tg3_nvram_lock(tp)) {
  12435. netdev_warn(tp->dev,
  12436. "Cannot get nvram lock, %s failed\n",
  12437. __func__);
  12438. return;
  12439. }
  12440. tg3_enable_nvram_access(tp);
  12441. tp->nvram_size = 0;
  12442. if (tg3_asic_rev(tp) == ASIC_REV_5752)
  12443. tg3_get_5752_nvram_info(tp);
  12444. else if (tg3_asic_rev(tp) == ASIC_REV_5755)
  12445. tg3_get_5755_nvram_info(tp);
  12446. else if (tg3_asic_rev(tp) == ASIC_REV_5787 ||
  12447. tg3_asic_rev(tp) == ASIC_REV_5784 ||
  12448. tg3_asic_rev(tp) == ASIC_REV_5785)
  12449. tg3_get_5787_nvram_info(tp);
  12450. else if (tg3_asic_rev(tp) == ASIC_REV_5761)
  12451. tg3_get_5761_nvram_info(tp);
  12452. else if (tg3_asic_rev(tp) == ASIC_REV_5906)
  12453. tg3_get_5906_nvram_info(tp);
  12454. else if (tg3_asic_rev(tp) == ASIC_REV_57780 ||
  12455. tg3_flag(tp, 57765_CLASS))
  12456. tg3_get_57780_nvram_info(tp);
  12457. else if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
  12458. tg3_asic_rev(tp) == ASIC_REV_5719)
  12459. tg3_get_5717_nvram_info(tp);
  12460. else if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
  12461. tg3_asic_rev(tp) == ASIC_REV_5762)
  12462. tg3_get_5720_nvram_info(tp);
  12463. else
  12464. tg3_get_nvram_info(tp);
  12465. if (tp->nvram_size == 0)
  12466. tg3_get_nvram_size(tp);
  12467. tg3_disable_nvram_access(tp);
  12468. tg3_nvram_unlock(tp);
  12469. } else {
  12470. tg3_flag_clear(tp, NVRAM);
  12471. tg3_flag_clear(tp, NVRAM_BUFFERED);
  12472. tg3_get_eeprom_size(tp);
  12473. }
  12474. }
  12475. struct subsys_tbl_ent {
  12476. u16 subsys_vendor, subsys_devid;
  12477. u32 phy_id;
  12478. };
  12479. static struct subsys_tbl_ent subsys_id_to_phy_id[] = {
  12480. /* Broadcom boards. */
  12481. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  12482. TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
  12483. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  12484. TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
  12485. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  12486. TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
  12487. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  12488. TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
  12489. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  12490. TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
  12491. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  12492. TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
  12493. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  12494. TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
  12495. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  12496. TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
  12497. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  12498. TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
  12499. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  12500. TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
  12501. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  12502. TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
  12503. /* 3com boards. */
  12504. { TG3PCI_SUBVENDOR_ID_3COM,
  12505. TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
  12506. { TG3PCI_SUBVENDOR_ID_3COM,
  12507. TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
  12508. { TG3PCI_SUBVENDOR_ID_3COM,
  12509. TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
  12510. { TG3PCI_SUBVENDOR_ID_3COM,
  12511. TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
  12512. { TG3PCI_SUBVENDOR_ID_3COM,
  12513. TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
  12514. /* DELL boards. */
  12515. { TG3PCI_SUBVENDOR_ID_DELL,
  12516. TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
  12517. { TG3PCI_SUBVENDOR_ID_DELL,
  12518. TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
  12519. { TG3PCI_SUBVENDOR_ID_DELL,
  12520. TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
  12521. { TG3PCI_SUBVENDOR_ID_DELL,
  12522. TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
  12523. /* Compaq boards. */
  12524. { TG3PCI_SUBVENDOR_ID_COMPAQ,
  12525. TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
  12526. { TG3PCI_SUBVENDOR_ID_COMPAQ,
  12527. TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
  12528. { TG3PCI_SUBVENDOR_ID_COMPAQ,
  12529. TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
  12530. { TG3PCI_SUBVENDOR_ID_COMPAQ,
  12531. TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
  12532. { TG3PCI_SUBVENDOR_ID_COMPAQ,
  12533. TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
  12534. /* IBM boards. */
  12535. { TG3PCI_SUBVENDOR_ID_IBM,
  12536. TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
  12537. };
  12538. static struct subsys_tbl_ent *tg3_lookup_by_subsys(struct tg3 *tp)
  12539. {
  12540. int i;
  12541. for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
  12542. if ((subsys_id_to_phy_id[i].subsys_vendor ==
  12543. tp->pdev->subsystem_vendor) &&
  12544. (subsys_id_to_phy_id[i].subsys_devid ==
  12545. tp->pdev->subsystem_device))
  12546. return &subsys_id_to_phy_id[i];
  12547. }
  12548. return NULL;
  12549. }
  12550. static void tg3_get_eeprom_hw_cfg(struct tg3 *tp)
  12551. {
  12552. u32 val;
  12553. tp->phy_id = TG3_PHY_ID_INVALID;
  12554. tp->led_ctrl = LED_CTRL_MODE_PHY_1;
  12555. /* Assume an onboard device and WOL capable by default. */
  12556. tg3_flag_set(tp, EEPROM_WRITE_PROT);
  12557. tg3_flag_set(tp, WOL_CAP);
  12558. if (tg3_asic_rev(tp) == ASIC_REV_5906) {
  12559. if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
  12560. tg3_flag_clear(tp, EEPROM_WRITE_PROT);
  12561. tg3_flag_set(tp, IS_NIC);
  12562. }
  12563. val = tr32(VCPU_CFGSHDW);
  12564. if (val & VCPU_CFGSHDW_ASPM_DBNC)
  12565. tg3_flag_set(tp, ASPM_WORKAROUND);
  12566. if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
  12567. (val & VCPU_CFGSHDW_WOL_MAGPKT)) {
  12568. tg3_flag_set(tp, WOL_ENABLE);
  12569. device_set_wakeup_enable(&tp->pdev->dev, true);
  12570. }
  12571. goto done;
  12572. }
  12573. tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
  12574. if (val == NIC_SRAM_DATA_SIG_MAGIC) {
  12575. u32 nic_cfg, led_cfg;
  12576. u32 cfg2 = 0, cfg4 = 0, cfg5 = 0;
  12577. u32 nic_phy_id, ver, eeprom_phy_id;
  12578. int eeprom_phy_serdes = 0;
  12579. tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
  12580. tp->nic_sram_data_cfg = nic_cfg;
  12581. tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
  12582. ver >>= NIC_SRAM_DATA_VER_SHIFT;
  12583. if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
  12584. tg3_asic_rev(tp) != ASIC_REV_5701 &&
  12585. tg3_asic_rev(tp) != ASIC_REV_5703 &&
  12586. (ver > 0) && (ver < 0x100))
  12587. tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
  12588. if (tg3_asic_rev(tp) == ASIC_REV_5785)
  12589. tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
  12590. if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
  12591. tg3_asic_rev(tp) == ASIC_REV_5719 ||
  12592. tg3_asic_rev(tp) == ASIC_REV_5720)
  12593. tg3_read_mem(tp, NIC_SRAM_DATA_CFG_5, &cfg5);
  12594. if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
  12595. NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
  12596. eeprom_phy_serdes = 1;
  12597. tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
  12598. if (nic_phy_id != 0) {
  12599. u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
  12600. u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
  12601. eeprom_phy_id = (id1 >> 16) << 10;
  12602. eeprom_phy_id |= (id2 & 0xfc00) << 16;
  12603. eeprom_phy_id |= (id2 & 0x03ff) << 0;
  12604. } else
  12605. eeprom_phy_id = 0;
  12606. tp->phy_id = eeprom_phy_id;
  12607. if (eeprom_phy_serdes) {
  12608. if (!tg3_flag(tp, 5705_PLUS))
  12609. tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
  12610. else
  12611. tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
  12612. }
  12613. if (tg3_flag(tp, 5750_PLUS))
  12614. led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
  12615. SHASTA_EXT_LED_MODE_MASK);
  12616. else
  12617. led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
  12618. switch (led_cfg) {
  12619. default:
  12620. case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
  12621. tp->led_ctrl = LED_CTRL_MODE_PHY_1;
  12622. break;
  12623. case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
  12624. tp->led_ctrl = LED_CTRL_MODE_PHY_2;
  12625. break;
  12626. case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
  12627. tp->led_ctrl = LED_CTRL_MODE_MAC;
  12628. /* Default to PHY_1_MODE if 0 (MAC_MODE) is
  12629. * read on some older 5700/5701 bootcode.
  12630. */
  12631. if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
  12632. tg3_asic_rev(tp) == ASIC_REV_5701)
  12633. tp->led_ctrl = LED_CTRL_MODE_PHY_1;
  12634. break;
  12635. case SHASTA_EXT_LED_SHARED:
  12636. tp->led_ctrl = LED_CTRL_MODE_SHARED;
  12637. if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0 &&
  12638. tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A1)
  12639. tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
  12640. LED_CTRL_MODE_PHY_2);
  12641. if (tg3_flag(tp, 5717_PLUS) ||
  12642. tg3_asic_rev(tp) == ASIC_REV_5762)
  12643. tp->led_ctrl |= LED_CTRL_BLINK_RATE_OVERRIDE |
  12644. LED_CTRL_BLINK_RATE_MASK;
  12645. break;
  12646. case SHASTA_EXT_LED_MAC:
  12647. tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
  12648. break;
  12649. case SHASTA_EXT_LED_COMBO:
  12650. tp->led_ctrl = LED_CTRL_MODE_COMBO;
  12651. if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0)
  12652. tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
  12653. LED_CTRL_MODE_PHY_2);
  12654. break;
  12655. }
  12656. if ((tg3_asic_rev(tp) == ASIC_REV_5700 ||
  12657. tg3_asic_rev(tp) == ASIC_REV_5701) &&
  12658. tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
  12659. tp->led_ctrl = LED_CTRL_MODE_PHY_2;
  12660. if (tg3_chip_rev(tp) == CHIPREV_5784_AX)
  12661. tp->led_ctrl = LED_CTRL_MODE_PHY_1;
  12662. if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
  12663. tg3_flag_set(tp, EEPROM_WRITE_PROT);
  12664. if ((tp->pdev->subsystem_vendor ==
  12665. PCI_VENDOR_ID_ARIMA) &&
  12666. (tp->pdev->subsystem_device == 0x205a ||
  12667. tp->pdev->subsystem_device == 0x2063))
  12668. tg3_flag_clear(tp, EEPROM_WRITE_PROT);
  12669. } else {
  12670. tg3_flag_clear(tp, EEPROM_WRITE_PROT);
  12671. tg3_flag_set(tp, IS_NIC);
  12672. }
  12673. if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
  12674. tg3_flag_set(tp, ENABLE_ASF);
  12675. if (tg3_flag(tp, 5750_PLUS))
  12676. tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
  12677. }
  12678. if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
  12679. tg3_flag(tp, 5750_PLUS))
  12680. tg3_flag_set(tp, ENABLE_APE);
  12681. if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
  12682. !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
  12683. tg3_flag_clear(tp, WOL_CAP);
  12684. if (tg3_flag(tp, WOL_CAP) &&
  12685. (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE)) {
  12686. tg3_flag_set(tp, WOL_ENABLE);
  12687. device_set_wakeup_enable(&tp->pdev->dev, true);
  12688. }
  12689. if (cfg2 & (1 << 17))
  12690. tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
  12691. /* serdes signal pre-emphasis in register 0x590 set by */
  12692. /* bootcode if bit 18 is set */
  12693. if (cfg2 & (1 << 18))
  12694. tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
  12695. if ((tg3_flag(tp, 57765_PLUS) ||
  12696. (tg3_asic_rev(tp) == ASIC_REV_5784 &&
  12697. tg3_chip_rev(tp) != CHIPREV_5784_AX)) &&
  12698. (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
  12699. tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
  12700. if (tg3_flag(tp, PCI_EXPRESS)) {
  12701. u32 cfg3;
  12702. tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
  12703. if (tg3_asic_rev(tp) != ASIC_REV_5785 &&
  12704. !tg3_flag(tp, 57765_PLUS) &&
  12705. (cfg3 & NIC_SRAM_ASPM_DEBOUNCE))
  12706. tg3_flag_set(tp, ASPM_WORKAROUND);
  12707. if (cfg3 & NIC_SRAM_LNK_FLAP_AVOID)
  12708. tp->phy_flags |= TG3_PHYFLG_KEEP_LINK_ON_PWRDN;
  12709. if (cfg3 & NIC_SRAM_1G_ON_VAUX_OK)
  12710. tp->phy_flags |= TG3_PHYFLG_1G_ON_VAUX_OK;
  12711. }
  12712. if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
  12713. tg3_flag_set(tp, RGMII_INBAND_DISABLE);
  12714. if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
  12715. tg3_flag_set(tp, RGMII_EXT_IBND_RX_EN);
  12716. if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
  12717. tg3_flag_set(tp, RGMII_EXT_IBND_TX_EN);
  12718. if (cfg5 & NIC_SRAM_DISABLE_1G_HALF_ADV)
  12719. tp->phy_flags |= TG3_PHYFLG_DISABLE_1G_HD_ADV;
  12720. }
  12721. done:
  12722. if (tg3_flag(tp, WOL_CAP))
  12723. device_set_wakeup_enable(&tp->pdev->dev,
  12724. tg3_flag(tp, WOL_ENABLE));
  12725. else
  12726. device_set_wakeup_capable(&tp->pdev->dev, false);
  12727. }
  12728. static int tg3_ape_otp_read(struct tg3 *tp, u32 offset, u32 *val)
  12729. {
  12730. int i, err;
  12731. u32 val2, off = offset * 8;
  12732. err = tg3_nvram_lock(tp);
  12733. if (err)
  12734. return err;
  12735. tg3_ape_write32(tp, TG3_APE_OTP_ADDR, off | APE_OTP_ADDR_CPU_ENABLE);
  12736. tg3_ape_write32(tp, TG3_APE_OTP_CTRL, APE_OTP_CTRL_PROG_EN |
  12737. APE_OTP_CTRL_CMD_RD | APE_OTP_CTRL_START);
  12738. tg3_ape_read32(tp, TG3_APE_OTP_CTRL);
  12739. udelay(10);
  12740. for (i = 0; i < 100; i++) {
  12741. val2 = tg3_ape_read32(tp, TG3_APE_OTP_STATUS);
  12742. if (val2 & APE_OTP_STATUS_CMD_DONE) {
  12743. *val = tg3_ape_read32(tp, TG3_APE_OTP_RD_DATA);
  12744. break;
  12745. }
  12746. udelay(10);
  12747. }
  12748. tg3_ape_write32(tp, TG3_APE_OTP_CTRL, 0);
  12749. tg3_nvram_unlock(tp);
  12750. if (val2 & APE_OTP_STATUS_CMD_DONE)
  12751. return 0;
  12752. return -EBUSY;
  12753. }
  12754. static int tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
  12755. {
  12756. int i;
  12757. u32 val;
  12758. tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
  12759. tw32(OTP_CTRL, cmd);
  12760. /* Wait for up to 1 ms for command to execute. */
  12761. for (i = 0; i < 100; i++) {
  12762. val = tr32(OTP_STATUS);
  12763. if (val & OTP_STATUS_CMD_DONE)
  12764. break;
  12765. udelay(10);
  12766. }
  12767. return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
  12768. }
  12769. /* Read the gphy configuration from the OTP region of the chip. The gphy
  12770. * configuration is a 32-bit value that straddles the alignment boundary.
  12771. * We do two 32-bit reads and then shift and merge the results.
  12772. */
  12773. static u32 tg3_read_otp_phycfg(struct tg3 *tp)
  12774. {
  12775. u32 bhalf_otp, thalf_otp;
  12776. tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
  12777. if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
  12778. return 0;
  12779. tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
  12780. if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
  12781. return 0;
  12782. thalf_otp = tr32(OTP_READ_DATA);
  12783. tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
  12784. if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
  12785. return 0;
  12786. bhalf_otp = tr32(OTP_READ_DATA);
  12787. return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
  12788. }
  12789. static void tg3_phy_init_link_config(struct tg3 *tp)
  12790. {
  12791. u32 adv = ADVERTISED_Autoneg;
  12792. if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
  12793. if (!(tp->phy_flags & TG3_PHYFLG_DISABLE_1G_HD_ADV))
  12794. adv |= ADVERTISED_1000baseT_Half;
  12795. adv |= ADVERTISED_1000baseT_Full;
  12796. }
  12797. if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
  12798. adv |= ADVERTISED_100baseT_Half |
  12799. ADVERTISED_100baseT_Full |
  12800. ADVERTISED_10baseT_Half |
  12801. ADVERTISED_10baseT_Full |
  12802. ADVERTISED_TP;
  12803. else
  12804. adv |= ADVERTISED_FIBRE;
  12805. tp->link_config.advertising = adv;
  12806. tp->link_config.speed = SPEED_UNKNOWN;
  12807. tp->link_config.duplex = DUPLEX_UNKNOWN;
  12808. tp->link_config.autoneg = AUTONEG_ENABLE;
  12809. tp->link_config.active_speed = SPEED_UNKNOWN;
  12810. tp->link_config.active_duplex = DUPLEX_UNKNOWN;
  12811. tp->old_link = -1;
  12812. }
  12813. static int tg3_phy_probe(struct tg3 *tp)
  12814. {
  12815. u32 hw_phy_id_1, hw_phy_id_2;
  12816. u32 hw_phy_id, hw_phy_id_masked;
  12817. int err;
  12818. /* flow control autonegotiation is default behavior */
  12819. tg3_flag_set(tp, PAUSE_AUTONEG);
  12820. tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
  12821. if (tg3_flag(tp, ENABLE_APE)) {
  12822. switch (tp->pci_fn) {
  12823. case 0:
  12824. tp->phy_ape_lock = TG3_APE_LOCK_PHY0;
  12825. break;
  12826. case 1:
  12827. tp->phy_ape_lock = TG3_APE_LOCK_PHY1;
  12828. break;
  12829. case 2:
  12830. tp->phy_ape_lock = TG3_APE_LOCK_PHY2;
  12831. break;
  12832. case 3:
  12833. tp->phy_ape_lock = TG3_APE_LOCK_PHY3;
  12834. break;
  12835. }
  12836. }
  12837. if (!tg3_flag(tp, ENABLE_ASF) &&
  12838. !(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
  12839. !(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
  12840. tp->phy_flags &= ~(TG3_PHYFLG_1G_ON_VAUX_OK |
  12841. TG3_PHYFLG_KEEP_LINK_ON_PWRDN);
  12842. if (tg3_flag(tp, USE_PHYLIB))
  12843. return tg3_phy_init(tp);
  12844. /* Reading the PHY ID register can conflict with ASF
  12845. * firmware access to the PHY hardware.
  12846. */
  12847. err = 0;
  12848. if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)) {
  12849. hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
  12850. } else {
  12851. /* Now read the physical PHY_ID from the chip and verify
  12852. * that it is sane. If it doesn't look good, we fall back
  12853. * to either the hard-coded table based PHY_ID and failing
  12854. * that the value found in the eeprom area.
  12855. */
  12856. err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
  12857. err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
  12858. hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
  12859. hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
  12860. hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
  12861. hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
  12862. }
  12863. if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
  12864. tp->phy_id = hw_phy_id;
  12865. if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
  12866. tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
  12867. else
  12868. tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
  12869. } else {
  12870. if (tp->phy_id != TG3_PHY_ID_INVALID) {
  12871. /* Do nothing, phy ID already set up in
  12872. * tg3_get_eeprom_hw_cfg().
  12873. */
  12874. } else {
  12875. struct subsys_tbl_ent *p;
  12876. /* No eeprom signature? Try the hardcoded
  12877. * subsys device table.
  12878. */
  12879. p = tg3_lookup_by_subsys(tp);
  12880. if (p) {
  12881. tp->phy_id = p->phy_id;
  12882. } else if (!tg3_flag(tp, IS_SSB_CORE)) {
  12883. /* For now we saw the IDs 0xbc050cd0,
  12884. * 0xbc050f80 and 0xbc050c30 on devices
  12885. * connected to an BCM4785 and there are
  12886. * probably more. Just assume that the phy is
  12887. * supported when it is connected to a SSB core
  12888. * for now.
  12889. */
  12890. return -ENODEV;
  12891. }
  12892. if (!tp->phy_id ||
  12893. tp->phy_id == TG3_PHY_ID_BCM8002)
  12894. tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
  12895. }
  12896. }
  12897. if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
  12898. (tg3_asic_rev(tp) == ASIC_REV_5719 ||
  12899. tg3_asic_rev(tp) == ASIC_REV_5720 ||
  12900. tg3_asic_rev(tp) == ASIC_REV_57766 ||
  12901. tg3_asic_rev(tp) == ASIC_REV_5762 ||
  12902. (tg3_asic_rev(tp) == ASIC_REV_5717 &&
  12903. tg3_chip_rev_id(tp) != CHIPREV_ID_5717_A0) ||
  12904. (tg3_asic_rev(tp) == ASIC_REV_57765 &&
  12905. tg3_chip_rev_id(tp) != CHIPREV_ID_57765_A0))) {
  12906. tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
  12907. tp->eee.supported = SUPPORTED_100baseT_Full |
  12908. SUPPORTED_1000baseT_Full;
  12909. tp->eee.advertised = ADVERTISED_100baseT_Full |
  12910. ADVERTISED_1000baseT_Full;
  12911. tp->eee.eee_enabled = 1;
  12912. tp->eee.tx_lpi_enabled = 1;
  12913. tp->eee.tx_lpi_timer = TG3_CPMU_DBTMR1_LNKIDLE_2047US;
  12914. }
  12915. tg3_phy_init_link_config(tp);
  12916. if (!(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN) &&
  12917. !(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
  12918. !tg3_flag(tp, ENABLE_APE) &&
  12919. !tg3_flag(tp, ENABLE_ASF)) {
  12920. u32 bmsr, dummy;
  12921. tg3_readphy(tp, MII_BMSR, &bmsr);
  12922. if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
  12923. (bmsr & BMSR_LSTATUS))
  12924. goto skip_phy_reset;
  12925. err = tg3_phy_reset(tp);
  12926. if (err)
  12927. return err;
  12928. tg3_phy_set_wirespeed(tp);
  12929. if (!tg3_phy_copper_an_config_ok(tp, &dummy)) {
  12930. tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
  12931. tp->link_config.flowctrl);
  12932. tg3_writephy(tp, MII_BMCR,
  12933. BMCR_ANENABLE | BMCR_ANRESTART);
  12934. }
  12935. }
  12936. skip_phy_reset:
  12937. if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
  12938. err = tg3_init_5401phy_dsp(tp);
  12939. if (err)
  12940. return err;
  12941. err = tg3_init_5401phy_dsp(tp);
  12942. }
  12943. return err;
  12944. }
  12945. static void tg3_read_vpd(struct tg3 *tp)
  12946. {
  12947. u8 *vpd_data;
  12948. unsigned int block_end, rosize, len;
  12949. u32 vpdlen;
  12950. int j, i = 0;
  12951. vpd_data = (u8 *)tg3_vpd_readblock(tp, &vpdlen);
  12952. if (!vpd_data)
  12953. goto out_no_vpd;
  12954. i = pci_vpd_find_tag(vpd_data, 0, vpdlen, PCI_VPD_LRDT_RO_DATA);
  12955. if (i < 0)
  12956. goto out_not_found;
  12957. rosize = pci_vpd_lrdt_size(&vpd_data[i]);
  12958. block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
  12959. i += PCI_VPD_LRDT_TAG_SIZE;
  12960. if (block_end > vpdlen)
  12961. goto out_not_found;
  12962. j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
  12963. PCI_VPD_RO_KEYWORD_MFR_ID);
  12964. if (j > 0) {
  12965. len = pci_vpd_info_field_size(&vpd_data[j]);
  12966. j += PCI_VPD_INFO_FLD_HDR_SIZE;
  12967. if (j + len > block_end || len != 4 ||
  12968. memcmp(&vpd_data[j], "1028", 4))
  12969. goto partno;
  12970. j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
  12971. PCI_VPD_RO_KEYWORD_VENDOR0);
  12972. if (j < 0)
  12973. goto partno;
  12974. len = pci_vpd_info_field_size(&vpd_data[j]);
  12975. j += PCI_VPD_INFO_FLD_HDR_SIZE;
  12976. if (j + len > block_end)
  12977. goto partno;
  12978. if (len >= sizeof(tp->fw_ver))
  12979. len = sizeof(tp->fw_ver) - 1;
  12980. memset(tp->fw_ver, 0, sizeof(tp->fw_ver));
  12981. snprintf(tp->fw_ver, sizeof(tp->fw_ver), "%.*s bc ", len,
  12982. &vpd_data[j]);
  12983. }
  12984. partno:
  12985. i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
  12986. PCI_VPD_RO_KEYWORD_PARTNO);
  12987. if (i < 0)
  12988. goto out_not_found;
  12989. len = pci_vpd_info_field_size(&vpd_data[i]);
  12990. i += PCI_VPD_INFO_FLD_HDR_SIZE;
  12991. if (len > TG3_BPN_SIZE ||
  12992. (len + i) > vpdlen)
  12993. goto out_not_found;
  12994. memcpy(tp->board_part_number, &vpd_data[i], len);
  12995. out_not_found:
  12996. kfree(vpd_data);
  12997. if (tp->board_part_number[0])
  12998. return;
  12999. out_no_vpd:
  13000. if (tg3_asic_rev(tp) == ASIC_REV_5717) {
  13001. if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
  13002. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C)
  13003. strcpy(tp->board_part_number, "BCM5717");
  13004. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
  13005. strcpy(tp->board_part_number, "BCM5718");
  13006. else
  13007. goto nomatch;
  13008. } else if (tg3_asic_rev(tp) == ASIC_REV_57780) {
  13009. if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
  13010. strcpy(tp->board_part_number, "BCM57780");
  13011. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
  13012. strcpy(tp->board_part_number, "BCM57760");
  13013. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
  13014. strcpy(tp->board_part_number, "BCM57790");
  13015. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
  13016. strcpy(tp->board_part_number, "BCM57788");
  13017. else
  13018. goto nomatch;
  13019. } else if (tg3_asic_rev(tp) == ASIC_REV_57765) {
  13020. if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
  13021. strcpy(tp->board_part_number, "BCM57761");
  13022. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
  13023. strcpy(tp->board_part_number, "BCM57765");
  13024. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
  13025. strcpy(tp->board_part_number, "BCM57781");
  13026. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
  13027. strcpy(tp->board_part_number, "BCM57785");
  13028. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
  13029. strcpy(tp->board_part_number, "BCM57791");
  13030. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
  13031. strcpy(tp->board_part_number, "BCM57795");
  13032. else
  13033. goto nomatch;
  13034. } else if (tg3_asic_rev(tp) == ASIC_REV_57766) {
  13035. if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762)
  13036. strcpy(tp->board_part_number, "BCM57762");
  13037. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766)
  13038. strcpy(tp->board_part_number, "BCM57766");
  13039. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782)
  13040. strcpy(tp->board_part_number, "BCM57782");
  13041. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
  13042. strcpy(tp->board_part_number, "BCM57786");
  13043. else
  13044. goto nomatch;
  13045. } else if (tg3_asic_rev(tp) == ASIC_REV_5906) {
  13046. strcpy(tp->board_part_number, "BCM95906");
  13047. } else {
  13048. nomatch:
  13049. strcpy(tp->board_part_number, "none");
  13050. }
  13051. }
  13052. static int tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
  13053. {
  13054. u32 val;
  13055. if (tg3_nvram_read(tp, offset, &val) ||
  13056. (val & 0xfc000000) != 0x0c000000 ||
  13057. tg3_nvram_read(tp, offset + 4, &val) ||
  13058. val != 0)
  13059. return 0;
  13060. return 1;
  13061. }
  13062. static void tg3_read_bc_ver(struct tg3 *tp)
  13063. {
  13064. u32 val, offset, start, ver_offset;
  13065. int i, dst_off;
  13066. bool newver = false;
  13067. if (tg3_nvram_read(tp, 0xc, &offset) ||
  13068. tg3_nvram_read(tp, 0x4, &start))
  13069. return;
  13070. offset = tg3_nvram_logical_addr(tp, offset);
  13071. if (tg3_nvram_read(tp, offset, &val))
  13072. return;
  13073. if ((val & 0xfc000000) == 0x0c000000) {
  13074. if (tg3_nvram_read(tp, offset + 4, &val))
  13075. return;
  13076. if (val == 0)
  13077. newver = true;
  13078. }
  13079. dst_off = strlen(tp->fw_ver);
  13080. if (newver) {
  13081. if (TG3_VER_SIZE - dst_off < 16 ||
  13082. tg3_nvram_read(tp, offset + 8, &ver_offset))
  13083. return;
  13084. offset = offset + ver_offset - start;
  13085. for (i = 0; i < 16; i += 4) {
  13086. __be32 v;
  13087. if (tg3_nvram_read_be32(tp, offset + i, &v))
  13088. return;
  13089. memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
  13090. }
  13091. } else {
  13092. u32 major, minor;
  13093. if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
  13094. return;
  13095. major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
  13096. TG3_NVM_BCVER_MAJSFT;
  13097. minor = ver_offset & TG3_NVM_BCVER_MINMSK;
  13098. snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
  13099. "v%d.%02d", major, minor);
  13100. }
  13101. }
  13102. static void tg3_read_hwsb_ver(struct tg3 *tp)
  13103. {
  13104. u32 val, major, minor;
  13105. /* Use native endian representation */
  13106. if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
  13107. return;
  13108. major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
  13109. TG3_NVM_HWSB_CFG1_MAJSFT;
  13110. minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
  13111. TG3_NVM_HWSB_CFG1_MINSFT;
  13112. snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
  13113. }
  13114. static void tg3_read_sb_ver(struct tg3 *tp, u32 val)
  13115. {
  13116. u32 offset, major, minor, build;
  13117. strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
  13118. if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
  13119. return;
  13120. switch (val & TG3_EEPROM_SB_REVISION_MASK) {
  13121. case TG3_EEPROM_SB_REVISION_0:
  13122. offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
  13123. break;
  13124. case TG3_EEPROM_SB_REVISION_2:
  13125. offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
  13126. break;
  13127. case TG3_EEPROM_SB_REVISION_3:
  13128. offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
  13129. break;
  13130. case TG3_EEPROM_SB_REVISION_4:
  13131. offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
  13132. break;
  13133. case TG3_EEPROM_SB_REVISION_5:
  13134. offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
  13135. break;
  13136. case TG3_EEPROM_SB_REVISION_6:
  13137. offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
  13138. break;
  13139. default:
  13140. return;
  13141. }
  13142. if (tg3_nvram_read(tp, offset, &val))
  13143. return;
  13144. build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
  13145. TG3_EEPROM_SB_EDH_BLD_SHFT;
  13146. major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
  13147. TG3_EEPROM_SB_EDH_MAJ_SHFT;
  13148. minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
  13149. if (minor > 99 || build > 26)
  13150. return;
  13151. offset = strlen(tp->fw_ver);
  13152. snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
  13153. " v%d.%02d", major, minor);
  13154. if (build > 0) {
  13155. offset = strlen(tp->fw_ver);
  13156. if (offset < TG3_VER_SIZE - 1)
  13157. tp->fw_ver[offset] = 'a' + build - 1;
  13158. }
  13159. }
  13160. static void tg3_read_mgmtfw_ver(struct tg3 *tp)
  13161. {
  13162. u32 val, offset, start;
  13163. int i, vlen;
  13164. for (offset = TG3_NVM_DIR_START;
  13165. offset < TG3_NVM_DIR_END;
  13166. offset += TG3_NVM_DIRENT_SIZE) {
  13167. if (tg3_nvram_read(tp, offset, &val))
  13168. return;
  13169. if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
  13170. break;
  13171. }
  13172. if (offset == TG3_NVM_DIR_END)
  13173. return;
  13174. if (!tg3_flag(tp, 5705_PLUS))
  13175. start = 0x08000000;
  13176. else if (tg3_nvram_read(tp, offset - 4, &start))
  13177. return;
  13178. if (tg3_nvram_read(tp, offset + 4, &offset) ||
  13179. !tg3_fw_img_is_valid(tp, offset) ||
  13180. tg3_nvram_read(tp, offset + 8, &val))
  13181. return;
  13182. offset += val - start;
  13183. vlen = strlen(tp->fw_ver);
  13184. tp->fw_ver[vlen++] = ',';
  13185. tp->fw_ver[vlen++] = ' ';
  13186. for (i = 0; i < 4; i++) {
  13187. __be32 v;
  13188. if (tg3_nvram_read_be32(tp, offset, &v))
  13189. return;
  13190. offset += sizeof(v);
  13191. if (vlen > TG3_VER_SIZE - sizeof(v)) {
  13192. memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
  13193. break;
  13194. }
  13195. memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
  13196. vlen += sizeof(v);
  13197. }
  13198. }
  13199. static void tg3_probe_ncsi(struct tg3 *tp)
  13200. {
  13201. u32 apedata;
  13202. apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
  13203. if (apedata != APE_SEG_SIG_MAGIC)
  13204. return;
  13205. apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
  13206. if (!(apedata & APE_FW_STATUS_READY))
  13207. return;
  13208. if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI)
  13209. tg3_flag_set(tp, APE_HAS_NCSI);
  13210. }
  13211. static void tg3_read_dash_ver(struct tg3 *tp)
  13212. {
  13213. int vlen;
  13214. u32 apedata;
  13215. char *fwtype;
  13216. apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
  13217. if (tg3_flag(tp, APE_HAS_NCSI))
  13218. fwtype = "NCSI";
  13219. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725)
  13220. fwtype = "SMASH";
  13221. else
  13222. fwtype = "DASH";
  13223. vlen = strlen(tp->fw_ver);
  13224. snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
  13225. fwtype,
  13226. (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
  13227. (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
  13228. (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
  13229. (apedata & APE_FW_VERSION_BLDMSK));
  13230. }
  13231. static void tg3_read_otp_ver(struct tg3 *tp)
  13232. {
  13233. u32 val, val2;
  13234. if (tg3_asic_rev(tp) != ASIC_REV_5762)
  13235. return;
  13236. if (!tg3_ape_otp_read(tp, OTP_ADDRESS_MAGIC0, &val) &&
  13237. !tg3_ape_otp_read(tp, OTP_ADDRESS_MAGIC0 + 4, &val2) &&
  13238. TG3_OTP_MAGIC0_VALID(val)) {
  13239. u64 val64 = (u64) val << 32 | val2;
  13240. u32 ver = 0;
  13241. int i, vlen;
  13242. for (i = 0; i < 7; i++) {
  13243. if ((val64 & 0xff) == 0)
  13244. break;
  13245. ver = val64 & 0xff;
  13246. val64 >>= 8;
  13247. }
  13248. vlen = strlen(tp->fw_ver);
  13249. snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " .%02d", ver);
  13250. }
  13251. }
  13252. static void tg3_read_fw_ver(struct tg3 *tp)
  13253. {
  13254. u32 val;
  13255. bool vpd_vers = false;
  13256. if (tp->fw_ver[0] != 0)
  13257. vpd_vers = true;
  13258. if (tg3_flag(tp, NO_NVRAM)) {
  13259. strcat(tp->fw_ver, "sb");
  13260. tg3_read_otp_ver(tp);
  13261. return;
  13262. }
  13263. if (tg3_nvram_read(tp, 0, &val))
  13264. return;
  13265. if (val == TG3_EEPROM_MAGIC)
  13266. tg3_read_bc_ver(tp);
  13267. else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
  13268. tg3_read_sb_ver(tp, val);
  13269. else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
  13270. tg3_read_hwsb_ver(tp);
  13271. if (tg3_flag(tp, ENABLE_ASF)) {
  13272. if (tg3_flag(tp, ENABLE_APE)) {
  13273. tg3_probe_ncsi(tp);
  13274. if (!vpd_vers)
  13275. tg3_read_dash_ver(tp);
  13276. } else if (!vpd_vers) {
  13277. tg3_read_mgmtfw_ver(tp);
  13278. }
  13279. }
  13280. tp->fw_ver[TG3_VER_SIZE - 1] = 0;
  13281. }
  13282. static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
  13283. {
  13284. if (tg3_flag(tp, LRG_PROD_RING_CAP))
  13285. return TG3_RX_RET_MAX_SIZE_5717;
  13286. else if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))
  13287. return TG3_RX_RET_MAX_SIZE_5700;
  13288. else
  13289. return TG3_RX_RET_MAX_SIZE_5705;
  13290. }
  13291. static const struct pci_device_id tg3_write_reorder_chipsets[] = {
  13292. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
  13293. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
  13294. { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
  13295. { },
  13296. };
  13297. static struct pci_dev *tg3_find_peer(struct tg3 *tp)
  13298. {
  13299. struct pci_dev *peer;
  13300. unsigned int func, devnr = tp->pdev->devfn & ~7;
  13301. for (func = 0; func < 8; func++) {
  13302. peer = pci_get_slot(tp->pdev->bus, devnr | func);
  13303. if (peer && peer != tp->pdev)
  13304. break;
  13305. pci_dev_put(peer);
  13306. }
  13307. /* 5704 can be configured in single-port mode, set peer to
  13308. * tp->pdev in that case.
  13309. */
  13310. if (!peer) {
  13311. peer = tp->pdev;
  13312. return peer;
  13313. }
  13314. /*
  13315. * We don't need to keep the refcount elevated; there's no way
  13316. * to remove one half of this device without removing the other
  13317. */
  13318. pci_dev_put(peer);
  13319. return peer;
  13320. }
  13321. static void tg3_detect_asic_rev(struct tg3 *tp, u32 misc_ctrl_reg)
  13322. {
  13323. tp->pci_chip_rev_id = misc_ctrl_reg >> MISC_HOST_CTRL_CHIPREV_SHIFT;
  13324. if (tg3_asic_rev(tp) == ASIC_REV_USE_PROD_ID_REG) {
  13325. u32 reg;
  13326. /* All devices that use the alternate
  13327. * ASIC REV location have a CPMU.
  13328. */
  13329. tg3_flag_set(tp, CPMU_PRESENT);
  13330. if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
  13331. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C ||
  13332. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
  13333. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
  13334. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720 ||
  13335. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57767 ||
  13336. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57764 ||
  13337. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5762 ||
  13338. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725 ||
  13339. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5727 ||
  13340. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57787)
  13341. reg = TG3PCI_GEN2_PRODID_ASICREV;
  13342. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
  13343. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
  13344. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
  13345. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
  13346. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
  13347. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
  13348. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762 ||
  13349. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766 ||
  13350. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782 ||
  13351. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
  13352. reg = TG3PCI_GEN15_PRODID_ASICREV;
  13353. else
  13354. reg = TG3PCI_PRODID_ASICREV;
  13355. pci_read_config_dword(tp->pdev, reg, &tp->pci_chip_rev_id);
  13356. }
  13357. /* Wrong chip ID in 5752 A0. This code can be removed later
  13358. * as A0 is not in production.
  13359. */
  13360. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5752_A0_HW)
  13361. tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
  13362. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5717_C0)
  13363. tp->pci_chip_rev_id = CHIPREV_ID_5720_A0;
  13364. if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
  13365. tg3_asic_rev(tp) == ASIC_REV_5719 ||
  13366. tg3_asic_rev(tp) == ASIC_REV_5720)
  13367. tg3_flag_set(tp, 5717_PLUS);
  13368. if (tg3_asic_rev(tp) == ASIC_REV_57765 ||
  13369. tg3_asic_rev(tp) == ASIC_REV_57766)
  13370. tg3_flag_set(tp, 57765_CLASS);
  13371. if (tg3_flag(tp, 57765_CLASS) || tg3_flag(tp, 5717_PLUS) ||
  13372. tg3_asic_rev(tp) == ASIC_REV_5762)
  13373. tg3_flag_set(tp, 57765_PLUS);
  13374. /* Intentionally exclude ASIC_REV_5906 */
  13375. if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
  13376. tg3_asic_rev(tp) == ASIC_REV_5787 ||
  13377. tg3_asic_rev(tp) == ASIC_REV_5784 ||
  13378. tg3_asic_rev(tp) == ASIC_REV_5761 ||
  13379. tg3_asic_rev(tp) == ASIC_REV_5785 ||
  13380. tg3_asic_rev(tp) == ASIC_REV_57780 ||
  13381. tg3_flag(tp, 57765_PLUS))
  13382. tg3_flag_set(tp, 5755_PLUS);
  13383. if (tg3_asic_rev(tp) == ASIC_REV_5780 ||
  13384. tg3_asic_rev(tp) == ASIC_REV_5714)
  13385. tg3_flag_set(tp, 5780_CLASS);
  13386. if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
  13387. tg3_asic_rev(tp) == ASIC_REV_5752 ||
  13388. tg3_asic_rev(tp) == ASIC_REV_5906 ||
  13389. tg3_flag(tp, 5755_PLUS) ||
  13390. tg3_flag(tp, 5780_CLASS))
  13391. tg3_flag_set(tp, 5750_PLUS);
  13392. if (tg3_asic_rev(tp) == ASIC_REV_5705 ||
  13393. tg3_flag(tp, 5750_PLUS))
  13394. tg3_flag_set(tp, 5705_PLUS);
  13395. }
  13396. static bool tg3_10_100_only_device(struct tg3 *tp,
  13397. const struct pci_device_id *ent)
  13398. {
  13399. u32 grc_misc_cfg = tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK;
  13400. if ((tg3_asic_rev(tp) == ASIC_REV_5703 &&
  13401. (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
  13402. (tp->phy_flags & TG3_PHYFLG_IS_FET))
  13403. return true;
  13404. if (ent->driver_data & TG3_DRV_DATA_FLAG_10_100_ONLY) {
  13405. if (tg3_asic_rev(tp) == ASIC_REV_5705) {
  13406. if (ent->driver_data & TG3_DRV_DATA_FLAG_5705_10_100)
  13407. return true;
  13408. } else {
  13409. return true;
  13410. }
  13411. }
  13412. return false;
  13413. }
  13414. static int tg3_get_invariants(struct tg3 *tp, const struct pci_device_id *ent)
  13415. {
  13416. u32 misc_ctrl_reg;
  13417. u32 pci_state_reg, grc_misc_cfg;
  13418. u32 val;
  13419. u16 pci_cmd;
  13420. int err;
  13421. /* Force memory write invalidate off. If we leave it on,
  13422. * then on 5700_BX chips we have to enable a workaround.
  13423. * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
  13424. * to match the cacheline size. The Broadcom driver have this
  13425. * workaround but turns MWI off all the times so never uses
  13426. * it. This seems to suggest that the workaround is insufficient.
  13427. */
  13428. pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
  13429. pci_cmd &= ~PCI_COMMAND_INVALIDATE;
  13430. pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
  13431. /* Important! -- Make sure register accesses are byteswapped
  13432. * correctly. Also, for those chips that require it, make
  13433. * sure that indirect register accesses are enabled before
  13434. * the first operation.
  13435. */
  13436. pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
  13437. &misc_ctrl_reg);
  13438. tp->misc_host_ctrl |= (misc_ctrl_reg &
  13439. MISC_HOST_CTRL_CHIPREV);
  13440. pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
  13441. tp->misc_host_ctrl);
  13442. tg3_detect_asic_rev(tp, misc_ctrl_reg);
  13443. /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
  13444. * we need to disable memory and use config. cycles
  13445. * only to access all registers. The 5702/03 chips
  13446. * can mistakenly decode the special cycles from the
  13447. * ICH chipsets as memory write cycles, causing corruption
  13448. * of register and memory space. Only certain ICH bridges
  13449. * will drive special cycles with non-zero data during the
  13450. * address phase which can fall within the 5703's address
  13451. * range. This is not an ICH bug as the PCI spec allows
  13452. * non-zero address during special cycles. However, only
  13453. * these ICH bridges are known to drive non-zero addresses
  13454. * during special cycles.
  13455. *
  13456. * Since special cycles do not cross PCI bridges, we only
  13457. * enable this workaround if the 5703 is on the secondary
  13458. * bus of these ICH bridges.
  13459. */
  13460. if ((tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A1) ||
  13461. (tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A2)) {
  13462. static struct tg3_dev_id {
  13463. u32 vendor;
  13464. u32 device;
  13465. u32 rev;
  13466. } ich_chipsets[] = {
  13467. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
  13468. PCI_ANY_ID },
  13469. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
  13470. PCI_ANY_ID },
  13471. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
  13472. 0xa },
  13473. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
  13474. PCI_ANY_ID },
  13475. { },
  13476. };
  13477. struct tg3_dev_id *pci_id = &ich_chipsets[0];
  13478. struct pci_dev *bridge = NULL;
  13479. while (pci_id->vendor != 0) {
  13480. bridge = pci_get_device(pci_id->vendor, pci_id->device,
  13481. bridge);
  13482. if (!bridge) {
  13483. pci_id++;
  13484. continue;
  13485. }
  13486. if (pci_id->rev != PCI_ANY_ID) {
  13487. if (bridge->revision > pci_id->rev)
  13488. continue;
  13489. }
  13490. if (bridge->subordinate &&
  13491. (bridge->subordinate->number ==
  13492. tp->pdev->bus->number)) {
  13493. tg3_flag_set(tp, ICH_WORKAROUND);
  13494. pci_dev_put(bridge);
  13495. break;
  13496. }
  13497. }
  13498. }
  13499. if (tg3_asic_rev(tp) == ASIC_REV_5701) {
  13500. static struct tg3_dev_id {
  13501. u32 vendor;
  13502. u32 device;
  13503. } bridge_chipsets[] = {
  13504. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
  13505. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
  13506. { },
  13507. };
  13508. struct tg3_dev_id *pci_id = &bridge_chipsets[0];
  13509. struct pci_dev *bridge = NULL;
  13510. while (pci_id->vendor != 0) {
  13511. bridge = pci_get_device(pci_id->vendor,
  13512. pci_id->device,
  13513. bridge);
  13514. if (!bridge) {
  13515. pci_id++;
  13516. continue;
  13517. }
  13518. if (bridge->subordinate &&
  13519. (bridge->subordinate->number <=
  13520. tp->pdev->bus->number) &&
  13521. (bridge->subordinate->busn_res.end >=
  13522. tp->pdev->bus->number)) {
  13523. tg3_flag_set(tp, 5701_DMA_BUG);
  13524. pci_dev_put(bridge);
  13525. break;
  13526. }
  13527. }
  13528. }
  13529. /* The EPB bridge inside 5714, 5715, and 5780 cannot support
  13530. * DMA addresses > 40-bit. This bridge may have other additional
  13531. * 57xx devices behind it in some 4-port NIC designs for example.
  13532. * Any tg3 device found behind the bridge will also need the 40-bit
  13533. * DMA workaround.
  13534. */
  13535. if (tg3_flag(tp, 5780_CLASS)) {
  13536. tg3_flag_set(tp, 40BIT_DMA_BUG);
  13537. tp->msi_cap = tp->pdev->msi_cap;
  13538. } else {
  13539. struct pci_dev *bridge = NULL;
  13540. do {
  13541. bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
  13542. PCI_DEVICE_ID_SERVERWORKS_EPB,
  13543. bridge);
  13544. if (bridge && bridge->subordinate &&
  13545. (bridge->subordinate->number <=
  13546. tp->pdev->bus->number) &&
  13547. (bridge->subordinate->busn_res.end >=
  13548. tp->pdev->bus->number)) {
  13549. tg3_flag_set(tp, 40BIT_DMA_BUG);
  13550. pci_dev_put(bridge);
  13551. break;
  13552. }
  13553. } while (bridge);
  13554. }
  13555. if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
  13556. tg3_asic_rev(tp) == ASIC_REV_5714)
  13557. tp->pdev_peer = tg3_find_peer(tp);
  13558. /* Determine TSO capabilities */
  13559. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0)
  13560. ; /* Do nothing. HW bug. */
  13561. else if (tg3_flag(tp, 57765_PLUS))
  13562. tg3_flag_set(tp, HW_TSO_3);
  13563. else if (tg3_flag(tp, 5755_PLUS) ||
  13564. tg3_asic_rev(tp) == ASIC_REV_5906)
  13565. tg3_flag_set(tp, HW_TSO_2);
  13566. else if (tg3_flag(tp, 5750_PLUS)) {
  13567. tg3_flag_set(tp, HW_TSO_1);
  13568. tg3_flag_set(tp, TSO_BUG);
  13569. if (tg3_asic_rev(tp) == ASIC_REV_5750 &&
  13570. tg3_chip_rev_id(tp) >= CHIPREV_ID_5750_C2)
  13571. tg3_flag_clear(tp, TSO_BUG);
  13572. } else if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
  13573. tg3_asic_rev(tp) != ASIC_REV_5701 &&
  13574. tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
  13575. tg3_flag_set(tp, FW_TSO);
  13576. tg3_flag_set(tp, TSO_BUG);
  13577. if (tg3_asic_rev(tp) == ASIC_REV_5705)
  13578. tp->fw_needed = FIRMWARE_TG3TSO5;
  13579. else
  13580. tp->fw_needed = FIRMWARE_TG3TSO;
  13581. }
  13582. /* Selectively allow TSO based on operating conditions */
  13583. if (tg3_flag(tp, HW_TSO_1) ||
  13584. tg3_flag(tp, HW_TSO_2) ||
  13585. tg3_flag(tp, HW_TSO_3) ||
  13586. tg3_flag(tp, FW_TSO)) {
  13587. /* For firmware TSO, assume ASF is disabled.
  13588. * We'll disable TSO later if we discover ASF
  13589. * is enabled in tg3_get_eeprom_hw_cfg().
  13590. */
  13591. tg3_flag_set(tp, TSO_CAPABLE);
  13592. } else {
  13593. tg3_flag_clear(tp, TSO_CAPABLE);
  13594. tg3_flag_clear(tp, TSO_BUG);
  13595. tp->fw_needed = NULL;
  13596. }
  13597. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0)
  13598. tp->fw_needed = FIRMWARE_TG3;
  13599. if (tg3_asic_rev(tp) == ASIC_REV_57766)
  13600. tp->fw_needed = FIRMWARE_TG357766;
  13601. tp->irq_max = 1;
  13602. if (tg3_flag(tp, 5750_PLUS)) {
  13603. tg3_flag_set(tp, SUPPORT_MSI);
  13604. if (tg3_chip_rev(tp) == CHIPREV_5750_AX ||
  13605. tg3_chip_rev(tp) == CHIPREV_5750_BX ||
  13606. (tg3_asic_rev(tp) == ASIC_REV_5714 &&
  13607. tg3_chip_rev_id(tp) <= CHIPREV_ID_5714_A2 &&
  13608. tp->pdev_peer == tp->pdev))
  13609. tg3_flag_clear(tp, SUPPORT_MSI);
  13610. if (tg3_flag(tp, 5755_PLUS) ||
  13611. tg3_asic_rev(tp) == ASIC_REV_5906) {
  13612. tg3_flag_set(tp, 1SHOT_MSI);
  13613. }
  13614. if (tg3_flag(tp, 57765_PLUS)) {
  13615. tg3_flag_set(tp, SUPPORT_MSIX);
  13616. tp->irq_max = TG3_IRQ_MAX_VECS;
  13617. }
  13618. }
  13619. tp->txq_max = 1;
  13620. tp->rxq_max = 1;
  13621. if (tp->irq_max > 1) {
  13622. tp->rxq_max = TG3_RSS_MAX_NUM_QS;
  13623. tg3_rss_init_dflt_indir_tbl(tp, TG3_RSS_MAX_NUM_QS);
  13624. if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
  13625. tg3_asic_rev(tp) == ASIC_REV_5720)
  13626. tp->txq_max = tp->irq_max - 1;
  13627. }
  13628. if (tg3_flag(tp, 5755_PLUS) ||
  13629. tg3_asic_rev(tp) == ASIC_REV_5906)
  13630. tg3_flag_set(tp, SHORT_DMA_BUG);
  13631. if (tg3_asic_rev(tp) == ASIC_REV_5719)
  13632. tp->dma_limit = TG3_TX_BD_DMA_MAX_4K;
  13633. if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
  13634. tg3_asic_rev(tp) == ASIC_REV_5719 ||
  13635. tg3_asic_rev(tp) == ASIC_REV_5720 ||
  13636. tg3_asic_rev(tp) == ASIC_REV_5762)
  13637. tg3_flag_set(tp, LRG_PROD_RING_CAP);
  13638. if (tg3_flag(tp, 57765_PLUS) &&
  13639. tg3_chip_rev_id(tp) != CHIPREV_ID_5719_A0)
  13640. tg3_flag_set(tp, USE_JUMBO_BDFLAG);
  13641. if (!tg3_flag(tp, 5705_PLUS) ||
  13642. tg3_flag(tp, 5780_CLASS) ||
  13643. tg3_flag(tp, USE_JUMBO_BDFLAG))
  13644. tg3_flag_set(tp, JUMBO_CAPABLE);
  13645. pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
  13646. &pci_state_reg);
  13647. if (pci_is_pcie(tp->pdev)) {
  13648. u16 lnkctl;
  13649. tg3_flag_set(tp, PCI_EXPRESS);
  13650. pcie_capability_read_word(tp->pdev, PCI_EXP_LNKCTL, &lnkctl);
  13651. if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
  13652. if (tg3_asic_rev(tp) == ASIC_REV_5906) {
  13653. tg3_flag_clear(tp, HW_TSO_2);
  13654. tg3_flag_clear(tp, TSO_CAPABLE);
  13655. }
  13656. if (tg3_asic_rev(tp) == ASIC_REV_5784 ||
  13657. tg3_asic_rev(tp) == ASIC_REV_5761 ||
  13658. tg3_chip_rev_id(tp) == CHIPREV_ID_57780_A0 ||
  13659. tg3_chip_rev_id(tp) == CHIPREV_ID_57780_A1)
  13660. tg3_flag_set(tp, CLKREQ_BUG);
  13661. } else if (tg3_chip_rev_id(tp) == CHIPREV_ID_5717_A0) {
  13662. tg3_flag_set(tp, L1PLLPD_EN);
  13663. }
  13664. } else if (tg3_asic_rev(tp) == ASIC_REV_5785) {
  13665. /* BCM5785 devices are effectively PCIe devices, and should
  13666. * follow PCIe codepaths, but do not have a PCIe capabilities
  13667. * section.
  13668. */
  13669. tg3_flag_set(tp, PCI_EXPRESS);
  13670. } else if (!tg3_flag(tp, 5705_PLUS) ||
  13671. tg3_flag(tp, 5780_CLASS)) {
  13672. tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
  13673. if (!tp->pcix_cap) {
  13674. dev_err(&tp->pdev->dev,
  13675. "Cannot find PCI-X capability, aborting\n");
  13676. return -EIO;
  13677. }
  13678. if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
  13679. tg3_flag_set(tp, PCIX_MODE);
  13680. }
  13681. /* If we have an AMD 762 or VIA K8T800 chipset, write
  13682. * reordering to the mailbox registers done by the host
  13683. * controller can cause major troubles. We read back from
  13684. * every mailbox register write to force the writes to be
  13685. * posted to the chip in order.
  13686. */
  13687. if (pci_dev_present(tg3_write_reorder_chipsets) &&
  13688. !tg3_flag(tp, PCI_EXPRESS))
  13689. tg3_flag_set(tp, MBOX_WRITE_REORDER);
  13690. pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
  13691. &tp->pci_cacheline_sz);
  13692. pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
  13693. &tp->pci_lat_timer);
  13694. if (tg3_asic_rev(tp) == ASIC_REV_5703 &&
  13695. tp->pci_lat_timer < 64) {
  13696. tp->pci_lat_timer = 64;
  13697. pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
  13698. tp->pci_lat_timer);
  13699. }
  13700. /* Important! -- It is critical that the PCI-X hw workaround
  13701. * situation is decided before the first MMIO register access.
  13702. */
  13703. if (tg3_chip_rev(tp) == CHIPREV_5700_BX) {
  13704. /* 5700 BX chips need to have their TX producer index
  13705. * mailboxes written twice to workaround a bug.
  13706. */
  13707. tg3_flag_set(tp, TXD_MBOX_HWBUG);
  13708. /* If we are in PCI-X mode, enable register write workaround.
  13709. *
  13710. * The workaround is to use indirect register accesses
  13711. * for all chip writes not to mailbox registers.
  13712. */
  13713. if (tg3_flag(tp, PCIX_MODE)) {
  13714. u32 pm_reg;
  13715. tg3_flag_set(tp, PCIX_TARGET_HWBUG);
  13716. /* The chip can have it's power management PCI config
  13717. * space registers clobbered due to this bug.
  13718. * So explicitly force the chip into D0 here.
  13719. */
  13720. pci_read_config_dword(tp->pdev,
  13721. tp->pdev->pm_cap + PCI_PM_CTRL,
  13722. &pm_reg);
  13723. pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
  13724. pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
  13725. pci_write_config_dword(tp->pdev,
  13726. tp->pdev->pm_cap + PCI_PM_CTRL,
  13727. pm_reg);
  13728. /* Also, force SERR#/PERR# in PCI command. */
  13729. pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
  13730. pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
  13731. pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
  13732. }
  13733. }
  13734. if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
  13735. tg3_flag_set(tp, PCI_HIGH_SPEED);
  13736. if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
  13737. tg3_flag_set(tp, PCI_32BIT);
  13738. /* Chip-specific fixup from Broadcom driver */
  13739. if ((tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0) &&
  13740. (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
  13741. pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
  13742. pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
  13743. }
  13744. /* Default fast path register access methods */
  13745. tp->read32 = tg3_read32;
  13746. tp->write32 = tg3_write32;
  13747. tp->read32_mbox = tg3_read32;
  13748. tp->write32_mbox = tg3_write32;
  13749. tp->write32_tx_mbox = tg3_write32;
  13750. tp->write32_rx_mbox = tg3_write32;
  13751. /* Various workaround register access methods */
  13752. if (tg3_flag(tp, PCIX_TARGET_HWBUG))
  13753. tp->write32 = tg3_write_indirect_reg32;
  13754. else if (tg3_asic_rev(tp) == ASIC_REV_5701 ||
  13755. (tg3_flag(tp, PCI_EXPRESS) &&
  13756. tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A0)) {
  13757. /*
  13758. * Back to back register writes can cause problems on these
  13759. * chips, the workaround is to read back all reg writes
  13760. * except those to mailbox regs.
  13761. *
  13762. * See tg3_write_indirect_reg32().
  13763. */
  13764. tp->write32 = tg3_write_flush_reg32;
  13765. }
  13766. if (tg3_flag(tp, TXD_MBOX_HWBUG) || tg3_flag(tp, MBOX_WRITE_REORDER)) {
  13767. tp->write32_tx_mbox = tg3_write32_tx_mbox;
  13768. if (tg3_flag(tp, MBOX_WRITE_REORDER))
  13769. tp->write32_rx_mbox = tg3_write_flush_reg32;
  13770. }
  13771. if (tg3_flag(tp, ICH_WORKAROUND)) {
  13772. tp->read32 = tg3_read_indirect_reg32;
  13773. tp->write32 = tg3_write_indirect_reg32;
  13774. tp->read32_mbox = tg3_read_indirect_mbox;
  13775. tp->write32_mbox = tg3_write_indirect_mbox;
  13776. tp->write32_tx_mbox = tg3_write_indirect_mbox;
  13777. tp->write32_rx_mbox = tg3_write_indirect_mbox;
  13778. iounmap(tp->regs);
  13779. tp->regs = NULL;
  13780. pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
  13781. pci_cmd &= ~PCI_COMMAND_MEMORY;
  13782. pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
  13783. }
  13784. if (tg3_asic_rev(tp) == ASIC_REV_5906) {
  13785. tp->read32_mbox = tg3_read32_mbox_5906;
  13786. tp->write32_mbox = tg3_write32_mbox_5906;
  13787. tp->write32_tx_mbox = tg3_write32_mbox_5906;
  13788. tp->write32_rx_mbox = tg3_write32_mbox_5906;
  13789. }
  13790. if (tp->write32 == tg3_write_indirect_reg32 ||
  13791. (tg3_flag(tp, PCIX_MODE) &&
  13792. (tg3_asic_rev(tp) == ASIC_REV_5700 ||
  13793. tg3_asic_rev(tp) == ASIC_REV_5701)))
  13794. tg3_flag_set(tp, SRAM_USE_CONFIG);
  13795. /* The memory arbiter has to be enabled in order for SRAM accesses
  13796. * to succeed. Normally on powerup the tg3 chip firmware will make
  13797. * sure it is enabled, but other entities such as system netboot
  13798. * code might disable it.
  13799. */
  13800. val = tr32(MEMARB_MODE);
  13801. tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
  13802. tp->pci_fn = PCI_FUNC(tp->pdev->devfn) & 3;
  13803. if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
  13804. tg3_flag(tp, 5780_CLASS)) {
  13805. if (tg3_flag(tp, PCIX_MODE)) {
  13806. pci_read_config_dword(tp->pdev,
  13807. tp->pcix_cap + PCI_X_STATUS,
  13808. &val);
  13809. tp->pci_fn = val & 0x7;
  13810. }
  13811. } else if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
  13812. tg3_asic_rev(tp) == ASIC_REV_5719 ||
  13813. tg3_asic_rev(tp) == ASIC_REV_5720) {
  13814. tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
  13815. if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) != NIC_SRAM_CPMUSTAT_SIG)
  13816. val = tr32(TG3_CPMU_STATUS);
  13817. if (tg3_asic_rev(tp) == ASIC_REV_5717)
  13818. tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5717) ? 1 : 0;
  13819. else
  13820. tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5719) >>
  13821. TG3_CPMU_STATUS_FSHFT_5719;
  13822. }
  13823. if (tg3_flag(tp, FLUSH_POSTED_WRITES)) {
  13824. tp->write32_tx_mbox = tg3_write_flush_reg32;
  13825. tp->write32_rx_mbox = tg3_write_flush_reg32;
  13826. }
  13827. /* Get eeprom hw config before calling tg3_set_power_state().
  13828. * In particular, the TG3_FLAG_IS_NIC flag must be
  13829. * determined before calling tg3_set_power_state() so that
  13830. * we know whether or not to switch out of Vaux power.
  13831. * When the flag is set, it means that GPIO1 is used for eeprom
  13832. * write protect and also implies that it is a LOM where GPIOs
  13833. * are not used to switch power.
  13834. */
  13835. tg3_get_eeprom_hw_cfg(tp);
  13836. if (tg3_flag(tp, FW_TSO) && tg3_flag(tp, ENABLE_ASF)) {
  13837. tg3_flag_clear(tp, TSO_CAPABLE);
  13838. tg3_flag_clear(tp, TSO_BUG);
  13839. tp->fw_needed = NULL;
  13840. }
  13841. if (tg3_flag(tp, ENABLE_APE)) {
  13842. /* Allow reads and writes to the
  13843. * APE register and memory space.
  13844. */
  13845. pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
  13846. PCISTATE_ALLOW_APE_SHMEM_WR |
  13847. PCISTATE_ALLOW_APE_PSPACE_WR;
  13848. pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
  13849. pci_state_reg);
  13850. tg3_ape_lock_init(tp);
  13851. tp->ape_hb_interval =
  13852. msecs_to_jiffies(APE_HOST_HEARTBEAT_INT_5SEC);
  13853. }
  13854. /* Set up tp->grc_local_ctrl before calling
  13855. * tg3_pwrsrc_switch_to_vmain(). GPIO1 driven high
  13856. * will bring 5700's external PHY out of reset.
  13857. * It is also used as eeprom write protect on LOMs.
  13858. */
  13859. tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
  13860. if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
  13861. tg3_flag(tp, EEPROM_WRITE_PROT))
  13862. tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
  13863. GRC_LCLCTRL_GPIO_OUTPUT1);
  13864. /* Unused GPIO3 must be driven as output on 5752 because there
  13865. * are no pull-up resistors on unused GPIO pins.
  13866. */
  13867. else if (tg3_asic_rev(tp) == ASIC_REV_5752)
  13868. tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
  13869. if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
  13870. tg3_asic_rev(tp) == ASIC_REV_57780 ||
  13871. tg3_flag(tp, 57765_CLASS))
  13872. tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
  13873. if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
  13874. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
  13875. /* Turn off the debug UART. */
  13876. tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
  13877. if (tg3_flag(tp, IS_NIC))
  13878. /* Keep VMain power. */
  13879. tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
  13880. GRC_LCLCTRL_GPIO_OUTPUT0;
  13881. }
  13882. if (tg3_asic_rev(tp) == ASIC_REV_5762)
  13883. tp->grc_local_ctrl |=
  13884. tr32(GRC_LOCAL_CTRL) & GRC_LCLCTRL_GPIO_UART_SEL;
  13885. /* Switch out of Vaux if it is a NIC */
  13886. tg3_pwrsrc_switch_to_vmain(tp);
  13887. /* Derive initial jumbo mode from MTU assigned in
  13888. * ether_setup() via the alloc_etherdev() call
  13889. */
  13890. if (tp->dev->mtu > ETH_DATA_LEN && !tg3_flag(tp, 5780_CLASS))
  13891. tg3_flag_set(tp, JUMBO_RING_ENABLE);
  13892. /* Determine WakeOnLan speed to use. */
  13893. if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
  13894. tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
  13895. tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0 ||
  13896. tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B2) {
  13897. tg3_flag_clear(tp, WOL_SPEED_100MB);
  13898. } else {
  13899. tg3_flag_set(tp, WOL_SPEED_100MB);
  13900. }
  13901. if (tg3_asic_rev(tp) == ASIC_REV_5906)
  13902. tp->phy_flags |= TG3_PHYFLG_IS_FET;
  13903. /* A few boards don't want Ethernet@WireSpeed phy feature */
  13904. if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
  13905. (tg3_asic_rev(tp) == ASIC_REV_5705 &&
  13906. (tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) &&
  13907. (tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A1)) ||
  13908. (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
  13909. (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
  13910. tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
  13911. if (tg3_chip_rev(tp) == CHIPREV_5703_AX ||
  13912. tg3_chip_rev(tp) == CHIPREV_5704_AX)
  13913. tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
  13914. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0)
  13915. tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
  13916. if (tg3_flag(tp, 5705_PLUS) &&
  13917. !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
  13918. tg3_asic_rev(tp) != ASIC_REV_5785 &&
  13919. tg3_asic_rev(tp) != ASIC_REV_57780 &&
  13920. !tg3_flag(tp, 57765_PLUS)) {
  13921. if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
  13922. tg3_asic_rev(tp) == ASIC_REV_5787 ||
  13923. tg3_asic_rev(tp) == ASIC_REV_5784 ||
  13924. tg3_asic_rev(tp) == ASIC_REV_5761) {
  13925. if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
  13926. tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
  13927. tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
  13928. if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
  13929. tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
  13930. } else
  13931. tp->phy_flags |= TG3_PHYFLG_BER_BUG;
  13932. }
  13933. if (tg3_asic_rev(tp) == ASIC_REV_5784 &&
  13934. tg3_chip_rev(tp) != CHIPREV_5784_AX) {
  13935. tp->phy_otp = tg3_read_otp_phycfg(tp);
  13936. if (tp->phy_otp == 0)
  13937. tp->phy_otp = TG3_OTP_DEFAULT;
  13938. }
  13939. if (tg3_flag(tp, CPMU_PRESENT))
  13940. tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
  13941. else
  13942. tp->mi_mode = MAC_MI_MODE_BASE;
  13943. tp->coalesce_mode = 0;
  13944. if (tg3_chip_rev(tp) != CHIPREV_5700_AX &&
  13945. tg3_chip_rev(tp) != CHIPREV_5700_BX)
  13946. tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
  13947. /* Set these bits to enable statistics workaround. */
  13948. if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
  13949. tg3_asic_rev(tp) == ASIC_REV_5762 ||
  13950. tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
  13951. tg3_chip_rev_id(tp) == CHIPREV_ID_5720_A0) {
  13952. tp->coalesce_mode |= HOSTCC_MODE_ATTN;
  13953. tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
  13954. }
  13955. if (tg3_asic_rev(tp) == ASIC_REV_5785 ||
  13956. tg3_asic_rev(tp) == ASIC_REV_57780)
  13957. tg3_flag_set(tp, USE_PHYLIB);
  13958. err = tg3_mdio_init(tp);
  13959. if (err)
  13960. return err;
  13961. /* Initialize data/descriptor byte/word swapping. */
  13962. val = tr32(GRC_MODE);
  13963. if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
  13964. tg3_asic_rev(tp) == ASIC_REV_5762)
  13965. val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
  13966. GRC_MODE_WORD_SWAP_B2HRX_DATA |
  13967. GRC_MODE_B2HRX_ENABLE |
  13968. GRC_MODE_HTX2B_ENABLE |
  13969. GRC_MODE_HOST_STACKUP);
  13970. else
  13971. val &= GRC_MODE_HOST_STACKUP;
  13972. tw32(GRC_MODE, val | tp->grc_mode);
  13973. tg3_switch_clocks(tp);
  13974. /* Clear this out for sanity. */
  13975. tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
  13976. /* Clear TG3PCI_REG_BASE_ADDR to prevent hangs. */
  13977. tw32(TG3PCI_REG_BASE_ADDR, 0);
  13978. pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
  13979. &pci_state_reg);
  13980. if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
  13981. !tg3_flag(tp, PCIX_TARGET_HWBUG)) {
  13982. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
  13983. tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0 ||
  13984. tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B2 ||
  13985. tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B5) {
  13986. void __iomem *sram_base;
  13987. /* Write some dummy words into the SRAM status block
  13988. * area, see if it reads back correctly. If the return
  13989. * value is bad, force enable the PCIX workaround.
  13990. */
  13991. sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
  13992. writel(0x00000000, sram_base);
  13993. writel(0x00000000, sram_base + 4);
  13994. writel(0xffffffff, sram_base + 4);
  13995. if (readl(sram_base) != 0x00000000)
  13996. tg3_flag_set(tp, PCIX_TARGET_HWBUG);
  13997. }
  13998. }
  13999. udelay(50);
  14000. tg3_nvram_init(tp);
  14001. /* If the device has an NVRAM, no need to load patch firmware */
  14002. if (tg3_asic_rev(tp) == ASIC_REV_57766 &&
  14003. !tg3_flag(tp, NO_NVRAM))
  14004. tp->fw_needed = NULL;
  14005. grc_misc_cfg = tr32(GRC_MISC_CFG);
  14006. grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
  14007. if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
  14008. (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
  14009. grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
  14010. tg3_flag_set(tp, IS_5788);
  14011. if (!tg3_flag(tp, IS_5788) &&
  14012. tg3_asic_rev(tp) != ASIC_REV_5700)
  14013. tg3_flag_set(tp, TAGGED_STATUS);
  14014. if (tg3_flag(tp, TAGGED_STATUS)) {
  14015. tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
  14016. HOSTCC_MODE_CLRTICK_TXBD);
  14017. tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
  14018. pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
  14019. tp->misc_host_ctrl);
  14020. }
  14021. /* Preserve the APE MAC_MODE bits */
  14022. if (tg3_flag(tp, ENABLE_APE))
  14023. tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
  14024. else
  14025. tp->mac_mode = 0;
  14026. if (tg3_10_100_only_device(tp, ent))
  14027. tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
  14028. err = tg3_phy_probe(tp);
  14029. if (err) {
  14030. dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
  14031. /* ... but do not return immediately ... */
  14032. tg3_mdio_fini(tp);
  14033. }
  14034. tg3_read_vpd(tp);
  14035. tg3_read_fw_ver(tp);
  14036. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
  14037. tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
  14038. } else {
  14039. if (tg3_asic_rev(tp) == ASIC_REV_5700)
  14040. tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
  14041. else
  14042. tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
  14043. }
  14044. /* 5700 {AX,BX} chips have a broken status block link
  14045. * change bit implementation, so we must use the
  14046. * status register in those cases.
  14047. */
  14048. if (tg3_asic_rev(tp) == ASIC_REV_5700)
  14049. tg3_flag_set(tp, USE_LINKCHG_REG);
  14050. else
  14051. tg3_flag_clear(tp, USE_LINKCHG_REG);
  14052. /* The led_ctrl is set during tg3_phy_probe, here we might
  14053. * have to force the link status polling mechanism based
  14054. * upon subsystem IDs.
  14055. */
  14056. if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
  14057. tg3_asic_rev(tp) == ASIC_REV_5701 &&
  14058. !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
  14059. tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
  14060. tg3_flag_set(tp, USE_LINKCHG_REG);
  14061. }
  14062. /* For all SERDES we poll the MAC status register. */
  14063. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
  14064. tg3_flag_set(tp, POLL_SERDES);
  14065. else
  14066. tg3_flag_clear(tp, POLL_SERDES);
  14067. if (tg3_flag(tp, ENABLE_APE) && tg3_flag(tp, ENABLE_ASF))
  14068. tg3_flag_set(tp, POLL_CPMU_LINK);
  14069. tp->rx_offset = NET_SKB_PAD + NET_IP_ALIGN;
  14070. tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
  14071. if (tg3_asic_rev(tp) == ASIC_REV_5701 &&
  14072. tg3_flag(tp, PCIX_MODE)) {
  14073. tp->rx_offset = NET_SKB_PAD;
  14074. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  14075. tp->rx_copy_thresh = ~(u16)0;
  14076. #endif
  14077. }
  14078. tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
  14079. tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
  14080. tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
  14081. tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
  14082. /* Increment the rx prod index on the rx std ring by at most
  14083. * 8 for these chips to workaround hw errata.
  14084. */
  14085. if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
  14086. tg3_asic_rev(tp) == ASIC_REV_5752 ||
  14087. tg3_asic_rev(tp) == ASIC_REV_5755)
  14088. tp->rx_std_max_post = 8;
  14089. if (tg3_flag(tp, ASPM_WORKAROUND))
  14090. tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
  14091. PCIE_PWR_MGMT_L1_THRESH_MSK;
  14092. return err;
  14093. }
  14094. #ifdef CONFIG_SPARC
  14095. static int tg3_get_macaddr_sparc(struct tg3 *tp)
  14096. {
  14097. struct net_device *dev = tp->dev;
  14098. struct pci_dev *pdev = tp->pdev;
  14099. struct device_node *dp = pci_device_to_OF_node(pdev);
  14100. const unsigned char *addr;
  14101. int len;
  14102. addr = of_get_property(dp, "local-mac-address", &len);
  14103. if (addr && len == ETH_ALEN) {
  14104. memcpy(dev->dev_addr, addr, ETH_ALEN);
  14105. return 0;
  14106. }
  14107. return -ENODEV;
  14108. }
  14109. static int tg3_get_default_macaddr_sparc(struct tg3 *tp)
  14110. {
  14111. struct net_device *dev = tp->dev;
  14112. memcpy(dev->dev_addr, idprom->id_ethaddr, ETH_ALEN);
  14113. return 0;
  14114. }
  14115. #endif
  14116. static int tg3_get_device_address(struct tg3 *tp)
  14117. {
  14118. struct net_device *dev = tp->dev;
  14119. u32 hi, lo, mac_offset;
  14120. int addr_ok = 0;
  14121. int err;
  14122. #ifdef CONFIG_SPARC
  14123. if (!tg3_get_macaddr_sparc(tp))
  14124. return 0;
  14125. #endif
  14126. if (tg3_flag(tp, IS_SSB_CORE)) {
  14127. err = ssb_gige_get_macaddr(tp->pdev, &dev->dev_addr[0]);
  14128. if (!err && is_valid_ether_addr(&dev->dev_addr[0]))
  14129. return 0;
  14130. }
  14131. mac_offset = 0x7c;
  14132. if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
  14133. tg3_flag(tp, 5780_CLASS)) {
  14134. if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
  14135. mac_offset = 0xcc;
  14136. if (tg3_nvram_lock(tp))
  14137. tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
  14138. else
  14139. tg3_nvram_unlock(tp);
  14140. } else if (tg3_flag(tp, 5717_PLUS)) {
  14141. if (tp->pci_fn & 1)
  14142. mac_offset = 0xcc;
  14143. if (tp->pci_fn > 1)
  14144. mac_offset += 0x18c;
  14145. } else if (tg3_asic_rev(tp) == ASIC_REV_5906)
  14146. mac_offset = 0x10;
  14147. /* First try to get it from MAC address mailbox. */
  14148. tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
  14149. if ((hi >> 16) == 0x484b) {
  14150. dev->dev_addr[0] = (hi >> 8) & 0xff;
  14151. dev->dev_addr[1] = (hi >> 0) & 0xff;
  14152. tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
  14153. dev->dev_addr[2] = (lo >> 24) & 0xff;
  14154. dev->dev_addr[3] = (lo >> 16) & 0xff;
  14155. dev->dev_addr[4] = (lo >> 8) & 0xff;
  14156. dev->dev_addr[5] = (lo >> 0) & 0xff;
  14157. /* Some old bootcode may report a 0 MAC address in SRAM */
  14158. addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
  14159. }
  14160. if (!addr_ok) {
  14161. /* Next, try NVRAM. */
  14162. if (!tg3_flag(tp, NO_NVRAM) &&
  14163. !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
  14164. !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
  14165. memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
  14166. memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
  14167. }
  14168. /* Finally just fetch it out of the MAC control regs. */
  14169. else {
  14170. hi = tr32(MAC_ADDR_0_HIGH);
  14171. lo = tr32(MAC_ADDR_0_LOW);
  14172. dev->dev_addr[5] = lo & 0xff;
  14173. dev->dev_addr[4] = (lo >> 8) & 0xff;
  14174. dev->dev_addr[3] = (lo >> 16) & 0xff;
  14175. dev->dev_addr[2] = (lo >> 24) & 0xff;
  14176. dev->dev_addr[1] = hi & 0xff;
  14177. dev->dev_addr[0] = (hi >> 8) & 0xff;
  14178. }
  14179. }
  14180. if (!is_valid_ether_addr(&dev->dev_addr[0])) {
  14181. #ifdef CONFIG_SPARC
  14182. if (!tg3_get_default_macaddr_sparc(tp))
  14183. return 0;
  14184. #endif
  14185. return -EINVAL;
  14186. }
  14187. return 0;
  14188. }
  14189. #define BOUNDARY_SINGLE_CACHELINE 1
  14190. #define BOUNDARY_MULTI_CACHELINE 2
  14191. static u32 tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
  14192. {
  14193. int cacheline_size;
  14194. u8 byte;
  14195. int goal;
  14196. pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
  14197. if (byte == 0)
  14198. cacheline_size = 1024;
  14199. else
  14200. cacheline_size = (int) byte * 4;
  14201. /* On 5703 and later chips, the boundary bits have no
  14202. * effect.
  14203. */
  14204. if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
  14205. tg3_asic_rev(tp) != ASIC_REV_5701 &&
  14206. !tg3_flag(tp, PCI_EXPRESS))
  14207. goto out;
  14208. #if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
  14209. goal = BOUNDARY_MULTI_CACHELINE;
  14210. #else
  14211. #if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
  14212. goal = BOUNDARY_SINGLE_CACHELINE;
  14213. #else
  14214. goal = 0;
  14215. #endif
  14216. #endif
  14217. if (tg3_flag(tp, 57765_PLUS)) {
  14218. val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
  14219. goto out;
  14220. }
  14221. if (!goal)
  14222. goto out;
  14223. /* PCI controllers on most RISC systems tend to disconnect
  14224. * when a device tries to burst across a cache-line boundary.
  14225. * Therefore, letting tg3 do so just wastes PCI bandwidth.
  14226. *
  14227. * Unfortunately, for PCI-E there are only limited
  14228. * write-side controls for this, and thus for reads
  14229. * we will still get the disconnects. We'll also waste
  14230. * these PCI cycles for both read and write for chips
  14231. * other than 5700 and 5701 which do not implement the
  14232. * boundary bits.
  14233. */
  14234. if (tg3_flag(tp, PCIX_MODE) && !tg3_flag(tp, PCI_EXPRESS)) {
  14235. switch (cacheline_size) {
  14236. case 16:
  14237. case 32:
  14238. case 64:
  14239. case 128:
  14240. if (goal == BOUNDARY_SINGLE_CACHELINE) {
  14241. val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
  14242. DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
  14243. } else {
  14244. val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
  14245. DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
  14246. }
  14247. break;
  14248. case 256:
  14249. val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
  14250. DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
  14251. break;
  14252. default:
  14253. val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
  14254. DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
  14255. break;
  14256. }
  14257. } else if (tg3_flag(tp, PCI_EXPRESS)) {
  14258. switch (cacheline_size) {
  14259. case 16:
  14260. case 32:
  14261. case 64:
  14262. if (goal == BOUNDARY_SINGLE_CACHELINE) {
  14263. val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
  14264. val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
  14265. break;
  14266. }
  14267. /* fallthrough */
  14268. case 128:
  14269. default:
  14270. val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
  14271. val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
  14272. break;
  14273. }
  14274. } else {
  14275. switch (cacheline_size) {
  14276. case 16:
  14277. if (goal == BOUNDARY_SINGLE_CACHELINE) {
  14278. val |= (DMA_RWCTRL_READ_BNDRY_16 |
  14279. DMA_RWCTRL_WRITE_BNDRY_16);
  14280. break;
  14281. }
  14282. /* fallthrough */
  14283. case 32:
  14284. if (goal == BOUNDARY_SINGLE_CACHELINE) {
  14285. val |= (DMA_RWCTRL_READ_BNDRY_32 |
  14286. DMA_RWCTRL_WRITE_BNDRY_32);
  14287. break;
  14288. }
  14289. /* fallthrough */
  14290. case 64:
  14291. if (goal == BOUNDARY_SINGLE_CACHELINE) {
  14292. val |= (DMA_RWCTRL_READ_BNDRY_64 |
  14293. DMA_RWCTRL_WRITE_BNDRY_64);
  14294. break;
  14295. }
  14296. /* fallthrough */
  14297. case 128:
  14298. if (goal == BOUNDARY_SINGLE_CACHELINE) {
  14299. val |= (DMA_RWCTRL_READ_BNDRY_128 |
  14300. DMA_RWCTRL_WRITE_BNDRY_128);
  14301. break;
  14302. }
  14303. /* fallthrough */
  14304. case 256:
  14305. val |= (DMA_RWCTRL_READ_BNDRY_256 |
  14306. DMA_RWCTRL_WRITE_BNDRY_256);
  14307. break;
  14308. case 512:
  14309. val |= (DMA_RWCTRL_READ_BNDRY_512 |
  14310. DMA_RWCTRL_WRITE_BNDRY_512);
  14311. break;
  14312. case 1024:
  14313. default:
  14314. val |= (DMA_RWCTRL_READ_BNDRY_1024 |
  14315. DMA_RWCTRL_WRITE_BNDRY_1024);
  14316. break;
  14317. }
  14318. }
  14319. out:
  14320. return val;
  14321. }
  14322. static int tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma,
  14323. int size, bool to_device)
  14324. {
  14325. struct tg3_internal_buffer_desc test_desc;
  14326. u32 sram_dma_descs;
  14327. int i, ret;
  14328. sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
  14329. tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
  14330. tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
  14331. tw32(RDMAC_STATUS, 0);
  14332. tw32(WDMAC_STATUS, 0);
  14333. tw32(BUFMGR_MODE, 0);
  14334. tw32(FTQ_RESET, 0);
  14335. test_desc.addr_hi = ((u64) buf_dma) >> 32;
  14336. test_desc.addr_lo = buf_dma & 0xffffffff;
  14337. test_desc.nic_mbuf = 0x00002100;
  14338. test_desc.len = size;
  14339. /*
  14340. * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
  14341. * the *second* time the tg3 driver was getting loaded after an
  14342. * initial scan.
  14343. *
  14344. * Broadcom tells me:
  14345. * ...the DMA engine is connected to the GRC block and a DMA
  14346. * reset may affect the GRC block in some unpredictable way...
  14347. * The behavior of resets to individual blocks has not been tested.
  14348. *
  14349. * Broadcom noted the GRC reset will also reset all sub-components.
  14350. */
  14351. if (to_device) {
  14352. test_desc.cqid_sqid = (13 << 8) | 2;
  14353. tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
  14354. udelay(40);
  14355. } else {
  14356. test_desc.cqid_sqid = (16 << 8) | 7;
  14357. tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
  14358. udelay(40);
  14359. }
  14360. test_desc.flags = 0x00000005;
  14361. for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
  14362. u32 val;
  14363. val = *(((u32 *)&test_desc) + i);
  14364. pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
  14365. sram_dma_descs + (i * sizeof(u32)));
  14366. pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
  14367. }
  14368. pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
  14369. if (to_device)
  14370. tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
  14371. else
  14372. tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
  14373. ret = -ENODEV;
  14374. for (i = 0; i < 40; i++) {
  14375. u32 val;
  14376. if (to_device)
  14377. val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
  14378. else
  14379. val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
  14380. if ((val & 0xffff) == sram_dma_descs) {
  14381. ret = 0;
  14382. break;
  14383. }
  14384. udelay(100);
  14385. }
  14386. return ret;
  14387. }
  14388. #define TEST_BUFFER_SIZE 0x2000
  14389. static const struct pci_device_id tg3_dma_wait_state_chipsets[] = {
  14390. { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
  14391. { },
  14392. };
  14393. static int tg3_test_dma(struct tg3 *tp)
  14394. {
  14395. dma_addr_t buf_dma;
  14396. u32 *buf, saved_dma_rwctrl;
  14397. int ret = 0;
  14398. buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
  14399. &buf_dma, GFP_KERNEL);
  14400. if (!buf) {
  14401. ret = -ENOMEM;
  14402. goto out_nofree;
  14403. }
  14404. tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
  14405. (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
  14406. tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
  14407. if (tg3_flag(tp, 57765_PLUS))
  14408. goto out;
  14409. if (tg3_flag(tp, PCI_EXPRESS)) {
  14410. /* DMA read watermark not used on PCIE */
  14411. tp->dma_rwctrl |= 0x00180000;
  14412. } else if (!tg3_flag(tp, PCIX_MODE)) {
  14413. if (tg3_asic_rev(tp) == ASIC_REV_5705 ||
  14414. tg3_asic_rev(tp) == ASIC_REV_5750)
  14415. tp->dma_rwctrl |= 0x003f0000;
  14416. else
  14417. tp->dma_rwctrl |= 0x003f000f;
  14418. } else {
  14419. if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
  14420. tg3_asic_rev(tp) == ASIC_REV_5704) {
  14421. u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
  14422. u32 read_water = 0x7;
  14423. /* If the 5704 is behind the EPB bridge, we can
  14424. * do the less restrictive ONE_DMA workaround for
  14425. * better performance.
  14426. */
  14427. if (tg3_flag(tp, 40BIT_DMA_BUG) &&
  14428. tg3_asic_rev(tp) == ASIC_REV_5704)
  14429. tp->dma_rwctrl |= 0x8000;
  14430. else if (ccval == 0x6 || ccval == 0x7)
  14431. tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
  14432. if (tg3_asic_rev(tp) == ASIC_REV_5703)
  14433. read_water = 4;
  14434. /* Set bit 23 to enable PCIX hw bug fix */
  14435. tp->dma_rwctrl |=
  14436. (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
  14437. (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
  14438. (1 << 23);
  14439. } else if (tg3_asic_rev(tp) == ASIC_REV_5780) {
  14440. /* 5780 always in PCIX mode */
  14441. tp->dma_rwctrl |= 0x00144000;
  14442. } else if (tg3_asic_rev(tp) == ASIC_REV_5714) {
  14443. /* 5714 always in PCIX mode */
  14444. tp->dma_rwctrl |= 0x00148000;
  14445. } else {
  14446. tp->dma_rwctrl |= 0x001b000f;
  14447. }
  14448. }
  14449. if (tg3_flag(tp, ONE_DMA_AT_ONCE))
  14450. tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
  14451. if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
  14452. tg3_asic_rev(tp) == ASIC_REV_5704)
  14453. tp->dma_rwctrl &= 0xfffffff0;
  14454. if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
  14455. tg3_asic_rev(tp) == ASIC_REV_5701) {
  14456. /* Remove this if it causes problems for some boards. */
  14457. tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
  14458. /* On 5700/5701 chips, we need to set this bit.
  14459. * Otherwise the chip will issue cacheline transactions
  14460. * to streamable DMA memory with not all the byte
  14461. * enables turned on. This is an error on several
  14462. * RISC PCI controllers, in particular sparc64.
  14463. *
  14464. * On 5703/5704 chips, this bit has been reassigned
  14465. * a different meaning. In particular, it is used
  14466. * on those chips to enable a PCI-X workaround.
  14467. */
  14468. tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
  14469. }
  14470. tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
  14471. if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
  14472. tg3_asic_rev(tp) != ASIC_REV_5701)
  14473. goto out;
  14474. /* It is best to perform DMA test with maximum write burst size
  14475. * to expose the 5700/5701 write DMA bug.
  14476. */
  14477. saved_dma_rwctrl = tp->dma_rwctrl;
  14478. tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
  14479. tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
  14480. while (1) {
  14481. u32 *p = buf, i;
  14482. for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
  14483. p[i] = i;
  14484. /* Send the buffer to the chip. */
  14485. ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, true);
  14486. if (ret) {
  14487. dev_err(&tp->pdev->dev,
  14488. "%s: Buffer write failed. err = %d\n",
  14489. __func__, ret);
  14490. break;
  14491. }
  14492. /* Now read it back. */
  14493. ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, false);
  14494. if (ret) {
  14495. dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
  14496. "err = %d\n", __func__, ret);
  14497. break;
  14498. }
  14499. /* Verify it. */
  14500. for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
  14501. if (p[i] == i)
  14502. continue;
  14503. if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
  14504. DMA_RWCTRL_WRITE_BNDRY_16) {
  14505. tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
  14506. tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
  14507. tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
  14508. break;
  14509. } else {
  14510. dev_err(&tp->pdev->dev,
  14511. "%s: Buffer corrupted on read back! "
  14512. "(%d != %d)\n", __func__, p[i], i);
  14513. ret = -ENODEV;
  14514. goto out;
  14515. }
  14516. }
  14517. if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
  14518. /* Success. */
  14519. ret = 0;
  14520. break;
  14521. }
  14522. }
  14523. if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
  14524. DMA_RWCTRL_WRITE_BNDRY_16) {
  14525. /* DMA test passed without adjusting DMA boundary,
  14526. * now look for chipsets that are known to expose the
  14527. * DMA bug without failing the test.
  14528. */
  14529. if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
  14530. tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
  14531. tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
  14532. } else {
  14533. /* Safe to use the calculated DMA boundary. */
  14534. tp->dma_rwctrl = saved_dma_rwctrl;
  14535. }
  14536. tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
  14537. }
  14538. out:
  14539. dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
  14540. out_nofree:
  14541. return ret;
  14542. }
  14543. static void tg3_init_bufmgr_config(struct tg3 *tp)
  14544. {
  14545. if (tg3_flag(tp, 57765_PLUS)) {
  14546. tp->bufmgr_config.mbuf_read_dma_low_water =
  14547. DEFAULT_MB_RDMA_LOW_WATER_5705;
  14548. tp->bufmgr_config.mbuf_mac_rx_low_water =
  14549. DEFAULT_MB_MACRX_LOW_WATER_57765;
  14550. tp->bufmgr_config.mbuf_high_water =
  14551. DEFAULT_MB_HIGH_WATER_57765;
  14552. tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
  14553. DEFAULT_MB_RDMA_LOW_WATER_5705;
  14554. tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
  14555. DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
  14556. tp->bufmgr_config.mbuf_high_water_jumbo =
  14557. DEFAULT_MB_HIGH_WATER_JUMBO_57765;
  14558. } else if (tg3_flag(tp, 5705_PLUS)) {
  14559. tp->bufmgr_config.mbuf_read_dma_low_water =
  14560. DEFAULT_MB_RDMA_LOW_WATER_5705;
  14561. tp->bufmgr_config.mbuf_mac_rx_low_water =
  14562. DEFAULT_MB_MACRX_LOW_WATER_5705;
  14563. tp->bufmgr_config.mbuf_high_water =
  14564. DEFAULT_MB_HIGH_WATER_5705;
  14565. if (tg3_asic_rev(tp) == ASIC_REV_5906) {
  14566. tp->bufmgr_config.mbuf_mac_rx_low_water =
  14567. DEFAULT_MB_MACRX_LOW_WATER_5906;
  14568. tp->bufmgr_config.mbuf_high_water =
  14569. DEFAULT_MB_HIGH_WATER_5906;
  14570. }
  14571. tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
  14572. DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
  14573. tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
  14574. DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
  14575. tp->bufmgr_config.mbuf_high_water_jumbo =
  14576. DEFAULT_MB_HIGH_WATER_JUMBO_5780;
  14577. } else {
  14578. tp->bufmgr_config.mbuf_read_dma_low_water =
  14579. DEFAULT_MB_RDMA_LOW_WATER;
  14580. tp->bufmgr_config.mbuf_mac_rx_low_water =
  14581. DEFAULT_MB_MACRX_LOW_WATER;
  14582. tp->bufmgr_config.mbuf_high_water =
  14583. DEFAULT_MB_HIGH_WATER;
  14584. tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
  14585. DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
  14586. tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
  14587. DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
  14588. tp->bufmgr_config.mbuf_high_water_jumbo =
  14589. DEFAULT_MB_HIGH_WATER_JUMBO;
  14590. }
  14591. tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
  14592. tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
  14593. }
  14594. static char *tg3_phy_string(struct tg3 *tp)
  14595. {
  14596. switch (tp->phy_id & TG3_PHY_ID_MASK) {
  14597. case TG3_PHY_ID_BCM5400: return "5400";
  14598. case TG3_PHY_ID_BCM5401: return "5401";
  14599. case TG3_PHY_ID_BCM5411: return "5411";
  14600. case TG3_PHY_ID_BCM5701: return "5701";
  14601. case TG3_PHY_ID_BCM5703: return "5703";
  14602. case TG3_PHY_ID_BCM5704: return "5704";
  14603. case TG3_PHY_ID_BCM5705: return "5705";
  14604. case TG3_PHY_ID_BCM5750: return "5750";
  14605. case TG3_PHY_ID_BCM5752: return "5752";
  14606. case TG3_PHY_ID_BCM5714: return "5714";
  14607. case TG3_PHY_ID_BCM5780: return "5780";
  14608. case TG3_PHY_ID_BCM5755: return "5755";
  14609. case TG3_PHY_ID_BCM5787: return "5787";
  14610. case TG3_PHY_ID_BCM5784: return "5784";
  14611. case TG3_PHY_ID_BCM5756: return "5722/5756";
  14612. case TG3_PHY_ID_BCM5906: return "5906";
  14613. case TG3_PHY_ID_BCM5761: return "5761";
  14614. case TG3_PHY_ID_BCM5718C: return "5718C";
  14615. case TG3_PHY_ID_BCM5718S: return "5718S";
  14616. case TG3_PHY_ID_BCM57765: return "57765";
  14617. case TG3_PHY_ID_BCM5719C: return "5719C";
  14618. case TG3_PHY_ID_BCM5720C: return "5720C";
  14619. case TG3_PHY_ID_BCM5762: return "5762C";
  14620. case TG3_PHY_ID_BCM8002: return "8002/serdes";
  14621. case 0: return "serdes";
  14622. default: return "unknown";
  14623. }
  14624. }
  14625. static char *tg3_bus_string(struct tg3 *tp, char *str)
  14626. {
  14627. if (tg3_flag(tp, PCI_EXPRESS)) {
  14628. strcpy(str, "PCI Express");
  14629. return str;
  14630. } else if (tg3_flag(tp, PCIX_MODE)) {
  14631. u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
  14632. strcpy(str, "PCIX:");
  14633. if ((clock_ctrl == 7) ||
  14634. ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
  14635. GRC_MISC_CFG_BOARD_ID_5704CIOBE))
  14636. strcat(str, "133MHz");
  14637. else if (clock_ctrl == 0)
  14638. strcat(str, "33MHz");
  14639. else if (clock_ctrl == 2)
  14640. strcat(str, "50MHz");
  14641. else if (clock_ctrl == 4)
  14642. strcat(str, "66MHz");
  14643. else if (clock_ctrl == 6)
  14644. strcat(str, "100MHz");
  14645. } else {
  14646. strcpy(str, "PCI:");
  14647. if (tg3_flag(tp, PCI_HIGH_SPEED))
  14648. strcat(str, "66MHz");
  14649. else
  14650. strcat(str, "33MHz");
  14651. }
  14652. if (tg3_flag(tp, PCI_32BIT))
  14653. strcat(str, ":32-bit");
  14654. else
  14655. strcat(str, ":64-bit");
  14656. return str;
  14657. }
  14658. static void tg3_init_coal(struct tg3 *tp)
  14659. {
  14660. struct ethtool_coalesce *ec = &tp->coal;
  14661. memset(ec, 0, sizeof(*ec));
  14662. ec->cmd = ETHTOOL_GCOALESCE;
  14663. ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
  14664. ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
  14665. ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
  14666. ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
  14667. ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
  14668. ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
  14669. ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
  14670. ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
  14671. ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
  14672. if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
  14673. HOSTCC_MODE_CLRTICK_TXBD)) {
  14674. ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
  14675. ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
  14676. ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
  14677. ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
  14678. }
  14679. if (tg3_flag(tp, 5705_PLUS)) {
  14680. ec->rx_coalesce_usecs_irq = 0;
  14681. ec->tx_coalesce_usecs_irq = 0;
  14682. ec->stats_block_coalesce_usecs = 0;
  14683. }
  14684. }
  14685. static int tg3_init_one(struct pci_dev *pdev,
  14686. const struct pci_device_id *ent)
  14687. {
  14688. struct net_device *dev;
  14689. struct tg3 *tp;
  14690. int i, err;
  14691. u32 sndmbx, rcvmbx, intmbx;
  14692. char str[40];
  14693. u64 dma_mask, persist_dma_mask;
  14694. netdev_features_t features = 0;
  14695. printk_once(KERN_INFO "%s\n", version);
  14696. err = pci_enable_device(pdev);
  14697. if (err) {
  14698. dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
  14699. return err;
  14700. }
  14701. err = pci_request_regions(pdev, DRV_MODULE_NAME);
  14702. if (err) {
  14703. dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
  14704. goto err_out_disable_pdev;
  14705. }
  14706. pci_set_master(pdev);
  14707. dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
  14708. if (!dev) {
  14709. err = -ENOMEM;
  14710. goto err_out_free_res;
  14711. }
  14712. SET_NETDEV_DEV(dev, &pdev->dev);
  14713. tp = netdev_priv(dev);
  14714. tp->pdev = pdev;
  14715. tp->dev = dev;
  14716. tp->rx_mode = TG3_DEF_RX_MODE;
  14717. tp->tx_mode = TG3_DEF_TX_MODE;
  14718. tp->irq_sync = 1;
  14719. tp->pcierr_recovery = false;
  14720. if (tg3_debug > 0)
  14721. tp->msg_enable = tg3_debug;
  14722. else
  14723. tp->msg_enable = TG3_DEF_MSG_ENABLE;
  14724. if (pdev_is_ssb_gige_core(pdev)) {
  14725. tg3_flag_set(tp, IS_SSB_CORE);
  14726. if (ssb_gige_must_flush_posted_writes(pdev))
  14727. tg3_flag_set(tp, FLUSH_POSTED_WRITES);
  14728. if (ssb_gige_one_dma_at_once(pdev))
  14729. tg3_flag_set(tp, ONE_DMA_AT_ONCE);
  14730. if (ssb_gige_have_roboswitch(pdev)) {
  14731. tg3_flag_set(tp, USE_PHYLIB);
  14732. tg3_flag_set(tp, ROBOSWITCH);
  14733. }
  14734. if (ssb_gige_is_rgmii(pdev))
  14735. tg3_flag_set(tp, RGMII_MODE);
  14736. }
  14737. /* The word/byte swap controls here control register access byte
  14738. * swapping. DMA data byte swapping is controlled in the GRC_MODE
  14739. * setting below.
  14740. */
  14741. tp->misc_host_ctrl =
  14742. MISC_HOST_CTRL_MASK_PCI_INT |
  14743. MISC_HOST_CTRL_WORD_SWAP |
  14744. MISC_HOST_CTRL_INDIR_ACCESS |
  14745. MISC_HOST_CTRL_PCISTATE_RW;
  14746. /* The NONFRM (non-frame) byte/word swap controls take effect
  14747. * on descriptor entries, anything which isn't packet data.
  14748. *
  14749. * The StrongARM chips on the board (one for tx, one for rx)
  14750. * are running in big-endian mode.
  14751. */
  14752. tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
  14753. GRC_MODE_WSWAP_NONFRM_DATA);
  14754. #ifdef __BIG_ENDIAN
  14755. tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
  14756. #endif
  14757. spin_lock_init(&tp->lock);
  14758. spin_lock_init(&tp->indirect_lock);
  14759. INIT_WORK(&tp->reset_task, tg3_reset_task);
  14760. tp->regs = pci_ioremap_bar(pdev, BAR_0);
  14761. if (!tp->regs) {
  14762. dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
  14763. err = -ENOMEM;
  14764. goto err_out_free_dev;
  14765. }
  14766. if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
  14767. tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761E ||
  14768. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S ||
  14769. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761SE ||
  14770. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
  14771. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C ||
  14772. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
  14773. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
  14774. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720 ||
  14775. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57767 ||
  14776. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57764 ||
  14777. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5762 ||
  14778. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725 ||
  14779. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5727 ||
  14780. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57787) {
  14781. tg3_flag_set(tp, ENABLE_APE);
  14782. tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
  14783. if (!tp->aperegs) {
  14784. dev_err(&pdev->dev,
  14785. "Cannot map APE registers, aborting\n");
  14786. err = -ENOMEM;
  14787. goto err_out_iounmap;
  14788. }
  14789. }
  14790. tp->rx_pending = TG3_DEF_RX_RING_PENDING;
  14791. tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
  14792. dev->ethtool_ops = &tg3_ethtool_ops;
  14793. dev->watchdog_timeo = TG3_TX_TIMEOUT;
  14794. dev->netdev_ops = &tg3_netdev_ops;
  14795. dev->irq = pdev->irq;
  14796. err = tg3_get_invariants(tp, ent);
  14797. if (err) {
  14798. dev_err(&pdev->dev,
  14799. "Problem fetching invariants of chip, aborting\n");
  14800. goto err_out_apeunmap;
  14801. }
  14802. /* The EPB bridge inside 5714, 5715, and 5780 and any
  14803. * device behind the EPB cannot support DMA addresses > 40-bit.
  14804. * On 64-bit systems with IOMMU, use 40-bit dma_mask.
  14805. * On 64-bit systems without IOMMU, use 64-bit dma_mask and
  14806. * do DMA address check in tg3_start_xmit().
  14807. */
  14808. if (tg3_flag(tp, IS_5788))
  14809. persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
  14810. else if (tg3_flag(tp, 40BIT_DMA_BUG)) {
  14811. persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
  14812. #ifdef CONFIG_HIGHMEM
  14813. dma_mask = DMA_BIT_MASK(64);
  14814. #endif
  14815. } else
  14816. persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
  14817. /* Configure DMA attributes. */
  14818. if (dma_mask > DMA_BIT_MASK(32)) {
  14819. err = pci_set_dma_mask(pdev, dma_mask);
  14820. if (!err) {
  14821. features |= NETIF_F_HIGHDMA;
  14822. err = pci_set_consistent_dma_mask(pdev,
  14823. persist_dma_mask);
  14824. if (err < 0) {
  14825. dev_err(&pdev->dev, "Unable to obtain 64 bit "
  14826. "DMA for consistent allocations\n");
  14827. goto err_out_apeunmap;
  14828. }
  14829. }
  14830. }
  14831. if (err || dma_mask == DMA_BIT_MASK(32)) {
  14832. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  14833. if (err) {
  14834. dev_err(&pdev->dev,
  14835. "No usable DMA configuration, aborting\n");
  14836. goto err_out_apeunmap;
  14837. }
  14838. }
  14839. tg3_init_bufmgr_config(tp);
  14840. /* 5700 B0 chips do not support checksumming correctly due
  14841. * to hardware bugs.
  14842. */
  14843. if (tg3_chip_rev_id(tp) != CHIPREV_ID_5700_B0) {
  14844. features |= NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
  14845. if (tg3_flag(tp, 5755_PLUS))
  14846. features |= NETIF_F_IPV6_CSUM;
  14847. }
  14848. /* TSO is on by default on chips that support hardware TSO.
  14849. * Firmware TSO on older chips gives lower performance, so it
  14850. * is off by default, but can be enabled using ethtool.
  14851. */
  14852. if ((tg3_flag(tp, HW_TSO_1) ||
  14853. tg3_flag(tp, HW_TSO_2) ||
  14854. tg3_flag(tp, HW_TSO_3)) &&
  14855. (features & NETIF_F_IP_CSUM))
  14856. features |= NETIF_F_TSO;
  14857. if (tg3_flag(tp, HW_TSO_2) || tg3_flag(tp, HW_TSO_3)) {
  14858. if (features & NETIF_F_IPV6_CSUM)
  14859. features |= NETIF_F_TSO6;
  14860. if (tg3_flag(tp, HW_TSO_3) ||
  14861. tg3_asic_rev(tp) == ASIC_REV_5761 ||
  14862. (tg3_asic_rev(tp) == ASIC_REV_5784 &&
  14863. tg3_chip_rev(tp) != CHIPREV_5784_AX) ||
  14864. tg3_asic_rev(tp) == ASIC_REV_5785 ||
  14865. tg3_asic_rev(tp) == ASIC_REV_57780)
  14866. features |= NETIF_F_TSO_ECN;
  14867. }
  14868. dev->features |= features | NETIF_F_HW_VLAN_CTAG_TX |
  14869. NETIF_F_HW_VLAN_CTAG_RX;
  14870. dev->vlan_features |= features;
  14871. /*
  14872. * Add loopback capability only for a subset of devices that support
  14873. * MAC-LOOPBACK. Eventually this need to be enhanced to allow INT-PHY
  14874. * loopback for the remaining devices.
  14875. */
  14876. if (tg3_asic_rev(tp) != ASIC_REV_5780 &&
  14877. !tg3_flag(tp, CPMU_PRESENT))
  14878. /* Add the loopback capability */
  14879. features |= NETIF_F_LOOPBACK;
  14880. dev->hw_features |= features;
  14881. dev->priv_flags |= IFF_UNICAST_FLT;
  14882. /* MTU range: 60 - 9000 or 1500, depending on hardware */
  14883. dev->min_mtu = TG3_MIN_MTU;
  14884. dev->max_mtu = TG3_MAX_MTU(tp);
  14885. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A1 &&
  14886. !tg3_flag(tp, TSO_CAPABLE) &&
  14887. !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
  14888. tg3_flag_set(tp, MAX_RXPEND_64);
  14889. tp->rx_pending = 63;
  14890. }
  14891. err = tg3_get_device_address(tp);
  14892. if (err) {
  14893. dev_err(&pdev->dev,
  14894. "Could not obtain valid ethernet address, aborting\n");
  14895. goto err_out_apeunmap;
  14896. }
  14897. intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
  14898. rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
  14899. sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
  14900. for (i = 0; i < tp->irq_max; i++) {
  14901. struct tg3_napi *tnapi = &tp->napi[i];
  14902. tnapi->tp = tp;
  14903. tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
  14904. tnapi->int_mbox = intmbx;
  14905. if (i <= 4)
  14906. intmbx += 0x8;
  14907. else
  14908. intmbx += 0x4;
  14909. tnapi->consmbox = rcvmbx;
  14910. tnapi->prodmbox = sndmbx;
  14911. if (i)
  14912. tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
  14913. else
  14914. tnapi->coal_now = HOSTCC_MODE_NOW;
  14915. if (!tg3_flag(tp, SUPPORT_MSIX))
  14916. break;
  14917. /*
  14918. * If we support MSIX, we'll be using RSS. If we're using
  14919. * RSS, the first vector only handles link interrupts and the
  14920. * remaining vectors handle rx and tx interrupts. Reuse the
  14921. * mailbox values for the next iteration. The values we setup
  14922. * above are still useful for the single vectored mode.
  14923. */
  14924. if (!i)
  14925. continue;
  14926. rcvmbx += 0x8;
  14927. if (sndmbx & 0x4)
  14928. sndmbx -= 0x4;
  14929. else
  14930. sndmbx += 0xc;
  14931. }
  14932. /*
  14933. * Reset chip in case UNDI or EFI driver did not shutdown
  14934. * DMA self test will enable WDMAC and we'll see (spurious)
  14935. * pending DMA on the PCI bus at that point.
  14936. */
  14937. if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
  14938. (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
  14939. tg3_full_lock(tp, 0);
  14940. tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
  14941. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  14942. tg3_full_unlock(tp);
  14943. }
  14944. err = tg3_test_dma(tp);
  14945. if (err) {
  14946. dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
  14947. goto err_out_apeunmap;
  14948. }
  14949. tg3_init_coal(tp);
  14950. pci_set_drvdata(pdev, dev);
  14951. if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
  14952. tg3_asic_rev(tp) == ASIC_REV_5720 ||
  14953. tg3_asic_rev(tp) == ASIC_REV_5762)
  14954. tg3_flag_set(tp, PTP_CAPABLE);
  14955. tg3_timer_init(tp);
  14956. tg3_carrier_off(tp);
  14957. err = register_netdev(dev);
  14958. if (err) {
  14959. dev_err(&pdev->dev, "Cannot register net device, aborting\n");
  14960. goto err_out_apeunmap;
  14961. }
  14962. if (tg3_flag(tp, PTP_CAPABLE)) {
  14963. tg3_ptp_init(tp);
  14964. tp->ptp_clock = ptp_clock_register(&tp->ptp_info,
  14965. &tp->pdev->dev);
  14966. if (IS_ERR(tp->ptp_clock))
  14967. tp->ptp_clock = NULL;
  14968. }
  14969. netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
  14970. tp->board_part_number,
  14971. tg3_chip_rev_id(tp),
  14972. tg3_bus_string(tp, str),
  14973. dev->dev_addr);
  14974. if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)) {
  14975. char *ethtype;
  14976. if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
  14977. ethtype = "10/100Base-TX";
  14978. else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
  14979. ethtype = "1000Base-SX";
  14980. else
  14981. ethtype = "10/100/1000Base-T";
  14982. netdev_info(dev, "attached PHY is %s (%s Ethernet) "
  14983. "(WireSpeed[%d], EEE[%d])\n",
  14984. tg3_phy_string(tp), ethtype,
  14985. (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0,
  14986. (tp->phy_flags & TG3_PHYFLG_EEE_CAP) != 0);
  14987. }
  14988. netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
  14989. (dev->features & NETIF_F_RXCSUM) != 0,
  14990. tg3_flag(tp, USE_LINKCHG_REG) != 0,
  14991. (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
  14992. tg3_flag(tp, ENABLE_ASF) != 0,
  14993. tg3_flag(tp, TSO_CAPABLE) != 0);
  14994. netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
  14995. tp->dma_rwctrl,
  14996. pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
  14997. ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
  14998. pci_save_state(pdev);
  14999. return 0;
  15000. err_out_apeunmap:
  15001. if (tp->aperegs) {
  15002. iounmap(tp->aperegs);
  15003. tp->aperegs = NULL;
  15004. }
  15005. err_out_iounmap:
  15006. if (tp->regs) {
  15007. iounmap(tp->regs);
  15008. tp->regs = NULL;
  15009. }
  15010. err_out_free_dev:
  15011. free_netdev(dev);
  15012. err_out_free_res:
  15013. pci_release_regions(pdev);
  15014. err_out_disable_pdev:
  15015. if (pci_is_enabled(pdev))
  15016. pci_disable_device(pdev);
  15017. return err;
  15018. }
  15019. static void tg3_remove_one(struct pci_dev *pdev)
  15020. {
  15021. struct net_device *dev = pci_get_drvdata(pdev);
  15022. if (dev) {
  15023. struct tg3 *tp = netdev_priv(dev);
  15024. tg3_ptp_fini(tp);
  15025. release_firmware(tp->fw);
  15026. tg3_reset_task_cancel(tp);
  15027. if (tg3_flag(tp, USE_PHYLIB)) {
  15028. tg3_phy_fini(tp);
  15029. tg3_mdio_fini(tp);
  15030. }
  15031. unregister_netdev(dev);
  15032. if (tp->aperegs) {
  15033. iounmap(tp->aperegs);
  15034. tp->aperegs = NULL;
  15035. }
  15036. if (tp->regs) {
  15037. iounmap(tp->regs);
  15038. tp->regs = NULL;
  15039. }
  15040. free_netdev(dev);
  15041. pci_release_regions(pdev);
  15042. pci_disable_device(pdev);
  15043. }
  15044. }
  15045. #ifdef CONFIG_PM_SLEEP
  15046. static int tg3_suspend(struct device *device)
  15047. {
  15048. struct pci_dev *pdev = to_pci_dev(device);
  15049. struct net_device *dev = pci_get_drvdata(pdev);
  15050. struct tg3 *tp = netdev_priv(dev);
  15051. int err = 0;
  15052. rtnl_lock();
  15053. if (!netif_running(dev))
  15054. goto unlock;
  15055. tg3_reset_task_cancel(tp);
  15056. tg3_phy_stop(tp);
  15057. tg3_netif_stop(tp);
  15058. tg3_timer_stop(tp);
  15059. tg3_full_lock(tp, 1);
  15060. tg3_disable_ints(tp);
  15061. tg3_full_unlock(tp);
  15062. netif_device_detach(dev);
  15063. tg3_full_lock(tp, 0);
  15064. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  15065. tg3_flag_clear(tp, INIT_COMPLETE);
  15066. tg3_full_unlock(tp);
  15067. err = tg3_power_down_prepare(tp);
  15068. if (err) {
  15069. int err2;
  15070. tg3_full_lock(tp, 0);
  15071. tg3_flag_set(tp, INIT_COMPLETE);
  15072. err2 = tg3_restart_hw(tp, true);
  15073. if (err2)
  15074. goto out;
  15075. tg3_timer_start(tp);
  15076. netif_device_attach(dev);
  15077. tg3_netif_start(tp);
  15078. out:
  15079. tg3_full_unlock(tp);
  15080. if (!err2)
  15081. tg3_phy_start(tp);
  15082. }
  15083. unlock:
  15084. rtnl_unlock();
  15085. return err;
  15086. }
  15087. static int tg3_resume(struct device *device)
  15088. {
  15089. struct pci_dev *pdev = to_pci_dev(device);
  15090. struct net_device *dev = pci_get_drvdata(pdev);
  15091. struct tg3 *tp = netdev_priv(dev);
  15092. int err = 0;
  15093. rtnl_lock();
  15094. if (!netif_running(dev))
  15095. goto unlock;
  15096. netif_device_attach(dev);
  15097. tg3_full_lock(tp, 0);
  15098. tg3_ape_driver_state_change(tp, RESET_KIND_INIT);
  15099. tg3_flag_set(tp, INIT_COMPLETE);
  15100. err = tg3_restart_hw(tp,
  15101. !(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN));
  15102. if (err)
  15103. goto out;
  15104. tg3_timer_start(tp);
  15105. tg3_netif_start(tp);
  15106. out:
  15107. tg3_full_unlock(tp);
  15108. if (!err)
  15109. tg3_phy_start(tp);
  15110. unlock:
  15111. rtnl_unlock();
  15112. return err;
  15113. }
  15114. #endif /* CONFIG_PM_SLEEP */
  15115. static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
  15116. static void tg3_shutdown(struct pci_dev *pdev)
  15117. {
  15118. struct net_device *dev = pci_get_drvdata(pdev);
  15119. struct tg3 *tp = netdev_priv(dev);
  15120. rtnl_lock();
  15121. netif_device_detach(dev);
  15122. if (netif_running(dev))
  15123. dev_close(dev);
  15124. if (system_state == SYSTEM_POWER_OFF)
  15125. tg3_power_down(tp);
  15126. rtnl_unlock();
  15127. }
  15128. /**
  15129. * tg3_io_error_detected - called when PCI error is detected
  15130. * @pdev: Pointer to PCI device
  15131. * @state: The current pci connection state
  15132. *
  15133. * This function is called after a PCI bus error affecting
  15134. * this device has been detected.
  15135. */
  15136. static pci_ers_result_t tg3_io_error_detected(struct pci_dev *pdev,
  15137. pci_channel_state_t state)
  15138. {
  15139. struct net_device *netdev = pci_get_drvdata(pdev);
  15140. struct tg3 *tp = netdev_priv(netdev);
  15141. pci_ers_result_t err = PCI_ERS_RESULT_NEED_RESET;
  15142. netdev_info(netdev, "PCI I/O error detected\n");
  15143. rtnl_lock();
  15144. /* Could be second call or maybe we don't have netdev yet */
  15145. if (!netdev || tp->pcierr_recovery || !netif_running(netdev))
  15146. goto done;
  15147. /* We needn't recover from permanent error */
  15148. if (state == pci_channel_io_frozen)
  15149. tp->pcierr_recovery = true;
  15150. tg3_phy_stop(tp);
  15151. tg3_netif_stop(tp);
  15152. tg3_timer_stop(tp);
  15153. /* Want to make sure that the reset task doesn't run */
  15154. tg3_reset_task_cancel(tp);
  15155. netif_device_detach(netdev);
  15156. /* Clean up software state, even if MMIO is blocked */
  15157. tg3_full_lock(tp, 0);
  15158. tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
  15159. tg3_full_unlock(tp);
  15160. done:
  15161. if (state == pci_channel_io_perm_failure) {
  15162. if (netdev) {
  15163. tg3_napi_enable(tp);
  15164. dev_close(netdev);
  15165. }
  15166. err = PCI_ERS_RESULT_DISCONNECT;
  15167. } else {
  15168. pci_disable_device(pdev);
  15169. }
  15170. rtnl_unlock();
  15171. return err;
  15172. }
  15173. /**
  15174. * tg3_io_slot_reset - called after the pci bus has been reset.
  15175. * @pdev: Pointer to PCI device
  15176. *
  15177. * Restart the card from scratch, as if from a cold-boot.
  15178. * At this point, the card has exprienced a hard reset,
  15179. * followed by fixups by BIOS, and has its config space
  15180. * set up identically to what it was at cold boot.
  15181. */
  15182. static pci_ers_result_t tg3_io_slot_reset(struct pci_dev *pdev)
  15183. {
  15184. struct net_device *netdev = pci_get_drvdata(pdev);
  15185. struct tg3 *tp = netdev_priv(netdev);
  15186. pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
  15187. int err;
  15188. rtnl_lock();
  15189. if (pci_enable_device(pdev)) {
  15190. dev_err(&pdev->dev,
  15191. "Cannot re-enable PCI device after reset.\n");
  15192. goto done;
  15193. }
  15194. pci_set_master(pdev);
  15195. pci_restore_state(pdev);
  15196. pci_save_state(pdev);
  15197. if (!netdev || !netif_running(netdev)) {
  15198. rc = PCI_ERS_RESULT_RECOVERED;
  15199. goto done;
  15200. }
  15201. err = tg3_power_up(tp);
  15202. if (err)
  15203. goto done;
  15204. rc = PCI_ERS_RESULT_RECOVERED;
  15205. done:
  15206. if (rc != PCI_ERS_RESULT_RECOVERED && netdev && netif_running(netdev)) {
  15207. tg3_napi_enable(tp);
  15208. dev_close(netdev);
  15209. }
  15210. rtnl_unlock();
  15211. return rc;
  15212. }
  15213. /**
  15214. * tg3_io_resume - called when traffic can start flowing again.
  15215. * @pdev: Pointer to PCI device
  15216. *
  15217. * This callback is called when the error recovery driver tells
  15218. * us that its OK to resume normal operation.
  15219. */
  15220. static void tg3_io_resume(struct pci_dev *pdev)
  15221. {
  15222. struct net_device *netdev = pci_get_drvdata(pdev);
  15223. struct tg3 *tp = netdev_priv(netdev);
  15224. int err;
  15225. rtnl_lock();
  15226. if (!netdev || !netif_running(netdev))
  15227. goto done;
  15228. tg3_full_lock(tp, 0);
  15229. tg3_ape_driver_state_change(tp, RESET_KIND_INIT);
  15230. tg3_flag_set(tp, INIT_COMPLETE);
  15231. err = tg3_restart_hw(tp, true);
  15232. if (err) {
  15233. tg3_full_unlock(tp);
  15234. netdev_err(netdev, "Cannot restart hardware after reset.\n");
  15235. goto done;
  15236. }
  15237. netif_device_attach(netdev);
  15238. tg3_timer_start(tp);
  15239. tg3_netif_start(tp);
  15240. tg3_full_unlock(tp);
  15241. tg3_phy_start(tp);
  15242. done:
  15243. tp->pcierr_recovery = false;
  15244. rtnl_unlock();
  15245. }
  15246. static const struct pci_error_handlers tg3_err_handler = {
  15247. .error_detected = tg3_io_error_detected,
  15248. .slot_reset = tg3_io_slot_reset,
  15249. .resume = tg3_io_resume
  15250. };
  15251. static struct pci_driver tg3_driver = {
  15252. .name = DRV_MODULE_NAME,
  15253. .id_table = tg3_pci_tbl,
  15254. .probe = tg3_init_one,
  15255. .remove = tg3_remove_one,
  15256. .err_handler = &tg3_err_handler,
  15257. .driver.pm = &tg3_pm_ops,
  15258. .shutdown = tg3_shutdown,
  15259. };
  15260. module_pci_driver(tg3_driver);