tg3.c 469 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378
  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. cancel_work_sync(&tp->reset_task);
  5932. tg3_flag_clear(tp, RESET_TASK_PENDING);
  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. goto out;
  9233. tg3_netif_start(tp);
  9234. out:
  9235. tg3_full_unlock(tp);
  9236. if (!err)
  9237. tg3_phy_start(tp);
  9238. tg3_flag_clear(tp, RESET_TASK_PENDING);
  9239. rtnl_unlock();
  9240. }
  9241. static int tg3_request_irq(struct tg3 *tp, int irq_num)
  9242. {
  9243. irq_handler_t fn;
  9244. unsigned long flags;
  9245. char *name;
  9246. struct tg3_napi *tnapi = &tp->napi[irq_num];
  9247. if (tp->irq_cnt == 1)
  9248. name = tp->dev->name;
  9249. else {
  9250. name = &tnapi->irq_lbl[0];
  9251. if (tnapi->tx_buffers && tnapi->rx_rcb)
  9252. snprintf(name, IFNAMSIZ,
  9253. "%s-txrx-%d", tp->dev->name, irq_num);
  9254. else if (tnapi->tx_buffers)
  9255. snprintf(name, IFNAMSIZ,
  9256. "%s-tx-%d", tp->dev->name, irq_num);
  9257. else if (tnapi->rx_rcb)
  9258. snprintf(name, IFNAMSIZ,
  9259. "%s-rx-%d", tp->dev->name, irq_num);
  9260. else
  9261. snprintf(name, IFNAMSIZ,
  9262. "%s-%d", tp->dev->name, irq_num);
  9263. name[IFNAMSIZ-1] = 0;
  9264. }
  9265. if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
  9266. fn = tg3_msi;
  9267. if (tg3_flag(tp, 1SHOT_MSI))
  9268. fn = tg3_msi_1shot;
  9269. flags = 0;
  9270. } else {
  9271. fn = tg3_interrupt;
  9272. if (tg3_flag(tp, TAGGED_STATUS))
  9273. fn = tg3_interrupt_tagged;
  9274. flags = IRQF_SHARED;
  9275. }
  9276. return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
  9277. }
  9278. static int tg3_test_interrupt(struct tg3 *tp)
  9279. {
  9280. struct tg3_napi *tnapi = &tp->napi[0];
  9281. struct net_device *dev = tp->dev;
  9282. int err, i, intr_ok = 0;
  9283. u32 val;
  9284. if (!netif_running(dev))
  9285. return -ENODEV;
  9286. tg3_disable_ints(tp);
  9287. free_irq(tnapi->irq_vec, tnapi);
  9288. /*
  9289. * Turn off MSI one shot mode. Otherwise this test has no
  9290. * observable way to know whether the interrupt was delivered.
  9291. */
  9292. if (tg3_flag(tp, 57765_PLUS)) {
  9293. val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
  9294. tw32(MSGINT_MODE, val);
  9295. }
  9296. err = request_irq(tnapi->irq_vec, tg3_test_isr,
  9297. IRQF_SHARED, dev->name, tnapi);
  9298. if (err)
  9299. return err;
  9300. tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
  9301. tg3_enable_ints(tp);
  9302. tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
  9303. tnapi->coal_now);
  9304. for (i = 0; i < 5; i++) {
  9305. u32 int_mbox, misc_host_ctrl;
  9306. int_mbox = tr32_mailbox(tnapi->int_mbox);
  9307. misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
  9308. if ((int_mbox != 0) ||
  9309. (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
  9310. intr_ok = 1;
  9311. break;
  9312. }
  9313. if (tg3_flag(tp, 57765_PLUS) &&
  9314. tnapi->hw_status->status_tag != tnapi->last_tag)
  9315. tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
  9316. msleep(10);
  9317. }
  9318. tg3_disable_ints(tp);
  9319. free_irq(tnapi->irq_vec, tnapi);
  9320. err = tg3_request_irq(tp, 0);
  9321. if (err)
  9322. return err;
  9323. if (intr_ok) {
  9324. /* Reenable MSI one shot mode. */
  9325. if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, 1SHOT_MSI)) {
  9326. val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
  9327. tw32(MSGINT_MODE, val);
  9328. }
  9329. return 0;
  9330. }
  9331. return -EIO;
  9332. }
  9333. /* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
  9334. * successfully restored
  9335. */
  9336. static int tg3_test_msi(struct tg3 *tp)
  9337. {
  9338. int err;
  9339. u16 pci_cmd;
  9340. if (!tg3_flag(tp, USING_MSI))
  9341. return 0;
  9342. /* Turn off SERR reporting in case MSI terminates with Master
  9343. * Abort.
  9344. */
  9345. pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
  9346. pci_write_config_word(tp->pdev, PCI_COMMAND,
  9347. pci_cmd & ~PCI_COMMAND_SERR);
  9348. err = tg3_test_interrupt(tp);
  9349. pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
  9350. if (!err)
  9351. return 0;
  9352. /* other failures */
  9353. if (err != -EIO)
  9354. return err;
  9355. /* MSI test failed, go back to INTx mode */
  9356. netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
  9357. "to INTx mode. Please report this failure to the PCI "
  9358. "maintainer and include system chipset information\n");
  9359. free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
  9360. pci_disable_msi(tp->pdev);
  9361. tg3_flag_clear(tp, USING_MSI);
  9362. tp->napi[0].irq_vec = tp->pdev->irq;
  9363. err = tg3_request_irq(tp, 0);
  9364. if (err)
  9365. return err;
  9366. /* Need to reset the chip because the MSI cycle may have terminated
  9367. * with Master Abort.
  9368. */
  9369. tg3_full_lock(tp, 1);
  9370. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  9371. err = tg3_init_hw(tp, true);
  9372. tg3_full_unlock(tp);
  9373. if (err)
  9374. free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
  9375. return err;
  9376. }
  9377. static int tg3_request_firmware(struct tg3 *tp)
  9378. {
  9379. const struct tg3_firmware_hdr *fw_hdr;
  9380. if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev))
  9381. return -ENOENT;
  9382. fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
  9383. /* Firmware blob starts with version numbers, followed by
  9384. * start address and _full_ length including BSS sections
  9385. * (which must be longer than the actual data, of course
  9386. */
  9387. tp->fw_len = be32_to_cpu(fw_hdr->len); /* includes bss */
  9388. if (tp->fw_len < (tp->fw->size - TG3_FW_HDR_LEN)) {
  9389. netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
  9390. tp->fw_len, tp->fw_needed);
  9391. release_firmware(tp->fw);
  9392. tp->fw = NULL;
  9393. return -EINVAL;
  9394. }
  9395. /* We no longer need firmware; we have it. */
  9396. tp->fw_needed = NULL;
  9397. return 0;
  9398. }
  9399. static u32 tg3_irq_count(struct tg3 *tp)
  9400. {
  9401. u32 irq_cnt = max(tp->rxq_cnt, tp->txq_cnt);
  9402. if (irq_cnt > 1) {
  9403. /* We want as many rx rings enabled as there are cpus.
  9404. * In multiqueue MSI-X mode, the first MSI-X vector
  9405. * only deals with link interrupts, etc, so we add
  9406. * one to the number of vectors we are requesting.
  9407. */
  9408. irq_cnt = min_t(unsigned, irq_cnt + 1, tp->irq_max);
  9409. }
  9410. return irq_cnt;
  9411. }
  9412. static bool tg3_enable_msix(struct tg3 *tp)
  9413. {
  9414. int i, rc;
  9415. struct msix_entry msix_ent[TG3_IRQ_MAX_VECS];
  9416. tp->txq_cnt = tp->txq_req;
  9417. tp->rxq_cnt = tp->rxq_req;
  9418. if (!tp->rxq_cnt)
  9419. tp->rxq_cnt = netif_get_num_default_rss_queues();
  9420. if (tp->rxq_cnt > tp->rxq_max)
  9421. tp->rxq_cnt = tp->rxq_max;
  9422. /* Disable multiple TX rings by default. Simple round-robin hardware
  9423. * scheduling of the TX rings can cause starvation of rings with
  9424. * small packets when other rings have TSO or jumbo packets.
  9425. */
  9426. if (!tp->txq_req)
  9427. tp->txq_cnt = 1;
  9428. tp->irq_cnt = tg3_irq_count(tp);
  9429. for (i = 0; i < tp->irq_max; i++) {
  9430. msix_ent[i].entry = i;
  9431. msix_ent[i].vector = 0;
  9432. }
  9433. rc = pci_enable_msix_range(tp->pdev, msix_ent, 1, tp->irq_cnt);
  9434. if (rc < 0) {
  9435. return false;
  9436. } else if (rc < tp->irq_cnt) {
  9437. netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
  9438. tp->irq_cnt, rc);
  9439. tp->irq_cnt = rc;
  9440. tp->rxq_cnt = max(rc - 1, 1);
  9441. if (tp->txq_cnt)
  9442. tp->txq_cnt = min(tp->rxq_cnt, tp->txq_max);
  9443. }
  9444. for (i = 0; i < tp->irq_max; i++)
  9445. tp->napi[i].irq_vec = msix_ent[i].vector;
  9446. if (netif_set_real_num_rx_queues(tp->dev, tp->rxq_cnt)) {
  9447. pci_disable_msix(tp->pdev);
  9448. return false;
  9449. }
  9450. if (tp->irq_cnt == 1)
  9451. return true;
  9452. tg3_flag_set(tp, ENABLE_RSS);
  9453. if (tp->txq_cnt > 1)
  9454. tg3_flag_set(tp, ENABLE_TSS);
  9455. netif_set_real_num_tx_queues(tp->dev, tp->txq_cnt);
  9456. return true;
  9457. }
  9458. static void tg3_ints_init(struct tg3 *tp)
  9459. {
  9460. if ((tg3_flag(tp, SUPPORT_MSI) || tg3_flag(tp, SUPPORT_MSIX)) &&
  9461. !tg3_flag(tp, TAGGED_STATUS)) {
  9462. /* All MSI supporting chips should support tagged
  9463. * status. Assert that this is the case.
  9464. */
  9465. netdev_warn(tp->dev,
  9466. "MSI without TAGGED_STATUS? Not using MSI\n");
  9467. goto defcfg;
  9468. }
  9469. if (tg3_flag(tp, SUPPORT_MSIX) && tg3_enable_msix(tp))
  9470. tg3_flag_set(tp, USING_MSIX);
  9471. else if (tg3_flag(tp, SUPPORT_MSI) && pci_enable_msi(tp->pdev) == 0)
  9472. tg3_flag_set(tp, USING_MSI);
  9473. if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
  9474. u32 msi_mode = tr32(MSGINT_MODE);
  9475. if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1)
  9476. msi_mode |= MSGINT_MODE_MULTIVEC_EN;
  9477. if (!tg3_flag(tp, 1SHOT_MSI))
  9478. msi_mode |= MSGINT_MODE_ONE_SHOT_DISABLE;
  9479. tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
  9480. }
  9481. defcfg:
  9482. if (!tg3_flag(tp, USING_MSIX)) {
  9483. tp->irq_cnt = 1;
  9484. tp->napi[0].irq_vec = tp->pdev->irq;
  9485. }
  9486. if (tp->irq_cnt == 1) {
  9487. tp->txq_cnt = 1;
  9488. tp->rxq_cnt = 1;
  9489. netif_set_real_num_tx_queues(tp->dev, 1);
  9490. netif_set_real_num_rx_queues(tp->dev, 1);
  9491. }
  9492. }
  9493. static void tg3_ints_fini(struct tg3 *tp)
  9494. {
  9495. if (tg3_flag(tp, USING_MSIX))
  9496. pci_disable_msix(tp->pdev);
  9497. else if (tg3_flag(tp, USING_MSI))
  9498. pci_disable_msi(tp->pdev);
  9499. tg3_flag_clear(tp, USING_MSI);
  9500. tg3_flag_clear(tp, USING_MSIX);
  9501. tg3_flag_clear(tp, ENABLE_RSS);
  9502. tg3_flag_clear(tp, ENABLE_TSS);
  9503. }
  9504. static int tg3_start(struct tg3 *tp, bool reset_phy, bool test_irq,
  9505. bool init)
  9506. {
  9507. struct net_device *dev = tp->dev;
  9508. int i, err;
  9509. /*
  9510. * Setup interrupts first so we know how
  9511. * many NAPI resources to allocate
  9512. */
  9513. tg3_ints_init(tp);
  9514. tg3_rss_check_indir_tbl(tp);
  9515. /* The placement of this call is tied
  9516. * to the setup and use of Host TX descriptors.
  9517. */
  9518. err = tg3_alloc_consistent(tp);
  9519. if (err)
  9520. goto out_ints_fini;
  9521. tg3_napi_init(tp);
  9522. tg3_napi_enable(tp);
  9523. for (i = 0; i < tp->irq_cnt; i++) {
  9524. err = tg3_request_irq(tp, i);
  9525. if (err) {
  9526. for (i--; i >= 0; i--) {
  9527. struct tg3_napi *tnapi = &tp->napi[i];
  9528. free_irq(tnapi->irq_vec, tnapi);
  9529. }
  9530. goto out_napi_fini;
  9531. }
  9532. }
  9533. tg3_full_lock(tp, 0);
  9534. if (init)
  9535. tg3_ape_driver_state_change(tp, RESET_KIND_INIT);
  9536. err = tg3_init_hw(tp, reset_phy);
  9537. if (err) {
  9538. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  9539. tg3_free_rings(tp);
  9540. }
  9541. tg3_full_unlock(tp);
  9542. if (err)
  9543. goto out_free_irq;
  9544. if (test_irq && tg3_flag(tp, USING_MSI)) {
  9545. err = tg3_test_msi(tp);
  9546. if (err) {
  9547. tg3_full_lock(tp, 0);
  9548. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  9549. tg3_free_rings(tp);
  9550. tg3_full_unlock(tp);
  9551. goto out_napi_fini;
  9552. }
  9553. if (!tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
  9554. u32 val = tr32(PCIE_TRANSACTION_CFG);
  9555. tw32(PCIE_TRANSACTION_CFG,
  9556. val | PCIE_TRANS_CFG_1SHOT_MSI);
  9557. }
  9558. }
  9559. tg3_phy_start(tp);
  9560. tg3_hwmon_open(tp);
  9561. tg3_full_lock(tp, 0);
  9562. tg3_timer_start(tp);
  9563. tg3_flag_set(tp, INIT_COMPLETE);
  9564. tg3_enable_ints(tp);
  9565. tg3_ptp_resume(tp);
  9566. tg3_full_unlock(tp);
  9567. netif_tx_start_all_queues(dev);
  9568. /*
  9569. * Reset loopback feature if it was turned on while the device was down
  9570. * make sure that it's installed properly now.
  9571. */
  9572. if (dev->features & NETIF_F_LOOPBACK)
  9573. tg3_set_loopback(dev, dev->features);
  9574. return 0;
  9575. out_free_irq:
  9576. for (i = tp->irq_cnt - 1; i >= 0; i--) {
  9577. struct tg3_napi *tnapi = &tp->napi[i];
  9578. free_irq(tnapi->irq_vec, tnapi);
  9579. }
  9580. out_napi_fini:
  9581. tg3_napi_disable(tp);
  9582. tg3_napi_fini(tp);
  9583. tg3_free_consistent(tp);
  9584. out_ints_fini:
  9585. tg3_ints_fini(tp);
  9586. return err;
  9587. }
  9588. static void tg3_stop(struct tg3 *tp)
  9589. {
  9590. int i;
  9591. tg3_reset_task_cancel(tp);
  9592. tg3_netif_stop(tp);
  9593. tg3_timer_stop(tp);
  9594. tg3_hwmon_close(tp);
  9595. tg3_phy_stop(tp);
  9596. tg3_full_lock(tp, 1);
  9597. tg3_disable_ints(tp);
  9598. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  9599. tg3_free_rings(tp);
  9600. tg3_flag_clear(tp, INIT_COMPLETE);
  9601. tg3_full_unlock(tp);
  9602. for (i = tp->irq_cnt - 1; i >= 0; i--) {
  9603. struct tg3_napi *tnapi = &tp->napi[i];
  9604. free_irq(tnapi->irq_vec, tnapi);
  9605. }
  9606. tg3_ints_fini(tp);
  9607. tg3_napi_fini(tp);
  9608. tg3_free_consistent(tp);
  9609. }
  9610. static int tg3_open(struct net_device *dev)
  9611. {
  9612. struct tg3 *tp = netdev_priv(dev);
  9613. int err;
  9614. if (tp->pcierr_recovery) {
  9615. netdev_err(dev, "Failed to open device. PCI error recovery "
  9616. "in progress\n");
  9617. return -EAGAIN;
  9618. }
  9619. if (tp->fw_needed) {
  9620. err = tg3_request_firmware(tp);
  9621. if (tg3_asic_rev(tp) == ASIC_REV_57766) {
  9622. if (err) {
  9623. netdev_warn(tp->dev, "EEE capability disabled\n");
  9624. tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
  9625. } else if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP)) {
  9626. netdev_warn(tp->dev, "EEE capability restored\n");
  9627. tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
  9628. }
  9629. } else if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0) {
  9630. if (err)
  9631. return err;
  9632. } else if (err) {
  9633. netdev_warn(tp->dev, "TSO capability disabled\n");
  9634. tg3_flag_clear(tp, TSO_CAPABLE);
  9635. } else if (!tg3_flag(tp, TSO_CAPABLE)) {
  9636. netdev_notice(tp->dev, "TSO capability restored\n");
  9637. tg3_flag_set(tp, TSO_CAPABLE);
  9638. }
  9639. }
  9640. tg3_carrier_off(tp);
  9641. err = tg3_power_up(tp);
  9642. if (err)
  9643. return err;
  9644. tg3_full_lock(tp, 0);
  9645. tg3_disable_ints(tp);
  9646. tg3_flag_clear(tp, INIT_COMPLETE);
  9647. tg3_full_unlock(tp);
  9648. err = tg3_start(tp,
  9649. !(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN),
  9650. true, true);
  9651. if (err) {
  9652. tg3_frob_aux_power(tp, false);
  9653. pci_set_power_state(tp->pdev, PCI_D3hot);
  9654. }
  9655. return err;
  9656. }
  9657. static int tg3_close(struct net_device *dev)
  9658. {
  9659. struct tg3 *tp = netdev_priv(dev);
  9660. if (tp->pcierr_recovery) {
  9661. netdev_err(dev, "Failed to close device. PCI error recovery "
  9662. "in progress\n");
  9663. return -EAGAIN;
  9664. }
  9665. tg3_stop(tp);
  9666. if (pci_device_is_present(tp->pdev)) {
  9667. tg3_power_down_prepare(tp);
  9668. tg3_carrier_off(tp);
  9669. }
  9670. return 0;
  9671. }
  9672. static inline u64 get_stat64(tg3_stat64_t *val)
  9673. {
  9674. return ((u64)val->high << 32) | ((u64)val->low);
  9675. }
  9676. static u64 tg3_calc_crc_errors(struct tg3 *tp)
  9677. {
  9678. struct tg3_hw_stats *hw_stats = tp->hw_stats;
  9679. if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
  9680. (tg3_asic_rev(tp) == ASIC_REV_5700 ||
  9681. tg3_asic_rev(tp) == ASIC_REV_5701)) {
  9682. u32 val;
  9683. if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
  9684. tg3_writephy(tp, MII_TG3_TEST1,
  9685. val | MII_TG3_TEST1_CRC_EN);
  9686. tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
  9687. } else
  9688. val = 0;
  9689. tp->phy_crc_errors += val;
  9690. return tp->phy_crc_errors;
  9691. }
  9692. return get_stat64(&hw_stats->rx_fcs_errors);
  9693. }
  9694. #define ESTAT_ADD(member) \
  9695. estats->member = old_estats->member + \
  9696. get_stat64(&hw_stats->member)
  9697. static void tg3_get_estats(struct tg3 *tp, struct tg3_ethtool_stats *estats)
  9698. {
  9699. struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
  9700. struct tg3_hw_stats *hw_stats = tp->hw_stats;
  9701. ESTAT_ADD(rx_octets);
  9702. ESTAT_ADD(rx_fragments);
  9703. ESTAT_ADD(rx_ucast_packets);
  9704. ESTAT_ADD(rx_mcast_packets);
  9705. ESTAT_ADD(rx_bcast_packets);
  9706. ESTAT_ADD(rx_fcs_errors);
  9707. ESTAT_ADD(rx_align_errors);
  9708. ESTAT_ADD(rx_xon_pause_rcvd);
  9709. ESTAT_ADD(rx_xoff_pause_rcvd);
  9710. ESTAT_ADD(rx_mac_ctrl_rcvd);
  9711. ESTAT_ADD(rx_xoff_entered);
  9712. ESTAT_ADD(rx_frame_too_long_errors);
  9713. ESTAT_ADD(rx_jabbers);
  9714. ESTAT_ADD(rx_undersize_packets);
  9715. ESTAT_ADD(rx_in_length_errors);
  9716. ESTAT_ADD(rx_out_length_errors);
  9717. ESTAT_ADD(rx_64_or_less_octet_packets);
  9718. ESTAT_ADD(rx_65_to_127_octet_packets);
  9719. ESTAT_ADD(rx_128_to_255_octet_packets);
  9720. ESTAT_ADD(rx_256_to_511_octet_packets);
  9721. ESTAT_ADD(rx_512_to_1023_octet_packets);
  9722. ESTAT_ADD(rx_1024_to_1522_octet_packets);
  9723. ESTAT_ADD(rx_1523_to_2047_octet_packets);
  9724. ESTAT_ADD(rx_2048_to_4095_octet_packets);
  9725. ESTAT_ADD(rx_4096_to_8191_octet_packets);
  9726. ESTAT_ADD(rx_8192_to_9022_octet_packets);
  9727. ESTAT_ADD(tx_octets);
  9728. ESTAT_ADD(tx_collisions);
  9729. ESTAT_ADD(tx_xon_sent);
  9730. ESTAT_ADD(tx_xoff_sent);
  9731. ESTAT_ADD(tx_flow_control);
  9732. ESTAT_ADD(tx_mac_errors);
  9733. ESTAT_ADD(tx_single_collisions);
  9734. ESTAT_ADD(tx_mult_collisions);
  9735. ESTAT_ADD(tx_deferred);
  9736. ESTAT_ADD(tx_excessive_collisions);
  9737. ESTAT_ADD(tx_late_collisions);
  9738. ESTAT_ADD(tx_collide_2times);
  9739. ESTAT_ADD(tx_collide_3times);
  9740. ESTAT_ADD(tx_collide_4times);
  9741. ESTAT_ADD(tx_collide_5times);
  9742. ESTAT_ADD(tx_collide_6times);
  9743. ESTAT_ADD(tx_collide_7times);
  9744. ESTAT_ADD(tx_collide_8times);
  9745. ESTAT_ADD(tx_collide_9times);
  9746. ESTAT_ADD(tx_collide_10times);
  9747. ESTAT_ADD(tx_collide_11times);
  9748. ESTAT_ADD(tx_collide_12times);
  9749. ESTAT_ADD(tx_collide_13times);
  9750. ESTAT_ADD(tx_collide_14times);
  9751. ESTAT_ADD(tx_collide_15times);
  9752. ESTAT_ADD(tx_ucast_packets);
  9753. ESTAT_ADD(tx_mcast_packets);
  9754. ESTAT_ADD(tx_bcast_packets);
  9755. ESTAT_ADD(tx_carrier_sense_errors);
  9756. ESTAT_ADD(tx_discards);
  9757. ESTAT_ADD(tx_errors);
  9758. ESTAT_ADD(dma_writeq_full);
  9759. ESTAT_ADD(dma_write_prioq_full);
  9760. ESTAT_ADD(rxbds_empty);
  9761. ESTAT_ADD(rx_discards);
  9762. ESTAT_ADD(rx_errors);
  9763. ESTAT_ADD(rx_threshold_hit);
  9764. ESTAT_ADD(dma_readq_full);
  9765. ESTAT_ADD(dma_read_prioq_full);
  9766. ESTAT_ADD(tx_comp_queue_full);
  9767. ESTAT_ADD(ring_set_send_prod_index);
  9768. ESTAT_ADD(ring_status_update);
  9769. ESTAT_ADD(nic_irqs);
  9770. ESTAT_ADD(nic_avoided_irqs);
  9771. ESTAT_ADD(nic_tx_threshold_hit);
  9772. ESTAT_ADD(mbuf_lwm_thresh_hit);
  9773. }
  9774. static void tg3_get_nstats(struct tg3 *tp, struct rtnl_link_stats64 *stats)
  9775. {
  9776. struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
  9777. struct tg3_hw_stats *hw_stats = tp->hw_stats;
  9778. stats->rx_packets = old_stats->rx_packets +
  9779. get_stat64(&hw_stats->rx_ucast_packets) +
  9780. get_stat64(&hw_stats->rx_mcast_packets) +
  9781. get_stat64(&hw_stats->rx_bcast_packets);
  9782. stats->tx_packets = old_stats->tx_packets +
  9783. get_stat64(&hw_stats->tx_ucast_packets) +
  9784. get_stat64(&hw_stats->tx_mcast_packets) +
  9785. get_stat64(&hw_stats->tx_bcast_packets);
  9786. stats->rx_bytes = old_stats->rx_bytes +
  9787. get_stat64(&hw_stats->rx_octets);
  9788. stats->tx_bytes = old_stats->tx_bytes +
  9789. get_stat64(&hw_stats->tx_octets);
  9790. stats->rx_errors = old_stats->rx_errors +
  9791. get_stat64(&hw_stats->rx_errors);
  9792. stats->tx_errors = old_stats->tx_errors +
  9793. get_stat64(&hw_stats->tx_errors) +
  9794. get_stat64(&hw_stats->tx_mac_errors) +
  9795. get_stat64(&hw_stats->tx_carrier_sense_errors) +
  9796. get_stat64(&hw_stats->tx_discards);
  9797. stats->multicast = old_stats->multicast +
  9798. get_stat64(&hw_stats->rx_mcast_packets);
  9799. stats->collisions = old_stats->collisions +
  9800. get_stat64(&hw_stats->tx_collisions);
  9801. stats->rx_length_errors = old_stats->rx_length_errors +
  9802. get_stat64(&hw_stats->rx_frame_too_long_errors) +
  9803. get_stat64(&hw_stats->rx_undersize_packets);
  9804. stats->rx_frame_errors = old_stats->rx_frame_errors +
  9805. get_stat64(&hw_stats->rx_align_errors);
  9806. stats->tx_aborted_errors = old_stats->tx_aborted_errors +
  9807. get_stat64(&hw_stats->tx_discards);
  9808. stats->tx_carrier_errors = old_stats->tx_carrier_errors +
  9809. get_stat64(&hw_stats->tx_carrier_sense_errors);
  9810. stats->rx_crc_errors = old_stats->rx_crc_errors +
  9811. tg3_calc_crc_errors(tp);
  9812. stats->rx_missed_errors = old_stats->rx_missed_errors +
  9813. get_stat64(&hw_stats->rx_discards);
  9814. stats->rx_dropped = tp->rx_dropped;
  9815. stats->tx_dropped = tp->tx_dropped;
  9816. }
  9817. static int tg3_get_regs_len(struct net_device *dev)
  9818. {
  9819. return TG3_REG_BLK_SIZE;
  9820. }
  9821. static void tg3_get_regs(struct net_device *dev,
  9822. struct ethtool_regs *regs, void *_p)
  9823. {
  9824. struct tg3 *tp = netdev_priv(dev);
  9825. regs->version = 0;
  9826. memset(_p, 0, TG3_REG_BLK_SIZE);
  9827. if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
  9828. return;
  9829. tg3_full_lock(tp, 0);
  9830. tg3_dump_legacy_regs(tp, (u32 *)_p);
  9831. tg3_full_unlock(tp);
  9832. }
  9833. static int tg3_get_eeprom_len(struct net_device *dev)
  9834. {
  9835. struct tg3 *tp = netdev_priv(dev);
  9836. return tp->nvram_size;
  9837. }
  9838. static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
  9839. {
  9840. struct tg3 *tp = netdev_priv(dev);
  9841. int ret, cpmu_restore = 0;
  9842. u8 *pd;
  9843. u32 i, offset, len, b_offset, b_count, cpmu_val = 0;
  9844. __be32 val;
  9845. if (tg3_flag(tp, NO_NVRAM))
  9846. return -EINVAL;
  9847. offset = eeprom->offset;
  9848. len = eeprom->len;
  9849. eeprom->len = 0;
  9850. eeprom->magic = TG3_EEPROM_MAGIC;
  9851. /* Override clock, link aware and link idle modes */
  9852. if (tg3_flag(tp, CPMU_PRESENT)) {
  9853. cpmu_val = tr32(TG3_CPMU_CTRL);
  9854. if (cpmu_val & (CPMU_CTRL_LINK_AWARE_MODE |
  9855. CPMU_CTRL_LINK_IDLE_MODE)) {
  9856. tw32(TG3_CPMU_CTRL, cpmu_val &
  9857. ~(CPMU_CTRL_LINK_AWARE_MODE |
  9858. CPMU_CTRL_LINK_IDLE_MODE));
  9859. cpmu_restore = 1;
  9860. }
  9861. }
  9862. tg3_override_clk(tp);
  9863. if (offset & 3) {
  9864. /* adjustments to start on required 4 byte boundary */
  9865. b_offset = offset & 3;
  9866. b_count = 4 - b_offset;
  9867. if (b_count > len) {
  9868. /* i.e. offset=1 len=2 */
  9869. b_count = len;
  9870. }
  9871. ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
  9872. if (ret)
  9873. goto eeprom_done;
  9874. memcpy(data, ((char *)&val) + b_offset, b_count);
  9875. len -= b_count;
  9876. offset += b_count;
  9877. eeprom->len += b_count;
  9878. }
  9879. /* read bytes up to the last 4 byte boundary */
  9880. pd = &data[eeprom->len];
  9881. for (i = 0; i < (len - (len & 3)); i += 4) {
  9882. ret = tg3_nvram_read_be32(tp, offset + i, &val);
  9883. if (ret) {
  9884. if (i)
  9885. i -= 4;
  9886. eeprom->len += i;
  9887. goto eeprom_done;
  9888. }
  9889. memcpy(pd + i, &val, 4);
  9890. if (need_resched()) {
  9891. if (signal_pending(current)) {
  9892. eeprom->len += i;
  9893. ret = -EINTR;
  9894. goto eeprom_done;
  9895. }
  9896. cond_resched();
  9897. }
  9898. }
  9899. eeprom->len += i;
  9900. if (len & 3) {
  9901. /* read last bytes not ending on 4 byte boundary */
  9902. pd = &data[eeprom->len];
  9903. b_count = len & 3;
  9904. b_offset = offset + len - b_count;
  9905. ret = tg3_nvram_read_be32(tp, b_offset, &val);
  9906. if (ret)
  9907. goto eeprom_done;
  9908. memcpy(pd, &val, b_count);
  9909. eeprom->len += b_count;
  9910. }
  9911. ret = 0;
  9912. eeprom_done:
  9913. /* Restore clock, link aware and link idle modes */
  9914. tg3_restore_clk(tp);
  9915. if (cpmu_restore)
  9916. tw32(TG3_CPMU_CTRL, cpmu_val);
  9917. return ret;
  9918. }
  9919. static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
  9920. {
  9921. struct tg3 *tp = netdev_priv(dev);
  9922. int ret;
  9923. u32 offset, len, b_offset, odd_len;
  9924. u8 *buf;
  9925. __be32 start = 0, end;
  9926. if (tg3_flag(tp, NO_NVRAM) ||
  9927. eeprom->magic != TG3_EEPROM_MAGIC)
  9928. return -EINVAL;
  9929. offset = eeprom->offset;
  9930. len = eeprom->len;
  9931. if ((b_offset = (offset & 3))) {
  9932. /* adjustments to start on required 4 byte boundary */
  9933. ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
  9934. if (ret)
  9935. return ret;
  9936. len += b_offset;
  9937. offset &= ~3;
  9938. if (len < 4)
  9939. len = 4;
  9940. }
  9941. odd_len = 0;
  9942. if (len & 3) {
  9943. /* adjustments to end on required 4 byte boundary */
  9944. odd_len = 1;
  9945. len = (len + 3) & ~3;
  9946. ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
  9947. if (ret)
  9948. return ret;
  9949. }
  9950. buf = data;
  9951. if (b_offset || odd_len) {
  9952. buf = kmalloc(len, GFP_KERNEL);
  9953. if (!buf)
  9954. return -ENOMEM;
  9955. if (b_offset)
  9956. memcpy(buf, &start, 4);
  9957. if (odd_len)
  9958. memcpy(buf+len-4, &end, 4);
  9959. memcpy(buf + b_offset, data, eeprom->len);
  9960. }
  9961. ret = tg3_nvram_write_block(tp, offset, len, buf);
  9962. if (buf != data)
  9963. kfree(buf);
  9964. return ret;
  9965. }
  9966. static int tg3_get_link_ksettings(struct net_device *dev,
  9967. struct ethtool_link_ksettings *cmd)
  9968. {
  9969. struct tg3 *tp = netdev_priv(dev);
  9970. u32 supported, advertising;
  9971. if (tg3_flag(tp, USE_PHYLIB)) {
  9972. struct phy_device *phydev;
  9973. if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
  9974. return -EAGAIN;
  9975. phydev = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr);
  9976. phy_ethtool_ksettings_get(phydev, cmd);
  9977. return 0;
  9978. }
  9979. supported = (SUPPORTED_Autoneg);
  9980. if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
  9981. supported |= (SUPPORTED_1000baseT_Half |
  9982. SUPPORTED_1000baseT_Full);
  9983. if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
  9984. supported |= (SUPPORTED_100baseT_Half |
  9985. SUPPORTED_100baseT_Full |
  9986. SUPPORTED_10baseT_Half |
  9987. SUPPORTED_10baseT_Full |
  9988. SUPPORTED_TP);
  9989. cmd->base.port = PORT_TP;
  9990. } else {
  9991. supported |= SUPPORTED_FIBRE;
  9992. cmd->base.port = PORT_FIBRE;
  9993. }
  9994. ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
  9995. supported);
  9996. advertising = tp->link_config.advertising;
  9997. if (tg3_flag(tp, PAUSE_AUTONEG)) {
  9998. if (tp->link_config.flowctrl & FLOW_CTRL_RX) {
  9999. if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
  10000. advertising |= ADVERTISED_Pause;
  10001. } else {
  10002. advertising |= ADVERTISED_Pause |
  10003. ADVERTISED_Asym_Pause;
  10004. }
  10005. } else if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
  10006. advertising |= ADVERTISED_Asym_Pause;
  10007. }
  10008. }
  10009. ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
  10010. advertising);
  10011. if (netif_running(dev) && tp->link_up) {
  10012. cmd->base.speed = tp->link_config.active_speed;
  10013. cmd->base.duplex = tp->link_config.active_duplex;
  10014. ethtool_convert_legacy_u32_to_link_mode(
  10015. cmd->link_modes.lp_advertising,
  10016. tp->link_config.rmt_adv);
  10017. if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
  10018. if (tp->phy_flags & TG3_PHYFLG_MDIX_STATE)
  10019. cmd->base.eth_tp_mdix = ETH_TP_MDI_X;
  10020. else
  10021. cmd->base.eth_tp_mdix = ETH_TP_MDI;
  10022. }
  10023. } else {
  10024. cmd->base.speed = SPEED_UNKNOWN;
  10025. cmd->base.duplex = DUPLEX_UNKNOWN;
  10026. cmd->base.eth_tp_mdix = ETH_TP_MDI_INVALID;
  10027. }
  10028. cmd->base.phy_address = tp->phy_addr;
  10029. cmd->base.autoneg = tp->link_config.autoneg;
  10030. return 0;
  10031. }
  10032. static int tg3_set_link_ksettings(struct net_device *dev,
  10033. const struct ethtool_link_ksettings *cmd)
  10034. {
  10035. struct tg3 *tp = netdev_priv(dev);
  10036. u32 speed = cmd->base.speed;
  10037. u32 advertising;
  10038. if (tg3_flag(tp, USE_PHYLIB)) {
  10039. struct phy_device *phydev;
  10040. if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
  10041. return -EAGAIN;
  10042. phydev = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr);
  10043. return phy_ethtool_ksettings_set(phydev, cmd);
  10044. }
  10045. if (cmd->base.autoneg != AUTONEG_ENABLE &&
  10046. cmd->base.autoneg != AUTONEG_DISABLE)
  10047. return -EINVAL;
  10048. if (cmd->base.autoneg == AUTONEG_DISABLE &&
  10049. cmd->base.duplex != DUPLEX_FULL &&
  10050. cmd->base.duplex != DUPLEX_HALF)
  10051. return -EINVAL;
  10052. ethtool_convert_link_mode_to_legacy_u32(&advertising,
  10053. cmd->link_modes.advertising);
  10054. if (cmd->base.autoneg == AUTONEG_ENABLE) {
  10055. u32 mask = ADVERTISED_Autoneg |
  10056. ADVERTISED_Pause |
  10057. ADVERTISED_Asym_Pause;
  10058. if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
  10059. mask |= ADVERTISED_1000baseT_Half |
  10060. ADVERTISED_1000baseT_Full;
  10061. if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
  10062. mask |= ADVERTISED_100baseT_Half |
  10063. ADVERTISED_100baseT_Full |
  10064. ADVERTISED_10baseT_Half |
  10065. ADVERTISED_10baseT_Full |
  10066. ADVERTISED_TP;
  10067. else
  10068. mask |= ADVERTISED_FIBRE;
  10069. if (advertising & ~mask)
  10070. return -EINVAL;
  10071. mask &= (ADVERTISED_1000baseT_Half |
  10072. ADVERTISED_1000baseT_Full |
  10073. ADVERTISED_100baseT_Half |
  10074. ADVERTISED_100baseT_Full |
  10075. ADVERTISED_10baseT_Half |
  10076. ADVERTISED_10baseT_Full);
  10077. advertising &= mask;
  10078. } else {
  10079. if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
  10080. if (speed != SPEED_1000)
  10081. return -EINVAL;
  10082. if (cmd->base.duplex != DUPLEX_FULL)
  10083. return -EINVAL;
  10084. } else {
  10085. if (speed != SPEED_100 &&
  10086. speed != SPEED_10)
  10087. return -EINVAL;
  10088. }
  10089. }
  10090. tg3_full_lock(tp, 0);
  10091. tp->link_config.autoneg = cmd->base.autoneg;
  10092. if (cmd->base.autoneg == AUTONEG_ENABLE) {
  10093. tp->link_config.advertising = (advertising |
  10094. ADVERTISED_Autoneg);
  10095. tp->link_config.speed = SPEED_UNKNOWN;
  10096. tp->link_config.duplex = DUPLEX_UNKNOWN;
  10097. } else {
  10098. tp->link_config.advertising = 0;
  10099. tp->link_config.speed = speed;
  10100. tp->link_config.duplex = cmd->base.duplex;
  10101. }
  10102. tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
  10103. tg3_warn_mgmt_link_flap(tp);
  10104. if (netif_running(dev))
  10105. tg3_setup_phy(tp, true);
  10106. tg3_full_unlock(tp);
  10107. return 0;
  10108. }
  10109. static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  10110. {
  10111. struct tg3 *tp = netdev_priv(dev);
  10112. strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
  10113. strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
  10114. strlcpy(info->fw_version, tp->fw_ver, sizeof(info->fw_version));
  10115. strlcpy(info->bus_info, pci_name(tp->pdev), sizeof(info->bus_info));
  10116. }
  10117. static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  10118. {
  10119. struct tg3 *tp = netdev_priv(dev);
  10120. if (tg3_flag(tp, WOL_CAP) && device_can_wakeup(&tp->pdev->dev))
  10121. wol->supported = WAKE_MAGIC;
  10122. else
  10123. wol->supported = 0;
  10124. wol->wolopts = 0;
  10125. if (tg3_flag(tp, WOL_ENABLE) && device_can_wakeup(&tp->pdev->dev))
  10126. wol->wolopts = WAKE_MAGIC;
  10127. memset(&wol->sopass, 0, sizeof(wol->sopass));
  10128. }
  10129. static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  10130. {
  10131. struct tg3 *tp = netdev_priv(dev);
  10132. struct device *dp = &tp->pdev->dev;
  10133. if (wol->wolopts & ~WAKE_MAGIC)
  10134. return -EINVAL;
  10135. if ((wol->wolopts & WAKE_MAGIC) &&
  10136. !(tg3_flag(tp, WOL_CAP) && device_can_wakeup(dp)))
  10137. return -EINVAL;
  10138. device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
  10139. if (device_may_wakeup(dp))
  10140. tg3_flag_set(tp, WOL_ENABLE);
  10141. else
  10142. tg3_flag_clear(tp, WOL_ENABLE);
  10143. return 0;
  10144. }
  10145. static u32 tg3_get_msglevel(struct net_device *dev)
  10146. {
  10147. struct tg3 *tp = netdev_priv(dev);
  10148. return tp->msg_enable;
  10149. }
  10150. static void tg3_set_msglevel(struct net_device *dev, u32 value)
  10151. {
  10152. struct tg3 *tp = netdev_priv(dev);
  10153. tp->msg_enable = value;
  10154. }
  10155. static int tg3_nway_reset(struct net_device *dev)
  10156. {
  10157. struct tg3 *tp = netdev_priv(dev);
  10158. int r;
  10159. if (!netif_running(dev))
  10160. return -EAGAIN;
  10161. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
  10162. return -EINVAL;
  10163. tg3_warn_mgmt_link_flap(tp);
  10164. if (tg3_flag(tp, USE_PHYLIB)) {
  10165. if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
  10166. return -EAGAIN;
  10167. r = phy_start_aneg(mdiobus_get_phy(tp->mdio_bus, tp->phy_addr));
  10168. } else {
  10169. u32 bmcr;
  10170. spin_lock_bh(&tp->lock);
  10171. r = -EINVAL;
  10172. tg3_readphy(tp, MII_BMCR, &bmcr);
  10173. if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
  10174. ((bmcr & BMCR_ANENABLE) ||
  10175. (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
  10176. tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
  10177. BMCR_ANENABLE);
  10178. r = 0;
  10179. }
  10180. spin_unlock_bh(&tp->lock);
  10181. }
  10182. return r;
  10183. }
  10184. static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
  10185. {
  10186. struct tg3 *tp = netdev_priv(dev);
  10187. ering->rx_max_pending = tp->rx_std_ring_mask;
  10188. if (tg3_flag(tp, JUMBO_RING_ENABLE))
  10189. ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
  10190. else
  10191. ering->rx_jumbo_max_pending = 0;
  10192. ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
  10193. ering->rx_pending = tp->rx_pending;
  10194. if (tg3_flag(tp, JUMBO_RING_ENABLE))
  10195. ering->rx_jumbo_pending = tp->rx_jumbo_pending;
  10196. else
  10197. ering->rx_jumbo_pending = 0;
  10198. ering->tx_pending = tp->napi[0].tx_pending;
  10199. }
  10200. static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
  10201. {
  10202. struct tg3 *tp = netdev_priv(dev);
  10203. int i, irq_sync = 0, err = 0;
  10204. bool reset_phy = false;
  10205. if ((ering->rx_pending > tp->rx_std_ring_mask) ||
  10206. (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
  10207. (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
  10208. (ering->tx_pending <= MAX_SKB_FRAGS) ||
  10209. (tg3_flag(tp, TSO_BUG) &&
  10210. (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
  10211. return -EINVAL;
  10212. if (netif_running(dev)) {
  10213. tg3_phy_stop(tp);
  10214. tg3_netif_stop(tp);
  10215. irq_sync = 1;
  10216. }
  10217. tg3_full_lock(tp, irq_sync);
  10218. tp->rx_pending = ering->rx_pending;
  10219. if (tg3_flag(tp, MAX_RXPEND_64) &&
  10220. tp->rx_pending > 63)
  10221. tp->rx_pending = 63;
  10222. if (tg3_flag(tp, JUMBO_RING_ENABLE))
  10223. tp->rx_jumbo_pending = ering->rx_jumbo_pending;
  10224. for (i = 0; i < tp->irq_max; i++)
  10225. tp->napi[i].tx_pending = ering->tx_pending;
  10226. if (netif_running(dev)) {
  10227. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  10228. /* Reset PHY to avoid PHY lock up */
  10229. if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
  10230. tg3_asic_rev(tp) == ASIC_REV_5719 ||
  10231. tg3_asic_rev(tp) == ASIC_REV_5720)
  10232. reset_phy = true;
  10233. err = tg3_restart_hw(tp, reset_phy);
  10234. if (!err)
  10235. tg3_netif_start(tp);
  10236. }
  10237. tg3_full_unlock(tp);
  10238. if (irq_sync && !err)
  10239. tg3_phy_start(tp);
  10240. return err;
  10241. }
  10242. static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
  10243. {
  10244. struct tg3 *tp = netdev_priv(dev);
  10245. epause->autoneg = !!tg3_flag(tp, PAUSE_AUTONEG);
  10246. if (tp->link_config.flowctrl & FLOW_CTRL_RX)
  10247. epause->rx_pause = 1;
  10248. else
  10249. epause->rx_pause = 0;
  10250. if (tp->link_config.flowctrl & FLOW_CTRL_TX)
  10251. epause->tx_pause = 1;
  10252. else
  10253. epause->tx_pause = 0;
  10254. }
  10255. static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
  10256. {
  10257. struct tg3 *tp = netdev_priv(dev);
  10258. int err = 0;
  10259. bool reset_phy = false;
  10260. if (tp->link_config.autoneg == AUTONEG_ENABLE)
  10261. tg3_warn_mgmt_link_flap(tp);
  10262. if (tg3_flag(tp, USE_PHYLIB)) {
  10263. u32 newadv;
  10264. struct phy_device *phydev;
  10265. phydev = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr);
  10266. if (!(phydev->supported & SUPPORTED_Pause) ||
  10267. (!(phydev->supported & SUPPORTED_Asym_Pause) &&
  10268. (epause->rx_pause != epause->tx_pause)))
  10269. return -EINVAL;
  10270. tp->link_config.flowctrl = 0;
  10271. if (epause->rx_pause) {
  10272. tp->link_config.flowctrl |= FLOW_CTRL_RX;
  10273. if (epause->tx_pause) {
  10274. tp->link_config.flowctrl |= FLOW_CTRL_TX;
  10275. newadv = ADVERTISED_Pause;
  10276. } else
  10277. newadv = ADVERTISED_Pause |
  10278. ADVERTISED_Asym_Pause;
  10279. } else if (epause->tx_pause) {
  10280. tp->link_config.flowctrl |= FLOW_CTRL_TX;
  10281. newadv = ADVERTISED_Asym_Pause;
  10282. } else
  10283. newadv = 0;
  10284. if (epause->autoneg)
  10285. tg3_flag_set(tp, PAUSE_AUTONEG);
  10286. else
  10287. tg3_flag_clear(tp, PAUSE_AUTONEG);
  10288. if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
  10289. u32 oldadv = phydev->advertising &
  10290. (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
  10291. if (oldadv != newadv) {
  10292. phydev->advertising &=
  10293. ~(ADVERTISED_Pause |
  10294. ADVERTISED_Asym_Pause);
  10295. phydev->advertising |= newadv;
  10296. if (phydev->autoneg) {
  10297. /*
  10298. * Always renegotiate the link to
  10299. * inform our link partner of our
  10300. * flow control settings, even if the
  10301. * flow control is forced. Let
  10302. * tg3_adjust_link() do the final
  10303. * flow control setup.
  10304. */
  10305. return phy_start_aneg(phydev);
  10306. }
  10307. }
  10308. if (!epause->autoneg)
  10309. tg3_setup_flow_control(tp, 0, 0);
  10310. } else {
  10311. tp->link_config.advertising &=
  10312. ~(ADVERTISED_Pause |
  10313. ADVERTISED_Asym_Pause);
  10314. tp->link_config.advertising |= newadv;
  10315. }
  10316. } else {
  10317. int irq_sync = 0;
  10318. if (netif_running(dev)) {
  10319. tg3_netif_stop(tp);
  10320. irq_sync = 1;
  10321. }
  10322. tg3_full_lock(tp, irq_sync);
  10323. if (epause->autoneg)
  10324. tg3_flag_set(tp, PAUSE_AUTONEG);
  10325. else
  10326. tg3_flag_clear(tp, PAUSE_AUTONEG);
  10327. if (epause->rx_pause)
  10328. tp->link_config.flowctrl |= FLOW_CTRL_RX;
  10329. else
  10330. tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
  10331. if (epause->tx_pause)
  10332. tp->link_config.flowctrl |= FLOW_CTRL_TX;
  10333. else
  10334. tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
  10335. if (netif_running(dev)) {
  10336. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  10337. /* Reset PHY to avoid PHY lock up */
  10338. if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
  10339. tg3_asic_rev(tp) == ASIC_REV_5719 ||
  10340. tg3_asic_rev(tp) == ASIC_REV_5720)
  10341. reset_phy = true;
  10342. err = tg3_restart_hw(tp, reset_phy);
  10343. if (!err)
  10344. tg3_netif_start(tp);
  10345. }
  10346. tg3_full_unlock(tp);
  10347. }
  10348. tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
  10349. return err;
  10350. }
  10351. static int tg3_get_sset_count(struct net_device *dev, int sset)
  10352. {
  10353. switch (sset) {
  10354. case ETH_SS_TEST:
  10355. return TG3_NUM_TEST;
  10356. case ETH_SS_STATS:
  10357. return TG3_NUM_STATS;
  10358. default:
  10359. return -EOPNOTSUPP;
  10360. }
  10361. }
  10362. static int tg3_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
  10363. u32 *rules __always_unused)
  10364. {
  10365. struct tg3 *tp = netdev_priv(dev);
  10366. if (!tg3_flag(tp, SUPPORT_MSIX))
  10367. return -EOPNOTSUPP;
  10368. switch (info->cmd) {
  10369. case ETHTOOL_GRXRINGS:
  10370. if (netif_running(tp->dev))
  10371. info->data = tp->rxq_cnt;
  10372. else {
  10373. info->data = num_online_cpus();
  10374. if (info->data > TG3_RSS_MAX_NUM_QS)
  10375. info->data = TG3_RSS_MAX_NUM_QS;
  10376. }
  10377. return 0;
  10378. default:
  10379. return -EOPNOTSUPP;
  10380. }
  10381. }
  10382. static u32 tg3_get_rxfh_indir_size(struct net_device *dev)
  10383. {
  10384. u32 size = 0;
  10385. struct tg3 *tp = netdev_priv(dev);
  10386. if (tg3_flag(tp, SUPPORT_MSIX))
  10387. size = TG3_RSS_INDIR_TBL_SIZE;
  10388. return size;
  10389. }
  10390. static int tg3_get_rxfh(struct net_device *dev, u32 *indir, u8 *key, u8 *hfunc)
  10391. {
  10392. struct tg3 *tp = netdev_priv(dev);
  10393. int i;
  10394. if (hfunc)
  10395. *hfunc = ETH_RSS_HASH_TOP;
  10396. if (!indir)
  10397. return 0;
  10398. for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
  10399. indir[i] = tp->rss_ind_tbl[i];
  10400. return 0;
  10401. }
  10402. static int tg3_set_rxfh(struct net_device *dev, const u32 *indir, const u8 *key,
  10403. const u8 hfunc)
  10404. {
  10405. struct tg3 *tp = netdev_priv(dev);
  10406. size_t i;
  10407. /* We require at least one supported parameter to be changed and no
  10408. * change in any of the unsupported parameters
  10409. */
  10410. if (key ||
  10411. (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP))
  10412. return -EOPNOTSUPP;
  10413. if (!indir)
  10414. return 0;
  10415. for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
  10416. tp->rss_ind_tbl[i] = indir[i];
  10417. if (!netif_running(dev) || !tg3_flag(tp, ENABLE_RSS))
  10418. return 0;
  10419. /* It is legal to write the indirection
  10420. * table while the device is running.
  10421. */
  10422. tg3_full_lock(tp, 0);
  10423. tg3_rss_write_indir_tbl(tp);
  10424. tg3_full_unlock(tp);
  10425. return 0;
  10426. }
  10427. static void tg3_get_channels(struct net_device *dev,
  10428. struct ethtool_channels *channel)
  10429. {
  10430. struct tg3 *tp = netdev_priv(dev);
  10431. u32 deflt_qs = netif_get_num_default_rss_queues();
  10432. channel->max_rx = tp->rxq_max;
  10433. channel->max_tx = tp->txq_max;
  10434. if (netif_running(dev)) {
  10435. channel->rx_count = tp->rxq_cnt;
  10436. channel->tx_count = tp->txq_cnt;
  10437. } else {
  10438. if (tp->rxq_req)
  10439. channel->rx_count = tp->rxq_req;
  10440. else
  10441. channel->rx_count = min(deflt_qs, tp->rxq_max);
  10442. if (tp->txq_req)
  10443. channel->tx_count = tp->txq_req;
  10444. else
  10445. channel->tx_count = min(deflt_qs, tp->txq_max);
  10446. }
  10447. }
  10448. static int tg3_set_channels(struct net_device *dev,
  10449. struct ethtool_channels *channel)
  10450. {
  10451. struct tg3 *tp = netdev_priv(dev);
  10452. if (!tg3_flag(tp, SUPPORT_MSIX))
  10453. return -EOPNOTSUPP;
  10454. if (channel->rx_count > tp->rxq_max ||
  10455. channel->tx_count > tp->txq_max)
  10456. return -EINVAL;
  10457. tp->rxq_req = channel->rx_count;
  10458. tp->txq_req = channel->tx_count;
  10459. if (!netif_running(dev))
  10460. return 0;
  10461. tg3_stop(tp);
  10462. tg3_carrier_off(tp);
  10463. tg3_start(tp, true, false, false);
  10464. return 0;
  10465. }
  10466. static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
  10467. {
  10468. switch (stringset) {
  10469. case ETH_SS_STATS:
  10470. memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
  10471. break;
  10472. case ETH_SS_TEST:
  10473. memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
  10474. break;
  10475. default:
  10476. WARN_ON(1); /* we need a WARN() */
  10477. break;
  10478. }
  10479. }
  10480. static int tg3_set_phys_id(struct net_device *dev,
  10481. enum ethtool_phys_id_state state)
  10482. {
  10483. struct tg3 *tp = netdev_priv(dev);
  10484. if (!netif_running(tp->dev))
  10485. return -EAGAIN;
  10486. switch (state) {
  10487. case ETHTOOL_ID_ACTIVE:
  10488. return 1; /* cycle on/off once per second */
  10489. case ETHTOOL_ID_ON:
  10490. tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
  10491. LED_CTRL_1000MBPS_ON |
  10492. LED_CTRL_100MBPS_ON |
  10493. LED_CTRL_10MBPS_ON |
  10494. LED_CTRL_TRAFFIC_OVERRIDE |
  10495. LED_CTRL_TRAFFIC_BLINK |
  10496. LED_CTRL_TRAFFIC_LED);
  10497. break;
  10498. case ETHTOOL_ID_OFF:
  10499. tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
  10500. LED_CTRL_TRAFFIC_OVERRIDE);
  10501. break;
  10502. case ETHTOOL_ID_INACTIVE:
  10503. tw32(MAC_LED_CTRL, tp->led_ctrl);
  10504. break;
  10505. }
  10506. return 0;
  10507. }
  10508. static void tg3_get_ethtool_stats(struct net_device *dev,
  10509. struct ethtool_stats *estats, u64 *tmp_stats)
  10510. {
  10511. struct tg3 *tp = netdev_priv(dev);
  10512. if (tp->hw_stats)
  10513. tg3_get_estats(tp, (struct tg3_ethtool_stats *)tmp_stats);
  10514. else
  10515. memset(tmp_stats, 0, sizeof(struct tg3_ethtool_stats));
  10516. }
  10517. static __be32 *tg3_vpd_readblock(struct tg3 *tp, u32 *vpdlen)
  10518. {
  10519. int i;
  10520. __be32 *buf;
  10521. u32 offset = 0, len = 0;
  10522. u32 magic, val;
  10523. if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &magic))
  10524. return NULL;
  10525. if (magic == TG3_EEPROM_MAGIC) {
  10526. for (offset = TG3_NVM_DIR_START;
  10527. offset < TG3_NVM_DIR_END;
  10528. offset += TG3_NVM_DIRENT_SIZE) {
  10529. if (tg3_nvram_read(tp, offset, &val))
  10530. return NULL;
  10531. if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
  10532. TG3_NVM_DIRTYPE_EXTVPD)
  10533. break;
  10534. }
  10535. if (offset != TG3_NVM_DIR_END) {
  10536. len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
  10537. if (tg3_nvram_read(tp, offset + 4, &offset))
  10538. return NULL;
  10539. offset = tg3_nvram_logical_addr(tp, offset);
  10540. }
  10541. }
  10542. if (!offset || !len) {
  10543. offset = TG3_NVM_VPD_OFF;
  10544. len = TG3_NVM_VPD_LEN;
  10545. }
  10546. buf = kmalloc(len, GFP_KERNEL);
  10547. if (buf == NULL)
  10548. return NULL;
  10549. if (magic == TG3_EEPROM_MAGIC) {
  10550. for (i = 0; i < len; i += 4) {
  10551. /* The data is in little-endian format in NVRAM.
  10552. * Use the big-endian read routines to preserve
  10553. * the byte order as it exists in NVRAM.
  10554. */
  10555. if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
  10556. goto error;
  10557. }
  10558. } else {
  10559. u8 *ptr;
  10560. ssize_t cnt;
  10561. unsigned int pos = 0;
  10562. ptr = (u8 *)&buf[0];
  10563. for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
  10564. cnt = pci_read_vpd(tp->pdev, pos,
  10565. len - pos, ptr);
  10566. if (cnt == -ETIMEDOUT || cnt == -EINTR)
  10567. cnt = 0;
  10568. else if (cnt < 0)
  10569. goto error;
  10570. }
  10571. if (pos != len)
  10572. goto error;
  10573. }
  10574. *vpdlen = len;
  10575. return buf;
  10576. error:
  10577. kfree(buf);
  10578. return NULL;
  10579. }
  10580. #define NVRAM_TEST_SIZE 0x100
  10581. #define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
  10582. #define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
  10583. #define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
  10584. #define NVRAM_SELFBOOT_FORMAT1_4_SIZE 0x20
  10585. #define NVRAM_SELFBOOT_FORMAT1_5_SIZE 0x24
  10586. #define NVRAM_SELFBOOT_FORMAT1_6_SIZE 0x50
  10587. #define NVRAM_SELFBOOT_HW_SIZE 0x20
  10588. #define NVRAM_SELFBOOT_DATA_SIZE 0x1c
  10589. static int tg3_test_nvram(struct tg3 *tp)
  10590. {
  10591. u32 csum, magic, len;
  10592. __be32 *buf;
  10593. int i, j, k, err = 0, size;
  10594. if (tg3_flag(tp, NO_NVRAM))
  10595. return 0;
  10596. if (tg3_nvram_read(tp, 0, &magic) != 0)
  10597. return -EIO;
  10598. if (magic == TG3_EEPROM_MAGIC)
  10599. size = NVRAM_TEST_SIZE;
  10600. else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
  10601. if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
  10602. TG3_EEPROM_SB_FORMAT_1) {
  10603. switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
  10604. case TG3_EEPROM_SB_REVISION_0:
  10605. size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
  10606. break;
  10607. case TG3_EEPROM_SB_REVISION_2:
  10608. size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
  10609. break;
  10610. case TG3_EEPROM_SB_REVISION_3:
  10611. size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
  10612. break;
  10613. case TG3_EEPROM_SB_REVISION_4:
  10614. size = NVRAM_SELFBOOT_FORMAT1_4_SIZE;
  10615. break;
  10616. case TG3_EEPROM_SB_REVISION_5:
  10617. size = NVRAM_SELFBOOT_FORMAT1_5_SIZE;
  10618. break;
  10619. case TG3_EEPROM_SB_REVISION_6:
  10620. size = NVRAM_SELFBOOT_FORMAT1_6_SIZE;
  10621. break;
  10622. default:
  10623. return -EIO;
  10624. }
  10625. } else
  10626. return 0;
  10627. } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
  10628. size = NVRAM_SELFBOOT_HW_SIZE;
  10629. else
  10630. return -EIO;
  10631. buf = kmalloc(size, GFP_KERNEL);
  10632. if (buf == NULL)
  10633. return -ENOMEM;
  10634. err = -EIO;
  10635. for (i = 0, j = 0; i < size; i += 4, j++) {
  10636. err = tg3_nvram_read_be32(tp, i, &buf[j]);
  10637. if (err)
  10638. break;
  10639. }
  10640. if (i < size)
  10641. goto out;
  10642. /* Selfboot format */
  10643. magic = be32_to_cpu(buf[0]);
  10644. if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
  10645. TG3_EEPROM_MAGIC_FW) {
  10646. u8 *buf8 = (u8 *) buf, csum8 = 0;
  10647. if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
  10648. TG3_EEPROM_SB_REVISION_2) {
  10649. /* For rev 2, the csum doesn't include the MBA. */
  10650. for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
  10651. csum8 += buf8[i];
  10652. for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
  10653. csum8 += buf8[i];
  10654. } else {
  10655. for (i = 0; i < size; i++)
  10656. csum8 += buf8[i];
  10657. }
  10658. if (csum8 == 0) {
  10659. err = 0;
  10660. goto out;
  10661. }
  10662. err = -EIO;
  10663. goto out;
  10664. }
  10665. if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
  10666. TG3_EEPROM_MAGIC_HW) {
  10667. u8 data[NVRAM_SELFBOOT_DATA_SIZE];
  10668. u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
  10669. u8 *buf8 = (u8 *) buf;
  10670. /* Separate the parity bits and the data bytes. */
  10671. for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
  10672. if ((i == 0) || (i == 8)) {
  10673. int l;
  10674. u8 msk;
  10675. for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
  10676. parity[k++] = buf8[i] & msk;
  10677. i++;
  10678. } else if (i == 16) {
  10679. int l;
  10680. u8 msk;
  10681. for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
  10682. parity[k++] = buf8[i] & msk;
  10683. i++;
  10684. for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
  10685. parity[k++] = buf8[i] & msk;
  10686. i++;
  10687. }
  10688. data[j++] = buf8[i];
  10689. }
  10690. err = -EIO;
  10691. for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
  10692. u8 hw8 = hweight8(data[i]);
  10693. if ((hw8 & 0x1) && parity[i])
  10694. goto out;
  10695. else if (!(hw8 & 0x1) && !parity[i])
  10696. goto out;
  10697. }
  10698. err = 0;
  10699. goto out;
  10700. }
  10701. err = -EIO;
  10702. /* Bootstrap checksum at offset 0x10 */
  10703. csum = calc_crc((unsigned char *) buf, 0x10);
  10704. if (csum != le32_to_cpu(buf[0x10/4]))
  10705. goto out;
  10706. /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
  10707. csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
  10708. if (csum != le32_to_cpu(buf[0xfc/4]))
  10709. goto out;
  10710. kfree(buf);
  10711. buf = tg3_vpd_readblock(tp, &len);
  10712. if (!buf)
  10713. return -ENOMEM;
  10714. i = pci_vpd_find_tag((u8 *)buf, 0, len, PCI_VPD_LRDT_RO_DATA);
  10715. if (i > 0) {
  10716. j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
  10717. if (j < 0)
  10718. goto out;
  10719. if (i + PCI_VPD_LRDT_TAG_SIZE + j > len)
  10720. goto out;
  10721. i += PCI_VPD_LRDT_TAG_SIZE;
  10722. j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
  10723. PCI_VPD_RO_KEYWORD_CHKSUM);
  10724. if (j > 0) {
  10725. u8 csum8 = 0;
  10726. j += PCI_VPD_INFO_FLD_HDR_SIZE;
  10727. for (i = 0; i <= j; i++)
  10728. csum8 += ((u8 *)buf)[i];
  10729. if (csum8)
  10730. goto out;
  10731. }
  10732. }
  10733. err = 0;
  10734. out:
  10735. kfree(buf);
  10736. return err;
  10737. }
  10738. #define TG3_SERDES_TIMEOUT_SEC 2
  10739. #define TG3_COPPER_TIMEOUT_SEC 6
  10740. static int tg3_test_link(struct tg3 *tp)
  10741. {
  10742. int i, max;
  10743. if (!netif_running(tp->dev))
  10744. return -ENODEV;
  10745. if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
  10746. max = TG3_SERDES_TIMEOUT_SEC;
  10747. else
  10748. max = TG3_COPPER_TIMEOUT_SEC;
  10749. for (i = 0; i < max; i++) {
  10750. if (tp->link_up)
  10751. return 0;
  10752. if (msleep_interruptible(1000))
  10753. break;
  10754. }
  10755. return -EIO;
  10756. }
  10757. /* Only test the commonly used registers */
  10758. static int tg3_test_registers(struct tg3 *tp)
  10759. {
  10760. int i, is_5705, is_5750;
  10761. u32 offset, read_mask, write_mask, val, save_val, read_val;
  10762. static struct {
  10763. u16 offset;
  10764. u16 flags;
  10765. #define TG3_FL_5705 0x1
  10766. #define TG3_FL_NOT_5705 0x2
  10767. #define TG3_FL_NOT_5788 0x4
  10768. #define TG3_FL_NOT_5750 0x8
  10769. u32 read_mask;
  10770. u32 write_mask;
  10771. } reg_tbl[] = {
  10772. /* MAC Control Registers */
  10773. { MAC_MODE, TG3_FL_NOT_5705,
  10774. 0x00000000, 0x00ef6f8c },
  10775. { MAC_MODE, TG3_FL_5705,
  10776. 0x00000000, 0x01ef6b8c },
  10777. { MAC_STATUS, TG3_FL_NOT_5705,
  10778. 0x03800107, 0x00000000 },
  10779. { MAC_STATUS, TG3_FL_5705,
  10780. 0x03800100, 0x00000000 },
  10781. { MAC_ADDR_0_HIGH, 0x0000,
  10782. 0x00000000, 0x0000ffff },
  10783. { MAC_ADDR_0_LOW, 0x0000,
  10784. 0x00000000, 0xffffffff },
  10785. { MAC_RX_MTU_SIZE, 0x0000,
  10786. 0x00000000, 0x0000ffff },
  10787. { MAC_TX_MODE, 0x0000,
  10788. 0x00000000, 0x00000070 },
  10789. { MAC_TX_LENGTHS, 0x0000,
  10790. 0x00000000, 0x00003fff },
  10791. { MAC_RX_MODE, TG3_FL_NOT_5705,
  10792. 0x00000000, 0x000007fc },
  10793. { MAC_RX_MODE, TG3_FL_5705,
  10794. 0x00000000, 0x000007dc },
  10795. { MAC_HASH_REG_0, 0x0000,
  10796. 0x00000000, 0xffffffff },
  10797. { MAC_HASH_REG_1, 0x0000,
  10798. 0x00000000, 0xffffffff },
  10799. { MAC_HASH_REG_2, 0x0000,
  10800. 0x00000000, 0xffffffff },
  10801. { MAC_HASH_REG_3, 0x0000,
  10802. 0x00000000, 0xffffffff },
  10803. /* Receive Data and Receive BD Initiator Control Registers. */
  10804. { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
  10805. 0x00000000, 0xffffffff },
  10806. { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
  10807. 0x00000000, 0xffffffff },
  10808. { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
  10809. 0x00000000, 0x00000003 },
  10810. { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
  10811. 0x00000000, 0xffffffff },
  10812. { RCVDBDI_STD_BD+0, 0x0000,
  10813. 0x00000000, 0xffffffff },
  10814. { RCVDBDI_STD_BD+4, 0x0000,
  10815. 0x00000000, 0xffffffff },
  10816. { RCVDBDI_STD_BD+8, 0x0000,
  10817. 0x00000000, 0xffff0002 },
  10818. { RCVDBDI_STD_BD+0xc, 0x0000,
  10819. 0x00000000, 0xffffffff },
  10820. /* Receive BD Initiator Control Registers. */
  10821. { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
  10822. 0x00000000, 0xffffffff },
  10823. { RCVBDI_STD_THRESH, TG3_FL_5705,
  10824. 0x00000000, 0x000003ff },
  10825. { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
  10826. 0x00000000, 0xffffffff },
  10827. /* Host Coalescing Control Registers. */
  10828. { HOSTCC_MODE, TG3_FL_NOT_5705,
  10829. 0x00000000, 0x00000004 },
  10830. { HOSTCC_MODE, TG3_FL_5705,
  10831. 0x00000000, 0x000000f6 },
  10832. { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
  10833. 0x00000000, 0xffffffff },
  10834. { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
  10835. 0x00000000, 0x000003ff },
  10836. { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
  10837. 0x00000000, 0xffffffff },
  10838. { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
  10839. 0x00000000, 0x000003ff },
  10840. { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
  10841. 0x00000000, 0xffffffff },
  10842. { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
  10843. 0x00000000, 0x000000ff },
  10844. { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
  10845. 0x00000000, 0xffffffff },
  10846. { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
  10847. 0x00000000, 0x000000ff },
  10848. { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
  10849. 0x00000000, 0xffffffff },
  10850. { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
  10851. 0x00000000, 0xffffffff },
  10852. { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
  10853. 0x00000000, 0xffffffff },
  10854. { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
  10855. 0x00000000, 0x000000ff },
  10856. { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
  10857. 0x00000000, 0xffffffff },
  10858. { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
  10859. 0x00000000, 0x000000ff },
  10860. { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
  10861. 0x00000000, 0xffffffff },
  10862. { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
  10863. 0x00000000, 0xffffffff },
  10864. { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
  10865. 0x00000000, 0xffffffff },
  10866. { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
  10867. 0x00000000, 0xffffffff },
  10868. { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
  10869. 0x00000000, 0xffffffff },
  10870. { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
  10871. 0xffffffff, 0x00000000 },
  10872. { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
  10873. 0xffffffff, 0x00000000 },
  10874. /* Buffer Manager Control Registers. */
  10875. { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
  10876. 0x00000000, 0x007fff80 },
  10877. { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
  10878. 0x00000000, 0x007fffff },
  10879. { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
  10880. 0x00000000, 0x0000003f },
  10881. { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
  10882. 0x00000000, 0x000001ff },
  10883. { BUFMGR_MB_HIGH_WATER, 0x0000,
  10884. 0x00000000, 0x000001ff },
  10885. { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
  10886. 0xffffffff, 0x00000000 },
  10887. { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
  10888. 0xffffffff, 0x00000000 },
  10889. /* Mailbox Registers */
  10890. { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
  10891. 0x00000000, 0x000001ff },
  10892. { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
  10893. 0x00000000, 0x000001ff },
  10894. { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
  10895. 0x00000000, 0x000007ff },
  10896. { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
  10897. 0x00000000, 0x000001ff },
  10898. { 0xffff, 0x0000, 0x00000000, 0x00000000 },
  10899. };
  10900. is_5705 = is_5750 = 0;
  10901. if (tg3_flag(tp, 5705_PLUS)) {
  10902. is_5705 = 1;
  10903. if (tg3_flag(tp, 5750_PLUS))
  10904. is_5750 = 1;
  10905. }
  10906. for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
  10907. if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
  10908. continue;
  10909. if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
  10910. continue;
  10911. if (tg3_flag(tp, IS_5788) &&
  10912. (reg_tbl[i].flags & TG3_FL_NOT_5788))
  10913. continue;
  10914. if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
  10915. continue;
  10916. offset = (u32) reg_tbl[i].offset;
  10917. read_mask = reg_tbl[i].read_mask;
  10918. write_mask = reg_tbl[i].write_mask;
  10919. /* Save the original register content */
  10920. save_val = tr32(offset);
  10921. /* Determine the read-only value. */
  10922. read_val = save_val & read_mask;
  10923. /* Write zero to the register, then make sure the read-only bits
  10924. * are not changed and the read/write bits are all zeros.
  10925. */
  10926. tw32(offset, 0);
  10927. val = tr32(offset);
  10928. /* Test the read-only and read/write bits. */
  10929. if (((val & read_mask) != read_val) || (val & write_mask))
  10930. goto out;
  10931. /* Write ones to all the bits defined by RdMask and WrMask, then
  10932. * make sure the read-only bits are not changed and the
  10933. * read/write bits are all ones.
  10934. */
  10935. tw32(offset, read_mask | write_mask);
  10936. val = tr32(offset);
  10937. /* Test the read-only bits. */
  10938. if ((val & read_mask) != read_val)
  10939. goto out;
  10940. /* Test the read/write bits. */
  10941. if ((val & write_mask) != write_mask)
  10942. goto out;
  10943. tw32(offset, save_val);
  10944. }
  10945. return 0;
  10946. out:
  10947. if (netif_msg_hw(tp))
  10948. netdev_err(tp->dev,
  10949. "Register test failed at offset %x\n", offset);
  10950. tw32(offset, save_val);
  10951. return -EIO;
  10952. }
  10953. static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
  10954. {
  10955. static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
  10956. int i;
  10957. u32 j;
  10958. for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
  10959. for (j = 0; j < len; j += 4) {
  10960. u32 val;
  10961. tg3_write_mem(tp, offset + j, test_pattern[i]);
  10962. tg3_read_mem(tp, offset + j, &val);
  10963. if (val != test_pattern[i])
  10964. return -EIO;
  10965. }
  10966. }
  10967. return 0;
  10968. }
  10969. static int tg3_test_memory(struct tg3 *tp)
  10970. {
  10971. static struct mem_entry {
  10972. u32 offset;
  10973. u32 len;
  10974. } mem_tbl_570x[] = {
  10975. { 0x00000000, 0x00b50},
  10976. { 0x00002000, 0x1c000},
  10977. { 0xffffffff, 0x00000}
  10978. }, mem_tbl_5705[] = {
  10979. { 0x00000100, 0x0000c},
  10980. { 0x00000200, 0x00008},
  10981. { 0x00004000, 0x00800},
  10982. { 0x00006000, 0x01000},
  10983. { 0x00008000, 0x02000},
  10984. { 0x00010000, 0x0e000},
  10985. { 0xffffffff, 0x00000}
  10986. }, mem_tbl_5755[] = {
  10987. { 0x00000200, 0x00008},
  10988. { 0x00004000, 0x00800},
  10989. { 0x00006000, 0x00800},
  10990. { 0x00008000, 0x02000},
  10991. { 0x00010000, 0x0c000},
  10992. { 0xffffffff, 0x00000}
  10993. }, mem_tbl_5906[] = {
  10994. { 0x00000200, 0x00008},
  10995. { 0x00004000, 0x00400},
  10996. { 0x00006000, 0x00400},
  10997. { 0x00008000, 0x01000},
  10998. { 0x00010000, 0x01000},
  10999. { 0xffffffff, 0x00000}
  11000. }, mem_tbl_5717[] = {
  11001. { 0x00000200, 0x00008},
  11002. { 0x00010000, 0x0a000},
  11003. { 0x00020000, 0x13c00},
  11004. { 0xffffffff, 0x00000}
  11005. }, mem_tbl_57765[] = {
  11006. { 0x00000200, 0x00008},
  11007. { 0x00004000, 0x00800},
  11008. { 0x00006000, 0x09800},
  11009. { 0x00010000, 0x0a000},
  11010. { 0xffffffff, 0x00000}
  11011. };
  11012. struct mem_entry *mem_tbl;
  11013. int err = 0;
  11014. int i;
  11015. if (tg3_flag(tp, 5717_PLUS))
  11016. mem_tbl = mem_tbl_5717;
  11017. else if (tg3_flag(tp, 57765_CLASS) ||
  11018. tg3_asic_rev(tp) == ASIC_REV_5762)
  11019. mem_tbl = mem_tbl_57765;
  11020. else if (tg3_flag(tp, 5755_PLUS))
  11021. mem_tbl = mem_tbl_5755;
  11022. else if (tg3_asic_rev(tp) == ASIC_REV_5906)
  11023. mem_tbl = mem_tbl_5906;
  11024. else if (tg3_flag(tp, 5705_PLUS))
  11025. mem_tbl = mem_tbl_5705;
  11026. else
  11027. mem_tbl = mem_tbl_570x;
  11028. for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
  11029. err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
  11030. if (err)
  11031. break;
  11032. }
  11033. return err;
  11034. }
  11035. #define TG3_TSO_MSS 500
  11036. #define TG3_TSO_IP_HDR_LEN 20
  11037. #define TG3_TSO_TCP_HDR_LEN 20
  11038. #define TG3_TSO_TCP_OPT_LEN 12
  11039. static const u8 tg3_tso_header[] = {
  11040. 0x08, 0x00,
  11041. 0x45, 0x00, 0x00, 0x00,
  11042. 0x00, 0x00, 0x40, 0x00,
  11043. 0x40, 0x06, 0x00, 0x00,
  11044. 0x0a, 0x00, 0x00, 0x01,
  11045. 0x0a, 0x00, 0x00, 0x02,
  11046. 0x0d, 0x00, 0xe0, 0x00,
  11047. 0x00, 0x00, 0x01, 0x00,
  11048. 0x00, 0x00, 0x02, 0x00,
  11049. 0x80, 0x10, 0x10, 0x00,
  11050. 0x14, 0x09, 0x00, 0x00,
  11051. 0x01, 0x01, 0x08, 0x0a,
  11052. 0x11, 0x11, 0x11, 0x11,
  11053. 0x11, 0x11, 0x11, 0x11,
  11054. };
  11055. static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, bool tso_loopback)
  11056. {
  11057. u32 rx_start_idx, rx_idx, tx_idx, opaque_key;
  11058. u32 base_flags = 0, mss = 0, desc_idx, coal_now, data_off, val;
  11059. u32 budget;
  11060. struct sk_buff *skb;
  11061. u8 *tx_data, *rx_data;
  11062. dma_addr_t map;
  11063. int num_pkts, tx_len, rx_len, i, err;
  11064. struct tg3_rx_buffer_desc *desc;
  11065. struct tg3_napi *tnapi, *rnapi;
  11066. struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
  11067. tnapi = &tp->napi[0];
  11068. rnapi = &tp->napi[0];
  11069. if (tp->irq_cnt > 1) {
  11070. if (tg3_flag(tp, ENABLE_RSS))
  11071. rnapi = &tp->napi[1];
  11072. if (tg3_flag(tp, ENABLE_TSS))
  11073. tnapi = &tp->napi[1];
  11074. }
  11075. coal_now = tnapi->coal_now | rnapi->coal_now;
  11076. err = -EIO;
  11077. tx_len = pktsz;
  11078. skb = netdev_alloc_skb(tp->dev, tx_len);
  11079. if (!skb)
  11080. return -ENOMEM;
  11081. tx_data = skb_put(skb, tx_len);
  11082. memcpy(tx_data, tp->dev->dev_addr, ETH_ALEN);
  11083. memset(tx_data + ETH_ALEN, 0x0, 8);
  11084. tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
  11085. if (tso_loopback) {
  11086. struct iphdr *iph = (struct iphdr *)&tx_data[ETH_HLEN];
  11087. u32 hdr_len = TG3_TSO_IP_HDR_LEN + TG3_TSO_TCP_HDR_LEN +
  11088. TG3_TSO_TCP_OPT_LEN;
  11089. memcpy(tx_data + ETH_ALEN * 2, tg3_tso_header,
  11090. sizeof(tg3_tso_header));
  11091. mss = TG3_TSO_MSS;
  11092. val = tx_len - ETH_ALEN * 2 - sizeof(tg3_tso_header);
  11093. num_pkts = DIV_ROUND_UP(val, TG3_TSO_MSS);
  11094. /* Set the total length field in the IP header */
  11095. iph->tot_len = htons((u16)(mss + hdr_len));
  11096. base_flags = (TXD_FLAG_CPU_PRE_DMA |
  11097. TXD_FLAG_CPU_POST_DMA);
  11098. if (tg3_flag(tp, HW_TSO_1) ||
  11099. tg3_flag(tp, HW_TSO_2) ||
  11100. tg3_flag(tp, HW_TSO_3)) {
  11101. struct tcphdr *th;
  11102. val = ETH_HLEN + TG3_TSO_IP_HDR_LEN;
  11103. th = (struct tcphdr *)&tx_data[val];
  11104. th->check = 0;
  11105. } else
  11106. base_flags |= TXD_FLAG_TCPUDP_CSUM;
  11107. if (tg3_flag(tp, HW_TSO_3)) {
  11108. mss |= (hdr_len & 0xc) << 12;
  11109. if (hdr_len & 0x10)
  11110. base_flags |= 0x00000010;
  11111. base_flags |= (hdr_len & 0x3e0) << 5;
  11112. } else if (tg3_flag(tp, HW_TSO_2))
  11113. mss |= hdr_len << 9;
  11114. else if (tg3_flag(tp, HW_TSO_1) ||
  11115. tg3_asic_rev(tp) == ASIC_REV_5705) {
  11116. mss |= (TG3_TSO_TCP_OPT_LEN << 9);
  11117. } else {
  11118. base_flags |= (TG3_TSO_TCP_OPT_LEN << 10);
  11119. }
  11120. data_off = ETH_ALEN * 2 + sizeof(tg3_tso_header);
  11121. } else {
  11122. num_pkts = 1;
  11123. data_off = ETH_HLEN;
  11124. if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
  11125. tx_len > VLAN_ETH_FRAME_LEN)
  11126. base_flags |= TXD_FLAG_JMB_PKT;
  11127. }
  11128. for (i = data_off; i < tx_len; i++)
  11129. tx_data[i] = (u8) (i & 0xff);
  11130. map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
  11131. if (pci_dma_mapping_error(tp->pdev, map)) {
  11132. dev_kfree_skb(skb);
  11133. return -EIO;
  11134. }
  11135. val = tnapi->tx_prod;
  11136. tnapi->tx_buffers[val].skb = skb;
  11137. dma_unmap_addr_set(&tnapi->tx_buffers[val], mapping, map);
  11138. tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
  11139. rnapi->coal_now);
  11140. udelay(10);
  11141. rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
  11142. budget = tg3_tx_avail(tnapi);
  11143. if (tg3_tx_frag_set(tnapi, &val, &budget, map, tx_len,
  11144. base_flags | TXD_FLAG_END, mss, 0)) {
  11145. tnapi->tx_buffers[val].skb = NULL;
  11146. dev_kfree_skb(skb);
  11147. return -EIO;
  11148. }
  11149. tnapi->tx_prod++;
  11150. /* Sync BD data before updating mailbox */
  11151. wmb();
  11152. tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
  11153. tr32_mailbox(tnapi->prodmbox);
  11154. udelay(10);
  11155. /* 350 usec to allow enough time on some 10/100 Mbps devices. */
  11156. for (i = 0; i < 35; i++) {
  11157. tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
  11158. coal_now);
  11159. udelay(10);
  11160. tx_idx = tnapi->hw_status->idx[0].tx_consumer;
  11161. rx_idx = rnapi->hw_status->idx[0].rx_producer;
  11162. if ((tx_idx == tnapi->tx_prod) &&
  11163. (rx_idx == (rx_start_idx + num_pkts)))
  11164. break;
  11165. }
  11166. tg3_tx_skb_unmap(tnapi, tnapi->tx_prod - 1, -1);
  11167. dev_kfree_skb(skb);
  11168. if (tx_idx != tnapi->tx_prod)
  11169. goto out;
  11170. if (rx_idx != rx_start_idx + num_pkts)
  11171. goto out;
  11172. val = data_off;
  11173. while (rx_idx != rx_start_idx) {
  11174. desc = &rnapi->rx_rcb[rx_start_idx++];
  11175. desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
  11176. opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
  11177. if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
  11178. (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
  11179. goto out;
  11180. rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT)
  11181. - ETH_FCS_LEN;
  11182. if (!tso_loopback) {
  11183. if (rx_len != tx_len)
  11184. goto out;
  11185. if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
  11186. if (opaque_key != RXD_OPAQUE_RING_STD)
  11187. goto out;
  11188. } else {
  11189. if (opaque_key != RXD_OPAQUE_RING_JUMBO)
  11190. goto out;
  11191. }
  11192. } else if ((desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
  11193. (desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
  11194. >> RXD_TCPCSUM_SHIFT != 0xffff) {
  11195. goto out;
  11196. }
  11197. if (opaque_key == RXD_OPAQUE_RING_STD) {
  11198. rx_data = tpr->rx_std_buffers[desc_idx].data;
  11199. map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx],
  11200. mapping);
  11201. } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
  11202. rx_data = tpr->rx_jmb_buffers[desc_idx].data;
  11203. map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx],
  11204. mapping);
  11205. } else
  11206. goto out;
  11207. pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len,
  11208. PCI_DMA_FROMDEVICE);
  11209. rx_data += TG3_RX_OFFSET(tp);
  11210. for (i = data_off; i < rx_len; i++, val++) {
  11211. if (*(rx_data + i) != (u8) (val & 0xff))
  11212. goto out;
  11213. }
  11214. }
  11215. err = 0;
  11216. /* tg3_free_rings will unmap and free the rx_data */
  11217. out:
  11218. return err;
  11219. }
  11220. #define TG3_STD_LOOPBACK_FAILED 1
  11221. #define TG3_JMB_LOOPBACK_FAILED 2
  11222. #define TG3_TSO_LOOPBACK_FAILED 4
  11223. #define TG3_LOOPBACK_FAILED \
  11224. (TG3_STD_LOOPBACK_FAILED | \
  11225. TG3_JMB_LOOPBACK_FAILED | \
  11226. TG3_TSO_LOOPBACK_FAILED)
  11227. static int tg3_test_loopback(struct tg3 *tp, u64 *data, bool do_extlpbk)
  11228. {
  11229. int err = -EIO;
  11230. u32 eee_cap;
  11231. u32 jmb_pkt_sz = 9000;
  11232. if (tp->dma_limit)
  11233. jmb_pkt_sz = tp->dma_limit - ETH_HLEN;
  11234. eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
  11235. tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
  11236. if (!netif_running(tp->dev)) {
  11237. data[TG3_MAC_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
  11238. data[TG3_PHY_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
  11239. if (do_extlpbk)
  11240. data[TG3_EXT_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
  11241. goto done;
  11242. }
  11243. err = tg3_reset_hw(tp, true);
  11244. if (err) {
  11245. data[TG3_MAC_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
  11246. data[TG3_PHY_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
  11247. if (do_extlpbk)
  11248. data[TG3_EXT_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
  11249. goto done;
  11250. }
  11251. if (tg3_flag(tp, ENABLE_RSS)) {
  11252. int i;
  11253. /* Reroute all rx packets to the 1st queue */
  11254. for (i = MAC_RSS_INDIR_TBL_0;
  11255. i < MAC_RSS_INDIR_TBL_0 + TG3_RSS_INDIR_TBL_SIZE; i += 4)
  11256. tw32(i, 0x0);
  11257. }
  11258. /* HW errata - mac loopback fails in some cases on 5780.
  11259. * Normal traffic and PHY loopback are not affected by
  11260. * errata. Also, the MAC loopback test is deprecated for
  11261. * all newer ASIC revisions.
  11262. */
  11263. if (tg3_asic_rev(tp) != ASIC_REV_5780 &&
  11264. !tg3_flag(tp, CPMU_PRESENT)) {
  11265. tg3_mac_loopback(tp, true);
  11266. if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
  11267. data[TG3_MAC_LOOPB_TEST] |= TG3_STD_LOOPBACK_FAILED;
  11268. if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
  11269. tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
  11270. data[TG3_MAC_LOOPB_TEST] |= TG3_JMB_LOOPBACK_FAILED;
  11271. tg3_mac_loopback(tp, false);
  11272. }
  11273. if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
  11274. !tg3_flag(tp, USE_PHYLIB)) {
  11275. int i;
  11276. tg3_phy_lpbk_set(tp, 0, false);
  11277. /* Wait for link */
  11278. for (i = 0; i < 100; i++) {
  11279. if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
  11280. break;
  11281. mdelay(1);
  11282. }
  11283. if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
  11284. data[TG3_PHY_LOOPB_TEST] |= TG3_STD_LOOPBACK_FAILED;
  11285. if (tg3_flag(tp, TSO_CAPABLE) &&
  11286. tg3_run_loopback(tp, ETH_FRAME_LEN, true))
  11287. data[TG3_PHY_LOOPB_TEST] |= TG3_TSO_LOOPBACK_FAILED;
  11288. if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
  11289. tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
  11290. data[TG3_PHY_LOOPB_TEST] |= TG3_JMB_LOOPBACK_FAILED;
  11291. if (do_extlpbk) {
  11292. tg3_phy_lpbk_set(tp, 0, true);
  11293. /* All link indications report up, but the hardware
  11294. * isn't really ready for about 20 msec. Double it
  11295. * to be sure.
  11296. */
  11297. mdelay(40);
  11298. if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
  11299. data[TG3_EXT_LOOPB_TEST] |=
  11300. TG3_STD_LOOPBACK_FAILED;
  11301. if (tg3_flag(tp, TSO_CAPABLE) &&
  11302. tg3_run_loopback(tp, ETH_FRAME_LEN, true))
  11303. data[TG3_EXT_LOOPB_TEST] |=
  11304. TG3_TSO_LOOPBACK_FAILED;
  11305. if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
  11306. tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
  11307. data[TG3_EXT_LOOPB_TEST] |=
  11308. TG3_JMB_LOOPBACK_FAILED;
  11309. }
  11310. /* Re-enable gphy autopowerdown. */
  11311. if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
  11312. tg3_phy_toggle_apd(tp, true);
  11313. }
  11314. err = (data[TG3_MAC_LOOPB_TEST] | data[TG3_PHY_LOOPB_TEST] |
  11315. data[TG3_EXT_LOOPB_TEST]) ? -EIO : 0;
  11316. done:
  11317. tp->phy_flags |= eee_cap;
  11318. return err;
  11319. }
  11320. static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
  11321. u64 *data)
  11322. {
  11323. struct tg3 *tp = netdev_priv(dev);
  11324. bool doextlpbk = etest->flags & ETH_TEST_FL_EXTERNAL_LB;
  11325. if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
  11326. if (tg3_power_up(tp)) {
  11327. etest->flags |= ETH_TEST_FL_FAILED;
  11328. memset(data, 1, sizeof(u64) * TG3_NUM_TEST);
  11329. return;
  11330. }
  11331. tg3_ape_driver_state_change(tp, RESET_KIND_INIT);
  11332. }
  11333. memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
  11334. if (tg3_test_nvram(tp) != 0) {
  11335. etest->flags |= ETH_TEST_FL_FAILED;
  11336. data[TG3_NVRAM_TEST] = 1;
  11337. }
  11338. if (!doextlpbk && tg3_test_link(tp)) {
  11339. etest->flags |= ETH_TEST_FL_FAILED;
  11340. data[TG3_LINK_TEST] = 1;
  11341. }
  11342. if (etest->flags & ETH_TEST_FL_OFFLINE) {
  11343. int err, err2 = 0, irq_sync = 0;
  11344. if (netif_running(dev)) {
  11345. tg3_phy_stop(tp);
  11346. tg3_netif_stop(tp);
  11347. irq_sync = 1;
  11348. }
  11349. tg3_full_lock(tp, irq_sync);
  11350. tg3_halt(tp, RESET_KIND_SUSPEND, 1);
  11351. err = tg3_nvram_lock(tp);
  11352. tg3_halt_cpu(tp, RX_CPU_BASE);
  11353. if (!tg3_flag(tp, 5705_PLUS))
  11354. tg3_halt_cpu(tp, TX_CPU_BASE);
  11355. if (!err)
  11356. tg3_nvram_unlock(tp);
  11357. if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
  11358. tg3_phy_reset(tp);
  11359. if (tg3_test_registers(tp) != 0) {
  11360. etest->flags |= ETH_TEST_FL_FAILED;
  11361. data[TG3_REGISTER_TEST] = 1;
  11362. }
  11363. if (tg3_test_memory(tp) != 0) {
  11364. etest->flags |= ETH_TEST_FL_FAILED;
  11365. data[TG3_MEMORY_TEST] = 1;
  11366. }
  11367. if (doextlpbk)
  11368. etest->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE;
  11369. if (tg3_test_loopback(tp, data, doextlpbk))
  11370. etest->flags |= ETH_TEST_FL_FAILED;
  11371. tg3_full_unlock(tp);
  11372. if (tg3_test_interrupt(tp) != 0) {
  11373. etest->flags |= ETH_TEST_FL_FAILED;
  11374. data[TG3_INTERRUPT_TEST] = 1;
  11375. }
  11376. tg3_full_lock(tp, 0);
  11377. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  11378. if (netif_running(dev)) {
  11379. tg3_flag_set(tp, INIT_COMPLETE);
  11380. err2 = tg3_restart_hw(tp, true);
  11381. if (!err2)
  11382. tg3_netif_start(tp);
  11383. }
  11384. tg3_full_unlock(tp);
  11385. if (irq_sync && !err2)
  11386. tg3_phy_start(tp);
  11387. }
  11388. if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
  11389. tg3_power_down_prepare(tp);
  11390. }
  11391. static int tg3_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
  11392. {
  11393. struct tg3 *tp = netdev_priv(dev);
  11394. struct hwtstamp_config stmpconf;
  11395. if (!tg3_flag(tp, PTP_CAPABLE))
  11396. return -EOPNOTSUPP;
  11397. if (copy_from_user(&stmpconf, ifr->ifr_data, sizeof(stmpconf)))
  11398. return -EFAULT;
  11399. if (stmpconf.flags)
  11400. return -EINVAL;
  11401. if (stmpconf.tx_type != HWTSTAMP_TX_ON &&
  11402. stmpconf.tx_type != HWTSTAMP_TX_OFF)
  11403. return -ERANGE;
  11404. switch (stmpconf.rx_filter) {
  11405. case HWTSTAMP_FILTER_NONE:
  11406. tp->rxptpctl = 0;
  11407. break;
  11408. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  11409. tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
  11410. TG3_RX_PTP_CTL_ALL_V1_EVENTS;
  11411. break;
  11412. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  11413. tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
  11414. TG3_RX_PTP_CTL_SYNC_EVNT;
  11415. break;
  11416. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  11417. tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
  11418. TG3_RX_PTP_CTL_DELAY_REQ;
  11419. break;
  11420. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  11421. tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
  11422. TG3_RX_PTP_CTL_ALL_V2_EVENTS;
  11423. break;
  11424. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  11425. tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
  11426. TG3_RX_PTP_CTL_ALL_V2_EVENTS;
  11427. break;
  11428. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  11429. tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
  11430. TG3_RX_PTP_CTL_ALL_V2_EVENTS;
  11431. break;
  11432. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  11433. tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
  11434. TG3_RX_PTP_CTL_SYNC_EVNT;
  11435. break;
  11436. case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  11437. tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
  11438. TG3_RX_PTP_CTL_SYNC_EVNT;
  11439. break;
  11440. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  11441. tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
  11442. TG3_RX_PTP_CTL_SYNC_EVNT;
  11443. break;
  11444. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  11445. tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
  11446. TG3_RX_PTP_CTL_DELAY_REQ;
  11447. break;
  11448. case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
  11449. tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
  11450. TG3_RX_PTP_CTL_DELAY_REQ;
  11451. break;
  11452. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  11453. tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
  11454. TG3_RX_PTP_CTL_DELAY_REQ;
  11455. break;
  11456. default:
  11457. return -ERANGE;
  11458. }
  11459. if (netif_running(dev) && tp->rxptpctl)
  11460. tw32(TG3_RX_PTP_CTL,
  11461. tp->rxptpctl | TG3_RX_PTP_CTL_HWTS_INTERLOCK);
  11462. if (stmpconf.tx_type == HWTSTAMP_TX_ON)
  11463. tg3_flag_set(tp, TX_TSTAMP_EN);
  11464. else
  11465. tg3_flag_clear(tp, TX_TSTAMP_EN);
  11466. return copy_to_user(ifr->ifr_data, &stmpconf, sizeof(stmpconf)) ?
  11467. -EFAULT : 0;
  11468. }
  11469. static int tg3_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
  11470. {
  11471. struct tg3 *tp = netdev_priv(dev);
  11472. struct hwtstamp_config stmpconf;
  11473. if (!tg3_flag(tp, PTP_CAPABLE))
  11474. return -EOPNOTSUPP;
  11475. stmpconf.flags = 0;
  11476. stmpconf.tx_type = (tg3_flag(tp, TX_TSTAMP_EN) ?
  11477. HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF);
  11478. switch (tp->rxptpctl) {
  11479. case 0:
  11480. stmpconf.rx_filter = HWTSTAMP_FILTER_NONE;
  11481. break;
  11482. case TG3_RX_PTP_CTL_RX_PTP_V1_EN | TG3_RX_PTP_CTL_ALL_V1_EVENTS:
  11483. stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
  11484. break;
  11485. case TG3_RX_PTP_CTL_RX_PTP_V1_EN | TG3_RX_PTP_CTL_SYNC_EVNT:
  11486. stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_SYNC;
  11487. break;
  11488. case TG3_RX_PTP_CTL_RX_PTP_V1_EN | TG3_RX_PTP_CTL_DELAY_REQ:
  11489. stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ;
  11490. break;
  11491. case TG3_RX_PTP_CTL_RX_PTP_V2_EN | TG3_RX_PTP_CTL_ALL_V2_EVENTS:
  11492. stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
  11493. break;
  11494. case TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN | TG3_RX_PTP_CTL_ALL_V2_EVENTS:
  11495. stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
  11496. break;
  11497. case TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN | TG3_RX_PTP_CTL_ALL_V2_EVENTS:
  11498. stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
  11499. break;
  11500. case TG3_RX_PTP_CTL_RX_PTP_V2_EN | TG3_RX_PTP_CTL_SYNC_EVNT:
  11501. stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_SYNC;
  11502. break;
  11503. case TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN | TG3_RX_PTP_CTL_SYNC_EVNT:
  11504. stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_SYNC;
  11505. break;
  11506. case TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN | TG3_RX_PTP_CTL_SYNC_EVNT:
  11507. stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_SYNC;
  11508. break;
  11509. case TG3_RX_PTP_CTL_RX_PTP_V2_EN | TG3_RX_PTP_CTL_DELAY_REQ:
  11510. stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_DELAY_REQ;
  11511. break;
  11512. case TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN | TG3_RX_PTP_CTL_DELAY_REQ:
  11513. stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ;
  11514. break;
  11515. case TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN | TG3_RX_PTP_CTL_DELAY_REQ:
  11516. stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ;
  11517. break;
  11518. default:
  11519. WARN_ON_ONCE(1);
  11520. return -ERANGE;
  11521. }
  11522. return copy_to_user(ifr->ifr_data, &stmpconf, sizeof(stmpconf)) ?
  11523. -EFAULT : 0;
  11524. }
  11525. static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  11526. {
  11527. struct mii_ioctl_data *data = if_mii(ifr);
  11528. struct tg3 *tp = netdev_priv(dev);
  11529. int err;
  11530. if (tg3_flag(tp, USE_PHYLIB)) {
  11531. struct phy_device *phydev;
  11532. if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
  11533. return -EAGAIN;
  11534. phydev = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr);
  11535. return phy_mii_ioctl(phydev, ifr, cmd);
  11536. }
  11537. switch (cmd) {
  11538. case SIOCGMIIPHY:
  11539. data->phy_id = tp->phy_addr;
  11540. /* fallthru */
  11541. case SIOCGMIIREG: {
  11542. u32 mii_regval;
  11543. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
  11544. break; /* We have no PHY */
  11545. if (!netif_running(dev))
  11546. return -EAGAIN;
  11547. spin_lock_bh(&tp->lock);
  11548. err = __tg3_readphy(tp, data->phy_id & 0x1f,
  11549. data->reg_num & 0x1f, &mii_regval);
  11550. spin_unlock_bh(&tp->lock);
  11551. data->val_out = mii_regval;
  11552. return err;
  11553. }
  11554. case SIOCSMIIREG:
  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_writephy(tp, data->phy_id & 0x1f,
  11561. data->reg_num & 0x1f, data->val_in);
  11562. spin_unlock_bh(&tp->lock);
  11563. return err;
  11564. case SIOCSHWTSTAMP:
  11565. return tg3_hwtstamp_set(dev, ifr);
  11566. case SIOCGHWTSTAMP:
  11567. return tg3_hwtstamp_get(dev, ifr);
  11568. default:
  11569. /* do nothing */
  11570. break;
  11571. }
  11572. return -EOPNOTSUPP;
  11573. }
  11574. static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
  11575. {
  11576. struct tg3 *tp = netdev_priv(dev);
  11577. memcpy(ec, &tp->coal, sizeof(*ec));
  11578. return 0;
  11579. }
  11580. static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
  11581. {
  11582. struct tg3 *tp = netdev_priv(dev);
  11583. u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
  11584. u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
  11585. if (!tg3_flag(tp, 5705_PLUS)) {
  11586. max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
  11587. max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
  11588. max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
  11589. min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
  11590. }
  11591. if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
  11592. (!ec->rx_coalesce_usecs) ||
  11593. (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
  11594. (!ec->tx_coalesce_usecs) ||
  11595. (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
  11596. (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
  11597. (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
  11598. (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
  11599. (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
  11600. (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
  11601. (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
  11602. (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
  11603. return -EINVAL;
  11604. /* Only copy relevant parameters, ignore all others. */
  11605. tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
  11606. tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
  11607. tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
  11608. tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
  11609. tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
  11610. tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
  11611. tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
  11612. tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
  11613. tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
  11614. if (netif_running(dev)) {
  11615. tg3_full_lock(tp, 0);
  11616. __tg3_set_coalesce(tp, &tp->coal);
  11617. tg3_full_unlock(tp);
  11618. }
  11619. return 0;
  11620. }
  11621. static int tg3_set_eee(struct net_device *dev, struct ethtool_eee *edata)
  11622. {
  11623. struct tg3 *tp = netdev_priv(dev);
  11624. if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP)) {
  11625. netdev_warn(tp->dev, "Board does not support EEE!\n");
  11626. return -EOPNOTSUPP;
  11627. }
  11628. if (edata->advertised != tp->eee.advertised) {
  11629. netdev_warn(tp->dev,
  11630. "Direct manipulation of EEE advertisement is not supported\n");
  11631. return -EINVAL;
  11632. }
  11633. if (edata->tx_lpi_timer > TG3_CPMU_DBTMR1_LNKIDLE_MAX) {
  11634. netdev_warn(tp->dev,
  11635. "Maximal Tx Lpi timer supported is %#x(u)\n",
  11636. TG3_CPMU_DBTMR1_LNKIDLE_MAX);
  11637. return -EINVAL;
  11638. }
  11639. tp->eee = *edata;
  11640. tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
  11641. tg3_warn_mgmt_link_flap(tp);
  11642. if (netif_running(tp->dev)) {
  11643. tg3_full_lock(tp, 0);
  11644. tg3_setup_eee(tp);
  11645. tg3_phy_reset(tp);
  11646. tg3_full_unlock(tp);
  11647. }
  11648. return 0;
  11649. }
  11650. static int tg3_get_eee(struct net_device *dev, struct ethtool_eee *edata)
  11651. {
  11652. struct tg3 *tp = netdev_priv(dev);
  11653. if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP)) {
  11654. netdev_warn(tp->dev,
  11655. "Board does not support EEE!\n");
  11656. return -EOPNOTSUPP;
  11657. }
  11658. *edata = tp->eee;
  11659. return 0;
  11660. }
  11661. static const struct ethtool_ops tg3_ethtool_ops = {
  11662. .get_drvinfo = tg3_get_drvinfo,
  11663. .get_regs_len = tg3_get_regs_len,
  11664. .get_regs = tg3_get_regs,
  11665. .get_wol = tg3_get_wol,
  11666. .set_wol = tg3_set_wol,
  11667. .get_msglevel = tg3_get_msglevel,
  11668. .set_msglevel = tg3_set_msglevel,
  11669. .nway_reset = tg3_nway_reset,
  11670. .get_link = ethtool_op_get_link,
  11671. .get_eeprom_len = tg3_get_eeprom_len,
  11672. .get_eeprom = tg3_get_eeprom,
  11673. .set_eeprom = tg3_set_eeprom,
  11674. .get_ringparam = tg3_get_ringparam,
  11675. .set_ringparam = tg3_set_ringparam,
  11676. .get_pauseparam = tg3_get_pauseparam,
  11677. .set_pauseparam = tg3_set_pauseparam,
  11678. .self_test = tg3_self_test,
  11679. .get_strings = tg3_get_strings,
  11680. .set_phys_id = tg3_set_phys_id,
  11681. .get_ethtool_stats = tg3_get_ethtool_stats,
  11682. .get_coalesce = tg3_get_coalesce,
  11683. .set_coalesce = tg3_set_coalesce,
  11684. .get_sset_count = tg3_get_sset_count,
  11685. .get_rxnfc = tg3_get_rxnfc,
  11686. .get_rxfh_indir_size = tg3_get_rxfh_indir_size,
  11687. .get_rxfh = tg3_get_rxfh,
  11688. .set_rxfh = tg3_set_rxfh,
  11689. .get_channels = tg3_get_channels,
  11690. .set_channels = tg3_set_channels,
  11691. .get_ts_info = tg3_get_ts_info,
  11692. .get_eee = tg3_get_eee,
  11693. .set_eee = tg3_set_eee,
  11694. .get_link_ksettings = tg3_get_link_ksettings,
  11695. .set_link_ksettings = tg3_set_link_ksettings,
  11696. };
  11697. static void tg3_get_stats64(struct net_device *dev,
  11698. struct rtnl_link_stats64 *stats)
  11699. {
  11700. struct tg3 *tp = netdev_priv(dev);
  11701. spin_lock_bh(&tp->lock);
  11702. if (!tp->hw_stats || !tg3_flag(tp, INIT_COMPLETE)) {
  11703. *stats = tp->net_stats_prev;
  11704. spin_unlock_bh(&tp->lock);
  11705. return;
  11706. }
  11707. tg3_get_nstats(tp, stats);
  11708. spin_unlock_bh(&tp->lock);
  11709. }
  11710. static void tg3_set_rx_mode(struct net_device *dev)
  11711. {
  11712. struct tg3 *tp = netdev_priv(dev);
  11713. if (!netif_running(dev))
  11714. return;
  11715. tg3_full_lock(tp, 0);
  11716. __tg3_set_rx_mode(dev);
  11717. tg3_full_unlock(tp);
  11718. }
  11719. static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
  11720. int new_mtu)
  11721. {
  11722. dev->mtu = new_mtu;
  11723. if (new_mtu > ETH_DATA_LEN) {
  11724. if (tg3_flag(tp, 5780_CLASS)) {
  11725. netdev_update_features(dev);
  11726. tg3_flag_clear(tp, TSO_CAPABLE);
  11727. } else {
  11728. tg3_flag_set(tp, JUMBO_RING_ENABLE);
  11729. }
  11730. } else {
  11731. if (tg3_flag(tp, 5780_CLASS)) {
  11732. tg3_flag_set(tp, TSO_CAPABLE);
  11733. netdev_update_features(dev);
  11734. }
  11735. tg3_flag_clear(tp, JUMBO_RING_ENABLE);
  11736. }
  11737. }
  11738. static int tg3_change_mtu(struct net_device *dev, int new_mtu)
  11739. {
  11740. struct tg3 *tp = netdev_priv(dev);
  11741. int err;
  11742. bool reset_phy = false;
  11743. if (!netif_running(dev)) {
  11744. /* We'll just catch it later when the
  11745. * device is up'd.
  11746. */
  11747. tg3_set_mtu(dev, tp, new_mtu);
  11748. return 0;
  11749. }
  11750. tg3_phy_stop(tp);
  11751. tg3_netif_stop(tp);
  11752. tg3_set_mtu(dev, tp, new_mtu);
  11753. tg3_full_lock(tp, 1);
  11754. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  11755. /* Reset PHY, otherwise the read DMA engine will be in a mode that
  11756. * breaks all requests to 256 bytes.
  11757. */
  11758. if (tg3_asic_rev(tp) == ASIC_REV_57766 ||
  11759. tg3_asic_rev(tp) == ASIC_REV_5717 ||
  11760. tg3_asic_rev(tp) == ASIC_REV_5719 ||
  11761. tg3_asic_rev(tp) == ASIC_REV_5720)
  11762. reset_phy = true;
  11763. err = tg3_restart_hw(tp, reset_phy);
  11764. if (!err)
  11765. tg3_netif_start(tp);
  11766. tg3_full_unlock(tp);
  11767. if (!err)
  11768. tg3_phy_start(tp);
  11769. return err;
  11770. }
  11771. static const struct net_device_ops tg3_netdev_ops = {
  11772. .ndo_open = tg3_open,
  11773. .ndo_stop = tg3_close,
  11774. .ndo_start_xmit = tg3_start_xmit,
  11775. .ndo_get_stats64 = tg3_get_stats64,
  11776. .ndo_validate_addr = eth_validate_addr,
  11777. .ndo_set_rx_mode = tg3_set_rx_mode,
  11778. .ndo_set_mac_address = tg3_set_mac_addr,
  11779. .ndo_do_ioctl = tg3_ioctl,
  11780. .ndo_tx_timeout = tg3_tx_timeout,
  11781. .ndo_change_mtu = tg3_change_mtu,
  11782. .ndo_fix_features = tg3_fix_features,
  11783. .ndo_set_features = tg3_set_features,
  11784. #ifdef CONFIG_NET_POLL_CONTROLLER
  11785. .ndo_poll_controller = tg3_poll_controller,
  11786. #endif
  11787. };
  11788. static void tg3_get_eeprom_size(struct tg3 *tp)
  11789. {
  11790. u32 cursize, val, magic;
  11791. tp->nvram_size = EEPROM_CHIP_SIZE;
  11792. if (tg3_nvram_read(tp, 0, &magic) != 0)
  11793. return;
  11794. if ((magic != TG3_EEPROM_MAGIC) &&
  11795. ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
  11796. ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
  11797. return;
  11798. /*
  11799. * Size the chip by reading offsets at increasing powers of two.
  11800. * When we encounter our validation signature, we know the addressing
  11801. * has wrapped around, and thus have our chip size.
  11802. */
  11803. cursize = 0x10;
  11804. while (cursize < tp->nvram_size) {
  11805. if (tg3_nvram_read(tp, cursize, &val) != 0)
  11806. return;
  11807. if (val == magic)
  11808. break;
  11809. cursize <<= 1;
  11810. }
  11811. tp->nvram_size = cursize;
  11812. }
  11813. static void tg3_get_nvram_size(struct tg3 *tp)
  11814. {
  11815. u32 val;
  11816. if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &val) != 0)
  11817. return;
  11818. /* Selfboot format */
  11819. if (val != TG3_EEPROM_MAGIC) {
  11820. tg3_get_eeprom_size(tp);
  11821. return;
  11822. }
  11823. if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
  11824. if (val != 0) {
  11825. /* This is confusing. We want to operate on the
  11826. * 16-bit value at offset 0xf2. The tg3_nvram_read()
  11827. * call will read from NVRAM and byteswap the data
  11828. * according to the byteswapping settings for all
  11829. * other register accesses. This ensures the data we
  11830. * want will always reside in the lower 16-bits.
  11831. * However, the data in NVRAM is in LE format, which
  11832. * means the data from the NVRAM read will always be
  11833. * opposite the endianness of the CPU. The 16-bit
  11834. * byteswap then brings the data to CPU endianness.
  11835. */
  11836. tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
  11837. return;
  11838. }
  11839. }
  11840. tp->nvram_size = TG3_NVRAM_SIZE_512KB;
  11841. }
  11842. static void tg3_get_nvram_info(struct tg3 *tp)
  11843. {
  11844. u32 nvcfg1;
  11845. nvcfg1 = tr32(NVRAM_CFG1);
  11846. if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
  11847. tg3_flag_set(tp, FLASH);
  11848. } else {
  11849. nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
  11850. tw32(NVRAM_CFG1, nvcfg1);
  11851. }
  11852. if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
  11853. tg3_flag(tp, 5780_CLASS)) {
  11854. switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
  11855. case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
  11856. tp->nvram_jedecnum = JEDEC_ATMEL;
  11857. tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
  11858. tg3_flag_set(tp, NVRAM_BUFFERED);
  11859. break;
  11860. case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
  11861. tp->nvram_jedecnum = JEDEC_ATMEL;
  11862. tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
  11863. break;
  11864. case FLASH_VENDOR_ATMEL_EEPROM:
  11865. tp->nvram_jedecnum = JEDEC_ATMEL;
  11866. tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
  11867. tg3_flag_set(tp, NVRAM_BUFFERED);
  11868. break;
  11869. case FLASH_VENDOR_ST:
  11870. tp->nvram_jedecnum = JEDEC_ST;
  11871. tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
  11872. tg3_flag_set(tp, NVRAM_BUFFERED);
  11873. break;
  11874. case FLASH_VENDOR_SAIFUN:
  11875. tp->nvram_jedecnum = JEDEC_SAIFUN;
  11876. tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
  11877. break;
  11878. case FLASH_VENDOR_SST_SMALL:
  11879. case FLASH_VENDOR_SST_LARGE:
  11880. tp->nvram_jedecnum = JEDEC_SST;
  11881. tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
  11882. break;
  11883. }
  11884. } else {
  11885. tp->nvram_jedecnum = JEDEC_ATMEL;
  11886. tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
  11887. tg3_flag_set(tp, NVRAM_BUFFERED);
  11888. }
  11889. }
  11890. static void tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
  11891. {
  11892. switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
  11893. case FLASH_5752PAGE_SIZE_256:
  11894. tp->nvram_pagesize = 256;
  11895. break;
  11896. case FLASH_5752PAGE_SIZE_512:
  11897. tp->nvram_pagesize = 512;
  11898. break;
  11899. case FLASH_5752PAGE_SIZE_1K:
  11900. tp->nvram_pagesize = 1024;
  11901. break;
  11902. case FLASH_5752PAGE_SIZE_2K:
  11903. tp->nvram_pagesize = 2048;
  11904. break;
  11905. case FLASH_5752PAGE_SIZE_4K:
  11906. tp->nvram_pagesize = 4096;
  11907. break;
  11908. case FLASH_5752PAGE_SIZE_264:
  11909. tp->nvram_pagesize = 264;
  11910. break;
  11911. case FLASH_5752PAGE_SIZE_528:
  11912. tp->nvram_pagesize = 528;
  11913. break;
  11914. }
  11915. }
  11916. static void tg3_get_5752_nvram_info(struct tg3 *tp)
  11917. {
  11918. u32 nvcfg1;
  11919. nvcfg1 = tr32(NVRAM_CFG1);
  11920. /* NVRAM protection for TPM */
  11921. if (nvcfg1 & (1 << 27))
  11922. tg3_flag_set(tp, PROTECTED_NVRAM);
  11923. switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
  11924. case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
  11925. case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
  11926. tp->nvram_jedecnum = JEDEC_ATMEL;
  11927. tg3_flag_set(tp, NVRAM_BUFFERED);
  11928. break;
  11929. case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
  11930. tp->nvram_jedecnum = JEDEC_ATMEL;
  11931. tg3_flag_set(tp, NVRAM_BUFFERED);
  11932. tg3_flag_set(tp, FLASH);
  11933. break;
  11934. case FLASH_5752VENDOR_ST_M45PE10:
  11935. case FLASH_5752VENDOR_ST_M45PE20:
  11936. case FLASH_5752VENDOR_ST_M45PE40:
  11937. tp->nvram_jedecnum = JEDEC_ST;
  11938. tg3_flag_set(tp, NVRAM_BUFFERED);
  11939. tg3_flag_set(tp, FLASH);
  11940. break;
  11941. }
  11942. if (tg3_flag(tp, FLASH)) {
  11943. tg3_nvram_get_pagesize(tp, nvcfg1);
  11944. } else {
  11945. /* For eeprom, set pagesize to maximum eeprom size */
  11946. tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
  11947. nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
  11948. tw32(NVRAM_CFG1, nvcfg1);
  11949. }
  11950. }
  11951. static void tg3_get_5755_nvram_info(struct tg3 *tp)
  11952. {
  11953. u32 nvcfg1, protect = 0;
  11954. nvcfg1 = tr32(NVRAM_CFG1);
  11955. /* NVRAM protection for TPM */
  11956. if (nvcfg1 & (1 << 27)) {
  11957. tg3_flag_set(tp, PROTECTED_NVRAM);
  11958. protect = 1;
  11959. }
  11960. nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
  11961. switch (nvcfg1) {
  11962. case FLASH_5755VENDOR_ATMEL_FLASH_1:
  11963. case FLASH_5755VENDOR_ATMEL_FLASH_2:
  11964. case FLASH_5755VENDOR_ATMEL_FLASH_3:
  11965. case FLASH_5755VENDOR_ATMEL_FLASH_5:
  11966. tp->nvram_jedecnum = JEDEC_ATMEL;
  11967. tg3_flag_set(tp, NVRAM_BUFFERED);
  11968. tg3_flag_set(tp, FLASH);
  11969. tp->nvram_pagesize = 264;
  11970. if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
  11971. nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
  11972. tp->nvram_size = (protect ? 0x3e200 :
  11973. TG3_NVRAM_SIZE_512KB);
  11974. else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
  11975. tp->nvram_size = (protect ? 0x1f200 :
  11976. TG3_NVRAM_SIZE_256KB);
  11977. else
  11978. tp->nvram_size = (protect ? 0x1f200 :
  11979. TG3_NVRAM_SIZE_128KB);
  11980. break;
  11981. case FLASH_5752VENDOR_ST_M45PE10:
  11982. case FLASH_5752VENDOR_ST_M45PE20:
  11983. case FLASH_5752VENDOR_ST_M45PE40:
  11984. tp->nvram_jedecnum = JEDEC_ST;
  11985. tg3_flag_set(tp, NVRAM_BUFFERED);
  11986. tg3_flag_set(tp, FLASH);
  11987. tp->nvram_pagesize = 256;
  11988. if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
  11989. tp->nvram_size = (protect ?
  11990. TG3_NVRAM_SIZE_64KB :
  11991. TG3_NVRAM_SIZE_128KB);
  11992. else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
  11993. tp->nvram_size = (protect ?
  11994. TG3_NVRAM_SIZE_64KB :
  11995. TG3_NVRAM_SIZE_256KB);
  11996. else
  11997. tp->nvram_size = (protect ?
  11998. TG3_NVRAM_SIZE_128KB :
  11999. TG3_NVRAM_SIZE_512KB);
  12000. break;
  12001. }
  12002. }
  12003. static void tg3_get_5787_nvram_info(struct tg3 *tp)
  12004. {
  12005. u32 nvcfg1;
  12006. nvcfg1 = tr32(NVRAM_CFG1);
  12007. switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
  12008. case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
  12009. case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
  12010. case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
  12011. case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
  12012. tp->nvram_jedecnum = JEDEC_ATMEL;
  12013. tg3_flag_set(tp, NVRAM_BUFFERED);
  12014. tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
  12015. nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
  12016. tw32(NVRAM_CFG1, nvcfg1);
  12017. break;
  12018. case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
  12019. case FLASH_5755VENDOR_ATMEL_FLASH_1:
  12020. case FLASH_5755VENDOR_ATMEL_FLASH_2:
  12021. case FLASH_5755VENDOR_ATMEL_FLASH_3:
  12022. tp->nvram_jedecnum = JEDEC_ATMEL;
  12023. tg3_flag_set(tp, NVRAM_BUFFERED);
  12024. tg3_flag_set(tp, FLASH);
  12025. tp->nvram_pagesize = 264;
  12026. break;
  12027. case FLASH_5752VENDOR_ST_M45PE10:
  12028. case FLASH_5752VENDOR_ST_M45PE20:
  12029. case FLASH_5752VENDOR_ST_M45PE40:
  12030. tp->nvram_jedecnum = JEDEC_ST;
  12031. tg3_flag_set(tp, NVRAM_BUFFERED);
  12032. tg3_flag_set(tp, FLASH);
  12033. tp->nvram_pagesize = 256;
  12034. break;
  12035. }
  12036. }
  12037. static void tg3_get_5761_nvram_info(struct tg3 *tp)
  12038. {
  12039. u32 nvcfg1, protect = 0;
  12040. nvcfg1 = tr32(NVRAM_CFG1);
  12041. /* NVRAM protection for TPM */
  12042. if (nvcfg1 & (1 << 27)) {
  12043. tg3_flag_set(tp, PROTECTED_NVRAM);
  12044. protect = 1;
  12045. }
  12046. nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
  12047. switch (nvcfg1) {
  12048. case FLASH_5761VENDOR_ATMEL_ADB021D:
  12049. case FLASH_5761VENDOR_ATMEL_ADB041D:
  12050. case FLASH_5761VENDOR_ATMEL_ADB081D:
  12051. case FLASH_5761VENDOR_ATMEL_ADB161D:
  12052. case FLASH_5761VENDOR_ATMEL_MDB021D:
  12053. case FLASH_5761VENDOR_ATMEL_MDB041D:
  12054. case FLASH_5761VENDOR_ATMEL_MDB081D:
  12055. case FLASH_5761VENDOR_ATMEL_MDB161D:
  12056. tp->nvram_jedecnum = JEDEC_ATMEL;
  12057. tg3_flag_set(tp, NVRAM_BUFFERED);
  12058. tg3_flag_set(tp, FLASH);
  12059. tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
  12060. tp->nvram_pagesize = 256;
  12061. break;
  12062. case FLASH_5761VENDOR_ST_A_M45PE20:
  12063. case FLASH_5761VENDOR_ST_A_M45PE40:
  12064. case FLASH_5761VENDOR_ST_A_M45PE80:
  12065. case FLASH_5761VENDOR_ST_A_M45PE16:
  12066. case FLASH_5761VENDOR_ST_M_M45PE20:
  12067. case FLASH_5761VENDOR_ST_M_M45PE40:
  12068. case FLASH_5761VENDOR_ST_M_M45PE80:
  12069. case FLASH_5761VENDOR_ST_M_M45PE16:
  12070. tp->nvram_jedecnum = JEDEC_ST;
  12071. tg3_flag_set(tp, NVRAM_BUFFERED);
  12072. tg3_flag_set(tp, FLASH);
  12073. tp->nvram_pagesize = 256;
  12074. break;
  12075. }
  12076. if (protect) {
  12077. tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
  12078. } else {
  12079. switch (nvcfg1) {
  12080. case FLASH_5761VENDOR_ATMEL_ADB161D:
  12081. case FLASH_5761VENDOR_ATMEL_MDB161D:
  12082. case FLASH_5761VENDOR_ST_A_M45PE16:
  12083. case FLASH_5761VENDOR_ST_M_M45PE16:
  12084. tp->nvram_size = TG3_NVRAM_SIZE_2MB;
  12085. break;
  12086. case FLASH_5761VENDOR_ATMEL_ADB081D:
  12087. case FLASH_5761VENDOR_ATMEL_MDB081D:
  12088. case FLASH_5761VENDOR_ST_A_M45PE80:
  12089. case FLASH_5761VENDOR_ST_M_M45PE80:
  12090. tp->nvram_size = TG3_NVRAM_SIZE_1MB;
  12091. break;
  12092. case FLASH_5761VENDOR_ATMEL_ADB041D:
  12093. case FLASH_5761VENDOR_ATMEL_MDB041D:
  12094. case FLASH_5761VENDOR_ST_A_M45PE40:
  12095. case FLASH_5761VENDOR_ST_M_M45PE40:
  12096. tp->nvram_size = TG3_NVRAM_SIZE_512KB;
  12097. break;
  12098. case FLASH_5761VENDOR_ATMEL_ADB021D:
  12099. case FLASH_5761VENDOR_ATMEL_MDB021D:
  12100. case FLASH_5761VENDOR_ST_A_M45PE20:
  12101. case FLASH_5761VENDOR_ST_M_M45PE20:
  12102. tp->nvram_size = TG3_NVRAM_SIZE_256KB;
  12103. break;
  12104. }
  12105. }
  12106. }
  12107. static void tg3_get_5906_nvram_info(struct tg3 *tp)
  12108. {
  12109. tp->nvram_jedecnum = JEDEC_ATMEL;
  12110. tg3_flag_set(tp, NVRAM_BUFFERED);
  12111. tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
  12112. }
  12113. static void tg3_get_57780_nvram_info(struct tg3 *tp)
  12114. {
  12115. u32 nvcfg1;
  12116. nvcfg1 = tr32(NVRAM_CFG1);
  12117. switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
  12118. case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
  12119. case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
  12120. tp->nvram_jedecnum = JEDEC_ATMEL;
  12121. tg3_flag_set(tp, NVRAM_BUFFERED);
  12122. tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
  12123. nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
  12124. tw32(NVRAM_CFG1, nvcfg1);
  12125. return;
  12126. case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
  12127. case FLASH_57780VENDOR_ATMEL_AT45DB011D:
  12128. case FLASH_57780VENDOR_ATMEL_AT45DB011B:
  12129. case FLASH_57780VENDOR_ATMEL_AT45DB021D:
  12130. case FLASH_57780VENDOR_ATMEL_AT45DB021B:
  12131. case FLASH_57780VENDOR_ATMEL_AT45DB041D:
  12132. case FLASH_57780VENDOR_ATMEL_AT45DB041B:
  12133. tp->nvram_jedecnum = JEDEC_ATMEL;
  12134. tg3_flag_set(tp, NVRAM_BUFFERED);
  12135. tg3_flag_set(tp, FLASH);
  12136. switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
  12137. case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
  12138. case FLASH_57780VENDOR_ATMEL_AT45DB011D:
  12139. case FLASH_57780VENDOR_ATMEL_AT45DB011B:
  12140. tp->nvram_size = TG3_NVRAM_SIZE_128KB;
  12141. break;
  12142. case FLASH_57780VENDOR_ATMEL_AT45DB021D:
  12143. case FLASH_57780VENDOR_ATMEL_AT45DB021B:
  12144. tp->nvram_size = TG3_NVRAM_SIZE_256KB;
  12145. break;
  12146. case FLASH_57780VENDOR_ATMEL_AT45DB041D:
  12147. case FLASH_57780VENDOR_ATMEL_AT45DB041B:
  12148. tp->nvram_size = TG3_NVRAM_SIZE_512KB;
  12149. break;
  12150. }
  12151. break;
  12152. case FLASH_5752VENDOR_ST_M45PE10:
  12153. case FLASH_5752VENDOR_ST_M45PE20:
  12154. case FLASH_5752VENDOR_ST_M45PE40:
  12155. tp->nvram_jedecnum = JEDEC_ST;
  12156. tg3_flag_set(tp, NVRAM_BUFFERED);
  12157. tg3_flag_set(tp, FLASH);
  12158. switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
  12159. case FLASH_5752VENDOR_ST_M45PE10:
  12160. tp->nvram_size = TG3_NVRAM_SIZE_128KB;
  12161. break;
  12162. case FLASH_5752VENDOR_ST_M45PE20:
  12163. tp->nvram_size = TG3_NVRAM_SIZE_256KB;
  12164. break;
  12165. case FLASH_5752VENDOR_ST_M45PE40:
  12166. tp->nvram_size = TG3_NVRAM_SIZE_512KB;
  12167. break;
  12168. }
  12169. break;
  12170. default:
  12171. tg3_flag_set(tp, NO_NVRAM);
  12172. return;
  12173. }
  12174. tg3_nvram_get_pagesize(tp, nvcfg1);
  12175. if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
  12176. tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
  12177. }
  12178. static void tg3_get_5717_nvram_info(struct tg3 *tp)
  12179. {
  12180. u32 nvcfg1;
  12181. nvcfg1 = tr32(NVRAM_CFG1);
  12182. switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
  12183. case FLASH_5717VENDOR_ATMEL_EEPROM:
  12184. case FLASH_5717VENDOR_MICRO_EEPROM:
  12185. tp->nvram_jedecnum = JEDEC_ATMEL;
  12186. tg3_flag_set(tp, NVRAM_BUFFERED);
  12187. tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
  12188. nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
  12189. tw32(NVRAM_CFG1, nvcfg1);
  12190. return;
  12191. case FLASH_5717VENDOR_ATMEL_MDB011D:
  12192. case FLASH_5717VENDOR_ATMEL_ADB011B:
  12193. case FLASH_5717VENDOR_ATMEL_ADB011D:
  12194. case FLASH_5717VENDOR_ATMEL_MDB021D:
  12195. case FLASH_5717VENDOR_ATMEL_ADB021B:
  12196. case FLASH_5717VENDOR_ATMEL_ADB021D:
  12197. case FLASH_5717VENDOR_ATMEL_45USPT:
  12198. tp->nvram_jedecnum = JEDEC_ATMEL;
  12199. tg3_flag_set(tp, NVRAM_BUFFERED);
  12200. tg3_flag_set(tp, FLASH);
  12201. switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
  12202. case FLASH_5717VENDOR_ATMEL_MDB021D:
  12203. /* Detect size with tg3_nvram_get_size() */
  12204. break;
  12205. case FLASH_5717VENDOR_ATMEL_ADB021B:
  12206. case FLASH_5717VENDOR_ATMEL_ADB021D:
  12207. tp->nvram_size = TG3_NVRAM_SIZE_256KB;
  12208. break;
  12209. default:
  12210. tp->nvram_size = TG3_NVRAM_SIZE_128KB;
  12211. break;
  12212. }
  12213. break;
  12214. case FLASH_5717VENDOR_ST_M_M25PE10:
  12215. case FLASH_5717VENDOR_ST_A_M25PE10:
  12216. case FLASH_5717VENDOR_ST_M_M45PE10:
  12217. case FLASH_5717VENDOR_ST_A_M45PE10:
  12218. case FLASH_5717VENDOR_ST_M_M25PE20:
  12219. case FLASH_5717VENDOR_ST_A_M25PE20:
  12220. case FLASH_5717VENDOR_ST_M_M45PE20:
  12221. case FLASH_5717VENDOR_ST_A_M45PE20:
  12222. case FLASH_5717VENDOR_ST_25USPT:
  12223. case FLASH_5717VENDOR_ST_45USPT:
  12224. tp->nvram_jedecnum = JEDEC_ST;
  12225. tg3_flag_set(tp, NVRAM_BUFFERED);
  12226. tg3_flag_set(tp, FLASH);
  12227. switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
  12228. case FLASH_5717VENDOR_ST_M_M25PE20:
  12229. case FLASH_5717VENDOR_ST_M_M45PE20:
  12230. /* Detect size with tg3_nvram_get_size() */
  12231. break;
  12232. case FLASH_5717VENDOR_ST_A_M25PE20:
  12233. case FLASH_5717VENDOR_ST_A_M45PE20:
  12234. tp->nvram_size = TG3_NVRAM_SIZE_256KB;
  12235. break;
  12236. default:
  12237. tp->nvram_size = TG3_NVRAM_SIZE_128KB;
  12238. break;
  12239. }
  12240. break;
  12241. default:
  12242. tg3_flag_set(tp, NO_NVRAM);
  12243. return;
  12244. }
  12245. tg3_nvram_get_pagesize(tp, nvcfg1);
  12246. if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
  12247. tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
  12248. }
  12249. static void tg3_get_5720_nvram_info(struct tg3 *tp)
  12250. {
  12251. u32 nvcfg1, nvmpinstrp, nv_status;
  12252. nvcfg1 = tr32(NVRAM_CFG1);
  12253. nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
  12254. if (tg3_asic_rev(tp) == ASIC_REV_5762) {
  12255. if (!(nvcfg1 & NVRAM_CFG1_5762VENDOR_MASK)) {
  12256. tg3_flag_set(tp, NO_NVRAM);
  12257. return;
  12258. }
  12259. switch (nvmpinstrp) {
  12260. case FLASH_5762_MX25L_100:
  12261. case FLASH_5762_MX25L_200:
  12262. case FLASH_5762_MX25L_400:
  12263. case FLASH_5762_MX25L_800:
  12264. case FLASH_5762_MX25L_160_320:
  12265. tp->nvram_pagesize = 4096;
  12266. tp->nvram_jedecnum = JEDEC_MACRONIX;
  12267. tg3_flag_set(tp, NVRAM_BUFFERED);
  12268. tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
  12269. tg3_flag_set(tp, FLASH);
  12270. nv_status = tr32(NVRAM_AUTOSENSE_STATUS);
  12271. tp->nvram_size =
  12272. (1 << (nv_status >> AUTOSENSE_DEVID &
  12273. AUTOSENSE_DEVID_MASK)
  12274. << AUTOSENSE_SIZE_IN_MB);
  12275. return;
  12276. case FLASH_5762_EEPROM_HD:
  12277. nvmpinstrp = FLASH_5720_EEPROM_HD;
  12278. break;
  12279. case FLASH_5762_EEPROM_LD:
  12280. nvmpinstrp = FLASH_5720_EEPROM_LD;
  12281. break;
  12282. case FLASH_5720VENDOR_M_ST_M45PE20:
  12283. /* This pinstrap supports multiple sizes, so force it
  12284. * to read the actual size from location 0xf0.
  12285. */
  12286. nvmpinstrp = FLASH_5720VENDOR_ST_45USPT;
  12287. break;
  12288. }
  12289. }
  12290. switch (nvmpinstrp) {
  12291. case FLASH_5720_EEPROM_HD:
  12292. case FLASH_5720_EEPROM_LD:
  12293. tp->nvram_jedecnum = JEDEC_ATMEL;
  12294. tg3_flag_set(tp, NVRAM_BUFFERED);
  12295. nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
  12296. tw32(NVRAM_CFG1, nvcfg1);
  12297. if (nvmpinstrp == FLASH_5720_EEPROM_HD)
  12298. tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
  12299. else
  12300. tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
  12301. return;
  12302. case FLASH_5720VENDOR_M_ATMEL_DB011D:
  12303. case FLASH_5720VENDOR_A_ATMEL_DB011B:
  12304. case FLASH_5720VENDOR_A_ATMEL_DB011D:
  12305. case FLASH_5720VENDOR_M_ATMEL_DB021D:
  12306. case FLASH_5720VENDOR_A_ATMEL_DB021B:
  12307. case FLASH_5720VENDOR_A_ATMEL_DB021D:
  12308. case FLASH_5720VENDOR_M_ATMEL_DB041D:
  12309. case FLASH_5720VENDOR_A_ATMEL_DB041B:
  12310. case FLASH_5720VENDOR_A_ATMEL_DB041D:
  12311. case FLASH_5720VENDOR_M_ATMEL_DB081D:
  12312. case FLASH_5720VENDOR_A_ATMEL_DB081D:
  12313. case FLASH_5720VENDOR_ATMEL_45USPT:
  12314. tp->nvram_jedecnum = JEDEC_ATMEL;
  12315. tg3_flag_set(tp, NVRAM_BUFFERED);
  12316. tg3_flag_set(tp, FLASH);
  12317. switch (nvmpinstrp) {
  12318. case FLASH_5720VENDOR_M_ATMEL_DB021D:
  12319. case FLASH_5720VENDOR_A_ATMEL_DB021B:
  12320. case FLASH_5720VENDOR_A_ATMEL_DB021D:
  12321. tp->nvram_size = TG3_NVRAM_SIZE_256KB;
  12322. break;
  12323. case FLASH_5720VENDOR_M_ATMEL_DB041D:
  12324. case FLASH_5720VENDOR_A_ATMEL_DB041B:
  12325. case FLASH_5720VENDOR_A_ATMEL_DB041D:
  12326. tp->nvram_size = TG3_NVRAM_SIZE_512KB;
  12327. break;
  12328. case FLASH_5720VENDOR_M_ATMEL_DB081D:
  12329. case FLASH_5720VENDOR_A_ATMEL_DB081D:
  12330. tp->nvram_size = TG3_NVRAM_SIZE_1MB;
  12331. break;
  12332. default:
  12333. if (tg3_asic_rev(tp) != ASIC_REV_5762)
  12334. tp->nvram_size = TG3_NVRAM_SIZE_128KB;
  12335. break;
  12336. }
  12337. break;
  12338. case FLASH_5720VENDOR_M_ST_M25PE10:
  12339. case FLASH_5720VENDOR_M_ST_M45PE10:
  12340. case FLASH_5720VENDOR_A_ST_M25PE10:
  12341. case FLASH_5720VENDOR_A_ST_M45PE10:
  12342. case FLASH_5720VENDOR_M_ST_M25PE20:
  12343. case FLASH_5720VENDOR_M_ST_M45PE20:
  12344. case FLASH_5720VENDOR_A_ST_M25PE20:
  12345. case FLASH_5720VENDOR_A_ST_M45PE20:
  12346. case FLASH_5720VENDOR_M_ST_M25PE40:
  12347. case FLASH_5720VENDOR_M_ST_M45PE40:
  12348. case FLASH_5720VENDOR_A_ST_M25PE40:
  12349. case FLASH_5720VENDOR_A_ST_M45PE40:
  12350. case FLASH_5720VENDOR_M_ST_M25PE80:
  12351. case FLASH_5720VENDOR_M_ST_M45PE80:
  12352. case FLASH_5720VENDOR_A_ST_M25PE80:
  12353. case FLASH_5720VENDOR_A_ST_M45PE80:
  12354. case FLASH_5720VENDOR_ST_25USPT:
  12355. case FLASH_5720VENDOR_ST_45USPT:
  12356. tp->nvram_jedecnum = JEDEC_ST;
  12357. tg3_flag_set(tp, NVRAM_BUFFERED);
  12358. tg3_flag_set(tp, FLASH);
  12359. switch (nvmpinstrp) {
  12360. case FLASH_5720VENDOR_M_ST_M25PE20:
  12361. case FLASH_5720VENDOR_M_ST_M45PE20:
  12362. case FLASH_5720VENDOR_A_ST_M25PE20:
  12363. case FLASH_5720VENDOR_A_ST_M45PE20:
  12364. tp->nvram_size = TG3_NVRAM_SIZE_256KB;
  12365. break;
  12366. case FLASH_5720VENDOR_M_ST_M25PE40:
  12367. case FLASH_5720VENDOR_M_ST_M45PE40:
  12368. case FLASH_5720VENDOR_A_ST_M25PE40:
  12369. case FLASH_5720VENDOR_A_ST_M45PE40:
  12370. tp->nvram_size = TG3_NVRAM_SIZE_512KB;
  12371. break;
  12372. case FLASH_5720VENDOR_M_ST_M25PE80:
  12373. case FLASH_5720VENDOR_M_ST_M45PE80:
  12374. case FLASH_5720VENDOR_A_ST_M25PE80:
  12375. case FLASH_5720VENDOR_A_ST_M45PE80:
  12376. tp->nvram_size = TG3_NVRAM_SIZE_1MB;
  12377. break;
  12378. default:
  12379. if (tg3_asic_rev(tp) != ASIC_REV_5762)
  12380. tp->nvram_size = TG3_NVRAM_SIZE_128KB;
  12381. break;
  12382. }
  12383. break;
  12384. default:
  12385. tg3_flag_set(tp, NO_NVRAM);
  12386. return;
  12387. }
  12388. tg3_nvram_get_pagesize(tp, nvcfg1);
  12389. if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
  12390. tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
  12391. if (tg3_asic_rev(tp) == ASIC_REV_5762) {
  12392. u32 val;
  12393. if (tg3_nvram_read(tp, 0, &val))
  12394. return;
  12395. if (val != TG3_EEPROM_MAGIC &&
  12396. (val & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW)
  12397. tg3_flag_set(tp, NO_NVRAM);
  12398. }
  12399. }
  12400. /* Chips other than 5700/5701 use the NVRAM for fetching info. */
  12401. static void tg3_nvram_init(struct tg3 *tp)
  12402. {
  12403. if (tg3_flag(tp, IS_SSB_CORE)) {
  12404. /* No NVRAM and EEPROM on the SSB Broadcom GigE core. */
  12405. tg3_flag_clear(tp, NVRAM);
  12406. tg3_flag_clear(tp, NVRAM_BUFFERED);
  12407. tg3_flag_set(tp, NO_NVRAM);
  12408. return;
  12409. }
  12410. tw32_f(GRC_EEPROM_ADDR,
  12411. (EEPROM_ADDR_FSM_RESET |
  12412. (EEPROM_DEFAULT_CLOCK_PERIOD <<
  12413. EEPROM_ADDR_CLKPERD_SHIFT)));
  12414. msleep(1);
  12415. /* Enable seeprom accesses. */
  12416. tw32_f(GRC_LOCAL_CTRL,
  12417. tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
  12418. udelay(100);
  12419. if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
  12420. tg3_asic_rev(tp) != ASIC_REV_5701) {
  12421. tg3_flag_set(tp, NVRAM);
  12422. if (tg3_nvram_lock(tp)) {
  12423. netdev_warn(tp->dev,
  12424. "Cannot get nvram lock, %s failed\n",
  12425. __func__);
  12426. return;
  12427. }
  12428. tg3_enable_nvram_access(tp);
  12429. tp->nvram_size = 0;
  12430. if (tg3_asic_rev(tp) == ASIC_REV_5752)
  12431. tg3_get_5752_nvram_info(tp);
  12432. else if (tg3_asic_rev(tp) == ASIC_REV_5755)
  12433. tg3_get_5755_nvram_info(tp);
  12434. else if (tg3_asic_rev(tp) == ASIC_REV_5787 ||
  12435. tg3_asic_rev(tp) == ASIC_REV_5784 ||
  12436. tg3_asic_rev(tp) == ASIC_REV_5785)
  12437. tg3_get_5787_nvram_info(tp);
  12438. else if (tg3_asic_rev(tp) == ASIC_REV_5761)
  12439. tg3_get_5761_nvram_info(tp);
  12440. else if (tg3_asic_rev(tp) == ASIC_REV_5906)
  12441. tg3_get_5906_nvram_info(tp);
  12442. else if (tg3_asic_rev(tp) == ASIC_REV_57780 ||
  12443. tg3_flag(tp, 57765_CLASS))
  12444. tg3_get_57780_nvram_info(tp);
  12445. else if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
  12446. tg3_asic_rev(tp) == ASIC_REV_5719)
  12447. tg3_get_5717_nvram_info(tp);
  12448. else if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
  12449. tg3_asic_rev(tp) == ASIC_REV_5762)
  12450. tg3_get_5720_nvram_info(tp);
  12451. else
  12452. tg3_get_nvram_info(tp);
  12453. if (tp->nvram_size == 0)
  12454. tg3_get_nvram_size(tp);
  12455. tg3_disable_nvram_access(tp);
  12456. tg3_nvram_unlock(tp);
  12457. } else {
  12458. tg3_flag_clear(tp, NVRAM);
  12459. tg3_flag_clear(tp, NVRAM_BUFFERED);
  12460. tg3_get_eeprom_size(tp);
  12461. }
  12462. }
  12463. struct subsys_tbl_ent {
  12464. u16 subsys_vendor, subsys_devid;
  12465. u32 phy_id;
  12466. };
  12467. static struct subsys_tbl_ent subsys_id_to_phy_id[] = {
  12468. /* Broadcom boards. */
  12469. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  12470. TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
  12471. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  12472. TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
  12473. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  12474. TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
  12475. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  12476. TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
  12477. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  12478. TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
  12479. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  12480. TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
  12481. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  12482. TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
  12483. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  12484. TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
  12485. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  12486. TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
  12487. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  12488. TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
  12489. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  12490. TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
  12491. /* 3com boards. */
  12492. { TG3PCI_SUBVENDOR_ID_3COM,
  12493. TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
  12494. { TG3PCI_SUBVENDOR_ID_3COM,
  12495. TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
  12496. { TG3PCI_SUBVENDOR_ID_3COM,
  12497. TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
  12498. { TG3PCI_SUBVENDOR_ID_3COM,
  12499. TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
  12500. { TG3PCI_SUBVENDOR_ID_3COM,
  12501. TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
  12502. /* DELL boards. */
  12503. { TG3PCI_SUBVENDOR_ID_DELL,
  12504. TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
  12505. { TG3PCI_SUBVENDOR_ID_DELL,
  12506. TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
  12507. { TG3PCI_SUBVENDOR_ID_DELL,
  12508. TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
  12509. { TG3PCI_SUBVENDOR_ID_DELL,
  12510. TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
  12511. /* Compaq boards. */
  12512. { TG3PCI_SUBVENDOR_ID_COMPAQ,
  12513. TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
  12514. { TG3PCI_SUBVENDOR_ID_COMPAQ,
  12515. TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
  12516. { TG3PCI_SUBVENDOR_ID_COMPAQ,
  12517. TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
  12518. { TG3PCI_SUBVENDOR_ID_COMPAQ,
  12519. TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
  12520. { TG3PCI_SUBVENDOR_ID_COMPAQ,
  12521. TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
  12522. /* IBM boards. */
  12523. { TG3PCI_SUBVENDOR_ID_IBM,
  12524. TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
  12525. };
  12526. static struct subsys_tbl_ent *tg3_lookup_by_subsys(struct tg3 *tp)
  12527. {
  12528. int i;
  12529. for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
  12530. if ((subsys_id_to_phy_id[i].subsys_vendor ==
  12531. tp->pdev->subsystem_vendor) &&
  12532. (subsys_id_to_phy_id[i].subsys_devid ==
  12533. tp->pdev->subsystem_device))
  12534. return &subsys_id_to_phy_id[i];
  12535. }
  12536. return NULL;
  12537. }
  12538. static void tg3_get_eeprom_hw_cfg(struct tg3 *tp)
  12539. {
  12540. u32 val;
  12541. tp->phy_id = TG3_PHY_ID_INVALID;
  12542. tp->led_ctrl = LED_CTRL_MODE_PHY_1;
  12543. /* Assume an onboard device and WOL capable by default. */
  12544. tg3_flag_set(tp, EEPROM_WRITE_PROT);
  12545. tg3_flag_set(tp, WOL_CAP);
  12546. if (tg3_asic_rev(tp) == ASIC_REV_5906) {
  12547. if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
  12548. tg3_flag_clear(tp, EEPROM_WRITE_PROT);
  12549. tg3_flag_set(tp, IS_NIC);
  12550. }
  12551. val = tr32(VCPU_CFGSHDW);
  12552. if (val & VCPU_CFGSHDW_ASPM_DBNC)
  12553. tg3_flag_set(tp, ASPM_WORKAROUND);
  12554. if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
  12555. (val & VCPU_CFGSHDW_WOL_MAGPKT)) {
  12556. tg3_flag_set(tp, WOL_ENABLE);
  12557. device_set_wakeup_enable(&tp->pdev->dev, true);
  12558. }
  12559. goto done;
  12560. }
  12561. tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
  12562. if (val == NIC_SRAM_DATA_SIG_MAGIC) {
  12563. u32 nic_cfg, led_cfg;
  12564. u32 cfg2 = 0, cfg4 = 0, cfg5 = 0;
  12565. u32 nic_phy_id, ver, eeprom_phy_id;
  12566. int eeprom_phy_serdes = 0;
  12567. tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
  12568. tp->nic_sram_data_cfg = nic_cfg;
  12569. tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
  12570. ver >>= NIC_SRAM_DATA_VER_SHIFT;
  12571. if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
  12572. tg3_asic_rev(tp) != ASIC_REV_5701 &&
  12573. tg3_asic_rev(tp) != ASIC_REV_5703 &&
  12574. (ver > 0) && (ver < 0x100))
  12575. tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
  12576. if (tg3_asic_rev(tp) == ASIC_REV_5785)
  12577. tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
  12578. if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
  12579. tg3_asic_rev(tp) == ASIC_REV_5719 ||
  12580. tg3_asic_rev(tp) == ASIC_REV_5720)
  12581. tg3_read_mem(tp, NIC_SRAM_DATA_CFG_5, &cfg5);
  12582. if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
  12583. NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
  12584. eeprom_phy_serdes = 1;
  12585. tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
  12586. if (nic_phy_id != 0) {
  12587. u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
  12588. u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
  12589. eeprom_phy_id = (id1 >> 16) << 10;
  12590. eeprom_phy_id |= (id2 & 0xfc00) << 16;
  12591. eeprom_phy_id |= (id2 & 0x03ff) << 0;
  12592. } else
  12593. eeprom_phy_id = 0;
  12594. tp->phy_id = eeprom_phy_id;
  12595. if (eeprom_phy_serdes) {
  12596. if (!tg3_flag(tp, 5705_PLUS))
  12597. tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
  12598. else
  12599. tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
  12600. }
  12601. if (tg3_flag(tp, 5750_PLUS))
  12602. led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
  12603. SHASTA_EXT_LED_MODE_MASK);
  12604. else
  12605. led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
  12606. switch (led_cfg) {
  12607. default:
  12608. case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
  12609. tp->led_ctrl = LED_CTRL_MODE_PHY_1;
  12610. break;
  12611. case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
  12612. tp->led_ctrl = LED_CTRL_MODE_PHY_2;
  12613. break;
  12614. case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
  12615. tp->led_ctrl = LED_CTRL_MODE_MAC;
  12616. /* Default to PHY_1_MODE if 0 (MAC_MODE) is
  12617. * read on some older 5700/5701 bootcode.
  12618. */
  12619. if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
  12620. tg3_asic_rev(tp) == ASIC_REV_5701)
  12621. tp->led_ctrl = LED_CTRL_MODE_PHY_1;
  12622. break;
  12623. case SHASTA_EXT_LED_SHARED:
  12624. tp->led_ctrl = LED_CTRL_MODE_SHARED;
  12625. if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0 &&
  12626. tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A1)
  12627. tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
  12628. LED_CTRL_MODE_PHY_2);
  12629. if (tg3_flag(tp, 5717_PLUS) ||
  12630. tg3_asic_rev(tp) == ASIC_REV_5762)
  12631. tp->led_ctrl |= LED_CTRL_BLINK_RATE_OVERRIDE |
  12632. LED_CTRL_BLINK_RATE_MASK;
  12633. break;
  12634. case SHASTA_EXT_LED_MAC:
  12635. tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
  12636. break;
  12637. case SHASTA_EXT_LED_COMBO:
  12638. tp->led_ctrl = LED_CTRL_MODE_COMBO;
  12639. if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0)
  12640. tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
  12641. LED_CTRL_MODE_PHY_2);
  12642. break;
  12643. }
  12644. if ((tg3_asic_rev(tp) == ASIC_REV_5700 ||
  12645. tg3_asic_rev(tp) == ASIC_REV_5701) &&
  12646. tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
  12647. tp->led_ctrl = LED_CTRL_MODE_PHY_2;
  12648. if (tg3_chip_rev(tp) == CHIPREV_5784_AX)
  12649. tp->led_ctrl = LED_CTRL_MODE_PHY_1;
  12650. if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
  12651. tg3_flag_set(tp, EEPROM_WRITE_PROT);
  12652. if ((tp->pdev->subsystem_vendor ==
  12653. PCI_VENDOR_ID_ARIMA) &&
  12654. (tp->pdev->subsystem_device == 0x205a ||
  12655. tp->pdev->subsystem_device == 0x2063))
  12656. tg3_flag_clear(tp, EEPROM_WRITE_PROT);
  12657. } else {
  12658. tg3_flag_clear(tp, EEPROM_WRITE_PROT);
  12659. tg3_flag_set(tp, IS_NIC);
  12660. }
  12661. if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
  12662. tg3_flag_set(tp, ENABLE_ASF);
  12663. if (tg3_flag(tp, 5750_PLUS))
  12664. tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
  12665. }
  12666. if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
  12667. tg3_flag(tp, 5750_PLUS))
  12668. tg3_flag_set(tp, ENABLE_APE);
  12669. if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
  12670. !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
  12671. tg3_flag_clear(tp, WOL_CAP);
  12672. if (tg3_flag(tp, WOL_CAP) &&
  12673. (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE)) {
  12674. tg3_flag_set(tp, WOL_ENABLE);
  12675. device_set_wakeup_enable(&tp->pdev->dev, true);
  12676. }
  12677. if (cfg2 & (1 << 17))
  12678. tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
  12679. /* serdes signal pre-emphasis in register 0x590 set by */
  12680. /* bootcode if bit 18 is set */
  12681. if (cfg2 & (1 << 18))
  12682. tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
  12683. if ((tg3_flag(tp, 57765_PLUS) ||
  12684. (tg3_asic_rev(tp) == ASIC_REV_5784 &&
  12685. tg3_chip_rev(tp) != CHIPREV_5784_AX)) &&
  12686. (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
  12687. tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
  12688. if (tg3_flag(tp, PCI_EXPRESS)) {
  12689. u32 cfg3;
  12690. tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
  12691. if (tg3_asic_rev(tp) != ASIC_REV_5785 &&
  12692. !tg3_flag(tp, 57765_PLUS) &&
  12693. (cfg3 & NIC_SRAM_ASPM_DEBOUNCE))
  12694. tg3_flag_set(tp, ASPM_WORKAROUND);
  12695. if (cfg3 & NIC_SRAM_LNK_FLAP_AVOID)
  12696. tp->phy_flags |= TG3_PHYFLG_KEEP_LINK_ON_PWRDN;
  12697. if (cfg3 & NIC_SRAM_1G_ON_VAUX_OK)
  12698. tp->phy_flags |= TG3_PHYFLG_1G_ON_VAUX_OK;
  12699. }
  12700. if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
  12701. tg3_flag_set(tp, RGMII_INBAND_DISABLE);
  12702. if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
  12703. tg3_flag_set(tp, RGMII_EXT_IBND_RX_EN);
  12704. if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
  12705. tg3_flag_set(tp, RGMII_EXT_IBND_TX_EN);
  12706. if (cfg5 & NIC_SRAM_DISABLE_1G_HALF_ADV)
  12707. tp->phy_flags |= TG3_PHYFLG_DISABLE_1G_HD_ADV;
  12708. }
  12709. done:
  12710. if (tg3_flag(tp, WOL_CAP))
  12711. device_set_wakeup_enable(&tp->pdev->dev,
  12712. tg3_flag(tp, WOL_ENABLE));
  12713. else
  12714. device_set_wakeup_capable(&tp->pdev->dev, false);
  12715. }
  12716. static int tg3_ape_otp_read(struct tg3 *tp, u32 offset, u32 *val)
  12717. {
  12718. int i, err;
  12719. u32 val2, off = offset * 8;
  12720. err = tg3_nvram_lock(tp);
  12721. if (err)
  12722. return err;
  12723. tg3_ape_write32(tp, TG3_APE_OTP_ADDR, off | APE_OTP_ADDR_CPU_ENABLE);
  12724. tg3_ape_write32(tp, TG3_APE_OTP_CTRL, APE_OTP_CTRL_PROG_EN |
  12725. APE_OTP_CTRL_CMD_RD | APE_OTP_CTRL_START);
  12726. tg3_ape_read32(tp, TG3_APE_OTP_CTRL);
  12727. udelay(10);
  12728. for (i = 0; i < 100; i++) {
  12729. val2 = tg3_ape_read32(tp, TG3_APE_OTP_STATUS);
  12730. if (val2 & APE_OTP_STATUS_CMD_DONE) {
  12731. *val = tg3_ape_read32(tp, TG3_APE_OTP_RD_DATA);
  12732. break;
  12733. }
  12734. udelay(10);
  12735. }
  12736. tg3_ape_write32(tp, TG3_APE_OTP_CTRL, 0);
  12737. tg3_nvram_unlock(tp);
  12738. if (val2 & APE_OTP_STATUS_CMD_DONE)
  12739. return 0;
  12740. return -EBUSY;
  12741. }
  12742. static int tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
  12743. {
  12744. int i;
  12745. u32 val;
  12746. tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
  12747. tw32(OTP_CTRL, cmd);
  12748. /* Wait for up to 1 ms for command to execute. */
  12749. for (i = 0; i < 100; i++) {
  12750. val = tr32(OTP_STATUS);
  12751. if (val & OTP_STATUS_CMD_DONE)
  12752. break;
  12753. udelay(10);
  12754. }
  12755. return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
  12756. }
  12757. /* Read the gphy configuration from the OTP region of the chip. The gphy
  12758. * configuration is a 32-bit value that straddles the alignment boundary.
  12759. * We do two 32-bit reads and then shift and merge the results.
  12760. */
  12761. static u32 tg3_read_otp_phycfg(struct tg3 *tp)
  12762. {
  12763. u32 bhalf_otp, thalf_otp;
  12764. tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
  12765. if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
  12766. return 0;
  12767. tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
  12768. if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
  12769. return 0;
  12770. thalf_otp = tr32(OTP_READ_DATA);
  12771. tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
  12772. if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
  12773. return 0;
  12774. bhalf_otp = tr32(OTP_READ_DATA);
  12775. return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
  12776. }
  12777. static void tg3_phy_init_link_config(struct tg3 *tp)
  12778. {
  12779. u32 adv = ADVERTISED_Autoneg;
  12780. if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
  12781. if (!(tp->phy_flags & TG3_PHYFLG_DISABLE_1G_HD_ADV))
  12782. adv |= ADVERTISED_1000baseT_Half;
  12783. adv |= ADVERTISED_1000baseT_Full;
  12784. }
  12785. if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
  12786. adv |= ADVERTISED_100baseT_Half |
  12787. ADVERTISED_100baseT_Full |
  12788. ADVERTISED_10baseT_Half |
  12789. ADVERTISED_10baseT_Full |
  12790. ADVERTISED_TP;
  12791. else
  12792. adv |= ADVERTISED_FIBRE;
  12793. tp->link_config.advertising = adv;
  12794. tp->link_config.speed = SPEED_UNKNOWN;
  12795. tp->link_config.duplex = DUPLEX_UNKNOWN;
  12796. tp->link_config.autoneg = AUTONEG_ENABLE;
  12797. tp->link_config.active_speed = SPEED_UNKNOWN;
  12798. tp->link_config.active_duplex = DUPLEX_UNKNOWN;
  12799. tp->old_link = -1;
  12800. }
  12801. static int tg3_phy_probe(struct tg3 *tp)
  12802. {
  12803. u32 hw_phy_id_1, hw_phy_id_2;
  12804. u32 hw_phy_id, hw_phy_id_masked;
  12805. int err;
  12806. /* flow control autonegotiation is default behavior */
  12807. tg3_flag_set(tp, PAUSE_AUTONEG);
  12808. tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
  12809. if (tg3_flag(tp, ENABLE_APE)) {
  12810. switch (tp->pci_fn) {
  12811. case 0:
  12812. tp->phy_ape_lock = TG3_APE_LOCK_PHY0;
  12813. break;
  12814. case 1:
  12815. tp->phy_ape_lock = TG3_APE_LOCK_PHY1;
  12816. break;
  12817. case 2:
  12818. tp->phy_ape_lock = TG3_APE_LOCK_PHY2;
  12819. break;
  12820. case 3:
  12821. tp->phy_ape_lock = TG3_APE_LOCK_PHY3;
  12822. break;
  12823. }
  12824. }
  12825. if (!tg3_flag(tp, ENABLE_ASF) &&
  12826. !(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
  12827. !(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
  12828. tp->phy_flags &= ~(TG3_PHYFLG_1G_ON_VAUX_OK |
  12829. TG3_PHYFLG_KEEP_LINK_ON_PWRDN);
  12830. if (tg3_flag(tp, USE_PHYLIB))
  12831. return tg3_phy_init(tp);
  12832. /* Reading the PHY ID register can conflict with ASF
  12833. * firmware access to the PHY hardware.
  12834. */
  12835. err = 0;
  12836. if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)) {
  12837. hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
  12838. } else {
  12839. /* Now read the physical PHY_ID from the chip and verify
  12840. * that it is sane. If it doesn't look good, we fall back
  12841. * to either the hard-coded table based PHY_ID and failing
  12842. * that the value found in the eeprom area.
  12843. */
  12844. err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
  12845. err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
  12846. hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
  12847. hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
  12848. hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
  12849. hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
  12850. }
  12851. if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
  12852. tp->phy_id = hw_phy_id;
  12853. if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
  12854. tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
  12855. else
  12856. tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
  12857. } else {
  12858. if (tp->phy_id != TG3_PHY_ID_INVALID) {
  12859. /* Do nothing, phy ID already set up in
  12860. * tg3_get_eeprom_hw_cfg().
  12861. */
  12862. } else {
  12863. struct subsys_tbl_ent *p;
  12864. /* No eeprom signature? Try the hardcoded
  12865. * subsys device table.
  12866. */
  12867. p = tg3_lookup_by_subsys(tp);
  12868. if (p) {
  12869. tp->phy_id = p->phy_id;
  12870. } else if (!tg3_flag(tp, IS_SSB_CORE)) {
  12871. /* For now we saw the IDs 0xbc050cd0,
  12872. * 0xbc050f80 and 0xbc050c30 on devices
  12873. * connected to an BCM4785 and there are
  12874. * probably more. Just assume that the phy is
  12875. * supported when it is connected to a SSB core
  12876. * for now.
  12877. */
  12878. return -ENODEV;
  12879. }
  12880. if (!tp->phy_id ||
  12881. tp->phy_id == TG3_PHY_ID_BCM8002)
  12882. tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
  12883. }
  12884. }
  12885. if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
  12886. (tg3_asic_rev(tp) == ASIC_REV_5719 ||
  12887. tg3_asic_rev(tp) == ASIC_REV_5720 ||
  12888. tg3_asic_rev(tp) == ASIC_REV_57766 ||
  12889. tg3_asic_rev(tp) == ASIC_REV_5762 ||
  12890. (tg3_asic_rev(tp) == ASIC_REV_5717 &&
  12891. tg3_chip_rev_id(tp) != CHIPREV_ID_5717_A0) ||
  12892. (tg3_asic_rev(tp) == ASIC_REV_57765 &&
  12893. tg3_chip_rev_id(tp) != CHIPREV_ID_57765_A0))) {
  12894. tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
  12895. tp->eee.supported = SUPPORTED_100baseT_Full |
  12896. SUPPORTED_1000baseT_Full;
  12897. tp->eee.advertised = ADVERTISED_100baseT_Full |
  12898. ADVERTISED_1000baseT_Full;
  12899. tp->eee.eee_enabled = 1;
  12900. tp->eee.tx_lpi_enabled = 1;
  12901. tp->eee.tx_lpi_timer = TG3_CPMU_DBTMR1_LNKIDLE_2047US;
  12902. }
  12903. tg3_phy_init_link_config(tp);
  12904. if (!(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN) &&
  12905. !(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
  12906. !tg3_flag(tp, ENABLE_APE) &&
  12907. !tg3_flag(tp, ENABLE_ASF)) {
  12908. u32 bmsr, dummy;
  12909. tg3_readphy(tp, MII_BMSR, &bmsr);
  12910. if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
  12911. (bmsr & BMSR_LSTATUS))
  12912. goto skip_phy_reset;
  12913. err = tg3_phy_reset(tp);
  12914. if (err)
  12915. return err;
  12916. tg3_phy_set_wirespeed(tp);
  12917. if (!tg3_phy_copper_an_config_ok(tp, &dummy)) {
  12918. tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
  12919. tp->link_config.flowctrl);
  12920. tg3_writephy(tp, MII_BMCR,
  12921. BMCR_ANENABLE | BMCR_ANRESTART);
  12922. }
  12923. }
  12924. skip_phy_reset:
  12925. if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
  12926. err = tg3_init_5401phy_dsp(tp);
  12927. if (err)
  12928. return err;
  12929. err = tg3_init_5401phy_dsp(tp);
  12930. }
  12931. return err;
  12932. }
  12933. static void tg3_read_vpd(struct tg3 *tp)
  12934. {
  12935. u8 *vpd_data;
  12936. unsigned int block_end, rosize, len;
  12937. u32 vpdlen;
  12938. int j, i = 0;
  12939. vpd_data = (u8 *)tg3_vpd_readblock(tp, &vpdlen);
  12940. if (!vpd_data)
  12941. goto out_no_vpd;
  12942. i = pci_vpd_find_tag(vpd_data, 0, vpdlen, PCI_VPD_LRDT_RO_DATA);
  12943. if (i < 0)
  12944. goto out_not_found;
  12945. rosize = pci_vpd_lrdt_size(&vpd_data[i]);
  12946. block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
  12947. i += PCI_VPD_LRDT_TAG_SIZE;
  12948. if (block_end > vpdlen)
  12949. goto out_not_found;
  12950. j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
  12951. PCI_VPD_RO_KEYWORD_MFR_ID);
  12952. if (j > 0) {
  12953. len = pci_vpd_info_field_size(&vpd_data[j]);
  12954. j += PCI_VPD_INFO_FLD_HDR_SIZE;
  12955. if (j + len > block_end || len != 4 ||
  12956. memcmp(&vpd_data[j], "1028", 4))
  12957. goto partno;
  12958. j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
  12959. PCI_VPD_RO_KEYWORD_VENDOR0);
  12960. if (j < 0)
  12961. goto partno;
  12962. len = pci_vpd_info_field_size(&vpd_data[j]);
  12963. j += PCI_VPD_INFO_FLD_HDR_SIZE;
  12964. if (j + len > block_end)
  12965. goto partno;
  12966. if (len >= sizeof(tp->fw_ver))
  12967. len = sizeof(tp->fw_ver) - 1;
  12968. memset(tp->fw_ver, 0, sizeof(tp->fw_ver));
  12969. snprintf(tp->fw_ver, sizeof(tp->fw_ver), "%.*s bc ", len,
  12970. &vpd_data[j]);
  12971. }
  12972. partno:
  12973. i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
  12974. PCI_VPD_RO_KEYWORD_PARTNO);
  12975. if (i < 0)
  12976. goto out_not_found;
  12977. len = pci_vpd_info_field_size(&vpd_data[i]);
  12978. i += PCI_VPD_INFO_FLD_HDR_SIZE;
  12979. if (len > TG3_BPN_SIZE ||
  12980. (len + i) > vpdlen)
  12981. goto out_not_found;
  12982. memcpy(tp->board_part_number, &vpd_data[i], len);
  12983. out_not_found:
  12984. kfree(vpd_data);
  12985. if (tp->board_part_number[0])
  12986. return;
  12987. out_no_vpd:
  12988. if (tg3_asic_rev(tp) == ASIC_REV_5717) {
  12989. if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
  12990. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C)
  12991. strcpy(tp->board_part_number, "BCM5717");
  12992. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
  12993. strcpy(tp->board_part_number, "BCM5718");
  12994. else
  12995. goto nomatch;
  12996. } else if (tg3_asic_rev(tp) == ASIC_REV_57780) {
  12997. if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
  12998. strcpy(tp->board_part_number, "BCM57780");
  12999. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
  13000. strcpy(tp->board_part_number, "BCM57760");
  13001. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
  13002. strcpy(tp->board_part_number, "BCM57790");
  13003. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
  13004. strcpy(tp->board_part_number, "BCM57788");
  13005. else
  13006. goto nomatch;
  13007. } else if (tg3_asic_rev(tp) == ASIC_REV_57765) {
  13008. if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
  13009. strcpy(tp->board_part_number, "BCM57761");
  13010. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
  13011. strcpy(tp->board_part_number, "BCM57765");
  13012. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
  13013. strcpy(tp->board_part_number, "BCM57781");
  13014. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
  13015. strcpy(tp->board_part_number, "BCM57785");
  13016. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
  13017. strcpy(tp->board_part_number, "BCM57791");
  13018. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
  13019. strcpy(tp->board_part_number, "BCM57795");
  13020. else
  13021. goto nomatch;
  13022. } else if (tg3_asic_rev(tp) == ASIC_REV_57766) {
  13023. if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762)
  13024. strcpy(tp->board_part_number, "BCM57762");
  13025. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766)
  13026. strcpy(tp->board_part_number, "BCM57766");
  13027. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782)
  13028. strcpy(tp->board_part_number, "BCM57782");
  13029. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
  13030. strcpy(tp->board_part_number, "BCM57786");
  13031. else
  13032. goto nomatch;
  13033. } else if (tg3_asic_rev(tp) == ASIC_REV_5906) {
  13034. strcpy(tp->board_part_number, "BCM95906");
  13035. } else {
  13036. nomatch:
  13037. strcpy(tp->board_part_number, "none");
  13038. }
  13039. }
  13040. static int tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
  13041. {
  13042. u32 val;
  13043. if (tg3_nvram_read(tp, offset, &val) ||
  13044. (val & 0xfc000000) != 0x0c000000 ||
  13045. tg3_nvram_read(tp, offset + 4, &val) ||
  13046. val != 0)
  13047. return 0;
  13048. return 1;
  13049. }
  13050. static void tg3_read_bc_ver(struct tg3 *tp)
  13051. {
  13052. u32 val, offset, start, ver_offset;
  13053. int i, dst_off;
  13054. bool newver = false;
  13055. if (tg3_nvram_read(tp, 0xc, &offset) ||
  13056. tg3_nvram_read(tp, 0x4, &start))
  13057. return;
  13058. offset = tg3_nvram_logical_addr(tp, offset);
  13059. if (tg3_nvram_read(tp, offset, &val))
  13060. return;
  13061. if ((val & 0xfc000000) == 0x0c000000) {
  13062. if (tg3_nvram_read(tp, offset + 4, &val))
  13063. return;
  13064. if (val == 0)
  13065. newver = true;
  13066. }
  13067. dst_off = strlen(tp->fw_ver);
  13068. if (newver) {
  13069. if (TG3_VER_SIZE - dst_off < 16 ||
  13070. tg3_nvram_read(tp, offset + 8, &ver_offset))
  13071. return;
  13072. offset = offset + ver_offset - start;
  13073. for (i = 0; i < 16; i += 4) {
  13074. __be32 v;
  13075. if (tg3_nvram_read_be32(tp, offset + i, &v))
  13076. return;
  13077. memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
  13078. }
  13079. } else {
  13080. u32 major, minor;
  13081. if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
  13082. return;
  13083. major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
  13084. TG3_NVM_BCVER_MAJSFT;
  13085. minor = ver_offset & TG3_NVM_BCVER_MINMSK;
  13086. snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
  13087. "v%d.%02d", major, minor);
  13088. }
  13089. }
  13090. static void tg3_read_hwsb_ver(struct tg3 *tp)
  13091. {
  13092. u32 val, major, minor;
  13093. /* Use native endian representation */
  13094. if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
  13095. return;
  13096. major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
  13097. TG3_NVM_HWSB_CFG1_MAJSFT;
  13098. minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
  13099. TG3_NVM_HWSB_CFG1_MINSFT;
  13100. snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
  13101. }
  13102. static void tg3_read_sb_ver(struct tg3 *tp, u32 val)
  13103. {
  13104. u32 offset, major, minor, build;
  13105. strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
  13106. if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
  13107. return;
  13108. switch (val & TG3_EEPROM_SB_REVISION_MASK) {
  13109. case TG3_EEPROM_SB_REVISION_0:
  13110. offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
  13111. break;
  13112. case TG3_EEPROM_SB_REVISION_2:
  13113. offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
  13114. break;
  13115. case TG3_EEPROM_SB_REVISION_3:
  13116. offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
  13117. break;
  13118. case TG3_EEPROM_SB_REVISION_4:
  13119. offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
  13120. break;
  13121. case TG3_EEPROM_SB_REVISION_5:
  13122. offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
  13123. break;
  13124. case TG3_EEPROM_SB_REVISION_6:
  13125. offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
  13126. break;
  13127. default:
  13128. return;
  13129. }
  13130. if (tg3_nvram_read(tp, offset, &val))
  13131. return;
  13132. build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
  13133. TG3_EEPROM_SB_EDH_BLD_SHFT;
  13134. major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
  13135. TG3_EEPROM_SB_EDH_MAJ_SHFT;
  13136. minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
  13137. if (minor > 99 || build > 26)
  13138. return;
  13139. offset = strlen(tp->fw_ver);
  13140. snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
  13141. " v%d.%02d", major, minor);
  13142. if (build > 0) {
  13143. offset = strlen(tp->fw_ver);
  13144. if (offset < TG3_VER_SIZE - 1)
  13145. tp->fw_ver[offset] = 'a' + build - 1;
  13146. }
  13147. }
  13148. static void tg3_read_mgmtfw_ver(struct tg3 *tp)
  13149. {
  13150. u32 val, offset, start;
  13151. int i, vlen;
  13152. for (offset = TG3_NVM_DIR_START;
  13153. offset < TG3_NVM_DIR_END;
  13154. offset += TG3_NVM_DIRENT_SIZE) {
  13155. if (tg3_nvram_read(tp, offset, &val))
  13156. return;
  13157. if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
  13158. break;
  13159. }
  13160. if (offset == TG3_NVM_DIR_END)
  13161. return;
  13162. if (!tg3_flag(tp, 5705_PLUS))
  13163. start = 0x08000000;
  13164. else if (tg3_nvram_read(tp, offset - 4, &start))
  13165. return;
  13166. if (tg3_nvram_read(tp, offset + 4, &offset) ||
  13167. !tg3_fw_img_is_valid(tp, offset) ||
  13168. tg3_nvram_read(tp, offset + 8, &val))
  13169. return;
  13170. offset += val - start;
  13171. vlen = strlen(tp->fw_ver);
  13172. tp->fw_ver[vlen++] = ',';
  13173. tp->fw_ver[vlen++] = ' ';
  13174. for (i = 0; i < 4; i++) {
  13175. __be32 v;
  13176. if (tg3_nvram_read_be32(tp, offset, &v))
  13177. return;
  13178. offset += sizeof(v);
  13179. if (vlen > TG3_VER_SIZE - sizeof(v)) {
  13180. memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
  13181. break;
  13182. }
  13183. memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
  13184. vlen += sizeof(v);
  13185. }
  13186. }
  13187. static void tg3_probe_ncsi(struct tg3 *tp)
  13188. {
  13189. u32 apedata;
  13190. apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
  13191. if (apedata != APE_SEG_SIG_MAGIC)
  13192. return;
  13193. apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
  13194. if (!(apedata & APE_FW_STATUS_READY))
  13195. return;
  13196. if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI)
  13197. tg3_flag_set(tp, APE_HAS_NCSI);
  13198. }
  13199. static void tg3_read_dash_ver(struct tg3 *tp)
  13200. {
  13201. int vlen;
  13202. u32 apedata;
  13203. char *fwtype;
  13204. apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
  13205. if (tg3_flag(tp, APE_HAS_NCSI))
  13206. fwtype = "NCSI";
  13207. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725)
  13208. fwtype = "SMASH";
  13209. else
  13210. fwtype = "DASH";
  13211. vlen = strlen(tp->fw_ver);
  13212. snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
  13213. fwtype,
  13214. (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
  13215. (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
  13216. (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
  13217. (apedata & APE_FW_VERSION_BLDMSK));
  13218. }
  13219. static void tg3_read_otp_ver(struct tg3 *tp)
  13220. {
  13221. u32 val, val2;
  13222. if (tg3_asic_rev(tp) != ASIC_REV_5762)
  13223. return;
  13224. if (!tg3_ape_otp_read(tp, OTP_ADDRESS_MAGIC0, &val) &&
  13225. !tg3_ape_otp_read(tp, OTP_ADDRESS_MAGIC0 + 4, &val2) &&
  13226. TG3_OTP_MAGIC0_VALID(val)) {
  13227. u64 val64 = (u64) val << 32 | val2;
  13228. u32 ver = 0;
  13229. int i, vlen;
  13230. for (i = 0; i < 7; i++) {
  13231. if ((val64 & 0xff) == 0)
  13232. break;
  13233. ver = val64 & 0xff;
  13234. val64 >>= 8;
  13235. }
  13236. vlen = strlen(tp->fw_ver);
  13237. snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " .%02d", ver);
  13238. }
  13239. }
  13240. static void tg3_read_fw_ver(struct tg3 *tp)
  13241. {
  13242. u32 val;
  13243. bool vpd_vers = false;
  13244. if (tp->fw_ver[0] != 0)
  13245. vpd_vers = true;
  13246. if (tg3_flag(tp, NO_NVRAM)) {
  13247. strcat(tp->fw_ver, "sb");
  13248. tg3_read_otp_ver(tp);
  13249. return;
  13250. }
  13251. if (tg3_nvram_read(tp, 0, &val))
  13252. return;
  13253. if (val == TG3_EEPROM_MAGIC)
  13254. tg3_read_bc_ver(tp);
  13255. else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
  13256. tg3_read_sb_ver(tp, val);
  13257. else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
  13258. tg3_read_hwsb_ver(tp);
  13259. if (tg3_flag(tp, ENABLE_ASF)) {
  13260. if (tg3_flag(tp, ENABLE_APE)) {
  13261. tg3_probe_ncsi(tp);
  13262. if (!vpd_vers)
  13263. tg3_read_dash_ver(tp);
  13264. } else if (!vpd_vers) {
  13265. tg3_read_mgmtfw_ver(tp);
  13266. }
  13267. }
  13268. tp->fw_ver[TG3_VER_SIZE - 1] = 0;
  13269. }
  13270. static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
  13271. {
  13272. if (tg3_flag(tp, LRG_PROD_RING_CAP))
  13273. return TG3_RX_RET_MAX_SIZE_5717;
  13274. else if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))
  13275. return TG3_RX_RET_MAX_SIZE_5700;
  13276. else
  13277. return TG3_RX_RET_MAX_SIZE_5705;
  13278. }
  13279. static const struct pci_device_id tg3_write_reorder_chipsets[] = {
  13280. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
  13281. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
  13282. { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
  13283. { },
  13284. };
  13285. static struct pci_dev *tg3_find_peer(struct tg3 *tp)
  13286. {
  13287. struct pci_dev *peer;
  13288. unsigned int func, devnr = tp->pdev->devfn & ~7;
  13289. for (func = 0; func < 8; func++) {
  13290. peer = pci_get_slot(tp->pdev->bus, devnr | func);
  13291. if (peer && peer != tp->pdev)
  13292. break;
  13293. pci_dev_put(peer);
  13294. }
  13295. /* 5704 can be configured in single-port mode, set peer to
  13296. * tp->pdev in that case.
  13297. */
  13298. if (!peer) {
  13299. peer = tp->pdev;
  13300. return peer;
  13301. }
  13302. /*
  13303. * We don't need to keep the refcount elevated; there's no way
  13304. * to remove one half of this device without removing the other
  13305. */
  13306. pci_dev_put(peer);
  13307. return peer;
  13308. }
  13309. static void tg3_detect_asic_rev(struct tg3 *tp, u32 misc_ctrl_reg)
  13310. {
  13311. tp->pci_chip_rev_id = misc_ctrl_reg >> MISC_HOST_CTRL_CHIPREV_SHIFT;
  13312. if (tg3_asic_rev(tp) == ASIC_REV_USE_PROD_ID_REG) {
  13313. u32 reg;
  13314. /* All devices that use the alternate
  13315. * ASIC REV location have a CPMU.
  13316. */
  13317. tg3_flag_set(tp, CPMU_PRESENT);
  13318. if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
  13319. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C ||
  13320. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
  13321. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
  13322. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720 ||
  13323. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57767 ||
  13324. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57764 ||
  13325. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5762 ||
  13326. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725 ||
  13327. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5727 ||
  13328. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57787)
  13329. reg = TG3PCI_GEN2_PRODID_ASICREV;
  13330. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
  13331. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
  13332. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
  13333. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
  13334. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
  13335. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
  13336. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762 ||
  13337. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766 ||
  13338. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782 ||
  13339. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
  13340. reg = TG3PCI_GEN15_PRODID_ASICREV;
  13341. else
  13342. reg = TG3PCI_PRODID_ASICREV;
  13343. pci_read_config_dword(tp->pdev, reg, &tp->pci_chip_rev_id);
  13344. }
  13345. /* Wrong chip ID in 5752 A0. This code can be removed later
  13346. * as A0 is not in production.
  13347. */
  13348. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5752_A0_HW)
  13349. tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
  13350. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5717_C0)
  13351. tp->pci_chip_rev_id = CHIPREV_ID_5720_A0;
  13352. if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
  13353. tg3_asic_rev(tp) == ASIC_REV_5719 ||
  13354. tg3_asic_rev(tp) == ASIC_REV_5720)
  13355. tg3_flag_set(tp, 5717_PLUS);
  13356. if (tg3_asic_rev(tp) == ASIC_REV_57765 ||
  13357. tg3_asic_rev(tp) == ASIC_REV_57766)
  13358. tg3_flag_set(tp, 57765_CLASS);
  13359. if (tg3_flag(tp, 57765_CLASS) || tg3_flag(tp, 5717_PLUS) ||
  13360. tg3_asic_rev(tp) == ASIC_REV_5762)
  13361. tg3_flag_set(tp, 57765_PLUS);
  13362. /* Intentionally exclude ASIC_REV_5906 */
  13363. if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
  13364. tg3_asic_rev(tp) == ASIC_REV_5787 ||
  13365. tg3_asic_rev(tp) == ASIC_REV_5784 ||
  13366. tg3_asic_rev(tp) == ASIC_REV_5761 ||
  13367. tg3_asic_rev(tp) == ASIC_REV_5785 ||
  13368. tg3_asic_rev(tp) == ASIC_REV_57780 ||
  13369. tg3_flag(tp, 57765_PLUS))
  13370. tg3_flag_set(tp, 5755_PLUS);
  13371. if (tg3_asic_rev(tp) == ASIC_REV_5780 ||
  13372. tg3_asic_rev(tp) == ASIC_REV_5714)
  13373. tg3_flag_set(tp, 5780_CLASS);
  13374. if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
  13375. tg3_asic_rev(tp) == ASIC_REV_5752 ||
  13376. tg3_asic_rev(tp) == ASIC_REV_5906 ||
  13377. tg3_flag(tp, 5755_PLUS) ||
  13378. tg3_flag(tp, 5780_CLASS))
  13379. tg3_flag_set(tp, 5750_PLUS);
  13380. if (tg3_asic_rev(tp) == ASIC_REV_5705 ||
  13381. tg3_flag(tp, 5750_PLUS))
  13382. tg3_flag_set(tp, 5705_PLUS);
  13383. }
  13384. static bool tg3_10_100_only_device(struct tg3 *tp,
  13385. const struct pci_device_id *ent)
  13386. {
  13387. u32 grc_misc_cfg = tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK;
  13388. if ((tg3_asic_rev(tp) == ASIC_REV_5703 &&
  13389. (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
  13390. (tp->phy_flags & TG3_PHYFLG_IS_FET))
  13391. return true;
  13392. if (ent->driver_data & TG3_DRV_DATA_FLAG_10_100_ONLY) {
  13393. if (tg3_asic_rev(tp) == ASIC_REV_5705) {
  13394. if (ent->driver_data & TG3_DRV_DATA_FLAG_5705_10_100)
  13395. return true;
  13396. } else {
  13397. return true;
  13398. }
  13399. }
  13400. return false;
  13401. }
  13402. static int tg3_get_invariants(struct tg3 *tp, const struct pci_device_id *ent)
  13403. {
  13404. u32 misc_ctrl_reg;
  13405. u32 pci_state_reg, grc_misc_cfg;
  13406. u32 val;
  13407. u16 pci_cmd;
  13408. int err;
  13409. /* Force memory write invalidate off. If we leave it on,
  13410. * then on 5700_BX chips we have to enable a workaround.
  13411. * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
  13412. * to match the cacheline size. The Broadcom driver have this
  13413. * workaround but turns MWI off all the times so never uses
  13414. * it. This seems to suggest that the workaround is insufficient.
  13415. */
  13416. pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
  13417. pci_cmd &= ~PCI_COMMAND_INVALIDATE;
  13418. pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
  13419. /* Important! -- Make sure register accesses are byteswapped
  13420. * correctly. Also, for those chips that require it, make
  13421. * sure that indirect register accesses are enabled before
  13422. * the first operation.
  13423. */
  13424. pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
  13425. &misc_ctrl_reg);
  13426. tp->misc_host_ctrl |= (misc_ctrl_reg &
  13427. MISC_HOST_CTRL_CHIPREV);
  13428. pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
  13429. tp->misc_host_ctrl);
  13430. tg3_detect_asic_rev(tp, misc_ctrl_reg);
  13431. /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
  13432. * we need to disable memory and use config. cycles
  13433. * only to access all registers. The 5702/03 chips
  13434. * can mistakenly decode the special cycles from the
  13435. * ICH chipsets as memory write cycles, causing corruption
  13436. * of register and memory space. Only certain ICH bridges
  13437. * will drive special cycles with non-zero data during the
  13438. * address phase which can fall within the 5703's address
  13439. * range. This is not an ICH bug as the PCI spec allows
  13440. * non-zero address during special cycles. However, only
  13441. * these ICH bridges are known to drive non-zero addresses
  13442. * during special cycles.
  13443. *
  13444. * Since special cycles do not cross PCI bridges, we only
  13445. * enable this workaround if the 5703 is on the secondary
  13446. * bus of these ICH bridges.
  13447. */
  13448. if ((tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A1) ||
  13449. (tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A2)) {
  13450. static struct tg3_dev_id {
  13451. u32 vendor;
  13452. u32 device;
  13453. u32 rev;
  13454. } ich_chipsets[] = {
  13455. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
  13456. PCI_ANY_ID },
  13457. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
  13458. PCI_ANY_ID },
  13459. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
  13460. 0xa },
  13461. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
  13462. PCI_ANY_ID },
  13463. { },
  13464. };
  13465. struct tg3_dev_id *pci_id = &ich_chipsets[0];
  13466. struct pci_dev *bridge = NULL;
  13467. while (pci_id->vendor != 0) {
  13468. bridge = pci_get_device(pci_id->vendor, pci_id->device,
  13469. bridge);
  13470. if (!bridge) {
  13471. pci_id++;
  13472. continue;
  13473. }
  13474. if (pci_id->rev != PCI_ANY_ID) {
  13475. if (bridge->revision > pci_id->rev)
  13476. continue;
  13477. }
  13478. if (bridge->subordinate &&
  13479. (bridge->subordinate->number ==
  13480. tp->pdev->bus->number)) {
  13481. tg3_flag_set(tp, ICH_WORKAROUND);
  13482. pci_dev_put(bridge);
  13483. break;
  13484. }
  13485. }
  13486. }
  13487. if (tg3_asic_rev(tp) == ASIC_REV_5701) {
  13488. static struct tg3_dev_id {
  13489. u32 vendor;
  13490. u32 device;
  13491. } bridge_chipsets[] = {
  13492. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
  13493. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
  13494. { },
  13495. };
  13496. struct tg3_dev_id *pci_id = &bridge_chipsets[0];
  13497. struct pci_dev *bridge = NULL;
  13498. while (pci_id->vendor != 0) {
  13499. bridge = pci_get_device(pci_id->vendor,
  13500. pci_id->device,
  13501. bridge);
  13502. if (!bridge) {
  13503. pci_id++;
  13504. continue;
  13505. }
  13506. if (bridge->subordinate &&
  13507. (bridge->subordinate->number <=
  13508. tp->pdev->bus->number) &&
  13509. (bridge->subordinate->busn_res.end >=
  13510. tp->pdev->bus->number)) {
  13511. tg3_flag_set(tp, 5701_DMA_BUG);
  13512. pci_dev_put(bridge);
  13513. break;
  13514. }
  13515. }
  13516. }
  13517. /* The EPB bridge inside 5714, 5715, and 5780 cannot support
  13518. * DMA addresses > 40-bit. This bridge may have other additional
  13519. * 57xx devices behind it in some 4-port NIC designs for example.
  13520. * Any tg3 device found behind the bridge will also need the 40-bit
  13521. * DMA workaround.
  13522. */
  13523. if (tg3_flag(tp, 5780_CLASS)) {
  13524. tg3_flag_set(tp, 40BIT_DMA_BUG);
  13525. tp->msi_cap = tp->pdev->msi_cap;
  13526. } else {
  13527. struct pci_dev *bridge = NULL;
  13528. do {
  13529. bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
  13530. PCI_DEVICE_ID_SERVERWORKS_EPB,
  13531. bridge);
  13532. if (bridge && bridge->subordinate &&
  13533. (bridge->subordinate->number <=
  13534. tp->pdev->bus->number) &&
  13535. (bridge->subordinate->busn_res.end >=
  13536. tp->pdev->bus->number)) {
  13537. tg3_flag_set(tp, 40BIT_DMA_BUG);
  13538. pci_dev_put(bridge);
  13539. break;
  13540. }
  13541. } while (bridge);
  13542. }
  13543. if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
  13544. tg3_asic_rev(tp) == ASIC_REV_5714)
  13545. tp->pdev_peer = tg3_find_peer(tp);
  13546. /* Determine TSO capabilities */
  13547. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0)
  13548. ; /* Do nothing. HW bug. */
  13549. else if (tg3_flag(tp, 57765_PLUS))
  13550. tg3_flag_set(tp, HW_TSO_3);
  13551. else if (tg3_flag(tp, 5755_PLUS) ||
  13552. tg3_asic_rev(tp) == ASIC_REV_5906)
  13553. tg3_flag_set(tp, HW_TSO_2);
  13554. else if (tg3_flag(tp, 5750_PLUS)) {
  13555. tg3_flag_set(tp, HW_TSO_1);
  13556. tg3_flag_set(tp, TSO_BUG);
  13557. if (tg3_asic_rev(tp) == ASIC_REV_5750 &&
  13558. tg3_chip_rev_id(tp) >= CHIPREV_ID_5750_C2)
  13559. tg3_flag_clear(tp, TSO_BUG);
  13560. } else if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
  13561. tg3_asic_rev(tp) != ASIC_REV_5701 &&
  13562. tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
  13563. tg3_flag_set(tp, FW_TSO);
  13564. tg3_flag_set(tp, TSO_BUG);
  13565. if (tg3_asic_rev(tp) == ASIC_REV_5705)
  13566. tp->fw_needed = FIRMWARE_TG3TSO5;
  13567. else
  13568. tp->fw_needed = FIRMWARE_TG3TSO;
  13569. }
  13570. /* Selectively allow TSO based on operating conditions */
  13571. if (tg3_flag(tp, HW_TSO_1) ||
  13572. tg3_flag(tp, HW_TSO_2) ||
  13573. tg3_flag(tp, HW_TSO_3) ||
  13574. tg3_flag(tp, FW_TSO)) {
  13575. /* For firmware TSO, assume ASF is disabled.
  13576. * We'll disable TSO later if we discover ASF
  13577. * is enabled in tg3_get_eeprom_hw_cfg().
  13578. */
  13579. tg3_flag_set(tp, TSO_CAPABLE);
  13580. } else {
  13581. tg3_flag_clear(tp, TSO_CAPABLE);
  13582. tg3_flag_clear(tp, TSO_BUG);
  13583. tp->fw_needed = NULL;
  13584. }
  13585. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0)
  13586. tp->fw_needed = FIRMWARE_TG3;
  13587. if (tg3_asic_rev(tp) == ASIC_REV_57766)
  13588. tp->fw_needed = FIRMWARE_TG357766;
  13589. tp->irq_max = 1;
  13590. if (tg3_flag(tp, 5750_PLUS)) {
  13591. tg3_flag_set(tp, SUPPORT_MSI);
  13592. if (tg3_chip_rev(tp) == CHIPREV_5750_AX ||
  13593. tg3_chip_rev(tp) == CHIPREV_5750_BX ||
  13594. (tg3_asic_rev(tp) == ASIC_REV_5714 &&
  13595. tg3_chip_rev_id(tp) <= CHIPREV_ID_5714_A2 &&
  13596. tp->pdev_peer == tp->pdev))
  13597. tg3_flag_clear(tp, SUPPORT_MSI);
  13598. if (tg3_flag(tp, 5755_PLUS) ||
  13599. tg3_asic_rev(tp) == ASIC_REV_5906) {
  13600. tg3_flag_set(tp, 1SHOT_MSI);
  13601. }
  13602. if (tg3_flag(tp, 57765_PLUS)) {
  13603. tg3_flag_set(tp, SUPPORT_MSIX);
  13604. tp->irq_max = TG3_IRQ_MAX_VECS;
  13605. }
  13606. }
  13607. tp->txq_max = 1;
  13608. tp->rxq_max = 1;
  13609. if (tp->irq_max > 1) {
  13610. tp->rxq_max = TG3_RSS_MAX_NUM_QS;
  13611. tg3_rss_init_dflt_indir_tbl(tp, TG3_RSS_MAX_NUM_QS);
  13612. if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
  13613. tg3_asic_rev(tp) == ASIC_REV_5720)
  13614. tp->txq_max = tp->irq_max - 1;
  13615. }
  13616. if (tg3_flag(tp, 5755_PLUS) ||
  13617. tg3_asic_rev(tp) == ASIC_REV_5906)
  13618. tg3_flag_set(tp, SHORT_DMA_BUG);
  13619. if (tg3_asic_rev(tp) == ASIC_REV_5719)
  13620. tp->dma_limit = TG3_TX_BD_DMA_MAX_4K;
  13621. if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
  13622. tg3_asic_rev(tp) == ASIC_REV_5719 ||
  13623. tg3_asic_rev(tp) == ASIC_REV_5720 ||
  13624. tg3_asic_rev(tp) == ASIC_REV_5762)
  13625. tg3_flag_set(tp, LRG_PROD_RING_CAP);
  13626. if (tg3_flag(tp, 57765_PLUS) &&
  13627. tg3_chip_rev_id(tp) != CHIPREV_ID_5719_A0)
  13628. tg3_flag_set(tp, USE_JUMBO_BDFLAG);
  13629. if (!tg3_flag(tp, 5705_PLUS) ||
  13630. tg3_flag(tp, 5780_CLASS) ||
  13631. tg3_flag(tp, USE_JUMBO_BDFLAG))
  13632. tg3_flag_set(tp, JUMBO_CAPABLE);
  13633. pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
  13634. &pci_state_reg);
  13635. if (pci_is_pcie(tp->pdev)) {
  13636. u16 lnkctl;
  13637. tg3_flag_set(tp, PCI_EXPRESS);
  13638. pcie_capability_read_word(tp->pdev, PCI_EXP_LNKCTL, &lnkctl);
  13639. if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
  13640. if (tg3_asic_rev(tp) == ASIC_REV_5906) {
  13641. tg3_flag_clear(tp, HW_TSO_2);
  13642. tg3_flag_clear(tp, TSO_CAPABLE);
  13643. }
  13644. if (tg3_asic_rev(tp) == ASIC_REV_5784 ||
  13645. tg3_asic_rev(tp) == ASIC_REV_5761 ||
  13646. tg3_chip_rev_id(tp) == CHIPREV_ID_57780_A0 ||
  13647. tg3_chip_rev_id(tp) == CHIPREV_ID_57780_A1)
  13648. tg3_flag_set(tp, CLKREQ_BUG);
  13649. } else if (tg3_chip_rev_id(tp) == CHIPREV_ID_5717_A0) {
  13650. tg3_flag_set(tp, L1PLLPD_EN);
  13651. }
  13652. } else if (tg3_asic_rev(tp) == ASIC_REV_5785) {
  13653. /* BCM5785 devices are effectively PCIe devices, and should
  13654. * follow PCIe codepaths, but do not have a PCIe capabilities
  13655. * section.
  13656. */
  13657. tg3_flag_set(tp, PCI_EXPRESS);
  13658. } else if (!tg3_flag(tp, 5705_PLUS) ||
  13659. tg3_flag(tp, 5780_CLASS)) {
  13660. tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
  13661. if (!tp->pcix_cap) {
  13662. dev_err(&tp->pdev->dev,
  13663. "Cannot find PCI-X capability, aborting\n");
  13664. return -EIO;
  13665. }
  13666. if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
  13667. tg3_flag_set(tp, PCIX_MODE);
  13668. }
  13669. /* If we have an AMD 762 or VIA K8T800 chipset, write
  13670. * reordering to the mailbox registers done by the host
  13671. * controller can cause major troubles. We read back from
  13672. * every mailbox register write to force the writes to be
  13673. * posted to the chip in order.
  13674. */
  13675. if (pci_dev_present(tg3_write_reorder_chipsets) &&
  13676. !tg3_flag(tp, PCI_EXPRESS))
  13677. tg3_flag_set(tp, MBOX_WRITE_REORDER);
  13678. pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
  13679. &tp->pci_cacheline_sz);
  13680. pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
  13681. &tp->pci_lat_timer);
  13682. if (tg3_asic_rev(tp) == ASIC_REV_5703 &&
  13683. tp->pci_lat_timer < 64) {
  13684. tp->pci_lat_timer = 64;
  13685. pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
  13686. tp->pci_lat_timer);
  13687. }
  13688. /* Important! -- It is critical that the PCI-X hw workaround
  13689. * situation is decided before the first MMIO register access.
  13690. */
  13691. if (tg3_chip_rev(tp) == CHIPREV_5700_BX) {
  13692. /* 5700 BX chips need to have their TX producer index
  13693. * mailboxes written twice to workaround a bug.
  13694. */
  13695. tg3_flag_set(tp, TXD_MBOX_HWBUG);
  13696. /* If we are in PCI-X mode, enable register write workaround.
  13697. *
  13698. * The workaround is to use indirect register accesses
  13699. * for all chip writes not to mailbox registers.
  13700. */
  13701. if (tg3_flag(tp, PCIX_MODE)) {
  13702. u32 pm_reg;
  13703. tg3_flag_set(tp, PCIX_TARGET_HWBUG);
  13704. /* The chip can have it's power management PCI config
  13705. * space registers clobbered due to this bug.
  13706. * So explicitly force the chip into D0 here.
  13707. */
  13708. pci_read_config_dword(tp->pdev,
  13709. tp->pdev->pm_cap + PCI_PM_CTRL,
  13710. &pm_reg);
  13711. pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
  13712. pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
  13713. pci_write_config_dword(tp->pdev,
  13714. tp->pdev->pm_cap + PCI_PM_CTRL,
  13715. pm_reg);
  13716. /* Also, force SERR#/PERR# in PCI command. */
  13717. pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
  13718. pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
  13719. pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
  13720. }
  13721. }
  13722. if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
  13723. tg3_flag_set(tp, PCI_HIGH_SPEED);
  13724. if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
  13725. tg3_flag_set(tp, PCI_32BIT);
  13726. /* Chip-specific fixup from Broadcom driver */
  13727. if ((tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0) &&
  13728. (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
  13729. pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
  13730. pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
  13731. }
  13732. /* Default fast path register access methods */
  13733. tp->read32 = tg3_read32;
  13734. tp->write32 = tg3_write32;
  13735. tp->read32_mbox = tg3_read32;
  13736. tp->write32_mbox = tg3_write32;
  13737. tp->write32_tx_mbox = tg3_write32;
  13738. tp->write32_rx_mbox = tg3_write32;
  13739. /* Various workaround register access methods */
  13740. if (tg3_flag(tp, PCIX_TARGET_HWBUG))
  13741. tp->write32 = tg3_write_indirect_reg32;
  13742. else if (tg3_asic_rev(tp) == ASIC_REV_5701 ||
  13743. (tg3_flag(tp, PCI_EXPRESS) &&
  13744. tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A0)) {
  13745. /*
  13746. * Back to back register writes can cause problems on these
  13747. * chips, the workaround is to read back all reg writes
  13748. * except those to mailbox regs.
  13749. *
  13750. * See tg3_write_indirect_reg32().
  13751. */
  13752. tp->write32 = tg3_write_flush_reg32;
  13753. }
  13754. if (tg3_flag(tp, TXD_MBOX_HWBUG) || tg3_flag(tp, MBOX_WRITE_REORDER)) {
  13755. tp->write32_tx_mbox = tg3_write32_tx_mbox;
  13756. if (tg3_flag(tp, MBOX_WRITE_REORDER))
  13757. tp->write32_rx_mbox = tg3_write_flush_reg32;
  13758. }
  13759. if (tg3_flag(tp, ICH_WORKAROUND)) {
  13760. tp->read32 = tg3_read_indirect_reg32;
  13761. tp->write32 = tg3_write_indirect_reg32;
  13762. tp->read32_mbox = tg3_read_indirect_mbox;
  13763. tp->write32_mbox = tg3_write_indirect_mbox;
  13764. tp->write32_tx_mbox = tg3_write_indirect_mbox;
  13765. tp->write32_rx_mbox = tg3_write_indirect_mbox;
  13766. iounmap(tp->regs);
  13767. tp->regs = NULL;
  13768. pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
  13769. pci_cmd &= ~PCI_COMMAND_MEMORY;
  13770. pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
  13771. }
  13772. if (tg3_asic_rev(tp) == ASIC_REV_5906) {
  13773. tp->read32_mbox = tg3_read32_mbox_5906;
  13774. tp->write32_mbox = tg3_write32_mbox_5906;
  13775. tp->write32_tx_mbox = tg3_write32_mbox_5906;
  13776. tp->write32_rx_mbox = tg3_write32_mbox_5906;
  13777. }
  13778. if (tp->write32 == tg3_write_indirect_reg32 ||
  13779. (tg3_flag(tp, PCIX_MODE) &&
  13780. (tg3_asic_rev(tp) == ASIC_REV_5700 ||
  13781. tg3_asic_rev(tp) == ASIC_REV_5701)))
  13782. tg3_flag_set(tp, SRAM_USE_CONFIG);
  13783. /* The memory arbiter has to be enabled in order for SRAM accesses
  13784. * to succeed. Normally on powerup the tg3 chip firmware will make
  13785. * sure it is enabled, but other entities such as system netboot
  13786. * code might disable it.
  13787. */
  13788. val = tr32(MEMARB_MODE);
  13789. tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
  13790. tp->pci_fn = PCI_FUNC(tp->pdev->devfn) & 3;
  13791. if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
  13792. tg3_flag(tp, 5780_CLASS)) {
  13793. if (tg3_flag(tp, PCIX_MODE)) {
  13794. pci_read_config_dword(tp->pdev,
  13795. tp->pcix_cap + PCI_X_STATUS,
  13796. &val);
  13797. tp->pci_fn = val & 0x7;
  13798. }
  13799. } else if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
  13800. tg3_asic_rev(tp) == ASIC_REV_5719 ||
  13801. tg3_asic_rev(tp) == ASIC_REV_5720) {
  13802. tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
  13803. if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) != NIC_SRAM_CPMUSTAT_SIG)
  13804. val = tr32(TG3_CPMU_STATUS);
  13805. if (tg3_asic_rev(tp) == ASIC_REV_5717)
  13806. tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5717) ? 1 : 0;
  13807. else
  13808. tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5719) >>
  13809. TG3_CPMU_STATUS_FSHFT_5719;
  13810. }
  13811. if (tg3_flag(tp, FLUSH_POSTED_WRITES)) {
  13812. tp->write32_tx_mbox = tg3_write_flush_reg32;
  13813. tp->write32_rx_mbox = tg3_write_flush_reg32;
  13814. }
  13815. /* Get eeprom hw config before calling tg3_set_power_state().
  13816. * In particular, the TG3_FLAG_IS_NIC flag must be
  13817. * determined before calling tg3_set_power_state() so that
  13818. * we know whether or not to switch out of Vaux power.
  13819. * When the flag is set, it means that GPIO1 is used for eeprom
  13820. * write protect and also implies that it is a LOM where GPIOs
  13821. * are not used to switch power.
  13822. */
  13823. tg3_get_eeprom_hw_cfg(tp);
  13824. if (tg3_flag(tp, FW_TSO) && tg3_flag(tp, ENABLE_ASF)) {
  13825. tg3_flag_clear(tp, TSO_CAPABLE);
  13826. tg3_flag_clear(tp, TSO_BUG);
  13827. tp->fw_needed = NULL;
  13828. }
  13829. if (tg3_flag(tp, ENABLE_APE)) {
  13830. /* Allow reads and writes to the
  13831. * APE register and memory space.
  13832. */
  13833. pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
  13834. PCISTATE_ALLOW_APE_SHMEM_WR |
  13835. PCISTATE_ALLOW_APE_PSPACE_WR;
  13836. pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
  13837. pci_state_reg);
  13838. tg3_ape_lock_init(tp);
  13839. tp->ape_hb_interval =
  13840. msecs_to_jiffies(APE_HOST_HEARTBEAT_INT_5SEC);
  13841. }
  13842. /* Set up tp->grc_local_ctrl before calling
  13843. * tg3_pwrsrc_switch_to_vmain(). GPIO1 driven high
  13844. * will bring 5700's external PHY out of reset.
  13845. * It is also used as eeprom write protect on LOMs.
  13846. */
  13847. tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
  13848. if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
  13849. tg3_flag(tp, EEPROM_WRITE_PROT))
  13850. tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
  13851. GRC_LCLCTRL_GPIO_OUTPUT1);
  13852. /* Unused GPIO3 must be driven as output on 5752 because there
  13853. * are no pull-up resistors on unused GPIO pins.
  13854. */
  13855. else if (tg3_asic_rev(tp) == ASIC_REV_5752)
  13856. tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
  13857. if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
  13858. tg3_asic_rev(tp) == ASIC_REV_57780 ||
  13859. tg3_flag(tp, 57765_CLASS))
  13860. tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
  13861. if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
  13862. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
  13863. /* Turn off the debug UART. */
  13864. tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
  13865. if (tg3_flag(tp, IS_NIC))
  13866. /* Keep VMain power. */
  13867. tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
  13868. GRC_LCLCTRL_GPIO_OUTPUT0;
  13869. }
  13870. if (tg3_asic_rev(tp) == ASIC_REV_5762)
  13871. tp->grc_local_ctrl |=
  13872. tr32(GRC_LOCAL_CTRL) & GRC_LCLCTRL_GPIO_UART_SEL;
  13873. /* Switch out of Vaux if it is a NIC */
  13874. tg3_pwrsrc_switch_to_vmain(tp);
  13875. /* Derive initial jumbo mode from MTU assigned in
  13876. * ether_setup() via the alloc_etherdev() call
  13877. */
  13878. if (tp->dev->mtu > ETH_DATA_LEN && !tg3_flag(tp, 5780_CLASS))
  13879. tg3_flag_set(tp, JUMBO_RING_ENABLE);
  13880. /* Determine WakeOnLan speed to use. */
  13881. if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
  13882. tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
  13883. tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0 ||
  13884. tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B2) {
  13885. tg3_flag_clear(tp, WOL_SPEED_100MB);
  13886. } else {
  13887. tg3_flag_set(tp, WOL_SPEED_100MB);
  13888. }
  13889. if (tg3_asic_rev(tp) == ASIC_REV_5906)
  13890. tp->phy_flags |= TG3_PHYFLG_IS_FET;
  13891. /* A few boards don't want Ethernet@WireSpeed phy feature */
  13892. if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
  13893. (tg3_asic_rev(tp) == ASIC_REV_5705 &&
  13894. (tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) &&
  13895. (tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A1)) ||
  13896. (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
  13897. (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
  13898. tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
  13899. if (tg3_chip_rev(tp) == CHIPREV_5703_AX ||
  13900. tg3_chip_rev(tp) == CHIPREV_5704_AX)
  13901. tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
  13902. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0)
  13903. tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
  13904. if (tg3_flag(tp, 5705_PLUS) &&
  13905. !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
  13906. tg3_asic_rev(tp) != ASIC_REV_5785 &&
  13907. tg3_asic_rev(tp) != ASIC_REV_57780 &&
  13908. !tg3_flag(tp, 57765_PLUS)) {
  13909. if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
  13910. tg3_asic_rev(tp) == ASIC_REV_5787 ||
  13911. tg3_asic_rev(tp) == ASIC_REV_5784 ||
  13912. tg3_asic_rev(tp) == ASIC_REV_5761) {
  13913. if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
  13914. tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
  13915. tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
  13916. if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
  13917. tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
  13918. } else
  13919. tp->phy_flags |= TG3_PHYFLG_BER_BUG;
  13920. }
  13921. if (tg3_asic_rev(tp) == ASIC_REV_5784 &&
  13922. tg3_chip_rev(tp) != CHIPREV_5784_AX) {
  13923. tp->phy_otp = tg3_read_otp_phycfg(tp);
  13924. if (tp->phy_otp == 0)
  13925. tp->phy_otp = TG3_OTP_DEFAULT;
  13926. }
  13927. if (tg3_flag(tp, CPMU_PRESENT))
  13928. tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
  13929. else
  13930. tp->mi_mode = MAC_MI_MODE_BASE;
  13931. tp->coalesce_mode = 0;
  13932. if (tg3_chip_rev(tp) != CHIPREV_5700_AX &&
  13933. tg3_chip_rev(tp) != CHIPREV_5700_BX)
  13934. tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
  13935. /* Set these bits to enable statistics workaround. */
  13936. if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
  13937. tg3_asic_rev(tp) == ASIC_REV_5762 ||
  13938. tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
  13939. tg3_chip_rev_id(tp) == CHIPREV_ID_5720_A0) {
  13940. tp->coalesce_mode |= HOSTCC_MODE_ATTN;
  13941. tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
  13942. }
  13943. if (tg3_asic_rev(tp) == ASIC_REV_5785 ||
  13944. tg3_asic_rev(tp) == ASIC_REV_57780)
  13945. tg3_flag_set(tp, USE_PHYLIB);
  13946. err = tg3_mdio_init(tp);
  13947. if (err)
  13948. return err;
  13949. /* Initialize data/descriptor byte/word swapping. */
  13950. val = tr32(GRC_MODE);
  13951. if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
  13952. tg3_asic_rev(tp) == ASIC_REV_5762)
  13953. val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
  13954. GRC_MODE_WORD_SWAP_B2HRX_DATA |
  13955. GRC_MODE_B2HRX_ENABLE |
  13956. GRC_MODE_HTX2B_ENABLE |
  13957. GRC_MODE_HOST_STACKUP);
  13958. else
  13959. val &= GRC_MODE_HOST_STACKUP;
  13960. tw32(GRC_MODE, val | tp->grc_mode);
  13961. tg3_switch_clocks(tp);
  13962. /* Clear this out for sanity. */
  13963. tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
  13964. /* Clear TG3PCI_REG_BASE_ADDR to prevent hangs. */
  13965. tw32(TG3PCI_REG_BASE_ADDR, 0);
  13966. pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
  13967. &pci_state_reg);
  13968. if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
  13969. !tg3_flag(tp, PCIX_TARGET_HWBUG)) {
  13970. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
  13971. tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0 ||
  13972. tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B2 ||
  13973. tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B5) {
  13974. void __iomem *sram_base;
  13975. /* Write some dummy words into the SRAM status block
  13976. * area, see if it reads back correctly. If the return
  13977. * value is bad, force enable the PCIX workaround.
  13978. */
  13979. sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
  13980. writel(0x00000000, sram_base);
  13981. writel(0x00000000, sram_base + 4);
  13982. writel(0xffffffff, sram_base + 4);
  13983. if (readl(sram_base) != 0x00000000)
  13984. tg3_flag_set(tp, PCIX_TARGET_HWBUG);
  13985. }
  13986. }
  13987. udelay(50);
  13988. tg3_nvram_init(tp);
  13989. /* If the device has an NVRAM, no need to load patch firmware */
  13990. if (tg3_asic_rev(tp) == ASIC_REV_57766 &&
  13991. !tg3_flag(tp, NO_NVRAM))
  13992. tp->fw_needed = NULL;
  13993. grc_misc_cfg = tr32(GRC_MISC_CFG);
  13994. grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
  13995. if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
  13996. (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
  13997. grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
  13998. tg3_flag_set(tp, IS_5788);
  13999. if (!tg3_flag(tp, IS_5788) &&
  14000. tg3_asic_rev(tp) != ASIC_REV_5700)
  14001. tg3_flag_set(tp, TAGGED_STATUS);
  14002. if (tg3_flag(tp, TAGGED_STATUS)) {
  14003. tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
  14004. HOSTCC_MODE_CLRTICK_TXBD);
  14005. tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
  14006. pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
  14007. tp->misc_host_ctrl);
  14008. }
  14009. /* Preserve the APE MAC_MODE bits */
  14010. if (tg3_flag(tp, ENABLE_APE))
  14011. tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
  14012. else
  14013. tp->mac_mode = 0;
  14014. if (tg3_10_100_only_device(tp, ent))
  14015. tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
  14016. err = tg3_phy_probe(tp);
  14017. if (err) {
  14018. dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
  14019. /* ... but do not return immediately ... */
  14020. tg3_mdio_fini(tp);
  14021. }
  14022. tg3_read_vpd(tp);
  14023. tg3_read_fw_ver(tp);
  14024. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
  14025. tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
  14026. } else {
  14027. if (tg3_asic_rev(tp) == ASIC_REV_5700)
  14028. tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
  14029. else
  14030. tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
  14031. }
  14032. /* 5700 {AX,BX} chips have a broken status block link
  14033. * change bit implementation, so we must use the
  14034. * status register in those cases.
  14035. */
  14036. if (tg3_asic_rev(tp) == ASIC_REV_5700)
  14037. tg3_flag_set(tp, USE_LINKCHG_REG);
  14038. else
  14039. tg3_flag_clear(tp, USE_LINKCHG_REG);
  14040. /* The led_ctrl is set during tg3_phy_probe, here we might
  14041. * have to force the link status polling mechanism based
  14042. * upon subsystem IDs.
  14043. */
  14044. if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
  14045. tg3_asic_rev(tp) == ASIC_REV_5701 &&
  14046. !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
  14047. tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
  14048. tg3_flag_set(tp, USE_LINKCHG_REG);
  14049. }
  14050. /* For all SERDES we poll the MAC status register. */
  14051. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
  14052. tg3_flag_set(tp, POLL_SERDES);
  14053. else
  14054. tg3_flag_clear(tp, POLL_SERDES);
  14055. if (tg3_flag(tp, ENABLE_APE) && tg3_flag(tp, ENABLE_ASF))
  14056. tg3_flag_set(tp, POLL_CPMU_LINK);
  14057. tp->rx_offset = NET_SKB_PAD + NET_IP_ALIGN;
  14058. tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
  14059. if (tg3_asic_rev(tp) == ASIC_REV_5701 &&
  14060. tg3_flag(tp, PCIX_MODE)) {
  14061. tp->rx_offset = NET_SKB_PAD;
  14062. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  14063. tp->rx_copy_thresh = ~(u16)0;
  14064. #endif
  14065. }
  14066. tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
  14067. tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
  14068. tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
  14069. tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
  14070. /* Increment the rx prod index on the rx std ring by at most
  14071. * 8 for these chips to workaround hw errata.
  14072. */
  14073. if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
  14074. tg3_asic_rev(tp) == ASIC_REV_5752 ||
  14075. tg3_asic_rev(tp) == ASIC_REV_5755)
  14076. tp->rx_std_max_post = 8;
  14077. if (tg3_flag(tp, ASPM_WORKAROUND))
  14078. tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
  14079. PCIE_PWR_MGMT_L1_THRESH_MSK;
  14080. return err;
  14081. }
  14082. #ifdef CONFIG_SPARC
  14083. static int tg3_get_macaddr_sparc(struct tg3 *tp)
  14084. {
  14085. struct net_device *dev = tp->dev;
  14086. struct pci_dev *pdev = tp->pdev;
  14087. struct device_node *dp = pci_device_to_OF_node(pdev);
  14088. const unsigned char *addr;
  14089. int len;
  14090. addr = of_get_property(dp, "local-mac-address", &len);
  14091. if (addr && len == ETH_ALEN) {
  14092. memcpy(dev->dev_addr, addr, ETH_ALEN);
  14093. return 0;
  14094. }
  14095. return -ENODEV;
  14096. }
  14097. static int tg3_get_default_macaddr_sparc(struct tg3 *tp)
  14098. {
  14099. struct net_device *dev = tp->dev;
  14100. memcpy(dev->dev_addr, idprom->id_ethaddr, ETH_ALEN);
  14101. return 0;
  14102. }
  14103. #endif
  14104. static int tg3_get_device_address(struct tg3 *tp)
  14105. {
  14106. struct net_device *dev = tp->dev;
  14107. u32 hi, lo, mac_offset;
  14108. int addr_ok = 0;
  14109. int err;
  14110. #ifdef CONFIG_SPARC
  14111. if (!tg3_get_macaddr_sparc(tp))
  14112. return 0;
  14113. #endif
  14114. if (tg3_flag(tp, IS_SSB_CORE)) {
  14115. err = ssb_gige_get_macaddr(tp->pdev, &dev->dev_addr[0]);
  14116. if (!err && is_valid_ether_addr(&dev->dev_addr[0]))
  14117. return 0;
  14118. }
  14119. mac_offset = 0x7c;
  14120. if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
  14121. tg3_flag(tp, 5780_CLASS)) {
  14122. if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
  14123. mac_offset = 0xcc;
  14124. if (tg3_nvram_lock(tp))
  14125. tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
  14126. else
  14127. tg3_nvram_unlock(tp);
  14128. } else if (tg3_flag(tp, 5717_PLUS)) {
  14129. if (tp->pci_fn & 1)
  14130. mac_offset = 0xcc;
  14131. if (tp->pci_fn > 1)
  14132. mac_offset += 0x18c;
  14133. } else if (tg3_asic_rev(tp) == ASIC_REV_5906)
  14134. mac_offset = 0x10;
  14135. /* First try to get it from MAC address mailbox. */
  14136. tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
  14137. if ((hi >> 16) == 0x484b) {
  14138. dev->dev_addr[0] = (hi >> 8) & 0xff;
  14139. dev->dev_addr[1] = (hi >> 0) & 0xff;
  14140. tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
  14141. dev->dev_addr[2] = (lo >> 24) & 0xff;
  14142. dev->dev_addr[3] = (lo >> 16) & 0xff;
  14143. dev->dev_addr[4] = (lo >> 8) & 0xff;
  14144. dev->dev_addr[5] = (lo >> 0) & 0xff;
  14145. /* Some old bootcode may report a 0 MAC address in SRAM */
  14146. addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
  14147. }
  14148. if (!addr_ok) {
  14149. /* Next, try NVRAM. */
  14150. if (!tg3_flag(tp, NO_NVRAM) &&
  14151. !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
  14152. !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
  14153. memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
  14154. memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
  14155. }
  14156. /* Finally just fetch it out of the MAC control regs. */
  14157. else {
  14158. hi = tr32(MAC_ADDR_0_HIGH);
  14159. lo = tr32(MAC_ADDR_0_LOW);
  14160. dev->dev_addr[5] = lo & 0xff;
  14161. dev->dev_addr[4] = (lo >> 8) & 0xff;
  14162. dev->dev_addr[3] = (lo >> 16) & 0xff;
  14163. dev->dev_addr[2] = (lo >> 24) & 0xff;
  14164. dev->dev_addr[1] = hi & 0xff;
  14165. dev->dev_addr[0] = (hi >> 8) & 0xff;
  14166. }
  14167. }
  14168. if (!is_valid_ether_addr(&dev->dev_addr[0])) {
  14169. #ifdef CONFIG_SPARC
  14170. if (!tg3_get_default_macaddr_sparc(tp))
  14171. return 0;
  14172. #endif
  14173. return -EINVAL;
  14174. }
  14175. return 0;
  14176. }
  14177. #define BOUNDARY_SINGLE_CACHELINE 1
  14178. #define BOUNDARY_MULTI_CACHELINE 2
  14179. static u32 tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
  14180. {
  14181. int cacheline_size;
  14182. u8 byte;
  14183. int goal;
  14184. pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
  14185. if (byte == 0)
  14186. cacheline_size = 1024;
  14187. else
  14188. cacheline_size = (int) byte * 4;
  14189. /* On 5703 and later chips, the boundary bits have no
  14190. * effect.
  14191. */
  14192. if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
  14193. tg3_asic_rev(tp) != ASIC_REV_5701 &&
  14194. !tg3_flag(tp, PCI_EXPRESS))
  14195. goto out;
  14196. #if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
  14197. goal = BOUNDARY_MULTI_CACHELINE;
  14198. #else
  14199. #if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
  14200. goal = BOUNDARY_SINGLE_CACHELINE;
  14201. #else
  14202. goal = 0;
  14203. #endif
  14204. #endif
  14205. if (tg3_flag(tp, 57765_PLUS)) {
  14206. val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
  14207. goto out;
  14208. }
  14209. if (!goal)
  14210. goto out;
  14211. /* PCI controllers on most RISC systems tend to disconnect
  14212. * when a device tries to burst across a cache-line boundary.
  14213. * Therefore, letting tg3 do so just wastes PCI bandwidth.
  14214. *
  14215. * Unfortunately, for PCI-E there are only limited
  14216. * write-side controls for this, and thus for reads
  14217. * we will still get the disconnects. We'll also waste
  14218. * these PCI cycles for both read and write for chips
  14219. * other than 5700 and 5701 which do not implement the
  14220. * boundary bits.
  14221. */
  14222. if (tg3_flag(tp, PCIX_MODE) && !tg3_flag(tp, PCI_EXPRESS)) {
  14223. switch (cacheline_size) {
  14224. case 16:
  14225. case 32:
  14226. case 64:
  14227. case 128:
  14228. if (goal == BOUNDARY_SINGLE_CACHELINE) {
  14229. val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
  14230. DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
  14231. } else {
  14232. val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
  14233. DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
  14234. }
  14235. break;
  14236. case 256:
  14237. val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
  14238. DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
  14239. break;
  14240. default:
  14241. val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
  14242. DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
  14243. break;
  14244. }
  14245. } else if (tg3_flag(tp, PCI_EXPRESS)) {
  14246. switch (cacheline_size) {
  14247. case 16:
  14248. case 32:
  14249. case 64:
  14250. if (goal == BOUNDARY_SINGLE_CACHELINE) {
  14251. val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
  14252. val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
  14253. break;
  14254. }
  14255. /* fallthrough */
  14256. case 128:
  14257. default:
  14258. val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
  14259. val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
  14260. break;
  14261. }
  14262. } else {
  14263. switch (cacheline_size) {
  14264. case 16:
  14265. if (goal == BOUNDARY_SINGLE_CACHELINE) {
  14266. val |= (DMA_RWCTRL_READ_BNDRY_16 |
  14267. DMA_RWCTRL_WRITE_BNDRY_16);
  14268. break;
  14269. }
  14270. /* fallthrough */
  14271. case 32:
  14272. if (goal == BOUNDARY_SINGLE_CACHELINE) {
  14273. val |= (DMA_RWCTRL_READ_BNDRY_32 |
  14274. DMA_RWCTRL_WRITE_BNDRY_32);
  14275. break;
  14276. }
  14277. /* fallthrough */
  14278. case 64:
  14279. if (goal == BOUNDARY_SINGLE_CACHELINE) {
  14280. val |= (DMA_RWCTRL_READ_BNDRY_64 |
  14281. DMA_RWCTRL_WRITE_BNDRY_64);
  14282. break;
  14283. }
  14284. /* fallthrough */
  14285. case 128:
  14286. if (goal == BOUNDARY_SINGLE_CACHELINE) {
  14287. val |= (DMA_RWCTRL_READ_BNDRY_128 |
  14288. DMA_RWCTRL_WRITE_BNDRY_128);
  14289. break;
  14290. }
  14291. /* fallthrough */
  14292. case 256:
  14293. val |= (DMA_RWCTRL_READ_BNDRY_256 |
  14294. DMA_RWCTRL_WRITE_BNDRY_256);
  14295. break;
  14296. case 512:
  14297. val |= (DMA_RWCTRL_READ_BNDRY_512 |
  14298. DMA_RWCTRL_WRITE_BNDRY_512);
  14299. break;
  14300. case 1024:
  14301. default:
  14302. val |= (DMA_RWCTRL_READ_BNDRY_1024 |
  14303. DMA_RWCTRL_WRITE_BNDRY_1024);
  14304. break;
  14305. }
  14306. }
  14307. out:
  14308. return val;
  14309. }
  14310. static int tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma,
  14311. int size, bool to_device)
  14312. {
  14313. struct tg3_internal_buffer_desc test_desc;
  14314. u32 sram_dma_descs;
  14315. int i, ret;
  14316. sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
  14317. tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
  14318. tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
  14319. tw32(RDMAC_STATUS, 0);
  14320. tw32(WDMAC_STATUS, 0);
  14321. tw32(BUFMGR_MODE, 0);
  14322. tw32(FTQ_RESET, 0);
  14323. test_desc.addr_hi = ((u64) buf_dma) >> 32;
  14324. test_desc.addr_lo = buf_dma & 0xffffffff;
  14325. test_desc.nic_mbuf = 0x00002100;
  14326. test_desc.len = size;
  14327. /*
  14328. * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
  14329. * the *second* time the tg3 driver was getting loaded after an
  14330. * initial scan.
  14331. *
  14332. * Broadcom tells me:
  14333. * ...the DMA engine is connected to the GRC block and a DMA
  14334. * reset may affect the GRC block in some unpredictable way...
  14335. * The behavior of resets to individual blocks has not been tested.
  14336. *
  14337. * Broadcom noted the GRC reset will also reset all sub-components.
  14338. */
  14339. if (to_device) {
  14340. test_desc.cqid_sqid = (13 << 8) | 2;
  14341. tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
  14342. udelay(40);
  14343. } else {
  14344. test_desc.cqid_sqid = (16 << 8) | 7;
  14345. tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
  14346. udelay(40);
  14347. }
  14348. test_desc.flags = 0x00000005;
  14349. for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
  14350. u32 val;
  14351. val = *(((u32 *)&test_desc) + i);
  14352. pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
  14353. sram_dma_descs + (i * sizeof(u32)));
  14354. pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
  14355. }
  14356. pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
  14357. if (to_device)
  14358. tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
  14359. else
  14360. tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
  14361. ret = -ENODEV;
  14362. for (i = 0; i < 40; i++) {
  14363. u32 val;
  14364. if (to_device)
  14365. val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
  14366. else
  14367. val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
  14368. if ((val & 0xffff) == sram_dma_descs) {
  14369. ret = 0;
  14370. break;
  14371. }
  14372. udelay(100);
  14373. }
  14374. return ret;
  14375. }
  14376. #define TEST_BUFFER_SIZE 0x2000
  14377. static const struct pci_device_id tg3_dma_wait_state_chipsets[] = {
  14378. { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
  14379. { },
  14380. };
  14381. static int tg3_test_dma(struct tg3 *tp)
  14382. {
  14383. dma_addr_t buf_dma;
  14384. u32 *buf, saved_dma_rwctrl;
  14385. int ret = 0;
  14386. buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
  14387. &buf_dma, GFP_KERNEL);
  14388. if (!buf) {
  14389. ret = -ENOMEM;
  14390. goto out_nofree;
  14391. }
  14392. tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
  14393. (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
  14394. tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
  14395. if (tg3_flag(tp, 57765_PLUS))
  14396. goto out;
  14397. if (tg3_flag(tp, PCI_EXPRESS)) {
  14398. /* DMA read watermark not used on PCIE */
  14399. tp->dma_rwctrl |= 0x00180000;
  14400. } else if (!tg3_flag(tp, PCIX_MODE)) {
  14401. if (tg3_asic_rev(tp) == ASIC_REV_5705 ||
  14402. tg3_asic_rev(tp) == ASIC_REV_5750)
  14403. tp->dma_rwctrl |= 0x003f0000;
  14404. else
  14405. tp->dma_rwctrl |= 0x003f000f;
  14406. } else {
  14407. if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
  14408. tg3_asic_rev(tp) == ASIC_REV_5704) {
  14409. u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
  14410. u32 read_water = 0x7;
  14411. /* If the 5704 is behind the EPB bridge, we can
  14412. * do the less restrictive ONE_DMA workaround for
  14413. * better performance.
  14414. */
  14415. if (tg3_flag(tp, 40BIT_DMA_BUG) &&
  14416. tg3_asic_rev(tp) == ASIC_REV_5704)
  14417. tp->dma_rwctrl |= 0x8000;
  14418. else if (ccval == 0x6 || ccval == 0x7)
  14419. tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
  14420. if (tg3_asic_rev(tp) == ASIC_REV_5703)
  14421. read_water = 4;
  14422. /* Set bit 23 to enable PCIX hw bug fix */
  14423. tp->dma_rwctrl |=
  14424. (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
  14425. (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
  14426. (1 << 23);
  14427. } else if (tg3_asic_rev(tp) == ASIC_REV_5780) {
  14428. /* 5780 always in PCIX mode */
  14429. tp->dma_rwctrl |= 0x00144000;
  14430. } else if (tg3_asic_rev(tp) == ASIC_REV_5714) {
  14431. /* 5714 always in PCIX mode */
  14432. tp->dma_rwctrl |= 0x00148000;
  14433. } else {
  14434. tp->dma_rwctrl |= 0x001b000f;
  14435. }
  14436. }
  14437. if (tg3_flag(tp, ONE_DMA_AT_ONCE))
  14438. tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
  14439. if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
  14440. tg3_asic_rev(tp) == ASIC_REV_5704)
  14441. tp->dma_rwctrl &= 0xfffffff0;
  14442. if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
  14443. tg3_asic_rev(tp) == ASIC_REV_5701) {
  14444. /* Remove this if it causes problems for some boards. */
  14445. tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
  14446. /* On 5700/5701 chips, we need to set this bit.
  14447. * Otherwise the chip will issue cacheline transactions
  14448. * to streamable DMA memory with not all the byte
  14449. * enables turned on. This is an error on several
  14450. * RISC PCI controllers, in particular sparc64.
  14451. *
  14452. * On 5703/5704 chips, this bit has been reassigned
  14453. * a different meaning. In particular, it is used
  14454. * on those chips to enable a PCI-X workaround.
  14455. */
  14456. tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
  14457. }
  14458. tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
  14459. if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
  14460. tg3_asic_rev(tp) != ASIC_REV_5701)
  14461. goto out;
  14462. /* It is best to perform DMA test with maximum write burst size
  14463. * to expose the 5700/5701 write DMA bug.
  14464. */
  14465. saved_dma_rwctrl = tp->dma_rwctrl;
  14466. tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
  14467. tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
  14468. while (1) {
  14469. u32 *p = buf, i;
  14470. for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
  14471. p[i] = i;
  14472. /* Send the buffer to the chip. */
  14473. ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, true);
  14474. if (ret) {
  14475. dev_err(&tp->pdev->dev,
  14476. "%s: Buffer write failed. err = %d\n",
  14477. __func__, ret);
  14478. break;
  14479. }
  14480. /* Now read it back. */
  14481. ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, false);
  14482. if (ret) {
  14483. dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
  14484. "err = %d\n", __func__, ret);
  14485. break;
  14486. }
  14487. /* Verify it. */
  14488. for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
  14489. if (p[i] == i)
  14490. continue;
  14491. if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
  14492. DMA_RWCTRL_WRITE_BNDRY_16) {
  14493. tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
  14494. tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
  14495. tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
  14496. break;
  14497. } else {
  14498. dev_err(&tp->pdev->dev,
  14499. "%s: Buffer corrupted on read back! "
  14500. "(%d != %d)\n", __func__, p[i], i);
  14501. ret = -ENODEV;
  14502. goto out;
  14503. }
  14504. }
  14505. if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
  14506. /* Success. */
  14507. ret = 0;
  14508. break;
  14509. }
  14510. }
  14511. if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
  14512. DMA_RWCTRL_WRITE_BNDRY_16) {
  14513. /* DMA test passed without adjusting DMA boundary,
  14514. * now look for chipsets that are known to expose the
  14515. * DMA bug without failing the test.
  14516. */
  14517. if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
  14518. tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
  14519. tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
  14520. } else {
  14521. /* Safe to use the calculated DMA boundary. */
  14522. tp->dma_rwctrl = saved_dma_rwctrl;
  14523. }
  14524. tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
  14525. }
  14526. out:
  14527. dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
  14528. out_nofree:
  14529. return ret;
  14530. }
  14531. static void tg3_init_bufmgr_config(struct tg3 *tp)
  14532. {
  14533. if (tg3_flag(tp, 57765_PLUS)) {
  14534. tp->bufmgr_config.mbuf_read_dma_low_water =
  14535. DEFAULT_MB_RDMA_LOW_WATER_5705;
  14536. tp->bufmgr_config.mbuf_mac_rx_low_water =
  14537. DEFAULT_MB_MACRX_LOW_WATER_57765;
  14538. tp->bufmgr_config.mbuf_high_water =
  14539. DEFAULT_MB_HIGH_WATER_57765;
  14540. tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
  14541. DEFAULT_MB_RDMA_LOW_WATER_5705;
  14542. tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
  14543. DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
  14544. tp->bufmgr_config.mbuf_high_water_jumbo =
  14545. DEFAULT_MB_HIGH_WATER_JUMBO_57765;
  14546. } else if (tg3_flag(tp, 5705_PLUS)) {
  14547. tp->bufmgr_config.mbuf_read_dma_low_water =
  14548. DEFAULT_MB_RDMA_LOW_WATER_5705;
  14549. tp->bufmgr_config.mbuf_mac_rx_low_water =
  14550. DEFAULT_MB_MACRX_LOW_WATER_5705;
  14551. tp->bufmgr_config.mbuf_high_water =
  14552. DEFAULT_MB_HIGH_WATER_5705;
  14553. if (tg3_asic_rev(tp) == ASIC_REV_5906) {
  14554. tp->bufmgr_config.mbuf_mac_rx_low_water =
  14555. DEFAULT_MB_MACRX_LOW_WATER_5906;
  14556. tp->bufmgr_config.mbuf_high_water =
  14557. DEFAULT_MB_HIGH_WATER_5906;
  14558. }
  14559. tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
  14560. DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
  14561. tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
  14562. DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
  14563. tp->bufmgr_config.mbuf_high_water_jumbo =
  14564. DEFAULT_MB_HIGH_WATER_JUMBO_5780;
  14565. } else {
  14566. tp->bufmgr_config.mbuf_read_dma_low_water =
  14567. DEFAULT_MB_RDMA_LOW_WATER;
  14568. tp->bufmgr_config.mbuf_mac_rx_low_water =
  14569. DEFAULT_MB_MACRX_LOW_WATER;
  14570. tp->bufmgr_config.mbuf_high_water =
  14571. DEFAULT_MB_HIGH_WATER;
  14572. tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
  14573. DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
  14574. tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
  14575. DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
  14576. tp->bufmgr_config.mbuf_high_water_jumbo =
  14577. DEFAULT_MB_HIGH_WATER_JUMBO;
  14578. }
  14579. tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
  14580. tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
  14581. }
  14582. static char *tg3_phy_string(struct tg3 *tp)
  14583. {
  14584. switch (tp->phy_id & TG3_PHY_ID_MASK) {
  14585. case TG3_PHY_ID_BCM5400: return "5400";
  14586. case TG3_PHY_ID_BCM5401: return "5401";
  14587. case TG3_PHY_ID_BCM5411: return "5411";
  14588. case TG3_PHY_ID_BCM5701: return "5701";
  14589. case TG3_PHY_ID_BCM5703: return "5703";
  14590. case TG3_PHY_ID_BCM5704: return "5704";
  14591. case TG3_PHY_ID_BCM5705: return "5705";
  14592. case TG3_PHY_ID_BCM5750: return "5750";
  14593. case TG3_PHY_ID_BCM5752: return "5752";
  14594. case TG3_PHY_ID_BCM5714: return "5714";
  14595. case TG3_PHY_ID_BCM5780: return "5780";
  14596. case TG3_PHY_ID_BCM5755: return "5755";
  14597. case TG3_PHY_ID_BCM5787: return "5787";
  14598. case TG3_PHY_ID_BCM5784: return "5784";
  14599. case TG3_PHY_ID_BCM5756: return "5722/5756";
  14600. case TG3_PHY_ID_BCM5906: return "5906";
  14601. case TG3_PHY_ID_BCM5761: return "5761";
  14602. case TG3_PHY_ID_BCM5718C: return "5718C";
  14603. case TG3_PHY_ID_BCM5718S: return "5718S";
  14604. case TG3_PHY_ID_BCM57765: return "57765";
  14605. case TG3_PHY_ID_BCM5719C: return "5719C";
  14606. case TG3_PHY_ID_BCM5720C: return "5720C";
  14607. case TG3_PHY_ID_BCM5762: return "5762C";
  14608. case TG3_PHY_ID_BCM8002: return "8002/serdes";
  14609. case 0: return "serdes";
  14610. default: return "unknown";
  14611. }
  14612. }
  14613. static char *tg3_bus_string(struct tg3 *tp, char *str)
  14614. {
  14615. if (tg3_flag(tp, PCI_EXPRESS)) {
  14616. strcpy(str, "PCI Express");
  14617. return str;
  14618. } else if (tg3_flag(tp, PCIX_MODE)) {
  14619. u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
  14620. strcpy(str, "PCIX:");
  14621. if ((clock_ctrl == 7) ||
  14622. ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
  14623. GRC_MISC_CFG_BOARD_ID_5704CIOBE))
  14624. strcat(str, "133MHz");
  14625. else if (clock_ctrl == 0)
  14626. strcat(str, "33MHz");
  14627. else if (clock_ctrl == 2)
  14628. strcat(str, "50MHz");
  14629. else if (clock_ctrl == 4)
  14630. strcat(str, "66MHz");
  14631. else if (clock_ctrl == 6)
  14632. strcat(str, "100MHz");
  14633. } else {
  14634. strcpy(str, "PCI:");
  14635. if (tg3_flag(tp, PCI_HIGH_SPEED))
  14636. strcat(str, "66MHz");
  14637. else
  14638. strcat(str, "33MHz");
  14639. }
  14640. if (tg3_flag(tp, PCI_32BIT))
  14641. strcat(str, ":32-bit");
  14642. else
  14643. strcat(str, ":64-bit");
  14644. return str;
  14645. }
  14646. static void tg3_init_coal(struct tg3 *tp)
  14647. {
  14648. struct ethtool_coalesce *ec = &tp->coal;
  14649. memset(ec, 0, sizeof(*ec));
  14650. ec->cmd = ETHTOOL_GCOALESCE;
  14651. ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
  14652. ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
  14653. ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
  14654. ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
  14655. ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
  14656. ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
  14657. ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
  14658. ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
  14659. ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
  14660. if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
  14661. HOSTCC_MODE_CLRTICK_TXBD)) {
  14662. ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
  14663. ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
  14664. ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
  14665. ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
  14666. }
  14667. if (tg3_flag(tp, 5705_PLUS)) {
  14668. ec->rx_coalesce_usecs_irq = 0;
  14669. ec->tx_coalesce_usecs_irq = 0;
  14670. ec->stats_block_coalesce_usecs = 0;
  14671. }
  14672. }
  14673. static int tg3_init_one(struct pci_dev *pdev,
  14674. const struct pci_device_id *ent)
  14675. {
  14676. struct net_device *dev;
  14677. struct tg3 *tp;
  14678. int i, err;
  14679. u32 sndmbx, rcvmbx, intmbx;
  14680. char str[40];
  14681. u64 dma_mask, persist_dma_mask;
  14682. netdev_features_t features = 0;
  14683. printk_once(KERN_INFO "%s\n", version);
  14684. err = pci_enable_device(pdev);
  14685. if (err) {
  14686. dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
  14687. return err;
  14688. }
  14689. err = pci_request_regions(pdev, DRV_MODULE_NAME);
  14690. if (err) {
  14691. dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
  14692. goto err_out_disable_pdev;
  14693. }
  14694. pci_set_master(pdev);
  14695. dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
  14696. if (!dev) {
  14697. err = -ENOMEM;
  14698. goto err_out_free_res;
  14699. }
  14700. SET_NETDEV_DEV(dev, &pdev->dev);
  14701. tp = netdev_priv(dev);
  14702. tp->pdev = pdev;
  14703. tp->dev = dev;
  14704. tp->rx_mode = TG3_DEF_RX_MODE;
  14705. tp->tx_mode = TG3_DEF_TX_MODE;
  14706. tp->irq_sync = 1;
  14707. tp->pcierr_recovery = false;
  14708. if (tg3_debug > 0)
  14709. tp->msg_enable = tg3_debug;
  14710. else
  14711. tp->msg_enable = TG3_DEF_MSG_ENABLE;
  14712. if (pdev_is_ssb_gige_core(pdev)) {
  14713. tg3_flag_set(tp, IS_SSB_CORE);
  14714. if (ssb_gige_must_flush_posted_writes(pdev))
  14715. tg3_flag_set(tp, FLUSH_POSTED_WRITES);
  14716. if (ssb_gige_one_dma_at_once(pdev))
  14717. tg3_flag_set(tp, ONE_DMA_AT_ONCE);
  14718. if (ssb_gige_have_roboswitch(pdev)) {
  14719. tg3_flag_set(tp, USE_PHYLIB);
  14720. tg3_flag_set(tp, ROBOSWITCH);
  14721. }
  14722. if (ssb_gige_is_rgmii(pdev))
  14723. tg3_flag_set(tp, RGMII_MODE);
  14724. }
  14725. /* The word/byte swap controls here control register access byte
  14726. * swapping. DMA data byte swapping is controlled in the GRC_MODE
  14727. * setting below.
  14728. */
  14729. tp->misc_host_ctrl =
  14730. MISC_HOST_CTRL_MASK_PCI_INT |
  14731. MISC_HOST_CTRL_WORD_SWAP |
  14732. MISC_HOST_CTRL_INDIR_ACCESS |
  14733. MISC_HOST_CTRL_PCISTATE_RW;
  14734. /* The NONFRM (non-frame) byte/word swap controls take effect
  14735. * on descriptor entries, anything which isn't packet data.
  14736. *
  14737. * The StrongARM chips on the board (one for tx, one for rx)
  14738. * are running in big-endian mode.
  14739. */
  14740. tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
  14741. GRC_MODE_WSWAP_NONFRM_DATA);
  14742. #ifdef __BIG_ENDIAN
  14743. tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
  14744. #endif
  14745. spin_lock_init(&tp->lock);
  14746. spin_lock_init(&tp->indirect_lock);
  14747. INIT_WORK(&tp->reset_task, tg3_reset_task);
  14748. tp->regs = pci_ioremap_bar(pdev, BAR_0);
  14749. if (!tp->regs) {
  14750. dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
  14751. err = -ENOMEM;
  14752. goto err_out_free_dev;
  14753. }
  14754. if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
  14755. tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761E ||
  14756. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S ||
  14757. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761SE ||
  14758. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
  14759. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C ||
  14760. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
  14761. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
  14762. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720 ||
  14763. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57767 ||
  14764. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57764 ||
  14765. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5762 ||
  14766. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725 ||
  14767. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5727 ||
  14768. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57787) {
  14769. tg3_flag_set(tp, ENABLE_APE);
  14770. tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
  14771. if (!tp->aperegs) {
  14772. dev_err(&pdev->dev,
  14773. "Cannot map APE registers, aborting\n");
  14774. err = -ENOMEM;
  14775. goto err_out_iounmap;
  14776. }
  14777. }
  14778. tp->rx_pending = TG3_DEF_RX_RING_PENDING;
  14779. tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
  14780. dev->ethtool_ops = &tg3_ethtool_ops;
  14781. dev->watchdog_timeo = TG3_TX_TIMEOUT;
  14782. dev->netdev_ops = &tg3_netdev_ops;
  14783. dev->irq = pdev->irq;
  14784. err = tg3_get_invariants(tp, ent);
  14785. if (err) {
  14786. dev_err(&pdev->dev,
  14787. "Problem fetching invariants of chip, aborting\n");
  14788. goto err_out_apeunmap;
  14789. }
  14790. /* The EPB bridge inside 5714, 5715, and 5780 and any
  14791. * device behind the EPB cannot support DMA addresses > 40-bit.
  14792. * On 64-bit systems with IOMMU, use 40-bit dma_mask.
  14793. * On 64-bit systems without IOMMU, use 64-bit dma_mask and
  14794. * do DMA address check in tg3_start_xmit().
  14795. */
  14796. if (tg3_flag(tp, IS_5788))
  14797. persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
  14798. else if (tg3_flag(tp, 40BIT_DMA_BUG)) {
  14799. persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
  14800. #ifdef CONFIG_HIGHMEM
  14801. dma_mask = DMA_BIT_MASK(64);
  14802. #endif
  14803. } else
  14804. persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
  14805. /* Configure DMA attributes. */
  14806. if (dma_mask > DMA_BIT_MASK(32)) {
  14807. err = pci_set_dma_mask(pdev, dma_mask);
  14808. if (!err) {
  14809. features |= NETIF_F_HIGHDMA;
  14810. err = pci_set_consistent_dma_mask(pdev,
  14811. persist_dma_mask);
  14812. if (err < 0) {
  14813. dev_err(&pdev->dev, "Unable to obtain 64 bit "
  14814. "DMA for consistent allocations\n");
  14815. goto err_out_apeunmap;
  14816. }
  14817. }
  14818. }
  14819. if (err || dma_mask == DMA_BIT_MASK(32)) {
  14820. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  14821. if (err) {
  14822. dev_err(&pdev->dev,
  14823. "No usable DMA configuration, aborting\n");
  14824. goto err_out_apeunmap;
  14825. }
  14826. }
  14827. tg3_init_bufmgr_config(tp);
  14828. /* 5700 B0 chips do not support checksumming correctly due
  14829. * to hardware bugs.
  14830. */
  14831. if (tg3_chip_rev_id(tp) != CHIPREV_ID_5700_B0) {
  14832. features |= NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
  14833. if (tg3_flag(tp, 5755_PLUS))
  14834. features |= NETIF_F_IPV6_CSUM;
  14835. }
  14836. /* TSO is on by default on chips that support hardware TSO.
  14837. * Firmware TSO on older chips gives lower performance, so it
  14838. * is off by default, but can be enabled using ethtool.
  14839. */
  14840. if ((tg3_flag(tp, HW_TSO_1) ||
  14841. tg3_flag(tp, HW_TSO_2) ||
  14842. tg3_flag(tp, HW_TSO_3)) &&
  14843. (features & NETIF_F_IP_CSUM))
  14844. features |= NETIF_F_TSO;
  14845. if (tg3_flag(tp, HW_TSO_2) || tg3_flag(tp, HW_TSO_3)) {
  14846. if (features & NETIF_F_IPV6_CSUM)
  14847. features |= NETIF_F_TSO6;
  14848. if (tg3_flag(tp, HW_TSO_3) ||
  14849. tg3_asic_rev(tp) == ASIC_REV_5761 ||
  14850. (tg3_asic_rev(tp) == ASIC_REV_5784 &&
  14851. tg3_chip_rev(tp) != CHIPREV_5784_AX) ||
  14852. tg3_asic_rev(tp) == ASIC_REV_5785 ||
  14853. tg3_asic_rev(tp) == ASIC_REV_57780)
  14854. features |= NETIF_F_TSO_ECN;
  14855. }
  14856. dev->features |= features | NETIF_F_HW_VLAN_CTAG_TX |
  14857. NETIF_F_HW_VLAN_CTAG_RX;
  14858. dev->vlan_features |= features;
  14859. /*
  14860. * Add loopback capability only for a subset of devices that support
  14861. * MAC-LOOPBACK. Eventually this need to be enhanced to allow INT-PHY
  14862. * loopback for the remaining devices.
  14863. */
  14864. if (tg3_asic_rev(tp) != ASIC_REV_5780 &&
  14865. !tg3_flag(tp, CPMU_PRESENT))
  14866. /* Add the loopback capability */
  14867. features |= NETIF_F_LOOPBACK;
  14868. dev->hw_features |= features;
  14869. dev->priv_flags |= IFF_UNICAST_FLT;
  14870. /* MTU range: 60 - 9000 or 1500, depending on hardware */
  14871. dev->min_mtu = TG3_MIN_MTU;
  14872. dev->max_mtu = TG3_MAX_MTU(tp);
  14873. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A1 &&
  14874. !tg3_flag(tp, TSO_CAPABLE) &&
  14875. !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
  14876. tg3_flag_set(tp, MAX_RXPEND_64);
  14877. tp->rx_pending = 63;
  14878. }
  14879. err = tg3_get_device_address(tp);
  14880. if (err) {
  14881. dev_err(&pdev->dev,
  14882. "Could not obtain valid ethernet address, aborting\n");
  14883. goto err_out_apeunmap;
  14884. }
  14885. intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
  14886. rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
  14887. sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
  14888. for (i = 0; i < tp->irq_max; i++) {
  14889. struct tg3_napi *tnapi = &tp->napi[i];
  14890. tnapi->tp = tp;
  14891. tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
  14892. tnapi->int_mbox = intmbx;
  14893. if (i <= 4)
  14894. intmbx += 0x8;
  14895. else
  14896. intmbx += 0x4;
  14897. tnapi->consmbox = rcvmbx;
  14898. tnapi->prodmbox = sndmbx;
  14899. if (i)
  14900. tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
  14901. else
  14902. tnapi->coal_now = HOSTCC_MODE_NOW;
  14903. if (!tg3_flag(tp, SUPPORT_MSIX))
  14904. break;
  14905. /*
  14906. * If we support MSIX, we'll be using RSS. If we're using
  14907. * RSS, the first vector only handles link interrupts and the
  14908. * remaining vectors handle rx and tx interrupts. Reuse the
  14909. * mailbox values for the next iteration. The values we setup
  14910. * above are still useful for the single vectored mode.
  14911. */
  14912. if (!i)
  14913. continue;
  14914. rcvmbx += 0x8;
  14915. if (sndmbx & 0x4)
  14916. sndmbx -= 0x4;
  14917. else
  14918. sndmbx += 0xc;
  14919. }
  14920. /*
  14921. * Reset chip in case UNDI or EFI driver did not shutdown
  14922. * DMA self test will enable WDMAC and we'll see (spurious)
  14923. * pending DMA on the PCI bus at that point.
  14924. */
  14925. if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
  14926. (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
  14927. tg3_full_lock(tp, 0);
  14928. tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
  14929. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  14930. tg3_full_unlock(tp);
  14931. }
  14932. err = tg3_test_dma(tp);
  14933. if (err) {
  14934. dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
  14935. goto err_out_apeunmap;
  14936. }
  14937. tg3_init_coal(tp);
  14938. pci_set_drvdata(pdev, dev);
  14939. if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
  14940. tg3_asic_rev(tp) == ASIC_REV_5720 ||
  14941. tg3_asic_rev(tp) == ASIC_REV_5762)
  14942. tg3_flag_set(tp, PTP_CAPABLE);
  14943. tg3_timer_init(tp);
  14944. tg3_carrier_off(tp);
  14945. err = register_netdev(dev);
  14946. if (err) {
  14947. dev_err(&pdev->dev, "Cannot register net device, aborting\n");
  14948. goto err_out_apeunmap;
  14949. }
  14950. if (tg3_flag(tp, PTP_CAPABLE)) {
  14951. tg3_ptp_init(tp);
  14952. tp->ptp_clock = ptp_clock_register(&tp->ptp_info,
  14953. &tp->pdev->dev);
  14954. if (IS_ERR(tp->ptp_clock))
  14955. tp->ptp_clock = NULL;
  14956. }
  14957. netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
  14958. tp->board_part_number,
  14959. tg3_chip_rev_id(tp),
  14960. tg3_bus_string(tp, str),
  14961. dev->dev_addr);
  14962. if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)) {
  14963. char *ethtype;
  14964. if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
  14965. ethtype = "10/100Base-TX";
  14966. else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
  14967. ethtype = "1000Base-SX";
  14968. else
  14969. ethtype = "10/100/1000Base-T";
  14970. netdev_info(dev, "attached PHY is %s (%s Ethernet) "
  14971. "(WireSpeed[%d], EEE[%d])\n",
  14972. tg3_phy_string(tp), ethtype,
  14973. (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0,
  14974. (tp->phy_flags & TG3_PHYFLG_EEE_CAP) != 0);
  14975. }
  14976. netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
  14977. (dev->features & NETIF_F_RXCSUM) != 0,
  14978. tg3_flag(tp, USE_LINKCHG_REG) != 0,
  14979. (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
  14980. tg3_flag(tp, ENABLE_ASF) != 0,
  14981. tg3_flag(tp, TSO_CAPABLE) != 0);
  14982. netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
  14983. tp->dma_rwctrl,
  14984. pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
  14985. ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
  14986. pci_save_state(pdev);
  14987. return 0;
  14988. err_out_apeunmap:
  14989. if (tp->aperegs) {
  14990. iounmap(tp->aperegs);
  14991. tp->aperegs = NULL;
  14992. }
  14993. err_out_iounmap:
  14994. if (tp->regs) {
  14995. iounmap(tp->regs);
  14996. tp->regs = NULL;
  14997. }
  14998. err_out_free_dev:
  14999. free_netdev(dev);
  15000. err_out_free_res:
  15001. pci_release_regions(pdev);
  15002. err_out_disable_pdev:
  15003. if (pci_is_enabled(pdev))
  15004. pci_disable_device(pdev);
  15005. return err;
  15006. }
  15007. static void tg3_remove_one(struct pci_dev *pdev)
  15008. {
  15009. struct net_device *dev = pci_get_drvdata(pdev);
  15010. if (dev) {
  15011. struct tg3 *tp = netdev_priv(dev);
  15012. tg3_ptp_fini(tp);
  15013. release_firmware(tp->fw);
  15014. tg3_reset_task_cancel(tp);
  15015. if (tg3_flag(tp, USE_PHYLIB)) {
  15016. tg3_phy_fini(tp);
  15017. tg3_mdio_fini(tp);
  15018. }
  15019. unregister_netdev(dev);
  15020. if (tp->aperegs) {
  15021. iounmap(tp->aperegs);
  15022. tp->aperegs = NULL;
  15023. }
  15024. if (tp->regs) {
  15025. iounmap(tp->regs);
  15026. tp->regs = NULL;
  15027. }
  15028. free_netdev(dev);
  15029. pci_release_regions(pdev);
  15030. pci_disable_device(pdev);
  15031. }
  15032. }
  15033. #ifdef CONFIG_PM_SLEEP
  15034. static int tg3_suspend(struct device *device)
  15035. {
  15036. struct pci_dev *pdev = to_pci_dev(device);
  15037. struct net_device *dev = pci_get_drvdata(pdev);
  15038. struct tg3 *tp = netdev_priv(dev);
  15039. int err = 0;
  15040. rtnl_lock();
  15041. if (!netif_running(dev))
  15042. goto unlock;
  15043. tg3_reset_task_cancel(tp);
  15044. tg3_phy_stop(tp);
  15045. tg3_netif_stop(tp);
  15046. tg3_timer_stop(tp);
  15047. tg3_full_lock(tp, 1);
  15048. tg3_disable_ints(tp);
  15049. tg3_full_unlock(tp);
  15050. netif_device_detach(dev);
  15051. tg3_full_lock(tp, 0);
  15052. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  15053. tg3_flag_clear(tp, INIT_COMPLETE);
  15054. tg3_full_unlock(tp);
  15055. err = tg3_power_down_prepare(tp);
  15056. if (err) {
  15057. int err2;
  15058. tg3_full_lock(tp, 0);
  15059. tg3_flag_set(tp, INIT_COMPLETE);
  15060. err2 = tg3_restart_hw(tp, true);
  15061. if (err2)
  15062. goto out;
  15063. tg3_timer_start(tp);
  15064. netif_device_attach(dev);
  15065. tg3_netif_start(tp);
  15066. out:
  15067. tg3_full_unlock(tp);
  15068. if (!err2)
  15069. tg3_phy_start(tp);
  15070. }
  15071. unlock:
  15072. rtnl_unlock();
  15073. return err;
  15074. }
  15075. static int tg3_resume(struct device *device)
  15076. {
  15077. struct pci_dev *pdev = to_pci_dev(device);
  15078. struct net_device *dev = pci_get_drvdata(pdev);
  15079. struct tg3 *tp = netdev_priv(dev);
  15080. int err = 0;
  15081. rtnl_lock();
  15082. if (!netif_running(dev))
  15083. goto unlock;
  15084. netif_device_attach(dev);
  15085. tg3_full_lock(tp, 0);
  15086. tg3_ape_driver_state_change(tp, RESET_KIND_INIT);
  15087. tg3_flag_set(tp, INIT_COMPLETE);
  15088. err = tg3_restart_hw(tp,
  15089. !(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN));
  15090. if (err)
  15091. goto out;
  15092. tg3_timer_start(tp);
  15093. tg3_netif_start(tp);
  15094. out:
  15095. tg3_full_unlock(tp);
  15096. if (!err)
  15097. tg3_phy_start(tp);
  15098. unlock:
  15099. rtnl_unlock();
  15100. return err;
  15101. }
  15102. #endif /* CONFIG_PM_SLEEP */
  15103. static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
  15104. static void tg3_shutdown(struct pci_dev *pdev)
  15105. {
  15106. struct net_device *dev = pci_get_drvdata(pdev);
  15107. struct tg3 *tp = netdev_priv(dev);
  15108. rtnl_lock();
  15109. netif_device_detach(dev);
  15110. if (netif_running(dev))
  15111. dev_close(dev);
  15112. if (system_state == SYSTEM_POWER_OFF)
  15113. tg3_power_down(tp);
  15114. rtnl_unlock();
  15115. }
  15116. /**
  15117. * tg3_io_error_detected - called when PCI error is detected
  15118. * @pdev: Pointer to PCI device
  15119. * @state: The current pci connection state
  15120. *
  15121. * This function is called after a PCI bus error affecting
  15122. * this device has been detected.
  15123. */
  15124. static pci_ers_result_t tg3_io_error_detected(struct pci_dev *pdev,
  15125. pci_channel_state_t state)
  15126. {
  15127. struct net_device *netdev = pci_get_drvdata(pdev);
  15128. struct tg3 *tp = netdev_priv(netdev);
  15129. pci_ers_result_t err = PCI_ERS_RESULT_NEED_RESET;
  15130. netdev_info(netdev, "PCI I/O error detected\n");
  15131. rtnl_lock();
  15132. /* We probably don't have netdev yet */
  15133. if (!netdev || !netif_running(netdev))
  15134. goto done;
  15135. /* We needn't recover from permanent error */
  15136. if (state == pci_channel_io_frozen)
  15137. tp->pcierr_recovery = true;
  15138. tg3_phy_stop(tp);
  15139. tg3_netif_stop(tp);
  15140. tg3_timer_stop(tp);
  15141. /* Want to make sure that the reset task doesn't run */
  15142. tg3_reset_task_cancel(tp);
  15143. netif_device_detach(netdev);
  15144. /* Clean up software state, even if MMIO is blocked */
  15145. tg3_full_lock(tp, 0);
  15146. tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
  15147. tg3_full_unlock(tp);
  15148. done:
  15149. if (state == pci_channel_io_perm_failure) {
  15150. if (netdev) {
  15151. tg3_napi_enable(tp);
  15152. dev_close(netdev);
  15153. }
  15154. err = PCI_ERS_RESULT_DISCONNECT;
  15155. } else {
  15156. pci_disable_device(pdev);
  15157. }
  15158. rtnl_unlock();
  15159. return err;
  15160. }
  15161. /**
  15162. * tg3_io_slot_reset - called after the pci bus has been reset.
  15163. * @pdev: Pointer to PCI device
  15164. *
  15165. * Restart the card from scratch, as if from a cold-boot.
  15166. * At this point, the card has exprienced a hard reset,
  15167. * followed by fixups by BIOS, and has its config space
  15168. * set up identically to what it was at cold boot.
  15169. */
  15170. static pci_ers_result_t tg3_io_slot_reset(struct pci_dev *pdev)
  15171. {
  15172. struct net_device *netdev = pci_get_drvdata(pdev);
  15173. struct tg3 *tp = netdev_priv(netdev);
  15174. pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
  15175. int err;
  15176. rtnl_lock();
  15177. if (pci_enable_device(pdev)) {
  15178. dev_err(&pdev->dev,
  15179. "Cannot re-enable PCI device after reset.\n");
  15180. goto done;
  15181. }
  15182. pci_set_master(pdev);
  15183. pci_restore_state(pdev);
  15184. pci_save_state(pdev);
  15185. if (!netdev || !netif_running(netdev)) {
  15186. rc = PCI_ERS_RESULT_RECOVERED;
  15187. goto done;
  15188. }
  15189. err = tg3_power_up(tp);
  15190. if (err)
  15191. goto done;
  15192. rc = PCI_ERS_RESULT_RECOVERED;
  15193. done:
  15194. if (rc != PCI_ERS_RESULT_RECOVERED && netdev && netif_running(netdev)) {
  15195. tg3_napi_enable(tp);
  15196. dev_close(netdev);
  15197. }
  15198. rtnl_unlock();
  15199. return rc;
  15200. }
  15201. /**
  15202. * tg3_io_resume - called when traffic can start flowing again.
  15203. * @pdev: Pointer to PCI device
  15204. *
  15205. * This callback is called when the error recovery driver tells
  15206. * us that its OK to resume normal operation.
  15207. */
  15208. static void tg3_io_resume(struct pci_dev *pdev)
  15209. {
  15210. struct net_device *netdev = pci_get_drvdata(pdev);
  15211. struct tg3 *tp = netdev_priv(netdev);
  15212. int err;
  15213. rtnl_lock();
  15214. if (!netdev || !netif_running(netdev))
  15215. goto done;
  15216. tg3_full_lock(tp, 0);
  15217. tg3_ape_driver_state_change(tp, RESET_KIND_INIT);
  15218. tg3_flag_set(tp, INIT_COMPLETE);
  15219. err = tg3_restart_hw(tp, true);
  15220. if (err) {
  15221. tg3_full_unlock(tp);
  15222. netdev_err(netdev, "Cannot restart hardware after reset.\n");
  15223. goto done;
  15224. }
  15225. netif_device_attach(netdev);
  15226. tg3_timer_start(tp);
  15227. tg3_netif_start(tp);
  15228. tg3_full_unlock(tp);
  15229. tg3_phy_start(tp);
  15230. done:
  15231. tp->pcierr_recovery = false;
  15232. rtnl_unlock();
  15233. }
  15234. static const struct pci_error_handlers tg3_err_handler = {
  15235. .error_detected = tg3_io_error_detected,
  15236. .slot_reset = tg3_io_slot_reset,
  15237. .resume = tg3_io_resume
  15238. };
  15239. static struct pci_driver tg3_driver = {
  15240. .name = DRV_MODULE_NAME,
  15241. .id_table = tg3_pci_tbl,
  15242. .probe = tg3_init_one,
  15243. .remove = tg3_remove_one,
  15244. .err_handler = &tg3_err_handler,
  15245. .driver.pm = &tg3_pm_ops,
  15246. .shutdown = tg3_shutdown,
  15247. };
  15248. module_pci_driver(tg3_driver);