bnxt.c 190 KB

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