bnxt.c 205 KB

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