bnxt.c 153 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156
  1. /* Broadcom NetXtreme-C/E network driver.
  2. *
  3. * Copyright (c) 2014-2015 Broadcom Corporation
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation.
  8. */
  9. #include <linux/module.h>
  10. #include <linux/stringify.h>
  11. #include <linux/kernel.h>
  12. #include <linux/timer.h>
  13. #include <linux/errno.h>
  14. #include <linux/ioport.h>
  15. #include <linux/slab.h>
  16. #include <linux/vmalloc.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/pci.h>
  19. #include <linux/netdevice.h>
  20. #include <linux/etherdevice.h>
  21. #include <linux/skbuff.h>
  22. #include <linux/dma-mapping.h>
  23. #include <linux/bitops.h>
  24. #include <linux/io.h>
  25. #include <linux/irq.h>
  26. #include <linux/delay.h>
  27. #include <asm/byteorder.h>
  28. #include <asm/page.h>
  29. #include <linux/time.h>
  30. #include <linux/mii.h>
  31. #include <linux/if.h>
  32. #include <linux/if_vlan.h>
  33. #include <net/ip.h>
  34. #include <net/tcp.h>
  35. #include <net/udp.h>
  36. #include <net/checksum.h>
  37. #include <net/ip6_checksum.h>
  38. #if defined(CONFIG_VXLAN) || defined(CONFIG_VXLAN_MODULE)
  39. #include <net/vxlan.h>
  40. #endif
  41. #ifdef CONFIG_NET_RX_BUSY_POLL
  42. #include <net/busy_poll.h>
  43. #endif
  44. #include <linux/workqueue.h>
  45. #include <linux/prefetch.h>
  46. #include <linux/cache.h>
  47. #include <linux/log2.h>
  48. #include <linux/aer.h>
  49. #include <linux/bitmap.h>
  50. #include <linux/cpu_rmap.h>
  51. #include "bnxt_hsi.h"
  52. #include "bnxt.h"
  53. #include "bnxt_sriov.h"
  54. #include "bnxt_ethtool.h"
  55. #define BNXT_TX_TIMEOUT (5 * HZ)
  56. static const char version[] =
  57. "Broadcom NetXtreme-C/E driver " DRV_MODULE_NAME " v" DRV_MODULE_VERSION "\n";
  58. MODULE_LICENSE("GPL");
  59. MODULE_DESCRIPTION("Broadcom BCM573xx network driver");
  60. MODULE_VERSION(DRV_MODULE_VERSION);
  61. #define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN)
  62. #define BNXT_RX_DMA_OFFSET NET_SKB_PAD
  63. #define BNXT_RX_COPY_THRESH 256
  64. #define BNXT_TX_PUSH_THRESH 164
  65. enum board_idx {
  66. BCM57301,
  67. BCM57302,
  68. BCM57304,
  69. BCM57402,
  70. BCM57404,
  71. BCM57406,
  72. BCM57304_VF,
  73. BCM57404_VF,
  74. };
  75. /* indexed by enum above */
  76. static const struct {
  77. char *name;
  78. } board_info[] = {
  79. { "Broadcom BCM57301 NetXtreme-C Single-port 10Gb Ethernet" },
  80. { "Broadcom BCM57302 NetXtreme-C Dual-port 10Gb/25Gb Ethernet" },
  81. { "Broadcom BCM57304 NetXtreme-C Dual-port 10Gb/25Gb/40Gb/50Gb Ethernet" },
  82. { "Broadcom BCM57402 NetXtreme-E Dual-port 10Gb Ethernet" },
  83. { "Broadcom BCM57404 NetXtreme-E Dual-port 10Gb/25Gb Ethernet" },
  84. { "Broadcom BCM57406 NetXtreme-E Dual-port 10GBase-T Ethernet" },
  85. { "Broadcom BCM57304 NetXtreme-C Ethernet Virtual Function" },
  86. { "Broadcom BCM57404 NetXtreme-E Ethernet Virtual Function" },
  87. };
  88. static const struct pci_device_id bnxt_pci_tbl[] = {
  89. { PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
  90. { PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
  91. { PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
  92. { PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
  93. { PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
  94. { PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
  95. #ifdef CONFIG_BNXT_SRIOV
  96. { PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = BCM57304_VF },
  97. { PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = BCM57404_VF },
  98. #endif
  99. { 0 }
  100. };
  101. MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
  102. static const u16 bnxt_vf_req_snif[] = {
  103. HWRM_FUNC_CFG,
  104. HWRM_PORT_PHY_QCFG,
  105. HWRM_CFA_L2_FILTER_ALLOC,
  106. };
  107. static bool bnxt_vf_pciid(enum board_idx idx)
  108. {
  109. return (idx == BCM57304_VF || idx == BCM57404_VF);
  110. }
  111. #define DB_CP_REARM_FLAGS (DB_KEY_CP | DB_IDX_VALID)
  112. #define DB_CP_FLAGS (DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
  113. #define DB_CP_IRQ_DIS_FLAGS (DB_KEY_CP | DB_IRQ_DIS)
  114. #define BNXT_CP_DB_REARM(db, raw_cons) \
  115. writel(DB_CP_REARM_FLAGS | RING_CMP(raw_cons), db)
  116. #define BNXT_CP_DB(db, raw_cons) \
  117. writel(DB_CP_FLAGS | RING_CMP(raw_cons), db)
  118. #define BNXT_CP_DB_IRQ_DIS(db) \
  119. writel(DB_CP_IRQ_DIS_FLAGS, db)
  120. static inline u32 bnxt_tx_avail(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
  121. {
  122. /* Tell compiler to fetch tx indices from memory. */
  123. barrier();
  124. return bp->tx_ring_size -
  125. ((txr->tx_prod - txr->tx_cons) & bp->tx_ring_mask);
  126. }
  127. static const u16 bnxt_lhint_arr[] = {
  128. TX_BD_FLAGS_LHINT_512_AND_SMALLER,
  129. TX_BD_FLAGS_LHINT_512_TO_1023,
  130. TX_BD_FLAGS_LHINT_1024_TO_2047,
  131. TX_BD_FLAGS_LHINT_1024_TO_2047,
  132. TX_BD_FLAGS_LHINT_2048_AND_LARGER,
  133. TX_BD_FLAGS_LHINT_2048_AND_LARGER,
  134. TX_BD_FLAGS_LHINT_2048_AND_LARGER,
  135. TX_BD_FLAGS_LHINT_2048_AND_LARGER,
  136. TX_BD_FLAGS_LHINT_2048_AND_LARGER,
  137. TX_BD_FLAGS_LHINT_2048_AND_LARGER,
  138. TX_BD_FLAGS_LHINT_2048_AND_LARGER,
  139. TX_BD_FLAGS_LHINT_2048_AND_LARGER,
  140. TX_BD_FLAGS_LHINT_2048_AND_LARGER,
  141. TX_BD_FLAGS_LHINT_2048_AND_LARGER,
  142. TX_BD_FLAGS_LHINT_2048_AND_LARGER,
  143. TX_BD_FLAGS_LHINT_2048_AND_LARGER,
  144. TX_BD_FLAGS_LHINT_2048_AND_LARGER,
  145. TX_BD_FLAGS_LHINT_2048_AND_LARGER,
  146. TX_BD_FLAGS_LHINT_2048_AND_LARGER,
  147. };
  148. static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
  149. {
  150. struct bnxt *bp = netdev_priv(dev);
  151. struct tx_bd *txbd;
  152. struct tx_bd_ext *txbd1;
  153. struct netdev_queue *txq;
  154. int i;
  155. dma_addr_t mapping;
  156. unsigned int length, pad = 0;
  157. u32 len, free_size, vlan_tag_flags, cfa_action, flags;
  158. u16 prod, last_frag;
  159. struct pci_dev *pdev = bp->pdev;
  160. struct bnxt_tx_ring_info *txr;
  161. struct bnxt_sw_tx_bd *tx_buf;
  162. i = skb_get_queue_mapping(skb);
  163. if (unlikely(i >= bp->tx_nr_rings)) {
  164. dev_kfree_skb_any(skb);
  165. return NETDEV_TX_OK;
  166. }
  167. txr = &bp->tx_ring[i];
  168. txq = netdev_get_tx_queue(dev, i);
  169. prod = txr->tx_prod;
  170. free_size = bnxt_tx_avail(bp, txr);
  171. if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
  172. netif_tx_stop_queue(txq);
  173. return NETDEV_TX_BUSY;
  174. }
  175. length = skb->len;
  176. len = skb_headlen(skb);
  177. last_frag = skb_shinfo(skb)->nr_frags;
  178. txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
  179. txbd->tx_bd_opaque = prod;
  180. tx_buf = &txr->tx_buf_ring[prod];
  181. tx_buf->skb = skb;
  182. tx_buf->nr_frags = last_frag;
  183. vlan_tag_flags = 0;
  184. cfa_action = 0;
  185. if (skb_vlan_tag_present(skb)) {
  186. vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
  187. skb_vlan_tag_get(skb);
  188. /* Currently supports 8021Q, 8021AD vlan offloads
  189. * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
  190. */
  191. if (skb->vlan_proto == htons(ETH_P_8021Q))
  192. vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
  193. }
  194. if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh) {
  195. struct tx_push_buffer *tx_push_buf = txr->tx_push;
  196. struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
  197. struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
  198. void *pdata = tx_push_buf->data;
  199. u64 *end;
  200. int j, push_len;
  201. /* Set COAL_NOW to be ready quickly for the next push */
  202. tx_push->tx_bd_len_flags_type =
  203. cpu_to_le32((length << TX_BD_LEN_SHIFT) |
  204. TX_BD_TYPE_LONG_TX_BD |
  205. TX_BD_FLAGS_LHINT_512_AND_SMALLER |
  206. TX_BD_FLAGS_COAL_NOW |
  207. TX_BD_FLAGS_PACKET_END |
  208. (2 << TX_BD_FLAGS_BD_CNT_SHIFT));
  209. if (skb->ip_summed == CHECKSUM_PARTIAL)
  210. tx_push1->tx_bd_hsize_lflags =
  211. cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
  212. else
  213. tx_push1->tx_bd_hsize_lflags = 0;
  214. tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
  215. tx_push1->tx_bd_cfa_action = cpu_to_le32(cfa_action);
  216. end = pdata + length;
  217. end = PTR_ALIGN(end, 8) - 1;
  218. *end = 0;
  219. skb_copy_from_linear_data(skb, pdata, len);
  220. pdata += len;
  221. for (j = 0; j < last_frag; j++) {
  222. skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
  223. void *fptr;
  224. fptr = skb_frag_address_safe(frag);
  225. if (!fptr)
  226. goto normal_tx;
  227. memcpy(pdata, fptr, skb_frag_size(frag));
  228. pdata += skb_frag_size(frag);
  229. }
  230. txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
  231. txbd->tx_bd_haddr = txr->data_mapping;
  232. prod = NEXT_TX(prod);
  233. txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
  234. memcpy(txbd, tx_push1, sizeof(*txbd));
  235. prod = NEXT_TX(prod);
  236. tx_push->doorbell =
  237. cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH | prod);
  238. txr->tx_prod = prod;
  239. netdev_tx_sent_queue(txq, skb->len);
  240. push_len = (length + sizeof(*tx_push) + 7) / 8;
  241. if (push_len > 16) {
  242. __iowrite64_copy(txr->tx_doorbell, tx_push_buf, 16);
  243. __iowrite64_copy(txr->tx_doorbell + 4, tx_push_buf + 1,
  244. push_len - 16);
  245. } else {
  246. __iowrite64_copy(txr->tx_doorbell, tx_push_buf,
  247. push_len);
  248. }
  249. tx_buf->is_push = 1;
  250. goto tx_done;
  251. }
  252. normal_tx:
  253. if (length < BNXT_MIN_PKT_SIZE) {
  254. pad = BNXT_MIN_PKT_SIZE - length;
  255. if (skb_pad(skb, pad)) {
  256. /* SKB already freed. */
  257. tx_buf->skb = NULL;
  258. return NETDEV_TX_OK;
  259. }
  260. length = BNXT_MIN_PKT_SIZE;
  261. }
  262. mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
  263. if (unlikely(dma_mapping_error(&pdev->dev, mapping))) {
  264. dev_kfree_skb_any(skb);
  265. tx_buf->skb = NULL;
  266. return NETDEV_TX_OK;
  267. }
  268. dma_unmap_addr_set(tx_buf, mapping, mapping);
  269. flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
  270. ((last_frag + 2) << TX_BD_FLAGS_BD_CNT_SHIFT);
  271. txbd->tx_bd_haddr = cpu_to_le64(mapping);
  272. prod = NEXT_TX(prod);
  273. txbd1 = (struct tx_bd_ext *)
  274. &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
  275. txbd1->tx_bd_hsize_lflags = 0;
  276. if (skb_is_gso(skb)) {
  277. u32 hdr_len;
  278. if (skb->encapsulation)
  279. hdr_len = skb_inner_network_offset(skb) +
  280. skb_inner_network_header_len(skb) +
  281. inner_tcp_hdrlen(skb);
  282. else
  283. hdr_len = skb_transport_offset(skb) +
  284. tcp_hdrlen(skb);
  285. txbd1->tx_bd_hsize_lflags = cpu_to_le32(TX_BD_FLAGS_LSO |
  286. TX_BD_FLAGS_T_IPID |
  287. (hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
  288. length = skb_shinfo(skb)->gso_size;
  289. txbd1->tx_bd_mss = cpu_to_le32(length);
  290. length += hdr_len;
  291. } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
  292. txbd1->tx_bd_hsize_lflags =
  293. cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
  294. txbd1->tx_bd_mss = 0;
  295. }
  296. length >>= 9;
  297. flags |= bnxt_lhint_arr[length];
  298. txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
  299. txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
  300. txbd1->tx_bd_cfa_action = cpu_to_le32(cfa_action);
  301. for (i = 0; i < last_frag; i++) {
  302. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  303. prod = NEXT_TX(prod);
  304. txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
  305. len = skb_frag_size(frag);
  306. mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
  307. DMA_TO_DEVICE);
  308. if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
  309. goto tx_dma_error;
  310. tx_buf = &txr->tx_buf_ring[prod];
  311. dma_unmap_addr_set(tx_buf, mapping, mapping);
  312. txbd->tx_bd_haddr = cpu_to_le64(mapping);
  313. flags = len << TX_BD_LEN_SHIFT;
  314. txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
  315. }
  316. flags &= ~TX_BD_LEN;
  317. txbd->tx_bd_len_flags_type =
  318. cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
  319. TX_BD_FLAGS_PACKET_END);
  320. netdev_tx_sent_queue(txq, skb->len);
  321. /* Sync BD data before updating doorbell */
  322. wmb();
  323. prod = NEXT_TX(prod);
  324. txr->tx_prod = prod;
  325. writel(DB_KEY_TX | prod, txr->tx_doorbell);
  326. writel(DB_KEY_TX | prod, txr->tx_doorbell);
  327. tx_done:
  328. mmiowb();
  329. if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
  330. netif_tx_stop_queue(txq);
  331. /* netif_tx_stop_queue() must be done before checking
  332. * tx index in bnxt_tx_avail() below, because in
  333. * bnxt_tx_int(), we update tx index before checking for
  334. * netif_tx_queue_stopped().
  335. */
  336. smp_mb();
  337. if (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)
  338. netif_tx_wake_queue(txq);
  339. }
  340. return NETDEV_TX_OK;
  341. tx_dma_error:
  342. last_frag = i;
  343. /* start back at beginning and unmap skb */
  344. prod = txr->tx_prod;
  345. tx_buf = &txr->tx_buf_ring[prod];
  346. tx_buf->skb = NULL;
  347. dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
  348. skb_headlen(skb), PCI_DMA_TODEVICE);
  349. prod = NEXT_TX(prod);
  350. /* unmap remaining mapped pages */
  351. for (i = 0; i < last_frag; i++) {
  352. prod = NEXT_TX(prod);
  353. tx_buf = &txr->tx_buf_ring[prod];
  354. dma_unmap_page(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
  355. skb_frag_size(&skb_shinfo(skb)->frags[i]),
  356. PCI_DMA_TODEVICE);
  357. }
  358. dev_kfree_skb_any(skb);
  359. return NETDEV_TX_OK;
  360. }
  361. static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int nr_pkts)
  362. {
  363. struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
  364. int index = txr - &bp->tx_ring[0];
  365. struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, index);
  366. u16 cons = txr->tx_cons;
  367. struct pci_dev *pdev = bp->pdev;
  368. int i;
  369. unsigned int tx_bytes = 0;
  370. for (i = 0; i < nr_pkts; i++) {
  371. struct bnxt_sw_tx_bd *tx_buf;
  372. struct sk_buff *skb;
  373. int j, last;
  374. tx_buf = &txr->tx_buf_ring[cons];
  375. cons = NEXT_TX(cons);
  376. skb = tx_buf->skb;
  377. tx_buf->skb = NULL;
  378. if (tx_buf->is_push) {
  379. tx_buf->is_push = 0;
  380. goto next_tx_int;
  381. }
  382. dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
  383. skb_headlen(skb), PCI_DMA_TODEVICE);
  384. last = tx_buf->nr_frags;
  385. for (j = 0; j < last; j++) {
  386. cons = NEXT_TX(cons);
  387. tx_buf = &txr->tx_buf_ring[cons];
  388. dma_unmap_page(
  389. &pdev->dev,
  390. dma_unmap_addr(tx_buf, mapping),
  391. skb_frag_size(&skb_shinfo(skb)->frags[j]),
  392. PCI_DMA_TODEVICE);
  393. }
  394. next_tx_int:
  395. cons = NEXT_TX(cons);
  396. tx_bytes += skb->len;
  397. dev_kfree_skb_any(skb);
  398. }
  399. netdev_tx_completed_queue(txq, nr_pkts, tx_bytes);
  400. txr->tx_cons = cons;
  401. /* Need to make the tx_cons update visible to bnxt_start_xmit()
  402. * before checking for netif_tx_queue_stopped(). Without the
  403. * memory barrier, there is a small possibility that bnxt_start_xmit()
  404. * will miss it and cause the queue to be stopped forever.
  405. */
  406. smp_mb();
  407. if (unlikely(netif_tx_queue_stopped(txq)) &&
  408. (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)) {
  409. __netif_tx_lock(txq, smp_processor_id());
  410. if (netif_tx_queue_stopped(txq) &&
  411. bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh &&
  412. txr->dev_state != BNXT_DEV_STATE_CLOSING)
  413. netif_tx_wake_queue(txq);
  414. __netif_tx_unlock(txq);
  415. }
  416. }
  417. static inline u8 *__bnxt_alloc_rx_data(struct bnxt *bp, dma_addr_t *mapping,
  418. gfp_t gfp)
  419. {
  420. u8 *data;
  421. struct pci_dev *pdev = bp->pdev;
  422. data = kmalloc(bp->rx_buf_size, gfp);
  423. if (!data)
  424. return NULL;
  425. *mapping = dma_map_single(&pdev->dev, data + BNXT_RX_DMA_OFFSET,
  426. bp->rx_buf_use_size, PCI_DMA_FROMDEVICE);
  427. if (dma_mapping_error(&pdev->dev, *mapping)) {
  428. kfree(data);
  429. data = NULL;
  430. }
  431. return data;
  432. }
  433. static inline int bnxt_alloc_rx_data(struct bnxt *bp,
  434. struct bnxt_rx_ring_info *rxr,
  435. u16 prod, gfp_t gfp)
  436. {
  437. struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
  438. struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[prod];
  439. u8 *data;
  440. dma_addr_t mapping;
  441. data = __bnxt_alloc_rx_data(bp, &mapping, gfp);
  442. if (!data)
  443. return -ENOMEM;
  444. rx_buf->data = data;
  445. dma_unmap_addr_set(rx_buf, mapping, mapping);
  446. rxbd->rx_bd_haddr = cpu_to_le64(mapping);
  447. return 0;
  448. }
  449. static void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons,
  450. u8 *data)
  451. {
  452. u16 prod = rxr->rx_prod;
  453. struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
  454. struct rx_bd *cons_bd, *prod_bd;
  455. prod_rx_buf = &rxr->rx_buf_ring[prod];
  456. cons_rx_buf = &rxr->rx_buf_ring[cons];
  457. prod_rx_buf->data = data;
  458. dma_unmap_addr_set(prod_rx_buf, mapping,
  459. dma_unmap_addr(cons_rx_buf, mapping));
  460. prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
  461. cons_bd = &rxr->rx_desc_ring[RX_RING(cons)][RX_IDX(cons)];
  462. prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
  463. }
  464. static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
  465. {
  466. u16 next, max = rxr->rx_agg_bmap_size;
  467. next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
  468. if (next >= max)
  469. next = find_first_zero_bit(rxr->rx_agg_bmap, max);
  470. return next;
  471. }
  472. static inline int bnxt_alloc_rx_page(struct bnxt *bp,
  473. struct bnxt_rx_ring_info *rxr,
  474. u16 prod, gfp_t gfp)
  475. {
  476. struct rx_bd *rxbd =
  477. &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
  478. struct bnxt_sw_rx_agg_bd *rx_agg_buf;
  479. struct pci_dev *pdev = bp->pdev;
  480. struct page *page;
  481. dma_addr_t mapping;
  482. u16 sw_prod = rxr->rx_sw_agg_prod;
  483. page = alloc_page(gfp);
  484. if (!page)
  485. return -ENOMEM;
  486. mapping = dma_map_page(&pdev->dev, page, 0, PAGE_SIZE,
  487. PCI_DMA_FROMDEVICE);
  488. if (dma_mapping_error(&pdev->dev, mapping)) {
  489. __free_page(page);
  490. return -EIO;
  491. }
  492. if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
  493. sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
  494. __set_bit(sw_prod, rxr->rx_agg_bmap);
  495. rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
  496. rxr->rx_sw_agg_prod = NEXT_RX_AGG(sw_prod);
  497. rx_agg_buf->page = page;
  498. rx_agg_buf->mapping = mapping;
  499. rxbd->rx_bd_haddr = cpu_to_le64(mapping);
  500. rxbd->rx_bd_opaque = sw_prod;
  501. return 0;
  502. }
  503. static void bnxt_reuse_rx_agg_bufs(struct bnxt_napi *bnapi, u16 cp_cons,
  504. u32 agg_bufs)
  505. {
  506. struct bnxt *bp = bnapi->bp;
  507. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  508. struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
  509. u16 prod = rxr->rx_agg_prod;
  510. u16 sw_prod = rxr->rx_sw_agg_prod;
  511. u32 i;
  512. for (i = 0; i < agg_bufs; i++) {
  513. u16 cons;
  514. struct rx_agg_cmp *agg;
  515. struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
  516. struct rx_bd *prod_bd;
  517. struct page *page;
  518. agg = (struct rx_agg_cmp *)
  519. &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
  520. cons = agg->rx_agg_cmp_opaque;
  521. __clear_bit(cons, rxr->rx_agg_bmap);
  522. if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
  523. sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
  524. __set_bit(sw_prod, rxr->rx_agg_bmap);
  525. prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
  526. cons_rx_buf = &rxr->rx_agg_ring[cons];
  527. /* It is possible for sw_prod to be equal to cons, so
  528. * set cons_rx_buf->page to NULL first.
  529. */
  530. page = cons_rx_buf->page;
  531. cons_rx_buf->page = NULL;
  532. prod_rx_buf->page = page;
  533. prod_rx_buf->mapping = cons_rx_buf->mapping;
  534. prod_bd = &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
  535. prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
  536. prod_bd->rx_bd_opaque = sw_prod;
  537. prod = NEXT_RX_AGG(prod);
  538. sw_prod = NEXT_RX_AGG(sw_prod);
  539. cp_cons = NEXT_CMP(cp_cons);
  540. }
  541. rxr->rx_agg_prod = prod;
  542. rxr->rx_sw_agg_prod = sw_prod;
  543. }
  544. static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
  545. struct bnxt_rx_ring_info *rxr, u16 cons,
  546. u16 prod, u8 *data, dma_addr_t dma_addr,
  547. unsigned int len)
  548. {
  549. int err;
  550. struct sk_buff *skb;
  551. err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
  552. if (unlikely(err)) {
  553. bnxt_reuse_rx_data(rxr, cons, data);
  554. return NULL;
  555. }
  556. skb = build_skb(data, 0);
  557. dma_unmap_single(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
  558. PCI_DMA_FROMDEVICE);
  559. if (!skb) {
  560. kfree(data);
  561. return NULL;
  562. }
  563. skb_reserve(skb, BNXT_RX_OFFSET);
  564. skb_put(skb, len);
  565. return skb;
  566. }
  567. static struct sk_buff *bnxt_rx_pages(struct bnxt *bp, struct bnxt_napi *bnapi,
  568. struct sk_buff *skb, u16 cp_cons,
  569. u32 agg_bufs)
  570. {
  571. struct pci_dev *pdev = bp->pdev;
  572. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  573. struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
  574. u16 prod = rxr->rx_agg_prod;
  575. u32 i;
  576. for (i = 0; i < agg_bufs; i++) {
  577. u16 cons, frag_len;
  578. struct rx_agg_cmp *agg;
  579. struct bnxt_sw_rx_agg_bd *cons_rx_buf;
  580. struct page *page;
  581. dma_addr_t mapping;
  582. agg = (struct rx_agg_cmp *)
  583. &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
  584. cons = agg->rx_agg_cmp_opaque;
  585. frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
  586. RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
  587. cons_rx_buf = &rxr->rx_agg_ring[cons];
  588. skb_fill_page_desc(skb, i, cons_rx_buf->page, 0, frag_len);
  589. __clear_bit(cons, rxr->rx_agg_bmap);
  590. /* It is possible for bnxt_alloc_rx_page() to allocate
  591. * a sw_prod index that equals the cons index, so we
  592. * need to clear the cons entry now.
  593. */
  594. mapping = dma_unmap_addr(cons_rx_buf, mapping);
  595. page = cons_rx_buf->page;
  596. cons_rx_buf->page = NULL;
  597. if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_ATOMIC) != 0) {
  598. struct skb_shared_info *shinfo;
  599. unsigned int nr_frags;
  600. shinfo = skb_shinfo(skb);
  601. nr_frags = --shinfo->nr_frags;
  602. __skb_frag_set_page(&shinfo->frags[nr_frags], NULL);
  603. dev_kfree_skb(skb);
  604. cons_rx_buf->page = page;
  605. /* Update prod since possibly some pages have been
  606. * allocated already.
  607. */
  608. rxr->rx_agg_prod = prod;
  609. bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs - i);
  610. return NULL;
  611. }
  612. dma_unmap_page(&pdev->dev, mapping, PAGE_SIZE,
  613. PCI_DMA_FROMDEVICE);
  614. skb->data_len += frag_len;
  615. skb->len += frag_len;
  616. skb->truesize += PAGE_SIZE;
  617. prod = NEXT_RX_AGG(prod);
  618. cp_cons = NEXT_CMP(cp_cons);
  619. }
  620. rxr->rx_agg_prod = prod;
  621. return skb;
  622. }
  623. static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
  624. u8 agg_bufs, u32 *raw_cons)
  625. {
  626. u16 last;
  627. struct rx_agg_cmp *agg;
  628. *raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
  629. last = RING_CMP(*raw_cons);
  630. agg = (struct rx_agg_cmp *)
  631. &cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
  632. return RX_AGG_CMP_VALID(agg, *raw_cons);
  633. }
  634. static inline struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
  635. unsigned int len,
  636. dma_addr_t mapping)
  637. {
  638. struct bnxt *bp = bnapi->bp;
  639. struct pci_dev *pdev = bp->pdev;
  640. struct sk_buff *skb;
  641. skb = napi_alloc_skb(&bnapi->napi, len);
  642. if (!skb)
  643. return NULL;
  644. dma_sync_single_for_cpu(&pdev->dev, mapping,
  645. bp->rx_copy_thresh, PCI_DMA_FROMDEVICE);
  646. memcpy(skb->data - BNXT_RX_OFFSET, data, len + BNXT_RX_OFFSET);
  647. dma_sync_single_for_device(&pdev->dev, mapping,
  648. bp->rx_copy_thresh,
  649. PCI_DMA_FROMDEVICE);
  650. skb_put(skb, len);
  651. return skb;
  652. }
  653. static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
  654. struct rx_tpa_start_cmp *tpa_start,
  655. struct rx_tpa_start_cmp_ext *tpa_start1)
  656. {
  657. u8 agg_id = TPA_START_AGG_ID(tpa_start);
  658. u16 cons, prod;
  659. struct bnxt_tpa_info *tpa_info;
  660. struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
  661. struct rx_bd *prod_bd;
  662. dma_addr_t mapping;
  663. cons = tpa_start->rx_tpa_start_cmp_opaque;
  664. prod = rxr->rx_prod;
  665. cons_rx_buf = &rxr->rx_buf_ring[cons];
  666. prod_rx_buf = &rxr->rx_buf_ring[prod];
  667. tpa_info = &rxr->rx_tpa[agg_id];
  668. prod_rx_buf->data = tpa_info->data;
  669. mapping = tpa_info->mapping;
  670. dma_unmap_addr_set(prod_rx_buf, mapping, mapping);
  671. prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
  672. prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
  673. tpa_info->data = cons_rx_buf->data;
  674. cons_rx_buf->data = NULL;
  675. tpa_info->mapping = dma_unmap_addr(cons_rx_buf, mapping);
  676. tpa_info->len =
  677. le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
  678. RX_TPA_START_CMP_LEN_SHIFT;
  679. if (likely(TPA_START_HASH_VALID(tpa_start))) {
  680. u32 hash_type = TPA_START_HASH_TYPE(tpa_start);
  681. tpa_info->hash_type = PKT_HASH_TYPE_L4;
  682. tpa_info->gso_type = SKB_GSO_TCPV4;
  683. /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
  684. if (hash_type == 3)
  685. tpa_info->gso_type = SKB_GSO_TCPV6;
  686. tpa_info->rss_hash =
  687. le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
  688. } else {
  689. tpa_info->hash_type = PKT_HASH_TYPE_NONE;
  690. tpa_info->gso_type = 0;
  691. if (netif_msg_rx_err(bp))
  692. netdev_warn(bp->dev, "TPA packet without valid hash\n");
  693. }
  694. tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
  695. tpa_info->metadata = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
  696. rxr->rx_prod = NEXT_RX(prod);
  697. cons = NEXT_RX(cons);
  698. cons_rx_buf = &rxr->rx_buf_ring[cons];
  699. bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
  700. rxr->rx_prod = NEXT_RX(rxr->rx_prod);
  701. cons_rx_buf->data = NULL;
  702. }
  703. static void bnxt_abort_tpa(struct bnxt *bp, struct bnxt_napi *bnapi,
  704. u16 cp_cons, u32 agg_bufs)
  705. {
  706. if (agg_bufs)
  707. bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs);
  708. }
  709. #define BNXT_IPV4_HDR_SIZE (sizeof(struct iphdr) + sizeof(struct tcphdr))
  710. #define BNXT_IPV6_HDR_SIZE (sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
  711. static inline struct sk_buff *bnxt_gro_skb(struct bnxt_tpa_info *tpa_info,
  712. struct rx_tpa_end_cmp *tpa_end,
  713. struct rx_tpa_end_cmp_ext *tpa_end1,
  714. struct sk_buff *skb)
  715. {
  716. #ifdef CONFIG_INET
  717. struct tcphdr *th;
  718. int payload_off, tcp_opt_len = 0;
  719. int len, nw_off;
  720. u16 segs;
  721. segs = TPA_END_TPA_SEGS(tpa_end);
  722. if (segs == 1)
  723. return skb;
  724. NAPI_GRO_CB(skb)->count = segs;
  725. skb_shinfo(skb)->gso_size =
  726. le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
  727. skb_shinfo(skb)->gso_type = tpa_info->gso_type;
  728. payload_off = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
  729. RX_TPA_END_CMP_PAYLOAD_OFFSET) >>
  730. RX_TPA_END_CMP_PAYLOAD_OFFSET_SHIFT;
  731. if (TPA_END_GRO_TS(tpa_end))
  732. tcp_opt_len = 12;
  733. if (tpa_info->gso_type == SKB_GSO_TCPV4) {
  734. struct iphdr *iph;
  735. nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
  736. ETH_HLEN;
  737. skb_set_network_header(skb, nw_off);
  738. iph = ip_hdr(skb);
  739. skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
  740. len = skb->len - skb_transport_offset(skb);
  741. th = tcp_hdr(skb);
  742. th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
  743. } else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
  744. struct ipv6hdr *iph;
  745. nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
  746. ETH_HLEN;
  747. skb_set_network_header(skb, nw_off);
  748. iph = ipv6_hdr(skb);
  749. skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
  750. len = skb->len - skb_transport_offset(skb);
  751. th = tcp_hdr(skb);
  752. th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
  753. } else {
  754. dev_kfree_skb_any(skb);
  755. return NULL;
  756. }
  757. tcp_gro_complete(skb);
  758. if (nw_off) { /* tunnel */
  759. struct udphdr *uh = NULL;
  760. if (skb->protocol == htons(ETH_P_IP)) {
  761. struct iphdr *iph = (struct iphdr *)skb->data;
  762. if (iph->protocol == IPPROTO_UDP)
  763. uh = (struct udphdr *)(iph + 1);
  764. } else {
  765. struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
  766. if (iph->nexthdr == IPPROTO_UDP)
  767. uh = (struct udphdr *)(iph + 1);
  768. }
  769. if (uh) {
  770. if (uh->check)
  771. skb_shinfo(skb)->gso_type |=
  772. SKB_GSO_UDP_TUNNEL_CSUM;
  773. else
  774. skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
  775. }
  776. }
  777. #endif
  778. return skb;
  779. }
  780. static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
  781. struct bnxt_napi *bnapi,
  782. u32 *raw_cons,
  783. struct rx_tpa_end_cmp *tpa_end,
  784. struct rx_tpa_end_cmp_ext *tpa_end1,
  785. bool *agg_event)
  786. {
  787. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  788. struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
  789. u8 agg_id = TPA_END_AGG_ID(tpa_end);
  790. u8 *data, agg_bufs;
  791. u16 cp_cons = RING_CMP(*raw_cons);
  792. unsigned int len;
  793. struct bnxt_tpa_info *tpa_info;
  794. dma_addr_t mapping;
  795. struct sk_buff *skb;
  796. tpa_info = &rxr->rx_tpa[agg_id];
  797. data = tpa_info->data;
  798. prefetch(data);
  799. len = tpa_info->len;
  800. mapping = tpa_info->mapping;
  801. agg_bufs = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
  802. RX_TPA_END_CMP_AGG_BUFS) >> RX_TPA_END_CMP_AGG_BUFS_SHIFT;
  803. if (agg_bufs) {
  804. if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
  805. return ERR_PTR(-EBUSY);
  806. *agg_event = true;
  807. cp_cons = NEXT_CMP(cp_cons);
  808. }
  809. if (unlikely(agg_bufs > MAX_SKB_FRAGS)) {
  810. bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
  811. netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
  812. agg_bufs, (int)MAX_SKB_FRAGS);
  813. return NULL;
  814. }
  815. if (len <= bp->rx_copy_thresh) {
  816. skb = bnxt_copy_skb(bnapi, data, len, mapping);
  817. if (!skb) {
  818. bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
  819. return NULL;
  820. }
  821. } else {
  822. u8 *new_data;
  823. dma_addr_t new_mapping;
  824. new_data = __bnxt_alloc_rx_data(bp, &new_mapping, GFP_ATOMIC);
  825. if (!new_data) {
  826. bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
  827. return NULL;
  828. }
  829. tpa_info->data = new_data;
  830. tpa_info->mapping = new_mapping;
  831. skb = build_skb(data, 0);
  832. dma_unmap_single(&bp->pdev->dev, mapping, bp->rx_buf_use_size,
  833. PCI_DMA_FROMDEVICE);
  834. if (!skb) {
  835. kfree(data);
  836. bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
  837. return NULL;
  838. }
  839. skb_reserve(skb, BNXT_RX_OFFSET);
  840. skb_put(skb, len);
  841. }
  842. if (agg_bufs) {
  843. skb = bnxt_rx_pages(bp, bnapi, skb, cp_cons, agg_bufs);
  844. if (!skb) {
  845. /* Page reuse already handled by bnxt_rx_pages(). */
  846. return NULL;
  847. }
  848. }
  849. skb->protocol = eth_type_trans(skb, bp->dev);
  850. if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
  851. skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
  852. if (tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) {
  853. netdev_features_t features = skb->dev->features;
  854. u16 vlan_proto = tpa_info->metadata >>
  855. RX_CMP_FLAGS2_METADATA_TPID_SFT;
  856. if (((features & NETIF_F_HW_VLAN_CTAG_RX) &&
  857. vlan_proto == ETH_P_8021Q) ||
  858. ((features & NETIF_F_HW_VLAN_STAG_RX) &&
  859. vlan_proto == ETH_P_8021AD)) {
  860. __vlan_hwaccel_put_tag(skb, htons(vlan_proto),
  861. tpa_info->metadata &
  862. RX_CMP_FLAGS2_METADATA_VID_MASK);
  863. }
  864. }
  865. skb_checksum_none_assert(skb);
  866. if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
  867. skb->ip_summed = CHECKSUM_UNNECESSARY;
  868. skb->csum_level =
  869. (tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
  870. }
  871. if (TPA_END_GRO(tpa_end))
  872. skb = bnxt_gro_skb(tpa_info, tpa_end, tpa_end1, skb);
  873. return skb;
  874. }
  875. /* returns the following:
  876. * 1 - 1 packet successfully received
  877. * 0 - successful TPA_START, packet not completed yet
  878. * -EBUSY - completion ring does not have all the agg buffers yet
  879. * -ENOMEM - packet aborted due to out of memory
  880. * -EIO - packet aborted due to hw error indicated in BD
  881. */
  882. static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_napi *bnapi, u32 *raw_cons,
  883. bool *agg_event)
  884. {
  885. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  886. struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
  887. struct net_device *dev = bp->dev;
  888. struct rx_cmp *rxcmp;
  889. struct rx_cmp_ext *rxcmp1;
  890. u32 tmp_raw_cons = *raw_cons;
  891. u16 cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
  892. struct bnxt_sw_rx_bd *rx_buf;
  893. unsigned int len;
  894. u8 *data, agg_bufs, cmp_type;
  895. dma_addr_t dma_addr;
  896. struct sk_buff *skb;
  897. int rc = 0;
  898. rxcmp = (struct rx_cmp *)
  899. &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
  900. tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
  901. cp_cons = RING_CMP(tmp_raw_cons);
  902. rxcmp1 = (struct rx_cmp_ext *)
  903. &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
  904. if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
  905. return -EBUSY;
  906. cmp_type = RX_CMP_TYPE(rxcmp);
  907. prod = rxr->rx_prod;
  908. if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP) {
  909. bnxt_tpa_start(bp, rxr, (struct rx_tpa_start_cmp *)rxcmp,
  910. (struct rx_tpa_start_cmp_ext *)rxcmp1);
  911. goto next_rx_no_prod;
  912. } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
  913. skb = bnxt_tpa_end(bp, bnapi, &tmp_raw_cons,
  914. (struct rx_tpa_end_cmp *)rxcmp,
  915. (struct rx_tpa_end_cmp_ext *)rxcmp1,
  916. agg_event);
  917. if (unlikely(IS_ERR(skb)))
  918. return -EBUSY;
  919. rc = -ENOMEM;
  920. if (likely(skb)) {
  921. skb_record_rx_queue(skb, bnapi->index);
  922. skb_mark_napi_id(skb, &bnapi->napi);
  923. if (bnxt_busy_polling(bnapi))
  924. netif_receive_skb(skb);
  925. else
  926. napi_gro_receive(&bnapi->napi, skb);
  927. rc = 1;
  928. }
  929. goto next_rx_no_prod;
  930. }
  931. cons = rxcmp->rx_cmp_opaque;
  932. rx_buf = &rxr->rx_buf_ring[cons];
  933. data = rx_buf->data;
  934. prefetch(data);
  935. agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) & RX_CMP_AGG_BUFS) >>
  936. RX_CMP_AGG_BUFS_SHIFT;
  937. if (agg_bufs) {
  938. if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
  939. return -EBUSY;
  940. cp_cons = NEXT_CMP(cp_cons);
  941. *agg_event = true;
  942. }
  943. rx_buf->data = NULL;
  944. if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
  945. bnxt_reuse_rx_data(rxr, cons, data);
  946. if (agg_bufs)
  947. bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs);
  948. rc = -EIO;
  949. goto next_rx;
  950. }
  951. len = le32_to_cpu(rxcmp->rx_cmp_len_flags_type) >> RX_CMP_LEN_SHIFT;
  952. dma_addr = dma_unmap_addr(rx_buf, mapping);
  953. if (len <= bp->rx_copy_thresh) {
  954. skb = bnxt_copy_skb(bnapi, data, len, dma_addr);
  955. bnxt_reuse_rx_data(rxr, cons, data);
  956. if (!skb) {
  957. rc = -ENOMEM;
  958. goto next_rx;
  959. }
  960. } else {
  961. skb = bnxt_rx_skb(bp, rxr, cons, prod, data, dma_addr, len);
  962. if (!skb) {
  963. rc = -ENOMEM;
  964. goto next_rx;
  965. }
  966. }
  967. if (agg_bufs) {
  968. skb = bnxt_rx_pages(bp, bnapi, skb, cp_cons, agg_bufs);
  969. if (!skb) {
  970. rc = -ENOMEM;
  971. goto next_rx;
  972. }
  973. }
  974. if (RX_CMP_HASH_VALID(rxcmp)) {
  975. u32 hash_type = RX_CMP_HASH_TYPE(rxcmp);
  976. enum pkt_hash_types type = PKT_HASH_TYPE_L4;
  977. /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
  978. if (hash_type != 1 && hash_type != 3)
  979. type = PKT_HASH_TYPE_L3;
  980. skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
  981. }
  982. skb->protocol = eth_type_trans(skb, dev);
  983. if (rxcmp1->rx_cmp_flags2 &
  984. cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)) {
  985. netdev_features_t features = skb->dev->features;
  986. u32 meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
  987. u16 vlan_proto = meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT;
  988. if (((features & NETIF_F_HW_VLAN_CTAG_RX) &&
  989. vlan_proto == ETH_P_8021Q) ||
  990. ((features & NETIF_F_HW_VLAN_STAG_RX) &&
  991. vlan_proto == ETH_P_8021AD))
  992. __vlan_hwaccel_put_tag(skb, htons(vlan_proto),
  993. meta_data &
  994. RX_CMP_FLAGS2_METADATA_VID_MASK);
  995. }
  996. skb_checksum_none_assert(skb);
  997. if (RX_CMP_L4_CS_OK(rxcmp1)) {
  998. if (dev->features & NETIF_F_RXCSUM) {
  999. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1000. skb->csum_level = RX_CMP_ENCAP(rxcmp1);
  1001. }
  1002. } else {
  1003. if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
  1004. if (dev->features & NETIF_F_RXCSUM)
  1005. cpr->rx_l4_csum_errors++;
  1006. }
  1007. }
  1008. skb_record_rx_queue(skb, bnapi->index);
  1009. skb_mark_napi_id(skb, &bnapi->napi);
  1010. if (bnxt_busy_polling(bnapi))
  1011. netif_receive_skb(skb);
  1012. else
  1013. napi_gro_receive(&bnapi->napi, skb);
  1014. rc = 1;
  1015. next_rx:
  1016. rxr->rx_prod = NEXT_RX(prod);
  1017. next_rx_no_prod:
  1018. *raw_cons = tmp_raw_cons;
  1019. return rc;
  1020. }
  1021. static int bnxt_async_event_process(struct bnxt *bp,
  1022. struct hwrm_async_event_cmpl *cmpl)
  1023. {
  1024. u16 event_id = le16_to_cpu(cmpl->event_id);
  1025. /* TODO CHIMP_FW: Define event id's for link change, error etc */
  1026. switch (event_id) {
  1027. case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
  1028. set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
  1029. break;
  1030. case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
  1031. set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
  1032. break;
  1033. default:
  1034. netdev_err(bp->dev, "unhandled ASYNC event (id 0x%x)\n",
  1035. event_id);
  1036. goto async_event_process_exit;
  1037. }
  1038. schedule_work(&bp->sp_task);
  1039. async_event_process_exit:
  1040. return 0;
  1041. }
  1042. static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
  1043. {
  1044. u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
  1045. struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
  1046. struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
  1047. (struct hwrm_fwd_req_cmpl *)txcmp;
  1048. switch (cmpl_type) {
  1049. case CMPL_BASE_TYPE_HWRM_DONE:
  1050. seq_id = le16_to_cpu(h_cmpl->sequence_id);
  1051. if (seq_id == bp->hwrm_intr_seq_id)
  1052. bp->hwrm_intr_seq_id = HWRM_SEQ_ID_INVALID;
  1053. else
  1054. netdev_err(bp->dev, "Invalid hwrm seq id %d\n", seq_id);
  1055. break;
  1056. case CMPL_BASE_TYPE_HWRM_FWD_REQ:
  1057. vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
  1058. if ((vf_id < bp->pf.first_vf_id) ||
  1059. (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
  1060. netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
  1061. vf_id);
  1062. return -EINVAL;
  1063. }
  1064. set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
  1065. set_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event);
  1066. schedule_work(&bp->sp_task);
  1067. break;
  1068. case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
  1069. bnxt_async_event_process(bp,
  1070. (struct hwrm_async_event_cmpl *)txcmp);
  1071. default:
  1072. break;
  1073. }
  1074. return 0;
  1075. }
  1076. static irqreturn_t bnxt_msix(int irq, void *dev_instance)
  1077. {
  1078. struct bnxt_napi *bnapi = dev_instance;
  1079. struct bnxt *bp = bnapi->bp;
  1080. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  1081. u32 cons = RING_CMP(cpr->cp_raw_cons);
  1082. prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
  1083. napi_schedule(&bnapi->napi);
  1084. return IRQ_HANDLED;
  1085. }
  1086. static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
  1087. {
  1088. u32 raw_cons = cpr->cp_raw_cons;
  1089. u16 cons = RING_CMP(raw_cons);
  1090. struct tx_cmp *txcmp;
  1091. txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
  1092. return TX_CMP_VALID(txcmp, raw_cons);
  1093. }
  1094. static irqreturn_t bnxt_inta(int irq, void *dev_instance)
  1095. {
  1096. struct bnxt_napi *bnapi = dev_instance;
  1097. struct bnxt *bp = bnapi->bp;
  1098. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  1099. u32 cons = RING_CMP(cpr->cp_raw_cons);
  1100. u32 int_status;
  1101. prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
  1102. if (!bnxt_has_work(bp, cpr)) {
  1103. int_status = readl(bp->bar0 + BNXT_CAG_REG_LEGACY_INT_STATUS);
  1104. /* return if erroneous interrupt */
  1105. if (!(int_status & (0x10000 << cpr->cp_ring_struct.fw_ring_id)))
  1106. return IRQ_NONE;
  1107. }
  1108. /* disable ring IRQ */
  1109. BNXT_CP_DB_IRQ_DIS(cpr->cp_doorbell);
  1110. /* Return here if interrupt is shared and is disabled. */
  1111. if (unlikely(atomic_read(&bp->intr_sem) != 0))
  1112. return IRQ_HANDLED;
  1113. napi_schedule(&bnapi->napi);
  1114. return IRQ_HANDLED;
  1115. }
  1116. static int bnxt_poll_work(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
  1117. {
  1118. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  1119. u32 raw_cons = cpr->cp_raw_cons;
  1120. u32 cons;
  1121. int tx_pkts = 0;
  1122. int rx_pkts = 0;
  1123. bool rx_event = false;
  1124. bool agg_event = false;
  1125. struct tx_cmp *txcmp;
  1126. while (1) {
  1127. int rc;
  1128. cons = RING_CMP(raw_cons);
  1129. txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
  1130. if (!TX_CMP_VALID(txcmp, raw_cons))
  1131. break;
  1132. if (TX_CMP_TYPE(txcmp) == CMP_TYPE_TX_L2_CMP) {
  1133. tx_pkts++;
  1134. /* return full budget so NAPI will complete. */
  1135. if (unlikely(tx_pkts > bp->tx_wake_thresh))
  1136. rx_pkts = budget;
  1137. } else if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
  1138. rc = bnxt_rx_pkt(bp, bnapi, &raw_cons, &agg_event);
  1139. if (likely(rc >= 0))
  1140. rx_pkts += rc;
  1141. else if (rc == -EBUSY) /* partial completion */
  1142. break;
  1143. rx_event = true;
  1144. } else if (unlikely((TX_CMP_TYPE(txcmp) ==
  1145. CMPL_BASE_TYPE_HWRM_DONE) ||
  1146. (TX_CMP_TYPE(txcmp) ==
  1147. CMPL_BASE_TYPE_HWRM_FWD_REQ) ||
  1148. (TX_CMP_TYPE(txcmp) ==
  1149. CMPL_BASE_TYPE_HWRM_ASYNC_EVENT))) {
  1150. bnxt_hwrm_handler(bp, txcmp);
  1151. }
  1152. raw_cons = NEXT_RAW_CMP(raw_cons);
  1153. if (rx_pkts == budget)
  1154. break;
  1155. }
  1156. cpr->cp_raw_cons = raw_cons;
  1157. /* ACK completion ring before freeing tx ring and producing new
  1158. * buffers in rx/agg rings to prevent overflowing the completion
  1159. * ring.
  1160. */
  1161. BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
  1162. if (tx_pkts)
  1163. bnxt_tx_int(bp, bnapi, tx_pkts);
  1164. if (rx_event) {
  1165. struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
  1166. writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
  1167. writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
  1168. if (agg_event) {
  1169. writel(DB_KEY_RX | rxr->rx_agg_prod,
  1170. rxr->rx_agg_doorbell);
  1171. writel(DB_KEY_RX | rxr->rx_agg_prod,
  1172. rxr->rx_agg_doorbell);
  1173. }
  1174. }
  1175. return rx_pkts;
  1176. }
  1177. static int bnxt_poll(struct napi_struct *napi, int budget)
  1178. {
  1179. struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
  1180. struct bnxt *bp = bnapi->bp;
  1181. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  1182. int work_done = 0;
  1183. if (!bnxt_lock_napi(bnapi))
  1184. return budget;
  1185. while (1) {
  1186. work_done += bnxt_poll_work(bp, bnapi, budget - work_done);
  1187. if (work_done >= budget)
  1188. break;
  1189. if (!bnxt_has_work(bp, cpr)) {
  1190. napi_complete(napi);
  1191. BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
  1192. break;
  1193. }
  1194. }
  1195. mmiowb();
  1196. bnxt_unlock_napi(bnapi);
  1197. return work_done;
  1198. }
  1199. #ifdef CONFIG_NET_RX_BUSY_POLL
  1200. static int bnxt_busy_poll(struct napi_struct *napi)
  1201. {
  1202. struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
  1203. struct bnxt *bp = bnapi->bp;
  1204. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  1205. int rx_work, budget = 4;
  1206. if (atomic_read(&bp->intr_sem) != 0)
  1207. return LL_FLUSH_FAILED;
  1208. if (!bnxt_lock_poll(bnapi))
  1209. return LL_FLUSH_BUSY;
  1210. rx_work = bnxt_poll_work(bp, bnapi, budget);
  1211. BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
  1212. bnxt_unlock_poll(bnapi);
  1213. return rx_work;
  1214. }
  1215. #endif
  1216. static void bnxt_free_tx_skbs(struct bnxt *bp)
  1217. {
  1218. int i, max_idx;
  1219. struct pci_dev *pdev = bp->pdev;
  1220. if (!bp->tx_ring)
  1221. return;
  1222. max_idx = bp->tx_nr_pages * TX_DESC_CNT;
  1223. for (i = 0; i < bp->tx_nr_rings; i++) {
  1224. struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
  1225. int j;
  1226. for (j = 0; j < max_idx;) {
  1227. struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[j];
  1228. struct sk_buff *skb = tx_buf->skb;
  1229. int k, last;
  1230. if (!skb) {
  1231. j++;
  1232. continue;
  1233. }
  1234. tx_buf->skb = NULL;
  1235. if (tx_buf->is_push) {
  1236. dev_kfree_skb(skb);
  1237. j += 2;
  1238. continue;
  1239. }
  1240. dma_unmap_single(&pdev->dev,
  1241. dma_unmap_addr(tx_buf, mapping),
  1242. skb_headlen(skb),
  1243. PCI_DMA_TODEVICE);
  1244. last = tx_buf->nr_frags;
  1245. j += 2;
  1246. for (k = 0; k < last; k++, j++) {
  1247. int ring_idx = j & bp->tx_ring_mask;
  1248. skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
  1249. tx_buf = &txr->tx_buf_ring[ring_idx];
  1250. dma_unmap_page(
  1251. &pdev->dev,
  1252. dma_unmap_addr(tx_buf, mapping),
  1253. skb_frag_size(frag), PCI_DMA_TODEVICE);
  1254. }
  1255. dev_kfree_skb(skb);
  1256. }
  1257. netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, i));
  1258. }
  1259. }
  1260. static void bnxt_free_rx_skbs(struct bnxt *bp)
  1261. {
  1262. int i, max_idx, max_agg_idx;
  1263. struct pci_dev *pdev = bp->pdev;
  1264. if (!bp->rx_ring)
  1265. return;
  1266. max_idx = bp->rx_nr_pages * RX_DESC_CNT;
  1267. max_agg_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
  1268. for (i = 0; i < bp->rx_nr_rings; i++) {
  1269. struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
  1270. int j;
  1271. if (rxr->rx_tpa) {
  1272. for (j = 0; j < MAX_TPA; j++) {
  1273. struct bnxt_tpa_info *tpa_info =
  1274. &rxr->rx_tpa[j];
  1275. u8 *data = tpa_info->data;
  1276. if (!data)
  1277. continue;
  1278. dma_unmap_single(
  1279. &pdev->dev,
  1280. dma_unmap_addr(tpa_info, mapping),
  1281. bp->rx_buf_use_size,
  1282. PCI_DMA_FROMDEVICE);
  1283. tpa_info->data = NULL;
  1284. kfree(data);
  1285. }
  1286. }
  1287. for (j = 0; j < max_idx; j++) {
  1288. struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[j];
  1289. u8 *data = rx_buf->data;
  1290. if (!data)
  1291. continue;
  1292. dma_unmap_single(&pdev->dev,
  1293. dma_unmap_addr(rx_buf, mapping),
  1294. bp->rx_buf_use_size,
  1295. PCI_DMA_FROMDEVICE);
  1296. rx_buf->data = NULL;
  1297. kfree(data);
  1298. }
  1299. for (j = 0; j < max_agg_idx; j++) {
  1300. struct bnxt_sw_rx_agg_bd *rx_agg_buf =
  1301. &rxr->rx_agg_ring[j];
  1302. struct page *page = rx_agg_buf->page;
  1303. if (!page)
  1304. continue;
  1305. dma_unmap_page(&pdev->dev,
  1306. dma_unmap_addr(rx_agg_buf, mapping),
  1307. PAGE_SIZE, PCI_DMA_FROMDEVICE);
  1308. rx_agg_buf->page = NULL;
  1309. __clear_bit(j, rxr->rx_agg_bmap);
  1310. __free_page(page);
  1311. }
  1312. }
  1313. }
  1314. static void bnxt_free_skbs(struct bnxt *bp)
  1315. {
  1316. bnxt_free_tx_skbs(bp);
  1317. bnxt_free_rx_skbs(bp);
  1318. }
  1319. static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_struct *ring)
  1320. {
  1321. struct pci_dev *pdev = bp->pdev;
  1322. int i;
  1323. for (i = 0; i < ring->nr_pages; i++) {
  1324. if (!ring->pg_arr[i])
  1325. continue;
  1326. dma_free_coherent(&pdev->dev, ring->page_size,
  1327. ring->pg_arr[i], ring->dma_arr[i]);
  1328. ring->pg_arr[i] = NULL;
  1329. }
  1330. if (ring->pg_tbl) {
  1331. dma_free_coherent(&pdev->dev, ring->nr_pages * 8,
  1332. ring->pg_tbl, ring->pg_tbl_map);
  1333. ring->pg_tbl = NULL;
  1334. }
  1335. if (ring->vmem_size && *ring->vmem) {
  1336. vfree(*ring->vmem);
  1337. *ring->vmem = NULL;
  1338. }
  1339. }
  1340. static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_struct *ring)
  1341. {
  1342. int i;
  1343. struct pci_dev *pdev = bp->pdev;
  1344. if (ring->nr_pages > 1) {
  1345. ring->pg_tbl = dma_alloc_coherent(&pdev->dev,
  1346. ring->nr_pages * 8,
  1347. &ring->pg_tbl_map,
  1348. GFP_KERNEL);
  1349. if (!ring->pg_tbl)
  1350. return -ENOMEM;
  1351. }
  1352. for (i = 0; i < ring->nr_pages; i++) {
  1353. ring->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
  1354. ring->page_size,
  1355. &ring->dma_arr[i],
  1356. GFP_KERNEL);
  1357. if (!ring->pg_arr[i])
  1358. return -ENOMEM;
  1359. if (ring->nr_pages > 1)
  1360. ring->pg_tbl[i] = cpu_to_le64(ring->dma_arr[i]);
  1361. }
  1362. if (ring->vmem_size) {
  1363. *ring->vmem = vzalloc(ring->vmem_size);
  1364. if (!(*ring->vmem))
  1365. return -ENOMEM;
  1366. }
  1367. return 0;
  1368. }
  1369. static void bnxt_free_rx_rings(struct bnxt *bp)
  1370. {
  1371. int i;
  1372. if (!bp->rx_ring)
  1373. return;
  1374. for (i = 0; i < bp->rx_nr_rings; i++) {
  1375. struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
  1376. struct bnxt_ring_struct *ring;
  1377. kfree(rxr->rx_tpa);
  1378. rxr->rx_tpa = NULL;
  1379. kfree(rxr->rx_agg_bmap);
  1380. rxr->rx_agg_bmap = NULL;
  1381. ring = &rxr->rx_ring_struct;
  1382. bnxt_free_ring(bp, ring);
  1383. ring = &rxr->rx_agg_ring_struct;
  1384. bnxt_free_ring(bp, ring);
  1385. }
  1386. }
  1387. static int bnxt_alloc_rx_rings(struct bnxt *bp)
  1388. {
  1389. int i, rc, agg_rings = 0, tpa_rings = 0;
  1390. if (!bp->rx_ring)
  1391. return -ENOMEM;
  1392. if (bp->flags & BNXT_FLAG_AGG_RINGS)
  1393. agg_rings = 1;
  1394. if (bp->flags & BNXT_FLAG_TPA)
  1395. tpa_rings = 1;
  1396. for (i = 0; i < bp->rx_nr_rings; i++) {
  1397. struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
  1398. struct bnxt_ring_struct *ring;
  1399. ring = &rxr->rx_ring_struct;
  1400. rc = bnxt_alloc_ring(bp, ring);
  1401. if (rc)
  1402. return rc;
  1403. if (agg_rings) {
  1404. u16 mem_size;
  1405. ring = &rxr->rx_agg_ring_struct;
  1406. rc = bnxt_alloc_ring(bp, ring);
  1407. if (rc)
  1408. return rc;
  1409. rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
  1410. mem_size = rxr->rx_agg_bmap_size / 8;
  1411. rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
  1412. if (!rxr->rx_agg_bmap)
  1413. return -ENOMEM;
  1414. if (tpa_rings) {
  1415. rxr->rx_tpa = kcalloc(MAX_TPA,
  1416. sizeof(struct bnxt_tpa_info),
  1417. GFP_KERNEL);
  1418. if (!rxr->rx_tpa)
  1419. return -ENOMEM;
  1420. }
  1421. }
  1422. }
  1423. return 0;
  1424. }
  1425. static void bnxt_free_tx_rings(struct bnxt *bp)
  1426. {
  1427. int i;
  1428. struct pci_dev *pdev = bp->pdev;
  1429. if (!bp->tx_ring)
  1430. return;
  1431. for (i = 0; i < bp->tx_nr_rings; i++) {
  1432. struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
  1433. struct bnxt_ring_struct *ring;
  1434. if (txr->tx_push) {
  1435. dma_free_coherent(&pdev->dev, bp->tx_push_size,
  1436. txr->tx_push, txr->tx_push_mapping);
  1437. txr->tx_push = NULL;
  1438. }
  1439. ring = &txr->tx_ring_struct;
  1440. bnxt_free_ring(bp, ring);
  1441. }
  1442. }
  1443. static int bnxt_alloc_tx_rings(struct bnxt *bp)
  1444. {
  1445. int i, j, rc;
  1446. struct pci_dev *pdev = bp->pdev;
  1447. bp->tx_push_size = 0;
  1448. if (bp->tx_push_thresh) {
  1449. int push_size;
  1450. push_size = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
  1451. bp->tx_push_thresh);
  1452. if (push_size > 256) {
  1453. push_size = 0;
  1454. bp->tx_push_thresh = 0;
  1455. }
  1456. bp->tx_push_size = push_size;
  1457. }
  1458. for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
  1459. struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
  1460. struct bnxt_ring_struct *ring;
  1461. ring = &txr->tx_ring_struct;
  1462. rc = bnxt_alloc_ring(bp, ring);
  1463. if (rc)
  1464. return rc;
  1465. if (bp->tx_push_size) {
  1466. dma_addr_t mapping;
  1467. /* One pre-allocated DMA buffer to backup
  1468. * TX push operation
  1469. */
  1470. txr->tx_push = dma_alloc_coherent(&pdev->dev,
  1471. bp->tx_push_size,
  1472. &txr->tx_push_mapping,
  1473. GFP_KERNEL);
  1474. if (!txr->tx_push)
  1475. return -ENOMEM;
  1476. mapping = txr->tx_push_mapping +
  1477. sizeof(struct tx_push_bd);
  1478. txr->data_mapping = cpu_to_le64(mapping);
  1479. memset(txr->tx_push, 0, sizeof(struct tx_push_bd));
  1480. }
  1481. ring->queue_id = bp->q_info[j].queue_id;
  1482. if (i % bp->tx_nr_rings_per_tc == (bp->tx_nr_rings_per_tc - 1))
  1483. j++;
  1484. }
  1485. return 0;
  1486. }
  1487. static void bnxt_free_cp_rings(struct bnxt *bp)
  1488. {
  1489. int i;
  1490. if (!bp->bnapi)
  1491. return;
  1492. for (i = 0; i < bp->cp_nr_rings; i++) {
  1493. struct bnxt_napi *bnapi = bp->bnapi[i];
  1494. struct bnxt_cp_ring_info *cpr;
  1495. struct bnxt_ring_struct *ring;
  1496. if (!bnapi)
  1497. continue;
  1498. cpr = &bnapi->cp_ring;
  1499. ring = &cpr->cp_ring_struct;
  1500. bnxt_free_ring(bp, ring);
  1501. }
  1502. }
  1503. static int bnxt_alloc_cp_rings(struct bnxt *bp)
  1504. {
  1505. int i, rc;
  1506. for (i = 0; i < bp->cp_nr_rings; i++) {
  1507. struct bnxt_napi *bnapi = bp->bnapi[i];
  1508. struct bnxt_cp_ring_info *cpr;
  1509. struct bnxt_ring_struct *ring;
  1510. if (!bnapi)
  1511. continue;
  1512. cpr = &bnapi->cp_ring;
  1513. ring = &cpr->cp_ring_struct;
  1514. rc = bnxt_alloc_ring(bp, ring);
  1515. if (rc)
  1516. return rc;
  1517. }
  1518. return 0;
  1519. }
  1520. static void bnxt_init_ring_struct(struct bnxt *bp)
  1521. {
  1522. int i;
  1523. for (i = 0; i < bp->cp_nr_rings; i++) {
  1524. struct bnxt_napi *bnapi = bp->bnapi[i];
  1525. struct bnxt_cp_ring_info *cpr;
  1526. struct bnxt_rx_ring_info *rxr;
  1527. struct bnxt_tx_ring_info *txr;
  1528. struct bnxt_ring_struct *ring;
  1529. if (!bnapi)
  1530. continue;
  1531. cpr = &bnapi->cp_ring;
  1532. ring = &cpr->cp_ring_struct;
  1533. ring->nr_pages = bp->cp_nr_pages;
  1534. ring->page_size = HW_CMPD_RING_SIZE;
  1535. ring->pg_arr = (void **)cpr->cp_desc_ring;
  1536. ring->dma_arr = cpr->cp_desc_mapping;
  1537. ring->vmem_size = 0;
  1538. rxr = bnapi->rx_ring;
  1539. if (!rxr)
  1540. goto skip_rx;
  1541. ring = &rxr->rx_ring_struct;
  1542. ring->nr_pages = bp->rx_nr_pages;
  1543. ring->page_size = HW_RXBD_RING_SIZE;
  1544. ring->pg_arr = (void **)rxr->rx_desc_ring;
  1545. ring->dma_arr = rxr->rx_desc_mapping;
  1546. ring->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
  1547. ring->vmem = (void **)&rxr->rx_buf_ring;
  1548. ring = &rxr->rx_agg_ring_struct;
  1549. ring->nr_pages = bp->rx_agg_nr_pages;
  1550. ring->page_size = HW_RXBD_RING_SIZE;
  1551. ring->pg_arr = (void **)rxr->rx_agg_desc_ring;
  1552. ring->dma_arr = rxr->rx_agg_desc_mapping;
  1553. ring->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
  1554. ring->vmem = (void **)&rxr->rx_agg_ring;
  1555. skip_rx:
  1556. txr = bnapi->tx_ring;
  1557. if (!txr)
  1558. continue;
  1559. ring = &txr->tx_ring_struct;
  1560. ring->nr_pages = bp->tx_nr_pages;
  1561. ring->page_size = HW_RXBD_RING_SIZE;
  1562. ring->pg_arr = (void **)txr->tx_desc_ring;
  1563. ring->dma_arr = txr->tx_desc_mapping;
  1564. ring->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
  1565. ring->vmem = (void **)&txr->tx_buf_ring;
  1566. }
  1567. }
  1568. static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
  1569. {
  1570. int i;
  1571. u32 prod;
  1572. struct rx_bd **rx_buf_ring;
  1573. rx_buf_ring = (struct rx_bd **)ring->pg_arr;
  1574. for (i = 0, prod = 0; i < ring->nr_pages; i++) {
  1575. int j;
  1576. struct rx_bd *rxbd;
  1577. rxbd = rx_buf_ring[i];
  1578. if (!rxbd)
  1579. continue;
  1580. for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
  1581. rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
  1582. rxbd->rx_bd_opaque = prod;
  1583. }
  1584. }
  1585. }
  1586. static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
  1587. {
  1588. struct net_device *dev = bp->dev;
  1589. struct bnxt_rx_ring_info *rxr;
  1590. struct bnxt_ring_struct *ring;
  1591. u32 prod, type;
  1592. int i;
  1593. type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
  1594. RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
  1595. if (NET_IP_ALIGN == 2)
  1596. type |= RX_BD_FLAGS_SOP;
  1597. rxr = &bp->rx_ring[ring_nr];
  1598. ring = &rxr->rx_ring_struct;
  1599. bnxt_init_rxbd_pages(ring, type);
  1600. prod = rxr->rx_prod;
  1601. for (i = 0; i < bp->rx_ring_size; i++) {
  1602. if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL) != 0) {
  1603. netdev_warn(dev, "init'ed rx ring %d with %d/%d skbs only\n",
  1604. ring_nr, i, bp->rx_ring_size);
  1605. break;
  1606. }
  1607. prod = NEXT_RX(prod);
  1608. }
  1609. rxr->rx_prod = prod;
  1610. ring->fw_ring_id = INVALID_HW_RING_ID;
  1611. ring = &rxr->rx_agg_ring_struct;
  1612. ring->fw_ring_id = INVALID_HW_RING_ID;
  1613. if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
  1614. return 0;
  1615. type = ((u32)PAGE_SIZE << RX_BD_LEN_SHIFT) |
  1616. RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
  1617. bnxt_init_rxbd_pages(ring, type);
  1618. prod = rxr->rx_agg_prod;
  1619. for (i = 0; i < bp->rx_agg_ring_size; i++) {
  1620. if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL) != 0) {
  1621. netdev_warn(dev, "init'ed rx ring %d with %d/%d pages only\n",
  1622. ring_nr, i, bp->rx_ring_size);
  1623. break;
  1624. }
  1625. prod = NEXT_RX_AGG(prod);
  1626. }
  1627. rxr->rx_agg_prod = prod;
  1628. if (bp->flags & BNXT_FLAG_TPA) {
  1629. if (rxr->rx_tpa) {
  1630. u8 *data;
  1631. dma_addr_t mapping;
  1632. for (i = 0; i < MAX_TPA; i++) {
  1633. data = __bnxt_alloc_rx_data(bp, &mapping,
  1634. GFP_KERNEL);
  1635. if (!data)
  1636. return -ENOMEM;
  1637. rxr->rx_tpa[i].data = data;
  1638. rxr->rx_tpa[i].mapping = mapping;
  1639. }
  1640. } else {
  1641. netdev_err(bp->dev, "No resource allocated for LRO/GRO\n");
  1642. return -ENOMEM;
  1643. }
  1644. }
  1645. return 0;
  1646. }
  1647. static int bnxt_init_rx_rings(struct bnxt *bp)
  1648. {
  1649. int i, rc = 0;
  1650. for (i = 0; i < bp->rx_nr_rings; i++) {
  1651. rc = bnxt_init_one_rx_ring(bp, i);
  1652. if (rc)
  1653. break;
  1654. }
  1655. return rc;
  1656. }
  1657. static int bnxt_init_tx_rings(struct bnxt *bp)
  1658. {
  1659. u16 i;
  1660. bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
  1661. MAX_SKB_FRAGS + 1);
  1662. for (i = 0; i < bp->tx_nr_rings; i++) {
  1663. struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
  1664. struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
  1665. ring->fw_ring_id = INVALID_HW_RING_ID;
  1666. }
  1667. return 0;
  1668. }
  1669. static void bnxt_free_ring_grps(struct bnxt *bp)
  1670. {
  1671. kfree(bp->grp_info);
  1672. bp->grp_info = NULL;
  1673. }
  1674. static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
  1675. {
  1676. int i;
  1677. if (irq_re_init) {
  1678. bp->grp_info = kcalloc(bp->cp_nr_rings,
  1679. sizeof(struct bnxt_ring_grp_info),
  1680. GFP_KERNEL);
  1681. if (!bp->grp_info)
  1682. return -ENOMEM;
  1683. }
  1684. for (i = 0; i < bp->cp_nr_rings; i++) {
  1685. if (irq_re_init)
  1686. bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
  1687. bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
  1688. bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
  1689. bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
  1690. bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
  1691. }
  1692. return 0;
  1693. }
  1694. static void bnxt_free_vnics(struct bnxt *bp)
  1695. {
  1696. kfree(bp->vnic_info);
  1697. bp->vnic_info = NULL;
  1698. bp->nr_vnics = 0;
  1699. }
  1700. static int bnxt_alloc_vnics(struct bnxt *bp)
  1701. {
  1702. int num_vnics = 1;
  1703. #ifdef CONFIG_RFS_ACCEL
  1704. if (bp->flags & BNXT_FLAG_RFS)
  1705. num_vnics += bp->rx_nr_rings;
  1706. #endif
  1707. bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
  1708. GFP_KERNEL);
  1709. if (!bp->vnic_info)
  1710. return -ENOMEM;
  1711. bp->nr_vnics = num_vnics;
  1712. return 0;
  1713. }
  1714. static void bnxt_init_vnics(struct bnxt *bp)
  1715. {
  1716. int i;
  1717. for (i = 0; i < bp->nr_vnics; i++) {
  1718. struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
  1719. vnic->fw_vnic_id = INVALID_HW_RING_ID;
  1720. vnic->fw_rss_cos_lb_ctx = INVALID_HW_RING_ID;
  1721. vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
  1722. if (bp->vnic_info[i].rss_hash_key) {
  1723. if (i == 0)
  1724. prandom_bytes(vnic->rss_hash_key,
  1725. HW_HASH_KEY_SIZE);
  1726. else
  1727. memcpy(vnic->rss_hash_key,
  1728. bp->vnic_info[0].rss_hash_key,
  1729. HW_HASH_KEY_SIZE);
  1730. }
  1731. }
  1732. }
  1733. static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
  1734. {
  1735. int pages;
  1736. pages = ring_size / desc_per_pg;
  1737. if (!pages)
  1738. return 1;
  1739. pages++;
  1740. while (pages & (pages - 1))
  1741. pages++;
  1742. return pages;
  1743. }
  1744. static void bnxt_set_tpa_flags(struct bnxt *bp)
  1745. {
  1746. bp->flags &= ~BNXT_FLAG_TPA;
  1747. if (bp->dev->features & NETIF_F_LRO)
  1748. bp->flags |= BNXT_FLAG_LRO;
  1749. if ((bp->dev->features & NETIF_F_GRO) && (bp->pdev->revision > 0))
  1750. bp->flags |= BNXT_FLAG_GRO;
  1751. }
  1752. /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
  1753. * be set on entry.
  1754. */
  1755. void bnxt_set_ring_params(struct bnxt *bp)
  1756. {
  1757. u32 ring_size, rx_size, rx_space;
  1758. u32 agg_factor = 0, agg_ring_size = 0;
  1759. /* 8 for CRC and VLAN */
  1760. rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
  1761. rx_space = rx_size + NET_SKB_PAD +
  1762. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  1763. bp->rx_copy_thresh = BNXT_RX_COPY_THRESH;
  1764. ring_size = bp->rx_ring_size;
  1765. bp->rx_agg_ring_size = 0;
  1766. bp->rx_agg_nr_pages = 0;
  1767. if (bp->flags & BNXT_FLAG_TPA)
  1768. agg_factor = 4;
  1769. bp->flags &= ~BNXT_FLAG_JUMBO;
  1770. if (rx_space > PAGE_SIZE) {
  1771. u32 jumbo_factor;
  1772. bp->flags |= BNXT_FLAG_JUMBO;
  1773. jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
  1774. if (jumbo_factor > agg_factor)
  1775. agg_factor = jumbo_factor;
  1776. }
  1777. agg_ring_size = ring_size * agg_factor;
  1778. if (agg_ring_size) {
  1779. bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
  1780. RX_DESC_CNT);
  1781. if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
  1782. u32 tmp = agg_ring_size;
  1783. bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
  1784. agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
  1785. netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
  1786. tmp, agg_ring_size);
  1787. }
  1788. bp->rx_agg_ring_size = agg_ring_size;
  1789. bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
  1790. rx_size = SKB_DATA_ALIGN(BNXT_RX_COPY_THRESH + NET_IP_ALIGN);
  1791. rx_space = rx_size + NET_SKB_PAD +
  1792. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  1793. }
  1794. bp->rx_buf_use_size = rx_size;
  1795. bp->rx_buf_size = rx_space;
  1796. bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
  1797. bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
  1798. ring_size = bp->tx_ring_size;
  1799. bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
  1800. bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
  1801. ring_size = bp->rx_ring_size * (2 + agg_factor) + bp->tx_ring_size;
  1802. bp->cp_ring_size = ring_size;
  1803. bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
  1804. if (bp->cp_nr_pages > MAX_CP_PAGES) {
  1805. bp->cp_nr_pages = MAX_CP_PAGES;
  1806. bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
  1807. netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
  1808. ring_size, bp->cp_ring_size);
  1809. }
  1810. bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
  1811. bp->cp_ring_mask = bp->cp_bit - 1;
  1812. }
  1813. static void bnxt_free_vnic_attributes(struct bnxt *bp)
  1814. {
  1815. int i;
  1816. struct bnxt_vnic_info *vnic;
  1817. struct pci_dev *pdev = bp->pdev;
  1818. if (!bp->vnic_info)
  1819. return;
  1820. for (i = 0; i < bp->nr_vnics; i++) {
  1821. vnic = &bp->vnic_info[i];
  1822. kfree(vnic->fw_grp_ids);
  1823. vnic->fw_grp_ids = NULL;
  1824. kfree(vnic->uc_list);
  1825. vnic->uc_list = NULL;
  1826. if (vnic->mc_list) {
  1827. dma_free_coherent(&pdev->dev, vnic->mc_list_size,
  1828. vnic->mc_list, vnic->mc_list_mapping);
  1829. vnic->mc_list = NULL;
  1830. }
  1831. if (vnic->rss_table) {
  1832. dma_free_coherent(&pdev->dev, PAGE_SIZE,
  1833. vnic->rss_table,
  1834. vnic->rss_table_dma_addr);
  1835. vnic->rss_table = NULL;
  1836. }
  1837. vnic->rss_hash_key = NULL;
  1838. vnic->flags = 0;
  1839. }
  1840. }
  1841. static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
  1842. {
  1843. int i, rc = 0, size;
  1844. struct bnxt_vnic_info *vnic;
  1845. struct pci_dev *pdev = bp->pdev;
  1846. int max_rings;
  1847. for (i = 0; i < bp->nr_vnics; i++) {
  1848. vnic = &bp->vnic_info[i];
  1849. if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
  1850. int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
  1851. if (mem_size > 0) {
  1852. vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
  1853. if (!vnic->uc_list) {
  1854. rc = -ENOMEM;
  1855. goto out;
  1856. }
  1857. }
  1858. }
  1859. if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
  1860. vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
  1861. vnic->mc_list =
  1862. dma_alloc_coherent(&pdev->dev,
  1863. vnic->mc_list_size,
  1864. &vnic->mc_list_mapping,
  1865. GFP_KERNEL);
  1866. if (!vnic->mc_list) {
  1867. rc = -ENOMEM;
  1868. goto out;
  1869. }
  1870. }
  1871. if (vnic->flags & BNXT_VNIC_RSS_FLAG)
  1872. max_rings = bp->rx_nr_rings;
  1873. else
  1874. max_rings = 1;
  1875. vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
  1876. if (!vnic->fw_grp_ids) {
  1877. rc = -ENOMEM;
  1878. goto out;
  1879. }
  1880. /* Allocate rss table and hash key */
  1881. vnic->rss_table = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
  1882. &vnic->rss_table_dma_addr,
  1883. GFP_KERNEL);
  1884. if (!vnic->rss_table) {
  1885. rc = -ENOMEM;
  1886. goto out;
  1887. }
  1888. size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
  1889. vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
  1890. vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
  1891. }
  1892. return 0;
  1893. out:
  1894. return rc;
  1895. }
  1896. static void bnxt_free_hwrm_resources(struct bnxt *bp)
  1897. {
  1898. struct pci_dev *pdev = bp->pdev;
  1899. dma_free_coherent(&pdev->dev, PAGE_SIZE, bp->hwrm_cmd_resp_addr,
  1900. bp->hwrm_cmd_resp_dma_addr);
  1901. bp->hwrm_cmd_resp_addr = NULL;
  1902. if (bp->hwrm_dbg_resp_addr) {
  1903. dma_free_coherent(&pdev->dev, HWRM_DBG_REG_BUF_SIZE,
  1904. bp->hwrm_dbg_resp_addr,
  1905. bp->hwrm_dbg_resp_dma_addr);
  1906. bp->hwrm_dbg_resp_addr = NULL;
  1907. }
  1908. }
  1909. static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
  1910. {
  1911. struct pci_dev *pdev = bp->pdev;
  1912. bp->hwrm_cmd_resp_addr = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
  1913. &bp->hwrm_cmd_resp_dma_addr,
  1914. GFP_KERNEL);
  1915. if (!bp->hwrm_cmd_resp_addr)
  1916. return -ENOMEM;
  1917. bp->hwrm_dbg_resp_addr = dma_alloc_coherent(&pdev->dev,
  1918. HWRM_DBG_REG_BUF_SIZE,
  1919. &bp->hwrm_dbg_resp_dma_addr,
  1920. GFP_KERNEL);
  1921. if (!bp->hwrm_dbg_resp_addr)
  1922. netdev_warn(bp->dev, "fail to alloc debug register dma mem\n");
  1923. return 0;
  1924. }
  1925. static void bnxt_free_stats(struct bnxt *bp)
  1926. {
  1927. u32 size, i;
  1928. struct pci_dev *pdev = bp->pdev;
  1929. if (bp->hw_rx_port_stats) {
  1930. dma_free_coherent(&pdev->dev, bp->hw_port_stats_size,
  1931. bp->hw_rx_port_stats,
  1932. bp->hw_rx_port_stats_map);
  1933. bp->hw_rx_port_stats = NULL;
  1934. bp->flags &= ~BNXT_FLAG_PORT_STATS;
  1935. }
  1936. if (!bp->bnapi)
  1937. return;
  1938. size = sizeof(struct ctx_hw_stats);
  1939. for (i = 0; i < bp->cp_nr_rings; i++) {
  1940. struct bnxt_napi *bnapi = bp->bnapi[i];
  1941. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  1942. if (cpr->hw_stats) {
  1943. dma_free_coherent(&pdev->dev, size, cpr->hw_stats,
  1944. cpr->hw_stats_map);
  1945. cpr->hw_stats = NULL;
  1946. }
  1947. }
  1948. }
  1949. static int bnxt_alloc_stats(struct bnxt *bp)
  1950. {
  1951. u32 size, i;
  1952. struct pci_dev *pdev = bp->pdev;
  1953. size = sizeof(struct ctx_hw_stats);
  1954. for (i = 0; i < bp->cp_nr_rings; i++) {
  1955. struct bnxt_napi *bnapi = bp->bnapi[i];
  1956. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  1957. cpr->hw_stats = dma_alloc_coherent(&pdev->dev, size,
  1958. &cpr->hw_stats_map,
  1959. GFP_KERNEL);
  1960. if (!cpr->hw_stats)
  1961. return -ENOMEM;
  1962. cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
  1963. }
  1964. if (BNXT_PF(bp)) {
  1965. bp->hw_port_stats_size = sizeof(struct rx_port_stats) +
  1966. sizeof(struct tx_port_stats) + 1024;
  1967. bp->hw_rx_port_stats =
  1968. dma_alloc_coherent(&pdev->dev, bp->hw_port_stats_size,
  1969. &bp->hw_rx_port_stats_map,
  1970. GFP_KERNEL);
  1971. if (!bp->hw_rx_port_stats)
  1972. return -ENOMEM;
  1973. bp->hw_tx_port_stats = (void *)(bp->hw_rx_port_stats + 1) +
  1974. 512;
  1975. bp->hw_tx_port_stats_map = bp->hw_rx_port_stats_map +
  1976. sizeof(struct rx_port_stats) + 512;
  1977. bp->flags |= BNXT_FLAG_PORT_STATS;
  1978. }
  1979. return 0;
  1980. }
  1981. static void bnxt_clear_ring_indices(struct bnxt *bp)
  1982. {
  1983. int i;
  1984. if (!bp->bnapi)
  1985. return;
  1986. for (i = 0; i < bp->cp_nr_rings; i++) {
  1987. struct bnxt_napi *bnapi = bp->bnapi[i];
  1988. struct bnxt_cp_ring_info *cpr;
  1989. struct bnxt_rx_ring_info *rxr;
  1990. struct bnxt_tx_ring_info *txr;
  1991. if (!bnapi)
  1992. continue;
  1993. cpr = &bnapi->cp_ring;
  1994. cpr->cp_raw_cons = 0;
  1995. txr = bnapi->tx_ring;
  1996. if (txr) {
  1997. txr->tx_prod = 0;
  1998. txr->tx_cons = 0;
  1999. }
  2000. rxr = bnapi->rx_ring;
  2001. if (rxr) {
  2002. rxr->rx_prod = 0;
  2003. rxr->rx_agg_prod = 0;
  2004. rxr->rx_sw_agg_prod = 0;
  2005. }
  2006. }
  2007. }
  2008. static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool irq_reinit)
  2009. {
  2010. #ifdef CONFIG_RFS_ACCEL
  2011. int i;
  2012. /* Under rtnl_lock and all our NAPIs have been disabled. It's
  2013. * safe to delete the hash table.
  2014. */
  2015. for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
  2016. struct hlist_head *head;
  2017. struct hlist_node *tmp;
  2018. struct bnxt_ntuple_filter *fltr;
  2019. head = &bp->ntp_fltr_hash_tbl[i];
  2020. hlist_for_each_entry_safe(fltr, tmp, head, hash) {
  2021. hlist_del(&fltr->hash);
  2022. kfree(fltr);
  2023. }
  2024. }
  2025. if (irq_reinit) {
  2026. kfree(bp->ntp_fltr_bmap);
  2027. bp->ntp_fltr_bmap = NULL;
  2028. }
  2029. bp->ntp_fltr_count = 0;
  2030. #endif
  2031. }
  2032. static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
  2033. {
  2034. #ifdef CONFIG_RFS_ACCEL
  2035. int i, rc = 0;
  2036. if (!(bp->flags & BNXT_FLAG_RFS))
  2037. return 0;
  2038. for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
  2039. INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
  2040. bp->ntp_fltr_count = 0;
  2041. bp->ntp_fltr_bmap = kzalloc(BITS_TO_LONGS(BNXT_NTP_FLTR_MAX_FLTR),
  2042. GFP_KERNEL);
  2043. if (!bp->ntp_fltr_bmap)
  2044. rc = -ENOMEM;
  2045. return rc;
  2046. #else
  2047. return 0;
  2048. #endif
  2049. }
  2050. static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
  2051. {
  2052. bnxt_free_vnic_attributes(bp);
  2053. bnxt_free_tx_rings(bp);
  2054. bnxt_free_rx_rings(bp);
  2055. bnxt_free_cp_rings(bp);
  2056. bnxt_free_ntp_fltrs(bp, irq_re_init);
  2057. if (irq_re_init) {
  2058. bnxt_free_stats(bp);
  2059. bnxt_free_ring_grps(bp);
  2060. bnxt_free_vnics(bp);
  2061. kfree(bp->tx_ring);
  2062. bp->tx_ring = NULL;
  2063. kfree(bp->rx_ring);
  2064. bp->rx_ring = NULL;
  2065. kfree(bp->bnapi);
  2066. bp->bnapi = NULL;
  2067. } else {
  2068. bnxt_clear_ring_indices(bp);
  2069. }
  2070. }
  2071. static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
  2072. {
  2073. int i, j, rc, size, arr_size;
  2074. void *bnapi;
  2075. if (irq_re_init) {
  2076. /* Allocate bnapi mem pointer array and mem block for
  2077. * all queues
  2078. */
  2079. arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
  2080. bp->cp_nr_rings);
  2081. size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
  2082. bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
  2083. if (!bnapi)
  2084. return -ENOMEM;
  2085. bp->bnapi = bnapi;
  2086. bnapi += arr_size;
  2087. for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
  2088. bp->bnapi[i] = bnapi;
  2089. bp->bnapi[i]->index = i;
  2090. bp->bnapi[i]->bp = bp;
  2091. }
  2092. bp->rx_ring = kcalloc(bp->rx_nr_rings,
  2093. sizeof(struct bnxt_rx_ring_info),
  2094. GFP_KERNEL);
  2095. if (!bp->rx_ring)
  2096. return -ENOMEM;
  2097. for (i = 0; i < bp->rx_nr_rings; i++) {
  2098. bp->rx_ring[i].bnapi = bp->bnapi[i];
  2099. bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
  2100. }
  2101. bp->tx_ring = kcalloc(bp->tx_nr_rings,
  2102. sizeof(struct bnxt_tx_ring_info),
  2103. GFP_KERNEL);
  2104. if (!bp->tx_ring)
  2105. return -ENOMEM;
  2106. if (bp->flags & BNXT_FLAG_SHARED_RINGS)
  2107. j = 0;
  2108. else
  2109. j = bp->rx_nr_rings;
  2110. for (i = 0; i < bp->tx_nr_rings; i++, j++) {
  2111. bp->tx_ring[i].bnapi = bp->bnapi[j];
  2112. bp->bnapi[j]->tx_ring = &bp->tx_ring[i];
  2113. }
  2114. rc = bnxt_alloc_stats(bp);
  2115. if (rc)
  2116. goto alloc_mem_err;
  2117. rc = bnxt_alloc_ntp_fltrs(bp);
  2118. if (rc)
  2119. goto alloc_mem_err;
  2120. rc = bnxt_alloc_vnics(bp);
  2121. if (rc)
  2122. goto alloc_mem_err;
  2123. }
  2124. bnxt_init_ring_struct(bp);
  2125. rc = bnxt_alloc_rx_rings(bp);
  2126. if (rc)
  2127. goto alloc_mem_err;
  2128. rc = bnxt_alloc_tx_rings(bp);
  2129. if (rc)
  2130. goto alloc_mem_err;
  2131. rc = bnxt_alloc_cp_rings(bp);
  2132. if (rc)
  2133. goto alloc_mem_err;
  2134. bp->vnic_info[0].flags |= BNXT_VNIC_RSS_FLAG | BNXT_VNIC_MCAST_FLAG |
  2135. BNXT_VNIC_UCAST_FLAG;
  2136. rc = bnxt_alloc_vnic_attributes(bp);
  2137. if (rc)
  2138. goto alloc_mem_err;
  2139. return 0;
  2140. alloc_mem_err:
  2141. bnxt_free_mem(bp, true);
  2142. return rc;
  2143. }
  2144. void bnxt_hwrm_cmd_hdr_init(struct bnxt *bp, void *request, u16 req_type,
  2145. u16 cmpl_ring, u16 target_id)
  2146. {
  2147. struct input *req = request;
  2148. req->req_type = cpu_to_le16(req_type);
  2149. req->cmpl_ring = cpu_to_le16(cmpl_ring);
  2150. req->target_id = cpu_to_le16(target_id);
  2151. req->resp_addr = cpu_to_le64(bp->hwrm_cmd_resp_dma_addr);
  2152. }
  2153. static int bnxt_hwrm_do_send_msg(struct bnxt *bp, void *msg, u32 msg_len,
  2154. int timeout, bool silent)
  2155. {
  2156. int i, intr_process, rc;
  2157. struct input *req = msg;
  2158. u32 *data = msg;
  2159. __le32 *resp_len, *valid;
  2160. u16 cp_ring_id, len = 0;
  2161. struct hwrm_err_output *resp = bp->hwrm_cmd_resp_addr;
  2162. req->seq_id = cpu_to_le16(bp->hwrm_cmd_seq++);
  2163. memset(resp, 0, PAGE_SIZE);
  2164. cp_ring_id = le16_to_cpu(req->cmpl_ring);
  2165. intr_process = (cp_ring_id == INVALID_HW_RING_ID) ? 0 : 1;
  2166. /* Write request msg to hwrm channel */
  2167. __iowrite32_copy(bp->bar0, data, msg_len / 4);
  2168. for (i = msg_len; i < BNXT_HWRM_MAX_REQ_LEN; i += 4)
  2169. writel(0, bp->bar0 + i);
  2170. /* currently supports only one outstanding message */
  2171. if (intr_process)
  2172. bp->hwrm_intr_seq_id = le16_to_cpu(req->seq_id);
  2173. /* Ring channel doorbell */
  2174. writel(1, bp->bar0 + 0x100);
  2175. if (!timeout)
  2176. timeout = DFLT_HWRM_CMD_TIMEOUT;
  2177. i = 0;
  2178. if (intr_process) {
  2179. /* Wait until hwrm response cmpl interrupt is processed */
  2180. while (bp->hwrm_intr_seq_id != HWRM_SEQ_ID_INVALID &&
  2181. i++ < timeout) {
  2182. usleep_range(600, 800);
  2183. }
  2184. if (bp->hwrm_intr_seq_id != HWRM_SEQ_ID_INVALID) {
  2185. netdev_err(bp->dev, "Resp cmpl intr err msg: 0x%x\n",
  2186. le16_to_cpu(req->req_type));
  2187. return -1;
  2188. }
  2189. } else {
  2190. /* Check if response len is updated */
  2191. resp_len = bp->hwrm_cmd_resp_addr + HWRM_RESP_LEN_OFFSET;
  2192. for (i = 0; i < timeout; i++) {
  2193. len = (le32_to_cpu(*resp_len) & HWRM_RESP_LEN_MASK) >>
  2194. HWRM_RESP_LEN_SFT;
  2195. if (len)
  2196. break;
  2197. usleep_range(600, 800);
  2198. }
  2199. if (i >= timeout) {
  2200. netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d\n",
  2201. timeout, le16_to_cpu(req->req_type),
  2202. le16_to_cpu(req->seq_id), *resp_len);
  2203. return -1;
  2204. }
  2205. /* Last word of resp contains valid bit */
  2206. valid = bp->hwrm_cmd_resp_addr + len - 4;
  2207. for (i = 0; i < timeout; i++) {
  2208. if (le32_to_cpu(*valid) & HWRM_RESP_VALID_MASK)
  2209. break;
  2210. usleep_range(600, 800);
  2211. }
  2212. if (i >= timeout) {
  2213. netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d v:%d\n",
  2214. timeout, le16_to_cpu(req->req_type),
  2215. le16_to_cpu(req->seq_id), len, *valid);
  2216. return -1;
  2217. }
  2218. }
  2219. rc = le16_to_cpu(resp->error_code);
  2220. if (rc && !silent)
  2221. netdev_err(bp->dev, "hwrm req_type 0x%x seq id 0x%x error 0x%x\n",
  2222. le16_to_cpu(resp->req_type),
  2223. le16_to_cpu(resp->seq_id), rc);
  2224. return rc;
  2225. }
  2226. int _hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
  2227. {
  2228. return bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, false);
  2229. }
  2230. int hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
  2231. {
  2232. int rc;
  2233. mutex_lock(&bp->hwrm_cmd_lock);
  2234. rc = _hwrm_send_message(bp, msg, msg_len, timeout);
  2235. mutex_unlock(&bp->hwrm_cmd_lock);
  2236. return rc;
  2237. }
  2238. int hwrm_send_message_silent(struct bnxt *bp, void *msg, u32 msg_len,
  2239. int timeout)
  2240. {
  2241. int rc;
  2242. mutex_lock(&bp->hwrm_cmd_lock);
  2243. rc = bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, true);
  2244. mutex_unlock(&bp->hwrm_cmd_lock);
  2245. return rc;
  2246. }
  2247. static int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp)
  2248. {
  2249. struct hwrm_func_drv_rgtr_input req = {0};
  2250. int i;
  2251. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_RGTR, -1, -1);
  2252. req.enables =
  2253. cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
  2254. FUNC_DRV_RGTR_REQ_ENABLES_VER |
  2255. FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
  2256. /* TODO: current async event fwd bits are not defined and the firmware
  2257. * only checks if it is non-zero to enable async event forwarding
  2258. */
  2259. req.async_event_fwd[0] |= cpu_to_le32(1);
  2260. req.os_type = cpu_to_le16(1);
  2261. req.ver_maj = DRV_VER_MAJ;
  2262. req.ver_min = DRV_VER_MIN;
  2263. req.ver_upd = DRV_VER_UPD;
  2264. if (BNXT_PF(bp)) {
  2265. DECLARE_BITMAP(vf_req_snif_bmap, 256);
  2266. u32 *data = (u32 *)vf_req_snif_bmap;
  2267. memset(vf_req_snif_bmap, 0, sizeof(vf_req_snif_bmap));
  2268. for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++)
  2269. __set_bit(bnxt_vf_req_snif[i], vf_req_snif_bmap);
  2270. for (i = 0; i < 8; i++)
  2271. req.vf_req_fwd[i] = cpu_to_le32(data[i]);
  2272. req.enables |=
  2273. cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
  2274. }
  2275. return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  2276. }
  2277. static int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
  2278. {
  2279. struct hwrm_func_drv_unrgtr_input req = {0};
  2280. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_UNRGTR, -1, -1);
  2281. return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  2282. }
  2283. static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
  2284. {
  2285. u32 rc = 0;
  2286. struct hwrm_tunnel_dst_port_free_input req = {0};
  2287. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_FREE, -1, -1);
  2288. req.tunnel_type = tunnel_type;
  2289. switch (tunnel_type) {
  2290. case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
  2291. req.tunnel_dst_port_id = bp->vxlan_fw_dst_port_id;
  2292. break;
  2293. case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
  2294. req.tunnel_dst_port_id = bp->nge_fw_dst_port_id;
  2295. break;
  2296. default:
  2297. break;
  2298. }
  2299. rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  2300. if (rc)
  2301. netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
  2302. rc);
  2303. return rc;
  2304. }
  2305. static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
  2306. u8 tunnel_type)
  2307. {
  2308. u32 rc = 0;
  2309. struct hwrm_tunnel_dst_port_alloc_input req = {0};
  2310. struct hwrm_tunnel_dst_port_alloc_output *resp = bp->hwrm_cmd_resp_addr;
  2311. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_ALLOC, -1, -1);
  2312. req.tunnel_type = tunnel_type;
  2313. req.tunnel_dst_port_val = port;
  2314. mutex_lock(&bp->hwrm_cmd_lock);
  2315. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  2316. if (rc) {
  2317. netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
  2318. rc);
  2319. goto err_out;
  2320. }
  2321. if (tunnel_type & TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN)
  2322. bp->vxlan_fw_dst_port_id = resp->tunnel_dst_port_id;
  2323. else if (tunnel_type & TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE)
  2324. bp->nge_fw_dst_port_id = resp->tunnel_dst_port_id;
  2325. err_out:
  2326. mutex_unlock(&bp->hwrm_cmd_lock);
  2327. return rc;
  2328. }
  2329. static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
  2330. {
  2331. struct hwrm_cfa_l2_set_rx_mask_input req = {0};
  2332. struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
  2333. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_SET_RX_MASK, -1, -1);
  2334. req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
  2335. req.num_mc_entries = cpu_to_le32(vnic->mc_list_count);
  2336. req.mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
  2337. req.mask = cpu_to_le32(vnic->rx_mask);
  2338. return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  2339. }
  2340. #ifdef CONFIG_RFS_ACCEL
  2341. static int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
  2342. struct bnxt_ntuple_filter *fltr)
  2343. {
  2344. struct hwrm_cfa_ntuple_filter_free_input req = {0};
  2345. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_FREE, -1, -1);
  2346. req.ntuple_filter_id = fltr->filter_id;
  2347. return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  2348. }
  2349. #define BNXT_NTP_FLTR_FLAGS \
  2350. (CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID | \
  2351. CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE | \
  2352. CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_MACADDR | \
  2353. CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE | \
  2354. CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR | \
  2355. CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK | \
  2356. CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR | \
  2357. CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK | \
  2358. CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL | \
  2359. CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT | \
  2360. CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK | \
  2361. CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT | \
  2362. CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK | \
  2363. CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
  2364. static int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
  2365. struct bnxt_ntuple_filter *fltr)
  2366. {
  2367. int rc = 0;
  2368. struct hwrm_cfa_ntuple_filter_alloc_input req = {0};
  2369. struct hwrm_cfa_ntuple_filter_alloc_output *resp =
  2370. bp->hwrm_cmd_resp_addr;
  2371. struct flow_keys *keys = &fltr->fkeys;
  2372. struct bnxt_vnic_info *vnic = &bp->vnic_info[fltr->rxq + 1];
  2373. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_ALLOC, -1, -1);
  2374. req.l2_filter_id = bp->vnic_info[0].fw_l2_filter_id[0];
  2375. req.enables = cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
  2376. req.ethertype = htons(ETH_P_IP);
  2377. memcpy(req.src_macaddr, fltr->src_mac_addr, ETH_ALEN);
  2378. req.ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
  2379. req.ip_protocol = keys->basic.ip_proto;
  2380. req.src_ipaddr[0] = keys->addrs.v4addrs.src;
  2381. req.src_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
  2382. req.dst_ipaddr[0] = keys->addrs.v4addrs.dst;
  2383. req.dst_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
  2384. req.src_port = keys->ports.src;
  2385. req.src_port_mask = cpu_to_be16(0xffff);
  2386. req.dst_port = keys->ports.dst;
  2387. req.dst_port_mask = cpu_to_be16(0xffff);
  2388. req.dst_id = cpu_to_le16(vnic->fw_vnic_id);
  2389. mutex_lock(&bp->hwrm_cmd_lock);
  2390. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  2391. if (!rc)
  2392. fltr->filter_id = resp->ntuple_filter_id;
  2393. mutex_unlock(&bp->hwrm_cmd_lock);
  2394. return rc;
  2395. }
  2396. #endif
  2397. static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
  2398. u8 *mac_addr)
  2399. {
  2400. u32 rc = 0;
  2401. struct hwrm_cfa_l2_filter_alloc_input req = {0};
  2402. struct hwrm_cfa_l2_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
  2403. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_ALLOC, -1, -1);
  2404. req.flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX |
  2405. CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
  2406. req.dst_id = cpu_to_le16(bp->vnic_info[vnic_id].fw_vnic_id);
  2407. req.enables =
  2408. cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
  2409. CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
  2410. CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
  2411. memcpy(req.l2_addr, mac_addr, ETH_ALEN);
  2412. req.l2_addr_mask[0] = 0xff;
  2413. req.l2_addr_mask[1] = 0xff;
  2414. req.l2_addr_mask[2] = 0xff;
  2415. req.l2_addr_mask[3] = 0xff;
  2416. req.l2_addr_mask[4] = 0xff;
  2417. req.l2_addr_mask[5] = 0xff;
  2418. mutex_lock(&bp->hwrm_cmd_lock);
  2419. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  2420. if (!rc)
  2421. bp->vnic_info[vnic_id].fw_l2_filter_id[idx] =
  2422. resp->l2_filter_id;
  2423. mutex_unlock(&bp->hwrm_cmd_lock);
  2424. return rc;
  2425. }
  2426. static int bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
  2427. {
  2428. u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
  2429. int rc = 0;
  2430. /* Any associated ntuple filters will also be cleared by firmware. */
  2431. mutex_lock(&bp->hwrm_cmd_lock);
  2432. for (i = 0; i < num_of_vnics; i++) {
  2433. struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
  2434. for (j = 0; j < vnic->uc_filter_count; j++) {
  2435. struct hwrm_cfa_l2_filter_free_input req = {0};
  2436. bnxt_hwrm_cmd_hdr_init(bp, &req,
  2437. HWRM_CFA_L2_FILTER_FREE, -1, -1);
  2438. req.l2_filter_id = vnic->fw_l2_filter_id[j];
  2439. rc = _hwrm_send_message(bp, &req, sizeof(req),
  2440. HWRM_CMD_TIMEOUT);
  2441. }
  2442. vnic->uc_filter_count = 0;
  2443. }
  2444. mutex_unlock(&bp->hwrm_cmd_lock);
  2445. return rc;
  2446. }
  2447. static int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, u16 vnic_id, u32 tpa_flags)
  2448. {
  2449. struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
  2450. struct hwrm_vnic_tpa_cfg_input req = {0};
  2451. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_TPA_CFG, -1, -1);
  2452. if (tpa_flags) {
  2453. u16 mss = bp->dev->mtu - 40;
  2454. u32 nsegs, n, segs = 0, flags;
  2455. flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
  2456. VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
  2457. VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
  2458. VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
  2459. VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
  2460. if (tpa_flags & BNXT_FLAG_GRO)
  2461. flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
  2462. req.flags = cpu_to_le32(flags);
  2463. req.enables =
  2464. cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
  2465. VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
  2466. VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
  2467. /* Number of segs are log2 units, and first packet is not
  2468. * included as part of this units.
  2469. */
  2470. if (mss <= PAGE_SIZE) {
  2471. n = PAGE_SIZE / mss;
  2472. nsegs = (MAX_SKB_FRAGS - 1) * n;
  2473. } else {
  2474. n = mss / PAGE_SIZE;
  2475. if (mss & (PAGE_SIZE - 1))
  2476. n++;
  2477. nsegs = (MAX_SKB_FRAGS - n) / n;
  2478. }
  2479. segs = ilog2(nsegs);
  2480. req.max_agg_segs = cpu_to_le16(segs);
  2481. req.max_aggs = cpu_to_le16(VNIC_TPA_CFG_REQ_MAX_AGGS_MAX);
  2482. req.min_agg_len = cpu_to_le32(512);
  2483. }
  2484. req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
  2485. return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  2486. }
  2487. static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, u16 vnic_id, bool set_rss)
  2488. {
  2489. u32 i, j, max_rings;
  2490. struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
  2491. struct hwrm_vnic_rss_cfg_input req = {0};
  2492. if (vnic->fw_rss_cos_lb_ctx == INVALID_HW_RING_ID)
  2493. return 0;
  2494. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_CFG, -1, -1);
  2495. if (set_rss) {
  2496. vnic->hash_type = BNXT_RSS_HASH_TYPE_FLAG_IPV4 |
  2497. BNXT_RSS_HASH_TYPE_FLAG_TCP_IPV4 |
  2498. BNXT_RSS_HASH_TYPE_FLAG_IPV6 |
  2499. BNXT_RSS_HASH_TYPE_FLAG_TCP_IPV6;
  2500. req.hash_type = cpu_to_le32(vnic->hash_type);
  2501. if (vnic->flags & BNXT_VNIC_RSS_FLAG)
  2502. max_rings = bp->rx_nr_rings;
  2503. else
  2504. max_rings = 1;
  2505. /* Fill the RSS indirection table with ring group ids */
  2506. for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++, j++) {
  2507. if (j == max_rings)
  2508. j = 0;
  2509. vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
  2510. }
  2511. req.ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
  2512. req.hash_key_tbl_addr =
  2513. cpu_to_le64(vnic->rss_hash_key_dma_addr);
  2514. }
  2515. req.rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx);
  2516. return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  2517. }
  2518. static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, u16 vnic_id)
  2519. {
  2520. struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
  2521. struct hwrm_vnic_plcmodes_cfg_input req = {0};
  2522. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_PLCMODES_CFG, -1, -1);
  2523. req.flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT |
  2524. VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
  2525. VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
  2526. req.enables =
  2527. cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID |
  2528. VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
  2529. /* thresholds not implemented in firmware yet */
  2530. req.jumbo_thresh = cpu_to_le16(bp->rx_copy_thresh);
  2531. req.hds_threshold = cpu_to_le16(bp->rx_copy_thresh);
  2532. req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
  2533. return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  2534. }
  2535. static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp, u16 vnic_id)
  2536. {
  2537. struct hwrm_vnic_rss_cos_lb_ctx_free_input req = {0};
  2538. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_FREE, -1, -1);
  2539. req.rss_cos_lb_ctx_id =
  2540. cpu_to_le16(bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx);
  2541. hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  2542. bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx = INVALID_HW_RING_ID;
  2543. }
  2544. static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
  2545. {
  2546. int i;
  2547. for (i = 0; i < bp->nr_vnics; i++) {
  2548. struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
  2549. if (vnic->fw_rss_cos_lb_ctx != INVALID_HW_RING_ID)
  2550. bnxt_hwrm_vnic_ctx_free_one(bp, i);
  2551. }
  2552. bp->rsscos_nr_ctxs = 0;
  2553. }
  2554. static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp, u16 vnic_id)
  2555. {
  2556. int rc;
  2557. struct hwrm_vnic_rss_cos_lb_ctx_alloc_input req = {0};
  2558. struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp =
  2559. bp->hwrm_cmd_resp_addr;
  2560. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC, -1,
  2561. -1);
  2562. mutex_lock(&bp->hwrm_cmd_lock);
  2563. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  2564. if (!rc)
  2565. bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx =
  2566. le16_to_cpu(resp->rss_cos_lb_ctx_id);
  2567. mutex_unlock(&bp->hwrm_cmd_lock);
  2568. return rc;
  2569. }
  2570. static int bnxt_hwrm_vnic_cfg(struct bnxt *bp, u16 vnic_id)
  2571. {
  2572. unsigned int ring = 0, grp_idx;
  2573. struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
  2574. struct hwrm_vnic_cfg_input req = {0};
  2575. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_CFG, -1, -1);
  2576. /* Only RSS support for now TBD: COS & LB */
  2577. req.enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP |
  2578. VNIC_CFG_REQ_ENABLES_RSS_RULE);
  2579. req.rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx);
  2580. req.cos_rule = cpu_to_le16(0xffff);
  2581. if (vnic->flags & BNXT_VNIC_RSS_FLAG)
  2582. ring = 0;
  2583. else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
  2584. ring = vnic_id - 1;
  2585. grp_idx = bp->rx_ring[ring].bnapi->index;
  2586. req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
  2587. req.dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
  2588. req.lb_rule = cpu_to_le16(0xffff);
  2589. req.mru = cpu_to_le16(bp->dev->mtu + ETH_HLEN + ETH_FCS_LEN +
  2590. VLAN_HLEN);
  2591. if (bp->flags & BNXT_FLAG_STRIP_VLAN)
  2592. req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
  2593. return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  2594. }
  2595. static int bnxt_hwrm_vnic_free_one(struct bnxt *bp, u16 vnic_id)
  2596. {
  2597. u32 rc = 0;
  2598. if (bp->vnic_info[vnic_id].fw_vnic_id != INVALID_HW_RING_ID) {
  2599. struct hwrm_vnic_free_input req = {0};
  2600. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_FREE, -1, -1);
  2601. req.vnic_id =
  2602. cpu_to_le32(bp->vnic_info[vnic_id].fw_vnic_id);
  2603. rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  2604. if (rc)
  2605. return rc;
  2606. bp->vnic_info[vnic_id].fw_vnic_id = INVALID_HW_RING_ID;
  2607. }
  2608. return rc;
  2609. }
  2610. static void bnxt_hwrm_vnic_free(struct bnxt *bp)
  2611. {
  2612. u16 i;
  2613. for (i = 0; i < bp->nr_vnics; i++)
  2614. bnxt_hwrm_vnic_free_one(bp, i);
  2615. }
  2616. static int bnxt_hwrm_vnic_alloc(struct bnxt *bp, u16 vnic_id,
  2617. unsigned int start_rx_ring_idx,
  2618. unsigned int nr_rings)
  2619. {
  2620. int rc = 0;
  2621. unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
  2622. struct hwrm_vnic_alloc_input req = {0};
  2623. struct hwrm_vnic_alloc_output *resp = bp->hwrm_cmd_resp_addr;
  2624. /* map ring groups to this vnic */
  2625. for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
  2626. grp_idx = bp->rx_ring[i].bnapi->index;
  2627. if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
  2628. netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
  2629. j, nr_rings);
  2630. break;
  2631. }
  2632. bp->vnic_info[vnic_id].fw_grp_ids[j] =
  2633. bp->grp_info[grp_idx].fw_grp_id;
  2634. }
  2635. bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx = INVALID_HW_RING_ID;
  2636. if (vnic_id == 0)
  2637. req.flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
  2638. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_ALLOC, -1, -1);
  2639. mutex_lock(&bp->hwrm_cmd_lock);
  2640. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  2641. if (!rc)
  2642. bp->vnic_info[vnic_id].fw_vnic_id = le32_to_cpu(resp->vnic_id);
  2643. mutex_unlock(&bp->hwrm_cmd_lock);
  2644. return rc;
  2645. }
  2646. static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
  2647. {
  2648. u16 i;
  2649. u32 rc = 0;
  2650. mutex_lock(&bp->hwrm_cmd_lock);
  2651. for (i = 0; i < bp->rx_nr_rings; i++) {
  2652. struct hwrm_ring_grp_alloc_input req = {0};
  2653. struct hwrm_ring_grp_alloc_output *resp =
  2654. bp->hwrm_cmd_resp_addr;
  2655. unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
  2656. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_ALLOC, -1, -1);
  2657. req.cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
  2658. req.rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
  2659. req.ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
  2660. req.sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
  2661. rc = _hwrm_send_message(bp, &req, sizeof(req),
  2662. HWRM_CMD_TIMEOUT);
  2663. if (rc)
  2664. break;
  2665. bp->grp_info[grp_idx].fw_grp_id =
  2666. le32_to_cpu(resp->ring_group_id);
  2667. }
  2668. mutex_unlock(&bp->hwrm_cmd_lock);
  2669. return rc;
  2670. }
  2671. static int bnxt_hwrm_ring_grp_free(struct bnxt *bp)
  2672. {
  2673. u16 i;
  2674. u32 rc = 0;
  2675. struct hwrm_ring_grp_free_input req = {0};
  2676. if (!bp->grp_info)
  2677. return 0;
  2678. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_FREE, -1, -1);
  2679. mutex_lock(&bp->hwrm_cmd_lock);
  2680. for (i = 0; i < bp->cp_nr_rings; i++) {
  2681. if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
  2682. continue;
  2683. req.ring_group_id =
  2684. cpu_to_le32(bp->grp_info[i].fw_grp_id);
  2685. rc = _hwrm_send_message(bp, &req, sizeof(req),
  2686. HWRM_CMD_TIMEOUT);
  2687. if (rc)
  2688. break;
  2689. bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
  2690. }
  2691. mutex_unlock(&bp->hwrm_cmd_lock);
  2692. return rc;
  2693. }
  2694. static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
  2695. struct bnxt_ring_struct *ring,
  2696. u32 ring_type, u32 map_index,
  2697. u32 stats_ctx_id)
  2698. {
  2699. int rc = 0, err = 0;
  2700. struct hwrm_ring_alloc_input req = {0};
  2701. struct hwrm_ring_alloc_output *resp = bp->hwrm_cmd_resp_addr;
  2702. u16 ring_id;
  2703. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_ALLOC, -1, -1);
  2704. req.enables = 0;
  2705. if (ring->nr_pages > 1) {
  2706. req.page_tbl_addr = cpu_to_le64(ring->pg_tbl_map);
  2707. /* Page size is in log2 units */
  2708. req.page_size = BNXT_PAGE_SHIFT;
  2709. req.page_tbl_depth = 1;
  2710. } else {
  2711. req.page_tbl_addr = cpu_to_le64(ring->dma_arr[0]);
  2712. }
  2713. req.fbo = 0;
  2714. /* Association of ring index with doorbell index and MSIX number */
  2715. req.logical_id = cpu_to_le16(map_index);
  2716. switch (ring_type) {
  2717. case HWRM_RING_ALLOC_TX:
  2718. req.ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
  2719. /* Association of transmit ring with completion ring */
  2720. req.cmpl_ring_id =
  2721. cpu_to_le16(bp->grp_info[map_index].cp_fw_ring_id);
  2722. req.length = cpu_to_le32(bp->tx_ring_mask + 1);
  2723. req.stat_ctx_id = cpu_to_le32(stats_ctx_id);
  2724. req.queue_id = cpu_to_le16(ring->queue_id);
  2725. break;
  2726. case HWRM_RING_ALLOC_RX:
  2727. req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
  2728. req.length = cpu_to_le32(bp->rx_ring_mask + 1);
  2729. break;
  2730. case HWRM_RING_ALLOC_AGG:
  2731. req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
  2732. req.length = cpu_to_le32(bp->rx_agg_ring_mask + 1);
  2733. break;
  2734. case HWRM_RING_ALLOC_CMPL:
  2735. req.ring_type = RING_ALLOC_REQ_RING_TYPE_CMPL;
  2736. req.length = cpu_to_le32(bp->cp_ring_mask + 1);
  2737. if (bp->flags & BNXT_FLAG_USING_MSIX)
  2738. req.int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
  2739. break;
  2740. default:
  2741. netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
  2742. ring_type);
  2743. return -1;
  2744. }
  2745. mutex_lock(&bp->hwrm_cmd_lock);
  2746. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  2747. err = le16_to_cpu(resp->error_code);
  2748. ring_id = le16_to_cpu(resp->ring_id);
  2749. mutex_unlock(&bp->hwrm_cmd_lock);
  2750. if (rc || err) {
  2751. switch (ring_type) {
  2752. case RING_FREE_REQ_RING_TYPE_CMPL:
  2753. netdev_err(bp->dev, "hwrm_ring_alloc cp failed. rc:%x err:%x\n",
  2754. rc, err);
  2755. return -1;
  2756. case RING_FREE_REQ_RING_TYPE_RX:
  2757. netdev_err(bp->dev, "hwrm_ring_alloc rx failed. rc:%x err:%x\n",
  2758. rc, err);
  2759. return -1;
  2760. case RING_FREE_REQ_RING_TYPE_TX:
  2761. netdev_err(bp->dev, "hwrm_ring_alloc tx failed. rc:%x err:%x\n",
  2762. rc, err);
  2763. return -1;
  2764. default:
  2765. netdev_err(bp->dev, "Invalid ring\n");
  2766. return -1;
  2767. }
  2768. }
  2769. ring->fw_ring_id = ring_id;
  2770. return rc;
  2771. }
  2772. static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
  2773. {
  2774. int i, rc = 0;
  2775. for (i = 0; i < bp->cp_nr_rings; i++) {
  2776. struct bnxt_napi *bnapi = bp->bnapi[i];
  2777. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  2778. struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
  2779. cpr->cp_doorbell = bp->bar1 + i * 0x80;
  2780. rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_CMPL, i,
  2781. INVALID_STATS_CTX_ID);
  2782. if (rc)
  2783. goto err_out;
  2784. BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
  2785. bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
  2786. }
  2787. for (i = 0; i < bp->tx_nr_rings; i++) {
  2788. struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
  2789. struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
  2790. u32 map_idx = txr->bnapi->index;
  2791. u16 fw_stats_ctx = bp->grp_info[map_idx].fw_stats_ctx;
  2792. rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_TX,
  2793. map_idx, fw_stats_ctx);
  2794. if (rc)
  2795. goto err_out;
  2796. txr->tx_doorbell = bp->bar1 + map_idx * 0x80;
  2797. }
  2798. for (i = 0; i < bp->rx_nr_rings; i++) {
  2799. struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
  2800. struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
  2801. u32 map_idx = rxr->bnapi->index;
  2802. rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_RX,
  2803. map_idx, INVALID_STATS_CTX_ID);
  2804. if (rc)
  2805. goto err_out;
  2806. rxr->rx_doorbell = bp->bar1 + map_idx * 0x80;
  2807. writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
  2808. bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
  2809. }
  2810. if (bp->flags & BNXT_FLAG_AGG_RINGS) {
  2811. for (i = 0; i < bp->rx_nr_rings; i++) {
  2812. struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
  2813. struct bnxt_ring_struct *ring =
  2814. &rxr->rx_agg_ring_struct;
  2815. u32 grp_idx = rxr->bnapi->index;
  2816. u32 map_idx = grp_idx + bp->rx_nr_rings;
  2817. rc = hwrm_ring_alloc_send_msg(bp, ring,
  2818. HWRM_RING_ALLOC_AGG,
  2819. map_idx,
  2820. INVALID_STATS_CTX_ID);
  2821. if (rc)
  2822. goto err_out;
  2823. rxr->rx_agg_doorbell = bp->bar1 + map_idx * 0x80;
  2824. writel(DB_KEY_RX | rxr->rx_agg_prod,
  2825. rxr->rx_agg_doorbell);
  2826. bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
  2827. }
  2828. }
  2829. err_out:
  2830. return rc;
  2831. }
  2832. static int hwrm_ring_free_send_msg(struct bnxt *bp,
  2833. struct bnxt_ring_struct *ring,
  2834. u32 ring_type, int cmpl_ring_id)
  2835. {
  2836. int rc;
  2837. struct hwrm_ring_free_input req = {0};
  2838. struct hwrm_ring_free_output *resp = bp->hwrm_cmd_resp_addr;
  2839. u16 error_code;
  2840. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_FREE, cmpl_ring_id, -1);
  2841. req.ring_type = ring_type;
  2842. req.ring_id = cpu_to_le16(ring->fw_ring_id);
  2843. mutex_lock(&bp->hwrm_cmd_lock);
  2844. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  2845. error_code = le16_to_cpu(resp->error_code);
  2846. mutex_unlock(&bp->hwrm_cmd_lock);
  2847. if (rc || error_code) {
  2848. switch (ring_type) {
  2849. case RING_FREE_REQ_RING_TYPE_CMPL:
  2850. netdev_err(bp->dev, "hwrm_ring_free cp failed. rc:%d\n",
  2851. rc);
  2852. return rc;
  2853. case RING_FREE_REQ_RING_TYPE_RX:
  2854. netdev_err(bp->dev, "hwrm_ring_free rx failed. rc:%d\n",
  2855. rc);
  2856. return rc;
  2857. case RING_FREE_REQ_RING_TYPE_TX:
  2858. netdev_err(bp->dev, "hwrm_ring_free tx failed. rc:%d\n",
  2859. rc);
  2860. return rc;
  2861. default:
  2862. netdev_err(bp->dev, "Invalid ring\n");
  2863. return -1;
  2864. }
  2865. }
  2866. return 0;
  2867. }
  2868. static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
  2869. {
  2870. int i;
  2871. if (!bp->bnapi)
  2872. return;
  2873. for (i = 0; i < bp->tx_nr_rings; i++) {
  2874. struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
  2875. struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
  2876. u32 grp_idx = txr->bnapi->index;
  2877. u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
  2878. if (ring->fw_ring_id != INVALID_HW_RING_ID) {
  2879. hwrm_ring_free_send_msg(bp, ring,
  2880. RING_FREE_REQ_RING_TYPE_TX,
  2881. close_path ? cmpl_ring_id :
  2882. INVALID_HW_RING_ID);
  2883. ring->fw_ring_id = INVALID_HW_RING_ID;
  2884. }
  2885. }
  2886. for (i = 0; i < bp->rx_nr_rings; i++) {
  2887. struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
  2888. struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
  2889. u32 grp_idx = rxr->bnapi->index;
  2890. u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
  2891. if (ring->fw_ring_id != INVALID_HW_RING_ID) {
  2892. hwrm_ring_free_send_msg(bp, ring,
  2893. RING_FREE_REQ_RING_TYPE_RX,
  2894. close_path ? cmpl_ring_id :
  2895. INVALID_HW_RING_ID);
  2896. ring->fw_ring_id = INVALID_HW_RING_ID;
  2897. bp->grp_info[grp_idx].rx_fw_ring_id =
  2898. INVALID_HW_RING_ID;
  2899. }
  2900. }
  2901. for (i = 0; i < bp->rx_nr_rings; i++) {
  2902. struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
  2903. struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
  2904. u32 grp_idx = rxr->bnapi->index;
  2905. u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
  2906. if (ring->fw_ring_id != INVALID_HW_RING_ID) {
  2907. hwrm_ring_free_send_msg(bp, ring,
  2908. RING_FREE_REQ_RING_TYPE_RX,
  2909. close_path ? cmpl_ring_id :
  2910. INVALID_HW_RING_ID);
  2911. ring->fw_ring_id = INVALID_HW_RING_ID;
  2912. bp->grp_info[grp_idx].agg_fw_ring_id =
  2913. INVALID_HW_RING_ID;
  2914. }
  2915. }
  2916. for (i = 0; i < bp->cp_nr_rings; i++) {
  2917. struct bnxt_napi *bnapi = bp->bnapi[i];
  2918. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  2919. struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
  2920. if (ring->fw_ring_id != INVALID_HW_RING_ID) {
  2921. hwrm_ring_free_send_msg(bp, ring,
  2922. RING_FREE_REQ_RING_TYPE_CMPL,
  2923. INVALID_HW_RING_ID);
  2924. ring->fw_ring_id = INVALID_HW_RING_ID;
  2925. bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
  2926. }
  2927. }
  2928. }
  2929. static void bnxt_hwrm_set_coal_params(struct bnxt *bp, u32 max_bufs,
  2930. u32 buf_tmrs, u16 flags,
  2931. struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
  2932. {
  2933. req->flags = cpu_to_le16(flags);
  2934. req->num_cmpl_dma_aggr = cpu_to_le16((u16)max_bufs);
  2935. req->num_cmpl_dma_aggr_during_int = cpu_to_le16(max_bufs >> 16);
  2936. req->cmpl_aggr_dma_tmr = cpu_to_le16((u16)buf_tmrs);
  2937. req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(buf_tmrs >> 16);
  2938. /* Minimum time between 2 interrupts set to buf_tmr x 2 */
  2939. req->int_lat_tmr_min = cpu_to_le16((u16)buf_tmrs * 2);
  2940. req->int_lat_tmr_max = cpu_to_le16((u16)buf_tmrs * 4);
  2941. req->num_cmpl_aggr_int = cpu_to_le16((u16)max_bufs * 4);
  2942. }
  2943. int bnxt_hwrm_set_coal(struct bnxt *bp)
  2944. {
  2945. int i, rc = 0;
  2946. struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req_rx = {0},
  2947. req_tx = {0}, *req;
  2948. u16 max_buf, max_buf_irq;
  2949. u16 buf_tmr, buf_tmr_irq;
  2950. u32 flags;
  2951. bnxt_hwrm_cmd_hdr_init(bp, &req_rx,
  2952. HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
  2953. bnxt_hwrm_cmd_hdr_init(bp, &req_tx,
  2954. HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
  2955. /* Each rx completion (2 records) should be DMAed immediately.
  2956. * DMA 1/4 of the completion buffers at a time.
  2957. */
  2958. max_buf = min_t(u16, bp->rx_coal_bufs / 4, 2);
  2959. /* max_buf must not be zero */
  2960. max_buf = clamp_t(u16, max_buf, 1, 63);
  2961. max_buf_irq = clamp_t(u16, bp->rx_coal_bufs_irq, 1, 63);
  2962. buf_tmr = BNXT_USEC_TO_COAL_TIMER(bp->rx_coal_ticks);
  2963. /* buf timer set to 1/4 of interrupt timer */
  2964. buf_tmr = max_t(u16, buf_tmr / 4, 1);
  2965. buf_tmr_irq = BNXT_USEC_TO_COAL_TIMER(bp->rx_coal_ticks_irq);
  2966. buf_tmr_irq = max_t(u16, buf_tmr_irq, 1);
  2967. flags = RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
  2968. /* RING_IDLE generates more IRQs for lower latency. Enable it only
  2969. * if coal_ticks is less than 25 us.
  2970. */
  2971. if (bp->rx_coal_ticks < 25)
  2972. flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
  2973. bnxt_hwrm_set_coal_params(bp, max_buf_irq << 16 | max_buf,
  2974. buf_tmr_irq << 16 | buf_tmr, flags, &req_rx);
  2975. /* max_buf must not be zero */
  2976. max_buf = clamp_t(u16, bp->tx_coal_bufs, 1, 63);
  2977. max_buf_irq = clamp_t(u16, bp->tx_coal_bufs_irq, 1, 63);
  2978. buf_tmr = BNXT_USEC_TO_COAL_TIMER(bp->tx_coal_ticks);
  2979. /* buf timer set to 1/4 of interrupt timer */
  2980. buf_tmr = max_t(u16, buf_tmr / 4, 1);
  2981. buf_tmr_irq = BNXT_USEC_TO_COAL_TIMER(bp->tx_coal_ticks_irq);
  2982. buf_tmr_irq = max_t(u16, buf_tmr_irq, 1);
  2983. flags = RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
  2984. bnxt_hwrm_set_coal_params(bp, max_buf_irq << 16 | max_buf,
  2985. buf_tmr_irq << 16 | buf_tmr, flags, &req_tx);
  2986. mutex_lock(&bp->hwrm_cmd_lock);
  2987. for (i = 0; i < bp->cp_nr_rings; i++) {
  2988. struct bnxt_napi *bnapi = bp->bnapi[i];
  2989. req = &req_rx;
  2990. if (!bnapi->rx_ring)
  2991. req = &req_tx;
  2992. req->ring_id = cpu_to_le16(bp->grp_info[i].cp_fw_ring_id);
  2993. rc = _hwrm_send_message(bp, req, sizeof(*req),
  2994. HWRM_CMD_TIMEOUT);
  2995. if (rc)
  2996. break;
  2997. }
  2998. mutex_unlock(&bp->hwrm_cmd_lock);
  2999. return rc;
  3000. }
  3001. static int bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
  3002. {
  3003. int rc = 0, i;
  3004. struct hwrm_stat_ctx_free_input req = {0};
  3005. if (!bp->bnapi)
  3006. return 0;
  3007. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_FREE, -1, -1);
  3008. mutex_lock(&bp->hwrm_cmd_lock);
  3009. for (i = 0; i < bp->cp_nr_rings; i++) {
  3010. struct bnxt_napi *bnapi = bp->bnapi[i];
  3011. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  3012. if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
  3013. req.stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
  3014. rc = _hwrm_send_message(bp, &req, sizeof(req),
  3015. HWRM_CMD_TIMEOUT);
  3016. if (rc)
  3017. break;
  3018. cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
  3019. }
  3020. }
  3021. mutex_unlock(&bp->hwrm_cmd_lock);
  3022. return rc;
  3023. }
  3024. static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
  3025. {
  3026. int rc = 0, i;
  3027. struct hwrm_stat_ctx_alloc_input req = {0};
  3028. struct hwrm_stat_ctx_alloc_output *resp = bp->hwrm_cmd_resp_addr;
  3029. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_ALLOC, -1, -1);
  3030. req.update_period_ms = cpu_to_le32(1000);
  3031. mutex_lock(&bp->hwrm_cmd_lock);
  3032. for (i = 0; i < bp->cp_nr_rings; i++) {
  3033. struct bnxt_napi *bnapi = bp->bnapi[i];
  3034. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  3035. req.stats_dma_addr = cpu_to_le64(cpr->hw_stats_map);
  3036. rc = _hwrm_send_message(bp, &req, sizeof(req),
  3037. HWRM_CMD_TIMEOUT);
  3038. if (rc)
  3039. break;
  3040. cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
  3041. bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
  3042. }
  3043. mutex_unlock(&bp->hwrm_cmd_lock);
  3044. return 0;
  3045. }
  3046. int bnxt_hwrm_func_qcaps(struct bnxt *bp)
  3047. {
  3048. int rc = 0;
  3049. struct hwrm_func_qcaps_input req = {0};
  3050. struct hwrm_func_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
  3051. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCAPS, -1, -1);
  3052. req.fid = cpu_to_le16(0xffff);
  3053. mutex_lock(&bp->hwrm_cmd_lock);
  3054. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3055. if (rc)
  3056. goto hwrm_func_qcaps_exit;
  3057. if (BNXT_PF(bp)) {
  3058. struct bnxt_pf_info *pf = &bp->pf;
  3059. pf->fw_fid = le16_to_cpu(resp->fid);
  3060. pf->port_id = le16_to_cpu(resp->port_id);
  3061. memcpy(pf->mac_addr, resp->perm_mac_address, ETH_ALEN);
  3062. memcpy(bp->dev->dev_addr, pf->mac_addr, ETH_ALEN);
  3063. pf->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
  3064. pf->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
  3065. pf->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
  3066. pf->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
  3067. pf->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
  3068. if (!pf->max_hw_ring_grps)
  3069. pf->max_hw_ring_grps = pf->max_tx_rings;
  3070. pf->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
  3071. pf->max_vnics = le16_to_cpu(resp->max_vnics);
  3072. pf->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
  3073. pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
  3074. pf->max_vfs = le16_to_cpu(resp->max_vfs);
  3075. pf->max_encap_records = le32_to_cpu(resp->max_encap_records);
  3076. pf->max_decap_records = le32_to_cpu(resp->max_decap_records);
  3077. pf->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
  3078. pf->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
  3079. pf->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
  3080. pf->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
  3081. } else {
  3082. #ifdef CONFIG_BNXT_SRIOV
  3083. struct bnxt_vf_info *vf = &bp->vf;
  3084. vf->fw_fid = le16_to_cpu(resp->fid);
  3085. memcpy(vf->mac_addr, resp->perm_mac_address, ETH_ALEN);
  3086. if (is_valid_ether_addr(vf->mac_addr))
  3087. /* overwrite netdev dev_adr with admin VF MAC */
  3088. memcpy(bp->dev->dev_addr, vf->mac_addr, ETH_ALEN);
  3089. else
  3090. random_ether_addr(bp->dev->dev_addr);
  3091. vf->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
  3092. vf->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
  3093. vf->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
  3094. vf->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
  3095. vf->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
  3096. if (!vf->max_hw_ring_grps)
  3097. vf->max_hw_ring_grps = vf->max_tx_rings;
  3098. vf->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
  3099. vf->max_vnics = le16_to_cpu(resp->max_vnics);
  3100. vf->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
  3101. #endif
  3102. }
  3103. bp->tx_push_thresh = 0;
  3104. if (resp->flags &
  3105. cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED))
  3106. bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
  3107. hwrm_func_qcaps_exit:
  3108. mutex_unlock(&bp->hwrm_cmd_lock);
  3109. return rc;
  3110. }
  3111. static int bnxt_hwrm_func_reset(struct bnxt *bp)
  3112. {
  3113. struct hwrm_func_reset_input req = {0};
  3114. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_RESET, -1, -1);
  3115. req.enables = 0;
  3116. return hwrm_send_message(bp, &req, sizeof(req), HWRM_RESET_TIMEOUT);
  3117. }
  3118. static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
  3119. {
  3120. int rc = 0;
  3121. struct hwrm_queue_qportcfg_input req = {0};
  3122. struct hwrm_queue_qportcfg_output *resp = bp->hwrm_cmd_resp_addr;
  3123. u8 i, *qptr;
  3124. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_QUEUE_QPORTCFG, -1, -1);
  3125. mutex_lock(&bp->hwrm_cmd_lock);
  3126. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3127. if (rc)
  3128. goto qportcfg_exit;
  3129. if (!resp->max_configurable_queues) {
  3130. rc = -EINVAL;
  3131. goto qportcfg_exit;
  3132. }
  3133. bp->max_tc = resp->max_configurable_queues;
  3134. if (bp->max_tc > BNXT_MAX_QUEUE)
  3135. bp->max_tc = BNXT_MAX_QUEUE;
  3136. qptr = &resp->queue_id0;
  3137. for (i = 0; i < bp->max_tc; i++) {
  3138. bp->q_info[i].queue_id = *qptr++;
  3139. bp->q_info[i].queue_profile = *qptr++;
  3140. }
  3141. qportcfg_exit:
  3142. mutex_unlock(&bp->hwrm_cmd_lock);
  3143. return rc;
  3144. }
  3145. static int bnxt_hwrm_ver_get(struct bnxt *bp)
  3146. {
  3147. int rc;
  3148. struct hwrm_ver_get_input req = {0};
  3149. struct hwrm_ver_get_output *resp = bp->hwrm_cmd_resp_addr;
  3150. bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
  3151. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VER_GET, -1, -1);
  3152. req.hwrm_intf_maj = HWRM_VERSION_MAJOR;
  3153. req.hwrm_intf_min = HWRM_VERSION_MINOR;
  3154. req.hwrm_intf_upd = HWRM_VERSION_UPDATE;
  3155. mutex_lock(&bp->hwrm_cmd_lock);
  3156. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3157. if (rc)
  3158. goto hwrm_ver_get_exit;
  3159. memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
  3160. if (resp->hwrm_intf_maj < 1) {
  3161. netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
  3162. resp->hwrm_intf_maj, resp->hwrm_intf_min,
  3163. resp->hwrm_intf_upd);
  3164. netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
  3165. }
  3166. snprintf(bp->fw_ver_str, BC_HWRM_STR_LEN, "%d.%d.%d/%d.%d.%d",
  3167. resp->hwrm_fw_maj, resp->hwrm_fw_min, resp->hwrm_fw_bld,
  3168. resp->hwrm_intf_maj, resp->hwrm_intf_min, resp->hwrm_intf_upd);
  3169. bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
  3170. if (!bp->hwrm_cmd_timeout)
  3171. bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
  3172. if (resp->hwrm_intf_maj >= 1)
  3173. bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
  3174. hwrm_ver_get_exit:
  3175. mutex_unlock(&bp->hwrm_cmd_lock);
  3176. return rc;
  3177. }
  3178. static int bnxt_hwrm_port_qstats(struct bnxt *bp)
  3179. {
  3180. int rc;
  3181. struct bnxt_pf_info *pf = &bp->pf;
  3182. struct hwrm_port_qstats_input req = {0};
  3183. if (!(bp->flags & BNXT_FLAG_PORT_STATS))
  3184. return 0;
  3185. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_QSTATS, -1, -1);
  3186. req.port_id = cpu_to_le16(pf->port_id);
  3187. req.tx_stat_host_addr = cpu_to_le64(bp->hw_tx_port_stats_map);
  3188. req.rx_stat_host_addr = cpu_to_le64(bp->hw_rx_port_stats_map);
  3189. rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3190. return rc;
  3191. }
  3192. static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
  3193. {
  3194. if (bp->vxlan_port_cnt) {
  3195. bnxt_hwrm_tunnel_dst_port_free(
  3196. bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
  3197. }
  3198. bp->vxlan_port_cnt = 0;
  3199. if (bp->nge_port_cnt) {
  3200. bnxt_hwrm_tunnel_dst_port_free(
  3201. bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
  3202. }
  3203. bp->nge_port_cnt = 0;
  3204. }
  3205. static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
  3206. {
  3207. int rc, i;
  3208. u32 tpa_flags = 0;
  3209. if (set_tpa)
  3210. tpa_flags = bp->flags & BNXT_FLAG_TPA;
  3211. for (i = 0; i < bp->nr_vnics; i++) {
  3212. rc = bnxt_hwrm_vnic_set_tpa(bp, i, tpa_flags);
  3213. if (rc) {
  3214. netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
  3215. rc, i);
  3216. return rc;
  3217. }
  3218. }
  3219. return 0;
  3220. }
  3221. static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
  3222. {
  3223. int i;
  3224. for (i = 0; i < bp->nr_vnics; i++)
  3225. bnxt_hwrm_vnic_set_rss(bp, i, false);
  3226. }
  3227. static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
  3228. bool irq_re_init)
  3229. {
  3230. if (bp->vnic_info) {
  3231. bnxt_hwrm_clear_vnic_filter(bp);
  3232. /* clear all RSS setting before free vnic ctx */
  3233. bnxt_hwrm_clear_vnic_rss(bp);
  3234. bnxt_hwrm_vnic_ctx_free(bp);
  3235. /* before free the vnic, undo the vnic tpa settings */
  3236. if (bp->flags & BNXT_FLAG_TPA)
  3237. bnxt_set_tpa(bp, false);
  3238. bnxt_hwrm_vnic_free(bp);
  3239. }
  3240. bnxt_hwrm_ring_free(bp, close_path);
  3241. bnxt_hwrm_ring_grp_free(bp);
  3242. if (irq_re_init) {
  3243. bnxt_hwrm_stat_ctx_free(bp);
  3244. bnxt_hwrm_free_tunnel_ports(bp);
  3245. }
  3246. }
  3247. static int bnxt_setup_vnic(struct bnxt *bp, u16 vnic_id)
  3248. {
  3249. int rc;
  3250. /* allocate context for vnic */
  3251. rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id);
  3252. if (rc) {
  3253. netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
  3254. vnic_id, rc);
  3255. goto vnic_setup_err;
  3256. }
  3257. bp->rsscos_nr_ctxs++;
  3258. /* configure default vnic, ring grp */
  3259. rc = bnxt_hwrm_vnic_cfg(bp, vnic_id);
  3260. if (rc) {
  3261. netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
  3262. vnic_id, rc);
  3263. goto vnic_setup_err;
  3264. }
  3265. /* Enable RSS hashing on vnic */
  3266. rc = bnxt_hwrm_vnic_set_rss(bp, vnic_id, true);
  3267. if (rc) {
  3268. netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
  3269. vnic_id, rc);
  3270. goto vnic_setup_err;
  3271. }
  3272. if (bp->flags & BNXT_FLAG_AGG_RINGS) {
  3273. rc = bnxt_hwrm_vnic_set_hds(bp, vnic_id);
  3274. if (rc) {
  3275. netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
  3276. vnic_id, rc);
  3277. }
  3278. }
  3279. vnic_setup_err:
  3280. return rc;
  3281. }
  3282. static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
  3283. {
  3284. #ifdef CONFIG_RFS_ACCEL
  3285. int i, rc = 0;
  3286. for (i = 0; i < bp->rx_nr_rings; i++) {
  3287. u16 vnic_id = i + 1;
  3288. u16 ring_id = i;
  3289. if (vnic_id >= bp->nr_vnics)
  3290. break;
  3291. bp->vnic_info[vnic_id].flags |= BNXT_VNIC_RFS_FLAG;
  3292. rc = bnxt_hwrm_vnic_alloc(bp, vnic_id, ring_id, 1);
  3293. if (rc) {
  3294. netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
  3295. vnic_id, rc);
  3296. break;
  3297. }
  3298. rc = bnxt_setup_vnic(bp, vnic_id);
  3299. if (rc)
  3300. break;
  3301. }
  3302. return rc;
  3303. #else
  3304. return 0;
  3305. #endif
  3306. }
  3307. static int bnxt_cfg_rx_mode(struct bnxt *);
  3308. static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
  3309. {
  3310. int rc = 0;
  3311. if (irq_re_init) {
  3312. rc = bnxt_hwrm_stat_ctx_alloc(bp);
  3313. if (rc) {
  3314. netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
  3315. rc);
  3316. goto err_out;
  3317. }
  3318. }
  3319. rc = bnxt_hwrm_ring_alloc(bp);
  3320. if (rc) {
  3321. netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
  3322. goto err_out;
  3323. }
  3324. rc = bnxt_hwrm_ring_grp_alloc(bp);
  3325. if (rc) {
  3326. netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
  3327. goto err_out;
  3328. }
  3329. /* default vnic 0 */
  3330. rc = bnxt_hwrm_vnic_alloc(bp, 0, 0, bp->rx_nr_rings);
  3331. if (rc) {
  3332. netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
  3333. goto err_out;
  3334. }
  3335. rc = bnxt_setup_vnic(bp, 0);
  3336. if (rc)
  3337. goto err_out;
  3338. if (bp->flags & BNXT_FLAG_RFS) {
  3339. rc = bnxt_alloc_rfs_vnics(bp);
  3340. if (rc)
  3341. goto err_out;
  3342. }
  3343. if (bp->flags & BNXT_FLAG_TPA) {
  3344. rc = bnxt_set_tpa(bp, true);
  3345. if (rc)
  3346. goto err_out;
  3347. }
  3348. if (BNXT_VF(bp))
  3349. bnxt_update_vf_mac(bp);
  3350. /* Filter for default vnic 0 */
  3351. rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
  3352. if (rc) {
  3353. netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
  3354. goto err_out;
  3355. }
  3356. bp->vnic_info[0].uc_filter_count = 1;
  3357. bp->vnic_info[0].rx_mask = CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
  3358. if ((bp->dev->flags & IFF_PROMISC) && BNXT_PF(bp))
  3359. bp->vnic_info[0].rx_mask |=
  3360. CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
  3361. rc = bnxt_cfg_rx_mode(bp);
  3362. if (rc)
  3363. goto err_out;
  3364. rc = bnxt_hwrm_set_coal(bp);
  3365. if (rc)
  3366. netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
  3367. rc);
  3368. return 0;
  3369. err_out:
  3370. bnxt_hwrm_resource_free(bp, 0, true);
  3371. return rc;
  3372. }
  3373. static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
  3374. {
  3375. bnxt_hwrm_resource_free(bp, 1, irq_re_init);
  3376. return 0;
  3377. }
  3378. static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
  3379. {
  3380. bnxt_init_rx_rings(bp);
  3381. bnxt_init_tx_rings(bp);
  3382. bnxt_init_ring_grps(bp, irq_re_init);
  3383. bnxt_init_vnics(bp);
  3384. return bnxt_init_chip(bp, irq_re_init);
  3385. }
  3386. static void bnxt_disable_int(struct bnxt *bp)
  3387. {
  3388. int i;
  3389. if (!bp->bnapi)
  3390. return;
  3391. for (i = 0; i < bp->cp_nr_rings; i++) {
  3392. struct bnxt_napi *bnapi = bp->bnapi[i];
  3393. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  3394. BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
  3395. }
  3396. }
  3397. static void bnxt_enable_int(struct bnxt *bp)
  3398. {
  3399. int i;
  3400. atomic_set(&bp->intr_sem, 0);
  3401. for (i = 0; i < bp->cp_nr_rings; i++) {
  3402. struct bnxt_napi *bnapi = bp->bnapi[i];
  3403. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  3404. BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
  3405. }
  3406. }
  3407. static int bnxt_set_real_num_queues(struct bnxt *bp)
  3408. {
  3409. int rc;
  3410. struct net_device *dev = bp->dev;
  3411. rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings);
  3412. if (rc)
  3413. return rc;
  3414. rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
  3415. if (rc)
  3416. return rc;
  3417. #ifdef CONFIG_RFS_ACCEL
  3418. if (bp->flags & BNXT_FLAG_RFS)
  3419. dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
  3420. #endif
  3421. return rc;
  3422. }
  3423. static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
  3424. bool shared)
  3425. {
  3426. int _rx = *rx, _tx = *tx;
  3427. if (shared) {
  3428. *rx = min_t(int, _rx, max);
  3429. *tx = min_t(int, _tx, max);
  3430. } else {
  3431. if (max < 2)
  3432. return -ENOMEM;
  3433. while (_rx + _tx > max) {
  3434. if (_rx > _tx && _rx > 1)
  3435. _rx--;
  3436. else if (_tx > 1)
  3437. _tx--;
  3438. }
  3439. *rx = _rx;
  3440. *tx = _tx;
  3441. }
  3442. return 0;
  3443. }
  3444. static int bnxt_setup_msix(struct bnxt *bp)
  3445. {
  3446. struct msix_entry *msix_ent;
  3447. struct net_device *dev = bp->dev;
  3448. int i, total_vecs, rc = 0, min = 1;
  3449. const int len = sizeof(bp->irq_tbl[0].name);
  3450. bp->flags &= ~BNXT_FLAG_USING_MSIX;
  3451. total_vecs = bp->cp_nr_rings;
  3452. msix_ent = kcalloc(total_vecs, sizeof(struct msix_entry), GFP_KERNEL);
  3453. if (!msix_ent)
  3454. return -ENOMEM;
  3455. for (i = 0; i < total_vecs; i++) {
  3456. msix_ent[i].entry = i;
  3457. msix_ent[i].vector = 0;
  3458. }
  3459. if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
  3460. min = 2;
  3461. total_vecs = pci_enable_msix_range(bp->pdev, msix_ent, min, total_vecs);
  3462. if (total_vecs < 0) {
  3463. rc = -ENODEV;
  3464. goto msix_setup_exit;
  3465. }
  3466. bp->irq_tbl = kcalloc(total_vecs, sizeof(struct bnxt_irq), GFP_KERNEL);
  3467. if (bp->irq_tbl) {
  3468. int tcs;
  3469. /* Trim rings based upon num of vectors allocated */
  3470. rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
  3471. total_vecs, min == 1);
  3472. if (rc)
  3473. goto msix_setup_exit;
  3474. bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
  3475. tcs = netdev_get_num_tc(dev);
  3476. if (tcs > 1) {
  3477. bp->tx_nr_rings_per_tc = bp->tx_nr_rings / tcs;
  3478. if (bp->tx_nr_rings_per_tc == 0) {
  3479. netdev_reset_tc(dev);
  3480. bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
  3481. } else {
  3482. int i, off, count;
  3483. bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tcs;
  3484. for (i = 0; i < tcs; i++) {
  3485. count = bp->tx_nr_rings_per_tc;
  3486. off = i * count;
  3487. netdev_set_tc_queue(dev, i, count, off);
  3488. }
  3489. }
  3490. }
  3491. bp->cp_nr_rings = total_vecs;
  3492. for (i = 0; i < bp->cp_nr_rings; i++) {
  3493. char *attr;
  3494. bp->irq_tbl[i].vector = msix_ent[i].vector;
  3495. if (bp->flags & BNXT_FLAG_SHARED_RINGS)
  3496. attr = "TxRx";
  3497. else if (i < bp->rx_nr_rings)
  3498. attr = "rx";
  3499. else
  3500. attr = "tx";
  3501. snprintf(bp->irq_tbl[i].name, len,
  3502. "%s-%s-%d", dev->name, attr, i);
  3503. bp->irq_tbl[i].handler = bnxt_msix;
  3504. }
  3505. rc = bnxt_set_real_num_queues(bp);
  3506. if (rc)
  3507. goto msix_setup_exit;
  3508. } else {
  3509. rc = -ENOMEM;
  3510. goto msix_setup_exit;
  3511. }
  3512. bp->flags |= BNXT_FLAG_USING_MSIX;
  3513. kfree(msix_ent);
  3514. return 0;
  3515. msix_setup_exit:
  3516. netdev_err(bp->dev, "bnxt_setup_msix err: %x\n", rc);
  3517. pci_disable_msix(bp->pdev);
  3518. kfree(msix_ent);
  3519. return rc;
  3520. }
  3521. static int bnxt_setup_inta(struct bnxt *bp)
  3522. {
  3523. int rc;
  3524. const int len = sizeof(bp->irq_tbl[0].name);
  3525. if (netdev_get_num_tc(bp->dev))
  3526. netdev_reset_tc(bp->dev);
  3527. bp->irq_tbl = kcalloc(1, sizeof(struct bnxt_irq), GFP_KERNEL);
  3528. if (!bp->irq_tbl) {
  3529. rc = -ENOMEM;
  3530. return rc;
  3531. }
  3532. bp->rx_nr_rings = 1;
  3533. bp->tx_nr_rings = 1;
  3534. bp->cp_nr_rings = 1;
  3535. bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
  3536. bp->flags |= BNXT_FLAG_SHARED_RINGS;
  3537. bp->irq_tbl[0].vector = bp->pdev->irq;
  3538. snprintf(bp->irq_tbl[0].name, len,
  3539. "%s-%s-%d", bp->dev->name, "TxRx", 0);
  3540. bp->irq_tbl[0].handler = bnxt_inta;
  3541. rc = bnxt_set_real_num_queues(bp);
  3542. return rc;
  3543. }
  3544. static int bnxt_setup_int_mode(struct bnxt *bp)
  3545. {
  3546. int rc = 0;
  3547. if (bp->flags & BNXT_FLAG_MSIX_CAP)
  3548. rc = bnxt_setup_msix(bp);
  3549. if (!(bp->flags & BNXT_FLAG_USING_MSIX)) {
  3550. /* fallback to INTA */
  3551. rc = bnxt_setup_inta(bp);
  3552. }
  3553. return rc;
  3554. }
  3555. static void bnxt_free_irq(struct bnxt *bp)
  3556. {
  3557. struct bnxt_irq *irq;
  3558. int i;
  3559. #ifdef CONFIG_RFS_ACCEL
  3560. free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
  3561. bp->dev->rx_cpu_rmap = NULL;
  3562. #endif
  3563. if (!bp->irq_tbl)
  3564. return;
  3565. for (i = 0; i < bp->cp_nr_rings; i++) {
  3566. irq = &bp->irq_tbl[i];
  3567. if (irq->requested)
  3568. free_irq(irq->vector, bp->bnapi[i]);
  3569. irq->requested = 0;
  3570. }
  3571. if (bp->flags & BNXT_FLAG_USING_MSIX)
  3572. pci_disable_msix(bp->pdev);
  3573. kfree(bp->irq_tbl);
  3574. bp->irq_tbl = NULL;
  3575. }
  3576. static int bnxt_request_irq(struct bnxt *bp)
  3577. {
  3578. int i, j, rc = 0;
  3579. unsigned long flags = 0;
  3580. #ifdef CONFIG_RFS_ACCEL
  3581. struct cpu_rmap *rmap = bp->dev->rx_cpu_rmap;
  3582. #endif
  3583. if (!(bp->flags & BNXT_FLAG_USING_MSIX))
  3584. flags = IRQF_SHARED;
  3585. for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
  3586. struct bnxt_irq *irq = &bp->irq_tbl[i];
  3587. #ifdef CONFIG_RFS_ACCEL
  3588. if (rmap && bp->bnapi[i]->rx_ring) {
  3589. rc = irq_cpu_rmap_add(rmap, irq->vector);
  3590. if (rc)
  3591. netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
  3592. j);
  3593. j++;
  3594. }
  3595. #endif
  3596. rc = request_irq(irq->vector, irq->handler, flags, irq->name,
  3597. bp->bnapi[i]);
  3598. if (rc)
  3599. break;
  3600. irq->requested = 1;
  3601. }
  3602. return rc;
  3603. }
  3604. static void bnxt_del_napi(struct bnxt *bp)
  3605. {
  3606. int i;
  3607. if (!bp->bnapi)
  3608. return;
  3609. for (i = 0; i < bp->cp_nr_rings; i++) {
  3610. struct bnxt_napi *bnapi = bp->bnapi[i];
  3611. napi_hash_del(&bnapi->napi);
  3612. netif_napi_del(&bnapi->napi);
  3613. }
  3614. }
  3615. static void bnxt_init_napi(struct bnxt *bp)
  3616. {
  3617. int i;
  3618. struct bnxt_napi *bnapi;
  3619. if (bp->flags & BNXT_FLAG_USING_MSIX) {
  3620. for (i = 0; i < bp->cp_nr_rings; i++) {
  3621. bnapi = bp->bnapi[i];
  3622. netif_napi_add(bp->dev, &bnapi->napi,
  3623. bnxt_poll, 64);
  3624. }
  3625. } else {
  3626. bnapi = bp->bnapi[0];
  3627. netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll, 64);
  3628. }
  3629. }
  3630. static void bnxt_disable_napi(struct bnxt *bp)
  3631. {
  3632. int i;
  3633. if (!bp->bnapi)
  3634. return;
  3635. for (i = 0; i < bp->cp_nr_rings; i++) {
  3636. napi_disable(&bp->bnapi[i]->napi);
  3637. bnxt_disable_poll(bp->bnapi[i]);
  3638. }
  3639. }
  3640. static void bnxt_enable_napi(struct bnxt *bp)
  3641. {
  3642. int i;
  3643. for (i = 0; i < bp->cp_nr_rings; i++) {
  3644. bnxt_enable_poll(bp->bnapi[i]);
  3645. napi_enable(&bp->bnapi[i]->napi);
  3646. }
  3647. }
  3648. static void bnxt_tx_disable(struct bnxt *bp)
  3649. {
  3650. int i;
  3651. struct bnxt_tx_ring_info *txr;
  3652. struct netdev_queue *txq;
  3653. if (bp->tx_ring) {
  3654. for (i = 0; i < bp->tx_nr_rings; i++) {
  3655. txr = &bp->tx_ring[i];
  3656. txq = netdev_get_tx_queue(bp->dev, i);
  3657. __netif_tx_lock(txq, smp_processor_id());
  3658. txr->dev_state = BNXT_DEV_STATE_CLOSING;
  3659. __netif_tx_unlock(txq);
  3660. }
  3661. }
  3662. /* Stop all TX queues */
  3663. netif_tx_disable(bp->dev);
  3664. netif_carrier_off(bp->dev);
  3665. }
  3666. static void bnxt_tx_enable(struct bnxt *bp)
  3667. {
  3668. int i;
  3669. struct bnxt_tx_ring_info *txr;
  3670. struct netdev_queue *txq;
  3671. for (i = 0; i < bp->tx_nr_rings; i++) {
  3672. txr = &bp->tx_ring[i];
  3673. txq = netdev_get_tx_queue(bp->dev, i);
  3674. txr->dev_state = 0;
  3675. }
  3676. netif_tx_wake_all_queues(bp->dev);
  3677. if (bp->link_info.link_up)
  3678. netif_carrier_on(bp->dev);
  3679. }
  3680. static void bnxt_report_link(struct bnxt *bp)
  3681. {
  3682. if (bp->link_info.link_up) {
  3683. const char *duplex;
  3684. const char *flow_ctrl;
  3685. u16 speed;
  3686. netif_carrier_on(bp->dev);
  3687. if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
  3688. duplex = "full";
  3689. else
  3690. duplex = "half";
  3691. if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
  3692. flow_ctrl = "ON - receive & transmit";
  3693. else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
  3694. flow_ctrl = "ON - transmit";
  3695. else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
  3696. flow_ctrl = "ON - receive";
  3697. else
  3698. flow_ctrl = "none";
  3699. speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
  3700. netdev_info(bp->dev, "NIC Link is Up, %d Mbps %s duplex, Flow control: %s\n",
  3701. speed, duplex, flow_ctrl);
  3702. } else {
  3703. netif_carrier_off(bp->dev);
  3704. netdev_err(bp->dev, "NIC Link is Down\n");
  3705. }
  3706. }
  3707. static int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
  3708. {
  3709. int rc = 0;
  3710. struct bnxt_link_info *link_info = &bp->link_info;
  3711. struct hwrm_port_phy_qcfg_input req = {0};
  3712. struct hwrm_port_phy_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
  3713. u8 link_up = link_info->link_up;
  3714. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCFG, -1, -1);
  3715. mutex_lock(&bp->hwrm_cmd_lock);
  3716. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3717. if (rc) {
  3718. mutex_unlock(&bp->hwrm_cmd_lock);
  3719. return rc;
  3720. }
  3721. memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
  3722. link_info->phy_link_status = resp->link;
  3723. link_info->duplex = resp->duplex;
  3724. link_info->pause = resp->pause;
  3725. link_info->auto_mode = resp->auto_mode;
  3726. link_info->auto_pause_setting = resp->auto_pause;
  3727. link_info->lp_pause = resp->link_partner_adv_pause;
  3728. link_info->force_pause_setting = resp->force_pause;
  3729. link_info->duplex_setting = resp->duplex;
  3730. if (link_info->phy_link_status == BNXT_LINK_LINK)
  3731. link_info->link_speed = le16_to_cpu(resp->link_speed);
  3732. else
  3733. link_info->link_speed = 0;
  3734. link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
  3735. link_info->auto_link_speed = le16_to_cpu(resp->auto_link_speed);
  3736. link_info->support_speeds = le16_to_cpu(resp->support_speeds);
  3737. link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
  3738. link_info->lp_auto_link_speeds =
  3739. le16_to_cpu(resp->link_partner_adv_speeds);
  3740. link_info->preemphasis = le32_to_cpu(resp->preemphasis);
  3741. link_info->phy_ver[0] = resp->phy_maj;
  3742. link_info->phy_ver[1] = resp->phy_min;
  3743. link_info->phy_ver[2] = resp->phy_bld;
  3744. link_info->media_type = resp->media_type;
  3745. link_info->transceiver = resp->transceiver_type;
  3746. link_info->phy_addr = resp->phy_addr;
  3747. /* TODO: need to add more logic to report VF link */
  3748. if (chng_link_state) {
  3749. if (link_info->phy_link_status == BNXT_LINK_LINK)
  3750. link_info->link_up = 1;
  3751. else
  3752. link_info->link_up = 0;
  3753. if (link_up != link_info->link_up)
  3754. bnxt_report_link(bp);
  3755. } else {
  3756. /* alwasy link down if not require to update link state */
  3757. link_info->link_up = 0;
  3758. }
  3759. mutex_unlock(&bp->hwrm_cmd_lock);
  3760. return 0;
  3761. }
  3762. static void
  3763. bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
  3764. {
  3765. if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
  3766. if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
  3767. req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
  3768. if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
  3769. req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
  3770. req->enables |=
  3771. cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
  3772. } else {
  3773. if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
  3774. req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
  3775. if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
  3776. req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
  3777. req->enables |=
  3778. cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
  3779. }
  3780. }
  3781. static void bnxt_hwrm_set_link_common(struct bnxt *bp,
  3782. struct hwrm_port_phy_cfg_input *req)
  3783. {
  3784. u8 autoneg = bp->link_info.autoneg;
  3785. u16 fw_link_speed = bp->link_info.req_link_speed;
  3786. u32 advertising = bp->link_info.advertising;
  3787. if (autoneg & BNXT_AUTONEG_SPEED) {
  3788. req->auto_mode |=
  3789. PORT_PHY_CFG_REQ_AUTO_MODE_MASK;
  3790. req->enables |= cpu_to_le32(
  3791. PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
  3792. req->auto_link_speed_mask = cpu_to_le16(advertising);
  3793. req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
  3794. req->flags |=
  3795. cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
  3796. } else {
  3797. req->force_link_speed = cpu_to_le16(fw_link_speed);
  3798. req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
  3799. }
  3800. /* currently don't support half duplex */
  3801. req->auto_duplex = PORT_PHY_CFG_REQ_AUTO_DUPLEX_FULL;
  3802. req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_DUPLEX);
  3803. /* tell chimp that the setting takes effect immediately */
  3804. req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
  3805. }
  3806. int bnxt_hwrm_set_pause(struct bnxt *bp)
  3807. {
  3808. struct hwrm_port_phy_cfg_input req = {0};
  3809. int rc;
  3810. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
  3811. bnxt_hwrm_set_pause_common(bp, &req);
  3812. if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
  3813. bp->link_info.force_link_chng)
  3814. bnxt_hwrm_set_link_common(bp, &req);
  3815. mutex_lock(&bp->hwrm_cmd_lock);
  3816. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3817. if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
  3818. /* since changing of pause setting doesn't trigger any link
  3819. * change event, the driver needs to update the current pause
  3820. * result upon successfully return of the phy_cfg command
  3821. */
  3822. bp->link_info.pause =
  3823. bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
  3824. bp->link_info.auto_pause_setting = 0;
  3825. if (!bp->link_info.force_link_chng)
  3826. bnxt_report_link(bp);
  3827. }
  3828. bp->link_info.force_link_chng = false;
  3829. mutex_unlock(&bp->hwrm_cmd_lock);
  3830. return rc;
  3831. }
  3832. int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause)
  3833. {
  3834. struct hwrm_port_phy_cfg_input req = {0};
  3835. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
  3836. if (set_pause)
  3837. bnxt_hwrm_set_pause_common(bp, &req);
  3838. bnxt_hwrm_set_link_common(bp, &req);
  3839. return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3840. }
  3841. static int bnxt_update_phy_setting(struct bnxt *bp)
  3842. {
  3843. int rc;
  3844. bool update_link = false;
  3845. bool update_pause = false;
  3846. struct bnxt_link_info *link_info = &bp->link_info;
  3847. rc = bnxt_update_link(bp, true);
  3848. if (rc) {
  3849. netdev_err(bp->dev, "failed to update link (rc: %x)\n",
  3850. rc);
  3851. return rc;
  3852. }
  3853. if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
  3854. link_info->auto_pause_setting != link_info->req_flow_ctrl)
  3855. update_pause = true;
  3856. if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
  3857. link_info->force_pause_setting != link_info->req_flow_ctrl)
  3858. update_pause = true;
  3859. if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
  3860. if (BNXT_AUTO_MODE(link_info->auto_mode))
  3861. update_link = true;
  3862. if (link_info->req_link_speed != link_info->force_link_speed)
  3863. update_link = true;
  3864. if (link_info->req_duplex != link_info->duplex_setting)
  3865. update_link = true;
  3866. } else {
  3867. if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
  3868. update_link = true;
  3869. if (link_info->advertising != link_info->auto_link_speeds)
  3870. update_link = true;
  3871. }
  3872. if (update_link)
  3873. rc = bnxt_hwrm_set_link_setting(bp, update_pause);
  3874. else if (update_pause)
  3875. rc = bnxt_hwrm_set_pause(bp);
  3876. if (rc) {
  3877. netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
  3878. rc);
  3879. return rc;
  3880. }
  3881. return rc;
  3882. }
  3883. /* Common routine to pre-map certain register block to different GRC window.
  3884. * A PF has 16 4K windows and a VF has 4 4K windows. However, only 15 windows
  3885. * in PF and 3 windows in VF that can be customized to map in different
  3886. * register blocks.
  3887. */
  3888. static void bnxt_preset_reg_win(struct bnxt *bp)
  3889. {
  3890. if (BNXT_PF(bp)) {
  3891. /* CAG registers map to GRC window #4 */
  3892. writel(BNXT_CAG_REG_BASE,
  3893. bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 12);
  3894. }
  3895. }
  3896. static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
  3897. {
  3898. int rc = 0;
  3899. bnxt_preset_reg_win(bp);
  3900. netif_carrier_off(bp->dev);
  3901. if (irq_re_init) {
  3902. rc = bnxt_setup_int_mode(bp);
  3903. if (rc) {
  3904. netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
  3905. rc);
  3906. return rc;
  3907. }
  3908. }
  3909. if ((bp->flags & BNXT_FLAG_RFS) &&
  3910. !(bp->flags & BNXT_FLAG_USING_MSIX)) {
  3911. /* disable RFS if falling back to INTA */
  3912. bp->dev->hw_features &= ~NETIF_F_NTUPLE;
  3913. bp->flags &= ~BNXT_FLAG_RFS;
  3914. }
  3915. rc = bnxt_alloc_mem(bp, irq_re_init);
  3916. if (rc) {
  3917. netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
  3918. goto open_err_free_mem;
  3919. }
  3920. if (irq_re_init) {
  3921. bnxt_init_napi(bp);
  3922. rc = bnxt_request_irq(bp);
  3923. if (rc) {
  3924. netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
  3925. goto open_err;
  3926. }
  3927. }
  3928. bnxt_enable_napi(bp);
  3929. rc = bnxt_init_nic(bp, irq_re_init);
  3930. if (rc) {
  3931. netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
  3932. goto open_err;
  3933. }
  3934. if (link_re_init) {
  3935. rc = bnxt_update_phy_setting(bp);
  3936. if (rc)
  3937. netdev_warn(bp->dev, "failed to update phy settings\n");
  3938. }
  3939. if (irq_re_init) {
  3940. #if defined(CONFIG_VXLAN) || defined(CONFIG_VXLAN_MODULE)
  3941. vxlan_get_rx_port(bp->dev);
  3942. #endif
  3943. if (!bnxt_hwrm_tunnel_dst_port_alloc(
  3944. bp, htons(0x17c1),
  3945. TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE))
  3946. bp->nge_port_cnt = 1;
  3947. }
  3948. set_bit(BNXT_STATE_OPEN, &bp->state);
  3949. bnxt_enable_int(bp);
  3950. /* Enable TX queues */
  3951. bnxt_tx_enable(bp);
  3952. mod_timer(&bp->timer, jiffies + bp->current_interval);
  3953. bnxt_update_link(bp, true);
  3954. return 0;
  3955. open_err:
  3956. bnxt_disable_napi(bp);
  3957. bnxt_del_napi(bp);
  3958. open_err_free_mem:
  3959. bnxt_free_skbs(bp);
  3960. bnxt_free_irq(bp);
  3961. bnxt_free_mem(bp, true);
  3962. return rc;
  3963. }
  3964. /* rtnl_lock held */
  3965. int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
  3966. {
  3967. int rc = 0;
  3968. rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
  3969. if (rc) {
  3970. netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
  3971. dev_close(bp->dev);
  3972. }
  3973. return rc;
  3974. }
  3975. static int bnxt_open(struct net_device *dev)
  3976. {
  3977. struct bnxt *bp = netdev_priv(dev);
  3978. int rc = 0;
  3979. rc = bnxt_hwrm_func_reset(bp);
  3980. if (rc) {
  3981. netdev_err(bp->dev, "hwrm chip reset failure rc: %x\n",
  3982. rc);
  3983. rc = -1;
  3984. return rc;
  3985. }
  3986. return __bnxt_open_nic(bp, true, true);
  3987. }
  3988. static void bnxt_disable_int_sync(struct bnxt *bp)
  3989. {
  3990. int i;
  3991. atomic_inc(&bp->intr_sem);
  3992. if (!netif_running(bp->dev))
  3993. return;
  3994. bnxt_disable_int(bp);
  3995. for (i = 0; i < bp->cp_nr_rings; i++)
  3996. synchronize_irq(bp->irq_tbl[i].vector);
  3997. }
  3998. int bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
  3999. {
  4000. int rc = 0;
  4001. #ifdef CONFIG_BNXT_SRIOV
  4002. if (bp->sriov_cfg) {
  4003. rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
  4004. !bp->sriov_cfg,
  4005. BNXT_SRIOV_CFG_WAIT_TMO);
  4006. if (rc)
  4007. netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete!\n");
  4008. }
  4009. #endif
  4010. /* Change device state to avoid TX queue wake up's */
  4011. bnxt_tx_disable(bp);
  4012. clear_bit(BNXT_STATE_OPEN, &bp->state);
  4013. smp_mb__after_atomic();
  4014. while (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state))
  4015. msleep(20);
  4016. /* Flush rings before disabling interrupts */
  4017. bnxt_shutdown_nic(bp, irq_re_init);
  4018. /* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
  4019. bnxt_disable_napi(bp);
  4020. bnxt_disable_int_sync(bp);
  4021. del_timer_sync(&bp->timer);
  4022. bnxt_free_skbs(bp);
  4023. if (irq_re_init) {
  4024. bnxt_free_irq(bp);
  4025. bnxt_del_napi(bp);
  4026. }
  4027. bnxt_free_mem(bp, irq_re_init);
  4028. return rc;
  4029. }
  4030. static int bnxt_close(struct net_device *dev)
  4031. {
  4032. struct bnxt *bp = netdev_priv(dev);
  4033. bnxt_close_nic(bp, true, true);
  4034. return 0;
  4035. }
  4036. /* rtnl_lock held */
  4037. static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  4038. {
  4039. switch (cmd) {
  4040. case SIOCGMIIPHY:
  4041. /* fallthru */
  4042. case SIOCGMIIREG: {
  4043. if (!netif_running(dev))
  4044. return -EAGAIN;
  4045. return 0;
  4046. }
  4047. case SIOCSMIIREG:
  4048. if (!netif_running(dev))
  4049. return -EAGAIN;
  4050. return 0;
  4051. default:
  4052. /* do nothing */
  4053. break;
  4054. }
  4055. return -EOPNOTSUPP;
  4056. }
  4057. static struct rtnl_link_stats64 *
  4058. bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
  4059. {
  4060. u32 i;
  4061. struct bnxt *bp = netdev_priv(dev);
  4062. memset(stats, 0, sizeof(struct rtnl_link_stats64));
  4063. if (!bp->bnapi)
  4064. return stats;
  4065. /* TODO check if we need to synchronize with bnxt_close path */
  4066. for (i = 0; i < bp->cp_nr_rings; i++) {
  4067. struct bnxt_napi *bnapi = bp->bnapi[i];
  4068. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  4069. struct ctx_hw_stats *hw_stats = cpr->hw_stats;
  4070. stats->rx_packets += le64_to_cpu(hw_stats->rx_ucast_pkts);
  4071. stats->rx_packets += le64_to_cpu(hw_stats->rx_mcast_pkts);
  4072. stats->rx_packets += le64_to_cpu(hw_stats->rx_bcast_pkts);
  4073. stats->tx_packets += le64_to_cpu(hw_stats->tx_ucast_pkts);
  4074. stats->tx_packets += le64_to_cpu(hw_stats->tx_mcast_pkts);
  4075. stats->tx_packets += le64_to_cpu(hw_stats->tx_bcast_pkts);
  4076. stats->rx_bytes += le64_to_cpu(hw_stats->rx_ucast_bytes);
  4077. stats->rx_bytes += le64_to_cpu(hw_stats->rx_mcast_bytes);
  4078. stats->rx_bytes += le64_to_cpu(hw_stats->rx_bcast_bytes);
  4079. stats->tx_bytes += le64_to_cpu(hw_stats->tx_ucast_bytes);
  4080. stats->tx_bytes += le64_to_cpu(hw_stats->tx_mcast_bytes);
  4081. stats->tx_bytes += le64_to_cpu(hw_stats->tx_bcast_bytes);
  4082. stats->rx_missed_errors +=
  4083. le64_to_cpu(hw_stats->rx_discard_pkts);
  4084. stats->multicast += le64_to_cpu(hw_stats->rx_mcast_pkts);
  4085. stats->tx_dropped += le64_to_cpu(hw_stats->tx_drop_pkts);
  4086. }
  4087. if (bp->flags & BNXT_FLAG_PORT_STATS) {
  4088. struct rx_port_stats *rx = bp->hw_rx_port_stats;
  4089. struct tx_port_stats *tx = bp->hw_tx_port_stats;
  4090. stats->rx_crc_errors = le64_to_cpu(rx->rx_fcs_err_frames);
  4091. stats->rx_frame_errors = le64_to_cpu(rx->rx_align_err_frames);
  4092. stats->rx_length_errors = le64_to_cpu(rx->rx_undrsz_frames) +
  4093. le64_to_cpu(rx->rx_ovrsz_frames) +
  4094. le64_to_cpu(rx->rx_runt_frames);
  4095. stats->rx_errors = le64_to_cpu(rx->rx_false_carrier_frames) +
  4096. le64_to_cpu(rx->rx_jbr_frames);
  4097. stats->collisions = le64_to_cpu(tx->tx_total_collisions);
  4098. stats->tx_fifo_errors = le64_to_cpu(tx->tx_fifo_underruns);
  4099. stats->tx_errors = le64_to_cpu(tx->tx_err);
  4100. }
  4101. return stats;
  4102. }
  4103. static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
  4104. {
  4105. struct net_device *dev = bp->dev;
  4106. struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
  4107. struct netdev_hw_addr *ha;
  4108. u8 *haddr;
  4109. int mc_count = 0;
  4110. bool update = false;
  4111. int off = 0;
  4112. netdev_for_each_mc_addr(ha, dev) {
  4113. if (mc_count >= BNXT_MAX_MC_ADDRS) {
  4114. *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
  4115. vnic->mc_list_count = 0;
  4116. return false;
  4117. }
  4118. haddr = ha->addr;
  4119. if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
  4120. memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
  4121. update = true;
  4122. }
  4123. off += ETH_ALEN;
  4124. mc_count++;
  4125. }
  4126. if (mc_count)
  4127. *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
  4128. if (mc_count != vnic->mc_list_count) {
  4129. vnic->mc_list_count = mc_count;
  4130. update = true;
  4131. }
  4132. return update;
  4133. }
  4134. static bool bnxt_uc_list_updated(struct bnxt *bp)
  4135. {
  4136. struct net_device *dev = bp->dev;
  4137. struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
  4138. struct netdev_hw_addr *ha;
  4139. int off = 0;
  4140. if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
  4141. return true;
  4142. netdev_for_each_uc_addr(ha, dev) {
  4143. if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
  4144. return true;
  4145. off += ETH_ALEN;
  4146. }
  4147. return false;
  4148. }
  4149. static void bnxt_set_rx_mode(struct net_device *dev)
  4150. {
  4151. struct bnxt *bp = netdev_priv(dev);
  4152. struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
  4153. u32 mask = vnic->rx_mask;
  4154. bool mc_update = false;
  4155. bool uc_update;
  4156. if (!netif_running(dev))
  4157. return;
  4158. mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
  4159. CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
  4160. CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST);
  4161. /* Only allow PF to be in promiscuous mode */
  4162. if ((dev->flags & IFF_PROMISC) && BNXT_PF(bp))
  4163. mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
  4164. uc_update = bnxt_uc_list_updated(bp);
  4165. if (dev->flags & IFF_ALLMULTI) {
  4166. mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
  4167. vnic->mc_list_count = 0;
  4168. } else {
  4169. mc_update = bnxt_mc_list_updated(bp, &mask);
  4170. }
  4171. if (mask != vnic->rx_mask || uc_update || mc_update) {
  4172. vnic->rx_mask = mask;
  4173. set_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event);
  4174. schedule_work(&bp->sp_task);
  4175. }
  4176. }
  4177. static int bnxt_cfg_rx_mode(struct bnxt *bp)
  4178. {
  4179. struct net_device *dev = bp->dev;
  4180. struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
  4181. struct netdev_hw_addr *ha;
  4182. int i, off = 0, rc;
  4183. bool uc_update;
  4184. netif_addr_lock_bh(dev);
  4185. uc_update = bnxt_uc_list_updated(bp);
  4186. netif_addr_unlock_bh(dev);
  4187. if (!uc_update)
  4188. goto skip_uc;
  4189. mutex_lock(&bp->hwrm_cmd_lock);
  4190. for (i = 1; i < vnic->uc_filter_count; i++) {
  4191. struct hwrm_cfa_l2_filter_free_input req = {0};
  4192. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_FREE, -1,
  4193. -1);
  4194. req.l2_filter_id = vnic->fw_l2_filter_id[i];
  4195. rc = _hwrm_send_message(bp, &req, sizeof(req),
  4196. HWRM_CMD_TIMEOUT);
  4197. }
  4198. mutex_unlock(&bp->hwrm_cmd_lock);
  4199. vnic->uc_filter_count = 1;
  4200. netif_addr_lock_bh(dev);
  4201. if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
  4202. vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
  4203. } else {
  4204. netdev_for_each_uc_addr(ha, dev) {
  4205. memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
  4206. off += ETH_ALEN;
  4207. vnic->uc_filter_count++;
  4208. }
  4209. }
  4210. netif_addr_unlock_bh(dev);
  4211. for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
  4212. rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
  4213. if (rc) {
  4214. netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n",
  4215. rc);
  4216. vnic->uc_filter_count = i;
  4217. return rc;
  4218. }
  4219. }
  4220. skip_uc:
  4221. rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
  4222. if (rc)
  4223. netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %x\n",
  4224. rc);
  4225. return rc;
  4226. }
  4227. static bool bnxt_rfs_capable(struct bnxt *bp)
  4228. {
  4229. #ifdef CONFIG_RFS_ACCEL
  4230. struct bnxt_pf_info *pf = &bp->pf;
  4231. int vnics;
  4232. if (BNXT_VF(bp) || !(bp->flags & BNXT_FLAG_MSIX_CAP))
  4233. return false;
  4234. vnics = 1 + bp->rx_nr_rings;
  4235. if (vnics > pf->max_rsscos_ctxs || vnics > pf->max_vnics)
  4236. return false;
  4237. return true;
  4238. #else
  4239. return false;
  4240. #endif
  4241. }
  4242. static netdev_features_t bnxt_fix_features(struct net_device *dev,
  4243. netdev_features_t features)
  4244. {
  4245. struct bnxt *bp = netdev_priv(dev);
  4246. if (!bnxt_rfs_capable(bp))
  4247. features &= ~NETIF_F_NTUPLE;
  4248. return features;
  4249. }
  4250. static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
  4251. {
  4252. struct bnxt *bp = netdev_priv(dev);
  4253. u32 flags = bp->flags;
  4254. u32 changes;
  4255. int rc = 0;
  4256. bool re_init = false;
  4257. bool update_tpa = false;
  4258. flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
  4259. if ((features & NETIF_F_GRO) && (bp->pdev->revision > 0))
  4260. flags |= BNXT_FLAG_GRO;
  4261. if (features & NETIF_F_LRO)
  4262. flags |= BNXT_FLAG_LRO;
  4263. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  4264. flags |= BNXT_FLAG_STRIP_VLAN;
  4265. if (features & NETIF_F_NTUPLE)
  4266. flags |= BNXT_FLAG_RFS;
  4267. changes = flags ^ bp->flags;
  4268. if (changes & BNXT_FLAG_TPA) {
  4269. update_tpa = true;
  4270. if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
  4271. (flags & BNXT_FLAG_TPA) == 0)
  4272. re_init = true;
  4273. }
  4274. if (changes & ~BNXT_FLAG_TPA)
  4275. re_init = true;
  4276. if (flags != bp->flags) {
  4277. u32 old_flags = bp->flags;
  4278. bp->flags = flags;
  4279. if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
  4280. if (update_tpa)
  4281. bnxt_set_ring_params(bp);
  4282. return rc;
  4283. }
  4284. if (re_init) {
  4285. bnxt_close_nic(bp, false, false);
  4286. if (update_tpa)
  4287. bnxt_set_ring_params(bp);
  4288. return bnxt_open_nic(bp, false, false);
  4289. }
  4290. if (update_tpa) {
  4291. rc = bnxt_set_tpa(bp,
  4292. (flags & BNXT_FLAG_TPA) ?
  4293. true : false);
  4294. if (rc)
  4295. bp->flags = old_flags;
  4296. }
  4297. }
  4298. return rc;
  4299. }
  4300. static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
  4301. {
  4302. struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
  4303. int i = bnapi->index;
  4304. if (!txr)
  4305. return;
  4306. netdev_info(bnapi->bp->dev, "[%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
  4307. i, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
  4308. txr->tx_cons);
  4309. }
  4310. static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
  4311. {
  4312. struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
  4313. int i = bnapi->index;
  4314. if (!rxr)
  4315. return;
  4316. netdev_info(bnapi->bp->dev, "[%d]: rx{fw_ring: %d prod: %x} rx_agg{fw_ring: %d agg_prod: %x sw_agg_prod: %x}\n",
  4317. i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
  4318. rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
  4319. rxr->rx_sw_agg_prod);
  4320. }
  4321. static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
  4322. {
  4323. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  4324. int i = bnapi->index;
  4325. netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
  4326. i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
  4327. }
  4328. static void bnxt_dbg_dump_states(struct bnxt *bp)
  4329. {
  4330. int i;
  4331. struct bnxt_napi *bnapi;
  4332. for (i = 0; i < bp->cp_nr_rings; i++) {
  4333. bnapi = bp->bnapi[i];
  4334. if (netif_msg_drv(bp)) {
  4335. bnxt_dump_tx_sw_state(bnapi);
  4336. bnxt_dump_rx_sw_state(bnapi);
  4337. bnxt_dump_cp_sw_state(bnapi);
  4338. }
  4339. }
  4340. }
  4341. static void bnxt_reset_task(struct bnxt *bp)
  4342. {
  4343. bnxt_dbg_dump_states(bp);
  4344. if (netif_running(bp->dev)) {
  4345. bnxt_close_nic(bp, false, false);
  4346. bnxt_open_nic(bp, false, false);
  4347. }
  4348. }
  4349. static void bnxt_tx_timeout(struct net_device *dev)
  4350. {
  4351. struct bnxt *bp = netdev_priv(dev);
  4352. netdev_err(bp->dev, "TX timeout detected, starting reset task!\n");
  4353. set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
  4354. schedule_work(&bp->sp_task);
  4355. }
  4356. #ifdef CONFIG_NET_POLL_CONTROLLER
  4357. static void bnxt_poll_controller(struct net_device *dev)
  4358. {
  4359. struct bnxt *bp = netdev_priv(dev);
  4360. int i;
  4361. for (i = 0; i < bp->cp_nr_rings; i++) {
  4362. struct bnxt_irq *irq = &bp->irq_tbl[i];
  4363. disable_irq(irq->vector);
  4364. irq->handler(irq->vector, bp->bnapi[i]);
  4365. enable_irq(irq->vector);
  4366. }
  4367. }
  4368. #endif
  4369. static void bnxt_timer(unsigned long data)
  4370. {
  4371. struct bnxt *bp = (struct bnxt *)data;
  4372. struct net_device *dev = bp->dev;
  4373. if (!netif_running(dev))
  4374. return;
  4375. if (atomic_read(&bp->intr_sem) != 0)
  4376. goto bnxt_restart_timer;
  4377. if (bp->link_info.link_up && (bp->flags & BNXT_FLAG_PORT_STATS)) {
  4378. set_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event);
  4379. schedule_work(&bp->sp_task);
  4380. }
  4381. bnxt_restart_timer:
  4382. mod_timer(&bp->timer, jiffies + bp->current_interval);
  4383. }
  4384. static void bnxt_cfg_ntp_filters(struct bnxt *);
  4385. static void bnxt_sp_task(struct work_struct *work)
  4386. {
  4387. struct bnxt *bp = container_of(work, struct bnxt, sp_task);
  4388. int rc;
  4389. set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
  4390. smp_mb__after_atomic();
  4391. if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
  4392. clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
  4393. return;
  4394. }
  4395. if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
  4396. bnxt_cfg_rx_mode(bp);
  4397. if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
  4398. bnxt_cfg_ntp_filters(bp);
  4399. if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
  4400. rc = bnxt_update_link(bp, true);
  4401. if (rc)
  4402. netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
  4403. rc);
  4404. }
  4405. if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
  4406. bnxt_hwrm_exec_fwd_req(bp);
  4407. if (test_and_clear_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event)) {
  4408. bnxt_hwrm_tunnel_dst_port_alloc(
  4409. bp, bp->vxlan_port,
  4410. TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
  4411. }
  4412. if (test_and_clear_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event)) {
  4413. bnxt_hwrm_tunnel_dst_port_free(
  4414. bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
  4415. }
  4416. if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event)) {
  4417. /* bnxt_reset_task() calls bnxt_close_nic() which waits
  4418. * for BNXT_STATE_IN_SP_TASK to clear.
  4419. */
  4420. clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
  4421. rtnl_lock();
  4422. bnxt_reset_task(bp);
  4423. set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
  4424. rtnl_unlock();
  4425. }
  4426. if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event))
  4427. bnxt_hwrm_port_qstats(bp);
  4428. smp_mb__before_atomic();
  4429. clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
  4430. }
  4431. static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
  4432. {
  4433. int rc;
  4434. struct bnxt *bp = netdev_priv(dev);
  4435. SET_NETDEV_DEV(dev, &pdev->dev);
  4436. /* enable device (incl. PCI PM wakeup), and bus-mastering */
  4437. rc = pci_enable_device(pdev);
  4438. if (rc) {
  4439. dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
  4440. goto init_err;
  4441. }
  4442. if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
  4443. dev_err(&pdev->dev,
  4444. "Cannot find PCI device base address, aborting\n");
  4445. rc = -ENODEV;
  4446. goto init_err_disable;
  4447. }
  4448. rc = pci_request_regions(pdev, DRV_MODULE_NAME);
  4449. if (rc) {
  4450. dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
  4451. goto init_err_disable;
  4452. }
  4453. if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
  4454. dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
  4455. dev_err(&pdev->dev, "System does not support DMA, aborting\n");
  4456. goto init_err_disable;
  4457. }
  4458. pci_set_master(pdev);
  4459. bp->dev = dev;
  4460. bp->pdev = pdev;
  4461. bp->bar0 = pci_ioremap_bar(pdev, 0);
  4462. if (!bp->bar0) {
  4463. dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
  4464. rc = -ENOMEM;
  4465. goto init_err_release;
  4466. }
  4467. bp->bar1 = pci_ioremap_bar(pdev, 2);
  4468. if (!bp->bar1) {
  4469. dev_err(&pdev->dev, "Cannot map doorbell registers, aborting\n");
  4470. rc = -ENOMEM;
  4471. goto init_err_release;
  4472. }
  4473. bp->bar2 = pci_ioremap_bar(pdev, 4);
  4474. if (!bp->bar2) {
  4475. dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
  4476. rc = -ENOMEM;
  4477. goto init_err_release;
  4478. }
  4479. pci_enable_pcie_error_reporting(pdev);
  4480. INIT_WORK(&bp->sp_task, bnxt_sp_task);
  4481. spin_lock_init(&bp->ntp_fltr_lock);
  4482. bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
  4483. bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
  4484. /* tick values in micro seconds */
  4485. bp->rx_coal_ticks = 12;
  4486. bp->rx_coal_bufs = 30;
  4487. bp->rx_coal_ticks_irq = 1;
  4488. bp->rx_coal_bufs_irq = 2;
  4489. bp->tx_coal_ticks = 25;
  4490. bp->tx_coal_bufs = 30;
  4491. bp->tx_coal_ticks_irq = 2;
  4492. bp->tx_coal_bufs_irq = 2;
  4493. init_timer(&bp->timer);
  4494. bp->timer.data = (unsigned long)bp;
  4495. bp->timer.function = bnxt_timer;
  4496. bp->current_interval = BNXT_TIMER_INTERVAL;
  4497. clear_bit(BNXT_STATE_OPEN, &bp->state);
  4498. return 0;
  4499. init_err_release:
  4500. if (bp->bar2) {
  4501. pci_iounmap(pdev, bp->bar2);
  4502. bp->bar2 = NULL;
  4503. }
  4504. if (bp->bar1) {
  4505. pci_iounmap(pdev, bp->bar1);
  4506. bp->bar1 = NULL;
  4507. }
  4508. if (bp->bar0) {
  4509. pci_iounmap(pdev, bp->bar0);
  4510. bp->bar0 = NULL;
  4511. }
  4512. pci_release_regions(pdev);
  4513. init_err_disable:
  4514. pci_disable_device(pdev);
  4515. init_err:
  4516. return rc;
  4517. }
  4518. /* rtnl_lock held */
  4519. static int bnxt_change_mac_addr(struct net_device *dev, void *p)
  4520. {
  4521. struct sockaddr *addr = p;
  4522. struct bnxt *bp = netdev_priv(dev);
  4523. int rc = 0;
  4524. if (!is_valid_ether_addr(addr->sa_data))
  4525. return -EADDRNOTAVAIL;
  4526. #ifdef CONFIG_BNXT_SRIOV
  4527. if (BNXT_VF(bp) && is_valid_ether_addr(bp->vf.mac_addr))
  4528. return -EADDRNOTAVAIL;
  4529. #endif
  4530. if (ether_addr_equal(addr->sa_data, dev->dev_addr))
  4531. return 0;
  4532. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  4533. if (netif_running(dev)) {
  4534. bnxt_close_nic(bp, false, false);
  4535. rc = bnxt_open_nic(bp, false, false);
  4536. }
  4537. return rc;
  4538. }
  4539. /* rtnl_lock held */
  4540. static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
  4541. {
  4542. struct bnxt *bp = netdev_priv(dev);
  4543. if (new_mtu < 60 || new_mtu > 9000)
  4544. return -EINVAL;
  4545. if (netif_running(dev))
  4546. bnxt_close_nic(bp, false, false);
  4547. dev->mtu = new_mtu;
  4548. bnxt_set_ring_params(bp);
  4549. if (netif_running(dev))
  4550. return bnxt_open_nic(bp, false, false);
  4551. return 0;
  4552. }
  4553. static int bnxt_setup_tc(struct net_device *dev, u32 handle, __be16 proto,
  4554. struct tc_to_netdev *ntc)
  4555. {
  4556. struct bnxt *bp = netdev_priv(dev);
  4557. u8 tc;
  4558. if (ntc->type != TC_SETUP_MQPRIO)
  4559. return -EINVAL;
  4560. tc = ntc->tc;
  4561. if (tc > bp->max_tc) {
  4562. netdev_err(dev, "too many traffic classes requested: %d Max supported is %d\n",
  4563. tc, bp->max_tc);
  4564. return -EINVAL;
  4565. }
  4566. if (netdev_get_num_tc(dev) == tc)
  4567. return 0;
  4568. if (tc) {
  4569. int max_rx_rings, max_tx_rings, rc;
  4570. bool sh = false;
  4571. if (bp->flags & BNXT_FLAG_SHARED_RINGS)
  4572. sh = true;
  4573. rc = bnxt_get_max_rings(bp, &max_rx_rings, &max_tx_rings, sh);
  4574. if (rc || bp->tx_nr_rings_per_tc * tc > max_tx_rings)
  4575. return -ENOMEM;
  4576. }
  4577. /* Needs to close the device and do hw resource re-allocations */
  4578. if (netif_running(bp->dev))
  4579. bnxt_close_nic(bp, true, false);
  4580. if (tc) {
  4581. bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
  4582. netdev_set_num_tc(dev, tc);
  4583. } else {
  4584. bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
  4585. netdev_reset_tc(dev);
  4586. }
  4587. bp->cp_nr_rings = max_t(int, bp->tx_nr_rings, bp->rx_nr_rings);
  4588. bp->num_stat_ctxs = bp->cp_nr_rings;
  4589. if (netif_running(bp->dev))
  4590. return bnxt_open_nic(bp, true, false);
  4591. return 0;
  4592. }
  4593. #ifdef CONFIG_RFS_ACCEL
  4594. static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
  4595. struct bnxt_ntuple_filter *f2)
  4596. {
  4597. struct flow_keys *keys1 = &f1->fkeys;
  4598. struct flow_keys *keys2 = &f2->fkeys;
  4599. if (keys1->addrs.v4addrs.src == keys2->addrs.v4addrs.src &&
  4600. keys1->addrs.v4addrs.dst == keys2->addrs.v4addrs.dst &&
  4601. keys1->ports.ports == keys2->ports.ports &&
  4602. keys1->basic.ip_proto == keys2->basic.ip_proto &&
  4603. keys1->basic.n_proto == keys2->basic.n_proto &&
  4604. ether_addr_equal(f1->src_mac_addr, f2->src_mac_addr))
  4605. return true;
  4606. return false;
  4607. }
  4608. static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
  4609. u16 rxq_index, u32 flow_id)
  4610. {
  4611. struct bnxt *bp = netdev_priv(dev);
  4612. struct bnxt_ntuple_filter *fltr, *new_fltr;
  4613. struct flow_keys *fkeys;
  4614. struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
  4615. int rc = 0, idx, bit_id;
  4616. struct hlist_head *head;
  4617. if (skb->encapsulation)
  4618. return -EPROTONOSUPPORT;
  4619. new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
  4620. if (!new_fltr)
  4621. return -ENOMEM;
  4622. fkeys = &new_fltr->fkeys;
  4623. if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
  4624. rc = -EPROTONOSUPPORT;
  4625. goto err_free;
  4626. }
  4627. if ((fkeys->basic.n_proto != htons(ETH_P_IP)) ||
  4628. ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
  4629. (fkeys->basic.ip_proto != IPPROTO_UDP))) {
  4630. rc = -EPROTONOSUPPORT;
  4631. goto err_free;
  4632. }
  4633. memcpy(new_fltr->src_mac_addr, eth->h_source, ETH_ALEN);
  4634. idx = skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
  4635. head = &bp->ntp_fltr_hash_tbl[idx];
  4636. rcu_read_lock();
  4637. hlist_for_each_entry_rcu(fltr, head, hash) {
  4638. if (bnxt_fltr_match(fltr, new_fltr)) {
  4639. rcu_read_unlock();
  4640. rc = 0;
  4641. goto err_free;
  4642. }
  4643. }
  4644. rcu_read_unlock();
  4645. spin_lock_bh(&bp->ntp_fltr_lock);
  4646. bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
  4647. BNXT_NTP_FLTR_MAX_FLTR, 0);
  4648. if (bit_id < 0) {
  4649. spin_unlock_bh(&bp->ntp_fltr_lock);
  4650. rc = -ENOMEM;
  4651. goto err_free;
  4652. }
  4653. new_fltr->sw_id = (u16)bit_id;
  4654. new_fltr->flow_id = flow_id;
  4655. new_fltr->rxq = rxq_index;
  4656. hlist_add_head_rcu(&new_fltr->hash, head);
  4657. bp->ntp_fltr_count++;
  4658. spin_unlock_bh(&bp->ntp_fltr_lock);
  4659. set_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event);
  4660. schedule_work(&bp->sp_task);
  4661. return new_fltr->sw_id;
  4662. err_free:
  4663. kfree(new_fltr);
  4664. return rc;
  4665. }
  4666. static void bnxt_cfg_ntp_filters(struct bnxt *bp)
  4667. {
  4668. int i;
  4669. for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
  4670. struct hlist_head *head;
  4671. struct hlist_node *tmp;
  4672. struct bnxt_ntuple_filter *fltr;
  4673. int rc;
  4674. head = &bp->ntp_fltr_hash_tbl[i];
  4675. hlist_for_each_entry_safe(fltr, tmp, head, hash) {
  4676. bool del = false;
  4677. if (test_bit(BNXT_FLTR_VALID, &fltr->state)) {
  4678. if (rps_may_expire_flow(bp->dev, fltr->rxq,
  4679. fltr->flow_id,
  4680. fltr->sw_id)) {
  4681. bnxt_hwrm_cfa_ntuple_filter_free(bp,
  4682. fltr);
  4683. del = true;
  4684. }
  4685. } else {
  4686. rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
  4687. fltr);
  4688. if (rc)
  4689. del = true;
  4690. else
  4691. set_bit(BNXT_FLTR_VALID, &fltr->state);
  4692. }
  4693. if (del) {
  4694. spin_lock_bh(&bp->ntp_fltr_lock);
  4695. hlist_del_rcu(&fltr->hash);
  4696. bp->ntp_fltr_count--;
  4697. spin_unlock_bh(&bp->ntp_fltr_lock);
  4698. synchronize_rcu();
  4699. clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
  4700. kfree(fltr);
  4701. }
  4702. }
  4703. }
  4704. if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
  4705. netdev_info(bp->dev, "Receive PF driver unload event!");
  4706. }
  4707. #else
  4708. static void bnxt_cfg_ntp_filters(struct bnxt *bp)
  4709. {
  4710. }
  4711. #endif /* CONFIG_RFS_ACCEL */
  4712. static void bnxt_add_vxlan_port(struct net_device *dev, sa_family_t sa_family,
  4713. __be16 port)
  4714. {
  4715. struct bnxt *bp = netdev_priv(dev);
  4716. if (!netif_running(dev))
  4717. return;
  4718. if (sa_family != AF_INET6 && sa_family != AF_INET)
  4719. return;
  4720. if (bp->vxlan_port_cnt && bp->vxlan_port != port)
  4721. return;
  4722. bp->vxlan_port_cnt++;
  4723. if (bp->vxlan_port_cnt == 1) {
  4724. bp->vxlan_port = port;
  4725. set_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event);
  4726. schedule_work(&bp->sp_task);
  4727. }
  4728. }
  4729. static void bnxt_del_vxlan_port(struct net_device *dev, sa_family_t sa_family,
  4730. __be16 port)
  4731. {
  4732. struct bnxt *bp = netdev_priv(dev);
  4733. if (!netif_running(dev))
  4734. return;
  4735. if (sa_family != AF_INET6 && sa_family != AF_INET)
  4736. return;
  4737. if (bp->vxlan_port_cnt && bp->vxlan_port == port) {
  4738. bp->vxlan_port_cnt--;
  4739. if (bp->vxlan_port_cnt == 0) {
  4740. set_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event);
  4741. schedule_work(&bp->sp_task);
  4742. }
  4743. }
  4744. }
  4745. static const struct net_device_ops bnxt_netdev_ops = {
  4746. .ndo_open = bnxt_open,
  4747. .ndo_start_xmit = bnxt_start_xmit,
  4748. .ndo_stop = bnxt_close,
  4749. .ndo_get_stats64 = bnxt_get_stats64,
  4750. .ndo_set_rx_mode = bnxt_set_rx_mode,
  4751. .ndo_do_ioctl = bnxt_ioctl,
  4752. .ndo_validate_addr = eth_validate_addr,
  4753. .ndo_set_mac_address = bnxt_change_mac_addr,
  4754. .ndo_change_mtu = bnxt_change_mtu,
  4755. .ndo_fix_features = bnxt_fix_features,
  4756. .ndo_set_features = bnxt_set_features,
  4757. .ndo_tx_timeout = bnxt_tx_timeout,
  4758. #ifdef CONFIG_BNXT_SRIOV
  4759. .ndo_get_vf_config = bnxt_get_vf_config,
  4760. .ndo_set_vf_mac = bnxt_set_vf_mac,
  4761. .ndo_set_vf_vlan = bnxt_set_vf_vlan,
  4762. .ndo_set_vf_rate = bnxt_set_vf_bw,
  4763. .ndo_set_vf_link_state = bnxt_set_vf_link_state,
  4764. .ndo_set_vf_spoofchk = bnxt_set_vf_spoofchk,
  4765. #endif
  4766. #ifdef CONFIG_NET_POLL_CONTROLLER
  4767. .ndo_poll_controller = bnxt_poll_controller,
  4768. #endif
  4769. .ndo_setup_tc = bnxt_setup_tc,
  4770. #ifdef CONFIG_RFS_ACCEL
  4771. .ndo_rx_flow_steer = bnxt_rx_flow_steer,
  4772. #endif
  4773. .ndo_add_vxlan_port = bnxt_add_vxlan_port,
  4774. .ndo_del_vxlan_port = bnxt_del_vxlan_port,
  4775. #ifdef CONFIG_NET_RX_BUSY_POLL
  4776. .ndo_busy_poll = bnxt_busy_poll,
  4777. #endif
  4778. };
  4779. static void bnxt_remove_one(struct pci_dev *pdev)
  4780. {
  4781. struct net_device *dev = pci_get_drvdata(pdev);
  4782. struct bnxt *bp = netdev_priv(dev);
  4783. if (BNXT_PF(bp))
  4784. bnxt_sriov_disable(bp);
  4785. pci_disable_pcie_error_reporting(pdev);
  4786. unregister_netdev(dev);
  4787. cancel_work_sync(&bp->sp_task);
  4788. bp->sp_event = 0;
  4789. bnxt_hwrm_func_drv_unrgtr(bp);
  4790. bnxt_free_hwrm_resources(bp);
  4791. pci_iounmap(pdev, bp->bar2);
  4792. pci_iounmap(pdev, bp->bar1);
  4793. pci_iounmap(pdev, bp->bar0);
  4794. free_netdev(dev);
  4795. pci_release_regions(pdev);
  4796. pci_disable_device(pdev);
  4797. }
  4798. static int bnxt_probe_phy(struct bnxt *bp)
  4799. {
  4800. int rc = 0;
  4801. struct bnxt_link_info *link_info = &bp->link_info;
  4802. rc = bnxt_update_link(bp, false);
  4803. if (rc) {
  4804. netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
  4805. rc);
  4806. return rc;
  4807. }
  4808. /*initialize the ethool setting copy with NVM settings */
  4809. if (BNXT_AUTO_MODE(link_info->auto_mode)) {
  4810. link_info->autoneg = BNXT_AUTONEG_SPEED |
  4811. BNXT_AUTONEG_FLOW_CTRL;
  4812. link_info->advertising = link_info->auto_link_speeds;
  4813. link_info->req_flow_ctrl = link_info->auto_pause_setting;
  4814. } else {
  4815. link_info->req_link_speed = link_info->force_link_speed;
  4816. link_info->req_duplex = link_info->duplex_setting;
  4817. link_info->req_flow_ctrl = link_info->force_pause_setting;
  4818. }
  4819. return rc;
  4820. }
  4821. static int bnxt_get_max_irq(struct pci_dev *pdev)
  4822. {
  4823. u16 ctrl;
  4824. if (!pdev->msix_cap)
  4825. return 1;
  4826. pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
  4827. return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
  4828. }
  4829. static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
  4830. int *max_cp)
  4831. {
  4832. int max_ring_grps = 0;
  4833. #ifdef CONFIG_BNXT_SRIOV
  4834. if (!BNXT_PF(bp)) {
  4835. *max_tx = bp->vf.max_tx_rings;
  4836. *max_rx = bp->vf.max_rx_rings;
  4837. *max_cp = min_t(int, bp->vf.max_irqs, bp->vf.max_cp_rings);
  4838. *max_cp = min_t(int, *max_cp, bp->vf.max_stat_ctxs);
  4839. max_ring_grps = bp->vf.max_hw_ring_grps;
  4840. } else
  4841. #endif
  4842. {
  4843. *max_tx = bp->pf.max_tx_rings;
  4844. *max_rx = bp->pf.max_rx_rings;
  4845. *max_cp = min_t(int, bp->pf.max_irqs, bp->pf.max_cp_rings);
  4846. *max_cp = min_t(int, *max_cp, bp->pf.max_stat_ctxs);
  4847. max_ring_grps = bp->pf.max_hw_ring_grps;
  4848. }
  4849. if (bp->flags & BNXT_FLAG_AGG_RINGS)
  4850. *max_rx >>= 1;
  4851. *max_rx = min_t(int, *max_rx, max_ring_grps);
  4852. }
  4853. int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
  4854. {
  4855. int rx, tx, cp;
  4856. _bnxt_get_max_rings(bp, &rx, &tx, &cp);
  4857. if (!rx || !tx || !cp)
  4858. return -ENOMEM;
  4859. *max_rx = rx;
  4860. *max_tx = tx;
  4861. return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
  4862. }
  4863. static int bnxt_set_dflt_rings(struct bnxt *bp)
  4864. {
  4865. int dflt_rings, max_rx_rings, max_tx_rings, rc;
  4866. bool sh = true;
  4867. if (sh)
  4868. bp->flags |= BNXT_FLAG_SHARED_RINGS;
  4869. dflt_rings = netif_get_num_default_rss_queues();
  4870. rc = bnxt_get_max_rings(bp, &max_rx_rings, &max_tx_rings, sh);
  4871. if (rc)
  4872. return rc;
  4873. bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
  4874. bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
  4875. bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
  4876. bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
  4877. bp->tx_nr_rings + bp->rx_nr_rings;
  4878. bp->num_stat_ctxs = bp->cp_nr_rings;
  4879. return rc;
  4880. }
  4881. static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  4882. {
  4883. static int version_printed;
  4884. struct net_device *dev;
  4885. struct bnxt *bp;
  4886. int rc, max_irqs;
  4887. if (version_printed++ == 0)
  4888. pr_info("%s", version);
  4889. max_irqs = bnxt_get_max_irq(pdev);
  4890. dev = alloc_etherdev_mq(sizeof(*bp), max_irqs);
  4891. if (!dev)
  4892. return -ENOMEM;
  4893. bp = netdev_priv(dev);
  4894. if (bnxt_vf_pciid(ent->driver_data))
  4895. bp->flags |= BNXT_FLAG_VF;
  4896. if (pdev->msix_cap)
  4897. bp->flags |= BNXT_FLAG_MSIX_CAP;
  4898. rc = bnxt_init_board(pdev, dev);
  4899. if (rc < 0)
  4900. goto init_err_free;
  4901. dev->netdev_ops = &bnxt_netdev_ops;
  4902. dev->watchdog_timeo = BNXT_TX_TIMEOUT;
  4903. dev->ethtool_ops = &bnxt_ethtool_ops;
  4904. pci_set_drvdata(pdev, dev);
  4905. dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
  4906. NETIF_F_TSO | NETIF_F_TSO6 |
  4907. NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
  4908. NETIF_F_GSO_IPIP | NETIF_F_GSO_SIT |
  4909. NETIF_F_RXHASH |
  4910. NETIF_F_RXCSUM | NETIF_F_LRO | NETIF_F_GRO;
  4911. dev->hw_enc_features =
  4912. NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
  4913. NETIF_F_TSO | NETIF_F_TSO6 |
  4914. NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
  4915. NETIF_F_GSO_IPIP | NETIF_F_GSO_SIT;
  4916. dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
  4917. dev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX |
  4918. NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX;
  4919. dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
  4920. dev->priv_flags |= IFF_UNICAST_FLT;
  4921. #ifdef CONFIG_BNXT_SRIOV
  4922. init_waitqueue_head(&bp->sriov_cfg_wait);
  4923. #endif
  4924. rc = bnxt_alloc_hwrm_resources(bp);
  4925. if (rc)
  4926. goto init_err;
  4927. mutex_init(&bp->hwrm_cmd_lock);
  4928. bnxt_hwrm_ver_get(bp);
  4929. rc = bnxt_hwrm_func_drv_rgtr(bp);
  4930. if (rc)
  4931. goto init_err;
  4932. /* Get the MAX capabilities for this function */
  4933. rc = bnxt_hwrm_func_qcaps(bp);
  4934. if (rc) {
  4935. netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
  4936. rc);
  4937. rc = -1;
  4938. goto init_err;
  4939. }
  4940. rc = bnxt_hwrm_queue_qportcfg(bp);
  4941. if (rc) {
  4942. netdev_err(bp->dev, "hwrm query qportcfg failure rc: %x\n",
  4943. rc);
  4944. rc = -1;
  4945. goto init_err;
  4946. }
  4947. bnxt_set_tpa_flags(bp);
  4948. bnxt_set_ring_params(bp);
  4949. if (BNXT_PF(bp))
  4950. bp->pf.max_irqs = max_irqs;
  4951. #if defined(CONFIG_BNXT_SRIOV)
  4952. else
  4953. bp->vf.max_irqs = max_irqs;
  4954. #endif
  4955. bnxt_set_dflt_rings(bp);
  4956. if (BNXT_PF(bp)) {
  4957. dev->hw_features |= NETIF_F_NTUPLE;
  4958. if (bnxt_rfs_capable(bp)) {
  4959. bp->flags |= BNXT_FLAG_RFS;
  4960. dev->features |= NETIF_F_NTUPLE;
  4961. }
  4962. }
  4963. if (dev->hw_features & NETIF_F_HW_VLAN_CTAG_RX)
  4964. bp->flags |= BNXT_FLAG_STRIP_VLAN;
  4965. rc = bnxt_probe_phy(bp);
  4966. if (rc)
  4967. goto init_err;
  4968. rc = register_netdev(dev);
  4969. if (rc)
  4970. goto init_err;
  4971. netdev_info(dev, "%s found at mem %lx, node addr %pM\n",
  4972. board_info[ent->driver_data].name,
  4973. (long)pci_resource_start(pdev, 0), dev->dev_addr);
  4974. return 0;
  4975. init_err:
  4976. pci_iounmap(pdev, bp->bar0);
  4977. pci_release_regions(pdev);
  4978. pci_disable_device(pdev);
  4979. init_err_free:
  4980. free_netdev(dev);
  4981. return rc;
  4982. }
  4983. /**
  4984. * bnxt_io_error_detected - called when PCI error is detected
  4985. * @pdev: Pointer to PCI device
  4986. * @state: The current pci connection state
  4987. *
  4988. * This function is called after a PCI bus error affecting
  4989. * this device has been detected.
  4990. */
  4991. static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
  4992. pci_channel_state_t state)
  4993. {
  4994. struct net_device *netdev = pci_get_drvdata(pdev);
  4995. netdev_info(netdev, "PCI I/O error detected\n");
  4996. rtnl_lock();
  4997. netif_device_detach(netdev);
  4998. if (state == pci_channel_io_perm_failure) {
  4999. rtnl_unlock();
  5000. return PCI_ERS_RESULT_DISCONNECT;
  5001. }
  5002. if (netif_running(netdev))
  5003. bnxt_close(netdev);
  5004. pci_disable_device(pdev);
  5005. rtnl_unlock();
  5006. /* Request a slot slot reset. */
  5007. return PCI_ERS_RESULT_NEED_RESET;
  5008. }
  5009. /**
  5010. * bnxt_io_slot_reset - called after the pci bus has been reset.
  5011. * @pdev: Pointer to PCI device
  5012. *
  5013. * Restart the card from scratch, as if from a cold-boot.
  5014. * At this point, the card has exprienced a hard reset,
  5015. * followed by fixups by BIOS, and has its config space
  5016. * set up identically to what it was at cold boot.
  5017. */
  5018. static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
  5019. {
  5020. struct net_device *netdev = pci_get_drvdata(pdev);
  5021. struct bnxt *bp = netdev_priv(netdev);
  5022. int err = 0;
  5023. pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
  5024. netdev_info(bp->dev, "PCI Slot Reset\n");
  5025. rtnl_lock();
  5026. if (pci_enable_device(pdev)) {
  5027. dev_err(&pdev->dev,
  5028. "Cannot re-enable PCI device after reset.\n");
  5029. } else {
  5030. pci_set_master(pdev);
  5031. if (netif_running(netdev))
  5032. err = bnxt_open(netdev);
  5033. if (!err)
  5034. result = PCI_ERS_RESULT_RECOVERED;
  5035. }
  5036. if (result != PCI_ERS_RESULT_RECOVERED && netif_running(netdev))
  5037. dev_close(netdev);
  5038. rtnl_unlock();
  5039. err = pci_cleanup_aer_uncorrect_error_status(pdev);
  5040. if (err) {
  5041. dev_err(&pdev->dev,
  5042. "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
  5043. err); /* non-fatal, continue */
  5044. }
  5045. return PCI_ERS_RESULT_RECOVERED;
  5046. }
  5047. /**
  5048. * bnxt_io_resume - called when traffic can start flowing again.
  5049. * @pdev: Pointer to PCI device
  5050. *
  5051. * This callback is called when the error recovery driver tells
  5052. * us that its OK to resume normal operation.
  5053. */
  5054. static void bnxt_io_resume(struct pci_dev *pdev)
  5055. {
  5056. struct net_device *netdev = pci_get_drvdata(pdev);
  5057. rtnl_lock();
  5058. netif_device_attach(netdev);
  5059. rtnl_unlock();
  5060. }
  5061. static const struct pci_error_handlers bnxt_err_handler = {
  5062. .error_detected = bnxt_io_error_detected,
  5063. .slot_reset = bnxt_io_slot_reset,
  5064. .resume = bnxt_io_resume
  5065. };
  5066. static struct pci_driver bnxt_pci_driver = {
  5067. .name = DRV_MODULE_NAME,
  5068. .id_table = bnxt_pci_tbl,
  5069. .probe = bnxt_init_one,
  5070. .remove = bnxt_remove_one,
  5071. .err_handler = &bnxt_err_handler,
  5072. #if defined(CONFIG_BNXT_SRIOV)
  5073. .sriov_configure = bnxt_sriov_configure,
  5074. #endif
  5075. };
  5076. module_pci_driver(bnxt_pci_driver);