bnxt.c 178 KB

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