tg3.c 465 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272
  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 = tp->mdio_bus->phy_map[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. tp->mdio_bus->irq = &tp->mdio_irq[0];
  1270. for (i = 0; i < PHY_MAX_ADDR; i++)
  1271. tp->mdio_bus->irq[i] = PHY_POLL;
  1272. /* The bus registration will look for all the PHYs on the mdio bus.
  1273. * Unfortunately, it does not ensure the PHY is powered up before
  1274. * accessing the PHY ID registers. A chip reset is the
  1275. * quickest way to bring the device back to an operational state..
  1276. */
  1277. if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
  1278. tg3_bmcr_reset(tp);
  1279. i = mdiobus_register(tp->mdio_bus);
  1280. if (i) {
  1281. dev_warn(&tp->pdev->dev, "mdiobus_reg failed (0x%x)\n", i);
  1282. mdiobus_free(tp->mdio_bus);
  1283. return i;
  1284. }
  1285. phydev = tp->mdio_bus->phy_map[tp->phy_addr];
  1286. if (!phydev || !phydev->drv) {
  1287. dev_warn(&tp->pdev->dev, "No PHY devices\n");
  1288. mdiobus_unregister(tp->mdio_bus);
  1289. mdiobus_free(tp->mdio_bus);
  1290. return -ENODEV;
  1291. }
  1292. switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
  1293. case PHY_ID_BCM57780:
  1294. phydev->interface = PHY_INTERFACE_MODE_GMII;
  1295. phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
  1296. break;
  1297. case PHY_ID_BCM50610:
  1298. case PHY_ID_BCM50610M:
  1299. phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
  1300. PHY_BRCM_RX_REFCLK_UNUSED |
  1301. PHY_BRCM_DIS_TXCRXC_NOENRGY |
  1302. PHY_BRCM_AUTO_PWRDWN_ENABLE;
  1303. if (tg3_flag(tp, RGMII_INBAND_DISABLE))
  1304. phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
  1305. if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
  1306. phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
  1307. if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
  1308. phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
  1309. /* fallthru */
  1310. case PHY_ID_RTL8211C:
  1311. phydev->interface = PHY_INTERFACE_MODE_RGMII;
  1312. break;
  1313. case PHY_ID_RTL8201E:
  1314. case PHY_ID_BCMAC131:
  1315. phydev->interface = PHY_INTERFACE_MODE_MII;
  1316. phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
  1317. tp->phy_flags |= TG3_PHYFLG_IS_FET;
  1318. break;
  1319. }
  1320. tg3_flag_set(tp, MDIOBUS_INITED);
  1321. if (tg3_asic_rev(tp) == ASIC_REV_5785)
  1322. tg3_mdio_config_5785(tp);
  1323. return 0;
  1324. }
  1325. static void tg3_mdio_fini(struct tg3 *tp)
  1326. {
  1327. if (tg3_flag(tp, MDIOBUS_INITED)) {
  1328. tg3_flag_clear(tp, MDIOBUS_INITED);
  1329. mdiobus_unregister(tp->mdio_bus);
  1330. mdiobus_free(tp->mdio_bus);
  1331. }
  1332. }
  1333. /* tp->lock is held. */
  1334. static inline void tg3_generate_fw_event(struct tg3 *tp)
  1335. {
  1336. u32 val;
  1337. val = tr32(GRC_RX_CPU_EVENT);
  1338. val |= GRC_RX_CPU_DRIVER_EVENT;
  1339. tw32_f(GRC_RX_CPU_EVENT, val);
  1340. tp->last_event_jiffies = jiffies;
  1341. }
  1342. #define TG3_FW_EVENT_TIMEOUT_USEC 2500
  1343. /* tp->lock is held. */
  1344. static void tg3_wait_for_event_ack(struct tg3 *tp)
  1345. {
  1346. int i;
  1347. unsigned int delay_cnt;
  1348. long time_remain;
  1349. /* If enough time has passed, no wait is necessary. */
  1350. time_remain = (long)(tp->last_event_jiffies + 1 +
  1351. usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
  1352. (long)jiffies;
  1353. if (time_remain < 0)
  1354. return;
  1355. /* Check if we can shorten the wait time. */
  1356. delay_cnt = jiffies_to_usecs(time_remain);
  1357. if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
  1358. delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
  1359. delay_cnt = (delay_cnt >> 3) + 1;
  1360. for (i = 0; i < delay_cnt; i++) {
  1361. if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
  1362. break;
  1363. if (pci_channel_offline(tp->pdev))
  1364. break;
  1365. udelay(8);
  1366. }
  1367. }
  1368. /* tp->lock is held. */
  1369. static void tg3_phy_gather_ump_data(struct tg3 *tp, u32 *data)
  1370. {
  1371. u32 reg, val;
  1372. val = 0;
  1373. if (!tg3_readphy(tp, MII_BMCR, &reg))
  1374. val = reg << 16;
  1375. if (!tg3_readphy(tp, MII_BMSR, &reg))
  1376. val |= (reg & 0xffff);
  1377. *data++ = val;
  1378. val = 0;
  1379. if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
  1380. val = reg << 16;
  1381. if (!tg3_readphy(tp, MII_LPA, &reg))
  1382. val |= (reg & 0xffff);
  1383. *data++ = val;
  1384. val = 0;
  1385. if (!(tp->phy_flags & TG3_PHYFLG_MII_SERDES)) {
  1386. if (!tg3_readphy(tp, MII_CTRL1000, &reg))
  1387. val = reg << 16;
  1388. if (!tg3_readphy(tp, MII_STAT1000, &reg))
  1389. val |= (reg & 0xffff);
  1390. }
  1391. *data++ = val;
  1392. if (!tg3_readphy(tp, MII_PHYADDR, &reg))
  1393. val = reg << 16;
  1394. else
  1395. val = 0;
  1396. *data++ = val;
  1397. }
  1398. /* tp->lock is held. */
  1399. static void tg3_ump_link_report(struct tg3 *tp)
  1400. {
  1401. u32 data[4];
  1402. if (!tg3_flag(tp, 5780_CLASS) || !tg3_flag(tp, ENABLE_ASF))
  1403. return;
  1404. tg3_phy_gather_ump_data(tp, data);
  1405. tg3_wait_for_event_ack(tp);
  1406. tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
  1407. tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
  1408. tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x0, data[0]);
  1409. tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x4, data[1]);
  1410. tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x8, data[2]);
  1411. tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0xc, data[3]);
  1412. tg3_generate_fw_event(tp);
  1413. }
  1414. /* tp->lock is held. */
  1415. static void tg3_stop_fw(struct tg3 *tp)
  1416. {
  1417. if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
  1418. /* Wait for RX cpu to ACK the previous event. */
  1419. tg3_wait_for_event_ack(tp);
  1420. tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
  1421. tg3_generate_fw_event(tp);
  1422. /* Wait for RX cpu to ACK this event. */
  1423. tg3_wait_for_event_ack(tp);
  1424. }
  1425. }
  1426. /* tp->lock is held. */
  1427. static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
  1428. {
  1429. tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
  1430. NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
  1431. if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
  1432. switch (kind) {
  1433. case RESET_KIND_INIT:
  1434. tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
  1435. DRV_STATE_START);
  1436. break;
  1437. case RESET_KIND_SHUTDOWN:
  1438. tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
  1439. DRV_STATE_UNLOAD);
  1440. break;
  1441. case RESET_KIND_SUSPEND:
  1442. tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
  1443. DRV_STATE_SUSPEND);
  1444. break;
  1445. default:
  1446. break;
  1447. }
  1448. }
  1449. }
  1450. /* tp->lock is held. */
  1451. static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
  1452. {
  1453. if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
  1454. switch (kind) {
  1455. case RESET_KIND_INIT:
  1456. tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
  1457. DRV_STATE_START_DONE);
  1458. break;
  1459. case RESET_KIND_SHUTDOWN:
  1460. tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
  1461. DRV_STATE_UNLOAD_DONE);
  1462. break;
  1463. default:
  1464. break;
  1465. }
  1466. }
  1467. }
  1468. /* tp->lock is held. */
  1469. static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
  1470. {
  1471. if (tg3_flag(tp, ENABLE_ASF)) {
  1472. switch (kind) {
  1473. case RESET_KIND_INIT:
  1474. tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
  1475. DRV_STATE_START);
  1476. break;
  1477. case RESET_KIND_SHUTDOWN:
  1478. tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
  1479. DRV_STATE_UNLOAD);
  1480. break;
  1481. case RESET_KIND_SUSPEND:
  1482. tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
  1483. DRV_STATE_SUSPEND);
  1484. break;
  1485. default:
  1486. break;
  1487. }
  1488. }
  1489. }
  1490. static int tg3_poll_fw(struct tg3 *tp)
  1491. {
  1492. int i;
  1493. u32 val;
  1494. if (tg3_flag(tp, NO_FWARE_REPORTED))
  1495. return 0;
  1496. if (tg3_flag(tp, IS_SSB_CORE)) {
  1497. /* We don't use firmware. */
  1498. return 0;
  1499. }
  1500. if (tg3_asic_rev(tp) == ASIC_REV_5906) {
  1501. /* Wait up to 20ms for init done. */
  1502. for (i = 0; i < 200; i++) {
  1503. if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
  1504. return 0;
  1505. if (pci_channel_offline(tp->pdev))
  1506. return -ENODEV;
  1507. udelay(100);
  1508. }
  1509. return -ENODEV;
  1510. }
  1511. /* Wait for firmware initialization to complete. */
  1512. for (i = 0; i < 100000; i++) {
  1513. tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
  1514. if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
  1515. break;
  1516. if (pci_channel_offline(tp->pdev)) {
  1517. if (!tg3_flag(tp, NO_FWARE_REPORTED)) {
  1518. tg3_flag_set(tp, NO_FWARE_REPORTED);
  1519. netdev_info(tp->dev, "No firmware running\n");
  1520. }
  1521. break;
  1522. }
  1523. udelay(10);
  1524. }
  1525. /* Chip might not be fitted with firmware. Some Sun onboard
  1526. * parts are configured like that. So don't signal the timeout
  1527. * of the above loop as an error, but do report the lack of
  1528. * running firmware once.
  1529. */
  1530. if (i >= 100000 && !tg3_flag(tp, NO_FWARE_REPORTED)) {
  1531. tg3_flag_set(tp, NO_FWARE_REPORTED);
  1532. netdev_info(tp->dev, "No firmware running\n");
  1533. }
  1534. if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0) {
  1535. /* The 57765 A0 needs a little more
  1536. * time to do some important work.
  1537. */
  1538. mdelay(10);
  1539. }
  1540. return 0;
  1541. }
  1542. static void tg3_link_report(struct tg3 *tp)
  1543. {
  1544. if (!netif_carrier_ok(tp->dev)) {
  1545. netif_info(tp, link, tp->dev, "Link is down\n");
  1546. tg3_ump_link_report(tp);
  1547. } else if (netif_msg_link(tp)) {
  1548. netdev_info(tp->dev, "Link is up at %d Mbps, %s duplex\n",
  1549. (tp->link_config.active_speed == SPEED_1000 ?
  1550. 1000 :
  1551. (tp->link_config.active_speed == SPEED_100 ?
  1552. 100 : 10)),
  1553. (tp->link_config.active_duplex == DUPLEX_FULL ?
  1554. "full" : "half"));
  1555. netdev_info(tp->dev, "Flow control is %s for TX and %s for RX\n",
  1556. (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
  1557. "on" : "off",
  1558. (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
  1559. "on" : "off");
  1560. if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
  1561. netdev_info(tp->dev, "EEE is %s\n",
  1562. tp->setlpicnt ? "enabled" : "disabled");
  1563. tg3_ump_link_report(tp);
  1564. }
  1565. tp->link_up = netif_carrier_ok(tp->dev);
  1566. }
  1567. static u32 tg3_decode_flowctrl_1000T(u32 adv)
  1568. {
  1569. u32 flowctrl = 0;
  1570. if (adv & ADVERTISE_PAUSE_CAP) {
  1571. flowctrl |= FLOW_CTRL_RX;
  1572. if (!(adv & ADVERTISE_PAUSE_ASYM))
  1573. flowctrl |= FLOW_CTRL_TX;
  1574. } else if (adv & ADVERTISE_PAUSE_ASYM)
  1575. flowctrl |= FLOW_CTRL_TX;
  1576. return flowctrl;
  1577. }
  1578. static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
  1579. {
  1580. u16 miireg;
  1581. if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
  1582. miireg = ADVERTISE_1000XPAUSE;
  1583. else if (flow_ctrl & FLOW_CTRL_TX)
  1584. miireg = ADVERTISE_1000XPSE_ASYM;
  1585. else if (flow_ctrl & FLOW_CTRL_RX)
  1586. miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
  1587. else
  1588. miireg = 0;
  1589. return miireg;
  1590. }
  1591. static u32 tg3_decode_flowctrl_1000X(u32 adv)
  1592. {
  1593. u32 flowctrl = 0;
  1594. if (adv & ADVERTISE_1000XPAUSE) {
  1595. flowctrl |= FLOW_CTRL_RX;
  1596. if (!(adv & ADVERTISE_1000XPSE_ASYM))
  1597. flowctrl |= FLOW_CTRL_TX;
  1598. } else if (adv & ADVERTISE_1000XPSE_ASYM)
  1599. flowctrl |= FLOW_CTRL_TX;
  1600. return flowctrl;
  1601. }
  1602. static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
  1603. {
  1604. u8 cap = 0;
  1605. if (lcladv & rmtadv & ADVERTISE_1000XPAUSE) {
  1606. cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
  1607. } else if (lcladv & rmtadv & ADVERTISE_1000XPSE_ASYM) {
  1608. if (lcladv & ADVERTISE_1000XPAUSE)
  1609. cap = FLOW_CTRL_RX;
  1610. if (rmtadv & ADVERTISE_1000XPAUSE)
  1611. cap = FLOW_CTRL_TX;
  1612. }
  1613. return cap;
  1614. }
  1615. static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
  1616. {
  1617. u8 autoneg;
  1618. u8 flowctrl = 0;
  1619. u32 old_rx_mode = tp->rx_mode;
  1620. u32 old_tx_mode = tp->tx_mode;
  1621. if (tg3_flag(tp, USE_PHYLIB))
  1622. autoneg = tp->mdio_bus->phy_map[tp->phy_addr]->autoneg;
  1623. else
  1624. autoneg = tp->link_config.autoneg;
  1625. if (autoneg == AUTONEG_ENABLE && tg3_flag(tp, PAUSE_AUTONEG)) {
  1626. if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
  1627. flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
  1628. else
  1629. flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
  1630. } else
  1631. flowctrl = tp->link_config.flowctrl;
  1632. tp->link_config.active_flowctrl = flowctrl;
  1633. if (flowctrl & FLOW_CTRL_RX)
  1634. tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
  1635. else
  1636. tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
  1637. if (old_rx_mode != tp->rx_mode)
  1638. tw32_f(MAC_RX_MODE, tp->rx_mode);
  1639. if (flowctrl & FLOW_CTRL_TX)
  1640. tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
  1641. else
  1642. tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
  1643. if (old_tx_mode != tp->tx_mode)
  1644. tw32_f(MAC_TX_MODE, tp->tx_mode);
  1645. }
  1646. static void tg3_adjust_link(struct net_device *dev)
  1647. {
  1648. u8 oldflowctrl, linkmesg = 0;
  1649. u32 mac_mode, lcl_adv, rmt_adv;
  1650. struct tg3 *tp = netdev_priv(dev);
  1651. struct phy_device *phydev = tp->mdio_bus->phy_map[tp->phy_addr];
  1652. spin_lock_bh(&tp->lock);
  1653. mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
  1654. MAC_MODE_HALF_DUPLEX);
  1655. oldflowctrl = tp->link_config.active_flowctrl;
  1656. if (phydev->link) {
  1657. lcl_adv = 0;
  1658. rmt_adv = 0;
  1659. if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
  1660. mac_mode |= MAC_MODE_PORT_MODE_MII;
  1661. else if (phydev->speed == SPEED_1000 ||
  1662. tg3_asic_rev(tp) != ASIC_REV_5785)
  1663. mac_mode |= MAC_MODE_PORT_MODE_GMII;
  1664. else
  1665. mac_mode |= MAC_MODE_PORT_MODE_MII;
  1666. if (phydev->duplex == DUPLEX_HALF)
  1667. mac_mode |= MAC_MODE_HALF_DUPLEX;
  1668. else {
  1669. lcl_adv = mii_advertise_flowctrl(
  1670. tp->link_config.flowctrl);
  1671. if (phydev->pause)
  1672. rmt_adv = LPA_PAUSE_CAP;
  1673. if (phydev->asym_pause)
  1674. rmt_adv |= LPA_PAUSE_ASYM;
  1675. }
  1676. tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
  1677. } else
  1678. mac_mode |= MAC_MODE_PORT_MODE_GMII;
  1679. if (mac_mode != tp->mac_mode) {
  1680. tp->mac_mode = mac_mode;
  1681. tw32_f(MAC_MODE, tp->mac_mode);
  1682. udelay(40);
  1683. }
  1684. if (tg3_asic_rev(tp) == ASIC_REV_5785) {
  1685. if (phydev->speed == SPEED_10)
  1686. tw32(MAC_MI_STAT,
  1687. MAC_MI_STAT_10MBPS_MODE |
  1688. MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
  1689. else
  1690. tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
  1691. }
  1692. if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
  1693. tw32(MAC_TX_LENGTHS,
  1694. ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
  1695. (6 << TX_LENGTHS_IPG_SHIFT) |
  1696. (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
  1697. else
  1698. tw32(MAC_TX_LENGTHS,
  1699. ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
  1700. (6 << TX_LENGTHS_IPG_SHIFT) |
  1701. (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
  1702. if (phydev->link != tp->old_link ||
  1703. phydev->speed != tp->link_config.active_speed ||
  1704. phydev->duplex != tp->link_config.active_duplex ||
  1705. oldflowctrl != tp->link_config.active_flowctrl)
  1706. linkmesg = 1;
  1707. tp->old_link = phydev->link;
  1708. tp->link_config.active_speed = phydev->speed;
  1709. tp->link_config.active_duplex = phydev->duplex;
  1710. spin_unlock_bh(&tp->lock);
  1711. if (linkmesg)
  1712. tg3_link_report(tp);
  1713. }
  1714. static int tg3_phy_init(struct tg3 *tp)
  1715. {
  1716. struct phy_device *phydev;
  1717. if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)
  1718. return 0;
  1719. /* Bring the PHY back to a known state. */
  1720. tg3_bmcr_reset(tp);
  1721. phydev = tp->mdio_bus->phy_map[tp->phy_addr];
  1722. /* Attach the MAC to the PHY. */
  1723. phydev = phy_connect(tp->dev, dev_name(&phydev->dev),
  1724. tg3_adjust_link, phydev->interface);
  1725. if (IS_ERR(phydev)) {
  1726. dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
  1727. return PTR_ERR(phydev);
  1728. }
  1729. /* Mask with MAC supported features. */
  1730. switch (phydev->interface) {
  1731. case PHY_INTERFACE_MODE_GMII:
  1732. case PHY_INTERFACE_MODE_RGMII:
  1733. if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
  1734. phydev->supported &= (PHY_GBIT_FEATURES |
  1735. SUPPORTED_Pause |
  1736. SUPPORTED_Asym_Pause);
  1737. break;
  1738. }
  1739. /* fallthru */
  1740. case PHY_INTERFACE_MODE_MII:
  1741. phydev->supported &= (PHY_BASIC_FEATURES |
  1742. SUPPORTED_Pause |
  1743. SUPPORTED_Asym_Pause);
  1744. break;
  1745. default:
  1746. phy_disconnect(tp->mdio_bus->phy_map[tp->phy_addr]);
  1747. return -EINVAL;
  1748. }
  1749. tp->phy_flags |= TG3_PHYFLG_IS_CONNECTED;
  1750. phydev->advertising = phydev->supported;
  1751. return 0;
  1752. }
  1753. static void tg3_phy_start(struct tg3 *tp)
  1754. {
  1755. struct phy_device *phydev;
  1756. if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
  1757. return;
  1758. phydev = tp->mdio_bus->phy_map[tp->phy_addr];
  1759. if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
  1760. tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
  1761. phydev->speed = tp->link_config.speed;
  1762. phydev->duplex = tp->link_config.duplex;
  1763. phydev->autoneg = tp->link_config.autoneg;
  1764. phydev->advertising = tp->link_config.advertising;
  1765. }
  1766. phy_start(phydev);
  1767. phy_start_aneg(phydev);
  1768. }
  1769. static void tg3_phy_stop(struct tg3 *tp)
  1770. {
  1771. if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
  1772. return;
  1773. phy_stop(tp->mdio_bus->phy_map[tp->phy_addr]);
  1774. }
  1775. static void tg3_phy_fini(struct tg3 *tp)
  1776. {
  1777. if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
  1778. phy_disconnect(tp->mdio_bus->phy_map[tp->phy_addr]);
  1779. tp->phy_flags &= ~TG3_PHYFLG_IS_CONNECTED;
  1780. }
  1781. }
  1782. static int tg3_phy_set_extloopbk(struct tg3 *tp)
  1783. {
  1784. int err;
  1785. u32 val;
  1786. if (tp->phy_flags & TG3_PHYFLG_IS_FET)
  1787. return 0;
  1788. if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
  1789. /* Cannot do read-modify-write on 5401 */
  1790. err = tg3_phy_auxctl_write(tp,
  1791. MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
  1792. MII_TG3_AUXCTL_ACTL_EXTLOOPBK |
  1793. 0x4c20);
  1794. goto done;
  1795. }
  1796. err = tg3_phy_auxctl_read(tp,
  1797. MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
  1798. if (err)
  1799. return err;
  1800. val |= MII_TG3_AUXCTL_ACTL_EXTLOOPBK;
  1801. err = tg3_phy_auxctl_write(tp,
  1802. MII_TG3_AUXCTL_SHDWSEL_AUXCTL, val);
  1803. done:
  1804. return err;
  1805. }
  1806. static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
  1807. {
  1808. u32 phytest;
  1809. if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
  1810. u32 phy;
  1811. tg3_writephy(tp, MII_TG3_FET_TEST,
  1812. phytest | MII_TG3_FET_SHADOW_EN);
  1813. if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
  1814. if (enable)
  1815. phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
  1816. else
  1817. phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
  1818. tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
  1819. }
  1820. tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
  1821. }
  1822. }
  1823. static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
  1824. {
  1825. u32 reg;
  1826. if (!tg3_flag(tp, 5705_PLUS) ||
  1827. (tg3_flag(tp, 5717_PLUS) &&
  1828. (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
  1829. return;
  1830. if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
  1831. tg3_phy_fet_toggle_apd(tp, enable);
  1832. return;
  1833. }
  1834. reg = MII_TG3_MISC_SHDW_SCR5_LPED |
  1835. MII_TG3_MISC_SHDW_SCR5_DLPTLM |
  1836. MII_TG3_MISC_SHDW_SCR5_SDTL |
  1837. MII_TG3_MISC_SHDW_SCR5_C125OE;
  1838. if (tg3_asic_rev(tp) != ASIC_REV_5784 || !enable)
  1839. reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
  1840. tg3_phy_shdw_write(tp, MII_TG3_MISC_SHDW_SCR5_SEL, reg);
  1841. reg = MII_TG3_MISC_SHDW_APD_WKTM_84MS;
  1842. if (enable)
  1843. reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
  1844. tg3_phy_shdw_write(tp, MII_TG3_MISC_SHDW_APD_SEL, reg);
  1845. }
  1846. static void tg3_phy_toggle_automdix(struct tg3 *tp, bool enable)
  1847. {
  1848. u32 phy;
  1849. if (!tg3_flag(tp, 5705_PLUS) ||
  1850. (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
  1851. return;
  1852. if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
  1853. u32 ephy;
  1854. if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
  1855. u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
  1856. tg3_writephy(tp, MII_TG3_FET_TEST,
  1857. ephy | MII_TG3_FET_SHADOW_EN);
  1858. if (!tg3_readphy(tp, reg, &phy)) {
  1859. if (enable)
  1860. phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
  1861. else
  1862. phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
  1863. tg3_writephy(tp, reg, phy);
  1864. }
  1865. tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
  1866. }
  1867. } else {
  1868. int ret;
  1869. ret = tg3_phy_auxctl_read(tp,
  1870. MII_TG3_AUXCTL_SHDWSEL_MISC, &phy);
  1871. if (!ret) {
  1872. if (enable)
  1873. phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
  1874. else
  1875. phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
  1876. tg3_phy_auxctl_write(tp,
  1877. MII_TG3_AUXCTL_SHDWSEL_MISC, phy);
  1878. }
  1879. }
  1880. }
  1881. static void tg3_phy_set_wirespeed(struct tg3 *tp)
  1882. {
  1883. int ret;
  1884. u32 val;
  1885. if (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED)
  1886. return;
  1887. ret = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_MISC, &val);
  1888. if (!ret)
  1889. tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_MISC,
  1890. val | MII_TG3_AUXCTL_MISC_WIRESPD_EN);
  1891. }
  1892. static void tg3_phy_apply_otp(struct tg3 *tp)
  1893. {
  1894. u32 otp, phy;
  1895. if (!tp->phy_otp)
  1896. return;
  1897. otp = tp->phy_otp;
  1898. if (tg3_phy_toggle_auxctl_smdsp(tp, true))
  1899. return;
  1900. phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
  1901. phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
  1902. tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
  1903. phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
  1904. ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
  1905. tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
  1906. phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
  1907. phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
  1908. tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
  1909. phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
  1910. tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
  1911. phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
  1912. tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
  1913. phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
  1914. ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
  1915. tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
  1916. tg3_phy_toggle_auxctl_smdsp(tp, false);
  1917. }
  1918. static void tg3_eee_pull_config(struct tg3 *tp, struct ethtool_eee *eee)
  1919. {
  1920. u32 val;
  1921. struct ethtool_eee *dest = &tp->eee;
  1922. if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
  1923. return;
  1924. if (eee)
  1925. dest = eee;
  1926. if (tg3_phy_cl45_read(tp, MDIO_MMD_AN, TG3_CL45_D7_EEERES_STAT, &val))
  1927. return;
  1928. /* Pull eee_active */
  1929. if (val == TG3_CL45_D7_EEERES_STAT_LP_1000T ||
  1930. val == TG3_CL45_D7_EEERES_STAT_LP_100TX) {
  1931. dest->eee_active = 1;
  1932. } else
  1933. dest->eee_active = 0;
  1934. /* Pull lp advertised settings */
  1935. if (tg3_phy_cl45_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE, &val))
  1936. return;
  1937. dest->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(val);
  1938. /* Pull advertised and eee_enabled settings */
  1939. if (tg3_phy_cl45_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, &val))
  1940. return;
  1941. dest->eee_enabled = !!val;
  1942. dest->advertised = mmd_eee_adv_to_ethtool_adv_t(val);
  1943. /* Pull tx_lpi_enabled */
  1944. val = tr32(TG3_CPMU_EEE_MODE);
  1945. dest->tx_lpi_enabled = !!(val & TG3_CPMU_EEEMD_LPI_IN_TX);
  1946. /* Pull lpi timer value */
  1947. dest->tx_lpi_timer = tr32(TG3_CPMU_EEE_DBTMR1) & 0xffff;
  1948. }
  1949. static void tg3_phy_eee_adjust(struct tg3 *tp, bool current_link_up)
  1950. {
  1951. u32 val;
  1952. if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
  1953. return;
  1954. tp->setlpicnt = 0;
  1955. if (tp->link_config.autoneg == AUTONEG_ENABLE &&
  1956. current_link_up &&
  1957. tp->link_config.active_duplex == DUPLEX_FULL &&
  1958. (tp->link_config.active_speed == SPEED_100 ||
  1959. tp->link_config.active_speed == SPEED_1000)) {
  1960. u32 eeectl;
  1961. if (tp->link_config.active_speed == SPEED_1000)
  1962. eeectl = TG3_CPMU_EEE_CTRL_EXIT_16_5_US;
  1963. else
  1964. eeectl = TG3_CPMU_EEE_CTRL_EXIT_36_US;
  1965. tw32(TG3_CPMU_EEE_CTRL, eeectl);
  1966. tg3_eee_pull_config(tp, NULL);
  1967. if (tp->eee.eee_active)
  1968. tp->setlpicnt = 2;
  1969. }
  1970. if (!tp->setlpicnt) {
  1971. if (current_link_up &&
  1972. !tg3_phy_toggle_auxctl_smdsp(tp, true)) {
  1973. tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, 0x0000);
  1974. tg3_phy_toggle_auxctl_smdsp(tp, false);
  1975. }
  1976. val = tr32(TG3_CPMU_EEE_MODE);
  1977. tw32(TG3_CPMU_EEE_MODE, val & ~TG3_CPMU_EEEMD_LPI_ENABLE);
  1978. }
  1979. }
  1980. static void tg3_phy_eee_enable(struct tg3 *tp)
  1981. {
  1982. u32 val;
  1983. if (tp->link_config.active_speed == SPEED_1000 &&
  1984. (tg3_asic_rev(tp) == ASIC_REV_5717 ||
  1985. tg3_asic_rev(tp) == ASIC_REV_5719 ||
  1986. tg3_flag(tp, 57765_CLASS)) &&
  1987. !tg3_phy_toggle_auxctl_smdsp(tp, true)) {
  1988. val = MII_TG3_DSP_TAP26_ALNOKO |
  1989. MII_TG3_DSP_TAP26_RMRXSTO;
  1990. tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
  1991. tg3_phy_toggle_auxctl_smdsp(tp, false);
  1992. }
  1993. val = tr32(TG3_CPMU_EEE_MODE);
  1994. tw32(TG3_CPMU_EEE_MODE, val | TG3_CPMU_EEEMD_LPI_ENABLE);
  1995. }
  1996. static int tg3_wait_macro_done(struct tg3 *tp)
  1997. {
  1998. int limit = 100;
  1999. while (limit--) {
  2000. u32 tmp32;
  2001. if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
  2002. if ((tmp32 & 0x1000) == 0)
  2003. break;
  2004. }
  2005. }
  2006. if (limit < 0)
  2007. return -EBUSY;
  2008. return 0;
  2009. }
  2010. static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
  2011. {
  2012. static const u32 test_pat[4][6] = {
  2013. { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
  2014. { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
  2015. { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
  2016. { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
  2017. };
  2018. int chan;
  2019. for (chan = 0; chan < 4; chan++) {
  2020. int i;
  2021. tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
  2022. (chan * 0x2000) | 0x0200);
  2023. tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
  2024. for (i = 0; i < 6; i++)
  2025. tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
  2026. test_pat[chan][i]);
  2027. tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
  2028. if (tg3_wait_macro_done(tp)) {
  2029. *resetp = 1;
  2030. return -EBUSY;
  2031. }
  2032. tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
  2033. (chan * 0x2000) | 0x0200);
  2034. tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
  2035. if (tg3_wait_macro_done(tp)) {
  2036. *resetp = 1;
  2037. return -EBUSY;
  2038. }
  2039. tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
  2040. if (tg3_wait_macro_done(tp)) {
  2041. *resetp = 1;
  2042. return -EBUSY;
  2043. }
  2044. for (i = 0; i < 6; i += 2) {
  2045. u32 low, high;
  2046. if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
  2047. tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
  2048. tg3_wait_macro_done(tp)) {
  2049. *resetp = 1;
  2050. return -EBUSY;
  2051. }
  2052. low &= 0x7fff;
  2053. high &= 0x000f;
  2054. if (low != test_pat[chan][i] ||
  2055. high != test_pat[chan][i+1]) {
  2056. tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
  2057. tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
  2058. tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
  2059. return -EBUSY;
  2060. }
  2061. }
  2062. }
  2063. return 0;
  2064. }
  2065. static int tg3_phy_reset_chanpat(struct tg3 *tp)
  2066. {
  2067. int chan;
  2068. for (chan = 0; chan < 4; chan++) {
  2069. int i;
  2070. tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
  2071. (chan * 0x2000) | 0x0200);
  2072. tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
  2073. for (i = 0; i < 6; i++)
  2074. tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
  2075. tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
  2076. if (tg3_wait_macro_done(tp))
  2077. return -EBUSY;
  2078. }
  2079. return 0;
  2080. }
  2081. static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
  2082. {
  2083. u32 reg32, phy9_orig;
  2084. int retries, do_phy_reset, err;
  2085. retries = 10;
  2086. do_phy_reset = 1;
  2087. do {
  2088. if (do_phy_reset) {
  2089. err = tg3_bmcr_reset(tp);
  2090. if (err)
  2091. return err;
  2092. do_phy_reset = 0;
  2093. }
  2094. /* Disable transmitter and interrupt. */
  2095. if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
  2096. continue;
  2097. reg32 |= 0x3000;
  2098. tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
  2099. /* Set full-duplex, 1000 mbps. */
  2100. tg3_writephy(tp, MII_BMCR,
  2101. BMCR_FULLDPLX | BMCR_SPEED1000);
  2102. /* Set to master mode. */
  2103. if (tg3_readphy(tp, MII_CTRL1000, &phy9_orig))
  2104. continue;
  2105. tg3_writephy(tp, MII_CTRL1000,
  2106. CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
  2107. err = tg3_phy_toggle_auxctl_smdsp(tp, true);
  2108. if (err)
  2109. return err;
  2110. /* Block the PHY control access. */
  2111. tg3_phydsp_write(tp, 0x8005, 0x0800);
  2112. err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
  2113. if (!err)
  2114. break;
  2115. } while (--retries);
  2116. err = tg3_phy_reset_chanpat(tp);
  2117. if (err)
  2118. return err;
  2119. tg3_phydsp_write(tp, 0x8005, 0x0000);
  2120. tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
  2121. tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0000);
  2122. tg3_phy_toggle_auxctl_smdsp(tp, false);
  2123. tg3_writephy(tp, MII_CTRL1000, phy9_orig);
  2124. err = tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32);
  2125. if (err)
  2126. return err;
  2127. reg32 &= ~0x3000;
  2128. tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
  2129. return 0;
  2130. }
  2131. static void tg3_carrier_off(struct tg3 *tp)
  2132. {
  2133. netif_carrier_off(tp->dev);
  2134. tp->link_up = false;
  2135. }
  2136. static void tg3_warn_mgmt_link_flap(struct tg3 *tp)
  2137. {
  2138. if (tg3_flag(tp, ENABLE_ASF))
  2139. netdev_warn(tp->dev,
  2140. "Management side-band traffic will be interrupted during phy settings change\n");
  2141. }
  2142. /* This will reset the tigon3 PHY if there is no valid
  2143. * link unless the FORCE argument is non-zero.
  2144. */
  2145. static int tg3_phy_reset(struct tg3 *tp)
  2146. {
  2147. u32 val, cpmuctrl;
  2148. int err;
  2149. if (tg3_asic_rev(tp) == ASIC_REV_5906) {
  2150. val = tr32(GRC_MISC_CFG);
  2151. tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
  2152. udelay(40);
  2153. }
  2154. err = tg3_readphy(tp, MII_BMSR, &val);
  2155. err |= tg3_readphy(tp, MII_BMSR, &val);
  2156. if (err != 0)
  2157. return -EBUSY;
  2158. if (netif_running(tp->dev) && tp->link_up) {
  2159. netif_carrier_off(tp->dev);
  2160. tg3_link_report(tp);
  2161. }
  2162. if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
  2163. tg3_asic_rev(tp) == ASIC_REV_5704 ||
  2164. tg3_asic_rev(tp) == ASIC_REV_5705) {
  2165. err = tg3_phy_reset_5703_4_5(tp);
  2166. if (err)
  2167. return err;
  2168. goto out;
  2169. }
  2170. cpmuctrl = 0;
  2171. if (tg3_asic_rev(tp) == ASIC_REV_5784 &&
  2172. tg3_chip_rev(tp) != CHIPREV_5784_AX) {
  2173. cpmuctrl = tr32(TG3_CPMU_CTRL);
  2174. if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
  2175. tw32(TG3_CPMU_CTRL,
  2176. cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
  2177. }
  2178. err = tg3_bmcr_reset(tp);
  2179. if (err)
  2180. return err;
  2181. if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
  2182. val = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
  2183. tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, val);
  2184. tw32(TG3_CPMU_CTRL, cpmuctrl);
  2185. }
  2186. if (tg3_chip_rev(tp) == CHIPREV_5784_AX ||
  2187. tg3_chip_rev(tp) == CHIPREV_5761_AX) {
  2188. val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
  2189. if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
  2190. CPMU_LSPD_1000MB_MACCLK_12_5) {
  2191. val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
  2192. udelay(40);
  2193. tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
  2194. }
  2195. }
  2196. if (tg3_flag(tp, 5717_PLUS) &&
  2197. (tp->phy_flags & TG3_PHYFLG_MII_SERDES))
  2198. return 0;
  2199. tg3_phy_apply_otp(tp);
  2200. if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
  2201. tg3_phy_toggle_apd(tp, true);
  2202. else
  2203. tg3_phy_toggle_apd(tp, false);
  2204. out:
  2205. if ((tp->phy_flags & TG3_PHYFLG_ADC_BUG) &&
  2206. !tg3_phy_toggle_auxctl_smdsp(tp, true)) {
  2207. tg3_phydsp_write(tp, 0x201f, 0x2aaa);
  2208. tg3_phydsp_write(tp, 0x000a, 0x0323);
  2209. tg3_phy_toggle_auxctl_smdsp(tp, false);
  2210. }
  2211. if (tp->phy_flags & TG3_PHYFLG_5704_A0_BUG) {
  2212. tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
  2213. tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
  2214. }
  2215. if (tp->phy_flags & TG3_PHYFLG_BER_BUG) {
  2216. if (!tg3_phy_toggle_auxctl_smdsp(tp, true)) {
  2217. tg3_phydsp_write(tp, 0x000a, 0x310b);
  2218. tg3_phydsp_write(tp, 0x201f, 0x9506);
  2219. tg3_phydsp_write(tp, 0x401f, 0x14e2);
  2220. tg3_phy_toggle_auxctl_smdsp(tp, false);
  2221. }
  2222. } else if (tp->phy_flags & TG3_PHYFLG_JITTER_BUG) {
  2223. if (!tg3_phy_toggle_auxctl_smdsp(tp, true)) {
  2224. tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
  2225. if (tp->phy_flags & TG3_PHYFLG_ADJUST_TRIM) {
  2226. tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
  2227. tg3_writephy(tp, MII_TG3_TEST1,
  2228. MII_TG3_TEST1_TRIM_EN | 0x4);
  2229. } else
  2230. tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
  2231. tg3_phy_toggle_auxctl_smdsp(tp, false);
  2232. }
  2233. }
  2234. /* Set Extended packet length bit (bit 14) on all chips that */
  2235. /* support jumbo frames */
  2236. if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
  2237. /* Cannot do read-modify-write on 5401 */
  2238. tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
  2239. } else if (tg3_flag(tp, JUMBO_CAPABLE)) {
  2240. /* Set bit 14 with read-modify-write to preserve other bits */
  2241. err = tg3_phy_auxctl_read(tp,
  2242. MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
  2243. if (!err)
  2244. tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
  2245. val | MII_TG3_AUXCTL_ACTL_EXTPKTLEN);
  2246. }
  2247. /* Set phy register 0x10 bit 0 to high fifo elasticity to support
  2248. * jumbo frames transmission.
  2249. */
  2250. if (tg3_flag(tp, JUMBO_CAPABLE)) {
  2251. if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &val))
  2252. tg3_writephy(tp, MII_TG3_EXT_CTRL,
  2253. val | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
  2254. }
  2255. if (tg3_asic_rev(tp) == ASIC_REV_5906) {
  2256. /* adjust output voltage */
  2257. tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
  2258. }
  2259. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5762_A0)
  2260. tg3_phydsp_write(tp, 0xffb, 0x4000);
  2261. tg3_phy_toggle_automdix(tp, true);
  2262. tg3_phy_set_wirespeed(tp);
  2263. return 0;
  2264. }
  2265. #define TG3_GPIO_MSG_DRVR_PRES 0x00000001
  2266. #define TG3_GPIO_MSG_NEED_VAUX 0x00000002
  2267. #define TG3_GPIO_MSG_MASK (TG3_GPIO_MSG_DRVR_PRES | \
  2268. TG3_GPIO_MSG_NEED_VAUX)
  2269. #define TG3_GPIO_MSG_ALL_DRVR_PRES_MASK \
  2270. ((TG3_GPIO_MSG_DRVR_PRES << 0) | \
  2271. (TG3_GPIO_MSG_DRVR_PRES << 4) | \
  2272. (TG3_GPIO_MSG_DRVR_PRES << 8) | \
  2273. (TG3_GPIO_MSG_DRVR_PRES << 12))
  2274. #define TG3_GPIO_MSG_ALL_NEED_VAUX_MASK \
  2275. ((TG3_GPIO_MSG_NEED_VAUX << 0) | \
  2276. (TG3_GPIO_MSG_NEED_VAUX << 4) | \
  2277. (TG3_GPIO_MSG_NEED_VAUX << 8) | \
  2278. (TG3_GPIO_MSG_NEED_VAUX << 12))
  2279. static inline u32 tg3_set_function_status(struct tg3 *tp, u32 newstat)
  2280. {
  2281. u32 status, shift;
  2282. if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
  2283. tg3_asic_rev(tp) == ASIC_REV_5719)
  2284. status = tg3_ape_read32(tp, TG3_APE_GPIO_MSG);
  2285. else
  2286. status = tr32(TG3_CPMU_DRV_STATUS);
  2287. shift = TG3_APE_GPIO_MSG_SHIFT + 4 * tp->pci_fn;
  2288. status &= ~(TG3_GPIO_MSG_MASK << shift);
  2289. status |= (newstat << shift);
  2290. if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
  2291. tg3_asic_rev(tp) == ASIC_REV_5719)
  2292. tg3_ape_write32(tp, TG3_APE_GPIO_MSG, status);
  2293. else
  2294. tw32(TG3_CPMU_DRV_STATUS, status);
  2295. return status >> TG3_APE_GPIO_MSG_SHIFT;
  2296. }
  2297. static inline int tg3_pwrsrc_switch_to_vmain(struct tg3 *tp)
  2298. {
  2299. if (!tg3_flag(tp, IS_NIC))
  2300. return 0;
  2301. if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
  2302. tg3_asic_rev(tp) == ASIC_REV_5719 ||
  2303. tg3_asic_rev(tp) == ASIC_REV_5720) {
  2304. if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
  2305. return -EIO;
  2306. tg3_set_function_status(tp, TG3_GPIO_MSG_DRVR_PRES);
  2307. tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
  2308. TG3_GRC_LCLCTL_PWRSW_DELAY);
  2309. tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
  2310. } else {
  2311. tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
  2312. TG3_GRC_LCLCTL_PWRSW_DELAY);
  2313. }
  2314. return 0;
  2315. }
  2316. static void tg3_pwrsrc_die_with_vmain(struct tg3 *tp)
  2317. {
  2318. u32 grc_local_ctrl;
  2319. if (!tg3_flag(tp, IS_NIC) ||
  2320. tg3_asic_rev(tp) == ASIC_REV_5700 ||
  2321. tg3_asic_rev(tp) == ASIC_REV_5701)
  2322. return;
  2323. grc_local_ctrl = tp->grc_local_ctrl | GRC_LCLCTRL_GPIO_OE1;
  2324. tw32_wait_f(GRC_LOCAL_CTRL,
  2325. grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
  2326. TG3_GRC_LCLCTL_PWRSW_DELAY);
  2327. tw32_wait_f(GRC_LOCAL_CTRL,
  2328. grc_local_ctrl,
  2329. TG3_GRC_LCLCTL_PWRSW_DELAY);
  2330. tw32_wait_f(GRC_LOCAL_CTRL,
  2331. grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
  2332. TG3_GRC_LCLCTL_PWRSW_DELAY);
  2333. }
  2334. static void tg3_pwrsrc_switch_to_vaux(struct tg3 *tp)
  2335. {
  2336. if (!tg3_flag(tp, IS_NIC))
  2337. return;
  2338. if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
  2339. tg3_asic_rev(tp) == ASIC_REV_5701) {
  2340. tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
  2341. (GRC_LCLCTRL_GPIO_OE0 |
  2342. GRC_LCLCTRL_GPIO_OE1 |
  2343. GRC_LCLCTRL_GPIO_OE2 |
  2344. GRC_LCLCTRL_GPIO_OUTPUT0 |
  2345. GRC_LCLCTRL_GPIO_OUTPUT1),
  2346. TG3_GRC_LCLCTL_PWRSW_DELAY);
  2347. } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
  2348. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
  2349. /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
  2350. u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
  2351. GRC_LCLCTRL_GPIO_OE1 |
  2352. GRC_LCLCTRL_GPIO_OE2 |
  2353. GRC_LCLCTRL_GPIO_OUTPUT0 |
  2354. GRC_LCLCTRL_GPIO_OUTPUT1 |
  2355. tp->grc_local_ctrl;
  2356. tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
  2357. TG3_GRC_LCLCTL_PWRSW_DELAY);
  2358. grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
  2359. tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
  2360. TG3_GRC_LCLCTL_PWRSW_DELAY);
  2361. grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
  2362. tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
  2363. TG3_GRC_LCLCTL_PWRSW_DELAY);
  2364. } else {
  2365. u32 no_gpio2;
  2366. u32 grc_local_ctrl = 0;
  2367. /* Workaround to prevent overdrawing Amps. */
  2368. if (tg3_asic_rev(tp) == ASIC_REV_5714) {
  2369. grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
  2370. tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
  2371. grc_local_ctrl,
  2372. TG3_GRC_LCLCTL_PWRSW_DELAY);
  2373. }
  2374. /* On 5753 and variants, GPIO2 cannot be used. */
  2375. no_gpio2 = tp->nic_sram_data_cfg &
  2376. NIC_SRAM_DATA_CFG_NO_GPIO2;
  2377. grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
  2378. GRC_LCLCTRL_GPIO_OE1 |
  2379. GRC_LCLCTRL_GPIO_OE2 |
  2380. GRC_LCLCTRL_GPIO_OUTPUT1 |
  2381. GRC_LCLCTRL_GPIO_OUTPUT2;
  2382. if (no_gpio2) {
  2383. grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
  2384. GRC_LCLCTRL_GPIO_OUTPUT2);
  2385. }
  2386. tw32_wait_f(GRC_LOCAL_CTRL,
  2387. tp->grc_local_ctrl | grc_local_ctrl,
  2388. TG3_GRC_LCLCTL_PWRSW_DELAY);
  2389. grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
  2390. tw32_wait_f(GRC_LOCAL_CTRL,
  2391. tp->grc_local_ctrl | grc_local_ctrl,
  2392. TG3_GRC_LCLCTL_PWRSW_DELAY);
  2393. if (!no_gpio2) {
  2394. grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
  2395. tw32_wait_f(GRC_LOCAL_CTRL,
  2396. tp->grc_local_ctrl | grc_local_ctrl,
  2397. TG3_GRC_LCLCTL_PWRSW_DELAY);
  2398. }
  2399. }
  2400. }
  2401. static void tg3_frob_aux_power_5717(struct tg3 *tp, bool wol_enable)
  2402. {
  2403. u32 msg = 0;
  2404. /* Serialize power state transitions */
  2405. if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
  2406. return;
  2407. if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE) || wol_enable)
  2408. msg = TG3_GPIO_MSG_NEED_VAUX;
  2409. msg = tg3_set_function_status(tp, msg);
  2410. if (msg & TG3_GPIO_MSG_ALL_DRVR_PRES_MASK)
  2411. goto done;
  2412. if (msg & TG3_GPIO_MSG_ALL_NEED_VAUX_MASK)
  2413. tg3_pwrsrc_switch_to_vaux(tp);
  2414. else
  2415. tg3_pwrsrc_die_with_vmain(tp);
  2416. done:
  2417. tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
  2418. }
  2419. static void tg3_frob_aux_power(struct tg3 *tp, bool include_wol)
  2420. {
  2421. bool need_vaux = false;
  2422. /* The GPIOs do something completely different on 57765. */
  2423. if (!tg3_flag(tp, IS_NIC) || tg3_flag(tp, 57765_CLASS))
  2424. return;
  2425. if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
  2426. tg3_asic_rev(tp) == ASIC_REV_5719 ||
  2427. tg3_asic_rev(tp) == ASIC_REV_5720) {
  2428. tg3_frob_aux_power_5717(tp, include_wol ?
  2429. tg3_flag(tp, WOL_ENABLE) != 0 : 0);
  2430. return;
  2431. }
  2432. if (tp->pdev_peer && tp->pdev_peer != tp->pdev) {
  2433. struct net_device *dev_peer;
  2434. dev_peer = pci_get_drvdata(tp->pdev_peer);
  2435. /* remove_one() may have been run on the peer. */
  2436. if (dev_peer) {
  2437. struct tg3 *tp_peer = netdev_priv(dev_peer);
  2438. if (tg3_flag(tp_peer, INIT_COMPLETE))
  2439. return;
  2440. if ((include_wol && tg3_flag(tp_peer, WOL_ENABLE)) ||
  2441. tg3_flag(tp_peer, ENABLE_ASF))
  2442. need_vaux = true;
  2443. }
  2444. }
  2445. if ((include_wol && tg3_flag(tp, WOL_ENABLE)) ||
  2446. tg3_flag(tp, ENABLE_ASF))
  2447. need_vaux = true;
  2448. if (need_vaux)
  2449. tg3_pwrsrc_switch_to_vaux(tp);
  2450. else
  2451. tg3_pwrsrc_die_with_vmain(tp);
  2452. }
  2453. static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
  2454. {
  2455. if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
  2456. return 1;
  2457. else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
  2458. if (speed != SPEED_10)
  2459. return 1;
  2460. } else if (speed == SPEED_10)
  2461. return 1;
  2462. return 0;
  2463. }
  2464. static bool tg3_phy_power_bug(struct tg3 *tp)
  2465. {
  2466. switch (tg3_asic_rev(tp)) {
  2467. case ASIC_REV_5700:
  2468. case ASIC_REV_5704:
  2469. return true;
  2470. case ASIC_REV_5780:
  2471. if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
  2472. return true;
  2473. return false;
  2474. case ASIC_REV_5717:
  2475. if (!tp->pci_fn)
  2476. return true;
  2477. return false;
  2478. case ASIC_REV_5719:
  2479. case ASIC_REV_5720:
  2480. if ((tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
  2481. !tp->pci_fn)
  2482. return true;
  2483. return false;
  2484. }
  2485. return false;
  2486. }
  2487. static bool tg3_phy_led_bug(struct tg3 *tp)
  2488. {
  2489. switch (tg3_asic_rev(tp)) {
  2490. case ASIC_REV_5719:
  2491. case ASIC_REV_5720:
  2492. if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
  2493. !tp->pci_fn)
  2494. return true;
  2495. return false;
  2496. }
  2497. return false;
  2498. }
  2499. static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
  2500. {
  2501. u32 val;
  2502. if (tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN)
  2503. return;
  2504. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
  2505. if (tg3_asic_rev(tp) == ASIC_REV_5704) {
  2506. u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
  2507. u32 serdes_cfg = tr32(MAC_SERDES_CFG);
  2508. sg_dig_ctrl |=
  2509. SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
  2510. tw32(SG_DIG_CTRL, sg_dig_ctrl);
  2511. tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
  2512. }
  2513. return;
  2514. }
  2515. if (tg3_asic_rev(tp) == ASIC_REV_5906) {
  2516. tg3_bmcr_reset(tp);
  2517. val = tr32(GRC_MISC_CFG);
  2518. tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
  2519. udelay(40);
  2520. return;
  2521. } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
  2522. u32 phytest;
  2523. if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
  2524. u32 phy;
  2525. tg3_writephy(tp, MII_ADVERTISE, 0);
  2526. tg3_writephy(tp, MII_BMCR,
  2527. BMCR_ANENABLE | BMCR_ANRESTART);
  2528. tg3_writephy(tp, MII_TG3_FET_TEST,
  2529. phytest | MII_TG3_FET_SHADOW_EN);
  2530. if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
  2531. phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
  2532. tg3_writephy(tp,
  2533. MII_TG3_FET_SHDW_AUXMODE4,
  2534. phy);
  2535. }
  2536. tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
  2537. }
  2538. return;
  2539. } else if (do_low_power) {
  2540. if (!tg3_phy_led_bug(tp))
  2541. tg3_writephy(tp, MII_TG3_EXT_CTRL,
  2542. MII_TG3_EXT_CTRL_FORCE_LED_OFF);
  2543. val = MII_TG3_AUXCTL_PCTL_100TX_LPWR |
  2544. MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
  2545. MII_TG3_AUXCTL_PCTL_VREG_11V;
  2546. tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, val);
  2547. }
  2548. /* The PHY should not be powered down on some chips because
  2549. * of bugs.
  2550. */
  2551. if (tg3_phy_power_bug(tp))
  2552. return;
  2553. if (tg3_chip_rev(tp) == CHIPREV_5784_AX ||
  2554. tg3_chip_rev(tp) == CHIPREV_5761_AX) {
  2555. val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
  2556. val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
  2557. val |= CPMU_LSPD_1000MB_MACCLK_12_5;
  2558. tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
  2559. }
  2560. tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
  2561. }
  2562. /* tp->lock is held. */
  2563. static int tg3_nvram_lock(struct tg3 *tp)
  2564. {
  2565. if (tg3_flag(tp, NVRAM)) {
  2566. int i;
  2567. if (tp->nvram_lock_cnt == 0) {
  2568. tw32(NVRAM_SWARB, SWARB_REQ_SET1);
  2569. for (i = 0; i < 8000; i++) {
  2570. if (tr32(NVRAM_SWARB) & SWARB_GNT1)
  2571. break;
  2572. udelay(20);
  2573. }
  2574. if (i == 8000) {
  2575. tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
  2576. return -ENODEV;
  2577. }
  2578. }
  2579. tp->nvram_lock_cnt++;
  2580. }
  2581. return 0;
  2582. }
  2583. /* tp->lock is held. */
  2584. static void tg3_nvram_unlock(struct tg3 *tp)
  2585. {
  2586. if (tg3_flag(tp, NVRAM)) {
  2587. if (tp->nvram_lock_cnt > 0)
  2588. tp->nvram_lock_cnt--;
  2589. if (tp->nvram_lock_cnt == 0)
  2590. tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
  2591. }
  2592. }
  2593. /* tp->lock is held. */
  2594. static void tg3_enable_nvram_access(struct tg3 *tp)
  2595. {
  2596. if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
  2597. u32 nvaccess = tr32(NVRAM_ACCESS);
  2598. tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
  2599. }
  2600. }
  2601. /* tp->lock is held. */
  2602. static void tg3_disable_nvram_access(struct tg3 *tp)
  2603. {
  2604. if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
  2605. u32 nvaccess = tr32(NVRAM_ACCESS);
  2606. tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
  2607. }
  2608. }
  2609. static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
  2610. u32 offset, u32 *val)
  2611. {
  2612. u32 tmp;
  2613. int i;
  2614. if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
  2615. return -EINVAL;
  2616. tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
  2617. EEPROM_ADDR_DEVID_MASK |
  2618. EEPROM_ADDR_READ);
  2619. tw32(GRC_EEPROM_ADDR,
  2620. tmp |
  2621. (0 << EEPROM_ADDR_DEVID_SHIFT) |
  2622. ((offset << EEPROM_ADDR_ADDR_SHIFT) &
  2623. EEPROM_ADDR_ADDR_MASK) |
  2624. EEPROM_ADDR_READ | EEPROM_ADDR_START);
  2625. for (i = 0; i < 1000; i++) {
  2626. tmp = tr32(GRC_EEPROM_ADDR);
  2627. if (tmp & EEPROM_ADDR_COMPLETE)
  2628. break;
  2629. msleep(1);
  2630. }
  2631. if (!(tmp & EEPROM_ADDR_COMPLETE))
  2632. return -EBUSY;
  2633. tmp = tr32(GRC_EEPROM_DATA);
  2634. /*
  2635. * The data will always be opposite the native endian
  2636. * format. Perform a blind byteswap to compensate.
  2637. */
  2638. *val = swab32(tmp);
  2639. return 0;
  2640. }
  2641. #define NVRAM_CMD_TIMEOUT 5000
  2642. static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
  2643. {
  2644. int i;
  2645. tw32(NVRAM_CMD, nvram_cmd);
  2646. for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
  2647. usleep_range(10, 40);
  2648. if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
  2649. udelay(10);
  2650. break;
  2651. }
  2652. }
  2653. if (i == NVRAM_CMD_TIMEOUT)
  2654. return -EBUSY;
  2655. return 0;
  2656. }
  2657. static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
  2658. {
  2659. if (tg3_flag(tp, NVRAM) &&
  2660. tg3_flag(tp, NVRAM_BUFFERED) &&
  2661. tg3_flag(tp, FLASH) &&
  2662. !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
  2663. (tp->nvram_jedecnum == JEDEC_ATMEL))
  2664. addr = ((addr / tp->nvram_pagesize) <<
  2665. ATMEL_AT45DB0X1B_PAGE_POS) +
  2666. (addr % tp->nvram_pagesize);
  2667. return addr;
  2668. }
  2669. static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
  2670. {
  2671. if (tg3_flag(tp, NVRAM) &&
  2672. tg3_flag(tp, NVRAM_BUFFERED) &&
  2673. tg3_flag(tp, FLASH) &&
  2674. !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
  2675. (tp->nvram_jedecnum == JEDEC_ATMEL))
  2676. addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
  2677. tp->nvram_pagesize) +
  2678. (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
  2679. return addr;
  2680. }
  2681. /* NOTE: Data read in from NVRAM is byteswapped according to
  2682. * the byteswapping settings for all other register accesses.
  2683. * tg3 devices are BE devices, so on a BE machine, the data
  2684. * returned will be exactly as it is seen in NVRAM. On a LE
  2685. * machine, the 32-bit value will be byteswapped.
  2686. */
  2687. static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
  2688. {
  2689. int ret;
  2690. if (!tg3_flag(tp, NVRAM))
  2691. return tg3_nvram_read_using_eeprom(tp, offset, val);
  2692. offset = tg3_nvram_phys_addr(tp, offset);
  2693. if (offset > NVRAM_ADDR_MSK)
  2694. return -EINVAL;
  2695. ret = tg3_nvram_lock(tp);
  2696. if (ret)
  2697. return ret;
  2698. tg3_enable_nvram_access(tp);
  2699. tw32(NVRAM_ADDR, offset);
  2700. ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
  2701. NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
  2702. if (ret == 0)
  2703. *val = tr32(NVRAM_RDDATA);
  2704. tg3_disable_nvram_access(tp);
  2705. tg3_nvram_unlock(tp);
  2706. return ret;
  2707. }
  2708. /* Ensures NVRAM data is in bytestream format. */
  2709. static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
  2710. {
  2711. u32 v;
  2712. int res = tg3_nvram_read(tp, offset, &v);
  2713. if (!res)
  2714. *val = cpu_to_be32(v);
  2715. return res;
  2716. }
  2717. static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
  2718. u32 offset, u32 len, u8 *buf)
  2719. {
  2720. int i, j, rc = 0;
  2721. u32 val;
  2722. for (i = 0; i < len; i += 4) {
  2723. u32 addr;
  2724. __be32 data;
  2725. addr = offset + i;
  2726. memcpy(&data, buf + i, 4);
  2727. /*
  2728. * The SEEPROM interface expects the data to always be opposite
  2729. * the native endian format. We accomplish this by reversing
  2730. * all the operations that would have been performed on the
  2731. * data from a call to tg3_nvram_read_be32().
  2732. */
  2733. tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
  2734. val = tr32(GRC_EEPROM_ADDR);
  2735. tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
  2736. val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
  2737. EEPROM_ADDR_READ);
  2738. tw32(GRC_EEPROM_ADDR, val |
  2739. (0 << EEPROM_ADDR_DEVID_SHIFT) |
  2740. (addr & EEPROM_ADDR_ADDR_MASK) |
  2741. EEPROM_ADDR_START |
  2742. EEPROM_ADDR_WRITE);
  2743. for (j = 0; j < 1000; j++) {
  2744. val = tr32(GRC_EEPROM_ADDR);
  2745. if (val & EEPROM_ADDR_COMPLETE)
  2746. break;
  2747. msleep(1);
  2748. }
  2749. if (!(val & EEPROM_ADDR_COMPLETE)) {
  2750. rc = -EBUSY;
  2751. break;
  2752. }
  2753. }
  2754. return rc;
  2755. }
  2756. /* offset and length are dword aligned */
  2757. static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
  2758. u8 *buf)
  2759. {
  2760. int ret = 0;
  2761. u32 pagesize = tp->nvram_pagesize;
  2762. u32 pagemask = pagesize - 1;
  2763. u32 nvram_cmd;
  2764. u8 *tmp;
  2765. tmp = kmalloc(pagesize, GFP_KERNEL);
  2766. if (tmp == NULL)
  2767. return -ENOMEM;
  2768. while (len) {
  2769. int j;
  2770. u32 phy_addr, page_off, size;
  2771. phy_addr = offset & ~pagemask;
  2772. for (j = 0; j < pagesize; j += 4) {
  2773. ret = tg3_nvram_read_be32(tp, phy_addr + j,
  2774. (__be32 *) (tmp + j));
  2775. if (ret)
  2776. break;
  2777. }
  2778. if (ret)
  2779. break;
  2780. page_off = offset & pagemask;
  2781. size = pagesize;
  2782. if (len < size)
  2783. size = len;
  2784. len -= size;
  2785. memcpy(tmp + page_off, buf, size);
  2786. offset = offset + (pagesize - page_off);
  2787. tg3_enable_nvram_access(tp);
  2788. /*
  2789. * Before we can erase the flash page, we need
  2790. * to issue a special "write enable" command.
  2791. */
  2792. nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
  2793. if (tg3_nvram_exec_cmd(tp, nvram_cmd))
  2794. break;
  2795. /* Erase the target page */
  2796. tw32(NVRAM_ADDR, phy_addr);
  2797. nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
  2798. NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
  2799. if (tg3_nvram_exec_cmd(tp, nvram_cmd))
  2800. break;
  2801. /* Issue another write enable to start the write. */
  2802. nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
  2803. if (tg3_nvram_exec_cmd(tp, nvram_cmd))
  2804. break;
  2805. for (j = 0; j < pagesize; j += 4) {
  2806. __be32 data;
  2807. data = *((__be32 *) (tmp + j));
  2808. tw32(NVRAM_WRDATA, be32_to_cpu(data));
  2809. tw32(NVRAM_ADDR, phy_addr + j);
  2810. nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
  2811. NVRAM_CMD_WR;
  2812. if (j == 0)
  2813. nvram_cmd |= NVRAM_CMD_FIRST;
  2814. else if (j == (pagesize - 4))
  2815. nvram_cmd |= NVRAM_CMD_LAST;
  2816. ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
  2817. if (ret)
  2818. break;
  2819. }
  2820. if (ret)
  2821. break;
  2822. }
  2823. nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
  2824. tg3_nvram_exec_cmd(tp, nvram_cmd);
  2825. kfree(tmp);
  2826. return ret;
  2827. }
  2828. /* offset and length are dword aligned */
  2829. static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
  2830. u8 *buf)
  2831. {
  2832. int i, ret = 0;
  2833. for (i = 0; i < len; i += 4, offset += 4) {
  2834. u32 page_off, phy_addr, nvram_cmd;
  2835. __be32 data;
  2836. memcpy(&data, buf + i, 4);
  2837. tw32(NVRAM_WRDATA, be32_to_cpu(data));
  2838. page_off = offset % tp->nvram_pagesize;
  2839. phy_addr = tg3_nvram_phys_addr(tp, offset);
  2840. nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
  2841. if (page_off == 0 || i == 0)
  2842. nvram_cmd |= NVRAM_CMD_FIRST;
  2843. if (page_off == (tp->nvram_pagesize - 4))
  2844. nvram_cmd |= NVRAM_CMD_LAST;
  2845. if (i == (len - 4))
  2846. nvram_cmd |= NVRAM_CMD_LAST;
  2847. if ((nvram_cmd & NVRAM_CMD_FIRST) ||
  2848. !tg3_flag(tp, FLASH) ||
  2849. !tg3_flag(tp, 57765_PLUS))
  2850. tw32(NVRAM_ADDR, phy_addr);
  2851. if (tg3_asic_rev(tp) != ASIC_REV_5752 &&
  2852. !tg3_flag(tp, 5755_PLUS) &&
  2853. (tp->nvram_jedecnum == JEDEC_ST) &&
  2854. (nvram_cmd & NVRAM_CMD_FIRST)) {
  2855. u32 cmd;
  2856. cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
  2857. ret = tg3_nvram_exec_cmd(tp, cmd);
  2858. if (ret)
  2859. break;
  2860. }
  2861. if (!tg3_flag(tp, FLASH)) {
  2862. /* We always do complete word writes to eeprom. */
  2863. nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
  2864. }
  2865. ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
  2866. if (ret)
  2867. break;
  2868. }
  2869. return ret;
  2870. }
  2871. /* offset and length are dword aligned */
  2872. static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
  2873. {
  2874. int ret;
  2875. if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
  2876. tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
  2877. ~GRC_LCLCTRL_GPIO_OUTPUT1);
  2878. udelay(40);
  2879. }
  2880. if (!tg3_flag(tp, NVRAM)) {
  2881. ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
  2882. } else {
  2883. u32 grc_mode;
  2884. ret = tg3_nvram_lock(tp);
  2885. if (ret)
  2886. return ret;
  2887. tg3_enable_nvram_access(tp);
  2888. if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM))
  2889. tw32(NVRAM_WRITE1, 0x406);
  2890. grc_mode = tr32(GRC_MODE);
  2891. tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
  2892. if (tg3_flag(tp, NVRAM_BUFFERED) || !tg3_flag(tp, FLASH)) {
  2893. ret = tg3_nvram_write_block_buffered(tp, offset, len,
  2894. buf);
  2895. } else {
  2896. ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
  2897. buf);
  2898. }
  2899. grc_mode = tr32(GRC_MODE);
  2900. tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
  2901. tg3_disable_nvram_access(tp);
  2902. tg3_nvram_unlock(tp);
  2903. }
  2904. if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
  2905. tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
  2906. udelay(40);
  2907. }
  2908. return ret;
  2909. }
  2910. #define RX_CPU_SCRATCH_BASE 0x30000
  2911. #define RX_CPU_SCRATCH_SIZE 0x04000
  2912. #define TX_CPU_SCRATCH_BASE 0x34000
  2913. #define TX_CPU_SCRATCH_SIZE 0x04000
  2914. /* tp->lock is held. */
  2915. static int tg3_pause_cpu(struct tg3 *tp, u32 cpu_base)
  2916. {
  2917. int i;
  2918. const int iters = 10000;
  2919. for (i = 0; i < iters; i++) {
  2920. tw32(cpu_base + CPU_STATE, 0xffffffff);
  2921. tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
  2922. if (tr32(cpu_base + CPU_MODE) & CPU_MODE_HALT)
  2923. break;
  2924. if (pci_channel_offline(tp->pdev))
  2925. return -EBUSY;
  2926. }
  2927. return (i == iters) ? -EBUSY : 0;
  2928. }
  2929. /* tp->lock is held. */
  2930. static int tg3_rxcpu_pause(struct tg3 *tp)
  2931. {
  2932. int rc = tg3_pause_cpu(tp, RX_CPU_BASE);
  2933. tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
  2934. tw32_f(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
  2935. udelay(10);
  2936. return rc;
  2937. }
  2938. /* tp->lock is held. */
  2939. static int tg3_txcpu_pause(struct tg3 *tp)
  2940. {
  2941. return tg3_pause_cpu(tp, TX_CPU_BASE);
  2942. }
  2943. /* tp->lock is held. */
  2944. static void tg3_resume_cpu(struct tg3 *tp, u32 cpu_base)
  2945. {
  2946. tw32(cpu_base + CPU_STATE, 0xffffffff);
  2947. tw32_f(cpu_base + CPU_MODE, 0x00000000);
  2948. }
  2949. /* tp->lock is held. */
  2950. static void tg3_rxcpu_resume(struct tg3 *tp)
  2951. {
  2952. tg3_resume_cpu(tp, RX_CPU_BASE);
  2953. }
  2954. /* tp->lock is held. */
  2955. static int tg3_halt_cpu(struct tg3 *tp, u32 cpu_base)
  2956. {
  2957. int rc;
  2958. BUG_ON(cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS));
  2959. if (tg3_asic_rev(tp) == ASIC_REV_5906) {
  2960. u32 val = tr32(GRC_VCPU_EXT_CTRL);
  2961. tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
  2962. return 0;
  2963. }
  2964. if (cpu_base == RX_CPU_BASE) {
  2965. rc = tg3_rxcpu_pause(tp);
  2966. } else {
  2967. /*
  2968. * There is only an Rx CPU for the 5750 derivative in the
  2969. * BCM4785.
  2970. */
  2971. if (tg3_flag(tp, IS_SSB_CORE))
  2972. return 0;
  2973. rc = tg3_txcpu_pause(tp);
  2974. }
  2975. if (rc) {
  2976. netdev_err(tp->dev, "%s timed out, %s CPU\n",
  2977. __func__, cpu_base == RX_CPU_BASE ? "RX" : "TX");
  2978. return -ENODEV;
  2979. }
  2980. /* Clear firmware's nvram arbitration. */
  2981. if (tg3_flag(tp, NVRAM))
  2982. tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
  2983. return 0;
  2984. }
  2985. static int tg3_fw_data_len(struct tg3 *tp,
  2986. const struct tg3_firmware_hdr *fw_hdr)
  2987. {
  2988. int fw_len;
  2989. /* Non fragmented firmware have one firmware header followed by a
  2990. * contiguous chunk of data to be written. The length field in that
  2991. * header is not the length of data to be written but the complete
  2992. * length of the bss. The data length is determined based on
  2993. * tp->fw->size minus headers.
  2994. *
  2995. * Fragmented firmware have a main header followed by multiple
  2996. * fragments. Each fragment is identical to non fragmented firmware
  2997. * with a firmware header followed by a contiguous chunk of data. In
  2998. * the main header, the length field is unused and set to 0xffffffff.
  2999. * In each fragment header the length is the entire size of that
  3000. * fragment i.e. fragment data + header length. Data length is
  3001. * therefore length field in the header minus TG3_FW_HDR_LEN.
  3002. */
  3003. if (tp->fw_len == 0xffffffff)
  3004. fw_len = be32_to_cpu(fw_hdr->len);
  3005. else
  3006. fw_len = tp->fw->size;
  3007. return (fw_len - TG3_FW_HDR_LEN) / sizeof(u32);
  3008. }
  3009. /* tp->lock is held. */
  3010. static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base,
  3011. u32 cpu_scratch_base, int cpu_scratch_size,
  3012. const struct tg3_firmware_hdr *fw_hdr)
  3013. {
  3014. int err, i;
  3015. void (*write_op)(struct tg3 *, u32, u32);
  3016. int total_len = tp->fw->size;
  3017. if (cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS)) {
  3018. netdev_err(tp->dev,
  3019. "%s: Trying to load TX cpu firmware which is 5705\n",
  3020. __func__);
  3021. return -EINVAL;
  3022. }
  3023. if (tg3_flag(tp, 5705_PLUS) && tg3_asic_rev(tp) != ASIC_REV_57766)
  3024. write_op = tg3_write_mem;
  3025. else
  3026. write_op = tg3_write_indirect_reg32;
  3027. if (tg3_asic_rev(tp) != ASIC_REV_57766) {
  3028. /* It is possible that bootcode is still loading at this point.
  3029. * Get the nvram lock first before halting the cpu.
  3030. */
  3031. int lock_err = tg3_nvram_lock(tp);
  3032. err = tg3_halt_cpu(tp, cpu_base);
  3033. if (!lock_err)
  3034. tg3_nvram_unlock(tp);
  3035. if (err)
  3036. goto out;
  3037. for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
  3038. write_op(tp, cpu_scratch_base + i, 0);
  3039. tw32(cpu_base + CPU_STATE, 0xffffffff);
  3040. tw32(cpu_base + CPU_MODE,
  3041. tr32(cpu_base + CPU_MODE) | CPU_MODE_HALT);
  3042. } else {
  3043. /* Subtract additional main header for fragmented firmware and
  3044. * advance to the first fragment
  3045. */
  3046. total_len -= TG3_FW_HDR_LEN;
  3047. fw_hdr++;
  3048. }
  3049. do {
  3050. u32 *fw_data = (u32 *)(fw_hdr + 1);
  3051. for (i = 0; i < tg3_fw_data_len(tp, fw_hdr); i++)
  3052. write_op(tp, cpu_scratch_base +
  3053. (be32_to_cpu(fw_hdr->base_addr) & 0xffff) +
  3054. (i * sizeof(u32)),
  3055. be32_to_cpu(fw_data[i]));
  3056. total_len -= be32_to_cpu(fw_hdr->len);
  3057. /* Advance to next fragment */
  3058. fw_hdr = (struct tg3_firmware_hdr *)
  3059. ((void *)fw_hdr + be32_to_cpu(fw_hdr->len));
  3060. } while (total_len > 0);
  3061. err = 0;
  3062. out:
  3063. return err;
  3064. }
  3065. /* tp->lock is held. */
  3066. static int tg3_pause_cpu_and_set_pc(struct tg3 *tp, u32 cpu_base, u32 pc)
  3067. {
  3068. int i;
  3069. const int iters = 5;
  3070. tw32(cpu_base + CPU_STATE, 0xffffffff);
  3071. tw32_f(cpu_base + CPU_PC, pc);
  3072. for (i = 0; i < iters; i++) {
  3073. if (tr32(cpu_base + CPU_PC) == pc)
  3074. break;
  3075. tw32(cpu_base + CPU_STATE, 0xffffffff);
  3076. tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
  3077. tw32_f(cpu_base + CPU_PC, pc);
  3078. udelay(1000);
  3079. }
  3080. return (i == iters) ? -EBUSY : 0;
  3081. }
  3082. /* tp->lock is held. */
  3083. static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
  3084. {
  3085. const struct tg3_firmware_hdr *fw_hdr;
  3086. int err;
  3087. fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
  3088. /* Firmware blob starts with version numbers, followed by
  3089. start address and length. We are setting complete length.
  3090. length = end_address_of_bss - start_address_of_text.
  3091. Remainder is the blob to be loaded contiguously
  3092. from start address. */
  3093. err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
  3094. RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
  3095. fw_hdr);
  3096. if (err)
  3097. return err;
  3098. err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
  3099. TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
  3100. fw_hdr);
  3101. if (err)
  3102. return err;
  3103. /* Now startup only the RX cpu. */
  3104. err = tg3_pause_cpu_and_set_pc(tp, RX_CPU_BASE,
  3105. be32_to_cpu(fw_hdr->base_addr));
  3106. if (err) {
  3107. netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
  3108. "should be %08x\n", __func__,
  3109. tr32(RX_CPU_BASE + CPU_PC),
  3110. be32_to_cpu(fw_hdr->base_addr));
  3111. return -ENODEV;
  3112. }
  3113. tg3_rxcpu_resume(tp);
  3114. return 0;
  3115. }
  3116. static int tg3_validate_rxcpu_state(struct tg3 *tp)
  3117. {
  3118. const int iters = 1000;
  3119. int i;
  3120. u32 val;
  3121. /* Wait for boot code to complete initialization and enter service
  3122. * loop. It is then safe to download service patches
  3123. */
  3124. for (i = 0; i < iters; i++) {
  3125. if (tr32(RX_CPU_HWBKPT) == TG3_SBROM_IN_SERVICE_LOOP)
  3126. break;
  3127. udelay(10);
  3128. }
  3129. if (i == iters) {
  3130. netdev_err(tp->dev, "Boot code not ready for service patches\n");
  3131. return -EBUSY;
  3132. }
  3133. val = tg3_read_indirect_reg32(tp, TG3_57766_FW_HANDSHAKE);
  3134. if (val & 0xff) {
  3135. netdev_warn(tp->dev,
  3136. "Other patches exist. Not downloading EEE patch\n");
  3137. return -EEXIST;
  3138. }
  3139. return 0;
  3140. }
  3141. /* tp->lock is held. */
  3142. static void tg3_load_57766_firmware(struct tg3 *tp)
  3143. {
  3144. struct tg3_firmware_hdr *fw_hdr;
  3145. if (!tg3_flag(tp, NO_NVRAM))
  3146. return;
  3147. if (tg3_validate_rxcpu_state(tp))
  3148. return;
  3149. if (!tp->fw)
  3150. return;
  3151. /* This firmware blob has a different format than older firmware
  3152. * releases as given below. The main difference is we have fragmented
  3153. * data to be written to non-contiguous locations.
  3154. *
  3155. * In the beginning we have a firmware header identical to other
  3156. * firmware which consists of version, base addr and length. The length
  3157. * here is unused and set to 0xffffffff.
  3158. *
  3159. * This is followed by a series of firmware fragments which are
  3160. * individually identical to previous firmware. i.e. they have the
  3161. * firmware header and followed by data for that fragment. The version
  3162. * field of the individual fragment header is unused.
  3163. */
  3164. fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
  3165. if (be32_to_cpu(fw_hdr->base_addr) != TG3_57766_FW_BASE_ADDR)
  3166. return;
  3167. if (tg3_rxcpu_pause(tp))
  3168. return;
  3169. /* tg3_load_firmware_cpu() will always succeed for the 57766 */
  3170. tg3_load_firmware_cpu(tp, 0, TG3_57766_FW_BASE_ADDR, 0, fw_hdr);
  3171. tg3_rxcpu_resume(tp);
  3172. }
  3173. /* tp->lock is held. */
  3174. static int tg3_load_tso_firmware(struct tg3 *tp)
  3175. {
  3176. const struct tg3_firmware_hdr *fw_hdr;
  3177. unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
  3178. int err;
  3179. if (!tg3_flag(tp, FW_TSO))
  3180. return 0;
  3181. fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
  3182. /* Firmware blob starts with version numbers, followed by
  3183. start address and length. We are setting complete length.
  3184. length = end_address_of_bss - start_address_of_text.
  3185. Remainder is the blob to be loaded contiguously
  3186. from start address. */
  3187. cpu_scratch_size = tp->fw_len;
  3188. if (tg3_asic_rev(tp) == ASIC_REV_5705) {
  3189. cpu_base = RX_CPU_BASE;
  3190. cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
  3191. } else {
  3192. cpu_base = TX_CPU_BASE;
  3193. cpu_scratch_base = TX_CPU_SCRATCH_BASE;
  3194. cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
  3195. }
  3196. err = tg3_load_firmware_cpu(tp, cpu_base,
  3197. cpu_scratch_base, cpu_scratch_size,
  3198. fw_hdr);
  3199. if (err)
  3200. return err;
  3201. /* Now startup the cpu. */
  3202. err = tg3_pause_cpu_and_set_pc(tp, cpu_base,
  3203. be32_to_cpu(fw_hdr->base_addr));
  3204. if (err) {
  3205. netdev_err(tp->dev,
  3206. "%s fails to set CPU PC, is %08x should be %08x\n",
  3207. __func__, tr32(cpu_base + CPU_PC),
  3208. be32_to_cpu(fw_hdr->base_addr));
  3209. return -ENODEV;
  3210. }
  3211. tg3_resume_cpu(tp, cpu_base);
  3212. return 0;
  3213. }
  3214. /* tp->lock is held. */
  3215. static void __tg3_set_one_mac_addr(struct tg3 *tp, u8 *mac_addr, int index)
  3216. {
  3217. u32 addr_high, addr_low;
  3218. addr_high = ((mac_addr[0] << 8) | mac_addr[1]);
  3219. addr_low = ((mac_addr[2] << 24) | (mac_addr[3] << 16) |
  3220. (mac_addr[4] << 8) | mac_addr[5]);
  3221. if (index < 4) {
  3222. tw32(MAC_ADDR_0_HIGH + (index * 8), addr_high);
  3223. tw32(MAC_ADDR_0_LOW + (index * 8), addr_low);
  3224. } else {
  3225. index -= 4;
  3226. tw32(MAC_EXTADDR_0_HIGH + (index * 8), addr_high);
  3227. tw32(MAC_EXTADDR_0_LOW + (index * 8), addr_low);
  3228. }
  3229. }
  3230. /* tp->lock is held. */
  3231. static void __tg3_set_mac_addr(struct tg3 *tp, bool skip_mac_1)
  3232. {
  3233. u32 addr_high;
  3234. int i;
  3235. for (i = 0; i < 4; i++) {
  3236. if (i == 1 && skip_mac_1)
  3237. continue;
  3238. __tg3_set_one_mac_addr(tp, tp->dev->dev_addr, i);
  3239. }
  3240. if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
  3241. tg3_asic_rev(tp) == ASIC_REV_5704) {
  3242. for (i = 4; i < 16; i++)
  3243. __tg3_set_one_mac_addr(tp, tp->dev->dev_addr, i);
  3244. }
  3245. addr_high = (tp->dev->dev_addr[0] +
  3246. tp->dev->dev_addr[1] +
  3247. tp->dev->dev_addr[2] +
  3248. tp->dev->dev_addr[3] +
  3249. tp->dev->dev_addr[4] +
  3250. tp->dev->dev_addr[5]) &
  3251. TX_BACKOFF_SEED_MASK;
  3252. tw32(MAC_TX_BACKOFF_SEED, addr_high);
  3253. }
  3254. static void tg3_enable_register_access(struct tg3 *tp)
  3255. {
  3256. /*
  3257. * Make sure register accesses (indirect or otherwise) will function
  3258. * correctly.
  3259. */
  3260. pci_write_config_dword(tp->pdev,
  3261. TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
  3262. }
  3263. static int tg3_power_up(struct tg3 *tp)
  3264. {
  3265. int err;
  3266. tg3_enable_register_access(tp);
  3267. err = pci_set_power_state(tp->pdev, PCI_D0);
  3268. if (!err) {
  3269. /* Switch out of Vaux if it is a NIC */
  3270. tg3_pwrsrc_switch_to_vmain(tp);
  3271. } else {
  3272. netdev_err(tp->dev, "Transition to D0 failed\n");
  3273. }
  3274. return err;
  3275. }
  3276. static int tg3_setup_phy(struct tg3 *, bool);
  3277. static int tg3_power_down_prepare(struct tg3 *tp)
  3278. {
  3279. u32 misc_host_ctrl;
  3280. bool device_should_wake, do_low_power;
  3281. tg3_enable_register_access(tp);
  3282. /* Restore the CLKREQ setting. */
  3283. if (tg3_flag(tp, CLKREQ_BUG))
  3284. pcie_capability_set_word(tp->pdev, PCI_EXP_LNKCTL,
  3285. PCI_EXP_LNKCTL_CLKREQ_EN);
  3286. misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
  3287. tw32(TG3PCI_MISC_HOST_CTRL,
  3288. misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
  3289. device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
  3290. tg3_flag(tp, WOL_ENABLE);
  3291. if (tg3_flag(tp, USE_PHYLIB)) {
  3292. do_low_power = false;
  3293. if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
  3294. !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
  3295. struct phy_device *phydev;
  3296. u32 phyid, advertising;
  3297. phydev = tp->mdio_bus->phy_map[tp->phy_addr];
  3298. tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
  3299. tp->link_config.speed = phydev->speed;
  3300. tp->link_config.duplex = phydev->duplex;
  3301. tp->link_config.autoneg = phydev->autoneg;
  3302. tp->link_config.advertising = phydev->advertising;
  3303. advertising = ADVERTISED_TP |
  3304. ADVERTISED_Pause |
  3305. ADVERTISED_Autoneg |
  3306. ADVERTISED_10baseT_Half;
  3307. if (tg3_flag(tp, ENABLE_ASF) || device_should_wake) {
  3308. if (tg3_flag(tp, WOL_SPEED_100MB))
  3309. advertising |=
  3310. ADVERTISED_100baseT_Half |
  3311. ADVERTISED_100baseT_Full |
  3312. ADVERTISED_10baseT_Full;
  3313. else
  3314. advertising |= ADVERTISED_10baseT_Full;
  3315. }
  3316. phydev->advertising = advertising;
  3317. phy_start_aneg(phydev);
  3318. phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
  3319. if (phyid != PHY_ID_BCMAC131) {
  3320. phyid &= PHY_BCM_OUI_MASK;
  3321. if (phyid == PHY_BCM_OUI_1 ||
  3322. phyid == PHY_BCM_OUI_2 ||
  3323. phyid == PHY_BCM_OUI_3)
  3324. do_low_power = true;
  3325. }
  3326. }
  3327. } else {
  3328. do_low_power = true;
  3329. if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER))
  3330. tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
  3331. if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
  3332. tg3_setup_phy(tp, false);
  3333. }
  3334. if (tg3_asic_rev(tp) == ASIC_REV_5906) {
  3335. u32 val;
  3336. val = tr32(GRC_VCPU_EXT_CTRL);
  3337. tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
  3338. } else if (!tg3_flag(tp, ENABLE_ASF)) {
  3339. int i;
  3340. u32 val;
  3341. for (i = 0; i < 200; i++) {
  3342. tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
  3343. if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
  3344. break;
  3345. msleep(1);
  3346. }
  3347. }
  3348. if (tg3_flag(tp, WOL_CAP))
  3349. tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
  3350. WOL_DRV_STATE_SHUTDOWN |
  3351. WOL_DRV_WOL |
  3352. WOL_SET_MAGIC_PKT);
  3353. if (device_should_wake) {
  3354. u32 mac_mode;
  3355. if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
  3356. if (do_low_power &&
  3357. !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
  3358. tg3_phy_auxctl_write(tp,
  3359. MII_TG3_AUXCTL_SHDWSEL_PWRCTL,
  3360. MII_TG3_AUXCTL_PCTL_WOL_EN |
  3361. MII_TG3_AUXCTL_PCTL_100TX_LPWR |
  3362. MII_TG3_AUXCTL_PCTL_CL_AB_TXDAC);
  3363. udelay(40);
  3364. }
  3365. if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
  3366. mac_mode = MAC_MODE_PORT_MODE_GMII;
  3367. else if (tp->phy_flags &
  3368. TG3_PHYFLG_KEEP_LINK_ON_PWRDN) {
  3369. if (tp->link_config.active_speed == SPEED_1000)
  3370. mac_mode = MAC_MODE_PORT_MODE_GMII;
  3371. else
  3372. mac_mode = MAC_MODE_PORT_MODE_MII;
  3373. } else
  3374. mac_mode = MAC_MODE_PORT_MODE_MII;
  3375. mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
  3376. if (tg3_asic_rev(tp) == ASIC_REV_5700) {
  3377. u32 speed = tg3_flag(tp, WOL_SPEED_100MB) ?
  3378. SPEED_100 : SPEED_10;
  3379. if (tg3_5700_link_polarity(tp, speed))
  3380. mac_mode |= MAC_MODE_LINK_POLARITY;
  3381. else
  3382. mac_mode &= ~MAC_MODE_LINK_POLARITY;
  3383. }
  3384. } else {
  3385. mac_mode = MAC_MODE_PORT_MODE_TBI;
  3386. }
  3387. if (!tg3_flag(tp, 5750_PLUS))
  3388. tw32(MAC_LED_CTRL, tp->led_ctrl);
  3389. mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
  3390. if ((tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS)) &&
  3391. (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)))
  3392. mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
  3393. if (tg3_flag(tp, ENABLE_APE))
  3394. mac_mode |= MAC_MODE_APE_TX_EN |
  3395. MAC_MODE_APE_RX_EN |
  3396. MAC_MODE_TDE_ENABLE;
  3397. tw32_f(MAC_MODE, mac_mode);
  3398. udelay(100);
  3399. tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
  3400. udelay(10);
  3401. }
  3402. if (!tg3_flag(tp, WOL_SPEED_100MB) &&
  3403. (tg3_asic_rev(tp) == ASIC_REV_5700 ||
  3404. tg3_asic_rev(tp) == ASIC_REV_5701)) {
  3405. u32 base_val;
  3406. base_val = tp->pci_clock_ctrl;
  3407. base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
  3408. CLOCK_CTRL_TXCLK_DISABLE);
  3409. tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
  3410. CLOCK_CTRL_PWRDOWN_PLL133, 40);
  3411. } else if (tg3_flag(tp, 5780_CLASS) ||
  3412. tg3_flag(tp, CPMU_PRESENT) ||
  3413. tg3_asic_rev(tp) == ASIC_REV_5906) {
  3414. /* do nothing */
  3415. } else if (!(tg3_flag(tp, 5750_PLUS) && tg3_flag(tp, ENABLE_ASF))) {
  3416. u32 newbits1, newbits2;
  3417. if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
  3418. tg3_asic_rev(tp) == ASIC_REV_5701) {
  3419. newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
  3420. CLOCK_CTRL_TXCLK_DISABLE |
  3421. CLOCK_CTRL_ALTCLK);
  3422. newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
  3423. } else if (tg3_flag(tp, 5705_PLUS)) {
  3424. newbits1 = CLOCK_CTRL_625_CORE;
  3425. newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
  3426. } else {
  3427. newbits1 = CLOCK_CTRL_ALTCLK;
  3428. newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
  3429. }
  3430. tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
  3431. 40);
  3432. tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
  3433. 40);
  3434. if (!tg3_flag(tp, 5705_PLUS)) {
  3435. u32 newbits3;
  3436. if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
  3437. tg3_asic_rev(tp) == ASIC_REV_5701) {
  3438. newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
  3439. CLOCK_CTRL_TXCLK_DISABLE |
  3440. CLOCK_CTRL_44MHZ_CORE);
  3441. } else {
  3442. newbits3 = CLOCK_CTRL_44MHZ_CORE;
  3443. }
  3444. tw32_wait_f(TG3PCI_CLOCK_CTRL,
  3445. tp->pci_clock_ctrl | newbits3, 40);
  3446. }
  3447. }
  3448. if (!(device_should_wake) && !tg3_flag(tp, ENABLE_ASF))
  3449. tg3_power_down_phy(tp, do_low_power);
  3450. tg3_frob_aux_power(tp, true);
  3451. /* Workaround for unstable PLL clock */
  3452. if ((!tg3_flag(tp, IS_SSB_CORE)) &&
  3453. ((tg3_chip_rev(tp) == CHIPREV_5750_AX) ||
  3454. (tg3_chip_rev(tp) == CHIPREV_5750_BX))) {
  3455. u32 val = tr32(0x7d00);
  3456. val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
  3457. tw32(0x7d00, val);
  3458. if (!tg3_flag(tp, ENABLE_ASF)) {
  3459. int err;
  3460. err = tg3_nvram_lock(tp);
  3461. tg3_halt_cpu(tp, RX_CPU_BASE);
  3462. if (!err)
  3463. tg3_nvram_unlock(tp);
  3464. }
  3465. }
  3466. tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
  3467. tg3_ape_driver_state_change(tp, RESET_KIND_SHUTDOWN);
  3468. return 0;
  3469. }
  3470. static void tg3_power_down(struct tg3 *tp)
  3471. {
  3472. pci_wake_from_d3(tp->pdev, tg3_flag(tp, WOL_ENABLE));
  3473. pci_set_power_state(tp->pdev, PCI_D3hot);
  3474. }
  3475. static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
  3476. {
  3477. switch (val & MII_TG3_AUX_STAT_SPDMASK) {
  3478. case MII_TG3_AUX_STAT_10HALF:
  3479. *speed = SPEED_10;
  3480. *duplex = DUPLEX_HALF;
  3481. break;
  3482. case MII_TG3_AUX_STAT_10FULL:
  3483. *speed = SPEED_10;
  3484. *duplex = DUPLEX_FULL;
  3485. break;
  3486. case MII_TG3_AUX_STAT_100HALF:
  3487. *speed = SPEED_100;
  3488. *duplex = DUPLEX_HALF;
  3489. break;
  3490. case MII_TG3_AUX_STAT_100FULL:
  3491. *speed = SPEED_100;
  3492. *duplex = DUPLEX_FULL;
  3493. break;
  3494. case MII_TG3_AUX_STAT_1000HALF:
  3495. *speed = SPEED_1000;
  3496. *duplex = DUPLEX_HALF;
  3497. break;
  3498. case MII_TG3_AUX_STAT_1000FULL:
  3499. *speed = SPEED_1000;
  3500. *duplex = DUPLEX_FULL;
  3501. break;
  3502. default:
  3503. if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
  3504. *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
  3505. SPEED_10;
  3506. *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
  3507. DUPLEX_HALF;
  3508. break;
  3509. }
  3510. *speed = SPEED_UNKNOWN;
  3511. *duplex = DUPLEX_UNKNOWN;
  3512. break;
  3513. }
  3514. }
  3515. static int tg3_phy_autoneg_cfg(struct tg3 *tp, u32 advertise, u32 flowctrl)
  3516. {
  3517. int err = 0;
  3518. u32 val, new_adv;
  3519. new_adv = ADVERTISE_CSMA;
  3520. new_adv |= ethtool_adv_to_mii_adv_t(advertise) & ADVERTISE_ALL;
  3521. new_adv |= mii_advertise_flowctrl(flowctrl);
  3522. err = tg3_writephy(tp, MII_ADVERTISE, new_adv);
  3523. if (err)
  3524. goto done;
  3525. if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
  3526. new_adv = ethtool_adv_to_mii_ctrl1000_t(advertise);
  3527. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
  3528. tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0)
  3529. new_adv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
  3530. err = tg3_writephy(tp, MII_CTRL1000, new_adv);
  3531. if (err)
  3532. goto done;
  3533. }
  3534. if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
  3535. goto done;
  3536. tw32(TG3_CPMU_EEE_MODE,
  3537. tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
  3538. err = tg3_phy_toggle_auxctl_smdsp(tp, true);
  3539. if (!err) {
  3540. u32 err2;
  3541. val = 0;
  3542. /* Advertise 100-BaseTX EEE ability */
  3543. if (advertise & ADVERTISED_100baseT_Full)
  3544. val |= MDIO_AN_EEE_ADV_100TX;
  3545. /* Advertise 1000-BaseT EEE ability */
  3546. if (advertise & ADVERTISED_1000baseT_Full)
  3547. val |= MDIO_AN_EEE_ADV_1000T;
  3548. if (!tp->eee.eee_enabled) {
  3549. val = 0;
  3550. tp->eee.advertised = 0;
  3551. } else {
  3552. tp->eee.advertised = advertise &
  3553. (ADVERTISED_100baseT_Full |
  3554. ADVERTISED_1000baseT_Full);
  3555. }
  3556. err = tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
  3557. if (err)
  3558. val = 0;
  3559. switch (tg3_asic_rev(tp)) {
  3560. case ASIC_REV_5717:
  3561. case ASIC_REV_57765:
  3562. case ASIC_REV_57766:
  3563. case ASIC_REV_5719:
  3564. /* If we advertised any eee advertisements above... */
  3565. if (val)
  3566. val = MII_TG3_DSP_TAP26_ALNOKO |
  3567. MII_TG3_DSP_TAP26_RMRXSTO |
  3568. MII_TG3_DSP_TAP26_OPCSINPT;
  3569. tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
  3570. /* Fall through */
  3571. case ASIC_REV_5720:
  3572. case ASIC_REV_5762:
  3573. if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
  3574. tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
  3575. MII_TG3_DSP_CH34TP2_HIBW01);
  3576. }
  3577. err2 = tg3_phy_toggle_auxctl_smdsp(tp, false);
  3578. if (!err)
  3579. err = err2;
  3580. }
  3581. done:
  3582. return err;
  3583. }
  3584. static void tg3_phy_copper_begin(struct tg3 *tp)
  3585. {
  3586. if (tp->link_config.autoneg == AUTONEG_ENABLE ||
  3587. (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
  3588. u32 adv, fc;
  3589. if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
  3590. !(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN)) {
  3591. adv = ADVERTISED_10baseT_Half |
  3592. ADVERTISED_10baseT_Full;
  3593. if (tg3_flag(tp, WOL_SPEED_100MB))
  3594. adv |= ADVERTISED_100baseT_Half |
  3595. ADVERTISED_100baseT_Full;
  3596. if (tp->phy_flags & TG3_PHYFLG_1G_ON_VAUX_OK) {
  3597. if (!(tp->phy_flags &
  3598. TG3_PHYFLG_DISABLE_1G_HD_ADV))
  3599. adv |= ADVERTISED_1000baseT_Half;
  3600. adv |= ADVERTISED_1000baseT_Full;
  3601. }
  3602. fc = FLOW_CTRL_TX | FLOW_CTRL_RX;
  3603. } else {
  3604. adv = tp->link_config.advertising;
  3605. if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
  3606. adv &= ~(ADVERTISED_1000baseT_Half |
  3607. ADVERTISED_1000baseT_Full);
  3608. fc = tp->link_config.flowctrl;
  3609. }
  3610. tg3_phy_autoneg_cfg(tp, adv, fc);
  3611. if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
  3612. (tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN)) {
  3613. /* Normally during power down we want to autonegotiate
  3614. * the lowest possible speed for WOL. However, to avoid
  3615. * link flap, we leave it untouched.
  3616. */
  3617. return;
  3618. }
  3619. tg3_writephy(tp, MII_BMCR,
  3620. BMCR_ANENABLE | BMCR_ANRESTART);
  3621. } else {
  3622. int i;
  3623. u32 bmcr, orig_bmcr;
  3624. tp->link_config.active_speed = tp->link_config.speed;
  3625. tp->link_config.active_duplex = tp->link_config.duplex;
  3626. if (tg3_asic_rev(tp) == ASIC_REV_5714) {
  3627. /* With autoneg disabled, 5715 only links up when the
  3628. * advertisement register has the configured speed
  3629. * enabled.
  3630. */
  3631. tg3_writephy(tp, MII_ADVERTISE, ADVERTISE_ALL);
  3632. }
  3633. bmcr = 0;
  3634. switch (tp->link_config.speed) {
  3635. default:
  3636. case SPEED_10:
  3637. break;
  3638. case SPEED_100:
  3639. bmcr |= BMCR_SPEED100;
  3640. break;
  3641. case SPEED_1000:
  3642. bmcr |= BMCR_SPEED1000;
  3643. break;
  3644. }
  3645. if (tp->link_config.duplex == DUPLEX_FULL)
  3646. bmcr |= BMCR_FULLDPLX;
  3647. if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
  3648. (bmcr != orig_bmcr)) {
  3649. tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
  3650. for (i = 0; i < 1500; i++) {
  3651. u32 tmp;
  3652. udelay(10);
  3653. if (tg3_readphy(tp, MII_BMSR, &tmp) ||
  3654. tg3_readphy(tp, MII_BMSR, &tmp))
  3655. continue;
  3656. if (!(tmp & BMSR_LSTATUS)) {
  3657. udelay(40);
  3658. break;
  3659. }
  3660. }
  3661. tg3_writephy(tp, MII_BMCR, bmcr);
  3662. udelay(40);
  3663. }
  3664. }
  3665. }
  3666. static int tg3_phy_pull_config(struct tg3 *tp)
  3667. {
  3668. int err;
  3669. u32 val;
  3670. err = tg3_readphy(tp, MII_BMCR, &val);
  3671. if (err)
  3672. goto done;
  3673. if (!(val & BMCR_ANENABLE)) {
  3674. tp->link_config.autoneg = AUTONEG_DISABLE;
  3675. tp->link_config.advertising = 0;
  3676. tg3_flag_clear(tp, PAUSE_AUTONEG);
  3677. err = -EIO;
  3678. switch (val & (BMCR_SPEED1000 | BMCR_SPEED100)) {
  3679. case 0:
  3680. if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
  3681. goto done;
  3682. tp->link_config.speed = SPEED_10;
  3683. break;
  3684. case BMCR_SPEED100:
  3685. if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
  3686. goto done;
  3687. tp->link_config.speed = SPEED_100;
  3688. break;
  3689. case BMCR_SPEED1000:
  3690. if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
  3691. tp->link_config.speed = SPEED_1000;
  3692. break;
  3693. }
  3694. /* Fall through */
  3695. default:
  3696. goto done;
  3697. }
  3698. if (val & BMCR_FULLDPLX)
  3699. tp->link_config.duplex = DUPLEX_FULL;
  3700. else
  3701. tp->link_config.duplex = DUPLEX_HALF;
  3702. tp->link_config.flowctrl = FLOW_CTRL_RX | FLOW_CTRL_TX;
  3703. err = 0;
  3704. goto done;
  3705. }
  3706. tp->link_config.autoneg = AUTONEG_ENABLE;
  3707. tp->link_config.advertising = ADVERTISED_Autoneg;
  3708. tg3_flag_set(tp, PAUSE_AUTONEG);
  3709. if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
  3710. u32 adv;
  3711. err = tg3_readphy(tp, MII_ADVERTISE, &val);
  3712. if (err)
  3713. goto done;
  3714. adv = mii_adv_to_ethtool_adv_t(val & ADVERTISE_ALL);
  3715. tp->link_config.advertising |= adv | ADVERTISED_TP;
  3716. tp->link_config.flowctrl = tg3_decode_flowctrl_1000T(val);
  3717. } else {
  3718. tp->link_config.advertising |= ADVERTISED_FIBRE;
  3719. }
  3720. if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
  3721. u32 adv;
  3722. if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
  3723. err = tg3_readphy(tp, MII_CTRL1000, &val);
  3724. if (err)
  3725. goto done;
  3726. adv = mii_ctrl1000_to_ethtool_adv_t(val);
  3727. } else {
  3728. err = tg3_readphy(tp, MII_ADVERTISE, &val);
  3729. if (err)
  3730. goto done;
  3731. adv = tg3_decode_flowctrl_1000X(val);
  3732. tp->link_config.flowctrl = adv;
  3733. val &= (ADVERTISE_1000XHALF | ADVERTISE_1000XFULL);
  3734. adv = mii_adv_to_ethtool_adv_x(val);
  3735. }
  3736. tp->link_config.advertising |= adv;
  3737. }
  3738. done:
  3739. return err;
  3740. }
  3741. static int tg3_init_5401phy_dsp(struct tg3 *tp)
  3742. {
  3743. int err;
  3744. /* Turn off tap power management. */
  3745. /* Set Extended packet length bit */
  3746. err = tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
  3747. err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
  3748. err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
  3749. err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
  3750. err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
  3751. err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
  3752. udelay(40);
  3753. return err;
  3754. }
  3755. static bool tg3_phy_eee_config_ok(struct tg3 *tp)
  3756. {
  3757. struct ethtool_eee eee;
  3758. if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
  3759. return true;
  3760. tg3_eee_pull_config(tp, &eee);
  3761. if (tp->eee.eee_enabled) {
  3762. if (tp->eee.advertised != eee.advertised ||
  3763. tp->eee.tx_lpi_timer != eee.tx_lpi_timer ||
  3764. tp->eee.tx_lpi_enabled != eee.tx_lpi_enabled)
  3765. return false;
  3766. } else {
  3767. /* EEE is disabled but we're advertising */
  3768. if (eee.advertised)
  3769. return false;
  3770. }
  3771. return true;
  3772. }
  3773. static bool tg3_phy_copper_an_config_ok(struct tg3 *tp, u32 *lcladv)
  3774. {
  3775. u32 advmsk, tgtadv, advertising;
  3776. advertising = tp->link_config.advertising;
  3777. tgtadv = ethtool_adv_to_mii_adv_t(advertising) & ADVERTISE_ALL;
  3778. advmsk = ADVERTISE_ALL;
  3779. if (tp->link_config.active_duplex == DUPLEX_FULL) {
  3780. tgtadv |= mii_advertise_flowctrl(tp->link_config.flowctrl);
  3781. advmsk |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
  3782. }
  3783. if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
  3784. return false;
  3785. if ((*lcladv & advmsk) != tgtadv)
  3786. return false;
  3787. if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
  3788. u32 tg3_ctrl;
  3789. tgtadv = ethtool_adv_to_mii_ctrl1000_t(advertising);
  3790. if (tg3_readphy(tp, MII_CTRL1000, &tg3_ctrl))
  3791. return false;
  3792. if (tgtadv &&
  3793. (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
  3794. tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0)) {
  3795. tgtadv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
  3796. tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL |
  3797. CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
  3798. } else {
  3799. tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL);
  3800. }
  3801. if (tg3_ctrl != tgtadv)
  3802. return false;
  3803. }
  3804. return true;
  3805. }
  3806. static bool tg3_phy_copper_fetch_rmtadv(struct tg3 *tp, u32 *rmtadv)
  3807. {
  3808. u32 lpeth = 0;
  3809. if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
  3810. u32 val;
  3811. if (tg3_readphy(tp, MII_STAT1000, &val))
  3812. return false;
  3813. lpeth = mii_stat1000_to_ethtool_lpa_t(val);
  3814. }
  3815. if (tg3_readphy(tp, MII_LPA, rmtadv))
  3816. return false;
  3817. lpeth |= mii_lpa_to_ethtool_lpa_t(*rmtadv);
  3818. tp->link_config.rmt_adv = lpeth;
  3819. return true;
  3820. }
  3821. static bool tg3_test_and_report_link_chg(struct tg3 *tp, bool curr_link_up)
  3822. {
  3823. if (curr_link_up != tp->link_up) {
  3824. if (curr_link_up) {
  3825. netif_carrier_on(tp->dev);
  3826. } else {
  3827. netif_carrier_off(tp->dev);
  3828. if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
  3829. tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
  3830. }
  3831. tg3_link_report(tp);
  3832. return true;
  3833. }
  3834. return false;
  3835. }
  3836. static void tg3_clear_mac_status(struct tg3 *tp)
  3837. {
  3838. tw32(MAC_EVENT, 0);
  3839. tw32_f(MAC_STATUS,
  3840. MAC_STATUS_SYNC_CHANGED |
  3841. MAC_STATUS_CFG_CHANGED |
  3842. MAC_STATUS_MI_COMPLETION |
  3843. MAC_STATUS_LNKSTATE_CHANGED);
  3844. udelay(40);
  3845. }
  3846. static void tg3_setup_eee(struct tg3 *tp)
  3847. {
  3848. u32 val;
  3849. val = TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
  3850. TG3_CPMU_EEE_LNKIDL_UART_IDL;
  3851. if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0)
  3852. val |= TG3_CPMU_EEE_LNKIDL_APE_TX_MT;
  3853. tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL, val);
  3854. tw32_f(TG3_CPMU_EEE_CTRL,
  3855. TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
  3856. val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
  3857. (tp->eee.tx_lpi_enabled ? TG3_CPMU_EEEMD_LPI_IN_TX : 0) |
  3858. TG3_CPMU_EEEMD_LPI_IN_RX |
  3859. TG3_CPMU_EEEMD_EEE_ENABLE;
  3860. if (tg3_asic_rev(tp) != ASIC_REV_5717)
  3861. val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
  3862. if (tg3_flag(tp, ENABLE_APE))
  3863. val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
  3864. tw32_f(TG3_CPMU_EEE_MODE, tp->eee.eee_enabled ? val : 0);
  3865. tw32_f(TG3_CPMU_EEE_DBTMR1,
  3866. TG3_CPMU_DBTMR1_PCIEXIT_2047US |
  3867. (tp->eee.tx_lpi_timer & 0xffff));
  3868. tw32_f(TG3_CPMU_EEE_DBTMR2,
  3869. TG3_CPMU_DBTMR2_APE_TX_2047US |
  3870. TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
  3871. }
  3872. static int tg3_setup_copper_phy(struct tg3 *tp, bool force_reset)
  3873. {
  3874. bool current_link_up;
  3875. u32 bmsr, val;
  3876. u32 lcl_adv, rmt_adv;
  3877. u16 current_speed;
  3878. u8 current_duplex;
  3879. int i, err;
  3880. tg3_clear_mac_status(tp);
  3881. if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
  3882. tw32_f(MAC_MI_MODE,
  3883. (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
  3884. udelay(80);
  3885. }
  3886. tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, 0);
  3887. /* Some third-party PHYs need to be reset on link going
  3888. * down.
  3889. */
  3890. if ((tg3_asic_rev(tp) == ASIC_REV_5703 ||
  3891. tg3_asic_rev(tp) == ASIC_REV_5704 ||
  3892. tg3_asic_rev(tp) == ASIC_REV_5705) &&
  3893. tp->link_up) {
  3894. tg3_readphy(tp, MII_BMSR, &bmsr);
  3895. if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
  3896. !(bmsr & BMSR_LSTATUS))
  3897. force_reset = true;
  3898. }
  3899. if (force_reset)
  3900. tg3_phy_reset(tp);
  3901. if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
  3902. tg3_readphy(tp, MII_BMSR, &bmsr);
  3903. if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
  3904. !tg3_flag(tp, INIT_COMPLETE))
  3905. bmsr = 0;
  3906. if (!(bmsr & BMSR_LSTATUS)) {
  3907. err = tg3_init_5401phy_dsp(tp);
  3908. if (err)
  3909. return err;
  3910. tg3_readphy(tp, MII_BMSR, &bmsr);
  3911. for (i = 0; i < 1000; i++) {
  3912. udelay(10);
  3913. if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
  3914. (bmsr & BMSR_LSTATUS)) {
  3915. udelay(40);
  3916. break;
  3917. }
  3918. }
  3919. if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
  3920. TG3_PHY_REV_BCM5401_B0 &&
  3921. !(bmsr & BMSR_LSTATUS) &&
  3922. tp->link_config.active_speed == SPEED_1000) {
  3923. err = tg3_phy_reset(tp);
  3924. if (!err)
  3925. err = tg3_init_5401phy_dsp(tp);
  3926. if (err)
  3927. return err;
  3928. }
  3929. }
  3930. } else if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
  3931. tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0) {
  3932. /* 5701 {A0,B0} CRC bug workaround */
  3933. tg3_writephy(tp, 0x15, 0x0a75);
  3934. tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
  3935. tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
  3936. tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
  3937. }
  3938. /* Clear pending interrupts... */
  3939. tg3_readphy(tp, MII_TG3_ISTAT, &val);
  3940. tg3_readphy(tp, MII_TG3_ISTAT, &val);
  3941. if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
  3942. tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
  3943. else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
  3944. tg3_writephy(tp, MII_TG3_IMASK, ~0);
  3945. if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
  3946. tg3_asic_rev(tp) == ASIC_REV_5701) {
  3947. if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
  3948. tg3_writephy(tp, MII_TG3_EXT_CTRL,
  3949. MII_TG3_EXT_CTRL_LNK3_LED_MODE);
  3950. else
  3951. tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
  3952. }
  3953. current_link_up = false;
  3954. current_speed = SPEED_UNKNOWN;
  3955. current_duplex = DUPLEX_UNKNOWN;
  3956. tp->phy_flags &= ~TG3_PHYFLG_MDIX_STATE;
  3957. tp->link_config.rmt_adv = 0;
  3958. if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
  3959. err = tg3_phy_auxctl_read(tp,
  3960. MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
  3961. &val);
  3962. if (!err && !(val & (1 << 10))) {
  3963. tg3_phy_auxctl_write(tp,
  3964. MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
  3965. val | (1 << 10));
  3966. goto relink;
  3967. }
  3968. }
  3969. bmsr = 0;
  3970. for (i = 0; i < 100; i++) {
  3971. tg3_readphy(tp, MII_BMSR, &bmsr);
  3972. if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
  3973. (bmsr & BMSR_LSTATUS))
  3974. break;
  3975. udelay(40);
  3976. }
  3977. if (bmsr & BMSR_LSTATUS) {
  3978. u32 aux_stat, bmcr;
  3979. tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
  3980. for (i = 0; i < 2000; i++) {
  3981. udelay(10);
  3982. if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
  3983. aux_stat)
  3984. break;
  3985. }
  3986. tg3_aux_stat_to_speed_duplex(tp, aux_stat,
  3987. &current_speed,
  3988. &current_duplex);
  3989. bmcr = 0;
  3990. for (i = 0; i < 200; i++) {
  3991. tg3_readphy(tp, MII_BMCR, &bmcr);
  3992. if (tg3_readphy(tp, MII_BMCR, &bmcr))
  3993. continue;
  3994. if (bmcr && bmcr != 0x7fff)
  3995. break;
  3996. udelay(10);
  3997. }
  3998. lcl_adv = 0;
  3999. rmt_adv = 0;
  4000. tp->link_config.active_speed = current_speed;
  4001. tp->link_config.active_duplex = current_duplex;
  4002. if (tp->link_config.autoneg == AUTONEG_ENABLE) {
  4003. bool eee_config_ok = tg3_phy_eee_config_ok(tp);
  4004. if ((bmcr & BMCR_ANENABLE) &&
  4005. eee_config_ok &&
  4006. tg3_phy_copper_an_config_ok(tp, &lcl_adv) &&
  4007. tg3_phy_copper_fetch_rmtadv(tp, &rmt_adv))
  4008. current_link_up = true;
  4009. /* EEE settings changes take effect only after a phy
  4010. * reset. If we have skipped a reset due to Link Flap
  4011. * Avoidance being enabled, do it now.
  4012. */
  4013. if (!eee_config_ok &&
  4014. (tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN) &&
  4015. !force_reset) {
  4016. tg3_setup_eee(tp);
  4017. tg3_phy_reset(tp);
  4018. }
  4019. } else {
  4020. if (!(bmcr & BMCR_ANENABLE) &&
  4021. tp->link_config.speed == current_speed &&
  4022. tp->link_config.duplex == current_duplex) {
  4023. current_link_up = true;
  4024. }
  4025. }
  4026. if (current_link_up &&
  4027. tp->link_config.active_duplex == DUPLEX_FULL) {
  4028. u32 reg, bit;
  4029. if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
  4030. reg = MII_TG3_FET_GEN_STAT;
  4031. bit = MII_TG3_FET_GEN_STAT_MDIXSTAT;
  4032. } else {
  4033. reg = MII_TG3_EXT_STAT;
  4034. bit = MII_TG3_EXT_STAT_MDIX;
  4035. }
  4036. if (!tg3_readphy(tp, reg, &val) && (val & bit))
  4037. tp->phy_flags |= TG3_PHYFLG_MDIX_STATE;
  4038. tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
  4039. }
  4040. }
  4041. relink:
  4042. if (!current_link_up || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
  4043. tg3_phy_copper_begin(tp);
  4044. if (tg3_flag(tp, ROBOSWITCH)) {
  4045. current_link_up = true;
  4046. /* FIXME: when BCM5325 switch is used use 100 MBit/s */
  4047. current_speed = SPEED_1000;
  4048. current_duplex = DUPLEX_FULL;
  4049. tp->link_config.active_speed = current_speed;
  4050. tp->link_config.active_duplex = current_duplex;
  4051. }
  4052. tg3_readphy(tp, MII_BMSR, &bmsr);
  4053. if ((!tg3_readphy(tp, MII_BMSR, &bmsr) && (bmsr & BMSR_LSTATUS)) ||
  4054. (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
  4055. current_link_up = true;
  4056. }
  4057. tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
  4058. if (current_link_up) {
  4059. if (tp->link_config.active_speed == SPEED_100 ||
  4060. tp->link_config.active_speed == SPEED_10)
  4061. tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
  4062. else
  4063. tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
  4064. } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
  4065. tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
  4066. else
  4067. tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
  4068. /* In order for the 5750 core in BCM4785 chip to work properly
  4069. * in RGMII mode, the Led Control Register must be set up.
  4070. */
  4071. if (tg3_flag(tp, RGMII_MODE)) {
  4072. u32 led_ctrl = tr32(MAC_LED_CTRL);
  4073. led_ctrl &= ~(LED_CTRL_1000MBPS_ON | LED_CTRL_100MBPS_ON);
  4074. if (tp->link_config.active_speed == SPEED_10)
  4075. led_ctrl |= LED_CTRL_LNKLED_OVERRIDE;
  4076. else if (tp->link_config.active_speed == SPEED_100)
  4077. led_ctrl |= (LED_CTRL_LNKLED_OVERRIDE |
  4078. LED_CTRL_100MBPS_ON);
  4079. else if (tp->link_config.active_speed == SPEED_1000)
  4080. led_ctrl |= (LED_CTRL_LNKLED_OVERRIDE |
  4081. LED_CTRL_1000MBPS_ON);
  4082. tw32(MAC_LED_CTRL, led_ctrl);
  4083. udelay(40);
  4084. }
  4085. tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
  4086. if (tp->link_config.active_duplex == DUPLEX_HALF)
  4087. tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
  4088. if (tg3_asic_rev(tp) == ASIC_REV_5700) {
  4089. if (current_link_up &&
  4090. tg3_5700_link_polarity(tp, tp->link_config.active_speed))
  4091. tp->mac_mode |= MAC_MODE_LINK_POLARITY;
  4092. else
  4093. tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
  4094. }
  4095. /* ??? Without this setting Netgear GA302T PHY does not
  4096. * ??? send/receive packets...
  4097. */
  4098. if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
  4099. tg3_chip_rev_id(tp) == CHIPREV_ID_5700_ALTIMA) {
  4100. tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
  4101. tw32_f(MAC_MI_MODE, tp->mi_mode);
  4102. udelay(80);
  4103. }
  4104. tw32_f(MAC_MODE, tp->mac_mode);
  4105. udelay(40);
  4106. tg3_phy_eee_adjust(tp, current_link_up);
  4107. if (tg3_flag(tp, USE_LINKCHG_REG)) {
  4108. /* Polled via timer. */
  4109. tw32_f(MAC_EVENT, 0);
  4110. } else {
  4111. tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
  4112. }
  4113. udelay(40);
  4114. if (tg3_asic_rev(tp) == ASIC_REV_5700 &&
  4115. current_link_up &&
  4116. tp->link_config.active_speed == SPEED_1000 &&
  4117. (tg3_flag(tp, PCIX_MODE) || tg3_flag(tp, PCI_HIGH_SPEED))) {
  4118. udelay(120);
  4119. tw32_f(MAC_STATUS,
  4120. (MAC_STATUS_SYNC_CHANGED |
  4121. MAC_STATUS_CFG_CHANGED));
  4122. udelay(40);
  4123. tg3_write_mem(tp,
  4124. NIC_SRAM_FIRMWARE_MBOX,
  4125. NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
  4126. }
  4127. /* Prevent send BD corruption. */
  4128. if (tg3_flag(tp, CLKREQ_BUG)) {
  4129. if (tp->link_config.active_speed == SPEED_100 ||
  4130. tp->link_config.active_speed == SPEED_10)
  4131. pcie_capability_clear_word(tp->pdev, PCI_EXP_LNKCTL,
  4132. PCI_EXP_LNKCTL_CLKREQ_EN);
  4133. else
  4134. pcie_capability_set_word(tp->pdev, PCI_EXP_LNKCTL,
  4135. PCI_EXP_LNKCTL_CLKREQ_EN);
  4136. }
  4137. tg3_test_and_report_link_chg(tp, current_link_up);
  4138. return 0;
  4139. }
  4140. struct tg3_fiber_aneginfo {
  4141. int state;
  4142. #define ANEG_STATE_UNKNOWN 0
  4143. #define ANEG_STATE_AN_ENABLE 1
  4144. #define ANEG_STATE_RESTART_INIT 2
  4145. #define ANEG_STATE_RESTART 3
  4146. #define ANEG_STATE_DISABLE_LINK_OK 4
  4147. #define ANEG_STATE_ABILITY_DETECT_INIT 5
  4148. #define ANEG_STATE_ABILITY_DETECT 6
  4149. #define ANEG_STATE_ACK_DETECT_INIT 7
  4150. #define ANEG_STATE_ACK_DETECT 8
  4151. #define ANEG_STATE_COMPLETE_ACK_INIT 9
  4152. #define ANEG_STATE_COMPLETE_ACK 10
  4153. #define ANEG_STATE_IDLE_DETECT_INIT 11
  4154. #define ANEG_STATE_IDLE_DETECT 12
  4155. #define ANEG_STATE_LINK_OK 13
  4156. #define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
  4157. #define ANEG_STATE_NEXT_PAGE_WAIT 15
  4158. u32 flags;
  4159. #define MR_AN_ENABLE 0x00000001
  4160. #define MR_RESTART_AN 0x00000002
  4161. #define MR_AN_COMPLETE 0x00000004
  4162. #define MR_PAGE_RX 0x00000008
  4163. #define MR_NP_LOADED 0x00000010
  4164. #define MR_TOGGLE_TX 0x00000020
  4165. #define MR_LP_ADV_FULL_DUPLEX 0x00000040
  4166. #define MR_LP_ADV_HALF_DUPLEX 0x00000080
  4167. #define MR_LP_ADV_SYM_PAUSE 0x00000100
  4168. #define MR_LP_ADV_ASYM_PAUSE 0x00000200
  4169. #define MR_LP_ADV_REMOTE_FAULT1 0x00000400
  4170. #define MR_LP_ADV_REMOTE_FAULT2 0x00000800
  4171. #define MR_LP_ADV_NEXT_PAGE 0x00001000
  4172. #define MR_TOGGLE_RX 0x00002000
  4173. #define MR_NP_RX 0x00004000
  4174. #define MR_LINK_OK 0x80000000
  4175. unsigned long link_time, cur_time;
  4176. u32 ability_match_cfg;
  4177. int ability_match_count;
  4178. char ability_match, idle_match, ack_match;
  4179. u32 txconfig, rxconfig;
  4180. #define ANEG_CFG_NP 0x00000080
  4181. #define ANEG_CFG_ACK 0x00000040
  4182. #define ANEG_CFG_RF2 0x00000020
  4183. #define ANEG_CFG_RF1 0x00000010
  4184. #define ANEG_CFG_PS2 0x00000001
  4185. #define ANEG_CFG_PS1 0x00008000
  4186. #define ANEG_CFG_HD 0x00004000
  4187. #define ANEG_CFG_FD 0x00002000
  4188. #define ANEG_CFG_INVAL 0x00001f06
  4189. };
  4190. #define ANEG_OK 0
  4191. #define ANEG_DONE 1
  4192. #define ANEG_TIMER_ENAB 2
  4193. #define ANEG_FAILED -1
  4194. #define ANEG_STATE_SETTLE_TIME 10000
  4195. static int tg3_fiber_aneg_smachine(struct tg3 *tp,
  4196. struct tg3_fiber_aneginfo *ap)
  4197. {
  4198. u16 flowctrl;
  4199. unsigned long delta;
  4200. u32 rx_cfg_reg;
  4201. int ret;
  4202. if (ap->state == ANEG_STATE_UNKNOWN) {
  4203. ap->rxconfig = 0;
  4204. ap->link_time = 0;
  4205. ap->cur_time = 0;
  4206. ap->ability_match_cfg = 0;
  4207. ap->ability_match_count = 0;
  4208. ap->ability_match = 0;
  4209. ap->idle_match = 0;
  4210. ap->ack_match = 0;
  4211. }
  4212. ap->cur_time++;
  4213. if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
  4214. rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
  4215. if (rx_cfg_reg != ap->ability_match_cfg) {
  4216. ap->ability_match_cfg = rx_cfg_reg;
  4217. ap->ability_match = 0;
  4218. ap->ability_match_count = 0;
  4219. } else {
  4220. if (++ap->ability_match_count > 1) {
  4221. ap->ability_match = 1;
  4222. ap->ability_match_cfg = rx_cfg_reg;
  4223. }
  4224. }
  4225. if (rx_cfg_reg & ANEG_CFG_ACK)
  4226. ap->ack_match = 1;
  4227. else
  4228. ap->ack_match = 0;
  4229. ap->idle_match = 0;
  4230. } else {
  4231. ap->idle_match = 1;
  4232. ap->ability_match_cfg = 0;
  4233. ap->ability_match_count = 0;
  4234. ap->ability_match = 0;
  4235. ap->ack_match = 0;
  4236. rx_cfg_reg = 0;
  4237. }
  4238. ap->rxconfig = rx_cfg_reg;
  4239. ret = ANEG_OK;
  4240. switch (ap->state) {
  4241. case ANEG_STATE_UNKNOWN:
  4242. if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
  4243. ap->state = ANEG_STATE_AN_ENABLE;
  4244. /* fallthru */
  4245. case ANEG_STATE_AN_ENABLE:
  4246. ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
  4247. if (ap->flags & MR_AN_ENABLE) {
  4248. ap->link_time = 0;
  4249. ap->cur_time = 0;
  4250. ap->ability_match_cfg = 0;
  4251. ap->ability_match_count = 0;
  4252. ap->ability_match = 0;
  4253. ap->idle_match = 0;
  4254. ap->ack_match = 0;
  4255. ap->state = ANEG_STATE_RESTART_INIT;
  4256. } else {
  4257. ap->state = ANEG_STATE_DISABLE_LINK_OK;
  4258. }
  4259. break;
  4260. case ANEG_STATE_RESTART_INIT:
  4261. ap->link_time = ap->cur_time;
  4262. ap->flags &= ~(MR_NP_LOADED);
  4263. ap->txconfig = 0;
  4264. tw32(MAC_TX_AUTO_NEG, 0);
  4265. tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
  4266. tw32_f(MAC_MODE, tp->mac_mode);
  4267. udelay(40);
  4268. ret = ANEG_TIMER_ENAB;
  4269. ap->state = ANEG_STATE_RESTART;
  4270. /* fallthru */
  4271. case ANEG_STATE_RESTART:
  4272. delta = ap->cur_time - ap->link_time;
  4273. if (delta > ANEG_STATE_SETTLE_TIME)
  4274. ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
  4275. else
  4276. ret = ANEG_TIMER_ENAB;
  4277. break;
  4278. case ANEG_STATE_DISABLE_LINK_OK:
  4279. ret = ANEG_DONE;
  4280. break;
  4281. case ANEG_STATE_ABILITY_DETECT_INIT:
  4282. ap->flags &= ~(MR_TOGGLE_TX);
  4283. ap->txconfig = ANEG_CFG_FD;
  4284. flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
  4285. if (flowctrl & ADVERTISE_1000XPAUSE)
  4286. ap->txconfig |= ANEG_CFG_PS1;
  4287. if (flowctrl & ADVERTISE_1000XPSE_ASYM)
  4288. ap->txconfig |= ANEG_CFG_PS2;
  4289. tw32(MAC_TX_AUTO_NEG, ap->txconfig);
  4290. tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
  4291. tw32_f(MAC_MODE, tp->mac_mode);
  4292. udelay(40);
  4293. ap->state = ANEG_STATE_ABILITY_DETECT;
  4294. break;
  4295. case ANEG_STATE_ABILITY_DETECT:
  4296. if (ap->ability_match != 0 && ap->rxconfig != 0)
  4297. ap->state = ANEG_STATE_ACK_DETECT_INIT;
  4298. break;
  4299. case ANEG_STATE_ACK_DETECT_INIT:
  4300. ap->txconfig |= ANEG_CFG_ACK;
  4301. tw32(MAC_TX_AUTO_NEG, ap->txconfig);
  4302. tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
  4303. tw32_f(MAC_MODE, tp->mac_mode);
  4304. udelay(40);
  4305. ap->state = ANEG_STATE_ACK_DETECT;
  4306. /* fallthru */
  4307. case ANEG_STATE_ACK_DETECT:
  4308. if (ap->ack_match != 0) {
  4309. if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
  4310. (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
  4311. ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
  4312. } else {
  4313. ap->state = ANEG_STATE_AN_ENABLE;
  4314. }
  4315. } else if (ap->ability_match != 0 &&
  4316. ap->rxconfig == 0) {
  4317. ap->state = ANEG_STATE_AN_ENABLE;
  4318. }
  4319. break;
  4320. case ANEG_STATE_COMPLETE_ACK_INIT:
  4321. if (ap->rxconfig & ANEG_CFG_INVAL) {
  4322. ret = ANEG_FAILED;
  4323. break;
  4324. }
  4325. ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
  4326. MR_LP_ADV_HALF_DUPLEX |
  4327. MR_LP_ADV_SYM_PAUSE |
  4328. MR_LP_ADV_ASYM_PAUSE |
  4329. MR_LP_ADV_REMOTE_FAULT1 |
  4330. MR_LP_ADV_REMOTE_FAULT2 |
  4331. MR_LP_ADV_NEXT_PAGE |
  4332. MR_TOGGLE_RX |
  4333. MR_NP_RX);
  4334. if (ap->rxconfig & ANEG_CFG_FD)
  4335. ap->flags |= MR_LP_ADV_FULL_DUPLEX;
  4336. if (ap->rxconfig & ANEG_CFG_HD)
  4337. ap->flags |= MR_LP_ADV_HALF_DUPLEX;
  4338. if (ap->rxconfig & ANEG_CFG_PS1)
  4339. ap->flags |= MR_LP_ADV_SYM_PAUSE;
  4340. if (ap->rxconfig & ANEG_CFG_PS2)
  4341. ap->flags |= MR_LP_ADV_ASYM_PAUSE;
  4342. if (ap->rxconfig & ANEG_CFG_RF1)
  4343. ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
  4344. if (ap->rxconfig & ANEG_CFG_RF2)
  4345. ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
  4346. if (ap->rxconfig & ANEG_CFG_NP)
  4347. ap->flags |= MR_LP_ADV_NEXT_PAGE;
  4348. ap->link_time = ap->cur_time;
  4349. ap->flags ^= (MR_TOGGLE_TX);
  4350. if (ap->rxconfig & 0x0008)
  4351. ap->flags |= MR_TOGGLE_RX;
  4352. if (ap->rxconfig & ANEG_CFG_NP)
  4353. ap->flags |= MR_NP_RX;
  4354. ap->flags |= MR_PAGE_RX;
  4355. ap->state = ANEG_STATE_COMPLETE_ACK;
  4356. ret = ANEG_TIMER_ENAB;
  4357. break;
  4358. case ANEG_STATE_COMPLETE_ACK:
  4359. if (ap->ability_match != 0 &&
  4360. ap->rxconfig == 0) {
  4361. ap->state = ANEG_STATE_AN_ENABLE;
  4362. break;
  4363. }
  4364. delta = ap->cur_time - ap->link_time;
  4365. if (delta > ANEG_STATE_SETTLE_TIME) {
  4366. if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
  4367. ap->state = ANEG_STATE_IDLE_DETECT_INIT;
  4368. } else {
  4369. if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
  4370. !(ap->flags & MR_NP_RX)) {
  4371. ap->state = ANEG_STATE_IDLE_DETECT_INIT;
  4372. } else {
  4373. ret = ANEG_FAILED;
  4374. }
  4375. }
  4376. }
  4377. break;
  4378. case ANEG_STATE_IDLE_DETECT_INIT:
  4379. ap->link_time = ap->cur_time;
  4380. tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
  4381. tw32_f(MAC_MODE, tp->mac_mode);
  4382. udelay(40);
  4383. ap->state = ANEG_STATE_IDLE_DETECT;
  4384. ret = ANEG_TIMER_ENAB;
  4385. break;
  4386. case ANEG_STATE_IDLE_DETECT:
  4387. if (ap->ability_match != 0 &&
  4388. ap->rxconfig == 0) {
  4389. ap->state = ANEG_STATE_AN_ENABLE;
  4390. break;
  4391. }
  4392. delta = ap->cur_time - ap->link_time;
  4393. if (delta > ANEG_STATE_SETTLE_TIME) {
  4394. /* XXX another gem from the Broadcom driver :( */
  4395. ap->state = ANEG_STATE_LINK_OK;
  4396. }
  4397. break;
  4398. case ANEG_STATE_LINK_OK:
  4399. ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
  4400. ret = ANEG_DONE;
  4401. break;
  4402. case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
  4403. /* ??? unimplemented */
  4404. break;
  4405. case ANEG_STATE_NEXT_PAGE_WAIT:
  4406. /* ??? unimplemented */
  4407. break;
  4408. default:
  4409. ret = ANEG_FAILED;
  4410. break;
  4411. }
  4412. return ret;
  4413. }
  4414. static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
  4415. {
  4416. int res = 0;
  4417. struct tg3_fiber_aneginfo aninfo;
  4418. int status = ANEG_FAILED;
  4419. unsigned int tick;
  4420. u32 tmp;
  4421. tw32_f(MAC_TX_AUTO_NEG, 0);
  4422. tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
  4423. tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
  4424. udelay(40);
  4425. tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
  4426. udelay(40);
  4427. memset(&aninfo, 0, sizeof(aninfo));
  4428. aninfo.flags |= MR_AN_ENABLE;
  4429. aninfo.state = ANEG_STATE_UNKNOWN;
  4430. aninfo.cur_time = 0;
  4431. tick = 0;
  4432. while (++tick < 195000) {
  4433. status = tg3_fiber_aneg_smachine(tp, &aninfo);
  4434. if (status == ANEG_DONE || status == ANEG_FAILED)
  4435. break;
  4436. udelay(1);
  4437. }
  4438. tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
  4439. tw32_f(MAC_MODE, tp->mac_mode);
  4440. udelay(40);
  4441. *txflags = aninfo.txconfig;
  4442. *rxflags = aninfo.flags;
  4443. if (status == ANEG_DONE &&
  4444. (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
  4445. MR_LP_ADV_FULL_DUPLEX)))
  4446. res = 1;
  4447. return res;
  4448. }
  4449. static void tg3_init_bcm8002(struct tg3 *tp)
  4450. {
  4451. u32 mac_status = tr32(MAC_STATUS);
  4452. int i;
  4453. /* Reset when initting first time or we have a link. */
  4454. if (tg3_flag(tp, INIT_COMPLETE) &&
  4455. !(mac_status & MAC_STATUS_PCS_SYNCED))
  4456. return;
  4457. /* Set PLL lock range. */
  4458. tg3_writephy(tp, 0x16, 0x8007);
  4459. /* SW reset */
  4460. tg3_writephy(tp, MII_BMCR, BMCR_RESET);
  4461. /* Wait for reset to complete. */
  4462. /* XXX schedule_timeout() ... */
  4463. for (i = 0; i < 500; i++)
  4464. udelay(10);
  4465. /* Config mode; select PMA/Ch 1 regs. */
  4466. tg3_writephy(tp, 0x10, 0x8411);
  4467. /* Enable auto-lock and comdet, select txclk for tx. */
  4468. tg3_writephy(tp, 0x11, 0x0a10);
  4469. tg3_writephy(tp, 0x18, 0x00a0);
  4470. tg3_writephy(tp, 0x16, 0x41ff);
  4471. /* Assert and deassert POR. */
  4472. tg3_writephy(tp, 0x13, 0x0400);
  4473. udelay(40);
  4474. tg3_writephy(tp, 0x13, 0x0000);
  4475. tg3_writephy(tp, 0x11, 0x0a50);
  4476. udelay(40);
  4477. tg3_writephy(tp, 0x11, 0x0a10);
  4478. /* Wait for signal to stabilize */
  4479. /* XXX schedule_timeout() ... */
  4480. for (i = 0; i < 15000; i++)
  4481. udelay(10);
  4482. /* Deselect the channel register so we can read the PHYID
  4483. * later.
  4484. */
  4485. tg3_writephy(tp, 0x10, 0x8011);
  4486. }
  4487. static bool tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
  4488. {
  4489. u16 flowctrl;
  4490. bool current_link_up;
  4491. u32 sg_dig_ctrl, sg_dig_status;
  4492. u32 serdes_cfg, expected_sg_dig_ctrl;
  4493. int workaround, port_a;
  4494. serdes_cfg = 0;
  4495. expected_sg_dig_ctrl = 0;
  4496. workaround = 0;
  4497. port_a = 1;
  4498. current_link_up = false;
  4499. if (tg3_chip_rev_id(tp) != CHIPREV_ID_5704_A0 &&
  4500. tg3_chip_rev_id(tp) != CHIPREV_ID_5704_A1) {
  4501. workaround = 1;
  4502. if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
  4503. port_a = 0;
  4504. /* preserve bits 0-11,13,14 for signal pre-emphasis */
  4505. /* preserve bits 20-23 for voltage regulator */
  4506. serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
  4507. }
  4508. sg_dig_ctrl = tr32(SG_DIG_CTRL);
  4509. if (tp->link_config.autoneg != AUTONEG_ENABLE) {
  4510. if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
  4511. if (workaround) {
  4512. u32 val = serdes_cfg;
  4513. if (port_a)
  4514. val |= 0xc010000;
  4515. else
  4516. val |= 0x4010000;
  4517. tw32_f(MAC_SERDES_CFG, val);
  4518. }
  4519. tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
  4520. }
  4521. if (mac_status & MAC_STATUS_PCS_SYNCED) {
  4522. tg3_setup_flow_control(tp, 0, 0);
  4523. current_link_up = true;
  4524. }
  4525. goto out;
  4526. }
  4527. /* Want auto-negotiation. */
  4528. expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
  4529. flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
  4530. if (flowctrl & ADVERTISE_1000XPAUSE)
  4531. expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
  4532. if (flowctrl & ADVERTISE_1000XPSE_ASYM)
  4533. expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
  4534. if (sg_dig_ctrl != expected_sg_dig_ctrl) {
  4535. if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
  4536. tp->serdes_counter &&
  4537. ((mac_status & (MAC_STATUS_PCS_SYNCED |
  4538. MAC_STATUS_RCVD_CFG)) ==
  4539. MAC_STATUS_PCS_SYNCED)) {
  4540. tp->serdes_counter--;
  4541. current_link_up = true;
  4542. goto out;
  4543. }
  4544. restart_autoneg:
  4545. if (workaround)
  4546. tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
  4547. tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
  4548. udelay(5);
  4549. tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
  4550. tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
  4551. tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
  4552. } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
  4553. MAC_STATUS_SIGNAL_DET)) {
  4554. sg_dig_status = tr32(SG_DIG_STATUS);
  4555. mac_status = tr32(MAC_STATUS);
  4556. if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
  4557. (mac_status & MAC_STATUS_PCS_SYNCED)) {
  4558. u32 local_adv = 0, remote_adv = 0;
  4559. if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
  4560. local_adv |= ADVERTISE_1000XPAUSE;
  4561. if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
  4562. local_adv |= ADVERTISE_1000XPSE_ASYM;
  4563. if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
  4564. remote_adv |= LPA_1000XPAUSE;
  4565. if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
  4566. remote_adv |= LPA_1000XPAUSE_ASYM;
  4567. tp->link_config.rmt_adv =
  4568. mii_adv_to_ethtool_adv_x(remote_adv);
  4569. tg3_setup_flow_control(tp, local_adv, remote_adv);
  4570. current_link_up = true;
  4571. tp->serdes_counter = 0;
  4572. tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
  4573. } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
  4574. if (tp->serdes_counter)
  4575. tp->serdes_counter--;
  4576. else {
  4577. if (workaround) {
  4578. u32 val = serdes_cfg;
  4579. if (port_a)
  4580. val |= 0xc010000;
  4581. else
  4582. val |= 0x4010000;
  4583. tw32_f(MAC_SERDES_CFG, val);
  4584. }
  4585. tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
  4586. udelay(40);
  4587. /* Link parallel detection - link is up */
  4588. /* only if we have PCS_SYNC and not */
  4589. /* receiving config code words */
  4590. mac_status = tr32(MAC_STATUS);
  4591. if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
  4592. !(mac_status & MAC_STATUS_RCVD_CFG)) {
  4593. tg3_setup_flow_control(tp, 0, 0);
  4594. current_link_up = true;
  4595. tp->phy_flags |=
  4596. TG3_PHYFLG_PARALLEL_DETECT;
  4597. tp->serdes_counter =
  4598. SERDES_PARALLEL_DET_TIMEOUT;
  4599. } else
  4600. goto restart_autoneg;
  4601. }
  4602. }
  4603. } else {
  4604. tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
  4605. tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
  4606. }
  4607. out:
  4608. return current_link_up;
  4609. }
  4610. static bool tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
  4611. {
  4612. bool current_link_up = false;
  4613. if (!(mac_status & MAC_STATUS_PCS_SYNCED))
  4614. goto out;
  4615. if (tp->link_config.autoneg == AUTONEG_ENABLE) {
  4616. u32 txflags, rxflags;
  4617. int i;
  4618. if (fiber_autoneg(tp, &txflags, &rxflags)) {
  4619. u32 local_adv = 0, remote_adv = 0;
  4620. if (txflags & ANEG_CFG_PS1)
  4621. local_adv |= ADVERTISE_1000XPAUSE;
  4622. if (txflags & ANEG_CFG_PS2)
  4623. local_adv |= ADVERTISE_1000XPSE_ASYM;
  4624. if (rxflags & MR_LP_ADV_SYM_PAUSE)
  4625. remote_adv |= LPA_1000XPAUSE;
  4626. if (rxflags & MR_LP_ADV_ASYM_PAUSE)
  4627. remote_adv |= LPA_1000XPAUSE_ASYM;
  4628. tp->link_config.rmt_adv =
  4629. mii_adv_to_ethtool_adv_x(remote_adv);
  4630. tg3_setup_flow_control(tp, local_adv, remote_adv);
  4631. current_link_up = true;
  4632. }
  4633. for (i = 0; i < 30; i++) {
  4634. udelay(20);
  4635. tw32_f(MAC_STATUS,
  4636. (MAC_STATUS_SYNC_CHANGED |
  4637. MAC_STATUS_CFG_CHANGED));
  4638. udelay(40);
  4639. if ((tr32(MAC_STATUS) &
  4640. (MAC_STATUS_SYNC_CHANGED |
  4641. MAC_STATUS_CFG_CHANGED)) == 0)
  4642. break;
  4643. }
  4644. mac_status = tr32(MAC_STATUS);
  4645. if (!current_link_up &&
  4646. (mac_status & MAC_STATUS_PCS_SYNCED) &&
  4647. !(mac_status & MAC_STATUS_RCVD_CFG))
  4648. current_link_up = true;
  4649. } else {
  4650. tg3_setup_flow_control(tp, 0, 0);
  4651. /* Forcing 1000FD link up. */
  4652. current_link_up = true;
  4653. tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
  4654. udelay(40);
  4655. tw32_f(MAC_MODE, tp->mac_mode);
  4656. udelay(40);
  4657. }
  4658. out:
  4659. return current_link_up;
  4660. }
  4661. static int tg3_setup_fiber_phy(struct tg3 *tp, bool force_reset)
  4662. {
  4663. u32 orig_pause_cfg;
  4664. u16 orig_active_speed;
  4665. u8 orig_active_duplex;
  4666. u32 mac_status;
  4667. bool current_link_up;
  4668. int i;
  4669. orig_pause_cfg = tp->link_config.active_flowctrl;
  4670. orig_active_speed = tp->link_config.active_speed;
  4671. orig_active_duplex = tp->link_config.active_duplex;
  4672. if (!tg3_flag(tp, HW_AUTONEG) &&
  4673. tp->link_up &&
  4674. tg3_flag(tp, INIT_COMPLETE)) {
  4675. mac_status = tr32(MAC_STATUS);
  4676. mac_status &= (MAC_STATUS_PCS_SYNCED |
  4677. MAC_STATUS_SIGNAL_DET |
  4678. MAC_STATUS_CFG_CHANGED |
  4679. MAC_STATUS_RCVD_CFG);
  4680. if (mac_status == (MAC_STATUS_PCS_SYNCED |
  4681. MAC_STATUS_SIGNAL_DET)) {
  4682. tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
  4683. MAC_STATUS_CFG_CHANGED));
  4684. return 0;
  4685. }
  4686. }
  4687. tw32_f(MAC_TX_AUTO_NEG, 0);
  4688. tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
  4689. tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
  4690. tw32_f(MAC_MODE, tp->mac_mode);
  4691. udelay(40);
  4692. if (tp->phy_id == TG3_PHY_ID_BCM8002)
  4693. tg3_init_bcm8002(tp);
  4694. /* Enable link change event even when serdes polling. */
  4695. tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
  4696. udelay(40);
  4697. current_link_up = false;
  4698. tp->link_config.rmt_adv = 0;
  4699. mac_status = tr32(MAC_STATUS);
  4700. if (tg3_flag(tp, HW_AUTONEG))
  4701. current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
  4702. else
  4703. current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
  4704. tp->napi[0].hw_status->status =
  4705. (SD_STATUS_UPDATED |
  4706. (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
  4707. for (i = 0; i < 100; i++) {
  4708. tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
  4709. MAC_STATUS_CFG_CHANGED));
  4710. udelay(5);
  4711. if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
  4712. MAC_STATUS_CFG_CHANGED |
  4713. MAC_STATUS_LNKSTATE_CHANGED)) == 0)
  4714. break;
  4715. }
  4716. mac_status = tr32(MAC_STATUS);
  4717. if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
  4718. current_link_up = false;
  4719. if (tp->link_config.autoneg == AUTONEG_ENABLE &&
  4720. tp->serdes_counter == 0) {
  4721. tw32_f(MAC_MODE, (tp->mac_mode |
  4722. MAC_MODE_SEND_CONFIGS));
  4723. udelay(1);
  4724. tw32_f(MAC_MODE, tp->mac_mode);
  4725. }
  4726. }
  4727. if (current_link_up) {
  4728. tp->link_config.active_speed = SPEED_1000;
  4729. tp->link_config.active_duplex = DUPLEX_FULL;
  4730. tw32(MAC_LED_CTRL, (tp->led_ctrl |
  4731. LED_CTRL_LNKLED_OVERRIDE |
  4732. LED_CTRL_1000MBPS_ON));
  4733. } else {
  4734. tp->link_config.active_speed = SPEED_UNKNOWN;
  4735. tp->link_config.active_duplex = DUPLEX_UNKNOWN;
  4736. tw32(MAC_LED_CTRL, (tp->led_ctrl |
  4737. LED_CTRL_LNKLED_OVERRIDE |
  4738. LED_CTRL_TRAFFIC_OVERRIDE));
  4739. }
  4740. if (!tg3_test_and_report_link_chg(tp, current_link_up)) {
  4741. u32 now_pause_cfg = tp->link_config.active_flowctrl;
  4742. if (orig_pause_cfg != now_pause_cfg ||
  4743. orig_active_speed != tp->link_config.active_speed ||
  4744. orig_active_duplex != tp->link_config.active_duplex)
  4745. tg3_link_report(tp);
  4746. }
  4747. return 0;
  4748. }
  4749. static int tg3_setup_fiber_mii_phy(struct tg3 *tp, bool force_reset)
  4750. {
  4751. int err = 0;
  4752. u32 bmsr, bmcr;
  4753. u16 current_speed = SPEED_UNKNOWN;
  4754. u8 current_duplex = DUPLEX_UNKNOWN;
  4755. bool current_link_up = false;
  4756. u32 local_adv, remote_adv, sgsr;
  4757. if ((tg3_asic_rev(tp) == ASIC_REV_5719 ||
  4758. tg3_asic_rev(tp) == ASIC_REV_5720) &&
  4759. !tg3_readphy(tp, SERDES_TG3_1000X_STATUS, &sgsr) &&
  4760. (sgsr & SERDES_TG3_SGMII_MODE)) {
  4761. if (force_reset)
  4762. tg3_phy_reset(tp);
  4763. tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
  4764. if (!(sgsr & SERDES_TG3_LINK_UP)) {
  4765. tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
  4766. } else {
  4767. current_link_up = true;
  4768. if (sgsr & SERDES_TG3_SPEED_1000) {
  4769. current_speed = SPEED_1000;
  4770. tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
  4771. } else if (sgsr & SERDES_TG3_SPEED_100) {
  4772. current_speed = SPEED_100;
  4773. tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
  4774. } else {
  4775. current_speed = SPEED_10;
  4776. tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
  4777. }
  4778. if (sgsr & SERDES_TG3_FULL_DUPLEX)
  4779. current_duplex = DUPLEX_FULL;
  4780. else
  4781. current_duplex = DUPLEX_HALF;
  4782. }
  4783. tw32_f(MAC_MODE, tp->mac_mode);
  4784. udelay(40);
  4785. tg3_clear_mac_status(tp);
  4786. goto fiber_setup_done;
  4787. }
  4788. tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
  4789. tw32_f(MAC_MODE, tp->mac_mode);
  4790. udelay(40);
  4791. tg3_clear_mac_status(tp);
  4792. if (force_reset)
  4793. tg3_phy_reset(tp);
  4794. tp->link_config.rmt_adv = 0;
  4795. err |= tg3_readphy(tp, MII_BMSR, &bmsr);
  4796. err |= tg3_readphy(tp, MII_BMSR, &bmsr);
  4797. if (tg3_asic_rev(tp) == ASIC_REV_5714) {
  4798. if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
  4799. bmsr |= BMSR_LSTATUS;
  4800. else
  4801. bmsr &= ~BMSR_LSTATUS;
  4802. }
  4803. err |= tg3_readphy(tp, MII_BMCR, &bmcr);
  4804. if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
  4805. (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
  4806. /* do nothing, just check for link up at the end */
  4807. } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
  4808. u32 adv, newadv;
  4809. err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
  4810. newadv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
  4811. ADVERTISE_1000XPAUSE |
  4812. ADVERTISE_1000XPSE_ASYM |
  4813. ADVERTISE_SLCT);
  4814. newadv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
  4815. newadv |= ethtool_adv_to_mii_adv_x(tp->link_config.advertising);
  4816. if ((newadv != adv) || !(bmcr & BMCR_ANENABLE)) {
  4817. tg3_writephy(tp, MII_ADVERTISE, newadv);
  4818. bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
  4819. tg3_writephy(tp, MII_BMCR, bmcr);
  4820. tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
  4821. tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
  4822. tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
  4823. return err;
  4824. }
  4825. } else {
  4826. u32 new_bmcr;
  4827. bmcr &= ~BMCR_SPEED1000;
  4828. new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
  4829. if (tp->link_config.duplex == DUPLEX_FULL)
  4830. new_bmcr |= BMCR_FULLDPLX;
  4831. if (new_bmcr != bmcr) {
  4832. /* BMCR_SPEED1000 is a reserved bit that needs
  4833. * to be set on write.
  4834. */
  4835. new_bmcr |= BMCR_SPEED1000;
  4836. /* Force a linkdown */
  4837. if (tp->link_up) {
  4838. u32 adv;
  4839. err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
  4840. adv &= ~(ADVERTISE_1000XFULL |
  4841. ADVERTISE_1000XHALF |
  4842. ADVERTISE_SLCT);
  4843. tg3_writephy(tp, MII_ADVERTISE, adv);
  4844. tg3_writephy(tp, MII_BMCR, bmcr |
  4845. BMCR_ANRESTART |
  4846. BMCR_ANENABLE);
  4847. udelay(10);
  4848. tg3_carrier_off(tp);
  4849. }
  4850. tg3_writephy(tp, MII_BMCR, new_bmcr);
  4851. bmcr = new_bmcr;
  4852. err |= tg3_readphy(tp, MII_BMSR, &bmsr);
  4853. err |= tg3_readphy(tp, MII_BMSR, &bmsr);
  4854. if (tg3_asic_rev(tp) == ASIC_REV_5714) {
  4855. if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
  4856. bmsr |= BMSR_LSTATUS;
  4857. else
  4858. bmsr &= ~BMSR_LSTATUS;
  4859. }
  4860. tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
  4861. }
  4862. }
  4863. if (bmsr & BMSR_LSTATUS) {
  4864. current_speed = SPEED_1000;
  4865. current_link_up = true;
  4866. if (bmcr & BMCR_FULLDPLX)
  4867. current_duplex = DUPLEX_FULL;
  4868. else
  4869. current_duplex = DUPLEX_HALF;
  4870. local_adv = 0;
  4871. remote_adv = 0;
  4872. if (bmcr & BMCR_ANENABLE) {
  4873. u32 common;
  4874. err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
  4875. err |= tg3_readphy(tp, MII_LPA, &remote_adv);
  4876. common = local_adv & remote_adv;
  4877. if (common & (ADVERTISE_1000XHALF |
  4878. ADVERTISE_1000XFULL)) {
  4879. if (common & ADVERTISE_1000XFULL)
  4880. current_duplex = DUPLEX_FULL;
  4881. else
  4882. current_duplex = DUPLEX_HALF;
  4883. tp->link_config.rmt_adv =
  4884. mii_adv_to_ethtool_adv_x(remote_adv);
  4885. } else if (!tg3_flag(tp, 5780_CLASS)) {
  4886. /* Link is up via parallel detect */
  4887. } else {
  4888. current_link_up = false;
  4889. }
  4890. }
  4891. }
  4892. fiber_setup_done:
  4893. if (current_link_up && current_duplex == DUPLEX_FULL)
  4894. tg3_setup_flow_control(tp, local_adv, remote_adv);
  4895. tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
  4896. if (tp->link_config.active_duplex == DUPLEX_HALF)
  4897. tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
  4898. tw32_f(MAC_MODE, tp->mac_mode);
  4899. udelay(40);
  4900. tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
  4901. tp->link_config.active_speed = current_speed;
  4902. tp->link_config.active_duplex = current_duplex;
  4903. tg3_test_and_report_link_chg(tp, current_link_up);
  4904. return err;
  4905. }
  4906. static void tg3_serdes_parallel_detect(struct tg3 *tp)
  4907. {
  4908. if (tp->serdes_counter) {
  4909. /* Give autoneg time to complete. */
  4910. tp->serdes_counter--;
  4911. return;
  4912. }
  4913. if (!tp->link_up &&
  4914. (tp->link_config.autoneg == AUTONEG_ENABLE)) {
  4915. u32 bmcr;
  4916. tg3_readphy(tp, MII_BMCR, &bmcr);
  4917. if (bmcr & BMCR_ANENABLE) {
  4918. u32 phy1, phy2;
  4919. /* Select shadow register 0x1f */
  4920. tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
  4921. tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
  4922. /* Select expansion interrupt status register */
  4923. tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
  4924. MII_TG3_DSP_EXP1_INT_STAT);
  4925. tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
  4926. tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
  4927. if ((phy1 & 0x10) && !(phy2 & 0x20)) {
  4928. /* We have signal detect and not receiving
  4929. * config code words, link is up by parallel
  4930. * detection.
  4931. */
  4932. bmcr &= ~BMCR_ANENABLE;
  4933. bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
  4934. tg3_writephy(tp, MII_BMCR, bmcr);
  4935. tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
  4936. }
  4937. }
  4938. } else if (tp->link_up &&
  4939. (tp->link_config.autoneg == AUTONEG_ENABLE) &&
  4940. (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
  4941. u32 phy2;
  4942. /* Select expansion interrupt status register */
  4943. tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
  4944. MII_TG3_DSP_EXP1_INT_STAT);
  4945. tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
  4946. if (phy2 & 0x20) {
  4947. u32 bmcr;
  4948. /* Config code words received, turn on autoneg. */
  4949. tg3_readphy(tp, MII_BMCR, &bmcr);
  4950. tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
  4951. tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
  4952. }
  4953. }
  4954. }
  4955. static int tg3_setup_phy(struct tg3 *tp, bool force_reset)
  4956. {
  4957. u32 val;
  4958. int err;
  4959. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
  4960. err = tg3_setup_fiber_phy(tp, force_reset);
  4961. else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
  4962. err = tg3_setup_fiber_mii_phy(tp, force_reset);
  4963. else
  4964. err = tg3_setup_copper_phy(tp, force_reset);
  4965. if (tg3_chip_rev(tp) == CHIPREV_5784_AX) {
  4966. u32 scale;
  4967. val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
  4968. if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
  4969. scale = 65;
  4970. else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
  4971. scale = 6;
  4972. else
  4973. scale = 12;
  4974. val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
  4975. val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
  4976. tw32(GRC_MISC_CFG, val);
  4977. }
  4978. val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
  4979. (6 << TX_LENGTHS_IPG_SHIFT);
  4980. if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
  4981. tg3_asic_rev(tp) == ASIC_REV_5762)
  4982. val |= tr32(MAC_TX_LENGTHS) &
  4983. (TX_LENGTHS_JMB_FRM_LEN_MSK |
  4984. TX_LENGTHS_CNT_DWN_VAL_MSK);
  4985. if (tp->link_config.active_speed == SPEED_1000 &&
  4986. tp->link_config.active_duplex == DUPLEX_HALF)
  4987. tw32(MAC_TX_LENGTHS, val |
  4988. (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
  4989. else
  4990. tw32(MAC_TX_LENGTHS, val |
  4991. (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
  4992. if (!tg3_flag(tp, 5705_PLUS)) {
  4993. if (tp->link_up) {
  4994. tw32(HOSTCC_STAT_COAL_TICKS,
  4995. tp->coal.stats_block_coalesce_usecs);
  4996. } else {
  4997. tw32(HOSTCC_STAT_COAL_TICKS, 0);
  4998. }
  4999. }
  5000. if (tg3_flag(tp, ASPM_WORKAROUND)) {
  5001. val = tr32(PCIE_PWR_MGMT_THRESH);
  5002. if (!tp->link_up)
  5003. val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
  5004. tp->pwrmgmt_thresh;
  5005. else
  5006. val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
  5007. tw32(PCIE_PWR_MGMT_THRESH, val);
  5008. }
  5009. return err;
  5010. }
  5011. /* tp->lock must be held */
  5012. static u64 tg3_refclk_read(struct tg3 *tp)
  5013. {
  5014. u64 stamp = tr32(TG3_EAV_REF_CLCK_LSB);
  5015. return stamp | (u64)tr32(TG3_EAV_REF_CLCK_MSB) << 32;
  5016. }
  5017. /* tp->lock must be held */
  5018. static void tg3_refclk_write(struct tg3 *tp, u64 newval)
  5019. {
  5020. u32 clock_ctl = tr32(TG3_EAV_REF_CLCK_CTL);
  5021. tw32(TG3_EAV_REF_CLCK_CTL, clock_ctl | TG3_EAV_REF_CLCK_CTL_STOP);
  5022. tw32(TG3_EAV_REF_CLCK_LSB, newval & 0xffffffff);
  5023. tw32(TG3_EAV_REF_CLCK_MSB, newval >> 32);
  5024. tw32_f(TG3_EAV_REF_CLCK_CTL, clock_ctl | TG3_EAV_REF_CLCK_CTL_RESUME);
  5025. }
  5026. static inline void tg3_full_lock(struct tg3 *tp, int irq_sync);
  5027. static inline void tg3_full_unlock(struct tg3 *tp);
  5028. static int tg3_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info)
  5029. {
  5030. struct tg3 *tp = netdev_priv(dev);
  5031. info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
  5032. SOF_TIMESTAMPING_RX_SOFTWARE |
  5033. SOF_TIMESTAMPING_SOFTWARE;
  5034. if (tg3_flag(tp, PTP_CAPABLE)) {
  5035. info->so_timestamping |= SOF_TIMESTAMPING_TX_HARDWARE |
  5036. SOF_TIMESTAMPING_RX_HARDWARE |
  5037. SOF_TIMESTAMPING_RAW_HARDWARE;
  5038. }
  5039. if (tp->ptp_clock)
  5040. info->phc_index = ptp_clock_index(tp->ptp_clock);
  5041. else
  5042. info->phc_index = -1;
  5043. info->tx_types = (1 << HWTSTAMP_TX_OFF) | (1 << HWTSTAMP_TX_ON);
  5044. info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
  5045. (1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT) |
  5046. (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
  5047. (1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT);
  5048. return 0;
  5049. }
  5050. static int tg3_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
  5051. {
  5052. struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
  5053. bool neg_adj = false;
  5054. u32 correction = 0;
  5055. if (ppb < 0) {
  5056. neg_adj = true;
  5057. ppb = -ppb;
  5058. }
  5059. /* Frequency adjustment is performed using hardware with a 24 bit
  5060. * accumulator and a programmable correction value. On each clk, the
  5061. * correction value gets added to the accumulator and when it
  5062. * overflows, the time counter is incremented/decremented.
  5063. *
  5064. * So conversion from ppb to correction value is
  5065. * ppb * (1 << 24) / 1000000000
  5066. */
  5067. correction = div_u64((u64)ppb * (1 << 24), 1000000000ULL) &
  5068. TG3_EAV_REF_CLK_CORRECT_MASK;
  5069. tg3_full_lock(tp, 0);
  5070. if (correction)
  5071. tw32(TG3_EAV_REF_CLK_CORRECT_CTL,
  5072. TG3_EAV_REF_CLK_CORRECT_EN |
  5073. (neg_adj ? TG3_EAV_REF_CLK_CORRECT_NEG : 0) | correction);
  5074. else
  5075. tw32(TG3_EAV_REF_CLK_CORRECT_CTL, 0);
  5076. tg3_full_unlock(tp);
  5077. return 0;
  5078. }
  5079. static int tg3_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
  5080. {
  5081. struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
  5082. tg3_full_lock(tp, 0);
  5083. tp->ptp_adjust += delta;
  5084. tg3_full_unlock(tp);
  5085. return 0;
  5086. }
  5087. static int tg3_ptp_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
  5088. {
  5089. u64 ns;
  5090. struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
  5091. tg3_full_lock(tp, 0);
  5092. ns = tg3_refclk_read(tp);
  5093. ns += tp->ptp_adjust;
  5094. tg3_full_unlock(tp);
  5095. *ts = ns_to_timespec64(ns);
  5096. return 0;
  5097. }
  5098. static int tg3_ptp_settime(struct ptp_clock_info *ptp,
  5099. const struct timespec64 *ts)
  5100. {
  5101. u64 ns;
  5102. struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
  5103. ns = timespec64_to_ns(ts);
  5104. tg3_full_lock(tp, 0);
  5105. tg3_refclk_write(tp, ns);
  5106. tp->ptp_adjust = 0;
  5107. tg3_full_unlock(tp);
  5108. return 0;
  5109. }
  5110. static int tg3_ptp_enable(struct ptp_clock_info *ptp,
  5111. struct ptp_clock_request *rq, int on)
  5112. {
  5113. struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
  5114. u32 clock_ctl;
  5115. int rval = 0;
  5116. switch (rq->type) {
  5117. case PTP_CLK_REQ_PEROUT:
  5118. if (rq->perout.index != 0)
  5119. return -EINVAL;
  5120. tg3_full_lock(tp, 0);
  5121. clock_ctl = tr32(TG3_EAV_REF_CLCK_CTL);
  5122. clock_ctl &= ~TG3_EAV_CTL_TSYNC_GPIO_MASK;
  5123. if (on) {
  5124. u64 nsec;
  5125. nsec = rq->perout.start.sec * 1000000000ULL +
  5126. rq->perout.start.nsec;
  5127. if (rq->perout.period.sec || rq->perout.period.nsec) {
  5128. netdev_warn(tp->dev,
  5129. "Device supports only a one-shot timesync output, period must be 0\n");
  5130. rval = -EINVAL;
  5131. goto err_out;
  5132. }
  5133. if (nsec & (1ULL << 63)) {
  5134. netdev_warn(tp->dev,
  5135. "Start value (nsec) is over limit. Maximum size of start is only 63 bits\n");
  5136. rval = -EINVAL;
  5137. goto err_out;
  5138. }
  5139. tw32(TG3_EAV_WATCHDOG0_LSB, (nsec & 0xffffffff));
  5140. tw32(TG3_EAV_WATCHDOG0_MSB,
  5141. TG3_EAV_WATCHDOG0_EN |
  5142. ((nsec >> 32) & TG3_EAV_WATCHDOG_MSB_MASK));
  5143. tw32(TG3_EAV_REF_CLCK_CTL,
  5144. clock_ctl | TG3_EAV_CTL_TSYNC_WDOG0);
  5145. } else {
  5146. tw32(TG3_EAV_WATCHDOG0_MSB, 0);
  5147. tw32(TG3_EAV_REF_CLCK_CTL, clock_ctl);
  5148. }
  5149. err_out:
  5150. tg3_full_unlock(tp);
  5151. return rval;
  5152. default:
  5153. break;
  5154. }
  5155. return -EOPNOTSUPP;
  5156. }
  5157. static const struct ptp_clock_info tg3_ptp_caps = {
  5158. .owner = THIS_MODULE,
  5159. .name = "tg3 clock",
  5160. .max_adj = 250000000,
  5161. .n_alarm = 0,
  5162. .n_ext_ts = 0,
  5163. .n_per_out = 1,
  5164. .n_pins = 0,
  5165. .pps = 0,
  5166. .adjfreq = tg3_ptp_adjfreq,
  5167. .adjtime = tg3_ptp_adjtime,
  5168. .gettime64 = tg3_ptp_gettime,
  5169. .settime64 = tg3_ptp_settime,
  5170. .enable = tg3_ptp_enable,
  5171. };
  5172. static void tg3_hwclock_to_timestamp(struct tg3 *tp, u64 hwclock,
  5173. struct skb_shared_hwtstamps *timestamp)
  5174. {
  5175. memset(timestamp, 0, sizeof(struct skb_shared_hwtstamps));
  5176. timestamp->hwtstamp = ns_to_ktime((hwclock & TG3_TSTAMP_MASK) +
  5177. tp->ptp_adjust);
  5178. }
  5179. /* tp->lock must be held */
  5180. static void tg3_ptp_init(struct tg3 *tp)
  5181. {
  5182. if (!tg3_flag(tp, PTP_CAPABLE))
  5183. return;
  5184. /* Initialize the hardware clock to the system time. */
  5185. tg3_refclk_write(tp, ktime_to_ns(ktime_get_real()));
  5186. tp->ptp_adjust = 0;
  5187. tp->ptp_info = tg3_ptp_caps;
  5188. }
  5189. /* tp->lock must be held */
  5190. static void tg3_ptp_resume(struct tg3 *tp)
  5191. {
  5192. if (!tg3_flag(tp, PTP_CAPABLE))
  5193. return;
  5194. tg3_refclk_write(tp, ktime_to_ns(ktime_get_real()) + tp->ptp_adjust);
  5195. tp->ptp_adjust = 0;
  5196. }
  5197. static void tg3_ptp_fini(struct tg3 *tp)
  5198. {
  5199. if (!tg3_flag(tp, PTP_CAPABLE) || !tp->ptp_clock)
  5200. return;
  5201. ptp_clock_unregister(tp->ptp_clock);
  5202. tp->ptp_clock = NULL;
  5203. tp->ptp_adjust = 0;
  5204. }
  5205. static inline int tg3_irq_sync(struct tg3 *tp)
  5206. {
  5207. return tp->irq_sync;
  5208. }
  5209. static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
  5210. {
  5211. int i;
  5212. dst = (u32 *)((u8 *)dst + off);
  5213. for (i = 0; i < len; i += sizeof(u32))
  5214. *dst++ = tr32(off + i);
  5215. }
  5216. static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
  5217. {
  5218. tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
  5219. tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
  5220. tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
  5221. tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
  5222. tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
  5223. tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
  5224. tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
  5225. tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
  5226. tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
  5227. tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
  5228. tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
  5229. tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
  5230. tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
  5231. tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
  5232. tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
  5233. tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
  5234. tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
  5235. tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
  5236. tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
  5237. if (tg3_flag(tp, SUPPORT_MSIX))
  5238. tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
  5239. tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
  5240. tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
  5241. tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
  5242. tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
  5243. tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
  5244. tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
  5245. tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
  5246. tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
  5247. if (!tg3_flag(tp, 5705_PLUS)) {
  5248. tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
  5249. tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
  5250. tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
  5251. }
  5252. tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
  5253. tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
  5254. tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
  5255. tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
  5256. tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
  5257. if (tg3_flag(tp, NVRAM))
  5258. tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
  5259. }
  5260. static void tg3_dump_state(struct tg3 *tp)
  5261. {
  5262. int i;
  5263. u32 *regs;
  5264. regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
  5265. if (!regs)
  5266. return;
  5267. if (tg3_flag(tp, PCI_EXPRESS)) {
  5268. /* Read up to but not including private PCI registers */
  5269. for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
  5270. regs[i / sizeof(u32)] = tr32(i);
  5271. } else
  5272. tg3_dump_legacy_regs(tp, regs);
  5273. for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
  5274. if (!regs[i + 0] && !regs[i + 1] &&
  5275. !regs[i + 2] && !regs[i + 3])
  5276. continue;
  5277. netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
  5278. i * 4,
  5279. regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
  5280. }
  5281. kfree(regs);
  5282. for (i = 0; i < tp->irq_cnt; i++) {
  5283. struct tg3_napi *tnapi = &tp->napi[i];
  5284. /* SW status block */
  5285. netdev_err(tp->dev,
  5286. "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
  5287. i,
  5288. tnapi->hw_status->status,
  5289. tnapi->hw_status->status_tag,
  5290. tnapi->hw_status->rx_jumbo_consumer,
  5291. tnapi->hw_status->rx_consumer,
  5292. tnapi->hw_status->rx_mini_consumer,
  5293. tnapi->hw_status->idx[0].rx_producer,
  5294. tnapi->hw_status->idx[0].tx_consumer);
  5295. netdev_err(tp->dev,
  5296. "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
  5297. i,
  5298. tnapi->last_tag, tnapi->last_irq_tag,
  5299. tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
  5300. tnapi->rx_rcb_ptr,
  5301. tnapi->prodring.rx_std_prod_idx,
  5302. tnapi->prodring.rx_std_cons_idx,
  5303. tnapi->prodring.rx_jmb_prod_idx,
  5304. tnapi->prodring.rx_jmb_cons_idx);
  5305. }
  5306. }
  5307. /* This is called whenever we suspect that the system chipset is re-
  5308. * ordering the sequence of MMIO to the tx send mailbox. The symptom
  5309. * is bogus tx completions. We try to recover by setting the
  5310. * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
  5311. * in the workqueue.
  5312. */
  5313. static void tg3_tx_recover(struct tg3 *tp)
  5314. {
  5315. BUG_ON(tg3_flag(tp, MBOX_WRITE_REORDER) ||
  5316. tp->write32_tx_mbox == tg3_write_indirect_mbox);
  5317. netdev_warn(tp->dev,
  5318. "The system may be re-ordering memory-mapped I/O "
  5319. "cycles to the network device, attempting to recover. "
  5320. "Please report the problem to the driver maintainer "
  5321. "and include system chipset information.\n");
  5322. tg3_flag_set(tp, TX_RECOVERY_PENDING);
  5323. }
  5324. static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
  5325. {
  5326. /* Tell compiler to fetch tx indices from memory. */
  5327. barrier();
  5328. return tnapi->tx_pending -
  5329. ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
  5330. }
  5331. /* Tigon3 never reports partial packet sends. So we do not
  5332. * need special logic to handle SKBs that have not had all
  5333. * of their frags sent yet, like SunGEM does.
  5334. */
  5335. static void tg3_tx(struct tg3_napi *tnapi)
  5336. {
  5337. struct tg3 *tp = tnapi->tp;
  5338. u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
  5339. u32 sw_idx = tnapi->tx_cons;
  5340. struct netdev_queue *txq;
  5341. int index = tnapi - tp->napi;
  5342. unsigned int pkts_compl = 0, bytes_compl = 0;
  5343. if (tg3_flag(tp, ENABLE_TSS))
  5344. index--;
  5345. txq = netdev_get_tx_queue(tp->dev, index);
  5346. while (sw_idx != hw_idx) {
  5347. struct tg3_tx_ring_info *ri = &tnapi->tx_buffers[sw_idx];
  5348. struct sk_buff *skb = ri->skb;
  5349. int i, tx_bug = 0;
  5350. if (unlikely(skb == NULL)) {
  5351. tg3_tx_recover(tp);
  5352. return;
  5353. }
  5354. if (tnapi->tx_ring[sw_idx].len_flags & TXD_FLAG_HWTSTAMP) {
  5355. struct skb_shared_hwtstamps timestamp;
  5356. u64 hwclock = tr32(TG3_TX_TSTAMP_LSB);
  5357. hwclock |= (u64)tr32(TG3_TX_TSTAMP_MSB) << 32;
  5358. tg3_hwclock_to_timestamp(tp, hwclock, &timestamp);
  5359. skb_tstamp_tx(skb, &timestamp);
  5360. }
  5361. pci_unmap_single(tp->pdev,
  5362. dma_unmap_addr(ri, mapping),
  5363. skb_headlen(skb),
  5364. PCI_DMA_TODEVICE);
  5365. ri->skb = NULL;
  5366. while (ri->fragmented) {
  5367. ri->fragmented = false;
  5368. sw_idx = NEXT_TX(sw_idx);
  5369. ri = &tnapi->tx_buffers[sw_idx];
  5370. }
  5371. sw_idx = NEXT_TX(sw_idx);
  5372. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  5373. ri = &tnapi->tx_buffers[sw_idx];
  5374. if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
  5375. tx_bug = 1;
  5376. pci_unmap_page(tp->pdev,
  5377. dma_unmap_addr(ri, mapping),
  5378. skb_frag_size(&skb_shinfo(skb)->frags[i]),
  5379. PCI_DMA_TODEVICE);
  5380. while (ri->fragmented) {
  5381. ri->fragmented = false;
  5382. sw_idx = NEXT_TX(sw_idx);
  5383. ri = &tnapi->tx_buffers[sw_idx];
  5384. }
  5385. sw_idx = NEXT_TX(sw_idx);
  5386. }
  5387. pkts_compl++;
  5388. bytes_compl += skb->len;
  5389. dev_kfree_skb_any(skb);
  5390. if (unlikely(tx_bug)) {
  5391. tg3_tx_recover(tp);
  5392. return;
  5393. }
  5394. }
  5395. netdev_tx_completed_queue(txq, pkts_compl, bytes_compl);
  5396. tnapi->tx_cons = sw_idx;
  5397. /* Need to make the tx_cons update visible to tg3_start_xmit()
  5398. * before checking for netif_queue_stopped(). Without the
  5399. * memory barrier, there is a small possibility that tg3_start_xmit()
  5400. * will miss it and cause the queue to be stopped forever.
  5401. */
  5402. smp_mb();
  5403. if (unlikely(netif_tx_queue_stopped(txq) &&
  5404. (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
  5405. __netif_tx_lock(txq, smp_processor_id());
  5406. if (netif_tx_queue_stopped(txq) &&
  5407. (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
  5408. netif_tx_wake_queue(txq);
  5409. __netif_tx_unlock(txq);
  5410. }
  5411. }
  5412. static void tg3_frag_free(bool is_frag, void *data)
  5413. {
  5414. if (is_frag)
  5415. skb_free_frag(data);
  5416. else
  5417. kfree(data);
  5418. }
  5419. static void tg3_rx_data_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
  5420. {
  5421. unsigned int skb_size = SKB_DATA_ALIGN(map_sz + TG3_RX_OFFSET(tp)) +
  5422. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  5423. if (!ri->data)
  5424. return;
  5425. pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
  5426. map_sz, PCI_DMA_FROMDEVICE);
  5427. tg3_frag_free(skb_size <= PAGE_SIZE, ri->data);
  5428. ri->data = NULL;
  5429. }
  5430. /* Returns size of skb allocated or < 0 on error.
  5431. *
  5432. * We only need to fill in the address because the other members
  5433. * of the RX descriptor are invariant, see tg3_init_rings.
  5434. *
  5435. * Note the purposeful assymetry of cpu vs. chip accesses. For
  5436. * posting buffers we only dirty the first cache line of the RX
  5437. * descriptor (containing the address). Whereas for the RX status
  5438. * buffers the cpu only reads the last cacheline of the RX descriptor
  5439. * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
  5440. */
  5441. static int tg3_alloc_rx_data(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
  5442. u32 opaque_key, u32 dest_idx_unmasked,
  5443. unsigned int *frag_size)
  5444. {
  5445. struct tg3_rx_buffer_desc *desc;
  5446. struct ring_info *map;
  5447. u8 *data;
  5448. dma_addr_t mapping;
  5449. int skb_size, data_size, dest_idx;
  5450. switch (opaque_key) {
  5451. case RXD_OPAQUE_RING_STD:
  5452. dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
  5453. desc = &tpr->rx_std[dest_idx];
  5454. map = &tpr->rx_std_buffers[dest_idx];
  5455. data_size = tp->rx_pkt_map_sz;
  5456. break;
  5457. case RXD_OPAQUE_RING_JUMBO:
  5458. dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
  5459. desc = &tpr->rx_jmb[dest_idx].std;
  5460. map = &tpr->rx_jmb_buffers[dest_idx];
  5461. data_size = TG3_RX_JMB_MAP_SZ;
  5462. break;
  5463. default:
  5464. return -EINVAL;
  5465. }
  5466. /* Do not overwrite any of the map or rp information
  5467. * until we are sure we can commit to a new buffer.
  5468. *
  5469. * Callers depend upon this behavior and assume that
  5470. * we leave everything unchanged if we fail.
  5471. */
  5472. skb_size = SKB_DATA_ALIGN(data_size + TG3_RX_OFFSET(tp)) +
  5473. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  5474. if (skb_size <= PAGE_SIZE) {
  5475. data = netdev_alloc_frag(skb_size);
  5476. *frag_size = skb_size;
  5477. } else {
  5478. data = kmalloc(skb_size, GFP_ATOMIC);
  5479. *frag_size = 0;
  5480. }
  5481. if (!data)
  5482. return -ENOMEM;
  5483. mapping = pci_map_single(tp->pdev,
  5484. data + TG3_RX_OFFSET(tp),
  5485. data_size,
  5486. PCI_DMA_FROMDEVICE);
  5487. if (unlikely(pci_dma_mapping_error(tp->pdev, mapping))) {
  5488. tg3_frag_free(skb_size <= PAGE_SIZE, data);
  5489. return -EIO;
  5490. }
  5491. map->data = data;
  5492. dma_unmap_addr_set(map, mapping, mapping);
  5493. desc->addr_hi = ((u64)mapping >> 32);
  5494. desc->addr_lo = ((u64)mapping & 0xffffffff);
  5495. return data_size;
  5496. }
  5497. /* We only need to move over in the address because the other
  5498. * members of the RX descriptor are invariant. See notes above
  5499. * tg3_alloc_rx_data for full details.
  5500. */
  5501. static void tg3_recycle_rx(struct tg3_napi *tnapi,
  5502. struct tg3_rx_prodring_set *dpr,
  5503. u32 opaque_key, int src_idx,
  5504. u32 dest_idx_unmasked)
  5505. {
  5506. struct tg3 *tp = tnapi->tp;
  5507. struct tg3_rx_buffer_desc *src_desc, *dest_desc;
  5508. struct ring_info *src_map, *dest_map;
  5509. struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
  5510. int dest_idx;
  5511. switch (opaque_key) {
  5512. case RXD_OPAQUE_RING_STD:
  5513. dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
  5514. dest_desc = &dpr->rx_std[dest_idx];
  5515. dest_map = &dpr->rx_std_buffers[dest_idx];
  5516. src_desc = &spr->rx_std[src_idx];
  5517. src_map = &spr->rx_std_buffers[src_idx];
  5518. break;
  5519. case RXD_OPAQUE_RING_JUMBO:
  5520. dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
  5521. dest_desc = &dpr->rx_jmb[dest_idx].std;
  5522. dest_map = &dpr->rx_jmb_buffers[dest_idx];
  5523. src_desc = &spr->rx_jmb[src_idx].std;
  5524. src_map = &spr->rx_jmb_buffers[src_idx];
  5525. break;
  5526. default:
  5527. return;
  5528. }
  5529. dest_map->data = src_map->data;
  5530. dma_unmap_addr_set(dest_map, mapping,
  5531. dma_unmap_addr(src_map, mapping));
  5532. dest_desc->addr_hi = src_desc->addr_hi;
  5533. dest_desc->addr_lo = src_desc->addr_lo;
  5534. /* Ensure that the update to the skb happens after the physical
  5535. * addresses have been transferred to the new BD location.
  5536. */
  5537. smp_wmb();
  5538. src_map->data = NULL;
  5539. }
  5540. /* The RX ring scheme is composed of multiple rings which post fresh
  5541. * buffers to the chip, and one special ring the chip uses to report
  5542. * status back to the host.
  5543. *
  5544. * The special ring reports the status of received packets to the
  5545. * host. The chip does not write into the original descriptor the
  5546. * RX buffer was obtained from. The chip simply takes the original
  5547. * descriptor as provided by the host, updates the status and length
  5548. * field, then writes this into the next status ring entry.
  5549. *
  5550. * Each ring the host uses to post buffers to the chip is described
  5551. * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
  5552. * it is first placed into the on-chip ram. When the packet's length
  5553. * is known, it walks down the TG3_BDINFO entries to select the ring.
  5554. * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
  5555. * which is within the range of the new packet's length is chosen.
  5556. *
  5557. * The "separate ring for rx status" scheme may sound queer, but it makes
  5558. * sense from a cache coherency perspective. If only the host writes
  5559. * to the buffer post rings, and only the chip writes to the rx status
  5560. * rings, then cache lines never move beyond shared-modified state.
  5561. * If both the host and chip were to write into the same ring, cache line
  5562. * eviction could occur since both entities want it in an exclusive state.
  5563. */
  5564. static int tg3_rx(struct tg3_napi *tnapi, int budget)
  5565. {
  5566. struct tg3 *tp = tnapi->tp;
  5567. u32 work_mask, rx_std_posted = 0;
  5568. u32 std_prod_idx, jmb_prod_idx;
  5569. u32 sw_idx = tnapi->rx_rcb_ptr;
  5570. u16 hw_idx;
  5571. int received;
  5572. struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
  5573. hw_idx = *(tnapi->rx_rcb_prod_idx);
  5574. /*
  5575. * We need to order the read of hw_idx and the read of
  5576. * the opaque cookie.
  5577. */
  5578. rmb();
  5579. work_mask = 0;
  5580. received = 0;
  5581. std_prod_idx = tpr->rx_std_prod_idx;
  5582. jmb_prod_idx = tpr->rx_jmb_prod_idx;
  5583. while (sw_idx != hw_idx && budget > 0) {
  5584. struct ring_info *ri;
  5585. struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
  5586. unsigned int len;
  5587. struct sk_buff *skb;
  5588. dma_addr_t dma_addr;
  5589. u32 opaque_key, desc_idx, *post_ptr;
  5590. u8 *data;
  5591. u64 tstamp = 0;
  5592. desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
  5593. opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
  5594. if (opaque_key == RXD_OPAQUE_RING_STD) {
  5595. ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
  5596. dma_addr = dma_unmap_addr(ri, mapping);
  5597. data = ri->data;
  5598. post_ptr = &std_prod_idx;
  5599. rx_std_posted++;
  5600. } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
  5601. ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
  5602. dma_addr = dma_unmap_addr(ri, mapping);
  5603. data = ri->data;
  5604. post_ptr = &jmb_prod_idx;
  5605. } else
  5606. goto next_pkt_nopost;
  5607. work_mask |= opaque_key;
  5608. if (desc->err_vlan & RXD_ERR_MASK) {
  5609. drop_it:
  5610. tg3_recycle_rx(tnapi, tpr, opaque_key,
  5611. desc_idx, *post_ptr);
  5612. drop_it_no_recycle:
  5613. /* Other statistics kept track of by card. */
  5614. tp->rx_dropped++;
  5615. goto next_pkt;
  5616. }
  5617. prefetch(data + TG3_RX_OFFSET(tp));
  5618. len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
  5619. ETH_FCS_LEN;
  5620. if ((desc->type_flags & RXD_FLAG_PTPSTAT_MASK) ==
  5621. RXD_FLAG_PTPSTAT_PTPV1 ||
  5622. (desc->type_flags & RXD_FLAG_PTPSTAT_MASK) ==
  5623. RXD_FLAG_PTPSTAT_PTPV2) {
  5624. tstamp = tr32(TG3_RX_TSTAMP_LSB);
  5625. tstamp |= (u64)tr32(TG3_RX_TSTAMP_MSB) << 32;
  5626. }
  5627. if (len > TG3_RX_COPY_THRESH(tp)) {
  5628. int skb_size;
  5629. unsigned int frag_size;
  5630. skb_size = tg3_alloc_rx_data(tp, tpr, opaque_key,
  5631. *post_ptr, &frag_size);
  5632. if (skb_size < 0)
  5633. goto drop_it;
  5634. pci_unmap_single(tp->pdev, dma_addr, skb_size,
  5635. PCI_DMA_FROMDEVICE);
  5636. /* Ensure that the update to the data happens
  5637. * after the usage of the old DMA mapping.
  5638. */
  5639. smp_wmb();
  5640. ri->data = NULL;
  5641. skb = build_skb(data, frag_size);
  5642. if (!skb) {
  5643. tg3_frag_free(frag_size != 0, data);
  5644. goto drop_it_no_recycle;
  5645. }
  5646. skb_reserve(skb, TG3_RX_OFFSET(tp));
  5647. } else {
  5648. tg3_recycle_rx(tnapi, tpr, opaque_key,
  5649. desc_idx, *post_ptr);
  5650. skb = netdev_alloc_skb(tp->dev,
  5651. len + TG3_RAW_IP_ALIGN);
  5652. if (skb == NULL)
  5653. goto drop_it_no_recycle;
  5654. skb_reserve(skb, TG3_RAW_IP_ALIGN);
  5655. pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
  5656. memcpy(skb->data,
  5657. data + TG3_RX_OFFSET(tp),
  5658. len);
  5659. pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
  5660. }
  5661. skb_put(skb, len);
  5662. if (tstamp)
  5663. tg3_hwclock_to_timestamp(tp, tstamp,
  5664. skb_hwtstamps(skb));
  5665. if ((tp->dev->features & NETIF_F_RXCSUM) &&
  5666. (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
  5667. (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
  5668. >> RXD_TCPCSUM_SHIFT) == 0xffff))
  5669. skb->ip_summed = CHECKSUM_UNNECESSARY;
  5670. else
  5671. skb_checksum_none_assert(skb);
  5672. skb->protocol = eth_type_trans(skb, tp->dev);
  5673. if (len > (tp->dev->mtu + ETH_HLEN) &&
  5674. skb->protocol != htons(ETH_P_8021Q) &&
  5675. skb->protocol != htons(ETH_P_8021AD)) {
  5676. dev_kfree_skb_any(skb);
  5677. goto drop_it_no_recycle;
  5678. }
  5679. if (desc->type_flags & RXD_FLAG_VLAN &&
  5680. !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
  5681. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
  5682. desc->err_vlan & RXD_VLAN_MASK);
  5683. napi_gro_receive(&tnapi->napi, skb);
  5684. received++;
  5685. budget--;
  5686. next_pkt:
  5687. (*post_ptr)++;
  5688. if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
  5689. tpr->rx_std_prod_idx = std_prod_idx &
  5690. tp->rx_std_ring_mask;
  5691. tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
  5692. tpr->rx_std_prod_idx);
  5693. work_mask &= ~RXD_OPAQUE_RING_STD;
  5694. rx_std_posted = 0;
  5695. }
  5696. next_pkt_nopost:
  5697. sw_idx++;
  5698. sw_idx &= tp->rx_ret_ring_mask;
  5699. /* Refresh hw_idx to see if there is new work */
  5700. if (sw_idx == hw_idx) {
  5701. hw_idx = *(tnapi->rx_rcb_prod_idx);
  5702. rmb();
  5703. }
  5704. }
  5705. /* ACK the status ring. */
  5706. tnapi->rx_rcb_ptr = sw_idx;
  5707. tw32_rx_mbox(tnapi->consmbox, sw_idx);
  5708. /* Refill RX ring(s). */
  5709. if (!tg3_flag(tp, ENABLE_RSS)) {
  5710. /* Sync BD data before updating mailbox */
  5711. wmb();
  5712. if (work_mask & RXD_OPAQUE_RING_STD) {
  5713. tpr->rx_std_prod_idx = std_prod_idx &
  5714. tp->rx_std_ring_mask;
  5715. tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
  5716. tpr->rx_std_prod_idx);
  5717. }
  5718. if (work_mask & RXD_OPAQUE_RING_JUMBO) {
  5719. tpr->rx_jmb_prod_idx = jmb_prod_idx &
  5720. tp->rx_jmb_ring_mask;
  5721. tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
  5722. tpr->rx_jmb_prod_idx);
  5723. }
  5724. mmiowb();
  5725. } else if (work_mask) {
  5726. /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
  5727. * updated before the producer indices can be updated.
  5728. */
  5729. smp_wmb();
  5730. tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
  5731. tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
  5732. if (tnapi != &tp->napi[1]) {
  5733. tp->rx_refill = true;
  5734. napi_schedule(&tp->napi[1].napi);
  5735. }
  5736. }
  5737. return received;
  5738. }
  5739. static void tg3_poll_link(struct tg3 *tp)
  5740. {
  5741. /* handle link change and other phy events */
  5742. if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
  5743. struct tg3_hw_status *sblk = tp->napi[0].hw_status;
  5744. if (sblk->status & SD_STATUS_LINK_CHG) {
  5745. sblk->status = SD_STATUS_UPDATED |
  5746. (sblk->status & ~SD_STATUS_LINK_CHG);
  5747. spin_lock(&tp->lock);
  5748. if (tg3_flag(tp, USE_PHYLIB)) {
  5749. tw32_f(MAC_STATUS,
  5750. (MAC_STATUS_SYNC_CHANGED |
  5751. MAC_STATUS_CFG_CHANGED |
  5752. MAC_STATUS_MI_COMPLETION |
  5753. MAC_STATUS_LNKSTATE_CHANGED));
  5754. udelay(40);
  5755. } else
  5756. tg3_setup_phy(tp, false);
  5757. spin_unlock(&tp->lock);
  5758. }
  5759. }
  5760. }
  5761. static int tg3_rx_prodring_xfer(struct tg3 *tp,
  5762. struct tg3_rx_prodring_set *dpr,
  5763. struct tg3_rx_prodring_set *spr)
  5764. {
  5765. u32 si, di, cpycnt, src_prod_idx;
  5766. int i, err = 0;
  5767. while (1) {
  5768. src_prod_idx = spr->rx_std_prod_idx;
  5769. /* Make sure updates to the rx_std_buffers[] entries and the
  5770. * standard producer index are seen in the correct order.
  5771. */
  5772. smp_rmb();
  5773. if (spr->rx_std_cons_idx == src_prod_idx)
  5774. break;
  5775. if (spr->rx_std_cons_idx < src_prod_idx)
  5776. cpycnt = src_prod_idx - spr->rx_std_cons_idx;
  5777. else
  5778. cpycnt = tp->rx_std_ring_mask + 1 -
  5779. spr->rx_std_cons_idx;
  5780. cpycnt = min(cpycnt,
  5781. tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
  5782. si = spr->rx_std_cons_idx;
  5783. di = dpr->rx_std_prod_idx;
  5784. for (i = di; i < di + cpycnt; i++) {
  5785. if (dpr->rx_std_buffers[i].data) {
  5786. cpycnt = i - di;
  5787. err = -ENOSPC;
  5788. break;
  5789. }
  5790. }
  5791. if (!cpycnt)
  5792. break;
  5793. /* Ensure that updates to the rx_std_buffers ring and the
  5794. * shadowed hardware producer ring from tg3_recycle_skb() are
  5795. * ordered correctly WRT the skb check above.
  5796. */
  5797. smp_rmb();
  5798. memcpy(&dpr->rx_std_buffers[di],
  5799. &spr->rx_std_buffers[si],
  5800. cpycnt * sizeof(struct ring_info));
  5801. for (i = 0; i < cpycnt; i++, di++, si++) {
  5802. struct tg3_rx_buffer_desc *sbd, *dbd;
  5803. sbd = &spr->rx_std[si];
  5804. dbd = &dpr->rx_std[di];
  5805. dbd->addr_hi = sbd->addr_hi;
  5806. dbd->addr_lo = sbd->addr_lo;
  5807. }
  5808. spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
  5809. tp->rx_std_ring_mask;
  5810. dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
  5811. tp->rx_std_ring_mask;
  5812. }
  5813. while (1) {
  5814. src_prod_idx = spr->rx_jmb_prod_idx;
  5815. /* Make sure updates to the rx_jmb_buffers[] entries and
  5816. * the jumbo producer index are seen in the correct order.
  5817. */
  5818. smp_rmb();
  5819. if (spr->rx_jmb_cons_idx == src_prod_idx)
  5820. break;
  5821. if (spr->rx_jmb_cons_idx < src_prod_idx)
  5822. cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
  5823. else
  5824. cpycnt = tp->rx_jmb_ring_mask + 1 -
  5825. spr->rx_jmb_cons_idx;
  5826. cpycnt = min(cpycnt,
  5827. tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
  5828. si = spr->rx_jmb_cons_idx;
  5829. di = dpr->rx_jmb_prod_idx;
  5830. for (i = di; i < di + cpycnt; i++) {
  5831. if (dpr->rx_jmb_buffers[i].data) {
  5832. cpycnt = i - di;
  5833. err = -ENOSPC;
  5834. break;
  5835. }
  5836. }
  5837. if (!cpycnt)
  5838. break;
  5839. /* Ensure that updates to the rx_jmb_buffers ring and the
  5840. * shadowed hardware producer ring from tg3_recycle_skb() are
  5841. * ordered correctly WRT the skb check above.
  5842. */
  5843. smp_rmb();
  5844. memcpy(&dpr->rx_jmb_buffers[di],
  5845. &spr->rx_jmb_buffers[si],
  5846. cpycnt * sizeof(struct ring_info));
  5847. for (i = 0; i < cpycnt; i++, di++, si++) {
  5848. struct tg3_rx_buffer_desc *sbd, *dbd;
  5849. sbd = &spr->rx_jmb[si].std;
  5850. dbd = &dpr->rx_jmb[di].std;
  5851. dbd->addr_hi = sbd->addr_hi;
  5852. dbd->addr_lo = sbd->addr_lo;
  5853. }
  5854. spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
  5855. tp->rx_jmb_ring_mask;
  5856. dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
  5857. tp->rx_jmb_ring_mask;
  5858. }
  5859. return err;
  5860. }
  5861. static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
  5862. {
  5863. struct tg3 *tp = tnapi->tp;
  5864. /* run TX completion thread */
  5865. if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
  5866. tg3_tx(tnapi);
  5867. if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
  5868. return work_done;
  5869. }
  5870. if (!tnapi->rx_rcb_prod_idx)
  5871. return work_done;
  5872. /* run RX thread, within the bounds set by NAPI.
  5873. * All RX "locking" is done by ensuring outside
  5874. * code synchronizes with tg3->napi.poll()
  5875. */
  5876. if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
  5877. work_done += tg3_rx(tnapi, budget - work_done);
  5878. if (tg3_flag(tp, ENABLE_RSS) && tnapi == &tp->napi[1]) {
  5879. struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
  5880. int i, err = 0;
  5881. u32 std_prod_idx = dpr->rx_std_prod_idx;
  5882. u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
  5883. tp->rx_refill = false;
  5884. for (i = 1; i <= tp->rxq_cnt; i++)
  5885. err |= tg3_rx_prodring_xfer(tp, dpr,
  5886. &tp->napi[i].prodring);
  5887. wmb();
  5888. if (std_prod_idx != dpr->rx_std_prod_idx)
  5889. tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
  5890. dpr->rx_std_prod_idx);
  5891. if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
  5892. tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
  5893. dpr->rx_jmb_prod_idx);
  5894. mmiowb();
  5895. if (err)
  5896. tw32_f(HOSTCC_MODE, tp->coal_now);
  5897. }
  5898. return work_done;
  5899. }
  5900. static inline void tg3_reset_task_schedule(struct tg3 *tp)
  5901. {
  5902. if (!test_and_set_bit(TG3_FLAG_RESET_TASK_PENDING, tp->tg3_flags))
  5903. schedule_work(&tp->reset_task);
  5904. }
  5905. static inline void tg3_reset_task_cancel(struct tg3 *tp)
  5906. {
  5907. cancel_work_sync(&tp->reset_task);
  5908. tg3_flag_clear(tp, RESET_TASK_PENDING);
  5909. tg3_flag_clear(tp, TX_RECOVERY_PENDING);
  5910. }
  5911. static int tg3_poll_msix(struct napi_struct *napi, int budget)
  5912. {
  5913. struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
  5914. struct tg3 *tp = tnapi->tp;
  5915. int work_done = 0;
  5916. struct tg3_hw_status *sblk = tnapi->hw_status;
  5917. while (1) {
  5918. work_done = tg3_poll_work(tnapi, work_done, budget);
  5919. if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
  5920. goto tx_recovery;
  5921. if (unlikely(work_done >= budget))
  5922. break;
  5923. /* tp->last_tag is used in tg3_int_reenable() below
  5924. * to tell the hw how much work has been processed,
  5925. * so we must read it before checking for more work.
  5926. */
  5927. tnapi->last_tag = sblk->status_tag;
  5928. tnapi->last_irq_tag = tnapi->last_tag;
  5929. rmb();
  5930. /* check for RX/TX work to do */
  5931. if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
  5932. *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
  5933. /* This test here is not race free, but will reduce
  5934. * the number of interrupts by looping again.
  5935. */
  5936. if (tnapi == &tp->napi[1] && tp->rx_refill)
  5937. continue;
  5938. napi_complete_done(napi, work_done);
  5939. /* Reenable interrupts. */
  5940. tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
  5941. /* This test here is synchronized by napi_schedule()
  5942. * and napi_complete() to close the race condition.
  5943. */
  5944. if (unlikely(tnapi == &tp->napi[1] && tp->rx_refill)) {
  5945. tw32(HOSTCC_MODE, tp->coalesce_mode |
  5946. HOSTCC_MODE_ENABLE |
  5947. tnapi->coal_now);
  5948. }
  5949. mmiowb();
  5950. break;
  5951. }
  5952. }
  5953. return work_done;
  5954. tx_recovery:
  5955. /* work_done is guaranteed to be less than budget. */
  5956. napi_complete(napi);
  5957. tg3_reset_task_schedule(tp);
  5958. return work_done;
  5959. }
  5960. static void tg3_process_error(struct tg3 *tp)
  5961. {
  5962. u32 val;
  5963. bool real_error = false;
  5964. if (tg3_flag(tp, ERROR_PROCESSED))
  5965. return;
  5966. /* Check Flow Attention register */
  5967. val = tr32(HOSTCC_FLOW_ATTN);
  5968. if (val & ~HOSTCC_FLOW_ATTN_MBUF_LWM) {
  5969. netdev_err(tp->dev, "FLOW Attention error. Resetting chip.\n");
  5970. real_error = true;
  5971. }
  5972. if (tr32(MSGINT_STATUS) & ~MSGINT_STATUS_MSI_REQ) {
  5973. netdev_err(tp->dev, "MSI Status error. Resetting chip.\n");
  5974. real_error = true;
  5975. }
  5976. if (tr32(RDMAC_STATUS) || tr32(WDMAC_STATUS)) {
  5977. netdev_err(tp->dev, "DMA Status error. Resetting chip.\n");
  5978. real_error = true;
  5979. }
  5980. if (!real_error)
  5981. return;
  5982. tg3_dump_state(tp);
  5983. tg3_flag_set(tp, ERROR_PROCESSED);
  5984. tg3_reset_task_schedule(tp);
  5985. }
  5986. static int tg3_poll(struct napi_struct *napi, int budget)
  5987. {
  5988. struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
  5989. struct tg3 *tp = tnapi->tp;
  5990. int work_done = 0;
  5991. struct tg3_hw_status *sblk = tnapi->hw_status;
  5992. while (1) {
  5993. if (sblk->status & SD_STATUS_ERROR)
  5994. tg3_process_error(tp);
  5995. tg3_poll_link(tp);
  5996. work_done = tg3_poll_work(tnapi, work_done, budget);
  5997. if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
  5998. goto tx_recovery;
  5999. if (unlikely(work_done >= budget))
  6000. break;
  6001. if (tg3_flag(tp, TAGGED_STATUS)) {
  6002. /* tp->last_tag is used in tg3_int_reenable() below
  6003. * to tell the hw how much work has been processed,
  6004. * so we must read it before checking for more work.
  6005. */
  6006. tnapi->last_tag = sblk->status_tag;
  6007. tnapi->last_irq_tag = tnapi->last_tag;
  6008. rmb();
  6009. } else
  6010. sblk->status &= ~SD_STATUS_UPDATED;
  6011. if (likely(!tg3_has_work(tnapi))) {
  6012. napi_complete_done(napi, work_done);
  6013. tg3_int_reenable(tnapi);
  6014. break;
  6015. }
  6016. }
  6017. return work_done;
  6018. tx_recovery:
  6019. /* work_done is guaranteed to be less than budget. */
  6020. napi_complete(napi);
  6021. tg3_reset_task_schedule(tp);
  6022. return work_done;
  6023. }
  6024. static void tg3_napi_disable(struct tg3 *tp)
  6025. {
  6026. int i;
  6027. for (i = tp->irq_cnt - 1; i >= 0; i--)
  6028. napi_disable(&tp->napi[i].napi);
  6029. }
  6030. static void tg3_napi_enable(struct tg3 *tp)
  6031. {
  6032. int i;
  6033. for (i = 0; i < tp->irq_cnt; i++)
  6034. napi_enable(&tp->napi[i].napi);
  6035. }
  6036. static void tg3_napi_init(struct tg3 *tp)
  6037. {
  6038. int i;
  6039. netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
  6040. for (i = 1; i < tp->irq_cnt; i++)
  6041. netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
  6042. }
  6043. static void tg3_napi_fini(struct tg3 *tp)
  6044. {
  6045. int i;
  6046. for (i = 0; i < tp->irq_cnt; i++)
  6047. netif_napi_del(&tp->napi[i].napi);
  6048. }
  6049. static inline void tg3_netif_stop(struct tg3 *tp)
  6050. {
  6051. tp->dev->trans_start = jiffies; /* prevent tx timeout */
  6052. tg3_napi_disable(tp);
  6053. netif_carrier_off(tp->dev);
  6054. netif_tx_disable(tp->dev);
  6055. }
  6056. /* tp->lock must be held */
  6057. static inline void tg3_netif_start(struct tg3 *tp)
  6058. {
  6059. tg3_ptp_resume(tp);
  6060. /* NOTE: unconditional netif_tx_wake_all_queues is only
  6061. * appropriate so long as all callers are assured to
  6062. * have free tx slots (such as after tg3_init_hw)
  6063. */
  6064. netif_tx_wake_all_queues(tp->dev);
  6065. if (tp->link_up)
  6066. netif_carrier_on(tp->dev);
  6067. tg3_napi_enable(tp);
  6068. tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
  6069. tg3_enable_ints(tp);
  6070. }
  6071. static void tg3_irq_quiesce(struct tg3 *tp)
  6072. __releases(tp->lock)
  6073. __acquires(tp->lock)
  6074. {
  6075. int i;
  6076. BUG_ON(tp->irq_sync);
  6077. tp->irq_sync = 1;
  6078. smp_mb();
  6079. spin_unlock_bh(&tp->lock);
  6080. for (i = 0; i < tp->irq_cnt; i++)
  6081. synchronize_irq(tp->napi[i].irq_vec);
  6082. spin_lock_bh(&tp->lock);
  6083. }
  6084. /* Fully shutdown all tg3 driver activity elsewhere in the system.
  6085. * If irq_sync is non-zero, then the IRQ handler must be synchronized
  6086. * with as well. Most of the time, this is not necessary except when
  6087. * shutting down the device.
  6088. */
  6089. static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
  6090. {
  6091. spin_lock_bh(&tp->lock);
  6092. if (irq_sync)
  6093. tg3_irq_quiesce(tp);
  6094. }
  6095. static inline void tg3_full_unlock(struct tg3 *tp)
  6096. {
  6097. spin_unlock_bh(&tp->lock);
  6098. }
  6099. /* One-shot MSI handler - Chip automatically disables interrupt
  6100. * after sending MSI so driver doesn't have to do it.
  6101. */
  6102. static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
  6103. {
  6104. struct tg3_napi *tnapi = dev_id;
  6105. struct tg3 *tp = tnapi->tp;
  6106. prefetch(tnapi->hw_status);
  6107. if (tnapi->rx_rcb)
  6108. prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
  6109. if (likely(!tg3_irq_sync(tp)))
  6110. napi_schedule(&tnapi->napi);
  6111. return IRQ_HANDLED;
  6112. }
  6113. /* MSI ISR - No need to check for interrupt sharing and no need to
  6114. * flush status block and interrupt mailbox. PCI ordering rules
  6115. * guarantee that MSI will arrive after the status block.
  6116. */
  6117. static irqreturn_t tg3_msi(int irq, void *dev_id)
  6118. {
  6119. struct tg3_napi *tnapi = dev_id;
  6120. struct tg3 *tp = tnapi->tp;
  6121. prefetch(tnapi->hw_status);
  6122. if (tnapi->rx_rcb)
  6123. prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
  6124. /*
  6125. * Writing any value to intr-mbox-0 clears PCI INTA# and
  6126. * chip-internal interrupt pending events.
  6127. * Writing non-zero to intr-mbox-0 additional tells the
  6128. * NIC to stop sending us irqs, engaging "in-intr-handler"
  6129. * event coalescing.
  6130. */
  6131. tw32_mailbox(tnapi->int_mbox, 0x00000001);
  6132. if (likely(!tg3_irq_sync(tp)))
  6133. napi_schedule(&tnapi->napi);
  6134. return IRQ_RETVAL(1);
  6135. }
  6136. static irqreturn_t tg3_interrupt(int irq, void *dev_id)
  6137. {
  6138. struct tg3_napi *tnapi = dev_id;
  6139. struct tg3 *tp = tnapi->tp;
  6140. struct tg3_hw_status *sblk = tnapi->hw_status;
  6141. unsigned int handled = 1;
  6142. /* In INTx mode, it is possible for the interrupt to arrive at
  6143. * the CPU before the status block posted prior to the interrupt.
  6144. * Reading the PCI State register will confirm whether the
  6145. * interrupt is ours and will flush the status block.
  6146. */
  6147. if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
  6148. if (tg3_flag(tp, CHIP_RESETTING) ||
  6149. (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
  6150. handled = 0;
  6151. goto out;
  6152. }
  6153. }
  6154. /*
  6155. * Writing any value to intr-mbox-0 clears PCI INTA# and
  6156. * chip-internal interrupt pending events.
  6157. * Writing non-zero to intr-mbox-0 additional tells the
  6158. * NIC to stop sending us irqs, engaging "in-intr-handler"
  6159. * event coalescing.
  6160. *
  6161. * Flush the mailbox to de-assert the IRQ immediately to prevent
  6162. * spurious interrupts. The flush impacts performance but
  6163. * excessive spurious interrupts can be worse in some cases.
  6164. */
  6165. tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
  6166. if (tg3_irq_sync(tp))
  6167. goto out;
  6168. sblk->status &= ~SD_STATUS_UPDATED;
  6169. if (likely(tg3_has_work(tnapi))) {
  6170. prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
  6171. napi_schedule(&tnapi->napi);
  6172. } else {
  6173. /* No work, shared interrupt perhaps? re-enable
  6174. * interrupts, and flush that PCI write
  6175. */
  6176. tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
  6177. 0x00000000);
  6178. }
  6179. out:
  6180. return IRQ_RETVAL(handled);
  6181. }
  6182. static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
  6183. {
  6184. struct tg3_napi *tnapi = dev_id;
  6185. struct tg3 *tp = tnapi->tp;
  6186. struct tg3_hw_status *sblk = tnapi->hw_status;
  6187. unsigned int handled = 1;
  6188. /* In INTx mode, it is possible for the interrupt to arrive at
  6189. * the CPU before the status block posted prior to the interrupt.
  6190. * Reading the PCI State register will confirm whether the
  6191. * interrupt is ours and will flush the status block.
  6192. */
  6193. if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
  6194. if (tg3_flag(tp, CHIP_RESETTING) ||
  6195. (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
  6196. handled = 0;
  6197. goto out;
  6198. }
  6199. }
  6200. /*
  6201. * writing any value to intr-mbox-0 clears PCI INTA# and
  6202. * chip-internal interrupt pending events.
  6203. * writing non-zero to intr-mbox-0 additional tells the
  6204. * NIC to stop sending us irqs, engaging "in-intr-handler"
  6205. * event coalescing.
  6206. *
  6207. * Flush the mailbox to de-assert the IRQ immediately to prevent
  6208. * spurious interrupts. The flush impacts performance but
  6209. * excessive spurious interrupts can be worse in some cases.
  6210. */
  6211. tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
  6212. /*
  6213. * In a shared interrupt configuration, sometimes other devices'
  6214. * interrupts will scream. We record the current status tag here
  6215. * so that the above check can report that the screaming interrupts
  6216. * are unhandled. Eventually they will be silenced.
  6217. */
  6218. tnapi->last_irq_tag = sblk->status_tag;
  6219. if (tg3_irq_sync(tp))
  6220. goto out;
  6221. prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
  6222. napi_schedule(&tnapi->napi);
  6223. out:
  6224. return IRQ_RETVAL(handled);
  6225. }
  6226. /* ISR for interrupt test */
  6227. static irqreturn_t tg3_test_isr(int irq, void *dev_id)
  6228. {
  6229. struct tg3_napi *tnapi = dev_id;
  6230. struct tg3 *tp = tnapi->tp;
  6231. struct tg3_hw_status *sblk = tnapi->hw_status;
  6232. if ((sblk->status & SD_STATUS_UPDATED) ||
  6233. !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
  6234. tg3_disable_ints(tp);
  6235. return IRQ_RETVAL(1);
  6236. }
  6237. return IRQ_RETVAL(0);
  6238. }
  6239. #ifdef CONFIG_NET_POLL_CONTROLLER
  6240. static void tg3_poll_controller(struct net_device *dev)
  6241. {
  6242. int i;
  6243. struct tg3 *tp = netdev_priv(dev);
  6244. if (tg3_irq_sync(tp))
  6245. return;
  6246. for (i = 0; i < tp->irq_cnt; i++)
  6247. tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
  6248. }
  6249. #endif
  6250. static void tg3_tx_timeout(struct net_device *dev)
  6251. {
  6252. struct tg3 *tp = netdev_priv(dev);
  6253. if (netif_msg_tx_err(tp)) {
  6254. netdev_err(dev, "transmit timed out, resetting\n");
  6255. tg3_dump_state(tp);
  6256. }
  6257. tg3_reset_task_schedule(tp);
  6258. }
  6259. /* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
  6260. static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
  6261. {
  6262. u32 base = (u32) mapping & 0xffffffff;
  6263. return base + len + 8 < base;
  6264. }
  6265. /* Test for TSO DMA buffers that cross into regions which are within MSS bytes
  6266. * of any 4GB boundaries: 4G, 8G, etc
  6267. */
  6268. static inline int tg3_4g_tso_overflow_test(struct tg3 *tp, dma_addr_t mapping,
  6269. u32 len, u32 mss)
  6270. {
  6271. if (tg3_asic_rev(tp) == ASIC_REV_5762 && mss) {
  6272. u32 base = (u32) mapping & 0xffffffff;
  6273. return ((base + len + (mss & 0x3fff)) < base);
  6274. }
  6275. return 0;
  6276. }
  6277. /* Test for DMA addresses > 40-bit */
  6278. static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
  6279. int len)
  6280. {
  6281. #if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
  6282. if (tg3_flag(tp, 40BIT_DMA_BUG))
  6283. return ((u64) mapping + len) > DMA_BIT_MASK(40);
  6284. return 0;
  6285. #else
  6286. return 0;
  6287. #endif
  6288. }
  6289. static inline void tg3_tx_set_bd(struct tg3_tx_buffer_desc *txbd,
  6290. dma_addr_t mapping, u32 len, u32 flags,
  6291. u32 mss, u32 vlan)
  6292. {
  6293. txbd->addr_hi = ((u64) mapping >> 32);
  6294. txbd->addr_lo = ((u64) mapping & 0xffffffff);
  6295. txbd->len_flags = (len << TXD_LEN_SHIFT) | (flags & 0x0000ffff);
  6296. txbd->vlan_tag = (mss << TXD_MSS_SHIFT) | (vlan << TXD_VLAN_TAG_SHIFT);
  6297. }
  6298. static bool tg3_tx_frag_set(struct tg3_napi *tnapi, u32 *entry, u32 *budget,
  6299. dma_addr_t map, u32 len, u32 flags,
  6300. u32 mss, u32 vlan)
  6301. {
  6302. struct tg3 *tp = tnapi->tp;
  6303. bool hwbug = false;
  6304. if (tg3_flag(tp, SHORT_DMA_BUG) && len <= 8)
  6305. hwbug = true;
  6306. if (tg3_4g_overflow_test(map, len))
  6307. hwbug = true;
  6308. if (tg3_4g_tso_overflow_test(tp, map, len, mss))
  6309. hwbug = true;
  6310. if (tg3_40bit_overflow_test(tp, map, len))
  6311. hwbug = true;
  6312. if (tp->dma_limit) {
  6313. u32 prvidx = *entry;
  6314. u32 tmp_flag = flags & ~TXD_FLAG_END;
  6315. while (len > tp->dma_limit && *budget) {
  6316. u32 frag_len = tp->dma_limit;
  6317. len -= tp->dma_limit;
  6318. /* Avoid the 8byte DMA problem */
  6319. if (len <= 8) {
  6320. len += tp->dma_limit / 2;
  6321. frag_len = tp->dma_limit / 2;
  6322. }
  6323. tnapi->tx_buffers[*entry].fragmented = true;
  6324. tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
  6325. frag_len, tmp_flag, mss, vlan);
  6326. *budget -= 1;
  6327. prvidx = *entry;
  6328. *entry = NEXT_TX(*entry);
  6329. map += frag_len;
  6330. }
  6331. if (len) {
  6332. if (*budget) {
  6333. tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
  6334. len, flags, mss, vlan);
  6335. *budget -= 1;
  6336. *entry = NEXT_TX(*entry);
  6337. } else {
  6338. hwbug = true;
  6339. tnapi->tx_buffers[prvidx].fragmented = false;
  6340. }
  6341. }
  6342. } else {
  6343. tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
  6344. len, flags, mss, vlan);
  6345. *entry = NEXT_TX(*entry);
  6346. }
  6347. return hwbug;
  6348. }
  6349. static void tg3_tx_skb_unmap(struct tg3_napi *tnapi, u32 entry, int last)
  6350. {
  6351. int i;
  6352. struct sk_buff *skb;
  6353. struct tg3_tx_ring_info *txb = &tnapi->tx_buffers[entry];
  6354. skb = txb->skb;
  6355. txb->skb = NULL;
  6356. pci_unmap_single(tnapi->tp->pdev,
  6357. dma_unmap_addr(txb, mapping),
  6358. skb_headlen(skb),
  6359. PCI_DMA_TODEVICE);
  6360. while (txb->fragmented) {
  6361. txb->fragmented = false;
  6362. entry = NEXT_TX(entry);
  6363. txb = &tnapi->tx_buffers[entry];
  6364. }
  6365. for (i = 0; i <= last; i++) {
  6366. const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  6367. entry = NEXT_TX(entry);
  6368. txb = &tnapi->tx_buffers[entry];
  6369. pci_unmap_page(tnapi->tp->pdev,
  6370. dma_unmap_addr(txb, mapping),
  6371. skb_frag_size(frag), PCI_DMA_TODEVICE);
  6372. while (txb->fragmented) {
  6373. txb->fragmented = false;
  6374. entry = NEXT_TX(entry);
  6375. txb = &tnapi->tx_buffers[entry];
  6376. }
  6377. }
  6378. }
  6379. /* Workaround 4GB and 40-bit hardware DMA bugs. */
  6380. static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
  6381. struct sk_buff **pskb,
  6382. u32 *entry, u32 *budget,
  6383. u32 base_flags, u32 mss, u32 vlan)
  6384. {
  6385. struct tg3 *tp = tnapi->tp;
  6386. struct sk_buff *new_skb, *skb = *pskb;
  6387. dma_addr_t new_addr = 0;
  6388. int ret = 0;
  6389. if (tg3_asic_rev(tp) != ASIC_REV_5701)
  6390. new_skb = skb_copy(skb, GFP_ATOMIC);
  6391. else {
  6392. int more_headroom = 4 - ((unsigned long)skb->data & 3);
  6393. new_skb = skb_copy_expand(skb,
  6394. skb_headroom(skb) + more_headroom,
  6395. skb_tailroom(skb), GFP_ATOMIC);
  6396. }
  6397. if (!new_skb) {
  6398. ret = -1;
  6399. } else {
  6400. /* New SKB is guaranteed to be linear. */
  6401. new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
  6402. PCI_DMA_TODEVICE);
  6403. /* Make sure the mapping succeeded */
  6404. if (pci_dma_mapping_error(tp->pdev, new_addr)) {
  6405. dev_kfree_skb_any(new_skb);
  6406. ret = -1;
  6407. } else {
  6408. u32 save_entry = *entry;
  6409. base_flags |= TXD_FLAG_END;
  6410. tnapi->tx_buffers[*entry].skb = new_skb;
  6411. dma_unmap_addr_set(&tnapi->tx_buffers[*entry],
  6412. mapping, new_addr);
  6413. if (tg3_tx_frag_set(tnapi, entry, budget, new_addr,
  6414. new_skb->len, base_flags,
  6415. mss, vlan)) {
  6416. tg3_tx_skb_unmap(tnapi, save_entry, -1);
  6417. dev_kfree_skb_any(new_skb);
  6418. ret = -1;
  6419. }
  6420. }
  6421. }
  6422. dev_kfree_skb_any(skb);
  6423. *pskb = new_skb;
  6424. return ret;
  6425. }
  6426. static netdev_tx_t tg3_start_xmit(struct sk_buff *, struct net_device *);
  6427. /* Use GSO to workaround all TSO packets that meet HW bug conditions
  6428. * indicated in tg3_tx_frag_set()
  6429. */
  6430. static int tg3_tso_bug(struct tg3 *tp, struct tg3_napi *tnapi,
  6431. struct netdev_queue *txq, struct sk_buff *skb)
  6432. {
  6433. struct sk_buff *segs, *nskb;
  6434. u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
  6435. /* Estimate the number of fragments in the worst case */
  6436. if (unlikely(tg3_tx_avail(tnapi) <= frag_cnt_est)) {
  6437. netif_tx_stop_queue(txq);
  6438. /* netif_tx_stop_queue() must be done before checking
  6439. * checking tx index in tg3_tx_avail() below, because in
  6440. * tg3_tx(), we update tx index before checking for
  6441. * netif_tx_queue_stopped().
  6442. */
  6443. smp_mb();
  6444. if (tg3_tx_avail(tnapi) <= frag_cnt_est)
  6445. return NETDEV_TX_BUSY;
  6446. netif_tx_wake_queue(txq);
  6447. }
  6448. segs = skb_gso_segment(skb, tp->dev->features &
  6449. ~(NETIF_F_TSO | NETIF_F_TSO6));
  6450. if (IS_ERR(segs) || !segs)
  6451. goto tg3_tso_bug_end;
  6452. do {
  6453. nskb = segs;
  6454. segs = segs->next;
  6455. nskb->next = NULL;
  6456. tg3_start_xmit(nskb, tp->dev);
  6457. } while (segs);
  6458. tg3_tso_bug_end:
  6459. dev_kfree_skb_any(skb);
  6460. return NETDEV_TX_OK;
  6461. }
  6462. /* hard_start_xmit for all devices */
  6463. static netdev_tx_t tg3_start_xmit(struct sk_buff *skb, struct net_device *dev)
  6464. {
  6465. struct tg3 *tp = netdev_priv(dev);
  6466. u32 len, entry, base_flags, mss, vlan = 0;
  6467. u32 budget;
  6468. int i = -1, would_hit_hwbug;
  6469. dma_addr_t mapping;
  6470. struct tg3_napi *tnapi;
  6471. struct netdev_queue *txq;
  6472. unsigned int last;
  6473. struct iphdr *iph = NULL;
  6474. struct tcphdr *tcph = NULL;
  6475. __sum16 tcp_csum = 0, ip_csum = 0;
  6476. __be16 ip_tot_len = 0;
  6477. txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
  6478. tnapi = &tp->napi[skb_get_queue_mapping(skb)];
  6479. if (tg3_flag(tp, ENABLE_TSS))
  6480. tnapi++;
  6481. budget = tg3_tx_avail(tnapi);
  6482. /* We are running in BH disabled context with netif_tx_lock
  6483. * and TX reclaim runs via tp->napi.poll inside of a software
  6484. * interrupt. Furthermore, IRQ processing runs lockless so we have
  6485. * no IRQ context deadlocks to worry about either. Rejoice!
  6486. */
  6487. if (unlikely(budget <= (skb_shinfo(skb)->nr_frags + 1))) {
  6488. if (!netif_tx_queue_stopped(txq)) {
  6489. netif_tx_stop_queue(txq);
  6490. /* This is a hard error, log it. */
  6491. netdev_err(dev,
  6492. "BUG! Tx Ring full when queue awake!\n");
  6493. }
  6494. return NETDEV_TX_BUSY;
  6495. }
  6496. entry = tnapi->tx_prod;
  6497. base_flags = 0;
  6498. mss = skb_shinfo(skb)->gso_size;
  6499. if (mss) {
  6500. u32 tcp_opt_len, hdr_len;
  6501. if (skb_cow_head(skb, 0))
  6502. goto drop;
  6503. iph = ip_hdr(skb);
  6504. tcp_opt_len = tcp_optlen(skb);
  6505. hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb) - ETH_HLEN;
  6506. /* HW/FW can not correctly segment packets that have been
  6507. * vlan encapsulated.
  6508. */
  6509. if (skb->protocol == htons(ETH_P_8021Q) ||
  6510. skb->protocol == htons(ETH_P_8021AD))
  6511. return tg3_tso_bug(tp, tnapi, txq, skb);
  6512. if (!skb_is_gso_v6(skb)) {
  6513. if (unlikely((ETH_HLEN + hdr_len) > 80) &&
  6514. tg3_flag(tp, TSO_BUG))
  6515. return tg3_tso_bug(tp, tnapi, txq, skb);
  6516. ip_csum = iph->check;
  6517. ip_tot_len = iph->tot_len;
  6518. iph->check = 0;
  6519. iph->tot_len = htons(mss + hdr_len);
  6520. }
  6521. base_flags |= (TXD_FLAG_CPU_PRE_DMA |
  6522. TXD_FLAG_CPU_POST_DMA);
  6523. tcph = tcp_hdr(skb);
  6524. tcp_csum = tcph->check;
  6525. if (tg3_flag(tp, HW_TSO_1) ||
  6526. tg3_flag(tp, HW_TSO_2) ||
  6527. tg3_flag(tp, HW_TSO_3)) {
  6528. tcph->check = 0;
  6529. base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
  6530. } else {
  6531. tcph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
  6532. 0, IPPROTO_TCP, 0);
  6533. }
  6534. if (tg3_flag(tp, HW_TSO_3)) {
  6535. mss |= (hdr_len & 0xc) << 12;
  6536. if (hdr_len & 0x10)
  6537. base_flags |= 0x00000010;
  6538. base_flags |= (hdr_len & 0x3e0) << 5;
  6539. } else if (tg3_flag(tp, HW_TSO_2))
  6540. mss |= hdr_len << 9;
  6541. else if (tg3_flag(tp, HW_TSO_1) ||
  6542. tg3_asic_rev(tp) == ASIC_REV_5705) {
  6543. if (tcp_opt_len || iph->ihl > 5) {
  6544. int tsflags;
  6545. tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
  6546. mss |= (tsflags << 11);
  6547. }
  6548. } else {
  6549. if (tcp_opt_len || iph->ihl > 5) {
  6550. int tsflags;
  6551. tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
  6552. base_flags |= tsflags << 12;
  6553. }
  6554. }
  6555. } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
  6556. /* HW/FW can not correctly checksum packets that have been
  6557. * vlan encapsulated.
  6558. */
  6559. if (skb->protocol == htons(ETH_P_8021Q) ||
  6560. skb->protocol == htons(ETH_P_8021AD)) {
  6561. if (skb_checksum_help(skb))
  6562. goto drop;
  6563. } else {
  6564. base_flags |= TXD_FLAG_TCPUDP_CSUM;
  6565. }
  6566. }
  6567. if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
  6568. !mss && skb->len > VLAN_ETH_FRAME_LEN)
  6569. base_flags |= TXD_FLAG_JMB_PKT;
  6570. if (skb_vlan_tag_present(skb)) {
  6571. base_flags |= TXD_FLAG_VLAN;
  6572. vlan = skb_vlan_tag_get(skb);
  6573. }
  6574. if ((unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) &&
  6575. tg3_flag(tp, TX_TSTAMP_EN)) {
  6576. skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
  6577. base_flags |= TXD_FLAG_HWTSTAMP;
  6578. }
  6579. len = skb_headlen(skb);
  6580. mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
  6581. if (pci_dma_mapping_error(tp->pdev, mapping))
  6582. goto drop;
  6583. tnapi->tx_buffers[entry].skb = skb;
  6584. dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
  6585. would_hit_hwbug = 0;
  6586. if (tg3_flag(tp, 5701_DMA_BUG))
  6587. would_hit_hwbug = 1;
  6588. if (tg3_tx_frag_set(tnapi, &entry, &budget, mapping, len, base_flags |
  6589. ((skb_shinfo(skb)->nr_frags == 0) ? TXD_FLAG_END : 0),
  6590. mss, vlan)) {
  6591. would_hit_hwbug = 1;
  6592. } else if (skb_shinfo(skb)->nr_frags > 0) {
  6593. u32 tmp_mss = mss;
  6594. if (!tg3_flag(tp, HW_TSO_1) &&
  6595. !tg3_flag(tp, HW_TSO_2) &&
  6596. !tg3_flag(tp, HW_TSO_3))
  6597. tmp_mss = 0;
  6598. /* Now loop through additional data
  6599. * fragments, and queue them.
  6600. */
  6601. last = skb_shinfo(skb)->nr_frags - 1;
  6602. for (i = 0; i <= last; i++) {
  6603. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  6604. len = skb_frag_size(frag);
  6605. mapping = skb_frag_dma_map(&tp->pdev->dev, frag, 0,
  6606. len, DMA_TO_DEVICE);
  6607. tnapi->tx_buffers[entry].skb = NULL;
  6608. dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
  6609. mapping);
  6610. if (dma_mapping_error(&tp->pdev->dev, mapping))
  6611. goto dma_error;
  6612. if (!budget ||
  6613. tg3_tx_frag_set(tnapi, &entry, &budget, mapping,
  6614. len, base_flags |
  6615. ((i == last) ? TXD_FLAG_END : 0),
  6616. tmp_mss, vlan)) {
  6617. would_hit_hwbug = 1;
  6618. break;
  6619. }
  6620. }
  6621. }
  6622. if (would_hit_hwbug) {
  6623. tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, i);
  6624. if (mss) {
  6625. /* If it's a TSO packet, do GSO instead of
  6626. * allocating and copying to a large linear SKB
  6627. */
  6628. if (ip_tot_len) {
  6629. iph->check = ip_csum;
  6630. iph->tot_len = ip_tot_len;
  6631. }
  6632. tcph->check = tcp_csum;
  6633. return tg3_tso_bug(tp, tnapi, txq, skb);
  6634. }
  6635. /* If the workaround fails due to memory/mapping
  6636. * failure, silently drop this packet.
  6637. */
  6638. entry = tnapi->tx_prod;
  6639. budget = tg3_tx_avail(tnapi);
  6640. if (tigon3_dma_hwbug_workaround(tnapi, &skb, &entry, &budget,
  6641. base_flags, mss, vlan))
  6642. goto drop_nofree;
  6643. }
  6644. skb_tx_timestamp(skb);
  6645. netdev_tx_sent_queue(txq, skb->len);
  6646. /* Sync BD data before updating mailbox */
  6647. wmb();
  6648. tnapi->tx_prod = entry;
  6649. if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
  6650. netif_tx_stop_queue(txq);
  6651. /* netif_tx_stop_queue() must be done before checking
  6652. * checking tx index in tg3_tx_avail() below, because in
  6653. * tg3_tx(), we update tx index before checking for
  6654. * netif_tx_queue_stopped().
  6655. */
  6656. smp_mb();
  6657. if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
  6658. netif_tx_wake_queue(txq);
  6659. }
  6660. if (!skb->xmit_more || netif_xmit_stopped(txq)) {
  6661. /* Packets are ready, update Tx producer idx on card. */
  6662. tw32_tx_mbox(tnapi->prodmbox, entry);
  6663. mmiowb();
  6664. }
  6665. return NETDEV_TX_OK;
  6666. dma_error:
  6667. tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, --i);
  6668. tnapi->tx_buffers[tnapi->tx_prod].skb = NULL;
  6669. drop:
  6670. dev_kfree_skb_any(skb);
  6671. drop_nofree:
  6672. tp->tx_dropped++;
  6673. return NETDEV_TX_OK;
  6674. }
  6675. static void tg3_mac_loopback(struct tg3 *tp, bool enable)
  6676. {
  6677. if (enable) {
  6678. tp->mac_mode &= ~(MAC_MODE_HALF_DUPLEX |
  6679. MAC_MODE_PORT_MODE_MASK);
  6680. tp->mac_mode |= MAC_MODE_PORT_INT_LPBACK;
  6681. if (!tg3_flag(tp, 5705_PLUS))
  6682. tp->mac_mode |= MAC_MODE_LINK_POLARITY;
  6683. if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
  6684. tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
  6685. else
  6686. tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
  6687. } else {
  6688. tp->mac_mode &= ~MAC_MODE_PORT_INT_LPBACK;
  6689. if (tg3_flag(tp, 5705_PLUS) ||
  6690. (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) ||
  6691. tg3_asic_rev(tp) == ASIC_REV_5700)
  6692. tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
  6693. }
  6694. tw32(MAC_MODE, tp->mac_mode);
  6695. udelay(40);
  6696. }
  6697. static int tg3_phy_lpbk_set(struct tg3 *tp, u32 speed, bool extlpbk)
  6698. {
  6699. u32 val, bmcr, mac_mode, ptest = 0;
  6700. tg3_phy_toggle_apd(tp, false);
  6701. tg3_phy_toggle_automdix(tp, false);
  6702. if (extlpbk && tg3_phy_set_extloopbk(tp))
  6703. return -EIO;
  6704. bmcr = BMCR_FULLDPLX;
  6705. switch (speed) {
  6706. case SPEED_10:
  6707. break;
  6708. case SPEED_100:
  6709. bmcr |= BMCR_SPEED100;
  6710. break;
  6711. case SPEED_1000:
  6712. default:
  6713. if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
  6714. speed = SPEED_100;
  6715. bmcr |= BMCR_SPEED100;
  6716. } else {
  6717. speed = SPEED_1000;
  6718. bmcr |= BMCR_SPEED1000;
  6719. }
  6720. }
  6721. if (extlpbk) {
  6722. if (!(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
  6723. tg3_readphy(tp, MII_CTRL1000, &val);
  6724. val |= CTL1000_AS_MASTER |
  6725. CTL1000_ENABLE_MASTER;
  6726. tg3_writephy(tp, MII_CTRL1000, val);
  6727. } else {
  6728. ptest = MII_TG3_FET_PTEST_TRIM_SEL |
  6729. MII_TG3_FET_PTEST_TRIM_2;
  6730. tg3_writephy(tp, MII_TG3_FET_PTEST, ptest);
  6731. }
  6732. } else
  6733. bmcr |= BMCR_LOOPBACK;
  6734. tg3_writephy(tp, MII_BMCR, bmcr);
  6735. /* The write needs to be flushed for the FETs */
  6736. if (tp->phy_flags & TG3_PHYFLG_IS_FET)
  6737. tg3_readphy(tp, MII_BMCR, &bmcr);
  6738. udelay(40);
  6739. if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
  6740. tg3_asic_rev(tp) == ASIC_REV_5785) {
  6741. tg3_writephy(tp, MII_TG3_FET_PTEST, ptest |
  6742. MII_TG3_FET_PTEST_FRC_TX_LINK |
  6743. MII_TG3_FET_PTEST_FRC_TX_LOCK);
  6744. /* The write needs to be flushed for the AC131 */
  6745. tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
  6746. }
  6747. /* Reset to prevent losing 1st rx packet intermittently */
  6748. if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
  6749. tg3_flag(tp, 5780_CLASS)) {
  6750. tw32_f(MAC_RX_MODE, RX_MODE_RESET);
  6751. udelay(10);
  6752. tw32_f(MAC_RX_MODE, tp->rx_mode);
  6753. }
  6754. mac_mode = tp->mac_mode &
  6755. ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
  6756. if (speed == SPEED_1000)
  6757. mac_mode |= MAC_MODE_PORT_MODE_GMII;
  6758. else
  6759. mac_mode |= MAC_MODE_PORT_MODE_MII;
  6760. if (tg3_asic_rev(tp) == ASIC_REV_5700) {
  6761. u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
  6762. if (masked_phy_id == TG3_PHY_ID_BCM5401)
  6763. mac_mode &= ~MAC_MODE_LINK_POLARITY;
  6764. else if (masked_phy_id == TG3_PHY_ID_BCM5411)
  6765. mac_mode |= MAC_MODE_LINK_POLARITY;
  6766. tg3_writephy(tp, MII_TG3_EXT_CTRL,
  6767. MII_TG3_EXT_CTRL_LNK3_LED_MODE);
  6768. }
  6769. tw32(MAC_MODE, mac_mode);
  6770. udelay(40);
  6771. return 0;
  6772. }
  6773. static void tg3_set_loopback(struct net_device *dev, netdev_features_t features)
  6774. {
  6775. struct tg3 *tp = netdev_priv(dev);
  6776. if (features & NETIF_F_LOOPBACK) {
  6777. if (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK)
  6778. return;
  6779. spin_lock_bh(&tp->lock);
  6780. tg3_mac_loopback(tp, true);
  6781. netif_carrier_on(tp->dev);
  6782. spin_unlock_bh(&tp->lock);
  6783. netdev_info(dev, "Internal MAC loopback mode enabled.\n");
  6784. } else {
  6785. if (!(tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
  6786. return;
  6787. spin_lock_bh(&tp->lock);
  6788. tg3_mac_loopback(tp, false);
  6789. /* Force link status check */
  6790. tg3_setup_phy(tp, true);
  6791. spin_unlock_bh(&tp->lock);
  6792. netdev_info(dev, "Internal MAC loopback mode disabled.\n");
  6793. }
  6794. }
  6795. static netdev_features_t tg3_fix_features(struct net_device *dev,
  6796. netdev_features_t features)
  6797. {
  6798. struct tg3 *tp = netdev_priv(dev);
  6799. if (dev->mtu > ETH_DATA_LEN && tg3_flag(tp, 5780_CLASS))
  6800. features &= ~NETIF_F_ALL_TSO;
  6801. return features;
  6802. }
  6803. static int tg3_set_features(struct net_device *dev, netdev_features_t features)
  6804. {
  6805. netdev_features_t changed = dev->features ^ features;
  6806. if ((changed & NETIF_F_LOOPBACK) && netif_running(dev))
  6807. tg3_set_loopback(dev, features);
  6808. return 0;
  6809. }
  6810. static void tg3_rx_prodring_free(struct tg3 *tp,
  6811. struct tg3_rx_prodring_set *tpr)
  6812. {
  6813. int i;
  6814. if (tpr != &tp->napi[0].prodring) {
  6815. for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
  6816. i = (i + 1) & tp->rx_std_ring_mask)
  6817. tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
  6818. tp->rx_pkt_map_sz);
  6819. if (tg3_flag(tp, JUMBO_CAPABLE)) {
  6820. for (i = tpr->rx_jmb_cons_idx;
  6821. i != tpr->rx_jmb_prod_idx;
  6822. i = (i + 1) & tp->rx_jmb_ring_mask) {
  6823. tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
  6824. TG3_RX_JMB_MAP_SZ);
  6825. }
  6826. }
  6827. return;
  6828. }
  6829. for (i = 0; i <= tp->rx_std_ring_mask; i++)
  6830. tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
  6831. tp->rx_pkt_map_sz);
  6832. if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
  6833. for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
  6834. tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
  6835. TG3_RX_JMB_MAP_SZ);
  6836. }
  6837. }
  6838. /* Initialize rx rings for packet processing.
  6839. *
  6840. * The chip has been shut down and the driver detached from
  6841. * the networking, so no interrupts or new tx packets will
  6842. * end up in the driver. tp->{tx,}lock are held and thus
  6843. * we may not sleep.
  6844. */
  6845. static int tg3_rx_prodring_alloc(struct tg3 *tp,
  6846. struct tg3_rx_prodring_set *tpr)
  6847. {
  6848. u32 i, rx_pkt_dma_sz;
  6849. tpr->rx_std_cons_idx = 0;
  6850. tpr->rx_std_prod_idx = 0;
  6851. tpr->rx_jmb_cons_idx = 0;
  6852. tpr->rx_jmb_prod_idx = 0;
  6853. if (tpr != &tp->napi[0].prodring) {
  6854. memset(&tpr->rx_std_buffers[0], 0,
  6855. TG3_RX_STD_BUFF_RING_SIZE(tp));
  6856. if (tpr->rx_jmb_buffers)
  6857. memset(&tpr->rx_jmb_buffers[0], 0,
  6858. TG3_RX_JMB_BUFF_RING_SIZE(tp));
  6859. goto done;
  6860. }
  6861. /* Zero out all descriptors. */
  6862. memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
  6863. rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
  6864. if (tg3_flag(tp, 5780_CLASS) &&
  6865. tp->dev->mtu > ETH_DATA_LEN)
  6866. rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
  6867. tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
  6868. /* Initialize invariants of the rings, we only set this
  6869. * stuff once. This works because the card does not
  6870. * write into the rx buffer posting rings.
  6871. */
  6872. for (i = 0; i <= tp->rx_std_ring_mask; i++) {
  6873. struct tg3_rx_buffer_desc *rxd;
  6874. rxd = &tpr->rx_std[i];
  6875. rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
  6876. rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
  6877. rxd->opaque = (RXD_OPAQUE_RING_STD |
  6878. (i << RXD_OPAQUE_INDEX_SHIFT));
  6879. }
  6880. /* Now allocate fresh SKBs for each rx ring. */
  6881. for (i = 0; i < tp->rx_pending; i++) {
  6882. unsigned int frag_size;
  6883. if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_STD, i,
  6884. &frag_size) < 0) {
  6885. netdev_warn(tp->dev,
  6886. "Using a smaller RX standard ring. Only "
  6887. "%d out of %d buffers were allocated "
  6888. "successfully\n", i, tp->rx_pending);
  6889. if (i == 0)
  6890. goto initfail;
  6891. tp->rx_pending = i;
  6892. break;
  6893. }
  6894. }
  6895. if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
  6896. goto done;
  6897. memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
  6898. if (!tg3_flag(tp, JUMBO_RING_ENABLE))
  6899. goto done;
  6900. for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
  6901. struct tg3_rx_buffer_desc *rxd;
  6902. rxd = &tpr->rx_jmb[i].std;
  6903. rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
  6904. rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
  6905. RXD_FLAG_JUMBO;
  6906. rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
  6907. (i << RXD_OPAQUE_INDEX_SHIFT));
  6908. }
  6909. for (i = 0; i < tp->rx_jumbo_pending; i++) {
  6910. unsigned int frag_size;
  6911. if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_JUMBO, i,
  6912. &frag_size) < 0) {
  6913. netdev_warn(tp->dev,
  6914. "Using a smaller RX jumbo ring. Only %d "
  6915. "out of %d buffers were allocated "
  6916. "successfully\n", i, tp->rx_jumbo_pending);
  6917. if (i == 0)
  6918. goto initfail;
  6919. tp->rx_jumbo_pending = i;
  6920. break;
  6921. }
  6922. }
  6923. done:
  6924. return 0;
  6925. initfail:
  6926. tg3_rx_prodring_free(tp, tpr);
  6927. return -ENOMEM;
  6928. }
  6929. static void tg3_rx_prodring_fini(struct tg3 *tp,
  6930. struct tg3_rx_prodring_set *tpr)
  6931. {
  6932. kfree(tpr->rx_std_buffers);
  6933. tpr->rx_std_buffers = NULL;
  6934. kfree(tpr->rx_jmb_buffers);
  6935. tpr->rx_jmb_buffers = NULL;
  6936. if (tpr->rx_std) {
  6937. dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
  6938. tpr->rx_std, tpr->rx_std_mapping);
  6939. tpr->rx_std = NULL;
  6940. }
  6941. if (tpr->rx_jmb) {
  6942. dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
  6943. tpr->rx_jmb, tpr->rx_jmb_mapping);
  6944. tpr->rx_jmb = NULL;
  6945. }
  6946. }
  6947. static int tg3_rx_prodring_init(struct tg3 *tp,
  6948. struct tg3_rx_prodring_set *tpr)
  6949. {
  6950. tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
  6951. GFP_KERNEL);
  6952. if (!tpr->rx_std_buffers)
  6953. return -ENOMEM;
  6954. tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
  6955. TG3_RX_STD_RING_BYTES(tp),
  6956. &tpr->rx_std_mapping,
  6957. GFP_KERNEL);
  6958. if (!tpr->rx_std)
  6959. goto err_out;
  6960. if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
  6961. tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
  6962. GFP_KERNEL);
  6963. if (!tpr->rx_jmb_buffers)
  6964. goto err_out;
  6965. tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
  6966. TG3_RX_JMB_RING_BYTES(tp),
  6967. &tpr->rx_jmb_mapping,
  6968. GFP_KERNEL);
  6969. if (!tpr->rx_jmb)
  6970. goto err_out;
  6971. }
  6972. return 0;
  6973. err_out:
  6974. tg3_rx_prodring_fini(tp, tpr);
  6975. return -ENOMEM;
  6976. }
  6977. /* Free up pending packets in all rx/tx rings.
  6978. *
  6979. * The chip has been shut down and the driver detached from
  6980. * the networking, so no interrupts or new tx packets will
  6981. * end up in the driver. tp->{tx,}lock is not held and we are not
  6982. * in an interrupt context and thus may sleep.
  6983. */
  6984. static void tg3_free_rings(struct tg3 *tp)
  6985. {
  6986. int i, j;
  6987. for (j = 0; j < tp->irq_cnt; j++) {
  6988. struct tg3_napi *tnapi = &tp->napi[j];
  6989. tg3_rx_prodring_free(tp, &tnapi->prodring);
  6990. if (!tnapi->tx_buffers)
  6991. continue;
  6992. for (i = 0; i < TG3_TX_RING_SIZE; i++) {
  6993. struct sk_buff *skb = tnapi->tx_buffers[i].skb;
  6994. if (!skb)
  6995. continue;
  6996. tg3_tx_skb_unmap(tnapi, i,
  6997. skb_shinfo(skb)->nr_frags - 1);
  6998. dev_kfree_skb_any(skb);
  6999. }
  7000. netdev_tx_reset_queue(netdev_get_tx_queue(tp->dev, j));
  7001. }
  7002. }
  7003. /* Initialize tx/rx rings for packet processing.
  7004. *
  7005. * The chip has been shut down and the driver detached from
  7006. * the networking, so no interrupts or new tx packets will
  7007. * end up in the driver. tp->{tx,}lock are held and thus
  7008. * we may not sleep.
  7009. */
  7010. static int tg3_init_rings(struct tg3 *tp)
  7011. {
  7012. int i;
  7013. /* Free up all the SKBs. */
  7014. tg3_free_rings(tp);
  7015. for (i = 0; i < tp->irq_cnt; i++) {
  7016. struct tg3_napi *tnapi = &tp->napi[i];
  7017. tnapi->last_tag = 0;
  7018. tnapi->last_irq_tag = 0;
  7019. tnapi->hw_status->status = 0;
  7020. tnapi->hw_status->status_tag = 0;
  7021. memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
  7022. tnapi->tx_prod = 0;
  7023. tnapi->tx_cons = 0;
  7024. if (tnapi->tx_ring)
  7025. memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
  7026. tnapi->rx_rcb_ptr = 0;
  7027. if (tnapi->rx_rcb)
  7028. memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
  7029. if (tnapi->prodring.rx_std &&
  7030. tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
  7031. tg3_free_rings(tp);
  7032. return -ENOMEM;
  7033. }
  7034. }
  7035. return 0;
  7036. }
  7037. static void tg3_mem_tx_release(struct tg3 *tp)
  7038. {
  7039. int i;
  7040. for (i = 0; i < tp->irq_max; i++) {
  7041. struct tg3_napi *tnapi = &tp->napi[i];
  7042. if (tnapi->tx_ring) {
  7043. dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
  7044. tnapi->tx_ring, tnapi->tx_desc_mapping);
  7045. tnapi->tx_ring = NULL;
  7046. }
  7047. kfree(tnapi->tx_buffers);
  7048. tnapi->tx_buffers = NULL;
  7049. }
  7050. }
  7051. static int tg3_mem_tx_acquire(struct tg3 *tp)
  7052. {
  7053. int i;
  7054. struct tg3_napi *tnapi = &tp->napi[0];
  7055. /* If multivector TSS is enabled, vector 0 does not handle
  7056. * tx interrupts. Don't allocate any resources for it.
  7057. */
  7058. if (tg3_flag(tp, ENABLE_TSS))
  7059. tnapi++;
  7060. for (i = 0; i < tp->txq_cnt; i++, tnapi++) {
  7061. tnapi->tx_buffers = kzalloc(sizeof(struct tg3_tx_ring_info) *
  7062. TG3_TX_RING_SIZE, GFP_KERNEL);
  7063. if (!tnapi->tx_buffers)
  7064. goto err_out;
  7065. tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
  7066. TG3_TX_RING_BYTES,
  7067. &tnapi->tx_desc_mapping,
  7068. GFP_KERNEL);
  7069. if (!tnapi->tx_ring)
  7070. goto err_out;
  7071. }
  7072. return 0;
  7073. err_out:
  7074. tg3_mem_tx_release(tp);
  7075. return -ENOMEM;
  7076. }
  7077. static void tg3_mem_rx_release(struct tg3 *tp)
  7078. {
  7079. int i;
  7080. for (i = 0; i < tp->irq_max; i++) {
  7081. struct tg3_napi *tnapi = &tp->napi[i];
  7082. tg3_rx_prodring_fini(tp, &tnapi->prodring);
  7083. if (!tnapi->rx_rcb)
  7084. continue;
  7085. dma_free_coherent(&tp->pdev->dev,
  7086. TG3_RX_RCB_RING_BYTES(tp),
  7087. tnapi->rx_rcb,
  7088. tnapi->rx_rcb_mapping);
  7089. tnapi->rx_rcb = NULL;
  7090. }
  7091. }
  7092. static int tg3_mem_rx_acquire(struct tg3 *tp)
  7093. {
  7094. unsigned int i, limit;
  7095. limit = tp->rxq_cnt;
  7096. /* If RSS is enabled, we need a (dummy) producer ring
  7097. * set on vector zero. This is the true hw prodring.
  7098. */
  7099. if (tg3_flag(tp, ENABLE_RSS))
  7100. limit++;
  7101. for (i = 0; i < limit; i++) {
  7102. struct tg3_napi *tnapi = &tp->napi[i];
  7103. if (tg3_rx_prodring_init(tp, &tnapi->prodring))
  7104. goto err_out;
  7105. /* If multivector RSS is enabled, vector 0
  7106. * does not handle rx or tx interrupts.
  7107. * Don't allocate any resources for it.
  7108. */
  7109. if (!i && tg3_flag(tp, ENABLE_RSS))
  7110. continue;
  7111. tnapi->rx_rcb = dma_zalloc_coherent(&tp->pdev->dev,
  7112. TG3_RX_RCB_RING_BYTES(tp),
  7113. &tnapi->rx_rcb_mapping,
  7114. GFP_KERNEL);
  7115. if (!tnapi->rx_rcb)
  7116. goto err_out;
  7117. }
  7118. return 0;
  7119. err_out:
  7120. tg3_mem_rx_release(tp);
  7121. return -ENOMEM;
  7122. }
  7123. /*
  7124. * Must not be invoked with interrupt sources disabled and
  7125. * the hardware shutdown down.
  7126. */
  7127. static void tg3_free_consistent(struct tg3 *tp)
  7128. {
  7129. int i;
  7130. for (i = 0; i < tp->irq_cnt; i++) {
  7131. struct tg3_napi *tnapi = &tp->napi[i];
  7132. if (tnapi->hw_status) {
  7133. dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
  7134. tnapi->hw_status,
  7135. tnapi->status_mapping);
  7136. tnapi->hw_status = NULL;
  7137. }
  7138. }
  7139. tg3_mem_rx_release(tp);
  7140. tg3_mem_tx_release(tp);
  7141. if (tp->hw_stats) {
  7142. dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
  7143. tp->hw_stats, tp->stats_mapping);
  7144. tp->hw_stats = NULL;
  7145. }
  7146. }
  7147. /*
  7148. * Must not be invoked with interrupt sources disabled and
  7149. * the hardware shutdown down. Can sleep.
  7150. */
  7151. static int tg3_alloc_consistent(struct tg3 *tp)
  7152. {
  7153. int i;
  7154. tp->hw_stats = dma_zalloc_coherent(&tp->pdev->dev,
  7155. sizeof(struct tg3_hw_stats),
  7156. &tp->stats_mapping, GFP_KERNEL);
  7157. if (!tp->hw_stats)
  7158. goto err_out;
  7159. for (i = 0; i < tp->irq_cnt; i++) {
  7160. struct tg3_napi *tnapi = &tp->napi[i];
  7161. struct tg3_hw_status *sblk;
  7162. tnapi->hw_status = dma_zalloc_coherent(&tp->pdev->dev,
  7163. TG3_HW_STATUS_SIZE,
  7164. &tnapi->status_mapping,
  7165. GFP_KERNEL);
  7166. if (!tnapi->hw_status)
  7167. goto err_out;
  7168. sblk = tnapi->hw_status;
  7169. if (tg3_flag(tp, ENABLE_RSS)) {
  7170. u16 *prodptr = NULL;
  7171. /*
  7172. * When RSS is enabled, the status block format changes
  7173. * slightly. The "rx_jumbo_consumer", "reserved",
  7174. * and "rx_mini_consumer" members get mapped to the
  7175. * other three rx return ring producer indexes.
  7176. */
  7177. switch (i) {
  7178. case 1:
  7179. prodptr = &sblk->idx[0].rx_producer;
  7180. break;
  7181. case 2:
  7182. prodptr = &sblk->rx_jumbo_consumer;
  7183. break;
  7184. case 3:
  7185. prodptr = &sblk->reserved;
  7186. break;
  7187. case 4:
  7188. prodptr = &sblk->rx_mini_consumer;
  7189. break;
  7190. }
  7191. tnapi->rx_rcb_prod_idx = prodptr;
  7192. } else {
  7193. tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
  7194. }
  7195. }
  7196. if (tg3_mem_tx_acquire(tp) || tg3_mem_rx_acquire(tp))
  7197. goto err_out;
  7198. return 0;
  7199. err_out:
  7200. tg3_free_consistent(tp);
  7201. return -ENOMEM;
  7202. }
  7203. #define MAX_WAIT_CNT 1000
  7204. /* To stop a block, clear the enable bit and poll till it
  7205. * clears. tp->lock is held.
  7206. */
  7207. static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, bool silent)
  7208. {
  7209. unsigned int i;
  7210. u32 val;
  7211. if (tg3_flag(tp, 5705_PLUS)) {
  7212. switch (ofs) {
  7213. case RCVLSC_MODE:
  7214. case DMAC_MODE:
  7215. case MBFREE_MODE:
  7216. case BUFMGR_MODE:
  7217. case MEMARB_MODE:
  7218. /* We can't enable/disable these bits of the
  7219. * 5705/5750, just say success.
  7220. */
  7221. return 0;
  7222. default:
  7223. break;
  7224. }
  7225. }
  7226. val = tr32(ofs);
  7227. val &= ~enable_bit;
  7228. tw32_f(ofs, val);
  7229. for (i = 0; i < MAX_WAIT_CNT; i++) {
  7230. if (pci_channel_offline(tp->pdev)) {
  7231. dev_err(&tp->pdev->dev,
  7232. "tg3_stop_block device offline, "
  7233. "ofs=%lx enable_bit=%x\n",
  7234. ofs, enable_bit);
  7235. return -ENODEV;
  7236. }
  7237. udelay(100);
  7238. val = tr32(ofs);
  7239. if ((val & enable_bit) == 0)
  7240. break;
  7241. }
  7242. if (i == MAX_WAIT_CNT && !silent) {
  7243. dev_err(&tp->pdev->dev,
  7244. "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
  7245. ofs, enable_bit);
  7246. return -ENODEV;
  7247. }
  7248. return 0;
  7249. }
  7250. /* tp->lock is held. */
  7251. static int tg3_abort_hw(struct tg3 *tp, bool silent)
  7252. {
  7253. int i, err;
  7254. tg3_disable_ints(tp);
  7255. if (pci_channel_offline(tp->pdev)) {
  7256. tp->rx_mode &= ~(RX_MODE_ENABLE | TX_MODE_ENABLE);
  7257. tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
  7258. err = -ENODEV;
  7259. goto err_no_dev;
  7260. }
  7261. tp->rx_mode &= ~RX_MODE_ENABLE;
  7262. tw32_f(MAC_RX_MODE, tp->rx_mode);
  7263. udelay(10);
  7264. err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
  7265. err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
  7266. err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
  7267. err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
  7268. err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
  7269. err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
  7270. err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
  7271. err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
  7272. err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
  7273. err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
  7274. err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
  7275. err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
  7276. err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
  7277. tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
  7278. tw32_f(MAC_MODE, tp->mac_mode);
  7279. udelay(40);
  7280. tp->tx_mode &= ~TX_MODE_ENABLE;
  7281. tw32_f(MAC_TX_MODE, tp->tx_mode);
  7282. for (i = 0; i < MAX_WAIT_CNT; i++) {
  7283. udelay(100);
  7284. if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
  7285. break;
  7286. }
  7287. if (i >= MAX_WAIT_CNT) {
  7288. dev_err(&tp->pdev->dev,
  7289. "%s timed out, TX_MODE_ENABLE will not clear "
  7290. "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
  7291. err |= -ENODEV;
  7292. }
  7293. err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
  7294. err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
  7295. err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
  7296. tw32(FTQ_RESET, 0xffffffff);
  7297. tw32(FTQ_RESET, 0x00000000);
  7298. err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
  7299. err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
  7300. err_no_dev:
  7301. for (i = 0; i < tp->irq_cnt; i++) {
  7302. struct tg3_napi *tnapi = &tp->napi[i];
  7303. if (tnapi->hw_status)
  7304. memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
  7305. }
  7306. return err;
  7307. }
  7308. /* Save PCI command register before chip reset */
  7309. static void tg3_save_pci_state(struct tg3 *tp)
  7310. {
  7311. pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
  7312. }
  7313. /* Restore PCI state after chip reset */
  7314. static void tg3_restore_pci_state(struct tg3 *tp)
  7315. {
  7316. u32 val;
  7317. /* Re-enable indirect register accesses. */
  7318. pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
  7319. tp->misc_host_ctrl);
  7320. /* Set MAX PCI retry to zero. */
  7321. val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
  7322. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0 &&
  7323. tg3_flag(tp, PCIX_MODE))
  7324. val |= PCISTATE_RETRY_SAME_DMA;
  7325. /* Allow reads and writes to the APE register and memory space. */
  7326. if (tg3_flag(tp, ENABLE_APE))
  7327. val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
  7328. PCISTATE_ALLOW_APE_SHMEM_WR |
  7329. PCISTATE_ALLOW_APE_PSPACE_WR;
  7330. pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
  7331. pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
  7332. if (!tg3_flag(tp, PCI_EXPRESS)) {
  7333. pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
  7334. tp->pci_cacheline_sz);
  7335. pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
  7336. tp->pci_lat_timer);
  7337. }
  7338. /* Make sure PCI-X relaxed ordering bit is clear. */
  7339. if (tg3_flag(tp, PCIX_MODE)) {
  7340. u16 pcix_cmd;
  7341. pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
  7342. &pcix_cmd);
  7343. pcix_cmd &= ~PCI_X_CMD_ERO;
  7344. pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
  7345. pcix_cmd);
  7346. }
  7347. if (tg3_flag(tp, 5780_CLASS)) {
  7348. /* Chip reset on 5780 will reset MSI enable bit,
  7349. * so need to restore it.
  7350. */
  7351. if (tg3_flag(tp, USING_MSI)) {
  7352. u16 ctrl;
  7353. pci_read_config_word(tp->pdev,
  7354. tp->msi_cap + PCI_MSI_FLAGS,
  7355. &ctrl);
  7356. pci_write_config_word(tp->pdev,
  7357. tp->msi_cap + PCI_MSI_FLAGS,
  7358. ctrl | PCI_MSI_FLAGS_ENABLE);
  7359. val = tr32(MSGINT_MODE);
  7360. tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
  7361. }
  7362. }
  7363. }
  7364. static void tg3_override_clk(struct tg3 *tp)
  7365. {
  7366. u32 val;
  7367. switch (tg3_asic_rev(tp)) {
  7368. case ASIC_REV_5717:
  7369. val = tr32(TG3_CPMU_CLCK_ORIDE_ENABLE);
  7370. tw32(TG3_CPMU_CLCK_ORIDE_ENABLE, val |
  7371. TG3_CPMU_MAC_ORIDE_ENABLE);
  7372. break;
  7373. case ASIC_REV_5719:
  7374. case ASIC_REV_5720:
  7375. tw32(TG3_CPMU_CLCK_ORIDE, CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
  7376. break;
  7377. default:
  7378. return;
  7379. }
  7380. }
  7381. static void tg3_restore_clk(struct tg3 *tp)
  7382. {
  7383. u32 val;
  7384. switch (tg3_asic_rev(tp)) {
  7385. case ASIC_REV_5717:
  7386. val = tr32(TG3_CPMU_CLCK_ORIDE_ENABLE);
  7387. tw32(TG3_CPMU_CLCK_ORIDE_ENABLE,
  7388. val & ~TG3_CPMU_MAC_ORIDE_ENABLE);
  7389. break;
  7390. case ASIC_REV_5719:
  7391. case ASIC_REV_5720:
  7392. val = tr32(TG3_CPMU_CLCK_ORIDE);
  7393. tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
  7394. break;
  7395. default:
  7396. return;
  7397. }
  7398. }
  7399. /* tp->lock is held. */
  7400. static int tg3_chip_reset(struct tg3 *tp)
  7401. __releases(tp->lock)
  7402. __acquires(tp->lock)
  7403. {
  7404. u32 val;
  7405. void (*write_op)(struct tg3 *, u32, u32);
  7406. int i, err;
  7407. if (!pci_device_is_present(tp->pdev))
  7408. return -ENODEV;
  7409. tg3_nvram_lock(tp);
  7410. tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
  7411. /* No matching tg3_nvram_unlock() after this because
  7412. * chip reset below will undo the nvram lock.
  7413. */
  7414. tp->nvram_lock_cnt = 0;
  7415. /* GRC_MISC_CFG core clock reset will clear the memory
  7416. * enable bit in PCI register 4 and the MSI enable bit
  7417. * on some chips, so we save relevant registers here.
  7418. */
  7419. tg3_save_pci_state(tp);
  7420. if (tg3_asic_rev(tp) == ASIC_REV_5752 ||
  7421. tg3_flag(tp, 5755_PLUS))
  7422. tw32(GRC_FASTBOOT_PC, 0);
  7423. /*
  7424. * We must avoid the readl() that normally takes place.
  7425. * It locks machines, causes machine checks, and other
  7426. * fun things. So, temporarily disable the 5701
  7427. * hardware workaround, while we do the reset.
  7428. */
  7429. write_op = tp->write32;
  7430. if (write_op == tg3_write_flush_reg32)
  7431. tp->write32 = tg3_write32;
  7432. /* Prevent the irq handler from reading or writing PCI registers
  7433. * during chip reset when the memory enable bit in the PCI command
  7434. * register may be cleared. The chip does not generate interrupt
  7435. * at this time, but the irq handler may still be called due to irq
  7436. * sharing or irqpoll.
  7437. */
  7438. tg3_flag_set(tp, CHIP_RESETTING);
  7439. for (i = 0; i < tp->irq_cnt; i++) {
  7440. struct tg3_napi *tnapi = &tp->napi[i];
  7441. if (tnapi->hw_status) {
  7442. tnapi->hw_status->status = 0;
  7443. tnapi->hw_status->status_tag = 0;
  7444. }
  7445. tnapi->last_tag = 0;
  7446. tnapi->last_irq_tag = 0;
  7447. }
  7448. smp_mb();
  7449. tg3_full_unlock(tp);
  7450. for (i = 0; i < tp->irq_cnt; i++)
  7451. synchronize_irq(tp->napi[i].irq_vec);
  7452. tg3_full_lock(tp, 0);
  7453. if (tg3_asic_rev(tp) == ASIC_REV_57780) {
  7454. val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
  7455. tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
  7456. }
  7457. /* do the reset */
  7458. val = GRC_MISC_CFG_CORECLK_RESET;
  7459. if (tg3_flag(tp, PCI_EXPRESS)) {
  7460. /* Force PCIe 1.0a mode */
  7461. if (tg3_asic_rev(tp) != ASIC_REV_5785 &&
  7462. !tg3_flag(tp, 57765_PLUS) &&
  7463. tr32(TG3_PCIE_PHY_TSTCTL) ==
  7464. (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
  7465. tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
  7466. if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0) {
  7467. tw32(GRC_MISC_CFG, (1 << 29));
  7468. val |= (1 << 29);
  7469. }
  7470. }
  7471. if (tg3_asic_rev(tp) == ASIC_REV_5906) {
  7472. tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
  7473. tw32(GRC_VCPU_EXT_CTRL,
  7474. tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
  7475. }
  7476. /* Set the clock to the highest frequency to avoid timeouts. With link
  7477. * aware mode, the clock speed could be slow and bootcode does not
  7478. * complete within the expected time. Override the clock to allow the
  7479. * bootcode to finish sooner and then restore it.
  7480. */
  7481. tg3_override_clk(tp);
  7482. /* Manage gphy power for all CPMU absent PCIe devices. */
  7483. if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, CPMU_PRESENT))
  7484. val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
  7485. tw32(GRC_MISC_CFG, val);
  7486. /* restore 5701 hardware bug workaround write method */
  7487. tp->write32 = write_op;
  7488. /* Unfortunately, we have to delay before the PCI read back.
  7489. * Some 575X chips even will not respond to a PCI cfg access
  7490. * when the reset command is given to the chip.
  7491. *
  7492. * How do these hardware designers expect things to work
  7493. * properly if the PCI write is posted for a long period
  7494. * of time? It is always necessary to have some method by
  7495. * which a register read back can occur to push the write
  7496. * out which does the reset.
  7497. *
  7498. * For most tg3 variants the trick below was working.
  7499. * Ho hum...
  7500. */
  7501. udelay(120);
  7502. /* Flush PCI posted writes. The normal MMIO registers
  7503. * are inaccessible at this time so this is the only
  7504. * way to make this reliably (actually, this is no longer
  7505. * the case, see above). I tried to use indirect
  7506. * register read/write but this upset some 5701 variants.
  7507. */
  7508. pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
  7509. udelay(120);
  7510. if (tg3_flag(tp, PCI_EXPRESS) && pci_is_pcie(tp->pdev)) {
  7511. u16 val16;
  7512. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A0) {
  7513. int j;
  7514. u32 cfg_val;
  7515. /* Wait for link training to complete. */
  7516. for (j = 0; j < 5000; j++)
  7517. udelay(100);
  7518. pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
  7519. pci_write_config_dword(tp->pdev, 0xc4,
  7520. cfg_val | (1 << 15));
  7521. }
  7522. /* Clear the "no snoop" and "relaxed ordering" bits. */
  7523. val16 = PCI_EXP_DEVCTL_RELAX_EN | PCI_EXP_DEVCTL_NOSNOOP_EN;
  7524. /*
  7525. * Older PCIe devices only support the 128 byte
  7526. * MPS setting. Enforce the restriction.
  7527. */
  7528. if (!tg3_flag(tp, CPMU_PRESENT))
  7529. val16 |= PCI_EXP_DEVCTL_PAYLOAD;
  7530. pcie_capability_clear_word(tp->pdev, PCI_EXP_DEVCTL, val16);
  7531. /* Clear error status */
  7532. pcie_capability_write_word(tp->pdev, PCI_EXP_DEVSTA,
  7533. PCI_EXP_DEVSTA_CED |
  7534. PCI_EXP_DEVSTA_NFED |
  7535. PCI_EXP_DEVSTA_FED |
  7536. PCI_EXP_DEVSTA_URD);
  7537. }
  7538. tg3_restore_pci_state(tp);
  7539. tg3_flag_clear(tp, CHIP_RESETTING);
  7540. tg3_flag_clear(tp, ERROR_PROCESSED);
  7541. val = 0;
  7542. if (tg3_flag(tp, 5780_CLASS))
  7543. val = tr32(MEMARB_MODE);
  7544. tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
  7545. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A3) {
  7546. tg3_stop_fw(tp);
  7547. tw32(0x5000, 0x400);
  7548. }
  7549. if (tg3_flag(tp, IS_SSB_CORE)) {
  7550. /*
  7551. * BCM4785: In order to avoid repercussions from using
  7552. * potentially defective internal ROM, stop the Rx RISC CPU,
  7553. * which is not required.
  7554. */
  7555. tg3_stop_fw(tp);
  7556. tg3_halt_cpu(tp, RX_CPU_BASE);
  7557. }
  7558. err = tg3_poll_fw(tp);
  7559. if (err)
  7560. return err;
  7561. tw32(GRC_MODE, tp->grc_mode);
  7562. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A0) {
  7563. val = tr32(0xc4);
  7564. tw32(0xc4, val | (1 << 15));
  7565. }
  7566. if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
  7567. tg3_asic_rev(tp) == ASIC_REV_5705) {
  7568. tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
  7569. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A0)
  7570. tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
  7571. tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
  7572. }
  7573. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
  7574. tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
  7575. val = tp->mac_mode;
  7576. } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
  7577. tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
  7578. val = tp->mac_mode;
  7579. } else
  7580. val = 0;
  7581. tw32_f(MAC_MODE, val);
  7582. udelay(40);
  7583. tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
  7584. tg3_mdio_start(tp);
  7585. if (tg3_flag(tp, PCI_EXPRESS) &&
  7586. tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0 &&
  7587. tg3_asic_rev(tp) != ASIC_REV_5785 &&
  7588. !tg3_flag(tp, 57765_PLUS)) {
  7589. val = tr32(0x7c00);
  7590. tw32(0x7c00, val | (1 << 25));
  7591. }
  7592. tg3_restore_clk(tp);
  7593. /* Reprobe ASF enable state. */
  7594. tg3_flag_clear(tp, ENABLE_ASF);
  7595. tp->phy_flags &= ~(TG3_PHYFLG_1G_ON_VAUX_OK |
  7596. TG3_PHYFLG_KEEP_LINK_ON_PWRDN);
  7597. tg3_flag_clear(tp, ASF_NEW_HANDSHAKE);
  7598. tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
  7599. if (val == NIC_SRAM_DATA_SIG_MAGIC) {
  7600. u32 nic_cfg;
  7601. tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
  7602. if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
  7603. tg3_flag_set(tp, ENABLE_ASF);
  7604. tp->last_event_jiffies = jiffies;
  7605. if (tg3_flag(tp, 5750_PLUS))
  7606. tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
  7607. tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &nic_cfg);
  7608. if (nic_cfg & NIC_SRAM_1G_ON_VAUX_OK)
  7609. tp->phy_flags |= TG3_PHYFLG_1G_ON_VAUX_OK;
  7610. if (nic_cfg & NIC_SRAM_LNK_FLAP_AVOID)
  7611. tp->phy_flags |= TG3_PHYFLG_KEEP_LINK_ON_PWRDN;
  7612. }
  7613. }
  7614. return 0;
  7615. }
  7616. static void tg3_get_nstats(struct tg3 *, struct rtnl_link_stats64 *);
  7617. static void tg3_get_estats(struct tg3 *, struct tg3_ethtool_stats *);
  7618. static void __tg3_set_rx_mode(struct net_device *);
  7619. /* tp->lock is held. */
  7620. static int tg3_halt(struct tg3 *tp, int kind, bool silent)
  7621. {
  7622. int err;
  7623. tg3_stop_fw(tp);
  7624. tg3_write_sig_pre_reset(tp, kind);
  7625. tg3_abort_hw(tp, silent);
  7626. err = tg3_chip_reset(tp);
  7627. __tg3_set_mac_addr(tp, false);
  7628. tg3_write_sig_legacy(tp, kind);
  7629. tg3_write_sig_post_reset(tp, kind);
  7630. if (tp->hw_stats) {
  7631. /* Save the stats across chip resets... */
  7632. tg3_get_nstats(tp, &tp->net_stats_prev);
  7633. tg3_get_estats(tp, &tp->estats_prev);
  7634. /* And make sure the next sample is new data */
  7635. memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
  7636. }
  7637. return err;
  7638. }
  7639. static int tg3_set_mac_addr(struct net_device *dev, void *p)
  7640. {
  7641. struct tg3 *tp = netdev_priv(dev);
  7642. struct sockaddr *addr = p;
  7643. int err = 0;
  7644. bool skip_mac_1 = false;
  7645. if (!is_valid_ether_addr(addr->sa_data))
  7646. return -EADDRNOTAVAIL;
  7647. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  7648. if (!netif_running(dev))
  7649. return 0;
  7650. if (tg3_flag(tp, ENABLE_ASF)) {
  7651. u32 addr0_high, addr0_low, addr1_high, addr1_low;
  7652. addr0_high = tr32(MAC_ADDR_0_HIGH);
  7653. addr0_low = tr32(MAC_ADDR_0_LOW);
  7654. addr1_high = tr32(MAC_ADDR_1_HIGH);
  7655. addr1_low = tr32(MAC_ADDR_1_LOW);
  7656. /* Skip MAC addr 1 if ASF is using it. */
  7657. if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
  7658. !(addr1_high == 0 && addr1_low == 0))
  7659. skip_mac_1 = true;
  7660. }
  7661. spin_lock_bh(&tp->lock);
  7662. __tg3_set_mac_addr(tp, skip_mac_1);
  7663. __tg3_set_rx_mode(dev);
  7664. spin_unlock_bh(&tp->lock);
  7665. return err;
  7666. }
  7667. /* tp->lock is held. */
  7668. static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
  7669. dma_addr_t mapping, u32 maxlen_flags,
  7670. u32 nic_addr)
  7671. {
  7672. tg3_write_mem(tp,
  7673. (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
  7674. ((u64) mapping >> 32));
  7675. tg3_write_mem(tp,
  7676. (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
  7677. ((u64) mapping & 0xffffffff));
  7678. tg3_write_mem(tp,
  7679. (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
  7680. maxlen_flags);
  7681. if (!tg3_flag(tp, 5705_PLUS))
  7682. tg3_write_mem(tp,
  7683. (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
  7684. nic_addr);
  7685. }
  7686. static void tg3_coal_tx_init(struct tg3 *tp, struct ethtool_coalesce *ec)
  7687. {
  7688. int i = 0;
  7689. if (!tg3_flag(tp, ENABLE_TSS)) {
  7690. tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
  7691. tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
  7692. tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
  7693. } else {
  7694. tw32(HOSTCC_TXCOL_TICKS, 0);
  7695. tw32(HOSTCC_TXMAX_FRAMES, 0);
  7696. tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
  7697. for (; i < tp->txq_cnt; i++) {
  7698. u32 reg;
  7699. reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
  7700. tw32(reg, ec->tx_coalesce_usecs);
  7701. reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
  7702. tw32(reg, ec->tx_max_coalesced_frames);
  7703. reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
  7704. tw32(reg, ec->tx_max_coalesced_frames_irq);
  7705. }
  7706. }
  7707. for (; i < tp->irq_max - 1; i++) {
  7708. tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
  7709. tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
  7710. tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
  7711. }
  7712. }
  7713. static void tg3_coal_rx_init(struct tg3 *tp, struct ethtool_coalesce *ec)
  7714. {
  7715. int i = 0;
  7716. u32 limit = tp->rxq_cnt;
  7717. if (!tg3_flag(tp, ENABLE_RSS)) {
  7718. tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
  7719. tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
  7720. tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
  7721. limit--;
  7722. } else {
  7723. tw32(HOSTCC_RXCOL_TICKS, 0);
  7724. tw32(HOSTCC_RXMAX_FRAMES, 0);
  7725. tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
  7726. }
  7727. for (; i < limit; i++) {
  7728. u32 reg;
  7729. reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
  7730. tw32(reg, ec->rx_coalesce_usecs);
  7731. reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
  7732. tw32(reg, ec->rx_max_coalesced_frames);
  7733. reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
  7734. tw32(reg, ec->rx_max_coalesced_frames_irq);
  7735. }
  7736. for (; i < tp->irq_max - 1; i++) {
  7737. tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
  7738. tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
  7739. tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
  7740. }
  7741. }
  7742. static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
  7743. {
  7744. tg3_coal_tx_init(tp, ec);
  7745. tg3_coal_rx_init(tp, ec);
  7746. if (!tg3_flag(tp, 5705_PLUS)) {
  7747. u32 val = ec->stats_block_coalesce_usecs;
  7748. tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
  7749. tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
  7750. if (!tp->link_up)
  7751. val = 0;
  7752. tw32(HOSTCC_STAT_COAL_TICKS, val);
  7753. }
  7754. }
  7755. /* tp->lock is held. */
  7756. static void tg3_tx_rcbs_disable(struct tg3 *tp)
  7757. {
  7758. u32 txrcb, limit;
  7759. /* Disable all transmit rings but the first. */
  7760. if (!tg3_flag(tp, 5705_PLUS))
  7761. limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
  7762. else if (tg3_flag(tp, 5717_PLUS))
  7763. limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
  7764. else if (tg3_flag(tp, 57765_CLASS) ||
  7765. tg3_asic_rev(tp) == ASIC_REV_5762)
  7766. limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
  7767. else
  7768. limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
  7769. for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
  7770. txrcb < limit; txrcb += TG3_BDINFO_SIZE)
  7771. tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
  7772. BDINFO_FLAGS_DISABLED);
  7773. }
  7774. /* tp->lock is held. */
  7775. static void tg3_tx_rcbs_init(struct tg3 *tp)
  7776. {
  7777. int i = 0;
  7778. u32 txrcb = NIC_SRAM_SEND_RCB;
  7779. if (tg3_flag(tp, ENABLE_TSS))
  7780. i++;
  7781. for (; i < tp->irq_max; i++, txrcb += TG3_BDINFO_SIZE) {
  7782. struct tg3_napi *tnapi = &tp->napi[i];
  7783. if (!tnapi->tx_ring)
  7784. continue;
  7785. tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
  7786. (TG3_TX_RING_SIZE << BDINFO_FLAGS_MAXLEN_SHIFT),
  7787. NIC_SRAM_TX_BUFFER_DESC);
  7788. }
  7789. }
  7790. /* tp->lock is held. */
  7791. static void tg3_rx_ret_rcbs_disable(struct tg3 *tp)
  7792. {
  7793. u32 rxrcb, limit;
  7794. /* Disable all receive return rings but the first. */
  7795. if (tg3_flag(tp, 5717_PLUS))
  7796. limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
  7797. else if (!tg3_flag(tp, 5705_PLUS))
  7798. limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
  7799. else if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
  7800. tg3_asic_rev(tp) == ASIC_REV_5762 ||
  7801. tg3_flag(tp, 57765_CLASS))
  7802. limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
  7803. else
  7804. limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
  7805. for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
  7806. rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
  7807. tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
  7808. BDINFO_FLAGS_DISABLED);
  7809. }
  7810. /* tp->lock is held. */
  7811. static void tg3_rx_ret_rcbs_init(struct tg3 *tp)
  7812. {
  7813. int i = 0;
  7814. u32 rxrcb = NIC_SRAM_RCV_RET_RCB;
  7815. if (tg3_flag(tp, ENABLE_RSS))
  7816. i++;
  7817. for (; i < tp->irq_max; i++, rxrcb += TG3_BDINFO_SIZE) {
  7818. struct tg3_napi *tnapi = &tp->napi[i];
  7819. if (!tnapi->rx_rcb)
  7820. continue;
  7821. tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
  7822. (tp->rx_ret_ring_mask + 1) <<
  7823. BDINFO_FLAGS_MAXLEN_SHIFT, 0);
  7824. }
  7825. }
  7826. /* tp->lock is held. */
  7827. static void tg3_rings_reset(struct tg3 *tp)
  7828. {
  7829. int i;
  7830. u32 stblk;
  7831. struct tg3_napi *tnapi = &tp->napi[0];
  7832. tg3_tx_rcbs_disable(tp);
  7833. tg3_rx_ret_rcbs_disable(tp);
  7834. /* Disable interrupts */
  7835. tw32_mailbox_f(tp->napi[0].int_mbox, 1);
  7836. tp->napi[0].chk_msi_cnt = 0;
  7837. tp->napi[0].last_rx_cons = 0;
  7838. tp->napi[0].last_tx_cons = 0;
  7839. /* Zero mailbox registers. */
  7840. if (tg3_flag(tp, SUPPORT_MSIX)) {
  7841. for (i = 1; i < tp->irq_max; i++) {
  7842. tp->napi[i].tx_prod = 0;
  7843. tp->napi[i].tx_cons = 0;
  7844. if (tg3_flag(tp, ENABLE_TSS))
  7845. tw32_mailbox(tp->napi[i].prodmbox, 0);
  7846. tw32_rx_mbox(tp->napi[i].consmbox, 0);
  7847. tw32_mailbox_f(tp->napi[i].int_mbox, 1);
  7848. tp->napi[i].chk_msi_cnt = 0;
  7849. tp->napi[i].last_rx_cons = 0;
  7850. tp->napi[i].last_tx_cons = 0;
  7851. }
  7852. if (!tg3_flag(tp, ENABLE_TSS))
  7853. tw32_mailbox(tp->napi[0].prodmbox, 0);
  7854. } else {
  7855. tp->napi[0].tx_prod = 0;
  7856. tp->napi[0].tx_cons = 0;
  7857. tw32_mailbox(tp->napi[0].prodmbox, 0);
  7858. tw32_rx_mbox(tp->napi[0].consmbox, 0);
  7859. }
  7860. /* Make sure the NIC-based send BD rings are disabled. */
  7861. if (!tg3_flag(tp, 5705_PLUS)) {
  7862. u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
  7863. for (i = 0; i < 16; i++)
  7864. tw32_tx_mbox(mbox + i * 8, 0);
  7865. }
  7866. /* Clear status block in ram. */
  7867. memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
  7868. /* Set status block DMA address */
  7869. tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
  7870. ((u64) tnapi->status_mapping >> 32));
  7871. tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
  7872. ((u64) tnapi->status_mapping & 0xffffffff));
  7873. stblk = HOSTCC_STATBLCK_RING1;
  7874. for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
  7875. u64 mapping = (u64)tnapi->status_mapping;
  7876. tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
  7877. tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
  7878. stblk += 8;
  7879. /* Clear status block in ram. */
  7880. memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
  7881. }
  7882. tg3_tx_rcbs_init(tp);
  7883. tg3_rx_ret_rcbs_init(tp);
  7884. }
  7885. static void tg3_setup_rxbd_thresholds(struct tg3 *tp)
  7886. {
  7887. u32 val, bdcache_maxcnt, host_rep_thresh, nic_rep_thresh;
  7888. if (!tg3_flag(tp, 5750_PLUS) ||
  7889. tg3_flag(tp, 5780_CLASS) ||
  7890. tg3_asic_rev(tp) == ASIC_REV_5750 ||
  7891. tg3_asic_rev(tp) == ASIC_REV_5752 ||
  7892. tg3_flag(tp, 57765_PLUS))
  7893. bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5700;
  7894. else if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
  7895. tg3_asic_rev(tp) == ASIC_REV_5787)
  7896. bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5755;
  7897. else
  7898. bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5906;
  7899. nic_rep_thresh = min(bdcache_maxcnt / 2, tp->rx_std_max_post);
  7900. host_rep_thresh = max_t(u32, tp->rx_pending / 8, 1);
  7901. val = min(nic_rep_thresh, host_rep_thresh);
  7902. tw32(RCVBDI_STD_THRESH, val);
  7903. if (tg3_flag(tp, 57765_PLUS))
  7904. tw32(STD_REPLENISH_LWM, bdcache_maxcnt);
  7905. if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
  7906. return;
  7907. bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5700;
  7908. host_rep_thresh = max_t(u32, tp->rx_jumbo_pending / 8, 1);
  7909. val = min(bdcache_maxcnt / 2, host_rep_thresh);
  7910. tw32(RCVBDI_JUMBO_THRESH, val);
  7911. if (tg3_flag(tp, 57765_PLUS))
  7912. tw32(JMB_REPLENISH_LWM, bdcache_maxcnt);
  7913. }
  7914. static inline u32 calc_crc(unsigned char *buf, int len)
  7915. {
  7916. u32 reg;
  7917. u32 tmp;
  7918. int j, k;
  7919. reg = 0xffffffff;
  7920. for (j = 0; j < len; j++) {
  7921. reg ^= buf[j];
  7922. for (k = 0; k < 8; k++) {
  7923. tmp = reg & 0x01;
  7924. reg >>= 1;
  7925. if (tmp)
  7926. reg ^= 0xedb88320;
  7927. }
  7928. }
  7929. return ~reg;
  7930. }
  7931. static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
  7932. {
  7933. /* accept or reject all multicast frames */
  7934. tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
  7935. tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
  7936. tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
  7937. tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
  7938. }
  7939. static void __tg3_set_rx_mode(struct net_device *dev)
  7940. {
  7941. struct tg3 *tp = netdev_priv(dev);
  7942. u32 rx_mode;
  7943. rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
  7944. RX_MODE_KEEP_VLAN_TAG);
  7945. #if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
  7946. /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
  7947. * flag clear.
  7948. */
  7949. if (!tg3_flag(tp, ENABLE_ASF))
  7950. rx_mode |= RX_MODE_KEEP_VLAN_TAG;
  7951. #endif
  7952. if (dev->flags & IFF_PROMISC) {
  7953. /* Promiscuous mode. */
  7954. rx_mode |= RX_MODE_PROMISC;
  7955. } else if (dev->flags & IFF_ALLMULTI) {
  7956. /* Accept all multicast. */
  7957. tg3_set_multi(tp, 1);
  7958. } else if (netdev_mc_empty(dev)) {
  7959. /* Reject all multicast. */
  7960. tg3_set_multi(tp, 0);
  7961. } else {
  7962. /* Accept one or more multicast(s). */
  7963. struct netdev_hw_addr *ha;
  7964. u32 mc_filter[4] = { 0, };
  7965. u32 regidx;
  7966. u32 bit;
  7967. u32 crc;
  7968. netdev_for_each_mc_addr(ha, dev) {
  7969. crc = calc_crc(ha->addr, ETH_ALEN);
  7970. bit = ~crc & 0x7f;
  7971. regidx = (bit & 0x60) >> 5;
  7972. bit &= 0x1f;
  7973. mc_filter[regidx] |= (1 << bit);
  7974. }
  7975. tw32(MAC_HASH_REG_0, mc_filter[0]);
  7976. tw32(MAC_HASH_REG_1, mc_filter[1]);
  7977. tw32(MAC_HASH_REG_2, mc_filter[2]);
  7978. tw32(MAC_HASH_REG_3, mc_filter[3]);
  7979. }
  7980. if (netdev_uc_count(dev) > TG3_MAX_UCAST_ADDR(tp)) {
  7981. rx_mode |= RX_MODE_PROMISC;
  7982. } else if (!(dev->flags & IFF_PROMISC)) {
  7983. /* Add all entries into to the mac addr filter list */
  7984. int i = 0;
  7985. struct netdev_hw_addr *ha;
  7986. netdev_for_each_uc_addr(ha, dev) {
  7987. __tg3_set_one_mac_addr(tp, ha->addr,
  7988. i + TG3_UCAST_ADDR_IDX(tp));
  7989. i++;
  7990. }
  7991. }
  7992. if (rx_mode != tp->rx_mode) {
  7993. tp->rx_mode = rx_mode;
  7994. tw32_f(MAC_RX_MODE, rx_mode);
  7995. udelay(10);
  7996. }
  7997. }
  7998. static void tg3_rss_init_dflt_indir_tbl(struct tg3 *tp, u32 qcnt)
  7999. {
  8000. int i;
  8001. for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
  8002. tp->rss_ind_tbl[i] = ethtool_rxfh_indir_default(i, qcnt);
  8003. }
  8004. static void tg3_rss_check_indir_tbl(struct tg3 *tp)
  8005. {
  8006. int i;
  8007. if (!tg3_flag(tp, SUPPORT_MSIX))
  8008. return;
  8009. if (tp->rxq_cnt == 1) {
  8010. memset(&tp->rss_ind_tbl[0], 0, sizeof(tp->rss_ind_tbl));
  8011. return;
  8012. }
  8013. /* Validate table against current IRQ count */
  8014. for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
  8015. if (tp->rss_ind_tbl[i] >= tp->rxq_cnt)
  8016. break;
  8017. }
  8018. if (i != TG3_RSS_INDIR_TBL_SIZE)
  8019. tg3_rss_init_dflt_indir_tbl(tp, tp->rxq_cnt);
  8020. }
  8021. static void tg3_rss_write_indir_tbl(struct tg3 *tp)
  8022. {
  8023. int i = 0;
  8024. u32 reg = MAC_RSS_INDIR_TBL_0;
  8025. while (i < TG3_RSS_INDIR_TBL_SIZE) {
  8026. u32 val = tp->rss_ind_tbl[i];
  8027. i++;
  8028. for (; i % 8; i++) {
  8029. val <<= 4;
  8030. val |= tp->rss_ind_tbl[i];
  8031. }
  8032. tw32(reg, val);
  8033. reg += 4;
  8034. }
  8035. }
  8036. static inline u32 tg3_lso_rd_dma_workaround_bit(struct tg3 *tp)
  8037. {
  8038. if (tg3_asic_rev(tp) == ASIC_REV_5719)
  8039. return TG3_LSO_RD_DMA_TX_LENGTH_WA_5719;
  8040. else
  8041. return TG3_LSO_RD_DMA_TX_LENGTH_WA_5720;
  8042. }
  8043. /* tp->lock is held. */
  8044. static int tg3_reset_hw(struct tg3 *tp, bool reset_phy)
  8045. {
  8046. u32 val, rdmac_mode;
  8047. int i, err, limit;
  8048. struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
  8049. tg3_disable_ints(tp);
  8050. tg3_stop_fw(tp);
  8051. tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
  8052. if (tg3_flag(tp, INIT_COMPLETE))
  8053. tg3_abort_hw(tp, 1);
  8054. if ((tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN) &&
  8055. !(tp->phy_flags & TG3_PHYFLG_USER_CONFIGURED)) {
  8056. tg3_phy_pull_config(tp);
  8057. tg3_eee_pull_config(tp, NULL);
  8058. tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
  8059. }
  8060. /* Enable MAC control of LPI */
  8061. if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
  8062. tg3_setup_eee(tp);
  8063. if (reset_phy)
  8064. tg3_phy_reset(tp);
  8065. err = tg3_chip_reset(tp);
  8066. if (err)
  8067. return err;
  8068. tg3_write_sig_legacy(tp, RESET_KIND_INIT);
  8069. if (tg3_chip_rev(tp) == CHIPREV_5784_AX) {
  8070. val = tr32(TG3_CPMU_CTRL);
  8071. val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
  8072. tw32(TG3_CPMU_CTRL, val);
  8073. val = tr32(TG3_CPMU_LSPD_10MB_CLK);
  8074. val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
  8075. val |= CPMU_LSPD_10MB_MACCLK_6_25;
  8076. tw32(TG3_CPMU_LSPD_10MB_CLK, val);
  8077. val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
  8078. val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
  8079. val |= CPMU_LNK_AWARE_MACCLK_6_25;
  8080. tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
  8081. val = tr32(TG3_CPMU_HST_ACC);
  8082. val &= ~CPMU_HST_ACC_MACCLK_MASK;
  8083. val |= CPMU_HST_ACC_MACCLK_6_25;
  8084. tw32(TG3_CPMU_HST_ACC, val);
  8085. }
  8086. if (tg3_asic_rev(tp) == ASIC_REV_57780) {
  8087. val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
  8088. val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
  8089. PCIE_PWR_MGMT_L1_THRESH_4MS;
  8090. tw32(PCIE_PWR_MGMT_THRESH, val);
  8091. val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
  8092. tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
  8093. tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
  8094. val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
  8095. tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
  8096. }
  8097. if (tg3_flag(tp, L1PLLPD_EN)) {
  8098. u32 grc_mode = tr32(GRC_MODE);
  8099. /* Access the lower 1K of PL PCIE block registers. */
  8100. val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
  8101. tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
  8102. val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
  8103. tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
  8104. val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
  8105. tw32(GRC_MODE, grc_mode);
  8106. }
  8107. if (tg3_flag(tp, 57765_CLASS)) {
  8108. if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0) {
  8109. u32 grc_mode = tr32(GRC_MODE);
  8110. /* Access the lower 1K of PL PCIE block registers. */
  8111. val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
  8112. tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
  8113. val = tr32(TG3_PCIE_TLDLPL_PORT +
  8114. TG3_PCIE_PL_LO_PHYCTL5);
  8115. tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
  8116. val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
  8117. tw32(GRC_MODE, grc_mode);
  8118. }
  8119. if (tg3_chip_rev(tp) != CHIPREV_57765_AX) {
  8120. u32 grc_mode;
  8121. /* Fix transmit hangs */
  8122. val = tr32(TG3_CPMU_PADRNG_CTL);
  8123. val |= TG3_CPMU_PADRNG_CTL_RDIV2;
  8124. tw32(TG3_CPMU_PADRNG_CTL, val);
  8125. grc_mode = tr32(GRC_MODE);
  8126. /* Access the lower 1K of DL PCIE block registers. */
  8127. val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
  8128. tw32(GRC_MODE, val | GRC_MODE_PCIE_DL_SEL);
  8129. val = tr32(TG3_PCIE_TLDLPL_PORT +
  8130. TG3_PCIE_DL_LO_FTSMAX);
  8131. val &= ~TG3_PCIE_DL_LO_FTSMAX_MSK;
  8132. tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_DL_LO_FTSMAX,
  8133. val | TG3_PCIE_DL_LO_FTSMAX_VAL);
  8134. tw32(GRC_MODE, grc_mode);
  8135. }
  8136. val = tr32(TG3_CPMU_LSPD_10MB_CLK);
  8137. val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
  8138. val |= CPMU_LSPD_10MB_MACCLK_6_25;
  8139. tw32(TG3_CPMU_LSPD_10MB_CLK, val);
  8140. }
  8141. /* This works around an issue with Athlon chipsets on
  8142. * B3 tigon3 silicon. This bit has no effect on any
  8143. * other revision. But do not set this on PCI Express
  8144. * chips and don't even touch the clocks if the CPMU is present.
  8145. */
  8146. if (!tg3_flag(tp, CPMU_PRESENT)) {
  8147. if (!tg3_flag(tp, PCI_EXPRESS))
  8148. tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
  8149. tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
  8150. }
  8151. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0 &&
  8152. tg3_flag(tp, PCIX_MODE)) {
  8153. val = tr32(TG3PCI_PCISTATE);
  8154. val |= PCISTATE_RETRY_SAME_DMA;
  8155. tw32(TG3PCI_PCISTATE, val);
  8156. }
  8157. if (tg3_flag(tp, ENABLE_APE)) {
  8158. /* Allow reads and writes to the
  8159. * APE register and memory space.
  8160. */
  8161. val = tr32(TG3PCI_PCISTATE);
  8162. val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
  8163. PCISTATE_ALLOW_APE_SHMEM_WR |
  8164. PCISTATE_ALLOW_APE_PSPACE_WR;
  8165. tw32(TG3PCI_PCISTATE, val);
  8166. }
  8167. if (tg3_chip_rev(tp) == CHIPREV_5704_BX) {
  8168. /* Enable some hw fixes. */
  8169. val = tr32(TG3PCI_MSI_DATA);
  8170. val |= (1 << 26) | (1 << 28) | (1 << 29);
  8171. tw32(TG3PCI_MSI_DATA, val);
  8172. }
  8173. /* Descriptor ring init may make accesses to the
  8174. * NIC SRAM area to setup the TX descriptors, so we
  8175. * can only do this after the hardware has been
  8176. * successfully reset.
  8177. */
  8178. err = tg3_init_rings(tp);
  8179. if (err)
  8180. return err;
  8181. if (tg3_flag(tp, 57765_PLUS)) {
  8182. val = tr32(TG3PCI_DMA_RW_CTRL) &
  8183. ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
  8184. if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0)
  8185. val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
  8186. if (!tg3_flag(tp, 57765_CLASS) &&
  8187. tg3_asic_rev(tp) != ASIC_REV_5717 &&
  8188. tg3_asic_rev(tp) != ASIC_REV_5762)
  8189. val |= DMA_RWCTRL_TAGGED_STAT_WA;
  8190. tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
  8191. } else if (tg3_asic_rev(tp) != ASIC_REV_5784 &&
  8192. tg3_asic_rev(tp) != ASIC_REV_5761) {
  8193. /* This value is determined during the probe time DMA
  8194. * engine test, tg3_test_dma.
  8195. */
  8196. tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
  8197. }
  8198. tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
  8199. GRC_MODE_4X_NIC_SEND_RINGS |
  8200. GRC_MODE_NO_TX_PHDR_CSUM |
  8201. GRC_MODE_NO_RX_PHDR_CSUM);
  8202. tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
  8203. /* Pseudo-header checksum is done by hardware logic and not
  8204. * the offload processers, so make the chip do the pseudo-
  8205. * header checksums on receive. For transmit it is more
  8206. * convenient to do the pseudo-header checksum in software
  8207. * as Linux does that on transmit for us in all cases.
  8208. */
  8209. tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
  8210. val = GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP;
  8211. if (tp->rxptpctl)
  8212. tw32(TG3_RX_PTP_CTL,
  8213. tp->rxptpctl | TG3_RX_PTP_CTL_HWTS_INTERLOCK);
  8214. if (tg3_flag(tp, PTP_CAPABLE))
  8215. val |= GRC_MODE_TIME_SYNC_ENABLE;
  8216. tw32(GRC_MODE, tp->grc_mode | val);
  8217. /* Setup the timer prescalar register. Clock is always 66Mhz. */
  8218. val = tr32(GRC_MISC_CFG);
  8219. val &= ~0xff;
  8220. val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
  8221. tw32(GRC_MISC_CFG, val);
  8222. /* Initialize MBUF/DESC pool. */
  8223. if (tg3_flag(tp, 5750_PLUS)) {
  8224. /* Do nothing. */
  8225. } else if (tg3_asic_rev(tp) != ASIC_REV_5705) {
  8226. tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
  8227. if (tg3_asic_rev(tp) == ASIC_REV_5704)
  8228. tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
  8229. else
  8230. tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
  8231. tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
  8232. tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
  8233. } else if (tg3_flag(tp, TSO_CAPABLE)) {
  8234. int fw_len;
  8235. fw_len = tp->fw_len;
  8236. fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
  8237. tw32(BUFMGR_MB_POOL_ADDR,
  8238. NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
  8239. tw32(BUFMGR_MB_POOL_SIZE,
  8240. NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
  8241. }
  8242. if (tp->dev->mtu <= ETH_DATA_LEN) {
  8243. tw32(BUFMGR_MB_RDMA_LOW_WATER,
  8244. tp->bufmgr_config.mbuf_read_dma_low_water);
  8245. tw32(BUFMGR_MB_MACRX_LOW_WATER,
  8246. tp->bufmgr_config.mbuf_mac_rx_low_water);
  8247. tw32(BUFMGR_MB_HIGH_WATER,
  8248. tp->bufmgr_config.mbuf_high_water);
  8249. } else {
  8250. tw32(BUFMGR_MB_RDMA_LOW_WATER,
  8251. tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
  8252. tw32(BUFMGR_MB_MACRX_LOW_WATER,
  8253. tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
  8254. tw32(BUFMGR_MB_HIGH_WATER,
  8255. tp->bufmgr_config.mbuf_high_water_jumbo);
  8256. }
  8257. tw32(BUFMGR_DMA_LOW_WATER,
  8258. tp->bufmgr_config.dma_low_water);
  8259. tw32(BUFMGR_DMA_HIGH_WATER,
  8260. tp->bufmgr_config.dma_high_water);
  8261. val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
  8262. if (tg3_asic_rev(tp) == ASIC_REV_5719)
  8263. val |= BUFMGR_MODE_NO_TX_UNDERRUN;
  8264. if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
  8265. tg3_asic_rev(tp) == ASIC_REV_5762 ||
  8266. tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
  8267. tg3_chip_rev_id(tp) == CHIPREV_ID_5720_A0)
  8268. val |= BUFMGR_MODE_MBLOW_ATTN_ENAB;
  8269. tw32(BUFMGR_MODE, val);
  8270. for (i = 0; i < 2000; i++) {
  8271. if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
  8272. break;
  8273. udelay(10);
  8274. }
  8275. if (i >= 2000) {
  8276. netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
  8277. return -ENODEV;
  8278. }
  8279. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5906_A1)
  8280. tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
  8281. tg3_setup_rxbd_thresholds(tp);
  8282. /* Initialize TG3_BDINFO's at:
  8283. * RCVDBDI_STD_BD: standard eth size rx ring
  8284. * RCVDBDI_JUMBO_BD: jumbo frame rx ring
  8285. * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
  8286. *
  8287. * like so:
  8288. * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
  8289. * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
  8290. * ring attribute flags
  8291. * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
  8292. *
  8293. * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
  8294. * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
  8295. *
  8296. * The size of each ring is fixed in the firmware, but the location is
  8297. * configurable.
  8298. */
  8299. tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
  8300. ((u64) tpr->rx_std_mapping >> 32));
  8301. tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
  8302. ((u64) tpr->rx_std_mapping & 0xffffffff));
  8303. if (!tg3_flag(tp, 5717_PLUS))
  8304. tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
  8305. NIC_SRAM_RX_BUFFER_DESC);
  8306. /* Disable the mini ring */
  8307. if (!tg3_flag(tp, 5705_PLUS))
  8308. tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
  8309. BDINFO_FLAGS_DISABLED);
  8310. /* Program the jumbo buffer descriptor ring control
  8311. * blocks on those devices that have them.
  8312. */
  8313. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
  8314. (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))) {
  8315. if (tg3_flag(tp, JUMBO_RING_ENABLE)) {
  8316. tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
  8317. ((u64) tpr->rx_jmb_mapping >> 32));
  8318. tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
  8319. ((u64) tpr->rx_jmb_mapping & 0xffffffff));
  8320. val = TG3_RX_JMB_RING_SIZE(tp) <<
  8321. BDINFO_FLAGS_MAXLEN_SHIFT;
  8322. tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
  8323. val | BDINFO_FLAGS_USE_EXT_RECV);
  8324. if (!tg3_flag(tp, USE_JUMBO_BDFLAG) ||
  8325. tg3_flag(tp, 57765_CLASS) ||
  8326. tg3_asic_rev(tp) == ASIC_REV_5762)
  8327. tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
  8328. NIC_SRAM_RX_JUMBO_BUFFER_DESC);
  8329. } else {
  8330. tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
  8331. BDINFO_FLAGS_DISABLED);
  8332. }
  8333. if (tg3_flag(tp, 57765_PLUS)) {
  8334. val = TG3_RX_STD_RING_SIZE(tp);
  8335. val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
  8336. val |= (TG3_RX_STD_DMA_SZ << 2);
  8337. } else
  8338. val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
  8339. } else
  8340. val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
  8341. tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
  8342. tpr->rx_std_prod_idx = tp->rx_pending;
  8343. tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
  8344. tpr->rx_jmb_prod_idx =
  8345. tg3_flag(tp, JUMBO_RING_ENABLE) ? tp->rx_jumbo_pending : 0;
  8346. tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
  8347. tg3_rings_reset(tp);
  8348. /* Initialize MAC address and backoff seed. */
  8349. __tg3_set_mac_addr(tp, false);
  8350. /* MTU + ethernet header + FCS + optional VLAN tag */
  8351. tw32(MAC_RX_MTU_SIZE,
  8352. tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
  8353. /* The slot time is changed by tg3_setup_phy if we
  8354. * run at gigabit with half duplex.
  8355. */
  8356. val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
  8357. (6 << TX_LENGTHS_IPG_SHIFT) |
  8358. (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
  8359. if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
  8360. tg3_asic_rev(tp) == ASIC_REV_5762)
  8361. val |= tr32(MAC_TX_LENGTHS) &
  8362. (TX_LENGTHS_JMB_FRM_LEN_MSK |
  8363. TX_LENGTHS_CNT_DWN_VAL_MSK);
  8364. tw32(MAC_TX_LENGTHS, val);
  8365. /* Receive rules. */
  8366. tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
  8367. tw32(RCVLPC_CONFIG, 0x0181);
  8368. /* Calculate RDMAC_MODE setting early, we need it to determine
  8369. * the RCVLPC_STATE_ENABLE mask.
  8370. */
  8371. rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
  8372. RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
  8373. RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
  8374. RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
  8375. RDMAC_MODE_LNGREAD_ENAB);
  8376. if (tg3_asic_rev(tp) == ASIC_REV_5717)
  8377. rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
  8378. if (tg3_asic_rev(tp) == ASIC_REV_5784 ||
  8379. tg3_asic_rev(tp) == ASIC_REV_5785 ||
  8380. tg3_asic_rev(tp) == ASIC_REV_57780)
  8381. rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
  8382. RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
  8383. RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
  8384. if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
  8385. tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
  8386. if (tg3_flag(tp, TSO_CAPABLE) &&
  8387. tg3_asic_rev(tp) == ASIC_REV_5705) {
  8388. rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
  8389. } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
  8390. !tg3_flag(tp, IS_5788)) {
  8391. rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
  8392. }
  8393. }
  8394. if (tg3_flag(tp, PCI_EXPRESS))
  8395. rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
  8396. if (tg3_asic_rev(tp) == ASIC_REV_57766) {
  8397. tp->dma_limit = 0;
  8398. if (tp->dev->mtu <= ETH_DATA_LEN) {
  8399. rdmac_mode |= RDMAC_MODE_JMB_2K_MMRR;
  8400. tp->dma_limit = TG3_TX_BD_DMA_MAX_2K;
  8401. }
  8402. }
  8403. if (tg3_flag(tp, HW_TSO_1) ||
  8404. tg3_flag(tp, HW_TSO_2) ||
  8405. tg3_flag(tp, HW_TSO_3))
  8406. rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
  8407. if (tg3_flag(tp, 57765_PLUS) ||
  8408. tg3_asic_rev(tp) == ASIC_REV_5785 ||
  8409. tg3_asic_rev(tp) == ASIC_REV_57780)
  8410. rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
  8411. if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
  8412. tg3_asic_rev(tp) == ASIC_REV_5762)
  8413. rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
  8414. if (tg3_asic_rev(tp) == ASIC_REV_5761 ||
  8415. tg3_asic_rev(tp) == ASIC_REV_5784 ||
  8416. tg3_asic_rev(tp) == ASIC_REV_5785 ||
  8417. tg3_asic_rev(tp) == ASIC_REV_57780 ||
  8418. tg3_flag(tp, 57765_PLUS)) {
  8419. u32 tgtreg;
  8420. if (tg3_asic_rev(tp) == ASIC_REV_5762)
  8421. tgtreg = TG3_RDMA_RSRVCTRL_REG2;
  8422. else
  8423. tgtreg = TG3_RDMA_RSRVCTRL_REG;
  8424. val = tr32(tgtreg);
  8425. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
  8426. tg3_asic_rev(tp) == ASIC_REV_5762) {
  8427. val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
  8428. TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
  8429. TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
  8430. val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
  8431. TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
  8432. TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
  8433. }
  8434. tw32(tgtreg, val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
  8435. }
  8436. if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
  8437. tg3_asic_rev(tp) == ASIC_REV_5720 ||
  8438. tg3_asic_rev(tp) == ASIC_REV_5762) {
  8439. u32 tgtreg;
  8440. if (tg3_asic_rev(tp) == ASIC_REV_5762)
  8441. tgtreg = TG3_LSO_RD_DMA_CRPTEN_CTRL2;
  8442. else
  8443. tgtreg = TG3_LSO_RD_DMA_CRPTEN_CTRL;
  8444. val = tr32(tgtreg);
  8445. tw32(tgtreg, val |
  8446. TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
  8447. TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
  8448. }
  8449. /* Receive/send statistics. */
  8450. if (tg3_flag(tp, 5750_PLUS)) {
  8451. val = tr32(RCVLPC_STATS_ENABLE);
  8452. val &= ~RCVLPC_STATSENAB_DACK_FIX;
  8453. tw32(RCVLPC_STATS_ENABLE, val);
  8454. } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
  8455. tg3_flag(tp, TSO_CAPABLE)) {
  8456. val = tr32(RCVLPC_STATS_ENABLE);
  8457. val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
  8458. tw32(RCVLPC_STATS_ENABLE, val);
  8459. } else {
  8460. tw32(RCVLPC_STATS_ENABLE, 0xffffff);
  8461. }
  8462. tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
  8463. tw32(SNDDATAI_STATSENAB, 0xffffff);
  8464. tw32(SNDDATAI_STATSCTRL,
  8465. (SNDDATAI_SCTRL_ENABLE |
  8466. SNDDATAI_SCTRL_FASTUPD));
  8467. /* Setup host coalescing engine. */
  8468. tw32(HOSTCC_MODE, 0);
  8469. for (i = 0; i < 2000; i++) {
  8470. if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
  8471. break;
  8472. udelay(10);
  8473. }
  8474. __tg3_set_coalesce(tp, &tp->coal);
  8475. if (!tg3_flag(tp, 5705_PLUS)) {
  8476. /* Status/statistics block address. See tg3_timer,
  8477. * the tg3_periodic_fetch_stats call there, and
  8478. * tg3_get_stats to see how this works for 5705/5750 chips.
  8479. */
  8480. tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
  8481. ((u64) tp->stats_mapping >> 32));
  8482. tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
  8483. ((u64) tp->stats_mapping & 0xffffffff));
  8484. tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
  8485. tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
  8486. /* Clear statistics and status block memory areas */
  8487. for (i = NIC_SRAM_STATS_BLK;
  8488. i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
  8489. i += sizeof(u32)) {
  8490. tg3_write_mem(tp, i, 0);
  8491. udelay(40);
  8492. }
  8493. }
  8494. tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
  8495. tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
  8496. tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
  8497. if (!tg3_flag(tp, 5705_PLUS))
  8498. tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
  8499. if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
  8500. tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
  8501. /* reset to prevent losing 1st rx packet intermittently */
  8502. tw32_f(MAC_RX_MODE, RX_MODE_RESET);
  8503. udelay(10);
  8504. }
  8505. tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
  8506. MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE |
  8507. MAC_MODE_FHDE_ENABLE;
  8508. if (tg3_flag(tp, ENABLE_APE))
  8509. tp->mac_mode |= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
  8510. if (!tg3_flag(tp, 5705_PLUS) &&
  8511. !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
  8512. tg3_asic_rev(tp) != ASIC_REV_5700)
  8513. tp->mac_mode |= MAC_MODE_LINK_POLARITY;
  8514. tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
  8515. udelay(40);
  8516. /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
  8517. * If TG3_FLAG_IS_NIC is zero, we should read the
  8518. * register to preserve the GPIO settings for LOMs. The GPIOs,
  8519. * whether used as inputs or outputs, are set by boot code after
  8520. * reset.
  8521. */
  8522. if (!tg3_flag(tp, IS_NIC)) {
  8523. u32 gpio_mask;
  8524. gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
  8525. GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
  8526. GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
  8527. if (tg3_asic_rev(tp) == ASIC_REV_5752)
  8528. gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
  8529. GRC_LCLCTRL_GPIO_OUTPUT3;
  8530. if (tg3_asic_rev(tp) == ASIC_REV_5755)
  8531. gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
  8532. tp->grc_local_ctrl &= ~gpio_mask;
  8533. tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
  8534. /* GPIO1 must be driven high for eeprom write protect */
  8535. if (tg3_flag(tp, EEPROM_WRITE_PROT))
  8536. tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
  8537. GRC_LCLCTRL_GPIO_OUTPUT1);
  8538. }
  8539. tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
  8540. udelay(100);
  8541. if (tg3_flag(tp, USING_MSIX)) {
  8542. val = tr32(MSGINT_MODE);
  8543. val |= MSGINT_MODE_ENABLE;
  8544. if (tp->irq_cnt > 1)
  8545. val |= MSGINT_MODE_MULTIVEC_EN;
  8546. if (!tg3_flag(tp, 1SHOT_MSI))
  8547. val |= MSGINT_MODE_ONE_SHOT_DISABLE;
  8548. tw32(MSGINT_MODE, val);
  8549. }
  8550. if (!tg3_flag(tp, 5705_PLUS)) {
  8551. tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
  8552. udelay(40);
  8553. }
  8554. val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
  8555. WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
  8556. WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
  8557. WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
  8558. WDMAC_MODE_LNGREAD_ENAB);
  8559. if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
  8560. tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
  8561. if (tg3_flag(tp, TSO_CAPABLE) &&
  8562. (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A1 ||
  8563. tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A2)) {
  8564. /* nothing */
  8565. } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
  8566. !tg3_flag(tp, IS_5788)) {
  8567. val |= WDMAC_MODE_RX_ACCEL;
  8568. }
  8569. }
  8570. /* Enable host coalescing bug fix */
  8571. if (tg3_flag(tp, 5755_PLUS))
  8572. val |= WDMAC_MODE_STATUS_TAG_FIX;
  8573. if (tg3_asic_rev(tp) == ASIC_REV_5785)
  8574. val |= WDMAC_MODE_BURST_ALL_DATA;
  8575. tw32_f(WDMAC_MODE, val);
  8576. udelay(40);
  8577. if (tg3_flag(tp, PCIX_MODE)) {
  8578. u16 pcix_cmd;
  8579. pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
  8580. &pcix_cmd);
  8581. if (tg3_asic_rev(tp) == ASIC_REV_5703) {
  8582. pcix_cmd &= ~PCI_X_CMD_MAX_READ;
  8583. pcix_cmd |= PCI_X_CMD_READ_2K;
  8584. } else if (tg3_asic_rev(tp) == ASIC_REV_5704) {
  8585. pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
  8586. pcix_cmd |= PCI_X_CMD_READ_2K;
  8587. }
  8588. pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
  8589. pcix_cmd);
  8590. }
  8591. tw32_f(RDMAC_MODE, rdmac_mode);
  8592. udelay(40);
  8593. if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
  8594. tg3_asic_rev(tp) == ASIC_REV_5720) {
  8595. for (i = 0; i < TG3_NUM_RDMA_CHANNELS; i++) {
  8596. if (tr32(TG3_RDMA_LENGTH + (i << 2)) > TG3_MAX_MTU(tp))
  8597. break;
  8598. }
  8599. if (i < TG3_NUM_RDMA_CHANNELS) {
  8600. val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
  8601. val |= tg3_lso_rd_dma_workaround_bit(tp);
  8602. tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val);
  8603. tg3_flag_set(tp, 5719_5720_RDMA_BUG);
  8604. }
  8605. }
  8606. tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
  8607. if (!tg3_flag(tp, 5705_PLUS))
  8608. tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
  8609. if (tg3_asic_rev(tp) == ASIC_REV_5761)
  8610. tw32(SNDDATAC_MODE,
  8611. SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
  8612. else
  8613. tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
  8614. tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
  8615. tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
  8616. val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
  8617. if (tg3_flag(tp, LRG_PROD_RING_CAP))
  8618. val |= RCVDBDI_MODE_LRG_RING_SZ;
  8619. tw32(RCVDBDI_MODE, val);
  8620. tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
  8621. if (tg3_flag(tp, HW_TSO_1) ||
  8622. tg3_flag(tp, HW_TSO_2) ||
  8623. tg3_flag(tp, HW_TSO_3))
  8624. tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
  8625. val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
  8626. if (tg3_flag(tp, ENABLE_TSS))
  8627. val |= SNDBDI_MODE_MULTI_TXQ_EN;
  8628. tw32(SNDBDI_MODE, val);
  8629. tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
  8630. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0) {
  8631. err = tg3_load_5701_a0_firmware_fix(tp);
  8632. if (err)
  8633. return err;
  8634. }
  8635. if (tg3_asic_rev(tp) == ASIC_REV_57766) {
  8636. /* Ignore any errors for the firmware download. If download
  8637. * fails, the device will operate with EEE disabled
  8638. */
  8639. tg3_load_57766_firmware(tp);
  8640. }
  8641. if (tg3_flag(tp, TSO_CAPABLE)) {
  8642. err = tg3_load_tso_firmware(tp);
  8643. if (err)
  8644. return err;
  8645. }
  8646. tp->tx_mode = TX_MODE_ENABLE;
  8647. if (tg3_flag(tp, 5755_PLUS) ||
  8648. tg3_asic_rev(tp) == ASIC_REV_5906)
  8649. tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
  8650. if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
  8651. tg3_asic_rev(tp) == ASIC_REV_5762) {
  8652. val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
  8653. tp->tx_mode &= ~val;
  8654. tp->tx_mode |= tr32(MAC_TX_MODE) & val;
  8655. }
  8656. tw32_f(MAC_TX_MODE, tp->tx_mode);
  8657. udelay(100);
  8658. if (tg3_flag(tp, ENABLE_RSS)) {
  8659. u32 rss_key[10];
  8660. tg3_rss_write_indir_tbl(tp);
  8661. netdev_rss_key_fill(rss_key, 10 * sizeof(u32));
  8662. for (i = 0; i < 10 ; i++)
  8663. tw32(MAC_RSS_HASH_KEY_0 + i*4, rss_key[i]);
  8664. }
  8665. tp->rx_mode = RX_MODE_ENABLE;
  8666. if (tg3_flag(tp, 5755_PLUS))
  8667. tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
  8668. if (tg3_asic_rev(tp) == ASIC_REV_5762)
  8669. tp->rx_mode |= RX_MODE_IPV4_FRAG_FIX;
  8670. if (tg3_flag(tp, ENABLE_RSS))
  8671. tp->rx_mode |= RX_MODE_RSS_ENABLE |
  8672. RX_MODE_RSS_ITBL_HASH_BITS_7 |
  8673. RX_MODE_RSS_IPV6_HASH_EN |
  8674. RX_MODE_RSS_TCP_IPV6_HASH_EN |
  8675. RX_MODE_RSS_IPV4_HASH_EN |
  8676. RX_MODE_RSS_TCP_IPV4_HASH_EN;
  8677. tw32_f(MAC_RX_MODE, tp->rx_mode);
  8678. udelay(10);
  8679. tw32(MAC_LED_CTRL, tp->led_ctrl);
  8680. tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
  8681. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
  8682. tw32_f(MAC_RX_MODE, RX_MODE_RESET);
  8683. udelay(10);
  8684. }
  8685. tw32_f(MAC_RX_MODE, tp->rx_mode);
  8686. udelay(10);
  8687. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
  8688. if ((tg3_asic_rev(tp) == ASIC_REV_5704) &&
  8689. !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
  8690. /* Set drive transmission level to 1.2V */
  8691. /* only if the signal pre-emphasis bit is not set */
  8692. val = tr32(MAC_SERDES_CFG);
  8693. val &= 0xfffff000;
  8694. val |= 0x880;
  8695. tw32(MAC_SERDES_CFG, val);
  8696. }
  8697. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A1)
  8698. tw32(MAC_SERDES_CFG, 0x616000);
  8699. }
  8700. /* Prevent chip from dropping frames when flow control
  8701. * is enabled.
  8702. */
  8703. if (tg3_flag(tp, 57765_CLASS))
  8704. val = 1;
  8705. else
  8706. val = 2;
  8707. tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
  8708. if (tg3_asic_rev(tp) == ASIC_REV_5704 &&
  8709. (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
  8710. /* Use hardware link auto-negotiation */
  8711. tg3_flag_set(tp, HW_AUTONEG);
  8712. }
  8713. if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
  8714. tg3_asic_rev(tp) == ASIC_REV_5714) {
  8715. u32 tmp;
  8716. tmp = tr32(SERDES_RX_CTRL);
  8717. tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
  8718. tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
  8719. tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
  8720. tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
  8721. }
  8722. if (!tg3_flag(tp, USE_PHYLIB)) {
  8723. if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
  8724. tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
  8725. err = tg3_setup_phy(tp, false);
  8726. if (err)
  8727. return err;
  8728. if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
  8729. !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
  8730. u32 tmp;
  8731. /* Clear CRC stats. */
  8732. if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
  8733. tg3_writephy(tp, MII_TG3_TEST1,
  8734. tmp | MII_TG3_TEST1_CRC_EN);
  8735. tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
  8736. }
  8737. }
  8738. }
  8739. __tg3_set_rx_mode(tp->dev);
  8740. /* Initialize receive rules. */
  8741. tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
  8742. tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
  8743. tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
  8744. tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
  8745. if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS))
  8746. limit = 8;
  8747. else
  8748. limit = 16;
  8749. if (tg3_flag(tp, ENABLE_ASF))
  8750. limit -= 4;
  8751. switch (limit) {
  8752. case 16:
  8753. tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
  8754. case 15:
  8755. tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
  8756. case 14:
  8757. tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
  8758. case 13:
  8759. tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
  8760. case 12:
  8761. tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
  8762. case 11:
  8763. tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
  8764. case 10:
  8765. tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
  8766. case 9:
  8767. tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
  8768. case 8:
  8769. tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
  8770. case 7:
  8771. tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
  8772. case 6:
  8773. tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
  8774. case 5:
  8775. tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
  8776. case 4:
  8777. /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
  8778. case 3:
  8779. /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
  8780. case 2:
  8781. case 1:
  8782. default:
  8783. break;
  8784. }
  8785. if (tg3_flag(tp, ENABLE_APE))
  8786. /* Write our heartbeat update interval to APE. */
  8787. tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
  8788. APE_HOST_HEARTBEAT_INT_DISABLE);
  8789. tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
  8790. return 0;
  8791. }
  8792. /* Called at device open time to get the chip ready for
  8793. * packet processing. Invoked with tp->lock held.
  8794. */
  8795. static int tg3_init_hw(struct tg3 *tp, bool reset_phy)
  8796. {
  8797. /* Chip may have been just powered on. If so, the boot code may still
  8798. * be running initialization. Wait for it to finish to avoid races in
  8799. * accessing the hardware.
  8800. */
  8801. tg3_enable_register_access(tp);
  8802. tg3_poll_fw(tp);
  8803. tg3_switch_clocks(tp);
  8804. tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
  8805. return tg3_reset_hw(tp, reset_phy);
  8806. }
  8807. static void tg3_sd_scan_scratchpad(struct tg3 *tp, struct tg3_ocir *ocir)
  8808. {
  8809. int i;
  8810. for (i = 0; i < TG3_SD_NUM_RECS; i++, ocir++) {
  8811. u32 off = i * TG3_OCIR_LEN, len = TG3_OCIR_LEN;
  8812. tg3_ape_scratchpad_read(tp, (u32 *) ocir, off, len);
  8813. off += len;
  8814. if (ocir->signature != TG3_OCIR_SIG_MAGIC ||
  8815. !(ocir->version_flags & TG3_OCIR_FLAG_ACTIVE))
  8816. memset(ocir, 0, TG3_OCIR_LEN);
  8817. }
  8818. }
  8819. /* sysfs attributes for hwmon */
  8820. static ssize_t tg3_show_temp(struct device *dev,
  8821. struct device_attribute *devattr, char *buf)
  8822. {
  8823. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  8824. struct tg3 *tp = dev_get_drvdata(dev);
  8825. u32 temperature;
  8826. spin_lock_bh(&tp->lock);
  8827. tg3_ape_scratchpad_read(tp, &temperature, attr->index,
  8828. sizeof(temperature));
  8829. spin_unlock_bh(&tp->lock);
  8830. return sprintf(buf, "%u\n", temperature);
  8831. }
  8832. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, tg3_show_temp, NULL,
  8833. TG3_TEMP_SENSOR_OFFSET);
  8834. static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, tg3_show_temp, NULL,
  8835. TG3_TEMP_CAUTION_OFFSET);
  8836. static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, tg3_show_temp, NULL,
  8837. TG3_TEMP_MAX_OFFSET);
  8838. static struct attribute *tg3_attrs[] = {
  8839. &sensor_dev_attr_temp1_input.dev_attr.attr,
  8840. &sensor_dev_attr_temp1_crit.dev_attr.attr,
  8841. &sensor_dev_attr_temp1_max.dev_attr.attr,
  8842. NULL
  8843. };
  8844. ATTRIBUTE_GROUPS(tg3);
  8845. static void tg3_hwmon_close(struct tg3 *tp)
  8846. {
  8847. if (tp->hwmon_dev) {
  8848. hwmon_device_unregister(tp->hwmon_dev);
  8849. tp->hwmon_dev = NULL;
  8850. }
  8851. }
  8852. static void tg3_hwmon_open(struct tg3 *tp)
  8853. {
  8854. int i;
  8855. u32 size = 0;
  8856. struct pci_dev *pdev = tp->pdev;
  8857. struct tg3_ocir ocirs[TG3_SD_NUM_RECS];
  8858. tg3_sd_scan_scratchpad(tp, ocirs);
  8859. for (i = 0; i < TG3_SD_NUM_RECS; i++) {
  8860. if (!ocirs[i].src_data_length)
  8861. continue;
  8862. size += ocirs[i].src_hdr_length;
  8863. size += ocirs[i].src_data_length;
  8864. }
  8865. if (!size)
  8866. return;
  8867. tp->hwmon_dev = hwmon_device_register_with_groups(&pdev->dev, "tg3",
  8868. tp, tg3_groups);
  8869. if (IS_ERR(tp->hwmon_dev)) {
  8870. tp->hwmon_dev = NULL;
  8871. dev_err(&pdev->dev, "Cannot register hwmon device, aborting\n");
  8872. }
  8873. }
  8874. #define TG3_STAT_ADD32(PSTAT, REG) \
  8875. do { u32 __val = tr32(REG); \
  8876. (PSTAT)->low += __val; \
  8877. if ((PSTAT)->low < __val) \
  8878. (PSTAT)->high += 1; \
  8879. } while (0)
  8880. static void tg3_periodic_fetch_stats(struct tg3 *tp)
  8881. {
  8882. struct tg3_hw_stats *sp = tp->hw_stats;
  8883. if (!tp->link_up)
  8884. return;
  8885. TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
  8886. TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
  8887. TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
  8888. TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
  8889. TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
  8890. TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
  8891. TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
  8892. TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
  8893. TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
  8894. TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
  8895. TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
  8896. TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
  8897. TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
  8898. if (unlikely(tg3_flag(tp, 5719_5720_RDMA_BUG) &&
  8899. (sp->tx_ucast_packets.low + sp->tx_mcast_packets.low +
  8900. sp->tx_bcast_packets.low) > TG3_NUM_RDMA_CHANNELS)) {
  8901. u32 val;
  8902. val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
  8903. val &= ~tg3_lso_rd_dma_workaround_bit(tp);
  8904. tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val);
  8905. tg3_flag_clear(tp, 5719_5720_RDMA_BUG);
  8906. }
  8907. TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
  8908. TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
  8909. TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
  8910. TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
  8911. TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
  8912. TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
  8913. TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
  8914. TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
  8915. TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
  8916. TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
  8917. TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
  8918. TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
  8919. TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
  8920. TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
  8921. TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
  8922. if (tg3_asic_rev(tp) != ASIC_REV_5717 &&
  8923. tg3_asic_rev(tp) != ASIC_REV_5762 &&
  8924. tg3_chip_rev_id(tp) != CHIPREV_ID_5719_A0 &&
  8925. tg3_chip_rev_id(tp) != CHIPREV_ID_5720_A0) {
  8926. TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
  8927. } else {
  8928. u32 val = tr32(HOSTCC_FLOW_ATTN);
  8929. val = (val & HOSTCC_FLOW_ATTN_MBUF_LWM) ? 1 : 0;
  8930. if (val) {
  8931. tw32(HOSTCC_FLOW_ATTN, HOSTCC_FLOW_ATTN_MBUF_LWM);
  8932. sp->rx_discards.low += val;
  8933. if (sp->rx_discards.low < val)
  8934. sp->rx_discards.high += 1;
  8935. }
  8936. sp->mbuf_lwm_thresh_hit = sp->rx_discards;
  8937. }
  8938. TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
  8939. }
  8940. static void tg3_chk_missed_msi(struct tg3 *tp)
  8941. {
  8942. u32 i;
  8943. for (i = 0; i < tp->irq_cnt; i++) {
  8944. struct tg3_napi *tnapi = &tp->napi[i];
  8945. if (tg3_has_work(tnapi)) {
  8946. if (tnapi->last_rx_cons == tnapi->rx_rcb_ptr &&
  8947. tnapi->last_tx_cons == tnapi->tx_cons) {
  8948. if (tnapi->chk_msi_cnt < 1) {
  8949. tnapi->chk_msi_cnt++;
  8950. return;
  8951. }
  8952. tg3_msi(0, tnapi);
  8953. }
  8954. }
  8955. tnapi->chk_msi_cnt = 0;
  8956. tnapi->last_rx_cons = tnapi->rx_rcb_ptr;
  8957. tnapi->last_tx_cons = tnapi->tx_cons;
  8958. }
  8959. }
  8960. static void tg3_timer(unsigned long __opaque)
  8961. {
  8962. struct tg3 *tp = (struct tg3 *) __opaque;
  8963. spin_lock(&tp->lock);
  8964. if (tp->irq_sync || tg3_flag(tp, RESET_TASK_PENDING)) {
  8965. spin_unlock(&tp->lock);
  8966. goto restart_timer;
  8967. }
  8968. if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
  8969. tg3_flag(tp, 57765_CLASS))
  8970. tg3_chk_missed_msi(tp);
  8971. if (tg3_flag(tp, FLUSH_POSTED_WRITES)) {
  8972. /* BCM4785: Flush posted writes from GbE to host memory. */
  8973. tr32(HOSTCC_MODE);
  8974. }
  8975. if (!tg3_flag(tp, TAGGED_STATUS)) {
  8976. /* All of this garbage is because when using non-tagged
  8977. * IRQ status the mailbox/status_block protocol the chip
  8978. * uses with the cpu is race prone.
  8979. */
  8980. if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
  8981. tw32(GRC_LOCAL_CTRL,
  8982. tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
  8983. } else {
  8984. tw32(HOSTCC_MODE, tp->coalesce_mode |
  8985. HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
  8986. }
  8987. if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
  8988. spin_unlock(&tp->lock);
  8989. tg3_reset_task_schedule(tp);
  8990. goto restart_timer;
  8991. }
  8992. }
  8993. /* This part only runs once per second. */
  8994. if (!--tp->timer_counter) {
  8995. if (tg3_flag(tp, 5705_PLUS))
  8996. tg3_periodic_fetch_stats(tp);
  8997. if (tp->setlpicnt && !--tp->setlpicnt)
  8998. tg3_phy_eee_enable(tp);
  8999. if (tg3_flag(tp, USE_LINKCHG_REG)) {
  9000. u32 mac_stat;
  9001. int phy_event;
  9002. mac_stat = tr32(MAC_STATUS);
  9003. phy_event = 0;
  9004. if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
  9005. if (mac_stat & MAC_STATUS_MI_INTERRUPT)
  9006. phy_event = 1;
  9007. } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
  9008. phy_event = 1;
  9009. if (phy_event)
  9010. tg3_setup_phy(tp, false);
  9011. } else if (tg3_flag(tp, POLL_SERDES)) {
  9012. u32 mac_stat = tr32(MAC_STATUS);
  9013. int need_setup = 0;
  9014. if (tp->link_up &&
  9015. (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
  9016. need_setup = 1;
  9017. }
  9018. if (!tp->link_up &&
  9019. (mac_stat & (MAC_STATUS_PCS_SYNCED |
  9020. MAC_STATUS_SIGNAL_DET))) {
  9021. need_setup = 1;
  9022. }
  9023. if (need_setup) {
  9024. if (!tp->serdes_counter) {
  9025. tw32_f(MAC_MODE,
  9026. (tp->mac_mode &
  9027. ~MAC_MODE_PORT_MODE_MASK));
  9028. udelay(40);
  9029. tw32_f(MAC_MODE, tp->mac_mode);
  9030. udelay(40);
  9031. }
  9032. tg3_setup_phy(tp, false);
  9033. }
  9034. } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
  9035. tg3_flag(tp, 5780_CLASS)) {
  9036. tg3_serdes_parallel_detect(tp);
  9037. } else if (tg3_flag(tp, POLL_CPMU_LINK)) {
  9038. u32 cpmu = tr32(TG3_CPMU_STATUS);
  9039. bool link_up = !((cpmu & TG3_CPMU_STATUS_LINK_MASK) ==
  9040. TG3_CPMU_STATUS_LINK_MASK);
  9041. if (link_up != tp->link_up)
  9042. tg3_setup_phy(tp, false);
  9043. }
  9044. tp->timer_counter = tp->timer_multiplier;
  9045. }
  9046. /* Heartbeat is only sent once every 2 seconds.
  9047. *
  9048. * The heartbeat is to tell the ASF firmware that the host
  9049. * driver is still alive. In the event that the OS crashes,
  9050. * ASF needs to reset the hardware to free up the FIFO space
  9051. * that may be filled with rx packets destined for the host.
  9052. * If the FIFO is full, ASF will no longer function properly.
  9053. *
  9054. * Unintended resets have been reported on real time kernels
  9055. * where the timer doesn't run on time. Netpoll will also have
  9056. * same problem.
  9057. *
  9058. * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
  9059. * to check the ring condition when the heartbeat is expiring
  9060. * before doing the reset. This will prevent most unintended
  9061. * resets.
  9062. */
  9063. if (!--tp->asf_counter) {
  9064. if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
  9065. tg3_wait_for_event_ack(tp);
  9066. tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
  9067. FWCMD_NICDRV_ALIVE3);
  9068. tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
  9069. tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
  9070. TG3_FW_UPDATE_TIMEOUT_SEC);
  9071. tg3_generate_fw_event(tp);
  9072. }
  9073. tp->asf_counter = tp->asf_multiplier;
  9074. }
  9075. spin_unlock(&tp->lock);
  9076. restart_timer:
  9077. tp->timer.expires = jiffies + tp->timer_offset;
  9078. add_timer(&tp->timer);
  9079. }
  9080. static void tg3_timer_init(struct tg3 *tp)
  9081. {
  9082. if (tg3_flag(tp, TAGGED_STATUS) &&
  9083. tg3_asic_rev(tp) != ASIC_REV_5717 &&
  9084. !tg3_flag(tp, 57765_CLASS))
  9085. tp->timer_offset = HZ;
  9086. else
  9087. tp->timer_offset = HZ / 10;
  9088. BUG_ON(tp->timer_offset > HZ);
  9089. tp->timer_multiplier = (HZ / tp->timer_offset);
  9090. tp->asf_multiplier = (HZ / tp->timer_offset) *
  9091. TG3_FW_UPDATE_FREQ_SEC;
  9092. init_timer(&tp->timer);
  9093. tp->timer.data = (unsigned long) tp;
  9094. tp->timer.function = tg3_timer;
  9095. }
  9096. static void tg3_timer_start(struct tg3 *tp)
  9097. {
  9098. tp->asf_counter = tp->asf_multiplier;
  9099. tp->timer_counter = tp->timer_multiplier;
  9100. tp->timer.expires = jiffies + tp->timer_offset;
  9101. add_timer(&tp->timer);
  9102. }
  9103. static void tg3_timer_stop(struct tg3 *tp)
  9104. {
  9105. del_timer_sync(&tp->timer);
  9106. }
  9107. /* Restart hardware after configuration changes, self-test, etc.
  9108. * Invoked with tp->lock held.
  9109. */
  9110. static int tg3_restart_hw(struct tg3 *tp, bool reset_phy)
  9111. __releases(tp->lock)
  9112. __acquires(tp->lock)
  9113. {
  9114. int err;
  9115. err = tg3_init_hw(tp, reset_phy);
  9116. if (err) {
  9117. netdev_err(tp->dev,
  9118. "Failed to re-initialize device, aborting\n");
  9119. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  9120. tg3_full_unlock(tp);
  9121. tg3_timer_stop(tp);
  9122. tp->irq_sync = 0;
  9123. tg3_napi_enable(tp);
  9124. dev_close(tp->dev);
  9125. tg3_full_lock(tp, 0);
  9126. }
  9127. return err;
  9128. }
  9129. static void tg3_reset_task(struct work_struct *work)
  9130. {
  9131. struct tg3 *tp = container_of(work, struct tg3, reset_task);
  9132. int err;
  9133. rtnl_lock();
  9134. tg3_full_lock(tp, 0);
  9135. if (!netif_running(tp->dev)) {
  9136. tg3_flag_clear(tp, RESET_TASK_PENDING);
  9137. tg3_full_unlock(tp);
  9138. rtnl_unlock();
  9139. return;
  9140. }
  9141. tg3_full_unlock(tp);
  9142. tg3_phy_stop(tp);
  9143. tg3_netif_stop(tp);
  9144. tg3_full_lock(tp, 1);
  9145. if (tg3_flag(tp, TX_RECOVERY_PENDING)) {
  9146. tp->write32_tx_mbox = tg3_write32_tx_mbox;
  9147. tp->write32_rx_mbox = tg3_write_flush_reg32;
  9148. tg3_flag_set(tp, MBOX_WRITE_REORDER);
  9149. tg3_flag_clear(tp, TX_RECOVERY_PENDING);
  9150. }
  9151. tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
  9152. err = tg3_init_hw(tp, true);
  9153. if (err)
  9154. goto out;
  9155. tg3_netif_start(tp);
  9156. out:
  9157. tg3_full_unlock(tp);
  9158. if (!err)
  9159. tg3_phy_start(tp);
  9160. tg3_flag_clear(tp, RESET_TASK_PENDING);
  9161. rtnl_unlock();
  9162. }
  9163. static int tg3_request_irq(struct tg3 *tp, int irq_num)
  9164. {
  9165. irq_handler_t fn;
  9166. unsigned long flags;
  9167. char *name;
  9168. struct tg3_napi *tnapi = &tp->napi[irq_num];
  9169. if (tp->irq_cnt == 1)
  9170. name = tp->dev->name;
  9171. else {
  9172. name = &tnapi->irq_lbl[0];
  9173. if (tnapi->tx_buffers && tnapi->rx_rcb)
  9174. snprintf(name, IFNAMSIZ,
  9175. "%s-txrx-%d", tp->dev->name, irq_num);
  9176. else if (tnapi->tx_buffers)
  9177. snprintf(name, IFNAMSIZ,
  9178. "%s-tx-%d", tp->dev->name, irq_num);
  9179. else if (tnapi->rx_rcb)
  9180. snprintf(name, IFNAMSIZ,
  9181. "%s-rx-%d", tp->dev->name, irq_num);
  9182. else
  9183. snprintf(name, IFNAMSIZ,
  9184. "%s-%d", tp->dev->name, irq_num);
  9185. name[IFNAMSIZ-1] = 0;
  9186. }
  9187. if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
  9188. fn = tg3_msi;
  9189. if (tg3_flag(tp, 1SHOT_MSI))
  9190. fn = tg3_msi_1shot;
  9191. flags = 0;
  9192. } else {
  9193. fn = tg3_interrupt;
  9194. if (tg3_flag(tp, TAGGED_STATUS))
  9195. fn = tg3_interrupt_tagged;
  9196. flags = IRQF_SHARED;
  9197. }
  9198. return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
  9199. }
  9200. static int tg3_test_interrupt(struct tg3 *tp)
  9201. {
  9202. struct tg3_napi *tnapi = &tp->napi[0];
  9203. struct net_device *dev = tp->dev;
  9204. int err, i, intr_ok = 0;
  9205. u32 val;
  9206. if (!netif_running(dev))
  9207. return -ENODEV;
  9208. tg3_disable_ints(tp);
  9209. free_irq(tnapi->irq_vec, tnapi);
  9210. /*
  9211. * Turn off MSI one shot mode. Otherwise this test has no
  9212. * observable way to know whether the interrupt was delivered.
  9213. */
  9214. if (tg3_flag(tp, 57765_PLUS)) {
  9215. val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
  9216. tw32(MSGINT_MODE, val);
  9217. }
  9218. err = request_irq(tnapi->irq_vec, tg3_test_isr,
  9219. IRQF_SHARED, dev->name, tnapi);
  9220. if (err)
  9221. return err;
  9222. tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
  9223. tg3_enable_ints(tp);
  9224. tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
  9225. tnapi->coal_now);
  9226. for (i = 0; i < 5; i++) {
  9227. u32 int_mbox, misc_host_ctrl;
  9228. int_mbox = tr32_mailbox(tnapi->int_mbox);
  9229. misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
  9230. if ((int_mbox != 0) ||
  9231. (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
  9232. intr_ok = 1;
  9233. break;
  9234. }
  9235. if (tg3_flag(tp, 57765_PLUS) &&
  9236. tnapi->hw_status->status_tag != tnapi->last_tag)
  9237. tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
  9238. msleep(10);
  9239. }
  9240. tg3_disable_ints(tp);
  9241. free_irq(tnapi->irq_vec, tnapi);
  9242. err = tg3_request_irq(tp, 0);
  9243. if (err)
  9244. return err;
  9245. if (intr_ok) {
  9246. /* Reenable MSI one shot mode. */
  9247. if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, 1SHOT_MSI)) {
  9248. val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
  9249. tw32(MSGINT_MODE, val);
  9250. }
  9251. return 0;
  9252. }
  9253. return -EIO;
  9254. }
  9255. /* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
  9256. * successfully restored
  9257. */
  9258. static int tg3_test_msi(struct tg3 *tp)
  9259. {
  9260. int err;
  9261. u16 pci_cmd;
  9262. if (!tg3_flag(tp, USING_MSI))
  9263. return 0;
  9264. /* Turn off SERR reporting in case MSI terminates with Master
  9265. * Abort.
  9266. */
  9267. pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
  9268. pci_write_config_word(tp->pdev, PCI_COMMAND,
  9269. pci_cmd & ~PCI_COMMAND_SERR);
  9270. err = tg3_test_interrupt(tp);
  9271. pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
  9272. if (!err)
  9273. return 0;
  9274. /* other failures */
  9275. if (err != -EIO)
  9276. return err;
  9277. /* MSI test failed, go back to INTx mode */
  9278. netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
  9279. "to INTx mode. Please report this failure to the PCI "
  9280. "maintainer and include system chipset information\n");
  9281. free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
  9282. pci_disable_msi(tp->pdev);
  9283. tg3_flag_clear(tp, USING_MSI);
  9284. tp->napi[0].irq_vec = tp->pdev->irq;
  9285. err = tg3_request_irq(tp, 0);
  9286. if (err)
  9287. return err;
  9288. /* Need to reset the chip because the MSI cycle may have terminated
  9289. * with Master Abort.
  9290. */
  9291. tg3_full_lock(tp, 1);
  9292. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  9293. err = tg3_init_hw(tp, true);
  9294. tg3_full_unlock(tp);
  9295. if (err)
  9296. free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
  9297. return err;
  9298. }
  9299. static int tg3_request_firmware(struct tg3 *tp)
  9300. {
  9301. const struct tg3_firmware_hdr *fw_hdr;
  9302. if (reject_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
  9303. netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
  9304. tp->fw_needed);
  9305. return -ENOENT;
  9306. }
  9307. fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
  9308. /* Firmware blob starts with version numbers, followed by
  9309. * start address and _full_ length including BSS sections
  9310. * (which must be longer than the actual data, of course
  9311. */
  9312. tp->fw_len = be32_to_cpu(fw_hdr->len); /* includes bss */
  9313. if (tp->fw_len < (tp->fw->size - TG3_FW_HDR_LEN)) {
  9314. netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
  9315. tp->fw_len, tp->fw_needed);
  9316. release_firmware(tp->fw);
  9317. tp->fw = NULL;
  9318. return -EINVAL;
  9319. }
  9320. /* We no longer need firmware; we have it. */
  9321. tp->fw_needed = NULL;
  9322. return 0;
  9323. }
  9324. static u32 tg3_irq_count(struct tg3 *tp)
  9325. {
  9326. u32 irq_cnt = max(tp->rxq_cnt, tp->txq_cnt);
  9327. if (irq_cnt > 1) {
  9328. /* We want as many rx rings enabled as there are cpus.
  9329. * In multiqueue MSI-X mode, the first MSI-X vector
  9330. * only deals with link interrupts, etc, so we add
  9331. * one to the number of vectors we are requesting.
  9332. */
  9333. irq_cnt = min_t(unsigned, irq_cnt + 1, tp->irq_max);
  9334. }
  9335. return irq_cnt;
  9336. }
  9337. static bool tg3_enable_msix(struct tg3 *tp)
  9338. {
  9339. int i, rc;
  9340. struct msix_entry msix_ent[TG3_IRQ_MAX_VECS];
  9341. tp->txq_cnt = tp->txq_req;
  9342. tp->rxq_cnt = tp->rxq_req;
  9343. if (!tp->rxq_cnt)
  9344. tp->rxq_cnt = netif_get_num_default_rss_queues();
  9345. if (tp->rxq_cnt > tp->rxq_max)
  9346. tp->rxq_cnt = tp->rxq_max;
  9347. /* Disable multiple TX rings by default. Simple round-robin hardware
  9348. * scheduling of the TX rings can cause starvation of rings with
  9349. * small packets when other rings have TSO or jumbo packets.
  9350. */
  9351. if (!tp->txq_req)
  9352. tp->txq_cnt = 1;
  9353. tp->irq_cnt = tg3_irq_count(tp);
  9354. for (i = 0; i < tp->irq_max; i++) {
  9355. msix_ent[i].entry = i;
  9356. msix_ent[i].vector = 0;
  9357. }
  9358. rc = pci_enable_msix_range(tp->pdev, msix_ent, 1, tp->irq_cnt);
  9359. if (rc < 0) {
  9360. return false;
  9361. } else if (rc < tp->irq_cnt) {
  9362. netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
  9363. tp->irq_cnt, rc);
  9364. tp->irq_cnt = rc;
  9365. tp->rxq_cnt = max(rc - 1, 1);
  9366. if (tp->txq_cnt)
  9367. tp->txq_cnt = min(tp->rxq_cnt, tp->txq_max);
  9368. }
  9369. for (i = 0; i < tp->irq_max; i++)
  9370. tp->napi[i].irq_vec = msix_ent[i].vector;
  9371. if (netif_set_real_num_rx_queues(tp->dev, tp->rxq_cnt)) {
  9372. pci_disable_msix(tp->pdev);
  9373. return false;
  9374. }
  9375. if (tp->irq_cnt == 1)
  9376. return true;
  9377. tg3_flag_set(tp, ENABLE_RSS);
  9378. if (tp->txq_cnt > 1)
  9379. tg3_flag_set(tp, ENABLE_TSS);
  9380. netif_set_real_num_tx_queues(tp->dev, tp->txq_cnt);
  9381. return true;
  9382. }
  9383. static void tg3_ints_init(struct tg3 *tp)
  9384. {
  9385. if ((tg3_flag(tp, SUPPORT_MSI) || tg3_flag(tp, SUPPORT_MSIX)) &&
  9386. !tg3_flag(tp, TAGGED_STATUS)) {
  9387. /* All MSI supporting chips should support tagged
  9388. * status. Assert that this is the case.
  9389. */
  9390. netdev_warn(tp->dev,
  9391. "MSI without TAGGED_STATUS? Not using MSI\n");
  9392. goto defcfg;
  9393. }
  9394. if (tg3_flag(tp, SUPPORT_MSIX) && tg3_enable_msix(tp))
  9395. tg3_flag_set(tp, USING_MSIX);
  9396. else if (tg3_flag(tp, SUPPORT_MSI) && pci_enable_msi(tp->pdev) == 0)
  9397. tg3_flag_set(tp, USING_MSI);
  9398. if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
  9399. u32 msi_mode = tr32(MSGINT_MODE);
  9400. if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1)
  9401. msi_mode |= MSGINT_MODE_MULTIVEC_EN;
  9402. if (!tg3_flag(tp, 1SHOT_MSI))
  9403. msi_mode |= MSGINT_MODE_ONE_SHOT_DISABLE;
  9404. tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
  9405. }
  9406. defcfg:
  9407. if (!tg3_flag(tp, USING_MSIX)) {
  9408. tp->irq_cnt = 1;
  9409. tp->napi[0].irq_vec = tp->pdev->irq;
  9410. }
  9411. if (tp->irq_cnt == 1) {
  9412. tp->txq_cnt = 1;
  9413. tp->rxq_cnt = 1;
  9414. netif_set_real_num_tx_queues(tp->dev, 1);
  9415. netif_set_real_num_rx_queues(tp->dev, 1);
  9416. }
  9417. }
  9418. static void tg3_ints_fini(struct tg3 *tp)
  9419. {
  9420. if (tg3_flag(tp, USING_MSIX))
  9421. pci_disable_msix(tp->pdev);
  9422. else if (tg3_flag(tp, USING_MSI))
  9423. pci_disable_msi(tp->pdev);
  9424. tg3_flag_clear(tp, USING_MSI);
  9425. tg3_flag_clear(tp, USING_MSIX);
  9426. tg3_flag_clear(tp, ENABLE_RSS);
  9427. tg3_flag_clear(tp, ENABLE_TSS);
  9428. }
  9429. static int tg3_start(struct tg3 *tp, bool reset_phy, bool test_irq,
  9430. bool init)
  9431. {
  9432. struct net_device *dev = tp->dev;
  9433. int i, err;
  9434. /*
  9435. * Setup interrupts first so we know how
  9436. * many NAPI resources to allocate
  9437. */
  9438. tg3_ints_init(tp);
  9439. tg3_rss_check_indir_tbl(tp);
  9440. /* The placement of this call is tied
  9441. * to the setup and use of Host TX descriptors.
  9442. */
  9443. err = tg3_alloc_consistent(tp);
  9444. if (err)
  9445. goto out_ints_fini;
  9446. tg3_napi_init(tp);
  9447. tg3_napi_enable(tp);
  9448. for (i = 0; i < tp->irq_cnt; i++) {
  9449. struct tg3_napi *tnapi = &tp->napi[i];
  9450. err = tg3_request_irq(tp, i);
  9451. if (err) {
  9452. for (i--; i >= 0; i--) {
  9453. tnapi = &tp->napi[i];
  9454. free_irq(tnapi->irq_vec, tnapi);
  9455. }
  9456. goto out_napi_fini;
  9457. }
  9458. }
  9459. tg3_full_lock(tp, 0);
  9460. if (init)
  9461. tg3_ape_driver_state_change(tp, RESET_KIND_INIT);
  9462. err = tg3_init_hw(tp, reset_phy);
  9463. if (err) {
  9464. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  9465. tg3_free_rings(tp);
  9466. }
  9467. tg3_full_unlock(tp);
  9468. if (err)
  9469. goto out_free_irq;
  9470. if (test_irq && tg3_flag(tp, USING_MSI)) {
  9471. err = tg3_test_msi(tp);
  9472. if (err) {
  9473. tg3_full_lock(tp, 0);
  9474. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  9475. tg3_free_rings(tp);
  9476. tg3_full_unlock(tp);
  9477. goto out_napi_fini;
  9478. }
  9479. if (!tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
  9480. u32 val = tr32(PCIE_TRANSACTION_CFG);
  9481. tw32(PCIE_TRANSACTION_CFG,
  9482. val | PCIE_TRANS_CFG_1SHOT_MSI);
  9483. }
  9484. }
  9485. tg3_phy_start(tp);
  9486. tg3_hwmon_open(tp);
  9487. tg3_full_lock(tp, 0);
  9488. tg3_timer_start(tp);
  9489. tg3_flag_set(tp, INIT_COMPLETE);
  9490. tg3_enable_ints(tp);
  9491. tg3_ptp_resume(tp);
  9492. tg3_full_unlock(tp);
  9493. netif_tx_start_all_queues(dev);
  9494. /*
  9495. * Reset loopback feature if it was turned on while the device was down
  9496. * make sure that it's installed properly now.
  9497. */
  9498. if (dev->features & NETIF_F_LOOPBACK)
  9499. tg3_set_loopback(dev, dev->features);
  9500. return 0;
  9501. out_free_irq:
  9502. for (i = tp->irq_cnt - 1; i >= 0; i--) {
  9503. struct tg3_napi *tnapi = &tp->napi[i];
  9504. free_irq(tnapi->irq_vec, tnapi);
  9505. }
  9506. out_napi_fini:
  9507. tg3_napi_disable(tp);
  9508. tg3_napi_fini(tp);
  9509. tg3_free_consistent(tp);
  9510. out_ints_fini:
  9511. tg3_ints_fini(tp);
  9512. return err;
  9513. }
  9514. static void tg3_stop(struct tg3 *tp)
  9515. {
  9516. int i;
  9517. tg3_reset_task_cancel(tp);
  9518. tg3_netif_stop(tp);
  9519. tg3_timer_stop(tp);
  9520. tg3_hwmon_close(tp);
  9521. tg3_phy_stop(tp);
  9522. tg3_full_lock(tp, 1);
  9523. tg3_disable_ints(tp);
  9524. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  9525. tg3_free_rings(tp);
  9526. tg3_flag_clear(tp, INIT_COMPLETE);
  9527. tg3_full_unlock(tp);
  9528. for (i = tp->irq_cnt - 1; i >= 0; i--) {
  9529. struct tg3_napi *tnapi = &tp->napi[i];
  9530. free_irq(tnapi->irq_vec, tnapi);
  9531. }
  9532. tg3_ints_fini(tp);
  9533. tg3_napi_fini(tp);
  9534. tg3_free_consistent(tp);
  9535. }
  9536. static int tg3_open(struct net_device *dev)
  9537. {
  9538. struct tg3 *tp = netdev_priv(dev);
  9539. int err;
  9540. if (tp->pcierr_recovery) {
  9541. netdev_err(dev, "Failed to open device. PCI error recovery "
  9542. "in progress\n");
  9543. return -EAGAIN;
  9544. }
  9545. if (tp->fw_needed) {
  9546. err = tg3_request_firmware(tp);
  9547. if (tg3_asic_rev(tp) == ASIC_REV_57766) {
  9548. if (err) {
  9549. netdev_warn(tp->dev, "EEE capability disabled\n");
  9550. tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
  9551. } else if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP)) {
  9552. netdev_warn(tp->dev, "EEE capability restored\n");
  9553. tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
  9554. }
  9555. } else if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0) {
  9556. if (err)
  9557. return err;
  9558. } else if (err) {
  9559. netdev_warn(tp->dev, "TSO capability disabled\n");
  9560. tg3_flag_clear(tp, TSO_CAPABLE);
  9561. } else if (!tg3_flag(tp, TSO_CAPABLE)) {
  9562. netdev_notice(tp->dev, "TSO capability restored\n");
  9563. tg3_flag_set(tp, TSO_CAPABLE);
  9564. }
  9565. }
  9566. tg3_carrier_off(tp);
  9567. err = tg3_power_up(tp);
  9568. if (err)
  9569. return err;
  9570. tg3_full_lock(tp, 0);
  9571. tg3_disable_ints(tp);
  9572. tg3_flag_clear(tp, INIT_COMPLETE);
  9573. tg3_full_unlock(tp);
  9574. err = tg3_start(tp,
  9575. !(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN),
  9576. true, true);
  9577. if (err) {
  9578. tg3_frob_aux_power(tp, false);
  9579. pci_set_power_state(tp->pdev, PCI_D3hot);
  9580. }
  9581. return err;
  9582. }
  9583. static int tg3_close(struct net_device *dev)
  9584. {
  9585. struct tg3 *tp = netdev_priv(dev);
  9586. if (tp->pcierr_recovery) {
  9587. netdev_err(dev, "Failed to close device. PCI error recovery "
  9588. "in progress\n");
  9589. return -EAGAIN;
  9590. }
  9591. tg3_stop(tp);
  9592. /* Clear stats across close / open calls */
  9593. memset(&tp->net_stats_prev, 0, sizeof(tp->net_stats_prev));
  9594. memset(&tp->estats_prev, 0, sizeof(tp->estats_prev));
  9595. if (pci_device_is_present(tp->pdev)) {
  9596. tg3_power_down_prepare(tp);
  9597. tg3_carrier_off(tp);
  9598. }
  9599. return 0;
  9600. }
  9601. static inline u64 get_stat64(tg3_stat64_t *val)
  9602. {
  9603. return ((u64)val->high << 32) | ((u64)val->low);
  9604. }
  9605. static u64 tg3_calc_crc_errors(struct tg3 *tp)
  9606. {
  9607. struct tg3_hw_stats *hw_stats = tp->hw_stats;
  9608. if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
  9609. (tg3_asic_rev(tp) == ASIC_REV_5700 ||
  9610. tg3_asic_rev(tp) == ASIC_REV_5701)) {
  9611. u32 val;
  9612. if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
  9613. tg3_writephy(tp, MII_TG3_TEST1,
  9614. val | MII_TG3_TEST1_CRC_EN);
  9615. tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
  9616. } else
  9617. val = 0;
  9618. tp->phy_crc_errors += val;
  9619. return tp->phy_crc_errors;
  9620. }
  9621. return get_stat64(&hw_stats->rx_fcs_errors);
  9622. }
  9623. #define ESTAT_ADD(member) \
  9624. estats->member = old_estats->member + \
  9625. get_stat64(&hw_stats->member)
  9626. static void tg3_get_estats(struct tg3 *tp, struct tg3_ethtool_stats *estats)
  9627. {
  9628. struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
  9629. struct tg3_hw_stats *hw_stats = tp->hw_stats;
  9630. ESTAT_ADD(rx_octets);
  9631. ESTAT_ADD(rx_fragments);
  9632. ESTAT_ADD(rx_ucast_packets);
  9633. ESTAT_ADD(rx_mcast_packets);
  9634. ESTAT_ADD(rx_bcast_packets);
  9635. ESTAT_ADD(rx_fcs_errors);
  9636. ESTAT_ADD(rx_align_errors);
  9637. ESTAT_ADD(rx_xon_pause_rcvd);
  9638. ESTAT_ADD(rx_xoff_pause_rcvd);
  9639. ESTAT_ADD(rx_mac_ctrl_rcvd);
  9640. ESTAT_ADD(rx_xoff_entered);
  9641. ESTAT_ADD(rx_frame_too_long_errors);
  9642. ESTAT_ADD(rx_jabbers);
  9643. ESTAT_ADD(rx_undersize_packets);
  9644. ESTAT_ADD(rx_in_length_errors);
  9645. ESTAT_ADD(rx_out_length_errors);
  9646. ESTAT_ADD(rx_64_or_less_octet_packets);
  9647. ESTAT_ADD(rx_65_to_127_octet_packets);
  9648. ESTAT_ADD(rx_128_to_255_octet_packets);
  9649. ESTAT_ADD(rx_256_to_511_octet_packets);
  9650. ESTAT_ADD(rx_512_to_1023_octet_packets);
  9651. ESTAT_ADD(rx_1024_to_1522_octet_packets);
  9652. ESTAT_ADD(rx_1523_to_2047_octet_packets);
  9653. ESTAT_ADD(rx_2048_to_4095_octet_packets);
  9654. ESTAT_ADD(rx_4096_to_8191_octet_packets);
  9655. ESTAT_ADD(rx_8192_to_9022_octet_packets);
  9656. ESTAT_ADD(tx_octets);
  9657. ESTAT_ADD(tx_collisions);
  9658. ESTAT_ADD(tx_xon_sent);
  9659. ESTAT_ADD(tx_xoff_sent);
  9660. ESTAT_ADD(tx_flow_control);
  9661. ESTAT_ADD(tx_mac_errors);
  9662. ESTAT_ADD(tx_single_collisions);
  9663. ESTAT_ADD(tx_mult_collisions);
  9664. ESTAT_ADD(tx_deferred);
  9665. ESTAT_ADD(tx_excessive_collisions);
  9666. ESTAT_ADD(tx_late_collisions);
  9667. ESTAT_ADD(tx_collide_2times);
  9668. ESTAT_ADD(tx_collide_3times);
  9669. ESTAT_ADD(tx_collide_4times);
  9670. ESTAT_ADD(tx_collide_5times);
  9671. ESTAT_ADD(tx_collide_6times);
  9672. ESTAT_ADD(tx_collide_7times);
  9673. ESTAT_ADD(tx_collide_8times);
  9674. ESTAT_ADD(tx_collide_9times);
  9675. ESTAT_ADD(tx_collide_10times);
  9676. ESTAT_ADD(tx_collide_11times);
  9677. ESTAT_ADD(tx_collide_12times);
  9678. ESTAT_ADD(tx_collide_13times);
  9679. ESTAT_ADD(tx_collide_14times);
  9680. ESTAT_ADD(tx_collide_15times);
  9681. ESTAT_ADD(tx_ucast_packets);
  9682. ESTAT_ADD(tx_mcast_packets);
  9683. ESTAT_ADD(tx_bcast_packets);
  9684. ESTAT_ADD(tx_carrier_sense_errors);
  9685. ESTAT_ADD(tx_discards);
  9686. ESTAT_ADD(tx_errors);
  9687. ESTAT_ADD(dma_writeq_full);
  9688. ESTAT_ADD(dma_write_prioq_full);
  9689. ESTAT_ADD(rxbds_empty);
  9690. ESTAT_ADD(rx_discards);
  9691. ESTAT_ADD(rx_errors);
  9692. ESTAT_ADD(rx_threshold_hit);
  9693. ESTAT_ADD(dma_readq_full);
  9694. ESTAT_ADD(dma_read_prioq_full);
  9695. ESTAT_ADD(tx_comp_queue_full);
  9696. ESTAT_ADD(ring_set_send_prod_index);
  9697. ESTAT_ADD(ring_status_update);
  9698. ESTAT_ADD(nic_irqs);
  9699. ESTAT_ADD(nic_avoided_irqs);
  9700. ESTAT_ADD(nic_tx_threshold_hit);
  9701. ESTAT_ADD(mbuf_lwm_thresh_hit);
  9702. }
  9703. static void tg3_get_nstats(struct tg3 *tp, struct rtnl_link_stats64 *stats)
  9704. {
  9705. struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
  9706. struct tg3_hw_stats *hw_stats = tp->hw_stats;
  9707. stats->rx_packets = old_stats->rx_packets +
  9708. get_stat64(&hw_stats->rx_ucast_packets) +
  9709. get_stat64(&hw_stats->rx_mcast_packets) +
  9710. get_stat64(&hw_stats->rx_bcast_packets);
  9711. stats->tx_packets = old_stats->tx_packets +
  9712. get_stat64(&hw_stats->tx_ucast_packets) +
  9713. get_stat64(&hw_stats->tx_mcast_packets) +
  9714. get_stat64(&hw_stats->tx_bcast_packets);
  9715. stats->rx_bytes = old_stats->rx_bytes +
  9716. get_stat64(&hw_stats->rx_octets);
  9717. stats->tx_bytes = old_stats->tx_bytes +
  9718. get_stat64(&hw_stats->tx_octets);
  9719. stats->rx_errors = old_stats->rx_errors +
  9720. get_stat64(&hw_stats->rx_errors);
  9721. stats->tx_errors = old_stats->tx_errors +
  9722. get_stat64(&hw_stats->tx_errors) +
  9723. get_stat64(&hw_stats->tx_mac_errors) +
  9724. get_stat64(&hw_stats->tx_carrier_sense_errors) +
  9725. get_stat64(&hw_stats->tx_discards);
  9726. stats->multicast = old_stats->multicast +
  9727. get_stat64(&hw_stats->rx_mcast_packets);
  9728. stats->collisions = old_stats->collisions +
  9729. get_stat64(&hw_stats->tx_collisions);
  9730. stats->rx_length_errors = old_stats->rx_length_errors +
  9731. get_stat64(&hw_stats->rx_frame_too_long_errors) +
  9732. get_stat64(&hw_stats->rx_undersize_packets);
  9733. stats->rx_frame_errors = old_stats->rx_frame_errors +
  9734. get_stat64(&hw_stats->rx_align_errors);
  9735. stats->tx_aborted_errors = old_stats->tx_aborted_errors +
  9736. get_stat64(&hw_stats->tx_discards);
  9737. stats->tx_carrier_errors = old_stats->tx_carrier_errors +
  9738. get_stat64(&hw_stats->tx_carrier_sense_errors);
  9739. stats->rx_crc_errors = old_stats->rx_crc_errors +
  9740. tg3_calc_crc_errors(tp);
  9741. stats->rx_missed_errors = old_stats->rx_missed_errors +
  9742. get_stat64(&hw_stats->rx_discards);
  9743. stats->rx_dropped = tp->rx_dropped;
  9744. stats->tx_dropped = tp->tx_dropped;
  9745. }
  9746. static int tg3_get_regs_len(struct net_device *dev)
  9747. {
  9748. return TG3_REG_BLK_SIZE;
  9749. }
  9750. static void tg3_get_regs(struct net_device *dev,
  9751. struct ethtool_regs *regs, void *_p)
  9752. {
  9753. struct tg3 *tp = netdev_priv(dev);
  9754. regs->version = 0;
  9755. memset(_p, 0, TG3_REG_BLK_SIZE);
  9756. if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
  9757. return;
  9758. tg3_full_lock(tp, 0);
  9759. tg3_dump_legacy_regs(tp, (u32 *)_p);
  9760. tg3_full_unlock(tp);
  9761. }
  9762. static int tg3_get_eeprom_len(struct net_device *dev)
  9763. {
  9764. struct tg3 *tp = netdev_priv(dev);
  9765. return tp->nvram_size;
  9766. }
  9767. static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
  9768. {
  9769. struct tg3 *tp = netdev_priv(dev);
  9770. int ret, cpmu_restore = 0;
  9771. u8 *pd;
  9772. u32 i, offset, len, b_offset, b_count, cpmu_val = 0;
  9773. __be32 val;
  9774. if (tg3_flag(tp, NO_NVRAM))
  9775. return -EINVAL;
  9776. offset = eeprom->offset;
  9777. len = eeprom->len;
  9778. eeprom->len = 0;
  9779. eeprom->magic = TG3_EEPROM_MAGIC;
  9780. /* Override clock, link aware and link idle modes */
  9781. if (tg3_flag(tp, CPMU_PRESENT)) {
  9782. cpmu_val = tr32(TG3_CPMU_CTRL);
  9783. if (cpmu_val & (CPMU_CTRL_LINK_AWARE_MODE |
  9784. CPMU_CTRL_LINK_IDLE_MODE)) {
  9785. tw32(TG3_CPMU_CTRL, cpmu_val &
  9786. ~(CPMU_CTRL_LINK_AWARE_MODE |
  9787. CPMU_CTRL_LINK_IDLE_MODE));
  9788. cpmu_restore = 1;
  9789. }
  9790. }
  9791. tg3_override_clk(tp);
  9792. if (offset & 3) {
  9793. /* adjustments to start on required 4 byte boundary */
  9794. b_offset = offset & 3;
  9795. b_count = 4 - b_offset;
  9796. if (b_count > len) {
  9797. /* i.e. offset=1 len=2 */
  9798. b_count = len;
  9799. }
  9800. ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
  9801. if (ret)
  9802. goto eeprom_done;
  9803. memcpy(data, ((char *)&val) + b_offset, b_count);
  9804. len -= b_count;
  9805. offset += b_count;
  9806. eeprom->len += b_count;
  9807. }
  9808. /* read bytes up to the last 4 byte boundary */
  9809. pd = &data[eeprom->len];
  9810. for (i = 0; i < (len - (len & 3)); i += 4) {
  9811. ret = tg3_nvram_read_be32(tp, offset + i, &val);
  9812. if (ret) {
  9813. if (i)
  9814. i -= 4;
  9815. eeprom->len += i;
  9816. goto eeprom_done;
  9817. }
  9818. memcpy(pd + i, &val, 4);
  9819. if (need_resched()) {
  9820. if (signal_pending(current)) {
  9821. eeprom->len += i;
  9822. ret = -EINTR;
  9823. goto eeprom_done;
  9824. }
  9825. cond_resched();
  9826. }
  9827. }
  9828. eeprom->len += i;
  9829. if (len & 3) {
  9830. /* read last bytes not ending on 4 byte boundary */
  9831. pd = &data[eeprom->len];
  9832. b_count = len & 3;
  9833. b_offset = offset + len - b_count;
  9834. ret = tg3_nvram_read_be32(tp, b_offset, &val);
  9835. if (ret)
  9836. goto eeprom_done;
  9837. memcpy(pd, &val, b_count);
  9838. eeprom->len += b_count;
  9839. }
  9840. ret = 0;
  9841. eeprom_done:
  9842. /* Restore clock, link aware and link idle modes */
  9843. tg3_restore_clk(tp);
  9844. if (cpmu_restore)
  9845. tw32(TG3_CPMU_CTRL, cpmu_val);
  9846. return ret;
  9847. }
  9848. static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
  9849. {
  9850. struct tg3 *tp = netdev_priv(dev);
  9851. int ret;
  9852. u32 offset, len, b_offset, odd_len;
  9853. u8 *buf;
  9854. __be32 start, end;
  9855. if (tg3_flag(tp, NO_NVRAM) ||
  9856. eeprom->magic != TG3_EEPROM_MAGIC)
  9857. return -EINVAL;
  9858. offset = eeprom->offset;
  9859. len = eeprom->len;
  9860. if ((b_offset = (offset & 3))) {
  9861. /* adjustments to start on required 4 byte boundary */
  9862. ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
  9863. if (ret)
  9864. return ret;
  9865. len += b_offset;
  9866. offset &= ~3;
  9867. if (len < 4)
  9868. len = 4;
  9869. }
  9870. odd_len = 0;
  9871. if (len & 3) {
  9872. /* adjustments to end on required 4 byte boundary */
  9873. odd_len = 1;
  9874. len = (len + 3) & ~3;
  9875. ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
  9876. if (ret)
  9877. return ret;
  9878. }
  9879. buf = data;
  9880. if (b_offset || odd_len) {
  9881. buf = kmalloc(len, GFP_KERNEL);
  9882. if (!buf)
  9883. return -ENOMEM;
  9884. if (b_offset)
  9885. memcpy(buf, &start, 4);
  9886. if (odd_len)
  9887. memcpy(buf+len-4, &end, 4);
  9888. memcpy(buf + b_offset, data, eeprom->len);
  9889. }
  9890. ret = tg3_nvram_write_block(tp, offset, len, buf);
  9891. if (buf != data)
  9892. kfree(buf);
  9893. return ret;
  9894. }
  9895. static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  9896. {
  9897. struct tg3 *tp = netdev_priv(dev);
  9898. if (tg3_flag(tp, USE_PHYLIB)) {
  9899. struct phy_device *phydev;
  9900. if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
  9901. return -EAGAIN;
  9902. phydev = tp->mdio_bus->phy_map[tp->phy_addr];
  9903. return phy_ethtool_gset(phydev, cmd);
  9904. }
  9905. cmd->supported = (SUPPORTED_Autoneg);
  9906. if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
  9907. cmd->supported |= (SUPPORTED_1000baseT_Half |
  9908. SUPPORTED_1000baseT_Full);
  9909. if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
  9910. cmd->supported |= (SUPPORTED_100baseT_Half |
  9911. SUPPORTED_100baseT_Full |
  9912. SUPPORTED_10baseT_Half |
  9913. SUPPORTED_10baseT_Full |
  9914. SUPPORTED_TP);
  9915. cmd->port = PORT_TP;
  9916. } else {
  9917. cmd->supported |= SUPPORTED_FIBRE;
  9918. cmd->port = PORT_FIBRE;
  9919. }
  9920. cmd->advertising = tp->link_config.advertising;
  9921. if (tg3_flag(tp, PAUSE_AUTONEG)) {
  9922. if (tp->link_config.flowctrl & FLOW_CTRL_RX) {
  9923. if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
  9924. cmd->advertising |= ADVERTISED_Pause;
  9925. } else {
  9926. cmd->advertising |= ADVERTISED_Pause |
  9927. ADVERTISED_Asym_Pause;
  9928. }
  9929. } else if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
  9930. cmd->advertising |= ADVERTISED_Asym_Pause;
  9931. }
  9932. }
  9933. if (netif_running(dev) && tp->link_up) {
  9934. ethtool_cmd_speed_set(cmd, tp->link_config.active_speed);
  9935. cmd->duplex = tp->link_config.active_duplex;
  9936. cmd->lp_advertising = tp->link_config.rmt_adv;
  9937. if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
  9938. if (tp->phy_flags & TG3_PHYFLG_MDIX_STATE)
  9939. cmd->eth_tp_mdix = ETH_TP_MDI_X;
  9940. else
  9941. cmd->eth_tp_mdix = ETH_TP_MDI;
  9942. }
  9943. } else {
  9944. ethtool_cmd_speed_set(cmd, SPEED_UNKNOWN);
  9945. cmd->duplex = DUPLEX_UNKNOWN;
  9946. cmd->eth_tp_mdix = ETH_TP_MDI_INVALID;
  9947. }
  9948. cmd->phy_address = tp->phy_addr;
  9949. cmd->transceiver = XCVR_INTERNAL;
  9950. cmd->autoneg = tp->link_config.autoneg;
  9951. cmd->maxtxpkt = 0;
  9952. cmd->maxrxpkt = 0;
  9953. return 0;
  9954. }
  9955. static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  9956. {
  9957. struct tg3 *tp = netdev_priv(dev);
  9958. u32 speed = ethtool_cmd_speed(cmd);
  9959. if (tg3_flag(tp, USE_PHYLIB)) {
  9960. struct phy_device *phydev;
  9961. if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
  9962. return -EAGAIN;
  9963. phydev = tp->mdio_bus->phy_map[tp->phy_addr];
  9964. return phy_ethtool_sset(phydev, cmd);
  9965. }
  9966. if (cmd->autoneg != AUTONEG_ENABLE &&
  9967. cmd->autoneg != AUTONEG_DISABLE)
  9968. return -EINVAL;
  9969. if (cmd->autoneg == AUTONEG_DISABLE &&
  9970. cmd->duplex != DUPLEX_FULL &&
  9971. cmd->duplex != DUPLEX_HALF)
  9972. return -EINVAL;
  9973. if (cmd->autoneg == AUTONEG_ENABLE) {
  9974. u32 mask = ADVERTISED_Autoneg |
  9975. ADVERTISED_Pause |
  9976. ADVERTISED_Asym_Pause;
  9977. if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
  9978. mask |= ADVERTISED_1000baseT_Half |
  9979. ADVERTISED_1000baseT_Full;
  9980. if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
  9981. mask |= ADVERTISED_100baseT_Half |
  9982. ADVERTISED_100baseT_Full |
  9983. ADVERTISED_10baseT_Half |
  9984. ADVERTISED_10baseT_Full |
  9985. ADVERTISED_TP;
  9986. else
  9987. mask |= ADVERTISED_FIBRE;
  9988. if (cmd->advertising & ~mask)
  9989. return -EINVAL;
  9990. mask &= (ADVERTISED_1000baseT_Half |
  9991. ADVERTISED_1000baseT_Full |
  9992. ADVERTISED_100baseT_Half |
  9993. ADVERTISED_100baseT_Full |
  9994. ADVERTISED_10baseT_Half |
  9995. ADVERTISED_10baseT_Full);
  9996. cmd->advertising &= mask;
  9997. } else {
  9998. if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
  9999. if (speed != SPEED_1000)
  10000. return -EINVAL;
  10001. if (cmd->duplex != DUPLEX_FULL)
  10002. return -EINVAL;
  10003. } else {
  10004. if (speed != SPEED_100 &&
  10005. speed != SPEED_10)
  10006. return -EINVAL;
  10007. }
  10008. }
  10009. tg3_full_lock(tp, 0);
  10010. tp->link_config.autoneg = cmd->autoneg;
  10011. if (cmd->autoneg == AUTONEG_ENABLE) {
  10012. tp->link_config.advertising = (cmd->advertising |
  10013. ADVERTISED_Autoneg);
  10014. tp->link_config.speed = SPEED_UNKNOWN;
  10015. tp->link_config.duplex = DUPLEX_UNKNOWN;
  10016. } else {
  10017. tp->link_config.advertising = 0;
  10018. tp->link_config.speed = speed;
  10019. tp->link_config.duplex = cmd->duplex;
  10020. }
  10021. tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
  10022. tg3_warn_mgmt_link_flap(tp);
  10023. if (netif_running(dev))
  10024. tg3_setup_phy(tp, true);
  10025. tg3_full_unlock(tp);
  10026. return 0;
  10027. }
  10028. static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  10029. {
  10030. struct tg3 *tp = netdev_priv(dev);
  10031. strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
  10032. strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
  10033. strlcpy(info->fw_version, tp->fw_ver, sizeof(info->fw_version));
  10034. strlcpy(info->bus_info, pci_name(tp->pdev), sizeof(info->bus_info));
  10035. }
  10036. static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  10037. {
  10038. struct tg3 *tp = netdev_priv(dev);
  10039. if (tg3_flag(tp, WOL_CAP) && device_can_wakeup(&tp->pdev->dev))
  10040. wol->supported = WAKE_MAGIC;
  10041. else
  10042. wol->supported = 0;
  10043. wol->wolopts = 0;
  10044. if (tg3_flag(tp, WOL_ENABLE) && device_can_wakeup(&tp->pdev->dev))
  10045. wol->wolopts = WAKE_MAGIC;
  10046. memset(&wol->sopass, 0, sizeof(wol->sopass));
  10047. }
  10048. static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  10049. {
  10050. struct tg3 *tp = netdev_priv(dev);
  10051. struct device *dp = &tp->pdev->dev;
  10052. if (wol->wolopts & ~WAKE_MAGIC)
  10053. return -EINVAL;
  10054. if ((wol->wolopts & WAKE_MAGIC) &&
  10055. !(tg3_flag(tp, WOL_CAP) && device_can_wakeup(dp)))
  10056. return -EINVAL;
  10057. device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
  10058. if (device_may_wakeup(dp))
  10059. tg3_flag_set(tp, WOL_ENABLE);
  10060. else
  10061. tg3_flag_clear(tp, WOL_ENABLE);
  10062. return 0;
  10063. }
  10064. static u32 tg3_get_msglevel(struct net_device *dev)
  10065. {
  10066. struct tg3 *tp = netdev_priv(dev);
  10067. return tp->msg_enable;
  10068. }
  10069. static void tg3_set_msglevel(struct net_device *dev, u32 value)
  10070. {
  10071. struct tg3 *tp = netdev_priv(dev);
  10072. tp->msg_enable = value;
  10073. }
  10074. static int tg3_nway_reset(struct net_device *dev)
  10075. {
  10076. struct tg3 *tp = netdev_priv(dev);
  10077. int r;
  10078. if (!netif_running(dev))
  10079. return -EAGAIN;
  10080. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
  10081. return -EINVAL;
  10082. tg3_warn_mgmt_link_flap(tp);
  10083. if (tg3_flag(tp, USE_PHYLIB)) {
  10084. if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
  10085. return -EAGAIN;
  10086. r = phy_start_aneg(tp->mdio_bus->phy_map[tp->phy_addr]);
  10087. } else {
  10088. u32 bmcr;
  10089. spin_lock_bh(&tp->lock);
  10090. r = -EINVAL;
  10091. tg3_readphy(tp, MII_BMCR, &bmcr);
  10092. if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
  10093. ((bmcr & BMCR_ANENABLE) ||
  10094. (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
  10095. tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
  10096. BMCR_ANENABLE);
  10097. r = 0;
  10098. }
  10099. spin_unlock_bh(&tp->lock);
  10100. }
  10101. return r;
  10102. }
  10103. static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
  10104. {
  10105. struct tg3 *tp = netdev_priv(dev);
  10106. ering->rx_max_pending = tp->rx_std_ring_mask;
  10107. if (tg3_flag(tp, JUMBO_RING_ENABLE))
  10108. ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
  10109. else
  10110. ering->rx_jumbo_max_pending = 0;
  10111. ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
  10112. ering->rx_pending = tp->rx_pending;
  10113. if (tg3_flag(tp, JUMBO_RING_ENABLE))
  10114. ering->rx_jumbo_pending = tp->rx_jumbo_pending;
  10115. else
  10116. ering->rx_jumbo_pending = 0;
  10117. ering->tx_pending = tp->napi[0].tx_pending;
  10118. }
  10119. static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
  10120. {
  10121. struct tg3 *tp = netdev_priv(dev);
  10122. int i, irq_sync = 0, err = 0;
  10123. if ((ering->rx_pending > tp->rx_std_ring_mask) ||
  10124. (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
  10125. (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
  10126. (ering->tx_pending <= MAX_SKB_FRAGS) ||
  10127. (tg3_flag(tp, TSO_BUG) &&
  10128. (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
  10129. return -EINVAL;
  10130. if (netif_running(dev)) {
  10131. tg3_phy_stop(tp);
  10132. tg3_netif_stop(tp);
  10133. irq_sync = 1;
  10134. }
  10135. tg3_full_lock(tp, irq_sync);
  10136. tp->rx_pending = ering->rx_pending;
  10137. if (tg3_flag(tp, MAX_RXPEND_64) &&
  10138. tp->rx_pending > 63)
  10139. tp->rx_pending = 63;
  10140. if (tg3_flag(tp, JUMBO_RING_ENABLE))
  10141. tp->rx_jumbo_pending = ering->rx_jumbo_pending;
  10142. for (i = 0; i < tp->irq_max; i++)
  10143. tp->napi[i].tx_pending = ering->tx_pending;
  10144. if (netif_running(dev)) {
  10145. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  10146. err = tg3_restart_hw(tp, false);
  10147. if (!err)
  10148. tg3_netif_start(tp);
  10149. }
  10150. tg3_full_unlock(tp);
  10151. if (irq_sync && !err)
  10152. tg3_phy_start(tp);
  10153. return err;
  10154. }
  10155. static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
  10156. {
  10157. struct tg3 *tp = netdev_priv(dev);
  10158. epause->autoneg = !!tg3_flag(tp, PAUSE_AUTONEG);
  10159. if (tp->link_config.flowctrl & FLOW_CTRL_RX)
  10160. epause->rx_pause = 1;
  10161. else
  10162. epause->rx_pause = 0;
  10163. if (tp->link_config.flowctrl & FLOW_CTRL_TX)
  10164. epause->tx_pause = 1;
  10165. else
  10166. epause->tx_pause = 0;
  10167. }
  10168. static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
  10169. {
  10170. struct tg3 *tp = netdev_priv(dev);
  10171. int err = 0;
  10172. if (tp->link_config.autoneg == AUTONEG_ENABLE)
  10173. tg3_warn_mgmt_link_flap(tp);
  10174. if (tg3_flag(tp, USE_PHYLIB)) {
  10175. u32 newadv;
  10176. struct phy_device *phydev;
  10177. phydev = tp->mdio_bus->phy_map[tp->phy_addr];
  10178. if (!(phydev->supported & SUPPORTED_Pause) ||
  10179. (!(phydev->supported & SUPPORTED_Asym_Pause) &&
  10180. (epause->rx_pause != epause->tx_pause)))
  10181. return -EINVAL;
  10182. tp->link_config.flowctrl = 0;
  10183. if (epause->rx_pause) {
  10184. tp->link_config.flowctrl |= FLOW_CTRL_RX;
  10185. if (epause->tx_pause) {
  10186. tp->link_config.flowctrl |= FLOW_CTRL_TX;
  10187. newadv = ADVERTISED_Pause;
  10188. } else
  10189. newadv = ADVERTISED_Pause |
  10190. ADVERTISED_Asym_Pause;
  10191. } else if (epause->tx_pause) {
  10192. tp->link_config.flowctrl |= FLOW_CTRL_TX;
  10193. newadv = ADVERTISED_Asym_Pause;
  10194. } else
  10195. newadv = 0;
  10196. if (epause->autoneg)
  10197. tg3_flag_set(tp, PAUSE_AUTONEG);
  10198. else
  10199. tg3_flag_clear(tp, PAUSE_AUTONEG);
  10200. if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
  10201. u32 oldadv = phydev->advertising &
  10202. (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
  10203. if (oldadv != newadv) {
  10204. phydev->advertising &=
  10205. ~(ADVERTISED_Pause |
  10206. ADVERTISED_Asym_Pause);
  10207. phydev->advertising |= newadv;
  10208. if (phydev->autoneg) {
  10209. /*
  10210. * Always renegotiate the link to
  10211. * inform our link partner of our
  10212. * flow control settings, even if the
  10213. * flow control is forced. Let
  10214. * tg3_adjust_link() do the final
  10215. * flow control setup.
  10216. */
  10217. return phy_start_aneg(phydev);
  10218. }
  10219. }
  10220. if (!epause->autoneg)
  10221. tg3_setup_flow_control(tp, 0, 0);
  10222. } else {
  10223. tp->link_config.advertising &=
  10224. ~(ADVERTISED_Pause |
  10225. ADVERTISED_Asym_Pause);
  10226. tp->link_config.advertising |= newadv;
  10227. }
  10228. } else {
  10229. int irq_sync = 0;
  10230. if (netif_running(dev)) {
  10231. tg3_netif_stop(tp);
  10232. irq_sync = 1;
  10233. }
  10234. tg3_full_lock(tp, irq_sync);
  10235. if (epause->autoneg)
  10236. tg3_flag_set(tp, PAUSE_AUTONEG);
  10237. else
  10238. tg3_flag_clear(tp, PAUSE_AUTONEG);
  10239. if (epause->rx_pause)
  10240. tp->link_config.flowctrl |= FLOW_CTRL_RX;
  10241. else
  10242. tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
  10243. if (epause->tx_pause)
  10244. tp->link_config.flowctrl |= FLOW_CTRL_TX;
  10245. else
  10246. tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
  10247. if (netif_running(dev)) {
  10248. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  10249. err = tg3_restart_hw(tp, false);
  10250. if (!err)
  10251. tg3_netif_start(tp);
  10252. }
  10253. tg3_full_unlock(tp);
  10254. }
  10255. tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
  10256. return err;
  10257. }
  10258. static int tg3_get_sset_count(struct net_device *dev, int sset)
  10259. {
  10260. switch (sset) {
  10261. case ETH_SS_TEST:
  10262. return TG3_NUM_TEST;
  10263. case ETH_SS_STATS:
  10264. return TG3_NUM_STATS;
  10265. default:
  10266. return -EOPNOTSUPP;
  10267. }
  10268. }
  10269. static int tg3_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
  10270. u32 *rules __always_unused)
  10271. {
  10272. struct tg3 *tp = netdev_priv(dev);
  10273. if (!tg3_flag(tp, SUPPORT_MSIX))
  10274. return -EOPNOTSUPP;
  10275. switch (info->cmd) {
  10276. case ETHTOOL_GRXRINGS:
  10277. if (netif_running(tp->dev))
  10278. info->data = tp->rxq_cnt;
  10279. else {
  10280. info->data = num_online_cpus();
  10281. if (info->data > TG3_RSS_MAX_NUM_QS)
  10282. info->data = TG3_RSS_MAX_NUM_QS;
  10283. }
  10284. /* The first interrupt vector only
  10285. * handles link interrupts.
  10286. */
  10287. info->data -= 1;
  10288. return 0;
  10289. default:
  10290. return -EOPNOTSUPP;
  10291. }
  10292. }
  10293. static u32 tg3_get_rxfh_indir_size(struct net_device *dev)
  10294. {
  10295. u32 size = 0;
  10296. struct tg3 *tp = netdev_priv(dev);
  10297. if (tg3_flag(tp, SUPPORT_MSIX))
  10298. size = TG3_RSS_INDIR_TBL_SIZE;
  10299. return size;
  10300. }
  10301. static int tg3_get_rxfh(struct net_device *dev, u32 *indir, u8 *key, u8 *hfunc)
  10302. {
  10303. struct tg3 *tp = netdev_priv(dev);
  10304. int i;
  10305. if (hfunc)
  10306. *hfunc = ETH_RSS_HASH_TOP;
  10307. if (!indir)
  10308. return 0;
  10309. for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
  10310. indir[i] = tp->rss_ind_tbl[i];
  10311. return 0;
  10312. }
  10313. static int tg3_set_rxfh(struct net_device *dev, const u32 *indir, const u8 *key,
  10314. const u8 hfunc)
  10315. {
  10316. struct tg3 *tp = netdev_priv(dev);
  10317. size_t i;
  10318. /* We require at least one supported parameter to be changed and no
  10319. * change in any of the unsupported parameters
  10320. */
  10321. if (key ||
  10322. (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP))
  10323. return -EOPNOTSUPP;
  10324. if (!indir)
  10325. return 0;
  10326. for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
  10327. tp->rss_ind_tbl[i] = indir[i];
  10328. if (!netif_running(dev) || !tg3_flag(tp, ENABLE_RSS))
  10329. return 0;
  10330. /* It is legal to write the indirection
  10331. * table while the device is running.
  10332. */
  10333. tg3_full_lock(tp, 0);
  10334. tg3_rss_write_indir_tbl(tp);
  10335. tg3_full_unlock(tp);
  10336. return 0;
  10337. }
  10338. static void tg3_get_channels(struct net_device *dev,
  10339. struct ethtool_channels *channel)
  10340. {
  10341. struct tg3 *tp = netdev_priv(dev);
  10342. u32 deflt_qs = netif_get_num_default_rss_queues();
  10343. channel->max_rx = tp->rxq_max;
  10344. channel->max_tx = tp->txq_max;
  10345. if (netif_running(dev)) {
  10346. channel->rx_count = tp->rxq_cnt;
  10347. channel->tx_count = tp->txq_cnt;
  10348. } else {
  10349. if (tp->rxq_req)
  10350. channel->rx_count = tp->rxq_req;
  10351. else
  10352. channel->rx_count = min(deflt_qs, tp->rxq_max);
  10353. if (tp->txq_req)
  10354. channel->tx_count = tp->txq_req;
  10355. else
  10356. channel->tx_count = min(deflt_qs, tp->txq_max);
  10357. }
  10358. }
  10359. static int tg3_set_channels(struct net_device *dev,
  10360. struct ethtool_channels *channel)
  10361. {
  10362. struct tg3 *tp = netdev_priv(dev);
  10363. if (!tg3_flag(tp, SUPPORT_MSIX))
  10364. return -EOPNOTSUPP;
  10365. if (channel->rx_count > tp->rxq_max ||
  10366. channel->tx_count > tp->txq_max)
  10367. return -EINVAL;
  10368. tp->rxq_req = channel->rx_count;
  10369. tp->txq_req = channel->tx_count;
  10370. if (!netif_running(dev))
  10371. return 0;
  10372. tg3_stop(tp);
  10373. tg3_carrier_off(tp);
  10374. tg3_start(tp, true, false, false);
  10375. return 0;
  10376. }
  10377. static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
  10378. {
  10379. switch (stringset) {
  10380. case ETH_SS_STATS:
  10381. memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
  10382. break;
  10383. case ETH_SS_TEST:
  10384. memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
  10385. break;
  10386. default:
  10387. WARN_ON(1); /* we need a WARN() */
  10388. break;
  10389. }
  10390. }
  10391. static int tg3_set_phys_id(struct net_device *dev,
  10392. enum ethtool_phys_id_state state)
  10393. {
  10394. struct tg3 *tp = netdev_priv(dev);
  10395. if (!netif_running(tp->dev))
  10396. return -EAGAIN;
  10397. switch (state) {
  10398. case ETHTOOL_ID_ACTIVE:
  10399. return 1; /* cycle on/off once per second */
  10400. case ETHTOOL_ID_ON:
  10401. tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
  10402. LED_CTRL_1000MBPS_ON |
  10403. LED_CTRL_100MBPS_ON |
  10404. LED_CTRL_10MBPS_ON |
  10405. LED_CTRL_TRAFFIC_OVERRIDE |
  10406. LED_CTRL_TRAFFIC_BLINK |
  10407. LED_CTRL_TRAFFIC_LED);
  10408. break;
  10409. case ETHTOOL_ID_OFF:
  10410. tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
  10411. LED_CTRL_TRAFFIC_OVERRIDE);
  10412. break;
  10413. case ETHTOOL_ID_INACTIVE:
  10414. tw32(MAC_LED_CTRL, tp->led_ctrl);
  10415. break;
  10416. }
  10417. return 0;
  10418. }
  10419. static void tg3_get_ethtool_stats(struct net_device *dev,
  10420. struct ethtool_stats *estats, u64 *tmp_stats)
  10421. {
  10422. struct tg3 *tp = netdev_priv(dev);
  10423. if (tp->hw_stats)
  10424. tg3_get_estats(tp, (struct tg3_ethtool_stats *)tmp_stats);
  10425. else
  10426. memset(tmp_stats, 0, sizeof(struct tg3_ethtool_stats));
  10427. }
  10428. static __be32 *tg3_vpd_readblock(struct tg3 *tp, u32 *vpdlen)
  10429. {
  10430. int i;
  10431. __be32 *buf;
  10432. u32 offset = 0, len = 0;
  10433. u32 magic, val;
  10434. if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &magic))
  10435. return NULL;
  10436. if (magic == TG3_EEPROM_MAGIC) {
  10437. for (offset = TG3_NVM_DIR_START;
  10438. offset < TG3_NVM_DIR_END;
  10439. offset += TG3_NVM_DIRENT_SIZE) {
  10440. if (tg3_nvram_read(tp, offset, &val))
  10441. return NULL;
  10442. if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
  10443. TG3_NVM_DIRTYPE_EXTVPD)
  10444. break;
  10445. }
  10446. if (offset != TG3_NVM_DIR_END) {
  10447. len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
  10448. if (tg3_nvram_read(tp, offset + 4, &offset))
  10449. return NULL;
  10450. offset = tg3_nvram_logical_addr(tp, offset);
  10451. }
  10452. }
  10453. if (!offset || !len) {
  10454. offset = TG3_NVM_VPD_OFF;
  10455. len = TG3_NVM_VPD_LEN;
  10456. }
  10457. buf = kmalloc(len, GFP_KERNEL);
  10458. if (buf == NULL)
  10459. return NULL;
  10460. if (magic == TG3_EEPROM_MAGIC) {
  10461. for (i = 0; i < len; i += 4) {
  10462. /* The data is in little-endian format in NVRAM.
  10463. * Use the big-endian read routines to preserve
  10464. * the byte order as it exists in NVRAM.
  10465. */
  10466. if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
  10467. goto error;
  10468. }
  10469. } else {
  10470. u8 *ptr;
  10471. ssize_t cnt;
  10472. unsigned int pos = 0;
  10473. ptr = (u8 *)&buf[0];
  10474. for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
  10475. cnt = pci_read_vpd(tp->pdev, pos,
  10476. len - pos, ptr);
  10477. if (cnt == -ETIMEDOUT || cnt == -EINTR)
  10478. cnt = 0;
  10479. else if (cnt < 0)
  10480. goto error;
  10481. }
  10482. if (pos != len)
  10483. goto error;
  10484. }
  10485. *vpdlen = len;
  10486. return buf;
  10487. error:
  10488. kfree(buf);
  10489. return NULL;
  10490. }
  10491. #define NVRAM_TEST_SIZE 0x100
  10492. #define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
  10493. #define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
  10494. #define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
  10495. #define NVRAM_SELFBOOT_FORMAT1_4_SIZE 0x20
  10496. #define NVRAM_SELFBOOT_FORMAT1_5_SIZE 0x24
  10497. #define NVRAM_SELFBOOT_FORMAT1_6_SIZE 0x50
  10498. #define NVRAM_SELFBOOT_HW_SIZE 0x20
  10499. #define NVRAM_SELFBOOT_DATA_SIZE 0x1c
  10500. static int tg3_test_nvram(struct tg3 *tp)
  10501. {
  10502. u32 csum, magic, len;
  10503. __be32 *buf;
  10504. int i, j, k, err = 0, size;
  10505. if (tg3_flag(tp, NO_NVRAM))
  10506. return 0;
  10507. if (tg3_nvram_read(tp, 0, &magic) != 0)
  10508. return -EIO;
  10509. if (magic == TG3_EEPROM_MAGIC)
  10510. size = NVRAM_TEST_SIZE;
  10511. else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
  10512. if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
  10513. TG3_EEPROM_SB_FORMAT_1) {
  10514. switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
  10515. case TG3_EEPROM_SB_REVISION_0:
  10516. size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
  10517. break;
  10518. case TG3_EEPROM_SB_REVISION_2:
  10519. size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
  10520. break;
  10521. case TG3_EEPROM_SB_REVISION_3:
  10522. size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
  10523. break;
  10524. case TG3_EEPROM_SB_REVISION_4:
  10525. size = NVRAM_SELFBOOT_FORMAT1_4_SIZE;
  10526. break;
  10527. case TG3_EEPROM_SB_REVISION_5:
  10528. size = NVRAM_SELFBOOT_FORMAT1_5_SIZE;
  10529. break;
  10530. case TG3_EEPROM_SB_REVISION_6:
  10531. size = NVRAM_SELFBOOT_FORMAT1_6_SIZE;
  10532. break;
  10533. default:
  10534. return -EIO;
  10535. }
  10536. } else
  10537. return 0;
  10538. } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
  10539. size = NVRAM_SELFBOOT_HW_SIZE;
  10540. else
  10541. return -EIO;
  10542. buf = kmalloc(size, GFP_KERNEL);
  10543. if (buf == NULL)
  10544. return -ENOMEM;
  10545. err = -EIO;
  10546. for (i = 0, j = 0; i < size; i += 4, j++) {
  10547. err = tg3_nvram_read_be32(tp, i, &buf[j]);
  10548. if (err)
  10549. break;
  10550. }
  10551. if (i < size)
  10552. goto out;
  10553. /* Selfboot format */
  10554. magic = be32_to_cpu(buf[0]);
  10555. if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
  10556. TG3_EEPROM_MAGIC_FW) {
  10557. u8 *buf8 = (u8 *) buf, csum8 = 0;
  10558. if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
  10559. TG3_EEPROM_SB_REVISION_2) {
  10560. /* For rev 2, the csum doesn't include the MBA. */
  10561. for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
  10562. csum8 += buf8[i];
  10563. for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
  10564. csum8 += buf8[i];
  10565. } else {
  10566. for (i = 0; i < size; i++)
  10567. csum8 += buf8[i];
  10568. }
  10569. if (csum8 == 0) {
  10570. err = 0;
  10571. goto out;
  10572. }
  10573. err = -EIO;
  10574. goto out;
  10575. }
  10576. if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
  10577. TG3_EEPROM_MAGIC_HW) {
  10578. u8 data[NVRAM_SELFBOOT_DATA_SIZE];
  10579. u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
  10580. u8 *buf8 = (u8 *) buf;
  10581. /* Separate the parity bits and the data bytes. */
  10582. for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
  10583. if ((i == 0) || (i == 8)) {
  10584. int l;
  10585. u8 msk;
  10586. for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
  10587. parity[k++] = buf8[i] & msk;
  10588. i++;
  10589. } else if (i == 16) {
  10590. int l;
  10591. u8 msk;
  10592. for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
  10593. parity[k++] = buf8[i] & msk;
  10594. i++;
  10595. for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
  10596. parity[k++] = buf8[i] & msk;
  10597. i++;
  10598. }
  10599. data[j++] = buf8[i];
  10600. }
  10601. err = -EIO;
  10602. for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
  10603. u8 hw8 = hweight8(data[i]);
  10604. if ((hw8 & 0x1) && parity[i])
  10605. goto out;
  10606. else if (!(hw8 & 0x1) && !parity[i])
  10607. goto out;
  10608. }
  10609. err = 0;
  10610. goto out;
  10611. }
  10612. err = -EIO;
  10613. /* Bootstrap checksum at offset 0x10 */
  10614. csum = calc_crc((unsigned char *) buf, 0x10);
  10615. if (csum != le32_to_cpu(buf[0x10/4]))
  10616. goto out;
  10617. /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
  10618. csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
  10619. if (csum != le32_to_cpu(buf[0xfc/4]))
  10620. goto out;
  10621. kfree(buf);
  10622. buf = tg3_vpd_readblock(tp, &len);
  10623. if (!buf)
  10624. return -ENOMEM;
  10625. i = pci_vpd_find_tag((u8 *)buf, 0, len, PCI_VPD_LRDT_RO_DATA);
  10626. if (i > 0) {
  10627. j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
  10628. if (j < 0)
  10629. goto out;
  10630. if (i + PCI_VPD_LRDT_TAG_SIZE + j > len)
  10631. goto out;
  10632. i += PCI_VPD_LRDT_TAG_SIZE;
  10633. j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
  10634. PCI_VPD_RO_KEYWORD_CHKSUM);
  10635. if (j > 0) {
  10636. u8 csum8 = 0;
  10637. j += PCI_VPD_INFO_FLD_HDR_SIZE;
  10638. for (i = 0; i <= j; i++)
  10639. csum8 += ((u8 *)buf)[i];
  10640. if (csum8)
  10641. goto out;
  10642. }
  10643. }
  10644. err = 0;
  10645. out:
  10646. kfree(buf);
  10647. return err;
  10648. }
  10649. #define TG3_SERDES_TIMEOUT_SEC 2
  10650. #define TG3_COPPER_TIMEOUT_SEC 6
  10651. static int tg3_test_link(struct tg3 *tp)
  10652. {
  10653. int i, max;
  10654. if (!netif_running(tp->dev))
  10655. return -ENODEV;
  10656. if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
  10657. max = TG3_SERDES_TIMEOUT_SEC;
  10658. else
  10659. max = TG3_COPPER_TIMEOUT_SEC;
  10660. for (i = 0; i < max; i++) {
  10661. if (tp->link_up)
  10662. return 0;
  10663. if (msleep_interruptible(1000))
  10664. break;
  10665. }
  10666. return -EIO;
  10667. }
  10668. /* Only test the commonly used registers */
  10669. static int tg3_test_registers(struct tg3 *tp)
  10670. {
  10671. int i, is_5705, is_5750;
  10672. u32 offset, read_mask, write_mask, val, save_val, read_val;
  10673. static struct {
  10674. u16 offset;
  10675. u16 flags;
  10676. #define TG3_FL_5705 0x1
  10677. #define TG3_FL_NOT_5705 0x2
  10678. #define TG3_FL_NOT_5788 0x4
  10679. #define TG3_FL_NOT_5750 0x8
  10680. u32 read_mask;
  10681. u32 write_mask;
  10682. } reg_tbl[] = {
  10683. /* MAC Control Registers */
  10684. { MAC_MODE, TG3_FL_NOT_5705,
  10685. 0x00000000, 0x00ef6f8c },
  10686. { MAC_MODE, TG3_FL_5705,
  10687. 0x00000000, 0x01ef6b8c },
  10688. { MAC_STATUS, TG3_FL_NOT_5705,
  10689. 0x03800107, 0x00000000 },
  10690. { MAC_STATUS, TG3_FL_5705,
  10691. 0x03800100, 0x00000000 },
  10692. { MAC_ADDR_0_HIGH, 0x0000,
  10693. 0x00000000, 0x0000ffff },
  10694. { MAC_ADDR_0_LOW, 0x0000,
  10695. 0x00000000, 0xffffffff },
  10696. { MAC_RX_MTU_SIZE, 0x0000,
  10697. 0x00000000, 0x0000ffff },
  10698. { MAC_TX_MODE, 0x0000,
  10699. 0x00000000, 0x00000070 },
  10700. { MAC_TX_LENGTHS, 0x0000,
  10701. 0x00000000, 0x00003fff },
  10702. { MAC_RX_MODE, TG3_FL_NOT_5705,
  10703. 0x00000000, 0x000007fc },
  10704. { MAC_RX_MODE, TG3_FL_5705,
  10705. 0x00000000, 0x000007dc },
  10706. { MAC_HASH_REG_0, 0x0000,
  10707. 0x00000000, 0xffffffff },
  10708. { MAC_HASH_REG_1, 0x0000,
  10709. 0x00000000, 0xffffffff },
  10710. { MAC_HASH_REG_2, 0x0000,
  10711. 0x00000000, 0xffffffff },
  10712. { MAC_HASH_REG_3, 0x0000,
  10713. 0x00000000, 0xffffffff },
  10714. /* Receive Data and Receive BD Initiator Control Registers. */
  10715. { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
  10716. 0x00000000, 0xffffffff },
  10717. { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
  10718. 0x00000000, 0xffffffff },
  10719. { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
  10720. 0x00000000, 0x00000003 },
  10721. { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
  10722. 0x00000000, 0xffffffff },
  10723. { RCVDBDI_STD_BD+0, 0x0000,
  10724. 0x00000000, 0xffffffff },
  10725. { RCVDBDI_STD_BD+4, 0x0000,
  10726. 0x00000000, 0xffffffff },
  10727. { RCVDBDI_STD_BD+8, 0x0000,
  10728. 0x00000000, 0xffff0002 },
  10729. { RCVDBDI_STD_BD+0xc, 0x0000,
  10730. 0x00000000, 0xffffffff },
  10731. /* Receive BD Initiator Control Registers. */
  10732. { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
  10733. 0x00000000, 0xffffffff },
  10734. { RCVBDI_STD_THRESH, TG3_FL_5705,
  10735. 0x00000000, 0x000003ff },
  10736. { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
  10737. 0x00000000, 0xffffffff },
  10738. /* Host Coalescing Control Registers. */
  10739. { HOSTCC_MODE, TG3_FL_NOT_5705,
  10740. 0x00000000, 0x00000004 },
  10741. { HOSTCC_MODE, TG3_FL_5705,
  10742. 0x00000000, 0x000000f6 },
  10743. { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
  10744. 0x00000000, 0xffffffff },
  10745. { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
  10746. 0x00000000, 0x000003ff },
  10747. { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
  10748. 0x00000000, 0xffffffff },
  10749. { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
  10750. 0x00000000, 0x000003ff },
  10751. { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
  10752. 0x00000000, 0xffffffff },
  10753. { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
  10754. 0x00000000, 0x000000ff },
  10755. { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
  10756. 0x00000000, 0xffffffff },
  10757. { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
  10758. 0x00000000, 0x000000ff },
  10759. { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
  10760. 0x00000000, 0xffffffff },
  10761. { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
  10762. 0x00000000, 0xffffffff },
  10763. { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
  10764. 0x00000000, 0xffffffff },
  10765. { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
  10766. 0x00000000, 0x000000ff },
  10767. { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
  10768. 0x00000000, 0xffffffff },
  10769. { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
  10770. 0x00000000, 0x000000ff },
  10771. { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
  10772. 0x00000000, 0xffffffff },
  10773. { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
  10774. 0x00000000, 0xffffffff },
  10775. { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
  10776. 0x00000000, 0xffffffff },
  10777. { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
  10778. 0x00000000, 0xffffffff },
  10779. { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
  10780. 0x00000000, 0xffffffff },
  10781. { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
  10782. 0xffffffff, 0x00000000 },
  10783. { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
  10784. 0xffffffff, 0x00000000 },
  10785. /* Buffer Manager Control Registers. */
  10786. { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
  10787. 0x00000000, 0x007fff80 },
  10788. { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
  10789. 0x00000000, 0x007fffff },
  10790. { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
  10791. 0x00000000, 0x0000003f },
  10792. { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
  10793. 0x00000000, 0x000001ff },
  10794. { BUFMGR_MB_HIGH_WATER, 0x0000,
  10795. 0x00000000, 0x000001ff },
  10796. { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
  10797. 0xffffffff, 0x00000000 },
  10798. { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
  10799. 0xffffffff, 0x00000000 },
  10800. /* Mailbox Registers */
  10801. { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
  10802. 0x00000000, 0x000001ff },
  10803. { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
  10804. 0x00000000, 0x000001ff },
  10805. { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
  10806. 0x00000000, 0x000007ff },
  10807. { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
  10808. 0x00000000, 0x000001ff },
  10809. { 0xffff, 0x0000, 0x00000000, 0x00000000 },
  10810. };
  10811. is_5705 = is_5750 = 0;
  10812. if (tg3_flag(tp, 5705_PLUS)) {
  10813. is_5705 = 1;
  10814. if (tg3_flag(tp, 5750_PLUS))
  10815. is_5750 = 1;
  10816. }
  10817. for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
  10818. if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
  10819. continue;
  10820. if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
  10821. continue;
  10822. if (tg3_flag(tp, IS_5788) &&
  10823. (reg_tbl[i].flags & TG3_FL_NOT_5788))
  10824. continue;
  10825. if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
  10826. continue;
  10827. offset = (u32) reg_tbl[i].offset;
  10828. read_mask = reg_tbl[i].read_mask;
  10829. write_mask = reg_tbl[i].write_mask;
  10830. /* Save the original register content */
  10831. save_val = tr32(offset);
  10832. /* Determine the read-only value. */
  10833. read_val = save_val & read_mask;
  10834. /* Write zero to the register, then make sure the read-only bits
  10835. * are not changed and the read/write bits are all zeros.
  10836. */
  10837. tw32(offset, 0);
  10838. val = tr32(offset);
  10839. /* Test the read-only and read/write bits. */
  10840. if (((val & read_mask) != read_val) || (val & write_mask))
  10841. goto out;
  10842. /* Write ones to all the bits defined by RdMask and WrMask, then
  10843. * make sure the read-only bits are not changed and the
  10844. * read/write bits are all ones.
  10845. */
  10846. tw32(offset, read_mask | write_mask);
  10847. val = tr32(offset);
  10848. /* Test the read-only bits. */
  10849. if ((val & read_mask) != read_val)
  10850. goto out;
  10851. /* Test the read/write bits. */
  10852. if ((val & write_mask) != write_mask)
  10853. goto out;
  10854. tw32(offset, save_val);
  10855. }
  10856. return 0;
  10857. out:
  10858. if (netif_msg_hw(tp))
  10859. netdev_err(tp->dev,
  10860. "Register test failed at offset %x\n", offset);
  10861. tw32(offset, save_val);
  10862. return -EIO;
  10863. }
  10864. static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
  10865. {
  10866. static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
  10867. int i;
  10868. u32 j;
  10869. for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
  10870. for (j = 0; j < len; j += 4) {
  10871. u32 val;
  10872. tg3_write_mem(tp, offset + j, test_pattern[i]);
  10873. tg3_read_mem(tp, offset + j, &val);
  10874. if (val != test_pattern[i])
  10875. return -EIO;
  10876. }
  10877. }
  10878. return 0;
  10879. }
  10880. static int tg3_test_memory(struct tg3 *tp)
  10881. {
  10882. static struct mem_entry {
  10883. u32 offset;
  10884. u32 len;
  10885. } mem_tbl_570x[] = {
  10886. { 0x00000000, 0x00b50},
  10887. { 0x00002000, 0x1c000},
  10888. { 0xffffffff, 0x00000}
  10889. }, mem_tbl_5705[] = {
  10890. { 0x00000100, 0x0000c},
  10891. { 0x00000200, 0x00008},
  10892. { 0x00004000, 0x00800},
  10893. { 0x00006000, 0x01000},
  10894. { 0x00008000, 0x02000},
  10895. { 0x00010000, 0x0e000},
  10896. { 0xffffffff, 0x00000}
  10897. }, mem_tbl_5755[] = {
  10898. { 0x00000200, 0x00008},
  10899. { 0x00004000, 0x00800},
  10900. { 0x00006000, 0x00800},
  10901. { 0x00008000, 0x02000},
  10902. { 0x00010000, 0x0c000},
  10903. { 0xffffffff, 0x00000}
  10904. }, mem_tbl_5906[] = {
  10905. { 0x00000200, 0x00008},
  10906. { 0x00004000, 0x00400},
  10907. { 0x00006000, 0x00400},
  10908. { 0x00008000, 0x01000},
  10909. { 0x00010000, 0x01000},
  10910. { 0xffffffff, 0x00000}
  10911. }, mem_tbl_5717[] = {
  10912. { 0x00000200, 0x00008},
  10913. { 0x00010000, 0x0a000},
  10914. { 0x00020000, 0x13c00},
  10915. { 0xffffffff, 0x00000}
  10916. }, mem_tbl_57765[] = {
  10917. { 0x00000200, 0x00008},
  10918. { 0x00004000, 0x00800},
  10919. { 0x00006000, 0x09800},
  10920. { 0x00010000, 0x0a000},
  10921. { 0xffffffff, 0x00000}
  10922. };
  10923. struct mem_entry *mem_tbl;
  10924. int err = 0;
  10925. int i;
  10926. if (tg3_flag(tp, 5717_PLUS))
  10927. mem_tbl = mem_tbl_5717;
  10928. else if (tg3_flag(tp, 57765_CLASS) ||
  10929. tg3_asic_rev(tp) == ASIC_REV_5762)
  10930. mem_tbl = mem_tbl_57765;
  10931. else if (tg3_flag(tp, 5755_PLUS))
  10932. mem_tbl = mem_tbl_5755;
  10933. else if (tg3_asic_rev(tp) == ASIC_REV_5906)
  10934. mem_tbl = mem_tbl_5906;
  10935. else if (tg3_flag(tp, 5705_PLUS))
  10936. mem_tbl = mem_tbl_5705;
  10937. else
  10938. mem_tbl = mem_tbl_570x;
  10939. for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
  10940. err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
  10941. if (err)
  10942. break;
  10943. }
  10944. return err;
  10945. }
  10946. #define TG3_TSO_MSS 500
  10947. #define TG3_TSO_IP_HDR_LEN 20
  10948. #define TG3_TSO_TCP_HDR_LEN 20
  10949. #define TG3_TSO_TCP_OPT_LEN 12
  10950. static const u8 tg3_tso_header[] = {
  10951. 0x08, 0x00,
  10952. 0x45, 0x00, 0x00, 0x00,
  10953. 0x00, 0x00, 0x40, 0x00,
  10954. 0x40, 0x06, 0x00, 0x00,
  10955. 0x0a, 0x00, 0x00, 0x01,
  10956. 0x0a, 0x00, 0x00, 0x02,
  10957. 0x0d, 0x00, 0xe0, 0x00,
  10958. 0x00, 0x00, 0x01, 0x00,
  10959. 0x00, 0x00, 0x02, 0x00,
  10960. 0x80, 0x10, 0x10, 0x00,
  10961. 0x14, 0x09, 0x00, 0x00,
  10962. 0x01, 0x01, 0x08, 0x0a,
  10963. 0x11, 0x11, 0x11, 0x11,
  10964. 0x11, 0x11, 0x11, 0x11,
  10965. };
  10966. static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, bool tso_loopback)
  10967. {
  10968. u32 rx_start_idx, rx_idx, tx_idx, opaque_key;
  10969. u32 base_flags = 0, mss = 0, desc_idx, coal_now, data_off, val;
  10970. u32 budget;
  10971. struct sk_buff *skb;
  10972. u8 *tx_data, *rx_data;
  10973. dma_addr_t map;
  10974. int num_pkts, tx_len, rx_len, i, err;
  10975. struct tg3_rx_buffer_desc *desc;
  10976. struct tg3_napi *tnapi, *rnapi;
  10977. struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
  10978. tnapi = &tp->napi[0];
  10979. rnapi = &tp->napi[0];
  10980. if (tp->irq_cnt > 1) {
  10981. if (tg3_flag(tp, ENABLE_RSS))
  10982. rnapi = &tp->napi[1];
  10983. if (tg3_flag(tp, ENABLE_TSS))
  10984. tnapi = &tp->napi[1];
  10985. }
  10986. coal_now = tnapi->coal_now | rnapi->coal_now;
  10987. err = -EIO;
  10988. tx_len = pktsz;
  10989. skb = netdev_alloc_skb(tp->dev, tx_len);
  10990. if (!skb)
  10991. return -ENOMEM;
  10992. tx_data = skb_put(skb, tx_len);
  10993. memcpy(tx_data, tp->dev->dev_addr, ETH_ALEN);
  10994. memset(tx_data + ETH_ALEN, 0x0, 8);
  10995. tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
  10996. if (tso_loopback) {
  10997. struct iphdr *iph = (struct iphdr *)&tx_data[ETH_HLEN];
  10998. u32 hdr_len = TG3_TSO_IP_HDR_LEN + TG3_TSO_TCP_HDR_LEN +
  10999. TG3_TSO_TCP_OPT_LEN;
  11000. memcpy(tx_data + ETH_ALEN * 2, tg3_tso_header,
  11001. sizeof(tg3_tso_header));
  11002. mss = TG3_TSO_MSS;
  11003. val = tx_len - ETH_ALEN * 2 - sizeof(tg3_tso_header);
  11004. num_pkts = DIV_ROUND_UP(val, TG3_TSO_MSS);
  11005. /* Set the total length field in the IP header */
  11006. iph->tot_len = htons((u16)(mss + hdr_len));
  11007. base_flags = (TXD_FLAG_CPU_PRE_DMA |
  11008. TXD_FLAG_CPU_POST_DMA);
  11009. if (tg3_flag(tp, HW_TSO_1) ||
  11010. tg3_flag(tp, HW_TSO_2) ||
  11011. tg3_flag(tp, HW_TSO_3)) {
  11012. struct tcphdr *th;
  11013. val = ETH_HLEN + TG3_TSO_IP_HDR_LEN;
  11014. th = (struct tcphdr *)&tx_data[val];
  11015. th->check = 0;
  11016. } else
  11017. base_flags |= TXD_FLAG_TCPUDP_CSUM;
  11018. if (tg3_flag(tp, HW_TSO_3)) {
  11019. mss |= (hdr_len & 0xc) << 12;
  11020. if (hdr_len & 0x10)
  11021. base_flags |= 0x00000010;
  11022. base_flags |= (hdr_len & 0x3e0) << 5;
  11023. } else if (tg3_flag(tp, HW_TSO_2))
  11024. mss |= hdr_len << 9;
  11025. else if (tg3_flag(tp, HW_TSO_1) ||
  11026. tg3_asic_rev(tp) == ASIC_REV_5705) {
  11027. mss |= (TG3_TSO_TCP_OPT_LEN << 9);
  11028. } else {
  11029. base_flags |= (TG3_TSO_TCP_OPT_LEN << 10);
  11030. }
  11031. data_off = ETH_ALEN * 2 + sizeof(tg3_tso_header);
  11032. } else {
  11033. num_pkts = 1;
  11034. data_off = ETH_HLEN;
  11035. if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
  11036. tx_len > VLAN_ETH_FRAME_LEN)
  11037. base_flags |= TXD_FLAG_JMB_PKT;
  11038. }
  11039. for (i = data_off; i < tx_len; i++)
  11040. tx_data[i] = (u8) (i & 0xff);
  11041. map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
  11042. if (pci_dma_mapping_error(tp->pdev, map)) {
  11043. dev_kfree_skb(skb);
  11044. return -EIO;
  11045. }
  11046. val = tnapi->tx_prod;
  11047. tnapi->tx_buffers[val].skb = skb;
  11048. dma_unmap_addr_set(&tnapi->tx_buffers[val], mapping, map);
  11049. tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
  11050. rnapi->coal_now);
  11051. udelay(10);
  11052. rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
  11053. budget = tg3_tx_avail(tnapi);
  11054. if (tg3_tx_frag_set(tnapi, &val, &budget, map, tx_len,
  11055. base_flags | TXD_FLAG_END, mss, 0)) {
  11056. tnapi->tx_buffers[val].skb = NULL;
  11057. dev_kfree_skb(skb);
  11058. return -EIO;
  11059. }
  11060. tnapi->tx_prod++;
  11061. /* Sync BD data before updating mailbox */
  11062. wmb();
  11063. tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
  11064. tr32_mailbox(tnapi->prodmbox);
  11065. udelay(10);
  11066. /* 350 usec to allow enough time on some 10/100 Mbps devices. */
  11067. for (i = 0; i < 35; i++) {
  11068. tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
  11069. coal_now);
  11070. udelay(10);
  11071. tx_idx = tnapi->hw_status->idx[0].tx_consumer;
  11072. rx_idx = rnapi->hw_status->idx[0].rx_producer;
  11073. if ((tx_idx == tnapi->tx_prod) &&
  11074. (rx_idx == (rx_start_idx + num_pkts)))
  11075. break;
  11076. }
  11077. tg3_tx_skb_unmap(tnapi, tnapi->tx_prod - 1, -1);
  11078. dev_kfree_skb(skb);
  11079. if (tx_idx != tnapi->tx_prod)
  11080. goto out;
  11081. if (rx_idx != rx_start_idx + num_pkts)
  11082. goto out;
  11083. val = data_off;
  11084. while (rx_idx != rx_start_idx) {
  11085. desc = &rnapi->rx_rcb[rx_start_idx++];
  11086. desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
  11087. opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
  11088. if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
  11089. (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
  11090. goto out;
  11091. rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT)
  11092. - ETH_FCS_LEN;
  11093. if (!tso_loopback) {
  11094. if (rx_len != tx_len)
  11095. goto out;
  11096. if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
  11097. if (opaque_key != RXD_OPAQUE_RING_STD)
  11098. goto out;
  11099. } else {
  11100. if (opaque_key != RXD_OPAQUE_RING_JUMBO)
  11101. goto out;
  11102. }
  11103. } else if ((desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
  11104. (desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
  11105. >> RXD_TCPCSUM_SHIFT != 0xffff) {
  11106. goto out;
  11107. }
  11108. if (opaque_key == RXD_OPAQUE_RING_STD) {
  11109. rx_data = tpr->rx_std_buffers[desc_idx].data;
  11110. map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx],
  11111. mapping);
  11112. } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
  11113. rx_data = tpr->rx_jmb_buffers[desc_idx].data;
  11114. map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx],
  11115. mapping);
  11116. } else
  11117. goto out;
  11118. pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len,
  11119. PCI_DMA_FROMDEVICE);
  11120. rx_data += TG3_RX_OFFSET(tp);
  11121. for (i = data_off; i < rx_len; i++, val++) {
  11122. if (*(rx_data + i) != (u8) (val & 0xff))
  11123. goto out;
  11124. }
  11125. }
  11126. err = 0;
  11127. /* tg3_free_rings will unmap and free the rx_data */
  11128. out:
  11129. return err;
  11130. }
  11131. #define TG3_STD_LOOPBACK_FAILED 1
  11132. #define TG3_JMB_LOOPBACK_FAILED 2
  11133. #define TG3_TSO_LOOPBACK_FAILED 4
  11134. #define TG3_LOOPBACK_FAILED \
  11135. (TG3_STD_LOOPBACK_FAILED | \
  11136. TG3_JMB_LOOPBACK_FAILED | \
  11137. TG3_TSO_LOOPBACK_FAILED)
  11138. static int tg3_test_loopback(struct tg3 *tp, u64 *data, bool do_extlpbk)
  11139. {
  11140. int err = -EIO;
  11141. u32 eee_cap;
  11142. u32 jmb_pkt_sz = 9000;
  11143. if (tp->dma_limit)
  11144. jmb_pkt_sz = tp->dma_limit - ETH_HLEN;
  11145. eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
  11146. tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
  11147. if (!netif_running(tp->dev)) {
  11148. data[TG3_MAC_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
  11149. data[TG3_PHY_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
  11150. if (do_extlpbk)
  11151. data[TG3_EXT_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
  11152. goto done;
  11153. }
  11154. err = tg3_reset_hw(tp, true);
  11155. if (err) {
  11156. data[TG3_MAC_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
  11157. data[TG3_PHY_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
  11158. if (do_extlpbk)
  11159. data[TG3_EXT_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
  11160. goto done;
  11161. }
  11162. if (tg3_flag(tp, ENABLE_RSS)) {
  11163. int i;
  11164. /* Reroute all rx packets to the 1st queue */
  11165. for (i = MAC_RSS_INDIR_TBL_0;
  11166. i < MAC_RSS_INDIR_TBL_0 + TG3_RSS_INDIR_TBL_SIZE; i += 4)
  11167. tw32(i, 0x0);
  11168. }
  11169. /* HW errata - mac loopback fails in some cases on 5780.
  11170. * Normal traffic and PHY loopback are not affected by
  11171. * errata. Also, the MAC loopback test is deprecated for
  11172. * all newer ASIC revisions.
  11173. */
  11174. if (tg3_asic_rev(tp) != ASIC_REV_5780 &&
  11175. !tg3_flag(tp, CPMU_PRESENT)) {
  11176. tg3_mac_loopback(tp, true);
  11177. if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
  11178. data[TG3_MAC_LOOPB_TEST] |= TG3_STD_LOOPBACK_FAILED;
  11179. if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
  11180. tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
  11181. data[TG3_MAC_LOOPB_TEST] |= TG3_JMB_LOOPBACK_FAILED;
  11182. tg3_mac_loopback(tp, false);
  11183. }
  11184. if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
  11185. !tg3_flag(tp, USE_PHYLIB)) {
  11186. int i;
  11187. tg3_phy_lpbk_set(tp, 0, false);
  11188. /* Wait for link */
  11189. for (i = 0; i < 100; i++) {
  11190. if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
  11191. break;
  11192. mdelay(1);
  11193. }
  11194. if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
  11195. data[TG3_PHY_LOOPB_TEST] |= TG3_STD_LOOPBACK_FAILED;
  11196. if (tg3_flag(tp, TSO_CAPABLE) &&
  11197. tg3_run_loopback(tp, ETH_FRAME_LEN, true))
  11198. data[TG3_PHY_LOOPB_TEST] |= TG3_TSO_LOOPBACK_FAILED;
  11199. if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
  11200. tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
  11201. data[TG3_PHY_LOOPB_TEST] |= TG3_JMB_LOOPBACK_FAILED;
  11202. if (do_extlpbk) {
  11203. tg3_phy_lpbk_set(tp, 0, true);
  11204. /* All link indications report up, but the hardware
  11205. * isn't really ready for about 20 msec. Double it
  11206. * to be sure.
  11207. */
  11208. mdelay(40);
  11209. if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
  11210. data[TG3_EXT_LOOPB_TEST] |=
  11211. TG3_STD_LOOPBACK_FAILED;
  11212. if (tg3_flag(tp, TSO_CAPABLE) &&
  11213. tg3_run_loopback(tp, ETH_FRAME_LEN, true))
  11214. data[TG3_EXT_LOOPB_TEST] |=
  11215. TG3_TSO_LOOPBACK_FAILED;
  11216. if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
  11217. tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
  11218. data[TG3_EXT_LOOPB_TEST] |=
  11219. TG3_JMB_LOOPBACK_FAILED;
  11220. }
  11221. /* Re-enable gphy autopowerdown. */
  11222. if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
  11223. tg3_phy_toggle_apd(tp, true);
  11224. }
  11225. err = (data[TG3_MAC_LOOPB_TEST] | data[TG3_PHY_LOOPB_TEST] |
  11226. data[TG3_EXT_LOOPB_TEST]) ? -EIO : 0;
  11227. done:
  11228. tp->phy_flags |= eee_cap;
  11229. return err;
  11230. }
  11231. static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
  11232. u64 *data)
  11233. {
  11234. struct tg3 *tp = netdev_priv(dev);
  11235. bool doextlpbk = etest->flags & ETH_TEST_FL_EXTERNAL_LB;
  11236. if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
  11237. if (tg3_power_up(tp)) {
  11238. etest->flags |= ETH_TEST_FL_FAILED;
  11239. memset(data, 1, sizeof(u64) * TG3_NUM_TEST);
  11240. return;
  11241. }
  11242. tg3_ape_driver_state_change(tp, RESET_KIND_INIT);
  11243. }
  11244. memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
  11245. if (tg3_test_nvram(tp) != 0) {
  11246. etest->flags |= ETH_TEST_FL_FAILED;
  11247. data[TG3_NVRAM_TEST] = 1;
  11248. }
  11249. if (!doextlpbk && tg3_test_link(tp)) {
  11250. etest->flags |= ETH_TEST_FL_FAILED;
  11251. data[TG3_LINK_TEST] = 1;
  11252. }
  11253. if (etest->flags & ETH_TEST_FL_OFFLINE) {
  11254. int err, err2 = 0, irq_sync = 0;
  11255. if (netif_running(dev)) {
  11256. tg3_phy_stop(tp);
  11257. tg3_netif_stop(tp);
  11258. irq_sync = 1;
  11259. }
  11260. tg3_full_lock(tp, irq_sync);
  11261. tg3_halt(tp, RESET_KIND_SUSPEND, 1);
  11262. err = tg3_nvram_lock(tp);
  11263. tg3_halt_cpu(tp, RX_CPU_BASE);
  11264. if (!tg3_flag(tp, 5705_PLUS))
  11265. tg3_halt_cpu(tp, TX_CPU_BASE);
  11266. if (!err)
  11267. tg3_nvram_unlock(tp);
  11268. if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
  11269. tg3_phy_reset(tp);
  11270. if (tg3_test_registers(tp) != 0) {
  11271. etest->flags |= ETH_TEST_FL_FAILED;
  11272. data[TG3_REGISTER_TEST] = 1;
  11273. }
  11274. if (tg3_test_memory(tp) != 0) {
  11275. etest->flags |= ETH_TEST_FL_FAILED;
  11276. data[TG3_MEMORY_TEST] = 1;
  11277. }
  11278. if (doextlpbk)
  11279. etest->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE;
  11280. if (tg3_test_loopback(tp, data, doextlpbk))
  11281. etest->flags |= ETH_TEST_FL_FAILED;
  11282. tg3_full_unlock(tp);
  11283. if (tg3_test_interrupt(tp) != 0) {
  11284. etest->flags |= ETH_TEST_FL_FAILED;
  11285. data[TG3_INTERRUPT_TEST] = 1;
  11286. }
  11287. tg3_full_lock(tp, 0);
  11288. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  11289. if (netif_running(dev)) {
  11290. tg3_flag_set(tp, INIT_COMPLETE);
  11291. err2 = tg3_restart_hw(tp, true);
  11292. if (!err2)
  11293. tg3_netif_start(tp);
  11294. }
  11295. tg3_full_unlock(tp);
  11296. if (irq_sync && !err2)
  11297. tg3_phy_start(tp);
  11298. }
  11299. if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
  11300. tg3_power_down_prepare(tp);
  11301. }
  11302. static int tg3_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
  11303. {
  11304. struct tg3 *tp = netdev_priv(dev);
  11305. struct hwtstamp_config stmpconf;
  11306. if (!tg3_flag(tp, PTP_CAPABLE))
  11307. return -EOPNOTSUPP;
  11308. if (copy_from_user(&stmpconf, ifr->ifr_data, sizeof(stmpconf)))
  11309. return -EFAULT;
  11310. if (stmpconf.flags)
  11311. return -EINVAL;
  11312. if (stmpconf.tx_type != HWTSTAMP_TX_ON &&
  11313. stmpconf.tx_type != HWTSTAMP_TX_OFF)
  11314. return -ERANGE;
  11315. switch (stmpconf.rx_filter) {
  11316. case HWTSTAMP_FILTER_NONE:
  11317. tp->rxptpctl = 0;
  11318. break;
  11319. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  11320. tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
  11321. TG3_RX_PTP_CTL_ALL_V1_EVENTS;
  11322. break;
  11323. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  11324. tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
  11325. TG3_RX_PTP_CTL_SYNC_EVNT;
  11326. break;
  11327. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  11328. tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
  11329. TG3_RX_PTP_CTL_DELAY_REQ;
  11330. break;
  11331. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  11332. tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
  11333. TG3_RX_PTP_CTL_ALL_V2_EVENTS;
  11334. break;
  11335. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  11336. tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
  11337. TG3_RX_PTP_CTL_ALL_V2_EVENTS;
  11338. break;
  11339. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  11340. tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
  11341. TG3_RX_PTP_CTL_ALL_V2_EVENTS;
  11342. break;
  11343. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  11344. tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
  11345. TG3_RX_PTP_CTL_SYNC_EVNT;
  11346. break;
  11347. case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  11348. tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
  11349. TG3_RX_PTP_CTL_SYNC_EVNT;
  11350. break;
  11351. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  11352. tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
  11353. TG3_RX_PTP_CTL_SYNC_EVNT;
  11354. break;
  11355. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  11356. tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
  11357. TG3_RX_PTP_CTL_DELAY_REQ;
  11358. break;
  11359. case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
  11360. tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
  11361. TG3_RX_PTP_CTL_DELAY_REQ;
  11362. break;
  11363. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  11364. tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
  11365. TG3_RX_PTP_CTL_DELAY_REQ;
  11366. break;
  11367. default:
  11368. return -ERANGE;
  11369. }
  11370. if (netif_running(dev) && tp->rxptpctl)
  11371. tw32(TG3_RX_PTP_CTL,
  11372. tp->rxptpctl | TG3_RX_PTP_CTL_HWTS_INTERLOCK);
  11373. if (stmpconf.tx_type == HWTSTAMP_TX_ON)
  11374. tg3_flag_set(tp, TX_TSTAMP_EN);
  11375. else
  11376. tg3_flag_clear(tp, TX_TSTAMP_EN);
  11377. return copy_to_user(ifr->ifr_data, &stmpconf, sizeof(stmpconf)) ?
  11378. -EFAULT : 0;
  11379. }
  11380. static int tg3_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
  11381. {
  11382. struct tg3 *tp = netdev_priv(dev);
  11383. struct hwtstamp_config stmpconf;
  11384. if (!tg3_flag(tp, PTP_CAPABLE))
  11385. return -EOPNOTSUPP;
  11386. stmpconf.flags = 0;
  11387. stmpconf.tx_type = (tg3_flag(tp, TX_TSTAMP_EN) ?
  11388. HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF);
  11389. switch (tp->rxptpctl) {
  11390. case 0:
  11391. stmpconf.rx_filter = HWTSTAMP_FILTER_NONE;
  11392. break;
  11393. case TG3_RX_PTP_CTL_RX_PTP_V1_EN | TG3_RX_PTP_CTL_ALL_V1_EVENTS:
  11394. stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
  11395. break;
  11396. case TG3_RX_PTP_CTL_RX_PTP_V1_EN | TG3_RX_PTP_CTL_SYNC_EVNT:
  11397. stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_SYNC;
  11398. break;
  11399. case TG3_RX_PTP_CTL_RX_PTP_V1_EN | TG3_RX_PTP_CTL_DELAY_REQ:
  11400. stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ;
  11401. break;
  11402. case TG3_RX_PTP_CTL_RX_PTP_V2_EN | TG3_RX_PTP_CTL_ALL_V2_EVENTS:
  11403. stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
  11404. break;
  11405. case TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN | TG3_RX_PTP_CTL_ALL_V2_EVENTS:
  11406. stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
  11407. break;
  11408. case TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN | TG3_RX_PTP_CTL_ALL_V2_EVENTS:
  11409. stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
  11410. break;
  11411. case TG3_RX_PTP_CTL_RX_PTP_V2_EN | TG3_RX_PTP_CTL_SYNC_EVNT:
  11412. stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_SYNC;
  11413. break;
  11414. case TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN | TG3_RX_PTP_CTL_SYNC_EVNT:
  11415. stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_SYNC;
  11416. break;
  11417. case TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN | TG3_RX_PTP_CTL_SYNC_EVNT:
  11418. stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_SYNC;
  11419. break;
  11420. case TG3_RX_PTP_CTL_RX_PTP_V2_EN | TG3_RX_PTP_CTL_DELAY_REQ:
  11421. stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_DELAY_REQ;
  11422. break;
  11423. case TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN | TG3_RX_PTP_CTL_DELAY_REQ:
  11424. stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ;
  11425. break;
  11426. case TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN | TG3_RX_PTP_CTL_DELAY_REQ:
  11427. stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ;
  11428. break;
  11429. default:
  11430. WARN_ON_ONCE(1);
  11431. return -ERANGE;
  11432. }
  11433. return copy_to_user(ifr->ifr_data, &stmpconf, sizeof(stmpconf)) ?
  11434. -EFAULT : 0;
  11435. }
  11436. static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  11437. {
  11438. struct mii_ioctl_data *data = if_mii(ifr);
  11439. struct tg3 *tp = netdev_priv(dev);
  11440. int err;
  11441. if (tg3_flag(tp, USE_PHYLIB)) {
  11442. struct phy_device *phydev;
  11443. if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
  11444. return -EAGAIN;
  11445. phydev = tp->mdio_bus->phy_map[tp->phy_addr];
  11446. return phy_mii_ioctl(phydev, ifr, cmd);
  11447. }
  11448. switch (cmd) {
  11449. case SIOCGMIIPHY:
  11450. data->phy_id = tp->phy_addr;
  11451. /* fallthru */
  11452. case SIOCGMIIREG: {
  11453. u32 mii_regval;
  11454. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
  11455. break; /* We have no PHY */
  11456. if (!netif_running(dev))
  11457. return -EAGAIN;
  11458. spin_lock_bh(&tp->lock);
  11459. err = __tg3_readphy(tp, data->phy_id & 0x1f,
  11460. data->reg_num & 0x1f, &mii_regval);
  11461. spin_unlock_bh(&tp->lock);
  11462. data->val_out = mii_regval;
  11463. return err;
  11464. }
  11465. case SIOCSMIIREG:
  11466. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
  11467. break; /* We have no PHY */
  11468. if (!netif_running(dev))
  11469. return -EAGAIN;
  11470. spin_lock_bh(&tp->lock);
  11471. err = __tg3_writephy(tp, data->phy_id & 0x1f,
  11472. data->reg_num & 0x1f, data->val_in);
  11473. spin_unlock_bh(&tp->lock);
  11474. return err;
  11475. case SIOCSHWTSTAMP:
  11476. return tg3_hwtstamp_set(dev, ifr);
  11477. case SIOCGHWTSTAMP:
  11478. return tg3_hwtstamp_get(dev, ifr);
  11479. default:
  11480. /* do nothing */
  11481. break;
  11482. }
  11483. return -EOPNOTSUPP;
  11484. }
  11485. static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
  11486. {
  11487. struct tg3 *tp = netdev_priv(dev);
  11488. memcpy(ec, &tp->coal, sizeof(*ec));
  11489. return 0;
  11490. }
  11491. static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
  11492. {
  11493. struct tg3 *tp = netdev_priv(dev);
  11494. u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
  11495. u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
  11496. if (!tg3_flag(tp, 5705_PLUS)) {
  11497. max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
  11498. max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
  11499. max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
  11500. min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
  11501. }
  11502. if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
  11503. (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
  11504. (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
  11505. (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
  11506. (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
  11507. (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
  11508. (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
  11509. (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
  11510. (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
  11511. (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
  11512. return -EINVAL;
  11513. /* No rx interrupts will be generated if both are zero */
  11514. if ((ec->rx_coalesce_usecs == 0) &&
  11515. (ec->rx_max_coalesced_frames == 0))
  11516. return -EINVAL;
  11517. /* No tx interrupts will be generated if both are zero */
  11518. if ((ec->tx_coalesce_usecs == 0) &&
  11519. (ec->tx_max_coalesced_frames == 0))
  11520. return -EINVAL;
  11521. /* Only copy relevant parameters, ignore all others. */
  11522. tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
  11523. tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
  11524. tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
  11525. tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
  11526. tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
  11527. tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
  11528. tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
  11529. tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
  11530. tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
  11531. if (netif_running(dev)) {
  11532. tg3_full_lock(tp, 0);
  11533. __tg3_set_coalesce(tp, &tp->coal);
  11534. tg3_full_unlock(tp);
  11535. }
  11536. return 0;
  11537. }
  11538. static int tg3_set_eee(struct net_device *dev, struct ethtool_eee *edata)
  11539. {
  11540. struct tg3 *tp = netdev_priv(dev);
  11541. if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP)) {
  11542. netdev_warn(tp->dev, "Board does not support EEE!\n");
  11543. return -EOPNOTSUPP;
  11544. }
  11545. if (edata->advertised != tp->eee.advertised) {
  11546. netdev_warn(tp->dev,
  11547. "Direct manipulation of EEE advertisement is not supported\n");
  11548. return -EINVAL;
  11549. }
  11550. if (edata->tx_lpi_timer > TG3_CPMU_DBTMR1_LNKIDLE_MAX) {
  11551. netdev_warn(tp->dev,
  11552. "Maximal Tx Lpi timer supported is %#x(u)\n",
  11553. TG3_CPMU_DBTMR1_LNKIDLE_MAX);
  11554. return -EINVAL;
  11555. }
  11556. tp->eee = *edata;
  11557. tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
  11558. tg3_warn_mgmt_link_flap(tp);
  11559. if (netif_running(tp->dev)) {
  11560. tg3_full_lock(tp, 0);
  11561. tg3_setup_eee(tp);
  11562. tg3_phy_reset(tp);
  11563. tg3_full_unlock(tp);
  11564. }
  11565. return 0;
  11566. }
  11567. static int tg3_get_eee(struct net_device *dev, struct ethtool_eee *edata)
  11568. {
  11569. struct tg3 *tp = netdev_priv(dev);
  11570. if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP)) {
  11571. netdev_warn(tp->dev,
  11572. "Board does not support EEE!\n");
  11573. return -EOPNOTSUPP;
  11574. }
  11575. *edata = tp->eee;
  11576. return 0;
  11577. }
  11578. static const struct ethtool_ops tg3_ethtool_ops = {
  11579. .get_settings = tg3_get_settings,
  11580. .set_settings = tg3_set_settings,
  11581. .get_drvinfo = tg3_get_drvinfo,
  11582. .get_regs_len = tg3_get_regs_len,
  11583. .get_regs = tg3_get_regs,
  11584. .get_wol = tg3_get_wol,
  11585. .set_wol = tg3_set_wol,
  11586. .get_msglevel = tg3_get_msglevel,
  11587. .set_msglevel = tg3_set_msglevel,
  11588. .nway_reset = tg3_nway_reset,
  11589. .get_link = ethtool_op_get_link,
  11590. .get_eeprom_len = tg3_get_eeprom_len,
  11591. .get_eeprom = tg3_get_eeprom,
  11592. .set_eeprom = tg3_set_eeprom,
  11593. .get_ringparam = tg3_get_ringparam,
  11594. .set_ringparam = tg3_set_ringparam,
  11595. .get_pauseparam = tg3_get_pauseparam,
  11596. .set_pauseparam = tg3_set_pauseparam,
  11597. .self_test = tg3_self_test,
  11598. .get_strings = tg3_get_strings,
  11599. .set_phys_id = tg3_set_phys_id,
  11600. .get_ethtool_stats = tg3_get_ethtool_stats,
  11601. .get_coalesce = tg3_get_coalesce,
  11602. .set_coalesce = tg3_set_coalesce,
  11603. .get_sset_count = tg3_get_sset_count,
  11604. .get_rxnfc = tg3_get_rxnfc,
  11605. .get_rxfh_indir_size = tg3_get_rxfh_indir_size,
  11606. .get_rxfh = tg3_get_rxfh,
  11607. .set_rxfh = tg3_set_rxfh,
  11608. .get_channels = tg3_get_channels,
  11609. .set_channels = tg3_set_channels,
  11610. .get_ts_info = tg3_get_ts_info,
  11611. .get_eee = tg3_get_eee,
  11612. .set_eee = tg3_set_eee,
  11613. };
  11614. static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
  11615. struct rtnl_link_stats64 *stats)
  11616. {
  11617. struct tg3 *tp = netdev_priv(dev);
  11618. spin_lock_bh(&tp->lock);
  11619. if (!tp->hw_stats) {
  11620. *stats = tp->net_stats_prev;
  11621. spin_unlock_bh(&tp->lock);
  11622. return stats;
  11623. }
  11624. tg3_get_nstats(tp, stats);
  11625. spin_unlock_bh(&tp->lock);
  11626. return stats;
  11627. }
  11628. static void tg3_set_rx_mode(struct net_device *dev)
  11629. {
  11630. struct tg3 *tp = netdev_priv(dev);
  11631. if (!netif_running(dev))
  11632. return;
  11633. tg3_full_lock(tp, 0);
  11634. __tg3_set_rx_mode(dev);
  11635. tg3_full_unlock(tp);
  11636. }
  11637. static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
  11638. int new_mtu)
  11639. {
  11640. dev->mtu = new_mtu;
  11641. if (new_mtu > ETH_DATA_LEN) {
  11642. if (tg3_flag(tp, 5780_CLASS)) {
  11643. netdev_update_features(dev);
  11644. tg3_flag_clear(tp, TSO_CAPABLE);
  11645. } else {
  11646. tg3_flag_set(tp, JUMBO_RING_ENABLE);
  11647. }
  11648. } else {
  11649. if (tg3_flag(tp, 5780_CLASS)) {
  11650. tg3_flag_set(tp, TSO_CAPABLE);
  11651. netdev_update_features(dev);
  11652. }
  11653. tg3_flag_clear(tp, JUMBO_RING_ENABLE);
  11654. }
  11655. }
  11656. static int tg3_change_mtu(struct net_device *dev, int new_mtu)
  11657. {
  11658. struct tg3 *tp = netdev_priv(dev);
  11659. int err;
  11660. bool reset_phy = false;
  11661. if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
  11662. return -EINVAL;
  11663. if (!netif_running(dev)) {
  11664. /* We'll just catch it later when the
  11665. * device is up'd.
  11666. */
  11667. tg3_set_mtu(dev, tp, new_mtu);
  11668. return 0;
  11669. }
  11670. tg3_phy_stop(tp);
  11671. tg3_netif_stop(tp);
  11672. tg3_set_mtu(dev, tp, new_mtu);
  11673. tg3_full_lock(tp, 1);
  11674. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  11675. /* Reset PHY, otherwise the read DMA engine will be in a mode that
  11676. * breaks all requests to 256 bytes.
  11677. */
  11678. if (tg3_asic_rev(tp) == ASIC_REV_57766)
  11679. reset_phy = true;
  11680. err = tg3_restart_hw(tp, reset_phy);
  11681. if (!err)
  11682. tg3_netif_start(tp);
  11683. tg3_full_unlock(tp);
  11684. if (!err)
  11685. tg3_phy_start(tp);
  11686. return err;
  11687. }
  11688. static const struct net_device_ops tg3_netdev_ops = {
  11689. .ndo_open = tg3_open,
  11690. .ndo_stop = tg3_close,
  11691. .ndo_start_xmit = tg3_start_xmit,
  11692. .ndo_get_stats64 = tg3_get_stats64,
  11693. .ndo_validate_addr = eth_validate_addr,
  11694. .ndo_set_rx_mode = tg3_set_rx_mode,
  11695. .ndo_set_mac_address = tg3_set_mac_addr,
  11696. .ndo_do_ioctl = tg3_ioctl,
  11697. .ndo_tx_timeout = tg3_tx_timeout,
  11698. .ndo_change_mtu = tg3_change_mtu,
  11699. .ndo_fix_features = tg3_fix_features,
  11700. .ndo_set_features = tg3_set_features,
  11701. #ifdef CONFIG_NET_POLL_CONTROLLER
  11702. .ndo_poll_controller = tg3_poll_controller,
  11703. #endif
  11704. };
  11705. static void tg3_get_eeprom_size(struct tg3 *tp)
  11706. {
  11707. u32 cursize, val, magic;
  11708. tp->nvram_size = EEPROM_CHIP_SIZE;
  11709. if (tg3_nvram_read(tp, 0, &magic) != 0)
  11710. return;
  11711. if ((magic != TG3_EEPROM_MAGIC) &&
  11712. ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
  11713. ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
  11714. return;
  11715. /*
  11716. * Size the chip by reading offsets at increasing powers of two.
  11717. * When we encounter our validation signature, we know the addressing
  11718. * has wrapped around, and thus have our chip size.
  11719. */
  11720. cursize = 0x10;
  11721. while (cursize < tp->nvram_size) {
  11722. if (tg3_nvram_read(tp, cursize, &val) != 0)
  11723. return;
  11724. if (val == magic)
  11725. break;
  11726. cursize <<= 1;
  11727. }
  11728. tp->nvram_size = cursize;
  11729. }
  11730. static void tg3_get_nvram_size(struct tg3 *tp)
  11731. {
  11732. u32 val;
  11733. if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &val) != 0)
  11734. return;
  11735. /* Selfboot format */
  11736. if (val != TG3_EEPROM_MAGIC) {
  11737. tg3_get_eeprom_size(tp);
  11738. return;
  11739. }
  11740. if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
  11741. if (val != 0) {
  11742. /* This is confusing. We want to operate on the
  11743. * 16-bit value at offset 0xf2. The tg3_nvram_read()
  11744. * call will read from NVRAM and byteswap the data
  11745. * according to the byteswapping settings for all
  11746. * other register accesses. This ensures the data we
  11747. * want will always reside in the lower 16-bits.
  11748. * However, the data in NVRAM is in LE format, which
  11749. * means the data from the NVRAM read will always be
  11750. * opposite the endianness of the CPU. The 16-bit
  11751. * byteswap then brings the data to CPU endianness.
  11752. */
  11753. tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
  11754. return;
  11755. }
  11756. }
  11757. tp->nvram_size = TG3_NVRAM_SIZE_512KB;
  11758. }
  11759. static void tg3_get_nvram_info(struct tg3 *tp)
  11760. {
  11761. u32 nvcfg1;
  11762. nvcfg1 = tr32(NVRAM_CFG1);
  11763. if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
  11764. tg3_flag_set(tp, FLASH);
  11765. } else {
  11766. nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
  11767. tw32(NVRAM_CFG1, nvcfg1);
  11768. }
  11769. if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
  11770. tg3_flag(tp, 5780_CLASS)) {
  11771. switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
  11772. case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
  11773. tp->nvram_jedecnum = JEDEC_ATMEL;
  11774. tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
  11775. tg3_flag_set(tp, NVRAM_BUFFERED);
  11776. break;
  11777. case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
  11778. tp->nvram_jedecnum = JEDEC_ATMEL;
  11779. tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
  11780. break;
  11781. case FLASH_VENDOR_ATMEL_EEPROM:
  11782. tp->nvram_jedecnum = JEDEC_ATMEL;
  11783. tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
  11784. tg3_flag_set(tp, NVRAM_BUFFERED);
  11785. break;
  11786. case FLASH_VENDOR_ST:
  11787. tp->nvram_jedecnum = JEDEC_ST;
  11788. tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
  11789. tg3_flag_set(tp, NVRAM_BUFFERED);
  11790. break;
  11791. case FLASH_VENDOR_SAIFUN:
  11792. tp->nvram_jedecnum = JEDEC_SAIFUN;
  11793. tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
  11794. break;
  11795. case FLASH_VENDOR_SST_SMALL:
  11796. case FLASH_VENDOR_SST_LARGE:
  11797. tp->nvram_jedecnum = JEDEC_SST;
  11798. tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
  11799. break;
  11800. }
  11801. } else {
  11802. tp->nvram_jedecnum = JEDEC_ATMEL;
  11803. tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
  11804. tg3_flag_set(tp, NVRAM_BUFFERED);
  11805. }
  11806. }
  11807. static void tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
  11808. {
  11809. switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
  11810. case FLASH_5752PAGE_SIZE_256:
  11811. tp->nvram_pagesize = 256;
  11812. break;
  11813. case FLASH_5752PAGE_SIZE_512:
  11814. tp->nvram_pagesize = 512;
  11815. break;
  11816. case FLASH_5752PAGE_SIZE_1K:
  11817. tp->nvram_pagesize = 1024;
  11818. break;
  11819. case FLASH_5752PAGE_SIZE_2K:
  11820. tp->nvram_pagesize = 2048;
  11821. break;
  11822. case FLASH_5752PAGE_SIZE_4K:
  11823. tp->nvram_pagesize = 4096;
  11824. break;
  11825. case FLASH_5752PAGE_SIZE_264:
  11826. tp->nvram_pagesize = 264;
  11827. break;
  11828. case FLASH_5752PAGE_SIZE_528:
  11829. tp->nvram_pagesize = 528;
  11830. break;
  11831. }
  11832. }
  11833. static void tg3_get_5752_nvram_info(struct tg3 *tp)
  11834. {
  11835. u32 nvcfg1;
  11836. nvcfg1 = tr32(NVRAM_CFG1);
  11837. /* NVRAM protection for TPM */
  11838. if (nvcfg1 & (1 << 27))
  11839. tg3_flag_set(tp, PROTECTED_NVRAM);
  11840. switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
  11841. case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
  11842. case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
  11843. tp->nvram_jedecnum = JEDEC_ATMEL;
  11844. tg3_flag_set(tp, NVRAM_BUFFERED);
  11845. break;
  11846. case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
  11847. tp->nvram_jedecnum = JEDEC_ATMEL;
  11848. tg3_flag_set(tp, NVRAM_BUFFERED);
  11849. tg3_flag_set(tp, FLASH);
  11850. break;
  11851. case FLASH_5752VENDOR_ST_M45PE10:
  11852. case FLASH_5752VENDOR_ST_M45PE20:
  11853. case FLASH_5752VENDOR_ST_M45PE40:
  11854. tp->nvram_jedecnum = JEDEC_ST;
  11855. tg3_flag_set(tp, NVRAM_BUFFERED);
  11856. tg3_flag_set(tp, FLASH);
  11857. break;
  11858. }
  11859. if (tg3_flag(tp, FLASH)) {
  11860. tg3_nvram_get_pagesize(tp, nvcfg1);
  11861. } else {
  11862. /* For eeprom, set pagesize to maximum eeprom size */
  11863. tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
  11864. nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
  11865. tw32(NVRAM_CFG1, nvcfg1);
  11866. }
  11867. }
  11868. static void tg3_get_5755_nvram_info(struct tg3 *tp)
  11869. {
  11870. u32 nvcfg1, protect = 0;
  11871. nvcfg1 = tr32(NVRAM_CFG1);
  11872. /* NVRAM protection for TPM */
  11873. if (nvcfg1 & (1 << 27)) {
  11874. tg3_flag_set(tp, PROTECTED_NVRAM);
  11875. protect = 1;
  11876. }
  11877. nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
  11878. switch (nvcfg1) {
  11879. case FLASH_5755VENDOR_ATMEL_FLASH_1:
  11880. case FLASH_5755VENDOR_ATMEL_FLASH_2:
  11881. case FLASH_5755VENDOR_ATMEL_FLASH_3:
  11882. case FLASH_5755VENDOR_ATMEL_FLASH_5:
  11883. tp->nvram_jedecnum = JEDEC_ATMEL;
  11884. tg3_flag_set(tp, NVRAM_BUFFERED);
  11885. tg3_flag_set(tp, FLASH);
  11886. tp->nvram_pagesize = 264;
  11887. if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
  11888. nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
  11889. tp->nvram_size = (protect ? 0x3e200 :
  11890. TG3_NVRAM_SIZE_512KB);
  11891. else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
  11892. tp->nvram_size = (protect ? 0x1f200 :
  11893. TG3_NVRAM_SIZE_256KB);
  11894. else
  11895. tp->nvram_size = (protect ? 0x1f200 :
  11896. TG3_NVRAM_SIZE_128KB);
  11897. break;
  11898. case FLASH_5752VENDOR_ST_M45PE10:
  11899. case FLASH_5752VENDOR_ST_M45PE20:
  11900. case FLASH_5752VENDOR_ST_M45PE40:
  11901. tp->nvram_jedecnum = JEDEC_ST;
  11902. tg3_flag_set(tp, NVRAM_BUFFERED);
  11903. tg3_flag_set(tp, FLASH);
  11904. tp->nvram_pagesize = 256;
  11905. if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
  11906. tp->nvram_size = (protect ?
  11907. TG3_NVRAM_SIZE_64KB :
  11908. TG3_NVRAM_SIZE_128KB);
  11909. else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
  11910. tp->nvram_size = (protect ?
  11911. TG3_NVRAM_SIZE_64KB :
  11912. TG3_NVRAM_SIZE_256KB);
  11913. else
  11914. tp->nvram_size = (protect ?
  11915. TG3_NVRAM_SIZE_128KB :
  11916. TG3_NVRAM_SIZE_512KB);
  11917. break;
  11918. }
  11919. }
  11920. static void tg3_get_5787_nvram_info(struct tg3 *tp)
  11921. {
  11922. u32 nvcfg1;
  11923. nvcfg1 = tr32(NVRAM_CFG1);
  11924. switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
  11925. case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
  11926. case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
  11927. case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
  11928. case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
  11929. tp->nvram_jedecnum = JEDEC_ATMEL;
  11930. tg3_flag_set(tp, NVRAM_BUFFERED);
  11931. tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
  11932. nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
  11933. tw32(NVRAM_CFG1, nvcfg1);
  11934. break;
  11935. case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
  11936. case FLASH_5755VENDOR_ATMEL_FLASH_1:
  11937. case FLASH_5755VENDOR_ATMEL_FLASH_2:
  11938. case FLASH_5755VENDOR_ATMEL_FLASH_3:
  11939. tp->nvram_jedecnum = JEDEC_ATMEL;
  11940. tg3_flag_set(tp, NVRAM_BUFFERED);
  11941. tg3_flag_set(tp, FLASH);
  11942. tp->nvram_pagesize = 264;
  11943. break;
  11944. case FLASH_5752VENDOR_ST_M45PE10:
  11945. case FLASH_5752VENDOR_ST_M45PE20:
  11946. case FLASH_5752VENDOR_ST_M45PE40:
  11947. tp->nvram_jedecnum = JEDEC_ST;
  11948. tg3_flag_set(tp, NVRAM_BUFFERED);
  11949. tg3_flag_set(tp, FLASH);
  11950. tp->nvram_pagesize = 256;
  11951. break;
  11952. }
  11953. }
  11954. static void tg3_get_5761_nvram_info(struct tg3 *tp)
  11955. {
  11956. u32 nvcfg1, protect = 0;
  11957. nvcfg1 = tr32(NVRAM_CFG1);
  11958. /* NVRAM protection for TPM */
  11959. if (nvcfg1 & (1 << 27)) {
  11960. tg3_flag_set(tp, PROTECTED_NVRAM);
  11961. protect = 1;
  11962. }
  11963. nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
  11964. switch (nvcfg1) {
  11965. case FLASH_5761VENDOR_ATMEL_ADB021D:
  11966. case FLASH_5761VENDOR_ATMEL_ADB041D:
  11967. case FLASH_5761VENDOR_ATMEL_ADB081D:
  11968. case FLASH_5761VENDOR_ATMEL_ADB161D:
  11969. case FLASH_5761VENDOR_ATMEL_MDB021D:
  11970. case FLASH_5761VENDOR_ATMEL_MDB041D:
  11971. case FLASH_5761VENDOR_ATMEL_MDB081D:
  11972. case FLASH_5761VENDOR_ATMEL_MDB161D:
  11973. tp->nvram_jedecnum = JEDEC_ATMEL;
  11974. tg3_flag_set(tp, NVRAM_BUFFERED);
  11975. tg3_flag_set(tp, FLASH);
  11976. tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
  11977. tp->nvram_pagesize = 256;
  11978. break;
  11979. case FLASH_5761VENDOR_ST_A_M45PE20:
  11980. case FLASH_5761VENDOR_ST_A_M45PE40:
  11981. case FLASH_5761VENDOR_ST_A_M45PE80:
  11982. case FLASH_5761VENDOR_ST_A_M45PE16:
  11983. case FLASH_5761VENDOR_ST_M_M45PE20:
  11984. case FLASH_5761VENDOR_ST_M_M45PE40:
  11985. case FLASH_5761VENDOR_ST_M_M45PE80:
  11986. case FLASH_5761VENDOR_ST_M_M45PE16:
  11987. tp->nvram_jedecnum = JEDEC_ST;
  11988. tg3_flag_set(tp, NVRAM_BUFFERED);
  11989. tg3_flag_set(tp, FLASH);
  11990. tp->nvram_pagesize = 256;
  11991. break;
  11992. }
  11993. if (protect) {
  11994. tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
  11995. } else {
  11996. switch (nvcfg1) {
  11997. case FLASH_5761VENDOR_ATMEL_ADB161D:
  11998. case FLASH_5761VENDOR_ATMEL_MDB161D:
  11999. case FLASH_5761VENDOR_ST_A_M45PE16:
  12000. case FLASH_5761VENDOR_ST_M_M45PE16:
  12001. tp->nvram_size = TG3_NVRAM_SIZE_2MB;
  12002. break;
  12003. case FLASH_5761VENDOR_ATMEL_ADB081D:
  12004. case FLASH_5761VENDOR_ATMEL_MDB081D:
  12005. case FLASH_5761VENDOR_ST_A_M45PE80:
  12006. case FLASH_5761VENDOR_ST_M_M45PE80:
  12007. tp->nvram_size = TG3_NVRAM_SIZE_1MB;
  12008. break;
  12009. case FLASH_5761VENDOR_ATMEL_ADB041D:
  12010. case FLASH_5761VENDOR_ATMEL_MDB041D:
  12011. case FLASH_5761VENDOR_ST_A_M45PE40:
  12012. case FLASH_5761VENDOR_ST_M_M45PE40:
  12013. tp->nvram_size = TG3_NVRAM_SIZE_512KB;
  12014. break;
  12015. case FLASH_5761VENDOR_ATMEL_ADB021D:
  12016. case FLASH_5761VENDOR_ATMEL_MDB021D:
  12017. case FLASH_5761VENDOR_ST_A_M45PE20:
  12018. case FLASH_5761VENDOR_ST_M_M45PE20:
  12019. tp->nvram_size = TG3_NVRAM_SIZE_256KB;
  12020. break;
  12021. }
  12022. }
  12023. }
  12024. static void tg3_get_5906_nvram_info(struct tg3 *tp)
  12025. {
  12026. tp->nvram_jedecnum = JEDEC_ATMEL;
  12027. tg3_flag_set(tp, NVRAM_BUFFERED);
  12028. tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
  12029. }
  12030. static void tg3_get_57780_nvram_info(struct tg3 *tp)
  12031. {
  12032. u32 nvcfg1;
  12033. nvcfg1 = tr32(NVRAM_CFG1);
  12034. switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
  12035. case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
  12036. case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
  12037. tp->nvram_jedecnum = JEDEC_ATMEL;
  12038. tg3_flag_set(tp, NVRAM_BUFFERED);
  12039. tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
  12040. nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
  12041. tw32(NVRAM_CFG1, nvcfg1);
  12042. return;
  12043. case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
  12044. case FLASH_57780VENDOR_ATMEL_AT45DB011D:
  12045. case FLASH_57780VENDOR_ATMEL_AT45DB011B:
  12046. case FLASH_57780VENDOR_ATMEL_AT45DB021D:
  12047. case FLASH_57780VENDOR_ATMEL_AT45DB021B:
  12048. case FLASH_57780VENDOR_ATMEL_AT45DB041D:
  12049. case FLASH_57780VENDOR_ATMEL_AT45DB041B:
  12050. tp->nvram_jedecnum = JEDEC_ATMEL;
  12051. tg3_flag_set(tp, NVRAM_BUFFERED);
  12052. tg3_flag_set(tp, FLASH);
  12053. switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
  12054. case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
  12055. case FLASH_57780VENDOR_ATMEL_AT45DB011D:
  12056. case FLASH_57780VENDOR_ATMEL_AT45DB011B:
  12057. tp->nvram_size = TG3_NVRAM_SIZE_128KB;
  12058. break;
  12059. case FLASH_57780VENDOR_ATMEL_AT45DB021D:
  12060. case FLASH_57780VENDOR_ATMEL_AT45DB021B:
  12061. tp->nvram_size = TG3_NVRAM_SIZE_256KB;
  12062. break;
  12063. case FLASH_57780VENDOR_ATMEL_AT45DB041D:
  12064. case FLASH_57780VENDOR_ATMEL_AT45DB041B:
  12065. tp->nvram_size = TG3_NVRAM_SIZE_512KB;
  12066. break;
  12067. }
  12068. break;
  12069. case FLASH_5752VENDOR_ST_M45PE10:
  12070. case FLASH_5752VENDOR_ST_M45PE20:
  12071. case FLASH_5752VENDOR_ST_M45PE40:
  12072. tp->nvram_jedecnum = JEDEC_ST;
  12073. tg3_flag_set(tp, NVRAM_BUFFERED);
  12074. tg3_flag_set(tp, FLASH);
  12075. switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
  12076. case FLASH_5752VENDOR_ST_M45PE10:
  12077. tp->nvram_size = TG3_NVRAM_SIZE_128KB;
  12078. break;
  12079. case FLASH_5752VENDOR_ST_M45PE20:
  12080. tp->nvram_size = TG3_NVRAM_SIZE_256KB;
  12081. break;
  12082. case FLASH_5752VENDOR_ST_M45PE40:
  12083. tp->nvram_size = TG3_NVRAM_SIZE_512KB;
  12084. break;
  12085. }
  12086. break;
  12087. default:
  12088. tg3_flag_set(tp, NO_NVRAM);
  12089. return;
  12090. }
  12091. tg3_nvram_get_pagesize(tp, nvcfg1);
  12092. if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
  12093. tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
  12094. }
  12095. static void tg3_get_5717_nvram_info(struct tg3 *tp)
  12096. {
  12097. u32 nvcfg1;
  12098. nvcfg1 = tr32(NVRAM_CFG1);
  12099. switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
  12100. case FLASH_5717VENDOR_ATMEL_EEPROM:
  12101. case FLASH_5717VENDOR_MICRO_EEPROM:
  12102. tp->nvram_jedecnum = JEDEC_ATMEL;
  12103. tg3_flag_set(tp, NVRAM_BUFFERED);
  12104. tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
  12105. nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
  12106. tw32(NVRAM_CFG1, nvcfg1);
  12107. return;
  12108. case FLASH_5717VENDOR_ATMEL_MDB011D:
  12109. case FLASH_5717VENDOR_ATMEL_ADB011B:
  12110. case FLASH_5717VENDOR_ATMEL_ADB011D:
  12111. case FLASH_5717VENDOR_ATMEL_MDB021D:
  12112. case FLASH_5717VENDOR_ATMEL_ADB021B:
  12113. case FLASH_5717VENDOR_ATMEL_ADB021D:
  12114. case FLASH_5717VENDOR_ATMEL_45USPT:
  12115. tp->nvram_jedecnum = JEDEC_ATMEL;
  12116. tg3_flag_set(tp, NVRAM_BUFFERED);
  12117. tg3_flag_set(tp, FLASH);
  12118. switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
  12119. case FLASH_5717VENDOR_ATMEL_MDB021D:
  12120. /* Detect size with tg3_nvram_get_size() */
  12121. break;
  12122. case FLASH_5717VENDOR_ATMEL_ADB021B:
  12123. case FLASH_5717VENDOR_ATMEL_ADB021D:
  12124. tp->nvram_size = TG3_NVRAM_SIZE_256KB;
  12125. break;
  12126. default:
  12127. tp->nvram_size = TG3_NVRAM_SIZE_128KB;
  12128. break;
  12129. }
  12130. break;
  12131. case FLASH_5717VENDOR_ST_M_M25PE10:
  12132. case FLASH_5717VENDOR_ST_A_M25PE10:
  12133. case FLASH_5717VENDOR_ST_M_M45PE10:
  12134. case FLASH_5717VENDOR_ST_A_M45PE10:
  12135. case FLASH_5717VENDOR_ST_M_M25PE20:
  12136. case FLASH_5717VENDOR_ST_A_M25PE20:
  12137. case FLASH_5717VENDOR_ST_M_M45PE20:
  12138. case FLASH_5717VENDOR_ST_A_M45PE20:
  12139. case FLASH_5717VENDOR_ST_25USPT:
  12140. case FLASH_5717VENDOR_ST_45USPT:
  12141. tp->nvram_jedecnum = JEDEC_ST;
  12142. tg3_flag_set(tp, NVRAM_BUFFERED);
  12143. tg3_flag_set(tp, FLASH);
  12144. switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
  12145. case FLASH_5717VENDOR_ST_M_M25PE20:
  12146. case FLASH_5717VENDOR_ST_M_M45PE20:
  12147. /* Detect size with tg3_nvram_get_size() */
  12148. break;
  12149. case FLASH_5717VENDOR_ST_A_M25PE20:
  12150. case FLASH_5717VENDOR_ST_A_M45PE20:
  12151. tp->nvram_size = TG3_NVRAM_SIZE_256KB;
  12152. break;
  12153. default:
  12154. tp->nvram_size = TG3_NVRAM_SIZE_128KB;
  12155. break;
  12156. }
  12157. break;
  12158. default:
  12159. tg3_flag_set(tp, NO_NVRAM);
  12160. return;
  12161. }
  12162. tg3_nvram_get_pagesize(tp, nvcfg1);
  12163. if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
  12164. tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
  12165. }
  12166. static void tg3_get_5720_nvram_info(struct tg3 *tp)
  12167. {
  12168. u32 nvcfg1, nvmpinstrp;
  12169. nvcfg1 = tr32(NVRAM_CFG1);
  12170. nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
  12171. if (tg3_asic_rev(tp) == ASIC_REV_5762) {
  12172. if (!(nvcfg1 & NVRAM_CFG1_5762VENDOR_MASK)) {
  12173. tg3_flag_set(tp, NO_NVRAM);
  12174. return;
  12175. }
  12176. switch (nvmpinstrp) {
  12177. case FLASH_5762_EEPROM_HD:
  12178. nvmpinstrp = FLASH_5720_EEPROM_HD;
  12179. break;
  12180. case FLASH_5762_EEPROM_LD:
  12181. nvmpinstrp = FLASH_5720_EEPROM_LD;
  12182. break;
  12183. case FLASH_5720VENDOR_M_ST_M45PE20:
  12184. /* This pinstrap supports multiple sizes, so force it
  12185. * to read the actual size from location 0xf0.
  12186. */
  12187. nvmpinstrp = FLASH_5720VENDOR_ST_45USPT;
  12188. break;
  12189. }
  12190. }
  12191. switch (nvmpinstrp) {
  12192. case FLASH_5720_EEPROM_HD:
  12193. case FLASH_5720_EEPROM_LD:
  12194. tp->nvram_jedecnum = JEDEC_ATMEL;
  12195. tg3_flag_set(tp, NVRAM_BUFFERED);
  12196. nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
  12197. tw32(NVRAM_CFG1, nvcfg1);
  12198. if (nvmpinstrp == FLASH_5720_EEPROM_HD)
  12199. tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
  12200. else
  12201. tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
  12202. return;
  12203. case FLASH_5720VENDOR_M_ATMEL_DB011D:
  12204. case FLASH_5720VENDOR_A_ATMEL_DB011B:
  12205. case FLASH_5720VENDOR_A_ATMEL_DB011D:
  12206. case FLASH_5720VENDOR_M_ATMEL_DB021D:
  12207. case FLASH_5720VENDOR_A_ATMEL_DB021B:
  12208. case FLASH_5720VENDOR_A_ATMEL_DB021D:
  12209. case FLASH_5720VENDOR_M_ATMEL_DB041D:
  12210. case FLASH_5720VENDOR_A_ATMEL_DB041B:
  12211. case FLASH_5720VENDOR_A_ATMEL_DB041D:
  12212. case FLASH_5720VENDOR_M_ATMEL_DB081D:
  12213. case FLASH_5720VENDOR_A_ATMEL_DB081D:
  12214. case FLASH_5720VENDOR_ATMEL_45USPT:
  12215. tp->nvram_jedecnum = JEDEC_ATMEL;
  12216. tg3_flag_set(tp, NVRAM_BUFFERED);
  12217. tg3_flag_set(tp, FLASH);
  12218. switch (nvmpinstrp) {
  12219. case FLASH_5720VENDOR_M_ATMEL_DB021D:
  12220. case FLASH_5720VENDOR_A_ATMEL_DB021B:
  12221. case FLASH_5720VENDOR_A_ATMEL_DB021D:
  12222. tp->nvram_size = TG3_NVRAM_SIZE_256KB;
  12223. break;
  12224. case FLASH_5720VENDOR_M_ATMEL_DB041D:
  12225. case FLASH_5720VENDOR_A_ATMEL_DB041B:
  12226. case FLASH_5720VENDOR_A_ATMEL_DB041D:
  12227. tp->nvram_size = TG3_NVRAM_SIZE_512KB;
  12228. break;
  12229. case FLASH_5720VENDOR_M_ATMEL_DB081D:
  12230. case FLASH_5720VENDOR_A_ATMEL_DB081D:
  12231. tp->nvram_size = TG3_NVRAM_SIZE_1MB;
  12232. break;
  12233. default:
  12234. if (tg3_asic_rev(tp) != ASIC_REV_5762)
  12235. tp->nvram_size = TG3_NVRAM_SIZE_128KB;
  12236. break;
  12237. }
  12238. break;
  12239. case FLASH_5720VENDOR_M_ST_M25PE10:
  12240. case FLASH_5720VENDOR_M_ST_M45PE10:
  12241. case FLASH_5720VENDOR_A_ST_M25PE10:
  12242. case FLASH_5720VENDOR_A_ST_M45PE10:
  12243. case FLASH_5720VENDOR_M_ST_M25PE20:
  12244. case FLASH_5720VENDOR_M_ST_M45PE20:
  12245. case FLASH_5720VENDOR_A_ST_M25PE20:
  12246. case FLASH_5720VENDOR_A_ST_M45PE20:
  12247. case FLASH_5720VENDOR_M_ST_M25PE40:
  12248. case FLASH_5720VENDOR_M_ST_M45PE40:
  12249. case FLASH_5720VENDOR_A_ST_M25PE40:
  12250. case FLASH_5720VENDOR_A_ST_M45PE40:
  12251. case FLASH_5720VENDOR_M_ST_M25PE80:
  12252. case FLASH_5720VENDOR_M_ST_M45PE80:
  12253. case FLASH_5720VENDOR_A_ST_M25PE80:
  12254. case FLASH_5720VENDOR_A_ST_M45PE80:
  12255. case FLASH_5720VENDOR_ST_25USPT:
  12256. case FLASH_5720VENDOR_ST_45USPT:
  12257. tp->nvram_jedecnum = JEDEC_ST;
  12258. tg3_flag_set(tp, NVRAM_BUFFERED);
  12259. tg3_flag_set(tp, FLASH);
  12260. switch (nvmpinstrp) {
  12261. case FLASH_5720VENDOR_M_ST_M25PE20:
  12262. case FLASH_5720VENDOR_M_ST_M45PE20:
  12263. case FLASH_5720VENDOR_A_ST_M25PE20:
  12264. case FLASH_5720VENDOR_A_ST_M45PE20:
  12265. tp->nvram_size = TG3_NVRAM_SIZE_256KB;
  12266. break;
  12267. case FLASH_5720VENDOR_M_ST_M25PE40:
  12268. case FLASH_5720VENDOR_M_ST_M45PE40:
  12269. case FLASH_5720VENDOR_A_ST_M25PE40:
  12270. case FLASH_5720VENDOR_A_ST_M45PE40:
  12271. tp->nvram_size = TG3_NVRAM_SIZE_512KB;
  12272. break;
  12273. case FLASH_5720VENDOR_M_ST_M25PE80:
  12274. case FLASH_5720VENDOR_M_ST_M45PE80:
  12275. case FLASH_5720VENDOR_A_ST_M25PE80:
  12276. case FLASH_5720VENDOR_A_ST_M45PE80:
  12277. tp->nvram_size = TG3_NVRAM_SIZE_1MB;
  12278. break;
  12279. default:
  12280. if (tg3_asic_rev(tp) != ASIC_REV_5762)
  12281. tp->nvram_size = TG3_NVRAM_SIZE_128KB;
  12282. break;
  12283. }
  12284. break;
  12285. default:
  12286. tg3_flag_set(tp, NO_NVRAM);
  12287. return;
  12288. }
  12289. tg3_nvram_get_pagesize(tp, nvcfg1);
  12290. if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
  12291. tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
  12292. if (tg3_asic_rev(tp) == ASIC_REV_5762) {
  12293. u32 val;
  12294. if (tg3_nvram_read(tp, 0, &val))
  12295. return;
  12296. if (val != TG3_EEPROM_MAGIC &&
  12297. (val & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW)
  12298. tg3_flag_set(tp, NO_NVRAM);
  12299. }
  12300. }
  12301. /* Chips other than 5700/5701 use the NVRAM for fetching info. */
  12302. static void tg3_nvram_init(struct tg3 *tp)
  12303. {
  12304. if (tg3_flag(tp, IS_SSB_CORE)) {
  12305. /* No NVRAM and EEPROM on the SSB Broadcom GigE core. */
  12306. tg3_flag_clear(tp, NVRAM);
  12307. tg3_flag_clear(tp, NVRAM_BUFFERED);
  12308. tg3_flag_set(tp, NO_NVRAM);
  12309. return;
  12310. }
  12311. tw32_f(GRC_EEPROM_ADDR,
  12312. (EEPROM_ADDR_FSM_RESET |
  12313. (EEPROM_DEFAULT_CLOCK_PERIOD <<
  12314. EEPROM_ADDR_CLKPERD_SHIFT)));
  12315. msleep(1);
  12316. /* Enable seeprom accesses. */
  12317. tw32_f(GRC_LOCAL_CTRL,
  12318. tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
  12319. udelay(100);
  12320. if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
  12321. tg3_asic_rev(tp) != ASIC_REV_5701) {
  12322. tg3_flag_set(tp, NVRAM);
  12323. if (tg3_nvram_lock(tp)) {
  12324. netdev_warn(tp->dev,
  12325. "Cannot get nvram lock, %s failed\n",
  12326. __func__);
  12327. return;
  12328. }
  12329. tg3_enable_nvram_access(tp);
  12330. tp->nvram_size = 0;
  12331. if (tg3_asic_rev(tp) == ASIC_REV_5752)
  12332. tg3_get_5752_nvram_info(tp);
  12333. else if (tg3_asic_rev(tp) == ASIC_REV_5755)
  12334. tg3_get_5755_nvram_info(tp);
  12335. else if (tg3_asic_rev(tp) == ASIC_REV_5787 ||
  12336. tg3_asic_rev(tp) == ASIC_REV_5784 ||
  12337. tg3_asic_rev(tp) == ASIC_REV_5785)
  12338. tg3_get_5787_nvram_info(tp);
  12339. else if (tg3_asic_rev(tp) == ASIC_REV_5761)
  12340. tg3_get_5761_nvram_info(tp);
  12341. else if (tg3_asic_rev(tp) == ASIC_REV_5906)
  12342. tg3_get_5906_nvram_info(tp);
  12343. else if (tg3_asic_rev(tp) == ASIC_REV_57780 ||
  12344. tg3_flag(tp, 57765_CLASS))
  12345. tg3_get_57780_nvram_info(tp);
  12346. else if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
  12347. tg3_asic_rev(tp) == ASIC_REV_5719)
  12348. tg3_get_5717_nvram_info(tp);
  12349. else if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
  12350. tg3_asic_rev(tp) == ASIC_REV_5762)
  12351. tg3_get_5720_nvram_info(tp);
  12352. else
  12353. tg3_get_nvram_info(tp);
  12354. if (tp->nvram_size == 0)
  12355. tg3_get_nvram_size(tp);
  12356. tg3_disable_nvram_access(tp);
  12357. tg3_nvram_unlock(tp);
  12358. } else {
  12359. tg3_flag_clear(tp, NVRAM);
  12360. tg3_flag_clear(tp, NVRAM_BUFFERED);
  12361. tg3_get_eeprom_size(tp);
  12362. }
  12363. }
  12364. struct subsys_tbl_ent {
  12365. u16 subsys_vendor, subsys_devid;
  12366. u32 phy_id;
  12367. };
  12368. static struct subsys_tbl_ent subsys_id_to_phy_id[] = {
  12369. /* Broadcom boards. */
  12370. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  12371. TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
  12372. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  12373. TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
  12374. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  12375. TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
  12376. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  12377. TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
  12378. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  12379. TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
  12380. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  12381. TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
  12382. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  12383. TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
  12384. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  12385. TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
  12386. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  12387. TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
  12388. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  12389. TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
  12390. { TG3PCI_SUBVENDOR_ID_BROADCOM,
  12391. TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
  12392. /* 3com boards. */
  12393. { TG3PCI_SUBVENDOR_ID_3COM,
  12394. TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
  12395. { TG3PCI_SUBVENDOR_ID_3COM,
  12396. TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
  12397. { TG3PCI_SUBVENDOR_ID_3COM,
  12398. TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
  12399. { TG3PCI_SUBVENDOR_ID_3COM,
  12400. TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
  12401. { TG3PCI_SUBVENDOR_ID_3COM,
  12402. TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
  12403. /* DELL boards. */
  12404. { TG3PCI_SUBVENDOR_ID_DELL,
  12405. TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
  12406. { TG3PCI_SUBVENDOR_ID_DELL,
  12407. TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
  12408. { TG3PCI_SUBVENDOR_ID_DELL,
  12409. TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
  12410. { TG3PCI_SUBVENDOR_ID_DELL,
  12411. TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
  12412. /* Compaq boards. */
  12413. { TG3PCI_SUBVENDOR_ID_COMPAQ,
  12414. TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
  12415. { TG3PCI_SUBVENDOR_ID_COMPAQ,
  12416. TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
  12417. { TG3PCI_SUBVENDOR_ID_COMPAQ,
  12418. TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
  12419. { TG3PCI_SUBVENDOR_ID_COMPAQ,
  12420. TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
  12421. { TG3PCI_SUBVENDOR_ID_COMPAQ,
  12422. TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
  12423. /* IBM boards. */
  12424. { TG3PCI_SUBVENDOR_ID_IBM,
  12425. TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
  12426. };
  12427. static struct subsys_tbl_ent *tg3_lookup_by_subsys(struct tg3 *tp)
  12428. {
  12429. int i;
  12430. for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
  12431. if ((subsys_id_to_phy_id[i].subsys_vendor ==
  12432. tp->pdev->subsystem_vendor) &&
  12433. (subsys_id_to_phy_id[i].subsys_devid ==
  12434. tp->pdev->subsystem_device))
  12435. return &subsys_id_to_phy_id[i];
  12436. }
  12437. return NULL;
  12438. }
  12439. static void tg3_get_eeprom_hw_cfg(struct tg3 *tp)
  12440. {
  12441. u32 val;
  12442. tp->phy_id = TG3_PHY_ID_INVALID;
  12443. tp->led_ctrl = LED_CTRL_MODE_PHY_1;
  12444. /* Assume an onboard device and WOL capable by default. */
  12445. tg3_flag_set(tp, EEPROM_WRITE_PROT);
  12446. tg3_flag_set(tp, WOL_CAP);
  12447. if (tg3_asic_rev(tp) == ASIC_REV_5906) {
  12448. if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
  12449. tg3_flag_clear(tp, EEPROM_WRITE_PROT);
  12450. tg3_flag_set(tp, IS_NIC);
  12451. }
  12452. val = tr32(VCPU_CFGSHDW);
  12453. if (val & VCPU_CFGSHDW_ASPM_DBNC)
  12454. tg3_flag_set(tp, ASPM_WORKAROUND);
  12455. if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
  12456. (val & VCPU_CFGSHDW_WOL_MAGPKT)) {
  12457. tg3_flag_set(tp, WOL_ENABLE);
  12458. device_set_wakeup_enable(&tp->pdev->dev, true);
  12459. }
  12460. goto done;
  12461. }
  12462. tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
  12463. if (val == NIC_SRAM_DATA_SIG_MAGIC) {
  12464. u32 nic_cfg, led_cfg;
  12465. u32 cfg2 = 0, cfg4 = 0, cfg5 = 0;
  12466. u32 nic_phy_id, ver, eeprom_phy_id;
  12467. int eeprom_phy_serdes = 0;
  12468. tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
  12469. tp->nic_sram_data_cfg = nic_cfg;
  12470. tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
  12471. ver >>= NIC_SRAM_DATA_VER_SHIFT;
  12472. if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
  12473. tg3_asic_rev(tp) != ASIC_REV_5701 &&
  12474. tg3_asic_rev(tp) != ASIC_REV_5703 &&
  12475. (ver > 0) && (ver < 0x100))
  12476. tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
  12477. if (tg3_asic_rev(tp) == ASIC_REV_5785)
  12478. tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
  12479. if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
  12480. tg3_asic_rev(tp) == ASIC_REV_5719 ||
  12481. tg3_asic_rev(tp) == ASIC_REV_5720)
  12482. tg3_read_mem(tp, NIC_SRAM_DATA_CFG_5, &cfg5);
  12483. if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
  12484. NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
  12485. eeprom_phy_serdes = 1;
  12486. tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
  12487. if (nic_phy_id != 0) {
  12488. u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
  12489. u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
  12490. eeprom_phy_id = (id1 >> 16) << 10;
  12491. eeprom_phy_id |= (id2 & 0xfc00) << 16;
  12492. eeprom_phy_id |= (id2 & 0x03ff) << 0;
  12493. } else
  12494. eeprom_phy_id = 0;
  12495. tp->phy_id = eeprom_phy_id;
  12496. if (eeprom_phy_serdes) {
  12497. if (!tg3_flag(tp, 5705_PLUS))
  12498. tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
  12499. else
  12500. tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
  12501. }
  12502. if (tg3_flag(tp, 5750_PLUS))
  12503. led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
  12504. SHASTA_EXT_LED_MODE_MASK);
  12505. else
  12506. led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
  12507. switch (led_cfg) {
  12508. default:
  12509. case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
  12510. tp->led_ctrl = LED_CTRL_MODE_PHY_1;
  12511. break;
  12512. case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
  12513. tp->led_ctrl = LED_CTRL_MODE_PHY_2;
  12514. break;
  12515. case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
  12516. tp->led_ctrl = LED_CTRL_MODE_MAC;
  12517. /* Default to PHY_1_MODE if 0 (MAC_MODE) is
  12518. * read on some older 5700/5701 bootcode.
  12519. */
  12520. if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
  12521. tg3_asic_rev(tp) == ASIC_REV_5701)
  12522. tp->led_ctrl = LED_CTRL_MODE_PHY_1;
  12523. break;
  12524. case SHASTA_EXT_LED_SHARED:
  12525. tp->led_ctrl = LED_CTRL_MODE_SHARED;
  12526. if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0 &&
  12527. tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A1)
  12528. tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
  12529. LED_CTRL_MODE_PHY_2);
  12530. if (tg3_flag(tp, 5717_PLUS) ||
  12531. tg3_asic_rev(tp) == ASIC_REV_5762)
  12532. tp->led_ctrl |= LED_CTRL_BLINK_RATE_OVERRIDE |
  12533. LED_CTRL_BLINK_RATE_MASK;
  12534. break;
  12535. case SHASTA_EXT_LED_MAC:
  12536. tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
  12537. break;
  12538. case SHASTA_EXT_LED_COMBO:
  12539. tp->led_ctrl = LED_CTRL_MODE_COMBO;
  12540. if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0)
  12541. tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
  12542. LED_CTRL_MODE_PHY_2);
  12543. break;
  12544. }
  12545. if ((tg3_asic_rev(tp) == ASIC_REV_5700 ||
  12546. tg3_asic_rev(tp) == ASIC_REV_5701) &&
  12547. tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
  12548. tp->led_ctrl = LED_CTRL_MODE_PHY_2;
  12549. if (tg3_chip_rev(tp) == CHIPREV_5784_AX)
  12550. tp->led_ctrl = LED_CTRL_MODE_PHY_1;
  12551. if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
  12552. tg3_flag_set(tp, EEPROM_WRITE_PROT);
  12553. if ((tp->pdev->subsystem_vendor ==
  12554. PCI_VENDOR_ID_ARIMA) &&
  12555. (tp->pdev->subsystem_device == 0x205a ||
  12556. tp->pdev->subsystem_device == 0x2063))
  12557. tg3_flag_clear(tp, EEPROM_WRITE_PROT);
  12558. } else {
  12559. tg3_flag_clear(tp, EEPROM_WRITE_PROT);
  12560. tg3_flag_set(tp, IS_NIC);
  12561. }
  12562. if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
  12563. tg3_flag_set(tp, ENABLE_ASF);
  12564. if (tg3_flag(tp, 5750_PLUS))
  12565. tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
  12566. }
  12567. if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
  12568. tg3_flag(tp, 5750_PLUS))
  12569. tg3_flag_set(tp, ENABLE_APE);
  12570. if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
  12571. !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
  12572. tg3_flag_clear(tp, WOL_CAP);
  12573. if (tg3_flag(tp, WOL_CAP) &&
  12574. (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE)) {
  12575. tg3_flag_set(tp, WOL_ENABLE);
  12576. device_set_wakeup_enable(&tp->pdev->dev, true);
  12577. }
  12578. if (cfg2 & (1 << 17))
  12579. tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
  12580. /* serdes signal pre-emphasis in register 0x590 set by */
  12581. /* bootcode if bit 18 is set */
  12582. if (cfg2 & (1 << 18))
  12583. tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
  12584. if ((tg3_flag(tp, 57765_PLUS) ||
  12585. (tg3_asic_rev(tp) == ASIC_REV_5784 &&
  12586. tg3_chip_rev(tp) != CHIPREV_5784_AX)) &&
  12587. (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
  12588. tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
  12589. if (tg3_flag(tp, PCI_EXPRESS)) {
  12590. u32 cfg3;
  12591. tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
  12592. if (tg3_asic_rev(tp) != ASIC_REV_5785 &&
  12593. !tg3_flag(tp, 57765_PLUS) &&
  12594. (cfg3 & NIC_SRAM_ASPM_DEBOUNCE))
  12595. tg3_flag_set(tp, ASPM_WORKAROUND);
  12596. if (cfg3 & NIC_SRAM_LNK_FLAP_AVOID)
  12597. tp->phy_flags |= TG3_PHYFLG_KEEP_LINK_ON_PWRDN;
  12598. if (cfg3 & NIC_SRAM_1G_ON_VAUX_OK)
  12599. tp->phy_flags |= TG3_PHYFLG_1G_ON_VAUX_OK;
  12600. }
  12601. if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
  12602. tg3_flag_set(tp, RGMII_INBAND_DISABLE);
  12603. if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
  12604. tg3_flag_set(tp, RGMII_EXT_IBND_RX_EN);
  12605. if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
  12606. tg3_flag_set(tp, RGMII_EXT_IBND_TX_EN);
  12607. if (cfg5 & NIC_SRAM_DISABLE_1G_HALF_ADV)
  12608. tp->phy_flags |= TG3_PHYFLG_DISABLE_1G_HD_ADV;
  12609. }
  12610. done:
  12611. if (tg3_flag(tp, WOL_CAP))
  12612. device_set_wakeup_enable(&tp->pdev->dev,
  12613. tg3_flag(tp, WOL_ENABLE));
  12614. else
  12615. device_set_wakeup_capable(&tp->pdev->dev, false);
  12616. }
  12617. static int tg3_ape_otp_read(struct tg3 *tp, u32 offset, u32 *val)
  12618. {
  12619. int i, err;
  12620. u32 val2, off = offset * 8;
  12621. err = tg3_nvram_lock(tp);
  12622. if (err)
  12623. return err;
  12624. tg3_ape_write32(tp, TG3_APE_OTP_ADDR, off | APE_OTP_ADDR_CPU_ENABLE);
  12625. tg3_ape_write32(tp, TG3_APE_OTP_CTRL, APE_OTP_CTRL_PROG_EN |
  12626. APE_OTP_CTRL_CMD_RD | APE_OTP_CTRL_START);
  12627. tg3_ape_read32(tp, TG3_APE_OTP_CTRL);
  12628. udelay(10);
  12629. for (i = 0; i < 100; i++) {
  12630. val2 = tg3_ape_read32(tp, TG3_APE_OTP_STATUS);
  12631. if (val2 & APE_OTP_STATUS_CMD_DONE) {
  12632. *val = tg3_ape_read32(tp, TG3_APE_OTP_RD_DATA);
  12633. break;
  12634. }
  12635. udelay(10);
  12636. }
  12637. tg3_ape_write32(tp, TG3_APE_OTP_CTRL, 0);
  12638. tg3_nvram_unlock(tp);
  12639. if (val2 & APE_OTP_STATUS_CMD_DONE)
  12640. return 0;
  12641. return -EBUSY;
  12642. }
  12643. static int tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
  12644. {
  12645. int i;
  12646. u32 val;
  12647. tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
  12648. tw32(OTP_CTRL, cmd);
  12649. /* Wait for up to 1 ms for command to execute. */
  12650. for (i = 0; i < 100; i++) {
  12651. val = tr32(OTP_STATUS);
  12652. if (val & OTP_STATUS_CMD_DONE)
  12653. break;
  12654. udelay(10);
  12655. }
  12656. return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
  12657. }
  12658. /* Read the gphy configuration from the OTP region of the chip. The gphy
  12659. * configuration is a 32-bit value that straddles the alignment boundary.
  12660. * We do two 32-bit reads and then shift and merge the results.
  12661. */
  12662. static u32 tg3_read_otp_phycfg(struct tg3 *tp)
  12663. {
  12664. u32 bhalf_otp, thalf_otp;
  12665. tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
  12666. if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
  12667. return 0;
  12668. tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
  12669. if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
  12670. return 0;
  12671. thalf_otp = tr32(OTP_READ_DATA);
  12672. tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
  12673. if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
  12674. return 0;
  12675. bhalf_otp = tr32(OTP_READ_DATA);
  12676. return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
  12677. }
  12678. static void tg3_phy_init_link_config(struct tg3 *tp)
  12679. {
  12680. u32 adv = ADVERTISED_Autoneg;
  12681. if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
  12682. if (!(tp->phy_flags & TG3_PHYFLG_DISABLE_1G_HD_ADV))
  12683. adv |= ADVERTISED_1000baseT_Half;
  12684. adv |= ADVERTISED_1000baseT_Full;
  12685. }
  12686. if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
  12687. adv |= ADVERTISED_100baseT_Half |
  12688. ADVERTISED_100baseT_Full |
  12689. ADVERTISED_10baseT_Half |
  12690. ADVERTISED_10baseT_Full |
  12691. ADVERTISED_TP;
  12692. else
  12693. adv |= ADVERTISED_FIBRE;
  12694. tp->link_config.advertising = adv;
  12695. tp->link_config.speed = SPEED_UNKNOWN;
  12696. tp->link_config.duplex = DUPLEX_UNKNOWN;
  12697. tp->link_config.autoneg = AUTONEG_ENABLE;
  12698. tp->link_config.active_speed = SPEED_UNKNOWN;
  12699. tp->link_config.active_duplex = DUPLEX_UNKNOWN;
  12700. tp->old_link = -1;
  12701. }
  12702. static int tg3_phy_probe(struct tg3 *tp)
  12703. {
  12704. u32 hw_phy_id_1, hw_phy_id_2;
  12705. u32 hw_phy_id, hw_phy_id_masked;
  12706. int err;
  12707. /* flow control autonegotiation is default behavior */
  12708. tg3_flag_set(tp, PAUSE_AUTONEG);
  12709. tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
  12710. if (tg3_flag(tp, ENABLE_APE)) {
  12711. switch (tp->pci_fn) {
  12712. case 0:
  12713. tp->phy_ape_lock = TG3_APE_LOCK_PHY0;
  12714. break;
  12715. case 1:
  12716. tp->phy_ape_lock = TG3_APE_LOCK_PHY1;
  12717. break;
  12718. case 2:
  12719. tp->phy_ape_lock = TG3_APE_LOCK_PHY2;
  12720. break;
  12721. case 3:
  12722. tp->phy_ape_lock = TG3_APE_LOCK_PHY3;
  12723. break;
  12724. }
  12725. }
  12726. if (!tg3_flag(tp, ENABLE_ASF) &&
  12727. !(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
  12728. !(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
  12729. tp->phy_flags &= ~(TG3_PHYFLG_1G_ON_VAUX_OK |
  12730. TG3_PHYFLG_KEEP_LINK_ON_PWRDN);
  12731. if (tg3_flag(tp, USE_PHYLIB))
  12732. return tg3_phy_init(tp);
  12733. /* Reading the PHY ID register can conflict with ASF
  12734. * firmware access to the PHY hardware.
  12735. */
  12736. err = 0;
  12737. if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)) {
  12738. hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
  12739. } else {
  12740. /* Now read the physical PHY_ID from the chip and verify
  12741. * that it is sane. If it doesn't look good, we fall back
  12742. * to either the hard-coded table based PHY_ID and failing
  12743. * that the value found in the eeprom area.
  12744. */
  12745. err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
  12746. err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
  12747. hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
  12748. hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
  12749. hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
  12750. hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
  12751. }
  12752. if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
  12753. tp->phy_id = hw_phy_id;
  12754. if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
  12755. tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
  12756. else
  12757. tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
  12758. } else {
  12759. if (tp->phy_id != TG3_PHY_ID_INVALID) {
  12760. /* Do nothing, phy ID already set up in
  12761. * tg3_get_eeprom_hw_cfg().
  12762. */
  12763. } else {
  12764. struct subsys_tbl_ent *p;
  12765. /* No eeprom signature? Try the hardcoded
  12766. * subsys device table.
  12767. */
  12768. p = tg3_lookup_by_subsys(tp);
  12769. if (p) {
  12770. tp->phy_id = p->phy_id;
  12771. } else if (!tg3_flag(tp, IS_SSB_CORE)) {
  12772. /* For now we saw the IDs 0xbc050cd0,
  12773. * 0xbc050f80 and 0xbc050c30 on devices
  12774. * connected to an BCM4785 and there are
  12775. * probably more. Just assume that the phy is
  12776. * supported when it is connected to a SSB core
  12777. * for now.
  12778. */
  12779. return -ENODEV;
  12780. }
  12781. if (!tp->phy_id ||
  12782. tp->phy_id == TG3_PHY_ID_BCM8002)
  12783. tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
  12784. }
  12785. }
  12786. if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
  12787. (tg3_asic_rev(tp) == ASIC_REV_5719 ||
  12788. tg3_asic_rev(tp) == ASIC_REV_5720 ||
  12789. tg3_asic_rev(tp) == ASIC_REV_57766 ||
  12790. tg3_asic_rev(tp) == ASIC_REV_5762 ||
  12791. (tg3_asic_rev(tp) == ASIC_REV_5717 &&
  12792. tg3_chip_rev_id(tp) != CHIPREV_ID_5717_A0) ||
  12793. (tg3_asic_rev(tp) == ASIC_REV_57765 &&
  12794. tg3_chip_rev_id(tp) != CHIPREV_ID_57765_A0))) {
  12795. tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
  12796. tp->eee.supported = SUPPORTED_100baseT_Full |
  12797. SUPPORTED_1000baseT_Full;
  12798. tp->eee.advertised = ADVERTISED_100baseT_Full |
  12799. ADVERTISED_1000baseT_Full;
  12800. tp->eee.eee_enabled = 1;
  12801. tp->eee.tx_lpi_enabled = 1;
  12802. tp->eee.tx_lpi_timer = TG3_CPMU_DBTMR1_LNKIDLE_2047US;
  12803. }
  12804. tg3_phy_init_link_config(tp);
  12805. if (!(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN) &&
  12806. !(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
  12807. !tg3_flag(tp, ENABLE_APE) &&
  12808. !tg3_flag(tp, ENABLE_ASF)) {
  12809. u32 bmsr, dummy;
  12810. tg3_readphy(tp, MII_BMSR, &bmsr);
  12811. if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
  12812. (bmsr & BMSR_LSTATUS))
  12813. goto skip_phy_reset;
  12814. err = tg3_phy_reset(tp);
  12815. if (err)
  12816. return err;
  12817. tg3_phy_set_wirespeed(tp);
  12818. if (!tg3_phy_copper_an_config_ok(tp, &dummy)) {
  12819. tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
  12820. tp->link_config.flowctrl);
  12821. tg3_writephy(tp, MII_BMCR,
  12822. BMCR_ANENABLE | BMCR_ANRESTART);
  12823. }
  12824. }
  12825. skip_phy_reset:
  12826. if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
  12827. err = tg3_init_5401phy_dsp(tp);
  12828. if (err)
  12829. return err;
  12830. err = tg3_init_5401phy_dsp(tp);
  12831. }
  12832. return err;
  12833. }
  12834. static void tg3_read_vpd(struct tg3 *tp)
  12835. {
  12836. u8 *vpd_data;
  12837. unsigned int block_end, rosize, len;
  12838. u32 vpdlen;
  12839. int j, i = 0;
  12840. vpd_data = (u8 *)tg3_vpd_readblock(tp, &vpdlen);
  12841. if (!vpd_data)
  12842. goto out_no_vpd;
  12843. i = pci_vpd_find_tag(vpd_data, 0, vpdlen, PCI_VPD_LRDT_RO_DATA);
  12844. if (i < 0)
  12845. goto out_not_found;
  12846. rosize = pci_vpd_lrdt_size(&vpd_data[i]);
  12847. block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
  12848. i += PCI_VPD_LRDT_TAG_SIZE;
  12849. if (block_end > vpdlen)
  12850. goto out_not_found;
  12851. j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
  12852. PCI_VPD_RO_KEYWORD_MFR_ID);
  12853. if (j > 0) {
  12854. len = pci_vpd_info_field_size(&vpd_data[j]);
  12855. j += PCI_VPD_INFO_FLD_HDR_SIZE;
  12856. if (j + len > block_end || len != 4 ||
  12857. memcmp(&vpd_data[j], "1028", 4))
  12858. goto partno;
  12859. j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
  12860. PCI_VPD_RO_KEYWORD_VENDOR0);
  12861. if (j < 0)
  12862. goto partno;
  12863. len = pci_vpd_info_field_size(&vpd_data[j]);
  12864. j += PCI_VPD_INFO_FLD_HDR_SIZE;
  12865. if (j + len > block_end)
  12866. goto partno;
  12867. if (len >= sizeof(tp->fw_ver))
  12868. len = sizeof(tp->fw_ver) - 1;
  12869. memset(tp->fw_ver, 0, sizeof(tp->fw_ver));
  12870. snprintf(tp->fw_ver, sizeof(tp->fw_ver), "%.*s bc ", len,
  12871. &vpd_data[j]);
  12872. }
  12873. partno:
  12874. i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
  12875. PCI_VPD_RO_KEYWORD_PARTNO);
  12876. if (i < 0)
  12877. goto out_not_found;
  12878. len = pci_vpd_info_field_size(&vpd_data[i]);
  12879. i += PCI_VPD_INFO_FLD_HDR_SIZE;
  12880. if (len > TG3_BPN_SIZE ||
  12881. (len + i) > vpdlen)
  12882. goto out_not_found;
  12883. memcpy(tp->board_part_number, &vpd_data[i], len);
  12884. out_not_found:
  12885. kfree(vpd_data);
  12886. if (tp->board_part_number[0])
  12887. return;
  12888. out_no_vpd:
  12889. if (tg3_asic_rev(tp) == ASIC_REV_5717) {
  12890. if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
  12891. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C)
  12892. strcpy(tp->board_part_number, "BCM5717");
  12893. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
  12894. strcpy(tp->board_part_number, "BCM5718");
  12895. else
  12896. goto nomatch;
  12897. } else if (tg3_asic_rev(tp) == ASIC_REV_57780) {
  12898. if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
  12899. strcpy(tp->board_part_number, "BCM57780");
  12900. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
  12901. strcpy(tp->board_part_number, "BCM57760");
  12902. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
  12903. strcpy(tp->board_part_number, "BCM57790");
  12904. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
  12905. strcpy(tp->board_part_number, "BCM57788");
  12906. else
  12907. goto nomatch;
  12908. } else if (tg3_asic_rev(tp) == ASIC_REV_57765) {
  12909. if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
  12910. strcpy(tp->board_part_number, "BCM57761");
  12911. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
  12912. strcpy(tp->board_part_number, "BCM57765");
  12913. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
  12914. strcpy(tp->board_part_number, "BCM57781");
  12915. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
  12916. strcpy(tp->board_part_number, "BCM57785");
  12917. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
  12918. strcpy(tp->board_part_number, "BCM57791");
  12919. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
  12920. strcpy(tp->board_part_number, "BCM57795");
  12921. else
  12922. goto nomatch;
  12923. } else if (tg3_asic_rev(tp) == ASIC_REV_57766) {
  12924. if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762)
  12925. strcpy(tp->board_part_number, "BCM57762");
  12926. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766)
  12927. strcpy(tp->board_part_number, "BCM57766");
  12928. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782)
  12929. strcpy(tp->board_part_number, "BCM57782");
  12930. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
  12931. strcpy(tp->board_part_number, "BCM57786");
  12932. else
  12933. goto nomatch;
  12934. } else if (tg3_asic_rev(tp) == ASIC_REV_5906) {
  12935. strcpy(tp->board_part_number, "BCM95906");
  12936. } else {
  12937. nomatch:
  12938. strcpy(tp->board_part_number, "none");
  12939. }
  12940. }
  12941. static int tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
  12942. {
  12943. u32 val;
  12944. if (tg3_nvram_read(tp, offset, &val) ||
  12945. (val & 0xfc000000) != 0x0c000000 ||
  12946. tg3_nvram_read(tp, offset + 4, &val) ||
  12947. val != 0)
  12948. return 0;
  12949. return 1;
  12950. }
  12951. static void tg3_read_bc_ver(struct tg3 *tp)
  12952. {
  12953. u32 val, offset, start, ver_offset;
  12954. int i, dst_off;
  12955. bool newver = false;
  12956. if (tg3_nvram_read(tp, 0xc, &offset) ||
  12957. tg3_nvram_read(tp, 0x4, &start))
  12958. return;
  12959. offset = tg3_nvram_logical_addr(tp, offset);
  12960. if (tg3_nvram_read(tp, offset, &val))
  12961. return;
  12962. if ((val & 0xfc000000) == 0x0c000000) {
  12963. if (tg3_nvram_read(tp, offset + 4, &val))
  12964. return;
  12965. if (val == 0)
  12966. newver = true;
  12967. }
  12968. dst_off = strlen(tp->fw_ver);
  12969. if (newver) {
  12970. if (TG3_VER_SIZE - dst_off < 16 ||
  12971. tg3_nvram_read(tp, offset + 8, &ver_offset))
  12972. return;
  12973. offset = offset + ver_offset - start;
  12974. for (i = 0; i < 16; i += 4) {
  12975. __be32 v;
  12976. if (tg3_nvram_read_be32(tp, offset + i, &v))
  12977. return;
  12978. memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
  12979. }
  12980. } else {
  12981. u32 major, minor;
  12982. if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
  12983. return;
  12984. major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
  12985. TG3_NVM_BCVER_MAJSFT;
  12986. minor = ver_offset & TG3_NVM_BCVER_MINMSK;
  12987. snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
  12988. "v%d.%02d", major, minor);
  12989. }
  12990. }
  12991. static void tg3_read_hwsb_ver(struct tg3 *tp)
  12992. {
  12993. u32 val, major, minor;
  12994. /* Use native endian representation */
  12995. if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
  12996. return;
  12997. major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
  12998. TG3_NVM_HWSB_CFG1_MAJSFT;
  12999. minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
  13000. TG3_NVM_HWSB_CFG1_MINSFT;
  13001. snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
  13002. }
  13003. static void tg3_read_sb_ver(struct tg3 *tp, u32 val)
  13004. {
  13005. u32 offset, major, minor, build;
  13006. strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
  13007. if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
  13008. return;
  13009. switch (val & TG3_EEPROM_SB_REVISION_MASK) {
  13010. case TG3_EEPROM_SB_REVISION_0:
  13011. offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
  13012. break;
  13013. case TG3_EEPROM_SB_REVISION_2:
  13014. offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
  13015. break;
  13016. case TG3_EEPROM_SB_REVISION_3:
  13017. offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
  13018. break;
  13019. case TG3_EEPROM_SB_REVISION_4:
  13020. offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
  13021. break;
  13022. case TG3_EEPROM_SB_REVISION_5:
  13023. offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
  13024. break;
  13025. case TG3_EEPROM_SB_REVISION_6:
  13026. offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
  13027. break;
  13028. default:
  13029. return;
  13030. }
  13031. if (tg3_nvram_read(tp, offset, &val))
  13032. return;
  13033. build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
  13034. TG3_EEPROM_SB_EDH_BLD_SHFT;
  13035. major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
  13036. TG3_EEPROM_SB_EDH_MAJ_SHFT;
  13037. minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
  13038. if (minor > 99 || build > 26)
  13039. return;
  13040. offset = strlen(tp->fw_ver);
  13041. snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
  13042. " v%d.%02d", major, minor);
  13043. if (build > 0) {
  13044. offset = strlen(tp->fw_ver);
  13045. if (offset < TG3_VER_SIZE - 1)
  13046. tp->fw_ver[offset] = 'a' + build - 1;
  13047. }
  13048. }
  13049. static void tg3_read_mgmtfw_ver(struct tg3 *tp)
  13050. {
  13051. u32 val, offset, start;
  13052. int i, vlen;
  13053. for (offset = TG3_NVM_DIR_START;
  13054. offset < TG3_NVM_DIR_END;
  13055. offset += TG3_NVM_DIRENT_SIZE) {
  13056. if (tg3_nvram_read(tp, offset, &val))
  13057. return;
  13058. if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
  13059. break;
  13060. }
  13061. if (offset == TG3_NVM_DIR_END)
  13062. return;
  13063. if (!tg3_flag(tp, 5705_PLUS))
  13064. start = 0x08000000;
  13065. else if (tg3_nvram_read(tp, offset - 4, &start))
  13066. return;
  13067. if (tg3_nvram_read(tp, offset + 4, &offset) ||
  13068. !tg3_fw_img_is_valid(tp, offset) ||
  13069. tg3_nvram_read(tp, offset + 8, &val))
  13070. return;
  13071. offset += val - start;
  13072. vlen = strlen(tp->fw_ver);
  13073. tp->fw_ver[vlen++] = ',';
  13074. tp->fw_ver[vlen++] = ' ';
  13075. for (i = 0; i < 4; i++) {
  13076. __be32 v;
  13077. if (tg3_nvram_read_be32(tp, offset, &v))
  13078. return;
  13079. offset += sizeof(v);
  13080. if (vlen > TG3_VER_SIZE - sizeof(v)) {
  13081. memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
  13082. break;
  13083. }
  13084. memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
  13085. vlen += sizeof(v);
  13086. }
  13087. }
  13088. static void tg3_probe_ncsi(struct tg3 *tp)
  13089. {
  13090. u32 apedata;
  13091. apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
  13092. if (apedata != APE_SEG_SIG_MAGIC)
  13093. return;
  13094. apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
  13095. if (!(apedata & APE_FW_STATUS_READY))
  13096. return;
  13097. if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI)
  13098. tg3_flag_set(tp, APE_HAS_NCSI);
  13099. }
  13100. static void tg3_read_dash_ver(struct tg3 *tp)
  13101. {
  13102. int vlen;
  13103. u32 apedata;
  13104. char *fwtype;
  13105. apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
  13106. if (tg3_flag(tp, APE_HAS_NCSI))
  13107. fwtype = "NCSI";
  13108. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725)
  13109. fwtype = "SMASH";
  13110. else
  13111. fwtype = "DASH";
  13112. vlen = strlen(tp->fw_ver);
  13113. snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
  13114. fwtype,
  13115. (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
  13116. (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
  13117. (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
  13118. (apedata & APE_FW_VERSION_BLDMSK));
  13119. }
  13120. static void tg3_read_otp_ver(struct tg3 *tp)
  13121. {
  13122. u32 val, val2;
  13123. if (tg3_asic_rev(tp) != ASIC_REV_5762)
  13124. return;
  13125. if (!tg3_ape_otp_read(tp, OTP_ADDRESS_MAGIC0, &val) &&
  13126. !tg3_ape_otp_read(tp, OTP_ADDRESS_MAGIC0 + 4, &val2) &&
  13127. TG3_OTP_MAGIC0_VALID(val)) {
  13128. u64 val64 = (u64) val << 32 | val2;
  13129. u32 ver = 0;
  13130. int i, vlen;
  13131. for (i = 0; i < 7; i++) {
  13132. if ((val64 & 0xff) == 0)
  13133. break;
  13134. ver = val64 & 0xff;
  13135. val64 >>= 8;
  13136. }
  13137. vlen = strlen(tp->fw_ver);
  13138. snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " .%02d", ver);
  13139. }
  13140. }
  13141. static void tg3_read_fw_ver(struct tg3 *tp)
  13142. {
  13143. u32 val;
  13144. bool vpd_vers = false;
  13145. if (tp->fw_ver[0] != 0)
  13146. vpd_vers = true;
  13147. if (tg3_flag(tp, NO_NVRAM)) {
  13148. strcat(tp->fw_ver, "sb");
  13149. tg3_read_otp_ver(tp);
  13150. return;
  13151. }
  13152. if (tg3_nvram_read(tp, 0, &val))
  13153. return;
  13154. if (val == TG3_EEPROM_MAGIC)
  13155. tg3_read_bc_ver(tp);
  13156. else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
  13157. tg3_read_sb_ver(tp, val);
  13158. else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
  13159. tg3_read_hwsb_ver(tp);
  13160. if (tg3_flag(tp, ENABLE_ASF)) {
  13161. if (tg3_flag(tp, ENABLE_APE)) {
  13162. tg3_probe_ncsi(tp);
  13163. if (!vpd_vers)
  13164. tg3_read_dash_ver(tp);
  13165. } else if (!vpd_vers) {
  13166. tg3_read_mgmtfw_ver(tp);
  13167. }
  13168. }
  13169. tp->fw_ver[TG3_VER_SIZE - 1] = 0;
  13170. }
  13171. static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
  13172. {
  13173. if (tg3_flag(tp, LRG_PROD_RING_CAP))
  13174. return TG3_RX_RET_MAX_SIZE_5717;
  13175. else if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))
  13176. return TG3_RX_RET_MAX_SIZE_5700;
  13177. else
  13178. return TG3_RX_RET_MAX_SIZE_5705;
  13179. }
  13180. static const struct pci_device_id tg3_write_reorder_chipsets[] = {
  13181. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
  13182. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
  13183. { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
  13184. { },
  13185. };
  13186. static struct pci_dev *tg3_find_peer(struct tg3 *tp)
  13187. {
  13188. struct pci_dev *peer;
  13189. unsigned int func, devnr = tp->pdev->devfn & ~7;
  13190. for (func = 0; func < 8; func++) {
  13191. peer = pci_get_slot(tp->pdev->bus, devnr | func);
  13192. if (peer && peer != tp->pdev)
  13193. break;
  13194. pci_dev_put(peer);
  13195. }
  13196. /* 5704 can be configured in single-port mode, set peer to
  13197. * tp->pdev in that case.
  13198. */
  13199. if (!peer) {
  13200. peer = tp->pdev;
  13201. return peer;
  13202. }
  13203. /*
  13204. * We don't need to keep the refcount elevated; there's no way
  13205. * to remove one half of this device without removing the other
  13206. */
  13207. pci_dev_put(peer);
  13208. return peer;
  13209. }
  13210. static void tg3_detect_asic_rev(struct tg3 *tp, u32 misc_ctrl_reg)
  13211. {
  13212. tp->pci_chip_rev_id = misc_ctrl_reg >> MISC_HOST_CTRL_CHIPREV_SHIFT;
  13213. if (tg3_asic_rev(tp) == ASIC_REV_USE_PROD_ID_REG) {
  13214. u32 reg;
  13215. /* All devices that use the alternate
  13216. * ASIC REV location have a CPMU.
  13217. */
  13218. tg3_flag_set(tp, CPMU_PRESENT);
  13219. if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
  13220. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C ||
  13221. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
  13222. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
  13223. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720 ||
  13224. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57767 ||
  13225. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57764 ||
  13226. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5762 ||
  13227. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725 ||
  13228. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5727 ||
  13229. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57787)
  13230. reg = TG3PCI_GEN2_PRODID_ASICREV;
  13231. else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
  13232. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
  13233. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
  13234. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
  13235. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
  13236. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
  13237. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762 ||
  13238. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766 ||
  13239. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782 ||
  13240. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
  13241. reg = TG3PCI_GEN15_PRODID_ASICREV;
  13242. else
  13243. reg = TG3PCI_PRODID_ASICREV;
  13244. pci_read_config_dword(tp->pdev, reg, &tp->pci_chip_rev_id);
  13245. }
  13246. /* Wrong chip ID in 5752 A0. This code can be removed later
  13247. * as A0 is not in production.
  13248. */
  13249. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5752_A0_HW)
  13250. tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
  13251. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5717_C0)
  13252. tp->pci_chip_rev_id = CHIPREV_ID_5720_A0;
  13253. if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
  13254. tg3_asic_rev(tp) == ASIC_REV_5719 ||
  13255. tg3_asic_rev(tp) == ASIC_REV_5720)
  13256. tg3_flag_set(tp, 5717_PLUS);
  13257. if (tg3_asic_rev(tp) == ASIC_REV_57765 ||
  13258. tg3_asic_rev(tp) == ASIC_REV_57766)
  13259. tg3_flag_set(tp, 57765_CLASS);
  13260. if (tg3_flag(tp, 57765_CLASS) || tg3_flag(tp, 5717_PLUS) ||
  13261. tg3_asic_rev(tp) == ASIC_REV_5762)
  13262. tg3_flag_set(tp, 57765_PLUS);
  13263. /* Intentionally exclude ASIC_REV_5906 */
  13264. if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
  13265. tg3_asic_rev(tp) == ASIC_REV_5787 ||
  13266. tg3_asic_rev(tp) == ASIC_REV_5784 ||
  13267. tg3_asic_rev(tp) == ASIC_REV_5761 ||
  13268. tg3_asic_rev(tp) == ASIC_REV_5785 ||
  13269. tg3_asic_rev(tp) == ASIC_REV_57780 ||
  13270. tg3_flag(tp, 57765_PLUS))
  13271. tg3_flag_set(tp, 5755_PLUS);
  13272. if (tg3_asic_rev(tp) == ASIC_REV_5780 ||
  13273. tg3_asic_rev(tp) == ASIC_REV_5714)
  13274. tg3_flag_set(tp, 5780_CLASS);
  13275. if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
  13276. tg3_asic_rev(tp) == ASIC_REV_5752 ||
  13277. tg3_asic_rev(tp) == ASIC_REV_5906 ||
  13278. tg3_flag(tp, 5755_PLUS) ||
  13279. tg3_flag(tp, 5780_CLASS))
  13280. tg3_flag_set(tp, 5750_PLUS);
  13281. if (tg3_asic_rev(tp) == ASIC_REV_5705 ||
  13282. tg3_flag(tp, 5750_PLUS))
  13283. tg3_flag_set(tp, 5705_PLUS);
  13284. }
  13285. static bool tg3_10_100_only_device(struct tg3 *tp,
  13286. const struct pci_device_id *ent)
  13287. {
  13288. u32 grc_misc_cfg = tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK;
  13289. if ((tg3_asic_rev(tp) == ASIC_REV_5703 &&
  13290. (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
  13291. (tp->phy_flags & TG3_PHYFLG_IS_FET))
  13292. return true;
  13293. if (ent->driver_data & TG3_DRV_DATA_FLAG_10_100_ONLY) {
  13294. if (tg3_asic_rev(tp) == ASIC_REV_5705) {
  13295. if (ent->driver_data & TG3_DRV_DATA_FLAG_5705_10_100)
  13296. return true;
  13297. } else {
  13298. return true;
  13299. }
  13300. }
  13301. return false;
  13302. }
  13303. static int tg3_get_invariants(struct tg3 *tp, const struct pci_device_id *ent)
  13304. {
  13305. u32 misc_ctrl_reg;
  13306. u32 pci_state_reg, grc_misc_cfg;
  13307. u32 val;
  13308. u16 pci_cmd;
  13309. int err;
  13310. /* Force memory write invalidate off. If we leave it on,
  13311. * then on 5700_BX chips we have to enable a workaround.
  13312. * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
  13313. * to match the cacheline size. The Broadcom driver have this
  13314. * workaround but turns MWI off all the times so never uses
  13315. * it. This seems to suggest that the workaround is insufficient.
  13316. */
  13317. pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
  13318. pci_cmd &= ~PCI_COMMAND_INVALIDATE;
  13319. pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
  13320. /* Important! -- Make sure register accesses are byteswapped
  13321. * correctly. Also, for those chips that require it, make
  13322. * sure that indirect register accesses are enabled before
  13323. * the first operation.
  13324. */
  13325. pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
  13326. &misc_ctrl_reg);
  13327. tp->misc_host_ctrl |= (misc_ctrl_reg &
  13328. MISC_HOST_CTRL_CHIPREV);
  13329. pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
  13330. tp->misc_host_ctrl);
  13331. tg3_detect_asic_rev(tp, misc_ctrl_reg);
  13332. /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
  13333. * we need to disable memory and use config. cycles
  13334. * only to access all registers. The 5702/03 chips
  13335. * can mistakenly decode the special cycles from the
  13336. * ICH chipsets as memory write cycles, causing corruption
  13337. * of register and memory space. Only certain ICH bridges
  13338. * will drive special cycles with non-zero data during the
  13339. * address phase which can fall within the 5703's address
  13340. * range. This is not an ICH bug as the PCI spec allows
  13341. * non-zero address during special cycles. However, only
  13342. * these ICH bridges are known to drive non-zero addresses
  13343. * during special cycles.
  13344. *
  13345. * Since special cycles do not cross PCI bridges, we only
  13346. * enable this workaround if the 5703 is on the secondary
  13347. * bus of these ICH bridges.
  13348. */
  13349. if ((tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A1) ||
  13350. (tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A2)) {
  13351. static struct tg3_dev_id {
  13352. u32 vendor;
  13353. u32 device;
  13354. u32 rev;
  13355. } ich_chipsets[] = {
  13356. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
  13357. PCI_ANY_ID },
  13358. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
  13359. PCI_ANY_ID },
  13360. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
  13361. 0xa },
  13362. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
  13363. PCI_ANY_ID },
  13364. { },
  13365. };
  13366. struct tg3_dev_id *pci_id = &ich_chipsets[0];
  13367. struct pci_dev *bridge = NULL;
  13368. while (pci_id->vendor != 0) {
  13369. bridge = pci_get_device(pci_id->vendor, pci_id->device,
  13370. bridge);
  13371. if (!bridge) {
  13372. pci_id++;
  13373. continue;
  13374. }
  13375. if (pci_id->rev != PCI_ANY_ID) {
  13376. if (bridge->revision > pci_id->rev)
  13377. continue;
  13378. }
  13379. if (bridge->subordinate &&
  13380. (bridge->subordinate->number ==
  13381. tp->pdev->bus->number)) {
  13382. tg3_flag_set(tp, ICH_WORKAROUND);
  13383. pci_dev_put(bridge);
  13384. break;
  13385. }
  13386. }
  13387. }
  13388. if (tg3_asic_rev(tp) == ASIC_REV_5701) {
  13389. static struct tg3_dev_id {
  13390. u32 vendor;
  13391. u32 device;
  13392. } bridge_chipsets[] = {
  13393. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
  13394. { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
  13395. { },
  13396. };
  13397. struct tg3_dev_id *pci_id = &bridge_chipsets[0];
  13398. struct pci_dev *bridge = NULL;
  13399. while (pci_id->vendor != 0) {
  13400. bridge = pci_get_device(pci_id->vendor,
  13401. pci_id->device,
  13402. bridge);
  13403. if (!bridge) {
  13404. pci_id++;
  13405. continue;
  13406. }
  13407. if (bridge->subordinate &&
  13408. (bridge->subordinate->number <=
  13409. tp->pdev->bus->number) &&
  13410. (bridge->subordinate->busn_res.end >=
  13411. tp->pdev->bus->number)) {
  13412. tg3_flag_set(tp, 5701_DMA_BUG);
  13413. pci_dev_put(bridge);
  13414. break;
  13415. }
  13416. }
  13417. }
  13418. /* The EPB bridge inside 5714, 5715, and 5780 cannot support
  13419. * DMA addresses > 40-bit. This bridge may have other additional
  13420. * 57xx devices behind it in some 4-port NIC designs for example.
  13421. * Any tg3 device found behind the bridge will also need the 40-bit
  13422. * DMA workaround.
  13423. */
  13424. if (tg3_flag(tp, 5780_CLASS)) {
  13425. tg3_flag_set(tp, 40BIT_DMA_BUG);
  13426. tp->msi_cap = tp->pdev->msi_cap;
  13427. } else {
  13428. struct pci_dev *bridge = NULL;
  13429. do {
  13430. bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
  13431. PCI_DEVICE_ID_SERVERWORKS_EPB,
  13432. bridge);
  13433. if (bridge && bridge->subordinate &&
  13434. (bridge->subordinate->number <=
  13435. tp->pdev->bus->number) &&
  13436. (bridge->subordinate->busn_res.end >=
  13437. tp->pdev->bus->number)) {
  13438. tg3_flag_set(tp, 40BIT_DMA_BUG);
  13439. pci_dev_put(bridge);
  13440. break;
  13441. }
  13442. } while (bridge);
  13443. }
  13444. if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
  13445. tg3_asic_rev(tp) == ASIC_REV_5714)
  13446. tp->pdev_peer = tg3_find_peer(tp);
  13447. /* Determine TSO capabilities */
  13448. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0)
  13449. ; /* Do nothing. HW bug. */
  13450. else if (tg3_flag(tp, 57765_PLUS))
  13451. tg3_flag_set(tp, HW_TSO_3);
  13452. else if (tg3_flag(tp, 5755_PLUS) ||
  13453. tg3_asic_rev(tp) == ASIC_REV_5906)
  13454. tg3_flag_set(tp, HW_TSO_2);
  13455. else if (tg3_flag(tp, 5750_PLUS)) {
  13456. tg3_flag_set(tp, HW_TSO_1);
  13457. tg3_flag_set(tp, TSO_BUG);
  13458. if (tg3_asic_rev(tp) == ASIC_REV_5750 &&
  13459. tg3_chip_rev_id(tp) >= CHIPREV_ID_5750_C2)
  13460. tg3_flag_clear(tp, TSO_BUG);
  13461. } else if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
  13462. tg3_asic_rev(tp) != ASIC_REV_5701 &&
  13463. tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
  13464. tg3_flag_set(tp, FW_TSO);
  13465. tg3_flag_set(tp, TSO_BUG);
  13466. if (tg3_asic_rev(tp) == ASIC_REV_5705)
  13467. tp->fw_needed = FIRMWARE_TG3TSO5;
  13468. else
  13469. tp->fw_needed = FIRMWARE_TG3TSO;
  13470. }
  13471. /* Selectively allow TSO based on operating conditions */
  13472. if (tg3_flag(tp, HW_TSO_1) ||
  13473. tg3_flag(tp, HW_TSO_2) ||
  13474. tg3_flag(tp, HW_TSO_3) ||
  13475. tg3_flag(tp, FW_TSO)) {
  13476. /* For firmware TSO, assume ASF is disabled.
  13477. * We'll disable TSO later if we discover ASF
  13478. * is enabled in tg3_get_eeprom_hw_cfg().
  13479. */
  13480. tg3_flag_set(tp, TSO_CAPABLE);
  13481. } else {
  13482. tg3_flag_clear(tp, TSO_CAPABLE);
  13483. tg3_flag_clear(tp, TSO_BUG);
  13484. tp->fw_needed = NULL;
  13485. }
  13486. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0)
  13487. tp->fw_needed = FIRMWARE_TG3;
  13488. if (tg3_asic_rev(tp) == ASIC_REV_57766)
  13489. tp->fw_needed = FIRMWARE_TG357766;
  13490. tp->irq_max = 1;
  13491. if (tg3_flag(tp, 5750_PLUS)) {
  13492. tg3_flag_set(tp, SUPPORT_MSI);
  13493. if (tg3_chip_rev(tp) == CHIPREV_5750_AX ||
  13494. tg3_chip_rev(tp) == CHIPREV_5750_BX ||
  13495. (tg3_asic_rev(tp) == ASIC_REV_5714 &&
  13496. tg3_chip_rev_id(tp) <= CHIPREV_ID_5714_A2 &&
  13497. tp->pdev_peer == tp->pdev))
  13498. tg3_flag_clear(tp, SUPPORT_MSI);
  13499. if (tg3_flag(tp, 5755_PLUS) ||
  13500. tg3_asic_rev(tp) == ASIC_REV_5906) {
  13501. tg3_flag_set(tp, 1SHOT_MSI);
  13502. }
  13503. if (tg3_flag(tp, 57765_PLUS)) {
  13504. tg3_flag_set(tp, SUPPORT_MSIX);
  13505. tp->irq_max = TG3_IRQ_MAX_VECS;
  13506. }
  13507. }
  13508. tp->txq_max = 1;
  13509. tp->rxq_max = 1;
  13510. if (tp->irq_max > 1) {
  13511. tp->rxq_max = TG3_RSS_MAX_NUM_QS;
  13512. tg3_rss_init_dflt_indir_tbl(tp, TG3_RSS_MAX_NUM_QS);
  13513. if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
  13514. tg3_asic_rev(tp) == ASIC_REV_5720)
  13515. tp->txq_max = tp->irq_max - 1;
  13516. }
  13517. if (tg3_flag(tp, 5755_PLUS) ||
  13518. tg3_asic_rev(tp) == ASIC_REV_5906)
  13519. tg3_flag_set(tp, SHORT_DMA_BUG);
  13520. if (tg3_asic_rev(tp) == ASIC_REV_5719)
  13521. tp->dma_limit = TG3_TX_BD_DMA_MAX_4K;
  13522. if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
  13523. tg3_asic_rev(tp) == ASIC_REV_5719 ||
  13524. tg3_asic_rev(tp) == ASIC_REV_5720 ||
  13525. tg3_asic_rev(tp) == ASIC_REV_5762)
  13526. tg3_flag_set(tp, LRG_PROD_RING_CAP);
  13527. if (tg3_flag(tp, 57765_PLUS) &&
  13528. tg3_chip_rev_id(tp) != CHIPREV_ID_5719_A0)
  13529. tg3_flag_set(tp, USE_JUMBO_BDFLAG);
  13530. if (!tg3_flag(tp, 5705_PLUS) ||
  13531. tg3_flag(tp, 5780_CLASS) ||
  13532. tg3_flag(tp, USE_JUMBO_BDFLAG))
  13533. tg3_flag_set(tp, JUMBO_CAPABLE);
  13534. pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
  13535. &pci_state_reg);
  13536. if (pci_is_pcie(tp->pdev)) {
  13537. u16 lnkctl;
  13538. tg3_flag_set(tp, PCI_EXPRESS);
  13539. pcie_capability_read_word(tp->pdev, PCI_EXP_LNKCTL, &lnkctl);
  13540. if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
  13541. if (tg3_asic_rev(tp) == ASIC_REV_5906) {
  13542. tg3_flag_clear(tp, HW_TSO_2);
  13543. tg3_flag_clear(tp, TSO_CAPABLE);
  13544. }
  13545. if (tg3_asic_rev(tp) == ASIC_REV_5784 ||
  13546. tg3_asic_rev(tp) == ASIC_REV_5761 ||
  13547. tg3_chip_rev_id(tp) == CHIPREV_ID_57780_A0 ||
  13548. tg3_chip_rev_id(tp) == CHIPREV_ID_57780_A1)
  13549. tg3_flag_set(tp, CLKREQ_BUG);
  13550. } else if (tg3_chip_rev_id(tp) == CHIPREV_ID_5717_A0) {
  13551. tg3_flag_set(tp, L1PLLPD_EN);
  13552. }
  13553. } else if (tg3_asic_rev(tp) == ASIC_REV_5785) {
  13554. /* BCM5785 devices are effectively PCIe devices, and should
  13555. * follow PCIe codepaths, but do not have a PCIe capabilities
  13556. * section.
  13557. */
  13558. tg3_flag_set(tp, PCI_EXPRESS);
  13559. } else if (!tg3_flag(tp, 5705_PLUS) ||
  13560. tg3_flag(tp, 5780_CLASS)) {
  13561. tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
  13562. if (!tp->pcix_cap) {
  13563. dev_err(&tp->pdev->dev,
  13564. "Cannot find PCI-X capability, aborting\n");
  13565. return -EIO;
  13566. }
  13567. if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
  13568. tg3_flag_set(tp, PCIX_MODE);
  13569. }
  13570. /* If we have an AMD 762 or VIA K8T800 chipset, write
  13571. * reordering to the mailbox registers done by the host
  13572. * controller can cause major troubles. We read back from
  13573. * every mailbox register write to force the writes to be
  13574. * posted to the chip in order.
  13575. */
  13576. if (pci_dev_present(tg3_write_reorder_chipsets) &&
  13577. !tg3_flag(tp, PCI_EXPRESS))
  13578. tg3_flag_set(tp, MBOX_WRITE_REORDER);
  13579. pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
  13580. &tp->pci_cacheline_sz);
  13581. pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
  13582. &tp->pci_lat_timer);
  13583. if (tg3_asic_rev(tp) == ASIC_REV_5703 &&
  13584. tp->pci_lat_timer < 64) {
  13585. tp->pci_lat_timer = 64;
  13586. pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
  13587. tp->pci_lat_timer);
  13588. }
  13589. /* Important! -- It is critical that the PCI-X hw workaround
  13590. * situation is decided before the first MMIO register access.
  13591. */
  13592. if (tg3_chip_rev(tp) == CHIPREV_5700_BX) {
  13593. /* 5700 BX chips need to have their TX producer index
  13594. * mailboxes written twice to workaround a bug.
  13595. */
  13596. tg3_flag_set(tp, TXD_MBOX_HWBUG);
  13597. /* If we are in PCI-X mode, enable register write workaround.
  13598. *
  13599. * The workaround is to use indirect register accesses
  13600. * for all chip writes not to mailbox registers.
  13601. */
  13602. if (tg3_flag(tp, PCIX_MODE)) {
  13603. u32 pm_reg;
  13604. tg3_flag_set(tp, PCIX_TARGET_HWBUG);
  13605. /* The chip can have it's power management PCI config
  13606. * space registers clobbered due to this bug.
  13607. * So explicitly force the chip into D0 here.
  13608. */
  13609. pci_read_config_dword(tp->pdev,
  13610. tp->pdev->pm_cap + PCI_PM_CTRL,
  13611. &pm_reg);
  13612. pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
  13613. pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
  13614. pci_write_config_dword(tp->pdev,
  13615. tp->pdev->pm_cap + PCI_PM_CTRL,
  13616. pm_reg);
  13617. /* Also, force SERR#/PERR# in PCI command. */
  13618. pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
  13619. pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
  13620. pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
  13621. }
  13622. }
  13623. if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
  13624. tg3_flag_set(tp, PCI_HIGH_SPEED);
  13625. if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
  13626. tg3_flag_set(tp, PCI_32BIT);
  13627. /* Chip-specific fixup from Broadcom driver */
  13628. if ((tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0) &&
  13629. (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
  13630. pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
  13631. pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
  13632. }
  13633. /* Default fast path register access methods */
  13634. tp->read32 = tg3_read32;
  13635. tp->write32 = tg3_write32;
  13636. tp->read32_mbox = tg3_read32;
  13637. tp->write32_mbox = tg3_write32;
  13638. tp->write32_tx_mbox = tg3_write32;
  13639. tp->write32_rx_mbox = tg3_write32;
  13640. /* Various workaround register access methods */
  13641. if (tg3_flag(tp, PCIX_TARGET_HWBUG))
  13642. tp->write32 = tg3_write_indirect_reg32;
  13643. else if (tg3_asic_rev(tp) == ASIC_REV_5701 ||
  13644. (tg3_flag(tp, PCI_EXPRESS) &&
  13645. tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A0)) {
  13646. /*
  13647. * Back to back register writes can cause problems on these
  13648. * chips, the workaround is to read back all reg writes
  13649. * except those to mailbox regs.
  13650. *
  13651. * See tg3_write_indirect_reg32().
  13652. */
  13653. tp->write32 = tg3_write_flush_reg32;
  13654. }
  13655. if (tg3_flag(tp, TXD_MBOX_HWBUG) || tg3_flag(tp, MBOX_WRITE_REORDER)) {
  13656. tp->write32_tx_mbox = tg3_write32_tx_mbox;
  13657. if (tg3_flag(tp, MBOX_WRITE_REORDER))
  13658. tp->write32_rx_mbox = tg3_write_flush_reg32;
  13659. }
  13660. if (tg3_flag(tp, ICH_WORKAROUND)) {
  13661. tp->read32 = tg3_read_indirect_reg32;
  13662. tp->write32 = tg3_write_indirect_reg32;
  13663. tp->read32_mbox = tg3_read_indirect_mbox;
  13664. tp->write32_mbox = tg3_write_indirect_mbox;
  13665. tp->write32_tx_mbox = tg3_write_indirect_mbox;
  13666. tp->write32_rx_mbox = tg3_write_indirect_mbox;
  13667. iounmap(tp->regs);
  13668. tp->regs = NULL;
  13669. pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
  13670. pci_cmd &= ~PCI_COMMAND_MEMORY;
  13671. pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
  13672. }
  13673. if (tg3_asic_rev(tp) == ASIC_REV_5906) {
  13674. tp->read32_mbox = tg3_read32_mbox_5906;
  13675. tp->write32_mbox = tg3_write32_mbox_5906;
  13676. tp->write32_tx_mbox = tg3_write32_mbox_5906;
  13677. tp->write32_rx_mbox = tg3_write32_mbox_5906;
  13678. }
  13679. if (tp->write32 == tg3_write_indirect_reg32 ||
  13680. (tg3_flag(tp, PCIX_MODE) &&
  13681. (tg3_asic_rev(tp) == ASIC_REV_5700 ||
  13682. tg3_asic_rev(tp) == ASIC_REV_5701)))
  13683. tg3_flag_set(tp, SRAM_USE_CONFIG);
  13684. /* The memory arbiter has to be enabled in order for SRAM accesses
  13685. * to succeed. Normally on powerup the tg3 chip firmware will make
  13686. * sure it is enabled, but other entities such as system netboot
  13687. * code might disable it.
  13688. */
  13689. val = tr32(MEMARB_MODE);
  13690. tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
  13691. tp->pci_fn = PCI_FUNC(tp->pdev->devfn) & 3;
  13692. if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
  13693. tg3_flag(tp, 5780_CLASS)) {
  13694. if (tg3_flag(tp, PCIX_MODE)) {
  13695. pci_read_config_dword(tp->pdev,
  13696. tp->pcix_cap + PCI_X_STATUS,
  13697. &val);
  13698. tp->pci_fn = val & 0x7;
  13699. }
  13700. } else if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
  13701. tg3_asic_rev(tp) == ASIC_REV_5719 ||
  13702. tg3_asic_rev(tp) == ASIC_REV_5720) {
  13703. tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
  13704. if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) != NIC_SRAM_CPMUSTAT_SIG)
  13705. val = tr32(TG3_CPMU_STATUS);
  13706. if (tg3_asic_rev(tp) == ASIC_REV_5717)
  13707. tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5717) ? 1 : 0;
  13708. else
  13709. tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5719) >>
  13710. TG3_CPMU_STATUS_FSHFT_5719;
  13711. }
  13712. if (tg3_flag(tp, FLUSH_POSTED_WRITES)) {
  13713. tp->write32_tx_mbox = tg3_write_flush_reg32;
  13714. tp->write32_rx_mbox = tg3_write_flush_reg32;
  13715. }
  13716. /* Get eeprom hw config before calling tg3_set_power_state().
  13717. * In particular, the TG3_FLAG_IS_NIC flag must be
  13718. * determined before calling tg3_set_power_state() so that
  13719. * we know whether or not to switch out of Vaux power.
  13720. * When the flag is set, it means that GPIO1 is used for eeprom
  13721. * write protect and also implies that it is a LOM where GPIOs
  13722. * are not used to switch power.
  13723. */
  13724. tg3_get_eeprom_hw_cfg(tp);
  13725. if (tg3_flag(tp, FW_TSO) && tg3_flag(tp, ENABLE_ASF)) {
  13726. tg3_flag_clear(tp, TSO_CAPABLE);
  13727. tg3_flag_clear(tp, TSO_BUG);
  13728. tp->fw_needed = NULL;
  13729. }
  13730. if (tg3_flag(tp, ENABLE_APE)) {
  13731. /* Allow reads and writes to the
  13732. * APE register and memory space.
  13733. */
  13734. pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
  13735. PCISTATE_ALLOW_APE_SHMEM_WR |
  13736. PCISTATE_ALLOW_APE_PSPACE_WR;
  13737. pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
  13738. pci_state_reg);
  13739. tg3_ape_lock_init(tp);
  13740. }
  13741. /* Set up tp->grc_local_ctrl before calling
  13742. * tg3_pwrsrc_switch_to_vmain(). GPIO1 driven high
  13743. * will bring 5700's external PHY out of reset.
  13744. * It is also used as eeprom write protect on LOMs.
  13745. */
  13746. tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
  13747. if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
  13748. tg3_flag(tp, EEPROM_WRITE_PROT))
  13749. tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
  13750. GRC_LCLCTRL_GPIO_OUTPUT1);
  13751. /* Unused GPIO3 must be driven as output on 5752 because there
  13752. * are no pull-up resistors on unused GPIO pins.
  13753. */
  13754. else if (tg3_asic_rev(tp) == ASIC_REV_5752)
  13755. tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
  13756. if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
  13757. tg3_asic_rev(tp) == ASIC_REV_57780 ||
  13758. tg3_flag(tp, 57765_CLASS))
  13759. tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
  13760. if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
  13761. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
  13762. /* Turn off the debug UART. */
  13763. tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
  13764. if (tg3_flag(tp, IS_NIC))
  13765. /* Keep VMain power. */
  13766. tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
  13767. GRC_LCLCTRL_GPIO_OUTPUT0;
  13768. }
  13769. if (tg3_asic_rev(tp) == ASIC_REV_5762)
  13770. tp->grc_local_ctrl |=
  13771. tr32(GRC_LOCAL_CTRL) & GRC_LCLCTRL_GPIO_UART_SEL;
  13772. /* Switch out of Vaux if it is a NIC */
  13773. tg3_pwrsrc_switch_to_vmain(tp);
  13774. /* Derive initial jumbo mode from MTU assigned in
  13775. * ether_setup() via the alloc_etherdev() call
  13776. */
  13777. if (tp->dev->mtu > ETH_DATA_LEN && !tg3_flag(tp, 5780_CLASS))
  13778. tg3_flag_set(tp, JUMBO_RING_ENABLE);
  13779. /* Determine WakeOnLan speed to use. */
  13780. if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
  13781. tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
  13782. tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0 ||
  13783. tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B2) {
  13784. tg3_flag_clear(tp, WOL_SPEED_100MB);
  13785. } else {
  13786. tg3_flag_set(tp, WOL_SPEED_100MB);
  13787. }
  13788. if (tg3_asic_rev(tp) == ASIC_REV_5906)
  13789. tp->phy_flags |= TG3_PHYFLG_IS_FET;
  13790. /* A few boards don't want Ethernet@WireSpeed phy feature */
  13791. if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
  13792. (tg3_asic_rev(tp) == ASIC_REV_5705 &&
  13793. (tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) &&
  13794. (tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A1)) ||
  13795. (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
  13796. (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
  13797. tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
  13798. if (tg3_chip_rev(tp) == CHIPREV_5703_AX ||
  13799. tg3_chip_rev(tp) == CHIPREV_5704_AX)
  13800. tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
  13801. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0)
  13802. tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
  13803. if (tg3_flag(tp, 5705_PLUS) &&
  13804. !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
  13805. tg3_asic_rev(tp) != ASIC_REV_5785 &&
  13806. tg3_asic_rev(tp) != ASIC_REV_57780 &&
  13807. !tg3_flag(tp, 57765_PLUS)) {
  13808. if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
  13809. tg3_asic_rev(tp) == ASIC_REV_5787 ||
  13810. tg3_asic_rev(tp) == ASIC_REV_5784 ||
  13811. tg3_asic_rev(tp) == ASIC_REV_5761) {
  13812. if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
  13813. tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
  13814. tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
  13815. if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
  13816. tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
  13817. } else
  13818. tp->phy_flags |= TG3_PHYFLG_BER_BUG;
  13819. }
  13820. if (tg3_asic_rev(tp) == ASIC_REV_5784 &&
  13821. tg3_chip_rev(tp) != CHIPREV_5784_AX) {
  13822. tp->phy_otp = tg3_read_otp_phycfg(tp);
  13823. if (tp->phy_otp == 0)
  13824. tp->phy_otp = TG3_OTP_DEFAULT;
  13825. }
  13826. if (tg3_flag(tp, CPMU_PRESENT))
  13827. tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
  13828. else
  13829. tp->mi_mode = MAC_MI_MODE_BASE;
  13830. tp->coalesce_mode = 0;
  13831. if (tg3_chip_rev(tp) != CHIPREV_5700_AX &&
  13832. tg3_chip_rev(tp) != CHIPREV_5700_BX)
  13833. tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
  13834. /* Set these bits to enable statistics workaround. */
  13835. if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
  13836. tg3_asic_rev(tp) == ASIC_REV_5762 ||
  13837. tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
  13838. tg3_chip_rev_id(tp) == CHIPREV_ID_5720_A0) {
  13839. tp->coalesce_mode |= HOSTCC_MODE_ATTN;
  13840. tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
  13841. }
  13842. if (tg3_asic_rev(tp) == ASIC_REV_5785 ||
  13843. tg3_asic_rev(tp) == ASIC_REV_57780)
  13844. tg3_flag_set(tp, USE_PHYLIB);
  13845. err = tg3_mdio_init(tp);
  13846. if (err)
  13847. return err;
  13848. /* Initialize data/descriptor byte/word swapping. */
  13849. val = tr32(GRC_MODE);
  13850. if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
  13851. tg3_asic_rev(tp) == ASIC_REV_5762)
  13852. val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
  13853. GRC_MODE_WORD_SWAP_B2HRX_DATA |
  13854. GRC_MODE_B2HRX_ENABLE |
  13855. GRC_MODE_HTX2B_ENABLE |
  13856. GRC_MODE_HOST_STACKUP);
  13857. else
  13858. val &= GRC_MODE_HOST_STACKUP;
  13859. tw32(GRC_MODE, val | tp->grc_mode);
  13860. tg3_switch_clocks(tp);
  13861. /* Clear this out for sanity. */
  13862. tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
  13863. /* Clear TG3PCI_REG_BASE_ADDR to prevent hangs. */
  13864. tw32(TG3PCI_REG_BASE_ADDR, 0);
  13865. pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
  13866. &pci_state_reg);
  13867. if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
  13868. !tg3_flag(tp, PCIX_TARGET_HWBUG)) {
  13869. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
  13870. tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0 ||
  13871. tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B2 ||
  13872. tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B5) {
  13873. void __iomem *sram_base;
  13874. /* Write some dummy words into the SRAM status block
  13875. * area, see if it reads back correctly. If the return
  13876. * value is bad, force enable the PCIX workaround.
  13877. */
  13878. sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
  13879. writel(0x00000000, sram_base);
  13880. writel(0x00000000, sram_base + 4);
  13881. writel(0xffffffff, sram_base + 4);
  13882. if (readl(sram_base) != 0x00000000)
  13883. tg3_flag_set(tp, PCIX_TARGET_HWBUG);
  13884. }
  13885. }
  13886. udelay(50);
  13887. tg3_nvram_init(tp);
  13888. /* If the device has an NVRAM, no need to load patch firmware */
  13889. if (tg3_asic_rev(tp) == ASIC_REV_57766 &&
  13890. !tg3_flag(tp, NO_NVRAM))
  13891. tp->fw_needed = NULL;
  13892. grc_misc_cfg = tr32(GRC_MISC_CFG);
  13893. grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
  13894. if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
  13895. (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
  13896. grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
  13897. tg3_flag_set(tp, IS_5788);
  13898. if (!tg3_flag(tp, IS_5788) &&
  13899. tg3_asic_rev(tp) != ASIC_REV_5700)
  13900. tg3_flag_set(tp, TAGGED_STATUS);
  13901. if (tg3_flag(tp, TAGGED_STATUS)) {
  13902. tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
  13903. HOSTCC_MODE_CLRTICK_TXBD);
  13904. tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
  13905. pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
  13906. tp->misc_host_ctrl);
  13907. }
  13908. /* Preserve the APE MAC_MODE bits */
  13909. if (tg3_flag(tp, ENABLE_APE))
  13910. tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
  13911. else
  13912. tp->mac_mode = 0;
  13913. if (tg3_10_100_only_device(tp, ent))
  13914. tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
  13915. err = tg3_phy_probe(tp);
  13916. if (err) {
  13917. dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
  13918. /* ... but do not return immediately ... */
  13919. tg3_mdio_fini(tp);
  13920. }
  13921. tg3_read_vpd(tp);
  13922. tg3_read_fw_ver(tp);
  13923. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
  13924. tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
  13925. } else {
  13926. if (tg3_asic_rev(tp) == ASIC_REV_5700)
  13927. tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
  13928. else
  13929. tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
  13930. }
  13931. /* 5700 {AX,BX} chips have a broken status block link
  13932. * change bit implementation, so we must use the
  13933. * status register in those cases.
  13934. */
  13935. if (tg3_asic_rev(tp) == ASIC_REV_5700)
  13936. tg3_flag_set(tp, USE_LINKCHG_REG);
  13937. else
  13938. tg3_flag_clear(tp, USE_LINKCHG_REG);
  13939. /* The led_ctrl is set during tg3_phy_probe, here we might
  13940. * have to force the link status polling mechanism based
  13941. * upon subsystem IDs.
  13942. */
  13943. if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
  13944. tg3_asic_rev(tp) == ASIC_REV_5701 &&
  13945. !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
  13946. tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
  13947. tg3_flag_set(tp, USE_LINKCHG_REG);
  13948. }
  13949. /* For all SERDES we poll the MAC status register. */
  13950. if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
  13951. tg3_flag_set(tp, POLL_SERDES);
  13952. else
  13953. tg3_flag_clear(tp, POLL_SERDES);
  13954. if (tg3_flag(tp, ENABLE_APE) && tg3_flag(tp, ENABLE_ASF))
  13955. tg3_flag_set(tp, POLL_CPMU_LINK);
  13956. tp->rx_offset = NET_SKB_PAD + NET_IP_ALIGN;
  13957. tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
  13958. if (tg3_asic_rev(tp) == ASIC_REV_5701 &&
  13959. tg3_flag(tp, PCIX_MODE)) {
  13960. tp->rx_offset = NET_SKB_PAD;
  13961. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  13962. tp->rx_copy_thresh = ~(u16)0;
  13963. #endif
  13964. }
  13965. tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
  13966. tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
  13967. tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
  13968. tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
  13969. /* Increment the rx prod index on the rx std ring by at most
  13970. * 8 for these chips to workaround hw errata.
  13971. */
  13972. if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
  13973. tg3_asic_rev(tp) == ASIC_REV_5752 ||
  13974. tg3_asic_rev(tp) == ASIC_REV_5755)
  13975. tp->rx_std_max_post = 8;
  13976. if (tg3_flag(tp, ASPM_WORKAROUND))
  13977. tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
  13978. PCIE_PWR_MGMT_L1_THRESH_MSK;
  13979. return err;
  13980. }
  13981. #ifdef CONFIG_SPARC
  13982. static int tg3_get_macaddr_sparc(struct tg3 *tp)
  13983. {
  13984. struct net_device *dev = tp->dev;
  13985. struct pci_dev *pdev = tp->pdev;
  13986. struct device_node *dp = pci_device_to_OF_node(pdev);
  13987. const unsigned char *addr;
  13988. int len;
  13989. addr = of_get_property(dp, "local-mac-address", &len);
  13990. if (addr && len == ETH_ALEN) {
  13991. memcpy(dev->dev_addr, addr, ETH_ALEN);
  13992. return 0;
  13993. }
  13994. return -ENODEV;
  13995. }
  13996. static int tg3_get_default_macaddr_sparc(struct tg3 *tp)
  13997. {
  13998. struct net_device *dev = tp->dev;
  13999. memcpy(dev->dev_addr, idprom->id_ethaddr, ETH_ALEN);
  14000. return 0;
  14001. }
  14002. #endif
  14003. static int tg3_get_device_address(struct tg3 *tp)
  14004. {
  14005. struct net_device *dev = tp->dev;
  14006. u32 hi, lo, mac_offset;
  14007. int addr_ok = 0;
  14008. int err;
  14009. #ifdef CONFIG_SPARC
  14010. if (!tg3_get_macaddr_sparc(tp))
  14011. return 0;
  14012. #endif
  14013. if (tg3_flag(tp, IS_SSB_CORE)) {
  14014. err = ssb_gige_get_macaddr(tp->pdev, &dev->dev_addr[0]);
  14015. if (!err && is_valid_ether_addr(&dev->dev_addr[0]))
  14016. return 0;
  14017. }
  14018. mac_offset = 0x7c;
  14019. if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
  14020. tg3_flag(tp, 5780_CLASS)) {
  14021. if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
  14022. mac_offset = 0xcc;
  14023. if (tg3_nvram_lock(tp))
  14024. tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
  14025. else
  14026. tg3_nvram_unlock(tp);
  14027. } else if (tg3_flag(tp, 5717_PLUS)) {
  14028. if (tp->pci_fn & 1)
  14029. mac_offset = 0xcc;
  14030. if (tp->pci_fn > 1)
  14031. mac_offset += 0x18c;
  14032. } else if (tg3_asic_rev(tp) == ASIC_REV_5906)
  14033. mac_offset = 0x10;
  14034. /* First try to get it from MAC address mailbox. */
  14035. tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
  14036. if ((hi >> 16) == 0x484b) {
  14037. dev->dev_addr[0] = (hi >> 8) & 0xff;
  14038. dev->dev_addr[1] = (hi >> 0) & 0xff;
  14039. tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
  14040. dev->dev_addr[2] = (lo >> 24) & 0xff;
  14041. dev->dev_addr[3] = (lo >> 16) & 0xff;
  14042. dev->dev_addr[4] = (lo >> 8) & 0xff;
  14043. dev->dev_addr[5] = (lo >> 0) & 0xff;
  14044. /* Some old bootcode may report a 0 MAC address in SRAM */
  14045. addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
  14046. }
  14047. if (!addr_ok) {
  14048. /* Next, try NVRAM. */
  14049. if (!tg3_flag(tp, NO_NVRAM) &&
  14050. !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
  14051. !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
  14052. memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
  14053. memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
  14054. }
  14055. /* Finally just fetch it out of the MAC control regs. */
  14056. else {
  14057. hi = tr32(MAC_ADDR_0_HIGH);
  14058. lo = tr32(MAC_ADDR_0_LOW);
  14059. dev->dev_addr[5] = lo & 0xff;
  14060. dev->dev_addr[4] = (lo >> 8) & 0xff;
  14061. dev->dev_addr[3] = (lo >> 16) & 0xff;
  14062. dev->dev_addr[2] = (lo >> 24) & 0xff;
  14063. dev->dev_addr[1] = hi & 0xff;
  14064. dev->dev_addr[0] = (hi >> 8) & 0xff;
  14065. }
  14066. }
  14067. if (!is_valid_ether_addr(&dev->dev_addr[0])) {
  14068. #ifdef CONFIG_SPARC
  14069. if (!tg3_get_default_macaddr_sparc(tp))
  14070. return 0;
  14071. #endif
  14072. return -EINVAL;
  14073. }
  14074. return 0;
  14075. }
  14076. #define BOUNDARY_SINGLE_CACHELINE 1
  14077. #define BOUNDARY_MULTI_CACHELINE 2
  14078. static u32 tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
  14079. {
  14080. int cacheline_size;
  14081. u8 byte;
  14082. int goal;
  14083. pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
  14084. if (byte == 0)
  14085. cacheline_size = 1024;
  14086. else
  14087. cacheline_size = (int) byte * 4;
  14088. /* On 5703 and later chips, the boundary bits have no
  14089. * effect.
  14090. */
  14091. if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
  14092. tg3_asic_rev(tp) != ASIC_REV_5701 &&
  14093. !tg3_flag(tp, PCI_EXPRESS))
  14094. goto out;
  14095. #if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
  14096. goal = BOUNDARY_MULTI_CACHELINE;
  14097. #else
  14098. #if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
  14099. goal = BOUNDARY_SINGLE_CACHELINE;
  14100. #else
  14101. goal = 0;
  14102. #endif
  14103. #endif
  14104. if (tg3_flag(tp, 57765_PLUS)) {
  14105. val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
  14106. goto out;
  14107. }
  14108. if (!goal)
  14109. goto out;
  14110. /* PCI controllers on most RISC systems tend to disconnect
  14111. * when a device tries to burst across a cache-line boundary.
  14112. * Therefore, letting tg3 do so just wastes PCI bandwidth.
  14113. *
  14114. * Unfortunately, for PCI-E there are only limited
  14115. * write-side controls for this, and thus for reads
  14116. * we will still get the disconnects. We'll also waste
  14117. * these PCI cycles for both read and write for chips
  14118. * other than 5700 and 5701 which do not implement the
  14119. * boundary bits.
  14120. */
  14121. if (tg3_flag(tp, PCIX_MODE) && !tg3_flag(tp, PCI_EXPRESS)) {
  14122. switch (cacheline_size) {
  14123. case 16:
  14124. case 32:
  14125. case 64:
  14126. case 128:
  14127. if (goal == BOUNDARY_SINGLE_CACHELINE) {
  14128. val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
  14129. DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
  14130. } else {
  14131. val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
  14132. DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
  14133. }
  14134. break;
  14135. case 256:
  14136. val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
  14137. DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
  14138. break;
  14139. default:
  14140. val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
  14141. DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
  14142. break;
  14143. }
  14144. } else if (tg3_flag(tp, PCI_EXPRESS)) {
  14145. switch (cacheline_size) {
  14146. case 16:
  14147. case 32:
  14148. case 64:
  14149. if (goal == BOUNDARY_SINGLE_CACHELINE) {
  14150. val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
  14151. val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
  14152. break;
  14153. }
  14154. /* fallthrough */
  14155. case 128:
  14156. default:
  14157. val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
  14158. val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
  14159. break;
  14160. }
  14161. } else {
  14162. switch (cacheline_size) {
  14163. case 16:
  14164. if (goal == BOUNDARY_SINGLE_CACHELINE) {
  14165. val |= (DMA_RWCTRL_READ_BNDRY_16 |
  14166. DMA_RWCTRL_WRITE_BNDRY_16);
  14167. break;
  14168. }
  14169. /* fallthrough */
  14170. case 32:
  14171. if (goal == BOUNDARY_SINGLE_CACHELINE) {
  14172. val |= (DMA_RWCTRL_READ_BNDRY_32 |
  14173. DMA_RWCTRL_WRITE_BNDRY_32);
  14174. break;
  14175. }
  14176. /* fallthrough */
  14177. case 64:
  14178. if (goal == BOUNDARY_SINGLE_CACHELINE) {
  14179. val |= (DMA_RWCTRL_READ_BNDRY_64 |
  14180. DMA_RWCTRL_WRITE_BNDRY_64);
  14181. break;
  14182. }
  14183. /* fallthrough */
  14184. case 128:
  14185. if (goal == BOUNDARY_SINGLE_CACHELINE) {
  14186. val |= (DMA_RWCTRL_READ_BNDRY_128 |
  14187. DMA_RWCTRL_WRITE_BNDRY_128);
  14188. break;
  14189. }
  14190. /* fallthrough */
  14191. case 256:
  14192. val |= (DMA_RWCTRL_READ_BNDRY_256 |
  14193. DMA_RWCTRL_WRITE_BNDRY_256);
  14194. break;
  14195. case 512:
  14196. val |= (DMA_RWCTRL_READ_BNDRY_512 |
  14197. DMA_RWCTRL_WRITE_BNDRY_512);
  14198. break;
  14199. case 1024:
  14200. default:
  14201. val |= (DMA_RWCTRL_READ_BNDRY_1024 |
  14202. DMA_RWCTRL_WRITE_BNDRY_1024);
  14203. break;
  14204. }
  14205. }
  14206. out:
  14207. return val;
  14208. }
  14209. static int tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma,
  14210. int size, bool to_device)
  14211. {
  14212. struct tg3_internal_buffer_desc test_desc;
  14213. u32 sram_dma_descs;
  14214. int i, ret;
  14215. sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
  14216. tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
  14217. tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
  14218. tw32(RDMAC_STATUS, 0);
  14219. tw32(WDMAC_STATUS, 0);
  14220. tw32(BUFMGR_MODE, 0);
  14221. tw32(FTQ_RESET, 0);
  14222. test_desc.addr_hi = ((u64) buf_dma) >> 32;
  14223. test_desc.addr_lo = buf_dma & 0xffffffff;
  14224. test_desc.nic_mbuf = 0x00002100;
  14225. test_desc.len = size;
  14226. /*
  14227. * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
  14228. * the *second* time the tg3 driver was getting loaded after an
  14229. * initial scan.
  14230. *
  14231. * Broadcom tells me:
  14232. * ...the DMA engine is connected to the GRC block and a DMA
  14233. * reset may affect the GRC block in some unpredictable way...
  14234. * The behavior of resets to individual blocks has not been tested.
  14235. *
  14236. * Broadcom noted the GRC reset will also reset all sub-components.
  14237. */
  14238. if (to_device) {
  14239. test_desc.cqid_sqid = (13 << 8) | 2;
  14240. tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
  14241. udelay(40);
  14242. } else {
  14243. test_desc.cqid_sqid = (16 << 8) | 7;
  14244. tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
  14245. udelay(40);
  14246. }
  14247. test_desc.flags = 0x00000005;
  14248. for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
  14249. u32 val;
  14250. val = *(((u32 *)&test_desc) + i);
  14251. pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
  14252. sram_dma_descs + (i * sizeof(u32)));
  14253. pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
  14254. }
  14255. pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
  14256. if (to_device)
  14257. tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
  14258. else
  14259. tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
  14260. ret = -ENODEV;
  14261. for (i = 0; i < 40; i++) {
  14262. u32 val;
  14263. if (to_device)
  14264. val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
  14265. else
  14266. val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
  14267. if ((val & 0xffff) == sram_dma_descs) {
  14268. ret = 0;
  14269. break;
  14270. }
  14271. udelay(100);
  14272. }
  14273. return ret;
  14274. }
  14275. #define TEST_BUFFER_SIZE 0x2000
  14276. static const struct pci_device_id tg3_dma_wait_state_chipsets[] = {
  14277. { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
  14278. { },
  14279. };
  14280. static int tg3_test_dma(struct tg3 *tp)
  14281. {
  14282. dma_addr_t buf_dma;
  14283. u32 *buf, saved_dma_rwctrl;
  14284. int ret = 0;
  14285. buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
  14286. &buf_dma, GFP_KERNEL);
  14287. if (!buf) {
  14288. ret = -ENOMEM;
  14289. goto out_nofree;
  14290. }
  14291. tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
  14292. (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
  14293. tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
  14294. if (tg3_flag(tp, 57765_PLUS))
  14295. goto out;
  14296. if (tg3_flag(tp, PCI_EXPRESS)) {
  14297. /* DMA read watermark not used on PCIE */
  14298. tp->dma_rwctrl |= 0x00180000;
  14299. } else if (!tg3_flag(tp, PCIX_MODE)) {
  14300. if (tg3_asic_rev(tp) == ASIC_REV_5705 ||
  14301. tg3_asic_rev(tp) == ASIC_REV_5750)
  14302. tp->dma_rwctrl |= 0x003f0000;
  14303. else
  14304. tp->dma_rwctrl |= 0x003f000f;
  14305. } else {
  14306. if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
  14307. tg3_asic_rev(tp) == ASIC_REV_5704) {
  14308. u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
  14309. u32 read_water = 0x7;
  14310. /* If the 5704 is behind the EPB bridge, we can
  14311. * do the less restrictive ONE_DMA workaround for
  14312. * better performance.
  14313. */
  14314. if (tg3_flag(tp, 40BIT_DMA_BUG) &&
  14315. tg3_asic_rev(tp) == ASIC_REV_5704)
  14316. tp->dma_rwctrl |= 0x8000;
  14317. else if (ccval == 0x6 || ccval == 0x7)
  14318. tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
  14319. if (tg3_asic_rev(tp) == ASIC_REV_5703)
  14320. read_water = 4;
  14321. /* Set bit 23 to enable PCIX hw bug fix */
  14322. tp->dma_rwctrl |=
  14323. (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
  14324. (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
  14325. (1 << 23);
  14326. } else if (tg3_asic_rev(tp) == ASIC_REV_5780) {
  14327. /* 5780 always in PCIX mode */
  14328. tp->dma_rwctrl |= 0x00144000;
  14329. } else if (tg3_asic_rev(tp) == ASIC_REV_5714) {
  14330. /* 5714 always in PCIX mode */
  14331. tp->dma_rwctrl |= 0x00148000;
  14332. } else {
  14333. tp->dma_rwctrl |= 0x001b000f;
  14334. }
  14335. }
  14336. if (tg3_flag(tp, ONE_DMA_AT_ONCE))
  14337. tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
  14338. if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
  14339. tg3_asic_rev(tp) == ASIC_REV_5704)
  14340. tp->dma_rwctrl &= 0xfffffff0;
  14341. if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
  14342. tg3_asic_rev(tp) == ASIC_REV_5701) {
  14343. /* Remove this if it causes problems for some boards. */
  14344. tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
  14345. /* On 5700/5701 chips, we need to set this bit.
  14346. * Otherwise the chip will issue cacheline transactions
  14347. * to streamable DMA memory with not all the byte
  14348. * enables turned on. This is an error on several
  14349. * RISC PCI controllers, in particular sparc64.
  14350. *
  14351. * On 5703/5704 chips, this bit has been reassigned
  14352. * a different meaning. In particular, it is used
  14353. * on those chips to enable a PCI-X workaround.
  14354. */
  14355. tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
  14356. }
  14357. tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
  14358. if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
  14359. tg3_asic_rev(tp) != ASIC_REV_5701)
  14360. goto out;
  14361. /* It is best to perform DMA test with maximum write burst size
  14362. * to expose the 5700/5701 write DMA bug.
  14363. */
  14364. saved_dma_rwctrl = tp->dma_rwctrl;
  14365. tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
  14366. tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
  14367. while (1) {
  14368. u32 *p = buf, i;
  14369. for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
  14370. p[i] = i;
  14371. /* Send the buffer to the chip. */
  14372. ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, true);
  14373. if (ret) {
  14374. dev_err(&tp->pdev->dev,
  14375. "%s: Buffer write failed. err = %d\n",
  14376. __func__, ret);
  14377. break;
  14378. }
  14379. /* Now read it back. */
  14380. ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, false);
  14381. if (ret) {
  14382. dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
  14383. "err = %d\n", __func__, ret);
  14384. break;
  14385. }
  14386. /* Verify it. */
  14387. for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
  14388. if (p[i] == i)
  14389. continue;
  14390. if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
  14391. DMA_RWCTRL_WRITE_BNDRY_16) {
  14392. tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
  14393. tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
  14394. tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
  14395. break;
  14396. } else {
  14397. dev_err(&tp->pdev->dev,
  14398. "%s: Buffer corrupted on read back! "
  14399. "(%d != %d)\n", __func__, p[i], i);
  14400. ret = -ENODEV;
  14401. goto out;
  14402. }
  14403. }
  14404. if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
  14405. /* Success. */
  14406. ret = 0;
  14407. break;
  14408. }
  14409. }
  14410. if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
  14411. DMA_RWCTRL_WRITE_BNDRY_16) {
  14412. /* DMA test passed without adjusting DMA boundary,
  14413. * now look for chipsets that are known to expose the
  14414. * DMA bug without failing the test.
  14415. */
  14416. if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
  14417. tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
  14418. tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
  14419. } else {
  14420. /* Safe to use the calculated DMA boundary. */
  14421. tp->dma_rwctrl = saved_dma_rwctrl;
  14422. }
  14423. tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
  14424. }
  14425. out:
  14426. dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
  14427. out_nofree:
  14428. return ret;
  14429. }
  14430. static void tg3_init_bufmgr_config(struct tg3 *tp)
  14431. {
  14432. if (tg3_flag(tp, 57765_PLUS)) {
  14433. tp->bufmgr_config.mbuf_read_dma_low_water =
  14434. DEFAULT_MB_RDMA_LOW_WATER_5705;
  14435. tp->bufmgr_config.mbuf_mac_rx_low_water =
  14436. DEFAULT_MB_MACRX_LOW_WATER_57765;
  14437. tp->bufmgr_config.mbuf_high_water =
  14438. DEFAULT_MB_HIGH_WATER_57765;
  14439. tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
  14440. DEFAULT_MB_RDMA_LOW_WATER_5705;
  14441. tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
  14442. DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
  14443. tp->bufmgr_config.mbuf_high_water_jumbo =
  14444. DEFAULT_MB_HIGH_WATER_JUMBO_57765;
  14445. } else if (tg3_flag(tp, 5705_PLUS)) {
  14446. tp->bufmgr_config.mbuf_read_dma_low_water =
  14447. DEFAULT_MB_RDMA_LOW_WATER_5705;
  14448. tp->bufmgr_config.mbuf_mac_rx_low_water =
  14449. DEFAULT_MB_MACRX_LOW_WATER_5705;
  14450. tp->bufmgr_config.mbuf_high_water =
  14451. DEFAULT_MB_HIGH_WATER_5705;
  14452. if (tg3_asic_rev(tp) == ASIC_REV_5906) {
  14453. tp->bufmgr_config.mbuf_mac_rx_low_water =
  14454. DEFAULT_MB_MACRX_LOW_WATER_5906;
  14455. tp->bufmgr_config.mbuf_high_water =
  14456. DEFAULT_MB_HIGH_WATER_5906;
  14457. }
  14458. tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
  14459. DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
  14460. tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
  14461. DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
  14462. tp->bufmgr_config.mbuf_high_water_jumbo =
  14463. DEFAULT_MB_HIGH_WATER_JUMBO_5780;
  14464. } else {
  14465. tp->bufmgr_config.mbuf_read_dma_low_water =
  14466. DEFAULT_MB_RDMA_LOW_WATER;
  14467. tp->bufmgr_config.mbuf_mac_rx_low_water =
  14468. DEFAULT_MB_MACRX_LOW_WATER;
  14469. tp->bufmgr_config.mbuf_high_water =
  14470. DEFAULT_MB_HIGH_WATER;
  14471. tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
  14472. DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
  14473. tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
  14474. DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
  14475. tp->bufmgr_config.mbuf_high_water_jumbo =
  14476. DEFAULT_MB_HIGH_WATER_JUMBO;
  14477. }
  14478. tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
  14479. tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
  14480. }
  14481. static char *tg3_phy_string(struct tg3 *tp)
  14482. {
  14483. switch (tp->phy_id & TG3_PHY_ID_MASK) {
  14484. case TG3_PHY_ID_BCM5400: return "5400";
  14485. case TG3_PHY_ID_BCM5401: return "5401";
  14486. case TG3_PHY_ID_BCM5411: return "5411";
  14487. case TG3_PHY_ID_BCM5701: return "5701";
  14488. case TG3_PHY_ID_BCM5703: return "5703";
  14489. case TG3_PHY_ID_BCM5704: return "5704";
  14490. case TG3_PHY_ID_BCM5705: return "5705";
  14491. case TG3_PHY_ID_BCM5750: return "5750";
  14492. case TG3_PHY_ID_BCM5752: return "5752";
  14493. case TG3_PHY_ID_BCM5714: return "5714";
  14494. case TG3_PHY_ID_BCM5780: return "5780";
  14495. case TG3_PHY_ID_BCM5755: return "5755";
  14496. case TG3_PHY_ID_BCM5787: return "5787";
  14497. case TG3_PHY_ID_BCM5784: return "5784";
  14498. case TG3_PHY_ID_BCM5756: return "5722/5756";
  14499. case TG3_PHY_ID_BCM5906: return "5906";
  14500. case TG3_PHY_ID_BCM5761: return "5761";
  14501. case TG3_PHY_ID_BCM5718C: return "5718C";
  14502. case TG3_PHY_ID_BCM5718S: return "5718S";
  14503. case TG3_PHY_ID_BCM57765: return "57765";
  14504. case TG3_PHY_ID_BCM5719C: return "5719C";
  14505. case TG3_PHY_ID_BCM5720C: return "5720C";
  14506. case TG3_PHY_ID_BCM5762: return "5762C";
  14507. case TG3_PHY_ID_BCM8002: return "8002/serdes";
  14508. case 0: return "serdes";
  14509. default: return "unknown";
  14510. }
  14511. }
  14512. static char *tg3_bus_string(struct tg3 *tp, char *str)
  14513. {
  14514. if (tg3_flag(tp, PCI_EXPRESS)) {
  14515. strcpy(str, "PCI Express");
  14516. return str;
  14517. } else if (tg3_flag(tp, PCIX_MODE)) {
  14518. u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
  14519. strcpy(str, "PCIX:");
  14520. if ((clock_ctrl == 7) ||
  14521. ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
  14522. GRC_MISC_CFG_BOARD_ID_5704CIOBE))
  14523. strcat(str, "133MHz");
  14524. else if (clock_ctrl == 0)
  14525. strcat(str, "33MHz");
  14526. else if (clock_ctrl == 2)
  14527. strcat(str, "50MHz");
  14528. else if (clock_ctrl == 4)
  14529. strcat(str, "66MHz");
  14530. else if (clock_ctrl == 6)
  14531. strcat(str, "100MHz");
  14532. } else {
  14533. strcpy(str, "PCI:");
  14534. if (tg3_flag(tp, PCI_HIGH_SPEED))
  14535. strcat(str, "66MHz");
  14536. else
  14537. strcat(str, "33MHz");
  14538. }
  14539. if (tg3_flag(tp, PCI_32BIT))
  14540. strcat(str, ":32-bit");
  14541. else
  14542. strcat(str, ":64-bit");
  14543. return str;
  14544. }
  14545. static void tg3_init_coal(struct tg3 *tp)
  14546. {
  14547. struct ethtool_coalesce *ec = &tp->coal;
  14548. memset(ec, 0, sizeof(*ec));
  14549. ec->cmd = ETHTOOL_GCOALESCE;
  14550. ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
  14551. ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
  14552. ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
  14553. ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
  14554. ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
  14555. ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
  14556. ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
  14557. ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
  14558. ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
  14559. if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
  14560. HOSTCC_MODE_CLRTICK_TXBD)) {
  14561. ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
  14562. ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
  14563. ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
  14564. ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
  14565. }
  14566. if (tg3_flag(tp, 5705_PLUS)) {
  14567. ec->rx_coalesce_usecs_irq = 0;
  14568. ec->tx_coalesce_usecs_irq = 0;
  14569. ec->stats_block_coalesce_usecs = 0;
  14570. }
  14571. }
  14572. static int tg3_init_one(struct pci_dev *pdev,
  14573. const struct pci_device_id *ent)
  14574. {
  14575. struct net_device *dev;
  14576. struct tg3 *tp;
  14577. int i, err;
  14578. u32 sndmbx, rcvmbx, intmbx;
  14579. char str[40];
  14580. u64 dma_mask, persist_dma_mask;
  14581. netdev_features_t features = 0;
  14582. printk_once(KERN_INFO "%s\n", version);
  14583. err = pci_enable_device(pdev);
  14584. if (err) {
  14585. dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
  14586. return err;
  14587. }
  14588. err = pci_request_regions(pdev, DRV_MODULE_NAME);
  14589. if (err) {
  14590. dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
  14591. goto err_out_disable_pdev;
  14592. }
  14593. pci_set_master(pdev);
  14594. dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
  14595. if (!dev) {
  14596. err = -ENOMEM;
  14597. goto err_out_free_res;
  14598. }
  14599. SET_NETDEV_DEV(dev, &pdev->dev);
  14600. tp = netdev_priv(dev);
  14601. tp->pdev = pdev;
  14602. tp->dev = dev;
  14603. tp->rx_mode = TG3_DEF_RX_MODE;
  14604. tp->tx_mode = TG3_DEF_TX_MODE;
  14605. tp->irq_sync = 1;
  14606. tp->pcierr_recovery = false;
  14607. if (tg3_debug > 0)
  14608. tp->msg_enable = tg3_debug;
  14609. else
  14610. tp->msg_enable = TG3_DEF_MSG_ENABLE;
  14611. if (pdev_is_ssb_gige_core(pdev)) {
  14612. tg3_flag_set(tp, IS_SSB_CORE);
  14613. if (ssb_gige_must_flush_posted_writes(pdev))
  14614. tg3_flag_set(tp, FLUSH_POSTED_WRITES);
  14615. if (ssb_gige_one_dma_at_once(pdev))
  14616. tg3_flag_set(tp, ONE_DMA_AT_ONCE);
  14617. if (ssb_gige_have_roboswitch(pdev)) {
  14618. tg3_flag_set(tp, USE_PHYLIB);
  14619. tg3_flag_set(tp, ROBOSWITCH);
  14620. }
  14621. if (ssb_gige_is_rgmii(pdev))
  14622. tg3_flag_set(tp, RGMII_MODE);
  14623. }
  14624. /* The word/byte swap controls here control register access byte
  14625. * swapping. DMA data byte swapping is controlled in the GRC_MODE
  14626. * setting below.
  14627. */
  14628. tp->misc_host_ctrl =
  14629. MISC_HOST_CTRL_MASK_PCI_INT |
  14630. MISC_HOST_CTRL_WORD_SWAP |
  14631. MISC_HOST_CTRL_INDIR_ACCESS |
  14632. MISC_HOST_CTRL_PCISTATE_RW;
  14633. /* The NONFRM (non-frame) byte/word swap controls take effect
  14634. * on descriptor entries, anything which isn't packet data.
  14635. *
  14636. * The StrongARM chips on the board (one for tx, one for rx)
  14637. * are running in big-endian mode.
  14638. */
  14639. tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
  14640. GRC_MODE_WSWAP_NONFRM_DATA);
  14641. #ifdef __BIG_ENDIAN
  14642. tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
  14643. #endif
  14644. spin_lock_init(&tp->lock);
  14645. spin_lock_init(&tp->indirect_lock);
  14646. INIT_WORK(&tp->reset_task, tg3_reset_task);
  14647. tp->regs = pci_ioremap_bar(pdev, BAR_0);
  14648. if (!tp->regs) {
  14649. dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
  14650. err = -ENOMEM;
  14651. goto err_out_free_dev;
  14652. }
  14653. if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
  14654. tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761E ||
  14655. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S ||
  14656. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761SE ||
  14657. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
  14658. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C ||
  14659. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
  14660. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
  14661. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720 ||
  14662. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57767 ||
  14663. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57764 ||
  14664. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5762 ||
  14665. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725 ||
  14666. tp->pdev->device == TG3PCI_DEVICE_TIGON3_5727 ||
  14667. tp->pdev->device == TG3PCI_DEVICE_TIGON3_57787) {
  14668. tg3_flag_set(tp, ENABLE_APE);
  14669. tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
  14670. if (!tp->aperegs) {
  14671. dev_err(&pdev->dev,
  14672. "Cannot map APE registers, aborting\n");
  14673. err = -ENOMEM;
  14674. goto err_out_iounmap;
  14675. }
  14676. }
  14677. tp->rx_pending = TG3_DEF_RX_RING_PENDING;
  14678. tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
  14679. dev->ethtool_ops = &tg3_ethtool_ops;
  14680. dev->watchdog_timeo = TG3_TX_TIMEOUT;
  14681. dev->netdev_ops = &tg3_netdev_ops;
  14682. dev->irq = pdev->irq;
  14683. err = tg3_get_invariants(tp, ent);
  14684. if (err) {
  14685. dev_err(&pdev->dev,
  14686. "Problem fetching invariants of chip, aborting\n");
  14687. goto err_out_apeunmap;
  14688. }
  14689. /* The EPB bridge inside 5714, 5715, and 5780 and any
  14690. * device behind the EPB cannot support DMA addresses > 40-bit.
  14691. * On 64-bit systems with IOMMU, use 40-bit dma_mask.
  14692. * On 64-bit systems without IOMMU, use 64-bit dma_mask and
  14693. * do DMA address check in tg3_start_xmit().
  14694. */
  14695. if (tg3_flag(tp, IS_5788))
  14696. persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
  14697. else if (tg3_flag(tp, 40BIT_DMA_BUG)) {
  14698. persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
  14699. #ifdef CONFIG_HIGHMEM
  14700. dma_mask = DMA_BIT_MASK(64);
  14701. #endif
  14702. } else
  14703. persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
  14704. /* Configure DMA attributes. */
  14705. if (dma_mask > DMA_BIT_MASK(32)) {
  14706. err = pci_set_dma_mask(pdev, dma_mask);
  14707. if (!err) {
  14708. features |= NETIF_F_HIGHDMA;
  14709. err = pci_set_consistent_dma_mask(pdev,
  14710. persist_dma_mask);
  14711. if (err < 0) {
  14712. dev_err(&pdev->dev, "Unable to obtain 64 bit "
  14713. "DMA for consistent allocations\n");
  14714. goto err_out_apeunmap;
  14715. }
  14716. }
  14717. }
  14718. if (err || dma_mask == DMA_BIT_MASK(32)) {
  14719. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  14720. if (err) {
  14721. dev_err(&pdev->dev,
  14722. "No usable DMA configuration, aborting\n");
  14723. goto err_out_apeunmap;
  14724. }
  14725. }
  14726. tg3_init_bufmgr_config(tp);
  14727. /* 5700 B0 chips do not support checksumming correctly due
  14728. * to hardware bugs.
  14729. */
  14730. if (tg3_chip_rev_id(tp) != CHIPREV_ID_5700_B0) {
  14731. features |= NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
  14732. if (tg3_flag(tp, 5755_PLUS))
  14733. features |= NETIF_F_IPV6_CSUM;
  14734. }
  14735. /* TSO is on by default on chips that support hardware TSO.
  14736. * Firmware TSO on older chips gives lower performance, so it
  14737. * is off by default, but can be enabled using ethtool.
  14738. */
  14739. if ((tg3_flag(tp, HW_TSO_1) ||
  14740. tg3_flag(tp, HW_TSO_2) ||
  14741. tg3_flag(tp, HW_TSO_3)) &&
  14742. (features & NETIF_F_IP_CSUM))
  14743. features |= NETIF_F_TSO;
  14744. if (tg3_flag(tp, HW_TSO_2) || tg3_flag(tp, HW_TSO_3)) {
  14745. if (features & NETIF_F_IPV6_CSUM)
  14746. features |= NETIF_F_TSO6;
  14747. if (tg3_flag(tp, HW_TSO_3) ||
  14748. tg3_asic_rev(tp) == ASIC_REV_5761 ||
  14749. (tg3_asic_rev(tp) == ASIC_REV_5784 &&
  14750. tg3_chip_rev(tp) != CHIPREV_5784_AX) ||
  14751. tg3_asic_rev(tp) == ASIC_REV_5785 ||
  14752. tg3_asic_rev(tp) == ASIC_REV_57780)
  14753. features |= NETIF_F_TSO_ECN;
  14754. }
  14755. dev->features |= features | NETIF_F_HW_VLAN_CTAG_TX |
  14756. NETIF_F_HW_VLAN_CTAG_RX;
  14757. dev->vlan_features |= features;
  14758. /*
  14759. * Add loopback capability only for a subset of devices that support
  14760. * MAC-LOOPBACK. Eventually this need to be enhanced to allow INT-PHY
  14761. * loopback for the remaining devices.
  14762. */
  14763. if (tg3_asic_rev(tp) != ASIC_REV_5780 &&
  14764. !tg3_flag(tp, CPMU_PRESENT))
  14765. /* Add the loopback capability */
  14766. features |= NETIF_F_LOOPBACK;
  14767. dev->hw_features |= features;
  14768. dev->priv_flags |= IFF_UNICAST_FLT;
  14769. if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A1 &&
  14770. !tg3_flag(tp, TSO_CAPABLE) &&
  14771. !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
  14772. tg3_flag_set(tp, MAX_RXPEND_64);
  14773. tp->rx_pending = 63;
  14774. }
  14775. err = tg3_get_device_address(tp);
  14776. if (err) {
  14777. dev_err(&pdev->dev,
  14778. "Could not obtain valid ethernet address, aborting\n");
  14779. goto err_out_apeunmap;
  14780. }
  14781. intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
  14782. rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
  14783. sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
  14784. for (i = 0; i < tp->irq_max; i++) {
  14785. struct tg3_napi *tnapi = &tp->napi[i];
  14786. tnapi->tp = tp;
  14787. tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
  14788. tnapi->int_mbox = intmbx;
  14789. if (i <= 4)
  14790. intmbx += 0x8;
  14791. else
  14792. intmbx += 0x4;
  14793. tnapi->consmbox = rcvmbx;
  14794. tnapi->prodmbox = sndmbx;
  14795. if (i)
  14796. tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
  14797. else
  14798. tnapi->coal_now = HOSTCC_MODE_NOW;
  14799. if (!tg3_flag(tp, SUPPORT_MSIX))
  14800. break;
  14801. /*
  14802. * If we support MSIX, we'll be using RSS. If we're using
  14803. * RSS, the first vector only handles link interrupts and the
  14804. * remaining vectors handle rx and tx interrupts. Reuse the
  14805. * mailbox values for the next iteration. The values we setup
  14806. * above are still useful for the single vectored mode.
  14807. */
  14808. if (!i)
  14809. continue;
  14810. rcvmbx += 0x8;
  14811. if (sndmbx & 0x4)
  14812. sndmbx -= 0x4;
  14813. else
  14814. sndmbx += 0xc;
  14815. }
  14816. /*
  14817. * Reset chip in case UNDI or EFI driver did not shutdown
  14818. * DMA self test will enable WDMAC and we'll see (spurious)
  14819. * pending DMA on the PCI bus at that point.
  14820. */
  14821. if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
  14822. (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
  14823. tg3_full_lock(tp, 0);
  14824. tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
  14825. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  14826. tg3_full_unlock(tp);
  14827. }
  14828. err = tg3_test_dma(tp);
  14829. if (err) {
  14830. dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
  14831. goto err_out_apeunmap;
  14832. }
  14833. tg3_init_coal(tp);
  14834. pci_set_drvdata(pdev, dev);
  14835. if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
  14836. tg3_asic_rev(tp) == ASIC_REV_5720 ||
  14837. tg3_asic_rev(tp) == ASIC_REV_5762)
  14838. tg3_flag_set(tp, PTP_CAPABLE);
  14839. tg3_timer_init(tp);
  14840. tg3_carrier_off(tp);
  14841. err = register_netdev(dev);
  14842. if (err) {
  14843. dev_err(&pdev->dev, "Cannot register net device, aborting\n");
  14844. goto err_out_apeunmap;
  14845. }
  14846. if (tg3_flag(tp, PTP_CAPABLE)) {
  14847. tg3_ptp_init(tp);
  14848. tp->ptp_clock = ptp_clock_register(&tp->ptp_info,
  14849. &tp->pdev->dev);
  14850. if (IS_ERR(tp->ptp_clock))
  14851. tp->ptp_clock = NULL;
  14852. }
  14853. netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
  14854. tp->board_part_number,
  14855. tg3_chip_rev_id(tp),
  14856. tg3_bus_string(tp, str),
  14857. dev->dev_addr);
  14858. if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
  14859. struct phy_device *phydev;
  14860. phydev = tp->mdio_bus->phy_map[tp->phy_addr];
  14861. netdev_info(dev,
  14862. "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
  14863. phydev->drv->name, dev_name(&phydev->dev));
  14864. } else {
  14865. char *ethtype;
  14866. if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
  14867. ethtype = "10/100Base-TX";
  14868. else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
  14869. ethtype = "1000Base-SX";
  14870. else
  14871. ethtype = "10/100/1000Base-T";
  14872. netdev_info(dev, "attached PHY is %s (%s Ethernet) "
  14873. "(WireSpeed[%d], EEE[%d])\n",
  14874. tg3_phy_string(tp), ethtype,
  14875. (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0,
  14876. (tp->phy_flags & TG3_PHYFLG_EEE_CAP) != 0);
  14877. }
  14878. netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
  14879. (dev->features & NETIF_F_RXCSUM) != 0,
  14880. tg3_flag(tp, USE_LINKCHG_REG) != 0,
  14881. (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
  14882. tg3_flag(tp, ENABLE_ASF) != 0,
  14883. tg3_flag(tp, TSO_CAPABLE) != 0);
  14884. netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
  14885. tp->dma_rwctrl,
  14886. pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
  14887. ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
  14888. pci_save_state(pdev);
  14889. return 0;
  14890. err_out_apeunmap:
  14891. if (tp->aperegs) {
  14892. iounmap(tp->aperegs);
  14893. tp->aperegs = NULL;
  14894. }
  14895. err_out_iounmap:
  14896. if (tp->regs) {
  14897. iounmap(tp->regs);
  14898. tp->regs = NULL;
  14899. }
  14900. err_out_free_dev:
  14901. free_netdev(dev);
  14902. err_out_free_res:
  14903. pci_release_regions(pdev);
  14904. err_out_disable_pdev:
  14905. if (pci_is_enabled(pdev))
  14906. pci_disable_device(pdev);
  14907. return err;
  14908. }
  14909. static void tg3_remove_one(struct pci_dev *pdev)
  14910. {
  14911. struct net_device *dev = pci_get_drvdata(pdev);
  14912. if (dev) {
  14913. struct tg3 *tp = netdev_priv(dev);
  14914. tg3_ptp_fini(tp);
  14915. release_firmware(tp->fw);
  14916. tg3_reset_task_cancel(tp);
  14917. if (tg3_flag(tp, USE_PHYLIB)) {
  14918. tg3_phy_fini(tp);
  14919. tg3_mdio_fini(tp);
  14920. }
  14921. unregister_netdev(dev);
  14922. if (tp->aperegs) {
  14923. iounmap(tp->aperegs);
  14924. tp->aperegs = NULL;
  14925. }
  14926. if (tp->regs) {
  14927. iounmap(tp->regs);
  14928. tp->regs = NULL;
  14929. }
  14930. free_netdev(dev);
  14931. pci_release_regions(pdev);
  14932. pci_disable_device(pdev);
  14933. }
  14934. }
  14935. #ifdef CONFIG_PM_SLEEP
  14936. static int tg3_suspend(struct device *device)
  14937. {
  14938. struct pci_dev *pdev = to_pci_dev(device);
  14939. struct net_device *dev = pci_get_drvdata(pdev);
  14940. struct tg3 *tp = netdev_priv(dev);
  14941. int err = 0;
  14942. rtnl_lock();
  14943. if (!netif_running(dev))
  14944. goto unlock;
  14945. tg3_reset_task_cancel(tp);
  14946. tg3_phy_stop(tp);
  14947. tg3_netif_stop(tp);
  14948. tg3_timer_stop(tp);
  14949. tg3_full_lock(tp, 1);
  14950. tg3_disable_ints(tp);
  14951. tg3_full_unlock(tp);
  14952. netif_device_detach(dev);
  14953. tg3_full_lock(tp, 0);
  14954. tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
  14955. tg3_flag_clear(tp, INIT_COMPLETE);
  14956. tg3_full_unlock(tp);
  14957. err = tg3_power_down_prepare(tp);
  14958. if (err) {
  14959. int err2;
  14960. tg3_full_lock(tp, 0);
  14961. tg3_flag_set(tp, INIT_COMPLETE);
  14962. err2 = tg3_restart_hw(tp, true);
  14963. if (err2)
  14964. goto out;
  14965. tg3_timer_start(tp);
  14966. netif_device_attach(dev);
  14967. tg3_netif_start(tp);
  14968. out:
  14969. tg3_full_unlock(tp);
  14970. if (!err2)
  14971. tg3_phy_start(tp);
  14972. }
  14973. unlock:
  14974. rtnl_unlock();
  14975. return err;
  14976. }
  14977. static int tg3_resume(struct device *device)
  14978. {
  14979. struct pci_dev *pdev = to_pci_dev(device);
  14980. struct net_device *dev = pci_get_drvdata(pdev);
  14981. struct tg3 *tp = netdev_priv(dev);
  14982. int err = 0;
  14983. rtnl_lock();
  14984. if (!netif_running(dev))
  14985. goto unlock;
  14986. netif_device_attach(dev);
  14987. tg3_full_lock(tp, 0);
  14988. tg3_ape_driver_state_change(tp, RESET_KIND_INIT);
  14989. tg3_flag_set(tp, INIT_COMPLETE);
  14990. err = tg3_restart_hw(tp,
  14991. !(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN));
  14992. if (err)
  14993. goto out;
  14994. tg3_timer_start(tp);
  14995. tg3_netif_start(tp);
  14996. out:
  14997. tg3_full_unlock(tp);
  14998. if (!err)
  14999. tg3_phy_start(tp);
  15000. unlock:
  15001. rtnl_unlock();
  15002. return err;
  15003. }
  15004. #endif /* CONFIG_PM_SLEEP */
  15005. static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
  15006. static void tg3_shutdown(struct pci_dev *pdev)
  15007. {
  15008. struct net_device *dev = pci_get_drvdata(pdev);
  15009. struct tg3 *tp = netdev_priv(dev);
  15010. rtnl_lock();
  15011. netif_device_detach(dev);
  15012. if (netif_running(dev))
  15013. dev_close(dev);
  15014. if (system_state == SYSTEM_POWER_OFF)
  15015. tg3_power_down(tp);
  15016. rtnl_unlock();
  15017. }
  15018. /**
  15019. * tg3_io_error_detected - called when PCI error is detected
  15020. * @pdev: Pointer to PCI device
  15021. * @state: The current pci connection state
  15022. *
  15023. * This function is called after a PCI bus error affecting
  15024. * this device has been detected.
  15025. */
  15026. static pci_ers_result_t tg3_io_error_detected(struct pci_dev *pdev,
  15027. pci_channel_state_t state)
  15028. {
  15029. struct net_device *netdev = pci_get_drvdata(pdev);
  15030. struct tg3 *tp = netdev_priv(netdev);
  15031. pci_ers_result_t err = PCI_ERS_RESULT_NEED_RESET;
  15032. netdev_info(netdev, "PCI I/O error detected\n");
  15033. rtnl_lock();
  15034. /* We needn't recover from permanent error */
  15035. if (state == pci_channel_io_frozen)
  15036. tp->pcierr_recovery = true;
  15037. /* We probably don't have netdev yet */
  15038. if (!netdev || !netif_running(netdev))
  15039. goto done;
  15040. tg3_phy_stop(tp);
  15041. tg3_netif_stop(tp);
  15042. tg3_timer_stop(tp);
  15043. /* Want to make sure that the reset task doesn't run */
  15044. tg3_reset_task_cancel(tp);
  15045. netif_device_detach(netdev);
  15046. /* Clean up software state, even if MMIO is blocked */
  15047. tg3_full_lock(tp, 0);
  15048. tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
  15049. tg3_full_unlock(tp);
  15050. done:
  15051. if (state == pci_channel_io_perm_failure) {
  15052. if (netdev) {
  15053. tg3_napi_enable(tp);
  15054. dev_close(netdev);
  15055. }
  15056. err = PCI_ERS_RESULT_DISCONNECT;
  15057. } else {
  15058. pci_disable_device(pdev);
  15059. }
  15060. rtnl_unlock();
  15061. return err;
  15062. }
  15063. /**
  15064. * tg3_io_slot_reset - called after the pci bus has been reset.
  15065. * @pdev: Pointer to PCI device
  15066. *
  15067. * Restart the card from scratch, as if from a cold-boot.
  15068. * At this point, the card has exprienced a hard reset,
  15069. * followed by fixups by BIOS, and has its config space
  15070. * set up identically to what it was at cold boot.
  15071. */
  15072. static pci_ers_result_t tg3_io_slot_reset(struct pci_dev *pdev)
  15073. {
  15074. struct net_device *netdev = pci_get_drvdata(pdev);
  15075. struct tg3 *tp = netdev_priv(netdev);
  15076. pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
  15077. int err;
  15078. rtnl_lock();
  15079. if (pci_enable_device(pdev)) {
  15080. dev_err(&pdev->dev,
  15081. "Cannot re-enable PCI device after reset.\n");
  15082. goto done;
  15083. }
  15084. pci_set_master(pdev);
  15085. pci_restore_state(pdev);
  15086. pci_save_state(pdev);
  15087. if (!netdev || !netif_running(netdev)) {
  15088. rc = PCI_ERS_RESULT_RECOVERED;
  15089. goto done;
  15090. }
  15091. err = tg3_power_up(tp);
  15092. if (err)
  15093. goto done;
  15094. rc = PCI_ERS_RESULT_RECOVERED;
  15095. done:
  15096. if (rc != PCI_ERS_RESULT_RECOVERED && netdev && netif_running(netdev)) {
  15097. tg3_napi_enable(tp);
  15098. dev_close(netdev);
  15099. }
  15100. rtnl_unlock();
  15101. return rc;
  15102. }
  15103. /**
  15104. * tg3_io_resume - called when traffic can start flowing again.
  15105. * @pdev: Pointer to PCI device
  15106. *
  15107. * This callback is called when the error recovery driver tells
  15108. * us that its OK to resume normal operation.
  15109. */
  15110. static void tg3_io_resume(struct pci_dev *pdev)
  15111. {
  15112. struct net_device *netdev = pci_get_drvdata(pdev);
  15113. struct tg3 *tp = netdev_priv(netdev);
  15114. int err;
  15115. rtnl_lock();
  15116. if (!netif_running(netdev))
  15117. goto done;
  15118. tg3_full_lock(tp, 0);
  15119. tg3_ape_driver_state_change(tp, RESET_KIND_INIT);
  15120. tg3_flag_set(tp, INIT_COMPLETE);
  15121. err = tg3_restart_hw(tp, true);
  15122. if (err) {
  15123. tg3_full_unlock(tp);
  15124. netdev_err(netdev, "Cannot restart hardware after reset.\n");
  15125. goto done;
  15126. }
  15127. netif_device_attach(netdev);
  15128. tg3_timer_start(tp);
  15129. tg3_netif_start(tp);
  15130. tg3_full_unlock(tp);
  15131. tg3_phy_start(tp);
  15132. done:
  15133. tp->pcierr_recovery = false;
  15134. rtnl_unlock();
  15135. }
  15136. static const struct pci_error_handlers tg3_err_handler = {
  15137. .error_detected = tg3_io_error_detected,
  15138. .slot_reset = tg3_io_slot_reset,
  15139. .resume = tg3_io_resume
  15140. };
  15141. static struct pci_driver tg3_driver = {
  15142. .name = DRV_MODULE_NAME,
  15143. .id_table = tg3_pci_tbl,
  15144. .probe = tg3_init_one,
  15145. .remove = tg3_remove_one,
  15146. .err_handler = &tg3_err_handler,
  15147. .driver.pm = &tg3_pm_ops,
  15148. .shutdown = tg3_shutdown,
  15149. };
  15150. module_pci_driver(tg3_driver);