bnx2.c 176 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305
  1. /* bnx2.c: Broadcom NX2 network driver.
  2. *
  3. * Copyright (c) 2004-2007 Broadcom Corporation
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation.
  8. *
  9. * Written by: Michael Chan (mchan@broadcom.com)
  10. */
  11. #include <linux/module.h>
  12. #include <linux/moduleparam.h>
  13. #include <linux/kernel.h>
  14. #include <linux/timer.h>
  15. #include <linux/errno.h>
  16. #include <linux/ioport.h>
  17. #include <linux/slab.h>
  18. #include <linux/vmalloc.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/pci.h>
  21. #include <linux/init.h>
  22. #include <linux/netdevice.h>
  23. #include <linux/etherdevice.h>
  24. #include <linux/skbuff.h>
  25. #include <linux/dma-mapping.h>
  26. #include <linux/bitops.h>
  27. #include <asm/io.h>
  28. #include <asm/irq.h>
  29. #include <linux/delay.h>
  30. #include <asm/byteorder.h>
  31. #include <asm/page.h>
  32. #include <linux/time.h>
  33. #include <linux/ethtool.h>
  34. #include <linux/mii.h>
  35. #ifdef NETIF_F_HW_VLAN_TX
  36. #include <linux/if_vlan.h>
  37. #define BCM_VLAN 1
  38. #endif
  39. #include <net/ip.h>
  40. #include <net/tcp.h>
  41. #include <net/checksum.h>
  42. #include <linux/workqueue.h>
  43. #include <linux/crc32.h>
  44. #include <linux/prefetch.h>
  45. #include <linux/cache.h>
  46. #include <linux/zlib.h>
  47. #include "bnx2.h"
  48. #include "bnx2_fw.h"
  49. #include "bnx2_fw2.h"
  50. #define FW_BUF_SIZE 0x10000
  51. #define DRV_MODULE_NAME "bnx2"
  52. #define PFX DRV_MODULE_NAME ": "
  53. #define DRV_MODULE_VERSION "1.7.0"
  54. #define DRV_MODULE_RELDATE "December 11, 2007"
  55. #define RUN_AT(x) (jiffies + (x))
  56. /* Time in jiffies before concluding the transmitter is hung. */
  57. #define TX_TIMEOUT (5*HZ)
  58. static const char version[] __devinitdata =
  59. "Broadcom NetXtreme II Gigabit Ethernet Driver " DRV_MODULE_NAME " v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
  60. MODULE_AUTHOR("Michael Chan <mchan@broadcom.com>");
  61. MODULE_DESCRIPTION("Broadcom NetXtreme II BCM5706/5708 Driver");
  62. MODULE_LICENSE("GPL");
  63. MODULE_VERSION(DRV_MODULE_VERSION);
  64. static int disable_msi = 0;
  65. module_param(disable_msi, int, 0);
  66. MODULE_PARM_DESC(disable_msi, "Disable Message Signaled Interrupt (MSI)");
  67. typedef enum {
  68. BCM5706 = 0,
  69. NC370T,
  70. NC370I,
  71. BCM5706S,
  72. NC370F,
  73. BCM5708,
  74. BCM5708S,
  75. BCM5709,
  76. BCM5709S,
  77. } board_t;
  78. /* indexed by board_t, above */
  79. static const struct {
  80. char *name;
  81. } board_info[] __devinitdata = {
  82. { "Broadcom NetXtreme II BCM5706 1000Base-T" },
  83. { "HP NC370T Multifunction Gigabit Server Adapter" },
  84. { "HP NC370i Multifunction Gigabit Server Adapter" },
  85. { "Broadcom NetXtreme II BCM5706 1000Base-SX" },
  86. { "HP NC370F Multifunction Gigabit Server Adapter" },
  87. { "Broadcom NetXtreme II BCM5708 1000Base-T" },
  88. { "Broadcom NetXtreme II BCM5708 1000Base-SX" },
  89. { "Broadcom NetXtreme II BCM5709 1000Base-T" },
  90. { "Broadcom NetXtreme II BCM5709 1000Base-SX" },
  91. };
  92. static struct pci_device_id bnx2_pci_tbl[] = {
  93. { PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_NX2_5706,
  94. PCI_VENDOR_ID_HP, 0x3101, 0, 0, NC370T },
  95. { PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_NX2_5706,
  96. PCI_VENDOR_ID_HP, 0x3106, 0, 0, NC370I },
  97. { PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_NX2_5706,
  98. PCI_ANY_ID, PCI_ANY_ID, 0, 0, BCM5706 },
  99. { PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_NX2_5708,
  100. PCI_ANY_ID, PCI_ANY_ID, 0, 0, BCM5708 },
  101. { PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_NX2_5706S,
  102. PCI_VENDOR_ID_HP, 0x3102, 0, 0, NC370F },
  103. { PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_NX2_5706S,
  104. PCI_ANY_ID, PCI_ANY_ID, 0, 0, BCM5706S },
  105. { PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_NX2_5708S,
  106. PCI_ANY_ID, PCI_ANY_ID, 0, 0, BCM5708S },
  107. { PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_NX2_5709,
  108. PCI_ANY_ID, PCI_ANY_ID, 0, 0, BCM5709 },
  109. { PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_NX2_5709S,
  110. PCI_ANY_ID, PCI_ANY_ID, 0, 0, BCM5709S },
  111. { 0, }
  112. };
  113. static struct flash_spec flash_table[] =
  114. {
  115. #define BUFFERED_FLAGS (BNX2_NV_BUFFERED | BNX2_NV_TRANSLATE)
  116. #define NONBUFFERED_FLAGS (BNX2_NV_WREN)
  117. /* Slow EEPROM */
  118. {0x00000000, 0x40830380, 0x009f0081, 0xa184a053, 0xaf000400,
  119. BUFFERED_FLAGS, SEEPROM_PAGE_BITS, SEEPROM_PAGE_SIZE,
  120. SEEPROM_BYTE_ADDR_MASK, SEEPROM_TOTAL_SIZE,
  121. "EEPROM - slow"},
  122. /* Expansion entry 0001 */
  123. {0x08000002, 0x4b808201, 0x00050081, 0x03840253, 0xaf020406,
  124. NONBUFFERED_FLAGS, SAIFUN_FLASH_PAGE_BITS, SAIFUN_FLASH_PAGE_SIZE,
  125. SAIFUN_FLASH_BYTE_ADDR_MASK, 0,
  126. "Entry 0001"},
  127. /* Saifun SA25F010 (non-buffered flash) */
  128. /* strap, cfg1, & write1 need updates */
  129. {0x04000001, 0x47808201, 0x00050081, 0x03840253, 0xaf020406,
  130. NONBUFFERED_FLAGS, SAIFUN_FLASH_PAGE_BITS, SAIFUN_FLASH_PAGE_SIZE,
  131. SAIFUN_FLASH_BYTE_ADDR_MASK, SAIFUN_FLASH_BASE_TOTAL_SIZE*2,
  132. "Non-buffered flash (128kB)"},
  133. /* Saifun SA25F020 (non-buffered flash) */
  134. /* strap, cfg1, & write1 need updates */
  135. {0x0c000003, 0x4f808201, 0x00050081, 0x03840253, 0xaf020406,
  136. NONBUFFERED_FLAGS, SAIFUN_FLASH_PAGE_BITS, SAIFUN_FLASH_PAGE_SIZE,
  137. SAIFUN_FLASH_BYTE_ADDR_MASK, SAIFUN_FLASH_BASE_TOTAL_SIZE*4,
  138. "Non-buffered flash (256kB)"},
  139. /* Expansion entry 0100 */
  140. {0x11000000, 0x53808201, 0x00050081, 0x03840253, 0xaf020406,
  141. NONBUFFERED_FLAGS, SAIFUN_FLASH_PAGE_BITS, SAIFUN_FLASH_PAGE_SIZE,
  142. SAIFUN_FLASH_BYTE_ADDR_MASK, 0,
  143. "Entry 0100"},
  144. /* Entry 0101: ST M45PE10 (non-buffered flash, TetonII B0) */
  145. {0x19000002, 0x5b808201, 0x000500db, 0x03840253, 0xaf020406,
  146. NONBUFFERED_FLAGS, ST_MICRO_FLASH_PAGE_BITS, ST_MICRO_FLASH_PAGE_SIZE,
  147. ST_MICRO_FLASH_BYTE_ADDR_MASK, ST_MICRO_FLASH_BASE_TOTAL_SIZE*2,
  148. "Entry 0101: ST M45PE10 (128kB non-bufferred)"},
  149. /* Entry 0110: ST M45PE20 (non-buffered flash)*/
  150. {0x15000001, 0x57808201, 0x000500db, 0x03840253, 0xaf020406,
  151. NONBUFFERED_FLAGS, ST_MICRO_FLASH_PAGE_BITS, ST_MICRO_FLASH_PAGE_SIZE,
  152. ST_MICRO_FLASH_BYTE_ADDR_MASK, ST_MICRO_FLASH_BASE_TOTAL_SIZE*4,
  153. "Entry 0110: ST M45PE20 (256kB non-bufferred)"},
  154. /* Saifun SA25F005 (non-buffered flash) */
  155. /* strap, cfg1, & write1 need updates */
  156. {0x1d000003, 0x5f808201, 0x00050081, 0x03840253, 0xaf020406,
  157. NONBUFFERED_FLAGS, SAIFUN_FLASH_PAGE_BITS, SAIFUN_FLASH_PAGE_SIZE,
  158. SAIFUN_FLASH_BYTE_ADDR_MASK, SAIFUN_FLASH_BASE_TOTAL_SIZE,
  159. "Non-buffered flash (64kB)"},
  160. /* Fast EEPROM */
  161. {0x22000000, 0x62808380, 0x009f0081, 0xa184a053, 0xaf000400,
  162. BUFFERED_FLAGS, SEEPROM_PAGE_BITS, SEEPROM_PAGE_SIZE,
  163. SEEPROM_BYTE_ADDR_MASK, SEEPROM_TOTAL_SIZE,
  164. "EEPROM - fast"},
  165. /* Expansion entry 1001 */
  166. {0x2a000002, 0x6b808201, 0x00050081, 0x03840253, 0xaf020406,
  167. NONBUFFERED_FLAGS, SAIFUN_FLASH_PAGE_BITS, SAIFUN_FLASH_PAGE_SIZE,
  168. SAIFUN_FLASH_BYTE_ADDR_MASK, 0,
  169. "Entry 1001"},
  170. /* Expansion entry 1010 */
  171. {0x26000001, 0x67808201, 0x00050081, 0x03840253, 0xaf020406,
  172. NONBUFFERED_FLAGS, SAIFUN_FLASH_PAGE_BITS, SAIFUN_FLASH_PAGE_SIZE,
  173. SAIFUN_FLASH_BYTE_ADDR_MASK, 0,
  174. "Entry 1010"},
  175. /* ATMEL AT45DB011B (buffered flash) */
  176. {0x2e000003, 0x6e808273, 0x00570081, 0x68848353, 0xaf000400,
  177. BUFFERED_FLAGS, BUFFERED_FLASH_PAGE_BITS, BUFFERED_FLASH_PAGE_SIZE,
  178. BUFFERED_FLASH_BYTE_ADDR_MASK, BUFFERED_FLASH_TOTAL_SIZE,
  179. "Buffered flash (128kB)"},
  180. /* Expansion entry 1100 */
  181. {0x33000000, 0x73808201, 0x00050081, 0x03840253, 0xaf020406,
  182. NONBUFFERED_FLAGS, SAIFUN_FLASH_PAGE_BITS, SAIFUN_FLASH_PAGE_SIZE,
  183. SAIFUN_FLASH_BYTE_ADDR_MASK, 0,
  184. "Entry 1100"},
  185. /* Expansion entry 1101 */
  186. {0x3b000002, 0x7b808201, 0x00050081, 0x03840253, 0xaf020406,
  187. NONBUFFERED_FLAGS, SAIFUN_FLASH_PAGE_BITS, SAIFUN_FLASH_PAGE_SIZE,
  188. SAIFUN_FLASH_BYTE_ADDR_MASK, 0,
  189. "Entry 1101"},
  190. /* Ateml Expansion entry 1110 */
  191. {0x37000001, 0x76808273, 0x00570081, 0x68848353, 0xaf000400,
  192. BUFFERED_FLAGS, BUFFERED_FLASH_PAGE_BITS, BUFFERED_FLASH_PAGE_SIZE,
  193. BUFFERED_FLASH_BYTE_ADDR_MASK, 0,
  194. "Entry 1110 (Atmel)"},
  195. /* ATMEL AT45DB021B (buffered flash) */
  196. {0x3f000003, 0x7e808273, 0x00570081, 0x68848353, 0xaf000400,
  197. BUFFERED_FLAGS, BUFFERED_FLASH_PAGE_BITS, BUFFERED_FLASH_PAGE_SIZE,
  198. BUFFERED_FLASH_BYTE_ADDR_MASK, BUFFERED_FLASH_TOTAL_SIZE*2,
  199. "Buffered flash (256kB)"},
  200. };
  201. static struct flash_spec flash_5709 = {
  202. .flags = BNX2_NV_BUFFERED,
  203. .page_bits = BCM5709_FLASH_PAGE_BITS,
  204. .page_size = BCM5709_FLASH_PAGE_SIZE,
  205. .addr_mask = BCM5709_FLASH_BYTE_ADDR_MASK,
  206. .total_size = BUFFERED_FLASH_TOTAL_SIZE*2,
  207. .name = "5709 Buffered flash (256kB)",
  208. };
  209. MODULE_DEVICE_TABLE(pci, bnx2_pci_tbl);
  210. static inline u32 bnx2_tx_avail(struct bnx2 *bp, struct bnx2_napi *bnapi)
  211. {
  212. u32 diff;
  213. smp_mb();
  214. /* The ring uses 256 indices for 255 entries, one of them
  215. * needs to be skipped.
  216. */
  217. diff = bp->tx_prod - bnapi->tx_cons;
  218. if (unlikely(diff >= TX_DESC_CNT)) {
  219. diff &= 0xffff;
  220. if (diff == TX_DESC_CNT)
  221. diff = MAX_TX_DESC_CNT;
  222. }
  223. return (bp->tx_ring_size - diff);
  224. }
  225. static u32
  226. bnx2_reg_rd_ind(struct bnx2 *bp, u32 offset)
  227. {
  228. u32 val;
  229. spin_lock_bh(&bp->indirect_lock);
  230. REG_WR(bp, BNX2_PCICFG_REG_WINDOW_ADDRESS, offset);
  231. val = REG_RD(bp, BNX2_PCICFG_REG_WINDOW);
  232. spin_unlock_bh(&bp->indirect_lock);
  233. return val;
  234. }
  235. static void
  236. bnx2_reg_wr_ind(struct bnx2 *bp, u32 offset, u32 val)
  237. {
  238. spin_lock_bh(&bp->indirect_lock);
  239. REG_WR(bp, BNX2_PCICFG_REG_WINDOW_ADDRESS, offset);
  240. REG_WR(bp, BNX2_PCICFG_REG_WINDOW, val);
  241. spin_unlock_bh(&bp->indirect_lock);
  242. }
  243. static void
  244. bnx2_ctx_wr(struct bnx2 *bp, u32 cid_addr, u32 offset, u32 val)
  245. {
  246. offset += cid_addr;
  247. spin_lock_bh(&bp->indirect_lock);
  248. if (CHIP_NUM(bp) == CHIP_NUM_5709) {
  249. int i;
  250. REG_WR(bp, BNX2_CTX_CTX_DATA, val);
  251. REG_WR(bp, BNX2_CTX_CTX_CTRL,
  252. offset | BNX2_CTX_CTX_CTRL_WRITE_REQ);
  253. for (i = 0; i < 5; i++) {
  254. u32 val;
  255. val = REG_RD(bp, BNX2_CTX_CTX_CTRL);
  256. if ((val & BNX2_CTX_CTX_CTRL_WRITE_REQ) == 0)
  257. break;
  258. udelay(5);
  259. }
  260. } else {
  261. REG_WR(bp, BNX2_CTX_DATA_ADR, offset);
  262. REG_WR(bp, BNX2_CTX_DATA, val);
  263. }
  264. spin_unlock_bh(&bp->indirect_lock);
  265. }
  266. static int
  267. bnx2_read_phy(struct bnx2 *bp, u32 reg, u32 *val)
  268. {
  269. u32 val1;
  270. int i, ret;
  271. if (bp->phy_flags & PHY_INT_MODE_AUTO_POLLING_FLAG) {
  272. val1 = REG_RD(bp, BNX2_EMAC_MDIO_MODE);
  273. val1 &= ~BNX2_EMAC_MDIO_MODE_AUTO_POLL;
  274. REG_WR(bp, BNX2_EMAC_MDIO_MODE, val1);
  275. REG_RD(bp, BNX2_EMAC_MDIO_MODE);
  276. udelay(40);
  277. }
  278. val1 = (bp->phy_addr << 21) | (reg << 16) |
  279. BNX2_EMAC_MDIO_COMM_COMMAND_READ | BNX2_EMAC_MDIO_COMM_DISEXT |
  280. BNX2_EMAC_MDIO_COMM_START_BUSY;
  281. REG_WR(bp, BNX2_EMAC_MDIO_COMM, val1);
  282. for (i = 0; i < 50; i++) {
  283. udelay(10);
  284. val1 = REG_RD(bp, BNX2_EMAC_MDIO_COMM);
  285. if (!(val1 & BNX2_EMAC_MDIO_COMM_START_BUSY)) {
  286. udelay(5);
  287. val1 = REG_RD(bp, BNX2_EMAC_MDIO_COMM);
  288. val1 &= BNX2_EMAC_MDIO_COMM_DATA;
  289. break;
  290. }
  291. }
  292. if (val1 & BNX2_EMAC_MDIO_COMM_START_BUSY) {
  293. *val = 0x0;
  294. ret = -EBUSY;
  295. }
  296. else {
  297. *val = val1;
  298. ret = 0;
  299. }
  300. if (bp->phy_flags & PHY_INT_MODE_AUTO_POLLING_FLAG) {
  301. val1 = REG_RD(bp, BNX2_EMAC_MDIO_MODE);
  302. val1 |= BNX2_EMAC_MDIO_MODE_AUTO_POLL;
  303. REG_WR(bp, BNX2_EMAC_MDIO_MODE, val1);
  304. REG_RD(bp, BNX2_EMAC_MDIO_MODE);
  305. udelay(40);
  306. }
  307. return ret;
  308. }
  309. static int
  310. bnx2_write_phy(struct bnx2 *bp, u32 reg, u32 val)
  311. {
  312. u32 val1;
  313. int i, ret;
  314. if (bp->phy_flags & PHY_INT_MODE_AUTO_POLLING_FLAG) {
  315. val1 = REG_RD(bp, BNX2_EMAC_MDIO_MODE);
  316. val1 &= ~BNX2_EMAC_MDIO_MODE_AUTO_POLL;
  317. REG_WR(bp, BNX2_EMAC_MDIO_MODE, val1);
  318. REG_RD(bp, BNX2_EMAC_MDIO_MODE);
  319. udelay(40);
  320. }
  321. val1 = (bp->phy_addr << 21) | (reg << 16) | val |
  322. BNX2_EMAC_MDIO_COMM_COMMAND_WRITE |
  323. BNX2_EMAC_MDIO_COMM_START_BUSY | BNX2_EMAC_MDIO_COMM_DISEXT;
  324. REG_WR(bp, BNX2_EMAC_MDIO_COMM, val1);
  325. for (i = 0; i < 50; i++) {
  326. udelay(10);
  327. val1 = REG_RD(bp, BNX2_EMAC_MDIO_COMM);
  328. if (!(val1 & BNX2_EMAC_MDIO_COMM_START_BUSY)) {
  329. udelay(5);
  330. break;
  331. }
  332. }
  333. if (val1 & BNX2_EMAC_MDIO_COMM_START_BUSY)
  334. ret = -EBUSY;
  335. else
  336. ret = 0;
  337. if (bp->phy_flags & PHY_INT_MODE_AUTO_POLLING_FLAG) {
  338. val1 = REG_RD(bp, BNX2_EMAC_MDIO_MODE);
  339. val1 |= BNX2_EMAC_MDIO_MODE_AUTO_POLL;
  340. REG_WR(bp, BNX2_EMAC_MDIO_MODE, val1);
  341. REG_RD(bp, BNX2_EMAC_MDIO_MODE);
  342. udelay(40);
  343. }
  344. return ret;
  345. }
  346. static void
  347. bnx2_disable_int(struct bnx2 *bp)
  348. {
  349. REG_WR(bp, BNX2_PCICFG_INT_ACK_CMD,
  350. BNX2_PCICFG_INT_ACK_CMD_MASK_INT);
  351. REG_RD(bp, BNX2_PCICFG_INT_ACK_CMD);
  352. }
  353. static void
  354. bnx2_enable_int(struct bnx2 *bp)
  355. {
  356. struct bnx2_napi *bnapi = &bp->bnx2_napi;
  357. REG_WR(bp, BNX2_PCICFG_INT_ACK_CMD,
  358. BNX2_PCICFG_INT_ACK_CMD_INDEX_VALID |
  359. BNX2_PCICFG_INT_ACK_CMD_MASK_INT | bnapi->last_status_idx);
  360. REG_WR(bp, BNX2_PCICFG_INT_ACK_CMD,
  361. BNX2_PCICFG_INT_ACK_CMD_INDEX_VALID | bnapi->last_status_idx);
  362. REG_WR(bp, BNX2_HC_COMMAND, bp->hc_cmd | BNX2_HC_COMMAND_COAL_NOW);
  363. }
  364. static void
  365. bnx2_disable_int_sync(struct bnx2 *bp)
  366. {
  367. atomic_inc(&bp->intr_sem);
  368. bnx2_disable_int(bp);
  369. synchronize_irq(bp->pdev->irq);
  370. }
  371. static void
  372. bnx2_napi_disable(struct bnx2 *bp)
  373. {
  374. napi_disable(&bp->bnx2_napi.napi);
  375. }
  376. static void
  377. bnx2_napi_enable(struct bnx2 *bp)
  378. {
  379. napi_enable(&bp->bnx2_napi.napi);
  380. }
  381. static void
  382. bnx2_netif_stop(struct bnx2 *bp)
  383. {
  384. bnx2_disable_int_sync(bp);
  385. if (netif_running(bp->dev)) {
  386. bnx2_napi_disable(bp);
  387. netif_tx_disable(bp->dev);
  388. bp->dev->trans_start = jiffies; /* prevent tx timeout */
  389. }
  390. }
  391. static void
  392. bnx2_netif_start(struct bnx2 *bp)
  393. {
  394. if (atomic_dec_and_test(&bp->intr_sem)) {
  395. if (netif_running(bp->dev)) {
  396. netif_wake_queue(bp->dev);
  397. bnx2_napi_enable(bp);
  398. bnx2_enable_int(bp);
  399. }
  400. }
  401. }
  402. static void
  403. bnx2_free_mem(struct bnx2 *bp)
  404. {
  405. int i;
  406. for (i = 0; i < bp->ctx_pages; i++) {
  407. if (bp->ctx_blk[i]) {
  408. pci_free_consistent(bp->pdev, BCM_PAGE_SIZE,
  409. bp->ctx_blk[i],
  410. bp->ctx_blk_mapping[i]);
  411. bp->ctx_blk[i] = NULL;
  412. }
  413. }
  414. if (bp->status_blk) {
  415. pci_free_consistent(bp->pdev, bp->status_stats_size,
  416. bp->status_blk, bp->status_blk_mapping);
  417. bp->status_blk = NULL;
  418. bp->stats_blk = NULL;
  419. }
  420. if (bp->tx_desc_ring) {
  421. pci_free_consistent(bp->pdev, TXBD_RING_SIZE,
  422. bp->tx_desc_ring, bp->tx_desc_mapping);
  423. bp->tx_desc_ring = NULL;
  424. }
  425. kfree(bp->tx_buf_ring);
  426. bp->tx_buf_ring = NULL;
  427. for (i = 0; i < bp->rx_max_ring; i++) {
  428. if (bp->rx_desc_ring[i])
  429. pci_free_consistent(bp->pdev, RXBD_RING_SIZE,
  430. bp->rx_desc_ring[i],
  431. bp->rx_desc_mapping[i]);
  432. bp->rx_desc_ring[i] = NULL;
  433. }
  434. vfree(bp->rx_buf_ring);
  435. bp->rx_buf_ring = NULL;
  436. for (i = 0; i < bp->rx_max_pg_ring; i++) {
  437. if (bp->rx_pg_desc_ring[i])
  438. pci_free_consistent(bp->pdev, RXBD_RING_SIZE,
  439. bp->rx_pg_desc_ring[i],
  440. bp->rx_pg_desc_mapping[i]);
  441. bp->rx_pg_desc_ring[i] = NULL;
  442. }
  443. if (bp->rx_pg_ring)
  444. vfree(bp->rx_pg_ring);
  445. bp->rx_pg_ring = NULL;
  446. }
  447. static int
  448. bnx2_alloc_mem(struct bnx2 *bp)
  449. {
  450. int i, status_blk_size;
  451. bp->tx_buf_ring = kzalloc(SW_TXBD_RING_SIZE, GFP_KERNEL);
  452. if (bp->tx_buf_ring == NULL)
  453. return -ENOMEM;
  454. bp->tx_desc_ring = pci_alloc_consistent(bp->pdev, TXBD_RING_SIZE,
  455. &bp->tx_desc_mapping);
  456. if (bp->tx_desc_ring == NULL)
  457. goto alloc_mem_err;
  458. bp->rx_buf_ring = vmalloc(SW_RXBD_RING_SIZE * bp->rx_max_ring);
  459. if (bp->rx_buf_ring == NULL)
  460. goto alloc_mem_err;
  461. memset(bp->rx_buf_ring, 0, SW_RXBD_RING_SIZE * bp->rx_max_ring);
  462. for (i = 0; i < bp->rx_max_ring; i++) {
  463. bp->rx_desc_ring[i] =
  464. pci_alloc_consistent(bp->pdev, RXBD_RING_SIZE,
  465. &bp->rx_desc_mapping[i]);
  466. if (bp->rx_desc_ring[i] == NULL)
  467. goto alloc_mem_err;
  468. }
  469. if (bp->rx_pg_ring_size) {
  470. bp->rx_pg_ring = vmalloc(SW_RXPG_RING_SIZE *
  471. bp->rx_max_pg_ring);
  472. if (bp->rx_pg_ring == NULL)
  473. goto alloc_mem_err;
  474. memset(bp->rx_pg_ring, 0, SW_RXPG_RING_SIZE *
  475. bp->rx_max_pg_ring);
  476. }
  477. for (i = 0; i < bp->rx_max_pg_ring; i++) {
  478. bp->rx_pg_desc_ring[i] =
  479. pci_alloc_consistent(bp->pdev, RXBD_RING_SIZE,
  480. &bp->rx_pg_desc_mapping[i]);
  481. if (bp->rx_pg_desc_ring[i] == NULL)
  482. goto alloc_mem_err;
  483. }
  484. /* Combine status and statistics blocks into one allocation. */
  485. status_blk_size = L1_CACHE_ALIGN(sizeof(struct status_block));
  486. bp->status_stats_size = status_blk_size +
  487. sizeof(struct statistics_block);
  488. bp->status_blk = pci_alloc_consistent(bp->pdev, bp->status_stats_size,
  489. &bp->status_blk_mapping);
  490. if (bp->status_blk == NULL)
  491. goto alloc_mem_err;
  492. memset(bp->status_blk, 0, bp->status_stats_size);
  493. bp->bnx2_napi.status_blk = bp->status_blk;
  494. bp->stats_blk = (void *) ((unsigned long) bp->status_blk +
  495. status_blk_size);
  496. bp->stats_blk_mapping = bp->status_blk_mapping + status_blk_size;
  497. if (CHIP_NUM(bp) == CHIP_NUM_5709) {
  498. bp->ctx_pages = 0x2000 / BCM_PAGE_SIZE;
  499. if (bp->ctx_pages == 0)
  500. bp->ctx_pages = 1;
  501. for (i = 0; i < bp->ctx_pages; i++) {
  502. bp->ctx_blk[i] = pci_alloc_consistent(bp->pdev,
  503. BCM_PAGE_SIZE,
  504. &bp->ctx_blk_mapping[i]);
  505. if (bp->ctx_blk[i] == NULL)
  506. goto alloc_mem_err;
  507. }
  508. }
  509. return 0;
  510. alloc_mem_err:
  511. bnx2_free_mem(bp);
  512. return -ENOMEM;
  513. }
  514. static void
  515. bnx2_report_fw_link(struct bnx2 *bp)
  516. {
  517. u32 fw_link_status = 0;
  518. if (bp->phy_flags & REMOTE_PHY_CAP_FLAG)
  519. return;
  520. if (bp->link_up) {
  521. u32 bmsr;
  522. switch (bp->line_speed) {
  523. case SPEED_10:
  524. if (bp->duplex == DUPLEX_HALF)
  525. fw_link_status = BNX2_LINK_STATUS_10HALF;
  526. else
  527. fw_link_status = BNX2_LINK_STATUS_10FULL;
  528. break;
  529. case SPEED_100:
  530. if (bp->duplex == DUPLEX_HALF)
  531. fw_link_status = BNX2_LINK_STATUS_100HALF;
  532. else
  533. fw_link_status = BNX2_LINK_STATUS_100FULL;
  534. break;
  535. case SPEED_1000:
  536. if (bp->duplex == DUPLEX_HALF)
  537. fw_link_status = BNX2_LINK_STATUS_1000HALF;
  538. else
  539. fw_link_status = BNX2_LINK_STATUS_1000FULL;
  540. break;
  541. case SPEED_2500:
  542. if (bp->duplex == DUPLEX_HALF)
  543. fw_link_status = BNX2_LINK_STATUS_2500HALF;
  544. else
  545. fw_link_status = BNX2_LINK_STATUS_2500FULL;
  546. break;
  547. }
  548. fw_link_status |= BNX2_LINK_STATUS_LINK_UP;
  549. if (bp->autoneg) {
  550. fw_link_status |= BNX2_LINK_STATUS_AN_ENABLED;
  551. bnx2_read_phy(bp, bp->mii_bmsr, &bmsr);
  552. bnx2_read_phy(bp, bp->mii_bmsr, &bmsr);
  553. if (!(bmsr & BMSR_ANEGCOMPLETE) ||
  554. bp->phy_flags & PHY_PARALLEL_DETECT_FLAG)
  555. fw_link_status |= BNX2_LINK_STATUS_PARALLEL_DET;
  556. else
  557. fw_link_status |= BNX2_LINK_STATUS_AN_COMPLETE;
  558. }
  559. }
  560. else
  561. fw_link_status = BNX2_LINK_STATUS_LINK_DOWN;
  562. REG_WR_IND(bp, bp->shmem_base + BNX2_LINK_STATUS, fw_link_status);
  563. }
  564. static char *
  565. bnx2_xceiver_str(struct bnx2 *bp)
  566. {
  567. return ((bp->phy_port == PORT_FIBRE) ? "SerDes" :
  568. ((bp->phy_flags & PHY_SERDES_FLAG) ? "Remote Copper" :
  569. "Copper"));
  570. }
  571. static void
  572. bnx2_report_link(struct bnx2 *bp)
  573. {
  574. if (bp->link_up) {
  575. netif_carrier_on(bp->dev);
  576. printk(KERN_INFO PFX "%s NIC %s Link is Up, ", bp->dev->name,
  577. bnx2_xceiver_str(bp));
  578. printk("%d Mbps ", bp->line_speed);
  579. if (bp->duplex == DUPLEX_FULL)
  580. printk("full duplex");
  581. else
  582. printk("half duplex");
  583. if (bp->flow_ctrl) {
  584. if (bp->flow_ctrl & FLOW_CTRL_RX) {
  585. printk(", receive ");
  586. if (bp->flow_ctrl & FLOW_CTRL_TX)
  587. printk("& transmit ");
  588. }
  589. else {
  590. printk(", transmit ");
  591. }
  592. printk("flow control ON");
  593. }
  594. printk("\n");
  595. }
  596. else {
  597. netif_carrier_off(bp->dev);
  598. printk(KERN_ERR PFX "%s NIC %s Link is Down\n", bp->dev->name,
  599. bnx2_xceiver_str(bp));
  600. }
  601. bnx2_report_fw_link(bp);
  602. }
  603. static void
  604. bnx2_resolve_flow_ctrl(struct bnx2 *bp)
  605. {
  606. u32 local_adv, remote_adv;
  607. bp->flow_ctrl = 0;
  608. if ((bp->autoneg & (AUTONEG_SPEED | AUTONEG_FLOW_CTRL)) !=
  609. (AUTONEG_SPEED | AUTONEG_FLOW_CTRL)) {
  610. if (bp->duplex == DUPLEX_FULL) {
  611. bp->flow_ctrl = bp->req_flow_ctrl;
  612. }
  613. return;
  614. }
  615. if (bp->duplex != DUPLEX_FULL) {
  616. return;
  617. }
  618. if ((bp->phy_flags & PHY_SERDES_FLAG) &&
  619. (CHIP_NUM(bp) == CHIP_NUM_5708)) {
  620. u32 val;
  621. bnx2_read_phy(bp, BCM5708S_1000X_STAT1, &val);
  622. if (val & BCM5708S_1000X_STAT1_TX_PAUSE)
  623. bp->flow_ctrl |= FLOW_CTRL_TX;
  624. if (val & BCM5708S_1000X_STAT1_RX_PAUSE)
  625. bp->flow_ctrl |= FLOW_CTRL_RX;
  626. return;
  627. }
  628. bnx2_read_phy(bp, bp->mii_adv, &local_adv);
  629. bnx2_read_phy(bp, bp->mii_lpa, &remote_adv);
  630. if (bp->phy_flags & PHY_SERDES_FLAG) {
  631. u32 new_local_adv = 0;
  632. u32 new_remote_adv = 0;
  633. if (local_adv & ADVERTISE_1000XPAUSE)
  634. new_local_adv |= ADVERTISE_PAUSE_CAP;
  635. if (local_adv & ADVERTISE_1000XPSE_ASYM)
  636. new_local_adv |= ADVERTISE_PAUSE_ASYM;
  637. if (remote_adv & ADVERTISE_1000XPAUSE)
  638. new_remote_adv |= ADVERTISE_PAUSE_CAP;
  639. if (remote_adv & ADVERTISE_1000XPSE_ASYM)
  640. new_remote_adv |= ADVERTISE_PAUSE_ASYM;
  641. local_adv = new_local_adv;
  642. remote_adv = new_remote_adv;
  643. }
  644. /* See Table 28B-3 of 802.3ab-1999 spec. */
  645. if (local_adv & ADVERTISE_PAUSE_CAP) {
  646. if(local_adv & ADVERTISE_PAUSE_ASYM) {
  647. if (remote_adv & ADVERTISE_PAUSE_CAP) {
  648. bp->flow_ctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
  649. }
  650. else if (remote_adv & ADVERTISE_PAUSE_ASYM) {
  651. bp->flow_ctrl = FLOW_CTRL_RX;
  652. }
  653. }
  654. else {
  655. if (remote_adv & ADVERTISE_PAUSE_CAP) {
  656. bp->flow_ctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
  657. }
  658. }
  659. }
  660. else if (local_adv & ADVERTISE_PAUSE_ASYM) {
  661. if ((remote_adv & ADVERTISE_PAUSE_CAP) &&
  662. (remote_adv & ADVERTISE_PAUSE_ASYM)) {
  663. bp->flow_ctrl = FLOW_CTRL_TX;
  664. }
  665. }
  666. }
  667. static int
  668. bnx2_5709s_linkup(struct bnx2 *bp)
  669. {
  670. u32 val, speed;
  671. bp->link_up = 1;
  672. bnx2_write_phy(bp, MII_BNX2_BLK_ADDR, MII_BNX2_BLK_ADDR_GP_STATUS);
  673. bnx2_read_phy(bp, MII_BNX2_GP_TOP_AN_STATUS1, &val);
  674. bnx2_write_phy(bp, MII_BNX2_BLK_ADDR, MII_BNX2_BLK_ADDR_COMBO_IEEEB0);
  675. if ((bp->autoneg & AUTONEG_SPEED) == 0) {
  676. bp->line_speed = bp->req_line_speed;
  677. bp->duplex = bp->req_duplex;
  678. return 0;
  679. }
  680. speed = val & MII_BNX2_GP_TOP_AN_SPEED_MSK;
  681. switch (speed) {
  682. case MII_BNX2_GP_TOP_AN_SPEED_10:
  683. bp->line_speed = SPEED_10;
  684. break;
  685. case MII_BNX2_GP_TOP_AN_SPEED_100:
  686. bp->line_speed = SPEED_100;
  687. break;
  688. case MII_BNX2_GP_TOP_AN_SPEED_1G:
  689. case MII_BNX2_GP_TOP_AN_SPEED_1GKV:
  690. bp->line_speed = SPEED_1000;
  691. break;
  692. case MII_BNX2_GP_TOP_AN_SPEED_2_5G:
  693. bp->line_speed = SPEED_2500;
  694. break;
  695. }
  696. if (val & MII_BNX2_GP_TOP_AN_FD)
  697. bp->duplex = DUPLEX_FULL;
  698. else
  699. bp->duplex = DUPLEX_HALF;
  700. return 0;
  701. }
  702. static int
  703. bnx2_5708s_linkup(struct bnx2 *bp)
  704. {
  705. u32 val;
  706. bp->link_up = 1;
  707. bnx2_read_phy(bp, BCM5708S_1000X_STAT1, &val);
  708. switch (val & BCM5708S_1000X_STAT1_SPEED_MASK) {
  709. case BCM5708S_1000X_STAT1_SPEED_10:
  710. bp->line_speed = SPEED_10;
  711. break;
  712. case BCM5708S_1000X_STAT1_SPEED_100:
  713. bp->line_speed = SPEED_100;
  714. break;
  715. case BCM5708S_1000X_STAT1_SPEED_1G:
  716. bp->line_speed = SPEED_1000;
  717. break;
  718. case BCM5708S_1000X_STAT1_SPEED_2G5:
  719. bp->line_speed = SPEED_2500;
  720. break;
  721. }
  722. if (val & BCM5708S_1000X_STAT1_FD)
  723. bp->duplex = DUPLEX_FULL;
  724. else
  725. bp->duplex = DUPLEX_HALF;
  726. return 0;
  727. }
  728. static int
  729. bnx2_5706s_linkup(struct bnx2 *bp)
  730. {
  731. u32 bmcr, local_adv, remote_adv, common;
  732. bp->link_up = 1;
  733. bp->line_speed = SPEED_1000;
  734. bnx2_read_phy(bp, bp->mii_bmcr, &bmcr);
  735. if (bmcr & BMCR_FULLDPLX) {
  736. bp->duplex = DUPLEX_FULL;
  737. }
  738. else {
  739. bp->duplex = DUPLEX_HALF;
  740. }
  741. if (!(bmcr & BMCR_ANENABLE)) {
  742. return 0;
  743. }
  744. bnx2_read_phy(bp, bp->mii_adv, &local_adv);
  745. bnx2_read_phy(bp, bp->mii_lpa, &remote_adv);
  746. common = local_adv & remote_adv;
  747. if (common & (ADVERTISE_1000XHALF | ADVERTISE_1000XFULL)) {
  748. if (common & ADVERTISE_1000XFULL) {
  749. bp->duplex = DUPLEX_FULL;
  750. }
  751. else {
  752. bp->duplex = DUPLEX_HALF;
  753. }
  754. }
  755. return 0;
  756. }
  757. static int
  758. bnx2_copper_linkup(struct bnx2 *bp)
  759. {
  760. u32 bmcr;
  761. bnx2_read_phy(bp, bp->mii_bmcr, &bmcr);
  762. if (bmcr & BMCR_ANENABLE) {
  763. u32 local_adv, remote_adv, common;
  764. bnx2_read_phy(bp, MII_CTRL1000, &local_adv);
  765. bnx2_read_phy(bp, MII_STAT1000, &remote_adv);
  766. common = local_adv & (remote_adv >> 2);
  767. if (common & ADVERTISE_1000FULL) {
  768. bp->line_speed = SPEED_1000;
  769. bp->duplex = DUPLEX_FULL;
  770. }
  771. else if (common & ADVERTISE_1000HALF) {
  772. bp->line_speed = SPEED_1000;
  773. bp->duplex = DUPLEX_HALF;
  774. }
  775. else {
  776. bnx2_read_phy(bp, bp->mii_adv, &local_adv);
  777. bnx2_read_phy(bp, bp->mii_lpa, &remote_adv);
  778. common = local_adv & remote_adv;
  779. if (common & ADVERTISE_100FULL) {
  780. bp->line_speed = SPEED_100;
  781. bp->duplex = DUPLEX_FULL;
  782. }
  783. else if (common & ADVERTISE_100HALF) {
  784. bp->line_speed = SPEED_100;
  785. bp->duplex = DUPLEX_HALF;
  786. }
  787. else if (common & ADVERTISE_10FULL) {
  788. bp->line_speed = SPEED_10;
  789. bp->duplex = DUPLEX_FULL;
  790. }
  791. else if (common & ADVERTISE_10HALF) {
  792. bp->line_speed = SPEED_10;
  793. bp->duplex = DUPLEX_HALF;
  794. }
  795. else {
  796. bp->line_speed = 0;
  797. bp->link_up = 0;
  798. }
  799. }
  800. }
  801. else {
  802. if (bmcr & BMCR_SPEED100) {
  803. bp->line_speed = SPEED_100;
  804. }
  805. else {
  806. bp->line_speed = SPEED_10;
  807. }
  808. if (bmcr & BMCR_FULLDPLX) {
  809. bp->duplex = DUPLEX_FULL;
  810. }
  811. else {
  812. bp->duplex = DUPLEX_HALF;
  813. }
  814. }
  815. return 0;
  816. }
  817. static int
  818. bnx2_set_mac_link(struct bnx2 *bp)
  819. {
  820. u32 val;
  821. REG_WR(bp, BNX2_EMAC_TX_LENGTHS, 0x2620);
  822. if (bp->link_up && (bp->line_speed == SPEED_1000) &&
  823. (bp->duplex == DUPLEX_HALF)) {
  824. REG_WR(bp, BNX2_EMAC_TX_LENGTHS, 0x26ff);
  825. }
  826. /* Configure the EMAC mode register. */
  827. val = REG_RD(bp, BNX2_EMAC_MODE);
  828. val &= ~(BNX2_EMAC_MODE_PORT | BNX2_EMAC_MODE_HALF_DUPLEX |
  829. BNX2_EMAC_MODE_MAC_LOOP | BNX2_EMAC_MODE_FORCE_LINK |
  830. BNX2_EMAC_MODE_25G_MODE);
  831. if (bp->link_up) {
  832. switch (bp->line_speed) {
  833. case SPEED_10:
  834. if (CHIP_NUM(bp) != CHIP_NUM_5706) {
  835. val |= BNX2_EMAC_MODE_PORT_MII_10M;
  836. break;
  837. }
  838. /* fall through */
  839. case SPEED_100:
  840. val |= BNX2_EMAC_MODE_PORT_MII;
  841. break;
  842. case SPEED_2500:
  843. val |= BNX2_EMAC_MODE_25G_MODE;
  844. /* fall through */
  845. case SPEED_1000:
  846. val |= BNX2_EMAC_MODE_PORT_GMII;
  847. break;
  848. }
  849. }
  850. else {
  851. val |= BNX2_EMAC_MODE_PORT_GMII;
  852. }
  853. /* Set the MAC to operate in the appropriate duplex mode. */
  854. if (bp->duplex == DUPLEX_HALF)
  855. val |= BNX2_EMAC_MODE_HALF_DUPLEX;
  856. REG_WR(bp, BNX2_EMAC_MODE, val);
  857. /* Enable/disable rx PAUSE. */
  858. bp->rx_mode &= ~BNX2_EMAC_RX_MODE_FLOW_EN;
  859. if (bp->flow_ctrl & FLOW_CTRL_RX)
  860. bp->rx_mode |= BNX2_EMAC_RX_MODE_FLOW_EN;
  861. REG_WR(bp, BNX2_EMAC_RX_MODE, bp->rx_mode);
  862. /* Enable/disable tx PAUSE. */
  863. val = REG_RD(bp, BNX2_EMAC_TX_MODE);
  864. val &= ~BNX2_EMAC_TX_MODE_FLOW_EN;
  865. if (bp->flow_ctrl & FLOW_CTRL_TX)
  866. val |= BNX2_EMAC_TX_MODE_FLOW_EN;
  867. REG_WR(bp, BNX2_EMAC_TX_MODE, val);
  868. /* Acknowledge the interrupt. */
  869. REG_WR(bp, BNX2_EMAC_STATUS, BNX2_EMAC_STATUS_LINK_CHANGE);
  870. return 0;
  871. }
  872. static void
  873. bnx2_enable_bmsr1(struct bnx2 *bp)
  874. {
  875. if ((bp->phy_flags & PHY_SERDES_FLAG) &&
  876. (CHIP_NUM(bp) == CHIP_NUM_5709))
  877. bnx2_write_phy(bp, MII_BNX2_BLK_ADDR,
  878. MII_BNX2_BLK_ADDR_GP_STATUS);
  879. }
  880. static void
  881. bnx2_disable_bmsr1(struct bnx2 *bp)
  882. {
  883. if ((bp->phy_flags & PHY_SERDES_FLAG) &&
  884. (CHIP_NUM(bp) == CHIP_NUM_5709))
  885. bnx2_write_phy(bp, MII_BNX2_BLK_ADDR,
  886. MII_BNX2_BLK_ADDR_COMBO_IEEEB0);
  887. }
  888. static int
  889. bnx2_test_and_enable_2g5(struct bnx2 *bp)
  890. {
  891. u32 up1;
  892. int ret = 1;
  893. if (!(bp->phy_flags & PHY_2_5G_CAPABLE_FLAG))
  894. return 0;
  895. if (bp->autoneg & AUTONEG_SPEED)
  896. bp->advertising |= ADVERTISED_2500baseX_Full;
  897. if (CHIP_NUM(bp) == CHIP_NUM_5709)
  898. bnx2_write_phy(bp, MII_BNX2_BLK_ADDR, MII_BNX2_BLK_ADDR_OVER1G);
  899. bnx2_read_phy(bp, bp->mii_up1, &up1);
  900. if (!(up1 & BCM5708S_UP1_2G5)) {
  901. up1 |= BCM5708S_UP1_2G5;
  902. bnx2_write_phy(bp, bp->mii_up1, up1);
  903. ret = 0;
  904. }
  905. if (CHIP_NUM(bp) == CHIP_NUM_5709)
  906. bnx2_write_phy(bp, MII_BNX2_BLK_ADDR,
  907. MII_BNX2_BLK_ADDR_COMBO_IEEEB0);
  908. return ret;
  909. }
  910. static int
  911. bnx2_test_and_disable_2g5(struct bnx2 *bp)
  912. {
  913. u32 up1;
  914. int ret = 0;
  915. if (!(bp->phy_flags & PHY_2_5G_CAPABLE_FLAG))
  916. return 0;
  917. if (CHIP_NUM(bp) == CHIP_NUM_5709)
  918. bnx2_write_phy(bp, MII_BNX2_BLK_ADDR, MII_BNX2_BLK_ADDR_OVER1G);
  919. bnx2_read_phy(bp, bp->mii_up1, &up1);
  920. if (up1 & BCM5708S_UP1_2G5) {
  921. up1 &= ~BCM5708S_UP1_2G5;
  922. bnx2_write_phy(bp, bp->mii_up1, up1);
  923. ret = 1;
  924. }
  925. if (CHIP_NUM(bp) == CHIP_NUM_5709)
  926. bnx2_write_phy(bp, MII_BNX2_BLK_ADDR,
  927. MII_BNX2_BLK_ADDR_COMBO_IEEEB0);
  928. return ret;
  929. }
  930. static void
  931. bnx2_enable_forced_2g5(struct bnx2 *bp)
  932. {
  933. u32 bmcr;
  934. if (!(bp->phy_flags & PHY_2_5G_CAPABLE_FLAG))
  935. return;
  936. if (CHIP_NUM(bp) == CHIP_NUM_5709) {
  937. u32 val;
  938. bnx2_write_phy(bp, MII_BNX2_BLK_ADDR,
  939. MII_BNX2_BLK_ADDR_SERDES_DIG);
  940. bnx2_read_phy(bp, MII_BNX2_SERDES_DIG_MISC1, &val);
  941. val &= ~MII_BNX2_SD_MISC1_FORCE_MSK;
  942. val |= MII_BNX2_SD_MISC1_FORCE | MII_BNX2_SD_MISC1_FORCE_2_5G;
  943. bnx2_write_phy(bp, MII_BNX2_SERDES_DIG_MISC1, val);
  944. bnx2_write_phy(bp, MII_BNX2_BLK_ADDR,
  945. MII_BNX2_BLK_ADDR_COMBO_IEEEB0);
  946. bnx2_read_phy(bp, bp->mii_bmcr, &bmcr);
  947. } else if (CHIP_NUM(bp) == CHIP_NUM_5708) {
  948. bnx2_read_phy(bp, bp->mii_bmcr, &bmcr);
  949. bmcr |= BCM5708S_BMCR_FORCE_2500;
  950. }
  951. if (bp->autoneg & AUTONEG_SPEED) {
  952. bmcr &= ~BMCR_ANENABLE;
  953. if (bp->req_duplex == DUPLEX_FULL)
  954. bmcr |= BMCR_FULLDPLX;
  955. }
  956. bnx2_write_phy(bp, bp->mii_bmcr, bmcr);
  957. }
  958. static void
  959. bnx2_disable_forced_2g5(struct bnx2 *bp)
  960. {
  961. u32 bmcr;
  962. if (!(bp->phy_flags & PHY_2_5G_CAPABLE_FLAG))
  963. return;
  964. if (CHIP_NUM(bp) == CHIP_NUM_5709) {
  965. u32 val;
  966. bnx2_write_phy(bp, MII_BNX2_BLK_ADDR,
  967. MII_BNX2_BLK_ADDR_SERDES_DIG);
  968. bnx2_read_phy(bp, MII_BNX2_SERDES_DIG_MISC1, &val);
  969. val &= ~MII_BNX2_SD_MISC1_FORCE;
  970. bnx2_write_phy(bp, MII_BNX2_SERDES_DIG_MISC1, val);
  971. bnx2_write_phy(bp, MII_BNX2_BLK_ADDR,
  972. MII_BNX2_BLK_ADDR_COMBO_IEEEB0);
  973. bnx2_read_phy(bp, bp->mii_bmcr, &bmcr);
  974. } else if (CHIP_NUM(bp) == CHIP_NUM_5708) {
  975. bnx2_read_phy(bp, bp->mii_bmcr, &bmcr);
  976. bmcr &= ~BCM5708S_BMCR_FORCE_2500;
  977. }
  978. if (bp->autoneg & AUTONEG_SPEED)
  979. bmcr |= BMCR_SPEED1000 | BMCR_ANENABLE | BMCR_ANRESTART;
  980. bnx2_write_phy(bp, bp->mii_bmcr, bmcr);
  981. }
  982. static int
  983. bnx2_set_link(struct bnx2 *bp)
  984. {
  985. u32 bmsr;
  986. u8 link_up;
  987. if (bp->loopback == MAC_LOOPBACK || bp->loopback == PHY_LOOPBACK) {
  988. bp->link_up = 1;
  989. return 0;
  990. }
  991. if (bp->phy_flags & REMOTE_PHY_CAP_FLAG)
  992. return 0;
  993. link_up = bp->link_up;
  994. bnx2_enable_bmsr1(bp);
  995. bnx2_read_phy(bp, bp->mii_bmsr1, &bmsr);
  996. bnx2_read_phy(bp, bp->mii_bmsr1, &bmsr);
  997. bnx2_disable_bmsr1(bp);
  998. if ((bp->phy_flags & PHY_SERDES_FLAG) &&
  999. (CHIP_NUM(bp) == CHIP_NUM_5706)) {
  1000. u32 val;
  1001. val = REG_RD(bp, BNX2_EMAC_STATUS);
  1002. if (val & BNX2_EMAC_STATUS_LINK)
  1003. bmsr |= BMSR_LSTATUS;
  1004. else
  1005. bmsr &= ~BMSR_LSTATUS;
  1006. }
  1007. if (bmsr & BMSR_LSTATUS) {
  1008. bp->link_up = 1;
  1009. if (bp->phy_flags & PHY_SERDES_FLAG) {
  1010. if (CHIP_NUM(bp) == CHIP_NUM_5706)
  1011. bnx2_5706s_linkup(bp);
  1012. else if (CHIP_NUM(bp) == CHIP_NUM_5708)
  1013. bnx2_5708s_linkup(bp);
  1014. else if (CHIP_NUM(bp) == CHIP_NUM_5709)
  1015. bnx2_5709s_linkup(bp);
  1016. }
  1017. else {
  1018. bnx2_copper_linkup(bp);
  1019. }
  1020. bnx2_resolve_flow_ctrl(bp);
  1021. }
  1022. else {
  1023. if ((bp->phy_flags & PHY_SERDES_FLAG) &&
  1024. (bp->autoneg & AUTONEG_SPEED))
  1025. bnx2_disable_forced_2g5(bp);
  1026. bp->phy_flags &= ~PHY_PARALLEL_DETECT_FLAG;
  1027. bp->link_up = 0;
  1028. }
  1029. if (bp->link_up != link_up) {
  1030. bnx2_report_link(bp);
  1031. }
  1032. bnx2_set_mac_link(bp);
  1033. return 0;
  1034. }
  1035. static int
  1036. bnx2_reset_phy(struct bnx2 *bp)
  1037. {
  1038. int i;
  1039. u32 reg;
  1040. bnx2_write_phy(bp, bp->mii_bmcr, BMCR_RESET);
  1041. #define PHY_RESET_MAX_WAIT 100
  1042. for (i = 0; i < PHY_RESET_MAX_WAIT; i++) {
  1043. udelay(10);
  1044. bnx2_read_phy(bp, bp->mii_bmcr, &reg);
  1045. if (!(reg & BMCR_RESET)) {
  1046. udelay(20);
  1047. break;
  1048. }
  1049. }
  1050. if (i == PHY_RESET_MAX_WAIT) {
  1051. return -EBUSY;
  1052. }
  1053. return 0;
  1054. }
  1055. static u32
  1056. bnx2_phy_get_pause_adv(struct bnx2 *bp)
  1057. {
  1058. u32 adv = 0;
  1059. if ((bp->req_flow_ctrl & (FLOW_CTRL_RX | FLOW_CTRL_TX)) ==
  1060. (FLOW_CTRL_RX | FLOW_CTRL_TX)) {
  1061. if (bp->phy_flags & PHY_SERDES_FLAG) {
  1062. adv = ADVERTISE_1000XPAUSE;
  1063. }
  1064. else {
  1065. adv = ADVERTISE_PAUSE_CAP;
  1066. }
  1067. }
  1068. else if (bp->req_flow_ctrl & FLOW_CTRL_TX) {
  1069. if (bp->phy_flags & PHY_SERDES_FLAG) {
  1070. adv = ADVERTISE_1000XPSE_ASYM;
  1071. }
  1072. else {
  1073. adv = ADVERTISE_PAUSE_ASYM;
  1074. }
  1075. }
  1076. else if (bp->req_flow_ctrl & FLOW_CTRL_RX) {
  1077. if (bp->phy_flags & PHY_SERDES_FLAG) {
  1078. adv = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
  1079. }
  1080. else {
  1081. adv = ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
  1082. }
  1083. }
  1084. return adv;
  1085. }
  1086. static int bnx2_fw_sync(struct bnx2 *, u32, int);
  1087. static int
  1088. bnx2_setup_remote_phy(struct bnx2 *bp, u8 port)
  1089. {
  1090. u32 speed_arg = 0, pause_adv;
  1091. pause_adv = bnx2_phy_get_pause_adv(bp);
  1092. if (bp->autoneg & AUTONEG_SPEED) {
  1093. speed_arg |= BNX2_NETLINK_SET_LINK_ENABLE_AUTONEG;
  1094. if (bp->advertising & ADVERTISED_10baseT_Half)
  1095. speed_arg |= BNX2_NETLINK_SET_LINK_SPEED_10HALF;
  1096. if (bp->advertising & ADVERTISED_10baseT_Full)
  1097. speed_arg |= BNX2_NETLINK_SET_LINK_SPEED_10FULL;
  1098. if (bp->advertising & ADVERTISED_100baseT_Half)
  1099. speed_arg |= BNX2_NETLINK_SET_LINK_SPEED_100HALF;
  1100. if (bp->advertising & ADVERTISED_100baseT_Full)
  1101. speed_arg |= BNX2_NETLINK_SET_LINK_SPEED_100FULL;
  1102. if (bp->advertising & ADVERTISED_1000baseT_Full)
  1103. speed_arg |= BNX2_NETLINK_SET_LINK_SPEED_1GFULL;
  1104. if (bp->advertising & ADVERTISED_2500baseX_Full)
  1105. speed_arg |= BNX2_NETLINK_SET_LINK_SPEED_2G5FULL;
  1106. } else {
  1107. if (bp->req_line_speed == SPEED_2500)
  1108. speed_arg = BNX2_NETLINK_SET_LINK_SPEED_2G5FULL;
  1109. else if (bp->req_line_speed == SPEED_1000)
  1110. speed_arg = BNX2_NETLINK_SET_LINK_SPEED_1GFULL;
  1111. else if (bp->req_line_speed == SPEED_100) {
  1112. if (bp->req_duplex == DUPLEX_FULL)
  1113. speed_arg = BNX2_NETLINK_SET_LINK_SPEED_100FULL;
  1114. else
  1115. speed_arg = BNX2_NETLINK_SET_LINK_SPEED_100HALF;
  1116. } else if (bp->req_line_speed == SPEED_10) {
  1117. if (bp->req_duplex == DUPLEX_FULL)
  1118. speed_arg = BNX2_NETLINK_SET_LINK_SPEED_10FULL;
  1119. else
  1120. speed_arg = BNX2_NETLINK_SET_LINK_SPEED_10HALF;
  1121. }
  1122. }
  1123. if (pause_adv & (ADVERTISE_1000XPAUSE | ADVERTISE_PAUSE_CAP))
  1124. speed_arg |= BNX2_NETLINK_SET_LINK_FC_SYM_PAUSE;
  1125. if (pause_adv & (ADVERTISE_1000XPSE_ASYM | ADVERTISE_1000XPSE_ASYM))
  1126. speed_arg |= BNX2_NETLINK_SET_LINK_FC_ASYM_PAUSE;
  1127. if (port == PORT_TP)
  1128. speed_arg |= BNX2_NETLINK_SET_LINK_PHY_APP_REMOTE |
  1129. BNX2_NETLINK_SET_LINK_ETH_AT_WIRESPEED;
  1130. REG_WR_IND(bp, bp->shmem_base + BNX2_DRV_MB_ARG0, speed_arg);
  1131. spin_unlock_bh(&bp->phy_lock);
  1132. bnx2_fw_sync(bp, BNX2_DRV_MSG_CODE_CMD_SET_LINK, 0);
  1133. spin_lock_bh(&bp->phy_lock);
  1134. return 0;
  1135. }
  1136. static int
  1137. bnx2_setup_serdes_phy(struct bnx2 *bp, u8 port)
  1138. {
  1139. u32 adv, bmcr;
  1140. u32 new_adv = 0;
  1141. if (bp->phy_flags & REMOTE_PHY_CAP_FLAG)
  1142. return (bnx2_setup_remote_phy(bp, port));
  1143. if (!(bp->autoneg & AUTONEG_SPEED)) {
  1144. u32 new_bmcr;
  1145. int force_link_down = 0;
  1146. if (bp->req_line_speed == SPEED_2500) {
  1147. if (!bnx2_test_and_enable_2g5(bp))
  1148. force_link_down = 1;
  1149. } else if (bp->req_line_speed == SPEED_1000) {
  1150. if (bnx2_test_and_disable_2g5(bp))
  1151. force_link_down = 1;
  1152. }
  1153. bnx2_read_phy(bp, bp->mii_adv, &adv);
  1154. adv &= ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF);
  1155. bnx2_read_phy(bp, bp->mii_bmcr, &bmcr);
  1156. new_bmcr = bmcr & ~BMCR_ANENABLE;
  1157. new_bmcr |= BMCR_SPEED1000;
  1158. if (CHIP_NUM(bp) == CHIP_NUM_5709) {
  1159. if (bp->req_line_speed == SPEED_2500)
  1160. bnx2_enable_forced_2g5(bp);
  1161. else if (bp->req_line_speed == SPEED_1000) {
  1162. bnx2_disable_forced_2g5(bp);
  1163. new_bmcr &= ~0x2000;
  1164. }
  1165. } else if (CHIP_NUM(bp) == CHIP_NUM_5708) {
  1166. if (bp->req_line_speed == SPEED_2500)
  1167. new_bmcr |= BCM5708S_BMCR_FORCE_2500;
  1168. else
  1169. new_bmcr = bmcr & ~BCM5708S_BMCR_FORCE_2500;
  1170. }
  1171. if (bp->req_duplex == DUPLEX_FULL) {
  1172. adv |= ADVERTISE_1000XFULL;
  1173. new_bmcr |= BMCR_FULLDPLX;
  1174. }
  1175. else {
  1176. adv |= ADVERTISE_1000XHALF;
  1177. new_bmcr &= ~BMCR_FULLDPLX;
  1178. }
  1179. if ((new_bmcr != bmcr) || (force_link_down)) {
  1180. /* Force a link down visible on the other side */
  1181. if (bp->link_up) {
  1182. bnx2_write_phy(bp, bp->mii_adv, adv &
  1183. ~(ADVERTISE_1000XFULL |
  1184. ADVERTISE_1000XHALF));
  1185. bnx2_write_phy(bp, bp->mii_bmcr, bmcr |
  1186. BMCR_ANRESTART | BMCR_ANENABLE);
  1187. bp->link_up = 0;
  1188. netif_carrier_off(bp->dev);
  1189. bnx2_write_phy(bp, bp->mii_bmcr, new_bmcr);
  1190. bnx2_report_link(bp);
  1191. }
  1192. bnx2_write_phy(bp, bp->mii_adv, adv);
  1193. bnx2_write_phy(bp, bp->mii_bmcr, new_bmcr);
  1194. } else {
  1195. bnx2_resolve_flow_ctrl(bp);
  1196. bnx2_set_mac_link(bp);
  1197. }
  1198. return 0;
  1199. }
  1200. bnx2_test_and_enable_2g5(bp);
  1201. if (bp->advertising & ADVERTISED_1000baseT_Full)
  1202. new_adv |= ADVERTISE_1000XFULL;
  1203. new_adv |= bnx2_phy_get_pause_adv(bp);
  1204. bnx2_read_phy(bp, bp->mii_adv, &adv);
  1205. bnx2_read_phy(bp, bp->mii_bmcr, &bmcr);
  1206. bp->serdes_an_pending = 0;
  1207. if ((adv != new_adv) || ((bmcr & BMCR_ANENABLE) == 0)) {
  1208. /* Force a link down visible on the other side */
  1209. if (bp->link_up) {
  1210. bnx2_write_phy(bp, bp->mii_bmcr, BMCR_LOOPBACK);
  1211. spin_unlock_bh(&bp->phy_lock);
  1212. msleep(20);
  1213. spin_lock_bh(&bp->phy_lock);
  1214. }
  1215. bnx2_write_phy(bp, bp->mii_adv, new_adv);
  1216. bnx2_write_phy(bp, bp->mii_bmcr, bmcr | BMCR_ANRESTART |
  1217. BMCR_ANENABLE);
  1218. /* Speed up link-up time when the link partner
  1219. * does not autonegotiate which is very common
  1220. * in blade servers. Some blade servers use
  1221. * IPMI for kerboard input and it's important
  1222. * to minimize link disruptions. Autoneg. involves
  1223. * exchanging base pages plus 3 next pages and
  1224. * normally completes in about 120 msec.
  1225. */
  1226. bp->current_interval = SERDES_AN_TIMEOUT;
  1227. bp->serdes_an_pending = 1;
  1228. mod_timer(&bp->timer, jiffies + bp->current_interval);
  1229. } else {
  1230. bnx2_resolve_flow_ctrl(bp);
  1231. bnx2_set_mac_link(bp);
  1232. }
  1233. return 0;
  1234. }
  1235. #define ETHTOOL_ALL_FIBRE_SPEED \
  1236. (bp->phy_flags & PHY_2_5G_CAPABLE_FLAG) ? \
  1237. (ADVERTISED_2500baseX_Full | ADVERTISED_1000baseT_Full) :\
  1238. (ADVERTISED_1000baseT_Full)
  1239. #define ETHTOOL_ALL_COPPER_SPEED \
  1240. (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full | \
  1241. ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full | \
  1242. ADVERTISED_1000baseT_Full)
  1243. #define PHY_ALL_10_100_SPEED (ADVERTISE_10HALF | ADVERTISE_10FULL | \
  1244. ADVERTISE_100HALF | ADVERTISE_100FULL | ADVERTISE_CSMA)
  1245. #define PHY_ALL_1000_SPEED (ADVERTISE_1000HALF | ADVERTISE_1000FULL)
  1246. static void
  1247. bnx2_set_default_remote_link(struct bnx2 *bp)
  1248. {
  1249. u32 link;
  1250. if (bp->phy_port == PORT_TP)
  1251. link = REG_RD_IND(bp, bp->shmem_base + BNX2_RPHY_COPPER_LINK);
  1252. else
  1253. link = REG_RD_IND(bp, bp->shmem_base + BNX2_RPHY_SERDES_LINK);
  1254. if (link & BNX2_NETLINK_SET_LINK_ENABLE_AUTONEG) {
  1255. bp->req_line_speed = 0;
  1256. bp->autoneg |= AUTONEG_SPEED;
  1257. bp->advertising = ADVERTISED_Autoneg;
  1258. if (link & BNX2_NETLINK_SET_LINK_SPEED_10HALF)
  1259. bp->advertising |= ADVERTISED_10baseT_Half;
  1260. if (link & BNX2_NETLINK_SET_LINK_SPEED_10FULL)
  1261. bp->advertising |= ADVERTISED_10baseT_Full;
  1262. if (link & BNX2_NETLINK_SET_LINK_SPEED_100HALF)
  1263. bp->advertising |= ADVERTISED_100baseT_Half;
  1264. if (link & BNX2_NETLINK_SET_LINK_SPEED_100FULL)
  1265. bp->advertising |= ADVERTISED_100baseT_Full;
  1266. if (link & BNX2_NETLINK_SET_LINK_SPEED_1GFULL)
  1267. bp->advertising |= ADVERTISED_1000baseT_Full;
  1268. if (link & BNX2_NETLINK_SET_LINK_SPEED_2G5FULL)
  1269. bp->advertising |= ADVERTISED_2500baseX_Full;
  1270. } else {
  1271. bp->autoneg = 0;
  1272. bp->advertising = 0;
  1273. bp->req_duplex = DUPLEX_FULL;
  1274. if (link & BNX2_NETLINK_SET_LINK_SPEED_10) {
  1275. bp->req_line_speed = SPEED_10;
  1276. if (link & BNX2_NETLINK_SET_LINK_SPEED_10HALF)
  1277. bp->req_duplex = DUPLEX_HALF;
  1278. }
  1279. if (link & BNX2_NETLINK_SET_LINK_SPEED_100) {
  1280. bp->req_line_speed = SPEED_100;
  1281. if (link & BNX2_NETLINK_SET_LINK_SPEED_100HALF)
  1282. bp->req_duplex = DUPLEX_HALF;
  1283. }
  1284. if (link & BNX2_NETLINK_SET_LINK_SPEED_1GFULL)
  1285. bp->req_line_speed = SPEED_1000;
  1286. if (link & BNX2_NETLINK_SET_LINK_SPEED_2G5FULL)
  1287. bp->req_line_speed = SPEED_2500;
  1288. }
  1289. }
  1290. static void
  1291. bnx2_set_default_link(struct bnx2 *bp)
  1292. {
  1293. if (bp->phy_flags & REMOTE_PHY_CAP_FLAG)
  1294. return bnx2_set_default_remote_link(bp);
  1295. bp->autoneg = AUTONEG_SPEED | AUTONEG_FLOW_CTRL;
  1296. bp->req_line_speed = 0;
  1297. if (bp->phy_flags & PHY_SERDES_FLAG) {
  1298. u32 reg;
  1299. bp->advertising = ETHTOOL_ALL_FIBRE_SPEED | ADVERTISED_Autoneg;
  1300. reg = REG_RD_IND(bp, bp->shmem_base + BNX2_PORT_HW_CFG_CONFIG);
  1301. reg &= BNX2_PORT_HW_CFG_CFG_DFLT_LINK_MASK;
  1302. if (reg == BNX2_PORT_HW_CFG_CFG_DFLT_LINK_1G) {
  1303. bp->autoneg = 0;
  1304. bp->req_line_speed = bp->line_speed = SPEED_1000;
  1305. bp->req_duplex = DUPLEX_FULL;
  1306. }
  1307. } else
  1308. bp->advertising = ETHTOOL_ALL_COPPER_SPEED | ADVERTISED_Autoneg;
  1309. }
  1310. static void
  1311. bnx2_send_heart_beat(struct bnx2 *bp)
  1312. {
  1313. u32 msg;
  1314. u32 addr;
  1315. spin_lock(&bp->indirect_lock);
  1316. msg = (u32) (++bp->fw_drv_pulse_wr_seq & BNX2_DRV_PULSE_SEQ_MASK);
  1317. addr = bp->shmem_base + BNX2_DRV_PULSE_MB;
  1318. REG_WR(bp, BNX2_PCICFG_REG_WINDOW_ADDRESS, addr);
  1319. REG_WR(bp, BNX2_PCICFG_REG_WINDOW, msg);
  1320. spin_unlock(&bp->indirect_lock);
  1321. }
  1322. static void
  1323. bnx2_remote_phy_event(struct bnx2 *bp)
  1324. {
  1325. u32 msg;
  1326. u8 link_up = bp->link_up;
  1327. u8 old_port;
  1328. msg = REG_RD_IND(bp, bp->shmem_base + BNX2_LINK_STATUS);
  1329. if (msg & BNX2_LINK_STATUS_HEART_BEAT_EXPIRED)
  1330. bnx2_send_heart_beat(bp);
  1331. msg &= ~BNX2_LINK_STATUS_HEART_BEAT_EXPIRED;
  1332. if ((msg & BNX2_LINK_STATUS_LINK_UP) == BNX2_LINK_STATUS_LINK_DOWN)
  1333. bp->link_up = 0;
  1334. else {
  1335. u32 speed;
  1336. bp->link_up = 1;
  1337. speed = msg & BNX2_LINK_STATUS_SPEED_MASK;
  1338. bp->duplex = DUPLEX_FULL;
  1339. switch (speed) {
  1340. case BNX2_LINK_STATUS_10HALF:
  1341. bp->duplex = DUPLEX_HALF;
  1342. case BNX2_LINK_STATUS_10FULL:
  1343. bp->line_speed = SPEED_10;
  1344. break;
  1345. case BNX2_LINK_STATUS_100HALF:
  1346. bp->duplex = DUPLEX_HALF;
  1347. case BNX2_LINK_STATUS_100BASE_T4:
  1348. case BNX2_LINK_STATUS_100FULL:
  1349. bp->line_speed = SPEED_100;
  1350. break;
  1351. case BNX2_LINK_STATUS_1000HALF:
  1352. bp->duplex = DUPLEX_HALF;
  1353. case BNX2_LINK_STATUS_1000FULL:
  1354. bp->line_speed = SPEED_1000;
  1355. break;
  1356. case BNX2_LINK_STATUS_2500HALF:
  1357. bp->duplex = DUPLEX_HALF;
  1358. case BNX2_LINK_STATUS_2500FULL:
  1359. bp->line_speed = SPEED_2500;
  1360. break;
  1361. default:
  1362. bp->line_speed = 0;
  1363. break;
  1364. }
  1365. spin_lock(&bp->phy_lock);
  1366. bp->flow_ctrl = 0;
  1367. if ((bp->autoneg & (AUTONEG_SPEED | AUTONEG_FLOW_CTRL)) !=
  1368. (AUTONEG_SPEED | AUTONEG_FLOW_CTRL)) {
  1369. if (bp->duplex == DUPLEX_FULL)
  1370. bp->flow_ctrl = bp->req_flow_ctrl;
  1371. } else {
  1372. if (msg & BNX2_LINK_STATUS_TX_FC_ENABLED)
  1373. bp->flow_ctrl |= FLOW_CTRL_TX;
  1374. if (msg & BNX2_LINK_STATUS_RX_FC_ENABLED)
  1375. bp->flow_ctrl |= FLOW_CTRL_RX;
  1376. }
  1377. old_port = bp->phy_port;
  1378. if (msg & BNX2_LINK_STATUS_SERDES_LINK)
  1379. bp->phy_port = PORT_FIBRE;
  1380. else
  1381. bp->phy_port = PORT_TP;
  1382. if (old_port != bp->phy_port)
  1383. bnx2_set_default_link(bp);
  1384. spin_unlock(&bp->phy_lock);
  1385. }
  1386. if (bp->link_up != link_up)
  1387. bnx2_report_link(bp);
  1388. bnx2_set_mac_link(bp);
  1389. }
  1390. static int
  1391. bnx2_set_remote_link(struct bnx2 *bp)
  1392. {
  1393. u32 evt_code;
  1394. evt_code = REG_RD_IND(bp, bp->shmem_base + BNX2_FW_EVT_CODE_MB);
  1395. switch (evt_code) {
  1396. case BNX2_FW_EVT_CODE_LINK_EVENT:
  1397. bnx2_remote_phy_event(bp);
  1398. break;
  1399. case BNX2_FW_EVT_CODE_SW_TIMER_EXPIRATION_EVENT:
  1400. default:
  1401. bnx2_send_heart_beat(bp);
  1402. break;
  1403. }
  1404. return 0;
  1405. }
  1406. static int
  1407. bnx2_setup_copper_phy(struct bnx2 *bp)
  1408. {
  1409. u32 bmcr;
  1410. u32 new_bmcr;
  1411. bnx2_read_phy(bp, bp->mii_bmcr, &bmcr);
  1412. if (bp->autoneg & AUTONEG_SPEED) {
  1413. u32 adv_reg, adv1000_reg;
  1414. u32 new_adv_reg = 0;
  1415. u32 new_adv1000_reg = 0;
  1416. bnx2_read_phy(bp, bp->mii_adv, &adv_reg);
  1417. adv_reg &= (PHY_ALL_10_100_SPEED | ADVERTISE_PAUSE_CAP |
  1418. ADVERTISE_PAUSE_ASYM);
  1419. bnx2_read_phy(bp, MII_CTRL1000, &adv1000_reg);
  1420. adv1000_reg &= PHY_ALL_1000_SPEED;
  1421. if (bp->advertising & ADVERTISED_10baseT_Half)
  1422. new_adv_reg |= ADVERTISE_10HALF;
  1423. if (bp->advertising & ADVERTISED_10baseT_Full)
  1424. new_adv_reg |= ADVERTISE_10FULL;
  1425. if (bp->advertising & ADVERTISED_100baseT_Half)
  1426. new_adv_reg |= ADVERTISE_100HALF;
  1427. if (bp->advertising & ADVERTISED_100baseT_Full)
  1428. new_adv_reg |= ADVERTISE_100FULL;
  1429. if (bp->advertising & ADVERTISED_1000baseT_Full)
  1430. new_adv1000_reg |= ADVERTISE_1000FULL;
  1431. new_adv_reg |= ADVERTISE_CSMA;
  1432. new_adv_reg |= bnx2_phy_get_pause_adv(bp);
  1433. if ((adv1000_reg != new_adv1000_reg) ||
  1434. (adv_reg != new_adv_reg) ||
  1435. ((bmcr & BMCR_ANENABLE) == 0)) {
  1436. bnx2_write_phy(bp, bp->mii_adv, new_adv_reg);
  1437. bnx2_write_phy(bp, MII_CTRL1000, new_adv1000_reg);
  1438. bnx2_write_phy(bp, bp->mii_bmcr, BMCR_ANRESTART |
  1439. BMCR_ANENABLE);
  1440. }
  1441. else if (bp->link_up) {
  1442. /* Flow ctrl may have changed from auto to forced */
  1443. /* or vice-versa. */
  1444. bnx2_resolve_flow_ctrl(bp);
  1445. bnx2_set_mac_link(bp);
  1446. }
  1447. return 0;
  1448. }
  1449. new_bmcr = 0;
  1450. if (bp->req_line_speed == SPEED_100) {
  1451. new_bmcr |= BMCR_SPEED100;
  1452. }
  1453. if (bp->req_duplex == DUPLEX_FULL) {
  1454. new_bmcr |= BMCR_FULLDPLX;
  1455. }
  1456. if (new_bmcr != bmcr) {
  1457. u32 bmsr;
  1458. bnx2_read_phy(bp, bp->mii_bmsr, &bmsr);
  1459. bnx2_read_phy(bp, bp->mii_bmsr, &bmsr);
  1460. if (bmsr & BMSR_LSTATUS) {
  1461. /* Force link down */
  1462. bnx2_write_phy(bp, bp->mii_bmcr, BMCR_LOOPBACK);
  1463. spin_unlock_bh(&bp->phy_lock);
  1464. msleep(50);
  1465. spin_lock_bh(&bp->phy_lock);
  1466. bnx2_read_phy(bp, bp->mii_bmsr, &bmsr);
  1467. bnx2_read_phy(bp, bp->mii_bmsr, &bmsr);
  1468. }
  1469. bnx2_write_phy(bp, bp->mii_bmcr, new_bmcr);
  1470. /* Normally, the new speed is setup after the link has
  1471. * gone down and up again. In some cases, link will not go
  1472. * down so we need to set up the new speed here.
  1473. */
  1474. if (bmsr & BMSR_LSTATUS) {
  1475. bp->line_speed = bp->req_line_speed;
  1476. bp->duplex = bp->req_duplex;
  1477. bnx2_resolve_flow_ctrl(bp);
  1478. bnx2_set_mac_link(bp);
  1479. }
  1480. } else {
  1481. bnx2_resolve_flow_ctrl(bp);
  1482. bnx2_set_mac_link(bp);
  1483. }
  1484. return 0;
  1485. }
  1486. static int
  1487. bnx2_setup_phy(struct bnx2 *bp, u8 port)
  1488. {
  1489. if (bp->loopback == MAC_LOOPBACK)
  1490. return 0;
  1491. if (bp->phy_flags & PHY_SERDES_FLAG) {
  1492. return (bnx2_setup_serdes_phy(bp, port));
  1493. }
  1494. else {
  1495. return (bnx2_setup_copper_phy(bp));
  1496. }
  1497. }
  1498. static int
  1499. bnx2_init_5709s_phy(struct bnx2 *bp)
  1500. {
  1501. u32 val;
  1502. bp->mii_bmcr = MII_BMCR + 0x10;
  1503. bp->mii_bmsr = MII_BMSR + 0x10;
  1504. bp->mii_bmsr1 = MII_BNX2_GP_TOP_AN_STATUS1;
  1505. bp->mii_adv = MII_ADVERTISE + 0x10;
  1506. bp->mii_lpa = MII_LPA + 0x10;
  1507. bp->mii_up1 = MII_BNX2_OVER1G_UP1;
  1508. bnx2_write_phy(bp, MII_BNX2_BLK_ADDR, MII_BNX2_BLK_ADDR_AER);
  1509. bnx2_write_phy(bp, MII_BNX2_AER_AER, MII_BNX2_AER_AER_AN_MMD);
  1510. bnx2_write_phy(bp, MII_BNX2_BLK_ADDR, MII_BNX2_BLK_ADDR_COMBO_IEEEB0);
  1511. bnx2_reset_phy(bp);
  1512. bnx2_write_phy(bp, MII_BNX2_BLK_ADDR, MII_BNX2_BLK_ADDR_SERDES_DIG);
  1513. bnx2_read_phy(bp, MII_BNX2_SERDES_DIG_1000XCTL1, &val);
  1514. val &= ~MII_BNX2_SD_1000XCTL1_AUTODET;
  1515. val |= MII_BNX2_SD_1000XCTL1_FIBER;
  1516. bnx2_write_phy(bp, MII_BNX2_SERDES_DIG_1000XCTL1, val);
  1517. bnx2_write_phy(bp, MII_BNX2_BLK_ADDR, MII_BNX2_BLK_ADDR_OVER1G);
  1518. bnx2_read_phy(bp, MII_BNX2_OVER1G_UP1, &val);
  1519. if (bp->phy_flags & PHY_2_5G_CAPABLE_FLAG)
  1520. val |= BCM5708S_UP1_2G5;
  1521. else
  1522. val &= ~BCM5708S_UP1_2G5;
  1523. bnx2_write_phy(bp, MII_BNX2_OVER1G_UP1, val);
  1524. bnx2_write_phy(bp, MII_BNX2_BLK_ADDR, MII_BNX2_BLK_ADDR_BAM_NXTPG);
  1525. bnx2_read_phy(bp, MII_BNX2_BAM_NXTPG_CTL, &val);
  1526. val |= MII_BNX2_NXTPG_CTL_T2 | MII_BNX2_NXTPG_CTL_BAM;
  1527. bnx2_write_phy(bp, MII_BNX2_BAM_NXTPG_CTL, val);
  1528. bnx2_write_phy(bp, MII_BNX2_BLK_ADDR, MII_BNX2_BLK_ADDR_CL73_USERB0);
  1529. val = MII_BNX2_CL73_BAM_EN | MII_BNX2_CL73_BAM_STA_MGR_EN |
  1530. MII_BNX2_CL73_BAM_NP_AFT_BP_EN;
  1531. bnx2_write_phy(bp, MII_BNX2_CL73_BAM_CTL1, val);
  1532. bnx2_write_phy(bp, MII_BNX2_BLK_ADDR, MII_BNX2_BLK_ADDR_COMBO_IEEEB0);
  1533. return 0;
  1534. }
  1535. static int
  1536. bnx2_init_5708s_phy(struct bnx2 *bp)
  1537. {
  1538. u32 val;
  1539. bnx2_reset_phy(bp);
  1540. bp->mii_up1 = BCM5708S_UP1;
  1541. bnx2_write_phy(bp, BCM5708S_BLK_ADDR, BCM5708S_BLK_ADDR_DIG3);
  1542. bnx2_write_phy(bp, BCM5708S_DIG_3_0, BCM5708S_DIG_3_0_USE_IEEE);
  1543. bnx2_write_phy(bp, BCM5708S_BLK_ADDR, BCM5708S_BLK_ADDR_DIG);
  1544. bnx2_read_phy(bp, BCM5708S_1000X_CTL1, &val);
  1545. val |= BCM5708S_1000X_CTL1_FIBER_MODE | BCM5708S_1000X_CTL1_AUTODET_EN;
  1546. bnx2_write_phy(bp, BCM5708S_1000X_CTL1, val);
  1547. bnx2_read_phy(bp, BCM5708S_1000X_CTL2, &val);
  1548. val |= BCM5708S_1000X_CTL2_PLLEL_DET_EN;
  1549. bnx2_write_phy(bp, BCM5708S_1000X_CTL2, val);
  1550. if (bp->phy_flags & PHY_2_5G_CAPABLE_FLAG) {
  1551. bnx2_read_phy(bp, BCM5708S_UP1, &val);
  1552. val |= BCM5708S_UP1_2G5;
  1553. bnx2_write_phy(bp, BCM5708S_UP1, val);
  1554. }
  1555. if ((CHIP_ID(bp) == CHIP_ID_5708_A0) ||
  1556. (CHIP_ID(bp) == CHIP_ID_5708_B0) ||
  1557. (CHIP_ID(bp) == CHIP_ID_5708_B1)) {
  1558. /* increase tx signal amplitude */
  1559. bnx2_write_phy(bp, BCM5708S_BLK_ADDR,
  1560. BCM5708S_BLK_ADDR_TX_MISC);
  1561. bnx2_read_phy(bp, BCM5708S_TX_ACTL1, &val);
  1562. val &= ~BCM5708S_TX_ACTL1_DRIVER_VCM;
  1563. bnx2_write_phy(bp, BCM5708S_TX_ACTL1, val);
  1564. bnx2_write_phy(bp, BCM5708S_BLK_ADDR, BCM5708S_BLK_ADDR_DIG);
  1565. }
  1566. val = REG_RD_IND(bp, bp->shmem_base + BNX2_PORT_HW_CFG_CONFIG) &
  1567. BNX2_PORT_HW_CFG_CFG_TXCTL3_MASK;
  1568. if (val) {
  1569. u32 is_backplane;
  1570. is_backplane = REG_RD_IND(bp, bp->shmem_base +
  1571. BNX2_SHARED_HW_CFG_CONFIG);
  1572. if (is_backplane & BNX2_SHARED_HW_CFG_PHY_BACKPLANE) {
  1573. bnx2_write_phy(bp, BCM5708S_BLK_ADDR,
  1574. BCM5708S_BLK_ADDR_TX_MISC);
  1575. bnx2_write_phy(bp, BCM5708S_TX_ACTL3, val);
  1576. bnx2_write_phy(bp, BCM5708S_BLK_ADDR,
  1577. BCM5708S_BLK_ADDR_DIG);
  1578. }
  1579. }
  1580. return 0;
  1581. }
  1582. static int
  1583. bnx2_init_5706s_phy(struct bnx2 *bp)
  1584. {
  1585. bnx2_reset_phy(bp);
  1586. bp->phy_flags &= ~PHY_PARALLEL_DETECT_FLAG;
  1587. if (CHIP_NUM(bp) == CHIP_NUM_5706)
  1588. REG_WR(bp, BNX2_MISC_GP_HW_CTL0, 0x300);
  1589. if (bp->dev->mtu > 1500) {
  1590. u32 val;
  1591. /* Set extended packet length bit */
  1592. bnx2_write_phy(bp, 0x18, 0x7);
  1593. bnx2_read_phy(bp, 0x18, &val);
  1594. bnx2_write_phy(bp, 0x18, (val & 0xfff8) | 0x4000);
  1595. bnx2_write_phy(bp, 0x1c, 0x6c00);
  1596. bnx2_read_phy(bp, 0x1c, &val);
  1597. bnx2_write_phy(bp, 0x1c, (val & 0x3ff) | 0xec02);
  1598. }
  1599. else {
  1600. u32 val;
  1601. bnx2_write_phy(bp, 0x18, 0x7);
  1602. bnx2_read_phy(bp, 0x18, &val);
  1603. bnx2_write_phy(bp, 0x18, val & ~0x4007);
  1604. bnx2_write_phy(bp, 0x1c, 0x6c00);
  1605. bnx2_read_phy(bp, 0x1c, &val);
  1606. bnx2_write_phy(bp, 0x1c, (val & 0x3fd) | 0xec00);
  1607. }
  1608. return 0;
  1609. }
  1610. static int
  1611. bnx2_init_copper_phy(struct bnx2 *bp)
  1612. {
  1613. u32 val;
  1614. bnx2_reset_phy(bp);
  1615. if (bp->phy_flags & PHY_CRC_FIX_FLAG) {
  1616. bnx2_write_phy(bp, 0x18, 0x0c00);
  1617. bnx2_write_phy(bp, 0x17, 0x000a);
  1618. bnx2_write_phy(bp, 0x15, 0x310b);
  1619. bnx2_write_phy(bp, 0x17, 0x201f);
  1620. bnx2_write_phy(bp, 0x15, 0x9506);
  1621. bnx2_write_phy(bp, 0x17, 0x401f);
  1622. bnx2_write_phy(bp, 0x15, 0x14e2);
  1623. bnx2_write_phy(bp, 0x18, 0x0400);
  1624. }
  1625. if (bp->phy_flags & PHY_DIS_EARLY_DAC_FLAG) {
  1626. bnx2_write_phy(bp, MII_BNX2_DSP_ADDRESS,
  1627. MII_BNX2_DSP_EXPAND_REG | 0x8);
  1628. bnx2_read_phy(bp, MII_BNX2_DSP_RW_PORT, &val);
  1629. val &= ~(1 << 8);
  1630. bnx2_write_phy(bp, MII_BNX2_DSP_RW_PORT, val);
  1631. }
  1632. if (bp->dev->mtu > 1500) {
  1633. /* Set extended packet length bit */
  1634. bnx2_write_phy(bp, 0x18, 0x7);
  1635. bnx2_read_phy(bp, 0x18, &val);
  1636. bnx2_write_phy(bp, 0x18, val | 0x4000);
  1637. bnx2_read_phy(bp, 0x10, &val);
  1638. bnx2_write_phy(bp, 0x10, val | 0x1);
  1639. }
  1640. else {
  1641. bnx2_write_phy(bp, 0x18, 0x7);
  1642. bnx2_read_phy(bp, 0x18, &val);
  1643. bnx2_write_phy(bp, 0x18, val & ~0x4007);
  1644. bnx2_read_phy(bp, 0x10, &val);
  1645. bnx2_write_phy(bp, 0x10, val & ~0x1);
  1646. }
  1647. /* ethernet@wirespeed */
  1648. bnx2_write_phy(bp, 0x18, 0x7007);
  1649. bnx2_read_phy(bp, 0x18, &val);
  1650. bnx2_write_phy(bp, 0x18, val | (1 << 15) | (1 << 4));
  1651. return 0;
  1652. }
  1653. static int
  1654. bnx2_init_phy(struct bnx2 *bp)
  1655. {
  1656. u32 val;
  1657. int rc = 0;
  1658. bp->phy_flags &= ~PHY_INT_MODE_MASK_FLAG;
  1659. bp->phy_flags |= PHY_INT_MODE_LINK_READY_FLAG;
  1660. bp->mii_bmcr = MII_BMCR;
  1661. bp->mii_bmsr = MII_BMSR;
  1662. bp->mii_bmsr1 = MII_BMSR;
  1663. bp->mii_adv = MII_ADVERTISE;
  1664. bp->mii_lpa = MII_LPA;
  1665. REG_WR(bp, BNX2_EMAC_ATTENTION_ENA, BNX2_EMAC_ATTENTION_ENA_LINK);
  1666. if (bp->phy_flags & REMOTE_PHY_CAP_FLAG)
  1667. goto setup_phy;
  1668. bnx2_read_phy(bp, MII_PHYSID1, &val);
  1669. bp->phy_id = val << 16;
  1670. bnx2_read_phy(bp, MII_PHYSID2, &val);
  1671. bp->phy_id |= val & 0xffff;
  1672. if (bp->phy_flags & PHY_SERDES_FLAG) {
  1673. if (CHIP_NUM(bp) == CHIP_NUM_5706)
  1674. rc = bnx2_init_5706s_phy(bp);
  1675. else if (CHIP_NUM(bp) == CHIP_NUM_5708)
  1676. rc = bnx2_init_5708s_phy(bp);
  1677. else if (CHIP_NUM(bp) == CHIP_NUM_5709)
  1678. rc = bnx2_init_5709s_phy(bp);
  1679. }
  1680. else {
  1681. rc = bnx2_init_copper_phy(bp);
  1682. }
  1683. setup_phy:
  1684. if (!rc)
  1685. rc = bnx2_setup_phy(bp, bp->phy_port);
  1686. return rc;
  1687. }
  1688. static int
  1689. bnx2_set_mac_loopback(struct bnx2 *bp)
  1690. {
  1691. u32 mac_mode;
  1692. mac_mode = REG_RD(bp, BNX2_EMAC_MODE);
  1693. mac_mode &= ~BNX2_EMAC_MODE_PORT;
  1694. mac_mode |= BNX2_EMAC_MODE_MAC_LOOP | BNX2_EMAC_MODE_FORCE_LINK;
  1695. REG_WR(bp, BNX2_EMAC_MODE, mac_mode);
  1696. bp->link_up = 1;
  1697. return 0;
  1698. }
  1699. static int bnx2_test_link(struct bnx2 *);
  1700. static int
  1701. bnx2_set_phy_loopback(struct bnx2 *bp)
  1702. {
  1703. u32 mac_mode;
  1704. int rc, i;
  1705. spin_lock_bh(&bp->phy_lock);
  1706. rc = bnx2_write_phy(bp, bp->mii_bmcr, BMCR_LOOPBACK | BMCR_FULLDPLX |
  1707. BMCR_SPEED1000);
  1708. spin_unlock_bh(&bp->phy_lock);
  1709. if (rc)
  1710. return rc;
  1711. for (i = 0; i < 10; i++) {
  1712. if (bnx2_test_link(bp) == 0)
  1713. break;
  1714. msleep(100);
  1715. }
  1716. mac_mode = REG_RD(bp, BNX2_EMAC_MODE);
  1717. mac_mode &= ~(BNX2_EMAC_MODE_PORT | BNX2_EMAC_MODE_HALF_DUPLEX |
  1718. BNX2_EMAC_MODE_MAC_LOOP | BNX2_EMAC_MODE_FORCE_LINK |
  1719. BNX2_EMAC_MODE_25G_MODE);
  1720. mac_mode |= BNX2_EMAC_MODE_PORT_GMII;
  1721. REG_WR(bp, BNX2_EMAC_MODE, mac_mode);
  1722. bp->link_up = 1;
  1723. return 0;
  1724. }
  1725. static int
  1726. bnx2_fw_sync(struct bnx2 *bp, u32 msg_data, int silent)
  1727. {
  1728. int i;
  1729. u32 val;
  1730. bp->fw_wr_seq++;
  1731. msg_data |= bp->fw_wr_seq;
  1732. REG_WR_IND(bp, bp->shmem_base + BNX2_DRV_MB, msg_data);
  1733. /* wait for an acknowledgement. */
  1734. for (i = 0; i < (FW_ACK_TIME_OUT_MS / 10); i++) {
  1735. msleep(10);
  1736. val = REG_RD_IND(bp, bp->shmem_base + BNX2_FW_MB);
  1737. if ((val & BNX2_FW_MSG_ACK) == (msg_data & BNX2_DRV_MSG_SEQ))
  1738. break;
  1739. }
  1740. if ((msg_data & BNX2_DRV_MSG_DATA) == BNX2_DRV_MSG_DATA_WAIT0)
  1741. return 0;
  1742. /* If we timed out, inform the firmware that this is the case. */
  1743. if ((val & BNX2_FW_MSG_ACK) != (msg_data & BNX2_DRV_MSG_SEQ)) {
  1744. if (!silent)
  1745. printk(KERN_ERR PFX "fw sync timeout, reset code = "
  1746. "%x\n", msg_data);
  1747. msg_data &= ~BNX2_DRV_MSG_CODE;
  1748. msg_data |= BNX2_DRV_MSG_CODE_FW_TIMEOUT;
  1749. REG_WR_IND(bp, bp->shmem_base + BNX2_DRV_MB, msg_data);
  1750. return -EBUSY;
  1751. }
  1752. if ((val & BNX2_FW_MSG_STATUS_MASK) != BNX2_FW_MSG_STATUS_OK)
  1753. return -EIO;
  1754. return 0;
  1755. }
  1756. static int
  1757. bnx2_init_5709_context(struct bnx2 *bp)
  1758. {
  1759. int i, ret = 0;
  1760. u32 val;
  1761. val = BNX2_CTX_COMMAND_ENABLED | BNX2_CTX_COMMAND_MEM_INIT | (1 << 12);
  1762. val |= (BCM_PAGE_BITS - 8) << 16;
  1763. REG_WR(bp, BNX2_CTX_COMMAND, val);
  1764. for (i = 0; i < 10; i++) {
  1765. val = REG_RD(bp, BNX2_CTX_COMMAND);
  1766. if (!(val & BNX2_CTX_COMMAND_MEM_INIT))
  1767. break;
  1768. udelay(2);
  1769. }
  1770. if (val & BNX2_CTX_COMMAND_MEM_INIT)
  1771. return -EBUSY;
  1772. for (i = 0; i < bp->ctx_pages; i++) {
  1773. int j;
  1774. REG_WR(bp, BNX2_CTX_HOST_PAGE_TBL_DATA0,
  1775. (bp->ctx_blk_mapping[i] & 0xffffffff) |
  1776. BNX2_CTX_HOST_PAGE_TBL_DATA0_VALID);
  1777. REG_WR(bp, BNX2_CTX_HOST_PAGE_TBL_DATA1,
  1778. (u64) bp->ctx_blk_mapping[i] >> 32);
  1779. REG_WR(bp, BNX2_CTX_HOST_PAGE_TBL_CTRL, i |
  1780. BNX2_CTX_HOST_PAGE_TBL_CTRL_WRITE_REQ);
  1781. for (j = 0; j < 10; j++) {
  1782. val = REG_RD(bp, BNX2_CTX_HOST_PAGE_TBL_CTRL);
  1783. if (!(val & BNX2_CTX_HOST_PAGE_TBL_CTRL_WRITE_REQ))
  1784. break;
  1785. udelay(5);
  1786. }
  1787. if (val & BNX2_CTX_HOST_PAGE_TBL_CTRL_WRITE_REQ) {
  1788. ret = -EBUSY;
  1789. break;
  1790. }
  1791. }
  1792. return ret;
  1793. }
  1794. static void
  1795. bnx2_init_context(struct bnx2 *bp)
  1796. {
  1797. u32 vcid;
  1798. vcid = 96;
  1799. while (vcid) {
  1800. u32 vcid_addr, pcid_addr, offset;
  1801. int i;
  1802. vcid--;
  1803. if (CHIP_ID(bp) == CHIP_ID_5706_A0) {
  1804. u32 new_vcid;
  1805. vcid_addr = GET_PCID_ADDR(vcid);
  1806. if (vcid & 0x8) {
  1807. new_vcid = 0x60 + (vcid & 0xf0) + (vcid & 0x7);
  1808. }
  1809. else {
  1810. new_vcid = vcid;
  1811. }
  1812. pcid_addr = GET_PCID_ADDR(new_vcid);
  1813. }
  1814. else {
  1815. vcid_addr = GET_CID_ADDR(vcid);
  1816. pcid_addr = vcid_addr;
  1817. }
  1818. for (i = 0; i < (CTX_SIZE / PHY_CTX_SIZE); i++) {
  1819. vcid_addr += (i << PHY_CTX_SHIFT);
  1820. pcid_addr += (i << PHY_CTX_SHIFT);
  1821. REG_WR(bp, BNX2_CTX_VIRT_ADDR, vcid_addr);
  1822. REG_WR(bp, BNX2_CTX_PAGE_TBL, pcid_addr);
  1823. /* Zero out the context. */
  1824. for (offset = 0; offset < PHY_CTX_SIZE; offset += 4)
  1825. CTX_WR(bp, vcid_addr, offset, 0);
  1826. }
  1827. }
  1828. }
  1829. static int
  1830. bnx2_alloc_bad_rbuf(struct bnx2 *bp)
  1831. {
  1832. u16 *good_mbuf;
  1833. u32 good_mbuf_cnt;
  1834. u32 val;
  1835. good_mbuf = kmalloc(512 * sizeof(u16), GFP_KERNEL);
  1836. if (good_mbuf == NULL) {
  1837. printk(KERN_ERR PFX "Failed to allocate memory in "
  1838. "bnx2_alloc_bad_rbuf\n");
  1839. return -ENOMEM;
  1840. }
  1841. REG_WR(bp, BNX2_MISC_ENABLE_SET_BITS,
  1842. BNX2_MISC_ENABLE_SET_BITS_RX_MBUF_ENABLE);
  1843. good_mbuf_cnt = 0;
  1844. /* Allocate a bunch of mbufs and save the good ones in an array. */
  1845. val = REG_RD_IND(bp, BNX2_RBUF_STATUS1);
  1846. while (val & BNX2_RBUF_STATUS1_FREE_COUNT) {
  1847. REG_WR_IND(bp, BNX2_RBUF_COMMAND, BNX2_RBUF_COMMAND_ALLOC_REQ);
  1848. val = REG_RD_IND(bp, BNX2_RBUF_FW_BUF_ALLOC);
  1849. val &= BNX2_RBUF_FW_BUF_ALLOC_VALUE;
  1850. /* The addresses with Bit 9 set are bad memory blocks. */
  1851. if (!(val & (1 << 9))) {
  1852. good_mbuf[good_mbuf_cnt] = (u16) val;
  1853. good_mbuf_cnt++;
  1854. }
  1855. val = REG_RD_IND(bp, BNX2_RBUF_STATUS1);
  1856. }
  1857. /* Free the good ones back to the mbuf pool thus discarding
  1858. * all the bad ones. */
  1859. while (good_mbuf_cnt) {
  1860. good_mbuf_cnt--;
  1861. val = good_mbuf[good_mbuf_cnt];
  1862. val = (val << 9) | val | 1;
  1863. REG_WR_IND(bp, BNX2_RBUF_FW_BUF_FREE, val);
  1864. }
  1865. kfree(good_mbuf);
  1866. return 0;
  1867. }
  1868. static void
  1869. bnx2_set_mac_addr(struct bnx2 *bp)
  1870. {
  1871. u32 val;
  1872. u8 *mac_addr = bp->dev->dev_addr;
  1873. val = (mac_addr[0] << 8) | mac_addr[1];
  1874. REG_WR(bp, BNX2_EMAC_MAC_MATCH0, val);
  1875. val = (mac_addr[2] << 24) | (mac_addr[3] << 16) |
  1876. (mac_addr[4] << 8) | mac_addr[5];
  1877. REG_WR(bp, BNX2_EMAC_MAC_MATCH1, val);
  1878. }
  1879. static inline int
  1880. bnx2_alloc_rx_page(struct bnx2 *bp, u16 index)
  1881. {
  1882. dma_addr_t mapping;
  1883. struct sw_pg *rx_pg = &bp->rx_pg_ring[index];
  1884. struct rx_bd *rxbd =
  1885. &bp->rx_pg_desc_ring[RX_RING(index)][RX_IDX(index)];
  1886. struct page *page = alloc_page(GFP_ATOMIC);
  1887. if (!page)
  1888. return -ENOMEM;
  1889. mapping = pci_map_page(bp->pdev, page, 0, PAGE_SIZE,
  1890. PCI_DMA_FROMDEVICE);
  1891. rx_pg->page = page;
  1892. pci_unmap_addr_set(rx_pg, mapping, mapping);
  1893. rxbd->rx_bd_haddr_hi = (u64) mapping >> 32;
  1894. rxbd->rx_bd_haddr_lo = (u64) mapping & 0xffffffff;
  1895. return 0;
  1896. }
  1897. static void
  1898. bnx2_free_rx_page(struct bnx2 *bp, u16 index)
  1899. {
  1900. struct sw_pg *rx_pg = &bp->rx_pg_ring[index];
  1901. struct page *page = rx_pg->page;
  1902. if (!page)
  1903. return;
  1904. pci_unmap_page(bp->pdev, pci_unmap_addr(rx_pg, mapping), PAGE_SIZE,
  1905. PCI_DMA_FROMDEVICE);
  1906. __free_page(page);
  1907. rx_pg->page = NULL;
  1908. }
  1909. static inline int
  1910. bnx2_alloc_rx_skb(struct bnx2 *bp, struct bnx2_napi *bnapi, u16 index)
  1911. {
  1912. struct sk_buff *skb;
  1913. struct sw_bd *rx_buf = &bp->rx_buf_ring[index];
  1914. dma_addr_t mapping;
  1915. struct rx_bd *rxbd = &bp->rx_desc_ring[RX_RING(index)][RX_IDX(index)];
  1916. unsigned long align;
  1917. skb = netdev_alloc_skb(bp->dev, bp->rx_buf_size);
  1918. if (skb == NULL) {
  1919. return -ENOMEM;
  1920. }
  1921. if (unlikely((align = (unsigned long) skb->data & (BNX2_RX_ALIGN - 1))))
  1922. skb_reserve(skb, BNX2_RX_ALIGN - align);
  1923. mapping = pci_map_single(bp->pdev, skb->data, bp->rx_buf_use_size,
  1924. PCI_DMA_FROMDEVICE);
  1925. rx_buf->skb = skb;
  1926. pci_unmap_addr_set(rx_buf, mapping, mapping);
  1927. rxbd->rx_bd_haddr_hi = (u64) mapping >> 32;
  1928. rxbd->rx_bd_haddr_lo = (u64) mapping & 0xffffffff;
  1929. bnapi->rx_prod_bseq += bp->rx_buf_use_size;
  1930. return 0;
  1931. }
  1932. static int
  1933. bnx2_phy_event_is_set(struct bnx2 *bp, struct bnx2_napi *bnapi, u32 event)
  1934. {
  1935. struct status_block *sblk = bnapi->status_blk;
  1936. u32 new_link_state, old_link_state;
  1937. int is_set = 1;
  1938. new_link_state = sblk->status_attn_bits & event;
  1939. old_link_state = sblk->status_attn_bits_ack & event;
  1940. if (new_link_state != old_link_state) {
  1941. if (new_link_state)
  1942. REG_WR(bp, BNX2_PCICFG_STATUS_BIT_SET_CMD, event);
  1943. else
  1944. REG_WR(bp, BNX2_PCICFG_STATUS_BIT_CLEAR_CMD, event);
  1945. } else
  1946. is_set = 0;
  1947. return is_set;
  1948. }
  1949. static void
  1950. bnx2_phy_int(struct bnx2 *bp, struct bnx2_napi *bnapi)
  1951. {
  1952. if (bnx2_phy_event_is_set(bp, bnapi, STATUS_ATTN_BITS_LINK_STATE)) {
  1953. spin_lock(&bp->phy_lock);
  1954. bnx2_set_link(bp);
  1955. spin_unlock(&bp->phy_lock);
  1956. }
  1957. if (bnx2_phy_event_is_set(bp, bnapi, STATUS_ATTN_BITS_TIMER_ABORT))
  1958. bnx2_set_remote_link(bp);
  1959. }
  1960. static inline u16
  1961. bnx2_get_hw_tx_cons(struct bnx2_napi *bnapi)
  1962. {
  1963. u16 cons;
  1964. cons = bnapi->status_blk->status_tx_quick_consumer_index0;
  1965. if (unlikely((cons & MAX_TX_DESC_CNT) == MAX_TX_DESC_CNT))
  1966. cons++;
  1967. return cons;
  1968. }
  1969. static void
  1970. bnx2_tx_int(struct bnx2 *bp, struct bnx2_napi *bnapi)
  1971. {
  1972. u16 hw_cons, sw_cons, sw_ring_cons;
  1973. int tx_free_bd = 0;
  1974. hw_cons = bnx2_get_hw_tx_cons(bnapi);
  1975. sw_cons = bnapi->tx_cons;
  1976. while (sw_cons != hw_cons) {
  1977. struct sw_bd *tx_buf;
  1978. struct sk_buff *skb;
  1979. int i, last;
  1980. sw_ring_cons = TX_RING_IDX(sw_cons);
  1981. tx_buf = &bp->tx_buf_ring[sw_ring_cons];
  1982. skb = tx_buf->skb;
  1983. /* partial BD completions possible with TSO packets */
  1984. if (skb_is_gso(skb)) {
  1985. u16 last_idx, last_ring_idx;
  1986. last_idx = sw_cons +
  1987. skb_shinfo(skb)->nr_frags + 1;
  1988. last_ring_idx = sw_ring_cons +
  1989. skb_shinfo(skb)->nr_frags + 1;
  1990. if (unlikely(last_ring_idx >= MAX_TX_DESC_CNT)) {
  1991. last_idx++;
  1992. }
  1993. if (((s16) ((s16) last_idx - (s16) hw_cons)) > 0) {
  1994. break;
  1995. }
  1996. }
  1997. pci_unmap_single(bp->pdev, pci_unmap_addr(tx_buf, mapping),
  1998. skb_headlen(skb), PCI_DMA_TODEVICE);
  1999. tx_buf->skb = NULL;
  2000. last = skb_shinfo(skb)->nr_frags;
  2001. for (i = 0; i < last; i++) {
  2002. sw_cons = NEXT_TX_BD(sw_cons);
  2003. pci_unmap_page(bp->pdev,
  2004. pci_unmap_addr(
  2005. &bp->tx_buf_ring[TX_RING_IDX(sw_cons)],
  2006. mapping),
  2007. skb_shinfo(skb)->frags[i].size,
  2008. PCI_DMA_TODEVICE);
  2009. }
  2010. sw_cons = NEXT_TX_BD(sw_cons);
  2011. tx_free_bd += last + 1;
  2012. dev_kfree_skb(skb);
  2013. hw_cons = bnx2_get_hw_tx_cons(bnapi);
  2014. }
  2015. bnapi->hw_tx_cons = hw_cons;
  2016. bnapi->tx_cons = sw_cons;
  2017. /* Need to make the tx_cons update visible to bnx2_start_xmit()
  2018. * before checking for netif_queue_stopped(). Without the
  2019. * memory barrier, there is a small possibility that bnx2_start_xmit()
  2020. * will miss it and cause the queue to be stopped forever.
  2021. */
  2022. smp_mb();
  2023. if (unlikely(netif_queue_stopped(bp->dev)) &&
  2024. (bnx2_tx_avail(bp, bnapi) > bp->tx_wake_thresh)) {
  2025. netif_tx_lock(bp->dev);
  2026. if ((netif_queue_stopped(bp->dev)) &&
  2027. (bnx2_tx_avail(bp, bnapi) > bp->tx_wake_thresh))
  2028. netif_wake_queue(bp->dev);
  2029. netif_tx_unlock(bp->dev);
  2030. }
  2031. }
  2032. static void
  2033. bnx2_reuse_rx_skb_pages(struct bnx2 *bp, struct bnx2_napi *bnapi,
  2034. struct sk_buff *skb, int count)
  2035. {
  2036. struct sw_pg *cons_rx_pg, *prod_rx_pg;
  2037. struct rx_bd *cons_bd, *prod_bd;
  2038. dma_addr_t mapping;
  2039. int i;
  2040. u16 hw_prod = bnapi->rx_pg_prod, prod;
  2041. u16 cons = bnapi->rx_pg_cons;
  2042. for (i = 0; i < count; i++) {
  2043. prod = RX_PG_RING_IDX(hw_prod);
  2044. prod_rx_pg = &bp->rx_pg_ring[prod];
  2045. cons_rx_pg = &bp->rx_pg_ring[cons];
  2046. cons_bd = &bp->rx_pg_desc_ring[RX_RING(cons)][RX_IDX(cons)];
  2047. prod_bd = &bp->rx_pg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
  2048. if (i == 0 && skb) {
  2049. struct page *page;
  2050. struct skb_shared_info *shinfo;
  2051. shinfo = skb_shinfo(skb);
  2052. shinfo->nr_frags--;
  2053. page = shinfo->frags[shinfo->nr_frags].page;
  2054. shinfo->frags[shinfo->nr_frags].page = NULL;
  2055. mapping = pci_map_page(bp->pdev, page, 0, PAGE_SIZE,
  2056. PCI_DMA_FROMDEVICE);
  2057. cons_rx_pg->page = page;
  2058. pci_unmap_addr_set(cons_rx_pg, mapping, mapping);
  2059. dev_kfree_skb(skb);
  2060. }
  2061. if (prod != cons) {
  2062. prod_rx_pg->page = cons_rx_pg->page;
  2063. cons_rx_pg->page = NULL;
  2064. pci_unmap_addr_set(prod_rx_pg, mapping,
  2065. pci_unmap_addr(cons_rx_pg, mapping));
  2066. prod_bd->rx_bd_haddr_hi = cons_bd->rx_bd_haddr_hi;
  2067. prod_bd->rx_bd_haddr_lo = cons_bd->rx_bd_haddr_lo;
  2068. }
  2069. cons = RX_PG_RING_IDX(NEXT_RX_BD(cons));
  2070. hw_prod = NEXT_RX_BD(hw_prod);
  2071. }
  2072. bnapi->rx_pg_prod = hw_prod;
  2073. bnapi->rx_pg_cons = cons;
  2074. }
  2075. static inline void
  2076. bnx2_reuse_rx_skb(struct bnx2 *bp, struct bnx2_napi *bnapi, struct sk_buff *skb,
  2077. u16 cons, u16 prod)
  2078. {
  2079. struct sw_bd *cons_rx_buf, *prod_rx_buf;
  2080. struct rx_bd *cons_bd, *prod_bd;
  2081. cons_rx_buf = &bp->rx_buf_ring[cons];
  2082. prod_rx_buf = &bp->rx_buf_ring[prod];
  2083. pci_dma_sync_single_for_device(bp->pdev,
  2084. pci_unmap_addr(cons_rx_buf, mapping),
  2085. bp->rx_offset + RX_COPY_THRESH, PCI_DMA_FROMDEVICE);
  2086. bnapi->rx_prod_bseq += bp->rx_buf_use_size;
  2087. prod_rx_buf->skb = skb;
  2088. if (cons == prod)
  2089. return;
  2090. pci_unmap_addr_set(prod_rx_buf, mapping,
  2091. pci_unmap_addr(cons_rx_buf, mapping));
  2092. cons_bd = &bp->rx_desc_ring[RX_RING(cons)][RX_IDX(cons)];
  2093. prod_bd = &bp->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
  2094. prod_bd->rx_bd_haddr_hi = cons_bd->rx_bd_haddr_hi;
  2095. prod_bd->rx_bd_haddr_lo = cons_bd->rx_bd_haddr_lo;
  2096. }
  2097. static int
  2098. bnx2_rx_skb(struct bnx2 *bp, struct bnx2_napi *bnapi, struct sk_buff *skb,
  2099. unsigned int len, unsigned int hdr_len, dma_addr_t dma_addr,
  2100. u32 ring_idx)
  2101. {
  2102. int err;
  2103. u16 prod = ring_idx & 0xffff;
  2104. err = bnx2_alloc_rx_skb(bp, bnapi, prod);
  2105. if (unlikely(err)) {
  2106. bnx2_reuse_rx_skb(bp, bnapi, skb, (u16) (ring_idx >> 16), prod);
  2107. if (hdr_len) {
  2108. unsigned int raw_len = len + 4;
  2109. int pages = PAGE_ALIGN(raw_len - hdr_len) >> PAGE_SHIFT;
  2110. bnx2_reuse_rx_skb_pages(bp, bnapi, NULL, pages);
  2111. }
  2112. return err;
  2113. }
  2114. skb_reserve(skb, bp->rx_offset);
  2115. pci_unmap_single(bp->pdev, dma_addr, bp->rx_buf_use_size,
  2116. PCI_DMA_FROMDEVICE);
  2117. if (hdr_len == 0) {
  2118. skb_put(skb, len);
  2119. return 0;
  2120. } else {
  2121. unsigned int i, frag_len, frag_size, pages;
  2122. struct sw_pg *rx_pg;
  2123. u16 pg_cons = bnapi->rx_pg_cons;
  2124. u16 pg_prod = bnapi->rx_pg_prod;
  2125. frag_size = len + 4 - hdr_len;
  2126. pages = PAGE_ALIGN(frag_size) >> PAGE_SHIFT;
  2127. skb_put(skb, hdr_len);
  2128. for (i = 0; i < pages; i++) {
  2129. frag_len = min(frag_size, (unsigned int) PAGE_SIZE);
  2130. if (unlikely(frag_len <= 4)) {
  2131. unsigned int tail = 4 - frag_len;
  2132. bnapi->rx_pg_cons = pg_cons;
  2133. bnapi->rx_pg_prod = pg_prod;
  2134. bnx2_reuse_rx_skb_pages(bp, bnapi, NULL,
  2135. pages - i);
  2136. skb->len -= tail;
  2137. if (i == 0) {
  2138. skb->tail -= tail;
  2139. } else {
  2140. skb_frag_t *frag =
  2141. &skb_shinfo(skb)->frags[i - 1];
  2142. frag->size -= tail;
  2143. skb->data_len -= tail;
  2144. skb->truesize -= tail;
  2145. }
  2146. return 0;
  2147. }
  2148. rx_pg = &bp->rx_pg_ring[pg_cons];
  2149. pci_unmap_page(bp->pdev, pci_unmap_addr(rx_pg, mapping),
  2150. PAGE_SIZE, PCI_DMA_FROMDEVICE);
  2151. if (i == pages - 1)
  2152. frag_len -= 4;
  2153. skb_fill_page_desc(skb, i, rx_pg->page, 0, frag_len);
  2154. rx_pg->page = NULL;
  2155. err = bnx2_alloc_rx_page(bp, RX_PG_RING_IDX(pg_prod));
  2156. if (unlikely(err)) {
  2157. bnapi->rx_pg_cons = pg_cons;
  2158. bnapi->rx_pg_prod = pg_prod;
  2159. bnx2_reuse_rx_skb_pages(bp, bnapi, skb,
  2160. pages - i);
  2161. return err;
  2162. }
  2163. frag_size -= frag_len;
  2164. skb->data_len += frag_len;
  2165. skb->truesize += frag_len;
  2166. skb->len += frag_len;
  2167. pg_prod = NEXT_RX_BD(pg_prod);
  2168. pg_cons = RX_PG_RING_IDX(NEXT_RX_BD(pg_cons));
  2169. }
  2170. bnapi->rx_pg_prod = pg_prod;
  2171. bnapi->rx_pg_cons = pg_cons;
  2172. }
  2173. return 0;
  2174. }
  2175. static inline u16
  2176. bnx2_get_hw_rx_cons(struct bnx2_napi *bnapi)
  2177. {
  2178. u16 cons = bnapi->status_blk->status_rx_quick_consumer_index0;
  2179. if (unlikely((cons & MAX_RX_DESC_CNT) == MAX_RX_DESC_CNT))
  2180. cons++;
  2181. return cons;
  2182. }
  2183. static int
  2184. bnx2_rx_int(struct bnx2 *bp, struct bnx2_napi *bnapi, int budget)
  2185. {
  2186. u16 hw_cons, sw_cons, sw_ring_cons, sw_prod, sw_ring_prod;
  2187. struct l2_fhdr *rx_hdr;
  2188. int rx_pkt = 0, pg_ring_used = 0;
  2189. hw_cons = bnx2_get_hw_rx_cons(bnapi);
  2190. sw_cons = bnapi->rx_cons;
  2191. sw_prod = bnapi->rx_prod;
  2192. /* Memory barrier necessary as speculative reads of the rx
  2193. * buffer can be ahead of the index in the status block
  2194. */
  2195. rmb();
  2196. while (sw_cons != hw_cons) {
  2197. unsigned int len, hdr_len;
  2198. u32 status;
  2199. struct sw_bd *rx_buf;
  2200. struct sk_buff *skb;
  2201. dma_addr_t dma_addr;
  2202. sw_ring_cons = RX_RING_IDX(sw_cons);
  2203. sw_ring_prod = RX_RING_IDX(sw_prod);
  2204. rx_buf = &bp->rx_buf_ring[sw_ring_cons];
  2205. skb = rx_buf->skb;
  2206. rx_buf->skb = NULL;
  2207. dma_addr = pci_unmap_addr(rx_buf, mapping);
  2208. pci_dma_sync_single_for_cpu(bp->pdev, dma_addr,
  2209. bp->rx_offset + RX_COPY_THRESH, PCI_DMA_FROMDEVICE);
  2210. rx_hdr = (struct l2_fhdr *) skb->data;
  2211. len = rx_hdr->l2_fhdr_pkt_len;
  2212. if ((status = rx_hdr->l2_fhdr_status) &
  2213. (L2_FHDR_ERRORS_BAD_CRC |
  2214. L2_FHDR_ERRORS_PHY_DECODE |
  2215. L2_FHDR_ERRORS_ALIGNMENT |
  2216. L2_FHDR_ERRORS_TOO_SHORT |
  2217. L2_FHDR_ERRORS_GIANT_FRAME)) {
  2218. bnx2_reuse_rx_skb(bp, bnapi, skb, sw_ring_cons,
  2219. sw_ring_prod);
  2220. goto next_rx;
  2221. }
  2222. hdr_len = 0;
  2223. if (status & L2_FHDR_STATUS_SPLIT) {
  2224. hdr_len = rx_hdr->l2_fhdr_ip_xsum;
  2225. pg_ring_used = 1;
  2226. } else if (len > bp->rx_jumbo_thresh) {
  2227. hdr_len = bp->rx_jumbo_thresh;
  2228. pg_ring_used = 1;
  2229. }
  2230. len -= 4;
  2231. if (len <= bp->rx_copy_thresh) {
  2232. struct sk_buff *new_skb;
  2233. new_skb = netdev_alloc_skb(bp->dev, len + 2);
  2234. if (new_skb == NULL) {
  2235. bnx2_reuse_rx_skb(bp, bnapi, skb, sw_ring_cons,
  2236. sw_ring_prod);
  2237. goto next_rx;
  2238. }
  2239. /* aligned copy */
  2240. skb_copy_from_linear_data_offset(skb, bp->rx_offset - 2,
  2241. new_skb->data, len + 2);
  2242. skb_reserve(new_skb, 2);
  2243. skb_put(new_skb, len);
  2244. bnx2_reuse_rx_skb(bp, bnapi, skb,
  2245. sw_ring_cons, sw_ring_prod);
  2246. skb = new_skb;
  2247. } else if (unlikely(bnx2_rx_skb(bp, bnapi, skb, len, hdr_len,
  2248. dma_addr, (sw_ring_cons << 16) | sw_ring_prod)))
  2249. goto next_rx;
  2250. skb->protocol = eth_type_trans(skb, bp->dev);
  2251. if ((len > (bp->dev->mtu + ETH_HLEN)) &&
  2252. (ntohs(skb->protocol) != 0x8100)) {
  2253. dev_kfree_skb(skb);
  2254. goto next_rx;
  2255. }
  2256. skb->ip_summed = CHECKSUM_NONE;
  2257. if (bp->rx_csum &&
  2258. (status & (L2_FHDR_STATUS_TCP_SEGMENT |
  2259. L2_FHDR_STATUS_UDP_DATAGRAM))) {
  2260. if (likely((status & (L2_FHDR_ERRORS_TCP_XSUM |
  2261. L2_FHDR_ERRORS_UDP_XSUM)) == 0))
  2262. skb->ip_summed = CHECKSUM_UNNECESSARY;
  2263. }
  2264. #ifdef BCM_VLAN
  2265. if ((status & L2_FHDR_STATUS_L2_VLAN_TAG) && (bp->vlgrp != 0)) {
  2266. vlan_hwaccel_receive_skb(skb, bp->vlgrp,
  2267. rx_hdr->l2_fhdr_vlan_tag);
  2268. }
  2269. else
  2270. #endif
  2271. netif_receive_skb(skb);
  2272. bp->dev->last_rx = jiffies;
  2273. rx_pkt++;
  2274. next_rx:
  2275. sw_cons = NEXT_RX_BD(sw_cons);
  2276. sw_prod = NEXT_RX_BD(sw_prod);
  2277. if ((rx_pkt == budget))
  2278. break;
  2279. /* Refresh hw_cons to see if there is new work */
  2280. if (sw_cons == hw_cons) {
  2281. hw_cons = bnx2_get_hw_rx_cons(bnapi);
  2282. rmb();
  2283. }
  2284. }
  2285. bnapi->rx_cons = sw_cons;
  2286. bnapi->rx_prod = sw_prod;
  2287. if (pg_ring_used)
  2288. REG_WR16(bp, MB_RX_CID_ADDR + BNX2_L2CTX_HOST_PG_BDIDX,
  2289. bnapi->rx_pg_prod);
  2290. REG_WR16(bp, MB_RX_CID_ADDR + BNX2_L2CTX_HOST_BDIDX, sw_prod);
  2291. REG_WR(bp, MB_RX_CID_ADDR + BNX2_L2CTX_HOST_BSEQ, bnapi->rx_prod_bseq);
  2292. mmiowb();
  2293. return rx_pkt;
  2294. }
  2295. /* MSI ISR - The only difference between this and the INTx ISR
  2296. * is that the MSI interrupt is always serviced.
  2297. */
  2298. static irqreturn_t
  2299. bnx2_msi(int irq, void *dev_instance)
  2300. {
  2301. struct net_device *dev = dev_instance;
  2302. struct bnx2 *bp = netdev_priv(dev);
  2303. struct bnx2_napi *bnapi = &bp->bnx2_napi;
  2304. prefetch(bnapi->status_blk);
  2305. REG_WR(bp, BNX2_PCICFG_INT_ACK_CMD,
  2306. BNX2_PCICFG_INT_ACK_CMD_USE_INT_HC_PARAM |
  2307. BNX2_PCICFG_INT_ACK_CMD_MASK_INT);
  2308. /* Return here if interrupt is disabled. */
  2309. if (unlikely(atomic_read(&bp->intr_sem) != 0))
  2310. return IRQ_HANDLED;
  2311. netif_rx_schedule(dev, &bnapi->napi);
  2312. return IRQ_HANDLED;
  2313. }
  2314. static irqreturn_t
  2315. bnx2_msi_1shot(int irq, void *dev_instance)
  2316. {
  2317. struct net_device *dev = dev_instance;
  2318. struct bnx2 *bp = netdev_priv(dev);
  2319. struct bnx2_napi *bnapi = &bp->bnx2_napi;
  2320. prefetch(bnapi->status_blk);
  2321. /* Return here if interrupt is disabled. */
  2322. if (unlikely(atomic_read(&bp->intr_sem) != 0))
  2323. return IRQ_HANDLED;
  2324. netif_rx_schedule(dev, &bnapi->napi);
  2325. return IRQ_HANDLED;
  2326. }
  2327. static irqreturn_t
  2328. bnx2_interrupt(int irq, void *dev_instance)
  2329. {
  2330. struct net_device *dev = dev_instance;
  2331. struct bnx2 *bp = netdev_priv(dev);
  2332. struct bnx2_napi *bnapi = &bp->bnx2_napi;
  2333. struct status_block *sblk = bnapi->status_blk;
  2334. /* When using INTx, it is possible for the interrupt to arrive
  2335. * at the CPU before the status block posted prior to the
  2336. * interrupt. Reading a register will flush the status block.
  2337. * When using MSI, the MSI message will always complete after
  2338. * the status block write.
  2339. */
  2340. if ((sblk->status_idx == bnapi->last_status_idx) &&
  2341. (REG_RD(bp, BNX2_PCICFG_MISC_STATUS) &
  2342. BNX2_PCICFG_MISC_STATUS_INTA_VALUE))
  2343. return IRQ_NONE;
  2344. REG_WR(bp, BNX2_PCICFG_INT_ACK_CMD,
  2345. BNX2_PCICFG_INT_ACK_CMD_USE_INT_HC_PARAM |
  2346. BNX2_PCICFG_INT_ACK_CMD_MASK_INT);
  2347. /* Read back to deassert IRQ immediately to avoid too many
  2348. * spurious interrupts.
  2349. */
  2350. REG_RD(bp, BNX2_PCICFG_INT_ACK_CMD);
  2351. /* Return here if interrupt is shared and is disabled. */
  2352. if (unlikely(atomic_read(&bp->intr_sem) != 0))
  2353. return IRQ_HANDLED;
  2354. if (netif_rx_schedule_prep(dev, &bnapi->napi)) {
  2355. bnapi->last_status_idx = sblk->status_idx;
  2356. __netif_rx_schedule(dev, &bnapi->napi);
  2357. }
  2358. return IRQ_HANDLED;
  2359. }
  2360. #define STATUS_ATTN_EVENTS (STATUS_ATTN_BITS_LINK_STATE | \
  2361. STATUS_ATTN_BITS_TIMER_ABORT)
  2362. static inline int
  2363. bnx2_has_work(struct bnx2_napi *bnapi)
  2364. {
  2365. struct bnx2 *bp = bnapi->bp;
  2366. struct status_block *sblk = bp->status_blk;
  2367. if ((bnx2_get_hw_rx_cons(bnapi) != bnapi->rx_cons) ||
  2368. (bnx2_get_hw_tx_cons(bnapi) != bnapi->hw_tx_cons))
  2369. return 1;
  2370. if ((sblk->status_attn_bits & STATUS_ATTN_EVENTS) !=
  2371. (sblk->status_attn_bits_ack & STATUS_ATTN_EVENTS))
  2372. return 1;
  2373. return 0;
  2374. }
  2375. static int bnx2_poll_work(struct bnx2 *bp, struct bnx2_napi *bnapi,
  2376. int work_done, int budget)
  2377. {
  2378. struct status_block *sblk = bnapi->status_blk;
  2379. u32 status_attn_bits = sblk->status_attn_bits;
  2380. u32 status_attn_bits_ack = sblk->status_attn_bits_ack;
  2381. if ((status_attn_bits & STATUS_ATTN_EVENTS) !=
  2382. (status_attn_bits_ack & STATUS_ATTN_EVENTS)) {
  2383. bnx2_phy_int(bp, bnapi);
  2384. /* This is needed to take care of transient status
  2385. * during link changes.
  2386. */
  2387. REG_WR(bp, BNX2_HC_COMMAND,
  2388. bp->hc_cmd | BNX2_HC_COMMAND_COAL_NOW_WO_INT);
  2389. REG_RD(bp, BNX2_HC_COMMAND);
  2390. }
  2391. if (bnx2_get_hw_tx_cons(bnapi) != bnapi->hw_tx_cons)
  2392. bnx2_tx_int(bp, bnapi);
  2393. if (bnx2_get_hw_rx_cons(bnapi) != bnapi->rx_cons)
  2394. work_done += bnx2_rx_int(bp, bnapi, budget - work_done);
  2395. return work_done;
  2396. }
  2397. static int bnx2_poll(struct napi_struct *napi, int budget)
  2398. {
  2399. struct bnx2_napi *bnapi = container_of(napi, struct bnx2_napi, napi);
  2400. struct bnx2 *bp = bnapi->bp;
  2401. int work_done = 0;
  2402. struct status_block *sblk = bnapi->status_blk;
  2403. while (1) {
  2404. work_done = bnx2_poll_work(bp, bnapi, work_done, budget);
  2405. if (unlikely(work_done >= budget))
  2406. break;
  2407. /* bnapi->last_status_idx is used below to tell the hw how
  2408. * much work has been processed, so we must read it before
  2409. * checking for more work.
  2410. */
  2411. bnapi->last_status_idx = sblk->status_idx;
  2412. rmb();
  2413. if (likely(!bnx2_has_work(bnapi))) {
  2414. netif_rx_complete(bp->dev, napi);
  2415. if (likely(bp->flags & USING_MSI_FLAG)) {
  2416. REG_WR(bp, BNX2_PCICFG_INT_ACK_CMD,
  2417. BNX2_PCICFG_INT_ACK_CMD_INDEX_VALID |
  2418. bnapi->last_status_idx);
  2419. break;
  2420. }
  2421. REG_WR(bp, BNX2_PCICFG_INT_ACK_CMD,
  2422. BNX2_PCICFG_INT_ACK_CMD_INDEX_VALID |
  2423. BNX2_PCICFG_INT_ACK_CMD_MASK_INT |
  2424. bnapi->last_status_idx);
  2425. REG_WR(bp, BNX2_PCICFG_INT_ACK_CMD,
  2426. BNX2_PCICFG_INT_ACK_CMD_INDEX_VALID |
  2427. bnapi->last_status_idx);
  2428. break;
  2429. }
  2430. }
  2431. return work_done;
  2432. }
  2433. /* Called with rtnl_lock from vlan functions and also netif_tx_lock
  2434. * from set_multicast.
  2435. */
  2436. static void
  2437. bnx2_set_rx_mode(struct net_device *dev)
  2438. {
  2439. struct bnx2 *bp = netdev_priv(dev);
  2440. u32 rx_mode, sort_mode;
  2441. int i;
  2442. spin_lock_bh(&bp->phy_lock);
  2443. rx_mode = bp->rx_mode & ~(BNX2_EMAC_RX_MODE_PROMISCUOUS |
  2444. BNX2_EMAC_RX_MODE_KEEP_VLAN_TAG);
  2445. sort_mode = 1 | BNX2_RPM_SORT_USER0_BC_EN;
  2446. #ifdef BCM_VLAN
  2447. if (!bp->vlgrp && !(bp->flags & ASF_ENABLE_FLAG))
  2448. rx_mode |= BNX2_EMAC_RX_MODE_KEEP_VLAN_TAG;
  2449. #else
  2450. if (!(bp->flags & ASF_ENABLE_FLAG))
  2451. rx_mode |= BNX2_EMAC_RX_MODE_KEEP_VLAN_TAG;
  2452. #endif
  2453. if (dev->flags & IFF_PROMISC) {
  2454. /* Promiscuous mode. */
  2455. rx_mode |= BNX2_EMAC_RX_MODE_PROMISCUOUS;
  2456. sort_mode |= BNX2_RPM_SORT_USER0_PROM_EN |
  2457. BNX2_RPM_SORT_USER0_PROM_VLAN;
  2458. }
  2459. else if (dev->flags & IFF_ALLMULTI) {
  2460. for (i = 0; i < NUM_MC_HASH_REGISTERS; i++) {
  2461. REG_WR(bp, BNX2_EMAC_MULTICAST_HASH0 + (i * 4),
  2462. 0xffffffff);
  2463. }
  2464. sort_mode |= BNX2_RPM_SORT_USER0_MC_EN;
  2465. }
  2466. else {
  2467. /* Accept one or more multicast(s). */
  2468. struct dev_mc_list *mclist;
  2469. u32 mc_filter[NUM_MC_HASH_REGISTERS];
  2470. u32 regidx;
  2471. u32 bit;
  2472. u32 crc;
  2473. memset(mc_filter, 0, 4 * NUM_MC_HASH_REGISTERS);
  2474. for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count;
  2475. i++, mclist = mclist->next) {
  2476. crc = ether_crc_le(ETH_ALEN, mclist->dmi_addr);
  2477. bit = crc & 0xff;
  2478. regidx = (bit & 0xe0) >> 5;
  2479. bit &= 0x1f;
  2480. mc_filter[regidx] |= (1 << bit);
  2481. }
  2482. for (i = 0; i < NUM_MC_HASH_REGISTERS; i++) {
  2483. REG_WR(bp, BNX2_EMAC_MULTICAST_HASH0 + (i * 4),
  2484. mc_filter[i]);
  2485. }
  2486. sort_mode |= BNX2_RPM_SORT_USER0_MC_HSH_EN;
  2487. }
  2488. if (rx_mode != bp->rx_mode) {
  2489. bp->rx_mode = rx_mode;
  2490. REG_WR(bp, BNX2_EMAC_RX_MODE, rx_mode);
  2491. }
  2492. REG_WR(bp, BNX2_RPM_SORT_USER0, 0x0);
  2493. REG_WR(bp, BNX2_RPM_SORT_USER0, sort_mode);
  2494. REG_WR(bp, BNX2_RPM_SORT_USER0, sort_mode | BNX2_RPM_SORT_USER0_ENA);
  2495. spin_unlock_bh(&bp->phy_lock);
  2496. }
  2497. static void
  2498. load_rv2p_fw(struct bnx2 *bp, u32 *rv2p_code, u32 rv2p_code_len,
  2499. u32 rv2p_proc)
  2500. {
  2501. int i;
  2502. u32 val;
  2503. for (i = 0; i < rv2p_code_len; i += 8) {
  2504. REG_WR(bp, BNX2_RV2P_INSTR_HIGH, cpu_to_le32(*rv2p_code));
  2505. rv2p_code++;
  2506. REG_WR(bp, BNX2_RV2P_INSTR_LOW, cpu_to_le32(*rv2p_code));
  2507. rv2p_code++;
  2508. if (rv2p_proc == RV2P_PROC1) {
  2509. val = (i / 8) | BNX2_RV2P_PROC1_ADDR_CMD_RDWR;
  2510. REG_WR(bp, BNX2_RV2P_PROC1_ADDR_CMD, val);
  2511. }
  2512. else {
  2513. val = (i / 8) | BNX2_RV2P_PROC2_ADDR_CMD_RDWR;
  2514. REG_WR(bp, BNX2_RV2P_PROC2_ADDR_CMD, val);
  2515. }
  2516. }
  2517. /* Reset the processor, un-stall is done later. */
  2518. if (rv2p_proc == RV2P_PROC1) {
  2519. REG_WR(bp, BNX2_RV2P_COMMAND, BNX2_RV2P_COMMAND_PROC1_RESET);
  2520. }
  2521. else {
  2522. REG_WR(bp, BNX2_RV2P_COMMAND, BNX2_RV2P_COMMAND_PROC2_RESET);
  2523. }
  2524. }
  2525. static int
  2526. load_cpu_fw(struct bnx2 *bp, struct cpu_reg *cpu_reg, struct fw_info *fw)
  2527. {
  2528. u32 offset;
  2529. u32 val;
  2530. int rc;
  2531. /* Halt the CPU. */
  2532. val = REG_RD_IND(bp, cpu_reg->mode);
  2533. val |= cpu_reg->mode_value_halt;
  2534. REG_WR_IND(bp, cpu_reg->mode, val);
  2535. REG_WR_IND(bp, cpu_reg->state, cpu_reg->state_value_clear);
  2536. /* Load the Text area. */
  2537. offset = cpu_reg->spad_base + (fw->text_addr - cpu_reg->mips_view_base);
  2538. if (fw->gz_text) {
  2539. int j;
  2540. rc = zlib_inflate_blob(fw->text, FW_BUF_SIZE, fw->gz_text,
  2541. fw->gz_text_len);
  2542. if (rc < 0)
  2543. return rc;
  2544. for (j = 0; j < (fw->text_len / 4); j++, offset += 4) {
  2545. REG_WR_IND(bp, offset, cpu_to_le32(fw->text[j]));
  2546. }
  2547. }
  2548. /* Load the Data area. */
  2549. offset = cpu_reg->spad_base + (fw->data_addr - cpu_reg->mips_view_base);
  2550. if (fw->data) {
  2551. int j;
  2552. for (j = 0; j < (fw->data_len / 4); j++, offset += 4) {
  2553. REG_WR_IND(bp, offset, fw->data[j]);
  2554. }
  2555. }
  2556. /* Load the SBSS area. */
  2557. offset = cpu_reg->spad_base + (fw->sbss_addr - cpu_reg->mips_view_base);
  2558. if (fw->sbss_len) {
  2559. int j;
  2560. for (j = 0; j < (fw->sbss_len / 4); j++, offset += 4) {
  2561. REG_WR_IND(bp, offset, 0);
  2562. }
  2563. }
  2564. /* Load the BSS area. */
  2565. offset = cpu_reg->spad_base + (fw->bss_addr - cpu_reg->mips_view_base);
  2566. if (fw->bss_len) {
  2567. int j;
  2568. for (j = 0; j < (fw->bss_len/4); j++, offset += 4) {
  2569. REG_WR_IND(bp, offset, 0);
  2570. }
  2571. }
  2572. /* Load the Read-Only area. */
  2573. offset = cpu_reg->spad_base +
  2574. (fw->rodata_addr - cpu_reg->mips_view_base);
  2575. if (fw->rodata) {
  2576. int j;
  2577. for (j = 0; j < (fw->rodata_len / 4); j++, offset += 4) {
  2578. REG_WR_IND(bp, offset, fw->rodata[j]);
  2579. }
  2580. }
  2581. /* Clear the pre-fetch instruction. */
  2582. REG_WR_IND(bp, cpu_reg->inst, 0);
  2583. REG_WR_IND(bp, cpu_reg->pc, fw->start_addr);
  2584. /* Start the CPU. */
  2585. val = REG_RD_IND(bp, cpu_reg->mode);
  2586. val &= ~cpu_reg->mode_value_halt;
  2587. REG_WR_IND(bp, cpu_reg->state, cpu_reg->state_value_clear);
  2588. REG_WR_IND(bp, cpu_reg->mode, val);
  2589. return 0;
  2590. }
  2591. static int
  2592. bnx2_init_cpus(struct bnx2 *bp)
  2593. {
  2594. struct cpu_reg cpu_reg;
  2595. struct fw_info *fw;
  2596. int rc, rv2p_len;
  2597. void *text, *rv2p;
  2598. /* Initialize the RV2P processor. */
  2599. text = vmalloc(FW_BUF_SIZE);
  2600. if (!text)
  2601. return -ENOMEM;
  2602. if (CHIP_NUM(bp) == CHIP_NUM_5709) {
  2603. rv2p = bnx2_xi_rv2p_proc1;
  2604. rv2p_len = sizeof(bnx2_xi_rv2p_proc1);
  2605. } else {
  2606. rv2p = bnx2_rv2p_proc1;
  2607. rv2p_len = sizeof(bnx2_rv2p_proc1);
  2608. }
  2609. rc = zlib_inflate_blob(text, FW_BUF_SIZE, rv2p, rv2p_len);
  2610. if (rc < 0)
  2611. goto init_cpu_err;
  2612. load_rv2p_fw(bp, text, rc /* == len */, RV2P_PROC1);
  2613. if (CHIP_NUM(bp) == CHIP_NUM_5709) {
  2614. rv2p = bnx2_xi_rv2p_proc2;
  2615. rv2p_len = sizeof(bnx2_xi_rv2p_proc2);
  2616. } else {
  2617. rv2p = bnx2_rv2p_proc2;
  2618. rv2p_len = sizeof(bnx2_rv2p_proc2);
  2619. }
  2620. rc = zlib_inflate_blob(text, FW_BUF_SIZE, rv2p, rv2p_len);
  2621. if (rc < 0)
  2622. goto init_cpu_err;
  2623. load_rv2p_fw(bp, text, rc /* == len */, RV2P_PROC2);
  2624. /* Initialize the RX Processor. */
  2625. cpu_reg.mode = BNX2_RXP_CPU_MODE;
  2626. cpu_reg.mode_value_halt = BNX2_RXP_CPU_MODE_SOFT_HALT;
  2627. cpu_reg.mode_value_sstep = BNX2_RXP_CPU_MODE_STEP_ENA;
  2628. cpu_reg.state = BNX2_RXP_CPU_STATE;
  2629. cpu_reg.state_value_clear = 0xffffff;
  2630. cpu_reg.gpr0 = BNX2_RXP_CPU_REG_FILE;
  2631. cpu_reg.evmask = BNX2_RXP_CPU_EVENT_MASK;
  2632. cpu_reg.pc = BNX2_RXP_CPU_PROGRAM_COUNTER;
  2633. cpu_reg.inst = BNX2_RXP_CPU_INSTRUCTION;
  2634. cpu_reg.bp = BNX2_RXP_CPU_HW_BREAKPOINT;
  2635. cpu_reg.spad_base = BNX2_RXP_SCRATCH;
  2636. cpu_reg.mips_view_base = 0x8000000;
  2637. if (CHIP_NUM(bp) == CHIP_NUM_5709)
  2638. fw = &bnx2_rxp_fw_09;
  2639. else
  2640. fw = &bnx2_rxp_fw_06;
  2641. fw->text = text;
  2642. rc = load_cpu_fw(bp, &cpu_reg, fw);
  2643. if (rc)
  2644. goto init_cpu_err;
  2645. /* Initialize the TX Processor. */
  2646. cpu_reg.mode = BNX2_TXP_CPU_MODE;
  2647. cpu_reg.mode_value_halt = BNX2_TXP_CPU_MODE_SOFT_HALT;
  2648. cpu_reg.mode_value_sstep = BNX2_TXP_CPU_MODE_STEP_ENA;
  2649. cpu_reg.state = BNX2_TXP_CPU_STATE;
  2650. cpu_reg.state_value_clear = 0xffffff;
  2651. cpu_reg.gpr0 = BNX2_TXP_CPU_REG_FILE;
  2652. cpu_reg.evmask = BNX2_TXP_CPU_EVENT_MASK;
  2653. cpu_reg.pc = BNX2_TXP_CPU_PROGRAM_COUNTER;
  2654. cpu_reg.inst = BNX2_TXP_CPU_INSTRUCTION;
  2655. cpu_reg.bp = BNX2_TXP_CPU_HW_BREAKPOINT;
  2656. cpu_reg.spad_base = BNX2_TXP_SCRATCH;
  2657. cpu_reg.mips_view_base = 0x8000000;
  2658. if (CHIP_NUM(bp) == CHIP_NUM_5709)
  2659. fw = &bnx2_txp_fw_09;
  2660. else
  2661. fw = &bnx2_txp_fw_06;
  2662. fw->text = text;
  2663. rc = load_cpu_fw(bp, &cpu_reg, fw);
  2664. if (rc)
  2665. goto init_cpu_err;
  2666. /* Initialize the TX Patch-up Processor. */
  2667. cpu_reg.mode = BNX2_TPAT_CPU_MODE;
  2668. cpu_reg.mode_value_halt = BNX2_TPAT_CPU_MODE_SOFT_HALT;
  2669. cpu_reg.mode_value_sstep = BNX2_TPAT_CPU_MODE_STEP_ENA;
  2670. cpu_reg.state = BNX2_TPAT_CPU_STATE;
  2671. cpu_reg.state_value_clear = 0xffffff;
  2672. cpu_reg.gpr0 = BNX2_TPAT_CPU_REG_FILE;
  2673. cpu_reg.evmask = BNX2_TPAT_CPU_EVENT_MASK;
  2674. cpu_reg.pc = BNX2_TPAT_CPU_PROGRAM_COUNTER;
  2675. cpu_reg.inst = BNX2_TPAT_CPU_INSTRUCTION;
  2676. cpu_reg.bp = BNX2_TPAT_CPU_HW_BREAKPOINT;
  2677. cpu_reg.spad_base = BNX2_TPAT_SCRATCH;
  2678. cpu_reg.mips_view_base = 0x8000000;
  2679. if (CHIP_NUM(bp) == CHIP_NUM_5709)
  2680. fw = &bnx2_tpat_fw_09;
  2681. else
  2682. fw = &bnx2_tpat_fw_06;
  2683. fw->text = text;
  2684. rc = load_cpu_fw(bp, &cpu_reg, fw);
  2685. if (rc)
  2686. goto init_cpu_err;
  2687. /* Initialize the Completion Processor. */
  2688. cpu_reg.mode = BNX2_COM_CPU_MODE;
  2689. cpu_reg.mode_value_halt = BNX2_COM_CPU_MODE_SOFT_HALT;
  2690. cpu_reg.mode_value_sstep = BNX2_COM_CPU_MODE_STEP_ENA;
  2691. cpu_reg.state = BNX2_COM_CPU_STATE;
  2692. cpu_reg.state_value_clear = 0xffffff;
  2693. cpu_reg.gpr0 = BNX2_COM_CPU_REG_FILE;
  2694. cpu_reg.evmask = BNX2_COM_CPU_EVENT_MASK;
  2695. cpu_reg.pc = BNX2_COM_CPU_PROGRAM_COUNTER;
  2696. cpu_reg.inst = BNX2_COM_CPU_INSTRUCTION;
  2697. cpu_reg.bp = BNX2_COM_CPU_HW_BREAKPOINT;
  2698. cpu_reg.spad_base = BNX2_COM_SCRATCH;
  2699. cpu_reg.mips_view_base = 0x8000000;
  2700. if (CHIP_NUM(bp) == CHIP_NUM_5709)
  2701. fw = &bnx2_com_fw_09;
  2702. else
  2703. fw = &bnx2_com_fw_06;
  2704. fw->text = text;
  2705. rc = load_cpu_fw(bp, &cpu_reg, fw);
  2706. if (rc)
  2707. goto init_cpu_err;
  2708. /* Initialize the Command Processor. */
  2709. cpu_reg.mode = BNX2_CP_CPU_MODE;
  2710. cpu_reg.mode_value_halt = BNX2_CP_CPU_MODE_SOFT_HALT;
  2711. cpu_reg.mode_value_sstep = BNX2_CP_CPU_MODE_STEP_ENA;
  2712. cpu_reg.state = BNX2_CP_CPU_STATE;
  2713. cpu_reg.state_value_clear = 0xffffff;
  2714. cpu_reg.gpr0 = BNX2_CP_CPU_REG_FILE;
  2715. cpu_reg.evmask = BNX2_CP_CPU_EVENT_MASK;
  2716. cpu_reg.pc = BNX2_CP_CPU_PROGRAM_COUNTER;
  2717. cpu_reg.inst = BNX2_CP_CPU_INSTRUCTION;
  2718. cpu_reg.bp = BNX2_CP_CPU_HW_BREAKPOINT;
  2719. cpu_reg.spad_base = BNX2_CP_SCRATCH;
  2720. cpu_reg.mips_view_base = 0x8000000;
  2721. if (CHIP_NUM(bp) == CHIP_NUM_5709)
  2722. fw = &bnx2_cp_fw_09;
  2723. else
  2724. fw = &bnx2_cp_fw_06;
  2725. fw->text = text;
  2726. rc = load_cpu_fw(bp, &cpu_reg, fw);
  2727. init_cpu_err:
  2728. vfree(text);
  2729. return rc;
  2730. }
  2731. static int
  2732. bnx2_set_power_state(struct bnx2 *bp, pci_power_t state)
  2733. {
  2734. u16 pmcsr;
  2735. pci_read_config_word(bp->pdev, bp->pm_cap + PCI_PM_CTRL, &pmcsr);
  2736. switch (state) {
  2737. case PCI_D0: {
  2738. u32 val;
  2739. pci_write_config_word(bp->pdev, bp->pm_cap + PCI_PM_CTRL,
  2740. (pmcsr & ~PCI_PM_CTRL_STATE_MASK) |
  2741. PCI_PM_CTRL_PME_STATUS);
  2742. if (pmcsr & PCI_PM_CTRL_STATE_MASK)
  2743. /* delay required during transition out of D3hot */
  2744. msleep(20);
  2745. val = REG_RD(bp, BNX2_EMAC_MODE);
  2746. val |= BNX2_EMAC_MODE_MPKT_RCVD | BNX2_EMAC_MODE_ACPI_RCVD;
  2747. val &= ~BNX2_EMAC_MODE_MPKT;
  2748. REG_WR(bp, BNX2_EMAC_MODE, val);
  2749. val = REG_RD(bp, BNX2_RPM_CONFIG);
  2750. val &= ~BNX2_RPM_CONFIG_ACPI_ENA;
  2751. REG_WR(bp, BNX2_RPM_CONFIG, val);
  2752. break;
  2753. }
  2754. case PCI_D3hot: {
  2755. int i;
  2756. u32 val, wol_msg;
  2757. if (bp->wol) {
  2758. u32 advertising;
  2759. u8 autoneg;
  2760. autoneg = bp->autoneg;
  2761. advertising = bp->advertising;
  2762. if (bp->phy_port == PORT_TP) {
  2763. bp->autoneg = AUTONEG_SPEED;
  2764. bp->advertising = ADVERTISED_10baseT_Half |
  2765. ADVERTISED_10baseT_Full |
  2766. ADVERTISED_100baseT_Half |
  2767. ADVERTISED_100baseT_Full |
  2768. ADVERTISED_Autoneg;
  2769. }
  2770. spin_lock_bh(&bp->phy_lock);
  2771. bnx2_setup_phy(bp, bp->phy_port);
  2772. spin_unlock_bh(&bp->phy_lock);
  2773. bp->autoneg = autoneg;
  2774. bp->advertising = advertising;
  2775. bnx2_set_mac_addr(bp);
  2776. val = REG_RD(bp, BNX2_EMAC_MODE);
  2777. /* Enable port mode. */
  2778. val &= ~BNX2_EMAC_MODE_PORT;
  2779. val |= BNX2_EMAC_MODE_MPKT_RCVD |
  2780. BNX2_EMAC_MODE_ACPI_RCVD |
  2781. BNX2_EMAC_MODE_MPKT;
  2782. if (bp->phy_port == PORT_TP)
  2783. val |= BNX2_EMAC_MODE_PORT_MII;
  2784. else {
  2785. val |= BNX2_EMAC_MODE_PORT_GMII;
  2786. if (bp->line_speed == SPEED_2500)
  2787. val |= BNX2_EMAC_MODE_25G_MODE;
  2788. }
  2789. REG_WR(bp, BNX2_EMAC_MODE, val);
  2790. /* receive all multicast */
  2791. for (i = 0; i < NUM_MC_HASH_REGISTERS; i++) {
  2792. REG_WR(bp, BNX2_EMAC_MULTICAST_HASH0 + (i * 4),
  2793. 0xffffffff);
  2794. }
  2795. REG_WR(bp, BNX2_EMAC_RX_MODE,
  2796. BNX2_EMAC_RX_MODE_SORT_MODE);
  2797. val = 1 | BNX2_RPM_SORT_USER0_BC_EN |
  2798. BNX2_RPM_SORT_USER0_MC_EN;
  2799. REG_WR(bp, BNX2_RPM_SORT_USER0, 0x0);
  2800. REG_WR(bp, BNX2_RPM_SORT_USER0, val);
  2801. REG_WR(bp, BNX2_RPM_SORT_USER0, val |
  2802. BNX2_RPM_SORT_USER0_ENA);
  2803. /* Need to enable EMAC and RPM for WOL. */
  2804. REG_WR(bp, BNX2_MISC_ENABLE_SET_BITS,
  2805. BNX2_MISC_ENABLE_SET_BITS_RX_PARSER_MAC_ENABLE |
  2806. BNX2_MISC_ENABLE_SET_BITS_TX_HEADER_Q_ENABLE |
  2807. BNX2_MISC_ENABLE_SET_BITS_EMAC_ENABLE);
  2808. val = REG_RD(bp, BNX2_RPM_CONFIG);
  2809. val &= ~BNX2_RPM_CONFIG_ACPI_ENA;
  2810. REG_WR(bp, BNX2_RPM_CONFIG, val);
  2811. wol_msg = BNX2_DRV_MSG_CODE_SUSPEND_WOL;
  2812. }
  2813. else {
  2814. wol_msg = BNX2_DRV_MSG_CODE_SUSPEND_NO_WOL;
  2815. }
  2816. if (!(bp->flags & NO_WOL_FLAG))
  2817. bnx2_fw_sync(bp, BNX2_DRV_MSG_DATA_WAIT3 | wol_msg, 0);
  2818. pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
  2819. if ((CHIP_ID(bp) == CHIP_ID_5706_A0) ||
  2820. (CHIP_ID(bp) == CHIP_ID_5706_A1)) {
  2821. if (bp->wol)
  2822. pmcsr |= 3;
  2823. }
  2824. else {
  2825. pmcsr |= 3;
  2826. }
  2827. if (bp->wol) {
  2828. pmcsr |= PCI_PM_CTRL_PME_ENABLE;
  2829. }
  2830. pci_write_config_word(bp->pdev, bp->pm_cap + PCI_PM_CTRL,
  2831. pmcsr);
  2832. /* No more memory access after this point until
  2833. * device is brought back to D0.
  2834. */
  2835. udelay(50);
  2836. break;
  2837. }
  2838. default:
  2839. return -EINVAL;
  2840. }
  2841. return 0;
  2842. }
  2843. static int
  2844. bnx2_acquire_nvram_lock(struct bnx2 *bp)
  2845. {
  2846. u32 val;
  2847. int j;
  2848. /* Request access to the flash interface. */
  2849. REG_WR(bp, BNX2_NVM_SW_ARB, BNX2_NVM_SW_ARB_ARB_REQ_SET2);
  2850. for (j = 0; j < NVRAM_TIMEOUT_COUNT; j++) {
  2851. val = REG_RD(bp, BNX2_NVM_SW_ARB);
  2852. if (val & BNX2_NVM_SW_ARB_ARB_ARB2)
  2853. break;
  2854. udelay(5);
  2855. }
  2856. if (j >= NVRAM_TIMEOUT_COUNT)
  2857. return -EBUSY;
  2858. return 0;
  2859. }
  2860. static int
  2861. bnx2_release_nvram_lock(struct bnx2 *bp)
  2862. {
  2863. int j;
  2864. u32 val;
  2865. /* Relinquish nvram interface. */
  2866. REG_WR(bp, BNX2_NVM_SW_ARB, BNX2_NVM_SW_ARB_ARB_REQ_CLR2);
  2867. for (j = 0; j < NVRAM_TIMEOUT_COUNT; j++) {
  2868. val = REG_RD(bp, BNX2_NVM_SW_ARB);
  2869. if (!(val & BNX2_NVM_SW_ARB_ARB_ARB2))
  2870. break;
  2871. udelay(5);
  2872. }
  2873. if (j >= NVRAM_TIMEOUT_COUNT)
  2874. return -EBUSY;
  2875. return 0;
  2876. }
  2877. static int
  2878. bnx2_enable_nvram_write(struct bnx2 *bp)
  2879. {
  2880. u32 val;
  2881. val = REG_RD(bp, BNX2_MISC_CFG);
  2882. REG_WR(bp, BNX2_MISC_CFG, val | BNX2_MISC_CFG_NVM_WR_EN_PCI);
  2883. if (bp->flash_info->flags & BNX2_NV_WREN) {
  2884. int j;
  2885. REG_WR(bp, BNX2_NVM_COMMAND, BNX2_NVM_COMMAND_DONE);
  2886. REG_WR(bp, BNX2_NVM_COMMAND,
  2887. BNX2_NVM_COMMAND_WREN | BNX2_NVM_COMMAND_DOIT);
  2888. for (j = 0; j < NVRAM_TIMEOUT_COUNT; j++) {
  2889. udelay(5);
  2890. val = REG_RD(bp, BNX2_NVM_COMMAND);
  2891. if (val & BNX2_NVM_COMMAND_DONE)
  2892. break;
  2893. }
  2894. if (j >= NVRAM_TIMEOUT_COUNT)
  2895. return -EBUSY;
  2896. }
  2897. return 0;
  2898. }
  2899. static void
  2900. bnx2_disable_nvram_write(struct bnx2 *bp)
  2901. {
  2902. u32 val;
  2903. val = REG_RD(bp, BNX2_MISC_CFG);
  2904. REG_WR(bp, BNX2_MISC_CFG, val & ~BNX2_MISC_CFG_NVM_WR_EN);
  2905. }
  2906. static void
  2907. bnx2_enable_nvram_access(struct bnx2 *bp)
  2908. {
  2909. u32 val;
  2910. val = REG_RD(bp, BNX2_NVM_ACCESS_ENABLE);
  2911. /* Enable both bits, even on read. */
  2912. REG_WR(bp, BNX2_NVM_ACCESS_ENABLE,
  2913. val | BNX2_NVM_ACCESS_ENABLE_EN | BNX2_NVM_ACCESS_ENABLE_WR_EN);
  2914. }
  2915. static void
  2916. bnx2_disable_nvram_access(struct bnx2 *bp)
  2917. {
  2918. u32 val;
  2919. val = REG_RD(bp, BNX2_NVM_ACCESS_ENABLE);
  2920. /* Disable both bits, even after read. */
  2921. REG_WR(bp, BNX2_NVM_ACCESS_ENABLE,
  2922. val & ~(BNX2_NVM_ACCESS_ENABLE_EN |
  2923. BNX2_NVM_ACCESS_ENABLE_WR_EN));
  2924. }
  2925. static int
  2926. bnx2_nvram_erase_page(struct bnx2 *bp, u32 offset)
  2927. {
  2928. u32 cmd;
  2929. int j;
  2930. if (bp->flash_info->flags & BNX2_NV_BUFFERED)
  2931. /* Buffered flash, no erase needed */
  2932. return 0;
  2933. /* Build an erase command */
  2934. cmd = BNX2_NVM_COMMAND_ERASE | BNX2_NVM_COMMAND_WR |
  2935. BNX2_NVM_COMMAND_DOIT;
  2936. /* Need to clear DONE bit separately. */
  2937. REG_WR(bp, BNX2_NVM_COMMAND, BNX2_NVM_COMMAND_DONE);
  2938. /* Address of the NVRAM to read from. */
  2939. REG_WR(bp, BNX2_NVM_ADDR, offset & BNX2_NVM_ADDR_NVM_ADDR_VALUE);
  2940. /* Issue an erase command. */
  2941. REG_WR(bp, BNX2_NVM_COMMAND, cmd);
  2942. /* Wait for completion. */
  2943. for (j = 0; j < NVRAM_TIMEOUT_COUNT; j++) {
  2944. u32 val;
  2945. udelay(5);
  2946. val = REG_RD(bp, BNX2_NVM_COMMAND);
  2947. if (val & BNX2_NVM_COMMAND_DONE)
  2948. break;
  2949. }
  2950. if (j >= NVRAM_TIMEOUT_COUNT)
  2951. return -EBUSY;
  2952. return 0;
  2953. }
  2954. static int
  2955. bnx2_nvram_read_dword(struct bnx2 *bp, u32 offset, u8 *ret_val, u32 cmd_flags)
  2956. {
  2957. u32 cmd;
  2958. int j;
  2959. /* Build the command word. */
  2960. cmd = BNX2_NVM_COMMAND_DOIT | cmd_flags;
  2961. /* Calculate an offset of a buffered flash, not needed for 5709. */
  2962. if (bp->flash_info->flags & BNX2_NV_TRANSLATE) {
  2963. offset = ((offset / bp->flash_info->page_size) <<
  2964. bp->flash_info->page_bits) +
  2965. (offset % bp->flash_info->page_size);
  2966. }
  2967. /* Need to clear DONE bit separately. */
  2968. REG_WR(bp, BNX2_NVM_COMMAND, BNX2_NVM_COMMAND_DONE);
  2969. /* Address of the NVRAM to read from. */
  2970. REG_WR(bp, BNX2_NVM_ADDR, offset & BNX2_NVM_ADDR_NVM_ADDR_VALUE);
  2971. /* Issue a read command. */
  2972. REG_WR(bp, BNX2_NVM_COMMAND, cmd);
  2973. /* Wait for completion. */
  2974. for (j = 0; j < NVRAM_TIMEOUT_COUNT; j++) {
  2975. u32 val;
  2976. udelay(5);
  2977. val = REG_RD(bp, BNX2_NVM_COMMAND);
  2978. if (val & BNX2_NVM_COMMAND_DONE) {
  2979. val = REG_RD(bp, BNX2_NVM_READ);
  2980. val = be32_to_cpu(val);
  2981. memcpy(ret_val, &val, 4);
  2982. break;
  2983. }
  2984. }
  2985. if (j >= NVRAM_TIMEOUT_COUNT)
  2986. return -EBUSY;
  2987. return 0;
  2988. }
  2989. static int
  2990. bnx2_nvram_write_dword(struct bnx2 *bp, u32 offset, u8 *val, u32 cmd_flags)
  2991. {
  2992. u32 cmd, val32;
  2993. int j;
  2994. /* Build the command word. */
  2995. cmd = BNX2_NVM_COMMAND_DOIT | BNX2_NVM_COMMAND_WR | cmd_flags;
  2996. /* Calculate an offset of a buffered flash, not needed for 5709. */
  2997. if (bp->flash_info->flags & BNX2_NV_TRANSLATE) {
  2998. offset = ((offset / bp->flash_info->page_size) <<
  2999. bp->flash_info->page_bits) +
  3000. (offset % bp->flash_info->page_size);
  3001. }
  3002. /* Need to clear DONE bit separately. */
  3003. REG_WR(bp, BNX2_NVM_COMMAND, BNX2_NVM_COMMAND_DONE);
  3004. memcpy(&val32, val, 4);
  3005. val32 = cpu_to_be32(val32);
  3006. /* Write the data. */
  3007. REG_WR(bp, BNX2_NVM_WRITE, val32);
  3008. /* Address of the NVRAM to write to. */
  3009. REG_WR(bp, BNX2_NVM_ADDR, offset & BNX2_NVM_ADDR_NVM_ADDR_VALUE);
  3010. /* Issue the write command. */
  3011. REG_WR(bp, BNX2_NVM_COMMAND, cmd);
  3012. /* Wait for completion. */
  3013. for (j = 0; j < NVRAM_TIMEOUT_COUNT; j++) {
  3014. udelay(5);
  3015. if (REG_RD(bp, BNX2_NVM_COMMAND) & BNX2_NVM_COMMAND_DONE)
  3016. break;
  3017. }
  3018. if (j >= NVRAM_TIMEOUT_COUNT)
  3019. return -EBUSY;
  3020. return 0;
  3021. }
  3022. static int
  3023. bnx2_init_nvram(struct bnx2 *bp)
  3024. {
  3025. u32 val;
  3026. int j, entry_count, rc = 0;
  3027. struct flash_spec *flash;
  3028. if (CHIP_NUM(bp) == CHIP_NUM_5709) {
  3029. bp->flash_info = &flash_5709;
  3030. goto get_flash_size;
  3031. }
  3032. /* Determine the selected interface. */
  3033. val = REG_RD(bp, BNX2_NVM_CFG1);
  3034. entry_count = ARRAY_SIZE(flash_table);
  3035. if (val & 0x40000000) {
  3036. /* Flash interface has been reconfigured */
  3037. for (j = 0, flash = &flash_table[0]; j < entry_count;
  3038. j++, flash++) {
  3039. if ((val & FLASH_BACKUP_STRAP_MASK) ==
  3040. (flash->config1 & FLASH_BACKUP_STRAP_MASK)) {
  3041. bp->flash_info = flash;
  3042. break;
  3043. }
  3044. }
  3045. }
  3046. else {
  3047. u32 mask;
  3048. /* Not yet been reconfigured */
  3049. if (val & (1 << 23))
  3050. mask = FLASH_BACKUP_STRAP_MASK;
  3051. else
  3052. mask = FLASH_STRAP_MASK;
  3053. for (j = 0, flash = &flash_table[0]; j < entry_count;
  3054. j++, flash++) {
  3055. if ((val & mask) == (flash->strapping & mask)) {
  3056. bp->flash_info = flash;
  3057. /* Request access to the flash interface. */
  3058. if ((rc = bnx2_acquire_nvram_lock(bp)) != 0)
  3059. return rc;
  3060. /* Enable access to flash interface */
  3061. bnx2_enable_nvram_access(bp);
  3062. /* Reconfigure the flash interface */
  3063. REG_WR(bp, BNX2_NVM_CFG1, flash->config1);
  3064. REG_WR(bp, BNX2_NVM_CFG2, flash->config2);
  3065. REG_WR(bp, BNX2_NVM_CFG3, flash->config3);
  3066. REG_WR(bp, BNX2_NVM_WRITE1, flash->write1);
  3067. /* Disable access to flash interface */
  3068. bnx2_disable_nvram_access(bp);
  3069. bnx2_release_nvram_lock(bp);
  3070. break;
  3071. }
  3072. }
  3073. } /* if (val & 0x40000000) */
  3074. if (j == entry_count) {
  3075. bp->flash_info = NULL;
  3076. printk(KERN_ALERT PFX "Unknown flash/EEPROM type.\n");
  3077. return -ENODEV;
  3078. }
  3079. get_flash_size:
  3080. val = REG_RD_IND(bp, bp->shmem_base + BNX2_SHARED_HW_CFG_CONFIG2);
  3081. val &= BNX2_SHARED_HW_CFG2_NVM_SIZE_MASK;
  3082. if (val)
  3083. bp->flash_size = val;
  3084. else
  3085. bp->flash_size = bp->flash_info->total_size;
  3086. return rc;
  3087. }
  3088. static int
  3089. bnx2_nvram_read(struct bnx2 *bp, u32 offset, u8 *ret_buf,
  3090. int buf_size)
  3091. {
  3092. int rc = 0;
  3093. u32 cmd_flags, offset32, len32, extra;
  3094. if (buf_size == 0)
  3095. return 0;
  3096. /* Request access to the flash interface. */
  3097. if ((rc = bnx2_acquire_nvram_lock(bp)) != 0)
  3098. return rc;
  3099. /* Enable access to flash interface */
  3100. bnx2_enable_nvram_access(bp);
  3101. len32 = buf_size;
  3102. offset32 = offset;
  3103. extra = 0;
  3104. cmd_flags = 0;
  3105. if (offset32 & 3) {
  3106. u8 buf[4];
  3107. u32 pre_len;
  3108. offset32 &= ~3;
  3109. pre_len = 4 - (offset & 3);
  3110. if (pre_len >= len32) {
  3111. pre_len = len32;
  3112. cmd_flags = BNX2_NVM_COMMAND_FIRST |
  3113. BNX2_NVM_COMMAND_LAST;
  3114. }
  3115. else {
  3116. cmd_flags = BNX2_NVM_COMMAND_FIRST;
  3117. }
  3118. rc = bnx2_nvram_read_dword(bp, offset32, buf, cmd_flags);
  3119. if (rc)
  3120. return rc;
  3121. memcpy(ret_buf, buf + (offset & 3), pre_len);
  3122. offset32 += 4;
  3123. ret_buf += pre_len;
  3124. len32 -= pre_len;
  3125. }
  3126. if (len32 & 3) {
  3127. extra = 4 - (len32 & 3);
  3128. len32 = (len32 + 4) & ~3;
  3129. }
  3130. if (len32 == 4) {
  3131. u8 buf[4];
  3132. if (cmd_flags)
  3133. cmd_flags = BNX2_NVM_COMMAND_LAST;
  3134. else
  3135. cmd_flags = BNX2_NVM_COMMAND_FIRST |
  3136. BNX2_NVM_COMMAND_LAST;
  3137. rc = bnx2_nvram_read_dword(bp, offset32, buf, cmd_flags);
  3138. memcpy(ret_buf, buf, 4 - extra);
  3139. }
  3140. else if (len32 > 0) {
  3141. u8 buf[4];
  3142. /* Read the first word. */
  3143. if (cmd_flags)
  3144. cmd_flags = 0;
  3145. else
  3146. cmd_flags = BNX2_NVM_COMMAND_FIRST;
  3147. rc = bnx2_nvram_read_dword(bp, offset32, ret_buf, cmd_flags);
  3148. /* Advance to the next dword. */
  3149. offset32 += 4;
  3150. ret_buf += 4;
  3151. len32 -= 4;
  3152. while (len32 > 4 && rc == 0) {
  3153. rc = bnx2_nvram_read_dword(bp, offset32, ret_buf, 0);
  3154. /* Advance to the next dword. */
  3155. offset32 += 4;
  3156. ret_buf += 4;
  3157. len32 -= 4;
  3158. }
  3159. if (rc)
  3160. return rc;
  3161. cmd_flags = BNX2_NVM_COMMAND_LAST;
  3162. rc = bnx2_nvram_read_dword(bp, offset32, buf, cmd_flags);
  3163. memcpy(ret_buf, buf, 4 - extra);
  3164. }
  3165. /* Disable access to flash interface */
  3166. bnx2_disable_nvram_access(bp);
  3167. bnx2_release_nvram_lock(bp);
  3168. return rc;
  3169. }
  3170. static int
  3171. bnx2_nvram_write(struct bnx2 *bp, u32 offset, u8 *data_buf,
  3172. int buf_size)
  3173. {
  3174. u32 written, offset32, len32;
  3175. u8 *buf, start[4], end[4], *align_buf = NULL, *flash_buffer = NULL;
  3176. int rc = 0;
  3177. int align_start, align_end;
  3178. buf = data_buf;
  3179. offset32 = offset;
  3180. len32 = buf_size;
  3181. align_start = align_end = 0;
  3182. if ((align_start = (offset32 & 3))) {
  3183. offset32 &= ~3;
  3184. len32 += align_start;
  3185. if (len32 < 4)
  3186. len32 = 4;
  3187. if ((rc = bnx2_nvram_read(bp, offset32, start, 4)))
  3188. return rc;
  3189. }
  3190. if (len32 & 3) {
  3191. align_end = 4 - (len32 & 3);
  3192. len32 += align_end;
  3193. if ((rc = bnx2_nvram_read(bp, offset32 + len32 - 4, end, 4)))
  3194. return rc;
  3195. }
  3196. if (align_start || align_end) {
  3197. align_buf = kmalloc(len32, GFP_KERNEL);
  3198. if (align_buf == NULL)
  3199. return -ENOMEM;
  3200. if (align_start) {
  3201. memcpy(align_buf, start, 4);
  3202. }
  3203. if (align_end) {
  3204. memcpy(align_buf + len32 - 4, end, 4);
  3205. }
  3206. memcpy(align_buf + align_start, data_buf, buf_size);
  3207. buf = align_buf;
  3208. }
  3209. if (!(bp->flash_info->flags & BNX2_NV_BUFFERED)) {
  3210. flash_buffer = kmalloc(264, GFP_KERNEL);
  3211. if (flash_buffer == NULL) {
  3212. rc = -ENOMEM;
  3213. goto nvram_write_end;
  3214. }
  3215. }
  3216. written = 0;
  3217. while ((written < len32) && (rc == 0)) {
  3218. u32 page_start, page_end, data_start, data_end;
  3219. u32 addr, cmd_flags;
  3220. int i;
  3221. /* Find the page_start addr */
  3222. page_start = offset32 + written;
  3223. page_start -= (page_start % bp->flash_info->page_size);
  3224. /* Find the page_end addr */
  3225. page_end = page_start + bp->flash_info->page_size;
  3226. /* Find the data_start addr */
  3227. data_start = (written == 0) ? offset32 : page_start;
  3228. /* Find the data_end addr */
  3229. data_end = (page_end > offset32 + len32) ?
  3230. (offset32 + len32) : page_end;
  3231. /* Request access to the flash interface. */
  3232. if ((rc = bnx2_acquire_nvram_lock(bp)) != 0)
  3233. goto nvram_write_end;
  3234. /* Enable access to flash interface */
  3235. bnx2_enable_nvram_access(bp);
  3236. cmd_flags = BNX2_NVM_COMMAND_FIRST;
  3237. if (!(bp->flash_info->flags & BNX2_NV_BUFFERED)) {
  3238. int j;
  3239. /* Read the whole page into the buffer
  3240. * (non-buffer flash only) */
  3241. for (j = 0; j < bp->flash_info->page_size; j += 4) {
  3242. if (j == (bp->flash_info->page_size - 4)) {
  3243. cmd_flags |= BNX2_NVM_COMMAND_LAST;
  3244. }
  3245. rc = bnx2_nvram_read_dword(bp,
  3246. page_start + j,
  3247. &flash_buffer[j],
  3248. cmd_flags);
  3249. if (rc)
  3250. goto nvram_write_end;
  3251. cmd_flags = 0;
  3252. }
  3253. }
  3254. /* Enable writes to flash interface (unlock write-protect) */
  3255. if ((rc = bnx2_enable_nvram_write(bp)) != 0)
  3256. goto nvram_write_end;
  3257. /* Loop to write back the buffer data from page_start to
  3258. * data_start */
  3259. i = 0;
  3260. if (!(bp->flash_info->flags & BNX2_NV_BUFFERED)) {
  3261. /* Erase the page */
  3262. if ((rc = bnx2_nvram_erase_page(bp, page_start)) != 0)
  3263. goto nvram_write_end;
  3264. /* Re-enable the write again for the actual write */
  3265. bnx2_enable_nvram_write(bp);
  3266. for (addr = page_start; addr < data_start;
  3267. addr += 4, i += 4) {
  3268. rc = bnx2_nvram_write_dword(bp, addr,
  3269. &flash_buffer[i], cmd_flags);
  3270. if (rc != 0)
  3271. goto nvram_write_end;
  3272. cmd_flags = 0;
  3273. }
  3274. }
  3275. /* Loop to write the new data from data_start to data_end */
  3276. for (addr = data_start; addr < data_end; addr += 4, i += 4) {
  3277. if ((addr == page_end - 4) ||
  3278. ((bp->flash_info->flags & BNX2_NV_BUFFERED) &&
  3279. (addr == data_end - 4))) {
  3280. cmd_flags |= BNX2_NVM_COMMAND_LAST;
  3281. }
  3282. rc = bnx2_nvram_write_dword(bp, addr, buf,
  3283. cmd_flags);
  3284. if (rc != 0)
  3285. goto nvram_write_end;
  3286. cmd_flags = 0;
  3287. buf += 4;
  3288. }
  3289. /* Loop to write back the buffer data from data_end
  3290. * to page_end */
  3291. if (!(bp->flash_info->flags & BNX2_NV_BUFFERED)) {
  3292. for (addr = data_end; addr < page_end;
  3293. addr += 4, i += 4) {
  3294. if (addr == page_end-4) {
  3295. cmd_flags = BNX2_NVM_COMMAND_LAST;
  3296. }
  3297. rc = bnx2_nvram_write_dword(bp, addr,
  3298. &flash_buffer[i], cmd_flags);
  3299. if (rc != 0)
  3300. goto nvram_write_end;
  3301. cmd_flags = 0;
  3302. }
  3303. }
  3304. /* Disable writes to flash interface (lock write-protect) */
  3305. bnx2_disable_nvram_write(bp);
  3306. /* Disable access to flash interface */
  3307. bnx2_disable_nvram_access(bp);
  3308. bnx2_release_nvram_lock(bp);
  3309. /* Increment written */
  3310. written += data_end - data_start;
  3311. }
  3312. nvram_write_end:
  3313. kfree(flash_buffer);
  3314. kfree(align_buf);
  3315. return rc;
  3316. }
  3317. static void
  3318. bnx2_init_remote_phy(struct bnx2 *bp)
  3319. {
  3320. u32 val;
  3321. bp->phy_flags &= ~REMOTE_PHY_CAP_FLAG;
  3322. if (!(bp->phy_flags & PHY_SERDES_FLAG))
  3323. return;
  3324. val = REG_RD_IND(bp, bp->shmem_base + BNX2_FW_CAP_MB);
  3325. if ((val & BNX2_FW_CAP_SIGNATURE_MASK) != BNX2_FW_CAP_SIGNATURE)
  3326. return;
  3327. if (val & BNX2_FW_CAP_REMOTE_PHY_CAPABLE) {
  3328. bp->phy_flags |= REMOTE_PHY_CAP_FLAG;
  3329. val = REG_RD_IND(bp, bp->shmem_base + BNX2_LINK_STATUS);
  3330. if (val & BNX2_LINK_STATUS_SERDES_LINK)
  3331. bp->phy_port = PORT_FIBRE;
  3332. else
  3333. bp->phy_port = PORT_TP;
  3334. if (netif_running(bp->dev)) {
  3335. u32 sig;
  3336. if (val & BNX2_LINK_STATUS_LINK_UP) {
  3337. bp->link_up = 1;
  3338. netif_carrier_on(bp->dev);
  3339. } else {
  3340. bp->link_up = 0;
  3341. netif_carrier_off(bp->dev);
  3342. }
  3343. sig = BNX2_DRV_ACK_CAP_SIGNATURE |
  3344. BNX2_FW_CAP_REMOTE_PHY_CAPABLE;
  3345. REG_WR_IND(bp, bp->shmem_base + BNX2_DRV_ACK_CAP_MB,
  3346. sig);
  3347. }
  3348. }
  3349. }
  3350. static int
  3351. bnx2_reset_chip(struct bnx2 *bp, u32 reset_code)
  3352. {
  3353. u32 val;
  3354. int i, rc = 0;
  3355. u8 old_port;
  3356. /* Wait for the current PCI transaction to complete before
  3357. * issuing a reset. */
  3358. REG_WR(bp, BNX2_MISC_ENABLE_CLR_BITS,
  3359. BNX2_MISC_ENABLE_CLR_BITS_TX_DMA_ENABLE |
  3360. BNX2_MISC_ENABLE_CLR_BITS_DMA_ENGINE_ENABLE |
  3361. BNX2_MISC_ENABLE_CLR_BITS_RX_DMA_ENABLE |
  3362. BNX2_MISC_ENABLE_CLR_BITS_HOST_COALESCE_ENABLE);
  3363. val = REG_RD(bp, BNX2_MISC_ENABLE_CLR_BITS);
  3364. udelay(5);
  3365. /* Wait for the firmware to tell us it is ok to issue a reset. */
  3366. bnx2_fw_sync(bp, BNX2_DRV_MSG_DATA_WAIT0 | reset_code, 1);
  3367. /* Deposit a driver reset signature so the firmware knows that
  3368. * this is a soft reset. */
  3369. REG_WR_IND(bp, bp->shmem_base + BNX2_DRV_RESET_SIGNATURE,
  3370. BNX2_DRV_RESET_SIGNATURE_MAGIC);
  3371. /* Do a dummy read to force the chip to complete all current transaction
  3372. * before we issue a reset. */
  3373. val = REG_RD(bp, BNX2_MISC_ID);
  3374. if (CHIP_NUM(bp) == CHIP_NUM_5709) {
  3375. REG_WR(bp, BNX2_MISC_COMMAND, BNX2_MISC_COMMAND_SW_RESET);
  3376. REG_RD(bp, BNX2_MISC_COMMAND);
  3377. udelay(5);
  3378. val = BNX2_PCICFG_MISC_CONFIG_REG_WINDOW_ENA |
  3379. BNX2_PCICFG_MISC_CONFIG_TARGET_MB_WORD_SWAP;
  3380. pci_write_config_dword(bp->pdev, BNX2_PCICFG_MISC_CONFIG, val);
  3381. } else {
  3382. val = BNX2_PCICFG_MISC_CONFIG_CORE_RST_REQ |
  3383. BNX2_PCICFG_MISC_CONFIG_REG_WINDOW_ENA |
  3384. BNX2_PCICFG_MISC_CONFIG_TARGET_MB_WORD_SWAP;
  3385. /* Chip reset. */
  3386. REG_WR(bp, BNX2_PCICFG_MISC_CONFIG, val);
  3387. /* Reading back any register after chip reset will hang the
  3388. * bus on 5706 A0 and A1. The msleep below provides plenty
  3389. * of margin for write posting.
  3390. */
  3391. if ((CHIP_ID(bp) == CHIP_ID_5706_A0) ||
  3392. (CHIP_ID(bp) == CHIP_ID_5706_A1))
  3393. msleep(20);
  3394. /* Reset takes approximate 30 usec */
  3395. for (i = 0; i < 10; i++) {
  3396. val = REG_RD(bp, BNX2_PCICFG_MISC_CONFIG);
  3397. if ((val & (BNX2_PCICFG_MISC_CONFIG_CORE_RST_REQ |
  3398. BNX2_PCICFG_MISC_CONFIG_CORE_RST_BSY)) == 0)
  3399. break;
  3400. udelay(10);
  3401. }
  3402. if (val & (BNX2_PCICFG_MISC_CONFIG_CORE_RST_REQ |
  3403. BNX2_PCICFG_MISC_CONFIG_CORE_RST_BSY)) {
  3404. printk(KERN_ERR PFX "Chip reset did not complete\n");
  3405. return -EBUSY;
  3406. }
  3407. }
  3408. /* Make sure byte swapping is properly configured. */
  3409. val = REG_RD(bp, BNX2_PCI_SWAP_DIAG0);
  3410. if (val != 0x01020304) {
  3411. printk(KERN_ERR PFX "Chip not in correct endian mode\n");
  3412. return -ENODEV;
  3413. }
  3414. /* Wait for the firmware to finish its initialization. */
  3415. rc = bnx2_fw_sync(bp, BNX2_DRV_MSG_DATA_WAIT1 | reset_code, 0);
  3416. if (rc)
  3417. return rc;
  3418. spin_lock_bh(&bp->phy_lock);
  3419. old_port = bp->phy_port;
  3420. bnx2_init_remote_phy(bp);
  3421. if ((bp->phy_flags & REMOTE_PHY_CAP_FLAG) && old_port != bp->phy_port)
  3422. bnx2_set_default_remote_link(bp);
  3423. spin_unlock_bh(&bp->phy_lock);
  3424. if (CHIP_ID(bp) == CHIP_ID_5706_A0) {
  3425. /* Adjust the voltage regular to two steps lower. The default
  3426. * of this register is 0x0000000e. */
  3427. REG_WR(bp, BNX2_MISC_VREG_CONTROL, 0x000000fa);
  3428. /* Remove bad rbuf memory from the free pool. */
  3429. rc = bnx2_alloc_bad_rbuf(bp);
  3430. }
  3431. return rc;
  3432. }
  3433. static int
  3434. bnx2_init_chip(struct bnx2 *bp)
  3435. {
  3436. u32 val;
  3437. int rc;
  3438. /* Make sure the interrupt is not active. */
  3439. REG_WR(bp, BNX2_PCICFG_INT_ACK_CMD, BNX2_PCICFG_INT_ACK_CMD_MASK_INT);
  3440. val = BNX2_DMA_CONFIG_DATA_BYTE_SWAP |
  3441. BNX2_DMA_CONFIG_DATA_WORD_SWAP |
  3442. #ifdef __BIG_ENDIAN
  3443. BNX2_DMA_CONFIG_CNTL_BYTE_SWAP |
  3444. #endif
  3445. BNX2_DMA_CONFIG_CNTL_WORD_SWAP |
  3446. DMA_READ_CHANS << 12 |
  3447. DMA_WRITE_CHANS << 16;
  3448. val |= (0x2 << 20) | (1 << 11);
  3449. if ((bp->flags & PCIX_FLAG) && (bp->bus_speed_mhz == 133))
  3450. val |= (1 << 23);
  3451. if ((CHIP_NUM(bp) == CHIP_NUM_5706) &&
  3452. (CHIP_ID(bp) != CHIP_ID_5706_A0) && !(bp->flags & PCIX_FLAG))
  3453. val |= BNX2_DMA_CONFIG_CNTL_PING_PONG_DMA;
  3454. REG_WR(bp, BNX2_DMA_CONFIG, val);
  3455. if (CHIP_ID(bp) == CHIP_ID_5706_A0) {
  3456. val = REG_RD(bp, BNX2_TDMA_CONFIG);
  3457. val |= BNX2_TDMA_CONFIG_ONE_DMA;
  3458. REG_WR(bp, BNX2_TDMA_CONFIG, val);
  3459. }
  3460. if (bp->flags & PCIX_FLAG) {
  3461. u16 val16;
  3462. pci_read_config_word(bp->pdev, bp->pcix_cap + PCI_X_CMD,
  3463. &val16);
  3464. pci_write_config_word(bp->pdev, bp->pcix_cap + PCI_X_CMD,
  3465. val16 & ~PCI_X_CMD_ERO);
  3466. }
  3467. REG_WR(bp, BNX2_MISC_ENABLE_SET_BITS,
  3468. BNX2_MISC_ENABLE_SET_BITS_HOST_COALESCE_ENABLE |
  3469. BNX2_MISC_ENABLE_STATUS_BITS_RX_V2P_ENABLE |
  3470. BNX2_MISC_ENABLE_STATUS_BITS_CONTEXT_ENABLE);
  3471. /* Initialize context mapping and zero out the quick contexts. The
  3472. * context block must have already been enabled. */
  3473. if (CHIP_NUM(bp) == CHIP_NUM_5709) {
  3474. rc = bnx2_init_5709_context(bp);
  3475. if (rc)
  3476. return rc;
  3477. } else
  3478. bnx2_init_context(bp);
  3479. if ((rc = bnx2_init_cpus(bp)) != 0)
  3480. return rc;
  3481. bnx2_init_nvram(bp);
  3482. bnx2_set_mac_addr(bp);
  3483. val = REG_RD(bp, BNX2_MQ_CONFIG);
  3484. val &= ~BNX2_MQ_CONFIG_KNL_BYP_BLK_SIZE;
  3485. val |= BNX2_MQ_CONFIG_KNL_BYP_BLK_SIZE_256;
  3486. if (CHIP_ID(bp) == CHIP_ID_5709_A0 || CHIP_ID(bp) == CHIP_ID_5709_A1)
  3487. val |= BNX2_MQ_CONFIG_HALT_DIS;
  3488. REG_WR(bp, BNX2_MQ_CONFIG, val);
  3489. val = 0x10000 + (MAX_CID_CNT * MB_KERNEL_CTX_SIZE);
  3490. REG_WR(bp, BNX2_MQ_KNL_BYP_WIND_START, val);
  3491. REG_WR(bp, BNX2_MQ_KNL_WIND_END, val);
  3492. val = (BCM_PAGE_BITS - 8) << 24;
  3493. REG_WR(bp, BNX2_RV2P_CONFIG, val);
  3494. /* Configure page size. */
  3495. val = REG_RD(bp, BNX2_TBDR_CONFIG);
  3496. val &= ~BNX2_TBDR_CONFIG_PAGE_SIZE;
  3497. val |= (BCM_PAGE_BITS - 8) << 24 | 0x40;
  3498. REG_WR(bp, BNX2_TBDR_CONFIG, val);
  3499. val = bp->mac_addr[0] +
  3500. (bp->mac_addr[1] << 8) +
  3501. (bp->mac_addr[2] << 16) +
  3502. bp->mac_addr[3] +
  3503. (bp->mac_addr[4] << 8) +
  3504. (bp->mac_addr[5] << 16);
  3505. REG_WR(bp, BNX2_EMAC_BACKOFF_SEED, val);
  3506. /* Program the MTU. Also include 4 bytes for CRC32. */
  3507. val = bp->dev->mtu + ETH_HLEN + 4;
  3508. if (val > (MAX_ETHERNET_PACKET_SIZE + 4))
  3509. val |= BNX2_EMAC_RX_MTU_SIZE_JUMBO_ENA;
  3510. REG_WR(bp, BNX2_EMAC_RX_MTU_SIZE, val);
  3511. bp->bnx2_napi.last_status_idx = 0;
  3512. bp->rx_mode = BNX2_EMAC_RX_MODE_SORT_MODE;
  3513. /* Set up how to generate a link change interrupt. */
  3514. REG_WR(bp, BNX2_EMAC_ATTENTION_ENA, BNX2_EMAC_ATTENTION_ENA_LINK);
  3515. REG_WR(bp, BNX2_HC_STATUS_ADDR_L,
  3516. (u64) bp->status_blk_mapping & 0xffffffff);
  3517. REG_WR(bp, BNX2_HC_STATUS_ADDR_H, (u64) bp->status_blk_mapping >> 32);
  3518. REG_WR(bp, BNX2_HC_STATISTICS_ADDR_L,
  3519. (u64) bp->stats_blk_mapping & 0xffffffff);
  3520. REG_WR(bp, BNX2_HC_STATISTICS_ADDR_H,
  3521. (u64) bp->stats_blk_mapping >> 32);
  3522. REG_WR(bp, BNX2_HC_TX_QUICK_CONS_TRIP,
  3523. (bp->tx_quick_cons_trip_int << 16) | bp->tx_quick_cons_trip);
  3524. REG_WR(bp, BNX2_HC_RX_QUICK_CONS_TRIP,
  3525. (bp->rx_quick_cons_trip_int << 16) | bp->rx_quick_cons_trip);
  3526. REG_WR(bp, BNX2_HC_COMP_PROD_TRIP,
  3527. (bp->comp_prod_trip_int << 16) | bp->comp_prod_trip);
  3528. REG_WR(bp, BNX2_HC_TX_TICKS, (bp->tx_ticks_int << 16) | bp->tx_ticks);
  3529. REG_WR(bp, BNX2_HC_RX_TICKS, (bp->rx_ticks_int << 16) | bp->rx_ticks);
  3530. REG_WR(bp, BNX2_HC_COM_TICKS,
  3531. (bp->com_ticks_int << 16) | bp->com_ticks);
  3532. REG_WR(bp, BNX2_HC_CMD_TICKS,
  3533. (bp->cmd_ticks_int << 16) | bp->cmd_ticks);
  3534. if (CHIP_NUM(bp) == CHIP_NUM_5708)
  3535. REG_WR(bp, BNX2_HC_STATS_TICKS, 0);
  3536. else
  3537. REG_WR(bp, BNX2_HC_STATS_TICKS, bp->stats_ticks);
  3538. REG_WR(bp, BNX2_HC_STAT_COLLECT_TICKS, 0xbb8); /* 3ms */
  3539. if (CHIP_ID(bp) == CHIP_ID_5706_A1)
  3540. val = BNX2_HC_CONFIG_COLLECT_STATS;
  3541. else {
  3542. val = BNX2_HC_CONFIG_RX_TMR_MODE | BNX2_HC_CONFIG_TX_TMR_MODE |
  3543. BNX2_HC_CONFIG_COLLECT_STATS;
  3544. }
  3545. if (bp->flags & ONE_SHOT_MSI_FLAG)
  3546. val |= BNX2_HC_CONFIG_ONE_SHOT;
  3547. REG_WR(bp, BNX2_HC_CONFIG, val);
  3548. /* Clear internal stats counters. */
  3549. REG_WR(bp, BNX2_HC_COMMAND, BNX2_HC_COMMAND_CLR_STAT_NOW);
  3550. REG_WR(bp, BNX2_HC_ATTN_BITS_ENABLE, STATUS_ATTN_EVENTS);
  3551. /* Initialize the receive filter. */
  3552. bnx2_set_rx_mode(bp->dev);
  3553. if (CHIP_NUM(bp) == CHIP_NUM_5709) {
  3554. val = REG_RD(bp, BNX2_MISC_NEW_CORE_CTL);
  3555. val |= BNX2_MISC_NEW_CORE_CTL_DMA_ENABLE;
  3556. REG_WR(bp, BNX2_MISC_NEW_CORE_CTL, val);
  3557. }
  3558. rc = bnx2_fw_sync(bp, BNX2_DRV_MSG_DATA_WAIT2 | BNX2_DRV_MSG_CODE_RESET,
  3559. 0);
  3560. REG_WR(bp, BNX2_MISC_ENABLE_SET_BITS, BNX2_MISC_ENABLE_DEFAULT);
  3561. REG_RD(bp, BNX2_MISC_ENABLE_SET_BITS);
  3562. udelay(20);
  3563. bp->hc_cmd = REG_RD(bp, BNX2_HC_COMMAND);
  3564. return rc;
  3565. }
  3566. static void
  3567. bnx2_init_tx_context(struct bnx2 *bp, u32 cid)
  3568. {
  3569. u32 val, offset0, offset1, offset2, offset3;
  3570. if (CHIP_NUM(bp) == CHIP_NUM_5709) {
  3571. offset0 = BNX2_L2CTX_TYPE_XI;
  3572. offset1 = BNX2_L2CTX_CMD_TYPE_XI;
  3573. offset2 = BNX2_L2CTX_TBDR_BHADDR_HI_XI;
  3574. offset3 = BNX2_L2CTX_TBDR_BHADDR_LO_XI;
  3575. } else {
  3576. offset0 = BNX2_L2CTX_TYPE;
  3577. offset1 = BNX2_L2CTX_CMD_TYPE;
  3578. offset2 = BNX2_L2CTX_TBDR_BHADDR_HI;
  3579. offset3 = BNX2_L2CTX_TBDR_BHADDR_LO;
  3580. }
  3581. val = BNX2_L2CTX_TYPE_TYPE_L2 | BNX2_L2CTX_TYPE_SIZE_L2;
  3582. CTX_WR(bp, GET_CID_ADDR(cid), offset0, val);
  3583. val = BNX2_L2CTX_CMD_TYPE_TYPE_L2 | (8 << 16);
  3584. CTX_WR(bp, GET_CID_ADDR(cid), offset1, val);
  3585. val = (u64) bp->tx_desc_mapping >> 32;
  3586. CTX_WR(bp, GET_CID_ADDR(cid), offset2, val);
  3587. val = (u64) bp->tx_desc_mapping & 0xffffffff;
  3588. CTX_WR(bp, GET_CID_ADDR(cid), offset3, val);
  3589. }
  3590. static void
  3591. bnx2_init_tx_ring(struct bnx2 *bp)
  3592. {
  3593. struct tx_bd *txbd;
  3594. u32 cid;
  3595. struct bnx2_napi *bnapi = &bp->bnx2_napi;
  3596. bp->tx_wake_thresh = bp->tx_ring_size / 2;
  3597. txbd = &bp->tx_desc_ring[MAX_TX_DESC_CNT];
  3598. txbd->tx_bd_haddr_hi = (u64) bp->tx_desc_mapping >> 32;
  3599. txbd->tx_bd_haddr_lo = (u64) bp->tx_desc_mapping & 0xffffffff;
  3600. bp->tx_prod = 0;
  3601. bnapi->tx_cons = 0;
  3602. bnapi->hw_tx_cons = 0;
  3603. bp->tx_prod_bseq = 0;
  3604. cid = TX_CID;
  3605. bp->tx_bidx_addr = MB_GET_CID_ADDR(cid) + BNX2_L2CTX_TX_HOST_BIDX;
  3606. bp->tx_bseq_addr = MB_GET_CID_ADDR(cid) + BNX2_L2CTX_TX_HOST_BSEQ;
  3607. bnx2_init_tx_context(bp, cid);
  3608. }
  3609. static void
  3610. bnx2_init_rxbd_rings(struct rx_bd *rx_ring[], dma_addr_t dma[], u32 buf_size,
  3611. int num_rings)
  3612. {
  3613. int i;
  3614. struct rx_bd *rxbd;
  3615. for (i = 0; i < num_rings; i++) {
  3616. int j;
  3617. rxbd = &rx_ring[i][0];
  3618. for (j = 0; j < MAX_RX_DESC_CNT; j++, rxbd++) {
  3619. rxbd->rx_bd_len = buf_size;
  3620. rxbd->rx_bd_flags = RX_BD_FLAGS_START | RX_BD_FLAGS_END;
  3621. }
  3622. if (i == (num_rings - 1))
  3623. j = 0;
  3624. else
  3625. j = i + 1;
  3626. rxbd->rx_bd_haddr_hi = (u64) dma[j] >> 32;
  3627. rxbd->rx_bd_haddr_lo = (u64) dma[j] & 0xffffffff;
  3628. }
  3629. }
  3630. static void
  3631. bnx2_init_rx_ring(struct bnx2 *bp)
  3632. {
  3633. int i;
  3634. u16 prod, ring_prod;
  3635. u32 val, rx_cid_addr = GET_CID_ADDR(RX_CID);
  3636. struct bnx2_napi *bnapi = &bp->bnx2_napi;
  3637. bnapi->rx_prod = 0;
  3638. bnapi->rx_cons = 0;
  3639. bnapi->rx_prod_bseq = 0;
  3640. bnapi->rx_pg_prod = 0;
  3641. bnapi->rx_pg_cons = 0;
  3642. bnx2_init_rxbd_rings(bp->rx_desc_ring, bp->rx_desc_mapping,
  3643. bp->rx_buf_use_size, bp->rx_max_ring);
  3644. CTX_WR(bp, rx_cid_addr, BNX2_L2CTX_PG_BUF_SIZE, 0);
  3645. if (bp->rx_pg_ring_size) {
  3646. bnx2_init_rxbd_rings(bp->rx_pg_desc_ring,
  3647. bp->rx_pg_desc_mapping,
  3648. PAGE_SIZE, bp->rx_max_pg_ring);
  3649. val = (bp->rx_buf_use_size << 16) | PAGE_SIZE;
  3650. CTX_WR(bp, rx_cid_addr, BNX2_L2CTX_PG_BUF_SIZE, val);
  3651. CTX_WR(bp, rx_cid_addr, BNX2_L2CTX_RBDC_KEY,
  3652. BNX2_L2CTX_RBDC_JUMBO_KEY);
  3653. val = (u64) bp->rx_pg_desc_mapping[0] >> 32;
  3654. CTX_WR(bp, rx_cid_addr, BNX2_L2CTX_NX_PG_BDHADDR_HI, val);
  3655. val = (u64) bp->rx_pg_desc_mapping[0] & 0xffffffff;
  3656. CTX_WR(bp, rx_cid_addr, BNX2_L2CTX_NX_PG_BDHADDR_LO, val);
  3657. if (CHIP_NUM(bp) == CHIP_NUM_5709)
  3658. REG_WR(bp, BNX2_MQ_MAP_L2_3, BNX2_MQ_MAP_L2_3_DEFAULT);
  3659. }
  3660. val = BNX2_L2CTX_CTX_TYPE_CTX_BD_CHN_TYPE_VALUE;
  3661. val |= BNX2_L2CTX_CTX_TYPE_SIZE_L2;
  3662. val |= 0x02 << 8;
  3663. CTX_WR(bp, rx_cid_addr, BNX2_L2CTX_CTX_TYPE, val);
  3664. val = (u64) bp->rx_desc_mapping[0] >> 32;
  3665. CTX_WR(bp, rx_cid_addr, BNX2_L2CTX_NX_BDHADDR_HI, val);
  3666. val = (u64) bp->rx_desc_mapping[0] & 0xffffffff;
  3667. CTX_WR(bp, rx_cid_addr, BNX2_L2CTX_NX_BDHADDR_LO, val);
  3668. ring_prod = prod = bnapi->rx_pg_prod;
  3669. for (i = 0; i < bp->rx_pg_ring_size; i++) {
  3670. if (bnx2_alloc_rx_page(bp, ring_prod) < 0)
  3671. break;
  3672. prod = NEXT_RX_BD(prod);
  3673. ring_prod = RX_PG_RING_IDX(prod);
  3674. }
  3675. bnapi->rx_pg_prod = prod;
  3676. ring_prod = prod = bnapi->rx_prod;
  3677. for (i = 0; i < bp->rx_ring_size; i++) {
  3678. if (bnx2_alloc_rx_skb(bp, bnapi, ring_prod) < 0) {
  3679. break;
  3680. }
  3681. prod = NEXT_RX_BD(prod);
  3682. ring_prod = RX_RING_IDX(prod);
  3683. }
  3684. bnapi->rx_prod = prod;
  3685. REG_WR16(bp, MB_RX_CID_ADDR + BNX2_L2CTX_HOST_PG_BDIDX,
  3686. bnapi->rx_pg_prod);
  3687. REG_WR16(bp, MB_RX_CID_ADDR + BNX2_L2CTX_HOST_BDIDX, prod);
  3688. REG_WR(bp, MB_RX_CID_ADDR + BNX2_L2CTX_HOST_BSEQ, bnapi->rx_prod_bseq);
  3689. }
  3690. static u32 bnx2_find_max_ring(u32 ring_size, u32 max_size)
  3691. {
  3692. u32 max, num_rings = 1;
  3693. while (ring_size > MAX_RX_DESC_CNT) {
  3694. ring_size -= MAX_RX_DESC_CNT;
  3695. num_rings++;
  3696. }
  3697. /* round to next power of 2 */
  3698. max = max_size;
  3699. while ((max & num_rings) == 0)
  3700. max >>= 1;
  3701. if (num_rings != max)
  3702. max <<= 1;
  3703. return max;
  3704. }
  3705. static void
  3706. bnx2_set_rx_ring_size(struct bnx2 *bp, u32 size)
  3707. {
  3708. u32 rx_size, rx_space, jumbo_size;
  3709. /* 8 for CRC and VLAN */
  3710. rx_size = bp->dev->mtu + ETH_HLEN + bp->rx_offset + 8;
  3711. rx_space = SKB_DATA_ALIGN(rx_size + BNX2_RX_ALIGN) + NET_SKB_PAD +
  3712. sizeof(struct skb_shared_info);
  3713. bp->rx_copy_thresh = RX_COPY_THRESH;
  3714. bp->rx_pg_ring_size = 0;
  3715. bp->rx_max_pg_ring = 0;
  3716. bp->rx_max_pg_ring_idx = 0;
  3717. if (rx_space > PAGE_SIZE) {
  3718. int pages = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
  3719. jumbo_size = size * pages;
  3720. if (jumbo_size > MAX_TOTAL_RX_PG_DESC_CNT)
  3721. jumbo_size = MAX_TOTAL_RX_PG_DESC_CNT;
  3722. bp->rx_pg_ring_size = jumbo_size;
  3723. bp->rx_max_pg_ring = bnx2_find_max_ring(jumbo_size,
  3724. MAX_RX_PG_RINGS);
  3725. bp->rx_max_pg_ring_idx = (bp->rx_max_pg_ring * RX_DESC_CNT) - 1;
  3726. rx_size = RX_COPY_THRESH + bp->rx_offset;
  3727. bp->rx_copy_thresh = 0;
  3728. }
  3729. bp->rx_buf_use_size = rx_size;
  3730. /* hw alignment */
  3731. bp->rx_buf_size = bp->rx_buf_use_size + BNX2_RX_ALIGN;
  3732. bp->rx_jumbo_thresh = rx_size - bp->rx_offset;
  3733. bp->rx_ring_size = size;
  3734. bp->rx_max_ring = bnx2_find_max_ring(size, MAX_RX_RINGS);
  3735. bp->rx_max_ring_idx = (bp->rx_max_ring * RX_DESC_CNT) - 1;
  3736. }
  3737. static void
  3738. bnx2_free_tx_skbs(struct bnx2 *bp)
  3739. {
  3740. int i;
  3741. if (bp->tx_buf_ring == NULL)
  3742. return;
  3743. for (i = 0; i < TX_DESC_CNT; ) {
  3744. struct sw_bd *tx_buf = &bp->tx_buf_ring[i];
  3745. struct sk_buff *skb = tx_buf->skb;
  3746. int j, last;
  3747. if (skb == NULL) {
  3748. i++;
  3749. continue;
  3750. }
  3751. pci_unmap_single(bp->pdev, pci_unmap_addr(tx_buf, mapping),
  3752. skb_headlen(skb), PCI_DMA_TODEVICE);
  3753. tx_buf->skb = NULL;
  3754. last = skb_shinfo(skb)->nr_frags;
  3755. for (j = 0; j < last; j++) {
  3756. tx_buf = &bp->tx_buf_ring[i + j + 1];
  3757. pci_unmap_page(bp->pdev,
  3758. pci_unmap_addr(tx_buf, mapping),
  3759. skb_shinfo(skb)->frags[j].size,
  3760. PCI_DMA_TODEVICE);
  3761. }
  3762. dev_kfree_skb(skb);
  3763. i += j + 1;
  3764. }
  3765. }
  3766. static void
  3767. bnx2_free_rx_skbs(struct bnx2 *bp)
  3768. {
  3769. int i;
  3770. if (bp->rx_buf_ring == NULL)
  3771. return;
  3772. for (i = 0; i < bp->rx_max_ring_idx; i++) {
  3773. struct sw_bd *rx_buf = &bp->rx_buf_ring[i];
  3774. struct sk_buff *skb = rx_buf->skb;
  3775. if (skb == NULL)
  3776. continue;
  3777. pci_unmap_single(bp->pdev, pci_unmap_addr(rx_buf, mapping),
  3778. bp->rx_buf_use_size, PCI_DMA_FROMDEVICE);
  3779. rx_buf->skb = NULL;
  3780. dev_kfree_skb(skb);
  3781. }
  3782. for (i = 0; i < bp->rx_max_pg_ring_idx; i++)
  3783. bnx2_free_rx_page(bp, i);
  3784. }
  3785. static void
  3786. bnx2_free_skbs(struct bnx2 *bp)
  3787. {
  3788. bnx2_free_tx_skbs(bp);
  3789. bnx2_free_rx_skbs(bp);
  3790. }
  3791. static int
  3792. bnx2_reset_nic(struct bnx2 *bp, u32 reset_code)
  3793. {
  3794. int rc;
  3795. rc = bnx2_reset_chip(bp, reset_code);
  3796. bnx2_free_skbs(bp);
  3797. if (rc)
  3798. return rc;
  3799. if ((rc = bnx2_init_chip(bp)) != 0)
  3800. return rc;
  3801. bnx2_init_tx_ring(bp);
  3802. bnx2_init_rx_ring(bp);
  3803. return 0;
  3804. }
  3805. static int
  3806. bnx2_init_nic(struct bnx2 *bp)
  3807. {
  3808. int rc;
  3809. if ((rc = bnx2_reset_nic(bp, BNX2_DRV_MSG_CODE_RESET)) != 0)
  3810. return rc;
  3811. spin_lock_bh(&bp->phy_lock);
  3812. bnx2_init_phy(bp);
  3813. bnx2_set_link(bp);
  3814. spin_unlock_bh(&bp->phy_lock);
  3815. return 0;
  3816. }
  3817. static int
  3818. bnx2_test_registers(struct bnx2 *bp)
  3819. {
  3820. int ret;
  3821. int i, is_5709;
  3822. static const struct {
  3823. u16 offset;
  3824. u16 flags;
  3825. #define BNX2_FL_NOT_5709 1
  3826. u32 rw_mask;
  3827. u32 ro_mask;
  3828. } reg_tbl[] = {
  3829. { 0x006c, 0, 0x00000000, 0x0000003f },
  3830. { 0x0090, 0, 0xffffffff, 0x00000000 },
  3831. { 0x0094, 0, 0x00000000, 0x00000000 },
  3832. { 0x0404, BNX2_FL_NOT_5709, 0x00003f00, 0x00000000 },
  3833. { 0x0418, BNX2_FL_NOT_5709, 0x00000000, 0xffffffff },
  3834. { 0x041c, BNX2_FL_NOT_5709, 0x00000000, 0xffffffff },
  3835. { 0x0420, BNX2_FL_NOT_5709, 0x00000000, 0x80ffffff },
  3836. { 0x0424, BNX2_FL_NOT_5709, 0x00000000, 0x00000000 },
  3837. { 0x0428, BNX2_FL_NOT_5709, 0x00000000, 0x00000001 },
  3838. { 0x0450, BNX2_FL_NOT_5709, 0x00000000, 0x0000ffff },
  3839. { 0x0454, BNX2_FL_NOT_5709, 0x00000000, 0xffffffff },
  3840. { 0x0458, BNX2_FL_NOT_5709, 0x00000000, 0xffffffff },
  3841. { 0x0808, BNX2_FL_NOT_5709, 0x00000000, 0xffffffff },
  3842. { 0x0854, BNX2_FL_NOT_5709, 0x00000000, 0xffffffff },
  3843. { 0x0868, BNX2_FL_NOT_5709, 0x00000000, 0x77777777 },
  3844. { 0x086c, BNX2_FL_NOT_5709, 0x00000000, 0x77777777 },
  3845. { 0x0870, BNX2_FL_NOT_5709, 0x00000000, 0x77777777 },
  3846. { 0x0874, BNX2_FL_NOT_5709, 0x00000000, 0x77777777 },
  3847. { 0x0c00, BNX2_FL_NOT_5709, 0x00000000, 0x00000001 },
  3848. { 0x0c04, BNX2_FL_NOT_5709, 0x00000000, 0x03ff0001 },
  3849. { 0x0c08, BNX2_FL_NOT_5709, 0x0f0ff073, 0x00000000 },
  3850. { 0x1000, 0, 0x00000000, 0x00000001 },
  3851. { 0x1004, 0, 0x00000000, 0x000f0001 },
  3852. { 0x1408, 0, 0x01c00800, 0x00000000 },
  3853. { 0x149c, 0, 0x8000ffff, 0x00000000 },
  3854. { 0x14a8, 0, 0x00000000, 0x000001ff },
  3855. { 0x14ac, 0, 0x0fffffff, 0x10000000 },
  3856. { 0x14b0, 0, 0x00000002, 0x00000001 },
  3857. { 0x14b8, 0, 0x00000000, 0x00000000 },
  3858. { 0x14c0, 0, 0x00000000, 0x00000009 },
  3859. { 0x14c4, 0, 0x00003fff, 0x00000000 },
  3860. { 0x14cc, 0, 0x00000000, 0x00000001 },
  3861. { 0x14d0, 0, 0xffffffff, 0x00000000 },
  3862. { 0x1800, 0, 0x00000000, 0x00000001 },
  3863. { 0x1804, 0, 0x00000000, 0x00000003 },
  3864. { 0x2800, 0, 0x00000000, 0x00000001 },
  3865. { 0x2804, 0, 0x00000000, 0x00003f01 },
  3866. { 0x2808, 0, 0x0f3f3f03, 0x00000000 },
  3867. { 0x2810, 0, 0xffff0000, 0x00000000 },
  3868. { 0x2814, 0, 0xffff0000, 0x00000000 },
  3869. { 0x2818, 0, 0xffff0000, 0x00000000 },
  3870. { 0x281c, 0, 0xffff0000, 0x00000000 },
  3871. { 0x2834, 0, 0xffffffff, 0x00000000 },
  3872. { 0x2840, 0, 0x00000000, 0xffffffff },
  3873. { 0x2844, 0, 0x00000000, 0xffffffff },
  3874. { 0x2848, 0, 0xffffffff, 0x00000000 },
  3875. { 0x284c, 0, 0xf800f800, 0x07ff07ff },
  3876. { 0x2c00, 0, 0x00000000, 0x00000011 },
  3877. { 0x2c04, 0, 0x00000000, 0x00030007 },
  3878. { 0x3c00, 0, 0x00000000, 0x00000001 },
  3879. { 0x3c04, 0, 0x00000000, 0x00070000 },
  3880. { 0x3c08, 0, 0x00007f71, 0x07f00000 },
  3881. { 0x3c0c, 0, 0x1f3ffffc, 0x00000000 },
  3882. { 0x3c10, 0, 0xffffffff, 0x00000000 },
  3883. { 0x3c14, 0, 0x00000000, 0xffffffff },
  3884. { 0x3c18, 0, 0x00000000, 0xffffffff },
  3885. { 0x3c1c, 0, 0xfffff000, 0x00000000 },
  3886. { 0x3c20, 0, 0xffffff00, 0x00000000 },
  3887. { 0x5004, 0, 0x00000000, 0x0000007f },
  3888. { 0x5008, 0, 0x0f0007ff, 0x00000000 },
  3889. { 0x5c00, 0, 0x00000000, 0x00000001 },
  3890. { 0x5c04, 0, 0x00000000, 0x0003000f },
  3891. { 0x5c08, 0, 0x00000003, 0x00000000 },
  3892. { 0x5c0c, 0, 0x0000fff8, 0x00000000 },
  3893. { 0x5c10, 0, 0x00000000, 0xffffffff },
  3894. { 0x5c80, 0, 0x00000000, 0x0f7113f1 },
  3895. { 0x5c84, 0, 0x00000000, 0x0000f333 },
  3896. { 0x5c88, 0, 0x00000000, 0x00077373 },
  3897. { 0x5c8c, 0, 0x00000000, 0x0007f737 },
  3898. { 0x6808, 0, 0x0000ff7f, 0x00000000 },
  3899. { 0x680c, 0, 0xffffffff, 0x00000000 },
  3900. { 0x6810, 0, 0xffffffff, 0x00000000 },
  3901. { 0x6814, 0, 0xffffffff, 0x00000000 },
  3902. { 0x6818, 0, 0xffffffff, 0x00000000 },
  3903. { 0x681c, 0, 0xffffffff, 0x00000000 },
  3904. { 0x6820, 0, 0x00ff00ff, 0x00000000 },
  3905. { 0x6824, 0, 0x00ff00ff, 0x00000000 },
  3906. { 0x6828, 0, 0x00ff00ff, 0x00000000 },
  3907. { 0x682c, 0, 0x03ff03ff, 0x00000000 },
  3908. { 0x6830, 0, 0x03ff03ff, 0x00000000 },
  3909. { 0x6834, 0, 0x03ff03ff, 0x00000000 },
  3910. { 0x6838, 0, 0x03ff03ff, 0x00000000 },
  3911. { 0x683c, 0, 0x0000ffff, 0x00000000 },
  3912. { 0x6840, 0, 0x00000ff0, 0x00000000 },
  3913. { 0x6844, 0, 0x00ffff00, 0x00000000 },
  3914. { 0x684c, 0, 0xffffffff, 0x00000000 },
  3915. { 0x6850, 0, 0x7f7f7f7f, 0x00000000 },
  3916. { 0x6854, 0, 0x7f7f7f7f, 0x00000000 },
  3917. { 0x6858, 0, 0x7f7f7f7f, 0x00000000 },
  3918. { 0x685c, 0, 0x7f7f7f7f, 0x00000000 },
  3919. { 0x6908, 0, 0x00000000, 0x0001ff0f },
  3920. { 0x690c, 0, 0x00000000, 0x0ffe00f0 },
  3921. { 0xffff, 0, 0x00000000, 0x00000000 },
  3922. };
  3923. ret = 0;
  3924. is_5709 = 0;
  3925. if (CHIP_NUM(bp) == CHIP_NUM_5709)
  3926. is_5709 = 1;
  3927. for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
  3928. u32 offset, rw_mask, ro_mask, save_val, val;
  3929. u16 flags = reg_tbl[i].flags;
  3930. if (is_5709 && (flags & BNX2_FL_NOT_5709))
  3931. continue;
  3932. offset = (u32) reg_tbl[i].offset;
  3933. rw_mask = reg_tbl[i].rw_mask;
  3934. ro_mask = reg_tbl[i].ro_mask;
  3935. save_val = readl(bp->regview + offset);
  3936. writel(0, bp->regview + offset);
  3937. val = readl(bp->regview + offset);
  3938. if ((val & rw_mask) != 0) {
  3939. goto reg_test_err;
  3940. }
  3941. if ((val & ro_mask) != (save_val & ro_mask)) {
  3942. goto reg_test_err;
  3943. }
  3944. writel(0xffffffff, bp->regview + offset);
  3945. val = readl(bp->regview + offset);
  3946. if ((val & rw_mask) != rw_mask) {
  3947. goto reg_test_err;
  3948. }
  3949. if ((val & ro_mask) != (save_val & ro_mask)) {
  3950. goto reg_test_err;
  3951. }
  3952. writel(save_val, bp->regview + offset);
  3953. continue;
  3954. reg_test_err:
  3955. writel(save_val, bp->regview + offset);
  3956. ret = -ENODEV;
  3957. break;
  3958. }
  3959. return ret;
  3960. }
  3961. static int
  3962. bnx2_do_mem_test(struct bnx2 *bp, u32 start, u32 size)
  3963. {
  3964. static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0x55555555,
  3965. 0xaaaaaaaa , 0xaa55aa55, 0x55aa55aa };
  3966. int i;
  3967. for (i = 0; i < sizeof(test_pattern) / 4; i++) {
  3968. u32 offset;
  3969. for (offset = 0; offset < size; offset += 4) {
  3970. REG_WR_IND(bp, start + offset, test_pattern[i]);
  3971. if (REG_RD_IND(bp, start + offset) !=
  3972. test_pattern[i]) {
  3973. return -ENODEV;
  3974. }
  3975. }
  3976. }
  3977. return 0;
  3978. }
  3979. static int
  3980. bnx2_test_memory(struct bnx2 *bp)
  3981. {
  3982. int ret = 0;
  3983. int i;
  3984. static struct mem_entry {
  3985. u32 offset;
  3986. u32 len;
  3987. } mem_tbl_5706[] = {
  3988. { 0x60000, 0x4000 },
  3989. { 0xa0000, 0x3000 },
  3990. { 0xe0000, 0x4000 },
  3991. { 0x120000, 0x4000 },
  3992. { 0x1a0000, 0x4000 },
  3993. { 0x160000, 0x4000 },
  3994. { 0xffffffff, 0 },
  3995. },
  3996. mem_tbl_5709[] = {
  3997. { 0x60000, 0x4000 },
  3998. { 0xa0000, 0x3000 },
  3999. { 0xe0000, 0x4000 },
  4000. { 0x120000, 0x4000 },
  4001. { 0x1a0000, 0x4000 },
  4002. { 0xffffffff, 0 },
  4003. };
  4004. struct mem_entry *mem_tbl;
  4005. if (CHIP_NUM(bp) == CHIP_NUM_5709)
  4006. mem_tbl = mem_tbl_5709;
  4007. else
  4008. mem_tbl = mem_tbl_5706;
  4009. for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
  4010. if ((ret = bnx2_do_mem_test(bp, mem_tbl[i].offset,
  4011. mem_tbl[i].len)) != 0) {
  4012. return ret;
  4013. }
  4014. }
  4015. return ret;
  4016. }
  4017. #define BNX2_MAC_LOOPBACK 0
  4018. #define BNX2_PHY_LOOPBACK 1
  4019. static int
  4020. bnx2_run_loopback(struct bnx2 *bp, int loopback_mode)
  4021. {
  4022. unsigned int pkt_size, num_pkts, i;
  4023. struct sk_buff *skb, *rx_skb;
  4024. unsigned char *packet;
  4025. u16 rx_start_idx, rx_idx;
  4026. dma_addr_t map;
  4027. struct tx_bd *txbd;
  4028. struct sw_bd *rx_buf;
  4029. struct l2_fhdr *rx_hdr;
  4030. int ret = -ENODEV;
  4031. struct bnx2_napi *bnapi = &bp->bnx2_napi;
  4032. if (loopback_mode == BNX2_MAC_LOOPBACK) {
  4033. bp->loopback = MAC_LOOPBACK;
  4034. bnx2_set_mac_loopback(bp);
  4035. }
  4036. else if (loopback_mode == BNX2_PHY_LOOPBACK) {
  4037. if (bp->phy_flags & REMOTE_PHY_CAP_FLAG)
  4038. return 0;
  4039. bp->loopback = PHY_LOOPBACK;
  4040. bnx2_set_phy_loopback(bp);
  4041. }
  4042. else
  4043. return -EINVAL;
  4044. pkt_size = min(bp->dev->mtu + ETH_HLEN, bp->rx_jumbo_thresh - 4);
  4045. skb = netdev_alloc_skb(bp->dev, pkt_size);
  4046. if (!skb)
  4047. return -ENOMEM;
  4048. packet = skb_put(skb, pkt_size);
  4049. memcpy(packet, bp->dev->dev_addr, 6);
  4050. memset(packet + 6, 0x0, 8);
  4051. for (i = 14; i < pkt_size; i++)
  4052. packet[i] = (unsigned char) (i & 0xff);
  4053. map = pci_map_single(bp->pdev, skb->data, pkt_size,
  4054. PCI_DMA_TODEVICE);
  4055. REG_WR(bp, BNX2_HC_COMMAND,
  4056. bp->hc_cmd | BNX2_HC_COMMAND_COAL_NOW_WO_INT);
  4057. REG_RD(bp, BNX2_HC_COMMAND);
  4058. udelay(5);
  4059. rx_start_idx = bnx2_get_hw_rx_cons(bnapi);
  4060. num_pkts = 0;
  4061. txbd = &bp->tx_desc_ring[TX_RING_IDX(bp->tx_prod)];
  4062. txbd->tx_bd_haddr_hi = (u64) map >> 32;
  4063. txbd->tx_bd_haddr_lo = (u64) map & 0xffffffff;
  4064. txbd->tx_bd_mss_nbytes = pkt_size;
  4065. txbd->tx_bd_vlan_tag_flags = TX_BD_FLAGS_START | TX_BD_FLAGS_END;
  4066. num_pkts++;
  4067. bp->tx_prod = NEXT_TX_BD(bp->tx_prod);
  4068. bp->tx_prod_bseq += pkt_size;
  4069. REG_WR16(bp, bp->tx_bidx_addr, bp->tx_prod);
  4070. REG_WR(bp, bp->tx_bseq_addr, bp->tx_prod_bseq);
  4071. udelay(100);
  4072. REG_WR(bp, BNX2_HC_COMMAND,
  4073. bp->hc_cmd | BNX2_HC_COMMAND_COAL_NOW_WO_INT);
  4074. REG_RD(bp, BNX2_HC_COMMAND);
  4075. udelay(5);
  4076. pci_unmap_single(bp->pdev, map, pkt_size, PCI_DMA_TODEVICE);
  4077. dev_kfree_skb(skb);
  4078. if (bnx2_get_hw_tx_cons(bnapi) != bp->tx_prod)
  4079. goto loopback_test_done;
  4080. rx_idx = bnx2_get_hw_rx_cons(bnapi);
  4081. if (rx_idx != rx_start_idx + num_pkts) {
  4082. goto loopback_test_done;
  4083. }
  4084. rx_buf = &bp->rx_buf_ring[rx_start_idx];
  4085. rx_skb = rx_buf->skb;
  4086. rx_hdr = (struct l2_fhdr *) rx_skb->data;
  4087. skb_reserve(rx_skb, bp->rx_offset);
  4088. pci_dma_sync_single_for_cpu(bp->pdev,
  4089. pci_unmap_addr(rx_buf, mapping),
  4090. bp->rx_buf_size, PCI_DMA_FROMDEVICE);
  4091. if (rx_hdr->l2_fhdr_status &
  4092. (L2_FHDR_ERRORS_BAD_CRC |
  4093. L2_FHDR_ERRORS_PHY_DECODE |
  4094. L2_FHDR_ERRORS_ALIGNMENT |
  4095. L2_FHDR_ERRORS_TOO_SHORT |
  4096. L2_FHDR_ERRORS_GIANT_FRAME)) {
  4097. goto loopback_test_done;
  4098. }
  4099. if ((rx_hdr->l2_fhdr_pkt_len - 4) != pkt_size) {
  4100. goto loopback_test_done;
  4101. }
  4102. for (i = 14; i < pkt_size; i++) {
  4103. if (*(rx_skb->data + i) != (unsigned char) (i & 0xff)) {
  4104. goto loopback_test_done;
  4105. }
  4106. }
  4107. ret = 0;
  4108. loopback_test_done:
  4109. bp->loopback = 0;
  4110. return ret;
  4111. }
  4112. #define BNX2_MAC_LOOPBACK_FAILED 1
  4113. #define BNX2_PHY_LOOPBACK_FAILED 2
  4114. #define BNX2_LOOPBACK_FAILED (BNX2_MAC_LOOPBACK_FAILED | \
  4115. BNX2_PHY_LOOPBACK_FAILED)
  4116. static int
  4117. bnx2_test_loopback(struct bnx2 *bp)
  4118. {
  4119. int rc = 0;
  4120. if (!netif_running(bp->dev))
  4121. return BNX2_LOOPBACK_FAILED;
  4122. bnx2_reset_nic(bp, BNX2_DRV_MSG_CODE_RESET);
  4123. spin_lock_bh(&bp->phy_lock);
  4124. bnx2_init_phy(bp);
  4125. spin_unlock_bh(&bp->phy_lock);
  4126. if (bnx2_run_loopback(bp, BNX2_MAC_LOOPBACK))
  4127. rc |= BNX2_MAC_LOOPBACK_FAILED;
  4128. if (bnx2_run_loopback(bp, BNX2_PHY_LOOPBACK))
  4129. rc |= BNX2_PHY_LOOPBACK_FAILED;
  4130. return rc;
  4131. }
  4132. #define NVRAM_SIZE 0x200
  4133. #define CRC32_RESIDUAL 0xdebb20e3
  4134. static int
  4135. bnx2_test_nvram(struct bnx2 *bp)
  4136. {
  4137. u32 buf[NVRAM_SIZE / 4];
  4138. u8 *data = (u8 *) buf;
  4139. int rc = 0;
  4140. u32 magic, csum;
  4141. if ((rc = bnx2_nvram_read(bp, 0, data, 4)) != 0)
  4142. goto test_nvram_done;
  4143. magic = be32_to_cpu(buf[0]);
  4144. if (magic != 0x669955aa) {
  4145. rc = -ENODEV;
  4146. goto test_nvram_done;
  4147. }
  4148. if ((rc = bnx2_nvram_read(bp, 0x100, data, NVRAM_SIZE)) != 0)
  4149. goto test_nvram_done;
  4150. csum = ether_crc_le(0x100, data);
  4151. if (csum != CRC32_RESIDUAL) {
  4152. rc = -ENODEV;
  4153. goto test_nvram_done;
  4154. }
  4155. csum = ether_crc_le(0x100, data + 0x100);
  4156. if (csum != CRC32_RESIDUAL) {
  4157. rc = -ENODEV;
  4158. }
  4159. test_nvram_done:
  4160. return rc;
  4161. }
  4162. static int
  4163. bnx2_test_link(struct bnx2 *bp)
  4164. {
  4165. u32 bmsr;
  4166. if (bp->phy_flags & REMOTE_PHY_CAP_FLAG) {
  4167. if (bp->link_up)
  4168. return 0;
  4169. return -ENODEV;
  4170. }
  4171. spin_lock_bh(&bp->phy_lock);
  4172. bnx2_enable_bmsr1(bp);
  4173. bnx2_read_phy(bp, bp->mii_bmsr1, &bmsr);
  4174. bnx2_read_phy(bp, bp->mii_bmsr1, &bmsr);
  4175. bnx2_disable_bmsr1(bp);
  4176. spin_unlock_bh(&bp->phy_lock);
  4177. if (bmsr & BMSR_LSTATUS) {
  4178. return 0;
  4179. }
  4180. return -ENODEV;
  4181. }
  4182. static int
  4183. bnx2_test_intr(struct bnx2 *bp)
  4184. {
  4185. int i;
  4186. u16 status_idx;
  4187. if (!netif_running(bp->dev))
  4188. return -ENODEV;
  4189. status_idx = REG_RD(bp, BNX2_PCICFG_INT_ACK_CMD) & 0xffff;
  4190. /* This register is not touched during run-time. */
  4191. REG_WR(bp, BNX2_HC_COMMAND, bp->hc_cmd | BNX2_HC_COMMAND_COAL_NOW);
  4192. REG_RD(bp, BNX2_HC_COMMAND);
  4193. for (i = 0; i < 10; i++) {
  4194. if ((REG_RD(bp, BNX2_PCICFG_INT_ACK_CMD) & 0xffff) !=
  4195. status_idx) {
  4196. break;
  4197. }
  4198. msleep_interruptible(10);
  4199. }
  4200. if (i < 10)
  4201. return 0;
  4202. return -ENODEV;
  4203. }
  4204. static void
  4205. bnx2_5706_serdes_timer(struct bnx2 *bp)
  4206. {
  4207. spin_lock(&bp->phy_lock);
  4208. if (bp->serdes_an_pending)
  4209. bp->serdes_an_pending--;
  4210. else if ((bp->link_up == 0) && (bp->autoneg & AUTONEG_SPEED)) {
  4211. u32 bmcr;
  4212. bp->current_interval = bp->timer_interval;
  4213. bnx2_read_phy(bp, bp->mii_bmcr, &bmcr);
  4214. if (bmcr & BMCR_ANENABLE) {
  4215. u32 phy1, phy2;
  4216. bnx2_write_phy(bp, 0x1c, 0x7c00);
  4217. bnx2_read_phy(bp, 0x1c, &phy1);
  4218. bnx2_write_phy(bp, 0x17, 0x0f01);
  4219. bnx2_read_phy(bp, 0x15, &phy2);
  4220. bnx2_write_phy(bp, 0x17, 0x0f01);
  4221. bnx2_read_phy(bp, 0x15, &phy2);
  4222. if ((phy1 & 0x10) && /* SIGNAL DETECT */
  4223. !(phy2 & 0x20)) { /* no CONFIG */
  4224. bmcr &= ~BMCR_ANENABLE;
  4225. bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
  4226. bnx2_write_phy(bp, bp->mii_bmcr, bmcr);
  4227. bp->phy_flags |= PHY_PARALLEL_DETECT_FLAG;
  4228. }
  4229. }
  4230. }
  4231. else if ((bp->link_up) && (bp->autoneg & AUTONEG_SPEED) &&
  4232. (bp->phy_flags & PHY_PARALLEL_DETECT_FLAG)) {
  4233. u32 phy2;
  4234. bnx2_write_phy(bp, 0x17, 0x0f01);
  4235. bnx2_read_phy(bp, 0x15, &phy2);
  4236. if (phy2 & 0x20) {
  4237. u32 bmcr;
  4238. bnx2_read_phy(bp, bp->mii_bmcr, &bmcr);
  4239. bmcr |= BMCR_ANENABLE;
  4240. bnx2_write_phy(bp, bp->mii_bmcr, bmcr);
  4241. bp->phy_flags &= ~PHY_PARALLEL_DETECT_FLAG;
  4242. }
  4243. } else
  4244. bp->current_interval = bp->timer_interval;
  4245. spin_unlock(&bp->phy_lock);
  4246. }
  4247. static void
  4248. bnx2_5708_serdes_timer(struct bnx2 *bp)
  4249. {
  4250. if (bp->phy_flags & REMOTE_PHY_CAP_FLAG)
  4251. return;
  4252. if ((bp->phy_flags & PHY_2_5G_CAPABLE_FLAG) == 0) {
  4253. bp->serdes_an_pending = 0;
  4254. return;
  4255. }
  4256. spin_lock(&bp->phy_lock);
  4257. if (bp->serdes_an_pending)
  4258. bp->serdes_an_pending--;
  4259. else if ((bp->link_up == 0) && (bp->autoneg & AUTONEG_SPEED)) {
  4260. u32 bmcr;
  4261. bnx2_read_phy(bp, bp->mii_bmcr, &bmcr);
  4262. if (bmcr & BMCR_ANENABLE) {
  4263. bnx2_enable_forced_2g5(bp);
  4264. bp->current_interval = SERDES_FORCED_TIMEOUT;
  4265. } else {
  4266. bnx2_disable_forced_2g5(bp);
  4267. bp->serdes_an_pending = 2;
  4268. bp->current_interval = bp->timer_interval;
  4269. }
  4270. } else
  4271. bp->current_interval = bp->timer_interval;
  4272. spin_unlock(&bp->phy_lock);
  4273. }
  4274. static void
  4275. bnx2_timer(unsigned long data)
  4276. {
  4277. struct bnx2 *bp = (struct bnx2 *) data;
  4278. if (!netif_running(bp->dev))
  4279. return;
  4280. if (atomic_read(&bp->intr_sem) != 0)
  4281. goto bnx2_restart_timer;
  4282. bnx2_send_heart_beat(bp);
  4283. bp->stats_blk->stat_FwRxDrop = REG_RD_IND(bp, BNX2_FW_RX_DROP_COUNT);
  4284. /* workaround occasional corrupted counters */
  4285. if (CHIP_NUM(bp) == CHIP_NUM_5708 && bp->stats_ticks)
  4286. REG_WR(bp, BNX2_HC_COMMAND, bp->hc_cmd |
  4287. BNX2_HC_COMMAND_STATS_NOW);
  4288. if (bp->phy_flags & PHY_SERDES_FLAG) {
  4289. if (CHIP_NUM(bp) == CHIP_NUM_5706)
  4290. bnx2_5706_serdes_timer(bp);
  4291. else
  4292. bnx2_5708_serdes_timer(bp);
  4293. }
  4294. bnx2_restart_timer:
  4295. mod_timer(&bp->timer, jiffies + bp->current_interval);
  4296. }
  4297. static int
  4298. bnx2_request_irq(struct bnx2 *bp)
  4299. {
  4300. struct net_device *dev = bp->dev;
  4301. unsigned long flags;
  4302. struct bnx2_irq *irq = &bp->irq_tbl[0];
  4303. int rc;
  4304. if (bp->flags & USING_MSI_FLAG)
  4305. flags = 0;
  4306. else
  4307. flags = IRQF_SHARED;
  4308. rc = request_irq(irq->vector, irq->handler, flags, dev->name, dev);
  4309. return rc;
  4310. }
  4311. static void
  4312. bnx2_free_irq(struct bnx2 *bp)
  4313. {
  4314. struct net_device *dev = bp->dev;
  4315. free_irq(bp->irq_tbl[0].vector, dev);
  4316. if (bp->flags & USING_MSI_FLAG) {
  4317. pci_disable_msi(bp->pdev);
  4318. bp->flags &= ~(USING_MSI_FLAG | ONE_SHOT_MSI_FLAG);
  4319. }
  4320. }
  4321. static void
  4322. bnx2_setup_int_mode(struct bnx2 *bp, int dis_msi)
  4323. {
  4324. bp->irq_tbl[0].handler = bnx2_interrupt;
  4325. strcpy(bp->irq_tbl[0].name, bp->dev->name);
  4326. if ((bp->flags & MSI_CAP_FLAG) && !dis_msi) {
  4327. if (pci_enable_msi(bp->pdev) == 0) {
  4328. bp->flags |= USING_MSI_FLAG;
  4329. if (CHIP_NUM(bp) == CHIP_NUM_5709) {
  4330. bp->flags |= ONE_SHOT_MSI_FLAG;
  4331. bp->irq_tbl[0].handler = bnx2_msi_1shot;
  4332. } else
  4333. bp->irq_tbl[0].handler = bnx2_msi;
  4334. }
  4335. }
  4336. bp->irq_tbl[0].vector = bp->pdev->irq;
  4337. }
  4338. /* Called with rtnl_lock */
  4339. static int
  4340. bnx2_open(struct net_device *dev)
  4341. {
  4342. struct bnx2 *bp = netdev_priv(dev);
  4343. int rc;
  4344. netif_carrier_off(dev);
  4345. bnx2_set_power_state(bp, PCI_D0);
  4346. bnx2_disable_int(bp);
  4347. rc = bnx2_alloc_mem(bp);
  4348. if (rc)
  4349. return rc;
  4350. bnx2_setup_int_mode(bp, disable_msi);
  4351. bnx2_napi_enable(bp);
  4352. rc = bnx2_request_irq(bp);
  4353. if (rc) {
  4354. bnx2_napi_disable(bp);
  4355. bnx2_free_mem(bp);
  4356. return rc;
  4357. }
  4358. rc = bnx2_init_nic(bp);
  4359. if (rc) {
  4360. bnx2_napi_disable(bp);
  4361. bnx2_free_irq(bp);
  4362. bnx2_free_skbs(bp);
  4363. bnx2_free_mem(bp);
  4364. return rc;
  4365. }
  4366. mod_timer(&bp->timer, jiffies + bp->current_interval);
  4367. atomic_set(&bp->intr_sem, 0);
  4368. bnx2_enable_int(bp);
  4369. if (bp->flags & USING_MSI_FLAG) {
  4370. /* Test MSI to make sure it is working
  4371. * If MSI test fails, go back to INTx mode
  4372. */
  4373. if (bnx2_test_intr(bp) != 0) {
  4374. printk(KERN_WARNING PFX "%s: No interrupt was generated"
  4375. " using MSI, switching to INTx mode. Please"
  4376. " report this failure to the PCI maintainer"
  4377. " and include system chipset information.\n",
  4378. bp->dev->name);
  4379. bnx2_disable_int(bp);
  4380. bnx2_free_irq(bp);
  4381. bnx2_setup_int_mode(bp, 1);
  4382. rc = bnx2_init_nic(bp);
  4383. if (!rc)
  4384. rc = bnx2_request_irq(bp);
  4385. if (rc) {
  4386. bnx2_napi_disable(bp);
  4387. bnx2_free_skbs(bp);
  4388. bnx2_free_mem(bp);
  4389. del_timer_sync(&bp->timer);
  4390. return rc;
  4391. }
  4392. bnx2_enable_int(bp);
  4393. }
  4394. }
  4395. if (bp->flags & USING_MSI_FLAG) {
  4396. printk(KERN_INFO PFX "%s: using MSI\n", dev->name);
  4397. }
  4398. netif_start_queue(dev);
  4399. return 0;
  4400. }
  4401. static void
  4402. bnx2_reset_task(struct work_struct *work)
  4403. {
  4404. struct bnx2 *bp = container_of(work, struct bnx2, reset_task);
  4405. if (!netif_running(bp->dev))
  4406. return;
  4407. bp->in_reset_task = 1;
  4408. bnx2_netif_stop(bp);
  4409. bnx2_init_nic(bp);
  4410. atomic_set(&bp->intr_sem, 1);
  4411. bnx2_netif_start(bp);
  4412. bp->in_reset_task = 0;
  4413. }
  4414. static void
  4415. bnx2_tx_timeout(struct net_device *dev)
  4416. {
  4417. struct bnx2 *bp = netdev_priv(dev);
  4418. /* This allows the netif to be shutdown gracefully before resetting */
  4419. schedule_work(&bp->reset_task);
  4420. }
  4421. #ifdef BCM_VLAN
  4422. /* Called with rtnl_lock */
  4423. static void
  4424. bnx2_vlan_rx_register(struct net_device *dev, struct vlan_group *vlgrp)
  4425. {
  4426. struct bnx2 *bp = netdev_priv(dev);
  4427. bnx2_netif_stop(bp);
  4428. bp->vlgrp = vlgrp;
  4429. bnx2_set_rx_mode(dev);
  4430. bnx2_netif_start(bp);
  4431. }
  4432. #endif
  4433. /* Called with netif_tx_lock.
  4434. * bnx2_tx_int() runs without netif_tx_lock unless it needs to call
  4435. * netif_wake_queue().
  4436. */
  4437. static int
  4438. bnx2_start_xmit(struct sk_buff *skb, struct net_device *dev)
  4439. {
  4440. struct bnx2 *bp = netdev_priv(dev);
  4441. dma_addr_t mapping;
  4442. struct tx_bd *txbd;
  4443. struct sw_bd *tx_buf;
  4444. u32 len, vlan_tag_flags, last_frag, mss;
  4445. u16 prod, ring_prod;
  4446. int i;
  4447. struct bnx2_napi *bnapi = &bp->bnx2_napi;
  4448. if (unlikely(bnx2_tx_avail(bp, bnapi) <
  4449. (skb_shinfo(skb)->nr_frags + 1))) {
  4450. netif_stop_queue(dev);
  4451. printk(KERN_ERR PFX "%s: BUG! Tx ring full when queue awake!\n",
  4452. dev->name);
  4453. return NETDEV_TX_BUSY;
  4454. }
  4455. len = skb_headlen(skb);
  4456. prod = bp->tx_prod;
  4457. ring_prod = TX_RING_IDX(prod);
  4458. vlan_tag_flags = 0;
  4459. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  4460. vlan_tag_flags |= TX_BD_FLAGS_TCP_UDP_CKSUM;
  4461. }
  4462. if (bp->vlgrp != 0 && vlan_tx_tag_present(skb)) {
  4463. vlan_tag_flags |=
  4464. (TX_BD_FLAGS_VLAN_TAG | (vlan_tx_tag_get(skb) << 16));
  4465. }
  4466. if ((mss = skb_shinfo(skb)->gso_size)) {
  4467. u32 tcp_opt_len, ip_tcp_len;
  4468. struct iphdr *iph;
  4469. vlan_tag_flags |= TX_BD_FLAGS_SW_LSO;
  4470. tcp_opt_len = tcp_optlen(skb);
  4471. if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) {
  4472. u32 tcp_off = skb_transport_offset(skb) -
  4473. sizeof(struct ipv6hdr) - ETH_HLEN;
  4474. vlan_tag_flags |= ((tcp_opt_len >> 2) << 8) |
  4475. TX_BD_FLAGS_SW_FLAGS;
  4476. if (likely(tcp_off == 0))
  4477. vlan_tag_flags &= ~TX_BD_FLAGS_TCP6_OFF0_MSK;
  4478. else {
  4479. tcp_off >>= 3;
  4480. vlan_tag_flags |= ((tcp_off & 0x3) <<
  4481. TX_BD_FLAGS_TCP6_OFF0_SHL) |
  4482. ((tcp_off & 0x10) <<
  4483. TX_BD_FLAGS_TCP6_OFF4_SHL);
  4484. mss |= (tcp_off & 0xc) << TX_BD_TCP6_OFF2_SHL;
  4485. }
  4486. } else {
  4487. if (skb_header_cloned(skb) &&
  4488. pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
  4489. dev_kfree_skb(skb);
  4490. return NETDEV_TX_OK;
  4491. }
  4492. ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
  4493. iph = ip_hdr(skb);
  4494. iph->check = 0;
  4495. iph->tot_len = htons(mss + ip_tcp_len + tcp_opt_len);
  4496. tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
  4497. iph->daddr, 0,
  4498. IPPROTO_TCP,
  4499. 0);
  4500. if (tcp_opt_len || (iph->ihl > 5)) {
  4501. vlan_tag_flags |= ((iph->ihl - 5) +
  4502. (tcp_opt_len >> 2)) << 8;
  4503. }
  4504. }
  4505. } else
  4506. mss = 0;
  4507. mapping = pci_map_single(bp->pdev, skb->data, len, PCI_DMA_TODEVICE);
  4508. tx_buf = &bp->tx_buf_ring[ring_prod];
  4509. tx_buf->skb = skb;
  4510. pci_unmap_addr_set(tx_buf, mapping, mapping);
  4511. txbd = &bp->tx_desc_ring[ring_prod];
  4512. txbd->tx_bd_haddr_hi = (u64) mapping >> 32;
  4513. txbd->tx_bd_haddr_lo = (u64) mapping & 0xffffffff;
  4514. txbd->tx_bd_mss_nbytes = len | (mss << 16);
  4515. txbd->tx_bd_vlan_tag_flags = vlan_tag_flags | TX_BD_FLAGS_START;
  4516. last_frag = skb_shinfo(skb)->nr_frags;
  4517. for (i = 0; i < last_frag; i++) {
  4518. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  4519. prod = NEXT_TX_BD(prod);
  4520. ring_prod = TX_RING_IDX(prod);
  4521. txbd = &bp->tx_desc_ring[ring_prod];
  4522. len = frag->size;
  4523. mapping = pci_map_page(bp->pdev, frag->page, frag->page_offset,
  4524. len, PCI_DMA_TODEVICE);
  4525. pci_unmap_addr_set(&bp->tx_buf_ring[ring_prod],
  4526. mapping, mapping);
  4527. txbd->tx_bd_haddr_hi = (u64) mapping >> 32;
  4528. txbd->tx_bd_haddr_lo = (u64) mapping & 0xffffffff;
  4529. txbd->tx_bd_mss_nbytes = len | (mss << 16);
  4530. txbd->tx_bd_vlan_tag_flags = vlan_tag_flags;
  4531. }
  4532. txbd->tx_bd_vlan_tag_flags |= TX_BD_FLAGS_END;
  4533. prod = NEXT_TX_BD(prod);
  4534. bp->tx_prod_bseq += skb->len;
  4535. REG_WR16(bp, bp->tx_bidx_addr, prod);
  4536. REG_WR(bp, bp->tx_bseq_addr, bp->tx_prod_bseq);
  4537. mmiowb();
  4538. bp->tx_prod = prod;
  4539. dev->trans_start = jiffies;
  4540. if (unlikely(bnx2_tx_avail(bp, bnapi) <= MAX_SKB_FRAGS)) {
  4541. netif_stop_queue(dev);
  4542. if (bnx2_tx_avail(bp, bnapi) > bp->tx_wake_thresh)
  4543. netif_wake_queue(dev);
  4544. }
  4545. return NETDEV_TX_OK;
  4546. }
  4547. /* Called with rtnl_lock */
  4548. static int
  4549. bnx2_close(struct net_device *dev)
  4550. {
  4551. struct bnx2 *bp = netdev_priv(dev);
  4552. u32 reset_code;
  4553. /* Calling flush_scheduled_work() may deadlock because
  4554. * linkwatch_event() may be on the workqueue and it will try to get
  4555. * the rtnl_lock which we are holding.
  4556. */
  4557. while (bp->in_reset_task)
  4558. msleep(1);
  4559. bnx2_disable_int_sync(bp);
  4560. bnx2_napi_disable(bp);
  4561. del_timer_sync(&bp->timer);
  4562. if (bp->flags & NO_WOL_FLAG)
  4563. reset_code = BNX2_DRV_MSG_CODE_UNLOAD_LNK_DN;
  4564. else if (bp->wol)
  4565. reset_code = BNX2_DRV_MSG_CODE_SUSPEND_WOL;
  4566. else
  4567. reset_code = BNX2_DRV_MSG_CODE_SUSPEND_NO_WOL;
  4568. bnx2_reset_chip(bp, reset_code);
  4569. bnx2_free_irq(bp);
  4570. bnx2_free_skbs(bp);
  4571. bnx2_free_mem(bp);
  4572. bp->link_up = 0;
  4573. netif_carrier_off(bp->dev);
  4574. bnx2_set_power_state(bp, PCI_D3hot);
  4575. return 0;
  4576. }
  4577. #define GET_NET_STATS64(ctr) \
  4578. (unsigned long) ((unsigned long) (ctr##_hi) << 32) + \
  4579. (unsigned long) (ctr##_lo)
  4580. #define GET_NET_STATS32(ctr) \
  4581. (ctr##_lo)
  4582. #if (BITS_PER_LONG == 64)
  4583. #define GET_NET_STATS GET_NET_STATS64
  4584. #else
  4585. #define GET_NET_STATS GET_NET_STATS32
  4586. #endif
  4587. static struct net_device_stats *
  4588. bnx2_get_stats(struct net_device *dev)
  4589. {
  4590. struct bnx2 *bp = netdev_priv(dev);
  4591. struct statistics_block *stats_blk = bp->stats_blk;
  4592. struct net_device_stats *net_stats = &bp->net_stats;
  4593. if (bp->stats_blk == NULL) {
  4594. return net_stats;
  4595. }
  4596. net_stats->rx_packets =
  4597. GET_NET_STATS(stats_blk->stat_IfHCInUcastPkts) +
  4598. GET_NET_STATS(stats_blk->stat_IfHCInMulticastPkts) +
  4599. GET_NET_STATS(stats_blk->stat_IfHCInBroadcastPkts);
  4600. net_stats->tx_packets =
  4601. GET_NET_STATS(stats_blk->stat_IfHCOutUcastPkts) +
  4602. GET_NET_STATS(stats_blk->stat_IfHCOutMulticastPkts) +
  4603. GET_NET_STATS(stats_blk->stat_IfHCOutBroadcastPkts);
  4604. net_stats->rx_bytes =
  4605. GET_NET_STATS(stats_blk->stat_IfHCInOctets);
  4606. net_stats->tx_bytes =
  4607. GET_NET_STATS(stats_blk->stat_IfHCOutOctets);
  4608. net_stats->multicast =
  4609. GET_NET_STATS(stats_blk->stat_IfHCOutMulticastPkts);
  4610. net_stats->collisions =
  4611. (unsigned long) stats_blk->stat_EtherStatsCollisions;
  4612. net_stats->rx_length_errors =
  4613. (unsigned long) (stats_blk->stat_EtherStatsUndersizePkts +
  4614. stats_blk->stat_EtherStatsOverrsizePkts);
  4615. net_stats->rx_over_errors =
  4616. (unsigned long) stats_blk->stat_IfInMBUFDiscards;
  4617. net_stats->rx_frame_errors =
  4618. (unsigned long) stats_blk->stat_Dot3StatsAlignmentErrors;
  4619. net_stats->rx_crc_errors =
  4620. (unsigned long) stats_blk->stat_Dot3StatsFCSErrors;
  4621. net_stats->rx_errors = net_stats->rx_length_errors +
  4622. net_stats->rx_over_errors + net_stats->rx_frame_errors +
  4623. net_stats->rx_crc_errors;
  4624. net_stats->tx_aborted_errors =
  4625. (unsigned long) (stats_blk->stat_Dot3StatsExcessiveCollisions +
  4626. stats_blk->stat_Dot3StatsLateCollisions);
  4627. if ((CHIP_NUM(bp) == CHIP_NUM_5706) ||
  4628. (CHIP_ID(bp) == CHIP_ID_5708_A0))
  4629. net_stats->tx_carrier_errors = 0;
  4630. else {
  4631. net_stats->tx_carrier_errors =
  4632. (unsigned long)
  4633. stats_blk->stat_Dot3StatsCarrierSenseErrors;
  4634. }
  4635. net_stats->tx_errors =
  4636. (unsigned long)
  4637. stats_blk->stat_emac_tx_stat_dot3statsinternalmactransmiterrors
  4638. +
  4639. net_stats->tx_aborted_errors +
  4640. net_stats->tx_carrier_errors;
  4641. net_stats->rx_missed_errors =
  4642. (unsigned long) (stats_blk->stat_IfInMBUFDiscards +
  4643. stats_blk->stat_FwRxDrop);
  4644. return net_stats;
  4645. }
  4646. /* All ethtool functions called with rtnl_lock */
  4647. static int
  4648. bnx2_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  4649. {
  4650. struct bnx2 *bp = netdev_priv(dev);
  4651. int support_serdes = 0, support_copper = 0;
  4652. cmd->supported = SUPPORTED_Autoneg;
  4653. if (bp->phy_flags & REMOTE_PHY_CAP_FLAG) {
  4654. support_serdes = 1;
  4655. support_copper = 1;
  4656. } else if (bp->phy_port == PORT_FIBRE)
  4657. support_serdes = 1;
  4658. else
  4659. support_copper = 1;
  4660. if (support_serdes) {
  4661. cmd->supported |= SUPPORTED_1000baseT_Full |
  4662. SUPPORTED_FIBRE;
  4663. if (bp->phy_flags & PHY_2_5G_CAPABLE_FLAG)
  4664. cmd->supported |= SUPPORTED_2500baseX_Full;
  4665. }
  4666. if (support_copper) {
  4667. cmd->supported |= SUPPORTED_10baseT_Half |
  4668. SUPPORTED_10baseT_Full |
  4669. SUPPORTED_100baseT_Half |
  4670. SUPPORTED_100baseT_Full |
  4671. SUPPORTED_1000baseT_Full |
  4672. SUPPORTED_TP;
  4673. }
  4674. spin_lock_bh(&bp->phy_lock);
  4675. cmd->port = bp->phy_port;
  4676. cmd->advertising = bp->advertising;
  4677. if (bp->autoneg & AUTONEG_SPEED) {
  4678. cmd->autoneg = AUTONEG_ENABLE;
  4679. }
  4680. else {
  4681. cmd->autoneg = AUTONEG_DISABLE;
  4682. }
  4683. if (netif_carrier_ok(dev)) {
  4684. cmd->speed = bp->line_speed;
  4685. cmd->duplex = bp->duplex;
  4686. }
  4687. else {
  4688. cmd->speed = -1;
  4689. cmd->duplex = -1;
  4690. }
  4691. spin_unlock_bh(&bp->phy_lock);
  4692. cmd->transceiver = XCVR_INTERNAL;
  4693. cmd->phy_address = bp->phy_addr;
  4694. return 0;
  4695. }
  4696. static int
  4697. bnx2_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  4698. {
  4699. struct bnx2 *bp = netdev_priv(dev);
  4700. u8 autoneg = bp->autoneg;
  4701. u8 req_duplex = bp->req_duplex;
  4702. u16 req_line_speed = bp->req_line_speed;
  4703. u32 advertising = bp->advertising;
  4704. int err = -EINVAL;
  4705. spin_lock_bh(&bp->phy_lock);
  4706. if (cmd->port != PORT_TP && cmd->port != PORT_FIBRE)
  4707. goto err_out_unlock;
  4708. if (cmd->port != bp->phy_port && !(bp->phy_flags & REMOTE_PHY_CAP_FLAG))
  4709. goto err_out_unlock;
  4710. if (cmd->autoneg == AUTONEG_ENABLE) {
  4711. autoneg |= AUTONEG_SPEED;
  4712. cmd->advertising &= ETHTOOL_ALL_COPPER_SPEED;
  4713. /* allow advertising 1 speed */
  4714. if ((cmd->advertising == ADVERTISED_10baseT_Half) ||
  4715. (cmd->advertising == ADVERTISED_10baseT_Full) ||
  4716. (cmd->advertising == ADVERTISED_100baseT_Half) ||
  4717. (cmd->advertising == ADVERTISED_100baseT_Full)) {
  4718. if (cmd->port == PORT_FIBRE)
  4719. goto err_out_unlock;
  4720. advertising = cmd->advertising;
  4721. } else if (cmd->advertising == ADVERTISED_2500baseX_Full) {
  4722. if (!(bp->phy_flags & PHY_2_5G_CAPABLE_FLAG) ||
  4723. (cmd->port == PORT_TP))
  4724. goto err_out_unlock;
  4725. } else if (cmd->advertising == ADVERTISED_1000baseT_Full)
  4726. advertising = cmd->advertising;
  4727. else if (cmd->advertising == ADVERTISED_1000baseT_Half)
  4728. goto err_out_unlock;
  4729. else {
  4730. if (cmd->port == PORT_FIBRE)
  4731. advertising = ETHTOOL_ALL_FIBRE_SPEED;
  4732. else
  4733. advertising = ETHTOOL_ALL_COPPER_SPEED;
  4734. }
  4735. advertising |= ADVERTISED_Autoneg;
  4736. }
  4737. else {
  4738. if (cmd->port == PORT_FIBRE) {
  4739. if ((cmd->speed != SPEED_1000 &&
  4740. cmd->speed != SPEED_2500) ||
  4741. (cmd->duplex != DUPLEX_FULL))
  4742. goto err_out_unlock;
  4743. if (cmd->speed == SPEED_2500 &&
  4744. !(bp->phy_flags & PHY_2_5G_CAPABLE_FLAG))
  4745. goto err_out_unlock;
  4746. }
  4747. else if (cmd->speed == SPEED_1000 || cmd->speed == SPEED_2500)
  4748. goto err_out_unlock;
  4749. autoneg &= ~AUTONEG_SPEED;
  4750. req_line_speed = cmd->speed;
  4751. req_duplex = cmd->duplex;
  4752. advertising = 0;
  4753. }
  4754. bp->autoneg = autoneg;
  4755. bp->advertising = advertising;
  4756. bp->req_line_speed = req_line_speed;
  4757. bp->req_duplex = req_duplex;
  4758. err = bnx2_setup_phy(bp, cmd->port);
  4759. err_out_unlock:
  4760. spin_unlock_bh(&bp->phy_lock);
  4761. return err;
  4762. }
  4763. static void
  4764. bnx2_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  4765. {
  4766. struct bnx2 *bp = netdev_priv(dev);
  4767. strcpy(info->driver, DRV_MODULE_NAME);
  4768. strcpy(info->version, DRV_MODULE_VERSION);
  4769. strcpy(info->bus_info, pci_name(bp->pdev));
  4770. strcpy(info->fw_version, bp->fw_version);
  4771. }
  4772. #define BNX2_REGDUMP_LEN (32 * 1024)
  4773. static int
  4774. bnx2_get_regs_len(struct net_device *dev)
  4775. {
  4776. return BNX2_REGDUMP_LEN;
  4777. }
  4778. static void
  4779. bnx2_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
  4780. {
  4781. u32 *p = _p, i, offset;
  4782. u8 *orig_p = _p;
  4783. struct bnx2 *bp = netdev_priv(dev);
  4784. u32 reg_boundaries[] = { 0x0000, 0x0098, 0x0400, 0x045c,
  4785. 0x0800, 0x0880, 0x0c00, 0x0c10,
  4786. 0x0c30, 0x0d08, 0x1000, 0x101c,
  4787. 0x1040, 0x1048, 0x1080, 0x10a4,
  4788. 0x1400, 0x1490, 0x1498, 0x14f0,
  4789. 0x1500, 0x155c, 0x1580, 0x15dc,
  4790. 0x1600, 0x1658, 0x1680, 0x16d8,
  4791. 0x1800, 0x1820, 0x1840, 0x1854,
  4792. 0x1880, 0x1894, 0x1900, 0x1984,
  4793. 0x1c00, 0x1c0c, 0x1c40, 0x1c54,
  4794. 0x1c80, 0x1c94, 0x1d00, 0x1d84,
  4795. 0x2000, 0x2030, 0x23c0, 0x2400,
  4796. 0x2800, 0x2820, 0x2830, 0x2850,
  4797. 0x2b40, 0x2c10, 0x2fc0, 0x3058,
  4798. 0x3c00, 0x3c94, 0x4000, 0x4010,
  4799. 0x4080, 0x4090, 0x43c0, 0x4458,
  4800. 0x4c00, 0x4c18, 0x4c40, 0x4c54,
  4801. 0x4fc0, 0x5010, 0x53c0, 0x5444,
  4802. 0x5c00, 0x5c18, 0x5c80, 0x5c90,
  4803. 0x5fc0, 0x6000, 0x6400, 0x6428,
  4804. 0x6800, 0x6848, 0x684c, 0x6860,
  4805. 0x6888, 0x6910, 0x8000 };
  4806. regs->version = 0;
  4807. memset(p, 0, BNX2_REGDUMP_LEN);
  4808. if (!netif_running(bp->dev))
  4809. return;
  4810. i = 0;
  4811. offset = reg_boundaries[0];
  4812. p += offset;
  4813. while (offset < BNX2_REGDUMP_LEN) {
  4814. *p++ = REG_RD(bp, offset);
  4815. offset += 4;
  4816. if (offset == reg_boundaries[i + 1]) {
  4817. offset = reg_boundaries[i + 2];
  4818. p = (u32 *) (orig_p + offset);
  4819. i += 2;
  4820. }
  4821. }
  4822. }
  4823. static void
  4824. bnx2_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  4825. {
  4826. struct bnx2 *bp = netdev_priv(dev);
  4827. if (bp->flags & NO_WOL_FLAG) {
  4828. wol->supported = 0;
  4829. wol->wolopts = 0;
  4830. }
  4831. else {
  4832. wol->supported = WAKE_MAGIC;
  4833. if (bp->wol)
  4834. wol->wolopts = WAKE_MAGIC;
  4835. else
  4836. wol->wolopts = 0;
  4837. }
  4838. memset(&wol->sopass, 0, sizeof(wol->sopass));
  4839. }
  4840. static int
  4841. bnx2_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  4842. {
  4843. struct bnx2 *bp = netdev_priv(dev);
  4844. if (wol->wolopts & ~WAKE_MAGIC)
  4845. return -EINVAL;
  4846. if (wol->wolopts & WAKE_MAGIC) {
  4847. if (bp->flags & NO_WOL_FLAG)
  4848. return -EINVAL;
  4849. bp->wol = 1;
  4850. }
  4851. else {
  4852. bp->wol = 0;
  4853. }
  4854. return 0;
  4855. }
  4856. static int
  4857. bnx2_nway_reset(struct net_device *dev)
  4858. {
  4859. struct bnx2 *bp = netdev_priv(dev);
  4860. u32 bmcr;
  4861. if (!(bp->autoneg & AUTONEG_SPEED)) {
  4862. return -EINVAL;
  4863. }
  4864. spin_lock_bh(&bp->phy_lock);
  4865. if (bp->phy_flags & REMOTE_PHY_CAP_FLAG) {
  4866. int rc;
  4867. rc = bnx2_setup_remote_phy(bp, bp->phy_port);
  4868. spin_unlock_bh(&bp->phy_lock);
  4869. return rc;
  4870. }
  4871. /* Force a link down visible on the other side */
  4872. if (bp->phy_flags & PHY_SERDES_FLAG) {
  4873. bnx2_write_phy(bp, bp->mii_bmcr, BMCR_LOOPBACK);
  4874. spin_unlock_bh(&bp->phy_lock);
  4875. msleep(20);
  4876. spin_lock_bh(&bp->phy_lock);
  4877. bp->current_interval = SERDES_AN_TIMEOUT;
  4878. bp->serdes_an_pending = 1;
  4879. mod_timer(&bp->timer, jiffies + bp->current_interval);
  4880. }
  4881. bnx2_read_phy(bp, bp->mii_bmcr, &bmcr);
  4882. bmcr &= ~BMCR_LOOPBACK;
  4883. bnx2_write_phy(bp, bp->mii_bmcr, bmcr | BMCR_ANRESTART | BMCR_ANENABLE);
  4884. spin_unlock_bh(&bp->phy_lock);
  4885. return 0;
  4886. }
  4887. static int
  4888. bnx2_get_eeprom_len(struct net_device *dev)
  4889. {
  4890. struct bnx2 *bp = netdev_priv(dev);
  4891. if (bp->flash_info == NULL)
  4892. return 0;
  4893. return (int) bp->flash_size;
  4894. }
  4895. static int
  4896. bnx2_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom,
  4897. u8 *eebuf)
  4898. {
  4899. struct bnx2 *bp = netdev_priv(dev);
  4900. int rc;
  4901. /* parameters already validated in ethtool_get_eeprom */
  4902. rc = bnx2_nvram_read(bp, eeprom->offset, eebuf, eeprom->len);
  4903. return rc;
  4904. }
  4905. static int
  4906. bnx2_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom,
  4907. u8 *eebuf)
  4908. {
  4909. struct bnx2 *bp = netdev_priv(dev);
  4910. int rc;
  4911. /* parameters already validated in ethtool_set_eeprom */
  4912. rc = bnx2_nvram_write(bp, eeprom->offset, eebuf, eeprom->len);
  4913. return rc;
  4914. }
  4915. static int
  4916. bnx2_get_coalesce(struct net_device *dev, struct ethtool_coalesce *coal)
  4917. {
  4918. struct bnx2 *bp = netdev_priv(dev);
  4919. memset(coal, 0, sizeof(struct ethtool_coalesce));
  4920. coal->rx_coalesce_usecs = bp->rx_ticks;
  4921. coal->rx_max_coalesced_frames = bp->rx_quick_cons_trip;
  4922. coal->rx_coalesce_usecs_irq = bp->rx_ticks_int;
  4923. coal->rx_max_coalesced_frames_irq = bp->rx_quick_cons_trip_int;
  4924. coal->tx_coalesce_usecs = bp->tx_ticks;
  4925. coal->tx_max_coalesced_frames = bp->tx_quick_cons_trip;
  4926. coal->tx_coalesce_usecs_irq = bp->tx_ticks_int;
  4927. coal->tx_max_coalesced_frames_irq = bp->tx_quick_cons_trip_int;
  4928. coal->stats_block_coalesce_usecs = bp->stats_ticks;
  4929. return 0;
  4930. }
  4931. static int
  4932. bnx2_set_coalesce(struct net_device *dev, struct ethtool_coalesce *coal)
  4933. {
  4934. struct bnx2 *bp = netdev_priv(dev);
  4935. bp->rx_ticks = (u16) coal->rx_coalesce_usecs;
  4936. if (bp->rx_ticks > 0x3ff) bp->rx_ticks = 0x3ff;
  4937. bp->rx_quick_cons_trip = (u16) coal->rx_max_coalesced_frames;
  4938. if (bp->rx_quick_cons_trip > 0xff) bp->rx_quick_cons_trip = 0xff;
  4939. bp->rx_ticks_int = (u16) coal->rx_coalesce_usecs_irq;
  4940. if (bp->rx_ticks_int > 0x3ff) bp->rx_ticks_int = 0x3ff;
  4941. bp->rx_quick_cons_trip_int = (u16) coal->rx_max_coalesced_frames_irq;
  4942. if (bp->rx_quick_cons_trip_int > 0xff)
  4943. bp->rx_quick_cons_trip_int = 0xff;
  4944. bp->tx_ticks = (u16) coal->tx_coalesce_usecs;
  4945. if (bp->tx_ticks > 0x3ff) bp->tx_ticks = 0x3ff;
  4946. bp->tx_quick_cons_trip = (u16) coal->tx_max_coalesced_frames;
  4947. if (bp->tx_quick_cons_trip > 0xff) bp->tx_quick_cons_trip = 0xff;
  4948. bp->tx_ticks_int = (u16) coal->tx_coalesce_usecs_irq;
  4949. if (bp->tx_ticks_int > 0x3ff) bp->tx_ticks_int = 0x3ff;
  4950. bp->tx_quick_cons_trip_int = (u16) coal->tx_max_coalesced_frames_irq;
  4951. if (bp->tx_quick_cons_trip_int > 0xff) bp->tx_quick_cons_trip_int =
  4952. 0xff;
  4953. bp->stats_ticks = coal->stats_block_coalesce_usecs;
  4954. if (CHIP_NUM(bp) == CHIP_NUM_5708) {
  4955. if (bp->stats_ticks != 0 && bp->stats_ticks != USEC_PER_SEC)
  4956. bp->stats_ticks = USEC_PER_SEC;
  4957. }
  4958. if (bp->stats_ticks > BNX2_HC_STATS_TICKS_HC_STAT_TICKS)
  4959. bp->stats_ticks = BNX2_HC_STATS_TICKS_HC_STAT_TICKS;
  4960. bp->stats_ticks &= BNX2_HC_STATS_TICKS_HC_STAT_TICKS;
  4961. if (netif_running(bp->dev)) {
  4962. bnx2_netif_stop(bp);
  4963. bnx2_init_nic(bp);
  4964. bnx2_netif_start(bp);
  4965. }
  4966. return 0;
  4967. }
  4968. static void
  4969. bnx2_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
  4970. {
  4971. struct bnx2 *bp = netdev_priv(dev);
  4972. ering->rx_max_pending = MAX_TOTAL_RX_DESC_CNT;
  4973. ering->rx_mini_max_pending = 0;
  4974. ering->rx_jumbo_max_pending = MAX_TOTAL_RX_PG_DESC_CNT;
  4975. ering->rx_pending = bp->rx_ring_size;
  4976. ering->rx_mini_pending = 0;
  4977. ering->rx_jumbo_pending = bp->rx_pg_ring_size;
  4978. ering->tx_max_pending = MAX_TX_DESC_CNT;
  4979. ering->tx_pending = bp->tx_ring_size;
  4980. }
  4981. static int
  4982. bnx2_change_ring_size(struct bnx2 *bp, u32 rx, u32 tx)
  4983. {
  4984. if (netif_running(bp->dev)) {
  4985. bnx2_netif_stop(bp);
  4986. bnx2_reset_chip(bp, BNX2_DRV_MSG_CODE_RESET);
  4987. bnx2_free_skbs(bp);
  4988. bnx2_free_mem(bp);
  4989. }
  4990. bnx2_set_rx_ring_size(bp, rx);
  4991. bp->tx_ring_size = tx;
  4992. if (netif_running(bp->dev)) {
  4993. int rc;
  4994. rc = bnx2_alloc_mem(bp);
  4995. if (rc)
  4996. return rc;
  4997. bnx2_init_nic(bp);
  4998. bnx2_netif_start(bp);
  4999. }
  5000. return 0;
  5001. }
  5002. static int
  5003. bnx2_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
  5004. {
  5005. struct bnx2 *bp = netdev_priv(dev);
  5006. int rc;
  5007. if ((ering->rx_pending > MAX_TOTAL_RX_DESC_CNT) ||
  5008. (ering->tx_pending > MAX_TX_DESC_CNT) ||
  5009. (ering->tx_pending <= MAX_SKB_FRAGS)) {
  5010. return -EINVAL;
  5011. }
  5012. rc = bnx2_change_ring_size(bp, ering->rx_pending, ering->tx_pending);
  5013. return rc;
  5014. }
  5015. static void
  5016. bnx2_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
  5017. {
  5018. struct bnx2 *bp = netdev_priv(dev);
  5019. epause->autoneg = ((bp->autoneg & AUTONEG_FLOW_CTRL) != 0);
  5020. epause->rx_pause = ((bp->flow_ctrl & FLOW_CTRL_RX) != 0);
  5021. epause->tx_pause = ((bp->flow_ctrl & FLOW_CTRL_TX) != 0);
  5022. }
  5023. static int
  5024. bnx2_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
  5025. {
  5026. struct bnx2 *bp = netdev_priv(dev);
  5027. bp->req_flow_ctrl = 0;
  5028. if (epause->rx_pause)
  5029. bp->req_flow_ctrl |= FLOW_CTRL_RX;
  5030. if (epause->tx_pause)
  5031. bp->req_flow_ctrl |= FLOW_CTRL_TX;
  5032. if (epause->autoneg) {
  5033. bp->autoneg |= AUTONEG_FLOW_CTRL;
  5034. }
  5035. else {
  5036. bp->autoneg &= ~AUTONEG_FLOW_CTRL;
  5037. }
  5038. spin_lock_bh(&bp->phy_lock);
  5039. bnx2_setup_phy(bp, bp->phy_port);
  5040. spin_unlock_bh(&bp->phy_lock);
  5041. return 0;
  5042. }
  5043. static u32
  5044. bnx2_get_rx_csum(struct net_device *dev)
  5045. {
  5046. struct bnx2 *bp = netdev_priv(dev);
  5047. return bp->rx_csum;
  5048. }
  5049. static int
  5050. bnx2_set_rx_csum(struct net_device *dev, u32 data)
  5051. {
  5052. struct bnx2 *bp = netdev_priv(dev);
  5053. bp->rx_csum = data;
  5054. return 0;
  5055. }
  5056. static int
  5057. bnx2_set_tso(struct net_device *dev, u32 data)
  5058. {
  5059. struct bnx2 *bp = netdev_priv(dev);
  5060. if (data) {
  5061. dev->features |= NETIF_F_TSO | NETIF_F_TSO_ECN;
  5062. if (CHIP_NUM(bp) == CHIP_NUM_5709)
  5063. dev->features |= NETIF_F_TSO6;
  5064. } else
  5065. dev->features &= ~(NETIF_F_TSO | NETIF_F_TSO6 |
  5066. NETIF_F_TSO_ECN);
  5067. return 0;
  5068. }
  5069. #define BNX2_NUM_STATS 46
  5070. static struct {
  5071. char string[ETH_GSTRING_LEN];
  5072. } bnx2_stats_str_arr[BNX2_NUM_STATS] = {
  5073. { "rx_bytes" },
  5074. { "rx_error_bytes" },
  5075. { "tx_bytes" },
  5076. { "tx_error_bytes" },
  5077. { "rx_ucast_packets" },
  5078. { "rx_mcast_packets" },
  5079. { "rx_bcast_packets" },
  5080. { "tx_ucast_packets" },
  5081. { "tx_mcast_packets" },
  5082. { "tx_bcast_packets" },
  5083. { "tx_mac_errors" },
  5084. { "tx_carrier_errors" },
  5085. { "rx_crc_errors" },
  5086. { "rx_align_errors" },
  5087. { "tx_single_collisions" },
  5088. { "tx_multi_collisions" },
  5089. { "tx_deferred" },
  5090. { "tx_excess_collisions" },
  5091. { "tx_late_collisions" },
  5092. { "tx_total_collisions" },
  5093. { "rx_fragments" },
  5094. { "rx_jabbers" },
  5095. { "rx_undersize_packets" },
  5096. { "rx_oversize_packets" },
  5097. { "rx_64_byte_packets" },
  5098. { "rx_65_to_127_byte_packets" },
  5099. { "rx_128_to_255_byte_packets" },
  5100. { "rx_256_to_511_byte_packets" },
  5101. { "rx_512_to_1023_byte_packets" },
  5102. { "rx_1024_to_1522_byte_packets" },
  5103. { "rx_1523_to_9022_byte_packets" },
  5104. { "tx_64_byte_packets" },
  5105. { "tx_65_to_127_byte_packets" },
  5106. { "tx_128_to_255_byte_packets" },
  5107. { "tx_256_to_511_byte_packets" },
  5108. { "tx_512_to_1023_byte_packets" },
  5109. { "tx_1024_to_1522_byte_packets" },
  5110. { "tx_1523_to_9022_byte_packets" },
  5111. { "rx_xon_frames" },
  5112. { "rx_xoff_frames" },
  5113. { "tx_xon_frames" },
  5114. { "tx_xoff_frames" },
  5115. { "rx_mac_ctrl_frames" },
  5116. { "rx_filtered_packets" },
  5117. { "rx_discards" },
  5118. { "rx_fw_discards" },
  5119. };
  5120. #define STATS_OFFSET32(offset_name) (offsetof(struct statistics_block, offset_name) / 4)
  5121. static const unsigned long bnx2_stats_offset_arr[BNX2_NUM_STATS] = {
  5122. STATS_OFFSET32(stat_IfHCInOctets_hi),
  5123. STATS_OFFSET32(stat_IfHCInBadOctets_hi),
  5124. STATS_OFFSET32(stat_IfHCOutOctets_hi),
  5125. STATS_OFFSET32(stat_IfHCOutBadOctets_hi),
  5126. STATS_OFFSET32(stat_IfHCInUcastPkts_hi),
  5127. STATS_OFFSET32(stat_IfHCInMulticastPkts_hi),
  5128. STATS_OFFSET32(stat_IfHCInBroadcastPkts_hi),
  5129. STATS_OFFSET32(stat_IfHCOutUcastPkts_hi),
  5130. STATS_OFFSET32(stat_IfHCOutMulticastPkts_hi),
  5131. STATS_OFFSET32(stat_IfHCOutBroadcastPkts_hi),
  5132. STATS_OFFSET32(stat_emac_tx_stat_dot3statsinternalmactransmiterrors),
  5133. STATS_OFFSET32(stat_Dot3StatsCarrierSenseErrors),
  5134. STATS_OFFSET32(stat_Dot3StatsFCSErrors),
  5135. STATS_OFFSET32(stat_Dot3StatsAlignmentErrors),
  5136. STATS_OFFSET32(stat_Dot3StatsSingleCollisionFrames),
  5137. STATS_OFFSET32(stat_Dot3StatsMultipleCollisionFrames),
  5138. STATS_OFFSET32(stat_Dot3StatsDeferredTransmissions),
  5139. STATS_OFFSET32(stat_Dot3StatsExcessiveCollisions),
  5140. STATS_OFFSET32(stat_Dot3StatsLateCollisions),
  5141. STATS_OFFSET32(stat_EtherStatsCollisions),
  5142. STATS_OFFSET32(stat_EtherStatsFragments),
  5143. STATS_OFFSET32(stat_EtherStatsJabbers),
  5144. STATS_OFFSET32(stat_EtherStatsUndersizePkts),
  5145. STATS_OFFSET32(stat_EtherStatsOverrsizePkts),
  5146. STATS_OFFSET32(stat_EtherStatsPktsRx64Octets),
  5147. STATS_OFFSET32(stat_EtherStatsPktsRx65Octetsto127Octets),
  5148. STATS_OFFSET32(stat_EtherStatsPktsRx128Octetsto255Octets),
  5149. STATS_OFFSET32(stat_EtherStatsPktsRx256Octetsto511Octets),
  5150. STATS_OFFSET32(stat_EtherStatsPktsRx512Octetsto1023Octets),
  5151. STATS_OFFSET32(stat_EtherStatsPktsRx1024Octetsto1522Octets),
  5152. STATS_OFFSET32(stat_EtherStatsPktsRx1523Octetsto9022Octets),
  5153. STATS_OFFSET32(stat_EtherStatsPktsTx64Octets),
  5154. STATS_OFFSET32(stat_EtherStatsPktsTx65Octetsto127Octets),
  5155. STATS_OFFSET32(stat_EtherStatsPktsTx128Octetsto255Octets),
  5156. STATS_OFFSET32(stat_EtherStatsPktsTx256Octetsto511Octets),
  5157. STATS_OFFSET32(stat_EtherStatsPktsTx512Octetsto1023Octets),
  5158. STATS_OFFSET32(stat_EtherStatsPktsTx1024Octetsto1522Octets),
  5159. STATS_OFFSET32(stat_EtherStatsPktsTx1523Octetsto9022Octets),
  5160. STATS_OFFSET32(stat_XonPauseFramesReceived),
  5161. STATS_OFFSET32(stat_XoffPauseFramesReceived),
  5162. STATS_OFFSET32(stat_OutXonSent),
  5163. STATS_OFFSET32(stat_OutXoffSent),
  5164. STATS_OFFSET32(stat_MacControlFramesReceived),
  5165. STATS_OFFSET32(stat_IfInFramesL2FilterDiscards),
  5166. STATS_OFFSET32(stat_IfInMBUFDiscards),
  5167. STATS_OFFSET32(stat_FwRxDrop),
  5168. };
  5169. /* stat_IfHCInBadOctets and stat_Dot3StatsCarrierSenseErrors are
  5170. * skipped because of errata.
  5171. */
  5172. static u8 bnx2_5706_stats_len_arr[BNX2_NUM_STATS] = {
  5173. 8,0,8,8,8,8,8,8,8,8,
  5174. 4,0,4,4,4,4,4,4,4,4,
  5175. 4,4,4,4,4,4,4,4,4,4,
  5176. 4,4,4,4,4,4,4,4,4,4,
  5177. 4,4,4,4,4,4,
  5178. };
  5179. static u8 bnx2_5708_stats_len_arr[BNX2_NUM_STATS] = {
  5180. 8,0,8,8,8,8,8,8,8,8,
  5181. 4,4,4,4,4,4,4,4,4,4,
  5182. 4,4,4,4,4,4,4,4,4,4,
  5183. 4,4,4,4,4,4,4,4,4,4,
  5184. 4,4,4,4,4,4,
  5185. };
  5186. #define BNX2_NUM_TESTS 6
  5187. static struct {
  5188. char string[ETH_GSTRING_LEN];
  5189. } bnx2_tests_str_arr[BNX2_NUM_TESTS] = {
  5190. { "register_test (offline)" },
  5191. { "memory_test (offline)" },
  5192. { "loopback_test (offline)" },
  5193. { "nvram_test (online)" },
  5194. { "interrupt_test (online)" },
  5195. { "link_test (online)" },
  5196. };
  5197. static int
  5198. bnx2_get_sset_count(struct net_device *dev, int sset)
  5199. {
  5200. switch (sset) {
  5201. case ETH_SS_TEST:
  5202. return BNX2_NUM_TESTS;
  5203. case ETH_SS_STATS:
  5204. return BNX2_NUM_STATS;
  5205. default:
  5206. return -EOPNOTSUPP;
  5207. }
  5208. }
  5209. static void
  5210. bnx2_self_test(struct net_device *dev, struct ethtool_test *etest, u64 *buf)
  5211. {
  5212. struct bnx2 *bp = netdev_priv(dev);
  5213. memset(buf, 0, sizeof(u64) * BNX2_NUM_TESTS);
  5214. if (etest->flags & ETH_TEST_FL_OFFLINE) {
  5215. int i;
  5216. bnx2_netif_stop(bp);
  5217. bnx2_reset_chip(bp, BNX2_DRV_MSG_CODE_DIAG);
  5218. bnx2_free_skbs(bp);
  5219. if (bnx2_test_registers(bp) != 0) {
  5220. buf[0] = 1;
  5221. etest->flags |= ETH_TEST_FL_FAILED;
  5222. }
  5223. if (bnx2_test_memory(bp) != 0) {
  5224. buf[1] = 1;
  5225. etest->flags |= ETH_TEST_FL_FAILED;
  5226. }
  5227. if ((buf[2] = bnx2_test_loopback(bp)) != 0)
  5228. etest->flags |= ETH_TEST_FL_FAILED;
  5229. if (!netif_running(bp->dev)) {
  5230. bnx2_reset_chip(bp, BNX2_DRV_MSG_CODE_RESET);
  5231. }
  5232. else {
  5233. bnx2_init_nic(bp);
  5234. bnx2_netif_start(bp);
  5235. }
  5236. /* wait for link up */
  5237. for (i = 0; i < 7; i++) {
  5238. if (bp->link_up)
  5239. break;
  5240. msleep_interruptible(1000);
  5241. }
  5242. }
  5243. if (bnx2_test_nvram(bp) != 0) {
  5244. buf[3] = 1;
  5245. etest->flags |= ETH_TEST_FL_FAILED;
  5246. }
  5247. if (bnx2_test_intr(bp) != 0) {
  5248. buf[4] = 1;
  5249. etest->flags |= ETH_TEST_FL_FAILED;
  5250. }
  5251. if (bnx2_test_link(bp) != 0) {
  5252. buf[5] = 1;
  5253. etest->flags |= ETH_TEST_FL_FAILED;
  5254. }
  5255. }
  5256. static void
  5257. bnx2_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
  5258. {
  5259. switch (stringset) {
  5260. case ETH_SS_STATS:
  5261. memcpy(buf, bnx2_stats_str_arr,
  5262. sizeof(bnx2_stats_str_arr));
  5263. break;
  5264. case ETH_SS_TEST:
  5265. memcpy(buf, bnx2_tests_str_arr,
  5266. sizeof(bnx2_tests_str_arr));
  5267. break;
  5268. }
  5269. }
  5270. static void
  5271. bnx2_get_ethtool_stats(struct net_device *dev,
  5272. struct ethtool_stats *stats, u64 *buf)
  5273. {
  5274. struct bnx2 *bp = netdev_priv(dev);
  5275. int i;
  5276. u32 *hw_stats = (u32 *) bp->stats_blk;
  5277. u8 *stats_len_arr = NULL;
  5278. if (hw_stats == NULL) {
  5279. memset(buf, 0, sizeof(u64) * BNX2_NUM_STATS);
  5280. return;
  5281. }
  5282. if ((CHIP_ID(bp) == CHIP_ID_5706_A0) ||
  5283. (CHIP_ID(bp) == CHIP_ID_5706_A1) ||
  5284. (CHIP_ID(bp) == CHIP_ID_5706_A2) ||
  5285. (CHIP_ID(bp) == CHIP_ID_5708_A0))
  5286. stats_len_arr = bnx2_5706_stats_len_arr;
  5287. else
  5288. stats_len_arr = bnx2_5708_stats_len_arr;
  5289. for (i = 0; i < BNX2_NUM_STATS; i++) {
  5290. if (stats_len_arr[i] == 0) {
  5291. /* skip this counter */
  5292. buf[i] = 0;
  5293. continue;
  5294. }
  5295. if (stats_len_arr[i] == 4) {
  5296. /* 4-byte counter */
  5297. buf[i] = (u64)
  5298. *(hw_stats + bnx2_stats_offset_arr[i]);
  5299. continue;
  5300. }
  5301. /* 8-byte counter */
  5302. buf[i] = (((u64) *(hw_stats +
  5303. bnx2_stats_offset_arr[i])) << 32) +
  5304. *(hw_stats + bnx2_stats_offset_arr[i] + 1);
  5305. }
  5306. }
  5307. static int
  5308. bnx2_phys_id(struct net_device *dev, u32 data)
  5309. {
  5310. struct bnx2 *bp = netdev_priv(dev);
  5311. int i;
  5312. u32 save;
  5313. if (data == 0)
  5314. data = 2;
  5315. save = REG_RD(bp, BNX2_MISC_CFG);
  5316. REG_WR(bp, BNX2_MISC_CFG, BNX2_MISC_CFG_LEDMODE_MAC);
  5317. for (i = 0; i < (data * 2); i++) {
  5318. if ((i % 2) == 0) {
  5319. REG_WR(bp, BNX2_EMAC_LED, BNX2_EMAC_LED_OVERRIDE);
  5320. }
  5321. else {
  5322. REG_WR(bp, BNX2_EMAC_LED, BNX2_EMAC_LED_OVERRIDE |
  5323. BNX2_EMAC_LED_1000MB_OVERRIDE |
  5324. BNX2_EMAC_LED_100MB_OVERRIDE |
  5325. BNX2_EMAC_LED_10MB_OVERRIDE |
  5326. BNX2_EMAC_LED_TRAFFIC_OVERRIDE |
  5327. BNX2_EMAC_LED_TRAFFIC);
  5328. }
  5329. msleep_interruptible(500);
  5330. if (signal_pending(current))
  5331. break;
  5332. }
  5333. REG_WR(bp, BNX2_EMAC_LED, 0);
  5334. REG_WR(bp, BNX2_MISC_CFG, save);
  5335. return 0;
  5336. }
  5337. static int
  5338. bnx2_set_tx_csum(struct net_device *dev, u32 data)
  5339. {
  5340. struct bnx2 *bp = netdev_priv(dev);
  5341. if (CHIP_NUM(bp) == CHIP_NUM_5709)
  5342. return (ethtool_op_set_tx_ipv6_csum(dev, data));
  5343. else
  5344. return (ethtool_op_set_tx_csum(dev, data));
  5345. }
  5346. static const struct ethtool_ops bnx2_ethtool_ops = {
  5347. .get_settings = bnx2_get_settings,
  5348. .set_settings = bnx2_set_settings,
  5349. .get_drvinfo = bnx2_get_drvinfo,
  5350. .get_regs_len = bnx2_get_regs_len,
  5351. .get_regs = bnx2_get_regs,
  5352. .get_wol = bnx2_get_wol,
  5353. .set_wol = bnx2_set_wol,
  5354. .nway_reset = bnx2_nway_reset,
  5355. .get_link = ethtool_op_get_link,
  5356. .get_eeprom_len = bnx2_get_eeprom_len,
  5357. .get_eeprom = bnx2_get_eeprom,
  5358. .set_eeprom = bnx2_set_eeprom,
  5359. .get_coalesce = bnx2_get_coalesce,
  5360. .set_coalesce = bnx2_set_coalesce,
  5361. .get_ringparam = bnx2_get_ringparam,
  5362. .set_ringparam = bnx2_set_ringparam,
  5363. .get_pauseparam = bnx2_get_pauseparam,
  5364. .set_pauseparam = bnx2_set_pauseparam,
  5365. .get_rx_csum = bnx2_get_rx_csum,
  5366. .set_rx_csum = bnx2_set_rx_csum,
  5367. .set_tx_csum = bnx2_set_tx_csum,
  5368. .set_sg = ethtool_op_set_sg,
  5369. .set_tso = bnx2_set_tso,
  5370. .self_test = bnx2_self_test,
  5371. .get_strings = bnx2_get_strings,
  5372. .phys_id = bnx2_phys_id,
  5373. .get_ethtool_stats = bnx2_get_ethtool_stats,
  5374. .get_sset_count = bnx2_get_sset_count,
  5375. };
  5376. /* Called with rtnl_lock */
  5377. static int
  5378. bnx2_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  5379. {
  5380. struct mii_ioctl_data *data = if_mii(ifr);
  5381. struct bnx2 *bp = netdev_priv(dev);
  5382. int err;
  5383. switch(cmd) {
  5384. case SIOCGMIIPHY:
  5385. data->phy_id = bp->phy_addr;
  5386. /* fallthru */
  5387. case SIOCGMIIREG: {
  5388. u32 mii_regval;
  5389. if (bp->phy_flags & REMOTE_PHY_CAP_FLAG)
  5390. return -EOPNOTSUPP;
  5391. if (!netif_running(dev))
  5392. return -EAGAIN;
  5393. spin_lock_bh(&bp->phy_lock);
  5394. err = bnx2_read_phy(bp, data->reg_num & 0x1f, &mii_regval);
  5395. spin_unlock_bh(&bp->phy_lock);
  5396. data->val_out = mii_regval;
  5397. return err;
  5398. }
  5399. case SIOCSMIIREG:
  5400. if (!capable(CAP_NET_ADMIN))
  5401. return -EPERM;
  5402. if (bp->phy_flags & REMOTE_PHY_CAP_FLAG)
  5403. return -EOPNOTSUPP;
  5404. if (!netif_running(dev))
  5405. return -EAGAIN;
  5406. spin_lock_bh(&bp->phy_lock);
  5407. err = bnx2_write_phy(bp, data->reg_num & 0x1f, data->val_in);
  5408. spin_unlock_bh(&bp->phy_lock);
  5409. return err;
  5410. default:
  5411. /* do nothing */
  5412. break;
  5413. }
  5414. return -EOPNOTSUPP;
  5415. }
  5416. /* Called with rtnl_lock */
  5417. static int
  5418. bnx2_change_mac_addr(struct net_device *dev, void *p)
  5419. {
  5420. struct sockaddr *addr = p;
  5421. struct bnx2 *bp = netdev_priv(dev);
  5422. if (!is_valid_ether_addr(addr->sa_data))
  5423. return -EINVAL;
  5424. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  5425. if (netif_running(dev))
  5426. bnx2_set_mac_addr(bp);
  5427. return 0;
  5428. }
  5429. /* Called with rtnl_lock */
  5430. static int
  5431. bnx2_change_mtu(struct net_device *dev, int new_mtu)
  5432. {
  5433. struct bnx2 *bp = netdev_priv(dev);
  5434. if (((new_mtu + ETH_HLEN) > MAX_ETHERNET_JUMBO_PACKET_SIZE) ||
  5435. ((new_mtu + ETH_HLEN) < MIN_ETHERNET_PACKET_SIZE))
  5436. return -EINVAL;
  5437. dev->mtu = new_mtu;
  5438. return (bnx2_change_ring_size(bp, bp->rx_ring_size, bp->tx_ring_size));
  5439. }
  5440. #if defined(HAVE_POLL_CONTROLLER) || defined(CONFIG_NET_POLL_CONTROLLER)
  5441. static void
  5442. poll_bnx2(struct net_device *dev)
  5443. {
  5444. struct bnx2 *bp = netdev_priv(dev);
  5445. disable_irq(bp->pdev->irq);
  5446. bnx2_interrupt(bp->pdev->irq, dev);
  5447. enable_irq(bp->pdev->irq);
  5448. }
  5449. #endif
  5450. static void __devinit
  5451. bnx2_get_5709_media(struct bnx2 *bp)
  5452. {
  5453. u32 val = REG_RD(bp, BNX2_MISC_DUAL_MEDIA_CTRL);
  5454. u32 bond_id = val & BNX2_MISC_DUAL_MEDIA_CTRL_BOND_ID;
  5455. u32 strap;
  5456. if (bond_id == BNX2_MISC_DUAL_MEDIA_CTRL_BOND_ID_C)
  5457. return;
  5458. else if (bond_id == BNX2_MISC_DUAL_MEDIA_CTRL_BOND_ID_S) {
  5459. bp->phy_flags |= PHY_SERDES_FLAG;
  5460. return;
  5461. }
  5462. if (val & BNX2_MISC_DUAL_MEDIA_CTRL_STRAP_OVERRIDE)
  5463. strap = (val & BNX2_MISC_DUAL_MEDIA_CTRL_PHY_CTRL) >> 21;
  5464. else
  5465. strap = (val & BNX2_MISC_DUAL_MEDIA_CTRL_PHY_CTRL_STRAP) >> 8;
  5466. if (PCI_FUNC(bp->pdev->devfn) == 0) {
  5467. switch (strap) {
  5468. case 0x4:
  5469. case 0x5:
  5470. case 0x6:
  5471. bp->phy_flags |= PHY_SERDES_FLAG;
  5472. return;
  5473. }
  5474. } else {
  5475. switch (strap) {
  5476. case 0x1:
  5477. case 0x2:
  5478. case 0x4:
  5479. bp->phy_flags |= PHY_SERDES_FLAG;
  5480. return;
  5481. }
  5482. }
  5483. }
  5484. static void __devinit
  5485. bnx2_get_pci_speed(struct bnx2 *bp)
  5486. {
  5487. u32 reg;
  5488. reg = REG_RD(bp, BNX2_PCICFG_MISC_STATUS);
  5489. if (reg & BNX2_PCICFG_MISC_STATUS_PCIX_DET) {
  5490. u32 clkreg;
  5491. bp->flags |= PCIX_FLAG;
  5492. clkreg = REG_RD(bp, BNX2_PCICFG_PCI_CLOCK_CONTROL_BITS);
  5493. clkreg &= BNX2_PCICFG_PCI_CLOCK_CONTROL_BITS_PCI_CLK_SPD_DET;
  5494. switch (clkreg) {
  5495. case BNX2_PCICFG_PCI_CLOCK_CONTROL_BITS_PCI_CLK_SPD_DET_133MHZ:
  5496. bp->bus_speed_mhz = 133;
  5497. break;
  5498. case BNX2_PCICFG_PCI_CLOCK_CONTROL_BITS_PCI_CLK_SPD_DET_95MHZ:
  5499. bp->bus_speed_mhz = 100;
  5500. break;
  5501. case BNX2_PCICFG_PCI_CLOCK_CONTROL_BITS_PCI_CLK_SPD_DET_66MHZ:
  5502. case BNX2_PCICFG_PCI_CLOCK_CONTROL_BITS_PCI_CLK_SPD_DET_80MHZ:
  5503. bp->bus_speed_mhz = 66;
  5504. break;
  5505. case BNX2_PCICFG_PCI_CLOCK_CONTROL_BITS_PCI_CLK_SPD_DET_48MHZ:
  5506. case BNX2_PCICFG_PCI_CLOCK_CONTROL_BITS_PCI_CLK_SPD_DET_55MHZ:
  5507. bp->bus_speed_mhz = 50;
  5508. break;
  5509. case BNX2_PCICFG_PCI_CLOCK_CONTROL_BITS_PCI_CLK_SPD_DET_LOW:
  5510. case BNX2_PCICFG_PCI_CLOCK_CONTROL_BITS_PCI_CLK_SPD_DET_32MHZ:
  5511. case BNX2_PCICFG_PCI_CLOCK_CONTROL_BITS_PCI_CLK_SPD_DET_38MHZ:
  5512. bp->bus_speed_mhz = 33;
  5513. break;
  5514. }
  5515. }
  5516. else {
  5517. if (reg & BNX2_PCICFG_MISC_STATUS_M66EN)
  5518. bp->bus_speed_mhz = 66;
  5519. else
  5520. bp->bus_speed_mhz = 33;
  5521. }
  5522. if (reg & BNX2_PCICFG_MISC_STATUS_32BIT_DET)
  5523. bp->flags |= PCI_32BIT_FLAG;
  5524. }
  5525. static int __devinit
  5526. bnx2_init_board(struct pci_dev *pdev, struct net_device *dev)
  5527. {
  5528. struct bnx2 *bp;
  5529. unsigned long mem_len;
  5530. int rc, i, j;
  5531. u32 reg;
  5532. u64 dma_mask, persist_dma_mask;
  5533. SET_NETDEV_DEV(dev, &pdev->dev);
  5534. bp = netdev_priv(dev);
  5535. bp->flags = 0;
  5536. bp->phy_flags = 0;
  5537. /* enable device (incl. PCI PM wakeup), and bus-mastering */
  5538. rc = pci_enable_device(pdev);
  5539. if (rc) {
  5540. dev_err(&pdev->dev, "Cannot enable PCI device, aborting.\n");
  5541. goto err_out;
  5542. }
  5543. if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
  5544. dev_err(&pdev->dev,
  5545. "Cannot find PCI device base address, aborting.\n");
  5546. rc = -ENODEV;
  5547. goto err_out_disable;
  5548. }
  5549. rc = pci_request_regions(pdev, DRV_MODULE_NAME);
  5550. if (rc) {
  5551. dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting.\n");
  5552. goto err_out_disable;
  5553. }
  5554. pci_set_master(pdev);
  5555. bp->pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
  5556. if (bp->pm_cap == 0) {
  5557. dev_err(&pdev->dev,
  5558. "Cannot find power management capability, aborting.\n");
  5559. rc = -EIO;
  5560. goto err_out_release;
  5561. }
  5562. bp->dev = dev;
  5563. bp->pdev = pdev;
  5564. spin_lock_init(&bp->phy_lock);
  5565. spin_lock_init(&bp->indirect_lock);
  5566. INIT_WORK(&bp->reset_task, bnx2_reset_task);
  5567. dev->base_addr = dev->mem_start = pci_resource_start(pdev, 0);
  5568. mem_len = MB_GET_CID_ADDR(TX_TSS_CID + 1);
  5569. dev->mem_end = dev->mem_start + mem_len;
  5570. dev->irq = pdev->irq;
  5571. bp->regview = ioremap_nocache(dev->base_addr, mem_len);
  5572. if (!bp->regview) {
  5573. dev_err(&pdev->dev, "Cannot map register space, aborting.\n");
  5574. rc = -ENOMEM;
  5575. goto err_out_release;
  5576. }
  5577. /* Configure byte swap and enable write to the reg_window registers.
  5578. * Rely on CPU to do target byte swapping on big endian systems
  5579. * The chip's target access swapping will not swap all accesses
  5580. */
  5581. pci_write_config_dword(bp->pdev, BNX2_PCICFG_MISC_CONFIG,
  5582. BNX2_PCICFG_MISC_CONFIG_REG_WINDOW_ENA |
  5583. BNX2_PCICFG_MISC_CONFIG_TARGET_MB_WORD_SWAP);
  5584. bnx2_set_power_state(bp, PCI_D0);
  5585. bp->chip_id = REG_RD(bp, BNX2_MISC_ID);
  5586. if (CHIP_NUM(bp) == CHIP_NUM_5709) {
  5587. if (pci_find_capability(pdev, PCI_CAP_ID_EXP) == 0) {
  5588. dev_err(&pdev->dev,
  5589. "Cannot find PCIE capability, aborting.\n");
  5590. rc = -EIO;
  5591. goto err_out_unmap;
  5592. }
  5593. bp->flags |= PCIE_FLAG;
  5594. } else {
  5595. bp->pcix_cap = pci_find_capability(pdev, PCI_CAP_ID_PCIX);
  5596. if (bp->pcix_cap == 0) {
  5597. dev_err(&pdev->dev,
  5598. "Cannot find PCIX capability, aborting.\n");
  5599. rc = -EIO;
  5600. goto err_out_unmap;
  5601. }
  5602. }
  5603. if (CHIP_ID(bp) != CHIP_ID_5706_A0 && CHIP_ID(bp) != CHIP_ID_5706_A1) {
  5604. if (pci_find_capability(pdev, PCI_CAP_ID_MSI))
  5605. bp->flags |= MSI_CAP_FLAG;
  5606. }
  5607. /* 5708 cannot support DMA addresses > 40-bit. */
  5608. if (CHIP_NUM(bp) == CHIP_NUM_5708)
  5609. persist_dma_mask = dma_mask = DMA_40BIT_MASK;
  5610. else
  5611. persist_dma_mask = dma_mask = DMA_64BIT_MASK;
  5612. /* Configure DMA attributes. */
  5613. if (pci_set_dma_mask(pdev, dma_mask) == 0) {
  5614. dev->features |= NETIF_F_HIGHDMA;
  5615. rc = pci_set_consistent_dma_mask(pdev, persist_dma_mask);
  5616. if (rc) {
  5617. dev_err(&pdev->dev,
  5618. "pci_set_consistent_dma_mask failed, aborting.\n");
  5619. goto err_out_unmap;
  5620. }
  5621. } else if ((rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK)) != 0) {
  5622. dev_err(&pdev->dev, "System does not support DMA, aborting.\n");
  5623. goto err_out_unmap;
  5624. }
  5625. if (!(bp->flags & PCIE_FLAG))
  5626. bnx2_get_pci_speed(bp);
  5627. /* 5706A0 may falsely detect SERR and PERR. */
  5628. if (CHIP_ID(bp) == CHIP_ID_5706_A0) {
  5629. reg = REG_RD(bp, PCI_COMMAND);
  5630. reg &= ~(PCI_COMMAND_SERR | PCI_COMMAND_PARITY);
  5631. REG_WR(bp, PCI_COMMAND, reg);
  5632. }
  5633. else if ((CHIP_ID(bp) == CHIP_ID_5706_A1) &&
  5634. !(bp->flags & PCIX_FLAG)) {
  5635. dev_err(&pdev->dev,
  5636. "5706 A1 can only be used in a PCIX bus, aborting.\n");
  5637. goto err_out_unmap;
  5638. }
  5639. bnx2_init_nvram(bp);
  5640. reg = REG_RD_IND(bp, BNX2_SHM_HDR_SIGNATURE);
  5641. if ((reg & BNX2_SHM_HDR_SIGNATURE_SIG_MASK) ==
  5642. BNX2_SHM_HDR_SIGNATURE_SIG) {
  5643. u32 off = PCI_FUNC(pdev->devfn) << 2;
  5644. bp->shmem_base = REG_RD_IND(bp, BNX2_SHM_HDR_ADDR_0 + off);
  5645. } else
  5646. bp->shmem_base = HOST_VIEW_SHMEM_BASE;
  5647. /* Get the permanent MAC address. First we need to make sure the
  5648. * firmware is actually running.
  5649. */
  5650. reg = REG_RD_IND(bp, bp->shmem_base + BNX2_DEV_INFO_SIGNATURE);
  5651. if ((reg & BNX2_DEV_INFO_SIGNATURE_MAGIC_MASK) !=
  5652. BNX2_DEV_INFO_SIGNATURE_MAGIC) {
  5653. dev_err(&pdev->dev, "Firmware not running, aborting.\n");
  5654. rc = -ENODEV;
  5655. goto err_out_unmap;
  5656. }
  5657. reg = REG_RD_IND(bp, bp->shmem_base + BNX2_DEV_INFO_BC_REV);
  5658. for (i = 0, j = 0; i < 3; i++) {
  5659. u8 num, k, skip0;
  5660. num = (u8) (reg >> (24 - (i * 8)));
  5661. for (k = 100, skip0 = 1; k >= 1; num %= k, k /= 10) {
  5662. if (num >= k || !skip0 || k == 1) {
  5663. bp->fw_version[j++] = (num / k) + '0';
  5664. skip0 = 0;
  5665. }
  5666. }
  5667. if (i != 2)
  5668. bp->fw_version[j++] = '.';
  5669. }
  5670. reg = REG_RD_IND(bp, bp->shmem_base + BNX2_PORT_FEATURE);
  5671. if (reg & BNX2_PORT_FEATURE_WOL_ENABLED)
  5672. bp->wol = 1;
  5673. if (reg & BNX2_PORT_FEATURE_ASF_ENABLED) {
  5674. bp->flags |= ASF_ENABLE_FLAG;
  5675. for (i = 0; i < 30; i++) {
  5676. reg = REG_RD_IND(bp, bp->shmem_base +
  5677. BNX2_BC_STATE_CONDITION);
  5678. if (reg & BNX2_CONDITION_MFW_RUN_MASK)
  5679. break;
  5680. msleep(10);
  5681. }
  5682. }
  5683. reg = REG_RD_IND(bp, bp->shmem_base + BNX2_BC_STATE_CONDITION);
  5684. reg &= BNX2_CONDITION_MFW_RUN_MASK;
  5685. if (reg != BNX2_CONDITION_MFW_RUN_UNKNOWN &&
  5686. reg != BNX2_CONDITION_MFW_RUN_NONE) {
  5687. int i;
  5688. u32 addr = REG_RD_IND(bp, bp->shmem_base + BNX2_MFW_VER_PTR);
  5689. bp->fw_version[j++] = ' ';
  5690. for (i = 0; i < 3; i++) {
  5691. reg = REG_RD_IND(bp, addr + i * 4);
  5692. reg = swab32(reg);
  5693. memcpy(&bp->fw_version[j], &reg, 4);
  5694. j += 4;
  5695. }
  5696. }
  5697. reg = REG_RD_IND(bp, bp->shmem_base + BNX2_PORT_HW_CFG_MAC_UPPER);
  5698. bp->mac_addr[0] = (u8) (reg >> 8);
  5699. bp->mac_addr[1] = (u8) reg;
  5700. reg = REG_RD_IND(bp, bp->shmem_base + BNX2_PORT_HW_CFG_MAC_LOWER);
  5701. bp->mac_addr[2] = (u8) (reg >> 24);
  5702. bp->mac_addr[3] = (u8) (reg >> 16);
  5703. bp->mac_addr[4] = (u8) (reg >> 8);
  5704. bp->mac_addr[5] = (u8) reg;
  5705. bp->rx_offset = sizeof(struct l2_fhdr) + 2;
  5706. bp->tx_ring_size = MAX_TX_DESC_CNT;
  5707. bnx2_set_rx_ring_size(bp, 255);
  5708. bp->rx_csum = 1;
  5709. bp->tx_quick_cons_trip_int = 20;
  5710. bp->tx_quick_cons_trip = 20;
  5711. bp->tx_ticks_int = 80;
  5712. bp->tx_ticks = 80;
  5713. bp->rx_quick_cons_trip_int = 6;
  5714. bp->rx_quick_cons_trip = 6;
  5715. bp->rx_ticks_int = 18;
  5716. bp->rx_ticks = 18;
  5717. bp->stats_ticks = USEC_PER_SEC & BNX2_HC_STATS_TICKS_HC_STAT_TICKS;
  5718. bp->timer_interval = HZ;
  5719. bp->current_interval = HZ;
  5720. bp->phy_addr = 1;
  5721. /* Disable WOL support if we are running on a SERDES chip. */
  5722. if (CHIP_NUM(bp) == CHIP_NUM_5709)
  5723. bnx2_get_5709_media(bp);
  5724. else if (CHIP_BOND_ID(bp) & CHIP_BOND_ID_SERDES_BIT)
  5725. bp->phy_flags |= PHY_SERDES_FLAG;
  5726. bp->phy_port = PORT_TP;
  5727. if (bp->phy_flags & PHY_SERDES_FLAG) {
  5728. bp->phy_port = PORT_FIBRE;
  5729. reg = REG_RD_IND(bp, bp->shmem_base +
  5730. BNX2_SHARED_HW_CFG_CONFIG);
  5731. if (!(reg & BNX2_SHARED_HW_CFG_GIG_LINK_ON_VAUX)) {
  5732. bp->flags |= NO_WOL_FLAG;
  5733. bp->wol = 0;
  5734. }
  5735. if (CHIP_NUM(bp) != CHIP_NUM_5706) {
  5736. bp->phy_addr = 2;
  5737. if (reg & BNX2_SHARED_HW_CFG_PHY_2_5G)
  5738. bp->phy_flags |= PHY_2_5G_CAPABLE_FLAG;
  5739. }
  5740. bnx2_init_remote_phy(bp);
  5741. } else if (CHIP_NUM(bp) == CHIP_NUM_5706 ||
  5742. CHIP_NUM(bp) == CHIP_NUM_5708)
  5743. bp->phy_flags |= PHY_CRC_FIX_FLAG;
  5744. else if (CHIP_NUM(bp) == CHIP_NUM_5709 &&
  5745. (CHIP_REV(bp) == CHIP_REV_Ax ||
  5746. CHIP_REV(bp) == CHIP_REV_Bx))
  5747. bp->phy_flags |= PHY_DIS_EARLY_DAC_FLAG;
  5748. if ((CHIP_ID(bp) == CHIP_ID_5708_A0) ||
  5749. (CHIP_ID(bp) == CHIP_ID_5708_B0) ||
  5750. (CHIP_ID(bp) == CHIP_ID_5708_B1)) {
  5751. bp->flags |= NO_WOL_FLAG;
  5752. bp->wol = 0;
  5753. }
  5754. if (CHIP_ID(bp) == CHIP_ID_5706_A0) {
  5755. bp->tx_quick_cons_trip_int =
  5756. bp->tx_quick_cons_trip;
  5757. bp->tx_ticks_int = bp->tx_ticks;
  5758. bp->rx_quick_cons_trip_int =
  5759. bp->rx_quick_cons_trip;
  5760. bp->rx_ticks_int = bp->rx_ticks;
  5761. bp->comp_prod_trip_int = bp->comp_prod_trip;
  5762. bp->com_ticks_int = bp->com_ticks;
  5763. bp->cmd_ticks_int = bp->cmd_ticks;
  5764. }
  5765. /* Disable MSI on 5706 if AMD 8132 bridge is found.
  5766. *
  5767. * MSI is defined to be 32-bit write. The 5706 does 64-bit MSI writes
  5768. * with byte enables disabled on the unused 32-bit word. This is legal
  5769. * but causes problems on the AMD 8132 which will eventually stop
  5770. * responding after a while.
  5771. *
  5772. * AMD believes this incompatibility is unique to the 5706, and
  5773. * prefers to locally disable MSI rather than globally disabling it.
  5774. */
  5775. if (CHIP_NUM(bp) == CHIP_NUM_5706 && disable_msi == 0) {
  5776. struct pci_dev *amd_8132 = NULL;
  5777. while ((amd_8132 = pci_get_device(PCI_VENDOR_ID_AMD,
  5778. PCI_DEVICE_ID_AMD_8132_BRIDGE,
  5779. amd_8132))) {
  5780. if (amd_8132->revision >= 0x10 &&
  5781. amd_8132->revision <= 0x13) {
  5782. disable_msi = 1;
  5783. pci_dev_put(amd_8132);
  5784. break;
  5785. }
  5786. }
  5787. }
  5788. bnx2_set_default_link(bp);
  5789. bp->req_flow_ctrl = FLOW_CTRL_RX | FLOW_CTRL_TX;
  5790. init_timer(&bp->timer);
  5791. bp->timer.expires = RUN_AT(bp->timer_interval);
  5792. bp->timer.data = (unsigned long) bp;
  5793. bp->timer.function = bnx2_timer;
  5794. return 0;
  5795. err_out_unmap:
  5796. if (bp->regview) {
  5797. iounmap(bp->regview);
  5798. bp->regview = NULL;
  5799. }
  5800. err_out_release:
  5801. pci_release_regions(pdev);
  5802. err_out_disable:
  5803. pci_disable_device(pdev);
  5804. pci_set_drvdata(pdev, NULL);
  5805. err_out:
  5806. return rc;
  5807. }
  5808. static char * __devinit
  5809. bnx2_bus_string(struct bnx2 *bp, char *str)
  5810. {
  5811. char *s = str;
  5812. if (bp->flags & PCIE_FLAG) {
  5813. s += sprintf(s, "PCI Express");
  5814. } else {
  5815. s += sprintf(s, "PCI");
  5816. if (bp->flags & PCIX_FLAG)
  5817. s += sprintf(s, "-X");
  5818. if (bp->flags & PCI_32BIT_FLAG)
  5819. s += sprintf(s, " 32-bit");
  5820. else
  5821. s += sprintf(s, " 64-bit");
  5822. s += sprintf(s, " %dMHz", bp->bus_speed_mhz);
  5823. }
  5824. return str;
  5825. }
  5826. static int __devinit
  5827. bnx2_init_napi(struct bnx2 *bp)
  5828. {
  5829. struct bnx2_napi *bnapi = &bp->bnx2_napi;
  5830. bnapi->bp = bp;
  5831. netif_napi_add(bp->dev, &bnapi->napi, bnx2_poll, 64);
  5832. }
  5833. static int __devinit
  5834. bnx2_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  5835. {
  5836. static int version_printed = 0;
  5837. struct net_device *dev = NULL;
  5838. struct bnx2 *bp;
  5839. int rc;
  5840. char str[40];
  5841. DECLARE_MAC_BUF(mac);
  5842. if (version_printed++ == 0)
  5843. printk(KERN_INFO "%s", version);
  5844. /* dev zeroed in init_etherdev */
  5845. dev = alloc_etherdev(sizeof(*bp));
  5846. if (!dev)
  5847. return -ENOMEM;
  5848. rc = bnx2_init_board(pdev, dev);
  5849. if (rc < 0) {
  5850. free_netdev(dev);
  5851. return rc;
  5852. }
  5853. dev->open = bnx2_open;
  5854. dev->hard_start_xmit = bnx2_start_xmit;
  5855. dev->stop = bnx2_close;
  5856. dev->get_stats = bnx2_get_stats;
  5857. dev->set_multicast_list = bnx2_set_rx_mode;
  5858. dev->do_ioctl = bnx2_ioctl;
  5859. dev->set_mac_address = bnx2_change_mac_addr;
  5860. dev->change_mtu = bnx2_change_mtu;
  5861. dev->tx_timeout = bnx2_tx_timeout;
  5862. dev->watchdog_timeo = TX_TIMEOUT;
  5863. #ifdef BCM_VLAN
  5864. dev->vlan_rx_register = bnx2_vlan_rx_register;
  5865. #endif
  5866. dev->ethtool_ops = &bnx2_ethtool_ops;
  5867. bp = netdev_priv(dev);
  5868. bnx2_init_napi(bp);
  5869. #if defined(HAVE_POLL_CONTROLLER) || defined(CONFIG_NET_POLL_CONTROLLER)
  5870. dev->poll_controller = poll_bnx2;
  5871. #endif
  5872. pci_set_drvdata(pdev, dev);
  5873. memcpy(dev->dev_addr, bp->mac_addr, 6);
  5874. memcpy(dev->perm_addr, bp->mac_addr, 6);
  5875. bp->name = board_info[ent->driver_data].name;
  5876. dev->features |= NETIF_F_IP_CSUM | NETIF_F_SG;
  5877. if (CHIP_NUM(bp) == CHIP_NUM_5709)
  5878. dev->features |= NETIF_F_IPV6_CSUM;
  5879. #ifdef BCM_VLAN
  5880. dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
  5881. #endif
  5882. dev->features |= NETIF_F_TSO | NETIF_F_TSO_ECN;
  5883. if (CHIP_NUM(bp) == CHIP_NUM_5709)
  5884. dev->features |= NETIF_F_TSO6;
  5885. if ((rc = register_netdev(dev))) {
  5886. dev_err(&pdev->dev, "Cannot register net device\n");
  5887. if (bp->regview)
  5888. iounmap(bp->regview);
  5889. pci_release_regions(pdev);
  5890. pci_disable_device(pdev);
  5891. pci_set_drvdata(pdev, NULL);
  5892. free_netdev(dev);
  5893. return rc;
  5894. }
  5895. printk(KERN_INFO "%s: %s (%c%d) %s found at mem %lx, "
  5896. "IRQ %d, node addr %s\n",
  5897. dev->name,
  5898. bp->name,
  5899. ((CHIP_ID(bp) & 0xf000) >> 12) + 'A',
  5900. ((CHIP_ID(bp) & 0x0ff0) >> 4),
  5901. bnx2_bus_string(bp, str),
  5902. dev->base_addr,
  5903. bp->pdev->irq, print_mac(mac, dev->dev_addr));
  5904. return 0;
  5905. }
  5906. static void __devexit
  5907. bnx2_remove_one(struct pci_dev *pdev)
  5908. {
  5909. struct net_device *dev = pci_get_drvdata(pdev);
  5910. struct bnx2 *bp = netdev_priv(dev);
  5911. flush_scheduled_work();
  5912. unregister_netdev(dev);
  5913. if (bp->regview)
  5914. iounmap(bp->regview);
  5915. free_netdev(dev);
  5916. pci_release_regions(pdev);
  5917. pci_disable_device(pdev);
  5918. pci_set_drvdata(pdev, NULL);
  5919. }
  5920. static int
  5921. bnx2_suspend(struct pci_dev *pdev, pm_message_t state)
  5922. {
  5923. struct net_device *dev = pci_get_drvdata(pdev);
  5924. struct bnx2 *bp = netdev_priv(dev);
  5925. u32 reset_code;
  5926. /* PCI register 4 needs to be saved whether netif_running() or not.
  5927. * MSI address and data need to be saved if using MSI and
  5928. * netif_running().
  5929. */
  5930. pci_save_state(pdev);
  5931. if (!netif_running(dev))
  5932. return 0;
  5933. flush_scheduled_work();
  5934. bnx2_netif_stop(bp);
  5935. netif_device_detach(dev);
  5936. del_timer_sync(&bp->timer);
  5937. if (bp->flags & NO_WOL_FLAG)
  5938. reset_code = BNX2_DRV_MSG_CODE_UNLOAD_LNK_DN;
  5939. else if (bp->wol)
  5940. reset_code = BNX2_DRV_MSG_CODE_SUSPEND_WOL;
  5941. else
  5942. reset_code = BNX2_DRV_MSG_CODE_SUSPEND_NO_WOL;
  5943. bnx2_reset_chip(bp, reset_code);
  5944. bnx2_free_skbs(bp);
  5945. bnx2_set_power_state(bp, pci_choose_state(pdev, state));
  5946. return 0;
  5947. }
  5948. static int
  5949. bnx2_resume(struct pci_dev *pdev)
  5950. {
  5951. struct net_device *dev = pci_get_drvdata(pdev);
  5952. struct bnx2 *bp = netdev_priv(dev);
  5953. pci_restore_state(pdev);
  5954. if (!netif_running(dev))
  5955. return 0;
  5956. bnx2_set_power_state(bp, PCI_D0);
  5957. netif_device_attach(dev);
  5958. bnx2_init_nic(bp);
  5959. bnx2_netif_start(bp);
  5960. return 0;
  5961. }
  5962. static struct pci_driver bnx2_pci_driver = {
  5963. .name = DRV_MODULE_NAME,
  5964. .id_table = bnx2_pci_tbl,
  5965. .probe = bnx2_init_one,
  5966. .remove = __devexit_p(bnx2_remove_one),
  5967. .suspend = bnx2_suspend,
  5968. .resume = bnx2_resume,
  5969. };
  5970. static int __init bnx2_init(void)
  5971. {
  5972. return pci_register_driver(&bnx2_pci_driver);
  5973. }
  5974. static void __exit bnx2_cleanup(void)
  5975. {
  5976. pci_unregister_driver(&bnx2_pci_driver);
  5977. }
  5978. module_init(bnx2_init);
  5979. module_exit(bnx2_cleanup);