bnxt.c 196 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721
  1. /* Broadcom NetXtreme-C/E network driver.
  2. *
  3. * Copyright (c) 2014-2016 Broadcom Corporation
  4. * Copyright (c) 2016-2017 Broadcom Limited
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/stringify.h>
  12. #include <linux/kernel.h>
  13. #include <linux/timer.h>
  14. #include <linux/errno.h>
  15. #include <linux/ioport.h>
  16. #include <linux/slab.h>
  17. #include <linux/vmalloc.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/pci.h>
  20. #include <linux/netdevice.h>
  21. #include <linux/etherdevice.h>
  22. #include <linux/skbuff.h>
  23. #include <linux/dma-mapping.h>
  24. #include <linux/bitops.h>
  25. #include <linux/io.h>
  26. #include <linux/irq.h>
  27. #include <linux/delay.h>
  28. #include <asm/byteorder.h>
  29. #include <asm/page.h>
  30. #include <linux/time.h>
  31. #include <linux/mii.h>
  32. #include <linux/if.h>
  33. #include <linux/if_vlan.h>
  34. #include <linux/rtc.h>
  35. #include <linux/bpf.h>
  36. #include <net/ip.h>
  37. #include <net/tcp.h>
  38. #include <net/udp.h>
  39. #include <net/checksum.h>
  40. #include <net/ip6_checksum.h>
  41. #include <net/udp_tunnel.h>
  42. #include <linux/workqueue.h>
  43. #include <linux/prefetch.h>
  44. #include <linux/cache.h>
  45. #include <linux/log2.h>
  46. #include <linux/aer.h>
  47. #include <linux/bitmap.h>
  48. #include <linux/cpu_rmap.h>
  49. #include "bnxt_hsi.h"
  50. #include "bnxt.h"
  51. #include "bnxt_ulp.h"
  52. #include "bnxt_sriov.h"
  53. #include "bnxt_ethtool.h"
  54. #include "bnxt_dcb.h"
  55. #include "bnxt_xdp.h"
  56. #define BNXT_TX_TIMEOUT (5 * HZ)
  57. static const char version[] =
  58. "Broadcom NetXtreme-C/E driver " DRV_MODULE_NAME " v" DRV_MODULE_VERSION "\n";
  59. MODULE_LICENSE("GPL");
  60. MODULE_DESCRIPTION("Broadcom BCM573xx network driver");
  61. MODULE_VERSION(DRV_MODULE_VERSION);
  62. #define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN)
  63. #define BNXT_RX_DMA_OFFSET NET_SKB_PAD
  64. #define BNXT_RX_COPY_THRESH 256
  65. #define BNXT_TX_PUSH_THRESH 164
  66. enum board_idx {
  67. BCM57301,
  68. BCM57302,
  69. BCM57304,
  70. BCM57417_NPAR,
  71. BCM58700,
  72. BCM57311,
  73. BCM57312,
  74. BCM57402,
  75. BCM57404,
  76. BCM57406,
  77. BCM57402_NPAR,
  78. BCM57407,
  79. BCM57412,
  80. BCM57414,
  81. BCM57416,
  82. BCM57417,
  83. BCM57412_NPAR,
  84. BCM57314,
  85. BCM57417_SFP,
  86. BCM57416_SFP,
  87. BCM57404_NPAR,
  88. BCM57406_NPAR,
  89. BCM57407_SFP,
  90. BCM57407_NPAR,
  91. BCM57414_NPAR,
  92. BCM57416_NPAR,
  93. BCM57452,
  94. BCM57454,
  95. NETXTREME_E_VF,
  96. NETXTREME_C_VF,
  97. };
  98. /* indexed by enum above */
  99. static const struct {
  100. char *name;
  101. } board_info[] = {
  102. { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" },
  103. { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" },
  104. { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
  105. { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" },
  106. { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" },
  107. { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" },
  108. { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" },
  109. { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" },
  110. { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" },
  111. { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" },
  112. { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" },
  113. { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" },
  114. { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" },
  115. { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" },
  116. { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" },
  117. { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" },
  118. { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" },
  119. { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
  120. { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" },
  121. { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" },
  122. { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" },
  123. { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" },
  124. { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" },
  125. { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" },
  126. { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" },
  127. { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" },
  128. { "Broadcom BCM57452 NetXtreme-E 10Gb/25Gb/40Gb/50Gb Ethernet" },
  129. { "Broadcom BCM57454 NetXtreme-E 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
  130. { "Broadcom NetXtreme-E Ethernet Virtual Function" },
  131. { "Broadcom NetXtreme-C Ethernet Virtual Function" },
  132. };
  133. static const struct pci_device_id bnxt_pci_tbl[] = {
  134. { PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR },
  135. { PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
  136. { PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
  137. { PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
  138. { PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR },
  139. { PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 },
  140. { PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
  141. { PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
  142. { PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
  143. { PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
  144. { PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
  145. { PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR },
  146. { PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 },
  147. { PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
  148. { PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
  149. { PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
  150. { PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
  151. { PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR },
  152. { PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
  153. { PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP },
  154. { PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP },
  155. { PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR },
  156. { PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR },
  157. { PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP },
  158. { PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR },
  159. { PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR },
  160. { PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR },
  161. { PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR },
  162. { PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR },
  163. { PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR },
  164. { PCI_VDEVICE(BROADCOM, 0x16f1), .driver_data = BCM57452 },
  165. { PCI_VDEVICE(BROADCOM, 0x1614), .driver_data = BCM57454 },
  166. #ifdef CONFIG_BNXT_SRIOV
  167. { PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF },
  168. { PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF },
  169. { PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF },
  170. { PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF },
  171. { PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF },
  172. { PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF },
  173. #endif
  174. { 0 }
  175. };
  176. MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
  177. static const u16 bnxt_vf_req_snif[] = {
  178. HWRM_FUNC_CFG,
  179. HWRM_PORT_PHY_QCFG,
  180. HWRM_CFA_L2_FILTER_ALLOC,
  181. };
  182. static const u16 bnxt_async_events_arr[] = {
  183. ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
  184. ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
  185. ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
  186. ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
  187. ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
  188. };
  189. static bool bnxt_vf_pciid(enum board_idx idx)
  190. {
  191. return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF);
  192. }
  193. #define DB_CP_REARM_FLAGS (DB_KEY_CP | DB_IDX_VALID)
  194. #define DB_CP_FLAGS (DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
  195. #define DB_CP_IRQ_DIS_FLAGS (DB_KEY_CP | DB_IRQ_DIS)
  196. #define BNXT_CP_DB_REARM(db, raw_cons) \
  197. writel(DB_CP_REARM_FLAGS | RING_CMP(raw_cons), db)
  198. #define BNXT_CP_DB(db, raw_cons) \
  199. writel(DB_CP_FLAGS | RING_CMP(raw_cons), db)
  200. #define BNXT_CP_DB_IRQ_DIS(db) \
  201. writel(DB_CP_IRQ_DIS_FLAGS, db)
  202. const u16 bnxt_lhint_arr[] = {
  203. TX_BD_FLAGS_LHINT_512_AND_SMALLER,
  204. TX_BD_FLAGS_LHINT_512_TO_1023,
  205. TX_BD_FLAGS_LHINT_1024_TO_2047,
  206. TX_BD_FLAGS_LHINT_1024_TO_2047,
  207. TX_BD_FLAGS_LHINT_2048_AND_LARGER,
  208. TX_BD_FLAGS_LHINT_2048_AND_LARGER,
  209. TX_BD_FLAGS_LHINT_2048_AND_LARGER,
  210. TX_BD_FLAGS_LHINT_2048_AND_LARGER,
  211. TX_BD_FLAGS_LHINT_2048_AND_LARGER,
  212. TX_BD_FLAGS_LHINT_2048_AND_LARGER,
  213. TX_BD_FLAGS_LHINT_2048_AND_LARGER,
  214. TX_BD_FLAGS_LHINT_2048_AND_LARGER,
  215. TX_BD_FLAGS_LHINT_2048_AND_LARGER,
  216. TX_BD_FLAGS_LHINT_2048_AND_LARGER,
  217. TX_BD_FLAGS_LHINT_2048_AND_LARGER,
  218. TX_BD_FLAGS_LHINT_2048_AND_LARGER,
  219. TX_BD_FLAGS_LHINT_2048_AND_LARGER,
  220. TX_BD_FLAGS_LHINT_2048_AND_LARGER,
  221. TX_BD_FLAGS_LHINT_2048_AND_LARGER,
  222. };
  223. static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
  224. {
  225. struct bnxt *bp = netdev_priv(dev);
  226. struct tx_bd *txbd;
  227. struct tx_bd_ext *txbd1;
  228. struct netdev_queue *txq;
  229. int i;
  230. dma_addr_t mapping;
  231. unsigned int length, pad = 0;
  232. u32 len, free_size, vlan_tag_flags, cfa_action, flags;
  233. u16 prod, last_frag;
  234. struct pci_dev *pdev = bp->pdev;
  235. struct bnxt_tx_ring_info *txr;
  236. struct bnxt_sw_tx_bd *tx_buf;
  237. i = skb_get_queue_mapping(skb);
  238. if (unlikely(i >= bp->tx_nr_rings)) {
  239. dev_kfree_skb_any(skb);
  240. return NETDEV_TX_OK;
  241. }
  242. txq = netdev_get_tx_queue(dev, i);
  243. txr = &bp->tx_ring[bp->tx_ring_map[i]];
  244. prod = txr->tx_prod;
  245. free_size = bnxt_tx_avail(bp, txr);
  246. if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
  247. netif_tx_stop_queue(txq);
  248. return NETDEV_TX_BUSY;
  249. }
  250. length = skb->len;
  251. len = skb_headlen(skb);
  252. last_frag = skb_shinfo(skb)->nr_frags;
  253. txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
  254. txbd->tx_bd_opaque = prod;
  255. tx_buf = &txr->tx_buf_ring[prod];
  256. tx_buf->skb = skb;
  257. tx_buf->nr_frags = last_frag;
  258. vlan_tag_flags = 0;
  259. cfa_action = 0;
  260. if (skb_vlan_tag_present(skb)) {
  261. vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
  262. skb_vlan_tag_get(skb);
  263. /* Currently supports 8021Q, 8021AD vlan offloads
  264. * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
  265. */
  266. if (skb->vlan_proto == htons(ETH_P_8021Q))
  267. vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
  268. }
  269. if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh) {
  270. struct tx_push_buffer *tx_push_buf = txr->tx_push;
  271. struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
  272. struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
  273. void *pdata = tx_push_buf->data;
  274. u64 *end;
  275. int j, push_len;
  276. /* Set COAL_NOW to be ready quickly for the next push */
  277. tx_push->tx_bd_len_flags_type =
  278. cpu_to_le32((length << TX_BD_LEN_SHIFT) |
  279. TX_BD_TYPE_LONG_TX_BD |
  280. TX_BD_FLAGS_LHINT_512_AND_SMALLER |
  281. TX_BD_FLAGS_COAL_NOW |
  282. TX_BD_FLAGS_PACKET_END |
  283. (2 << TX_BD_FLAGS_BD_CNT_SHIFT));
  284. if (skb->ip_summed == CHECKSUM_PARTIAL)
  285. tx_push1->tx_bd_hsize_lflags =
  286. cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
  287. else
  288. tx_push1->tx_bd_hsize_lflags = 0;
  289. tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
  290. tx_push1->tx_bd_cfa_action = cpu_to_le32(cfa_action);
  291. end = pdata + length;
  292. end = PTR_ALIGN(end, 8) - 1;
  293. *end = 0;
  294. skb_copy_from_linear_data(skb, pdata, len);
  295. pdata += len;
  296. for (j = 0; j < last_frag; j++) {
  297. skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
  298. void *fptr;
  299. fptr = skb_frag_address_safe(frag);
  300. if (!fptr)
  301. goto normal_tx;
  302. memcpy(pdata, fptr, skb_frag_size(frag));
  303. pdata += skb_frag_size(frag);
  304. }
  305. txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
  306. txbd->tx_bd_haddr = txr->data_mapping;
  307. prod = NEXT_TX(prod);
  308. txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
  309. memcpy(txbd, tx_push1, sizeof(*txbd));
  310. prod = NEXT_TX(prod);
  311. tx_push->doorbell =
  312. cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH | prod);
  313. txr->tx_prod = prod;
  314. tx_buf->is_push = 1;
  315. netdev_tx_sent_queue(txq, skb->len);
  316. wmb(); /* Sync is_push and byte queue before pushing data */
  317. push_len = (length + sizeof(*tx_push) + 7) / 8;
  318. if (push_len > 16) {
  319. __iowrite64_copy(txr->tx_doorbell, tx_push_buf, 16);
  320. __iowrite32_copy(txr->tx_doorbell + 4, tx_push_buf + 1,
  321. (push_len - 16) << 1);
  322. } else {
  323. __iowrite64_copy(txr->tx_doorbell, tx_push_buf,
  324. push_len);
  325. }
  326. goto tx_done;
  327. }
  328. normal_tx:
  329. if (length < BNXT_MIN_PKT_SIZE) {
  330. pad = BNXT_MIN_PKT_SIZE - length;
  331. if (skb_pad(skb, pad)) {
  332. /* SKB already freed. */
  333. tx_buf->skb = NULL;
  334. return NETDEV_TX_OK;
  335. }
  336. length = BNXT_MIN_PKT_SIZE;
  337. }
  338. mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
  339. if (unlikely(dma_mapping_error(&pdev->dev, mapping))) {
  340. dev_kfree_skb_any(skb);
  341. tx_buf->skb = NULL;
  342. return NETDEV_TX_OK;
  343. }
  344. dma_unmap_addr_set(tx_buf, mapping, mapping);
  345. flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
  346. ((last_frag + 2) << TX_BD_FLAGS_BD_CNT_SHIFT);
  347. txbd->tx_bd_haddr = cpu_to_le64(mapping);
  348. prod = NEXT_TX(prod);
  349. txbd1 = (struct tx_bd_ext *)
  350. &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
  351. txbd1->tx_bd_hsize_lflags = 0;
  352. if (skb_is_gso(skb)) {
  353. u32 hdr_len;
  354. if (skb->encapsulation)
  355. hdr_len = skb_inner_network_offset(skb) +
  356. skb_inner_network_header_len(skb) +
  357. inner_tcp_hdrlen(skb);
  358. else
  359. hdr_len = skb_transport_offset(skb) +
  360. tcp_hdrlen(skb);
  361. txbd1->tx_bd_hsize_lflags = cpu_to_le32(TX_BD_FLAGS_LSO |
  362. TX_BD_FLAGS_T_IPID |
  363. (hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
  364. length = skb_shinfo(skb)->gso_size;
  365. txbd1->tx_bd_mss = cpu_to_le32(length);
  366. length += hdr_len;
  367. } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
  368. txbd1->tx_bd_hsize_lflags =
  369. cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
  370. txbd1->tx_bd_mss = 0;
  371. }
  372. length >>= 9;
  373. flags |= bnxt_lhint_arr[length];
  374. txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
  375. txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
  376. txbd1->tx_bd_cfa_action = cpu_to_le32(cfa_action);
  377. for (i = 0; i < last_frag; i++) {
  378. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  379. prod = NEXT_TX(prod);
  380. txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
  381. len = skb_frag_size(frag);
  382. mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
  383. DMA_TO_DEVICE);
  384. if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
  385. goto tx_dma_error;
  386. tx_buf = &txr->tx_buf_ring[prod];
  387. dma_unmap_addr_set(tx_buf, mapping, mapping);
  388. txbd->tx_bd_haddr = cpu_to_le64(mapping);
  389. flags = len << TX_BD_LEN_SHIFT;
  390. txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
  391. }
  392. flags &= ~TX_BD_LEN;
  393. txbd->tx_bd_len_flags_type =
  394. cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
  395. TX_BD_FLAGS_PACKET_END);
  396. netdev_tx_sent_queue(txq, skb->len);
  397. /* Sync BD data before updating doorbell */
  398. wmb();
  399. prod = NEXT_TX(prod);
  400. txr->tx_prod = prod;
  401. writel(DB_KEY_TX | prod, txr->tx_doorbell);
  402. writel(DB_KEY_TX | prod, txr->tx_doorbell);
  403. tx_done:
  404. mmiowb();
  405. if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
  406. netif_tx_stop_queue(txq);
  407. /* netif_tx_stop_queue() must be done before checking
  408. * tx index in bnxt_tx_avail() below, because in
  409. * bnxt_tx_int(), we update tx index before checking for
  410. * netif_tx_queue_stopped().
  411. */
  412. smp_mb();
  413. if (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)
  414. netif_tx_wake_queue(txq);
  415. }
  416. return NETDEV_TX_OK;
  417. tx_dma_error:
  418. last_frag = i;
  419. /* start back at beginning and unmap skb */
  420. prod = txr->tx_prod;
  421. tx_buf = &txr->tx_buf_ring[prod];
  422. tx_buf->skb = NULL;
  423. dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
  424. skb_headlen(skb), PCI_DMA_TODEVICE);
  425. prod = NEXT_TX(prod);
  426. /* unmap remaining mapped pages */
  427. for (i = 0; i < last_frag; i++) {
  428. prod = NEXT_TX(prod);
  429. tx_buf = &txr->tx_buf_ring[prod];
  430. dma_unmap_page(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
  431. skb_frag_size(&skb_shinfo(skb)->frags[i]),
  432. PCI_DMA_TODEVICE);
  433. }
  434. dev_kfree_skb_any(skb);
  435. return NETDEV_TX_OK;
  436. }
  437. static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int nr_pkts)
  438. {
  439. struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
  440. struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
  441. u16 cons = txr->tx_cons;
  442. struct pci_dev *pdev = bp->pdev;
  443. int i;
  444. unsigned int tx_bytes = 0;
  445. for (i = 0; i < nr_pkts; i++) {
  446. struct bnxt_sw_tx_bd *tx_buf;
  447. struct sk_buff *skb;
  448. int j, last;
  449. tx_buf = &txr->tx_buf_ring[cons];
  450. cons = NEXT_TX(cons);
  451. skb = tx_buf->skb;
  452. tx_buf->skb = NULL;
  453. if (tx_buf->is_push) {
  454. tx_buf->is_push = 0;
  455. goto next_tx_int;
  456. }
  457. dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
  458. skb_headlen(skb), PCI_DMA_TODEVICE);
  459. last = tx_buf->nr_frags;
  460. for (j = 0; j < last; j++) {
  461. cons = NEXT_TX(cons);
  462. tx_buf = &txr->tx_buf_ring[cons];
  463. dma_unmap_page(
  464. &pdev->dev,
  465. dma_unmap_addr(tx_buf, mapping),
  466. skb_frag_size(&skb_shinfo(skb)->frags[j]),
  467. PCI_DMA_TODEVICE);
  468. }
  469. next_tx_int:
  470. cons = NEXT_TX(cons);
  471. tx_bytes += skb->len;
  472. dev_kfree_skb_any(skb);
  473. }
  474. netdev_tx_completed_queue(txq, nr_pkts, tx_bytes);
  475. txr->tx_cons = cons;
  476. /* Need to make the tx_cons update visible to bnxt_start_xmit()
  477. * before checking for netif_tx_queue_stopped(). Without the
  478. * memory barrier, there is a small possibility that bnxt_start_xmit()
  479. * will miss it and cause the queue to be stopped forever.
  480. */
  481. smp_mb();
  482. if (unlikely(netif_tx_queue_stopped(txq)) &&
  483. (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)) {
  484. __netif_tx_lock(txq, smp_processor_id());
  485. if (netif_tx_queue_stopped(txq) &&
  486. bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh &&
  487. txr->dev_state != BNXT_DEV_STATE_CLOSING)
  488. netif_tx_wake_queue(txq);
  489. __netif_tx_unlock(txq);
  490. }
  491. }
  492. static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping,
  493. gfp_t gfp)
  494. {
  495. struct device *dev = &bp->pdev->dev;
  496. struct page *page;
  497. page = alloc_page(gfp);
  498. if (!page)
  499. return NULL;
  500. *mapping = dma_map_page(dev, page, 0, PAGE_SIZE, bp->rx_dir);
  501. if (dma_mapping_error(dev, *mapping)) {
  502. __free_page(page);
  503. return NULL;
  504. }
  505. *mapping += bp->rx_dma_offset;
  506. return page;
  507. }
  508. static inline u8 *__bnxt_alloc_rx_data(struct bnxt *bp, dma_addr_t *mapping,
  509. gfp_t gfp)
  510. {
  511. u8 *data;
  512. struct pci_dev *pdev = bp->pdev;
  513. data = kmalloc(bp->rx_buf_size, gfp);
  514. if (!data)
  515. return NULL;
  516. *mapping = dma_map_single(&pdev->dev, data + bp->rx_dma_offset,
  517. bp->rx_buf_use_size, bp->rx_dir);
  518. if (dma_mapping_error(&pdev->dev, *mapping)) {
  519. kfree(data);
  520. data = NULL;
  521. }
  522. return data;
  523. }
  524. int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
  525. u16 prod, gfp_t gfp)
  526. {
  527. struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
  528. struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[prod];
  529. dma_addr_t mapping;
  530. if (BNXT_RX_PAGE_MODE(bp)) {
  531. struct page *page = __bnxt_alloc_rx_page(bp, &mapping, gfp);
  532. if (!page)
  533. return -ENOMEM;
  534. rx_buf->data = page;
  535. rx_buf->data_ptr = page_address(page) + bp->rx_offset;
  536. } else {
  537. u8 *data = __bnxt_alloc_rx_data(bp, &mapping, gfp);
  538. if (!data)
  539. return -ENOMEM;
  540. rx_buf->data = data;
  541. rx_buf->data_ptr = data + bp->rx_offset;
  542. }
  543. rx_buf->mapping = mapping;
  544. rxbd->rx_bd_haddr = cpu_to_le64(mapping);
  545. return 0;
  546. }
  547. void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data)
  548. {
  549. u16 prod = rxr->rx_prod;
  550. struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
  551. struct rx_bd *cons_bd, *prod_bd;
  552. prod_rx_buf = &rxr->rx_buf_ring[prod];
  553. cons_rx_buf = &rxr->rx_buf_ring[cons];
  554. prod_rx_buf->data = data;
  555. prod_rx_buf->data_ptr = cons_rx_buf->data_ptr;
  556. prod_rx_buf->mapping = cons_rx_buf->mapping;
  557. prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
  558. cons_bd = &rxr->rx_desc_ring[RX_RING(cons)][RX_IDX(cons)];
  559. prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
  560. }
  561. static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
  562. {
  563. u16 next, max = rxr->rx_agg_bmap_size;
  564. next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
  565. if (next >= max)
  566. next = find_first_zero_bit(rxr->rx_agg_bmap, max);
  567. return next;
  568. }
  569. static inline int bnxt_alloc_rx_page(struct bnxt *bp,
  570. struct bnxt_rx_ring_info *rxr,
  571. u16 prod, gfp_t gfp)
  572. {
  573. struct rx_bd *rxbd =
  574. &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
  575. struct bnxt_sw_rx_agg_bd *rx_agg_buf;
  576. struct pci_dev *pdev = bp->pdev;
  577. struct page *page;
  578. dma_addr_t mapping;
  579. u16 sw_prod = rxr->rx_sw_agg_prod;
  580. unsigned int offset = 0;
  581. if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) {
  582. page = rxr->rx_page;
  583. if (!page) {
  584. page = alloc_page(gfp);
  585. if (!page)
  586. return -ENOMEM;
  587. rxr->rx_page = page;
  588. rxr->rx_page_offset = 0;
  589. }
  590. offset = rxr->rx_page_offset;
  591. rxr->rx_page_offset += BNXT_RX_PAGE_SIZE;
  592. if (rxr->rx_page_offset == PAGE_SIZE)
  593. rxr->rx_page = NULL;
  594. else
  595. get_page(page);
  596. } else {
  597. page = alloc_page(gfp);
  598. if (!page)
  599. return -ENOMEM;
  600. }
  601. mapping = dma_map_page(&pdev->dev, page, offset, BNXT_RX_PAGE_SIZE,
  602. PCI_DMA_FROMDEVICE);
  603. if (dma_mapping_error(&pdev->dev, mapping)) {
  604. __free_page(page);
  605. return -EIO;
  606. }
  607. if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
  608. sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
  609. __set_bit(sw_prod, rxr->rx_agg_bmap);
  610. rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
  611. rxr->rx_sw_agg_prod = NEXT_RX_AGG(sw_prod);
  612. rx_agg_buf->page = page;
  613. rx_agg_buf->offset = offset;
  614. rx_agg_buf->mapping = mapping;
  615. rxbd->rx_bd_haddr = cpu_to_le64(mapping);
  616. rxbd->rx_bd_opaque = sw_prod;
  617. return 0;
  618. }
  619. static void bnxt_reuse_rx_agg_bufs(struct bnxt_napi *bnapi, u16 cp_cons,
  620. u32 agg_bufs)
  621. {
  622. struct bnxt *bp = bnapi->bp;
  623. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  624. struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
  625. u16 prod = rxr->rx_agg_prod;
  626. u16 sw_prod = rxr->rx_sw_agg_prod;
  627. u32 i;
  628. for (i = 0; i < agg_bufs; i++) {
  629. u16 cons;
  630. struct rx_agg_cmp *agg;
  631. struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
  632. struct rx_bd *prod_bd;
  633. struct page *page;
  634. agg = (struct rx_agg_cmp *)
  635. &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
  636. cons = agg->rx_agg_cmp_opaque;
  637. __clear_bit(cons, rxr->rx_agg_bmap);
  638. if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
  639. sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
  640. __set_bit(sw_prod, rxr->rx_agg_bmap);
  641. prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
  642. cons_rx_buf = &rxr->rx_agg_ring[cons];
  643. /* It is possible for sw_prod to be equal to cons, so
  644. * set cons_rx_buf->page to NULL first.
  645. */
  646. page = cons_rx_buf->page;
  647. cons_rx_buf->page = NULL;
  648. prod_rx_buf->page = page;
  649. prod_rx_buf->offset = cons_rx_buf->offset;
  650. prod_rx_buf->mapping = cons_rx_buf->mapping;
  651. prod_bd = &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
  652. prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
  653. prod_bd->rx_bd_opaque = sw_prod;
  654. prod = NEXT_RX_AGG(prod);
  655. sw_prod = NEXT_RX_AGG(sw_prod);
  656. cp_cons = NEXT_CMP(cp_cons);
  657. }
  658. rxr->rx_agg_prod = prod;
  659. rxr->rx_sw_agg_prod = sw_prod;
  660. }
  661. static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp,
  662. struct bnxt_rx_ring_info *rxr,
  663. u16 cons, void *data, u8 *data_ptr,
  664. dma_addr_t dma_addr,
  665. unsigned int offset_and_len)
  666. {
  667. unsigned int payload = offset_and_len >> 16;
  668. unsigned int len = offset_and_len & 0xffff;
  669. struct skb_frag_struct *frag;
  670. struct page *page = data;
  671. u16 prod = rxr->rx_prod;
  672. struct sk_buff *skb;
  673. int off, err;
  674. err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
  675. if (unlikely(err)) {
  676. bnxt_reuse_rx_data(rxr, cons, data);
  677. return NULL;
  678. }
  679. dma_addr -= bp->rx_dma_offset;
  680. dma_unmap_page(&bp->pdev->dev, dma_addr, PAGE_SIZE, bp->rx_dir);
  681. if (unlikely(!payload))
  682. payload = eth_get_headlen(data_ptr, len);
  683. skb = napi_alloc_skb(&rxr->bnapi->napi, payload);
  684. if (!skb) {
  685. __free_page(page);
  686. return NULL;
  687. }
  688. off = (void *)data_ptr - page_address(page);
  689. skb_add_rx_frag(skb, 0, page, off, len, PAGE_SIZE);
  690. memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN,
  691. payload + NET_IP_ALIGN);
  692. frag = &skb_shinfo(skb)->frags[0];
  693. skb_frag_size_sub(frag, payload);
  694. frag->page_offset += payload;
  695. skb->data_len -= payload;
  696. skb->tail += payload;
  697. return skb;
  698. }
  699. static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
  700. struct bnxt_rx_ring_info *rxr, u16 cons,
  701. void *data, u8 *data_ptr,
  702. dma_addr_t dma_addr,
  703. unsigned int offset_and_len)
  704. {
  705. u16 prod = rxr->rx_prod;
  706. struct sk_buff *skb;
  707. int err;
  708. err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
  709. if (unlikely(err)) {
  710. bnxt_reuse_rx_data(rxr, cons, data);
  711. return NULL;
  712. }
  713. skb = build_skb(data, 0);
  714. dma_unmap_single(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
  715. bp->rx_dir);
  716. if (!skb) {
  717. kfree(data);
  718. return NULL;
  719. }
  720. skb_reserve(skb, bp->rx_offset);
  721. skb_put(skb, offset_and_len & 0xffff);
  722. return skb;
  723. }
  724. static struct sk_buff *bnxt_rx_pages(struct bnxt *bp, struct bnxt_napi *bnapi,
  725. struct sk_buff *skb, u16 cp_cons,
  726. u32 agg_bufs)
  727. {
  728. struct pci_dev *pdev = bp->pdev;
  729. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  730. struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
  731. u16 prod = rxr->rx_agg_prod;
  732. u32 i;
  733. for (i = 0; i < agg_bufs; i++) {
  734. u16 cons, frag_len;
  735. struct rx_agg_cmp *agg;
  736. struct bnxt_sw_rx_agg_bd *cons_rx_buf;
  737. struct page *page;
  738. dma_addr_t mapping;
  739. agg = (struct rx_agg_cmp *)
  740. &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
  741. cons = agg->rx_agg_cmp_opaque;
  742. frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
  743. RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
  744. cons_rx_buf = &rxr->rx_agg_ring[cons];
  745. skb_fill_page_desc(skb, i, cons_rx_buf->page,
  746. cons_rx_buf->offset, frag_len);
  747. __clear_bit(cons, rxr->rx_agg_bmap);
  748. /* It is possible for bnxt_alloc_rx_page() to allocate
  749. * a sw_prod index that equals the cons index, so we
  750. * need to clear the cons entry now.
  751. */
  752. mapping = cons_rx_buf->mapping;
  753. page = cons_rx_buf->page;
  754. cons_rx_buf->page = NULL;
  755. if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_ATOMIC) != 0) {
  756. struct skb_shared_info *shinfo;
  757. unsigned int nr_frags;
  758. shinfo = skb_shinfo(skb);
  759. nr_frags = --shinfo->nr_frags;
  760. __skb_frag_set_page(&shinfo->frags[nr_frags], NULL);
  761. dev_kfree_skb(skb);
  762. cons_rx_buf->page = page;
  763. /* Update prod since possibly some pages have been
  764. * allocated already.
  765. */
  766. rxr->rx_agg_prod = prod;
  767. bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs - i);
  768. return NULL;
  769. }
  770. dma_unmap_page(&pdev->dev, mapping, BNXT_RX_PAGE_SIZE,
  771. PCI_DMA_FROMDEVICE);
  772. skb->data_len += frag_len;
  773. skb->len += frag_len;
  774. skb->truesize += PAGE_SIZE;
  775. prod = NEXT_RX_AGG(prod);
  776. cp_cons = NEXT_CMP(cp_cons);
  777. }
  778. rxr->rx_agg_prod = prod;
  779. return skb;
  780. }
  781. static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
  782. u8 agg_bufs, u32 *raw_cons)
  783. {
  784. u16 last;
  785. struct rx_agg_cmp *agg;
  786. *raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
  787. last = RING_CMP(*raw_cons);
  788. agg = (struct rx_agg_cmp *)
  789. &cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
  790. return RX_AGG_CMP_VALID(agg, *raw_cons);
  791. }
  792. static inline struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
  793. unsigned int len,
  794. dma_addr_t mapping)
  795. {
  796. struct bnxt *bp = bnapi->bp;
  797. struct pci_dev *pdev = bp->pdev;
  798. struct sk_buff *skb;
  799. skb = napi_alloc_skb(&bnapi->napi, len);
  800. if (!skb)
  801. return NULL;
  802. dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copy_thresh,
  803. bp->rx_dir);
  804. memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN,
  805. len + NET_IP_ALIGN);
  806. dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copy_thresh,
  807. bp->rx_dir);
  808. skb_put(skb, len);
  809. return skb;
  810. }
  811. static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_napi *bnapi,
  812. u32 *raw_cons, void *cmp)
  813. {
  814. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  815. struct rx_cmp *rxcmp = cmp;
  816. u32 tmp_raw_cons = *raw_cons;
  817. u8 cmp_type, agg_bufs = 0;
  818. cmp_type = RX_CMP_TYPE(rxcmp);
  819. if (cmp_type == CMP_TYPE_RX_L2_CMP) {
  820. agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
  821. RX_CMP_AGG_BUFS) >>
  822. RX_CMP_AGG_BUFS_SHIFT;
  823. } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
  824. struct rx_tpa_end_cmp *tpa_end = cmp;
  825. agg_bufs = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
  826. RX_TPA_END_CMP_AGG_BUFS) >>
  827. RX_TPA_END_CMP_AGG_BUFS_SHIFT;
  828. }
  829. if (agg_bufs) {
  830. if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
  831. return -EBUSY;
  832. }
  833. *raw_cons = tmp_raw_cons;
  834. return 0;
  835. }
  836. static void bnxt_sched_reset(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
  837. {
  838. if (!rxr->bnapi->in_reset) {
  839. rxr->bnapi->in_reset = true;
  840. set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
  841. schedule_work(&bp->sp_task);
  842. }
  843. rxr->rx_next_cons = 0xffff;
  844. }
  845. static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
  846. struct rx_tpa_start_cmp *tpa_start,
  847. struct rx_tpa_start_cmp_ext *tpa_start1)
  848. {
  849. u8 agg_id = TPA_START_AGG_ID(tpa_start);
  850. u16 cons, prod;
  851. struct bnxt_tpa_info *tpa_info;
  852. struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
  853. struct rx_bd *prod_bd;
  854. dma_addr_t mapping;
  855. cons = tpa_start->rx_tpa_start_cmp_opaque;
  856. prod = rxr->rx_prod;
  857. cons_rx_buf = &rxr->rx_buf_ring[cons];
  858. prod_rx_buf = &rxr->rx_buf_ring[prod];
  859. tpa_info = &rxr->rx_tpa[agg_id];
  860. if (unlikely(cons != rxr->rx_next_cons)) {
  861. bnxt_sched_reset(bp, rxr);
  862. return;
  863. }
  864. prod_rx_buf->data = tpa_info->data;
  865. prod_rx_buf->data_ptr = tpa_info->data_ptr;
  866. mapping = tpa_info->mapping;
  867. prod_rx_buf->mapping = mapping;
  868. prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
  869. prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
  870. tpa_info->data = cons_rx_buf->data;
  871. tpa_info->data_ptr = cons_rx_buf->data_ptr;
  872. cons_rx_buf->data = NULL;
  873. tpa_info->mapping = cons_rx_buf->mapping;
  874. tpa_info->len =
  875. le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
  876. RX_TPA_START_CMP_LEN_SHIFT;
  877. if (likely(TPA_START_HASH_VALID(tpa_start))) {
  878. u32 hash_type = TPA_START_HASH_TYPE(tpa_start);
  879. tpa_info->hash_type = PKT_HASH_TYPE_L4;
  880. tpa_info->gso_type = SKB_GSO_TCPV4;
  881. /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
  882. if (hash_type == 3)
  883. tpa_info->gso_type = SKB_GSO_TCPV6;
  884. tpa_info->rss_hash =
  885. le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
  886. } else {
  887. tpa_info->hash_type = PKT_HASH_TYPE_NONE;
  888. tpa_info->gso_type = 0;
  889. if (netif_msg_rx_err(bp))
  890. netdev_warn(bp->dev, "TPA packet without valid hash\n");
  891. }
  892. tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
  893. tpa_info->metadata = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
  894. tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
  895. rxr->rx_prod = NEXT_RX(prod);
  896. cons = NEXT_RX(cons);
  897. rxr->rx_next_cons = NEXT_RX(cons);
  898. cons_rx_buf = &rxr->rx_buf_ring[cons];
  899. bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
  900. rxr->rx_prod = NEXT_RX(rxr->rx_prod);
  901. cons_rx_buf->data = NULL;
  902. }
  903. static void bnxt_abort_tpa(struct bnxt *bp, struct bnxt_napi *bnapi,
  904. u16 cp_cons, u32 agg_bufs)
  905. {
  906. if (agg_bufs)
  907. bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs);
  908. }
  909. static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
  910. int payload_off, int tcp_ts,
  911. struct sk_buff *skb)
  912. {
  913. #ifdef CONFIG_INET
  914. struct tcphdr *th;
  915. int len, nw_off;
  916. u16 outer_ip_off, inner_ip_off, inner_mac_off;
  917. u32 hdr_info = tpa_info->hdr_info;
  918. bool loopback = false;
  919. inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
  920. inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
  921. outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
  922. /* If the packet is an internal loopback packet, the offsets will
  923. * have an extra 4 bytes.
  924. */
  925. if (inner_mac_off == 4) {
  926. loopback = true;
  927. } else if (inner_mac_off > 4) {
  928. __be16 proto = *((__be16 *)(skb->data + inner_ip_off -
  929. ETH_HLEN - 2));
  930. /* We only support inner iPv4/ipv6. If we don't see the
  931. * correct protocol ID, it must be a loopback packet where
  932. * the offsets are off by 4.
  933. */
  934. if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
  935. loopback = true;
  936. }
  937. if (loopback) {
  938. /* internal loopback packet, subtract all offsets by 4 */
  939. inner_ip_off -= 4;
  940. inner_mac_off -= 4;
  941. outer_ip_off -= 4;
  942. }
  943. nw_off = inner_ip_off - ETH_HLEN;
  944. skb_set_network_header(skb, nw_off);
  945. if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
  946. struct ipv6hdr *iph = ipv6_hdr(skb);
  947. skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
  948. len = skb->len - skb_transport_offset(skb);
  949. th = tcp_hdr(skb);
  950. th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
  951. } else {
  952. struct iphdr *iph = ip_hdr(skb);
  953. skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
  954. len = skb->len - skb_transport_offset(skb);
  955. th = tcp_hdr(skb);
  956. th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
  957. }
  958. if (inner_mac_off) { /* tunnel */
  959. struct udphdr *uh = NULL;
  960. __be16 proto = *((__be16 *)(skb->data + outer_ip_off -
  961. ETH_HLEN - 2));
  962. if (proto == htons(ETH_P_IP)) {
  963. struct iphdr *iph = (struct iphdr *)skb->data;
  964. if (iph->protocol == IPPROTO_UDP)
  965. uh = (struct udphdr *)(iph + 1);
  966. } else {
  967. struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
  968. if (iph->nexthdr == IPPROTO_UDP)
  969. uh = (struct udphdr *)(iph + 1);
  970. }
  971. if (uh) {
  972. if (uh->check)
  973. skb_shinfo(skb)->gso_type |=
  974. SKB_GSO_UDP_TUNNEL_CSUM;
  975. else
  976. skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
  977. }
  978. }
  979. #endif
  980. return skb;
  981. }
  982. #define BNXT_IPV4_HDR_SIZE (sizeof(struct iphdr) + sizeof(struct tcphdr))
  983. #define BNXT_IPV6_HDR_SIZE (sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
  984. static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
  985. int payload_off, int tcp_ts,
  986. struct sk_buff *skb)
  987. {
  988. #ifdef CONFIG_INET
  989. struct tcphdr *th;
  990. int len, nw_off, tcp_opt_len = 0;
  991. if (tcp_ts)
  992. tcp_opt_len = 12;
  993. if (tpa_info->gso_type == SKB_GSO_TCPV4) {
  994. struct iphdr *iph;
  995. nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
  996. ETH_HLEN;
  997. skb_set_network_header(skb, nw_off);
  998. iph = ip_hdr(skb);
  999. skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
  1000. len = skb->len - skb_transport_offset(skb);
  1001. th = tcp_hdr(skb);
  1002. th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
  1003. } else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
  1004. struct ipv6hdr *iph;
  1005. nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
  1006. ETH_HLEN;
  1007. skb_set_network_header(skb, nw_off);
  1008. iph = ipv6_hdr(skb);
  1009. skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
  1010. len = skb->len - skb_transport_offset(skb);
  1011. th = tcp_hdr(skb);
  1012. th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
  1013. } else {
  1014. dev_kfree_skb_any(skb);
  1015. return NULL;
  1016. }
  1017. if (nw_off) { /* tunnel */
  1018. struct udphdr *uh = NULL;
  1019. if (skb->protocol == htons(ETH_P_IP)) {
  1020. struct iphdr *iph = (struct iphdr *)skb->data;
  1021. if (iph->protocol == IPPROTO_UDP)
  1022. uh = (struct udphdr *)(iph + 1);
  1023. } else {
  1024. struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
  1025. if (iph->nexthdr == IPPROTO_UDP)
  1026. uh = (struct udphdr *)(iph + 1);
  1027. }
  1028. if (uh) {
  1029. if (uh->check)
  1030. skb_shinfo(skb)->gso_type |=
  1031. SKB_GSO_UDP_TUNNEL_CSUM;
  1032. else
  1033. skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
  1034. }
  1035. }
  1036. #endif
  1037. return skb;
  1038. }
  1039. static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
  1040. struct bnxt_tpa_info *tpa_info,
  1041. struct rx_tpa_end_cmp *tpa_end,
  1042. struct rx_tpa_end_cmp_ext *tpa_end1,
  1043. struct sk_buff *skb)
  1044. {
  1045. #ifdef CONFIG_INET
  1046. int payload_off;
  1047. u16 segs;
  1048. segs = TPA_END_TPA_SEGS(tpa_end);
  1049. if (segs == 1)
  1050. return skb;
  1051. NAPI_GRO_CB(skb)->count = segs;
  1052. skb_shinfo(skb)->gso_size =
  1053. le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
  1054. skb_shinfo(skb)->gso_type = tpa_info->gso_type;
  1055. payload_off = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
  1056. RX_TPA_END_CMP_PAYLOAD_OFFSET) >>
  1057. RX_TPA_END_CMP_PAYLOAD_OFFSET_SHIFT;
  1058. skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
  1059. if (likely(skb))
  1060. tcp_gro_complete(skb);
  1061. #endif
  1062. return skb;
  1063. }
  1064. static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
  1065. struct bnxt_napi *bnapi,
  1066. u32 *raw_cons,
  1067. struct rx_tpa_end_cmp *tpa_end,
  1068. struct rx_tpa_end_cmp_ext *tpa_end1,
  1069. u8 *event)
  1070. {
  1071. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  1072. struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
  1073. u8 agg_id = TPA_END_AGG_ID(tpa_end);
  1074. u8 *data_ptr, agg_bufs;
  1075. u16 cp_cons = RING_CMP(*raw_cons);
  1076. unsigned int len;
  1077. struct bnxt_tpa_info *tpa_info;
  1078. dma_addr_t mapping;
  1079. struct sk_buff *skb;
  1080. void *data;
  1081. if (unlikely(bnapi->in_reset)) {
  1082. int rc = bnxt_discard_rx(bp, bnapi, raw_cons, tpa_end);
  1083. if (rc < 0)
  1084. return ERR_PTR(-EBUSY);
  1085. return NULL;
  1086. }
  1087. tpa_info = &rxr->rx_tpa[agg_id];
  1088. data = tpa_info->data;
  1089. data_ptr = tpa_info->data_ptr;
  1090. prefetch(data_ptr);
  1091. len = tpa_info->len;
  1092. mapping = tpa_info->mapping;
  1093. agg_bufs = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
  1094. RX_TPA_END_CMP_AGG_BUFS) >> RX_TPA_END_CMP_AGG_BUFS_SHIFT;
  1095. if (agg_bufs) {
  1096. if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
  1097. return ERR_PTR(-EBUSY);
  1098. *event |= BNXT_AGG_EVENT;
  1099. cp_cons = NEXT_CMP(cp_cons);
  1100. }
  1101. if (unlikely(agg_bufs > MAX_SKB_FRAGS)) {
  1102. bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
  1103. netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
  1104. agg_bufs, (int)MAX_SKB_FRAGS);
  1105. return NULL;
  1106. }
  1107. if (len <= bp->rx_copy_thresh) {
  1108. skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping);
  1109. if (!skb) {
  1110. bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
  1111. return NULL;
  1112. }
  1113. } else {
  1114. u8 *new_data;
  1115. dma_addr_t new_mapping;
  1116. new_data = __bnxt_alloc_rx_data(bp, &new_mapping, GFP_ATOMIC);
  1117. if (!new_data) {
  1118. bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
  1119. return NULL;
  1120. }
  1121. tpa_info->data = new_data;
  1122. tpa_info->data_ptr = new_data + bp->rx_offset;
  1123. tpa_info->mapping = new_mapping;
  1124. skb = build_skb(data, 0);
  1125. dma_unmap_single(&bp->pdev->dev, mapping, bp->rx_buf_use_size,
  1126. bp->rx_dir);
  1127. if (!skb) {
  1128. kfree(data);
  1129. bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
  1130. return NULL;
  1131. }
  1132. skb_reserve(skb, bp->rx_offset);
  1133. skb_put(skb, len);
  1134. }
  1135. if (agg_bufs) {
  1136. skb = bnxt_rx_pages(bp, bnapi, skb, cp_cons, agg_bufs);
  1137. if (!skb) {
  1138. /* Page reuse already handled by bnxt_rx_pages(). */
  1139. return NULL;
  1140. }
  1141. }
  1142. skb->protocol = eth_type_trans(skb, bp->dev);
  1143. if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
  1144. skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
  1145. if ((tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) &&
  1146. (skb->dev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
  1147. u16 vlan_proto = tpa_info->metadata >>
  1148. RX_CMP_FLAGS2_METADATA_TPID_SFT;
  1149. u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_VID_MASK;
  1150. __vlan_hwaccel_put_tag(skb, htons(vlan_proto), vtag);
  1151. }
  1152. skb_checksum_none_assert(skb);
  1153. if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
  1154. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1155. skb->csum_level =
  1156. (tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
  1157. }
  1158. if (TPA_END_GRO(tpa_end))
  1159. skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb);
  1160. return skb;
  1161. }
  1162. /* returns the following:
  1163. * 1 - 1 packet successfully received
  1164. * 0 - successful TPA_START, packet not completed yet
  1165. * -EBUSY - completion ring does not have all the agg buffers yet
  1166. * -ENOMEM - packet aborted due to out of memory
  1167. * -EIO - packet aborted due to hw error indicated in BD
  1168. */
  1169. static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_napi *bnapi, u32 *raw_cons,
  1170. u8 *event)
  1171. {
  1172. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  1173. struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
  1174. struct net_device *dev = bp->dev;
  1175. struct rx_cmp *rxcmp;
  1176. struct rx_cmp_ext *rxcmp1;
  1177. u32 tmp_raw_cons = *raw_cons;
  1178. u16 cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
  1179. struct bnxt_sw_rx_bd *rx_buf;
  1180. unsigned int len;
  1181. u8 *data_ptr, agg_bufs, cmp_type;
  1182. dma_addr_t dma_addr;
  1183. struct sk_buff *skb;
  1184. void *data;
  1185. int rc = 0;
  1186. u32 misc;
  1187. rxcmp = (struct rx_cmp *)
  1188. &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
  1189. tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
  1190. cp_cons = RING_CMP(tmp_raw_cons);
  1191. rxcmp1 = (struct rx_cmp_ext *)
  1192. &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
  1193. if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
  1194. return -EBUSY;
  1195. cmp_type = RX_CMP_TYPE(rxcmp);
  1196. prod = rxr->rx_prod;
  1197. if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP) {
  1198. bnxt_tpa_start(bp, rxr, (struct rx_tpa_start_cmp *)rxcmp,
  1199. (struct rx_tpa_start_cmp_ext *)rxcmp1);
  1200. *event |= BNXT_RX_EVENT;
  1201. goto next_rx_no_prod;
  1202. } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
  1203. skb = bnxt_tpa_end(bp, bnapi, &tmp_raw_cons,
  1204. (struct rx_tpa_end_cmp *)rxcmp,
  1205. (struct rx_tpa_end_cmp_ext *)rxcmp1, event);
  1206. if (unlikely(IS_ERR(skb)))
  1207. return -EBUSY;
  1208. rc = -ENOMEM;
  1209. if (likely(skb)) {
  1210. skb_record_rx_queue(skb, bnapi->index);
  1211. napi_gro_receive(&bnapi->napi, skb);
  1212. rc = 1;
  1213. }
  1214. *event |= BNXT_RX_EVENT;
  1215. goto next_rx_no_prod;
  1216. }
  1217. cons = rxcmp->rx_cmp_opaque;
  1218. rx_buf = &rxr->rx_buf_ring[cons];
  1219. data = rx_buf->data;
  1220. data_ptr = rx_buf->data_ptr;
  1221. if (unlikely(cons != rxr->rx_next_cons)) {
  1222. int rc1 = bnxt_discard_rx(bp, bnapi, raw_cons, rxcmp);
  1223. bnxt_sched_reset(bp, rxr);
  1224. return rc1;
  1225. }
  1226. prefetch(data_ptr);
  1227. misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1);
  1228. agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT;
  1229. if (agg_bufs) {
  1230. if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
  1231. return -EBUSY;
  1232. cp_cons = NEXT_CMP(cp_cons);
  1233. *event |= BNXT_AGG_EVENT;
  1234. }
  1235. *event |= BNXT_RX_EVENT;
  1236. rx_buf->data = NULL;
  1237. if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
  1238. bnxt_reuse_rx_data(rxr, cons, data);
  1239. if (agg_bufs)
  1240. bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs);
  1241. rc = -EIO;
  1242. goto next_rx;
  1243. }
  1244. len = le32_to_cpu(rxcmp->rx_cmp_len_flags_type) >> RX_CMP_LEN_SHIFT;
  1245. dma_addr = rx_buf->mapping;
  1246. if (bnxt_rx_xdp(bp, rxr, cons, data, &data_ptr, &len, event)) {
  1247. rc = 1;
  1248. goto next_rx;
  1249. }
  1250. if (len <= bp->rx_copy_thresh) {
  1251. skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr);
  1252. bnxt_reuse_rx_data(rxr, cons, data);
  1253. if (!skb) {
  1254. rc = -ENOMEM;
  1255. goto next_rx;
  1256. }
  1257. } else {
  1258. u32 payload;
  1259. if (rx_buf->data_ptr == data_ptr)
  1260. payload = misc & RX_CMP_PAYLOAD_OFFSET;
  1261. else
  1262. payload = 0;
  1263. skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr,
  1264. payload | len);
  1265. if (!skb) {
  1266. rc = -ENOMEM;
  1267. goto next_rx;
  1268. }
  1269. }
  1270. if (agg_bufs) {
  1271. skb = bnxt_rx_pages(bp, bnapi, skb, cp_cons, agg_bufs);
  1272. if (!skb) {
  1273. rc = -ENOMEM;
  1274. goto next_rx;
  1275. }
  1276. }
  1277. if (RX_CMP_HASH_VALID(rxcmp)) {
  1278. u32 hash_type = RX_CMP_HASH_TYPE(rxcmp);
  1279. enum pkt_hash_types type = PKT_HASH_TYPE_L4;
  1280. /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
  1281. if (hash_type != 1 && hash_type != 3)
  1282. type = PKT_HASH_TYPE_L3;
  1283. skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
  1284. }
  1285. skb->protocol = eth_type_trans(skb, dev);
  1286. if ((rxcmp1->rx_cmp_flags2 &
  1287. cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)) &&
  1288. (skb->dev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
  1289. u32 meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
  1290. u16 vtag = meta_data & RX_CMP_FLAGS2_METADATA_VID_MASK;
  1291. u16 vlan_proto = meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT;
  1292. __vlan_hwaccel_put_tag(skb, htons(vlan_proto), vtag);
  1293. }
  1294. skb_checksum_none_assert(skb);
  1295. if (RX_CMP_L4_CS_OK(rxcmp1)) {
  1296. if (dev->features & NETIF_F_RXCSUM) {
  1297. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1298. skb->csum_level = RX_CMP_ENCAP(rxcmp1);
  1299. }
  1300. } else {
  1301. if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
  1302. if (dev->features & NETIF_F_RXCSUM)
  1303. cpr->rx_l4_csum_errors++;
  1304. }
  1305. }
  1306. skb_record_rx_queue(skb, bnapi->index);
  1307. napi_gro_receive(&bnapi->napi, skb);
  1308. rc = 1;
  1309. next_rx:
  1310. rxr->rx_prod = NEXT_RX(prod);
  1311. rxr->rx_next_cons = NEXT_RX(cons);
  1312. next_rx_no_prod:
  1313. *raw_cons = tmp_raw_cons;
  1314. return rc;
  1315. }
  1316. #define BNXT_GET_EVENT_PORT(data) \
  1317. ((data) & \
  1318. ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
  1319. static int bnxt_async_event_process(struct bnxt *bp,
  1320. struct hwrm_async_event_cmpl *cmpl)
  1321. {
  1322. u16 event_id = le16_to_cpu(cmpl->event_id);
  1323. /* TODO CHIMP_FW: Define event id's for link change, error etc */
  1324. switch (event_id) {
  1325. case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
  1326. u32 data1 = le32_to_cpu(cmpl->event_data1);
  1327. struct bnxt_link_info *link_info = &bp->link_info;
  1328. if (BNXT_VF(bp))
  1329. goto async_event_process_exit;
  1330. if (data1 & 0x20000) {
  1331. u16 fw_speed = link_info->force_link_speed;
  1332. u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
  1333. netdev_warn(bp->dev, "Link speed %d no longer supported\n",
  1334. speed);
  1335. }
  1336. set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
  1337. /* fall thru */
  1338. }
  1339. case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
  1340. set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
  1341. break;
  1342. case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
  1343. set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
  1344. break;
  1345. case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
  1346. u32 data1 = le32_to_cpu(cmpl->event_data1);
  1347. u16 port_id = BNXT_GET_EVENT_PORT(data1);
  1348. if (BNXT_VF(bp))
  1349. break;
  1350. if (bp->pf.port_id != port_id)
  1351. break;
  1352. set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
  1353. break;
  1354. }
  1355. case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
  1356. if (BNXT_PF(bp))
  1357. goto async_event_process_exit;
  1358. set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
  1359. break;
  1360. default:
  1361. goto async_event_process_exit;
  1362. }
  1363. schedule_work(&bp->sp_task);
  1364. async_event_process_exit:
  1365. bnxt_ulp_async_events(bp, cmpl);
  1366. return 0;
  1367. }
  1368. static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
  1369. {
  1370. u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
  1371. struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
  1372. struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
  1373. (struct hwrm_fwd_req_cmpl *)txcmp;
  1374. switch (cmpl_type) {
  1375. case CMPL_BASE_TYPE_HWRM_DONE:
  1376. seq_id = le16_to_cpu(h_cmpl->sequence_id);
  1377. if (seq_id == bp->hwrm_intr_seq_id)
  1378. bp->hwrm_intr_seq_id = HWRM_SEQ_ID_INVALID;
  1379. else
  1380. netdev_err(bp->dev, "Invalid hwrm seq id %d\n", seq_id);
  1381. break;
  1382. case CMPL_BASE_TYPE_HWRM_FWD_REQ:
  1383. vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
  1384. if ((vf_id < bp->pf.first_vf_id) ||
  1385. (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
  1386. netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
  1387. vf_id);
  1388. return -EINVAL;
  1389. }
  1390. set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
  1391. set_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event);
  1392. schedule_work(&bp->sp_task);
  1393. break;
  1394. case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
  1395. bnxt_async_event_process(bp,
  1396. (struct hwrm_async_event_cmpl *)txcmp);
  1397. default:
  1398. break;
  1399. }
  1400. return 0;
  1401. }
  1402. static irqreturn_t bnxt_msix(int irq, void *dev_instance)
  1403. {
  1404. struct bnxt_napi *bnapi = dev_instance;
  1405. struct bnxt *bp = bnapi->bp;
  1406. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  1407. u32 cons = RING_CMP(cpr->cp_raw_cons);
  1408. prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
  1409. napi_schedule(&bnapi->napi);
  1410. return IRQ_HANDLED;
  1411. }
  1412. static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
  1413. {
  1414. u32 raw_cons = cpr->cp_raw_cons;
  1415. u16 cons = RING_CMP(raw_cons);
  1416. struct tx_cmp *txcmp;
  1417. txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
  1418. return TX_CMP_VALID(txcmp, raw_cons);
  1419. }
  1420. static irqreturn_t bnxt_inta(int irq, void *dev_instance)
  1421. {
  1422. struct bnxt_napi *bnapi = dev_instance;
  1423. struct bnxt *bp = bnapi->bp;
  1424. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  1425. u32 cons = RING_CMP(cpr->cp_raw_cons);
  1426. u32 int_status;
  1427. prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
  1428. if (!bnxt_has_work(bp, cpr)) {
  1429. int_status = readl(bp->bar0 + BNXT_CAG_REG_LEGACY_INT_STATUS);
  1430. /* return if erroneous interrupt */
  1431. if (!(int_status & (0x10000 << cpr->cp_ring_struct.fw_ring_id)))
  1432. return IRQ_NONE;
  1433. }
  1434. /* disable ring IRQ */
  1435. BNXT_CP_DB_IRQ_DIS(cpr->cp_doorbell);
  1436. /* Return here if interrupt is shared and is disabled. */
  1437. if (unlikely(atomic_read(&bp->intr_sem) != 0))
  1438. return IRQ_HANDLED;
  1439. napi_schedule(&bnapi->napi);
  1440. return IRQ_HANDLED;
  1441. }
  1442. static int bnxt_poll_work(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
  1443. {
  1444. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  1445. u32 raw_cons = cpr->cp_raw_cons;
  1446. u32 cons;
  1447. int tx_pkts = 0;
  1448. int rx_pkts = 0;
  1449. u8 event = 0;
  1450. struct tx_cmp *txcmp;
  1451. while (1) {
  1452. int rc;
  1453. cons = RING_CMP(raw_cons);
  1454. txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
  1455. if (!TX_CMP_VALID(txcmp, raw_cons))
  1456. break;
  1457. /* The valid test of the entry must be done first before
  1458. * reading any further.
  1459. */
  1460. dma_rmb();
  1461. if (TX_CMP_TYPE(txcmp) == CMP_TYPE_TX_L2_CMP) {
  1462. tx_pkts++;
  1463. /* return full budget so NAPI will complete. */
  1464. if (unlikely(tx_pkts > bp->tx_wake_thresh))
  1465. rx_pkts = budget;
  1466. } else if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
  1467. rc = bnxt_rx_pkt(bp, bnapi, &raw_cons, &event);
  1468. if (likely(rc >= 0))
  1469. rx_pkts += rc;
  1470. else if (rc == -EBUSY) /* partial completion */
  1471. break;
  1472. } else if (unlikely((TX_CMP_TYPE(txcmp) ==
  1473. CMPL_BASE_TYPE_HWRM_DONE) ||
  1474. (TX_CMP_TYPE(txcmp) ==
  1475. CMPL_BASE_TYPE_HWRM_FWD_REQ) ||
  1476. (TX_CMP_TYPE(txcmp) ==
  1477. CMPL_BASE_TYPE_HWRM_ASYNC_EVENT))) {
  1478. bnxt_hwrm_handler(bp, txcmp);
  1479. }
  1480. raw_cons = NEXT_RAW_CMP(raw_cons);
  1481. if (rx_pkts == budget)
  1482. break;
  1483. }
  1484. if (event & BNXT_TX_EVENT) {
  1485. struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
  1486. void __iomem *db = txr->tx_doorbell;
  1487. u16 prod = txr->tx_prod;
  1488. /* Sync BD data before updating doorbell */
  1489. wmb();
  1490. writel(DB_KEY_TX | prod, db);
  1491. writel(DB_KEY_TX | prod, db);
  1492. }
  1493. cpr->cp_raw_cons = raw_cons;
  1494. /* ACK completion ring before freeing tx ring and producing new
  1495. * buffers in rx/agg rings to prevent overflowing the completion
  1496. * ring.
  1497. */
  1498. BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
  1499. if (tx_pkts)
  1500. bnapi->tx_int(bp, bnapi, tx_pkts);
  1501. if (event & BNXT_RX_EVENT) {
  1502. struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
  1503. writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
  1504. writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
  1505. if (event & BNXT_AGG_EVENT) {
  1506. writel(DB_KEY_RX | rxr->rx_agg_prod,
  1507. rxr->rx_agg_doorbell);
  1508. writel(DB_KEY_RX | rxr->rx_agg_prod,
  1509. rxr->rx_agg_doorbell);
  1510. }
  1511. }
  1512. return rx_pkts;
  1513. }
  1514. static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
  1515. {
  1516. struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
  1517. struct bnxt *bp = bnapi->bp;
  1518. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  1519. struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
  1520. struct tx_cmp *txcmp;
  1521. struct rx_cmp_ext *rxcmp1;
  1522. u32 cp_cons, tmp_raw_cons;
  1523. u32 raw_cons = cpr->cp_raw_cons;
  1524. u32 rx_pkts = 0;
  1525. u8 event = 0;
  1526. while (1) {
  1527. int rc;
  1528. cp_cons = RING_CMP(raw_cons);
  1529. txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
  1530. if (!TX_CMP_VALID(txcmp, raw_cons))
  1531. break;
  1532. if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
  1533. tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
  1534. cp_cons = RING_CMP(tmp_raw_cons);
  1535. rxcmp1 = (struct rx_cmp_ext *)
  1536. &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
  1537. if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
  1538. break;
  1539. /* force an error to recycle the buffer */
  1540. rxcmp1->rx_cmp_cfa_code_errors_v2 |=
  1541. cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
  1542. rc = bnxt_rx_pkt(bp, bnapi, &raw_cons, &event);
  1543. if (likely(rc == -EIO))
  1544. rx_pkts++;
  1545. else if (rc == -EBUSY) /* partial completion */
  1546. break;
  1547. } else if (unlikely(TX_CMP_TYPE(txcmp) ==
  1548. CMPL_BASE_TYPE_HWRM_DONE)) {
  1549. bnxt_hwrm_handler(bp, txcmp);
  1550. } else {
  1551. netdev_err(bp->dev,
  1552. "Invalid completion received on special ring\n");
  1553. }
  1554. raw_cons = NEXT_RAW_CMP(raw_cons);
  1555. if (rx_pkts == budget)
  1556. break;
  1557. }
  1558. cpr->cp_raw_cons = raw_cons;
  1559. BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
  1560. writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
  1561. writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
  1562. if (event & BNXT_AGG_EVENT) {
  1563. writel(DB_KEY_RX | rxr->rx_agg_prod, rxr->rx_agg_doorbell);
  1564. writel(DB_KEY_RX | rxr->rx_agg_prod, rxr->rx_agg_doorbell);
  1565. }
  1566. if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
  1567. napi_complete_done(napi, rx_pkts);
  1568. BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
  1569. }
  1570. return rx_pkts;
  1571. }
  1572. static int bnxt_poll(struct napi_struct *napi, int budget)
  1573. {
  1574. struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
  1575. struct bnxt *bp = bnapi->bp;
  1576. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  1577. int work_done = 0;
  1578. while (1) {
  1579. work_done += bnxt_poll_work(bp, bnapi, budget - work_done);
  1580. if (work_done >= budget)
  1581. break;
  1582. if (!bnxt_has_work(bp, cpr)) {
  1583. if (napi_complete_done(napi, work_done))
  1584. BNXT_CP_DB_REARM(cpr->cp_doorbell,
  1585. cpr->cp_raw_cons);
  1586. break;
  1587. }
  1588. }
  1589. mmiowb();
  1590. return work_done;
  1591. }
  1592. static void bnxt_free_tx_skbs(struct bnxt *bp)
  1593. {
  1594. int i, max_idx;
  1595. struct pci_dev *pdev = bp->pdev;
  1596. if (!bp->tx_ring)
  1597. return;
  1598. max_idx = bp->tx_nr_pages * TX_DESC_CNT;
  1599. for (i = 0; i < bp->tx_nr_rings; i++) {
  1600. struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
  1601. int j;
  1602. for (j = 0; j < max_idx;) {
  1603. struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[j];
  1604. struct sk_buff *skb = tx_buf->skb;
  1605. int k, last;
  1606. if (!skb) {
  1607. j++;
  1608. continue;
  1609. }
  1610. tx_buf->skb = NULL;
  1611. if (tx_buf->is_push) {
  1612. dev_kfree_skb(skb);
  1613. j += 2;
  1614. continue;
  1615. }
  1616. dma_unmap_single(&pdev->dev,
  1617. dma_unmap_addr(tx_buf, mapping),
  1618. skb_headlen(skb),
  1619. PCI_DMA_TODEVICE);
  1620. last = tx_buf->nr_frags;
  1621. j += 2;
  1622. for (k = 0; k < last; k++, j++) {
  1623. int ring_idx = j & bp->tx_ring_mask;
  1624. skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
  1625. tx_buf = &txr->tx_buf_ring[ring_idx];
  1626. dma_unmap_page(
  1627. &pdev->dev,
  1628. dma_unmap_addr(tx_buf, mapping),
  1629. skb_frag_size(frag), PCI_DMA_TODEVICE);
  1630. }
  1631. dev_kfree_skb(skb);
  1632. }
  1633. netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, i));
  1634. }
  1635. }
  1636. static void bnxt_free_rx_skbs(struct bnxt *bp)
  1637. {
  1638. int i, max_idx, max_agg_idx;
  1639. struct pci_dev *pdev = bp->pdev;
  1640. if (!bp->rx_ring)
  1641. return;
  1642. max_idx = bp->rx_nr_pages * RX_DESC_CNT;
  1643. max_agg_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
  1644. for (i = 0; i < bp->rx_nr_rings; i++) {
  1645. struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
  1646. int j;
  1647. if (rxr->rx_tpa) {
  1648. for (j = 0; j < MAX_TPA; j++) {
  1649. struct bnxt_tpa_info *tpa_info =
  1650. &rxr->rx_tpa[j];
  1651. u8 *data = tpa_info->data;
  1652. if (!data)
  1653. continue;
  1654. dma_unmap_single(&pdev->dev, tpa_info->mapping,
  1655. bp->rx_buf_use_size,
  1656. bp->rx_dir);
  1657. tpa_info->data = NULL;
  1658. kfree(data);
  1659. }
  1660. }
  1661. for (j = 0; j < max_idx; j++) {
  1662. struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[j];
  1663. void *data = rx_buf->data;
  1664. if (!data)
  1665. continue;
  1666. dma_unmap_single(&pdev->dev, rx_buf->mapping,
  1667. bp->rx_buf_use_size, bp->rx_dir);
  1668. rx_buf->data = NULL;
  1669. if (BNXT_RX_PAGE_MODE(bp))
  1670. __free_page(data);
  1671. else
  1672. kfree(data);
  1673. }
  1674. for (j = 0; j < max_agg_idx; j++) {
  1675. struct bnxt_sw_rx_agg_bd *rx_agg_buf =
  1676. &rxr->rx_agg_ring[j];
  1677. struct page *page = rx_agg_buf->page;
  1678. if (!page)
  1679. continue;
  1680. dma_unmap_page(&pdev->dev, rx_agg_buf->mapping,
  1681. BNXT_RX_PAGE_SIZE, PCI_DMA_FROMDEVICE);
  1682. rx_agg_buf->page = NULL;
  1683. __clear_bit(j, rxr->rx_agg_bmap);
  1684. __free_page(page);
  1685. }
  1686. if (rxr->rx_page) {
  1687. __free_page(rxr->rx_page);
  1688. rxr->rx_page = NULL;
  1689. }
  1690. }
  1691. }
  1692. static void bnxt_free_skbs(struct bnxt *bp)
  1693. {
  1694. bnxt_free_tx_skbs(bp);
  1695. bnxt_free_rx_skbs(bp);
  1696. }
  1697. static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_struct *ring)
  1698. {
  1699. struct pci_dev *pdev = bp->pdev;
  1700. int i;
  1701. for (i = 0; i < ring->nr_pages; i++) {
  1702. if (!ring->pg_arr[i])
  1703. continue;
  1704. dma_free_coherent(&pdev->dev, ring->page_size,
  1705. ring->pg_arr[i], ring->dma_arr[i]);
  1706. ring->pg_arr[i] = NULL;
  1707. }
  1708. if (ring->pg_tbl) {
  1709. dma_free_coherent(&pdev->dev, ring->nr_pages * 8,
  1710. ring->pg_tbl, ring->pg_tbl_map);
  1711. ring->pg_tbl = NULL;
  1712. }
  1713. if (ring->vmem_size && *ring->vmem) {
  1714. vfree(*ring->vmem);
  1715. *ring->vmem = NULL;
  1716. }
  1717. }
  1718. static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_struct *ring)
  1719. {
  1720. int i;
  1721. struct pci_dev *pdev = bp->pdev;
  1722. if (ring->nr_pages > 1) {
  1723. ring->pg_tbl = dma_alloc_coherent(&pdev->dev,
  1724. ring->nr_pages * 8,
  1725. &ring->pg_tbl_map,
  1726. GFP_KERNEL);
  1727. if (!ring->pg_tbl)
  1728. return -ENOMEM;
  1729. }
  1730. for (i = 0; i < ring->nr_pages; i++) {
  1731. ring->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
  1732. ring->page_size,
  1733. &ring->dma_arr[i],
  1734. GFP_KERNEL);
  1735. if (!ring->pg_arr[i])
  1736. return -ENOMEM;
  1737. if (ring->nr_pages > 1)
  1738. ring->pg_tbl[i] = cpu_to_le64(ring->dma_arr[i]);
  1739. }
  1740. if (ring->vmem_size) {
  1741. *ring->vmem = vzalloc(ring->vmem_size);
  1742. if (!(*ring->vmem))
  1743. return -ENOMEM;
  1744. }
  1745. return 0;
  1746. }
  1747. static void bnxt_free_rx_rings(struct bnxt *bp)
  1748. {
  1749. int i;
  1750. if (!bp->rx_ring)
  1751. return;
  1752. for (i = 0; i < bp->rx_nr_rings; i++) {
  1753. struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
  1754. struct bnxt_ring_struct *ring;
  1755. if (rxr->xdp_prog)
  1756. bpf_prog_put(rxr->xdp_prog);
  1757. kfree(rxr->rx_tpa);
  1758. rxr->rx_tpa = NULL;
  1759. kfree(rxr->rx_agg_bmap);
  1760. rxr->rx_agg_bmap = NULL;
  1761. ring = &rxr->rx_ring_struct;
  1762. bnxt_free_ring(bp, ring);
  1763. ring = &rxr->rx_agg_ring_struct;
  1764. bnxt_free_ring(bp, ring);
  1765. }
  1766. }
  1767. static int bnxt_alloc_rx_rings(struct bnxt *bp)
  1768. {
  1769. int i, rc, agg_rings = 0, tpa_rings = 0;
  1770. if (!bp->rx_ring)
  1771. return -ENOMEM;
  1772. if (bp->flags & BNXT_FLAG_AGG_RINGS)
  1773. agg_rings = 1;
  1774. if (bp->flags & BNXT_FLAG_TPA)
  1775. tpa_rings = 1;
  1776. for (i = 0; i < bp->rx_nr_rings; i++) {
  1777. struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
  1778. struct bnxt_ring_struct *ring;
  1779. ring = &rxr->rx_ring_struct;
  1780. rc = bnxt_alloc_ring(bp, ring);
  1781. if (rc)
  1782. return rc;
  1783. if (agg_rings) {
  1784. u16 mem_size;
  1785. ring = &rxr->rx_agg_ring_struct;
  1786. rc = bnxt_alloc_ring(bp, ring);
  1787. if (rc)
  1788. return rc;
  1789. rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
  1790. mem_size = rxr->rx_agg_bmap_size / 8;
  1791. rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
  1792. if (!rxr->rx_agg_bmap)
  1793. return -ENOMEM;
  1794. if (tpa_rings) {
  1795. rxr->rx_tpa = kcalloc(MAX_TPA,
  1796. sizeof(struct bnxt_tpa_info),
  1797. GFP_KERNEL);
  1798. if (!rxr->rx_tpa)
  1799. return -ENOMEM;
  1800. }
  1801. }
  1802. }
  1803. return 0;
  1804. }
  1805. static void bnxt_free_tx_rings(struct bnxt *bp)
  1806. {
  1807. int i;
  1808. struct pci_dev *pdev = bp->pdev;
  1809. if (!bp->tx_ring)
  1810. return;
  1811. for (i = 0; i < bp->tx_nr_rings; i++) {
  1812. struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
  1813. struct bnxt_ring_struct *ring;
  1814. if (txr->tx_push) {
  1815. dma_free_coherent(&pdev->dev, bp->tx_push_size,
  1816. txr->tx_push, txr->tx_push_mapping);
  1817. txr->tx_push = NULL;
  1818. }
  1819. ring = &txr->tx_ring_struct;
  1820. bnxt_free_ring(bp, ring);
  1821. }
  1822. }
  1823. static int bnxt_alloc_tx_rings(struct bnxt *bp)
  1824. {
  1825. int i, j, rc;
  1826. struct pci_dev *pdev = bp->pdev;
  1827. bp->tx_push_size = 0;
  1828. if (bp->tx_push_thresh) {
  1829. int push_size;
  1830. push_size = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
  1831. bp->tx_push_thresh);
  1832. if (push_size > 256) {
  1833. push_size = 0;
  1834. bp->tx_push_thresh = 0;
  1835. }
  1836. bp->tx_push_size = push_size;
  1837. }
  1838. for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
  1839. struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
  1840. struct bnxt_ring_struct *ring;
  1841. ring = &txr->tx_ring_struct;
  1842. rc = bnxt_alloc_ring(bp, ring);
  1843. if (rc)
  1844. return rc;
  1845. if (bp->tx_push_size) {
  1846. dma_addr_t mapping;
  1847. /* One pre-allocated DMA buffer to backup
  1848. * TX push operation
  1849. */
  1850. txr->tx_push = dma_alloc_coherent(&pdev->dev,
  1851. bp->tx_push_size,
  1852. &txr->tx_push_mapping,
  1853. GFP_KERNEL);
  1854. if (!txr->tx_push)
  1855. return -ENOMEM;
  1856. mapping = txr->tx_push_mapping +
  1857. sizeof(struct tx_push_bd);
  1858. txr->data_mapping = cpu_to_le64(mapping);
  1859. memset(txr->tx_push, 0, sizeof(struct tx_push_bd));
  1860. }
  1861. ring->queue_id = bp->q_info[j].queue_id;
  1862. if (i < bp->tx_nr_rings_xdp)
  1863. continue;
  1864. if (i % bp->tx_nr_rings_per_tc == (bp->tx_nr_rings_per_tc - 1))
  1865. j++;
  1866. }
  1867. return 0;
  1868. }
  1869. static void bnxt_free_cp_rings(struct bnxt *bp)
  1870. {
  1871. int i;
  1872. if (!bp->bnapi)
  1873. return;
  1874. for (i = 0; i < bp->cp_nr_rings; i++) {
  1875. struct bnxt_napi *bnapi = bp->bnapi[i];
  1876. struct bnxt_cp_ring_info *cpr;
  1877. struct bnxt_ring_struct *ring;
  1878. if (!bnapi)
  1879. continue;
  1880. cpr = &bnapi->cp_ring;
  1881. ring = &cpr->cp_ring_struct;
  1882. bnxt_free_ring(bp, ring);
  1883. }
  1884. }
  1885. static int bnxt_alloc_cp_rings(struct bnxt *bp)
  1886. {
  1887. int i, rc;
  1888. for (i = 0; i < bp->cp_nr_rings; i++) {
  1889. struct bnxt_napi *bnapi = bp->bnapi[i];
  1890. struct bnxt_cp_ring_info *cpr;
  1891. struct bnxt_ring_struct *ring;
  1892. if (!bnapi)
  1893. continue;
  1894. cpr = &bnapi->cp_ring;
  1895. ring = &cpr->cp_ring_struct;
  1896. rc = bnxt_alloc_ring(bp, ring);
  1897. if (rc)
  1898. return rc;
  1899. }
  1900. return 0;
  1901. }
  1902. static void bnxt_init_ring_struct(struct bnxt *bp)
  1903. {
  1904. int i;
  1905. for (i = 0; i < bp->cp_nr_rings; i++) {
  1906. struct bnxt_napi *bnapi = bp->bnapi[i];
  1907. struct bnxt_cp_ring_info *cpr;
  1908. struct bnxt_rx_ring_info *rxr;
  1909. struct bnxt_tx_ring_info *txr;
  1910. struct bnxt_ring_struct *ring;
  1911. if (!bnapi)
  1912. continue;
  1913. cpr = &bnapi->cp_ring;
  1914. ring = &cpr->cp_ring_struct;
  1915. ring->nr_pages = bp->cp_nr_pages;
  1916. ring->page_size = HW_CMPD_RING_SIZE;
  1917. ring->pg_arr = (void **)cpr->cp_desc_ring;
  1918. ring->dma_arr = cpr->cp_desc_mapping;
  1919. ring->vmem_size = 0;
  1920. rxr = bnapi->rx_ring;
  1921. if (!rxr)
  1922. goto skip_rx;
  1923. ring = &rxr->rx_ring_struct;
  1924. ring->nr_pages = bp->rx_nr_pages;
  1925. ring->page_size = HW_RXBD_RING_SIZE;
  1926. ring->pg_arr = (void **)rxr->rx_desc_ring;
  1927. ring->dma_arr = rxr->rx_desc_mapping;
  1928. ring->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
  1929. ring->vmem = (void **)&rxr->rx_buf_ring;
  1930. ring = &rxr->rx_agg_ring_struct;
  1931. ring->nr_pages = bp->rx_agg_nr_pages;
  1932. ring->page_size = HW_RXBD_RING_SIZE;
  1933. ring->pg_arr = (void **)rxr->rx_agg_desc_ring;
  1934. ring->dma_arr = rxr->rx_agg_desc_mapping;
  1935. ring->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
  1936. ring->vmem = (void **)&rxr->rx_agg_ring;
  1937. skip_rx:
  1938. txr = bnapi->tx_ring;
  1939. if (!txr)
  1940. continue;
  1941. ring = &txr->tx_ring_struct;
  1942. ring->nr_pages = bp->tx_nr_pages;
  1943. ring->page_size = HW_RXBD_RING_SIZE;
  1944. ring->pg_arr = (void **)txr->tx_desc_ring;
  1945. ring->dma_arr = txr->tx_desc_mapping;
  1946. ring->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
  1947. ring->vmem = (void **)&txr->tx_buf_ring;
  1948. }
  1949. }
  1950. static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
  1951. {
  1952. int i;
  1953. u32 prod;
  1954. struct rx_bd **rx_buf_ring;
  1955. rx_buf_ring = (struct rx_bd **)ring->pg_arr;
  1956. for (i = 0, prod = 0; i < ring->nr_pages; i++) {
  1957. int j;
  1958. struct rx_bd *rxbd;
  1959. rxbd = rx_buf_ring[i];
  1960. if (!rxbd)
  1961. continue;
  1962. for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
  1963. rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
  1964. rxbd->rx_bd_opaque = prod;
  1965. }
  1966. }
  1967. }
  1968. static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
  1969. {
  1970. struct net_device *dev = bp->dev;
  1971. struct bnxt_rx_ring_info *rxr;
  1972. struct bnxt_ring_struct *ring;
  1973. u32 prod, type;
  1974. int i;
  1975. type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
  1976. RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
  1977. if (NET_IP_ALIGN == 2)
  1978. type |= RX_BD_FLAGS_SOP;
  1979. rxr = &bp->rx_ring[ring_nr];
  1980. ring = &rxr->rx_ring_struct;
  1981. bnxt_init_rxbd_pages(ring, type);
  1982. if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
  1983. rxr->xdp_prog = bpf_prog_add(bp->xdp_prog, 1);
  1984. if (IS_ERR(rxr->xdp_prog)) {
  1985. int rc = PTR_ERR(rxr->xdp_prog);
  1986. rxr->xdp_prog = NULL;
  1987. return rc;
  1988. }
  1989. }
  1990. prod = rxr->rx_prod;
  1991. for (i = 0; i < bp->rx_ring_size; i++) {
  1992. if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL) != 0) {
  1993. netdev_warn(dev, "init'ed rx ring %d with %d/%d skbs only\n",
  1994. ring_nr, i, bp->rx_ring_size);
  1995. break;
  1996. }
  1997. prod = NEXT_RX(prod);
  1998. }
  1999. rxr->rx_prod = prod;
  2000. ring->fw_ring_id = INVALID_HW_RING_ID;
  2001. ring = &rxr->rx_agg_ring_struct;
  2002. ring->fw_ring_id = INVALID_HW_RING_ID;
  2003. if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
  2004. return 0;
  2005. type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
  2006. RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
  2007. bnxt_init_rxbd_pages(ring, type);
  2008. prod = rxr->rx_agg_prod;
  2009. for (i = 0; i < bp->rx_agg_ring_size; i++) {
  2010. if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL) != 0) {
  2011. netdev_warn(dev, "init'ed rx ring %d with %d/%d pages only\n",
  2012. ring_nr, i, bp->rx_ring_size);
  2013. break;
  2014. }
  2015. prod = NEXT_RX_AGG(prod);
  2016. }
  2017. rxr->rx_agg_prod = prod;
  2018. if (bp->flags & BNXT_FLAG_TPA) {
  2019. if (rxr->rx_tpa) {
  2020. u8 *data;
  2021. dma_addr_t mapping;
  2022. for (i = 0; i < MAX_TPA; i++) {
  2023. data = __bnxt_alloc_rx_data(bp, &mapping,
  2024. GFP_KERNEL);
  2025. if (!data)
  2026. return -ENOMEM;
  2027. rxr->rx_tpa[i].data = data;
  2028. rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
  2029. rxr->rx_tpa[i].mapping = mapping;
  2030. }
  2031. } else {
  2032. netdev_err(bp->dev, "No resource allocated for LRO/GRO\n");
  2033. return -ENOMEM;
  2034. }
  2035. }
  2036. return 0;
  2037. }
  2038. static int bnxt_init_rx_rings(struct bnxt *bp)
  2039. {
  2040. int i, rc = 0;
  2041. if (BNXT_RX_PAGE_MODE(bp)) {
  2042. bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
  2043. bp->rx_dma_offset = XDP_PACKET_HEADROOM;
  2044. } else {
  2045. bp->rx_offset = BNXT_RX_OFFSET;
  2046. bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
  2047. }
  2048. for (i = 0; i < bp->rx_nr_rings; i++) {
  2049. rc = bnxt_init_one_rx_ring(bp, i);
  2050. if (rc)
  2051. break;
  2052. }
  2053. return rc;
  2054. }
  2055. static int bnxt_init_tx_rings(struct bnxt *bp)
  2056. {
  2057. u16 i;
  2058. bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
  2059. MAX_SKB_FRAGS + 1);
  2060. for (i = 0; i < bp->tx_nr_rings; i++) {
  2061. struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
  2062. struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
  2063. ring->fw_ring_id = INVALID_HW_RING_ID;
  2064. }
  2065. return 0;
  2066. }
  2067. static void bnxt_free_ring_grps(struct bnxt *bp)
  2068. {
  2069. kfree(bp->grp_info);
  2070. bp->grp_info = NULL;
  2071. }
  2072. static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
  2073. {
  2074. int i;
  2075. if (irq_re_init) {
  2076. bp->grp_info = kcalloc(bp->cp_nr_rings,
  2077. sizeof(struct bnxt_ring_grp_info),
  2078. GFP_KERNEL);
  2079. if (!bp->grp_info)
  2080. return -ENOMEM;
  2081. }
  2082. for (i = 0; i < bp->cp_nr_rings; i++) {
  2083. if (irq_re_init)
  2084. bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
  2085. bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
  2086. bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
  2087. bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
  2088. bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
  2089. }
  2090. return 0;
  2091. }
  2092. static void bnxt_free_vnics(struct bnxt *bp)
  2093. {
  2094. kfree(bp->vnic_info);
  2095. bp->vnic_info = NULL;
  2096. bp->nr_vnics = 0;
  2097. }
  2098. static int bnxt_alloc_vnics(struct bnxt *bp)
  2099. {
  2100. int num_vnics = 1;
  2101. #ifdef CONFIG_RFS_ACCEL
  2102. if (bp->flags & BNXT_FLAG_RFS)
  2103. num_vnics += bp->rx_nr_rings;
  2104. #endif
  2105. if (BNXT_CHIP_TYPE_NITRO_A0(bp))
  2106. num_vnics++;
  2107. bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
  2108. GFP_KERNEL);
  2109. if (!bp->vnic_info)
  2110. return -ENOMEM;
  2111. bp->nr_vnics = num_vnics;
  2112. return 0;
  2113. }
  2114. static void bnxt_init_vnics(struct bnxt *bp)
  2115. {
  2116. int i;
  2117. for (i = 0; i < bp->nr_vnics; i++) {
  2118. struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
  2119. vnic->fw_vnic_id = INVALID_HW_RING_ID;
  2120. vnic->fw_rss_cos_lb_ctx[0] = INVALID_HW_RING_ID;
  2121. vnic->fw_rss_cos_lb_ctx[1] = INVALID_HW_RING_ID;
  2122. vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
  2123. if (bp->vnic_info[i].rss_hash_key) {
  2124. if (i == 0)
  2125. prandom_bytes(vnic->rss_hash_key,
  2126. HW_HASH_KEY_SIZE);
  2127. else
  2128. memcpy(vnic->rss_hash_key,
  2129. bp->vnic_info[0].rss_hash_key,
  2130. HW_HASH_KEY_SIZE);
  2131. }
  2132. }
  2133. }
  2134. static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
  2135. {
  2136. int pages;
  2137. pages = ring_size / desc_per_pg;
  2138. if (!pages)
  2139. return 1;
  2140. pages++;
  2141. while (pages & (pages - 1))
  2142. pages++;
  2143. return pages;
  2144. }
  2145. void bnxt_set_tpa_flags(struct bnxt *bp)
  2146. {
  2147. bp->flags &= ~BNXT_FLAG_TPA;
  2148. if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
  2149. return;
  2150. if (bp->dev->features & NETIF_F_LRO)
  2151. bp->flags |= BNXT_FLAG_LRO;
  2152. if (bp->dev->features & NETIF_F_GRO)
  2153. bp->flags |= BNXT_FLAG_GRO;
  2154. }
  2155. /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
  2156. * be set on entry.
  2157. */
  2158. void bnxt_set_ring_params(struct bnxt *bp)
  2159. {
  2160. u32 ring_size, rx_size, rx_space;
  2161. u32 agg_factor = 0, agg_ring_size = 0;
  2162. /* 8 for CRC and VLAN */
  2163. rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
  2164. rx_space = rx_size + NET_SKB_PAD +
  2165. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  2166. bp->rx_copy_thresh = BNXT_RX_COPY_THRESH;
  2167. ring_size = bp->rx_ring_size;
  2168. bp->rx_agg_ring_size = 0;
  2169. bp->rx_agg_nr_pages = 0;
  2170. if (bp->flags & BNXT_FLAG_TPA)
  2171. agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
  2172. bp->flags &= ~BNXT_FLAG_JUMBO;
  2173. if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
  2174. u32 jumbo_factor;
  2175. bp->flags |= BNXT_FLAG_JUMBO;
  2176. jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
  2177. if (jumbo_factor > agg_factor)
  2178. agg_factor = jumbo_factor;
  2179. }
  2180. agg_ring_size = ring_size * agg_factor;
  2181. if (agg_ring_size) {
  2182. bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
  2183. RX_DESC_CNT);
  2184. if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
  2185. u32 tmp = agg_ring_size;
  2186. bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
  2187. agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
  2188. netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
  2189. tmp, agg_ring_size);
  2190. }
  2191. bp->rx_agg_ring_size = agg_ring_size;
  2192. bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
  2193. rx_size = SKB_DATA_ALIGN(BNXT_RX_COPY_THRESH + NET_IP_ALIGN);
  2194. rx_space = rx_size + NET_SKB_PAD +
  2195. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  2196. }
  2197. bp->rx_buf_use_size = rx_size;
  2198. bp->rx_buf_size = rx_space;
  2199. bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
  2200. bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
  2201. ring_size = bp->tx_ring_size;
  2202. bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
  2203. bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
  2204. ring_size = bp->rx_ring_size * (2 + agg_factor) + bp->tx_ring_size;
  2205. bp->cp_ring_size = ring_size;
  2206. bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
  2207. if (bp->cp_nr_pages > MAX_CP_PAGES) {
  2208. bp->cp_nr_pages = MAX_CP_PAGES;
  2209. bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
  2210. netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
  2211. ring_size, bp->cp_ring_size);
  2212. }
  2213. bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
  2214. bp->cp_ring_mask = bp->cp_bit - 1;
  2215. }
  2216. int bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
  2217. {
  2218. if (page_mode) {
  2219. if (bp->dev->mtu > BNXT_MAX_PAGE_MODE_MTU)
  2220. return -EOPNOTSUPP;
  2221. bp->dev->max_mtu = BNXT_MAX_PAGE_MODE_MTU;
  2222. bp->flags &= ~BNXT_FLAG_AGG_RINGS;
  2223. bp->flags |= BNXT_FLAG_NO_AGG_RINGS | BNXT_FLAG_RX_PAGE_MODE;
  2224. bp->dev->hw_features &= ~NETIF_F_LRO;
  2225. bp->dev->features &= ~NETIF_F_LRO;
  2226. bp->rx_dir = DMA_BIDIRECTIONAL;
  2227. bp->rx_skb_func = bnxt_rx_page_skb;
  2228. } else {
  2229. bp->dev->max_mtu = BNXT_MAX_MTU;
  2230. bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
  2231. bp->rx_dir = DMA_FROM_DEVICE;
  2232. bp->rx_skb_func = bnxt_rx_skb;
  2233. }
  2234. return 0;
  2235. }
  2236. static void bnxt_free_vnic_attributes(struct bnxt *bp)
  2237. {
  2238. int i;
  2239. struct bnxt_vnic_info *vnic;
  2240. struct pci_dev *pdev = bp->pdev;
  2241. if (!bp->vnic_info)
  2242. return;
  2243. for (i = 0; i < bp->nr_vnics; i++) {
  2244. vnic = &bp->vnic_info[i];
  2245. kfree(vnic->fw_grp_ids);
  2246. vnic->fw_grp_ids = NULL;
  2247. kfree(vnic->uc_list);
  2248. vnic->uc_list = NULL;
  2249. if (vnic->mc_list) {
  2250. dma_free_coherent(&pdev->dev, vnic->mc_list_size,
  2251. vnic->mc_list, vnic->mc_list_mapping);
  2252. vnic->mc_list = NULL;
  2253. }
  2254. if (vnic->rss_table) {
  2255. dma_free_coherent(&pdev->dev, PAGE_SIZE,
  2256. vnic->rss_table,
  2257. vnic->rss_table_dma_addr);
  2258. vnic->rss_table = NULL;
  2259. }
  2260. vnic->rss_hash_key = NULL;
  2261. vnic->flags = 0;
  2262. }
  2263. }
  2264. static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
  2265. {
  2266. int i, rc = 0, size;
  2267. struct bnxt_vnic_info *vnic;
  2268. struct pci_dev *pdev = bp->pdev;
  2269. int max_rings;
  2270. for (i = 0; i < bp->nr_vnics; i++) {
  2271. vnic = &bp->vnic_info[i];
  2272. if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
  2273. int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
  2274. if (mem_size > 0) {
  2275. vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
  2276. if (!vnic->uc_list) {
  2277. rc = -ENOMEM;
  2278. goto out;
  2279. }
  2280. }
  2281. }
  2282. if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
  2283. vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
  2284. vnic->mc_list =
  2285. dma_alloc_coherent(&pdev->dev,
  2286. vnic->mc_list_size,
  2287. &vnic->mc_list_mapping,
  2288. GFP_KERNEL);
  2289. if (!vnic->mc_list) {
  2290. rc = -ENOMEM;
  2291. goto out;
  2292. }
  2293. }
  2294. if (vnic->flags & BNXT_VNIC_RSS_FLAG)
  2295. max_rings = bp->rx_nr_rings;
  2296. else
  2297. max_rings = 1;
  2298. vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
  2299. if (!vnic->fw_grp_ids) {
  2300. rc = -ENOMEM;
  2301. goto out;
  2302. }
  2303. if ((bp->flags & BNXT_FLAG_NEW_RSS_CAP) &&
  2304. !(vnic->flags & BNXT_VNIC_RSS_FLAG))
  2305. continue;
  2306. /* Allocate rss table and hash key */
  2307. vnic->rss_table = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
  2308. &vnic->rss_table_dma_addr,
  2309. GFP_KERNEL);
  2310. if (!vnic->rss_table) {
  2311. rc = -ENOMEM;
  2312. goto out;
  2313. }
  2314. size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
  2315. vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
  2316. vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
  2317. }
  2318. return 0;
  2319. out:
  2320. return rc;
  2321. }
  2322. static void bnxt_free_hwrm_resources(struct bnxt *bp)
  2323. {
  2324. struct pci_dev *pdev = bp->pdev;
  2325. dma_free_coherent(&pdev->dev, PAGE_SIZE, bp->hwrm_cmd_resp_addr,
  2326. bp->hwrm_cmd_resp_dma_addr);
  2327. bp->hwrm_cmd_resp_addr = NULL;
  2328. if (bp->hwrm_dbg_resp_addr) {
  2329. dma_free_coherent(&pdev->dev, HWRM_DBG_REG_BUF_SIZE,
  2330. bp->hwrm_dbg_resp_addr,
  2331. bp->hwrm_dbg_resp_dma_addr);
  2332. bp->hwrm_dbg_resp_addr = NULL;
  2333. }
  2334. }
  2335. static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
  2336. {
  2337. struct pci_dev *pdev = bp->pdev;
  2338. bp->hwrm_cmd_resp_addr = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
  2339. &bp->hwrm_cmd_resp_dma_addr,
  2340. GFP_KERNEL);
  2341. if (!bp->hwrm_cmd_resp_addr)
  2342. return -ENOMEM;
  2343. bp->hwrm_dbg_resp_addr = dma_alloc_coherent(&pdev->dev,
  2344. HWRM_DBG_REG_BUF_SIZE,
  2345. &bp->hwrm_dbg_resp_dma_addr,
  2346. GFP_KERNEL);
  2347. if (!bp->hwrm_dbg_resp_addr)
  2348. netdev_warn(bp->dev, "fail to alloc debug register dma mem\n");
  2349. return 0;
  2350. }
  2351. static void bnxt_free_stats(struct bnxt *bp)
  2352. {
  2353. u32 size, i;
  2354. struct pci_dev *pdev = bp->pdev;
  2355. if (bp->hw_rx_port_stats) {
  2356. dma_free_coherent(&pdev->dev, bp->hw_port_stats_size,
  2357. bp->hw_rx_port_stats,
  2358. bp->hw_rx_port_stats_map);
  2359. bp->hw_rx_port_stats = NULL;
  2360. bp->flags &= ~BNXT_FLAG_PORT_STATS;
  2361. }
  2362. if (!bp->bnapi)
  2363. return;
  2364. size = sizeof(struct ctx_hw_stats);
  2365. for (i = 0; i < bp->cp_nr_rings; i++) {
  2366. struct bnxt_napi *bnapi = bp->bnapi[i];
  2367. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  2368. if (cpr->hw_stats) {
  2369. dma_free_coherent(&pdev->dev, size, cpr->hw_stats,
  2370. cpr->hw_stats_map);
  2371. cpr->hw_stats = NULL;
  2372. }
  2373. }
  2374. }
  2375. static int bnxt_alloc_stats(struct bnxt *bp)
  2376. {
  2377. u32 size, i;
  2378. struct pci_dev *pdev = bp->pdev;
  2379. size = sizeof(struct ctx_hw_stats);
  2380. for (i = 0; i < bp->cp_nr_rings; i++) {
  2381. struct bnxt_napi *bnapi = bp->bnapi[i];
  2382. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  2383. cpr->hw_stats = dma_alloc_coherent(&pdev->dev, size,
  2384. &cpr->hw_stats_map,
  2385. GFP_KERNEL);
  2386. if (!cpr->hw_stats)
  2387. return -ENOMEM;
  2388. cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
  2389. }
  2390. if (BNXT_PF(bp) && bp->chip_num != CHIP_NUM_58700) {
  2391. bp->hw_port_stats_size = sizeof(struct rx_port_stats) +
  2392. sizeof(struct tx_port_stats) + 1024;
  2393. bp->hw_rx_port_stats =
  2394. dma_alloc_coherent(&pdev->dev, bp->hw_port_stats_size,
  2395. &bp->hw_rx_port_stats_map,
  2396. GFP_KERNEL);
  2397. if (!bp->hw_rx_port_stats)
  2398. return -ENOMEM;
  2399. bp->hw_tx_port_stats = (void *)(bp->hw_rx_port_stats + 1) +
  2400. 512;
  2401. bp->hw_tx_port_stats_map = bp->hw_rx_port_stats_map +
  2402. sizeof(struct rx_port_stats) + 512;
  2403. bp->flags |= BNXT_FLAG_PORT_STATS;
  2404. }
  2405. return 0;
  2406. }
  2407. static void bnxt_clear_ring_indices(struct bnxt *bp)
  2408. {
  2409. int i;
  2410. if (!bp->bnapi)
  2411. return;
  2412. for (i = 0; i < bp->cp_nr_rings; i++) {
  2413. struct bnxt_napi *bnapi = bp->bnapi[i];
  2414. struct bnxt_cp_ring_info *cpr;
  2415. struct bnxt_rx_ring_info *rxr;
  2416. struct bnxt_tx_ring_info *txr;
  2417. if (!bnapi)
  2418. continue;
  2419. cpr = &bnapi->cp_ring;
  2420. cpr->cp_raw_cons = 0;
  2421. txr = bnapi->tx_ring;
  2422. if (txr) {
  2423. txr->tx_prod = 0;
  2424. txr->tx_cons = 0;
  2425. }
  2426. rxr = bnapi->rx_ring;
  2427. if (rxr) {
  2428. rxr->rx_prod = 0;
  2429. rxr->rx_agg_prod = 0;
  2430. rxr->rx_sw_agg_prod = 0;
  2431. rxr->rx_next_cons = 0;
  2432. }
  2433. }
  2434. }
  2435. static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool irq_reinit)
  2436. {
  2437. #ifdef CONFIG_RFS_ACCEL
  2438. int i;
  2439. /* Under rtnl_lock and all our NAPIs have been disabled. It's
  2440. * safe to delete the hash table.
  2441. */
  2442. for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
  2443. struct hlist_head *head;
  2444. struct hlist_node *tmp;
  2445. struct bnxt_ntuple_filter *fltr;
  2446. head = &bp->ntp_fltr_hash_tbl[i];
  2447. hlist_for_each_entry_safe(fltr, tmp, head, hash) {
  2448. hlist_del(&fltr->hash);
  2449. kfree(fltr);
  2450. }
  2451. }
  2452. if (irq_reinit) {
  2453. kfree(bp->ntp_fltr_bmap);
  2454. bp->ntp_fltr_bmap = NULL;
  2455. }
  2456. bp->ntp_fltr_count = 0;
  2457. #endif
  2458. }
  2459. static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
  2460. {
  2461. #ifdef CONFIG_RFS_ACCEL
  2462. int i, rc = 0;
  2463. if (!(bp->flags & BNXT_FLAG_RFS))
  2464. return 0;
  2465. for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
  2466. INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
  2467. bp->ntp_fltr_count = 0;
  2468. bp->ntp_fltr_bmap = kzalloc(BITS_TO_LONGS(BNXT_NTP_FLTR_MAX_FLTR),
  2469. GFP_KERNEL);
  2470. if (!bp->ntp_fltr_bmap)
  2471. rc = -ENOMEM;
  2472. return rc;
  2473. #else
  2474. return 0;
  2475. #endif
  2476. }
  2477. static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
  2478. {
  2479. bnxt_free_vnic_attributes(bp);
  2480. bnxt_free_tx_rings(bp);
  2481. bnxt_free_rx_rings(bp);
  2482. bnxt_free_cp_rings(bp);
  2483. bnxt_free_ntp_fltrs(bp, irq_re_init);
  2484. if (irq_re_init) {
  2485. bnxt_free_stats(bp);
  2486. bnxt_free_ring_grps(bp);
  2487. bnxt_free_vnics(bp);
  2488. kfree(bp->tx_ring_map);
  2489. bp->tx_ring_map = NULL;
  2490. kfree(bp->tx_ring);
  2491. bp->tx_ring = NULL;
  2492. kfree(bp->rx_ring);
  2493. bp->rx_ring = NULL;
  2494. kfree(bp->bnapi);
  2495. bp->bnapi = NULL;
  2496. } else {
  2497. bnxt_clear_ring_indices(bp);
  2498. }
  2499. }
  2500. static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
  2501. {
  2502. int i, j, rc, size, arr_size;
  2503. void *bnapi;
  2504. if (irq_re_init) {
  2505. /* Allocate bnapi mem pointer array and mem block for
  2506. * all queues
  2507. */
  2508. arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
  2509. bp->cp_nr_rings);
  2510. size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
  2511. bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
  2512. if (!bnapi)
  2513. return -ENOMEM;
  2514. bp->bnapi = bnapi;
  2515. bnapi += arr_size;
  2516. for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
  2517. bp->bnapi[i] = bnapi;
  2518. bp->bnapi[i]->index = i;
  2519. bp->bnapi[i]->bp = bp;
  2520. }
  2521. bp->rx_ring = kcalloc(bp->rx_nr_rings,
  2522. sizeof(struct bnxt_rx_ring_info),
  2523. GFP_KERNEL);
  2524. if (!bp->rx_ring)
  2525. return -ENOMEM;
  2526. for (i = 0; i < bp->rx_nr_rings; i++) {
  2527. bp->rx_ring[i].bnapi = bp->bnapi[i];
  2528. bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
  2529. }
  2530. bp->tx_ring = kcalloc(bp->tx_nr_rings,
  2531. sizeof(struct bnxt_tx_ring_info),
  2532. GFP_KERNEL);
  2533. if (!bp->tx_ring)
  2534. return -ENOMEM;
  2535. bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
  2536. GFP_KERNEL);
  2537. if (!bp->tx_ring_map)
  2538. return -ENOMEM;
  2539. if (bp->flags & BNXT_FLAG_SHARED_RINGS)
  2540. j = 0;
  2541. else
  2542. j = bp->rx_nr_rings;
  2543. for (i = 0; i < bp->tx_nr_rings; i++, j++) {
  2544. bp->tx_ring[i].bnapi = bp->bnapi[j];
  2545. bp->bnapi[j]->tx_ring = &bp->tx_ring[i];
  2546. bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
  2547. if (i >= bp->tx_nr_rings_xdp) {
  2548. bp->tx_ring[i].txq_index = i -
  2549. bp->tx_nr_rings_xdp;
  2550. bp->bnapi[j]->tx_int = bnxt_tx_int;
  2551. } else {
  2552. bp->bnapi[j]->flags |= BNXT_NAPI_FLAG_XDP;
  2553. bp->bnapi[j]->tx_int = bnxt_tx_int_xdp;
  2554. }
  2555. }
  2556. rc = bnxt_alloc_stats(bp);
  2557. if (rc)
  2558. goto alloc_mem_err;
  2559. rc = bnxt_alloc_ntp_fltrs(bp);
  2560. if (rc)
  2561. goto alloc_mem_err;
  2562. rc = bnxt_alloc_vnics(bp);
  2563. if (rc)
  2564. goto alloc_mem_err;
  2565. }
  2566. bnxt_init_ring_struct(bp);
  2567. rc = bnxt_alloc_rx_rings(bp);
  2568. if (rc)
  2569. goto alloc_mem_err;
  2570. rc = bnxt_alloc_tx_rings(bp);
  2571. if (rc)
  2572. goto alloc_mem_err;
  2573. rc = bnxt_alloc_cp_rings(bp);
  2574. if (rc)
  2575. goto alloc_mem_err;
  2576. bp->vnic_info[0].flags |= BNXT_VNIC_RSS_FLAG | BNXT_VNIC_MCAST_FLAG |
  2577. BNXT_VNIC_UCAST_FLAG;
  2578. rc = bnxt_alloc_vnic_attributes(bp);
  2579. if (rc)
  2580. goto alloc_mem_err;
  2581. return 0;
  2582. alloc_mem_err:
  2583. bnxt_free_mem(bp, true);
  2584. return rc;
  2585. }
  2586. static void bnxt_disable_int(struct bnxt *bp)
  2587. {
  2588. int i;
  2589. if (!bp->bnapi)
  2590. return;
  2591. for (i = 0; i < bp->cp_nr_rings; i++) {
  2592. struct bnxt_napi *bnapi = bp->bnapi[i];
  2593. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  2594. struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
  2595. if (ring->fw_ring_id != INVALID_HW_RING_ID)
  2596. BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
  2597. }
  2598. }
  2599. static void bnxt_disable_int_sync(struct bnxt *bp)
  2600. {
  2601. int i;
  2602. atomic_inc(&bp->intr_sem);
  2603. bnxt_disable_int(bp);
  2604. for (i = 0; i < bp->cp_nr_rings; i++)
  2605. synchronize_irq(bp->irq_tbl[i].vector);
  2606. }
  2607. static void bnxt_enable_int(struct bnxt *bp)
  2608. {
  2609. int i;
  2610. atomic_set(&bp->intr_sem, 0);
  2611. for (i = 0; i < bp->cp_nr_rings; i++) {
  2612. struct bnxt_napi *bnapi = bp->bnapi[i];
  2613. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  2614. BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
  2615. }
  2616. }
  2617. void bnxt_hwrm_cmd_hdr_init(struct bnxt *bp, void *request, u16 req_type,
  2618. u16 cmpl_ring, u16 target_id)
  2619. {
  2620. struct input *req = request;
  2621. req->req_type = cpu_to_le16(req_type);
  2622. req->cmpl_ring = cpu_to_le16(cmpl_ring);
  2623. req->target_id = cpu_to_le16(target_id);
  2624. req->resp_addr = cpu_to_le64(bp->hwrm_cmd_resp_dma_addr);
  2625. }
  2626. static int bnxt_hwrm_do_send_msg(struct bnxt *bp, void *msg, u32 msg_len,
  2627. int timeout, bool silent)
  2628. {
  2629. int i, intr_process, rc, tmo_count;
  2630. struct input *req = msg;
  2631. u32 *data = msg;
  2632. __le32 *resp_len, *valid;
  2633. u16 cp_ring_id, len = 0;
  2634. struct hwrm_err_output *resp = bp->hwrm_cmd_resp_addr;
  2635. req->seq_id = cpu_to_le16(bp->hwrm_cmd_seq++);
  2636. memset(resp, 0, PAGE_SIZE);
  2637. cp_ring_id = le16_to_cpu(req->cmpl_ring);
  2638. intr_process = (cp_ring_id == INVALID_HW_RING_ID) ? 0 : 1;
  2639. /* Write request msg to hwrm channel */
  2640. __iowrite32_copy(bp->bar0, data, msg_len / 4);
  2641. for (i = msg_len; i < BNXT_HWRM_MAX_REQ_LEN; i += 4)
  2642. writel(0, bp->bar0 + i);
  2643. /* currently supports only one outstanding message */
  2644. if (intr_process)
  2645. bp->hwrm_intr_seq_id = le16_to_cpu(req->seq_id);
  2646. /* Ring channel doorbell */
  2647. writel(1, bp->bar0 + 0x100);
  2648. if (!timeout)
  2649. timeout = DFLT_HWRM_CMD_TIMEOUT;
  2650. i = 0;
  2651. tmo_count = timeout * 40;
  2652. if (intr_process) {
  2653. /* Wait until hwrm response cmpl interrupt is processed */
  2654. while (bp->hwrm_intr_seq_id != HWRM_SEQ_ID_INVALID &&
  2655. i++ < tmo_count) {
  2656. usleep_range(25, 40);
  2657. }
  2658. if (bp->hwrm_intr_seq_id != HWRM_SEQ_ID_INVALID) {
  2659. netdev_err(bp->dev, "Resp cmpl intr err msg: 0x%x\n",
  2660. le16_to_cpu(req->req_type));
  2661. return -1;
  2662. }
  2663. } else {
  2664. /* Check if response len is updated */
  2665. resp_len = bp->hwrm_cmd_resp_addr + HWRM_RESP_LEN_OFFSET;
  2666. for (i = 0; i < tmo_count; i++) {
  2667. len = (le32_to_cpu(*resp_len) & HWRM_RESP_LEN_MASK) >>
  2668. HWRM_RESP_LEN_SFT;
  2669. if (len)
  2670. break;
  2671. usleep_range(25, 40);
  2672. }
  2673. if (i >= tmo_count) {
  2674. netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d\n",
  2675. timeout, le16_to_cpu(req->req_type),
  2676. le16_to_cpu(req->seq_id), len);
  2677. return -1;
  2678. }
  2679. /* Last word of resp contains valid bit */
  2680. valid = bp->hwrm_cmd_resp_addr + len - 4;
  2681. for (i = 0; i < 5; i++) {
  2682. if (le32_to_cpu(*valid) & HWRM_RESP_VALID_MASK)
  2683. break;
  2684. udelay(1);
  2685. }
  2686. if (i >= 5) {
  2687. netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d v:%d\n",
  2688. timeout, le16_to_cpu(req->req_type),
  2689. le16_to_cpu(req->seq_id), len, *valid);
  2690. return -1;
  2691. }
  2692. }
  2693. rc = le16_to_cpu(resp->error_code);
  2694. if (rc && !silent)
  2695. netdev_err(bp->dev, "hwrm req_type 0x%x seq id 0x%x error 0x%x\n",
  2696. le16_to_cpu(resp->req_type),
  2697. le16_to_cpu(resp->seq_id), rc);
  2698. return rc;
  2699. }
  2700. int _hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
  2701. {
  2702. return bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, false);
  2703. }
  2704. int hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
  2705. {
  2706. int rc;
  2707. mutex_lock(&bp->hwrm_cmd_lock);
  2708. rc = _hwrm_send_message(bp, msg, msg_len, timeout);
  2709. mutex_unlock(&bp->hwrm_cmd_lock);
  2710. return rc;
  2711. }
  2712. int hwrm_send_message_silent(struct bnxt *bp, void *msg, u32 msg_len,
  2713. int timeout)
  2714. {
  2715. int rc;
  2716. mutex_lock(&bp->hwrm_cmd_lock);
  2717. rc = bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, true);
  2718. mutex_unlock(&bp->hwrm_cmd_lock);
  2719. return rc;
  2720. }
  2721. int bnxt_hwrm_func_rgtr_async_events(struct bnxt *bp, unsigned long *bmap,
  2722. int bmap_size)
  2723. {
  2724. struct hwrm_func_drv_rgtr_input req = {0};
  2725. DECLARE_BITMAP(async_events_bmap, 256);
  2726. u32 *events = (u32 *)async_events_bmap;
  2727. int i;
  2728. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_RGTR, -1, -1);
  2729. req.enables =
  2730. cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
  2731. memset(async_events_bmap, 0, sizeof(async_events_bmap));
  2732. for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++)
  2733. __set_bit(bnxt_async_events_arr[i], async_events_bmap);
  2734. if (bmap && bmap_size) {
  2735. for (i = 0; i < bmap_size; i++) {
  2736. if (test_bit(i, bmap))
  2737. __set_bit(i, async_events_bmap);
  2738. }
  2739. }
  2740. for (i = 0; i < 8; i++)
  2741. req.async_event_fwd[i] |= cpu_to_le32(events[i]);
  2742. return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  2743. }
  2744. static int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp)
  2745. {
  2746. struct hwrm_func_drv_rgtr_input req = {0};
  2747. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_RGTR, -1, -1);
  2748. req.enables =
  2749. cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
  2750. FUNC_DRV_RGTR_REQ_ENABLES_VER);
  2751. req.os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
  2752. req.ver_maj = DRV_VER_MAJ;
  2753. req.ver_min = DRV_VER_MIN;
  2754. req.ver_upd = DRV_VER_UPD;
  2755. if (BNXT_PF(bp)) {
  2756. DECLARE_BITMAP(vf_req_snif_bmap, 256);
  2757. u32 *data = (u32 *)vf_req_snif_bmap;
  2758. int i;
  2759. memset(vf_req_snif_bmap, 0, sizeof(vf_req_snif_bmap));
  2760. for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++)
  2761. __set_bit(bnxt_vf_req_snif[i], vf_req_snif_bmap);
  2762. for (i = 0; i < 8; i++)
  2763. req.vf_req_fwd[i] = cpu_to_le32(data[i]);
  2764. req.enables |=
  2765. cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
  2766. }
  2767. return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  2768. }
  2769. static int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
  2770. {
  2771. struct hwrm_func_drv_unrgtr_input req = {0};
  2772. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_UNRGTR, -1, -1);
  2773. return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  2774. }
  2775. static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
  2776. {
  2777. u32 rc = 0;
  2778. struct hwrm_tunnel_dst_port_free_input req = {0};
  2779. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_FREE, -1, -1);
  2780. req.tunnel_type = tunnel_type;
  2781. switch (tunnel_type) {
  2782. case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
  2783. req.tunnel_dst_port_id = bp->vxlan_fw_dst_port_id;
  2784. break;
  2785. case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
  2786. req.tunnel_dst_port_id = bp->nge_fw_dst_port_id;
  2787. break;
  2788. default:
  2789. break;
  2790. }
  2791. rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  2792. if (rc)
  2793. netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
  2794. rc);
  2795. return rc;
  2796. }
  2797. static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
  2798. u8 tunnel_type)
  2799. {
  2800. u32 rc = 0;
  2801. struct hwrm_tunnel_dst_port_alloc_input req = {0};
  2802. struct hwrm_tunnel_dst_port_alloc_output *resp = bp->hwrm_cmd_resp_addr;
  2803. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_ALLOC, -1, -1);
  2804. req.tunnel_type = tunnel_type;
  2805. req.tunnel_dst_port_val = port;
  2806. mutex_lock(&bp->hwrm_cmd_lock);
  2807. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  2808. if (rc) {
  2809. netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
  2810. rc);
  2811. goto err_out;
  2812. }
  2813. switch (tunnel_type) {
  2814. case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
  2815. bp->vxlan_fw_dst_port_id = resp->tunnel_dst_port_id;
  2816. break;
  2817. case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
  2818. bp->nge_fw_dst_port_id = resp->tunnel_dst_port_id;
  2819. break;
  2820. default:
  2821. break;
  2822. }
  2823. err_out:
  2824. mutex_unlock(&bp->hwrm_cmd_lock);
  2825. return rc;
  2826. }
  2827. static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
  2828. {
  2829. struct hwrm_cfa_l2_set_rx_mask_input req = {0};
  2830. struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
  2831. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_SET_RX_MASK, -1, -1);
  2832. req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
  2833. req.num_mc_entries = cpu_to_le32(vnic->mc_list_count);
  2834. req.mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
  2835. req.mask = cpu_to_le32(vnic->rx_mask);
  2836. return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  2837. }
  2838. #ifdef CONFIG_RFS_ACCEL
  2839. static int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
  2840. struct bnxt_ntuple_filter *fltr)
  2841. {
  2842. struct hwrm_cfa_ntuple_filter_free_input req = {0};
  2843. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_FREE, -1, -1);
  2844. req.ntuple_filter_id = fltr->filter_id;
  2845. return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  2846. }
  2847. #define BNXT_NTP_FLTR_FLAGS \
  2848. (CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID | \
  2849. CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE | \
  2850. CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_MACADDR | \
  2851. CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE | \
  2852. CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR | \
  2853. CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK | \
  2854. CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR | \
  2855. CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK | \
  2856. CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL | \
  2857. CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT | \
  2858. CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK | \
  2859. CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT | \
  2860. CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK | \
  2861. CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
  2862. #define BNXT_NTP_TUNNEL_FLTR_FLAG \
  2863. CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
  2864. static int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
  2865. struct bnxt_ntuple_filter *fltr)
  2866. {
  2867. int rc = 0;
  2868. struct hwrm_cfa_ntuple_filter_alloc_input req = {0};
  2869. struct hwrm_cfa_ntuple_filter_alloc_output *resp =
  2870. bp->hwrm_cmd_resp_addr;
  2871. struct flow_keys *keys = &fltr->fkeys;
  2872. struct bnxt_vnic_info *vnic = &bp->vnic_info[fltr->rxq + 1];
  2873. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_ALLOC, -1, -1);
  2874. req.l2_filter_id = bp->vnic_info[0].fw_l2_filter_id[fltr->l2_fltr_idx];
  2875. req.enables = cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
  2876. req.ethertype = htons(ETH_P_IP);
  2877. memcpy(req.src_macaddr, fltr->src_mac_addr, ETH_ALEN);
  2878. req.ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
  2879. req.ip_protocol = keys->basic.ip_proto;
  2880. if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
  2881. int i;
  2882. req.ethertype = htons(ETH_P_IPV6);
  2883. req.ip_addr_type =
  2884. CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
  2885. *(struct in6_addr *)&req.src_ipaddr[0] =
  2886. keys->addrs.v6addrs.src;
  2887. *(struct in6_addr *)&req.dst_ipaddr[0] =
  2888. keys->addrs.v6addrs.dst;
  2889. for (i = 0; i < 4; i++) {
  2890. req.src_ipaddr_mask[i] = cpu_to_be32(0xffffffff);
  2891. req.dst_ipaddr_mask[i] = cpu_to_be32(0xffffffff);
  2892. }
  2893. } else {
  2894. req.src_ipaddr[0] = keys->addrs.v4addrs.src;
  2895. req.src_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
  2896. req.dst_ipaddr[0] = keys->addrs.v4addrs.dst;
  2897. req.dst_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
  2898. }
  2899. if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
  2900. req.enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
  2901. req.tunnel_type =
  2902. CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
  2903. }
  2904. req.src_port = keys->ports.src;
  2905. req.src_port_mask = cpu_to_be16(0xffff);
  2906. req.dst_port = keys->ports.dst;
  2907. req.dst_port_mask = cpu_to_be16(0xffff);
  2908. req.dst_id = cpu_to_le16(vnic->fw_vnic_id);
  2909. mutex_lock(&bp->hwrm_cmd_lock);
  2910. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  2911. if (!rc)
  2912. fltr->filter_id = resp->ntuple_filter_id;
  2913. mutex_unlock(&bp->hwrm_cmd_lock);
  2914. return rc;
  2915. }
  2916. #endif
  2917. static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
  2918. u8 *mac_addr)
  2919. {
  2920. u32 rc = 0;
  2921. struct hwrm_cfa_l2_filter_alloc_input req = {0};
  2922. struct hwrm_cfa_l2_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
  2923. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_ALLOC, -1, -1);
  2924. req.flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
  2925. if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
  2926. req.flags |=
  2927. cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
  2928. req.dst_id = cpu_to_le16(bp->vnic_info[vnic_id].fw_vnic_id);
  2929. req.enables =
  2930. cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
  2931. CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
  2932. CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
  2933. memcpy(req.l2_addr, mac_addr, ETH_ALEN);
  2934. req.l2_addr_mask[0] = 0xff;
  2935. req.l2_addr_mask[1] = 0xff;
  2936. req.l2_addr_mask[2] = 0xff;
  2937. req.l2_addr_mask[3] = 0xff;
  2938. req.l2_addr_mask[4] = 0xff;
  2939. req.l2_addr_mask[5] = 0xff;
  2940. mutex_lock(&bp->hwrm_cmd_lock);
  2941. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  2942. if (!rc)
  2943. bp->vnic_info[vnic_id].fw_l2_filter_id[idx] =
  2944. resp->l2_filter_id;
  2945. mutex_unlock(&bp->hwrm_cmd_lock);
  2946. return rc;
  2947. }
  2948. static int bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
  2949. {
  2950. u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
  2951. int rc = 0;
  2952. /* Any associated ntuple filters will also be cleared by firmware. */
  2953. mutex_lock(&bp->hwrm_cmd_lock);
  2954. for (i = 0; i < num_of_vnics; i++) {
  2955. struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
  2956. for (j = 0; j < vnic->uc_filter_count; j++) {
  2957. struct hwrm_cfa_l2_filter_free_input req = {0};
  2958. bnxt_hwrm_cmd_hdr_init(bp, &req,
  2959. HWRM_CFA_L2_FILTER_FREE, -1, -1);
  2960. req.l2_filter_id = vnic->fw_l2_filter_id[j];
  2961. rc = _hwrm_send_message(bp, &req, sizeof(req),
  2962. HWRM_CMD_TIMEOUT);
  2963. }
  2964. vnic->uc_filter_count = 0;
  2965. }
  2966. mutex_unlock(&bp->hwrm_cmd_lock);
  2967. return rc;
  2968. }
  2969. static int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, u16 vnic_id, u32 tpa_flags)
  2970. {
  2971. struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
  2972. struct hwrm_vnic_tpa_cfg_input req = {0};
  2973. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_TPA_CFG, -1, -1);
  2974. if (tpa_flags) {
  2975. u16 mss = bp->dev->mtu - 40;
  2976. u32 nsegs, n, segs = 0, flags;
  2977. flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
  2978. VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
  2979. VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
  2980. VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
  2981. VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
  2982. if (tpa_flags & BNXT_FLAG_GRO)
  2983. flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
  2984. req.flags = cpu_to_le32(flags);
  2985. req.enables =
  2986. cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
  2987. VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
  2988. VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
  2989. /* Number of segs are log2 units, and first packet is not
  2990. * included as part of this units.
  2991. */
  2992. if (mss <= BNXT_RX_PAGE_SIZE) {
  2993. n = BNXT_RX_PAGE_SIZE / mss;
  2994. nsegs = (MAX_SKB_FRAGS - 1) * n;
  2995. } else {
  2996. n = mss / BNXT_RX_PAGE_SIZE;
  2997. if (mss & (BNXT_RX_PAGE_SIZE - 1))
  2998. n++;
  2999. nsegs = (MAX_SKB_FRAGS - n) / n;
  3000. }
  3001. segs = ilog2(nsegs);
  3002. req.max_agg_segs = cpu_to_le16(segs);
  3003. req.max_aggs = cpu_to_le16(VNIC_TPA_CFG_REQ_MAX_AGGS_MAX);
  3004. req.min_agg_len = cpu_to_le32(512);
  3005. }
  3006. req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
  3007. return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3008. }
  3009. static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, u16 vnic_id, bool set_rss)
  3010. {
  3011. u32 i, j, max_rings;
  3012. struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
  3013. struct hwrm_vnic_rss_cfg_input req = {0};
  3014. if (vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
  3015. return 0;
  3016. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_CFG, -1, -1);
  3017. if (set_rss) {
  3018. req.hash_type = cpu_to_le32(bp->rss_hash_cfg);
  3019. if (vnic->flags & BNXT_VNIC_RSS_FLAG) {
  3020. if (BNXT_CHIP_TYPE_NITRO_A0(bp))
  3021. max_rings = bp->rx_nr_rings - 1;
  3022. else
  3023. max_rings = bp->rx_nr_rings;
  3024. } else {
  3025. max_rings = 1;
  3026. }
  3027. /* Fill the RSS indirection table with ring group ids */
  3028. for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++, j++) {
  3029. if (j == max_rings)
  3030. j = 0;
  3031. vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
  3032. }
  3033. req.ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
  3034. req.hash_key_tbl_addr =
  3035. cpu_to_le64(vnic->rss_hash_key_dma_addr);
  3036. }
  3037. req.rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
  3038. return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3039. }
  3040. static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, u16 vnic_id)
  3041. {
  3042. struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
  3043. struct hwrm_vnic_plcmodes_cfg_input req = {0};
  3044. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_PLCMODES_CFG, -1, -1);
  3045. req.flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT |
  3046. VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
  3047. VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
  3048. req.enables =
  3049. cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID |
  3050. VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
  3051. /* thresholds not implemented in firmware yet */
  3052. req.jumbo_thresh = cpu_to_le16(bp->rx_copy_thresh);
  3053. req.hds_threshold = cpu_to_le16(bp->rx_copy_thresh);
  3054. req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
  3055. return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3056. }
  3057. static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp, u16 vnic_id,
  3058. u16 ctx_idx)
  3059. {
  3060. struct hwrm_vnic_rss_cos_lb_ctx_free_input req = {0};
  3061. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_FREE, -1, -1);
  3062. req.rss_cos_lb_ctx_id =
  3063. cpu_to_le16(bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx]);
  3064. hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3065. bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
  3066. }
  3067. static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
  3068. {
  3069. int i, j;
  3070. for (i = 0; i < bp->nr_vnics; i++) {
  3071. struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
  3072. for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
  3073. if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
  3074. bnxt_hwrm_vnic_ctx_free_one(bp, i, j);
  3075. }
  3076. }
  3077. bp->rsscos_nr_ctxs = 0;
  3078. }
  3079. static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp, u16 vnic_id, u16 ctx_idx)
  3080. {
  3081. int rc;
  3082. struct hwrm_vnic_rss_cos_lb_ctx_alloc_input req = {0};
  3083. struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp =
  3084. bp->hwrm_cmd_resp_addr;
  3085. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC, -1,
  3086. -1);
  3087. mutex_lock(&bp->hwrm_cmd_lock);
  3088. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3089. if (!rc)
  3090. bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] =
  3091. le16_to_cpu(resp->rss_cos_lb_ctx_id);
  3092. mutex_unlock(&bp->hwrm_cmd_lock);
  3093. return rc;
  3094. }
  3095. int bnxt_hwrm_vnic_cfg(struct bnxt *bp, u16 vnic_id)
  3096. {
  3097. unsigned int ring = 0, grp_idx;
  3098. struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
  3099. struct hwrm_vnic_cfg_input req = {0};
  3100. u16 def_vlan = 0;
  3101. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_CFG, -1, -1);
  3102. req.enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
  3103. /* Only RSS support for now TBD: COS & LB */
  3104. if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
  3105. req.rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
  3106. req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
  3107. VNIC_CFG_REQ_ENABLES_MRU);
  3108. } else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
  3109. req.rss_rule =
  3110. cpu_to_le16(bp->vnic_info[0].fw_rss_cos_lb_ctx[0]);
  3111. req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
  3112. VNIC_CFG_REQ_ENABLES_MRU);
  3113. req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
  3114. } else {
  3115. req.rss_rule = cpu_to_le16(0xffff);
  3116. }
  3117. if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
  3118. (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
  3119. req.cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
  3120. req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
  3121. } else {
  3122. req.cos_rule = cpu_to_le16(0xffff);
  3123. }
  3124. if (vnic->flags & BNXT_VNIC_RSS_FLAG)
  3125. ring = 0;
  3126. else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
  3127. ring = vnic_id - 1;
  3128. else if ((vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
  3129. ring = bp->rx_nr_rings - 1;
  3130. grp_idx = bp->rx_ring[ring].bnapi->index;
  3131. req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
  3132. req.dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
  3133. req.lb_rule = cpu_to_le16(0xffff);
  3134. req.mru = cpu_to_le16(bp->dev->mtu + ETH_HLEN + ETH_FCS_LEN +
  3135. VLAN_HLEN);
  3136. #ifdef CONFIG_BNXT_SRIOV
  3137. if (BNXT_VF(bp))
  3138. def_vlan = bp->vf.vlan;
  3139. #endif
  3140. if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
  3141. req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
  3142. if (!vnic_id && bnxt_ulp_registered(bp->edev, BNXT_ROCE_ULP))
  3143. req.flags |=
  3144. cpu_to_le32(VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE);
  3145. return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3146. }
  3147. static int bnxt_hwrm_vnic_free_one(struct bnxt *bp, u16 vnic_id)
  3148. {
  3149. u32 rc = 0;
  3150. if (bp->vnic_info[vnic_id].fw_vnic_id != INVALID_HW_RING_ID) {
  3151. struct hwrm_vnic_free_input req = {0};
  3152. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_FREE, -1, -1);
  3153. req.vnic_id =
  3154. cpu_to_le32(bp->vnic_info[vnic_id].fw_vnic_id);
  3155. rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3156. if (rc)
  3157. return rc;
  3158. bp->vnic_info[vnic_id].fw_vnic_id = INVALID_HW_RING_ID;
  3159. }
  3160. return rc;
  3161. }
  3162. static void bnxt_hwrm_vnic_free(struct bnxt *bp)
  3163. {
  3164. u16 i;
  3165. for (i = 0; i < bp->nr_vnics; i++)
  3166. bnxt_hwrm_vnic_free_one(bp, i);
  3167. }
  3168. static int bnxt_hwrm_vnic_alloc(struct bnxt *bp, u16 vnic_id,
  3169. unsigned int start_rx_ring_idx,
  3170. unsigned int nr_rings)
  3171. {
  3172. int rc = 0;
  3173. unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
  3174. struct hwrm_vnic_alloc_input req = {0};
  3175. struct hwrm_vnic_alloc_output *resp = bp->hwrm_cmd_resp_addr;
  3176. /* map ring groups to this vnic */
  3177. for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
  3178. grp_idx = bp->rx_ring[i].bnapi->index;
  3179. if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
  3180. netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
  3181. j, nr_rings);
  3182. break;
  3183. }
  3184. bp->vnic_info[vnic_id].fw_grp_ids[j] =
  3185. bp->grp_info[grp_idx].fw_grp_id;
  3186. }
  3187. bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[0] = INVALID_HW_RING_ID;
  3188. bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[1] = INVALID_HW_RING_ID;
  3189. if (vnic_id == 0)
  3190. req.flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
  3191. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_ALLOC, -1, -1);
  3192. mutex_lock(&bp->hwrm_cmd_lock);
  3193. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3194. if (!rc)
  3195. bp->vnic_info[vnic_id].fw_vnic_id = le32_to_cpu(resp->vnic_id);
  3196. mutex_unlock(&bp->hwrm_cmd_lock);
  3197. return rc;
  3198. }
  3199. static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
  3200. {
  3201. struct hwrm_vnic_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
  3202. struct hwrm_vnic_qcaps_input req = {0};
  3203. int rc;
  3204. if (bp->hwrm_spec_code < 0x10600)
  3205. return 0;
  3206. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_QCAPS, -1, -1);
  3207. mutex_lock(&bp->hwrm_cmd_lock);
  3208. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3209. if (!rc) {
  3210. if (resp->flags &
  3211. cpu_to_le32(VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
  3212. bp->flags |= BNXT_FLAG_NEW_RSS_CAP;
  3213. }
  3214. mutex_unlock(&bp->hwrm_cmd_lock);
  3215. return rc;
  3216. }
  3217. static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
  3218. {
  3219. u16 i;
  3220. u32 rc = 0;
  3221. mutex_lock(&bp->hwrm_cmd_lock);
  3222. for (i = 0; i < bp->rx_nr_rings; i++) {
  3223. struct hwrm_ring_grp_alloc_input req = {0};
  3224. struct hwrm_ring_grp_alloc_output *resp =
  3225. bp->hwrm_cmd_resp_addr;
  3226. unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
  3227. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_ALLOC, -1, -1);
  3228. req.cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
  3229. req.rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
  3230. req.ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
  3231. req.sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
  3232. rc = _hwrm_send_message(bp, &req, sizeof(req),
  3233. HWRM_CMD_TIMEOUT);
  3234. if (rc)
  3235. break;
  3236. bp->grp_info[grp_idx].fw_grp_id =
  3237. le32_to_cpu(resp->ring_group_id);
  3238. }
  3239. mutex_unlock(&bp->hwrm_cmd_lock);
  3240. return rc;
  3241. }
  3242. static int bnxt_hwrm_ring_grp_free(struct bnxt *bp)
  3243. {
  3244. u16 i;
  3245. u32 rc = 0;
  3246. struct hwrm_ring_grp_free_input req = {0};
  3247. if (!bp->grp_info)
  3248. return 0;
  3249. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_FREE, -1, -1);
  3250. mutex_lock(&bp->hwrm_cmd_lock);
  3251. for (i = 0; i < bp->cp_nr_rings; i++) {
  3252. if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
  3253. continue;
  3254. req.ring_group_id =
  3255. cpu_to_le32(bp->grp_info[i].fw_grp_id);
  3256. rc = _hwrm_send_message(bp, &req, sizeof(req),
  3257. HWRM_CMD_TIMEOUT);
  3258. if (rc)
  3259. break;
  3260. bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
  3261. }
  3262. mutex_unlock(&bp->hwrm_cmd_lock);
  3263. return rc;
  3264. }
  3265. static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
  3266. struct bnxt_ring_struct *ring,
  3267. u32 ring_type, u32 map_index,
  3268. u32 stats_ctx_id)
  3269. {
  3270. int rc = 0, err = 0;
  3271. struct hwrm_ring_alloc_input req = {0};
  3272. struct hwrm_ring_alloc_output *resp = bp->hwrm_cmd_resp_addr;
  3273. u16 ring_id;
  3274. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_ALLOC, -1, -1);
  3275. req.enables = 0;
  3276. if (ring->nr_pages > 1) {
  3277. req.page_tbl_addr = cpu_to_le64(ring->pg_tbl_map);
  3278. /* Page size is in log2 units */
  3279. req.page_size = BNXT_PAGE_SHIFT;
  3280. req.page_tbl_depth = 1;
  3281. } else {
  3282. req.page_tbl_addr = cpu_to_le64(ring->dma_arr[0]);
  3283. }
  3284. req.fbo = 0;
  3285. /* Association of ring index with doorbell index and MSIX number */
  3286. req.logical_id = cpu_to_le16(map_index);
  3287. switch (ring_type) {
  3288. case HWRM_RING_ALLOC_TX:
  3289. req.ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
  3290. /* Association of transmit ring with completion ring */
  3291. req.cmpl_ring_id =
  3292. cpu_to_le16(bp->grp_info[map_index].cp_fw_ring_id);
  3293. req.length = cpu_to_le32(bp->tx_ring_mask + 1);
  3294. req.stat_ctx_id = cpu_to_le32(stats_ctx_id);
  3295. req.queue_id = cpu_to_le16(ring->queue_id);
  3296. break;
  3297. case HWRM_RING_ALLOC_RX:
  3298. req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
  3299. req.length = cpu_to_le32(bp->rx_ring_mask + 1);
  3300. break;
  3301. case HWRM_RING_ALLOC_AGG:
  3302. req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
  3303. req.length = cpu_to_le32(bp->rx_agg_ring_mask + 1);
  3304. break;
  3305. case HWRM_RING_ALLOC_CMPL:
  3306. req.ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
  3307. req.length = cpu_to_le32(bp->cp_ring_mask + 1);
  3308. if (bp->flags & BNXT_FLAG_USING_MSIX)
  3309. req.int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
  3310. break;
  3311. default:
  3312. netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
  3313. ring_type);
  3314. return -1;
  3315. }
  3316. mutex_lock(&bp->hwrm_cmd_lock);
  3317. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3318. err = le16_to_cpu(resp->error_code);
  3319. ring_id = le16_to_cpu(resp->ring_id);
  3320. mutex_unlock(&bp->hwrm_cmd_lock);
  3321. if (rc || err) {
  3322. switch (ring_type) {
  3323. case RING_FREE_REQ_RING_TYPE_L2_CMPL:
  3324. netdev_err(bp->dev, "hwrm_ring_alloc cp failed. rc:%x err:%x\n",
  3325. rc, err);
  3326. return -1;
  3327. case RING_FREE_REQ_RING_TYPE_RX:
  3328. netdev_err(bp->dev, "hwrm_ring_alloc rx failed. rc:%x err:%x\n",
  3329. rc, err);
  3330. return -1;
  3331. case RING_FREE_REQ_RING_TYPE_TX:
  3332. netdev_err(bp->dev, "hwrm_ring_alloc tx failed. rc:%x err:%x\n",
  3333. rc, err);
  3334. return -1;
  3335. default:
  3336. netdev_err(bp->dev, "Invalid ring\n");
  3337. return -1;
  3338. }
  3339. }
  3340. ring->fw_ring_id = ring_id;
  3341. return rc;
  3342. }
  3343. static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
  3344. {
  3345. int rc;
  3346. if (BNXT_PF(bp)) {
  3347. struct hwrm_func_cfg_input req = {0};
  3348. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
  3349. req.fid = cpu_to_le16(0xffff);
  3350. req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
  3351. req.async_event_cr = cpu_to_le16(idx);
  3352. rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3353. } else {
  3354. struct hwrm_func_vf_cfg_input req = {0};
  3355. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_VF_CFG, -1, -1);
  3356. req.enables =
  3357. cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
  3358. req.async_event_cr = cpu_to_le16(idx);
  3359. rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3360. }
  3361. return rc;
  3362. }
  3363. static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
  3364. {
  3365. int i, rc = 0;
  3366. for (i = 0; i < bp->cp_nr_rings; i++) {
  3367. struct bnxt_napi *bnapi = bp->bnapi[i];
  3368. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  3369. struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
  3370. cpr->cp_doorbell = bp->bar1 + i * 0x80;
  3371. rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_CMPL, i,
  3372. INVALID_STATS_CTX_ID);
  3373. if (rc)
  3374. goto err_out;
  3375. BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
  3376. bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
  3377. if (!i) {
  3378. rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
  3379. if (rc)
  3380. netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
  3381. }
  3382. }
  3383. for (i = 0; i < bp->tx_nr_rings; i++) {
  3384. struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
  3385. struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
  3386. u32 map_idx = txr->bnapi->index;
  3387. u16 fw_stats_ctx = bp->grp_info[map_idx].fw_stats_ctx;
  3388. rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_TX,
  3389. map_idx, fw_stats_ctx);
  3390. if (rc)
  3391. goto err_out;
  3392. txr->tx_doorbell = bp->bar1 + map_idx * 0x80;
  3393. }
  3394. for (i = 0; i < bp->rx_nr_rings; i++) {
  3395. struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
  3396. struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
  3397. u32 map_idx = rxr->bnapi->index;
  3398. rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_RX,
  3399. map_idx, INVALID_STATS_CTX_ID);
  3400. if (rc)
  3401. goto err_out;
  3402. rxr->rx_doorbell = bp->bar1 + map_idx * 0x80;
  3403. writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
  3404. bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
  3405. }
  3406. if (bp->flags & BNXT_FLAG_AGG_RINGS) {
  3407. for (i = 0; i < bp->rx_nr_rings; i++) {
  3408. struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
  3409. struct bnxt_ring_struct *ring =
  3410. &rxr->rx_agg_ring_struct;
  3411. u32 grp_idx = rxr->bnapi->index;
  3412. u32 map_idx = grp_idx + bp->rx_nr_rings;
  3413. rc = hwrm_ring_alloc_send_msg(bp, ring,
  3414. HWRM_RING_ALLOC_AGG,
  3415. map_idx,
  3416. INVALID_STATS_CTX_ID);
  3417. if (rc)
  3418. goto err_out;
  3419. rxr->rx_agg_doorbell = bp->bar1 + map_idx * 0x80;
  3420. writel(DB_KEY_RX | rxr->rx_agg_prod,
  3421. rxr->rx_agg_doorbell);
  3422. bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
  3423. }
  3424. }
  3425. err_out:
  3426. return rc;
  3427. }
  3428. static int hwrm_ring_free_send_msg(struct bnxt *bp,
  3429. struct bnxt_ring_struct *ring,
  3430. u32 ring_type, int cmpl_ring_id)
  3431. {
  3432. int rc;
  3433. struct hwrm_ring_free_input req = {0};
  3434. struct hwrm_ring_free_output *resp = bp->hwrm_cmd_resp_addr;
  3435. u16 error_code;
  3436. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_FREE, cmpl_ring_id, -1);
  3437. req.ring_type = ring_type;
  3438. req.ring_id = cpu_to_le16(ring->fw_ring_id);
  3439. mutex_lock(&bp->hwrm_cmd_lock);
  3440. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3441. error_code = le16_to_cpu(resp->error_code);
  3442. mutex_unlock(&bp->hwrm_cmd_lock);
  3443. if (rc || error_code) {
  3444. switch (ring_type) {
  3445. case RING_FREE_REQ_RING_TYPE_L2_CMPL:
  3446. netdev_err(bp->dev, "hwrm_ring_free cp failed. rc:%d\n",
  3447. rc);
  3448. return rc;
  3449. case RING_FREE_REQ_RING_TYPE_RX:
  3450. netdev_err(bp->dev, "hwrm_ring_free rx failed. rc:%d\n",
  3451. rc);
  3452. return rc;
  3453. case RING_FREE_REQ_RING_TYPE_TX:
  3454. netdev_err(bp->dev, "hwrm_ring_free tx failed. rc:%d\n",
  3455. rc);
  3456. return rc;
  3457. default:
  3458. netdev_err(bp->dev, "Invalid ring\n");
  3459. return -1;
  3460. }
  3461. }
  3462. return 0;
  3463. }
  3464. static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
  3465. {
  3466. int i;
  3467. if (!bp->bnapi)
  3468. return;
  3469. for (i = 0; i < bp->tx_nr_rings; i++) {
  3470. struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
  3471. struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
  3472. u32 grp_idx = txr->bnapi->index;
  3473. u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
  3474. if (ring->fw_ring_id != INVALID_HW_RING_ID) {
  3475. hwrm_ring_free_send_msg(bp, ring,
  3476. RING_FREE_REQ_RING_TYPE_TX,
  3477. close_path ? cmpl_ring_id :
  3478. INVALID_HW_RING_ID);
  3479. ring->fw_ring_id = INVALID_HW_RING_ID;
  3480. }
  3481. }
  3482. for (i = 0; i < bp->rx_nr_rings; i++) {
  3483. struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
  3484. struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
  3485. u32 grp_idx = rxr->bnapi->index;
  3486. u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
  3487. if (ring->fw_ring_id != INVALID_HW_RING_ID) {
  3488. hwrm_ring_free_send_msg(bp, ring,
  3489. RING_FREE_REQ_RING_TYPE_RX,
  3490. close_path ? cmpl_ring_id :
  3491. INVALID_HW_RING_ID);
  3492. ring->fw_ring_id = INVALID_HW_RING_ID;
  3493. bp->grp_info[grp_idx].rx_fw_ring_id =
  3494. INVALID_HW_RING_ID;
  3495. }
  3496. }
  3497. for (i = 0; i < bp->rx_nr_rings; i++) {
  3498. struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
  3499. struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
  3500. u32 grp_idx = rxr->bnapi->index;
  3501. u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
  3502. if (ring->fw_ring_id != INVALID_HW_RING_ID) {
  3503. hwrm_ring_free_send_msg(bp, ring,
  3504. RING_FREE_REQ_RING_TYPE_RX,
  3505. close_path ? cmpl_ring_id :
  3506. INVALID_HW_RING_ID);
  3507. ring->fw_ring_id = INVALID_HW_RING_ID;
  3508. bp->grp_info[grp_idx].agg_fw_ring_id =
  3509. INVALID_HW_RING_ID;
  3510. }
  3511. }
  3512. /* The completion rings are about to be freed. After that the
  3513. * IRQ doorbell will not work anymore. So we need to disable
  3514. * IRQ here.
  3515. */
  3516. bnxt_disable_int_sync(bp);
  3517. for (i = 0; i < bp->cp_nr_rings; i++) {
  3518. struct bnxt_napi *bnapi = bp->bnapi[i];
  3519. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  3520. struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
  3521. if (ring->fw_ring_id != INVALID_HW_RING_ID) {
  3522. hwrm_ring_free_send_msg(bp, ring,
  3523. RING_FREE_REQ_RING_TYPE_L2_CMPL,
  3524. INVALID_HW_RING_ID);
  3525. ring->fw_ring_id = INVALID_HW_RING_ID;
  3526. bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
  3527. }
  3528. }
  3529. }
  3530. /* Caller must hold bp->hwrm_cmd_lock */
  3531. int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
  3532. {
  3533. struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
  3534. struct hwrm_func_qcfg_input req = {0};
  3535. int rc;
  3536. if (bp->hwrm_spec_code < 0x10601)
  3537. return 0;
  3538. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
  3539. req.fid = cpu_to_le16(fid);
  3540. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3541. if (!rc)
  3542. *tx_rings = le16_to_cpu(resp->alloc_tx_rings);
  3543. return rc;
  3544. }
  3545. static int bnxt_hwrm_reserve_tx_rings(struct bnxt *bp, int *tx_rings)
  3546. {
  3547. struct hwrm_func_cfg_input req = {0};
  3548. int rc;
  3549. if (bp->hwrm_spec_code < 0x10601)
  3550. return 0;
  3551. if (BNXT_VF(bp))
  3552. return 0;
  3553. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
  3554. req.fid = cpu_to_le16(0xffff);
  3555. req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS);
  3556. req.num_tx_rings = cpu_to_le16(*tx_rings);
  3557. rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3558. if (rc)
  3559. return rc;
  3560. mutex_lock(&bp->hwrm_cmd_lock);
  3561. rc = __bnxt_hwrm_get_tx_rings(bp, 0xffff, tx_rings);
  3562. mutex_unlock(&bp->hwrm_cmd_lock);
  3563. return rc;
  3564. }
  3565. static void bnxt_hwrm_set_coal_params(struct bnxt *bp, u32 max_bufs,
  3566. u32 buf_tmrs, u16 flags,
  3567. struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
  3568. {
  3569. req->flags = cpu_to_le16(flags);
  3570. req->num_cmpl_dma_aggr = cpu_to_le16((u16)max_bufs);
  3571. req->num_cmpl_dma_aggr_during_int = cpu_to_le16(max_bufs >> 16);
  3572. req->cmpl_aggr_dma_tmr = cpu_to_le16((u16)buf_tmrs);
  3573. req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(buf_tmrs >> 16);
  3574. /* Minimum time between 2 interrupts set to buf_tmr x 2 */
  3575. req->int_lat_tmr_min = cpu_to_le16((u16)buf_tmrs * 2);
  3576. req->int_lat_tmr_max = cpu_to_le16((u16)buf_tmrs * 4);
  3577. req->num_cmpl_aggr_int = cpu_to_le16((u16)max_bufs * 4);
  3578. }
  3579. int bnxt_hwrm_set_coal(struct bnxt *bp)
  3580. {
  3581. int i, rc = 0;
  3582. struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req_rx = {0},
  3583. req_tx = {0}, *req;
  3584. u16 max_buf, max_buf_irq;
  3585. u16 buf_tmr, buf_tmr_irq;
  3586. u32 flags;
  3587. bnxt_hwrm_cmd_hdr_init(bp, &req_rx,
  3588. HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
  3589. bnxt_hwrm_cmd_hdr_init(bp, &req_tx,
  3590. HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
  3591. /* Each rx completion (2 records) should be DMAed immediately.
  3592. * DMA 1/4 of the completion buffers at a time.
  3593. */
  3594. max_buf = min_t(u16, bp->rx_coal_bufs / 4, 2);
  3595. /* max_buf must not be zero */
  3596. max_buf = clamp_t(u16, max_buf, 1, 63);
  3597. max_buf_irq = clamp_t(u16, bp->rx_coal_bufs_irq, 1, 63);
  3598. buf_tmr = BNXT_USEC_TO_COAL_TIMER(bp->rx_coal_ticks);
  3599. /* buf timer set to 1/4 of interrupt timer */
  3600. buf_tmr = max_t(u16, buf_tmr / 4, 1);
  3601. buf_tmr_irq = BNXT_USEC_TO_COAL_TIMER(bp->rx_coal_ticks_irq);
  3602. buf_tmr_irq = max_t(u16, buf_tmr_irq, 1);
  3603. flags = RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
  3604. /* RING_IDLE generates more IRQs for lower latency. Enable it only
  3605. * if coal_ticks is less than 25 us.
  3606. */
  3607. if (bp->rx_coal_ticks < 25)
  3608. flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
  3609. bnxt_hwrm_set_coal_params(bp, max_buf_irq << 16 | max_buf,
  3610. buf_tmr_irq << 16 | buf_tmr, flags, &req_rx);
  3611. /* max_buf must not be zero */
  3612. max_buf = clamp_t(u16, bp->tx_coal_bufs, 1, 63);
  3613. max_buf_irq = clamp_t(u16, bp->tx_coal_bufs_irq, 1, 63);
  3614. buf_tmr = BNXT_USEC_TO_COAL_TIMER(bp->tx_coal_ticks);
  3615. /* buf timer set to 1/4 of interrupt timer */
  3616. buf_tmr = max_t(u16, buf_tmr / 4, 1);
  3617. buf_tmr_irq = BNXT_USEC_TO_COAL_TIMER(bp->tx_coal_ticks_irq);
  3618. buf_tmr_irq = max_t(u16, buf_tmr_irq, 1);
  3619. flags = RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
  3620. bnxt_hwrm_set_coal_params(bp, max_buf_irq << 16 | max_buf,
  3621. buf_tmr_irq << 16 | buf_tmr, flags, &req_tx);
  3622. mutex_lock(&bp->hwrm_cmd_lock);
  3623. for (i = 0; i < bp->cp_nr_rings; i++) {
  3624. struct bnxt_napi *bnapi = bp->bnapi[i];
  3625. req = &req_rx;
  3626. if (!bnapi->rx_ring)
  3627. req = &req_tx;
  3628. req->ring_id = cpu_to_le16(bp->grp_info[i].cp_fw_ring_id);
  3629. rc = _hwrm_send_message(bp, req, sizeof(*req),
  3630. HWRM_CMD_TIMEOUT);
  3631. if (rc)
  3632. break;
  3633. }
  3634. mutex_unlock(&bp->hwrm_cmd_lock);
  3635. return rc;
  3636. }
  3637. static int bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
  3638. {
  3639. int rc = 0, i;
  3640. struct hwrm_stat_ctx_free_input req = {0};
  3641. if (!bp->bnapi)
  3642. return 0;
  3643. if (BNXT_CHIP_TYPE_NITRO_A0(bp))
  3644. return 0;
  3645. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_FREE, -1, -1);
  3646. mutex_lock(&bp->hwrm_cmd_lock);
  3647. for (i = 0; i < bp->cp_nr_rings; i++) {
  3648. struct bnxt_napi *bnapi = bp->bnapi[i];
  3649. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  3650. if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
  3651. req.stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
  3652. rc = _hwrm_send_message(bp, &req, sizeof(req),
  3653. HWRM_CMD_TIMEOUT);
  3654. if (rc)
  3655. break;
  3656. cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
  3657. }
  3658. }
  3659. mutex_unlock(&bp->hwrm_cmd_lock);
  3660. return rc;
  3661. }
  3662. static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
  3663. {
  3664. int rc = 0, i;
  3665. struct hwrm_stat_ctx_alloc_input req = {0};
  3666. struct hwrm_stat_ctx_alloc_output *resp = bp->hwrm_cmd_resp_addr;
  3667. if (BNXT_CHIP_TYPE_NITRO_A0(bp))
  3668. return 0;
  3669. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_ALLOC, -1, -1);
  3670. req.update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
  3671. mutex_lock(&bp->hwrm_cmd_lock);
  3672. for (i = 0; i < bp->cp_nr_rings; i++) {
  3673. struct bnxt_napi *bnapi = bp->bnapi[i];
  3674. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  3675. req.stats_dma_addr = cpu_to_le64(cpr->hw_stats_map);
  3676. rc = _hwrm_send_message(bp, &req, sizeof(req),
  3677. HWRM_CMD_TIMEOUT);
  3678. if (rc)
  3679. break;
  3680. cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
  3681. bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
  3682. }
  3683. mutex_unlock(&bp->hwrm_cmd_lock);
  3684. return rc;
  3685. }
  3686. static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
  3687. {
  3688. struct hwrm_func_qcfg_input req = {0};
  3689. struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
  3690. int rc;
  3691. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
  3692. req.fid = cpu_to_le16(0xffff);
  3693. mutex_lock(&bp->hwrm_cmd_lock);
  3694. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3695. if (rc)
  3696. goto func_qcfg_exit;
  3697. #ifdef CONFIG_BNXT_SRIOV
  3698. if (BNXT_VF(bp)) {
  3699. struct bnxt_vf_info *vf = &bp->vf;
  3700. vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
  3701. }
  3702. #endif
  3703. if (BNXT_PF(bp) && (le16_to_cpu(resp->flags) &
  3704. FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED))
  3705. bp->flags |= BNXT_FLAG_FW_LLDP_AGENT;
  3706. switch (resp->port_partition_type) {
  3707. case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
  3708. case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
  3709. case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
  3710. bp->port_partition_type = resp->port_partition_type;
  3711. break;
  3712. }
  3713. func_qcfg_exit:
  3714. mutex_unlock(&bp->hwrm_cmd_lock);
  3715. return rc;
  3716. }
  3717. static int bnxt_hwrm_func_qcaps(struct bnxt *bp)
  3718. {
  3719. int rc = 0;
  3720. struct hwrm_func_qcaps_input req = {0};
  3721. struct hwrm_func_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
  3722. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCAPS, -1, -1);
  3723. req.fid = cpu_to_le16(0xffff);
  3724. mutex_lock(&bp->hwrm_cmd_lock);
  3725. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3726. if (rc)
  3727. goto hwrm_func_qcaps_exit;
  3728. if (resp->flags & cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED))
  3729. bp->flags |= BNXT_FLAG_ROCEV1_CAP;
  3730. if (resp->flags & cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED))
  3731. bp->flags |= BNXT_FLAG_ROCEV2_CAP;
  3732. bp->tx_push_thresh = 0;
  3733. if (resp->flags &
  3734. cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED))
  3735. bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
  3736. if (BNXT_PF(bp)) {
  3737. struct bnxt_pf_info *pf = &bp->pf;
  3738. pf->fw_fid = le16_to_cpu(resp->fid);
  3739. pf->port_id = le16_to_cpu(resp->port_id);
  3740. bp->dev->dev_port = pf->port_id;
  3741. memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
  3742. memcpy(bp->dev->dev_addr, pf->mac_addr, ETH_ALEN);
  3743. pf->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
  3744. pf->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
  3745. pf->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
  3746. pf->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
  3747. pf->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
  3748. if (!pf->max_hw_ring_grps)
  3749. pf->max_hw_ring_grps = pf->max_tx_rings;
  3750. pf->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
  3751. pf->max_vnics = le16_to_cpu(resp->max_vnics);
  3752. pf->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
  3753. pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
  3754. pf->max_vfs = le16_to_cpu(resp->max_vfs);
  3755. pf->max_encap_records = le32_to_cpu(resp->max_encap_records);
  3756. pf->max_decap_records = le32_to_cpu(resp->max_decap_records);
  3757. pf->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
  3758. pf->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
  3759. pf->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
  3760. pf->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
  3761. } else {
  3762. #ifdef CONFIG_BNXT_SRIOV
  3763. struct bnxt_vf_info *vf = &bp->vf;
  3764. vf->fw_fid = le16_to_cpu(resp->fid);
  3765. vf->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
  3766. vf->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
  3767. vf->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
  3768. vf->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
  3769. vf->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
  3770. if (!vf->max_hw_ring_grps)
  3771. vf->max_hw_ring_grps = vf->max_tx_rings;
  3772. vf->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
  3773. vf->max_vnics = le16_to_cpu(resp->max_vnics);
  3774. vf->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
  3775. memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
  3776. mutex_unlock(&bp->hwrm_cmd_lock);
  3777. if (is_valid_ether_addr(vf->mac_addr)) {
  3778. /* overwrite netdev dev_adr with admin VF MAC */
  3779. memcpy(bp->dev->dev_addr, vf->mac_addr, ETH_ALEN);
  3780. } else {
  3781. eth_hw_addr_random(bp->dev);
  3782. rc = bnxt_approve_mac(bp, bp->dev->dev_addr);
  3783. }
  3784. return rc;
  3785. #endif
  3786. }
  3787. hwrm_func_qcaps_exit:
  3788. mutex_unlock(&bp->hwrm_cmd_lock);
  3789. return rc;
  3790. }
  3791. static int bnxt_hwrm_func_reset(struct bnxt *bp)
  3792. {
  3793. struct hwrm_func_reset_input req = {0};
  3794. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_RESET, -1, -1);
  3795. req.enables = 0;
  3796. return hwrm_send_message(bp, &req, sizeof(req), HWRM_RESET_TIMEOUT);
  3797. }
  3798. static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
  3799. {
  3800. int rc = 0;
  3801. struct hwrm_queue_qportcfg_input req = {0};
  3802. struct hwrm_queue_qportcfg_output *resp = bp->hwrm_cmd_resp_addr;
  3803. u8 i, *qptr;
  3804. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_QUEUE_QPORTCFG, -1, -1);
  3805. mutex_lock(&bp->hwrm_cmd_lock);
  3806. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3807. if (rc)
  3808. goto qportcfg_exit;
  3809. if (!resp->max_configurable_queues) {
  3810. rc = -EINVAL;
  3811. goto qportcfg_exit;
  3812. }
  3813. bp->max_tc = resp->max_configurable_queues;
  3814. bp->max_lltc = resp->max_configurable_lossless_queues;
  3815. if (bp->max_tc > BNXT_MAX_QUEUE)
  3816. bp->max_tc = BNXT_MAX_QUEUE;
  3817. if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
  3818. bp->max_tc = 1;
  3819. if (bp->max_lltc > bp->max_tc)
  3820. bp->max_lltc = bp->max_tc;
  3821. qptr = &resp->queue_id0;
  3822. for (i = 0; i < bp->max_tc; i++) {
  3823. bp->q_info[i].queue_id = *qptr++;
  3824. bp->q_info[i].queue_profile = *qptr++;
  3825. }
  3826. qportcfg_exit:
  3827. mutex_unlock(&bp->hwrm_cmd_lock);
  3828. return rc;
  3829. }
  3830. static int bnxt_hwrm_ver_get(struct bnxt *bp)
  3831. {
  3832. int rc;
  3833. struct hwrm_ver_get_input req = {0};
  3834. struct hwrm_ver_get_output *resp = bp->hwrm_cmd_resp_addr;
  3835. bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
  3836. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VER_GET, -1, -1);
  3837. req.hwrm_intf_maj = HWRM_VERSION_MAJOR;
  3838. req.hwrm_intf_min = HWRM_VERSION_MINOR;
  3839. req.hwrm_intf_upd = HWRM_VERSION_UPDATE;
  3840. mutex_lock(&bp->hwrm_cmd_lock);
  3841. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3842. if (rc)
  3843. goto hwrm_ver_get_exit;
  3844. memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
  3845. bp->hwrm_spec_code = resp->hwrm_intf_maj << 16 |
  3846. resp->hwrm_intf_min << 8 | resp->hwrm_intf_upd;
  3847. if (resp->hwrm_intf_maj < 1) {
  3848. netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
  3849. resp->hwrm_intf_maj, resp->hwrm_intf_min,
  3850. resp->hwrm_intf_upd);
  3851. netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
  3852. }
  3853. snprintf(bp->fw_ver_str, BC_HWRM_STR_LEN, "%d.%d.%d/%d.%d.%d",
  3854. resp->hwrm_fw_maj, resp->hwrm_fw_min, resp->hwrm_fw_bld,
  3855. resp->hwrm_intf_maj, resp->hwrm_intf_min, resp->hwrm_intf_upd);
  3856. bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
  3857. if (!bp->hwrm_cmd_timeout)
  3858. bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
  3859. if (resp->hwrm_intf_maj >= 1)
  3860. bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
  3861. bp->chip_num = le16_to_cpu(resp->chip_num);
  3862. if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
  3863. !resp->chip_metal)
  3864. bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
  3865. hwrm_ver_get_exit:
  3866. mutex_unlock(&bp->hwrm_cmd_lock);
  3867. return rc;
  3868. }
  3869. int bnxt_hwrm_fw_set_time(struct bnxt *bp)
  3870. {
  3871. #if IS_ENABLED(CONFIG_RTC_LIB)
  3872. struct hwrm_fw_set_time_input req = {0};
  3873. struct rtc_time tm;
  3874. struct timeval tv;
  3875. if (bp->hwrm_spec_code < 0x10400)
  3876. return -EOPNOTSUPP;
  3877. do_gettimeofday(&tv);
  3878. rtc_time_to_tm(tv.tv_sec, &tm);
  3879. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FW_SET_TIME, -1, -1);
  3880. req.year = cpu_to_le16(1900 + tm.tm_year);
  3881. req.month = 1 + tm.tm_mon;
  3882. req.day = tm.tm_mday;
  3883. req.hour = tm.tm_hour;
  3884. req.minute = tm.tm_min;
  3885. req.second = tm.tm_sec;
  3886. return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3887. #else
  3888. return -EOPNOTSUPP;
  3889. #endif
  3890. }
  3891. static int bnxt_hwrm_port_qstats(struct bnxt *bp)
  3892. {
  3893. int rc;
  3894. struct bnxt_pf_info *pf = &bp->pf;
  3895. struct hwrm_port_qstats_input req = {0};
  3896. if (!(bp->flags & BNXT_FLAG_PORT_STATS))
  3897. return 0;
  3898. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_QSTATS, -1, -1);
  3899. req.port_id = cpu_to_le16(pf->port_id);
  3900. req.tx_stat_host_addr = cpu_to_le64(bp->hw_tx_port_stats_map);
  3901. req.rx_stat_host_addr = cpu_to_le64(bp->hw_rx_port_stats_map);
  3902. rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3903. return rc;
  3904. }
  3905. static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
  3906. {
  3907. if (bp->vxlan_port_cnt) {
  3908. bnxt_hwrm_tunnel_dst_port_free(
  3909. bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
  3910. }
  3911. bp->vxlan_port_cnt = 0;
  3912. if (bp->nge_port_cnt) {
  3913. bnxt_hwrm_tunnel_dst_port_free(
  3914. bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
  3915. }
  3916. bp->nge_port_cnt = 0;
  3917. }
  3918. static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
  3919. {
  3920. int rc, i;
  3921. u32 tpa_flags = 0;
  3922. if (set_tpa)
  3923. tpa_flags = bp->flags & BNXT_FLAG_TPA;
  3924. for (i = 0; i < bp->nr_vnics; i++) {
  3925. rc = bnxt_hwrm_vnic_set_tpa(bp, i, tpa_flags);
  3926. if (rc) {
  3927. netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
  3928. rc, i);
  3929. return rc;
  3930. }
  3931. }
  3932. return 0;
  3933. }
  3934. static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
  3935. {
  3936. int i;
  3937. for (i = 0; i < bp->nr_vnics; i++)
  3938. bnxt_hwrm_vnic_set_rss(bp, i, false);
  3939. }
  3940. static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
  3941. bool irq_re_init)
  3942. {
  3943. if (bp->vnic_info) {
  3944. bnxt_hwrm_clear_vnic_filter(bp);
  3945. /* clear all RSS setting before free vnic ctx */
  3946. bnxt_hwrm_clear_vnic_rss(bp);
  3947. bnxt_hwrm_vnic_ctx_free(bp);
  3948. /* before free the vnic, undo the vnic tpa settings */
  3949. if (bp->flags & BNXT_FLAG_TPA)
  3950. bnxt_set_tpa(bp, false);
  3951. bnxt_hwrm_vnic_free(bp);
  3952. }
  3953. bnxt_hwrm_ring_free(bp, close_path);
  3954. bnxt_hwrm_ring_grp_free(bp);
  3955. if (irq_re_init) {
  3956. bnxt_hwrm_stat_ctx_free(bp);
  3957. bnxt_hwrm_free_tunnel_ports(bp);
  3958. }
  3959. }
  3960. static int bnxt_setup_vnic(struct bnxt *bp, u16 vnic_id)
  3961. {
  3962. struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
  3963. int rc;
  3964. if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
  3965. goto skip_rss_ctx;
  3966. /* allocate context for vnic */
  3967. rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 0);
  3968. if (rc) {
  3969. netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
  3970. vnic_id, rc);
  3971. goto vnic_setup_err;
  3972. }
  3973. bp->rsscos_nr_ctxs++;
  3974. if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
  3975. rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 1);
  3976. if (rc) {
  3977. netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
  3978. vnic_id, rc);
  3979. goto vnic_setup_err;
  3980. }
  3981. bp->rsscos_nr_ctxs++;
  3982. }
  3983. skip_rss_ctx:
  3984. /* configure default vnic, ring grp */
  3985. rc = bnxt_hwrm_vnic_cfg(bp, vnic_id);
  3986. if (rc) {
  3987. netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
  3988. vnic_id, rc);
  3989. goto vnic_setup_err;
  3990. }
  3991. /* Enable RSS hashing on vnic */
  3992. rc = bnxt_hwrm_vnic_set_rss(bp, vnic_id, true);
  3993. if (rc) {
  3994. netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
  3995. vnic_id, rc);
  3996. goto vnic_setup_err;
  3997. }
  3998. if (bp->flags & BNXT_FLAG_AGG_RINGS) {
  3999. rc = bnxt_hwrm_vnic_set_hds(bp, vnic_id);
  4000. if (rc) {
  4001. netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
  4002. vnic_id, rc);
  4003. }
  4004. }
  4005. vnic_setup_err:
  4006. return rc;
  4007. }
  4008. static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
  4009. {
  4010. #ifdef CONFIG_RFS_ACCEL
  4011. int i, rc = 0;
  4012. for (i = 0; i < bp->rx_nr_rings; i++) {
  4013. struct bnxt_vnic_info *vnic;
  4014. u16 vnic_id = i + 1;
  4015. u16 ring_id = i;
  4016. if (vnic_id >= bp->nr_vnics)
  4017. break;
  4018. vnic = &bp->vnic_info[vnic_id];
  4019. vnic->flags |= BNXT_VNIC_RFS_FLAG;
  4020. if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
  4021. vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
  4022. rc = bnxt_hwrm_vnic_alloc(bp, vnic_id, ring_id, 1);
  4023. if (rc) {
  4024. netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
  4025. vnic_id, rc);
  4026. break;
  4027. }
  4028. rc = bnxt_setup_vnic(bp, vnic_id);
  4029. if (rc)
  4030. break;
  4031. }
  4032. return rc;
  4033. #else
  4034. return 0;
  4035. #endif
  4036. }
  4037. /* Allow PF and VF with default VLAN to be in promiscuous mode */
  4038. static bool bnxt_promisc_ok(struct bnxt *bp)
  4039. {
  4040. #ifdef CONFIG_BNXT_SRIOV
  4041. if (BNXT_VF(bp) && !bp->vf.vlan)
  4042. return false;
  4043. #endif
  4044. return true;
  4045. }
  4046. static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
  4047. {
  4048. unsigned int rc = 0;
  4049. rc = bnxt_hwrm_vnic_alloc(bp, 1, bp->rx_nr_rings - 1, 1);
  4050. if (rc) {
  4051. netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
  4052. rc);
  4053. return rc;
  4054. }
  4055. rc = bnxt_hwrm_vnic_cfg(bp, 1);
  4056. if (rc) {
  4057. netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
  4058. rc);
  4059. return rc;
  4060. }
  4061. return rc;
  4062. }
  4063. static int bnxt_cfg_rx_mode(struct bnxt *);
  4064. static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
  4065. static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
  4066. {
  4067. struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
  4068. int rc = 0;
  4069. unsigned int rx_nr_rings = bp->rx_nr_rings;
  4070. if (irq_re_init) {
  4071. rc = bnxt_hwrm_stat_ctx_alloc(bp);
  4072. if (rc) {
  4073. netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
  4074. rc);
  4075. goto err_out;
  4076. }
  4077. }
  4078. rc = bnxt_hwrm_ring_alloc(bp);
  4079. if (rc) {
  4080. netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
  4081. goto err_out;
  4082. }
  4083. rc = bnxt_hwrm_ring_grp_alloc(bp);
  4084. if (rc) {
  4085. netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
  4086. goto err_out;
  4087. }
  4088. if (BNXT_CHIP_TYPE_NITRO_A0(bp))
  4089. rx_nr_rings--;
  4090. /* default vnic 0 */
  4091. rc = bnxt_hwrm_vnic_alloc(bp, 0, 0, rx_nr_rings);
  4092. if (rc) {
  4093. netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
  4094. goto err_out;
  4095. }
  4096. rc = bnxt_setup_vnic(bp, 0);
  4097. if (rc)
  4098. goto err_out;
  4099. if (bp->flags & BNXT_FLAG_RFS) {
  4100. rc = bnxt_alloc_rfs_vnics(bp);
  4101. if (rc)
  4102. goto err_out;
  4103. }
  4104. if (bp->flags & BNXT_FLAG_TPA) {
  4105. rc = bnxt_set_tpa(bp, true);
  4106. if (rc)
  4107. goto err_out;
  4108. }
  4109. if (BNXT_VF(bp))
  4110. bnxt_update_vf_mac(bp);
  4111. /* Filter for default vnic 0 */
  4112. rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
  4113. if (rc) {
  4114. netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
  4115. goto err_out;
  4116. }
  4117. vnic->uc_filter_count = 1;
  4118. vnic->rx_mask = CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
  4119. if ((bp->dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
  4120. vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
  4121. if (bp->dev->flags & IFF_ALLMULTI) {
  4122. vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
  4123. vnic->mc_list_count = 0;
  4124. } else {
  4125. u32 mask = 0;
  4126. bnxt_mc_list_updated(bp, &mask);
  4127. vnic->rx_mask |= mask;
  4128. }
  4129. rc = bnxt_cfg_rx_mode(bp);
  4130. if (rc)
  4131. goto err_out;
  4132. rc = bnxt_hwrm_set_coal(bp);
  4133. if (rc)
  4134. netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
  4135. rc);
  4136. if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
  4137. rc = bnxt_setup_nitroa0_vnic(bp);
  4138. if (rc)
  4139. netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
  4140. rc);
  4141. }
  4142. if (BNXT_VF(bp)) {
  4143. bnxt_hwrm_func_qcfg(bp);
  4144. netdev_update_features(bp->dev);
  4145. }
  4146. return 0;
  4147. err_out:
  4148. bnxt_hwrm_resource_free(bp, 0, true);
  4149. return rc;
  4150. }
  4151. static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
  4152. {
  4153. bnxt_hwrm_resource_free(bp, 1, irq_re_init);
  4154. return 0;
  4155. }
  4156. static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
  4157. {
  4158. bnxt_init_rx_rings(bp);
  4159. bnxt_init_tx_rings(bp);
  4160. bnxt_init_ring_grps(bp, irq_re_init);
  4161. bnxt_init_vnics(bp);
  4162. return bnxt_init_chip(bp, irq_re_init);
  4163. }
  4164. static int bnxt_set_real_num_queues(struct bnxt *bp)
  4165. {
  4166. int rc;
  4167. struct net_device *dev = bp->dev;
  4168. rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
  4169. bp->tx_nr_rings_xdp);
  4170. if (rc)
  4171. return rc;
  4172. rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
  4173. if (rc)
  4174. return rc;
  4175. #ifdef CONFIG_RFS_ACCEL
  4176. if (bp->flags & BNXT_FLAG_RFS)
  4177. dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
  4178. #endif
  4179. return rc;
  4180. }
  4181. static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
  4182. bool shared)
  4183. {
  4184. int _rx = *rx, _tx = *tx;
  4185. if (shared) {
  4186. *rx = min_t(int, _rx, max);
  4187. *tx = min_t(int, _tx, max);
  4188. } else {
  4189. if (max < 2)
  4190. return -ENOMEM;
  4191. while (_rx + _tx > max) {
  4192. if (_rx > _tx && _rx > 1)
  4193. _rx--;
  4194. else if (_tx > 1)
  4195. _tx--;
  4196. }
  4197. *rx = _rx;
  4198. *tx = _tx;
  4199. }
  4200. return 0;
  4201. }
  4202. static void bnxt_setup_msix(struct bnxt *bp)
  4203. {
  4204. const int len = sizeof(bp->irq_tbl[0].name);
  4205. struct net_device *dev = bp->dev;
  4206. int tcs, i;
  4207. tcs = netdev_get_num_tc(dev);
  4208. if (tcs > 1) {
  4209. int i, off, count;
  4210. for (i = 0; i < tcs; i++) {
  4211. count = bp->tx_nr_rings_per_tc;
  4212. off = i * count;
  4213. netdev_set_tc_queue(dev, i, count, off);
  4214. }
  4215. }
  4216. for (i = 0; i < bp->cp_nr_rings; i++) {
  4217. char *attr;
  4218. if (bp->flags & BNXT_FLAG_SHARED_RINGS)
  4219. attr = "TxRx";
  4220. else if (i < bp->rx_nr_rings)
  4221. attr = "rx";
  4222. else
  4223. attr = "tx";
  4224. snprintf(bp->irq_tbl[i].name, len, "%s-%s-%d", dev->name, attr,
  4225. i);
  4226. bp->irq_tbl[i].handler = bnxt_msix;
  4227. }
  4228. }
  4229. static void bnxt_setup_inta(struct bnxt *bp)
  4230. {
  4231. const int len = sizeof(bp->irq_tbl[0].name);
  4232. if (netdev_get_num_tc(bp->dev))
  4233. netdev_reset_tc(bp->dev);
  4234. snprintf(bp->irq_tbl[0].name, len, "%s-%s-%d", bp->dev->name, "TxRx",
  4235. 0);
  4236. bp->irq_tbl[0].handler = bnxt_inta;
  4237. }
  4238. static int bnxt_setup_int_mode(struct bnxt *bp)
  4239. {
  4240. int rc;
  4241. if (bp->flags & BNXT_FLAG_USING_MSIX)
  4242. bnxt_setup_msix(bp);
  4243. else
  4244. bnxt_setup_inta(bp);
  4245. rc = bnxt_set_real_num_queues(bp);
  4246. return rc;
  4247. }
  4248. #ifdef CONFIG_RFS_ACCEL
  4249. static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
  4250. {
  4251. #if defined(CONFIG_BNXT_SRIOV)
  4252. if (BNXT_VF(bp))
  4253. return bp->vf.max_rsscos_ctxs;
  4254. #endif
  4255. return bp->pf.max_rsscos_ctxs;
  4256. }
  4257. static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
  4258. {
  4259. #if defined(CONFIG_BNXT_SRIOV)
  4260. if (BNXT_VF(bp))
  4261. return bp->vf.max_vnics;
  4262. #endif
  4263. return bp->pf.max_vnics;
  4264. }
  4265. #endif
  4266. unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
  4267. {
  4268. #if defined(CONFIG_BNXT_SRIOV)
  4269. if (BNXT_VF(bp))
  4270. return bp->vf.max_stat_ctxs;
  4271. #endif
  4272. return bp->pf.max_stat_ctxs;
  4273. }
  4274. void bnxt_set_max_func_stat_ctxs(struct bnxt *bp, unsigned int max)
  4275. {
  4276. #if defined(CONFIG_BNXT_SRIOV)
  4277. if (BNXT_VF(bp))
  4278. bp->vf.max_stat_ctxs = max;
  4279. else
  4280. #endif
  4281. bp->pf.max_stat_ctxs = max;
  4282. }
  4283. unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
  4284. {
  4285. #if defined(CONFIG_BNXT_SRIOV)
  4286. if (BNXT_VF(bp))
  4287. return bp->vf.max_cp_rings;
  4288. #endif
  4289. return bp->pf.max_cp_rings;
  4290. }
  4291. void bnxt_set_max_func_cp_rings(struct bnxt *bp, unsigned int max)
  4292. {
  4293. #if defined(CONFIG_BNXT_SRIOV)
  4294. if (BNXT_VF(bp))
  4295. bp->vf.max_cp_rings = max;
  4296. else
  4297. #endif
  4298. bp->pf.max_cp_rings = max;
  4299. }
  4300. static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
  4301. {
  4302. #if defined(CONFIG_BNXT_SRIOV)
  4303. if (BNXT_VF(bp))
  4304. return bp->vf.max_irqs;
  4305. #endif
  4306. return bp->pf.max_irqs;
  4307. }
  4308. void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
  4309. {
  4310. #if defined(CONFIG_BNXT_SRIOV)
  4311. if (BNXT_VF(bp))
  4312. bp->vf.max_irqs = max_irqs;
  4313. else
  4314. #endif
  4315. bp->pf.max_irqs = max_irqs;
  4316. }
  4317. static int bnxt_init_msix(struct bnxt *bp)
  4318. {
  4319. int i, total_vecs, rc = 0, min = 1;
  4320. struct msix_entry *msix_ent;
  4321. total_vecs = bnxt_get_max_func_irqs(bp);
  4322. msix_ent = kcalloc(total_vecs, sizeof(struct msix_entry), GFP_KERNEL);
  4323. if (!msix_ent)
  4324. return -ENOMEM;
  4325. for (i = 0; i < total_vecs; i++) {
  4326. msix_ent[i].entry = i;
  4327. msix_ent[i].vector = 0;
  4328. }
  4329. if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
  4330. min = 2;
  4331. total_vecs = pci_enable_msix_range(bp->pdev, msix_ent, min, total_vecs);
  4332. if (total_vecs < 0) {
  4333. rc = -ENODEV;
  4334. goto msix_setup_exit;
  4335. }
  4336. bp->irq_tbl = kcalloc(total_vecs, sizeof(struct bnxt_irq), GFP_KERNEL);
  4337. if (bp->irq_tbl) {
  4338. for (i = 0; i < total_vecs; i++)
  4339. bp->irq_tbl[i].vector = msix_ent[i].vector;
  4340. bp->total_irqs = total_vecs;
  4341. /* Trim rings based upon num of vectors allocated */
  4342. rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
  4343. total_vecs, min == 1);
  4344. if (rc)
  4345. goto msix_setup_exit;
  4346. bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
  4347. bp->cp_nr_rings = (min == 1) ?
  4348. max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
  4349. bp->tx_nr_rings + bp->rx_nr_rings;
  4350. } else {
  4351. rc = -ENOMEM;
  4352. goto msix_setup_exit;
  4353. }
  4354. bp->flags |= BNXT_FLAG_USING_MSIX;
  4355. kfree(msix_ent);
  4356. return 0;
  4357. msix_setup_exit:
  4358. netdev_err(bp->dev, "bnxt_init_msix err: %x\n", rc);
  4359. kfree(bp->irq_tbl);
  4360. bp->irq_tbl = NULL;
  4361. pci_disable_msix(bp->pdev);
  4362. kfree(msix_ent);
  4363. return rc;
  4364. }
  4365. static int bnxt_init_inta(struct bnxt *bp)
  4366. {
  4367. bp->irq_tbl = kcalloc(1, sizeof(struct bnxt_irq), GFP_KERNEL);
  4368. if (!bp->irq_tbl)
  4369. return -ENOMEM;
  4370. bp->total_irqs = 1;
  4371. bp->rx_nr_rings = 1;
  4372. bp->tx_nr_rings = 1;
  4373. bp->cp_nr_rings = 1;
  4374. bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
  4375. bp->flags |= BNXT_FLAG_SHARED_RINGS;
  4376. bp->irq_tbl[0].vector = bp->pdev->irq;
  4377. return 0;
  4378. }
  4379. static int bnxt_init_int_mode(struct bnxt *bp)
  4380. {
  4381. int rc = 0;
  4382. if (bp->flags & BNXT_FLAG_MSIX_CAP)
  4383. rc = bnxt_init_msix(bp);
  4384. if (!(bp->flags & BNXT_FLAG_USING_MSIX) && BNXT_PF(bp)) {
  4385. /* fallback to INTA */
  4386. rc = bnxt_init_inta(bp);
  4387. }
  4388. return rc;
  4389. }
  4390. static void bnxt_clear_int_mode(struct bnxt *bp)
  4391. {
  4392. if (bp->flags & BNXT_FLAG_USING_MSIX)
  4393. pci_disable_msix(bp->pdev);
  4394. kfree(bp->irq_tbl);
  4395. bp->irq_tbl = NULL;
  4396. bp->flags &= ~BNXT_FLAG_USING_MSIX;
  4397. }
  4398. static void bnxt_free_irq(struct bnxt *bp)
  4399. {
  4400. struct bnxt_irq *irq;
  4401. int i;
  4402. #ifdef CONFIG_RFS_ACCEL
  4403. free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
  4404. bp->dev->rx_cpu_rmap = NULL;
  4405. #endif
  4406. if (!bp->irq_tbl)
  4407. return;
  4408. for (i = 0; i < bp->cp_nr_rings; i++) {
  4409. irq = &bp->irq_tbl[i];
  4410. if (irq->requested)
  4411. free_irq(irq->vector, bp->bnapi[i]);
  4412. irq->requested = 0;
  4413. }
  4414. }
  4415. static int bnxt_request_irq(struct bnxt *bp)
  4416. {
  4417. int i, j, rc = 0;
  4418. unsigned long flags = 0;
  4419. #ifdef CONFIG_RFS_ACCEL
  4420. struct cpu_rmap *rmap = bp->dev->rx_cpu_rmap;
  4421. #endif
  4422. if (!(bp->flags & BNXT_FLAG_USING_MSIX))
  4423. flags = IRQF_SHARED;
  4424. for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
  4425. struct bnxt_irq *irq = &bp->irq_tbl[i];
  4426. #ifdef CONFIG_RFS_ACCEL
  4427. if (rmap && bp->bnapi[i]->rx_ring) {
  4428. rc = irq_cpu_rmap_add(rmap, irq->vector);
  4429. if (rc)
  4430. netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
  4431. j);
  4432. j++;
  4433. }
  4434. #endif
  4435. rc = request_irq(irq->vector, irq->handler, flags, irq->name,
  4436. bp->bnapi[i]);
  4437. if (rc)
  4438. break;
  4439. irq->requested = 1;
  4440. }
  4441. return rc;
  4442. }
  4443. static void bnxt_del_napi(struct bnxt *bp)
  4444. {
  4445. int i;
  4446. if (!bp->bnapi)
  4447. return;
  4448. for (i = 0; i < bp->cp_nr_rings; i++) {
  4449. struct bnxt_napi *bnapi = bp->bnapi[i];
  4450. napi_hash_del(&bnapi->napi);
  4451. netif_napi_del(&bnapi->napi);
  4452. }
  4453. /* We called napi_hash_del() before netif_napi_del(), we need
  4454. * to respect an RCU grace period before freeing napi structures.
  4455. */
  4456. synchronize_net();
  4457. }
  4458. static void bnxt_init_napi(struct bnxt *bp)
  4459. {
  4460. int i;
  4461. unsigned int cp_nr_rings = bp->cp_nr_rings;
  4462. struct bnxt_napi *bnapi;
  4463. if (bp->flags & BNXT_FLAG_USING_MSIX) {
  4464. if (BNXT_CHIP_TYPE_NITRO_A0(bp))
  4465. cp_nr_rings--;
  4466. for (i = 0; i < cp_nr_rings; i++) {
  4467. bnapi = bp->bnapi[i];
  4468. netif_napi_add(bp->dev, &bnapi->napi,
  4469. bnxt_poll, 64);
  4470. }
  4471. if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
  4472. bnapi = bp->bnapi[cp_nr_rings];
  4473. netif_napi_add(bp->dev, &bnapi->napi,
  4474. bnxt_poll_nitroa0, 64);
  4475. }
  4476. } else {
  4477. bnapi = bp->bnapi[0];
  4478. netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll, 64);
  4479. }
  4480. }
  4481. static void bnxt_disable_napi(struct bnxt *bp)
  4482. {
  4483. int i;
  4484. if (!bp->bnapi)
  4485. return;
  4486. for (i = 0; i < bp->cp_nr_rings; i++)
  4487. napi_disable(&bp->bnapi[i]->napi);
  4488. }
  4489. static void bnxt_enable_napi(struct bnxt *bp)
  4490. {
  4491. int i;
  4492. for (i = 0; i < bp->cp_nr_rings; i++) {
  4493. bp->bnapi[i]->in_reset = false;
  4494. napi_enable(&bp->bnapi[i]->napi);
  4495. }
  4496. }
  4497. void bnxt_tx_disable(struct bnxt *bp)
  4498. {
  4499. int i;
  4500. struct bnxt_tx_ring_info *txr;
  4501. struct netdev_queue *txq;
  4502. if (bp->tx_ring) {
  4503. for (i = 0; i < bp->tx_nr_rings; i++) {
  4504. txr = &bp->tx_ring[i];
  4505. txq = netdev_get_tx_queue(bp->dev, i);
  4506. txr->dev_state = BNXT_DEV_STATE_CLOSING;
  4507. }
  4508. }
  4509. /* Stop all TX queues */
  4510. netif_tx_disable(bp->dev);
  4511. netif_carrier_off(bp->dev);
  4512. }
  4513. void bnxt_tx_enable(struct bnxt *bp)
  4514. {
  4515. int i;
  4516. struct bnxt_tx_ring_info *txr;
  4517. struct netdev_queue *txq;
  4518. for (i = 0; i < bp->tx_nr_rings; i++) {
  4519. txr = &bp->tx_ring[i];
  4520. txq = netdev_get_tx_queue(bp->dev, i);
  4521. txr->dev_state = 0;
  4522. }
  4523. netif_tx_wake_all_queues(bp->dev);
  4524. if (bp->link_info.link_up)
  4525. netif_carrier_on(bp->dev);
  4526. }
  4527. static void bnxt_report_link(struct bnxt *bp)
  4528. {
  4529. if (bp->link_info.link_up) {
  4530. const char *duplex;
  4531. const char *flow_ctrl;
  4532. u16 speed, fec;
  4533. netif_carrier_on(bp->dev);
  4534. if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
  4535. duplex = "full";
  4536. else
  4537. duplex = "half";
  4538. if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
  4539. flow_ctrl = "ON - receive & transmit";
  4540. else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
  4541. flow_ctrl = "ON - transmit";
  4542. else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
  4543. flow_ctrl = "ON - receive";
  4544. else
  4545. flow_ctrl = "none";
  4546. speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
  4547. netdev_info(bp->dev, "NIC Link is Up, %d Mbps %s duplex, Flow control: %s\n",
  4548. speed, duplex, flow_ctrl);
  4549. if (bp->flags & BNXT_FLAG_EEE_CAP)
  4550. netdev_info(bp->dev, "EEE is %s\n",
  4551. bp->eee.eee_active ? "active" :
  4552. "not active");
  4553. fec = bp->link_info.fec_cfg;
  4554. if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
  4555. netdev_info(bp->dev, "FEC autoneg %s encodings: %s\n",
  4556. (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
  4557. (fec & BNXT_FEC_ENC_BASE_R) ? "BaseR" :
  4558. (fec & BNXT_FEC_ENC_RS) ? "RS" : "None");
  4559. } else {
  4560. netif_carrier_off(bp->dev);
  4561. netdev_err(bp->dev, "NIC Link is Down\n");
  4562. }
  4563. }
  4564. static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
  4565. {
  4566. int rc = 0;
  4567. struct hwrm_port_phy_qcaps_input req = {0};
  4568. struct hwrm_port_phy_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
  4569. struct bnxt_link_info *link_info = &bp->link_info;
  4570. if (bp->hwrm_spec_code < 0x10201)
  4571. return 0;
  4572. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCAPS, -1, -1);
  4573. mutex_lock(&bp->hwrm_cmd_lock);
  4574. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  4575. if (rc)
  4576. goto hwrm_phy_qcaps_exit;
  4577. if (resp->eee_supported & PORT_PHY_QCAPS_RESP_EEE_SUPPORTED) {
  4578. struct ethtool_eee *eee = &bp->eee;
  4579. u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
  4580. bp->flags |= BNXT_FLAG_EEE_CAP;
  4581. eee->supported = _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
  4582. bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
  4583. PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
  4584. bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
  4585. PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
  4586. }
  4587. if (resp->supported_speeds_auto_mode)
  4588. link_info->support_auto_speeds =
  4589. le16_to_cpu(resp->supported_speeds_auto_mode);
  4590. hwrm_phy_qcaps_exit:
  4591. mutex_unlock(&bp->hwrm_cmd_lock);
  4592. return rc;
  4593. }
  4594. static int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
  4595. {
  4596. int rc = 0;
  4597. struct bnxt_link_info *link_info = &bp->link_info;
  4598. struct hwrm_port_phy_qcfg_input req = {0};
  4599. struct hwrm_port_phy_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
  4600. u8 link_up = link_info->link_up;
  4601. u16 diff;
  4602. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCFG, -1, -1);
  4603. mutex_lock(&bp->hwrm_cmd_lock);
  4604. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  4605. if (rc) {
  4606. mutex_unlock(&bp->hwrm_cmd_lock);
  4607. return rc;
  4608. }
  4609. memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
  4610. link_info->phy_link_status = resp->link;
  4611. link_info->duplex = resp->duplex;
  4612. link_info->pause = resp->pause;
  4613. link_info->auto_mode = resp->auto_mode;
  4614. link_info->auto_pause_setting = resp->auto_pause;
  4615. link_info->lp_pause = resp->link_partner_adv_pause;
  4616. link_info->force_pause_setting = resp->force_pause;
  4617. link_info->duplex_setting = resp->duplex;
  4618. if (link_info->phy_link_status == BNXT_LINK_LINK)
  4619. link_info->link_speed = le16_to_cpu(resp->link_speed);
  4620. else
  4621. link_info->link_speed = 0;
  4622. link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
  4623. link_info->support_speeds = le16_to_cpu(resp->support_speeds);
  4624. link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
  4625. link_info->lp_auto_link_speeds =
  4626. le16_to_cpu(resp->link_partner_adv_speeds);
  4627. link_info->preemphasis = le32_to_cpu(resp->preemphasis);
  4628. link_info->phy_ver[0] = resp->phy_maj;
  4629. link_info->phy_ver[1] = resp->phy_min;
  4630. link_info->phy_ver[2] = resp->phy_bld;
  4631. link_info->media_type = resp->media_type;
  4632. link_info->phy_type = resp->phy_type;
  4633. link_info->transceiver = resp->xcvr_pkg_type;
  4634. link_info->phy_addr = resp->eee_config_phy_addr &
  4635. PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
  4636. link_info->module_status = resp->module_status;
  4637. if (bp->flags & BNXT_FLAG_EEE_CAP) {
  4638. struct ethtool_eee *eee = &bp->eee;
  4639. u16 fw_speeds;
  4640. eee->eee_active = 0;
  4641. if (resp->eee_config_phy_addr &
  4642. PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
  4643. eee->eee_active = 1;
  4644. fw_speeds = le16_to_cpu(
  4645. resp->link_partner_adv_eee_link_speed_mask);
  4646. eee->lp_advertised =
  4647. _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
  4648. }
  4649. /* Pull initial EEE config */
  4650. if (!chng_link_state) {
  4651. if (resp->eee_config_phy_addr &
  4652. PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
  4653. eee->eee_enabled = 1;
  4654. fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
  4655. eee->advertised =
  4656. _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
  4657. if (resp->eee_config_phy_addr &
  4658. PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
  4659. __le32 tmr;
  4660. eee->tx_lpi_enabled = 1;
  4661. tmr = resp->xcvr_identifier_type_tx_lpi_timer;
  4662. eee->tx_lpi_timer = le32_to_cpu(tmr) &
  4663. PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
  4664. }
  4665. }
  4666. }
  4667. link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
  4668. if (bp->hwrm_spec_code >= 0x10504)
  4669. link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
  4670. /* TODO: need to add more logic to report VF link */
  4671. if (chng_link_state) {
  4672. if (link_info->phy_link_status == BNXT_LINK_LINK)
  4673. link_info->link_up = 1;
  4674. else
  4675. link_info->link_up = 0;
  4676. if (link_up != link_info->link_up)
  4677. bnxt_report_link(bp);
  4678. } else {
  4679. /* alwasy link down if not require to update link state */
  4680. link_info->link_up = 0;
  4681. }
  4682. mutex_unlock(&bp->hwrm_cmd_lock);
  4683. diff = link_info->support_auto_speeds ^ link_info->advertising;
  4684. if ((link_info->support_auto_speeds | diff) !=
  4685. link_info->support_auto_speeds) {
  4686. /* An advertised speed is no longer supported, so we need to
  4687. * update the advertisement settings. Caller holds RTNL
  4688. * so we can modify link settings.
  4689. */
  4690. link_info->advertising = link_info->support_auto_speeds;
  4691. if (link_info->autoneg & BNXT_AUTONEG_SPEED)
  4692. bnxt_hwrm_set_link_setting(bp, true, false);
  4693. }
  4694. return 0;
  4695. }
  4696. static void bnxt_get_port_module_status(struct bnxt *bp)
  4697. {
  4698. struct bnxt_link_info *link_info = &bp->link_info;
  4699. struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
  4700. u8 module_status;
  4701. if (bnxt_update_link(bp, true))
  4702. return;
  4703. module_status = link_info->module_status;
  4704. switch (module_status) {
  4705. case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
  4706. case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
  4707. case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
  4708. netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
  4709. bp->pf.port_id);
  4710. if (bp->hwrm_spec_code >= 0x10201) {
  4711. netdev_warn(bp->dev, "Module part number %s\n",
  4712. resp->phy_vendor_partnumber);
  4713. }
  4714. if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
  4715. netdev_warn(bp->dev, "TX is disabled\n");
  4716. if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
  4717. netdev_warn(bp->dev, "SFP+ module is shutdown\n");
  4718. }
  4719. }
  4720. static void
  4721. bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
  4722. {
  4723. if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
  4724. if (bp->hwrm_spec_code >= 0x10201)
  4725. req->auto_pause =
  4726. PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
  4727. if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
  4728. req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
  4729. if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
  4730. req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
  4731. req->enables |=
  4732. cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
  4733. } else {
  4734. if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
  4735. req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
  4736. if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
  4737. req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
  4738. req->enables |=
  4739. cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
  4740. if (bp->hwrm_spec_code >= 0x10201) {
  4741. req->auto_pause = req->force_pause;
  4742. req->enables |= cpu_to_le32(
  4743. PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
  4744. }
  4745. }
  4746. }
  4747. static void bnxt_hwrm_set_link_common(struct bnxt *bp,
  4748. struct hwrm_port_phy_cfg_input *req)
  4749. {
  4750. u8 autoneg = bp->link_info.autoneg;
  4751. u16 fw_link_speed = bp->link_info.req_link_speed;
  4752. u16 advertising = bp->link_info.advertising;
  4753. if (autoneg & BNXT_AUTONEG_SPEED) {
  4754. req->auto_mode |=
  4755. PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
  4756. req->enables |= cpu_to_le32(
  4757. PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
  4758. req->auto_link_speed_mask = cpu_to_le16(advertising);
  4759. req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
  4760. req->flags |=
  4761. cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
  4762. } else {
  4763. req->force_link_speed = cpu_to_le16(fw_link_speed);
  4764. req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
  4765. }
  4766. /* tell chimp that the setting takes effect immediately */
  4767. req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
  4768. }
  4769. int bnxt_hwrm_set_pause(struct bnxt *bp)
  4770. {
  4771. struct hwrm_port_phy_cfg_input req = {0};
  4772. int rc;
  4773. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
  4774. bnxt_hwrm_set_pause_common(bp, &req);
  4775. if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
  4776. bp->link_info.force_link_chng)
  4777. bnxt_hwrm_set_link_common(bp, &req);
  4778. mutex_lock(&bp->hwrm_cmd_lock);
  4779. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  4780. if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
  4781. /* since changing of pause setting doesn't trigger any link
  4782. * change event, the driver needs to update the current pause
  4783. * result upon successfully return of the phy_cfg command
  4784. */
  4785. bp->link_info.pause =
  4786. bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
  4787. bp->link_info.auto_pause_setting = 0;
  4788. if (!bp->link_info.force_link_chng)
  4789. bnxt_report_link(bp);
  4790. }
  4791. bp->link_info.force_link_chng = false;
  4792. mutex_unlock(&bp->hwrm_cmd_lock);
  4793. return rc;
  4794. }
  4795. static void bnxt_hwrm_set_eee(struct bnxt *bp,
  4796. struct hwrm_port_phy_cfg_input *req)
  4797. {
  4798. struct ethtool_eee *eee = &bp->eee;
  4799. if (eee->eee_enabled) {
  4800. u16 eee_speeds;
  4801. u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
  4802. if (eee->tx_lpi_enabled)
  4803. flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
  4804. else
  4805. flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
  4806. req->flags |= cpu_to_le32(flags);
  4807. eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
  4808. req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
  4809. req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
  4810. } else {
  4811. req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
  4812. }
  4813. }
  4814. int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
  4815. {
  4816. struct hwrm_port_phy_cfg_input req = {0};
  4817. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
  4818. if (set_pause)
  4819. bnxt_hwrm_set_pause_common(bp, &req);
  4820. bnxt_hwrm_set_link_common(bp, &req);
  4821. if (set_eee)
  4822. bnxt_hwrm_set_eee(bp, &req);
  4823. return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  4824. }
  4825. static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
  4826. {
  4827. struct hwrm_port_phy_cfg_input req = {0};
  4828. if (!BNXT_SINGLE_PF(bp))
  4829. return 0;
  4830. if (pci_num_vf(bp->pdev))
  4831. return 0;
  4832. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
  4833. req.flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
  4834. return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  4835. }
  4836. static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
  4837. {
  4838. struct hwrm_port_led_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
  4839. struct hwrm_port_led_qcaps_input req = {0};
  4840. struct bnxt_pf_info *pf = &bp->pf;
  4841. int rc;
  4842. if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
  4843. return 0;
  4844. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_LED_QCAPS, -1, -1);
  4845. req.port_id = cpu_to_le16(pf->port_id);
  4846. mutex_lock(&bp->hwrm_cmd_lock);
  4847. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  4848. if (rc) {
  4849. mutex_unlock(&bp->hwrm_cmd_lock);
  4850. return rc;
  4851. }
  4852. if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
  4853. int i;
  4854. bp->num_leds = resp->num_leds;
  4855. memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
  4856. bp->num_leds);
  4857. for (i = 0; i < bp->num_leds; i++) {
  4858. struct bnxt_led_info *led = &bp->leds[i];
  4859. __le16 caps = led->led_state_caps;
  4860. if (!led->led_group_id ||
  4861. !BNXT_LED_ALT_BLINK_CAP(caps)) {
  4862. bp->num_leds = 0;
  4863. break;
  4864. }
  4865. }
  4866. }
  4867. mutex_unlock(&bp->hwrm_cmd_lock);
  4868. return 0;
  4869. }
  4870. static bool bnxt_eee_config_ok(struct bnxt *bp)
  4871. {
  4872. struct ethtool_eee *eee = &bp->eee;
  4873. struct bnxt_link_info *link_info = &bp->link_info;
  4874. if (!(bp->flags & BNXT_FLAG_EEE_CAP))
  4875. return true;
  4876. if (eee->eee_enabled) {
  4877. u32 advertising =
  4878. _bnxt_fw_to_ethtool_adv_spds(link_info->advertising, 0);
  4879. if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
  4880. eee->eee_enabled = 0;
  4881. return false;
  4882. }
  4883. if (eee->advertised & ~advertising) {
  4884. eee->advertised = advertising & eee->supported;
  4885. return false;
  4886. }
  4887. }
  4888. return true;
  4889. }
  4890. static int bnxt_update_phy_setting(struct bnxt *bp)
  4891. {
  4892. int rc;
  4893. bool update_link = false;
  4894. bool update_pause = false;
  4895. bool update_eee = false;
  4896. struct bnxt_link_info *link_info = &bp->link_info;
  4897. rc = bnxt_update_link(bp, true);
  4898. if (rc) {
  4899. netdev_err(bp->dev, "failed to update link (rc: %x)\n",
  4900. rc);
  4901. return rc;
  4902. }
  4903. if (!BNXT_SINGLE_PF(bp))
  4904. return 0;
  4905. if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
  4906. (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
  4907. link_info->req_flow_ctrl)
  4908. update_pause = true;
  4909. if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
  4910. link_info->force_pause_setting != link_info->req_flow_ctrl)
  4911. update_pause = true;
  4912. if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
  4913. if (BNXT_AUTO_MODE(link_info->auto_mode))
  4914. update_link = true;
  4915. if (link_info->req_link_speed != link_info->force_link_speed)
  4916. update_link = true;
  4917. if (link_info->req_duplex != link_info->duplex_setting)
  4918. update_link = true;
  4919. } else {
  4920. if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
  4921. update_link = true;
  4922. if (link_info->advertising != link_info->auto_link_speeds)
  4923. update_link = true;
  4924. }
  4925. /* The last close may have shutdown the link, so need to call
  4926. * PHY_CFG to bring it back up.
  4927. */
  4928. if (!netif_carrier_ok(bp->dev))
  4929. update_link = true;
  4930. if (!bnxt_eee_config_ok(bp))
  4931. update_eee = true;
  4932. if (update_link)
  4933. rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
  4934. else if (update_pause)
  4935. rc = bnxt_hwrm_set_pause(bp);
  4936. if (rc) {
  4937. netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
  4938. rc);
  4939. return rc;
  4940. }
  4941. return rc;
  4942. }
  4943. /* Common routine to pre-map certain register block to different GRC window.
  4944. * A PF has 16 4K windows and a VF has 4 4K windows. However, only 15 windows
  4945. * in PF and 3 windows in VF that can be customized to map in different
  4946. * register blocks.
  4947. */
  4948. static void bnxt_preset_reg_win(struct bnxt *bp)
  4949. {
  4950. if (BNXT_PF(bp)) {
  4951. /* CAG registers map to GRC window #4 */
  4952. writel(BNXT_CAG_REG_BASE,
  4953. bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 12);
  4954. }
  4955. }
  4956. static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
  4957. {
  4958. int rc = 0;
  4959. bnxt_preset_reg_win(bp);
  4960. netif_carrier_off(bp->dev);
  4961. if (irq_re_init) {
  4962. rc = bnxt_setup_int_mode(bp);
  4963. if (rc) {
  4964. netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
  4965. rc);
  4966. return rc;
  4967. }
  4968. }
  4969. if ((bp->flags & BNXT_FLAG_RFS) &&
  4970. !(bp->flags & BNXT_FLAG_USING_MSIX)) {
  4971. /* disable RFS if falling back to INTA */
  4972. bp->dev->hw_features &= ~NETIF_F_NTUPLE;
  4973. bp->flags &= ~BNXT_FLAG_RFS;
  4974. }
  4975. rc = bnxt_alloc_mem(bp, irq_re_init);
  4976. if (rc) {
  4977. netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
  4978. goto open_err_free_mem;
  4979. }
  4980. if (irq_re_init) {
  4981. bnxt_init_napi(bp);
  4982. rc = bnxt_request_irq(bp);
  4983. if (rc) {
  4984. netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
  4985. goto open_err;
  4986. }
  4987. }
  4988. bnxt_enable_napi(bp);
  4989. rc = bnxt_init_nic(bp, irq_re_init);
  4990. if (rc) {
  4991. netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
  4992. goto open_err;
  4993. }
  4994. if (link_re_init) {
  4995. rc = bnxt_update_phy_setting(bp);
  4996. if (rc)
  4997. netdev_warn(bp->dev, "failed to update phy settings\n");
  4998. }
  4999. if (irq_re_init)
  5000. udp_tunnel_get_rx_info(bp->dev);
  5001. set_bit(BNXT_STATE_OPEN, &bp->state);
  5002. bnxt_enable_int(bp);
  5003. /* Enable TX queues */
  5004. bnxt_tx_enable(bp);
  5005. mod_timer(&bp->timer, jiffies + bp->current_interval);
  5006. /* Poll link status and check for SFP+ module status */
  5007. bnxt_get_port_module_status(bp);
  5008. return 0;
  5009. open_err:
  5010. bnxt_disable_napi(bp);
  5011. bnxt_del_napi(bp);
  5012. open_err_free_mem:
  5013. bnxt_free_skbs(bp);
  5014. bnxt_free_irq(bp);
  5015. bnxt_free_mem(bp, true);
  5016. return rc;
  5017. }
  5018. /* rtnl_lock held */
  5019. int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
  5020. {
  5021. int rc = 0;
  5022. rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
  5023. if (rc) {
  5024. netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
  5025. dev_close(bp->dev);
  5026. }
  5027. return rc;
  5028. }
  5029. static int bnxt_open(struct net_device *dev)
  5030. {
  5031. struct bnxt *bp = netdev_priv(dev);
  5032. return __bnxt_open_nic(bp, true, true);
  5033. }
  5034. int bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
  5035. {
  5036. int rc = 0;
  5037. #ifdef CONFIG_BNXT_SRIOV
  5038. if (bp->sriov_cfg) {
  5039. rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
  5040. !bp->sriov_cfg,
  5041. BNXT_SRIOV_CFG_WAIT_TMO);
  5042. if (rc)
  5043. netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete!\n");
  5044. }
  5045. #endif
  5046. /* Change device state to avoid TX queue wake up's */
  5047. bnxt_tx_disable(bp);
  5048. clear_bit(BNXT_STATE_OPEN, &bp->state);
  5049. smp_mb__after_atomic();
  5050. while (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state))
  5051. msleep(20);
  5052. /* Flush rings and and disable interrupts */
  5053. bnxt_shutdown_nic(bp, irq_re_init);
  5054. /* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
  5055. bnxt_disable_napi(bp);
  5056. del_timer_sync(&bp->timer);
  5057. bnxt_free_skbs(bp);
  5058. if (irq_re_init) {
  5059. bnxt_free_irq(bp);
  5060. bnxt_del_napi(bp);
  5061. }
  5062. bnxt_free_mem(bp, irq_re_init);
  5063. return rc;
  5064. }
  5065. static int bnxt_close(struct net_device *dev)
  5066. {
  5067. struct bnxt *bp = netdev_priv(dev);
  5068. bnxt_close_nic(bp, true, true);
  5069. bnxt_hwrm_shutdown_link(bp);
  5070. return 0;
  5071. }
  5072. /* rtnl_lock held */
  5073. static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  5074. {
  5075. switch (cmd) {
  5076. case SIOCGMIIPHY:
  5077. /* fallthru */
  5078. case SIOCGMIIREG: {
  5079. if (!netif_running(dev))
  5080. return -EAGAIN;
  5081. return 0;
  5082. }
  5083. case SIOCSMIIREG:
  5084. if (!netif_running(dev))
  5085. return -EAGAIN;
  5086. return 0;
  5087. default:
  5088. /* do nothing */
  5089. break;
  5090. }
  5091. return -EOPNOTSUPP;
  5092. }
  5093. static void
  5094. bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
  5095. {
  5096. u32 i;
  5097. struct bnxt *bp = netdev_priv(dev);
  5098. if (!bp->bnapi)
  5099. return;
  5100. /* TODO check if we need to synchronize with bnxt_close path */
  5101. for (i = 0; i < bp->cp_nr_rings; i++) {
  5102. struct bnxt_napi *bnapi = bp->bnapi[i];
  5103. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  5104. struct ctx_hw_stats *hw_stats = cpr->hw_stats;
  5105. stats->rx_packets += le64_to_cpu(hw_stats->rx_ucast_pkts);
  5106. stats->rx_packets += le64_to_cpu(hw_stats->rx_mcast_pkts);
  5107. stats->rx_packets += le64_to_cpu(hw_stats->rx_bcast_pkts);
  5108. stats->tx_packets += le64_to_cpu(hw_stats->tx_ucast_pkts);
  5109. stats->tx_packets += le64_to_cpu(hw_stats->tx_mcast_pkts);
  5110. stats->tx_packets += le64_to_cpu(hw_stats->tx_bcast_pkts);
  5111. stats->rx_bytes += le64_to_cpu(hw_stats->rx_ucast_bytes);
  5112. stats->rx_bytes += le64_to_cpu(hw_stats->rx_mcast_bytes);
  5113. stats->rx_bytes += le64_to_cpu(hw_stats->rx_bcast_bytes);
  5114. stats->tx_bytes += le64_to_cpu(hw_stats->tx_ucast_bytes);
  5115. stats->tx_bytes += le64_to_cpu(hw_stats->tx_mcast_bytes);
  5116. stats->tx_bytes += le64_to_cpu(hw_stats->tx_bcast_bytes);
  5117. stats->rx_missed_errors +=
  5118. le64_to_cpu(hw_stats->rx_discard_pkts);
  5119. stats->multicast += le64_to_cpu(hw_stats->rx_mcast_pkts);
  5120. stats->tx_dropped += le64_to_cpu(hw_stats->tx_drop_pkts);
  5121. }
  5122. if (bp->flags & BNXT_FLAG_PORT_STATS) {
  5123. struct rx_port_stats *rx = bp->hw_rx_port_stats;
  5124. struct tx_port_stats *tx = bp->hw_tx_port_stats;
  5125. stats->rx_crc_errors = le64_to_cpu(rx->rx_fcs_err_frames);
  5126. stats->rx_frame_errors = le64_to_cpu(rx->rx_align_err_frames);
  5127. stats->rx_length_errors = le64_to_cpu(rx->rx_undrsz_frames) +
  5128. le64_to_cpu(rx->rx_ovrsz_frames) +
  5129. le64_to_cpu(rx->rx_runt_frames);
  5130. stats->rx_errors = le64_to_cpu(rx->rx_false_carrier_frames) +
  5131. le64_to_cpu(rx->rx_jbr_frames);
  5132. stats->collisions = le64_to_cpu(tx->tx_total_collisions);
  5133. stats->tx_fifo_errors = le64_to_cpu(tx->tx_fifo_underruns);
  5134. stats->tx_errors = le64_to_cpu(tx->tx_err);
  5135. }
  5136. }
  5137. static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
  5138. {
  5139. struct net_device *dev = bp->dev;
  5140. struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
  5141. struct netdev_hw_addr *ha;
  5142. u8 *haddr;
  5143. int mc_count = 0;
  5144. bool update = false;
  5145. int off = 0;
  5146. netdev_for_each_mc_addr(ha, dev) {
  5147. if (mc_count >= BNXT_MAX_MC_ADDRS) {
  5148. *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
  5149. vnic->mc_list_count = 0;
  5150. return false;
  5151. }
  5152. haddr = ha->addr;
  5153. if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
  5154. memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
  5155. update = true;
  5156. }
  5157. off += ETH_ALEN;
  5158. mc_count++;
  5159. }
  5160. if (mc_count)
  5161. *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
  5162. if (mc_count != vnic->mc_list_count) {
  5163. vnic->mc_list_count = mc_count;
  5164. update = true;
  5165. }
  5166. return update;
  5167. }
  5168. static bool bnxt_uc_list_updated(struct bnxt *bp)
  5169. {
  5170. struct net_device *dev = bp->dev;
  5171. struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
  5172. struct netdev_hw_addr *ha;
  5173. int off = 0;
  5174. if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
  5175. return true;
  5176. netdev_for_each_uc_addr(ha, dev) {
  5177. if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
  5178. return true;
  5179. off += ETH_ALEN;
  5180. }
  5181. return false;
  5182. }
  5183. static void bnxt_set_rx_mode(struct net_device *dev)
  5184. {
  5185. struct bnxt *bp = netdev_priv(dev);
  5186. struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
  5187. u32 mask = vnic->rx_mask;
  5188. bool mc_update = false;
  5189. bool uc_update;
  5190. if (!netif_running(dev))
  5191. return;
  5192. mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
  5193. CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
  5194. CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST);
  5195. if ((dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
  5196. mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
  5197. uc_update = bnxt_uc_list_updated(bp);
  5198. if (dev->flags & IFF_ALLMULTI) {
  5199. mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
  5200. vnic->mc_list_count = 0;
  5201. } else {
  5202. mc_update = bnxt_mc_list_updated(bp, &mask);
  5203. }
  5204. if (mask != vnic->rx_mask || uc_update || mc_update) {
  5205. vnic->rx_mask = mask;
  5206. set_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event);
  5207. schedule_work(&bp->sp_task);
  5208. }
  5209. }
  5210. static int bnxt_cfg_rx_mode(struct bnxt *bp)
  5211. {
  5212. struct net_device *dev = bp->dev;
  5213. struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
  5214. struct netdev_hw_addr *ha;
  5215. int i, off = 0, rc;
  5216. bool uc_update;
  5217. netif_addr_lock_bh(dev);
  5218. uc_update = bnxt_uc_list_updated(bp);
  5219. netif_addr_unlock_bh(dev);
  5220. if (!uc_update)
  5221. goto skip_uc;
  5222. mutex_lock(&bp->hwrm_cmd_lock);
  5223. for (i = 1; i < vnic->uc_filter_count; i++) {
  5224. struct hwrm_cfa_l2_filter_free_input req = {0};
  5225. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_FREE, -1,
  5226. -1);
  5227. req.l2_filter_id = vnic->fw_l2_filter_id[i];
  5228. rc = _hwrm_send_message(bp, &req, sizeof(req),
  5229. HWRM_CMD_TIMEOUT);
  5230. }
  5231. mutex_unlock(&bp->hwrm_cmd_lock);
  5232. vnic->uc_filter_count = 1;
  5233. netif_addr_lock_bh(dev);
  5234. if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
  5235. vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
  5236. } else {
  5237. netdev_for_each_uc_addr(ha, dev) {
  5238. memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
  5239. off += ETH_ALEN;
  5240. vnic->uc_filter_count++;
  5241. }
  5242. }
  5243. netif_addr_unlock_bh(dev);
  5244. for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
  5245. rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
  5246. if (rc) {
  5247. netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n",
  5248. rc);
  5249. vnic->uc_filter_count = i;
  5250. return rc;
  5251. }
  5252. }
  5253. skip_uc:
  5254. rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
  5255. if (rc)
  5256. netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %x\n",
  5257. rc);
  5258. return rc;
  5259. }
  5260. /* If the chip and firmware supports RFS */
  5261. static bool bnxt_rfs_supported(struct bnxt *bp)
  5262. {
  5263. if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
  5264. return true;
  5265. if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
  5266. return true;
  5267. return false;
  5268. }
  5269. /* If runtime conditions support RFS */
  5270. static bool bnxt_rfs_capable(struct bnxt *bp)
  5271. {
  5272. #ifdef CONFIG_RFS_ACCEL
  5273. int vnics, max_vnics, max_rss_ctxs;
  5274. if (!(bp->flags & BNXT_FLAG_MSIX_CAP))
  5275. return false;
  5276. vnics = 1 + bp->rx_nr_rings;
  5277. max_vnics = bnxt_get_max_func_vnics(bp);
  5278. max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
  5279. /* RSS contexts not a limiting factor */
  5280. if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
  5281. max_rss_ctxs = max_vnics;
  5282. if (vnics > max_vnics || vnics > max_rss_ctxs) {
  5283. netdev_warn(bp->dev,
  5284. "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
  5285. min(max_rss_ctxs - 1, max_vnics - 1));
  5286. return false;
  5287. }
  5288. return true;
  5289. #else
  5290. return false;
  5291. #endif
  5292. }
  5293. static netdev_features_t bnxt_fix_features(struct net_device *dev,
  5294. netdev_features_t features)
  5295. {
  5296. struct bnxt *bp = netdev_priv(dev);
  5297. if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp))
  5298. features &= ~NETIF_F_NTUPLE;
  5299. /* Both CTAG and STAG VLAN accelaration on the RX side have to be
  5300. * turned on or off together.
  5301. */
  5302. if ((features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX)) !=
  5303. (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX)) {
  5304. if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
  5305. features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
  5306. NETIF_F_HW_VLAN_STAG_RX);
  5307. else
  5308. features |= NETIF_F_HW_VLAN_CTAG_RX |
  5309. NETIF_F_HW_VLAN_STAG_RX;
  5310. }
  5311. #ifdef CONFIG_BNXT_SRIOV
  5312. if (BNXT_VF(bp)) {
  5313. if (bp->vf.vlan) {
  5314. features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
  5315. NETIF_F_HW_VLAN_STAG_RX);
  5316. }
  5317. }
  5318. #endif
  5319. return features;
  5320. }
  5321. static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
  5322. {
  5323. struct bnxt *bp = netdev_priv(dev);
  5324. u32 flags = bp->flags;
  5325. u32 changes;
  5326. int rc = 0;
  5327. bool re_init = false;
  5328. bool update_tpa = false;
  5329. flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
  5330. if ((features & NETIF_F_GRO) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
  5331. flags |= BNXT_FLAG_GRO;
  5332. if (features & NETIF_F_LRO)
  5333. flags |= BNXT_FLAG_LRO;
  5334. if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
  5335. flags &= ~BNXT_FLAG_TPA;
  5336. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  5337. flags |= BNXT_FLAG_STRIP_VLAN;
  5338. if (features & NETIF_F_NTUPLE)
  5339. flags |= BNXT_FLAG_RFS;
  5340. changes = flags ^ bp->flags;
  5341. if (changes & BNXT_FLAG_TPA) {
  5342. update_tpa = true;
  5343. if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
  5344. (flags & BNXT_FLAG_TPA) == 0)
  5345. re_init = true;
  5346. }
  5347. if (changes & ~BNXT_FLAG_TPA)
  5348. re_init = true;
  5349. if (flags != bp->flags) {
  5350. u32 old_flags = bp->flags;
  5351. bp->flags = flags;
  5352. if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
  5353. if (update_tpa)
  5354. bnxt_set_ring_params(bp);
  5355. return rc;
  5356. }
  5357. if (re_init) {
  5358. bnxt_close_nic(bp, false, false);
  5359. if (update_tpa)
  5360. bnxt_set_ring_params(bp);
  5361. return bnxt_open_nic(bp, false, false);
  5362. }
  5363. if (update_tpa) {
  5364. rc = bnxt_set_tpa(bp,
  5365. (flags & BNXT_FLAG_TPA) ?
  5366. true : false);
  5367. if (rc)
  5368. bp->flags = old_flags;
  5369. }
  5370. }
  5371. return rc;
  5372. }
  5373. static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
  5374. {
  5375. struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
  5376. int i = bnapi->index;
  5377. if (!txr)
  5378. return;
  5379. netdev_info(bnapi->bp->dev, "[%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
  5380. i, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
  5381. txr->tx_cons);
  5382. }
  5383. static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
  5384. {
  5385. struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
  5386. int i = bnapi->index;
  5387. if (!rxr)
  5388. return;
  5389. 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",
  5390. i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
  5391. rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
  5392. rxr->rx_sw_agg_prod);
  5393. }
  5394. static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
  5395. {
  5396. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  5397. int i = bnapi->index;
  5398. netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
  5399. i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
  5400. }
  5401. static void bnxt_dbg_dump_states(struct bnxt *bp)
  5402. {
  5403. int i;
  5404. struct bnxt_napi *bnapi;
  5405. for (i = 0; i < bp->cp_nr_rings; i++) {
  5406. bnapi = bp->bnapi[i];
  5407. if (netif_msg_drv(bp)) {
  5408. bnxt_dump_tx_sw_state(bnapi);
  5409. bnxt_dump_rx_sw_state(bnapi);
  5410. bnxt_dump_cp_sw_state(bnapi);
  5411. }
  5412. }
  5413. }
  5414. static void bnxt_reset_task(struct bnxt *bp, bool silent)
  5415. {
  5416. if (!silent)
  5417. bnxt_dbg_dump_states(bp);
  5418. if (netif_running(bp->dev)) {
  5419. int rc;
  5420. if (!silent)
  5421. bnxt_ulp_stop(bp);
  5422. bnxt_close_nic(bp, false, false);
  5423. rc = bnxt_open_nic(bp, false, false);
  5424. if (!silent && !rc)
  5425. bnxt_ulp_start(bp);
  5426. }
  5427. }
  5428. static void bnxt_tx_timeout(struct net_device *dev)
  5429. {
  5430. struct bnxt *bp = netdev_priv(dev);
  5431. netdev_err(bp->dev, "TX timeout detected, starting reset task!\n");
  5432. set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
  5433. schedule_work(&bp->sp_task);
  5434. }
  5435. #ifdef CONFIG_NET_POLL_CONTROLLER
  5436. static void bnxt_poll_controller(struct net_device *dev)
  5437. {
  5438. struct bnxt *bp = netdev_priv(dev);
  5439. int i;
  5440. for (i = 0; i < bp->cp_nr_rings; i++) {
  5441. struct bnxt_irq *irq = &bp->irq_tbl[i];
  5442. disable_irq(irq->vector);
  5443. irq->handler(irq->vector, bp->bnapi[i]);
  5444. enable_irq(irq->vector);
  5445. }
  5446. }
  5447. #endif
  5448. static void bnxt_timer(unsigned long data)
  5449. {
  5450. struct bnxt *bp = (struct bnxt *)data;
  5451. struct net_device *dev = bp->dev;
  5452. if (!netif_running(dev))
  5453. return;
  5454. if (atomic_read(&bp->intr_sem) != 0)
  5455. goto bnxt_restart_timer;
  5456. if (bp->link_info.link_up && (bp->flags & BNXT_FLAG_PORT_STATS)) {
  5457. set_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event);
  5458. schedule_work(&bp->sp_task);
  5459. }
  5460. bnxt_restart_timer:
  5461. mod_timer(&bp->timer, jiffies + bp->current_interval);
  5462. }
  5463. static void bnxt_rtnl_lock_sp(struct bnxt *bp)
  5464. {
  5465. /* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
  5466. * set. If the device is being closed, bnxt_close() may be holding
  5467. * rtnl() and waiting for BNXT_STATE_IN_SP_TASK to clear. So we
  5468. * must clear BNXT_STATE_IN_SP_TASK before holding rtnl().
  5469. */
  5470. clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
  5471. rtnl_lock();
  5472. }
  5473. static void bnxt_rtnl_unlock_sp(struct bnxt *bp)
  5474. {
  5475. set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
  5476. rtnl_unlock();
  5477. }
  5478. /* Only called from bnxt_sp_task() */
  5479. static void bnxt_reset(struct bnxt *bp, bool silent)
  5480. {
  5481. bnxt_rtnl_lock_sp(bp);
  5482. if (test_bit(BNXT_STATE_OPEN, &bp->state))
  5483. bnxt_reset_task(bp, silent);
  5484. bnxt_rtnl_unlock_sp(bp);
  5485. }
  5486. static void bnxt_cfg_ntp_filters(struct bnxt *);
  5487. static void bnxt_sp_task(struct work_struct *work)
  5488. {
  5489. struct bnxt *bp = container_of(work, struct bnxt, sp_task);
  5490. set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
  5491. smp_mb__after_atomic();
  5492. if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
  5493. clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
  5494. return;
  5495. }
  5496. if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
  5497. bnxt_cfg_rx_mode(bp);
  5498. if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
  5499. bnxt_cfg_ntp_filters(bp);
  5500. if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
  5501. bnxt_hwrm_exec_fwd_req(bp);
  5502. if (test_and_clear_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event)) {
  5503. bnxt_hwrm_tunnel_dst_port_alloc(
  5504. bp, bp->vxlan_port,
  5505. TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
  5506. }
  5507. if (test_and_clear_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event)) {
  5508. bnxt_hwrm_tunnel_dst_port_free(
  5509. bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
  5510. }
  5511. if (test_and_clear_bit(BNXT_GENEVE_ADD_PORT_SP_EVENT, &bp->sp_event)) {
  5512. bnxt_hwrm_tunnel_dst_port_alloc(
  5513. bp, bp->nge_port,
  5514. TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
  5515. }
  5516. if (test_and_clear_bit(BNXT_GENEVE_DEL_PORT_SP_EVENT, &bp->sp_event)) {
  5517. bnxt_hwrm_tunnel_dst_port_free(
  5518. bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
  5519. }
  5520. if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event))
  5521. bnxt_hwrm_port_qstats(bp);
  5522. /* These functions below will clear BNXT_STATE_IN_SP_TASK. They
  5523. * must be the last functions to be called before exiting.
  5524. */
  5525. if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
  5526. int rc = 0;
  5527. if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
  5528. &bp->sp_event))
  5529. bnxt_hwrm_phy_qcaps(bp);
  5530. bnxt_rtnl_lock_sp(bp);
  5531. if (test_bit(BNXT_STATE_OPEN, &bp->state))
  5532. rc = bnxt_update_link(bp, true);
  5533. bnxt_rtnl_unlock_sp(bp);
  5534. if (rc)
  5535. netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
  5536. rc);
  5537. }
  5538. if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
  5539. bnxt_rtnl_lock_sp(bp);
  5540. if (test_bit(BNXT_STATE_OPEN, &bp->state))
  5541. bnxt_get_port_module_status(bp);
  5542. bnxt_rtnl_unlock_sp(bp);
  5543. }
  5544. if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
  5545. bnxt_reset(bp, false);
  5546. if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
  5547. bnxt_reset(bp, true);
  5548. smp_mb__before_atomic();
  5549. clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
  5550. }
  5551. /* Under rtnl_lock */
  5552. int bnxt_reserve_rings(struct bnxt *bp, int tx, int rx, int tcs, int tx_xdp)
  5553. {
  5554. int max_rx, max_tx, tx_sets = 1;
  5555. int tx_rings_needed;
  5556. bool sh = true;
  5557. int rc;
  5558. if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
  5559. sh = false;
  5560. if (tcs)
  5561. tx_sets = tcs;
  5562. rc = bnxt_get_max_rings(bp, &max_rx, &max_tx, sh);
  5563. if (rc)
  5564. return rc;
  5565. if (max_rx < rx)
  5566. return -ENOMEM;
  5567. tx_rings_needed = tx * tx_sets + tx_xdp;
  5568. if (max_tx < tx_rings_needed)
  5569. return -ENOMEM;
  5570. if (bnxt_hwrm_reserve_tx_rings(bp, &tx_rings_needed) ||
  5571. tx_rings_needed < (tx * tx_sets + tx_xdp))
  5572. return -ENOMEM;
  5573. return 0;
  5574. }
  5575. static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
  5576. {
  5577. if (bp->bar2) {
  5578. pci_iounmap(pdev, bp->bar2);
  5579. bp->bar2 = NULL;
  5580. }
  5581. if (bp->bar1) {
  5582. pci_iounmap(pdev, bp->bar1);
  5583. bp->bar1 = NULL;
  5584. }
  5585. if (bp->bar0) {
  5586. pci_iounmap(pdev, bp->bar0);
  5587. bp->bar0 = NULL;
  5588. }
  5589. }
  5590. static void bnxt_cleanup_pci(struct bnxt *bp)
  5591. {
  5592. bnxt_unmap_bars(bp, bp->pdev);
  5593. pci_release_regions(bp->pdev);
  5594. pci_disable_device(bp->pdev);
  5595. }
  5596. static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
  5597. {
  5598. int rc;
  5599. struct bnxt *bp = netdev_priv(dev);
  5600. SET_NETDEV_DEV(dev, &pdev->dev);
  5601. /* enable device (incl. PCI PM wakeup), and bus-mastering */
  5602. rc = pci_enable_device(pdev);
  5603. if (rc) {
  5604. dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
  5605. goto init_err;
  5606. }
  5607. if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
  5608. dev_err(&pdev->dev,
  5609. "Cannot find PCI device base address, aborting\n");
  5610. rc = -ENODEV;
  5611. goto init_err_disable;
  5612. }
  5613. rc = pci_request_regions(pdev, DRV_MODULE_NAME);
  5614. if (rc) {
  5615. dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
  5616. goto init_err_disable;
  5617. }
  5618. if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
  5619. dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
  5620. dev_err(&pdev->dev, "System does not support DMA, aborting\n");
  5621. goto init_err_disable;
  5622. }
  5623. pci_set_master(pdev);
  5624. bp->dev = dev;
  5625. bp->pdev = pdev;
  5626. bp->bar0 = pci_ioremap_bar(pdev, 0);
  5627. if (!bp->bar0) {
  5628. dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
  5629. rc = -ENOMEM;
  5630. goto init_err_release;
  5631. }
  5632. bp->bar1 = pci_ioremap_bar(pdev, 2);
  5633. if (!bp->bar1) {
  5634. dev_err(&pdev->dev, "Cannot map doorbell registers, aborting\n");
  5635. rc = -ENOMEM;
  5636. goto init_err_release;
  5637. }
  5638. bp->bar2 = pci_ioremap_bar(pdev, 4);
  5639. if (!bp->bar2) {
  5640. dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
  5641. rc = -ENOMEM;
  5642. goto init_err_release;
  5643. }
  5644. pci_enable_pcie_error_reporting(pdev);
  5645. INIT_WORK(&bp->sp_task, bnxt_sp_task);
  5646. spin_lock_init(&bp->ntp_fltr_lock);
  5647. bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
  5648. bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
  5649. /* tick values in micro seconds */
  5650. bp->rx_coal_ticks = 12;
  5651. bp->rx_coal_bufs = 30;
  5652. bp->rx_coal_ticks_irq = 1;
  5653. bp->rx_coal_bufs_irq = 2;
  5654. bp->tx_coal_ticks = 25;
  5655. bp->tx_coal_bufs = 30;
  5656. bp->tx_coal_ticks_irq = 2;
  5657. bp->tx_coal_bufs_irq = 2;
  5658. bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
  5659. init_timer(&bp->timer);
  5660. bp->timer.data = (unsigned long)bp;
  5661. bp->timer.function = bnxt_timer;
  5662. bp->current_interval = BNXT_TIMER_INTERVAL;
  5663. clear_bit(BNXT_STATE_OPEN, &bp->state);
  5664. return 0;
  5665. init_err_release:
  5666. bnxt_unmap_bars(bp, pdev);
  5667. pci_release_regions(pdev);
  5668. init_err_disable:
  5669. pci_disable_device(pdev);
  5670. init_err:
  5671. return rc;
  5672. }
  5673. /* rtnl_lock held */
  5674. static int bnxt_change_mac_addr(struct net_device *dev, void *p)
  5675. {
  5676. struct sockaddr *addr = p;
  5677. struct bnxt *bp = netdev_priv(dev);
  5678. int rc = 0;
  5679. if (!is_valid_ether_addr(addr->sa_data))
  5680. return -EADDRNOTAVAIL;
  5681. rc = bnxt_approve_mac(bp, addr->sa_data);
  5682. if (rc)
  5683. return rc;
  5684. if (ether_addr_equal(addr->sa_data, dev->dev_addr))
  5685. return 0;
  5686. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  5687. if (netif_running(dev)) {
  5688. bnxt_close_nic(bp, false, false);
  5689. rc = bnxt_open_nic(bp, false, false);
  5690. }
  5691. return rc;
  5692. }
  5693. /* rtnl_lock held */
  5694. static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
  5695. {
  5696. struct bnxt *bp = netdev_priv(dev);
  5697. if (netif_running(dev))
  5698. bnxt_close_nic(bp, false, false);
  5699. dev->mtu = new_mtu;
  5700. bnxt_set_ring_params(bp);
  5701. if (netif_running(dev))
  5702. return bnxt_open_nic(bp, false, false);
  5703. return 0;
  5704. }
  5705. int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
  5706. {
  5707. struct bnxt *bp = netdev_priv(dev);
  5708. bool sh = false;
  5709. int rc;
  5710. if (tc > bp->max_tc) {
  5711. netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
  5712. tc, bp->max_tc);
  5713. return -EINVAL;
  5714. }
  5715. if (netdev_get_num_tc(dev) == tc)
  5716. return 0;
  5717. if (bp->flags & BNXT_FLAG_SHARED_RINGS)
  5718. sh = true;
  5719. rc = bnxt_reserve_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
  5720. tc, bp->tx_nr_rings_xdp);
  5721. if (rc)
  5722. return rc;
  5723. /* Needs to close the device and do hw resource re-allocations */
  5724. if (netif_running(bp->dev))
  5725. bnxt_close_nic(bp, true, false);
  5726. if (tc) {
  5727. bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
  5728. netdev_set_num_tc(dev, tc);
  5729. } else {
  5730. bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
  5731. netdev_reset_tc(dev);
  5732. }
  5733. bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
  5734. bp->tx_nr_rings + bp->rx_nr_rings;
  5735. bp->num_stat_ctxs = bp->cp_nr_rings;
  5736. if (netif_running(bp->dev))
  5737. return bnxt_open_nic(bp, true, false);
  5738. return 0;
  5739. }
  5740. static int bnxt_setup_tc(struct net_device *dev, u32 handle, __be16 proto,
  5741. struct tc_to_netdev *ntc)
  5742. {
  5743. if (ntc->type != TC_SETUP_MQPRIO)
  5744. return -EINVAL;
  5745. return bnxt_setup_mq_tc(dev, ntc->tc);
  5746. }
  5747. #ifdef CONFIG_RFS_ACCEL
  5748. static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
  5749. struct bnxt_ntuple_filter *f2)
  5750. {
  5751. struct flow_keys *keys1 = &f1->fkeys;
  5752. struct flow_keys *keys2 = &f2->fkeys;
  5753. if (keys1->addrs.v4addrs.src == keys2->addrs.v4addrs.src &&
  5754. keys1->addrs.v4addrs.dst == keys2->addrs.v4addrs.dst &&
  5755. keys1->ports.ports == keys2->ports.ports &&
  5756. keys1->basic.ip_proto == keys2->basic.ip_proto &&
  5757. keys1->basic.n_proto == keys2->basic.n_proto &&
  5758. keys1->control.flags == keys2->control.flags &&
  5759. ether_addr_equal(f1->src_mac_addr, f2->src_mac_addr) &&
  5760. ether_addr_equal(f1->dst_mac_addr, f2->dst_mac_addr))
  5761. return true;
  5762. return false;
  5763. }
  5764. static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
  5765. u16 rxq_index, u32 flow_id)
  5766. {
  5767. struct bnxt *bp = netdev_priv(dev);
  5768. struct bnxt_ntuple_filter *fltr, *new_fltr;
  5769. struct flow_keys *fkeys;
  5770. struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
  5771. int rc = 0, idx, bit_id, l2_idx = 0;
  5772. struct hlist_head *head;
  5773. if (!ether_addr_equal(dev->dev_addr, eth->h_dest)) {
  5774. struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
  5775. int off = 0, j;
  5776. netif_addr_lock_bh(dev);
  5777. for (j = 0; j < vnic->uc_filter_count; j++, off += ETH_ALEN) {
  5778. if (ether_addr_equal(eth->h_dest,
  5779. vnic->uc_list + off)) {
  5780. l2_idx = j + 1;
  5781. break;
  5782. }
  5783. }
  5784. netif_addr_unlock_bh(dev);
  5785. if (!l2_idx)
  5786. return -EINVAL;
  5787. }
  5788. new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
  5789. if (!new_fltr)
  5790. return -ENOMEM;
  5791. fkeys = &new_fltr->fkeys;
  5792. if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
  5793. rc = -EPROTONOSUPPORT;
  5794. goto err_free;
  5795. }
  5796. if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
  5797. fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
  5798. ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
  5799. (fkeys->basic.ip_proto != IPPROTO_UDP))) {
  5800. rc = -EPROTONOSUPPORT;
  5801. goto err_free;
  5802. }
  5803. if (fkeys->basic.n_proto == htons(ETH_P_IPV6) &&
  5804. bp->hwrm_spec_code < 0x10601) {
  5805. rc = -EPROTONOSUPPORT;
  5806. goto err_free;
  5807. }
  5808. if ((fkeys->control.flags & FLOW_DIS_ENCAPSULATION) &&
  5809. bp->hwrm_spec_code < 0x10601) {
  5810. rc = -EPROTONOSUPPORT;
  5811. goto err_free;
  5812. }
  5813. memcpy(new_fltr->dst_mac_addr, eth->h_dest, ETH_ALEN);
  5814. memcpy(new_fltr->src_mac_addr, eth->h_source, ETH_ALEN);
  5815. idx = skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
  5816. head = &bp->ntp_fltr_hash_tbl[idx];
  5817. rcu_read_lock();
  5818. hlist_for_each_entry_rcu(fltr, head, hash) {
  5819. if (bnxt_fltr_match(fltr, new_fltr)) {
  5820. rcu_read_unlock();
  5821. rc = 0;
  5822. goto err_free;
  5823. }
  5824. }
  5825. rcu_read_unlock();
  5826. spin_lock_bh(&bp->ntp_fltr_lock);
  5827. bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
  5828. BNXT_NTP_FLTR_MAX_FLTR, 0);
  5829. if (bit_id < 0) {
  5830. spin_unlock_bh(&bp->ntp_fltr_lock);
  5831. rc = -ENOMEM;
  5832. goto err_free;
  5833. }
  5834. new_fltr->sw_id = (u16)bit_id;
  5835. new_fltr->flow_id = flow_id;
  5836. new_fltr->l2_fltr_idx = l2_idx;
  5837. new_fltr->rxq = rxq_index;
  5838. hlist_add_head_rcu(&new_fltr->hash, head);
  5839. bp->ntp_fltr_count++;
  5840. spin_unlock_bh(&bp->ntp_fltr_lock);
  5841. set_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event);
  5842. schedule_work(&bp->sp_task);
  5843. return new_fltr->sw_id;
  5844. err_free:
  5845. kfree(new_fltr);
  5846. return rc;
  5847. }
  5848. static void bnxt_cfg_ntp_filters(struct bnxt *bp)
  5849. {
  5850. int i;
  5851. for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
  5852. struct hlist_head *head;
  5853. struct hlist_node *tmp;
  5854. struct bnxt_ntuple_filter *fltr;
  5855. int rc;
  5856. head = &bp->ntp_fltr_hash_tbl[i];
  5857. hlist_for_each_entry_safe(fltr, tmp, head, hash) {
  5858. bool del = false;
  5859. if (test_bit(BNXT_FLTR_VALID, &fltr->state)) {
  5860. if (rps_may_expire_flow(bp->dev, fltr->rxq,
  5861. fltr->flow_id,
  5862. fltr->sw_id)) {
  5863. bnxt_hwrm_cfa_ntuple_filter_free(bp,
  5864. fltr);
  5865. del = true;
  5866. }
  5867. } else {
  5868. rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
  5869. fltr);
  5870. if (rc)
  5871. del = true;
  5872. else
  5873. set_bit(BNXT_FLTR_VALID, &fltr->state);
  5874. }
  5875. if (del) {
  5876. spin_lock_bh(&bp->ntp_fltr_lock);
  5877. hlist_del_rcu(&fltr->hash);
  5878. bp->ntp_fltr_count--;
  5879. spin_unlock_bh(&bp->ntp_fltr_lock);
  5880. synchronize_rcu();
  5881. clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
  5882. kfree(fltr);
  5883. }
  5884. }
  5885. }
  5886. if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
  5887. netdev_info(bp->dev, "Receive PF driver unload event!");
  5888. }
  5889. #else
  5890. static void bnxt_cfg_ntp_filters(struct bnxt *bp)
  5891. {
  5892. }
  5893. #endif /* CONFIG_RFS_ACCEL */
  5894. static void bnxt_udp_tunnel_add(struct net_device *dev,
  5895. struct udp_tunnel_info *ti)
  5896. {
  5897. struct bnxt *bp = netdev_priv(dev);
  5898. if (ti->sa_family != AF_INET6 && ti->sa_family != AF_INET)
  5899. return;
  5900. if (!netif_running(dev))
  5901. return;
  5902. switch (ti->type) {
  5903. case UDP_TUNNEL_TYPE_VXLAN:
  5904. if (bp->vxlan_port_cnt && bp->vxlan_port != ti->port)
  5905. return;
  5906. bp->vxlan_port_cnt++;
  5907. if (bp->vxlan_port_cnt == 1) {
  5908. bp->vxlan_port = ti->port;
  5909. set_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event);
  5910. schedule_work(&bp->sp_task);
  5911. }
  5912. break;
  5913. case UDP_TUNNEL_TYPE_GENEVE:
  5914. if (bp->nge_port_cnt && bp->nge_port != ti->port)
  5915. return;
  5916. bp->nge_port_cnt++;
  5917. if (bp->nge_port_cnt == 1) {
  5918. bp->nge_port = ti->port;
  5919. set_bit(BNXT_GENEVE_ADD_PORT_SP_EVENT, &bp->sp_event);
  5920. }
  5921. break;
  5922. default:
  5923. return;
  5924. }
  5925. schedule_work(&bp->sp_task);
  5926. }
  5927. static void bnxt_udp_tunnel_del(struct net_device *dev,
  5928. struct udp_tunnel_info *ti)
  5929. {
  5930. struct bnxt *bp = netdev_priv(dev);
  5931. if (ti->sa_family != AF_INET6 && ti->sa_family != AF_INET)
  5932. return;
  5933. if (!netif_running(dev))
  5934. return;
  5935. switch (ti->type) {
  5936. case UDP_TUNNEL_TYPE_VXLAN:
  5937. if (!bp->vxlan_port_cnt || bp->vxlan_port != ti->port)
  5938. return;
  5939. bp->vxlan_port_cnt--;
  5940. if (bp->vxlan_port_cnt != 0)
  5941. return;
  5942. set_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event);
  5943. break;
  5944. case UDP_TUNNEL_TYPE_GENEVE:
  5945. if (!bp->nge_port_cnt || bp->nge_port != ti->port)
  5946. return;
  5947. bp->nge_port_cnt--;
  5948. if (bp->nge_port_cnt != 0)
  5949. return;
  5950. set_bit(BNXT_GENEVE_DEL_PORT_SP_EVENT, &bp->sp_event);
  5951. break;
  5952. default:
  5953. return;
  5954. }
  5955. schedule_work(&bp->sp_task);
  5956. }
  5957. static const struct net_device_ops bnxt_netdev_ops = {
  5958. .ndo_open = bnxt_open,
  5959. .ndo_start_xmit = bnxt_start_xmit,
  5960. .ndo_stop = bnxt_close,
  5961. .ndo_get_stats64 = bnxt_get_stats64,
  5962. .ndo_set_rx_mode = bnxt_set_rx_mode,
  5963. .ndo_do_ioctl = bnxt_ioctl,
  5964. .ndo_validate_addr = eth_validate_addr,
  5965. .ndo_set_mac_address = bnxt_change_mac_addr,
  5966. .ndo_change_mtu = bnxt_change_mtu,
  5967. .ndo_fix_features = bnxt_fix_features,
  5968. .ndo_set_features = bnxt_set_features,
  5969. .ndo_tx_timeout = bnxt_tx_timeout,
  5970. #ifdef CONFIG_BNXT_SRIOV
  5971. .ndo_get_vf_config = bnxt_get_vf_config,
  5972. .ndo_set_vf_mac = bnxt_set_vf_mac,
  5973. .ndo_set_vf_vlan = bnxt_set_vf_vlan,
  5974. .ndo_set_vf_rate = bnxt_set_vf_bw,
  5975. .ndo_set_vf_link_state = bnxt_set_vf_link_state,
  5976. .ndo_set_vf_spoofchk = bnxt_set_vf_spoofchk,
  5977. #endif
  5978. #ifdef CONFIG_NET_POLL_CONTROLLER
  5979. .ndo_poll_controller = bnxt_poll_controller,
  5980. #endif
  5981. .ndo_setup_tc = bnxt_setup_tc,
  5982. #ifdef CONFIG_RFS_ACCEL
  5983. .ndo_rx_flow_steer = bnxt_rx_flow_steer,
  5984. #endif
  5985. .ndo_udp_tunnel_add = bnxt_udp_tunnel_add,
  5986. .ndo_udp_tunnel_del = bnxt_udp_tunnel_del,
  5987. .ndo_xdp = bnxt_xdp,
  5988. };
  5989. static void bnxt_remove_one(struct pci_dev *pdev)
  5990. {
  5991. struct net_device *dev = pci_get_drvdata(pdev);
  5992. struct bnxt *bp = netdev_priv(dev);
  5993. if (BNXT_PF(bp))
  5994. bnxt_sriov_disable(bp);
  5995. pci_disable_pcie_error_reporting(pdev);
  5996. unregister_netdev(dev);
  5997. cancel_work_sync(&bp->sp_task);
  5998. bp->sp_event = 0;
  5999. bnxt_clear_int_mode(bp);
  6000. bnxt_hwrm_func_drv_unrgtr(bp);
  6001. bnxt_free_hwrm_resources(bp);
  6002. bnxt_dcb_free(bp);
  6003. kfree(bp->edev);
  6004. bp->edev = NULL;
  6005. if (bp->xdp_prog)
  6006. bpf_prog_put(bp->xdp_prog);
  6007. bnxt_cleanup_pci(bp);
  6008. free_netdev(dev);
  6009. }
  6010. static int bnxt_probe_phy(struct bnxt *bp)
  6011. {
  6012. int rc = 0;
  6013. struct bnxt_link_info *link_info = &bp->link_info;
  6014. rc = bnxt_hwrm_phy_qcaps(bp);
  6015. if (rc) {
  6016. netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
  6017. rc);
  6018. return rc;
  6019. }
  6020. rc = bnxt_update_link(bp, false);
  6021. if (rc) {
  6022. netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
  6023. rc);
  6024. return rc;
  6025. }
  6026. /* Older firmware does not have supported_auto_speeds, so assume
  6027. * that all supported speeds can be autonegotiated.
  6028. */
  6029. if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
  6030. link_info->support_auto_speeds = link_info->support_speeds;
  6031. /*initialize the ethool setting copy with NVM settings */
  6032. if (BNXT_AUTO_MODE(link_info->auto_mode)) {
  6033. link_info->autoneg = BNXT_AUTONEG_SPEED;
  6034. if (bp->hwrm_spec_code >= 0x10201) {
  6035. if (link_info->auto_pause_setting &
  6036. PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
  6037. link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
  6038. } else {
  6039. link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
  6040. }
  6041. link_info->advertising = link_info->auto_link_speeds;
  6042. } else {
  6043. link_info->req_link_speed = link_info->force_link_speed;
  6044. link_info->req_duplex = link_info->duplex_setting;
  6045. }
  6046. if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
  6047. link_info->req_flow_ctrl =
  6048. link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
  6049. else
  6050. link_info->req_flow_ctrl = link_info->force_pause_setting;
  6051. return rc;
  6052. }
  6053. static int bnxt_get_max_irq(struct pci_dev *pdev)
  6054. {
  6055. u16 ctrl;
  6056. if (!pdev->msix_cap)
  6057. return 1;
  6058. pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
  6059. return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
  6060. }
  6061. static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
  6062. int *max_cp)
  6063. {
  6064. int max_ring_grps = 0;
  6065. #ifdef CONFIG_BNXT_SRIOV
  6066. if (!BNXT_PF(bp)) {
  6067. *max_tx = bp->vf.max_tx_rings;
  6068. *max_rx = bp->vf.max_rx_rings;
  6069. *max_cp = min_t(int, bp->vf.max_irqs, bp->vf.max_cp_rings);
  6070. *max_cp = min_t(int, *max_cp, bp->vf.max_stat_ctxs);
  6071. max_ring_grps = bp->vf.max_hw_ring_grps;
  6072. } else
  6073. #endif
  6074. {
  6075. *max_tx = bp->pf.max_tx_rings;
  6076. *max_rx = bp->pf.max_rx_rings;
  6077. *max_cp = min_t(int, bp->pf.max_irqs, bp->pf.max_cp_rings);
  6078. *max_cp = min_t(int, *max_cp, bp->pf.max_stat_ctxs);
  6079. max_ring_grps = bp->pf.max_hw_ring_grps;
  6080. }
  6081. if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
  6082. *max_cp -= 1;
  6083. *max_rx -= 2;
  6084. }
  6085. if (bp->flags & BNXT_FLAG_AGG_RINGS)
  6086. *max_rx >>= 1;
  6087. *max_rx = min_t(int, *max_rx, max_ring_grps);
  6088. }
  6089. int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
  6090. {
  6091. int rx, tx, cp;
  6092. _bnxt_get_max_rings(bp, &rx, &tx, &cp);
  6093. if (!rx || !tx || !cp)
  6094. return -ENOMEM;
  6095. *max_rx = rx;
  6096. *max_tx = tx;
  6097. return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
  6098. }
  6099. static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
  6100. bool shared)
  6101. {
  6102. int rc;
  6103. rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
  6104. if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
  6105. /* Not enough rings, try disabling agg rings. */
  6106. bp->flags &= ~BNXT_FLAG_AGG_RINGS;
  6107. rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
  6108. if (rc)
  6109. return rc;
  6110. bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
  6111. bp->dev->hw_features &= ~NETIF_F_LRO;
  6112. bp->dev->features &= ~NETIF_F_LRO;
  6113. bnxt_set_ring_params(bp);
  6114. }
  6115. if (bp->flags & BNXT_FLAG_ROCE_CAP) {
  6116. int max_cp, max_stat, max_irq;
  6117. /* Reserve minimum resources for RoCE */
  6118. max_cp = bnxt_get_max_func_cp_rings(bp);
  6119. max_stat = bnxt_get_max_func_stat_ctxs(bp);
  6120. max_irq = bnxt_get_max_func_irqs(bp);
  6121. if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
  6122. max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
  6123. max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
  6124. return 0;
  6125. max_cp -= BNXT_MIN_ROCE_CP_RINGS;
  6126. max_irq -= BNXT_MIN_ROCE_CP_RINGS;
  6127. max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
  6128. max_cp = min_t(int, max_cp, max_irq);
  6129. max_cp = min_t(int, max_cp, max_stat);
  6130. rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
  6131. if (rc)
  6132. rc = 0;
  6133. }
  6134. return rc;
  6135. }
  6136. static int bnxt_set_dflt_rings(struct bnxt *bp)
  6137. {
  6138. int dflt_rings, max_rx_rings, max_tx_rings, rc;
  6139. bool sh = true;
  6140. if (sh)
  6141. bp->flags |= BNXT_FLAG_SHARED_RINGS;
  6142. dflt_rings = netif_get_num_default_rss_queues();
  6143. rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
  6144. if (rc)
  6145. return rc;
  6146. bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
  6147. bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
  6148. rc = bnxt_hwrm_reserve_tx_rings(bp, &bp->tx_nr_rings_per_tc);
  6149. if (rc)
  6150. netdev_warn(bp->dev, "Unable to reserve tx rings\n");
  6151. bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
  6152. bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
  6153. bp->tx_nr_rings + bp->rx_nr_rings;
  6154. bp->num_stat_ctxs = bp->cp_nr_rings;
  6155. if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
  6156. bp->rx_nr_rings++;
  6157. bp->cp_nr_rings++;
  6158. }
  6159. return rc;
  6160. }
  6161. void bnxt_restore_pf_fw_resources(struct bnxt *bp)
  6162. {
  6163. ASSERT_RTNL();
  6164. bnxt_hwrm_func_qcaps(bp);
  6165. bnxt_subtract_ulp_resources(bp, BNXT_ROCE_ULP);
  6166. }
  6167. static void bnxt_parse_log_pcie_link(struct bnxt *bp)
  6168. {
  6169. enum pcie_link_width width = PCIE_LNK_WIDTH_UNKNOWN;
  6170. enum pci_bus_speed speed = PCI_SPEED_UNKNOWN;
  6171. if (pcie_get_minimum_link(bp->pdev, &speed, &width) ||
  6172. speed == PCI_SPEED_UNKNOWN || width == PCIE_LNK_WIDTH_UNKNOWN)
  6173. netdev_info(bp->dev, "Failed to determine PCIe Link Info\n");
  6174. else
  6175. netdev_info(bp->dev, "PCIe: Speed %s Width x%d\n",
  6176. speed == PCIE_SPEED_2_5GT ? "2.5GT/s" :
  6177. speed == PCIE_SPEED_5_0GT ? "5.0GT/s" :
  6178. speed == PCIE_SPEED_8_0GT ? "8.0GT/s" :
  6179. "Unknown", width);
  6180. }
  6181. static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  6182. {
  6183. static int version_printed;
  6184. struct net_device *dev;
  6185. struct bnxt *bp;
  6186. int rc, max_irqs;
  6187. if (pci_is_bridge(pdev))
  6188. return -ENODEV;
  6189. if (version_printed++ == 0)
  6190. pr_info("%s", version);
  6191. max_irqs = bnxt_get_max_irq(pdev);
  6192. dev = alloc_etherdev_mq(sizeof(*bp), max_irqs);
  6193. if (!dev)
  6194. return -ENOMEM;
  6195. bp = netdev_priv(dev);
  6196. if (bnxt_vf_pciid(ent->driver_data))
  6197. bp->flags |= BNXT_FLAG_VF;
  6198. if (pdev->msix_cap)
  6199. bp->flags |= BNXT_FLAG_MSIX_CAP;
  6200. rc = bnxt_init_board(pdev, dev);
  6201. if (rc < 0)
  6202. goto init_err_free;
  6203. dev->netdev_ops = &bnxt_netdev_ops;
  6204. dev->watchdog_timeo = BNXT_TX_TIMEOUT;
  6205. dev->ethtool_ops = &bnxt_ethtool_ops;
  6206. pci_set_drvdata(pdev, dev);
  6207. rc = bnxt_alloc_hwrm_resources(bp);
  6208. if (rc)
  6209. goto init_err_pci_clean;
  6210. mutex_init(&bp->hwrm_cmd_lock);
  6211. rc = bnxt_hwrm_ver_get(bp);
  6212. if (rc)
  6213. goto init_err_pci_clean;
  6214. rc = bnxt_hwrm_func_reset(bp);
  6215. if (rc)
  6216. goto init_err_pci_clean;
  6217. bnxt_hwrm_fw_set_time(bp);
  6218. dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
  6219. NETIF_F_TSO | NETIF_F_TSO6 |
  6220. NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
  6221. NETIF_F_GSO_IPXIP4 |
  6222. NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
  6223. NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
  6224. NETIF_F_RXCSUM | NETIF_F_GRO;
  6225. if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
  6226. dev->hw_features |= NETIF_F_LRO;
  6227. dev->hw_enc_features =
  6228. NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
  6229. NETIF_F_TSO | NETIF_F_TSO6 |
  6230. NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
  6231. NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
  6232. NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
  6233. dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
  6234. NETIF_F_GSO_GRE_CSUM;
  6235. dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
  6236. dev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX |
  6237. NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX;
  6238. dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
  6239. dev->priv_flags |= IFF_UNICAST_FLT;
  6240. /* MTU range: 60 - 9500 */
  6241. dev->min_mtu = ETH_ZLEN;
  6242. dev->max_mtu = BNXT_MAX_MTU;
  6243. bnxt_dcb_init(bp);
  6244. #ifdef CONFIG_BNXT_SRIOV
  6245. init_waitqueue_head(&bp->sriov_cfg_wait);
  6246. #endif
  6247. bp->gro_func = bnxt_gro_func_5730x;
  6248. if (BNXT_CHIP_NUM_57X1X(bp->chip_num))
  6249. bp->gro_func = bnxt_gro_func_5731x;
  6250. rc = bnxt_hwrm_func_drv_rgtr(bp);
  6251. if (rc)
  6252. goto init_err_pci_clean;
  6253. rc = bnxt_hwrm_func_rgtr_async_events(bp, NULL, 0);
  6254. if (rc)
  6255. goto init_err_pci_clean;
  6256. bp->ulp_probe = bnxt_ulp_probe;
  6257. /* Get the MAX capabilities for this function */
  6258. rc = bnxt_hwrm_func_qcaps(bp);
  6259. if (rc) {
  6260. netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
  6261. rc);
  6262. rc = -1;
  6263. goto init_err_pci_clean;
  6264. }
  6265. rc = bnxt_hwrm_queue_qportcfg(bp);
  6266. if (rc) {
  6267. netdev_err(bp->dev, "hwrm query qportcfg failure rc: %x\n",
  6268. rc);
  6269. rc = -1;
  6270. goto init_err_pci_clean;
  6271. }
  6272. bnxt_hwrm_func_qcfg(bp);
  6273. bnxt_hwrm_port_led_qcaps(bp);
  6274. bnxt_set_rx_skb_mode(bp, false);
  6275. bnxt_set_tpa_flags(bp);
  6276. bnxt_set_ring_params(bp);
  6277. bnxt_set_max_func_irqs(bp, max_irqs);
  6278. rc = bnxt_set_dflt_rings(bp);
  6279. if (rc) {
  6280. netdev_err(bp->dev, "Not enough rings available.\n");
  6281. rc = -ENOMEM;
  6282. goto init_err_pci_clean;
  6283. }
  6284. /* Default RSS hash cfg. */
  6285. bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
  6286. VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
  6287. VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
  6288. VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
  6289. if (!BNXT_CHIP_NUM_57X0X(bp->chip_num) &&
  6290. !BNXT_CHIP_TYPE_NITRO_A0(bp) &&
  6291. bp->hwrm_spec_code >= 0x10501) {
  6292. bp->flags |= BNXT_FLAG_UDP_RSS_CAP;
  6293. bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
  6294. VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
  6295. }
  6296. bnxt_hwrm_vnic_qcaps(bp);
  6297. if (bnxt_rfs_supported(bp)) {
  6298. dev->hw_features |= NETIF_F_NTUPLE;
  6299. if (bnxt_rfs_capable(bp)) {
  6300. bp->flags |= BNXT_FLAG_RFS;
  6301. dev->features |= NETIF_F_NTUPLE;
  6302. }
  6303. }
  6304. if (dev->hw_features & NETIF_F_HW_VLAN_CTAG_RX)
  6305. bp->flags |= BNXT_FLAG_STRIP_VLAN;
  6306. rc = bnxt_probe_phy(bp);
  6307. if (rc)
  6308. goto init_err_pci_clean;
  6309. rc = bnxt_init_int_mode(bp);
  6310. if (rc)
  6311. goto init_err_pci_clean;
  6312. rc = register_netdev(dev);
  6313. if (rc)
  6314. goto init_err_clr_int;
  6315. netdev_info(dev, "%s found at mem %lx, node addr %pM\n",
  6316. board_info[ent->driver_data].name,
  6317. (long)pci_resource_start(pdev, 0), dev->dev_addr);
  6318. bnxt_parse_log_pcie_link(bp);
  6319. return 0;
  6320. init_err_clr_int:
  6321. bnxt_clear_int_mode(bp);
  6322. init_err_pci_clean:
  6323. bnxt_cleanup_pci(bp);
  6324. init_err_free:
  6325. free_netdev(dev);
  6326. return rc;
  6327. }
  6328. /**
  6329. * bnxt_io_error_detected - called when PCI error is detected
  6330. * @pdev: Pointer to PCI device
  6331. * @state: The current pci connection state
  6332. *
  6333. * This function is called after a PCI bus error affecting
  6334. * this device has been detected.
  6335. */
  6336. static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
  6337. pci_channel_state_t state)
  6338. {
  6339. struct net_device *netdev = pci_get_drvdata(pdev);
  6340. struct bnxt *bp = netdev_priv(netdev);
  6341. netdev_info(netdev, "PCI I/O error detected\n");
  6342. rtnl_lock();
  6343. netif_device_detach(netdev);
  6344. bnxt_ulp_stop(bp);
  6345. if (state == pci_channel_io_perm_failure) {
  6346. rtnl_unlock();
  6347. return PCI_ERS_RESULT_DISCONNECT;
  6348. }
  6349. if (netif_running(netdev))
  6350. bnxt_close(netdev);
  6351. pci_disable_device(pdev);
  6352. rtnl_unlock();
  6353. /* Request a slot slot reset. */
  6354. return PCI_ERS_RESULT_NEED_RESET;
  6355. }
  6356. /**
  6357. * bnxt_io_slot_reset - called after the pci bus has been reset.
  6358. * @pdev: Pointer to PCI device
  6359. *
  6360. * Restart the card from scratch, as if from a cold-boot.
  6361. * At this point, the card has exprienced a hard reset,
  6362. * followed by fixups by BIOS, and has its config space
  6363. * set up identically to what it was at cold boot.
  6364. */
  6365. static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
  6366. {
  6367. struct net_device *netdev = pci_get_drvdata(pdev);
  6368. struct bnxt *bp = netdev_priv(netdev);
  6369. int err = 0;
  6370. pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
  6371. netdev_info(bp->dev, "PCI Slot Reset\n");
  6372. rtnl_lock();
  6373. if (pci_enable_device(pdev)) {
  6374. dev_err(&pdev->dev,
  6375. "Cannot re-enable PCI device after reset.\n");
  6376. } else {
  6377. pci_set_master(pdev);
  6378. err = bnxt_hwrm_func_reset(bp);
  6379. if (!err && netif_running(netdev))
  6380. err = bnxt_open(netdev);
  6381. if (!err) {
  6382. result = PCI_ERS_RESULT_RECOVERED;
  6383. bnxt_ulp_start(bp);
  6384. }
  6385. }
  6386. if (result != PCI_ERS_RESULT_RECOVERED && netif_running(netdev))
  6387. dev_close(netdev);
  6388. rtnl_unlock();
  6389. err = pci_cleanup_aer_uncorrect_error_status(pdev);
  6390. if (err) {
  6391. dev_err(&pdev->dev,
  6392. "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
  6393. err); /* non-fatal, continue */
  6394. }
  6395. return PCI_ERS_RESULT_RECOVERED;
  6396. }
  6397. /**
  6398. * bnxt_io_resume - called when traffic can start flowing again.
  6399. * @pdev: Pointer to PCI device
  6400. *
  6401. * This callback is called when the error recovery driver tells
  6402. * us that its OK to resume normal operation.
  6403. */
  6404. static void bnxt_io_resume(struct pci_dev *pdev)
  6405. {
  6406. struct net_device *netdev = pci_get_drvdata(pdev);
  6407. rtnl_lock();
  6408. netif_device_attach(netdev);
  6409. rtnl_unlock();
  6410. }
  6411. static const struct pci_error_handlers bnxt_err_handler = {
  6412. .error_detected = bnxt_io_error_detected,
  6413. .slot_reset = bnxt_io_slot_reset,
  6414. .resume = bnxt_io_resume
  6415. };
  6416. static struct pci_driver bnxt_pci_driver = {
  6417. .name = DRV_MODULE_NAME,
  6418. .id_table = bnxt_pci_tbl,
  6419. .probe = bnxt_init_one,
  6420. .remove = bnxt_remove_one,
  6421. .err_handler = &bnxt_err_handler,
  6422. #if defined(CONFIG_BNXT_SRIOV)
  6423. .sriov_configure = bnxt_sriov_configure,
  6424. #endif
  6425. };
  6426. module_pci_driver(bnxt_pci_driver);