bnx2x_sp.c 162 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943
  1. /* bnx2x_sp.c: Broadcom Everest network driver.
  2. *
  3. * Copyright (c) 2011-2013 Broadcom Corporation
  4. *
  5. * Unless you and Broadcom execute a separate written software license
  6. * agreement governing use of this software, this software is licensed to you
  7. * under the terms of the GNU General Public License version 2, available
  8. * at http://www.gnu.org/licenses/old-licenses/gpl-2.0.html (the "GPL").
  9. *
  10. * Notwithstanding the above, under no circumstances may you combine this
  11. * software in any way with any other Broadcom software provided under a
  12. * license other than the GPL, without Broadcom's express prior written
  13. * consent.
  14. *
  15. * Maintained by: Ariel Elior <ariel.elior@qlogic.com>
  16. * Written by: Vladislav Zolotarov
  17. *
  18. */
  19. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  20. #include <linux/module.h>
  21. #include <linux/crc32.h>
  22. #include <linux/netdevice.h>
  23. #include <linux/etherdevice.h>
  24. #include <linux/crc32c.h>
  25. #include "bnx2x.h"
  26. #include "bnx2x_cmn.h"
  27. #include "bnx2x_sp.h"
  28. #define BNX2X_MAX_EMUL_MULTI 16
  29. /**** Exe Queue interfaces ****/
  30. /**
  31. * bnx2x_exe_queue_init - init the Exe Queue object
  32. *
  33. * @o: pointer to the object
  34. * @exe_len: length
  35. * @owner: pointer to the owner
  36. * @validate: validate function pointer
  37. * @optimize: optimize function pointer
  38. * @exec: execute function pointer
  39. * @get: get function pointer
  40. */
  41. static inline void bnx2x_exe_queue_init(struct bnx2x *bp,
  42. struct bnx2x_exe_queue_obj *o,
  43. int exe_len,
  44. union bnx2x_qable_obj *owner,
  45. exe_q_validate validate,
  46. exe_q_remove remove,
  47. exe_q_optimize optimize,
  48. exe_q_execute exec,
  49. exe_q_get get)
  50. {
  51. memset(o, 0, sizeof(*o));
  52. INIT_LIST_HEAD(&o->exe_queue);
  53. INIT_LIST_HEAD(&o->pending_comp);
  54. spin_lock_init(&o->lock);
  55. o->exe_chunk_len = exe_len;
  56. o->owner = owner;
  57. /* Owner specific callbacks */
  58. o->validate = validate;
  59. o->remove = remove;
  60. o->optimize = optimize;
  61. o->execute = exec;
  62. o->get = get;
  63. DP(BNX2X_MSG_SP, "Setup the execution queue with the chunk length of %d\n",
  64. exe_len);
  65. }
  66. static inline void bnx2x_exe_queue_free_elem(struct bnx2x *bp,
  67. struct bnx2x_exeq_elem *elem)
  68. {
  69. DP(BNX2X_MSG_SP, "Deleting an exe_queue element\n");
  70. kfree(elem);
  71. }
  72. static inline int bnx2x_exe_queue_length(struct bnx2x_exe_queue_obj *o)
  73. {
  74. struct bnx2x_exeq_elem *elem;
  75. int cnt = 0;
  76. spin_lock_bh(&o->lock);
  77. list_for_each_entry(elem, &o->exe_queue, link)
  78. cnt++;
  79. spin_unlock_bh(&o->lock);
  80. return cnt;
  81. }
  82. /**
  83. * bnx2x_exe_queue_add - add a new element to the execution queue
  84. *
  85. * @bp: driver handle
  86. * @o: queue
  87. * @cmd: new command to add
  88. * @restore: true - do not optimize the command
  89. *
  90. * If the element is optimized or is illegal, frees it.
  91. */
  92. static inline int bnx2x_exe_queue_add(struct bnx2x *bp,
  93. struct bnx2x_exe_queue_obj *o,
  94. struct bnx2x_exeq_elem *elem,
  95. bool restore)
  96. {
  97. int rc;
  98. spin_lock_bh(&o->lock);
  99. if (!restore) {
  100. /* Try to cancel this element queue */
  101. rc = o->optimize(bp, o->owner, elem);
  102. if (rc)
  103. goto free_and_exit;
  104. /* Check if this request is ok */
  105. rc = o->validate(bp, o->owner, elem);
  106. if (rc) {
  107. DP(BNX2X_MSG_SP, "Preamble failed: %d\n", rc);
  108. goto free_and_exit;
  109. }
  110. }
  111. /* If so, add it to the execution queue */
  112. list_add_tail(&elem->link, &o->exe_queue);
  113. spin_unlock_bh(&o->lock);
  114. return 0;
  115. free_and_exit:
  116. bnx2x_exe_queue_free_elem(bp, elem);
  117. spin_unlock_bh(&o->lock);
  118. return rc;
  119. }
  120. static inline void __bnx2x_exe_queue_reset_pending(
  121. struct bnx2x *bp,
  122. struct bnx2x_exe_queue_obj *o)
  123. {
  124. struct bnx2x_exeq_elem *elem;
  125. while (!list_empty(&o->pending_comp)) {
  126. elem = list_first_entry(&o->pending_comp,
  127. struct bnx2x_exeq_elem, link);
  128. list_del(&elem->link);
  129. bnx2x_exe_queue_free_elem(bp, elem);
  130. }
  131. }
  132. /**
  133. * bnx2x_exe_queue_step - execute one execution chunk atomically
  134. *
  135. * @bp: driver handle
  136. * @o: queue
  137. * @ramrod_flags: flags
  138. *
  139. * (Should be called while holding the exe_queue->lock).
  140. */
  141. static inline int bnx2x_exe_queue_step(struct bnx2x *bp,
  142. struct bnx2x_exe_queue_obj *o,
  143. unsigned long *ramrod_flags)
  144. {
  145. struct bnx2x_exeq_elem *elem, spacer;
  146. int cur_len = 0, rc;
  147. memset(&spacer, 0, sizeof(spacer));
  148. /* Next step should not be performed until the current is finished,
  149. * unless a DRV_CLEAR_ONLY bit is set. In this case we just want to
  150. * properly clear object internals without sending any command to the FW
  151. * which also implies there won't be any completion to clear the
  152. * 'pending' list.
  153. */
  154. if (!list_empty(&o->pending_comp)) {
  155. if (test_bit(RAMROD_DRV_CLR_ONLY, ramrod_flags)) {
  156. DP(BNX2X_MSG_SP, "RAMROD_DRV_CLR_ONLY requested: resetting a pending_comp list\n");
  157. __bnx2x_exe_queue_reset_pending(bp, o);
  158. } else {
  159. return 1;
  160. }
  161. }
  162. /* Run through the pending commands list and create a next
  163. * execution chunk.
  164. */
  165. while (!list_empty(&o->exe_queue)) {
  166. elem = list_first_entry(&o->exe_queue, struct bnx2x_exeq_elem,
  167. link);
  168. WARN_ON(!elem->cmd_len);
  169. if (cur_len + elem->cmd_len <= o->exe_chunk_len) {
  170. cur_len += elem->cmd_len;
  171. /* Prevent from both lists being empty when moving an
  172. * element. This will allow the call of
  173. * bnx2x_exe_queue_empty() without locking.
  174. */
  175. list_add_tail(&spacer.link, &o->pending_comp);
  176. mb();
  177. list_move_tail(&elem->link, &o->pending_comp);
  178. list_del(&spacer.link);
  179. } else
  180. break;
  181. }
  182. /* Sanity check */
  183. if (!cur_len)
  184. return 0;
  185. rc = o->execute(bp, o->owner, &o->pending_comp, ramrod_flags);
  186. if (rc < 0)
  187. /* In case of an error return the commands back to the queue
  188. * and reset the pending_comp.
  189. */
  190. list_splice_init(&o->pending_comp, &o->exe_queue);
  191. else if (!rc)
  192. /* If zero is returned, means there are no outstanding pending
  193. * completions and we may dismiss the pending list.
  194. */
  195. __bnx2x_exe_queue_reset_pending(bp, o);
  196. return rc;
  197. }
  198. static inline bool bnx2x_exe_queue_empty(struct bnx2x_exe_queue_obj *o)
  199. {
  200. bool empty = list_empty(&o->exe_queue);
  201. /* Don't reorder!!! */
  202. mb();
  203. return empty && list_empty(&o->pending_comp);
  204. }
  205. static inline struct bnx2x_exeq_elem *bnx2x_exe_queue_alloc_elem(
  206. struct bnx2x *bp)
  207. {
  208. DP(BNX2X_MSG_SP, "Allocating a new exe_queue element\n");
  209. return kzalloc(sizeof(struct bnx2x_exeq_elem), GFP_ATOMIC);
  210. }
  211. /************************ raw_obj functions ***********************************/
  212. static bool bnx2x_raw_check_pending(struct bnx2x_raw_obj *o)
  213. {
  214. return !!test_bit(o->state, o->pstate);
  215. }
  216. static void bnx2x_raw_clear_pending(struct bnx2x_raw_obj *o)
  217. {
  218. smp_mb__before_atomic();
  219. clear_bit(o->state, o->pstate);
  220. smp_mb__after_atomic();
  221. }
  222. static void bnx2x_raw_set_pending(struct bnx2x_raw_obj *o)
  223. {
  224. smp_mb__before_atomic();
  225. set_bit(o->state, o->pstate);
  226. smp_mb__after_atomic();
  227. }
  228. /**
  229. * bnx2x_state_wait - wait until the given bit(state) is cleared
  230. *
  231. * @bp: device handle
  232. * @state: state which is to be cleared
  233. * @state_p: state buffer
  234. *
  235. */
  236. static inline int bnx2x_state_wait(struct bnx2x *bp, int state,
  237. unsigned long *pstate)
  238. {
  239. /* can take a while if any port is running */
  240. int cnt = 5000;
  241. if (CHIP_REV_IS_EMUL(bp))
  242. cnt *= 20;
  243. DP(BNX2X_MSG_SP, "waiting for state to become %d\n", state);
  244. might_sleep();
  245. while (cnt--) {
  246. if (!test_bit(state, pstate)) {
  247. #ifdef BNX2X_STOP_ON_ERROR
  248. DP(BNX2X_MSG_SP, "exit (cnt %d)\n", 5000 - cnt);
  249. #endif
  250. return 0;
  251. }
  252. usleep_range(1000, 2000);
  253. if (bp->panic)
  254. return -EIO;
  255. }
  256. /* timeout! */
  257. BNX2X_ERR("timeout waiting for state %d\n", state);
  258. #ifdef BNX2X_STOP_ON_ERROR
  259. bnx2x_panic();
  260. #endif
  261. return -EBUSY;
  262. }
  263. static int bnx2x_raw_wait(struct bnx2x *bp, struct bnx2x_raw_obj *raw)
  264. {
  265. return bnx2x_state_wait(bp, raw->state, raw->pstate);
  266. }
  267. /***************** Classification verbs: Set/Del MAC/VLAN/VLAN-MAC ************/
  268. /* credit handling callbacks */
  269. static bool bnx2x_get_cam_offset_mac(struct bnx2x_vlan_mac_obj *o, int *offset)
  270. {
  271. struct bnx2x_credit_pool_obj *mp = o->macs_pool;
  272. WARN_ON(!mp);
  273. return mp->get_entry(mp, offset);
  274. }
  275. static bool bnx2x_get_credit_mac(struct bnx2x_vlan_mac_obj *o)
  276. {
  277. struct bnx2x_credit_pool_obj *mp = o->macs_pool;
  278. WARN_ON(!mp);
  279. return mp->get(mp, 1);
  280. }
  281. static bool bnx2x_get_cam_offset_vlan(struct bnx2x_vlan_mac_obj *o, int *offset)
  282. {
  283. struct bnx2x_credit_pool_obj *vp = o->vlans_pool;
  284. WARN_ON(!vp);
  285. return vp->get_entry(vp, offset);
  286. }
  287. static bool bnx2x_get_credit_vlan(struct bnx2x_vlan_mac_obj *o)
  288. {
  289. struct bnx2x_credit_pool_obj *vp = o->vlans_pool;
  290. WARN_ON(!vp);
  291. return vp->get(vp, 1);
  292. }
  293. static bool bnx2x_put_cam_offset_mac(struct bnx2x_vlan_mac_obj *o, int offset)
  294. {
  295. struct bnx2x_credit_pool_obj *mp = o->macs_pool;
  296. return mp->put_entry(mp, offset);
  297. }
  298. static bool bnx2x_put_credit_mac(struct bnx2x_vlan_mac_obj *o)
  299. {
  300. struct bnx2x_credit_pool_obj *mp = o->macs_pool;
  301. return mp->put(mp, 1);
  302. }
  303. static bool bnx2x_put_cam_offset_vlan(struct bnx2x_vlan_mac_obj *o, int offset)
  304. {
  305. struct bnx2x_credit_pool_obj *vp = o->vlans_pool;
  306. return vp->put_entry(vp, offset);
  307. }
  308. static bool bnx2x_put_credit_vlan(struct bnx2x_vlan_mac_obj *o)
  309. {
  310. struct bnx2x_credit_pool_obj *vp = o->vlans_pool;
  311. return vp->put(vp, 1);
  312. }
  313. /**
  314. * __bnx2x_vlan_mac_h_write_trylock - try getting the vlan mac writer lock
  315. *
  316. * @bp: device handle
  317. * @o: vlan_mac object
  318. *
  319. * @details: Non-blocking implementation; should be called under execution
  320. * queue lock.
  321. */
  322. static int __bnx2x_vlan_mac_h_write_trylock(struct bnx2x *bp,
  323. struct bnx2x_vlan_mac_obj *o)
  324. {
  325. if (o->head_reader) {
  326. DP(BNX2X_MSG_SP, "vlan_mac_lock writer - There are readers; Busy\n");
  327. return -EBUSY;
  328. }
  329. DP(BNX2X_MSG_SP, "vlan_mac_lock writer - Taken\n");
  330. return 0;
  331. }
  332. /**
  333. * __bnx2x_vlan_mac_h_exec_pending - execute step instead of a previous step
  334. *
  335. * @bp: device handle
  336. * @o: vlan_mac object
  337. *
  338. * @details Should be called under execution queue lock; notice it might release
  339. * and reclaim it during its run.
  340. */
  341. static void __bnx2x_vlan_mac_h_exec_pending(struct bnx2x *bp,
  342. struct bnx2x_vlan_mac_obj *o)
  343. {
  344. int rc;
  345. unsigned long ramrod_flags = o->saved_ramrod_flags;
  346. DP(BNX2X_MSG_SP, "vlan_mac_lock execute pending command with ramrod flags %lu\n",
  347. ramrod_flags);
  348. o->head_exe_request = false;
  349. o->saved_ramrod_flags = 0;
  350. rc = bnx2x_exe_queue_step(bp, &o->exe_queue, &ramrod_flags);
  351. if (rc != 0) {
  352. BNX2X_ERR("execution of pending commands failed with rc %d\n",
  353. rc);
  354. #ifdef BNX2X_STOP_ON_ERROR
  355. bnx2x_panic();
  356. #endif
  357. }
  358. }
  359. /**
  360. * __bnx2x_vlan_mac_h_pend - Pend an execution step which couldn't run
  361. *
  362. * @bp: device handle
  363. * @o: vlan_mac object
  364. * @ramrod_flags: ramrod flags of missed execution
  365. *
  366. * @details Should be called under execution queue lock.
  367. */
  368. static void __bnx2x_vlan_mac_h_pend(struct bnx2x *bp,
  369. struct bnx2x_vlan_mac_obj *o,
  370. unsigned long ramrod_flags)
  371. {
  372. o->head_exe_request = true;
  373. o->saved_ramrod_flags = ramrod_flags;
  374. DP(BNX2X_MSG_SP, "Placing pending execution with ramrod flags %lu\n",
  375. ramrod_flags);
  376. }
  377. /**
  378. * __bnx2x_vlan_mac_h_write_unlock - unlock the vlan mac head list writer lock
  379. *
  380. * @bp: device handle
  381. * @o: vlan_mac object
  382. *
  383. * @details Should be called under execution queue lock. Notice if a pending
  384. * execution exists, it would perform it - possibly releasing and
  385. * reclaiming the execution queue lock.
  386. */
  387. static void __bnx2x_vlan_mac_h_write_unlock(struct bnx2x *bp,
  388. struct bnx2x_vlan_mac_obj *o)
  389. {
  390. /* It's possible a new pending execution was added since this writer
  391. * executed. If so, execute again. [Ad infinitum]
  392. */
  393. while (o->head_exe_request) {
  394. DP(BNX2X_MSG_SP, "vlan_mac_lock - writer release encountered a pending request\n");
  395. __bnx2x_vlan_mac_h_exec_pending(bp, o);
  396. }
  397. }
  398. /**
  399. * __bnx2x_vlan_mac_h_read_lock - lock the vlan mac head list reader lock
  400. *
  401. * @bp: device handle
  402. * @o: vlan_mac object
  403. *
  404. * @details Should be called under the execution queue lock. May sleep. May
  405. * release and reclaim execution queue lock during its run.
  406. */
  407. static int __bnx2x_vlan_mac_h_read_lock(struct bnx2x *bp,
  408. struct bnx2x_vlan_mac_obj *o)
  409. {
  410. /* If we got here, we're holding lock --> no WRITER exists */
  411. o->head_reader++;
  412. DP(BNX2X_MSG_SP, "vlan_mac_lock - locked reader - number %d\n",
  413. o->head_reader);
  414. return 0;
  415. }
  416. /**
  417. * bnx2x_vlan_mac_h_read_lock - lock the vlan mac head list reader lock
  418. *
  419. * @bp: device handle
  420. * @o: vlan_mac object
  421. *
  422. * @details May sleep. Claims and releases execution queue lock during its run.
  423. */
  424. int bnx2x_vlan_mac_h_read_lock(struct bnx2x *bp,
  425. struct bnx2x_vlan_mac_obj *o)
  426. {
  427. int rc;
  428. spin_lock_bh(&o->exe_queue.lock);
  429. rc = __bnx2x_vlan_mac_h_read_lock(bp, o);
  430. spin_unlock_bh(&o->exe_queue.lock);
  431. return rc;
  432. }
  433. /**
  434. * __bnx2x_vlan_mac_h_read_unlock - unlock the vlan mac head list reader lock
  435. *
  436. * @bp: device handle
  437. * @o: vlan_mac object
  438. *
  439. * @details Should be called under execution queue lock. Notice if a pending
  440. * execution exists, it would be performed if this was the last
  441. * reader. possibly releasing and reclaiming the execution queue lock.
  442. */
  443. static void __bnx2x_vlan_mac_h_read_unlock(struct bnx2x *bp,
  444. struct bnx2x_vlan_mac_obj *o)
  445. {
  446. if (!o->head_reader) {
  447. BNX2X_ERR("Need to release vlan mac reader lock, but lock isn't taken\n");
  448. #ifdef BNX2X_STOP_ON_ERROR
  449. bnx2x_panic();
  450. #endif
  451. } else {
  452. o->head_reader--;
  453. DP(BNX2X_MSG_SP, "vlan_mac_lock - decreased readers to %d\n",
  454. o->head_reader);
  455. }
  456. /* It's possible a new pending execution was added, and that this reader
  457. * was last - if so we need to execute the command.
  458. */
  459. if (!o->head_reader && o->head_exe_request) {
  460. DP(BNX2X_MSG_SP, "vlan_mac_lock - reader release encountered a pending request\n");
  461. /* Writer release will do the trick */
  462. __bnx2x_vlan_mac_h_write_unlock(bp, o);
  463. }
  464. }
  465. /**
  466. * bnx2x_vlan_mac_h_read_unlock - unlock the vlan mac head list reader lock
  467. *
  468. * @bp: device handle
  469. * @o: vlan_mac object
  470. *
  471. * @details Notice if a pending execution exists, it would be performed if this
  472. * was the last reader. Claims and releases the execution queue lock
  473. * during its run.
  474. */
  475. void bnx2x_vlan_mac_h_read_unlock(struct bnx2x *bp,
  476. struct bnx2x_vlan_mac_obj *o)
  477. {
  478. spin_lock_bh(&o->exe_queue.lock);
  479. __bnx2x_vlan_mac_h_read_unlock(bp, o);
  480. spin_unlock_bh(&o->exe_queue.lock);
  481. }
  482. static int bnx2x_get_n_elements(struct bnx2x *bp, struct bnx2x_vlan_mac_obj *o,
  483. int n, u8 *base, u8 stride, u8 size)
  484. {
  485. struct bnx2x_vlan_mac_registry_elem *pos;
  486. u8 *next = base;
  487. int counter = 0;
  488. int read_lock;
  489. DP(BNX2X_MSG_SP, "get_n_elements - taking vlan_mac_lock (reader)\n");
  490. read_lock = bnx2x_vlan_mac_h_read_lock(bp, o);
  491. if (read_lock != 0)
  492. BNX2X_ERR("get_n_elements failed to get vlan mac reader lock; Access without lock\n");
  493. /* traverse list */
  494. list_for_each_entry(pos, &o->head, link) {
  495. if (counter < n) {
  496. memcpy(next, &pos->u, size);
  497. counter++;
  498. DP(BNX2X_MSG_SP, "copied element number %d to address %p element was:\n",
  499. counter, next);
  500. next += stride + size;
  501. }
  502. }
  503. if (read_lock == 0) {
  504. DP(BNX2X_MSG_SP, "get_n_elements - releasing vlan_mac_lock (reader)\n");
  505. bnx2x_vlan_mac_h_read_unlock(bp, o);
  506. }
  507. return counter * ETH_ALEN;
  508. }
  509. /* check_add() callbacks */
  510. static int bnx2x_check_mac_add(struct bnx2x *bp,
  511. struct bnx2x_vlan_mac_obj *o,
  512. union bnx2x_classification_ramrod_data *data)
  513. {
  514. struct bnx2x_vlan_mac_registry_elem *pos;
  515. DP(BNX2X_MSG_SP, "Checking MAC %pM for ADD command\n", data->mac.mac);
  516. if (!is_valid_ether_addr(data->mac.mac))
  517. return -EINVAL;
  518. /* Check if a requested MAC already exists */
  519. list_for_each_entry(pos, &o->head, link)
  520. if (ether_addr_equal(data->mac.mac, pos->u.mac.mac) &&
  521. (data->mac.is_inner_mac == pos->u.mac.is_inner_mac))
  522. return -EEXIST;
  523. return 0;
  524. }
  525. static int bnx2x_check_vlan_add(struct bnx2x *bp,
  526. struct bnx2x_vlan_mac_obj *o,
  527. union bnx2x_classification_ramrod_data *data)
  528. {
  529. struct bnx2x_vlan_mac_registry_elem *pos;
  530. DP(BNX2X_MSG_SP, "Checking VLAN %d for ADD command\n", data->vlan.vlan);
  531. list_for_each_entry(pos, &o->head, link)
  532. if (data->vlan.vlan == pos->u.vlan.vlan)
  533. return -EEXIST;
  534. return 0;
  535. }
  536. /* check_del() callbacks */
  537. static struct bnx2x_vlan_mac_registry_elem *
  538. bnx2x_check_mac_del(struct bnx2x *bp,
  539. struct bnx2x_vlan_mac_obj *o,
  540. union bnx2x_classification_ramrod_data *data)
  541. {
  542. struct bnx2x_vlan_mac_registry_elem *pos;
  543. DP(BNX2X_MSG_SP, "Checking MAC %pM for DEL command\n", data->mac.mac);
  544. list_for_each_entry(pos, &o->head, link)
  545. if (ether_addr_equal(data->mac.mac, pos->u.mac.mac) &&
  546. (data->mac.is_inner_mac == pos->u.mac.is_inner_mac))
  547. return pos;
  548. return NULL;
  549. }
  550. static struct bnx2x_vlan_mac_registry_elem *
  551. bnx2x_check_vlan_del(struct bnx2x *bp,
  552. struct bnx2x_vlan_mac_obj *o,
  553. union bnx2x_classification_ramrod_data *data)
  554. {
  555. struct bnx2x_vlan_mac_registry_elem *pos;
  556. DP(BNX2X_MSG_SP, "Checking VLAN %d for DEL command\n", data->vlan.vlan);
  557. list_for_each_entry(pos, &o->head, link)
  558. if (data->vlan.vlan == pos->u.vlan.vlan)
  559. return pos;
  560. return NULL;
  561. }
  562. /* check_move() callback */
  563. static bool bnx2x_check_move(struct bnx2x *bp,
  564. struct bnx2x_vlan_mac_obj *src_o,
  565. struct bnx2x_vlan_mac_obj *dst_o,
  566. union bnx2x_classification_ramrod_data *data)
  567. {
  568. struct bnx2x_vlan_mac_registry_elem *pos;
  569. int rc;
  570. /* Check if we can delete the requested configuration from the first
  571. * object.
  572. */
  573. pos = src_o->check_del(bp, src_o, data);
  574. /* check if configuration can be added */
  575. rc = dst_o->check_add(bp, dst_o, data);
  576. /* If this classification can not be added (is already set)
  577. * or can't be deleted - return an error.
  578. */
  579. if (rc || !pos)
  580. return false;
  581. return true;
  582. }
  583. static bool bnx2x_check_move_always_err(
  584. struct bnx2x *bp,
  585. struct bnx2x_vlan_mac_obj *src_o,
  586. struct bnx2x_vlan_mac_obj *dst_o,
  587. union bnx2x_classification_ramrod_data *data)
  588. {
  589. return false;
  590. }
  591. static inline u8 bnx2x_vlan_mac_get_rx_tx_flag(struct bnx2x_vlan_mac_obj *o)
  592. {
  593. struct bnx2x_raw_obj *raw = &o->raw;
  594. u8 rx_tx_flag = 0;
  595. if ((raw->obj_type == BNX2X_OBJ_TYPE_TX) ||
  596. (raw->obj_type == BNX2X_OBJ_TYPE_RX_TX))
  597. rx_tx_flag |= ETH_CLASSIFY_CMD_HEADER_TX_CMD;
  598. if ((raw->obj_type == BNX2X_OBJ_TYPE_RX) ||
  599. (raw->obj_type == BNX2X_OBJ_TYPE_RX_TX))
  600. rx_tx_flag |= ETH_CLASSIFY_CMD_HEADER_RX_CMD;
  601. return rx_tx_flag;
  602. }
  603. static void bnx2x_set_mac_in_nig(struct bnx2x *bp,
  604. bool add, unsigned char *dev_addr, int index)
  605. {
  606. u32 wb_data[2];
  607. u32 reg_offset = BP_PORT(bp) ? NIG_REG_LLH1_FUNC_MEM :
  608. NIG_REG_LLH0_FUNC_MEM;
  609. if (!IS_MF_SI(bp) && !IS_MF_AFEX(bp))
  610. return;
  611. if (index > BNX2X_LLH_CAM_MAX_PF_LINE)
  612. return;
  613. DP(BNX2X_MSG_SP, "Going to %s LLH configuration at entry %d\n",
  614. (add ? "ADD" : "DELETE"), index);
  615. if (add) {
  616. /* LLH_FUNC_MEM is a u64 WB register */
  617. reg_offset += 8*index;
  618. wb_data[0] = ((dev_addr[2] << 24) | (dev_addr[3] << 16) |
  619. (dev_addr[4] << 8) | dev_addr[5]);
  620. wb_data[1] = ((dev_addr[0] << 8) | dev_addr[1]);
  621. REG_WR_DMAE(bp, reg_offset, wb_data, 2);
  622. }
  623. REG_WR(bp, (BP_PORT(bp) ? NIG_REG_LLH1_FUNC_MEM_ENABLE :
  624. NIG_REG_LLH0_FUNC_MEM_ENABLE) + 4*index, add);
  625. }
  626. /**
  627. * bnx2x_vlan_mac_set_cmd_hdr_e2 - set a header in a single classify ramrod
  628. *
  629. * @bp: device handle
  630. * @o: queue for which we want to configure this rule
  631. * @add: if true the command is an ADD command, DEL otherwise
  632. * @opcode: CLASSIFY_RULE_OPCODE_XXX
  633. * @hdr: pointer to a header to setup
  634. *
  635. */
  636. static inline void bnx2x_vlan_mac_set_cmd_hdr_e2(struct bnx2x *bp,
  637. struct bnx2x_vlan_mac_obj *o, bool add, int opcode,
  638. struct eth_classify_cmd_header *hdr)
  639. {
  640. struct bnx2x_raw_obj *raw = &o->raw;
  641. hdr->client_id = raw->cl_id;
  642. hdr->func_id = raw->func_id;
  643. /* Rx or/and Tx (internal switching) configuration ? */
  644. hdr->cmd_general_data |=
  645. bnx2x_vlan_mac_get_rx_tx_flag(o);
  646. if (add)
  647. hdr->cmd_general_data |= ETH_CLASSIFY_CMD_HEADER_IS_ADD;
  648. hdr->cmd_general_data |=
  649. (opcode << ETH_CLASSIFY_CMD_HEADER_OPCODE_SHIFT);
  650. }
  651. /**
  652. * bnx2x_vlan_mac_set_rdata_hdr_e2 - set the classify ramrod data header
  653. *
  654. * @cid: connection id
  655. * @type: BNX2X_FILTER_XXX_PENDING
  656. * @hdr: pointer to header to setup
  657. * @rule_cnt:
  658. *
  659. * currently we always configure one rule and echo field to contain a CID and an
  660. * opcode type.
  661. */
  662. static inline void bnx2x_vlan_mac_set_rdata_hdr_e2(u32 cid, int type,
  663. struct eth_classify_header *hdr, int rule_cnt)
  664. {
  665. hdr->echo = cpu_to_le32((cid & BNX2X_SWCID_MASK) |
  666. (type << BNX2X_SWCID_SHIFT));
  667. hdr->rule_cnt = (u8)rule_cnt;
  668. }
  669. /* hw_config() callbacks */
  670. static void bnx2x_set_one_mac_e2(struct bnx2x *bp,
  671. struct bnx2x_vlan_mac_obj *o,
  672. struct bnx2x_exeq_elem *elem, int rule_idx,
  673. int cam_offset)
  674. {
  675. struct bnx2x_raw_obj *raw = &o->raw;
  676. struct eth_classify_rules_ramrod_data *data =
  677. (struct eth_classify_rules_ramrod_data *)(raw->rdata);
  678. int rule_cnt = rule_idx + 1, cmd = elem->cmd_data.vlan_mac.cmd;
  679. union eth_classify_rule_cmd *rule_entry = &data->rules[rule_idx];
  680. bool add = (cmd == BNX2X_VLAN_MAC_ADD) ? true : false;
  681. unsigned long *vlan_mac_flags = &elem->cmd_data.vlan_mac.vlan_mac_flags;
  682. u8 *mac = elem->cmd_data.vlan_mac.u.mac.mac;
  683. /* Set LLH CAM entry: currently only iSCSI and ETH macs are
  684. * relevant. In addition, current implementation is tuned for a
  685. * single ETH MAC.
  686. *
  687. * When multiple unicast ETH MACs PF configuration in switch
  688. * independent mode is required (NetQ, multiple netdev MACs,
  689. * etc.), consider better utilisation of 8 per function MAC
  690. * entries in the LLH register. There is also
  691. * NIG_REG_P[01]_LLH_FUNC_MEM2 registers that complete the
  692. * total number of CAM entries to 16.
  693. *
  694. * Currently we won't configure NIG for MACs other than a primary ETH
  695. * MAC and iSCSI L2 MAC.
  696. *
  697. * If this MAC is moving from one Queue to another, no need to change
  698. * NIG configuration.
  699. */
  700. if (cmd != BNX2X_VLAN_MAC_MOVE) {
  701. if (test_bit(BNX2X_ISCSI_ETH_MAC, vlan_mac_flags))
  702. bnx2x_set_mac_in_nig(bp, add, mac,
  703. BNX2X_LLH_CAM_ISCSI_ETH_LINE);
  704. else if (test_bit(BNX2X_ETH_MAC, vlan_mac_flags))
  705. bnx2x_set_mac_in_nig(bp, add, mac,
  706. BNX2X_LLH_CAM_ETH_LINE);
  707. }
  708. /* Reset the ramrod data buffer for the first rule */
  709. if (rule_idx == 0)
  710. memset(data, 0, sizeof(*data));
  711. /* Setup a command header */
  712. bnx2x_vlan_mac_set_cmd_hdr_e2(bp, o, add, CLASSIFY_RULE_OPCODE_MAC,
  713. &rule_entry->mac.header);
  714. DP(BNX2X_MSG_SP, "About to %s MAC %pM for Queue %d\n",
  715. (add ? "add" : "delete"), mac, raw->cl_id);
  716. /* Set a MAC itself */
  717. bnx2x_set_fw_mac_addr(&rule_entry->mac.mac_msb,
  718. &rule_entry->mac.mac_mid,
  719. &rule_entry->mac.mac_lsb, mac);
  720. rule_entry->mac.inner_mac =
  721. cpu_to_le16(elem->cmd_data.vlan_mac.u.mac.is_inner_mac);
  722. /* MOVE: Add a rule that will add this MAC to the target Queue */
  723. if (cmd == BNX2X_VLAN_MAC_MOVE) {
  724. rule_entry++;
  725. rule_cnt++;
  726. /* Setup ramrod data */
  727. bnx2x_vlan_mac_set_cmd_hdr_e2(bp,
  728. elem->cmd_data.vlan_mac.target_obj,
  729. true, CLASSIFY_RULE_OPCODE_MAC,
  730. &rule_entry->mac.header);
  731. /* Set a MAC itself */
  732. bnx2x_set_fw_mac_addr(&rule_entry->mac.mac_msb,
  733. &rule_entry->mac.mac_mid,
  734. &rule_entry->mac.mac_lsb, mac);
  735. rule_entry->mac.inner_mac =
  736. cpu_to_le16(elem->cmd_data.vlan_mac.
  737. u.mac.is_inner_mac);
  738. }
  739. /* Set the ramrod data header */
  740. /* TODO: take this to the higher level in order to prevent multiple
  741. writing */
  742. bnx2x_vlan_mac_set_rdata_hdr_e2(raw->cid, raw->state, &data->header,
  743. rule_cnt);
  744. }
  745. /**
  746. * bnx2x_vlan_mac_set_rdata_hdr_e1x - set a header in a single classify ramrod
  747. *
  748. * @bp: device handle
  749. * @o: queue
  750. * @type:
  751. * @cam_offset: offset in cam memory
  752. * @hdr: pointer to a header to setup
  753. *
  754. * E1/E1H
  755. */
  756. static inline void bnx2x_vlan_mac_set_rdata_hdr_e1x(struct bnx2x *bp,
  757. struct bnx2x_vlan_mac_obj *o, int type, int cam_offset,
  758. struct mac_configuration_hdr *hdr)
  759. {
  760. struct bnx2x_raw_obj *r = &o->raw;
  761. hdr->length = 1;
  762. hdr->offset = (u8)cam_offset;
  763. hdr->client_id = cpu_to_le16(0xff);
  764. hdr->echo = cpu_to_le32((r->cid & BNX2X_SWCID_MASK) |
  765. (type << BNX2X_SWCID_SHIFT));
  766. }
  767. static inline void bnx2x_vlan_mac_set_cfg_entry_e1x(struct bnx2x *bp,
  768. struct bnx2x_vlan_mac_obj *o, bool add, int opcode, u8 *mac,
  769. u16 vlan_id, struct mac_configuration_entry *cfg_entry)
  770. {
  771. struct bnx2x_raw_obj *r = &o->raw;
  772. u32 cl_bit_vec = (1 << r->cl_id);
  773. cfg_entry->clients_bit_vector = cpu_to_le32(cl_bit_vec);
  774. cfg_entry->pf_id = r->func_id;
  775. cfg_entry->vlan_id = cpu_to_le16(vlan_id);
  776. if (add) {
  777. SET_FLAG(cfg_entry->flags, MAC_CONFIGURATION_ENTRY_ACTION_TYPE,
  778. T_ETH_MAC_COMMAND_SET);
  779. SET_FLAG(cfg_entry->flags,
  780. MAC_CONFIGURATION_ENTRY_VLAN_FILTERING_MODE, opcode);
  781. /* Set a MAC in a ramrod data */
  782. bnx2x_set_fw_mac_addr(&cfg_entry->msb_mac_addr,
  783. &cfg_entry->middle_mac_addr,
  784. &cfg_entry->lsb_mac_addr, mac);
  785. } else
  786. SET_FLAG(cfg_entry->flags, MAC_CONFIGURATION_ENTRY_ACTION_TYPE,
  787. T_ETH_MAC_COMMAND_INVALIDATE);
  788. }
  789. static inline void bnx2x_vlan_mac_set_rdata_e1x(struct bnx2x *bp,
  790. struct bnx2x_vlan_mac_obj *o, int type, int cam_offset, bool add,
  791. u8 *mac, u16 vlan_id, int opcode, struct mac_configuration_cmd *config)
  792. {
  793. struct mac_configuration_entry *cfg_entry = &config->config_table[0];
  794. struct bnx2x_raw_obj *raw = &o->raw;
  795. bnx2x_vlan_mac_set_rdata_hdr_e1x(bp, o, type, cam_offset,
  796. &config->hdr);
  797. bnx2x_vlan_mac_set_cfg_entry_e1x(bp, o, add, opcode, mac, vlan_id,
  798. cfg_entry);
  799. DP(BNX2X_MSG_SP, "%s MAC %pM CLID %d CAM offset %d\n",
  800. (add ? "setting" : "clearing"),
  801. mac, raw->cl_id, cam_offset);
  802. }
  803. /**
  804. * bnx2x_set_one_mac_e1x - fill a single MAC rule ramrod data
  805. *
  806. * @bp: device handle
  807. * @o: bnx2x_vlan_mac_obj
  808. * @elem: bnx2x_exeq_elem
  809. * @rule_idx: rule_idx
  810. * @cam_offset: cam_offset
  811. */
  812. static void bnx2x_set_one_mac_e1x(struct bnx2x *bp,
  813. struct bnx2x_vlan_mac_obj *o,
  814. struct bnx2x_exeq_elem *elem, int rule_idx,
  815. int cam_offset)
  816. {
  817. struct bnx2x_raw_obj *raw = &o->raw;
  818. struct mac_configuration_cmd *config =
  819. (struct mac_configuration_cmd *)(raw->rdata);
  820. /* 57710 and 57711 do not support MOVE command,
  821. * so it's either ADD or DEL
  822. */
  823. bool add = (elem->cmd_data.vlan_mac.cmd == BNX2X_VLAN_MAC_ADD) ?
  824. true : false;
  825. /* Reset the ramrod data buffer */
  826. memset(config, 0, sizeof(*config));
  827. bnx2x_vlan_mac_set_rdata_e1x(bp, o, raw->state,
  828. cam_offset, add,
  829. elem->cmd_data.vlan_mac.u.mac.mac, 0,
  830. ETH_VLAN_FILTER_ANY_VLAN, config);
  831. }
  832. static void bnx2x_set_one_vlan_e2(struct bnx2x *bp,
  833. struct bnx2x_vlan_mac_obj *o,
  834. struct bnx2x_exeq_elem *elem, int rule_idx,
  835. int cam_offset)
  836. {
  837. struct bnx2x_raw_obj *raw = &o->raw;
  838. struct eth_classify_rules_ramrod_data *data =
  839. (struct eth_classify_rules_ramrod_data *)(raw->rdata);
  840. int rule_cnt = rule_idx + 1;
  841. union eth_classify_rule_cmd *rule_entry = &data->rules[rule_idx];
  842. enum bnx2x_vlan_mac_cmd cmd = elem->cmd_data.vlan_mac.cmd;
  843. bool add = (cmd == BNX2X_VLAN_MAC_ADD) ? true : false;
  844. u16 vlan = elem->cmd_data.vlan_mac.u.vlan.vlan;
  845. /* Reset the ramrod data buffer for the first rule */
  846. if (rule_idx == 0)
  847. memset(data, 0, sizeof(*data));
  848. /* Set a rule header */
  849. bnx2x_vlan_mac_set_cmd_hdr_e2(bp, o, add, CLASSIFY_RULE_OPCODE_VLAN,
  850. &rule_entry->vlan.header);
  851. DP(BNX2X_MSG_SP, "About to %s VLAN %d\n", (add ? "add" : "delete"),
  852. vlan);
  853. /* Set a VLAN itself */
  854. rule_entry->vlan.vlan = cpu_to_le16(vlan);
  855. /* MOVE: Add a rule that will add this MAC to the target Queue */
  856. if (cmd == BNX2X_VLAN_MAC_MOVE) {
  857. rule_entry++;
  858. rule_cnt++;
  859. /* Setup ramrod data */
  860. bnx2x_vlan_mac_set_cmd_hdr_e2(bp,
  861. elem->cmd_data.vlan_mac.target_obj,
  862. true, CLASSIFY_RULE_OPCODE_VLAN,
  863. &rule_entry->vlan.header);
  864. /* Set a VLAN itself */
  865. rule_entry->vlan.vlan = cpu_to_le16(vlan);
  866. }
  867. /* Set the ramrod data header */
  868. /* TODO: take this to the higher level in order to prevent multiple
  869. writing */
  870. bnx2x_vlan_mac_set_rdata_hdr_e2(raw->cid, raw->state, &data->header,
  871. rule_cnt);
  872. }
  873. /**
  874. * bnx2x_vlan_mac_restore - reconfigure next MAC/VLAN/VLAN-MAC element
  875. *
  876. * @bp: device handle
  877. * @p: command parameters
  878. * @ppos: pointer to the cookie
  879. *
  880. * reconfigure next MAC/VLAN/VLAN-MAC element from the
  881. * previously configured elements list.
  882. *
  883. * from command parameters only RAMROD_COMP_WAIT bit in ramrod_flags is taken
  884. * into an account
  885. *
  886. * pointer to the cookie - that should be given back in the next call to make
  887. * function handle the next element. If *ppos is set to NULL it will restart the
  888. * iterator. If returned *ppos == NULL this means that the last element has been
  889. * handled.
  890. *
  891. */
  892. static int bnx2x_vlan_mac_restore(struct bnx2x *bp,
  893. struct bnx2x_vlan_mac_ramrod_params *p,
  894. struct bnx2x_vlan_mac_registry_elem **ppos)
  895. {
  896. struct bnx2x_vlan_mac_registry_elem *pos;
  897. struct bnx2x_vlan_mac_obj *o = p->vlan_mac_obj;
  898. /* If list is empty - there is nothing to do here */
  899. if (list_empty(&o->head)) {
  900. *ppos = NULL;
  901. return 0;
  902. }
  903. /* make a step... */
  904. if (*ppos == NULL)
  905. *ppos = list_first_entry(&o->head,
  906. struct bnx2x_vlan_mac_registry_elem,
  907. link);
  908. else
  909. *ppos = list_next_entry(*ppos, link);
  910. pos = *ppos;
  911. /* If it's the last step - return NULL */
  912. if (list_is_last(&pos->link, &o->head))
  913. *ppos = NULL;
  914. /* Prepare a 'user_req' */
  915. memcpy(&p->user_req.u, &pos->u, sizeof(pos->u));
  916. /* Set the command */
  917. p->user_req.cmd = BNX2X_VLAN_MAC_ADD;
  918. /* Set vlan_mac_flags */
  919. p->user_req.vlan_mac_flags = pos->vlan_mac_flags;
  920. /* Set a restore bit */
  921. __set_bit(RAMROD_RESTORE, &p->ramrod_flags);
  922. return bnx2x_config_vlan_mac(bp, p);
  923. }
  924. /* bnx2x_exeq_get_mac/bnx2x_exeq_get_vlan/bnx2x_exeq_get_vlan_mac return a
  925. * pointer to an element with a specific criteria and NULL if such an element
  926. * hasn't been found.
  927. */
  928. static struct bnx2x_exeq_elem *bnx2x_exeq_get_mac(
  929. struct bnx2x_exe_queue_obj *o,
  930. struct bnx2x_exeq_elem *elem)
  931. {
  932. struct bnx2x_exeq_elem *pos;
  933. struct bnx2x_mac_ramrod_data *data = &elem->cmd_data.vlan_mac.u.mac;
  934. /* Check pending for execution commands */
  935. list_for_each_entry(pos, &o->exe_queue, link)
  936. if (!memcmp(&pos->cmd_data.vlan_mac.u.mac, data,
  937. sizeof(*data)) &&
  938. (pos->cmd_data.vlan_mac.cmd == elem->cmd_data.vlan_mac.cmd))
  939. return pos;
  940. return NULL;
  941. }
  942. static struct bnx2x_exeq_elem *bnx2x_exeq_get_vlan(
  943. struct bnx2x_exe_queue_obj *o,
  944. struct bnx2x_exeq_elem *elem)
  945. {
  946. struct bnx2x_exeq_elem *pos;
  947. struct bnx2x_vlan_ramrod_data *data = &elem->cmd_data.vlan_mac.u.vlan;
  948. /* Check pending for execution commands */
  949. list_for_each_entry(pos, &o->exe_queue, link)
  950. if (!memcmp(&pos->cmd_data.vlan_mac.u.vlan, data,
  951. sizeof(*data)) &&
  952. (pos->cmd_data.vlan_mac.cmd == elem->cmd_data.vlan_mac.cmd))
  953. return pos;
  954. return NULL;
  955. }
  956. /**
  957. * bnx2x_validate_vlan_mac_add - check if an ADD command can be executed
  958. *
  959. * @bp: device handle
  960. * @qo: bnx2x_qable_obj
  961. * @elem: bnx2x_exeq_elem
  962. *
  963. * Checks that the requested configuration can be added. If yes and if
  964. * requested, consume CAM credit.
  965. *
  966. * The 'validate' is run after the 'optimize'.
  967. *
  968. */
  969. static inline int bnx2x_validate_vlan_mac_add(struct bnx2x *bp,
  970. union bnx2x_qable_obj *qo,
  971. struct bnx2x_exeq_elem *elem)
  972. {
  973. struct bnx2x_vlan_mac_obj *o = &qo->vlan_mac;
  974. struct bnx2x_exe_queue_obj *exeq = &o->exe_queue;
  975. int rc;
  976. /* Check the registry */
  977. rc = o->check_add(bp, o, &elem->cmd_data.vlan_mac.u);
  978. if (rc) {
  979. DP(BNX2X_MSG_SP, "ADD command is not allowed considering current registry state.\n");
  980. return rc;
  981. }
  982. /* Check if there is a pending ADD command for this
  983. * MAC/VLAN/VLAN-MAC. Return an error if there is.
  984. */
  985. if (exeq->get(exeq, elem)) {
  986. DP(BNX2X_MSG_SP, "There is a pending ADD command already\n");
  987. return -EEXIST;
  988. }
  989. /* TODO: Check the pending MOVE from other objects where this
  990. * object is a destination object.
  991. */
  992. /* Consume the credit if not requested not to */
  993. if (!(test_bit(BNX2X_DONT_CONSUME_CAM_CREDIT,
  994. &elem->cmd_data.vlan_mac.vlan_mac_flags) ||
  995. o->get_credit(o)))
  996. return -EINVAL;
  997. return 0;
  998. }
  999. /**
  1000. * bnx2x_validate_vlan_mac_del - check if the DEL command can be executed
  1001. *
  1002. * @bp: device handle
  1003. * @qo: quable object to check
  1004. * @elem: element that needs to be deleted
  1005. *
  1006. * Checks that the requested configuration can be deleted. If yes and if
  1007. * requested, returns a CAM credit.
  1008. *
  1009. * The 'validate' is run after the 'optimize'.
  1010. */
  1011. static inline int bnx2x_validate_vlan_mac_del(struct bnx2x *bp,
  1012. union bnx2x_qable_obj *qo,
  1013. struct bnx2x_exeq_elem *elem)
  1014. {
  1015. struct bnx2x_vlan_mac_obj *o = &qo->vlan_mac;
  1016. struct bnx2x_vlan_mac_registry_elem *pos;
  1017. struct bnx2x_exe_queue_obj *exeq = &o->exe_queue;
  1018. struct bnx2x_exeq_elem query_elem;
  1019. /* If this classification can not be deleted (doesn't exist)
  1020. * - return a BNX2X_EXIST.
  1021. */
  1022. pos = o->check_del(bp, o, &elem->cmd_data.vlan_mac.u);
  1023. if (!pos) {
  1024. DP(BNX2X_MSG_SP, "DEL command is not allowed considering current registry state\n");
  1025. return -EEXIST;
  1026. }
  1027. /* Check if there are pending DEL or MOVE commands for this
  1028. * MAC/VLAN/VLAN-MAC. Return an error if so.
  1029. */
  1030. memcpy(&query_elem, elem, sizeof(query_elem));
  1031. /* Check for MOVE commands */
  1032. query_elem.cmd_data.vlan_mac.cmd = BNX2X_VLAN_MAC_MOVE;
  1033. if (exeq->get(exeq, &query_elem)) {
  1034. BNX2X_ERR("There is a pending MOVE command already\n");
  1035. return -EINVAL;
  1036. }
  1037. /* Check for DEL commands */
  1038. if (exeq->get(exeq, elem)) {
  1039. DP(BNX2X_MSG_SP, "There is a pending DEL command already\n");
  1040. return -EEXIST;
  1041. }
  1042. /* Return the credit to the credit pool if not requested not to */
  1043. if (!(test_bit(BNX2X_DONT_CONSUME_CAM_CREDIT,
  1044. &elem->cmd_data.vlan_mac.vlan_mac_flags) ||
  1045. o->put_credit(o))) {
  1046. BNX2X_ERR("Failed to return a credit\n");
  1047. return -EINVAL;
  1048. }
  1049. return 0;
  1050. }
  1051. /**
  1052. * bnx2x_validate_vlan_mac_move - check if the MOVE command can be executed
  1053. *
  1054. * @bp: device handle
  1055. * @qo: quable object to check (source)
  1056. * @elem: element that needs to be moved
  1057. *
  1058. * Checks that the requested configuration can be moved. If yes and if
  1059. * requested, returns a CAM credit.
  1060. *
  1061. * The 'validate' is run after the 'optimize'.
  1062. */
  1063. static inline int bnx2x_validate_vlan_mac_move(struct bnx2x *bp,
  1064. union bnx2x_qable_obj *qo,
  1065. struct bnx2x_exeq_elem *elem)
  1066. {
  1067. struct bnx2x_vlan_mac_obj *src_o = &qo->vlan_mac;
  1068. struct bnx2x_vlan_mac_obj *dest_o = elem->cmd_data.vlan_mac.target_obj;
  1069. struct bnx2x_exeq_elem query_elem;
  1070. struct bnx2x_exe_queue_obj *src_exeq = &src_o->exe_queue;
  1071. struct bnx2x_exe_queue_obj *dest_exeq = &dest_o->exe_queue;
  1072. /* Check if we can perform this operation based on the current registry
  1073. * state.
  1074. */
  1075. if (!src_o->check_move(bp, src_o, dest_o,
  1076. &elem->cmd_data.vlan_mac.u)) {
  1077. DP(BNX2X_MSG_SP, "MOVE command is not allowed considering current registry state\n");
  1078. return -EINVAL;
  1079. }
  1080. /* Check if there is an already pending DEL or MOVE command for the
  1081. * source object or ADD command for a destination object. Return an
  1082. * error if so.
  1083. */
  1084. memcpy(&query_elem, elem, sizeof(query_elem));
  1085. /* Check DEL on source */
  1086. query_elem.cmd_data.vlan_mac.cmd = BNX2X_VLAN_MAC_DEL;
  1087. if (src_exeq->get(src_exeq, &query_elem)) {
  1088. BNX2X_ERR("There is a pending DEL command on the source queue already\n");
  1089. return -EINVAL;
  1090. }
  1091. /* Check MOVE on source */
  1092. if (src_exeq->get(src_exeq, elem)) {
  1093. DP(BNX2X_MSG_SP, "There is a pending MOVE command already\n");
  1094. return -EEXIST;
  1095. }
  1096. /* Check ADD on destination */
  1097. query_elem.cmd_data.vlan_mac.cmd = BNX2X_VLAN_MAC_ADD;
  1098. if (dest_exeq->get(dest_exeq, &query_elem)) {
  1099. BNX2X_ERR("There is a pending ADD command on the destination queue already\n");
  1100. return -EINVAL;
  1101. }
  1102. /* Consume the credit if not requested not to */
  1103. if (!(test_bit(BNX2X_DONT_CONSUME_CAM_CREDIT_DEST,
  1104. &elem->cmd_data.vlan_mac.vlan_mac_flags) ||
  1105. dest_o->get_credit(dest_o)))
  1106. return -EINVAL;
  1107. if (!(test_bit(BNX2X_DONT_CONSUME_CAM_CREDIT,
  1108. &elem->cmd_data.vlan_mac.vlan_mac_flags) ||
  1109. src_o->put_credit(src_o))) {
  1110. /* return the credit taken from dest... */
  1111. dest_o->put_credit(dest_o);
  1112. return -EINVAL;
  1113. }
  1114. return 0;
  1115. }
  1116. static int bnx2x_validate_vlan_mac(struct bnx2x *bp,
  1117. union bnx2x_qable_obj *qo,
  1118. struct bnx2x_exeq_elem *elem)
  1119. {
  1120. switch (elem->cmd_data.vlan_mac.cmd) {
  1121. case BNX2X_VLAN_MAC_ADD:
  1122. return bnx2x_validate_vlan_mac_add(bp, qo, elem);
  1123. case BNX2X_VLAN_MAC_DEL:
  1124. return bnx2x_validate_vlan_mac_del(bp, qo, elem);
  1125. case BNX2X_VLAN_MAC_MOVE:
  1126. return bnx2x_validate_vlan_mac_move(bp, qo, elem);
  1127. default:
  1128. return -EINVAL;
  1129. }
  1130. }
  1131. static int bnx2x_remove_vlan_mac(struct bnx2x *bp,
  1132. union bnx2x_qable_obj *qo,
  1133. struct bnx2x_exeq_elem *elem)
  1134. {
  1135. int rc = 0;
  1136. /* If consumption wasn't required, nothing to do */
  1137. if (test_bit(BNX2X_DONT_CONSUME_CAM_CREDIT,
  1138. &elem->cmd_data.vlan_mac.vlan_mac_flags))
  1139. return 0;
  1140. switch (elem->cmd_data.vlan_mac.cmd) {
  1141. case BNX2X_VLAN_MAC_ADD:
  1142. case BNX2X_VLAN_MAC_MOVE:
  1143. rc = qo->vlan_mac.put_credit(&qo->vlan_mac);
  1144. break;
  1145. case BNX2X_VLAN_MAC_DEL:
  1146. rc = qo->vlan_mac.get_credit(&qo->vlan_mac);
  1147. break;
  1148. default:
  1149. return -EINVAL;
  1150. }
  1151. if (rc != true)
  1152. return -EINVAL;
  1153. return 0;
  1154. }
  1155. /**
  1156. * bnx2x_wait_vlan_mac - passively wait for 5 seconds until all work completes.
  1157. *
  1158. * @bp: device handle
  1159. * @o: bnx2x_vlan_mac_obj
  1160. *
  1161. */
  1162. static int bnx2x_wait_vlan_mac(struct bnx2x *bp,
  1163. struct bnx2x_vlan_mac_obj *o)
  1164. {
  1165. int cnt = 5000, rc;
  1166. struct bnx2x_exe_queue_obj *exeq = &o->exe_queue;
  1167. struct bnx2x_raw_obj *raw = &o->raw;
  1168. while (cnt--) {
  1169. /* Wait for the current command to complete */
  1170. rc = raw->wait_comp(bp, raw);
  1171. if (rc)
  1172. return rc;
  1173. /* Wait until there are no pending commands */
  1174. if (!bnx2x_exe_queue_empty(exeq))
  1175. usleep_range(1000, 2000);
  1176. else
  1177. return 0;
  1178. }
  1179. return -EBUSY;
  1180. }
  1181. static int __bnx2x_vlan_mac_execute_step(struct bnx2x *bp,
  1182. struct bnx2x_vlan_mac_obj *o,
  1183. unsigned long *ramrod_flags)
  1184. {
  1185. int rc = 0;
  1186. spin_lock_bh(&o->exe_queue.lock);
  1187. DP(BNX2X_MSG_SP, "vlan_mac_execute_step - trying to take writer lock\n");
  1188. rc = __bnx2x_vlan_mac_h_write_trylock(bp, o);
  1189. if (rc != 0) {
  1190. __bnx2x_vlan_mac_h_pend(bp, o, *ramrod_flags);
  1191. /* Calling function should not diffrentiate between this case
  1192. * and the case in which there is already a pending ramrod
  1193. */
  1194. rc = 1;
  1195. } else {
  1196. rc = bnx2x_exe_queue_step(bp, &o->exe_queue, ramrod_flags);
  1197. }
  1198. spin_unlock_bh(&o->exe_queue.lock);
  1199. return rc;
  1200. }
  1201. /**
  1202. * bnx2x_complete_vlan_mac - complete one VLAN-MAC ramrod
  1203. *
  1204. * @bp: device handle
  1205. * @o: bnx2x_vlan_mac_obj
  1206. * @cqe:
  1207. * @cont: if true schedule next execution chunk
  1208. *
  1209. */
  1210. static int bnx2x_complete_vlan_mac(struct bnx2x *bp,
  1211. struct bnx2x_vlan_mac_obj *o,
  1212. union event_ring_elem *cqe,
  1213. unsigned long *ramrod_flags)
  1214. {
  1215. struct bnx2x_raw_obj *r = &o->raw;
  1216. int rc;
  1217. /* Clearing the pending list & raw state should be made
  1218. * atomically (as execution flow assumes they represent the same).
  1219. */
  1220. spin_lock_bh(&o->exe_queue.lock);
  1221. /* Reset pending list */
  1222. __bnx2x_exe_queue_reset_pending(bp, &o->exe_queue);
  1223. /* Clear pending */
  1224. r->clear_pending(r);
  1225. spin_unlock_bh(&o->exe_queue.lock);
  1226. /* If ramrod failed this is most likely a SW bug */
  1227. if (cqe->message.error)
  1228. return -EINVAL;
  1229. /* Run the next bulk of pending commands if requested */
  1230. if (test_bit(RAMROD_CONT, ramrod_flags)) {
  1231. rc = __bnx2x_vlan_mac_execute_step(bp, o, ramrod_flags);
  1232. if (rc < 0)
  1233. return rc;
  1234. }
  1235. /* If there is more work to do return PENDING */
  1236. if (!bnx2x_exe_queue_empty(&o->exe_queue))
  1237. return 1;
  1238. return 0;
  1239. }
  1240. /**
  1241. * bnx2x_optimize_vlan_mac - optimize ADD and DEL commands.
  1242. *
  1243. * @bp: device handle
  1244. * @o: bnx2x_qable_obj
  1245. * @elem: bnx2x_exeq_elem
  1246. */
  1247. static int bnx2x_optimize_vlan_mac(struct bnx2x *bp,
  1248. union bnx2x_qable_obj *qo,
  1249. struct bnx2x_exeq_elem *elem)
  1250. {
  1251. struct bnx2x_exeq_elem query, *pos;
  1252. struct bnx2x_vlan_mac_obj *o = &qo->vlan_mac;
  1253. struct bnx2x_exe_queue_obj *exeq = &o->exe_queue;
  1254. memcpy(&query, elem, sizeof(query));
  1255. switch (elem->cmd_data.vlan_mac.cmd) {
  1256. case BNX2X_VLAN_MAC_ADD:
  1257. query.cmd_data.vlan_mac.cmd = BNX2X_VLAN_MAC_DEL;
  1258. break;
  1259. case BNX2X_VLAN_MAC_DEL:
  1260. query.cmd_data.vlan_mac.cmd = BNX2X_VLAN_MAC_ADD;
  1261. break;
  1262. default:
  1263. /* Don't handle anything other than ADD or DEL */
  1264. return 0;
  1265. }
  1266. /* If we found the appropriate element - delete it */
  1267. pos = exeq->get(exeq, &query);
  1268. if (pos) {
  1269. /* Return the credit of the optimized command */
  1270. if (!test_bit(BNX2X_DONT_CONSUME_CAM_CREDIT,
  1271. &pos->cmd_data.vlan_mac.vlan_mac_flags)) {
  1272. if ((query.cmd_data.vlan_mac.cmd ==
  1273. BNX2X_VLAN_MAC_ADD) && !o->put_credit(o)) {
  1274. BNX2X_ERR("Failed to return the credit for the optimized ADD command\n");
  1275. return -EINVAL;
  1276. } else if (!o->get_credit(o)) { /* VLAN_MAC_DEL */
  1277. BNX2X_ERR("Failed to recover the credit from the optimized DEL command\n");
  1278. return -EINVAL;
  1279. }
  1280. }
  1281. DP(BNX2X_MSG_SP, "Optimizing %s command\n",
  1282. (elem->cmd_data.vlan_mac.cmd == BNX2X_VLAN_MAC_ADD) ?
  1283. "ADD" : "DEL");
  1284. list_del(&pos->link);
  1285. bnx2x_exe_queue_free_elem(bp, pos);
  1286. return 1;
  1287. }
  1288. return 0;
  1289. }
  1290. /**
  1291. * bnx2x_vlan_mac_get_registry_elem - prepare a registry element
  1292. *
  1293. * @bp: device handle
  1294. * @o:
  1295. * @elem:
  1296. * @restore:
  1297. * @re:
  1298. *
  1299. * prepare a registry element according to the current command request.
  1300. */
  1301. static inline int bnx2x_vlan_mac_get_registry_elem(
  1302. struct bnx2x *bp,
  1303. struct bnx2x_vlan_mac_obj *o,
  1304. struct bnx2x_exeq_elem *elem,
  1305. bool restore,
  1306. struct bnx2x_vlan_mac_registry_elem **re)
  1307. {
  1308. enum bnx2x_vlan_mac_cmd cmd = elem->cmd_data.vlan_mac.cmd;
  1309. struct bnx2x_vlan_mac_registry_elem *reg_elem;
  1310. /* Allocate a new registry element if needed. */
  1311. if (!restore &&
  1312. ((cmd == BNX2X_VLAN_MAC_ADD) || (cmd == BNX2X_VLAN_MAC_MOVE))) {
  1313. reg_elem = kzalloc(sizeof(*reg_elem), GFP_ATOMIC);
  1314. if (!reg_elem)
  1315. return -ENOMEM;
  1316. /* Get a new CAM offset */
  1317. if (!o->get_cam_offset(o, &reg_elem->cam_offset)) {
  1318. /* This shall never happen, because we have checked the
  1319. * CAM availability in the 'validate'.
  1320. */
  1321. WARN_ON(1);
  1322. kfree(reg_elem);
  1323. return -EINVAL;
  1324. }
  1325. DP(BNX2X_MSG_SP, "Got cam offset %d\n", reg_elem->cam_offset);
  1326. /* Set a VLAN-MAC data */
  1327. memcpy(&reg_elem->u, &elem->cmd_data.vlan_mac.u,
  1328. sizeof(reg_elem->u));
  1329. /* Copy the flags (needed for DEL and RESTORE flows) */
  1330. reg_elem->vlan_mac_flags =
  1331. elem->cmd_data.vlan_mac.vlan_mac_flags;
  1332. } else /* DEL, RESTORE */
  1333. reg_elem = o->check_del(bp, o, &elem->cmd_data.vlan_mac.u);
  1334. *re = reg_elem;
  1335. return 0;
  1336. }
  1337. /**
  1338. * bnx2x_execute_vlan_mac - execute vlan mac command
  1339. *
  1340. * @bp: device handle
  1341. * @qo:
  1342. * @exe_chunk:
  1343. * @ramrod_flags:
  1344. *
  1345. * go and send a ramrod!
  1346. */
  1347. static int bnx2x_execute_vlan_mac(struct bnx2x *bp,
  1348. union bnx2x_qable_obj *qo,
  1349. struct list_head *exe_chunk,
  1350. unsigned long *ramrod_flags)
  1351. {
  1352. struct bnx2x_exeq_elem *elem;
  1353. struct bnx2x_vlan_mac_obj *o = &qo->vlan_mac, *cam_obj;
  1354. struct bnx2x_raw_obj *r = &o->raw;
  1355. int rc, idx = 0;
  1356. bool restore = test_bit(RAMROD_RESTORE, ramrod_flags);
  1357. bool drv_only = test_bit(RAMROD_DRV_CLR_ONLY, ramrod_flags);
  1358. struct bnx2x_vlan_mac_registry_elem *reg_elem;
  1359. enum bnx2x_vlan_mac_cmd cmd;
  1360. /* If DRIVER_ONLY execution is requested, cleanup a registry
  1361. * and exit. Otherwise send a ramrod to FW.
  1362. */
  1363. if (!drv_only) {
  1364. WARN_ON(r->check_pending(r));
  1365. /* Set pending */
  1366. r->set_pending(r);
  1367. /* Fill the ramrod data */
  1368. list_for_each_entry(elem, exe_chunk, link) {
  1369. cmd = elem->cmd_data.vlan_mac.cmd;
  1370. /* We will add to the target object in MOVE command, so
  1371. * change the object for a CAM search.
  1372. */
  1373. if (cmd == BNX2X_VLAN_MAC_MOVE)
  1374. cam_obj = elem->cmd_data.vlan_mac.target_obj;
  1375. else
  1376. cam_obj = o;
  1377. rc = bnx2x_vlan_mac_get_registry_elem(bp, cam_obj,
  1378. elem, restore,
  1379. &reg_elem);
  1380. if (rc)
  1381. goto error_exit;
  1382. WARN_ON(!reg_elem);
  1383. /* Push a new entry into the registry */
  1384. if (!restore &&
  1385. ((cmd == BNX2X_VLAN_MAC_ADD) ||
  1386. (cmd == BNX2X_VLAN_MAC_MOVE)))
  1387. list_add(&reg_elem->link, &cam_obj->head);
  1388. /* Configure a single command in a ramrod data buffer */
  1389. o->set_one_rule(bp, o, elem, idx,
  1390. reg_elem->cam_offset);
  1391. /* MOVE command consumes 2 entries in the ramrod data */
  1392. if (cmd == BNX2X_VLAN_MAC_MOVE)
  1393. idx += 2;
  1394. else
  1395. idx++;
  1396. }
  1397. /* No need for an explicit memory barrier here as long we would
  1398. * need to ensure the ordering of writing to the SPQ element
  1399. * and updating of the SPQ producer which involves a memory
  1400. * read and we will have to put a full memory barrier there
  1401. * (inside bnx2x_sp_post()).
  1402. */
  1403. rc = bnx2x_sp_post(bp, o->ramrod_cmd, r->cid,
  1404. U64_HI(r->rdata_mapping),
  1405. U64_LO(r->rdata_mapping),
  1406. ETH_CONNECTION_TYPE);
  1407. if (rc)
  1408. goto error_exit;
  1409. }
  1410. /* Now, when we are done with the ramrod - clean up the registry */
  1411. list_for_each_entry(elem, exe_chunk, link) {
  1412. cmd = elem->cmd_data.vlan_mac.cmd;
  1413. if ((cmd == BNX2X_VLAN_MAC_DEL) ||
  1414. (cmd == BNX2X_VLAN_MAC_MOVE)) {
  1415. reg_elem = o->check_del(bp, o,
  1416. &elem->cmd_data.vlan_mac.u);
  1417. WARN_ON(!reg_elem);
  1418. o->put_cam_offset(o, reg_elem->cam_offset);
  1419. list_del(&reg_elem->link);
  1420. kfree(reg_elem);
  1421. }
  1422. }
  1423. if (!drv_only)
  1424. return 1;
  1425. else
  1426. return 0;
  1427. error_exit:
  1428. r->clear_pending(r);
  1429. /* Cleanup a registry in case of a failure */
  1430. list_for_each_entry(elem, exe_chunk, link) {
  1431. cmd = elem->cmd_data.vlan_mac.cmd;
  1432. if (cmd == BNX2X_VLAN_MAC_MOVE)
  1433. cam_obj = elem->cmd_data.vlan_mac.target_obj;
  1434. else
  1435. cam_obj = o;
  1436. /* Delete all newly added above entries */
  1437. if (!restore &&
  1438. ((cmd == BNX2X_VLAN_MAC_ADD) ||
  1439. (cmd == BNX2X_VLAN_MAC_MOVE))) {
  1440. reg_elem = o->check_del(bp, cam_obj,
  1441. &elem->cmd_data.vlan_mac.u);
  1442. if (reg_elem) {
  1443. list_del(&reg_elem->link);
  1444. kfree(reg_elem);
  1445. }
  1446. }
  1447. }
  1448. return rc;
  1449. }
  1450. static inline int bnx2x_vlan_mac_push_new_cmd(
  1451. struct bnx2x *bp,
  1452. struct bnx2x_vlan_mac_ramrod_params *p)
  1453. {
  1454. struct bnx2x_exeq_elem *elem;
  1455. struct bnx2x_vlan_mac_obj *o = p->vlan_mac_obj;
  1456. bool restore = test_bit(RAMROD_RESTORE, &p->ramrod_flags);
  1457. /* Allocate the execution queue element */
  1458. elem = bnx2x_exe_queue_alloc_elem(bp);
  1459. if (!elem)
  1460. return -ENOMEM;
  1461. /* Set the command 'length' */
  1462. switch (p->user_req.cmd) {
  1463. case BNX2X_VLAN_MAC_MOVE:
  1464. elem->cmd_len = 2;
  1465. break;
  1466. default:
  1467. elem->cmd_len = 1;
  1468. }
  1469. /* Fill the object specific info */
  1470. memcpy(&elem->cmd_data.vlan_mac, &p->user_req, sizeof(p->user_req));
  1471. /* Try to add a new command to the pending list */
  1472. return bnx2x_exe_queue_add(bp, &o->exe_queue, elem, restore);
  1473. }
  1474. /**
  1475. * bnx2x_config_vlan_mac - configure VLAN/MAC/VLAN_MAC filtering rules.
  1476. *
  1477. * @bp: device handle
  1478. * @p:
  1479. *
  1480. */
  1481. int bnx2x_config_vlan_mac(struct bnx2x *bp,
  1482. struct bnx2x_vlan_mac_ramrod_params *p)
  1483. {
  1484. int rc = 0;
  1485. struct bnx2x_vlan_mac_obj *o = p->vlan_mac_obj;
  1486. unsigned long *ramrod_flags = &p->ramrod_flags;
  1487. bool cont = test_bit(RAMROD_CONT, ramrod_flags);
  1488. struct bnx2x_raw_obj *raw = &o->raw;
  1489. /*
  1490. * Add new elements to the execution list for commands that require it.
  1491. */
  1492. if (!cont) {
  1493. rc = bnx2x_vlan_mac_push_new_cmd(bp, p);
  1494. if (rc)
  1495. return rc;
  1496. }
  1497. /* If nothing will be executed further in this iteration we want to
  1498. * return PENDING if there are pending commands
  1499. */
  1500. if (!bnx2x_exe_queue_empty(&o->exe_queue))
  1501. rc = 1;
  1502. if (test_bit(RAMROD_DRV_CLR_ONLY, ramrod_flags)) {
  1503. DP(BNX2X_MSG_SP, "RAMROD_DRV_CLR_ONLY requested: clearing a pending bit.\n");
  1504. raw->clear_pending(raw);
  1505. }
  1506. /* Execute commands if required */
  1507. if (cont || test_bit(RAMROD_EXEC, ramrod_flags) ||
  1508. test_bit(RAMROD_COMP_WAIT, ramrod_flags)) {
  1509. rc = __bnx2x_vlan_mac_execute_step(bp, p->vlan_mac_obj,
  1510. &p->ramrod_flags);
  1511. if (rc < 0)
  1512. return rc;
  1513. }
  1514. /* RAMROD_COMP_WAIT is a superset of RAMROD_EXEC. If it was set
  1515. * then user want to wait until the last command is done.
  1516. */
  1517. if (test_bit(RAMROD_COMP_WAIT, &p->ramrod_flags)) {
  1518. /* Wait maximum for the current exe_queue length iterations plus
  1519. * one (for the current pending command).
  1520. */
  1521. int max_iterations = bnx2x_exe_queue_length(&o->exe_queue) + 1;
  1522. while (!bnx2x_exe_queue_empty(&o->exe_queue) &&
  1523. max_iterations--) {
  1524. /* Wait for the current command to complete */
  1525. rc = raw->wait_comp(bp, raw);
  1526. if (rc)
  1527. return rc;
  1528. /* Make a next step */
  1529. rc = __bnx2x_vlan_mac_execute_step(bp,
  1530. p->vlan_mac_obj,
  1531. &p->ramrod_flags);
  1532. if (rc < 0)
  1533. return rc;
  1534. }
  1535. return 0;
  1536. }
  1537. return rc;
  1538. }
  1539. /**
  1540. * bnx2x_vlan_mac_del_all - delete elements with given vlan_mac_flags spec
  1541. *
  1542. * @bp: device handle
  1543. * @o:
  1544. * @vlan_mac_flags:
  1545. * @ramrod_flags: execution flags to be used for this deletion
  1546. *
  1547. * if the last operation has completed successfully and there are no
  1548. * more elements left, positive value if the last operation has completed
  1549. * successfully and there are more previously configured elements, negative
  1550. * value is current operation has failed.
  1551. */
  1552. static int bnx2x_vlan_mac_del_all(struct bnx2x *bp,
  1553. struct bnx2x_vlan_mac_obj *o,
  1554. unsigned long *vlan_mac_flags,
  1555. unsigned long *ramrod_flags)
  1556. {
  1557. struct bnx2x_vlan_mac_registry_elem *pos = NULL;
  1558. struct bnx2x_vlan_mac_ramrod_params p;
  1559. struct bnx2x_exe_queue_obj *exeq = &o->exe_queue;
  1560. struct bnx2x_exeq_elem *exeq_pos, *exeq_pos_n;
  1561. unsigned long flags;
  1562. int read_lock;
  1563. int rc = 0;
  1564. /* Clear pending commands first */
  1565. spin_lock_bh(&exeq->lock);
  1566. list_for_each_entry_safe(exeq_pos, exeq_pos_n, &exeq->exe_queue, link) {
  1567. flags = exeq_pos->cmd_data.vlan_mac.vlan_mac_flags;
  1568. if (BNX2X_VLAN_MAC_CMP_FLAGS(flags) ==
  1569. BNX2X_VLAN_MAC_CMP_FLAGS(*vlan_mac_flags)) {
  1570. rc = exeq->remove(bp, exeq->owner, exeq_pos);
  1571. if (rc) {
  1572. BNX2X_ERR("Failed to remove command\n");
  1573. spin_unlock_bh(&exeq->lock);
  1574. return rc;
  1575. }
  1576. list_del(&exeq_pos->link);
  1577. bnx2x_exe_queue_free_elem(bp, exeq_pos);
  1578. }
  1579. }
  1580. spin_unlock_bh(&exeq->lock);
  1581. /* Prepare a command request */
  1582. memset(&p, 0, sizeof(p));
  1583. p.vlan_mac_obj = o;
  1584. p.ramrod_flags = *ramrod_flags;
  1585. p.user_req.cmd = BNX2X_VLAN_MAC_DEL;
  1586. /* Add all but the last VLAN-MAC to the execution queue without actually
  1587. * execution anything.
  1588. */
  1589. __clear_bit(RAMROD_COMP_WAIT, &p.ramrod_flags);
  1590. __clear_bit(RAMROD_EXEC, &p.ramrod_flags);
  1591. __clear_bit(RAMROD_CONT, &p.ramrod_flags);
  1592. DP(BNX2X_MSG_SP, "vlan_mac_del_all -- taking vlan_mac_lock (reader)\n");
  1593. read_lock = bnx2x_vlan_mac_h_read_lock(bp, o);
  1594. if (read_lock != 0)
  1595. return read_lock;
  1596. list_for_each_entry(pos, &o->head, link) {
  1597. flags = pos->vlan_mac_flags;
  1598. if (BNX2X_VLAN_MAC_CMP_FLAGS(flags) ==
  1599. BNX2X_VLAN_MAC_CMP_FLAGS(*vlan_mac_flags)) {
  1600. p.user_req.vlan_mac_flags = pos->vlan_mac_flags;
  1601. memcpy(&p.user_req.u, &pos->u, sizeof(pos->u));
  1602. rc = bnx2x_config_vlan_mac(bp, &p);
  1603. if (rc < 0) {
  1604. BNX2X_ERR("Failed to add a new DEL command\n");
  1605. bnx2x_vlan_mac_h_read_unlock(bp, o);
  1606. return rc;
  1607. }
  1608. }
  1609. }
  1610. DP(BNX2X_MSG_SP, "vlan_mac_del_all -- releasing vlan_mac_lock (reader)\n");
  1611. bnx2x_vlan_mac_h_read_unlock(bp, o);
  1612. p.ramrod_flags = *ramrod_flags;
  1613. __set_bit(RAMROD_CONT, &p.ramrod_flags);
  1614. return bnx2x_config_vlan_mac(bp, &p);
  1615. }
  1616. static inline void bnx2x_init_raw_obj(struct bnx2x_raw_obj *raw, u8 cl_id,
  1617. u32 cid, u8 func_id, void *rdata, dma_addr_t rdata_mapping, int state,
  1618. unsigned long *pstate, bnx2x_obj_type type)
  1619. {
  1620. raw->func_id = func_id;
  1621. raw->cid = cid;
  1622. raw->cl_id = cl_id;
  1623. raw->rdata = rdata;
  1624. raw->rdata_mapping = rdata_mapping;
  1625. raw->state = state;
  1626. raw->pstate = pstate;
  1627. raw->obj_type = type;
  1628. raw->check_pending = bnx2x_raw_check_pending;
  1629. raw->clear_pending = bnx2x_raw_clear_pending;
  1630. raw->set_pending = bnx2x_raw_set_pending;
  1631. raw->wait_comp = bnx2x_raw_wait;
  1632. }
  1633. static inline void bnx2x_init_vlan_mac_common(struct bnx2x_vlan_mac_obj *o,
  1634. u8 cl_id, u32 cid, u8 func_id, void *rdata, dma_addr_t rdata_mapping,
  1635. int state, unsigned long *pstate, bnx2x_obj_type type,
  1636. struct bnx2x_credit_pool_obj *macs_pool,
  1637. struct bnx2x_credit_pool_obj *vlans_pool)
  1638. {
  1639. INIT_LIST_HEAD(&o->head);
  1640. o->head_reader = 0;
  1641. o->head_exe_request = false;
  1642. o->saved_ramrod_flags = 0;
  1643. o->macs_pool = macs_pool;
  1644. o->vlans_pool = vlans_pool;
  1645. o->delete_all = bnx2x_vlan_mac_del_all;
  1646. o->restore = bnx2x_vlan_mac_restore;
  1647. o->complete = bnx2x_complete_vlan_mac;
  1648. o->wait = bnx2x_wait_vlan_mac;
  1649. bnx2x_init_raw_obj(&o->raw, cl_id, cid, func_id, rdata, rdata_mapping,
  1650. state, pstate, type);
  1651. }
  1652. void bnx2x_init_mac_obj(struct bnx2x *bp,
  1653. struct bnx2x_vlan_mac_obj *mac_obj,
  1654. u8 cl_id, u32 cid, u8 func_id, void *rdata,
  1655. dma_addr_t rdata_mapping, int state,
  1656. unsigned long *pstate, bnx2x_obj_type type,
  1657. struct bnx2x_credit_pool_obj *macs_pool)
  1658. {
  1659. union bnx2x_qable_obj *qable_obj = (union bnx2x_qable_obj *)mac_obj;
  1660. bnx2x_init_vlan_mac_common(mac_obj, cl_id, cid, func_id, rdata,
  1661. rdata_mapping, state, pstate, type,
  1662. macs_pool, NULL);
  1663. /* CAM credit pool handling */
  1664. mac_obj->get_credit = bnx2x_get_credit_mac;
  1665. mac_obj->put_credit = bnx2x_put_credit_mac;
  1666. mac_obj->get_cam_offset = bnx2x_get_cam_offset_mac;
  1667. mac_obj->put_cam_offset = bnx2x_put_cam_offset_mac;
  1668. if (CHIP_IS_E1x(bp)) {
  1669. mac_obj->set_one_rule = bnx2x_set_one_mac_e1x;
  1670. mac_obj->check_del = bnx2x_check_mac_del;
  1671. mac_obj->check_add = bnx2x_check_mac_add;
  1672. mac_obj->check_move = bnx2x_check_move_always_err;
  1673. mac_obj->ramrod_cmd = RAMROD_CMD_ID_ETH_SET_MAC;
  1674. /* Exe Queue */
  1675. bnx2x_exe_queue_init(bp,
  1676. &mac_obj->exe_queue, 1, qable_obj,
  1677. bnx2x_validate_vlan_mac,
  1678. bnx2x_remove_vlan_mac,
  1679. bnx2x_optimize_vlan_mac,
  1680. bnx2x_execute_vlan_mac,
  1681. bnx2x_exeq_get_mac);
  1682. } else {
  1683. mac_obj->set_one_rule = bnx2x_set_one_mac_e2;
  1684. mac_obj->check_del = bnx2x_check_mac_del;
  1685. mac_obj->check_add = bnx2x_check_mac_add;
  1686. mac_obj->check_move = bnx2x_check_move;
  1687. mac_obj->ramrod_cmd =
  1688. RAMROD_CMD_ID_ETH_CLASSIFICATION_RULES;
  1689. mac_obj->get_n_elements = bnx2x_get_n_elements;
  1690. /* Exe Queue */
  1691. bnx2x_exe_queue_init(bp,
  1692. &mac_obj->exe_queue, CLASSIFY_RULES_COUNT,
  1693. qable_obj, bnx2x_validate_vlan_mac,
  1694. bnx2x_remove_vlan_mac,
  1695. bnx2x_optimize_vlan_mac,
  1696. bnx2x_execute_vlan_mac,
  1697. bnx2x_exeq_get_mac);
  1698. }
  1699. }
  1700. void bnx2x_init_vlan_obj(struct bnx2x *bp,
  1701. struct bnx2x_vlan_mac_obj *vlan_obj,
  1702. u8 cl_id, u32 cid, u8 func_id, void *rdata,
  1703. dma_addr_t rdata_mapping, int state,
  1704. unsigned long *pstate, bnx2x_obj_type type,
  1705. struct bnx2x_credit_pool_obj *vlans_pool)
  1706. {
  1707. union bnx2x_qable_obj *qable_obj = (union bnx2x_qable_obj *)vlan_obj;
  1708. bnx2x_init_vlan_mac_common(vlan_obj, cl_id, cid, func_id, rdata,
  1709. rdata_mapping, state, pstate, type, NULL,
  1710. vlans_pool);
  1711. vlan_obj->get_credit = bnx2x_get_credit_vlan;
  1712. vlan_obj->put_credit = bnx2x_put_credit_vlan;
  1713. vlan_obj->get_cam_offset = bnx2x_get_cam_offset_vlan;
  1714. vlan_obj->put_cam_offset = bnx2x_put_cam_offset_vlan;
  1715. if (CHIP_IS_E1x(bp)) {
  1716. BNX2X_ERR("Do not support chips others than E2 and newer\n");
  1717. BUG();
  1718. } else {
  1719. vlan_obj->set_one_rule = bnx2x_set_one_vlan_e2;
  1720. vlan_obj->check_del = bnx2x_check_vlan_del;
  1721. vlan_obj->check_add = bnx2x_check_vlan_add;
  1722. vlan_obj->check_move = bnx2x_check_move;
  1723. vlan_obj->ramrod_cmd =
  1724. RAMROD_CMD_ID_ETH_CLASSIFICATION_RULES;
  1725. vlan_obj->get_n_elements = bnx2x_get_n_elements;
  1726. /* Exe Queue */
  1727. bnx2x_exe_queue_init(bp,
  1728. &vlan_obj->exe_queue, CLASSIFY_RULES_COUNT,
  1729. qable_obj, bnx2x_validate_vlan_mac,
  1730. bnx2x_remove_vlan_mac,
  1731. bnx2x_optimize_vlan_mac,
  1732. bnx2x_execute_vlan_mac,
  1733. bnx2x_exeq_get_vlan);
  1734. }
  1735. }
  1736. /* RX_MODE verbs: DROP_ALL/ACCEPT_ALL/ACCEPT_ALL_MULTI/ACCEPT_ALL_VLAN/NORMAL */
  1737. static inline void __storm_memset_mac_filters(struct bnx2x *bp,
  1738. struct tstorm_eth_mac_filter_config *mac_filters,
  1739. u16 pf_id)
  1740. {
  1741. size_t size = sizeof(struct tstorm_eth_mac_filter_config);
  1742. u32 addr = BAR_TSTRORM_INTMEM +
  1743. TSTORM_MAC_FILTER_CONFIG_OFFSET(pf_id);
  1744. __storm_memset_struct(bp, addr, size, (u32 *)mac_filters);
  1745. }
  1746. static int bnx2x_set_rx_mode_e1x(struct bnx2x *bp,
  1747. struct bnx2x_rx_mode_ramrod_params *p)
  1748. {
  1749. /* update the bp MAC filter structure */
  1750. u32 mask = (1 << p->cl_id);
  1751. struct tstorm_eth_mac_filter_config *mac_filters =
  1752. (struct tstorm_eth_mac_filter_config *)p->rdata;
  1753. /* initial setting is drop-all */
  1754. u8 drop_all_ucast = 1, drop_all_mcast = 1;
  1755. u8 accp_all_ucast = 0, accp_all_bcast = 0, accp_all_mcast = 0;
  1756. u8 unmatched_unicast = 0;
  1757. /* In e1x there we only take into account rx accept flag since tx switching
  1758. * isn't enabled. */
  1759. if (test_bit(BNX2X_ACCEPT_UNICAST, &p->rx_accept_flags))
  1760. /* accept matched ucast */
  1761. drop_all_ucast = 0;
  1762. if (test_bit(BNX2X_ACCEPT_MULTICAST, &p->rx_accept_flags))
  1763. /* accept matched mcast */
  1764. drop_all_mcast = 0;
  1765. if (test_bit(BNX2X_ACCEPT_ALL_UNICAST, &p->rx_accept_flags)) {
  1766. /* accept all mcast */
  1767. drop_all_ucast = 0;
  1768. accp_all_ucast = 1;
  1769. }
  1770. if (test_bit(BNX2X_ACCEPT_ALL_MULTICAST, &p->rx_accept_flags)) {
  1771. /* accept all mcast */
  1772. drop_all_mcast = 0;
  1773. accp_all_mcast = 1;
  1774. }
  1775. if (test_bit(BNX2X_ACCEPT_BROADCAST, &p->rx_accept_flags))
  1776. /* accept (all) bcast */
  1777. accp_all_bcast = 1;
  1778. if (test_bit(BNX2X_ACCEPT_UNMATCHED, &p->rx_accept_flags))
  1779. /* accept unmatched unicasts */
  1780. unmatched_unicast = 1;
  1781. mac_filters->ucast_drop_all = drop_all_ucast ?
  1782. mac_filters->ucast_drop_all | mask :
  1783. mac_filters->ucast_drop_all & ~mask;
  1784. mac_filters->mcast_drop_all = drop_all_mcast ?
  1785. mac_filters->mcast_drop_all | mask :
  1786. mac_filters->mcast_drop_all & ~mask;
  1787. mac_filters->ucast_accept_all = accp_all_ucast ?
  1788. mac_filters->ucast_accept_all | mask :
  1789. mac_filters->ucast_accept_all & ~mask;
  1790. mac_filters->mcast_accept_all = accp_all_mcast ?
  1791. mac_filters->mcast_accept_all | mask :
  1792. mac_filters->mcast_accept_all & ~mask;
  1793. mac_filters->bcast_accept_all = accp_all_bcast ?
  1794. mac_filters->bcast_accept_all | mask :
  1795. mac_filters->bcast_accept_all & ~mask;
  1796. mac_filters->unmatched_unicast = unmatched_unicast ?
  1797. mac_filters->unmatched_unicast | mask :
  1798. mac_filters->unmatched_unicast & ~mask;
  1799. DP(BNX2X_MSG_SP, "drop_ucast 0x%x\ndrop_mcast 0x%x\n accp_ucast 0x%x\n"
  1800. "accp_mcast 0x%x\naccp_bcast 0x%x\n",
  1801. mac_filters->ucast_drop_all, mac_filters->mcast_drop_all,
  1802. mac_filters->ucast_accept_all, mac_filters->mcast_accept_all,
  1803. mac_filters->bcast_accept_all);
  1804. /* write the MAC filter structure*/
  1805. __storm_memset_mac_filters(bp, mac_filters, p->func_id);
  1806. /* The operation is completed */
  1807. clear_bit(p->state, p->pstate);
  1808. smp_mb__after_atomic();
  1809. return 0;
  1810. }
  1811. /* Setup ramrod data */
  1812. static inline void bnx2x_rx_mode_set_rdata_hdr_e2(u32 cid,
  1813. struct eth_classify_header *hdr,
  1814. u8 rule_cnt)
  1815. {
  1816. hdr->echo = cpu_to_le32(cid);
  1817. hdr->rule_cnt = rule_cnt;
  1818. }
  1819. static inline void bnx2x_rx_mode_set_cmd_state_e2(struct bnx2x *bp,
  1820. unsigned long *accept_flags,
  1821. struct eth_filter_rules_cmd *cmd,
  1822. bool clear_accept_all)
  1823. {
  1824. u16 state;
  1825. /* start with 'drop-all' */
  1826. state = ETH_FILTER_RULES_CMD_UCAST_DROP_ALL |
  1827. ETH_FILTER_RULES_CMD_MCAST_DROP_ALL;
  1828. if (test_bit(BNX2X_ACCEPT_UNICAST, accept_flags))
  1829. state &= ~ETH_FILTER_RULES_CMD_UCAST_DROP_ALL;
  1830. if (test_bit(BNX2X_ACCEPT_MULTICAST, accept_flags))
  1831. state &= ~ETH_FILTER_RULES_CMD_MCAST_DROP_ALL;
  1832. if (test_bit(BNX2X_ACCEPT_ALL_UNICAST, accept_flags)) {
  1833. state &= ~ETH_FILTER_RULES_CMD_UCAST_DROP_ALL;
  1834. state |= ETH_FILTER_RULES_CMD_UCAST_ACCEPT_ALL;
  1835. }
  1836. if (test_bit(BNX2X_ACCEPT_ALL_MULTICAST, accept_flags)) {
  1837. state |= ETH_FILTER_RULES_CMD_MCAST_ACCEPT_ALL;
  1838. state &= ~ETH_FILTER_RULES_CMD_MCAST_DROP_ALL;
  1839. }
  1840. if (test_bit(BNX2X_ACCEPT_BROADCAST, accept_flags))
  1841. state |= ETH_FILTER_RULES_CMD_BCAST_ACCEPT_ALL;
  1842. if (test_bit(BNX2X_ACCEPT_UNMATCHED, accept_flags)) {
  1843. state &= ~ETH_FILTER_RULES_CMD_UCAST_DROP_ALL;
  1844. state |= ETH_FILTER_RULES_CMD_UCAST_ACCEPT_UNMATCHED;
  1845. }
  1846. if (test_bit(BNX2X_ACCEPT_ANY_VLAN, accept_flags))
  1847. state |= ETH_FILTER_RULES_CMD_ACCEPT_ANY_VLAN;
  1848. /* Clear ACCEPT_ALL_XXX flags for FCoE L2 Queue */
  1849. if (clear_accept_all) {
  1850. state &= ~ETH_FILTER_RULES_CMD_MCAST_ACCEPT_ALL;
  1851. state &= ~ETH_FILTER_RULES_CMD_BCAST_ACCEPT_ALL;
  1852. state &= ~ETH_FILTER_RULES_CMD_UCAST_ACCEPT_ALL;
  1853. state &= ~ETH_FILTER_RULES_CMD_UCAST_ACCEPT_UNMATCHED;
  1854. }
  1855. cmd->state = cpu_to_le16(state);
  1856. }
  1857. static int bnx2x_set_rx_mode_e2(struct bnx2x *bp,
  1858. struct bnx2x_rx_mode_ramrod_params *p)
  1859. {
  1860. struct eth_filter_rules_ramrod_data *data = p->rdata;
  1861. int rc;
  1862. u8 rule_idx = 0;
  1863. /* Reset the ramrod data buffer */
  1864. memset(data, 0, sizeof(*data));
  1865. /* Setup ramrod data */
  1866. /* Tx (internal switching) */
  1867. if (test_bit(RAMROD_TX, &p->ramrod_flags)) {
  1868. data->rules[rule_idx].client_id = p->cl_id;
  1869. data->rules[rule_idx].func_id = p->func_id;
  1870. data->rules[rule_idx].cmd_general_data =
  1871. ETH_FILTER_RULES_CMD_TX_CMD;
  1872. bnx2x_rx_mode_set_cmd_state_e2(bp, &p->tx_accept_flags,
  1873. &(data->rules[rule_idx++]),
  1874. false);
  1875. }
  1876. /* Rx */
  1877. if (test_bit(RAMROD_RX, &p->ramrod_flags)) {
  1878. data->rules[rule_idx].client_id = p->cl_id;
  1879. data->rules[rule_idx].func_id = p->func_id;
  1880. data->rules[rule_idx].cmd_general_data =
  1881. ETH_FILTER_RULES_CMD_RX_CMD;
  1882. bnx2x_rx_mode_set_cmd_state_e2(bp, &p->rx_accept_flags,
  1883. &(data->rules[rule_idx++]),
  1884. false);
  1885. }
  1886. /* If FCoE Queue configuration has been requested configure the Rx and
  1887. * internal switching modes for this queue in separate rules.
  1888. *
  1889. * FCoE queue shell never be set to ACCEPT_ALL packets of any sort:
  1890. * MCAST_ALL, UCAST_ALL, BCAST_ALL and UNMATCHED.
  1891. */
  1892. if (test_bit(BNX2X_RX_MODE_FCOE_ETH, &p->rx_mode_flags)) {
  1893. /* Tx (internal switching) */
  1894. if (test_bit(RAMROD_TX, &p->ramrod_flags)) {
  1895. data->rules[rule_idx].client_id = bnx2x_fcoe(bp, cl_id);
  1896. data->rules[rule_idx].func_id = p->func_id;
  1897. data->rules[rule_idx].cmd_general_data =
  1898. ETH_FILTER_RULES_CMD_TX_CMD;
  1899. bnx2x_rx_mode_set_cmd_state_e2(bp, &p->tx_accept_flags,
  1900. &(data->rules[rule_idx]),
  1901. true);
  1902. rule_idx++;
  1903. }
  1904. /* Rx */
  1905. if (test_bit(RAMROD_RX, &p->ramrod_flags)) {
  1906. data->rules[rule_idx].client_id = bnx2x_fcoe(bp, cl_id);
  1907. data->rules[rule_idx].func_id = p->func_id;
  1908. data->rules[rule_idx].cmd_general_data =
  1909. ETH_FILTER_RULES_CMD_RX_CMD;
  1910. bnx2x_rx_mode_set_cmd_state_e2(bp, &p->rx_accept_flags,
  1911. &(data->rules[rule_idx]),
  1912. true);
  1913. rule_idx++;
  1914. }
  1915. }
  1916. /* Set the ramrod header (most importantly - number of rules to
  1917. * configure).
  1918. */
  1919. bnx2x_rx_mode_set_rdata_hdr_e2(p->cid, &data->header, rule_idx);
  1920. DP(BNX2X_MSG_SP, "About to configure %d rules, rx_accept_flags 0x%lx, tx_accept_flags 0x%lx\n",
  1921. data->header.rule_cnt, p->rx_accept_flags,
  1922. p->tx_accept_flags);
  1923. /* No need for an explicit memory barrier here as long as we
  1924. * ensure the ordering of writing to the SPQ element
  1925. * and updating of the SPQ producer which involves a memory
  1926. * read. If the memory read is removed we will have to put a
  1927. * full memory barrier there (inside bnx2x_sp_post()).
  1928. */
  1929. /* Send a ramrod */
  1930. rc = bnx2x_sp_post(bp, RAMROD_CMD_ID_ETH_FILTER_RULES, p->cid,
  1931. U64_HI(p->rdata_mapping),
  1932. U64_LO(p->rdata_mapping),
  1933. ETH_CONNECTION_TYPE);
  1934. if (rc)
  1935. return rc;
  1936. /* Ramrod completion is pending */
  1937. return 1;
  1938. }
  1939. static int bnx2x_wait_rx_mode_comp_e2(struct bnx2x *bp,
  1940. struct bnx2x_rx_mode_ramrod_params *p)
  1941. {
  1942. return bnx2x_state_wait(bp, p->state, p->pstate);
  1943. }
  1944. static int bnx2x_empty_rx_mode_wait(struct bnx2x *bp,
  1945. struct bnx2x_rx_mode_ramrod_params *p)
  1946. {
  1947. /* Do nothing */
  1948. return 0;
  1949. }
  1950. int bnx2x_config_rx_mode(struct bnx2x *bp,
  1951. struct bnx2x_rx_mode_ramrod_params *p)
  1952. {
  1953. int rc;
  1954. /* Configure the new classification in the chip */
  1955. rc = p->rx_mode_obj->config_rx_mode(bp, p);
  1956. if (rc < 0)
  1957. return rc;
  1958. /* Wait for a ramrod completion if was requested */
  1959. if (test_bit(RAMROD_COMP_WAIT, &p->ramrod_flags)) {
  1960. rc = p->rx_mode_obj->wait_comp(bp, p);
  1961. if (rc)
  1962. return rc;
  1963. }
  1964. return rc;
  1965. }
  1966. void bnx2x_init_rx_mode_obj(struct bnx2x *bp,
  1967. struct bnx2x_rx_mode_obj *o)
  1968. {
  1969. if (CHIP_IS_E1x(bp)) {
  1970. o->wait_comp = bnx2x_empty_rx_mode_wait;
  1971. o->config_rx_mode = bnx2x_set_rx_mode_e1x;
  1972. } else {
  1973. o->wait_comp = bnx2x_wait_rx_mode_comp_e2;
  1974. o->config_rx_mode = bnx2x_set_rx_mode_e2;
  1975. }
  1976. }
  1977. /********************* Multicast verbs: SET, CLEAR ****************************/
  1978. static inline u8 bnx2x_mcast_bin_from_mac(u8 *mac)
  1979. {
  1980. return (crc32c_le(0, mac, ETH_ALEN) >> 24) & 0xff;
  1981. }
  1982. struct bnx2x_mcast_mac_elem {
  1983. struct list_head link;
  1984. u8 mac[ETH_ALEN];
  1985. u8 pad[2]; /* For a natural alignment of the following buffer */
  1986. };
  1987. struct bnx2x_pending_mcast_cmd {
  1988. struct list_head link;
  1989. int type; /* BNX2X_MCAST_CMD_X */
  1990. union {
  1991. struct list_head macs_head;
  1992. u32 macs_num; /* Needed for DEL command */
  1993. int next_bin; /* Needed for RESTORE flow with aprox match */
  1994. } data;
  1995. bool done; /* set to true, when the command has been handled,
  1996. * practically used in 57712 handling only, where one pending
  1997. * command may be handled in a few operations. As long as for
  1998. * other chips every operation handling is completed in a
  1999. * single ramrod, there is no need to utilize this field.
  2000. */
  2001. };
  2002. static int bnx2x_mcast_wait(struct bnx2x *bp,
  2003. struct bnx2x_mcast_obj *o)
  2004. {
  2005. if (bnx2x_state_wait(bp, o->sched_state, o->raw.pstate) ||
  2006. o->raw.wait_comp(bp, &o->raw))
  2007. return -EBUSY;
  2008. return 0;
  2009. }
  2010. static int bnx2x_mcast_enqueue_cmd(struct bnx2x *bp,
  2011. struct bnx2x_mcast_obj *o,
  2012. struct bnx2x_mcast_ramrod_params *p,
  2013. enum bnx2x_mcast_cmd cmd)
  2014. {
  2015. int total_sz;
  2016. struct bnx2x_pending_mcast_cmd *new_cmd;
  2017. struct bnx2x_mcast_mac_elem *cur_mac = NULL;
  2018. struct bnx2x_mcast_list_elem *pos;
  2019. int macs_list_len = ((cmd == BNX2X_MCAST_CMD_ADD) ?
  2020. p->mcast_list_len : 0);
  2021. /* If the command is empty ("handle pending commands only"), break */
  2022. if (!p->mcast_list_len)
  2023. return 0;
  2024. total_sz = sizeof(*new_cmd) +
  2025. macs_list_len * sizeof(struct bnx2x_mcast_mac_elem);
  2026. /* Add mcast is called under spin_lock, thus calling with GFP_ATOMIC */
  2027. new_cmd = kzalloc(total_sz, GFP_ATOMIC);
  2028. if (!new_cmd)
  2029. return -ENOMEM;
  2030. DP(BNX2X_MSG_SP, "About to enqueue a new %d command. macs_list_len=%d\n",
  2031. cmd, macs_list_len);
  2032. INIT_LIST_HEAD(&new_cmd->data.macs_head);
  2033. new_cmd->type = cmd;
  2034. new_cmd->done = false;
  2035. switch (cmd) {
  2036. case BNX2X_MCAST_CMD_ADD:
  2037. cur_mac = (struct bnx2x_mcast_mac_elem *)
  2038. ((u8 *)new_cmd + sizeof(*new_cmd));
  2039. /* Push the MACs of the current command into the pending command
  2040. * MACs list: FIFO
  2041. */
  2042. list_for_each_entry(pos, &p->mcast_list, link) {
  2043. memcpy(cur_mac->mac, pos->mac, ETH_ALEN);
  2044. list_add_tail(&cur_mac->link, &new_cmd->data.macs_head);
  2045. cur_mac++;
  2046. }
  2047. break;
  2048. case BNX2X_MCAST_CMD_DEL:
  2049. new_cmd->data.macs_num = p->mcast_list_len;
  2050. break;
  2051. case BNX2X_MCAST_CMD_RESTORE:
  2052. new_cmd->data.next_bin = 0;
  2053. break;
  2054. default:
  2055. kfree(new_cmd);
  2056. BNX2X_ERR("Unknown command: %d\n", cmd);
  2057. return -EINVAL;
  2058. }
  2059. /* Push the new pending command to the tail of the pending list: FIFO */
  2060. list_add_tail(&new_cmd->link, &o->pending_cmds_head);
  2061. o->set_sched(o);
  2062. return 1;
  2063. }
  2064. /**
  2065. * bnx2x_mcast_get_next_bin - get the next set bin (index)
  2066. *
  2067. * @o:
  2068. * @last: index to start looking from (including)
  2069. *
  2070. * returns the next found (set) bin or a negative value if none is found.
  2071. */
  2072. static inline int bnx2x_mcast_get_next_bin(struct bnx2x_mcast_obj *o, int last)
  2073. {
  2074. int i, j, inner_start = last % BIT_VEC64_ELEM_SZ;
  2075. for (i = last / BIT_VEC64_ELEM_SZ; i < BNX2X_MCAST_VEC_SZ; i++) {
  2076. if (o->registry.aprox_match.vec[i])
  2077. for (j = inner_start; j < BIT_VEC64_ELEM_SZ; j++) {
  2078. int cur_bit = j + BIT_VEC64_ELEM_SZ * i;
  2079. if (BIT_VEC64_TEST_BIT(o->registry.aprox_match.
  2080. vec, cur_bit)) {
  2081. return cur_bit;
  2082. }
  2083. }
  2084. inner_start = 0;
  2085. }
  2086. /* None found */
  2087. return -1;
  2088. }
  2089. /**
  2090. * bnx2x_mcast_clear_first_bin - find the first set bin and clear it
  2091. *
  2092. * @o:
  2093. *
  2094. * returns the index of the found bin or -1 if none is found
  2095. */
  2096. static inline int bnx2x_mcast_clear_first_bin(struct bnx2x_mcast_obj *o)
  2097. {
  2098. int cur_bit = bnx2x_mcast_get_next_bin(o, 0);
  2099. if (cur_bit >= 0)
  2100. BIT_VEC64_CLEAR_BIT(o->registry.aprox_match.vec, cur_bit);
  2101. return cur_bit;
  2102. }
  2103. static inline u8 bnx2x_mcast_get_rx_tx_flag(struct bnx2x_mcast_obj *o)
  2104. {
  2105. struct bnx2x_raw_obj *raw = &o->raw;
  2106. u8 rx_tx_flag = 0;
  2107. if ((raw->obj_type == BNX2X_OBJ_TYPE_TX) ||
  2108. (raw->obj_type == BNX2X_OBJ_TYPE_RX_TX))
  2109. rx_tx_flag |= ETH_MULTICAST_RULES_CMD_TX_CMD;
  2110. if ((raw->obj_type == BNX2X_OBJ_TYPE_RX) ||
  2111. (raw->obj_type == BNX2X_OBJ_TYPE_RX_TX))
  2112. rx_tx_flag |= ETH_MULTICAST_RULES_CMD_RX_CMD;
  2113. return rx_tx_flag;
  2114. }
  2115. static void bnx2x_mcast_set_one_rule_e2(struct bnx2x *bp,
  2116. struct bnx2x_mcast_obj *o, int idx,
  2117. union bnx2x_mcast_config_data *cfg_data,
  2118. enum bnx2x_mcast_cmd cmd)
  2119. {
  2120. struct bnx2x_raw_obj *r = &o->raw;
  2121. struct eth_multicast_rules_ramrod_data *data =
  2122. (struct eth_multicast_rules_ramrod_data *)(r->rdata);
  2123. u8 func_id = r->func_id;
  2124. u8 rx_tx_add_flag = bnx2x_mcast_get_rx_tx_flag(o);
  2125. int bin;
  2126. if ((cmd == BNX2X_MCAST_CMD_ADD) || (cmd == BNX2X_MCAST_CMD_RESTORE))
  2127. rx_tx_add_flag |= ETH_MULTICAST_RULES_CMD_IS_ADD;
  2128. data->rules[idx].cmd_general_data |= rx_tx_add_flag;
  2129. /* Get a bin and update a bins' vector */
  2130. switch (cmd) {
  2131. case BNX2X_MCAST_CMD_ADD:
  2132. bin = bnx2x_mcast_bin_from_mac(cfg_data->mac);
  2133. BIT_VEC64_SET_BIT(o->registry.aprox_match.vec, bin);
  2134. break;
  2135. case BNX2X_MCAST_CMD_DEL:
  2136. /* If there were no more bins to clear
  2137. * (bnx2x_mcast_clear_first_bin() returns -1) then we would
  2138. * clear any (0xff) bin.
  2139. * See bnx2x_mcast_validate_e2() for explanation when it may
  2140. * happen.
  2141. */
  2142. bin = bnx2x_mcast_clear_first_bin(o);
  2143. break;
  2144. case BNX2X_MCAST_CMD_RESTORE:
  2145. bin = cfg_data->bin;
  2146. break;
  2147. default:
  2148. BNX2X_ERR("Unknown command: %d\n", cmd);
  2149. return;
  2150. }
  2151. DP(BNX2X_MSG_SP, "%s bin %d\n",
  2152. ((rx_tx_add_flag & ETH_MULTICAST_RULES_CMD_IS_ADD) ?
  2153. "Setting" : "Clearing"), bin);
  2154. data->rules[idx].bin_id = (u8)bin;
  2155. data->rules[idx].func_id = func_id;
  2156. data->rules[idx].engine_id = o->engine_id;
  2157. }
  2158. /**
  2159. * bnx2x_mcast_handle_restore_cmd_e2 - restore configuration from the registry
  2160. *
  2161. * @bp: device handle
  2162. * @o:
  2163. * @start_bin: index in the registry to start from (including)
  2164. * @rdata_idx: index in the ramrod data to start from
  2165. *
  2166. * returns last handled bin index or -1 if all bins have been handled
  2167. */
  2168. static inline int bnx2x_mcast_handle_restore_cmd_e2(
  2169. struct bnx2x *bp, struct bnx2x_mcast_obj *o , int start_bin,
  2170. int *rdata_idx)
  2171. {
  2172. int cur_bin, cnt = *rdata_idx;
  2173. union bnx2x_mcast_config_data cfg_data = {NULL};
  2174. /* go through the registry and configure the bins from it */
  2175. for (cur_bin = bnx2x_mcast_get_next_bin(o, start_bin); cur_bin >= 0;
  2176. cur_bin = bnx2x_mcast_get_next_bin(o, cur_bin + 1)) {
  2177. cfg_data.bin = (u8)cur_bin;
  2178. o->set_one_rule(bp, o, cnt, &cfg_data,
  2179. BNX2X_MCAST_CMD_RESTORE);
  2180. cnt++;
  2181. DP(BNX2X_MSG_SP, "About to configure a bin %d\n", cur_bin);
  2182. /* Break if we reached the maximum number
  2183. * of rules.
  2184. */
  2185. if (cnt >= o->max_cmd_len)
  2186. break;
  2187. }
  2188. *rdata_idx = cnt;
  2189. return cur_bin;
  2190. }
  2191. static inline void bnx2x_mcast_hdl_pending_add_e2(struct bnx2x *bp,
  2192. struct bnx2x_mcast_obj *o, struct bnx2x_pending_mcast_cmd *cmd_pos,
  2193. int *line_idx)
  2194. {
  2195. struct bnx2x_mcast_mac_elem *pmac_pos, *pmac_pos_n;
  2196. int cnt = *line_idx;
  2197. union bnx2x_mcast_config_data cfg_data = {NULL};
  2198. list_for_each_entry_safe(pmac_pos, pmac_pos_n, &cmd_pos->data.macs_head,
  2199. link) {
  2200. cfg_data.mac = &pmac_pos->mac[0];
  2201. o->set_one_rule(bp, o, cnt, &cfg_data, cmd_pos->type);
  2202. cnt++;
  2203. DP(BNX2X_MSG_SP, "About to configure %pM mcast MAC\n",
  2204. pmac_pos->mac);
  2205. list_del(&pmac_pos->link);
  2206. /* Break if we reached the maximum number
  2207. * of rules.
  2208. */
  2209. if (cnt >= o->max_cmd_len)
  2210. break;
  2211. }
  2212. *line_idx = cnt;
  2213. /* if no more MACs to configure - we are done */
  2214. if (list_empty(&cmd_pos->data.macs_head))
  2215. cmd_pos->done = true;
  2216. }
  2217. static inline void bnx2x_mcast_hdl_pending_del_e2(struct bnx2x *bp,
  2218. struct bnx2x_mcast_obj *o, struct bnx2x_pending_mcast_cmd *cmd_pos,
  2219. int *line_idx)
  2220. {
  2221. int cnt = *line_idx;
  2222. while (cmd_pos->data.macs_num) {
  2223. o->set_one_rule(bp, o, cnt, NULL, cmd_pos->type);
  2224. cnt++;
  2225. cmd_pos->data.macs_num--;
  2226. DP(BNX2X_MSG_SP, "Deleting MAC. %d left,cnt is %d\n",
  2227. cmd_pos->data.macs_num, cnt);
  2228. /* Break if we reached the maximum
  2229. * number of rules.
  2230. */
  2231. if (cnt >= o->max_cmd_len)
  2232. break;
  2233. }
  2234. *line_idx = cnt;
  2235. /* If we cleared all bins - we are done */
  2236. if (!cmd_pos->data.macs_num)
  2237. cmd_pos->done = true;
  2238. }
  2239. static inline void bnx2x_mcast_hdl_pending_restore_e2(struct bnx2x *bp,
  2240. struct bnx2x_mcast_obj *o, struct bnx2x_pending_mcast_cmd *cmd_pos,
  2241. int *line_idx)
  2242. {
  2243. cmd_pos->data.next_bin = o->hdl_restore(bp, o, cmd_pos->data.next_bin,
  2244. line_idx);
  2245. if (cmd_pos->data.next_bin < 0)
  2246. /* If o->set_restore returned -1 we are done */
  2247. cmd_pos->done = true;
  2248. else
  2249. /* Start from the next bin next time */
  2250. cmd_pos->data.next_bin++;
  2251. }
  2252. static inline int bnx2x_mcast_handle_pending_cmds_e2(struct bnx2x *bp,
  2253. struct bnx2x_mcast_ramrod_params *p)
  2254. {
  2255. struct bnx2x_pending_mcast_cmd *cmd_pos, *cmd_pos_n;
  2256. int cnt = 0;
  2257. struct bnx2x_mcast_obj *o = p->mcast_obj;
  2258. list_for_each_entry_safe(cmd_pos, cmd_pos_n, &o->pending_cmds_head,
  2259. link) {
  2260. switch (cmd_pos->type) {
  2261. case BNX2X_MCAST_CMD_ADD:
  2262. bnx2x_mcast_hdl_pending_add_e2(bp, o, cmd_pos, &cnt);
  2263. break;
  2264. case BNX2X_MCAST_CMD_DEL:
  2265. bnx2x_mcast_hdl_pending_del_e2(bp, o, cmd_pos, &cnt);
  2266. break;
  2267. case BNX2X_MCAST_CMD_RESTORE:
  2268. bnx2x_mcast_hdl_pending_restore_e2(bp, o, cmd_pos,
  2269. &cnt);
  2270. break;
  2271. default:
  2272. BNX2X_ERR("Unknown command: %d\n", cmd_pos->type);
  2273. return -EINVAL;
  2274. }
  2275. /* If the command has been completed - remove it from the list
  2276. * and free the memory
  2277. */
  2278. if (cmd_pos->done) {
  2279. list_del(&cmd_pos->link);
  2280. kfree(cmd_pos);
  2281. }
  2282. /* Break if we reached the maximum number of rules */
  2283. if (cnt >= o->max_cmd_len)
  2284. break;
  2285. }
  2286. return cnt;
  2287. }
  2288. static inline void bnx2x_mcast_hdl_add(struct bnx2x *bp,
  2289. struct bnx2x_mcast_obj *o, struct bnx2x_mcast_ramrod_params *p,
  2290. int *line_idx)
  2291. {
  2292. struct bnx2x_mcast_list_elem *mlist_pos;
  2293. union bnx2x_mcast_config_data cfg_data = {NULL};
  2294. int cnt = *line_idx;
  2295. list_for_each_entry(mlist_pos, &p->mcast_list, link) {
  2296. cfg_data.mac = mlist_pos->mac;
  2297. o->set_one_rule(bp, o, cnt, &cfg_data, BNX2X_MCAST_CMD_ADD);
  2298. cnt++;
  2299. DP(BNX2X_MSG_SP, "About to configure %pM mcast MAC\n",
  2300. mlist_pos->mac);
  2301. }
  2302. *line_idx = cnt;
  2303. }
  2304. static inline void bnx2x_mcast_hdl_del(struct bnx2x *bp,
  2305. struct bnx2x_mcast_obj *o, struct bnx2x_mcast_ramrod_params *p,
  2306. int *line_idx)
  2307. {
  2308. int cnt = *line_idx, i;
  2309. for (i = 0; i < p->mcast_list_len; i++) {
  2310. o->set_one_rule(bp, o, cnt, NULL, BNX2X_MCAST_CMD_DEL);
  2311. cnt++;
  2312. DP(BNX2X_MSG_SP, "Deleting MAC. %d left\n",
  2313. p->mcast_list_len - i - 1);
  2314. }
  2315. *line_idx = cnt;
  2316. }
  2317. /**
  2318. * bnx2x_mcast_handle_current_cmd -
  2319. *
  2320. * @bp: device handle
  2321. * @p:
  2322. * @cmd:
  2323. * @start_cnt: first line in the ramrod data that may be used
  2324. *
  2325. * This function is called iff there is enough place for the current command in
  2326. * the ramrod data.
  2327. * Returns number of lines filled in the ramrod data in total.
  2328. */
  2329. static inline int bnx2x_mcast_handle_current_cmd(struct bnx2x *bp,
  2330. struct bnx2x_mcast_ramrod_params *p,
  2331. enum bnx2x_mcast_cmd cmd,
  2332. int start_cnt)
  2333. {
  2334. struct bnx2x_mcast_obj *o = p->mcast_obj;
  2335. int cnt = start_cnt;
  2336. DP(BNX2X_MSG_SP, "p->mcast_list_len=%d\n", p->mcast_list_len);
  2337. switch (cmd) {
  2338. case BNX2X_MCAST_CMD_ADD:
  2339. bnx2x_mcast_hdl_add(bp, o, p, &cnt);
  2340. break;
  2341. case BNX2X_MCAST_CMD_DEL:
  2342. bnx2x_mcast_hdl_del(bp, o, p, &cnt);
  2343. break;
  2344. case BNX2X_MCAST_CMD_RESTORE:
  2345. o->hdl_restore(bp, o, 0, &cnt);
  2346. break;
  2347. default:
  2348. BNX2X_ERR("Unknown command: %d\n", cmd);
  2349. return -EINVAL;
  2350. }
  2351. /* The current command has been handled */
  2352. p->mcast_list_len = 0;
  2353. return cnt;
  2354. }
  2355. static int bnx2x_mcast_validate_e2(struct bnx2x *bp,
  2356. struct bnx2x_mcast_ramrod_params *p,
  2357. enum bnx2x_mcast_cmd cmd)
  2358. {
  2359. struct bnx2x_mcast_obj *o = p->mcast_obj;
  2360. int reg_sz = o->get_registry_size(o);
  2361. switch (cmd) {
  2362. /* DEL command deletes all currently configured MACs */
  2363. case BNX2X_MCAST_CMD_DEL:
  2364. o->set_registry_size(o, 0);
  2365. /* Don't break */
  2366. /* RESTORE command will restore the entire multicast configuration */
  2367. case BNX2X_MCAST_CMD_RESTORE:
  2368. /* Here we set the approximate amount of work to do, which in
  2369. * fact may be only less as some MACs in postponed ADD
  2370. * command(s) scheduled before this command may fall into
  2371. * the same bin and the actual number of bins set in the
  2372. * registry would be less than we estimated here. See
  2373. * bnx2x_mcast_set_one_rule_e2() for further details.
  2374. */
  2375. p->mcast_list_len = reg_sz;
  2376. break;
  2377. case BNX2X_MCAST_CMD_ADD:
  2378. case BNX2X_MCAST_CMD_CONT:
  2379. /* Here we assume that all new MACs will fall into new bins.
  2380. * However we will correct the real registry size after we
  2381. * handle all pending commands.
  2382. */
  2383. o->set_registry_size(o, reg_sz + p->mcast_list_len);
  2384. break;
  2385. default:
  2386. BNX2X_ERR("Unknown command: %d\n", cmd);
  2387. return -EINVAL;
  2388. }
  2389. /* Increase the total number of MACs pending to be configured */
  2390. o->total_pending_num += p->mcast_list_len;
  2391. return 0;
  2392. }
  2393. static void bnx2x_mcast_revert_e2(struct bnx2x *bp,
  2394. struct bnx2x_mcast_ramrod_params *p,
  2395. int old_num_bins)
  2396. {
  2397. struct bnx2x_mcast_obj *o = p->mcast_obj;
  2398. o->set_registry_size(o, old_num_bins);
  2399. o->total_pending_num -= p->mcast_list_len;
  2400. }
  2401. /**
  2402. * bnx2x_mcast_set_rdata_hdr_e2 - sets a header values
  2403. *
  2404. * @bp: device handle
  2405. * @p:
  2406. * @len: number of rules to handle
  2407. */
  2408. static inline void bnx2x_mcast_set_rdata_hdr_e2(struct bnx2x *bp,
  2409. struct bnx2x_mcast_ramrod_params *p,
  2410. u8 len)
  2411. {
  2412. struct bnx2x_raw_obj *r = &p->mcast_obj->raw;
  2413. struct eth_multicast_rules_ramrod_data *data =
  2414. (struct eth_multicast_rules_ramrod_data *)(r->rdata);
  2415. data->header.echo = cpu_to_le32((r->cid & BNX2X_SWCID_MASK) |
  2416. (BNX2X_FILTER_MCAST_PENDING <<
  2417. BNX2X_SWCID_SHIFT));
  2418. data->header.rule_cnt = len;
  2419. }
  2420. /**
  2421. * bnx2x_mcast_refresh_registry_e2 - recalculate the actual number of set bins
  2422. *
  2423. * @bp: device handle
  2424. * @o:
  2425. *
  2426. * Recalculate the actual number of set bins in the registry using Brian
  2427. * Kernighan's algorithm: it's execution complexity is as a number of set bins.
  2428. *
  2429. * returns 0 for the compliance with bnx2x_mcast_refresh_registry_e1().
  2430. */
  2431. static inline int bnx2x_mcast_refresh_registry_e2(struct bnx2x *bp,
  2432. struct bnx2x_mcast_obj *o)
  2433. {
  2434. int i, cnt = 0;
  2435. u64 elem;
  2436. for (i = 0; i < BNX2X_MCAST_VEC_SZ; i++) {
  2437. elem = o->registry.aprox_match.vec[i];
  2438. for (; elem; cnt++)
  2439. elem &= elem - 1;
  2440. }
  2441. o->set_registry_size(o, cnt);
  2442. return 0;
  2443. }
  2444. static int bnx2x_mcast_setup_e2(struct bnx2x *bp,
  2445. struct bnx2x_mcast_ramrod_params *p,
  2446. enum bnx2x_mcast_cmd cmd)
  2447. {
  2448. struct bnx2x_raw_obj *raw = &p->mcast_obj->raw;
  2449. struct bnx2x_mcast_obj *o = p->mcast_obj;
  2450. struct eth_multicast_rules_ramrod_data *data =
  2451. (struct eth_multicast_rules_ramrod_data *)(raw->rdata);
  2452. int cnt = 0, rc;
  2453. /* Reset the ramrod data buffer */
  2454. memset(data, 0, sizeof(*data));
  2455. cnt = bnx2x_mcast_handle_pending_cmds_e2(bp, p);
  2456. /* If there are no more pending commands - clear SCHEDULED state */
  2457. if (list_empty(&o->pending_cmds_head))
  2458. o->clear_sched(o);
  2459. /* The below may be true iff there was enough room in ramrod
  2460. * data for all pending commands and for the current
  2461. * command. Otherwise the current command would have been added
  2462. * to the pending commands and p->mcast_list_len would have been
  2463. * zeroed.
  2464. */
  2465. if (p->mcast_list_len > 0)
  2466. cnt = bnx2x_mcast_handle_current_cmd(bp, p, cmd, cnt);
  2467. /* We've pulled out some MACs - update the total number of
  2468. * outstanding.
  2469. */
  2470. o->total_pending_num -= cnt;
  2471. /* send a ramrod */
  2472. WARN_ON(o->total_pending_num < 0);
  2473. WARN_ON(cnt > o->max_cmd_len);
  2474. bnx2x_mcast_set_rdata_hdr_e2(bp, p, (u8)cnt);
  2475. /* Update a registry size if there are no more pending operations.
  2476. *
  2477. * We don't want to change the value of the registry size if there are
  2478. * pending operations because we want it to always be equal to the
  2479. * exact or the approximate number (see bnx2x_mcast_validate_e2()) of
  2480. * set bins after the last requested operation in order to properly
  2481. * evaluate the size of the next DEL/RESTORE operation.
  2482. *
  2483. * Note that we update the registry itself during command(s) handling
  2484. * - see bnx2x_mcast_set_one_rule_e2(). That's because for 57712 we
  2485. * aggregate multiple commands (ADD/DEL/RESTORE) into one ramrod but
  2486. * with a limited amount of update commands (per MAC/bin) and we don't
  2487. * know in this scope what the actual state of bins configuration is
  2488. * going to be after this ramrod.
  2489. */
  2490. if (!o->total_pending_num)
  2491. bnx2x_mcast_refresh_registry_e2(bp, o);
  2492. /* If CLEAR_ONLY was requested - don't send a ramrod and clear
  2493. * RAMROD_PENDING status immediately.
  2494. */
  2495. if (test_bit(RAMROD_DRV_CLR_ONLY, &p->ramrod_flags)) {
  2496. raw->clear_pending(raw);
  2497. return 0;
  2498. } else {
  2499. /* No need for an explicit memory barrier here as long as we
  2500. * ensure the ordering of writing to the SPQ element
  2501. * and updating of the SPQ producer which involves a memory
  2502. * read. If the memory read is removed we will have to put a
  2503. * full memory barrier there (inside bnx2x_sp_post()).
  2504. */
  2505. /* Send a ramrod */
  2506. rc = bnx2x_sp_post(bp, RAMROD_CMD_ID_ETH_MULTICAST_RULES,
  2507. raw->cid, U64_HI(raw->rdata_mapping),
  2508. U64_LO(raw->rdata_mapping),
  2509. ETH_CONNECTION_TYPE);
  2510. if (rc)
  2511. return rc;
  2512. /* Ramrod completion is pending */
  2513. return 1;
  2514. }
  2515. }
  2516. static int bnx2x_mcast_validate_e1h(struct bnx2x *bp,
  2517. struct bnx2x_mcast_ramrod_params *p,
  2518. enum bnx2x_mcast_cmd cmd)
  2519. {
  2520. /* Mark, that there is a work to do */
  2521. if ((cmd == BNX2X_MCAST_CMD_DEL) || (cmd == BNX2X_MCAST_CMD_RESTORE))
  2522. p->mcast_list_len = 1;
  2523. return 0;
  2524. }
  2525. static void bnx2x_mcast_revert_e1h(struct bnx2x *bp,
  2526. struct bnx2x_mcast_ramrod_params *p,
  2527. int old_num_bins)
  2528. {
  2529. /* Do nothing */
  2530. }
  2531. #define BNX2X_57711_SET_MC_FILTER(filter, bit) \
  2532. do { \
  2533. (filter)[(bit) >> 5] |= (1 << ((bit) & 0x1f)); \
  2534. } while (0)
  2535. static inline void bnx2x_mcast_hdl_add_e1h(struct bnx2x *bp,
  2536. struct bnx2x_mcast_obj *o,
  2537. struct bnx2x_mcast_ramrod_params *p,
  2538. u32 *mc_filter)
  2539. {
  2540. struct bnx2x_mcast_list_elem *mlist_pos;
  2541. int bit;
  2542. list_for_each_entry(mlist_pos, &p->mcast_list, link) {
  2543. bit = bnx2x_mcast_bin_from_mac(mlist_pos->mac);
  2544. BNX2X_57711_SET_MC_FILTER(mc_filter, bit);
  2545. DP(BNX2X_MSG_SP, "About to configure %pM mcast MAC, bin %d\n",
  2546. mlist_pos->mac, bit);
  2547. /* bookkeeping... */
  2548. BIT_VEC64_SET_BIT(o->registry.aprox_match.vec,
  2549. bit);
  2550. }
  2551. }
  2552. static inline void bnx2x_mcast_hdl_restore_e1h(struct bnx2x *bp,
  2553. struct bnx2x_mcast_obj *o, struct bnx2x_mcast_ramrod_params *p,
  2554. u32 *mc_filter)
  2555. {
  2556. int bit;
  2557. for (bit = bnx2x_mcast_get_next_bin(o, 0);
  2558. bit >= 0;
  2559. bit = bnx2x_mcast_get_next_bin(o, bit + 1)) {
  2560. BNX2X_57711_SET_MC_FILTER(mc_filter, bit);
  2561. DP(BNX2X_MSG_SP, "About to set bin %d\n", bit);
  2562. }
  2563. }
  2564. /* On 57711 we write the multicast MACs' approximate match
  2565. * table by directly into the TSTORM's internal RAM. So we don't
  2566. * really need to handle any tricks to make it work.
  2567. */
  2568. static int bnx2x_mcast_setup_e1h(struct bnx2x *bp,
  2569. struct bnx2x_mcast_ramrod_params *p,
  2570. enum bnx2x_mcast_cmd cmd)
  2571. {
  2572. int i;
  2573. struct bnx2x_mcast_obj *o = p->mcast_obj;
  2574. struct bnx2x_raw_obj *r = &o->raw;
  2575. /* If CLEAR_ONLY has been requested - clear the registry
  2576. * and clear a pending bit.
  2577. */
  2578. if (!test_bit(RAMROD_DRV_CLR_ONLY, &p->ramrod_flags)) {
  2579. u32 mc_filter[MC_HASH_SIZE] = {0};
  2580. /* Set the multicast filter bits before writing it into
  2581. * the internal memory.
  2582. */
  2583. switch (cmd) {
  2584. case BNX2X_MCAST_CMD_ADD:
  2585. bnx2x_mcast_hdl_add_e1h(bp, o, p, mc_filter);
  2586. break;
  2587. case BNX2X_MCAST_CMD_DEL:
  2588. DP(BNX2X_MSG_SP,
  2589. "Invalidating multicast MACs configuration\n");
  2590. /* clear the registry */
  2591. memset(o->registry.aprox_match.vec, 0,
  2592. sizeof(o->registry.aprox_match.vec));
  2593. break;
  2594. case BNX2X_MCAST_CMD_RESTORE:
  2595. bnx2x_mcast_hdl_restore_e1h(bp, o, p, mc_filter);
  2596. break;
  2597. default:
  2598. BNX2X_ERR("Unknown command: %d\n", cmd);
  2599. return -EINVAL;
  2600. }
  2601. /* Set the mcast filter in the internal memory */
  2602. for (i = 0; i < MC_HASH_SIZE; i++)
  2603. REG_WR(bp, MC_HASH_OFFSET(bp, i), mc_filter[i]);
  2604. } else
  2605. /* clear the registry */
  2606. memset(o->registry.aprox_match.vec, 0,
  2607. sizeof(o->registry.aprox_match.vec));
  2608. /* We are done */
  2609. r->clear_pending(r);
  2610. return 0;
  2611. }
  2612. static int bnx2x_mcast_validate_e1(struct bnx2x *bp,
  2613. struct bnx2x_mcast_ramrod_params *p,
  2614. enum bnx2x_mcast_cmd cmd)
  2615. {
  2616. struct bnx2x_mcast_obj *o = p->mcast_obj;
  2617. int reg_sz = o->get_registry_size(o);
  2618. switch (cmd) {
  2619. /* DEL command deletes all currently configured MACs */
  2620. case BNX2X_MCAST_CMD_DEL:
  2621. o->set_registry_size(o, 0);
  2622. /* Don't break */
  2623. /* RESTORE command will restore the entire multicast configuration */
  2624. case BNX2X_MCAST_CMD_RESTORE:
  2625. p->mcast_list_len = reg_sz;
  2626. DP(BNX2X_MSG_SP, "Command %d, p->mcast_list_len=%d\n",
  2627. cmd, p->mcast_list_len);
  2628. break;
  2629. case BNX2X_MCAST_CMD_ADD:
  2630. case BNX2X_MCAST_CMD_CONT:
  2631. /* Multicast MACs on 57710 are configured as unicast MACs and
  2632. * there is only a limited number of CAM entries for that
  2633. * matter.
  2634. */
  2635. if (p->mcast_list_len > o->max_cmd_len) {
  2636. BNX2X_ERR("Can't configure more than %d multicast MACs on 57710\n",
  2637. o->max_cmd_len);
  2638. return -EINVAL;
  2639. }
  2640. /* Every configured MAC should be cleared if DEL command is
  2641. * called. Only the last ADD command is relevant as long as
  2642. * every ADD commands overrides the previous configuration.
  2643. */
  2644. DP(BNX2X_MSG_SP, "p->mcast_list_len=%d\n", p->mcast_list_len);
  2645. if (p->mcast_list_len > 0)
  2646. o->set_registry_size(o, p->mcast_list_len);
  2647. break;
  2648. default:
  2649. BNX2X_ERR("Unknown command: %d\n", cmd);
  2650. return -EINVAL;
  2651. }
  2652. /* We want to ensure that commands are executed one by one for 57710.
  2653. * Therefore each none-empty command will consume o->max_cmd_len.
  2654. */
  2655. if (p->mcast_list_len)
  2656. o->total_pending_num += o->max_cmd_len;
  2657. return 0;
  2658. }
  2659. static void bnx2x_mcast_revert_e1(struct bnx2x *bp,
  2660. struct bnx2x_mcast_ramrod_params *p,
  2661. int old_num_macs)
  2662. {
  2663. struct bnx2x_mcast_obj *o = p->mcast_obj;
  2664. o->set_registry_size(o, old_num_macs);
  2665. /* If current command hasn't been handled yet and we are
  2666. * here means that it's meant to be dropped and we have to
  2667. * update the number of outstanding MACs accordingly.
  2668. */
  2669. if (p->mcast_list_len)
  2670. o->total_pending_num -= o->max_cmd_len;
  2671. }
  2672. static void bnx2x_mcast_set_one_rule_e1(struct bnx2x *bp,
  2673. struct bnx2x_mcast_obj *o, int idx,
  2674. union bnx2x_mcast_config_data *cfg_data,
  2675. enum bnx2x_mcast_cmd cmd)
  2676. {
  2677. struct bnx2x_raw_obj *r = &o->raw;
  2678. struct mac_configuration_cmd *data =
  2679. (struct mac_configuration_cmd *)(r->rdata);
  2680. /* copy mac */
  2681. if ((cmd == BNX2X_MCAST_CMD_ADD) || (cmd == BNX2X_MCAST_CMD_RESTORE)) {
  2682. bnx2x_set_fw_mac_addr(&data->config_table[idx].msb_mac_addr,
  2683. &data->config_table[idx].middle_mac_addr,
  2684. &data->config_table[idx].lsb_mac_addr,
  2685. cfg_data->mac);
  2686. data->config_table[idx].vlan_id = 0;
  2687. data->config_table[idx].pf_id = r->func_id;
  2688. data->config_table[idx].clients_bit_vector =
  2689. cpu_to_le32(1 << r->cl_id);
  2690. SET_FLAG(data->config_table[idx].flags,
  2691. MAC_CONFIGURATION_ENTRY_ACTION_TYPE,
  2692. T_ETH_MAC_COMMAND_SET);
  2693. }
  2694. }
  2695. /**
  2696. * bnx2x_mcast_set_rdata_hdr_e1 - set header values in mac_configuration_cmd
  2697. *
  2698. * @bp: device handle
  2699. * @p:
  2700. * @len: number of rules to handle
  2701. */
  2702. static inline void bnx2x_mcast_set_rdata_hdr_e1(struct bnx2x *bp,
  2703. struct bnx2x_mcast_ramrod_params *p,
  2704. u8 len)
  2705. {
  2706. struct bnx2x_raw_obj *r = &p->mcast_obj->raw;
  2707. struct mac_configuration_cmd *data =
  2708. (struct mac_configuration_cmd *)(r->rdata);
  2709. u8 offset = (CHIP_REV_IS_SLOW(bp) ?
  2710. BNX2X_MAX_EMUL_MULTI*(1 + r->func_id) :
  2711. BNX2X_MAX_MULTICAST*(1 + r->func_id));
  2712. data->hdr.offset = offset;
  2713. data->hdr.client_id = cpu_to_le16(0xff);
  2714. data->hdr.echo = cpu_to_le32((r->cid & BNX2X_SWCID_MASK) |
  2715. (BNX2X_FILTER_MCAST_PENDING <<
  2716. BNX2X_SWCID_SHIFT));
  2717. data->hdr.length = len;
  2718. }
  2719. /**
  2720. * bnx2x_mcast_handle_restore_cmd_e1 - restore command for 57710
  2721. *
  2722. * @bp: device handle
  2723. * @o:
  2724. * @start_idx: index in the registry to start from
  2725. * @rdata_idx: index in the ramrod data to start from
  2726. *
  2727. * restore command for 57710 is like all other commands - always a stand alone
  2728. * command - start_idx and rdata_idx will always be 0. This function will always
  2729. * succeed.
  2730. * returns -1 to comply with 57712 variant.
  2731. */
  2732. static inline int bnx2x_mcast_handle_restore_cmd_e1(
  2733. struct bnx2x *bp, struct bnx2x_mcast_obj *o , int start_idx,
  2734. int *rdata_idx)
  2735. {
  2736. struct bnx2x_mcast_mac_elem *elem;
  2737. int i = 0;
  2738. union bnx2x_mcast_config_data cfg_data = {NULL};
  2739. /* go through the registry and configure the MACs from it. */
  2740. list_for_each_entry(elem, &o->registry.exact_match.macs, link) {
  2741. cfg_data.mac = &elem->mac[0];
  2742. o->set_one_rule(bp, o, i, &cfg_data, BNX2X_MCAST_CMD_RESTORE);
  2743. i++;
  2744. DP(BNX2X_MSG_SP, "About to configure %pM mcast MAC\n",
  2745. cfg_data.mac);
  2746. }
  2747. *rdata_idx = i;
  2748. return -1;
  2749. }
  2750. static inline int bnx2x_mcast_handle_pending_cmds_e1(
  2751. struct bnx2x *bp, struct bnx2x_mcast_ramrod_params *p)
  2752. {
  2753. struct bnx2x_pending_mcast_cmd *cmd_pos;
  2754. struct bnx2x_mcast_mac_elem *pmac_pos;
  2755. struct bnx2x_mcast_obj *o = p->mcast_obj;
  2756. union bnx2x_mcast_config_data cfg_data = {NULL};
  2757. int cnt = 0;
  2758. /* If nothing to be done - return */
  2759. if (list_empty(&o->pending_cmds_head))
  2760. return 0;
  2761. /* Handle the first command */
  2762. cmd_pos = list_first_entry(&o->pending_cmds_head,
  2763. struct bnx2x_pending_mcast_cmd, link);
  2764. switch (cmd_pos->type) {
  2765. case BNX2X_MCAST_CMD_ADD:
  2766. list_for_each_entry(pmac_pos, &cmd_pos->data.macs_head, link) {
  2767. cfg_data.mac = &pmac_pos->mac[0];
  2768. o->set_one_rule(bp, o, cnt, &cfg_data, cmd_pos->type);
  2769. cnt++;
  2770. DP(BNX2X_MSG_SP, "About to configure %pM mcast MAC\n",
  2771. pmac_pos->mac);
  2772. }
  2773. break;
  2774. case BNX2X_MCAST_CMD_DEL:
  2775. cnt = cmd_pos->data.macs_num;
  2776. DP(BNX2X_MSG_SP, "About to delete %d multicast MACs\n", cnt);
  2777. break;
  2778. case BNX2X_MCAST_CMD_RESTORE:
  2779. o->hdl_restore(bp, o, 0, &cnt);
  2780. break;
  2781. default:
  2782. BNX2X_ERR("Unknown command: %d\n", cmd_pos->type);
  2783. return -EINVAL;
  2784. }
  2785. list_del(&cmd_pos->link);
  2786. kfree(cmd_pos);
  2787. return cnt;
  2788. }
  2789. /**
  2790. * bnx2x_get_fw_mac_addr - revert the bnx2x_set_fw_mac_addr().
  2791. *
  2792. * @fw_hi:
  2793. * @fw_mid:
  2794. * @fw_lo:
  2795. * @mac:
  2796. */
  2797. static inline void bnx2x_get_fw_mac_addr(__le16 *fw_hi, __le16 *fw_mid,
  2798. __le16 *fw_lo, u8 *mac)
  2799. {
  2800. mac[1] = ((u8 *)fw_hi)[0];
  2801. mac[0] = ((u8 *)fw_hi)[1];
  2802. mac[3] = ((u8 *)fw_mid)[0];
  2803. mac[2] = ((u8 *)fw_mid)[1];
  2804. mac[5] = ((u8 *)fw_lo)[0];
  2805. mac[4] = ((u8 *)fw_lo)[1];
  2806. }
  2807. /**
  2808. * bnx2x_mcast_refresh_registry_e1 -
  2809. *
  2810. * @bp: device handle
  2811. * @cnt:
  2812. *
  2813. * Check the ramrod data first entry flag to see if it's a DELETE or ADD command
  2814. * and update the registry correspondingly: if ADD - allocate a memory and add
  2815. * the entries to the registry (list), if DELETE - clear the registry and free
  2816. * the memory.
  2817. */
  2818. static inline int bnx2x_mcast_refresh_registry_e1(struct bnx2x *bp,
  2819. struct bnx2x_mcast_obj *o)
  2820. {
  2821. struct bnx2x_raw_obj *raw = &o->raw;
  2822. struct bnx2x_mcast_mac_elem *elem;
  2823. struct mac_configuration_cmd *data =
  2824. (struct mac_configuration_cmd *)(raw->rdata);
  2825. /* If first entry contains a SET bit - the command was ADD,
  2826. * otherwise - DEL_ALL
  2827. */
  2828. if (GET_FLAG(data->config_table[0].flags,
  2829. MAC_CONFIGURATION_ENTRY_ACTION_TYPE)) {
  2830. int i, len = data->hdr.length;
  2831. /* Break if it was a RESTORE command */
  2832. if (!list_empty(&o->registry.exact_match.macs))
  2833. return 0;
  2834. elem = kcalloc(len, sizeof(*elem), GFP_ATOMIC);
  2835. if (!elem) {
  2836. BNX2X_ERR("Failed to allocate registry memory\n");
  2837. return -ENOMEM;
  2838. }
  2839. for (i = 0; i < len; i++, elem++) {
  2840. bnx2x_get_fw_mac_addr(
  2841. &data->config_table[i].msb_mac_addr,
  2842. &data->config_table[i].middle_mac_addr,
  2843. &data->config_table[i].lsb_mac_addr,
  2844. elem->mac);
  2845. DP(BNX2X_MSG_SP, "Adding registry entry for [%pM]\n",
  2846. elem->mac);
  2847. list_add_tail(&elem->link,
  2848. &o->registry.exact_match.macs);
  2849. }
  2850. } else {
  2851. elem = list_first_entry(&o->registry.exact_match.macs,
  2852. struct bnx2x_mcast_mac_elem, link);
  2853. DP(BNX2X_MSG_SP, "Deleting a registry\n");
  2854. kfree(elem);
  2855. INIT_LIST_HEAD(&o->registry.exact_match.macs);
  2856. }
  2857. return 0;
  2858. }
  2859. static int bnx2x_mcast_setup_e1(struct bnx2x *bp,
  2860. struct bnx2x_mcast_ramrod_params *p,
  2861. enum bnx2x_mcast_cmd cmd)
  2862. {
  2863. struct bnx2x_mcast_obj *o = p->mcast_obj;
  2864. struct bnx2x_raw_obj *raw = &o->raw;
  2865. struct mac_configuration_cmd *data =
  2866. (struct mac_configuration_cmd *)(raw->rdata);
  2867. int cnt = 0, i, rc;
  2868. /* Reset the ramrod data buffer */
  2869. memset(data, 0, sizeof(*data));
  2870. /* First set all entries as invalid */
  2871. for (i = 0; i < o->max_cmd_len ; i++)
  2872. SET_FLAG(data->config_table[i].flags,
  2873. MAC_CONFIGURATION_ENTRY_ACTION_TYPE,
  2874. T_ETH_MAC_COMMAND_INVALIDATE);
  2875. /* Handle pending commands first */
  2876. cnt = bnx2x_mcast_handle_pending_cmds_e1(bp, p);
  2877. /* If there are no more pending commands - clear SCHEDULED state */
  2878. if (list_empty(&o->pending_cmds_head))
  2879. o->clear_sched(o);
  2880. /* The below may be true iff there were no pending commands */
  2881. if (!cnt)
  2882. cnt = bnx2x_mcast_handle_current_cmd(bp, p, cmd, 0);
  2883. /* For 57710 every command has o->max_cmd_len length to ensure that
  2884. * commands are done one at a time.
  2885. */
  2886. o->total_pending_num -= o->max_cmd_len;
  2887. /* send a ramrod */
  2888. WARN_ON(cnt > o->max_cmd_len);
  2889. /* Set ramrod header (in particular, a number of entries to update) */
  2890. bnx2x_mcast_set_rdata_hdr_e1(bp, p, (u8)cnt);
  2891. /* update a registry: we need the registry contents to be always up
  2892. * to date in order to be able to execute a RESTORE opcode. Here
  2893. * we use the fact that for 57710 we sent one command at a time
  2894. * hence we may take the registry update out of the command handling
  2895. * and do it in a simpler way here.
  2896. */
  2897. rc = bnx2x_mcast_refresh_registry_e1(bp, o);
  2898. if (rc)
  2899. return rc;
  2900. /* If CLEAR_ONLY was requested - don't send a ramrod and clear
  2901. * RAMROD_PENDING status immediately.
  2902. */
  2903. if (test_bit(RAMROD_DRV_CLR_ONLY, &p->ramrod_flags)) {
  2904. raw->clear_pending(raw);
  2905. return 0;
  2906. } else {
  2907. /* No need for an explicit memory barrier here as long as we
  2908. * ensure the ordering of writing to the SPQ element
  2909. * and updating of the SPQ producer which involves a memory
  2910. * read. If the memory read is removed we will have to put a
  2911. * full memory barrier there (inside bnx2x_sp_post()).
  2912. */
  2913. /* Send a ramrod */
  2914. rc = bnx2x_sp_post(bp, RAMROD_CMD_ID_ETH_SET_MAC, raw->cid,
  2915. U64_HI(raw->rdata_mapping),
  2916. U64_LO(raw->rdata_mapping),
  2917. ETH_CONNECTION_TYPE);
  2918. if (rc)
  2919. return rc;
  2920. /* Ramrod completion is pending */
  2921. return 1;
  2922. }
  2923. }
  2924. static int bnx2x_mcast_get_registry_size_exact(struct bnx2x_mcast_obj *o)
  2925. {
  2926. return o->registry.exact_match.num_macs_set;
  2927. }
  2928. static int bnx2x_mcast_get_registry_size_aprox(struct bnx2x_mcast_obj *o)
  2929. {
  2930. return o->registry.aprox_match.num_bins_set;
  2931. }
  2932. static void bnx2x_mcast_set_registry_size_exact(struct bnx2x_mcast_obj *o,
  2933. int n)
  2934. {
  2935. o->registry.exact_match.num_macs_set = n;
  2936. }
  2937. static void bnx2x_mcast_set_registry_size_aprox(struct bnx2x_mcast_obj *o,
  2938. int n)
  2939. {
  2940. o->registry.aprox_match.num_bins_set = n;
  2941. }
  2942. int bnx2x_config_mcast(struct bnx2x *bp,
  2943. struct bnx2x_mcast_ramrod_params *p,
  2944. enum bnx2x_mcast_cmd cmd)
  2945. {
  2946. struct bnx2x_mcast_obj *o = p->mcast_obj;
  2947. struct bnx2x_raw_obj *r = &o->raw;
  2948. int rc = 0, old_reg_size;
  2949. /* This is needed to recover number of currently configured mcast macs
  2950. * in case of failure.
  2951. */
  2952. old_reg_size = o->get_registry_size(o);
  2953. /* Do some calculations and checks */
  2954. rc = o->validate(bp, p, cmd);
  2955. if (rc)
  2956. return rc;
  2957. /* Return if there is no work to do */
  2958. if ((!p->mcast_list_len) && (!o->check_sched(o)))
  2959. return 0;
  2960. DP(BNX2X_MSG_SP, "o->total_pending_num=%d p->mcast_list_len=%d o->max_cmd_len=%d\n",
  2961. o->total_pending_num, p->mcast_list_len, o->max_cmd_len);
  2962. /* Enqueue the current command to the pending list if we can't complete
  2963. * it in the current iteration
  2964. */
  2965. if (r->check_pending(r) ||
  2966. ((o->max_cmd_len > 0) && (o->total_pending_num > o->max_cmd_len))) {
  2967. rc = o->enqueue_cmd(bp, p->mcast_obj, p, cmd);
  2968. if (rc < 0)
  2969. goto error_exit1;
  2970. /* As long as the current command is in a command list we
  2971. * don't need to handle it separately.
  2972. */
  2973. p->mcast_list_len = 0;
  2974. }
  2975. if (!r->check_pending(r)) {
  2976. /* Set 'pending' state */
  2977. r->set_pending(r);
  2978. /* Configure the new classification in the chip */
  2979. rc = o->config_mcast(bp, p, cmd);
  2980. if (rc < 0)
  2981. goto error_exit2;
  2982. /* Wait for a ramrod completion if was requested */
  2983. if (test_bit(RAMROD_COMP_WAIT, &p->ramrod_flags))
  2984. rc = o->wait_comp(bp, o);
  2985. }
  2986. return rc;
  2987. error_exit2:
  2988. r->clear_pending(r);
  2989. error_exit1:
  2990. o->revert(bp, p, old_reg_size);
  2991. return rc;
  2992. }
  2993. static void bnx2x_mcast_clear_sched(struct bnx2x_mcast_obj *o)
  2994. {
  2995. smp_mb__before_atomic();
  2996. clear_bit(o->sched_state, o->raw.pstate);
  2997. smp_mb__after_atomic();
  2998. }
  2999. static void bnx2x_mcast_set_sched(struct bnx2x_mcast_obj *o)
  3000. {
  3001. smp_mb__before_atomic();
  3002. set_bit(o->sched_state, o->raw.pstate);
  3003. smp_mb__after_atomic();
  3004. }
  3005. static bool bnx2x_mcast_check_sched(struct bnx2x_mcast_obj *o)
  3006. {
  3007. return !!test_bit(o->sched_state, o->raw.pstate);
  3008. }
  3009. static bool bnx2x_mcast_check_pending(struct bnx2x_mcast_obj *o)
  3010. {
  3011. return o->raw.check_pending(&o->raw) || o->check_sched(o);
  3012. }
  3013. void bnx2x_init_mcast_obj(struct bnx2x *bp,
  3014. struct bnx2x_mcast_obj *mcast_obj,
  3015. u8 mcast_cl_id, u32 mcast_cid, u8 func_id,
  3016. u8 engine_id, void *rdata, dma_addr_t rdata_mapping,
  3017. int state, unsigned long *pstate, bnx2x_obj_type type)
  3018. {
  3019. memset(mcast_obj, 0, sizeof(*mcast_obj));
  3020. bnx2x_init_raw_obj(&mcast_obj->raw, mcast_cl_id, mcast_cid, func_id,
  3021. rdata, rdata_mapping, state, pstate, type);
  3022. mcast_obj->engine_id = engine_id;
  3023. INIT_LIST_HEAD(&mcast_obj->pending_cmds_head);
  3024. mcast_obj->sched_state = BNX2X_FILTER_MCAST_SCHED;
  3025. mcast_obj->check_sched = bnx2x_mcast_check_sched;
  3026. mcast_obj->set_sched = bnx2x_mcast_set_sched;
  3027. mcast_obj->clear_sched = bnx2x_mcast_clear_sched;
  3028. if (CHIP_IS_E1(bp)) {
  3029. mcast_obj->config_mcast = bnx2x_mcast_setup_e1;
  3030. mcast_obj->enqueue_cmd = bnx2x_mcast_enqueue_cmd;
  3031. mcast_obj->hdl_restore =
  3032. bnx2x_mcast_handle_restore_cmd_e1;
  3033. mcast_obj->check_pending = bnx2x_mcast_check_pending;
  3034. if (CHIP_REV_IS_SLOW(bp))
  3035. mcast_obj->max_cmd_len = BNX2X_MAX_EMUL_MULTI;
  3036. else
  3037. mcast_obj->max_cmd_len = BNX2X_MAX_MULTICAST;
  3038. mcast_obj->wait_comp = bnx2x_mcast_wait;
  3039. mcast_obj->set_one_rule = bnx2x_mcast_set_one_rule_e1;
  3040. mcast_obj->validate = bnx2x_mcast_validate_e1;
  3041. mcast_obj->revert = bnx2x_mcast_revert_e1;
  3042. mcast_obj->get_registry_size =
  3043. bnx2x_mcast_get_registry_size_exact;
  3044. mcast_obj->set_registry_size =
  3045. bnx2x_mcast_set_registry_size_exact;
  3046. /* 57710 is the only chip that uses the exact match for mcast
  3047. * at the moment.
  3048. */
  3049. INIT_LIST_HEAD(&mcast_obj->registry.exact_match.macs);
  3050. } else if (CHIP_IS_E1H(bp)) {
  3051. mcast_obj->config_mcast = bnx2x_mcast_setup_e1h;
  3052. mcast_obj->enqueue_cmd = NULL;
  3053. mcast_obj->hdl_restore = NULL;
  3054. mcast_obj->check_pending = bnx2x_mcast_check_pending;
  3055. /* 57711 doesn't send a ramrod, so it has unlimited credit
  3056. * for one command.
  3057. */
  3058. mcast_obj->max_cmd_len = -1;
  3059. mcast_obj->wait_comp = bnx2x_mcast_wait;
  3060. mcast_obj->set_one_rule = NULL;
  3061. mcast_obj->validate = bnx2x_mcast_validate_e1h;
  3062. mcast_obj->revert = bnx2x_mcast_revert_e1h;
  3063. mcast_obj->get_registry_size =
  3064. bnx2x_mcast_get_registry_size_aprox;
  3065. mcast_obj->set_registry_size =
  3066. bnx2x_mcast_set_registry_size_aprox;
  3067. } else {
  3068. mcast_obj->config_mcast = bnx2x_mcast_setup_e2;
  3069. mcast_obj->enqueue_cmd = bnx2x_mcast_enqueue_cmd;
  3070. mcast_obj->hdl_restore =
  3071. bnx2x_mcast_handle_restore_cmd_e2;
  3072. mcast_obj->check_pending = bnx2x_mcast_check_pending;
  3073. /* TODO: There should be a proper HSI define for this number!!!
  3074. */
  3075. mcast_obj->max_cmd_len = 16;
  3076. mcast_obj->wait_comp = bnx2x_mcast_wait;
  3077. mcast_obj->set_one_rule = bnx2x_mcast_set_one_rule_e2;
  3078. mcast_obj->validate = bnx2x_mcast_validate_e2;
  3079. mcast_obj->revert = bnx2x_mcast_revert_e2;
  3080. mcast_obj->get_registry_size =
  3081. bnx2x_mcast_get_registry_size_aprox;
  3082. mcast_obj->set_registry_size =
  3083. bnx2x_mcast_set_registry_size_aprox;
  3084. }
  3085. }
  3086. /*************************** Credit handling **********************************/
  3087. /**
  3088. * atomic_add_ifless - add if the result is less than a given value.
  3089. *
  3090. * @v: pointer of type atomic_t
  3091. * @a: the amount to add to v...
  3092. * @u: ...if (v + a) is less than u.
  3093. *
  3094. * returns true if (v + a) was less than u, and false otherwise.
  3095. *
  3096. */
  3097. static inline bool __atomic_add_ifless(atomic_t *v, int a, int u)
  3098. {
  3099. int c, old;
  3100. c = atomic_read(v);
  3101. for (;;) {
  3102. if (unlikely(c + a >= u))
  3103. return false;
  3104. old = atomic_cmpxchg((v), c, c + a);
  3105. if (likely(old == c))
  3106. break;
  3107. c = old;
  3108. }
  3109. return true;
  3110. }
  3111. /**
  3112. * atomic_dec_ifmoe - dec if the result is more or equal than a given value.
  3113. *
  3114. * @v: pointer of type atomic_t
  3115. * @a: the amount to dec from v...
  3116. * @u: ...if (v - a) is more or equal than u.
  3117. *
  3118. * returns true if (v - a) was more or equal than u, and false
  3119. * otherwise.
  3120. */
  3121. static inline bool __atomic_dec_ifmoe(atomic_t *v, int a, int u)
  3122. {
  3123. int c, old;
  3124. c = atomic_read(v);
  3125. for (;;) {
  3126. if (unlikely(c - a < u))
  3127. return false;
  3128. old = atomic_cmpxchg((v), c, c - a);
  3129. if (likely(old == c))
  3130. break;
  3131. c = old;
  3132. }
  3133. return true;
  3134. }
  3135. static bool bnx2x_credit_pool_get(struct bnx2x_credit_pool_obj *o, int cnt)
  3136. {
  3137. bool rc;
  3138. smp_mb();
  3139. rc = __atomic_dec_ifmoe(&o->credit, cnt, 0);
  3140. smp_mb();
  3141. return rc;
  3142. }
  3143. static bool bnx2x_credit_pool_put(struct bnx2x_credit_pool_obj *o, int cnt)
  3144. {
  3145. bool rc;
  3146. smp_mb();
  3147. /* Don't let to refill if credit + cnt > pool_sz */
  3148. rc = __atomic_add_ifless(&o->credit, cnt, o->pool_sz + 1);
  3149. smp_mb();
  3150. return rc;
  3151. }
  3152. static int bnx2x_credit_pool_check(struct bnx2x_credit_pool_obj *o)
  3153. {
  3154. int cur_credit;
  3155. smp_mb();
  3156. cur_credit = atomic_read(&o->credit);
  3157. return cur_credit;
  3158. }
  3159. static bool bnx2x_credit_pool_always_true(struct bnx2x_credit_pool_obj *o,
  3160. int cnt)
  3161. {
  3162. return true;
  3163. }
  3164. static bool bnx2x_credit_pool_get_entry(
  3165. struct bnx2x_credit_pool_obj *o,
  3166. int *offset)
  3167. {
  3168. int idx, vec, i;
  3169. *offset = -1;
  3170. /* Find "internal cam-offset" then add to base for this object... */
  3171. for (vec = 0; vec < BNX2X_POOL_VEC_SIZE; vec++) {
  3172. /* Skip the current vector if there are no free entries in it */
  3173. if (!o->pool_mirror[vec])
  3174. continue;
  3175. /* If we've got here we are going to find a free entry */
  3176. for (idx = vec * BIT_VEC64_ELEM_SZ, i = 0;
  3177. i < BIT_VEC64_ELEM_SZ; idx++, i++)
  3178. if (BIT_VEC64_TEST_BIT(o->pool_mirror, idx)) {
  3179. /* Got one!! */
  3180. BIT_VEC64_CLEAR_BIT(o->pool_mirror, idx);
  3181. *offset = o->base_pool_offset + idx;
  3182. return true;
  3183. }
  3184. }
  3185. return false;
  3186. }
  3187. static bool bnx2x_credit_pool_put_entry(
  3188. struct bnx2x_credit_pool_obj *o,
  3189. int offset)
  3190. {
  3191. if (offset < o->base_pool_offset)
  3192. return false;
  3193. offset -= o->base_pool_offset;
  3194. if (offset >= o->pool_sz)
  3195. return false;
  3196. /* Return the entry to the pool */
  3197. BIT_VEC64_SET_BIT(o->pool_mirror, offset);
  3198. return true;
  3199. }
  3200. static bool bnx2x_credit_pool_put_entry_always_true(
  3201. struct bnx2x_credit_pool_obj *o,
  3202. int offset)
  3203. {
  3204. return true;
  3205. }
  3206. static bool bnx2x_credit_pool_get_entry_always_true(
  3207. struct bnx2x_credit_pool_obj *o,
  3208. int *offset)
  3209. {
  3210. *offset = -1;
  3211. return true;
  3212. }
  3213. /**
  3214. * bnx2x_init_credit_pool - initialize credit pool internals.
  3215. *
  3216. * @p:
  3217. * @base: Base entry in the CAM to use.
  3218. * @credit: pool size.
  3219. *
  3220. * If base is negative no CAM entries handling will be performed.
  3221. * If credit is negative pool operations will always succeed (unlimited pool).
  3222. *
  3223. */
  3224. static inline void bnx2x_init_credit_pool(struct bnx2x_credit_pool_obj *p,
  3225. int base, int credit)
  3226. {
  3227. /* Zero the object first */
  3228. memset(p, 0, sizeof(*p));
  3229. /* Set the table to all 1s */
  3230. memset(&p->pool_mirror, 0xff, sizeof(p->pool_mirror));
  3231. /* Init a pool as full */
  3232. atomic_set(&p->credit, credit);
  3233. /* The total poll size */
  3234. p->pool_sz = credit;
  3235. p->base_pool_offset = base;
  3236. /* Commit the change */
  3237. smp_mb();
  3238. p->check = bnx2x_credit_pool_check;
  3239. /* if pool credit is negative - disable the checks */
  3240. if (credit >= 0) {
  3241. p->put = bnx2x_credit_pool_put;
  3242. p->get = bnx2x_credit_pool_get;
  3243. p->put_entry = bnx2x_credit_pool_put_entry;
  3244. p->get_entry = bnx2x_credit_pool_get_entry;
  3245. } else {
  3246. p->put = bnx2x_credit_pool_always_true;
  3247. p->get = bnx2x_credit_pool_always_true;
  3248. p->put_entry = bnx2x_credit_pool_put_entry_always_true;
  3249. p->get_entry = bnx2x_credit_pool_get_entry_always_true;
  3250. }
  3251. /* If base is negative - disable entries handling */
  3252. if (base < 0) {
  3253. p->put_entry = bnx2x_credit_pool_put_entry_always_true;
  3254. p->get_entry = bnx2x_credit_pool_get_entry_always_true;
  3255. }
  3256. }
  3257. void bnx2x_init_mac_credit_pool(struct bnx2x *bp,
  3258. struct bnx2x_credit_pool_obj *p, u8 func_id,
  3259. u8 func_num)
  3260. {
  3261. /* TODO: this will be defined in consts as well... */
  3262. #define BNX2X_CAM_SIZE_EMUL 5
  3263. int cam_sz;
  3264. if (CHIP_IS_E1(bp)) {
  3265. /* In E1, Multicast is saved in cam... */
  3266. if (!CHIP_REV_IS_SLOW(bp))
  3267. cam_sz = (MAX_MAC_CREDIT_E1 / 2) - BNX2X_MAX_MULTICAST;
  3268. else
  3269. cam_sz = BNX2X_CAM_SIZE_EMUL - BNX2X_MAX_EMUL_MULTI;
  3270. bnx2x_init_credit_pool(p, func_id * cam_sz, cam_sz);
  3271. } else if (CHIP_IS_E1H(bp)) {
  3272. /* CAM credit is equaly divided between all active functions
  3273. * on the PORT!.
  3274. */
  3275. if ((func_num > 0)) {
  3276. if (!CHIP_REV_IS_SLOW(bp))
  3277. cam_sz = (MAX_MAC_CREDIT_E1H / (2*func_num));
  3278. else
  3279. cam_sz = BNX2X_CAM_SIZE_EMUL;
  3280. bnx2x_init_credit_pool(p, func_id * cam_sz, cam_sz);
  3281. } else {
  3282. /* this should never happen! Block MAC operations. */
  3283. bnx2x_init_credit_pool(p, 0, 0);
  3284. }
  3285. } else {
  3286. /* CAM credit is equaly divided between all active functions
  3287. * on the PATH.
  3288. */
  3289. if ((func_num > 0)) {
  3290. if (!CHIP_REV_IS_SLOW(bp))
  3291. cam_sz = (MAX_MAC_CREDIT_E2 / func_num);
  3292. else
  3293. cam_sz = BNX2X_CAM_SIZE_EMUL;
  3294. /* No need for CAM entries handling for 57712 and
  3295. * newer.
  3296. */
  3297. bnx2x_init_credit_pool(p, -1, cam_sz);
  3298. } else {
  3299. /* this should never happen! Block MAC operations. */
  3300. bnx2x_init_credit_pool(p, 0, 0);
  3301. }
  3302. }
  3303. }
  3304. void bnx2x_init_vlan_credit_pool(struct bnx2x *bp,
  3305. struct bnx2x_credit_pool_obj *p,
  3306. u8 func_id,
  3307. u8 func_num)
  3308. {
  3309. if (CHIP_IS_E1x(bp)) {
  3310. /* There is no VLAN credit in HW on 57710 and 57711 only
  3311. * MAC / MAC-VLAN can be set
  3312. */
  3313. bnx2x_init_credit_pool(p, 0, -1);
  3314. } else {
  3315. /* CAM credit is equally divided between all active functions
  3316. * on the PATH.
  3317. */
  3318. if (func_num > 0) {
  3319. int credit = MAX_VLAN_CREDIT_E2 / func_num;
  3320. bnx2x_init_credit_pool(p, func_id * credit, credit);
  3321. } else
  3322. /* this should never happen! Block VLAN operations. */
  3323. bnx2x_init_credit_pool(p, 0, 0);
  3324. }
  3325. }
  3326. /****************** RSS Configuration ******************/
  3327. /**
  3328. * bnx2x_debug_print_ind_table - prints the indirection table configuration.
  3329. *
  3330. * @bp: driver handle
  3331. * @p: pointer to rss configuration
  3332. *
  3333. * Prints it when NETIF_MSG_IFUP debug level is configured.
  3334. */
  3335. static inline void bnx2x_debug_print_ind_table(struct bnx2x *bp,
  3336. struct bnx2x_config_rss_params *p)
  3337. {
  3338. int i;
  3339. DP(BNX2X_MSG_SP, "Setting indirection table to:\n");
  3340. DP(BNX2X_MSG_SP, "0x0000: ");
  3341. for (i = 0; i < T_ETH_INDIRECTION_TABLE_SIZE; i++) {
  3342. DP_CONT(BNX2X_MSG_SP, "0x%02x ", p->ind_table[i]);
  3343. /* Print 4 bytes in a line */
  3344. if ((i + 1 < T_ETH_INDIRECTION_TABLE_SIZE) &&
  3345. (((i + 1) & 0x3) == 0)) {
  3346. DP_CONT(BNX2X_MSG_SP, "\n");
  3347. DP(BNX2X_MSG_SP, "0x%04x: ", i + 1);
  3348. }
  3349. }
  3350. DP_CONT(BNX2X_MSG_SP, "\n");
  3351. }
  3352. /**
  3353. * bnx2x_setup_rss - configure RSS
  3354. *
  3355. * @bp: device handle
  3356. * @p: rss configuration
  3357. *
  3358. * sends on UPDATE ramrod for that matter.
  3359. */
  3360. static int bnx2x_setup_rss(struct bnx2x *bp,
  3361. struct bnx2x_config_rss_params *p)
  3362. {
  3363. struct bnx2x_rss_config_obj *o = p->rss_obj;
  3364. struct bnx2x_raw_obj *r = &o->raw;
  3365. struct eth_rss_update_ramrod_data *data =
  3366. (struct eth_rss_update_ramrod_data *)(r->rdata);
  3367. u8 rss_mode = 0;
  3368. int rc;
  3369. memset(data, 0, sizeof(*data));
  3370. DP(BNX2X_MSG_SP, "Configuring RSS\n");
  3371. /* Set an echo field */
  3372. data->echo = cpu_to_le32((r->cid & BNX2X_SWCID_MASK) |
  3373. (r->state << BNX2X_SWCID_SHIFT));
  3374. /* RSS mode */
  3375. if (test_bit(BNX2X_RSS_MODE_DISABLED, &p->rss_flags))
  3376. rss_mode = ETH_RSS_MODE_DISABLED;
  3377. else if (test_bit(BNX2X_RSS_MODE_REGULAR, &p->rss_flags))
  3378. rss_mode = ETH_RSS_MODE_REGULAR;
  3379. data->rss_mode = rss_mode;
  3380. DP(BNX2X_MSG_SP, "rss_mode=%d\n", rss_mode);
  3381. /* RSS capabilities */
  3382. if (test_bit(BNX2X_RSS_IPV4, &p->rss_flags))
  3383. data->capabilities |=
  3384. ETH_RSS_UPDATE_RAMROD_DATA_IPV4_CAPABILITY;
  3385. if (test_bit(BNX2X_RSS_IPV4_TCP, &p->rss_flags))
  3386. data->capabilities |=
  3387. ETH_RSS_UPDATE_RAMROD_DATA_IPV4_TCP_CAPABILITY;
  3388. if (test_bit(BNX2X_RSS_IPV4_UDP, &p->rss_flags))
  3389. data->capabilities |=
  3390. ETH_RSS_UPDATE_RAMROD_DATA_IPV4_UDP_CAPABILITY;
  3391. if (test_bit(BNX2X_RSS_IPV6, &p->rss_flags))
  3392. data->capabilities |=
  3393. ETH_RSS_UPDATE_RAMROD_DATA_IPV6_CAPABILITY;
  3394. if (test_bit(BNX2X_RSS_IPV6_TCP, &p->rss_flags))
  3395. data->capabilities |=
  3396. ETH_RSS_UPDATE_RAMROD_DATA_IPV6_TCP_CAPABILITY;
  3397. if (test_bit(BNX2X_RSS_IPV6_UDP, &p->rss_flags))
  3398. data->capabilities |=
  3399. ETH_RSS_UPDATE_RAMROD_DATA_IPV6_UDP_CAPABILITY;
  3400. /* Hashing mask */
  3401. data->rss_result_mask = p->rss_result_mask;
  3402. /* RSS engine ID */
  3403. data->rss_engine_id = o->engine_id;
  3404. DP(BNX2X_MSG_SP, "rss_engine_id=%d\n", data->rss_engine_id);
  3405. /* Indirection table */
  3406. memcpy(data->indirection_table, p->ind_table,
  3407. T_ETH_INDIRECTION_TABLE_SIZE);
  3408. /* Remember the last configuration */
  3409. memcpy(o->ind_table, p->ind_table, T_ETH_INDIRECTION_TABLE_SIZE);
  3410. /* Print the indirection table */
  3411. if (netif_msg_ifup(bp))
  3412. bnx2x_debug_print_ind_table(bp, p);
  3413. /* RSS keys */
  3414. if (test_bit(BNX2X_RSS_SET_SRCH, &p->rss_flags)) {
  3415. memcpy(&data->rss_key[0], &p->rss_key[0],
  3416. sizeof(data->rss_key));
  3417. data->capabilities |= ETH_RSS_UPDATE_RAMROD_DATA_UPDATE_RSS_KEY;
  3418. }
  3419. /* No need for an explicit memory barrier here as long as we
  3420. * ensure the ordering of writing to the SPQ element
  3421. * and updating of the SPQ producer which involves a memory
  3422. * read. If the memory read is removed we will have to put a
  3423. * full memory barrier there (inside bnx2x_sp_post()).
  3424. */
  3425. /* Send a ramrod */
  3426. rc = bnx2x_sp_post(bp, RAMROD_CMD_ID_ETH_RSS_UPDATE, r->cid,
  3427. U64_HI(r->rdata_mapping),
  3428. U64_LO(r->rdata_mapping),
  3429. ETH_CONNECTION_TYPE);
  3430. if (rc < 0)
  3431. return rc;
  3432. return 1;
  3433. }
  3434. void bnx2x_get_rss_ind_table(struct bnx2x_rss_config_obj *rss_obj,
  3435. u8 *ind_table)
  3436. {
  3437. memcpy(ind_table, rss_obj->ind_table, sizeof(rss_obj->ind_table));
  3438. }
  3439. int bnx2x_config_rss(struct bnx2x *bp,
  3440. struct bnx2x_config_rss_params *p)
  3441. {
  3442. int rc;
  3443. struct bnx2x_rss_config_obj *o = p->rss_obj;
  3444. struct bnx2x_raw_obj *r = &o->raw;
  3445. /* Do nothing if only driver cleanup was requested */
  3446. if (test_bit(RAMROD_DRV_CLR_ONLY, &p->ramrod_flags)) {
  3447. DP(BNX2X_MSG_SP, "Not configuring RSS ramrod_flags=%lx\n",
  3448. p->ramrod_flags);
  3449. return 0;
  3450. }
  3451. r->set_pending(r);
  3452. rc = o->config_rss(bp, p);
  3453. if (rc < 0) {
  3454. r->clear_pending(r);
  3455. return rc;
  3456. }
  3457. if (test_bit(RAMROD_COMP_WAIT, &p->ramrod_flags))
  3458. rc = r->wait_comp(bp, r);
  3459. return rc;
  3460. }
  3461. void bnx2x_init_rss_config_obj(struct bnx2x *bp,
  3462. struct bnx2x_rss_config_obj *rss_obj,
  3463. u8 cl_id, u32 cid, u8 func_id, u8 engine_id,
  3464. void *rdata, dma_addr_t rdata_mapping,
  3465. int state, unsigned long *pstate,
  3466. bnx2x_obj_type type)
  3467. {
  3468. bnx2x_init_raw_obj(&rss_obj->raw, cl_id, cid, func_id, rdata,
  3469. rdata_mapping, state, pstate, type);
  3470. rss_obj->engine_id = engine_id;
  3471. rss_obj->config_rss = bnx2x_setup_rss;
  3472. }
  3473. /********************** Queue state object ***********************************/
  3474. /**
  3475. * bnx2x_queue_state_change - perform Queue state change transition
  3476. *
  3477. * @bp: device handle
  3478. * @params: parameters to perform the transition
  3479. *
  3480. * returns 0 in case of successfully completed transition, negative error
  3481. * code in case of failure, positive (EBUSY) value if there is a completion
  3482. * to that is still pending (possible only if RAMROD_COMP_WAIT is
  3483. * not set in params->ramrod_flags for asynchronous commands).
  3484. *
  3485. */
  3486. int bnx2x_queue_state_change(struct bnx2x *bp,
  3487. struct bnx2x_queue_state_params *params)
  3488. {
  3489. struct bnx2x_queue_sp_obj *o = params->q_obj;
  3490. int rc, pending_bit;
  3491. unsigned long *pending = &o->pending;
  3492. /* Check that the requested transition is legal */
  3493. rc = o->check_transition(bp, o, params);
  3494. if (rc) {
  3495. BNX2X_ERR("check transition returned an error. rc %d\n", rc);
  3496. return -EINVAL;
  3497. }
  3498. /* Set "pending" bit */
  3499. DP(BNX2X_MSG_SP, "pending bit was=%lx\n", o->pending);
  3500. pending_bit = o->set_pending(o, params);
  3501. DP(BNX2X_MSG_SP, "pending bit now=%lx\n", o->pending);
  3502. /* Don't send a command if only driver cleanup was requested */
  3503. if (test_bit(RAMROD_DRV_CLR_ONLY, &params->ramrod_flags))
  3504. o->complete_cmd(bp, o, pending_bit);
  3505. else {
  3506. /* Send a ramrod */
  3507. rc = o->send_cmd(bp, params);
  3508. if (rc) {
  3509. o->next_state = BNX2X_Q_STATE_MAX;
  3510. clear_bit(pending_bit, pending);
  3511. smp_mb__after_atomic();
  3512. return rc;
  3513. }
  3514. if (test_bit(RAMROD_COMP_WAIT, &params->ramrod_flags)) {
  3515. rc = o->wait_comp(bp, o, pending_bit);
  3516. if (rc)
  3517. return rc;
  3518. return 0;
  3519. }
  3520. }
  3521. return !!test_bit(pending_bit, pending);
  3522. }
  3523. static int bnx2x_queue_set_pending(struct bnx2x_queue_sp_obj *obj,
  3524. struct bnx2x_queue_state_params *params)
  3525. {
  3526. enum bnx2x_queue_cmd cmd = params->cmd, bit;
  3527. /* ACTIVATE and DEACTIVATE commands are implemented on top of
  3528. * UPDATE command.
  3529. */
  3530. if ((cmd == BNX2X_Q_CMD_ACTIVATE) ||
  3531. (cmd == BNX2X_Q_CMD_DEACTIVATE))
  3532. bit = BNX2X_Q_CMD_UPDATE;
  3533. else
  3534. bit = cmd;
  3535. set_bit(bit, &obj->pending);
  3536. return bit;
  3537. }
  3538. static int bnx2x_queue_wait_comp(struct bnx2x *bp,
  3539. struct bnx2x_queue_sp_obj *o,
  3540. enum bnx2x_queue_cmd cmd)
  3541. {
  3542. return bnx2x_state_wait(bp, cmd, &o->pending);
  3543. }
  3544. /**
  3545. * bnx2x_queue_comp_cmd - complete the state change command.
  3546. *
  3547. * @bp: device handle
  3548. * @o:
  3549. * @cmd:
  3550. *
  3551. * Checks that the arrived completion is expected.
  3552. */
  3553. static int bnx2x_queue_comp_cmd(struct bnx2x *bp,
  3554. struct bnx2x_queue_sp_obj *o,
  3555. enum bnx2x_queue_cmd cmd)
  3556. {
  3557. unsigned long cur_pending = o->pending;
  3558. if (!test_and_clear_bit(cmd, &cur_pending)) {
  3559. BNX2X_ERR("Bad MC reply %d for queue %d in state %d pending 0x%lx, next_state %d\n",
  3560. cmd, o->cids[BNX2X_PRIMARY_CID_INDEX],
  3561. o->state, cur_pending, o->next_state);
  3562. return -EINVAL;
  3563. }
  3564. if (o->next_tx_only >= o->max_cos)
  3565. /* >= because tx only must always be smaller than cos since the
  3566. * primary connection supports COS 0
  3567. */
  3568. BNX2X_ERR("illegal value for next tx_only: %d. max cos was %d",
  3569. o->next_tx_only, o->max_cos);
  3570. DP(BNX2X_MSG_SP,
  3571. "Completing command %d for queue %d, setting state to %d\n",
  3572. cmd, o->cids[BNX2X_PRIMARY_CID_INDEX], o->next_state);
  3573. if (o->next_tx_only) /* print num tx-only if any exist */
  3574. DP(BNX2X_MSG_SP, "primary cid %d: num tx-only cons %d\n",
  3575. o->cids[BNX2X_PRIMARY_CID_INDEX], o->next_tx_only);
  3576. o->state = o->next_state;
  3577. o->num_tx_only = o->next_tx_only;
  3578. o->next_state = BNX2X_Q_STATE_MAX;
  3579. /* It's important that o->state and o->next_state are
  3580. * updated before o->pending.
  3581. */
  3582. wmb();
  3583. clear_bit(cmd, &o->pending);
  3584. smp_mb__after_atomic();
  3585. return 0;
  3586. }
  3587. static void bnx2x_q_fill_setup_data_e2(struct bnx2x *bp,
  3588. struct bnx2x_queue_state_params *cmd_params,
  3589. struct client_init_ramrod_data *data)
  3590. {
  3591. struct bnx2x_queue_setup_params *params = &cmd_params->params.setup;
  3592. /* Rx data */
  3593. /* IPv6 TPA supported for E2 and above only */
  3594. data->rx.tpa_en |= test_bit(BNX2X_Q_FLG_TPA_IPV6, &params->flags) *
  3595. CLIENT_INIT_RX_DATA_TPA_EN_IPV6;
  3596. }
  3597. static void bnx2x_q_fill_init_general_data(struct bnx2x *bp,
  3598. struct bnx2x_queue_sp_obj *o,
  3599. struct bnx2x_general_setup_params *params,
  3600. struct client_init_general_data *gen_data,
  3601. unsigned long *flags)
  3602. {
  3603. gen_data->client_id = o->cl_id;
  3604. if (test_bit(BNX2X_Q_FLG_STATS, flags)) {
  3605. gen_data->statistics_counter_id =
  3606. params->stat_id;
  3607. gen_data->statistics_en_flg = 1;
  3608. gen_data->statistics_zero_flg =
  3609. test_bit(BNX2X_Q_FLG_ZERO_STATS, flags);
  3610. } else
  3611. gen_data->statistics_counter_id =
  3612. DISABLE_STATISTIC_COUNTER_ID_VALUE;
  3613. gen_data->is_fcoe_flg = test_bit(BNX2X_Q_FLG_FCOE, flags);
  3614. gen_data->activate_flg = test_bit(BNX2X_Q_FLG_ACTIVE, flags);
  3615. gen_data->sp_client_id = params->spcl_id;
  3616. gen_data->mtu = cpu_to_le16(params->mtu);
  3617. gen_data->func_id = o->func_id;
  3618. gen_data->cos = params->cos;
  3619. gen_data->traffic_type =
  3620. test_bit(BNX2X_Q_FLG_FCOE, flags) ?
  3621. LLFC_TRAFFIC_TYPE_FCOE : LLFC_TRAFFIC_TYPE_NW;
  3622. DP(BNX2X_MSG_SP, "flags: active %d, cos %d, stats en %d\n",
  3623. gen_data->activate_flg, gen_data->cos, gen_data->statistics_en_flg);
  3624. }
  3625. static void bnx2x_q_fill_init_tx_data(struct bnx2x_queue_sp_obj *o,
  3626. struct bnx2x_txq_setup_params *params,
  3627. struct client_init_tx_data *tx_data,
  3628. unsigned long *flags)
  3629. {
  3630. tx_data->enforce_security_flg =
  3631. test_bit(BNX2X_Q_FLG_TX_SEC, flags);
  3632. tx_data->default_vlan =
  3633. cpu_to_le16(params->default_vlan);
  3634. tx_data->default_vlan_flg =
  3635. test_bit(BNX2X_Q_FLG_DEF_VLAN, flags);
  3636. tx_data->tx_switching_flg =
  3637. test_bit(BNX2X_Q_FLG_TX_SWITCH, flags);
  3638. tx_data->anti_spoofing_flg =
  3639. test_bit(BNX2X_Q_FLG_ANTI_SPOOF, flags);
  3640. tx_data->force_default_pri_flg =
  3641. test_bit(BNX2X_Q_FLG_FORCE_DEFAULT_PRI, flags);
  3642. tx_data->tunnel_lso_inc_ip_id =
  3643. test_bit(BNX2X_Q_FLG_TUN_INC_INNER_IP_ID, flags);
  3644. tx_data->tunnel_non_lso_pcsum_location =
  3645. test_bit(BNX2X_Q_FLG_PCSUM_ON_PKT, flags) ? PCSUM_ON_PKT :
  3646. PCSUM_ON_BD;
  3647. tx_data->tx_status_block_id = params->fw_sb_id;
  3648. tx_data->tx_sb_index_number = params->sb_cq_index;
  3649. tx_data->tss_leading_client_id = params->tss_leading_cl_id;
  3650. tx_data->tx_bd_page_base.lo =
  3651. cpu_to_le32(U64_LO(params->dscr_map));
  3652. tx_data->tx_bd_page_base.hi =
  3653. cpu_to_le32(U64_HI(params->dscr_map));
  3654. /* Don't configure any Tx switching mode during queue SETUP */
  3655. tx_data->state = 0;
  3656. }
  3657. static void bnx2x_q_fill_init_pause_data(struct bnx2x_queue_sp_obj *o,
  3658. struct rxq_pause_params *params,
  3659. struct client_init_rx_data *rx_data)
  3660. {
  3661. /* flow control data */
  3662. rx_data->cqe_pause_thr_low = cpu_to_le16(params->rcq_th_lo);
  3663. rx_data->cqe_pause_thr_high = cpu_to_le16(params->rcq_th_hi);
  3664. rx_data->bd_pause_thr_low = cpu_to_le16(params->bd_th_lo);
  3665. rx_data->bd_pause_thr_high = cpu_to_le16(params->bd_th_hi);
  3666. rx_data->sge_pause_thr_low = cpu_to_le16(params->sge_th_lo);
  3667. rx_data->sge_pause_thr_high = cpu_to_le16(params->sge_th_hi);
  3668. rx_data->rx_cos_mask = cpu_to_le16(params->pri_map);
  3669. }
  3670. static void bnx2x_q_fill_init_rx_data(struct bnx2x_queue_sp_obj *o,
  3671. struct bnx2x_rxq_setup_params *params,
  3672. struct client_init_rx_data *rx_data,
  3673. unsigned long *flags)
  3674. {
  3675. rx_data->tpa_en = test_bit(BNX2X_Q_FLG_TPA, flags) *
  3676. CLIENT_INIT_RX_DATA_TPA_EN_IPV4;
  3677. rx_data->tpa_en |= test_bit(BNX2X_Q_FLG_TPA_GRO, flags) *
  3678. CLIENT_INIT_RX_DATA_TPA_MODE;
  3679. rx_data->vmqueue_mode_en_flg = 0;
  3680. rx_data->cache_line_alignment_log_size =
  3681. params->cache_line_log;
  3682. rx_data->enable_dynamic_hc =
  3683. test_bit(BNX2X_Q_FLG_DHC, flags);
  3684. rx_data->max_sges_for_packet = params->max_sges_pkt;
  3685. rx_data->client_qzone_id = params->cl_qzone_id;
  3686. rx_data->max_agg_size = cpu_to_le16(params->tpa_agg_sz);
  3687. /* Always start in DROP_ALL mode */
  3688. rx_data->state = cpu_to_le16(CLIENT_INIT_RX_DATA_UCAST_DROP_ALL |
  3689. CLIENT_INIT_RX_DATA_MCAST_DROP_ALL);
  3690. /* We don't set drop flags */
  3691. rx_data->drop_ip_cs_err_flg = 0;
  3692. rx_data->drop_tcp_cs_err_flg = 0;
  3693. rx_data->drop_ttl0_flg = 0;
  3694. rx_data->drop_udp_cs_err_flg = 0;
  3695. rx_data->inner_vlan_removal_enable_flg =
  3696. test_bit(BNX2X_Q_FLG_VLAN, flags);
  3697. rx_data->outer_vlan_removal_enable_flg =
  3698. test_bit(BNX2X_Q_FLG_OV, flags);
  3699. rx_data->status_block_id = params->fw_sb_id;
  3700. rx_data->rx_sb_index_number = params->sb_cq_index;
  3701. rx_data->max_tpa_queues = params->max_tpa_queues;
  3702. rx_data->max_bytes_on_bd = cpu_to_le16(params->buf_sz);
  3703. rx_data->sge_buff_size = cpu_to_le16(params->sge_buf_sz);
  3704. rx_data->bd_page_base.lo =
  3705. cpu_to_le32(U64_LO(params->dscr_map));
  3706. rx_data->bd_page_base.hi =
  3707. cpu_to_le32(U64_HI(params->dscr_map));
  3708. rx_data->sge_page_base.lo =
  3709. cpu_to_le32(U64_LO(params->sge_map));
  3710. rx_data->sge_page_base.hi =
  3711. cpu_to_le32(U64_HI(params->sge_map));
  3712. rx_data->cqe_page_base.lo =
  3713. cpu_to_le32(U64_LO(params->rcq_map));
  3714. rx_data->cqe_page_base.hi =
  3715. cpu_to_le32(U64_HI(params->rcq_map));
  3716. rx_data->is_leading_rss = test_bit(BNX2X_Q_FLG_LEADING_RSS, flags);
  3717. if (test_bit(BNX2X_Q_FLG_MCAST, flags)) {
  3718. rx_data->approx_mcast_engine_id = params->mcast_engine_id;
  3719. rx_data->is_approx_mcast = 1;
  3720. }
  3721. rx_data->rss_engine_id = params->rss_engine_id;
  3722. /* silent vlan removal */
  3723. rx_data->silent_vlan_removal_flg =
  3724. test_bit(BNX2X_Q_FLG_SILENT_VLAN_REM, flags);
  3725. rx_data->silent_vlan_value =
  3726. cpu_to_le16(params->silent_removal_value);
  3727. rx_data->silent_vlan_mask =
  3728. cpu_to_le16(params->silent_removal_mask);
  3729. }
  3730. /* initialize the general, tx and rx parts of a queue object */
  3731. static void bnx2x_q_fill_setup_data_cmn(struct bnx2x *bp,
  3732. struct bnx2x_queue_state_params *cmd_params,
  3733. struct client_init_ramrod_data *data)
  3734. {
  3735. bnx2x_q_fill_init_general_data(bp, cmd_params->q_obj,
  3736. &cmd_params->params.setup.gen_params,
  3737. &data->general,
  3738. &cmd_params->params.setup.flags);
  3739. bnx2x_q_fill_init_tx_data(cmd_params->q_obj,
  3740. &cmd_params->params.setup.txq_params,
  3741. &data->tx,
  3742. &cmd_params->params.setup.flags);
  3743. bnx2x_q_fill_init_rx_data(cmd_params->q_obj,
  3744. &cmd_params->params.setup.rxq_params,
  3745. &data->rx,
  3746. &cmd_params->params.setup.flags);
  3747. bnx2x_q_fill_init_pause_data(cmd_params->q_obj,
  3748. &cmd_params->params.setup.pause_params,
  3749. &data->rx);
  3750. }
  3751. /* initialize the general and tx parts of a tx-only queue object */
  3752. static void bnx2x_q_fill_setup_tx_only(struct bnx2x *bp,
  3753. struct bnx2x_queue_state_params *cmd_params,
  3754. struct tx_queue_init_ramrod_data *data)
  3755. {
  3756. bnx2x_q_fill_init_general_data(bp, cmd_params->q_obj,
  3757. &cmd_params->params.tx_only.gen_params,
  3758. &data->general,
  3759. &cmd_params->params.tx_only.flags);
  3760. bnx2x_q_fill_init_tx_data(cmd_params->q_obj,
  3761. &cmd_params->params.tx_only.txq_params,
  3762. &data->tx,
  3763. &cmd_params->params.tx_only.flags);
  3764. DP(BNX2X_MSG_SP, "cid %d, tx bd page lo %x hi %x",
  3765. cmd_params->q_obj->cids[0],
  3766. data->tx.tx_bd_page_base.lo,
  3767. data->tx.tx_bd_page_base.hi);
  3768. }
  3769. /**
  3770. * bnx2x_q_init - init HW/FW queue
  3771. *
  3772. * @bp: device handle
  3773. * @params:
  3774. *
  3775. * HW/FW initial Queue configuration:
  3776. * - HC: Rx and Tx
  3777. * - CDU context validation
  3778. *
  3779. */
  3780. static inline int bnx2x_q_init(struct bnx2x *bp,
  3781. struct bnx2x_queue_state_params *params)
  3782. {
  3783. struct bnx2x_queue_sp_obj *o = params->q_obj;
  3784. struct bnx2x_queue_init_params *init = &params->params.init;
  3785. u16 hc_usec;
  3786. u8 cos;
  3787. /* Tx HC configuration */
  3788. if (test_bit(BNX2X_Q_TYPE_HAS_TX, &o->type) &&
  3789. test_bit(BNX2X_Q_FLG_HC, &init->tx.flags)) {
  3790. hc_usec = init->tx.hc_rate ? 1000000 / init->tx.hc_rate : 0;
  3791. bnx2x_update_coalesce_sb_index(bp, init->tx.fw_sb_id,
  3792. init->tx.sb_cq_index,
  3793. !test_bit(BNX2X_Q_FLG_HC_EN, &init->tx.flags),
  3794. hc_usec);
  3795. }
  3796. /* Rx HC configuration */
  3797. if (test_bit(BNX2X_Q_TYPE_HAS_RX, &o->type) &&
  3798. test_bit(BNX2X_Q_FLG_HC, &init->rx.flags)) {
  3799. hc_usec = init->rx.hc_rate ? 1000000 / init->rx.hc_rate : 0;
  3800. bnx2x_update_coalesce_sb_index(bp, init->rx.fw_sb_id,
  3801. init->rx.sb_cq_index,
  3802. !test_bit(BNX2X_Q_FLG_HC_EN, &init->rx.flags),
  3803. hc_usec);
  3804. }
  3805. /* Set CDU context validation values */
  3806. for (cos = 0; cos < o->max_cos; cos++) {
  3807. DP(BNX2X_MSG_SP, "setting context validation. cid %d, cos %d\n",
  3808. o->cids[cos], cos);
  3809. DP(BNX2X_MSG_SP, "context pointer %p\n", init->cxts[cos]);
  3810. bnx2x_set_ctx_validation(bp, init->cxts[cos], o->cids[cos]);
  3811. }
  3812. /* As no ramrod is sent, complete the command immediately */
  3813. o->complete_cmd(bp, o, BNX2X_Q_CMD_INIT);
  3814. mmiowb();
  3815. smp_mb();
  3816. return 0;
  3817. }
  3818. static inline int bnx2x_q_send_setup_e1x(struct bnx2x *bp,
  3819. struct bnx2x_queue_state_params *params)
  3820. {
  3821. struct bnx2x_queue_sp_obj *o = params->q_obj;
  3822. struct client_init_ramrod_data *rdata =
  3823. (struct client_init_ramrod_data *)o->rdata;
  3824. dma_addr_t data_mapping = o->rdata_mapping;
  3825. int ramrod = RAMROD_CMD_ID_ETH_CLIENT_SETUP;
  3826. /* Clear the ramrod data */
  3827. memset(rdata, 0, sizeof(*rdata));
  3828. /* Fill the ramrod data */
  3829. bnx2x_q_fill_setup_data_cmn(bp, params, rdata);
  3830. /* No need for an explicit memory barrier here as long as we
  3831. * ensure the ordering of writing to the SPQ element
  3832. * and updating of the SPQ producer which involves a memory
  3833. * read. If the memory read is removed we will have to put a
  3834. * full memory barrier there (inside bnx2x_sp_post()).
  3835. */
  3836. return bnx2x_sp_post(bp, ramrod, o->cids[BNX2X_PRIMARY_CID_INDEX],
  3837. U64_HI(data_mapping),
  3838. U64_LO(data_mapping), ETH_CONNECTION_TYPE);
  3839. }
  3840. static inline int bnx2x_q_send_setup_e2(struct bnx2x *bp,
  3841. struct bnx2x_queue_state_params *params)
  3842. {
  3843. struct bnx2x_queue_sp_obj *o = params->q_obj;
  3844. struct client_init_ramrod_data *rdata =
  3845. (struct client_init_ramrod_data *)o->rdata;
  3846. dma_addr_t data_mapping = o->rdata_mapping;
  3847. int ramrod = RAMROD_CMD_ID_ETH_CLIENT_SETUP;
  3848. /* Clear the ramrod data */
  3849. memset(rdata, 0, sizeof(*rdata));
  3850. /* Fill the ramrod data */
  3851. bnx2x_q_fill_setup_data_cmn(bp, params, rdata);
  3852. bnx2x_q_fill_setup_data_e2(bp, params, rdata);
  3853. /* No need for an explicit memory barrier here as long as we
  3854. * ensure the ordering of writing to the SPQ element
  3855. * and updating of the SPQ producer which involves a memory
  3856. * read. If the memory read is removed we will have to put a
  3857. * full memory barrier there (inside bnx2x_sp_post()).
  3858. */
  3859. return bnx2x_sp_post(bp, ramrod, o->cids[BNX2X_PRIMARY_CID_INDEX],
  3860. U64_HI(data_mapping),
  3861. U64_LO(data_mapping), ETH_CONNECTION_TYPE);
  3862. }
  3863. static inline int bnx2x_q_send_setup_tx_only(struct bnx2x *bp,
  3864. struct bnx2x_queue_state_params *params)
  3865. {
  3866. struct bnx2x_queue_sp_obj *o = params->q_obj;
  3867. struct tx_queue_init_ramrod_data *rdata =
  3868. (struct tx_queue_init_ramrod_data *)o->rdata;
  3869. dma_addr_t data_mapping = o->rdata_mapping;
  3870. int ramrod = RAMROD_CMD_ID_ETH_TX_QUEUE_SETUP;
  3871. struct bnx2x_queue_setup_tx_only_params *tx_only_params =
  3872. &params->params.tx_only;
  3873. u8 cid_index = tx_only_params->cid_index;
  3874. if (cid_index >= o->max_cos) {
  3875. BNX2X_ERR("queue[%d]: cid_index (%d) is out of range\n",
  3876. o->cl_id, cid_index);
  3877. return -EINVAL;
  3878. }
  3879. DP(BNX2X_MSG_SP, "parameters received: cos: %d sp-id: %d\n",
  3880. tx_only_params->gen_params.cos,
  3881. tx_only_params->gen_params.spcl_id);
  3882. /* Clear the ramrod data */
  3883. memset(rdata, 0, sizeof(*rdata));
  3884. /* Fill the ramrod data */
  3885. bnx2x_q_fill_setup_tx_only(bp, params, rdata);
  3886. DP(BNX2X_MSG_SP, "sending tx-only ramrod: cid %d, client-id %d, sp-client id %d, cos %d\n",
  3887. o->cids[cid_index], rdata->general.client_id,
  3888. rdata->general.sp_client_id, rdata->general.cos);
  3889. /* No need for an explicit memory barrier here as long as we
  3890. * ensure the ordering of writing to the SPQ element
  3891. * and updating of the SPQ producer which involves a memory
  3892. * read. If the memory read is removed we will have to put a
  3893. * full memory barrier there (inside bnx2x_sp_post()).
  3894. */
  3895. return bnx2x_sp_post(bp, ramrod, o->cids[cid_index],
  3896. U64_HI(data_mapping),
  3897. U64_LO(data_mapping), ETH_CONNECTION_TYPE);
  3898. }
  3899. static void bnx2x_q_fill_update_data(struct bnx2x *bp,
  3900. struct bnx2x_queue_sp_obj *obj,
  3901. struct bnx2x_queue_update_params *params,
  3902. struct client_update_ramrod_data *data)
  3903. {
  3904. /* Client ID of the client to update */
  3905. data->client_id = obj->cl_id;
  3906. /* Function ID of the client to update */
  3907. data->func_id = obj->func_id;
  3908. /* Default VLAN value */
  3909. data->default_vlan = cpu_to_le16(params->def_vlan);
  3910. /* Inner VLAN stripping */
  3911. data->inner_vlan_removal_enable_flg =
  3912. test_bit(BNX2X_Q_UPDATE_IN_VLAN_REM, &params->update_flags);
  3913. data->inner_vlan_removal_change_flg =
  3914. test_bit(BNX2X_Q_UPDATE_IN_VLAN_REM_CHNG,
  3915. &params->update_flags);
  3916. /* Outer VLAN stripping */
  3917. data->outer_vlan_removal_enable_flg =
  3918. test_bit(BNX2X_Q_UPDATE_OUT_VLAN_REM, &params->update_flags);
  3919. data->outer_vlan_removal_change_flg =
  3920. test_bit(BNX2X_Q_UPDATE_OUT_VLAN_REM_CHNG,
  3921. &params->update_flags);
  3922. /* Drop packets that have source MAC that doesn't belong to this
  3923. * Queue.
  3924. */
  3925. data->anti_spoofing_enable_flg =
  3926. test_bit(BNX2X_Q_UPDATE_ANTI_SPOOF, &params->update_flags);
  3927. data->anti_spoofing_change_flg =
  3928. test_bit(BNX2X_Q_UPDATE_ANTI_SPOOF_CHNG, &params->update_flags);
  3929. /* Activate/Deactivate */
  3930. data->activate_flg =
  3931. test_bit(BNX2X_Q_UPDATE_ACTIVATE, &params->update_flags);
  3932. data->activate_change_flg =
  3933. test_bit(BNX2X_Q_UPDATE_ACTIVATE_CHNG, &params->update_flags);
  3934. /* Enable default VLAN */
  3935. data->default_vlan_enable_flg =
  3936. test_bit(BNX2X_Q_UPDATE_DEF_VLAN_EN, &params->update_flags);
  3937. data->default_vlan_change_flg =
  3938. test_bit(BNX2X_Q_UPDATE_DEF_VLAN_EN_CHNG,
  3939. &params->update_flags);
  3940. /* silent vlan removal */
  3941. data->silent_vlan_change_flg =
  3942. test_bit(BNX2X_Q_UPDATE_SILENT_VLAN_REM_CHNG,
  3943. &params->update_flags);
  3944. data->silent_vlan_removal_flg =
  3945. test_bit(BNX2X_Q_UPDATE_SILENT_VLAN_REM, &params->update_flags);
  3946. data->silent_vlan_value = cpu_to_le16(params->silent_removal_value);
  3947. data->silent_vlan_mask = cpu_to_le16(params->silent_removal_mask);
  3948. /* tx switching */
  3949. data->tx_switching_flg =
  3950. test_bit(BNX2X_Q_UPDATE_TX_SWITCHING, &params->update_flags);
  3951. data->tx_switching_change_flg =
  3952. test_bit(BNX2X_Q_UPDATE_TX_SWITCHING_CHNG,
  3953. &params->update_flags);
  3954. }
  3955. static inline int bnx2x_q_send_update(struct bnx2x *bp,
  3956. struct bnx2x_queue_state_params *params)
  3957. {
  3958. struct bnx2x_queue_sp_obj *o = params->q_obj;
  3959. struct client_update_ramrod_data *rdata =
  3960. (struct client_update_ramrod_data *)o->rdata;
  3961. dma_addr_t data_mapping = o->rdata_mapping;
  3962. struct bnx2x_queue_update_params *update_params =
  3963. &params->params.update;
  3964. u8 cid_index = update_params->cid_index;
  3965. if (cid_index >= o->max_cos) {
  3966. BNX2X_ERR("queue[%d]: cid_index (%d) is out of range\n",
  3967. o->cl_id, cid_index);
  3968. return -EINVAL;
  3969. }
  3970. /* Clear the ramrod data */
  3971. memset(rdata, 0, sizeof(*rdata));
  3972. /* Fill the ramrod data */
  3973. bnx2x_q_fill_update_data(bp, o, update_params, rdata);
  3974. /* No need for an explicit memory barrier here as long as we
  3975. * ensure the ordering of writing to the SPQ element
  3976. * and updating of the SPQ producer which involves a memory
  3977. * read. If the memory read is removed we will have to put a
  3978. * full memory barrier there (inside bnx2x_sp_post()).
  3979. */
  3980. return bnx2x_sp_post(bp, RAMROD_CMD_ID_ETH_CLIENT_UPDATE,
  3981. o->cids[cid_index], U64_HI(data_mapping),
  3982. U64_LO(data_mapping), ETH_CONNECTION_TYPE);
  3983. }
  3984. /**
  3985. * bnx2x_q_send_deactivate - send DEACTIVATE command
  3986. *
  3987. * @bp: device handle
  3988. * @params:
  3989. *
  3990. * implemented using the UPDATE command.
  3991. */
  3992. static inline int bnx2x_q_send_deactivate(struct bnx2x *bp,
  3993. struct bnx2x_queue_state_params *params)
  3994. {
  3995. struct bnx2x_queue_update_params *update = &params->params.update;
  3996. memset(update, 0, sizeof(*update));
  3997. __set_bit(BNX2X_Q_UPDATE_ACTIVATE_CHNG, &update->update_flags);
  3998. return bnx2x_q_send_update(bp, params);
  3999. }
  4000. /**
  4001. * bnx2x_q_send_activate - send ACTIVATE command
  4002. *
  4003. * @bp: device handle
  4004. * @params:
  4005. *
  4006. * implemented using the UPDATE command.
  4007. */
  4008. static inline int bnx2x_q_send_activate(struct bnx2x *bp,
  4009. struct bnx2x_queue_state_params *params)
  4010. {
  4011. struct bnx2x_queue_update_params *update = &params->params.update;
  4012. memset(update, 0, sizeof(*update));
  4013. __set_bit(BNX2X_Q_UPDATE_ACTIVATE, &update->update_flags);
  4014. __set_bit(BNX2X_Q_UPDATE_ACTIVATE_CHNG, &update->update_flags);
  4015. return bnx2x_q_send_update(bp, params);
  4016. }
  4017. static void bnx2x_q_fill_update_tpa_data(struct bnx2x *bp,
  4018. struct bnx2x_queue_sp_obj *obj,
  4019. struct bnx2x_queue_update_tpa_params *params,
  4020. struct tpa_update_ramrod_data *data)
  4021. {
  4022. data->client_id = obj->cl_id;
  4023. data->complete_on_both_clients = params->complete_on_both_clients;
  4024. data->dont_verify_rings_pause_thr_flg =
  4025. params->dont_verify_thr;
  4026. data->max_agg_size = cpu_to_le16(params->max_agg_sz);
  4027. data->max_sges_for_packet = params->max_sges_pkt;
  4028. data->max_tpa_queues = params->max_tpa_queues;
  4029. data->sge_buff_size = cpu_to_le16(params->sge_buff_sz);
  4030. data->sge_page_base_hi = cpu_to_le32(U64_HI(params->sge_map));
  4031. data->sge_page_base_lo = cpu_to_le32(U64_LO(params->sge_map));
  4032. data->sge_pause_thr_high = cpu_to_le16(params->sge_pause_thr_high);
  4033. data->sge_pause_thr_low = cpu_to_le16(params->sge_pause_thr_low);
  4034. data->tpa_mode = params->tpa_mode;
  4035. data->update_ipv4 = params->update_ipv4;
  4036. data->update_ipv6 = params->update_ipv6;
  4037. }
  4038. static inline int bnx2x_q_send_update_tpa(struct bnx2x *bp,
  4039. struct bnx2x_queue_state_params *params)
  4040. {
  4041. struct bnx2x_queue_sp_obj *o = params->q_obj;
  4042. struct tpa_update_ramrod_data *rdata =
  4043. (struct tpa_update_ramrod_data *)o->rdata;
  4044. dma_addr_t data_mapping = o->rdata_mapping;
  4045. struct bnx2x_queue_update_tpa_params *update_tpa_params =
  4046. &params->params.update_tpa;
  4047. u16 type;
  4048. /* Clear the ramrod data */
  4049. memset(rdata, 0, sizeof(*rdata));
  4050. /* Fill the ramrod data */
  4051. bnx2x_q_fill_update_tpa_data(bp, o, update_tpa_params, rdata);
  4052. /* Add the function id inside the type, so that sp post function
  4053. * doesn't automatically add the PF func-id, this is required
  4054. * for operations done by PFs on behalf of their VFs
  4055. */
  4056. type = ETH_CONNECTION_TYPE |
  4057. ((o->func_id) << SPE_HDR_FUNCTION_ID_SHIFT);
  4058. /* No need for an explicit memory barrier here as long as we
  4059. * ensure the ordering of writing to the SPQ element
  4060. * and updating of the SPQ producer which involves a memory
  4061. * read. If the memory read is removed we will have to put a
  4062. * full memory barrier there (inside bnx2x_sp_post()).
  4063. */
  4064. return bnx2x_sp_post(bp, RAMROD_CMD_ID_ETH_TPA_UPDATE,
  4065. o->cids[BNX2X_PRIMARY_CID_INDEX],
  4066. U64_HI(data_mapping),
  4067. U64_LO(data_mapping), type);
  4068. }
  4069. static inline int bnx2x_q_send_halt(struct bnx2x *bp,
  4070. struct bnx2x_queue_state_params *params)
  4071. {
  4072. struct bnx2x_queue_sp_obj *o = params->q_obj;
  4073. return bnx2x_sp_post(bp, RAMROD_CMD_ID_ETH_HALT,
  4074. o->cids[BNX2X_PRIMARY_CID_INDEX], 0, o->cl_id,
  4075. ETH_CONNECTION_TYPE);
  4076. }
  4077. static inline int bnx2x_q_send_cfc_del(struct bnx2x *bp,
  4078. struct bnx2x_queue_state_params *params)
  4079. {
  4080. struct bnx2x_queue_sp_obj *o = params->q_obj;
  4081. u8 cid_idx = params->params.cfc_del.cid_index;
  4082. if (cid_idx >= o->max_cos) {
  4083. BNX2X_ERR("queue[%d]: cid_index (%d) is out of range\n",
  4084. o->cl_id, cid_idx);
  4085. return -EINVAL;
  4086. }
  4087. return bnx2x_sp_post(bp, RAMROD_CMD_ID_COMMON_CFC_DEL,
  4088. o->cids[cid_idx], 0, 0, NONE_CONNECTION_TYPE);
  4089. }
  4090. static inline int bnx2x_q_send_terminate(struct bnx2x *bp,
  4091. struct bnx2x_queue_state_params *params)
  4092. {
  4093. struct bnx2x_queue_sp_obj *o = params->q_obj;
  4094. u8 cid_index = params->params.terminate.cid_index;
  4095. if (cid_index >= o->max_cos) {
  4096. BNX2X_ERR("queue[%d]: cid_index (%d) is out of range\n",
  4097. o->cl_id, cid_index);
  4098. return -EINVAL;
  4099. }
  4100. return bnx2x_sp_post(bp, RAMROD_CMD_ID_ETH_TERMINATE,
  4101. o->cids[cid_index], 0, 0, ETH_CONNECTION_TYPE);
  4102. }
  4103. static inline int bnx2x_q_send_empty(struct bnx2x *bp,
  4104. struct bnx2x_queue_state_params *params)
  4105. {
  4106. struct bnx2x_queue_sp_obj *o = params->q_obj;
  4107. return bnx2x_sp_post(bp, RAMROD_CMD_ID_ETH_EMPTY,
  4108. o->cids[BNX2X_PRIMARY_CID_INDEX], 0, 0,
  4109. ETH_CONNECTION_TYPE);
  4110. }
  4111. static inline int bnx2x_queue_send_cmd_cmn(struct bnx2x *bp,
  4112. struct bnx2x_queue_state_params *params)
  4113. {
  4114. switch (params->cmd) {
  4115. case BNX2X_Q_CMD_INIT:
  4116. return bnx2x_q_init(bp, params);
  4117. case BNX2X_Q_CMD_SETUP_TX_ONLY:
  4118. return bnx2x_q_send_setup_tx_only(bp, params);
  4119. case BNX2X_Q_CMD_DEACTIVATE:
  4120. return bnx2x_q_send_deactivate(bp, params);
  4121. case BNX2X_Q_CMD_ACTIVATE:
  4122. return bnx2x_q_send_activate(bp, params);
  4123. case BNX2X_Q_CMD_UPDATE:
  4124. return bnx2x_q_send_update(bp, params);
  4125. case BNX2X_Q_CMD_UPDATE_TPA:
  4126. return bnx2x_q_send_update_tpa(bp, params);
  4127. case BNX2X_Q_CMD_HALT:
  4128. return bnx2x_q_send_halt(bp, params);
  4129. case BNX2X_Q_CMD_CFC_DEL:
  4130. return bnx2x_q_send_cfc_del(bp, params);
  4131. case BNX2X_Q_CMD_TERMINATE:
  4132. return bnx2x_q_send_terminate(bp, params);
  4133. case BNX2X_Q_CMD_EMPTY:
  4134. return bnx2x_q_send_empty(bp, params);
  4135. default:
  4136. BNX2X_ERR("Unknown command: %d\n", params->cmd);
  4137. return -EINVAL;
  4138. }
  4139. }
  4140. static int bnx2x_queue_send_cmd_e1x(struct bnx2x *bp,
  4141. struct bnx2x_queue_state_params *params)
  4142. {
  4143. switch (params->cmd) {
  4144. case BNX2X_Q_CMD_SETUP:
  4145. return bnx2x_q_send_setup_e1x(bp, params);
  4146. case BNX2X_Q_CMD_INIT:
  4147. case BNX2X_Q_CMD_SETUP_TX_ONLY:
  4148. case BNX2X_Q_CMD_DEACTIVATE:
  4149. case BNX2X_Q_CMD_ACTIVATE:
  4150. case BNX2X_Q_CMD_UPDATE:
  4151. case BNX2X_Q_CMD_UPDATE_TPA:
  4152. case BNX2X_Q_CMD_HALT:
  4153. case BNX2X_Q_CMD_CFC_DEL:
  4154. case BNX2X_Q_CMD_TERMINATE:
  4155. case BNX2X_Q_CMD_EMPTY:
  4156. return bnx2x_queue_send_cmd_cmn(bp, params);
  4157. default:
  4158. BNX2X_ERR("Unknown command: %d\n", params->cmd);
  4159. return -EINVAL;
  4160. }
  4161. }
  4162. static int bnx2x_queue_send_cmd_e2(struct bnx2x *bp,
  4163. struct bnx2x_queue_state_params *params)
  4164. {
  4165. switch (params->cmd) {
  4166. case BNX2X_Q_CMD_SETUP:
  4167. return bnx2x_q_send_setup_e2(bp, params);
  4168. case BNX2X_Q_CMD_INIT:
  4169. case BNX2X_Q_CMD_SETUP_TX_ONLY:
  4170. case BNX2X_Q_CMD_DEACTIVATE:
  4171. case BNX2X_Q_CMD_ACTIVATE:
  4172. case BNX2X_Q_CMD_UPDATE:
  4173. case BNX2X_Q_CMD_UPDATE_TPA:
  4174. case BNX2X_Q_CMD_HALT:
  4175. case BNX2X_Q_CMD_CFC_DEL:
  4176. case BNX2X_Q_CMD_TERMINATE:
  4177. case BNX2X_Q_CMD_EMPTY:
  4178. return bnx2x_queue_send_cmd_cmn(bp, params);
  4179. default:
  4180. BNX2X_ERR("Unknown command: %d\n", params->cmd);
  4181. return -EINVAL;
  4182. }
  4183. }
  4184. /**
  4185. * bnx2x_queue_chk_transition - check state machine of a regular Queue
  4186. *
  4187. * @bp: device handle
  4188. * @o:
  4189. * @params:
  4190. *
  4191. * (not Forwarding)
  4192. * It both checks if the requested command is legal in a current
  4193. * state and, if it's legal, sets a `next_state' in the object
  4194. * that will be used in the completion flow to set the `state'
  4195. * of the object.
  4196. *
  4197. * returns 0 if a requested command is a legal transition,
  4198. * -EINVAL otherwise.
  4199. */
  4200. static int bnx2x_queue_chk_transition(struct bnx2x *bp,
  4201. struct bnx2x_queue_sp_obj *o,
  4202. struct bnx2x_queue_state_params *params)
  4203. {
  4204. enum bnx2x_q_state state = o->state, next_state = BNX2X_Q_STATE_MAX;
  4205. enum bnx2x_queue_cmd cmd = params->cmd;
  4206. struct bnx2x_queue_update_params *update_params =
  4207. &params->params.update;
  4208. u8 next_tx_only = o->num_tx_only;
  4209. /* Forget all pending for completion commands if a driver only state
  4210. * transition has been requested.
  4211. */
  4212. if (test_bit(RAMROD_DRV_CLR_ONLY, &params->ramrod_flags)) {
  4213. o->pending = 0;
  4214. o->next_state = BNX2X_Q_STATE_MAX;
  4215. }
  4216. /* Don't allow a next state transition if we are in the middle of
  4217. * the previous one.
  4218. */
  4219. if (o->pending) {
  4220. BNX2X_ERR("Blocking transition since pending was %lx\n",
  4221. o->pending);
  4222. return -EBUSY;
  4223. }
  4224. switch (state) {
  4225. case BNX2X_Q_STATE_RESET:
  4226. if (cmd == BNX2X_Q_CMD_INIT)
  4227. next_state = BNX2X_Q_STATE_INITIALIZED;
  4228. break;
  4229. case BNX2X_Q_STATE_INITIALIZED:
  4230. if (cmd == BNX2X_Q_CMD_SETUP) {
  4231. if (test_bit(BNX2X_Q_FLG_ACTIVE,
  4232. &params->params.setup.flags))
  4233. next_state = BNX2X_Q_STATE_ACTIVE;
  4234. else
  4235. next_state = BNX2X_Q_STATE_INACTIVE;
  4236. }
  4237. break;
  4238. case BNX2X_Q_STATE_ACTIVE:
  4239. if (cmd == BNX2X_Q_CMD_DEACTIVATE)
  4240. next_state = BNX2X_Q_STATE_INACTIVE;
  4241. else if ((cmd == BNX2X_Q_CMD_EMPTY) ||
  4242. (cmd == BNX2X_Q_CMD_UPDATE_TPA))
  4243. next_state = BNX2X_Q_STATE_ACTIVE;
  4244. else if (cmd == BNX2X_Q_CMD_SETUP_TX_ONLY) {
  4245. next_state = BNX2X_Q_STATE_MULTI_COS;
  4246. next_tx_only = 1;
  4247. }
  4248. else if (cmd == BNX2X_Q_CMD_HALT)
  4249. next_state = BNX2X_Q_STATE_STOPPED;
  4250. else if (cmd == BNX2X_Q_CMD_UPDATE) {
  4251. /* If "active" state change is requested, update the
  4252. * state accordingly.
  4253. */
  4254. if (test_bit(BNX2X_Q_UPDATE_ACTIVATE_CHNG,
  4255. &update_params->update_flags) &&
  4256. !test_bit(BNX2X_Q_UPDATE_ACTIVATE,
  4257. &update_params->update_flags))
  4258. next_state = BNX2X_Q_STATE_INACTIVE;
  4259. else
  4260. next_state = BNX2X_Q_STATE_ACTIVE;
  4261. }
  4262. break;
  4263. case BNX2X_Q_STATE_MULTI_COS:
  4264. if (cmd == BNX2X_Q_CMD_TERMINATE)
  4265. next_state = BNX2X_Q_STATE_MCOS_TERMINATED;
  4266. else if (cmd == BNX2X_Q_CMD_SETUP_TX_ONLY) {
  4267. next_state = BNX2X_Q_STATE_MULTI_COS;
  4268. next_tx_only = o->num_tx_only + 1;
  4269. }
  4270. else if ((cmd == BNX2X_Q_CMD_EMPTY) ||
  4271. (cmd == BNX2X_Q_CMD_UPDATE_TPA))
  4272. next_state = BNX2X_Q_STATE_MULTI_COS;
  4273. else if (cmd == BNX2X_Q_CMD_UPDATE) {
  4274. /* If "active" state change is requested, update the
  4275. * state accordingly.
  4276. */
  4277. if (test_bit(BNX2X_Q_UPDATE_ACTIVATE_CHNG,
  4278. &update_params->update_flags) &&
  4279. !test_bit(BNX2X_Q_UPDATE_ACTIVATE,
  4280. &update_params->update_flags))
  4281. next_state = BNX2X_Q_STATE_INACTIVE;
  4282. else
  4283. next_state = BNX2X_Q_STATE_MULTI_COS;
  4284. }
  4285. break;
  4286. case BNX2X_Q_STATE_MCOS_TERMINATED:
  4287. if (cmd == BNX2X_Q_CMD_CFC_DEL) {
  4288. next_tx_only = o->num_tx_only - 1;
  4289. if (next_tx_only == 0)
  4290. next_state = BNX2X_Q_STATE_ACTIVE;
  4291. else
  4292. next_state = BNX2X_Q_STATE_MULTI_COS;
  4293. }
  4294. break;
  4295. case BNX2X_Q_STATE_INACTIVE:
  4296. if (cmd == BNX2X_Q_CMD_ACTIVATE)
  4297. next_state = BNX2X_Q_STATE_ACTIVE;
  4298. else if ((cmd == BNX2X_Q_CMD_EMPTY) ||
  4299. (cmd == BNX2X_Q_CMD_UPDATE_TPA))
  4300. next_state = BNX2X_Q_STATE_INACTIVE;
  4301. else if (cmd == BNX2X_Q_CMD_HALT)
  4302. next_state = BNX2X_Q_STATE_STOPPED;
  4303. else if (cmd == BNX2X_Q_CMD_UPDATE) {
  4304. /* If "active" state change is requested, update the
  4305. * state accordingly.
  4306. */
  4307. if (test_bit(BNX2X_Q_UPDATE_ACTIVATE_CHNG,
  4308. &update_params->update_flags) &&
  4309. test_bit(BNX2X_Q_UPDATE_ACTIVATE,
  4310. &update_params->update_flags)){
  4311. if (o->num_tx_only == 0)
  4312. next_state = BNX2X_Q_STATE_ACTIVE;
  4313. else /* tx only queues exist for this queue */
  4314. next_state = BNX2X_Q_STATE_MULTI_COS;
  4315. } else
  4316. next_state = BNX2X_Q_STATE_INACTIVE;
  4317. }
  4318. break;
  4319. case BNX2X_Q_STATE_STOPPED:
  4320. if (cmd == BNX2X_Q_CMD_TERMINATE)
  4321. next_state = BNX2X_Q_STATE_TERMINATED;
  4322. break;
  4323. case BNX2X_Q_STATE_TERMINATED:
  4324. if (cmd == BNX2X_Q_CMD_CFC_DEL)
  4325. next_state = BNX2X_Q_STATE_RESET;
  4326. break;
  4327. default:
  4328. BNX2X_ERR("Illegal state: %d\n", state);
  4329. }
  4330. /* Transition is assured */
  4331. if (next_state != BNX2X_Q_STATE_MAX) {
  4332. DP(BNX2X_MSG_SP, "Good state transition: %d(%d)->%d\n",
  4333. state, cmd, next_state);
  4334. o->next_state = next_state;
  4335. o->next_tx_only = next_tx_only;
  4336. return 0;
  4337. }
  4338. DP(BNX2X_MSG_SP, "Bad state transition request: %d %d\n", state, cmd);
  4339. return -EINVAL;
  4340. }
  4341. void bnx2x_init_queue_obj(struct bnx2x *bp,
  4342. struct bnx2x_queue_sp_obj *obj,
  4343. u8 cl_id, u32 *cids, u8 cid_cnt, u8 func_id,
  4344. void *rdata,
  4345. dma_addr_t rdata_mapping, unsigned long type)
  4346. {
  4347. memset(obj, 0, sizeof(*obj));
  4348. /* We support only BNX2X_MULTI_TX_COS Tx CoS at the moment */
  4349. BUG_ON(BNX2X_MULTI_TX_COS < cid_cnt);
  4350. memcpy(obj->cids, cids, sizeof(obj->cids[0]) * cid_cnt);
  4351. obj->max_cos = cid_cnt;
  4352. obj->cl_id = cl_id;
  4353. obj->func_id = func_id;
  4354. obj->rdata = rdata;
  4355. obj->rdata_mapping = rdata_mapping;
  4356. obj->type = type;
  4357. obj->next_state = BNX2X_Q_STATE_MAX;
  4358. if (CHIP_IS_E1x(bp))
  4359. obj->send_cmd = bnx2x_queue_send_cmd_e1x;
  4360. else
  4361. obj->send_cmd = bnx2x_queue_send_cmd_e2;
  4362. obj->check_transition = bnx2x_queue_chk_transition;
  4363. obj->complete_cmd = bnx2x_queue_comp_cmd;
  4364. obj->wait_comp = bnx2x_queue_wait_comp;
  4365. obj->set_pending = bnx2x_queue_set_pending;
  4366. }
  4367. /* return a queue object's logical state*/
  4368. int bnx2x_get_q_logical_state(struct bnx2x *bp,
  4369. struct bnx2x_queue_sp_obj *obj)
  4370. {
  4371. switch (obj->state) {
  4372. case BNX2X_Q_STATE_ACTIVE:
  4373. case BNX2X_Q_STATE_MULTI_COS:
  4374. return BNX2X_Q_LOGICAL_STATE_ACTIVE;
  4375. case BNX2X_Q_STATE_RESET:
  4376. case BNX2X_Q_STATE_INITIALIZED:
  4377. case BNX2X_Q_STATE_MCOS_TERMINATED:
  4378. case BNX2X_Q_STATE_INACTIVE:
  4379. case BNX2X_Q_STATE_STOPPED:
  4380. case BNX2X_Q_STATE_TERMINATED:
  4381. case BNX2X_Q_STATE_FLRED:
  4382. return BNX2X_Q_LOGICAL_STATE_STOPPED;
  4383. default:
  4384. return -EINVAL;
  4385. }
  4386. }
  4387. /********************** Function state object *********************************/
  4388. enum bnx2x_func_state bnx2x_func_get_state(struct bnx2x *bp,
  4389. struct bnx2x_func_sp_obj *o)
  4390. {
  4391. /* in the middle of transaction - return INVALID state */
  4392. if (o->pending)
  4393. return BNX2X_F_STATE_MAX;
  4394. /* unsure the order of reading of o->pending and o->state
  4395. * o->pending should be read first
  4396. */
  4397. rmb();
  4398. return o->state;
  4399. }
  4400. static int bnx2x_func_wait_comp(struct bnx2x *bp,
  4401. struct bnx2x_func_sp_obj *o,
  4402. enum bnx2x_func_cmd cmd)
  4403. {
  4404. return bnx2x_state_wait(bp, cmd, &o->pending);
  4405. }
  4406. /**
  4407. * bnx2x_func_state_change_comp - complete the state machine transition
  4408. *
  4409. * @bp: device handle
  4410. * @o:
  4411. * @cmd:
  4412. *
  4413. * Called on state change transition. Completes the state
  4414. * machine transition only - no HW interaction.
  4415. */
  4416. static inline int bnx2x_func_state_change_comp(struct bnx2x *bp,
  4417. struct bnx2x_func_sp_obj *o,
  4418. enum bnx2x_func_cmd cmd)
  4419. {
  4420. unsigned long cur_pending = o->pending;
  4421. if (!test_and_clear_bit(cmd, &cur_pending)) {
  4422. BNX2X_ERR("Bad MC reply %d for func %d in state %d pending 0x%lx, next_state %d\n",
  4423. cmd, BP_FUNC(bp), o->state,
  4424. cur_pending, o->next_state);
  4425. return -EINVAL;
  4426. }
  4427. DP(BNX2X_MSG_SP,
  4428. "Completing command %d for func %d, setting state to %d\n",
  4429. cmd, BP_FUNC(bp), o->next_state);
  4430. o->state = o->next_state;
  4431. o->next_state = BNX2X_F_STATE_MAX;
  4432. /* It's important that o->state and o->next_state are
  4433. * updated before o->pending.
  4434. */
  4435. wmb();
  4436. clear_bit(cmd, &o->pending);
  4437. smp_mb__after_atomic();
  4438. return 0;
  4439. }
  4440. /**
  4441. * bnx2x_func_comp_cmd - complete the state change command
  4442. *
  4443. * @bp: device handle
  4444. * @o:
  4445. * @cmd:
  4446. *
  4447. * Checks that the arrived completion is expected.
  4448. */
  4449. static int bnx2x_func_comp_cmd(struct bnx2x *bp,
  4450. struct bnx2x_func_sp_obj *o,
  4451. enum bnx2x_func_cmd cmd)
  4452. {
  4453. /* Complete the state machine part first, check if it's a
  4454. * legal completion.
  4455. */
  4456. int rc = bnx2x_func_state_change_comp(bp, o, cmd);
  4457. return rc;
  4458. }
  4459. /**
  4460. * bnx2x_func_chk_transition - perform function state machine transition
  4461. *
  4462. * @bp: device handle
  4463. * @o:
  4464. * @params:
  4465. *
  4466. * It both checks if the requested command is legal in a current
  4467. * state and, if it's legal, sets a `next_state' in the object
  4468. * that will be used in the completion flow to set the `state'
  4469. * of the object.
  4470. *
  4471. * returns 0 if a requested command is a legal transition,
  4472. * -EINVAL otherwise.
  4473. */
  4474. static int bnx2x_func_chk_transition(struct bnx2x *bp,
  4475. struct bnx2x_func_sp_obj *o,
  4476. struct bnx2x_func_state_params *params)
  4477. {
  4478. enum bnx2x_func_state state = o->state, next_state = BNX2X_F_STATE_MAX;
  4479. enum bnx2x_func_cmd cmd = params->cmd;
  4480. /* Forget all pending for completion commands if a driver only state
  4481. * transition has been requested.
  4482. */
  4483. if (test_bit(RAMROD_DRV_CLR_ONLY, &params->ramrod_flags)) {
  4484. o->pending = 0;
  4485. o->next_state = BNX2X_F_STATE_MAX;
  4486. }
  4487. /* Don't allow a next state transition if we are in the middle of
  4488. * the previous one.
  4489. */
  4490. if (o->pending)
  4491. return -EBUSY;
  4492. switch (state) {
  4493. case BNX2X_F_STATE_RESET:
  4494. if (cmd == BNX2X_F_CMD_HW_INIT)
  4495. next_state = BNX2X_F_STATE_INITIALIZED;
  4496. break;
  4497. case BNX2X_F_STATE_INITIALIZED:
  4498. if (cmd == BNX2X_F_CMD_START)
  4499. next_state = BNX2X_F_STATE_STARTED;
  4500. else if (cmd == BNX2X_F_CMD_HW_RESET)
  4501. next_state = BNX2X_F_STATE_RESET;
  4502. break;
  4503. case BNX2X_F_STATE_STARTED:
  4504. if (cmd == BNX2X_F_CMD_STOP)
  4505. next_state = BNX2X_F_STATE_INITIALIZED;
  4506. /* afex ramrods can be sent only in started mode, and only
  4507. * if not pending for function_stop ramrod completion
  4508. * for these events - next state remained STARTED.
  4509. */
  4510. else if ((cmd == BNX2X_F_CMD_AFEX_UPDATE) &&
  4511. (!test_bit(BNX2X_F_CMD_STOP, &o->pending)))
  4512. next_state = BNX2X_F_STATE_STARTED;
  4513. else if ((cmd == BNX2X_F_CMD_AFEX_VIFLISTS) &&
  4514. (!test_bit(BNX2X_F_CMD_STOP, &o->pending)))
  4515. next_state = BNX2X_F_STATE_STARTED;
  4516. /* Switch_update ramrod can be sent in either started or
  4517. * tx_stopped state, and it doesn't change the state.
  4518. */
  4519. else if ((cmd == BNX2X_F_CMD_SWITCH_UPDATE) &&
  4520. (!test_bit(BNX2X_F_CMD_STOP, &o->pending)))
  4521. next_state = BNX2X_F_STATE_STARTED;
  4522. else if (cmd == BNX2X_F_CMD_TX_STOP)
  4523. next_state = BNX2X_F_STATE_TX_STOPPED;
  4524. break;
  4525. case BNX2X_F_STATE_TX_STOPPED:
  4526. if ((cmd == BNX2X_F_CMD_SWITCH_UPDATE) &&
  4527. (!test_bit(BNX2X_F_CMD_STOP, &o->pending)))
  4528. next_state = BNX2X_F_STATE_TX_STOPPED;
  4529. else if (cmd == BNX2X_F_CMD_TX_START)
  4530. next_state = BNX2X_F_STATE_STARTED;
  4531. break;
  4532. default:
  4533. BNX2X_ERR("Unknown state: %d\n", state);
  4534. }
  4535. /* Transition is assured */
  4536. if (next_state != BNX2X_F_STATE_MAX) {
  4537. DP(BNX2X_MSG_SP, "Good function state transition: %d(%d)->%d\n",
  4538. state, cmd, next_state);
  4539. o->next_state = next_state;
  4540. return 0;
  4541. }
  4542. DP(BNX2X_MSG_SP, "Bad function state transition request: %d %d\n",
  4543. state, cmd);
  4544. return -EINVAL;
  4545. }
  4546. /**
  4547. * bnx2x_func_init_func - performs HW init at function stage
  4548. *
  4549. * @bp: device handle
  4550. * @drv:
  4551. *
  4552. * Init HW when the current phase is
  4553. * FW_MSG_CODE_DRV_LOAD_FUNCTION: initialize only FUNCTION-only
  4554. * HW blocks.
  4555. */
  4556. static inline int bnx2x_func_init_func(struct bnx2x *bp,
  4557. const struct bnx2x_func_sp_drv_ops *drv)
  4558. {
  4559. return drv->init_hw_func(bp);
  4560. }
  4561. /**
  4562. * bnx2x_func_init_port - performs HW init at port stage
  4563. *
  4564. * @bp: device handle
  4565. * @drv:
  4566. *
  4567. * Init HW when the current phase is
  4568. * FW_MSG_CODE_DRV_LOAD_PORT: initialize PORT-only and
  4569. * FUNCTION-only HW blocks.
  4570. *
  4571. */
  4572. static inline int bnx2x_func_init_port(struct bnx2x *bp,
  4573. const struct bnx2x_func_sp_drv_ops *drv)
  4574. {
  4575. int rc = drv->init_hw_port(bp);
  4576. if (rc)
  4577. return rc;
  4578. return bnx2x_func_init_func(bp, drv);
  4579. }
  4580. /**
  4581. * bnx2x_func_init_cmn_chip - performs HW init at chip-common stage
  4582. *
  4583. * @bp: device handle
  4584. * @drv:
  4585. *
  4586. * Init HW when the current phase is
  4587. * FW_MSG_CODE_DRV_LOAD_COMMON_CHIP: initialize COMMON_CHIP,
  4588. * PORT-only and FUNCTION-only HW blocks.
  4589. */
  4590. static inline int bnx2x_func_init_cmn_chip(struct bnx2x *bp,
  4591. const struct bnx2x_func_sp_drv_ops *drv)
  4592. {
  4593. int rc = drv->init_hw_cmn_chip(bp);
  4594. if (rc)
  4595. return rc;
  4596. return bnx2x_func_init_port(bp, drv);
  4597. }
  4598. /**
  4599. * bnx2x_func_init_cmn - performs HW init at common stage
  4600. *
  4601. * @bp: device handle
  4602. * @drv:
  4603. *
  4604. * Init HW when the current phase is
  4605. * FW_MSG_CODE_DRV_LOAD_COMMON_CHIP: initialize COMMON,
  4606. * PORT-only and FUNCTION-only HW blocks.
  4607. */
  4608. static inline int bnx2x_func_init_cmn(struct bnx2x *bp,
  4609. const struct bnx2x_func_sp_drv_ops *drv)
  4610. {
  4611. int rc = drv->init_hw_cmn(bp);
  4612. if (rc)
  4613. return rc;
  4614. return bnx2x_func_init_port(bp, drv);
  4615. }
  4616. static int bnx2x_func_hw_init(struct bnx2x *bp,
  4617. struct bnx2x_func_state_params *params)
  4618. {
  4619. u32 load_code = params->params.hw_init.load_phase;
  4620. struct bnx2x_func_sp_obj *o = params->f_obj;
  4621. const struct bnx2x_func_sp_drv_ops *drv = o->drv;
  4622. int rc = 0;
  4623. DP(BNX2X_MSG_SP, "function %d load_code %x\n",
  4624. BP_ABS_FUNC(bp), load_code);
  4625. /* Prepare buffers for unzipping the FW */
  4626. rc = drv->gunzip_init(bp);
  4627. if (rc)
  4628. return rc;
  4629. /* Prepare FW */
  4630. rc = drv->init_fw(bp);
  4631. if (rc) {
  4632. BNX2X_ERR("Error loading firmware\n");
  4633. goto init_err;
  4634. }
  4635. /* Handle the beginning of COMMON_XXX pases separately... */
  4636. switch (load_code) {
  4637. case FW_MSG_CODE_DRV_LOAD_COMMON_CHIP:
  4638. rc = bnx2x_func_init_cmn_chip(bp, drv);
  4639. if (rc)
  4640. goto init_err;
  4641. break;
  4642. case FW_MSG_CODE_DRV_LOAD_COMMON:
  4643. rc = bnx2x_func_init_cmn(bp, drv);
  4644. if (rc)
  4645. goto init_err;
  4646. break;
  4647. case FW_MSG_CODE_DRV_LOAD_PORT:
  4648. rc = bnx2x_func_init_port(bp, drv);
  4649. if (rc)
  4650. goto init_err;
  4651. break;
  4652. case FW_MSG_CODE_DRV_LOAD_FUNCTION:
  4653. rc = bnx2x_func_init_func(bp, drv);
  4654. if (rc)
  4655. goto init_err;
  4656. break;
  4657. default:
  4658. BNX2X_ERR("Unknown load_code (0x%x) from MCP\n", load_code);
  4659. rc = -EINVAL;
  4660. }
  4661. init_err:
  4662. drv->gunzip_end(bp);
  4663. /* In case of success, complete the command immediately: no ramrods
  4664. * have been sent.
  4665. */
  4666. if (!rc)
  4667. o->complete_cmd(bp, o, BNX2X_F_CMD_HW_INIT);
  4668. return rc;
  4669. }
  4670. /**
  4671. * bnx2x_func_reset_func - reset HW at function stage
  4672. *
  4673. * @bp: device handle
  4674. * @drv:
  4675. *
  4676. * Reset HW at FW_MSG_CODE_DRV_UNLOAD_FUNCTION stage: reset only
  4677. * FUNCTION-only HW blocks.
  4678. */
  4679. static inline void bnx2x_func_reset_func(struct bnx2x *bp,
  4680. const struct bnx2x_func_sp_drv_ops *drv)
  4681. {
  4682. drv->reset_hw_func(bp);
  4683. }
  4684. /**
  4685. * bnx2x_func_reset_port - reset HW at port stage
  4686. *
  4687. * @bp: device handle
  4688. * @drv:
  4689. *
  4690. * Reset HW at FW_MSG_CODE_DRV_UNLOAD_PORT stage: reset
  4691. * FUNCTION-only and PORT-only HW blocks.
  4692. *
  4693. * !!!IMPORTANT!!!
  4694. *
  4695. * It's important to call reset_port before reset_func() as the last thing
  4696. * reset_func does is pf_disable() thus disabling PGLUE_B, which
  4697. * makes impossible any DMAE transactions.
  4698. */
  4699. static inline void bnx2x_func_reset_port(struct bnx2x *bp,
  4700. const struct bnx2x_func_sp_drv_ops *drv)
  4701. {
  4702. drv->reset_hw_port(bp);
  4703. bnx2x_func_reset_func(bp, drv);
  4704. }
  4705. /**
  4706. * bnx2x_func_reset_cmn - reset HW at common stage
  4707. *
  4708. * @bp: device handle
  4709. * @drv:
  4710. *
  4711. * Reset HW at FW_MSG_CODE_DRV_UNLOAD_COMMON and
  4712. * FW_MSG_CODE_DRV_UNLOAD_COMMON_CHIP stages: reset COMMON,
  4713. * COMMON_CHIP, FUNCTION-only and PORT-only HW blocks.
  4714. */
  4715. static inline void bnx2x_func_reset_cmn(struct bnx2x *bp,
  4716. const struct bnx2x_func_sp_drv_ops *drv)
  4717. {
  4718. bnx2x_func_reset_port(bp, drv);
  4719. drv->reset_hw_cmn(bp);
  4720. }
  4721. static inline int bnx2x_func_hw_reset(struct bnx2x *bp,
  4722. struct bnx2x_func_state_params *params)
  4723. {
  4724. u32 reset_phase = params->params.hw_reset.reset_phase;
  4725. struct bnx2x_func_sp_obj *o = params->f_obj;
  4726. const struct bnx2x_func_sp_drv_ops *drv = o->drv;
  4727. DP(BNX2X_MSG_SP, "function %d reset_phase %x\n", BP_ABS_FUNC(bp),
  4728. reset_phase);
  4729. switch (reset_phase) {
  4730. case FW_MSG_CODE_DRV_UNLOAD_COMMON:
  4731. bnx2x_func_reset_cmn(bp, drv);
  4732. break;
  4733. case FW_MSG_CODE_DRV_UNLOAD_PORT:
  4734. bnx2x_func_reset_port(bp, drv);
  4735. break;
  4736. case FW_MSG_CODE_DRV_UNLOAD_FUNCTION:
  4737. bnx2x_func_reset_func(bp, drv);
  4738. break;
  4739. default:
  4740. BNX2X_ERR("Unknown reset_phase (0x%x) from MCP\n",
  4741. reset_phase);
  4742. break;
  4743. }
  4744. /* Complete the command immediately: no ramrods have been sent. */
  4745. o->complete_cmd(bp, o, BNX2X_F_CMD_HW_RESET);
  4746. return 0;
  4747. }
  4748. static inline int bnx2x_func_send_start(struct bnx2x *bp,
  4749. struct bnx2x_func_state_params *params)
  4750. {
  4751. struct bnx2x_func_sp_obj *o = params->f_obj;
  4752. struct function_start_data *rdata =
  4753. (struct function_start_data *)o->rdata;
  4754. dma_addr_t data_mapping = o->rdata_mapping;
  4755. struct bnx2x_func_start_params *start_params = &params->params.start;
  4756. memset(rdata, 0, sizeof(*rdata));
  4757. /* Fill the ramrod data with provided parameters */
  4758. rdata->function_mode = (u8)start_params->mf_mode;
  4759. rdata->sd_vlan_tag = cpu_to_le16(start_params->sd_vlan_tag);
  4760. rdata->path_id = BP_PATH(bp);
  4761. rdata->network_cos_mode = start_params->network_cos_mode;
  4762. rdata->gre_tunnel_mode = start_params->gre_tunnel_mode;
  4763. rdata->gre_tunnel_rss = start_params->gre_tunnel_rss;
  4764. /* No need for an explicit memory barrier here as long we would
  4765. * need to ensure the ordering of writing to the SPQ element
  4766. * and updating of the SPQ producer which involves a memory
  4767. * read and we will have to put a full memory barrier there
  4768. * (inside bnx2x_sp_post()).
  4769. */
  4770. return bnx2x_sp_post(bp, RAMROD_CMD_ID_COMMON_FUNCTION_START, 0,
  4771. U64_HI(data_mapping),
  4772. U64_LO(data_mapping), NONE_CONNECTION_TYPE);
  4773. }
  4774. static inline int bnx2x_func_send_switch_update(struct bnx2x *bp,
  4775. struct bnx2x_func_state_params *params)
  4776. {
  4777. struct bnx2x_func_sp_obj *o = params->f_obj;
  4778. struct function_update_data *rdata =
  4779. (struct function_update_data *)o->rdata;
  4780. dma_addr_t data_mapping = o->rdata_mapping;
  4781. struct bnx2x_func_switch_update_params *switch_update_params =
  4782. &params->params.switch_update;
  4783. memset(rdata, 0, sizeof(*rdata));
  4784. /* Fill the ramrod data with provided parameters */
  4785. rdata->tx_switch_suspend_change_flg = 1;
  4786. rdata->tx_switch_suspend = switch_update_params->suspend;
  4787. rdata->echo = SWITCH_UPDATE;
  4788. /* No need for an explicit memory barrier here as long as we
  4789. * ensure the ordering of writing to the SPQ element
  4790. * and updating of the SPQ producer which involves a memory
  4791. * read. If the memory read is removed we will have to put a
  4792. * full memory barrier there (inside bnx2x_sp_post()).
  4793. */
  4794. return bnx2x_sp_post(bp, RAMROD_CMD_ID_COMMON_FUNCTION_UPDATE, 0,
  4795. U64_HI(data_mapping),
  4796. U64_LO(data_mapping), NONE_CONNECTION_TYPE);
  4797. }
  4798. static inline int bnx2x_func_send_afex_update(struct bnx2x *bp,
  4799. struct bnx2x_func_state_params *params)
  4800. {
  4801. struct bnx2x_func_sp_obj *o = params->f_obj;
  4802. struct function_update_data *rdata =
  4803. (struct function_update_data *)o->afex_rdata;
  4804. dma_addr_t data_mapping = o->afex_rdata_mapping;
  4805. struct bnx2x_func_afex_update_params *afex_update_params =
  4806. &params->params.afex_update;
  4807. memset(rdata, 0, sizeof(*rdata));
  4808. /* Fill the ramrod data with provided parameters */
  4809. rdata->vif_id_change_flg = 1;
  4810. rdata->vif_id = cpu_to_le16(afex_update_params->vif_id);
  4811. rdata->afex_default_vlan_change_flg = 1;
  4812. rdata->afex_default_vlan =
  4813. cpu_to_le16(afex_update_params->afex_default_vlan);
  4814. rdata->allowed_priorities_change_flg = 1;
  4815. rdata->allowed_priorities = afex_update_params->allowed_priorities;
  4816. rdata->echo = AFEX_UPDATE;
  4817. /* No need for an explicit memory barrier here as long as we
  4818. * ensure the ordering of writing to the SPQ element
  4819. * and updating of the SPQ producer which involves a memory
  4820. * read. If the memory read is removed we will have to put a
  4821. * full memory barrier there (inside bnx2x_sp_post()).
  4822. */
  4823. DP(BNX2X_MSG_SP,
  4824. "afex: sending func_update vif_id 0x%x dvlan 0x%x prio 0x%x\n",
  4825. rdata->vif_id,
  4826. rdata->afex_default_vlan, rdata->allowed_priorities);
  4827. return bnx2x_sp_post(bp, RAMROD_CMD_ID_COMMON_FUNCTION_UPDATE, 0,
  4828. U64_HI(data_mapping),
  4829. U64_LO(data_mapping), NONE_CONNECTION_TYPE);
  4830. }
  4831. static
  4832. inline int bnx2x_func_send_afex_viflists(struct bnx2x *bp,
  4833. struct bnx2x_func_state_params *params)
  4834. {
  4835. struct bnx2x_func_sp_obj *o = params->f_obj;
  4836. struct afex_vif_list_ramrod_data *rdata =
  4837. (struct afex_vif_list_ramrod_data *)o->afex_rdata;
  4838. struct bnx2x_func_afex_viflists_params *afex_vif_params =
  4839. &params->params.afex_viflists;
  4840. u64 *p_rdata = (u64 *)rdata;
  4841. memset(rdata, 0, sizeof(*rdata));
  4842. /* Fill the ramrod data with provided parameters */
  4843. rdata->vif_list_index = cpu_to_le16(afex_vif_params->vif_list_index);
  4844. rdata->func_bit_map = afex_vif_params->func_bit_map;
  4845. rdata->afex_vif_list_command = afex_vif_params->afex_vif_list_command;
  4846. rdata->func_to_clear = afex_vif_params->func_to_clear;
  4847. /* send in echo type of sub command */
  4848. rdata->echo = afex_vif_params->afex_vif_list_command;
  4849. /* No need for an explicit memory barrier here as long we would
  4850. * need to ensure the ordering of writing to the SPQ element
  4851. * and updating of the SPQ producer which involves a memory
  4852. * read and we will have to put a full memory barrier there
  4853. * (inside bnx2x_sp_post()).
  4854. */
  4855. DP(BNX2X_MSG_SP, "afex: ramrod lists, cmd 0x%x index 0x%x func_bit_map 0x%x func_to_clr 0x%x\n",
  4856. rdata->afex_vif_list_command, rdata->vif_list_index,
  4857. rdata->func_bit_map, rdata->func_to_clear);
  4858. /* this ramrod sends data directly and not through DMA mapping */
  4859. return bnx2x_sp_post(bp, RAMROD_CMD_ID_COMMON_AFEX_VIF_LISTS, 0,
  4860. U64_HI(*p_rdata), U64_LO(*p_rdata),
  4861. NONE_CONNECTION_TYPE);
  4862. }
  4863. static inline int bnx2x_func_send_stop(struct bnx2x *bp,
  4864. struct bnx2x_func_state_params *params)
  4865. {
  4866. return bnx2x_sp_post(bp, RAMROD_CMD_ID_COMMON_FUNCTION_STOP, 0, 0, 0,
  4867. NONE_CONNECTION_TYPE);
  4868. }
  4869. static inline int bnx2x_func_send_tx_stop(struct bnx2x *bp,
  4870. struct bnx2x_func_state_params *params)
  4871. {
  4872. return bnx2x_sp_post(bp, RAMROD_CMD_ID_COMMON_STOP_TRAFFIC, 0, 0, 0,
  4873. NONE_CONNECTION_TYPE);
  4874. }
  4875. static inline int bnx2x_func_send_tx_start(struct bnx2x *bp,
  4876. struct bnx2x_func_state_params *params)
  4877. {
  4878. struct bnx2x_func_sp_obj *o = params->f_obj;
  4879. struct flow_control_configuration *rdata =
  4880. (struct flow_control_configuration *)o->rdata;
  4881. dma_addr_t data_mapping = o->rdata_mapping;
  4882. struct bnx2x_func_tx_start_params *tx_start_params =
  4883. &params->params.tx_start;
  4884. int i;
  4885. memset(rdata, 0, sizeof(*rdata));
  4886. rdata->dcb_enabled = tx_start_params->dcb_enabled;
  4887. rdata->dcb_version = tx_start_params->dcb_version;
  4888. rdata->dont_add_pri_0_en = tx_start_params->dont_add_pri_0_en;
  4889. for (i = 0; i < ARRAY_SIZE(rdata->traffic_type_to_priority_cos); i++)
  4890. rdata->traffic_type_to_priority_cos[i] =
  4891. tx_start_params->traffic_type_to_priority_cos[i];
  4892. /* No need for an explicit memory barrier here as long as we
  4893. * ensure the ordering of writing to the SPQ element
  4894. * and updating of the SPQ producer which involves a memory
  4895. * read. If the memory read is removed we will have to put a
  4896. * full memory barrier there (inside bnx2x_sp_post()).
  4897. */
  4898. return bnx2x_sp_post(bp, RAMROD_CMD_ID_COMMON_START_TRAFFIC, 0,
  4899. U64_HI(data_mapping),
  4900. U64_LO(data_mapping), NONE_CONNECTION_TYPE);
  4901. }
  4902. static int bnx2x_func_send_cmd(struct bnx2x *bp,
  4903. struct bnx2x_func_state_params *params)
  4904. {
  4905. switch (params->cmd) {
  4906. case BNX2X_F_CMD_HW_INIT:
  4907. return bnx2x_func_hw_init(bp, params);
  4908. case BNX2X_F_CMD_START:
  4909. return bnx2x_func_send_start(bp, params);
  4910. case BNX2X_F_CMD_STOP:
  4911. return bnx2x_func_send_stop(bp, params);
  4912. case BNX2X_F_CMD_HW_RESET:
  4913. return bnx2x_func_hw_reset(bp, params);
  4914. case BNX2X_F_CMD_AFEX_UPDATE:
  4915. return bnx2x_func_send_afex_update(bp, params);
  4916. case BNX2X_F_CMD_AFEX_VIFLISTS:
  4917. return bnx2x_func_send_afex_viflists(bp, params);
  4918. case BNX2X_F_CMD_TX_STOP:
  4919. return bnx2x_func_send_tx_stop(bp, params);
  4920. case BNX2X_F_CMD_TX_START:
  4921. return bnx2x_func_send_tx_start(bp, params);
  4922. case BNX2X_F_CMD_SWITCH_UPDATE:
  4923. return bnx2x_func_send_switch_update(bp, params);
  4924. default:
  4925. BNX2X_ERR("Unknown command: %d\n", params->cmd);
  4926. return -EINVAL;
  4927. }
  4928. }
  4929. void bnx2x_init_func_obj(struct bnx2x *bp,
  4930. struct bnx2x_func_sp_obj *obj,
  4931. void *rdata, dma_addr_t rdata_mapping,
  4932. void *afex_rdata, dma_addr_t afex_rdata_mapping,
  4933. struct bnx2x_func_sp_drv_ops *drv_iface)
  4934. {
  4935. memset(obj, 0, sizeof(*obj));
  4936. mutex_init(&obj->one_pending_mutex);
  4937. obj->rdata = rdata;
  4938. obj->rdata_mapping = rdata_mapping;
  4939. obj->afex_rdata = afex_rdata;
  4940. obj->afex_rdata_mapping = afex_rdata_mapping;
  4941. obj->send_cmd = bnx2x_func_send_cmd;
  4942. obj->check_transition = bnx2x_func_chk_transition;
  4943. obj->complete_cmd = bnx2x_func_comp_cmd;
  4944. obj->wait_comp = bnx2x_func_wait_comp;
  4945. obj->drv = drv_iface;
  4946. }
  4947. /**
  4948. * bnx2x_func_state_change - perform Function state change transition
  4949. *
  4950. * @bp: device handle
  4951. * @params: parameters to perform the transaction
  4952. *
  4953. * returns 0 in case of successfully completed transition,
  4954. * negative error code in case of failure, positive
  4955. * (EBUSY) value if there is a completion to that is
  4956. * still pending (possible only if RAMROD_COMP_WAIT is
  4957. * not set in params->ramrod_flags for asynchronous
  4958. * commands).
  4959. */
  4960. int bnx2x_func_state_change(struct bnx2x *bp,
  4961. struct bnx2x_func_state_params *params)
  4962. {
  4963. struct bnx2x_func_sp_obj *o = params->f_obj;
  4964. int rc, cnt = 300;
  4965. enum bnx2x_func_cmd cmd = params->cmd;
  4966. unsigned long *pending = &o->pending;
  4967. mutex_lock(&o->one_pending_mutex);
  4968. /* Check that the requested transition is legal */
  4969. rc = o->check_transition(bp, o, params);
  4970. if ((rc == -EBUSY) &&
  4971. (test_bit(RAMROD_RETRY, &params->ramrod_flags))) {
  4972. while ((rc == -EBUSY) && (--cnt > 0)) {
  4973. mutex_unlock(&o->one_pending_mutex);
  4974. msleep(10);
  4975. mutex_lock(&o->one_pending_mutex);
  4976. rc = o->check_transition(bp, o, params);
  4977. }
  4978. if (rc == -EBUSY) {
  4979. mutex_unlock(&o->one_pending_mutex);
  4980. BNX2X_ERR("timeout waiting for previous ramrod completion\n");
  4981. return rc;
  4982. }
  4983. } else if (rc) {
  4984. mutex_unlock(&o->one_pending_mutex);
  4985. return rc;
  4986. }
  4987. /* Set "pending" bit */
  4988. set_bit(cmd, pending);
  4989. /* Don't send a command if only driver cleanup was requested */
  4990. if (test_bit(RAMROD_DRV_CLR_ONLY, &params->ramrod_flags)) {
  4991. bnx2x_func_state_change_comp(bp, o, cmd);
  4992. mutex_unlock(&o->one_pending_mutex);
  4993. } else {
  4994. /* Send a ramrod */
  4995. rc = o->send_cmd(bp, params);
  4996. mutex_unlock(&o->one_pending_mutex);
  4997. if (rc) {
  4998. o->next_state = BNX2X_F_STATE_MAX;
  4999. clear_bit(cmd, pending);
  5000. smp_mb__after_atomic();
  5001. return rc;
  5002. }
  5003. if (test_bit(RAMROD_COMP_WAIT, &params->ramrod_flags)) {
  5004. rc = o->wait_comp(bp, o, cmd);
  5005. if (rc)
  5006. return rc;
  5007. return 0;
  5008. }
  5009. }
  5010. return !!test_bit(cmd, pending);
  5011. }