bnxt.c 153 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152
  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 < 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. rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_CMPL, i,
  2780. INVALID_STATS_CTX_ID);
  2781. if (rc)
  2782. goto err_out;
  2783. cpr->cp_doorbell = bp->bar1 + i * 0x80;
  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. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VER_GET, -1, -1);
  3151. req.hwrm_intf_maj = HWRM_VERSION_MAJOR;
  3152. req.hwrm_intf_min = HWRM_VERSION_MINOR;
  3153. req.hwrm_intf_upd = HWRM_VERSION_UPDATE;
  3154. mutex_lock(&bp->hwrm_cmd_lock);
  3155. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3156. if (rc)
  3157. goto hwrm_ver_get_exit;
  3158. memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
  3159. if (resp->hwrm_intf_maj < 1) {
  3160. netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
  3161. resp->hwrm_intf_maj, resp->hwrm_intf_min,
  3162. resp->hwrm_intf_upd);
  3163. netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
  3164. }
  3165. snprintf(bp->fw_ver_str, BC_HWRM_STR_LEN, "%d.%d.%d/%d.%d.%d",
  3166. resp->hwrm_fw_maj, resp->hwrm_fw_min, resp->hwrm_fw_bld,
  3167. resp->hwrm_intf_maj, resp->hwrm_intf_min, resp->hwrm_intf_upd);
  3168. bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
  3169. if (!bp->hwrm_cmd_timeout)
  3170. bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
  3171. hwrm_ver_get_exit:
  3172. mutex_unlock(&bp->hwrm_cmd_lock);
  3173. return rc;
  3174. }
  3175. static int bnxt_hwrm_port_qstats(struct bnxt *bp)
  3176. {
  3177. int rc;
  3178. struct bnxt_pf_info *pf = &bp->pf;
  3179. struct hwrm_port_qstats_input req = {0};
  3180. if (!(bp->flags & BNXT_FLAG_PORT_STATS))
  3181. return 0;
  3182. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_QSTATS, -1, -1);
  3183. req.port_id = cpu_to_le16(pf->port_id);
  3184. req.tx_stat_host_addr = cpu_to_le64(bp->hw_tx_port_stats_map);
  3185. req.rx_stat_host_addr = cpu_to_le64(bp->hw_rx_port_stats_map);
  3186. rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3187. return rc;
  3188. }
  3189. static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
  3190. {
  3191. if (bp->vxlan_port_cnt) {
  3192. bnxt_hwrm_tunnel_dst_port_free(
  3193. bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
  3194. }
  3195. bp->vxlan_port_cnt = 0;
  3196. if (bp->nge_port_cnt) {
  3197. bnxt_hwrm_tunnel_dst_port_free(
  3198. bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
  3199. }
  3200. bp->nge_port_cnt = 0;
  3201. }
  3202. static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
  3203. {
  3204. int rc, i;
  3205. u32 tpa_flags = 0;
  3206. if (set_tpa)
  3207. tpa_flags = bp->flags & BNXT_FLAG_TPA;
  3208. for (i = 0; i < bp->nr_vnics; i++) {
  3209. rc = bnxt_hwrm_vnic_set_tpa(bp, i, tpa_flags);
  3210. if (rc) {
  3211. netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
  3212. rc, i);
  3213. return rc;
  3214. }
  3215. }
  3216. return 0;
  3217. }
  3218. static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
  3219. {
  3220. int i;
  3221. for (i = 0; i < bp->nr_vnics; i++)
  3222. bnxt_hwrm_vnic_set_rss(bp, i, false);
  3223. }
  3224. static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
  3225. bool irq_re_init)
  3226. {
  3227. if (bp->vnic_info) {
  3228. bnxt_hwrm_clear_vnic_filter(bp);
  3229. /* clear all RSS setting before free vnic ctx */
  3230. bnxt_hwrm_clear_vnic_rss(bp);
  3231. bnxt_hwrm_vnic_ctx_free(bp);
  3232. /* before free the vnic, undo the vnic tpa settings */
  3233. if (bp->flags & BNXT_FLAG_TPA)
  3234. bnxt_set_tpa(bp, false);
  3235. bnxt_hwrm_vnic_free(bp);
  3236. }
  3237. bnxt_hwrm_ring_free(bp, close_path);
  3238. bnxt_hwrm_ring_grp_free(bp);
  3239. if (irq_re_init) {
  3240. bnxt_hwrm_stat_ctx_free(bp);
  3241. bnxt_hwrm_free_tunnel_ports(bp);
  3242. }
  3243. }
  3244. static int bnxt_setup_vnic(struct bnxt *bp, u16 vnic_id)
  3245. {
  3246. int rc;
  3247. /* allocate context for vnic */
  3248. rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id);
  3249. if (rc) {
  3250. netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
  3251. vnic_id, rc);
  3252. goto vnic_setup_err;
  3253. }
  3254. bp->rsscos_nr_ctxs++;
  3255. /* configure default vnic, ring grp */
  3256. rc = bnxt_hwrm_vnic_cfg(bp, vnic_id);
  3257. if (rc) {
  3258. netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
  3259. vnic_id, rc);
  3260. goto vnic_setup_err;
  3261. }
  3262. /* Enable RSS hashing on vnic */
  3263. rc = bnxt_hwrm_vnic_set_rss(bp, vnic_id, true);
  3264. if (rc) {
  3265. netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
  3266. vnic_id, rc);
  3267. goto vnic_setup_err;
  3268. }
  3269. if (bp->flags & BNXT_FLAG_AGG_RINGS) {
  3270. rc = bnxt_hwrm_vnic_set_hds(bp, vnic_id);
  3271. if (rc) {
  3272. netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
  3273. vnic_id, rc);
  3274. }
  3275. }
  3276. vnic_setup_err:
  3277. return rc;
  3278. }
  3279. static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
  3280. {
  3281. #ifdef CONFIG_RFS_ACCEL
  3282. int i, rc = 0;
  3283. for (i = 0; i < bp->rx_nr_rings; i++) {
  3284. u16 vnic_id = i + 1;
  3285. u16 ring_id = i;
  3286. if (vnic_id >= bp->nr_vnics)
  3287. break;
  3288. bp->vnic_info[vnic_id].flags |= BNXT_VNIC_RFS_FLAG;
  3289. rc = bnxt_hwrm_vnic_alloc(bp, vnic_id, ring_id, 1);
  3290. if (rc) {
  3291. netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
  3292. vnic_id, rc);
  3293. break;
  3294. }
  3295. rc = bnxt_setup_vnic(bp, vnic_id);
  3296. if (rc)
  3297. break;
  3298. }
  3299. return rc;
  3300. #else
  3301. return 0;
  3302. #endif
  3303. }
  3304. static int bnxt_cfg_rx_mode(struct bnxt *);
  3305. static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
  3306. {
  3307. int rc = 0;
  3308. if (irq_re_init) {
  3309. rc = bnxt_hwrm_stat_ctx_alloc(bp);
  3310. if (rc) {
  3311. netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
  3312. rc);
  3313. goto err_out;
  3314. }
  3315. }
  3316. rc = bnxt_hwrm_ring_alloc(bp);
  3317. if (rc) {
  3318. netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
  3319. goto err_out;
  3320. }
  3321. rc = bnxt_hwrm_ring_grp_alloc(bp);
  3322. if (rc) {
  3323. netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
  3324. goto err_out;
  3325. }
  3326. /* default vnic 0 */
  3327. rc = bnxt_hwrm_vnic_alloc(bp, 0, 0, bp->rx_nr_rings);
  3328. if (rc) {
  3329. netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
  3330. goto err_out;
  3331. }
  3332. rc = bnxt_setup_vnic(bp, 0);
  3333. if (rc)
  3334. goto err_out;
  3335. if (bp->flags & BNXT_FLAG_RFS) {
  3336. rc = bnxt_alloc_rfs_vnics(bp);
  3337. if (rc)
  3338. goto err_out;
  3339. }
  3340. if (bp->flags & BNXT_FLAG_TPA) {
  3341. rc = bnxt_set_tpa(bp, true);
  3342. if (rc)
  3343. goto err_out;
  3344. }
  3345. if (BNXT_VF(bp))
  3346. bnxt_update_vf_mac(bp);
  3347. /* Filter for default vnic 0 */
  3348. rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
  3349. if (rc) {
  3350. netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
  3351. goto err_out;
  3352. }
  3353. bp->vnic_info[0].uc_filter_count = 1;
  3354. bp->vnic_info[0].rx_mask = CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
  3355. if ((bp->dev->flags & IFF_PROMISC) && BNXT_PF(bp))
  3356. bp->vnic_info[0].rx_mask |=
  3357. CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
  3358. rc = bnxt_cfg_rx_mode(bp);
  3359. if (rc)
  3360. goto err_out;
  3361. rc = bnxt_hwrm_set_coal(bp);
  3362. if (rc)
  3363. netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
  3364. rc);
  3365. return 0;
  3366. err_out:
  3367. bnxt_hwrm_resource_free(bp, 0, true);
  3368. return rc;
  3369. }
  3370. static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
  3371. {
  3372. bnxt_hwrm_resource_free(bp, 1, irq_re_init);
  3373. return 0;
  3374. }
  3375. static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
  3376. {
  3377. bnxt_init_rx_rings(bp);
  3378. bnxt_init_tx_rings(bp);
  3379. bnxt_init_ring_grps(bp, irq_re_init);
  3380. bnxt_init_vnics(bp);
  3381. return bnxt_init_chip(bp, irq_re_init);
  3382. }
  3383. static void bnxt_disable_int(struct bnxt *bp)
  3384. {
  3385. int i;
  3386. if (!bp->bnapi)
  3387. return;
  3388. for (i = 0; i < bp->cp_nr_rings; i++) {
  3389. struct bnxt_napi *bnapi = bp->bnapi[i];
  3390. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  3391. BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
  3392. }
  3393. }
  3394. static void bnxt_enable_int(struct bnxt *bp)
  3395. {
  3396. int i;
  3397. atomic_set(&bp->intr_sem, 0);
  3398. for (i = 0; i < bp->cp_nr_rings; i++) {
  3399. struct bnxt_napi *bnapi = bp->bnapi[i];
  3400. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  3401. BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
  3402. }
  3403. }
  3404. static int bnxt_set_real_num_queues(struct bnxt *bp)
  3405. {
  3406. int rc;
  3407. struct net_device *dev = bp->dev;
  3408. rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings);
  3409. if (rc)
  3410. return rc;
  3411. rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
  3412. if (rc)
  3413. return rc;
  3414. #ifdef CONFIG_RFS_ACCEL
  3415. if (bp->flags & BNXT_FLAG_RFS)
  3416. dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
  3417. #endif
  3418. return rc;
  3419. }
  3420. static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
  3421. bool shared)
  3422. {
  3423. int _rx = *rx, _tx = *tx;
  3424. if (shared) {
  3425. *rx = min_t(int, _rx, max);
  3426. *tx = min_t(int, _tx, max);
  3427. } else {
  3428. if (max < 2)
  3429. return -ENOMEM;
  3430. while (_rx + _tx > max) {
  3431. if (_rx > _tx && _rx > 1)
  3432. _rx--;
  3433. else if (_tx > 1)
  3434. _tx--;
  3435. }
  3436. *rx = _rx;
  3437. *tx = _tx;
  3438. }
  3439. return 0;
  3440. }
  3441. static int bnxt_setup_msix(struct bnxt *bp)
  3442. {
  3443. struct msix_entry *msix_ent;
  3444. struct net_device *dev = bp->dev;
  3445. int i, total_vecs, rc = 0, min = 1;
  3446. const int len = sizeof(bp->irq_tbl[0].name);
  3447. bp->flags &= ~BNXT_FLAG_USING_MSIX;
  3448. total_vecs = bp->cp_nr_rings;
  3449. msix_ent = kcalloc(total_vecs, sizeof(struct msix_entry), GFP_KERNEL);
  3450. if (!msix_ent)
  3451. return -ENOMEM;
  3452. for (i = 0; i < total_vecs; i++) {
  3453. msix_ent[i].entry = i;
  3454. msix_ent[i].vector = 0;
  3455. }
  3456. if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
  3457. min = 2;
  3458. total_vecs = pci_enable_msix_range(bp->pdev, msix_ent, min, total_vecs);
  3459. if (total_vecs < 0) {
  3460. rc = -ENODEV;
  3461. goto msix_setup_exit;
  3462. }
  3463. bp->irq_tbl = kcalloc(total_vecs, sizeof(struct bnxt_irq), GFP_KERNEL);
  3464. if (bp->irq_tbl) {
  3465. int tcs;
  3466. /* Trim rings based upon num of vectors allocated */
  3467. rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
  3468. total_vecs, min == 1);
  3469. if (rc)
  3470. goto msix_setup_exit;
  3471. bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
  3472. tcs = netdev_get_num_tc(dev);
  3473. if (tcs > 1) {
  3474. bp->tx_nr_rings_per_tc = bp->tx_nr_rings / tcs;
  3475. if (bp->tx_nr_rings_per_tc == 0) {
  3476. netdev_reset_tc(dev);
  3477. bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
  3478. } else {
  3479. int i, off, count;
  3480. bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tcs;
  3481. for (i = 0; i < tcs; i++) {
  3482. count = bp->tx_nr_rings_per_tc;
  3483. off = i * count;
  3484. netdev_set_tc_queue(dev, i, count, off);
  3485. }
  3486. }
  3487. }
  3488. bp->cp_nr_rings = total_vecs;
  3489. for (i = 0; i < bp->cp_nr_rings; i++) {
  3490. char *attr;
  3491. bp->irq_tbl[i].vector = msix_ent[i].vector;
  3492. if (bp->flags & BNXT_FLAG_SHARED_RINGS)
  3493. attr = "TxRx";
  3494. else if (i < bp->rx_nr_rings)
  3495. attr = "rx";
  3496. else
  3497. attr = "tx";
  3498. snprintf(bp->irq_tbl[i].name, len,
  3499. "%s-%s-%d", dev->name, attr, i);
  3500. bp->irq_tbl[i].handler = bnxt_msix;
  3501. }
  3502. rc = bnxt_set_real_num_queues(bp);
  3503. if (rc)
  3504. goto msix_setup_exit;
  3505. } else {
  3506. rc = -ENOMEM;
  3507. goto msix_setup_exit;
  3508. }
  3509. bp->flags |= BNXT_FLAG_USING_MSIX;
  3510. kfree(msix_ent);
  3511. return 0;
  3512. msix_setup_exit:
  3513. netdev_err(bp->dev, "bnxt_setup_msix err: %x\n", rc);
  3514. pci_disable_msix(bp->pdev);
  3515. kfree(msix_ent);
  3516. return rc;
  3517. }
  3518. static int bnxt_setup_inta(struct bnxt *bp)
  3519. {
  3520. int rc;
  3521. const int len = sizeof(bp->irq_tbl[0].name);
  3522. if (netdev_get_num_tc(bp->dev))
  3523. netdev_reset_tc(bp->dev);
  3524. bp->irq_tbl = kcalloc(1, sizeof(struct bnxt_irq), GFP_KERNEL);
  3525. if (!bp->irq_tbl) {
  3526. rc = -ENOMEM;
  3527. return rc;
  3528. }
  3529. bp->rx_nr_rings = 1;
  3530. bp->tx_nr_rings = 1;
  3531. bp->cp_nr_rings = 1;
  3532. bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
  3533. bp->flags |= BNXT_FLAG_SHARED_RINGS;
  3534. bp->irq_tbl[0].vector = bp->pdev->irq;
  3535. snprintf(bp->irq_tbl[0].name, len,
  3536. "%s-%s-%d", bp->dev->name, "TxRx", 0);
  3537. bp->irq_tbl[0].handler = bnxt_inta;
  3538. rc = bnxt_set_real_num_queues(bp);
  3539. return rc;
  3540. }
  3541. static int bnxt_setup_int_mode(struct bnxt *bp)
  3542. {
  3543. int rc = 0;
  3544. if (bp->flags & BNXT_FLAG_MSIX_CAP)
  3545. rc = bnxt_setup_msix(bp);
  3546. if (!(bp->flags & BNXT_FLAG_USING_MSIX)) {
  3547. /* fallback to INTA */
  3548. rc = bnxt_setup_inta(bp);
  3549. }
  3550. return rc;
  3551. }
  3552. static void bnxt_free_irq(struct bnxt *bp)
  3553. {
  3554. struct bnxt_irq *irq;
  3555. int i;
  3556. #ifdef CONFIG_RFS_ACCEL
  3557. free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
  3558. bp->dev->rx_cpu_rmap = NULL;
  3559. #endif
  3560. if (!bp->irq_tbl)
  3561. return;
  3562. for (i = 0; i < bp->cp_nr_rings; i++) {
  3563. irq = &bp->irq_tbl[i];
  3564. if (irq->requested)
  3565. free_irq(irq->vector, bp->bnapi[i]);
  3566. irq->requested = 0;
  3567. }
  3568. if (bp->flags & BNXT_FLAG_USING_MSIX)
  3569. pci_disable_msix(bp->pdev);
  3570. kfree(bp->irq_tbl);
  3571. bp->irq_tbl = NULL;
  3572. }
  3573. static int bnxt_request_irq(struct bnxt *bp)
  3574. {
  3575. int i, j, rc = 0;
  3576. unsigned long flags = 0;
  3577. #ifdef CONFIG_RFS_ACCEL
  3578. struct cpu_rmap *rmap = bp->dev->rx_cpu_rmap;
  3579. #endif
  3580. if (!(bp->flags & BNXT_FLAG_USING_MSIX))
  3581. flags = IRQF_SHARED;
  3582. for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
  3583. struct bnxt_irq *irq = &bp->irq_tbl[i];
  3584. #ifdef CONFIG_RFS_ACCEL
  3585. if (rmap && bp->bnapi[i]->rx_ring) {
  3586. rc = irq_cpu_rmap_add(rmap, irq->vector);
  3587. if (rc)
  3588. netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
  3589. j);
  3590. j++;
  3591. }
  3592. #endif
  3593. rc = request_irq(irq->vector, irq->handler, flags, irq->name,
  3594. bp->bnapi[i]);
  3595. if (rc)
  3596. break;
  3597. irq->requested = 1;
  3598. }
  3599. return rc;
  3600. }
  3601. static void bnxt_del_napi(struct bnxt *bp)
  3602. {
  3603. int i;
  3604. if (!bp->bnapi)
  3605. return;
  3606. for (i = 0; i < bp->cp_nr_rings; i++) {
  3607. struct bnxt_napi *bnapi = bp->bnapi[i];
  3608. napi_hash_del(&bnapi->napi);
  3609. netif_napi_del(&bnapi->napi);
  3610. }
  3611. }
  3612. static void bnxt_init_napi(struct bnxt *bp)
  3613. {
  3614. int i;
  3615. struct bnxt_napi *bnapi;
  3616. if (bp->flags & BNXT_FLAG_USING_MSIX) {
  3617. for (i = 0; i < bp->cp_nr_rings; i++) {
  3618. bnapi = bp->bnapi[i];
  3619. netif_napi_add(bp->dev, &bnapi->napi,
  3620. bnxt_poll, 64);
  3621. }
  3622. } else {
  3623. bnapi = bp->bnapi[0];
  3624. netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll, 64);
  3625. }
  3626. }
  3627. static void bnxt_disable_napi(struct bnxt *bp)
  3628. {
  3629. int i;
  3630. if (!bp->bnapi)
  3631. return;
  3632. for (i = 0; i < bp->cp_nr_rings; i++) {
  3633. napi_disable(&bp->bnapi[i]->napi);
  3634. bnxt_disable_poll(bp->bnapi[i]);
  3635. }
  3636. }
  3637. static void bnxt_enable_napi(struct bnxt *bp)
  3638. {
  3639. int i;
  3640. for (i = 0; i < bp->cp_nr_rings; i++) {
  3641. bnxt_enable_poll(bp->bnapi[i]);
  3642. napi_enable(&bp->bnapi[i]->napi);
  3643. }
  3644. }
  3645. static void bnxt_tx_disable(struct bnxt *bp)
  3646. {
  3647. int i;
  3648. struct bnxt_tx_ring_info *txr;
  3649. struct netdev_queue *txq;
  3650. if (bp->tx_ring) {
  3651. for (i = 0; i < bp->tx_nr_rings; i++) {
  3652. txr = &bp->tx_ring[i];
  3653. txq = netdev_get_tx_queue(bp->dev, i);
  3654. __netif_tx_lock(txq, smp_processor_id());
  3655. txr->dev_state = BNXT_DEV_STATE_CLOSING;
  3656. __netif_tx_unlock(txq);
  3657. }
  3658. }
  3659. /* Stop all TX queues */
  3660. netif_tx_disable(bp->dev);
  3661. netif_carrier_off(bp->dev);
  3662. }
  3663. static void bnxt_tx_enable(struct bnxt *bp)
  3664. {
  3665. int i;
  3666. struct bnxt_tx_ring_info *txr;
  3667. struct netdev_queue *txq;
  3668. for (i = 0; i < bp->tx_nr_rings; i++) {
  3669. txr = &bp->tx_ring[i];
  3670. txq = netdev_get_tx_queue(bp->dev, i);
  3671. txr->dev_state = 0;
  3672. }
  3673. netif_tx_wake_all_queues(bp->dev);
  3674. if (bp->link_info.link_up)
  3675. netif_carrier_on(bp->dev);
  3676. }
  3677. static void bnxt_report_link(struct bnxt *bp)
  3678. {
  3679. if (bp->link_info.link_up) {
  3680. const char *duplex;
  3681. const char *flow_ctrl;
  3682. u16 speed;
  3683. netif_carrier_on(bp->dev);
  3684. if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
  3685. duplex = "full";
  3686. else
  3687. duplex = "half";
  3688. if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
  3689. flow_ctrl = "ON - receive & transmit";
  3690. else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
  3691. flow_ctrl = "ON - transmit";
  3692. else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
  3693. flow_ctrl = "ON - receive";
  3694. else
  3695. flow_ctrl = "none";
  3696. speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
  3697. netdev_info(bp->dev, "NIC Link is Up, %d Mbps %s duplex, Flow control: %s\n",
  3698. speed, duplex, flow_ctrl);
  3699. } else {
  3700. netif_carrier_off(bp->dev);
  3701. netdev_err(bp->dev, "NIC Link is Down\n");
  3702. }
  3703. }
  3704. static int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
  3705. {
  3706. int rc = 0;
  3707. struct bnxt_link_info *link_info = &bp->link_info;
  3708. struct hwrm_port_phy_qcfg_input req = {0};
  3709. struct hwrm_port_phy_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
  3710. u8 link_up = link_info->link_up;
  3711. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCFG, -1, -1);
  3712. mutex_lock(&bp->hwrm_cmd_lock);
  3713. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3714. if (rc) {
  3715. mutex_unlock(&bp->hwrm_cmd_lock);
  3716. return rc;
  3717. }
  3718. memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
  3719. link_info->phy_link_status = resp->link;
  3720. link_info->duplex = resp->duplex;
  3721. link_info->pause = resp->pause;
  3722. link_info->auto_mode = resp->auto_mode;
  3723. link_info->auto_pause_setting = resp->auto_pause;
  3724. link_info->lp_pause = resp->link_partner_adv_pause;
  3725. link_info->force_pause_setting = resp->force_pause;
  3726. link_info->duplex_setting = resp->duplex;
  3727. if (link_info->phy_link_status == BNXT_LINK_LINK)
  3728. link_info->link_speed = le16_to_cpu(resp->link_speed);
  3729. else
  3730. link_info->link_speed = 0;
  3731. link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
  3732. link_info->auto_link_speed = le16_to_cpu(resp->auto_link_speed);
  3733. link_info->support_speeds = le16_to_cpu(resp->support_speeds);
  3734. link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
  3735. link_info->lp_auto_link_speeds =
  3736. le16_to_cpu(resp->link_partner_adv_speeds);
  3737. link_info->preemphasis = le32_to_cpu(resp->preemphasis);
  3738. link_info->phy_ver[0] = resp->phy_maj;
  3739. link_info->phy_ver[1] = resp->phy_min;
  3740. link_info->phy_ver[2] = resp->phy_bld;
  3741. link_info->media_type = resp->media_type;
  3742. link_info->transceiver = resp->transceiver_type;
  3743. link_info->phy_addr = resp->phy_addr;
  3744. /* TODO: need to add more logic to report VF link */
  3745. if (chng_link_state) {
  3746. if (link_info->phy_link_status == BNXT_LINK_LINK)
  3747. link_info->link_up = 1;
  3748. else
  3749. link_info->link_up = 0;
  3750. if (link_up != link_info->link_up)
  3751. bnxt_report_link(bp);
  3752. } else {
  3753. /* alwasy link down if not require to update link state */
  3754. link_info->link_up = 0;
  3755. }
  3756. mutex_unlock(&bp->hwrm_cmd_lock);
  3757. return 0;
  3758. }
  3759. static void
  3760. bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
  3761. {
  3762. if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
  3763. if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
  3764. req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
  3765. if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
  3766. req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
  3767. req->enables |=
  3768. cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
  3769. } else {
  3770. if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
  3771. req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
  3772. if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
  3773. req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
  3774. req->enables |=
  3775. cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
  3776. }
  3777. }
  3778. static void bnxt_hwrm_set_link_common(struct bnxt *bp,
  3779. struct hwrm_port_phy_cfg_input *req)
  3780. {
  3781. u8 autoneg = bp->link_info.autoneg;
  3782. u16 fw_link_speed = bp->link_info.req_link_speed;
  3783. u32 advertising = bp->link_info.advertising;
  3784. if (autoneg & BNXT_AUTONEG_SPEED) {
  3785. req->auto_mode |=
  3786. PORT_PHY_CFG_REQ_AUTO_MODE_MASK;
  3787. req->enables |= cpu_to_le32(
  3788. PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
  3789. req->auto_link_speed_mask = cpu_to_le16(advertising);
  3790. req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
  3791. req->flags |=
  3792. cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
  3793. } else {
  3794. req->force_link_speed = cpu_to_le16(fw_link_speed);
  3795. req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
  3796. }
  3797. /* currently don't support half duplex */
  3798. req->auto_duplex = PORT_PHY_CFG_REQ_AUTO_DUPLEX_FULL;
  3799. req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_DUPLEX);
  3800. /* tell chimp that the setting takes effect immediately */
  3801. req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
  3802. }
  3803. int bnxt_hwrm_set_pause(struct bnxt *bp)
  3804. {
  3805. struct hwrm_port_phy_cfg_input req = {0};
  3806. int rc;
  3807. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
  3808. bnxt_hwrm_set_pause_common(bp, &req);
  3809. if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
  3810. bp->link_info.force_link_chng)
  3811. bnxt_hwrm_set_link_common(bp, &req);
  3812. mutex_lock(&bp->hwrm_cmd_lock);
  3813. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3814. if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
  3815. /* since changing of pause setting doesn't trigger any link
  3816. * change event, the driver needs to update the current pause
  3817. * result upon successfully return of the phy_cfg command
  3818. */
  3819. bp->link_info.pause =
  3820. bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
  3821. bp->link_info.auto_pause_setting = 0;
  3822. if (!bp->link_info.force_link_chng)
  3823. bnxt_report_link(bp);
  3824. }
  3825. bp->link_info.force_link_chng = false;
  3826. mutex_unlock(&bp->hwrm_cmd_lock);
  3827. return rc;
  3828. }
  3829. int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause)
  3830. {
  3831. struct hwrm_port_phy_cfg_input req = {0};
  3832. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
  3833. if (set_pause)
  3834. bnxt_hwrm_set_pause_common(bp, &req);
  3835. bnxt_hwrm_set_link_common(bp, &req);
  3836. return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3837. }
  3838. static int bnxt_update_phy_setting(struct bnxt *bp)
  3839. {
  3840. int rc;
  3841. bool update_link = false;
  3842. bool update_pause = false;
  3843. struct bnxt_link_info *link_info = &bp->link_info;
  3844. rc = bnxt_update_link(bp, true);
  3845. if (rc) {
  3846. netdev_err(bp->dev, "failed to update link (rc: %x)\n",
  3847. rc);
  3848. return rc;
  3849. }
  3850. if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
  3851. link_info->auto_pause_setting != link_info->req_flow_ctrl)
  3852. update_pause = true;
  3853. if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
  3854. link_info->force_pause_setting != link_info->req_flow_ctrl)
  3855. update_pause = true;
  3856. if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
  3857. if (BNXT_AUTO_MODE(link_info->auto_mode))
  3858. update_link = true;
  3859. if (link_info->req_link_speed != link_info->force_link_speed)
  3860. update_link = true;
  3861. if (link_info->req_duplex != link_info->duplex_setting)
  3862. update_link = true;
  3863. } else {
  3864. if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
  3865. update_link = true;
  3866. if (link_info->advertising != link_info->auto_link_speeds)
  3867. update_link = true;
  3868. }
  3869. if (update_link)
  3870. rc = bnxt_hwrm_set_link_setting(bp, update_pause);
  3871. else if (update_pause)
  3872. rc = bnxt_hwrm_set_pause(bp);
  3873. if (rc) {
  3874. netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
  3875. rc);
  3876. return rc;
  3877. }
  3878. return rc;
  3879. }
  3880. /* Common routine to pre-map certain register block to different GRC window.
  3881. * A PF has 16 4K windows and a VF has 4 4K windows. However, only 15 windows
  3882. * in PF and 3 windows in VF that can be customized to map in different
  3883. * register blocks.
  3884. */
  3885. static void bnxt_preset_reg_win(struct bnxt *bp)
  3886. {
  3887. if (BNXT_PF(bp)) {
  3888. /* CAG registers map to GRC window #4 */
  3889. writel(BNXT_CAG_REG_BASE,
  3890. bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 12);
  3891. }
  3892. }
  3893. static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
  3894. {
  3895. int rc = 0;
  3896. bnxt_preset_reg_win(bp);
  3897. netif_carrier_off(bp->dev);
  3898. if (irq_re_init) {
  3899. rc = bnxt_setup_int_mode(bp);
  3900. if (rc) {
  3901. netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
  3902. rc);
  3903. return rc;
  3904. }
  3905. }
  3906. if ((bp->flags & BNXT_FLAG_RFS) &&
  3907. !(bp->flags & BNXT_FLAG_USING_MSIX)) {
  3908. /* disable RFS if falling back to INTA */
  3909. bp->dev->hw_features &= ~NETIF_F_NTUPLE;
  3910. bp->flags &= ~BNXT_FLAG_RFS;
  3911. }
  3912. rc = bnxt_alloc_mem(bp, irq_re_init);
  3913. if (rc) {
  3914. netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
  3915. goto open_err_free_mem;
  3916. }
  3917. if (irq_re_init) {
  3918. bnxt_init_napi(bp);
  3919. rc = bnxt_request_irq(bp);
  3920. if (rc) {
  3921. netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
  3922. goto open_err;
  3923. }
  3924. }
  3925. bnxt_enable_napi(bp);
  3926. rc = bnxt_init_nic(bp, irq_re_init);
  3927. if (rc) {
  3928. netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
  3929. goto open_err;
  3930. }
  3931. if (link_re_init) {
  3932. rc = bnxt_update_phy_setting(bp);
  3933. if (rc)
  3934. netdev_warn(bp->dev, "failed to update phy settings\n");
  3935. }
  3936. if (irq_re_init) {
  3937. #if defined(CONFIG_VXLAN) || defined(CONFIG_VXLAN_MODULE)
  3938. vxlan_get_rx_port(bp->dev);
  3939. #endif
  3940. if (!bnxt_hwrm_tunnel_dst_port_alloc(
  3941. bp, htons(0x17c1),
  3942. TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE))
  3943. bp->nge_port_cnt = 1;
  3944. }
  3945. set_bit(BNXT_STATE_OPEN, &bp->state);
  3946. bnxt_enable_int(bp);
  3947. /* Enable TX queues */
  3948. bnxt_tx_enable(bp);
  3949. mod_timer(&bp->timer, jiffies + bp->current_interval);
  3950. bnxt_update_link(bp, true);
  3951. return 0;
  3952. open_err:
  3953. bnxt_disable_napi(bp);
  3954. bnxt_del_napi(bp);
  3955. open_err_free_mem:
  3956. bnxt_free_skbs(bp);
  3957. bnxt_free_irq(bp);
  3958. bnxt_free_mem(bp, true);
  3959. return rc;
  3960. }
  3961. /* rtnl_lock held */
  3962. int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
  3963. {
  3964. int rc = 0;
  3965. rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
  3966. if (rc) {
  3967. netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
  3968. dev_close(bp->dev);
  3969. }
  3970. return rc;
  3971. }
  3972. static int bnxt_open(struct net_device *dev)
  3973. {
  3974. struct bnxt *bp = netdev_priv(dev);
  3975. int rc = 0;
  3976. rc = bnxt_hwrm_func_reset(bp);
  3977. if (rc) {
  3978. netdev_err(bp->dev, "hwrm chip reset failure rc: %x\n",
  3979. rc);
  3980. rc = -1;
  3981. return rc;
  3982. }
  3983. return __bnxt_open_nic(bp, true, true);
  3984. }
  3985. static void bnxt_disable_int_sync(struct bnxt *bp)
  3986. {
  3987. int i;
  3988. atomic_inc(&bp->intr_sem);
  3989. if (!netif_running(bp->dev))
  3990. return;
  3991. bnxt_disable_int(bp);
  3992. for (i = 0; i < bp->cp_nr_rings; i++)
  3993. synchronize_irq(bp->irq_tbl[i].vector);
  3994. }
  3995. int bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
  3996. {
  3997. int rc = 0;
  3998. #ifdef CONFIG_BNXT_SRIOV
  3999. if (bp->sriov_cfg) {
  4000. rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
  4001. !bp->sriov_cfg,
  4002. BNXT_SRIOV_CFG_WAIT_TMO);
  4003. if (rc)
  4004. netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete!\n");
  4005. }
  4006. #endif
  4007. /* Change device state to avoid TX queue wake up's */
  4008. bnxt_tx_disable(bp);
  4009. clear_bit(BNXT_STATE_OPEN, &bp->state);
  4010. smp_mb__after_atomic();
  4011. while (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state))
  4012. msleep(20);
  4013. /* Flush rings before disabling interrupts */
  4014. bnxt_shutdown_nic(bp, irq_re_init);
  4015. /* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
  4016. bnxt_disable_napi(bp);
  4017. bnxt_disable_int_sync(bp);
  4018. del_timer_sync(&bp->timer);
  4019. bnxt_free_skbs(bp);
  4020. if (irq_re_init) {
  4021. bnxt_free_irq(bp);
  4022. bnxt_del_napi(bp);
  4023. }
  4024. bnxt_free_mem(bp, irq_re_init);
  4025. return rc;
  4026. }
  4027. static int bnxt_close(struct net_device *dev)
  4028. {
  4029. struct bnxt *bp = netdev_priv(dev);
  4030. bnxt_close_nic(bp, true, true);
  4031. return 0;
  4032. }
  4033. /* rtnl_lock held */
  4034. static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  4035. {
  4036. switch (cmd) {
  4037. case SIOCGMIIPHY:
  4038. /* fallthru */
  4039. case SIOCGMIIREG: {
  4040. if (!netif_running(dev))
  4041. return -EAGAIN;
  4042. return 0;
  4043. }
  4044. case SIOCSMIIREG:
  4045. if (!netif_running(dev))
  4046. return -EAGAIN;
  4047. return 0;
  4048. default:
  4049. /* do nothing */
  4050. break;
  4051. }
  4052. return -EOPNOTSUPP;
  4053. }
  4054. static struct rtnl_link_stats64 *
  4055. bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
  4056. {
  4057. u32 i;
  4058. struct bnxt *bp = netdev_priv(dev);
  4059. memset(stats, 0, sizeof(struct rtnl_link_stats64));
  4060. if (!bp->bnapi)
  4061. return stats;
  4062. /* TODO check if we need to synchronize with bnxt_close path */
  4063. for (i = 0; i < bp->cp_nr_rings; i++) {
  4064. struct bnxt_napi *bnapi = bp->bnapi[i];
  4065. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  4066. struct ctx_hw_stats *hw_stats = cpr->hw_stats;
  4067. stats->rx_packets += le64_to_cpu(hw_stats->rx_ucast_pkts);
  4068. stats->rx_packets += le64_to_cpu(hw_stats->rx_mcast_pkts);
  4069. stats->rx_packets += le64_to_cpu(hw_stats->rx_bcast_pkts);
  4070. stats->tx_packets += le64_to_cpu(hw_stats->tx_ucast_pkts);
  4071. stats->tx_packets += le64_to_cpu(hw_stats->tx_mcast_pkts);
  4072. stats->tx_packets += le64_to_cpu(hw_stats->tx_bcast_pkts);
  4073. stats->rx_bytes += le64_to_cpu(hw_stats->rx_ucast_bytes);
  4074. stats->rx_bytes += le64_to_cpu(hw_stats->rx_mcast_bytes);
  4075. stats->rx_bytes += le64_to_cpu(hw_stats->rx_bcast_bytes);
  4076. stats->tx_bytes += le64_to_cpu(hw_stats->tx_ucast_bytes);
  4077. stats->tx_bytes += le64_to_cpu(hw_stats->tx_mcast_bytes);
  4078. stats->tx_bytes += le64_to_cpu(hw_stats->tx_bcast_bytes);
  4079. stats->rx_missed_errors +=
  4080. le64_to_cpu(hw_stats->rx_discard_pkts);
  4081. stats->multicast += le64_to_cpu(hw_stats->rx_mcast_pkts);
  4082. stats->tx_dropped += le64_to_cpu(hw_stats->tx_drop_pkts);
  4083. }
  4084. if (bp->flags & BNXT_FLAG_PORT_STATS) {
  4085. struct rx_port_stats *rx = bp->hw_rx_port_stats;
  4086. struct tx_port_stats *tx = bp->hw_tx_port_stats;
  4087. stats->rx_crc_errors = le64_to_cpu(rx->rx_fcs_err_frames);
  4088. stats->rx_frame_errors = le64_to_cpu(rx->rx_align_err_frames);
  4089. stats->rx_length_errors = le64_to_cpu(rx->rx_undrsz_frames) +
  4090. le64_to_cpu(rx->rx_ovrsz_frames) +
  4091. le64_to_cpu(rx->rx_runt_frames);
  4092. stats->rx_errors = le64_to_cpu(rx->rx_false_carrier_frames) +
  4093. le64_to_cpu(rx->rx_jbr_frames);
  4094. stats->collisions = le64_to_cpu(tx->tx_total_collisions);
  4095. stats->tx_fifo_errors = le64_to_cpu(tx->tx_fifo_underruns);
  4096. stats->tx_errors = le64_to_cpu(tx->tx_err);
  4097. }
  4098. return stats;
  4099. }
  4100. static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
  4101. {
  4102. struct net_device *dev = bp->dev;
  4103. struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
  4104. struct netdev_hw_addr *ha;
  4105. u8 *haddr;
  4106. int mc_count = 0;
  4107. bool update = false;
  4108. int off = 0;
  4109. netdev_for_each_mc_addr(ha, dev) {
  4110. if (mc_count >= BNXT_MAX_MC_ADDRS) {
  4111. *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
  4112. vnic->mc_list_count = 0;
  4113. return false;
  4114. }
  4115. haddr = ha->addr;
  4116. if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
  4117. memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
  4118. update = true;
  4119. }
  4120. off += ETH_ALEN;
  4121. mc_count++;
  4122. }
  4123. if (mc_count)
  4124. *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
  4125. if (mc_count != vnic->mc_list_count) {
  4126. vnic->mc_list_count = mc_count;
  4127. update = true;
  4128. }
  4129. return update;
  4130. }
  4131. static bool bnxt_uc_list_updated(struct bnxt *bp)
  4132. {
  4133. struct net_device *dev = bp->dev;
  4134. struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
  4135. struct netdev_hw_addr *ha;
  4136. int off = 0;
  4137. if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
  4138. return true;
  4139. netdev_for_each_uc_addr(ha, dev) {
  4140. if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
  4141. return true;
  4142. off += ETH_ALEN;
  4143. }
  4144. return false;
  4145. }
  4146. static void bnxt_set_rx_mode(struct net_device *dev)
  4147. {
  4148. struct bnxt *bp = netdev_priv(dev);
  4149. struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
  4150. u32 mask = vnic->rx_mask;
  4151. bool mc_update = false;
  4152. bool uc_update;
  4153. if (!netif_running(dev))
  4154. return;
  4155. mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
  4156. CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
  4157. CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST);
  4158. /* Only allow PF to be in promiscuous mode */
  4159. if ((dev->flags & IFF_PROMISC) && BNXT_PF(bp))
  4160. mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
  4161. uc_update = bnxt_uc_list_updated(bp);
  4162. if (dev->flags & IFF_ALLMULTI) {
  4163. mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
  4164. vnic->mc_list_count = 0;
  4165. } else {
  4166. mc_update = bnxt_mc_list_updated(bp, &mask);
  4167. }
  4168. if (mask != vnic->rx_mask || uc_update || mc_update) {
  4169. vnic->rx_mask = mask;
  4170. set_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event);
  4171. schedule_work(&bp->sp_task);
  4172. }
  4173. }
  4174. static int bnxt_cfg_rx_mode(struct bnxt *bp)
  4175. {
  4176. struct net_device *dev = bp->dev;
  4177. struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
  4178. struct netdev_hw_addr *ha;
  4179. int i, off = 0, rc;
  4180. bool uc_update;
  4181. netif_addr_lock_bh(dev);
  4182. uc_update = bnxt_uc_list_updated(bp);
  4183. netif_addr_unlock_bh(dev);
  4184. if (!uc_update)
  4185. goto skip_uc;
  4186. mutex_lock(&bp->hwrm_cmd_lock);
  4187. for (i = 1; i < vnic->uc_filter_count; i++) {
  4188. struct hwrm_cfa_l2_filter_free_input req = {0};
  4189. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_FREE, -1,
  4190. -1);
  4191. req.l2_filter_id = vnic->fw_l2_filter_id[i];
  4192. rc = _hwrm_send_message(bp, &req, sizeof(req),
  4193. HWRM_CMD_TIMEOUT);
  4194. }
  4195. mutex_unlock(&bp->hwrm_cmd_lock);
  4196. vnic->uc_filter_count = 1;
  4197. netif_addr_lock_bh(dev);
  4198. if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
  4199. vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
  4200. } else {
  4201. netdev_for_each_uc_addr(ha, dev) {
  4202. memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
  4203. off += ETH_ALEN;
  4204. vnic->uc_filter_count++;
  4205. }
  4206. }
  4207. netif_addr_unlock_bh(dev);
  4208. for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
  4209. rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
  4210. if (rc) {
  4211. netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n",
  4212. rc);
  4213. vnic->uc_filter_count = i;
  4214. return rc;
  4215. }
  4216. }
  4217. skip_uc:
  4218. rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
  4219. if (rc)
  4220. netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %x\n",
  4221. rc);
  4222. return rc;
  4223. }
  4224. static bool bnxt_rfs_capable(struct bnxt *bp)
  4225. {
  4226. #ifdef CONFIG_RFS_ACCEL
  4227. struct bnxt_pf_info *pf = &bp->pf;
  4228. int vnics;
  4229. if (BNXT_VF(bp) || !(bp->flags & BNXT_FLAG_MSIX_CAP))
  4230. return false;
  4231. vnics = 1 + bp->rx_nr_rings;
  4232. if (vnics > pf->max_rsscos_ctxs || vnics > pf->max_vnics)
  4233. return false;
  4234. return true;
  4235. #else
  4236. return false;
  4237. #endif
  4238. }
  4239. static netdev_features_t bnxt_fix_features(struct net_device *dev,
  4240. netdev_features_t features)
  4241. {
  4242. struct bnxt *bp = netdev_priv(dev);
  4243. if (!bnxt_rfs_capable(bp))
  4244. features &= ~NETIF_F_NTUPLE;
  4245. return features;
  4246. }
  4247. static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
  4248. {
  4249. struct bnxt *bp = netdev_priv(dev);
  4250. u32 flags = bp->flags;
  4251. u32 changes;
  4252. int rc = 0;
  4253. bool re_init = false;
  4254. bool update_tpa = false;
  4255. flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
  4256. if ((features & NETIF_F_GRO) && (bp->pdev->revision > 0))
  4257. flags |= BNXT_FLAG_GRO;
  4258. if (features & NETIF_F_LRO)
  4259. flags |= BNXT_FLAG_LRO;
  4260. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  4261. flags |= BNXT_FLAG_STRIP_VLAN;
  4262. if (features & NETIF_F_NTUPLE)
  4263. flags |= BNXT_FLAG_RFS;
  4264. changes = flags ^ bp->flags;
  4265. if (changes & BNXT_FLAG_TPA) {
  4266. update_tpa = true;
  4267. if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
  4268. (flags & BNXT_FLAG_TPA) == 0)
  4269. re_init = true;
  4270. }
  4271. if (changes & ~BNXT_FLAG_TPA)
  4272. re_init = true;
  4273. if (flags != bp->flags) {
  4274. u32 old_flags = bp->flags;
  4275. bp->flags = flags;
  4276. if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
  4277. if (update_tpa)
  4278. bnxt_set_ring_params(bp);
  4279. return rc;
  4280. }
  4281. if (re_init) {
  4282. bnxt_close_nic(bp, false, false);
  4283. if (update_tpa)
  4284. bnxt_set_ring_params(bp);
  4285. return bnxt_open_nic(bp, false, false);
  4286. }
  4287. if (update_tpa) {
  4288. rc = bnxt_set_tpa(bp,
  4289. (flags & BNXT_FLAG_TPA) ?
  4290. true : false);
  4291. if (rc)
  4292. bp->flags = old_flags;
  4293. }
  4294. }
  4295. return rc;
  4296. }
  4297. static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
  4298. {
  4299. struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
  4300. int i = bnapi->index;
  4301. if (!txr)
  4302. return;
  4303. netdev_info(bnapi->bp->dev, "[%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
  4304. i, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
  4305. txr->tx_cons);
  4306. }
  4307. static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
  4308. {
  4309. struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
  4310. int i = bnapi->index;
  4311. if (!rxr)
  4312. return;
  4313. 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",
  4314. i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
  4315. rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
  4316. rxr->rx_sw_agg_prod);
  4317. }
  4318. static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
  4319. {
  4320. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  4321. int i = bnapi->index;
  4322. netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
  4323. i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
  4324. }
  4325. static void bnxt_dbg_dump_states(struct bnxt *bp)
  4326. {
  4327. int i;
  4328. struct bnxt_napi *bnapi;
  4329. for (i = 0; i < bp->cp_nr_rings; i++) {
  4330. bnapi = bp->bnapi[i];
  4331. if (netif_msg_drv(bp)) {
  4332. bnxt_dump_tx_sw_state(bnapi);
  4333. bnxt_dump_rx_sw_state(bnapi);
  4334. bnxt_dump_cp_sw_state(bnapi);
  4335. }
  4336. }
  4337. }
  4338. static void bnxt_reset_task(struct bnxt *bp)
  4339. {
  4340. bnxt_dbg_dump_states(bp);
  4341. if (netif_running(bp->dev)) {
  4342. bnxt_close_nic(bp, false, false);
  4343. bnxt_open_nic(bp, false, false);
  4344. }
  4345. }
  4346. static void bnxt_tx_timeout(struct net_device *dev)
  4347. {
  4348. struct bnxt *bp = netdev_priv(dev);
  4349. netdev_err(bp->dev, "TX timeout detected, starting reset task!\n");
  4350. set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
  4351. schedule_work(&bp->sp_task);
  4352. }
  4353. #ifdef CONFIG_NET_POLL_CONTROLLER
  4354. static void bnxt_poll_controller(struct net_device *dev)
  4355. {
  4356. struct bnxt *bp = netdev_priv(dev);
  4357. int i;
  4358. for (i = 0; i < bp->cp_nr_rings; i++) {
  4359. struct bnxt_irq *irq = &bp->irq_tbl[i];
  4360. disable_irq(irq->vector);
  4361. irq->handler(irq->vector, bp->bnapi[i]);
  4362. enable_irq(irq->vector);
  4363. }
  4364. }
  4365. #endif
  4366. static void bnxt_timer(unsigned long data)
  4367. {
  4368. struct bnxt *bp = (struct bnxt *)data;
  4369. struct net_device *dev = bp->dev;
  4370. if (!netif_running(dev))
  4371. return;
  4372. if (atomic_read(&bp->intr_sem) != 0)
  4373. goto bnxt_restart_timer;
  4374. if (bp->link_info.link_up && (bp->flags & BNXT_FLAG_PORT_STATS)) {
  4375. set_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event);
  4376. schedule_work(&bp->sp_task);
  4377. }
  4378. bnxt_restart_timer:
  4379. mod_timer(&bp->timer, jiffies + bp->current_interval);
  4380. }
  4381. static void bnxt_cfg_ntp_filters(struct bnxt *);
  4382. static void bnxt_sp_task(struct work_struct *work)
  4383. {
  4384. struct bnxt *bp = container_of(work, struct bnxt, sp_task);
  4385. int rc;
  4386. set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
  4387. smp_mb__after_atomic();
  4388. if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
  4389. clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
  4390. return;
  4391. }
  4392. if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
  4393. bnxt_cfg_rx_mode(bp);
  4394. if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
  4395. bnxt_cfg_ntp_filters(bp);
  4396. if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
  4397. rc = bnxt_update_link(bp, true);
  4398. if (rc)
  4399. netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
  4400. rc);
  4401. }
  4402. if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
  4403. bnxt_hwrm_exec_fwd_req(bp);
  4404. if (test_and_clear_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event)) {
  4405. bnxt_hwrm_tunnel_dst_port_alloc(
  4406. bp, bp->vxlan_port,
  4407. TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
  4408. }
  4409. if (test_and_clear_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event)) {
  4410. bnxt_hwrm_tunnel_dst_port_free(
  4411. bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
  4412. }
  4413. if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event)) {
  4414. /* bnxt_reset_task() calls bnxt_close_nic() which waits
  4415. * for BNXT_STATE_IN_SP_TASK to clear.
  4416. */
  4417. clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
  4418. rtnl_lock();
  4419. bnxt_reset_task(bp);
  4420. set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
  4421. rtnl_unlock();
  4422. }
  4423. if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event))
  4424. bnxt_hwrm_port_qstats(bp);
  4425. smp_mb__before_atomic();
  4426. clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
  4427. }
  4428. static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
  4429. {
  4430. int rc;
  4431. struct bnxt *bp = netdev_priv(dev);
  4432. SET_NETDEV_DEV(dev, &pdev->dev);
  4433. /* enable device (incl. PCI PM wakeup), and bus-mastering */
  4434. rc = pci_enable_device(pdev);
  4435. if (rc) {
  4436. dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
  4437. goto init_err;
  4438. }
  4439. if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
  4440. dev_err(&pdev->dev,
  4441. "Cannot find PCI device base address, aborting\n");
  4442. rc = -ENODEV;
  4443. goto init_err_disable;
  4444. }
  4445. rc = pci_request_regions(pdev, DRV_MODULE_NAME);
  4446. if (rc) {
  4447. dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
  4448. goto init_err_disable;
  4449. }
  4450. if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
  4451. dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
  4452. dev_err(&pdev->dev, "System does not support DMA, aborting\n");
  4453. goto init_err_disable;
  4454. }
  4455. pci_set_master(pdev);
  4456. bp->dev = dev;
  4457. bp->pdev = pdev;
  4458. bp->bar0 = pci_ioremap_bar(pdev, 0);
  4459. if (!bp->bar0) {
  4460. dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
  4461. rc = -ENOMEM;
  4462. goto init_err_release;
  4463. }
  4464. bp->bar1 = pci_ioremap_bar(pdev, 2);
  4465. if (!bp->bar1) {
  4466. dev_err(&pdev->dev, "Cannot map doorbell registers, aborting\n");
  4467. rc = -ENOMEM;
  4468. goto init_err_release;
  4469. }
  4470. bp->bar2 = pci_ioremap_bar(pdev, 4);
  4471. if (!bp->bar2) {
  4472. dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
  4473. rc = -ENOMEM;
  4474. goto init_err_release;
  4475. }
  4476. pci_enable_pcie_error_reporting(pdev);
  4477. INIT_WORK(&bp->sp_task, bnxt_sp_task);
  4478. spin_lock_init(&bp->ntp_fltr_lock);
  4479. bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
  4480. bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
  4481. /* tick values in micro seconds */
  4482. bp->rx_coal_ticks = 12;
  4483. bp->rx_coal_bufs = 30;
  4484. bp->rx_coal_ticks_irq = 1;
  4485. bp->rx_coal_bufs_irq = 2;
  4486. bp->tx_coal_ticks = 25;
  4487. bp->tx_coal_bufs = 30;
  4488. bp->tx_coal_ticks_irq = 2;
  4489. bp->tx_coal_bufs_irq = 2;
  4490. init_timer(&bp->timer);
  4491. bp->timer.data = (unsigned long)bp;
  4492. bp->timer.function = bnxt_timer;
  4493. bp->current_interval = BNXT_TIMER_INTERVAL;
  4494. clear_bit(BNXT_STATE_OPEN, &bp->state);
  4495. return 0;
  4496. init_err_release:
  4497. if (bp->bar2) {
  4498. pci_iounmap(pdev, bp->bar2);
  4499. bp->bar2 = NULL;
  4500. }
  4501. if (bp->bar1) {
  4502. pci_iounmap(pdev, bp->bar1);
  4503. bp->bar1 = NULL;
  4504. }
  4505. if (bp->bar0) {
  4506. pci_iounmap(pdev, bp->bar0);
  4507. bp->bar0 = NULL;
  4508. }
  4509. pci_release_regions(pdev);
  4510. init_err_disable:
  4511. pci_disable_device(pdev);
  4512. init_err:
  4513. return rc;
  4514. }
  4515. /* rtnl_lock held */
  4516. static int bnxt_change_mac_addr(struct net_device *dev, void *p)
  4517. {
  4518. struct sockaddr *addr = p;
  4519. struct bnxt *bp = netdev_priv(dev);
  4520. int rc = 0;
  4521. if (!is_valid_ether_addr(addr->sa_data))
  4522. return -EADDRNOTAVAIL;
  4523. #ifdef CONFIG_BNXT_SRIOV
  4524. if (BNXT_VF(bp) && is_valid_ether_addr(bp->vf.mac_addr))
  4525. return -EADDRNOTAVAIL;
  4526. #endif
  4527. if (ether_addr_equal(addr->sa_data, dev->dev_addr))
  4528. return 0;
  4529. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  4530. if (netif_running(dev)) {
  4531. bnxt_close_nic(bp, false, false);
  4532. rc = bnxt_open_nic(bp, false, false);
  4533. }
  4534. return rc;
  4535. }
  4536. /* rtnl_lock held */
  4537. static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
  4538. {
  4539. struct bnxt *bp = netdev_priv(dev);
  4540. if (new_mtu < 60 || new_mtu > 9000)
  4541. return -EINVAL;
  4542. if (netif_running(dev))
  4543. bnxt_close_nic(bp, false, false);
  4544. dev->mtu = new_mtu;
  4545. bnxt_set_ring_params(bp);
  4546. if (netif_running(dev))
  4547. return bnxt_open_nic(bp, false, false);
  4548. return 0;
  4549. }
  4550. static int bnxt_setup_tc(struct net_device *dev, u32 handle, __be16 proto,
  4551. struct tc_to_netdev *ntc)
  4552. {
  4553. struct bnxt *bp = netdev_priv(dev);
  4554. u8 tc;
  4555. if (ntc->type != TC_SETUP_MQPRIO)
  4556. return -EINVAL;
  4557. tc = ntc->tc;
  4558. if (tc > bp->max_tc) {
  4559. netdev_err(dev, "too many traffic classes requested: %d Max supported is %d\n",
  4560. tc, bp->max_tc);
  4561. return -EINVAL;
  4562. }
  4563. if (netdev_get_num_tc(dev) == tc)
  4564. return 0;
  4565. if (tc) {
  4566. int max_rx_rings, max_tx_rings, rc;
  4567. bool sh = false;
  4568. if (bp->flags & BNXT_FLAG_SHARED_RINGS)
  4569. sh = true;
  4570. rc = bnxt_get_max_rings(bp, &max_rx_rings, &max_tx_rings, sh);
  4571. if (rc || bp->tx_nr_rings_per_tc * tc > max_tx_rings)
  4572. return -ENOMEM;
  4573. }
  4574. /* Needs to close the device and do hw resource re-allocations */
  4575. if (netif_running(bp->dev))
  4576. bnxt_close_nic(bp, true, false);
  4577. if (tc) {
  4578. bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
  4579. netdev_set_num_tc(dev, tc);
  4580. } else {
  4581. bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
  4582. netdev_reset_tc(dev);
  4583. }
  4584. bp->cp_nr_rings = max_t(int, bp->tx_nr_rings, bp->rx_nr_rings);
  4585. bp->num_stat_ctxs = bp->cp_nr_rings;
  4586. if (netif_running(bp->dev))
  4587. return bnxt_open_nic(bp, true, false);
  4588. return 0;
  4589. }
  4590. #ifdef CONFIG_RFS_ACCEL
  4591. static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
  4592. struct bnxt_ntuple_filter *f2)
  4593. {
  4594. struct flow_keys *keys1 = &f1->fkeys;
  4595. struct flow_keys *keys2 = &f2->fkeys;
  4596. if (keys1->addrs.v4addrs.src == keys2->addrs.v4addrs.src &&
  4597. keys1->addrs.v4addrs.dst == keys2->addrs.v4addrs.dst &&
  4598. keys1->ports.ports == keys2->ports.ports &&
  4599. keys1->basic.ip_proto == keys2->basic.ip_proto &&
  4600. keys1->basic.n_proto == keys2->basic.n_proto &&
  4601. ether_addr_equal(f1->src_mac_addr, f2->src_mac_addr))
  4602. return true;
  4603. return false;
  4604. }
  4605. static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
  4606. u16 rxq_index, u32 flow_id)
  4607. {
  4608. struct bnxt *bp = netdev_priv(dev);
  4609. struct bnxt_ntuple_filter *fltr, *new_fltr;
  4610. struct flow_keys *fkeys;
  4611. struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
  4612. int rc = 0, idx, bit_id;
  4613. struct hlist_head *head;
  4614. if (skb->encapsulation)
  4615. return -EPROTONOSUPPORT;
  4616. new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
  4617. if (!new_fltr)
  4618. return -ENOMEM;
  4619. fkeys = &new_fltr->fkeys;
  4620. if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
  4621. rc = -EPROTONOSUPPORT;
  4622. goto err_free;
  4623. }
  4624. if ((fkeys->basic.n_proto != htons(ETH_P_IP)) ||
  4625. ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
  4626. (fkeys->basic.ip_proto != IPPROTO_UDP))) {
  4627. rc = -EPROTONOSUPPORT;
  4628. goto err_free;
  4629. }
  4630. memcpy(new_fltr->src_mac_addr, eth->h_source, ETH_ALEN);
  4631. idx = skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
  4632. head = &bp->ntp_fltr_hash_tbl[idx];
  4633. rcu_read_lock();
  4634. hlist_for_each_entry_rcu(fltr, head, hash) {
  4635. if (bnxt_fltr_match(fltr, new_fltr)) {
  4636. rcu_read_unlock();
  4637. rc = 0;
  4638. goto err_free;
  4639. }
  4640. }
  4641. rcu_read_unlock();
  4642. spin_lock_bh(&bp->ntp_fltr_lock);
  4643. bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
  4644. BNXT_NTP_FLTR_MAX_FLTR, 0);
  4645. if (bit_id < 0) {
  4646. spin_unlock_bh(&bp->ntp_fltr_lock);
  4647. rc = -ENOMEM;
  4648. goto err_free;
  4649. }
  4650. new_fltr->sw_id = (u16)bit_id;
  4651. new_fltr->flow_id = flow_id;
  4652. new_fltr->rxq = rxq_index;
  4653. hlist_add_head_rcu(&new_fltr->hash, head);
  4654. bp->ntp_fltr_count++;
  4655. spin_unlock_bh(&bp->ntp_fltr_lock);
  4656. set_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event);
  4657. schedule_work(&bp->sp_task);
  4658. return new_fltr->sw_id;
  4659. err_free:
  4660. kfree(new_fltr);
  4661. return rc;
  4662. }
  4663. static void bnxt_cfg_ntp_filters(struct bnxt *bp)
  4664. {
  4665. int i;
  4666. for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
  4667. struct hlist_head *head;
  4668. struct hlist_node *tmp;
  4669. struct bnxt_ntuple_filter *fltr;
  4670. int rc;
  4671. head = &bp->ntp_fltr_hash_tbl[i];
  4672. hlist_for_each_entry_safe(fltr, tmp, head, hash) {
  4673. bool del = false;
  4674. if (test_bit(BNXT_FLTR_VALID, &fltr->state)) {
  4675. if (rps_may_expire_flow(bp->dev, fltr->rxq,
  4676. fltr->flow_id,
  4677. fltr->sw_id)) {
  4678. bnxt_hwrm_cfa_ntuple_filter_free(bp,
  4679. fltr);
  4680. del = true;
  4681. }
  4682. } else {
  4683. rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
  4684. fltr);
  4685. if (rc)
  4686. del = true;
  4687. else
  4688. set_bit(BNXT_FLTR_VALID, &fltr->state);
  4689. }
  4690. if (del) {
  4691. spin_lock_bh(&bp->ntp_fltr_lock);
  4692. hlist_del_rcu(&fltr->hash);
  4693. bp->ntp_fltr_count--;
  4694. spin_unlock_bh(&bp->ntp_fltr_lock);
  4695. synchronize_rcu();
  4696. clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
  4697. kfree(fltr);
  4698. }
  4699. }
  4700. }
  4701. if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
  4702. netdev_info(bp->dev, "Receive PF driver unload event!");
  4703. }
  4704. #else
  4705. static void bnxt_cfg_ntp_filters(struct bnxt *bp)
  4706. {
  4707. }
  4708. #endif /* CONFIG_RFS_ACCEL */
  4709. static void bnxt_add_vxlan_port(struct net_device *dev, sa_family_t sa_family,
  4710. __be16 port)
  4711. {
  4712. struct bnxt *bp = netdev_priv(dev);
  4713. if (!netif_running(dev))
  4714. return;
  4715. if (sa_family != AF_INET6 && sa_family != AF_INET)
  4716. return;
  4717. if (bp->vxlan_port_cnt && bp->vxlan_port != port)
  4718. return;
  4719. bp->vxlan_port_cnt++;
  4720. if (bp->vxlan_port_cnt == 1) {
  4721. bp->vxlan_port = port;
  4722. set_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event);
  4723. schedule_work(&bp->sp_task);
  4724. }
  4725. }
  4726. static void bnxt_del_vxlan_port(struct net_device *dev, sa_family_t sa_family,
  4727. __be16 port)
  4728. {
  4729. struct bnxt *bp = netdev_priv(dev);
  4730. if (!netif_running(dev))
  4731. return;
  4732. if (sa_family != AF_INET6 && sa_family != AF_INET)
  4733. return;
  4734. if (bp->vxlan_port_cnt && bp->vxlan_port == port) {
  4735. bp->vxlan_port_cnt--;
  4736. if (bp->vxlan_port_cnt == 0) {
  4737. set_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event);
  4738. schedule_work(&bp->sp_task);
  4739. }
  4740. }
  4741. }
  4742. static const struct net_device_ops bnxt_netdev_ops = {
  4743. .ndo_open = bnxt_open,
  4744. .ndo_start_xmit = bnxt_start_xmit,
  4745. .ndo_stop = bnxt_close,
  4746. .ndo_get_stats64 = bnxt_get_stats64,
  4747. .ndo_set_rx_mode = bnxt_set_rx_mode,
  4748. .ndo_do_ioctl = bnxt_ioctl,
  4749. .ndo_validate_addr = eth_validate_addr,
  4750. .ndo_set_mac_address = bnxt_change_mac_addr,
  4751. .ndo_change_mtu = bnxt_change_mtu,
  4752. .ndo_fix_features = bnxt_fix_features,
  4753. .ndo_set_features = bnxt_set_features,
  4754. .ndo_tx_timeout = bnxt_tx_timeout,
  4755. #ifdef CONFIG_BNXT_SRIOV
  4756. .ndo_get_vf_config = bnxt_get_vf_config,
  4757. .ndo_set_vf_mac = bnxt_set_vf_mac,
  4758. .ndo_set_vf_vlan = bnxt_set_vf_vlan,
  4759. .ndo_set_vf_rate = bnxt_set_vf_bw,
  4760. .ndo_set_vf_link_state = bnxt_set_vf_link_state,
  4761. .ndo_set_vf_spoofchk = bnxt_set_vf_spoofchk,
  4762. #endif
  4763. #ifdef CONFIG_NET_POLL_CONTROLLER
  4764. .ndo_poll_controller = bnxt_poll_controller,
  4765. #endif
  4766. .ndo_setup_tc = bnxt_setup_tc,
  4767. #ifdef CONFIG_RFS_ACCEL
  4768. .ndo_rx_flow_steer = bnxt_rx_flow_steer,
  4769. #endif
  4770. .ndo_add_vxlan_port = bnxt_add_vxlan_port,
  4771. .ndo_del_vxlan_port = bnxt_del_vxlan_port,
  4772. #ifdef CONFIG_NET_RX_BUSY_POLL
  4773. .ndo_busy_poll = bnxt_busy_poll,
  4774. #endif
  4775. };
  4776. static void bnxt_remove_one(struct pci_dev *pdev)
  4777. {
  4778. struct net_device *dev = pci_get_drvdata(pdev);
  4779. struct bnxt *bp = netdev_priv(dev);
  4780. if (BNXT_PF(bp))
  4781. bnxt_sriov_disable(bp);
  4782. pci_disable_pcie_error_reporting(pdev);
  4783. unregister_netdev(dev);
  4784. cancel_work_sync(&bp->sp_task);
  4785. bp->sp_event = 0;
  4786. bnxt_hwrm_func_drv_unrgtr(bp);
  4787. bnxt_free_hwrm_resources(bp);
  4788. pci_iounmap(pdev, bp->bar2);
  4789. pci_iounmap(pdev, bp->bar1);
  4790. pci_iounmap(pdev, bp->bar0);
  4791. free_netdev(dev);
  4792. pci_release_regions(pdev);
  4793. pci_disable_device(pdev);
  4794. }
  4795. static int bnxt_probe_phy(struct bnxt *bp)
  4796. {
  4797. int rc = 0;
  4798. struct bnxt_link_info *link_info = &bp->link_info;
  4799. rc = bnxt_update_link(bp, false);
  4800. if (rc) {
  4801. netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
  4802. rc);
  4803. return rc;
  4804. }
  4805. /*initialize the ethool setting copy with NVM settings */
  4806. if (BNXT_AUTO_MODE(link_info->auto_mode)) {
  4807. link_info->autoneg = BNXT_AUTONEG_SPEED |
  4808. BNXT_AUTONEG_FLOW_CTRL;
  4809. link_info->advertising = link_info->auto_link_speeds;
  4810. link_info->req_flow_ctrl = link_info->auto_pause_setting;
  4811. } else {
  4812. link_info->req_link_speed = link_info->force_link_speed;
  4813. link_info->req_duplex = link_info->duplex_setting;
  4814. link_info->req_flow_ctrl = link_info->force_pause_setting;
  4815. }
  4816. return rc;
  4817. }
  4818. static int bnxt_get_max_irq(struct pci_dev *pdev)
  4819. {
  4820. u16 ctrl;
  4821. if (!pdev->msix_cap)
  4822. return 1;
  4823. pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
  4824. return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
  4825. }
  4826. static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
  4827. int *max_cp)
  4828. {
  4829. int max_ring_grps = 0;
  4830. #ifdef CONFIG_BNXT_SRIOV
  4831. if (!BNXT_PF(bp)) {
  4832. *max_tx = bp->vf.max_tx_rings;
  4833. *max_rx = bp->vf.max_rx_rings;
  4834. *max_cp = min_t(int, bp->vf.max_irqs, bp->vf.max_cp_rings);
  4835. *max_cp = min_t(int, *max_cp, bp->vf.max_stat_ctxs);
  4836. max_ring_grps = bp->vf.max_hw_ring_grps;
  4837. } else
  4838. #endif
  4839. {
  4840. *max_tx = bp->pf.max_tx_rings;
  4841. *max_rx = bp->pf.max_rx_rings;
  4842. *max_cp = min_t(int, bp->pf.max_irqs, bp->pf.max_cp_rings);
  4843. *max_cp = min_t(int, *max_cp, bp->pf.max_stat_ctxs);
  4844. max_ring_grps = bp->pf.max_hw_ring_grps;
  4845. }
  4846. if (bp->flags & BNXT_FLAG_AGG_RINGS)
  4847. *max_rx >>= 1;
  4848. *max_rx = min_t(int, *max_rx, max_ring_grps);
  4849. }
  4850. int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
  4851. {
  4852. int rx, tx, cp;
  4853. _bnxt_get_max_rings(bp, &rx, &tx, &cp);
  4854. if (!rx || !tx || !cp)
  4855. return -ENOMEM;
  4856. *max_rx = rx;
  4857. *max_tx = tx;
  4858. return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
  4859. }
  4860. static int bnxt_set_dflt_rings(struct bnxt *bp)
  4861. {
  4862. int dflt_rings, max_rx_rings, max_tx_rings, rc;
  4863. bool sh = true;
  4864. if (sh)
  4865. bp->flags |= BNXT_FLAG_SHARED_RINGS;
  4866. dflt_rings = netif_get_num_default_rss_queues();
  4867. rc = bnxt_get_max_rings(bp, &max_rx_rings, &max_tx_rings, sh);
  4868. if (rc)
  4869. return rc;
  4870. bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
  4871. bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
  4872. bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
  4873. bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
  4874. bp->tx_nr_rings + bp->rx_nr_rings;
  4875. bp->num_stat_ctxs = bp->cp_nr_rings;
  4876. return rc;
  4877. }
  4878. static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  4879. {
  4880. static int version_printed;
  4881. struct net_device *dev;
  4882. struct bnxt *bp;
  4883. int rc, max_irqs;
  4884. if (version_printed++ == 0)
  4885. pr_info("%s", version);
  4886. max_irqs = bnxt_get_max_irq(pdev);
  4887. dev = alloc_etherdev_mq(sizeof(*bp), max_irqs);
  4888. if (!dev)
  4889. return -ENOMEM;
  4890. bp = netdev_priv(dev);
  4891. if (bnxt_vf_pciid(ent->driver_data))
  4892. bp->flags |= BNXT_FLAG_VF;
  4893. if (pdev->msix_cap)
  4894. bp->flags |= BNXT_FLAG_MSIX_CAP;
  4895. rc = bnxt_init_board(pdev, dev);
  4896. if (rc < 0)
  4897. goto init_err_free;
  4898. dev->netdev_ops = &bnxt_netdev_ops;
  4899. dev->watchdog_timeo = BNXT_TX_TIMEOUT;
  4900. dev->ethtool_ops = &bnxt_ethtool_ops;
  4901. pci_set_drvdata(pdev, dev);
  4902. dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
  4903. NETIF_F_TSO | NETIF_F_TSO6 |
  4904. NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
  4905. NETIF_F_GSO_IPIP | NETIF_F_GSO_SIT |
  4906. NETIF_F_RXHASH |
  4907. NETIF_F_RXCSUM | NETIF_F_LRO | NETIF_F_GRO;
  4908. dev->hw_enc_features =
  4909. NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
  4910. NETIF_F_TSO | NETIF_F_TSO6 |
  4911. NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
  4912. NETIF_F_GSO_IPIP | NETIF_F_GSO_SIT;
  4913. dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
  4914. dev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX |
  4915. NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX;
  4916. dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
  4917. dev->priv_flags |= IFF_UNICAST_FLT;
  4918. #ifdef CONFIG_BNXT_SRIOV
  4919. init_waitqueue_head(&bp->sriov_cfg_wait);
  4920. #endif
  4921. rc = bnxt_alloc_hwrm_resources(bp);
  4922. if (rc)
  4923. goto init_err;
  4924. mutex_init(&bp->hwrm_cmd_lock);
  4925. bnxt_hwrm_ver_get(bp);
  4926. rc = bnxt_hwrm_func_drv_rgtr(bp);
  4927. if (rc)
  4928. goto init_err;
  4929. /* Get the MAX capabilities for this function */
  4930. rc = bnxt_hwrm_func_qcaps(bp);
  4931. if (rc) {
  4932. netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
  4933. rc);
  4934. rc = -1;
  4935. goto init_err;
  4936. }
  4937. rc = bnxt_hwrm_queue_qportcfg(bp);
  4938. if (rc) {
  4939. netdev_err(bp->dev, "hwrm query qportcfg failure rc: %x\n",
  4940. rc);
  4941. rc = -1;
  4942. goto init_err;
  4943. }
  4944. bnxt_set_tpa_flags(bp);
  4945. bnxt_set_ring_params(bp);
  4946. if (BNXT_PF(bp))
  4947. bp->pf.max_irqs = max_irqs;
  4948. #if defined(CONFIG_BNXT_SRIOV)
  4949. else
  4950. bp->vf.max_irqs = max_irqs;
  4951. #endif
  4952. bnxt_set_dflt_rings(bp);
  4953. if (BNXT_PF(bp)) {
  4954. dev->hw_features |= NETIF_F_NTUPLE;
  4955. if (bnxt_rfs_capable(bp)) {
  4956. bp->flags |= BNXT_FLAG_RFS;
  4957. dev->features |= NETIF_F_NTUPLE;
  4958. }
  4959. }
  4960. if (dev->hw_features & NETIF_F_HW_VLAN_CTAG_RX)
  4961. bp->flags |= BNXT_FLAG_STRIP_VLAN;
  4962. rc = bnxt_probe_phy(bp);
  4963. if (rc)
  4964. goto init_err;
  4965. rc = register_netdev(dev);
  4966. if (rc)
  4967. goto init_err;
  4968. netdev_info(dev, "%s found at mem %lx, node addr %pM\n",
  4969. board_info[ent->driver_data].name,
  4970. (long)pci_resource_start(pdev, 0), dev->dev_addr);
  4971. return 0;
  4972. init_err:
  4973. pci_iounmap(pdev, bp->bar0);
  4974. pci_release_regions(pdev);
  4975. pci_disable_device(pdev);
  4976. init_err_free:
  4977. free_netdev(dev);
  4978. return rc;
  4979. }
  4980. /**
  4981. * bnxt_io_error_detected - called when PCI error is detected
  4982. * @pdev: Pointer to PCI device
  4983. * @state: The current pci connection state
  4984. *
  4985. * This function is called after a PCI bus error affecting
  4986. * this device has been detected.
  4987. */
  4988. static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
  4989. pci_channel_state_t state)
  4990. {
  4991. struct net_device *netdev = pci_get_drvdata(pdev);
  4992. netdev_info(netdev, "PCI I/O error detected\n");
  4993. rtnl_lock();
  4994. netif_device_detach(netdev);
  4995. if (state == pci_channel_io_perm_failure) {
  4996. rtnl_unlock();
  4997. return PCI_ERS_RESULT_DISCONNECT;
  4998. }
  4999. if (netif_running(netdev))
  5000. bnxt_close(netdev);
  5001. pci_disable_device(pdev);
  5002. rtnl_unlock();
  5003. /* Request a slot slot reset. */
  5004. return PCI_ERS_RESULT_NEED_RESET;
  5005. }
  5006. /**
  5007. * bnxt_io_slot_reset - called after the pci bus has been reset.
  5008. * @pdev: Pointer to PCI device
  5009. *
  5010. * Restart the card from scratch, as if from a cold-boot.
  5011. * At this point, the card has exprienced a hard reset,
  5012. * followed by fixups by BIOS, and has its config space
  5013. * set up identically to what it was at cold boot.
  5014. */
  5015. static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
  5016. {
  5017. struct net_device *netdev = pci_get_drvdata(pdev);
  5018. struct bnxt *bp = netdev_priv(netdev);
  5019. int err = 0;
  5020. pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
  5021. netdev_info(bp->dev, "PCI Slot Reset\n");
  5022. rtnl_lock();
  5023. if (pci_enable_device(pdev)) {
  5024. dev_err(&pdev->dev,
  5025. "Cannot re-enable PCI device after reset.\n");
  5026. } else {
  5027. pci_set_master(pdev);
  5028. if (netif_running(netdev))
  5029. err = bnxt_open(netdev);
  5030. if (!err)
  5031. result = PCI_ERS_RESULT_RECOVERED;
  5032. }
  5033. if (result != PCI_ERS_RESULT_RECOVERED && netif_running(netdev))
  5034. dev_close(netdev);
  5035. rtnl_unlock();
  5036. err = pci_cleanup_aer_uncorrect_error_status(pdev);
  5037. if (err) {
  5038. dev_err(&pdev->dev,
  5039. "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
  5040. err); /* non-fatal, continue */
  5041. }
  5042. return PCI_ERS_RESULT_RECOVERED;
  5043. }
  5044. /**
  5045. * bnxt_io_resume - called when traffic can start flowing again.
  5046. * @pdev: Pointer to PCI device
  5047. *
  5048. * This callback is called when the error recovery driver tells
  5049. * us that its OK to resume normal operation.
  5050. */
  5051. static void bnxt_io_resume(struct pci_dev *pdev)
  5052. {
  5053. struct net_device *netdev = pci_get_drvdata(pdev);
  5054. rtnl_lock();
  5055. netif_device_attach(netdev);
  5056. rtnl_unlock();
  5057. }
  5058. static const struct pci_error_handlers bnxt_err_handler = {
  5059. .error_detected = bnxt_io_error_detected,
  5060. .slot_reset = bnxt_io_slot_reset,
  5061. .resume = bnxt_io_resume
  5062. };
  5063. static struct pci_driver bnxt_pci_driver = {
  5064. .name = DRV_MODULE_NAME,
  5065. .id_table = bnxt_pci_tbl,
  5066. .probe = bnxt_init_one,
  5067. .remove = bnxt_remove_one,
  5068. .err_handler = &bnxt_err_handler,
  5069. #if defined(CONFIG_BNXT_SRIOV)
  5070. .sriov_configure = bnxt_sriov_configure,
  5071. #endif
  5072. };
  5073. module_pci_driver(bnxt_pci_driver);