tg3.c 466 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303
  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-2014 Broadcom Corporation.
  8. *
  9. /*(DEBLOBBED)*/
  10. #include <linux/module.h>
  11. #include <linux/moduleparam.h>
  12. #include <linux/stringify.h>
  13. #include <linux/kernel.h>
  14. #include <linux/types.h>
  15. #include <linux/compiler.h>
  16. #include <linux/slab.h>
  17. #include <linux/delay.h>
  18. #include <linux/in.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/ioport.h>
  21. #include <linux/pci.h>
  22. #include <linux/netdevice.h>
  23. #include <linux/etherdevice.h>
  24. #include <linux/skbuff.h>
  25. #include <linux/ethtool.h>
  26. #include <linux/mdio.h>
  27. #include <linux/mii.h>
  28. #include <linux/phy.h>
  29. #include <linux/brcmphy.h>
  30. #include <linux/if.h>
  31. #include <linux/if_vlan.h>
  32. #include <linux/ip.h>
  33. #include <linux/tcp.h>
  34. #include <linux/workqueue.h>
  35. #include <linux/prefetch.h>
  36. #include <linux/dma-mapping.h>
  37. #include <linux/firmware.h>
  38. #include <linux/ssb/ssb_driver_gige.h>
  39. #include <linux/hwmon.h>
  40. #include <linux/hwmon-sysfs.h>
  41. #include <net/checksum.h>
  42. #include <net/ip.h>
  43. #include <linux/io.h>
  44. #include <asm/byteorder.h>
  45. #include <linux/uaccess.h>
  46. #include <uapi/linux/net_tstamp.h>
  47. #include <linux/ptp_clock_kernel.h>
  48. #ifdef CONFIG_SPARC
  49. #include <asm/idprom.h>
  50. #include <asm/prom.h>
  51. #endif
  52. #define BAR_0 0
  53. #define BAR_2 2
  54. #include "tg3.h"
  55. /* Functions & macros to verify TG3_FLAGS types */
  56. static inline int _tg3_flag(enum TG3_FLAGS flag, unsigned long *bits)
  57. {
  58. return test_bit(flag, bits);
  59. }
  60. static inline void _tg3_flag_set(enum TG3_FLAGS flag, unsigned long *bits)
  61. {
  62. set_bit(flag, bits);
  63. }
  64. static inline void _tg3_flag_clear(enum TG3_FLAGS flag, unsigned long *bits)
  65. {
  66. clear_bit(flag, bits);
  67. }
  68. #define tg3_flag(tp, flag) \
  69. _tg3_flag(TG3_FLAG_##flag, (tp)->tg3_flags)
  70. #define tg3_flag_set(tp, flag) \
  71. _tg3_flag_set(TG3_FLAG_##flag, (tp)->tg3_flags)
  72. #define tg3_flag_clear(tp, flag) \
  73. _tg3_flag_clear(TG3_FLAG_##flag, (tp)->tg3_flags)
  74. #define DRV_MODULE_NAME "tg3"
  75. #define TG3_MAJ_NUM 3
  76. #define TG3_MIN_NUM 137
  77. #define DRV_MODULE_VERSION \
  78. __stringify(TG3_MAJ_NUM) "." __stringify(TG3_MIN_NUM)
  79. #define DRV_MODULE_RELDATE "May 11, 2014"
  80. #define RESET_KIND_SHUTDOWN 0
  81. #define RESET_KIND_INIT 1
  82. #define RESET_KIND_SUSPEND 2
  83. #define TG3_DEF_RX_MODE 0
  84. #define TG3_DEF_TX_MODE 0
  85. #define TG3_DEF_MSG_ENABLE \
  86. (NETIF_MSG_DRV | \
  87. NETIF_MSG_PROBE | \
  88. NETIF_MSG_LINK | \
  89. NETIF_MSG_TIMER | \
  90. NETIF_MSG_IFDOWN | \
  91. NETIF_MSG_IFUP | \
  92. NETIF_MSG_RX_ERR | \
  93. NETIF_MSG_TX_ERR)
  94. #define TG3_GRC_LCLCTL_PWRSW_DELAY 100
  95. /* length of time before we decide the hardware is borked,
  96. * and dev->tx_timeout() should be called to fix the problem
  97. */
  98. #define TG3_TX_TIMEOUT (5 * HZ)
  99. /* hardware minimum and maximum for a single frame's data payload */
  100. #define TG3_MIN_MTU 60
  101. #define TG3_MAX_MTU(tp) \
  102. (tg3_flag(tp, JUMBO_CAPABLE) ? 9000 : 1500)
  103. /* These numbers seem to be hard coded in the NIC firmware somehow.
  104. * You can't change the ring sizes, but you can change where you place
  105. * them in the NIC onboard memory.
  106. */
  107. #define TG3_RX_STD_RING_SIZE(tp) \
  108. (tg3_flag(tp, LRG_PROD_RING_CAP) ? \
  109. TG3_RX_STD_MAX_SIZE_5717 : TG3_RX_STD_MAX_SIZE_5700)
  110. #define TG3_DEF_RX_RING_PENDING 200
  111. #define TG3_RX_JMB_RING_SIZE(tp) \
  112. (tg3_flag(tp, LRG_PROD_RING_CAP) ? \
  113. TG3_RX_JMB_MAX_SIZE_5717 : TG3_RX_JMB_MAX_SIZE_5700)
  114. #define TG3_DEF_RX_JUMBO_RING_PENDING 100
  115. /* Do not place this n-ring entries value into the tp struct itself,
  116. * we really want to expose these constants to GCC so that modulo et
  117. * al. operations are done with shifts and masks instead of with
  118. * hw multiply/modulo instructions. Another solution would be to
  119. * replace things like '% foo' with '& (foo - 1)'.
  120. */
  121. #define TG3_TX_RING_SIZE 512
  122. #define TG3_DEF_TX_RING_PENDING (TG3_TX_RING_SIZE - 1)
  123. #define TG3_RX_STD_RING_BYTES(tp) \
  124. (sizeof(struct tg3_rx_buffer_desc) * TG3_RX_STD_RING_SIZE(tp))
  125. #define TG3_RX_JMB_RING_BYTES(tp) \
  126. (sizeof(struct tg3_ext_rx_buffer_desc) * TG3_RX_JMB_RING_SIZE(tp))
  127. #define TG3_RX_RCB_RING_BYTES(tp) \
  128. (sizeof(struct tg3_rx_buffer_desc) * (tp->rx_ret_ring_mask + 1))
  129. #define TG3_TX_RING_BYTES (sizeof(struct tg3_tx_buffer_desc) * \
  130. TG3_TX_RING_SIZE)
  131. #define NEXT_TX(N) (((N) + 1) & (TG3_TX_RING_SIZE - 1))
  132. #define TG3_DMA_BYTE_ENAB 64
  133. #define TG3_RX_STD_DMA_SZ 1536
  134. #define TG3_RX_JMB_DMA_SZ 9046
  135. #define TG3_RX_DMA_TO_MAP_SZ(x) ((x) + TG3_DMA_BYTE_ENAB)
  136. #define TG3_RX_STD_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_STD_DMA_SZ)
  137. #define TG3_RX_JMB_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_JMB_DMA_SZ)
  138. #define TG3_RX_STD_BUFF_RING_SIZE(tp) \
  139. (sizeof(struct ring_info) * TG3_RX_STD_RING_SIZE(tp))
  140. #define TG3_RX_JMB_BUFF_RING_SIZE(tp) \
  141. (sizeof(struct ring_info) * TG3_RX_JMB_RING_SIZE(tp))
  142. /* Due to a hardware bug, the 5701 can only DMA to memory addresses
  143. * that are at least dword aligned when used in PCIX mode. The driver
  144. * works around this bug by double copying the packet. This workaround
  145. * is built into the normal double copy length check for efficiency.
  146. *
  147. * However, the double copy is only necessary on those architectures
  148. * where unaligned memory accesses are inefficient. For those architectures
  149. * where unaligned memory accesses incur little penalty, we can reintegrate
  150. * the 5701 in the normal rx path. Doing so saves a device structure
  151. * dereference by hardcoding the double copy threshold in place.
  152. */
  153. #define TG3_RX_COPY_THRESHOLD 256
  154. #if NET_IP_ALIGN == 0 || defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
  155. #define TG3_RX_COPY_THRESH(tp) TG3_RX_COPY_THRESHOLD
  156. #else
  157. #define TG3_RX_COPY_THRESH(tp) ((tp)->rx_copy_thresh)
  158. #endif
  159. #if (NET_IP_ALIGN != 0)
  160. #define TG3_RX_OFFSET(tp) ((tp)->rx_offset)
  161. #else
  162. #define TG3_RX_OFFSET(tp) (NET_SKB_PAD)
  163. #endif
  164. /* minimum number of free TX descriptors required to wake up TX process */
  165. #define TG3_TX_WAKEUP_THRESH(tnapi) ((tnapi)->tx_pending / 4)
  166. #define TG3_TX_BD_DMA_MAX_2K 2048
  167. #define TG3_TX_BD_DMA_MAX_4K 4096
  168. #define TG3_RAW_IP_ALIGN 2
  169. #define TG3_MAX_UCAST_ADDR(tp) (tg3_flag((tp), ENABLE_ASF) ? 2 : 3)
  170. #define TG3_UCAST_ADDR_IDX(tp) (tg3_flag((tp), ENABLE_ASF) ? 2 : 1)
  171. #define TG3_FW_UPDATE_TIMEOUT_SEC 5
  172. #define TG3_FW_UPDATE_FREQ_SEC (TG3_FW_UPDATE_TIMEOUT_SEC / 2)
  173. #define FIRMWARE_TG3 "/*(DEBLOBBED)*/"
  174. #define FIRMWARE_TG357766 "/*(DEBLOBBED)*/"
  175. #define FIRMWARE_TG3TSO "/*(DEBLOBBED)*/"
  176. #define FIRMWARE_TG3TSO5 "/*(DEBLOBBED)*/"
  177. static char version[] =
  178. DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")";
  179. MODULE_AUTHOR("David S. Miller (davem@redhat.com) and Jeff Garzik (jgarzik@pobox.com)");
  180. MODULE_DESCRIPTION("Broadcom Tigon3 ethernet driver");
  181. MODULE_LICENSE("GPL");
  182. MODULE_VERSION(DRV_MODULE_VERSION);
  183. /*(DEBLOBBED)*/
  184. static int tg3_debug = -1; /* -1 == use TG3_DEF_MSG_ENABLE as value */
  185. module_param(tg3_debug, int, 0);
  186. MODULE_PARM_DESC(tg3_debug, "Tigon3 bitmapped debugging message enable value");
  187. #define TG3_DRV_DATA_FLAG_10_100_ONLY 0x0001
  188. #define TG3_DRV_DATA_FLAG_5705_10_100 0x0002
  189. static const struct pci_device_id tg3_pci_tbl[] = {
  190. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5700)},
  191. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5701)},
  192. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702)},
  193. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703)},
  194. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704)},
  195. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702FE)},
  196. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705)},
  197. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705_2)},
  198. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M)},
  199. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M_2)},
  200. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702X)},
  201. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703X)},
  202. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S)},
  203. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702A3)},
  204. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703A3)},
  205. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5782)},
  206. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5788)},
  207. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5789)},
  208. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901),
  209. .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY |
  210. TG3_DRV_DATA_FLAG_5705_10_100},
  211. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901_2),
  212. .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY |
  213. TG3_DRV_DATA_FLAG_5705_10_100},
  214. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S_2)},
  215. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705F),
  216. .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY |
  217. TG3_DRV_DATA_FLAG_5705_10_100},
  218. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5721)},
  219. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5722)},
  220. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5750)},
  221. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751)},
  222. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751M)},
  223. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751F),
  224. .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
  225. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752)},
  226. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752M)},
  227. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753)},
  228. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753M)},
  229. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753F),
  230. .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
  231. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754)},
  232. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754M)},
  233. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755)},
  234. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755M)},
  235. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5756)},
  236. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5786)},
  237. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787)},
  238. {PCI_DEVICE_SUB(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5787M,
  239. PCI_VENDOR_ID_LENOVO,
  240. TG3PCI_SUBDEVICE_ID_LENOVO_5787M),
  241. .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
  242. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787M)},
  243. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787F),
  244. .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
  245. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714)},
  246. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714S)},
  247. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715)},
  248. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715S)},
  249. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780)},
  250. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780S)},
  251. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5781)},
  252. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906)},
  253. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906M)},
  254. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5784)},
  255. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5764)},
  256. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5723)},
  257. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761)},
  258. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761E)},
  259. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761S)},
  260. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761SE)},
  261. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_G)},
  262. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_F)},
  263. {PCI_DEVICE_SUB(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780,
  264. PCI_VENDOR_ID_AI, TG3PCI_SUBDEVICE_ID_ACER_57780_A),
  265. .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
  266. {PCI_DEVICE_SUB(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780,
  267. PCI_VENDOR_ID_AI, TG3PCI_SUBDEVICE_ID_ACER_57780_B),
  268. .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
  269. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780)},
  270. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57760)},
  271. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57790),
  272. .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
  273. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57788)},
  274. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717)},
  275. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717_C)},
  276. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5718)},
  277. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57781)},
  278. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57785)},
  279. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57761)},
  280. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57765)},
  281. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57791),
  282. .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
  283. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57795),
  284. .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
  285. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5719)},
  286. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5720)},
  287. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57762)},
  288. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57766)},
  289. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5762)},
  290. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5725)},
  291. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5727)},
  292. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57764)},
  293. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57767)},
  294. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57787)},
  295. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57782)},
  296. {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57786)},
  297. {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9DXX)},
  298. {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9MXX)},
  299. {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1000)},
  300. {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1001)},
  301. {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1003)},
  302. {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC9100)},
  303. {PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_TIGON3)},
  304. {PCI_DEVICE(0x10cf, 0x11a2)}, /* Fujitsu 1000base-SX with BCM5703SKHB */
  305. {}
  306. };
  307. MODULE_DEVICE_TABLE(pci, tg3_pci_tbl);
  308. static const struct {
  309. const char string[ETH_GSTRING_LEN];
  310. } ethtool_stats_keys[] = {
  311. { "rx_octets" },
  312. { "rx_fragments" },
  313. { "rx_ucast_packets" },
  314. { "rx_mcast_packets" },
  315. { "rx_bcast_packets" },
  316. { "rx_fcs_errors" },
  317. { "rx_align_errors" },
  318. { "rx_xon_pause_rcvd" },
  319. { "rx_xoff_pause_rcvd" },
  320. { "rx_mac_ctrl_rcvd" },
  321. { "rx_xoff_entered" },
  322. { "rx_frame_too_long_errors" },
  323. { "rx_jabbers" },
  324. { "rx_undersize_packets" },
  325. { "rx_in_length_errors" },
  326. { "rx_out_length_errors" },
  327. { "rx_64_or_less_octet_packets" },
  328. { "rx_65_to_127_octet_packets" },
  329. { "rx_128_to_255_octet_packets" },
  330. { "rx_256_to_511_octet_packets" },
  331. { "rx_512_to_1023_octet_packets" },
  332. { "rx_1024_to_1522_octet_packets" },
  333. { "rx_1523_to_2047_octet_packets" },
  334. { "rx_2048_to_4095_octet_packets" },
  335. { "rx_4096_to_8191_octet_packets" },
  336. { "rx_8192_to_9022_octet_packets" },
  337. { "tx_octets" },
  338. { "tx_collisions" },
  339. { "tx_xon_sent" },
  340. { "tx_xoff_sent" },
  341. { "tx_flow_control" },
  342. { "tx_mac_errors" },
  343. { "tx_single_collisions" },
  344. { "tx_mult_collisions" },
  345. { "tx_deferred" },
  346. { "tx_excessive_collisions" },
  347. { "tx_late_collisions" },
  348. { "tx_collide_2times" },
  349. { "tx_collide_3times" },
  350. { "tx_collide_4times" },
  351. { "tx_collide_5times" },
  352. { "tx_collide_6times" },
  353. { "tx_collide_7times" },
  354. { "tx_collide_8times" },
  355. { "tx_collide_9times" },
  356. { "tx_collide_10times" },
  357. { "tx_collide_11times" },
  358. { "tx_collide_12times" },
  359. { "tx_collide_13times" },
  360. { "tx_collide_14times" },
  361. { "tx_collide_15times" },
  362. { "tx_ucast_packets" },
  363. { "tx_mcast_packets" },
  364. { "tx_bcast_packets" },
  365. { "tx_carrier_sense_errors" },
  366. { "tx_discards" },
  367. { "tx_errors" },
  368. { "dma_writeq_full" },
  369. { "dma_write_prioq_full" },
  370. { "rxbds_empty" },
  371. { "rx_discards" },
  372. { "rx_errors" },
  373. { "rx_threshold_hit" },
  374. { "dma_readq_full" },
  375. { "dma_read_prioq_full" },
  376. { "tx_comp_queue_full" },
  377. { "ring_set_send_prod_index" },
  378. { "ring_status_update" },
  379. { "nic_irqs" },
  380. { "nic_avoided_irqs" },
  381. { "nic_tx_threshold_hit" },
  382. { "mbuf_lwm_thresh_hit" },
  383. };
  384. #define TG3_NUM_STATS ARRAY_SIZE(ethtool_stats_keys)
  385. #define TG3_NVRAM_TEST 0
  386. #define TG3_LINK_TEST 1
  387. #define TG3_REGISTER_TEST 2
  388. #define TG3_MEMORY_TEST 3
  389. #define TG3_MAC_LOOPB_TEST 4
  390. #define TG3_PHY_LOOPB_TEST 5
  391. #define TG3_EXT_LOOPB_TEST 6
  392. #define TG3_INTERRUPT_TEST 7
  393. static const struct {
  394. const char string[ETH_GSTRING_LEN];
  395. } ethtool_test_keys[] = {
  396. [TG3_NVRAM_TEST] = { "nvram test (online) " },
  397. [TG3_LINK_TEST] = { "link test (online) " },
  398. [TG3_REGISTER_TEST] = { "register test (offline)" },
  399. [TG3_MEMORY_TEST] = { "memory test (offline)" },
  400. [TG3_MAC_LOOPB_TEST] = { "mac loopback test (offline)" },
  401. [TG3_PHY_LOOPB_TEST] = { "phy loopback test (offline)" },
  402. [TG3_EXT_LOOPB_TEST] = { "ext loopback test (offline)" },
  403. [TG3_INTERRUPT_TEST] = { "interrupt test (offline)" },
  404. };
  405. #define TG3_NUM_TEST ARRAY_SIZE(ethtool_test_keys)
  406. static void tg3_write32(struct tg3 *tp, u32 off, u32 val)
  407. {
  408. writel(val, tp->regs + off);
  409. }
  410. static u32 tg3_read32(struct tg3 *tp, u32 off)
  411. {
  412. return readl(tp->regs + off);
  413. }
  414. static void tg3_ape_write32(struct tg3 *tp, u32 off, u32 val)
  415. {
  416. writel(val, tp->aperegs + off);
  417. }
  418. static u32 tg3_ape_read32(struct tg3 *tp, u32 off)
  419. {
  420. return readl(tp->aperegs + off);
  421. }
  422. static void tg3_write_indirect_reg32(struct tg3 *tp, u32 off, u32 val)
  423. {
  424. unsigned long flags;
  425. spin_lock_irqsave(&tp->indirect_lock, flags);
  426. pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
  427. pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
  428. spin_unlock_irqrestore(&tp->indirect_lock, flags);
  429. }
  430. static void tg3_write_flush_reg32(struct tg3 *tp, u32 off, u32 val)
  431. {
  432. writel(val, tp->regs + off);
  433. readl(tp->regs + off);
  434. }
  435. static u32 tg3_read_indirect_reg32(struct tg3 *tp, u32 off)
  436. {
  437. unsigned long flags;
  438. u32 val;
  439. spin_lock_irqsave(&tp->indirect_lock, flags);
  440. pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
  441. pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
  442. spin_unlock_irqrestore(&tp->indirect_lock, flags);
  443. return val;
  444. }
  445. static void tg3_write_indirect_mbox(struct tg3 *tp, u32 off, u32 val)
  446. {
  447. unsigned long flags;
  448. if (off == (MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW)) {
  449. pci_write_config_dword(tp->pdev, TG3PCI_RCV_RET_RING_CON_IDX +
  450. TG3_64BIT_REG_LOW, val);
  451. return;
  452. }
  453. if (off == TG3_RX_STD_PROD_IDX_REG) {
  454. pci_write_config_dword(tp->pdev, TG3PCI_STD_RING_PROD_IDX +
  455. TG3_64BIT_REG_LOW, val);
  456. return;
  457. }
  458. spin_lock_irqsave(&tp->indirect_lock, flags);
  459. pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
  460. pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
  461. spin_unlock_irqrestore(&tp->indirect_lock, flags);
  462. /* In indirect mode when disabling interrupts, we also need
  463. * to clear the interrupt bit in the GRC local ctrl register.
  464. */
  465. if ((off == (MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW)) &&
  466. (val == 0x1)) {
  467. pci_write_config_dword(tp->pdev, TG3PCI_MISC_LOCAL_CTRL,
  468. tp->grc_local_ctrl|GRC_LCLCTRL_CLEARINT);
  469. }
  470. }
  471. static u32 tg3_read_indirect_mbox(struct tg3 *tp, u32 off)
  472. {
  473. unsigned long flags;
  474. u32 val;
  475. spin_lock_irqsave(&tp->indirect_lock, flags);
  476. pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
  477. pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
  478. spin_unlock_irqrestore(&tp->indirect_lock, flags);
  479. return val;
  480. }
  481. /* usec_wait specifies the wait time in usec when writing to certain registers
  482. * where it is unsafe to read back the register without some delay.
  483. * GRC_LOCAL_CTRL is one example if the GPIOs are toggled to switch power.
  484. * TG3PCI_CLOCK_CTRL is another example if the clock frequencies are changed.
  485. */
  486. static void _tw32_flush(struct tg3 *tp, u32 off, u32 val, u32 usec_wait)
  487. {
  488. if (tg3_flag(tp, PCIX_TARGET_HWBUG) || tg3_flag(tp, ICH_WORKAROUND))
  489. /* Non-posted methods */
  490. tp->write32(tp, off, val);
  491. else {
  492. /* Posted method */
  493. tg3_write32(tp, off, val);
  494. if (usec_wait)
  495. udelay(usec_wait);
  496. tp->read32(tp, off);
  497. }
  498. /* Wait again after the read for the posted method to guarantee that
  499. * the wait time is met.
  500. */
  501. if (usec_wait)
  502. udelay(usec_wait);
  503. }
  504. static inline void tw32_mailbox_flush(struct tg3 *tp, u32 off, u32 val)
  505. {
  506. tp->write32_mbox(tp, off, val);
  507. if (tg3_flag(tp, FLUSH_POSTED_WRITES) ||
  508. (!tg3_flag(tp, MBOX_WRITE_REORDER) &&
  509. !tg3_flag(tp, ICH_WORKAROUND)))
  510. tp->read32_mbox(tp, off);
  511. }
  512. static void tg3_write32_tx_mbox(struct tg3 *tp, u32 off, u32 val)
  513. {
  514. void __iomem *mbox = tp->regs + off;
  515. writel(val, mbox);
  516. if (tg3_flag(tp, TXD_MBOX_HWBUG))
  517. writel(val, mbox);
  518. if (tg3_flag(tp, MBOX_WRITE_REORDER) ||
  519. tg3_flag(tp, FLUSH_POSTED_WRITES))
  520. readl(mbox);
  521. }
  522. static u32 tg3_read32_mbox_5906(struct tg3 *tp, u32 off)
  523. {
  524. return readl(tp->regs + off + GRCMBOX_BASE);
  525. }
  526. static void tg3_write32_mbox_5906(struct tg3 *tp, u32 off, u32 val)
  527. {
  528. writel(val, tp->regs + off + GRCMBOX_BASE);
  529. }
  530. #define tw32_mailbox(reg, val) tp->write32_mbox(tp, reg, val)
  531. #define tw32_mailbox_f(reg, val) tw32_mailbox_flush(tp, (reg), (val))
  532. #define tw32_rx_mbox(reg, val) tp->write32_rx_mbox(tp, reg, val)
  533. #define tw32_tx_mbox(reg, val) tp->write32_tx_mbox(tp, reg, val)
  534. #define tr32_mailbox(reg) tp->read32_mbox(tp, reg)
  535. #define tw32(reg, val) tp->write32(tp, reg, val)
  536. #define tw32_f(reg, val) _tw32_flush(tp, (reg), (val), 0)
  537. #define tw32_wait_f(reg, val, us) _tw32_flush(tp, (reg), (val), (us))
  538. #define tr32(reg) tp->read32(tp, reg)
  539. static void tg3_write_mem(struct tg3 *tp, u32 off, u32 val)
  540. {
  541. unsigned long flags;
  542. if (tg3_asic_rev(tp) == ASIC_REV_5906 &&
  543. (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC))
  544. return;
  545. spin_lock_irqsave(&tp->indirect_lock, flags);
  546. if (tg3_flag(tp, SRAM_USE_CONFIG)) {
  547. pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
  548. pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
  549. /* Always leave this as zero. */
  550. pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
  551. } else {
  552. tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
  553. tw32_f(TG3PCI_MEM_WIN_DATA, val);
  554. /* Always leave this as zero. */
  555. tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
  556. }
  557. spin_unlock_irqrestore(&tp->indirect_lock, flags);
  558. }
  559. static void tg3_read_mem(struct tg3 *tp, u32 off, u32 *val)
  560. {
  561. unsigned long flags;
  562. if (tg3_asic_rev(tp) == ASIC_REV_5906 &&
  563. (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC)) {
  564. *val = 0;
  565. return;
  566. }
  567. spin_lock_irqsave(&tp->indirect_lock, flags);
  568. if (tg3_flag(tp, SRAM_USE_CONFIG)) {
  569. pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
  570. pci_read_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
  571. /* Always leave this as zero. */
  572. pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
  573. } else {
  574. tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
  575. *val = tr32(TG3PCI_MEM_WIN_DATA);
  576. /* Always leave this as zero. */
  577. tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
  578. }
  579. spin_unlock_irqrestore(&tp->indirect_lock, flags);
  580. }
  581. static void tg3_ape_lock_init(struct tg3 *tp)
  582. {
  583. int i;
  584. u32 regbase, bit;
  585. if (tg3_asic_rev(tp) == ASIC_REV_5761)
  586. regbase = TG3_APE_LOCK_GRANT;
  587. else
  588. regbase = TG3_APE_PER_LOCK_GRANT;
  589. /* Make sure the driver hasn't any stale locks. */
  590. for (i = TG3_APE_LOCK_PHY0; i <= TG3_APE_LOCK_GPIO; i++) {
  591. switch (i) {
  592. case TG3_APE_LOCK_PHY0:
  593. case TG3_APE_LOCK_PHY1:
  594. case TG3_APE_LOCK_PHY2:
  595. case TG3_APE_LOCK_PHY3:
  596. bit = APE_LOCK_GRANT_DRIVER;
  597. break;
  598. default:
  599. if (!tp->pci_fn)
  600. bit = APE_LOCK_GRANT_DRIVER;
  601. else
  602. bit = 1 << tp->pci_fn;
  603. }
  604. tg3_ape_write32(tp, regbase + 4 * i, bit);
  605. }
  606. }
  607. static int tg3_ape_lock(struct tg3 *tp, int locknum)
  608. {
  609. int i, off;
  610. int ret = 0;
  611. u32 status, req, gnt, bit;
  612. if (!tg3_flag(tp, ENABLE_APE))
  613. return 0;
  614. switch (locknum) {
  615. case TG3_APE_LOCK_GPIO:
  616. if (tg3_asic_rev(tp) == ASIC_REV_5761)
  617. return 0;
  618. case TG3_APE_LOCK_GRC:
  619. case TG3_APE_LOCK_MEM:
  620. if (!tp->pci_fn)
  621. bit = APE_LOCK_REQ_DRIVER;
  622. else
  623. bit = 1 << tp->pci_fn;
  624. break;
  625. case TG3_APE_LOCK_PHY0:
  626. case TG3_APE_LOCK_PHY1:
  627. case TG3_APE_LOCK_PHY2:
  628. case TG3_APE_LOCK_PHY3:
  629. bit = APE_LOCK_REQ_DRIVER;
  630. break;
  631. default:
  632. return -EINVAL;
  633. }
  634. if (tg3_asic_rev(tp) == ASIC_REV_5761) {
  635. req = TG3_APE_LOCK_REQ;
  636. gnt = TG3_APE_LOCK_GRANT;
  637. } else {
  638. req = TG3_APE_PER_LOCK_REQ;
  639. gnt = TG3_APE_PER_LOCK_GRANT;
  640. }
  641. off = 4 * locknum;
  642. tg3_ape_write32(tp, req + off, bit);
  643. /* Wait for up to 1 millisecond to acquire lock. */
  644. for (i = 0; i < 100; i++) {
  645. status = tg3_ape_read32(tp, gnt + off);
  646. if (status == bit)
  647. break;
  648. if (pci_channel_offline(tp->pdev))
  649. break;
  650. udelay(10);
  651. }
  652. if (status != bit) {
  653. /* Revoke the lock request. */
  654. tg3_ape_write32(tp, gnt + off, bit);
  655. ret = -EBUSY;
  656. }
  657. return ret;
  658. }
  659. static void tg3_ape_unlock(struct tg3 *tp, int locknum)
  660. {
  661. u32 gnt, bit;
  662. if (!tg3_flag(tp, ENABLE_APE))
  663. return;
  664. switch (locknum) {
  665. case TG3_APE_LOCK_GPIO:
  666. if (tg3_asic_rev(tp) == ASIC_REV_5761)
  667. return;
  668. case TG3_APE_LOCK_GRC:
  669. case TG3_APE_LOCK_MEM:
  670. if (!tp->pci_fn)
  671. bit = APE_LOCK_GRANT_DRIVER;
  672. else
  673. bit = 1 << tp->pci_fn;
  674. break;
  675. case TG3_APE_LOCK_PHY0:
  676. case TG3_APE_LOCK_PHY1:
  677. case TG3_APE_LOCK_PHY2:
  678. case TG3_APE_LOCK_PHY3:
  679. bit = APE_LOCK_GRANT_DRIVER;
  680. break;
  681. default:
  682. return;
  683. }
  684. if (tg3_asic_rev(tp) == ASIC_REV_5761)
  685. gnt = TG3_APE_LOCK_GRANT;
  686. else
  687. gnt = TG3_APE_PER_LOCK_GRANT;
  688. tg3_ape_write32(tp, gnt + 4 * locknum, bit);
  689. }
  690. static int tg3_ape_event_lock(struct tg3 *tp, u32 timeout_us)
  691. {
  692. u32 apedata;
  693. while (timeout_us) {
  694. if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
  695. return -EBUSY;
  696. apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
  697. if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
  698. break;
  699. tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
  700. udelay(10);
  701. timeout_us -= (timeout_us > 10) ? 10 : timeout_us;
  702. }
  703. return timeout_us ? 0 : -EBUSY;
  704. }
  705. static int tg3_ape_wait_for_event(struct tg3 *tp, u32 timeout_us)
  706. {
  707. u32 i, apedata;
  708. for (i = 0; i < timeout_us / 10; i++) {
  709. apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
  710. if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
  711. break;
  712. udelay(10);
  713. }
  714. return i == timeout_us / 10;
  715. }
  716. static int tg3_ape_scratchpad_read(struct tg3 *tp, u32 *data, u32 base_off,
  717. u32 len)
  718. {
  719. int err;
  720. u32 i, bufoff, msgoff, maxlen, apedata;
  721. if (!tg3_flag(tp, APE_HAS_NCSI))
  722. return 0;
  723. apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
  724. if (apedata != APE_SEG_SIG_MAGIC)
  725. return -ENODEV;
  726. apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
  727. if (!(apedata & APE_FW_STATUS_READY))
  728. return -EAGAIN;
  729. bufoff = tg3_ape_read32(tp, TG3_APE_SEG_MSG_BUF_OFF) +
  730. TG3_APE_SHMEM_BASE;
  731. msgoff = bufoff + 2 * sizeof(u32);
  732. maxlen = tg3_ape_read32(tp, TG3_APE_SEG_MSG_BUF_LEN);
  733. while (len) {
  734. u32 length;
  735. /* Cap xfer sizes to scratchpad limits. */
  736. length = (len > maxlen) ? maxlen : len;
  737. len -= length;
  738. apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
  739. if (!(apedata & APE_FW_STATUS_READY))
  740. return -EAGAIN;
  741. /* Wait for up to 1 msec for APE to service previous event. */
  742. err = tg3_ape_event_lock(tp, 1000);
  743. if (err)
  744. return err;
  745. apedata = APE_EVENT_STATUS_DRIVER_EVNT |
  746. APE_EVENT_STATUS_SCRTCHPD_READ |
  747. APE_EVENT_STATUS_EVENT_PENDING;
  748. tg3_ape_write32(tp, TG3_APE_EVENT_STATUS, apedata);
  749. tg3_ape_write32(tp, bufoff, base_off);
  750. tg3_ape_write32(tp, bufoff + sizeof(u32), length);
  751. tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
  752. tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
  753. base_off += length;
  754. if (tg3_ape_wait_for_event(tp, 30000))
  755. return -EAGAIN;
  756. for (i = 0; length; i += 4, length -= 4) {
  757. u32 val = tg3_ape_read32(tp, msgoff + i);
  758. memcpy(data, &val, sizeof(u32));
  759. data++;
  760. }
  761. }
  762. return 0;
  763. }
  764. static int tg3_ape_send_event(struct tg3 *tp, u32 event)
  765. {
  766. int err;
  767. u32 apedata;
  768. apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
  769. if (apedata != APE_SEG_SIG_MAGIC)
  770. return -EAGAIN;
  771. apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
  772. if (!(apedata & APE_FW_STATUS_READY))
  773. return -EAGAIN;
  774. /* Wait for up to 1 millisecond for APE to service previous event. */
  775. err = tg3_ape_event_lock(tp, 1000);
  776. if (err)
  777. return err;
  778. tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
  779. event | APE_EVENT_STATUS_EVENT_PENDING);
  780. tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
  781. tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
  782. return 0;
  783. }
  784. static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
  785. {
  786. u32 event;
  787. u32 apedata;
  788. if (!tg3_flag(tp, ENABLE_APE))
  789. return;
  790. switch (kind) {
  791. case RESET_KIND_INIT:
  792. tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
  793. APE_HOST_SEG_SIG_MAGIC);
  794. tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
  795. APE_HOST_SEG_LEN_MAGIC);
  796. apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
  797. tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
  798. tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
  799. APE_HOST_DRIVER_ID_MAGIC(TG3_MAJ_NUM, TG3_MIN_NUM));
  800. tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
  801. APE_HOST_BEHAV_NO_PHYLOCK);
  802. tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE,
  803. TG3_APE_HOST_DRVR_STATE_START);
  804. event = APE_EVENT_STATUS_STATE_START;
  805. break;
  806. case RESET_KIND_SHUTDOWN:
  807. /* With the interface we are currently using,
  808. * APE does not track driver state. Wiping
  809. * out the HOST SEGMENT SIGNATURE forces
  810. * the APE to assume OS absent status.
  811. */
  812. tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
  813. if (device_may_wakeup(&tp->pdev->dev) &&
  814. tg3_flag(tp, WOL_ENABLE)) {
  815. tg3_ape_write32(tp, TG3_APE_HOST_WOL_SPEED,
  816. TG3_APE_HOST_WOL_SPEED_AUTO);
  817. apedata = TG3_APE_HOST_DRVR_STATE_WOL;
  818. } else
  819. apedata = TG3_APE_HOST_DRVR_STATE_UNLOAD;
  820. tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE, apedata);
  821. event = APE_EVENT_STATUS_STATE_UNLOAD;
  822. break;
  823. default:
  824. return;
  825. }
  826. event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
  827. tg3_ape_send_event(tp, event);
  828. }
  829. static void tg3_disable_ints(struct tg3 *tp)
  830. {
  831. int i;
  832. tw32(TG3PCI_MISC_HOST_CTRL,
  833. (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
  834. for (i = 0; i < tp->irq_max; i++)
  835. tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
  836. }
  837. static void tg3_enable_ints(struct tg3 *tp)
  838. {
  839. int i;
  840. tp->irq_sync = 0;
  841. wmb();
  842. tw32(TG3PCI_MISC_HOST_CTRL,
  843. (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
  844. tp->coal_now = tp->coalesce_mode | HOSTCC_MODE_ENABLE;
  845. for (i = 0; i < tp->irq_cnt; i++) {
  846. struct tg3_napi *tnapi = &tp->napi[i];
  847. tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
  848. if (tg3_flag(tp, 1SHOT_MSI))
  849. tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
  850. tp->coal_now |= tnapi->coal_now;
  851. }
  852. /* Force an initial interrupt */
  853. if (!tg3_flag(tp, TAGGED_STATUS) &&
  854. (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
  855. tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
  856. else
  857. tw32(HOSTCC_MODE, tp->coal_now);
  858. tp->coal_now &= ~(tp->napi[0].coal_now | tp->napi[1].coal_now);
  859. }
  860. static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
  861. {
  862. struct tg3 *tp = tnapi->tp;
  863. struct tg3_hw_status *sblk = tnapi->hw_status;
  864. unsigned int work_exists = 0;
  865. /* check for phy events */
  866. if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
  867. if (sblk->status & SD_STATUS_LINK_CHG)
  868. work_exists = 1;
  869. }
  870. /* check for TX work to do */
  871. if (sblk->idx[0].tx_consumer != tnapi->tx_cons)
  872. work_exists = 1;
  873. /* check for RX work to do */
  874. if (tnapi->rx_rcb_prod_idx &&
  875. *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
  876. work_exists = 1;
  877. return work_exists;
  878. }
  879. /* tg3_int_reenable
  880. * similar to tg3_enable_ints, but it accurately determines whether there
  881. * is new work pending and can return without flushing the PIO write
  882. * which reenables interrupts
  883. */
  884. static void tg3_int_reenable(struct tg3_napi *tnapi)
  885. {
  886. struct tg3 *tp = tnapi->tp;
  887. tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
  888. mmiowb();
  889. /* When doing tagged status, this work check is unnecessary.
  890. * The last_tag we write above tells the chip which piece of
  891. * work we've completed.
  892. */
  893. if (!tg3_flag(tp, TAGGED_STATUS) && tg3_has_work(tnapi))
  894. tw32(HOSTCC_MODE, tp->coalesce_mode |
  895. HOSTCC_MODE_ENABLE | tnapi->coal_now);
  896. }
  897. static void tg3_switch_clocks(struct tg3 *tp)
  898. {
  899. u32 clock_ctrl;
  900. u32 orig_clock_ctrl;
  901. if (tg3_flag(tp, CPMU_PRESENT) || tg3_flag(tp, 5780_CLASS))
  902. return;
  903. clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
  904. orig_clock_ctrl = clock_ctrl;
  905. clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
  906. CLOCK_CTRL_CLKRUN_OENABLE |
  907. 0x1f);
  908. tp->pci_clock_ctrl = clock_ctrl;
  909. if (tg3_flag(tp, 5705_PLUS)) {
  910. if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
  911. tw32_wait_f(TG3PCI_CLOCK_CTRL,
  912. clock_ctrl | CLOCK_CTRL_625_CORE, 40);
  913. }
  914. } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
  915. tw32_wait_f(TG3PCI_CLOCK_CTRL,
  916. clock_ctrl |
  917. (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
  918. 40);
  919. tw32_wait_f(TG3PCI_CLOCK_CTRL,
  920. clock_ctrl | (CLOCK_CTRL_ALTCLK),
  921. 40);
  922. }
  923. tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
  924. }
  925. #define PHY_BUSY_LOOPS 5000
  926. static int __tg3_readphy(struct tg3 *tp, unsigned int phy_addr, int reg,
  927. u32 *val)
  928. {
  929. u32 frame_val;
  930. unsigned int loops;
  931. int ret;
  932. if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
  933. tw32_f(MAC_MI_MODE,
  934. (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
  935. udelay(80);
  936. }
  937. tg3_ape_lock(tp, tp->phy_ape_lock);
  938. *val = 0x0;
  939. frame_val = ((phy_addr << MI_COM_PHY_ADDR_SHIFT) &
  940. MI_COM_PHY_ADDR_MASK);
  941. frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
  942. MI_COM_REG_ADDR_MASK);
  943. frame_val |= (MI_COM_CMD_READ | MI_COM_START);
  944. tw32_f(MAC_MI_COM, frame_val);
  945. loops = PHY_BUSY_LOOPS;
  946. while (loops != 0) {
  947. udelay(10);
  948. frame_val = tr32(MAC_MI_COM);
  949. if ((frame_val & MI_COM_BUSY) == 0) {
  950. udelay(5);
  951. frame_val = tr32(MAC_MI_COM);
  952. break;
  953. }
  954. loops -= 1;
  955. }
  956. ret = -EBUSY;
  957. if (loops != 0) {
  958. *val = frame_val & MI_COM_DATA_MASK;
  959. ret = 0;
  960. }
  961. if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
  962. tw32_f(MAC_MI_MODE, tp->mi_mode);
  963. udelay(80);
  964. }
  965. tg3_ape_unlock(tp, tp->phy_ape_lock);
  966. return ret;
  967. }
  968. static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
  969. {
  970. return __tg3_readphy(tp, tp->phy_addr, reg, val);
  971. }
  972. static int __tg3_writephy(struct tg3 *tp, unsigned int phy_addr, int reg,
  973. u32 val)
  974. {
  975. u32 frame_val;
  976. unsigned int loops;
  977. int ret;
  978. if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
  979. (reg == MII_CTRL1000 || reg == MII_TG3_AUX_CTRL))
  980. return 0;
  981. if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
  982. tw32_f(MAC_MI_MODE,
  983. (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
  984. udelay(80);
  985. }
  986. tg3_ape_lock(tp, tp->phy_ape_lock);
  987. frame_val = ((phy_addr << MI_COM_PHY_ADDR_SHIFT) &
  988. MI_COM_PHY_ADDR_MASK);
  989. frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
  990. MI_COM_REG_ADDR_MASK);
  991. frame_val |= (val & MI_COM_DATA_MASK);
  992. frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
  993. tw32_f(MAC_MI_COM, frame_val);
  994. loops = PHY_BUSY_LOOPS;
  995. while (loops != 0) {
  996. udelay(10);
  997. frame_val = tr32(MAC_MI_COM);
  998. if ((frame_val & MI_COM_BUSY) == 0) {
  999. udelay(5);
  1000. frame_val = tr32(MAC_MI_COM);
  1001. break;
  1002. }
  1003. loops -= 1;
  1004. }
  1005. ret = -EBUSY;
  1006. if (loops != 0)
  1007. ret = 0;
  1008. if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
  1009. tw32_f(MAC_MI_MODE, tp->mi_mode);
  1010. udelay(80);
  1011. }
  1012. tg3_ape_unlock(tp, tp->phy_ape_lock);
  1013. return ret;
  1014. }
  1015. static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
  1016. {
  1017. return __tg3_writephy(tp, tp->phy_addr, reg, val);
  1018. }
  1019. static int tg3_phy_cl45_write(struct tg3 *tp, u32 devad, u32 addr, u32 val)
  1020. {
  1021. int err;
  1022. err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
  1023. if (err)
  1024. goto done;
  1025. err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
  1026. if (err)
  1027. goto done;
  1028. err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
  1029. MII_TG3_MMD_CTRL_DATA_NOINC | devad);
  1030. if (err)
  1031. goto done;
  1032. err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, val);
  1033. done:
  1034. return err;
  1035. }
  1036. static int tg3_phy_cl45_read(struct tg3 *tp, u32 devad, u32 addr, u32 *val)
  1037. {
  1038. int err;
  1039. err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
  1040. if (err)
  1041. goto done;
  1042. err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
  1043. if (err)
  1044. goto done;
  1045. err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
  1046. MII_TG3_MMD_CTRL_DATA_NOINC | devad);
  1047. if (err)
  1048. goto done;
  1049. err = tg3_readphy(tp, MII_TG3_MMD_ADDRESS, val);
  1050. done:
  1051. return err;
  1052. }
  1053. static int tg3_phydsp_read(struct tg3 *tp, u32 reg, u32 *val)
  1054. {
  1055. int err;
  1056. err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
  1057. if (!err)
  1058. err = tg3_readphy(tp, MII_TG3_DSP_RW_PORT, val);
  1059. return err;
  1060. }
  1061. static int tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
  1062. {
  1063. int err;
  1064. err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
  1065. if (!err)
  1066. err = tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
  1067. return err;
  1068. }
  1069. static int tg3_phy_auxctl_read(struct tg3 *tp, int reg, u32 *val)
  1070. {
  1071. int err;
  1072. err = tg3_writephy(tp, MII_TG3_AUX_CTRL,
  1073. (reg << MII_TG3_AUXCTL_MISC_RDSEL_SHIFT) |
  1074. MII_TG3_AUXCTL_SHDWSEL_MISC);
  1075. if (!err)
  1076. err = tg3_readphy(tp, MII_TG3_AUX_CTRL, val);
  1077. return err;
  1078. }
  1079. static int tg3_phy_auxctl_write(struct tg3 *tp, int reg, u32 set)
  1080. {
  1081. if (reg == MII_TG3_AUXCTL_SHDWSEL_MISC)
  1082. set |= MII_TG3_AUXCTL_MISC_WREN;
  1083. return tg3_writephy(tp, MII_TG3_AUX_CTRL, set | reg);
  1084. }
  1085. static int tg3_phy_toggle_auxctl_smdsp(struct tg3 *tp, bool enable)
  1086. {
  1087. u32 val;
  1088. int err;
  1089. err = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
  1090. if (err)
  1091. return err;
  1092. if (enable)
  1093. val |= MII_TG3_AUXCTL_ACTL_SMDSP_ENA;
  1094. else
  1095. val &= ~MII_TG3_AUXCTL_ACTL_SMDSP_ENA;
  1096. err = tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
  1097. val | MII_TG3_AUXCTL_ACTL_TX_6DB);
  1098. return err;
  1099. }
  1100. static int tg3_phy_shdw_write(struct tg3 *tp, int reg, u32 val)
  1101. {
  1102. return tg3_writephy(tp, MII_TG3_MISC_SHDW,
  1103. reg | val | MII_TG3_MISC_SHDW_WREN);
  1104. }
  1105. static int tg3_bmcr_reset(struct tg3 *tp)
  1106. {
  1107. u32 phy_control;
  1108. int limit, err;
  1109. /* OK, reset it, and poll the BMCR_RESET bit until it
  1110. * clears or we time out.
  1111. */
  1112. phy_control = BMCR_RESET;
  1113. err = tg3_writephy(tp, MII_BMCR, phy_control);
  1114. if (err != 0)
  1115. return -EBUSY;
  1116. limit = 5000;
  1117. while (limit--) {
  1118. err = tg3_readphy(tp, MII_BMCR, &phy_control);
  1119. if (err != 0)
  1120. return -EBUSY;
  1121. if ((phy_control & BMCR_RESET) == 0) {
  1122. udelay(40);
  1123. break;
  1124. }
  1125. udelay(10);
  1126. }
  1127. if (limit < 0)
  1128. return -EBUSY;
  1129. return 0;
  1130. }
  1131. static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
  1132. {
  1133. struct tg3 *tp = bp->priv;
  1134. u32 val;
  1135. spin_lock_bh(&tp->lock);
  1136. if (__tg3_readphy(tp, mii_id, reg, &val))
  1137. val = -EIO;
  1138. spin_unlock_bh(&tp->lock);
  1139. return val;
  1140. }
  1141. static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
  1142. {
  1143. struct tg3 *tp = bp->priv;
  1144. u32 ret = 0;
  1145. spin_lock_bh(&tp->lock);
  1146. if (__tg3_writephy(tp, mii_id, reg, val))
  1147. ret = -EIO;
  1148. spin_unlock_bh(&tp->lock);
  1149. return ret;
  1150. }
  1151. static void tg3_mdio_config_5785(struct tg3 *tp)
  1152. {
  1153. u32 val;
  1154. struct phy_device *phydev;
  1155. phydev = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr);
  1156. switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
  1157. case PHY_ID_BCM50610:
  1158. case PHY_ID_BCM50610M:
  1159. val = MAC_PHYCFG2_50610_LED_MODES;
  1160. break;
  1161. case PHY_ID_BCMAC131:
  1162. val = MAC_PHYCFG2_AC131_LED_MODES;
  1163. break;
  1164. case PHY_ID_RTL8211C:
  1165. val = MAC_PHYCFG2_RTL8211C_LED_MODES;
  1166. break;
  1167. case PHY_ID_RTL8201E:
  1168. val = MAC_PHYCFG2_RTL8201E_LED_MODES;
  1169. break;
  1170. default:
  1171. return;
  1172. }
  1173. if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
  1174. tw32(MAC_PHYCFG2, val);
  1175. val = tr32(MAC_PHYCFG1);
  1176. val &= ~(MAC_PHYCFG1_RGMII_INT |
  1177. MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
  1178. val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
  1179. tw32(MAC_PHYCFG1, val);
  1180. return;
  1181. }
  1182. if (!tg3_flag(tp, RGMII_INBAND_DISABLE))
  1183. val |= MAC_PHYCFG2_EMODE_MASK_MASK |
  1184. MAC_PHYCFG2_FMODE_MASK_MASK |
  1185. MAC_PHYCFG2_GMODE_MASK_MASK |
  1186. MAC_PHYCFG2_ACT_MASK_MASK |
  1187. MAC_PHYCFG2_QUAL_MASK_MASK |
  1188. MAC_PHYCFG2_INBAND_ENABLE;
  1189. tw32(MAC_PHYCFG2, val);
  1190. val = tr32(MAC_PHYCFG1);
  1191. val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
  1192. MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
  1193. if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
  1194. if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
  1195. val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
  1196. if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
  1197. val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
  1198. }
  1199. val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
  1200. MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
  1201. tw32(MAC_PHYCFG1, val);
  1202. val = tr32(MAC_EXT_RGMII_MODE);
  1203. val &= ~(MAC_RGMII_MODE_RX_INT_B |
  1204. MAC_RGMII_MODE_RX_QUALITY |
  1205. MAC_RGMII_MODE_RX_ACTIVITY |
  1206. MAC_RGMII_MODE_RX_ENG_DET |
  1207. MAC_RGMII_MODE_TX_ENABLE |
  1208. MAC_RGMII_MODE_TX_LOWPWR |
  1209. MAC_RGMII_MODE_TX_RESET);
  1210. if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
  1211. if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
  1212. val |= MAC_RGMII_MODE_RX_INT_B |
  1213. MAC_RGMII_MODE_RX_QUALITY |
  1214. MAC_RGMII_MODE_RX_ACTIVITY |
  1215. MAC_RGMII_MODE_RX_ENG_DET;
  1216. if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
  1217. val |= MAC_RGMII_MODE_TX_ENABLE |
  1218. MAC_RGMII_MODE_TX_LOWPWR |
  1219. MAC_RGMII_MODE_TX_RESET;
  1220. }
  1221. tw32(MAC_EXT_RGMII_MODE, val);
  1222. }
  1223. static void tg3_mdio_start(struct tg3 *tp)
  1224. {
  1225. tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
  1226. tw32_f(MAC_MI_MODE, tp->mi_mode);
  1227. udelay(80);
  1228. if (tg3_flag(tp, MDIOBUS_INITED) &&
  1229. tg3_asic_rev(tp) == ASIC_REV_5785)
  1230. tg3_mdio_config_5785(tp);
  1231. }
  1232. static int tg3_mdio_init(struct tg3 *tp)
  1233. {
  1234. int i;
  1235. u32 reg;
  1236. struct phy_device *phydev;
  1237. if (tg3_flag(tp, 5717_PLUS)) {
  1238. u32 is_serdes;
  1239. tp->phy_addr = tp->pci_fn + 1;
  1240. if (tg3_chip_rev_id(tp) != CHIPREV_ID_5717_A0)
  1241. is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
  1242. else
  1243. is_serdes = tr32(TG3_CPMU_PHY_STRAP) &
  1244. TG3_CPMU_PHY_STRAP_IS_SERDES;
  1245. if (is_serdes)
  1246. tp->phy_addr += 7;
  1247. } else if (tg3_flag(tp, IS_SSB_CORE) && tg3_flag(tp, ROBOSWITCH)) {
  1248. int addr;
  1249. addr = ssb_gige_get_phyaddr(tp->pdev);
  1250. if (addr < 0)
  1251. return addr;
  1252. tp->phy_addr = addr;
  1253. } else
  1254. tp->phy_addr = TG3_PHY_MII_ADDR;
  1255. tg3_mdio_start(tp);
  1256. if (!tg3_flag(tp, USE_PHYLIB) || tg3_flag(tp, MDIOBUS_INITED))
  1257. return 0;
  1258. tp->mdio_bus = mdiobus_alloc();
  1259. if (tp->mdio_bus == NULL)
  1260. return -ENOMEM;
  1261. tp->mdio_bus->name = "tg3 mdio bus";
  1262. snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
  1263. (tp->pdev->bus->number << 8) | tp->pdev->devfn);
  1264. tp->mdio_bus->priv = tp;
  1265. tp->mdio_bus->parent = &tp->pdev->dev;
  1266. tp->mdio_bus->read = &tg3_mdio_read;
  1267. tp->mdio_bus->write = &tg3_mdio_write;
  1268. tp->mdio_bus->phy_mask = ~(1 << tp->phy_addr);
  1269. /* The bus registration will look for all the PHYs on the mdio bus.
  1270. * Unfortunately, it does not ensure the PHY is powered up before
  1271. * accessing the PHY ID registers. A chip reset is the
  1272. * quickest way to bring the device back to an operational state..
  1273. */
  1274. if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
  1275. tg3_bmcr_reset(tp);
  1276. i = mdiobus_register(tp->mdio_bus);
  1277. if (i) {
  1278. dev_warn(&tp->pdev->dev, "mdiobus_reg failed (0x%x)\n", i);
  1279. mdiobus_free(tp->mdio_bus);
  1280. return i;
  1281. }
  1282. phydev = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr);
  1283. if (!phydev || !phydev->drv) {
  1284. dev_warn(&tp->pdev->dev, "No PHY devices\n");
  1285. mdiobus_unregister(tp->mdio_bus);
  1286. mdiobus_free(tp->mdio_bus);
  1287. return -ENODEV;
  1288. }
  1289. switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
  1290. case PHY_ID_BCM57780:
  1291. phydev->interface = PHY_INTERFACE_MODE_GMII;
  1292. phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
  1293. break;
  1294. case PHY_ID_BCM50610:
  1295. case PHY_ID_BCM50610M:
  1296. phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
  1297. PHY_BRCM_RX_REFCLK_UNUSED |
  1298. PHY_BRCM_DIS_TXCRXC_NOENRGY |
  1299. PHY_BRCM_AUTO_PWRDWN_ENABLE;
  1300. if (tg3_flag(tp, RGMII_INBAND_DISABLE))
  1301. phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
  1302. if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
  1303. phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
  1304. if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
  1305. phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
  1306. /* fallthru */
  1307. case PHY_ID_RTL8211C:
  1308. phydev->interface = PHY_INTERFACE_MODE_RGMII;
  1309. break;
  1310. case PHY_ID_RTL8201E:
  1311. case PHY_ID_BCMAC131:
  1312. phydev->interface = PHY_INTERFACE_MODE_MII;
  1313. phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
  1314. tp->phy_flags |= TG3_PHYFLG_IS_FET;
  1315. break;
  1316. }
  1317. tg3_flag_set(tp, MDIOBUS_INITED);
  1318. if (tg3_asic_rev(tp) == ASIC_REV_5785)
  1319. tg3_mdio_config_5785(tp);
  1320. return 0;
  1321. }
  1322. static void tg3_mdio_fini(struct tg3 *tp)
  1323. {
  1324. if (tg3_flag(tp, MDIOBUS_INITED)) {
  1325. tg3_flag_clear(tp, MDIOBUS_INITED);
  1326. mdiobus_unregister(tp->mdio_bus);
  1327. mdiobus_free(tp->mdio_bus);
  1328. }
  1329. }
  1330. /* tp->lock is held. */
  1331. static inline void tg3_generate_fw_event(struct tg3 *tp)
  1332. {
  1333. u32 val;
  1334. val = tr32(GRC_RX_CPU_EVENT);
  1335. val |= GRC_RX_CPU_DRIVER_EVENT;
  1336. tw32_f(GRC_RX_CPU_EVENT, val);
  1337. tp->last_event_jiffies = jiffies;
  1338. }
  1339. #define TG3_FW_EVENT_TIMEOUT_USEC 2500
  1340. /* tp->lock is held. */
  1341. static void tg3_wait_for_event_ack(struct tg3 *tp)
  1342. {
  1343. int i;
  1344. unsigned int delay_cnt;
  1345. long time_remain;
  1346. /* If enough time has passed, no wait is necessary. */
  1347. time_remain = (long)(tp->last_event_jiffies + 1 +
  1348. usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
  1349. (long)jiffies;
  1350. if (time_remain < 0)
  1351. return;
  1352. /* Check if we can shorten the wait time. */
  1353. delay_cnt = jiffies_to_usecs(time_remain);
  1354. if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
  1355. delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
  1356. delay_cnt = (delay_cnt >> 3) + 1;
  1357. for (i = 0; i < delay_cnt; i++) {
  1358. if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
  1359. break;
  1360. if (pci_channel_offline(tp->pdev))
  1361. break;
  1362. udelay(8);
  1363. }
  1364. }
  1365. /* tp->lock is held. */
  1366. static void tg3_phy_gather_ump_data(struct tg3 *tp, u32 *data)
  1367. {
  1368. u32 reg, val;
  1369. val = 0;
  1370. if (!tg3_readphy(tp, MII_BMCR, &reg))
  1371. val = reg << 16;
  1372. if (!tg3_readphy(tp, MII_BMSR, &reg))
  1373. val |= (reg & 0xffff);
  1374. *data++ = val;
  1375. val = 0;
  1376. if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
  1377. val = reg << 16;
  1378. if (!tg3_readphy(tp, MII_LPA, &reg))
  1379. val |= (reg & 0xffff);
  1380. *data++ = val;
  1381. val = 0;
  1382. if (!(tp->phy_flags & TG3_PHYFLG_MII_SERDES)) {
  1383. if (!tg3_readphy(tp, MII_CTRL1000, &reg))
  1384. val = reg << 16;
  1385. if (!tg3_readphy(tp, MII_STAT1000, &reg))
  1386. val |= (reg & 0xffff);
  1387. }
  1388. *data++ = val;
  1389. if (!tg3_readphy(tp, MII_PHYADDR, &reg))
  1390. val = reg << 16;
  1391. else
  1392. val = 0;
  1393. *data++ = val;
  1394. }
  1395. /* tp->lock is held. */
  1396. static void tg3_ump_link_report(struct tg3 *tp)
  1397. {
  1398. u32 data[4];
  1399. if (!tg3_flag(tp, 5780_CLASS) || !tg3_flag(tp, ENABLE_ASF))
  1400. return;
  1401. tg3_phy_gather_ump_data(tp, data);
  1402. tg3_wait_for_event_ack(tp);
  1403. tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
  1404. tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
  1405. tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x0, data[0]);
  1406. tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x4, data[1]);
  1407. tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x8, data[2]);
  1408. tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0xc, data[3]);
  1409. tg3_generate_fw_event(tp);
  1410. }
  1411. /* tp->lock is held. */
  1412. static void tg3_stop_fw(struct tg3 *tp)
  1413. {
  1414. if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
  1415. /* Wait for RX cpu to ACK the previous event. */
  1416. tg3_wait_for_event_ack(tp);
  1417. tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
  1418. tg3_generate_fw_event(tp);
  1419. /* Wait for RX cpu to ACK this event. */
  1420. tg3_wait_for_event_ack(tp);
  1421. }
  1422. }
  1423. /* tp->lock is held. */
  1424. static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
  1425. {
  1426. tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
  1427. NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
  1428. if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
  1429. switch (kind) {
  1430. case RESET_KIND_INIT:
  1431. tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
  1432. DRV_STATE_START);
  1433. break;
  1434. case RESET_KIND_SHUTDOWN:
  1435. tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
  1436. DRV_STATE_UNLOAD);
  1437. break;
  1438. case RESET_KIND_SUSPEND:
  1439. tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
  1440. DRV_STATE_SUSPEND);
  1441. break;
  1442. default:
  1443. break;
  1444. }
  1445. }
  1446. }
  1447. /* tp->lock is held. */
  1448. static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
  1449. {
  1450. if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
  1451. switch (kind) {
  1452. case RESET_KIND_INIT:
  1453. tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
  1454. DRV_STATE_START_DONE);
  1455. break;
  1456. case RESET_KIND_SHUTDOWN:
  1457. tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
  1458. DRV_STATE_UNLOAD_DONE);
  1459. break;
  1460. default:
  1461. break;
  1462. }
  1463. }
  1464. }
  1465. /* tp->lock is held. */
  1466. static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
  1467. {
  1468. if (tg3_flag(tp, ENABLE_ASF)) {
  1469. switch (kind) {
  1470. case RESET_KIND_INIT:
  1471. tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
  1472. DRV_STATE_START);
  1473. break;
  1474. case RESET_KIND_SHUTDOWN:
  1475. tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
  1476. DRV_STATE_UNLOAD);
  1477. break;
  1478. case RESET_KIND_SUSPEND:
  1479. tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
  1480. DRV_STATE_SUSPEND);
  1481. break;
  1482. default:
  1483. break;
  1484. }
  1485. }
  1486. }
  1487. static int tg3_poll_fw(struct tg3 *tp)
  1488. {
  1489. int i;
  1490. u32 val;
  1491. if (tg3_flag(tp, NO_FWARE_REPORTED))
  1492. return 0;
  1493. if (tg3_flag(tp, IS_SSB_CORE)) {
  1494. /* We don't use firmware. */
  1495. return 0;
  1496. }
  1497. if (tg3_asic_rev(tp) == ASIC_REV_5906) {
  1498. /* Wait up to 20ms for init done. */
  1499. for (i = 0; i < 200; i++) {
  1500. if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
  1501. return 0;
  1502. if (pci_channel_offline(tp->pdev))
  1503. return -ENODEV;
  1504. udelay(100);
  1505. }
  1506. return -ENODEV;
  1507. }
  1508. /* Wait for firmware initialization to complete. */
  1509. for (i = 0; i < 100000; i++) {
  1510. tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
  1511. if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
  1512. break;
  1513. if (pci_channel_offline(tp->pdev)) {
  1514. if (!tg3_flag(tp, NO_FWARE_REPORTED)) {
  1515. tg3_flag_set(tp, NO_FWARE_REPORTED);
  1516. netdev_info(tp->dev, "No firmware running\n");
  1517. }
  1518. break;
  1519. }
  1520. udelay(10);
  1521. }
  1522. /* Chip might not be fitted with firmware. Some Sun onboard
  1523. * parts are configured like that. So don't signal the timeout
  1524. * of the above loop as an error, but do report the lack of
  1525. * running firmware once.
  1526. */
  1527. if (i >= 100000 && !tg3_flag(tp, NO_FWARE_REPORTED)) {
  1528. tg3_flag_set(tp, NO_FWARE_REPORTED);
  1529. netdev_info(tp->dev, "No firmware running\n");
  1530. }
  1531. if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0) {
  1532. /* The 57765 A0 needs a little more
  1533. * time to do some important work.
  1534. */
  1535. mdelay(10);
  1536. }
  1537. return 0;
  1538. }
  1539. static void tg3_link_report(struct tg3 *tp)
  1540. {
  1541. if (!netif_carrier_ok(tp->dev)) {
  1542. netif_info(tp, link, tp->dev, "Link is down\n");
  1543. tg3_ump_link_report(tp);
  1544. } else if (netif_msg_link(tp)) {
  1545. netdev_info(tp->dev, "Link is up at %d Mbps, %s duplex\n",
  1546. (tp->link_config.active_speed == SPEED_1000 ?
  1547. 1000 :
  1548. (tp->link_config.active_speed == SPEED_100 ?
  1549. 100 : 10)),
  1550. (tp->link_config.active_duplex == DUPLEX_FULL ?
  1551. "full" : "half"));
  1552. netdev_info(tp->dev, "Flow control is %s for TX and %s for RX\n",
  1553. (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
  1554. "on" : "off",
  1555. (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
  1556. "on" : "off");
  1557. if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
  1558. netdev_info(tp->dev, "EEE is %s\n",
  1559. tp->setlpicnt ? "enabled" : "disabled");
  1560. tg3_ump_link_report(tp);
  1561. }
  1562. tp->link_up = netif_carrier_ok(tp->dev);
  1563. }
  1564. static u32 tg3_decode_flowctrl_1000T(u32 adv)
  1565. {
  1566. u32 flowctrl = 0;
  1567. if (adv & ADVERTISE_PAUSE_CAP) {
  1568. flowctrl |= FLOW_CTRL_RX;
  1569. if (!(adv & ADVERTISE_PAUSE_ASYM))
  1570. flowctrl |= FLOW_CTRL_TX;
  1571. } else if (adv & ADVERTISE_PAUSE_ASYM)
  1572. flowctrl |= FLOW_CTRL_TX;
  1573. return flowctrl;
  1574. }
  1575. static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
  1576. {
  1577. u16 miireg;
  1578. if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
  1579. miireg = ADVERTISE_1000XPAUSE;
  1580. else if (flow_ctrl & FLOW_CTRL_TX)
  1581. miireg = ADVERTISE_1000XPSE_ASYM;
  1582. else if (flow_ctrl & FLOW_CTRL_RX)
  1583. miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
  1584. else
  1585. miireg = 0;
  1586. return miireg;
  1587. }
  1588. static u32 tg3_decode_flowctrl_1000X(u32 adv)
  1589. {
  1590. u32 flowctrl = 0;
  1591. if (adv & ADVERTISE_1000XPAUSE) {
  1592. flowctrl |= FLOW_CTRL_RX;
  1593. if (!(adv & ADVERTISE_1000XPSE_ASYM))
  1594. flowctrl |= FLOW_CTRL_TX;
  1595. } else if (adv & ADVERTISE_1000XPSE_ASYM)
  1596. flowctrl |= FLOW_CTRL_TX;
  1597. return flowctrl;
  1598. }
  1599. static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
  1600. {
  1601. u8 cap = 0;
  1602. if (lcladv & rmtadv & ADVERTISE_1000XPAUSE) {
  1603. cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
  1604. } else if (lcladv & rmtadv & ADVERTISE_1000XPSE_ASYM) {
  1605. if (lcladv & ADVERTISE_1000XPAUSE)
  1606. cap = FLOW_CTRL_RX;
  1607. if (rmtadv & ADVERTISE_1000XPAUSE)
  1608. cap = FLOW_CTRL_TX;
  1609. }
  1610. return cap;
  1611. }
  1612. static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
  1613. {
  1614. u8 autoneg;
  1615. u8 flowctrl = 0;
  1616. u32 old_rx_mode = tp->rx_mode;
  1617. u32 old_tx_mode = tp->tx_mode;
  1618. if (tg3_flag(tp, USE_PHYLIB))
  1619. autoneg = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr)->autoneg;
  1620. else
  1621. autoneg = tp->link_config.autoneg;
  1622. if (autoneg == AUTONEG_ENABLE && tg3_flag(tp, PAUSE_AUTONEG)) {
  1623. if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
  1624. flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
  1625. else
  1626. flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
  1627. } else
  1628. flowctrl = tp->link_config.flowctrl;
  1629. tp->link_config.active_flowctrl = flowctrl;
  1630. if (flowctrl & FLOW_CTRL_RX)
  1631. tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
  1632. else
  1633. tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
  1634. if (old_rx_mode != tp->rx_mode)
  1635. tw32_f(MAC_RX_MODE, tp->rx_mode);
  1636. if (flowctrl & FLOW_CTRL_TX)
  1637. tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
  1638. else
  1639. tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
  1640. if (old_tx_mode != tp->tx_mode)
  1641. tw32_f(MAC_TX_MODE, tp->tx_mode);
  1642. }
  1643. static void tg3_adjust_link(struct net_device *dev)
  1644. {
  1645. u8 oldflowctrl, linkmesg = 0;
  1646. u32 mac_mode, lcl_adv, rmt_adv;
  1647. struct tg3 *tp = netdev_priv(dev);
  1648. struct phy_device *phydev = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr);
  1649. spin_lock_bh(&tp->lock);
  1650. mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
  1651. MAC_MODE_HALF_DUPLEX);
  1652. oldflowctrl = tp->link_config.active_flowctrl;
  1653. if (phydev->link) {
  1654. lcl_adv = 0;
  1655. rmt_adv = 0;
  1656. if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
  1657. mac_mode |= MAC_MODE_PORT_MODE_MII;
  1658. else if (phydev->speed == SPEED_1000 ||
  1659. tg3_asic_rev(tp) != ASIC_REV_5785)
  1660. mac_mode |= MAC_MODE_PORT_MODE_GMII;
  1661. else
  1662. mac_mode |= MAC_MODE_PORT_MODE_MII;
  1663. if (phydev->duplex == DUPLEX_HALF)
  1664. mac_mode |= MAC_MODE_HALF_DUPLEX;
  1665. else {
  1666. lcl_adv = mii_advertise_flowctrl(
  1667. tp->link_config.flowctrl);
  1668. if (phydev->pause)
  1669. rmt_adv = LPA_PAUSE_CAP;
  1670. if (phydev->asym_pause)
  1671. rmt_adv |= LPA_PAUSE_ASYM;
  1672. }
  1673. tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
  1674. } else
  1675. mac_mode |= MAC_MODE_PORT_MODE_GMII;
  1676. if (mac_mode != tp->mac_mode) {
  1677. tp->mac_mode = mac_mode;
  1678. tw32_f(MAC_MODE, tp->mac_mode);
  1679. udelay(40);
  1680. }
  1681. if (tg3_asic_rev(tp) == ASIC_REV_5785) {
  1682. if (phydev->speed == SPEED_10)
  1683. tw32(MAC_MI_STAT,
  1684. MAC_MI_STAT_10MBPS_MODE |
  1685. MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
  1686. else
  1687. tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
  1688. }
  1689. if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
  1690. tw32(MAC_TX_LENGTHS,
  1691. ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
  1692. (6 << TX_LENGTHS_IPG_SHIFT) |
  1693. (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
  1694. else
  1695. tw32(MAC_TX_LENGTHS,
  1696. ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
  1697. (6 << TX_LENGTHS_IPG_SHIFT) |
  1698. (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
  1699. if (phydev->link != tp->old_link ||
  1700. phydev->speed != tp->link_config.active_speed ||
  1701. phydev->duplex != tp->link_config.active_duplex ||
  1702. oldflowctrl != tp->link_config.active_flowctrl)
  1703. linkmesg = 1;
  1704. tp->old_link = phydev->link;
  1705. tp->link_config.active_speed = phydev->speed;
  1706. tp->link_config.active_duplex = phydev->duplex;
  1707. spin_unlock_bh(&tp->lock);
  1708. if (linkmesg)
  1709. tg3_link_report(tp);
  1710. }
  1711. static int tg3_phy_init(struct tg3 *tp)
  1712. {
  1713. struct phy_device *phydev;
  1714. if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)
  1715. return 0;
  1716. /* Bring the PHY back to a known state. */
  1717. tg3_bmcr_reset(tp);
  1718. phydev = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr);
  1719. /* Attach the MAC to the PHY. */
  1720. phydev = phy_connect(tp->dev, phydev_name(phydev),
  1721. tg3_adjust_link, phydev->interface);
  1722. if (IS_ERR(phydev)) {
  1723. dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
  1724. return PTR_ERR(phydev);
  1725. }
  1726. /* Mask with MAC supported features. */
  1727. switch (phydev->interface) {
  1728. case PHY_INTERFACE_MODE_GMII:
  1729. case PHY_INTERFACE_MODE_RGMII:
  1730. if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
  1731. phydev->supported &= (PHY_GBIT_FEATURES |
  1732. SUPPORTED_Pause |
  1733. SUPPORTED_Asym_Pause);
  1734. break;
  1735. }
  1736. /* fallthru */
  1737. case PHY_INTERFACE_MODE_MII:
  1738. phydev->supported &= (PHY_BASIC_FEATURES |
  1739. SUPPORTED_Pause |
  1740. SUPPORTED_Asym_Pause);
  1741. break;
  1742. default:
  1743. phy_disconnect(mdiobus_get_phy(tp->mdio_bus, tp->phy_addr));
  1744. return -EINVAL;
  1745. }
  1746. tp->phy_flags |= TG3_PHYFLG_IS_CONNECTED;
  1747. phydev->advertising = phydev->supported;
  1748. phy_attached_info(phydev);
  1749. return 0;
  1750. }
  1751. static void tg3_phy_start(struct tg3 *tp)
  1752. {
  1753. struct phy_device *phydev;
  1754. if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
  1755. return;
  1756. phydev = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr);
  1757. if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
  1758. tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
  1759. phydev->speed = tp->link_config.speed;
  1760. phydev->duplex = tp->link_config.duplex;
  1761. phydev->autoneg = tp->link_config.autoneg;
  1762. phydev->advertising = tp->link_config.advertising;
  1763. }
  1764. phy_start(phydev);
  1765. phy_start_aneg(phydev);
  1766. }
  1767. static void tg3_phy_stop(struct tg3 *tp)
  1768. {
  1769. if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
  1770. return;
  1771. phy_stop(mdiobus_get_phy(tp->mdio_bus, tp->phy_addr));
  1772. }
  1773. static void tg3_phy_fini(struct tg3 *tp)
  1774. {
  1775. if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
  1776. phy_disconnect(mdiobus_get_phy(tp->mdio_bus, tp->phy_addr));
  1777. tp->phy_flags &= ~TG3_PHYFLG_IS_CONNECTED;
  1778. }
  1779. }
  1780. static int tg3_phy_set_extloopbk(struct tg3 *tp)
  1781. {
  1782. int err;
  1783. u32 val;
  1784. if (tp->phy_flags & TG3_PHYFLG_IS_FET)
  1785. return 0;
  1786. if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
  1787. /* Cannot do read-modify-write on 5401 */
  1788. err = tg3_phy_auxctl_write(tp,
  1789. MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
  1790. MII_TG3_AUXCTL_ACTL_EXTLOOPBK |
  1791. 0x4c20);
  1792. goto done;
  1793. }
  1794. err = tg3_phy_auxctl_read(tp,
  1795. MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
  1796. if (err)
  1797. return err;
  1798. val |= MII_TG3_AUXCTL_ACTL_EXTLOOPBK;
  1799. err = tg3_phy_auxctl_write(tp,
  1800. MII_TG3_AUXCTL_SHDWSEL_AUXCTL, val);
  1801. done:
  1802. return err;
  1803. }
  1804. static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
  1805. {
  1806. u32 phytest;
  1807. if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
  1808. u32 phy;
  1809. tg3_writephy(tp, MII_TG3_FET_TEST,
  1810. phytest | MII_TG3_FET_SHADOW_EN);
  1811. if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
  1812. if (enable)
  1813. phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
  1814. else
  1815. phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
  1816. tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
  1817. }
  1818. tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
  1819. }
  1820. }
  1821. static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
  1822. {
  1823. u32 reg;
  1824. if (!tg3_flag(tp, 5705_PLUS) ||
  1825. (tg3_flag(tp, 5717_PLUS) &&
  1826. (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
  1827. return;
  1828. if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
  1829. tg3_phy_fet_toggle_apd(tp, enable);
  1830. return;
  1831. }
  1832. reg = MII_TG3_MISC_SHDW_SCR5_LPED |
  1833. MII_TG3_MISC_SHDW_SCR5_DLPTLM |
  1834. MII_TG3_MISC_SHDW_SCR5_SDTL |
  1835. MII_TG3_MISC_SHDW_SCR5_C125OE;
  1836. if (tg3_asic_rev(tp) != ASIC_REV_5784 || !enable)
  1837. reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
  1838. tg3_phy_shdw_write(tp, MII_TG3_MISC_SHDW_SCR5_SEL, reg);
  1839. reg = MII_TG3_MISC_SHDW_APD_WKTM_84MS;
  1840. if (enable)
  1841. reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
  1842. tg3_phy_shdw_write(tp, MII_TG3_MISC_SHDW_APD_SEL, reg);
  1843. }
  1844. static void tg3_phy_toggle_automdix(struct tg3 *tp, bool enable)
  1845. {
  1846. u32 phy;
  1847. if (!tg3_flag(tp, 5705_PLUS) ||
  1848. (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
  1849. return;
  1850. if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
  1851. u32 ephy;
  1852. if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
  1853. u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
  1854. tg3_writephy(tp, MII_TG3_FET_TEST,
  1855. ephy | MII_TG3_FET_SHADOW_EN);
  1856. if (!tg3_readphy(tp, reg, &phy)) {
  1857. if (enable)
  1858. phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
  1859. else
  1860. phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
  1861. tg3_writephy(tp, reg, phy);
  1862. }
  1863. tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
  1864. }
  1865. } else {
  1866. int ret;
  1867. ret = tg3_phy_auxctl_read(tp,
  1868. MII_TG3_AUXCTL_SHDWSEL_MISC, &phy);
  1869. if (!ret) {
  1870. if (enable)
  1871. phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
  1872. else
  1873. phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
  1874. tg3_phy_auxctl_write(tp,
  1875. MII_TG3_AUXCTL_SHDWSEL_MISC, phy);
  1876. }
  1877. }
  1878. }
  1879. static void tg3_phy_set_wirespeed(struct tg3 *tp)
  1880. {
  1881. int ret;
  1882. u32 val;
  1883. if (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED)
  1884. return;
  1885. ret = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_MISC, &val);
  1886. if (!ret)
  1887. tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_MISC,
  1888. val | MII_TG3_AUXCTL_MISC_WIRESPD_EN);
  1889. }
  1890. static void tg3_phy_apply_otp(struct tg3 *tp)
  1891. {
  1892. u32 otp, phy;
  1893. if (!tp->phy_otp)
  1894. return;
  1895. otp = tp->phy_otp;
  1896. if (tg3_phy_toggle_auxctl_smdsp(tp, true))
  1897. return;
  1898. phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
  1899. phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
  1900. tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
  1901. phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
  1902. ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
  1903. tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
  1904. phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
  1905. phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
  1906. tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
  1907. phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
  1908. tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
  1909. phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
  1910. tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
  1911. phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
  1912. ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
  1913. tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
  1914. tg3_phy_toggle_auxctl_smdsp(tp, false);
  1915. }
  1916. static void tg3_eee_pull_config(struct tg3 *tp, struct ethtool_eee *eee)
  1917. {
  1918. u32 val;
  1919. struct ethtool_eee *dest = &tp->eee;
  1920. if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
  1921. return;
  1922. if (eee)
  1923. dest = eee;
  1924. if (tg3_phy_cl45_read(tp, MDIO_MMD_AN, TG3_CL45_D7_EEERES_STAT, &val))
  1925. return;
  1926. /* Pull eee_active */
  1927. if (val == TG3_CL45_D7_EEERES_STAT_LP_1000T ||
  1928. val == TG3_CL45_D7_EEERES_STAT_LP_100TX) {
  1929. dest->eee_active = 1;
  1930. } else
  1931. dest->eee_active = 0;
  1932. /* Pull lp advertised settings */
  1933. if (tg3_phy_cl45_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE, &val))
  1934. return;
  1935. dest->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(val);
  1936. /* Pull advertised and eee_enabled settings */
  1937. if (tg3_phy_cl45_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, &val))
  1938. return;
  1939. dest->eee_enabled = !!val;
  1940. dest->advertised = mmd_eee_adv_to_ethtool_adv_t(val);
  1941. /* Pull tx_lpi_enabled */
  1942. val = tr32(TG3_CPMU_EEE_MODE);
  1943. dest->tx_lpi_enabled = !!(val & TG3_CPMU_EEEMD_LPI_IN_TX);
  1944. /* Pull lpi timer value */
  1945. dest->tx_lpi_timer = tr32(TG3_CPMU_EEE_DBTMR1) & 0xffff;
  1946. }
  1947. static void tg3_phy_eee_adjust(struct tg3 *tp, bool current_link_up)
  1948. {
  1949. u32 val;
  1950. if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
  1951. return;
  1952. tp->setlpicnt = 0;
  1953. if (tp->link_config.autoneg == AUTONEG_ENABLE &&
  1954. current_link_up &&
  1955. tp->link_config.active_duplex == DUPLEX_FULL &&
  1956. (tp->link_config.active_speed == SPEED_100 ||
  1957. tp->link_config.active_speed == SPEED_1000)) {
  1958. u32 eeectl;
  1959. if (tp->link_config.active_speed == SPEED_1000)
  1960. eeectl = TG3_CPMU_EEE_CTRL_EXIT_16_5_US;
  1961. else
  1962. eeectl = TG3_CPMU_EEE_CTRL_EXIT_36_US;
  1963. tw32(TG3_CPMU_EEE_CTRL, eeectl);
  1964. tg3_eee_pull_config(tp, NULL);
  1965. if (tp->eee.eee_active)
  1966. tp->setlpicnt = 2;
  1967. }
  1968. if (!tp->setlpicnt) {
  1969. if (current_link_up &&
  1970. !tg3_phy_toggle_auxctl_smdsp(tp, true)) {
  1971. tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, 0x0000);
  1972. tg3_phy_toggle_auxctl_smdsp(tp, false);
  1973. }
  1974. val = tr32(TG3_CPMU_EEE_MODE);
  1975. tw32(TG3_CPMU_EEE_MODE, val & ~TG3_CPMU_EEEMD_LPI_ENABLE);
  1976. }
  1977. }
  1978. static void tg3_phy_eee_enable(struct tg3 *tp)
  1979. {
  1980. u32 val;
  1981. if (tp->link_config.active_speed == SPEED_1000 &&
  1982. (tg3_asic_rev(tp) == ASIC_REV_5717 ||
  1983. tg3_asic_rev(tp) == ASIC_REV_5719 ||
  1984. tg3_flag(tp, 57765_CLASS)) &&
  1985. !tg3_phy_toggle_auxctl_smdsp(tp, true)) {
  1986. val = MII_TG3_DSP_TAP26_ALNOKO |
  1987. MII_TG3_DSP_TAP26_RMRXSTO;
  1988. tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
  1989. tg3_phy_toggle_auxctl_smdsp(tp, false);
  1990. }
  1991. val = tr32(TG3_CPMU_EEE_MODE);
  1992. tw32(TG3_CPMU_EEE_MODE, val | TG3_CPMU_EEEMD_LPI_ENABLE);
  1993. }
  1994. static int tg3_wait_macro_done(struct tg3 *tp)
  1995. {
  1996. int limit = 100;
  1997. while (limit--) {
  1998. u32 tmp32;
  1999. if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
  2000. if ((tmp32 & 0x1000) == 0)
  2001. break;
  2002. }
  2003. }
  2004. if (limit < 0)
  2005. return -EBUSY;
  2006. return 0;
  2007. }
  2008. static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
  2009. {
  2010. static const u32 test_pat[4][6] = {
  2011. { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
  2012. { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
  2013. { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
  2014. { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
  2015. };
  2016. int chan;
  2017. for (chan = 0; chan < 4; chan++) {
  2018. int i;
  2019. tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
  2020. (chan * 0x2000) | 0x0200);
  2021. tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
  2022. for (i = 0; i < 6; i++)
  2023. tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
  2024. test_pat[chan][i]);
  2025. tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
  2026. if (tg3_wait_macro_done(tp)) {
  2027. *resetp = 1;
  2028. return -EBUSY;
  2029. }
  2030. tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
  2031. (chan * 0x2000) | 0x0200);
  2032. tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
  2033. if (tg3_wait_macro_done(tp)) {
  2034. *resetp = 1;
  2035. return -EBUSY;
  2036. }
  2037. tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
  2038. if (tg3_wait_macro_done(tp)) {
  2039. *resetp = 1;
  2040. return -EBUSY;
  2041. }
  2042. for (i = 0; i < 6; i += 2) {
  2043. u32 low, high;
  2044. if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
  2045. tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
  2046. tg3_wait_macro_done(tp)) {
  2047. *resetp = 1;
  2048. return -EBUSY;
  2049. }
  2050. low &= 0x7fff;
  2051. high &= 0x000f;
  2052. if (low != test_pat[chan][i] ||
  2053. high != test_pat[chan][i+1]) {
  2054. tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
  2055. tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
  2056. tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
  2057. return -EBUSY;
  2058. }
  2059. }
  2060. }
  2061. return 0;
  2062. }
  2063. static int tg3_phy_reset_chanpat(struct tg3 *tp)
  2064. {
  2065. int chan;
  2066. for (chan = 0; chan < 4; chan++) {
  2067. int i;
  2068. tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
  2069. (chan * 0x2000) | 0x0200);
  2070. tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
  2071. for (i = 0; i < 6; i++)
  2072. tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
  2073. tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
  2074. if (tg3_wait_macro_done(tp))
  2075. return -EBUSY;
  2076. }
  2077. return 0;
  2078. }
  2079. static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
  2080. {
  2081. u32 reg32, phy9_orig;
  2082. int retries, do_phy_reset, err;
  2083. retries = 10;
  2084. do_phy_reset = 1;
  2085. do {
  2086. if (do_phy_reset) {
  2087. err = tg3_bmcr_reset(tp);
  2088. if (err)
  2089. return err;
  2090. do_phy_reset = 0;
  2091. }
  2092. /* Disable transmitter and interrupt. */
  2093. if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
  2094. continue;
  2095. reg32 |= 0x3000;
  2096. tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
  2097. /* Set full-duplex, 1000 mbps. */
  2098. tg3_writephy(tp, MII_BMCR,
  2099. BMCR_FULLDPLX | BMCR_SPEED1000);
  2100. /* Set to master mode. */
  2101. if (tg3_readphy(tp, MII_CTRL1000, &phy9_orig))
  2102. continue;
  2103. tg3_writephy(tp, MII_CTRL1000,
  2104. CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
  2105. err = tg3_phy_toggle_auxctl_smdsp(tp, true);
  2106. if (err)
  2107. return err;
  2108. /* Block the PHY control access. */
  2109. tg3_phydsp_write(tp, 0x8005, 0x0800);
  2110. err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
  2111. if (!err)
  2112. break;
  2113. } while (--retries);
  2114. err = tg3_phy_reset_chanpat(tp);
  2115. if (err)
  2116. return err;
  2117. tg3_phydsp_write(tp, 0x8005, 0x0000);
  2118. tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
  2119. tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0000);
  2120. tg3_phy_toggle_auxctl_smdsp(tp, false);
  2121. tg3_writephy(tp, MII_CTRL1000, phy9_orig);
  2122. err = tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32);
  2123. if (err)
  2124. return err;
  2125. reg32 &= ~0x3000;
  2126. tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
  2127. return 0;
  2128. }
  2129. static void tg3_carrier_off(struct tg3 *tp)
  2130. {
  2131. netif_carrier_off(tp->dev);
  2132. tp->link_up = false;
  2133. }
  2134. static void tg3_warn_mgmt_link_flap(struct tg3 *tp)
  2135. {
  2136. if (tg3_flag(tp, ENABLE_ASF))
  2137. netdev_warn(tp->dev,
  2138. "Management side-band traffic will be interrupted during phy settings change\n");
  2139. }
  2140. /* This will reset the tigon3 PHY if there is no valid
  2141. * link unless the FORCE argument is non-zero.
  2142. */
  2143. static int tg3_phy_reset(struct tg3 *tp)
  2144. {
  2145. u32 val, cpmuctrl;
  2146. int err;
  2147. if (tg3_asic_rev(tp) == ASIC_REV_5906) {
  2148. val = tr32(GRC_MISC_CFG);
  2149. tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
  2150. udelay(40);
  2151. }
  2152. err = tg3_readphy(tp, MII_BMSR, &val);
  2153. err |= tg3_readphy(tp, MII_BMSR, &val);
  2154. if (err != 0)
  2155. return -EBUSY;
  2156. if (netif_running(tp->dev) && tp->link_up) {
  2157. netif_carrier_off(tp->dev);
  2158. tg3_link_report(tp);
  2159. }
  2160. if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
  2161. tg3_asic_rev(tp) == ASIC_REV_5704 ||
  2162. tg3_asic_rev(tp) == ASIC_REV_5705) {
  2163. err = tg3_phy_reset_5703_4_5(tp);
  2164. if (err)
  2165. return err;
  2166. goto out;
  2167. }
  2168. cpmuctrl = 0;
  2169. if (tg3_asic_rev(tp) == ASIC_REV_5784 &&
  2170. tg3_chip_rev(tp) != CHIPREV_5784_AX) {
  2171. cpmuctrl = tr32(TG3_CPMU_CTRL);
  2172. if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
  2173. tw32(TG3_CPMU_CTRL,
  2174. cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
  2175. }
  2176. err = tg3_bmcr_reset(tp);
  2177. if (err)
  2178. return err;
  2179. if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
  2180. val = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
  2181. tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, val);
  2182. tw32(TG3_CPMU_CTRL, cpmuctrl);
  2183. }
  2184. if (tg3_chip_rev(tp) == CHIPREV_5784_AX ||
  2185. tg3_chip_rev(tp) == CHIPREV_5761_AX) {
  2186. val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
  2187. if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
  2188. CPMU_LSPD_1000MB_MACCLK_12_5) {
  2189. val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
  2190. udelay(40);
  2191. tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
  2192. }
  2193. }
  2194. if (tg3_flag(tp, 5717_PLUS) &&
  2195. (tp->phy_flags & TG3_PHYFLG_MII_SERDES))
  2196. return 0;
  2197. tg3_phy_apply_otp(tp);
  2198. if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
  2199. tg3_phy_toggle_apd(tp, true);
  2200. else
  2201. tg3_phy_toggle_apd(tp, false);
  2202. out:
  2203. if ((tp->phy_flags & TG3_PHYFLG_ADC_BUG) &&
  2204. !tg3_phy_toggle_auxctl_smdsp(tp, true)) {
  2205. tg3_phydsp_write(tp, 0x201f, 0x2aaa);
  2206. tg3_phydsp_write(tp, 0x000a, 0x0323);
  2207. tg3_phy_toggle_auxctl_smdsp(tp, false);
  2208. }
  2209. if (tp->phy_flags & TG3_PHYFLG_5704_A0_BUG) {
  2210. tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
  2211. tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
  2212. }
  2213. if (tp->phy_flags & TG3_PHYFLG_BER_BUG) {
  2214. if (!tg3_phy_toggle_auxctl_smdsp(tp, true)) {
  2215. tg3_phydsp_write(tp, 0x000a, 0x310b);
  2216. tg3_phydsp_write(tp, 0x201f, 0x9506);
  2217. tg3_phydsp_write(tp, 0x401f, 0x14e2);
  2218. tg3_phy_toggle_auxctl_smdsp(tp, false);
  2219. }
  2220. } else if (tp->phy_flags & TG3_PHYFLG_JITTER_BUG) {
  2221. if (!tg3_phy_toggle_auxctl_smdsp(tp, true)) {
  2222. tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
  2223. if (tp->phy_flags & TG3_PHYFLG_ADJUST_TRIM) {
  2224. tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
  2225. tg3_writephy(tp, MII_TG3_TEST1,
  2226. MII_TG3_TEST1_TRIM_EN | 0x4);
  2227. } else
  2228. tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
  2229. tg3_phy_toggle_auxctl_smdsp(tp, false);
  2230. }
  2231. }
  2232. /* Set Extended packet length bit (bit 14) on all chips that */
  2233. /* support jumbo frames */
  2234. if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
  2235. /* Cannot do read-modify-write on 5401 */
  2236. tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
  2237. } else if (tg3_flag(tp, JUMBO_CAPABLE)) {
  2238. /* Set bit 14 with read-modify-write to preserve other bits */
  2239. err = tg3_phy_auxctl_read(tp,
  2240. MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
  2241. if (!err)
  2242. tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
  2243. val | MII_TG3_AUXCTL_ACTL_EXTPKTLEN);
  2244. }
  2245. /* Set phy register 0x10 bit 0 to high fifo elasticity to support
  2246. * jumbo frames transmission.
  2247. */
  2248. if (tg3_flag(tp, JUMBO_CAPABLE)) {
  2249. if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &val))
  2250. tg3_writephy(tp, MII_TG3_EXT_CTRL,
  2251. val | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
  2252. }
  2253. if (tg3_asic_rev(tp) == ASIC_REV_5906) {
  2254. /* adjust output voltage */
  2255. tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
  2256. }
  2257. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5762_A0)
  2258. tg3_phydsp_write(tp, 0xffb, 0x4000);
  2259. tg3_phy_toggle_automdix(tp, true);
  2260. tg3_phy_set_wirespeed(tp);
  2261. return 0;
  2262. }
  2263. #define TG3_GPIO_MSG_DRVR_PRES 0x00000001
  2264. #define TG3_GPIO_MSG_NEED_VAUX 0x00000002
  2265. #define TG3_GPIO_MSG_MASK (TG3_GPIO_MSG_DRVR_PRES | \
  2266. TG3_GPIO_MSG_NEED_VAUX)
  2267. #define TG3_GPIO_MSG_ALL_DRVR_PRES_MASK \
  2268. ((TG3_GPIO_MSG_DRVR_PRES << 0) | \
  2269. (TG3_GPIO_MSG_DRVR_PRES << 4) | \
  2270. (TG3_GPIO_MSG_DRVR_PRES << 8) | \
  2271. (TG3_GPIO_MSG_DRVR_PRES << 12))
  2272. #define TG3_GPIO_MSG_ALL_NEED_VAUX_MASK \
  2273. ((TG3_GPIO_MSG_NEED_VAUX << 0) | \
  2274. (TG3_GPIO_MSG_NEED_VAUX << 4) | \
  2275. (TG3_GPIO_MSG_NEED_VAUX << 8) | \
  2276. (TG3_GPIO_MSG_NEED_VAUX << 12))
  2277. static inline u32 tg3_set_function_status(struct tg3 *tp, u32 newstat)
  2278. {
  2279. u32 status, shift;
  2280. if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
  2281. tg3_asic_rev(tp) == ASIC_REV_5719)
  2282. status = tg3_ape_read32(tp, TG3_APE_GPIO_MSG);
  2283. else
  2284. status = tr32(TG3_CPMU_DRV_STATUS);
  2285. shift = TG3_APE_GPIO_MSG_SHIFT + 4 * tp->pci_fn;
  2286. status &= ~(TG3_GPIO_MSG_MASK << shift);
  2287. status |= (newstat << shift);
  2288. if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
  2289. tg3_asic_rev(tp) == ASIC_REV_5719)
  2290. tg3_ape_write32(tp, TG3_APE_GPIO_MSG, status);
  2291. else
  2292. tw32(TG3_CPMU_DRV_STATUS, status);
  2293. return status >> TG3_APE_GPIO_MSG_SHIFT;
  2294. }
  2295. static inline int tg3_pwrsrc_switch_to_vmain(struct tg3 *tp)
  2296. {
  2297. if (!tg3_flag(tp, IS_NIC))
  2298. return 0;
  2299. if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
  2300. tg3_asic_rev(tp) == ASIC_REV_5719 ||
  2301. tg3_asic_rev(tp) == ASIC_REV_5720) {
  2302. if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
  2303. return -EIO;
  2304. tg3_set_function_status(tp, TG3_GPIO_MSG_DRVR_PRES);
  2305. tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
  2306. TG3_GRC_LCLCTL_PWRSW_DELAY);
  2307. tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
  2308. } else {
  2309. tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
  2310. TG3_GRC_LCLCTL_PWRSW_DELAY);
  2311. }
  2312. return 0;
  2313. }
  2314. static void tg3_pwrsrc_die_with_vmain(struct tg3 *tp)
  2315. {
  2316. u32 grc_local_ctrl;
  2317. if (!tg3_flag(tp, IS_NIC) ||
  2318. tg3_asic_rev(tp) == ASIC_REV_5700 ||
  2319. tg3_asic_rev(tp) == ASIC_REV_5701)
  2320. return;
  2321. grc_local_ctrl = tp->grc_local_ctrl | GRC_LCLCTRL_GPIO_OE1;
  2322. tw32_wait_f(GRC_LOCAL_CTRL,
  2323. grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
  2324. TG3_GRC_LCLCTL_PWRSW_DELAY);
  2325. tw32_wait_f(GRC_LOCAL_CTRL,
  2326. grc_local_ctrl,
  2327. TG3_GRC_LCLCTL_PWRSW_DELAY);
  2328. tw32_wait_f(GRC_LOCAL_CTRL,
  2329. grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
  2330. TG3_GRC_LCLCTL_PWRSW_DELAY);
  2331. }
  2332. static void tg3_pwrsrc_switch_to_vaux(struct tg3 *tp)
  2333. {
  2334. if (!tg3_flag(tp, IS_NIC))
  2335. return;
  2336. if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
  2337. tg3_asic_rev(tp) == ASIC_REV_5701) {
  2338. tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
  2339. (GRC_LCLCTRL_GPIO_OE0 |
  2340. GRC_LCLCTRL_GPIO_OE1 |
  2341. GRC_LCLCTRL_GPIO_OE2 |
  2342. GRC_LCLCTRL_GPIO_OUTPUT0 |
  2343. GRC_LCLCTRL_GPIO_OUTPUT1),
  2344. TG3_GRC_LCLCTL_PWRSW_DELAY);
  2345. } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
  2346. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
  2347. /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
  2348. u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
  2349. GRC_LCLCTRL_GPIO_OE1 |
  2350. GRC_LCLCTRL_GPIO_OE2 |
  2351. GRC_LCLCTRL_GPIO_OUTPUT0 |
  2352. GRC_LCLCTRL_GPIO_OUTPUT1 |
  2353. tp->grc_local_ctrl;
  2354. tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
  2355. TG3_GRC_LCLCTL_PWRSW_DELAY);
  2356. grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
  2357. tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
  2358. TG3_GRC_LCLCTL_PWRSW_DELAY);
  2359. grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
  2360. tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
  2361. TG3_GRC_LCLCTL_PWRSW_DELAY);
  2362. } else {
  2363. u32 no_gpio2;
  2364. u32 grc_local_ctrl = 0;
  2365. /* Workaround to prevent overdrawing Amps. */
  2366. if (tg3_asic_rev(tp) == ASIC_REV_5714) {
  2367. grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
  2368. tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
  2369. grc_local_ctrl,
  2370. TG3_GRC_LCLCTL_PWRSW_DELAY);
  2371. }
  2372. /* On 5753 and variants, GPIO2 cannot be used. */
  2373. no_gpio2 = tp->nic_sram_data_cfg &
  2374. NIC_SRAM_DATA_CFG_NO_GPIO2;
  2375. grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
  2376. GRC_LCLCTRL_GPIO_OE1 |
  2377. GRC_LCLCTRL_GPIO_OE2 |
  2378. GRC_LCLCTRL_GPIO_OUTPUT1 |
  2379. GRC_LCLCTRL_GPIO_OUTPUT2;
  2380. if (no_gpio2) {
  2381. grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
  2382. GRC_LCLCTRL_GPIO_OUTPUT2);
  2383. }
  2384. tw32_wait_f(GRC_LOCAL_CTRL,
  2385. tp->grc_local_ctrl | grc_local_ctrl,
  2386. TG3_GRC_LCLCTL_PWRSW_DELAY);
  2387. grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
  2388. tw32_wait_f(GRC_LOCAL_CTRL,
  2389. tp->grc_local_ctrl | grc_local_ctrl,
  2390. TG3_GRC_LCLCTL_PWRSW_DELAY);
  2391. if (!no_gpio2) {
  2392. grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
  2393. tw32_wait_f(GRC_LOCAL_CTRL,
  2394. tp->grc_local_ctrl | grc_local_ctrl,
  2395. TG3_GRC_LCLCTL_PWRSW_DELAY);
  2396. }
  2397. }
  2398. }
  2399. static void tg3_frob_aux_power_5717(struct tg3 *tp, bool wol_enable)
  2400. {
  2401. u32 msg = 0;
  2402. /* Serialize power state transitions */
  2403. if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
  2404. return;
  2405. if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE) || wol_enable)
  2406. msg = TG3_GPIO_MSG_NEED_VAUX;
  2407. msg = tg3_set_function_status(tp, msg);
  2408. if (msg & TG3_GPIO_MSG_ALL_DRVR_PRES_MASK)
  2409. goto done;
  2410. if (msg & TG3_GPIO_MSG_ALL_NEED_VAUX_MASK)
  2411. tg3_pwrsrc_switch_to_vaux(tp);
  2412. else
  2413. tg3_pwrsrc_die_with_vmain(tp);
  2414. done:
  2415. tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
  2416. }
  2417. static void tg3_frob_aux_power(struct tg3 *tp, bool include_wol)
  2418. {
  2419. bool need_vaux = false;
  2420. /* The GPIOs do something completely different on 57765. */
  2421. if (!tg3_flag(tp, IS_NIC) || tg3_flag(tp, 57765_CLASS))
  2422. return;
  2423. if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
  2424. tg3_asic_rev(tp) == ASIC_REV_5719 ||
  2425. tg3_asic_rev(tp) == ASIC_REV_5720) {
  2426. tg3_frob_aux_power_5717(tp, include_wol ?
  2427. tg3_flag(tp, WOL_ENABLE) != 0 : 0);
  2428. return;
  2429. }
  2430. if (tp->pdev_peer && tp->pdev_peer != tp->pdev) {
  2431. struct net_device *dev_peer;
  2432. dev_peer = pci_get_drvdata(tp->pdev_peer);
  2433. /* remove_one() may have been run on the peer. */
  2434. if (dev_peer) {
  2435. struct tg3 *tp_peer = netdev_priv(dev_peer);
  2436. if (tg3_flag(tp_peer, INIT_COMPLETE))
  2437. return;
  2438. if ((include_wol && tg3_flag(tp_peer, WOL_ENABLE)) ||
  2439. tg3_flag(tp_peer, ENABLE_ASF))
  2440. need_vaux = true;
  2441. }
  2442. }
  2443. if ((include_wol && tg3_flag(tp, WOL_ENABLE)) ||
  2444. tg3_flag(tp, ENABLE_ASF))
  2445. need_vaux = true;
  2446. if (need_vaux)
  2447. tg3_pwrsrc_switch_to_vaux(tp);
  2448. else
  2449. tg3_pwrsrc_die_with_vmain(tp);
  2450. }
  2451. static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
  2452. {
  2453. if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
  2454. return 1;
  2455. else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
  2456. if (speed != SPEED_10)
  2457. return 1;
  2458. } else if (speed == SPEED_10)
  2459. return 1;
  2460. return 0;
  2461. }
  2462. static bool tg3_phy_power_bug(struct tg3 *tp)
  2463. {
  2464. switch (tg3_asic_rev(tp)) {
  2465. case ASIC_REV_5700:
  2466. case ASIC_REV_5704:
  2467. return true;
  2468. case ASIC_REV_5780:
  2469. if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
  2470. return true;
  2471. return false;
  2472. case ASIC_REV_5717:
  2473. if (!tp->pci_fn)
  2474. return true;
  2475. return false;
  2476. case ASIC_REV_5719:
  2477. case ASIC_REV_5720:
  2478. if ((tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
  2479. !tp->pci_fn)
  2480. return true;
  2481. return false;
  2482. }
  2483. return false;
  2484. }
  2485. static bool tg3_phy_led_bug(struct tg3 *tp)
  2486. {
  2487. switch (tg3_asic_rev(tp)) {
  2488. case ASIC_REV_5719:
  2489. case ASIC_REV_5720:
  2490. if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
  2491. !tp->pci_fn)
  2492. return true;
  2493. return false;
  2494. }
  2495. return false;
  2496. }
  2497. static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
  2498. {
  2499. u32 val;
  2500. if (tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN)
  2501. return;
  2502. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
  2503. if (tg3_asic_rev(tp) == ASIC_REV_5704) {
  2504. u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
  2505. u32 serdes_cfg = tr32(MAC_SERDES_CFG);
  2506. sg_dig_ctrl |=
  2507. SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
  2508. tw32(SG_DIG_CTRL, sg_dig_ctrl);
  2509. tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
  2510. }
  2511. return;
  2512. }
  2513. if (tg3_asic_rev(tp) == ASIC_REV_5906) {
  2514. tg3_bmcr_reset(tp);
  2515. val = tr32(GRC_MISC_CFG);
  2516. tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
  2517. udelay(40);
  2518. return;
  2519. } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
  2520. u32 phytest;
  2521. if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
  2522. u32 phy;
  2523. tg3_writephy(tp, MII_ADVERTISE, 0);
  2524. tg3_writephy(tp, MII_BMCR,
  2525. BMCR_ANENABLE | BMCR_ANRESTART);
  2526. tg3_writephy(tp, MII_TG3_FET_TEST,
  2527. phytest | MII_TG3_FET_SHADOW_EN);
  2528. if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
  2529. phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
  2530. tg3_writephy(tp,
  2531. MII_TG3_FET_SHDW_AUXMODE4,
  2532. phy);
  2533. }
  2534. tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
  2535. }
  2536. return;
  2537. } else if (do_low_power) {
  2538. if (!tg3_phy_led_bug(tp))
  2539. tg3_writephy(tp, MII_TG3_EXT_CTRL,
  2540. MII_TG3_EXT_CTRL_FORCE_LED_OFF);
  2541. val = MII_TG3_AUXCTL_PCTL_100TX_LPWR |
  2542. MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
  2543. MII_TG3_AUXCTL_PCTL_VREG_11V;
  2544. tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, val);
  2545. }
  2546. /* The PHY should not be powered down on some chips because
  2547. * of bugs.
  2548. */
  2549. if (tg3_phy_power_bug(tp))
  2550. return;
  2551. if (tg3_chip_rev(tp) == CHIPREV_5784_AX ||
  2552. tg3_chip_rev(tp) == CHIPREV_5761_AX) {
  2553. val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
  2554. val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
  2555. val |= CPMU_LSPD_1000MB_MACCLK_12_5;
  2556. tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
  2557. }
  2558. tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
  2559. }
  2560. /* tp->lock is held. */
  2561. static int tg3_nvram_lock(struct tg3 *tp)
  2562. {
  2563. if (tg3_flag(tp, NVRAM)) {
  2564. int i;
  2565. if (tp->nvram_lock_cnt == 0) {
  2566. tw32(NVRAM_SWARB, SWARB_REQ_SET1);
  2567. for (i = 0; i < 8000; i++) {
  2568. if (tr32(NVRAM_SWARB) & SWARB_GNT1)
  2569. break;
  2570. udelay(20);
  2571. }
  2572. if (i == 8000) {
  2573. tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
  2574. return -ENODEV;
  2575. }
  2576. }
  2577. tp->nvram_lock_cnt++;
  2578. }
  2579. return 0;
  2580. }
  2581. /* tp->lock is held. */
  2582. static void tg3_nvram_unlock(struct tg3 *tp)
  2583. {
  2584. if (tg3_flag(tp, NVRAM)) {
  2585. if (tp->nvram_lock_cnt > 0)
  2586. tp->nvram_lock_cnt--;
  2587. if (tp->nvram_lock_cnt == 0)
  2588. tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
  2589. }
  2590. }
  2591. /* tp->lock is held. */
  2592. static void tg3_enable_nvram_access(struct tg3 *tp)
  2593. {
  2594. if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
  2595. u32 nvaccess = tr32(NVRAM_ACCESS);
  2596. tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
  2597. }
  2598. }
  2599. /* tp->lock is held. */
  2600. static void tg3_disable_nvram_access(struct tg3 *tp)
  2601. {
  2602. if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
  2603. u32 nvaccess = tr32(NVRAM_ACCESS);
  2604. tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
  2605. }
  2606. }
  2607. static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
  2608. u32 offset, u32 *val)
  2609. {
  2610. u32 tmp;
  2611. int i;
  2612. if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
  2613. return -EINVAL;
  2614. tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
  2615. EEPROM_ADDR_DEVID_MASK |
  2616. EEPROM_ADDR_READ);
  2617. tw32(GRC_EEPROM_ADDR,
  2618. tmp |
  2619. (0 << EEPROM_ADDR_DEVID_SHIFT) |
  2620. ((offset << EEPROM_ADDR_ADDR_SHIFT) &
  2621. EEPROM_ADDR_ADDR_MASK) |
  2622. EEPROM_ADDR_READ | EEPROM_ADDR_START);
  2623. for (i = 0; i < 1000; i++) {
  2624. tmp = tr32(GRC_EEPROM_ADDR);
  2625. if (tmp & EEPROM_ADDR_COMPLETE)
  2626. break;
  2627. msleep(1);
  2628. }
  2629. if (!(tmp & EEPROM_ADDR_COMPLETE))
  2630. return -EBUSY;
  2631. tmp = tr32(GRC_EEPROM_DATA);
  2632. /*
  2633. * The data will always be opposite the native endian
  2634. * format. Perform a blind byteswap to compensate.
  2635. */
  2636. *val = swab32(tmp);
  2637. return 0;
  2638. }
  2639. #define NVRAM_CMD_TIMEOUT 5000
  2640. static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
  2641. {
  2642. int i;
  2643. tw32(NVRAM_CMD, nvram_cmd);
  2644. for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
  2645. usleep_range(10, 40);
  2646. if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
  2647. udelay(10);
  2648. break;
  2649. }
  2650. }
  2651. if (i == NVRAM_CMD_TIMEOUT)
  2652. return -EBUSY;
  2653. return 0;
  2654. }
  2655. static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
  2656. {
  2657. if (tg3_flag(tp, NVRAM) &&
  2658. tg3_flag(tp, NVRAM_BUFFERED) &&
  2659. tg3_flag(tp, FLASH) &&
  2660. !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
  2661. (tp->nvram_jedecnum == JEDEC_ATMEL))
  2662. addr = ((addr / tp->nvram_pagesize) <<
  2663. ATMEL_AT45DB0X1B_PAGE_POS) +
  2664. (addr % tp->nvram_pagesize);
  2665. return addr;
  2666. }
  2667. static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
  2668. {
  2669. if (tg3_flag(tp, NVRAM) &&
  2670. tg3_flag(tp, NVRAM_BUFFERED) &&
  2671. tg3_flag(tp, FLASH) &&
  2672. !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
  2673. (tp->nvram_jedecnum == JEDEC_ATMEL))
  2674. addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
  2675. tp->nvram_pagesize) +
  2676. (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
  2677. return addr;
  2678. }
  2679. /* NOTE: Data read in from NVRAM is byteswapped according to
  2680. * the byteswapping settings for all other register accesses.
  2681. * tg3 devices are BE devices, so on a BE machine, the data
  2682. * returned will be exactly as it is seen in NVRAM. On a LE
  2683. * machine, the 32-bit value will be byteswapped.
  2684. */
  2685. static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
  2686. {
  2687. int ret;
  2688. if (!tg3_flag(tp, NVRAM))
  2689. return tg3_nvram_read_using_eeprom(tp, offset, val);
  2690. offset = tg3_nvram_phys_addr(tp, offset);
  2691. if (offset > NVRAM_ADDR_MSK)
  2692. return -EINVAL;
  2693. ret = tg3_nvram_lock(tp);
  2694. if (ret)
  2695. return ret;
  2696. tg3_enable_nvram_access(tp);
  2697. tw32(NVRAM_ADDR, offset);
  2698. ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
  2699. NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
  2700. if (ret == 0)
  2701. *val = tr32(NVRAM_RDDATA);
  2702. tg3_disable_nvram_access(tp);
  2703. tg3_nvram_unlock(tp);
  2704. return ret;
  2705. }
  2706. /* Ensures NVRAM data is in bytestream format. */
  2707. static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
  2708. {
  2709. u32 v;
  2710. int res = tg3_nvram_read(tp, offset, &v);
  2711. if (!res)
  2712. *val = cpu_to_be32(v);
  2713. return res;
  2714. }
  2715. static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
  2716. u32 offset, u32 len, u8 *buf)
  2717. {
  2718. int i, j, rc = 0;
  2719. u32 val;
  2720. for (i = 0; i < len; i += 4) {
  2721. u32 addr;
  2722. __be32 data;
  2723. addr = offset + i;
  2724. memcpy(&data, buf + i, 4);
  2725. /*
  2726. * The SEEPROM interface expects the data to always be opposite
  2727. * the native endian format. We accomplish this by reversing
  2728. * all the operations that would have been performed on the
  2729. * data from a call to tg3_nvram_read_be32().
  2730. */
  2731. tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
  2732. val = tr32(GRC_EEPROM_ADDR);
  2733. tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
  2734. val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
  2735. EEPROM_ADDR_READ);
  2736. tw32(GRC_EEPROM_ADDR, val |
  2737. (0 << EEPROM_ADDR_DEVID_SHIFT) |
  2738. (addr & EEPROM_ADDR_ADDR_MASK) |
  2739. EEPROM_ADDR_START |
  2740. EEPROM_ADDR_WRITE);
  2741. for (j = 0; j < 1000; j++) {
  2742. val = tr32(GRC_EEPROM_ADDR);
  2743. if (val & EEPROM_ADDR_COMPLETE)
  2744. break;
  2745. msleep(1);
  2746. }
  2747. if (!(val & EEPROM_ADDR_COMPLETE)) {
  2748. rc = -EBUSY;
  2749. break;
  2750. }
  2751. }
  2752. return rc;
  2753. }
  2754. /* offset and length are dword aligned */
  2755. static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
  2756. u8 *buf)
  2757. {
  2758. int ret = 0;
  2759. u32 pagesize = tp->nvram_pagesize;
  2760. u32 pagemask = pagesize - 1;
  2761. u32 nvram_cmd;
  2762. u8 *tmp;
  2763. tmp = kmalloc(pagesize, GFP_KERNEL);
  2764. if (tmp == NULL)
  2765. return -ENOMEM;
  2766. while (len) {
  2767. int j;
  2768. u32 phy_addr, page_off, size;
  2769. phy_addr = offset & ~pagemask;
  2770. for (j = 0; j < pagesize; j += 4) {
  2771. ret = tg3_nvram_read_be32(tp, phy_addr + j,
  2772. (__be32 *) (tmp + j));
  2773. if (ret)
  2774. break;
  2775. }
  2776. if (ret)
  2777. break;
  2778. page_off = offset & pagemask;
  2779. size = pagesize;
  2780. if (len < size)
  2781. size = len;
  2782. len -= size;
  2783. memcpy(tmp + page_off, buf, size);
  2784. offset = offset + (pagesize - page_off);
  2785. tg3_enable_nvram_access(tp);
  2786. /*
  2787. * Before we can erase the flash page, we need
  2788. * to issue a special "write enable" command.
  2789. */
  2790. nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
  2791. if (tg3_nvram_exec_cmd(tp, nvram_cmd))
  2792. break;
  2793. /* Erase the target page */
  2794. tw32(NVRAM_ADDR, phy_addr);
  2795. nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
  2796. NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
  2797. if (tg3_nvram_exec_cmd(tp, nvram_cmd))
  2798. break;
  2799. /* Issue another write enable to start the write. */
  2800. nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
  2801. if (tg3_nvram_exec_cmd(tp, nvram_cmd))
  2802. break;
  2803. for (j = 0; j < pagesize; j += 4) {
  2804. __be32 data;
  2805. data = *((__be32 *) (tmp + j));
  2806. tw32(NVRAM_WRDATA, be32_to_cpu(data));
  2807. tw32(NVRAM_ADDR, phy_addr + j);
  2808. nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
  2809. NVRAM_CMD_WR;
  2810. if (j == 0)
  2811. nvram_cmd |= NVRAM_CMD_FIRST;
  2812. else if (j == (pagesize - 4))
  2813. nvram_cmd |= NVRAM_CMD_LAST;
  2814. ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
  2815. if (ret)
  2816. break;
  2817. }
  2818. if (ret)
  2819. break;
  2820. }
  2821. nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
  2822. tg3_nvram_exec_cmd(tp, nvram_cmd);
  2823. kfree(tmp);
  2824. return ret;
  2825. }
  2826. /* offset and length are dword aligned */
  2827. static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
  2828. u8 *buf)
  2829. {
  2830. int i, ret = 0;
  2831. for (i = 0; i < len; i += 4, offset += 4) {
  2832. u32 page_off, phy_addr, nvram_cmd;
  2833. __be32 data;
  2834. memcpy(&data, buf + i, 4);
  2835. tw32(NVRAM_WRDATA, be32_to_cpu(data));
  2836. page_off = offset % tp->nvram_pagesize;
  2837. phy_addr = tg3_nvram_phys_addr(tp, offset);
  2838. nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
  2839. if (page_off == 0 || i == 0)
  2840. nvram_cmd |= NVRAM_CMD_FIRST;
  2841. if (page_off == (tp->nvram_pagesize - 4))
  2842. nvram_cmd |= NVRAM_CMD_LAST;
  2843. if (i == (len - 4))
  2844. nvram_cmd |= NVRAM_CMD_LAST;
  2845. if ((nvram_cmd & NVRAM_CMD_FIRST) ||
  2846. !tg3_flag(tp, FLASH) ||
  2847. !tg3_flag(tp, 57765_PLUS))
  2848. tw32(NVRAM_ADDR, phy_addr);
  2849. if (tg3_asic_rev(tp) != ASIC_REV_5752 &&
  2850. !tg3_flag(tp, 5755_PLUS) &&
  2851. (tp->nvram_jedecnum == JEDEC_ST) &&
  2852. (nvram_cmd & NVRAM_CMD_FIRST)) {
  2853. u32 cmd;
  2854. cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
  2855. ret = tg3_nvram_exec_cmd(tp, cmd);
  2856. if (ret)
  2857. break;
  2858. }
  2859. if (!tg3_flag(tp, FLASH)) {
  2860. /* We always do complete word writes to eeprom. */
  2861. nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
  2862. }
  2863. ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
  2864. if (ret)
  2865. break;
  2866. }
  2867. return ret;
  2868. }
  2869. /* offset and length are dword aligned */
  2870. static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
  2871. {
  2872. int ret;
  2873. if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
  2874. tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
  2875. ~GRC_LCLCTRL_GPIO_OUTPUT1);
  2876. udelay(40);
  2877. }
  2878. if (!tg3_flag(tp, NVRAM)) {
  2879. ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
  2880. } else {
  2881. u32 grc_mode;
  2882. ret = tg3_nvram_lock(tp);
  2883. if (ret)
  2884. return ret;
  2885. tg3_enable_nvram_access(tp);
  2886. if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM))
  2887. tw32(NVRAM_WRITE1, 0x406);
  2888. grc_mode = tr32(GRC_MODE);
  2889. tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
  2890. if (tg3_flag(tp, NVRAM_BUFFERED) || !tg3_flag(tp, FLASH)) {
  2891. ret = tg3_nvram_write_block_buffered(tp, offset, len,
  2892. buf);
  2893. } else {
  2894. ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
  2895. buf);
  2896. }
  2897. grc_mode = tr32(GRC_MODE);
  2898. tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
  2899. tg3_disable_nvram_access(tp);
  2900. tg3_nvram_unlock(tp);
  2901. }
  2902. if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
  2903. tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
  2904. udelay(40);
  2905. }
  2906. return ret;
  2907. }
  2908. #define RX_CPU_SCRATCH_BASE 0x30000
  2909. #define RX_CPU_SCRATCH_SIZE 0x04000
  2910. #define TX_CPU_SCRATCH_BASE 0x34000
  2911. #define TX_CPU_SCRATCH_SIZE 0x04000
  2912. /* tp->lock is held. */
  2913. static int tg3_pause_cpu(struct tg3 *tp, u32 cpu_base)
  2914. {
  2915. int i;
  2916. const int iters = 10000;
  2917. for (i = 0; i < iters; i++) {
  2918. tw32(cpu_base + CPU_STATE, 0xffffffff);
  2919. tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
  2920. if (tr32(cpu_base + CPU_MODE) & CPU_MODE_HALT)
  2921. break;
  2922. if (pci_channel_offline(tp->pdev))
  2923. return -EBUSY;
  2924. }
  2925. return (i == iters) ? -EBUSY : 0;
  2926. }
  2927. /* tp->lock is held. */
  2928. static int tg3_rxcpu_pause(struct tg3 *tp)
  2929. {
  2930. int rc = tg3_pause_cpu(tp, RX_CPU_BASE);
  2931. tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
  2932. tw32_f(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
  2933. udelay(10);
  2934. return rc;
  2935. }
  2936. /* tp->lock is held. */
  2937. static int tg3_txcpu_pause(struct tg3 *tp)
  2938. {
  2939. return tg3_pause_cpu(tp, TX_CPU_BASE);
  2940. }
  2941. /* tp->lock is held. */
  2942. static void tg3_resume_cpu(struct tg3 *tp, u32 cpu_base)
  2943. {
  2944. tw32(cpu_base + CPU_STATE, 0xffffffff);
  2945. tw32_f(cpu_base + CPU_MODE, 0x00000000);
  2946. }
  2947. /* tp->lock is held. */
  2948. static void tg3_rxcpu_resume(struct tg3 *tp)
  2949. {
  2950. tg3_resume_cpu(tp, RX_CPU_BASE);
  2951. }
  2952. /* tp->lock is held. */
  2953. static int tg3_halt_cpu(struct tg3 *tp, u32 cpu_base)
  2954. {
  2955. int rc;
  2956. BUG_ON(cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS));
  2957. if (tg3_asic_rev(tp) == ASIC_REV_5906) {
  2958. u32 val = tr32(GRC_VCPU_EXT_CTRL);
  2959. tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
  2960. return 0;
  2961. }
  2962. if (cpu_base == RX_CPU_BASE) {
  2963. rc = tg3_rxcpu_pause(tp);
  2964. } else {
  2965. /*
  2966. * There is only an Rx CPU for the 5750 derivative in the
  2967. * BCM4785.
  2968. */
  2969. if (tg3_flag(tp, IS_SSB_CORE))
  2970. return 0;
  2971. rc = tg3_txcpu_pause(tp);
  2972. }
  2973. if (rc) {
  2974. netdev_err(tp->dev, "%s timed out, %s CPU\n",
  2975. __func__, cpu_base == RX_CPU_BASE ? "RX" : "TX");
  2976. return -ENODEV;
  2977. }
  2978. /* Clear firmware's nvram arbitration. */
  2979. if (tg3_flag(tp, NVRAM))
  2980. tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
  2981. return 0;
  2982. }
  2983. static int tg3_fw_data_len(struct tg3 *tp,
  2984. const struct tg3_firmware_hdr *fw_hdr)
  2985. {
  2986. int fw_len;
  2987. /* Non fragmented firmware have one firmware header followed by a
  2988. * contiguous chunk of data to be written. The length field in that
  2989. * header is not the length of data to be written but the complete
  2990. * length of the bss. The data length is determined based on
  2991. * tp->fw->size minus headers.
  2992. *
  2993. * Fragmented firmware have a main header followed by multiple
  2994. * fragments. Each fragment is identical to non fragmented firmware
  2995. * with a firmware header followed by a contiguous chunk of data. In
  2996. * the main header, the length field is unused and set to 0xffffffff.
  2997. * In each fragment header the length is the entire size of that
  2998. * fragment i.e. fragment data + header length. Data length is
  2999. * therefore length field in the header minus TG3_FW_HDR_LEN.
  3000. */
  3001. if (tp->fw_len == 0xffffffff)
  3002. fw_len = be32_to_cpu(fw_hdr->len);
  3003. else
  3004. fw_len = tp->fw->size;
  3005. return (fw_len - TG3_FW_HDR_LEN) / sizeof(u32);
  3006. }
  3007. /* tp->lock is held. */
  3008. static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base,
  3009. u32 cpu_scratch_base, int cpu_scratch_size,
  3010. const struct tg3_firmware_hdr *fw_hdr)
  3011. {
  3012. int err, i;
  3013. void (*write_op)(struct tg3 *, u32, u32);
  3014. int total_len = tp->fw->size;
  3015. if (cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS)) {
  3016. netdev_err(tp->dev,
  3017. "%s: Trying to load TX cpu firmware which is 5705\n",
  3018. __func__);
  3019. return -EINVAL;
  3020. }
  3021. if (tg3_flag(tp, 5705_PLUS) && tg3_asic_rev(tp) != ASIC_REV_57766)
  3022. write_op = tg3_write_mem;
  3023. else
  3024. write_op = tg3_write_indirect_reg32;
  3025. if (tg3_asic_rev(tp) != ASIC_REV_57766) {
  3026. /* It is possible that bootcode is still loading at this point.
  3027. * Get the nvram lock first before halting the cpu.
  3028. */
  3029. int lock_err = tg3_nvram_lock(tp);
  3030. err = tg3_halt_cpu(tp, cpu_base);
  3031. if (!lock_err)
  3032. tg3_nvram_unlock(tp);
  3033. if (err)
  3034. goto out;
  3035. for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
  3036. write_op(tp, cpu_scratch_base + i, 0);
  3037. tw32(cpu_base + CPU_STATE, 0xffffffff);
  3038. tw32(cpu_base + CPU_MODE,
  3039. tr32(cpu_base + CPU_MODE) | CPU_MODE_HALT);
  3040. } else {
  3041. /* Subtract additional main header for fragmented firmware and
  3042. * advance to the first fragment
  3043. */
  3044. total_len -= TG3_FW_HDR_LEN;
  3045. fw_hdr++;
  3046. }
  3047. do {
  3048. u32 *fw_data = (u32 *)(fw_hdr + 1);
  3049. for (i = 0; i < tg3_fw_data_len(tp, fw_hdr); i++)
  3050. write_op(tp, cpu_scratch_base +
  3051. (be32_to_cpu(fw_hdr->base_addr) & 0xffff) +
  3052. (i * sizeof(u32)),
  3053. be32_to_cpu(fw_data[i]));
  3054. total_len -= be32_to_cpu(fw_hdr->len);
  3055. /* Advance to next fragment */
  3056. fw_hdr = (struct tg3_firmware_hdr *)
  3057. ((void *)fw_hdr + be32_to_cpu(fw_hdr->len));
  3058. } while (total_len > 0);
  3059. err = 0;
  3060. out:
  3061. return err;
  3062. }
  3063. /* tp->lock is held. */
  3064. static int tg3_pause_cpu_and_set_pc(struct tg3 *tp, u32 cpu_base, u32 pc)
  3065. {
  3066. int i;
  3067. const int iters = 5;
  3068. tw32(cpu_base + CPU_STATE, 0xffffffff);
  3069. tw32_f(cpu_base + CPU_PC, pc);
  3070. for (i = 0; i < iters; i++) {
  3071. if (tr32(cpu_base + CPU_PC) == pc)
  3072. break;
  3073. tw32(cpu_base + CPU_STATE, 0xffffffff);
  3074. tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
  3075. tw32_f(cpu_base + CPU_PC, pc);
  3076. udelay(1000);
  3077. }
  3078. return (i == iters) ? -EBUSY : 0;
  3079. }
  3080. /* tp->lock is held. */
  3081. static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
  3082. {
  3083. const struct tg3_firmware_hdr *fw_hdr;
  3084. int err;
  3085. fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
  3086. /* Firmware blob starts with version numbers, followed by
  3087. start address and length. We are setting complete length.
  3088. length = end_address_of_bss - start_address_of_text.
  3089. Remainder is the blob to be loaded contiguously
  3090. from start address. */
  3091. err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
  3092. RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
  3093. fw_hdr);
  3094. if (err)
  3095. return err;
  3096. err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
  3097. TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
  3098. fw_hdr);
  3099. if (err)
  3100. return err;
  3101. /* Now startup only the RX cpu. */
  3102. err = tg3_pause_cpu_and_set_pc(tp, RX_CPU_BASE,
  3103. be32_to_cpu(fw_hdr->base_addr));
  3104. if (err) {
  3105. netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
  3106. "should be %08x\n", __func__,
  3107. tr32(RX_CPU_BASE + CPU_PC),
  3108. be32_to_cpu(fw_hdr->base_addr));
  3109. return -ENODEV;
  3110. }
  3111. tg3_rxcpu_resume(tp);
  3112. return 0;
  3113. }
  3114. static int tg3_validate_rxcpu_state(struct tg3 *tp)
  3115. {
  3116. const int iters = 1000;
  3117. int i;
  3118. u32 val;
  3119. /* Wait for boot code to complete initialization and enter service
  3120. * loop. It is then safe to download service patches
  3121. */
  3122. for (i = 0; i < iters; i++) {
  3123. if (tr32(RX_CPU_HWBKPT) == TG3_SBROM_IN_SERVICE_LOOP)
  3124. break;
  3125. udelay(10);
  3126. }
  3127. if (i == iters) {
  3128. netdev_err(tp->dev, "Boot code not ready for service patches\n");
  3129. return -EBUSY;
  3130. }
  3131. val = tg3_read_indirect_reg32(tp, TG3_57766_FW_HANDSHAKE);
  3132. if (val & 0xff) {
  3133. netdev_warn(tp->dev,
  3134. "Other patches exist. Not downloading EEE patch\n");
  3135. return -EEXIST;
  3136. }
  3137. return 0;
  3138. }
  3139. /* tp->lock is held. */
  3140. static void tg3_load_57766_firmware(struct tg3 *tp)
  3141. {
  3142. struct tg3_firmware_hdr *fw_hdr;
  3143. if (!tg3_flag(tp, NO_NVRAM))
  3144. return;
  3145. if (tg3_validate_rxcpu_state(tp))
  3146. return;
  3147. if (!tp->fw)
  3148. return;
  3149. /* This firmware blob has a different format than older firmware
  3150. * releases as given below. The main difference is we have fragmented
  3151. * data to be written to non-contiguous locations.
  3152. *
  3153. * In the beginning we have a firmware header identical to other
  3154. * firmware which consists of version, base addr and length. The length
  3155. * here is unused and set to 0xffffffff.
  3156. *
  3157. * This is followed by a series of firmware fragments which are
  3158. * individually identical to previous firmware. i.e. they have the
  3159. * firmware header and followed by data for that fragment. The version
  3160. * field of the individual fragment header is unused.
  3161. */
  3162. fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
  3163. if (be32_to_cpu(fw_hdr->base_addr) != TG3_57766_FW_BASE_ADDR)
  3164. return;
  3165. if (tg3_rxcpu_pause(tp))
  3166. return;
  3167. /* tg3_load_firmware_cpu() will always succeed for the 57766 */
  3168. tg3_load_firmware_cpu(tp, 0, TG3_57766_FW_BASE_ADDR, 0, fw_hdr);
  3169. tg3_rxcpu_resume(tp);
  3170. }
  3171. /* tp->lock is held. */
  3172. static int tg3_load_tso_firmware(struct tg3 *tp)
  3173. {
  3174. const struct tg3_firmware_hdr *fw_hdr;
  3175. unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
  3176. int err;
  3177. if (!tg3_flag(tp, FW_TSO))
  3178. return 0;
  3179. fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
  3180. /* Firmware blob starts with version numbers, followed by
  3181. start address and length. We are setting complete length.
  3182. length = end_address_of_bss - start_address_of_text.
  3183. Remainder is the blob to be loaded contiguously
  3184. from start address. */
  3185. cpu_scratch_size = tp->fw_len;
  3186. if (tg3_asic_rev(tp) == ASIC_REV_5705) {
  3187. cpu_base = RX_CPU_BASE;
  3188. cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
  3189. } else {
  3190. cpu_base = TX_CPU_BASE;
  3191. cpu_scratch_base = TX_CPU_SCRATCH_BASE;
  3192. cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
  3193. }
  3194. err = tg3_load_firmware_cpu(tp, cpu_base,
  3195. cpu_scratch_base, cpu_scratch_size,
  3196. fw_hdr);
  3197. if (err)
  3198. return err;
  3199. /* Now startup the cpu. */
  3200. err = tg3_pause_cpu_and_set_pc(tp, cpu_base,
  3201. be32_to_cpu(fw_hdr->base_addr));
  3202. if (err) {
  3203. netdev_err(tp->dev,
  3204. "%s fails to set CPU PC, is %08x should be %08x\n",
  3205. __func__, tr32(cpu_base + CPU_PC),
  3206. be32_to_cpu(fw_hdr->base_addr));
  3207. return -ENODEV;
  3208. }
  3209. tg3_resume_cpu(tp, cpu_base);
  3210. return 0;
  3211. }
  3212. /* tp->lock is held. */
  3213. static void __tg3_set_one_mac_addr(struct tg3 *tp, u8 *mac_addr, int index)
  3214. {
  3215. u32 addr_high, addr_low;
  3216. addr_high = ((mac_addr[0] << 8) | mac_addr[1]);
  3217. addr_low = ((mac_addr[2] << 24) | (mac_addr[3] << 16) |
  3218. (mac_addr[4] << 8) | mac_addr[5]);
  3219. if (index < 4) {
  3220. tw32(MAC_ADDR_0_HIGH + (index * 8), addr_high);
  3221. tw32(MAC_ADDR_0_LOW + (index * 8), addr_low);
  3222. } else {
  3223. index -= 4;
  3224. tw32(MAC_EXTADDR_0_HIGH + (index * 8), addr_high);
  3225. tw32(MAC_EXTADDR_0_LOW + (index * 8), addr_low);
  3226. }
  3227. }
  3228. /* tp->lock is held. */
  3229. static void __tg3_set_mac_addr(struct tg3 *tp, bool skip_mac_1)
  3230. {
  3231. u32 addr_high;
  3232. int i;
  3233. for (i = 0; i < 4; i++) {
  3234. if (i == 1 && skip_mac_1)
  3235. continue;
  3236. __tg3_set_one_mac_addr(tp, tp->dev->dev_addr, i);
  3237. }
  3238. if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
  3239. tg3_asic_rev(tp) == ASIC_REV_5704) {
  3240. for (i = 4; i < 16; i++)
  3241. __tg3_set_one_mac_addr(tp, tp->dev->dev_addr, i);
  3242. }
  3243. addr_high = (tp->dev->dev_addr[0] +
  3244. tp->dev->dev_addr[1] +
  3245. tp->dev->dev_addr[2] +
  3246. tp->dev->dev_addr[3] +
  3247. tp->dev->dev_addr[4] +
  3248. tp->dev->dev_addr[5]) &
  3249. TX_BACKOFF_SEED_MASK;
  3250. tw32(MAC_TX_BACKOFF_SEED, addr_high);
  3251. }
  3252. static void tg3_enable_register_access(struct tg3 *tp)
  3253. {
  3254. /*
  3255. * Make sure register accesses (indirect or otherwise) will function
  3256. * correctly.
  3257. */
  3258. pci_write_config_dword(tp->pdev,
  3259. TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
  3260. }
  3261. static int tg3_power_up(struct tg3 *tp)
  3262. {
  3263. int err;
  3264. tg3_enable_register_access(tp);
  3265. err = pci_set_power_state(tp->pdev, PCI_D0);
  3266. if (!err) {
  3267. /* Switch out of Vaux if it is a NIC */
  3268. tg3_pwrsrc_switch_to_vmain(tp);
  3269. } else {
  3270. netdev_err(tp->dev, "Transition to D0 failed\n");
  3271. }
  3272. return err;
  3273. }
  3274. static int tg3_setup_phy(struct tg3 *, bool);
  3275. static int tg3_power_down_prepare(struct tg3 *tp)
  3276. {
  3277. u32 misc_host_ctrl;
  3278. bool device_should_wake, do_low_power;
  3279. tg3_enable_register_access(tp);
  3280. /* Restore the CLKREQ setting. */
  3281. if (tg3_flag(tp, CLKREQ_BUG))
  3282. pcie_capability_set_word(tp->pdev, PCI_EXP_LNKCTL,
  3283. PCI_EXP_LNKCTL_CLKREQ_EN);
  3284. misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
  3285. tw32(TG3PCI_MISC_HOST_CTRL,
  3286. misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
  3287. device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
  3288. tg3_flag(tp, WOL_ENABLE);
  3289. if (tg3_flag(tp, USE_PHYLIB)) {
  3290. do_low_power = false;
  3291. if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
  3292. !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
  3293. struct phy_device *phydev;
  3294. u32 phyid, advertising;
  3295. phydev = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr);
  3296. tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
  3297. tp->link_config.speed = phydev->speed;
  3298. tp->link_config.duplex = phydev->duplex;
  3299. tp->link_config.autoneg = phydev->autoneg;
  3300. tp->link_config.advertising = phydev->advertising;
  3301. advertising = ADVERTISED_TP |
  3302. ADVERTISED_Pause |
  3303. ADVERTISED_Autoneg |
  3304. ADVERTISED_10baseT_Half;
  3305. if (tg3_flag(tp, ENABLE_ASF) || device_should_wake) {
  3306. if (tg3_flag(tp, WOL_SPEED_100MB))
  3307. advertising |=
  3308. ADVERTISED_100baseT_Half |
  3309. ADVERTISED_100baseT_Full |
  3310. ADVERTISED_10baseT_Full;
  3311. else
  3312. advertising |= ADVERTISED_10baseT_Full;
  3313. }
  3314. phydev->advertising = advertising;
  3315. phy_start_aneg(phydev);
  3316. phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
  3317. if (phyid != PHY_ID_BCMAC131) {
  3318. phyid &= PHY_BCM_OUI_MASK;
  3319. if (phyid == PHY_BCM_OUI_1 ||
  3320. phyid == PHY_BCM_OUI_2 ||
  3321. phyid == PHY_BCM_OUI_3)
  3322. do_low_power = true;
  3323. }
  3324. }
  3325. } else {
  3326. do_low_power = true;
  3327. if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER))
  3328. tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
  3329. if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
  3330. tg3_setup_phy(tp, false);
  3331. }
  3332. if (tg3_asic_rev(tp) == ASIC_REV_5906) {
  3333. u32 val;
  3334. val = tr32(GRC_VCPU_EXT_CTRL);
  3335. tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
  3336. } else if (!tg3_flag(tp, ENABLE_ASF)) {
  3337. int i;
  3338. u32 val;
  3339. for (i = 0; i < 200; i++) {
  3340. tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
  3341. if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
  3342. break;
  3343. msleep(1);
  3344. }
  3345. }
  3346. if (tg3_flag(tp, WOL_CAP))
  3347. tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
  3348. WOL_DRV_STATE_SHUTDOWN |
  3349. WOL_DRV_WOL |
  3350. WOL_SET_MAGIC_PKT);
  3351. if (device_should_wake) {
  3352. u32 mac_mode;
  3353. if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
  3354. if (do_low_power &&
  3355. !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
  3356. tg3_phy_auxctl_write(tp,
  3357. MII_TG3_AUXCTL_SHDWSEL_PWRCTL,
  3358. MII_TG3_AUXCTL_PCTL_WOL_EN |
  3359. MII_TG3_AUXCTL_PCTL_100TX_LPWR |
  3360. MII_TG3_AUXCTL_PCTL_CL_AB_TXDAC);
  3361. udelay(40);
  3362. }
  3363. if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
  3364. mac_mode = MAC_MODE_PORT_MODE_GMII;
  3365. else if (tp->phy_flags &
  3366. TG3_PHYFLG_KEEP_LINK_ON_PWRDN) {
  3367. if (tp->link_config.active_speed == SPEED_1000)
  3368. mac_mode = MAC_MODE_PORT_MODE_GMII;
  3369. else
  3370. mac_mode = MAC_MODE_PORT_MODE_MII;
  3371. } else
  3372. mac_mode = MAC_MODE_PORT_MODE_MII;
  3373. mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
  3374. if (tg3_asic_rev(tp) == ASIC_REV_5700) {
  3375. u32 speed = tg3_flag(tp, WOL_SPEED_100MB) ?
  3376. SPEED_100 : SPEED_10;
  3377. if (tg3_5700_link_polarity(tp, speed))
  3378. mac_mode |= MAC_MODE_LINK_POLARITY;
  3379. else
  3380. mac_mode &= ~MAC_MODE_LINK_POLARITY;
  3381. }
  3382. } else {
  3383. mac_mode = MAC_MODE_PORT_MODE_TBI;
  3384. }
  3385. if (!tg3_flag(tp, 5750_PLUS))
  3386. tw32(MAC_LED_CTRL, tp->led_ctrl);
  3387. mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
  3388. if ((tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS)) &&
  3389. (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)))
  3390. mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
  3391. if (tg3_flag(tp, ENABLE_APE))
  3392. mac_mode |= MAC_MODE_APE_TX_EN |
  3393. MAC_MODE_APE_RX_EN |
  3394. MAC_MODE_TDE_ENABLE;
  3395. tw32_f(MAC_MODE, mac_mode);
  3396. udelay(100);
  3397. tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
  3398. udelay(10);
  3399. }
  3400. if (!tg3_flag(tp, WOL_SPEED_100MB) &&
  3401. (tg3_asic_rev(tp) == ASIC_REV_5700 ||
  3402. tg3_asic_rev(tp) == ASIC_REV_5701)) {
  3403. u32 base_val;
  3404. base_val = tp->pci_clock_ctrl;
  3405. base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
  3406. CLOCK_CTRL_TXCLK_DISABLE);
  3407. tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
  3408. CLOCK_CTRL_PWRDOWN_PLL133, 40);
  3409. } else if (tg3_flag(tp, 5780_CLASS) ||
  3410. tg3_flag(tp, CPMU_PRESENT) ||
  3411. tg3_asic_rev(tp) == ASIC_REV_5906) {
  3412. /* do nothing */
  3413. } else if (!(tg3_flag(tp, 5750_PLUS) && tg3_flag(tp, ENABLE_ASF))) {
  3414. u32 newbits1, newbits2;
  3415. if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
  3416. tg3_asic_rev(tp) == ASIC_REV_5701) {
  3417. newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
  3418. CLOCK_CTRL_TXCLK_DISABLE |
  3419. CLOCK_CTRL_ALTCLK);
  3420. newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
  3421. } else if (tg3_flag(tp, 5705_PLUS)) {
  3422. newbits1 = CLOCK_CTRL_625_CORE;
  3423. newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
  3424. } else {
  3425. newbits1 = CLOCK_CTRL_ALTCLK;
  3426. newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
  3427. }
  3428. tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
  3429. 40);
  3430. tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
  3431. 40);
  3432. if (!tg3_flag(tp, 5705_PLUS)) {
  3433. u32 newbits3;
  3434. if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
  3435. tg3_asic_rev(tp) == ASIC_REV_5701) {
  3436. newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
  3437. CLOCK_CTRL_TXCLK_DISABLE |
  3438. CLOCK_CTRL_44MHZ_CORE);
  3439. } else {
  3440. newbits3 = CLOCK_CTRL_44MHZ_CORE;
  3441. }
  3442. tw32_wait_f(TG3PCI_CLOCK_CTRL,
  3443. tp->pci_clock_ctrl | newbits3, 40);
  3444. }
  3445. }
  3446. if (!(device_should_wake) && !tg3_flag(tp, ENABLE_ASF))
  3447. tg3_power_down_phy(tp, do_low_power);
  3448. tg3_frob_aux_power(tp, true);
  3449. /* Workaround for unstable PLL clock */
  3450. if ((!tg3_flag(tp, IS_SSB_CORE)) &&
  3451. ((tg3_chip_rev(tp) == CHIPREV_5750_AX) ||
  3452. (tg3_chip_rev(tp) == CHIPREV_5750_BX))) {
  3453. u32 val = tr32(0x7d00);
  3454. val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
  3455. tw32(0x7d00, val);
  3456. if (!tg3_flag(tp, ENABLE_ASF)) {
  3457. int err;
  3458. err = tg3_nvram_lock(tp);
  3459. tg3_halt_cpu(tp, RX_CPU_BASE);
  3460. if (!err)
  3461. tg3_nvram_unlock(tp);
  3462. }
  3463. }
  3464. tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
  3465. tg3_ape_driver_state_change(tp, RESET_KIND_SHUTDOWN);
  3466. return 0;
  3467. }
  3468. static void tg3_power_down(struct tg3 *tp)
  3469. {
  3470. pci_wake_from_d3(tp->pdev, tg3_flag(tp, WOL_ENABLE));
  3471. pci_set_power_state(tp->pdev, PCI_D3hot);
  3472. }
  3473. static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
  3474. {
  3475. switch (val & MII_TG3_AUX_STAT_SPDMASK) {
  3476. case MII_TG3_AUX_STAT_10HALF:
  3477. *speed = SPEED_10;
  3478. *duplex = DUPLEX_HALF;
  3479. break;
  3480. case MII_TG3_AUX_STAT_10FULL:
  3481. *speed = SPEED_10;
  3482. *duplex = DUPLEX_FULL;
  3483. break;
  3484. case MII_TG3_AUX_STAT_100HALF:
  3485. *speed = SPEED_100;
  3486. *duplex = DUPLEX_HALF;
  3487. break;
  3488. case MII_TG3_AUX_STAT_100FULL:
  3489. *speed = SPEED_100;
  3490. *duplex = DUPLEX_FULL;
  3491. break;
  3492. case MII_TG3_AUX_STAT_1000HALF:
  3493. *speed = SPEED_1000;
  3494. *duplex = DUPLEX_HALF;
  3495. break;
  3496. case MII_TG3_AUX_STAT_1000FULL:
  3497. *speed = SPEED_1000;
  3498. *duplex = DUPLEX_FULL;
  3499. break;
  3500. default:
  3501. if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
  3502. *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
  3503. SPEED_10;
  3504. *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
  3505. DUPLEX_HALF;
  3506. break;
  3507. }
  3508. *speed = SPEED_UNKNOWN;
  3509. *duplex = DUPLEX_UNKNOWN;
  3510. break;
  3511. }
  3512. }
  3513. static int tg3_phy_autoneg_cfg(struct tg3 *tp, u32 advertise, u32 flowctrl)
  3514. {
  3515. int err = 0;
  3516. u32 val, new_adv;
  3517. new_adv = ADVERTISE_CSMA;
  3518. new_adv |= ethtool_adv_to_mii_adv_t(advertise) & ADVERTISE_ALL;
  3519. new_adv |= mii_advertise_flowctrl(flowctrl);
  3520. err = tg3_writephy(tp, MII_ADVERTISE, new_adv);
  3521. if (err)
  3522. goto done;
  3523. if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
  3524. new_adv = ethtool_adv_to_mii_ctrl1000_t(advertise);
  3525. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
  3526. tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0)
  3527. new_adv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
  3528. err = tg3_writephy(tp, MII_CTRL1000, new_adv);
  3529. if (err)
  3530. goto done;
  3531. }
  3532. if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
  3533. goto done;
  3534. tw32(TG3_CPMU_EEE_MODE,
  3535. tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
  3536. err = tg3_phy_toggle_auxctl_smdsp(tp, true);
  3537. if (!err) {
  3538. u32 err2;
  3539. val = 0;
  3540. /* Advertise 100-BaseTX EEE ability */
  3541. if (advertise & ADVERTISED_100baseT_Full)
  3542. val |= MDIO_AN_EEE_ADV_100TX;
  3543. /* Advertise 1000-BaseT EEE ability */
  3544. if (advertise & ADVERTISED_1000baseT_Full)
  3545. val |= MDIO_AN_EEE_ADV_1000T;
  3546. if (!tp->eee.eee_enabled) {
  3547. val = 0;
  3548. tp->eee.advertised = 0;
  3549. } else {
  3550. tp->eee.advertised = advertise &
  3551. (ADVERTISED_100baseT_Full |
  3552. ADVERTISED_1000baseT_Full);
  3553. }
  3554. err = tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
  3555. if (err)
  3556. val = 0;
  3557. switch (tg3_asic_rev(tp)) {
  3558. case ASIC_REV_5717:
  3559. case ASIC_REV_57765:
  3560. case ASIC_REV_57766:
  3561. case ASIC_REV_5719:
  3562. /* If we advertised any eee advertisements above... */
  3563. if (val)
  3564. val = MII_TG3_DSP_TAP26_ALNOKO |
  3565. MII_TG3_DSP_TAP26_RMRXSTO |
  3566. MII_TG3_DSP_TAP26_OPCSINPT;
  3567. tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
  3568. /* Fall through */
  3569. case ASIC_REV_5720:
  3570. case ASIC_REV_5762:
  3571. if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
  3572. tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
  3573. MII_TG3_DSP_CH34TP2_HIBW01);
  3574. }
  3575. err2 = tg3_phy_toggle_auxctl_smdsp(tp, false);
  3576. if (!err)
  3577. err = err2;
  3578. }
  3579. done:
  3580. return err;
  3581. }
  3582. static void tg3_phy_copper_begin(struct tg3 *tp)
  3583. {
  3584. if (tp->link_config.autoneg == AUTONEG_ENABLE ||
  3585. (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
  3586. u32 adv, fc;
  3587. if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
  3588. !(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN)) {
  3589. adv = ADVERTISED_10baseT_Half |
  3590. ADVERTISED_10baseT_Full;
  3591. if (tg3_flag(tp, WOL_SPEED_100MB))
  3592. adv |= ADVERTISED_100baseT_Half |
  3593. ADVERTISED_100baseT_Full;
  3594. if (tp->phy_flags & TG3_PHYFLG_1G_ON_VAUX_OK) {
  3595. if (!(tp->phy_flags &
  3596. TG3_PHYFLG_DISABLE_1G_HD_ADV))
  3597. adv |= ADVERTISED_1000baseT_Half;
  3598. adv |= ADVERTISED_1000baseT_Full;
  3599. }
  3600. fc = FLOW_CTRL_TX | FLOW_CTRL_RX;
  3601. } else {
  3602. adv = tp->link_config.advertising;
  3603. if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
  3604. adv &= ~(ADVERTISED_1000baseT_Half |
  3605. ADVERTISED_1000baseT_Full);
  3606. fc = tp->link_config.flowctrl;
  3607. }
  3608. tg3_phy_autoneg_cfg(tp, adv, fc);
  3609. if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
  3610. (tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN)) {
  3611. /* Normally during power down we want to autonegotiate
  3612. * the lowest possible speed for WOL. However, to avoid
  3613. * link flap, we leave it untouched.
  3614. */
  3615. return;
  3616. }
  3617. tg3_writephy(tp, MII_BMCR,
  3618. BMCR_ANENABLE | BMCR_ANRESTART);
  3619. } else {
  3620. int i;
  3621. u32 bmcr, orig_bmcr;
  3622. tp->link_config.active_speed = tp->link_config.speed;
  3623. tp->link_config.active_duplex = tp->link_config.duplex;
  3624. if (tg3_asic_rev(tp) == ASIC_REV_5714) {
  3625. /* With autoneg disabled, 5715 only links up when the
  3626. * advertisement register has the configured speed
  3627. * enabled.
  3628. */
  3629. tg3_writephy(tp, MII_ADVERTISE, ADVERTISE_ALL);
  3630. }
  3631. bmcr = 0;
  3632. switch (tp->link_config.speed) {
  3633. default:
  3634. case SPEED_10:
  3635. break;
  3636. case SPEED_100:
  3637. bmcr |= BMCR_SPEED100;
  3638. break;
  3639. case SPEED_1000:
  3640. bmcr |= BMCR_SPEED1000;
  3641. break;
  3642. }
  3643. if (tp->link_config.duplex == DUPLEX_FULL)
  3644. bmcr |= BMCR_FULLDPLX;
  3645. if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
  3646. (bmcr != orig_bmcr)) {
  3647. tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
  3648. for (i = 0; i < 1500; i++) {
  3649. u32 tmp;
  3650. udelay(10);
  3651. if (tg3_readphy(tp, MII_BMSR, &tmp) ||
  3652. tg3_readphy(tp, MII_BMSR, &tmp))
  3653. continue;
  3654. if (!(tmp & BMSR_LSTATUS)) {
  3655. udelay(40);
  3656. break;
  3657. }
  3658. }
  3659. tg3_writephy(tp, MII_BMCR, bmcr);
  3660. udelay(40);
  3661. }
  3662. }
  3663. }
  3664. static int tg3_phy_pull_config(struct tg3 *tp)
  3665. {
  3666. int err;
  3667. u32 val;
  3668. err = tg3_readphy(tp, MII_BMCR, &val);
  3669. if (err)
  3670. goto done;
  3671. if (!(val & BMCR_ANENABLE)) {
  3672. tp->link_config.autoneg = AUTONEG_DISABLE;
  3673. tp->link_config.advertising = 0;
  3674. tg3_flag_clear(tp, PAUSE_AUTONEG);
  3675. err = -EIO;
  3676. switch (val & (BMCR_SPEED1000 | BMCR_SPEED100)) {
  3677. case 0:
  3678. if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
  3679. goto done;
  3680. tp->link_config.speed = SPEED_10;
  3681. break;
  3682. case BMCR_SPEED100:
  3683. if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
  3684. goto done;
  3685. tp->link_config.speed = SPEED_100;
  3686. break;
  3687. case BMCR_SPEED1000:
  3688. if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
  3689. tp->link_config.speed = SPEED_1000;
  3690. break;
  3691. }
  3692. /* Fall through */
  3693. default:
  3694. goto done;
  3695. }
  3696. if (val & BMCR_FULLDPLX)
  3697. tp->link_config.duplex = DUPLEX_FULL;
  3698. else
  3699. tp->link_config.duplex = DUPLEX_HALF;
  3700. tp->link_config.flowctrl = FLOW_CTRL_RX | FLOW_CTRL_TX;
  3701. err = 0;
  3702. goto done;
  3703. }
  3704. tp->link_config.autoneg = AUTONEG_ENABLE;
  3705. tp->link_config.advertising = ADVERTISED_Autoneg;
  3706. tg3_flag_set(tp, PAUSE_AUTONEG);
  3707. if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
  3708. u32 adv;
  3709. err = tg3_readphy(tp, MII_ADVERTISE, &val);
  3710. if (err)
  3711. goto done;
  3712. adv = mii_adv_to_ethtool_adv_t(val & ADVERTISE_ALL);
  3713. tp->link_config.advertising |= adv | ADVERTISED_TP;
  3714. tp->link_config.flowctrl = tg3_decode_flowctrl_1000T(val);
  3715. } else {
  3716. tp->link_config.advertising |= ADVERTISED_FIBRE;
  3717. }
  3718. if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
  3719. u32 adv;
  3720. if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
  3721. err = tg3_readphy(tp, MII_CTRL1000, &val);
  3722. if (err)
  3723. goto done;
  3724. adv = mii_ctrl1000_to_ethtool_adv_t(val);
  3725. } else {
  3726. err = tg3_readphy(tp, MII_ADVERTISE, &val);
  3727. if (err)
  3728. goto done;
  3729. adv = tg3_decode_flowctrl_1000X(val);
  3730. tp->link_config.flowctrl = adv;
  3731. val &= (ADVERTISE_1000XHALF | ADVERTISE_1000XFULL);
  3732. adv = mii_adv_to_ethtool_adv_x(val);
  3733. }
  3734. tp->link_config.advertising |= adv;
  3735. }
  3736. done:
  3737. return err;
  3738. }
  3739. static int tg3_init_5401phy_dsp(struct tg3 *tp)
  3740. {
  3741. int err;
  3742. /* Turn off tap power management. */
  3743. /* Set Extended packet length bit */
  3744. err = tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
  3745. err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
  3746. err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
  3747. err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
  3748. err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
  3749. err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
  3750. udelay(40);
  3751. return err;
  3752. }
  3753. static bool tg3_phy_eee_config_ok(struct tg3 *tp)
  3754. {
  3755. struct ethtool_eee eee;
  3756. if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
  3757. return true;
  3758. tg3_eee_pull_config(tp, &eee);
  3759. if (tp->eee.eee_enabled) {
  3760. if (tp->eee.advertised != eee.advertised ||
  3761. tp->eee.tx_lpi_timer != eee.tx_lpi_timer ||
  3762. tp->eee.tx_lpi_enabled != eee.tx_lpi_enabled)
  3763. return false;
  3764. } else {
  3765. /* EEE is disabled but we're advertising */
  3766. if (eee.advertised)
  3767. return false;
  3768. }
  3769. return true;
  3770. }
  3771. static bool tg3_phy_copper_an_config_ok(struct tg3 *tp, u32 *lcladv)
  3772. {
  3773. u32 advmsk, tgtadv, advertising;
  3774. advertising = tp->link_config.advertising;
  3775. tgtadv = ethtool_adv_to_mii_adv_t(advertising) & ADVERTISE_ALL;
  3776. advmsk = ADVERTISE_ALL;
  3777. if (tp->link_config.active_duplex == DUPLEX_FULL) {
  3778. tgtadv |= mii_advertise_flowctrl(tp->link_config.flowctrl);
  3779. advmsk |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
  3780. }
  3781. if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
  3782. return false;
  3783. if ((*lcladv & advmsk) != tgtadv)
  3784. return false;
  3785. if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
  3786. u32 tg3_ctrl;
  3787. tgtadv = ethtool_adv_to_mii_ctrl1000_t(advertising);
  3788. if (tg3_readphy(tp, MII_CTRL1000, &tg3_ctrl))
  3789. return false;
  3790. if (tgtadv &&
  3791. (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
  3792. tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0)) {
  3793. tgtadv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
  3794. tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL |
  3795. CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
  3796. } else {
  3797. tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL);
  3798. }
  3799. if (tg3_ctrl != tgtadv)
  3800. return false;
  3801. }
  3802. return true;
  3803. }
  3804. static bool tg3_phy_copper_fetch_rmtadv(struct tg3 *tp, u32 *rmtadv)
  3805. {
  3806. u32 lpeth = 0;
  3807. if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
  3808. u32 val;
  3809. if (tg3_readphy(tp, MII_STAT1000, &val))
  3810. return false;
  3811. lpeth = mii_stat1000_to_ethtool_lpa_t(val);
  3812. }
  3813. if (tg3_readphy(tp, MII_LPA, rmtadv))
  3814. return false;
  3815. lpeth |= mii_lpa_to_ethtool_lpa_t(*rmtadv);
  3816. tp->link_config.rmt_adv = lpeth;
  3817. return true;
  3818. }
  3819. static bool tg3_test_and_report_link_chg(struct tg3 *tp, bool curr_link_up)
  3820. {
  3821. if (curr_link_up != tp->link_up) {
  3822. if (curr_link_up) {
  3823. netif_carrier_on(tp->dev);
  3824. } else {
  3825. netif_carrier_off(tp->dev);
  3826. if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
  3827. tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
  3828. }
  3829. tg3_link_report(tp);
  3830. return true;
  3831. }
  3832. return false;
  3833. }
  3834. static void tg3_clear_mac_status(struct tg3 *tp)
  3835. {
  3836. tw32(MAC_EVENT, 0);
  3837. tw32_f(MAC_STATUS,
  3838. MAC_STATUS_SYNC_CHANGED |
  3839. MAC_STATUS_CFG_CHANGED |
  3840. MAC_STATUS_MI_COMPLETION |
  3841. MAC_STATUS_LNKSTATE_CHANGED);
  3842. udelay(40);
  3843. }
  3844. static void tg3_setup_eee(struct tg3 *tp)
  3845. {
  3846. u32 val;
  3847. val = TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
  3848. TG3_CPMU_EEE_LNKIDL_UART_IDL;
  3849. if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0)
  3850. val |= TG3_CPMU_EEE_LNKIDL_APE_TX_MT;
  3851. tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL, val);
  3852. tw32_f(TG3_CPMU_EEE_CTRL,
  3853. TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
  3854. val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
  3855. (tp->eee.tx_lpi_enabled ? TG3_CPMU_EEEMD_LPI_IN_TX : 0) |
  3856. TG3_CPMU_EEEMD_LPI_IN_RX |
  3857. TG3_CPMU_EEEMD_EEE_ENABLE;
  3858. if (tg3_asic_rev(tp) != ASIC_REV_5717)
  3859. val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
  3860. if (tg3_flag(tp, ENABLE_APE))
  3861. val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
  3862. tw32_f(TG3_CPMU_EEE_MODE, tp->eee.eee_enabled ? val : 0);
  3863. tw32_f(TG3_CPMU_EEE_DBTMR1,
  3864. TG3_CPMU_DBTMR1_PCIEXIT_2047US |
  3865. (tp->eee.tx_lpi_timer & 0xffff));
  3866. tw32_f(TG3_CPMU_EEE_DBTMR2,
  3867. TG3_CPMU_DBTMR2_APE_TX_2047US |
  3868. TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
  3869. }
  3870. static int tg3_setup_copper_phy(struct tg3 *tp, bool force_reset)
  3871. {
  3872. bool current_link_up;
  3873. u32 bmsr, val;
  3874. u32 lcl_adv, rmt_adv;
  3875. u16 current_speed;
  3876. u8 current_duplex;
  3877. int i, err;
  3878. tg3_clear_mac_status(tp);
  3879. if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
  3880. tw32_f(MAC_MI_MODE,
  3881. (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
  3882. udelay(80);
  3883. }
  3884. tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, 0);
  3885. /* Some third-party PHYs need to be reset on link going
  3886. * down.
  3887. */
  3888. if ((tg3_asic_rev(tp) == ASIC_REV_5703 ||
  3889. tg3_asic_rev(tp) == ASIC_REV_5704 ||
  3890. tg3_asic_rev(tp) == ASIC_REV_5705) &&
  3891. tp->link_up) {
  3892. tg3_readphy(tp, MII_BMSR, &bmsr);
  3893. if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
  3894. !(bmsr & BMSR_LSTATUS))
  3895. force_reset = true;
  3896. }
  3897. if (force_reset)
  3898. tg3_phy_reset(tp);
  3899. if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
  3900. tg3_readphy(tp, MII_BMSR, &bmsr);
  3901. if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
  3902. !tg3_flag(tp, INIT_COMPLETE))
  3903. bmsr = 0;
  3904. if (!(bmsr & BMSR_LSTATUS)) {
  3905. err = tg3_init_5401phy_dsp(tp);
  3906. if (err)
  3907. return err;
  3908. tg3_readphy(tp, MII_BMSR, &bmsr);
  3909. for (i = 0; i < 1000; i++) {
  3910. udelay(10);
  3911. if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
  3912. (bmsr & BMSR_LSTATUS)) {
  3913. udelay(40);
  3914. break;
  3915. }
  3916. }
  3917. if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
  3918. TG3_PHY_REV_BCM5401_B0 &&
  3919. !(bmsr & BMSR_LSTATUS) &&
  3920. tp->link_config.active_speed == SPEED_1000) {
  3921. err = tg3_phy_reset(tp);
  3922. if (!err)
  3923. err = tg3_init_5401phy_dsp(tp);
  3924. if (err)
  3925. return err;
  3926. }
  3927. }
  3928. } else if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
  3929. tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0) {
  3930. /* 5701 {A0,B0} CRC bug workaround */
  3931. tg3_writephy(tp, 0x15, 0x0a75);
  3932. tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
  3933. tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
  3934. tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
  3935. }
  3936. /* Clear pending interrupts... */
  3937. tg3_readphy(tp, MII_TG3_ISTAT, &val);
  3938. tg3_readphy(tp, MII_TG3_ISTAT, &val);
  3939. if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
  3940. tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
  3941. else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
  3942. tg3_writephy(tp, MII_TG3_IMASK, ~0);
  3943. if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
  3944. tg3_asic_rev(tp) == ASIC_REV_5701) {
  3945. if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
  3946. tg3_writephy(tp, MII_TG3_EXT_CTRL,
  3947. MII_TG3_EXT_CTRL_LNK3_LED_MODE);
  3948. else
  3949. tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
  3950. }
  3951. current_link_up = false;
  3952. current_speed = SPEED_UNKNOWN;
  3953. current_duplex = DUPLEX_UNKNOWN;
  3954. tp->phy_flags &= ~TG3_PHYFLG_MDIX_STATE;
  3955. tp->link_config.rmt_adv = 0;
  3956. if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
  3957. err = tg3_phy_auxctl_read(tp,
  3958. MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
  3959. &val);
  3960. if (!err && !(val & (1 << 10))) {
  3961. tg3_phy_auxctl_write(tp,
  3962. MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
  3963. val | (1 << 10));
  3964. goto relink;
  3965. }
  3966. }
  3967. bmsr = 0;
  3968. for (i = 0; i < 100; i++) {
  3969. tg3_readphy(tp, MII_BMSR, &bmsr);
  3970. if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
  3971. (bmsr & BMSR_LSTATUS))
  3972. break;
  3973. udelay(40);
  3974. }
  3975. if (bmsr & BMSR_LSTATUS) {
  3976. u32 aux_stat, bmcr;
  3977. tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
  3978. for (i = 0; i < 2000; i++) {
  3979. udelay(10);
  3980. if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
  3981. aux_stat)
  3982. break;
  3983. }
  3984. tg3_aux_stat_to_speed_duplex(tp, aux_stat,
  3985. &current_speed,
  3986. &current_duplex);
  3987. bmcr = 0;
  3988. for (i = 0; i < 200; i++) {
  3989. tg3_readphy(tp, MII_BMCR, &bmcr);
  3990. if (tg3_readphy(tp, MII_BMCR, &bmcr))
  3991. continue;
  3992. if (bmcr && bmcr != 0x7fff)
  3993. break;
  3994. udelay(10);
  3995. }
  3996. lcl_adv = 0;
  3997. rmt_adv = 0;
  3998. tp->link_config.active_speed = current_speed;
  3999. tp->link_config.active_duplex = current_duplex;
  4000. if (tp->link_config.autoneg == AUTONEG_ENABLE) {
  4001. bool eee_config_ok = tg3_phy_eee_config_ok(tp);
  4002. if ((bmcr & BMCR_ANENABLE) &&
  4003. eee_config_ok &&
  4004. tg3_phy_copper_an_config_ok(tp, &lcl_adv) &&
  4005. tg3_phy_copper_fetch_rmtadv(tp, &rmt_adv))
  4006. current_link_up = true;
  4007. /* EEE settings changes take effect only after a phy
  4008. * reset. If we have skipped a reset due to Link Flap
  4009. * Avoidance being enabled, do it now.
  4010. */
  4011. if (!eee_config_ok &&
  4012. (tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN) &&
  4013. !force_reset) {
  4014. tg3_setup_eee(tp);
  4015. tg3_phy_reset(tp);
  4016. }
  4017. } else {
  4018. if (!(bmcr & BMCR_ANENABLE) &&
  4019. tp->link_config.speed == current_speed &&
  4020. tp->link_config.duplex == current_duplex) {
  4021. current_link_up = true;
  4022. }
  4023. }
  4024. if (current_link_up &&
  4025. tp->link_config.active_duplex == DUPLEX_FULL) {
  4026. u32 reg, bit;
  4027. if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
  4028. reg = MII_TG3_FET_GEN_STAT;
  4029. bit = MII_TG3_FET_GEN_STAT_MDIXSTAT;
  4030. } else {
  4031. reg = MII_TG3_EXT_STAT;
  4032. bit = MII_TG3_EXT_STAT_MDIX;
  4033. }
  4034. if (!tg3_readphy(tp, reg, &val) && (val & bit))
  4035. tp->phy_flags |= TG3_PHYFLG_MDIX_STATE;
  4036. tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
  4037. }
  4038. }
  4039. relink:
  4040. if (!current_link_up || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
  4041. tg3_phy_copper_begin(tp);
  4042. if (tg3_flag(tp, ROBOSWITCH)) {
  4043. current_link_up = true;
  4044. /* FIXME: when BCM5325 switch is used use 100 MBit/s */
  4045. current_speed = SPEED_1000;
  4046. current_duplex = DUPLEX_FULL;
  4047. tp->link_config.active_speed = current_speed;
  4048. tp->link_config.active_duplex = current_duplex;
  4049. }
  4050. tg3_readphy(tp, MII_BMSR, &bmsr);
  4051. if ((!tg3_readphy(tp, MII_BMSR, &bmsr) && (bmsr & BMSR_LSTATUS)) ||
  4052. (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
  4053. current_link_up = true;
  4054. }
  4055. tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
  4056. if (current_link_up) {
  4057. if (tp->link_config.active_speed == SPEED_100 ||
  4058. tp->link_config.active_speed == SPEED_10)
  4059. tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
  4060. else
  4061. tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
  4062. } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
  4063. tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
  4064. else
  4065. tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
  4066. /* In order for the 5750 core in BCM4785 chip to work properly
  4067. * in RGMII mode, the Led Control Register must be set up.
  4068. */
  4069. if (tg3_flag(tp, RGMII_MODE)) {
  4070. u32 led_ctrl = tr32(MAC_LED_CTRL);
  4071. led_ctrl &= ~(LED_CTRL_1000MBPS_ON | LED_CTRL_100MBPS_ON);
  4072. if (tp->link_config.active_speed == SPEED_10)
  4073. led_ctrl |= LED_CTRL_LNKLED_OVERRIDE;
  4074. else if (tp->link_config.active_speed == SPEED_100)
  4075. led_ctrl |= (LED_CTRL_LNKLED_OVERRIDE |
  4076. LED_CTRL_100MBPS_ON);
  4077. else if (tp->link_config.active_speed == SPEED_1000)
  4078. led_ctrl |= (LED_CTRL_LNKLED_OVERRIDE |
  4079. LED_CTRL_1000MBPS_ON);
  4080. tw32(MAC_LED_CTRL, led_ctrl);
  4081. udelay(40);
  4082. }
  4083. tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
  4084. if (tp->link_config.active_duplex == DUPLEX_HALF)
  4085. tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
  4086. if (tg3_asic_rev(tp) == ASIC_REV_5700) {
  4087. if (current_link_up &&
  4088. tg3_5700_link_polarity(tp, tp->link_config.active_speed))
  4089. tp->mac_mode |= MAC_MODE_LINK_POLARITY;
  4090. else
  4091. tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
  4092. }
  4093. /* ??? Without this setting Netgear GA302T PHY does not
  4094. * ??? send/receive packets...
  4095. */
  4096. if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
  4097. tg3_chip_rev_id(tp) == CHIPREV_ID_5700_ALTIMA) {
  4098. tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
  4099. tw32_f(MAC_MI_MODE, tp->mi_mode);
  4100. udelay(80);
  4101. }
  4102. tw32_f(MAC_MODE, tp->mac_mode);
  4103. udelay(40);
  4104. tg3_phy_eee_adjust(tp, current_link_up);
  4105. if (tg3_flag(tp, USE_LINKCHG_REG)) {
  4106. /* Polled via timer. */
  4107. tw32_f(MAC_EVENT, 0);
  4108. } else {
  4109. tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
  4110. }
  4111. udelay(40);
  4112. if (tg3_asic_rev(tp) == ASIC_REV_5700 &&
  4113. current_link_up &&
  4114. tp->link_config.active_speed == SPEED_1000 &&
  4115. (tg3_flag(tp, PCIX_MODE) || tg3_flag(tp, PCI_HIGH_SPEED))) {
  4116. udelay(120);
  4117. tw32_f(MAC_STATUS,
  4118. (MAC_STATUS_SYNC_CHANGED |
  4119. MAC_STATUS_CFG_CHANGED));
  4120. udelay(40);
  4121. tg3_write_mem(tp,
  4122. NIC_SRAM_FIRMWARE_MBOX,
  4123. NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
  4124. }
  4125. /* Prevent send BD corruption. */
  4126. if (tg3_flag(tp, CLKREQ_BUG)) {
  4127. if (tp->link_config.active_speed == SPEED_100 ||
  4128. tp->link_config.active_speed == SPEED_10)
  4129. pcie_capability_clear_word(tp->pdev, PCI_EXP_LNKCTL,
  4130. PCI_EXP_LNKCTL_CLKREQ_EN);
  4131. else
  4132. pcie_capability_set_word(tp->pdev, PCI_EXP_LNKCTL,
  4133. PCI_EXP_LNKCTL_CLKREQ_EN);
  4134. }
  4135. tg3_test_and_report_link_chg(tp, current_link_up);
  4136. return 0;
  4137. }
  4138. struct tg3_fiber_aneginfo {
  4139. int state;
  4140. #define ANEG_STATE_UNKNOWN 0
  4141. #define ANEG_STATE_AN_ENABLE 1
  4142. #define ANEG_STATE_RESTART_INIT 2
  4143. #define ANEG_STATE_RESTART 3
  4144. #define ANEG_STATE_DISABLE_LINK_OK 4
  4145. #define ANEG_STATE_ABILITY_DETECT_INIT 5
  4146. #define ANEG_STATE_ABILITY_DETECT 6
  4147. #define ANEG_STATE_ACK_DETECT_INIT 7
  4148. #define ANEG_STATE_ACK_DETECT 8
  4149. #define ANEG_STATE_COMPLETE_ACK_INIT 9
  4150. #define ANEG_STATE_COMPLETE_ACK 10
  4151. #define ANEG_STATE_IDLE_DETECT_INIT 11
  4152. #define ANEG_STATE_IDLE_DETECT 12
  4153. #define ANEG_STATE_LINK_OK 13
  4154. #define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
  4155. #define ANEG_STATE_NEXT_PAGE_WAIT 15
  4156. u32 flags;
  4157. #define MR_AN_ENABLE 0x00000001
  4158. #define MR_RESTART_AN 0x00000002
  4159. #define MR_AN_COMPLETE 0x00000004
  4160. #define MR_PAGE_RX 0x00000008
  4161. #define MR_NP_LOADED 0x00000010
  4162. #define MR_TOGGLE_TX 0x00000020
  4163. #define MR_LP_ADV_FULL_DUPLEX 0x00000040
  4164. #define MR_LP_ADV_HALF_DUPLEX 0x00000080
  4165. #define MR_LP_ADV_SYM_PAUSE 0x00000100
  4166. #define MR_LP_ADV_ASYM_PAUSE 0x00000200
  4167. #define MR_LP_ADV_REMOTE_FAULT1 0x00000400
  4168. #define MR_LP_ADV_REMOTE_FAULT2 0x00000800
  4169. #define MR_LP_ADV_NEXT_PAGE 0x00001000
  4170. #define MR_TOGGLE_RX 0x00002000
  4171. #define MR_NP_RX 0x00004000
  4172. #define MR_LINK_OK 0x80000000
  4173. unsigned long link_time, cur_time;
  4174. u32 ability_match_cfg;
  4175. int ability_match_count;
  4176. char ability_match, idle_match, ack_match;
  4177. u32 txconfig, rxconfig;
  4178. #define ANEG_CFG_NP 0x00000080
  4179. #define ANEG_CFG_ACK 0x00000040
  4180. #define ANEG_CFG_RF2 0x00000020
  4181. #define ANEG_CFG_RF1 0x00000010
  4182. #define ANEG_CFG_PS2 0x00000001
  4183. #define ANEG_CFG_PS1 0x00008000
  4184. #define ANEG_CFG_HD 0x00004000
  4185. #define ANEG_CFG_FD 0x00002000
  4186. #define ANEG_CFG_INVAL 0x00001f06
  4187. };
  4188. #define ANEG_OK 0
  4189. #define ANEG_DONE 1
  4190. #define ANEG_TIMER_ENAB 2
  4191. #define ANEG_FAILED -1
  4192. #define ANEG_STATE_SETTLE_TIME 10000
  4193. static int tg3_fiber_aneg_smachine(struct tg3 *tp,
  4194. struct tg3_fiber_aneginfo *ap)
  4195. {
  4196. u16 flowctrl;
  4197. unsigned long delta;
  4198. u32 rx_cfg_reg;
  4199. int ret;
  4200. if (ap->state == ANEG_STATE_UNKNOWN) {
  4201. ap->rxconfig = 0;
  4202. ap->link_time = 0;
  4203. ap->cur_time = 0;
  4204. ap->ability_match_cfg = 0;
  4205. ap->ability_match_count = 0;
  4206. ap->ability_match = 0;
  4207. ap->idle_match = 0;
  4208. ap->ack_match = 0;
  4209. }
  4210. ap->cur_time++;
  4211. if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
  4212. rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
  4213. if (rx_cfg_reg != ap->ability_match_cfg) {
  4214. ap->ability_match_cfg = rx_cfg_reg;
  4215. ap->ability_match = 0;
  4216. ap->ability_match_count = 0;
  4217. } else {
  4218. if (++ap->ability_match_count > 1) {
  4219. ap->ability_match = 1;
  4220. ap->ability_match_cfg = rx_cfg_reg;
  4221. }
  4222. }
  4223. if (rx_cfg_reg & ANEG_CFG_ACK)
  4224. ap->ack_match = 1;
  4225. else
  4226. ap->ack_match = 0;
  4227. ap->idle_match = 0;
  4228. } else {
  4229. ap->idle_match = 1;
  4230. ap->ability_match_cfg = 0;
  4231. ap->ability_match_count = 0;
  4232. ap->ability_match = 0;
  4233. ap->ack_match = 0;
  4234. rx_cfg_reg = 0;
  4235. }
  4236. ap->rxconfig = rx_cfg_reg;
  4237. ret = ANEG_OK;
  4238. switch (ap->state) {
  4239. case ANEG_STATE_UNKNOWN:
  4240. if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
  4241. ap->state = ANEG_STATE_AN_ENABLE;
  4242. /* fallthru */
  4243. case ANEG_STATE_AN_ENABLE:
  4244. ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
  4245. if (ap->flags & MR_AN_ENABLE) {
  4246. ap->link_time = 0;
  4247. ap->cur_time = 0;
  4248. ap->ability_match_cfg = 0;
  4249. ap->ability_match_count = 0;
  4250. ap->ability_match = 0;
  4251. ap->idle_match = 0;
  4252. ap->ack_match = 0;
  4253. ap->state = ANEG_STATE_RESTART_INIT;
  4254. } else {
  4255. ap->state = ANEG_STATE_DISABLE_LINK_OK;
  4256. }
  4257. break;
  4258. case ANEG_STATE_RESTART_INIT:
  4259. ap->link_time = ap->cur_time;
  4260. ap->flags &= ~(MR_NP_LOADED);
  4261. ap->txconfig = 0;
  4262. tw32(MAC_TX_AUTO_NEG, 0);
  4263. tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
  4264. tw32_f(MAC_MODE, tp->mac_mode);
  4265. udelay(40);
  4266. ret = ANEG_TIMER_ENAB;
  4267. ap->state = ANEG_STATE_RESTART;
  4268. /* fallthru */
  4269. case ANEG_STATE_RESTART:
  4270. delta = ap->cur_time - ap->link_time;
  4271. if (delta > ANEG_STATE_SETTLE_TIME)
  4272. ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
  4273. else
  4274. ret = ANEG_TIMER_ENAB;
  4275. break;
  4276. case ANEG_STATE_DISABLE_LINK_OK:
  4277. ret = ANEG_DONE;
  4278. break;
  4279. case ANEG_STATE_ABILITY_DETECT_INIT:
  4280. ap->flags &= ~(MR_TOGGLE_TX);
  4281. ap->txconfig = ANEG_CFG_FD;
  4282. flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
  4283. if (flowctrl & ADVERTISE_1000XPAUSE)
  4284. ap->txconfig |= ANEG_CFG_PS1;
  4285. if (flowctrl & ADVERTISE_1000XPSE_ASYM)
  4286. ap->txconfig |= ANEG_CFG_PS2;
  4287. tw32(MAC_TX_AUTO_NEG, ap->txconfig);
  4288. tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
  4289. tw32_f(MAC_MODE, tp->mac_mode);
  4290. udelay(40);
  4291. ap->state = ANEG_STATE_ABILITY_DETECT;
  4292. break;
  4293. case ANEG_STATE_ABILITY_DETECT:
  4294. if (ap->ability_match != 0 && ap->rxconfig != 0)
  4295. ap->state = ANEG_STATE_ACK_DETECT_INIT;
  4296. break;
  4297. case ANEG_STATE_ACK_DETECT_INIT:
  4298. ap->txconfig |= ANEG_CFG_ACK;
  4299. tw32(MAC_TX_AUTO_NEG, ap->txconfig);
  4300. tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
  4301. tw32_f(MAC_MODE, tp->mac_mode);
  4302. udelay(40);
  4303. ap->state = ANEG_STATE_ACK_DETECT;
  4304. /* fallthru */
  4305. case ANEG_STATE_ACK_DETECT:
  4306. if (ap->ack_match != 0) {
  4307. if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
  4308. (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
  4309. ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
  4310. } else {
  4311. ap->state = ANEG_STATE_AN_ENABLE;
  4312. }
  4313. } else if (ap->ability_match != 0 &&
  4314. ap->rxconfig == 0) {
  4315. ap->state = ANEG_STATE_AN_ENABLE;
  4316. }
  4317. break;
  4318. case ANEG_STATE_COMPLETE_ACK_INIT:
  4319. if (ap->rxconfig & ANEG_CFG_INVAL) {
  4320. ret = ANEG_FAILED;
  4321. break;
  4322. }
  4323. ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
  4324. MR_LP_ADV_HALF_DUPLEX |
  4325. MR_LP_ADV_SYM_PAUSE |
  4326. MR_LP_ADV_ASYM_PAUSE |
  4327. MR_LP_ADV_REMOTE_FAULT1 |
  4328. MR_LP_ADV_REMOTE_FAULT2 |
  4329. MR_LP_ADV_NEXT_PAGE |
  4330. MR_TOGGLE_RX |
  4331. MR_NP_RX);
  4332. if (ap->rxconfig & ANEG_CFG_FD)
  4333. ap->flags |= MR_LP_ADV_FULL_DUPLEX;
  4334. if (ap->rxconfig & ANEG_CFG_HD)
  4335. ap->flags |= MR_LP_ADV_HALF_DUPLEX;
  4336. if (ap->rxconfig & ANEG_CFG_PS1)
  4337. ap->flags |= MR_LP_ADV_SYM_PAUSE;
  4338. if (ap->rxconfig & ANEG_CFG_PS2)
  4339. ap->flags |= MR_LP_ADV_ASYM_PAUSE;
  4340. if (ap->rxconfig & ANEG_CFG_RF1)
  4341. ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
  4342. if (ap->rxconfig & ANEG_CFG_RF2)
  4343. ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
  4344. if (ap->rxconfig & ANEG_CFG_NP)
  4345. ap->flags |= MR_LP_ADV_NEXT_PAGE;
  4346. ap->link_time = ap->cur_time;
  4347. ap->flags ^= (MR_TOGGLE_TX);
  4348. if (ap->rxconfig & 0x0008)
  4349. ap->flags |= MR_TOGGLE_RX;
  4350. if (ap->rxconfig & ANEG_CFG_NP)
  4351. ap->flags |= MR_NP_RX;
  4352. ap->flags |= MR_PAGE_RX;
  4353. ap->state = ANEG_STATE_COMPLETE_ACK;
  4354. ret = ANEG_TIMER_ENAB;
  4355. break;
  4356. case ANEG_STATE_COMPLETE_ACK:
  4357. if (ap->ability_match != 0 &&
  4358. ap->rxconfig == 0) {
  4359. ap->state = ANEG_STATE_AN_ENABLE;
  4360. break;
  4361. }
  4362. delta = ap->cur_time - ap->link_time;
  4363. if (delta > ANEG_STATE_SETTLE_TIME) {
  4364. if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
  4365. ap->state = ANEG_STATE_IDLE_DETECT_INIT;
  4366. } else {
  4367. if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
  4368. !(ap->flags & MR_NP_RX)) {
  4369. ap->state = ANEG_STATE_IDLE_DETECT_INIT;
  4370. } else {
  4371. ret = ANEG_FAILED;
  4372. }
  4373. }
  4374. }
  4375. break;
  4376. case ANEG_STATE_IDLE_DETECT_INIT:
  4377. ap->link_time = ap->cur_time;
  4378. tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
  4379. tw32_f(MAC_MODE, tp->mac_mode);
  4380. udelay(40);
  4381. ap->state = ANEG_STATE_IDLE_DETECT;
  4382. ret = ANEG_TIMER_ENAB;
  4383. break;
  4384. case ANEG_STATE_IDLE_DETECT:
  4385. if (ap->ability_match != 0 &&
  4386. ap->rxconfig == 0) {
  4387. ap->state = ANEG_STATE_AN_ENABLE;
  4388. break;
  4389. }
  4390. delta = ap->cur_time - ap->link_time;
  4391. if (delta > ANEG_STATE_SETTLE_TIME) {
  4392. /* XXX another gem from the Broadcom driver :( */
  4393. ap->state = ANEG_STATE_LINK_OK;
  4394. }
  4395. break;
  4396. case ANEG_STATE_LINK_OK:
  4397. ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
  4398. ret = ANEG_DONE;
  4399. break;
  4400. case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
  4401. /* ??? unimplemented */
  4402. break;
  4403. case ANEG_STATE_NEXT_PAGE_WAIT:
  4404. /* ??? unimplemented */
  4405. break;
  4406. default:
  4407. ret = ANEG_FAILED;
  4408. break;
  4409. }
  4410. return ret;
  4411. }
  4412. static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
  4413. {
  4414. int res = 0;
  4415. struct tg3_fiber_aneginfo aninfo;
  4416. int status = ANEG_FAILED;
  4417. unsigned int tick;
  4418. u32 tmp;
  4419. tw32_f(MAC_TX_AUTO_NEG, 0);
  4420. tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
  4421. tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
  4422. udelay(40);
  4423. tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
  4424. udelay(40);
  4425. memset(&aninfo, 0, sizeof(aninfo));
  4426. aninfo.flags |= MR_AN_ENABLE;
  4427. aninfo.state = ANEG_STATE_UNKNOWN;
  4428. aninfo.cur_time = 0;
  4429. tick = 0;
  4430. while (++tick < 195000) {
  4431. status = tg3_fiber_aneg_smachine(tp, &aninfo);
  4432. if (status == ANEG_DONE || status == ANEG_FAILED)
  4433. break;
  4434. udelay(1);
  4435. }
  4436. tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
  4437. tw32_f(MAC_MODE, tp->mac_mode);
  4438. udelay(40);
  4439. *txflags = aninfo.txconfig;
  4440. *rxflags = aninfo.flags;
  4441. if (status == ANEG_DONE &&
  4442. (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
  4443. MR_LP_ADV_FULL_DUPLEX)))
  4444. res = 1;
  4445. return res;
  4446. }
  4447. static void tg3_init_bcm8002(struct tg3 *tp)
  4448. {
  4449. u32 mac_status = tr32(MAC_STATUS);
  4450. int i;
  4451. /* Reset when initting first time or we have a link. */
  4452. if (tg3_flag(tp, INIT_COMPLETE) &&
  4453. !(mac_status & MAC_STATUS_PCS_SYNCED))
  4454. return;
  4455. /* Set PLL lock range. */
  4456. tg3_writephy(tp, 0x16, 0x8007);
  4457. /* SW reset */
  4458. tg3_writephy(tp, MII_BMCR, BMCR_RESET);
  4459. /* Wait for reset to complete. */
  4460. /* XXX schedule_timeout() ... */
  4461. for (i = 0; i < 500; i++)
  4462. udelay(10);
  4463. /* Config mode; select PMA/Ch 1 regs. */
  4464. tg3_writephy(tp, 0x10, 0x8411);
  4465. /* Enable auto-lock and comdet, select txclk for tx. */
  4466. tg3_writephy(tp, 0x11, 0x0a10);
  4467. tg3_writephy(tp, 0x18, 0x00a0);
  4468. tg3_writephy(tp, 0x16, 0x41ff);
  4469. /* Assert and deassert POR. */
  4470. tg3_writephy(tp, 0x13, 0x0400);
  4471. udelay(40);
  4472. tg3_writephy(tp, 0x13, 0x0000);
  4473. tg3_writephy(tp, 0x11, 0x0a50);
  4474. udelay(40);
  4475. tg3_writephy(tp, 0x11, 0x0a10);
  4476. /* Wait for signal to stabilize */
  4477. /* XXX schedule_timeout() ... */
  4478. for (i = 0; i < 15000; i++)
  4479. udelay(10);
  4480. /* Deselect the channel register so we can read the PHYID
  4481. * later.
  4482. */
  4483. tg3_writephy(tp, 0x10, 0x8011);
  4484. }
  4485. static bool tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
  4486. {
  4487. u16 flowctrl;
  4488. bool current_link_up;
  4489. u32 sg_dig_ctrl, sg_dig_status;
  4490. u32 serdes_cfg, expected_sg_dig_ctrl;
  4491. int workaround, port_a;
  4492. serdes_cfg = 0;
  4493. expected_sg_dig_ctrl = 0;
  4494. workaround = 0;
  4495. port_a = 1;
  4496. current_link_up = false;
  4497. if (tg3_chip_rev_id(tp) != CHIPREV_ID_5704_A0 &&
  4498. tg3_chip_rev_id(tp) != CHIPREV_ID_5704_A1) {
  4499. workaround = 1;
  4500. if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
  4501. port_a = 0;
  4502. /* preserve bits 0-11,13,14 for signal pre-emphasis */
  4503. /* preserve bits 20-23 for voltage regulator */
  4504. serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
  4505. }
  4506. sg_dig_ctrl = tr32(SG_DIG_CTRL);
  4507. if (tp->link_config.autoneg != AUTONEG_ENABLE) {
  4508. if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
  4509. if (workaround) {
  4510. u32 val = serdes_cfg;
  4511. if (port_a)
  4512. val |= 0xc010000;
  4513. else
  4514. val |= 0x4010000;
  4515. tw32_f(MAC_SERDES_CFG, val);
  4516. }
  4517. tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
  4518. }
  4519. if (mac_status & MAC_STATUS_PCS_SYNCED) {
  4520. tg3_setup_flow_control(tp, 0, 0);
  4521. current_link_up = true;
  4522. }
  4523. goto out;
  4524. }
  4525. /* Want auto-negotiation. */
  4526. expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
  4527. flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
  4528. if (flowctrl & ADVERTISE_1000XPAUSE)
  4529. expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
  4530. if (flowctrl & ADVERTISE_1000XPSE_ASYM)
  4531. expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
  4532. if (sg_dig_ctrl != expected_sg_dig_ctrl) {
  4533. if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
  4534. tp->serdes_counter &&
  4535. ((mac_status & (MAC_STATUS_PCS_SYNCED |
  4536. MAC_STATUS_RCVD_CFG)) ==
  4537. MAC_STATUS_PCS_SYNCED)) {
  4538. tp->serdes_counter--;
  4539. current_link_up = true;
  4540. goto out;
  4541. }
  4542. restart_autoneg:
  4543. if (workaround)
  4544. tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
  4545. tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
  4546. udelay(5);
  4547. tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
  4548. tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
  4549. tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
  4550. } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
  4551. MAC_STATUS_SIGNAL_DET)) {
  4552. sg_dig_status = tr32(SG_DIG_STATUS);
  4553. mac_status = tr32(MAC_STATUS);
  4554. if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
  4555. (mac_status & MAC_STATUS_PCS_SYNCED)) {
  4556. u32 local_adv = 0, remote_adv = 0;
  4557. if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
  4558. local_adv |= ADVERTISE_1000XPAUSE;
  4559. if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
  4560. local_adv |= ADVERTISE_1000XPSE_ASYM;
  4561. if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
  4562. remote_adv |= LPA_1000XPAUSE;
  4563. if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
  4564. remote_adv |= LPA_1000XPAUSE_ASYM;
  4565. tp->link_config.rmt_adv =
  4566. mii_adv_to_ethtool_adv_x(remote_adv);
  4567. tg3_setup_flow_control(tp, local_adv, remote_adv);
  4568. current_link_up = true;
  4569. tp->serdes_counter = 0;
  4570. tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
  4571. } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
  4572. if (tp->serdes_counter)
  4573. tp->serdes_counter--;
  4574. else {
  4575. if (workaround) {
  4576. u32 val = serdes_cfg;
  4577. if (port_a)
  4578. val |= 0xc010000;
  4579. else
  4580. val |= 0x4010000;
  4581. tw32_f(MAC_SERDES_CFG, val);
  4582. }
  4583. tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
  4584. udelay(40);
  4585. /* Link parallel detection - link is up */
  4586. /* only if we have PCS_SYNC and not */
  4587. /* receiving config code words */
  4588. mac_status = tr32(MAC_STATUS);
  4589. if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
  4590. !(mac_status & MAC_STATUS_RCVD_CFG)) {
  4591. tg3_setup_flow_control(tp, 0, 0);
  4592. current_link_up = true;
  4593. tp->phy_flags |=
  4594. TG3_PHYFLG_PARALLEL_DETECT;
  4595. tp->serdes_counter =
  4596. SERDES_PARALLEL_DET_TIMEOUT;
  4597. } else
  4598. goto restart_autoneg;
  4599. }
  4600. }
  4601. } else {
  4602. tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
  4603. tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
  4604. }
  4605. out:
  4606. return current_link_up;
  4607. }
  4608. static bool tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
  4609. {
  4610. bool current_link_up = false;
  4611. if (!(mac_status & MAC_STATUS_PCS_SYNCED))
  4612. goto out;
  4613. if (tp->link_config.autoneg == AUTONEG_ENABLE) {
  4614. u32 txflags, rxflags;
  4615. int i;
  4616. if (fiber_autoneg(tp, &txflags, &rxflags)) {
  4617. u32 local_adv = 0, remote_adv = 0;
  4618. if (txflags & ANEG_CFG_PS1)
  4619. local_adv |= ADVERTISE_1000XPAUSE;
  4620. if (txflags & ANEG_CFG_PS2)
  4621. local_adv |= ADVERTISE_1000XPSE_ASYM;
  4622. if (rxflags & MR_LP_ADV_SYM_PAUSE)
  4623. remote_adv |= LPA_1000XPAUSE;
  4624. if (rxflags & MR_LP_ADV_ASYM_PAUSE)
  4625. remote_adv |= LPA_1000XPAUSE_ASYM;
  4626. tp->link_config.rmt_adv =
  4627. mii_adv_to_ethtool_adv_x(remote_adv);
  4628. tg3_setup_flow_control(tp, local_adv, remote_adv);
  4629. current_link_up = true;
  4630. }
  4631. for (i = 0; i < 30; i++) {
  4632. udelay(20);
  4633. tw32_f(MAC_STATUS,
  4634. (MAC_STATUS_SYNC_CHANGED |
  4635. MAC_STATUS_CFG_CHANGED));
  4636. udelay(40);
  4637. if ((tr32(MAC_STATUS) &
  4638. (MAC_STATUS_SYNC_CHANGED |
  4639. MAC_STATUS_CFG_CHANGED)) == 0)
  4640. break;
  4641. }
  4642. mac_status = tr32(MAC_STATUS);
  4643. if (!current_link_up &&
  4644. (mac_status & MAC_STATUS_PCS_SYNCED) &&
  4645. !(mac_status & MAC_STATUS_RCVD_CFG))
  4646. current_link_up = true;
  4647. } else {
  4648. tg3_setup_flow_control(tp, 0, 0);
  4649. /* Forcing 1000FD link up. */
  4650. current_link_up = true;
  4651. tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
  4652. udelay(40);
  4653. tw32_f(MAC_MODE, tp->mac_mode);
  4654. udelay(40);
  4655. }
  4656. out:
  4657. return current_link_up;
  4658. }
  4659. static int tg3_setup_fiber_phy(struct tg3 *tp, bool force_reset)
  4660. {
  4661. u32 orig_pause_cfg;
  4662. u16 orig_active_speed;
  4663. u8 orig_active_duplex;
  4664. u32 mac_status;
  4665. bool current_link_up;
  4666. int i;
  4667. orig_pause_cfg = tp->link_config.active_flowctrl;
  4668. orig_active_speed = tp->link_config.active_speed;
  4669. orig_active_duplex = tp->link_config.active_duplex;
  4670. if (!tg3_flag(tp, HW_AUTONEG) &&
  4671. tp->link_up &&
  4672. tg3_flag(tp, INIT_COMPLETE)) {
  4673. mac_status = tr32(MAC_STATUS);
  4674. mac_status &= (MAC_STATUS_PCS_SYNCED |
  4675. MAC_STATUS_SIGNAL_DET |
  4676. MAC_STATUS_CFG_CHANGED |
  4677. MAC_STATUS_RCVD_CFG);
  4678. if (mac_status == (MAC_STATUS_PCS_SYNCED |
  4679. MAC_STATUS_SIGNAL_DET)) {
  4680. tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
  4681. MAC_STATUS_CFG_CHANGED));
  4682. return 0;
  4683. }
  4684. }
  4685. tw32_f(MAC_TX_AUTO_NEG, 0);
  4686. tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
  4687. tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
  4688. tw32_f(MAC_MODE, tp->mac_mode);
  4689. udelay(40);
  4690. if (tp->phy_id == TG3_PHY_ID_BCM8002)
  4691. tg3_init_bcm8002(tp);
  4692. /* Enable link change event even when serdes polling. */
  4693. tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
  4694. udelay(40);
  4695. current_link_up = false;
  4696. tp->link_config.rmt_adv = 0;
  4697. mac_status = tr32(MAC_STATUS);
  4698. if (tg3_flag(tp, HW_AUTONEG))
  4699. current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
  4700. else
  4701. current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
  4702. tp->napi[0].hw_status->status =
  4703. (SD_STATUS_UPDATED |
  4704. (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
  4705. for (i = 0; i < 100; i++) {
  4706. tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
  4707. MAC_STATUS_CFG_CHANGED));
  4708. udelay(5);
  4709. if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
  4710. MAC_STATUS_CFG_CHANGED |
  4711. MAC_STATUS_LNKSTATE_CHANGED)) == 0)
  4712. break;
  4713. }
  4714. mac_status = tr32(MAC_STATUS);
  4715. if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
  4716. current_link_up = false;
  4717. if (tp->link_config.autoneg == AUTONEG_ENABLE &&
  4718. tp->serdes_counter == 0) {
  4719. tw32_f(MAC_MODE, (tp->mac_mode |
  4720. MAC_MODE_SEND_CONFIGS));
  4721. udelay(1);
  4722. tw32_f(MAC_MODE, tp->mac_mode);
  4723. }
  4724. }
  4725. if (current_link_up) {
  4726. tp->link_config.active_speed = SPEED_1000;
  4727. tp->link_config.active_duplex = DUPLEX_FULL;
  4728. tw32(MAC_LED_CTRL, (tp->led_ctrl |
  4729. LED_CTRL_LNKLED_OVERRIDE |
  4730. LED_CTRL_1000MBPS_ON));
  4731. } else {
  4732. tp->link_config.active_speed = SPEED_UNKNOWN;
  4733. tp->link_config.active_duplex = DUPLEX_UNKNOWN;
  4734. tw32(MAC_LED_CTRL, (tp->led_ctrl |
  4735. LED_CTRL_LNKLED_OVERRIDE |
  4736. LED_CTRL_TRAFFIC_OVERRIDE));
  4737. }
  4738. if (!tg3_test_and_report_link_chg(tp, current_link_up)) {
  4739. u32 now_pause_cfg = tp->link_config.active_flowctrl;
  4740. if (orig_pause_cfg != now_pause_cfg ||
  4741. orig_active_speed != tp->link_config.active_speed ||
  4742. orig_active_duplex != tp->link_config.active_duplex)
  4743. tg3_link_report(tp);
  4744. }
  4745. return 0;
  4746. }
  4747. static int tg3_setup_fiber_mii_phy(struct tg3 *tp, bool force_reset)
  4748. {
  4749. int err = 0;
  4750. u32 bmsr, bmcr;
  4751. u16 current_speed = SPEED_UNKNOWN;
  4752. u8 current_duplex = DUPLEX_UNKNOWN;
  4753. bool current_link_up = false;
  4754. u32 local_adv, remote_adv, sgsr;
  4755. if ((tg3_asic_rev(tp) == ASIC_REV_5719 ||
  4756. tg3_asic_rev(tp) == ASIC_REV_5720) &&
  4757. !tg3_readphy(tp, SERDES_TG3_1000X_STATUS, &sgsr) &&
  4758. (sgsr & SERDES_TG3_SGMII_MODE)) {
  4759. if (force_reset)
  4760. tg3_phy_reset(tp);
  4761. tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
  4762. if (!(sgsr & SERDES_TG3_LINK_UP)) {
  4763. tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
  4764. } else {
  4765. current_link_up = true;
  4766. if (sgsr & SERDES_TG3_SPEED_1000) {
  4767. current_speed = SPEED_1000;
  4768. tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
  4769. } else if (sgsr & SERDES_TG3_SPEED_100) {
  4770. current_speed = SPEED_100;
  4771. tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
  4772. } else {
  4773. current_speed = SPEED_10;
  4774. tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
  4775. }
  4776. if (sgsr & SERDES_TG3_FULL_DUPLEX)
  4777. current_duplex = DUPLEX_FULL;
  4778. else
  4779. current_duplex = DUPLEX_HALF;
  4780. }
  4781. tw32_f(MAC_MODE, tp->mac_mode);
  4782. udelay(40);
  4783. tg3_clear_mac_status(tp);
  4784. goto fiber_setup_done;
  4785. }
  4786. tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
  4787. tw32_f(MAC_MODE, tp->mac_mode);
  4788. udelay(40);
  4789. tg3_clear_mac_status(tp);
  4790. if (force_reset)
  4791. tg3_phy_reset(tp);
  4792. tp->link_config.rmt_adv = 0;
  4793. err |= tg3_readphy(tp, MII_BMSR, &bmsr);
  4794. err |= tg3_readphy(tp, MII_BMSR, &bmsr);
  4795. if (tg3_asic_rev(tp) == ASIC_REV_5714) {
  4796. if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
  4797. bmsr |= BMSR_LSTATUS;
  4798. else
  4799. bmsr &= ~BMSR_LSTATUS;
  4800. }
  4801. err |= tg3_readphy(tp, MII_BMCR, &bmcr);
  4802. if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
  4803. (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
  4804. /* do nothing, just check for link up at the end */
  4805. } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
  4806. u32 adv, newadv;
  4807. err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
  4808. newadv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
  4809. ADVERTISE_1000XPAUSE |
  4810. ADVERTISE_1000XPSE_ASYM |
  4811. ADVERTISE_SLCT);
  4812. newadv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
  4813. newadv |= ethtool_adv_to_mii_adv_x(tp->link_config.advertising);
  4814. if ((newadv != adv) || !(bmcr & BMCR_ANENABLE)) {
  4815. tg3_writephy(tp, MII_ADVERTISE, newadv);
  4816. bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
  4817. tg3_writephy(tp, MII_BMCR, bmcr);
  4818. tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
  4819. tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
  4820. tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
  4821. return err;
  4822. }
  4823. } else {
  4824. u32 new_bmcr;
  4825. bmcr &= ~BMCR_SPEED1000;
  4826. new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
  4827. if (tp->link_config.duplex == DUPLEX_FULL)
  4828. new_bmcr |= BMCR_FULLDPLX;
  4829. if (new_bmcr != bmcr) {
  4830. /* BMCR_SPEED1000 is a reserved bit that needs
  4831. * to be set on write.
  4832. */
  4833. new_bmcr |= BMCR_SPEED1000;
  4834. /* Force a linkdown */
  4835. if (tp->link_up) {
  4836. u32 adv;
  4837. err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
  4838. adv &= ~(ADVERTISE_1000XFULL |
  4839. ADVERTISE_1000XHALF |
  4840. ADVERTISE_SLCT);
  4841. tg3_writephy(tp, MII_ADVERTISE, adv);
  4842. tg3_writephy(tp, MII_BMCR, bmcr |
  4843. BMCR_ANRESTART |
  4844. BMCR_ANENABLE);
  4845. udelay(10);
  4846. tg3_carrier_off(tp);
  4847. }
  4848. tg3_writephy(tp, MII_BMCR, new_bmcr);
  4849. bmcr = new_bmcr;
  4850. err |= tg3_readphy(tp, MII_BMSR, &bmsr);
  4851. err |= tg3_readphy(tp, MII_BMSR, &bmsr);
  4852. if (tg3_asic_rev(tp) == ASIC_REV_5714) {
  4853. if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
  4854. bmsr |= BMSR_LSTATUS;
  4855. else
  4856. bmsr &= ~BMSR_LSTATUS;
  4857. }
  4858. tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
  4859. }
  4860. }
  4861. if (bmsr & BMSR_LSTATUS) {
  4862. current_speed = SPEED_1000;
  4863. current_link_up = true;
  4864. if (bmcr & BMCR_FULLDPLX)
  4865. current_duplex = DUPLEX_FULL;
  4866. else
  4867. current_duplex = DUPLEX_HALF;
  4868. local_adv = 0;
  4869. remote_adv = 0;
  4870. if (bmcr & BMCR_ANENABLE) {
  4871. u32 common;
  4872. err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
  4873. err |= tg3_readphy(tp, MII_LPA, &remote_adv);
  4874. common = local_adv & remote_adv;
  4875. if (common & (ADVERTISE_1000XHALF |
  4876. ADVERTISE_1000XFULL)) {
  4877. if (common & ADVERTISE_1000XFULL)
  4878. current_duplex = DUPLEX_FULL;
  4879. else
  4880. current_duplex = DUPLEX_HALF;
  4881. tp->link_config.rmt_adv =
  4882. mii_adv_to_ethtool_adv_x(remote_adv);
  4883. } else if (!tg3_flag(tp, 5780_CLASS)) {
  4884. /* Link is up via parallel detect */
  4885. } else {
  4886. current_link_up = false;
  4887. }
  4888. }
  4889. }
  4890. fiber_setup_done:
  4891. if (current_link_up && current_duplex == DUPLEX_FULL)
  4892. tg3_setup_flow_control(tp, local_adv, remote_adv);
  4893. tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
  4894. if (tp->link_config.active_duplex == DUPLEX_HALF)
  4895. tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
  4896. tw32_f(MAC_MODE, tp->mac_mode);
  4897. udelay(40);
  4898. tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
  4899. tp->link_config.active_speed = current_speed;
  4900. tp->link_config.active_duplex = current_duplex;
  4901. tg3_test_and_report_link_chg(tp, current_link_up);
  4902. return err;
  4903. }
  4904. static void tg3_serdes_parallel_detect(struct tg3 *tp)
  4905. {
  4906. if (tp->serdes_counter) {
  4907. /* Give autoneg time to complete. */
  4908. tp->serdes_counter--;
  4909. return;
  4910. }
  4911. if (!tp->link_up &&
  4912. (tp->link_config.autoneg == AUTONEG_ENABLE)) {
  4913. u32 bmcr;
  4914. tg3_readphy(tp, MII_BMCR, &bmcr);
  4915. if (bmcr & BMCR_ANENABLE) {
  4916. u32 phy1, phy2;
  4917. /* Select shadow register 0x1f */
  4918. tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
  4919. tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
  4920. /* Select expansion interrupt status register */
  4921. tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
  4922. MII_TG3_DSP_EXP1_INT_STAT);
  4923. tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
  4924. tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
  4925. if ((phy1 & 0x10) && !(phy2 & 0x20)) {
  4926. /* We have signal detect and not receiving
  4927. * config code words, link is up by parallel
  4928. * detection.
  4929. */
  4930. bmcr &= ~BMCR_ANENABLE;
  4931. bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
  4932. tg3_writephy(tp, MII_BMCR, bmcr);
  4933. tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
  4934. }
  4935. }
  4936. } else if (tp->link_up &&
  4937. (tp->link_config.autoneg == AUTONEG_ENABLE) &&
  4938. (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
  4939. u32 phy2;
  4940. /* Select expansion interrupt status register */
  4941. tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
  4942. MII_TG3_DSP_EXP1_INT_STAT);
  4943. tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
  4944. if (phy2 & 0x20) {
  4945. u32 bmcr;
  4946. /* Config code words received, turn on autoneg. */
  4947. tg3_readphy(tp, MII_BMCR, &bmcr);
  4948. tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
  4949. tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
  4950. }
  4951. }
  4952. }
  4953. static int tg3_setup_phy(struct tg3 *tp, bool force_reset)
  4954. {
  4955. u32 val;
  4956. int err;
  4957. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
  4958. err = tg3_setup_fiber_phy(tp, force_reset);
  4959. else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
  4960. err = tg3_setup_fiber_mii_phy(tp, force_reset);
  4961. else
  4962. err = tg3_setup_copper_phy(tp, force_reset);
  4963. if (tg3_chip_rev(tp) == CHIPREV_5784_AX) {
  4964. u32 scale;
  4965. val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
  4966. if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
  4967. scale = 65;
  4968. else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
  4969. scale = 6;
  4970. else
  4971. scale = 12;
  4972. val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
  4973. val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
  4974. tw32(GRC_MISC_CFG, val);
  4975. }
  4976. val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
  4977. (6 << TX_LENGTHS_IPG_SHIFT);
  4978. if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
  4979. tg3_asic_rev(tp) == ASIC_REV_5762)
  4980. val |= tr32(MAC_TX_LENGTHS) &
  4981. (TX_LENGTHS_JMB_FRM_LEN_MSK |
  4982. TX_LENGTHS_CNT_DWN_VAL_MSK);
  4983. if (tp->link_config.active_speed == SPEED_1000 &&
  4984. tp->link_config.active_duplex == DUPLEX_HALF)
  4985. tw32(MAC_TX_LENGTHS, val |
  4986. (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
  4987. else
  4988. tw32(MAC_TX_LENGTHS, val |
  4989. (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
  4990. if (!tg3_flag(tp, 5705_PLUS)) {
  4991. if (tp->link_up) {
  4992. tw32(HOSTCC_STAT_COAL_TICKS,
  4993. tp->coal.stats_block_coalesce_usecs);
  4994. } else {
  4995. tw32(HOSTCC_STAT_COAL_TICKS, 0);
  4996. }
  4997. }
  4998. if (tg3_flag(tp, ASPM_WORKAROUND)) {
  4999. val = tr32(PCIE_PWR_MGMT_THRESH);
  5000. if (!tp->link_up)
  5001. val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
  5002. tp->pwrmgmt_thresh;
  5003. else
  5004. val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
  5005. tw32(PCIE_PWR_MGMT_THRESH, val);
  5006. }
  5007. return err;
  5008. }
  5009. /* tp->lock must be held */
  5010. static u64 tg3_refclk_read(struct tg3 *tp)
  5011. {
  5012. u64 stamp = tr32(TG3_EAV_REF_CLCK_LSB);
  5013. return stamp | (u64)tr32(TG3_EAV_REF_CLCK_MSB) << 32;
  5014. }
  5015. /* tp->lock must be held */
  5016. static void tg3_refclk_write(struct tg3 *tp, u64 newval)
  5017. {
  5018. u32 clock_ctl = tr32(TG3_EAV_REF_CLCK_CTL);
  5019. tw32(TG3_EAV_REF_CLCK_CTL, clock_ctl | TG3_EAV_REF_CLCK_CTL_STOP);
  5020. tw32(TG3_EAV_REF_CLCK_LSB, newval & 0xffffffff);
  5021. tw32(TG3_EAV_REF_CLCK_MSB, newval >> 32);
  5022. tw32_f(TG3_EAV_REF_CLCK_CTL, clock_ctl | TG3_EAV_REF_CLCK_CTL_RESUME);
  5023. }
  5024. static inline void tg3_full_lock(struct tg3 *tp, int irq_sync);
  5025. static inline void tg3_full_unlock(struct tg3 *tp);
  5026. static int tg3_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info)
  5027. {
  5028. struct tg3 *tp = netdev_priv(dev);
  5029. info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
  5030. SOF_TIMESTAMPING_RX_SOFTWARE |
  5031. SOF_TIMESTAMPING_SOFTWARE;
  5032. if (tg3_flag(tp, PTP_CAPABLE)) {
  5033. info->so_timestamping |= SOF_TIMESTAMPING_TX_HARDWARE |
  5034. SOF_TIMESTAMPING_RX_HARDWARE |
  5035. SOF_TIMESTAMPING_RAW_HARDWARE;
  5036. }
  5037. if (tp->ptp_clock)
  5038. info->phc_index = ptp_clock_index(tp->ptp_clock);
  5039. else
  5040. info->phc_index = -1;
  5041. info->tx_types = (1 << HWTSTAMP_TX_OFF) | (1 << HWTSTAMP_TX_ON);
  5042. info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
  5043. (1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT) |
  5044. (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
  5045. (1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT);
  5046. return 0;
  5047. }
  5048. static int tg3_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
  5049. {
  5050. struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
  5051. bool neg_adj = false;
  5052. u32 correction = 0;
  5053. if (ppb < 0) {
  5054. neg_adj = true;
  5055. ppb = -ppb;
  5056. }
  5057. /* Frequency adjustment is performed using hardware with a 24 bit
  5058. * accumulator and a programmable correction value. On each clk, the
  5059. * correction value gets added to the accumulator and when it
  5060. * overflows, the time counter is incremented/decremented.
  5061. *
  5062. * So conversion from ppb to correction value is
  5063. * ppb * (1 << 24) / 1000000000
  5064. */
  5065. correction = div_u64((u64)ppb * (1 << 24), 1000000000ULL) &
  5066. TG3_EAV_REF_CLK_CORRECT_MASK;
  5067. tg3_full_lock(tp, 0);
  5068. if (correction)
  5069. tw32(TG3_EAV_REF_CLK_CORRECT_CTL,
  5070. TG3_EAV_REF_CLK_CORRECT_EN |
  5071. (neg_adj ? TG3_EAV_REF_CLK_CORRECT_NEG : 0) | correction);
  5072. else
  5073. tw32(TG3_EAV_REF_CLK_CORRECT_CTL, 0);
  5074. tg3_full_unlock(tp);
  5075. return 0;
  5076. }
  5077. static int tg3_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
  5078. {
  5079. struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
  5080. tg3_full_lock(tp, 0);
  5081. tp->ptp_adjust += delta;
  5082. tg3_full_unlock(tp);
  5083. return 0;
  5084. }
  5085. static int tg3_ptp_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
  5086. {
  5087. u64 ns;
  5088. struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
  5089. tg3_full_lock(tp, 0);
  5090. ns = tg3_refclk_read(tp);
  5091. ns += tp->ptp_adjust;
  5092. tg3_full_unlock(tp);
  5093. *ts = ns_to_timespec64(ns);
  5094. return 0;
  5095. }
  5096. static int tg3_ptp_settime(struct ptp_clock_info *ptp,
  5097. const struct timespec64 *ts)
  5098. {
  5099. u64 ns;
  5100. struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
  5101. ns = timespec64_to_ns(ts);
  5102. tg3_full_lock(tp, 0);
  5103. tg3_refclk_write(tp, ns);
  5104. tp->ptp_adjust = 0;
  5105. tg3_full_unlock(tp);
  5106. return 0;
  5107. }
  5108. static int tg3_ptp_enable(struct ptp_clock_info *ptp,
  5109. struct ptp_clock_request *rq, int on)
  5110. {
  5111. struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
  5112. u32 clock_ctl;
  5113. int rval = 0;
  5114. switch (rq->type) {
  5115. case PTP_CLK_REQ_PEROUT:
  5116. if (rq->perout.index != 0)
  5117. return -EINVAL;
  5118. tg3_full_lock(tp, 0);
  5119. clock_ctl = tr32(TG3_EAV_REF_CLCK_CTL);
  5120. clock_ctl &= ~TG3_EAV_CTL_TSYNC_GPIO_MASK;
  5121. if (on) {
  5122. u64 nsec;
  5123. nsec = rq->perout.start.sec * 1000000000ULL +
  5124. rq->perout.start.nsec;
  5125. if (rq->perout.period.sec || rq->perout.period.nsec) {
  5126. netdev_warn(tp->dev,
  5127. "Device supports only a one-shot timesync output, period must be 0\n");
  5128. rval = -EINVAL;
  5129. goto err_out;
  5130. }
  5131. if (nsec & (1ULL << 63)) {
  5132. netdev_warn(tp->dev,
  5133. "Start value (nsec) is over limit. Maximum size of start is only 63 bits\n");
  5134. rval = -EINVAL;
  5135. goto err_out;
  5136. }
  5137. tw32(TG3_EAV_WATCHDOG0_LSB, (nsec & 0xffffffff));
  5138. tw32(TG3_EAV_WATCHDOG0_MSB,
  5139. TG3_EAV_WATCHDOG0_EN |
  5140. ((nsec >> 32) & TG3_EAV_WATCHDOG_MSB_MASK));
  5141. tw32(TG3_EAV_REF_CLCK_CTL,
  5142. clock_ctl | TG3_EAV_CTL_TSYNC_WDOG0);
  5143. } else {
  5144. tw32(TG3_EAV_WATCHDOG0_MSB, 0);
  5145. tw32(TG3_EAV_REF_CLCK_CTL, clock_ctl);
  5146. }
  5147. err_out:
  5148. tg3_full_unlock(tp);
  5149. return rval;
  5150. default:
  5151. break;
  5152. }
  5153. return -EOPNOTSUPP;
  5154. }
  5155. static const struct ptp_clock_info tg3_ptp_caps = {
  5156. .owner = THIS_MODULE,
  5157. .name = "tg3 clock",
  5158. .max_adj = 250000000,
  5159. .n_alarm = 0,
  5160. .n_ext_ts = 0,
  5161. .n_per_out = 1,
  5162. .n_pins = 0,
  5163. .pps = 0,
  5164. .adjfreq = tg3_ptp_adjfreq,
  5165. .adjtime = tg3_ptp_adjtime,
  5166. .gettime64 = tg3_ptp_gettime,
  5167. .settime64 = tg3_ptp_settime,
  5168. .enable = tg3_ptp_enable,
  5169. };
  5170. static void tg3_hwclock_to_timestamp(struct tg3 *tp, u64 hwclock,
  5171. struct skb_shared_hwtstamps *timestamp)
  5172. {
  5173. memset(timestamp, 0, sizeof(struct skb_shared_hwtstamps));
  5174. timestamp->hwtstamp = ns_to_ktime((hwclock & TG3_TSTAMP_MASK) +
  5175. tp->ptp_adjust);
  5176. }
  5177. /* tp->lock must be held */
  5178. static void tg3_ptp_init(struct tg3 *tp)
  5179. {
  5180. if (!tg3_flag(tp, PTP_CAPABLE))
  5181. return;
  5182. /* Initialize the hardware clock to the system time. */
  5183. tg3_refclk_write(tp, ktime_to_ns(ktime_get_real()));
  5184. tp->ptp_adjust = 0;
  5185. tp->ptp_info = tg3_ptp_caps;
  5186. }
  5187. /* tp->lock must be held */
  5188. static void tg3_ptp_resume(struct tg3 *tp)
  5189. {
  5190. if (!tg3_flag(tp, PTP_CAPABLE))
  5191. return;
  5192. tg3_refclk_write(tp, ktime_to_ns(ktime_get_real()) + tp->ptp_adjust);
  5193. tp->ptp_adjust = 0;
  5194. }
  5195. static void tg3_ptp_fini(struct tg3 *tp)
  5196. {
  5197. if (!tg3_flag(tp, PTP_CAPABLE) || !tp->ptp_clock)
  5198. return;
  5199. ptp_clock_unregister(tp->ptp_clock);
  5200. tp->ptp_clock = NULL;
  5201. tp->ptp_adjust = 0;
  5202. }
  5203. static inline int tg3_irq_sync(struct tg3 *tp)
  5204. {
  5205. return tp->irq_sync;
  5206. }
  5207. static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
  5208. {
  5209. int i;
  5210. dst = (u32 *)((u8 *)dst + off);
  5211. for (i = 0; i < len; i += sizeof(u32))
  5212. *dst++ = tr32(off + i);
  5213. }
  5214. static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
  5215. {
  5216. tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
  5217. tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
  5218. tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
  5219. tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
  5220. tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
  5221. tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
  5222. tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
  5223. tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
  5224. tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
  5225. tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
  5226. tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
  5227. tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
  5228. tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
  5229. tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
  5230. tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
  5231. tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
  5232. tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
  5233. tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
  5234. tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
  5235. if (tg3_flag(tp, SUPPORT_MSIX))
  5236. tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
  5237. tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
  5238. tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
  5239. tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
  5240. tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
  5241. tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
  5242. tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
  5243. tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
  5244. tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
  5245. if (!tg3_flag(tp, 5705_PLUS)) {
  5246. tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
  5247. tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
  5248. tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
  5249. }
  5250. tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
  5251. tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
  5252. tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
  5253. tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
  5254. tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
  5255. if (tg3_flag(tp, NVRAM))
  5256. tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
  5257. }
  5258. static void tg3_dump_state(struct tg3 *tp)
  5259. {
  5260. int i;
  5261. u32 *regs;
  5262. regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
  5263. if (!regs)
  5264. return;
  5265. if (tg3_flag(tp, PCI_EXPRESS)) {
  5266. /* Read up to but not including private PCI registers */
  5267. for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
  5268. regs[i / sizeof(u32)] = tr32(i);
  5269. } else
  5270. tg3_dump_legacy_regs(tp, regs);
  5271. for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
  5272. if (!regs[i + 0] && !regs[i + 1] &&
  5273. !regs[i + 2] && !regs[i + 3])
  5274. continue;
  5275. netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
  5276. i * 4,
  5277. regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
  5278. }
  5279. kfree(regs);
  5280. for (i = 0; i < tp->irq_cnt; i++) {
  5281. struct tg3_napi *tnapi = &tp->napi[i];
  5282. /* SW status block */
  5283. netdev_err(tp->dev,
  5284. "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
  5285. i,
  5286. tnapi->hw_status->status,
  5287. tnapi->hw_status->status_tag,
  5288. tnapi->hw_status->rx_jumbo_consumer,
  5289. tnapi->hw_status->rx_consumer,
  5290. tnapi->hw_status->rx_mini_consumer,
  5291. tnapi->hw_status->idx[0].rx_producer,
  5292. tnapi->hw_status->idx[0].tx_consumer);
  5293. netdev_err(tp->dev,
  5294. "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
  5295. i,
  5296. tnapi->last_tag, tnapi->last_irq_tag,
  5297. tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
  5298. tnapi->rx_rcb_ptr,
  5299. tnapi->prodring.rx_std_prod_idx,
  5300. tnapi->prodring.rx_std_cons_idx,
  5301. tnapi->prodring.rx_jmb_prod_idx,
  5302. tnapi->prodring.rx_jmb_cons_idx);
  5303. }
  5304. }
  5305. /* This is called whenever we suspect that the system chipset is re-
  5306. * ordering the sequence of MMIO to the tx send mailbox. The symptom
  5307. * is bogus tx completions. We try to recover by setting the
  5308. * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
  5309. * in the workqueue.
  5310. */
  5311. static void tg3_tx_recover(struct tg3 *tp)
  5312. {
  5313. BUG_ON(tg3_flag(tp, MBOX_WRITE_REORDER) ||
  5314. tp->write32_tx_mbox == tg3_write_indirect_mbox);
  5315. netdev_warn(tp->dev,
  5316. "The system may be re-ordering memory-mapped I/O "
  5317. "cycles to the network device, attempting to recover. "
  5318. "Please report the problem to the driver maintainer "
  5319. "and include system chipset information.\n");
  5320. tg3_flag_set(tp, TX_RECOVERY_PENDING);
  5321. }
  5322. static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
  5323. {
  5324. /* Tell compiler to fetch tx indices from memory. */
  5325. barrier();
  5326. return tnapi->tx_pending -
  5327. ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
  5328. }
  5329. /* Tigon3 never reports partial packet sends. So we do not
  5330. * need special logic to handle SKBs that have not had all
  5331. * of their frags sent yet, like SunGEM does.
  5332. */
  5333. static void tg3_tx(struct tg3_napi *tnapi)
  5334. {
  5335. struct tg3 *tp = tnapi->tp;
  5336. u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
  5337. u32 sw_idx = tnapi->tx_cons;
  5338. struct netdev_queue *txq;
  5339. int index = tnapi - tp->napi;
  5340. unsigned int pkts_compl = 0, bytes_compl = 0;
  5341. if (tg3_flag(tp, ENABLE_TSS))
  5342. index--;
  5343. txq = netdev_get_tx_queue(tp->dev, index);
  5344. while (sw_idx != hw_idx) {
  5345. struct tg3_tx_ring_info *ri = &tnapi->tx_buffers[sw_idx];
  5346. struct sk_buff *skb = ri->skb;
  5347. int i, tx_bug = 0;
  5348. if (unlikely(skb == NULL)) {
  5349. tg3_tx_recover(tp);
  5350. return;
  5351. }
  5352. if (tnapi->tx_ring[sw_idx].len_flags & TXD_FLAG_HWTSTAMP) {
  5353. struct skb_shared_hwtstamps timestamp;
  5354. u64 hwclock = tr32(TG3_TX_TSTAMP_LSB);
  5355. hwclock |= (u64)tr32(TG3_TX_TSTAMP_MSB) << 32;
  5356. tg3_hwclock_to_timestamp(tp, hwclock, &timestamp);
  5357. skb_tstamp_tx(skb, &timestamp);
  5358. }
  5359. pci_unmap_single(tp->pdev,
  5360. dma_unmap_addr(ri, mapping),
  5361. skb_headlen(skb),
  5362. PCI_DMA_TODEVICE);
  5363. ri->skb = NULL;
  5364. while (ri->fragmented) {
  5365. ri->fragmented = false;
  5366. sw_idx = NEXT_TX(sw_idx);
  5367. ri = &tnapi->tx_buffers[sw_idx];
  5368. }
  5369. sw_idx = NEXT_TX(sw_idx);
  5370. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  5371. ri = &tnapi->tx_buffers[sw_idx];
  5372. if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
  5373. tx_bug = 1;
  5374. pci_unmap_page(tp->pdev,
  5375. dma_unmap_addr(ri, mapping),
  5376. skb_frag_size(&skb_shinfo(skb)->frags[i]),
  5377. PCI_DMA_TODEVICE);
  5378. while (ri->fragmented) {
  5379. ri->fragmented = false;
  5380. sw_idx = NEXT_TX(sw_idx);
  5381. ri = &tnapi->tx_buffers[sw_idx];
  5382. }
  5383. sw_idx = NEXT_TX(sw_idx);
  5384. }
  5385. pkts_compl++;
  5386. bytes_compl += skb->len;
  5387. dev_kfree_skb_any(skb);
  5388. if (unlikely(tx_bug)) {
  5389. tg3_tx_recover(tp);
  5390. return;
  5391. }
  5392. }
  5393. netdev_tx_completed_queue(txq, pkts_compl, bytes_compl);
  5394. tnapi->tx_cons = sw_idx;
  5395. /* Need to make the tx_cons update visible to tg3_start_xmit()
  5396. * before checking for netif_queue_stopped(). Without the
  5397. * memory barrier, there is a small possibility that tg3_start_xmit()
  5398. * will miss it and cause the queue to be stopped forever.
  5399. */
  5400. smp_mb();
  5401. if (unlikely(netif_tx_queue_stopped(txq) &&
  5402. (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
  5403. __netif_tx_lock(txq, smp_processor_id());
  5404. if (netif_tx_queue_stopped(txq) &&
  5405. (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
  5406. netif_tx_wake_queue(txq);
  5407. __netif_tx_unlock(txq);
  5408. }
  5409. }
  5410. static void tg3_frag_free(bool is_frag, void *data)
  5411. {
  5412. if (is_frag)
  5413. skb_free_frag(data);
  5414. else
  5415. kfree(data);
  5416. }
  5417. static void tg3_rx_data_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
  5418. {
  5419. unsigned int skb_size = SKB_DATA_ALIGN(map_sz + TG3_RX_OFFSET(tp)) +
  5420. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  5421. if (!ri->data)
  5422. return;
  5423. pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
  5424. map_sz, PCI_DMA_FROMDEVICE);
  5425. tg3_frag_free(skb_size <= PAGE_SIZE, ri->data);
  5426. ri->data = NULL;
  5427. }
  5428. /* Returns size of skb allocated or < 0 on error.
  5429. *
  5430. * We only need to fill in the address because the other members
  5431. * of the RX descriptor are invariant, see tg3_init_rings.
  5432. *
  5433. * Note the purposeful assymetry of cpu vs. chip accesses. For
  5434. * posting buffers we only dirty the first cache line of the RX
  5435. * descriptor (containing the address). Whereas for the RX status
  5436. * buffers the cpu only reads the last cacheline of the RX descriptor
  5437. * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
  5438. */
  5439. static int tg3_alloc_rx_data(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
  5440. u32 opaque_key, u32 dest_idx_unmasked,
  5441. unsigned int *frag_size)
  5442. {
  5443. struct tg3_rx_buffer_desc *desc;
  5444. struct ring_info *map;
  5445. u8 *data;
  5446. dma_addr_t mapping;
  5447. int skb_size, data_size, dest_idx;
  5448. switch (opaque_key) {
  5449. case RXD_OPAQUE_RING_STD:
  5450. dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
  5451. desc = &tpr->rx_std[dest_idx];
  5452. map = &tpr->rx_std_buffers[dest_idx];
  5453. data_size = tp->rx_pkt_map_sz;
  5454. break;
  5455. case RXD_OPAQUE_RING_JUMBO:
  5456. dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
  5457. desc = &tpr->rx_jmb[dest_idx].std;
  5458. map = &tpr->rx_jmb_buffers[dest_idx];
  5459. data_size = TG3_RX_JMB_MAP_SZ;
  5460. break;
  5461. default:
  5462. return -EINVAL;
  5463. }
  5464. /* Do not overwrite any of the map or rp information
  5465. * until we are sure we can commit to a new buffer.
  5466. *
  5467. * Callers depend upon this behavior and assume that
  5468. * we leave everything unchanged if we fail.
  5469. */
  5470. skb_size = SKB_DATA_ALIGN(data_size + TG3_RX_OFFSET(tp)) +
  5471. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  5472. if (skb_size <= PAGE_SIZE) {
  5473. data = netdev_alloc_frag(skb_size);
  5474. *frag_size = skb_size;
  5475. } else {
  5476. data = kmalloc(skb_size, GFP_ATOMIC);
  5477. *frag_size = 0;
  5478. }
  5479. if (!data)
  5480. return -ENOMEM;
  5481. mapping = pci_map_single(tp->pdev,
  5482. data + TG3_RX_OFFSET(tp),
  5483. data_size,
  5484. PCI_DMA_FROMDEVICE);
  5485. if (unlikely(pci_dma_mapping_error(tp->pdev, mapping))) {
  5486. tg3_frag_free(skb_size <= PAGE_SIZE, data);
  5487. return -EIO;
  5488. }
  5489. map->data = data;
  5490. dma_unmap_addr_set(map, mapping, mapping);
  5491. desc->addr_hi = ((u64)mapping >> 32);
  5492. desc->addr_lo = ((u64)mapping & 0xffffffff);
  5493. return data_size;
  5494. }
  5495. /* We only need to move over in the address because the other
  5496. * members of the RX descriptor are invariant. See notes above
  5497. * tg3_alloc_rx_data for full details.
  5498. */
  5499. static void tg3_recycle_rx(struct tg3_napi *tnapi,
  5500. struct tg3_rx_prodring_set *dpr,
  5501. u32 opaque_key, int src_idx,
  5502. u32 dest_idx_unmasked)
  5503. {
  5504. struct tg3 *tp = tnapi->tp;
  5505. struct tg3_rx_buffer_desc *src_desc, *dest_desc;
  5506. struct ring_info *src_map, *dest_map;
  5507. struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
  5508. int dest_idx;
  5509. switch (opaque_key) {
  5510. case RXD_OPAQUE_RING_STD:
  5511. dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
  5512. dest_desc = &dpr->rx_std[dest_idx];
  5513. dest_map = &dpr->rx_std_buffers[dest_idx];
  5514. src_desc = &spr->rx_std[src_idx];
  5515. src_map = &spr->rx_std_buffers[src_idx];
  5516. break;
  5517. case RXD_OPAQUE_RING_JUMBO:
  5518. dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
  5519. dest_desc = &dpr->rx_jmb[dest_idx].std;
  5520. dest_map = &dpr->rx_jmb_buffers[dest_idx];
  5521. src_desc = &spr->rx_jmb[src_idx].std;
  5522. src_map = &spr->rx_jmb_buffers[src_idx];
  5523. break;
  5524. default:
  5525. return;
  5526. }
  5527. dest_map->data = src_map->data;
  5528. dma_unmap_addr_set(dest_map, mapping,
  5529. dma_unmap_addr(src_map, mapping));
  5530. dest_desc->addr_hi = src_desc->addr_hi;
  5531. dest_desc->addr_lo = src_desc->addr_lo;
  5532. /* Ensure that the update to the skb happens after the physical
  5533. * addresses have been transferred to the new BD location.
  5534. */
  5535. smp_wmb();
  5536. src_map->data = NULL;
  5537. }
  5538. /* The RX ring scheme is composed of multiple rings which post fresh
  5539. * buffers to the chip, and one special ring the chip uses to report
  5540. * status back to the host.
  5541. *
  5542. * The special ring reports the status of received packets to the
  5543. * host. The chip does not write into the original descriptor the
  5544. * RX buffer was obtained from. The chip simply takes the original
  5545. * descriptor as provided by the host, updates the status and length
  5546. * field, then writes this into the next status ring entry.
  5547. *
  5548. * Each ring the host uses to post buffers to the chip is described
  5549. * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
  5550. * it is first placed into the on-chip ram. When the packet's length
  5551. * is known, it walks down the TG3_BDINFO entries to select the ring.
  5552. * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
  5553. * which is within the range of the new packet's length is chosen.
  5554. *
  5555. * The "separate ring for rx status" scheme may sound queer, but it makes
  5556. * sense from a cache coherency perspective. If only the host writes
  5557. * to the buffer post rings, and only the chip writes to the rx status
  5558. * rings, then cache lines never move beyond shared-modified state.
  5559. * If both the host and chip were to write into the same ring, cache line
  5560. * eviction could occur since both entities want it in an exclusive state.
  5561. */
  5562. static int tg3_rx(struct tg3_napi *tnapi, int budget)
  5563. {
  5564. struct tg3 *tp = tnapi->tp;
  5565. u32 work_mask, rx_std_posted = 0;
  5566. u32 std_prod_idx, jmb_prod_idx;
  5567. u32 sw_idx = tnapi->rx_rcb_ptr;
  5568. u16 hw_idx;
  5569. int received;
  5570. struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
  5571. hw_idx = *(tnapi->rx_rcb_prod_idx);
  5572. /*
  5573. * We need to order the read of hw_idx and the read of
  5574. * the opaque cookie.
  5575. */
  5576. rmb();
  5577. work_mask = 0;
  5578. received = 0;
  5579. std_prod_idx = tpr->rx_std_prod_idx;
  5580. jmb_prod_idx = tpr->rx_jmb_prod_idx;
  5581. while (sw_idx != hw_idx && budget > 0) {
  5582. struct ring_info *ri;
  5583. struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
  5584. unsigned int len;
  5585. struct sk_buff *skb;
  5586. dma_addr_t dma_addr;
  5587. u32 opaque_key, desc_idx, *post_ptr;
  5588. u8 *data;
  5589. u64 tstamp = 0;
  5590. desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
  5591. opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
  5592. if (opaque_key == RXD_OPAQUE_RING_STD) {
  5593. ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
  5594. dma_addr = dma_unmap_addr(ri, mapping);
  5595. data = ri->data;
  5596. post_ptr = &std_prod_idx;
  5597. rx_std_posted++;
  5598. } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
  5599. ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
  5600. dma_addr = dma_unmap_addr(ri, mapping);
  5601. data = ri->data;
  5602. post_ptr = &jmb_prod_idx;
  5603. } else
  5604. goto next_pkt_nopost;
  5605. work_mask |= opaque_key;
  5606. if (desc->err_vlan & RXD_ERR_MASK) {
  5607. drop_it:
  5608. tg3_recycle_rx(tnapi, tpr, opaque_key,
  5609. desc_idx, *post_ptr);
  5610. drop_it_no_recycle:
  5611. /* Other statistics kept track of by card. */
  5612. tp->rx_dropped++;
  5613. goto next_pkt;
  5614. }
  5615. prefetch(data + TG3_RX_OFFSET(tp));
  5616. len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
  5617. ETH_FCS_LEN;
  5618. if ((desc->type_flags & RXD_FLAG_PTPSTAT_MASK) ==
  5619. RXD_FLAG_PTPSTAT_PTPV1 ||
  5620. (desc->type_flags & RXD_FLAG_PTPSTAT_MASK) ==
  5621. RXD_FLAG_PTPSTAT_PTPV2) {
  5622. tstamp = tr32(TG3_RX_TSTAMP_LSB);
  5623. tstamp |= (u64)tr32(TG3_RX_TSTAMP_MSB) << 32;
  5624. }
  5625. if (len > TG3_RX_COPY_THRESH(tp)) {
  5626. int skb_size;
  5627. unsigned int frag_size;
  5628. skb_size = tg3_alloc_rx_data(tp, tpr, opaque_key,
  5629. *post_ptr, &frag_size);
  5630. if (skb_size < 0)
  5631. goto drop_it;
  5632. pci_unmap_single(tp->pdev, dma_addr, skb_size,
  5633. PCI_DMA_FROMDEVICE);
  5634. /* Ensure that the update to the data happens
  5635. * after the usage of the old DMA mapping.
  5636. */
  5637. smp_wmb();
  5638. ri->data = NULL;
  5639. skb = build_skb(data, frag_size);
  5640. if (!skb) {
  5641. tg3_frag_free(frag_size != 0, data);
  5642. goto drop_it_no_recycle;
  5643. }
  5644. skb_reserve(skb, TG3_RX_OFFSET(tp));
  5645. } else {
  5646. tg3_recycle_rx(tnapi, tpr, opaque_key,
  5647. desc_idx, *post_ptr);
  5648. skb = netdev_alloc_skb(tp->dev,
  5649. len + TG3_RAW_IP_ALIGN);
  5650. if (skb == NULL)
  5651. goto drop_it_no_recycle;
  5652. skb_reserve(skb, TG3_RAW_IP_ALIGN);
  5653. pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
  5654. memcpy(skb->data,
  5655. data + TG3_RX_OFFSET(tp),
  5656. len);
  5657. pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
  5658. }
  5659. skb_put(skb, len);
  5660. if (tstamp)
  5661. tg3_hwclock_to_timestamp(tp, tstamp,
  5662. skb_hwtstamps(skb));
  5663. if ((tp->dev->features & NETIF_F_RXCSUM) &&
  5664. (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
  5665. (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
  5666. >> RXD_TCPCSUM_SHIFT) == 0xffff))
  5667. skb->ip_summed = CHECKSUM_UNNECESSARY;
  5668. else
  5669. skb_checksum_none_assert(skb);
  5670. skb->protocol = eth_type_trans(skb, tp->dev);
  5671. if (len > (tp->dev->mtu + ETH_HLEN) &&
  5672. skb->protocol != htons(ETH_P_8021Q) &&
  5673. skb->protocol != htons(ETH_P_8021AD)) {
  5674. dev_kfree_skb_any(skb);
  5675. goto drop_it_no_recycle;
  5676. }
  5677. if (desc->type_flags & RXD_FLAG_VLAN &&
  5678. !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
  5679. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
  5680. desc->err_vlan & RXD_VLAN_MASK);
  5681. napi_gro_receive(&tnapi->napi, skb);
  5682. received++;
  5683. budget--;
  5684. next_pkt:
  5685. (*post_ptr)++;
  5686. if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
  5687. tpr->rx_std_prod_idx = std_prod_idx &
  5688. tp->rx_std_ring_mask;
  5689. tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
  5690. tpr->rx_std_prod_idx);
  5691. work_mask &= ~RXD_OPAQUE_RING_STD;
  5692. rx_std_posted = 0;
  5693. }
  5694. next_pkt_nopost:
  5695. sw_idx++;
  5696. sw_idx &= tp->rx_ret_ring_mask;
  5697. /* Refresh hw_idx to see if there is new work */
  5698. if (sw_idx == hw_idx) {
  5699. hw_idx = *(tnapi->rx_rcb_prod_idx);
  5700. rmb();
  5701. }
  5702. }
  5703. /* ACK the status ring. */
  5704. tnapi->rx_rcb_ptr = sw_idx;
  5705. tw32_rx_mbox(tnapi->consmbox, sw_idx);
  5706. /* Refill RX ring(s). */
  5707. if (!tg3_flag(tp, ENABLE_RSS)) {
  5708. /* Sync BD data before updating mailbox */
  5709. wmb();
  5710. if (work_mask & RXD_OPAQUE_RING_STD) {
  5711. tpr->rx_std_prod_idx = std_prod_idx &
  5712. tp->rx_std_ring_mask;
  5713. tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
  5714. tpr->rx_std_prod_idx);
  5715. }
  5716. if (work_mask & RXD_OPAQUE_RING_JUMBO) {
  5717. tpr->rx_jmb_prod_idx = jmb_prod_idx &
  5718. tp->rx_jmb_ring_mask;
  5719. tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
  5720. tpr->rx_jmb_prod_idx);
  5721. }
  5722. mmiowb();
  5723. } else if (work_mask) {
  5724. /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
  5725. * updated before the producer indices can be updated.
  5726. */
  5727. smp_wmb();
  5728. tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
  5729. tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
  5730. if (tnapi != &tp->napi[1]) {
  5731. tp->rx_refill = true;
  5732. napi_schedule(&tp->napi[1].napi);
  5733. }
  5734. }
  5735. return received;
  5736. }
  5737. static void tg3_poll_link(struct tg3 *tp)
  5738. {
  5739. /* handle link change and other phy events */
  5740. if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
  5741. struct tg3_hw_status *sblk = tp->napi[0].hw_status;
  5742. if (sblk->status & SD_STATUS_LINK_CHG) {
  5743. sblk->status = SD_STATUS_UPDATED |
  5744. (sblk->status & ~SD_STATUS_LINK_CHG);
  5745. spin_lock(&tp->lock);
  5746. if (tg3_flag(tp, USE_PHYLIB)) {
  5747. tw32_f(MAC_STATUS,
  5748. (MAC_STATUS_SYNC_CHANGED |
  5749. MAC_STATUS_CFG_CHANGED |
  5750. MAC_STATUS_MI_COMPLETION |
  5751. MAC_STATUS_LNKSTATE_CHANGED));
  5752. udelay(40);
  5753. } else
  5754. tg3_setup_phy(tp, false);
  5755. spin_unlock(&tp->lock);
  5756. }
  5757. }
  5758. }
  5759. static int tg3_rx_prodring_xfer(struct tg3 *tp,
  5760. struct tg3_rx_prodring_set *dpr,
  5761. struct tg3_rx_prodring_set *spr)
  5762. {
  5763. u32 si, di, cpycnt, src_prod_idx;
  5764. int i, err = 0;
  5765. while (1) {
  5766. src_prod_idx = spr->rx_std_prod_idx;
  5767. /* Make sure updates to the rx_std_buffers[] entries and the
  5768. * standard producer index are seen in the correct order.
  5769. */
  5770. smp_rmb();
  5771. if (spr->rx_std_cons_idx == src_prod_idx)
  5772. break;
  5773. if (spr->rx_std_cons_idx < src_prod_idx)
  5774. cpycnt = src_prod_idx - spr->rx_std_cons_idx;
  5775. else
  5776. cpycnt = tp->rx_std_ring_mask + 1 -
  5777. spr->rx_std_cons_idx;
  5778. cpycnt = min(cpycnt,
  5779. tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
  5780. si = spr->rx_std_cons_idx;
  5781. di = dpr->rx_std_prod_idx;
  5782. for (i = di; i < di + cpycnt; i++) {
  5783. if (dpr->rx_std_buffers[i].data) {
  5784. cpycnt = i - di;
  5785. err = -ENOSPC;
  5786. break;
  5787. }
  5788. }
  5789. if (!cpycnt)
  5790. break;
  5791. /* Ensure that updates to the rx_std_buffers ring and the
  5792. * shadowed hardware producer ring from tg3_recycle_skb() are
  5793. * ordered correctly WRT the skb check above.
  5794. */
  5795. smp_rmb();
  5796. memcpy(&dpr->rx_std_buffers[di],
  5797. &spr->rx_std_buffers[si],
  5798. cpycnt * sizeof(struct ring_info));
  5799. for (i = 0; i < cpycnt; i++, di++, si++) {
  5800. struct tg3_rx_buffer_desc *sbd, *dbd;
  5801. sbd = &spr->rx_std[si];
  5802. dbd = &dpr->rx_std[di];
  5803. dbd->addr_hi = sbd->addr_hi;
  5804. dbd->addr_lo = sbd->addr_lo;
  5805. }
  5806. spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
  5807. tp->rx_std_ring_mask;
  5808. dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
  5809. tp->rx_std_ring_mask;
  5810. }
  5811. while (1) {
  5812. src_prod_idx = spr->rx_jmb_prod_idx;
  5813. /* Make sure updates to the rx_jmb_buffers[] entries and
  5814. * the jumbo producer index are seen in the correct order.
  5815. */
  5816. smp_rmb();
  5817. if (spr->rx_jmb_cons_idx == src_prod_idx)
  5818. break;
  5819. if (spr->rx_jmb_cons_idx < src_prod_idx)
  5820. cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
  5821. else
  5822. cpycnt = tp->rx_jmb_ring_mask + 1 -
  5823. spr->rx_jmb_cons_idx;
  5824. cpycnt = min(cpycnt,
  5825. tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
  5826. si = spr->rx_jmb_cons_idx;
  5827. di = dpr->rx_jmb_prod_idx;
  5828. for (i = di; i < di + cpycnt; i++) {
  5829. if (dpr->rx_jmb_buffers[i].data) {
  5830. cpycnt = i - di;
  5831. err = -ENOSPC;
  5832. break;
  5833. }
  5834. }
  5835. if (!cpycnt)
  5836. break;
  5837. /* Ensure that updates to the rx_jmb_buffers ring and the
  5838. * shadowed hardware producer ring from tg3_recycle_skb() are
  5839. * ordered correctly WRT the skb check above.
  5840. */
  5841. smp_rmb();
  5842. memcpy(&dpr->rx_jmb_buffers[di],
  5843. &spr->rx_jmb_buffers[si],
  5844. cpycnt * sizeof(struct ring_info));
  5845. for (i = 0; i < cpycnt; i++, di++, si++) {
  5846. struct tg3_rx_buffer_desc *sbd, *dbd;
  5847. sbd = &spr->rx_jmb[si].std;
  5848. dbd = &dpr->rx_jmb[di].std;
  5849. dbd->addr_hi = sbd->addr_hi;
  5850. dbd->addr_lo = sbd->addr_lo;
  5851. }
  5852. spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
  5853. tp->rx_jmb_ring_mask;
  5854. dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
  5855. tp->rx_jmb_ring_mask;
  5856. }
  5857. return err;
  5858. }
  5859. static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
  5860. {
  5861. struct tg3 *tp = tnapi->tp;
  5862. /* run TX completion thread */
  5863. if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
  5864. tg3_tx(tnapi);
  5865. if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
  5866. return work_done;
  5867. }
  5868. if (!tnapi->rx_rcb_prod_idx)
  5869. return work_done;
  5870. /* run RX thread, within the bounds set by NAPI.
  5871. * All RX "locking" is done by ensuring outside
  5872. * code synchronizes with tg3->napi.poll()
  5873. */
  5874. if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
  5875. work_done += tg3_rx(tnapi, budget - work_done);
  5876. if (tg3_flag(tp, ENABLE_RSS) && tnapi == &tp->napi[1]) {
  5877. struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
  5878. int i, err = 0;
  5879. u32 std_prod_idx = dpr->rx_std_prod_idx;
  5880. u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
  5881. tp->rx_refill = false;
  5882. for (i = 1; i <= tp->rxq_cnt; i++)
  5883. err |= tg3_rx_prodring_xfer(tp, dpr,
  5884. &tp->napi[i].prodring);
  5885. wmb();
  5886. if (std_prod_idx != dpr->rx_std_prod_idx)
  5887. tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
  5888. dpr->rx_std_prod_idx);
  5889. if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
  5890. tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
  5891. dpr->rx_jmb_prod_idx);
  5892. mmiowb();
  5893. if (err)
  5894. tw32_f(HOSTCC_MODE, tp->coal_now);
  5895. }
  5896. return work_done;
  5897. }
  5898. static inline void tg3_reset_task_schedule(struct tg3 *tp)
  5899. {
  5900. if (!test_and_set_bit(TG3_FLAG_RESET_TASK_PENDING, tp->tg3_flags))
  5901. schedule_work(&tp->reset_task);
  5902. }
  5903. static inline void tg3_reset_task_cancel(struct tg3 *tp)
  5904. {
  5905. cancel_work_sync(&tp->reset_task);
  5906. tg3_flag_clear(tp, RESET_TASK_PENDING);
  5907. tg3_flag_clear(tp, TX_RECOVERY_PENDING);
  5908. }
  5909. static int tg3_poll_msix(struct napi_struct *napi, int budget)
  5910. {
  5911. struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
  5912. struct tg3 *tp = tnapi->tp;
  5913. int work_done = 0;
  5914. struct tg3_hw_status *sblk = tnapi->hw_status;
  5915. while (1) {
  5916. work_done = tg3_poll_work(tnapi, work_done, budget);
  5917. if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
  5918. goto tx_recovery;
  5919. if (unlikely(work_done >= budget))
  5920. break;
  5921. /* tp->last_tag is used in tg3_int_reenable() below
  5922. * to tell the hw how much work has been processed,
  5923. * so we must read it before checking for more work.
  5924. */
  5925. tnapi->last_tag = sblk->status_tag;
  5926. tnapi->last_irq_tag = tnapi->last_tag;
  5927. rmb();
  5928. /* check for RX/TX work to do */
  5929. if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
  5930. *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
  5931. /* This test here is not race free, but will reduce
  5932. * the number of interrupts by looping again.
  5933. */
  5934. if (tnapi == &tp->napi[1] && tp->rx_refill)
  5935. continue;
  5936. napi_complete_done(napi, work_done);
  5937. /* Reenable interrupts. */
  5938. tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
  5939. /* This test here is synchronized by napi_schedule()
  5940. * and napi_complete() to close the race condition.
  5941. */
  5942. if (unlikely(tnapi == &tp->napi[1] && tp->rx_refill)) {
  5943. tw32(HOSTCC_MODE, tp->coalesce_mode |
  5944. HOSTCC_MODE_ENABLE |
  5945. tnapi->coal_now);
  5946. }
  5947. mmiowb();
  5948. break;
  5949. }
  5950. }
  5951. return work_done;
  5952. tx_recovery:
  5953. /* work_done is guaranteed to be less than budget. */
  5954. napi_complete(napi);
  5955. tg3_reset_task_schedule(tp);
  5956. return work_done;
  5957. }
  5958. static void tg3_process_error(struct tg3 *tp)
  5959. {
  5960. u32 val;
  5961. bool real_error = false;
  5962. if (tg3_flag(tp, ERROR_PROCESSED))
  5963. return;
  5964. /* Check Flow Attention register */
  5965. val = tr32(HOSTCC_FLOW_ATTN);
  5966. if (val & ~HOSTCC_FLOW_ATTN_MBUF_LWM) {
  5967. netdev_err(tp->dev, "FLOW Attention error. Resetting chip.\n");
  5968. real_error = true;
  5969. }
  5970. if (tr32(MSGINT_STATUS) & ~MSGINT_STATUS_MSI_REQ) {
  5971. netdev_err(tp->dev, "MSI Status error. Resetting chip.\n");
  5972. real_error = true;
  5973. }
  5974. if (tr32(RDMAC_STATUS) || tr32(WDMAC_STATUS)) {
  5975. netdev_err(tp->dev, "DMA Status error. Resetting chip.\n");
  5976. real_error = true;
  5977. }
  5978. if (!real_error)
  5979. return;
  5980. tg3_dump_state(tp);
  5981. tg3_flag_set(tp, ERROR_PROCESSED);
  5982. tg3_reset_task_schedule(tp);
  5983. }
  5984. static int tg3_poll(struct napi_struct *napi, int budget)
  5985. {
  5986. struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
  5987. struct tg3 *tp = tnapi->tp;
  5988. int work_done = 0;
  5989. struct tg3_hw_status *sblk = tnapi->hw_status;
  5990. while (1) {
  5991. if (sblk->status & SD_STATUS_ERROR)
  5992. tg3_process_error(tp);
  5993. tg3_poll_link(tp);
  5994. work_done = tg3_poll_work(tnapi, work_done, budget);
  5995. if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
  5996. goto tx_recovery;
  5997. if (unlikely(work_done >= budget))
  5998. break;
  5999. if (tg3_flag(tp, TAGGED_STATUS)) {
  6000. /* tp->last_tag is used in tg3_int_reenable() below
  6001. * to tell the hw how much work has been processed,
  6002. * so we must read it before checking for more work.
  6003. */
  6004. tnapi->last_tag = sblk->status_tag;
  6005. tnapi->last_irq_tag = tnapi->last_tag;
  6006. rmb();
  6007. } else
  6008. sblk->status &= ~SD_STATUS_UPDATED;
  6009. if (likely(!tg3_has_work(tnapi))) {
  6010. napi_complete_done(napi, work_done);
  6011. tg3_int_reenable(tnapi);
  6012. break;
  6013. }
  6014. }
  6015. return work_done;
  6016. tx_recovery:
  6017. /* work_done is guaranteed to be less than budget. */
  6018. napi_complete(napi);
  6019. tg3_reset_task_schedule(tp);
  6020. return work_done;
  6021. }
  6022. static void tg3_napi_disable(struct tg3 *tp)
  6023. {
  6024. int i;
  6025. for (i = tp->irq_cnt - 1; i >= 0; i--)
  6026. napi_disable(&tp->napi[i].napi);
  6027. }
  6028. static void tg3_napi_enable(struct tg3 *tp)
  6029. {
  6030. int i;
  6031. for (i = 0; i < tp->irq_cnt; i++)
  6032. napi_enable(&tp->napi[i].napi);
  6033. }
  6034. static void tg3_napi_init(struct tg3 *tp)
  6035. {
  6036. int i;
  6037. netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
  6038. for (i = 1; i < tp->irq_cnt; i++)
  6039. netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
  6040. }
  6041. static void tg3_napi_fini(struct tg3 *tp)
  6042. {
  6043. int i;
  6044. for (i = 0; i < tp->irq_cnt; i++)
  6045. netif_napi_del(&tp->napi[i].napi);
  6046. }
  6047. static inline void tg3_netif_stop(struct tg3 *tp)
  6048. {
  6049. netif_trans_update(tp->dev); /* prevent tx timeout */
  6050. tg3_napi_disable(tp);
  6051. netif_carrier_off(tp->dev);
  6052. netif_tx_disable(tp->dev);
  6053. }
  6054. /* tp->lock must be held */
  6055. static inline void tg3_netif_start(struct tg3 *tp)
  6056. {
  6057. tg3_ptp_resume(tp);
  6058. /* NOTE: unconditional netif_tx_wake_all_queues is only
  6059. * appropriate so long as all callers are assured to
  6060. * have free tx slots (such as after tg3_init_hw)
  6061. */
  6062. netif_tx_wake_all_queues(tp->dev);
  6063. if (tp->link_up)
  6064. netif_carrier_on(tp->dev);
  6065. tg3_napi_enable(tp);
  6066. tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
  6067. tg3_enable_ints(tp);
  6068. }
  6069. static void tg3_irq_quiesce(struct tg3 *tp)
  6070. __releases(tp->lock)
  6071. __acquires(tp->lock)
  6072. {
  6073. int i;
  6074. BUG_ON(tp->irq_sync);
  6075. tp->irq_sync = 1;
  6076. smp_mb();
  6077. spin_unlock_bh(&tp->lock);
  6078. for (i = 0; i < tp->irq_cnt; i++)
  6079. synchronize_irq(tp->napi[i].irq_vec);
  6080. spin_lock_bh(&tp->lock);
  6081. }
  6082. /* Fully shutdown all tg3 driver activity elsewhere in the system.
  6083. * If irq_sync is non-zero, then the IRQ handler must be synchronized
  6084. * with as well. Most of the time, this is not necessary except when
  6085. * shutting down the device.
  6086. */
  6087. static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
  6088. {
  6089. spin_lock_bh(&tp->lock);
  6090. if (irq_sync)
  6091. tg3_irq_quiesce(tp);
  6092. }
  6093. static inline void tg3_full_unlock(struct tg3 *tp)
  6094. {
  6095. spin_unlock_bh(&tp->lock);
  6096. }
  6097. /* One-shot MSI handler - Chip automatically disables interrupt
  6098. * after sending MSI so driver doesn't have to do it.
  6099. */
  6100. static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
  6101. {
  6102. struct tg3_napi *tnapi = dev_id;
  6103. struct tg3 *tp = tnapi->tp;
  6104. prefetch(tnapi->hw_status);
  6105. if (tnapi->rx_rcb)
  6106. prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
  6107. if (likely(!tg3_irq_sync(tp)))
  6108. napi_schedule(&tnapi->napi);
  6109. return IRQ_HANDLED;
  6110. }
  6111. /* MSI ISR - No need to check for interrupt sharing and no need to
  6112. * flush status block and interrupt mailbox. PCI ordering rules
  6113. * guarantee that MSI will arrive after the status block.
  6114. */
  6115. static irqreturn_t tg3_msi(int irq, void *dev_id)
  6116. {
  6117. struct tg3_napi *tnapi = dev_id;
  6118. struct tg3 *tp = tnapi->tp;
  6119. prefetch(tnapi->hw_status);
  6120. if (tnapi->rx_rcb)
  6121. prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
  6122. /*
  6123. * Writing any value to intr-mbox-0 clears PCI INTA# and
  6124. * chip-internal interrupt pending events.
  6125. * Writing non-zero to intr-mbox-0 additional tells the
  6126. * NIC to stop sending us irqs, engaging "in-intr-handler"
  6127. * event coalescing.
  6128. */
  6129. tw32_mailbox(tnapi->int_mbox, 0x00000001);
  6130. if (likely(!tg3_irq_sync(tp)))
  6131. napi_schedule(&tnapi->napi);
  6132. return IRQ_RETVAL(1);
  6133. }
  6134. static irqreturn_t tg3_interrupt(int irq, void *dev_id)
  6135. {
  6136. struct tg3_napi *tnapi = dev_id;
  6137. struct tg3 *tp = tnapi->tp;
  6138. struct tg3_hw_status *sblk = tnapi->hw_status;
  6139. unsigned int handled = 1;
  6140. /* In INTx mode, it is possible for the interrupt to arrive at
  6141. * the CPU before the status block posted prior to the interrupt.
  6142. * Reading the PCI State register will confirm whether the
  6143. * interrupt is ours and will flush the status block.
  6144. */
  6145. if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
  6146. if (tg3_flag(tp, CHIP_RESETTING) ||
  6147. (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
  6148. handled = 0;
  6149. goto out;
  6150. }
  6151. }
  6152. /*
  6153. * Writing any value to intr-mbox-0 clears PCI INTA# and
  6154. * chip-internal interrupt pending events.
  6155. * Writing non-zero to intr-mbox-0 additional tells the
  6156. * NIC to stop sending us irqs, engaging "in-intr-handler"
  6157. * event coalescing.
  6158. *
  6159. * Flush the mailbox to de-assert the IRQ immediately to prevent
  6160. * spurious interrupts. The flush impacts performance but
  6161. * excessive spurious interrupts can be worse in some cases.
  6162. */
  6163. tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
  6164. if (tg3_irq_sync(tp))
  6165. goto out;
  6166. sblk->status &= ~SD_STATUS_UPDATED;
  6167. if (likely(tg3_has_work(tnapi))) {
  6168. prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
  6169. napi_schedule(&tnapi->napi);
  6170. } else {
  6171. /* No work, shared interrupt perhaps? re-enable
  6172. * interrupts, and flush that PCI write
  6173. */
  6174. tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
  6175. 0x00000000);
  6176. }
  6177. out:
  6178. return IRQ_RETVAL(handled);
  6179. }
  6180. static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
  6181. {
  6182. struct tg3_napi *tnapi = dev_id;
  6183. struct tg3 *tp = tnapi->tp;
  6184. struct tg3_hw_status *sblk = tnapi->hw_status;
  6185. unsigned int handled = 1;
  6186. /* In INTx mode, it is possible for the interrupt to arrive at
  6187. * the CPU before the status block posted prior to the interrupt.
  6188. * Reading the PCI State register will confirm whether the
  6189. * interrupt is ours and will flush the status block.
  6190. */
  6191. if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
  6192. if (tg3_flag(tp, CHIP_RESETTING) ||
  6193. (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
  6194. handled = 0;
  6195. goto out;
  6196. }
  6197. }
  6198. /*
  6199. * writing any value to intr-mbox-0 clears PCI INTA# and
  6200. * chip-internal interrupt pending events.
  6201. * writing non-zero to intr-mbox-0 additional tells the
  6202. * NIC to stop sending us irqs, engaging "in-intr-handler"
  6203. * event coalescing.
  6204. *
  6205. * Flush the mailbox to de-assert the IRQ immediately to prevent
  6206. * spurious interrupts. The flush impacts performance but
  6207. * excessive spurious interrupts can be worse in some cases.
  6208. */
  6209. tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
  6210. /*
  6211. * In a shared interrupt configuration, sometimes other devices'
  6212. * interrupts will scream. We record the current status tag here
  6213. * so that the above check can report that the screaming interrupts
  6214. * are unhandled. Eventually they will be silenced.
  6215. */
  6216. tnapi->last_irq_tag = sblk->status_tag;
  6217. if (tg3_irq_sync(tp))
  6218. goto out;
  6219. prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
  6220. napi_schedule(&tnapi->napi);
  6221. out:
  6222. return IRQ_RETVAL(handled);
  6223. }
  6224. /* ISR for interrupt test */
  6225. static irqreturn_t tg3_test_isr(int irq, void *dev_id)
  6226. {
  6227. struct tg3_napi *tnapi = dev_id;
  6228. struct tg3 *tp = tnapi->tp;
  6229. struct tg3_hw_status *sblk = tnapi->hw_status;
  6230. if ((sblk->status & SD_STATUS_UPDATED) ||
  6231. !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
  6232. tg3_disable_ints(tp);
  6233. return IRQ_RETVAL(1);
  6234. }
  6235. return IRQ_RETVAL(0);
  6236. }
  6237. #ifdef CONFIG_NET_POLL_CONTROLLER
  6238. static void tg3_poll_controller(struct net_device *dev)
  6239. {
  6240. int i;
  6241. struct tg3 *tp = netdev_priv(dev);
  6242. if (tg3_irq_sync(tp))
  6243. return;
  6244. for (i = 0; i < tp->irq_cnt; i++)
  6245. tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
  6246. }
  6247. #endif
  6248. static void tg3_tx_timeout(struct net_device *dev)
  6249. {
  6250. struct tg3 *tp = netdev_priv(dev);
  6251. if (netif_msg_tx_err(tp)) {
  6252. netdev_err(dev, "transmit timed out, resetting\n");
  6253. tg3_dump_state(tp);
  6254. }
  6255. tg3_reset_task_schedule(tp);
  6256. }
  6257. /* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
  6258. static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
  6259. {
  6260. u32 base = (u32) mapping & 0xffffffff;
  6261. return base + len + 8 < base;
  6262. }
  6263. /* Test for TSO DMA buffers that cross into regions which are within MSS bytes
  6264. * of any 4GB boundaries: 4G, 8G, etc
  6265. */
  6266. static inline int tg3_4g_tso_overflow_test(struct tg3 *tp, dma_addr_t mapping,
  6267. u32 len, u32 mss)
  6268. {
  6269. if (tg3_asic_rev(tp) == ASIC_REV_5762 && mss) {
  6270. u32 base = (u32) mapping & 0xffffffff;
  6271. return ((base + len + (mss & 0x3fff)) < base);
  6272. }
  6273. return 0;
  6274. }
  6275. /* Test for DMA addresses > 40-bit */
  6276. static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
  6277. int len)
  6278. {
  6279. #if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
  6280. if (tg3_flag(tp, 40BIT_DMA_BUG))
  6281. return ((u64) mapping + len) > DMA_BIT_MASK(40);
  6282. return 0;
  6283. #else
  6284. return 0;
  6285. #endif
  6286. }
  6287. static inline void tg3_tx_set_bd(struct tg3_tx_buffer_desc *txbd,
  6288. dma_addr_t mapping, u32 len, u32 flags,
  6289. u32 mss, u32 vlan)
  6290. {
  6291. txbd->addr_hi = ((u64) mapping >> 32);
  6292. txbd->addr_lo = ((u64) mapping & 0xffffffff);
  6293. txbd->len_flags = (len << TXD_LEN_SHIFT) | (flags & 0x0000ffff);
  6294. txbd->vlan_tag = (mss << TXD_MSS_SHIFT) | (vlan << TXD_VLAN_TAG_SHIFT);
  6295. }
  6296. static bool tg3_tx_frag_set(struct tg3_napi *tnapi, u32 *entry, u32 *budget,
  6297. dma_addr_t map, u32 len, u32 flags,
  6298. u32 mss, u32 vlan)
  6299. {
  6300. struct tg3 *tp = tnapi->tp;
  6301. bool hwbug = false;
  6302. if (tg3_flag(tp, SHORT_DMA_BUG) && len <= 8)
  6303. hwbug = true;
  6304. if (tg3_4g_overflow_test(map, len))
  6305. hwbug = true;
  6306. if (tg3_4g_tso_overflow_test(tp, map, len, mss))
  6307. hwbug = true;
  6308. if (tg3_40bit_overflow_test(tp, map, len))
  6309. hwbug = true;
  6310. if (tp->dma_limit) {
  6311. u32 prvidx = *entry;
  6312. u32 tmp_flag = flags & ~TXD_FLAG_END;
  6313. while (len > tp->dma_limit && *budget) {
  6314. u32 frag_len = tp->dma_limit;
  6315. len -= tp->dma_limit;
  6316. /* Avoid the 8byte DMA problem */
  6317. if (len <= 8) {
  6318. len += tp->dma_limit / 2;
  6319. frag_len = tp->dma_limit / 2;
  6320. }
  6321. tnapi->tx_buffers[*entry].fragmented = true;
  6322. tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
  6323. frag_len, tmp_flag, mss, vlan);
  6324. *budget -= 1;
  6325. prvidx = *entry;
  6326. *entry = NEXT_TX(*entry);
  6327. map += frag_len;
  6328. }
  6329. if (len) {
  6330. if (*budget) {
  6331. tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
  6332. len, flags, mss, vlan);
  6333. *budget -= 1;
  6334. *entry = NEXT_TX(*entry);
  6335. } else {
  6336. hwbug = true;
  6337. tnapi->tx_buffers[prvidx].fragmented = false;
  6338. }
  6339. }
  6340. } else {
  6341. tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
  6342. len, flags, mss, vlan);
  6343. *entry = NEXT_TX(*entry);
  6344. }
  6345. return hwbug;
  6346. }
  6347. static void tg3_tx_skb_unmap(struct tg3_napi *tnapi, u32 entry, int last)
  6348. {
  6349. int i;
  6350. struct sk_buff *skb;
  6351. struct tg3_tx_ring_info *txb = &tnapi->tx_buffers[entry];
  6352. skb = txb->skb;
  6353. txb->skb = NULL;
  6354. pci_unmap_single(tnapi->tp->pdev,
  6355. dma_unmap_addr(txb, mapping),
  6356. skb_headlen(skb),
  6357. PCI_DMA_TODEVICE);
  6358. while (txb->fragmented) {
  6359. txb->fragmented = false;
  6360. entry = NEXT_TX(entry);
  6361. txb = &tnapi->tx_buffers[entry];
  6362. }
  6363. for (i = 0; i <= last; i++) {
  6364. const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  6365. entry = NEXT_TX(entry);
  6366. txb = &tnapi->tx_buffers[entry];
  6367. pci_unmap_page(tnapi->tp->pdev,
  6368. dma_unmap_addr(txb, mapping),
  6369. skb_frag_size(frag), PCI_DMA_TODEVICE);
  6370. while (txb->fragmented) {
  6371. txb->fragmented = false;
  6372. entry = NEXT_TX(entry);
  6373. txb = &tnapi->tx_buffers[entry];
  6374. }
  6375. }
  6376. }
  6377. /* Workaround 4GB and 40-bit hardware DMA bugs. */
  6378. static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
  6379. struct sk_buff **pskb,
  6380. u32 *entry, u32 *budget,
  6381. u32 base_flags, u32 mss, u32 vlan)
  6382. {
  6383. struct tg3 *tp = tnapi->tp;
  6384. struct sk_buff *new_skb, *skb = *pskb;
  6385. dma_addr_t new_addr = 0;
  6386. int ret = 0;
  6387. if (tg3_asic_rev(tp) != ASIC_REV_5701)
  6388. new_skb = skb_copy(skb, GFP_ATOMIC);
  6389. else {
  6390. int more_headroom = 4 - ((unsigned long)skb->data & 3);
  6391. new_skb = skb_copy_expand(skb,
  6392. skb_headroom(skb) + more_headroom,
  6393. skb_tailroom(skb), GFP_ATOMIC);
  6394. }
  6395. if (!new_skb) {
  6396. ret = -1;
  6397. } else {
  6398. /* New SKB is guaranteed to be linear. */
  6399. new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
  6400. PCI_DMA_TODEVICE);
  6401. /* Make sure the mapping succeeded */
  6402. if (pci_dma_mapping_error(tp->pdev, new_addr)) {
  6403. dev_kfree_skb_any(new_skb);
  6404. ret = -1;
  6405. } else {
  6406. u32 save_entry = *entry;
  6407. base_flags |= TXD_FLAG_END;
  6408. tnapi->tx_buffers[*entry].skb = new_skb;
  6409. dma_unmap_addr_set(&tnapi->tx_buffers[*entry],
  6410. mapping, new_addr);
  6411. if (tg3_tx_frag_set(tnapi, entry, budget, new_addr,
  6412. new_skb->len, base_flags,
  6413. mss, vlan)) {
  6414. tg3_tx_skb_unmap(tnapi, save_entry, -1);
  6415. dev_kfree_skb_any(new_skb);
  6416. ret = -1;
  6417. }
  6418. }
  6419. }
  6420. dev_kfree_skb_any(skb);
  6421. *pskb = new_skb;
  6422. return ret;
  6423. }
  6424. static bool tg3_tso_bug_gso_check(struct tg3_napi *tnapi, struct sk_buff *skb)
  6425. {
  6426. /* Check if we will never have enough descriptors,
  6427. * as gso_segs can be more than current ring size
  6428. */
  6429. return skb_shinfo(skb)->gso_segs < tnapi->tx_pending / 3;
  6430. }
  6431. static netdev_tx_t tg3_start_xmit(struct sk_buff *, struct net_device *);
  6432. /* Use GSO to workaround all TSO packets that meet HW bug conditions
  6433. * indicated in tg3_tx_frag_set()
  6434. */
  6435. static int tg3_tso_bug(struct tg3 *tp, struct tg3_napi *tnapi,
  6436. struct netdev_queue *txq, struct sk_buff *skb)
  6437. {
  6438. struct sk_buff *segs, *nskb;
  6439. u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
  6440. /* Estimate the number of fragments in the worst case */
  6441. if (unlikely(tg3_tx_avail(tnapi) <= frag_cnt_est)) {
  6442. netif_tx_stop_queue(txq);
  6443. /* netif_tx_stop_queue() must be done before checking
  6444. * checking tx index in tg3_tx_avail() below, because in
  6445. * tg3_tx(), we update tx index before checking for
  6446. * netif_tx_queue_stopped().
  6447. */
  6448. smp_mb();
  6449. if (tg3_tx_avail(tnapi) <= frag_cnt_est)
  6450. return NETDEV_TX_BUSY;
  6451. netif_tx_wake_queue(txq);
  6452. }
  6453. segs = skb_gso_segment(skb, tp->dev->features &
  6454. ~(NETIF_F_TSO | NETIF_F_TSO6));
  6455. if (IS_ERR(segs) || !segs)
  6456. goto tg3_tso_bug_end;
  6457. do {
  6458. nskb = segs;
  6459. segs = segs->next;
  6460. nskb->next = NULL;
  6461. tg3_start_xmit(nskb, tp->dev);
  6462. } while (segs);
  6463. tg3_tso_bug_end:
  6464. dev_kfree_skb_any(skb);
  6465. return NETDEV_TX_OK;
  6466. }
  6467. /* hard_start_xmit for all devices */
  6468. static netdev_tx_t tg3_start_xmit(struct sk_buff *skb, struct net_device *dev)
  6469. {
  6470. struct tg3 *tp = netdev_priv(dev);
  6471. u32 len, entry, base_flags, mss, vlan = 0;
  6472. u32 budget;
  6473. int i = -1, would_hit_hwbug;
  6474. dma_addr_t mapping;
  6475. struct tg3_napi *tnapi;
  6476. struct netdev_queue *txq;
  6477. unsigned int last;
  6478. struct iphdr *iph = NULL;
  6479. struct tcphdr *tcph = NULL;
  6480. __sum16 tcp_csum = 0, ip_csum = 0;
  6481. __be16 ip_tot_len = 0;
  6482. txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
  6483. tnapi = &tp->napi[skb_get_queue_mapping(skb)];
  6484. if (tg3_flag(tp, ENABLE_TSS))
  6485. tnapi++;
  6486. budget = tg3_tx_avail(tnapi);
  6487. /* We are running in BH disabled context with netif_tx_lock
  6488. * and TX reclaim runs via tp->napi.poll inside of a software
  6489. * interrupt. Furthermore, IRQ processing runs lockless so we have
  6490. * no IRQ context deadlocks to worry about either. Rejoice!
  6491. */
  6492. if (unlikely(budget <= (skb_shinfo(skb)->nr_frags + 1))) {
  6493. if (!netif_tx_queue_stopped(txq)) {
  6494. netif_tx_stop_queue(txq);
  6495. /* This is a hard error, log it. */
  6496. netdev_err(dev,
  6497. "BUG! Tx Ring full when queue awake!\n");
  6498. }
  6499. return NETDEV_TX_BUSY;
  6500. }
  6501. entry = tnapi->tx_prod;
  6502. base_flags = 0;
  6503. mss = skb_shinfo(skb)->gso_size;
  6504. if (mss) {
  6505. u32 tcp_opt_len, hdr_len;
  6506. if (skb_cow_head(skb, 0))
  6507. goto drop;
  6508. iph = ip_hdr(skb);
  6509. tcp_opt_len = tcp_optlen(skb);
  6510. hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb) - ETH_HLEN;
  6511. /* HW/FW can not correctly segment packets that have been
  6512. * vlan encapsulated.
  6513. */
  6514. if (skb->protocol == htons(ETH_P_8021Q) ||
  6515. skb->protocol == htons(ETH_P_8021AD)) {
  6516. if (tg3_tso_bug_gso_check(tnapi, skb))
  6517. return tg3_tso_bug(tp, tnapi, txq, skb);
  6518. goto drop;
  6519. }
  6520. if (!skb_is_gso_v6(skb)) {
  6521. if (unlikely((ETH_HLEN + hdr_len) > 80) &&
  6522. tg3_flag(tp, TSO_BUG)) {
  6523. if (tg3_tso_bug_gso_check(tnapi, skb))
  6524. return tg3_tso_bug(tp, tnapi, txq, skb);
  6525. goto drop;
  6526. }
  6527. ip_csum = iph->check;
  6528. ip_tot_len = iph->tot_len;
  6529. iph->check = 0;
  6530. iph->tot_len = htons(mss + hdr_len);
  6531. }
  6532. base_flags |= (TXD_FLAG_CPU_PRE_DMA |
  6533. TXD_FLAG_CPU_POST_DMA);
  6534. tcph = tcp_hdr(skb);
  6535. tcp_csum = tcph->check;
  6536. if (tg3_flag(tp, HW_TSO_1) ||
  6537. tg3_flag(tp, HW_TSO_2) ||
  6538. tg3_flag(tp, HW_TSO_3)) {
  6539. tcph->check = 0;
  6540. base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
  6541. } else {
  6542. tcph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
  6543. 0, IPPROTO_TCP, 0);
  6544. }
  6545. if (tg3_flag(tp, HW_TSO_3)) {
  6546. mss |= (hdr_len & 0xc) << 12;
  6547. if (hdr_len & 0x10)
  6548. base_flags |= 0x00000010;
  6549. base_flags |= (hdr_len & 0x3e0) << 5;
  6550. } else if (tg3_flag(tp, HW_TSO_2))
  6551. mss |= hdr_len << 9;
  6552. else if (tg3_flag(tp, HW_TSO_1) ||
  6553. tg3_asic_rev(tp) == ASIC_REV_5705) {
  6554. if (tcp_opt_len || iph->ihl > 5) {
  6555. int tsflags;
  6556. tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
  6557. mss |= (tsflags << 11);
  6558. }
  6559. } else {
  6560. if (tcp_opt_len || iph->ihl > 5) {
  6561. int tsflags;
  6562. tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
  6563. base_flags |= tsflags << 12;
  6564. }
  6565. }
  6566. } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
  6567. /* HW/FW can not correctly checksum packets that have been
  6568. * vlan encapsulated.
  6569. */
  6570. if (skb->protocol == htons(ETH_P_8021Q) ||
  6571. skb->protocol == htons(ETH_P_8021AD)) {
  6572. if (skb_checksum_help(skb))
  6573. goto drop;
  6574. } else {
  6575. base_flags |= TXD_FLAG_TCPUDP_CSUM;
  6576. }
  6577. }
  6578. if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
  6579. !mss && skb->len > VLAN_ETH_FRAME_LEN)
  6580. base_flags |= TXD_FLAG_JMB_PKT;
  6581. if (skb_vlan_tag_present(skb)) {
  6582. base_flags |= TXD_FLAG_VLAN;
  6583. vlan = skb_vlan_tag_get(skb);
  6584. }
  6585. if ((unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) &&
  6586. tg3_flag(tp, TX_TSTAMP_EN)) {
  6587. skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
  6588. base_flags |= TXD_FLAG_HWTSTAMP;
  6589. }
  6590. len = skb_headlen(skb);
  6591. mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
  6592. if (pci_dma_mapping_error(tp->pdev, mapping))
  6593. goto drop;
  6594. tnapi->tx_buffers[entry].skb = skb;
  6595. dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
  6596. would_hit_hwbug = 0;
  6597. if (tg3_flag(tp, 5701_DMA_BUG))
  6598. would_hit_hwbug = 1;
  6599. if (tg3_tx_frag_set(tnapi, &entry, &budget, mapping, len, base_flags |
  6600. ((skb_shinfo(skb)->nr_frags == 0) ? TXD_FLAG_END : 0),
  6601. mss, vlan)) {
  6602. would_hit_hwbug = 1;
  6603. } else if (skb_shinfo(skb)->nr_frags > 0) {
  6604. u32 tmp_mss = mss;
  6605. if (!tg3_flag(tp, HW_TSO_1) &&
  6606. !tg3_flag(tp, HW_TSO_2) &&
  6607. !tg3_flag(tp, HW_TSO_3))
  6608. tmp_mss = 0;
  6609. /* Now loop through additional data
  6610. * fragments, and queue them.
  6611. */
  6612. last = skb_shinfo(skb)->nr_frags - 1;
  6613. for (i = 0; i <= last; i++) {
  6614. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  6615. len = skb_frag_size(frag);
  6616. mapping = skb_frag_dma_map(&tp->pdev->dev, frag, 0,
  6617. len, DMA_TO_DEVICE);
  6618. tnapi->tx_buffers[entry].skb = NULL;
  6619. dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
  6620. mapping);
  6621. if (dma_mapping_error(&tp->pdev->dev, mapping))
  6622. goto dma_error;
  6623. if (!budget ||
  6624. tg3_tx_frag_set(tnapi, &entry, &budget, mapping,
  6625. len, base_flags |
  6626. ((i == last) ? TXD_FLAG_END : 0),
  6627. tmp_mss, vlan)) {
  6628. would_hit_hwbug = 1;
  6629. break;
  6630. }
  6631. }
  6632. }
  6633. if (would_hit_hwbug) {
  6634. tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, i);
  6635. if (mss && tg3_tso_bug_gso_check(tnapi, skb)) {
  6636. /* If it's a TSO packet, do GSO instead of
  6637. * allocating and copying to a large linear SKB
  6638. */
  6639. if (ip_tot_len) {
  6640. iph->check = ip_csum;
  6641. iph->tot_len = ip_tot_len;
  6642. }
  6643. tcph->check = tcp_csum;
  6644. return tg3_tso_bug(tp, tnapi, txq, skb);
  6645. }
  6646. /* If the workaround fails due to memory/mapping
  6647. * failure, silently drop this packet.
  6648. */
  6649. entry = tnapi->tx_prod;
  6650. budget = tg3_tx_avail(tnapi);
  6651. if (tigon3_dma_hwbug_workaround(tnapi, &skb, &entry, &budget,
  6652. base_flags, mss, vlan))
  6653. goto drop_nofree;
  6654. }
  6655. skb_tx_timestamp(skb);
  6656. netdev_tx_sent_queue(txq, skb->len);
  6657. /* Sync BD data before updating mailbox */
  6658. wmb();
  6659. tnapi->tx_prod = entry;
  6660. if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
  6661. netif_tx_stop_queue(txq);
  6662. /* netif_tx_stop_queue() must be done before checking
  6663. * checking tx index in tg3_tx_avail() below, because in
  6664. * tg3_tx(), we update tx index before checking for
  6665. * netif_tx_queue_stopped().
  6666. */
  6667. smp_mb();
  6668. if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
  6669. netif_tx_wake_queue(txq);
  6670. }
  6671. if (!skb->xmit_more || netif_xmit_stopped(txq)) {
  6672. /* Packets are ready, update Tx producer idx on card. */
  6673. tw32_tx_mbox(tnapi->prodmbox, entry);
  6674. mmiowb();
  6675. }
  6676. return NETDEV_TX_OK;
  6677. dma_error:
  6678. tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, --i);
  6679. tnapi->tx_buffers[tnapi->tx_prod].skb = NULL;
  6680. drop:
  6681. dev_kfree_skb_any(skb);
  6682. drop_nofree:
  6683. tp->tx_dropped++;
  6684. return NETDEV_TX_OK;
  6685. }
  6686. static void tg3_mac_loopback(struct tg3 *tp, bool enable)
  6687. {
  6688. if (enable) {
  6689. tp->mac_mode &= ~(MAC_MODE_HALF_DUPLEX |
  6690. MAC_MODE_PORT_MODE_MASK);
  6691. tp->mac_mode |= MAC_MODE_PORT_INT_LPBACK;
  6692. if (!tg3_flag(tp, 5705_PLUS))
  6693. tp->mac_mode |= MAC_MODE_LINK_POLARITY;
  6694. if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
  6695. tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
  6696. else
  6697. tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
  6698. } else {
  6699. tp->mac_mode &= ~MAC_MODE_PORT_INT_LPBACK;
  6700. if (tg3_flag(tp, 5705_PLUS) ||
  6701. (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) ||
  6702. tg3_asic_rev(tp) == ASIC_REV_5700)
  6703. tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
  6704. }
  6705. tw32(MAC_MODE, tp->mac_mode);
  6706. udelay(40);
  6707. }
  6708. static int tg3_phy_lpbk_set(struct tg3 *tp, u32 speed, bool extlpbk)
  6709. {
  6710. u32 val, bmcr, mac_mode, ptest = 0;
  6711. tg3_phy_toggle_apd(tp, false);
  6712. tg3_phy_toggle_automdix(tp, false);
  6713. if (extlpbk && tg3_phy_set_extloopbk(tp))
  6714. return -EIO;
  6715. bmcr = BMCR_FULLDPLX;
  6716. switch (speed) {
  6717. case SPEED_10:
  6718. break;
  6719. case SPEED_100:
  6720. bmcr |= BMCR_SPEED100;
  6721. break;
  6722. case SPEED_1000:
  6723. default:
  6724. if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
  6725. speed = SPEED_100;
  6726. bmcr |= BMCR_SPEED100;
  6727. } else {
  6728. speed = SPEED_1000;
  6729. bmcr |= BMCR_SPEED1000;
  6730. }
  6731. }
  6732. if (extlpbk) {
  6733. if (!(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
  6734. tg3_readphy(tp, MII_CTRL1000, &val);
  6735. val |= CTL1000_AS_MASTER |
  6736. CTL1000_ENABLE_MASTER;
  6737. tg3_writephy(tp, MII_CTRL1000, val);
  6738. } else {
  6739. ptest = MII_TG3_FET_PTEST_TRIM_SEL |
  6740. MII_TG3_FET_PTEST_TRIM_2;
  6741. tg3_writephy(tp, MII_TG3_FET_PTEST, ptest);
  6742. }
  6743. } else
  6744. bmcr |= BMCR_LOOPBACK;
  6745. tg3_writephy(tp, MII_BMCR, bmcr);
  6746. /* The write needs to be flushed for the FETs */
  6747. if (tp->phy_flags & TG3_PHYFLG_IS_FET)
  6748. tg3_readphy(tp, MII_BMCR, &bmcr);
  6749. udelay(40);
  6750. if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
  6751. tg3_asic_rev(tp) == ASIC_REV_5785) {
  6752. tg3_writephy(tp, MII_TG3_FET_PTEST, ptest |
  6753. MII_TG3_FET_PTEST_FRC_TX_LINK |
  6754. MII_TG3_FET_PTEST_FRC_TX_LOCK);
  6755. /* The write needs to be flushed for the AC131 */
  6756. tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
  6757. }
  6758. /* Reset to prevent losing 1st rx packet intermittently */
  6759. if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
  6760. tg3_flag(tp, 5780_CLASS)) {
  6761. tw32_f(MAC_RX_MODE, RX_MODE_RESET);
  6762. udelay(10);
  6763. tw32_f(MAC_RX_MODE, tp->rx_mode);
  6764. }
  6765. mac_mode = tp->mac_mode &
  6766. ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
  6767. if (speed == SPEED_1000)
  6768. mac_mode |= MAC_MODE_PORT_MODE_GMII;
  6769. else
  6770. mac_mode |= MAC_MODE_PORT_MODE_MII;
  6771. if (tg3_asic_rev(tp) == ASIC_REV_5700) {
  6772. u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
  6773. if (masked_phy_id == TG3_PHY_ID_BCM5401)
  6774. mac_mode &= ~MAC_MODE_LINK_POLARITY;
  6775. else if (masked_phy_id == TG3_PHY_ID_BCM5411)
  6776. mac_mode |= MAC_MODE_LINK_POLARITY;
  6777. tg3_writephy(tp, MII_TG3_EXT_CTRL,
  6778. MII_TG3_EXT_CTRL_LNK3_LED_MODE);
  6779. }
  6780. tw32(MAC_MODE, mac_mode);
  6781. udelay(40);
  6782. return 0;
  6783. }
  6784. static void tg3_set_loopback(struct net_device *dev, netdev_features_t features)
  6785. {
  6786. struct tg3 *tp = netdev_priv(dev);
  6787. if (features & NETIF_F_LOOPBACK) {
  6788. if (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK)
  6789. return;
  6790. spin_lock_bh(&tp->lock);
  6791. tg3_mac_loopback(tp, true);
  6792. netif_carrier_on(tp->dev);
  6793. spin_unlock_bh(&tp->lock);
  6794. netdev_info(dev, "Internal MAC loopback mode enabled.\n");
  6795. } else {
  6796. if (!(tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
  6797. return;
  6798. spin_lock_bh(&tp->lock);
  6799. tg3_mac_loopback(tp, false);
  6800. /* Force link status check */
  6801. tg3_setup_phy(tp, true);
  6802. spin_unlock_bh(&tp->lock);
  6803. netdev_info(dev, "Internal MAC loopback mode disabled.\n");
  6804. }
  6805. }
  6806. static netdev_features_t tg3_fix_features(struct net_device *dev,
  6807. netdev_features_t features)
  6808. {
  6809. struct tg3 *tp = netdev_priv(dev);
  6810. if (dev->mtu > ETH_DATA_LEN && tg3_flag(tp, 5780_CLASS))
  6811. features &= ~NETIF_F_ALL_TSO;
  6812. return features;
  6813. }
  6814. static int tg3_set_features(struct net_device *dev, netdev_features_t features)
  6815. {
  6816. netdev_features_t changed = dev->features ^ features;
  6817. if ((changed & NETIF_F_LOOPBACK) && netif_running(dev))
  6818. tg3_set_loopback(dev, features);
  6819. return 0;
  6820. }
  6821. static void tg3_rx_prodring_free(struct tg3 *tp,
  6822. struct tg3_rx_prodring_set *tpr)
  6823. {
  6824. int i;
  6825. if (tpr != &tp->napi[0].prodring) {
  6826. for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
  6827. i = (i + 1) & tp->rx_std_ring_mask)
  6828. tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
  6829. tp->rx_pkt_map_sz);
  6830. if (tg3_flag(tp, JUMBO_CAPABLE)) {
  6831. for (i = tpr->rx_jmb_cons_idx;
  6832. i != tpr->rx_jmb_prod_idx;
  6833. i = (i + 1) & tp->rx_jmb_ring_mask) {
  6834. tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
  6835. TG3_RX_JMB_MAP_SZ);
  6836. }
  6837. }
  6838. return;
  6839. }
  6840. for (i = 0; i <= tp->rx_std_ring_mask; i++)
  6841. tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
  6842. tp->rx_pkt_map_sz);
  6843. if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
  6844. for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
  6845. tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
  6846. TG3_RX_JMB_MAP_SZ);
  6847. }
  6848. }
  6849. /* Initialize rx rings for packet processing.
  6850. *
  6851. * The chip has been shut down and the driver detached from
  6852. * the networking, so no interrupts or new tx packets will
  6853. * end up in the driver. tp->{tx,}lock are held and thus
  6854. * we may not sleep.
  6855. */
  6856. static int tg3_rx_prodring_alloc(struct tg3 *tp,
  6857. struct tg3_rx_prodring_set *tpr)
  6858. {
  6859. u32 i, rx_pkt_dma_sz;
  6860. tpr->rx_std_cons_idx = 0;
  6861. tpr->rx_std_prod_idx = 0;
  6862. tpr->rx_jmb_cons_idx = 0;
  6863. tpr->rx_jmb_prod_idx = 0;
  6864. if (tpr != &tp->napi[0].prodring) {
  6865. memset(&tpr->rx_std_buffers[0], 0,
  6866. TG3_RX_STD_BUFF_RING_SIZE(tp));
  6867. if (tpr->rx_jmb_buffers)
  6868. memset(&tpr->rx_jmb_buffers[0], 0,
  6869. TG3_RX_JMB_BUFF_RING_SIZE(tp));
  6870. goto done;
  6871. }
  6872. /* Zero out all descriptors. */
  6873. memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
  6874. rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
  6875. if (tg3_flag(tp, 5780_CLASS) &&
  6876. tp->dev->mtu > ETH_DATA_LEN)
  6877. rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
  6878. tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
  6879. /* Initialize invariants of the rings, we only set this
  6880. * stuff once. This works because the card does not
  6881. * write into the rx buffer posting rings.
  6882. */
  6883. for (i = 0; i <= tp->rx_std_ring_mask; i++) {
  6884. struct tg3_rx_buffer_desc *rxd;
  6885. rxd = &tpr->rx_std[i];
  6886. rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
  6887. rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
  6888. rxd->opaque = (RXD_OPAQUE_RING_STD |
  6889. (i << RXD_OPAQUE_INDEX_SHIFT));
  6890. }
  6891. /* Now allocate fresh SKBs for each rx ring. */
  6892. for (i = 0; i < tp->rx_pending; i++) {
  6893. unsigned int frag_size;
  6894. if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_STD, i,
  6895. &frag_size) < 0) {
  6896. netdev_warn(tp->dev,
  6897. "Using a smaller RX standard ring. Only "
  6898. "%d out of %d buffers were allocated "
  6899. "successfully\n", i, tp->rx_pending);
  6900. if (i == 0)
  6901. goto initfail;
  6902. tp->rx_pending = i;
  6903. break;
  6904. }
  6905. }
  6906. if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
  6907. goto done;
  6908. memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
  6909. if (!tg3_flag(tp, JUMBO_RING_ENABLE))
  6910. goto done;
  6911. for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
  6912. struct tg3_rx_buffer_desc *rxd;
  6913. rxd = &tpr->rx_jmb[i].std;
  6914. rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
  6915. rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
  6916. RXD_FLAG_JUMBO;
  6917. rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
  6918. (i << RXD_OPAQUE_INDEX_SHIFT));
  6919. }
  6920. for (i = 0; i < tp->rx_jumbo_pending; i++) {
  6921. unsigned int frag_size;
  6922. if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_JUMBO, i,
  6923. &frag_size) < 0) {
  6924. netdev_warn(tp->dev,
  6925. "Using a smaller RX jumbo ring. Only %d "
  6926. "out of %d buffers were allocated "
  6927. "successfully\n", i, tp->rx_jumbo_pending);
  6928. if (i == 0)
  6929. goto initfail;
  6930. tp->rx_jumbo_pending = i;
  6931. break;
  6932. }
  6933. }
  6934. done:
  6935. return 0;
  6936. initfail:
  6937. tg3_rx_prodring_free(tp, tpr);
  6938. return -ENOMEM;
  6939. }
  6940. static void tg3_rx_prodring_fini(struct tg3 *tp,
  6941. struct tg3_rx_prodring_set *tpr)
  6942. {
  6943. kfree(tpr->rx_std_buffers);
  6944. tpr->rx_std_buffers = NULL;
  6945. kfree(tpr->rx_jmb_buffers);
  6946. tpr->rx_jmb_buffers = NULL;
  6947. if (tpr->rx_std) {
  6948. dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
  6949. tpr->rx_std, tpr->rx_std_mapping);
  6950. tpr->rx_std = NULL;
  6951. }
  6952. if (tpr->rx_jmb) {
  6953. dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
  6954. tpr->rx_jmb, tpr->rx_jmb_mapping);
  6955. tpr->rx_jmb = NULL;
  6956. }
  6957. }
  6958. static int tg3_rx_prodring_init(struct tg3 *tp,
  6959. struct tg3_rx_prodring_set *tpr)
  6960. {
  6961. tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
  6962. GFP_KERNEL);
  6963. if (!tpr->rx_std_buffers)
  6964. return -ENOMEM;
  6965. tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
  6966. TG3_RX_STD_RING_BYTES(tp),
  6967. &tpr->rx_std_mapping,
  6968. GFP_KERNEL);
  6969. if (!tpr->rx_std)
  6970. goto err_out;
  6971. if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
  6972. tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
  6973. GFP_KERNEL);
  6974. if (!tpr->rx_jmb_buffers)
  6975. goto err_out;
  6976. tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
  6977. TG3_RX_JMB_RING_BYTES(tp),
  6978. &tpr->rx_jmb_mapping,
  6979. GFP_KERNEL);
  6980. if (!tpr->rx_jmb)
  6981. goto err_out;
  6982. }
  6983. return 0;
  6984. err_out:
  6985. tg3_rx_prodring_fini(tp, tpr);
  6986. return -ENOMEM;
  6987. }
  6988. /* Free up pending packets in all rx/tx rings.
  6989. *
  6990. * The chip has been shut down and the driver detached from
  6991. * the networking, so no interrupts or new tx packets will
  6992. * end up in the driver. tp->{tx,}lock is not held and we are not
  6993. * in an interrupt context and thus may sleep.
  6994. */
  6995. static void tg3_free_rings(struct tg3 *tp)
  6996. {
  6997. int i, j;
  6998. for (j = 0; j < tp->irq_cnt; j++) {
  6999. struct tg3_napi *tnapi = &tp->napi[j];
  7000. tg3_rx_prodring_free(tp, &tnapi->prodring);
  7001. if (!tnapi->tx_buffers)
  7002. continue;
  7003. for (i = 0; i < TG3_TX_RING_SIZE; i++) {
  7004. struct sk_buff *skb = tnapi->tx_buffers[i].skb;
  7005. if (!skb)
  7006. continue;
  7007. tg3_tx_skb_unmap(tnapi, i,
  7008. skb_shinfo(skb)->nr_frags - 1);
  7009. dev_kfree_skb_any(skb);
  7010. }
  7011. netdev_tx_reset_queue(netdev_get_tx_queue(tp->dev, j));
  7012. }
  7013. }
  7014. /* Initialize tx/rx rings for packet processing.
  7015. *
  7016. * The chip has been shut down and the driver detached from
  7017. * the networking, so no interrupts or new tx packets will
  7018. * end up in the driver. tp->{tx,}lock are held and thus
  7019. * we may not sleep.
  7020. */
  7021. static int tg3_init_rings(struct tg3 *tp)
  7022. {
  7023. int i;
  7024. /* Free up all the SKBs. */
  7025. tg3_free_rings(tp);
  7026. for (i = 0; i < tp->irq_cnt; i++) {
  7027. struct tg3_napi *tnapi = &tp->napi[i];
  7028. tnapi->last_tag = 0;
  7029. tnapi->last_irq_tag = 0;
  7030. tnapi->hw_status->status = 0;
  7031. tnapi->hw_status->status_tag = 0;
  7032. memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
  7033. tnapi->tx_prod = 0;
  7034. tnapi->tx_cons = 0;
  7035. if (tnapi->tx_ring)
  7036. memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
  7037. tnapi->rx_rcb_ptr = 0;
  7038. if (tnapi->rx_rcb)
  7039. memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
  7040. if (tnapi->prodring.rx_std &&
  7041. tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
  7042. tg3_free_rings(tp);
  7043. return -ENOMEM;
  7044. }
  7045. }
  7046. return 0;
  7047. }
  7048. static void tg3_mem_tx_release(struct tg3 *tp)
  7049. {
  7050. int i;
  7051. for (i = 0; i < tp->irq_max; i++) {
  7052. struct tg3_napi *tnapi = &tp->napi[i];
  7053. if (tnapi->tx_ring) {
  7054. dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
  7055. tnapi->tx_ring, tnapi->tx_desc_mapping);
  7056. tnapi->tx_ring = NULL;
  7057. }
  7058. kfree(tnapi->tx_buffers);
  7059. tnapi->tx_buffers = NULL;
  7060. }
  7061. }
  7062. static int tg3_mem_tx_acquire(struct tg3 *tp)
  7063. {
  7064. int i;
  7065. struct tg3_napi *tnapi = &tp->napi[0];
  7066. /* If multivector TSS is enabled, vector 0 does not handle
  7067. * tx interrupts. Don't allocate any resources for it.
  7068. */
  7069. if (tg3_flag(tp, ENABLE_TSS))
  7070. tnapi++;
  7071. for (i = 0; i < tp->txq_cnt; i++, tnapi++) {
  7072. tnapi->tx_buffers = kzalloc(sizeof(struct tg3_tx_ring_info) *
  7073. TG3_TX_RING_SIZE, GFP_KERNEL);
  7074. if (!tnapi->tx_buffers)
  7075. goto err_out;
  7076. tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
  7077. TG3_TX_RING_BYTES,
  7078. &tnapi->tx_desc_mapping,
  7079. GFP_KERNEL);
  7080. if (!tnapi->tx_ring)
  7081. goto err_out;
  7082. }
  7083. return 0;
  7084. err_out:
  7085. tg3_mem_tx_release(tp);
  7086. return -ENOMEM;
  7087. }
  7088. static void tg3_mem_rx_release(struct tg3 *tp)
  7089. {
  7090. int i;
  7091. for (i = 0; i < tp->irq_max; i++) {
  7092. struct tg3_napi *tnapi = &tp->napi[i];
  7093. tg3_rx_prodring_fini(tp, &tnapi->prodring);
  7094. if (!tnapi->rx_rcb)
  7095. continue;
  7096. dma_free_coherent(&tp->pdev->dev,
  7097. TG3_RX_RCB_RING_BYTES(tp),
  7098. tnapi->rx_rcb,
  7099. tnapi->rx_rcb_mapping);
  7100. tnapi->rx_rcb = NULL;
  7101. }
  7102. }
  7103. static int tg3_mem_rx_acquire(struct tg3 *tp)
  7104. {
  7105. unsigned int i, limit;
  7106. limit = tp->rxq_cnt;
  7107. /* If RSS is enabled, we need a (dummy) producer ring
  7108. * set on vector zero. This is the true hw prodring.
  7109. */
  7110. if (tg3_flag(tp, ENABLE_RSS))
  7111. limit++;
  7112. for (i = 0; i < limit; i++) {
  7113. struct tg3_napi *tnapi = &tp->napi[i];
  7114. if (tg3_rx_prodring_init(tp, &tnapi->prodring))
  7115. goto err_out;
  7116. /* If multivector RSS is enabled, vector 0
  7117. * does not handle rx or tx interrupts.
  7118. * Don't allocate any resources for it.
  7119. */
  7120. if (!i && tg3_flag(tp, ENABLE_RSS))
  7121. continue;
  7122. tnapi->rx_rcb = dma_zalloc_coherent(&tp->pdev->dev,
  7123. TG3_RX_RCB_RING_BYTES(tp),
  7124. &tnapi->rx_rcb_mapping,
  7125. GFP_KERNEL);
  7126. if (!tnapi->rx_rcb)
  7127. goto err_out;
  7128. }
  7129. return 0;
  7130. err_out:
  7131. tg3_mem_rx_release(tp);
  7132. return -ENOMEM;
  7133. }
  7134. /*
  7135. * Must not be invoked with interrupt sources disabled and
  7136. * the hardware shutdown down.
  7137. */
  7138. static void tg3_free_consistent(struct tg3 *tp)
  7139. {
  7140. int i;
  7141. for (i = 0; i < tp->irq_cnt; i++) {
  7142. struct tg3_napi *tnapi = &tp->napi[i];
  7143. if (tnapi->hw_status) {
  7144. dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
  7145. tnapi->hw_status,
  7146. tnapi->status_mapping);
  7147. tnapi->hw_status = NULL;
  7148. }
  7149. }
  7150. tg3_mem_rx_release(tp);
  7151. tg3_mem_tx_release(tp);
  7152. /* tp->hw_stats can be referenced safely:
  7153. * 1. under rtnl_lock
  7154. * 2. or under tp->lock if TG3_FLAG_INIT_COMPLETE is set.
  7155. */
  7156. if (tp->hw_stats) {
  7157. dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
  7158. tp->hw_stats, tp->stats_mapping);
  7159. tp->hw_stats = NULL;
  7160. }
  7161. }
  7162. /*
  7163. * Must not be invoked with interrupt sources disabled and
  7164. * the hardware shutdown down. Can sleep.
  7165. */
  7166. static int tg3_alloc_consistent(struct tg3 *tp)
  7167. {
  7168. int i;
  7169. tp->hw_stats = dma_zalloc_coherent(&tp->pdev->dev,
  7170. sizeof(struct tg3_hw_stats),
  7171. &tp->stats_mapping, GFP_KERNEL);
  7172. if (!tp->hw_stats)
  7173. goto err_out;
  7174. for (i = 0; i < tp->irq_cnt; i++) {
  7175. struct tg3_napi *tnapi = &tp->napi[i];
  7176. struct tg3_hw_status *sblk;
  7177. tnapi->hw_status = dma_zalloc_coherent(&tp->pdev->dev,
  7178. TG3_HW_STATUS_SIZE,
  7179. &tnapi->status_mapping,
  7180. GFP_KERNEL);
  7181. if (!tnapi->hw_status)
  7182. goto err_out;
  7183. sblk = tnapi->hw_status;
  7184. if (tg3_flag(tp, ENABLE_RSS)) {
  7185. u16 *prodptr = NULL;
  7186. /*
  7187. * When RSS is enabled, the status block format changes
  7188. * slightly. The "rx_jumbo_consumer", "reserved",
  7189. * and "rx_mini_consumer" members get mapped to the
  7190. * other three rx return ring producer indexes.
  7191. */
  7192. switch (i) {
  7193. case 1:
  7194. prodptr = &sblk->idx[0].rx_producer;
  7195. break;
  7196. case 2:
  7197. prodptr = &sblk->rx_jumbo_consumer;
  7198. break;
  7199. case 3:
  7200. prodptr = &sblk->reserved;
  7201. break;
  7202. case 4:
  7203. prodptr = &sblk->rx_mini_consumer;
  7204. break;
  7205. }
  7206. tnapi->rx_rcb_prod_idx = prodptr;
  7207. } else {
  7208. tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
  7209. }
  7210. }
  7211. if (tg3_mem_tx_acquire(tp) || tg3_mem_rx_acquire(tp))
  7212. goto err_out;
  7213. return 0;
  7214. err_out:
  7215. tg3_free_consistent(tp);
  7216. return -ENOMEM;
  7217. }
  7218. #define MAX_WAIT_CNT 1000
  7219. /* To stop a block, clear the enable bit and poll till it
  7220. * clears. tp->lock is held.
  7221. */
  7222. static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, bool silent)
  7223. {
  7224. unsigned int i;
  7225. u32 val;
  7226. if (tg3_flag(tp, 5705_PLUS)) {
  7227. switch (ofs) {
  7228. case RCVLSC_MODE:
  7229. case DMAC_MODE:
  7230. case MBFREE_MODE:
  7231. case BUFMGR_MODE:
  7232. case MEMARB_MODE:
  7233. /* We can't enable/disable these bits of the
  7234. * 5705/5750, just say success.
  7235. */
  7236. return 0;
  7237. default:
  7238. break;
  7239. }
  7240. }
  7241. val = tr32(ofs);
  7242. val &= ~enable_bit;
  7243. tw32_f(ofs, val);
  7244. for (i = 0; i < MAX_WAIT_CNT; i++) {
  7245. if (pci_channel_offline(tp->pdev)) {
  7246. dev_err(&tp->pdev->dev,
  7247. "tg3_stop_block device offline, "
  7248. "ofs=%lx enable_bit=%x\n",
  7249. ofs, enable_bit);
  7250. return -ENODEV;
  7251. }
  7252. udelay(100);
  7253. val = tr32(ofs);
  7254. if ((val & enable_bit) == 0)
  7255. break;
  7256. }
  7257. if (i == MAX_WAIT_CNT && !silent) {
  7258. dev_err(&tp->pdev->dev,
  7259. "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
  7260. ofs, enable_bit);
  7261. return -ENODEV;
  7262. }
  7263. return 0;
  7264. }
  7265. /* tp->lock is held. */
  7266. static int tg3_abort_hw(struct tg3 *tp, bool silent)
  7267. {
  7268. int i, err;
  7269. tg3_disable_ints(tp);
  7270. if (pci_channel_offline(tp->pdev)) {
  7271. tp->rx_mode &= ~(RX_MODE_ENABLE | TX_MODE_ENABLE);
  7272. tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
  7273. err = -ENODEV;
  7274. goto err_no_dev;
  7275. }
  7276. tp->rx_mode &= ~RX_MODE_ENABLE;
  7277. tw32_f(MAC_RX_MODE, tp->rx_mode);
  7278. udelay(10);
  7279. err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
  7280. err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
  7281. err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
  7282. err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
  7283. err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
  7284. err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
  7285. err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
  7286. err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
  7287. err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
  7288. err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
  7289. err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
  7290. err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
  7291. err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
  7292. tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
  7293. tw32_f(MAC_MODE, tp->mac_mode);
  7294. udelay(40);
  7295. tp->tx_mode &= ~TX_MODE_ENABLE;
  7296. tw32_f(MAC_TX_MODE, tp->tx_mode);
  7297. for (i = 0; i < MAX_WAIT_CNT; i++) {
  7298. udelay(100);
  7299. if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
  7300. break;
  7301. }
  7302. if (i >= MAX_WAIT_CNT) {
  7303. dev_err(&tp->pdev->dev,
  7304. "%s timed out, TX_MODE_ENABLE will not clear "
  7305. "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
  7306. err |= -ENODEV;
  7307. }
  7308. err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
  7309. err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
  7310. err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
  7311. tw32(FTQ_RESET, 0xffffffff);
  7312. tw32(FTQ_RESET, 0x00000000);
  7313. err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
  7314. err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
  7315. err_no_dev:
  7316. for (i = 0; i < tp->irq_cnt; i++) {
  7317. struct tg3_napi *tnapi = &tp->napi[i];
  7318. if (tnapi->hw_status)
  7319. memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
  7320. }
  7321. return err;
  7322. }
  7323. /* Save PCI command register before chip reset */
  7324. static void tg3_save_pci_state(struct tg3 *tp)
  7325. {
  7326. pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
  7327. }
  7328. /* Restore PCI state after chip reset */
  7329. static void tg3_restore_pci_state(struct tg3 *tp)
  7330. {
  7331. u32 val;
  7332. /* Re-enable indirect register accesses. */
  7333. pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
  7334. tp->misc_host_ctrl);
  7335. /* Set MAX PCI retry to zero. */
  7336. val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
  7337. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0 &&
  7338. tg3_flag(tp, PCIX_MODE))
  7339. val |= PCISTATE_RETRY_SAME_DMA;
  7340. /* Allow reads and writes to the APE register and memory space. */
  7341. if (tg3_flag(tp, ENABLE_APE))
  7342. val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
  7343. PCISTATE_ALLOW_APE_SHMEM_WR |
  7344. PCISTATE_ALLOW_APE_PSPACE_WR;
  7345. pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
  7346. pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
  7347. if (!tg3_flag(tp, PCI_EXPRESS)) {
  7348. pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
  7349. tp->pci_cacheline_sz);
  7350. pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
  7351. tp->pci_lat_timer);
  7352. }
  7353. /* Make sure PCI-X relaxed ordering bit is clear. */
  7354. if (tg3_flag(tp, PCIX_MODE)) {
  7355. u16 pcix_cmd;
  7356. pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
  7357. &pcix_cmd);
  7358. pcix_cmd &= ~PCI_X_CMD_ERO;
  7359. pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
  7360. pcix_cmd);
  7361. }
  7362. if (tg3_flag(tp, 5780_CLASS)) {
  7363. /* Chip reset on 5780 will reset MSI enable bit,
  7364. * so need to restore it.
  7365. */
  7366. if (tg3_flag(tp, USING_MSI)) {
  7367. u16 ctrl;
  7368. pci_read_config_word(tp->pdev,
  7369. tp->msi_cap + PCI_MSI_FLAGS,
  7370. &ctrl);
  7371. pci_write_config_word(tp->pdev,
  7372. tp->msi_cap + PCI_MSI_FLAGS,
  7373. ctrl | PCI_MSI_FLAGS_ENABLE);
  7374. val = tr32(MSGINT_MODE);
  7375. tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
  7376. }
  7377. }
  7378. }
  7379. static void tg3_override_clk(struct tg3 *tp)
  7380. {
  7381. u32 val;
  7382. switch (tg3_asic_rev(tp)) {
  7383. case ASIC_REV_5717:
  7384. val = tr32(TG3_CPMU_CLCK_ORIDE_ENABLE);
  7385. tw32(TG3_CPMU_CLCK_ORIDE_ENABLE, val |
  7386. TG3_CPMU_MAC_ORIDE_ENABLE);
  7387. break;
  7388. case ASIC_REV_5719:
  7389. case ASIC_REV_5720:
  7390. tw32(TG3_CPMU_CLCK_ORIDE, CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
  7391. break;
  7392. default:
  7393. return;
  7394. }
  7395. }
  7396. static void tg3_restore_clk(struct tg3 *tp)
  7397. {
  7398. u32 val;
  7399. switch (tg3_asic_rev(tp)) {
  7400. case ASIC_REV_5717:
  7401. val = tr32(TG3_CPMU_CLCK_ORIDE_ENABLE);
  7402. tw32(TG3_CPMU_CLCK_ORIDE_ENABLE,
  7403. val & ~TG3_CPMU_MAC_ORIDE_ENABLE);
  7404. break;
  7405. case ASIC_REV_5719:
  7406. case ASIC_REV_5720:
  7407. val = tr32(TG3_CPMU_CLCK_ORIDE);
  7408. tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
  7409. break;
  7410. default:
  7411. return;
  7412. }
  7413. }
  7414. /* tp->lock is held. */
  7415. static int tg3_chip_reset(struct tg3 *tp)
  7416. __releases(tp->lock)
  7417. __acquires(tp->lock)
  7418. {
  7419. u32 val;
  7420. void (*write_op)(struct tg3 *, u32, u32);
  7421. int i, err;
  7422. if (!pci_device_is_present(tp->pdev))
  7423. return -ENODEV;
  7424. tg3_nvram_lock(tp);
  7425. tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
  7426. /* No matching tg3_nvram_unlock() after this because
  7427. * chip reset below will undo the nvram lock.
  7428. */
  7429. tp->nvram_lock_cnt = 0;
  7430. /* GRC_MISC_CFG core clock reset will clear the memory
  7431. * enable bit in PCI register 4 and the MSI enable bit
  7432. * on some chips, so we save relevant registers here.
  7433. */
  7434. tg3_save_pci_state(tp);
  7435. if (tg3_asic_rev(tp) == ASIC_REV_5752 ||
  7436. tg3_flag(tp, 5755_PLUS))
  7437. tw32(GRC_FASTBOOT_PC, 0);
  7438. /*
  7439. * We must avoid the readl() that normally takes place.
  7440. * It locks machines, causes machine checks, and other
  7441. * fun things. So, temporarily disable the 5701
  7442. * hardware workaround, while we do the reset.
  7443. */
  7444. write_op = tp->write32;
  7445. if (write_op == tg3_write_flush_reg32)
  7446. tp->write32 = tg3_write32;
  7447. /* Prevent the irq handler from reading or writing PCI registers
  7448. * during chip reset when the memory enable bit in the PCI command
  7449. * register may be cleared. The chip does not generate interrupt
  7450. * at this time, but the irq handler may still be called due to irq
  7451. * sharing or irqpoll.
  7452. */
  7453. tg3_flag_set(tp, CHIP_RESETTING);
  7454. for (i = 0; i < tp->irq_cnt; i++) {
  7455. struct tg3_napi *tnapi = &tp->napi[i];
  7456. if (tnapi->hw_status) {
  7457. tnapi->hw_status->status = 0;
  7458. tnapi->hw_status->status_tag = 0;
  7459. }
  7460. tnapi->last_tag = 0;
  7461. tnapi->last_irq_tag = 0;
  7462. }
  7463. smp_mb();
  7464. tg3_full_unlock(tp);
  7465. for (i = 0; i < tp->irq_cnt; i++)
  7466. synchronize_irq(tp->napi[i].irq_vec);
  7467. tg3_full_lock(tp, 0);
  7468. if (tg3_asic_rev(tp) == ASIC_REV_57780) {
  7469. val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
  7470. tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
  7471. }
  7472. /* do the reset */
  7473. val = GRC_MISC_CFG_CORECLK_RESET;
  7474. if (tg3_flag(tp, PCI_EXPRESS)) {
  7475. /* Force PCIe 1.0a mode */
  7476. if (tg3_asic_rev(tp) != ASIC_REV_5785 &&
  7477. !tg3_flag(tp, 57765_PLUS) &&
  7478. tr32(TG3_PCIE_PHY_TSTCTL) ==
  7479. (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
  7480. tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
  7481. if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0) {
  7482. tw32(GRC_MISC_CFG, (1 << 29));
  7483. val |= (1 << 29);
  7484. }
  7485. }
  7486. if (tg3_asic_rev(tp) == ASIC_REV_5906) {
  7487. tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
  7488. tw32(GRC_VCPU_EXT_CTRL,
  7489. tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
  7490. }
  7491. /* Set the clock to the highest frequency to avoid timeouts. With link
  7492. * aware mode, the clock speed could be slow and bootcode does not
  7493. * complete within the expected time. Override the clock to allow the
  7494. * bootcode to finish sooner and then restore it.
  7495. */
  7496. tg3_override_clk(tp);
  7497. /* Manage gphy power for all CPMU absent PCIe devices. */
  7498. if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, CPMU_PRESENT))
  7499. val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
  7500. tw32(GRC_MISC_CFG, val);
  7501. /* restore 5701 hardware bug workaround write method */
  7502. tp->write32 = write_op;
  7503. /* Unfortunately, we have to delay before the PCI read back.
  7504. * Some 575X chips even will not respond to a PCI cfg access
  7505. * when the reset command is given to the chip.
  7506. *
  7507. * How do these hardware designers expect things to work
  7508. * properly if the PCI write is posted for a long period
  7509. * of time? It is always necessary to have some method by
  7510. * which a register read back can occur to push the write
  7511. * out which does the reset.
  7512. *
  7513. * For most tg3 variants the trick below was working.
  7514. * Ho hum...
  7515. */
  7516. udelay(120);
  7517. /* Flush PCI posted writes. The normal MMIO registers
  7518. * are inaccessible at this time so this is the only
  7519. * way to make this reliably (actually, this is no longer
  7520. * the case, see above). I tried to use indirect
  7521. * register read/write but this upset some 5701 variants.
  7522. */
  7523. pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
  7524. udelay(120);
  7525. if (tg3_flag(tp, PCI_EXPRESS) && pci_is_pcie(tp->pdev)) {
  7526. u16 val16;
  7527. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A0) {
  7528. int j;
  7529. u32 cfg_val;
  7530. /* Wait for link training to complete. */
  7531. for (j = 0; j < 5000; j++)
  7532. udelay(100);
  7533. pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
  7534. pci_write_config_dword(tp->pdev, 0xc4,
  7535. cfg_val | (1 << 15));
  7536. }
  7537. /* Clear the "no snoop" and "relaxed ordering" bits. */
  7538. val16 = PCI_EXP_DEVCTL_RELAX_EN | PCI_EXP_DEVCTL_NOSNOOP_EN;
  7539. /*
  7540. * Older PCIe devices only support the 128 byte
  7541. * MPS setting. Enforce the restriction.
  7542. */
  7543. if (!tg3_flag(tp, CPMU_PRESENT))
  7544. val16 |= PCI_EXP_DEVCTL_PAYLOAD;
  7545. pcie_capability_clear_word(tp->pdev, PCI_EXP_DEVCTL, val16);
  7546. /* Clear error status */
  7547. pcie_capability_write_word(tp->pdev, PCI_EXP_DEVSTA,
  7548. PCI_EXP_DEVSTA_CED |
  7549. PCI_EXP_DEVSTA_NFED |
  7550. PCI_EXP_DEVSTA_FED |
  7551. PCI_EXP_DEVSTA_URD);
  7552. }
  7553. tg3_restore_pci_state(tp);
  7554. tg3_flag_clear(tp, CHIP_RESETTING);
  7555. tg3_flag_clear(tp, ERROR_PROCESSED);
  7556. val = 0;
  7557. if (tg3_flag(tp, 5780_CLASS))
  7558. val = tr32(MEMARB_MODE);
  7559. tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
  7560. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A3) {
  7561. tg3_stop_fw(tp);
  7562. tw32(0x5000, 0x400);
  7563. }
  7564. if (tg3_flag(tp, IS_SSB_CORE)) {
  7565. /*
  7566. * BCM4785: In order to avoid repercussions from using
  7567. * potentially defective internal ROM, stop the Rx RISC CPU,
  7568. * which is not required.
  7569. */
  7570. tg3_stop_fw(tp);
  7571. tg3_halt_cpu(tp, RX_CPU_BASE);
  7572. }
  7573. err = tg3_poll_fw(tp);
  7574. if (err)
  7575. return err;
  7576. tw32(GRC_MODE, tp->grc_mode);
  7577. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A0) {
  7578. val = tr32(0xc4);
  7579. tw32(0xc4, val | (1 << 15));
  7580. }
  7581. if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
  7582. tg3_asic_rev(tp) == ASIC_REV_5705) {
  7583. tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
  7584. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A0)
  7585. tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
  7586. tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
  7587. }
  7588. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
  7589. tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
  7590. val = tp->mac_mode;
  7591. } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
  7592. tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
  7593. val = tp->mac_mode;
  7594. } else
  7595. val = 0;
  7596. tw32_f(MAC_MODE, val);
  7597. udelay(40);
  7598. tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
  7599. tg3_mdio_start(tp);
  7600. if (tg3_flag(tp, PCI_EXPRESS) &&
  7601. tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0 &&
  7602. tg3_asic_rev(tp) != ASIC_REV_5785 &&
  7603. !tg3_flag(tp, 57765_PLUS)) {
  7604. val = tr32(0x7c00);
  7605. tw32(0x7c00, val | (1 << 25));
  7606. }
  7607. tg3_restore_clk(tp);
  7608. /* Increase the core clock speed to fix tx timeout issue for 5762
  7609. * with 100Mbps link speed.
  7610. */
  7611. if (tg3_asic_rev(tp) == ASIC_REV_5762) {
  7612. val = tr32(TG3_CPMU_CLCK_ORIDE_ENABLE);
  7613. tw32(TG3_CPMU_CLCK_ORIDE_ENABLE, val |
  7614. TG3_CPMU_MAC_ORIDE_ENABLE);
  7615. }
  7616. /* Reprobe ASF enable state. */
  7617. tg3_flag_clear(tp, ENABLE_ASF);
  7618. tp->phy_flags &= ~(TG3_PHYFLG_1G_ON_VAUX_OK |
  7619. TG3_PHYFLG_KEEP_LINK_ON_PWRDN);
  7620. tg3_flag_clear(tp, ASF_NEW_HANDSHAKE);
  7621. tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
  7622. if (val == NIC_SRAM_DATA_SIG_MAGIC) {
  7623. u32 nic_cfg;
  7624. tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
  7625. if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
  7626. tg3_flag_set(tp, ENABLE_ASF);
  7627. tp->last_event_jiffies = jiffies;
  7628. if (tg3_flag(tp, 5750_PLUS))
  7629. tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
  7630. tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &nic_cfg);
  7631. if (nic_cfg & NIC_SRAM_1G_ON_VAUX_OK)
  7632. tp->phy_flags |= TG3_PHYFLG_1G_ON_VAUX_OK;
  7633. if (nic_cfg & NIC_SRAM_LNK_FLAP_AVOID)
  7634. tp->phy_flags |= TG3_PHYFLG_KEEP_LINK_ON_PWRDN;
  7635. }
  7636. }
  7637. return 0;
  7638. }
  7639. static void tg3_get_nstats(struct tg3 *, struct rtnl_link_stats64 *);
  7640. static void tg3_get_estats(struct tg3 *, struct tg3_ethtool_stats *);
  7641. static void __tg3_set_rx_mode(struct net_device *);
  7642. /* tp->lock is held. */
  7643. static int tg3_halt(struct tg3 *tp, int kind, bool silent)
  7644. {
  7645. int err;
  7646. tg3_stop_fw(tp);
  7647. tg3_write_sig_pre_reset(tp, kind);
  7648. tg3_abort_hw(tp, silent);
  7649. err = tg3_chip_reset(tp);
  7650. __tg3_set_mac_addr(tp, false);
  7651. tg3_write_sig_legacy(tp, kind);
  7652. tg3_write_sig_post_reset(tp, kind);
  7653. if (tp->hw_stats) {
  7654. /* Save the stats across chip resets... */
  7655. tg3_get_nstats(tp, &tp->net_stats_prev);
  7656. tg3_get_estats(tp, &tp->estats_prev);
  7657. /* And make sure the next sample is new data */
  7658. memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
  7659. }
  7660. return err;
  7661. }
  7662. static int tg3_set_mac_addr(struct net_device *dev, void *p)
  7663. {
  7664. struct tg3 *tp = netdev_priv(dev);
  7665. struct sockaddr *addr = p;
  7666. int err = 0;
  7667. bool skip_mac_1 = false;
  7668. if (!is_valid_ether_addr(addr->sa_data))
  7669. return -EADDRNOTAVAIL;
  7670. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  7671. if (!netif_running(dev))
  7672. return 0;
  7673. if (tg3_flag(tp, ENABLE_ASF)) {
  7674. u32 addr0_high, addr0_low, addr1_high, addr1_low;
  7675. addr0_high = tr32(MAC_ADDR_0_HIGH);
  7676. addr0_low = tr32(MAC_ADDR_0_LOW);
  7677. addr1_high = tr32(MAC_ADDR_1_HIGH);
  7678. addr1_low = tr32(MAC_ADDR_1_LOW);
  7679. /* Skip MAC addr 1 if ASF is using it. */
  7680. if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
  7681. !(addr1_high == 0 && addr1_low == 0))
  7682. skip_mac_1 = true;
  7683. }
  7684. spin_lock_bh(&tp->lock);
  7685. __tg3_set_mac_addr(tp, skip_mac_1);
  7686. __tg3_set_rx_mode(dev);
  7687. spin_unlock_bh(&tp->lock);
  7688. return err;
  7689. }
  7690. /* tp->lock is held. */
  7691. static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
  7692. dma_addr_t mapping, u32 maxlen_flags,
  7693. u32 nic_addr)
  7694. {
  7695. tg3_write_mem(tp,
  7696. (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
  7697. ((u64) mapping >> 32));
  7698. tg3_write_mem(tp,
  7699. (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
  7700. ((u64) mapping & 0xffffffff));
  7701. tg3_write_mem(tp,
  7702. (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
  7703. maxlen_flags);
  7704. if (!tg3_flag(tp, 5705_PLUS))
  7705. tg3_write_mem(tp,
  7706. (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
  7707. nic_addr);
  7708. }
  7709. static void tg3_coal_tx_init(struct tg3 *tp, struct ethtool_coalesce *ec)
  7710. {
  7711. int i = 0;
  7712. if (!tg3_flag(tp, ENABLE_TSS)) {
  7713. tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
  7714. tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
  7715. tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
  7716. } else {
  7717. tw32(HOSTCC_TXCOL_TICKS, 0);
  7718. tw32(HOSTCC_TXMAX_FRAMES, 0);
  7719. tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
  7720. for (; i < tp->txq_cnt; i++) {
  7721. u32 reg;
  7722. reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
  7723. tw32(reg, ec->tx_coalesce_usecs);
  7724. reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
  7725. tw32(reg, ec->tx_max_coalesced_frames);
  7726. reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
  7727. tw32(reg, ec->tx_max_coalesced_frames_irq);
  7728. }
  7729. }
  7730. for (; i < tp->irq_max - 1; i++) {
  7731. tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
  7732. tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
  7733. tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
  7734. }
  7735. }
  7736. static void tg3_coal_rx_init(struct tg3 *tp, struct ethtool_coalesce *ec)
  7737. {
  7738. int i = 0;
  7739. u32 limit = tp->rxq_cnt;
  7740. if (!tg3_flag(tp, ENABLE_RSS)) {
  7741. tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
  7742. tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
  7743. tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
  7744. limit--;
  7745. } else {
  7746. tw32(HOSTCC_RXCOL_TICKS, 0);
  7747. tw32(HOSTCC_RXMAX_FRAMES, 0);
  7748. tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
  7749. }
  7750. for (; i < limit; i++) {
  7751. u32 reg;
  7752. reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
  7753. tw32(reg, ec->rx_coalesce_usecs);
  7754. reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
  7755. tw32(reg, ec->rx_max_coalesced_frames);
  7756. reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
  7757. tw32(reg, ec->rx_max_coalesced_frames_irq);
  7758. }
  7759. for (; i < tp->irq_max - 1; i++) {
  7760. tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
  7761. tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
  7762. tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
  7763. }
  7764. }
  7765. static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
  7766. {
  7767. tg3_coal_tx_init(tp, ec);
  7768. tg3_coal_rx_init(tp, ec);
  7769. if (!tg3_flag(tp, 5705_PLUS)) {
  7770. u32 val = ec->stats_block_coalesce_usecs;
  7771. tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
  7772. tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
  7773. if (!tp->link_up)
  7774. val = 0;
  7775. tw32(HOSTCC_STAT_COAL_TICKS, val);
  7776. }
  7777. }
  7778. /* tp->lock is held. */
  7779. static void tg3_tx_rcbs_disable(struct tg3 *tp)
  7780. {
  7781. u32 txrcb, limit;
  7782. /* Disable all transmit rings but the first. */
  7783. if (!tg3_flag(tp, 5705_PLUS))
  7784. limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
  7785. else if (tg3_flag(tp, 5717_PLUS))
  7786. limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
  7787. else if (tg3_flag(tp, 57765_CLASS) ||
  7788. tg3_asic_rev(tp) == ASIC_REV_5762)
  7789. limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
  7790. else
  7791. limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
  7792. for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
  7793. txrcb < limit; txrcb += TG3_BDINFO_SIZE)
  7794. tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
  7795. BDINFO_FLAGS_DISABLED);
  7796. }
  7797. /* tp->lock is held. */
  7798. static void tg3_tx_rcbs_init(struct tg3 *tp)
  7799. {
  7800. int i = 0;
  7801. u32 txrcb = NIC_SRAM_SEND_RCB;
  7802. if (tg3_flag(tp, ENABLE_TSS))
  7803. i++;
  7804. for (; i < tp->irq_max; i++, txrcb += TG3_BDINFO_SIZE) {
  7805. struct tg3_napi *tnapi = &tp->napi[i];
  7806. if (!tnapi->tx_ring)
  7807. continue;
  7808. tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
  7809. (TG3_TX_RING_SIZE << BDINFO_FLAGS_MAXLEN_SHIFT),
  7810. NIC_SRAM_TX_BUFFER_DESC);
  7811. }
  7812. }
  7813. /* tp->lock is held. */
  7814. static void tg3_rx_ret_rcbs_disable(struct tg3 *tp)
  7815. {
  7816. u32 rxrcb, limit;
  7817. /* Disable all receive return rings but the first. */
  7818. if (tg3_flag(tp, 5717_PLUS))
  7819. limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
  7820. else if (!tg3_flag(tp, 5705_PLUS))
  7821. limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
  7822. else if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
  7823. tg3_asic_rev(tp) == ASIC_REV_5762 ||
  7824. tg3_flag(tp, 57765_CLASS))
  7825. limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
  7826. else
  7827. limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
  7828. for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
  7829. rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
  7830. tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
  7831. BDINFO_FLAGS_DISABLED);
  7832. }
  7833. /* tp->lock is held. */
  7834. static void tg3_rx_ret_rcbs_init(struct tg3 *tp)
  7835. {
  7836. int i = 0;
  7837. u32 rxrcb = NIC_SRAM_RCV_RET_RCB;
  7838. if (tg3_flag(tp, ENABLE_RSS))
  7839. i++;
  7840. for (; i < tp->irq_max; i++, rxrcb += TG3_BDINFO_SIZE) {
  7841. struct tg3_napi *tnapi = &tp->napi[i];
  7842. if (!tnapi->rx_rcb)
  7843. continue;
  7844. tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
  7845. (tp->rx_ret_ring_mask + 1) <<
  7846. BDINFO_FLAGS_MAXLEN_SHIFT, 0);
  7847. }
  7848. }
  7849. /* tp->lock is held. */
  7850. static void tg3_rings_reset(struct tg3 *tp)
  7851. {
  7852. int i;
  7853. u32 stblk;
  7854. struct tg3_napi *tnapi = &tp->napi[0];
  7855. tg3_tx_rcbs_disable(tp);
  7856. tg3_rx_ret_rcbs_disable(tp);
  7857. /* Disable interrupts */
  7858. tw32_mailbox_f(tp->napi[0].int_mbox, 1);
  7859. tp->napi[0].chk_msi_cnt = 0;
  7860. tp->napi[0].last_rx_cons = 0;
  7861. tp->napi[0].last_tx_cons = 0;
  7862. /* Zero mailbox registers. */
  7863. if (tg3_flag(tp, SUPPORT_MSIX)) {
  7864. for (i = 1; i < tp->irq_max; i++) {
  7865. tp->napi[i].tx_prod = 0;
  7866. tp->napi[i].tx_cons = 0;
  7867. if (tg3_flag(tp, ENABLE_TSS))
  7868. tw32_mailbox(tp->napi[i].prodmbox, 0);
  7869. tw32_rx_mbox(tp->napi[i].consmbox, 0);
  7870. tw32_mailbox_f(tp->napi[i].int_mbox, 1);
  7871. tp->napi[i].chk_msi_cnt = 0;
  7872. tp->napi[i].last_rx_cons = 0;
  7873. tp->napi[i].last_tx_cons = 0;
  7874. }
  7875. if (!tg3_flag(tp, ENABLE_TSS))
  7876. tw32_mailbox(tp->napi[0].prodmbox, 0);
  7877. } else {
  7878. tp->napi[0].tx_prod = 0;
  7879. tp->napi[0].tx_cons = 0;
  7880. tw32_mailbox(tp->napi[0].prodmbox, 0);
  7881. tw32_rx_mbox(tp->napi[0].consmbox, 0);
  7882. }
  7883. /* Make sure the NIC-based send BD rings are disabled. */
  7884. if (!tg3_flag(tp, 5705_PLUS)) {
  7885. u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
  7886. for (i = 0; i < 16; i++)
  7887. tw32_tx_mbox(mbox + i * 8, 0);
  7888. }
  7889. /* Clear status block in ram. */
  7890. memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
  7891. /* Set status block DMA address */
  7892. tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
  7893. ((u64) tnapi->status_mapping >> 32));
  7894. tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
  7895. ((u64) tnapi->status_mapping & 0xffffffff));
  7896. stblk = HOSTCC_STATBLCK_RING1;
  7897. for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
  7898. u64 mapping = (u64)tnapi->status_mapping;
  7899. tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
  7900. tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
  7901. stblk += 8;
  7902. /* Clear status block in ram. */
  7903. memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
  7904. }
  7905. tg3_tx_rcbs_init(tp);
  7906. tg3_rx_ret_rcbs_init(tp);
  7907. }
  7908. static void tg3_setup_rxbd_thresholds(struct tg3 *tp)
  7909. {
  7910. u32 val, bdcache_maxcnt, host_rep_thresh, nic_rep_thresh;
  7911. if (!tg3_flag(tp, 5750_PLUS) ||
  7912. tg3_flag(tp, 5780_CLASS) ||
  7913. tg3_asic_rev(tp) == ASIC_REV_5750 ||
  7914. tg3_asic_rev(tp) == ASIC_REV_5752 ||
  7915. tg3_flag(tp, 57765_PLUS))
  7916. bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5700;
  7917. else if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
  7918. tg3_asic_rev(tp) == ASIC_REV_5787)
  7919. bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5755;
  7920. else
  7921. bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5906;
  7922. nic_rep_thresh = min(bdcache_maxcnt / 2, tp->rx_std_max_post);
  7923. host_rep_thresh = max_t(u32, tp->rx_pending / 8, 1);
  7924. val = min(nic_rep_thresh, host_rep_thresh);
  7925. tw32(RCVBDI_STD_THRESH, val);
  7926. if (tg3_flag(tp, 57765_PLUS))
  7927. tw32(STD_REPLENISH_LWM, bdcache_maxcnt);
  7928. if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
  7929. return;
  7930. bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5700;
  7931. host_rep_thresh = max_t(u32, tp->rx_jumbo_pending / 8, 1);
  7932. val = min(bdcache_maxcnt / 2, host_rep_thresh);
  7933. tw32(RCVBDI_JUMBO_THRESH, val);
  7934. if (tg3_flag(tp, 57765_PLUS))
  7935. tw32(JMB_REPLENISH_LWM, bdcache_maxcnt);
  7936. }
  7937. static inline u32 calc_crc(unsigned char *buf, int len)
  7938. {
  7939. u32 reg;
  7940. u32 tmp;
  7941. int j, k;
  7942. reg = 0xffffffff;
  7943. for (j = 0; j < len; j++) {
  7944. reg ^= buf[j];
  7945. for (k = 0; k < 8; k++) {
  7946. tmp = reg & 0x01;
  7947. reg >>= 1;
  7948. if (tmp)
  7949. reg ^= 0xedb88320;
  7950. }
  7951. }
  7952. return ~reg;
  7953. }
  7954. static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
  7955. {
  7956. /* accept or reject all multicast frames */
  7957. tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
  7958. tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
  7959. tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
  7960. tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
  7961. }
  7962. static void __tg3_set_rx_mode(struct net_device *dev)
  7963. {
  7964. struct tg3 *tp = netdev_priv(dev);
  7965. u32 rx_mode;
  7966. rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
  7967. RX_MODE_KEEP_VLAN_TAG);
  7968. #if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
  7969. /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
  7970. * flag clear.
  7971. */
  7972. if (!tg3_flag(tp, ENABLE_ASF))
  7973. rx_mode |= RX_MODE_KEEP_VLAN_TAG;
  7974. #endif
  7975. if (dev->flags & IFF_PROMISC) {
  7976. /* Promiscuous mode. */
  7977. rx_mode |= RX_MODE_PROMISC;
  7978. } else if (dev->flags & IFF_ALLMULTI) {
  7979. /* Accept all multicast. */
  7980. tg3_set_multi(tp, 1);
  7981. } else if (netdev_mc_empty(dev)) {
  7982. /* Reject all multicast. */
  7983. tg3_set_multi(tp, 0);
  7984. } else {
  7985. /* Accept one or more multicast(s). */
  7986. struct netdev_hw_addr *ha;
  7987. u32 mc_filter[4] = { 0, };
  7988. u32 regidx;
  7989. u32 bit;
  7990. u32 crc;
  7991. netdev_for_each_mc_addr(ha, dev) {
  7992. crc = calc_crc(ha->addr, ETH_ALEN);
  7993. bit = ~crc & 0x7f;
  7994. regidx = (bit & 0x60) >> 5;
  7995. bit &= 0x1f;
  7996. mc_filter[regidx] |= (1 << bit);
  7997. }
  7998. tw32(MAC_HASH_REG_0, mc_filter[0]);
  7999. tw32(MAC_HASH_REG_1, mc_filter[1]);
  8000. tw32(MAC_HASH_REG_2, mc_filter[2]);
  8001. tw32(MAC_HASH_REG_3, mc_filter[3]);
  8002. }
  8003. if (netdev_uc_count(dev) > TG3_MAX_UCAST_ADDR(tp)) {
  8004. rx_mode |= RX_MODE_PROMISC;
  8005. } else if (!(dev->flags & IFF_PROMISC)) {
  8006. /* Add all entries into to the mac addr filter list */
  8007. int i = 0;
  8008. struct netdev_hw_addr *ha;
  8009. netdev_for_each_uc_addr(ha, dev) {
  8010. __tg3_set_one_mac_addr(tp, ha->addr,
  8011. i + TG3_UCAST_ADDR_IDX(tp));
  8012. i++;
  8013. }
  8014. }
  8015. if (rx_mode != tp->rx_mode) {
  8016. tp->rx_mode = rx_mode;
  8017. tw32_f(MAC_RX_MODE, rx_mode);
  8018. udelay(10);
  8019. }
  8020. }
  8021. static void tg3_rss_init_dflt_indir_tbl(struct tg3 *tp, u32 qcnt)
  8022. {
  8023. int i;
  8024. for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
  8025. tp->rss_ind_tbl[i] = ethtool_rxfh_indir_default(i, qcnt);
  8026. }
  8027. static void tg3_rss_check_indir_tbl(struct tg3 *tp)
  8028. {
  8029. int i;
  8030. if (!tg3_flag(tp, SUPPORT_MSIX))
  8031. return;
  8032. if (tp->rxq_cnt == 1) {
  8033. memset(&tp->rss_ind_tbl[0], 0, sizeof(tp->rss_ind_tbl));
  8034. return;
  8035. }
  8036. /* Validate table against current IRQ count */
  8037. for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
  8038. if (tp->rss_ind_tbl[i] >= tp->rxq_cnt)
  8039. break;
  8040. }
  8041. if (i != TG3_RSS_INDIR_TBL_SIZE)
  8042. tg3_rss_init_dflt_indir_tbl(tp, tp->rxq_cnt);
  8043. }
  8044. static void tg3_rss_write_indir_tbl(struct tg3 *tp)
  8045. {
  8046. int i = 0;
  8047. u32 reg = MAC_RSS_INDIR_TBL_0;
  8048. while (i < TG3_RSS_INDIR_TBL_SIZE) {
  8049. u32 val = tp->rss_ind_tbl[i];
  8050. i++;
  8051. for (; i % 8; i++) {
  8052. val <<= 4;
  8053. val |= tp->rss_ind_tbl[i];
  8054. }
  8055. tw32(reg, val);
  8056. reg += 4;
  8057. }
  8058. }
  8059. static inline u32 tg3_lso_rd_dma_workaround_bit(struct tg3 *tp)
  8060. {
  8061. if (tg3_asic_rev(tp) == ASIC_REV_5719)
  8062. return TG3_LSO_RD_DMA_TX_LENGTH_WA_5719;
  8063. else
  8064. return TG3_LSO_RD_DMA_TX_LENGTH_WA_5720;
  8065. }
  8066. /* tp->lock is held. */
  8067. static int tg3_reset_hw(struct tg3 *tp, bool reset_phy)
  8068. {
  8069. u32 val, rdmac_mode;
  8070. int i, err, limit;
  8071. struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
  8072. tg3_disable_ints(tp);
  8073. tg3_stop_fw(tp);
  8074. tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
  8075. if (tg3_flag(tp, INIT_COMPLETE))
  8076. tg3_abort_hw(tp, 1);
  8077. if ((tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN) &&
  8078. !(tp->phy_flags & TG3_PHYFLG_USER_CONFIGURED)) {
  8079. tg3_phy_pull_config(tp);
  8080. tg3_eee_pull_config(tp, NULL);
  8081. tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
  8082. }
  8083. /* Enable MAC control of LPI */
  8084. if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
  8085. tg3_setup_eee(tp);
  8086. if (reset_phy)
  8087. tg3_phy_reset(tp);
  8088. err = tg3_chip_reset(tp);
  8089. if (err)
  8090. return err;
  8091. tg3_write_sig_legacy(tp, RESET_KIND_INIT);
  8092. if (tg3_chip_rev(tp) == CHIPREV_5784_AX) {
  8093. val = tr32(TG3_CPMU_CTRL);
  8094. val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
  8095. tw32(TG3_CPMU_CTRL, val);
  8096. val = tr32(TG3_CPMU_LSPD_10MB_CLK);
  8097. val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
  8098. val |= CPMU_LSPD_10MB_MACCLK_6_25;
  8099. tw32(TG3_CPMU_LSPD_10MB_CLK, val);
  8100. val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
  8101. val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
  8102. val |= CPMU_LNK_AWARE_MACCLK_6_25;
  8103. tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
  8104. val = tr32(TG3_CPMU_HST_ACC);
  8105. val &= ~CPMU_HST_ACC_MACCLK_MASK;
  8106. val |= CPMU_HST_ACC_MACCLK_6_25;
  8107. tw32(TG3_CPMU_HST_ACC, val);
  8108. }
  8109. if (tg3_asic_rev(tp) == ASIC_REV_57780) {
  8110. val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
  8111. val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
  8112. PCIE_PWR_MGMT_L1_THRESH_4MS;
  8113. tw32(PCIE_PWR_MGMT_THRESH, val);
  8114. val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
  8115. tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
  8116. tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
  8117. val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
  8118. tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
  8119. }
  8120. if (tg3_flag(tp, L1PLLPD_EN)) {
  8121. u32 grc_mode = tr32(GRC_MODE);
  8122. /* Access the lower 1K of PL PCIE block registers. */
  8123. val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
  8124. tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
  8125. val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
  8126. tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
  8127. val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
  8128. tw32(GRC_MODE, grc_mode);
  8129. }
  8130. if (tg3_flag(tp, 57765_CLASS)) {
  8131. if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0) {
  8132. u32 grc_mode = tr32(GRC_MODE);
  8133. /* Access the lower 1K of PL PCIE block registers. */
  8134. val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
  8135. tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
  8136. val = tr32(TG3_PCIE_TLDLPL_PORT +
  8137. TG3_PCIE_PL_LO_PHYCTL5);
  8138. tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
  8139. val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
  8140. tw32(GRC_MODE, grc_mode);
  8141. }
  8142. if (tg3_chip_rev(tp) != CHIPREV_57765_AX) {
  8143. u32 grc_mode;
  8144. /* Fix transmit hangs */
  8145. val = tr32(TG3_CPMU_PADRNG_CTL);
  8146. val |= TG3_CPMU_PADRNG_CTL_RDIV2;
  8147. tw32(TG3_CPMU_PADRNG_CTL, val);
  8148. grc_mode = tr32(GRC_MODE);
  8149. /* Access the lower 1K of DL PCIE block registers. */
  8150. val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
  8151. tw32(GRC_MODE, val | GRC_MODE_PCIE_DL_SEL);
  8152. val = tr32(TG3_PCIE_TLDLPL_PORT +
  8153. TG3_PCIE_DL_LO_FTSMAX);
  8154. val &= ~TG3_PCIE_DL_LO_FTSMAX_MSK;
  8155. tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_DL_LO_FTSMAX,
  8156. val | TG3_PCIE_DL_LO_FTSMAX_VAL);
  8157. tw32(GRC_MODE, grc_mode);
  8158. }
  8159. val = tr32(TG3_CPMU_LSPD_10MB_CLK);
  8160. val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
  8161. val |= CPMU_LSPD_10MB_MACCLK_6_25;
  8162. tw32(TG3_CPMU_LSPD_10MB_CLK, val);
  8163. }
  8164. /* This works around an issue with Athlon chipsets on
  8165. * B3 tigon3 silicon. This bit has no effect on any
  8166. * other revision. But do not set this on PCI Express
  8167. * chips and don't even touch the clocks if the CPMU is present.
  8168. */
  8169. if (!tg3_flag(tp, CPMU_PRESENT)) {
  8170. if (!tg3_flag(tp, PCI_EXPRESS))
  8171. tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
  8172. tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
  8173. }
  8174. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0 &&
  8175. tg3_flag(tp, PCIX_MODE)) {
  8176. val = tr32(TG3PCI_PCISTATE);
  8177. val |= PCISTATE_RETRY_SAME_DMA;
  8178. tw32(TG3PCI_PCISTATE, val);
  8179. }
  8180. if (tg3_flag(tp, ENABLE_APE)) {
  8181. /* Allow reads and writes to the
  8182. * APE register and memory space.
  8183. */
  8184. val = tr32(TG3PCI_PCISTATE);
  8185. val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
  8186. PCISTATE_ALLOW_APE_SHMEM_WR |
  8187. PCISTATE_ALLOW_APE_PSPACE_WR;
  8188. tw32(TG3PCI_PCISTATE, val);
  8189. }
  8190. if (tg3_chip_rev(tp) == CHIPREV_5704_BX) {
  8191. /* Enable some hw fixes. */
  8192. val = tr32(TG3PCI_MSI_DATA);
  8193. val |= (1 << 26) | (1 << 28) | (1 << 29);
  8194. tw32(TG3PCI_MSI_DATA, val);
  8195. }
  8196. /* Descriptor ring init may make accesses to the
  8197. * NIC SRAM area to setup the TX descriptors, so we
  8198. * can only do this after the hardware has been
  8199. * successfully reset.
  8200. */
  8201. err = tg3_init_rings(tp);
  8202. if (err)
  8203. return err;
  8204. if (tg3_flag(tp, 57765_PLUS)) {
  8205. val = tr32(TG3PCI_DMA_RW_CTRL) &
  8206. ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
  8207. if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0)
  8208. val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
  8209. if (!tg3_flag(tp, 57765_CLASS) &&
  8210. tg3_asic_rev(tp) != ASIC_REV_5717 &&
  8211. tg3_asic_rev(tp) != ASIC_REV_5762)
  8212. val |= DMA_RWCTRL_TAGGED_STAT_WA;
  8213. tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
  8214. } else if (tg3_asic_rev(tp) != ASIC_REV_5784 &&
  8215. tg3_asic_rev(tp) != ASIC_REV_5761) {
  8216. /* This value is determined during the probe time DMA
  8217. * engine test, tg3_test_dma.
  8218. */
  8219. tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
  8220. }
  8221. tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
  8222. GRC_MODE_4X_NIC_SEND_RINGS |
  8223. GRC_MODE_NO_TX_PHDR_CSUM |
  8224. GRC_MODE_NO_RX_PHDR_CSUM);
  8225. tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
  8226. /* Pseudo-header checksum is done by hardware logic and not
  8227. * the offload processers, so make the chip do the pseudo-
  8228. * header checksums on receive. For transmit it is more
  8229. * convenient to do the pseudo-header checksum in software
  8230. * as Linux does that on transmit for us in all cases.
  8231. */
  8232. tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
  8233. val = GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP;
  8234. if (tp->rxptpctl)
  8235. tw32(TG3_RX_PTP_CTL,
  8236. tp->rxptpctl | TG3_RX_PTP_CTL_HWTS_INTERLOCK);
  8237. if (tg3_flag(tp, PTP_CAPABLE))
  8238. val |= GRC_MODE_TIME_SYNC_ENABLE;
  8239. tw32(GRC_MODE, tp->grc_mode | val);
  8240. /* On one of the AMD platform, MRRS is restricted to 4000 because of
  8241. * south bridge limitation. As a workaround, Driver is setting MRRS
  8242. * to 2048 instead of default 4096.
  8243. */
  8244. if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
  8245. tp->pdev->subsystem_device == TG3PCI_SUBDEVICE_ID_DELL_5762) {
  8246. val = tr32(TG3PCI_DEV_STATUS_CTRL) & ~MAX_READ_REQ_MASK;
  8247. tw32(TG3PCI_DEV_STATUS_CTRL, val | MAX_READ_REQ_SIZE_2048);
  8248. }
  8249. /* Setup the timer prescalar register. Clock is always 66Mhz. */
  8250. val = tr32(GRC_MISC_CFG);
  8251. val &= ~0xff;
  8252. val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
  8253. tw32(GRC_MISC_CFG, val);
  8254. /* Initialize MBUF/DESC pool. */
  8255. if (tg3_flag(tp, 5750_PLUS)) {
  8256. /* Do nothing. */
  8257. } else if (tg3_asic_rev(tp) != ASIC_REV_5705) {
  8258. tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
  8259. if (tg3_asic_rev(tp) == ASIC_REV_5704)
  8260. tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
  8261. else
  8262. tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
  8263. tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
  8264. tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
  8265. } else if (tg3_flag(tp, TSO_CAPABLE)) {
  8266. int fw_len;
  8267. fw_len = tp->fw_len;
  8268. fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
  8269. tw32(BUFMGR_MB_POOL_ADDR,
  8270. NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
  8271. tw32(BUFMGR_MB_POOL_SIZE,
  8272. NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
  8273. }
  8274. if (tp->dev->mtu <= ETH_DATA_LEN) {
  8275. tw32(BUFMGR_MB_RDMA_LOW_WATER,
  8276. tp->bufmgr_config.mbuf_read_dma_low_water);
  8277. tw32(BUFMGR_MB_MACRX_LOW_WATER,
  8278. tp->bufmgr_config.mbuf_mac_rx_low_water);
  8279. tw32(BUFMGR_MB_HIGH_WATER,
  8280. tp->bufmgr_config.mbuf_high_water);
  8281. } else {
  8282. tw32(BUFMGR_MB_RDMA_LOW_WATER,
  8283. tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
  8284. tw32(BUFMGR_MB_MACRX_LOW_WATER,
  8285. tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
  8286. tw32(BUFMGR_MB_HIGH_WATER,
  8287. tp->bufmgr_config.mbuf_high_water_jumbo);
  8288. }
  8289. tw32(BUFMGR_DMA_LOW_WATER,
  8290. tp->bufmgr_config.dma_low_water);
  8291. tw32(BUFMGR_DMA_HIGH_WATER,
  8292. tp->bufmgr_config.dma_high_water);
  8293. val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
  8294. if (tg3_asic_rev(tp) == ASIC_REV_5719)
  8295. val |= BUFMGR_MODE_NO_TX_UNDERRUN;
  8296. if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
  8297. tg3_asic_rev(tp) == ASIC_REV_5762 ||
  8298. tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
  8299. tg3_chip_rev_id(tp) == CHIPREV_ID_5720_A0)
  8300. val |= BUFMGR_MODE_MBLOW_ATTN_ENAB;
  8301. tw32(BUFMGR_MODE, val);
  8302. for (i = 0; i < 2000; i++) {
  8303. if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
  8304. break;
  8305. udelay(10);
  8306. }
  8307. if (i >= 2000) {
  8308. netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
  8309. return -ENODEV;
  8310. }
  8311. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5906_A1)
  8312. tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
  8313. tg3_setup_rxbd_thresholds(tp);
  8314. /* Initialize TG3_BDINFO's at:
  8315. * RCVDBDI_STD_BD: standard eth size rx ring
  8316. * RCVDBDI_JUMBO_BD: jumbo frame rx ring
  8317. * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
  8318. *
  8319. * like so:
  8320. * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
  8321. * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
  8322. * ring attribute flags
  8323. * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
  8324. *
  8325. * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
  8326. * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
  8327. *
  8328. * The size of each ring is fixed in the firmware, but the location is
  8329. * configurable.
  8330. */
  8331. tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
  8332. ((u64) tpr->rx_std_mapping >> 32));
  8333. tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
  8334. ((u64) tpr->rx_std_mapping & 0xffffffff));
  8335. if (!tg3_flag(tp, 5717_PLUS))
  8336. tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
  8337. NIC_SRAM_RX_BUFFER_DESC);
  8338. /* Disable the mini ring */
  8339. if (!tg3_flag(tp, 5705_PLUS))
  8340. tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
  8341. BDINFO_FLAGS_DISABLED);
  8342. /* Program the jumbo buffer descriptor ring control
  8343. * blocks on those devices that have them.
  8344. */
  8345. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
  8346. (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))) {
  8347. if (tg3_flag(tp, JUMBO_RING_ENABLE)) {
  8348. tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
  8349. ((u64) tpr->rx_jmb_mapping >> 32));
  8350. tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
  8351. ((u64) tpr->rx_jmb_mapping & 0xffffffff));
  8352. val = TG3_RX_JMB_RING_SIZE(tp) <<
  8353. BDINFO_FLAGS_MAXLEN_SHIFT;
  8354. tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
  8355. val | BDINFO_FLAGS_USE_EXT_RECV);
  8356. if (!tg3_flag(tp, USE_JUMBO_BDFLAG) ||
  8357. tg3_flag(tp, 57765_CLASS) ||
  8358. tg3_asic_rev(tp) == ASIC_REV_5762)
  8359. tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
  8360. NIC_SRAM_RX_JUMBO_BUFFER_DESC);
  8361. } else {
  8362. tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
  8363. BDINFO_FLAGS_DISABLED);
  8364. }
  8365. if (tg3_flag(tp, 57765_PLUS)) {
  8366. val = TG3_RX_STD_RING_SIZE(tp);
  8367. val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
  8368. val |= (TG3_RX_STD_DMA_SZ << 2);
  8369. } else
  8370. val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
  8371. } else
  8372. val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
  8373. tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
  8374. tpr->rx_std_prod_idx = tp->rx_pending;
  8375. tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
  8376. tpr->rx_jmb_prod_idx =
  8377. tg3_flag(tp, JUMBO_RING_ENABLE) ? tp->rx_jumbo_pending : 0;
  8378. tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
  8379. tg3_rings_reset(tp);
  8380. /* Initialize MAC address and backoff seed. */
  8381. __tg3_set_mac_addr(tp, false);
  8382. /* MTU + ethernet header + FCS + optional VLAN tag */
  8383. tw32(MAC_RX_MTU_SIZE,
  8384. tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
  8385. /* The slot time is changed by tg3_setup_phy if we
  8386. * run at gigabit with half duplex.
  8387. */
  8388. val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
  8389. (6 << TX_LENGTHS_IPG_SHIFT) |
  8390. (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
  8391. if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
  8392. tg3_asic_rev(tp) == ASIC_REV_5762)
  8393. val |= tr32(MAC_TX_LENGTHS) &
  8394. (TX_LENGTHS_JMB_FRM_LEN_MSK |
  8395. TX_LENGTHS_CNT_DWN_VAL_MSK);
  8396. tw32(MAC_TX_LENGTHS, val);
  8397. /* Receive rules. */
  8398. tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
  8399. tw32(RCVLPC_CONFIG, 0x0181);
  8400. /* Calculate RDMAC_MODE setting early, we need it to determine
  8401. * the RCVLPC_STATE_ENABLE mask.
  8402. */
  8403. rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
  8404. RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
  8405. RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
  8406. RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
  8407. RDMAC_MODE_LNGREAD_ENAB);
  8408. if (tg3_asic_rev(tp) == ASIC_REV_5717)
  8409. rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
  8410. if (tg3_asic_rev(tp) == ASIC_REV_5784 ||
  8411. tg3_asic_rev(tp) == ASIC_REV_5785 ||
  8412. tg3_asic_rev(tp) == ASIC_REV_57780)
  8413. rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
  8414. RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
  8415. RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
  8416. if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
  8417. tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
  8418. if (tg3_flag(tp, TSO_CAPABLE) &&
  8419. tg3_asic_rev(tp) == ASIC_REV_5705) {
  8420. rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
  8421. } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
  8422. !tg3_flag(tp, IS_5788)) {
  8423. rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
  8424. }
  8425. }
  8426. if (tg3_flag(tp, PCI_EXPRESS))
  8427. rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
  8428. if (tg3_asic_rev(tp) == ASIC_REV_57766) {
  8429. tp->dma_limit = 0;
  8430. if (tp->dev->mtu <= ETH_DATA_LEN) {
  8431. rdmac_mode |= RDMAC_MODE_JMB_2K_MMRR;
  8432. tp->dma_limit = TG3_TX_BD_DMA_MAX_2K;
  8433. }
  8434. }
  8435. if (tg3_flag(tp, HW_TSO_1) ||
  8436. tg3_flag(tp, HW_TSO_2) ||
  8437. tg3_flag(tp, HW_TSO_3))
  8438. rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
  8439. if (tg3_flag(tp, 57765_PLUS) ||
  8440. tg3_asic_rev(tp) == ASIC_REV_5785 ||
  8441. tg3_asic_rev(tp) == ASIC_REV_57780)
  8442. rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
  8443. if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
  8444. tg3_asic_rev(tp) == ASIC_REV_5762)
  8445. rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
  8446. if (tg3_asic_rev(tp) == ASIC_REV_5761 ||
  8447. tg3_asic_rev(tp) == ASIC_REV_5784 ||
  8448. tg3_asic_rev(tp) == ASIC_REV_5785 ||
  8449. tg3_asic_rev(tp) == ASIC_REV_57780 ||
  8450. tg3_flag(tp, 57765_PLUS)) {
  8451. u32 tgtreg;
  8452. if (tg3_asic_rev(tp) == ASIC_REV_5762)
  8453. tgtreg = TG3_RDMA_RSRVCTRL_REG2;
  8454. else
  8455. tgtreg = TG3_RDMA_RSRVCTRL_REG;
  8456. val = tr32(tgtreg);
  8457. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
  8458. tg3_asic_rev(tp) == ASIC_REV_5762) {
  8459. val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
  8460. TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
  8461. TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
  8462. val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
  8463. TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
  8464. TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
  8465. }
  8466. tw32(tgtreg, val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
  8467. }
  8468. if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
  8469. tg3_asic_rev(tp) == ASIC_REV_5720 ||
  8470. tg3_asic_rev(tp) == ASIC_REV_5762) {
  8471. u32 tgtreg;
  8472. if (tg3_asic_rev(tp) == ASIC_REV_5762)
  8473. tgtreg = TG3_LSO_RD_DMA_CRPTEN_CTRL2;
  8474. else
  8475. tgtreg = TG3_LSO_RD_DMA_CRPTEN_CTRL;
  8476. val = tr32(tgtreg);
  8477. tw32(tgtreg, val |
  8478. TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
  8479. TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
  8480. }
  8481. /* Receive/send statistics. */
  8482. if (tg3_flag(tp, 5750_PLUS)) {
  8483. val = tr32(RCVLPC_STATS_ENABLE);
  8484. val &= ~RCVLPC_STATSENAB_DACK_FIX;
  8485. tw32(RCVLPC_STATS_ENABLE, val);
  8486. } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
  8487. tg3_flag(tp, TSO_CAPABLE)) {
  8488. val = tr32(RCVLPC_STATS_ENABLE);
  8489. val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
  8490. tw32(RCVLPC_STATS_ENABLE, val);
  8491. } else {
  8492. tw32(RCVLPC_STATS_ENABLE, 0xffffff);
  8493. }
  8494. tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
  8495. tw32(SNDDATAI_STATSENAB, 0xffffff);
  8496. tw32(SNDDATAI_STATSCTRL,
  8497. (SNDDATAI_SCTRL_ENABLE |
  8498. SNDDATAI_SCTRL_FASTUPD));
  8499. /* Setup host coalescing engine. */
  8500. tw32(HOSTCC_MODE, 0);
  8501. for (i = 0; i < 2000; i++) {
  8502. if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
  8503. break;
  8504. udelay(10);
  8505. }
  8506. __tg3_set_coalesce(tp, &tp->coal);
  8507. if (!tg3_flag(tp, 5705_PLUS)) {
  8508. /* Status/statistics block address. See tg3_timer,
  8509. * the tg3_periodic_fetch_stats call there, and
  8510. * tg3_get_stats to see how this works for 5705/5750 chips.
  8511. */
  8512. tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
  8513. ((u64) tp->stats_mapping >> 32));
  8514. tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
  8515. ((u64) tp->stats_mapping & 0xffffffff));
  8516. tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
  8517. tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
  8518. /* Clear statistics and status block memory areas */
  8519. for (i = NIC_SRAM_STATS_BLK;
  8520. i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
  8521. i += sizeof(u32)) {
  8522. tg3_write_mem(tp, i, 0);
  8523. udelay(40);
  8524. }
  8525. }
  8526. tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
  8527. tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
  8528. tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
  8529. if (!tg3_flag(tp, 5705_PLUS))
  8530. tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
  8531. if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
  8532. tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
  8533. /* reset to prevent losing 1st rx packet intermittently */
  8534. tw32_f(MAC_RX_MODE, RX_MODE_RESET);
  8535. udelay(10);
  8536. }
  8537. tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
  8538. MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE |
  8539. MAC_MODE_FHDE_ENABLE;
  8540. if (tg3_flag(tp, ENABLE_APE))
  8541. tp->mac_mode |= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
  8542. if (!tg3_flag(tp, 5705_PLUS) &&
  8543. !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
  8544. tg3_asic_rev(tp) != ASIC_REV_5700)
  8545. tp->mac_mode |= MAC_MODE_LINK_POLARITY;
  8546. tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
  8547. udelay(40);
  8548. /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
  8549. * If TG3_FLAG_IS_NIC is zero, we should read the
  8550. * register to preserve the GPIO settings for LOMs. The GPIOs,
  8551. * whether used as inputs or outputs, are set by boot code after
  8552. * reset.
  8553. */
  8554. if (!tg3_flag(tp, IS_NIC)) {
  8555. u32 gpio_mask;
  8556. gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
  8557. GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
  8558. GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
  8559. if (tg3_asic_rev(tp) == ASIC_REV_5752)
  8560. gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
  8561. GRC_LCLCTRL_GPIO_OUTPUT3;
  8562. if (tg3_asic_rev(tp) == ASIC_REV_5755)
  8563. gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
  8564. tp->grc_local_ctrl &= ~gpio_mask;
  8565. tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
  8566. /* GPIO1 must be driven high for eeprom write protect */
  8567. if (tg3_flag(tp, EEPROM_WRITE_PROT))
  8568. tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
  8569. GRC_LCLCTRL_GPIO_OUTPUT1);
  8570. }
  8571. tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
  8572. udelay(100);
  8573. if (tg3_flag(tp, USING_MSIX)) {
  8574. val = tr32(MSGINT_MODE);
  8575. val |= MSGINT_MODE_ENABLE;
  8576. if (tp->irq_cnt > 1)
  8577. val |= MSGINT_MODE_MULTIVEC_EN;
  8578. if (!tg3_flag(tp, 1SHOT_MSI))
  8579. val |= MSGINT_MODE_ONE_SHOT_DISABLE;
  8580. tw32(MSGINT_MODE, val);
  8581. }
  8582. if (!tg3_flag(tp, 5705_PLUS)) {
  8583. tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
  8584. udelay(40);
  8585. }
  8586. val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
  8587. WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
  8588. WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
  8589. WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
  8590. WDMAC_MODE_LNGREAD_ENAB);
  8591. if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
  8592. tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
  8593. if (tg3_flag(tp, TSO_CAPABLE) &&
  8594. (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A1 ||
  8595. tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A2)) {
  8596. /* nothing */
  8597. } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
  8598. !tg3_flag(tp, IS_5788)) {
  8599. val |= WDMAC_MODE_RX_ACCEL;
  8600. }
  8601. }
  8602. /* Enable host coalescing bug fix */
  8603. if (tg3_flag(tp, 5755_PLUS))
  8604. val |= WDMAC_MODE_STATUS_TAG_FIX;
  8605. if (tg3_asic_rev(tp) == ASIC_REV_5785)
  8606. val |= WDMAC_MODE_BURST_ALL_DATA;
  8607. tw32_f(WDMAC_MODE, val);
  8608. udelay(40);
  8609. if (tg3_flag(tp, PCIX_MODE)) {
  8610. u16 pcix_cmd;
  8611. pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
  8612. &pcix_cmd);
  8613. if (tg3_asic_rev(tp) == ASIC_REV_5703) {
  8614. pcix_cmd &= ~PCI_X_CMD_MAX_READ;
  8615. pcix_cmd |= PCI_X_CMD_READ_2K;
  8616. } else if (tg3_asic_rev(tp) == ASIC_REV_5704) {
  8617. pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
  8618. pcix_cmd |= PCI_X_CMD_READ_2K;
  8619. }
  8620. pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
  8621. pcix_cmd);
  8622. }
  8623. tw32_f(RDMAC_MODE, rdmac_mode);
  8624. udelay(40);
  8625. if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
  8626. tg3_asic_rev(tp) == ASIC_REV_5720) {
  8627. for (i = 0; i < TG3_NUM_RDMA_CHANNELS; i++) {
  8628. if (tr32(TG3_RDMA_LENGTH + (i << 2)) > TG3_MAX_MTU(tp))
  8629. break;
  8630. }
  8631. if (i < TG3_NUM_RDMA_CHANNELS) {
  8632. val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
  8633. val |= tg3_lso_rd_dma_workaround_bit(tp);
  8634. tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val);
  8635. tg3_flag_set(tp, 5719_5720_RDMA_BUG);
  8636. }
  8637. }
  8638. tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
  8639. if (!tg3_flag(tp, 5705_PLUS))
  8640. tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
  8641. if (tg3_asic_rev(tp) == ASIC_REV_5761)
  8642. tw32(SNDDATAC_MODE,
  8643. SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
  8644. else
  8645. tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
  8646. tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
  8647. tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
  8648. val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
  8649. if (tg3_flag(tp, LRG_PROD_RING_CAP))
  8650. val |= RCVDBDI_MODE_LRG_RING_SZ;
  8651. tw32(RCVDBDI_MODE, val);
  8652. tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
  8653. if (tg3_flag(tp, HW_TSO_1) ||
  8654. tg3_flag(tp, HW_TSO_2) ||
  8655. tg3_flag(tp, HW_TSO_3))
  8656. tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
  8657. val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
  8658. if (tg3_flag(tp, ENABLE_TSS))
  8659. val |= SNDBDI_MODE_MULTI_TXQ_EN;
  8660. tw32(SNDBDI_MODE, val);
  8661. tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
  8662. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0) {
  8663. err = tg3_load_5701_a0_firmware_fix(tp);
  8664. if (err)
  8665. return err;
  8666. }
  8667. if (tg3_asic_rev(tp) == ASIC_REV_57766) {
  8668. /* Ignore any errors for the firmware download. If download
  8669. * fails, the device will operate with EEE disabled
  8670. */
  8671. tg3_load_57766_firmware(tp);
  8672. }
  8673. if (tg3_flag(tp, TSO_CAPABLE)) {
  8674. err = tg3_load_tso_firmware(tp);
  8675. if (err)
  8676. return err;
  8677. }
  8678. tp->tx_mode = TX_MODE_ENABLE;
  8679. if (tg3_flag(tp, 5755_PLUS) ||
  8680. tg3_asic_rev(tp) == ASIC_REV_5906)
  8681. tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
  8682. if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
  8683. tg3_asic_rev(tp) == ASIC_REV_5762) {
  8684. val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
  8685. tp->tx_mode &= ~val;
  8686. tp->tx_mode |= tr32(MAC_TX_MODE) & val;
  8687. }
  8688. tw32_f(MAC_TX_MODE, tp->tx_mode);
  8689. udelay(100);
  8690. if (tg3_flag(tp, ENABLE_RSS)) {
  8691. u32 rss_key[10];
  8692. tg3_rss_write_indir_tbl(tp);
  8693. netdev_rss_key_fill(rss_key, 10 * sizeof(u32));
  8694. for (i = 0; i < 10 ; i++)
  8695. tw32(MAC_RSS_HASH_KEY_0 + i*4, rss_key[i]);
  8696. }
  8697. tp->rx_mode = RX_MODE_ENABLE;
  8698. if (tg3_flag(tp, 5755_PLUS))
  8699. tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
  8700. if (tg3_asic_rev(tp) == ASIC_REV_5762)
  8701. tp->rx_mode |= RX_MODE_IPV4_FRAG_FIX;
  8702. if (tg3_flag(tp, ENABLE_RSS))
  8703. tp->rx_mode |= RX_MODE_RSS_ENABLE |
  8704. RX_MODE_RSS_ITBL_HASH_BITS_7 |
  8705. RX_MODE_RSS_IPV6_HASH_EN |
  8706. RX_MODE_RSS_TCP_IPV6_HASH_EN |
  8707. RX_MODE_RSS_IPV4_HASH_EN |
  8708. RX_MODE_RSS_TCP_IPV4_HASH_EN;
  8709. tw32_f(MAC_RX_MODE, tp->rx_mode);
  8710. udelay(10);
  8711. tw32(MAC_LED_CTRL, tp->led_ctrl);
  8712. tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
  8713. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
  8714. tw32_f(MAC_RX_MODE, RX_MODE_RESET);
  8715. udelay(10);
  8716. }
  8717. tw32_f(MAC_RX_MODE, tp->rx_mode);
  8718. udelay(10);
  8719. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
  8720. if ((tg3_asic_rev(tp) == ASIC_REV_5704) &&
  8721. !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
  8722. /* Set drive transmission level to 1.2V */
  8723. /* only if the signal pre-emphasis bit is not set */
  8724. val = tr32(MAC_SERDES_CFG);
  8725. val &= 0xfffff000;
  8726. val |= 0x880;
  8727. tw32(MAC_SERDES_CFG, val);
  8728. }
  8729. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A1)
  8730. tw32(MAC_SERDES_CFG, 0x616000);
  8731. }
  8732. /* Prevent chip from dropping frames when flow control
  8733. * is enabled.
  8734. */
  8735. if (tg3_flag(tp, 57765_CLASS))
  8736. val = 1;
  8737. else
  8738. val = 2;
  8739. tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
  8740. if (tg3_asic_rev(tp) == ASIC_REV_5704 &&
  8741. (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
  8742. /* Use hardware link auto-negotiation */
  8743. tg3_flag_set(tp, HW_AUTONEG);
  8744. }
  8745. if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
  8746. tg3_asic_rev(tp) == ASIC_REV_5714) {
  8747. u32 tmp;
  8748. tmp = tr32(SERDES_RX_CTRL);
  8749. tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
  8750. tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
  8751. tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
  8752. tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
  8753. }
  8754. if (!tg3_flag(tp, USE_PHYLIB)) {
  8755. if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
  8756. tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
  8757. err = tg3_setup_phy(tp, false);
  8758. if (err)
  8759. return err;
  8760. if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
  8761. !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
  8762. u32 tmp;
  8763. /* Clear CRC stats. */
  8764. if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
  8765. tg3_writephy(tp, MII_TG3_TEST1,
  8766. tmp | MII_TG3_TEST1_CRC_EN);
  8767. tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
  8768. }
  8769. }
  8770. }
  8771. __tg3_set_rx_mode(tp->dev);
  8772. /* Initialize receive rules. */
  8773. tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
  8774. tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
  8775. tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
  8776. tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
  8777. if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS))
  8778. limit = 8;
  8779. else
  8780. limit = 16;
  8781. if (tg3_flag(tp, ENABLE_ASF))
  8782. limit -= 4;
  8783. switch (limit) {
  8784. case 16:
  8785. tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
  8786. case 15:
  8787. tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
  8788. case 14:
  8789. tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
  8790. case 13:
  8791. tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
  8792. case 12:
  8793. tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
  8794. case 11:
  8795. tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
  8796. case 10:
  8797. tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
  8798. case 9:
  8799. tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
  8800. case 8:
  8801. tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
  8802. case 7:
  8803. tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
  8804. case 6:
  8805. tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
  8806. case 5:
  8807. tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
  8808. case 4:
  8809. /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
  8810. case 3:
  8811. /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
  8812. case 2:
  8813. case 1:
  8814. default:
  8815. break;
  8816. }
  8817. if (tg3_flag(tp, ENABLE_APE))
  8818. /* Write our heartbeat update interval to APE. */
  8819. tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
  8820. APE_HOST_HEARTBEAT_INT_DISABLE);
  8821. tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
  8822. return 0;
  8823. }
  8824. /* Called at device open time to get the chip ready for
  8825. * packet processing. Invoked with tp->lock held.
  8826. */
  8827. static int tg3_init_hw(struct tg3 *tp, bool reset_phy)
  8828. {
  8829. /* Chip may have been just powered on. If so, the boot code may still
  8830. * be running initialization. Wait for it to finish to avoid races in
  8831. * accessing the hardware.
  8832. */
  8833. tg3_enable_register_access(tp);
  8834. tg3_poll_fw(tp);
  8835. tg3_switch_clocks(tp);
  8836. tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
  8837. return tg3_reset_hw(tp, reset_phy);
  8838. }
  8839. static void tg3_sd_scan_scratchpad(struct tg3 *tp, struct tg3_ocir *ocir)
  8840. {
  8841. int i;
  8842. for (i = 0; i < TG3_SD_NUM_RECS; i++, ocir++) {
  8843. u32 off = i * TG3_OCIR_LEN, len = TG3_OCIR_LEN;
  8844. tg3_ape_scratchpad_read(tp, (u32 *) ocir, off, len);
  8845. off += len;
  8846. if (ocir->signature != TG3_OCIR_SIG_MAGIC ||
  8847. !(ocir->version_flags & TG3_OCIR_FLAG_ACTIVE))
  8848. memset(ocir, 0, TG3_OCIR_LEN);
  8849. }
  8850. }
  8851. /* sysfs attributes for hwmon */
  8852. static ssize_t tg3_show_temp(struct device *dev,
  8853. struct device_attribute *devattr, char *buf)
  8854. {
  8855. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  8856. struct tg3 *tp = dev_get_drvdata(dev);
  8857. u32 temperature;
  8858. spin_lock_bh(&tp->lock);
  8859. tg3_ape_scratchpad_read(tp, &temperature, attr->index,
  8860. sizeof(temperature));
  8861. spin_unlock_bh(&tp->lock);
  8862. return sprintf(buf, "%u\n", temperature * 1000);
  8863. }
  8864. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, tg3_show_temp, NULL,
  8865. TG3_TEMP_SENSOR_OFFSET);
  8866. static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, tg3_show_temp, NULL,
  8867. TG3_TEMP_CAUTION_OFFSET);
  8868. static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, tg3_show_temp, NULL,
  8869. TG3_TEMP_MAX_OFFSET);
  8870. static struct attribute *tg3_attrs[] = {
  8871. &sensor_dev_attr_temp1_input.dev_attr.attr,
  8872. &sensor_dev_attr_temp1_crit.dev_attr.attr,
  8873. &sensor_dev_attr_temp1_max.dev_attr.attr,
  8874. NULL
  8875. };
  8876. ATTRIBUTE_GROUPS(tg3);
  8877. static void tg3_hwmon_close(struct tg3 *tp)
  8878. {
  8879. if (tp->hwmon_dev) {
  8880. hwmon_device_unregister(tp->hwmon_dev);
  8881. tp->hwmon_dev = NULL;
  8882. }
  8883. }
  8884. static void tg3_hwmon_open(struct tg3 *tp)
  8885. {
  8886. int i;
  8887. u32 size = 0;
  8888. struct pci_dev *pdev = tp->pdev;
  8889. struct tg3_ocir ocirs[TG3_SD_NUM_RECS];
  8890. tg3_sd_scan_scratchpad(tp, ocirs);
  8891. for (i = 0; i < TG3_SD_NUM_RECS; i++) {
  8892. if (!ocirs[i].src_data_length)
  8893. continue;
  8894. size += ocirs[i].src_hdr_length;
  8895. size += ocirs[i].src_data_length;
  8896. }
  8897. if (!size)
  8898. return;
  8899. tp->hwmon_dev = hwmon_device_register_with_groups(&pdev->dev, "tg3",
  8900. tp, tg3_groups);
  8901. if (IS_ERR(tp->hwmon_dev)) {
  8902. tp->hwmon_dev = NULL;
  8903. dev_err(&pdev->dev, "Cannot register hwmon device, aborting\n");
  8904. }
  8905. }
  8906. #define TG3_STAT_ADD32(PSTAT, REG) \
  8907. do { u32 __val = tr32(REG); \
  8908. (PSTAT)->low += __val; \
  8909. if ((PSTAT)->low < __val) \
  8910. (PSTAT)->high += 1; \
  8911. } while (0)
  8912. static void tg3_periodic_fetch_stats(struct tg3 *tp)
  8913. {
  8914. struct tg3_hw_stats *sp = tp->hw_stats;
  8915. if (!tp->link_up)
  8916. return;
  8917. TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
  8918. TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
  8919. TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
  8920. TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
  8921. TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
  8922. TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
  8923. TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
  8924. TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
  8925. TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
  8926. TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
  8927. TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
  8928. TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
  8929. TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
  8930. if (unlikely(tg3_flag(tp, 5719_5720_RDMA_BUG) &&
  8931. (sp->tx_ucast_packets.low + sp->tx_mcast_packets.low +
  8932. sp->tx_bcast_packets.low) > TG3_NUM_RDMA_CHANNELS)) {
  8933. u32 val;
  8934. val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
  8935. val &= ~tg3_lso_rd_dma_workaround_bit(tp);
  8936. tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val);
  8937. tg3_flag_clear(tp, 5719_5720_RDMA_BUG);
  8938. }
  8939. TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
  8940. TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
  8941. TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
  8942. TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
  8943. TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
  8944. TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
  8945. TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
  8946. TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
  8947. TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
  8948. TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
  8949. TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
  8950. TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
  8951. TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
  8952. TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
  8953. TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
  8954. if (tg3_asic_rev(tp) != ASIC_REV_5717 &&
  8955. tg3_asic_rev(tp) != ASIC_REV_5762 &&
  8956. tg3_chip_rev_id(tp) != CHIPREV_ID_5719_A0 &&
  8957. tg3_chip_rev_id(tp) != CHIPREV_ID_5720_A0) {
  8958. TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
  8959. } else {
  8960. u32 val = tr32(HOSTCC_FLOW_ATTN);
  8961. val = (val & HOSTCC_FLOW_ATTN_MBUF_LWM) ? 1 : 0;
  8962. if (val) {
  8963. tw32(HOSTCC_FLOW_ATTN, HOSTCC_FLOW_ATTN_MBUF_LWM);
  8964. sp->rx_discards.low += val;
  8965. if (sp->rx_discards.low < val)
  8966. sp->rx_discards.high += 1;
  8967. }
  8968. sp->mbuf_lwm_thresh_hit = sp->rx_discards;
  8969. }
  8970. TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
  8971. }
  8972. static void tg3_chk_missed_msi(struct tg3 *tp)
  8973. {
  8974. u32 i;
  8975. for (i = 0; i < tp->irq_cnt; i++) {
  8976. struct tg3_napi *tnapi = &tp->napi[i];
  8977. if (tg3_has_work(tnapi)) {
  8978. if (tnapi->last_rx_cons == tnapi->rx_rcb_ptr &&
  8979. tnapi->last_tx_cons == tnapi->tx_cons) {
  8980. if (tnapi->chk_msi_cnt < 1) {
  8981. tnapi->chk_msi_cnt++;
  8982. return;
  8983. }
  8984. tg3_msi(0, tnapi);
  8985. }
  8986. }
  8987. tnapi->chk_msi_cnt = 0;
  8988. tnapi->last_rx_cons = tnapi->rx_rcb_ptr;
  8989. tnapi->last_tx_cons = tnapi->tx_cons;
  8990. }
  8991. }
  8992. static void tg3_timer(unsigned long __opaque)
  8993. {
  8994. struct tg3 *tp = (struct tg3 *) __opaque;
  8995. spin_lock(&tp->lock);
  8996. if (tp->irq_sync || tg3_flag(tp, RESET_TASK_PENDING)) {
  8997. spin_unlock(&tp->lock);
  8998. goto restart_timer;
  8999. }
  9000. if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
  9001. tg3_flag(tp, 57765_CLASS))
  9002. tg3_chk_missed_msi(tp);
  9003. if (tg3_flag(tp, FLUSH_POSTED_WRITES)) {
  9004. /* BCM4785: Flush posted writes from GbE to host memory. */
  9005. tr32(HOSTCC_MODE);
  9006. }
  9007. if (!tg3_flag(tp, TAGGED_STATUS)) {
  9008. /* All of this garbage is because when using non-tagged
  9009. * IRQ status the mailbox/status_block protocol the chip
  9010. * uses with the cpu is race prone.
  9011. */
  9012. if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
  9013. tw32(GRC_LOCAL_CTRL,
  9014. tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
  9015. } else {
  9016. tw32(HOSTCC_MODE, tp->coalesce_mode |
  9017. HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
  9018. }
  9019. if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
  9020. spin_unlock(&tp->lock);
  9021. tg3_reset_task_schedule(tp);
  9022. goto restart_timer;
  9023. }
  9024. }
  9025. /* This part only runs once per second. */
  9026. if (!--tp->timer_counter) {
  9027. if (tg3_flag(tp, 5705_PLUS))
  9028. tg3_periodic_fetch_stats(tp);
  9029. if (tp->setlpicnt && !--tp->setlpicnt)
  9030. tg3_phy_eee_enable(tp);
  9031. if (tg3_flag(tp, USE_LINKCHG_REG)) {
  9032. u32 mac_stat;
  9033. int phy_event;
  9034. mac_stat = tr32(MAC_STATUS);
  9035. phy_event = 0;
  9036. if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
  9037. if (mac_stat & MAC_STATUS_MI_INTERRUPT)
  9038. phy_event = 1;
  9039. } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
  9040. phy_event = 1;
  9041. if (phy_event)
  9042. tg3_setup_phy(tp, false);
  9043. } else if (tg3_flag(tp, POLL_SERDES)) {
  9044. u32 mac_stat = tr32(MAC_STATUS);
  9045. int need_setup = 0;
  9046. if (tp->link_up &&
  9047. (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
  9048. need_setup = 1;
  9049. }
  9050. if (!tp->link_up &&
  9051. (mac_stat & (MAC_STATUS_PCS_SYNCED |
  9052. MAC_STATUS_SIGNAL_DET))) {
  9053. need_setup = 1;
  9054. }
  9055. if (need_setup) {
  9056. if (!tp->serdes_counter) {
  9057. tw32_f(MAC_MODE,
  9058. (tp->mac_mode &
  9059. ~MAC_MODE_PORT_MODE_MASK));
  9060. udelay(40);
  9061. tw32_f(MAC_MODE, tp->mac_mode);
  9062. udelay(40);
  9063. }
  9064. tg3_setup_phy(tp, false);
  9065. }
  9066. } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
  9067. tg3_flag(tp, 5780_CLASS)) {
  9068. tg3_serdes_parallel_detect(tp);
  9069. } else if (tg3_flag(tp, POLL_CPMU_LINK)) {
  9070. u32 cpmu = tr32(TG3_CPMU_STATUS);
  9071. bool link_up = !((cpmu & TG3_CPMU_STATUS_LINK_MASK) ==
  9072. TG3_CPMU_STATUS_LINK_MASK);
  9073. if (link_up != tp->link_up)
  9074. tg3_setup_phy(tp, false);
  9075. }
  9076. tp->timer_counter = tp->timer_multiplier;
  9077. }
  9078. /* Heartbeat is only sent once every 2 seconds.
  9079. *
  9080. * The heartbeat is to tell the ASF firmware that the host
  9081. * driver is still alive. In the event that the OS crashes,
  9082. * ASF needs to reset the hardware to free up the FIFO space
  9083. * that may be filled with rx packets destined for the host.
  9084. * If the FIFO is full, ASF will no longer function properly.
  9085. *
  9086. * Unintended resets have been reported on real time kernels
  9087. * where the timer doesn't run on time. Netpoll will also have
  9088. * same problem.
  9089. *
  9090. * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
  9091. * to check the ring condition when the heartbeat is expiring
  9092. * before doing the reset. This will prevent most unintended
  9093. * resets.
  9094. */
  9095. if (!--tp->asf_counter) {
  9096. if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
  9097. tg3_wait_for_event_ack(tp);
  9098. tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
  9099. FWCMD_NICDRV_ALIVE3);
  9100. tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
  9101. tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
  9102. TG3_FW_UPDATE_TIMEOUT_SEC);
  9103. tg3_generate_fw_event(tp);
  9104. }
  9105. tp->asf_counter = tp->asf_multiplier;
  9106. }
  9107. spin_unlock(&tp->lock);
  9108. restart_timer:
  9109. tp->timer.expires = jiffies + tp->timer_offset;
  9110. add_timer(&tp->timer);
  9111. }
  9112. static void tg3_timer_init(struct tg3 *tp)
  9113. {
  9114. if (tg3_flag(tp, TAGGED_STATUS) &&
  9115. tg3_asic_rev(tp) != ASIC_REV_5717 &&
  9116. !tg3_flag(tp, 57765_CLASS))
  9117. tp->timer_offset = HZ;
  9118. else
  9119. tp->timer_offset = HZ / 10;
  9120. BUG_ON(tp->timer_offset > HZ);
  9121. tp->timer_multiplier = (HZ / tp->timer_offset);
  9122. tp->asf_multiplier = (HZ / tp->timer_offset) *
  9123. TG3_FW_UPDATE_FREQ_SEC;
  9124. init_timer(&tp->timer);
  9125. tp->timer.data = (unsigned long) tp;
  9126. tp->timer.function = tg3_timer;
  9127. }
  9128. static void tg3_timer_start(struct tg3 *tp)
  9129. {
  9130. tp->asf_counter = tp->asf_multiplier;
  9131. tp->timer_counter = tp->timer_multiplier;
  9132. tp->timer.expires = jiffies + tp->timer_offset;
  9133. add_timer(&tp->timer);
  9134. }
  9135. static void tg3_timer_stop(struct tg3 *tp)
  9136. {
  9137. del_timer_sync(&tp->timer);
  9138. }
  9139. /* Restart hardware after configuration changes, self-test, etc.
  9140. * Invoked with tp->lock held.
  9141. */
  9142. static int tg3_restart_hw(struct tg3 *tp, bool reset_phy)
  9143. __releases(tp->lock)
  9144. __acquires(tp->lock)
  9145. {
  9146. int err;
  9147. err = tg3_init_hw(tp, reset_phy);
  9148. if (err) {
  9149. netdev_err(tp->dev,
  9150. "Failed to re-initialize device, aborting\n");
  9151. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  9152. tg3_full_unlock(tp);
  9153. tg3_timer_stop(tp);
  9154. tp->irq_sync = 0;
  9155. tg3_napi_enable(tp);
  9156. dev_close(tp->dev);
  9157. tg3_full_lock(tp, 0);
  9158. }
  9159. return err;
  9160. }
  9161. static void tg3_reset_task(struct work_struct *work)
  9162. {
  9163. struct tg3 *tp = container_of(work, struct tg3, reset_task);
  9164. int err;
  9165. rtnl_lock();
  9166. tg3_full_lock(tp, 0);
  9167. if (!netif_running(tp->dev)) {
  9168. tg3_flag_clear(tp, RESET_TASK_PENDING);
  9169. tg3_full_unlock(tp);
  9170. rtnl_unlock();
  9171. return;
  9172. }
  9173. tg3_full_unlock(tp);
  9174. tg3_phy_stop(tp);
  9175. tg3_netif_stop(tp);
  9176. tg3_full_lock(tp, 1);
  9177. if (tg3_flag(tp, TX_RECOVERY_PENDING)) {
  9178. tp->write32_tx_mbox = tg3_write32_tx_mbox;
  9179. tp->write32_rx_mbox = tg3_write_flush_reg32;
  9180. tg3_flag_set(tp, MBOX_WRITE_REORDER);
  9181. tg3_flag_clear(tp, TX_RECOVERY_PENDING);
  9182. }
  9183. tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
  9184. err = tg3_init_hw(tp, true);
  9185. if (err)
  9186. goto out;
  9187. tg3_netif_start(tp);
  9188. out:
  9189. tg3_full_unlock(tp);
  9190. if (!err)
  9191. tg3_phy_start(tp);
  9192. tg3_flag_clear(tp, RESET_TASK_PENDING);
  9193. rtnl_unlock();
  9194. }
  9195. static int tg3_request_irq(struct tg3 *tp, int irq_num)
  9196. {
  9197. irq_handler_t fn;
  9198. unsigned long flags;
  9199. char *name;
  9200. struct tg3_napi *tnapi = &tp->napi[irq_num];
  9201. if (tp->irq_cnt == 1)
  9202. name = tp->dev->name;
  9203. else {
  9204. name = &tnapi->irq_lbl[0];
  9205. if (tnapi->tx_buffers && tnapi->rx_rcb)
  9206. snprintf(name, IFNAMSIZ,
  9207. "%s-txrx-%d", tp->dev->name, irq_num);
  9208. else if (tnapi->tx_buffers)
  9209. snprintf(name, IFNAMSIZ,
  9210. "%s-tx-%d", tp->dev->name, irq_num);
  9211. else if (tnapi->rx_rcb)
  9212. snprintf(name, IFNAMSIZ,
  9213. "%s-rx-%d", tp->dev->name, irq_num);
  9214. else
  9215. snprintf(name, IFNAMSIZ,
  9216. "%s-%d", tp->dev->name, irq_num);
  9217. name[IFNAMSIZ-1] = 0;
  9218. }
  9219. if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
  9220. fn = tg3_msi;
  9221. if (tg3_flag(tp, 1SHOT_MSI))
  9222. fn = tg3_msi_1shot;
  9223. flags = 0;
  9224. } else {
  9225. fn = tg3_interrupt;
  9226. if (tg3_flag(tp, TAGGED_STATUS))
  9227. fn = tg3_interrupt_tagged;
  9228. flags = IRQF_SHARED;
  9229. }
  9230. return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
  9231. }
  9232. static int tg3_test_interrupt(struct tg3 *tp)
  9233. {
  9234. struct tg3_napi *tnapi = &tp->napi[0];
  9235. struct net_device *dev = tp->dev;
  9236. int err, i, intr_ok = 0;
  9237. u32 val;
  9238. if (!netif_running(dev))
  9239. return -ENODEV;
  9240. tg3_disable_ints(tp);
  9241. free_irq(tnapi->irq_vec, tnapi);
  9242. /*
  9243. * Turn off MSI one shot mode. Otherwise this test has no
  9244. * observable way to know whether the interrupt was delivered.
  9245. */
  9246. if (tg3_flag(tp, 57765_PLUS)) {
  9247. val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
  9248. tw32(MSGINT_MODE, val);
  9249. }
  9250. err = request_irq(tnapi->irq_vec, tg3_test_isr,
  9251. IRQF_SHARED, dev->name, tnapi);
  9252. if (err)
  9253. return err;
  9254. tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
  9255. tg3_enable_ints(tp);
  9256. tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
  9257. tnapi->coal_now);
  9258. for (i = 0; i < 5; i++) {
  9259. u32 int_mbox, misc_host_ctrl;
  9260. int_mbox = tr32_mailbox(tnapi->int_mbox);
  9261. misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
  9262. if ((int_mbox != 0) ||
  9263. (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
  9264. intr_ok = 1;
  9265. break;
  9266. }
  9267. if (tg3_flag(tp, 57765_PLUS) &&
  9268. tnapi->hw_status->status_tag != tnapi->last_tag)
  9269. tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
  9270. msleep(10);
  9271. }
  9272. tg3_disable_ints(tp);
  9273. free_irq(tnapi->irq_vec, tnapi);
  9274. err = tg3_request_irq(tp, 0);
  9275. if (err)
  9276. return err;
  9277. if (intr_ok) {
  9278. /* Reenable MSI one shot mode. */
  9279. if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, 1SHOT_MSI)) {
  9280. val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
  9281. tw32(MSGINT_MODE, val);
  9282. }
  9283. return 0;
  9284. }
  9285. return -EIO;
  9286. }
  9287. /* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
  9288. * successfully restored
  9289. */
  9290. static int tg3_test_msi(struct tg3 *tp)
  9291. {
  9292. int err;
  9293. u16 pci_cmd;
  9294. if (!tg3_flag(tp, USING_MSI))
  9295. return 0;
  9296. /* Turn off SERR reporting in case MSI terminates with Master
  9297. * Abort.
  9298. */
  9299. pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
  9300. pci_write_config_word(tp->pdev, PCI_COMMAND,
  9301. pci_cmd & ~PCI_COMMAND_SERR);
  9302. err = tg3_test_interrupt(tp);
  9303. pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
  9304. if (!err)
  9305. return 0;
  9306. /* other failures */
  9307. if (err != -EIO)
  9308. return err;
  9309. /* MSI test failed, go back to INTx mode */
  9310. netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
  9311. "to INTx mode. Please report this failure to the PCI "
  9312. "maintainer and include system chipset information\n");
  9313. free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
  9314. pci_disable_msi(tp->pdev);
  9315. tg3_flag_clear(tp, USING_MSI);
  9316. tp->napi[0].irq_vec = tp->pdev->irq;
  9317. err = tg3_request_irq(tp, 0);
  9318. if (err)
  9319. return err;
  9320. /* Need to reset the chip because the MSI cycle may have terminated
  9321. * with Master Abort.
  9322. */
  9323. tg3_full_lock(tp, 1);
  9324. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  9325. err = tg3_init_hw(tp, true);
  9326. tg3_full_unlock(tp);
  9327. if (err)
  9328. free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
  9329. return err;
  9330. }
  9331. static int tg3_request_firmware(struct tg3 *tp)
  9332. {
  9333. const struct tg3_firmware_hdr *fw_hdr;
  9334. if (reject_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
  9335. netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
  9336. tp->fw_needed);
  9337. return -ENOENT;
  9338. }
  9339. fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
  9340. /* Firmware blob starts with version numbers, followed by
  9341. * start address and _full_ length including BSS sections
  9342. * (which must be longer than the actual data, of course
  9343. */
  9344. tp->fw_len = be32_to_cpu(fw_hdr->len); /* includes bss */
  9345. if (tp->fw_len < (tp->fw->size - TG3_FW_HDR_LEN)) {
  9346. netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
  9347. tp->fw_len, tp->fw_needed);
  9348. release_firmware(tp->fw);
  9349. tp->fw = NULL;
  9350. return -EINVAL;
  9351. }
  9352. /* We no longer need firmware; we have it. */
  9353. tp->fw_needed = NULL;
  9354. return 0;
  9355. }
  9356. static u32 tg3_irq_count(struct tg3 *tp)
  9357. {
  9358. u32 irq_cnt = max(tp->rxq_cnt, tp->txq_cnt);
  9359. if (irq_cnt > 1) {
  9360. /* We want as many rx rings enabled as there are cpus.
  9361. * In multiqueue MSI-X mode, the first MSI-X vector
  9362. * only deals with link interrupts, etc, so we add
  9363. * one to the number of vectors we are requesting.
  9364. */
  9365. irq_cnt = min_t(unsigned, irq_cnt + 1, tp->irq_max);
  9366. }
  9367. return irq_cnt;
  9368. }
  9369. static bool tg3_enable_msix(struct tg3 *tp)
  9370. {
  9371. int i, rc;
  9372. struct msix_entry msix_ent[TG3_IRQ_MAX_VECS];
  9373. tp->txq_cnt = tp->txq_req;
  9374. tp->rxq_cnt = tp->rxq_req;
  9375. if (!tp->rxq_cnt)
  9376. tp->rxq_cnt = netif_get_num_default_rss_queues();
  9377. if (tp->rxq_cnt > tp->rxq_max)
  9378. tp->rxq_cnt = tp->rxq_max;
  9379. /* Disable multiple TX rings by default. Simple round-robin hardware
  9380. * scheduling of the TX rings can cause starvation of rings with
  9381. * small packets when other rings have TSO or jumbo packets.
  9382. */
  9383. if (!tp->txq_req)
  9384. tp->txq_cnt = 1;
  9385. tp->irq_cnt = tg3_irq_count(tp);
  9386. for (i = 0; i < tp->irq_max; i++) {
  9387. msix_ent[i].entry = i;
  9388. msix_ent[i].vector = 0;
  9389. }
  9390. rc = pci_enable_msix_range(tp->pdev, msix_ent, 1, tp->irq_cnt);
  9391. if (rc < 0) {
  9392. return false;
  9393. } else if (rc < tp->irq_cnt) {
  9394. netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
  9395. tp->irq_cnt, rc);
  9396. tp->irq_cnt = rc;
  9397. tp->rxq_cnt = max(rc - 1, 1);
  9398. if (tp->txq_cnt)
  9399. tp->txq_cnt = min(tp->rxq_cnt, tp->txq_max);
  9400. }
  9401. for (i = 0; i < tp->irq_max; i++)
  9402. tp->napi[i].irq_vec = msix_ent[i].vector;
  9403. if (netif_set_real_num_rx_queues(tp->dev, tp->rxq_cnt)) {
  9404. pci_disable_msix(tp->pdev);
  9405. return false;
  9406. }
  9407. if (tp->irq_cnt == 1)
  9408. return true;
  9409. tg3_flag_set(tp, ENABLE_RSS);
  9410. if (tp->txq_cnt > 1)
  9411. tg3_flag_set(tp, ENABLE_TSS);
  9412. netif_set_real_num_tx_queues(tp->dev, tp->txq_cnt);
  9413. return true;
  9414. }
  9415. static void tg3_ints_init(struct tg3 *tp)
  9416. {
  9417. if ((tg3_flag(tp, SUPPORT_MSI) || tg3_flag(tp, SUPPORT_MSIX)) &&
  9418. !tg3_flag(tp, TAGGED_STATUS)) {
  9419. /* All MSI supporting chips should support tagged
  9420. * status. Assert that this is the case.
  9421. */
  9422. netdev_warn(tp->dev,
  9423. "MSI without TAGGED_STATUS? Not using MSI\n");
  9424. goto defcfg;
  9425. }
  9426. if (tg3_flag(tp, SUPPORT_MSIX) && tg3_enable_msix(tp))
  9427. tg3_flag_set(tp, USING_MSIX);
  9428. else if (tg3_flag(tp, SUPPORT_MSI) && pci_enable_msi(tp->pdev) == 0)
  9429. tg3_flag_set(tp, USING_MSI);
  9430. if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
  9431. u32 msi_mode = tr32(MSGINT_MODE);
  9432. if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1)
  9433. msi_mode |= MSGINT_MODE_MULTIVEC_EN;
  9434. if (!tg3_flag(tp, 1SHOT_MSI))
  9435. msi_mode |= MSGINT_MODE_ONE_SHOT_DISABLE;
  9436. tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
  9437. }
  9438. defcfg:
  9439. if (!tg3_flag(tp, USING_MSIX)) {
  9440. tp->irq_cnt = 1;
  9441. tp->napi[0].irq_vec = tp->pdev->irq;
  9442. }
  9443. if (tp->irq_cnt == 1) {
  9444. tp->txq_cnt = 1;
  9445. tp->rxq_cnt = 1;
  9446. netif_set_real_num_tx_queues(tp->dev, 1);
  9447. netif_set_real_num_rx_queues(tp->dev, 1);
  9448. }
  9449. }
  9450. static void tg3_ints_fini(struct tg3 *tp)
  9451. {
  9452. if (tg3_flag(tp, USING_MSIX))
  9453. pci_disable_msix(tp->pdev);
  9454. else if (tg3_flag(tp, USING_MSI))
  9455. pci_disable_msi(tp->pdev);
  9456. tg3_flag_clear(tp, USING_MSI);
  9457. tg3_flag_clear(tp, USING_MSIX);
  9458. tg3_flag_clear(tp, ENABLE_RSS);
  9459. tg3_flag_clear(tp, ENABLE_TSS);
  9460. }
  9461. static int tg3_start(struct tg3 *tp, bool reset_phy, bool test_irq,
  9462. bool init)
  9463. {
  9464. struct net_device *dev = tp->dev;
  9465. int i, err;
  9466. /*
  9467. * Setup interrupts first so we know how
  9468. * many NAPI resources to allocate
  9469. */
  9470. tg3_ints_init(tp);
  9471. tg3_rss_check_indir_tbl(tp);
  9472. /* The placement of this call is tied
  9473. * to the setup and use of Host TX descriptors.
  9474. */
  9475. err = tg3_alloc_consistent(tp);
  9476. if (err)
  9477. goto out_ints_fini;
  9478. tg3_napi_init(tp);
  9479. tg3_napi_enable(tp);
  9480. for (i = 0; i < tp->irq_cnt; i++) {
  9481. struct tg3_napi *tnapi = &tp->napi[i];
  9482. err = tg3_request_irq(tp, i);
  9483. if (err) {
  9484. for (i--; i >= 0; i--) {
  9485. tnapi = &tp->napi[i];
  9486. free_irq(tnapi->irq_vec, tnapi);
  9487. }
  9488. goto out_napi_fini;
  9489. }
  9490. }
  9491. tg3_full_lock(tp, 0);
  9492. if (init)
  9493. tg3_ape_driver_state_change(tp, RESET_KIND_INIT);
  9494. err = tg3_init_hw(tp, reset_phy);
  9495. if (err) {
  9496. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  9497. tg3_free_rings(tp);
  9498. }
  9499. tg3_full_unlock(tp);
  9500. if (err)
  9501. goto out_free_irq;
  9502. if (test_irq && tg3_flag(tp, USING_MSI)) {
  9503. err = tg3_test_msi(tp);
  9504. if (err) {
  9505. tg3_full_lock(tp, 0);
  9506. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  9507. tg3_free_rings(tp);
  9508. tg3_full_unlock(tp);
  9509. goto out_napi_fini;
  9510. }
  9511. if (!tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
  9512. u32 val = tr32(PCIE_TRANSACTION_CFG);
  9513. tw32(PCIE_TRANSACTION_CFG,
  9514. val | PCIE_TRANS_CFG_1SHOT_MSI);
  9515. }
  9516. }
  9517. tg3_phy_start(tp);
  9518. tg3_hwmon_open(tp);
  9519. tg3_full_lock(tp, 0);
  9520. tg3_timer_start(tp);
  9521. tg3_flag_set(tp, INIT_COMPLETE);
  9522. tg3_enable_ints(tp);
  9523. tg3_ptp_resume(tp);
  9524. tg3_full_unlock(tp);
  9525. netif_tx_start_all_queues(dev);
  9526. /*
  9527. * Reset loopback feature if it was turned on while the device was down
  9528. * make sure that it's installed properly now.
  9529. */
  9530. if (dev->features & NETIF_F_LOOPBACK)
  9531. tg3_set_loopback(dev, dev->features);
  9532. return 0;
  9533. out_free_irq:
  9534. for (i = tp->irq_cnt - 1; i >= 0; i--) {
  9535. struct tg3_napi *tnapi = &tp->napi[i];
  9536. free_irq(tnapi->irq_vec, tnapi);
  9537. }
  9538. out_napi_fini:
  9539. tg3_napi_disable(tp);
  9540. tg3_napi_fini(tp);
  9541. tg3_free_consistent(tp);
  9542. out_ints_fini:
  9543. tg3_ints_fini(tp);
  9544. return err;
  9545. }
  9546. static void tg3_stop(struct tg3 *tp)
  9547. {
  9548. int i;
  9549. tg3_reset_task_cancel(tp);
  9550. tg3_netif_stop(tp);
  9551. tg3_timer_stop(tp);
  9552. tg3_hwmon_close(tp);
  9553. tg3_phy_stop(tp);
  9554. tg3_full_lock(tp, 1);
  9555. tg3_disable_ints(tp);
  9556. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  9557. tg3_free_rings(tp);
  9558. tg3_flag_clear(tp, INIT_COMPLETE);
  9559. tg3_full_unlock(tp);
  9560. for (i = tp->irq_cnt - 1; i >= 0; i--) {
  9561. struct tg3_napi *tnapi = &tp->napi[i];
  9562. free_irq(tnapi->irq_vec, tnapi);
  9563. }
  9564. tg3_ints_fini(tp);
  9565. tg3_napi_fini(tp);
  9566. tg3_free_consistent(tp);
  9567. }
  9568. static int tg3_open(struct net_device *dev)
  9569. {
  9570. struct tg3 *tp = netdev_priv(dev);
  9571. int err;
  9572. if (tp->pcierr_recovery) {
  9573. netdev_err(dev, "Failed to open device. PCI error recovery "
  9574. "in progress\n");
  9575. return -EAGAIN;
  9576. }
  9577. if (tp->fw_needed) {
  9578. err = tg3_request_firmware(tp);
  9579. if (tg3_asic_rev(tp) == ASIC_REV_57766) {
  9580. if (err) {
  9581. netdev_warn(tp->dev, "EEE capability disabled\n");
  9582. tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
  9583. } else if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP)) {
  9584. netdev_warn(tp->dev, "EEE capability restored\n");
  9585. tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
  9586. }
  9587. } else if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0) {
  9588. if (err)
  9589. return err;
  9590. } else if (err) {
  9591. netdev_warn(tp->dev, "TSO capability disabled\n");
  9592. tg3_flag_clear(tp, TSO_CAPABLE);
  9593. } else if (!tg3_flag(tp, TSO_CAPABLE)) {
  9594. netdev_notice(tp->dev, "TSO capability restored\n");
  9595. tg3_flag_set(tp, TSO_CAPABLE);
  9596. }
  9597. }
  9598. tg3_carrier_off(tp);
  9599. err = tg3_power_up(tp);
  9600. if (err)
  9601. return err;
  9602. tg3_full_lock(tp, 0);
  9603. tg3_disable_ints(tp);
  9604. tg3_flag_clear(tp, INIT_COMPLETE);
  9605. tg3_full_unlock(tp);
  9606. err = tg3_start(tp,
  9607. !(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN),
  9608. true, true);
  9609. if (err) {
  9610. tg3_frob_aux_power(tp, false);
  9611. pci_set_power_state(tp->pdev, PCI_D3hot);
  9612. }
  9613. return err;
  9614. }
  9615. static int tg3_close(struct net_device *dev)
  9616. {
  9617. struct tg3 *tp = netdev_priv(dev);
  9618. if (tp->pcierr_recovery) {
  9619. netdev_err(dev, "Failed to close device. PCI error recovery "
  9620. "in progress\n");
  9621. return -EAGAIN;
  9622. }
  9623. tg3_stop(tp);
  9624. /* Clear stats across close / open calls */
  9625. memset(&tp->net_stats_prev, 0, sizeof(tp->net_stats_prev));
  9626. memset(&tp->estats_prev, 0, sizeof(tp->estats_prev));
  9627. if (pci_device_is_present(tp->pdev)) {
  9628. tg3_power_down_prepare(tp);
  9629. tg3_carrier_off(tp);
  9630. }
  9631. return 0;
  9632. }
  9633. static inline u64 get_stat64(tg3_stat64_t *val)
  9634. {
  9635. return ((u64)val->high << 32) | ((u64)val->low);
  9636. }
  9637. static u64 tg3_calc_crc_errors(struct tg3 *tp)
  9638. {
  9639. struct tg3_hw_stats *hw_stats = tp->hw_stats;
  9640. if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
  9641. (tg3_asic_rev(tp) == ASIC_REV_5700 ||
  9642. tg3_asic_rev(tp) == ASIC_REV_5701)) {
  9643. u32 val;
  9644. if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
  9645. tg3_writephy(tp, MII_TG3_TEST1,
  9646. val | MII_TG3_TEST1_CRC_EN);
  9647. tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
  9648. } else
  9649. val = 0;
  9650. tp->phy_crc_errors += val;
  9651. return tp->phy_crc_errors;
  9652. }
  9653. return get_stat64(&hw_stats->rx_fcs_errors);
  9654. }
  9655. #define ESTAT_ADD(member) \
  9656. estats->member = old_estats->member + \
  9657. get_stat64(&hw_stats->member)
  9658. static void tg3_get_estats(struct tg3 *tp, struct tg3_ethtool_stats *estats)
  9659. {
  9660. struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
  9661. struct tg3_hw_stats *hw_stats = tp->hw_stats;
  9662. ESTAT_ADD(rx_octets);
  9663. ESTAT_ADD(rx_fragments);
  9664. ESTAT_ADD(rx_ucast_packets);
  9665. ESTAT_ADD(rx_mcast_packets);
  9666. ESTAT_ADD(rx_bcast_packets);
  9667. ESTAT_ADD(rx_fcs_errors);
  9668. ESTAT_ADD(rx_align_errors);
  9669. ESTAT_ADD(rx_xon_pause_rcvd);
  9670. ESTAT_ADD(rx_xoff_pause_rcvd);
  9671. ESTAT_ADD(rx_mac_ctrl_rcvd);
  9672. ESTAT_ADD(rx_xoff_entered);
  9673. ESTAT_ADD(rx_frame_too_long_errors);
  9674. ESTAT_ADD(rx_jabbers);
  9675. ESTAT_ADD(rx_undersize_packets);
  9676. ESTAT_ADD(rx_in_length_errors);
  9677. ESTAT_ADD(rx_out_length_errors);
  9678. ESTAT_ADD(rx_64_or_less_octet_packets);
  9679. ESTAT_ADD(rx_65_to_127_octet_packets);
  9680. ESTAT_ADD(rx_128_to_255_octet_packets);
  9681. ESTAT_ADD(rx_256_to_511_octet_packets);
  9682. ESTAT_ADD(rx_512_to_1023_octet_packets);
  9683. ESTAT_ADD(rx_1024_to_1522_octet_packets);
  9684. ESTAT_ADD(rx_1523_to_2047_octet_packets);
  9685. ESTAT_ADD(rx_2048_to_4095_octet_packets);
  9686. ESTAT_ADD(rx_4096_to_8191_octet_packets);
  9687. ESTAT_ADD(rx_8192_to_9022_octet_packets);
  9688. ESTAT_ADD(tx_octets);
  9689. ESTAT_ADD(tx_collisions);
  9690. ESTAT_ADD(tx_xon_sent);
  9691. ESTAT_ADD(tx_xoff_sent);
  9692. ESTAT_ADD(tx_flow_control);
  9693. ESTAT_ADD(tx_mac_errors);
  9694. ESTAT_ADD(tx_single_collisions);
  9695. ESTAT_ADD(tx_mult_collisions);
  9696. ESTAT_ADD(tx_deferred);
  9697. ESTAT_ADD(tx_excessive_collisions);
  9698. ESTAT_ADD(tx_late_collisions);
  9699. ESTAT_ADD(tx_collide_2times);
  9700. ESTAT_ADD(tx_collide_3times);
  9701. ESTAT_ADD(tx_collide_4times);
  9702. ESTAT_ADD(tx_collide_5times);
  9703. ESTAT_ADD(tx_collide_6times);
  9704. ESTAT_ADD(tx_collide_7times);
  9705. ESTAT_ADD(tx_collide_8times);
  9706. ESTAT_ADD(tx_collide_9times);
  9707. ESTAT_ADD(tx_collide_10times);
  9708. ESTAT_ADD(tx_collide_11times);
  9709. ESTAT_ADD(tx_collide_12times);
  9710. ESTAT_ADD(tx_collide_13times);
  9711. ESTAT_ADD(tx_collide_14times);
  9712. ESTAT_ADD(tx_collide_15times);
  9713. ESTAT_ADD(tx_ucast_packets);
  9714. ESTAT_ADD(tx_mcast_packets);
  9715. ESTAT_ADD(tx_bcast_packets);
  9716. ESTAT_ADD(tx_carrier_sense_errors);
  9717. ESTAT_ADD(tx_discards);
  9718. ESTAT_ADD(tx_errors);
  9719. ESTAT_ADD(dma_writeq_full);
  9720. ESTAT_ADD(dma_write_prioq_full);
  9721. ESTAT_ADD(rxbds_empty);
  9722. ESTAT_ADD(rx_discards);
  9723. ESTAT_ADD(rx_errors);
  9724. ESTAT_ADD(rx_threshold_hit);
  9725. ESTAT_ADD(dma_readq_full);
  9726. ESTAT_ADD(dma_read_prioq_full);
  9727. ESTAT_ADD(tx_comp_queue_full);
  9728. ESTAT_ADD(ring_set_send_prod_index);
  9729. ESTAT_ADD(ring_status_update);
  9730. ESTAT_ADD(nic_irqs);
  9731. ESTAT_ADD(nic_avoided_irqs);
  9732. ESTAT_ADD(nic_tx_threshold_hit);
  9733. ESTAT_ADD(mbuf_lwm_thresh_hit);
  9734. }
  9735. static void tg3_get_nstats(struct tg3 *tp, struct rtnl_link_stats64 *stats)
  9736. {
  9737. struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
  9738. struct tg3_hw_stats *hw_stats = tp->hw_stats;
  9739. stats->rx_packets = old_stats->rx_packets +
  9740. get_stat64(&hw_stats->rx_ucast_packets) +
  9741. get_stat64(&hw_stats->rx_mcast_packets) +
  9742. get_stat64(&hw_stats->rx_bcast_packets);
  9743. stats->tx_packets = old_stats->tx_packets +
  9744. get_stat64(&hw_stats->tx_ucast_packets) +
  9745. get_stat64(&hw_stats->tx_mcast_packets) +
  9746. get_stat64(&hw_stats->tx_bcast_packets);
  9747. stats->rx_bytes = old_stats->rx_bytes +
  9748. get_stat64(&hw_stats->rx_octets);
  9749. stats->tx_bytes = old_stats->tx_bytes +
  9750. get_stat64(&hw_stats->tx_octets);
  9751. stats->rx_errors = old_stats->rx_errors +
  9752. get_stat64(&hw_stats->rx_errors);
  9753. stats->tx_errors = old_stats->tx_errors +
  9754. get_stat64(&hw_stats->tx_errors) +
  9755. get_stat64(&hw_stats->tx_mac_errors) +
  9756. get_stat64(&hw_stats->tx_carrier_sense_errors) +
  9757. get_stat64(&hw_stats->tx_discards);
  9758. stats->multicast = old_stats->multicast +
  9759. get_stat64(&hw_stats->rx_mcast_packets);
  9760. stats->collisions = old_stats->collisions +
  9761. get_stat64(&hw_stats->tx_collisions);
  9762. stats->rx_length_errors = old_stats->rx_length_errors +
  9763. get_stat64(&hw_stats->rx_frame_too_long_errors) +
  9764. get_stat64(&hw_stats->rx_undersize_packets);
  9765. stats->rx_frame_errors = old_stats->rx_frame_errors +
  9766. get_stat64(&hw_stats->rx_align_errors);
  9767. stats->tx_aborted_errors = old_stats->tx_aborted_errors +
  9768. get_stat64(&hw_stats->tx_discards);
  9769. stats->tx_carrier_errors = old_stats->tx_carrier_errors +
  9770. get_stat64(&hw_stats->tx_carrier_sense_errors);
  9771. stats->rx_crc_errors = old_stats->rx_crc_errors +
  9772. tg3_calc_crc_errors(tp);
  9773. stats->rx_missed_errors = old_stats->rx_missed_errors +
  9774. get_stat64(&hw_stats->rx_discards);
  9775. stats->rx_dropped = tp->rx_dropped;
  9776. stats->tx_dropped = tp->tx_dropped;
  9777. }
  9778. static int tg3_get_regs_len(struct net_device *dev)
  9779. {
  9780. return TG3_REG_BLK_SIZE;
  9781. }
  9782. static void tg3_get_regs(struct net_device *dev,
  9783. struct ethtool_regs *regs, void *_p)
  9784. {
  9785. struct tg3 *tp = netdev_priv(dev);
  9786. regs->version = 0;
  9787. memset(_p, 0, TG3_REG_BLK_SIZE);
  9788. if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
  9789. return;
  9790. tg3_full_lock(tp, 0);
  9791. tg3_dump_legacy_regs(tp, (u32 *)_p);
  9792. tg3_full_unlock(tp);
  9793. }
  9794. static int tg3_get_eeprom_len(struct net_device *dev)
  9795. {
  9796. struct tg3 *tp = netdev_priv(dev);
  9797. return tp->nvram_size;
  9798. }
  9799. static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
  9800. {
  9801. struct tg3 *tp = netdev_priv(dev);
  9802. int ret, cpmu_restore = 0;
  9803. u8 *pd;
  9804. u32 i, offset, len, b_offset, b_count, cpmu_val = 0;
  9805. __be32 val;
  9806. if (tg3_flag(tp, NO_NVRAM))
  9807. return -EINVAL;
  9808. offset = eeprom->offset;
  9809. len = eeprom->len;
  9810. eeprom->len = 0;
  9811. eeprom->magic = TG3_EEPROM_MAGIC;
  9812. /* Override clock, link aware and link idle modes */
  9813. if (tg3_flag(tp, CPMU_PRESENT)) {
  9814. cpmu_val = tr32(TG3_CPMU_CTRL);
  9815. if (cpmu_val & (CPMU_CTRL_LINK_AWARE_MODE |
  9816. CPMU_CTRL_LINK_IDLE_MODE)) {
  9817. tw32(TG3_CPMU_CTRL, cpmu_val &
  9818. ~(CPMU_CTRL_LINK_AWARE_MODE |
  9819. CPMU_CTRL_LINK_IDLE_MODE));
  9820. cpmu_restore = 1;
  9821. }
  9822. }
  9823. tg3_override_clk(tp);
  9824. if (offset & 3) {
  9825. /* adjustments to start on required 4 byte boundary */
  9826. b_offset = offset & 3;
  9827. b_count = 4 - b_offset;
  9828. if (b_count > len) {
  9829. /* i.e. offset=1 len=2 */
  9830. b_count = len;
  9831. }
  9832. ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
  9833. if (ret)
  9834. goto eeprom_done;
  9835. memcpy(data, ((char *)&val) + b_offset, b_count);
  9836. len -= b_count;
  9837. offset += b_count;
  9838. eeprom->len += b_count;
  9839. }
  9840. /* read bytes up to the last 4 byte boundary */
  9841. pd = &data[eeprom->len];
  9842. for (i = 0; i < (len - (len & 3)); i += 4) {
  9843. ret = tg3_nvram_read_be32(tp, offset + i, &val);
  9844. if (ret) {
  9845. if (i)
  9846. i -= 4;
  9847. eeprom->len += i;
  9848. goto eeprom_done;
  9849. }
  9850. memcpy(pd + i, &val, 4);
  9851. if (need_resched()) {
  9852. if (signal_pending(current)) {
  9853. eeprom->len += i;
  9854. ret = -EINTR;
  9855. goto eeprom_done;
  9856. }
  9857. cond_resched();
  9858. }
  9859. }
  9860. eeprom->len += i;
  9861. if (len & 3) {
  9862. /* read last bytes not ending on 4 byte boundary */
  9863. pd = &data[eeprom->len];
  9864. b_count = len & 3;
  9865. b_offset = offset + len - b_count;
  9866. ret = tg3_nvram_read_be32(tp, b_offset, &val);
  9867. if (ret)
  9868. goto eeprom_done;
  9869. memcpy(pd, &val, b_count);
  9870. eeprom->len += b_count;
  9871. }
  9872. ret = 0;
  9873. eeprom_done:
  9874. /* Restore clock, link aware and link idle modes */
  9875. tg3_restore_clk(tp);
  9876. if (cpmu_restore)
  9877. tw32(TG3_CPMU_CTRL, cpmu_val);
  9878. return ret;
  9879. }
  9880. static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
  9881. {
  9882. struct tg3 *tp = netdev_priv(dev);
  9883. int ret;
  9884. u32 offset, len, b_offset, odd_len;
  9885. u8 *buf;
  9886. __be32 start = 0, end;
  9887. if (tg3_flag(tp, NO_NVRAM) ||
  9888. eeprom->magic != TG3_EEPROM_MAGIC)
  9889. return -EINVAL;
  9890. offset = eeprom->offset;
  9891. len = eeprom->len;
  9892. if ((b_offset = (offset & 3))) {
  9893. /* adjustments to start on required 4 byte boundary */
  9894. ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
  9895. if (ret)
  9896. return ret;
  9897. len += b_offset;
  9898. offset &= ~3;
  9899. if (len < 4)
  9900. len = 4;
  9901. }
  9902. odd_len = 0;
  9903. if (len & 3) {
  9904. /* adjustments to end on required 4 byte boundary */
  9905. odd_len = 1;
  9906. len = (len + 3) & ~3;
  9907. ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
  9908. if (ret)
  9909. return ret;
  9910. }
  9911. buf = data;
  9912. if (b_offset || odd_len) {
  9913. buf = kmalloc(len, GFP_KERNEL);
  9914. if (!buf)
  9915. return -ENOMEM;
  9916. if (b_offset)
  9917. memcpy(buf, &start, 4);
  9918. if (odd_len)
  9919. memcpy(buf+len-4, &end, 4);
  9920. memcpy(buf + b_offset, data, eeprom->len);
  9921. }
  9922. ret = tg3_nvram_write_block(tp, offset, len, buf);
  9923. if (buf != data)
  9924. kfree(buf);
  9925. return ret;
  9926. }
  9927. static int tg3_get_link_ksettings(struct net_device *dev,
  9928. struct ethtool_link_ksettings *cmd)
  9929. {
  9930. struct tg3 *tp = netdev_priv(dev);
  9931. u32 supported, advertising;
  9932. if (tg3_flag(tp, USE_PHYLIB)) {
  9933. struct phy_device *phydev;
  9934. if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
  9935. return -EAGAIN;
  9936. phydev = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr);
  9937. return phy_ethtool_ksettings_get(phydev, cmd);
  9938. }
  9939. supported = (SUPPORTED_Autoneg);
  9940. if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
  9941. supported |= (SUPPORTED_1000baseT_Half |
  9942. SUPPORTED_1000baseT_Full);
  9943. if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
  9944. supported |= (SUPPORTED_100baseT_Half |
  9945. SUPPORTED_100baseT_Full |
  9946. SUPPORTED_10baseT_Half |
  9947. SUPPORTED_10baseT_Full |
  9948. SUPPORTED_TP);
  9949. cmd->base.port = PORT_TP;
  9950. } else {
  9951. supported |= SUPPORTED_FIBRE;
  9952. cmd->base.port = PORT_FIBRE;
  9953. }
  9954. ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
  9955. supported);
  9956. advertising = tp->link_config.advertising;
  9957. if (tg3_flag(tp, PAUSE_AUTONEG)) {
  9958. if (tp->link_config.flowctrl & FLOW_CTRL_RX) {
  9959. if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
  9960. advertising |= ADVERTISED_Pause;
  9961. } else {
  9962. advertising |= ADVERTISED_Pause |
  9963. ADVERTISED_Asym_Pause;
  9964. }
  9965. } else if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
  9966. advertising |= ADVERTISED_Asym_Pause;
  9967. }
  9968. }
  9969. ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
  9970. advertising);
  9971. if (netif_running(dev) && tp->link_up) {
  9972. cmd->base.speed = tp->link_config.active_speed;
  9973. cmd->base.duplex = tp->link_config.active_duplex;
  9974. ethtool_convert_legacy_u32_to_link_mode(
  9975. cmd->link_modes.lp_advertising,
  9976. tp->link_config.rmt_adv);
  9977. if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
  9978. if (tp->phy_flags & TG3_PHYFLG_MDIX_STATE)
  9979. cmd->base.eth_tp_mdix = ETH_TP_MDI_X;
  9980. else
  9981. cmd->base.eth_tp_mdix = ETH_TP_MDI;
  9982. }
  9983. } else {
  9984. cmd->base.speed = SPEED_UNKNOWN;
  9985. cmd->base.duplex = DUPLEX_UNKNOWN;
  9986. cmd->base.eth_tp_mdix = ETH_TP_MDI_INVALID;
  9987. }
  9988. cmd->base.phy_address = tp->phy_addr;
  9989. cmd->base.autoneg = tp->link_config.autoneg;
  9990. return 0;
  9991. }
  9992. static int tg3_set_link_ksettings(struct net_device *dev,
  9993. const struct ethtool_link_ksettings *cmd)
  9994. {
  9995. struct tg3 *tp = netdev_priv(dev);
  9996. u32 speed = cmd->base.speed;
  9997. u32 advertising;
  9998. if (tg3_flag(tp, USE_PHYLIB)) {
  9999. struct phy_device *phydev;
  10000. if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
  10001. return -EAGAIN;
  10002. phydev = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr);
  10003. return phy_ethtool_ksettings_set(phydev, cmd);
  10004. }
  10005. if (cmd->base.autoneg != AUTONEG_ENABLE &&
  10006. cmd->base.autoneg != AUTONEG_DISABLE)
  10007. return -EINVAL;
  10008. if (cmd->base.autoneg == AUTONEG_DISABLE &&
  10009. cmd->base.duplex != DUPLEX_FULL &&
  10010. cmd->base.duplex != DUPLEX_HALF)
  10011. return -EINVAL;
  10012. ethtool_convert_link_mode_to_legacy_u32(&advertising,
  10013. cmd->link_modes.advertising);
  10014. if (cmd->base.autoneg == AUTONEG_ENABLE) {
  10015. u32 mask = ADVERTISED_Autoneg |
  10016. ADVERTISED_Pause |
  10017. ADVERTISED_Asym_Pause;
  10018. if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
  10019. mask |= ADVERTISED_1000baseT_Half |
  10020. ADVERTISED_1000baseT_Full;
  10021. if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
  10022. mask |= ADVERTISED_100baseT_Half |
  10023. ADVERTISED_100baseT_Full |
  10024. ADVERTISED_10baseT_Half |
  10025. ADVERTISED_10baseT_Full |
  10026. ADVERTISED_TP;
  10027. else
  10028. mask |= ADVERTISED_FIBRE;
  10029. if (advertising & ~mask)
  10030. return -EINVAL;
  10031. mask &= (ADVERTISED_1000baseT_Half |
  10032. ADVERTISED_1000baseT_Full |
  10033. ADVERTISED_100baseT_Half |
  10034. ADVERTISED_100baseT_Full |
  10035. ADVERTISED_10baseT_Half |
  10036. ADVERTISED_10baseT_Full);
  10037. advertising &= mask;
  10038. } else {
  10039. if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
  10040. if (speed != SPEED_1000)
  10041. return -EINVAL;
  10042. if (cmd->base.duplex != DUPLEX_FULL)
  10043. return -EINVAL;
  10044. } else {
  10045. if (speed != SPEED_100 &&
  10046. speed != SPEED_10)
  10047. return -EINVAL;
  10048. }
  10049. }
  10050. tg3_full_lock(tp, 0);
  10051. tp->link_config.autoneg = cmd->base.autoneg;
  10052. if (cmd->base.autoneg == AUTONEG_ENABLE) {
  10053. tp->link_config.advertising = (advertising |
  10054. ADVERTISED_Autoneg);
  10055. tp->link_config.speed = SPEED_UNKNOWN;
  10056. tp->link_config.duplex = DUPLEX_UNKNOWN;
  10057. } else {
  10058. tp->link_config.advertising = 0;
  10059. tp->link_config.speed = speed;
  10060. tp->link_config.duplex = cmd->base.duplex;
  10061. }
  10062. tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
  10063. tg3_warn_mgmt_link_flap(tp);
  10064. if (netif_running(dev))
  10065. tg3_setup_phy(tp, true);
  10066. tg3_full_unlock(tp);
  10067. return 0;
  10068. }
  10069. static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  10070. {
  10071. struct tg3 *tp = netdev_priv(dev);
  10072. strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
  10073. strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
  10074. strlcpy(info->fw_version, tp->fw_ver, sizeof(info->fw_version));
  10075. strlcpy(info->bus_info, pci_name(tp->pdev), sizeof(info->bus_info));
  10076. }
  10077. static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  10078. {
  10079. struct tg3 *tp = netdev_priv(dev);
  10080. if (tg3_flag(tp, WOL_CAP) && device_can_wakeup(&tp->pdev->dev))
  10081. wol->supported = WAKE_MAGIC;
  10082. else
  10083. wol->supported = 0;
  10084. wol->wolopts = 0;
  10085. if (tg3_flag(tp, WOL_ENABLE) && device_can_wakeup(&tp->pdev->dev))
  10086. wol->wolopts = WAKE_MAGIC;
  10087. memset(&wol->sopass, 0, sizeof(wol->sopass));
  10088. }
  10089. static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  10090. {
  10091. struct tg3 *tp = netdev_priv(dev);
  10092. struct device *dp = &tp->pdev->dev;
  10093. if (wol->wolopts & ~WAKE_MAGIC)
  10094. return -EINVAL;
  10095. if ((wol->wolopts & WAKE_MAGIC) &&
  10096. !(tg3_flag(tp, WOL_CAP) && device_can_wakeup(dp)))
  10097. return -EINVAL;
  10098. device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
  10099. if (device_may_wakeup(dp))
  10100. tg3_flag_set(tp, WOL_ENABLE);
  10101. else
  10102. tg3_flag_clear(tp, WOL_ENABLE);
  10103. return 0;
  10104. }
  10105. static u32 tg3_get_msglevel(struct net_device *dev)
  10106. {
  10107. struct tg3 *tp = netdev_priv(dev);
  10108. return tp->msg_enable;
  10109. }
  10110. static void tg3_set_msglevel(struct net_device *dev, u32 value)
  10111. {
  10112. struct tg3 *tp = netdev_priv(dev);
  10113. tp->msg_enable = value;
  10114. }
  10115. static int tg3_nway_reset(struct net_device *dev)
  10116. {
  10117. struct tg3 *tp = netdev_priv(dev);
  10118. int r;
  10119. if (!netif_running(dev))
  10120. return -EAGAIN;
  10121. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
  10122. return -EINVAL;
  10123. tg3_warn_mgmt_link_flap(tp);
  10124. if (tg3_flag(tp, USE_PHYLIB)) {
  10125. if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
  10126. return -EAGAIN;
  10127. r = phy_start_aneg(mdiobus_get_phy(tp->mdio_bus, tp->phy_addr));
  10128. } else {
  10129. u32 bmcr;
  10130. spin_lock_bh(&tp->lock);
  10131. r = -EINVAL;
  10132. tg3_readphy(tp, MII_BMCR, &bmcr);
  10133. if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
  10134. ((bmcr & BMCR_ANENABLE) ||
  10135. (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
  10136. tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
  10137. BMCR_ANENABLE);
  10138. r = 0;
  10139. }
  10140. spin_unlock_bh(&tp->lock);
  10141. }
  10142. return r;
  10143. }
  10144. static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
  10145. {
  10146. struct tg3 *tp = netdev_priv(dev);
  10147. ering->rx_max_pending = tp->rx_std_ring_mask;
  10148. if (tg3_flag(tp, JUMBO_RING_ENABLE))
  10149. ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
  10150. else
  10151. ering->rx_jumbo_max_pending = 0;
  10152. ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
  10153. ering->rx_pending = tp->rx_pending;
  10154. if (tg3_flag(tp, JUMBO_RING_ENABLE))
  10155. ering->rx_jumbo_pending = tp->rx_jumbo_pending;
  10156. else
  10157. ering->rx_jumbo_pending = 0;
  10158. ering->tx_pending = tp->napi[0].tx_pending;
  10159. }
  10160. static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
  10161. {
  10162. struct tg3 *tp = netdev_priv(dev);
  10163. int i, irq_sync = 0, err = 0;
  10164. if ((ering->rx_pending > tp->rx_std_ring_mask) ||
  10165. (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
  10166. (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
  10167. (ering->tx_pending <= MAX_SKB_FRAGS) ||
  10168. (tg3_flag(tp, TSO_BUG) &&
  10169. (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
  10170. return -EINVAL;
  10171. if (netif_running(dev)) {
  10172. tg3_phy_stop(tp);
  10173. tg3_netif_stop(tp);
  10174. irq_sync = 1;
  10175. }
  10176. tg3_full_lock(tp, irq_sync);
  10177. tp->rx_pending = ering->rx_pending;
  10178. if (tg3_flag(tp, MAX_RXPEND_64) &&
  10179. tp->rx_pending > 63)
  10180. tp->rx_pending = 63;
  10181. if (tg3_flag(tp, JUMBO_RING_ENABLE))
  10182. tp->rx_jumbo_pending = ering->rx_jumbo_pending;
  10183. for (i = 0; i < tp->irq_max; i++)
  10184. tp->napi[i].tx_pending = ering->tx_pending;
  10185. if (netif_running(dev)) {
  10186. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  10187. err = tg3_restart_hw(tp, false);
  10188. if (!err)
  10189. tg3_netif_start(tp);
  10190. }
  10191. tg3_full_unlock(tp);
  10192. if (irq_sync && !err)
  10193. tg3_phy_start(tp);
  10194. return err;
  10195. }
  10196. static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
  10197. {
  10198. struct tg3 *tp = netdev_priv(dev);
  10199. epause->autoneg = !!tg3_flag(tp, PAUSE_AUTONEG);
  10200. if (tp->link_config.flowctrl & FLOW_CTRL_RX)
  10201. epause->rx_pause = 1;
  10202. else
  10203. epause->rx_pause = 0;
  10204. if (tp->link_config.flowctrl & FLOW_CTRL_TX)
  10205. epause->tx_pause = 1;
  10206. else
  10207. epause->tx_pause = 0;
  10208. }
  10209. static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
  10210. {
  10211. struct tg3 *tp = netdev_priv(dev);
  10212. int err = 0;
  10213. if (tp->link_config.autoneg == AUTONEG_ENABLE)
  10214. tg3_warn_mgmt_link_flap(tp);
  10215. if (tg3_flag(tp, USE_PHYLIB)) {
  10216. u32 newadv;
  10217. struct phy_device *phydev;
  10218. phydev = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr);
  10219. if (!(phydev->supported & SUPPORTED_Pause) ||
  10220. (!(phydev->supported & SUPPORTED_Asym_Pause) &&
  10221. (epause->rx_pause != epause->tx_pause)))
  10222. return -EINVAL;
  10223. tp->link_config.flowctrl = 0;
  10224. if (epause->rx_pause) {
  10225. tp->link_config.flowctrl |= FLOW_CTRL_RX;
  10226. if (epause->tx_pause) {
  10227. tp->link_config.flowctrl |= FLOW_CTRL_TX;
  10228. newadv = ADVERTISED_Pause;
  10229. } else
  10230. newadv = ADVERTISED_Pause |
  10231. ADVERTISED_Asym_Pause;
  10232. } else if (epause->tx_pause) {
  10233. tp->link_config.flowctrl |= FLOW_CTRL_TX;
  10234. newadv = ADVERTISED_Asym_Pause;
  10235. } else
  10236. newadv = 0;
  10237. if (epause->autoneg)
  10238. tg3_flag_set(tp, PAUSE_AUTONEG);
  10239. else
  10240. tg3_flag_clear(tp, PAUSE_AUTONEG);
  10241. if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
  10242. u32 oldadv = phydev->advertising &
  10243. (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
  10244. if (oldadv != newadv) {
  10245. phydev->advertising &=
  10246. ~(ADVERTISED_Pause |
  10247. ADVERTISED_Asym_Pause);
  10248. phydev->advertising |= newadv;
  10249. if (phydev->autoneg) {
  10250. /*
  10251. * Always renegotiate the link to
  10252. * inform our link partner of our
  10253. * flow control settings, even if the
  10254. * flow control is forced. Let
  10255. * tg3_adjust_link() do the final
  10256. * flow control setup.
  10257. */
  10258. return phy_start_aneg(phydev);
  10259. }
  10260. }
  10261. if (!epause->autoneg)
  10262. tg3_setup_flow_control(tp, 0, 0);
  10263. } else {
  10264. tp->link_config.advertising &=
  10265. ~(ADVERTISED_Pause |
  10266. ADVERTISED_Asym_Pause);
  10267. tp->link_config.advertising |= newadv;
  10268. }
  10269. } else {
  10270. int irq_sync = 0;
  10271. if (netif_running(dev)) {
  10272. tg3_netif_stop(tp);
  10273. irq_sync = 1;
  10274. }
  10275. tg3_full_lock(tp, irq_sync);
  10276. if (epause->autoneg)
  10277. tg3_flag_set(tp, PAUSE_AUTONEG);
  10278. else
  10279. tg3_flag_clear(tp, PAUSE_AUTONEG);
  10280. if (epause->rx_pause)
  10281. tp->link_config.flowctrl |= FLOW_CTRL_RX;
  10282. else
  10283. tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
  10284. if (epause->tx_pause)
  10285. tp->link_config.flowctrl |= FLOW_CTRL_TX;
  10286. else
  10287. tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
  10288. if (netif_running(dev)) {
  10289. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  10290. err = tg3_restart_hw(tp, false);
  10291. if (!err)
  10292. tg3_netif_start(tp);
  10293. }
  10294. tg3_full_unlock(tp);
  10295. }
  10296. tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
  10297. return err;
  10298. }
  10299. static int tg3_get_sset_count(struct net_device *dev, int sset)
  10300. {
  10301. switch (sset) {
  10302. case ETH_SS_TEST:
  10303. return TG3_NUM_TEST;
  10304. case ETH_SS_STATS:
  10305. return TG3_NUM_STATS;
  10306. default:
  10307. return -EOPNOTSUPP;
  10308. }
  10309. }
  10310. static int tg3_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
  10311. u32 *rules __always_unused)
  10312. {
  10313. struct tg3 *tp = netdev_priv(dev);
  10314. if (!tg3_flag(tp, SUPPORT_MSIX))
  10315. return -EOPNOTSUPP;
  10316. switch (info->cmd) {
  10317. case ETHTOOL_GRXRINGS:
  10318. if (netif_running(tp->dev))
  10319. info->data = tp->rxq_cnt;
  10320. else {
  10321. info->data = num_online_cpus();
  10322. if (info->data > TG3_RSS_MAX_NUM_QS)
  10323. info->data = TG3_RSS_MAX_NUM_QS;
  10324. }
  10325. return 0;
  10326. default:
  10327. return -EOPNOTSUPP;
  10328. }
  10329. }
  10330. static u32 tg3_get_rxfh_indir_size(struct net_device *dev)
  10331. {
  10332. u32 size = 0;
  10333. struct tg3 *tp = netdev_priv(dev);
  10334. if (tg3_flag(tp, SUPPORT_MSIX))
  10335. size = TG3_RSS_INDIR_TBL_SIZE;
  10336. return size;
  10337. }
  10338. static int tg3_get_rxfh(struct net_device *dev, u32 *indir, u8 *key, u8 *hfunc)
  10339. {
  10340. struct tg3 *tp = netdev_priv(dev);
  10341. int i;
  10342. if (hfunc)
  10343. *hfunc = ETH_RSS_HASH_TOP;
  10344. if (!indir)
  10345. return 0;
  10346. for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
  10347. indir[i] = tp->rss_ind_tbl[i];
  10348. return 0;
  10349. }
  10350. static int tg3_set_rxfh(struct net_device *dev, const u32 *indir, const u8 *key,
  10351. const u8 hfunc)
  10352. {
  10353. struct tg3 *tp = netdev_priv(dev);
  10354. size_t i;
  10355. /* We require at least one supported parameter to be changed and no
  10356. * change in any of the unsupported parameters
  10357. */
  10358. if (key ||
  10359. (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP))
  10360. return -EOPNOTSUPP;
  10361. if (!indir)
  10362. return 0;
  10363. for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
  10364. tp->rss_ind_tbl[i] = indir[i];
  10365. if (!netif_running(dev) || !tg3_flag(tp, ENABLE_RSS))
  10366. return 0;
  10367. /* It is legal to write the indirection
  10368. * table while the device is running.
  10369. */
  10370. tg3_full_lock(tp, 0);
  10371. tg3_rss_write_indir_tbl(tp);
  10372. tg3_full_unlock(tp);
  10373. return 0;
  10374. }
  10375. static void tg3_get_channels(struct net_device *dev,
  10376. struct ethtool_channels *channel)
  10377. {
  10378. struct tg3 *tp = netdev_priv(dev);
  10379. u32 deflt_qs = netif_get_num_default_rss_queues();
  10380. channel->max_rx = tp->rxq_max;
  10381. channel->max_tx = tp->txq_max;
  10382. if (netif_running(dev)) {
  10383. channel->rx_count = tp->rxq_cnt;
  10384. channel->tx_count = tp->txq_cnt;
  10385. } else {
  10386. if (tp->rxq_req)
  10387. channel->rx_count = tp->rxq_req;
  10388. else
  10389. channel->rx_count = min(deflt_qs, tp->rxq_max);
  10390. if (tp->txq_req)
  10391. channel->tx_count = tp->txq_req;
  10392. else
  10393. channel->tx_count = min(deflt_qs, tp->txq_max);
  10394. }
  10395. }
  10396. static int tg3_set_channels(struct net_device *dev,
  10397. struct ethtool_channels *channel)
  10398. {
  10399. struct tg3 *tp = netdev_priv(dev);
  10400. if (!tg3_flag(tp, SUPPORT_MSIX))
  10401. return -EOPNOTSUPP;
  10402. if (channel->rx_count > tp->rxq_max ||
  10403. channel->tx_count > tp->txq_max)
  10404. return -EINVAL;
  10405. tp->rxq_req = channel->rx_count;
  10406. tp->txq_req = channel->tx_count;
  10407. if (!netif_running(dev))
  10408. return 0;
  10409. tg3_stop(tp);
  10410. tg3_carrier_off(tp);
  10411. tg3_start(tp, true, false, false);
  10412. return 0;
  10413. }
  10414. static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
  10415. {
  10416. switch (stringset) {
  10417. case ETH_SS_STATS:
  10418. memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
  10419. break;
  10420. case ETH_SS_TEST:
  10421. memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
  10422. break;
  10423. default:
  10424. WARN_ON(1); /* we need a WARN() */
  10425. break;
  10426. }
  10427. }
  10428. static int tg3_set_phys_id(struct net_device *dev,
  10429. enum ethtool_phys_id_state state)
  10430. {
  10431. struct tg3 *tp = netdev_priv(dev);
  10432. if (!netif_running(tp->dev))
  10433. return -EAGAIN;
  10434. switch (state) {
  10435. case ETHTOOL_ID_ACTIVE:
  10436. return 1; /* cycle on/off once per second */
  10437. case ETHTOOL_ID_ON:
  10438. tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
  10439. LED_CTRL_1000MBPS_ON |
  10440. LED_CTRL_100MBPS_ON |
  10441. LED_CTRL_10MBPS_ON |
  10442. LED_CTRL_TRAFFIC_OVERRIDE |
  10443. LED_CTRL_TRAFFIC_BLINK |
  10444. LED_CTRL_TRAFFIC_LED);
  10445. break;
  10446. case ETHTOOL_ID_OFF:
  10447. tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
  10448. LED_CTRL_TRAFFIC_OVERRIDE);
  10449. break;
  10450. case ETHTOOL_ID_INACTIVE:
  10451. tw32(MAC_LED_CTRL, tp->led_ctrl);
  10452. break;
  10453. }
  10454. return 0;
  10455. }
  10456. static void tg3_get_ethtool_stats(struct net_device *dev,
  10457. struct ethtool_stats *estats, u64 *tmp_stats)
  10458. {
  10459. struct tg3 *tp = netdev_priv(dev);
  10460. if (tp->hw_stats)
  10461. tg3_get_estats(tp, (struct tg3_ethtool_stats *)tmp_stats);
  10462. else
  10463. memset(tmp_stats, 0, sizeof(struct tg3_ethtool_stats));
  10464. }
  10465. static __be32 *tg3_vpd_readblock(struct tg3 *tp, u32 *vpdlen)
  10466. {
  10467. int i;
  10468. __be32 *buf;
  10469. u32 offset = 0, len = 0;
  10470. u32 magic, val;
  10471. if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &magic))
  10472. return NULL;
  10473. if (magic == TG3_EEPROM_MAGIC) {
  10474. for (offset = TG3_NVM_DIR_START;
  10475. offset < TG3_NVM_DIR_END;
  10476. offset += TG3_NVM_DIRENT_SIZE) {
  10477. if (tg3_nvram_read(tp, offset, &val))
  10478. return NULL;
  10479. if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
  10480. TG3_NVM_DIRTYPE_EXTVPD)
  10481. break;
  10482. }
  10483. if (offset != TG3_NVM_DIR_END) {
  10484. len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
  10485. if (tg3_nvram_read(tp, offset + 4, &offset))
  10486. return NULL;
  10487. offset = tg3_nvram_logical_addr(tp, offset);
  10488. }
  10489. }
  10490. if (!offset || !len) {
  10491. offset = TG3_NVM_VPD_OFF;
  10492. len = TG3_NVM_VPD_LEN;
  10493. }
  10494. buf = kmalloc(len, GFP_KERNEL);
  10495. if (buf == NULL)
  10496. return NULL;
  10497. if (magic == TG3_EEPROM_MAGIC) {
  10498. for (i = 0; i < len; i += 4) {
  10499. /* The data is in little-endian format in NVRAM.
  10500. * Use the big-endian read routines to preserve
  10501. * the byte order as it exists in NVRAM.
  10502. */
  10503. if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
  10504. goto error;
  10505. }
  10506. } else {
  10507. u8 *ptr;
  10508. ssize_t cnt;
  10509. unsigned int pos = 0;
  10510. ptr = (u8 *)&buf[0];
  10511. for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
  10512. cnt = pci_read_vpd(tp->pdev, pos,
  10513. len - pos, ptr);
  10514. if (cnt == -ETIMEDOUT || cnt == -EINTR)
  10515. cnt = 0;
  10516. else if (cnt < 0)
  10517. goto error;
  10518. }
  10519. if (pos != len)
  10520. goto error;
  10521. }
  10522. *vpdlen = len;
  10523. return buf;
  10524. error:
  10525. kfree(buf);
  10526. return NULL;
  10527. }
  10528. #define NVRAM_TEST_SIZE 0x100
  10529. #define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
  10530. #define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
  10531. #define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
  10532. #define NVRAM_SELFBOOT_FORMAT1_4_SIZE 0x20
  10533. #define NVRAM_SELFBOOT_FORMAT1_5_SIZE 0x24
  10534. #define NVRAM_SELFBOOT_FORMAT1_6_SIZE 0x50
  10535. #define NVRAM_SELFBOOT_HW_SIZE 0x20
  10536. #define NVRAM_SELFBOOT_DATA_SIZE 0x1c
  10537. static int tg3_test_nvram(struct tg3 *tp)
  10538. {
  10539. u32 csum, magic, len;
  10540. __be32 *buf;
  10541. int i, j, k, err = 0, size;
  10542. if (tg3_flag(tp, NO_NVRAM))
  10543. return 0;
  10544. if (tg3_nvram_read(tp, 0, &magic) != 0)
  10545. return -EIO;
  10546. if (magic == TG3_EEPROM_MAGIC)
  10547. size = NVRAM_TEST_SIZE;
  10548. else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
  10549. if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
  10550. TG3_EEPROM_SB_FORMAT_1) {
  10551. switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
  10552. case TG3_EEPROM_SB_REVISION_0:
  10553. size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
  10554. break;
  10555. case TG3_EEPROM_SB_REVISION_2:
  10556. size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
  10557. break;
  10558. case TG3_EEPROM_SB_REVISION_3:
  10559. size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
  10560. break;
  10561. case TG3_EEPROM_SB_REVISION_4:
  10562. size = NVRAM_SELFBOOT_FORMAT1_4_SIZE;
  10563. break;
  10564. case TG3_EEPROM_SB_REVISION_5:
  10565. size = NVRAM_SELFBOOT_FORMAT1_5_SIZE;
  10566. break;
  10567. case TG3_EEPROM_SB_REVISION_6:
  10568. size = NVRAM_SELFBOOT_FORMAT1_6_SIZE;
  10569. break;
  10570. default:
  10571. return -EIO;
  10572. }
  10573. } else
  10574. return 0;
  10575. } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
  10576. size = NVRAM_SELFBOOT_HW_SIZE;
  10577. else
  10578. return -EIO;
  10579. buf = kmalloc(size, GFP_KERNEL);
  10580. if (buf == NULL)
  10581. return -ENOMEM;
  10582. err = -EIO;
  10583. for (i = 0, j = 0; i < size; i += 4, j++) {
  10584. err = tg3_nvram_read_be32(tp, i, &buf[j]);
  10585. if (err)
  10586. break;
  10587. }
  10588. if (i < size)
  10589. goto out;
  10590. /* Selfboot format */
  10591. magic = be32_to_cpu(buf[0]);
  10592. if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
  10593. TG3_EEPROM_MAGIC_FW) {
  10594. u8 *buf8 = (u8 *) buf, csum8 = 0;
  10595. if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
  10596. TG3_EEPROM_SB_REVISION_2) {
  10597. /* For rev 2, the csum doesn't include the MBA. */
  10598. for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
  10599. csum8 += buf8[i];
  10600. for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
  10601. csum8 += buf8[i];
  10602. } else {
  10603. for (i = 0; i < size; i++)
  10604. csum8 += buf8[i];
  10605. }
  10606. if (csum8 == 0) {
  10607. err = 0;
  10608. goto out;
  10609. }
  10610. err = -EIO;
  10611. goto out;
  10612. }
  10613. if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
  10614. TG3_EEPROM_MAGIC_HW) {
  10615. u8 data[NVRAM_SELFBOOT_DATA_SIZE];
  10616. u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
  10617. u8 *buf8 = (u8 *) buf;
  10618. /* Separate the parity bits and the data bytes. */
  10619. for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
  10620. if ((i == 0) || (i == 8)) {
  10621. int l;
  10622. u8 msk;
  10623. for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
  10624. parity[k++] = buf8[i] & msk;
  10625. i++;
  10626. } else if (i == 16) {
  10627. int l;
  10628. u8 msk;
  10629. for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
  10630. parity[k++] = buf8[i] & msk;
  10631. i++;
  10632. for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
  10633. parity[k++] = buf8[i] & msk;
  10634. i++;
  10635. }
  10636. data[j++] = buf8[i];
  10637. }
  10638. err = -EIO;
  10639. for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
  10640. u8 hw8 = hweight8(data[i]);
  10641. if ((hw8 & 0x1) && parity[i])
  10642. goto out;
  10643. else if (!(hw8 & 0x1) && !parity[i])
  10644. goto out;
  10645. }
  10646. err = 0;
  10647. goto out;
  10648. }
  10649. err = -EIO;
  10650. /* Bootstrap checksum at offset 0x10 */
  10651. csum = calc_crc((unsigned char *) buf, 0x10);
  10652. if (csum != le32_to_cpu(buf[0x10/4]))
  10653. goto out;
  10654. /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
  10655. csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
  10656. if (csum != le32_to_cpu(buf[0xfc/4]))
  10657. goto out;
  10658. kfree(buf);
  10659. buf = tg3_vpd_readblock(tp, &len);
  10660. if (!buf)
  10661. return -ENOMEM;
  10662. i = pci_vpd_find_tag((u8 *)buf, 0, len, PCI_VPD_LRDT_RO_DATA);
  10663. if (i > 0) {
  10664. j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
  10665. if (j < 0)
  10666. goto out;
  10667. if (i + PCI_VPD_LRDT_TAG_SIZE + j > len)
  10668. goto out;
  10669. i += PCI_VPD_LRDT_TAG_SIZE;
  10670. j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
  10671. PCI_VPD_RO_KEYWORD_CHKSUM);
  10672. if (j > 0) {
  10673. u8 csum8 = 0;
  10674. j += PCI_VPD_INFO_FLD_HDR_SIZE;
  10675. for (i = 0; i <= j; i++)
  10676. csum8 += ((u8 *)buf)[i];
  10677. if (csum8)
  10678. goto out;
  10679. }
  10680. }
  10681. err = 0;
  10682. out:
  10683. kfree(buf);
  10684. return err;
  10685. }
  10686. #define TG3_SERDES_TIMEOUT_SEC 2
  10687. #define TG3_COPPER_TIMEOUT_SEC 6
  10688. static int tg3_test_link(struct tg3 *tp)
  10689. {
  10690. int i, max;
  10691. if (!netif_running(tp->dev))
  10692. return -ENODEV;
  10693. if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
  10694. max = TG3_SERDES_TIMEOUT_SEC;
  10695. else
  10696. max = TG3_COPPER_TIMEOUT_SEC;
  10697. for (i = 0; i < max; i++) {
  10698. if (tp->link_up)
  10699. return 0;
  10700. if (msleep_interruptible(1000))
  10701. break;
  10702. }
  10703. return -EIO;
  10704. }
  10705. /* Only test the commonly used registers */
  10706. static int tg3_test_registers(struct tg3 *tp)
  10707. {
  10708. int i, is_5705, is_5750;
  10709. u32 offset, read_mask, write_mask, val, save_val, read_val;
  10710. static struct {
  10711. u16 offset;
  10712. u16 flags;
  10713. #define TG3_FL_5705 0x1
  10714. #define TG3_FL_NOT_5705 0x2
  10715. #define TG3_FL_NOT_5788 0x4
  10716. #define TG3_FL_NOT_5750 0x8
  10717. u32 read_mask;
  10718. u32 write_mask;
  10719. } reg_tbl[] = {
  10720. /* MAC Control Registers */
  10721. { MAC_MODE, TG3_FL_NOT_5705,
  10722. 0x00000000, 0x00ef6f8c },
  10723. { MAC_MODE, TG3_FL_5705,
  10724. 0x00000000, 0x01ef6b8c },
  10725. { MAC_STATUS, TG3_FL_NOT_5705,
  10726. 0x03800107, 0x00000000 },
  10727. { MAC_STATUS, TG3_FL_5705,
  10728. 0x03800100, 0x00000000 },
  10729. { MAC_ADDR_0_HIGH, 0x0000,
  10730. 0x00000000, 0x0000ffff },
  10731. { MAC_ADDR_0_LOW, 0x0000,
  10732. 0x00000000, 0xffffffff },
  10733. { MAC_RX_MTU_SIZE, 0x0000,
  10734. 0x00000000, 0x0000ffff },
  10735. { MAC_TX_MODE, 0x0000,
  10736. 0x00000000, 0x00000070 },
  10737. { MAC_TX_LENGTHS, 0x0000,
  10738. 0x00000000, 0x00003fff },
  10739. { MAC_RX_MODE, TG3_FL_NOT_5705,
  10740. 0x00000000, 0x000007fc },
  10741. { MAC_RX_MODE, TG3_FL_5705,
  10742. 0x00000000, 0x000007dc },
  10743. { MAC_HASH_REG_0, 0x0000,
  10744. 0x00000000, 0xffffffff },
  10745. { MAC_HASH_REG_1, 0x0000,
  10746. 0x00000000, 0xffffffff },
  10747. { MAC_HASH_REG_2, 0x0000,
  10748. 0x00000000, 0xffffffff },
  10749. { MAC_HASH_REG_3, 0x0000,
  10750. 0x00000000, 0xffffffff },
  10751. /* Receive Data and Receive BD Initiator Control Registers. */
  10752. { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
  10753. 0x00000000, 0xffffffff },
  10754. { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
  10755. 0x00000000, 0xffffffff },
  10756. { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
  10757. 0x00000000, 0x00000003 },
  10758. { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
  10759. 0x00000000, 0xffffffff },
  10760. { RCVDBDI_STD_BD+0, 0x0000,
  10761. 0x00000000, 0xffffffff },
  10762. { RCVDBDI_STD_BD+4, 0x0000,
  10763. 0x00000000, 0xffffffff },
  10764. { RCVDBDI_STD_BD+8, 0x0000,
  10765. 0x00000000, 0xffff0002 },
  10766. { RCVDBDI_STD_BD+0xc, 0x0000,
  10767. 0x00000000, 0xffffffff },
  10768. /* Receive BD Initiator Control Registers. */
  10769. { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
  10770. 0x00000000, 0xffffffff },
  10771. { RCVBDI_STD_THRESH, TG3_FL_5705,
  10772. 0x00000000, 0x000003ff },
  10773. { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
  10774. 0x00000000, 0xffffffff },
  10775. /* Host Coalescing Control Registers. */
  10776. { HOSTCC_MODE, TG3_FL_NOT_5705,
  10777. 0x00000000, 0x00000004 },
  10778. { HOSTCC_MODE, TG3_FL_5705,
  10779. 0x00000000, 0x000000f6 },
  10780. { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
  10781. 0x00000000, 0xffffffff },
  10782. { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
  10783. 0x00000000, 0x000003ff },
  10784. { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
  10785. 0x00000000, 0xffffffff },
  10786. { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
  10787. 0x00000000, 0x000003ff },
  10788. { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
  10789. 0x00000000, 0xffffffff },
  10790. { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
  10791. 0x00000000, 0x000000ff },
  10792. { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
  10793. 0x00000000, 0xffffffff },
  10794. { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
  10795. 0x00000000, 0x000000ff },
  10796. { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
  10797. 0x00000000, 0xffffffff },
  10798. { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
  10799. 0x00000000, 0xffffffff },
  10800. { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
  10801. 0x00000000, 0xffffffff },
  10802. { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
  10803. 0x00000000, 0x000000ff },
  10804. { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
  10805. 0x00000000, 0xffffffff },
  10806. { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
  10807. 0x00000000, 0x000000ff },
  10808. { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
  10809. 0x00000000, 0xffffffff },
  10810. { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
  10811. 0x00000000, 0xffffffff },
  10812. { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
  10813. 0x00000000, 0xffffffff },
  10814. { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
  10815. 0x00000000, 0xffffffff },
  10816. { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
  10817. 0x00000000, 0xffffffff },
  10818. { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
  10819. 0xffffffff, 0x00000000 },
  10820. { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
  10821. 0xffffffff, 0x00000000 },
  10822. /* Buffer Manager Control Registers. */
  10823. { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
  10824. 0x00000000, 0x007fff80 },
  10825. { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
  10826. 0x00000000, 0x007fffff },
  10827. { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
  10828. 0x00000000, 0x0000003f },
  10829. { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
  10830. 0x00000000, 0x000001ff },
  10831. { BUFMGR_MB_HIGH_WATER, 0x0000,
  10832. 0x00000000, 0x000001ff },
  10833. { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
  10834. 0xffffffff, 0x00000000 },
  10835. { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
  10836. 0xffffffff, 0x00000000 },
  10837. /* Mailbox Registers */
  10838. { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
  10839. 0x00000000, 0x000001ff },
  10840. { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
  10841. 0x00000000, 0x000001ff },
  10842. { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
  10843. 0x00000000, 0x000007ff },
  10844. { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
  10845. 0x00000000, 0x000001ff },
  10846. { 0xffff, 0x0000, 0x00000000, 0x00000000 },
  10847. };
  10848. is_5705 = is_5750 = 0;
  10849. if (tg3_flag(tp, 5705_PLUS)) {
  10850. is_5705 = 1;
  10851. if (tg3_flag(tp, 5750_PLUS))
  10852. is_5750 = 1;
  10853. }
  10854. for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
  10855. if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
  10856. continue;
  10857. if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
  10858. continue;
  10859. if (tg3_flag(tp, IS_5788) &&
  10860. (reg_tbl[i].flags & TG3_FL_NOT_5788))
  10861. continue;
  10862. if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
  10863. continue;
  10864. offset = (u32) reg_tbl[i].offset;
  10865. read_mask = reg_tbl[i].read_mask;
  10866. write_mask = reg_tbl[i].write_mask;
  10867. /* Save the original register content */
  10868. save_val = tr32(offset);
  10869. /* Determine the read-only value. */
  10870. read_val = save_val & read_mask;
  10871. /* Write zero to the register, then make sure the read-only bits
  10872. * are not changed and the read/write bits are all zeros.
  10873. */
  10874. tw32(offset, 0);
  10875. val = tr32(offset);
  10876. /* Test the read-only and read/write bits. */
  10877. if (((val & read_mask) != read_val) || (val & write_mask))
  10878. goto out;
  10879. /* Write ones to all the bits defined by RdMask and WrMask, then
  10880. * make sure the read-only bits are not changed and the
  10881. * read/write bits are all ones.
  10882. */
  10883. tw32(offset, read_mask | write_mask);
  10884. val = tr32(offset);
  10885. /* Test the read-only bits. */
  10886. if ((val & read_mask) != read_val)
  10887. goto out;
  10888. /* Test the read/write bits. */
  10889. if ((val & write_mask) != write_mask)
  10890. goto out;
  10891. tw32(offset, save_val);
  10892. }
  10893. return 0;
  10894. out:
  10895. if (netif_msg_hw(tp))
  10896. netdev_err(tp->dev,
  10897. "Register test failed at offset %x\n", offset);
  10898. tw32(offset, save_val);
  10899. return -EIO;
  10900. }
  10901. static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
  10902. {
  10903. static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
  10904. int i;
  10905. u32 j;
  10906. for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
  10907. for (j = 0; j < len; j += 4) {
  10908. u32 val;
  10909. tg3_write_mem(tp, offset + j, test_pattern[i]);
  10910. tg3_read_mem(tp, offset + j, &val);
  10911. if (val != test_pattern[i])
  10912. return -EIO;
  10913. }
  10914. }
  10915. return 0;
  10916. }
  10917. static int tg3_test_memory(struct tg3 *tp)
  10918. {
  10919. static struct mem_entry {
  10920. u32 offset;
  10921. u32 len;
  10922. } mem_tbl_570x[] = {
  10923. { 0x00000000, 0x00b50},
  10924. { 0x00002000, 0x1c000},
  10925. { 0xffffffff, 0x00000}
  10926. }, mem_tbl_5705[] = {
  10927. { 0x00000100, 0x0000c},
  10928. { 0x00000200, 0x00008},
  10929. { 0x00004000, 0x00800},
  10930. { 0x00006000, 0x01000},
  10931. { 0x00008000, 0x02000},
  10932. { 0x00010000, 0x0e000},
  10933. { 0xffffffff, 0x00000}
  10934. }, mem_tbl_5755[] = {
  10935. { 0x00000200, 0x00008},
  10936. { 0x00004000, 0x00800},
  10937. { 0x00006000, 0x00800},
  10938. { 0x00008000, 0x02000},
  10939. { 0x00010000, 0x0c000},
  10940. { 0xffffffff, 0x00000}
  10941. }, mem_tbl_5906[] = {
  10942. { 0x00000200, 0x00008},
  10943. { 0x00004000, 0x00400},
  10944. { 0x00006000, 0x00400},
  10945. { 0x00008000, 0x01000},
  10946. { 0x00010000, 0x01000},
  10947. { 0xffffffff, 0x00000}
  10948. }, mem_tbl_5717[] = {
  10949. { 0x00000200, 0x00008},
  10950. { 0x00010000, 0x0a000},
  10951. { 0x00020000, 0x13c00},
  10952. { 0xffffffff, 0x00000}
  10953. }, mem_tbl_57765[] = {
  10954. { 0x00000200, 0x00008},
  10955. { 0x00004000, 0x00800},
  10956. { 0x00006000, 0x09800},
  10957. { 0x00010000, 0x0a000},
  10958. { 0xffffffff, 0x00000}
  10959. };
  10960. struct mem_entry *mem_tbl;
  10961. int err = 0;
  10962. int i;
  10963. if (tg3_flag(tp, 5717_PLUS))
  10964. mem_tbl = mem_tbl_5717;
  10965. else if (tg3_flag(tp, 57765_CLASS) ||
  10966. tg3_asic_rev(tp) == ASIC_REV_5762)
  10967. mem_tbl = mem_tbl_57765;
  10968. else if (tg3_flag(tp, 5755_PLUS))
  10969. mem_tbl = mem_tbl_5755;
  10970. else if (tg3_asic_rev(tp) == ASIC_REV_5906)
  10971. mem_tbl = mem_tbl_5906;
  10972. else if (tg3_flag(tp, 5705_PLUS))
  10973. mem_tbl = mem_tbl_5705;
  10974. else
  10975. mem_tbl = mem_tbl_570x;
  10976. for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
  10977. err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
  10978. if (err)
  10979. break;
  10980. }
  10981. return err;
  10982. }
  10983. #define TG3_TSO_MSS 500
  10984. #define TG3_TSO_IP_HDR_LEN 20
  10985. #define TG3_TSO_TCP_HDR_LEN 20
  10986. #define TG3_TSO_TCP_OPT_LEN 12
  10987. static const u8 tg3_tso_header[] = {
  10988. 0x08, 0x00,
  10989. 0x45, 0x00, 0x00, 0x00,
  10990. 0x00, 0x00, 0x40, 0x00,
  10991. 0x40, 0x06, 0x00, 0x00,
  10992. 0x0a, 0x00, 0x00, 0x01,
  10993. 0x0a, 0x00, 0x00, 0x02,
  10994. 0x0d, 0x00, 0xe0, 0x00,
  10995. 0x00, 0x00, 0x01, 0x00,
  10996. 0x00, 0x00, 0x02, 0x00,
  10997. 0x80, 0x10, 0x10, 0x00,
  10998. 0x14, 0x09, 0x00, 0x00,
  10999. 0x01, 0x01, 0x08, 0x0a,
  11000. 0x11, 0x11, 0x11, 0x11,
  11001. 0x11, 0x11, 0x11, 0x11,
  11002. };
  11003. static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, bool tso_loopback)
  11004. {
  11005. u32 rx_start_idx, rx_idx, tx_idx, opaque_key;
  11006. u32 base_flags = 0, mss = 0, desc_idx, coal_now, data_off, val;
  11007. u32 budget;
  11008. struct sk_buff *skb;
  11009. u8 *tx_data, *rx_data;
  11010. dma_addr_t map;
  11011. int num_pkts, tx_len, rx_len, i, err;
  11012. struct tg3_rx_buffer_desc *desc;
  11013. struct tg3_napi *tnapi, *rnapi;
  11014. struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
  11015. tnapi = &tp->napi[0];
  11016. rnapi = &tp->napi[0];
  11017. if (tp->irq_cnt > 1) {
  11018. if (tg3_flag(tp, ENABLE_RSS))
  11019. rnapi = &tp->napi[1];
  11020. if (tg3_flag(tp, ENABLE_TSS))
  11021. tnapi = &tp->napi[1];
  11022. }
  11023. coal_now = tnapi->coal_now | rnapi->coal_now;
  11024. err = -EIO;
  11025. tx_len = pktsz;
  11026. skb = netdev_alloc_skb(tp->dev, tx_len);
  11027. if (!skb)
  11028. return -ENOMEM;
  11029. tx_data = skb_put(skb, tx_len);
  11030. memcpy(tx_data, tp->dev->dev_addr, ETH_ALEN);
  11031. memset(tx_data + ETH_ALEN, 0x0, 8);
  11032. tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
  11033. if (tso_loopback) {
  11034. struct iphdr *iph = (struct iphdr *)&tx_data[ETH_HLEN];
  11035. u32 hdr_len = TG3_TSO_IP_HDR_LEN + TG3_TSO_TCP_HDR_LEN +
  11036. TG3_TSO_TCP_OPT_LEN;
  11037. memcpy(tx_data + ETH_ALEN * 2, tg3_tso_header,
  11038. sizeof(tg3_tso_header));
  11039. mss = TG3_TSO_MSS;
  11040. val = tx_len - ETH_ALEN * 2 - sizeof(tg3_tso_header);
  11041. num_pkts = DIV_ROUND_UP(val, TG3_TSO_MSS);
  11042. /* Set the total length field in the IP header */
  11043. iph->tot_len = htons((u16)(mss + hdr_len));
  11044. base_flags = (TXD_FLAG_CPU_PRE_DMA |
  11045. TXD_FLAG_CPU_POST_DMA);
  11046. if (tg3_flag(tp, HW_TSO_1) ||
  11047. tg3_flag(tp, HW_TSO_2) ||
  11048. tg3_flag(tp, HW_TSO_3)) {
  11049. struct tcphdr *th;
  11050. val = ETH_HLEN + TG3_TSO_IP_HDR_LEN;
  11051. th = (struct tcphdr *)&tx_data[val];
  11052. th->check = 0;
  11053. } else
  11054. base_flags |= TXD_FLAG_TCPUDP_CSUM;
  11055. if (tg3_flag(tp, HW_TSO_3)) {
  11056. mss |= (hdr_len & 0xc) << 12;
  11057. if (hdr_len & 0x10)
  11058. base_flags |= 0x00000010;
  11059. base_flags |= (hdr_len & 0x3e0) << 5;
  11060. } else if (tg3_flag(tp, HW_TSO_2))
  11061. mss |= hdr_len << 9;
  11062. else if (tg3_flag(tp, HW_TSO_1) ||
  11063. tg3_asic_rev(tp) == ASIC_REV_5705) {
  11064. mss |= (TG3_TSO_TCP_OPT_LEN << 9);
  11065. } else {
  11066. base_flags |= (TG3_TSO_TCP_OPT_LEN << 10);
  11067. }
  11068. data_off = ETH_ALEN * 2 + sizeof(tg3_tso_header);
  11069. } else {
  11070. num_pkts = 1;
  11071. data_off = ETH_HLEN;
  11072. if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
  11073. tx_len > VLAN_ETH_FRAME_LEN)
  11074. base_flags |= TXD_FLAG_JMB_PKT;
  11075. }
  11076. for (i = data_off; i < tx_len; i++)
  11077. tx_data[i] = (u8) (i & 0xff);
  11078. map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
  11079. if (pci_dma_mapping_error(tp->pdev, map)) {
  11080. dev_kfree_skb(skb);
  11081. return -EIO;
  11082. }
  11083. val = tnapi->tx_prod;
  11084. tnapi->tx_buffers[val].skb = skb;
  11085. dma_unmap_addr_set(&tnapi->tx_buffers[val], mapping, map);
  11086. tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
  11087. rnapi->coal_now);
  11088. udelay(10);
  11089. rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
  11090. budget = tg3_tx_avail(tnapi);
  11091. if (tg3_tx_frag_set(tnapi, &val, &budget, map, tx_len,
  11092. base_flags | TXD_FLAG_END, mss, 0)) {
  11093. tnapi->tx_buffers[val].skb = NULL;
  11094. dev_kfree_skb(skb);
  11095. return -EIO;
  11096. }
  11097. tnapi->tx_prod++;
  11098. /* Sync BD data before updating mailbox */
  11099. wmb();
  11100. tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
  11101. tr32_mailbox(tnapi->prodmbox);
  11102. udelay(10);
  11103. /* 350 usec to allow enough time on some 10/100 Mbps devices. */
  11104. for (i = 0; i < 35; i++) {
  11105. tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
  11106. coal_now);
  11107. udelay(10);
  11108. tx_idx = tnapi->hw_status->idx[0].tx_consumer;
  11109. rx_idx = rnapi->hw_status->idx[0].rx_producer;
  11110. if ((tx_idx == tnapi->tx_prod) &&
  11111. (rx_idx == (rx_start_idx + num_pkts)))
  11112. break;
  11113. }
  11114. tg3_tx_skb_unmap(tnapi, tnapi->tx_prod - 1, -1);
  11115. dev_kfree_skb(skb);
  11116. if (tx_idx != tnapi->tx_prod)
  11117. goto out;
  11118. if (rx_idx != rx_start_idx + num_pkts)
  11119. goto out;
  11120. val = data_off;
  11121. while (rx_idx != rx_start_idx) {
  11122. desc = &rnapi->rx_rcb[rx_start_idx++];
  11123. desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
  11124. opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
  11125. if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
  11126. (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
  11127. goto out;
  11128. rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT)
  11129. - ETH_FCS_LEN;
  11130. if (!tso_loopback) {
  11131. if (rx_len != tx_len)
  11132. goto out;
  11133. if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
  11134. if (opaque_key != RXD_OPAQUE_RING_STD)
  11135. goto out;
  11136. } else {
  11137. if (opaque_key != RXD_OPAQUE_RING_JUMBO)
  11138. goto out;
  11139. }
  11140. } else if ((desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
  11141. (desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
  11142. >> RXD_TCPCSUM_SHIFT != 0xffff) {
  11143. goto out;
  11144. }
  11145. if (opaque_key == RXD_OPAQUE_RING_STD) {
  11146. rx_data = tpr->rx_std_buffers[desc_idx].data;
  11147. map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx],
  11148. mapping);
  11149. } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
  11150. rx_data = tpr->rx_jmb_buffers[desc_idx].data;
  11151. map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx],
  11152. mapping);
  11153. } else
  11154. goto out;
  11155. pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len,
  11156. PCI_DMA_FROMDEVICE);
  11157. rx_data += TG3_RX_OFFSET(tp);
  11158. for (i = data_off; i < rx_len; i++, val++) {
  11159. if (*(rx_data + i) != (u8) (val & 0xff))
  11160. goto out;
  11161. }
  11162. }
  11163. err = 0;
  11164. /* tg3_free_rings will unmap and free the rx_data */
  11165. out:
  11166. return err;
  11167. }
  11168. #define TG3_STD_LOOPBACK_FAILED 1
  11169. #define TG3_JMB_LOOPBACK_FAILED 2
  11170. #define TG3_TSO_LOOPBACK_FAILED 4
  11171. #define TG3_LOOPBACK_FAILED \
  11172. (TG3_STD_LOOPBACK_FAILED | \
  11173. TG3_JMB_LOOPBACK_FAILED | \
  11174. TG3_TSO_LOOPBACK_FAILED)
  11175. static int tg3_test_loopback(struct tg3 *tp, u64 *data, bool do_extlpbk)
  11176. {
  11177. int err = -EIO;
  11178. u32 eee_cap;
  11179. u32 jmb_pkt_sz = 9000;
  11180. if (tp->dma_limit)
  11181. jmb_pkt_sz = tp->dma_limit - ETH_HLEN;
  11182. eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
  11183. tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
  11184. if (!netif_running(tp->dev)) {
  11185. data[TG3_MAC_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
  11186. data[TG3_PHY_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
  11187. if (do_extlpbk)
  11188. data[TG3_EXT_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
  11189. goto done;
  11190. }
  11191. err = tg3_reset_hw(tp, true);
  11192. if (err) {
  11193. data[TG3_MAC_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
  11194. data[TG3_PHY_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
  11195. if (do_extlpbk)
  11196. data[TG3_EXT_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
  11197. goto done;
  11198. }
  11199. if (tg3_flag(tp, ENABLE_RSS)) {
  11200. int i;
  11201. /* Reroute all rx packets to the 1st queue */
  11202. for (i = MAC_RSS_INDIR_TBL_0;
  11203. i < MAC_RSS_INDIR_TBL_0 + TG3_RSS_INDIR_TBL_SIZE; i += 4)
  11204. tw32(i, 0x0);
  11205. }
  11206. /* HW errata - mac loopback fails in some cases on 5780.
  11207. * Normal traffic and PHY loopback are not affected by
  11208. * errata. Also, the MAC loopback test is deprecated for
  11209. * all newer ASIC revisions.
  11210. */
  11211. if (tg3_asic_rev(tp) != ASIC_REV_5780 &&
  11212. !tg3_flag(tp, CPMU_PRESENT)) {
  11213. tg3_mac_loopback(tp, true);
  11214. if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
  11215. data[TG3_MAC_LOOPB_TEST] |= TG3_STD_LOOPBACK_FAILED;
  11216. if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
  11217. tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
  11218. data[TG3_MAC_LOOPB_TEST] |= TG3_JMB_LOOPBACK_FAILED;
  11219. tg3_mac_loopback(tp, false);
  11220. }
  11221. if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
  11222. !tg3_flag(tp, USE_PHYLIB)) {
  11223. int i;
  11224. tg3_phy_lpbk_set(tp, 0, false);
  11225. /* Wait for link */
  11226. for (i = 0; i < 100; i++) {
  11227. if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
  11228. break;
  11229. mdelay(1);
  11230. }
  11231. if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
  11232. data[TG3_PHY_LOOPB_TEST] |= TG3_STD_LOOPBACK_FAILED;
  11233. if (tg3_flag(tp, TSO_CAPABLE) &&
  11234. tg3_run_loopback(tp, ETH_FRAME_LEN, true))
  11235. data[TG3_PHY_LOOPB_TEST] |= TG3_TSO_LOOPBACK_FAILED;
  11236. if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
  11237. tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
  11238. data[TG3_PHY_LOOPB_TEST] |= TG3_JMB_LOOPBACK_FAILED;
  11239. if (do_extlpbk) {
  11240. tg3_phy_lpbk_set(tp, 0, true);
  11241. /* All link indications report up, but the hardware
  11242. * isn't really ready for about 20 msec. Double it
  11243. * to be sure.
  11244. */
  11245. mdelay(40);
  11246. if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
  11247. data[TG3_EXT_LOOPB_TEST] |=
  11248. TG3_STD_LOOPBACK_FAILED;
  11249. if (tg3_flag(tp, TSO_CAPABLE) &&
  11250. tg3_run_loopback(tp, ETH_FRAME_LEN, true))
  11251. data[TG3_EXT_LOOPB_TEST] |=
  11252. TG3_TSO_LOOPBACK_FAILED;
  11253. if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
  11254. tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
  11255. data[TG3_EXT_LOOPB_TEST] |=
  11256. TG3_JMB_LOOPBACK_FAILED;
  11257. }
  11258. /* Re-enable gphy autopowerdown. */
  11259. if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
  11260. tg3_phy_toggle_apd(tp, true);
  11261. }
  11262. err = (data[TG3_MAC_LOOPB_TEST] | data[TG3_PHY_LOOPB_TEST] |
  11263. data[TG3_EXT_LOOPB_TEST]) ? -EIO : 0;
  11264. done:
  11265. tp->phy_flags |= eee_cap;
  11266. return err;
  11267. }
  11268. static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
  11269. u64 *data)
  11270. {
  11271. struct tg3 *tp = netdev_priv(dev);
  11272. bool doextlpbk = etest->flags & ETH_TEST_FL_EXTERNAL_LB;
  11273. if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
  11274. if (tg3_power_up(tp)) {
  11275. etest->flags |= ETH_TEST_FL_FAILED;
  11276. memset(data, 1, sizeof(u64) * TG3_NUM_TEST);
  11277. return;
  11278. }
  11279. tg3_ape_driver_state_change(tp, RESET_KIND_INIT);
  11280. }
  11281. memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
  11282. if (tg3_test_nvram(tp) != 0) {
  11283. etest->flags |= ETH_TEST_FL_FAILED;
  11284. data[TG3_NVRAM_TEST] = 1;
  11285. }
  11286. if (!doextlpbk && tg3_test_link(tp)) {
  11287. etest->flags |= ETH_TEST_FL_FAILED;
  11288. data[TG3_LINK_TEST] = 1;
  11289. }
  11290. if (etest->flags & ETH_TEST_FL_OFFLINE) {
  11291. int err, err2 = 0, irq_sync = 0;
  11292. if (netif_running(dev)) {
  11293. tg3_phy_stop(tp);
  11294. tg3_netif_stop(tp);
  11295. irq_sync = 1;
  11296. }
  11297. tg3_full_lock(tp, irq_sync);
  11298. tg3_halt(tp, RESET_KIND_SUSPEND, 1);
  11299. err = tg3_nvram_lock(tp);
  11300. tg3_halt_cpu(tp, RX_CPU_BASE);
  11301. if (!tg3_flag(tp, 5705_PLUS))
  11302. tg3_halt_cpu(tp, TX_CPU_BASE);
  11303. if (!err)
  11304. tg3_nvram_unlock(tp);
  11305. if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
  11306. tg3_phy_reset(tp);
  11307. if (tg3_test_registers(tp) != 0) {
  11308. etest->flags |= ETH_TEST_FL_FAILED;
  11309. data[TG3_REGISTER_TEST] = 1;
  11310. }
  11311. if (tg3_test_memory(tp) != 0) {
  11312. etest->flags |= ETH_TEST_FL_FAILED;
  11313. data[TG3_MEMORY_TEST] = 1;
  11314. }
  11315. if (doextlpbk)
  11316. etest->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE;
  11317. if (tg3_test_loopback(tp, data, doextlpbk))
  11318. etest->flags |= ETH_TEST_FL_FAILED;
  11319. tg3_full_unlock(tp);
  11320. if (tg3_test_interrupt(tp) != 0) {
  11321. etest->flags |= ETH_TEST_FL_FAILED;
  11322. data[TG3_INTERRUPT_TEST] = 1;
  11323. }
  11324. tg3_full_lock(tp, 0);
  11325. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  11326. if (netif_running(dev)) {
  11327. tg3_flag_set(tp, INIT_COMPLETE);
  11328. err2 = tg3_restart_hw(tp, true);
  11329. if (!err2)
  11330. tg3_netif_start(tp);
  11331. }
  11332. tg3_full_unlock(tp);
  11333. if (irq_sync && !err2)
  11334. tg3_phy_start(tp);
  11335. }
  11336. if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
  11337. tg3_power_down_prepare(tp);
  11338. }
  11339. static int tg3_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
  11340. {
  11341. struct tg3 *tp = netdev_priv(dev);
  11342. struct hwtstamp_config stmpconf;
  11343. if (!tg3_flag(tp, PTP_CAPABLE))
  11344. return -EOPNOTSUPP;
  11345. if (copy_from_user(&stmpconf, ifr->ifr_data, sizeof(stmpconf)))
  11346. return -EFAULT;
  11347. if (stmpconf.flags)
  11348. return -EINVAL;
  11349. if (stmpconf.tx_type != HWTSTAMP_TX_ON &&
  11350. stmpconf.tx_type != HWTSTAMP_TX_OFF)
  11351. return -ERANGE;
  11352. switch (stmpconf.rx_filter) {
  11353. case HWTSTAMP_FILTER_NONE:
  11354. tp->rxptpctl = 0;
  11355. break;
  11356. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  11357. tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
  11358. TG3_RX_PTP_CTL_ALL_V1_EVENTS;
  11359. break;
  11360. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  11361. tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
  11362. TG3_RX_PTP_CTL_SYNC_EVNT;
  11363. break;
  11364. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  11365. tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
  11366. TG3_RX_PTP_CTL_DELAY_REQ;
  11367. break;
  11368. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  11369. tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
  11370. TG3_RX_PTP_CTL_ALL_V2_EVENTS;
  11371. break;
  11372. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  11373. tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
  11374. TG3_RX_PTP_CTL_ALL_V2_EVENTS;
  11375. break;
  11376. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  11377. tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
  11378. TG3_RX_PTP_CTL_ALL_V2_EVENTS;
  11379. break;
  11380. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  11381. tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
  11382. TG3_RX_PTP_CTL_SYNC_EVNT;
  11383. break;
  11384. case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  11385. tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
  11386. TG3_RX_PTP_CTL_SYNC_EVNT;
  11387. break;
  11388. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  11389. tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
  11390. TG3_RX_PTP_CTL_SYNC_EVNT;
  11391. break;
  11392. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  11393. tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
  11394. TG3_RX_PTP_CTL_DELAY_REQ;
  11395. break;
  11396. case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
  11397. tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
  11398. TG3_RX_PTP_CTL_DELAY_REQ;
  11399. break;
  11400. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  11401. tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
  11402. TG3_RX_PTP_CTL_DELAY_REQ;
  11403. break;
  11404. default:
  11405. return -ERANGE;
  11406. }
  11407. if (netif_running(dev) && tp->rxptpctl)
  11408. tw32(TG3_RX_PTP_CTL,
  11409. tp->rxptpctl | TG3_RX_PTP_CTL_HWTS_INTERLOCK);
  11410. if (stmpconf.tx_type == HWTSTAMP_TX_ON)
  11411. tg3_flag_set(tp, TX_TSTAMP_EN);
  11412. else
  11413. tg3_flag_clear(tp, TX_TSTAMP_EN);
  11414. return copy_to_user(ifr->ifr_data, &stmpconf, sizeof(stmpconf)) ?
  11415. -EFAULT : 0;
  11416. }
  11417. static int tg3_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
  11418. {
  11419. struct tg3 *tp = netdev_priv(dev);
  11420. struct hwtstamp_config stmpconf;
  11421. if (!tg3_flag(tp, PTP_CAPABLE))
  11422. return -EOPNOTSUPP;
  11423. stmpconf.flags = 0;
  11424. stmpconf.tx_type = (tg3_flag(tp, TX_TSTAMP_EN) ?
  11425. HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF);
  11426. switch (tp->rxptpctl) {
  11427. case 0:
  11428. stmpconf.rx_filter = HWTSTAMP_FILTER_NONE;
  11429. break;
  11430. case TG3_RX_PTP_CTL_RX_PTP_V1_EN | TG3_RX_PTP_CTL_ALL_V1_EVENTS:
  11431. stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
  11432. break;
  11433. case TG3_RX_PTP_CTL_RX_PTP_V1_EN | TG3_RX_PTP_CTL_SYNC_EVNT:
  11434. stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_SYNC;
  11435. break;
  11436. case TG3_RX_PTP_CTL_RX_PTP_V1_EN | TG3_RX_PTP_CTL_DELAY_REQ:
  11437. stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ;
  11438. break;
  11439. case TG3_RX_PTP_CTL_RX_PTP_V2_EN | TG3_RX_PTP_CTL_ALL_V2_EVENTS:
  11440. stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
  11441. break;
  11442. case TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN | TG3_RX_PTP_CTL_ALL_V2_EVENTS:
  11443. stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
  11444. break;
  11445. case TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN | TG3_RX_PTP_CTL_ALL_V2_EVENTS:
  11446. stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
  11447. break;
  11448. case TG3_RX_PTP_CTL_RX_PTP_V2_EN | TG3_RX_PTP_CTL_SYNC_EVNT:
  11449. stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_SYNC;
  11450. break;
  11451. case TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN | TG3_RX_PTP_CTL_SYNC_EVNT:
  11452. stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_SYNC;
  11453. break;
  11454. case TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN | TG3_RX_PTP_CTL_SYNC_EVNT:
  11455. stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_SYNC;
  11456. break;
  11457. case TG3_RX_PTP_CTL_RX_PTP_V2_EN | TG3_RX_PTP_CTL_DELAY_REQ:
  11458. stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_DELAY_REQ;
  11459. break;
  11460. case TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN | TG3_RX_PTP_CTL_DELAY_REQ:
  11461. stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ;
  11462. break;
  11463. case TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN | TG3_RX_PTP_CTL_DELAY_REQ:
  11464. stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ;
  11465. break;
  11466. default:
  11467. WARN_ON_ONCE(1);
  11468. return -ERANGE;
  11469. }
  11470. return copy_to_user(ifr->ifr_data, &stmpconf, sizeof(stmpconf)) ?
  11471. -EFAULT : 0;
  11472. }
  11473. static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  11474. {
  11475. struct mii_ioctl_data *data = if_mii(ifr);
  11476. struct tg3 *tp = netdev_priv(dev);
  11477. int err;
  11478. if (tg3_flag(tp, USE_PHYLIB)) {
  11479. struct phy_device *phydev;
  11480. if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
  11481. return -EAGAIN;
  11482. phydev = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr);
  11483. return phy_mii_ioctl(phydev, ifr, cmd);
  11484. }
  11485. switch (cmd) {
  11486. case SIOCGMIIPHY:
  11487. data->phy_id = tp->phy_addr;
  11488. /* fallthru */
  11489. case SIOCGMIIREG: {
  11490. u32 mii_regval;
  11491. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
  11492. break; /* We have no PHY */
  11493. if (!netif_running(dev))
  11494. return -EAGAIN;
  11495. spin_lock_bh(&tp->lock);
  11496. err = __tg3_readphy(tp, data->phy_id & 0x1f,
  11497. data->reg_num & 0x1f, &mii_regval);
  11498. spin_unlock_bh(&tp->lock);
  11499. data->val_out = mii_regval;
  11500. return err;
  11501. }
  11502. case SIOCSMIIREG:
  11503. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
  11504. break; /* We have no PHY */
  11505. if (!netif_running(dev))
  11506. return -EAGAIN;
  11507. spin_lock_bh(&tp->lock);
  11508. err = __tg3_writephy(tp, data->phy_id & 0x1f,
  11509. data->reg_num & 0x1f, data->val_in);
  11510. spin_unlock_bh(&tp->lock);
  11511. return err;
  11512. case SIOCSHWTSTAMP:
  11513. return tg3_hwtstamp_set(dev, ifr);
  11514. case SIOCGHWTSTAMP:
  11515. return tg3_hwtstamp_get(dev, ifr);
  11516. default:
  11517. /* do nothing */
  11518. break;
  11519. }
  11520. return -EOPNOTSUPP;
  11521. }
  11522. static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
  11523. {
  11524. struct tg3 *tp = netdev_priv(dev);
  11525. memcpy(ec, &tp->coal, sizeof(*ec));
  11526. return 0;
  11527. }
  11528. static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
  11529. {
  11530. struct tg3 *tp = netdev_priv(dev);
  11531. u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
  11532. u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
  11533. if (!tg3_flag(tp, 5705_PLUS)) {
  11534. max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
  11535. max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
  11536. max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
  11537. min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
  11538. }
  11539. if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
  11540. (!ec->rx_coalesce_usecs) ||
  11541. (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
  11542. (!ec->tx_coalesce_usecs) ||
  11543. (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
  11544. (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
  11545. (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
  11546. (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
  11547. (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
  11548. (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
  11549. (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
  11550. (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
  11551. return -EINVAL;
  11552. /* Only copy relevant parameters, ignore all others. */
  11553. tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
  11554. tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
  11555. tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
  11556. tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
  11557. tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
  11558. tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
  11559. tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
  11560. tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
  11561. tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
  11562. if (netif_running(dev)) {
  11563. tg3_full_lock(tp, 0);
  11564. __tg3_set_coalesce(tp, &tp->coal);
  11565. tg3_full_unlock(tp);
  11566. }
  11567. return 0;
  11568. }
  11569. static int tg3_set_eee(struct net_device *dev, struct ethtool_eee *edata)
  11570. {
  11571. struct tg3 *tp = netdev_priv(dev);
  11572. if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP)) {
  11573. netdev_warn(tp->dev, "Board does not support EEE!\n");
  11574. return -EOPNOTSUPP;
  11575. }
  11576. if (edata->advertised != tp->eee.advertised) {
  11577. netdev_warn(tp->dev,
  11578. "Direct manipulation of EEE advertisement is not supported\n");
  11579. return -EINVAL;
  11580. }
  11581. if (edata->tx_lpi_timer > TG3_CPMU_DBTMR1_LNKIDLE_MAX) {
  11582. netdev_warn(tp->dev,
  11583. "Maximal Tx Lpi timer supported is %#x(u)\n",
  11584. TG3_CPMU_DBTMR1_LNKIDLE_MAX);
  11585. return -EINVAL;
  11586. }
  11587. tp->eee = *edata;
  11588. tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
  11589. tg3_warn_mgmt_link_flap(tp);
  11590. if (netif_running(tp->dev)) {
  11591. tg3_full_lock(tp, 0);
  11592. tg3_setup_eee(tp);
  11593. tg3_phy_reset(tp);
  11594. tg3_full_unlock(tp);
  11595. }
  11596. return 0;
  11597. }
  11598. static int tg3_get_eee(struct net_device *dev, struct ethtool_eee *edata)
  11599. {
  11600. struct tg3 *tp = netdev_priv(dev);
  11601. if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP)) {
  11602. netdev_warn(tp->dev,
  11603. "Board does not support EEE!\n");
  11604. return -EOPNOTSUPP;
  11605. }
  11606. *edata = tp->eee;
  11607. return 0;
  11608. }
  11609. static const struct ethtool_ops tg3_ethtool_ops = {
  11610. .get_drvinfo = tg3_get_drvinfo,
  11611. .get_regs_len = tg3_get_regs_len,
  11612. .get_regs = tg3_get_regs,
  11613. .get_wol = tg3_get_wol,
  11614. .set_wol = tg3_set_wol,
  11615. .get_msglevel = tg3_get_msglevel,
  11616. .set_msglevel = tg3_set_msglevel,
  11617. .nway_reset = tg3_nway_reset,
  11618. .get_link = ethtool_op_get_link,
  11619. .get_eeprom_len = tg3_get_eeprom_len,
  11620. .get_eeprom = tg3_get_eeprom,
  11621. .set_eeprom = tg3_set_eeprom,
  11622. .get_ringparam = tg3_get_ringparam,
  11623. .set_ringparam = tg3_set_ringparam,
  11624. .get_pauseparam = tg3_get_pauseparam,
  11625. .set_pauseparam = tg3_set_pauseparam,
  11626. .self_test = tg3_self_test,
  11627. .get_strings = tg3_get_strings,
  11628. .set_phys_id = tg3_set_phys_id,
  11629. .get_ethtool_stats = tg3_get_ethtool_stats,
  11630. .get_coalesce = tg3_get_coalesce,
  11631. .set_coalesce = tg3_set_coalesce,
  11632. .get_sset_count = tg3_get_sset_count,
  11633. .get_rxnfc = tg3_get_rxnfc,
  11634. .get_rxfh_indir_size = tg3_get_rxfh_indir_size,
  11635. .get_rxfh = tg3_get_rxfh,
  11636. .set_rxfh = tg3_set_rxfh,
  11637. .get_channels = tg3_get_channels,
  11638. .set_channels = tg3_set_channels,
  11639. .get_ts_info = tg3_get_ts_info,
  11640. .get_eee = tg3_get_eee,
  11641. .set_eee = tg3_set_eee,
  11642. .get_link_ksettings = tg3_get_link_ksettings,
  11643. .set_link_ksettings = tg3_set_link_ksettings,
  11644. };
  11645. static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
  11646. struct rtnl_link_stats64 *stats)
  11647. {
  11648. struct tg3 *tp = netdev_priv(dev);
  11649. spin_lock_bh(&tp->lock);
  11650. if (!tp->hw_stats || !tg3_flag(tp, INIT_COMPLETE)) {
  11651. *stats = tp->net_stats_prev;
  11652. spin_unlock_bh(&tp->lock);
  11653. return stats;
  11654. }
  11655. tg3_get_nstats(tp, stats);
  11656. spin_unlock_bh(&tp->lock);
  11657. return stats;
  11658. }
  11659. static void tg3_set_rx_mode(struct net_device *dev)
  11660. {
  11661. struct tg3 *tp = netdev_priv(dev);
  11662. if (!netif_running(dev))
  11663. return;
  11664. tg3_full_lock(tp, 0);
  11665. __tg3_set_rx_mode(dev);
  11666. tg3_full_unlock(tp);
  11667. }
  11668. static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
  11669. int new_mtu)
  11670. {
  11671. dev->mtu = new_mtu;
  11672. if (new_mtu > ETH_DATA_LEN) {
  11673. if (tg3_flag(tp, 5780_CLASS)) {
  11674. netdev_update_features(dev);
  11675. tg3_flag_clear(tp, TSO_CAPABLE);
  11676. } else {
  11677. tg3_flag_set(tp, JUMBO_RING_ENABLE);
  11678. }
  11679. } else {
  11680. if (tg3_flag(tp, 5780_CLASS)) {
  11681. tg3_flag_set(tp, TSO_CAPABLE);
  11682. netdev_update_features(dev);
  11683. }
  11684. tg3_flag_clear(tp, JUMBO_RING_ENABLE);
  11685. }
  11686. }
  11687. static int tg3_change_mtu(struct net_device *dev, int new_mtu)
  11688. {
  11689. struct tg3 *tp = netdev_priv(dev);
  11690. int err;
  11691. bool reset_phy = false;
  11692. if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
  11693. return -EINVAL;
  11694. if (!netif_running(dev)) {
  11695. /* We'll just catch it later when the
  11696. * device is up'd.
  11697. */
  11698. tg3_set_mtu(dev, tp, new_mtu);
  11699. return 0;
  11700. }
  11701. tg3_phy_stop(tp);
  11702. tg3_netif_stop(tp);
  11703. tg3_set_mtu(dev, tp, new_mtu);
  11704. tg3_full_lock(tp, 1);
  11705. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  11706. /* Reset PHY, otherwise the read DMA engine will be in a mode that
  11707. * breaks all requests to 256 bytes.
  11708. */
  11709. if (tg3_asic_rev(tp) == ASIC_REV_57766 ||
  11710. tg3_asic_rev(tp) == ASIC_REV_5717 ||
  11711. tg3_asic_rev(tp) == ASIC_REV_5719 ||
  11712. tg3_asic_rev(tp) == ASIC_REV_5720)
  11713. reset_phy = true;
  11714. err = tg3_restart_hw(tp, reset_phy);
  11715. if (!err)
  11716. tg3_netif_start(tp);
  11717. tg3_full_unlock(tp);
  11718. if (!err)
  11719. tg3_phy_start(tp);
  11720. return err;
  11721. }
  11722. static const struct net_device_ops tg3_netdev_ops = {
  11723. .ndo_open = tg3_open,
  11724. .ndo_stop = tg3_close,
  11725. .ndo_start_xmit = tg3_start_xmit,
  11726. .ndo_get_stats64 = tg3_get_stats64,
  11727. .ndo_validate_addr = eth_validate_addr,
  11728. .ndo_set_rx_mode = tg3_set_rx_mode,
  11729. .ndo_set_mac_address = tg3_set_mac_addr,
  11730. .ndo_do_ioctl = tg3_ioctl,
  11731. .ndo_tx_timeout = tg3_tx_timeout,
  11732. .ndo_change_mtu = tg3_change_mtu,
  11733. .ndo_fix_features = tg3_fix_features,
  11734. .ndo_set_features = tg3_set_features,
  11735. #ifdef CONFIG_NET_POLL_CONTROLLER
  11736. .ndo_poll_controller = tg3_poll_controller,
  11737. #endif
  11738. };
  11739. static void tg3_get_eeprom_size(struct tg3 *tp)
  11740. {
  11741. u32 cursize, val, magic;
  11742. tp->nvram_size = EEPROM_CHIP_SIZE;
  11743. if (tg3_nvram_read(tp, 0, &magic) != 0)
  11744. return;
  11745. if ((magic != TG3_EEPROM_MAGIC) &&
  11746. ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
  11747. ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
  11748. return;
  11749. /*
  11750. * Size the chip by reading offsets at increasing powers of two.
  11751. * When we encounter our validation signature, we know the addressing
  11752. * has wrapped around, and thus have our chip size.
  11753. */
  11754. cursize = 0x10;
  11755. while (cursize < tp->nvram_size) {
  11756. if (tg3_nvram_read(tp, cursize, &val) != 0)
  11757. return;
  11758. if (val == magic)
  11759. break;
  11760. cursize <<= 1;
  11761. }
  11762. tp->nvram_size = cursize;
  11763. }
  11764. static void tg3_get_nvram_size(struct tg3 *tp)
  11765. {
  11766. u32 val;
  11767. if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &val) != 0)
  11768. return;
  11769. /* Selfboot format */
  11770. if (val != TG3_EEPROM_MAGIC) {
  11771. tg3_get_eeprom_size(tp);
  11772. return;
  11773. }
  11774. if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
  11775. if (val != 0) {
  11776. /* This is confusing. We want to operate on the
  11777. * 16-bit value at offset 0xf2. The tg3_nvram_read()
  11778. * call will read from NVRAM and byteswap the data
  11779. * according to the byteswapping settings for all
  11780. * other register accesses. This ensures the data we
  11781. * want will always reside in the lower 16-bits.
  11782. * However, the data in NVRAM is in LE format, which
  11783. * means the data from the NVRAM read will always be
  11784. * opposite the endianness of the CPU. The 16-bit
  11785. * byteswap then brings the data to CPU endianness.
  11786. */
  11787. tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
  11788. return;
  11789. }
  11790. }
  11791. tp->nvram_size = TG3_NVRAM_SIZE_512KB;
  11792. }
  11793. static void tg3_get_nvram_info(struct tg3 *tp)
  11794. {
  11795. u32 nvcfg1;
  11796. nvcfg1 = tr32(NVRAM_CFG1);
  11797. if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
  11798. tg3_flag_set(tp, FLASH);
  11799. } else {
  11800. nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
  11801. tw32(NVRAM_CFG1, nvcfg1);
  11802. }
  11803. if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
  11804. tg3_flag(tp, 5780_CLASS)) {
  11805. switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
  11806. case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
  11807. tp->nvram_jedecnum = JEDEC_ATMEL;
  11808. tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
  11809. tg3_flag_set(tp, NVRAM_BUFFERED);
  11810. break;
  11811. case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
  11812. tp->nvram_jedecnum = JEDEC_ATMEL;
  11813. tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
  11814. break;
  11815. case FLASH_VENDOR_ATMEL_EEPROM:
  11816. tp->nvram_jedecnum = JEDEC_ATMEL;
  11817. tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
  11818. tg3_flag_set(tp, NVRAM_BUFFERED);
  11819. break;
  11820. case FLASH_VENDOR_ST:
  11821. tp->nvram_jedecnum = JEDEC_ST;
  11822. tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
  11823. tg3_flag_set(tp, NVRAM_BUFFERED);
  11824. break;
  11825. case FLASH_VENDOR_SAIFUN:
  11826. tp->nvram_jedecnum = JEDEC_SAIFUN;
  11827. tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
  11828. break;
  11829. case FLASH_VENDOR_SST_SMALL:
  11830. case FLASH_VENDOR_SST_LARGE:
  11831. tp->nvram_jedecnum = JEDEC_SST;
  11832. tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
  11833. break;
  11834. }
  11835. } else {
  11836. tp->nvram_jedecnum = JEDEC_ATMEL;
  11837. tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
  11838. tg3_flag_set(tp, NVRAM_BUFFERED);
  11839. }
  11840. }
  11841. static void tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
  11842. {
  11843. switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
  11844. case FLASH_5752PAGE_SIZE_256:
  11845. tp->nvram_pagesize = 256;
  11846. break;
  11847. case FLASH_5752PAGE_SIZE_512:
  11848. tp->nvram_pagesize = 512;
  11849. break;
  11850. case FLASH_5752PAGE_SIZE_1K:
  11851. tp->nvram_pagesize = 1024;
  11852. break;
  11853. case FLASH_5752PAGE_SIZE_2K:
  11854. tp->nvram_pagesize = 2048;
  11855. break;
  11856. case FLASH_5752PAGE_SIZE_4K:
  11857. tp->nvram_pagesize = 4096;
  11858. break;
  11859. case FLASH_5752PAGE_SIZE_264:
  11860. tp->nvram_pagesize = 264;
  11861. break;
  11862. case FLASH_5752PAGE_SIZE_528:
  11863. tp->nvram_pagesize = 528;
  11864. break;
  11865. }
  11866. }
  11867. static void tg3_get_5752_nvram_info(struct tg3 *tp)
  11868. {
  11869. u32 nvcfg1;
  11870. nvcfg1 = tr32(NVRAM_CFG1);
  11871. /* NVRAM protection for TPM */
  11872. if (nvcfg1 & (1 << 27))
  11873. tg3_flag_set(tp, PROTECTED_NVRAM);
  11874. switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
  11875. case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
  11876. case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
  11877. tp->nvram_jedecnum = JEDEC_ATMEL;
  11878. tg3_flag_set(tp, NVRAM_BUFFERED);
  11879. break;
  11880. case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
  11881. tp->nvram_jedecnum = JEDEC_ATMEL;
  11882. tg3_flag_set(tp, NVRAM_BUFFERED);
  11883. tg3_flag_set(tp, FLASH);
  11884. break;
  11885. case FLASH_5752VENDOR_ST_M45PE10:
  11886. case FLASH_5752VENDOR_ST_M45PE20:
  11887. case FLASH_5752VENDOR_ST_M45PE40:
  11888. tp->nvram_jedecnum = JEDEC_ST;
  11889. tg3_flag_set(tp, NVRAM_BUFFERED);
  11890. tg3_flag_set(tp, FLASH);
  11891. break;
  11892. }
  11893. if (tg3_flag(tp, FLASH)) {
  11894. tg3_nvram_get_pagesize(tp, nvcfg1);
  11895. } else {
  11896. /* For eeprom, set pagesize to maximum eeprom size */
  11897. tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
  11898. nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
  11899. tw32(NVRAM_CFG1, nvcfg1);
  11900. }
  11901. }
  11902. static void tg3_get_5755_nvram_info(struct tg3 *tp)
  11903. {
  11904. u32 nvcfg1, protect = 0;
  11905. nvcfg1 = tr32(NVRAM_CFG1);
  11906. /* NVRAM protection for TPM */
  11907. if (nvcfg1 & (1 << 27)) {
  11908. tg3_flag_set(tp, PROTECTED_NVRAM);
  11909. protect = 1;
  11910. }
  11911. nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
  11912. switch (nvcfg1) {
  11913. case FLASH_5755VENDOR_ATMEL_FLASH_1:
  11914. case FLASH_5755VENDOR_ATMEL_FLASH_2:
  11915. case FLASH_5755VENDOR_ATMEL_FLASH_3:
  11916. case FLASH_5755VENDOR_ATMEL_FLASH_5:
  11917. tp->nvram_jedecnum = JEDEC_ATMEL;
  11918. tg3_flag_set(tp, NVRAM_BUFFERED);
  11919. tg3_flag_set(tp, FLASH);
  11920. tp->nvram_pagesize = 264;
  11921. if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
  11922. nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
  11923. tp->nvram_size = (protect ? 0x3e200 :
  11924. TG3_NVRAM_SIZE_512KB);
  11925. else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
  11926. tp->nvram_size = (protect ? 0x1f200 :
  11927. TG3_NVRAM_SIZE_256KB);
  11928. else
  11929. tp->nvram_size = (protect ? 0x1f200 :
  11930. TG3_NVRAM_SIZE_128KB);
  11931. break;
  11932. case FLASH_5752VENDOR_ST_M45PE10:
  11933. case FLASH_5752VENDOR_ST_M45PE20:
  11934. case FLASH_5752VENDOR_ST_M45PE40:
  11935. tp->nvram_jedecnum = JEDEC_ST;
  11936. tg3_flag_set(tp, NVRAM_BUFFERED);
  11937. tg3_flag_set(tp, FLASH);
  11938. tp->nvram_pagesize = 256;
  11939. if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
  11940. tp->nvram_size = (protect ?
  11941. TG3_NVRAM_SIZE_64KB :
  11942. TG3_NVRAM_SIZE_128KB);
  11943. else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
  11944. tp->nvram_size = (protect ?
  11945. TG3_NVRAM_SIZE_64KB :
  11946. TG3_NVRAM_SIZE_256KB);
  11947. else
  11948. tp->nvram_size = (protect ?
  11949. TG3_NVRAM_SIZE_128KB :
  11950. TG3_NVRAM_SIZE_512KB);
  11951. break;
  11952. }
  11953. }
  11954. static void tg3_get_5787_nvram_info(struct tg3 *tp)
  11955. {
  11956. u32 nvcfg1;
  11957. nvcfg1 = tr32(NVRAM_CFG1);
  11958. switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
  11959. case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
  11960. case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
  11961. case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
  11962. case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
  11963. tp->nvram_jedecnum = JEDEC_ATMEL;
  11964. tg3_flag_set(tp, NVRAM_BUFFERED);
  11965. tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
  11966. nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
  11967. tw32(NVRAM_CFG1, nvcfg1);
  11968. break;
  11969. case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
  11970. case FLASH_5755VENDOR_ATMEL_FLASH_1:
  11971. case FLASH_5755VENDOR_ATMEL_FLASH_2:
  11972. case FLASH_5755VENDOR_ATMEL_FLASH_3:
  11973. tp->nvram_jedecnum = JEDEC_ATMEL;
  11974. tg3_flag_set(tp, NVRAM_BUFFERED);
  11975. tg3_flag_set(tp, FLASH);
  11976. tp->nvram_pagesize = 264;
  11977. break;
  11978. case FLASH_5752VENDOR_ST_M45PE10:
  11979. case FLASH_5752VENDOR_ST_M45PE20:
  11980. case FLASH_5752VENDOR_ST_M45PE40:
  11981. tp->nvram_jedecnum = JEDEC_ST;
  11982. tg3_flag_set(tp, NVRAM_BUFFERED);
  11983. tg3_flag_set(tp, FLASH);
  11984. tp->nvram_pagesize = 256;
  11985. break;
  11986. }
  11987. }
  11988. static void tg3_get_5761_nvram_info(struct tg3 *tp)
  11989. {
  11990. u32 nvcfg1, protect = 0;
  11991. nvcfg1 = tr32(NVRAM_CFG1);
  11992. /* NVRAM protection for TPM */
  11993. if (nvcfg1 & (1 << 27)) {
  11994. tg3_flag_set(tp, PROTECTED_NVRAM);
  11995. protect = 1;
  11996. }
  11997. nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
  11998. switch (nvcfg1) {
  11999. case FLASH_5761VENDOR_ATMEL_ADB021D:
  12000. case FLASH_5761VENDOR_ATMEL_ADB041D:
  12001. case FLASH_5761VENDOR_ATMEL_ADB081D:
  12002. case FLASH_5761VENDOR_ATMEL_ADB161D:
  12003. case FLASH_5761VENDOR_ATMEL_MDB021D:
  12004. case FLASH_5761VENDOR_ATMEL_MDB041D:
  12005. case FLASH_5761VENDOR_ATMEL_MDB081D:
  12006. case FLASH_5761VENDOR_ATMEL_MDB161D:
  12007. tp->nvram_jedecnum = JEDEC_ATMEL;
  12008. tg3_flag_set(tp, NVRAM_BUFFERED);
  12009. tg3_flag_set(tp, FLASH);
  12010. tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
  12011. tp->nvram_pagesize = 256;
  12012. break;
  12013. case FLASH_5761VENDOR_ST_A_M45PE20:
  12014. case FLASH_5761VENDOR_ST_A_M45PE40:
  12015. case FLASH_5761VENDOR_ST_A_M45PE80:
  12016. case FLASH_5761VENDOR_ST_A_M45PE16:
  12017. case FLASH_5761VENDOR_ST_M_M45PE20:
  12018. case FLASH_5761VENDOR_ST_M_M45PE40:
  12019. case FLASH_5761VENDOR_ST_M_M45PE80:
  12020. case FLASH_5761VENDOR_ST_M_M45PE16:
  12021. tp->nvram_jedecnum = JEDEC_ST;
  12022. tg3_flag_set(tp, NVRAM_BUFFERED);
  12023. tg3_flag_set(tp, FLASH);
  12024. tp->nvram_pagesize = 256;
  12025. break;
  12026. }
  12027. if (protect) {
  12028. tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
  12029. } else {
  12030. switch (nvcfg1) {
  12031. case FLASH_5761VENDOR_ATMEL_ADB161D:
  12032. case FLASH_5761VENDOR_ATMEL_MDB161D:
  12033. case FLASH_5761VENDOR_ST_A_M45PE16:
  12034. case FLASH_5761VENDOR_ST_M_M45PE16:
  12035. tp->nvram_size = TG3_NVRAM_SIZE_2MB;
  12036. break;
  12037. case FLASH_5761VENDOR_ATMEL_ADB081D:
  12038. case FLASH_5761VENDOR_ATMEL_MDB081D:
  12039. case FLASH_5761VENDOR_ST_A_M45PE80:
  12040. case FLASH_5761VENDOR_ST_M_M45PE80:
  12041. tp->nvram_size = TG3_NVRAM_SIZE_1MB;
  12042. break;
  12043. case FLASH_5761VENDOR_ATMEL_ADB041D:
  12044. case FLASH_5761VENDOR_ATMEL_MDB041D:
  12045. case FLASH_5761VENDOR_ST_A_M45PE40:
  12046. case FLASH_5761VENDOR_ST_M_M45PE40:
  12047. tp->nvram_size = TG3_NVRAM_SIZE_512KB;
  12048. break;
  12049. case FLASH_5761VENDOR_ATMEL_ADB021D:
  12050. case FLASH_5761VENDOR_ATMEL_MDB021D:
  12051. case FLASH_5761VENDOR_ST_A_M45PE20:
  12052. case FLASH_5761VENDOR_ST_M_M45PE20:
  12053. tp->nvram_size = TG3_NVRAM_SIZE_256KB;
  12054. break;
  12055. }
  12056. }
  12057. }
  12058. static void tg3_get_5906_nvram_info(struct tg3 *tp)
  12059. {
  12060. tp->nvram_jedecnum = JEDEC_ATMEL;
  12061. tg3_flag_set(tp, NVRAM_BUFFERED);
  12062. tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
  12063. }
  12064. static void tg3_get_57780_nvram_info(struct tg3 *tp)
  12065. {
  12066. u32 nvcfg1;
  12067. nvcfg1 = tr32(NVRAM_CFG1);
  12068. switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
  12069. case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
  12070. case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
  12071. tp->nvram_jedecnum = JEDEC_ATMEL;
  12072. tg3_flag_set(tp, NVRAM_BUFFERED);
  12073. tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
  12074. nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
  12075. tw32(NVRAM_CFG1, nvcfg1);
  12076. return;
  12077. case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
  12078. case FLASH_57780VENDOR_ATMEL_AT45DB011D:
  12079. case FLASH_57780VENDOR_ATMEL_AT45DB011B:
  12080. case FLASH_57780VENDOR_ATMEL_AT45DB021D:
  12081. case FLASH_57780VENDOR_ATMEL_AT45DB021B:
  12082. case FLASH_57780VENDOR_ATMEL_AT45DB041D:
  12083. case FLASH_57780VENDOR_ATMEL_AT45DB041B:
  12084. tp->nvram_jedecnum = JEDEC_ATMEL;
  12085. tg3_flag_set(tp, NVRAM_BUFFERED);
  12086. tg3_flag_set(tp, FLASH);
  12087. switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
  12088. case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
  12089. case FLASH_57780VENDOR_ATMEL_AT45DB011D:
  12090. case FLASH_57780VENDOR_ATMEL_AT45DB011B:
  12091. tp->nvram_size = TG3_NVRAM_SIZE_128KB;
  12092. break;
  12093. case FLASH_57780VENDOR_ATMEL_AT45DB021D:
  12094. case FLASH_57780VENDOR_ATMEL_AT45DB021B:
  12095. tp->nvram_size = TG3_NVRAM_SIZE_256KB;
  12096. break;
  12097. case FLASH_57780VENDOR_ATMEL_AT45DB041D:
  12098. case FLASH_57780VENDOR_ATMEL_AT45DB041B:
  12099. tp->nvram_size = TG3_NVRAM_SIZE_512KB;
  12100. break;
  12101. }
  12102. break;
  12103. case FLASH_5752VENDOR_ST_M45PE10:
  12104. case FLASH_5752VENDOR_ST_M45PE20:
  12105. case FLASH_5752VENDOR_ST_M45PE40:
  12106. tp->nvram_jedecnum = JEDEC_ST;
  12107. tg3_flag_set(tp, NVRAM_BUFFERED);
  12108. tg3_flag_set(tp, FLASH);
  12109. switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
  12110. case FLASH_5752VENDOR_ST_M45PE10:
  12111. tp->nvram_size = TG3_NVRAM_SIZE_128KB;
  12112. break;
  12113. case FLASH_5752VENDOR_ST_M45PE20:
  12114. tp->nvram_size = TG3_NVRAM_SIZE_256KB;
  12115. break;
  12116. case FLASH_5752VENDOR_ST_M45PE40:
  12117. tp->nvram_size = TG3_NVRAM_SIZE_512KB;
  12118. break;
  12119. }
  12120. break;
  12121. default:
  12122. tg3_flag_set(tp, NO_NVRAM);
  12123. return;
  12124. }
  12125. tg3_nvram_get_pagesize(tp, nvcfg1);
  12126. if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
  12127. tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
  12128. }
  12129. static void tg3_get_5717_nvram_info(struct tg3 *tp)
  12130. {
  12131. u32 nvcfg1;
  12132. nvcfg1 = tr32(NVRAM_CFG1);
  12133. switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
  12134. case FLASH_5717VENDOR_ATMEL_EEPROM:
  12135. case FLASH_5717VENDOR_MICRO_EEPROM:
  12136. tp->nvram_jedecnum = JEDEC_ATMEL;
  12137. tg3_flag_set(tp, NVRAM_BUFFERED);
  12138. tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
  12139. nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
  12140. tw32(NVRAM_CFG1, nvcfg1);
  12141. return;
  12142. case FLASH_5717VENDOR_ATMEL_MDB011D:
  12143. case FLASH_5717VENDOR_ATMEL_ADB011B:
  12144. case FLASH_5717VENDOR_ATMEL_ADB011D:
  12145. case FLASH_5717VENDOR_ATMEL_MDB021D:
  12146. case FLASH_5717VENDOR_ATMEL_ADB021B:
  12147. case FLASH_5717VENDOR_ATMEL_ADB021D:
  12148. case FLASH_5717VENDOR_ATMEL_45USPT:
  12149. tp->nvram_jedecnum = JEDEC_ATMEL;
  12150. tg3_flag_set(tp, NVRAM_BUFFERED);
  12151. tg3_flag_set(tp, FLASH);
  12152. switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
  12153. case FLASH_5717VENDOR_ATMEL_MDB021D:
  12154. /* Detect size with tg3_nvram_get_size() */
  12155. break;
  12156. case FLASH_5717VENDOR_ATMEL_ADB021B:
  12157. case FLASH_5717VENDOR_ATMEL_ADB021D:
  12158. tp->nvram_size = TG3_NVRAM_SIZE_256KB;
  12159. break;
  12160. default:
  12161. tp->nvram_size = TG3_NVRAM_SIZE_128KB;
  12162. break;
  12163. }
  12164. break;
  12165. case FLASH_5717VENDOR_ST_M_M25PE10:
  12166. case FLASH_5717VENDOR_ST_A_M25PE10:
  12167. case FLASH_5717VENDOR_ST_M_M45PE10:
  12168. case FLASH_5717VENDOR_ST_A_M45PE10:
  12169. case FLASH_5717VENDOR_ST_M_M25PE20:
  12170. case FLASH_5717VENDOR_ST_A_M25PE20:
  12171. case FLASH_5717VENDOR_ST_M_M45PE20:
  12172. case FLASH_5717VENDOR_ST_A_M45PE20:
  12173. case FLASH_5717VENDOR_ST_25USPT:
  12174. case FLASH_5717VENDOR_ST_45USPT:
  12175. tp->nvram_jedecnum = JEDEC_ST;
  12176. tg3_flag_set(tp, NVRAM_BUFFERED);
  12177. tg3_flag_set(tp, FLASH);
  12178. switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
  12179. case FLASH_5717VENDOR_ST_M_M25PE20:
  12180. case FLASH_5717VENDOR_ST_M_M45PE20:
  12181. /* Detect size with tg3_nvram_get_size() */
  12182. break;
  12183. case FLASH_5717VENDOR_ST_A_M25PE20:
  12184. case FLASH_5717VENDOR_ST_A_M45PE20:
  12185. tp->nvram_size = TG3_NVRAM_SIZE_256KB;
  12186. break;
  12187. default:
  12188. tp->nvram_size = TG3_NVRAM_SIZE_128KB;
  12189. break;
  12190. }
  12191. break;
  12192. default:
  12193. tg3_flag_set(tp, NO_NVRAM);
  12194. return;
  12195. }
  12196. tg3_nvram_get_pagesize(tp, nvcfg1);
  12197. if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
  12198. tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
  12199. }
  12200. static void tg3_get_5720_nvram_info(struct tg3 *tp)
  12201. {
  12202. u32 nvcfg1, nvmpinstrp;
  12203. nvcfg1 = tr32(NVRAM_CFG1);
  12204. nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
  12205. if (tg3_asic_rev(tp) == ASIC_REV_5762) {
  12206. if (!(nvcfg1 & NVRAM_CFG1_5762VENDOR_MASK)) {
  12207. tg3_flag_set(tp, NO_NVRAM);
  12208. return;
  12209. }
  12210. switch (nvmpinstrp) {
  12211. case FLASH_5762_EEPROM_HD:
  12212. nvmpinstrp = FLASH_5720_EEPROM_HD;
  12213. break;
  12214. case FLASH_5762_EEPROM_LD:
  12215. nvmpinstrp = FLASH_5720_EEPROM_LD;
  12216. break;
  12217. case FLASH_5720VENDOR_M_ST_M45PE20:
  12218. /* This pinstrap supports multiple sizes, so force it
  12219. * to read the actual size from location 0xf0.
  12220. */
  12221. nvmpinstrp = FLASH_5720VENDOR_ST_45USPT;
  12222. break;
  12223. }
  12224. }
  12225. switch (nvmpinstrp) {
  12226. case FLASH_5720_EEPROM_HD:
  12227. case FLASH_5720_EEPROM_LD:
  12228. tp->nvram_jedecnum = JEDEC_ATMEL;
  12229. tg3_flag_set(tp, NVRAM_BUFFERED);
  12230. nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
  12231. tw32(NVRAM_CFG1, nvcfg1);
  12232. if (nvmpinstrp == FLASH_5720_EEPROM_HD)
  12233. tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
  12234. else
  12235. tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
  12236. return;
  12237. case FLASH_5720VENDOR_M_ATMEL_DB011D:
  12238. case FLASH_5720VENDOR_A_ATMEL_DB011B:
  12239. case FLASH_5720VENDOR_A_ATMEL_DB011D:
  12240. case FLASH_5720VENDOR_M_ATMEL_DB021D:
  12241. case FLASH_5720VENDOR_A_ATMEL_DB021B:
  12242. case FLASH_5720VENDOR_A_ATMEL_DB021D:
  12243. case FLASH_5720VENDOR_M_ATMEL_DB041D:
  12244. case FLASH_5720VENDOR_A_ATMEL_DB041B:
  12245. case FLASH_5720VENDOR_A_ATMEL_DB041D:
  12246. case FLASH_5720VENDOR_M_ATMEL_DB081D:
  12247. case FLASH_5720VENDOR_A_ATMEL_DB081D:
  12248. case FLASH_5720VENDOR_ATMEL_45USPT:
  12249. tp->nvram_jedecnum = JEDEC_ATMEL;
  12250. tg3_flag_set(tp, NVRAM_BUFFERED);
  12251. tg3_flag_set(tp, FLASH);
  12252. switch (nvmpinstrp) {
  12253. case FLASH_5720VENDOR_M_ATMEL_DB021D:
  12254. case FLASH_5720VENDOR_A_ATMEL_DB021B:
  12255. case FLASH_5720VENDOR_A_ATMEL_DB021D:
  12256. tp->nvram_size = TG3_NVRAM_SIZE_256KB;
  12257. break;
  12258. case FLASH_5720VENDOR_M_ATMEL_DB041D:
  12259. case FLASH_5720VENDOR_A_ATMEL_DB041B:
  12260. case FLASH_5720VENDOR_A_ATMEL_DB041D:
  12261. tp->nvram_size = TG3_NVRAM_SIZE_512KB;
  12262. break;
  12263. case FLASH_5720VENDOR_M_ATMEL_DB081D:
  12264. case FLASH_5720VENDOR_A_ATMEL_DB081D:
  12265. tp->nvram_size = TG3_NVRAM_SIZE_1MB;
  12266. break;
  12267. default:
  12268. if (tg3_asic_rev(tp) != ASIC_REV_5762)
  12269. tp->nvram_size = TG3_NVRAM_SIZE_128KB;
  12270. break;
  12271. }
  12272. break;
  12273. case FLASH_5720VENDOR_M_ST_M25PE10:
  12274. case FLASH_5720VENDOR_M_ST_M45PE10:
  12275. case FLASH_5720VENDOR_A_ST_M25PE10:
  12276. case FLASH_5720VENDOR_A_ST_M45PE10:
  12277. case FLASH_5720VENDOR_M_ST_M25PE20:
  12278. case FLASH_5720VENDOR_M_ST_M45PE20:
  12279. case FLASH_5720VENDOR_A_ST_M25PE20:
  12280. case FLASH_5720VENDOR_A_ST_M45PE20:
  12281. case FLASH_5720VENDOR_M_ST_M25PE40:
  12282. case FLASH_5720VENDOR_M_ST_M45PE40:
  12283. case FLASH_5720VENDOR_A_ST_M25PE40:
  12284. case FLASH_5720VENDOR_A_ST_M45PE40:
  12285. case FLASH_5720VENDOR_M_ST_M25PE80:
  12286. case FLASH_5720VENDOR_M_ST_M45PE80:
  12287. case FLASH_5720VENDOR_A_ST_M25PE80:
  12288. case FLASH_5720VENDOR_A_ST_M45PE80:
  12289. case FLASH_5720VENDOR_ST_25USPT:
  12290. case FLASH_5720VENDOR_ST_45USPT:
  12291. tp->nvram_jedecnum = JEDEC_ST;
  12292. tg3_flag_set(tp, NVRAM_BUFFERED);
  12293. tg3_flag_set(tp, FLASH);
  12294. switch (nvmpinstrp) {
  12295. case FLASH_5720VENDOR_M_ST_M25PE20:
  12296. case FLASH_5720VENDOR_M_ST_M45PE20:
  12297. case FLASH_5720VENDOR_A_ST_M25PE20:
  12298. case FLASH_5720VENDOR_A_ST_M45PE20:
  12299. tp->nvram_size = TG3_NVRAM_SIZE_256KB;
  12300. break;
  12301. case FLASH_5720VENDOR_M_ST_M25PE40:
  12302. case FLASH_5720VENDOR_M_ST_M45PE40:
  12303. case FLASH_5720VENDOR_A_ST_M25PE40:
  12304. case FLASH_5720VENDOR_A_ST_M45PE40:
  12305. tp->nvram_size = TG3_NVRAM_SIZE_512KB;
  12306. break;
  12307. case FLASH_5720VENDOR_M_ST_M25PE80:
  12308. case FLASH_5720VENDOR_M_ST_M45PE80:
  12309. case FLASH_5720VENDOR_A_ST_M25PE80:
  12310. case FLASH_5720VENDOR_A_ST_M45PE80:
  12311. tp->nvram_size = TG3_NVRAM_SIZE_1MB;
  12312. break;
  12313. default:
  12314. if (tg3_asic_rev(tp) != ASIC_REV_5762)
  12315. tp->nvram_size = TG3_NVRAM_SIZE_128KB;
  12316. break;
  12317. }
  12318. break;
  12319. default:
  12320. tg3_flag_set(tp, NO_NVRAM);
  12321. return;
  12322. }
  12323. tg3_nvram_get_pagesize(tp, nvcfg1);
  12324. if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
  12325. tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
  12326. if (tg3_asic_rev(tp) == ASIC_REV_5762) {
  12327. u32 val;
  12328. if (tg3_nvram_read(tp, 0, &val))
  12329. return;
  12330. if (val != TG3_EEPROM_MAGIC &&
  12331. (val & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW)
  12332. tg3_flag_set(tp, NO_NVRAM);
  12333. }
  12334. }
  12335. /* Chips other than 5700/5701 use the NVRAM for fetching info. */
  12336. static void tg3_nvram_init(struct tg3 *tp)
  12337. {
  12338. if (tg3_flag(tp, IS_SSB_CORE)) {
  12339. /* No NVRAM and EEPROM on the SSB Broadcom GigE core. */
  12340. tg3_flag_clear(tp, NVRAM);
  12341. tg3_flag_clear(tp, NVRAM_BUFFERED);
  12342. tg3_flag_set(tp, NO_NVRAM);
  12343. return;
  12344. }
  12345. tw32_f(GRC_EEPROM_ADDR,
  12346. (EEPROM_ADDR_FSM_RESET |
  12347. (EEPROM_DEFAULT_CLOCK_PERIOD <<
  12348. EEPROM_ADDR_CLKPERD_SHIFT)));
  12349. msleep(1);
  12350. /* Enable seeprom accesses. */
  12351. tw32_f(GRC_LOCAL_CTRL,
  12352. tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
  12353. udelay(100);
  12354. if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
  12355. tg3_asic_rev(tp) != ASIC_REV_5701) {
  12356. tg3_flag_set(tp, NVRAM);
  12357. if (tg3_nvram_lock(tp)) {
  12358. netdev_warn(tp->dev,
  12359. "Cannot get nvram lock, %s failed\n",
  12360. __func__);
  12361. return;
  12362. }
  12363. tg3_enable_nvram_access(tp);
  12364. tp->nvram_size = 0;
  12365. if (tg3_asic_rev(tp) == ASIC_REV_5752)
  12366. tg3_get_5752_nvram_info(tp);
  12367. else if (tg3_asic_rev(tp) == ASIC_REV_5755)
  12368. tg3_get_5755_nvram_info(tp);
  12369. else if (tg3_asic_rev(tp) == ASIC_REV_5787 ||
  12370. tg3_asic_rev(tp) == ASIC_REV_5784 ||
  12371. tg3_asic_rev(tp) == ASIC_REV_5785)
  12372. tg3_get_5787_nvram_info(tp);
  12373. else if (tg3_asic_rev(tp) == ASIC_REV_5761)
  12374. tg3_get_5761_nvram_info(tp);
  12375. else if (tg3_asic_rev(tp) == ASIC_REV_5906)
  12376. tg3_get_5906_nvram_info(tp);
  12377. else if (tg3_asic_rev(tp) == ASIC_REV_57780 ||
  12378. tg3_flag(tp, 57765_CLASS))
  12379. tg3_get_57780_nvram_info(tp);
  12380. else if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
  12381. tg3_asic_rev(tp) == ASIC_REV_5719)
  12382. tg3_get_5717_nvram_info(tp);
  12383. else if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
  12384. tg3_asic_rev(tp) == ASIC_REV_5762)
  12385. tg3_get_5720_nvram_info(tp);
  12386. else
  12387. tg3_get_nvram_info(tp);
  12388. if (tp->nvram_size == 0)
  12389. tg3_get_nvram_size(tp);
  12390. tg3_disable_nvram_access(tp);
  12391. tg3_nvram_unlock(tp);
  12392. } else {
  12393. tg3_flag_clear(tp, NVRAM);
  12394. tg3_flag_clear(tp, NVRAM_BUFFERED);
  12395. tg3_get_eeprom_size(tp);
  12396. }
  12397. }
  12398. struct subsys_tbl_ent {
  12399. u16 subsys_vendor, subsys_devid;
  12400. u32 phy_id;
  12401. };
  12402. static struct subsys_tbl_ent subsys_id_to_phy_id[] = {
  12403. /* Broadcom boards. */
  12404. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  12405. TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
  12406. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  12407. TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
  12408. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  12409. TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
  12410. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  12411. TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
  12412. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  12413. TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
  12414. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  12415. TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
  12416. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  12417. TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
  12418. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  12419. TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
  12420. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  12421. TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
  12422. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  12423. TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
  12424. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  12425. TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
  12426. /* 3com boards. */
  12427. { TG3PCI_SUBVENDOR_ID_3COM,
  12428. TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
  12429. { TG3PCI_SUBVENDOR_ID_3COM,
  12430. TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
  12431. { TG3PCI_SUBVENDOR_ID_3COM,
  12432. TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
  12433. { TG3PCI_SUBVENDOR_ID_3COM,
  12434. TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
  12435. { TG3PCI_SUBVENDOR_ID_3COM,
  12436. TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
  12437. /* DELL boards. */
  12438. { TG3PCI_SUBVENDOR_ID_DELL,
  12439. TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
  12440. { TG3PCI_SUBVENDOR_ID_DELL,
  12441. TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
  12442. { TG3PCI_SUBVENDOR_ID_DELL,
  12443. TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
  12444. { TG3PCI_SUBVENDOR_ID_DELL,
  12445. TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
  12446. /* Compaq boards. */
  12447. { TG3PCI_SUBVENDOR_ID_COMPAQ,
  12448. TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
  12449. { TG3PCI_SUBVENDOR_ID_COMPAQ,
  12450. TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
  12451. { TG3PCI_SUBVENDOR_ID_COMPAQ,
  12452. TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
  12453. { TG3PCI_SUBVENDOR_ID_COMPAQ,
  12454. TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
  12455. { TG3PCI_SUBVENDOR_ID_COMPAQ,
  12456. TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
  12457. /* IBM boards. */
  12458. { TG3PCI_SUBVENDOR_ID_IBM,
  12459. TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
  12460. };
  12461. static struct subsys_tbl_ent *tg3_lookup_by_subsys(struct tg3 *tp)
  12462. {
  12463. int i;
  12464. for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
  12465. if ((subsys_id_to_phy_id[i].subsys_vendor ==
  12466. tp->pdev->subsystem_vendor) &&
  12467. (subsys_id_to_phy_id[i].subsys_devid ==
  12468. tp->pdev->subsystem_device))
  12469. return &subsys_id_to_phy_id[i];
  12470. }
  12471. return NULL;
  12472. }
  12473. static void tg3_get_eeprom_hw_cfg(struct tg3 *tp)
  12474. {
  12475. u32 val;
  12476. tp->phy_id = TG3_PHY_ID_INVALID;
  12477. tp->led_ctrl = LED_CTRL_MODE_PHY_1;
  12478. /* Assume an onboard device and WOL capable by default. */
  12479. tg3_flag_set(tp, EEPROM_WRITE_PROT);
  12480. tg3_flag_set(tp, WOL_CAP);
  12481. if (tg3_asic_rev(tp) == ASIC_REV_5906) {
  12482. if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
  12483. tg3_flag_clear(tp, EEPROM_WRITE_PROT);
  12484. tg3_flag_set(tp, IS_NIC);
  12485. }
  12486. val = tr32(VCPU_CFGSHDW);
  12487. if (val & VCPU_CFGSHDW_ASPM_DBNC)
  12488. tg3_flag_set(tp, ASPM_WORKAROUND);
  12489. if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
  12490. (val & VCPU_CFGSHDW_WOL_MAGPKT)) {
  12491. tg3_flag_set(tp, WOL_ENABLE);
  12492. device_set_wakeup_enable(&tp->pdev->dev, true);
  12493. }
  12494. goto done;
  12495. }
  12496. tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
  12497. if (val == NIC_SRAM_DATA_SIG_MAGIC) {
  12498. u32 nic_cfg, led_cfg;
  12499. u32 cfg2 = 0, cfg4 = 0, cfg5 = 0;
  12500. u32 nic_phy_id, ver, eeprom_phy_id;
  12501. int eeprom_phy_serdes = 0;
  12502. tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
  12503. tp->nic_sram_data_cfg = nic_cfg;
  12504. tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
  12505. ver >>= NIC_SRAM_DATA_VER_SHIFT;
  12506. if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
  12507. tg3_asic_rev(tp) != ASIC_REV_5701 &&
  12508. tg3_asic_rev(tp) != ASIC_REV_5703 &&
  12509. (ver > 0) && (ver < 0x100))
  12510. tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
  12511. if (tg3_asic_rev(tp) == ASIC_REV_5785)
  12512. tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
  12513. if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
  12514. tg3_asic_rev(tp) == ASIC_REV_5719 ||
  12515. tg3_asic_rev(tp) == ASIC_REV_5720)
  12516. tg3_read_mem(tp, NIC_SRAM_DATA_CFG_5, &cfg5);
  12517. if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
  12518. NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
  12519. eeprom_phy_serdes = 1;
  12520. tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
  12521. if (nic_phy_id != 0) {
  12522. u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
  12523. u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
  12524. eeprom_phy_id = (id1 >> 16) << 10;
  12525. eeprom_phy_id |= (id2 & 0xfc00) << 16;
  12526. eeprom_phy_id |= (id2 & 0x03ff) << 0;
  12527. } else
  12528. eeprom_phy_id = 0;
  12529. tp->phy_id = eeprom_phy_id;
  12530. if (eeprom_phy_serdes) {
  12531. if (!tg3_flag(tp, 5705_PLUS))
  12532. tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
  12533. else
  12534. tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
  12535. }
  12536. if (tg3_flag(tp, 5750_PLUS))
  12537. led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
  12538. SHASTA_EXT_LED_MODE_MASK);
  12539. else
  12540. led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
  12541. switch (led_cfg) {
  12542. default:
  12543. case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
  12544. tp->led_ctrl = LED_CTRL_MODE_PHY_1;
  12545. break;
  12546. case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
  12547. tp->led_ctrl = LED_CTRL_MODE_PHY_2;
  12548. break;
  12549. case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
  12550. tp->led_ctrl = LED_CTRL_MODE_MAC;
  12551. /* Default to PHY_1_MODE if 0 (MAC_MODE) is
  12552. * read on some older 5700/5701 bootcode.
  12553. */
  12554. if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
  12555. tg3_asic_rev(tp) == ASIC_REV_5701)
  12556. tp->led_ctrl = LED_CTRL_MODE_PHY_1;
  12557. break;
  12558. case SHASTA_EXT_LED_SHARED:
  12559. tp->led_ctrl = LED_CTRL_MODE_SHARED;
  12560. if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0 &&
  12561. tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A1)
  12562. tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
  12563. LED_CTRL_MODE_PHY_2);
  12564. if (tg3_flag(tp, 5717_PLUS) ||
  12565. tg3_asic_rev(tp) == ASIC_REV_5762)
  12566. tp->led_ctrl |= LED_CTRL_BLINK_RATE_OVERRIDE |
  12567. LED_CTRL_BLINK_RATE_MASK;
  12568. break;
  12569. case SHASTA_EXT_LED_MAC:
  12570. tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
  12571. break;
  12572. case SHASTA_EXT_LED_COMBO:
  12573. tp->led_ctrl = LED_CTRL_MODE_COMBO;
  12574. if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0)
  12575. tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
  12576. LED_CTRL_MODE_PHY_2);
  12577. break;
  12578. }
  12579. if ((tg3_asic_rev(tp) == ASIC_REV_5700 ||
  12580. tg3_asic_rev(tp) == ASIC_REV_5701) &&
  12581. tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
  12582. tp->led_ctrl = LED_CTRL_MODE_PHY_2;
  12583. if (tg3_chip_rev(tp) == CHIPREV_5784_AX)
  12584. tp->led_ctrl = LED_CTRL_MODE_PHY_1;
  12585. if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
  12586. tg3_flag_set(tp, EEPROM_WRITE_PROT);
  12587. if ((tp->pdev->subsystem_vendor ==
  12588. PCI_VENDOR_ID_ARIMA) &&
  12589. (tp->pdev->subsystem_device == 0x205a ||
  12590. tp->pdev->subsystem_device == 0x2063))
  12591. tg3_flag_clear(tp, EEPROM_WRITE_PROT);
  12592. } else {
  12593. tg3_flag_clear(tp, EEPROM_WRITE_PROT);
  12594. tg3_flag_set(tp, IS_NIC);
  12595. }
  12596. if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
  12597. tg3_flag_set(tp, ENABLE_ASF);
  12598. if (tg3_flag(tp, 5750_PLUS))
  12599. tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
  12600. }
  12601. if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
  12602. tg3_flag(tp, 5750_PLUS))
  12603. tg3_flag_set(tp, ENABLE_APE);
  12604. if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
  12605. !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
  12606. tg3_flag_clear(tp, WOL_CAP);
  12607. if (tg3_flag(tp, WOL_CAP) &&
  12608. (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE)) {
  12609. tg3_flag_set(tp, WOL_ENABLE);
  12610. device_set_wakeup_enable(&tp->pdev->dev, true);
  12611. }
  12612. if (cfg2 & (1 << 17))
  12613. tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
  12614. /* serdes signal pre-emphasis in register 0x590 set by */
  12615. /* bootcode if bit 18 is set */
  12616. if (cfg2 & (1 << 18))
  12617. tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
  12618. if ((tg3_flag(tp, 57765_PLUS) ||
  12619. (tg3_asic_rev(tp) == ASIC_REV_5784 &&
  12620. tg3_chip_rev(tp) != CHIPREV_5784_AX)) &&
  12621. (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
  12622. tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
  12623. if (tg3_flag(tp, PCI_EXPRESS)) {
  12624. u32 cfg3;
  12625. tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
  12626. if (tg3_asic_rev(tp) != ASIC_REV_5785 &&
  12627. !tg3_flag(tp, 57765_PLUS) &&
  12628. (cfg3 & NIC_SRAM_ASPM_DEBOUNCE))
  12629. tg3_flag_set(tp, ASPM_WORKAROUND);
  12630. if (cfg3 & NIC_SRAM_LNK_FLAP_AVOID)
  12631. tp->phy_flags |= TG3_PHYFLG_KEEP_LINK_ON_PWRDN;
  12632. if (cfg3 & NIC_SRAM_1G_ON_VAUX_OK)
  12633. tp->phy_flags |= TG3_PHYFLG_1G_ON_VAUX_OK;
  12634. }
  12635. if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
  12636. tg3_flag_set(tp, RGMII_INBAND_DISABLE);
  12637. if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
  12638. tg3_flag_set(tp, RGMII_EXT_IBND_RX_EN);
  12639. if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
  12640. tg3_flag_set(tp, RGMII_EXT_IBND_TX_EN);
  12641. if (cfg5 & NIC_SRAM_DISABLE_1G_HALF_ADV)
  12642. tp->phy_flags |= TG3_PHYFLG_DISABLE_1G_HD_ADV;
  12643. }
  12644. done:
  12645. if (tg3_flag(tp, WOL_CAP))
  12646. device_set_wakeup_enable(&tp->pdev->dev,
  12647. tg3_flag(tp, WOL_ENABLE));
  12648. else
  12649. device_set_wakeup_capable(&tp->pdev->dev, false);
  12650. }
  12651. static int tg3_ape_otp_read(struct tg3 *tp, u32 offset, u32 *val)
  12652. {
  12653. int i, err;
  12654. u32 val2, off = offset * 8;
  12655. err = tg3_nvram_lock(tp);
  12656. if (err)
  12657. return err;
  12658. tg3_ape_write32(tp, TG3_APE_OTP_ADDR, off | APE_OTP_ADDR_CPU_ENABLE);
  12659. tg3_ape_write32(tp, TG3_APE_OTP_CTRL, APE_OTP_CTRL_PROG_EN |
  12660. APE_OTP_CTRL_CMD_RD | APE_OTP_CTRL_START);
  12661. tg3_ape_read32(tp, TG3_APE_OTP_CTRL);
  12662. udelay(10);
  12663. for (i = 0; i < 100; i++) {
  12664. val2 = tg3_ape_read32(tp, TG3_APE_OTP_STATUS);
  12665. if (val2 & APE_OTP_STATUS_CMD_DONE) {
  12666. *val = tg3_ape_read32(tp, TG3_APE_OTP_RD_DATA);
  12667. break;
  12668. }
  12669. udelay(10);
  12670. }
  12671. tg3_ape_write32(tp, TG3_APE_OTP_CTRL, 0);
  12672. tg3_nvram_unlock(tp);
  12673. if (val2 & APE_OTP_STATUS_CMD_DONE)
  12674. return 0;
  12675. return -EBUSY;
  12676. }
  12677. static int tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
  12678. {
  12679. int i;
  12680. u32 val;
  12681. tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
  12682. tw32(OTP_CTRL, cmd);
  12683. /* Wait for up to 1 ms for command to execute. */
  12684. for (i = 0; i < 100; i++) {
  12685. val = tr32(OTP_STATUS);
  12686. if (val & OTP_STATUS_CMD_DONE)
  12687. break;
  12688. udelay(10);
  12689. }
  12690. return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
  12691. }
  12692. /* Read the gphy configuration from the OTP region of the chip. The gphy
  12693. * configuration is a 32-bit value that straddles the alignment boundary.
  12694. * We do two 32-bit reads and then shift and merge the results.
  12695. */
  12696. static u32 tg3_read_otp_phycfg(struct tg3 *tp)
  12697. {
  12698. u32 bhalf_otp, thalf_otp;
  12699. tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
  12700. if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
  12701. return 0;
  12702. tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
  12703. if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
  12704. return 0;
  12705. thalf_otp = tr32(OTP_READ_DATA);
  12706. tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
  12707. if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
  12708. return 0;
  12709. bhalf_otp = tr32(OTP_READ_DATA);
  12710. return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
  12711. }
  12712. static void tg3_phy_init_link_config(struct tg3 *tp)
  12713. {
  12714. u32 adv = ADVERTISED_Autoneg;
  12715. if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
  12716. if (!(tp->phy_flags & TG3_PHYFLG_DISABLE_1G_HD_ADV))
  12717. adv |= ADVERTISED_1000baseT_Half;
  12718. adv |= ADVERTISED_1000baseT_Full;
  12719. }
  12720. if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
  12721. adv |= ADVERTISED_100baseT_Half |
  12722. ADVERTISED_100baseT_Full |
  12723. ADVERTISED_10baseT_Half |
  12724. ADVERTISED_10baseT_Full |
  12725. ADVERTISED_TP;
  12726. else
  12727. adv |= ADVERTISED_FIBRE;
  12728. tp->link_config.advertising = adv;
  12729. tp->link_config.speed = SPEED_UNKNOWN;
  12730. tp->link_config.duplex = DUPLEX_UNKNOWN;
  12731. tp->link_config.autoneg = AUTONEG_ENABLE;
  12732. tp->link_config.active_speed = SPEED_UNKNOWN;
  12733. tp->link_config.active_duplex = DUPLEX_UNKNOWN;
  12734. tp->old_link = -1;
  12735. }
  12736. static int tg3_phy_probe(struct tg3 *tp)
  12737. {
  12738. u32 hw_phy_id_1, hw_phy_id_2;
  12739. u32 hw_phy_id, hw_phy_id_masked;
  12740. int err;
  12741. /* flow control autonegotiation is default behavior */
  12742. tg3_flag_set(tp, PAUSE_AUTONEG);
  12743. tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
  12744. if (tg3_flag(tp, ENABLE_APE)) {
  12745. switch (tp->pci_fn) {
  12746. case 0:
  12747. tp->phy_ape_lock = TG3_APE_LOCK_PHY0;
  12748. break;
  12749. case 1:
  12750. tp->phy_ape_lock = TG3_APE_LOCK_PHY1;
  12751. break;
  12752. case 2:
  12753. tp->phy_ape_lock = TG3_APE_LOCK_PHY2;
  12754. break;
  12755. case 3:
  12756. tp->phy_ape_lock = TG3_APE_LOCK_PHY3;
  12757. break;
  12758. }
  12759. }
  12760. if (!tg3_flag(tp, ENABLE_ASF) &&
  12761. !(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
  12762. !(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
  12763. tp->phy_flags &= ~(TG3_PHYFLG_1G_ON_VAUX_OK |
  12764. TG3_PHYFLG_KEEP_LINK_ON_PWRDN);
  12765. if (tg3_flag(tp, USE_PHYLIB))
  12766. return tg3_phy_init(tp);
  12767. /* Reading the PHY ID register can conflict with ASF
  12768. * firmware access to the PHY hardware.
  12769. */
  12770. err = 0;
  12771. if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)) {
  12772. hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
  12773. } else {
  12774. /* Now read the physical PHY_ID from the chip and verify
  12775. * that it is sane. If it doesn't look good, we fall back
  12776. * to either the hard-coded table based PHY_ID and failing
  12777. * that the value found in the eeprom area.
  12778. */
  12779. err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
  12780. err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
  12781. hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
  12782. hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
  12783. hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
  12784. hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
  12785. }
  12786. if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
  12787. tp->phy_id = hw_phy_id;
  12788. if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
  12789. tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
  12790. else
  12791. tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
  12792. } else {
  12793. if (tp->phy_id != TG3_PHY_ID_INVALID) {
  12794. /* Do nothing, phy ID already set up in
  12795. * tg3_get_eeprom_hw_cfg().
  12796. */
  12797. } else {
  12798. struct subsys_tbl_ent *p;
  12799. /* No eeprom signature? Try the hardcoded
  12800. * subsys device table.
  12801. */
  12802. p = tg3_lookup_by_subsys(tp);
  12803. if (p) {
  12804. tp->phy_id = p->phy_id;
  12805. } else if (!tg3_flag(tp, IS_SSB_CORE)) {
  12806. /* For now we saw the IDs 0xbc050cd0,
  12807. * 0xbc050f80 and 0xbc050c30 on devices
  12808. * connected to an BCM4785 and there are
  12809. * probably more. Just assume that the phy is
  12810. * supported when it is connected to a SSB core
  12811. * for now.
  12812. */
  12813. return -ENODEV;
  12814. }
  12815. if (!tp->phy_id ||
  12816. tp->phy_id == TG3_PHY_ID_BCM8002)
  12817. tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
  12818. }
  12819. }
  12820. if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
  12821. (tg3_asic_rev(tp) == ASIC_REV_5719 ||
  12822. tg3_asic_rev(tp) == ASIC_REV_5720 ||
  12823. tg3_asic_rev(tp) == ASIC_REV_57766 ||
  12824. tg3_asic_rev(tp) == ASIC_REV_5762 ||
  12825. (tg3_asic_rev(tp) == ASIC_REV_5717 &&
  12826. tg3_chip_rev_id(tp) != CHIPREV_ID_5717_A0) ||
  12827. (tg3_asic_rev(tp) == ASIC_REV_57765 &&
  12828. tg3_chip_rev_id(tp) != CHIPREV_ID_57765_A0))) {
  12829. tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
  12830. tp->eee.supported = SUPPORTED_100baseT_Full |
  12831. SUPPORTED_1000baseT_Full;
  12832. tp->eee.advertised = ADVERTISED_100baseT_Full |
  12833. ADVERTISED_1000baseT_Full;
  12834. tp->eee.eee_enabled = 1;
  12835. tp->eee.tx_lpi_enabled = 1;
  12836. tp->eee.tx_lpi_timer = TG3_CPMU_DBTMR1_LNKIDLE_2047US;
  12837. }
  12838. tg3_phy_init_link_config(tp);
  12839. if (!(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN) &&
  12840. !(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
  12841. !tg3_flag(tp, ENABLE_APE) &&
  12842. !tg3_flag(tp, ENABLE_ASF)) {
  12843. u32 bmsr, dummy;
  12844. tg3_readphy(tp, MII_BMSR, &bmsr);
  12845. if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
  12846. (bmsr & BMSR_LSTATUS))
  12847. goto skip_phy_reset;
  12848. err = tg3_phy_reset(tp);
  12849. if (err)
  12850. return err;
  12851. tg3_phy_set_wirespeed(tp);
  12852. if (!tg3_phy_copper_an_config_ok(tp, &dummy)) {
  12853. tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
  12854. tp->link_config.flowctrl);
  12855. tg3_writephy(tp, MII_BMCR,
  12856. BMCR_ANENABLE | BMCR_ANRESTART);
  12857. }
  12858. }
  12859. skip_phy_reset:
  12860. if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
  12861. err = tg3_init_5401phy_dsp(tp);
  12862. if (err)
  12863. return err;
  12864. err = tg3_init_5401phy_dsp(tp);
  12865. }
  12866. return err;
  12867. }
  12868. static void tg3_read_vpd(struct tg3 *tp)
  12869. {
  12870. u8 *vpd_data;
  12871. unsigned int block_end, rosize, len;
  12872. u32 vpdlen;
  12873. int j, i = 0;
  12874. vpd_data = (u8 *)tg3_vpd_readblock(tp, &vpdlen);
  12875. if (!vpd_data)
  12876. goto out_no_vpd;
  12877. i = pci_vpd_find_tag(vpd_data, 0, vpdlen, PCI_VPD_LRDT_RO_DATA);
  12878. if (i < 0)
  12879. goto out_not_found;
  12880. rosize = pci_vpd_lrdt_size(&vpd_data[i]);
  12881. block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
  12882. i += PCI_VPD_LRDT_TAG_SIZE;
  12883. if (block_end > vpdlen)
  12884. goto out_not_found;
  12885. j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
  12886. PCI_VPD_RO_KEYWORD_MFR_ID);
  12887. if (j > 0) {
  12888. len = pci_vpd_info_field_size(&vpd_data[j]);
  12889. j += PCI_VPD_INFO_FLD_HDR_SIZE;
  12890. if (j + len > block_end || len != 4 ||
  12891. memcmp(&vpd_data[j], "1028", 4))
  12892. goto partno;
  12893. j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
  12894. PCI_VPD_RO_KEYWORD_VENDOR0);
  12895. if (j < 0)
  12896. goto partno;
  12897. len = pci_vpd_info_field_size(&vpd_data[j]);
  12898. j += PCI_VPD_INFO_FLD_HDR_SIZE;
  12899. if (j + len > block_end)
  12900. goto partno;
  12901. if (len >= sizeof(tp->fw_ver))
  12902. len = sizeof(tp->fw_ver) - 1;
  12903. memset(tp->fw_ver, 0, sizeof(tp->fw_ver));
  12904. snprintf(tp->fw_ver, sizeof(tp->fw_ver), "%.*s bc ", len,
  12905. &vpd_data[j]);
  12906. }
  12907. partno:
  12908. i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
  12909. PCI_VPD_RO_KEYWORD_PARTNO);
  12910. if (i < 0)
  12911. goto out_not_found;
  12912. len = pci_vpd_info_field_size(&vpd_data[i]);
  12913. i += PCI_VPD_INFO_FLD_HDR_SIZE;
  12914. if (len > TG3_BPN_SIZE ||
  12915. (len + i) > vpdlen)
  12916. goto out_not_found;
  12917. memcpy(tp->board_part_number, &vpd_data[i], len);
  12918. out_not_found:
  12919. kfree(vpd_data);
  12920. if (tp->board_part_number[0])
  12921. return;
  12922. out_no_vpd:
  12923. if (tg3_asic_rev(tp) == ASIC_REV_5717) {
  12924. if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
  12925. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C)
  12926. strcpy(tp->board_part_number, "BCM5717");
  12927. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
  12928. strcpy(tp->board_part_number, "BCM5718");
  12929. else
  12930. goto nomatch;
  12931. } else if (tg3_asic_rev(tp) == ASIC_REV_57780) {
  12932. if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
  12933. strcpy(tp->board_part_number, "BCM57780");
  12934. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
  12935. strcpy(tp->board_part_number, "BCM57760");
  12936. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
  12937. strcpy(tp->board_part_number, "BCM57790");
  12938. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
  12939. strcpy(tp->board_part_number, "BCM57788");
  12940. else
  12941. goto nomatch;
  12942. } else if (tg3_asic_rev(tp) == ASIC_REV_57765) {
  12943. if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
  12944. strcpy(tp->board_part_number, "BCM57761");
  12945. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
  12946. strcpy(tp->board_part_number, "BCM57765");
  12947. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
  12948. strcpy(tp->board_part_number, "BCM57781");
  12949. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
  12950. strcpy(tp->board_part_number, "BCM57785");
  12951. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
  12952. strcpy(tp->board_part_number, "BCM57791");
  12953. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
  12954. strcpy(tp->board_part_number, "BCM57795");
  12955. else
  12956. goto nomatch;
  12957. } else if (tg3_asic_rev(tp) == ASIC_REV_57766) {
  12958. if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762)
  12959. strcpy(tp->board_part_number, "BCM57762");
  12960. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766)
  12961. strcpy(tp->board_part_number, "BCM57766");
  12962. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782)
  12963. strcpy(tp->board_part_number, "BCM57782");
  12964. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
  12965. strcpy(tp->board_part_number, "BCM57786");
  12966. else
  12967. goto nomatch;
  12968. } else if (tg3_asic_rev(tp) == ASIC_REV_5906) {
  12969. strcpy(tp->board_part_number, "BCM95906");
  12970. } else {
  12971. nomatch:
  12972. strcpy(tp->board_part_number, "none");
  12973. }
  12974. }
  12975. static int tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
  12976. {
  12977. u32 val;
  12978. if (tg3_nvram_read(tp, offset, &val) ||
  12979. (val & 0xfc000000) != 0x0c000000 ||
  12980. tg3_nvram_read(tp, offset + 4, &val) ||
  12981. val != 0)
  12982. return 0;
  12983. return 1;
  12984. }
  12985. static void tg3_read_bc_ver(struct tg3 *tp)
  12986. {
  12987. u32 val, offset, start, ver_offset;
  12988. int i, dst_off;
  12989. bool newver = false;
  12990. if (tg3_nvram_read(tp, 0xc, &offset) ||
  12991. tg3_nvram_read(tp, 0x4, &start))
  12992. return;
  12993. offset = tg3_nvram_logical_addr(tp, offset);
  12994. if (tg3_nvram_read(tp, offset, &val))
  12995. return;
  12996. if ((val & 0xfc000000) == 0x0c000000) {
  12997. if (tg3_nvram_read(tp, offset + 4, &val))
  12998. return;
  12999. if (val == 0)
  13000. newver = true;
  13001. }
  13002. dst_off = strlen(tp->fw_ver);
  13003. if (newver) {
  13004. if (TG3_VER_SIZE - dst_off < 16 ||
  13005. tg3_nvram_read(tp, offset + 8, &ver_offset))
  13006. return;
  13007. offset = offset + ver_offset - start;
  13008. for (i = 0; i < 16; i += 4) {
  13009. __be32 v;
  13010. if (tg3_nvram_read_be32(tp, offset + i, &v))
  13011. return;
  13012. memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
  13013. }
  13014. } else {
  13015. u32 major, minor;
  13016. if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
  13017. return;
  13018. major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
  13019. TG3_NVM_BCVER_MAJSFT;
  13020. minor = ver_offset & TG3_NVM_BCVER_MINMSK;
  13021. snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
  13022. "v%d.%02d", major, minor);
  13023. }
  13024. }
  13025. static void tg3_read_hwsb_ver(struct tg3 *tp)
  13026. {
  13027. u32 val, major, minor;
  13028. /* Use native endian representation */
  13029. if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
  13030. return;
  13031. major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
  13032. TG3_NVM_HWSB_CFG1_MAJSFT;
  13033. minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
  13034. TG3_NVM_HWSB_CFG1_MINSFT;
  13035. snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
  13036. }
  13037. static void tg3_read_sb_ver(struct tg3 *tp, u32 val)
  13038. {
  13039. u32 offset, major, minor, build;
  13040. strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
  13041. if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
  13042. return;
  13043. switch (val & TG3_EEPROM_SB_REVISION_MASK) {
  13044. case TG3_EEPROM_SB_REVISION_0:
  13045. offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
  13046. break;
  13047. case TG3_EEPROM_SB_REVISION_2:
  13048. offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
  13049. break;
  13050. case TG3_EEPROM_SB_REVISION_3:
  13051. offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
  13052. break;
  13053. case TG3_EEPROM_SB_REVISION_4:
  13054. offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
  13055. break;
  13056. case TG3_EEPROM_SB_REVISION_5:
  13057. offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
  13058. break;
  13059. case TG3_EEPROM_SB_REVISION_6:
  13060. offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
  13061. break;
  13062. default:
  13063. return;
  13064. }
  13065. if (tg3_nvram_read(tp, offset, &val))
  13066. return;
  13067. build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
  13068. TG3_EEPROM_SB_EDH_BLD_SHFT;
  13069. major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
  13070. TG3_EEPROM_SB_EDH_MAJ_SHFT;
  13071. minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
  13072. if (minor > 99 || build > 26)
  13073. return;
  13074. offset = strlen(tp->fw_ver);
  13075. snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
  13076. " v%d.%02d", major, minor);
  13077. if (build > 0) {
  13078. offset = strlen(tp->fw_ver);
  13079. if (offset < TG3_VER_SIZE - 1)
  13080. tp->fw_ver[offset] = 'a' + build - 1;
  13081. }
  13082. }
  13083. static void tg3_read_mgmtfw_ver(struct tg3 *tp)
  13084. {
  13085. u32 val, offset, start;
  13086. int i, vlen;
  13087. for (offset = TG3_NVM_DIR_START;
  13088. offset < TG3_NVM_DIR_END;
  13089. offset += TG3_NVM_DIRENT_SIZE) {
  13090. if (tg3_nvram_read(tp, offset, &val))
  13091. return;
  13092. if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
  13093. break;
  13094. }
  13095. if (offset == TG3_NVM_DIR_END)
  13096. return;
  13097. if (!tg3_flag(tp, 5705_PLUS))
  13098. start = 0x08000000;
  13099. else if (tg3_nvram_read(tp, offset - 4, &start))
  13100. return;
  13101. if (tg3_nvram_read(tp, offset + 4, &offset) ||
  13102. !tg3_fw_img_is_valid(tp, offset) ||
  13103. tg3_nvram_read(tp, offset + 8, &val))
  13104. return;
  13105. offset += val - start;
  13106. vlen = strlen(tp->fw_ver);
  13107. tp->fw_ver[vlen++] = ',';
  13108. tp->fw_ver[vlen++] = ' ';
  13109. for (i = 0; i < 4; i++) {
  13110. __be32 v;
  13111. if (tg3_nvram_read_be32(tp, offset, &v))
  13112. return;
  13113. offset += sizeof(v);
  13114. if (vlen > TG3_VER_SIZE - sizeof(v)) {
  13115. memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
  13116. break;
  13117. }
  13118. memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
  13119. vlen += sizeof(v);
  13120. }
  13121. }
  13122. static void tg3_probe_ncsi(struct tg3 *tp)
  13123. {
  13124. u32 apedata;
  13125. apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
  13126. if (apedata != APE_SEG_SIG_MAGIC)
  13127. return;
  13128. apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
  13129. if (!(apedata & APE_FW_STATUS_READY))
  13130. return;
  13131. if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI)
  13132. tg3_flag_set(tp, APE_HAS_NCSI);
  13133. }
  13134. static void tg3_read_dash_ver(struct tg3 *tp)
  13135. {
  13136. int vlen;
  13137. u32 apedata;
  13138. char *fwtype;
  13139. apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
  13140. if (tg3_flag(tp, APE_HAS_NCSI))
  13141. fwtype = "NCSI";
  13142. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725)
  13143. fwtype = "SMASH";
  13144. else
  13145. fwtype = "DASH";
  13146. vlen = strlen(tp->fw_ver);
  13147. snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
  13148. fwtype,
  13149. (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
  13150. (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
  13151. (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
  13152. (apedata & APE_FW_VERSION_BLDMSK));
  13153. }
  13154. static void tg3_read_otp_ver(struct tg3 *tp)
  13155. {
  13156. u32 val, val2;
  13157. if (tg3_asic_rev(tp) != ASIC_REV_5762)
  13158. return;
  13159. if (!tg3_ape_otp_read(tp, OTP_ADDRESS_MAGIC0, &val) &&
  13160. !tg3_ape_otp_read(tp, OTP_ADDRESS_MAGIC0 + 4, &val2) &&
  13161. TG3_OTP_MAGIC0_VALID(val)) {
  13162. u64 val64 = (u64) val << 32 | val2;
  13163. u32 ver = 0;
  13164. int i, vlen;
  13165. for (i = 0; i < 7; i++) {
  13166. if ((val64 & 0xff) == 0)
  13167. break;
  13168. ver = val64 & 0xff;
  13169. val64 >>= 8;
  13170. }
  13171. vlen = strlen(tp->fw_ver);
  13172. snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " .%02d", ver);
  13173. }
  13174. }
  13175. static void tg3_read_fw_ver(struct tg3 *tp)
  13176. {
  13177. u32 val;
  13178. bool vpd_vers = false;
  13179. if (tp->fw_ver[0] != 0)
  13180. vpd_vers = true;
  13181. if (tg3_flag(tp, NO_NVRAM)) {
  13182. strcat(tp->fw_ver, "sb");
  13183. tg3_read_otp_ver(tp);
  13184. return;
  13185. }
  13186. if (tg3_nvram_read(tp, 0, &val))
  13187. return;
  13188. if (val == TG3_EEPROM_MAGIC)
  13189. tg3_read_bc_ver(tp);
  13190. else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
  13191. tg3_read_sb_ver(tp, val);
  13192. else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
  13193. tg3_read_hwsb_ver(tp);
  13194. if (tg3_flag(tp, ENABLE_ASF)) {
  13195. if (tg3_flag(tp, ENABLE_APE)) {
  13196. tg3_probe_ncsi(tp);
  13197. if (!vpd_vers)
  13198. tg3_read_dash_ver(tp);
  13199. } else if (!vpd_vers) {
  13200. tg3_read_mgmtfw_ver(tp);
  13201. }
  13202. }
  13203. tp->fw_ver[TG3_VER_SIZE - 1] = 0;
  13204. }
  13205. static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
  13206. {
  13207. if (tg3_flag(tp, LRG_PROD_RING_CAP))
  13208. return TG3_RX_RET_MAX_SIZE_5717;
  13209. else if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))
  13210. return TG3_RX_RET_MAX_SIZE_5700;
  13211. else
  13212. return TG3_RX_RET_MAX_SIZE_5705;
  13213. }
  13214. static const struct pci_device_id tg3_write_reorder_chipsets[] = {
  13215. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
  13216. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
  13217. { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
  13218. { },
  13219. };
  13220. static struct pci_dev *tg3_find_peer(struct tg3 *tp)
  13221. {
  13222. struct pci_dev *peer;
  13223. unsigned int func, devnr = tp->pdev->devfn & ~7;
  13224. for (func = 0; func < 8; func++) {
  13225. peer = pci_get_slot(tp->pdev->bus, devnr | func);
  13226. if (peer && peer != tp->pdev)
  13227. break;
  13228. pci_dev_put(peer);
  13229. }
  13230. /* 5704 can be configured in single-port mode, set peer to
  13231. * tp->pdev in that case.
  13232. */
  13233. if (!peer) {
  13234. peer = tp->pdev;
  13235. return peer;
  13236. }
  13237. /*
  13238. * We don't need to keep the refcount elevated; there's no way
  13239. * to remove one half of this device without removing the other
  13240. */
  13241. pci_dev_put(peer);
  13242. return peer;
  13243. }
  13244. static void tg3_detect_asic_rev(struct tg3 *tp, u32 misc_ctrl_reg)
  13245. {
  13246. tp->pci_chip_rev_id = misc_ctrl_reg >> MISC_HOST_CTRL_CHIPREV_SHIFT;
  13247. if (tg3_asic_rev(tp) == ASIC_REV_USE_PROD_ID_REG) {
  13248. u32 reg;
  13249. /* All devices that use the alternate
  13250. * ASIC REV location have a CPMU.
  13251. */
  13252. tg3_flag_set(tp, CPMU_PRESENT);
  13253. if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
  13254. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C ||
  13255. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
  13256. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
  13257. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720 ||
  13258. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57767 ||
  13259. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57764 ||
  13260. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5762 ||
  13261. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725 ||
  13262. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5727 ||
  13263. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57787)
  13264. reg = TG3PCI_GEN2_PRODID_ASICREV;
  13265. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
  13266. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
  13267. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
  13268. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
  13269. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
  13270. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
  13271. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762 ||
  13272. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766 ||
  13273. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782 ||
  13274. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
  13275. reg = TG3PCI_GEN15_PRODID_ASICREV;
  13276. else
  13277. reg = TG3PCI_PRODID_ASICREV;
  13278. pci_read_config_dword(tp->pdev, reg, &tp->pci_chip_rev_id);
  13279. }
  13280. /* Wrong chip ID in 5752 A0. This code can be removed later
  13281. * as A0 is not in production.
  13282. */
  13283. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5752_A0_HW)
  13284. tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
  13285. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5717_C0)
  13286. tp->pci_chip_rev_id = CHIPREV_ID_5720_A0;
  13287. if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
  13288. tg3_asic_rev(tp) == ASIC_REV_5719 ||
  13289. tg3_asic_rev(tp) == ASIC_REV_5720)
  13290. tg3_flag_set(tp, 5717_PLUS);
  13291. if (tg3_asic_rev(tp) == ASIC_REV_57765 ||
  13292. tg3_asic_rev(tp) == ASIC_REV_57766)
  13293. tg3_flag_set(tp, 57765_CLASS);
  13294. if (tg3_flag(tp, 57765_CLASS) || tg3_flag(tp, 5717_PLUS) ||
  13295. tg3_asic_rev(tp) == ASIC_REV_5762)
  13296. tg3_flag_set(tp, 57765_PLUS);
  13297. /* Intentionally exclude ASIC_REV_5906 */
  13298. if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
  13299. tg3_asic_rev(tp) == ASIC_REV_5787 ||
  13300. tg3_asic_rev(tp) == ASIC_REV_5784 ||
  13301. tg3_asic_rev(tp) == ASIC_REV_5761 ||
  13302. tg3_asic_rev(tp) == ASIC_REV_5785 ||
  13303. tg3_asic_rev(tp) == ASIC_REV_57780 ||
  13304. tg3_flag(tp, 57765_PLUS))
  13305. tg3_flag_set(tp, 5755_PLUS);
  13306. if (tg3_asic_rev(tp) == ASIC_REV_5780 ||
  13307. tg3_asic_rev(tp) == ASIC_REV_5714)
  13308. tg3_flag_set(tp, 5780_CLASS);
  13309. if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
  13310. tg3_asic_rev(tp) == ASIC_REV_5752 ||
  13311. tg3_asic_rev(tp) == ASIC_REV_5906 ||
  13312. tg3_flag(tp, 5755_PLUS) ||
  13313. tg3_flag(tp, 5780_CLASS))
  13314. tg3_flag_set(tp, 5750_PLUS);
  13315. if (tg3_asic_rev(tp) == ASIC_REV_5705 ||
  13316. tg3_flag(tp, 5750_PLUS))
  13317. tg3_flag_set(tp, 5705_PLUS);
  13318. }
  13319. static bool tg3_10_100_only_device(struct tg3 *tp,
  13320. const struct pci_device_id *ent)
  13321. {
  13322. u32 grc_misc_cfg = tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK;
  13323. if ((tg3_asic_rev(tp) == ASIC_REV_5703 &&
  13324. (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
  13325. (tp->phy_flags & TG3_PHYFLG_IS_FET))
  13326. return true;
  13327. if (ent->driver_data & TG3_DRV_DATA_FLAG_10_100_ONLY) {
  13328. if (tg3_asic_rev(tp) == ASIC_REV_5705) {
  13329. if (ent->driver_data & TG3_DRV_DATA_FLAG_5705_10_100)
  13330. return true;
  13331. } else {
  13332. return true;
  13333. }
  13334. }
  13335. return false;
  13336. }
  13337. static int tg3_get_invariants(struct tg3 *tp, const struct pci_device_id *ent)
  13338. {
  13339. u32 misc_ctrl_reg;
  13340. u32 pci_state_reg, grc_misc_cfg;
  13341. u32 val;
  13342. u16 pci_cmd;
  13343. int err;
  13344. /* Force memory write invalidate off. If we leave it on,
  13345. * then on 5700_BX chips we have to enable a workaround.
  13346. * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
  13347. * to match the cacheline size. The Broadcom driver have this
  13348. * workaround but turns MWI off all the times so never uses
  13349. * it. This seems to suggest that the workaround is insufficient.
  13350. */
  13351. pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
  13352. pci_cmd &= ~PCI_COMMAND_INVALIDATE;
  13353. pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
  13354. /* Important! -- Make sure register accesses are byteswapped
  13355. * correctly. Also, for those chips that require it, make
  13356. * sure that indirect register accesses are enabled before
  13357. * the first operation.
  13358. */
  13359. pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
  13360. &misc_ctrl_reg);
  13361. tp->misc_host_ctrl |= (misc_ctrl_reg &
  13362. MISC_HOST_CTRL_CHIPREV);
  13363. pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
  13364. tp->misc_host_ctrl);
  13365. tg3_detect_asic_rev(tp, misc_ctrl_reg);
  13366. /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
  13367. * we need to disable memory and use config. cycles
  13368. * only to access all registers. The 5702/03 chips
  13369. * can mistakenly decode the special cycles from the
  13370. * ICH chipsets as memory write cycles, causing corruption
  13371. * of register and memory space. Only certain ICH bridges
  13372. * will drive special cycles with non-zero data during the
  13373. * address phase which can fall within the 5703's address
  13374. * range. This is not an ICH bug as the PCI spec allows
  13375. * non-zero address during special cycles. However, only
  13376. * these ICH bridges are known to drive non-zero addresses
  13377. * during special cycles.
  13378. *
  13379. * Since special cycles do not cross PCI bridges, we only
  13380. * enable this workaround if the 5703 is on the secondary
  13381. * bus of these ICH bridges.
  13382. */
  13383. if ((tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A1) ||
  13384. (tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A2)) {
  13385. static struct tg3_dev_id {
  13386. u32 vendor;
  13387. u32 device;
  13388. u32 rev;
  13389. } ich_chipsets[] = {
  13390. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
  13391. PCI_ANY_ID },
  13392. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
  13393. PCI_ANY_ID },
  13394. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
  13395. 0xa },
  13396. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
  13397. PCI_ANY_ID },
  13398. { },
  13399. };
  13400. struct tg3_dev_id *pci_id = &ich_chipsets[0];
  13401. struct pci_dev *bridge = NULL;
  13402. while (pci_id->vendor != 0) {
  13403. bridge = pci_get_device(pci_id->vendor, pci_id->device,
  13404. bridge);
  13405. if (!bridge) {
  13406. pci_id++;
  13407. continue;
  13408. }
  13409. if (pci_id->rev != PCI_ANY_ID) {
  13410. if (bridge->revision > pci_id->rev)
  13411. continue;
  13412. }
  13413. if (bridge->subordinate &&
  13414. (bridge->subordinate->number ==
  13415. tp->pdev->bus->number)) {
  13416. tg3_flag_set(tp, ICH_WORKAROUND);
  13417. pci_dev_put(bridge);
  13418. break;
  13419. }
  13420. }
  13421. }
  13422. if (tg3_asic_rev(tp) == ASIC_REV_5701) {
  13423. static struct tg3_dev_id {
  13424. u32 vendor;
  13425. u32 device;
  13426. } bridge_chipsets[] = {
  13427. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
  13428. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
  13429. { },
  13430. };
  13431. struct tg3_dev_id *pci_id = &bridge_chipsets[0];
  13432. struct pci_dev *bridge = NULL;
  13433. while (pci_id->vendor != 0) {
  13434. bridge = pci_get_device(pci_id->vendor,
  13435. pci_id->device,
  13436. bridge);
  13437. if (!bridge) {
  13438. pci_id++;
  13439. continue;
  13440. }
  13441. if (bridge->subordinate &&
  13442. (bridge->subordinate->number <=
  13443. tp->pdev->bus->number) &&
  13444. (bridge->subordinate->busn_res.end >=
  13445. tp->pdev->bus->number)) {
  13446. tg3_flag_set(tp, 5701_DMA_BUG);
  13447. pci_dev_put(bridge);
  13448. break;
  13449. }
  13450. }
  13451. }
  13452. /* The EPB bridge inside 5714, 5715, and 5780 cannot support
  13453. * DMA addresses > 40-bit. This bridge may have other additional
  13454. * 57xx devices behind it in some 4-port NIC designs for example.
  13455. * Any tg3 device found behind the bridge will also need the 40-bit
  13456. * DMA workaround.
  13457. */
  13458. if (tg3_flag(tp, 5780_CLASS)) {
  13459. tg3_flag_set(tp, 40BIT_DMA_BUG);
  13460. tp->msi_cap = tp->pdev->msi_cap;
  13461. } else {
  13462. struct pci_dev *bridge = NULL;
  13463. do {
  13464. bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
  13465. PCI_DEVICE_ID_SERVERWORKS_EPB,
  13466. bridge);
  13467. if (bridge && bridge->subordinate &&
  13468. (bridge->subordinate->number <=
  13469. tp->pdev->bus->number) &&
  13470. (bridge->subordinate->busn_res.end >=
  13471. tp->pdev->bus->number)) {
  13472. tg3_flag_set(tp, 40BIT_DMA_BUG);
  13473. pci_dev_put(bridge);
  13474. break;
  13475. }
  13476. } while (bridge);
  13477. }
  13478. if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
  13479. tg3_asic_rev(tp) == ASIC_REV_5714)
  13480. tp->pdev_peer = tg3_find_peer(tp);
  13481. /* Determine TSO capabilities */
  13482. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0)
  13483. ; /* Do nothing. HW bug. */
  13484. else if (tg3_flag(tp, 57765_PLUS))
  13485. tg3_flag_set(tp, HW_TSO_3);
  13486. else if (tg3_flag(tp, 5755_PLUS) ||
  13487. tg3_asic_rev(tp) == ASIC_REV_5906)
  13488. tg3_flag_set(tp, HW_TSO_2);
  13489. else if (tg3_flag(tp, 5750_PLUS)) {
  13490. tg3_flag_set(tp, HW_TSO_1);
  13491. tg3_flag_set(tp, TSO_BUG);
  13492. if (tg3_asic_rev(tp) == ASIC_REV_5750 &&
  13493. tg3_chip_rev_id(tp) >= CHIPREV_ID_5750_C2)
  13494. tg3_flag_clear(tp, TSO_BUG);
  13495. } else if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
  13496. tg3_asic_rev(tp) != ASIC_REV_5701 &&
  13497. tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
  13498. tg3_flag_set(tp, FW_TSO);
  13499. tg3_flag_set(tp, TSO_BUG);
  13500. if (tg3_asic_rev(tp) == ASIC_REV_5705)
  13501. tp->fw_needed = FIRMWARE_TG3TSO5;
  13502. else
  13503. tp->fw_needed = FIRMWARE_TG3TSO;
  13504. }
  13505. /* Selectively allow TSO based on operating conditions */
  13506. if (tg3_flag(tp, HW_TSO_1) ||
  13507. tg3_flag(tp, HW_TSO_2) ||
  13508. tg3_flag(tp, HW_TSO_3) ||
  13509. tg3_flag(tp, FW_TSO)) {
  13510. /* For firmware TSO, assume ASF is disabled.
  13511. * We'll disable TSO later if we discover ASF
  13512. * is enabled in tg3_get_eeprom_hw_cfg().
  13513. */
  13514. tg3_flag_set(tp, TSO_CAPABLE);
  13515. } else {
  13516. tg3_flag_clear(tp, TSO_CAPABLE);
  13517. tg3_flag_clear(tp, TSO_BUG);
  13518. tp->fw_needed = NULL;
  13519. }
  13520. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0)
  13521. tp->fw_needed = FIRMWARE_TG3;
  13522. if (tg3_asic_rev(tp) == ASIC_REV_57766)
  13523. tp->fw_needed = FIRMWARE_TG357766;
  13524. tp->irq_max = 1;
  13525. if (tg3_flag(tp, 5750_PLUS)) {
  13526. tg3_flag_set(tp, SUPPORT_MSI);
  13527. if (tg3_chip_rev(tp) == CHIPREV_5750_AX ||
  13528. tg3_chip_rev(tp) == CHIPREV_5750_BX ||
  13529. (tg3_asic_rev(tp) == ASIC_REV_5714 &&
  13530. tg3_chip_rev_id(tp) <= CHIPREV_ID_5714_A2 &&
  13531. tp->pdev_peer == tp->pdev))
  13532. tg3_flag_clear(tp, SUPPORT_MSI);
  13533. if (tg3_flag(tp, 5755_PLUS) ||
  13534. tg3_asic_rev(tp) == ASIC_REV_5906) {
  13535. tg3_flag_set(tp, 1SHOT_MSI);
  13536. }
  13537. if (tg3_flag(tp, 57765_PLUS)) {
  13538. tg3_flag_set(tp, SUPPORT_MSIX);
  13539. tp->irq_max = TG3_IRQ_MAX_VECS;
  13540. }
  13541. }
  13542. tp->txq_max = 1;
  13543. tp->rxq_max = 1;
  13544. if (tp->irq_max > 1) {
  13545. tp->rxq_max = TG3_RSS_MAX_NUM_QS;
  13546. tg3_rss_init_dflt_indir_tbl(tp, TG3_RSS_MAX_NUM_QS);
  13547. if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
  13548. tg3_asic_rev(tp) == ASIC_REV_5720)
  13549. tp->txq_max = tp->irq_max - 1;
  13550. }
  13551. if (tg3_flag(tp, 5755_PLUS) ||
  13552. tg3_asic_rev(tp) == ASIC_REV_5906)
  13553. tg3_flag_set(tp, SHORT_DMA_BUG);
  13554. if (tg3_asic_rev(tp) == ASIC_REV_5719)
  13555. tp->dma_limit = TG3_TX_BD_DMA_MAX_4K;
  13556. if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
  13557. tg3_asic_rev(tp) == ASIC_REV_5719 ||
  13558. tg3_asic_rev(tp) == ASIC_REV_5720 ||
  13559. tg3_asic_rev(tp) == ASIC_REV_5762)
  13560. tg3_flag_set(tp, LRG_PROD_RING_CAP);
  13561. if (tg3_flag(tp, 57765_PLUS) &&
  13562. tg3_chip_rev_id(tp) != CHIPREV_ID_5719_A0)
  13563. tg3_flag_set(tp, USE_JUMBO_BDFLAG);
  13564. if (!tg3_flag(tp, 5705_PLUS) ||
  13565. tg3_flag(tp, 5780_CLASS) ||
  13566. tg3_flag(tp, USE_JUMBO_BDFLAG))
  13567. tg3_flag_set(tp, JUMBO_CAPABLE);
  13568. pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
  13569. &pci_state_reg);
  13570. if (pci_is_pcie(tp->pdev)) {
  13571. u16 lnkctl;
  13572. tg3_flag_set(tp, PCI_EXPRESS);
  13573. pcie_capability_read_word(tp->pdev, PCI_EXP_LNKCTL, &lnkctl);
  13574. if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
  13575. if (tg3_asic_rev(tp) == ASIC_REV_5906) {
  13576. tg3_flag_clear(tp, HW_TSO_2);
  13577. tg3_flag_clear(tp, TSO_CAPABLE);
  13578. }
  13579. if (tg3_asic_rev(tp) == ASIC_REV_5784 ||
  13580. tg3_asic_rev(tp) == ASIC_REV_5761 ||
  13581. tg3_chip_rev_id(tp) == CHIPREV_ID_57780_A0 ||
  13582. tg3_chip_rev_id(tp) == CHIPREV_ID_57780_A1)
  13583. tg3_flag_set(tp, CLKREQ_BUG);
  13584. } else if (tg3_chip_rev_id(tp) == CHIPREV_ID_5717_A0) {
  13585. tg3_flag_set(tp, L1PLLPD_EN);
  13586. }
  13587. } else if (tg3_asic_rev(tp) == ASIC_REV_5785) {
  13588. /* BCM5785 devices are effectively PCIe devices, and should
  13589. * follow PCIe codepaths, but do not have a PCIe capabilities
  13590. * section.
  13591. */
  13592. tg3_flag_set(tp, PCI_EXPRESS);
  13593. } else if (!tg3_flag(tp, 5705_PLUS) ||
  13594. tg3_flag(tp, 5780_CLASS)) {
  13595. tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
  13596. if (!tp->pcix_cap) {
  13597. dev_err(&tp->pdev->dev,
  13598. "Cannot find PCI-X capability, aborting\n");
  13599. return -EIO;
  13600. }
  13601. if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
  13602. tg3_flag_set(tp, PCIX_MODE);
  13603. }
  13604. /* If we have an AMD 762 or VIA K8T800 chipset, write
  13605. * reordering to the mailbox registers done by the host
  13606. * controller can cause major troubles. We read back from
  13607. * every mailbox register write to force the writes to be
  13608. * posted to the chip in order.
  13609. */
  13610. if (pci_dev_present(tg3_write_reorder_chipsets) &&
  13611. !tg3_flag(tp, PCI_EXPRESS))
  13612. tg3_flag_set(tp, MBOX_WRITE_REORDER);
  13613. pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
  13614. &tp->pci_cacheline_sz);
  13615. pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
  13616. &tp->pci_lat_timer);
  13617. if (tg3_asic_rev(tp) == ASIC_REV_5703 &&
  13618. tp->pci_lat_timer < 64) {
  13619. tp->pci_lat_timer = 64;
  13620. pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
  13621. tp->pci_lat_timer);
  13622. }
  13623. /* Important! -- It is critical that the PCI-X hw workaround
  13624. * situation is decided before the first MMIO register access.
  13625. */
  13626. if (tg3_chip_rev(tp) == CHIPREV_5700_BX) {
  13627. /* 5700 BX chips need to have their TX producer index
  13628. * mailboxes written twice to workaround a bug.
  13629. */
  13630. tg3_flag_set(tp, TXD_MBOX_HWBUG);
  13631. /* If we are in PCI-X mode, enable register write workaround.
  13632. *
  13633. * The workaround is to use indirect register accesses
  13634. * for all chip writes not to mailbox registers.
  13635. */
  13636. if (tg3_flag(tp, PCIX_MODE)) {
  13637. u32 pm_reg;
  13638. tg3_flag_set(tp, PCIX_TARGET_HWBUG);
  13639. /* The chip can have it's power management PCI config
  13640. * space registers clobbered due to this bug.
  13641. * So explicitly force the chip into D0 here.
  13642. */
  13643. pci_read_config_dword(tp->pdev,
  13644. tp->pdev->pm_cap + PCI_PM_CTRL,
  13645. &pm_reg);
  13646. pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
  13647. pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
  13648. pci_write_config_dword(tp->pdev,
  13649. tp->pdev->pm_cap + PCI_PM_CTRL,
  13650. pm_reg);
  13651. /* Also, force SERR#/PERR# in PCI command. */
  13652. pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
  13653. pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
  13654. pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
  13655. }
  13656. }
  13657. if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
  13658. tg3_flag_set(tp, PCI_HIGH_SPEED);
  13659. if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
  13660. tg3_flag_set(tp, PCI_32BIT);
  13661. /* Chip-specific fixup from Broadcom driver */
  13662. if ((tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0) &&
  13663. (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
  13664. pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
  13665. pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
  13666. }
  13667. /* Default fast path register access methods */
  13668. tp->read32 = tg3_read32;
  13669. tp->write32 = tg3_write32;
  13670. tp->read32_mbox = tg3_read32;
  13671. tp->write32_mbox = tg3_write32;
  13672. tp->write32_tx_mbox = tg3_write32;
  13673. tp->write32_rx_mbox = tg3_write32;
  13674. /* Various workaround register access methods */
  13675. if (tg3_flag(tp, PCIX_TARGET_HWBUG))
  13676. tp->write32 = tg3_write_indirect_reg32;
  13677. else if (tg3_asic_rev(tp) == ASIC_REV_5701 ||
  13678. (tg3_flag(tp, PCI_EXPRESS) &&
  13679. tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A0)) {
  13680. /*
  13681. * Back to back register writes can cause problems on these
  13682. * chips, the workaround is to read back all reg writes
  13683. * except those to mailbox regs.
  13684. *
  13685. * See tg3_write_indirect_reg32().
  13686. */
  13687. tp->write32 = tg3_write_flush_reg32;
  13688. }
  13689. if (tg3_flag(tp, TXD_MBOX_HWBUG) || tg3_flag(tp, MBOX_WRITE_REORDER)) {
  13690. tp->write32_tx_mbox = tg3_write32_tx_mbox;
  13691. if (tg3_flag(tp, MBOX_WRITE_REORDER))
  13692. tp->write32_rx_mbox = tg3_write_flush_reg32;
  13693. }
  13694. if (tg3_flag(tp, ICH_WORKAROUND)) {
  13695. tp->read32 = tg3_read_indirect_reg32;
  13696. tp->write32 = tg3_write_indirect_reg32;
  13697. tp->read32_mbox = tg3_read_indirect_mbox;
  13698. tp->write32_mbox = tg3_write_indirect_mbox;
  13699. tp->write32_tx_mbox = tg3_write_indirect_mbox;
  13700. tp->write32_rx_mbox = tg3_write_indirect_mbox;
  13701. iounmap(tp->regs);
  13702. tp->regs = NULL;
  13703. pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
  13704. pci_cmd &= ~PCI_COMMAND_MEMORY;
  13705. pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
  13706. }
  13707. if (tg3_asic_rev(tp) == ASIC_REV_5906) {
  13708. tp->read32_mbox = tg3_read32_mbox_5906;
  13709. tp->write32_mbox = tg3_write32_mbox_5906;
  13710. tp->write32_tx_mbox = tg3_write32_mbox_5906;
  13711. tp->write32_rx_mbox = tg3_write32_mbox_5906;
  13712. }
  13713. if (tp->write32 == tg3_write_indirect_reg32 ||
  13714. (tg3_flag(tp, PCIX_MODE) &&
  13715. (tg3_asic_rev(tp) == ASIC_REV_5700 ||
  13716. tg3_asic_rev(tp) == ASIC_REV_5701)))
  13717. tg3_flag_set(tp, SRAM_USE_CONFIG);
  13718. /* The memory arbiter has to be enabled in order for SRAM accesses
  13719. * to succeed. Normally on powerup the tg3 chip firmware will make
  13720. * sure it is enabled, but other entities such as system netboot
  13721. * code might disable it.
  13722. */
  13723. val = tr32(MEMARB_MODE);
  13724. tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
  13725. tp->pci_fn = PCI_FUNC(tp->pdev->devfn) & 3;
  13726. if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
  13727. tg3_flag(tp, 5780_CLASS)) {
  13728. if (tg3_flag(tp, PCIX_MODE)) {
  13729. pci_read_config_dword(tp->pdev,
  13730. tp->pcix_cap + PCI_X_STATUS,
  13731. &val);
  13732. tp->pci_fn = val & 0x7;
  13733. }
  13734. } else if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
  13735. tg3_asic_rev(tp) == ASIC_REV_5719 ||
  13736. tg3_asic_rev(tp) == ASIC_REV_5720) {
  13737. tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
  13738. if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) != NIC_SRAM_CPMUSTAT_SIG)
  13739. val = tr32(TG3_CPMU_STATUS);
  13740. if (tg3_asic_rev(tp) == ASIC_REV_5717)
  13741. tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5717) ? 1 : 0;
  13742. else
  13743. tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5719) >>
  13744. TG3_CPMU_STATUS_FSHFT_5719;
  13745. }
  13746. if (tg3_flag(tp, FLUSH_POSTED_WRITES)) {
  13747. tp->write32_tx_mbox = tg3_write_flush_reg32;
  13748. tp->write32_rx_mbox = tg3_write_flush_reg32;
  13749. }
  13750. /* Get eeprom hw config before calling tg3_set_power_state().
  13751. * In particular, the TG3_FLAG_IS_NIC flag must be
  13752. * determined before calling tg3_set_power_state() so that
  13753. * we know whether or not to switch out of Vaux power.
  13754. * When the flag is set, it means that GPIO1 is used for eeprom
  13755. * write protect and also implies that it is a LOM where GPIOs
  13756. * are not used to switch power.
  13757. */
  13758. tg3_get_eeprom_hw_cfg(tp);
  13759. if (tg3_flag(tp, FW_TSO) && tg3_flag(tp, ENABLE_ASF)) {
  13760. tg3_flag_clear(tp, TSO_CAPABLE);
  13761. tg3_flag_clear(tp, TSO_BUG);
  13762. tp->fw_needed = NULL;
  13763. }
  13764. if (tg3_flag(tp, ENABLE_APE)) {
  13765. /* Allow reads and writes to the
  13766. * APE register and memory space.
  13767. */
  13768. pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
  13769. PCISTATE_ALLOW_APE_SHMEM_WR |
  13770. PCISTATE_ALLOW_APE_PSPACE_WR;
  13771. pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
  13772. pci_state_reg);
  13773. tg3_ape_lock_init(tp);
  13774. }
  13775. /* Set up tp->grc_local_ctrl before calling
  13776. * tg3_pwrsrc_switch_to_vmain(). GPIO1 driven high
  13777. * will bring 5700's external PHY out of reset.
  13778. * It is also used as eeprom write protect on LOMs.
  13779. */
  13780. tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
  13781. if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
  13782. tg3_flag(tp, EEPROM_WRITE_PROT))
  13783. tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
  13784. GRC_LCLCTRL_GPIO_OUTPUT1);
  13785. /* Unused GPIO3 must be driven as output on 5752 because there
  13786. * are no pull-up resistors on unused GPIO pins.
  13787. */
  13788. else if (tg3_asic_rev(tp) == ASIC_REV_5752)
  13789. tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
  13790. if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
  13791. tg3_asic_rev(tp) == ASIC_REV_57780 ||
  13792. tg3_flag(tp, 57765_CLASS))
  13793. tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
  13794. if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
  13795. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
  13796. /* Turn off the debug UART. */
  13797. tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
  13798. if (tg3_flag(tp, IS_NIC))
  13799. /* Keep VMain power. */
  13800. tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
  13801. GRC_LCLCTRL_GPIO_OUTPUT0;
  13802. }
  13803. if (tg3_asic_rev(tp) == ASIC_REV_5762)
  13804. tp->grc_local_ctrl |=
  13805. tr32(GRC_LOCAL_CTRL) & GRC_LCLCTRL_GPIO_UART_SEL;
  13806. /* Switch out of Vaux if it is a NIC */
  13807. tg3_pwrsrc_switch_to_vmain(tp);
  13808. /* Derive initial jumbo mode from MTU assigned in
  13809. * ether_setup() via the alloc_etherdev() call
  13810. */
  13811. if (tp->dev->mtu > ETH_DATA_LEN && !tg3_flag(tp, 5780_CLASS))
  13812. tg3_flag_set(tp, JUMBO_RING_ENABLE);
  13813. /* Determine WakeOnLan speed to use. */
  13814. if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
  13815. tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
  13816. tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0 ||
  13817. tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B2) {
  13818. tg3_flag_clear(tp, WOL_SPEED_100MB);
  13819. } else {
  13820. tg3_flag_set(tp, WOL_SPEED_100MB);
  13821. }
  13822. if (tg3_asic_rev(tp) == ASIC_REV_5906)
  13823. tp->phy_flags |= TG3_PHYFLG_IS_FET;
  13824. /* A few boards don't want Ethernet@WireSpeed phy feature */
  13825. if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
  13826. (tg3_asic_rev(tp) == ASIC_REV_5705 &&
  13827. (tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) &&
  13828. (tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A1)) ||
  13829. (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
  13830. (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
  13831. tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
  13832. if (tg3_chip_rev(tp) == CHIPREV_5703_AX ||
  13833. tg3_chip_rev(tp) == CHIPREV_5704_AX)
  13834. tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
  13835. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0)
  13836. tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
  13837. if (tg3_flag(tp, 5705_PLUS) &&
  13838. !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
  13839. tg3_asic_rev(tp) != ASIC_REV_5785 &&
  13840. tg3_asic_rev(tp) != ASIC_REV_57780 &&
  13841. !tg3_flag(tp, 57765_PLUS)) {
  13842. if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
  13843. tg3_asic_rev(tp) == ASIC_REV_5787 ||
  13844. tg3_asic_rev(tp) == ASIC_REV_5784 ||
  13845. tg3_asic_rev(tp) == ASIC_REV_5761) {
  13846. if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
  13847. tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
  13848. tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
  13849. if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
  13850. tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
  13851. } else
  13852. tp->phy_flags |= TG3_PHYFLG_BER_BUG;
  13853. }
  13854. if (tg3_asic_rev(tp) == ASIC_REV_5784 &&
  13855. tg3_chip_rev(tp) != CHIPREV_5784_AX) {
  13856. tp->phy_otp = tg3_read_otp_phycfg(tp);
  13857. if (tp->phy_otp == 0)
  13858. tp->phy_otp = TG3_OTP_DEFAULT;
  13859. }
  13860. if (tg3_flag(tp, CPMU_PRESENT))
  13861. tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
  13862. else
  13863. tp->mi_mode = MAC_MI_MODE_BASE;
  13864. tp->coalesce_mode = 0;
  13865. if (tg3_chip_rev(tp) != CHIPREV_5700_AX &&
  13866. tg3_chip_rev(tp) != CHIPREV_5700_BX)
  13867. tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
  13868. /* Set these bits to enable statistics workaround. */
  13869. if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
  13870. tg3_asic_rev(tp) == ASIC_REV_5762 ||
  13871. tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
  13872. tg3_chip_rev_id(tp) == CHIPREV_ID_5720_A0) {
  13873. tp->coalesce_mode |= HOSTCC_MODE_ATTN;
  13874. tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
  13875. }
  13876. if (tg3_asic_rev(tp) == ASIC_REV_5785 ||
  13877. tg3_asic_rev(tp) == ASIC_REV_57780)
  13878. tg3_flag_set(tp, USE_PHYLIB);
  13879. err = tg3_mdio_init(tp);
  13880. if (err)
  13881. return err;
  13882. /* Initialize data/descriptor byte/word swapping. */
  13883. val = tr32(GRC_MODE);
  13884. if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
  13885. tg3_asic_rev(tp) == ASIC_REV_5762)
  13886. val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
  13887. GRC_MODE_WORD_SWAP_B2HRX_DATA |
  13888. GRC_MODE_B2HRX_ENABLE |
  13889. GRC_MODE_HTX2B_ENABLE |
  13890. GRC_MODE_HOST_STACKUP);
  13891. else
  13892. val &= GRC_MODE_HOST_STACKUP;
  13893. tw32(GRC_MODE, val | tp->grc_mode);
  13894. tg3_switch_clocks(tp);
  13895. /* Clear this out for sanity. */
  13896. tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
  13897. /* Clear TG3PCI_REG_BASE_ADDR to prevent hangs. */
  13898. tw32(TG3PCI_REG_BASE_ADDR, 0);
  13899. pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
  13900. &pci_state_reg);
  13901. if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
  13902. !tg3_flag(tp, PCIX_TARGET_HWBUG)) {
  13903. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
  13904. tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0 ||
  13905. tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B2 ||
  13906. tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B5) {
  13907. void __iomem *sram_base;
  13908. /* Write some dummy words into the SRAM status block
  13909. * area, see if it reads back correctly. If the return
  13910. * value is bad, force enable the PCIX workaround.
  13911. */
  13912. sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
  13913. writel(0x00000000, sram_base);
  13914. writel(0x00000000, sram_base + 4);
  13915. writel(0xffffffff, sram_base + 4);
  13916. if (readl(sram_base) != 0x00000000)
  13917. tg3_flag_set(tp, PCIX_TARGET_HWBUG);
  13918. }
  13919. }
  13920. udelay(50);
  13921. tg3_nvram_init(tp);
  13922. /* If the device has an NVRAM, no need to load patch firmware */
  13923. if (tg3_asic_rev(tp) == ASIC_REV_57766 &&
  13924. !tg3_flag(tp, NO_NVRAM))
  13925. tp->fw_needed = NULL;
  13926. grc_misc_cfg = tr32(GRC_MISC_CFG);
  13927. grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
  13928. if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
  13929. (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
  13930. grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
  13931. tg3_flag_set(tp, IS_5788);
  13932. if (!tg3_flag(tp, IS_5788) &&
  13933. tg3_asic_rev(tp) != ASIC_REV_5700)
  13934. tg3_flag_set(tp, TAGGED_STATUS);
  13935. if (tg3_flag(tp, TAGGED_STATUS)) {
  13936. tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
  13937. HOSTCC_MODE_CLRTICK_TXBD);
  13938. tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
  13939. pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
  13940. tp->misc_host_ctrl);
  13941. }
  13942. /* Preserve the APE MAC_MODE bits */
  13943. if (tg3_flag(tp, ENABLE_APE))
  13944. tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
  13945. else
  13946. tp->mac_mode = 0;
  13947. if (tg3_10_100_only_device(tp, ent))
  13948. tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
  13949. err = tg3_phy_probe(tp);
  13950. if (err) {
  13951. dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
  13952. /* ... but do not return immediately ... */
  13953. tg3_mdio_fini(tp);
  13954. }
  13955. tg3_read_vpd(tp);
  13956. tg3_read_fw_ver(tp);
  13957. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
  13958. tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
  13959. } else {
  13960. if (tg3_asic_rev(tp) == ASIC_REV_5700)
  13961. tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
  13962. else
  13963. tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
  13964. }
  13965. /* 5700 {AX,BX} chips have a broken status block link
  13966. * change bit implementation, so we must use the
  13967. * status register in those cases.
  13968. */
  13969. if (tg3_asic_rev(tp) == ASIC_REV_5700)
  13970. tg3_flag_set(tp, USE_LINKCHG_REG);
  13971. else
  13972. tg3_flag_clear(tp, USE_LINKCHG_REG);
  13973. /* The led_ctrl is set during tg3_phy_probe, here we might
  13974. * have to force the link status polling mechanism based
  13975. * upon subsystem IDs.
  13976. */
  13977. if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
  13978. tg3_asic_rev(tp) == ASIC_REV_5701 &&
  13979. !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
  13980. tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
  13981. tg3_flag_set(tp, USE_LINKCHG_REG);
  13982. }
  13983. /* For all SERDES we poll the MAC status register. */
  13984. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
  13985. tg3_flag_set(tp, POLL_SERDES);
  13986. else
  13987. tg3_flag_clear(tp, POLL_SERDES);
  13988. if (tg3_flag(tp, ENABLE_APE) && tg3_flag(tp, ENABLE_ASF))
  13989. tg3_flag_set(tp, POLL_CPMU_LINK);
  13990. tp->rx_offset = NET_SKB_PAD + NET_IP_ALIGN;
  13991. tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
  13992. if (tg3_asic_rev(tp) == ASIC_REV_5701 &&
  13993. tg3_flag(tp, PCIX_MODE)) {
  13994. tp->rx_offset = NET_SKB_PAD;
  13995. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  13996. tp->rx_copy_thresh = ~(u16)0;
  13997. #endif
  13998. }
  13999. tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
  14000. tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
  14001. tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
  14002. tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
  14003. /* Increment the rx prod index on the rx std ring by at most
  14004. * 8 for these chips to workaround hw errata.
  14005. */
  14006. if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
  14007. tg3_asic_rev(tp) == ASIC_REV_5752 ||
  14008. tg3_asic_rev(tp) == ASIC_REV_5755)
  14009. tp->rx_std_max_post = 8;
  14010. if (tg3_flag(tp, ASPM_WORKAROUND))
  14011. tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
  14012. PCIE_PWR_MGMT_L1_THRESH_MSK;
  14013. return err;
  14014. }
  14015. #ifdef CONFIG_SPARC
  14016. static int tg3_get_macaddr_sparc(struct tg3 *tp)
  14017. {
  14018. struct net_device *dev = tp->dev;
  14019. struct pci_dev *pdev = tp->pdev;
  14020. struct device_node *dp = pci_device_to_OF_node(pdev);
  14021. const unsigned char *addr;
  14022. int len;
  14023. addr = of_get_property(dp, "local-mac-address", &len);
  14024. if (addr && len == ETH_ALEN) {
  14025. memcpy(dev->dev_addr, addr, ETH_ALEN);
  14026. return 0;
  14027. }
  14028. return -ENODEV;
  14029. }
  14030. static int tg3_get_default_macaddr_sparc(struct tg3 *tp)
  14031. {
  14032. struct net_device *dev = tp->dev;
  14033. memcpy(dev->dev_addr, idprom->id_ethaddr, ETH_ALEN);
  14034. return 0;
  14035. }
  14036. #endif
  14037. static int tg3_get_device_address(struct tg3 *tp)
  14038. {
  14039. struct net_device *dev = tp->dev;
  14040. u32 hi, lo, mac_offset;
  14041. int addr_ok = 0;
  14042. int err;
  14043. #ifdef CONFIG_SPARC
  14044. if (!tg3_get_macaddr_sparc(tp))
  14045. return 0;
  14046. #endif
  14047. if (tg3_flag(tp, IS_SSB_CORE)) {
  14048. err = ssb_gige_get_macaddr(tp->pdev, &dev->dev_addr[0]);
  14049. if (!err && is_valid_ether_addr(&dev->dev_addr[0]))
  14050. return 0;
  14051. }
  14052. mac_offset = 0x7c;
  14053. if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
  14054. tg3_flag(tp, 5780_CLASS)) {
  14055. if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
  14056. mac_offset = 0xcc;
  14057. if (tg3_nvram_lock(tp))
  14058. tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
  14059. else
  14060. tg3_nvram_unlock(tp);
  14061. } else if (tg3_flag(tp, 5717_PLUS)) {
  14062. if (tp->pci_fn & 1)
  14063. mac_offset = 0xcc;
  14064. if (tp->pci_fn > 1)
  14065. mac_offset += 0x18c;
  14066. } else if (tg3_asic_rev(tp) == ASIC_REV_5906)
  14067. mac_offset = 0x10;
  14068. /* First try to get it from MAC address mailbox. */
  14069. tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
  14070. if ((hi >> 16) == 0x484b) {
  14071. dev->dev_addr[0] = (hi >> 8) & 0xff;
  14072. dev->dev_addr[1] = (hi >> 0) & 0xff;
  14073. tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
  14074. dev->dev_addr[2] = (lo >> 24) & 0xff;
  14075. dev->dev_addr[3] = (lo >> 16) & 0xff;
  14076. dev->dev_addr[4] = (lo >> 8) & 0xff;
  14077. dev->dev_addr[5] = (lo >> 0) & 0xff;
  14078. /* Some old bootcode may report a 0 MAC address in SRAM */
  14079. addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
  14080. }
  14081. if (!addr_ok) {
  14082. /* Next, try NVRAM. */
  14083. if (!tg3_flag(tp, NO_NVRAM) &&
  14084. !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
  14085. !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
  14086. memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
  14087. memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
  14088. }
  14089. /* Finally just fetch it out of the MAC control regs. */
  14090. else {
  14091. hi = tr32(MAC_ADDR_0_HIGH);
  14092. lo = tr32(MAC_ADDR_0_LOW);
  14093. dev->dev_addr[5] = lo & 0xff;
  14094. dev->dev_addr[4] = (lo >> 8) & 0xff;
  14095. dev->dev_addr[3] = (lo >> 16) & 0xff;
  14096. dev->dev_addr[2] = (lo >> 24) & 0xff;
  14097. dev->dev_addr[1] = hi & 0xff;
  14098. dev->dev_addr[0] = (hi >> 8) & 0xff;
  14099. }
  14100. }
  14101. if (!is_valid_ether_addr(&dev->dev_addr[0])) {
  14102. #ifdef CONFIG_SPARC
  14103. if (!tg3_get_default_macaddr_sparc(tp))
  14104. return 0;
  14105. #endif
  14106. return -EINVAL;
  14107. }
  14108. return 0;
  14109. }
  14110. #define BOUNDARY_SINGLE_CACHELINE 1
  14111. #define BOUNDARY_MULTI_CACHELINE 2
  14112. static u32 tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
  14113. {
  14114. int cacheline_size;
  14115. u8 byte;
  14116. int goal;
  14117. pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
  14118. if (byte == 0)
  14119. cacheline_size = 1024;
  14120. else
  14121. cacheline_size = (int) byte * 4;
  14122. /* On 5703 and later chips, the boundary bits have no
  14123. * effect.
  14124. */
  14125. if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
  14126. tg3_asic_rev(tp) != ASIC_REV_5701 &&
  14127. !tg3_flag(tp, PCI_EXPRESS))
  14128. goto out;
  14129. #if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
  14130. goal = BOUNDARY_MULTI_CACHELINE;
  14131. #else
  14132. #if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
  14133. goal = BOUNDARY_SINGLE_CACHELINE;
  14134. #else
  14135. goal = 0;
  14136. #endif
  14137. #endif
  14138. if (tg3_flag(tp, 57765_PLUS)) {
  14139. val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
  14140. goto out;
  14141. }
  14142. if (!goal)
  14143. goto out;
  14144. /* PCI controllers on most RISC systems tend to disconnect
  14145. * when a device tries to burst across a cache-line boundary.
  14146. * Therefore, letting tg3 do so just wastes PCI bandwidth.
  14147. *
  14148. * Unfortunately, for PCI-E there are only limited
  14149. * write-side controls for this, and thus for reads
  14150. * we will still get the disconnects. We'll also waste
  14151. * these PCI cycles for both read and write for chips
  14152. * other than 5700 and 5701 which do not implement the
  14153. * boundary bits.
  14154. */
  14155. if (tg3_flag(tp, PCIX_MODE) && !tg3_flag(tp, PCI_EXPRESS)) {
  14156. switch (cacheline_size) {
  14157. case 16:
  14158. case 32:
  14159. case 64:
  14160. case 128:
  14161. if (goal == BOUNDARY_SINGLE_CACHELINE) {
  14162. val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
  14163. DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
  14164. } else {
  14165. val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
  14166. DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
  14167. }
  14168. break;
  14169. case 256:
  14170. val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
  14171. DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
  14172. break;
  14173. default:
  14174. val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
  14175. DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
  14176. break;
  14177. }
  14178. } else if (tg3_flag(tp, PCI_EXPRESS)) {
  14179. switch (cacheline_size) {
  14180. case 16:
  14181. case 32:
  14182. case 64:
  14183. if (goal == BOUNDARY_SINGLE_CACHELINE) {
  14184. val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
  14185. val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
  14186. break;
  14187. }
  14188. /* fallthrough */
  14189. case 128:
  14190. default:
  14191. val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
  14192. val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
  14193. break;
  14194. }
  14195. } else {
  14196. switch (cacheline_size) {
  14197. case 16:
  14198. if (goal == BOUNDARY_SINGLE_CACHELINE) {
  14199. val |= (DMA_RWCTRL_READ_BNDRY_16 |
  14200. DMA_RWCTRL_WRITE_BNDRY_16);
  14201. break;
  14202. }
  14203. /* fallthrough */
  14204. case 32:
  14205. if (goal == BOUNDARY_SINGLE_CACHELINE) {
  14206. val |= (DMA_RWCTRL_READ_BNDRY_32 |
  14207. DMA_RWCTRL_WRITE_BNDRY_32);
  14208. break;
  14209. }
  14210. /* fallthrough */
  14211. case 64:
  14212. if (goal == BOUNDARY_SINGLE_CACHELINE) {
  14213. val |= (DMA_RWCTRL_READ_BNDRY_64 |
  14214. DMA_RWCTRL_WRITE_BNDRY_64);
  14215. break;
  14216. }
  14217. /* fallthrough */
  14218. case 128:
  14219. if (goal == BOUNDARY_SINGLE_CACHELINE) {
  14220. val |= (DMA_RWCTRL_READ_BNDRY_128 |
  14221. DMA_RWCTRL_WRITE_BNDRY_128);
  14222. break;
  14223. }
  14224. /* fallthrough */
  14225. case 256:
  14226. val |= (DMA_RWCTRL_READ_BNDRY_256 |
  14227. DMA_RWCTRL_WRITE_BNDRY_256);
  14228. break;
  14229. case 512:
  14230. val |= (DMA_RWCTRL_READ_BNDRY_512 |
  14231. DMA_RWCTRL_WRITE_BNDRY_512);
  14232. break;
  14233. case 1024:
  14234. default:
  14235. val |= (DMA_RWCTRL_READ_BNDRY_1024 |
  14236. DMA_RWCTRL_WRITE_BNDRY_1024);
  14237. break;
  14238. }
  14239. }
  14240. out:
  14241. return val;
  14242. }
  14243. static int tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma,
  14244. int size, bool to_device)
  14245. {
  14246. struct tg3_internal_buffer_desc test_desc;
  14247. u32 sram_dma_descs;
  14248. int i, ret;
  14249. sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
  14250. tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
  14251. tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
  14252. tw32(RDMAC_STATUS, 0);
  14253. tw32(WDMAC_STATUS, 0);
  14254. tw32(BUFMGR_MODE, 0);
  14255. tw32(FTQ_RESET, 0);
  14256. test_desc.addr_hi = ((u64) buf_dma) >> 32;
  14257. test_desc.addr_lo = buf_dma & 0xffffffff;
  14258. test_desc.nic_mbuf = 0x00002100;
  14259. test_desc.len = size;
  14260. /*
  14261. * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
  14262. * the *second* time the tg3 driver was getting loaded after an
  14263. * initial scan.
  14264. *
  14265. * Broadcom tells me:
  14266. * ...the DMA engine is connected to the GRC block and a DMA
  14267. * reset may affect the GRC block in some unpredictable way...
  14268. * The behavior of resets to individual blocks has not been tested.
  14269. *
  14270. * Broadcom noted the GRC reset will also reset all sub-components.
  14271. */
  14272. if (to_device) {
  14273. test_desc.cqid_sqid = (13 << 8) | 2;
  14274. tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
  14275. udelay(40);
  14276. } else {
  14277. test_desc.cqid_sqid = (16 << 8) | 7;
  14278. tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
  14279. udelay(40);
  14280. }
  14281. test_desc.flags = 0x00000005;
  14282. for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
  14283. u32 val;
  14284. val = *(((u32 *)&test_desc) + i);
  14285. pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
  14286. sram_dma_descs + (i * sizeof(u32)));
  14287. pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
  14288. }
  14289. pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
  14290. if (to_device)
  14291. tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
  14292. else
  14293. tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
  14294. ret = -ENODEV;
  14295. for (i = 0; i < 40; i++) {
  14296. u32 val;
  14297. if (to_device)
  14298. val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
  14299. else
  14300. val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
  14301. if ((val & 0xffff) == sram_dma_descs) {
  14302. ret = 0;
  14303. break;
  14304. }
  14305. udelay(100);
  14306. }
  14307. return ret;
  14308. }
  14309. #define TEST_BUFFER_SIZE 0x2000
  14310. static const struct pci_device_id tg3_dma_wait_state_chipsets[] = {
  14311. { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
  14312. { },
  14313. };
  14314. static int tg3_test_dma(struct tg3 *tp)
  14315. {
  14316. dma_addr_t buf_dma;
  14317. u32 *buf, saved_dma_rwctrl;
  14318. int ret = 0;
  14319. buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
  14320. &buf_dma, GFP_KERNEL);
  14321. if (!buf) {
  14322. ret = -ENOMEM;
  14323. goto out_nofree;
  14324. }
  14325. tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
  14326. (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
  14327. tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
  14328. if (tg3_flag(tp, 57765_PLUS))
  14329. goto out;
  14330. if (tg3_flag(tp, PCI_EXPRESS)) {
  14331. /* DMA read watermark not used on PCIE */
  14332. tp->dma_rwctrl |= 0x00180000;
  14333. } else if (!tg3_flag(tp, PCIX_MODE)) {
  14334. if (tg3_asic_rev(tp) == ASIC_REV_5705 ||
  14335. tg3_asic_rev(tp) == ASIC_REV_5750)
  14336. tp->dma_rwctrl |= 0x003f0000;
  14337. else
  14338. tp->dma_rwctrl |= 0x003f000f;
  14339. } else {
  14340. if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
  14341. tg3_asic_rev(tp) == ASIC_REV_5704) {
  14342. u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
  14343. u32 read_water = 0x7;
  14344. /* If the 5704 is behind the EPB bridge, we can
  14345. * do the less restrictive ONE_DMA workaround for
  14346. * better performance.
  14347. */
  14348. if (tg3_flag(tp, 40BIT_DMA_BUG) &&
  14349. tg3_asic_rev(tp) == ASIC_REV_5704)
  14350. tp->dma_rwctrl |= 0x8000;
  14351. else if (ccval == 0x6 || ccval == 0x7)
  14352. tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
  14353. if (tg3_asic_rev(tp) == ASIC_REV_5703)
  14354. read_water = 4;
  14355. /* Set bit 23 to enable PCIX hw bug fix */
  14356. tp->dma_rwctrl |=
  14357. (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
  14358. (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
  14359. (1 << 23);
  14360. } else if (tg3_asic_rev(tp) == ASIC_REV_5780) {
  14361. /* 5780 always in PCIX mode */
  14362. tp->dma_rwctrl |= 0x00144000;
  14363. } else if (tg3_asic_rev(tp) == ASIC_REV_5714) {
  14364. /* 5714 always in PCIX mode */
  14365. tp->dma_rwctrl |= 0x00148000;
  14366. } else {
  14367. tp->dma_rwctrl |= 0x001b000f;
  14368. }
  14369. }
  14370. if (tg3_flag(tp, ONE_DMA_AT_ONCE))
  14371. tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
  14372. if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
  14373. tg3_asic_rev(tp) == ASIC_REV_5704)
  14374. tp->dma_rwctrl &= 0xfffffff0;
  14375. if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
  14376. tg3_asic_rev(tp) == ASIC_REV_5701) {
  14377. /* Remove this if it causes problems for some boards. */
  14378. tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
  14379. /* On 5700/5701 chips, we need to set this bit.
  14380. * Otherwise the chip will issue cacheline transactions
  14381. * to streamable DMA memory with not all the byte
  14382. * enables turned on. This is an error on several
  14383. * RISC PCI controllers, in particular sparc64.
  14384. *
  14385. * On 5703/5704 chips, this bit has been reassigned
  14386. * a different meaning. In particular, it is used
  14387. * on those chips to enable a PCI-X workaround.
  14388. */
  14389. tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
  14390. }
  14391. tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
  14392. if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
  14393. tg3_asic_rev(tp) != ASIC_REV_5701)
  14394. goto out;
  14395. /* It is best to perform DMA test with maximum write burst size
  14396. * to expose the 5700/5701 write DMA bug.
  14397. */
  14398. saved_dma_rwctrl = tp->dma_rwctrl;
  14399. tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
  14400. tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
  14401. while (1) {
  14402. u32 *p = buf, i;
  14403. for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
  14404. p[i] = i;
  14405. /* Send the buffer to the chip. */
  14406. ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, true);
  14407. if (ret) {
  14408. dev_err(&tp->pdev->dev,
  14409. "%s: Buffer write failed. err = %d\n",
  14410. __func__, ret);
  14411. break;
  14412. }
  14413. /* Now read it back. */
  14414. ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, false);
  14415. if (ret) {
  14416. dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
  14417. "err = %d\n", __func__, ret);
  14418. break;
  14419. }
  14420. /* Verify it. */
  14421. for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
  14422. if (p[i] == i)
  14423. continue;
  14424. if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
  14425. DMA_RWCTRL_WRITE_BNDRY_16) {
  14426. tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
  14427. tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
  14428. tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
  14429. break;
  14430. } else {
  14431. dev_err(&tp->pdev->dev,
  14432. "%s: Buffer corrupted on read back! "
  14433. "(%d != %d)\n", __func__, p[i], i);
  14434. ret = -ENODEV;
  14435. goto out;
  14436. }
  14437. }
  14438. if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
  14439. /* Success. */
  14440. ret = 0;
  14441. break;
  14442. }
  14443. }
  14444. if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
  14445. DMA_RWCTRL_WRITE_BNDRY_16) {
  14446. /* DMA test passed without adjusting DMA boundary,
  14447. * now look for chipsets that are known to expose the
  14448. * DMA bug without failing the test.
  14449. */
  14450. if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
  14451. tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
  14452. tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
  14453. } else {
  14454. /* Safe to use the calculated DMA boundary. */
  14455. tp->dma_rwctrl = saved_dma_rwctrl;
  14456. }
  14457. tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
  14458. }
  14459. out:
  14460. dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
  14461. out_nofree:
  14462. return ret;
  14463. }
  14464. static void tg3_init_bufmgr_config(struct tg3 *tp)
  14465. {
  14466. if (tg3_flag(tp, 57765_PLUS)) {
  14467. tp->bufmgr_config.mbuf_read_dma_low_water =
  14468. DEFAULT_MB_RDMA_LOW_WATER_5705;
  14469. tp->bufmgr_config.mbuf_mac_rx_low_water =
  14470. DEFAULT_MB_MACRX_LOW_WATER_57765;
  14471. tp->bufmgr_config.mbuf_high_water =
  14472. DEFAULT_MB_HIGH_WATER_57765;
  14473. tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
  14474. DEFAULT_MB_RDMA_LOW_WATER_5705;
  14475. tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
  14476. DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
  14477. tp->bufmgr_config.mbuf_high_water_jumbo =
  14478. DEFAULT_MB_HIGH_WATER_JUMBO_57765;
  14479. } else if (tg3_flag(tp, 5705_PLUS)) {
  14480. tp->bufmgr_config.mbuf_read_dma_low_water =
  14481. DEFAULT_MB_RDMA_LOW_WATER_5705;
  14482. tp->bufmgr_config.mbuf_mac_rx_low_water =
  14483. DEFAULT_MB_MACRX_LOW_WATER_5705;
  14484. tp->bufmgr_config.mbuf_high_water =
  14485. DEFAULT_MB_HIGH_WATER_5705;
  14486. if (tg3_asic_rev(tp) == ASIC_REV_5906) {
  14487. tp->bufmgr_config.mbuf_mac_rx_low_water =
  14488. DEFAULT_MB_MACRX_LOW_WATER_5906;
  14489. tp->bufmgr_config.mbuf_high_water =
  14490. DEFAULT_MB_HIGH_WATER_5906;
  14491. }
  14492. tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
  14493. DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
  14494. tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
  14495. DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
  14496. tp->bufmgr_config.mbuf_high_water_jumbo =
  14497. DEFAULT_MB_HIGH_WATER_JUMBO_5780;
  14498. } else {
  14499. tp->bufmgr_config.mbuf_read_dma_low_water =
  14500. DEFAULT_MB_RDMA_LOW_WATER;
  14501. tp->bufmgr_config.mbuf_mac_rx_low_water =
  14502. DEFAULT_MB_MACRX_LOW_WATER;
  14503. tp->bufmgr_config.mbuf_high_water =
  14504. DEFAULT_MB_HIGH_WATER;
  14505. tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
  14506. DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
  14507. tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
  14508. DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
  14509. tp->bufmgr_config.mbuf_high_water_jumbo =
  14510. DEFAULT_MB_HIGH_WATER_JUMBO;
  14511. }
  14512. tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
  14513. tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
  14514. }
  14515. static char *tg3_phy_string(struct tg3 *tp)
  14516. {
  14517. switch (tp->phy_id & TG3_PHY_ID_MASK) {
  14518. case TG3_PHY_ID_BCM5400: return "5400";
  14519. case TG3_PHY_ID_BCM5401: return "5401";
  14520. case TG3_PHY_ID_BCM5411: return "5411";
  14521. case TG3_PHY_ID_BCM5701: return "5701";
  14522. case TG3_PHY_ID_BCM5703: return "5703";
  14523. case TG3_PHY_ID_BCM5704: return "5704";
  14524. case TG3_PHY_ID_BCM5705: return "5705";
  14525. case TG3_PHY_ID_BCM5750: return "5750";
  14526. case TG3_PHY_ID_BCM5752: return "5752";
  14527. case TG3_PHY_ID_BCM5714: return "5714";
  14528. case TG3_PHY_ID_BCM5780: return "5780";
  14529. case TG3_PHY_ID_BCM5755: return "5755";
  14530. case TG3_PHY_ID_BCM5787: return "5787";
  14531. case TG3_PHY_ID_BCM5784: return "5784";
  14532. case TG3_PHY_ID_BCM5756: return "5722/5756";
  14533. case TG3_PHY_ID_BCM5906: return "5906";
  14534. case TG3_PHY_ID_BCM5761: return "5761";
  14535. case TG3_PHY_ID_BCM5718C: return "5718C";
  14536. case TG3_PHY_ID_BCM5718S: return "5718S";
  14537. case TG3_PHY_ID_BCM57765: return "57765";
  14538. case TG3_PHY_ID_BCM5719C: return "5719C";
  14539. case TG3_PHY_ID_BCM5720C: return "5720C";
  14540. case TG3_PHY_ID_BCM5762: return "5762C";
  14541. case TG3_PHY_ID_BCM8002: return "8002/serdes";
  14542. case 0: return "serdes";
  14543. default: return "unknown";
  14544. }
  14545. }
  14546. static char *tg3_bus_string(struct tg3 *tp, char *str)
  14547. {
  14548. if (tg3_flag(tp, PCI_EXPRESS)) {
  14549. strcpy(str, "PCI Express");
  14550. return str;
  14551. } else if (tg3_flag(tp, PCIX_MODE)) {
  14552. u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
  14553. strcpy(str, "PCIX:");
  14554. if ((clock_ctrl == 7) ||
  14555. ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
  14556. GRC_MISC_CFG_BOARD_ID_5704CIOBE))
  14557. strcat(str, "133MHz");
  14558. else if (clock_ctrl == 0)
  14559. strcat(str, "33MHz");
  14560. else if (clock_ctrl == 2)
  14561. strcat(str, "50MHz");
  14562. else if (clock_ctrl == 4)
  14563. strcat(str, "66MHz");
  14564. else if (clock_ctrl == 6)
  14565. strcat(str, "100MHz");
  14566. } else {
  14567. strcpy(str, "PCI:");
  14568. if (tg3_flag(tp, PCI_HIGH_SPEED))
  14569. strcat(str, "66MHz");
  14570. else
  14571. strcat(str, "33MHz");
  14572. }
  14573. if (tg3_flag(tp, PCI_32BIT))
  14574. strcat(str, ":32-bit");
  14575. else
  14576. strcat(str, ":64-bit");
  14577. return str;
  14578. }
  14579. static void tg3_init_coal(struct tg3 *tp)
  14580. {
  14581. struct ethtool_coalesce *ec = &tp->coal;
  14582. memset(ec, 0, sizeof(*ec));
  14583. ec->cmd = ETHTOOL_GCOALESCE;
  14584. ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
  14585. ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
  14586. ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
  14587. ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
  14588. ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
  14589. ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
  14590. ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
  14591. ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
  14592. ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
  14593. if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
  14594. HOSTCC_MODE_CLRTICK_TXBD)) {
  14595. ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
  14596. ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
  14597. ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
  14598. ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
  14599. }
  14600. if (tg3_flag(tp, 5705_PLUS)) {
  14601. ec->rx_coalesce_usecs_irq = 0;
  14602. ec->tx_coalesce_usecs_irq = 0;
  14603. ec->stats_block_coalesce_usecs = 0;
  14604. }
  14605. }
  14606. static int tg3_init_one(struct pci_dev *pdev,
  14607. const struct pci_device_id *ent)
  14608. {
  14609. struct net_device *dev;
  14610. struct tg3 *tp;
  14611. int i, err;
  14612. u32 sndmbx, rcvmbx, intmbx;
  14613. char str[40];
  14614. u64 dma_mask, persist_dma_mask;
  14615. netdev_features_t features = 0;
  14616. printk_once(KERN_INFO "%s\n", version);
  14617. err = pci_enable_device(pdev);
  14618. if (err) {
  14619. dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
  14620. return err;
  14621. }
  14622. err = pci_request_regions(pdev, DRV_MODULE_NAME);
  14623. if (err) {
  14624. dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
  14625. goto err_out_disable_pdev;
  14626. }
  14627. pci_set_master(pdev);
  14628. dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
  14629. if (!dev) {
  14630. err = -ENOMEM;
  14631. goto err_out_free_res;
  14632. }
  14633. SET_NETDEV_DEV(dev, &pdev->dev);
  14634. tp = netdev_priv(dev);
  14635. tp->pdev = pdev;
  14636. tp->dev = dev;
  14637. tp->rx_mode = TG3_DEF_RX_MODE;
  14638. tp->tx_mode = TG3_DEF_TX_MODE;
  14639. tp->irq_sync = 1;
  14640. tp->pcierr_recovery = false;
  14641. if (tg3_debug > 0)
  14642. tp->msg_enable = tg3_debug;
  14643. else
  14644. tp->msg_enable = TG3_DEF_MSG_ENABLE;
  14645. if (pdev_is_ssb_gige_core(pdev)) {
  14646. tg3_flag_set(tp, IS_SSB_CORE);
  14647. if (ssb_gige_must_flush_posted_writes(pdev))
  14648. tg3_flag_set(tp, FLUSH_POSTED_WRITES);
  14649. if (ssb_gige_one_dma_at_once(pdev))
  14650. tg3_flag_set(tp, ONE_DMA_AT_ONCE);
  14651. if (ssb_gige_have_roboswitch(pdev)) {
  14652. tg3_flag_set(tp, USE_PHYLIB);
  14653. tg3_flag_set(tp, ROBOSWITCH);
  14654. }
  14655. if (ssb_gige_is_rgmii(pdev))
  14656. tg3_flag_set(tp, RGMII_MODE);
  14657. }
  14658. /* The word/byte swap controls here control register access byte
  14659. * swapping. DMA data byte swapping is controlled in the GRC_MODE
  14660. * setting below.
  14661. */
  14662. tp->misc_host_ctrl =
  14663. MISC_HOST_CTRL_MASK_PCI_INT |
  14664. MISC_HOST_CTRL_WORD_SWAP |
  14665. MISC_HOST_CTRL_INDIR_ACCESS |
  14666. MISC_HOST_CTRL_PCISTATE_RW;
  14667. /* The NONFRM (non-frame) byte/word swap controls take effect
  14668. * on descriptor entries, anything which isn't packet data.
  14669. *
  14670. * The StrongARM chips on the board (one for tx, one for rx)
  14671. * are running in big-endian mode.
  14672. */
  14673. tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
  14674. GRC_MODE_WSWAP_NONFRM_DATA);
  14675. #ifdef __BIG_ENDIAN
  14676. tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
  14677. #endif
  14678. spin_lock_init(&tp->lock);
  14679. spin_lock_init(&tp->indirect_lock);
  14680. INIT_WORK(&tp->reset_task, tg3_reset_task);
  14681. tp->regs = pci_ioremap_bar(pdev, BAR_0);
  14682. if (!tp->regs) {
  14683. dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
  14684. err = -ENOMEM;
  14685. goto err_out_free_dev;
  14686. }
  14687. if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
  14688. tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761E ||
  14689. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S ||
  14690. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761SE ||
  14691. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
  14692. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C ||
  14693. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
  14694. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
  14695. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720 ||
  14696. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57767 ||
  14697. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57764 ||
  14698. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5762 ||
  14699. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725 ||
  14700. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5727 ||
  14701. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57787) {
  14702. tg3_flag_set(tp, ENABLE_APE);
  14703. tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
  14704. if (!tp->aperegs) {
  14705. dev_err(&pdev->dev,
  14706. "Cannot map APE registers, aborting\n");
  14707. err = -ENOMEM;
  14708. goto err_out_iounmap;
  14709. }
  14710. }
  14711. tp->rx_pending = TG3_DEF_RX_RING_PENDING;
  14712. tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
  14713. dev->ethtool_ops = &tg3_ethtool_ops;
  14714. dev->watchdog_timeo = TG3_TX_TIMEOUT;
  14715. dev->netdev_ops = &tg3_netdev_ops;
  14716. dev->irq = pdev->irq;
  14717. err = tg3_get_invariants(tp, ent);
  14718. if (err) {
  14719. dev_err(&pdev->dev,
  14720. "Problem fetching invariants of chip, aborting\n");
  14721. goto err_out_apeunmap;
  14722. }
  14723. /* The EPB bridge inside 5714, 5715, and 5780 and any
  14724. * device behind the EPB cannot support DMA addresses > 40-bit.
  14725. * On 64-bit systems with IOMMU, use 40-bit dma_mask.
  14726. * On 64-bit systems without IOMMU, use 64-bit dma_mask and
  14727. * do DMA address check in tg3_start_xmit().
  14728. */
  14729. if (tg3_flag(tp, IS_5788))
  14730. persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
  14731. else if (tg3_flag(tp, 40BIT_DMA_BUG)) {
  14732. persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
  14733. #ifdef CONFIG_HIGHMEM
  14734. dma_mask = DMA_BIT_MASK(64);
  14735. #endif
  14736. } else
  14737. persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
  14738. /* Configure DMA attributes. */
  14739. if (dma_mask > DMA_BIT_MASK(32)) {
  14740. err = pci_set_dma_mask(pdev, dma_mask);
  14741. if (!err) {
  14742. features |= NETIF_F_HIGHDMA;
  14743. err = pci_set_consistent_dma_mask(pdev,
  14744. persist_dma_mask);
  14745. if (err < 0) {
  14746. dev_err(&pdev->dev, "Unable to obtain 64 bit "
  14747. "DMA for consistent allocations\n");
  14748. goto err_out_apeunmap;
  14749. }
  14750. }
  14751. }
  14752. if (err || dma_mask == DMA_BIT_MASK(32)) {
  14753. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  14754. if (err) {
  14755. dev_err(&pdev->dev,
  14756. "No usable DMA configuration, aborting\n");
  14757. goto err_out_apeunmap;
  14758. }
  14759. }
  14760. tg3_init_bufmgr_config(tp);
  14761. /* 5700 B0 chips do not support checksumming correctly due
  14762. * to hardware bugs.
  14763. */
  14764. if (tg3_chip_rev_id(tp) != CHIPREV_ID_5700_B0) {
  14765. features |= NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
  14766. if (tg3_flag(tp, 5755_PLUS))
  14767. features |= NETIF_F_IPV6_CSUM;
  14768. }
  14769. /* TSO is on by default on chips that support hardware TSO.
  14770. * Firmware TSO on older chips gives lower performance, so it
  14771. * is off by default, but can be enabled using ethtool.
  14772. */
  14773. if ((tg3_flag(tp, HW_TSO_1) ||
  14774. tg3_flag(tp, HW_TSO_2) ||
  14775. tg3_flag(tp, HW_TSO_3)) &&
  14776. (features & NETIF_F_IP_CSUM))
  14777. features |= NETIF_F_TSO;
  14778. if (tg3_flag(tp, HW_TSO_2) || tg3_flag(tp, HW_TSO_3)) {
  14779. if (features & NETIF_F_IPV6_CSUM)
  14780. features |= NETIF_F_TSO6;
  14781. if (tg3_flag(tp, HW_TSO_3) ||
  14782. tg3_asic_rev(tp) == ASIC_REV_5761 ||
  14783. (tg3_asic_rev(tp) == ASIC_REV_5784 &&
  14784. tg3_chip_rev(tp) != CHIPREV_5784_AX) ||
  14785. tg3_asic_rev(tp) == ASIC_REV_5785 ||
  14786. tg3_asic_rev(tp) == ASIC_REV_57780)
  14787. features |= NETIF_F_TSO_ECN;
  14788. }
  14789. dev->features |= features | NETIF_F_HW_VLAN_CTAG_TX |
  14790. NETIF_F_HW_VLAN_CTAG_RX;
  14791. dev->vlan_features |= features;
  14792. /*
  14793. * Add loopback capability only for a subset of devices that support
  14794. * MAC-LOOPBACK. Eventually this need to be enhanced to allow INT-PHY
  14795. * loopback for the remaining devices.
  14796. */
  14797. if (tg3_asic_rev(tp) != ASIC_REV_5780 &&
  14798. !tg3_flag(tp, CPMU_PRESENT))
  14799. /* Add the loopback capability */
  14800. features |= NETIF_F_LOOPBACK;
  14801. dev->hw_features |= features;
  14802. dev->priv_flags |= IFF_UNICAST_FLT;
  14803. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A1 &&
  14804. !tg3_flag(tp, TSO_CAPABLE) &&
  14805. !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
  14806. tg3_flag_set(tp, MAX_RXPEND_64);
  14807. tp->rx_pending = 63;
  14808. }
  14809. err = tg3_get_device_address(tp);
  14810. if (err) {
  14811. dev_err(&pdev->dev,
  14812. "Could not obtain valid ethernet address, aborting\n");
  14813. goto err_out_apeunmap;
  14814. }
  14815. intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
  14816. rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
  14817. sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
  14818. for (i = 0; i < tp->irq_max; i++) {
  14819. struct tg3_napi *tnapi = &tp->napi[i];
  14820. tnapi->tp = tp;
  14821. tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
  14822. tnapi->int_mbox = intmbx;
  14823. if (i <= 4)
  14824. intmbx += 0x8;
  14825. else
  14826. intmbx += 0x4;
  14827. tnapi->consmbox = rcvmbx;
  14828. tnapi->prodmbox = sndmbx;
  14829. if (i)
  14830. tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
  14831. else
  14832. tnapi->coal_now = HOSTCC_MODE_NOW;
  14833. if (!tg3_flag(tp, SUPPORT_MSIX))
  14834. break;
  14835. /*
  14836. * If we support MSIX, we'll be using RSS. If we're using
  14837. * RSS, the first vector only handles link interrupts and the
  14838. * remaining vectors handle rx and tx interrupts. Reuse the
  14839. * mailbox values for the next iteration. The values we setup
  14840. * above are still useful for the single vectored mode.
  14841. */
  14842. if (!i)
  14843. continue;
  14844. rcvmbx += 0x8;
  14845. if (sndmbx & 0x4)
  14846. sndmbx -= 0x4;
  14847. else
  14848. sndmbx += 0xc;
  14849. }
  14850. /*
  14851. * Reset chip in case UNDI or EFI driver did not shutdown
  14852. * DMA self test will enable WDMAC and we'll see (spurious)
  14853. * pending DMA on the PCI bus at that point.
  14854. */
  14855. if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
  14856. (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
  14857. tg3_full_lock(tp, 0);
  14858. tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
  14859. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  14860. tg3_full_unlock(tp);
  14861. }
  14862. err = tg3_test_dma(tp);
  14863. if (err) {
  14864. dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
  14865. goto err_out_apeunmap;
  14866. }
  14867. tg3_init_coal(tp);
  14868. pci_set_drvdata(pdev, dev);
  14869. if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
  14870. tg3_asic_rev(tp) == ASIC_REV_5720 ||
  14871. tg3_asic_rev(tp) == ASIC_REV_5762)
  14872. tg3_flag_set(tp, PTP_CAPABLE);
  14873. tg3_timer_init(tp);
  14874. tg3_carrier_off(tp);
  14875. err = register_netdev(dev);
  14876. if (err) {
  14877. dev_err(&pdev->dev, "Cannot register net device, aborting\n");
  14878. goto err_out_apeunmap;
  14879. }
  14880. if (tg3_flag(tp, PTP_CAPABLE)) {
  14881. tg3_ptp_init(tp);
  14882. tp->ptp_clock = ptp_clock_register(&tp->ptp_info,
  14883. &tp->pdev->dev);
  14884. if (IS_ERR(tp->ptp_clock))
  14885. tp->ptp_clock = NULL;
  14886. }
  14887. netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
  14888. tp->board_part_number,
  14889. tg3_chip_rev_id(tp),
  14890. tg3_bus_string(tp, str),
  14891. dev->dev_addr);
  14892. if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)) {
  14893. char *ethtype;
  14894. if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
  14895. ethtype = "10/100Base-TX";
  14896. else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
  14897. ethtype = "1000Base-SX";
  14898. else
  14899. ethtype = "10/100/1000Base-T";
  14900. netdev_info(dev, "attached PHY is %s (%s Ethernet) "
  14901. "(WireSpeed[%d], EEE[%d])\n",
  14902. tg3_phy_string(tp), ethtype,
  14903. (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0,
  14904. (tp->phy_flags & TG3_PHYFLG_EEE_CAP) != 0);
  14905. }
  14906. netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
  14907. (dev->features & NETIF_F_RXCSUM) != 0,
  14908. tg3_flag(tp, USE_LINKCHG_REG) != 0,
  14909. (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
  14910. tg3_flag(tp, ENABLE_ASF) != 0,
  14911. tg3_flag(tp, TSO_CAPABLE) != 0);
  14912. netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
  14913. tp->dma_rwctrl,
  14914. pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
  14915. ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
  14916. pci_save_state(pdev);
  14917. return 0;
  14918. err_out_apeunmap:
  14919. if (tp->aperegs) {
  14920. iounmap(tp->aperegs);
  14921. tp->aperegs = NULL;
  14922. }
  14923. err_out_iounmap:
  14924. if (tp->regs) {
  14925. iounmap(tp->regs);
  14926. tp->regs = NULL;
  14927. }
  14928. err_out_free_dev:
  14929. free_netdev(dev);
  14930. err_out_free_res:
  14931. pci_release_regions(pdev);
  14932. err_out_disable_pdev:
  14933. if (pci_is_enabled(pdev))
  14934. pci_disable_device(pdev);
  14935. return err;
  14936. }
  14937. static void tg3_remove_one(struct pci_dev *pdev)
  14938. {
  14939. struct net_device *dev = pci_get_drvdata(pdev);
  14940. if (dev) {
  14941. struct tg3 *tp = netdev_priv(dev);
  14942. tg3_ptp_fini(tp);
  14943. release_firmware(tp->fw);
  14944. tg3_reset_task_cancel(tp);
  14945. if (tg3_flag(tp, USE_PHYLIB)) {
  14946. tg3_phy_fini(tp);
  14947. tg3_mdio_fini(tp);
  14948. }
  14949. unregister_netdev(dev);
  14950. if (tp->aperegs) {
  14951. iounmap(tp->aperegs);
  14952. tp->aperegs = NULL;
  14953. }
  14954. if (tp->regs) {
  14955. iounmap(tp->regs);
  14956. tp->regs = NULL;
  14957. }
  14958. free_netdev(dev);
  14959. pci_release_regions(pdev);
  14960. pci_disable_device(pdev);
  14961. }
  14962. }
  14963. #ifdef CONFIG_PM_SLEEP
  14964. static int tg3_suspend(struct device *device)
  14965. {
  14966. struct pci_dev *pdev = to_pci_dev(device);
  14967. struct net_device *dev = pci_get_drvdata(pdev);
  14968. struct tg3 *tp = netdev_priv(dev);
  14969. int err = 0;
  14970. rtnl_lock();
  14971. if (!netif_running(dev))
  14972. goto unlock;
  14973. tg3_reset_task_cancel(tp);
  14974. tg3_phy_stop(tp);
  14975. tg3_netif_stop(tp);
  14976. tg3_timer_stop(tp);
  14977. tg3_full_lock(tp, 1);
  14978. tg3_disable_ints(tp);
  14979. tg3_full_unlock(tp);
  14980. netif_device_detach(dev);
  14981. tg3_full_lock(tp, 0);
  14982. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  14983. tg3_flag_clear(tp, INIT_COMPLETE);
  14984. tg3_full_unlock(tp);
  14985. err = tg3_power_down_prepare(tp);
  14986. if (err) {
  14987. int err2;
  14988. tg3_full_lock(tp, 0);
  14989. tg3_flag_set(tp, INIT_COMPLETE);
  14990. err2 = tg3_restart_hw(tp, true);
  14991. if (err2)
  14992. goto out;
  14993. tg3_timer_start(tp);
  14994. netif_device_attach(dev);
  14995. tg3_netif_start(tp);
  14996. out:
  14997. tg3_full_unlock(tp);
  14998. if (!err2)
  14999. tg3_phy_start(tp);
  15000. }
  15001. unlock:
  15002. rtnl_unlock();
  15003. return err;
  15004. }
  15005. static int tg3_resume(struct device *device)
  15006. {
  15007. struct pci_dev *pdev = to_pci_dev(device);
  15008. struct net_device *dev = pci_get_drvdata(pdev);
  15009. struct tg3 *tp = netdev_priv(dev);
  15010. int err = 0;
  15011. rtnl_lock();
  15012. if (!netif_running(dev))
  15013. goto unlock;
  15014. netif_device_attach(dev);
  15015. tg3_full_lock(tp, 0);
  15016. tg3_ape_driver_state_change(tp, RESET_KIND_INIT);
  15017. tg3_flag_set(tp, INIT_COMPLETE);
  15018. err = tg3_restart_hw(tp,
  15019. !(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN));
  15020. if (err)
  15021. goto out;
  15022. tg3_timer_start(tp);
  15023. tg3_netif_start(tp);
  15024. out:
  15025. tg3_full_unlock(tp);
  15026. if (!err)
  15027. tg3_phy_start(tp);
  15028. unlock:
  15029. rtnl_unlock();
  15030. return err;
  15031. }
  15032. #endif /* CONFIG_PM_SLEEP */
  15033. static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
  15034. static void tg3_shutdown(struct pci_dev *pdev)
  15035. {
  15036. struct net_device *dev = pci_get_drvdata(pdev);
  15037. struct tg3 *tp = netdev_priv(dev);
  15038. rtnl_lock();
  15039. netif_device_detach(dev);
  15040. if (netif_running(dev))
  15041. dev_close(dev);
  15042. if (system_state == SYSTEM_POWER_OFF)
  15043. tg3_power_down(tp);
  15044. rtnl_unlock();
  15045. }
  15046. /**
  15047. * tg3_io_error_detected - called when PCI error is detected
  15048. * @pdev: Pointer to PCI device
  15049. * @state: The current pci connection state
  15050. *
  15051. * This function is called after a PCI bus error affecting
  15052. * this device has been detected.
  15053. */
  15054. static pci_ers_result_t tg3_io_error_detected(struct pci_dev *pdev,
  15055. pci_channel_state_t state)
  15056. {
  15057. struct net_device *netdev = pci_get_drvdata(pdev);
  15058. struct tg3 *tp = netdev_priv(netdev);
  15059. pci_ers_result_t err = PCI_ERS_RESULT_NEED_RESET;
  15060. netdev_info(netdev, "PCI I/O error detected\n");
  15061. rtnl_lock();
  15062. /* We probably don't have netdev yet */
  15063. if (!netdev || !netif_running(netdev))
  15064. goto done;
  15065. /* We needn't recover from permanent error */
  15066. if (state == pci_channel_io_frozen)
  15067. tp->pcierr_recovery = true;
  15068. tg3_phy_stop(tp);
  15069. tg3_netif_stop(tp);
  15070. tg3_timer_stop(tp);
  15071. /* Want to make sure that the reset task doesn't run */
  15072. tg3_reset_task_cancel(tp);
  15073. netif_device_detach(netdev);
  15074. /* Clean up software state, even if MMIO is blocked */
  15075. tg3_full_lock(tp, 0);
  15076. tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
  15077. tg3_full_unlock(tp);
  15078. done:
  15079. if (state == pci_channel_io_perm_failure) {
  15080. if (netdev) {
  15081. tg3_napi_enable(tp);
  15082. dev_close(netdev);
  15083. }
  15084. err = PCI_ERS_RESULT_DISCONNECT;
  15085. } else {
  15086. pci_disable_device(pdev);
  15087. }
  15088. rtnl_unlock();
  15089. return err;
  15090. }
  15091. /**
  15092. * tg3_io_slot_reset - called after the pci bus has been reset.
  15093. * @pdev: Pointer to PCI device
  15094. *
  15095. * Restart the card from scratch, as if from a cold-boot.
  15096. * At this point, the card has exprienced a hard reset,
  15097. * followed by fixups by BIOS, and has its config space
  15098. * set up identically to what it was at cold boot.
  15099. */
  15100. static pci_ers_result_t tg3_io_slot_reset(struct pci_dev *pdev)
  15101. {
  15102. struct net_device *netdev = pci_get_drvdata(pdev);
  15103. struct tg3 *tp = netdev_priv(netdev);
  15104. pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
  15105. int err;
  15106. rtnl_lock();
  15107. if (pci_enable_device(pdev)) {
  15108. dev_err(&pdev->dev,
  15109. "Cannot re-enable PCI device after reset.\n");
  15110. goto done;
  15111. }
  15112. pci_set_master(pdev);
  15113. pci_restore_state(pdev);
  15114. pci_save_state(pdev);
  15115. if (!netdev || !netif_running(netdev)) {
  15116. rc = PCI_ERS_RESULT_RECOVERED;
  15117. goto done;
  15118. }
  15119. err = tg3_power_up(tp);
  15120. if (err)
  15121. goto done;
  15122. rc = PCI_ERS_RESULT_RECOVERED;
  15123. done:
  15124. if (rc != PCI_ERS_RESULT_RECOVERED && netdev && netif_running(netdev)) {
  15125. tg3_napi_enable(tp);
  15126. dev_close(netdev);
  15127. }
  15128. rtnl_unlock();
  15129. return rc;
  15130. }
  15131. /**
  15132. * tg3_io_resume - called when traffic can start flowing again.
  15133. * @pdev: Pointer to PCI device
  15134. *
  15135. * This callback is called when the error recovery driver tells
  15136. * us that its OK to resume normal operation.
  15137. */
  15138. static void tg3_io_resume(struct pci_dev *pdev)
  15139. {
  15140. struct net_device *netdev = pci_get_drvdata(pdev);
  15141. struct tg3 *tp = netdev_priv(netdev);
  15142. int err;
  15143. rtnl_lock();
  15144. if (!netdev || !netif_running(netdev))
  15145. goto done;
  15146. tg3_full_lock(tp, 0);
  15147. tg3_ape_driver_state_change(tp, RESET_KIND_INIT);
  15148. tg3_flag_set(tp, INIT_COMPLETE);
  15149. err = tg3_restart_hw(tp, true);
  15150. if (err) {
  15151. tg3_full_unlock(tp);
  15152. netdev_err(netdev, "Cannot restart hardware after reset.\n");
  15153. goto done;
  15154. }
  15155. netif_device_attach(netdev);
  15156. tg3_timer_start(tp);
  15157. tg3_netif_start(tp);
  15158. tg3_full_unlock(tp);
  15159. tg3_phy_start(tp);
  15160. done:
  15161. tp->pcierr_recovery = false;
  15162. rtnl_unlock();
  15163. }
  15164. static const struct pci_error_handlers tg3_err_handler = {
  15165. .error_detected = tg3_io_error_detected,
  15166. .slot_reset = tg3_io_slot_reset,
  15167. .resume = tg3_io_resume
  15168. };
  15169. static struct pci_driver tg3_driver = {
  15170. .name = DRV_MODULE_NAME,
  15171. .id_table = tg3_pci_tbl,
  15172. .probe = tg3_init_one,
  15173. .remove = tg3_remove_one,
  15174. .err_handler = &tg3_err_handler,
  15175. .driver.pm = &tg3_pm_ops,
  15176. .shutdown = tg3_shutdown,
  15177. };
  15178. module_pci_driver(tg3_driver);