i40e_main.c 462 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright(c) 2013 - 2021 Intel Corporation. */
  3. #include <generated/utsrelease.h>
  4. #include <linux/crash_dump.h>
  5. #include <linux/if_bridge.h>
  6. #include <linux/if_macvlan.h>
  7. #include <linux/module.h>
  8. #include <net/pkt_cls.h>
  9. #include <net/xdp_sock_drv.h>
  10. /* Local includes */
  11. #include "i40e.h"
  12. #include "i40e_devids.h"
  13. #include "i40e_diag.h"
  14. #include "i40e_lan_hmc.h"
  15. #include "i40e_virtchnl_pf.h"
  16. #include "i40e_xsk.h"
  17. /* All i40e tracepoints are defined by the include below, which
  18. * must be included exactly once across the whole kernel with
  19. * CREATE_TRACE_POINTS defined
  20. */
  21. #define CREATE_TRACE_POINTS
  22. #include "i40e_trace.h"
  23. const char i40e_driver_name[] = "i40e";
  24. static const char i40e_driver_string[] =
  25. "Intel(R) Ethernet Connection XL710 Network Driver";
  26. static const char i40e_copyright[] = "Copyright (c) 2013 - 2019 Intel Corporation.";
  27. /* a bit of forward declarations */
  28. static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
  29. static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired);
  30. static int i40e_add_vsi(struct i40e_vsi *vsi);
  31. static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
  32. static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired);
  33. static int i40e_setup_misc_vector(struct i40e_pf *pf);
  34. static void i40e_determine_queue_usage(struct i40e_pf *pf);
  35. static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
  36. static void i40e_prep_for_reset(struct i40e_pf *pf);
  37. static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
  38. bool lock_acquired);
  39. static int i40e_reset(struct i40e_pf *pf);
  40. static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired);
  41. static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf);
  42. static int i40e_restore_interrupt_scheme(struct i40e_pf *pf);
  43. static bool i40e_check_recovery_mode(struct i40e_pf *pf);
  44. static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw);
  45. static void i40e_fdir_sb_setup(struct i40e_pf *pf);
  46. static int i40e_veb_get_bw_info(struct i40e_veb *veb);
  47. static int i40e_get_capabilities(struct i40e_pf *pf,
  48. enum i40e_admin_queue_opc list_type);
  49. static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf);
  50. /* i40e_pci_tbl - PCI Device ID Table
  51. *
  52. * Last entry must be all 0s
  53. *
  54. * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
  55. * Class, Class Mask, private data (not used) }
  56. */
  57. static const struct pci_device_id i40e_pci_tbl[] = {
  58. {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
  59. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
  60. {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
  61. {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
  62. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
  63. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
  64. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
  65. {PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_BC), 0},
  66. {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
  67. {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
  68. {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_BC), 0},
  69. {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_SFP), 0},
  70. {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_B), 0},
  71. {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
  72. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
  73. {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
  74. {PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
  75. {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
  76. {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
  77. {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722_A), 0},
  78. {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
  79. {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
  80. {PCI_VDEVICE(INTEL, I40E_DEV_ID_X710_N3000), 0},
  81. {PCI_VDEVICE(INTEL, I40E_DEV_ID_XXV710_N3000), 0},
  82. {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
  83. {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
  84. /* required last entry */
  85. {0, }
  86. };
  87. MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
  88. #define I40E_MAX_VF_COUNT 128
  89. static int debug = -1;
  90. module_param(debug, uint, 0);
  91. MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
  92. MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
  93. MODULE_IMPORT_NS(LIBIE);
  94. MODULE_LICENSE("GPL v2");
  95. static struct workqueue_struct *i40e_wq;
  96. static void netdev_hw_addr_refcnt(struct i40e_mac_filter *f,
  97. struct net_device *netdev, int delta)
  98. {
  99. struct netdev_hw_addr_list *ha_list;
  100. struct netdev_hw_addr *ha;
  101. if (!f || !netdev)
  102. return;
  103. if (is_unicast_ether_addr(f->macaddr) || is_link_local_ether_addr(f->macaddr))
  104. ha_list = &netdev->uc;
  105. else
  106. ha_list = &netdev->mc;
  107. netdev_hw_addr_list_for_each(ha, ha_list) {
  108. if (ether_addr_equal(ha->addr, f->macaddr)) {
  109. ha->refcount += delta;
  110. if (ha->refcount <= 0)
  111. ha->refcount = 1;
  112. break;
  113. }
  114. }
  115. }
  116. /**
  117. * i40e_hw_to_dev - get device pointer from the hardware structure
  118. * @hw: pointer to the device HW structure
  119. **/
  120. struct device *i40e_hw_to_dev(struct i40e_hw *hw)
  121. {
  122. struct i40e_pf *pf = i40e_hw_to_pf(hw);
  123. return &pf->pdev->dev;
  124. }
  125. /**
  126. * i40e_allocate_dma_mem - OS specific memory alloc for shared code
  127. * @hw: pointer to the HW structure
  128. * @mem: ptr to mem struct to fill out
  129. * @size: size of memory requested
  130. * @alignment: what to align the allocation to
  131. **/
  132. int i40e_allocate_dma_mem(struct i40e_hw *hw, struct i40e_dma_mem *mem,
  133. u64 size, u32 alignment)
  134. {
  135. struct i40e_pf *pf = i40e_hw_to_pf(hw);
  136. mem->size = ALIGN(size, alignment);
  137. mem->va = dma_alloc_coherent(&pf->pdev->dev, mem->size, &mem->pa,
  138. GFP_KERNEL);
  139. if (!mem->va)
  140. return -ENOMEM;
  141. return 0;
  142. }
  143. /**
  144. * i40e_free_dma_mem - OS specific memory free for shared code
  145. * @hw: pointer to the HW structure
  146. * @mem: ptr to mem struct to free
  147. **/
  148. int i40e_free_dma_mem(struct i40e_hw *hw, struct i40e_dma_mem *mem)
  149. {
  150. struct i40e_pf *pf = i40e_hw_to_pf(hw);
  151. dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
  152. mem->va = NULL;
  153. mem->pa = 0;
  154. mem->size = 0;
  155. return 0;
  156. }
  157. /**
  158. * i40e_allocate_virt_mem - OS specific memory alloc for shared code
  159. * @hw: pointer to the HW structure
  160. * @mem: ptr to mem struct to fill out
  161. * @size: size of memory requested
  162. **/
  163. int i40e_allocate_virt_mem(struct i40e_hw *hw, struct i40e_virt_mem *mem,
  164. u32 size)
  165. {
  166. mem->size = size;
  167. mem->va = kzalloc(size, GFP_KERNEL);
  168. if (!mem->va)
  169. return -ENOMEM;
  170. return 0;
  171. }
  172. /**
  173. * i40e_free_virt_mem - OS specific memory free for shared code
  174. * @hw: pointer to the HW structure
  175. * @mem: ptr to mem struct to free
  176. **/
  177. int i40e_free_virt_mem(struct i40e_hw *hw, struct i40e_virt_mem *mem)
  178. {
  179. /* it's ok to kfree a NULL pointer */
  180. kfree(mem->va);
  181. mem->va = NULL;
  182. mem->size = 0;
  183. return 0;
  184. }
  185. /**
  186. * i40e_get_lump - find a lump of free generic resource
  187. * @pf: board private structure
  188. * @pile: the pile of resource to search
  189. * @needed: the number of items needed
  190. * @id: an owner id to stick on the items assigned
  191. *
  192. * Returns the base item index of the lump, or negative for error
  193. **/
  194. static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
  195. u16 needed, u16 id)
  196. {
  197. int ret = -ENOMEM;
  198. int i, j;
  199. if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
  200. dev_info(&pf->pdev->dev,
  201. "param err: pile=%s needed=%d id=0x%04x\n",
  202. pile ? "<valid>" : "<null>", needed, id);
  203. return -EINVAL;
  204. }
  205. /* Allocate last queue in the pile for FDIR VSI queue
  206. * so it doesn't fragment the qp_pile
  207. */
  208. if (pile == pf->qp_pile && pf->vsi[id]->type == I40E_VSI_FDIR) {
  209. if (pile->list[pile->num_entries - 1] & I40E_PILE_VALID_BIT) {
  210. dev_err(&pf->pdev->dev,
  211. "Cannot allocate queue %d for I40E_VSI_FDIR\n",
  212. pile->num_entries - 1);
  213. return -ENOMEM;
  214. }
  215. pile->list[pile->num_entries - 1] = id | I40E_PILE_VALID_BIT;
  216. return pile->num_entries - 1;
  217. }
  218. i = 0;
  219. while (i < pile->num_entries) {
  220. /* skip already allocated entries */
  221. if (pile->list[i] & I40E_PILE_VALID_BIT) {
  222. i++;
  223. continue;
  224. }
  225. /* do we have enough in this lump? */
  226. for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
  227. if (pile->list[i+j] & I40E_PILE_VALID_BIT)
  228. break;
  229. }
  230. if (j == needed) {
  231. /* there was enough, so assign it to the requestor */
  232. for (j = 0; j < needed; j++)
  233. pile->list[i+j] = id | I40E_PILE_VALID_BIT;
  234. ret = i;
  235. break;
  236. }
  237. /* not enough, so skip over it and continue looking */
  238. i += j;
  239. }
  240. return ret;
  241. }
  242. /**
  243. * i40e_put_lump - return a lump of generic resource
  244. * @pile: the pile of resource to search
  245. * @index: the base item index
  246. * @id: the owner id of the items assigned
  247. *
  248. * Returns the count of items in the lump
  249. **/
  250. static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
  251. {
  252. int valid_id = (id | I40E_PILE_VALID_BIT);
  253. int count = 0;
  254. u16 i;
  255. if (!pile || index >= pile->num_entries)
  256. return -EINVAL;
  257. for (i = index;
  258. i < pile->num_entries && pile->list[i] == valid_id;
  259. i++) {
  260. pile->list[i] = 0;
  261. count++;
  262. }
  263. return count;
  264. }
  265. /**
  266. * i40e_find_vsi_from_id - searches for the vsi with the given id
  267. * @pf: the pf structure to search for the vsi
  268. * @id: id of the vsi it is searching for
  269. **/
  270. struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
  271. {
  272. struct i40e_vsi *vsi;
  273. int i;
  274. i40e_pf_for_each_vsi(pf, i, vsi)
  275. if (vsi->id == id)
  276. return vsi;
  277. return NULL;
  278. }
  279. /**
  280. * i40e_service_event_schedule - Schedule the service task to wake up
  281. * @pf: board private structure
  282. *
  283. * If not already scheduled, this puts the task into the work queue
  284. **/
  285. void i40e_service_event_schedule(struct i40e_pf *pf)
  286. {
  287. if ((!test_bit(__I40E_DOWN, pf->state) &&
  288. !test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) ||
  289. test_bit(__I40E_RECOVERY_MODE, pf->state))
  290. queue_work(i40e_wq, &pf->service_task);
  291. }
  292. /**
  293. * i40e_tx_timeout - Respond to a Tx Hang
  294. * @netdev: network interface device structure
  295. * @txqueue: queue number timing out
  296. *
  297. * If any port has noticed a Tx timeout, it is likely that the whole
  298. * device is munged, not just the one netdev port, so go for the full
  299. * reset.
  300. **/
  301. static void i40e_tx_timeout(struct net_device *netdev, unsigned int txqueue)
  302. {
  303. struct i40e_netdev_priv *np = netdev_priv(netdev);
  304. struct i40e_vsi *vsi = np->vsi;
  305. struct i40e_pf *pf = vsi->back;
  306. struct i40e_ring *tx_ring = NULL;
  307. unsigned int i;
  308. u32 head, val;
  309. pf->tx_timeout_count++;
  310. /* with txqueue index, find the tx_ring struct */
  311. for (i = 0; i < vsi->num_queue_pairs; i++) {
  312. if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
  313. if (txqueue ==
  314. vsi->tx_rings[i]->queue_index) {
  315. tx_ring = vsi->tx_rings[i];
  316. break;
  317. }
  318. }
  319. }
  320. if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
  321. pf->tx_timeout_recovery_level = 1; /* reset after some time */
  322. else if (time_before(jiffies,
  323. (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
  324. return; /* don't do any new action before the next timeout */
  325. /* don't kick off another recovery if one is already pending */
  326. if (test_and_set_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state))
  327. return;
  328. if (tx_ring) {
  329. head = i40e_get_head(tx_ring);
  330. /* Read interrupt register */
  331. if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
  332. val = rd32(&pf->hw,
  333. I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
  334. tx_ring->vsi->base_vector - 1));
  335. else
  336. val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
  337. netdev_info(netdev, "tx_timeout: VSI_seid: %d, Q %d, NTC: 0x%x, HWB: 0x%x, NTU: 0x%x, TAIL: 0x%x, INT: 0x%x\n",
  338. vsi->seid, txqueue, tx_ring->next_to_clean,
  339. head, tx_ring->next_to_use,
  340. readl(tx_ring->tail), val);
  341. }
  342. pf->tx_timeout_last_recovery = jiffies;
  343. netdev_info(netdev, "tx_timeout recovery level %d, txqueue %d\n",
  344. pf->tx_timeout_recovery_level, txqueue);
  345. switch (pf->tx_timeout_recovery_level) {
  346. case 1:
  347. set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
  348. break;
  349. case 2:
  350. set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
  351. break;
  352. case 3:
  353. set_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
  354. break;
  355. default:
  356. netdev_err(netdev, "tx_timeout recovery unsuccessful, device is in non-recoverable state.\n");
  357. set_bit(__I40E_DOWN_REQUESTED, pf->state);
  358. set_bit(__I40E_VSI_DOWN_REQUESTED, vsi->state);
  359. break;
  360. }
  361. i40e_service_event_schedule(pf);
  362. pf->tx_timeout_recovery_level++;
  363. }
  364. /**
  365. * i40e_get_vsi_stats_struct - Get System Network Statistics
  366. * @vsi: the VSI we care about
  367. *
  368. * Returns the address of the device statistics structure.
  369. * The statistics are actually updated from the service task.
  370. **/
  371. struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
  372. {
  373. return &vsi->net_stats;
  374. }
  375. /**
  376. * i40e_get_netdev_stats_struct_tx - populate stats from a Tx ring
  377. * @ring: Tx ring to get statistics from
  378. * @stats: statistics entry to be updated
  379. **/
  380. static void i40e_get_netdev_stats_struct_tx(struct i40e_ring *ring,
  381. struct rtnl_link_stats64 *stats)
  382. {
  383. u64 bytes, packets;
  384. unsigned int start;
  385. do {
  386. start = u64_stats_fetch_begin(&ring->syncp);
  387. packets = ring->stats.packets;
  388. bytes = ring->stats.bytes;
  389. } while (u64_stats_fetch_retry(&ring->syncp, start));
  390. stats->tx_packets += packets;
  391. stats->tx_bytes += bytes;
  392. }
  393. /**
  394. * i40e_get_netdev_stats_struct - Get statistics for netdev interface
  395. * @netdev: network interface device structure
  396. * @stats: data structure to store statistics
  397. *
  398. * Returns the address of the device statistics structure.
  399. * The statistics are actually updated from the service task.
  400. **/
  401. static void i40e_get_netdev_stats_struct(struct net_device *netdev,
  402. struct rtnl_link_stats64 *stats)
  403. {
  404. struct i40e_netdev_priv *np = netdev_priv(netdev);
  405. struct i40e_vsi *vsi = np->vsi;
  406. struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
  407. struct i40e_ring *ring;
  408. int i;
  409. if (test_bit(__I40E_VSI_DOWN, vsi->state))
  410. return;
  411. if (!vsi->tx_rings)
  412. return;
  413. rcu_read_lock();
  414. for (i = 0; i < vsi->num_queue_pairs; i++) {
  415. u64 bytes, packets;
  416. unsigned int start;
  417. ring = READ_ONCE(vsi->tx_rings[i]);
  418. if (!ring)
  419. continue;
  420. i40e_get_netdev_stats_struct_tx(ring, stats);
  421. if (i40e_enabled_xdp_vsi(vsi)) {
  422. ring = READ_ONCE(vsi->xdp_rings[i]);
  423. if (!ring)
  424. continue;
  425. i40e_get_netdev_stats_struct_tx(ring, stats);
  426. }
  427. ring = READ_ONCE(vsi->rx_rings[i]);
  428. if (!ring)
  429. continue;
  430. do {
  431. start = u64_stats_fetch_begin(&ring->syncp);
  432. packets = ring->stats.packets;
  433. bytes = ring->stats.bytes;
  434. } while (u64_stats_fetch_retry(&ring->syncp, start));
  435. stats->rx_packets += packets;
  436. stats->rx_bytes += bytes;
  437. }
  438. rcu_read_unlock();
  439. /* following stats updated by i40e_watchdog_subtask() */
  440. stats->multicast = vsi_stats->multicast;
  441. stats->tx_errors = vsi_stats->tx_errors;
  442. stats->tx_dropped = vsi_stats->tx_dropped;
  443. stats->rx_errors = vsi_stats->rx_errors;
  444. stats->rx_dropped = vsi_stats->rx_dropped;
  445. stats->rx_missed_errors = vsi_stats->rx_missed_errors;
  446. stats->rx_crc_errors = vsi_stats->rx_crc_errors;
  447. stats->rx_length_errors = vsi_stats->rx_length_errors;
  448. }
  449. /**
  450. * i40e_vsi_reset_stats - Resets all stats of the given vsi
  451. * @vsi: the VSI to have its stats reset
  452. **/
  453. void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
  454. {
  455. struct rtnl_link_stats64 *ns;
  456. int i;
  457. if (!vsi)
  458. return;
  459. ns = i40e_get_vsi_stats_struct(vsi);
  460. memset(ns, 0, sizeof(*ns));
  461. memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
  462. memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
  463. memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
  464. if (vsi->rx_rings && vsi->rx_rings[0]) {
  465. for (i = 0; i < vsi->num_queue_pairs; i++) {
  466. memset(&vsi->rx_rings[i]->stats, 0,
  467. sizeof(vsi->rx_rings[i]->stats));
  468. memset(&vsi->rx_rings[i]->rx_stats, 0,
  469. sizeof(vsi->rx_rings[i]->rx_stats));
  470. memset(&vsi->tx_rings[i]->stats, 0,
  471. sizeof(vsi->tx_rings[i]->stats));
  472. memset(&vsi->tx_rings[i]->tx_stats, 0,
  473. sizeof(vsi->tx_rings[i]->tx_stats));
  474. }
  475. }
  476. vsi->stat_offsets_loaded = false;
  477. }
  478. /**
  479. * i40e_pf_reset_stats - Reset all of the stats for the given PF
  480. * @pf: the PF to be reset
  481. **/
  482. void i40e_pf_reset_stats(struct i40e_pf *pf)
  483. {
  484. struct i40e_veb *veb;
  485. int i;
  486. memset(&pf->stats, 0, sizeof(pf->stats));
  487. memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
  488. pf->stat_offsets_loaded = false;
  489. i40e_pf_for_each_veb(pf, i, veb) {
  490. memset(&veb->stats, 0, sizeof(veb->stats));
  491. memset(&veb->stats_offsets, 0, sizeof(veb->stats_offsets));
  492. memset(&veb->tc_stats, 0, sizeof(veb->tc_stats));
  493. memset(&veb->tc_stats_offsets, 0, sizeof(veb->tc_stats_offsets));
  494. veb->stat_offsets_loaded = false;
  495. }
  496. pf->hw_csum_rx_error = 0;
  497. }
  498. /**
  499. * i40e_compute_pci_to_hw_id - compute index form PCI function.
  500. * @vsi: ptr to the VSI to read from.
  501. * @hw: ptr to the hardware info.
  502. **/
  503. static u32 i40e_compute_pci_to_hw_id(struct i40e_vsi *vsi, struct i40e_hw *hw)
  504. {
  505. int pf_count = i40e_get_pf_count(hw);
  506. if (vsi->type == I40E_VSI_SRIOV)
  507. return (hw->port * BIT(7)) / pf_count + vsi->vf_id;
  508. return hw->port + BIT(7);
  509. }
  510. /**
  511. * i40e_stat_update64 - read and update a 64 bit stat from the chip.
  512. * @hw: ptr to the hardware info.
  513. * @hireg: the high 32 bit reg to read.
  514. * @loreg: the low 32 bit reg to read.
  515. * @offset_loaded: has the initial offset been loaded yet.
  516. * @offset: ptr to current offset value.
  517. * @stat: ptr to the stat.
  518. *
  519. * Since the device stats are not reset at PFReset, they will not
  520. * be zeroed when the driver starts. We'll save the first values read
  521. * and use them as offsets to be subtracted from the raw values in order
  522. * to report stats that count from zero.
  523. **/
  524. static void i40e_stat_update64(struct i40e_hw *hw, u32 hireg, u32 loreg,
  525. bool offset_loaded, u64 *offset, u64 *stat)
  526. {
  527. u64 new_data;
  528. new_data = rd64(hw, loreg);
  529. if (!offset_loaded || new_data < *offset)
  530. *offset = new_data;
  531. *stat = new_data - *offset;
  532. }
  533. /**
  534. * i40e_stat_update48 - read and update a 48 bit stat from the chip
  535. * @hw: ptr to the hardware info
  536. * @hireg: the high 32 bit reg to read
  537. * @loreg: the low 32 bit reg to read
  538. * @offset_loaded: has the initial offset been loaded yet
  539. * @offset: ptr to current offset value
  540. * @stat: ptr to the stat
  541. *
  542. * Since the device stats are not reset at PFReset, they likely will not
  543. * be zeroed when the driver starts. We'll save the first values read
  544. * and use them as offsets to be subtracted from the raw values in order
  545. * to report stats that count from zero. In the process, we also manage
  546. * the potential roll-over.
  547. **/
  548. static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
  549. bool offset_loaded, u64 *offset, u64 *stat)
  550. {
  551. u64 new_data;
  552. if (hw->device_id == I40E_DEV_ID_QEMU) {
  553. new_data = rd32(hw, loreg);
  554. new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
  555. } else {
  556. new_data = rd64(hw, loreg);
  557. }
  558. if (!offset_loaded)
  559. *offset = new_data;
  560. if (likely(new_data >= *offset))
  561. *stat = new_data - *offset;
  562. else
  563. *stat = (new_data + BIT_ULL(48)) - *offset;
  564. *stat &= 0xFFFFFFFFFFFFULL;
  565. }
  566. /**
  567. * i40e_stat_update32 - read and update a 32 bit stat from the chip
  568. * @hw: ptr to the hardware info
  569. * @reg: the hw reg to read
  570. * @offset_loaded: has the initial offset been loaded yet
  571. * @offset: ptr to current offset value
  572. * @stat: ptr to the stat
  573. **/
  574. static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
  575. bool offset_loaded, u64 *offset, u64 *stat)
  576. {
  577. u32 new_data;
  578. new_data = rd32(hw, reg);
  579. if (!offset_loaded)
  580. *offset = new_data;
  581. if (likely(new_data >= *offset))
  582. *stat = (u32)(new_data - *offset);
  583. else
  584. *stat = (u32)((new_data + BIT_ULL(32)) - *offset);
  585. }
  586. /**
  587. * i40e_stat_update_and_clear32 - read and clear hw reg, update a 32 bit stat
  588. * @hw: ptr to the hardware info
  589. * @reg: the hw reg to read and clear
  590. * @stat: ptr to the stat
  591. **/
  592. static void i40e_stat_update_and_clear32(struct i40e_hw *hw, u32 reg, u64 *stat)
  593. {
  594. u32 new_data = rd32(hw, reg);
  595. wr32(hw, reg, 1); /* must write a nonzero value to clear register */
  596. *stat += new_data;
  597. }
  598. /**
  599. * i40e_stats_update_rx_discards - update rx_discards.
  600. * @vsi: ptr to the VSI to be updated.
  601. * @hw: ptr to the hardware info.
  602. * @stat_idx: VSI's stat_counter_idx.
  603. * @offset_loaded: ptr to the VSI's stat_offsets_loaded.
  604. * @stat_offset: ptr to stat_offset to store first read of specific register.
  605. * @stat: ptr to VSI's stat to be updated.
  606. **/
  607. static void
  608. i40e_stats_update_rx_discards(struct i40e_vsi *vsi, struct i40e_hw *hw,
  609. int stat_idx, bool offset_loaded,
  610. struct i40e_eth_stats *stat_offset,
  611. struct i40e_eth_stats *stat)
  612. {
  613. i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx), offset_loaded,
  614. &stat_offset->rx_discards, &stat->rx_discards);
  615. i40e_stat_update64(hw,
  616. I40E_GL_RXERR1H(i40e_compute_pci_to_hw_id(vsi, hw)),
  617. I40E_GL_RXERR1L(i40e_compute_pci_to_hw_id(vsi, hw)),
  618. offset_loaded, &stat_offset->rx_discards_other,
  619. &stat->rx_discards_other);
  620. }
  621. /**
  622. * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
  623. * @vsi: the VSI to be updated
  624. **/
  625. void i40e_update_eth_stats(struct i40e_vsi *vsi)
  626. {
  627. int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
  628. struct i40e_pf *pf = vsi->back;
  629. struct i40e_hw *hw = &pf->hw;
  630. struct i40e_eth_stats *oes;
  631. struct i40e_eth_stats *es; /* device's eth stats */
  632. es = &vsi->eth_stats;
  633. oes = &vsi->eth_stats_offsets;
  634. /* Gather up the stats that the hw collects */
  635. i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
  636. vsi->stat_offsets_loaded,
  637. &oes->tx_errors, &es->tx_errors);
  638. i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
  639. vsi->stat_offsets_loaded,
  640. &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
  641. i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
  642. I40E_GLV_GORCL(stat_idx),
  643. vsi->stat_offsets_loaded,
  644. &oes->rx_bytes, &es->rx_bytes);
  645. i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
  646. I40E_GLV_UPRCL(stat_idx),
  647. vsi->stat_offsets_loaded,
  648. &oes->rx_unicast, &es->rx_unicast);
  649. i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
  650. I40E_GLV_MPRCL(stat_idx),
  651. vsi->stat_offsets_loaded,
  652. &oes->rx_multicast, &es->rx_multicast);
  653. i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
  654. I40E_GLV_BPRCL(stat_idx),
  655. vsi->stat_offsets_loaded,
  656. &oes->rx_broadcast, &es->rx_broadcast);
  657. i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
  658. I40E_GLV_GOTCL(stat_idx),
  659. vsi->stat_offsets_loaded,
  660. &oes->tx_bytes, &es->tx_bytes);
  661. i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
  662. I40E_GLV_UPTCL(stat_idx),
  663. vsi->stat_offsets_loaded,
  664. &oes->tx_unicast, &es->tx_unicast);
  665. i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
  666. I40E_GLV_MPTCL(stat_idx),
  667. vsi->stat_offsets_loaded,
  668. &oes->tx_multicast, &es->tx_multicast);
  669. i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
  670. I40E_GLV_BPTCL(stat_idx),
  671. vsi->stat_offsets_loaded,
  672. &oes->tx_broadcast, &es->tx_broadcast);
  673. i40e_stats_update_rx_discards(vsi, hw, stat_idx,
  674. vsi->stat_offsets_loaded, oes, es);
  675. vsi->stat_offsets_loaded = true;
  676. }
  677. /**
  678. * i40e_update_veb_stats - Update Switch component statistics
  679. * @veb: the VEB being updated
  680. **/
  681. void i40e_update_veb_stats(struct i40e_veb *veb)
  682. {
  683. struct i40e_pf *pf = veb->pf;
  684. struct i40e_hw *hw = &pf->hw;
  685. struct i40e_eth_stats *oes;
  686. struct i40e_eth_stats *es; /* device's eth stats */
  687. struct i40e_veb_tc_stats *veb_oes;
  688. struct i40e_veb_tc_stats *veb_es;
  689. int i, idx = 0;
  690. idx = veb->stats_idx;
  691. es = &veb->stats;
  692. oes = &veb->stats_offsets;
  693. veb_es = &veb->tc_stats;
  694. veb_oes = &veb->tc_stats_offsets;
  695. /* Gather up the stats that the hw collects */
  696. i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
  697. veb->stat_offsets_loaded,
  698. &oes->tx_discards, &es->tx_discards);
  699. if (hw->revision_id > 0)
  700. i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
  701. veb->stat_offsets_loaded,
  702. &oes->rx_unknown_protocol,
  703. &es->rx_unknown_protocol);
  704. i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
  705. veb->stat_offsets_loaded,
  706. &oes->rx_bytes, &es->rx_bytes);
  707. i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
  708. veb->stat_offsets_loaded,
  709. &oes->rx_unicast, &es->rx_unicast);
  710. i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
  711. veb->stat_offsets_loaded,
  712. &oes->rx_multicast, &es->rx_multicast);
  713. i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
  714. veb->stat_offsets_loaded,
  715. &oes->rx_broadcast, &es->rx_broadcast);
  716. i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
  717. veb->stat_offsets_loaded,
  718. &oes->tx_bytes, &es->tx_bytes);
  719. i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
  720. veb->stat_offsets_loaded,
  721. &oes->tx_unicast, &es->tx_unicast);
  722. i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
  723. veb->stat_offsets_loaded,
  724. &oes->tx_multicast, &es->tx_multicast);
  725. i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
  726. veb->stat_offsets_loaded,
  727. &oes->tx_broadcast, &es->tx_broadcast);
  728. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  729. i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
  730. I40E_GLVEBTC_RPCL(i, idx),
  731. veb->stat_offsets_loaded,
  732. &veb_oes->tc_rx_packets[i],
  733. &veb_es->tc_rx_packets[i]);
  734. i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
  735. I40E_GLVEBTC_RBCL(i, idx),
  736. veb->stat_offsets_loaded,
  737. &veb_oes->tc_rx_bytes[i],
  738. &veb_es->tc_rx_bytes[i]);
  739. i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
  740. I40E_GLVEBTC_TPCL(i, idx),
  741. veb->stat_offsets_loaded,
  742. &veb_oes->tc_tx_packets[i],
  743. &veb_es->tc_tx_packets[i]);
  744. i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
  745. I40E_GLVEBTC_TBCL(i, idx),
  746. veb->stat_offsets_loaded,
  747. &veb_oes->tc_tx_bytes[i],
  748. &veb_es->tc_tx_bytes[i]);
  749. }
  750. veb->stat_offsets_loaded = true;
  751. }
  752. /**
  753. * i40e_update_vsi_stats - Update the vsi statistics counters.
  754. * @vsi: the VSI to be updated
  755. *
  756. * There are a few instances where we store the same stat in a
  757. * couple of different structs. This is partly because we have
  758. * the netdev stats that need to be filled out, which is slightly
  759. * different from the "eth_stats" defined by the chip and used in
  760. * VF communications. We sort it out here.
  761. **/
  762. static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
  763. {
  764. u64 rx_page, rx_buf, rx_reuse, rx_alloc, rx_waive, rx_busy;
  765. struct i40e_pf *pf = vsi->back;
  766. struct rtnl_link_stats64 *ons;
  767. struct rtnl_link_stats64 *ns; /* netdev stats */
  768. struct i40e_eth_stats *oes;
  769. struct i40e_eth_stats *es; /* device's eth stats */
  770. u64 tx_restart, tx_busy;
  771. struct i40e_ring *p;
  772. u64 bytes, packets;
  773. unsigned int start;
  774. u64 tx_linearize;
  775. u64 tx_force_wb;
  776. u64 tx_stopped;
  777. u64 rx_p, rx_b;
  778. u64 tx_p, tx_b;
  779. u16 q;
  780. if (test_bit(__I40E_VSI_DOWN, vsi->state) ||
  781. test_bit(__I40E_CONFIG_BUSY, pf->state))
  782. return;
  783. ns = i40e_get_vsi_stats_struct(vsi);
  784. ons = &vsi->net_stats_offsets;
  785. es = &vsi->eth_stats;
  786. oes = &vsi->eth_stats_offsets;
  787. /* Gather up the netdev and vsi stats that the driver collects
  788. * on the fly during packet processing
  789. */
  790. rx_b = rx_p = 0;
  791. tx_b = tx_p = 0;
  792. tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
  793. tx_stopped = 0;
  794. rx_page = 0;
  795. rx_buf = 0;
  796. rx_reuse = 0;
  797. rx_alloc = 0;
  798. rx_waive = 0;
  799. rx_busy = 0;
  800. rcu_read_lock();
  801. for (q = 0; q < vsi->num_queue_pairs; q++) {
  802. /* locate Tx ring */
  803. p = READ_ONCE(vsi->tx_rings[q]);
  804. if (!p)
  805. continue;
  806. do {
  807. start = u64_stats_fetch_begin(&p->syncp);
  808. packets = p->stats.packets;
  809. bytes = p->stats.bytes;
  810. } while (u64_stats_fetch_retry(&p->syncp, start));
  811. tx_b += bytes;
  812. tx_p += packets;
  813. tx_restart += p->tx_stats.restart_queue;
  814. tx_busy += p->tx_stats.tx_busy;
  815. tx_linearize += p->tx_stats.tx_linearize;
  816. tx_force_wb += p->tx_stats.tx_force_wb;
  817. tx_stopped += p->tx_stats.tx_stopped;
  818. /* locate Rx ring */
  819. p = READ_ONCE(vsi->rx_rings[q]);
  820. if (!p)
  821. continue;
  822. do {
  823. start = u64_stats_fetch_begin(&p->syncp);
  824. packets = p->stats.packets;
  825. bytes = p->stats.bytes;
  826. } while (u64_stats_fetch_retry(&p->syncp, start));
  827. rx_b += bytes;
  828. rx_p += packets;
  829. rx_buf += p->rx_stats.alloc_buff_failed;
  830. rx_page += p->rx_stats.alloc_page_failed;
  831. rx_reuse += p->rx_stats.page_reuse_count;
  832. rx_alloc += p->rx_stats.page_alloc_count;
  833. rx_waive += p->rx_stats.page_waive_count;
  834. rx_busy += p->rx_stats.page_busy_count;
  835. if (i40e_enabled_xdp_vsi(vsi)) {
  836. /* locate XDP ring */
  837. p = READ_ONCE(vsi->xdp_rings[q]);
  838. if (!p)
  839. continue;
  840. do {
  841. start = u64_stats_fetch_begin(&p->syncp);
  842. packets = p->stats.packets;
  843. bytes = p->stats.bytes;
  844. } while (u64_stats_fetch_retry(&p->syncp, start));
  845. tx_b += bytes;
  846. tx_p += packets;
  847. tx_restart += p->tx_stats.restart_queue;
  848. tx_busy += p->tx_stats.tx_busy;
  849. tx_linearize += p->tx_stats.tx_linearize;
  850. tx_force_wb += p->tx_stats.tx_force_wb;
  851. }
  852. }
  853. rcu_read_unlock();
  854. vsi->tx_restart = tx_restart;
  855. vsi->tx_busy = tx_busy;
  856. vsi->tx_linearize = tx_linearize;
  857. vsi->tx_force_wb = tx_force_wb;
  858. vsi->tx_stopped = tx_stopped;
  859. vsi->rx_page_failed = rx_page;
  860. vsi->rx_buf_failed = rx_buf;
  861. vsi->rx_page_reuse = rx_reuse;
  862. vsi->rx_page_alloc = rx_alloc;
  863. vsi->rx_page_waive = rx_waive;
  864. vsi->rx_page_busy = rx_busy;
  865. ns->rx_packets = rx_p;
  866. ns->rx_bytes = rx_b;
  867. ns->tx_packets = tx_p;
  868. ns->tx_bytes = tx_b;
  869. /* update netdev stats from eth stats */
  870. i40e_update_eth_stats(vsi);
  871. ons->tx_errors = oes->tx_errors;
  872. ns->tx_errors = es->tx_errors;
  873. ons->multicast = oes->rx_multicast;
  874. ns->multicast = es->rx_multicast;
  875. ons->rx_dropped = oes->rx_discards_other;
  876. ns->rx_dropped = es->rx_discards_other;
  877. ons->rx_missed_errors = oes->rx_discards;
  878. ns->rx_missed_errors = es->rx_discards;
  879. ons->tx_dropped = oes->tx_discards;
  880. ns->tx_dropped = es->tx_discards;
  881. /* pull in a couple PF stats if this is the main vsi */
  882. if (vsi->type == I40E_VSI_MAIN) {
  883. ns->rx_crc_errors = pf->stats.crc_errors;
  884. ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
  885. ns->rx_length_errors = pf->stats.rx_length_errors;
  886. }
  887. }
  888. /**
  889. * i40e_update_pf_stats - Update the PF statistics counters.
  890. * @pf: the PF to be updated
  891. **/
  892. static void i40e_update_pf_stats(struct i40e_pf *pf)
  893. {
  894. struct i40e_hw_port_stats *osd = &pf->stats_offsets;
  895. struct i40e_hw_port_stats *nsd = &pf->stats;
  896. struct i40e_hw *hw = &pf->hw;
  897. u32 val;
  898. int i;
  899. i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
  900. I40E_GLPRT_GORCL(hw->port),
  901. pf->stat_offsets_loaded,
  902. &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
  903. i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
  904. I40E_GLPRT_GOTCL(hw->port),
  905. pf->stat_offsets_loaded,
  906. &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
  907. i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
  908. pf->stat_offsets_loaded,
  909. &osd->eth.rx_discards,
  910. &nsd->eth.rx_discards);
  911. i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
  912. I40E_GLPRT_UPRCL(hw->port),
  913. pf->stat_offsets_loaded,
  914. &osd->eth.rx_unicast,
  915. &nsd->eth.rx_unicast);
  916. i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
  917. I40E_GLPRT_MPRCL(hw->port),
  918. pf->stat_offsets_loaded,
  919. &osd->eth.rx_multicast,
  920. &nsd->eth.rx_multicast);
  921. i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
  922. I40E_GLPRT_BPRCL(hw->port),
  923. pf->stat_offsets_loaded,
  924. &osd->eth.rx_broadcast,
  925. &nsd->eth.rx_broadcast);
  926. i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
  927. I40E_GLPRT_UPTCL(hw->port),
  928. pf->stat_offsets_loaded,
  929. &osd->eth.tx_unicast,
  930. &nsd->eth.tx_unicast);
  931. i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
  932. I40E_GLPRT_MPTCL(hw->port),
  933. pf->stat_offsets_loaded,
  934. &osd->eth.tx_multicast,
  935. &nsd->eth.tx_multicast);
  936. i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
  937. I40E_GLPRT_BPTCL(hw->port),
  938. pf->stat_offsets_loaded,
  939. &osd->eth.tx_broadcast,
  940. &nsd->eth.tx_broadcast);
  941. i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
  942. pf->stat_offsets_loaded,
  943. &osd->tx_dropped_link_down,
  944. &nsd->tx_dropped_link_down);
  945. i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
  946. pf->stat_offsets_loaded,
  947. &osd->crc_errors, &nsd->crc_errors);
  948. i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
  949. pf->stat_offsets_loaded,
  950. &osd->illegal_bytes, &nsd->illegal_bytes);
  951. i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
  952. pf->stat_offsets_loaded,
  953. &osd->mac_local_faults,
  954. &nsd->mac_local_faults);
  955. i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
  956. pf->stat_offsets_loaded,
  957. &osd->mac_remote_faults,
  958. &nsd->mac_remote_faults);
  959. i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
  960. pf->stat_offsets_loaded,
  961. &osd->rx_length_errors,
  962. &nsd->rx_length_errors);
  963. i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
  964. pf->stat_offsets_loaded,
  965. &osd->link_xon_rx, &nsd->link_xon_rx);
  966. i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
  967. pf->stat_offsets_loaded,
  968. &osd->link_xon_tx, &nsd->link_xon_tx);
  969. i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
  970. pf->stat_offsets_loaded,
  971. &osd->link_xoff_rx, &nsd->link_xoff_rx);
  972. i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
  973. pf->stat_offsets_loaded,
  974. &osd->link_xoff_tx, &nsd->link_xoff_tx);
  975. for (i = 0; i < 8; i++) {
  976. i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
  977. pf->stat_offsets_loaded,
  978. &osd->priority_xoff_rx[i],
  979. &nsd->priority_xoff_rx[i]);
  980. i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
  981. pf->stat_offsets_loaded,
  982. &osd->priority_xon_rx[i],
  983. &nsd->priority_xon_rx[i]);
  984. i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
  985. pf->stat_offsets_loaded,
  986. &osd->priority_xon_tx[i],
  987. &nsd->priority_xon_tx[i]);
  988. i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
  989. pf->stat_offsets_loaded,
  990. &osd->priority_xoff_tx[i],
  991. &nsd->priority_xoff_tx[i]);
  992. i40e_stat_update32(hw,
  993. I40E_GLPRT_RXON2OFFCNT(hw->port, i),
  994. pf->stat_offsets_loaded,
  995. &osd->priority_xon_2_xoff[i],
  996. &nsd->priority_xon_2_xoff[i]);
  997. }
  998. i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
  999. I40E_GLPRT_PRC64L(hw->port),
  1000. pf->stat_offsets_loaded,
  1001. &osd->rx_size_64, &nsd->rx_size_64);
  1002. i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
  1003. I40E_GLPRT_PRC127L(hw->port),
  1004. pf->stat_offsets_loaded,
  1005. &osd->rx_size_127, &nsd->rx_size_127);
  1006. i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
  1007. I40E_GLPRT_PRC255L(hw->port),
  1008. pf->stat_offsets_loaded,
  1009. &osd->rx_size_255, &nsd->rx_size_255);
  1010. i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
  1011. I40E_GLPRT_PRC511L(hw->port),
  1012. pf->stat_offsets_loaded,
  1013. &osd->rx_size_511, &nsd->rx_size_511);
  1014. i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
  1015. I40E_GLPRT_PRC1023L(hw->port),
  1016. pf->stat_offsets_loaded,
  1017. &osd->rx_size_1023, &nsd->rx_size_1023);
  1018. i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
  1019. I40E_GLPRT_PRC1522L(hw->port),
  1020. pf->stat_offsets_loaded,
  1021. &osd->rx_size_1522, &nsd->rx_size_1522);
  1022. i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
  1023. I40E_GLPRT_PRC9522L(hw->port),
  1024. pf->stat_offsets_loaded,
  1025. &osd->rx_size_big, &nsd->rx_size_big);
  1026. i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
  1027. I40E_GLPRT_PTC64L(hw->port),
  1028. pf->stat_offsets_loaded,
  1029. &osd->tx_size_64, &nsd->tx_size_64);
  1030. i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
  1031. I40E_GLPRT_PTC127L(hw->port),
  1032. pf->stat_offsets_loaded,
  1033. &osd->tx_size_127, &nsd->tx_size_127);
  1034. i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
  1035. I40E_GLPRT_PTC255L(hw->port),
  1036. pf->stat_offsets_loaded,
  1037. &osd->tx_size_255, &nsd->tx_size_255);
  1038. i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
  1039. I40E_GLPRT_PTC511L(hw->port),
  1040. pf->stat_offsets_loaded,
  1041. &osd->tx_size_511, &nsd->tx_size_511);
  1042. i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
  1043. I40E_GLPRT_PTC1023L(hw->port),
  1044. pf->stat_offsets_loaded,
  1045. &osd->tx_size_1023, &nsd->tx_size_1023);
  1046. i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
  1047. I40E_GLPRT_PTC1522L(hw->port),
  1048. pf->stat_offsets_loaded,
  1049. &osd->tx_size_1522, &nsd->tx_size_1522);
  1050. i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
  1051. I40E_GLPRT_PTC9522L(hw->port),
  1052. pf->stat_offsets_loaded,
  1053. &osd->tx_size_big, &nsd->tx_size_big);
  1054. i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
  1055. pf->stat_offsets_loaded,
  1056. &osd->rx_undersize, &nsd->rx_undersize);
  1057. i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
  1058. pf->stat_offsets_loaded,
  1059. &osd->rx_fragments, &nsd->rx_fragments);
  1060. i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
  1061. pf->stat_offsets_loaded,
  1062. &osd->rx_oversize, &nsd->rx_oversize);
  1063. i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
  1064. pf->stat_offsets_loaded,
  1065. &osd->rx_jabber, &nsd->rx_jabber);
  1066. /* FDIR stats */
  1067. i40e_stat_update_and_clear32(hw,
  1068. I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(hw->pf_id)),
  1069. &nsd->fd_atr_match);
  1070. i40e_stat_update_and_clear32(hw,
  1071. I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(hw->pf_id)),
  1072. &nsd->fd_sb_match);
  1073. i40e_stat_update_and_clear32(hw,
  1074. I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(hw->pf_id)),
  1075. &nsd->fd_atr_tunnel_match);
  1076. val = rd32(hw, I40E_PRTPM_EEE_STAT);
  1077. nsd->tx_lpi_status =
  1078. FIELD_GET(I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK, val);
  1079. nsd->rx_lpi_status =
  1080. FIELD_GET(I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK, val);
  1081. i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
  1082. pf->stat_offsets_loaded,
  1083. &osd->tx_lpi_count, &nsd->tx_lpi_count);
  1084. i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
  1085. pf->stat_offsets_loaded,
  1086. &osd->rx_lpi_count, &nsd->rx_lpi_count);
  1087. if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags) &&
  1088. !test_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
  1089. nsd->fd_sb_status = true;
  1090. else
  1091. nsd->fd_sb_status = false;
  1092. if (test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags) &&
  1093. !test_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
  1094. nsd->fd_atr_status = true;
  1095. else
  1096. nsd->fd_atr_status = false;
  1097. pf->stat_offsets_loaded = true;
  1098. }
  1099. /**
  1100. * i40e_update_stats - Update the various statistics counters.
  1101. * @vsi: the VSI to be updated
  1102. *
  1103. * Update the various stats for this VSI and its related entities.
  1104. **/
  1105. void i40e_update_stats(struct i40e_vsi *vsi)
  1106. {
  1107. struct i40e_pf *pf = vsi->back;
  1108. if (vsi->type == I40E_VSI_MAIN)
  1109. i40e_update_pf_stats(pf);
  1110. i40e_update_vsi_stats(vsi);
  1111. }
  1112. /**
  1113. * i40e_count_all_filters - counts VSI MAC filters
  1114. * @vsi: the VSI to be searched
  1115. *
  1116. * Return: count of MAC filters in any state.
  1117. */
  1118. int i40e_count_all_filters(struct i40e_vsi *vsi)
  1119. {
  1120. struct i40e_mac_filter *f;
  1121. struct hlist_node *h;
  1122. int bkt, cnt = 0;
  1123. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
  1124. cnt++;
  1125. return cnt;
  1126. }
  1127. /**
  1128. * i40e_count_active_filters - counts VSI MAC filters
  1129. * @vsi: the VSI to be searched
  1130. *
  1131. * Return: count of active MAC filters.
  1132. */
  1133. int i40e_count_active_filters(struct i40e_vsi *vsi)
  1134. {
  1135. struct i40e_mac_filter *f;
  1136. struct hlist_node *h;
  1137. int bkt;
  1138. int cnt = 0;
  1139. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  1140. if (f->state == I40E_FILTER_NEW ||
  1141. f->state == I40E_FILTER_NEW_SYNC ||
  1142. f->state == I40E_FILTER_ACTIVE)
  1143. ++cnt;
  1144. }
  1145. return cnt;
  1146. }
  1147. /**
  1148. * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
  1149. * @vsi: the VSI to be searched
  1150. * @macaddr: the MAC address
  1151. * @vlan: the vlan
  1152. *
  1153. * Returns ptr to the filter object or NULL
  1154. **/
  1155. static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
  1156. const u8 *macaddr, s16 vlan)
  1157. {
  1158. struct i40e_mac_filter *f;
  1159. u64 key;
  1160. if (!vsi || !macaddr)
  1161. return NULL;
  1162. key = i40e_addr_to_hkey(macaddr);
  1163. hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
  1164. if ((ether_addr_equal(macaddr, f->macaddr)) &&
  1165. (vlan == f->vlan))
  1166. return f;
  1167. }
  1168. return NULL;
  1169. }
  1170. /**
  1171. * i40e_find_mac - Find a mac addr in the macvlan filters list
  1172. * @vsi: the VSI to be searched
  1173. * @macaddr: the MAC address we are searching for
  1174. *
  1175. * Returns the first filter with the provided MAC address or NULL if
  1176. * MAC address was not found
  1177. **/
  1178. struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
  1179. {
  1180. struct i40e_mac_filter *f;
  1181. u64 key;
  1182. if (!vsi || !macaddr)
  1183. return NULL;
  1184. key = i40e_addr_to_hkey(macaddr);
  1185. hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
  1186. if ((ether_addr_equal(macaddr, f->macaddr)))
  1187. return f;
  1188. }
  1189. return NULL;
  1190. }
  1191. /**
  1192. * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
  1193. * @vsi: the VSI to be searched
  1194. *
  1195. * Returns true if VSI is in vlan mode or false otherwise
  1196. **/
  1197. bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
  1198. {
  1199. /* If we have a PVID, always operate in VLAN mode */
  1200. if (vsi->info.pvid)
  1201. return true;
  1202. /* We need to operate in VLAN mode whenever we have any filters with
  1203. * a VLAN other than I40E_VLAN_ALL. We could check the table each
  1204. * time, incurring search cost repeatedly. However, we can notice two
  1205. * things:
  1206. *
  1207. * 1) the only place where we can gain a VLAN filter is in
  1208. * i40e_add_filter.
  1209. *
  1210. * 2) the only place where filters are actually removed is in
  1211. * i40e_sync_filters_subtask.
  1212. *
  1213. * Thus, we can simply use a boolean value, has_vlan_filters which we
  1214. * will set to true when we add a VLAN filter in i40e_add_filter. Then
  1215. * we have to perform the full search after deleting filters in
  1216. * i40e_sync_filters_subtask, but we already have to search
  1217. * filters here and can perform the check at the same time. This
  1218. * results in avoiding embedding a loop for VLAN mode inside another
  1219. * loop over all the filters, and should maintain correctness as noted
  1220. * above.
  1221. */
  1222. return vsi->has_vlan_filter;
  1223. }
  1224. /**
  1225. * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
  1226. * @vsi: the VSI to configure
  1227. * @tmp_add_list: list of filters ready to be added
  1228. * @tmp_del_list: list of filters ready to be deleted
  1229. * @vlan_filters: the number of active VLAN filters
  1230. *
  1231. * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
  1232. * behave as expected. If we have any active VLAN filters remaining or about
  1233. * to be added then we need to update non-VLAN filters to be marked as VLAN=0
  1234. * so that they only match against untagged traffic. If we no longer have any
  1235. * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
  1236. * so that they match against both tagged and untagged traffic. In this way,
  1237. * we ensure that we correctly receive the desired traffic. This ensures that
  1238. * when we have an active VLAN we will receive only untagged traffic and
  1239. * traffic matching active VLANs. If we have no active VLANs then we will
  1240. * operate in non-VLAN mode and receive all traffic, tagged or untagged.
  1241. *
  1242. * Finally, in a similar fashion, this function also corrects filters when
  1243. * there is an active PVID assigned to this VSI.
  1244. *
  1245. * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
  1246. *
  1247. * This function is only expected to be called from within
  1248. * i40e_sync_vsi_filters.
  1249. *
  1250. * NOTE: This function expects to be called while under the
  1251. * mac_filter_hash_lock
  1252. */
  1253. static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
  1254. struct hlist_head *tmp_add_list,
  1255. struct hlist_head *tmp_del_list,
  1256. int vlan_filters)
  1257. {
  1258. s16 pvid = le16_to_cpu(vsi->info.pvid);
  1259. struct i40e_mac_filter *f, *add_head;
  1260. struct i40e_new_mac_filter *new;
  1261. struct hlist_node *h;
  1262. int bkt, new_vlan;
  1263. /* To determine if a particular filter needs to be replaced we
  1264. * have the three following conditions:
  1265. *
  1266. * a) if we have a PVID assigned, then all filters which are
  1267. * not marked as VLAN=PVID must be replaced with filters that
  1268. * are.
  1269. * b) otherwise, if we have any active VLANS, all filters
  1270. * which are marked as VLAN=-1 must be replaced with
  1271. * filters marked as VLAN=0
  1272. * c) finally, if we do not have any active VLANS, all filters
  1273. * which are marked as VLAN=0 must be replaced with filters
  1274. * marked as VLAN=-1
  1275. */
  1276. /* Update the filters about to be added in place */
  1277. hlist_for_each_entry(new, tmp_add_list, hlist) {
  1278. if (pvid && new->f->vlan != pvid)
  1279. new->f->vlan = pvid;
  1280. else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
  1281. new->f->vlan = 0;
  1282. else if (!vlan_filters && new->f->vlan == 0)
  1283. new->f->vlan = I40E_VLAN_ANY;
  1284. }
  1285. /* Update the remaining active filters */
  1286. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  1287. /* Combine the checks for whether a filter needs to be changed
  1288. * and then determine the new VLAN inside the if block, in
  1289. * order to avoid duplicating code for adding the new filter
  1290. * then deleting the old filter.
  1291. */
  1292. if ((pvid && f->vlan != pvid) ||
  1293. (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
  1294. (!vlan_filters && f->vlan == 0)) {
  1295. /* Determine the new vlan we will be adding */
  1296. if (pvid)
  1297. new_vlan = pvid;
  1298. else if (vlan_filters)
  1299. new_vlan = 0;
  1300. else
  1301. new_vlan = I40E_VLAN_ANY;
  1302. /* Create the new filter */
  1303. add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
  1304. if (!add_head)
  1305. return -ENOMEM;
  1306. /* Create a temporary i40e_new_mac_filter */
  1307. new = kzalloc(sizeof(*new), GFP_ATOMIC);
  1308. if (!new)
  1309. return -ENOMEM;
  1310. new->f = add_head;
  1311. new->state = add_head->state;
  1312. if (add_head->state == I40E_FILTER_NEW)
  1313. add_head->state = I40E_FILTER_NEW_SYNC;
  1314. /* Add the new filter to the tmp list */
  1315. hlist_add_head(&new->hlist, tmp_add_list);
  1316. /* Put the original filter into the delete list */
  1317. f->state = I40E_FILTER_REMOVE;
  1318. hash_del(&f->hlist);
  1319. hlist_add_head(&f->hlist, tmp_del_list);
  1320. }
  1321. }
  1322. vsi->has_vlan_filter = !!vlan_filters;
  1323. return 0;
  1324. }
  1325. /**
  1326. * i40e_get_vf_new_vlan - Get new vlan id on a vf
  1327. * @vsi: the vsi to configure
  1328. * @new_mac: new mac filter to be added
  1329. * @f: existing mac filter, replaced with new_mac->f if new_mac is not NULL
  1330. * @vlan_filters: the number of active VLAN filters
  1331. * @trusted: flag if the VF is trusted
  1332. *
  1333. * Get new VLAN id based on current VLAN filters, trust, PVID
  1334. * and vf-vlan-prune-disable flag.
  1335. *
  1336. * Returns the value of the new vlan filter or
  1337. * the old value if no new filter is needed.
  1338. */
  1339. static s16 i40e_get_vf_new_vlan(struct i40e_vsi *vsi,
  1340. struct i40e_new_mac_filter *new_mac,
  1341. struct i40e_mac_filter *f,
  1342. int vlan_filters,
  1343. bool trusted)
  1344. {
  1345. s16 pvid = le16_to_cpu(vsi->info.pvid);
  1346. struct i40e_pf *pf = vsi->back;
  1347. bool is_any;
  1348. if (new_mac)
  1349. f = new_mac->f;
  1350. if (pvid && f->vlan != pvid)
  1351. return pvid;
  1352. is_any = (trusted ||
  1353. !test_bit(I40E_FLAG_VF_VLAN_PRUNING_ENA, pf->flags));
  1354. if ((vlan_filters && f->vlan == I40E_VLAN_ANY) ||
  1355. (!is_any && !vlan_filters && f->vlan == I40E_VLAN_ANY) ||
  1356. (is_any && !vlan_filters && f->vlan == 0)) {
  1357. if (is_any)
  1358. return I40E_VLAN_ANY;
  1359. else
  1360. return 0;
  1361. }
  1362. return f->vlan;
  1363. }
  1364. /**
  1365. * i40e_correct_vf_mac_vlan_filters - Correct non-VLAN VF filters if necessary
  1366. * @vsi: the vsi to configure
  1367. * @tmp_add_list: list of filters ready to be added
  1368. * @tmp_del_list: list of filters ready to be deleted
  1369. * @vlan_filters: the number of active VLAN filters
  1370. * @trusted: flag if the VF is trusted
  1371. *
  1372. * Correct VF VLAN filters based on current VLAN filters, trust, PVID
  1373. * and vf-vlan-prune-disable flag.
  1374. *
  1375. * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
  1376. *
  1377. * This function is only expected to be called from within
  1378. * i40e_sync_vsi_filters.
  1379. *
  1380. * NOTE: This function expects to be called while under the
  1381. * mac_filter_hash_lock
  1382. */
  1383. static int i40e_correct_vf_mac_vlan_filters(struct i40e_vsi *vsi,
  1384. struct hlist_head *tmp_add_list,
  1385. struct hlist_head *tmp_del_list,
  1386. int vlan_filters,
  1387. bool trusted)
  1388. {
  1389. struct i40e_mac_filter *f, *add_head;
  1390. struct i40e_new_mac_filter *new_mac;
  1391. struct hlist_node *h;
  1392. int bkt, new_vlan;
  1393. hlist_for_each_entry(new_mac, tmp_add_list, hlist) {
  1394. new_mac->f->vlan = i40e_get_vf_new_vlan(vsi, new_mac, NULL,
  1395. vlan_filters, trusted);
  1396. }
  1397. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  1398. new_vlan = i40e_get_vf_new_vlan(vsi, NULL, f, vlan_filters,
  1399. trusted);
  1400. if (new_vlan != f->vlan) {
  1401. add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
  1402. if (!add_head)
  1403. return -ENOMEM;
  1404. /* Create a temporary i40e_new_mac_filter */
  1405. new_mac = kzalloc(sizeof(*new_mac), GFP_ATOMIC);
  1406. if (!new_mac)
  1407. return -ENOMEM;
  1408. new_mac->f = add_head;
  1409. new_mac->state = add_head->state;
  1410. if (add_head->state == I40E_FILTER_NEW)
  1411. add_head->state = I40E_FILTER_NEW_SYNC;
  1412. /* Add the new filter to the tmp list */
  1413. hlist_add_head(&new_mac->hlist, tmp_add_list);
  1414. /* Put the original filter into the delete list */
  1415. f->state = I40E_FILTER_REMOVE;
  1416. hash_del(&f->hlist);
  1417. hlist_add_head(&f->hlist, tmp_del_list);
  1418. }
  1419. }
  1420. vsi->has_vlan_filter = !!vlan_filters;
  1421. return 0;
  1422. }
  1423. /**
  1424. * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
  1425. * @vsi: the PF Main VSI - inappropriate for any other VSI
  1426. * @macaddr: the MAC address
  1427. *
  1428. * Remove whatever filter the firmware set up so the driver can manage
  1429. * its own filtering intelligently.
  1430. **/
  1431. static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
  1432. {
  1433. struct i40e_aqc_remove_macvlan_element_data element;
  1434. struct i40e_pf *pf = vsi->back;
  1435. /* Only appropriate for the PF main VSI */
  1436. if (vsi->type != I40E_VSI_MAIN)
  1437. return;
  1438. memset(&element, 0, sizeof(element));
  1439. ether_addr_copy(element.mac_addr, macaddr);
  1440. element.vlan_tag = 0;
  1441. /* Ignore error returns, some firmware does it this way... */
  1442. element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
  1443. i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
  1444. memset(&element, 0, sizeof(element));
  1445. ether_addr_copy(element.mac_addr, macaddr);
  1446. element.vlan_tag = 0;
  1447. /* ...and some firmware does it this way. */
  1448. element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
  1449. I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
  1450. i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
  1451. }
  1452. /**
  1453. * i40e_add_filter - Add a mac/vlan filter to the VSI
  1454. * @vsi: the VSI to be searched
  1455. * @macaddr: the MAC address
  1456. * @vlan: the vlan
  1457. *
  1458. * Returns ptr to the filter object or NULL when no memory available.
  1459. *
  1460. * NOTE: This function is expected to be called with mac_filter_hash_lock
  1461. * being held.
  1462. **/
  1463. struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
  1464. const u8 *macaddr, s16 vlan)
  1465. {
  1466. struct i40e_mac_filter *f;
  1467. u64 key;
  1468. if (!vsi || !macaddr)
  1469. return NULL;
  1470. f = i40e_find_filter(vsi, macaddr, vlan);
  1471. if (!f) {
  1472. f = kzalloc(sizeof(*f), GFP_ATOMIC);
  1473. if (!f)
  1474. return NULL;
  1475. /* Update the boolean indicating if we need to function in
  1476. * VLAN mode.
  1477. */
  1478. if (vlan >= 0)
  1479. vsi->has_vlan_filter = true;
  1480. ether_addr_copy(f->macaddr, macaddr);
  1481. f->vlan = vlan;
  1482. f->state = I40E_FILTER_NEW;
  1483. INIT_HLIST_NODE(&f->hlist);
  1484. key = i40e_addr_to_hkey(macaddr);
  1485. hash_add(vsi->mac_filter_hash, &f->hlist, key);
  1486. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  1487. set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
  1488. }
  1489. /* If we're asked to add a filter that has been marked for removal, it
  1490. * is safe to simply restore it to active state. __i40e_del_filter
  1491. * will have simply deleted any filters which were previously marked
  1492. * NEW or FAILED, so if it is currently marked REMOVE it must have
  1493. * previously been ACTIVE. Since we haven't yet run the sync filters
  1494. * task, just restore this filter to the ACTIVE state so that the
  1495. * sync task leaves it in place
  1496. */
  1497. if (f->state == I40E_FILTER_REMOVE)
  1498. f->state = I40E_FILTER_ACTIVE;
  1499. return f;
  1500. }
  1501. /**
  1502. * __i40e_del_filter - Remove a specific filter from the VSI
  1503. * @vsi: VSI to remove from
  1504. * @f: the filter to remove from the list
  1505. *
  1506. * This function should be called instead of i40e_del_filter only if you know
  1507. * the exact filter you will remove already, such as via i40e_find_filter or
  1508. * i40e_find_mac.
  1509. *
  1510. * NOTE: This function is expected to be called with mac_filter_hash_lock
  1511. * being held.
  1512. * ANOTHER NOTE: This function MUST be called from within the context of
  1513. * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
  1514. * instead of list_for_each_entry().
  1515. **/
  1516. void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
  1517. {
  1518. if (!f)
  1519. return;
  1520. /* If the filter was never added to firmware then we can just delete it
  1521. * directly and we don't want to set the status to remove or else an
  1522. * admin queue command will unnecessarily fire.
  1523. */
  1524. if ((f->state == I40E_FILTER_FAILED) ||
  1525. (f->state == I40E_FILTER_NEW)) {
  1526. hash_del(&f->hlist);
  1527. kfree(f);
  1528. } else {
  1529. f->state = I40E_FILTER_REMOVE;
  1530. }
  1531. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  1532. set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
  1533. }
  1534. /**
  1535. * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
  1536. * @vsi: the VSI to be searched
  1537. * @macaddr: the MAC address
  1538. * @vlan: the VLAN
  1539. *
  1540. * NOTE: This function is expected to be called with mac_filter_hash_lock
  1541. * being held.
  1542. * ANOTHER NOTE: This function MUST be called from within the context of
  1543. * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
  1544. * instead of list_for_each_entry().
  1545. **/
  1546. void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
  1547. {
  1548. struct i40e_mac_filter *f;
  1549. if (!vsi || !macaddr)
  1550. return;
  1551. f = i40e_find_filter(vsi, macaddr, vlan);
  1552. __i40e_del_filter(vsi, f);
  1553. }
  1554. /**
  1555. * i40e_add_mac_filter - Add a MAC filter for all active VLANs
  1556. * @vsi: the VSI to be searched
  1557. * @macaddr: the mac address to be filtered
  1558. *
  1559. * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
  1560. * go through all the macvlan filters and add a macvlan filter for each
  1561. * unique vlan that already exists. If a PVID has been assigned, instead only
  1562. * add the macaddr to that VLAN.
  1563. *
  1564. * Returns last filter added on success, else NULL
  1565. **/
  1566. struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
  1567. const u8 *macaddr)
  1568. {
  1569. struct i40e_mac_filter *f, *add = NULL;
  1570. struct hlist_node *h;
  1571. int bkt;
  1572. lockdep_assert_held(&vsi->mac_filter_hash_lock);
  1573. if (vsi->info.pvid)
  1574. return i40e_add_filter(vsi, macaddr,
  1575. le16_to_cpu(vsi->info.pvid));
  1576. if (!i40e_is_vsi_in_vlan(vsi))
  1577. return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
  1578. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  1579. if (f->state == I40E_FILTER_REMOVE)
  1580. continue;
  1581. add = i40e_add_filter(vsi, macaddr, f->vlan);
  1582. if (!add)
  1583. return NULL;
  1584. }
  1585. return add;
  1586. }
  1587. /**
  1588. * i40e_del_mac_filter - Remove a MAC filter from all VLANs
  1589. * @vsi: the VSI to be searched
  1590. * @macaddr: the mac address to be removed
  1591. *
  1592. * Removes a given MAC address from a VSI regardless of what VLAN it has been
  1593. * associated with.
  1594. *
  1595. * Returns 0 for success, or error
  1596. **/
  1597. int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
  1598. {
  1599. struct i40e_mac_filter *f;
  1600. struct hlist_node *h;
  1601. bool found = false;
  1602. int bkt;
  1603. lockdep_assert_held(&vsi->mac_filter_hash_lock);
  1604. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  1605. if (ether_addr_equal(macaddr, f->macaddr)) {
  1606. __i40e_del_filter(vsi, f);
  1607. found = true;
  1608. }
  1609. }
  1610. if (found)
  1611. return 0;
  1612. else
  1613. return -ENOENT;
  1614. }
  1615. /**
  1616. * i40e_set_mac - NDO callback to set mac address
  1617. * @netdev: network interface device structure
  1618. * @p: pointer to an address structure
  1619. *
  1620. * Returns 0 on success, negative on failure
  1621. **/
  1622. static int i40e_set_mac(struct net_device *netdev, void *p)
  1623. {
  1624. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1625. struct i40e_vsi *vsi = np->vsi;
  1626. struct i40e_pf *pf = vsi->back;
  1627. struct i40e_hw *hw = &pf->hw;
  1628. struct sockaddr *addr = p;
  1629. if (!is_valid_ether_addr(addr->sa_data))
  1630. return -EADDRNOTAVAIL;
  1631. if (test_bit(__I40E_DOWN, pf->state) ||
  1632. test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
  1633. return -EADDRNOTAVAIL;
  1634. if (ether_addr_equal(hw->mac.addr, addr->sa_data))
  1635. netdev_info(netdev, "returning to hw mac address %pM\n",
  1636. hw->mac.addr);
  1637. else
  1638. netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
  1639. /* Copy the address first, so that we avoid a possible race with
  1640. * .set_rx_mode().
  1641. * - Remove old address from MAC filter
  1642. * - Copy new address
  1643. * - Add new address to MAC filter
  1644. */
  1645. spin_lock_bh(&vsi->mac_filter_hash_lock);
  1646. i40e_del_mac_filter(vsi, netdev->dev_addr);
  1647. eth_hw_addr_set(netdev, addr->sa_data);
  1648. i40e_add_mac_filter(vsi, netdev->dev_addr);
  1649. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  1650. if (vsi->type == I40E_VSI_MAIN) {
  1651. int ret;
  1652. ret = i40e_aq_mac_address_write(hw, I40E_AQC_WRITE_TYPE_LAA_WOL,
  1653. addr->sa_data, NULL);
  1654. if (ret)
  1655. netdev_info(netdev, "Ignoring error from firmware on LAA update, status %pe, AQ ret %s\n",
  1656. ERR_PTR(ret),
  1657. i40e_aq_str(hw, hw->aq.asq_last_status));
  1658. }
  1659. /* schedule our worker thread which will take care of
  1660. * applying the new filter changes
  1661. */
  1662. i40e_service_event_schedule(pf);
  1663. return 0;
  1664. }
  1665. /**
  1666. * i40e_config_rss_aq - Prepare for RSS using AQ commands
  1667. * @vsi: vsi structure
  1668. * @seed: RSS hash seed
  1669. * @lut: pointer to lookup table of lut_size
  1670. * @lut_size: size of the lookup table
  1671. **/
  1672. static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
  1673. u8 *lut, u16 lut_size)
  1674. {
  1675. struct i40e_pf *pf = vsi->back;
  1676. struct i40e_hw *hw = &pf->hw;
  1677. int ret = 0;
  1678. if (seed) {
  1679. struct i40e_aqc_get_set_rss_key_data *seed_dw =
  1680. (struct i40e_aqc_get_set_rss_key_data *)seed;
  1681. ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
  1682. if (ret) {
  1683. dev_info(&pf->pdev->dev,
  1684. "Cannot set RSS key, err %pe aq_err %s\n",
  1685. ERR_PTR(ret),
  1686. i40e_aq_str(hw, hw->aq.asq_last_status));
  1687. return ret;
  1688. }
  1689. }
  1690. if (lut) {
  1691. bool pf_lut = vsi->type == I40E_VSI_MAIN;
  1692. ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
  1693. if (ret) {
  1694. dev_info(&pf->pdev->dev,
  1695. "Cannot set RSS lut, err %pe aq_err %s\n",
  1696. ERR_PTR(ret),
  1697. i40e_aq_str(hw, hw->aq.asq_last_status));
  1698. return ret;
  1699. }
  1700. }
  1701. return ret;
  1702. }
  1703. /**
  1704. * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
  1705. * @vsi: VSI structure
  1706. **/
  1707. static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
  1708. {
  1709. struct i40e_pf *pf = vsi->back;
  1710. u8 seed[I40E_HKEY_ARRAY_SIZE];
  1711. u8 *lut;
  1712. int ret;
  1713. if (!test_bit(I40E_HW_CAP_RSS_AQ, pf->hw.caps))
  1714. return 0;
  1715. if (!vsi->rss_size)
  1716. vsi->rss_size = min_t(int, pf->alloc_rss_size,
  1717. vsi->num_queue_pairs);
  1718. if (!vsi->rss_size)
  1719. return -EINVAL;
  1720. lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
  1721. if (!lut)
  1722. return -ENOMEM;
  1723. /* Use the user configured hash keys and lookup table if there is one,
  1724. * otherwise use default
  1725. */
  1726. if (vsi->rss_lut_user)
  1727. memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
  1728. else
  1729. i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
  1730. if (vsi->rss_hkey_user)
  1731. memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
  1732. else
  1733. netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
  1734. ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
  1735. kfree(lut);
  1736. return ret;
  1737. }
  1738. /**
  1739. * i40e_vsi_setup_queue_map_mqprio - Prepares mqprio based tc_config
  1740. * @vsi: the VSI being configured,
  1741. * @ctxt: VSI context structure
  1742. * @enabled_tc: number of traffic classes to enable
  1743. *
  1744. * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
  1745. **/
  1746. static int i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi *vsi,
  1747. struct i40e_vsi_context *ctxt,
  1748. u8 enabled_tc)
  1749. {
  1750. u16 qcount = 0, max_qcount, qmap, sections = 0;
  1751. int i, override_q, pow, num_qps, ret;
  1752. u8 netdev_tc = 0, offset = 0;
  1753. if (vsi->type != I40E_VSI_MAIN)
  1754. return -EINVAL;
  1755. sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
  1756. sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
  1757. vsi->tc_config.numtc = vsi->mqprio_qopt.qopt.num_tc;
  1758. vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
  1759. num_qps = vsi->mqprio_qopt.qopt.count[0];
  1760. /* find the next higher power-of-2 of num queue pairs */
  1761. pow = ilog2(num_qps);
  1762. if (!is_power_of_2(num_qps))
  1763. pow++;
  1764. qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
  1765. (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
  1766. /* Setup queue offset/count for all TCs for given VSI */
  1767. max_qcount = vsi->mqprio_qopt.qopt.count[0];
  1768. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  1769. /* See if the given TC is enabled for the given VSI */
  1770. if (vsi->tc_config.enabled_tc & BIT(i)) {
  1771. offset = vsi->mqprio_qopt.qopt.offset[i];
  1772. qcount = vsi->mqprio_qopt.qopt.count[i];
  1773. if (qcount > max_qcount)
  1774. max_qcount = qcount;
  1775. vsi->tc_config.tc_info[i].qoffset = offset;
  1776. vsi->tc_config.tc_info[i].qcount = qcount;
  1777. vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
  1778. } else {
  1779. /* TC is not enabled so set the offset to
  1780. * default queue and allocate one queue
  1781. * for the given TC.
  1782. */
  1783. vsi->tc_config.tc_info[i].qoffset = 0;
  1784. vsi->tc_config.tc_info[i].qcount = 1;
  1785. vsi->tc_config.tc_info[i].netdev_tc = 0;
  1786. }
  1787. }
  1788. /* Set actual Tx/Rx queue pairs */
  1789. vsi->num_queue_pairs = offset + qcount;
  1790. /* Setup queue TC[0].qmap for given VSI context */
  1791. ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
  1792. ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
  1793. ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
  1794. ctxt->info.valid_sections |= cpu_to_le16(sections);
  1795. /* Reconfigure RSS for main VSI with max queue count */
  1796. vsi->rss_size = max_qcount;
  1797. ret = i40e_vsi_config_rss(vsi);
  1798. if (ret) {
  1799. dev_info(&vsi->back->pdev->dev,
  1800. "Failed to reconfig rss for num_queues (%u)\n",
  1801. max_qcount);
  1802. return ret;
  1803. }
  1804. vsi->reconfig_rss = true;
  1805. dev_dbg(&vsi->back->pdev->dev,
  1806. "Reconfigured rss with num_queues (%u)\n", max_qcount);
  1807. /* Find queue count available for channel VSIs and starting offset
  1808. * for channel VSIs
  1809. */
  1810. override_q = vsi->mqprio_qopt.qopt.count[0];
  1811. if (override_q && override_q < vsi->num_queue_pairs) {
  1812. vsi->cnt_q_avail = vsi->num_queue_pairs - override_q;
  1813. vsi->next_base_queue = override_q;
  1814. }
  1815. return 0;
  1816. }
  1817. /**
  1818. * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
  1819. * @vsi: the VSI being setup
  1820. * @ctxt: VSI context structure
  1821. * @enabled_tc: Enabled TCs bitmap
  1822. * @is_add: True if called before Add VSI
  1823. *
  1824. * Setup VSI queue mapping for enabled traffic classes.
  1825. **/
  1826. static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
  1827. struct i40e_vsi_context *ctxt,
  1828. u8 enabled_tc,
  1829. bool is_add)
  1830. {
  1831. struct i40e_pf *pf = vsi->back;
  1832. u16 num_tc_qps = 0;
  1833. u16 sections = 0;
  1834. u8 netdev_tc = 0;
  1835. u16 numtc = 1;
  1836. u16 qcount;
  1837. u8 offset;
  1838. u16 qmap;
  1839. int i;
  1840. sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
  1841. offset = 0;
  1842. /* zero out queue mapping, it will get updated on the end of the function */
  1843. memset(ctxt->info.queue_mapping, 0, sizeof(ctxt->info.queue_mapping));
  1844. if (vsi->type == I40E_VSI_MAIN) {
  1845. /* This code helps add more queue to the VSI if we have
  1846. * more cores than RSS can support, the higher cores will
  1847. * be served by ATR or other filters. Furthermore, the
  1848. * non-zero req_queue_pairs says that user requested a new
  1849. * queue count via ethtool's set_channels, so use this
  1850. * value for queues distribution across traffic classes
  1851. * We need at least one queue pair for the interface
  1852. * to be usable as we see in else statement.
  1853. */
  1854. if (vsi->req_queue_pairs > 0)
  1855. vsi->num_queue_pairs = vsi->req_queue_pairs;
  1856. else if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
  1857. vsi->num_queue_pairs = pf->num_lan_msix;
  1858. else
  1859. vsi->num_queue_pairs = 1;
  1860. }
  1861. /* Number of queues per enabled TC */
  1862. if (vsi->type == I40E_VSI_MAIN ||
  1863. (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs != 0))
  1864. num_tc_qps = vsi->num_queue_pairs;
  1865. else
  1866. num_tc_qps = vsi->alloc_queue_pairs;
  1867. if (enabled_tc && test_bit(I40E_FLAG_DCB_ENA, vsi->back->flags)) {
  1868. /* Find numtc from enabled TC bitmap */
  1869. for (i = 0, numtc = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  1870. if (enabled_tc & BIT(i)) /* TC is enabled */
  1871. numtc++;
  1872. }
  1873. if (!numtc) {
  1874. dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
  1875. numtc = 1;
  1876. }
  1877. num_tc_qps = num_tc_qps / numtc;
  1878. num_tc_qps = min_t(int, num_tc_qps,
  1879. i40e_pf_get_max_q_per_tc(pf));
  1880. }
  1881. vsi->tc_config.numtc = numtc;
  1882. vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
  1883. /* Do not allow use more TC queue pairs than MSI-X vectors exist */
  1884. if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
  1885. num_tc_qps = min_t(int, num_tc_qps, pf->num_lan_msix);
  1886. /* Setup queue offset/count for all TCs for given VSI */
  1887. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  1888. /* See if the given TC is enabled for the given VSI */
  1889. if (vsi->tc_config.enabled_tc & BIT(i)) {
  1890. /* TC is enabled */
  1891. int pow, num_qps;
  1892. switch (vsi->type) {
  1893. case I40E_VSI_MAIN:
  1894. if ((!test_bit(I40E_FLAG_FD_SB_ENA,
  1895. pf->flags) &&
  1896. !test_bit(I40E_FLAG_FD_ATR_ENA,
  1897. pf->flags)) ||
  1898. vsi->tc_config.enabled_tc != 1) {
  1899. qcount = min_t(int, pf->alloc_rss_size,
  1900. num_tc_qps);
  1901. break;
  1902. }
  1903. fallthrough;
  1904. case I40E_VSI_FDIR:
  1905. case I40E_VSI_SRIOV:
  1906. case I40E_VSI_VMDQ2:
  1907. default:
  1908. qcount = num_tc_qps;
  1909. WARN_ON(i != 0);
  1910. break;
  1911. }
  1912. vsi->tc_config.tc_info[i].qoffset = offset;
  1913. vsi->tc_config.tc_info[i].qcount = qcount;
  1914. /* find the next higher power-of-2 of num queue pairs */
  1915. num_qps = qcount;
  1916. pow = 0;
  1917. while (num_qps && (BIT_ULL(pow) < qcount)) {
  1918. pow++;
  1919. num_qps >>= 1;
  1920. }
  1921. vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
  1922. qmap =
  1923. (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
  1924. (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
  1925. offset += qcount;
  1926. } else {
  1927. /* TC is not enabled so set the offset to
  1928. * default queue and allocate one queue
  1929. * for the given TC.
  1930. */
  1931. vsi->tc_config.tc_info[i].qoffset = 0;
  1932. vsi->tc_config.tc_info[i].qcount = 1;
  1933. vsi->tc_config.tc_info[i].netdev_tc = 0;
  1934. qmap = 0;
  1935. }
  1936. ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
  1937. }
  1938. /* Do not change previously set num_queue_pairs for PFs and VFs*/
  1939. if ((vsi->type == I40E_VSI_MAIN && numtc != 1) ||
  1940. (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs == 0) ||
  1941. (vsi->type != I40E_VSI_MAIN && vsi->type != I40E_VSI_SRIOV))
  1942. vsi->num_queue_pairs = offset;
  1943. /* Scheduler section valid can only be set for ADD VSI */
  1944. if (is_add) {
  1945. sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
  1946. ctxt->info.up_enable_bits = enabled_tc;
  1947. }
  1948. if (vsi->type == I40E_VSI_SRIOV) {
  1949. ctxt->info.mapping_flags |=
  1950. cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
  1951. for (i = 0; i < vsi->num_queue_pairs; i++)
  1952. ctxt->info.queue_mapping[i] =
  1953. cpu_to_le16(vsi->base_queue + i);
  1954. } else {
  1955. ctxt->info.mapping_flags |=
  1956. cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
  1957. ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
  1958. }
  1959. ctxt->info.valid_sections |= cpu_to_le16(sections);
  1960. }
  1961. /**
  1962. * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
  1963. * @netdev: the netdevice
  1964. * @addr: address to add
  1965. *
  1966. * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
  1967. * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
  1968. */
  1969. static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
  1970. {
  1971. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1972. struct i40e_vsi *vsi = np->vsi;
  1973. if (i40e_add_mac_filter(vsi, addr))
  1974. return 0;
  1975. else
  1976. return -ENOMEM;
  1977. }
  1978. /**
  1979. * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
  1980. * @netdev: the netdevice
  1981. * @addr: address to add
  1982. *
  1983. * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
  1984. * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
  1985. */
  1986. static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
  1987. {
  1988. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1989. struct i40e_vsi *vsi = np->vsi;
  1990. /* Under some circumstances, we might receive a request to delete
  1991. * our own device address from our uc list. Because we store the
  1992. * device address in the VSI's MAC/VLAN filter list, we need to ignore
  1993. * such requests and not delete our device address from this list.
  1994. */
  1995. if (ether_addr_equal(addr, netdev->dev_addr))
  1996. return 0;
  1997. i40e_del_mac_filter(vsi, addr);
  1998. return 0;
  1999. }
  2000. /**
  2001. * i40e_set_rx_mode - NDO callback to set the netdev filters
  2002. * @netdev: network interface device structure
  2003. **/
  2004. static void i40e_set_rx_mode(struct net_device *netdev)
  2005. {
  2006. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2007. struct i40e_vsi *vsi = np->vsi;
  2008. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2009. __dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
  2010. __dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
  2011. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2012. /* check for other flag changes */
  2013. if (vsi->current_netdev_flags != vsi->netdev->flags) {
  2014. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  2015. set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
  2016. }
  2017. }
  2018. /**
  2019. * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
  2020. * @vsi: Pointer to VSI struct
  2021. * @from: Pointer to list which contains MAC filter entries - changes to
  2022. * those entries needs to be undone.
  2023. *
  2024. * MAC filter entries from this list were slated for deletion.
  2025. **/
  2026. static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
  2027. struct hlist_head *from)
  2028. {
  2029. struct i40e_mac_filter *f;
  2030. struct hlist_node *h;
  2031. hlist_for_each_entry_safe(f, h, from, hlist) {
  2032. u64 key = i40e_addr_to_hkey(f->macaddr);
  2033. /* Move the element back into MAC filter list*/
  2034. hlist_del(&f->hlist);
  2035. hash_add(vsi->mac_filter_hash, &f->hlist, key);
  2036. }
  2037. }
  2038. /**
  2039. * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
  2040. * @vsi: Pointer to vsi struct
  2041. * @from: Pointer to list which contains MAC filter entries - changes to
  2042. * those entries needs to be undone.
  2043. *
  2044. * MAC filter entries from this list were slated for addition.
  2045. **/
  2046. static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
  2047. struct hlist_head *from)
  2048. {
  2049. struct i40e_new_mac_filter *new;
  2050. struct hlist_node *h;
  2051. hlist_for_each_entry_safe(new, h, from, hlist) {
  2052. /* We can simply free the wrapper structure */
  2053. hlist_del(&new->hlist);
  2054. netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
  2055. kfree(new);
  2056. }
  2057. }
  2058. /**
  2059. * i40e_next_filter - Get the next non-broadcast filter from a list
  2060. * @next: pointer to filter in list
  2061. *
  2062. * Returns the next non-broadcast filter in the list. Required so that we
  2063. * ignore broadcast filters within the list, since these are not handled via
  2064. * the normal firmware update path.
  2065. */
  2066. static
  2067. struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
  2068. {
  2069. hlist_for_each_entry_continue(next, hlist) {
  2070. if (!is_broadcast_ether_addr(next->f->macaddr))
  2071. return next;
  2072. }
  2073. return NULL;
  2074. }
  2075. /**
  2076. * i40e_update_filter_state - Update filter state based on return data
  2077. * from firmware
  2078. * @count: Number of filters added
  2079. * @add_list: return data from fw
  2080. * @add_head: pointer to first filter in current batch
  2081. *
  2082. * MAC filter entries from list were slated to be added to device. Returns
  2083. * number of successful filters. Note that 0 does NOT mean success!
  2084. **/
  2085. static int
  2086. i40e_update_filter_state(int count,
  2087. struct i40e_aqc_add_macvlan_element_data *add_list,
  2088. struct i40e_new_mac_filter *add_head)
  2089. {
  2090. int retval = 0;
  2091. int i;
  2092. for (i = 0; i < count; i++) {
  2093. /* Always check status of each filter. We don't need to check
  2094. * the firmware return status because we pre-set the filter
  2095. * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
  2096. * request to the adminq. Thus, if it no longer matches then
  2097. * we know the filter is active.
  2098. */
  2099. if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
  2100. add_head->state = I40E_FILTER_FAILED;
  2101. } else {
  2102. add_head->state = I40E_FILTER_ACTIVE;
  2103. retval++;
  2104. }
  2105. add_head = i40e_next_filter(add_head);
  2106. if (!add_head)
  2107. break;
  2108. }
  2109. return retval;
  2110. }
  2111. /**
  2112. * i40e_aqc_del_filters - Request firmware to delete a set of filters
  2113. * @vsi: ptr to the VSI
  2114. * @vsi_name: name to display in messages
  2115. * @list: the list of filters to send to firmware
  2116. * @num_del: the number of filters to delete
  2117. * @retval: Set to -EIO on failure to delete
  2118. *
  2119. * Send a request to firmware via AdminQ to delete a set of filters. Uses
  2120. * *retval instead of a return value so that success does not force ret_val to
  2121. * be set to 0. This ensures that a sequence of calls to this function
  2122. * preserve the previous value of *retval on successful delete.
  2123. */
  2124. static
  2125. void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
  2126. struct i40e_aqc_remove_macvlan_element_data *list,
  2127. int num_del, int *retval)
  2128. {
  2129. struct i40e_hw *hw = &vsi->back->hw;
  2130. enum i40e_admin_queue_err aq_status;
  2131. int aq_ret;
  2132. aq_ret = i40e_aq_remove_macvlan_v2(hw, vsi->seid, list, num_del, NULL,
  2133. &aq_status);
  2134. /* Explicitly ignore and do not report when firmware returns ENOENT */
  2135. if (aq_ret && !(aq_status == I40E_AQ_RC_ENOENT)) {
  2136. *retval = -EIO;
  2137. dev_info(&vsi->back->pdev->dev,
  2138. "ignoring delete macvlan error on %s, err %pe, aq_err %s\n",
  2139. vsi_name, ERR_PTR(aq_ret),
  2140. i40e_aq_str(hw, aq_status));
  2141. }
  2142. }
  2143. /**
  2144. * i40e_aqc_add_filters - Request firmware to add a set of filters
  2145. * @vsi: ptr to the VSI
  2146. * @vsi_name: name to display in messages
  2147. * @list: the list of filters to send to firmware
  2148. * @add_head: Position in the add hlist
  2149. * @num_add: the number of filters to add
  2150. *
  2151. * Send a request to firmware via AdminQ to add a chunk of filters. Will set
  2152. * __I40E_VSI_OVERFLOW_PROMISC bit in vsi->state if the firmware has run out of
  2153. * space for more filters.
  2154. */
  2155. static
  2156. void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
  2157. struct i40e_aqc_add_macvlan_element_data *list,
  2158. struct i40e_new_mac_filter *add_head,
  2159. int num_add)
  2160. {
  2161. struct i40e_hw *hw = &vsi->back->hw;
  2162. enum i40e_admin_queue_err aq_status;
  2163. int fcnt;
  2164. i40e_aq_add_macvlan_v2(hw, vsi->seid, list, num_add, NULL, &aq_status);
  2165. fcnt = i40e_update_filter_state(num_add, list, add_head);
  2166. if (fcnt != num_add) {
  2167. if (vsi->type == I40E_VSI_MAIN) {
  2168. set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  2169. dev_warn(&vsi->back->pdev->dev,
  2170. "Error %s adding RX filters on %s, promiscuous mode forced on\n",
  2171. i40e_aq_str(hw, aq_status), vsi_name);
  2172. } else if (vsi->type == I40E_VSI_SRIOV ||
  2173. vsi->type == I40E_VSI_VMDQ1 ||
  2174. vsi->type == I40E_VSI_VMDQ2) {
  2175. dev_warn(&vsi->back->pdev->dev,
  2176. "Error %s adding RX filters on %s, please set promiscuous on manually for %s\n",
  2177. i40e_aq_str(hw, aq_status), vsi_name,
  2178. vsi_name);
  2179. } else {
  2180. dev_warn(&vsi->back->pdev->dev,
  2181. "Error %s adding RX filters on %s, incorrect VSI type: %i.\n",
  2182. i40e_aq_str(hw, aq_status), vsi_name,
  2183. vsi->type);
  2184. }
  2185. }
  2186. }
  2187. /**
  2188. * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
  2189. * @vsi: pointer to the VSI
  2190. * @vsi_name: the VSI name
  2191. * @f: filter data
  2192. *
  2193. * This function sets or clears the promiscuous broadcast flags for VLAN
  2194. * filters in order to properly receive broadcast frames. Assumes that only
  2195. * broadcast filters are passed.
  2196. *
  2197. * Returns status indicating success or failure;
  2198. **/
  2199. static int
  2200. i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
  2201. struct i40e_mac_filter *f)
  2202. {
  2203. bool enable = f->state == I40E_FILTER_NEW ||
  2204. f->state == I40E_FILTER_NEW_SYNC;
  2205. struct i40e_hw *hw = &vsi->back->hw;
  2206. int aq_ret;
  2207. if (f->vlan == I40E_VLAN_ANY) {
  2208. aq_ret = i40e_aq_set_vsi_broadcast(hw,
  2209. vsi->seid,
  2210. enable,
  2211. NULL);
  2212. } else {
  2213. aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
  2214. vsi->seid,
  2215. enable,
  2216. f->vlan,
  2217. NULL);
  2218. }
  2219. if (aq_ret) {
  2220. set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  2221. dev_warn(&vsi->back->pdev->dev,
  2222. "Error %s, forcing overflow promiscuous on %s\n",
  2223. i40e_aq_str(hw, hw->aq.asq_last_status),
  2224. vsi_name);
  2225. }
  2226. return aq_ret;
  2227. }
  2228. /**
  2229. * i40e_set_promiscuous - set promiscuous mode
  2230. * @pf: board private structure
  2231. * @promisc: promisc on or off
  2232. *
  2233. * There are different ways of setting promiscuous mode on a PF depending on
  2234. * what state/environment we're in. This identifies and sets it appropriately.
  2235. * Returns 0 on success.
  2236. **/
  2237. static int i40e_set_promiscuous(struct i40e_pf *pf, bool promisc)
  2238. {
  2239. struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
  2240. struct i40e_hw *hw = &pf->hw;
  2241. int aq_ret;
  2242. if (vsi->type == I40E_VSI_MAIN &&
  2243. i40e_pf_get_main_veb(pf) &&
  2244. !test_bit(I40E_FLAG_MFP_ENA, pf->flags)) {
  2245. /* set defport ON for Main VSI instead of true promisc
  2246. * this way we will get all unicast/multicast and VLAN
  2247. * promisc behavior but will not get VF or VMDq traffic
  2248. * replicated on the Main VSI.
  2249. */
  2250. if (promisc)
  2251. aq_ret = i40e_aq_set_default_vsi(hw,
  2252. vsi->seid,
  2253. NULL);
  2254. else
  2255. aq_ret = i40e_aq_clear_default_vsi(hw,
  2256. vsi->seid,
  2257. NULL);
  2258. if (aq_ret) {
  2259. dev_info(&pf->pdev->dev,
  2260. "Set default VSI failed, err %pe, aq_err %s\n",
  2261. ERR_PTR(aq_ret),
  2262. i40e_aq_str(hw, hw->aq.asq_last_status));
  2263. }
  2264. } else {
  2265. aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
  2266. hw,
  2267. vsi->seid,
  2268. promisc, NULL,
  2269. true);
  2270. if (aq_ret) {
  2271. dev_info(&pf->pdev->dev,
  2272. "set unicast promisc failed, err %pe, aq_err %s\n",
  2273. ERR_PTR(aq_ret),
  2274. i40e_aq_str(hw, hw->aq.asq_last_status));
  2275. }
  2276. aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
  2277. hw,
  2278. vsi->seid,
  2279. promisc, NULL);
  2280. if (aq_ret) {
  2281. dev_info(&pf->pdev->dev,
  2282. "set multicast promisc failed, err %pe, aq_err %s\n",
  2283. ERR_PTR(aq_ret),
  2284. i40e_aq_str(hw, hw->aq.asq_last_status));
  2285. }
  2286. }
  2287. if (!aq_ret)
  2288. pf->cur_promisc = promisc;
  2289. return aq_ret;
  2290. }
  2291. /**
  2292. * i40e_sync_vsi_filters - Update the VSI filter list to the HW
  2293. * @vsi: ptr to the VSI
  2294. *
  2295. * Push any outstanding VSI filter changes through the AdminQ.
  2296. *
  2297. * Returns 0 or error value
  2298. **/
  2299. int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
  2300. {
  2301. struct hlist_head tmp_add_list, tmp_del_list;
  2302. struct i40e_mac_filter *f;
  2303. struct i40e_new_mac_filter *new, *add_head = NULL;
  2304. struct i40e_hw *hw = &vsi->back->hw;
  2305. bool old_overflow, new_overflow;
  2306. unsigned int failed_filters = 0;
  2307. unsigned int vlan_filters = 0;
  2308. char vsi_name[16] = "PF";
  2309. int filter_list_len = 0;
  2310. u32 changed_flags = 0;
  2311. struct hlist_node *h;
  2312. struct i40e_pf *pf;
  2313. int num_add = 0;
  2314. int num_del = 0;
  2315. int aq_ret = 0;
  2316. int retval = 0;
  2317. u16 cmd_flags;
  2318. int list_size;
  2319. int bkt;
  2320. /* empty array typed pointers, kcalloc later */
  2321. struct i40e_aqc_add_macvlan_element_data *add_list;
  2322. struct i40e_aqc_remove_macvlan_element_data *del_list;
  2323. while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state))
  2324. usleep_range(1000, 2000);
  2325. pf = vsi->back;
  2326. old_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  2327. if (vsi->netdev) {
  2328. changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
  2329. vsi->current_netdev_flags = vsi->netdev->flags;
  2330. }
  2331. INIT_HLIST_HEAD(&tmp_add_list);
  2332. INIT_HLIST_HEAD(&tmp_del_list);
  2333. if (vsi->type == I40E_VSI_SRIOV)
  2334. snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
  2335. else if (vsi->type != I40E_VSI_MAIN)
  2336. snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
  2337. if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
  2338. vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
  2339. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2340. /* Create a list of filters to delete. */
  2341. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  2342. if (f->state == I40E_FILTER_REMOVE) {
  2343. /* Move the element into temporary del_list */
  2344. hash_del(&f->hlist);
  2345. hlist_add_head(&f->hlist, &tmp_del_list);
  2346. /* Avoid counting removed filters */
  2347. continue;
  2348. }
  2349. if (f->state == I40E_FILTER_NEW) {
  2350. /* Create a temporary i40e_new_mac_filter */
  2351. new = kzalloc(sizeof(*new), GFP_ATOMIC);
  2352. if (!new)
  2353. goto err_no_memory_locked;
  2354. /* Store pointer to the real filter */
  2355. new->f = f;
  2356. new->state = f->state;
  2357. /* Add it to the hash list */
  2358. hlist_add_head(&new->hlist, &tmp_add_list);
  2359. f->state = I40E_FILTER_NEW_SYNC;
  2360. }
  2361. /* Count the number of active (current and new) VLAN
  2362. * filters we have now. Does not count filters which
  2363. * are marked for deletion.
  2364. */
  2365. if (f->vlan > 0)
  2366. vlan_filters++;
  2367. }
  2368. if (vsi->type != I40E_VSI_SRIOV)
  2369. retval = i40e_correct_mac_vlan_filters
  2370. (vsi, &tmp_add_list, &tmp_del_list,
  2371. vlan_filters);
  2372. else if (pf->vf)
  2373. retval = i40e_correct_vf_mac_vlan_filters
  2374. (vsi, &tmp_add_list, &tmp_del_list,
  2375. vlan_filters, pf->vf[vsi->vf_id].trusted);
  2376. hlist_for_each_entry(new, &tmp_add_list, hlist)
  2377. netdev_hw_addr_refcnt(new->f, vsi->netdev, 1);
  2378. if (retval)
  2379. goto err_no_memory_locked;
  2380. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2381. }
  2382. /* Now process 'del_list' outside the lock */
  2383. if (!hlist_empty(&tmp_del_list)) {
  2384. filter_list_len = hw->aq.asq_buf_size /
  2385. sizeof(struct i40e_aqc_remove_macvlan_element_data);
  2386. list_size = filter_list_len *
  2387. sizeof(struct i40e_aqc_remove_macvlan_element_data);
  2388. del_list = kzalloc(list_size, GFP_ATOMIC);
  2389. if (!del_list)
  2390. goto err_no_memory;
  2391. hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
  2392. cmd_flags = 0;
  2393. /* handle broadcast filters by updating the broadcast
  2394. * promiscuous flag and release filter list.
  2395. */
  2396. if (is_broadcast_ether_addr(f->macaddr)) {
  2397. i40e_aqc_broadcast_filter(vsi, vsi_name, f);
  2398. hlist_del(&f->hlist);
  2399. kfree(f);
  2400. continue;
  2401. }
  2402. /* add to delete list */
  2403. ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
  2404. if (f->vlan == I40E_VLAN_ANY) {
  2405. del_list[num_del].vlan_tag = 0;
  2406. cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
  2407. } else {
  2408. del_list[num_del].vlan_tag =
  2409. cpu_to_le16((u16)(f->vlan));
  2410. }
  2411. cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
  2412. del_list[num_del].flags = cmd_flags;
  2413. num_del++;
  2414. /* flush a full buffer */
  2415. if (num_del == filter_list_len) {
  2416. i40e_aqc_del_filters(vsi, vsi_name, del_list,
  2417. num_del, &retval);
  2418. memset(del_list, 0, list_size);
  2419. num_del = 0;
  2420. }
  2421. /* Release memory for MAC filter entries which were
  2422. * synced up with HW.
  2423. */
  2424. hlist_del(&f->hlist);
  2425. kfree(f);
  2426. }
  2427. if (num_del) {
  2428. i40e_aqc_del_filters(vsi, vsi_name, del_list,
  2429. num_del, &retval);
  2430. }
  2431. kfree(del_list);
  2432. del_list = NULL;
  2433. }
  2434. if (!hlist_empty(&tmp_add_list)) {
  2435. /* Do all the adds now. */
  2436. filter_list_len = hw->aq.asq_buf_size /
  2437. sizeof(struct i40e_aqc_add_macvlan_element_data);
  2438. list_size = filter_list_len *
  2439. sizeof(struct i40e_aqc_add_macvlan_element_data);
  2440. add_list = kzalloc(list_size, GFP_ATOMIC);
  2441. if (!add_list)
  2442. goto err_no_memory;
  2443. num_add = 0;
  2444. hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
  2445. /* handle broadcast filters by updating the broadcast
  2446. * promiscuous flag instead of adding a MAC filter.
  2447. */
  2448. if (is_broadcast_ether_addr(new->f->macaddr)) {
  2449. if (i40e_aqc_broadcast_filter(vsi, vsi_name,
  2450. new->f))
  2451. new->state = I40E_FILTER_FAILED;
  2452. else
  2453. new->state = I40E_FILTER_ACTIVE;
  2454. continue;
  2455. }
  2456. /* add to add array */
  2457. if (num_add == 0)
  2458. add_head = new;
  2459. cmd_flags = 0;
  2460. ether_addr_copy(add_list[num_add].mac_addr,
  2461. new->f->macaddr);
  2462. if (new->f->vlan == I40E_VLAN_ANY) {
  2463. add_list[num_add].vlan_tag = 0;
  2464. cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
  2465. } else {
  2466. add_list[num_add].vlan_tag =
  2467. cpu_to_le16((u16)(new->f->vlan));
  2468. }
  2469. add_list[num_add].queue_number = 0;
  2470. /* set invalid match method for later detection */
  2471. add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
  2472. cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
  2473. add_list[num_add].flags = cpu_to_le16(cmd_flags);
  2474. num_add++;
  2475. /* flush a full buffer */
  2476. if (num_add == filter_list_len) {
  2477. i40e_aqc_add_filters(vsi, vsi_name, add_list,
  2478. add_head, num_add);
  2479. memset(add_list, 0, list_size);
  2480. num_add = 0;
  2481. }
  2482. }
  2483. if (num_add) {
  2484. i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
  2485. num_add);
  2486. }
  2487. /* Now move all of the filters from the temp add list back to
  2488. * the VSI's list.
  2489. */
  2490. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2491. hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
  2492. /* Only update the state if we're still NEW */
  2493. if (new->f->state == I40E_FILTER_NEW ||
  2494. new->f->state == I40E_FILTER_NEW_SYNC)
  2495. new->f->state = new->state;
  2496. hlist_del(&new->hlist);
  2497. netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
  2498. kfree(new);
  2499. }
  2500. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2501. kfree(add_list);
  2502. add_list = NULL;
  2503. }
  2504. /* Determine the number of active and failed filters. */
  2505. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2506. vsi->active_filters = 0;
  2507. hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
  2508. if (f->state == I40E_FILTER_ACTIVE)
  2509. vsi->active_filters++;
  2510. else if (f->state == I40E_FILTER_FAILED)
  2511. failed_filters++;
  2512. }
  2513. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2514. /* Check if we are able to exit overflow promiscuous mode. We can
  2515. * safely exit if we didn't just enter, we no longer have any failed
  2516. * filters, and we have reduced filters below the threshold value.
  2517. */
  2518. if (old_overflow && !failed_filters &&
  2519. vsi->active_filters < vsi->promisc_threshold) {
  2520. dev_info(&pf->pdev->dev,
  2521. "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
  2522. vsi_name);
  2523. clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  2524. vsi->promisc_threshold = 0;
  2525. }
  2526. /* if the VF is not trusted do not do promisc */
  2527. if (vsi->type == I40E_VSI_SRIOV && pf->vf &&
  2528. !pf->vf[vsi->vf_id].trusted) {
  2529. clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  2530. goto out;
  2531. }
  2532. new_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  2533. /* If we are entering overflow promiscuous, we need to calculate a new
  2534. * threshold for when we are safe to exit
  2535. */
  2536. if (!old_overflow && new_overflow)
  2537. vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
  2538. /* check for changes in promiscuous modes */
  2539. if (changed_flags & IFF_ALLMULTI) {
  2540. bool cur_multipromisc;
  2541. cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
  2542. aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
  2543. vsi->seid,
  2544. cur_multipromisc,
  2545. NULL);
  2546. if (aq_ret) {
  2547. retval = i40e_aq_rc_to_posix(aq_ret,
  2548. hw->aq.asq_last_status);
  2549. dev_info(&pf->pdev->dev,
  2550. "set multi promisc failed on %s, err %pe aq_err %s\n",
  2551. vsi_name,
  2552. ERR_PTR(aq_ret),
  2553. i40e_aq_str(hw, hw->aq.asq_last_status));
  2554. } else {
  2555. dev_info(&pf->pdev->dev, "%s allmulti mode.\n",
  2556. cur_multipromisc ? "entering" : "leaving");
  2557. }
  2558. }
  2559. if ((changed_flags & IFF_PROMISC) || old_overflow != new_overflow) {
  2560. bool cur_promisc;
  2561. cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
  2562. new_overflow);
  2563. aq_ret = i40e_set_promiscuous(pf, cur_promisc);
  2564. if (aq_ret) {
  2565. retval = i40e_aq_rc_to_posix(aq_ret,
  2566. hw->aq.asq_last_status);
  2567. dev_info(&pf->pdev->dev,
  2568. "Setting promiscuous %s failed on %s, err %pe aq_err %s\n",
  2569. cur_promisc ? "on" : "off",
  2570. vsi_name,
  2571. ERR_PTR(aq_ret),
  2572. i40e_aq_str(hw, hw->aq.asq_last_status));
  2573. }
  2574. }
  2575. out:
  2576. /* if something went wrong then set the changed flag so we try again */
  2577. if (retval)
  2578. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  2579. clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
  2580. return retval;
  2581. err_no_memory:
  2582. /* Restore elements on the temporary add and delete lists */
  2583. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2584. err_no_memory_locked:
  2585. i40e_undo_del_filter_entries(vsi, &tmp_del_list);
  2586. i40e_undo_add_filter_entries(vsi, &tmp_add_list);
  2587. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2588. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  2589. clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
  2590. return -ENOMEM;
  2591. }
  2592. /**
  2593. * i40e_sync_filters_subtask - Sync the VSI filter list with HW
  2594. * @pf: board private structure
  2595. **/
  2596. static void i40e_sync_filters_subtask(struct i40e_pf *pf)
  2597. {
  2598. struct i40e_vsi *vsi;
  2599. int v;
  2600. if (!pf)
  2601. return;
  2602. if (!test_and_clear_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state))
  2603. return;
  2604. if (test_bit(__I40E_VF_DISABLE, pf->state)) {
  2605. set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
  2606. return;
  2607. }
  2608. i40e_pf_for_each_vsi(pf, v, vsi) {
  2609. if ((vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) &&
  2610. !test_bit(__I40E_VSI_RELEASING, vsi->state)) {
  2611. int ret = i40e_sync_vsi_filters(vsi);
  2612. if (ret) {
  2613. /* come back and try again later */
  2614. set_bit(__I40E_MACVLAN_SYNC_PENDING,
  2615. pf->state);
  2616. break;
  2617. }
  2618. }
  2619. }
  2620. }
  2621. /**
  2622. * i40e_calculate_vsi_rx_buf_len - Calculates buffer length
  2623. *
  2624. * @vsi: VSI to calculate rx_buf_len from
  2625. */
  2626. static u16 i40e_calculate_vsi_rx_buf_len(struct i40e_vsi *vsi)
  2627. {
  2628. if (!vsi->netdev || test_bit(I40E_FLAG_LEGACY_RX_ENA, vsi->back->flags))
  2629. return SKB_WITH_OVERHEAD(I40E_RXBUFFER_2048);
  2630. return PAGE_SIZE < 8192 ? I40E_RXBUFFER_3072 : I40E_RXBUFFER_2048;
  2631. }
  2632. /**
  2633. * i40e_max_vsi_frame_size - returns the maximum allowed frame size for VSI
  2634. * @vsi: the vsi
  2635. * @xdp_prog: XDP program
  2636. **/
  2637. static int i40e_max_vsi_frame_size(struct i40e_vsi *vsi,
  2638. struct bpf_prog *xdp_prog)
  2639. {
  2640. u16 rx_buf_len = i40e_calculate_vsi_rx_buf_len(vsi);
  2641. u16 chain_len;
  2642. if (xdp_prog && !xdp_prog->aux->xdp_has_frags)
  2643. chain_len = 1;
  2644. else
  2645. chain_len = I40E_MAX_CHAINED_RX_BUFFERS;
  2646. return min_t(u16, rx_buf_len * chain_len, I40E_MAX_RXBUFFER);
  2647. }
  2648. /**
  2649. * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
  2650. * @netdev: network interface device structure
  2651. * @new_mtu: new value for maximum frame size
  2652. *
  2653. * Returns 0 on success, negative on failure
  2654. **/
  2655. static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
  2656. {
  2657. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2658. struct i40e_vsi *vsi = np->vsi;
  2659. struct i40e_pf *pf = vsi->back;
  2660. int frame_size;
  2661. frame_size = i40e_max_vsi_frame_size(vsi, vsi->xdp_prog);
  2662. if (new_mtu > frame_size - I40E_PACKET_HDR_PAD) {
  2663. netdev_err(netdev, "Error changing mtu to %d, Max is %d\n",
  2664. new_mtu, frame_size - I40E_PACKET_HDR_PAD);
  2665. return -EINVAL;
  2666. }
  2667. netdev_dbg(netdev, "changing MTU from %d to %d\n",
  2668. netdev->mtu, new_mtu);
  2669. WRITE_ONCE(netdev->mtu, new_mtu);
  2670. if (netif_running(netdev))
  2671. i40e_vsi_reinit_locked(vsi);
  2672. set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
  2673. set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
  2674. return 0;
  2675. }
  2676. /**
  2677. * i40e_ioctl - Access the hwtstamp interface
  2678. * @netdev: network interface device structure
  2679. * @ifr: interface request data
  2680. * @cmd: ioctl command
  2681. **/
  2682. int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  2683. {
  2684. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2685. struct i40e_pf *pf = np->vsi->back;
  2686. switch (cmd) {
  2687. case SIOCGHWTSTAMP:
  2688. return i40e_ptp_get_ts_config(pf, ifr);
  2689. case SIOCSHWTSTAMP:
  2690. return i40e_ptp_set_ts_config(pf, ifr);
  2691. default:
  2692. return -EOPNOTSUPP;
  2693. }
  2694. }
  2695. /**
  2696. * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
  2697. * @vsi: the vsi being adjusted
  2698. **/
  2699. void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
  2700. {
  2701. struct i40e_vsi_context ctxt;
  2702. int ret;
  2703. /* Don't modify stripping options if a port VLAN is active */
  2704. if (vsi->info.pvid)
  2705. return;
  2706. if ((vsi->info.valid_sections &
  2707. cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
  2708. ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
  2709. return; /* already enabled */
  2710. vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  2711. vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
  2712. I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
  2713. ctxt.seid = vsi->seid;
  2714. ctxt.info = vsi->info;
  2715. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  2716. if (ret) {
  2717. dev_info(&vsi->back->pdev->dev,
  2718. "update vlan stripping failed, err %pe aq_err %s\n",
  2719. ERR_PTR(ret),
  2720. i40e_aq_str(&vsi->back->hw,
  2721. vsi->back->hw.aq.asq_last_status));
  2722. }
  2723. }
  2724. /**
  2725. * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
  2726. * @vsi: the vsi being adjusted
  2727. **/
  2728. void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
  2729. {
  2730. struct i40e_vsi_context ctxt;
  2731. int ret;
  2732. /* Don't modify stripping options if a port VLAN is active */
  2733. if (vsi->info.pvid)
  2734. return;
  2735. if ((vsi->info.valid_sections &
  2736. cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
  2737. ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
  2738. I40E_AQ_VSI_PVLAN_EMOD_MASK))
  2739. return; /* already disabled */
  2740. vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  2741. vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
  2742. I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
  2743. ctxt.seid = vsi->seid;
  2744. ctxt.info = vsi->info;
  2745. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  2746. if (ret) {
  2747. dev_info(&vsi->back->pdev->dev,
  2748. "update vlan stripping failed, err %pe aq_err %s\n",
  2749. ERR_PTR(ret),
  2750. i40e_aq_str(&vsi->back->hw,
  2751. vsi->back->hw.aq.asq_last_status));
  2752. }
  2753. }
  2754. /**
  2755. * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
  2756. * @vsi: the vsi being configured
  2757. * @vid: vlan id to be added (0 = untagged only , -1 = any)
  2758. *
  2759. * This is a helper function for adding a new MAC/VLAN filter with the
  2760. * specified VLAN for each existing MAC address already in the hash table.
  2761. * This function does *not* perform any accounting to update filters based on
  2762. * VLAN mode.
  2763. *
  2764. * NOTE: this function expects to be called while under the
  2765. * mac_filter_hash_lock
  2766. **/
  2767. int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
  2768. {
  2769. struct i40e_mac_filter *f, *add_f;
  2770. struct hlist_node *h;
  2771. int bkt;
  2772. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  2773. /* If we're asked to add a filter that has been marked for
  2774. * removal, it is safe to simply restore it to active state.
  2775. * __i40e_del_filter will have simply deleted any filters which
  2776. * were previously marked NEW or FAILED, so if it is currently
  2777. * marked REMOVE it must have previously been ACTIVE. Since we
  2778. * haven't yet run the sync filters task, just restore this
  2779. * filter to the ACTIVE state so that the sync task leaves it
  2780. * in place.
  2781. */
  2782. if (f->state == I40E_FILTER_REMOVE && f->vlan == vid) {
  2783. f->state = I40E_FILTER_ACTIVE;
  2784. continue;
  2785. } else if (f->state == I40E_FILTER_REMOVE) {
  2786. continue;
  2787. }
  2788. add_f = i40e_add_filter(vsi, f->macaddr, vid);
  2789. if (!add_f) {
  2790. dev_info(&vsi->back->pdev->dev,
  2791. "Could not add vlan filter %d for %pM\n",
  2792. vid, f->macaddr);
  2793. return -ENOMEM;
  2794. }
  2795. }
  2796. return 0;
  2797. }
  2798. /**
  2799. * i40e_vsi_add_vlan - Add VSI membership for given VLAN
  2800. * @vsi: the VSI being configured
  2801. * @vid: VLAN id to be added
  2802. **/
  2803. int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
  2804. {
  2805. int err;
  2806. if (vsi->info.pvid)
  2807. return -EINVAL;
  2808. /* The network stack will attempt to add VID=0, with the intention to
  2809. * receive priority tagged packets with a VLAN of 0. Our HW receives
  2810. * these packets by default when configured to receive untagged
  2811. * packets, so we don't need to add a filter for this case.
  2812. * Additionally, HW interprets adding a VID=0 filter as meaning to
  2813. * receive *only* tagged traffic and stops receiving untagged traffic.
  2814. * Thus, we do not want to actually add a filter for VID=0
  2815. */
  2816. if (!vid)
  2817. return 0;
  2818. /* Locked once because all functions invoked below iterates list*/
  2819. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2820. err = i40e_add_vlan_all_mac(vsi, vid);
  2821. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2822. if (err)
  2823. return err;
  2824. /* schedule our worker thread which will take care of
  2825. * applying the new filter changes
  2826. */
  2827. i40e_service_event_schedule(vsi->back);
  2828. return 0;
  2829. }
  2830. /**
  2831. * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
  2832. * @vsi: the vsi being configured
  2833. * @vid: vlan id to be removed (0 = untagged only , -1 = any)
  2834. *
  2835. * This function should be used to remove all VLAN filters which match the
  2836. * given VID. It does not schedule the service event and does not take the
  2837. * mac_filter_hash_lock so it may be combined with other operations under
  2838. * a single invocation of the mac_filter_hash_lock.
  2839. *
  2840. * NOTE: this function expects to be called while under the
  2841. * mac_filter_hash_lock
  2842. */
  2843. void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
  2844. {
  2845. struct i40e_mac_filter *f;
  2846. struct hlist_node *h;
  2847. int bkt;
  2848. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  2849. if (f->vlan == vid)
  2850. __i40e_del_filter(vsi, f);
  2851. }
  2852. }
  2853. /**
  2854. * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
  2855. * @vsi: the VSI being configured
  2856. * @vid: VLAN id to be removed
  2857. **/
  2858. void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
  2859. {
  2860. if (!vid || vsi->info.pvid)
  2861. return;
  2862. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2863. i40e_rm_vlan_all_mac(vsi, vid);
  2864. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2865. /* schedule our worker thread which will take care of
  2866. * applying the new filter changes
  2867. */
  2868. i40e_service_event_schedule(vsi->back);
  2869. }
  2870. /**
  2871. * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
  2872. * @netdev: network interface to be adjusted
  2873. * @proto: unused protocol value
  2874. * @vid: vlan id to be added
  2875. *
  2876. * net_device_ops implementation for adding vlan ids
  2877. **/
  2878. static int i40e_vlan_rx_add_vid(struct net_device *netdev,
  2879. __always_unused __be16 proto, u16 vid)
  2880. {
  2881. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2882. struct i40e_vsi *vsi = np->vsi;
  2883. int ret = 0;
  2884. if (vid >= VLAN_N_VID)
  2885. return -EINVAL;
  2886. ret = i40e_vsi_add_vlan(vsi, vid);
  2887. if (!ret)
  2888. set_bit(vid, vsi->active_vlans);
  2889. return ret;
  2890. }
  2891. /**
  2892. * i40e_vlan_rx_add_vid_up - Add a vlan id filter to HW offload in UP path
  2893. * @netdev: network interface to be adjusted
  2894. * @proto: unused protocol value
  2895. * @vid: vlan id to be added
  2896. **/
  2897. static void i40e_vlan_rx_add_vid_up(struct net_device *netdev,
  2898. __always_unused __be16 proto, u16 vid)
  2899. {
  2900. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2901. struct i40e_vsi *vsi = np->vsi;
  2902. if (vid >= VLAN_N_VID)
  2903. return;
  2904. set_bit(vid, vsi->active_vlans);
  2905. }
  2906. /**
  2907. * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
  2908. * @netdev: network interface to be adjusted
  2909. * @proto: unused protocol value
  2910. * @vid: vlan id to be removed
  2911. *
  2912. * net_device_ops implementation for removing vlan ids
  2913. **/
  2914. static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
  2915. __always_unused __be16 proto, u16 vid)
  2916. {
  2917. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2918. struct i40e_vsi *vsi = np->vsi;
  2919. /* return code is ignored as there is nothing a user
  2920. * can do about failure to remove and a log message was
  2921. * already printed from the other function
  2922. */
  2923. i40e_vsi_kill_vlan(vsi, vid);
  2924. clear_bit(vid, vsi->active_vlans);
  2925. return 0;
  2926. }
  2927. /**
  2928. * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
  2929. * @vsi: the vsi being brought back up
  2930. **/
  2931. static void i40e_restore_vlan(struct i40e_vsi *vsi)
  2932. {
  2933. u16 vid;
  2934. if (!vsi->netdev)
  2935. return;
  2936. if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
  2937. i40e_vlan_stripping_enable(vsi);
  2938. else
  2939. i40e_vlan_stripping_disable(vsi);
  2940. for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
  2941. i40e_vlan_rx_add_vid_up(vsi->netdev, htons(ETH_P_8021Q),
  2942. vid);
  2943. }
  2944. /**
  2945. * i40e_vsi_add_pvid - Add pvid for the VSI
  2946. * @vsi: the vsi being adjusted
  2947. * @vid: the vlan id to set as a PVID
  2948. **/
  2949. int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
  2950. {
  2951. struct i40e_vsi_context ctxt;
  2952. int ret;
  2953. vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  2954. vsi->info.pvid = cpu_to_le16(vid);
  2955. vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
  2956. I40E_AQ_VSI_PVLAN_INSERT_PVID |
  2957. I40E_AQ_VSI_PVLAN_EMOD_STR;
  2958. ctxt.seid = vsi->seid;
  2959. ctxt.info = vsi->info;
  2960. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  2961. if (ret) {
  2962. dev_info(&vsi->back->pdev->dev,
  2963. "add pvid failed, err %pe aq_err %s\n",
  2964. ERR_PTR(ret),
  2965. i40e_aq_str(&vsi->back->hw,
  2966. vsi->back->hw.aq.asq_last_status));
  2967. return -ENOENT;
  2968. }
  2969. return 0;
  2970. }
  2971. /**
  2972. * i40e_vsi_remove_pvid - Remove the pvid from the VSI
  2973. * @vsi: the vsi being adjusted
  2974. *
  2975. * Just use the vlan_rx_register() service to put it back to normal
  2976. **/
  2977. void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
  2978. {
  2979. vsi->info.pvid = 0;
  2980. i40e_vlan_stripping_disable(vsi);
  2981. }
  2982. /**
  2983. * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
  2984. * @vsi: ptr to the VSI
  2985. *
  2986. * If this function returns with an error, then it's possible one or
  2987. * more of the rings is populated (while the rest are not). It is the
  2988. * callers duty to clean those orphaned rings.
  2989. *
  2990. * Return 0 on success, negative on failure
  2991. **/
  2992. static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
  2993. {
  2994. int i, err = 0;
  2995. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2996. err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
  2997. if (!i40e_enabled_xdp_vsi(vsi))
  2998. return err;
  2999. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  3000. err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
  3001. return err;
  3002. }
  3003. /**
  3004. * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
  3005. * @vsi: ptr to the VSI
  3006. *
  3007. * Free VSI's transmit software resources
  3008. **/
  3009. static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
  3010. {
  3011. int i;
  3012. if (vsi->tx_rings) {
  3013. for (i = 0; i < vsi->num_queue_pairs; i++)
  3014. if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
  3015. i40e_free_tx_resources(vsi->tx_rings[i]);
  3016. }
  3017. if (vsi->xdp_rings) {
  3018. for (i = 0; i < vsi->num_queue_pairs; i++)
  3019. if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
  3020. i40e_free_tx_resources(vsi->xdp_rings[i]);
  3021. }
  3022. }
  3023. /**
  3024. * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
  3025. * @vsi: ptr to the VSI
  3026. *
  3027. * If this function returns with an error, then it's possible one or
  3028. * more of the rings is populated (while the rest are not). It is the
  3029. * callers duty to clean those orphaned rings.
  3030. *
  3031. * Return 0 on success, negative on failure
  3032. **/
  3033. static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
  3034. {
  3035. int i, err = 0;
  3036. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  3037. err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
  3038. return err;
  3039. }
  3040. /**
  3041. * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
  3042. * @vsi: ptr to the VSI
  3043. *
  3044. * Free all receive software resources
  3045. **/
  3046. static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
  3047. {
  3048. int i;
  3049. if (!vsi->rx_rings)
  3050. return;
  3051. for (i = 0; i < vsi->num_queue_pairs; i++)
  3052. if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
  3053. i40e_free_rx_resources(vsi->rx_rings[i]);
  3054. }
  3055. /**
  3056. * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
  3057. * @ring: The Tx ring to configure
  3058. *
  3059. * This enables/disables XPS for a given Tx descriptor ring
  3060. * based on the TCs enabled for the VSI that ring belongs to.
  3061. **/
  3062. static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
  3063. {
  3064. int cpu;
  3065. if (!ring->q_vector || !ring->netdev || ring->ch)
  3066. return;
  3067. /* We only initialize XPS once, so as not to overwrite user settings */
  3068. if (test_and_set_bit(__I40E_TX_XPS_INIT_DONE, ring->state))
  3069. return;
  3070. cpu = cpumask_local_spread(ring->q_vector->v_idx, -1);
  3071. netif_set_xps_queue(ring->netdev, get_cpu_mask(cpu),
  3072. ring->queue_index);
  3073. }
  3074. /**
  3075. * i40e_xsk_pool - Retrieve the AF_XDP buffer pool if XDP and ZC is enabled
  3076. * @ring: The Tx or Rx ring
  3077. *
  3078. * Returns the AF_XDP buffer pool or NULL.
  3079. **/
  3080. static struct xsk_buff_pool *i40e_xsk_pool(struct i40e_ring *ring)
  3081. {
  3082. bool xdp_on = i40e_enabled_xdp_vsi(ring->vsi);
  3083. int qid = ring->queue_index;
  3084. if (ring_is_xdp(ring))
  3085. qid -= ring->vsi->alloc_queue_pairs;
  3086. if (!xdp_on || !test_bit(qid, ring->vsi->af_xdp_zc_qps))
  3087. return NULL;
  3088. return xsk_get_pool_from_qid(ring->vsi->netdev, qid);
  3089. }
  3090. /**
  3091. * i40e_configure_tx_ring - Configure a transmit ring context and rest
  3092. * @ring: The Tx ring to configure
  3093. *
  3094. * Configure the Tx descriptor ring in the HMC context.
  3095. **/
  3096. static int i40e_configure_tx_ring(struct i40e_ring *ring)
  3097. {
  3098. struct i40e_vsi *vsi = ring->vsi;
  3099. u16 pf_q = vsi->base_queue + ring->queue_index;
  3100. struct i40e_hw *hw = &vsi->back->hw;
  3101. struct i40e_hmc_obj_txq tx_ctx;
  3102. u32 qtx_ctl = 0;
  3103. int err = 0;
  3104. if (ring_is_xdp(ring))
  3105. ring->xsk_pool = i40e_xsk_pool(ring);
  3106. /* some ATR related tx ring init */
  3107. if (test_bit(I40E_FLAG_FD_ATR_ENA, vsi->back->flags)) {
  3108. ring->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
  3109. ring->atr_count = 0;
  3110. } else {
  3111. ring->atr_sample_rate = 0;
  3112. }
  3113. /* configure XPS */
  3114. i40e_config_xps_tx_ring(ring);
  3115. /* clear the context structure first */
  3116. memset(&tx_ctx, 0, sizeof(tx_ctx));
  3117. tx_ctx.new_context = 1;
  3118. tx_ctx.base = (ring->dma / 128);
  3119. tx_ctx.qlen = ring->count;
  3120. if (test_bit(I40E_FLAG_FD_SB_ENA, vsi->back->flags) ||
  3121. test_bit(I40E_FLAG_FD_ATR_ENA, vsi->back->flags))
  3122. tx_ctx.fd_ena = 1;
  3123. if (test_bit(I40E_FLAG_PTP_ENA, vsi->back->flags))
  3124. tx_ctx.timesync_ena = 1;
  3125. /* FDIR VSI tx ring can still use RS bit and writebacks */
  3126. if (vsi->type != I40E_VSI_FDIR)
  3127. tx_ctx.head_wb_ena = 1;
  3128. tx_ctx.head_wb_addr = ring->dma +
  3129. (ring->count * sizeof(struct i40e_tx_desc));
  3130. /* As part of VSI creation/update, FW allocates certain
  3131. * Tx arbitration queue sets for each TC enabled for
  3132. * the VSI. The FW returns the handles to these queue
  3133. * sets as part of the response buffer to Add VSI,
  3134. * Update VSI, etc. AQ commands. It is expected that
  3135. * these queue set handles be associated with the Tx
  3136. * queues by the driver as part of the TX queue context
  3137. * initialization. This has to be done regardless of
  3138. * DCB as by default everything is mapped to TC0.
  3139. */
  3140. if (ring->ch)
  3141. tx_ctx.rdylist =
  3142. le16_to_cpu(ring->ch->info.qs_handle[ring->dcb_tc]);
  3143. else
  3144. tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
  3145. tx_ctx.rdylist_act = 0;
  3146. /* clear the context in the HMC */
  3147. err = i40e_clear_lan_tx_queue_context(hw, pf_q);
  3148. if (err) {
  3149. dev_info(&vsi->back->pdev->dev,
  3150. "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
  3151. ring->queue_index, pf_q, err);
  3152. return -ENOMEM;
  3153. }
  3154. /* set the context in the HMC */
  3155. err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
  3156. if (err) {
  3157. dev_info(&vsi->back->pdev->dev,
  3158. "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
  3159. ring->queue_index, pf_q, err);
  3160. return -ENOMEM;
  3161. }
  3162. /* Now associate this queue with this PCI function */
  3163. if (ring->ch) {
  3164. if (ring->ch->type == I40E_VSI_VMDQ2)
  3165. qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
  3166. else
  3167. return -EINVAL;
  3168. qtx_ctl |= FIELD_PREP(I40E_QTX_CTL_VFVM_INDX_MASK,
  3169. ring->ch->vsi_number);
  3170. } else {
  3171. if (vsi->type == I40E_VSI_VMDQ2) {
  3172. qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
  3173. qtx_ctl |= FIELD_PREP(I40E_QTX_CTL_VFVM_INDX_MASK,
  3174. vsi->id);
  3175. } else {
  3176. qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
  3177. }
  3178. }
  3179. qtx_ctl |= FIELD_PREP(I40E_QTX_CTL_PF_INDX_MASK, hw->pf_id);
  3180. wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
  3181. i40e_flush(hw);
  3182. /* cache tail off for easier writes later */
  3183. ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
  3184. return 0;
  3185. }
  3186. /**
  3187. * i40e_rx_offset - Return expected offset into page to access data
  3188. * @rx_ring: Ring we are requesting offset of
  3189. *
  3190. * Returns the offset value for ring into the data buffer.
  3191. */
  3192. static unsigned int i40e_rx_offset(struct i40e_ring *rx_ring)
  3193. {
  3194. return ring_uses_build_skb(rx_ring) ? I40E_SKB_PAD : 0;
  3195. }
  3196. /**
  3197. * i40e_configure_rx_ring - Configure a receive ring context
  3198. * @ring: The Rx ring to configure
  3199. *
  3200. * Configure the Rx descriptor ring in the HMC context.
  3201. **/
  3202. static int i40e_configure_rx_ring(struct i40e_ring *ring)
  3203. {
  3204. struct i40e_vsi *vsi = ring->vsi;
  3205. u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
  3206. u16 pf_q = vsi->base_queue + ring->queue_index;
  3207. struct i40e_hw *hw = &vsi->back->hw;
  3208. struct i40e_hmc_obj_rxq rx_ctx;
  3209. int err = 0;
  3210. bool ok;
  3211. bitmap_zero(ring->state, __I40E_RING_STATE_NBITS);
  3212. /* clear the context structure first */
  3213. memset(&rx_ctx, 0, sizeof(rx_ctx));
  3214. ring->rx_buf_len = vsi->rx_buf_len;
  3215. /* XDP RX-queue info only needed for RX rings exposed to XDP */
  3216. if (ring->vsi->type != I40E_VSI_MAIN)
  3217. goto skip;
  3218. if (!xdp_rxq_info_is_reg(&ring->xdp_rxq)) {
  3219. err = __xdp_rxq_info_reg(&ring->xdp_rxq, ring->netdev,
  3220. ring->queue_index,
  3221. ring->q_vector->napi.napi_id,
  3222. ring->rx_buf_len);
  3223. if (err)
  3224. return err;
  3225. }
  3226. ring->xsk_pool = i40e_xsk_pool(ring);
  3227. if (ring->xsk_pool) {
  3228. xdp_rxq_info_unreg(&ring->xdp_rxq);
  3229. ring->rx_buf_len = xsk_pool_get_rx_frame_size(ring->xsk_pool);
  3230. err = __xdp_rxq_info_reg(&ring->xdp_rxq, ring->netdev,
  3231. ring->queue_index,
  3232. ring->q_vector->napi.napi_id,
  3233. ring->rx_buf_len);
  3234. if (err)
  3235. return err;
  3236. err = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
  3237. MEM_TYPE_XSK_BUFF_POOL,
  3238. NULL);
  3239. if (err)
  3240. return err;
  3241. dev_info(&vsi->back->pdev->dev,
  3242. "Registered XDP mem model MEM_TYPE_XSK_BUFF_POOL on Rx ring %d\n",
  3243. ring->queue_index);
  3244. } else {
  3245. err = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
  3246. MEM_TYPE_PAGE_SHARED,
  3247. NULL);
  3248. if (err)
  3249. return err;
  3250. }
  3251. skip:
  3252. xdp_init_buff(&ring->xdp, i40e_rx_pg_size(ring) / 2, &ring->xdp_rxq);
  3253. rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
  3254. BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
  3255. rx_ctx.base = (ring->dma / 128);
  3256. rx_ctx.qlen = ring->count;
  3257. /* use 16 byte descriptors */
  3258. rx_ctx.dsize = 0;
  3259. /* descriptor type is always zero
  3260. * rx_ctx.dtype = 0;
  3261. */
  3262. rx_ctx.hsplit_0 = 0;
  3263. rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
  3264. if (hw->revision_id == 0)
  3265. rx_ctx.lrxqthresh = 0;
  3266. else
  3267. rx_ctx.lrxqthresh = 1;
  3268. rx_ctx.crcstrip = 1;
  3269. rx_ctx.l2tsel = 1;
  3270. /* this controls whether VLAN is stripped from inner headers */
  3271. rx_ctx.showiv = 0;
  3272. /* set the prefena field to 1 because the manual says to */
  3273. rx_ctx.prefena = 1;
  3274. /* clear the context in the HMC */
  3275. err = i40e_clear_lan_rx_queue_context(hw, pf_q);
  3276. if (err) {
  3277. dev_info(&vsi->back->pdev->dev,
  3278. "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
  3279. ring->queue_index, pf_q, err);
  3280. return -ENOMEM;
  3281. }
  3282. /* set the context in the HMC */
  3283. err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
  3284. if (err) {
  3285. dev_info(&vsi->back->pdev->dev,
  3286. "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
  3287. ring->queue_index, pf_q, err);
  3288. return -ENOMEM;
  3289. }
  3290. /* configure Rx buffer alignment */
  3291. if (!vsi->netdev || test_bit(I40E_FLAG_LEGACY_RX_ENA, vsi->back->flags)) {
  3292. if (I40E_2K_TOO_SMALL_WITH_PADDING) {
  3293. dev_info(&vsi->back->pdev->dev,
  3294. "2k Rx buffer is too small to fit standard MTU and skb_shared_info\n");
  3295. return -EOPNOTSUPP;
  3296. }
  3297. clear_ring_build_skb_enabled(ring);
  3298. } else {
  3299. set_ring_build_skb_enabled(ring);
  3300. }
  3301. ring->rx_offset = i40e_rx_offset(ring);
  3302. /* cache tail for quicker writes, and clear the reg before use */
  3303. ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
  3304. writel(0, ring->tail);
  3305. if (ring->xsk_pool) {
  3306. xsk_pool_set_rxq_info(ring->xsk_pool, &ring->xdp_rxq);
  3307. ok = i40e_alloc_rx_buffers_zc(ring, I40E_DESC_UNUSED(ring));
  3308. } else {
  3309. ok = !i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
  3310. }
  3311. if (!ok) {
  3312. /* Log this in case the user has forgotten to give the kernel
  3313. * any buffers, even later in the application.
  3314. */
  3315. dev_info(&vsi->back->pdev->dev,
  3316. "Failed to allocate some buffers on %sRx ring %d (pf_q %d)\n",
  3317. ring->xsk_pool ? "AF_XDP ZC enabled " : "",
  3318. ring->queue_index, pf_q);
  3319. }
  3320. return 0;
  3321. }
  3322. /**
  3323. * i40e_vsi_configure_tx - Configure the VSI for Tx
  3324. * @vsi: VSI structure describing this set of rings and resources
  3325. *
  3326. * Configure the Tx VSI for operation.
  3327. **/
  3328. static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
  3329. {
  3330. int err = 0;
  3331. u16 i;
  3332. for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
  3333. err = i40e_configure_tx_ring(vsi->tx_rings[i]);
  3334. if (err || !i40e_enabled_xdp_vsi(vsi))
  3335. return err;
  3336. for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
  3337. err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
  3338. return err;
  3339. }
  3340. /**
  3341. * i40e_vsi_configure_rx - Configure the VSI for Rx
  3342. * @vsi: the VSI being configured
  3343. *
  3344. * Configure the Rx VSI for operation.
  3345. **/
  3346. static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
  3347. {
  3348. int err = 0;
  3349. u16 i;
  3350. vsi->max_frame = i40e_max_vsi_frame_size(vsi, vsi->xdp_prog);
  3351. vsi->rx_buf_len = i40e_calculate_vsi_rx_buf_len(vsi);
  3352. #if (PAGE_SIZE < 8192)
  3353. if (vsi->netdev && !I40E_2K_TOO_SMALL_WITH_PADDING &&
  3354. vsi->netdev->mtu <= ETH_DATA_LEN) {
  3355. vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
  3356. vsi->max_frame = vsi->rx_buf_len;
  3357. }
  3358. #endif
  3359. /* set up individual rings */
  3360. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  3361. err = i40e_configure_rx_ring(vsi->rx_rings[i]);
  3362. return err;
  3363. }
  3364. /**
  3365. * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
  3366. * @vsi: ptr to the VSI
  3367. **/
  3368. static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
  3369. {
  3370. struct i40e_ring *tx_ring, *rx_ring;
  3371. u16 qoffset, qcount;
  3372. int i, n;
  3373. if (!test_bit(I40E_FLAG_DCB_ENA, vsi->back->flags)) {
  3374. /* Reset the TC information */
  3375. for (i = 0; i < vsi->num_queue_pairs; i++) {
  3376. rx_ring = vsi->rx_rings[i];
  3377. tx_ring = vsi->tx_rings[i];
  3378. rx_ring->dcb_tc = 0;
  3379. tx_ring->dcb_tc = 0;
  3380. }
  3381. return;
  3382. }
  3383. for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
  3384. if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
  3385. continue;
  3386. qoffset = vsi->tc_config.tc_info[n].qoffset;
  3387. qcount = vsi->tc_config.tc_info[n].qcount;
  3388. for (i = qoffset; i < (qoffset + qcount); i++) {
  3389. rx_ring = vsi->rx_rings[i];
  3390. tx_ring = vsi->tx_rings[i];
  3391. rx_ring->dcb_tc = n;
  3392. tx_ring->dcb_tc = n;
  3393. }
  3394. }
  3395. }
  3396. /**
  3397. * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
  3398. * @vsi: ptr to the VSI
  3399. **/
  3400. static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
  3401. {
  3402. if (vsi->netdev)
  3403. i40e_set_rx_mode(vsi->netdev);
  3404. }
  3405. /**
  3406. * i40e_reset_fdir_filter_cnt - Reset flow director filter counters
  3407. * @pf: Pointer to the targeted PF
  3408. *
  3409. * Set all flow director counters to 0.
  3410. */
  3411. static void i40e_reset_fdir_filter_cnt(struct i40e_pf *pf)
  3412. {
  3413. pf->fd_tcp4_filter_cnt = 0;
  3414. pf->fd_udp4_filter_cnt = 0;
  3415. pf->fd_sctp4_filter_cnt = 0;
  3416. pf->fd_ip4_filter_cnt = 0;
  3417. pf->fd_tcp6_filter_cnt = 0;
  3418. pf->fd_udp6_filter_cnt = 0;
  3419. pf->fd_sctp6_filter_cnt = 0;
  3420. pf->fd_ip6_filter_cnt = 0;
  3421. }
  3422. /**
  3423. * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
  3424. * @vsi: Pointer to the targeted VSI
  3425. *
  3426. * This function replays the hlist on the hw where all the SB Flow Director
  3427. * filters were saved.
  3428. **/
  3429. static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
  3430. {
  3431. struct i40e_fdir_filter *filter;
  3432. struct i40e_pf *pf = vsi->back;
  3433. struct hlist_node *node;
  3434. if (!test_bit(I40E_FLAG_FD_SB_ENA, pf->flags))
  3435. return;
  3436. /* Reset FDir counters as we're replaying all existing filters */
  3437. i40e_reset_fdir_filter_cnt(pf);
  3438. hlist_for_each_entry_safe(filter, node,
  3439. &pf->fdir_filter_list, fdir_node) {
  3440. i40e_add_del_fdir(vsi, filter, true);
  3441. }
  3442. }
  3443. /**
  3444. * i40e_vsi_configure - Set up the VSI for action
  3445. * @vsi: the VSI being configured
  3446. **/
  3447. static int i40e_vsi_configure(struct i40e_vsi *vsi)
  3448. {
  3449. int err;
  3450. i40e_set_vsi_rx_mode(vsi);
  3451. i40e_restore_vlan(vsi);
  3452. i40e_vsi_config_dcb_rings(vsi);
  3453. err = i40e_vsi_configure_tx(vsi);
  3454. if (!err)
  3455. err = i40e_vsi_configure_rx(vsi);
  3456. return err;
  3457. }
  3458. /**
  3459. * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
  3460. * @vsi: the VSI being configured
  3461. **/
  3462. static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
  3463. {
  3464. bool has_xdp = i40e_enabled_xdp_vsi(vsi);
  3465. struct i40e_pf *pf = vsi->back;
  3466. struct i40e_hw *hw = &pf->hw;
  3467. u16 vector;
  3468. int i, q;
  3469. u32 qp;
  3470. /* The interrupt indexing is offset by 1 in the PFINT_ITRn
  3471. * and PFINT_LNKLSTn registers, e.g.:
  3472. * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
  3473. */
  3474. qp = vsi->base_queue;
  3475. vector = vsi->base_vector;
  3476. for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
  3477. struct i40e_q_vector *q_vector = vsi->q_vectors[i];
  3478. q_vector->rx.next_update = jiffies + 1;
  3479. q_vector->rx.target_itr =
  3480. ITR_TO_REG(vsi->rx_rings[i]->itr_setting);
  3481. wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
  3482. q_vector->rx.target_itr >> 1);
  3483. q_vector->rx.current_itr = q_vector->rx.target_itr;
  3484. q_vector->tx.next_update = jiffies + 1;
  3485. q_vector->tx.target_itr =
  3486. ITR_TO_REG(vsi->tx_rings[i]->itr_setting);
  3487. wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
  3488. q_vector->tx.target_itr >> 1);
  3489. q_vector->tx.current_itr = q_vector->tx.target_itr;
  3490. /* Set ITR for software interrupts triggered after exiting
  3491. * busy-loop polling.
  3492. */
  3493. wr32(hw, I40E_PFINT_ITRN(I40E_SW_ITR, vector - 1),
  3494. I40E_ITR_20K);
  3495. wr32(hw, I40E_PFINT_RATEN(vector - 1),
  3496. i40e_intrl_usec_to_reg(vsi->int_rate_limit));
  3497. /* begin of linked list for RX queue assigned to this vector */
  3498. wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
  3499. for (q = 0; q < q_vector->num_ringpairs; q++) {
  3500. u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
  3501. u32 val;
  3502. val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  3503. (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
  3504. (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
  3505. (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
  3506. (I40E_QUEUE_TYPE_TX <<
  3507. I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
  3508. wr32(hw, I40E_QINT_RQCTL(qp), val);
  3509. if (has_xdp) {
  3510. /* TX queue with next queue set to TX */
  3511. val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  3512. (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
  3513. (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
  3514. (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
  3515. (I40E_QUEUE_TYPE_TX <<
  3516. I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
  3517. wr32(hw, I40E_QINT_TQCTL(nextqp), val);
  3518. }
  3519. /* TX queue with next RX or end of linked list */
  3520. val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  3521. (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
  3522. (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
  3523. ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
  3524. (I40E_QUEUE_TYPE_RX <<
  3525. I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
  3526. /* Terminate the linked list */
  3527. if (q == (q_vector->num_ringpairs - 1))
  3528. val |= (I40E_QUEUE_END_OF_LIST <<
  3529. I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
  3530. wr32(hw, I40E_QINT_TQCTL(qp), val);
  3531. qp++;
  3532. }
  3533. }
  3534. i40e_flush(hw);
  3535. }
  3536. /**
  3537. * i40e_enable_misc_int_causes - enable the non-queue interrupts
  3538. * @pf: pointer to private device data structure
  3539. **/
  3540. static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
  3541. {
  3542. struct i40e_hw *hw = &pf->hw;
  3543. u32 val;
  3544. /* clear things first */
  3545. wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
  3546. rd32(hw, I40E_PFINT_ICR0); /* read to clear */
  3547. val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
  3548. I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
  3549. I40E_PFINT_ICR0_ENA_GRST_MASK |
  3550. I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
  3551. I40E_PFINT_ICR0_ENA_GPIO_MASK |
  3552. I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
  3553. I40E_PFINT_ICR0_ENA_VFLR_MASK |
  3554. I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
  3555. if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags))
  3556. val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
  3557. if (test_bit(I40E_FLAG_PTP_ENA, pf->flags))
  3558. val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
  3559. wr32(hw, I40E_PFINT_ICR0_ENA, val);
  3560. /* SW_ITR_IDX = 0, but don't change INTENA */
  3561. wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
  3562. I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
  3563. /* OTHER_ITR_IDX = 0 */
  3564. wr32(hw, I40E_PFINT_STAT_CTL0, 0);
  3565. }
  3566. /**
  3567. * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
  3568. * @vsi: the VSI being configured
  3569. **/
  3570. static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
  3571. {
  3572. u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
  3573. struct i40e_q_vector *q_vector = vsi->q_vectors[0];
  3574. struct i40e_pf *pf = vsi->back;
  3575. struct i40e_hw *hw = &pf->hw;
  3576. /* set the ITR configuration */
  3577. q_vector->rx.next_update = jiffies + 1;
  3578. q_vector->rx.target_itr = ITR_TO_REG(vsi->rx_rings[0]->itr_setting);
  3579. wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.target_itr >> 1);
  3580. q_vector->rx.current_itr = q_vector->rx.target_itr;
  3581. q_vector->tx.next_update = jiffies + 1;
  3582. q_vector->tx.target_itr = ITR_TO_REG(vsi->tx_rings[0]->itr_setting);
  3583. wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.target_itr >> 1);
  3584. q_vector->tx.current_itr = q_vector->tx.target_itr;
  3585. i40e_enable_misc_int_causes(pf);
  3586. /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
  3587. wr32(hw, I40E_PFINT_LNKLST0, 0);
  3588. /* Associate the queue pair to the vector and enable the queue
  3589. * interrupt RX queue in linked list with next queue set to TX
  3590. */
  3591. wr32(hw, I40E_QINT_RQCTL(0), I40E_QINT_RQCTL_VAL(nextqp, 0, TX));
  3592. if (i40e_enabled_xdp_vsi(vsi)) {
  3593. /* TX queue in linked list with next queue set to TX */
  3594. wr32(hw, I40E_QINT_TQCTL(nextqp),
  3595. I40E_QINT_TQCTL_VAL(nextqp, 0, TX));
  3596. }
  3597. /* last TX queue so the next RX queue doesn't matter */
  3598. wr32(hw, I40E_QINT_TQCTL(0),
  3599. I40E_QINT_TQCTL_VAL(I40E_QUEUE_END_OF_LIST, 0, RX));
  3600. i40e_flush(hw);
  3601. }
  3602. /**
  3603. * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
  3604. * @pf: board private structure
  3605. **/
  3606. void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
  3607. {
  3608. struct i40e_hw *hw = &pf->hw;
  3609. wr32(hw, I40E_PFINT_DYN_CTL0,
  3610. I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
  3611. i40e_flush(hw);
  3612. }
  3613. /**
  3614. * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
  3615. * @pf: board private structure
  3616. **/
  3617. void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
  3618. {
  3619. struct i40e_hw *hw = &pf->hw;
  3620. u32 val;
  3621. val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
  3622. I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
  3623. (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
  3624. wr32(hw, I40E_PFINT_DYN_CTL0, val);
  3625. i40e_flush(hw);
  3626. }
  3627. /**
  3628. * i40e_msix_clean_rings - MSIX mode Interrupt Handler
  3629. * @irq: interrupt number
  3630. * @data: pointer to a q_vector
  3631. **/
  3632. static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
  3633. {
  3634. struct i40e_q_vector *q_vector = data;
  3635. if (!q_vector->tx.ring && !q_vector->rx.ring)
  3636. return IRQ_HANDLED;
  3637. napi_schedule_irqoff(&q_vector->napi);
  3638. return IRQ_HANDLED;
  3639. }
  3640. /**
  3641. * i40e_irq_affinity_notify - Callback for affinity changes
  3642. * @notify: context as to what irq was changed
  3643. * @mask: the new affinity mask
  3644. *
  3645. * This is a callback function used by the irq_set_affinity_notifier function
  3646. * so that we may register to receive changes to the irq affinity masks.
  3647. **/
  3648. static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
  3649. const cpumask_t *mask)
  3650. {
  3651. struct i40e_q_vector *q_vector =
  3652. container_of(notify, struct i40e_q_vector, affinity_notify);
  3653. cpumask_copy(&q_vector->affinity_mask, mask);
  3654. }
  3655. /**
  3656. * i40e_irq_affinity_release - Callback for affinity notifier release
  3657. * @ref: internal core kernel usage
  3658. *
  3659. * This is a callback function used by the irq_set_affinity_notifier function
  3660. * to inform the current notification subscriber that they will no longer
  3661. * receive notifications.
  3662. **/
  3663. static void i40e_irq_affinity_release(struct kref *ref) {}
  3664. /**
  3665. * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
  3666. * @vsi: the VSI being configured
  3667. * @basename: name for the vector
  3668. *
  3669. * Allocates MSI-X vectors and requests interrupts from the kernel.
  3670. **/
  3671. static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
  3672. {
  3673. int q_vectors = vsi->num_q_vectors;
  3674. struct i40e_pf *pf = vsi->back;
  3675. int base = vsi->base_vector;
  3676. int rx_int_idx = 0;
  3677. int tx_int_idx = 0;
  3678. int vector, err;
  3679. int irq_num;
  3680. int cpu;
  3681. for (vector = 0; vector < q_vectors; vector++) {
  3682. struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
  3683. irq_num = pf->msix_entries[base + vector].vector;
  3684. if (q_vector->tx.ring && q_vector->rx.ring) {
  3685. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  3686. "%s-%s-%d", basename, "TxRx", rx_int_idx++);
  3687. tx_int_idx++;
  3688. } else if (q_vector->rx.ring) {
  3689. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  3690. "%s-%s-%d", basename, "rx", rx_int_idx++);
  3691. } else if (q_vector->tx.ring) {
  3692. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  3693. "%s-%s-%d", basename, "tx", tx_int_idx++);
  3694. } else {
  3695. /* skip this unused q_vector */
  3696. continue;
  3697. }
  3698. err = request_irq(irq_num,
  3699. vsi->irq_handler,
  3700. 0,
  3701. q_vector->name,
  3702. q_vector);
  3703. if (err) {
  3704. dev_info(&pf->pdev->dev,
  3705. "MSIX request_irq failed, error: %d\n", err);
  3706. goto free_queue_irqs;
  3707. }
  3708. /* register for affinity change notifications */
  3709. q_vector->irq_num = irq_num;
  3710. q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
  3711. q_vector->affinity_notify.release = i40e_irq_affinity_release;
  3712. irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
  3713. /* Spread affinity hints out across online CPUs.
  3714. *
  3715. * get_cpu_mask returns a static constant mask with
  3716. * a permanent lifetime so it's ok to pass to
  3717. * irq_update_affinity_hint without making a copy.
  3718. */
  3719. cpu = cpumask_local_spread(q_vector->v_idx, -1);
  3720. irq_update_affinity_hint(irq_num, get_cpu_mask(cpu));
  3721. }
  3722. vsi->irqs_ready = true;
  3723. return 0;
  3724. free_queue_irqs:
  3725. while (vector) {
  3726. vector--;
  3727. irq_num = pf->msix_entries[base + vector].vector;
  3728. irq_set_affinity_notifier(irq_num, NULL);
  3729. irq_update_affinity_hint(irq_num, NULL);
  3730. free_irq(irq_num, vsi->q_vectors[vector]);
  3731. }
  3732. return err;
  3733. }
  3734. /**
  3735. * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
  3736. * @vsi: the VSI being un-configured
  3737. **/
  3738. static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
  3739. {
  3740. struct i40e_pf *pf = vsi->back;
  3741. struct i40e_hw *hw = &pf->hw;
  3742. int base = vsi->base_vector;
  3743. int i;
  3744. /* disable interrupt causation from each queue */
  3745. for (i = 0; i < vsi->num_queue_pairs; i++) {
  3746. u32 val;
  3747. val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
  3748. val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
  3749. wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
  3750. val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
  3751. val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
  3752. wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
  3753. if (!i40e_enabled_xdp_vsi(vsi))
  3754. continue;
  3755. wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
  3756. }
  3757. /* disable each interrupt */
  3758. if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
  3759. for (i = vsi->base_vector;
  3760. i < (vsi->num_q_vectors + vsi->base_vector); i++)
  3761. wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
  3762. i40e_flush(hw);
  3763. for (i = 0; i < vsi->num_q_vectors; i++)
  3764. synchronize_irq(pf->msix_entries[i + base].vector);
  3765. } else {
  3766. /* Legacy and MSI mode - this stops all interrupt handling */
  3767. wr32(hw, I40E_PFINT_ICR0_ENA, 0);
  3768. wr32(hw, I40E_PFINT_DYN_CTL0, 0);
  3769. i40e_flush(hw);
  3770. synchronize_irq(pf->pdev->irq);
  3771. }
  3772. }
  3773. /**
  3774. * i40e_vsi_enable_irq - Enable IRQ for the given VSI
  3775. * @vsi: the VSI being configured
  3776. **/
  3777. static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
  3778. {
  3779. struct i40e_pf *pf = vsi->back;
  3780. int i;
  3781. if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
  3782. for (i = 0; i < vsi->num_q_vectors; i++)
  3783. i40e_irq_dynamic_enable(vsi, i);
  3784. } else {
  3785. i40e_irq_dynamic_enable_icr0(pf);
  3786. }
  3787. i40e_flush(&pf->hw);
  3788. return 0;
  3789. }
  3790. /**
  3791. * i40e_free_misc_vector - Free the vector that handles non-queue events
  3792. * @pf: board private structure
  3793. **/
  3794. static void i40e_free_misc_vector(struct i40e_pf *pf)
  3795. {
  3796. /* Disable ICR 0 */
  3797. wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
  3798. i40e_flush(&pf->hw);
  3799. if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags) && pf->msix_entries) {
  3800. free_irq(pf->msix_entries[0].vector, pf);
  3801. clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
  3802. }
  3803. }
  3804. /**
  3805. * i40e_intr - MSI/Legacy and non-queue interrupt handler
  3806. * @irq: interrupt number
  3807. * @data: pointer to a q_vector
  3808. *
  3809. * This is the handler used for all MSI/Legacy interrupts, and deals
  3810. * with both queue and non-queue interrupts. This is also used in
  3811. * MSIX mode to handle the non-queue interrupts.
  3812. **/
  3813. static irqreturn_t i40e_intr(int irq, void *data)
  3814. {
  3815. struct i40e_pf *pf = (struct i40e_pf *)data;
  3816. struct i40e_hw *hw = &pf->hw;
  3817. irqreturn_t ret = IRQ_NONE;
  3818. u32 icr0, icr0_remaining;
  3819. u32 val, ena_mask;
  3820. icr0 = rd32(hw, I40E_PFINT_ICR0);
  3821. ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
  3822. /* if sharing a legacy IRQ, we might get called w/o an intr pending */
  3823. if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
  3824. goto enable_intr;
  3825. /* if interrupt but no bits showing, must be SWINT */
  3826. if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
  3827. (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
  3828. pf->sw_int_count++;
  3829. if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags) &&
  3830. (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
  3831. ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
  3832. dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
  3833. set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
  3834. }
  3835. /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
  3836. if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
  3837. struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
  3838. struct i40e_q_vector *q_vector = vsi->q_vectors[0];
  3839. /* We do not have a way to disarm Queue causes while leaving
  3840. * interrupt enabled for all other causes, ideally
  3841. * interrupt should be disabled while we are in NAPI but
  3842. * this is not a performance path and napi_schedule()
  3843. * can deal with rescheduling.
  3844. */
  3845. if (!test_bit(__I40E_DOWN, pf->state))
  3846. napi_schedule_irqoff(&q_vector->napi);
  3847. }
  3848. if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
  3849. ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
  3850. set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
  3851. i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
  3852. }
  3853. if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
  3854. ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
  3855. set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
  3856. }
  3857. if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
  3858. /* disable any further VFLR event notifications */
  3859. if (test_bit(__I40E_VF_RESETS_DISABLED, pf->state)) {
  3860. u32 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
  3861. reg &= ~I40E_PFINT_ICR0_VFLR_MASK;
  3862. wr32(hw, I40E_PFINT_ICR0_ENA, reg);
  3863. } else {
  3864. ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
  3865. set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
  3866. }
  3867. }
  3868. if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
  3869. if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
  3870. set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
  3871. ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
  3872. val = rd32(hw, I40E_GLGEN_RSTAT);
  3873. val = FIELD_GET(I40E_GLGEN_RSTAT_RESET_TYPE_MASK, val);
  3874. if (val == I40E_RESET_CORER) {
  3875. pf->corer_count++;
  3876. } else if (val == I40E_RESET_GLOBR) {
  3877. pf->globr_count++;
  3878. } else if (val == I40E_RESET_EMPR) {
  3879. pf->empr_count++;
  3880. set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
  3881. }
  3882. }
  3883. if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
  3884. icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
  3885. dev_info(&pf->pdev->dev, "HMC error interrupt\n");
  3886. dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
  3887. rd32(hw, I40E_PFHMC_ERRORINFO),
  3888. rd32(hw, I40E_PFHMC_ERRORDATA));
  3889. }
  3890. if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
  3891. u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
  3892. if (prttsyn_stat & I40E_PRTTSYN_STAT_0_EVENT0_MASK)
  3893. schedule_work(&pf->ptp_extts0_work);
  3894. if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK)
  3895. i40e_ptp_tx_hwtstamp(pf);
  3896. icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
  3897. }
  3898. /* If a critical error is pending we have no choice but to reset the
  3899. * device.
  3900. * Report and mask out any remaining unexpected interrupts.
  3901. */
  3902. icr0_remaining = icr0 & ena_mask;
  3903. if (icr0_remaining) {
  3904. dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
  3905. icr0_remaining);
  3906. if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
  3907. (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
  3908. (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
  3909. dev_info(&pf->pdev->dev, "device will be reset\n");
  3910. set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
  3911. i40e_service_event_schedule(pf);
  3912. }
  3913. ena_mask &= ~icr0_remaining;
  3914. }
  3915. ret = IRQ_HANDLED;
  3916. enable_intr:
  3917. /* re-enable interrupt causes */
  3918. wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
  3919. if (!test_bit(__I40E_DOWN, pf->state) ||
  3920. test_bit(__I40E_RECOVERY_MODE, pf->state)) {
  3921. i40e_service_event_schedule(pf);
  3922. i40e_irq_dynamic_enable_icr0(pf);
  3923. }
  3924. return ret;
  3925. }
  3926. /**
  3927. * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
  3928. * @tx_ring: tx ring to clean
  3929. * @budget: how many cleans we're allowed
  3930. *
  3931. * Returns true if there's any budget left (e.g. the clean is finished)
  3932. **/
  3933. static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
  3934. {
  3935. struct i40e_vsi *vsi = tx_ring->vsi;
  3936. u16 i = tx_ring->next_to_clean;
  3937. struct i40e_tx_buffer *tx_buf;
  3938. struct i40e_tx_desc *tx_desc;
  3939. tx_buf = &tx_ring->tx_bi[i];
  3940. tx_desc = I40E_TX_DESC(tx_ring, i);
  3941. i -= tx_ring->count;
  3942. do {
  3943. struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
  3944. /* if next_to_watch is not set then there is no work pending */
  3945. if (!eop_desc)
  3946. break;
  3947. /* prevent any other reads prior to eop_desc */
  3948. smp_rmb();
  3949. /* if the descriptor isn't done, no work yet to do */
  3950. if (!(eop_desc->cmd_type_offset_bsz &
  3951. cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
  3952. break;
  3953. /* clear next_to_watch to prevent false hangs */
  3954. tx_buf->next_to_watch = NULL;
  3955. tx_desc->buffer_addr = 0;
  3956. tx_desc->cmd_type_offset_bsz = 0;
  3957. /* move past filter desc */
  3958. tx_buf++;
  3959. tx_desc++;
  3960. i++;
  3961. if (unlikely(!i)) {
  3962. i -= tx_ring->count;
  3963. tx_buf = tx_ring->tx_bi;
  3964. tx_desc = I40E_TX_DESC(tx_ring, 0);
  3965. }
  3966. /* unmap skb header data */
  3967. dma_unmap_single(tx_ring->dev,
  3968. dma_unmap_addr(tx_buf, dma),
  3969. dma_unmap_len(tx_buf, len),
  3970. DMA_TO_DEVICE);
  3971. if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
  3972. kfree(tx_buf->raw_buf);
  3973. tx_buf->raw_buf = NULL;
  3974. tx_buf->tx_flags = 0;
  3975. tx_buf->next_to_watch = NULL;
  3976. dma_unmap_len_set(tx_buf, len, 0);
  3977. tx_desc->buffer_addr = 0;
  3978. tx_desc->cmd_type_offset_bsz = 0;
  3979. /* move us past the eop_desc for start of next FD desc */
  3980. tx_buf++;
  3981. tx_desc++;
  3982. i++;
  3983. if (unlikely(!i)) {
  3984. i -= tx_ring->count;
  3985. tx_buf = tx_ring->tx_bi;
  3986. tx_desc = I40E_TX_DESC(tx_ring, 0);
  3987. }
  3988. /* update budget accounting */
  3989. budget--;
  3990. } while (likely(budget));
  3991. i += tx_ring->count;
  3992. tx_ring->next_to_clean = i;
  3993. if (test_bit(I40E_FLAG_MSIX_ENA, vsi->back->flags))
  3994. i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
  3995. return budget > 0;
  3996. }
  3997. /**
  3998. * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
  3999. * @irq: interrupt number
  4000. * @data: pointer to a q_vector
  4001. **/
  4002. static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
  4003. {
  4004. struct i40e_q_vector *q_vector = data;
  4005. struct i40e_vsi *vsi;
  4006. if (!q_vector->tx.ring)
  4007. return IRQ_HANDLED;
  4008. vsi = q_vector->tx.ring->vsi;
  4009. i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
  4010. return IRQ_HANDLED;
  4011. }
  4012. /**
  4013. * i40e_map_vector_to_qp - Assigns the queue pair to the vector
  4014. * @vsi: the VSI being configured
  4015. * @v_idx: vector index
  4016. * @qp_idx: queue pair index
  4017. **/
  4018. static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
  4019. {
  4020. struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
  4021. struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
  4022. struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
  4023. tx_ring->q_vector = q_vector;
  4024. tx_ring->next = q_vector->tx.ring;
  4025. q_vector->tx.ring = tx_ring;
  4026. q_vector->tx.count++;
  4027. /* Place XDP Tx ring in the same q_vector ring list as regular Tx */
  4028. if (i40e_enabled_xdp_vsi(vsi)) {
  4029. struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
  4030. xdp_ring->q_vector = q_vector;
  4031. xdp_ring->next = q_vector->tx.ring;
  4032. q_vector->tx.ring = xdp_ring;
  4033. q_vector->tx.count++;
  4034. }
  4035. rx_ring->q_vector = q_vector;
  4036. rx_ring->next = q_vector->rx.ring;
  4037. q_vector->rx.ring = rx_ring;
  4038. q_vector->rx.count++;
  4039. }
  4040. /**
  4041. * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
  4042. * @vsi: the VSI being configured
  4043. *
  4044. * This function maps descriptor rings to the queue-specific vectors
  4045. * we were allotted through the MSI-X enabling code. Ideally, we'd have
  4046. * one vector per queue pair, but on a constrained vector budget, we
  4047. * group the queue pairs as "efficiently" as possible.
  4048. **/
  4049. static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
  4050. {
  4051. int qp_remaining = vsi->num_queue_pairs;
  4052. int q_vectors = vsi->num_q_vectors;
  4053. int num_ringpairs;
  4054. int v_start = 0;
  4055. int qp_idx = 0;
  4056. /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
  4057. * group them so there are multiple queues per vector.
  4058. * It is also important to go through all the vectors available to be
  4059. * sure that if we don't use all the vectors, that the remaining vectors
  4060. * are cleared. This is especially important when decreasing the
  4061. * number of queues in use.
  4062. */
  4063. for (; v_start < q_vectors; v_start++) {
  4064. struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
  4065. num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
  4066. q_vector->num_ringpairs = num_ringpairs;
  4067. q_vector->reg_idx = q_vector->v_idx + vsi->base_vector - 1;
  4068. q_vector->rx.count = 0;
  4069. q_vector->tx.count = 0;
  4070. q_vector->rx.ring = NULL;
  4071. q_vector->tx.ring = NULL;
  4072. while (num_ringpairs--) {
  4073. i40e_map_vector_to_qp(vsi, v_start, qp_idx);
  4074. qp_idx++;
  4075. qp_remaining--;
  4076. }
  4077. }
  4078. }
  4079. /**
  4080. * i40e_vsi_request_irq - Request IRQ from the OS
  4081. * @vsi: the VSI being configured
  4082. * @basename: name for the vector
  4083. **/
  4084. static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
  4085. {
  4086. struct i40e_pf *pf = vsi->back;
  4087. int err;
  4088. if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
  4089. err = i40e_vsi_request_irq_msix(vsi, basename);
  4090. else if (test_bit(I40E_FLAG_MSI_ENA, pf->flags))
  4091. err = request_irq(pf->pdev->irq, i40e_intr, 0,
  4092. pf->int_name, pf);
  4093. else
  4094. err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
  4095. pf->int_name, pf);
  4096. if (err)
  4097. dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
  4098. return err;
  4099. }
  4100. #ifdef CONFIG_NET_POLL_CONTROLLER
  4101. /**
  4102. * i40e_netpoll - A Polling 'interrupt' handler
  4103. * @netdev: network interface device structure
  4104. *
  4105. * This is used by netconsole to send skbs without having to re-enable
  4106. * interrupts. It's not called while the normal interrupt routine is executing.
  4107. **/
  4108. static void i40e_netpoll(struct net_device *netdev)
  4109. {
  4110. struct i40e_netdev_priv *np = netdev_priv(netdev);
  4111. struct i40e_vsi *vsi = np->vsi;
  4112. struct i40e_pf *pf = vsi->back;
  4113. int i;
  4114. /* if interface is down do nothing */
  4115. if (test_bit(__I40E_VSI_DOWN, vsi->state))
  4116. return;
  4117. if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
  4118. for (i = 0; i < vsi->num_q_vectors; i++)
  4119. i40e_msix_clean_rings(0, vsi->q_vectors[i]);
  4120. } else {
  4121. i40e_intr(pf->pdev->irq, netdev);
  4122. }
  4123. }
  4124. #endif
  4125. #define I40E_QTX_ENA_WAIT_COUNT 50
  4126. /**
  4127. * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
  4128. * @pf: the PF being configured
  4129. * @pf_q: the PF queue
  4130. * @enable: enable or disable state of the queue
  4131. *
  4132. * This routine will wait for the given Tx queue of the PF to reach the
  4133. * enabled or disabled state.
  4134. * Returns -ETIMEDOUT in case of failing to reach the requested state after
  4135. * multiple retries; else will return 0 in case of success.
  4136. **/
  4137. static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
  4138. {
  4139. int i;
  4140. u32 tx_reg;
  4141. for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
  4142. tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
  4143. if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
  4144. break;
  4145. usleep_range(10, 20);
  4146. }
  4147. if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
  4148. return -ETIMEDOUT;
  4149. return 0;
  4150. }
  4151. /**
  4152. * i40e_control_tx_q - Start or stop a particular Tx queue
  4153. * @pf: the PF structure
  4154. * @pf_q: the PF queue to configure
  4155. * @enable: start or stop the queue
  4156. *
  4157. * This function enables or disables a single queue. Note that any delay
  4158. * required after the operation is expected to be handled by the caller of
  4159. * this function.
  4160. **/
  4161. static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
  4162. {
  4163. struct i40e_hw *hw = &pf->hw;
  4164. u32 tx_reg;
  4165. int i;
  4166. /* warn the TX unit of coming changes */
  4167. i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
  4168. if (!enable)
  4169. usleep_range(10, 20);
  4170. for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
  4171. tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
  4172. if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
  4173. ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
  4174. break;
  4175. usleep_range(1000, 2000);
  4176. }
  4177. /* Skip if the queue is already in the requested state */
  4178. if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
  4179. return;
  4180. /* turn on/off the queue */
  4181. if (enable) {
  4182. wr32(hw, I40E_QTX_HEAD(pf_q), 0);
  4183. tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
  4184. } else {
  4185. tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
  4186. }
  4187. wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
  4188. }
  4189. /**
  4190. * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
  4191. * @seid: VSI SEID
  4192. * @pf: the PF structure
  4193. * @pf_q: the PF queue to configure
  4194. * @is_xdp: true if the queue is used for XDP
  4195. * @enable: start or stop the queue
  4196. **/
  4197. int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
  4198. bool is_xdp, bool enable)
  4199. {
  4200. int ret;
  4201. i40e_control_tx_q(pf, pf_q, enable);
  4202. /* wait for the change to finish */
  4203. ret = i40e_pf_txq_wait(pf, pf_q, enable);
  4204. if (ret) {
  4205. dev_info(&pf->pdev->dev,
  4206. "VSI seid %d %sTx ring %d %sable timeout\n",
  4207. seid, (is_xdp ? "XDP " : ""), pf_q,
  4208. (enable ? "en" : "dis"));
  4209. }
  4210. return ret;
  4211. }
  4212. /**
  4213. * i40e_vsi_enable_tx - Start a VSI's rings
  4214. * @vsi: the VSI being configured
  4215. **/
  4216. static int i40e_vsi_enable_tx(struct i40e_vsi *vsi)
  4217. {
  4218. struct i40e_pf *pf = vsi->back;
  4219. int i, pf_q, ret = 0;
  4220. pf_q = vsi->base_queue;
  4221. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  4222. ret = i40e_control_wait_tx_q(vsi->seid, pf,
  4223. pf_q,
  4224. false /*is xdp*/, true);
  4225. if (ret)
  4226. break;
  4227. if (!i40e_enabled_xdp_vsi(vsi))
  4228. continue;
  4229. ret = i40e_control_wait_tx_q(vsi->seid, pf,
  4230. pf_q + vsi->alloc_queue_pairs,
  4231. true /*is xdp*/, true);
  4232. if (ret)
  4233. break;
  4234. }
  4235. return ret;
  4236. }
  4237. /**
  4238. * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
  4239. * @pf: the PF being configured
  4240. * @pf_q: the PF queue
  4241. * @enable: enable or disable state of the queue
  4242. *
  4243. * This routine will wait for the given Rx queue of the PF to reach the
  4244. * enabled or disabled state.
  4245. * Returns -ETIMEDOUT in case of failing to reach the requested state after
  4246. * multiple retries; else will return 0 in case of success.
  4247. **/
  4248. static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
  4249. {
  4250. int i;
  4251. u32 rx_reg;
  4252. for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
  4253. rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
  4254. if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
  4255. break;
  4256. usleep_range(10, 20);
  4257. }
  4258. if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
  4259. return -ETIMEDOUT;
  4260. return 0;
  4261. }
  4262. /**
  4263. * i40e_control_rx_q - Start or stop a particular Rx queue
  4264. * @pf: the PF structure
  4265. * @pf_q: the PF queue to configure
  4266. * @enable: start or stop the queue
  4267. *
  4268. * This function enables or disables a single queue. Note that
  4269. * any delay required after the operation is expected to be
  4270. * handled by the caller of this function.
  4271. **/
  4272. static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
  4273. {
  4274. struct i40e_hw *hw = &pf->hw;
  4275. u32 rx_reg;
  4276. int i;
  4277. for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
  4278. rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
  4279. if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
  4280. ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
  4281. break;
  4282. usleep_range(1000, 2000);
  4283. }
  4284. /* Skip if the queue is already in the requested state */
  4285. if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
  4286. return;
  4287. /* turn on/off the queue */
  4288. if (enable)
  4289. rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
  4290. else
  4291. rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
  4292. wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
  4293. }
  4294. /**
  4295. * i40e_control_wait_rx_q
  4296. * @pf: the PF structure
  4297. * @pf_q: queue being configured
  4298. * @enable: start or stop the rings
  4299. *
  4300. * This function enables or disables a single queue along with waiting
  4301. * for the change to finish. The caller of this function should handle
  4302. * the delays needed in the case of disabling queues.
  4303. **/
  4304. int i40e_control_wait_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
  4305. {
  4306. int ret = 0;
  4307. i40e_control_rx_q(pf, pf_q, enable);
  4308. /* wait for the change to finish */
  4309. ret = i40e_pf_rxq_wait(pf, pf_q, enable);
  4310. if (ret)
  4311. return ret;
  4312. return ret;
  4313. }
  4314. /**
  4315. * i40e_vsi_enable_rx - Start a VSI's rings
  4316. * @vsi: the VSI being configured
  4317. **/
  4318. static int i40e_vsi_enable_rx(struct i40e_vsi *vsi)
  4319. {
  4320. struct i40e_pf *pf = vsi->back;
  4321. int i, pf_q, ret = 0;
  4322. pf_q = vsi->base_queue;
  4323. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  4324. ret = i40e_control_wait_rx_q(pf, pf_q, true);
  4325. if (ret) {
  4326. dev_info(&pf->pdev->dev,
  4327. "VSI seid %d Rx ring %d enable timeout\n",
  4328. vsi->seid, pf_q);
  4329. break;
  4330. }
  4331. }
  4332. return ret;
  4333. }
  4334. /**
  4335. * i40e_vsi_start_rings - Start a VSI's rings
  4336. * @vsi: the VSI being configured
  4337. **/
  4338. int i40e_vsi_start_rings(struct i40e_vsi *vsi)
  4339. {
  4340. int ret = 0;
  4341. /* do rx first for enable and last for disable */
  4342. ret = i40e_vsi_enable_rx(vsi);
  4343. if (ret)
  4344. return ret;
  4345. ret = i40e_vsi_enable_tx(vsi);
  4346. return ret;
  4347. }
  4348. #define I40E_DISABLE_TX_GAP_MSEC 50
  4349. /**
  4350. * i40e_vsi_stop_rings - Stop a VSI's rings
  4351. * @vsi: the VSI being configured
  4352. **/
  4353. void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
  4354. {
  4355. struct i40e_pf *pf = vsi->back;
  4356. u32 pf_q, tx_q_end, rx_q_end;
  4357. /* When port TX is suspended, don't wait */
  4358. if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
  4359. return i40e_vsi_stop_rings_no_wait(vsi);
  4360. tx_q_end = vsi->base_queue +
  4361. vsi->alloc_queue_pairs * (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
  4362. for (pf_q = vsi->base_queue; pf_q < tx_q_end; pf_q++)
  4363. i40e_pre_tx_queue_cfg(&pf->hw, pf_q, false);
  4364. rx_q_end = vsi->base_queue + vsi->num_queue_pairs;
  4365. for (pf_q = vsi->base_queue; pf_q < rx_q_end; pf_q++)
  4366. i40e_control_rx_q(pf, pf_q, false);
  4367. msleep(I40E_DISABLE_TX_GAP_MSEC);
  4368. for (pf_q = vsi->base_queue; pf_q < tx_q_end; pf_q++)
  4369. wr32(&pf->hw, I40E_QTX_ENA(pf_q), 0);
  4370. i40e_vsi_wait_queues_disabled(vsi);
  4371. }
  4372. /**
  4373. * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
  4374. * @vsi: the VSI being shutdown
  4375. *
  4376. * This function stops all the rings for a VSI but does not delay to verify
  4377. * that rings have been disabled. It is expected that the caller is shutting
  4378. * down multiple VSIs at once and will delay together for all the VSIs after
  4379. * initiating the shutdown. This is particularly useful for shutting down lots
  4380. * of VFs together. Otherwise, a large delay can be incurred while configuring
  4381. * each VSI in serial.
  4382. **/
  4383. void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
  4384. {
  4385. struct i40e_pf *pf = vsi->back;
  4386. int i, pf_q;
  4387. pf_q = vsi->base_queue;
  4388. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  4389. i40e_control_tx_q(pf, pf_q, false);
  4390. i40e_control_rx_q(pf, pf_q, false);
  4391. }
  4392. }
  4393. /**
  4394. * i40e_vsi_free_irq - Free the irq association with the OS
  4395. * @vsi: the VSI being configured
  4396. **/
  4397. static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
  4398. {
  4399. struct i40e_pf *pf = vsi->back;
  4400. struct i40e_hw *hw = &pf->hw;
  4401. int base = vsi->base_vector;
  4402. u32 val, qp;
  4403. int i;
  4404. if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
  4405. if (!vsi->q_vectors)
  4406. return;
  4407. if (!vsi->irqs_ready)
  4408. return;
  4409. vsi->irqs_ready = false;
  4410. for (i = 0; i < vsi->num_q_vectors; i++) {
  4411. int irq_num;
  4412. u16 vector;
  4413. vector = i + base;
  4414. irq_num = pf->msix_entries[vector].vector;
  4415. /* free only the irqs that were actually requested */
  4416. if (!vsi->q_vectors[i] ||
  4417. !vsi->q_vectors[i]->num_ringpairs)
  4418. continue;
  4419. /* clear the affinity notifier in the IRQ descriptor */
  4420. irq_set_affinity_notifier(irq_num, NULL);
  4421. /* remove our suggested affinity mask for this IRQ */
  4422. irq_update_affinity_hint(irq_num, NULL);
  4423. free_irq(irq_num, vsi->q_vectors[i]);
  4424. /* Tear down the interrupt queue link list
  4425. *
  4426. * We know that they come in pairs and always
  4427. * the Rx first, then the Tx. To clear the
  4428. * link list, stick the EOL value into the
  4429. * next_q field of the registers.
  4430. */
  4431. val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
  4432. qp = FIELD_GET(I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK,
  4433. val);
  4434. val |= I40E_QUEUE_END_OF_LIST
  4435. << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
  4436. wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
  4437. while (qp != I40E_QUEUE_END_OF_LIST) {
  4438. u32 next;
  4439. val = rd32(hw, I40E_QINT_RQCTL(qp));
  4440. val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
  4441. I40E_QINT_RQCTL_MSIX0_INDX_MASK |
  4442. I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  4443. I40E_QINT_RQCTL_INTEVENT_MASK);
  4444. val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
  4445. I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
  4446. wr32(hw, I40E_QINT_RQCTL(qp), val);
  4447. val = rd32(hw, I40E_QINT_TQCTL(qp));
  4448. next = FIELD_GET(I40E_QINT_TQCTL_NEXTQ_INDX_MASK,
  4449. val);
  4450. val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
  4451. I40E_QINT_TQCTL_MSIX0_INDX_MASK |
  4452. I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  4453. I40E_QINT_TQCTL_INTEVENT_MASK);
  4454. val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
  4455. I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
  4456. wr32(hw, I40E_QINT_TQCTL(qp), val);
  4457. qp = next;
  4458. }
  4459. }
  4460. } else {
  4461. free_irq(pf->pdev->irq, pf);
  4462. val = rd32(hw, I40E_PFINT_LNKLST0);
  4463. qp = FIELD_GET(I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK, val);
  4464. val |= I40E_QUEUE_END_OF_LIST
  4465. << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
  4466. wr32(hw, I40E_PFINT_LNKLST0, val);
  4467. val = rd32(hw, I40E_QINT_RQCTL(qp));
  4468. val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
  4469. I40E_QINT_RQCTL_MSIX0_INDX_MASK |
  4470. I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  4471. I40E_QINT_RQCTL_INTEVENT_MASK);
  4472. val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
  4473. I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
  4474. wr32(hw, I40E_QINT_RQCTL(qp), val);
  4475. val = rd32(hw, I40E_QINT_TQCTL(qp));
  4476. val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
  4477. I40E_QINT_TQCTL_MSIX0_INDX_MASK |
  4478. I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  4479. I40E_QINT_TQCTL_INTEVENT_MASK);
  4480. val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
  4481. I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
  4482. wr32(hw, I40E_QINT_TQCTL(qp), val);
  4483. }
  4484. }
  4485. /**
  4486. * i40e_free_q_vector - Free memory allocated for specific interrupt vector
  4487. * @vsi: the VSI being configured
  4488. * @v_idx: Index of vector to be freed
  4489. *
  4490. * This function frees the memory allocated to the q_vector. In addition if
  4491. * NAPI is enabled it will delete any references to the NAPI struct prior
  4492. * to freeing the q_vector.
  4493. **/
  4494. static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
  4495. {
  4496. struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
  4497. struct i40e_ring *ring;
  4498. if (!q_vector)
  4499. return;
  4500. /* disassociate q_vector from rings */
  4501. i40e_for_each_ring(ring, q_vector->tx)
  4502. ring->q_vector = NULL;
  4503. i40e_for_each_ring(ring, q_vector->rx)
  4504. ring->q_vector = NULL;
  4505. /* only VSI w/ an associated netdev is set up w/ NAPI */
  4506. if (vsi->netdev)
  4507. netif_napi_del(&q_vector->napi);
  4508. vsi->q_vectors[v_idx] = NULL;
  4509. kfree_rcu(q_vector, rcu);
  4510. }
  4511. /**
  4512. * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
  4513. * @vsi: the VSI being un-configured
  4514. *
  4515. * This frees the memory allocated to the q_vectors and
  4516. * deletes references to the NAPI struct.
  4517. **/
  4518. static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
  4519. {
  4520. int v_idx;
  4521. for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
  4522. i40e_free_q_vector(vsi, v_idx);
  4523. }
  4524. /**
  4525. * i40e_reset_interrupt_capability - Disable interrupt setup in OS
  4526. * @pf: board private structure
  4527. **/
  4528. static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
  4529. {
  4530. /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
  4531. if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
  4532. pci_disable_msix(pf->pdev);
  4533. kfree(pf->msix_entries);
  4534. pf->msix_entries = NULL;
  4535. kfree(pf->irq_pile);
  4536. pf->irq_pile = NULL;
  4537. } else if (test_bit(I40E_FLAG_MSI_ENA, pf->flags)) {
  4538. pci_disable_msi(pf->pdev);
  4539. }
  4540. clear_bit(I40E_FLAG_MSI_ENA, pf->flags);
  4541. clear_bit(I40E_FLAG_MSIX_ENA, pf->flags);
  4542. }
  4543. /**
  4544. * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
  4545. * @pf: board private structure
  4546. *
  4547. * We go through and clear interrupt specific resources and reset the structure
  4548. * to pre-load conditions
  4549. **/
  4550. static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
  4551. {
  4552. struct i40e_vsi *vsi;
  4553. int i;
  4554. if (test_bit(__I40E_MISC_IRQ_REQUESTED, pf->state))
  4555. i40e_free_misc_vector(pf);
  4556. i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
  4557. I40E_IWARP_IRQ_PILE_ID);
  4558. i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
  4559. i40e_pf_for_each_vsi(pf, i, vsi)
  4560. i40e_vsi_free_q_vectors(vsi);
  4561. i40e_reset_interrupt_capability(pf);
  4562. }
  4563. /**
  4564. * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
  4565. * @vsi: the VSI being configured
  4566. **/
  4567. static void i40e_napi_enable_all(struct i40e_vsi *vsi)
  4568. {
  4569. int q_idx;
  4570. if (!vsi->netdev)
  4571. return;
  4572. for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
  4573. struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
  4574. if (q_vector->rx.ring || q_vector->tx.ring)
  4575. napi_enable(&q_vector->napi);
  4576. }
  4577. }
  4578. /**
  4579. * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
  4580. * @vsi: the VSI being configured
  4581. **/
  4582. static void i40e_napi_disable_all(struct i40e_vsi *vsi)
  4583. {
  4584. int q_idx;
  4585. if (!vsi->netdev)
  4586. return;
  4587. for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
  4588. struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
  4589. if (q_vector->rx.ring || q_vector->tx.ring)
  4590. napi_disable(&q_vector->napi);
  4591. }
  4592. }
  4593. /**
  4594. * i40e_vsi_close - Shut down a VSI
  4595. * @vsi: the vsi to be quelled
  4596. **/
  4597. static void i40e_vsi_close(struct i40e_vsi *vsi)
  4598. {
  4599. struct i40e_pf *pf = vsi->back;
  4600. if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
  4601. i40e_down(vsi);
  4602. i40e_vsi_free_irq(vsi);
  4603. i40e_vsi_free_tx_resources(vsi);
  4604. i40e_vsi_free_rx_resources(vsi);
  4605. vsi->current_netdev_flags = 0;
  4606. set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
  4607. if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
  4608. set_bit(__I40E_CLIENT_RESET, pf->state);
  4609. }
  4610. /**
  4611. * i40e_quiesce_vsi - Pause a given VSI
  4612. * @vsi: the VSI being paused
  4613. **/
  4614. static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
  4615. {
  4616. if (test_bit(__I40E_VSI_DOWN, vsi->state))
  4617. return;
  4618. set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
  4619. if (vsi->netdev && netif_running(vsi->netdev))
  4620. vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
  4621. else
  4622. i40e_vsi_close(vsi);
  4623. }
  4624. /**
  4625. * i40e_unquiesce_vsi - Resume a given VSI
  4626. * @vsi: the VSI being resumed
  4627. **/
  4628. static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
  4629. {
  4630. if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
  4631. return;
  4632. if (vsi->netdev && netif_running(vsi->netdev))
  4633. vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
  4634. else
  4635. i40e_vsi_open(vsi); /* this clears the DOWN bit */
  4636. }
  4637. /**
  4638. * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
  4639. * @pf: the PF
  4640. **/
  4641. static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
  4642. {
  4643. struct i40e_vsi *vsi;
  4644. int v;
  4645. i40e_pf_for_each_vsi(pf, v, vsi)
  4646. i40e_quiesce_vsi(vsi);
  4647. }
  4648. /**
  4649. * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
  4650. * @pf: the PF
  4651. **/
  4652. static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
  4653. {
  4654. struct i40e_vsi *vsi;
  4655. int v;
  4656. i40e_pf_for_each_vsi(pf, v, vsi)
  4657. i40e_unquiesce_vsi(vsi);
  4658. }
  4659. /**
  4660. * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
  4661. * @vsi: the VSI being configured
  4662. *
  4663. * Wait until all queues on a given VSI have been disabled.
  4664. **/
  4665. int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
  4666. {
  4667. struct i40e_pf *pf = vsi->back;
  4668. int i, pf_q, ret;
  4669. pf_q = vsi->base_queue;
  4670. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  4671. /* Check and wait for the Tx queue */
  4672. ret = i40e_pf_txq_wait(pf, pf_q, false);
  4673. if (ret) {
  4674. dev_info(&pf->pdev->dev,
  4675. "VSI seid %d Tx ring %d disable timeout\n",
  4676. vsi->seid, pf_q);
  4677. return ret;
  4678. }
  4679. if (!i40e_enabled_xdp_vsi(vsi))
  4680. goto wait_rx;
  4681. /* Check and wait for the XDP Tx queue */
  4682. ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
  4683. false);
  4684. if (ret) {
  4685. dev_info(&pf->pdev->dev,
  4686. "VSI seid %d XDP Tx ring %d disable timeout\n",
  4687. vsi->seid, pf_q);
  4688. return ret;
  4689. }
  4690. wait_rx:
  4691. /* Check and wait for the Rx queue */
  4692. ret = i40e_pf_rxq_wait(pf, pf_q, false);
  4693. if (ret) {
  4694. dev_info(&pf->pdev->dev,
  4695. "VSI seid %d Rx ring %d disable timeout\n",
  4696. vsi->seid, pf_q);
  4697. return ret;
  4698. }
  4699. }
  4700. return 0;
  4701. }
  4702. #ifdef CONFIG_I40E_DCB
  4703. /**
  4704. * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
  4705. * @pf: the PF
  4706. *
  4707. * This function waits for the queues to be in disabled state for all the
  4708. * VSIs that are managed by this PF.
  4709. **/
  4710. static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
  4711. {
  4712. struct i40e_vsi *vsi;
  4713. int v, ret = 0;
  4714. i40e_pf_for_each_vsi(pf, v, vsi) {
  4715. ret = i40e_vsi_wait_queues_disabled(vsi);
  4716. if (ret)
  4717. break;
  4718. }
  4719. return ret;
  4720. }
  4721. #endif
  4722. /**
  4723. * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
  4724. * @pf: pointer to PF
  4725. *
  4726. * Get TC map for ISCSI PF type that will include iSCSI TC
  4727. * and LAN TC.
  4728. **/
  4729. static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
  4730. {
  4731. struct i40e_dcb_app_priority_table app;
  4732. struct i40e_hw *hw = &pf->hw;
  4733. u8 enabled_tc = 1; /* TC0 is always enabled */
  4734. u8 tc, i;
  4735. /* Get the iSCSI APP TLV */
  4736. struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
  4737. for (i = 0; i < dcbcfg->numapps; i++) {
  4738. app = dcbcfg->app[i];
  4739. if (app.selector == I40E_APP_SEL_TCPIP &&
  4740. app.protocolid == I40E_APP_PROTOID_ISCSI) {
  4741. tc = dcbcfg->etscfg.prioritytable[app.priority];
  4742. enabled_tc |= BIT(tc);
  4743. break;
  4744. }
  4745. }
  4746. return enabled_tc;
  4747. }
  4748. /**
  4749. * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
  4750. * @dcbcfg: the corresponding DCBx configuration structure
  4751. *
  4752. * Return the number of TCs from given DCBx configuration
  4753. **/
  4754. static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
  4755. {
  4756. int i, tc_unused = 0;
  4757. u8 num_tc = 0;
  4758. u8 ret = 0;
  4759. /* Scan the ETS Config Priority Table to find
  4760. * traffic class enabled for a given priority
  4761. * and create a bitmask of enabled TCs
  4762. */
  4763. for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
  4764. num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
  4765. /* Now scan the bitmask to check for
  4766. * contiguous TCs starting with TC0
  4767. */
  4768. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4769. if (num_tc & BIT(i)) {
  4770. if (!tc_unused) {
  4771. ret++;
  4772. } else {
  4773. pr_err("Non-contiguous TC - Disabling DCB\n");
  4774. return 1;
  4775. }
  4776. } else {
  4777. tc_unused = 1;
  4778. }
  4779. }
  4780. /* There is always at least TC0 */
  4781. if (!ret)
  4782. ret = 1;
  4783. return ret;
  4784. }
  4785. /**
  4786. * i40e_dcb_get_enabled_tc - Get enabled traffic classes
  4787. * @dcbcfg: the corresponding DCBx configuration structure
  4788. *
  4789. * Query the current DCB configuration and return the number of
  4790. * traffic classes enabled from the given DCBX config
  4791. **/
  4792. static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
  4793. {
  4794. u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
  4795. u8 enabled_tc = 1;
  4796. u8 i;
  4797. for (i = 0; i < num_tc; i++)
  4798. enabled_tc |= BIT(i);
  4799. return enabled_tc;
  4800. }
  4801. /**
  4802. * i40e_mqprio_get_enabled_tc - Get enabled traffic classes
  4803. * @pf: PF being queried
  4804. *
  4805. * Query the current MQPRIO configuration and return the number of
  4806. * traffic classes enabled.
  4807. **/
  4808. static u8 i40e_mqprio_get_enabled_tc(struct i40e_pf *pf)
  4809. {
  4810. struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
  4811. u8 num_tc = vsi->mqprio_qopt.qopt.num_tc;
  4812. u8 enabled_tc = 1, i;
  4813. for (i = 1; i < num_tc; i++)
  4814. enabled_tc |= BIT(i);
  4815. return enabled_tc;
  4816. }
  4817. /**
  4818. * i40e_pf_get_num_tc - Get enabled traffic classes for PF
  4819. * @pf: PF being queried
  4820. *
  4821. * Return number of traffic classes enabled for the given PF
  4822. **/
  4823. static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
  4824. {
  4825. u8 i, enabled_tc = 1;
  4826. u8 num_tc = 0;
  4827. if (i40e_is_tc_mqprio_enabled(pf)) {
  4828. struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
  4829. return vsi->mqprio_qopt.qopt.num_tc;
  4830. }
  4831. /* If neither MQPRIO nor DCB is enabled, then always use single TC */
  4832. if (!test_bit(I40E_FLAG_DCB_ENA, pf->flags))
  4833. return 1;
  4834. /* SFP mode will be enabled for all TCs on port */
  4835. if (!test_bit(I40E_FLAG_MFP_ENA, pf->flags))
  4836. return i40e_dcb_get_num_tc(&pf->hw.local_dcbx_config);
  4837. /* MFP mode return count of enabled TCs for this PF */
  4838. if (pf->hw.func_caps.iscsi)
  4839. enabled_tc = i40e_get_iscsi_tc_map(pf);
  4840. else
  4841. return 1; /* Only TC0 */
  4842. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4843. if (enabled_tc & BIT(i))
  4844. num_tc++;
  4845. }
  4846. return num_tc;
  4847. }
  4848. /**
  4849. * i40e_pf_get_tc_map - Get bitmap for enabled traffic classes
  4850. * @pf: PF being queried
  4851. *
  4852. * Return a bitmap for enabled traffic classes for this PF.
  4853. **/
  4854. static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
  4855. {
  4856. if (i40e_is_tc_mqprio_enabled(pf))
  4857. return i40e_mqprio_get_enabled_tc(pf);
  4858. /* If neither MQPRIO nor DCB is enabled for this PF then just return
  4859. * default TC
  4860. */
  4861. if (!test_bit(I40E_FLAG_DCB_ENA, pf->flags))
  4862. return I40E_DEFAULT_TRAFFIC_CLASS;
  4863. /* SFP mode we want PF to be enabled for all TCs */
  4864. if (!test_bit(I40E_FLAG_MFP_ENA, pf->flags))
  4865. return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
  4866. /* MFP enabled and iSCSI PF type */
  4867. if (pf->hw.func_caps.iscsi)
  4868. return i40e_get_iscsi_tc_map(pf);
  4869. else
  4870. return I40E_DEFAULT_TRAFFIC_CLASS;
  4871. }
  4872. /**
  4873. * i40e_vsi_get_bw_info - Query VSI BW Information
  4874. * @vsi: the VSI being queried
  4875. *
  4876. * Returns 0 on success, negative value on failure
  4877. **/
  4878. static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
  4879. {
  4880. struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
  4881. struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
  4882. struct i40e_pf *pf = vsi->back;
  4883. struct i40e_hw *hw = &pf->hw;
  4884. u32 tc_bw_max;
  4885. int ret;
  4886. int i;
  4887. /* Get the VSI level BW configuration */
  4888. ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
  4889. if (ret) {
  4890. dev_info(&pf->pdev->dev,
  4891. "couldn't get PF vsi bw config, err %pe aq_err %s\n",
  4892. ERR_PTR(ret),
  4893. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4894. return -EINVAL;
  4895. }
  4896. /* Get the VSI level BW configuration per TC */
  4897. ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
  4898. NULL);
  4899. if (ret) {
  4900. dev_info(&pf->pdev->dev,
  4901. "couldn't get PF vsi ets bw config, err %pe aq_err %s\n",
  4902. ERR_PTR(ret),
  4903. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4904. return -EINVAL;
  4905. }
  4906. if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
  4907. dev_info(&pf->pdev->dev,
  4908. "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
  4909. bw_config.tc_valid_bits,
  4910. bw_ets_config.tc_valid_bits);
  4911. /* Still continuing */
  4912. }
  4913. vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
  4914. vsi->bw_max_quanta = bw_config.max_bw;
  4915. tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
  4916. (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
  4917. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4918. vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
  4919. vsi->bw_ets_limit_credits[i] =
  4920. le16_to_cpu(bw_ets_config.credits[i]);
  4921. /* 3 bits out of 4 for each TC */
  4922. vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
  4923. }
  4924. return 0;
  4925. }
  4926. /**
  4927. * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
  4928. * @vsi: the VSI being configured
  4929. * @enabled_tc: TC bitmap
  4930. * @bw_share: BW shared credits per TC
  4931. *
  4932. * Returns 0 on success, negative value on failure
  4933. **/
  4934. static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
  4935. u8 *bw_share)
  4936. {
  4937. struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
  4938. struct i40e_pf *pf = vsi->back;
  4939. int ret;
  4940. int i;
  4941. /* There is no need to reset BW when mqprio mode is on. */
  4942. if (i40e_is_tc_mqprio_enabled(pf))
  4943. return 0;
  4944. if (!vsi->mqprio_qopt.qopt.hw && !test_bit(I40E_FLAG_DCB_ENA, pf->flags)) {
  4945. ret = i40e_set_bw_limit(vsi, vsi->seid, 0);
  4946. if (ret)
  4947. dev_info(&pf->pdev->dev,
  4948. "Failed to reset tx rate for vsi->seid %u\n",
  4949. vsi->seid);
  4950. return ret;
  4951. }
  4952. memset(&bw_data, 0, sizeof(bw_data));
  4953. bw_data.tc_valid_bits = enabled_tc;
  4954. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
  4955. bw_data.tc_bw_credits[i] = bw_share[i];
  4956. ret = i40e_aq_config_vsi_tc_bw(&pf->hw, vsi->seid, &bw_data, NULL);
  4957. if (ret) {
  4958. dev_info(&pf->pdev->dev,
  4959. "AQ command Config VSI BW allocation per TC failed = %d\n",
  4960. pf->hw.aq.asq_last_status);
  4961. return -EINVAL;
  4962. }
  4963. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
  4964. vsi->info.qs_handle[i] = bw_data.qs_handles[i];
  4965. return 0;
  4966. }
  4967. /**
  4968. * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
  4969. * @vsi: the VSI being configured
  4970. * @enabled_tc: TC map to be enabled
  4971. *
  4972. **/
  4973. static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
  4974. {
  4975. struct net_device *netdev = vsi->netdev;
  4976. struct i40e_pf *pf = vsi->back;
  4977. struct i40e_hw *hw = &pf->hw;
  4978. u8 netdev_tc = 0;
  4979. int i;
  4980. struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
  4981. if (!netdev)
  4982. return;
  4983. if (!enabled_tc) {
  4984. netdev_reset_tc(netdev);
  4985. return;
  4986. }
  4987. /* Set up actual enabled TCs on the VSI */
  4988. if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
  4989. return;
  4990. /* set per TC queues for the VSI */
  4991. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4992. /* Only set TC queues for enabled tcs
  4993. *
  4994. * e.g. For a VSI that has TC0 and TC3 enabled the
  4995. * enabled_tc bitmap would be 0x00001001; the driver
  4996. * will set the numtc for netdev as 2 that will be
  4997. * referenced by the netdev layer as TC 0 and 1.
  4998. */
  4999. if (vsi->tc_config.enabled_tc & BIT(i))
  5000. netdev_set_tc_queue(netdev,
  5001. vsi->tc_config.tc_info[i].netdev_tc,
  5002. vsi->tc_config.tc_info[i].qcount,
  5003. vsi->tc_config.tc_info[i].qoffset);
  5004. }
  5005. if (i40e_is_tc_mqprio_enabled(pf))
  5006. return;
  5007. /* Assign UP2TC map for the VSI */
  5008. for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
  5009. /* Get the actual TC# for the UP */
  5010. u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
  5011. /* Get the mapped netdev TC# for the UP */
  5012. netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
  5013. netdev_set_prio_tc_map(netdev, i, netdev_tc);
  5014. }
  5015. }
  5016. /**
  5017. * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
  5018. * @vsi: the VSI being configured
  5019. * @ctxt: the ctxt buffer returned from AQ VSI update param command
  5020. **/
  5021. static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
  5022. struct i40e_vsi_context *ctxt)
  5023. {
  5024. /* copy just the sections touched not the entire info
  5025. * since not all sections are valid as returned by
  5026. * update vsi params
  5027. */
  5028. vsi->info.mapping_flags = ctxt->info.mapping_flags;
  5029. memcpy(&vsi->info.queue_mapping,
  5030. &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
  5031. memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
  5032. sizeof(vsi->info.tc_mapping));
  5033. }
  5034. /**
  5035. * i40e_update_adq_vsi_queues - update queue mapping for ADq VSI
  5036. * @vsi: the VSI being reconfigured
  5037. * @vsi_offset: offset from main VF VSI
  5038. */
  5039. int i40e_update_adq_vsi_queues(struct i40e_vsi *vsi, int vsi_offset)
  5040. {
  5041. struct i40e_vsi_context ctxt = {};
  5042. struct i40e_pf *pf;
  5043. struct i40e_hw *hw;
  5044. int ret;
  5045. if (!vsi)
  5046. return -EINVAL;
  5047. pf = vsi->back;
  5048. hw = &pf->hw;
  5049. ctxt.seid = vsi->seid;
  5050. ctxt.pf_num = hw->pf_id;
  5051. ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id + vsi_offset;
  5052. ctxt.uplink_seid = vsi->uplink_seid;
  5053. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  5054. ctxt.flags = I40E_AQ_VSI_TYPE_VF;
  5055. ctxt.info = vsi->info;
  5056. i40e_vsi_setup_queue_map(vsi, &ctxt, vsi->tc_config.enabled_tc,
  5057. false);
  5058. if (vsi->reconfig_rss) {
  5059. vsi->rss_size = min_t(int, pf->alloc_rss_size,
  5060. vsi->num_queue_pairs);
  5061. ret = i40e_vsi_config_rss(vsi);
  5062. if (ret) {
  5063. dev_info(&pf->pdev->dev, "Failed to reconfig rss for num_queues\n");
  5064. return ret;
  5065. }
  5066. vsi->reconfig_rss = false;
  5067. }
  5068. ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
  5069. if (ret) {
  5070. dev_info(&pf->pdev->dev, "Update vsi config failed, err %pe aq_err %s\n",
  5071. ERR_PTR(ret),
  5072. i40e_aq_str(hw, hw->aq.asq_last_status));
  5073. return ret;
  5074. }
  5075. /* update the local VSI info with updated queue map */
  5076. i40e_vsi_update_queue_map(vsi, &ctxt);
  5077. vsi->info.valid_sections = 0;
  5078. return ret;
  5079. }
  5080. /**
  5081. * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
  5082. * @vsi: VSI to be configured
  5083. * @enabled_tc: TC bitmap
  5084. *
  5085. * This configures a particular VSI for TCs that are mapped to the
  5086. * given TC bitmap. It uses default bandwidth share for TCs across
  5087. * VSIs to configure TC for a particular VSI.
  5088. *
  5089. * NOTE:
  5090. * It is expected that the VSI queues have been quisced before calling
  5091. * this function.
  5092. **/
  5093. static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
  5094. {
  5095. u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
  5096. struct i40e_pf *pf = vsi->back;
  5097. struct i40e_hw *hw = &pf->hw;
  5098. struct i40e_vsi_context ctxt;
  5099. int ret = 0;
  5100. int i;
  5101. /* Check if enabled_tc is same as existing or new TCs */
  5102. if (vsi->tc_config.enabled_tc == enabled_tc &&
  5103. vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
  5104. return ret;
  5105. /* Enable ETS TCs with equal BW Share for now across all VSIs */
  5106. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  5107. if (enabled_tc & BIT(i))
  5108. bw_share[i] = 1;
  5109. }
  5110. ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
  5111. if (ret) {
  5112. struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
  5113. dev_info(&pf->pdev->dev,
  5114. "Failed configuring TC map %d for VSI %d\n",
  5115. enabled_tc, vsi->seid);
  5116. ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid,
  5117. &bw_config, NULL);
  5118. if (ret) {
  5119. dev_info(&pf->pdev->dev,
  5120. "Failed querying vsi bw info, err %pe aq_err %s\n",
  5121. ERR_PTR(ret),
  5122. i40e_aq_str(hw, hw->aq.asq_last_status));
  5123. goto out;
  5124. }
  5125. if ((bw_config.tc_valid_bits & enabled_tc) != enabled_tc) {
  5126. u8 valid_tc = bw_config.tc_valid_bits & enabled_tc;
  5127. if (!valid_tc)
  5128. valid_tc = bw_config.tc_valid_bits;
  5129. /* Always enable TC0, no matter what */
  5130. valid_tc |= 1;
  5131. dev_info(&pf->pdev->dev,
  5132. "Requested tc 0x%x, but FW reports 0x%x as valid. Attempting to use 0x%x.\n",
  5133. enabled_tc, bw_config.tc_valid_bits, valid_tc);
  5134. enabled_tc = valid_tc;
  5135. }
  5136. ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
  5137. if (ret) {
  5138. dev_err(&pf->pdev->dev,
  5139. "Unable to configure TC map %d for VSI %d\n",
  5140. enabled_tc, vsi->seid);
  5141. goto out;
  5142. }
  5143. }
  5144. /* Update Queue Pairs Mapping for currently enabled UPs */
  5145. ctxt.seid = vsi->seid;
  5146. ctxt.pf_num = vsi->back->hw.pf_id;
  5147. ctxt.vf_num = 0;
  5148. ctxt.uplink_seid = vsi->uplink_seid;
  5149. ctxt.info = vsi->info;
  5150. if (i40e_is_tc_mqprio_enabled(pf)) {
  5151. ret = i40e_vsi_setup_queue_map_mqprio(vsi, &ctxt, enabled_tc);
  5152. if (ret)
  5153. goto out;
  5154. } else {
  5155. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
  5156. }
  5157. /* On destroying the qdisc, reset vsi->rss_size, as number of enabled
  5158. * queues changed.
  5159. */
  5160. if (!vsi->mqprio_qopt.qopt.hw && vsi->reconfig_rss) {
  5161. vsi->rss_size = min_t(int, vsi->back->alloc_rss_size,
  5162. vsi->num_queue_pairs);
  5163. ret = i40e_vsi_config_rss(vsi);
  5164. if (ret) {
  5165. dev_info(&vsi->back->pdev->dev,
  5166. "Failed to reconfig rss for num_queues\n");
  5167. return ret;
  5168. }
  5169. vsi->reconfig_rss = false;
  5170. }
  5171. if (test_bit(I40E_FLAG_IWARP_ENA, vsi->back->flags)) {
  5172. ctxt.info.valid_sections |=
  5173. cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
  5174. ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
  5175. }
  5176. /* Update the VSI after updating the VSI queue-mapping
  5177. * information
  5178. */
  5179. ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
  5180. if (ret) {
  5181. dev_info(&pf->pdev->dev,
  5182. "Update vsi tc config failed, err %pe aq_err %s\n",
  5183. ERR_PTR(ret),
  5184. i40e_aq_str(hw, hw->aq.asq_last_status));
  5185. goto out;
  5186. }
  5187. /* update the local VSI info with updated queue map */
  5188. i40e_vsi_update_queue_map(vsi, &ctxt);
  5189. vsi->info.valid_sections = 0;
  5190. /* Update current VSI BW information */
  5191. ret = i40e_vsi_get_bw_info(vsi);
  5192. if (ret) {
  5193. dev_info(&pf->pdev->dev,
  5194. "Failed updating vsi bw info, err %pe aq_err %s\n",
  5195. ERR_PTR(ret),
  5196. i40e_aq_str(hw, hw->aq.asq_last_status));
  5197. goto out;
  5198. }
  5199. /* Update the netdev TC setup */
  5200. i40e_vsi_config_netdev_tc(vsi, enabled_tc);
  5201. out:
  5202. return ret;
  5203. }
  5204. /**
  5205. * i40e_vsi_reconfig_tc - Reconfigure VSI Tx Scheduler for stored TC map
  5206. * @vsi: VSI to be reconfigured
  5207. *
  5208. * This reconfigures a particular VSI for TCs that are mapped to the
  5209. * TC bitmap stored previously for the VSI.
  5210. *
  5211. * Context: It is expected that the VSI queues have been quisced before
  5212. * calling this function.
  5213. *
  5214. * Return: 0 on success, negative value on failure
  5215. **/
  5216. static int i40e_vsi_reconfig_tc(struct i40e_vsi *vsi)
  5217. {
  5218. u8 enabled_tc;
  5219. enabled_tc = vsi->tc_config.enabled_tc;
  5220. vsi->tc_config.enabled_tc = 0;
  5221. return i40e_vsi_config_tc(vsi, enabled_tc);
  5222. }
  5223. /**
  5224. * i40e_get_link_speed - Returns link speed for the interface
  5225. * @vsi: VSI to be configured
  5226. *
  5227. **/
  5228. static int i40e_get_link_speed(struct i40e_vsi *vsi)
  5229. {
  5230. struct i40e_pf *pf = vsi->back;
  5231. switch (pf->hw.phy.link_info.link_speed) {
  5232. case I40E_LINK_SPEED_40GB:
  5233. return 40000;
  5234. case I40E_LINK_SPEED_25GB:
  5235. return 25000;
  5236. case I40E_LINK_SPEED_20GB:
  5237. return 20000;
  5238. case I40E_LINK_SPEED_10GB:
  5239. return 10000;
  5240. case I40E_LINK_SPEED_1GB:
  5241. return 1000;
  5242. default:
  5243. return -EINVAL;
  5244. }
  5245. }
  5246. /**
  5247. * i40e_bw_bytes_to_mbits - Convert max_tx_rate from bytes to mbits
  5248. * @vsi: Pointer to vsi structure
  5249. * @max_tx_rate: max TX rate in bytes to be converted into Mbits
  5250. *
  5251. * Helper function to convert units before send to set BW limit
  5252. **/
  5253. static u64 i40e_bw_bytes_to_mbits(struct i40e_vsi *vsi, u64 max_tx_rate)
  5254. {
  5255. if (max_tx_rate < I40E_BW_MBPS_DIVISOR) {
  5256. dev_warn(&vsi->back->pdev->dev,
  5257. "Setting max tx rate to minimum usable value of 50Mbps.\n");
  5258. max_tx_rate = I40E_BW_CREDIT_DIVISOR;
  5259. } else {
  5260. do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
  5261. }
  5262. return max_tx_rate;
  5263. }
  5264. /**
  5265. * i40e_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
  5266. * @vsi: VSI to be configured
  5267. * @seid: seid of the channel/VSI
  5268. * @max_tx_rate: max TX rate to be configured as BW limit
  5269. *
  5270. * Helper function to set BW limit for a given VSI
  5271. **/
  5272. int i40e_set_bw_limit(struct i40e_vsi *vsi, u16 seid, u64 max_tx_rate)
  5273. {
  5274. struct i40e_pf *pf = vsi->back;
  5275. u64 credits = 0;
  5276. int speed = 0;
  5277. int ret = 0;
  5278. speed = i40e_get_link_speed(vsi);
  5279. if (max_tx_rate > speed) {
  5280. dev_err(&pf->pdev->dev,
  5281. "Invalid max tx rate %llu specified for VSI seid %d.",
  5282. max_tx_rate, seid);
  5283. return -EINVAL;
  5284. }
  5285. if (max_tx_rate && max_tx_rate < I40E_BW_CREDIT_DIVISOR) {
  5286. dev_warn(&pf->pdev->dev,
  5287. "Setting max tx rate to minimum usable value of 50Mbps.\n");
  5288. max_tx_rate = I40E_BW_CREDIT_DIVISOR;
  5289. }
  5290. /* Tx rate credits are in values of 50Mbps, 0 is disabled */
  5291. credits = max_tx_rate;
  5292. do_div(credits, I40E_BW_CREDIT_DIVISOR);
  5293. ret = i40e_aq_config_vsi_bw_limit(&pf->hw, seid, credits,
  5294. I40E_MAX_BW_INACTIVE_ACCUM, NULL);
  5295. if (ret)
  5296. dev_err(&pf->pdev->dev,
  5297. "Failed set tx rate (%llu Mbps) for vsi->seid %u, err %pe aq_err %s\n",
  5298. max_tx_rate, seid, ERR_PTR(ret),
  5299. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5300. return ret;
  5301. }
  5302. /**
  5303. * i40e_remove_queue_channels - Remove queue channels for the TCs
  5304. * @vsi: VSI to be configured
  5305. *
  5306. * Remove queue channels for the TCs
  5307. **/
  5308. static void i40e_remove_queue_channels(struct i40e_vsi *vsi)
  5309. {
  5310. enum i40e_admin_queue_err last_aq_status;
  5311. struct i40e_cloud_filter *cfilter;
  5312. struct i40e_channel *ch, *ch_tmp;
  5313. struct i40e_pf *pf = vsi->back;
  5314. struct hlist_node *node;
  5315. int ret, i;
  5316. /* Reset rss size that was stored when reconfiguring rss for
  5317. * channel VSIs with non-power-of-2 queue count.
  5318. */
  5319. vsi->current_rss_size = 0;
  5320. /* perform cleanup for channels if they exist */
  5321. if (list_empty(&vsi->ch_list))
  5322. return;
  5323. list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
  5324. struct i40e_vsi *p_vsi;
  5325. list_del(&ch->list);
  5326. p_vsi = ch->parent_vsi;
  5327. if (!p_vsi || !ch->initialized) {
  5328. kfree(ch);
  5329. continue;
  5330. }
  5331. /* Reset queue contexts */
  5332. for (i = 0; i < ch->num_queue_pairs; i++) {
  5333. struct i40e_ring *tx_ring, *rx_ring;
  5334. u16 pf_q;
  5335. pf_q = ch->base_queue + i;
  5336. tx_ring = vsi->tx_rings[pf_q];
  5337. tx_ring->ch = NULL;
  5338. rx_ring = vsi->rx_rings[pf_q];
  5339. rx_ring->ch = NULL;
  5340. }
  5341. /* Reset BW configured for this VSI via mqprio */
  5342. ret = i40e_set_bw_limit(vsi, ch->seid, 0);
  5343. if (ret)
  5344. dev_info(&vsi->back->pdev->dev,
  5345. "Failed to reset tx rate for ch->seid %u\n",
  5346. ch->seid);
  5347. /* delete cloud filters associated with this channel */
  5348. hlist_for_each_entry_safe(cfilter, node,
  5349. &pf->cloud_filter_list, cloud_node) {
  5350. if (cfilter->seid != ch->seid)
  5351. continue;
  5352. hash_del(&cfilter->cloud_node);
  5353. if (cfilter->dst_port)
  5354. ret = i40e_add_del_cloud_filter_big_buf(vsi,
  5355. cfilter,
  5356. false);
  5357. else
  5358. ret = i40e_add_del_cloud_filter(vsi, cfilter,
  5359. false);
  5360. last_aq_status = pf->hw.aq.asq_last_status;
  5361. if (ret)
  5362. dev_info(&pf->pdev->dev,
  5363. "Failed to delete cloud filter, err %pe aq_err %s\n",
  5364. ERR_PTR(ret),
  5365. i40e_aq_str(&pf->hw, last_aq_status));
  5366. kfree(cfilter);
  5367. }
  5368. /* delete VSI from FW */
  5369. ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
  5370. NULL);
  5371. if (ret)
  5372. dev_err(&vsi->back->pdev->dev,
  5373. "unable to remove channel (%d) for parent VSI(%d)\n",
  5374. ch->seid, p_vsi->seid);
  5375. kfree(ch);
  5376. }
  5377. INIT_LIST_HEAD(&vsi->ch_list);
  5378. }
  5379. /**
  5380. * i40e_get_max_queues_for_channel
  5381. * @vsi: ptr to VSI to which channels are associated with
  5382. *
  5383. * Helper function which returns max value among the queue counts set on the
  5384. * channels/TCs created.
  5385. **/
  5386. static int i40e_get_max_queues_for_channel(struct i40e_vsi *vsi)
  5387. {
  5388. struct i40e_channel *ch, *ch_tmp;
  5389. int max = 0;
  5390. list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
  5391. if (!ch->initialized)
  5392. continue;
  5393. if (ch->num_queue_pairs > max)
  5394. max = ch->num_queue_pairs;
  5395. }
  5396. return max;
  5397. }
  5398. /**
  5399. * i40e_validate_num_queues - validate num_queues w.r.t channel
  5400. * @pf: ptr to PF device
  5401. * @num_queues: number of queues
  5402. * @vsi: the parent VSI
  5403. * @reconfig_rss: indicates should the RSS be reconfigured or not
  5404. *
  5405. * This function validates number of queues in the context of new channel
  5406. * which is being established and determines if RSS should be reconfigured
  5407. * or not for parent VSI.
  5408. **/
  5409. static int i40e_validate_num_queues(struct i40e_pf *pf, int num_queues,
  5410. struct i40e_vsi *vsi, bool *reconfig_rss)
  5411. {
  5412. int max_ch_queues;
  5413. if (!reconfig_rss)
  5414. return -EINVAL;
  5415. *reconfig_rss = false;
  5416. if (vsi->current_rss_size) {
  5417. if (num_queues > vsi->current_rss_size) {
  5418. dev_dbg(&pf->pdev->dev,
  5419. "Error: num_queues (%d) > vsi's current_size(%d)\n",
  5420. num_queues, vsi->current_rss_size);
  5421. return -EINVAL;
  5422. } else if ((num_queues < vsi->current_rss_size) &&
  5423. (!is_power_of_2(num_queues))) {
  5424. dev_dbg(&pf->pdev->dev,
  5425. "Error: num_queues (%d) < vsi's current_size(%d), but not power of 2\n",
  5426. num_queues, vsi->current_rss_size);
  5427. return -EINVAL;
  5428. }
  5429. }
  5430. if (!is_power_of_2(num_queues)) {
  5431. /* Find the max num_queues configured for channel if channel
  5432. * exist.
  5433. * if channel exist, then enforce 'num_queues' to be more than
  5434. * max ever queues configured for channel.
  5435. */
  5436. max_ch_queues = i40e_get_max_queues_for_channel(vsi);
  5437. if (num_queues < max_ch_queues) {
  5438. dev_dbg(&pf->pdev->dev,
  5439. "Error: num_queues (%d) < max queues configured for channel(%d)\n",
  5440. num_queues, max_ch_queues);
  5441. return -EINVAL;
  5442. }
  5443. *reconfig_rss = true;
  5444. }
  5445. return 0;
  5446. }
  5447. /**
  5448. * i40e_vsi_reconfig_rss - reconfig RSS based on specified rss_size
  5449. * @vsi: the VSI being setup
  5450. * @rss_size: size of RSS, accordingly LUT gets reprogrammed
  5451. *
  5452. * This function reconfigures RSS by reprogramming LUTs using 'rss_size'
  5453. **/
  5454. static int i40e_vsi_reconfig_rss(struct i40e_vsi *vsi, u16 rss_size)
  5455. {
  5456. struct i40e_pf *pf = vsi->back;
  5457. u8 seed[I40E_HKEY_ARRAY_SIZE];
  5458. struct i40e_hw *hw = &pf->hw;
  5459. int local_rss_size;
  5460. u8 *lut;
  5461. int ret;
  5462. if (!vsi->rss_size)
  5463. return -EINVAL;
  5464. if (rss_size > vsi->rss_size)
  5465. return -EINVAL;
  5466. local_rss_size = min_t(int, vsi->rss_size, rss_size);
  5467. lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
  5468. if (!lut)
  5469. return -ENOMEM;
  5470. /* Ignoring user configured lut if there is one */
  5471. i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, local_rss_size);
  5472. /* Use user configured hash key if there is one, otherwise
  5473. * use default.
  5474. */
  5475. if (vsi->rss_hkey_user)
  5476. memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
  5477. else
  5478. netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
  5479. ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
  5480. if (ret) {
  5481. dev_info(&pf->pdev->dev,
  5482. "Cannot set RSS lut, err %pe aq_err %s\n",
  5483. ERR_PTR(ret),
  5484. i40e_aq_str(hw, hw->aq.asq_last_status));
  5485. kfree(lut);
  5486. return ret;
  5487. }
  5488. kfree(lut);
  5489. /* Do the update w.r.t. storing rss_size */
  5490. if (!vsi->orig_rss_size)
  5491. vsi->orig_rss_size = vsi->rss_size;
  5492. vsi->current_rss_size = local_rss_size;
  5493. return ret;
  5494. }
  5495. /**
  5496. * i40e_channel_setup_queue_map - Setup a channel queue map
  5497. * @pf: ptr to PF device
  5498. * @ctxt: VSI context structure
  5499. * @ch: ptr to channel structure
  5500. *
  5501. * Setup queue map for a specific channel
  5502. **/
  5503. static void i40e_channel_setup_queue_map(struct i40e_pf *pf,
  5504. struct i40e_vsi_context *ctxt,
  5505. struct i40e_channel *ch)
  5506. {
  5507. u16 qcount, qmap, sections = 0;
  5508. u8 offset = 0;
  5509. int pow;
  5510. sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
  5511. sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
  5512. qcount = min_t(int, ch->num_queue_pairs, pf->num_lan_msix);
  5513. ch->num_queue_pairs = qcount;
  5514. /* find the next higher power-of-2 of num queue pairs */
  5515. pow = ilog2(qcount);
  5516. if (!is_power_of_2(qcount))
  5517. pow++;
  5518. qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
  5519. (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
  5520. /* Setup queue TC[0].qmap for given VSI context */
  5521. ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
  5522. ctxt->info.up_enable_bits = 0x1; /* TC0 enabled */
  5523. ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
  5524. ctxt->info.queue_mapping[0] = cpu_to_le16(ch->base_queue);
  5525. ctxt->info.valid_sections |= cpu_to_le16(sections);
  5526. }
  5527. /**
  5528. * i40e_add_channel - add a channel by adding VSI
  5529. * @pf: ptr to PF device
  5530. * @uplink_seid: underlying HW switching element (VEB) ID
  5531. * @ch: ptr to channel structure
  5532. *
  5533. * Add a channel (VSI) using add_vsi and queue_map
  5534. **/
  5535. static int i40e_add_channel(struct i40e_pf *pf, u16 uplink_seid,
  5536. struct i40e_channel *ch)
  5537. {
  5538. struct i40e_hw *hw = &pf->hw;
  5539. struct i40e_vsi_context ctxt;
  5540. u8 enabled_tc = 0x1; /* TC0 enabled */
  5541. int ret;
  5542. if (ch->type != I40E_VSI_VMDQ2) {
  5543. dev_info(&pf->pdev->dev,
  5544. "add new vsi failed, ch->type %d\n", ch->type);
  5545. return -EINVAL;
  5546. }
  5547. memset(&ctxt, 0, sizeof(ctxt));
  5548. ctxt.pf_num = hw->pf_id;
  5549. ctxt.vf_num = 0;
  5550. ctxt.uplink_seid = uplink_seid;
  5551. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  5552. if (ch->type == I40E_VSI_VMDQ2)
  5553. ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
  5554. if (test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags)) {
  5555. ctxt.info.valid_sections |=
  5556. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  5557. ctxt.info.switch_id =
  5558. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  5559. }
  5560. /* Set queue map for a given VSI context */
  5561. i40e_channel_setup_queue_map(pf, &ctxt, ch);
  5562. /* Now time to create VSI */
  5563. ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
  5564. if (ret) {
  5565. dev_info(&pf->pdev->dev,
  5566. "add new vsi failed, err %pe aq_err %s\n",
  5567. ERR_PTR(ret),
  5568. i40e_aq_str(&pf->hw,
  5569. pf->hw.aq.asq_last_status));
  5570. return -ENOENT;
  5571. }
  5572. /* Success, update channel, set enabled_tc only if the channel
  5573. * is not a macvlan
  5574. */
  5575. ch->enabled_tc = !i40e_is_channel_macvlan(ch) && enabled_tc;
  5576. ch->seid = ctxt.seid;
  5577. ch->vsi_number = ctxt.vsi_number;
  5578. ch->stat_counter_idx = le16_to_cpu(ctxt.info.stat_counter_idx);
  5579. /* copy just the sections touched not the entire info
  5580. * since not all sections are valid as returned by
  5581. * update vsi params
  5582. */
  5583. ch->info.mapping_flags = ctxt.info.mapping_flags;
  5584. memcpy(&ch->info.queue_mapping,
  5585. &ctxt.info.queue_mapping, sizeof(ctxt.info.queue_mapping));
  5586. memcpy(&ch->info.tc_mapping, ctxt.info.tc_mapping,
  5587. sizeof(ctxt.info.tc_mapping));
  5588. return 0;
  5589. }
  5590. static int i40e_channel_config_bw(struct i40e_vsi *vsi, struct i40e_channel *ch,
  5591. u8 *bw_share)
  5592. {
  5593. struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
  5594. int ret;
  5595. int i;
  5596. memset(&bw_data, 0, sizeof(bw_data));
  5597. bw_data.tc_valid_bits = ch->enabled_tc;
  5598. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
  5599. bw_data.tc_bw_credits[i] = bw_share[i];
  5600. ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, ch->seid,
  5601. &bw_data, NULL);
  5602. if (ret) {
  5603. dev_info(&vsi->back->pdev->dev,
  5604. "Config VSI BW allocation per TC failed, aq_err: %d for new_vsi->seid %u\n",
  5605. vsi->back->hw.aq.asq_last_status, ch->seid);
  5606. return -EINVAL;
  5607. }
  5608. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
  5609. ch->info.qs_handle[i] = bw_data.qs_handles[i];
  5610. return 0;
  5611. }
  5612. /**
  5613. * i40e_channel_config_tx_ring - config TX ring associated with new channel
  5614. * @pf: ptr to PF device
  5615. * @vsi: the VSI being setup
  5616. * @ch: ptr to channel structure
  5617. *
  5618. * Configure TX rings associated with channel (VSI) since queues are being
  5619. * from parent VSI.
  5620. **/
  5621. static int i40e_channel_config_tx_ring(struct i40e_pf *pf,
  5622. struct i40e_vsi *vsi,
  5623. struct i40e_channel *ch)
  5624. {
  5625. u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
  5626. int ret;
  5627. int i;
  5628. /* Enable ETS TCs with equal BW Share for now across all VSIs */
  5629. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  5630. if (ch->enabled_tc & BIT(i))
  5631. bw_share[i] = 1;
  5632. }
  5633. /* configure BW for new VSI */
  5634. ret = i40e_channel_config_bw(vsi, ch, bw_share);
  5635. if (ret) {
  5636. dev_info(&vsi->back->pdev->dev,
  5637. "Failed configuring TC map %d for channel (seid %u)\n",
  5638. ch->enabled_tc, ch->seid);
  5639. return ret;
  5640. }
  5641. for (i = 0; i < ch->num_queue_pairs; i++) {
  5642. struct i40e_ring *tx_ring, *rx_ring;
  5643. u16 pf_q;
  5644. pf_q = ch->base_queue + i;
  5645. /* Get to TX ring ptr of main VSI, for re-setup TX queue
  5646. * context
  5647. */
  5648. tx_ring = vsi->tx_rings[pf_q];
  5649. tx_ring->ch = ch;
  5650. /* Get the RX ring ptr */
  5651. rx_ring = vsi->rx_rings[pf_q];
  5652. rx_ring->ch = ch;
  5653. }
  5654. return 0;
  5655. }
  5656. /**
  5657. * i40e_setup_hw_channel - setup new channel
  5658. * @pf: ptr to PF device
  5659. * @vsi: the VSI being setup
  5660. * @ch: ptr to channel structure
  5661. * @uplink_seid: underlying HW switching element (VEB) ID
  5662. * @type: type of channel to be created (VMDq2/VF)
  5663. *
  5664. * Setup new channel (VSI) based on specified type (VMDq2/VF)
  5665. * and configures TX rings accordingly
  5666. **/
  5667. static inline int i40e_setup_hw_channel(struct i40e_pf *pf,
  5668. struct i40e_vsi *vsi,
  5669. struct i40e_channel *ch,
  5670. u16 uplink_seid, u8 type)
  5671. {
  5672. int ret;
  5673. ch->initialized = false;
  5674. ch->base_queue = vsi->next_base_queue;
  5675. ch->type = type;
  5676. /* Proceed with creation of channel (VMDq2) VSI */
  5677. ret = i40e_add_channel(pf, uplink_seid, ch);
  5678. if (ret) {
  5679. dev_info(&pf->pdev->dev,
  5680. "failed to add_channel using uplink_seid %u\n",
  5681. uplink_seid);
  5682. return ret;
  5683. }
  5684. /* Mark the successful creation of channel */
  5685. ch->initialized = true;
  5686. /* Reconfigure TX queues using QTX_CTL register */
  5687. ret = i40e_channel_config_tx_ring(pf, vsi, ch);
  5688. if (ret) {
  5689. dev_info(&pf->pdev->dev,
  5690. "failed to configure TX rings for channel %u\n",
  5691. ch->seid);
  5692. return ret;
  5693. }
  5694. /* update 'next_base_queue' */
  5695. vsi->next_base_queue = vsi->next_base_queue + ch->num_queue_pairs;
  5696. dev_dbg(&pf->pdev->dev,
  5697. "Added channel: vsi_seid %u, vsi_number %u, stat_counter_idx %u, num_queue_pairs %u, pf->next_base_queue %d\n",
  5698. ch->seid, ch->vsi_number, ch->stat_counter_idx,
  5699. ch->num_queue_pairs,
  5700. vsi->next_base_queue);
  5701. return ret;
  5702. }
  5703. /**
  5704. * i40e_setup_channel - setup new channel using uplink element
  5705. * @pf: ptr to PF device
  5706. * @vsi: pointer to the VSI to set up the channel within
  5707. * @ch: ptr to channel structure
  5708. *
  5709. * Setup new channel (VSI) based on specified type (VMDq2/VF)
  5710. * and uplink switching element (uplink_seid)
  5711. **/
  5712. static bool i40e_setup_channel(struct i40e_pf *pf, struct i40e_vsi *vsi,
  5713. struct i40e_channel *ch)
  5714. {
  5715. struct i40e_vsi *main_vsi;
  5716. u8 vsi_type;
  5717. u16 seid;
  5718. int ret;
  5719. if (vsi->type == I40E_VSI_MAIN) {
  5720. vsi_type = I40E_VSI_VMDQ2;
  5721. } else {
  5722. dev_err(&pf->pdev->dev, "unsupported parent vsi type(%d)\n",
  5723. vsi->type);
  5724. return false;
  5725. }
  5726. /* underlying switching element */
  5727. main_vsi = i40e_pf_get_main_vsi(pf);
  5728. seid = main_vsi->uplink_seid;
  5729. /* create channel (VSI), configure TX rings */
  5730. ret = i40e_setup_hw_channel(pf, vsi, ch, seid, vsi_type);
  5731. if (ret) {
  5732. dev_err(&pf->pdev->dev, "failed to setup hw_channel\n");
  5733. return false;
  5734. }
  5735. return ch->initialized ? true : false;
  5736. }
  5737. /**
  5738. * i40e_validate_and_set_switch_mode - sets up switch mode correctly
  5739. * @vsi: ptr to VSI which has PF backing
  5740. *
  5741. * Sets up switch mode correctly if it needs to be changed and perform
  5742. * what are allowed modes.
  5743. **/
  5744. static int i40e_validate_and_set_switch_mode(struct i40e_vsi *vsi)
  5745. {
  5746. u8 mode;
  5747. struct i40e_pf *pf = vsi->back;
  5748. struct i40e_hw *hw = &pf->hw;
  5749. int ret;
  5750. ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_dev_capabilities);
  5751. if (ret)
  5752. return -EINVAL;
  5753. if (hw->dev_caps.switch_mode) {
  5754. /* if switch mode is set, support mode2 (non-tunneled for
  5755. * cloud filter) for now
  5756. */
  5757. u32 switch_mode = hw->dev_caps.switch_mode &
  5758. I40E_SWITCH_MODE_MASK;
  5759. if (switch_mode >= I40E_CLOUD_FILTER_MODE1) {
  5760. if (switch_mode == I40E_CLOUD_FILTER_MODE2)
  5761. return 0;
  5762. dev_err(&pf->pdev->dev,
  5763. "Invalid switch_mode (%d), only non-tunneled mode for cloud filter is supported\n",
  5764. hw->dev_caps.switch_mode);
  5765. return -EINVAL;
  5766. }
  5767. }
  5768. /* Set Bit 7 to be valid */
  5769. mode = I40E_AQ_SET_SWITCH_BIT7_VALID;
  5770. /* Set L4type for TCP support */
  5771. mode |= I40E_AQ_SET_SWITCH_L4_TYPE_TCP;
  5772. /* Set cloud filter mode */
  5773. mode |= I40E_AQ_SET_SWITCH_MODE_NON_TUNNEL;
  5774. /* Prep mode field for set_switch_config */
  5775. ret = i40e_aq_set_switch_config(hw, pf->last_sw_conf_flags,
  5776. pf->last_sw_conf_valid_flags,
  5777. mode, NULL);
  5778. if (ret && hw->aq.asq_last_status != I40E_AQ_RC_ESRCH)
  5779. dev_err(&pf->pdev->dev,
  5780. "couldn't set switch config bits, err %pe aq_err %s\n",
  5781. ERR_PTR(ret),
  5782. i40e_aq_str(hw,
  5783. hw->aq.asq_last_status));
  5784. return ret;
  5785. }
  5786. /**
  5787. * i40e_create_queue_channel - function to create channel
  5788. * @vsi: VSI to be configured
  5789. * @ch: ptr to channel (it contains channel specific params)
  5790. *
  5791. * This function creates channel (VSI) using num_queues specified by user,
  5792. * reconfigs RSS if needed.
  5793. **/
  5794. int i40e_create_queue_channel(struct i40e_vsi *vsi,
  5795. struct i40e_channel *ch)
  5796. {
  5797. struct i40e_pf *pf = vsi->back;
  5798. bool reconfig_rss;
  5799. int err;
  5800. if (!ch)
  5801. return -EINVAL;
  5802. if (!ch->num_queue_pairs) {
  5803. dev_err(&pf->pdev->dev, "Invalid num_queues requested: %d\n",
  5804. ch->num_queue_pairs);
  5805. return -EINVAL;
  5806. }
  5807. /* validate user requested num_queues for channel */
  5808. err = i40e_validate_num_queues(pf, ch->num_queue_pairs, vsi,
  5809. &reconfig_rss);
  5810. if (err) {
  5811. dev_info(&pf->pdev->dev, "Failed to validate num_queues (%d)\n",
  5812. ch->num_queue_pairs);
  5813. return -EINVAL;
  5814. }
  5815. /* By default we are in VEPA mode, if this is the first VF/VMDq
  5816. * VSI to be added switch to VEB mode.
  5817. */
  5818. if (!test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags)) {
  5819. set_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags);
  5820. if (vsi->type == I40E_VSI_MAIN) {
  5821. if (i40e_is_tc_mqprio_enabled(pf))
  5822. i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
  5823. else
  5824. i40e_do_reset_safe(pf, I40E_PF_RESET_FLAG);
  5825. }
  5826. /* now onwards for main VSI, number of queues will be value
  5827. * of TC0's queue count
  5828. */
  5829. }
  5830. /* By this time, vsi->cnt_q_avail shall be set to non-zero and
  5831. * it should be more than num_queues
  5832. */
  5833. if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_queue_pairs) {
  5834. dev_dbg(&pf->pdev->dev,
  5835. "Error: cnt_q_avail (%u) less than num_queues %d\n",
  5836. vsi->cnt_q_avail, ch->num_queue_pairs);
  5837. return -EINVAL;
  5838. }
  5839. /* reconfig_rss only if vsi type is MAIN_VSI */
  5840. if (reconfig_rss && (vsi->type == I40E_VSI_MAIN)) {
  5841. err = i40e_vsi_reconfig_rss(vsi, ch->num_queue_pairs);
  5842. if (err) {
  5843. dev_info(&pf->pdev->dev,
  5844. "Error: unable to reconfig rss for num_queues (%u)\n",
  5845. ch->num_queue_pairs);
  5846. return -EINVAL;
  5847. }
  5848. }
  5849. if (!i40e_setup_channel(pf, vsi, ch)) {
  5850. dev_info(&pf->pdev->dev, "Failed to setup channel\n");
  5851. return -EINVAL;
  5852. }
  5853. dev_info(&pf->pdev->dev,
  5854. "Setup channel (id:%u) utilizing num_queues %d\n",
  5855. ch->seid, ch->num_queue_pairs);
  5856. /* configure VSI for BW limit */
  5857. if (ch->max_tx_rate) {
  5858. u64 credits = ch->max_tx_rate;
  5859. if (i40e_set_bw_limit(vsi, ch->seid, ch->max_tx_rate))
  5860. return -EINVAL;
  5861. do_div(credits, I40E_BW_CREDIT_DIVISOR);
  5862. dev_dbg(&pf->pdev->dev,
  5863. "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
  5864. ch->max_tx_rate,
  5865. credits,
  5866. ch->seid);
  5867. }
  5868. /* in case of VF, this will be main SRIOV VSI */
  5869. ch->parent_vsi = vsi;
  5870. /* and update main_vsi's count for queue_available to use */
  5871. vsi->cnt_q_avail -= ch->num_queue_pairs;
  5872. return 0;
  5873. }
  5874. /**
  5875. * i40e_configure_queue_channels - Add queue channel for the given TCs
  5876. * @vsi: VSI to be configured
  5877. *
  5878. * Configures queue channel mapping to the given TCs
  5879. **/
  5880. static int i40e_configure_queue_channels(struct i40e_vsi *vsi)
  5881. {
  5882. struct i40e_channel *ch;
  5883. u64 max_rate = 0;
  5884. int ret = 0, i;
  5885. /* Create app vsi with the TCs. Main VSI with TC0 is already set up */
  5886. vsi->tc_seid_map[0] = vsi->seid;
  5887. for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  5888. if (vsi->tc_config.enabled_tc & BIT(i)) {
  5889. ch = kzalloc(sizeof(*ch), GFP_KERNEL);
  5890. if (!ch) {
  5891. ret = -ENOMEM;
  5892. goto err_free;
  5893. }
  5894. INIT_LIST_HEAD(&ch->list);
  5895. ch->num_queue_pairs =
  5896. vsi->tc_config.tc_info[i].qcount;
  5897. ch->base_queue =
  5898. vsi->tc_config.tc_info[i].qoffset;
  5899. /* Bandwidth limit through tc interface is in bytes/s,
  5900. * change to Mbit/s
  5901. */
  5902. max_rate = vsi->mqprio_qopt.max_rate[i];
  5903. do_div(max_rate, I40E_BW_MBPS_DIVISOR);
  5904. ch->max_tx_rate = max_rate;
  5905. list_add_tail(&ch->list, &vsi->ch_list);
  5906. ret = i40e_create_queue_channel(vsi, ch);
  5907. if (ret) {
  5908. dev_err(&vsi->back->pdev->dev,
  5909. "Failed creating queue channel with TC%d: queues %d\n",
  5910. i, ch->num_queue_pairs);
  5911. goto err_free;
  5912. }
  5913. vsi->tc_seid_map[i] = ch->seid;
  5914. }
  5915. }
  5916. /* reset to reconfigure TX queue contexts */
  5917. i40e_do_reset(vsi->back, I40E_PF_RESET_FLAG, true);
  5918. return ret;
  5919. err_free:
  5920. i40e_remove_queue_channels(vsi);
  5921. return ret;
  5922. }
  5923. /**
  5924. * i40e_veb_config_tc - Configure TCs for given VEB
  5925. * @veb: given VEB
  5926. * @enabled_tc: TC bitmap
  5927. *
  5928. * Configures given TC bitmap for VEB (switching) element
  5929. **/
  5930. int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
  5931. {
  5932. struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
  5933. struct i40e_pf *pf = veb->pf;
  5934. int ret = 0;
  5935. int i;
  5936. /* No TCs or already enabled TCs just return */
  5937. if (!enabled_tc || veb->enabled_tc == enabled_tc)
  5938. return ret;
  5939. bw_data.tc_valid_bits = enabled_tc;
  5940. /* bw_data.absolute_credits is not set (relative) */
  5941. /* Enable ETS TCs with equal BW Share for now */
  5942. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  5943. if (enabled_tc & BIT(i))
  5944. bw_data.tc_bw_share_credits[i] = 1;
  5945. }
  5946. ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
  5947. &bw_data, NULL);
  5948. if (ret) {
  5949. dev_info(&pf->pdev->dev,
  5950. "VEB bw config failed, err %pe aq_err %s\n",
  5951. ERR_PTR(ret),
  5952. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5953. goto out;
  5954. }
  5955. /* Update the BW information */
  5956. ret = i40e_veb_get_bw_info(veb);
  5957. if (ret) {
  5958. dev_info(&pf->pdev->dev,
  5959. "Failed getting veb bw config, err %pe aq_err %s\n",
  5960. ERR_PTR(ret),
  5961. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5962. }
  5963. out:
  5964. return ret;
  5965. }
  5966. #ifdef CONFIG_I40E_DCB
  5967. /**
  5968. * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
  5969. * @pf: PF struct
  5970. *
  5971. * Reconfigure VEB/VSIs on a given PF; it is assumed that
  5972. * the caller would've quiesce all the VSIs before calling
  5973. * this function
  5974. **/
  5975. static void i40e_dcb_reconfigure(struct i40e_pf *pf)
  5976. {
  5977. struct i40e_vsi *vsi;
  5978. struct i40e_veb *veb;
  5979. u8 tc_map = 0;
  5980. int ret;
  5981. int v;
  5982. /* Enable the TCs available on PF to all VEBs */
  5983. tc_map = i40e_pf_get_tc_map(pf);
  5984. if (tc_map == I40E_DEFAULT_TRAFFIC_CLASS)
  5985. return;
  5986. i40e_pf_for_each_veb(pf, v, veb) {
  5987. ret = i40e_veb_config_tc(veb, tc_map);
  5988. if (ret) {
  5989. dev_info(&pf->pdev->dev,
  5990. "Failed configuring TC for VEB seid=%d\n",
  5991. veb->seid);
  5992. /* Will try to configure as many components */
  5993. }
  5994. }
  5995. /* Update each VSI */
  5996. i40e_pf_for_each_vsi(pf, v, vsi) {
  5997. /* - Enable all TCs for the LAN VSI
  5998. * - For all others keep them at TC0 for now
  5999. */
  6000. if (vsi->type == I40E_VSI_MAIN)
  6001. tc_map = i40e_pf_get_tc_map(pf);
  6002. else
  6003. tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
  6004. ret = i40e_vsi_config_tc(vsi, tc_map);
  6005. if (ret) {
  6006. dev_info(&pf->pdev->dev,
  6007. "Failed configuring TC for VSI seid=%d\n",
  6008. vsi->seid);
  6009. /* Will try to configure as many components */
  6010. } else {
  6011. /* Re-configure VSI vectors based on updated TC map */
  6012. i40e_vsi_map_rings_to_vectors(vsi);
  6013. if (vsi->netdev)
  6014. i40e_dcbnl_set_all(vsi);
  6015. }
  6016. }
  6017. }
  6018. /**
  6019. * i40e_resume_port_tx - Resume port Tx
  6020. * @pf: PF struct
  6021. *
  6022. * Resume a port's Tx and issue a PF reset in case of failure to
  6023. * resume.
  6024. **/
  6025. static int i40e_resume_port_tx(struct i40e_pf *pf)
  6026. {
  6027. struct i40e_hw *hw = &pf->hw;
  6028. int ret;
  6029. ret = i40e_aq_resume_port_tx(hw, NULL);
  6030. if (ret) {
  6031. dev_info(&pf->pdev->dev,
  6032. "Resume Port Tx failed, err %pe aq_err %s\n",
  6033. ERR_PTR(ret),
  6034. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  6035. /* Schedule PF reset to recover */
  6036. set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
  6037. i40e_service_event_schedule(pf);
  6038. }
  6039. return ret;
  6040. }
  6041. /**
  6042. * i40e_suspend_port_tx - Suspend port Tx
  6043. * @pf: PF struct
  6044. *
  6045. * Suspend a port's Tx and issue a PF reset in case of failure.
  6046. **/
  6047. static int i40e_suspend_port_tx(struct i40e_pf *pf)
  6048. {
  6049. struct i40e_hw *hw = &pf->hw;
  6050. int ret;
  6051. ret = i40e_aq_suspend_port_tx(hw, pf->mac_seid, NULL);
  6052. if (ret) {
  6053. dev_info(&pf->pdev->dev,
  6054. "Suspend Port Tx failed, err %pe aq_err %s\n",
  6055. ERR_PTR(ret),
  6056. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  6057. /* Schedule PF reset to recover */
  6058. set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
  6059. i40e_service_event_schedule(pf);
  6060. }
  6061. return ret;
  6062. }
  6063. /**
  6064. * i40e_hw_set_dcb_config - Program new DCBX settings into HW
  6065. * @pf: PF being configured
  6066. * @new_cfg: New DCBX configuration
  6067. *
  6068. * Program DCB settings into HW and reconfigure VEB/VSIs on
  6069. * given PF. Uses "Set LLDP MIB" AQC to program the hardware.
  6070. **/
  6071. static int i40e_hw_set_dcb_config(struct i40e_pf *pf,
  6072. struct i40e_dcbx_config *new_cfg)
  6073. {
  6074. struct i40e_dcbx_config *old_cfg = &pf->hw.local_dcbx_config;
  6075. int ret;
  6076. /* Check if need reconfiguration */
  6077. if (!memcmp(&new_cfg, &old_cfg, sizeof(new_cfg))) {
  6078. dev_dbg(&pf->pdev->dev, "No Change in DCB Config required.\n");
  6079. return 0;
  6080. }
  6081. /* Config change disable all VSIs */
  6082. i40e_pf_quiesce_all_vsi(pf);
  6083. /* Copy the new config to the current config */
  6084. *old_cfg = *new_cfg;
  6085. old_cfg->etsrec = old_cfg->etscfg;
  6086. ret = i40e_set_dcb_config(&pf->hw);
  6087. if (ret) {
  6088. dev_info(&pf->pdev->dev,
  6089. "Set DCB Config failed, err %pe aq_err %s\n",
  6090. ERR_PTR(ret),
  6091. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  6092. goto out;
  6093. }
  6094. /* Changes in configuration update VEB/VSI */
  6095. i40e_dcb_reconfigure(pf);
  6096. out:
  6097. /* In case of reset do not try to resume anything */
  6098. if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) {
  6099. /* Re-start the VSIs if disabled */
  6100. ret = i40e_resume_port_tx(pf);
  6101. /* In case of error no point in resuming VSIs */
  6102. if (ret)
  6103. goto err;
  6104. i40e_pf_unquiesce_all_vsi(pf);
  6105. }
  6106. err:
  6107. return ret;
  6108. }
  6109. /**
  6110. * i40e_hw_dcb_config - Program new DCBX settings into HW
  6111. * @pf: PF being configured
  6112. * @new_cfg: New DCBX configuration
  6113. *
  6114. * Program DCB settings into HW and reconfigure VEB/VSIs on
  6115. * given PF
  6116. **/
  6117. int i40e_hw_dcb_config(struct i40e_pf *pf, struct i40e_dcbx_config *new_cfg)
  6118. {
  6119. struct i40e_aqc_configure_switching_comp_ets_data ets_data;
  6120. u8 prio_type[I40E_MAX_TRAFFIC_CLASS] = {0};
  6121. u32 mfs_tc[I40E_MAX_TRAFFIC_CLASS];
  6122. struct i40e_dcbx_config *old_cfg;
  6123. u8 mode[I40E_MAX_TRAFFIC_CLASS];
  6124. struct i40e_rx_pb_config pb_cfg;
  6125. struct i40e_hw *hw = &pf->hw;
  6126. u8 num_ports = hw->num_ports;
  6127. bool need_reconfig;
  6128. int ret = -EINVAL;
  6129. u8 lltc_map = 0;
  6130. u8 tc_map = 0;
  6131. u8 new_numtc;
  6132. u8 i;
  6133. dev_dbg(&pf->pdev->dev, "Configuring DCB registers directly\n");
  6134. /* Un-pack information to Program ETS HW via shared API
  6135. * numtc, tcmap
  6136. * LLTC map
  6137. * ETS/NON-ETS arbiter mode
  6138. * max exponent (credit refills)
  6139. * Total number of ports
  6140. * PFC priority bit-map
  6141. * Priority Table
  6142. * BW % per TC
  6143. * Arbiter mode between UPs sharing same TC
  6144. * TSA table (ETS or non-ETS)
  6145. * EEE enabled or not
  6146. * MFS TC table
  6147. */
  6148. new_numtc = i40e_dcb_get_num_tc(new_cfg);
  6149. memset(&ets_data, 0, sizeof(ets_data));
  6150. for (i = 0; i < new_numtc; i++) {
  6151. tc_map |= BIT(i);
  6152. switch (new_cfg->etscfg.tsatable[i]) {
  6153. case I40E_IEEE_TSA_ETS:
  6154. prio_type[i] = I40E_DCB_PRIO_TYPE_ETS;
  6155. ets_data.tc_bw_share_credits[i] =
  6156. new_cfg->etscfg.tcbwtable[i];
  6157. break;
  6158. case I40E_IEEE_TSA_STRICT:
  6159. prio_type[i] = I40E_DCB_PRIO_TYPE_STRICT;
  6160. lltc_map |= BIT(i);
  6161. ets_data.tc_bw_share_credits[i] =
  6162. I40E_DCB_STRICT_PRIO_CREDITS;
  6163. break;
  6164. default:
  6165. /* Invalid TSA type */
  6166. need_reconfig = false;
  6167. goto out;
  6168. }
  6169. }
  6170. old_cfg = &hw->local_dcbx_config;
  6171. /* Check if need reconfiguration */
  6172. need_reconfig = i40e_dcb_need_reconfig(pf, old_cfg, new_cfg);
  6173. /* If needed, enable/disable frame tagging, disable all VSIs
  6174. * and suspend port tx
  6175. */
  6176. if (need_reconfig) {
  6177. /* Enable DCB tagging only when more than one TC */
  6178. if (new_numtc > 1)
  6179. set_bit(I40E_FLAG_DCB_ENA, pf->flags);
  6180. else
  6181. clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
  6182. set_bit(__I40E_PORT_SUSPENDED, pf->state);
  6183. /* Reconfiguration needed quiesce all VSIs */
  6184. i40e_pf_quiesce_all_vsi(pf);
  6185. ret = i40e_suspend_port_tx(pf);
  6186. if (ret)
  6187. goto err;
  6188. }
  6189. /* Configure Port ETS Tx Scheduler */
  6190. ets_data.tc_valid_bits = tc_map;
  6191. ets_data.tc_strict_priority_flags = lltc_map;
  6192. ret = i40e_aq_config_switch_comp_ets
  6193. (hw, pf->mac_seid, &ets_data,
  6194. i40e_aqc_opc_modify_switching_comp_ets, NULL);
  6195. if (ret) {
  6196. dev_info(&pf->pdev->dev,
  6197. "Modify Port ETS failed, err %pe aq_err %s\n",
  6198. ERR_PTR(ret),
  6199. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  6200. goto out;
  6201. }
  6202. /* Configure Rx ETS HW */
  6203. memset(&mode, I40E_DCB_ARB_MODE_ROUND_ROBIN, sizeof(mode));
  6204. i40e_dcb_hw_set_num_tc(hw, new_numtc);
  6205. i40e_dcb_hw_rx_fifo_config(hw, I40E_DCB_ARB_MODE_ROUND_ROBIN,
  6206. I40E_DCB_ARB_MODE_STRICT_PRIORITY,
  6207. I40E_DCB_DEFAULT_MAX_EXPONENT,
  6208. lltc_map);
  6209. i40e_dcb_hw_rx_cmd_monitor_config(hw, new_numtc, num_ports);
  6210. i40e_dcb_hw_rx_ets_bw_config(hw, new_cfg->etscfg.tcbwtable, mode,
  6211. prio_type);
  6212. i40e_dcb_hw_pfc_config(hw, new_cfg->pfc.pfcenable,
  6213. new_cfg->etscfg.prioritytable);
  6214. i40e_dcb_hw_rx_up2tc_config(hw, new_cfg->etscfg.prioritytable);
  6215. /* Configure Rx Packet Buffers in HW */
  6216. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  6217. struct i40e_vsi *main_vsi = i40e_pf_get_main_vsi(pf);
  6218. mfs_tc[i] = main_vsi->netdev->mtu;
  6219. mfs_tc[i] += I40E_PACKET_HDR_PAD;
  6220. }
  6221. i40e_dcb_hw_calculate_pool_sizes(hw, num_ports,
  6222. false, new_cfg->pfc.pfcenable,
  6223. mfs_tc, &pb_cfg);
  6224. i40e_dcb_hw_rx_pb_config(hw, &pf->pb_cfg, &pb_cfg);
  6225. /* Update the local Rx Packet buffer config */
  6226. pf->pb_cfg = pb_cfg;
  6227. /* Inform the FW about changes to DCB configuration */
  6228. ret = i40e_aq_dcb_updated(&pf->hw, NULL);
  6229. if (ret) {
  6230. dev_info(&pf->pdev->dev,
  6231. "DCB Updated failed, err %pe aq_err %s\n",
  6232. ERR_PTR(ret),
  6233. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  6234. goto out;
  6235. }
  6236. /* Update the port DCBx configuration */
  6237. *old_cfg = *new_cfg;
  6238. /* Changes in configuration update VEB/VSI */
  6239. i40e_dcb_reconfigure(pf);
  6240. out:
  6241. /* Re-start the VSIs if disabled */
  6242. if (need_reconfig) {
  6243. ret = i40e_resume_port_tx(pf);
  6244. clear_bit(__I40E_PORT_SUSPENDED, pf->state);
  6245. /* In case of error no point in resuming VSIs */
  6246. if (ret)
  6247. goto err;
  6248. /* Wait for the PF's queues to be disabled */
  6249. ret = i40e_pf_wait_queues_disabled(pf);
  6250. if (ret) {
  6251. /* Schedule PF reset to recover */
  6252. set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
  6253. i40e_service_event_schedule(pf);
  6254. goto err;
  6255. } else {
  6256. i40e_pf_unquiesce_all_vsi(pf);
  6257. set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
  6258. set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
  6259. }
  6260. /* registers are set, lets apply */
  6261. if (test_bit(I40E_HW_CAP_USE_SET_LLDP_MIB, pf->hw.caps))
  6262. ret = i40e_hw_set_dcb_config(pf, new_cfg);
  6263. }
  6264. err:
  6265. return ret;
  6266. }
  6267. /**
  6268. * i40e_dcb_sw_default_config - Set default DCB configuration when DCB in SW
  6269. * @pf: PF being queried
  6270. *
  6271. * Set default DCB configuration in case DCB is to be done in SW.
  6272. **/
  6273. int i40e_dcb_sw_default_config(struct i40e_pf *pf)
  6274. {
  6275. struct i40e_dcbx_config *dcb_cfg = &pf->hw.local_dcbx_config;
  6276. struct i40e_aqc_configure_switching_comp_ets_data ets_data;
  6277. struct i40e_hw *hw = &pf->hw;
  6278. int err;
  6279. if (test_bit(I40E_HW_CAP_USE_SET_LLDP_MIB, pf->hw.caps)) {
  6280. /* Update the local cached instance with TC0 ETS */
  6281. memset(&pf->tmp_cfg, 0, sizeof(struct i40e_dcbx_config));
  6282. pf->tmp_cfg.etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
  6283. pf->tmp_cfg.etscfg.maxtcs = 0;
  6284. pf->tmp_cfg.etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
  6285. pf->tmp_cfg.etscfg.tsatable[0] = I40E_IEEE_TSA_ETS;
  6286. pf->tmp_cfg.pfc.willing = I40E_IEEE_DEFAULT_PFC_WILLING;
  6287. pf->tmp_cfg.pfc.pfccap = I40E_MAX_TRAFFIC_CLASS;
  6288. /* FW needs one App to configure HW */
  6289. pf->tmp_cfg.numapps = I40E_IEEE_DEFAULT_NUM_APPS;
  6290. pf->tmp_cfg.app[0].selector = I40E_APP_SEL_ETHTYPE;
  6291. pf->tmp_cfg.app[0].priority = I40E_IEEE_DEFAULT_APP_PRIO;
  6292. pf->tmp_cfg.app[0].protocolid = I40E_APP_PROTOID_FCOE;
  6293. return i40e_hw_set_dcb_config(pf, &pf->tmp_cfg);
  6294. }
  6295. memset(&ets_data, 0, sizeof(ets_data));
  6296. ets_data.tc_valid_bits = I40E_DEFAULT_TRAFFIC_CLASS; /* TC0 only */
  6297. ets_data.tc_strict_priority_flags = 0; /* ETS */
  6298. ets_data.tc_bw_share_credits[0] = I40E_IEEE_DEFAULT_ETS_TCBW; /* 100% to TC0 */
  6299. /* Enable ETS on the Physical port */
  6300. err = i40e_aq_config_switch_comp_ets
  6301. (hw, pf->mac_seid, &ets_data,
  6302. i40e_aqc_opc_enable_switching_comp_ets, NULL);
  6303. if (err) {
  6304. dev_info(&pf->pdev->dev,
  6305. "Enable Port ETS failed, err %pe aq_err %s\n",
  6306. ERR_PTR(err),
  6307. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  6308. err = -ENOENT;
  6309. goto out;
  6310. }
  6311. /* Update the local cached instance with TC0 ETS */
  6312. dcb_cfg->etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
  6313. dcb_cfg->etscfg.cbs = 0;
  6314. dcb_cfg->etscfg.maxtcs = I40E_MAX_TRAFFIC_CLASS;
  6315. dcb_cfg->etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
  6316. out:
  6317. return err;
  6318. }
  6319. /**
  6320. * i40e_init_pf_dcb - Initialize DCB configuration
  6321. * @pf: PF being configured
  6322. *
  6323. * Query the current DCB configuration and cache it
  6324. * in the hardware structure
  6325. **/
  6326. static int i40e_init_pf_dcb(struct i40e_pf *pf)
  6327. {
  6328. struct i40e_hw *hw = &pf->hw;
  6329. int err;
  6330. /* Do not enable DCB for SW1 and SW2 images even if the FW is capable
  6331. * Also do not enable DCBx if FW LLDP agent is disabled
  6332. */
  6333. if (test_bit(I40E_HW_CAP_NO_DCB_SUPPORT, pf->hw.caps)) {
  6334. dev_info(&pf->pdev->dev, "DCB is not supported.\n");
  6335. err = -EOPNOTSUPP;
  6336. goto out;
  6337. }
  6338. if (test_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags)) {
  6339. dev_info(&pf->pdev->dev, "FW LLDP is disabled, attempting SW DCB\n");
  6340. err = i40e_dcb_sw_default_config(pf);
  6341. if (err) {
  6342. dev_info(&pf->pdev->dev, "Could not initialize SW DCB\n");
  6343. goto out;
  6344. }
  6345. dev_info(&pf->pdev->dev, "SW DCB initialization succeeded.\n");
  6346. pf->dcbx_cap = DCB_CAP_DCBX_HOST |
  6347. DCB_CAP_DCBX_VER_IEEE;
  6348. /* at init capable but disabled */
  6349. set_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
  6350. clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
  6351. goto out;
  6352. }
  6353. err = i40e_init_dcb(hw, true);
  6354. if (!err) {
  6355. /* Device/Function is not DCBX capable */
  6356. if ((!hw->func_caps.dcb) ||
  6357. (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
  6358. dev_info(&pf->pdev->dev,
  6359. "DCBX offload is not supported or is disabled for this PF.\n");
  6360. } else {
  6361. /* When status is not DISABLED then DCBX in FW */
  6362. pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
  6363. DCB_CAP_DCBX_VER_IEEE;
  6364. set_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
  6365. /* Enable DCB tagging only when more than one TC
  6366. * or explicitly disable if only one TC
  6367. */
  6368. if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
  6369. set_bit(I40E_FLAG_DCB_ENA, pf->flags);
  6370. else
  6371. clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
  6372. dev_dbg(&pf->pdev->dev,
  6373. "DCBX offload is supported for this PF.\n");
  6374. }
  6375. } else if (pf->hw.aq.asq_last_status == I40E_AQ_RC_EPERM) {
  6376. dev_info(&pf->pdev->dev, "FW LLDP disabled for this PF.\n");
  6377. set_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags);
  6378. } else {
  6379. dev_info(&pf->pdev->dev,
  6380. "Query for DCB configuration failed, err %pe aq_err %s\n",
  6381. ERR_PTR(err),
  6382. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  6383. }
  6384. out:
  6385. return err;
  6386. }
  6387. #endif /* CONFIG_I40E_DCB */
  6388. static void i40e_print_link_message_eee(struct i40e_vsi *vsi,
  6389. const char *speed, const char *fc)
  6390. {
  6391. struct ethtool_keee kedata;
  6392. memzero_explicit(&kedata, sizeof(kedata));
  6393. if (vsi->netdev->ethtool_ops->get_eee)
  6394. vsi->netdev->ethtool_ops->get_eee(vsi->netdev, &kedata);
  6395. if (!linkmode_empty(kedata.supported))
  6396. netdev_info(vsi->netdev,
  6397. "NIC Link is Up, %sbps Full Duplex, Flow Control: %s, EEE: %s\n",
  6398. speed, fc,
  6399. kedata.eee_enabled ? "Enabled" : "Disabled");
  6400. else
  6401. netdev_info(vsi->netdev,
  6402. "NIC Link is Up, %sbps Full Duplex, Flow Control: %s\n",
  6403. speed, fc);
  6404. }
  6405. /**
  6406. * i40e_print_link_message - print link up or down
  6407. * @vsi: the VSI for which link needs a message
  6408. * @isup: true of link is up, false otherwise
  6409. */
  6410. void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
  6411. {
  6412. enum i40e_aq_link_speed new_speed;
  6413. struct i40e_pf *pf = vsi->back;
  6414. char *speed = "Unknown";
  6415. char *fc = "Unknown";
  6416. char *fec = "";
  6417. char *req_fec = "";
  6418. char *an = "";
  6419. if (isup)
  6420. new_speed = pf->hw.phy.link_info.link_speed;
  6421. else
  6422. new_speed = I40E_LINK_SPEED_UNKNOWN;
  6423. if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
  6424. return;
  6425. vsi->current_isup = isup;
  6426. vsi->current_speed = new_speed;
  6427. if (!isup) {
  6428. netdev_info(vsi->netdev, "NIC Link is Down\n");
  6429. return;
  6430. }
  6431. /* Warn user if link speed on NPAR enabled partition is not at
  6432. * least 10GB
  6433. */
  6434. if (pf->hw.func_caps.npar_enable &&
  6435. (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
  6436. pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
  6437. netdev_warn(vsi->netdev,
  6438. "The partition detected link speed that is less than 10Gbps\n");
  6439. switch (pf->hw.phy.link_info.link_speed) {
  6440. case I40E_LINK_SPEED_40GB:
  6441. speed = "40 G";
  6442. break;
  6443. case I40E_LINK_SPEED_20GB:
  6444. speed = "20 G";
  6445. break;
  6446. case I40E_LINK_SPEED_25GB:
  6447. speed = "25 G";
  6448. break;
  6449. case I40E_LINK_SPEED_10GB:
  6450. speed = "10 G";
  6451. break;
  6452. case I40E_LINK_SPEED_5GB:
  6453. speed = "5 G";
  6454. break;
  6455. case I40E_LINK_SPEED_2_5GB:
  6456. speed = "2.5 G";
  6457. break;
  6458. case I40E_LINK_SPEED_1GB:
  6459. speed = "1000 M";
  6460. break;
  6461. case I40E_LINK_SPEED_100MB:
  6462. speed = "100 M";
  6463. break;
  6464. default:
  6465. break;
  6466. }
  6467. switch (pf->hw.fc.current_mode) {
  6468. case I40E_FC_FULL:
  6469. fc = "RX/TX";
  6470. break;
  6471. case I40E_FC_TX_PAUSE:
  6472. fc = "TX";
  6473. break;
  6474. case I40E_FC_RX_PAUSE:
  6475. fc = "RX";
  6476. break;
  6477. default:
  6478. fc = "None";
  6479. break;
  6480. }
  6481. if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
  6482. req_fec = "None";
  6483. fec = "None";
  6484. an = "False";
  6485. if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
  6486. an = "True";
  6487. if (pf->hw.phy.link_info.fec_info &
  6488. I40E_AQ_CONFIG_FEC_KR_ENA)
  6489. fec = "CL74 FC-FEC/BASE-R";
  6490. else if (pf->hw.phy.link_info.fec_info &
  6491. I40E_AQ_CONFIG_FEC_RS_ENA)
  6492. fec = "CL108 RS-FEC";
  6493. /* 'CL108 RS-FEC' should be displayed when RS is requested, or
  6494. * both RS and FC are requested
  6495. */
  6496. if (vsi->back->hw.phy.link_info.req_fec_info &
  6497. (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
  6498. if (vsi->back->hw.phy.link_info.req_fec_info &
  6499. I40E_AQ_REQUEST_FEC_RS)
  6500. req_fec = "CL108 RS-FEC";
  6501. else
  6502. req_fec = "CL74 FC-FEC/BASE-R";
  6503. }
  6504. netdev_info(vsi->netdev,
  6505. "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
  6506. speed, req_fec, fec, an, fc);
  6507. } else if (pf->hw.device_id == I40E_DEV_ID_KX_X722) {
  6508. req_fec = "None";
  6509. fec = "None";
  6510. an = "False";
  6511. if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
  6512. an = "True";
  6513. if (pf->hw.phy.link_info.fec_info &
  6514. I40E_AQ_CONFIG_FEC_KR_ENA)
  6515. fec = "CL74 FC-FEC/BASE-R";
  6516. if (pf->hw.phy.link_info.req_fec_info &
  6517. I40E_AQ_REQUEST_FEC_KR)
  6518. req_fec = "CL74 FC-FEC/BASE-R";
  6519. netdev_info(vsi->netdev,
  6520. "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
  6521. speed, req_fec, fec, an, fc);
  6522. } else {
  6523. i40e_print_link_message_eee(vsi, speed, fc);
  6524. }
  6525. }
  6526. /**
  6527. * i40e_up_complete - Finish the last steps of bringing up a connection
  6528. * @vsi: the VSI being configured
  6529. **/
  6530. static int i40e_up_complete(struct i40e_vsi *vsi)
  6531. {
  6532. struct i40e_pf *pf = vsi->back;
  6533. int err;
  6534. if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
  6535. i40e_vsi_configure_msix(vsi);
  6536. else
  6537. i40e_configure_msi_and_legacy(vsi);
  6538. /* start rings */
  6539. err = i40e_vsi_start_rings(vsi);
  6540. if (err)
  6541. return err;
  6542. clear_bit(__I40E_VSI_DOWN, vsi->state);
  6543. i40e_napi_enable_all(vsi);
  6544. i40e_vsi_enable_irq(vsi);
  6545. if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
  6546. (vsi->netdev)) {
  6547. i40e_print_link_message(vsi, true);
  6548. netif_tx_start_all_queues(vsi->netdev);
  6549. netif_carrier_on(vsi->netdev);
  6550. }
  6551. /* replay FDIR SB filters */
  6552. if (vsi->type == I40E_VSI_FDIR) {
  6553. /* reset fd counters */
  6554. pf->fd_add_err = 0;
  6555. pf->fd_atr_cnt = 0;
  6556. i40e_fdir_filter_restore(vsi);
  6557. }
  6558. /* On the next run of the service_task, notify any clients of the new
  6559. * opened netdev
  6560. */
  6561. set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
  6562. i40e_service_event_schedule(pf);
  6563. return 0;
  6564. }
  6565. /**
  6566. * i40e_vsi_reinit_locked - Reset the VSI
  6567. * @vsi: the VSI being configured
  6568. *
  6569. * Rebuild the ring structs after some configuration
  6570. * has changed, e.g. MTU size.
  6571. **/
  6572. static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
  6573. {
  6574. struct i40e_pf *pf = vsi->back;
  6575. while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
  6576. usleep_range(1000, 2000);
  6577. i40e_down(vsi);
  6578. i40e_up(vsi);
  6579. clear_bit(__I40E_CONFIG_BUSY, pf->state);
  6580. }
  6581. /**
  6582. * i40e_force_link_state - Force the link status
  6583. * @pf: board private structure
  6584. * @is_up: whether the link state should be forced up or down
  6585. **/
  6586. static int i40e_force_link_state(struct i40e_pf *pf, bool is_up)
  6587. {
  6588. struct i40e_aq_get_phy_abilities_resp abilities;
  6589. struct i40e_aq_set_phy_config config = {0};
  6590. bool non_zero_phy_type = is_up;
  6591. struct i40e_hw *hw = &pf->hw;
  6592. u64 mask;
  6593. u8 speed;
  6594. int err;
  6595. /* Card might've been put in an unstable state by other drivers
  6596. * and applications, which causes incorrect speed values being
  6597. * set on startup. In order to clear speed registers, we call
  6598. * get_phy_capabilities twice, once to get initial state of
  6599. * available speeds, and once to get current PHY config.
  6600. */
  6601. err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities,
  6602. NULL);
  6603. if (err) {
  6604. dev_err(&pf->pdev->dev,
  6605. "failed to get phy cap., ret = %pe last_status = %s\n",
  6606. ERR_PTR(err),
  6607. i40e_aq_str(hw, hw->aq.asq_last_status));
  6608. return err;
  6609. }
  6610. speed = abilities.link_speed;
  6611. /* Get the current phy config */
  6612. err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
  6613. NULL);
  6614. if (err) {
  6615. dev_err(&pf->pdev->dev,
  6616. "failed to get phy cap., ret = %pe last_status = %s\n",
  6617. ERR_PTR(err),
  6618. i40e_aq_str(hw, hw->aq.asq_last_status));
  6619. return err;
  6620. }
  6621. /* If link needs to go up, but was not forced to go down,
  6622. * and its speed values are OK, no need for a flap
  6623. * if non_zero_phy_type was set, still need to force up
  6624. */
  6625. if (test_bit(I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENA, pf->flags))
  6626. non_zero_phy_type = true;
  6627. else if (is_up && abilities.phy_type != 0 && abilities.link_speed != 0)
  6628. return 0;
  6629. /* To force link we need to set bits for all supported PHY types,
  6630. * but there are now more than 32, so we need to split the bitmap
  6631. * across two fields.
  6632. */
  6633. mask = I40E_PHY_TYPES_BITMASK;
  6634. config.phy_type =
  6635. non_zero_phy_type ? cpu_to_le32((u32)(mask & 0xffffffff)) : 0;
  6636. config.phy_type_ext =
  6637. non_zero_phy_type ? (u8)((mask >> 32) & 0xff) : 0;
  6638. /* Copy the old settings, except of phy_type */
  6639. config.abilities = abilities.abilities;
  6640. if (test_bit(I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENA, pf->flags)) {
  6641. if (is_up)
  6642. config.abilities |= I40E_AQ_PHY_ENABLE_LINK;
  6643. else
  6644. config.abilities &= ~(I40E_AQ_PHY_ENABLE_LINK);
  6645. }
  6646. if (abilities.link_speed != 0)
  6647. config.link_speed = abilities.link_speed;
  6648. else
  6649. config.link_speed = speed;
  6650. config.eee_capability = abilities.eee_capability;
  6651. config.eeer = abilities.eeer_val;
  6652. config.low_power_ctrl = abilities.d3_lpan;
  6653. config.fec_config = abilities.fec_cfg_curr_mod_ext_info &
  6654. I40E_AQ_PHY_FEC_CONFIG_MASK;
  6655. err = i40e_aq_set_phy_config(hw, &config, NULL);
  6656. if (err) {
  6657. dev_err(&pf->pdev->dev,
  6658. "set phy config ret = %pe last_status = %s\n",
  6659. ERR_PTR(err),
  6660. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  6661. return err;
  6662. }
  6663. /* Update the link info */
  6664. err = i40e_update_link_info(hw);
  6665. if (err) {
  6666. /* Wait a little bit (on 40G cards it sometimes takes a really
  6667. * long time for link to come back from the atomic reset)
  6668. * and try once more
  6669. */
  6670. msleep(1000);
  6671. i40e_update_link_info(hw);
  6672. }
  6673. i40e_aq_set_link_restart_an(hw, is_up, NULL);
  6674. return 0;
  6675. }
  6676. /**
  6677. * i40e_up - Bring the connection back up after being down
  6678. * @vsi: the VSI being configured
  6679. **/
  6680. int i40e_up(struct i40e_vsi *vsi)
  6681. {
  6682. int err;
  6683. if (vsi->type == I40E_VSI_MAIN &&
  6684. (test_bit(I40E_FLAG_LINK_DOWN_ON_CLOSE_ENA, vsi->back->flags) ||
  6685. test_bit(I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENA, vsi->back->flags)))
  6686. i40e_force_link_state(vsi->back, true);
  6687. err = i40e_vsi_configure(vsi);
  6688. if (!err)
  6689. err = i40e_up_complete(vsi);
  6690. return err;
  6691. }
  6692. /**
  6693. * i40e_down - Shutdown the connection processing
  6694. * @vsi: the VSI being stopped
  6695. **/
  6696. void i40e_down(struct i40e_vsi *vsi)
  6697. {
  6698. int i;
  6699. /* It is assumed that the caller of this function
  6700. * sets the vsi->state __I40E_VSI_DOWN bit.
  6701. */
  6702. if (vsi->netdev) {
  6703. netif_carrier_off(vsi->netdev);
  6704. netif_tx_disable(vsi->netdev);
  6705. }
  6706. i40e_vsi_disable_irq(vsi);
  6707. i40e_vsi_stop_rings(vsi);
  6708. if (vsi->type == I40E_VSI_MAIN &&
  6709. (test_bit(I40E_FLAG_LINK_DOWN_ON_CLOSE_ENA, vsi->back->flags) ||
  6710. test_bit(I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENA, vsi->back->flags)))
  6711. i40e_force_link_state(vsi->back, false);
  6712. i40e_napi_disable_all(vsi);
  6713. for (i = 0; i < vsi->num_queue_pairs; i++) {
  6714. i40e_clean_tx_ring(vsi->tx_rings[i]);
  6715. if (i40e_enabled_xdp_vsi(vsi)) {
  6716. /* Make sure that in-progress ndo_xdp_xmit and
  6717. * ndo_xsk_wakeup calls are completed.
  6718. */
  6719. synchronize_rcu();
  6720. i40e_clean_tx_ring(vsi->xdp_rings[i]);
  6721. }
  6722. i40e_clean_rx_ring(vsi->rx_rings[i]);
  6723. }
  6724. }
  6725. /**
  6726. * i40e_validate_mqprio_qopt- validate queue mapping info
  6727. * @vsi: the VSI being configured
  6728. * @mqprio_qopt: queue parametrs
  6729. **/
  6730. static int i40e_validate_mqprio_qopt(struct i40e_vsi *vsi,
  6731. struct tc_mqprio_qopt_offload *mqprio_qopt)
  6732. {
  6733. u64 sum_max_rate = 0;
  6734. u64 max_rate = 0;
  6735. int i;
  6736. if (mqprio_qopt->qopt.offset[0] != 0 ||
  6737. mqprio_qopt->qopt.num_tc < 1 ||
  6738. mqprio_qopt->qopt.num_tc > I40E_MAX_TRAFFIC_CLASS)
  6739. return -EINVAL;
  6740. for (i = 0; ; i++) {
  6741. if (!mqprio_qopt->qopt.count[i])
  6742. return -EINVAL;
  6743. if (mqprio_qopt->min_rate[i]) {
  6744. dev_err(&vsi->back->pdev->dev,
  6745. "Invalid min tx rate (greater than 0) specified\n");
  6746. return -EINVAL;
  6747. }
  6748. max_rate = mqprio_qopt->max_rate[i];
  6749. do_div(max_rate, I40E_BW_MBPS_DIVISOR);
  6750. sum_max_rate += max_rate;
  6751. if (i >= mqprio_qopt->qopt.num_tc - 1)
  6752. break;
  6753. if (mqprio_qopt->qopt.offset[i + 1] !=
  6754. (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
  6755. return -EINVAL;
  6756. }
  6757. if (vsi->num_queue_pairs <
  6758. (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i])) {
  6759. dev_err(&vsi->back->pdev->dev,
  6760. "Failed to create traffic channel, insufficient number of queues.\n");
  6761. return -EINVAL;
  6762. }
  6763. if (sum_max_rate > i40e_get_link_speed(vsi)) {
  6764. dev_err(&vsi->back->pdev->dev,
  6765. "Invalid max tx rate specified\n");
  6766. return -EINVAL;
  6767. }
  6768. return 0;
  6769. }
  6770. /**
  6771. * i40e_vsi_set_default_tc_config - set default values for tc configuration
  6772. * @vsi: the VSI being configured
  6773. **/
  6774. static void i40e_vsi_set_default_tc_config(struct i40e_vsi *vsi)
  6775. {
  6776. u16 qcount;
  6777. int i;
  6778. /* Only TC0 is enabled */
  6779. vsi->tc_config.numtc = 1;
  6780. vsi->tc_config.enabled_tc = 1;
  6781. qcount = min_t(int, vsi->alloc_queue_pairs,
  6782. i40e_pf_get_max_q_per_tc(vsi->back));
  6783. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  6784. /* For the TC that is not enabled set the offset to default
  6785. * queue and allocate one queue for the given TC.
  6786. */
  6787. vsi->tc_config.tc_info[i].qoffset = 0;
  6788. if (i == 0)
  6789. vsi->tc_config.tc_info[i].qcount = qcount;
  6790. else
  6791. vsi->tc_config.tc_info[i].qcount = 1;
  6792. vsi->tc_config.tc_info[i].netdev_tc = 0;
  6793. }
  6794. }
  6795. /**
  6796. * i40e_del_macvlan_filter
  6797. * @hw: pointer to the HW structure
  6798. * @seid: seid of the channel VSI
  6799. * @macaddr: the mac address to apply as a filter
  6800. * @aq_err: store the admin Q error
  6801. *
  6802. * This function deletes a mac filter on the channel VSI which serves as the
  6803. * macvlan. Returns 0 on success.
  6804. **/
  6805. static int i40e_del_macvlan_filter(struct i40e_hw *hw, u16 seid,
  6806. const u8 *macaddr, int *aq_err)
  6807. {
  6808. struct i40e_aqc_remove_macvlan_element_data element;
  6809. int status;
  6810. memset(&element, 0, sizeof(element));
  6811. ether_addr_copy(element.mac_addr, macaddr);
  6812. element.vlan_tag = 0;
  6813. element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
  6814. status = i40e_aq_remove_macvlan(hw, seid, &element, 1, NULL);
  6815. *aq_err = hw->aq.asq_last_status;
  6816. return status;
  6817. }
  6818. /**
  6819. * i40e_add_macvlan_filter
  6820. * @hw: pointer to the HW structure
  6821. * @seid: seid of the channel VSI
  6822. * @macaddr: the mac address to apply as a filter
  6823. * @aq_err: store the admin Q error
  6824. *
  6825. * This function adds a mac filter on the channel VSI which serves as the
  6826. * macvlan. Returns 0 on success.
  6827. **/
  6828. static int i40e_add_macvlan_filter(struct i40e_hw *hw, u16 seid,
  6829. const u8 *macaddr, int *aq_err)
  6830. {
  6831. struct i40e_aqc_add_macvlan_element_data element;
  6832. u16 cmd_flags = 0;
  6833. int status;
  6834. ether_addr_copy(element.mac_addr, macaddr);
  6835. element.vlan_tag = 0;
  6836. element.queue_number = 0;
  6837. element.match_method = I40E_AQC_MM_ERR_NO_RES;
  6838. cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
  6839. element.flags = cpu_to_le16(cmd_flags);
  6840. status = i40e_aq_add_macvlan(hw, seid, &element, 1, NULL);
  6841. *aq_err = hw->aq.asq_last_status;
  6842. return status;
  6843. }
  6844. /**
  6845. * i40e_reset_ch_rings - Reset the queue contexts in a channel
  6846. * @vsi: the VSI we want to access
  6847. * @ch: the channel we want to access
  6848. */
  6849. static void i40e_reset_ch_rings(struct i40e_vsi *vsi, struct i40e_channel *ch)
  6850. {
  6851. struct i40e_ring *tx_ring, *rx_ring;
  6852. u16 pf_q;
  6853. int i;
  6854. for (i = 0; i < ch->num_queue_pairs; i++) {
  6855. pf_q = ch->base_queue + i;
  6856. tx_ring = vsi->tx_rings[pf_q];
  6857. tx_ring->ch = NULL;
  6858. rx_ring = vsi->rx_rings[pf_q];
  6859. rx_ring->ch = NULL;
  6860. }
  6861. }
  6862. /**
  6863. * i40e_free_macvlan_channels
  6864. * @vsi: the VSI we want to access
  6865. *
  6866. * This function frees the Qs of the channel VSI from
  6867. * the stack and also deletes the channel VSIs which
  6868. * serve as macvlans.
  6869. */
  6870. static void i40e_free_macvlan_channels(struct i40e_vsi *vsi)
  6871. {
  6872. struct i40e_channel *ch, *ch_tmp;
  6873. int ret;
  6874. if (list_empty(&vsi->macvlan_list))
  6875. return;
  6876. list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
  6877. struct i40e_vsi *parent_vsi;
  6878. if (i40e_is_channel_macvlan(ch)) {
  6879. i40e_reset_ch_rings(vsi, ch);
  6880. clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
  6881. netdev_unbind_sb_channel(vsi->netdev, ch->fwd->netdev);
  6882. netdev_set_sb_channel(ch->fwd->netdev, 0);
  6883. kfree(ch->fwd);
  6884. ch->fwd = NULL;
  6885. }
  6886. list_del(&ch->list);
  6887. parent_vsi = ch->parent_vsi;
  6888. if (!parent_vsi || !ch->initialized) {
  6889. kfree(ch);
  6890. continue;
  6891. }
  6892. /* remove the VSI */
  6893. ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
  6894. NULL);
  6895. if (ret)
  6896. dev_err(&vsi->back->pdev->dev,
  6897. "unable to remove channel (%d) for parent VSI(%d)\n",
  6898. ch->seid, parent_vsi->seid);
  6899. kfree(ch);
  6900. }
  6901. vsi->macvlan_cnt = 0;
  6902. }
  6903. /**
  6904. * i40e_fwd_ring_up - bring the macvlan device up
  6905. * @vsi: the VSI we want to access
  6906. * @vdev: macvlan netdevice
  6907. * @fwd: the private fwd structure
  6908. */
  6909. static int i40e_fwd_ring_up(struct i40e_vsi *vsi, struct net_device *vdev,
  6910. struct i40e_fwd_adapter *fwd)
  6911. {
  6912. struct i40e_channel *ch = NULL, *ch_tmp, *iter;
  6913. int ret = 0, num_tc = 1, i, aq_err;
  6914. struct i40e_pf *pf = vsi->back;
  6915. struct i40e_hw *hw = &pf->hw;
  6916. /* Go through the list and find an available channel */
  6917. list_for_each_entry_safe(iter, ch_tmp, &vsi->macvlan_list, list) {
  6918. if (!i40e_is_channel_macvlan(iter)) {
  6919. iter->fwd = fwd;
  6920. /* record configuration for macvlan interface in vdev */
  6921. for (i = 0; i < num_tc; i++)
  6922. netdev_bind_sb_channel_queue(vsi->netdev, vdev,
  6923. i,
  6924. iter->num_queue_pairs,
  6925. iter->base_queue);
  6926. for (i = 0; i < iter->num_queue_pairs; i++) {
  6927. struct i40e_ring *tx_ring, *rx_ring;
  6928. u16 pf_q;
  6929. pf_q = iter->base_queue + i;
  6930. /* Get to TX ring ptr */
  6931. tx_ring = vsi->tx_rings[pf_q];
  6932. tx_ring->ch = iter;
  6933. /* Get the RX ring ptr */
  6934. rx_ring = vsi->rx_rings[pf_q];
  6935. rx_ring->ch = iter;
  6936. }
  6937. ch = iter;
  6938. break;
  6939. }
  6940. }
  6941. if (!ch)
  6942. return -EINVAL;
  6943. /* Guarantee all rings are updated before we update the
  6944. * MAC address filter.
  6945. */
  6946. wmb();
  6947. /* Add a mac filter */
  6948. ret = i40e_add_macvlan_filter(hw, ch->seid, vdev->dev_addr, &aq_err);
  6949. if (ret) {
  6950. /* if we cannot add the MAC rule then disable the offload */
  6951. macvlan_release_l2fw_offload(vdev);
  6952. for (i = 0; i < ch->num_queue_pairs; i++) {
  6953. struct i40e_ring *rx_ring;
  6954. u16 pf_q;
  6955. pf_q = ch->base_queue + i;
  6956. rx_ring = vsi->rx_rings[pf_q];
  6957. rx_ring->netdev = NULL;
  6958. }
  6959. dev_info(&pf->pdev->dev,
  6960. "Error adding mac filter on macvlan err %pe, aq_err %s\n",
  6961. ERR_PTR(ret),
  6962. i40e_aq_str(hw, aq_err));
  6963. netdev_err(vdev, "L2fwd offload disabled to L2 filter error\n");
  6964. }
  6965. return ret;
  6966. }
  6967. /**
  6968. * i40e_setup_macvlans - create the channels which will be macvlans
  6969. * @vsi: the VSI we want to access
  6970. * @macvlan_cnt: no. of macvlans to be setup
  6971. * @qcnt: no. of Qs per macvlan
  6972. * @vdev: macvlan netdevice
  6973. */
  6974. static int i40e_setup_macvlans(struct i40e_vsi *vsi, u16 macvlan_cnt, u16 qcnt,
  6975. struct net_device *vdev)
  6976. {
  6977. struct i40e_pf *pf = vsi->back;
  6978. struct i40e_hw *hw = &pf->hw;
  6979. struct i40e_vsi_context ctxt;
  6980. u16 sections, qmap, num_qps;
  6981. struct i40e_channel *ch;
  6982. int i, pow, ret = 0;
  6983. u8 offset = 0;
  6984. if (vsi->type != I40E_VSI_MAIN || !macvlan_cnt)
  6985. return -EINVAL;
  6986. num_qps = vsi->num_queue_pairs - (macvlan_cnt * qcnt);
  6987. /* find the next higher power-of-2 of num queue pairs */
  6988. pow = fls(roundup_pow_of_two(num_qps) - 1);
  6989. qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
  6990. (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
  6991. /* Setup context bits for the main VSI */
  6992. sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
  6993. sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
  6994. memset(&ctxt, 0, sizeof(ctxt));
  6995. ctxt.seid = vsi->seid;
  6996. ctxt.pf_num = vsi->back->hw.pf_id;
  6997. ctxt.vf_num = 0;
  6998. ctxt.uplink_seid = vsi->uplink_seid;
  6999. ctxt.info = vsi->info;
  7000. ctxt.info.tc_mapping[0] = cpu_to_le16(qmap);
  7001. ctxt.info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
  7002. ctxt.info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
  7003. ctxt.info.valid_sections |= cpu_to_le16(sections);
  7004. /* Reconfigure RSS for main VSI with new max queue count */
  7005. vsi->rss_size = max_t(u16, num_qps, qcnt);
  7006. ret = i40e_vsi_config_rss(vsi);
  7007. if (ret) {
  7008. dev_info(&pf->pdev->dev,
  7009. "Failed to reconfig RSS for num_queues (%u)\n",
  7010. vsi->rss_size);
  7011. return ret;
  7012. }
  7013. vsi->reconfig_rss = true;
  7014. dev_dbg(&vsi->back->pdev->dev,
  7015. "Reconfigured RSS with num_queues (%u)\n", vsi->rss_size);
  7016. vsi->next_base_queue = num_qps;
  7017. vsi->cnt_q_avail = vsi->num_queue_pairs - num_qps;
  7018. /* Update the VSI after updating the VSI queue-mapping
  7019. * information
  7020. */
  7021. ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
  7022. if (ret) {
  7023. dev_info(&pf->pdev->dev,
  7024. "Update vsi tc config failed, err %pe aq_err %s\n",
  7025. ERR_PTR(ret),
  7026. i40e_aq_str(hw, hw->aq.asq_last_status));
  7027. return ret;
  7028. }
  7029. /* update the local VSI info with updated queue map */
  7030. i40e_vsi_update_queue_map(vsi, &ctxt);
  7031. vsi->info.valid_sections = 0;
  7032. /* Create channels for macvlans */
  7033. INIT_LIST_HEAD(&vsi->macvlan_list);
  7034. for (i = 0; i < macvlan_cnt; i++) {
  7035. ch = kzalloc(sizeof(*ch), GFP_KERNEL);
  7036. if (!ch) {
  7037. ret = -ENOMEM;
  7038. goto err_free;
  7039. }
  7040. INIT_LIST_HEAD(&ch->list);
  7041. ch->num_queue_pairs = qcnt;
  7042. if (!i40e_setup_channel(pf, vsi, ch)) {
  7043. ret = -EINVAL;
  7044. kfree(ch);
  7045. goto err_free;
  7046. }
  7047. ch->parent_vsi = vsi;
  7048. vsi->cnt_q_avail -= ch->num_queue_pairs;
  7049. vsi->macvlan_cnt++;
  7050. list_add_tail(&ch->list, &vsi->macvlan_list);
  7051. }
  7052. return ret;
  7053. err_free:
  7054. dev_info(&pf->pdev->dev, "Failed to setup macvlans\n");
  7055. i40e_free_macvlan_channels(vsi);
  7056. return ret;
  7057. }
  7058. /**
  7059. * i40e_fwd_add - configure macvlans
  7060. * @netdev: net device to configure
  7061. * @vdev: macvlan netdevice
  7062. **/
  7063. static void *i40e_fwd_add(struct net_device *netdev, struct net_device *vdev)
  7064. {
  7065. struct i40e_netdev_priv *np = netdev_priv(netdev);
  7066. u16 q_per_macvlan = 0, macvlan_cnt = 0, vectors;
  7067. struct i40e_vsi *vsi = np->vsi;
  7068. struct i40e_pf *pf = vsi->back;
  7069. struct i40e_fwd_adapter *fwd;
  7070. int avail_macvlan, ret;
  7071. if (test_bit(I40E_FLAG_DCB_ENA, pf->flags)) {
  7072. netdev_info(netdev, "Macvlans are not supported when DCB is enabled\n");
  7073. return ERR_PTR(-EINVAL);
  7074. }
  7075. if (i40e_is_tc_mqprio_enabled(pf)) {
  7076. netdev_info(netdev, "Macvlans are not supported when HW TC offload is on\n");
  7077. return ERR_PTR(-EINVAL);
  7078. }
  7079. if (pf->num_lan_msix < I40E_MIN_MACVLAN_VECTORS) {
  7080. netdev_info(netdev, "Not enough vectors available to support macvlans\n");
  7081. return ERR_PTR(-EINVAL);
  7082. }
  7083. /* The macvlan device has to be a single Q device so that the
  7084. * tc_to_txq field can be reused to pick the tx queue.
  7085. */
  7086. if (netif_is_multiqueue(vdev))
  7087. return ERR_PTR(-ERANGE);
  7088. if (!vsi->macvlan_cnt) {
  7089. /* reserve bit 0 for the pf device */
  7090. set_bit(0, vsi->fwd_bitmask);
  7091. /* Try to reserve as many queues as possible for macvlans. First
  7092. * reserve 3/4th of max vectors, then half, then quarter and
  7093. * calculate Qs per macvlan as you go
  7094. */
  7095. vectors = pf->num_lan_msix;
  7096. if (vectors <= I40E_MAX_MACVLANS && vectors > 64) {
  7097. /* allocate 4 Qs per macvlan and 32 Qs to the PF*/
  7098. q_per_macvlan = 4;
  7099. macvlan_cnt = (vectors - 32) / 4;
  7100. } else if (vectors <= 64 && vectors > 32) {
  7101. /* allocate 2 Qs per macvlan and 16 Qs to the PF*/
  7102. q_per_macvlan = 2;
  7103. macvlan_cnt = (vectors - 16) / 2;
  7104. } else if (vectors <= 32 && vectors > 16) {
  7105. /* allocate 1 Q per macvlan and 16 Qs to the PF*/
  7106. q_per_macvlan = 1;
  7107. macvlan_cnt = vectors - 16;
  7108. } else if (vectors <= 16 && vectors > 8) {
  7109. /* allocate 1 Q per macvlan and 8 Qs to the PF */
  7110. q_per_macvlan = 1;
  7111. macvlan_cnt = vectors - 8;
  7112. } else {
  7113. /* allocate 1 Q per macvlan and 1 Q to the PF */
  7114. q_per_macvlan = 1;
  7115. macvlan_cnt = vectors - 1;
  7116. }
  7117. if (macvlan_cnt == 0)
  7118. return ERR_PTR(-EBUSY);
  7119. /* Quiesce VSI queues */
  7120. i40e_quiesce_vsi(vsi);
  7121. /* sets up the macvlans but does not "enable" them */
  7122. ret = i40e_setup_macvlans(vsi, macvlan_cnt, q_per_macvlan,
  7123. vdev);
  7124. if (ret)
  7125. return ERR_PTR(ret);
  7126. /* Unquiesce VSI */
  7127. i40e_unquiesce_vsi(vsi);
  7128. }
  7129. avail_macvlan = find_first_zero_bit(vsi->fwd_bitmask,
  7130. vsi->macvlan_cnt);
  7131. if (avail_macvlan >= I40E_MAX_MACVLANS)
  7132. return ERR_PTR(-EBUSY);
  7133. /* create the fwd struct */
  7134. fwd = kzalloc(sizeof(*fwd), GFP_KERNEL);
  7135. if (!fwd)
  7136. return ERR_PTR(-ENOMEM);
  7137. set_bit(avail_macvlan, vsi->fwd_bitmask);
  7138. fwd->bit_no = avail_macvlan;
  7139. netdev_set_sb_channel(vdev, avail_macvlan);
  7140. fwd->netdev = vdev;
  7141. if (!netif_running(netdev))
  7142. return fwd;
  7143. /* Set fwd ring up */
  7144. ret = i40e_fwd_ring_up(vsi, vdev, fwd);
  7145. if (ret) {
  7146. /* unbind the queues and drop the subordinate channel config */
  7147. netdev_unbind_sb_channel(netdev, vdev);
  7148. netdev_set_sb_channel(vdev, 0);
  7149. kfree(fwd);
  7150. return ERR_PTR(-EINVAL);
  7151. }
  7152. return fwd;
  7153. }
  7154. /**
  7155. * i40e_del_all_macvlans - Delete all the mac filters on the channels
  7156. * @vsi: the VSI we want to access
  7157. */
  7158. static void i40e_del_all_macvlans(struct i40e_vsi *vsi)
  7159. {
  7160. struct i40e_channel *ch, *ch_tmp;
  7161. struct i40e_pf *pf = vsi->back;
  7162. struct i40e_hw *hw = &pf->hw;
  7163. int aq_err, ret = 0;
  7164. if (list_empty(&vsi->macvlan_list))
  7165. return;
  7166. list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
  7167. if (i40e_is_channel_macvlan(ch)) {
  7168. ret = i40e_del_macvlan_filter(hw, ch->seid,
  7169. i40e_channel_mac(ch),
  7170. &aq_err);
  7171. if (!ret) {
  7172. /* Reset queue contexts */
  7173. i40e_reset_ch_rings(vsi, ch);
  7174. clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
  7175. netdev_unbind_sb_channel(vsi->netdev,
  7176. ch->fwd->netdev);
  7177. netdev_set_sb_channel(ch->fwd->netdev, 0);
  7178. kfree(ch->fwd);
  7179. ch->fwd = NULL;
  7180. }
  7181. }
  7182. }
  7183. }
  7184. /**
  7185. * i40e_fwd_del - delete macvlan interfaces
  7186. * @netdev: net device to configure
  7187. * @vdev: macvlan netdevice
  7188. */
  7189. static void i40e_fwd_del(struct net_device *netdev, void *vdev)
  7190. {
  7191. struct i40e_netdev_priv *np = netdev_priv(netdev);
  7192. struct i40e_fwd_adapter *fwd = vdev;
  7193. struct i40e_channel *ch, *ch_tmp;
  7194. struct i40e_vsi *vsi = np->vsi;
  7195. struct i40e_pf *pf = vsi->back;
  7196. struct i40e_hw *hw = &pf->hw;
  7197. int aq_err, ret = 0;
  7198. /* Find the channel associated with the macvlan and del mac filter */
  7199. list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
  7200. if (i40e_is_channel_macvlan(ch) &&
  7201. ether_addr_equal(i40e_channel_mac(ch),
  7202. fwd->netdev->dev_addr)) {
  7203. ret = i40e_del_macvlan_filter(hw, ch->seid,
  7204. i40e_channel_mac(ch),
  7205. &aq_err);
  7206. if (!ret) {
  7207. /* Reset queue contexts */
  7208. i40e_reset_ch_rings(vsi, ch);
  7209. clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
  7210. netdev_unbind_sb_channel(netdev, fwd->netdev);
  7211. netdev_set_sb_channel(fwd->netdev, 0);
  7212. kfree(ch->fwd);
  7213. ch->fwd = NULL;
  7214. } else {
  7215. dev_info(&pf->pdev->dev,
  7216. "Error deleting mac filter on macvlan err %pe, aq_err %s\n",
  7217. ERR_PTR(ret),
  7218. i40e_aq_str(hw, aq_err));
  7219. }
  7220. break;
  7221. }
  7222. }
  7223. }
  7224. /**
  7225. * i40e_setup_tc - configure multiple traffic classes
  7226. * @netdev: net device to configure
  7227. * @type_data: tc offload data
  7228. **/
  7229. static int i40e_setup_tc(struct net_device *netdev, void *type_data)
  7230. {
  7231. struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
  7232. struct i40e_netdev_priv *np = netdev_priv(netdev);
  7233. struct i40e_vsi *vsi = np->vsi;
  7234. struct i40e_pf *pf = vsi->back;
  7235. u8 enabled_tc = 0, num_tc, hw;
  7236. bool need_reset = false;
  7237. int old_queue_pairs;
  7238. int ret = -EINVAL;
  7239. u16 mode;
  7240. int i;
  7241. old_queue_pairs = vsi->num_queue_pairs;
  7242. num_tc = mqprio_qopt->qopt.num_tc;
  7243. hw = mqprio_qopt->qopt.hw;
  7244. mode = mqprio_qopt->mode;
  7245. if (!hw) {
  7246. clear_bit(I40E_FLAG_TC_MQPRIO_ENA, pf->flags);
  7247. memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
  7248. goto config_tc;
  7249. }
  7250. /* Check if MFP enabled */
  7251. if (test_bit(I40E_FLAG_MFP_ENA, pf->flags)) {
  7252. netdev_info(netdev,
  7253. "Configuring TC not supported in MFP mode\n");
  7254. return ret;
  7255. }
  7256. switch (mode) {
  7257. case TC_MQPRIO_MODE_DCB:
  7258. clear_bit(I40E_FLAG_TC_MQPRIO_ENA, pf->flags);
  7259. /* Check if DCB enabled to continue */
  7260. if (!test_bit(I40E_FLAG_DCB_ENA, pf->flags)) {
  7261. netdev_info(netdev,
  7262. "DCB is not enabled for adapter\n");
  7263. return ret;
  7264. }
  7265. /* Check whether tc count is within enabled limit */
  7266. if (num_tc > i40e_pf_get_num_tc(pf)) {
  7267. netdev_info(netdev,
  7268. "TC count greater than enabled on link for adapter\n");
  7269. return ret;
  7270. }
  7271. break;
  7272. case TC_MQPRIO_MODE_CHANNEL:
  7273. if (test_bit(I40E_FLAG_DCB_ENA, pf->flags)) {
  7274. netdev_info(netdev,
  7275. "Full offload of TC Mqprio options is not supported when DCB is enabled\n");
  7276. return ret;
  7277. }
  7278. if (!test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
  7279. return ret;
  7280. ret = i40e_validate_mqprio_qopt(vsi, mqprio_qopt);
  7281. if (ret)
  7282. return ret;
  7283. memcpy(&vsi->mqprio_qopt, mqprio_qopt,
  7284. sizeof(*mqprio_qopt));
  7285. set_bit(I40E_FLAG_TC_MQPRIO_ENA, pf->flags);
  7286. clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
  7287. break;
  7288. default:
  7289. return -EINVAL;
  7290. }
  7291. config_tc:
  7292. /* Generate TC map for number of tc requested */
  7293. for (i = 0; i < num_tc; i++)
  7294. enabled_tc |= BIT(i);
  7295. /* Requesting same TC configuration as already enabled */
  7296. if (enabled_tc == vsi->tc_config.enabled_tc &&
  7297. mode != TC_MQPRIO_MODE_CHANNEL)
  7298. return 0;
  7299. /* Quiesce VSI queues */
  7300. i40e_quiesce_vsi(vsi);
  7301. if (!hw && !i40e_is_tc_mqprio_enabled(pf))
  7302. i40e_remove_queue_channels(vsi);
  7303. /* Configure VSI for enabled TCs */
  7304. ret = i40e_vsi_config_tc(vsi, enabled_tc);
  7305. if (ret) {
  7306. netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
  7307. vsi->seid);
  7308. need_reset = true;
  7309. goto exit;
  7310. } else if (enabled_tc &&
  7311. (!is_power_of_2(vsi->tc_config.tc_info[0].qcount))) {
  7312. netdev_info(netdev,
  7313. "Failed to create channel. Override queues (%u) not power of 2\n",
  7314. vsi->tc_config.tc_info[0].qcount);
  7315. ret = -EINVAL;
  7316. need_reset = true;
  7317. goto exit;
  7318. }
  7319. dev_info(&vsi->back->pdev->dev,
  7320. "Setup channel (id:%u) utilizing num_queues %d\n",
  7321. vsi->seid, vsi->tc_config.tc_info[0].qcount);
  7322. if (i40e_is_tc_mqprio_enabled(pf)) {
  7323. if (vsi->mqprio_qopt.max_rate[0]) {
  7324. u64 max_tx_rate = i40e_bw_bytes_to_mbits(vsi,
  7325. vsi->mqprio_qopt.max_rate[0]);
  7326. ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
  7327. if (!ret) {
  7328. u64 credits = max_tx_rate;
  7329. do_div(credits, I40E_BW_CREDIT_DIVISOR);
  7330. dev_dbg(&vsi->back->pdev->dev,
  7331. "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
  7332. max_tx_rate,
  7333. credits,
  7334. vsi->seid);
  7335. } else {
  7336. need_reset = true;
  7337. goto exit;
  7338. }
  7339. }
  7340. ret = i40e_configure_queue_channels(vsi);
  7341. if (ret) {
  7342. vsi->num_queue_pairs = old_queue_pairs;
  7343. netdev_info(netdev,
  7344. "Failed configuring queue channels\n");
  7345. need_reset = true;
  7346. goto exit;
  7347. }
  7348. }
  7349. exit:
  7350. /* Reset the configuration data to defaults, only TC0 is enabled */
  7351. if (need_reset) {
  7352. i40e_vsi_set_default_tc_config(vsi);
  7353. need_reset = false;
  7354. }
  7355. /* Unquiesce VSI */
  7356. i40e_unquiesce_vsi(vsi);
  7357. return ret;
  7358. }
  7359. /**
  7360. * i40e_set_cld_element - sets cloud filter element data
  7361. * @filter: cloud filter rule
  7362. * @cld: ptr to cloud filter element data
  7363. *
  7364. * This is helper function to copy data into cloud filter element
  7365. **/
  7366. static inline void
  7367. i40e_set_cld_element(struct i40e_cloud_filter *filter,
  7368. struct i40e_aqc_cloud_filters_element_data *cld)
  7369. {
  7370. u32 ipa;
  7371. int i;
  7372. memset(cld, 0, sizeof(*cld));
  7373. ether_addr_copy(cld->outer_mac, filter->dst_mac);
  7374. ether_addr_copy(cld->inner_mac, filter->src_mac);
  7375. if (filter->n_proto != ETH_P_IP && filter->n_proto != ETH_P_IPV6)
  7376. return;
  7377. if (filter->n_proto == ETH_P_IPV6) {
  7378. #define IPV6_MAX_INDEX (ARRAY_SIZE(filter->dst_ipv6) - 1)
  7379. for (i = 0; i < ARRAY_SIZE(filter->dst_ipv6); i++) {
  7380. ipa = be32_to_cpu(filter->dst_ipv6[IPV6_MAX_INDEX - i]);
  7381. *(__le32 *)&cld->ipaddr.raw_v6.data[i * 2] = cpu_to_le32(ipa);
  7382. }
  7383. } else {
  7384. ipa = be32_to_cpu(filter->dst_ipv4);
  7385. memcpy(&cld->ipaddr.v4.data, &ipa, sizeof(ipa));
  7386. }
  7387. cld->inner_vlan = cpu_to_le16(ntohs(filter->vlan_id));
  7388. /* tenant_id is not supported by FW now, once the support is enabled
  7389. * fill the cld->tenant_id with cpu_to_le32(filter->tenant_id)
  7390. */
  7391. if (filter->tenant_id)
  7392. return;
  7393. }
  7394. /**
  7395. * i40e_add_del_cloud_filter - Add/del cloud filter
  7396. * @vsi: pointer to VSI
  7397. * @filter: cloud filter rule
  7398. * @add: if true, add, if false, delete
  7399. *
  7400. * Add or delete a cloud filter for a specific flow spec.
  7401. * Returns 0 if the filter were successfully added.
  7402. **/
  7403. int i40e_add_del_cloud_filter(struct i40e_vsi *vsi,
  7404. struct i40e_cloud_filter *filter, bool add)
  7405. {
  7406. struct i40e_aqc_cloud_filters_element_data cld_filter;
  7407. struct i40e_pf *pf = vsi->back;
  7408. int ret;
  7409. static const u16 flag_table[128] = {
  7410. [I40E_CLOUD_FILTER_FLAGS_OMAC] =
  7411. I40E_AQC_ADD_CLOUD_FILTER_OMAC,
  7412. [I40E_CLOUD_FILTER_FLAGS_IMAC] =
  7413. I40E_AQC_ADD_CLOUD_FILTER_IMAC,
  7414. [I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN] =
  7415. I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN,
  7416. [I40E_CLOUD_FILTER_FLAGS_IMAC_TEN_ID] =
  7417. I40E_AQC_ADD_CLOUD_FILTER_IMAC_TEN_ID,
  7418. [I40E_CLOUD_FILTER_FLAGS_OMAC_TEN_ID_IMAC] =
  7419. I40E_AQC_ADD_CLOUD_FILTER_OMAC_TEN_ID_IMAC,
  7420. [I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN_TEN_ID] =
  7421. I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN_TEN_ID,
  7422. [I40E_CLOUD_FILTER_FLAGS_IIP] =
  7423. I40E_AQC_ADD_CLOUD_FILTER_IIP,
  7424. };
  7425. if (filter->flags >= ARRAY_SIZE(flag_table))
  7426. return -EIO;
  7427. memset(&cld_filter, 0, sizeof(cld_filter));
  7428. /* copy element needed to add cloud filter from filter */
  7429. i40e_set_cld_element(filter, &cld_filter);
  7430. if (filter->tunnel_type != I40E_CLOUD_TNL_TYPE_NONE)
  7431. cld_filter.flags = cpu_to_le16(filter->tunnel_type <<
  7432. I40E_AQC_ADD_CLOUD_TNL_TYPE_SHIFT);
  7433. if (filter->n_proto == ETH_P_IPV6)
  7434. cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
  7435. I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
  7436. else
  7437. cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
  7438. I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
  7439. if (add)
  7440. ret = i40e_aq_add_cloud_filters(&pf->hw, filter->seid,
  7441. &cld_filter, 1);
  7442. else
  7443. ret = i40e_aq_rem_cloud_filters(&pf->hw, filter->seid,
  7444. &cld_filter, 1);
  7445. if (ret)
  7446. dev_dbg(&pf->pdev->dev,
  7447. "Failed to %s cloud filter using l4 port %u, err %d aq_err %d\n",
  7448. add ? "add" : "delete", filter->dst_port, ret,
  7449. pf->hw.aq.asq_last_status);
  7450. else
  7451. dev_info(&pf->pdev->dev,
  7452. "%s cloud filter for VSI: %d\n",
  7453. add ? "Added" : "Deleted", filter->seid);
  7454. return ret;
  7455. }
  7456. /**
  7457. * i40e_add_del_cloud_filter_big_buf - Add/del cloud filter using big_buf
  7458. * @vsi: pointer to VSI
  7459. * @filter: cloud filter rule
  7460. * @add: if true, add, if false, delete
  7461. *
  7462. * Add or delete a cloud filter for a specific flow spec using big buffer.
  7463. * Returns 0 if the filter were successfully added.
  7464. **/
  7465. int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi,
  7466. struct i40e_cloud_filter *filter,
  7467. bool add)
  7468. {
  7469. struct i40e_aqc_cloud_filters_element_bb cld_filter;
  7470. struct i40e_pf *pf = vsi->back;
  7471. int ret;
  7472. /* Both (src/dst) valid mac_addr are not supported */
  7473. if ((is_valid_ether_addr(filter->dst_mac) &&
  7474. is_valid_ether_addr(filter->src_mac)) ||
  7475. (is_multicast_ether_addr(filter->dst_mac) &&
  7476. is_multicast_ether_addr(filter->src_mac)))
  7477. return -EOPNOTSUPP;
  7478. /* Big buffer cloud filter needs 'L4 port' to be non-zero. Also, UDP
  7479. * ports are not supported via big buffer now.
  7480. */
  7481. if (!filter->dst_port || filter->ip_proto == IPPROTO_UDP)
  7482. return -EOPNOTSUPP;
  7483. /* adding filter using src_port/src_ip is not supported at this stage */
  7484. if (filter->src_port ||
  7485. (filter->src_ipv4 && filter->n_proto != ETH_P_IPV6) ||
  7486. !ipv6_addr_any(&filter->ip.v6.src_ip6))
  7487. return -EOPNOTSUPP;
  7488. memset(&cld_filter, 0, sizeof(cld_filter));
  7489. /* copy element needed to add cloud filter from filter */
  7490. i40e_set_cld_element(filter, &cld_filter.element);
  7491. if (is_valid_ether_addr(filter->dst_mac) ||
  7492. is_valid_ether_addr(filter->src_mac) ||
  7493. is_multicast_ether_addr(filter->dst_mac) ||
  7494. is_multicast_ether_addr(filter->src_mac)) {
  7495. /* MAC + IP : unsupported mode */
  7496. if (filter->dst_ipv4)
  7497. return -EOPNOTSUPP;
  7498. /* since we validated that L4 port must be valid before
  7499. * we get here, start with respective "flags" value
  7500. * and update if vlan is present or not
  7501. */
  7502. cld_filter.element.flags =
  7503. cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_PORT);
  7504. if (filter->vlan_id) {
  7505. cld_filter.element.flags =
  7506. cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_VLAN_PORT);
  7507. }
  7508. } else if ((filter->dst_ipv4 && filter->n_proto != ETH_P_IPV6) ||
  7509. !ipv6_addr_any(&filter->ip.v6.dst_ip6)) {
  7510. cld_filter.element.flags =
  7511. cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_IP_PORT);
  7512. if (filter->n_proto == ETH_P_IPV6)
  7513. cld_filter.element.flags |=
  7514. cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
  7515. else
  7516. cld_filter.element.flags |=
  7517. cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
  7518. } else {
  7519. dev_err(&pf->pdev->dev,
  7520. "either mac or ip has to be valid for cloud filter\n");
  7521. return -EINVAL;
  7522. }
  7523. /* Now copy L4 port in Byte 6..7 in general fields */
  7524. cld_filter.general_fields[I40E_AQC_ADD_CLOUD_FV_FLU_0X16_WORD0] =
  7525. be16_to_cpu(filter->dst_port);
  7526. if (add) {
  7527. /* Validate current device switch mode, change if necessary */
  7528. ret = i40e_validate_and_set_switch_mode(vsi);
  7529. if (ret) {
  7530. dev_err(&pf->pdev->dev,
  7531. "failed to set switch mode, ret %d\n",
  7532. ret);
  7533. return ret;
  7534. }
  7535. ret = i40e_aq_add_cloud_filters_bb(&pf->hw, filter->seid,
  7536. &cld_filter, 1);
  7537. } else {
  7538. ret = i40e_aq_rem_cloud_filters_bb(&pf->hw, filter->seid,
  7539. &cld_filter, 1);
  7540. }
  7541. if (ret)
  7542. dev_dbg(&pf->pdev->dev,
  7543. "Failed to %s cloud filter(big buffer) err %d aq_err %d\n",
  7544. add ? "add" : "delete", ret, pf->hw.aq.asq_last_status);
  7545. else
  7546. dev_info(&pf->pdev->dev,
  7547. "%s cloud filter for VSI: %d, L4 port: %d\n",
  7548. add ? "add" : "delete", filter->seid,
  7549. ntohs(filter->dst_port));
  7550. return ret;
  7551. }
  7552. /**
  7553. * i40e_parse_cls_flower - Parse tc flower filters provided by kernel
  7554. * @vsi: Pointer to VSI
  7555. * @f: Pointer to struct flow_cls_offload
  7556. * @filter: Pointer to cloud filter structure
  7557. *
  7558. **/
  7559. static int i40e_parse_cls_flower(struct i40e_vsi *vsi,
  7560. struct flow_cls_offload *f,
  7561. struct i40e_cloud_filter *filter)
  7562. {
  7563. struct flow_rule *rule = flow_cls_offload_flow_rule(f);
  7564. struct flow_dissector *dissector = rule->match.dissector;
  7565. u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
  7566. struct i40e_pf *pf = vsi->back;
  7567. u8 field_flags = 0;
  7568. if (dissector->used_keys &
  7569. ~(BIT_ULL(FLOW_DISSECTOR_KEY_CONTROL) |
  7570. BIT_ULL(FLOW_DISSECTOR_KEY_BASIC) |
  7571. BIT_ULL(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
  7572. BIT_ULL(FLOW_DISSECTOR_KEY_VLAN) |
  7573. BIT_ULL(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
  7574. BIT_ULL(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
  7575. BIT_ULL(FLOW_DISSECTOR_KEY_PORTS) |
  7576. BIT_ULL(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
  7577. dev_err(&pf->pdev->dev, "Unsupported key used: 0x%llx\n",
  7578. dissector->used_keys);
  7579. return -EOPNOTSUPP;
  7580. }
  7581. if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
  7582. struct flow_match_enc_keyid match;
  7583. flow_rule_match_enc_keyid(rule, &match);
  7584. if (match.mask->keyid != 0)
  7585. field_flags |= I40E_CLOUD_FIELD_TEN_ID;
  7586. filter->tenant_id = be32_to_cpu(match.key->keyid);
  7587. }
  7588. if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
  7589. struct flow_match_basic match;
  7590. flow_rule_match_basic(rule, &match);
  7591. n_proto_key = ntohs(match.key->n_proto);
  7592. n_proto_mask = ntohs(match.mask->n_proto);
  7593. if (n_proto_key == ETH_P_ALL) {
  7594. n_proto_key = 0;
  7595. n_proto_mask = 0;
  7596. }
  7597. filter->n_proto = n_proto_key & n_proto_mask;
  7598. filter->ip_proto = match.key->ip_proto;
  7599. }
  7600. if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
  7601. struct flow_match_eth_addrs match;
  7602. flow_rule_match_eth_addrs(rule, &match);
  7603. /* use is_broadcast and is_zero to check for all 0xf or 0 */
  7604. if (!is_zero_ether_addr(match.mask->dst)) {
  7605. if (is_broadcast_ether_addr(match.mask->dst)) {
  7606. field_flags |= I40E_CLOUD_FIELD_OMAC;
  7607. } else {
  7608. dev_err(&pf->pdev->dev, "Bad ether dest mask %pM\n",
  7609. match.mask->dst);
  7610. return -EIO;
  7611. }
  7612. }
  7613. if (!is_zero_ether_addr(match.mask->src)) {
  7614. if (is_broadcast_ether_addr(match.mask->src)) {
  7615. field_flags |= I40E_CLOUD_FIELD_IMAC;
  7616. } else {
  7617. dev_err(&pf->pdev->dev, "Bad ether src mask %pM\n",
  7618. match.mask->src);
  7619. return -EIO;
  7620. }
  7621. }
  7622. ether_addr_copy(filter->dst_mac, match.key->dst);
  7623. ether_addr_copy(filter->src_mac, match.key->src);
  7624. }
  7625. if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
  7626. struct flow_match_vlan match;
  7627. flow_rule_match_vlan(rule, &match);
  7628. if (match.mask->vlan_id) {
  7629. if (match.mask->vlan_id == VLAN_VID_MASK) {
  7630. field_flags |= I40E_CLOUD_FIELD_IVLAN;
  7631. } else {
  7632. dev_err(&pf->pdev->dev, "Bad vlan mask 0x%04x\n",
  7633. match.mask->vlan_id);
  7634. return -EIO;
  7635. }
  7636. }
  7637. filter->vlan_id = cpu_to_be16(match.key->vlan_id);
  7638. }
  7639. if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
  7640. struct flow_match_control match;
  7641. flow_rule_match_control(rule, &match);
  7642. addr_type = match.key->addr_type;
  7643. if (flow_rule_has_control_flags(match.mask->flags,
  7644. f->common.extack))
  7645. return -EOPNOTSUPP;
  7646. }
  7647. if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
  7648. struct flow_match_ipv4_addrs match;
  7649. flow_rule_match_ipv4_addrs(rule, &match);
  7650. if (match.mask->dst) {
  7651. if (match.mask->dst == cpu_to_be32(0xffffffff)) {
  7652. field_flags |= I40E_CLOUD_FIELD_IIP;
  7653. } else {
  7654. dev_err(&pf->pdev->dev, "Bad ip dst mask %pI4b\n",
  7655. &match.mask->dst);
  7656. return -EIO;
  7657. }
  7658. }
  7659. if (match.mask->src) {
  7660. if (match.mask->src == cpu_to_be32(0xffffffff)) {
  7661. field_flags |= I40E_CLOUD_FIELD_IIP;
  7662. } else {
  7663. dev_err(&pf->pdev->dev, "Bad ip src mask %pI4b\n",
  7664. &match.mask->src);
  7665. return -EIO;
  7666. }
  7667. }
  7668. if (field_flags & I40E_CLOUD_FIELD_TEN_ID) {
  7669. dev_err(&pf->pdev->dev, "Tenant id not allowed for ip filter\n");
  7670. return -EIO;
  7671. }
  7672. filter->dst_ipv4 = match.key->dst;
  7673. filter->src_ipv4 = match.key->src;
  7674. }
  7675. if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
  7676. struct flow_match_ipv6_addrs match;
  7677. flow_rule_match_ipv6_addrs(rule, &match);
  7678. /* src and dest IPV6 address should not be LOOPBACK
  7679. * (0:0:0:0:0:0:0:1), which can be represented as ::1
  7680. */
  7681. if (ipv6_addr_loopback(&match.key->dst) ||
  7682. ipv6_addr_loopback(&match.key->src)) {
  7683. dev_err(&pf->pdev->dev,
  7684. "Bad ipv6, addr is LOOPBACK\n");
  7685. return -EIO;
  7686. }
  7687. if (!ipv6_addr_any(&match.mask->dst) ||
  7688. !ipv6_addr_any(&match.mask->src))
  7689. field_flags |= I40E_CLOUD_FIELD_IIP;
  7690. memcpy(&filter->src_ipv6, &match.key->src.s6_addr32,
  7691. sizeof(filter->src_ipv6));
  7692. memcpy(&filter->dst_ipv6, &match.key->dst.s6_addr32,
  7693. sizeof(filter->dst_ipv6));
  7694. }
  7695. if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
  7696. struct flow_match_ports match;
  7697. flow_rule_match_ports(rule, &match);
  7698. if (match.mask->src) {
  7699. if (match.mask->src == cpu_to_be16(0xffff)) {
  7700. field_flags |= I40E_CLOUD_FIELD_IIP;
  7701. } else {
  7702. dev_err(&pf->pdev->dev, "Bad src port mask 0x%04x\n",
  7703. be16_to_cpu(match.mask->src));
  7704. return -EIO;
  7705. }
  7706. }
  7707. if (match.mask->dst) {
  7708. if (match.mask->dst == cpu_to_be16(0xffff)) {
  7709. field_flags |= I40E_CLOUD_FIELD_IIP;
  7710. } else {
  7711. dev_err(&pf->pdev->dev, "Bad dst port mask 0x%04x\n",
  7712. be16_to_cpu(match.mask->dst));
  7713. return -EIO;
  7714. }
  7715. }
  7716. filter->dst_port = match.key->dst;
  7717. filter->src_port = match.key->src;
  7718. switch (filter->ip_proto) {
  7719. case IPPROTO_TCP:
  7720. case IPPROTO_UDP:
  7721. break;
  7722. default:
  7723. dev_err(&pf->pdev->dev,
  7724. "Only UDP and TCP transport are supported\n");
  7725. return -EINVAL;
  7726. }
  7727. }
  7728. filter->flags = field_flags;
  7729. return 0;
  7730. }
  7731. /**
  7732. * i40e_handle_tclass: Forward to a traffic class on the device
  7733. * @vsi: Pointer to VSI
  7734. * @tc: traffic class index on the device
  7735. * @filter: Pointer to cloud filter structure
  7736. *
  7737. **/
  7738. static int i40e_handle_tclass(struct i40e_vsi *vsi, u32 tc,
  7739. struct i40e_cloud_filter *filter)
  7740. {
  7741. struct i40e_channel *ch, *ch_tmp;
  7742. /* direct to a traffic class on the same device */
  7743. if (tc == 0) {
  7744. filter->seid = vsi->seid;
  7745. return 0;
  7746. } else if (vsi->tc_config.enabled_tc & BIT(tc)) {
  7747. if (!filter->dst_port) {
  7748. dev_err(&vsi->back->pdev->dev,
  7749. "Specify destination port to direct to traffic class that is not default\n");
  7750. return -EINVAL;
  7751. }
  7752. if (list_empty(&vsi->ch_list))
  7753. return -EINVAL;
  7754. list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list,
  7755. list) {
  7756. if (ch->seid == vsi->tc_seid_map[tc])
  7757. filter->seid = ch->seid;
  7758. }
  7759. return 0;
  7760. }
  7761. dev_err(&vsi->back->pdev->dev, "TC is not enabled\n");
  7762. return -EINVAL;
  7763. }
  7764. /**
  7765. * i40e_configure_clsflower - Configure tc flower filters
  7766. * @vsi: Pointer to VSI
  7767. * @cls_flower: Pointer to struct flow_cls_offload
  7768. *
  7769. **/
  7770. static int i40e_configure_clsflower(struct i40e_vsi *vsi,
  7771. struct flow_cls_offload *cls_flower)
  7772. {
  7773. int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
  7774. struct i40e_cloud_filter *filter = NULL;
  7775. struct i40e_pf *pf = vsi->back;
  7776. int err = 0;
  7777. if (tc < 0) {
  7778. dev_err(&vsi->back->pdev->dev, "Invalid traffic class\n");
  7779. return -EOPNOTSUPP;
  7780. }
  7781. if (!tc) {
  7782. dev_err(&pf->pdev->dev, "Unable to add filter because of invalid destination");
  7783. return -EINVAL;
  7784. }
  7785. if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
  7786. test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
  7787. return -EBUSY;
  7788. if (pf->fdir_pf_active_filters ||
  7789. (!hlist_empty(&pf->fdir_filter_list))) {
  7790. dev_err(&vsi->back->pdev->dev,
  7791. "Flow Director Sideband filters exists, turn ntuple off to configure cloud filters\n");
  7792. return -EINVAL;
  7793. }
  7794. if (test_bit(I40E_FLAG_FD_SB_ENA, vsi->back->flags)) {
  7795. dev_err(&vsi->back->pdev->dev,
  7796. "Disable Flow Director Sideband, configuring Cloud filters via tc-flower\n");
  7797. clear_bit(I40E_FLAG_FD_SB_ENA, vsi->back->flags);
  7798. clear_bit(I40E_FLAG_FD_SB_TO_CLOUD_FILTER, vsi->back->flags);
  7799. }
  7800. filter = kzalloc(sizeof(*filter), GFP_KERNEL);
  7801. if (!filter)
  7802. return -ENOMEM;
  7803. filter->cookie = cls_flower->cookie;
  7804. err = i40e_parse_cls_flower(vsi, cls_flower, filter);
  7805. if (err < 0)
  7806. goto err;
  7807. err = i40e_handle_tclass(vsi, tc, filter);
  7808. if (err < 0)
  7809. goto err;
  7810. /* Add cloud filter */
  7811. if (filter->dst_port)
  7812. err = i40e_add_del_cloud_filter_big_buf(vsi, filter, true);
  7813. else
  7814. err = i40e_add_del_cloud_filter(vsi, filter, true);
  7815. if (err) {
  7816. dev_err(&pf->pdev->dev, "Failed to add cloud filter, err %d\n",
  7817. err);
  7818. goto err;
  7819. }
  7820. /* add filter to the ordered list */
  7821. INIT_HLIST_NODE(&filter->cloud_node);
  7822. hlist_add_head(&filter->cloud_node, &pf->cloud_filter_list);
  7823. pf->num_cloud_filters++;
  7824. return err;
  7825. err:
  7826. kfree(filter);
  7827. return err;
  7828. }
  7829. /**
  7830. * i40e_find_cloud_filter - Find the could filter in the list
  7831. * @vsi: Pointer to VSI
  7832. * @cookie: filter specific cookie
  7833. *
  7834. **/
  7835. static struct i40e_cloud_filter *i40e_find_cloud_filter(struct i40e_vsi *vsi,
  7836. unsigned long *cookie)
  7837. {
  7838. struct i40e_cloud_filter *filter = NULL;
  7839. struct hlist_node *node2;
  7840. hlist_for_each_entry_safe(filter, node2,
  7841. &vsi->back->cloud_filter_list, cloud_node)
  7842. if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
  7843. return filter;
  7844. return NULL;
  7845. }
  7846. /**
  7847. * i40e_delete_clsflower - Remove tc flower filters
  7848. * @vsi: Pointer to VSI
  7849. * @cls_flower: Pointer to struct flow_cls_offload
  7850. *
  7851. **/
  7852. static int i40e_delete_clsflower(struct i40e_vsi *vsi,
  7853. struct flow_cls_offload *cls_flower)
  7854. {
  7855. struct i40e_cloud_filter *filter = NULL;
  7856. struct i40e_pf *pf = vsi->back;
  7857. int err = 0;
  7858. filter = i40e_find_cloud_filter(vsi, &cls_flower->cookie);
  7859. if (!filter)
  7860. return -EINVAL;
  7861. hash_del(&filter->cloud_node);
  7862. if (filter->dst_port)
  7863. err = i40e_add_del_cloud_filter_big_buf(vsi, filter, false);
  7864. else
  7865. err = i40e_add_del_cloud_filter(vsi, filter, false);
  7866. kfree(filter);
  7867. if (err) {
  7868. dev_err(&pf->pdev->dev,
  7869. "Failed to delete cloud filter, err %pe\n",
  7870. ERR_PTR(err));
  7871. return i40e_aq_rc_to_posix(err, pf->hw.aq.asq_last_status);
  7872. }
  7873. pf->num_cloud_filters--;
  7874. if (!pf->num_cloud_filters)
  7875. if (test_bit(I40E_FLAG_FD_SB_TO_CLOUD_FILTER, pf->flags) &&
  7876. !test_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags)) {
  7877. set_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
  7878. clear_bit(I40E_FLAG_FD_SB_TO_CLOUD_FILTER, pf->flags);
  7879. clear_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
  7880. }
  7881. return 0;
  7882. }
  7883. /**
  7884. * i40e_setup_tc_cls_flower - flower classifier offloads
  7885. * @np: net device to configure
  7886. * @cls_flower: offload data
  7887. **/
  7888. static int i40e_setup_tc_cls_flower(struct i40e_netdev_priv *np,
  7889. struct flow_cls_offload *cls_flower)
  7890. {
  7891. struct i40e_vsi *vsi = np->vsi;
  7892. switch (cls_flower->command) {
  7893. case FLOW_CLS_REPLACE:
  7894. return i40e_configure_clsflower(vsi, cls_flower);
  7895. case FLOW_CLS_DESTROY:
  7896. return i40e_delete_clsflower(vsi, cls_flower);
  7897. case FLOW_CLS_STATS:
  7898. return -EOPNOTSUPP;
  7899. default:
  7900. return -EOPNOTSUPP;
  7901. }
  7902. }
  7903. static int i40e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
  7904. void *cb_priv)
  7905. {
  7906. struct i40e_netdev_priv *np = cb_priv;
  7907. if (!tc_cls_can_offload_and_chain0(np->vsi->netdev, type_data))
  7908. return -EOPNOTSUPP;
  7909. switch (type) {
  7910. case TC_SETUP_CLSFLOWER:
  7911. return i40e_setup_tc_cls_flower(np, type_data);
  7912. default:
  7913. return -EOPNOTSUPP;
  7914. }
  7915. }
  7916. static LIST_HEAD(i40e_block_cb_list);
  7917. static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
  7918. void *type_data)
  7919. {
  7920. struct i40e_netdev_priv *np = netdev_priv(netdev);
  7921. switch (type) {
  7922. case TC_SETUP_QDISC_MQPRIO:
  7923. return i40e_setup_tc(netdev, type_data);
  7924. case TC_SETUP_BLOCK:
  7925. return flow_block_cb_setup_simple(type_data,
  7926. &i40e_block_cb_list,
  7927. i40e_setup_tc_block_cb,
  7928. np, np, true);
  7929. default:
  7930. return -EOPNOTSUPP;
  7931. }
  7932. }
  7933. /**
  7934. * i40e_open - Called when a network interface is made active
  7935. * @netdev: network interface device structure
  7936. *
  7937. * The open entry point is called when a network interface is made
  7938. * active by the system (IFF_UP). At this point all resources needed
  7939. * for transmit and receive operations are allocated, the interrupt
  7940. * handler is registered with the OS, the netdev watchdog subtask is
  7941. * enabled, and the stack is notified that the interface is ready.
  7942. *
  7943. * Returns 0 on success, negative value on failure
  7944. **/
  7945. int i40e_open(struct net_device *netdev)
  7946. {
  7947. struct i40e_netdev_priv *np = netdev_priv(netdev);
  7948. struct i40e_vsi *vsi = np->vsi;
  7949. struct i40e_pf *pf = vsi->back;
  7950. int err;
  7951. /* disallow open during test or if eeprom is broken */
  7952. if (test_bit(__I40E_TESTING, pf->state) ||
  7953. test_bit(__I40E_BAD_EEPROM, pf->state))
  7954. return -EBUSY;
  7955. netif_carrier_off(netdev);
  7956. if (i40e_force_link_state(pf, true))
  7957. return -EAGAIN;
  7958. err = i40e_vsi_open(vsi);
  7959. if (err)
  7960. return err;
  7961. /* configure global TSO hardware offload settings */
  7962. wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
  7963. TCP_FLAG_FIN) >> 16);
  7964. wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
  7965. TCP_FLAG_FIN |
  7966. TCP_FLAG_CWR) >> 16);
  7967. wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
  7968. udp_tunnel_get_rx_info(netdev);
  7969. return 0;
  7970. }
  7971. /**
  7972. * i40e_netif_set_realnum_tx_rx_queues - Update number of tx/rx queues
  7973. * @vsi: vsi structure
  7974. *
  7975. * This updates netdev's number of tx/rx queues
  7976. *
  7977. * Returns status of setting tx/rx queues
  7978. **/
  7979. static int i40e_netif_set_realnum_tx_rx_queues(struct i40e_vsi *vsi)
  7980. {
  7981. int ret;
  7982. ret = netif_set_real_num_rx_queues(vsi->netdev,
  7983. vsi->num_queue_pairs);
  7984. if (ret)
  7985. return ret;
  7986. return netif_set_real_num_tx_queues(vsi->netdev,
  7987. vsi->num_queue_pairs);
  7988. }
  7989. /**
  7990. * i40e_vsi_open -
  7991. * @vsi: the VSI to open
  7992. *
  7993. * Finish initialization of the VSI.
  7994. *
  7995. * Returns 0 on success, negative value on failure
  7996. *
  7997. * Note: expects to be called while under rtnl_lock()
  7998. **/
  7999. int i40e_vsi_open(struct i40e_vsi *vsi)
  8000. {
  8001. struct i40e_pf *pf = vsi->back;
  8002. char int_name[I40E_INT_NAME_STR_LEN];
  8003. int err;
  8004. /* allocate descriptors */
  8005. err = i40e_vsi_setup_tx_resources(vsi);
  8006. if (err)
  8007. goto err_setup_tx;
  8008. err = i40e_vsi_setup_rx_resources(vsi);
  8009. if (err)
  8010. goto err_setup_rx;
  8011. err = i40e_vsi_configure(vsi);
  8012. if (err)
  8013. goto err_setup_rx;
  8014. if (vsi->netdev) {
  8015. snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
  8016. dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
  8017. err = i40e_vsi_request_irq(vsi, int_name);
  8018. if (err)
  8019. goto err_setup_rx;
  8020. /* Notify the stack of the actual queue counts. */
  8021. err = i40e_netif_set_realnum_tx_rx_queues(vsi);
  8022. if (err)
  8023. goto err_set_queues;
  8024. } else if (vsi->type == I40E_VSI_FDIR) {
  8025. snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
  8026. dev_driver_string(&pf->pdev->dev),
  8027. dev_name(&pf->pdev->dev));
  8028. err = i40e_vsi_request_irq(vsi, int_name);
  8029. if (err)
  8030. goto err_setup_rx;
  8031. } else {
  8032. err = -EINVAL;
  8033. goto err_setup_rx;
  8034. }
  8035. err = i40e_up_complete(vsi);
  8036. if (err)
  8037. goto err_up_complete;
  8038. return 0;
  8039. err_up_complete:
  8040. i40e_down(vsi);
  8041. err_set_queues:
  8042. i40e_vsi_free_irq(vsi);
  8043. err_setup_rx:
  8044. i40e_vsi_free_rx_resources(vsi);
  8045. err_setup_tx:
  8046. i40e_vsi_free_tx_resources(vsi);
  8047. if (vsi->type == I40E_VSI_MAIN)
  8048. i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
  8049. return err;
  8050. }
  8051. /**
  8052. * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
  8053. * @pf: Pointer to PF
  8054. *
  8055. * This function destroys the hlist where all the Flow Director
  8056. * filters were saved.
  8057. **/
  8058. static void i40e_fdir_filter_exit(struct i40e_pf *pf)
  8059. {
  8060. struct i40e_fdir_filter *filter;
  8061. struct i40e_flex_pit *pit_entry, *tmp;
  8062. struct hlist_node *node2;
  8063. hlist_for_each_entry_safe(filter, node2,
  8064. &pf->fdir_filter_list, fdir_node) {
  8065. hlist_del(&filter->fdir_node);
  8066. kfree(filter);
  8067. }
  8068. list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
  8069. list_del(&pit_entry->list);
  8070. kfree(pit_entry);
  8071. }
  8072. INIT_LIST_HEAD(&pf->l3_flex_pit_list);
  8073. list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
  8074. list_del(&pit_entry->list);
  8075. kfree(pit_entry);
  8076. }
  8077. INIT_LIST_HEAD(&pf->l4_flex_pit_list);
  8078. pf->fdir_pf_active_filters = 0;
  8079. i40e_reset_fdir_filter_cnt(pf);
  8080. /* Reprogram the default input set for TCP/IPv4 */
  8081. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
  8082. I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
  8083. I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
  8084. /* Reprogram the default input set for TCP/IPv6 */
  8085. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_TCP,
  8086. I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
  8087. I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
  8088. /* Reprogram the default input set for UDP/IPv4 */
  8089. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
  8090. I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
  8091. I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
  8092. /* Reprogram the default input set for UDP/IPv6 */
  8093. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_UDP,
  8094. I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
  8095. I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
  8096. /* Reprogram the default input set for SCTP/IPv4 */
  8097. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
  8098. I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
  8099. I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
  8100. /* Reprogram the default input set for SCTP/IPv6 */
  8101. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_SCTP,
  8102. I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
  8103. I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
  8104. /* Reprogram the default input set for Other/IPv4 */
  8105. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
  8106. I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
  8107. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV4,
  8108. I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
  8109. /* Reprogram the default input set for Other/IPv6 */
  8110. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_OTHER,
  8111. I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
  8112. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV6,
  8113. I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
  8114. }
  8115. /**
  8116. * i40e_cloud_filter_exit - Cleans up the cloud filters
  8117. * @pf: Pointer to PF
  8118. *
  8119. * This function destroys the hlist where all the cloud filters
  8120. * were saved.
  8121. **/
  8122. static void i40e_cloud_filter_exit(struct i40e_pf *pf)
  8123. {
  8124. struct i40e_cloud_filter *cfilter;
  8125. struct hlist_node *node;
  8126. hlist_for_each_entry_safe(cfilter, node,
  8127. &pf->cloud_filter_list, cloud_node) {
  8128. hlist_del(&cfilter->cloud_node);
  8129. kfree(cfilter);
  8130. }
  8131. pf->num_cloud_filters = 0;
  8132. if (test_bit(I40E_FLAG_FD_SB_TO_CLOUD_FILTER, pf->flags) &&
  8133. !test_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags)) {
  8134. set_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
  8135. clear_bit(I40E_FLAG_FD_SB_TO_CLOUD_FILTER, pf->flags);
  8136. clear_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
  8137. }
  8138. }
  8139. /**
  8140. * i40e_close - Disables a network interface
  8141. * @netdev: network interface device structure
  8142. *
  8143. * The close entry point is called when an interface is de-activated
  8144. * by the OS. The hardware is still under the driver's control, but
  8145. * this netdev interface is disabled.
  8146. *
  8147. * Returns 0, this is not allowed to fail
  8148. **/
  8149. int i40e_close(struct net_device *netdev)
  8150. {
  8151. struct i40e_netdev_priv *np = netdev_priv(netdev);
  8152. struct i40e_vsi *vsi = np->vsi;
  8153. i40e_vsi_close(vsi);
  8154. return 0;
  8155. }
  8156. /**
  8157. * i40e_do_reset - Start a PF or Core Reset sequence
  8158. * @pf: board private structure
  8159. * @reset_flags: which reset is requested
  8160. * @lock_acquired: indicates whether or not the lock has been acquired
  8161. * before this function was called.
  8162. *
  8163. * The essential difference in resets is that the PF Reset
  8164. * doesn't clear the packet buffers, doesn't reset the PE
  8165. * firmware, and doesn't bother the other PFs on the chip.
  8166. **/
  8167. void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
  8168. {
  8169. struct i40e_vsi *vsi;
  8170. u32 val;
  8171. int i;
  8172. /* do the biggest reset indicated */
  8173. if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
  8174. /* Request a Global Reset
  8175. *
  8176. * This will start the chip's countdown to the actual full
  8177. * chip reset event, and a warning interrupt to be sent
  8178. * to all PFs, including the requestor. Our handler
  8179. * for the warning interrupt will deal with the shutdown
  8180. * and recovery of the switch setup.
  8181. */
  8182. dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
  8183. val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  8184. val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
  8185. wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
  8186. } else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
  8187. /* Request a Core Reset
  8188. *
  8189. * Same as Global Reset, except does *not* include the MAC/PHY
  8190. */
  8191. dev_dbg(&pf->pdev->dev, "CoreR requested\n");
  8192. val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  8193. val |= I40E_GLGEN_RTRIG_CORER_MASK;
  8194. wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
  8195. i40e_flush(&pf->hw);
  8196. } else if (reset_flags & I40E_PF_RESET_FLAG) {
  8197. /* Request a PF Reset
  8198. *
  8199. * Resets only the PF-specific registers
  8200. *
  8201. * This goes directly to the tear-down and rebuild of
  8202. * the switch, since we need to do all the recovery as
  8203. * for the Core Reset.
  8204. */
  8205. dev_dbg(&pf->pdev->dev, "PFR requested\n");
  8206. i40e_handle_reset_warning(pf, lock_acquired);
  8207. } else if (reset_flags & I40E_PF_RESET_AND_REBUILD_FLAG) {
  8208. /* Request a PF Reset
  8209. *
  8210. * Resets PF and reinitializes PFs VSI.
  8211. */
  8212. i40e_prep_for_reset(pf);
  8213. i40e_reset_and_rebuild(pf, true, lock_acquired);
  8214. dev_info(&pf->pdev->dev,
  8215. test_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags) ?
  8216. "FW LLDP is disabled\n" :
  8217. "FW LLDP is enabled\n");
  8218. } else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
  8219. /* Find the VSI(s) that requested a re-init */
  8220. dev_info(&pf->pdev->dev, "VSI reinit requested\n");
  8221. i40e_pf_for_each_vsi(pf, i, vsi) {
  8222. if (test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
  8223. vsi->state))
  8224. i40e_vsi_reinit_locked(vsi);
  8225. }
  8226. } else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
  8227. /* Find the VSI(s) that needs to be brought down */
  8228. dev_info(&pf->pdev->dev, "VSI down requested\n");
  8229. i40e_pf_for_each_vsi(pf, i, vsi) {
  8230. if (test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
  8231. vsi->state)) {
  8232. set_bit(__I40E_VSI_DOWN, vsi->state);
  8233. i40e_down(vsi);
  8234. }
  8235. }
  8236. } else {
  8237. dev_info(&pf->pdev->dev,
  8238. "bad reset request 0x%08x\n", reset_flags);
  8239. }
  8240. }
  8241. #ifdef CONFIG_I40E_DCB
  8242. /**
  8243. * i40e_dcb_need_reconfig - Check if DCB needs reconfig
  8244. * @pf: board private structure
  8245. * @old_cfg: current DCB config
  8246. * @new_cfg: new DCB config
  8247. **/
  8248. bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
  8249. struct i40e_dcbx_config *old_cfg,
  8250. struct i40e_dcbx_config *new_cfg)
  8251. {
  8252. bool need_reconfig = false;
  8253. /* Check if ETS configuration has changed */
  8254. if (memcmp(&new_cfg->etscfg,
  8255. &old_cfg->etscfg,
  8256. sizeof(new_cfg->etscfg))) {
  8257. /* If Priority Table has changed reconfig is needed */
  8258. if (memcmp(&new_cfg->etscfg.prioritytable,
  8259. &old_cfg->etscfg.prioritytable,
  8260. sizeof(new_cfg->etscfg.prioritytable))) {
  8261. need_reconfig = true;
  8262. dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
  8263. }
  8264. if (memcmp(&new_cfg->etscfg.tcbwtable,
  8265. &old_cfg->etscfg.tcbwtable,
  8266. sizeof(new_cfg->etscfg.tcbwtable)))
  8267. dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
  8268. if (memcmp(&new_cfg->etscfg.tsatable,
  8269. &old_cfg->etscfg.tsatable,
  8270. sizeof(new_cfg->etscfg.tsatable)))
  8271. dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
  8272. }
  8273. /* Check if PFC configuration has changed */
  8274. if (memcmp(&new_cfg->pfc,
  8275. &old_cfg->pfc,
  8276. sizeof(new_cfg->pfc))) {
  8277. need_reconfig = true;
  8278. dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
  8279. }
  8280. /* Check if APP Table has changed */
  8281. if (memcmp(&new_cfg->app,
  8282. &old_cfg->app,
  8283. sizeof(new_cfg->app))) {
  8284. need_reconfig = true;
  8285. dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
  8286. }
  8287. dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
  8288. return need_reconfig;
  8289. }
  8290. /**
  8291. * i40e_handle_lldp_event - Handle LLDP Change MIB event
  8292. * @pf: board private structure
  8293. * @e: event info posted on ARQ
  8294. **/
  8295. static int i40e_handle_lldp_event(struct i40e_pf *pf,
  8296. struct i40e_arq_event_info *e)
  8297. {
  8298. struct i40e_aqc_lldp_get_mib *mib =
  8299. (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
  8300. struct i40e_hw *hw = &pf->hw;
  8301. struct i40e_dcbx_config tmp_dcbx_cfg;
  8302. bool need_reconfig = false;
  8303. int ret = 0;
  8304. u8 type;
  8305. /* X710-T*L 2.5G and 5G speeds don't support DCB */
  8306. if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
  8307. (hw->phy.link_info.link_speed &
  8308. ~(I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB)) &&
  8309. !test_bit(I40E_FLAG_DCB_CAPABLE, pf->flags))
  8310. /* let firmware decide if the DCB should be disabled */
  8311. set_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
  8312. /* Not DCB capable or capability disabled */
  8313. if (!test_bit(I40E_FLAG_DCB_CAPABLE, pf->flags))
  8314. return ret;
  8315. /* Ignore if event is not for Nearest Bridge */
  8316. type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
  8317. & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
  8318. dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
  8319. if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
  8320. return ret;
  8321. /* Check MIB Type and return if event for Remote MIB update */
  8322. type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
  8323. dev_dbg(&pf->pdev->dev,
  8324. "LLDP event mib type %s\n", type ? "remote" : "local");
  8325. if (type == I40E_AQ_LLDP_MIB_REMOTE) {
  8326. /* Update the remote cached instance and return */
  8327. ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
  8328. I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
  8329. &hw->remote_dcbx_config);
  8330. goto exit;
  8331. }
  8332. /* Store the old configuration */
  8333. tmp_dcbx_cfg = hw->local_dcbx_config;
  8334. /* Reset the old DCBx configuration data */
  8335. memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
  8336. /* Get updated DCBX data from firmware */
  8337. ret = i40e_get_dcb_config(&pf->hw);
  8338. if (ret) {
  8339. /* X710-T*L 2.5G and 5G speeds don't support DCB */
  8340. if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
  8341. (hw->phy.link_info.link_speed &
  8342. (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
  8343. dev_warn(&pf->pdev->dev,
  8344. "DCB is not supported for X710-T*L 2.5/5G speeds\n");
  8345. clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
  8346. } else {
  8347. dev_info(&pf->pdev->dev,
  8348. "Failed querying DCB configuration data from firmware, err %pe aq_err %s\n",
  8349. ERR_PTR(ret),
  8350. i40e_aq_str(&pf->hw,
  8351. pf->hw.aq.asq_last_status));
  8352. }
  8353. goto exit;
  8354. }
  8355. /* No change detected in DCBX configs */
  8356. if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
  8357. sizeof(tmp_dcbx_cfg))) {
  8358. dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
  8359. goto exit;
  8360. }
  8361. need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
  8362. &hw->local_dcbx_config);
  8363. i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
  8364. if (!need_reconfig)
  8365. goto exit;
  8366. /* Enable DCB tagging only when more than one TC */
  8367. if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
  8368. set_bit(I40E_FLAG_DCB_ENA, pf->flags);
  8369. else
  8370. clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
  8371. set_bit(__I40E_PORT_SUSPENDED, pf->state);
  8372. /* Reconfiguration needed quiesce all VSIs */
  8373. i40e_pf_quiesce_all_vsi(pf);
  8374. /* Changes in configuration update VEB/VSI */
  8375. i40e_dcb_reconfigure(pf);
  8376. ret = i40e_resume_port_tx(pf);
  8377. clear_bit(__I40E_PORT_SUSPENDED, pf->state);
  8378. /* In case of error no point in resuming VSIs */
  8379. if (ret)
  8380. goto exit;
  8381. /* Wait for the PF's queues to be disabled */
  8382. ret = i40e_pf_wait_queues_disabled(pf);
  8383. if (ret) {
  8384. /* Schedule PF reset to recover */
  8385. set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
  8386. i40e_service_event_schedule(pf);
  8387. } else {
  8388. i40e_pf_unquiesce_all_vsi(pf);
  8389. set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
  8390. set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
  8391. }
  8392. exit:
  8393. return ret;
  8394. }
  8395. #endif /* CONFIG_I40E_DCB */
  8396. /**
  8397. * i40e_do_reset_safe - Protected reset path for userland calls.
  8398. * @pf: board private structure
  8399. * @reset_flags: which reset is requested
  8400. *
  8401. **/
  8402. void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
  8403. {
  8404. rtnl_lock();
  8405. i40e_do_reset(pf, reset_flags, true);
  8406. rtnl_unlock();
  8407. }
  8408. /**
  8409. * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
  8410. * @pf: board private structure
  8411. * @e: event info posted on ARQ
  8412. *
  8413. * Handler for LAN Queue Overflow Event generated by the firmware for PF
  8414. * and VF queues
  8415. **/
  8416. static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
  8417. struct i40e_arq_event_info *e)
  8418. {
  8419. struct i40e_aqc_lan_overflow *data =
  8420. (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
  8421. u32 queue = le32_to_cpu(data->prtdcb_rupto);
  8422. u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
  8423. struct i40e_hw *hw = &pf->hw;
  8424. struct i40e_vf *vf;
  8425. u16 vf_id;
  8426. dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
  8427. queue, qtx_ctl);
  8428. if (FIELD_GET(I40E_QTX_CTL_PFVF_Q_MASK, qtx_ctl) !=
  8429. I40E_QTX_CTL_VF_QUEUE)
  8430. return;
  8431. /* Queue belongs to VF, find the VF and issue VF reset */
  8432. vf_id = FIELD_GET(I40E_QTX_CTL_VFVM_INDX_MASK, qtx_ctl);
  8433. vf_id -= hw->func_caps.vf_base_id;
  8434. vf = &pf->vf[vf_id];
  8435. i40e_vc_notify_vf_reset(vf);
  8436. /* Allow VF to process pending reset notification */
  8437. msleep(20);
  8438. i40e_reset_vf(vf, false);
  8439. }
  8440. /**
  8441. * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
  8442. * @pf: board private structure
  8443. **/
  8444. u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
  8445. {
  8446. u32 val, fcnt_prog;
  8447. val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
  8448. fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
  8449. return fcnt_prog;
  8450. }
  8451. /**
  8452. * i40e_get_current_fd_count - Get total FD filters programmed for this PF
  8453. * @pf: board private structure
  8454. **/
  8455. u32 i40e_get_current_fd_count(struct i40e_pf *pf)
  8456. {
  8457. u32 val, fcnt_prog;
  8458. val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
  8459. fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
  8460. FIELD_GET(I40E_PFQF_FDSTAT_BEST_CNT_MASK, val);
  8461. return fcnt_prog;
  8462. }
  8463. /**
  8464. * i40e_get_global_fd_count - Get total FD filters programmed on device
  8465. * @pf: board private structure
  8466. **/
  8467. u32 i40e_get_global_fd_count(struct i40e_pf *pf)
  8468. {
  8469. u32 val, fcnt_prog;
  8470. val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
  8471. fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
  8472. FIELD_GET(I40E_GLQF_FDCNT_0_BESTCNT_MASK, val);
  8473. return fcnt_prog;
  8474. }
  8475. /**
  8476. * i40e_reenable_fdir_sb - Restore FDir SB capability
  8477. * @pf: board private structure
  8478. **/
  8479. static void i40e_reenable_fdir_sb(struct i40e_pf *pf)
  8480. {
  8481. if (test_and_clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
  8482. if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags) &&
  8483. (I40E_DEBUG_FD & pf->hw.debug_mask))
  8484. dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
  8485. }
  8486. /**
  8487. * i40e_reenable_fdir_atr - Restore FDir ATR capability
  8488. * @pf: board private structure
  8489. **/
  8490. static void i40e_reenable_fdir_atr(struct i40e_pf *pf)
  8491. {
  8492. if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state)) {
  8493. /* ATR uses the same filtering logic as SB rules. It only
  8494. * functions properly if the input set mask is at the default
  8495. * settings. It is safe to restore the default input set
  8496. * because there are no active TCPv4 filter rules.
  8497. */
  8498. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
  8499. I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
  8500. I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
  8501. if (test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags) &&
  8502. (I40E_DEBUG_FD & pf->hw.debug_mask))
  8503. dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
  8504. }
  8505. }
  8506. /**
  8507. * i40e_delete_invalid_filter - Delete an invalid FDIR filter
  8508. * @pf: board private structure
  8509. * @filter: FDir filter to remove
  8510. */
  8511. static void i40e_delete_invalid_filter(struct i40e_pf *pf,
  8512. struct i40e_fdir_filter *filter)
  8513. {
  8514. /* Update counters */
  8515. pf->fdir_pf_active_filters--;
  8516. pf->fd_inv = 0;
  8517. switch (filter->flow_type) {
  8518. case TCP_V4_FLOW:
  8519. pf->fd_tcp4_filter_cnt--;
  8520. break;
  8521. case UDP_V4_FLOW:
  8522. pf->fd_udp4_filter_cnt--;
  8523. break;
  8524. case SCTP_V4_FLOW:
  8525. pf->fd_sctp4_filter_cnt--;
  8526. break;
  8527. case TCP_V6_FLOW:
  8528. pf->fd_tcp6_filter_cnt--;
  8529. break;
  8530. case UDP_V6_FLOW:
  8531. pf->fd_udp6_filter_cnt--;
  8532. break;
  8533. case SCTP_V6_FLOW:
  8534. pf->fd_udp6_filter_cnt--;
  8535. break;
  8536. case IP_USER_FLOW:
  8537. switch (filter->ipl4_proto) {
  8538. case IPPROTO_TCP:
  8539. pf->fd_tcp4_filter_cnt--;
  8540. break;
  8541. case IPPROTO_UDP:
  8542. pf->fd_udp4_filter_cnt--;
  8543. break;
  8544. case IPPROTO_SCTP:
  8545. pf->fd_sctp4_filter_cnt--;
  8546. break;
  8547. case IPPROTO_IP:
  8548. pf->fd_ip4_filter_cnt--;
  8549. break;
  8550. }
  8551. break;
  8552. case IPV6_USER_FLOW:
  8553. switch (filter->ipl4_proto) {
  8554. case IPPROTO_TCP:
  8555. pf->fd_tcp6_filter_cnt--;
  8556. break;
  8557. case IPPROTO_UDP:
  8558. pf->fd_udp6_filter_cnt--;
  8559. break;
  8560. case IPPROTO_SCTP:
  8561. pf->fd_sctp6_filter_cnt--;
  8562. break;
  8563. case IPPROTO_IP:
  8564. pf->fd_ip6_filter_cnt--;
  8565. break;
  8566. }
  8567. break;
  8568. }
  8569. /* Remove the filter from the list and free memory */
  8570. hlist_del(&filter->fdir_node);
  8571. kfree(filter);
  8572. }
  8573. /**
  8574. * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
  8575. * @pf: board private structure
  8576. **/
  8577. void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
  8578. {
  8579. struct i40e_fdir_filter *filter;
  8580. u32 fcnt_prog, fcnt_avail;
  8581. struct hlist_node *node;
  8582. if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
  8583. return;
  8584. /* Check if we have enough room to re-enable FDir SB capability. */
  8585. fcnt_prog = i40e_get_global_fd_count(pf);
  8586. fcnt_avail = pf->fdir_pf_filter_count;
  8587. if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
  8588. (pf->fd_add_err == 0) ||
  8589. (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt))
  8590. i40e_reenable_fdir_sb(pf);
  8591. /* We should wait for even more space before re-enabling ATR.
  8592. * Additionally, we cannot enable ATR as long as we still have TCP SB
  8593. * rules active.
  8594. */
  8595. if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
  8596. pf->fd_tcp4_filter_cnt == 0 && pf->fd_tcp6_filter_cnt == 0)
  8597. i40e_reenable_fdir_atr(pf);
  8598. /* if hw had a problem adding a filter, delete it */
  8599. if (pf->fd_inv > 0) {
  8600. hlist_for_each_entry_safe(filter, node,
  8601. &pf->fdir_filter_list, fdir_node)
  8602. if (filter->fd_id == pf->fd_inv)
  8603. i40e_delete_invalid_filter(pf, filter);
  8604. }
  8605. }
  8606. #define I40E_MIN_FD_FLUSH_INTERVAL 10
  8607. #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
  8608. /**
  8609. * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
  8610. * @pf: board private structure
  8611. **/
  8612. static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
  8613. {
  8614. unsigned long min_flush_time;
  8615. int flush_wait_retry = 50;
  8616. bool disable_atr = false;
  8617. int fd_room;
  8618. int reg;
  8619. if (!time_after(jiffies, pf->fd_flush_timestamp +
  8620. (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
  8621. return;
  8622. /* If the flush is happening too quick and we have mostly SB rules we
  8623. * should not re-enable ATR for some time.
  8624. */
  8625. min_flush_time = pf->fd_flush_timestamp +
  8626. (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
  8627. fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
  8628. if (!(time_after(jiffies, min_flush_time)) &&
  8629. (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
  8630. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  8631. dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
  8632. disable_atr = true;
  8633. }
  8634. pf->fd_flush_timestamp = jiffies;
  8635. set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
  8636. /* flush all filters */
  8637. wr32(&pf->hw, I40E_PFQF_CTL_1,
  8638. I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
  8639. i40e_flush(&pf->hw);
  8640. pf->fd_flush_cnt++;
  8641. pf->fd_add_err = 0;
  8642. do {
  8643. /* Check FD flush status every 5-6msec */
  8644. usleep_range(5000, 6000);
  8645. reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
  8646. if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
  8647. break;
  8648. } while (flush_wait_retry--);
  8649. if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
  8650. dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
  8651. } else {
  8652. /* replay sideband filters */
  8653. i40e_fdir_filter_restore(i40e_pf_get_main_vsi(pf));
  8654. if (!disable_atr && !pf->fd_tcp4_filter_cnt)
  8655. clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
  8656. clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
  8657. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  8658. dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
  8659. }
  8660. }
  8661. /**
  8662. * i40e_get_current_atr_cnt - Get the count of total FD ATR filters programmed
  8663. * @pf: board private structure
  8664. **/
  8665. u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
  8666. {
  8667. return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
  8668. }
  8669. /**
  8670. * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
  8671. * @pf: board private structure
  8672. **/
  8673. static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
  8674. {
  8675. /* if interface is down do nothing */
  8676. if (test_bit(__I40E_DOWN, pf->state))
  8677. return;
  8678. if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
  8679. i40e_fdir_flush_and_replay(pf);
  8680. i40e_fdir_check_and_reenable(pf);
  8681. }
  8682. /**
  8683. * i40e_vsi_link_event - notify VSI of a link event
  8684. * @vsi: vsi to be notified
  8685. * @link_up: link up or down
  8686. **/
  8687. static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
  8688. {
  8689. if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
  8690. return;
  8691. switch (vsi->type) {
  8692. case I40E_VSI_MAIN:
  8693. if (!vsi->netdev || !vsi->netdev_registered)
  8694. break;
  8695. if (link_up) {
  8696. netif_carrier_on(vsi->netdev);
  8697. netif_tx_wake_all_queues(vsi->netdev);
  8698. } else {
  8699. netif_carrier_off(vsi->netdev);
  8700. netif_tx_stop_all_queues(vsi->netdev);
  8701. }
  8702. break;
  8703. case I40E_VSI_SRIOV:
  8704. case I40E_VSI_VMDQ2:
  8705. case I40E_VSI_CTRL:
  8706. case I40E_VSI_IWARP:
  8707. case I40E_VSI_MIRROR:
  8708. default:
  8709. /* there is no notification for other VSIs */
  8710. break;
  8711. }
  8712. }
  8713. /**
  8714. * i40e_veb_link_event - notify elements on the veb of a link event
  8715. * @veb: veb to be notified
  8716. * @link_up: link up or down
  8717. **/
  8718. static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
  8719. {
  8720. struct i40e_vsi *vsi;
  8721. struct i40e_pf *pf;
  8722. int i;
  8723. if (!veb || !veb->pf)
  8724. return;
  8725. pf = veb->pf;
  8726. /* Send link event to contained VSIs */
  8727. i40e_pf_for_each_vsi(pf, i, vsi)
  8728. if (vsi->uplink_seid == veb->seid)
  8729. i40e_vsi_link_event(vsi, link_up);
  8730. }
  8731. /**
  8732. * i40e_link_event - Update netif_carrier status
  8733. * @pf: board private structure
  8734. **/
  8735. static void i40e_link_event(struct i40e_pf *pf)
  8736. {
  8737. struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
  8738. struct i40e_veb *veb = i40e_pf_get_main_veb(pf);
  8739. u8 new_link_speed, old_link_speed;
  8740. bool new_link, old_link;
  8741. int status;
  8742. #ifdef CONFIG_I40E_DCB
  8743. int err;
  8744. #endif /* CONFIG_I40E_DCB */
  8745. /* set this to force the get_link_status call to refresh state */
  8746. pf->hw.phy.get_link_info = true;
  8747. old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
  8748. status = i40e_get_link_status(&pf->hw, &new_link);
  8749. /* On success, disable temp link polling */
  8750. if (status == 0) {
  8751. clear_bit(__I40E_TEMP_LINK_POLLING, pf->state);
  8752. } else {
  8753. /* Enable link polling temporarily until i40e_get_link_status
  8754. * returns 0
  8755. */
  8756. set_bit(__I40E_TEMP_LINK_POLLING, pf->state);
  8757. dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
  8758. status);
  8759. return;
  8760. }
  8761. old_link_speed = pf->hw.phy.link_info_old.link_speed;
  8762. new_link_speed = pf->hw.phy.link_info.link_speed;
  8763. if (new_link == old_link &&
  8764. new_link_speed == old_link_speed &&
  8765. (test_bit(__I40E_VSI_DOWN, vsi->state) ||
  8766. new_link == netif_carrier_ok(vsi->netdev)))
  8767. return;
  8768. i40e_print_link_message(vsi, new_link);
  8769. /* Notify the base of the switch tree connected to
  8770. * the link. Floating VEBs are not notified.
  8771. */
  8772. if (veb)
  8773. i40e_veb_link_event(veb, new_link);
  8774. else
  8775. i40e_vsi_link_event(vsi, new_link);
  8776. if (pf->vf)
  8777. i40e_vc_notify_link_state(pf);
  8778. if (test_bit(I40E_FLAG_PTP_ENA, pf->flags))
  8779. i40e_ptp_set_increment(pf);
  8780. #ifdef CONFIG_I40E_DCB
  8781. if (new_link == old_link)
  8782. return;
  8783. /* Not SW DCB so firmware will take care of default settings */
  8784. if (pf->dcbx_cap & DCB_CAP_DCBX_LLD_MANAGED)
  8785. return;
  8786. /* We cover here only link down, as after link up in case of SW DCB
  8787. * SW LLDP agent will take care of setting it up
  8788. */
  8789. if (!new_link) {
  8790. dev_dbg(&pf->pdev->dev, "Reconfig DCB to single TC as result of Link Down\n");
  8791. memset(&pf->tmp_cfg, 0, sizeof(pf->tmp_cfg));
  8792. err = i40e_dcb_sw_default_config(pf);
  8793. if (err) {
  8794. clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
  8795. clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
  8796. } else {
  8797. pf->dcbx_cap = DCB_CAP_DCBX_HOST |
  8798. DCB_CAP_DCBX_VER_IEEE;
  8799. set_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
  8800. clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
  8801. }
  8802. }
  8803. #endif /* CONFIG_I40E_DCB */
  8804. }
  8805. /**
  8806. * i40e_watchdog_subtask - periodic checks not using event driven response
  8807. * @pf: board private structure
  8808. **/
  8809. static void i40e_watchdog_subtask(struct i40e_pf *pf)
  8810. {
  8811. struct i40e_vsi *vsi;
  8812. struct i40e_veb *veb;
  8813. int i;
  8814. /* if interface is down do nothing */
  8815. if (test_bit(__I40E_DOWN, pf->state) ||
  8816. test_bit(__I40E_CONFIG_BUSY, pf->state))
  8817. return;
  8818. /* make sure we don't do these things too often */
  8819. if (time_before(jiffies, (pf->service_timer_previous +
  8820. pf->service_timer_period)))
  8821. return;
  8822. pf->service_timer_previous = jiffies;
  8823. if (test_bit(I40E_FLAG_LINK_POLLING_ENA, pf->flags) ||
  8824. test_bit(__I40E_TEMP_LINK_POLLING, pf->state))
  8825. i40e_link_event(pf);
  8826. /* Update the stats for active netdevs so the network stack
  8827. * can look at updated numbers whenever it cares to
  8828. */
  8829. i40e_pf_for_each_vsi(pf, i, vsi)
  8830. if (vsi->netdev)
  8831. i40e_update_stats(vsi);
  8832. if (test_bit(I40E_FLAG_VEB_STATS_ENA, pf->flags)) {
  8833. /* Update the stats for the active switching components */
  8834. i40e_pf_for_each_veb(pf, i, veb)
  8835. i40e_update_veb_stats(veb);
  8836. }
  8837. i40e_ptp_rx_hang(pf);
  8838. i40e_ptp_tx_hang(pf);
  8839. }
  8840. /**
  8841. * i40e_reset_subtask - Set up for resetting the device and driver
  8842. * @pf: board private structure
  8843. **/
  8844. static void i40e_reset_subtask(struct i40e_pf *pf)
  8845. {
  8846. u32 reset_flags = 0;
  8847. if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
  8848. reset_flags |= BIT(__I40E_REINIT_REQUESTED);
  8849. clear_bit(__I40E_REINIT_REQUESTED, pf->state);
  8850. }
  8851. if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
  8852. reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
  8853. clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
  8854. }
  8855. if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
  8856. reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
  8857. clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
  8858. }
  8859. if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
  8860. reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
  8861. clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
  8862. }
  8863. if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
  8864. reset_flags |= BIT(__I40E_DOWN_REQUESTED);
  8865. clear_bit(__I40E_DOWN_REQUESTED, pf->state);
  8866. }
  8867. /* If there's a recovery already waiting, it takes
  8868. * precedence before starting a new reset sequence.
  8869. */
  8870. if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
  8871. i40e_prep_for_reset(pf);
  8872. i40e_reset(pf);
  8873. i40e_rebuild(pf, false, false);
  8874. }
  8875. /* If we're already down or resetting, just bail */
  8876. if (reset_flags &&
  8877. !test_bit(__I40E_DOWN, pf->state) &&
  8878. !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
  8879. i40e_do_reset(pf, reset_flags, false);
  8880. }
  8881. }
  8882. /**
  8883. * i40e_handle_link_event - Handle link event
  8884. * @pf: board private structure
  8885. * @e: event info posted on ARQ
  8886. **/
  8887. static void i40e_handle_link_event(struct i40e_pf *pf,
  8888. struct i40e_arq_event_info *e)
  8889. {
  8890. struct i40e_aqc_get_link_status *status =
  8891. (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
  8892. /* Do a new status request to re-enable LSE reporting
  8893. * and load new status information into the hw struct
  8894. * This completely ignores any state information
  8895. * in the ARQ event info, instead choosing to always
  8896. * issue the AQ update link status command.
  8897. */
  8898. i40e_link_event(pf);
  8899. /* Check if module meets thermal requirements */
  8900. if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
  8901. dev_err(&pf->pdev->dev,
  8902. "Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n");
  8903. dev_err(&pf->pdev->dev,
  8904. "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
  8905. } else {
  8906. /* check for unqualified module, if link is down, suppress
  8907. * the message if link was forced to be down.
  8908. */
  8909. if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
  8910. (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
  8911. (!(status->link_info & I40E_AQ_LINK_UP)) &&
  8912. (!test_bit(I40E_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags))) {
  8913. dev_err(&pf->pdev->dev,
  8914. "Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n");
  8915. dev_err(&pf->pdev->dev,
  8916. "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
  8917. }
  8918. }
  8919. }
  8920. /**
  8921. * i40e_clean_adminq_subtask - Clean the AdminQ rings
  8922. * @pf: board private structure
  8923. **/
  8924. static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
  8925. {
  8926. struct i40e_arq_event_info event;
  8927. struct i40e_hw *hw = &pf->hw;
  8928. u16 pending, i = 0;
  8929. u16 opcode;
  8930. u32 oldval;
  8931. int ret;
  8932. u32 val;
  8933. /* Do not run clean AQ when PF reset fails */
  8934. if (test_bit(__I40E_RESET_FAILED, pf->state))
  8935. return;
  8936. /* check for error indications */
  8937. val = rd32(&pf->hw, I40E_PF_ARQLEN);
  8938. oldval = val;
  8939. if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
  8940. if (hw->debug_mask & I40E_DEBUG_AQ)
  8941. dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
  8942. val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
  8943. }
  8944. if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
  8945. if (hw->debug_mask & I40E_DEBUG_AQ)
  8946. dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
  8947. val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
  8948. pf->arq_overflows++;
  8949. }
  8950. if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
  8951. if (hw->debug_mask & I40E_DEBUG_AQ)
  8952. dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
  8953. val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
  8954. }
  8955. if (oldval != val)
  8956. wr32(&pf->hw, I40E_PF_ARQLEN, val);
  8957. val = rd32(&pf->hw, I40E_PF_ATQLEN);
  8958. oldval = val;
  8959. if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
  8960. if (pf->hw.debug_mask & I40E_DEBUG_AQ)
  8961. dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
  8962. val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
  8963. }
  8964. if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
  8965. if (pf->hw.debug_mask & I40E_DEBUG_AQ)
  8966. dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
  8967. val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
  8968. }
  8969. if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
  8970. if (pf->hw.debug_mask & I40E_DEBUG_AQ)
  8971. dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
  8972. val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
  8973. }
  8974. if (oldval != val)
  8975. wr32(&pf->hw, I40E_PF_ATQLEN, val);
  8976. event.buf_len = I40E_MAX_AQ_BUF_SIZE;
  8977. event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
  8978. if (!event.msg_buf)
  8979. return;
  8980. do {
  8981. ret = i40e_clean_arq_element(hw, &event, &pending);
  8982. if (ret == -EALREADY)
  8983. break;
  8984. else if (ret) {
  8985. dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
  8986. break;
  8987. }
  8988. opcode = le16_to_cpu(event.desc.opcode);
  8989. switch (opcode) {
  8990. case i40e_aqc_opc_get_link_status:
  8991. rtnl_lock();
  8992. i40e_handle_link_event(pf, &event);
  8993. rtnl_unlock();
  8994. break;
  8995. case i40e_aqc_opc_send_msg_to_pf:
  8996. ret = i40e_vc_process_vf_msg(pf,
  8997. le16_to_cpu(event.desc.retval),
  8998. le32_to_cpu(event.desc.cookie_high),
  8999. le32_to_cpu(event.desc.cookie_low),
  9000. event.msg_buf,
  9001. event.msg_len);
  9002. break;
  9003. case i40e_aqc_opc_lldp_update_mib:
  9004. dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
  9005. #ifdef CONFIG_I40E_DCB
  9006. rtnl_lock();
  9007. i40e_handle_lldp_event(pf, &event);
  9008. rtnl_unlock();
  9009. #endif /* CONFIG_I40E_DCB */
  9010. break;
  9011. case i40e_aqc_opc_event_lan_overflow:
  9012. dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
  9013. i40e_handle_lan_overflow_event(pf, &event);
  9014. break;
  9015. case i40e_aqc_opc_send_msg_to_peer:
  9016. dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
  9017. break;
  9018. case i40e_aqc_opc_nvm_erase:
  9019. case i40e_aqc_opc_nvm_update:
  9020. case i40e_aqc_opc_oem_post_update:
  9021. i40e_debug(&pf->hw, I40E_DEBUG_NVM,
  9022. "ARQ NVM operation 0x%04x completed\n",
  9023. opcode);
  9024. break;
  9025. default:
  9026. dev_info(&pf->pdev->dev,
  9027. "ARQ: Unknown event 0x%04x ignored\n",
  9028. opcode);
  9029. break;
  9030. }
  9031. } while (i++ < I40E_AQ_WORK_LIMIT);
  9032. if (i < I40E_AQ_WORK_LIMIT)
  9033. clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
  9034. /* re-enable Admin queue interrupt cause */
  9035. val = rd32(hw, I40E_PFINT_ICR0_ENA);
  9036. val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
  9037. wr32(hw, I40E_PFINT_ICR0_ENA, val);
  9038. i40e_flush(hw);
  9039. kfree(event.msg_buf);
  9040. }
  9041. /**
  9042. * i40e_verify_eeprom - make sure eeprom is good to use
  9043. * @pf: board private structure
  9044. **/
  9045. static void i40e_verify_eeprom(struct i40e_pf *pf)
  9046. {
  9047. int err;
  9048. err = i40e_diag_eeprom_test(&pf->hw);
  9049. if (err) {
  9050. /* retry in case of garbage read */
  9051. err = i40e_diag_eeprom_test(&pf->hw);
  9052. if (err) {
  9053. dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
  9054. err);
  9055. set_bit(__I40E_BAD_EEPROM, pf->state);
  9056. }
  9057. }
  9058. if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
  9059. dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
  9060. clear_bit(__I40E_BAD_EEPROM, pf->state);
  9061. }
  9062. }
  9063. /**
  9064. * i40e_enable_pf_switch_lb
  9065. * @pf: pointer to the PF structure
  9066. *
  9067. * enable switch loop back or die - no point in a return value
  9068. **/
  9069. static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
  9070. {
  9071. struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
  9072. struct i40e_vsi_context ctxt;
  9073. int ret;
  9074. ctxt.seid = pf->main_vsi_seid;
  9075. ctxt.pf_num = pf->hw.pf_id;
  9076. ctxt.vf_num = 0;
  9077. ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
  9078. if (ret) {
  9079. dev_info(&pf->pdev->dev,
  9080. "couldn't get PF vsi config, err %pe aq_err %s\n",
  9081. ERR_PTR(ret),
  9082. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  9083. return;
  9084. }
  9085. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  9086. ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  9087. ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  9088. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  9089. if (ret) {
  9090. dev_info(&pf->pdev->dev,
  9091. "update vsi switch failed, err %pe aq_err %s\n",
  9092. ERR_PTR(ret),
  9093. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  9094. }
  9095. }
  9096. /**
  9097. * i40e_disable_pf_switch_lb
  9098. * @pf: pointer to the PF structure
  9099. *
  9100. * disable switch loop back or die - no point in a return value
  9101. **/
  9102. static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
  9103. {
  9104. struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
  9105. struct i40e_vsi_context ctxt;
  9106. int ret;
  9107. ctxt.seid = pf->main_vsi_seid;
  9108. ctxt.pf_num = pf->hw.pf_id;
  9109. ctxt.vf_num = 0;
  9110. ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
  9111. if (ret) {
  9112. dev_info(&pf->pdev->dev,
  9113. "couldn't get PF vsi config, err %pe aq_err %s\n",
  9114. ERR_PTR(ret),
  9115. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  9116. return;
  9117. }
  9118. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  9119. ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  9120. ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  9121. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  9122. if (ret) {
  9123. dev_info(&pf->pdev->dev,
  9124. "update vsi switch failed, err %pe aq_err %s\n",
  9125. ERR_PTR(ret),
  9126. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  9127. }
  9128. }
  9129. /**
  9130. * i40e_config_bridge_mode - Configure the HW bridge mode
  9131. * @veb: pointer to the bridge instance
  9132. *
  9133. * Configure the loop back mode for the LAN VSI that is downlink to the
  9134. * specified HW bridge instance. It is expected this function is called
  9135. * when a new HW bridge is instantiated.
  9136. **/
  9137. static void i40e_config_bridge_mode(struct i40e_veb *veb)
  9138. {
  9139. struct i40e_pf *pf = veb->pf;
  9140. if (pf->hw.debug_mask & I40E_DEBUG_LAN)
  9141. dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
  9142. veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
  9143. if (veb->bridge_mode & BRIDGE_MODE_VEPA)
  9144. i40e_disable_pf_switch_lb(pf);
  9145. else
  9146. i40e_enable_pf_switch_lb(pf);
  9147. }
  9148. /**
  9149. * i40e_reconstitute_veb - rebuild the VEB and VSIs connected to it
  9150. * @veb: pointer to the VEB instance
  9151. *
  9152. * This is a function that builds the attached VSIs. We track the connections
  9153. * through our own index numbers because the seid's from the HW could change
  9154. * across the reset.
  9155. **/
  9156. static int i40e_reconstitute_veb(struct i40e_veb *veb)
  9157. {
  9158. struct i40e_vsi *ctl_vsi = NULL;
  9159. struct i40e_pf *pf = veb->pf;
  9160. struct i40e_vsi *vsi;
  9161. int v, ret;
  9162. /* As we do not maintain PV (port virtualizer) switch element then
  9163. * there can be only one non-floating VEB that have uplink to MAC SEID
  9164. * and its control VSI is the main one.
  9165. */
  9166. if (WARN_ON(veb->uplink_seid && veb->uplink_seid != pf->mac_seid)) {
  9167. dev_err(&pf->pdev->dev,
  9168. "Invalid uplink SEID for VEB %d\n", veb->idx);
  9169. return -ENOENT;
  9170. }
  9171. if (veb->uplink_seid == pf->mac_seid) {
  9172. /* Check that the LAN VSI has VEB owning flag set */
  9173. ctl_vsi = i40e_pf_get_main_vsi(pf);
  9174. if (WARN_ON(ctl_vsi->veb_idx != veb->idx ||
  9175. !(ctl_vsi->flags & I40E_VSI_FLAG_VEB_OWNER))) {
  9176. dev_err(&pf->pdev->dev,
  9177. "Invalid control VSI for VEB %d\n", veb->idx);
  9178. return -ENOENT;
  9179. }
  9180. /* Add the control VSI to switch */
  9181. ret = i40e_add_vsi(ctl_vsi);
  9182. if (ret) {
  9183. dev_err(&pf->pdev->dev,
  9184. "Rebuild of owner VSI for VEB %d failed: %d\n",
  9185. veb->idx, ret);
  9186. return ret;
  9187. }
  9188. i40e_vsi_reset_stats(ctl_vsi);
  9189. }
  9190. /* create the VEB in the switch and move the VSI onto the VEB */
  9191. ret = i40e_add_veb(veb, ctl_vsi);
  9192. if (ret)
  9193. return ret;
  9194. if (veb->uplink_seid) {
  9195. if (test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags))
  9196. veb->bridge_mode = BRIDGE_MODE_VEB;
  9197. else
  9198. veb->bridge_mode = BRIDGE_MODE_VEPA;
  9199. i40e_config_bridge_mode(veb);
  9200. }
  9201. /* create the remaining VSIs attached to this VEB */
  9202. i40e_pf_for_each_vsi(pf, v, vsi) {
  9203. if (vsi == ctl_vsi)
  9204. continue;
  9205. if (vsi->veb_idx == veb->idx) {
  9206. vsi->uplink_seid = veb->seid;
  9207. ret = i40e_add_vsi(vsi);
  9208. if (ret) {
  9209. dev_info(&pf->pdev->dev,
  9210. "rebuild of vsi_idx %d failed: %d\n",
  9211. v, ret);
  9212. return ret;
  9213. }
  9214. i40e_vsi_reset_stats(vsi);
  9215. }
  9216. }
  9217. return ret;
  9218. }
  9219. /**
  9220. * i40e_get_capabilities - get info about the HW
  9221. * @pf: the PF struct
  9222. * @list_type: AQ capability to be queried
  9223. **/
  9224. static int i40e_get_capabilities(struct i40e_pf *pf,
  9225. enum i40e_admin_queue_opc list_type)
  9226. {
  9227. struct i40e_aqc_list_capabilities_element_resp *cap_buf;
  9228. u16 data_size;
  9229. int buf_len;
  9230. int err;
  9231. buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
  9232. do {
  9233. cap_buf = kzalloc(buf_len, GFP_KERNEL);
  9234. if (!cap_buf)
  9235. return -ENOMEM;
  9236. /* this loads the data into the hw struct for us */
  9237. err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
  9238. &data_size, list_type,
  9239. NULL);
  9240. /* data loaded, buffer no longer needed */
  9241. kfree(cap_buf);
  9242. if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
  9243. /* retry with a larger buffer */
  9244. buf_len = data_size;
  9245. } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK || err) {
  9246. dev_info(&pf->pdev->dev,
  9247. "capability discovery failed, err %pe aq_err %s\n",
  9248. ERR_PTR(err),
  9249. i40e_aq_str(&pf->hw,
  9250. pf->hw.aq.asq_last_status));
  9251. return -ENODEV;
  9252. }
  9253. } while (err);
  9254. if (pf->hw.debug_mask & I40E_DEBUG_USER) {
  9255. if (list_type == i40e_aqc_opc_list_func_capabilities) {
  9256. dev_info(&pf->pdev->dev,
  9257. "pf=%d, num_vfs=%d, msix_pf=%d, msix_vf=%d, fd_g=%d, fd_b=%d, pf_max_q=%d num_vsi=%d\n",
  9258. pf->hw.pf_id, pf->hw.func_caps.num_vfs,
  9259. pf->hw.func_caps.num_msix_vectors,
  9260. pf->hw.func_caps.num_msix_vectors_vf,
  9261. pf->hw.func_caps.fd_filters_guaranteed,
  9262. pf->hw.func_caps.fd_filters_best_effort,
  9263. pf->hw.func_caps.num_tx_qp,
  9264. pf->hw.func_caps.num_vsis);
  9265. } else if (list_type == i40e_aqc_opc_list_dev_capabilities) {
  9266. dev_info(&pf->pdev->dev,
  9267. "switch_mode=0x%04x, function_valid=0x%08x\n",
  9268. pf->hw.dev_caps.switch_mode,
  9269. pf->hw.dev_caps.valid_functions);
  9270. dev_info(&pf->pdev->dev,
  9271. "SR-IOV=%d, num_vfs for all function=%u\n",
  9272. pf->hw.dev_caps.sr_iov_1_1,
  9273. pf->hw.dev_caps.num_vfs);
  9274. dev_info(&pf->pdev->dev,
  9275. "num_vsis=%u, num_rx:%u, num_tx=%u\n",
  9276. pf->hw.dev_caps.num_vsis,
  9277. pf->hw.dev_caps.num_rx_qp,
  9278. pf->hw.dev_caps.num_tx_qp);
  9279. }
  9280. }
  9281. if (list_type == i40e_aqc_opc_list_func_capabilities) {
  9282. #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
  9283. + pf->hw.func_caps.num_vfs)
  9284. if (pf->hw.revision_id == 0 &&
  9285. pf->hw.func_caps.num_vsis < DEF_NUM_VSI) {
  9286. dev_info(&pf->pdev->dev,
  9287. "got num_vsis %d, setting num_vsis to %d\n",
  9288. pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
  9289. pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
  9290. }
  9291. }
  9292. return 0;
  9293. }
  9294. static int i40e_vsi_clear(struct i40e_vsi *vsi);
  9295. /**
  9296. * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
  9297. * @pf: board private structure
  9298. **/
  9299. static void i40e_fdir_sb_setup(struct i40e_pf *pf)
  9300. {
  9301. struct i40e_vsi *main_vsi, *vsi;
  9302. /* quick workaround for an NVM issue that leaves a critical register
  9303. * uninitialized
  9304. */
  9305. if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
  9306. static const u32 hkey[] = {
  9307. 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
  9308. 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
  9309. 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
  9310. 0x95b3a76d};
  9311. int i;
  9312. for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
  9313. wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
  9314. }
  9315. if (!test_bit(I40E_FLAG_FD_SB_ENA, pf->flags))
  9316. return;
  9317. /* find existing VSI and see if it needs configuring */
  9318. vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
  9319. /* create a new VSI if none exists */
  9320. if (!vsi) {
  9321. main_vsi = i40e_pf_get_main_vsi(pf);
  9322. vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR, main_vsi->seid, 0);
  9323. if (!vsi) {
  9324. dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
  9325. clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
  9326. set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
  9327. return;
  9328. }
  9329. }
  9330. i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
  9331. }
  9332. /**
  9333. * i40e_fdir_teardown - release the Flow Director resources
  9334. * @pf: board private structure
  9335. **/
  9336. static void i40e_fdir_teardown(struct i40e_pf *pf)
  9337. {
  9338. struct i40e_vsi *vsi;
  9339. i40e_fdir_filter_exit(pf);
  9340. vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
  9341. if (vsi)
  9342. i40e_vsi_release(vsi);
  9343. }
  9344. /**
  9345. * i40e_rebuild_cloud_filters - Rebuilds cloud filters for VSIs
  9346. * @vsi: PF main vsi
  9347. * @seid: seid of main or channel VSIs
  9348. *
  9349. * Rebuilds cloud filters associated with main VSI and channel VSIs if they
  9350. * existed before reset
  9351. **/
  9352. static int i40e_rebuild_cloud_filters(struct i40e_vsi *vsi, u16 seid)
  9353. {
  9354. struct i40e_cloud_filter *cfilter;
  9355. struct i40e_pf *pf = vsi->back;
  9356. struct hlist_node *node;
  9357. int ret;
  9358. /* Add cloud filters back if they exist */
  9359. hlist_for_each_entry_safe(cfilter, node, &pf->cloud_filter_list,
  9360. cloud_node) {
  9361. if (cfilter->seid != seid)
  9362. continue;
  9363. if (cfilter->dst_port)
  9364. ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
  9365. true);
  9366. else
  9367. ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
  9368. if (ret) {
  9369. dev_dbg(&pf->pdev->dev,
  9370. "Failed to rebuild cloud filter, err %pe aq_err %s\n",
  9371. ERR_PTR(ret),
  9372. i40e_aq_str(&pf->hw,
  9373. pf->hw.aq.asq_last_status));
  9374. return ret;
  9375. }
  9376. }
  9377. return 0;
  9378. }
  9379. /**
  9380. * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset
  9381. * @vsi: PF main vsi
  9382. *
  9383. * Rebuilds channel VSIs if they existed before reset
  9384. **/
  9385. static int i40e_rebuild_channels(struct i40e_vsi *vsi)
  9386. {
  9387. struct i40e_channel *ch, *ch_tmp;
  9388. int ret;
  9389. if (list_empty(&vsi->ch_list))
  9390. return 0;
  9391. list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
  9392. if (!ch->initialized)
  9393. break;
  9394. /* Proceed with creation of channel (VMDq2) VSI */
  9395. ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch);
  9396. if (ret) {
  9397. dev_info(&vsi->back->pdev->dev,
  9398. "failed to rebuild channels using uplink_seid %u\n",
  9399. vsi->uplink_seid);
  9400. return ret;
  9401. }
  9402. /* Reconfigure TX queues using QTX_CTL register */
  9403. ret = i40e_channel_config_tx_ring(vsi->back, vsi, ch);
  9404. if (ret) {
  9405. dev_info(&vsi->back->pdev->dev,
  9406. "failed to configure TX rings for channel %u\n",
  9407. ch->seid);
  9408. return ret;
  9409. }
  9410. /* update 'next_base_queue' */
  9411. vsi->next_base_queue = vsi->next_base_queue +
  9412. ch->num_queue_pairs;
  9413. if (ch->max_tx_rate) {
  9414. u64 credits = ch->max_tx_rate;
  9415. if (i40e_set_bw_limit(vsi, ch->seid,
  9416. ch->max_tx_rate))
  9417. return -EINVAL;
  9418. do_div(credits, I40E_BW_CREDIT_DIVISOR);
  9419. dev_dbg(&vsi->back->pdev->dev,
  9420. "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
  9421. ch->max_tx_rate,
  9422. credits,
  9423. ch->seid);
  9424. }
  9425. ret = i40e_rebuild_cloud_filters(vsi, ch->seid);
  9426. if (ret) {
  9427. dev_dbg(&vsi->back->pdev->dev,
  9428. "Failed to rebuild cloud filters for channel VSI %u\n",
  9429. ch->seid);
  9430. return ret;
  9431. }
  9432. }
  9433. return 0;
  9434. }
  9435. /**
  9436. * i40e_clean_xps_state - clean xps state for every tx_ring
  9437. * @vsi: ptr to the VSI
  9438. **/
  9439. static void i40e_clean_xps_state(struct i40e_vsi *vsi)
  9440. {
  9441. int i;
  9442. if (vsi->tx_rings)
  9443. for (i = 0; i < vsi->num_queue_pairs; i++)
  9444. if (vsi->tx_rings[i])
  9445. clear_bit(__I40E_TX_XPS_INIT_DONE,
  9446. vsi->tx_rings[i]->state);
  9447. }
  9448. /**
  9449. * i40e_prep_for_reset - prep for the core to reset
  9450. * @pf: board private structure
  9451. *
  9452. * Close up the VFs and other things in prep for PF Reset.
  9453. **/
  9454. static void i40e_prep_for_reset(struct i40e_pf *pf)
  9455. {
  9456. struct i40e_hw *hw = &pf->hw;
  9457. struct i40e_vsi *vsi;
  9458. int ret = 0;
  9459. u32 v;
  9460. clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
  9461. if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
  9462. return;
  9463. if (i40e_check_asq_alive(&pf->hw))
  9464. i40e_vc_notify_reset(pf);
  9465. dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
  9466. /* quiesce the VSIs and their queues that are not already DOWN */
  9467. i40e_pf_quiesce_all_vsi(pf);
  9468. i40e_pf_for_each_vsi(pf, v, vsi) {
  9469. i40e_clean_xps_state(vsi);
  9470. vsi->seid = 0;
  9471. }
  9472. i40e_shutdown_adminq(&pf->hw);
  9473. /* call shutdown HMC */
  9474. if (hw->hmc.hmc_obj) {
  9475. ret = i40e_shutdown_lan_hmc(hw);
  9476. if (ret)
  9477. dev_warn(&pf->pdev->dev,
  9478. "shutdown_lan_hmc failed: %d\n", ret);
  9479. }
  9480. /* Save the current PTP time so that we can restore the time after the
  9481. * reset completes.
  9482. */
  9483. i40e_ptp_save_hw_time(pf);
  9484. }
  9485. /**
  9486. * i40e_send_version - update firmware with driver version
  9487. * @pf: PF struct
  9488. */
  9489. static void i40e_send_version(struct i40e_pf *pf)
  9490. {
  9491. struct i40e_driver_version dv;
  9492. dv.major_version = 0xff;
  9493. dv.minor_version = 0xff;
  9494. dv.build_version = 0xff;
  9495. dv.subbuild_version = 0;
  9496. strscpy(dv.driver_string, UTS_RELEASE, sizeof(dv.driver_string));
  9497. i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
  9498. }
  9499. /**
  9500. * i40e_get_oem_version - get OEM specific version information
  9501. * @hw: pointer to the hardware structure
  9502. **/
  9503. static void i40e_get_oem_version(struct i40e_hw *hw)
  9504. {
  9505. u16 block_offset = 0xffff;
  9506. u16 block_length = 0;
  9507. u16 capabilities = 0;
  9508. u16 gen_snap = 0;
  9509. u16 release = 0;
  9510. #define I40E_SR_NVM_OEM_VERSION_PTR 0x1B
  9511. #define I40E_NVM_OEM_LENGTH_OFFSET 0x00
  9512. #define I40E_NVM_OEM_CAPABILITIES_OFFSET 0x01
  9513. #define I40E_NVM_OEM_GEN_OFFSET 0x02
  9514. #define I40E_NVM_OEM_RELEASE_OFFSET 0x03
  9515. #define I40E_NVM_OEM_CAPABILITIES_MASK 0x000F
  9516. #define I40E_NVM_OEM_LENGTH 3
  9517. /* Check if pointer to OEM version block is valid. */
  9518. i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
  9519. if (block_offset == 0xffff)
  9520. return;
  9521. /* Check if OEM version block has correct length. */
  9522. i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
  9523. &block_length);
  9524. if (block_length < I40E_NVM_OEM_LENGTH)
  9525. return;
  9526. /* Check if OEM version format is as expected. */
  9527. i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
  9528. &capabilities);
  9529. if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
  9530. return;
  9531. i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
  9532. &gen_snap);
  9533. i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
  9534. &release);
  9535. hw->nvm.oem_ver =
  9536. FIELD_PREP(I40E_OEM_GEN_MASK | I40E_OEM_SNAP_MASK, gen_snap) |
  9537. FIELD_PREP(I40E_OEM_RELEASE_MASK, release);
  9538. hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
  9539. }
  9540. /**
  9541. * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
  9542. * @pf: board private structure
  9543. **/
  9544. static int i40e_reset(struct i40e_pf *pf)
  9545. {
  9546. struct i40e_hw *hw = &pf->hw;
  9547. int ret;
  9548. ret = i40e_pf_reset(hw);
  9549. if (ret) {
  9550. dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
  9551. set_bit(__I40E_RESET_FAILED, pf->state);
  9552. clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
  9553. } else {
  9554. pf->pfr_count++;
  9555. }
  9556. return ret;
  9557. }
  9558. /**
  9559. * i40e_rebuild - rebuild using a saved config
  9560. * @pf: board private structure
  9561. * @reinit: if the Main VSI needs to re-initialized.
  9562. * @lock_acquired: indicates whether or not the lock has been acquired
  9563. * before this function was called.
  9564. **/
  9565. static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
  9566. {
  9567. const bool is_recovery_mode_reported = i40e_check_recovery_mode(pf);
  9568. struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
  9569. struct i40e_hw *hw = &pf->hw;
  9570. struct i40e_veb *veb;
  9571. int ret;
  9572. u32 val;
  9573. int v;
  9574. if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
  9575. is_recovery_mode_reported)
  9576. i40e_set_ethtool_ops(vsi->netdev);
  9577. if (test_bit(__I40E_DOWN, pf->state) &&
  9578. !test_bit(__I40E_RECOVERY_MODE, pf->state))
  9579. goto clear_recovery;
  9580. dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
  9581. /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
  9582. ret = i40e_init_adminq(&pf->hw);
  9583. if (ret) {
  9584. dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %pe aq_err %s\n",
  9585. ERR_PTR(ret),
  9586. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  9587. goto clear_recovery;
  9588. }
  9589. i40e_get_oem_version(&pf->hw);
  9590. if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state)) {
  9591. /* The following delay is necessary for firmware update. */
  9592. mdelay(1000);
  9593. }
  9594. /* re-verify the eeprom if we just had an EMP reset */
  9595. if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
  9596. i40e_verify_eeprom(pf);
  9597. /* if we are going out of or into recovery mode we have to act
  9598. * accordingly with regard to resources initialization
  9599. * and deinitialization
  9600. */
  9601. if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
  9602. if (i40e_get_capabilities(pf,
  9603. i40e_aqc_opc_list_func_capabilities))
  9604. goto end_unlock;
  9605. if (is_recovery_mode_reported) {
  9606. /* we're staying in recovery mode so we'll reinitialize
  9607. * misc vector here
  9608. */
  9609. if (i40e_setup_misc_vector_for_recovery_mode(pf))
  9610. goto end_unlock;
  9611. } else {
  9612. if (!lock_acquired)
  9613. rtnl_lock();
  9614. /* we're going out of recovery mode so we'll free
  9615. * the IRQ allocated specifically for recovery mode
  9616. * and restore the interrupt scheme
  9617. */
  9618. free_irq(pf->pdev->irq, pf);
  9619. i40e_clear_interrupt_scheme(pf);
  9620. if (i40e_restore_interrupt_scheme(pf))
  9621. goto end_unlock;
  9622. }
  9623. /* tell the firmware that we're starting */
  9624. i40e_send_version(pf);
  9625. /* bail out in case recovery mode was detected, as there is
  9626. * no need for further configuration.
  9627. */
  9628. goto end_unlock;
  9629. }
  9630. i40e_clear_pxe_mode(hw);
  9631. ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
  9632. if (ret)
  9633. goto end_core_reset;
  9634. ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
  9635. hw->func_caps.num_rx_qp, 0, 0);
  9636. if (ret) {
  9637. dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
  9638. goto end_core_reset;
  9639. }
  9640. ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
  9641. if (ret) {
  9642. dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
  9643. goto end_core_reset;
  9644. }
  9645. #ifdef CONFIG_I40E_DCB
  9646. /* Enable FW to write a default DCB config on link-up
  9647. * unless I40E_FLAG_TC_MQPRIO was enabled or DCB
  9648. * is not supported with new link speed
  9649. */
  9650. if (i40e_is_tc_mqprio_enabled(pf)) {
  9651. i40e_aq_set_dcb_parameters(hw, false, NULL);
  9652. } else {
  9653. if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
  9654. (hw->phy.link_info.link_speed &
  9655. (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
  9656. i40e_aq_set_dcb_parameters(hw, false, NULL);
  9657. dev_warn(&pf->pdev->dev,
  9658. "DCB is not supported for X710-T*L 2.5/5G speeds\n");
  9659. clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
  9660. } else {
  9661. i40e_aq_set_dcb_parameters(hw, true, NULL);
  9662. ret = i40e_init_pf_dcb(pf);
  9663. if (ret) {
  9664. dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n",
  9665. ret);
  9666. clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
  9667. /* Continue without DCB enabled */
  9668. }
  9669. }
  9670. }
  9671. #endif /* CONFIG_I40E_DCB */
  9672. if (!lock_acquired)
  9673. rtnl_lock();
  9674. ret = i40e_setup_pf_switch(pf, reinit, true);
  9675. if (ret)
  9676. goto end_unlock;
  9677. /* The driver only wants link up/down and module qualification
  9678. * reports from firmware. Note the negative logic.
  9679. */
  9680. ret = i40e_aq_set_phy_int_mask(&pf->hw,
  9681. ~(I40E_AQ_EVENT_LINK_UPDOWN |
  9682. I40E_AQ_EVENT_MEDIA_NA |
  9683. I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
  9684. if (ret)
  9685. dev_info(&pf->pdev->dev, "set phy mask fail, err %pe aq_err %s\n",
  9686. ERR_PTR(ret),
  9687. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  9688. /* Rebuild the VSIs and VEBs that existed before reset.
  9689. * They are still in our local switch element arrays, so only
  9690. * need to rebuild the switch model in the HW.
  9691. *
  9692. * If there were VEBs but the reconstitution failed, we'll try
  9693. * to recover minimal use by getting the basic PF VSI working.
  9694. */
  9695. if (vsi->uplink_seid != pf->mac_seid) {
  9696. dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
  9697. /* Rebuild VEBs */
  9698. i40e_pf_for_each_veb(pf, v, veb) {
  9699. ret = i40e_reconstitute_veb(veb);
  9700. if (!ret)
  9701. continue;
  9702. /* If Main VEB failed, we're in deep doodoo,
  9703. * so give up rebuilding the switch and set up
  9704. * for minimal rebuild of PF VSI.
  9705. * If orphan failed, we'll report the error
  9706. * but try to keep going.
  9707. */
  9708. if (veb->uplink_seid == pf->mac_seid) {
  9709. dev_info(&pf->pdev->dev,
  9710. "rebuild of switch failed: %d, will try to set up simple PF connection\n",
  9711. ret);
  9712. vsi->uplink_seid = pf->mac_seid;
  9713. break;
  9714. } else if (veb->uplink_seid == 0) {
  9715. dev_info(&pf->pdev->dev,
  9716. "rebuild of orphan VEB failed: %d\n",
  9717. ret);
  9718. }
  9719. }
  9720. }
  9721. if (vsi->uplink_seid == pf->mac_seid) {
  9722. dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
  9723. /* no VEB, so rebuild only the Main VSI */
  9724. ret = i40e_add_vsi(vsi);
  9725. if (ret) {
  9726. dev_info(&pf->pdev->dev,
  9727. "rebuild of Main VSI failed: %d\n", ret);
  9728. goto end_unlock;
  9729. }
  9730. }
  9731. if (vsi->mqprio_qopt.max_rate[0]) {
  9732. u64 max_tx_rate = i40e_bw_bytes_to_mbits(vsi,
  9733. vsi->mqprio_qopt.max_rate[0]);
  9734. u64 credits = 0;
  9735. ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
  9736. if (ret)
  9737. goto end_unlock;
  9738. credits = max_tx_rate;
  9739. do_div(credits, I40E_BW_CREDIT_DIVISOR);
  9740. dev_dbg(&vsi->back->pdev->dev,
  9741. "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
  9742. max_tx_rate,
  9743. credits,
  9744. vsi->seid);
  9745. }
  9746. ret = i40e_rebuild_cloud_filters(vsi, vsi->seid);
  9747. if (ret)
  9748. goto end_unlock;
  9749. /* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs
  9750. * for this main VSI if they exist
  9751. */
  9752. ret = i40e_rebuild_channels(vsi);
  9753. if (ret)
  9754. goto end_unlock;
  9755. /* Reconfigure hardware for allowing smaller MSS in the case
  9756. * of TSO, so that we avoid the MDD being fired and causing
  9757. * a reset in the case of small MSS+TSO.
  9758. */
  9759. #define I40E_REG_MSS 0x000E64DC
  9760. #define I40E_REG_MSS_MIN_MASK 0x3FF0000
  9761. #define I40E_64BYTE_MSS 0x400000
  9762. val = rd32(hw, I40E_REG_MSS);
  9763. if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
  9764. val &= ~I40E_REG_MSS_MIN_MASK;
  9765. val |= I40E_64BYTE_MSS;
  9766. wr32(hw, I40E_REG_MSS, val);
  9767. }
  9768. if (test_bit(I40E_HW_CAP_RESTART_AUTONEG, pf->hw.caps)) {
  9769. msleep(75);
  9770. ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
  9771. if (ret)
  9772. dev_info(&pf->pdev->dev, "link restart failed, err %pe aq_err %s\n",
  9773. ERR_PTR(ret),
  9774. i40e_aq_str(&pf->hw,
  9775. pf->hw.aq.asq_last_status));
  9776. }
  9777. /* reinit the misc interrupt */
  9778. if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
  9779. ret = i40e_setup_misc_vector(pf);
  9780. if (ret)
  9781. goto end_unlock;
  9782. }
  9783. /* Add a filter to drop all Flow control frames from any VSI from being
  9784. * transmitted. By doing so we stop a malicious VF from sending out
  9785. * PAUSE or PFC frames and potentially controlling traffic for other
  9786. * PF/VF VSIs.
  9787. * The FW can still send Flow control frames if enabled.
  9788. */
  9789. i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
  9790. pf->main_vsi_seid);
  9791. /* restart the VSIs that were rebuilt and running before the reset */
  9792. i40e_pf_unquiesce_all_vsi(pf);
  9793. /* Release the RTNL lock before we start resetting VFs */
  9794. if (!lock_acquired)
  9795. rtnl_unlock();
  9796. /* Restore promiscuous settings */
  9797. ret = i40e_set_promiscuous(pf, pf->cur_promisc);
  9798. if (ret)
  9799. dev_warn(&pf->pdev->dev,
  9800. "Failed to restore promiscuous setting: %s, err %pe aq_err %s\n",
  9801. pf->cur_promisc ? "on" : "off",
  9802. ERR_PTR(ret),
  9803. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  9804. i40e_reset_all_vfs(pf, true);
  9805. /* tell the firmware that we're starting */
  9806. i40e_send_version(pf);
  9807. /* We've already released the lock, so don't do it again */
  9808. goto end_core_reset;
  9809. end_unlock:
  9810. if (!lock_acquired)
  9811. rtnl_unlock();
  9812. end_core_reset:
  9813. clear_bit(__I40E_RESET_FAILED, pf->state);
  9814. clear_recovery:
  9815. clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
  9816. clear_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state);
  9817. }
  9818. /**
  9819. * i40e_reset_and_rebuild - reset and rebuild using a saved config
  9820. * @pf: board private structure
  9821. * @reinit: if the Main VSI needs to re-initialized.
  9822. * @lock_acquired: indicates whether or not the lock has been acquired
  9823. * before this function was called.
  9824. **/
  9825. static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
  9826. bool lock_acquired)
  9827. {
  9828. int ret;
  9829. if (test_bit(__I40E_IN_REMOVE, pf->state))
  9830. return;
  9831. /* Now we wait for GRST to settle out.
  9832. * We don't have to delete the VEBs or VSIs from the hw switch
  9833. * because the reset will make them disappear.
  9834. */
  9835. ret = i40e_reset(pf);
  9836. if (!ret)
  9837. i40e_rebuild(pf, reinit, lock_acquired);
  9838. else
  9839. dev_err(&pf->pdev->dev, "%s: i40e_reset() FAILED", __func__);
  9840. }
  9841. /**
  9842. * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
  9843. * @pf: board private structure
  9844. *
  9845. * Close up the VFs and other things in prep for a Core Reset,
  9846. * then get ready to rebuild the world.
  9847. * @lock_acquired: indicates whether or not the lock has been acquired
  9848. * before this function was called.
  9849. **/
  9850. static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
  9851. {
  9852. i40e_prep_for_reset(pf);
  9853. i40e_reset_and_rebuild(pf, false, lock_acquired);
  9854. }
  9855. /**
  9856. * i40e_handle_mdd_event
  9857. * @pf: pointer to the PF structure
  9858. *
  9859. * Called from the MDD irq handler to identify possibly malicious vfs
  9860. **/
  9861. static void i40e_handle_mdd_event(struct i40e_pf *pf)
  9862. {
  9863. struct i40e_hw *hw = &pf->hw;
  9864. bool mdd_detected = false;
  9865. struct i40e_vf *vf;
  9866. u32 reg;
  9867. int i;
  9868. if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
  9869. return;
  9870. /* find what triggered the MDD event */
  9871. reg = rd32(hw, I40E_GL_MDET_TX);
  9872. if (reg & I40E_GL_MDET_TX_VALID_MASK) {
  9873. u8 pf_num = FIELD_GET(I40E_GL_MDET_TX_PF_NUM_MASK, reg);
  9874. u16 vf_num = FIELD_GET(I40E_GL_MDET_TX_VF_NUM_MASK, reg);
  9875. u8 event = FIELD_GET(I40E_GL_MDET_TX_EVENT_MASK, reg);
  9876. u16 queue = FIELD_GET(I40E_GL_MDET_TX_QUEUE_MASK, reg) -
  9877. pf->hw.func_caps.base_queue;
  9878. if (netif_msg_tx_err(pf))
  9879. dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
  9880. event, queue, pf_num, vf_num);
  9881. wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
  9882. mdd_detected = true;
  9883. }
  9884. reg = rd32(hw, I40E_GL_MDET_RX);
  9885. if (reg & I40E_GL_MDET_RX_VALID_MASK) {
  9886. u8 func = FIELD_GET(I40E_GL_MDET_RX_FUNCTION_MASK, reg);
  9887. u8 event = FIELD_GET(I40E_GL_MDET_RX_EVENT_MASK, reg);
  9888. u16 queue = FIELD_GET(I40E_GL_MDET_RX_QUEUE_MASK, reg) -
  9889. pf->hw.func_caps.base_queue;
  9890. if (netif_msg_rx_err(pf))
  9891. dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
  9892. event, queue, func);
  9893. wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
  9894. mdd_detected = true;
  9895. }
  9896. if (mdd_detected) {
  9897. reg = rd32(hw, I40E_PF_MDET_TX);
  9898. if (reg & I40E_PF_MDET_TX_VALID_MASK) {
  9899. wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
  9900. dev_dbg(&pf->pdev->dev, "TX driver issue detected on PF\n");
  9901. }
  9902. reg = rd32(hw, I40E_PF_MDET_RX);
  9903. if (reg & I40E_PF_MDET_RX_VALID_MASK) {
  9904. wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
  9905. dev_dbg(&pf->pdev->dev, "RX driver issue detected on PF\n");
  9906. }
  9907. }
  9908. /* see if one of the VFs needs its hand slapped */
  9909. for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
  9910. vf = &(pf->vf[i]);
  9911. reg = rd32(hw, I40E_VP_MDET_TX(i));
  9912. if (reg & I40E_VP_MDET_TX_VALID_MASK) {
  9913. wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
  9914. vf->num_mdd_events++;
  9915. dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
  9916. i);
  9917. dev_info(&pf->pdev->dev,
  9918. "Use PF Control I/F to re-enable the VF\n");
  9919. set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
  9920. }
  9921. reg = rd32(hw, I40E_VP_MDET_RX(i));
  9922. if (reg & I40E_VP_MDET_RX_VALID_MASK) {
  9923. wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
  9924. vf->num_mdd_events++;
  9925. dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
  9926. i);
  9927. dev_info(&pf->pdev->dev,
  9928. "Use PF Control I/F to re-enable the VF\n");
  9929. set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
  9930. }
  9931. }
  9932. /* re-enable mdd interrupt cause */
  9933. clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
  9934. reg = rd32(hw, I40E_PFINT_ICR0_ENA);
  9935. reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
  9936. wr32(hw, I40E_PFINT_ICR0_ENA, reg);
  9937. i40e_flush(hw);
  9938. }
  9939. /**
  9940. * i40e_service_task - Run the driver's async subtasks
  9941. * @work: pointer to work_struct containing our data
  9942. **/
  9943. static void i40e_service_task(struct work_struct *work)
  9944. {
  9945. struct i40e_pf *pf = container_of(work,
  9946. struct i40e_pf,
  9947. service_task);
  9948. unsigned long start_time = jiffies;
  9949. /* don't bother with service tasks if a reset is in progress */
  9950. if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
  9951. test_bit(__I40E_SUSPENDED, pf->state))
  9952. return;
  9953. if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
  9954. return;
  9955. if (!test_bit(__I40E_RECOVERY_MODE, pf->state)) {
  9956. i40e_detect_recover_hung(pf);
  9957. i40e_sync_filters_subtask(pf);
  9958. i40e_reset_subtask(pf);
  9959. i40e_handle_mdd_event(pf);
  9960. i40e_vc_process_vflr_event(pf);
  9961. i40e_watchdog_subtask(pf);
  9962. i40e_fdir_reinit_subtask(pf);
  9963. if (test_and_clear_bit(__I40E_CLIENT_RESET, pf->state)) {
  9964. /* Client subtask will reopen next time through. */
  9965. i40e_notify_client_of_netdev_close(pf, true);
  9966. } else {
  9967. i40e_client_subtask(pf);
  9968. if (test_and_clear_bit(__I40E_CLIENT_L2_CHANGE,
  9969. pf->state))
  9970. i40e_notify_client_of_l2_param_changes(pf);
  9971. }
  9972. i40e_sync_filters_subtask(pf);
  9973. } else {
  9974. i40e_reset_subtask(pf);
  9975. }
  9976. i40e_clean_adminq_subtask(pf);
  9977. /* flush memory to make sure state is correct before next watchdog */
  9978. smp_mb__before_atomic();
  9979. clear_bit(__I40E_SERVICE_SCHED, pf->state);
  9980. /* If the tasks have taken longer than one timer cycle or there
  9981. * is more work to be done, reschedule the service task now
  9982. * rather than wait for the timer to tick again.
  9983. */
  9984. if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
  9985. test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state) ||
  9986. test_bit(__I40E_MDD_EVENT_PENDING, pf->state) ||
  9987. test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
  9988. i40e_service_event_schedule(pf);
  9989. }
  9990. /**
  9991. * i40e_service_timer - timer callback
  9992. * @t: timer list pointer
  9993. **/
  9994. static void i40e_service_timer(struct timer_list *t)
  9995. {
  9996. struct i40e_pf *pf = from_timer(pf, t, service_timer);
  9997. mod_timer(&pf->service_timer,
  9998. round_jiffies(jiffies + pf->service_timer_period));
  9999. i40e_service_event_schedule(pf);
  10000. }
  10001. /**
  10002. * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
  10003. * @vsi: the VSI being configured
  10004. **/
  10005. static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
  10006. {
  10007. struct i40e_pf *pf = vsi->back;
  10008. switch (vsi->type) {
  10009. case I40E_VSI_MAIN:
  10010. vsi->alloc_queue_pairs = pf->num_lan_qps;
  10011. if (!vsi->num_tx_desc)
  10012. vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  10013. I40E_REQ_DESCRIPTOR_MULTIPLE);
  10014. if (!vsi->num_rx_desc)
  10015. vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  10016. I40E_REQ_DESCRIPTOR_MULTIPLE);
  10017. if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
  10018. vsi->num_q_vectors = pf->num_lan_msix;
  10019. else
  10020. vsi->num_q_vectors = 1;
  10021. break;
  10022. case I40E_VSI_FDIR:
  10023. vsi->alloc_queue_pairs = 1;
  10024. vsi->num_tx_desc = ALIGN(I40E_FDIR_RING_COUNT,
  10025. I40E_REQ_DESCRIPTOR_MULTIPLE);
  10026. vsi->num_rx_desc = ALIGN(I40E_FDIR_RING_COUNT,
  10027. I40E_REQ_DESCRIPTOR_MULTIPLE);
  10028. vsi->num_q_vectors = pf->num_fdsb_msix;
  10029. break;
  10030. case I40E_VSI_VMDQ2:
  10031. vsi->alloc_queue_pairs = pf->num_vmdq_qps;
  10032. if (!vsi->num_tx_desc)
  10033. vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  10034. I40E_REQ_DESCRIPTOR_MULTIPLE);
  10035. if (!vsi->num_rx_desc)
  10036. vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  10037. I40E_REQ_DESCRIPTOR_MULTIPLE);
  10038. vsi->num_q_vectors = pf->num_vmdq_msix;
  10039. break;
  10040. case I40E_VSI_SRIOV:
  10041. vsi->alloc_queue_pairs = pf->num_vf_qps;
  10042. if (!vsi->num_tx_desc)
  10043. vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  10044. I40E_REQ_DESCRIPTOR_MULTIPLE);
  10045. if (!vsi->num_rx_desc)
  10046. vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  10047. I40E_REQ_DESCRIPTOR_MULTIPLE);
  10048. break;
  10049. default:
  10050. WARN_ON(1);
  10051. return -ENODATA;
  10052. }
  10053. if (is_kdump_kernel()) {
  10054. vsi->num_tx_desc = I40E_MIN_NUM_DESCRIPTORS;
  10055. vsi->num_rx_desc = I40E_MIN_NUM_DESCRIPTORS;
  10056. }
  10057. return 0;
  10058. }
  10059. /**
  10060. * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
  10061. * @vsi: VSI pointer
  10062. * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
  10063. *
  10064. * On error: returns error code (negative)
  10065. * On success: returns 0
  10066. **/
  10067. static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
  10068. {
  10069. struct i40e_ring **next_rings;
  10070. int size;
  10071. int ret = 0;
  10072. /* allocate memory for both Tx, XDP Tx and Rx ring pointers */
  10073. size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
  10074. (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
  10075. vsi->tx_rings = kzalloc(size, GFP_KERNEL);
  10076. if (!vsi->tx_rings)
  10077. return -ENOMEM;
  10078. next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
  10079. if (i40e_enabled_xdp_vsi(vsi)) {
  10080. vsi->xdp_rings = next_rings;
  10081. next_rings += vsi->alloc_queue_pairs;
  10082. }
  10083. vsi->rx_rings = next_rings;
  10084. if (alloc_qvectors) {
  10085. /* allocate memory for q_vector pointers */
  10086. size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
  10087. vsi->q_vectors = kzalloc(size, GFP_KERNEL);
  10088. if (!vsi->q_vectors) {
  10089. ret = -ENOMEM;
  10090. goto err_vectors;
  10091. }
  10092. }
  10093. return ret;
  10094. err_vectors:
  10095. kfree(vsi->tx_rings);
  10096. return ret;
  10097. }
  10098. /**
  10099. * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
  10100. * @pf: board private structure
  10101. * @type: type of VSI
  10102. *
  10103. * On error: returns error code (negative)
  10104. * On success: returns vsi index in PF (positive)
  10105. **/
  10106. static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
  10107. {
  10108. int ret = -ENODEV;
  10109. struct i40e_vsi *vsi;
  10110. int vsi_idx;
  10111. int i;
  10112. /* Need to protect the allocation of the VSIs at the PF level */
  10113. mutex_lock(&pf->switch_mutex);
  10114. /* VSI list may be fragmented if VSI creation/destruction has
  10115. * been happening. We can afford to do a quick scan to look
  10116. * for any free VSIs in the list.
  10117. *
  10118. * find next empty vsi slot, looping back around if necessary
  10119. */
  10120. i = pf->next_vsi;
  10121. while (i < pf->num_alloc_vsi && pf->vsi[i])
  10122. i++;
  10123. if (i >= pf->num_alloc_vsi) {
  10124. i = 0;
  10125. while (i < pf->next_vsi && pf->vsi[i])
  10126. i++;
  10127. }
  10128. if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
  10129. vsi_idx = i; /* Found one! */
  10130. } else {
  10131. ret = -ENODEV;
  10132. goto unlock_pf; /* out of VSI slots! */
  10133. }
  10134. pf->next_vsi = ++i;
  10135. vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
  10136. if (!vsi) {
  10137. ret = -ENOMEM;
  10138. goto unlock_pf;
  10139. }
  10140. vsi->type = type;
  10141. vsi->back = pf;
  10142. set_bit(__I40E_VSI_DOWN, vsi->state);
  10143. vsi->flags = 0;
  10144. vsi->idx = vsi_idx;
  10145. vsi->int_rate_limit = 0;
  10146. vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
  10147. pf->rss_table_size : 64;
  10148. vsi->netdev_registered = false;
  10149. vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
  10150. hash_init(vsi->mac_filter_hash);
  10151. vsi->irqs_ready = false;
  10152. if (type == I40E_VSI_MAIN) {
  10153. vsi->af_xdp_zc_qps = bitmap_zalloc(pf->num_lan_qps, GFP_KERNEL);
  10154. if (!vsi->af_xdp_zc_qps)
  10155. goto err_rings;
  10156. }
  10157. ret = i40e_set_num_rings_in_vsi(vsi);
  10158. if (ret)
  10159. goto err_rings;
  10160. ret = i40e_vsi_alloc_arrays(vsi, true);
  10161. if (ret)
  10162. goto err_rings;
  10163. /* Setup default MSIX irq handler for VSI */
  10164. i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
  10165. /* Initialize VSI lock */
  10166. spin_lock_init(&vsi->mac_filter_hash_lock);
  10167. pf->vsi[vsi_idx] = vsi;
  10168. ret = vsi_idx;
  10169. goto unlock_pf;
  10170. err_rings:
  10171. bitmap_free(vsi->af_xdp_zc_qps);
  10172. pf->next_vsi = i - 1;
  10173. kfree(vsi);
  10174. unlock_pf:
  10175. mutex_unlock(&pf->switch_mutex);
  10176. return ret;
  10177. }
  10178. /**
  10179. * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
  10180. * @vsi: VSI pointer
  10181. * @free_qvectors: a bool to specify if q_vectors need to be freed.
  10182. *
  10183. * On error: returns error code (negative)
  10184. * On success: returns 0
  10185. **/
  10186. static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
  10187. {
  10188. /* free the ring and vector containers */
  10189. if (free_qvectors) {
  10190. kfree(vsi->q_vectors);
  10191. vsi->q_vectors = NULL;
  10192. }
  10193. kfree(vsi->tx_rings);
  10194. vsi->tx_rings = NULL;
  10195. vsi->rx_rings = NULL;
  10196. vsi->xdp_rings = NULL;
  10197. }
  10198. /**
  10199. * i40e_clear_rss_config_user - clear the user configured RSS hash keys
  10200. * and lookup table
  10201. * @vsi: Pointer to VSI structure
  10202. */
  10203. static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
  10204. {
  10205. if (!vsi)
  10206. return;
  10207. kfree(vsi->rss_hkey_user);
  10208. vsi->rss_hkey_user = NULL;
  10209. kfree(vsi->rss_lut_user);
  10210. vsi->rss_lut_user = NULL;
  10211. }
  10212. /**
  10213. * i40e_vsi_clear - Deallocate the VSI provided
  10214. * @vsi: the VSI being un-configured
  10215. **/
  10216. static int i40e_vsi_clear(struct i40e_vsi *vsi)
  10217. {
  10218. struct i40e_pf *pf;
  10219. if (!vsi)
  10220. return 0;
  10221. if (!vsi->back)
  10222. goto free_vsi;
  10223. pf = vsi->back;
  10224. mutex_lock(&pf->switch_mutex);
  10225. if (!pf->vsi[vsi->idx]) {
  10226. dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](type %d)\n",
  10227. vsi->idx, vsi->idx, vsi->type);
  10228. goto unlock_vsi;
  10229. }
  10230. if (pf->vsi[vsi->idx] != vsi) {
  10231. dev_err(&pf->pdev->dev,
  10232. "pf->vsi[%d](type %d) != vsi[%d](type %d): no free!\n",
  10233. pf->vsi[vsi->idx]->idx,
  10234. pf->vsi[vsi->idx]->type,
  10235. vsi->idx, vsi->type);
  10236. goto unlock_vsi;
  10237. }
  10238. /* updates the PF for this cleared vsi */
  10239. i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
  10240. i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
  10241. bitmap_free(vsi->af_xdp_zc_qps);
  10242. i40e_vsi_free_arrays(vsi, true);
  10243. i40e_clear_rss_config_user(vsi);
  10244. pf->vsi[vsi->idx] = NULL;
  10245. if (vsi->idx < pf->next_vsi)
  10246. pf->next_vsi = vsi->idx;
  10247. unlock_vsi:
  10248. mutex_unlock(&pf->switch_mutex);
  10249. free_vsi:
  10250. kfree(vsi);
  10251. return 0;
  10252. }
  10253. /**
  10254. * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
  10255. * @vsi: the VSI being cleaned
  10256. **/
  10257. static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
  10258. {
  10259. int i;
  10260. if (vsi->tx_rings && vsi->tx_rings[0]) {
  10261. for (i = 0; i < vsi->alloc_queue_pairs; i++) {
  10262. kfree_rcu(vsi->tx_rings[i], rcu);
  10263. WRITE_ONCE(vsi->tx_rings[i], NULL);
  10264. WRITE_ONCE(vsi->rx_rings[i], NULL);
  10265. if (vsi->xdp_rings)
  10266. WRITE_ONCE(vsi->xdp_rings[i], NULL);
  10267. }
  10268. }
  10269. }
  10270. /**
  10271. * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
  10272. * @vsi: the VSI being configured
  10273. **/
  10274. static int i40e_alloc_rings(struct i40e_vsi *vsi)
  10275. {
  10276. int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
  10277. struct i40e_pf *pf = vsi->back;
  10278. struct i40e_ring *ring;
  10279. /* Set basic values in the rings to be used later during open() */
  10280. for (i = 0; i < vsi->alloc_queue_pairs; i++) {
  10281. /* allocate space for both Tx and Rx in one shot */
  10282. ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
  10283. if (!ring)
  10284. goto err_out;
  10285. ring->queue_index = i;
  10286. ring->reg_idx = vsi->base_queue + i;
  10287. ring->ring_active = false;
  10288. ring->vsi = vsi;
  10289. ring->netdev = vsi->netdev;
  10290. ring->dev = &pf->pdev->dev;
  10291. ring->count = vsi->num_tx_desc;
  10292. ring->size = 0;
  10293. ring->dcb_tc = 0;
  10294. if (test_bit(I40E_HW_CAP_WB_ON_ITR, vsi->back->hw.caps))
  10295. ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
  10296. ring->itr_setting = pf->tx_itr_default;
  10297. WRITE_ONCE(vsi->tx_rings[i], ring++);
  10298. if (!i40e_enabled_xdp_vsi(vsi))
  10299. goto setup_rx;
  10300. ring->queue_index = vsi->alloc_queue_pairs + i;
  10301. ring->reg_idx = vsi->base_queue + ring->queue_index;
  10302. ring->ring_active = false;
  10303. ring->vsi = vsi;
  10304. ring->netdev = NULL;
  10305. ring->dev = &pf->pdev->dev;
  10306. ring->count = vsi->num_tx_desc;
  10307. ring->size = 0;
  10308. ring->dcb_tc = 0;
  10309. if (test_bit(I40E_HW_CAP_WB_ON_ITR, vsi->back->hw.caps))
  10310. ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
  10311. set_ring_xdp(ring);
  10312. ring->itr_setting = pf->tx_itr_default;
  10313. WRITE_ONCE(vsi->xdp_rings[i], ring++);
  10314. setup_rx:
  10315. ring->queue_index = i;
  10316. ring->reg_idx = vsi->base_queue + i;
  10317. ring->ring_active = false;
  10318. ring->vsi = vsi;
  10319. ring->netdev = vsi->netdev;
  10320. ring->dev = &pf->pdev->dev;
  10321. ring->count = vsi->num_rx_desc;
  10322. ring->size = 0;
  10323. ring->dcb_tc = 0;
  10324. ring->itr_setting = pf->rx_itr_default;
  10325. WRITE_ONCE(vsi->rx_rings[i], ring);
  10326. }
  10327. return 0;
  10328. err_out:
  10329. i40e_vsi_clear_rings(vsi);
  10330. return -ENOMEM;
  10331. }
  10332. /**
  10333. * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
  10334. * @pf: board private structure
  10335. * @vectors: the number of MSI-X vectors to request
  10336. *
  10337. * Returns the number of vectors reserved, or error
  10338. **/
  10339. static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
  10340. {
  10341. vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
  10342. I40E_MIN_MSIX, vectors);
  10343. if (vectors < 0) {
  10344. dev_info(&pf->pdev->dev,
  10345. "MSI-X vector reservation failed: %d\n", vectors);
  10346. vectors = 0;
  10347. }
  10348. return vectors;
  10349. }
  10350. /**
  10351. * i40e_init_msix - Setup the MSIX capability
  10352. * @pf: board private structure
  10353. *
  10354. * Work with the OS to set up the MSIX vectors needed.
  10355. *
  10356. * Returns the number of vectors reserved or negative on failure
  10357. **/
  10358. static int i40e_init_msix(struct i40e_pf *pf)
  10359. {
  10360. struct i40e_hw *hw = &pf->hw;
  10361. int cpus, extra_vectors;
  10362. int vectors_left;
  10363. int v_budget, i;
  10364. int v_actual;
  10365. int iwarp_requested = 0;
  10366. if (!test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
  10367. return -ENODEV;
  10368. /* The number of vectors we'll request will be comprised of:
  10369. * - Add 1 for "other" cause for Admin Queue events, etc.
  10370. * - The number of LAN queue pairs
  10371. * - Queues being used for RSS.
  10372. * We don't need as many as max_rss_size vectors.
  10373. * use rss_size instead in the calculation since that
  10374. * is governed by number of cpus in the system.
  10375. * - assumes symmetric Tx/Rx pairing
  10376. * - The number of VMDq pairs
  10377. * - The CPU count within the NUMA node if iWARP is enabled
  10378. * Once we count this up, try the request.
  10379. *
  10380. * If we can't get what we want, we'll simplify to nearly nothing
  10381. * and try again. If that still fails, we punt.
  10382. */
  10383. vectors_left = hw->func_caps.num_msix_vectors;
  10384. v_budget = 0;
  10385. /* reserve one vector for miscellaneous handler */
  10386. if (vectors_left) {
  10387. v_budget++;
  10388. vectors_left--;
  10389. }
  10390. /* reserve some vectors for the main PF traffic queues. Initially we
  10391. * only reserve at most 50% of the available vectors, in the case that
  10392. * the number of online CPUs is large. This ensures that we can enable
  10393. * extra features as well. Once we've enabled the other features, we
  10394. * will use any remaining vectors to reach as close as we can to the
  10395. * number of online CPUs.
  10396. */
  10397. cpus = num_online_cpus();
  10398. pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
  10399. vectors_left -= pf->num_lan_msix;
  10400. /* reserve one vector for sideband flow director */
  10401. if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags)) {
  10402. if (vectors_left) {
  10403. pf->num_fdsb_msix = 1;
  10404. v_budget++;
  10405. vectors_left--;
  10406. } else {
  10407. pf->num_fdsb_msix = 0;
  10408. }
  10409. }
  10410. /* can we reserve enough for iWARP? */
  10411. if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
  10412. iwarp_requested = pf->num_iwarp_msix;
  10413. if (!vectors_left)
  10414. pf->num_iwarp_msix = 0;
  10415. else if (vectors_left < pf->num_iwarp_msix)
  10416. pf->num_iwarp_msix = 1;
  10417. v_budget += pf->num_iwarp_msix;
  10418. vectors_left -= pf->num_iwarp_msix;
  10419. }
  10420. /* any vectors left over go for VMDq support */
  10421. if (test_bit(I40E_FLAG_VMDQ_ENA, pf->flags)) {
  10422. if (!vectors_left) {
  10423. pf->num_vmdq_msix = 0;
  10424. pf->num_vmdq_qps = 0;
  10425. } else {
  10426. int vmdq_vecs_wanted =
  10427. pf->num_vmdq_vsis * pf->num_vmdq_qps;
  10428. int vmdq_vecs =
  10429. min_t(int, vectors_left, vmdq_vecs_wanted);
  10430. /* if we're short on vectors for what's desired, we limit
  10431. * the queues per vmdq. If this is still more than are
  10432. * available, the user will need to change the number of
  10433. * queues/vectors used by the PF later with the ethtool
  10434. * channels command
  10435. */
  10436. if (vectors_left < vmdq_vecs_wanted) {
  10437. pf->num_vmdq_qps = 1;
  10438. vmdq_vecs_wanted = pf->num_vmdq_vsis;
  10439. vmdq_vecs = min_t(int,
  10440. vectors_left,
  10441. vmdq_vecs_wanted);
  10442. }
  10443. pf->num_vmdq_msix = pf->num_vmdq_qps;
  10444. v_budget += vmdq_vecs;
  10445. vectors_left -= vmdq_vecs;
  10446. }
  10447. }
  10448. /* On systems with a large number of SMP cores, we previously limited
  10449. * the number of vectors for num_lan_msix to be at most 50% of the
  10450. * available vectors, to allow for other features. Now, we add back
  10451. * the remaining vectors. However, we ensure that the total
  10452. * num_lan_msix will not exceed num_online_cpus(). To do this, we
  10453. * calculate the number of vectors we can add without going over the
  10454. * cap of CPUs. For systems with a small number of CPUs this will be
  10455. * zero.
  10456. */
  10457. extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
  10458. pf->num_lan_msix += extra_vectors;
  10459. vectors_left -= extra_vectors;
  10460. WARN(vectors_left < 0,
  10461. "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
  10462. v_budget += pf->num_lan_msix;
  10463. pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
  10464. GFP_KERNEL);
  10465. if (!pf->msix_entries)
  10466. return -ENOMEM;
  10467. for (i = 0; i < v_budget; i++)
  10468. pf->msix_entries[i].entry = i;
  10469. v_actual = i40e_reserve_msix_vectors(pf, v_budget);
  10470. if (v_actual < I40E_MIN_MSIX) {
  10471. clear_bit(I40E_FLAG_MSIX_ENA, pf->flags);
  10472. kfree(pf->msix_entries);
  10473. pf->msix_entries = NULL;
  10474. pci_disable_msix(pf->pdev);
  10475. return -ENODEV;
  10476. } else if (v_actual == I40E_MIN_MSIX) {
  10477. /* Adjust for minimal MSIX use */
  10478. pf->num_vmdq_vsis = 0;
  10479. pf->num_vmdq_qps = 0;
  10480. pf->num_lan_qps = 1;
  10481. pf->num_lan_msix = 1;
  10482. } else if (v_actual != v_budget) {
  10483. /* If we have limited resources, we will start with no vectors
  10484. * for the special features and then allocate vectors to some
  10485. * of these features based on the policy and at the end disable
  10486. * the features that did not get any vectors.
  10487. */
  10488. int vec;
  10489. dev_info(&pf->pdev->dev,
  10490. "MSI-X vector limit reached with %d, wanted %d, attempting to redistribute vectors\n",
  10491. v_actual, v_budget);
  10492. /* reserve the misc vector */
  10493. vec = v_actual - 1;
  10494. /* Scale vector usage down */
  10495. pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
  10496. pf->num_vmdq_vsis = 1;
  10497. pf->num_vmdq_qps = 1;
  10498. /* partition out the remaining vectors */
  10499. switch (vec) {
  10500. case 2:
  10501. pf->num_lan_msix = 1;
  10502. break;
  10503. case 3:
  10504. if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
  10505. pf->num_lan_msix = 1;
  10506. pf->num_iwarp_msix = 1;
  10507. } else {
  10508. pf->num_lan_msix = 2;
  10509. }
  10510. break;
  10511. default:
  10512. if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
  10513. pf->num_iwarp_msix = min_t(int, (vec / 3),
  10514. iwarp_requested);
  10515. pf->num_vmdq_vsis = min_t(int, (vec / 3),
  10516. I40E_DEFAULT_NUM_VMDQ_VSI);
  10517. } else {
  10518. pf->num_vmdq_vsis = min_t(int, (vec / 2),
  10519. I40E_DEFAULT_NUM_VMDQ_VSI);
  10520. }
  10521. if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags)) {
  10522. pf->num_fdsb_msix = 1;
  10523. vec--;
  10524. }
  10525. pf->num_lan_msix = min_t(int,
  10526. (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
  10527. pf->num_lan_msix);
  10528. pf->num_lan_qps = pf->num_lan_msix;
  10529. break;
  10530. }
  10531. }
  10532. if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags) && pf->num_fdsb_msix == 0) {
  10533. dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
  10534. clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
  10535. set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
  10536. }
  10537. if (test_bit(I40E_FLAG_VMDQ_ENA, pf->flags) && pf->num_vmdq_msix == 0) {
  10538. dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
  10539. clear_bit(I40E_FLAG_VMDQ_ENA, pf->flags);
  10540. }
  10541. if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags) &&
  10542. pf->num_iwarp_msix == 0) {
  10543. dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
  10544. clear_bit(I40E_FLAG_IWARP_ENA, pf->flags);
  10545. }
  10546. i40e_debug(&pf->hw, I40E_DEBUG_INIT,
  10547. "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
  10548. pf->num_lan_msix,
  10549. pf->num_vmdq_msix * pf->num_vmdq_vsis,
  10550. pf->num_fdsb_msix,
  10551. pf->num_iwarp_msix);
  10552. return v_actual;
  10553. }
  10554. /**
  10555. * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
  10556. * @vsi: the VSI being configured
  10557. * @v_idx: index of the vector in the vsi struct
  10558. *
  10559. * We allocate one q_vector. If allocation fails we return -ENOMEM.
  10560. **/
  10561. static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
  10562. {
  10563. struct i40e_q_vector *q_vector;
  10564. /* allocate q_vector */
  10565. q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
  10566. if (!q_vector)
  10567. return -ENOMEM;
  10568. q_vector->vsi = vsi;
  10569. q_vector->v_idx = v_idx;
  10570. cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
  10571. if (vsi->netdev)
  10572. netif_napi_add(vsi->netdev, &q_vector->napi, i40e_napi_poll);
  10573. /* tie q_vector and vsi together */
  10574. vsi->q_vectors[v_idx] = q_vector;
  10575. return 0;
  10576. }
  10577. /**
  10578. * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
  10579. * @vsi: the VSI being configured
  10580. *
  10581. * We allocate one q_vector per queue interrupt. If allocation fails we
  10582. * return -ENOMEM.
  10583. **/
  10584. static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
  10585. {
  10586. struct i40e_pf *pf = vsi->back;
  10587. int err, v_idx, num_q_vectors;
  10588. /* if not MSIX, give the one vector only to the LAN VSI */
  10589. if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
  10590. num_q_vectors = vsi->num_q_vectors;
  10591. else if (vsi->type == I40E_VSI_MAIN)
  10592. num_q_vectors = 1;
  10593. else
  10594. return -EINVAL;
  10595. for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
  10596. err = i40e_vsi_alloc_q_vector(vsi, v_idx);
  10597. if (err)
  10598. goto err_out;
  10599. }
  10600. return 0;
  10601. err_out:
  10602. while (v_idx--)
  10603. i40e_free_q_vector(vsi, v_idx);
  10604. return err;
  10605. }
  10606. /**
  10607. * i40e_init_interrupt_scheme - Determine proper interrupt scheme
  10608. * @pf: board private structure to initialize
  10609. **/
  10610. static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
  10611. {
  10612. int vectors = 0;
  10613. ssize_t size;
  10614. if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
  10615. vectors = i40e_init_msix(pf);
  10616. if (vectors < 0) {
  10617. clear_bit(I40E_FLAG_MSIX_ENA, pf->flags);
  10618. clear_bit(I40E_FLAG_IWARP_ENA, pf->flags);
  10619. clear_bit(I40E_FLAG_RSS_ENA, pf->flags);
  10620. clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
  10621. clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
  10622. clear_bit(I40E_FLAG_SRIOV_ENA, pf->flags);
  10623. clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
  10624. clear_bit(I40E_FLAG_FD_ATR_ENA, pf->flags);
  10625. clear_bit(I40E_FLAG_VMDQ_ENA, pf->flags);
  10626. set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
  10627. /* rework the queue expectations without MSIX */
  10628. i40e_determine_queue_usage(pf);
  10629. }
  10630. }
  10631. if (!test_bit(I40E_FLAG_MSIX_ENA, pf->flags) &&
  10632. test_bit(I40E_FLAG_MSI_ENA, pf->flags)) {
  10633. dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
  10634. vectors = pci_enable_msi(pf->pdev);
  10635. if (vectors < 0) {
  10636. dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
  10637. vectors);
  10638. clear_bit(I40E_FLAG_MSI_ENA, pf->flags);
  10639. }
  10640. vectors = 1; /* one MSI or Legacy vector */
  10641. }
  10642. if (!test_bit(I40E_FLAG_MSI_ENA, pf->flags) &&
  10643. !test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
  10644. dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
  10645. /* set up vector assignment tracking */
  10646. size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
  10647. pf->irq_pile = kzalloc(size, GFP_KERNEL);
  10648. if (!pf->irq_pile)
  10649. return -ENOMEM;
  10650. pf->irq_pile->num_entries = vectors;
  10651. /* track first vector for misc interrupts, ignore return */
  10652. (void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
  10653. return 0;
  10654. }
  10655. /**
  10656. * i40e_restore_interrupt_scheme - Restore the interrupt scheme
  10657. * @pf: private board data structure
  10658. *
  10659. * Restore the interrupt scheme that was cleared when we suspended the
  10660. * device. This should be called during resume to re-allocate the q_vectors
  10661. * and reacquire IRQs.
  10662. */
  10663. static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
  10664. {
  10665. struct i40e_vsi *vsi;
  10666. int err, i;
  10667. /* We cleared the MSI and MSI-X flags when disabling the old interrupt
  10668. * scheme. We need to re-enabled them here in order to attempt to
  10669. * re-acquire the MSI or MSI-X vectors
  10670. */
  10671. set_bit(I40E_FLAG_MSI_ENA, pf->flags);
  10672. set_bit(I40E_FLAG_MSIX_ENA, pf->flags);
  10673. err = i40e_init_interrupt_scheme(pf);
  10674. if (err)
  10675. return err;
  10676. /* Now that we've re-acquired IRQs, we need to remap the vectors and
  10677. * rings together again.
  10678. */
  10679. i40e_pf_for_each_vsi(pf, i, vsi) {
  10680. err = i40e_vsi_alloc_q_vectors(vsi);
  10681. if (err)
  10682. goto err_unwind;
  10683. i40e_vsi_map_rings_to_vectors(vsi);
  10684. }
  10685. err = i40e_setup_misc_vector(pf);
  10686. if (err)
  10687. goto err_unwind;
  10688. if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags))
  10689. i40e_client_update_msix_info(pf);
  10690. return 0;
  10691. err_unwind:
  10692. while (i--) {
  10693. if (pf->vsi[i])
  10694. i40e_vsi_free_q_vectors(pf->vsi[i]);
  10695. }
  10696. return err;
  10697. }
  10698. /**
  10699. * i40e_setup_misc_vector_for_recovery_mode - Setup the misc vector to handle
  10700. * non queue events in recovery mode
  10701. * @pf: board private structure
  10702. *
  10703. * This sets up the handler for MSIX 0 or MSI/legacy, which is used to manage
  10704. * the non-queue interrupts, e.g. AdminQ and errors in recovery mode.
  10705. * This is handled differently than in recovery mode since no Tx/Rx resources
  10706. * are being allocated.
  10707. **/
  10708. static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf)
  10709. {
  10710. int err;
  10711. if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
  10712. err = i40e_setup_misc_vector(pf);
  10713. if (err) {
  10714. dev_info(&pf->pdev->dev,
  10715. "MSI-X misc vector request failed, error %d\n",
  10716. err);
  10717. return err;
  10718. }
  10719. } else {
  10720. u32 flags = test_bit(I40E_FLAG_MSI_ENA, pf->flags) ? 0 : IRQF_SHARED;
  10721. err = request_irq(pf->pdev->irq, i40e_intr, flags,
  10722. pf->int_name, pf);
  10723. if (err) {
  10724. dev_info(&pf->pdev->dev,
  10725. "MSI/legacy misc vector request failed, error %d\n",
  10726. err);
  10727. return err;
  10728. }
  10729. i40e_enable_misc_int_causes(pf);
  10730. i40e_irq_dynamic_enable_icr0(pf);
  10731. }
  10732. return 0;
  10733. }
  10734. /**
  10735. * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
  10736. * @pf: board private structure
  10737. *
  10738. * This sets up the handler for MSIX 0, which is used to manage the
  10739. * non-queue interrupts, e.g. AdminQ and errors. This is not used
  10740. * when in MSI or Legacy interrupt mode.
  10741. **/
  10742. static int i40e_setup_misc_vector(struct i40e_pf *pf)
  10743. {
  10744. struct i40e_hw *hw = &pf->hw;
  10745. int err = 0;
  10746. /* Only request the IRQ once, the first time through. */
  10747. if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
  10748. err = request_irq(pf->msix_entries[0].vector,
  10749. i40e_intr, 0, pf->int_name, pf);
  10750. if (err) {
  10751. clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
  10752. dev_info(&pf->pdev->dev,
  10753. "request_irq for %s failed: %d\n",
  10754. pf->int_name, err);
  10755. return -EFAULT;
  10756. }
  10757. }
  10758. i40e_enable_misc_int_causes(pf);
  10759. /* associate no queues to the misc vector */
  10760. wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
  10761. wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K >> 1);
  10762. i40e_flush(hw);
  10763. i40e_irq_dynamic_enable_icr0(pf);
  10764. return err;
  10765. }
  10766. /**
  10767. * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
  10768. * @vsi: Pointer to vsi structure
  10769. * @seed: Buffter to store the hash keys
  10770. * @lut: Buffer to store the lookup table entries
  10771. * @lut_size: Size of buffer to store the lookup table entries
  10772. *
  10773. * Return 0 on success, negative on failure
  10774. */
  10775. static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
  10776. u8 *lut, u16 lut_size)
  10777. {
  10778. struct i40e_pf *pf = vsi->back;
  10779. struct i40e_hw *hw = &pf->hw;
  10780. int ret = 0;
  10781. if (seed) {
  10782. ret = i40e_aq_get_rss_key(hw, vsi->id,
  10783. (struct i40e_aqc_get_set_rss_key_data *)seed);
  10784. if (ret) {
  10785. dev_info(&pf->pdev->dev,
  10786. "Cannot get RSS key, err %pe aq_err %s\n",
  10787. ERR_PTR(ret),
  10788. i40e_aq_str(&pf->hw,
  10789. pf->hw.aq.asq_last_status));
  10790. return ret;
  10791. }
  10792. }
  10793. if (lut) {
  10794. bool pf_lut = vsi->type == I40E_VSI_MAIN;
  10795. ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
  10796. if (ret) {
  10797. dev_info(&pf->pdev->dev,
  10798. "Cannot get RSS lut, err %pe aq_err %s\n",
  10799. ERR_PTR(ret),
  10800. i40e_aq_str(&pf->hw,
  10801. pf->hw.aq.asq_last_status));
  10802. return ret;
  10803. }
  10804. }
  10805. return ret;
  10806. }
  10807. /**
  10808. * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
  10809. * @vsi: Pointer to vsi structure
  10810. * @seed: RSS hash seed
  10811. * @lut: Lookup table
  10812. * @lut_size: Lookup table size
  10813. *
  10814. * Returns 0 on success, negative on failure
  10815. **/
  10816. static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
  10817. const u8 *lut, u16 lut_size)
  10818. {
  10819. struct i40e_pf *pf = vsi->back;
  10820. struct i40e_hw *hw = &pf->hw;
  10821. u16 vf_id = vsi->vf_id;
  10822. u8 i;
  10823. /* Fill out hash function seed */
  10824. if (seed) {
  10825. u32 *seed_dw = (u32 *)seed;
  10826. if (vsi->type == I40E_VSI_MAIN) {
  10827. for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
  10828. wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
  10829. } else if (vsi->type == I40E_VSI_SRIOV) {
  10830. for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
  10831. wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
  10832. } else {
  10833. dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
  10834. }
  10835. }
  10836. if (lut) {
  10837. u32 *lut_dw = (u32 *)lut;
  10838. if (vsi->type == I40E_VSI_MAIN) {
  10839. if (lut_size != I40E_HLUT_ARRAY_SIZE)
  10840. return -EINVAL;
  10841. for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
  10842. wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
  10843. } else if (vsi->type == I40E_VSI_SRIOV) {
  10844. if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
  10845. return -EINVAL;
  10846. for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
  10847. wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
  10848. } else {
  10849. dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
  10850. }
  10851. }
  10852. i40e_flush(hw);
  10853. return 0;
  10854. }
  10855. /**
  10856. * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
  10857. * @vsi: Pointer to VSI structure
  10858. * @seed: Buffer to store the keys
  10859. * @lut: Buffer to store the lookup table entries
  10860. * @lut_size: Size of buffer to store the lookup table entries
  10861. *
  10862. * Returns 0 on success, negative on failure
  10863. */
  10864. static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
  10865. u8 *lut, u16 lut_size)
  10866. {
  10867. struct i40e_pf *pf = vsi->back;
  10868. struct i40e_hw *hw = &pf->hw;
  10869. u16 i;
  10870. if (seed) {
  10871. u32 *seed_dw = (u32 *)seed;
  10872. for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
  10873. seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
  10874. }
  10875. if (lut) {
  10876. u32 *lut_dw = (u32 *)lut;
  10877. if (lut_size != I40E_HLUT_ARRAY_SIZE)
  10878. return -EINVAL;
  10879. for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
  10880. lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
  10881. }
  10882. return 0;
  10883. }
  10884. /**
  10885. * i40e_config_rss - Configure RSS keys and lut
  10886. * @vsi: Pointer to VSI structure
  10887. * @seed: RSS hash seed
  10888. * @lut: Lookup table
  10889. * @lut_size: Lookup table size
  10890. *
  10891. * Returns 0 on success, negative on failure
  10892. */
  10893. int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
  10894. {
  10895. struct i40e_pf *pf = vsi->back;
  10896. if (test_bit(I40E_HW_CAP_RSS_AQ, pf->hw.caps))
  10897. return i40e_config_rss_aq(vsi, seed, lut, lut_size);
  10898. else
  10899. return i40e_config_rss_reg(vsi, seed, lut, lut_size);
  10900. }
  10901. /**
  10902. * i40e_get_rss - Get RSS keys and lut
  10903. * @vsi: Pointer to VSI structure
  10904. * @seed: Buffer to store the keys
  10905. * @lut: Buffer to store the lookup table entries
  10906. * @lut_size: Size of buffer to store the lookup table entries
  10907. *
  10908. * Returns 0 on success, negative on failure
  10909. */
  10910. int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
  10911. {
  10912. struct i40e_pf *pf = vsi->back;
  10913. if (test_bit(I40E_HW_CAP_RSS_AQ, pf->hw.caps))
  10914. return i40e_get_rss_aq(vsi, seed, lut, lut_size);
  10915. else
  10916. return i40e_get_rss_reg(vsi, seed, lut, lut_size);
  10917. }
  10918. /**
  10919. * i40e_fill_rss_lut - Fill the RSS lookup table with default values
  10920. * @pf: Pointer to board private structure
  10921. * @lut: Lookup table
  10922. * @rss_table_size: Lookup table size
  10923. * @rss_size: Range of queue number for hashing
  10924. */
  10925. void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
  10926. u16 rss_table_size, u16 rss_size)
  10927. {
  10928. u16 i;
  10929. for (i = 0; i < rss_table_size; i++)
  10930. lut[i] = i % rss_size;
  10931. }
  10932. /**
  10933. * i40e_pf_config_rss - Prepare for RSS if used
  10934. * @pf: board private structure
  10935. **/
  10936. static int i40e_pf_config_rss(struct i40e_pf *pf)
  10937. {
  10938. struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
  10939. u8 seed[I40E_HKEY_ARRAY_SIZE];
  10940. u8 *lut;
  10941. struct i40e_hw *hw = &pf->hw;
  10942. u32 reg_val;
  10943. u64 hena;
  10944. int ret;
  10945. /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
  10946. hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
  10947. ((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
  10948. hena |= i40e_pf_get_default_rss_hena(pf);
  10949. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
  10950. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
  10951. /* Determine the RSS table size based on the hardware capabilities */
  10952. reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
  10953. reg_val = (pf->rss_table_size == 512) ?
  10954. (reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
  10955. (reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
  10956. i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
  10957. /* Determine the RSS size of the VSI */
  10958. if (!vsi->rss_size) {
  10959. u16 qcount;
  10960. /* If the firmware does something weird during VSI init, we
  10961. * could end up with zero TCs. Check for that to avoid
  10962. * divide-by-zero. It probably won't pass traffic, but it also
  10963. * won't panic.
  10964. */
  10965. qcount = vsi->num_queue_pairs /
  10966. (vsi->tc_config.numtc ? vsi->tc_config.numtc : 1);
  10967. vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
  10968. }
  10969. if (!vsi->rss_size)
  10970. return -EINVAL;
  10971. lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
  10972. if (!lut)
  10973. return -ENOMEM;
  10974. /* Use user configured lut if there is one, otherwise use default */
  10975. if (vsi->rss_lut_user)
  10976. memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
  10977. else
  10978. i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
  10979. /* Use user configured hash key if there is one, otherwise
  10980. * use default.
  10981. */
  10982. if (vsi->rss_hkey_user)
  10983. memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
  10984. else
  10985. netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
  10986. ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
  10987. kfree(lut);
  10988. return ret;
  10989. }
  10990. /**
  10991. * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
  10992. * @pf: board private structure
  10993. * @queue_count: the requested queue count for rss.
  10994. *
  10995. * returns 0 if rss is not enabled, if enabled returns the final rss queue
  10996. * count which may be different from the requested queue count.
  10997. * Note: expects to be called while under rtnl_lock()
  10998. **/
  10999. int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
  11000. {
  11001. struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
  11002. int new_rss_size;
  11003. if (!test_bit(I40E_FLAG_RSS_ENA, pf->flags))
  11004. return 0;
  11005. queue_count = min_t(int, queue_count, num_online_cpus());
  11006. new_rss_size = min_t(int, queue_count, pf->rss_size_max);
  11007. if (queue_count != vsi->num_queue_pairs) {
  11008. u16 qcount;
  11009. vsi->req_queue_pairs = queue_count;
  11010. i40e_prep_for_reset(pf);
  11011. if (test_bit(__I40E_IN_REMOVE, pf->state))
  11012. return pf->alloc_rss_size;
  11013. pf->alloc_rss_size = new_rss_size;
  11014. i40e_reset_and_rebuild(pf, true, true);
  11015. /* Discard the user configured hash keys and lut, if less
  11016. * queues are enabled.
  11017. */
  11018. if (queue_count < vsi->rss_size) {
  11019. i40e_clear_rss_config_user(vsi);
  11020. dev_dbg(&pf->pdev->dev,
  11021. "discard user configured hash keys and lut\n");
  11022. }
  11023. /* Reset vsi->rss_size, as number of enabled queues changed */
  11024. qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
  11025. vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
  11026. i40e_pf_config_rss(pf);
  11027. }
  11028. dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count: %d/%d\n",
  11029. vsi->req_queue_pairs, pf->rss_size_max);
  11030. return pf->alloc_rss_size;
  11031. }
  11032. /**
  11033. * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
  11034. * @pf: board private structure
  11035. **/
  11036. int i40e_get_partition_bw_setting(struct i40e_pf *pf)
  11037. {
  11038. bool min_valid, max_valid;
  11039. u32 max_bw, min_bw;
  11040. int status;
  11041. status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
  11042. &min_valid, &max_valid);
  11043. if (!status) {
  11044. if (min_valid)
  11045. pf->min_bw = min_bw;
  11046. if (max_valid)
  11047. pf->max_bw = max_bw;
  11048. }
  11049. return status;
  11050. }
  11051. /**
  11052. * i40e_set_partition_bw_setting - Set BW settings for this PF partition
  11053. * @pf: board private structure
  11054. **/
  11055. int i40e_set_partition_bw_setting(struct i40e_pf *pf)
  11056. {
  11057. struct i40e_aqc_configure_partition_bw_data bw_data;
  11058. int status;
  11059. memset(&bw_data, 0, sizeof(bw_data));
  11060. /* Set the valid bit for this PF */
  11061. bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
  11062. bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
  11063. bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
  11064. /* Set the new bandwidths */
  11065. status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
  11066. return status;
  11067. }
  11068. /**
  11069. * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
  11070. * @pf: board private structure
  11071. **/
  11072. int i40e_commit_partition_bw_setting(struct i40e_pf *pf)
  11073. {
  11074. /* Commit temporary BW setting to permanent NVM image */
  11075. enum i40e_admin_queue_err last_aq_status;
  11076. u16 nvm_word;
  11077. int ret;
  11078. if (pf->hw.partition_id != 1) {
  11079. dev_info(&pf->pdev->dev,
  11080. "Commit BW only works on partition 1! This is partition %d",
  11081. pf->hw.partition_id);
  11082. ret = -EOPNOTSUPP;
  11083. goto bw_commit_out;
  11084. }
  11085. /* Acquire NVM for read access */
  11086. ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
  11087. last_aq_status = pf->hw.aq.asq_last_status;
  11088. if (ret) {
  11089. dev_info(&pf->pdev->dev,
  11090. "Cannot acquire NVM for read access, err %pe aq_err %s\n",
  11091. ERR_PTR(ret),
  11092. i40e_aq_str(&pf->hw, last_aq_status));
  11093. goto bw_commit_out;
  11094. }
  11095. /* Read word 0x10 of NVM - SW compatibility word 1 */
  11096. ret = i40e_aq_read_nvm(&pf->hw,
  11097. I40E_SR_NVM_CONTROL_WORD,
  11098. 0x10, sizeof(nvm_word), &nvm_word,
  11099. false, NULL);
  11100. /* Save off last admin queue command status before releasing
  11101. * the NVM
  11102. */
  11103. last_aq_status = pf->hw.aq.asq_last_status;
  11104. i40e_release_nvm(&pf->hw);
  11105. if (ret) {
  11106. dev_info(&pf->pdev->dev, "NVM read error, err %pe aq_err %s\n",
  11107. ERR_PTR(ret),
  11108. i40e_aq_str(&pf->hw, last_aq_status));
  11109. goto bw_commit_out;
  11110. }
  11111. /* Wait a bit for NVM release to complete */
  11112. msleep(50);
  11113. /* Acquire NVM for write access */
  11114. ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
  11115. last_aq_status = pf->hw.aq.asq_last_status;
  11116. if (ret) {
  11117. dev_info(&pf->pdev->dev,
  11118. "Cannot acquire NVM for write access, err %pe aq_err %s\n",
  11119. ERR_PTR(ret),
  11120. i40e_aq_str(&pf->hw, last_aq_status));
  11121. goto bw_commit_out;
  11122. }
  11123. /* Write it back out unchanged to initiate update NVM,
  11124. * which will force a write of the shadow (alt) RAM to
  11125. * the NVM - thus storing the bandwidth values permanently.
  11126. */
  11127. ret = i40e_aq_update_nvm(&pf->hw,
  11128. I40E_SR_NVM_CONTROL_WORD,
  11129. 0x10, sizeof(nvm_word),
  11130. &nvm_word, true, 0, NULL);
  11131. /* Save off last admin queue command status before releasing
  11132. * the NVM
  11133. */
  11134. last_aq_status = pf->hw.aq.asq_last_status;
  11135. i40e_release_nvm(&pf->hw);
  11136. if (ret)
  11137. dev_info(&pf->pdev->dev,
  11138. "BW settings NOT SAVED, err %pe aq_err %s\n",
  11139. ERR_PTR(ret),
  11140. i40e_aq_str(&pf->hw, last_aq_status));
  11141. bw_commit_out:
  11142. return ret;
  11143. }
  11144. /**
  11145. * i40e_is_total_port_shutdown_enabled - read NVM and return value
  11146. * if total port shutdown feature is enabled for this PF
  11147. * @pf: board private structure
  11148. **/
  11149. static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf)
  11150. {
  11151. #define I40E_TOTAL_PORT_SHUTDOWN_ENABLED BIT(4)
  11152. #define I40E_FEATURES_ENABLE_PTR 0x2A
  11153. #define I40E_CURRENT_SETTING_PTR 0x2B
  11154. #define I40E_LINK_BEHAVIOR_WORD_OFFSET 0x2D
  11155. #define I40E_LINK_BEHAVIOR_WORD_LENGTH 0x1
  11156. #define I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED BIT(0)
  11157. #define I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH 4
  11158. u16 sr_emp_sr_settings_ptr = 0;
  11159. u16 features_enable = 0;
  11160. u16 link_behavior = 0;
  11161. int read_status = 0;
  11162. bool ret = false;
  11163. read_status = i40e_read_nvm_word(&pf->hw,
  11164. I40E_SR_EMP_SR_SETTINGS_PTR,
  11165. &sr_emp_sr_settings_ptr);
  11166. if (read_status)
  11167. goto err_nvm;
  11168. read_status = i40e_read_nvm_word(&pf->hw,
  11169. sr_emp_sr_settings_ptr +
  11170. I40E_FEATURES_ENABLE_PTR,
  11171. &features_enable);
  11172. if (read_status)
  11173. goto err_nvm;
  11174. if (I40E_TOTAL_PORT_SHUTDOWN_ENABLED & features_enable) {
  11175. read_status = i40e_read_nvm_module_data(&pf->hw,
  11176. I40E_SR_EMP_SR_SETTINGS_PTR,
  11177. I40E_CURRENT_SETTING_PTR,
  11178. I40E_LINK_BEHAVIOR_WORD_OFFSET,
  11179. I40E_LINK_BEHAVIOR_WORD_LENGTH,
  11180. &link_behavior);
  11181. if (read_status)
  11182. goto err_nvm;
  11183. link_behavior >>= (pf->hw.port * I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH);
  11184. ret = I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED & link_behavior;
  11185. }
  11186. return ret;
  11187. err_nvm:
  11188. dev_warn(&pf->pdev->dev,
  11189. "total-port-shutdown feature is off due to read nvm error: %pe\n",
  11190. ERR_PTR(read_status));
  11191. return ret;
  11192. }
  11193. /**
  11194. * i40e_sw_init - Initialize general software structures (struct i40e_pf)
  11195. * @pf: board private structure to initialize
  11196. *
  11197. * i40e_sw_init initializes the Adapter private data structure.
  11198. * Fields are initialized based on PCI device information and
  11199. * OS network device settings (MTU size).
  11200. **/
  11201. static int i40e_sw_init(struct i40e_pf *pf)
  11202. {
  11203. int err = 0;
  11204. int size;
  11205. u16 pow;
  11206. /* Set default capability flags */
  11207. bitmap_zero(pf->flags, I40E_PF_FLAGS_NBITS);
  11208. set_bit(I40E_FLAG_MSI_ENA, pf->flags);
  11209. set_bit(I40E_FLAG_MSIX_ENA, pf->flags);
  11210. /* Set default ITR */
  11211. pf->rx_itr_default = I40E_ITR_RX_DEF;
  11212. pf->tx_itr_default = I40E_ITR_TX_DEF;
  11213. /* Depending on PF configurations, it is possible that the RSS
  11214. * maximum might end up larger than the available queues
  11215. */
  11216. pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
  11217. pf->alloc_rss_size = 1;
  11218. pf->rss_table_size = pf->hw.func_caps.rss_table_size;
  11219. pf->rss_size_max = min_t(int, pf->rss_size_max,
  11220. pf->hw.func_caps.num_tx_qp);
  11221. /* find the next higher power-of-2 of num cpus */
  11222. pow = roundup_pow_of_two(num_online_cpus());
  11223. pf->rss_size_max = min_t(int, pf->rss_size_max, pow);
  11224. if (pf->hw.func_caps.rss) {
  11225. set_bit(I40E_FLAG_RSS_ENA, pf->flags);
  11226. pf->alloc_rss_size = min_t(int, pf->rss_size_max,
  11227. num_online_cpus());
  11228. }
  11229. /* MFP mode enabled */
  11230. if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
  11231. set_bit(I40E_FLAG_MFP_ENA, pf->flags);
  11232. dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
  11233. if (i40e_get_partition_bw_setting(pf)) {
  11234. dev_warn(&pf->pdev->dev,
  11235. "Could not get partition bw settings\n");
  11236. } else {
  11237. dev_info(&pf->pdev->dev,
  11238. "Partition BW Min = %8.8x, Max = %8.8x\n",
  11239. pf->min_bw, pf->max_bw);
  11240. /* nudge the Tx scheduler */
  11241. i40e_set_partition_bw_setting(pf);
  11242. }
  11243. }
  11244. if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
  11245. (pf->hw.func_caps.fd_filters_best_effort > 0)) {
  11246. set_bit(I40E_FLAG_FD_ATR_ENA, pf->flags);
  11247. if (test_bit(I40E_FLAG_MFP_ENA, pf->flags) &&
  11248. pf->hw.num_partitions > 1)
  11249. dev_info(&pf->pdev->dev,
  11250. "Flow Director Sideband mode Disabled in MFP mode\n");
  11251. else
  11252. set_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
  11253. pf->fdir_pf_filter_count =
  11254. pf->hw.func_caps.fd_filters_guaranteed;
  11255. pf->hw.fdir_shared_filter_count =
  11256. pf->hw.func_caps.fd_filters_best_effort;
  11257. }
  11258. /* Enable HW ATR eviction if possible */
  11259. if (test_bit(I40E_HW_CAP_ATR_EVICT, pf->hw.caps))
  11260. set_bit(I40E_FLAG_HW_ATR_EVICT_ENA, pf->flags);
  11261. if (pf->hw.func_caps.vmdq && num_online_cpus() != 1) {
  11262. pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
  11263. set_bit(I40E_FLAG_VMDQ_ENA, pf->flags);
  11264. pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
  11265. }
  11266. if (pf->hw.func_caps.iwarp && num_online_cpus() != 1) {
  11267. set_bit(I40E_FLAG_IWARP_ENA, pf->flags);
  11268. /* IWARP needs one extra vector for CQP just like MISC.*/
  11269. pf->num_iwarp_msix = (int)num_online_cpus() + 1;
  11270. }
  11271. /* Stopping FW LLDP engine is supported on XL710 and X722
  11272. * starting from FW versions determined in i40e_init_adminq.
  11273. * Stopping the FW LLDP engine is not supported on XL710
  11274. * if NPAR is functioning so unset this hw flag in this case.
  11275. */
  11276. if (pf->hw.mac.type == I40E_MAC_XL710 &&
  11277. pf->hw.func_caps.npar_enable)
  11278. clear_bit(I40E_HW_CAP_FW_LLDP_STOPPABLE, pf->hw.caps);
  11279. #ifdef CONFIG_PCI_IOV
  11280. if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
  11281. pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
  11282. set_bit(I40E_FLAG_SRIOV_ENA, pf->flags);
  11283. pf->num_req_vfs = min_t(int,
  11284. pf->hw.func_caps.num_vfs,
  11285. I40E_MAX_VF_COUNT);
  11286. }
  11287. #endif /* CONFIG_PCI_IOV */
  11288. pf->lan_veb = I40E_NO_VEB;
  11289. pf->lan_vsi = I40E_NO_VSI;
  11290. /* By default FW has this off for performance reasons */
  11291. clear_bit(I40E_FLAG_VEB_STATS_ENA, pf->flags);
  11292. /* set up queue assignment tracking */
  11293. size = sizeof(struct i40e_lump_tracking)
  11294. + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
  11295. pf->qp_pile = kzalloc(size, GFP_KERNEL);
  11296. if (!pf->qp_pile) {
  11297. err = -ENOMEM;
  11298. goto sw_init_done;
  11299. }
  11300. pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
  11301. pf->tx_timeout_recovery_level = 1;
  11302. if (pf->hw.mac.type != I40E_MAC_X722 &&
  11303. i40e_is_total_port_shutdown_enabled(pf)) {
  11304. /* Link down on close must be on when total port shutdown
  11305. * is enabled for a given port
  11306. */
  11307. set_bit(I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENA, pf->flags);
  11308. set_bit(I40E_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags);
  11309. dev_info(&pf->pdev->dev,
  11310. "total-port-shutdown was enabled, link-down-on-close is forced on\n");
  11311. }
  11312. mutex_init(&pf->switch_mutex);
  11313. sw_init_done:
  11314. return err;
  11315. }
  11316. /**
  11317. * i40e_set_ntuple - set the ntuple feature flag and take action
  11318. * @pf: board private structure to initialize
  11319. * @features: the feature set that the stack is suggesting
  11320. *
  11321. * returns a bool to indicate if reset needs to happen
  11322. **/
  11323. bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
  11324. {
  11325. bool need_reset = false;
  11326. /* Check if Flow Director n-tuple support was enabled or disabled. If
  11327. * the state changed, we need to reset.
  11328. */
  11329. if (features & NETIF_F_NTUPLE) {
  11330. /* Enable filters and mark for reset */
  11331. if (!test_bit(I40E_FLAG_FD_SB_ENA, pf->flags))
  11332. need_reset = true;
  11333. /* enable FD_SB only if there is MSI-X vector and no cloud
  11334. * filters exist
  11335. */
  11336. if (pf->num_fdsb_msix > 0 && !pf->num_cloud_filters) {
  11337. set_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
  11338. clear_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
  11339. }
  11340. } else {
  11341. /* turn off filters, mark for reset and clear SW filter list */
  11342. if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags)) {
  11343. need_reset = true;
  11344. i40e_fdir_filter_exit(pf);
  11345. }
  11346. clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
  11347. clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state);
  11348. set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
  11349. /* reset fd counters */
  11350. pf->fd_add_err = 0;
  11351. pf->fd_atr_cnt = 0;
  11352. /* if ATR was auto disabled it can be re-enabled. */
  11353. if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
  11354. if (test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags) &&
  11355. (I40E_DEBUG_FD & pf->hw.debug_mask))
  11356. dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
  11357. }
  11358. return need_reset;
  11359. }
  11360. /**
  11361. * i40e_clear_rss_lut - clear the rx hash lookup table
  11362. * @vsi: the VSI being configured
  11363. **/
  11364. static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
  11365. {
  11366. struct i40e_pf *pf = vsi->back;
  11367. struct i40e_hw *hw = &pf->hw;
  11368. u16 vf_id = vsi->vf_id;
  11369. u8 i;
  11370. if (vsi->type == I40E_VSI_MAIN) {
  11371. for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
  11372. wr32(hw, I40E_PFQF_HLUT(i), 0);
  11373. } else if (vsi->type == I40E_VSI_SRIOV) {
  11374. for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
  11375. i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
  11376. } else {
  11377. dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
  11378. }
  11379. }
  11380. /**
  11381. * i40e_set_loopback - turn on/off loopback mode on underlying PF
  11382. * @vsi: ptr to VSI
  11383. * @ena: flag to indicate the on/off setting
  11384. */
  11385. static int i40e_set_loopback(struct i40e_vsi *vsi, bool ena)
  11386. {
  11387. bool if_running = netif_running(vsi->netdev) &&
  11388. !test_and_set_bit(__I40E_VSI_DOWN, vsi->state);
  11389. int ret;
  11390. if (if_running)
  11391. i40e_down(vsi);
  11392. ret = i40e_aq_set_mac_loopback(&vsi->back->hw, ena, NULL);
  11393. if (ret)
  11394. netdev_err(vsi->netdev, "Failed to toggle loopback state\n");
  11395. if (if_running)
  11396. i40e_up(vsi);
  11397. return ret;
  11398. }
  11399. /**
  11400. * i40e_set_features - set the netdev feature flags
  11401. * @netdev: ptr to the netdev being adjusted
  11402. * @features: the feature set that the stack is suggesting
  11403. * Note: expects to be called while under rtnl_lock()
  11404. **/
  11405. static int i40e_set_features(struct net_device *netdev,
  11406. netdev_features_t features)
  11407. {
  11408. struct i40e_netdev_priv *np = netdev_priv(netdev);
  11409. struct i40e_vsi *vsi = np->vsi;
  11410. struct i40e_pf *pf = vsi->back;
  11411. bool need_reset;
  11412. if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
  11413. i40e_pf_config_rss(pf);
  11414. else if (!(features & NETIF_F_RXHASH) &&
  11415. netdev->features & NETIF_F_RXHASH)
  11416. i40e_clear_rss_lut(vsi);
  11417. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  11418. i40e_vlan_stripping_enable(vsi);
  11419. else
  11420. i40e_vlan_stripping_disable(vsi);
  11421. if (!(features & NETIF_F_HW_TC) &&
  11422. (netdev->features & NETIF_F_HW_TC) && pf->num_cloud_filters) {
  11423. dev_err(&pf->pdev->dev,
  11424. "Offloaded tc filters active, can't turn hw_tc_offload off");
  11425. return -EINVAL;
  11426. }
  11427. if (!(features & NETIF_F_HW_L2FW_DOFFLOAD) && vsi->macvlan_cnt)
  11428. i40e_del_all_macvlans(vsi);
  11429. need_reset = i40e_set_ntuple(pf, features);
  11430. if (need_reset)
  11431. i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
  11432. if ((features ^ netdev->features) & NETIF_F_LOOPBACK)
  11433. return i40e_set_loopback(vsi, !!(features & NETIF_F_LOOPBACK));
  11434. return 0;
  11435. }
  11436. static int i40e_udp_tunnel_set_port(struct net_device *netdev,
  11437. unsigned int table, unsigned int idx,
  11438. struct udp_tunnel_info *ti)
  11439. {
  11440. struct i40e_netdev_priv *np = netdev_priv(netdev);
  11441. struct i40e_hw *hw = &np->vsi->back->hw;
  11442. u8 type, filter_index;
  11443. int ret;
  11444. type = ti->type == UDP_TUNNEL_TYPE_VXLAN ? I40E_AQC_TUNNEL_TYPE_VXLAN :
  11445. I40E_AQC_TUNNEL_TYPE_NGE;
  11446. ret = i40e_aq_add_udp_tunnel(hw, ntohs(ti->port), type, &filter_index,
  11447. NULL);
  11448. if (ret) {
  11449. netdev_info(netdev, "add UDP port failed, err %pe aq_err %s\n",
  11450. ERR_PTR(ret),
  11451. i40e_aq_str(hw, hw->aq.asq_last_status));
  11452. return -EIO;
  11453. }
  11454. udp_tunnel_nic_set_port_priv(netdev, table, idx, filter_index);
  11455. return 0;
  11456. }
  11457. static int i40e_udp_tunnel_unset_port(struct net_device *netdev,
  11458. unsigned int table, unsigned int idx,
  11459. struct udp_tunnel_info *ti)
  11460. {
  11461. struct i40e_netdev_priv *np = netdev_priv(netdev);
  11462. struct i40e_hw *hw = &np->vsi->back->hw;
  11463. int ret;
  11464. ret = i40e_aq_del_udp_tunnel(hw, ti->hw_priv, NULL);
  11465. if (ret) {
  11466. netdev_info(netdev, "delete UDP port failed, err %pe aq_err %s\n",
  11467. ERR_PTR(ret),
  11468. i40e_aq_str(hw, hw->aq.asq_last_status));
  11469. return -EIO;
  11470. }
  11471. return 0;
  11472. }
  11473. static int i40e_get_phys_port_id(struct net_device *netdev,
  11474. struct netdev_phys_item_id *ppid)
  11475. {
  11476. struct i40e_netdev_priv *np = netdev_priv(netdev);
  11477. struct i40e_pf *pf = np->vsi->back;
  11478. struct i40e_hw *hw = &pf->hw;
  11479. if (!test_bit(I40E_HW_CAP_PORT_ID_VALID, pf->hw.caps))
  11480. return -EOPNOTSUPP;
  11481. ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
  11482. memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
  11483. return 0;
  11484. }
  11485. /**
  11486. * i40e_ndo_fdb_add - add an entry to the hardware database
  11487. * @ndm: the input from the stack
  11488. * @tb: pointer to array of nladdr (unused)
  11489. * @dev: the net device pointer
  11490. * @addr: the MAC address entry being added
  11491. * @vid: VLAN ID
  11492. * @flags: instructions from stack about fdb operation
  11493. * @extack: netlink extended ack, unused currently
  11494. */
  11495. static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
  11496. struct net_device *dev,
  11497. const unsigned char *addr, u16 vid,
  11498. u16 flags,
  11499. struct netlink_ext_ack *extack)
  11500. {
  11501. struct i40e_netdev_priv *np = netdev_priv(dev);
  11502. struct i40e_pf *pf = np->vsi->back;
  11503. int err = 0;
  11504. if (!test_bit(I40E_FLAG_SRIOV_ENA, pf->flags))
  11505. return -EOPNOTSUPP;
  11506. if (vid) {
  11507. pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
  11508. return -EINVAL;
  11509. }
  11510. /* Hardware does not support aging addresses so if a
  11511. * ndm_state is given only allow permanent addresses
  11512. */
  11513. if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
  11514. netdev_info(dev, "FDB only supports static addresses\n");
  11515. return -EINVAL;
  11516. }
  11517. if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
  11518. err = dev_uc_add_excl(dev, addr);
  11519. else if (is_multicast_ether_addr(addr))
  11520. err = dev_mc_add_excl(dev, addr);
  11521. else
  11522. err = -EINVAL;
  11523. /* Only return duplicate errors if NLM_F_EXCL is set */
  11524. if (err == -EEXIST && !(flags & NLM_F_EXCL))
  11525. err = 0;
  11526. return err;
  11527. }
  11528. /**
  11529. * i40e_ndo_bridge_setlink - Set the hardware bridge mode
  11530. * @dev: the netdev being configured
  11531. * @nlh: RTNL message
  11532. * @flags: bridge flags
  11533. * @extack: netlink extended ack
  11534. *
  11535. * Inserts a new hardware bridge if not already created and
  11536. * enables the bridging mode requested (VEB or VEPA). If the
  11537. * hardware bridge has already been inserted and the request
  11538. * is to change the mode then that requires a PF reset to
  11539. * allow rebuild of the components with required hardware
  11540. * bridge mode enabled.
  11541. *
  11542. * Note: expects to be called while under rtnl_lock()
  11543. **/
  11544. static int i40e_ndo_bridge_setlink(struct net_device *dev,
  11545. struct nlmsghdr *nlh,
  11546. u16 flags,
  11547. struct netlink_ext_ack *extack)
  11548. {
  11549. struct i40e_netdev_priv *np = netdev_priv(dev);
  11550. struct i40e_vsi *vsi = np->vsi;
  11551. struct i40e_pf *pf = vsi->back;
  11552. struct nlattr *attr, *br_spec;
  11553. struct i40e_veb *veb;
  11554. int rem;
  11555. /* Only for PF VSI for now */
  11556. if (vsi->type != I40E_VSI_MAIN)
  11557. return -EOPNOTSUPP;
  11558. /* Find the HW bridge for PF VSI */
  11559. veb = i40e_pf_get_veb_by_seid(pf, vsi->uplink_seid);
  11560. br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
  11561. if (!br_spec)
  11562. return -EINVAL;
  11563. nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) {
  11564. __u16 mode = nla_get_u16(attr);
  11565. if ((mode != BRIDGE_MODE_VEPA) &&
  11566. (mode != BRIDGE_MODE_VEB))
  11567. return -EINVAL;
  11568. /* Insert a new HW bridge */
  11569. if (!veb) {
  11570. veb = i40e_veb_setup(pf, vsi->uplink_seid, vsi->seid,
  11571. vsi->tc_config.enabled_tc);
  11572. if (veb) {
  11573. veb->bridge_mode = mode;
  11574. i40e_config_bridge_mode(veb);
  11575. } else {
  11576. /* No Bridge HW offload available */
  11577. return -ENOENT;
  11578. }
  11579. break;
  11580. } else if (mode != veb->bridge_mode) {
  11581. /* Existing HW bridge but different mode needs reset */
  11582. veb->bridge_mode = mode;
  11583. /* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
  11584. if (mode == BRIDGE_MODE_VEB)
  11585. set_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags);
  11586. else
  11587. clear_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags);
  11588. i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
  11589. break;
  11590. }
  11591. }
  11592. return 0;
  11593. }
  11594. /**
  11595. * i40e_ndo_bridge_getlink - Get the hardware bridge mode
  11596. * @skb: skb buff
  11597. * @pid: process id
  11598. * @seq: RTNL message seq #
  11599. * @dev: the netdev being configured
  11600. * @filter_mask: unused
  11601. * @nlflags: netlink flags passed in
  11602. *
  11603. * Return the mode in which the hardware bridge is operating in
  11604. * i.e VEB or VEPA.
  11605. **/
  11606. static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
  11607. struct net_device *dev,
  11608. u32 __always_unused filter_mask,
  11609. int nlflags)
  11610. {
  11611. struct i40e_netdev_priv *np = netdev_priv(dev);
  11612. struct i40e_vsi *vsi = np->vsi;
  11613. struct i40e_pf *pf = vsi->back;
  11614. struct i40e_veb *veb;
  11615. /* Only for PF VSI for now */
  11616. if (vsi->type != I40E_VSI_MAIN)
  11617. return -EOPNOTSUPP;
  11618. /* Find the HW bridge for the PF VSI */
  11619. veb = i40e_pf_get_veb_by_seid(pf, vsi->uplink_seid);
  11620. if (!veb)
  11621. return 0;
  11622. return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
  11623. 0, 0, nlflags, filter_mask, NULL);
  11624. }
  11625. /**
  11626. * i40e_features_check - Validate encapsulated packet conforms to limits
  11627. * @skb: skb buff
  11628. * @dev: This physical port's netdev
  11629. * @features: Offload features that the stack believes apply
  11630. **/
  11631. static netdev_features_t i40e_features_check(struct sk_buff *skb,
  11632. struct net_device *dev,
  11633. netdev_features_t features)
  11634. {
  11635. size_t len;
  11636. /* No point in doing any of this if neither checksum nor GSO are
  11637. * being requested for this frame. We can rule out both by just
  11638. * checking for CHECKSUM_PARTIAL
  11639. */
  11640. if (skb->ip_summed != CHECKSUM_PARTIAL)
  11641. return features;
  11642. /* We cannot support GSO if the MSS is going to be less than
  11643. * 64 bytes. If it is then we need to drop support for GSO.
  11644. */
  11645. if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
  11646. features &= ~NETIF_F_GSO_MASK;
  11647. /* MACLEN can support at most 63 words */
  11648. len = skb_network_offset(skb);
  11649. if (len & ~(63 * 2))
  11650. goto out_err;
  11651. /* IPLEN and EIPLEN can support at most 127 dwords */
  11652. len = skb_network_header_len(skb);
  11653. if (len & ~(127 * 4))
  11654. goto out_err;
  11655. if (skb->encapsulation) {
  11656. /* L4TUNLEN can support 127 words */
  11657. len = skb_inner_network_header(skb) - skb_transport_header(skb);
  11658. if (len & ~(127 * 2))
  11659. goto out_err;
  11660. /* IPLEN can support at most 127 dwords */
  11661. len = skb_inner_transport_header(skb) -
  11662. skb_inner_network_header(skb);
  11663. if (len & ~(127 * 4))
  11664. goto out_err;
  11665. }
  11666. /* No need to validate L4LEN as TCP is the only protocol with a
  11667. * flexible value and we support all possible values supported
  11668. * by TCP, which is at most 15 dwords
  11669. */
  11670. return features;
  11671. out_err:
  11672. return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
  11673. }
  11674. /**
  11675. * i40e_xdp_setup - add/remove an XDP program
  11676. * @vsi: VSI to changed
  11677. * @prog: XDP program
  11678. * @extack: netlink extended ack
  11679. **/
  11680. static int i40e_xdp_setup(struct i40e_vsi *vsi, struct bpf_prog *prog,
  11681. struct netlink_ext_ack *extack)
  11682. {
  11683. int frame_size = i40e_max_vsi_frame_size(vsi, prog);
  11684. struct i40e_pf *pf = vsi->back;
  11685. struct bpf_prog *old_prog;
  11686. bool need_reset;
  11687. int i;
  11688. /* VSI shall be deleted in a moment, block loading new programs */
  11689. if (prog && test_bit(__I40E_IN_REMOVE, pf->state))
  11690. return -EINVAL;
  11691. /* Don't allow frames that span over multiple buffers */
  11692. if (vsi->netdev->mtu > frame_size - I40E_PACKET_HDR_PAD) {
  11693. NL_SET_ERR_MSG_MOD(extack, "MTU too large for linear frames and XDP prog does not support frags");
  11694. return -EINVAL;
  11695. }
  11696. /* When turning XDP on->off/off->on we reset and rebuild the rings. */
  11697. need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
  11698. if (need_reset)
  11699. i40e_prep_for_reset(pf);
  11700. old_prog = xchg(&vsi->xdp_prog, prog);
  11701. if (need_reset) {
  11702. if (!prog) {
  11703. xdp_features_clear_redirect_target(vsi->netdev);
  11704. /* Wait until ndo_xsk_wakeup completes. */
  11705. synchronize_rcu();
  11706. }
  11707. i40e_reset_and_rebuild(pf, true, true);
  11708. }
  11709. if (!i40e_enabled_xdp_vsi(vsi) && prog) {
  11710. if (i40e_realloc_rx_bi_zc(vsi, true))
  11711. return -ENOMEM;
  11712. } else if (i40e_enabled_xdp_vsi(vsi) && !prog) {
  11713. if (i40e_realloc_rx_bi_zc(vsi, false))
  11714. return -ENOMEM;
  11715. }
  11716. for (i = 0; i < vsi->num_queue_pairs; i++)
  11717. WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
  11718. if (old_prog)
  11719. bpf_prog_put(old_prog);
  11720. /* Kick start the NAPI context if there is an AF_XDP socket open
  11721. * on that queue id. This so that receiving will start.
  11722. */
  11723. if (need_reset && prog) {
  11724. for (i = 0; i < vsi->num_queue_pairs; i++)
  11725. if (vsi->xdp_rings[i]->xsk_pool)
  11726. (void)i40e_xsk_wakeup(vsi->netdev, i,
  11727. XDP_WAKEUP_RX);
  11728. xdp_features_set_redirect_target(vsi->netdev, true);
  11729. }
  11730. return 0;
  11731. }
  11732. /**
  11733. * i40e_enter_busy_conf - Enters busy config state
  11734. * @vsi: vsi
  11735. *
  11736. * Returns 0 on success, <0 for failure.
  11737. **/
  11738. static int i40e_enter_busy_conf(struct i40e_vsi *vsi)
  11739. {
  11740. struct i40e_pf *pf = vsi->back;
  11741. int timeout = 50;
  11742. while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state)) {
  11743. timeout--;
  11744. if (!timeout)
  11745. return -EBUSY;
  11746. usleep_range(1000, 2000);
  11747. }
  11748. return 0;
  11749. }
  11750. /**
  11751. * i40e_exit_busy_conf - Exits busy config state
  11752. * @vsi: vsi
  11753. **/
  11754. static void i40e_exit_busy_conf(struct i40e_vsi *vsi)
  11755. {
  11756. struct i40e_pf *pf = vsi->back;
  11757. clear_bit(__I40E_CONFIG_BUSY, pf->state);
  11758. }
  11759. /**
  11760. * i40e_queue_pair_reset_stats - Resets all statistics for a queue pair
  11761. * @vsi: vsi
  11762. * @queue_pair: queue pair
  11763. **/
  11764. static void i40e_queue_pair_reset_stats(struct i40e_vsi *vsi, int queue_pair)
  11765. {
  11766. memset(&vsi->rx_rings[queue_pair]->rx_stats, 0,
  11767. sizeof(vsi->rx_rings[queue_pair]->rx_stats));
  11768. memset(&vsi->tx_rings[queue_pair]->stats, 0,
  11769. sizeof(vsi->tx_rings[queue_pair]->stats));
  11770. if (i40e_enabled_xdp_vsi(vsi)) {
  11771. memset(&vsi->xdp_rings[queue_pair]->stats, 0,
  11772. sizeof(vsi->xdp_rings[queue_pair]->stats));
  11773. }
  11774. }
  11775. /**
  11776. * i40e_queue_pair_clean_rings - Cleans all the rings of a queue pair
  11777. * @vsi: vsi
  11778. * @queue_pair: queue pair
  11779. **/
  11780. static void i40e_queue_pair_clean_rings(struct i40e_vsi *vsi, int queue_pair)
  11781. {
  11782. i40e_clean_tx_ring(vsi->tx_rings[queue_pair]);
  11783. if (i40e_enabled_xdp_vsi(vsi)) {
  11784. /* Make sure that in-progress ndo_xdp_xmit calls are
  11785. * completed.
  11786. */
  11787. synchronize_rcu();
  11788. i40e_clean_tx_ring(vsi->xdp_rings[queue_pair]);
  11789. }
  11790. i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
  11791. }
  11792. /**
  11793. * i40e_queue_pair_toggle_napi - Enables/disables NAPI for a queue pair
  11794. * @vsi: vsi
  11795. * @queue_pair: queue pair
  11796. * @enable: true for enable, false for disable
  11797. **/
  11798. static void i40e_queue_pair_toggle_napi(struct i40e_vsi *vsi, int queue_pair,
  11799. bool enable)
  11800. {
  11801. struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
  11802. struct i40e_q_vector *q_vector = rxr->q_vector;
  11803. if (!vsi->netdev)
  11804. return;
  11805. /* All rings in a qp belong to the same qvector. */
  11806. if (q_vector->rx.ring || q_vector->tx.ring) {
  11807. if (enable)
  11808. napi_enable(&q_vector->napi);
  11809. else
  11810. napi_disable(&q_vector->napi);
  11811. }
  11812. }
  11813. /**
  11814. * i40e_queue_pair_toggle_rings - Enables/disables all rings for a queue pair
  11815. * @vsi: vsi
  11816. * @queue_pair: queue pair
  11817. * @enable: true for enable, false for disable
  11818. *
  11819. * Returns 0 on success, <0 on failure.
  11820. **/
  11821. static int i40e_queue_pair_toggle_rings(struct i40e_vsi *vsi, int queue_pair,
  11822. bool enable)
  11823. {
  11824. struct i40e_pf *pf = vsi->back;
  11825. int pf_q, ret = 0;
  11826. pf_q = vsi->base_queue + queue_pair;
  11827. ret = i40e_control_wait_tx_q(vsi->seid, pf, pf_q,
  11828. false /*is xdp*/, enable);
  11829. if (ret) {
  11830. dev_info(&pf->pdev->dev,
  11831. "VSI seid %d Tx ring %d %sable timeout\n",
  11832. vsi->seid, pf_q, (enable ? "en" : "dis"));
  11833. return ret;
  11834. }
  11835. i40e_control_rx_q(pf, pf_q, enable);
  11836. ret = i40e_pf_rxq_wait(pf, pf_q, enable);
  11837. if (ret) {
  11838. dev_info(&pf->pdev->dev,
  11839. "VSI seid %d Rx ring %d %sable timeout\n",
  11840. vsi->seid, pf_q, (enable ? "en" : "dis"));
  11841. return ret;
  11842. }
  11843. /* Due to HW errata, on Rx disable only, the register can
  11844. * indicate done before it really is. Needs 50ms to be sure
  11845. */
  11846. if (!enable)
  11847. mdelay(50);
  11848. if (!i40e_enabled_xdp_vsi(vsi))
  11849. return ret;
  11850. ret = i40e_control_wait_tx_q(vsi->seid, pf,
  11851. pf_q + vsi->alloc_queue_pairs,
  11852. true /*is xdp*/, enable);
  11853. if (ret) {
  11854. dev_info(&pf->pdev->dev,
  11855. "VSI seid %d XDP Tx ring %d %sable timeout\n",
  11856. vsi->seid, pf_q, (enable ? "en" : "dis"));
  11857. }
  11858. return ret;
  11859. }
  11860. /**
  11861. * i40e_queue_pair_enable_irq - Enables interrupts for a queue pair
  11862. * @vsi: vsi
  11863. * @queue_pair: queue_pair
  11864. **/
  11865. static void i40e_queue_pair_enable_irq(struct i40e_vsi *vsi, int queue_pair)
  11866. {
  11867. struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
  11868. struct i40e_pf *pf = vsi->back;
  11869. struct i40e_hw *hw = &pf->hw;
  11870. /* All rings in a qp belong to the same qvector. */
  11871. if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
  11872. i40e_irq_dynamic_enable(vsi, rxr->q_vector->v_idx);
  11873. else
  11874. i40e_irq_dynamic_enable_icr0(pf);
  11875. i40e_flush(hw);
  11876. }
  11877. /**
  11878. * i40e_queue_pair_disable_irq - Disables interrupts for a queue pair
  11879. * @vsi: vsi
  11880. * @queue_pair: queue_pair
  11881. **/
  11882. static void i40e_queue_pair_disable_irq(struct i40e_vsi *vsi, int queue_pair)
  11883. {
  11884. struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
  11885. struct i40e_pf *pf = vsi->back;
  11886. struct i40e_hw *hw = &pf->hw;
  11887. /* For simplicity, instead of removing the qp interrupt causes
  11888. * from the interrupt linked list, we simply disable the interrupt, and
  11889. * leave the list intact.
  11890. *
  11891. * All rings in a qp belong to the same qvector.
  11892. */
  11893. if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
  11894. u32 intpf = vsi->base_vector + rxr->q_vector->v_idx;
  11895. wr32(hw, I40E_PFINT_DYN_CTLN(intpf - 1), 0);
  11896. i40e_flush(hw);
  11897. synchronize_irq(pf->msix_entries[intpf].vector);
  11898. } else {
  11899. /* Legacy and MSI mode - this stops all interrupt handling */
  11900. wr32(hw, I40E_PFINT_ICR0_ENA, 0);
  11901. wr32(hw, I40E_PFINT_DYN_CTL0, 0);
  11902. i40e_flush(hw);
  11903. synchronize_irq(pf->pdev->irq);
  11904. }
  11905. }
  11906. /**
  11907. * i40e_queue_pair_disable - Disables a queue pair
  11908. * @vsi: vsi
  11909. * @queue_pair: queue pair
  11910. *
  11911. * Returns 0 on success, <0 on failure.
  11912. **/
  11913. int i40e_queue_pair_disable(struct i40e_vsi *vsi, int queue_pair)
  11914. {
  11915. int err;
  11916. err = i40e_enter_busy_conf(vsi);
  11917. if (err)
  11918. return err;
  11919. i40e_queue_pair_disable_irq(vsi, queue_pair);
  11920. i40e_queue_pair_toggle_napi(vsi, queue_pair, false /* off */);
  11921. err = i40e_queue_pair_toggle_rings(vsi, queue_pair, false /* off */);
  11922. i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
  11923. i40e_queue_pair_clean_rings(vsi, queue_pair);
  11924. i40e_queue_pair_reset_stats(vsi, queue_pair);
  11925. return err;
  11926. }
  11927. /**
  11928. * i40e_queue_pair_enable - Enables a queue pair
  11929. * @vsi: vsi
  11930. * @queue_pair: queue pair
  11931. *
  11932. * Returns 0 on success, <0 on failure.
  11933. **/
  11934. int i40e_queue_pair_enable(struct i40e_vsi *vsi, int queue_pair)
  11935. {
  11936. int err;
  11937. err = i40e_configure_tx_ring(vsi->tx_rings[queue_pair]);
  11938. if (err)
  11939. return err;
  11940. if (i40e_enabled_xdp_vsi(vsi)) {
  11941. err = i40e_configure_tx_ring(vsi->xdp_rings[queue_pair]);
  11942. if (err)
  11943. return err;
  11944. }
  11945. err = i40e_configure_rx_ring(vsi->rx_rings[queue_pair]);
  11946. if (err)
  11947. return err;
  11948. err = i40e_queue_pair_toggle_rings(vsi, queue_pair, true /* on */);
  11949. i40e_queue_pair_toggle_napi(vsi, queue_pair, true /* on */);
  11950. i40e_queue_pair_enable_irq(vsi, queue_pair);
  11951. i40e_exit_busy_conf(vsi);
  11952. return err;
  11953. }
  11954. /**
  11955. * i40e_xdp - implements ndo_bpf for i40e
  11956. * @dev: netdevice
  11957. * @xdp: XDP command
  11958. **/
  11959. static int i40e_xdp(struct net_device *dev,
  11960. struct netdev_bpf *xdp)
  11961. {
  11962. struct i40e_netdev_priv *np = netdev_priv(dev);
  11963. struct i40e_vsi *vsi = np->vsi;
  11964. if (vsi->type != I40E_VSI_MAIN)
  11965. return -EINVAL;
  11966. switch (xdp->command) {
  11967. case XDP_SETUP_PROG:
  11968. return i40e_xdp_setup(vsi, xdp->prog, xdp->extack);
  11969. case XDP_SETUP_XSK_POOL:
  11970. return i40e_xsk_pool_setup(vsi, xdp->xsk.pool,
  11971. xdp->xsk.queue_id);
  11972. default:
  11973. return -EINVAL;
  11974. }
  11975. }
  11976. static const struct net_device_ops i40e_netdev_ops = {
  11977. .ndo_open = i40e_open,
  11978. .ndo_stop = i40e_close,
  11979. .ndo_start_xmit = i40e_lan_xmit_frame,
  11980. .ndo_get_stats64 = i40e_get_netdev_stats_struct,
  11981. .ndo_set_rx_mode = i40e_set_rx_mode,
  11982. .ndo_validate_addr = eth_validate_addr,
  11983. .ndo_set_mac_address = i40e_set_mac,
  11984. .ndo_change_mtu = i40e_change_mtu,
  11985. .ndo_eth_ioctl = i40e_ioctl,
  11986. .ndo_tx_timeout = i40e_tx_timeout,
  11987. .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
  11988. .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
  11989. #ifdef CONFIG_NET_POLL_CONTROLLER
  11990. .ndo_poll_controller = i40e_netpoll,
  11991. #endif
  11992. .ndo_setup_tc = __i40e_setup_tc,
  11993. .ndo_select_queue = i40e_lan_select_queue,
  11994. .ndo_set_features = i40e_set_features,
  11995. .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
  11996. .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
  11997. .ndo_get_vf_stats = i40e_get_vf_stats,
  11998. .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
  11999. .ndo_get_vf_config = i40e_ndo_get_vf_config,
  12000. .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
  12001. .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofchk,
  12002. .ndo_set_vf_trust = i40e_ndo_set_vf_trust,
  12003. .ndo_get_phys_port_id = i40e_get_phys_port_id,
  12004. .ndo_fdb_add = i40e_ndo_fdb_add,
  12005. .ndo_features_check = i40e_features_check,
  12006. .ndo_bridge_getlink = i40e_ndo_bridge_getlink,
  12007. .ndo_bridge_setlink = i40e_ndo_bridge_setlink,
  12008. .ndo_bpf = i40e_xdp,
  12009. .ndo_xdp_xmit = i40e_xdp_xmit,
  12010. .ndo_xsk_wakeup = i40e_xsk_wakeup,
  12011. .ndo_dfwd_add_station = i40e_fwd_add,
  12012. .ndo_dfwd_del_station = i40e_fwd_del,
  12013. };
  12014. /**
  12015. * i40e_config_netdev - Setup the netdev flags
  12016. * @vsi: the VSI being configured
  12017. *
  12018. * Returns 0 on success, negative value on failure
  12019. **/
  12020. static int i40e_config_netdev(struct i40e_vsi *vsi)
  12021. {
  12022. struct i40e_pf *pf = vsi->back;
  12023. struct i40e_hw *hw = &pf->hw;
  12024. struct i40e_netdev_priv *np;
  12025. struct net_device *netdev;
  12026. u8 broadcast[ETH_ALEN];
  12027. u8 mac_addr[ETH_ALEN];
  12028. int etherdev_size;
  12029. netdev_features_t hw_enc_features;
  12030. netdev_features_t hw_features;
  12031. etherdev_size = sizeof(struct i40e_netdev_priv);
  12032. netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
  12033. if (!netdev)
  12034. return -ENOMEM;
  12035. vsi->netdev = netdev;
  12036. np = netdev_priv(netdev);
  12037. np->vsi = vsi;
  12038. hw_enc_features = NETIF_F_SG |
  12039. NETIF_F_HW_CSUM |
  12040. NETIF_F_HIGHDMA |
  12041. NETIF_F_SOFT_FEATURES |
  12042. NETIF_F_TSO |
  12043. NETIF_F_TSO_ECN |
  12044. NETIF_F_TSO6 |
  12045. NETIF_F_GSO_GRE |
  12046. NETIF_F_GSO_GRE_CSUM |
  12047. NETIF_F_GSO_PARTIAL |
  12048. NETIF_F_GSO_IPXIP4 |
  12049. NETIF_F_GSO_IPXIP6 |
  12050. NETIF_F_GSO_UDP_TUNNEL |
  12051. NETIF_F_GSO_UDP_TUNNEL_CSUM |
  12052. NETIF_F_GSO_UDP_L4 |
  12053. NETIF_F_SCTP_CRC |
  12054. NETIF_F_RXHASH |
  12055. NETIF_F_RXCSUM |
  12056. 0;
  12057. if (!test_bit(I40E_HW_CAP_OUTER_UDP_CSUM, pf->hw.caps))
  12058. netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
  12059. netdev->udp_tunnel_nic_info = &pf->udp_tunnel_nic;
  12060. netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
  12061. netdev->hw_enc_features |= hw_enc_features;
  12062. /* record features VLANs can make use of */
  12063. netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
  12064. #define I40E_GSO_PARTIAL_FEATURES (NETIF_F_GSO_GRE | \
  12065. NETIF_F_GSO_GRE_CSUM | \
  12066. NETIF_F_GSO_IPXIP4 | \
  12067. NETIF_F_GSO_IPXIP6 | \
  12068. NETIF_F_GSO_UDP_TUNNEL | \
  12069. NETIF_F_GSO_UDP_TUNNEL_CSUM)
  12070. netdev->gso_partial_features = I40E_GSO_PARTIAL_FEATURES;
  12071. netdev->features |= NETIF_F_GSO_PARTIAL |
  12072. I40E_GSO_PARTIAL_FEATURES;
  12073. netdev->mpls_features |= NETIF_F_SG;
  12074. netdev->mpls_features |= NETIF_F_HW_CSUM;
  12075. netdev->mpls_features |= NETIF_F_TSO;
  12076. netdev->mpls_features |= NETIF_F_TSO6;
  12077. netdev->mpls_features |= I40E_GSO_PARTIAL_FEATURES;
  12078. /* enable macvlan offloads */
  12079. netdev->hw_features |= NETIF_F_HW_L2FW_DOFFLOAD;
  12080. hw_features = hw_enc_features |
  12081. NETIF_F_HW_VLAN_CTAG_TX |
  12082. NETIF_F_HW_VLAN_CTAG_RX;
  12083. if (!test_bit(I40E_FLAG_MFP_ENA, pf->flags))
  12084. hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
  12085. netdev->hw_features |= hw_features | NETIF_F_LOOPBACK;
  12086. netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
  12087. netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
  12088. netdev->features &= ~NETIF_F_HW_TC;
  12089. if (vsi->type == I40E_VSI_MAIN) {
  12090. SET_NETDEV_DEV(netdev, &pf->pdev->dev);
  12091. ether_addr_copy(mac_addr, hw->mac.perm_addr);
  12092. /* The following steps are necessary for two reasons. First,
  12093. * some older NVM configurations load a default MAC-VLAN
  12094. * filter that will accept any tagged packet, and we want to
  12095. * replace this with a normal filter. Additionally, it is
  12096. * possible our MAC address was provided by the platform using
  12097. * Open Firmware or similar.
  12098. *
  12099. * Thus, we need to remove the default filter and install one
  12100. * specific to the MAC address.
  12101. */
  12102. i40e_rm_default_mac_filter(vsi, mac_addr);
  12103. spin_lock_bh(&vsi->mac_filter_hash_lock);
  12104. i40e_add_mac_filter(vsi, mac_addr);
  12105. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  12106. netdev->xdp_features = NETDEV_XDP_ACT_BASIC |
  12107. NETDEV_XDP_ACT_REDIRECT |
  12108. NETDEV_XDP_ACT_XSK_ZEROCOPY |
  12109. NETDEV_XDP_ACT_RX_SG;
  12110. netdev->xdp_zc_max_segs = I40E_MAX_BUFFER_TXD;
  12111. } else {
  12112. /* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
  12113. * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
  12114. * the end, which is 4 bytes long, so force truncation of the
  12115. * original name by IFNAMSIZ - 4
  12116. */
  12117. struct i40e_vsi *main_vsi = i40e_pf_get_main_vsi(pf);
  12118. snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d", IFNAMSIZ - 4,
  12119. main_vsi->netdev->name);
  12120. eth_random_addr(mac_addr);
  12121. spin_lock_bh(&vsi->mac_filter_hash_lock);
  12122. i40e_add_mac_filter(vsi, mac_addr);
  12123. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  12124. }
  12125. /* Add the broadcast filter so that we initially will receive
  12126. * broadcast packets. Note that when a new VLAN is first added the
  12127. * driver will convert all filters marked I40E_VLAN_ANY into VLAN
  12128. * specific filters as part of transitioning into "vlan" operation.
  12129. * When more VLANs are added, the driver will copy each existing MAC
  12130. * filter and add it for the new VLAN.
  12131. *
  12132. * Broadcast filters are handled specially by
  12133. * i40e_sync_filters_subtask, as the driver must to set the broadcast
  12134. * promiscuous bit instead of adding this directly as a MAC/VLAN
  12135. * filter. The subtask will update the correct broadcast promiscuous
  12136. * bits as VLANs become active or inactive.
  12137. */
  12138. eth_broadcast_addr(broadcast);
  12139. spin_lock_bh(&vsi->mac_filter_hash_lock);
  12140. i40e_add_mac_filter(vsi, broadcast);
  12141. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  12142. eth_hw_addr_set(netdev, mac_addr);
  12143. ether_addr_copy(netdev->perm_addr, mac_addr);
  12144. /* i40iw_net_event() reads 16 bytes from neigh->primary_key */
  12145. netdev->neigh_priv_len = sizeof(u32) * 4;
  12146. netdev->priv_flags |= IFF_UNICAST_FLT;
  12147. netdev->priv_flags |= IFF_SUPP_NOFCS;
  12148. /* Setup netdev TC information */
  12149. i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
  12150. netdev->netdev_ops = &i40e_netdev_ops;
  12151. netdev->watchdog_timeo = 5 * HZ;
  12152. i40e_set_ethtool_ops(netdev);
  12153. /* MTU range: 68 - 9706 */
  12154. netdev->min_mtu = ETH_MIN_MTU;
  12155. netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
  12156. return 0;
  12157. }
  12158. /**
  12159. * i40e_vsi_delete - Delete a VSI from the switch
  12160. * @vsi: the VSI being removed
  12161. *
  12162. * Returns 0 on success, negative value on failure
  12163. **/
  12164. static void i40e_vsi_delete(struct i40e_vsi *vsi)
  12165. {
  12166. /* remove default VSI is not allowed */
  12167. if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
  12168. return;
  12169. i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
  12170. }
  12171. /**
  12172. * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
  12173. * @vsi: the VSI being queried
  12174. *
  12175. * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
  12176. **/
  12177. int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
  12178. {
  12179. struct i40e_veb *veb;
  12180. struct i40e_pf *pf = vsi->back;
  12181. /* Uplink is not a bridge so default to VEB */
  12182. if (vsi->veb_idx >= I40E_MAX_VEB)
  12183. return 1;
  12184. veb = pf->veb[vsi->veb_idx];
  12185. if (!veb) {
  12186. dev_info(&pf->pdev->dev,
  12187. "There is no veb associated with the bridge\n");
  12188. return -ENOENT;
  12189. }
  12190. /* Uplink is a bridge in VEPA mode */
  12191. if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
  12192. return 0;
  12193. } else {
  12194. /* Uplink is a bridge in VEB mode */
  12195. return 1;
  12196. }
  12197. /* VEPA is now default bridge, so return 0 */
  12198. return 0;
  12199. }
  12200. /**
  12201. * i40e_add_vsi - Add a VSI to the switch
  12202. * @vsi: the VSI being configured
  12203. *
  12204. * This initializes a VSI context depending on the VSI type to be added and
  12205. * passes it down to the add_vsi aq command.
  12206. **/
  12207. static int i40e_add_vsi(struct i40e_vsi *vsi)
  12208. {
  12209. int ret = -ENODEV;
  12210. struct i40e_pf *pf = vsi->back;
  12211. struct i40e_hw *hw = &pf->hw;
  12212. struct i40e_vsi_context ctxt;
  12213. struct i40e_mac_filter *f;
  12214. struct hlist_node *h;
  12215. int bkt;
  12216. u8 enabled_tc = 0x1; /* TC0 enabled */
  12217. int f_count = 0;
  12218. memset(&ctxt, 0, sizeof(ctxt));
  12219. switch (vsi->type) {
  12220. case I40E_VSI_MAIN:
  12221. /* The PF's main VSI is already setup as part of the
  12222. * device initialization, so we'll not bother with
  12223. * the add_vsi call, but we will retrieve the current
  12224. * VSI context.
  12225. */
  12226. ctxt.seid = pf->main_vsi_seid;
  12227. ctxt.pf_num = pf->hw.pf_id;
  12228. ctxt.vf_num = 0;
  12229. ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
  12230. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  12231. if (ret) {
  12232. dev_info(&pf->pdev->dev,
  12233. "couldn't get PF vsi config, err %pe aq_err %s\n",
  12234. ERR_PTR(ret),
  12235. i40e_aq_str(&pf->hw,
  12236. pf->hw.aq.asq_last_status));
  12237. return -ENOENT;
  12238. }
  12239. vsi->info = ctxt.info;
  12240. vsi->info.valid_sections = 0;
  12241. vsi->seid = ctxt.seid;
  12242. vsi->id = ctxt.vsi_number;
  12243. enabled_tc = i40e_pf_get_tc_map(pf);
  12244. /* Source pruning is enabled by default, so the flag is
  12245. * negative logic - if it's set, we need to fiddle with
  12246. * the VSI to disable source pruning.
  12247. */
  12248. if (test_bit(I40E_FLAG_SOURCE_PRUNING_DIS, pf->flags)) {
  12249. memset(&ctxt, 0, sizeof(ctxt));
  12250. ctxt.seid = pf->main_vsi_seid;
  12251. ctxt.pf_num = pf->hw.pf_id;
  12252. ctxt.vf_num = 0;
  12253. ctxt.info.valid_sections |=
  12254. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  12255. ctxt.info.switch_id =
  12256. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
  12257. ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
  12258. if (ret) {
  12259. dev_info(&pf->pdev->dev,
  12260. "update vsi failed, err %d aq_err %s\n",
  12261. ret,
  12262. i40e_aq_str(&pf->hw,
  12263. pf->hw.aq.asq_last_status));
  12264. ret = -ENOENT;
  12265. goto err;
  12266. }
  12267. }
  12268. /* MFP mode setup queue map and update VSI */
  12269. if (test_bit(I40E_FLAG_MFP_ENA, pf->flags) &&
  12270. !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
  12271. memset(&ctxt, 0, sizeof(ctxt));
  12272. ctxt.seid = pf->main_vsi_seid;
  12273. ctxt.pf_num = pf->hw.pf_id;
  12274. ctxt.vf_num = 0;
  12275. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
  12276. ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
  12277. if (ret) {
  12278. dev_info(&pf->pdev->dev,
  12279. "update vsi failed, err %pe aq_err %s\n",
  12280. ERR_PTR(ret),
  12281. i40e_aq_str(&pf->hw,
  12282. pf->hw.aq.asq_last_status));
  12283. ret = -ENOENT;
  12284. goto err;
  12285. }
  12286. /* update the local VSI info queue map */
  12287. i40e_vsi_update_queue_map(vsi, &ctxt);
  12288. vsi->info.valid_sections = 0;
  12289. } else {
  12290. /* Default/Main VSI is only enabled for TC0
  12291. * reconfigure it to enable all TCs that are
  12292. * available on the port in SFP mode.
  12293. * For MFP case the iSCSI PF would use this
  12294. * flow to enable LAN+iSCSI TC.
  12295. */
  12296. ret = i40e_vsi_config_tc(vsi, enabled_tc);
  12297. if (ret) {
  12298. /* Single TC condition is not fatal,
  12299. * message and continue
  12300. */
  12301. dev_info(&pf->pdev->dev,
  12302. "failed to configure TCs for main VSI tc_map 0x%08x, err %pe aq_err %s\n",
  12303. enabled_tc,
  12304. ERR_PTR(ret),
  12305. i40e_aq_str(&pf->hw,
  12306. pf->hw.aq.asq_last_status));
  12307. }
  12308. }
  12309. break;
  12310. case I40E_VSI_FDIR:
  12311. ctxt.pf_num = hw->pf_id;
  12312. ctxt.vf_num = 0;
  12313. ctxt.uplink_seid = vsi->uplink_seid;
  12314. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  12315. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  12316. if (test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags) &&
  12317. (i40e_is_vsi_uplink_mode_veb(vsi))) {
  12318. ctxt.info.valid_sections |=
  12319. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  12320. ctxt.info.switch_id =
  12321. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  12322. }
  12323. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
  12324. break;
  12325. case I40E_VSI_VMDQ2:
  12326. ctxt.pf_num = hw->pf_id;
  12327. ctxt.vf_num = 0;
  12328. ctxt.uplink_seid = vsi->uplink_seid;
  12329. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  12330. ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
  12331. /* This VSI is connected to VEB so the switch_id
  12332. * should be set to zero by default.
  12333. */
  12334. if (i40e_is_vsi_uplink_mode_veb(vsi)) {
  12335. ctxt.info.valid_sections |=
  12336. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  12337. ctxt.info.switch_id =
  12338. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  12339. }
  12340. /* Setup the VSI tx/rx queue map for TC0 only for now */
  12341. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
  12342. break;
  12343. case I40E_VSI_SRIOV:
  12344. ctxt.pf_num = hw->pf_id;
  12345. ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
  12346. ctxt.uplink_seid = vsi->uplink_seid;
  12347. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  12348. ctxt.flags = I40E_AQ_VSI_TYPE_VF;
  12349. /* This VSI is connected to VEB so the switch_id
  12350. * should be set to zero by default.
  12351. */
  12352. if (i40e_is_vsi_uplink_mode_veb(vsi)) {
  12353. ctxt.info.valid_sections |=
  12354. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  12355. ctxt.info.switch_id =
  12356. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  12357. }
  12358. if (test_bit(I40E_FLAG_IWARP_ENA, vsi->back->flags)) {
  12359. ctxt.info.valid_sections |=
  12360. cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
  12361. ctxt.info.queueing_opt_flags |=
  12362. (I40E_AQ_VSI_QUE_OPT_TCP_ENA |
  12363. I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
  12364. }
  12365. ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  12366. ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
  12367. if (pf->vf[vsi->vf_id].spoofchk) {
  12368. ctxt.info.valid_sections |=
  12369. cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
  12370. ctxt.info.sec_flags |=
  12371. (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
  12372. I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
  12373. }
  12374. /* Setup the VSI tx/rx queue map for TC0 only for now */
  12375. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
  12376. break;
  12377. case I40E_VSI_IWARP:
  12378. /* send down message to iWARP */
  12379. break;
  12380. default:
  12381. return -ENODEV;
  12382. }
  12383. if (vsi->type != I40E_VSI_MAIN) {
  12384. ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
  12385. if (ret) {
  12386. dev_info(&vsi->back->pdev->dev,
  12387. "add vsi failed, err %pe aq_err %s\n",
  12388. ERR_PTR(ret),
  12389. i40e_aq_str(&pf->hw,
  12390. pf->hw.aq.asq_last_status));
  12391. ret = -ENOENT;
  12392. goto err;
  12393. }
  12394. vsi->info = ctxt.info;
  12395. vsi->info.valid_sections = 0;
  12396. vsi->seid = ctxt.seid;
  12397. vsi->id = ctxt.vsi_number;
  12398. }
  12399. spin_lock_bh(&vsi->mac_filter_hash_lock);
  12400. vsi->active_filters = 0;
  12401. /* If macvlan filters already exist, force them to get loaded */
  12402. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  12403. f->state = I40E_FILTER_NEW;
  12404. f_count++;
  12405. }
  12406. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  12407. clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  12408. if (f_count) {
  12409. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  12410. set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
  12411. }
  12412. /* Update VSI BW information */
  12413. ret = i40e_vsi_get_bw_info(vsi);
  12414. if (ret) {
  12415. dev_info(&pf->pdev->dev,
  12416. "couldn't get vsi bw info, err %pe aq_err %s\n",
  12417. ERR_PTR(ret),
  12418. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  12419. /* VSI is already added so not tearing that up */
  12420. ret = 0;
  12421. }
  12422. err:
  12423. return ret;
  12424. }
  12425. /**
  12426. * i40e_vsi_release - Delete a VSI and free its resources
  12427. * @vsi: the VSI being removed
  12428. *
  12429. * Returns 0 on success or < 0 on error
  12430. **/
  12431. int i40e_vsi_release(struct i40e_vsi *vsi)
  12432. {
  12433. struct i40e_mac_filter *f;
  12434. struct hlist_node *h;
  12435. struct i40e_veb *veb;
  12436. struct i40e_pf *pf;
  12437. u16 uplink_seid;
  12438. int i, n, bkt;
  12439. pf = vsi->back;
  12440. /* release of a VEB-owner or last VSI is not allowed */
  12441. if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
  12442. dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
  12443. vsi->seid, vsi->uplink_seid);
  12444. return -ENODEV;
  12445. }
  12446. if (vsi->type == I40E_VSI_MAIN && !test_bit(__I40E_DOWN, pf->state)) {
  12447. dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
  12448. return -ENODEV;
  12449. }
  12450. set_bit(__I40E_VSI_RELEASING, vsi->state);
  12451. uplink_seid = vsi->uplink_seid;
  12452. if (vsi->type != I40E_VSI_SRIOV) {
  12453. if (vsi->netdev_registered) {
  12454. vsi->netdev_registered = false;
  12455. if (vsi->netdev) {
  12456. /* results in a call to i40e_close() */
  12457. unregister_netdev(vsi->netdev);
  12458. }
  12459. } else {
  12460. i40e_vsi_close(vsi);
  12461. }
  12462. i40e_vsi_disable_irq(vsi);
  12463. }
  12464. if (vsi->type == I40E_VSI_MAIN)
  12465. i40e_devlink_destroy_port(pf);
  12466. spin_lock_bh(&vsi->mac_filter_hash_lock);
  12467. /* clear the sync flag on all filters */
  12468. if (vsi->netdev) {
  12469. __dev_uc_unsync(vsi->netdev, NULL);
  12470. __dev_mc_unsync(vsi->netdev, NULL);
  12471. }
  12472. /* make sure any remaining filters are marked for deletion */
  12473. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
  12474. __i40e_del_filter(vsi, f);
  12475. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  12476. i40e_sync_vsi_filters(vsi);
  12477. i40e_vsi_delete(vsi);
  12478. i40e_vsi_free_q_vectors(vsi);
  12479. if (vsi->netdev) {
  12480. free_netdev(vsi->netdev);
  12481. vsi->netdev = NULL;
  12482. }
  12483. i40e_vsi_clear_rings(vsi);
  12484. i40e_vsi_clear(vsi);
  12485. /* If this was the last thing on the VEB, except for the
  12486. * controlling VSI, remove the VEB, which puts the controlling
  12487. * VSI onto the uplink port.
  12488. *
  12489. * Well, okay, there's one more exception here: don't remove
  12490. * the floating VEBs yet. We'll wait for an explicit remove request
  12491. * from up the network stack.
  12492. */
  12493. veb = i40e_pf_get_veb_by_seid(pf, uplink_seid);
  12494. if (veb && veb->uplink_seid) {
  12495. n = 0;
  12496. /* Count non-controlling VSIs present on the VEB */
  12497. i40e_pf_for_each_vsi(pf, i, vsi)
  12498. if (vsi->uplink_seid == uplink_seid &&
  12499. (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
  12500. n++;
  12501. /* If there is no VSI except the control one then release
  12502. * the VEB and put the control VSI onto VEB uplink.
  12503. */
  12504. if (!n)
  12505. i40e_veb_release(veb);
  12506. }
  12507. return 0;
  12508. }
  12509. /**
  12510. * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
  12511. * @vsi: ptr to the VSI
  12512. *
  12513. * This should only be called after i40e_vsi_mem_alloc() which allocates the
  12514. * corresponding SW VSI structure and initializes num_queue_pairs for the
  12515. * newly allocated VSI.
  12516. *
  12517. * Returns 0 on success or negative on failure
  12518. **/
  12519. static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
  12520. {
  12521. int ret = -ENOENT;
  12522. struct i40e_pf *pf = vsi->back;
  12523. if (vsi->q_vectors[0]) {
  12524. dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
  12525. vsi->seid);
  12526. return -EEXIST;
  12527. }
  12528. if (vsi->base_vector) {
  12529. dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
  12530. vsi->seid, vsi->base_vector);
  12531. return -EEXIST;
  12532. }
  12533. ret = i40e_vsi_alloc_q_vectors(vsi);
  12534. if (ret) {
  12535. dev_info(&pf->pdev->dev,
  12536. "failed to allocate %d q_vector for VSI %d, ret=%d\n",
  12537. vsi->num_q_vectors, vsi->seid, ret);
  12538. vsi->num_q_vectors = 0;
  12539. goto vector_setup_out;
  12540. }
  12541. /* In Legacy mode, we do not have to get any other vector since we
  12542. * piggyback on the misc/ICR0 for queue interrupts.
  12543. */
  12544. if (!test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
  12545. return ret;
  12546. if (vsi->num_q_vectors)
  12547. vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
  12548. vsi->num_q_vectors, vsi->idx);
  12549. if (vsi->base_vector < 0) {
  12550. dev_info(&pf->pdev->dev,
  12551. "failed to get tracking for %d vectors for VSI %d, err=%d\n",
  12552. vsi->num_q_vectors, vsi->seid, vsi->base_vector);
  12553. i40e_vsi_free_q_vectors(vsi);
  12554. ret = -ENOENT;
  12555. goto vector_setup_out;
  12556. }
  12557. vector_setup_out:
  12558. return ret;
  12559. }
  12560. /**
  12561. * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
  12562. * @vsi: pointer to the vsi.
  12563. *
  12564. * This re-allocates a vsi's queue resources.
  12565. *
  12566. * Returns pointer to the successfully allocated and configured VSI sw struct
  12567. * on success, otherwise returns NULL on failure.
  12568. **/
  12569. static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
  12570. {
  12571. struct i40e_vsi *main_vsi;
  12572. u16 alloc_queue_pairs;
  12573. struct i40e_pf *pf;
  12574. int ret;
  12575. if (!vsi)
  12576. return NULL;
  12577. pf = vsi->back;
  12578. i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
  12579. i40e_vsi_clear_rings(vsi);
  12580. i40e_vsi_free_arrays(vsi, false);
  12581. i40e_set_num_rings_in_vsi(vsi);
  12582. ret = i40e_vsi_alloc_arrays(vsi, false);
  12583. if (ret)
  12584. goto err_vsi;
  12585. alloc_queue_pairs = vsi->alloc_queue_pairs *
  12586. (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
  12587. ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
  12588. if (ret < 0) {
  12589. dev_info(&pf->pdev->dev,
  12590. "failed to get tracking for %d queues for VSI %d err %d\n",
  12591. alloc_queue_pairs, vsi->seid, ret);
  12592. goto err_vsi;
  12593. }
  12594. vsi->base_queue = ret;
  12595. /* Update the FW view of the VSI. Force a reset of TC and queue
  12596. * layout configurations.
  12597. */
  12598. main_vsi = i40e_pf_get_main_vsi(pf);
  12599. main_vsi->seid = pf->main_vsi_seid;
  12600. i40e_vsi_reconfig_tc(main_vsi);
  12601. if (vsi->type == I40E_VSI_MAIN)
  12602. i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
  12603. /* assign it some queues */
  12604. ret = i40e_alloc_rings(vsi);
  12605. if (ret)
  12606. goto err_rings;
  12607. /* map all of the rings to the q_vectors */
  12608. i40e_vsi_map_rings_to_vectors(vsi);
  12609. return vsi;
  12610. err_rings:
  12611. i40e_vsi_free_q_vectors(vsi);
  12612. if (vsi->netdev_registered) {
  12613. vsi->netdev_registered = false;
  12614. unregister_netdev(vsi->netdev);
  12615. free_netdev(vsi->netdev);
  12616. vsi->netdev = NULL;
  12617. }
  12618. if (vsi->type == I40E_VSI_MAIN)
  12619. i40e_devlink_destroy_port(pf);
  12620. i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
  12621. err_vsi:
  12622. i40e_vsi_clear(vsi);
  12623. return NULL;
  12624. }
  12625. /**
  12626. * i40e_vsi_setup - Set up a VSI by a given type
  12627. * @pf: board private structure
  12628. * @type: VSI type
  12629. * @uplink_seid: the switch element to link to
  12630. * @param1: usage depends upon VSI type. For VF types, indicates VF id
  12631. *
  12632. * This allocates the sw VSI structure and its queue resources, then add a VSI
  12633. * to the identified VEB.
  12634. *
  12635. * Returns pointer to the successfully allocated and configure VSI sw struct on
  12636. * success, otherwise returns NULL on failure.
  12637. **/
  12638. struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
  12639. u16 uplink_seid, u32 param1)
  12640. {
  12641. struct i40e_vsi *vsi = NULL;
  12642. struct i40e_veb *veb = NULL;
  12643. u16 alloc_queue_pairs;
  12644. int v_idx;
  12645. int ret;
  12646. /* The requested uplink_seid must be either
  12647. * - the PF's port seid
  12648. * no VEB is needed because this is the PF
  12649. * or this is a Flow Director special case VSI
  12650. * - seid of an existing VEB
  12651. * - seid of a VSI that owns an existing VEB
  12652. * - seid of a VSI that doesn't own a VEB
  12653. * a new VEB is created and the VSI becomes the owner
  12654. * - seid of the PF VSI, which is what creates the first VEB
  12655. * this is a special case of the previous
  12656. *
  12657. * Find which uplink_seid we were given and create a new VEB if needed
  12658. */
  12659. veb = i40e_pf_get_veb_by_seid(pf, uplink_seid);
  12660. if (!veb && uplink_seid != pf->mac_seid) {
  12661. vsi = i40e_pf_get_vsi_by_seid(pf, uplink_seid);
  12662. if (!vsi) {
  12663. dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
  12664. uplink_seid);
  12665. return NULL;
  12666. }
  12667. if (vsi->uplink_seid == pf->mac_seid)
  12668. veb = i40e_veb_setup(pf, pf->mac_seid, vsi->seid,
  12669. vsi->tc_config.enabled_tc);
  12670. else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
  12671. veb = i40e_veb_setup(pf, vsi->uplink_seid, vsi->seid,
  12672. vsi->tc_config.enabled_tc);
  12673. if (veb) {
  12674. if (vsi->type != I40E_VSI_MAIN) {
  12675. dev_info(&vsi->back->pdev->dev,
  12676. "New VSI creation error, uplink seid of LAN VSI expected.\n");
  12677. return NULL;
  12678. }
  12679. /* We come up by default in VEPA mode if SRIOV is not
  12680. * already enabled, in which case we can't force VEPA
  12681. * mode.
  12682. */
  12683. if (!test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags)) {
  12684. veb->bridge_mode = BRIDGE_MODE_VEPA;
  12685. clear_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags);
  12686. }
  12687. i40e_config_bridge_mode(veb);
  12688. }
  12689. veb = i40e_pf_get_veb_by_seid(pf, vsi->uplink_seid);
  12690. if (!veb) {
  12691. dev_info(&pf->pdev->dev, "couldn't add VEB\n");
  12692. return NULL;
  12693. }
  12694. vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
  12695. uplink_seid = veb->seid;
  12696. }
  12697. /* get vsi sw struct */
  12698. v_idx = i40e_vsi_mem_alloc(pf, type);
  12699. if (v_idx < 0)
  12700. goto err_alloc;
  12701. vsi = pf->vsi[v_idx];
  12702. if (!vsi)
  12703. goto err_alloc;
  12704. vsi->type = type;
  12705. vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
  12706. if (type == I40E_VSI_MAIN)
  12707. pf->lan_vsi = v_idx;
  12708. else if (type == I40E_VSI_SRIOV)
  12709. vsi->vf_id = param1;
  12710. /* assign it some queues */
  12711. alloc_queue_pairs = vsi->alloc_queue_pairs *
  12712. (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
  12713. ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
  12714. if (ret < 0) {
  12715. dev_info(&pf->pdev->dev,
  12716. "failed to get tracking for %d queues for VSI %d err=%d\n",
  12717. alloc_queue_pairs, vsi->seid, ret);
  12718. goto err_vsi;
  12719. }
  12720. vsi->base_queue = ret;
  12721. /* get a VSI from the hardware */
  12722. vsi->uplink_seid = uplink_seid;
  12723. ret = i40e_add_vsi(vsi);
  12724. if (ret)
  12725. goto err_vsi;
  12726. switch (vsi->type) {
  12727. /* setup the netdev if needed */
  12728. case I40E_VSI_MAIN:
  12729. case I40E_VSI_VMDQ2:
  12730. ret = i40e_config_netdev(vsi);
  12731. if (ret)
  12732. goto err_netdev;
  12733. ret = i40e_netif_set_realnum_tx_rx_queues(vsi);
  12734. if (ret)
  12735. goto err_netdev;
  12736. if (vsi->type == I40E_VSI_MAIN) {
  12737. ret = i40e_devlink_create_port(pf);
  12738. if (ret)
  12739. goto err_netdev;
  12740. SET_NETDEV_DEVLINK_PORT(vsi->netdev, &pf->devlink_port);
  12741. }
  12742. ret = register_netdev(vsi->netdev);
  12743. if (ret)
  12744. goto err_dl_port;
  12745. vsi->netdev_registered = true;
  12746. netif_carrier_off(vsi->netdev);
  12747. #ifdef CONFIG_I40E_DCB
  12748. /* Setup DCB netlink interface */
  12749. i40e_dcbnl_setup(vsi);
  12750. #endif /* CONFIG_I40E_DCB */
  12751. fallthrough;
  12752. case I40E_VSI_FDIR:
  12753. /* set up vectors and rings if needed */
  12754. ret = i40e_vsi_setup_vectors(vsi);
  12755. if (ret)
  12756. goto err_msix;
  12757. ret = i40e_alloc_rings(vsi);
  12758. if (ret)
  12759. goto err_rings;
  12760. /* map all of the rings to the q_vectors */
  12761. i40e_vsi_map_rings_to_vectors(vsi);
  12762. i40e_vsi_reset_stats(vsi);
  12763. break;
  12764. default:
  12765. /* no netdev or rings for the other VSI types */
  12766. break;
  12767. }
  12768. if (test_bit(I40E_HW_CAP_RSS_AQ, pf->hw.caps) &&
  12769. vsi->type == I40E_VSI_VMDQ2) {
  12770. ret = i40e_vsi_config_rss(vsi);
  12771. if (ret)
  12772. goto err_config;
  12773. }
  12774. return vsi;
  12775. err_config:
  12776. i40e_vsi_clear_rings(vsi);
  12777. err_rings:
  12778. i40e_vsi_free_q_vectors(vsi);
  12779. err_msix:
  12780. if (vsi->netdev_registered) {
  12781. vsi->netdev_registered = false;
  12782. unregister_netdev(vsi->netdev);
  12783. free_netdev(vsi->netdev);
  12784. vsi->netdev = NULL;
  12785. }
  12786. err_dl_port:
  12787. if (vsi->type == I40E_VSI_MAIN)
  12788. i40e_devlink_destroy_port(pf);
  12789. err_netdev:
  12790. i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
  12791. err_vsi:
  12792. i40e_vsi_clear(vsi);
  12793. err_alloc:
  12794. return NULL;
  12795. }
  12796. /**
  12797. * i40e_veb_get_bw_info - Query VEB BW information
  12798. * @veb: the veb to query
  12799. *
  12800. * Query the Tx scheduler BW configuration data for given VEB
  12801. **/
  12802. static int i40e_veb_get_bw_info(struct i40e_veb *veb)
  12803. {
  12804. struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
  12805. struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
  12806. struct i40e_pf *pf = veb->pf;
  12807. struct i40e_hw *hw = &pf->hw;
  12808. u32 tc_bw_max;
  12809. int ret = 0;
  12810. int i;
  12811. ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
  12812. &bw_data, NULL);
  12813. if (ret) {
  12814. dev_info(&pf->pdev->dev,
  12815. "query veb bw config failed, err %pe aq_err %s\n",
  12816. ERR_PTR(ret),
  12817. i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
  12818. goto out;
  12819. }
  12820. ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
  12821. &ets_data, NULL);
  12822. if (ret) {
  12823. dev_info(&pf->pdev->dev,
  12824. "query veb bw ets config failed, err %pe aq_err %s\n",
  12825. ERR_PTR(ret),
  12826. i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
  12827. goto out;
  12828. }
  12829. veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
  12830. veb->bw_max_quanta = ets_data.tc_bw_max;
  12831. veb->is_abs_credits = bw_data.absolute_credits_enable;
  12832. veb->enabled_tc = ets_data.tc_valid_bits;
  12833. tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
  12834. (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
  12835. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  12836. veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
  12837. veb->bw_tc_limit_credits[i] =
  12838. le16_to_cpu(bw_data.tc_bw_limits[i]);
  12839. veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
  12840. }
  12841. out:
  12842. return ret;
  12843. }
  12844. /**
  12845. * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
  12846. * @pf: board private structure
  12847. *
  12848. * On error: returns error code (negative)
  12849. * On success: returns vsi index in PF (positive)
  12850. **/
  12851. static int i40e_veb_mem_alloc(struct i40e_pf *pf)
  12852. {
  12853. int ret = -ENOENT;
  12854. struct i40e_veb *veb;
  12855. int i;
  12856. /* Need to protect the allocation of switch elements at the PF level */
  12857. mutex_lock(&pf->switch_mutex);
  12858. /* VEB list may be fragmented if VEB creation/destruction has
  12859. * been happening. We can afford to do a quick scan to look
  12860. * for any free slots in the list.
  12861. *
  12862. * find next empty veb slot, looping back around if necessary
  12863. */
  12864. i = 0;
  12865. while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
  12866. i++;
  12867. if (i >= I40E_MAX_VEB) {
  12868. ret = -ENOMEM;
  12869. goto err_alloc_veb; /* out of VEB slots! */
  12870. }
  12871. veb = kzalloc(sizeof(*veb), GFP_KERNEL);
  12872. if (!veb) {
  12873. ret = -ENOMEM;
  12874. goto err_alloc_veb;
  12875. }
  12876. veb->pf = pf;
  12877. veb->idx = i;
  12878. veb->enabled_tc = 1;
  12879. pf->veb[i] = veb;
  12880. ret = i;
  12881. err_alloc_veb:
  12882. mutex_unlock(&pf->switch_mutex);
  12883. return ret;
  12884. }
  12885. /**
  12886. * i40e_switch_branch_release - Delete a branch of the switch tree
  12887. * @branch: where to start deleting
  12888. *
  12889. * This uses recursion to find the tips of the branch to be
  12890. * removed, deleting until we get back to and can delete this VEB.
  12891. **/
  12892. static void i40e_switch_branch_release(struct i40e_veb *branch)
  12893. {
  12894. struct i40e_pf *pf = branch->pf;
  12895. u16 branch_seid = branch->seid;
  12896. u16 veb_idx = branch->idx;
  12897. struct i40e_vsi *vsi;
  12898. struct i40e_veb *veb;
  12899. int i;
  12900. /* release any VEBs on this VEB - RECURSION */
  12901. i40e_pf_for_each_veb(pf, i, veb)
  12902. if (veb->uplink_seid == branch->seid)
  12903. i40e_switch_branch_release(veb);
  12904. /* Release the VSIs on this VEB, but not the owner VSI.
  12905. *
  12906. * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
  12907. * the VEB itself, so don't use (*branch) after this loop.
  12908. */
  12909. i40e_pf_for_each_vsi(pf, i, vsi)
  12910. if (vsi->uplink_seid == branch_seid &&
  12911. (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
  12912. i40e_vsi_release(vsi);
  12913. /* There's one corner case where the VEB might not have been
  12914. * removed, so double check it here and remove it if needed.
  12915. * This case happens if the veb was created from the debugfs
  12916. * commands and no VSIs were added to it.
  12917. */
  12918. if (pf->veb[veb_idx])
  12919. i40e_veb_release(pf->veb[veb_idx]);
  12920. }
  12921. /**
  12922. * i40e_veb_clear - remove veb struct
  12923. * @veb: the veb to remove
  12924. **/
  12925. static void i40e_veb_clear(struct i40e_veb *veb)
  12926. {
  12927. if (!veb)
  12928. return;
  12929. if (veb->pf) {
  12930. struct i40e_pf *pf = veb->pf;
  12931. mutex_lock(&pf->switch_mutex);
  12932. if (pf->veb[veb->idx] == veb)
  12933. pf->veb[veb->idx] = NULL;
  12934. mutex_unlock(&pf->switch_mutex);
  12935. }
  12936. kfree(veb);
  12937. }
  12938. /**
  12939. * i40e_veb_release - Delete a VEB and free its resources
  12940. * @veb: the VEB being removed
  12941. **/
  12942. void i40e_veb_release(struct i40e_veb *veb)
  12943. {
  12944. struct i40e_vsi *vsi, *vsi_it;
  12945. struct i40e_pf *pf;
  12946. int i, n = 0;
  12947. pf = veb->pf;
  12948. /* find the remaining VSI and check for extras */
  12949. i40e_pf_for_each_vsi(pf, i, vsi_it)
  12950. if (vsi_it->uplink_seid == veb->seid) {
  12951. if (vsi_it->flags & I40E_VSI_FLAG_VEB_OWNER)
  12952. vsi = vsi_it;
  12953. n++;
  12954. }
  12955. /* Floating VEB has to be empty and regular one must have
  12956. * single owner VSI.
  12957. */
  12958. if ((veb->uplink_seid && n != 1) || (!veb->uplink_seid && n != 0)) {
  12959. dev_info(&pf->pdev->dev,
  12960. "can't remove VEB %d with %d VSIs left\n",
  12961. veb->seid, n);
  12962. return;
  12963. }
  12964. /* For regular VEB move the owner VSI to uplink port */
  12965. if (veb->uplink_seid) {
  12966. vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
  12967. vsi->uplink_seid = veb->uplink_seid;
  12968. vsi->veb_idx = I40E_NO_VEB;
  12969. }
  12970. i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
  12971. i40e_veb_clear(veb);
  12972. }
  12973. /**
  12974. * i40e_add_veb - create the VEB in the switch
  12975. * @veb: the VEB to be instantiated
  12976. * @vsi: the controlling VSI
  12977. **/
  12978. static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
  12979. {
  12980. struct i40e_pf *pf = veb->pf;
  12981. bool enable_stats = !!test_bit(I40E_FLAG_VEB_STATS_ENA, pf->flags);
  12982. int ret;
  12983. ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi ? vsi->seid : 0,
  12984. veb->enabled_tc, vsi ? false : true,
  12985. &veb->seid, enable_stats, NULL);
  12986. /* get a VEB from the hardware */
  12987. if (ret) {
  12988. dev_info(&pf->pdev->dev,
  12989. "couldn't add VEB, err %pe aq_err %s\n",
  12990. ERR_PTR(ret),
  12991. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  12992. return -EPERM;
  12993. }
  12994. /* get statistics counter */
  12995. ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
  12996. &veb->stats_idx, NULL, NULL, NULL);
  12997. if (ret) {
  12998. dev_info(&pf->pdev->dev,
  12999. "couldn't get VEB statistics idx, err %pe aq_err %s\n",
  13000. ERR_PTR(ret),
  13001. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  13002. return -EPERM;
  13003. }
  13004. ret = i40e_veb_get_bw_info(veb);
  13005. if (ret) {
  13006. dev_info(&pf->pdev->dev,
  13007. "couldn't get VEB bw info, err %pe aq_err %s\n",
  13008. ERR_PTR(ret),
  13009. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  13010. i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
  13011. return -ENOENT;
  13012. }
  13013. if (vsi) {
  13014. vsi->uplink_seid = veb->seid;
  13015. vsi->veb_idx = veb->idx;
  13016. vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
  13017. }
  13018. return 0;
  13019. }
  13020. /**
  13021. * i40e_veb_setup - Set up a VEB
  13022. * @pf: board private structure
  13023. * @uplink_seid: the switch element to link to
  13024. * @vsi_seid: the initial VSI seid
  13025. * @enabled_tc: Enabled TC bit-map
  13026. *
  13027. * This allocates the sw VEB structure and links it into the switch
  13028. * It is possible and legal for this to be a duplicate of an already
  13029. * existing VEB. It is also possible for both uplink and vsi seids
  13030. * to be zero, in order to create a floating VEB.
  13031. *
  13032. * Returns pointer to the successfully allocated VEB sw struct on
  13033. * success, otherwise returns NULL on failure.
  13034. **/
  13035. struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 uplink_seid,
  13036. u16 vsi_seid, u8 enabled_tc)
  13037. {
  13038. struct i40e_vsi *vsi = NULL;
  13039. struct i40e_veb *veb;
  13040. int veb_idx;
  13041. int ret;
  13042. /* if one seid is 0, the other must be 0 to create a floating relay */
  13043. if ((uplink_seid == 0 || vsi_seid == 0) &&
  13044. (uplink_seid + vsi_seid != 0)) {
  13045. dev_info(&pf->pdev->dev,
  13046. "one, not both seid's are 0: uplink=%d vsi=%d\n",
  13047. uplink_seid, vsi_seid);
  13048. return NULL;
  13049. }
  13050. /* make sure there is such a vsi and uplink */
  13051. if (vsi_seid) {
  13052. vsi = i40e_pf_get_vsi_by_seid(pf, vsi_seid);
  13053. if (!vsi) {
  13054. dev_err(&pf->pdev->dev, "vsi seid %d not found\n",
  13055. vsi_seid);
  13056. return NULL;
  13057. }
  13058. }
  13059. /* get veb sw struct */
  13060. veb_idx = i40e_veb_mem_alloc(pf);
  13061. if (veb_idx < 0)
  13062. goto err_alloc;
  13063. veb = pf->veb[veb_idx];
  13064. veb->uplink_seid = uplink_seid;
  13065. veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
  13066. /* create the VEB in the switch */
  13067. ret = i40e_add_veb(veb, vsi);
  13068. if (ret)
  13069. goto err_veb;
  13070. if (vsi && vsi->idx == pf->lan_vsi)
  13071. pf->lan_veb = veb->idx;
  13072. return veb;
  13073. err_veb:
  13074. i40e_veb_clear(veb);
  13075. err_alloc:
  13076. return NULL;
  13077. }
  13078. /**
  13079. * i40e_setup_pf_switch_element - set PF vars based on switch type
  13080. * @pf: board private structure
  13081. * @ele: element we are building info from
  13082. * @num_reported: total number of elements
  13083. * @printconfig: should we print the contents
  13084. *
  13085. * helper function to assist in extracting a few useful SEID values.
  13086. **/
  13087. static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
  13088. struct i40e_aqc_switch_config_element_resp *ele,
  13089. u16 num_reported, bool printconfig)
  13090. {
  13091. u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
  13092. u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
  13093. u8 element_type = ele->element_type;
  13094. u16 seid = le16_to_cpu(ele->seid);
  13095. struct i40e_veb *veb;
  13096. if (printconfig)
  13097. dev_info(&pf->pdev->dev,
  13098. "type=%d seid=%d uplink=%d downlink=%d\n",
  13099. element_type, seid, uplink_seid, downlink_seid);
  13100. switch (element_type) {
  13101. case I40E_SWITCH_ELEMENT_TYPE_MAC:
  13102. pf->mac_seid = seid;
  13103. break;
  13104. case I40E_SWITCH_ELEMENT_TYPE_VEB:
  13105. /* Main VEB? */
  13106. if (uplink_seid != pf->mac_seid)
  13107. break;
  13108. veb = i40e_pf_get_main_veb(pf);
  13109. if (!veb) {
  13110. int v;
  13111. /* find existing or else empty VEB */
  13112. veb = i40e_pf_get_veb_by_seid(pf, seid);
  13113. if (veb) {
  13114. pf->lan_veb = veb->idx;
  13115. } else {
  13116. v = i40e_veb_mem_alloc(pf);
  13117. if (v < 0)
  13118. break;
  13119. pf->lan_veb = v;
  13120. }
  13121. }
  13122. /* Try to get again main VEB as pf->lan_veb may have changed */
  13123. veb = i40e_pf_get_main_veb(pf);
  13124. if (!veb)
  13125. break;
  13126. veb->seid = seid;
  13127. veb->uplink_seid = pf->mac_seid;
  13128. veb->pf = pf;
  13129. break;
  13130. case I40E_SWITCH_ELEMENT_TYPE_VSI:
  13131. if (num_reported != 1)
  13132. break;
  13133. /* This is immediately after a reset so we can assume this is
  13134. * the PF's VSI
  13135. */
  13136. pf->mac_seid = uplink_seid;
  13137. pf->main_vsi_seid = seid;
  13138. if (printconfig)
  13139. dev_info(&pf->pdev->dev,
  13140. "pf_seid=%d main_vsi_seid=%d\n",
  13141. downlink_seid, pf->main_vsi_seid);
  13142. break;
  13143. case I40E_SWITCH_ELEMENT_TYPE_PF:
  13144. case I40E_SWITCH_ELEMENT_TYPE_VF:
  13145. case I40E_SWITCH_ELEMENT_TYPE_EMP:
  13146. case I40E_SWITCH_ELEMENT_TYPE_BMC:
  13147. case I40E_SWITCH_ELEMENT_TYPE_PE:
  13148. case I40E_SWITCH_ELEMENT_TYPE_PA:
  13149. /* ignore these for now */
  13150. break;
  13151. default:
  13152. dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
  13153. element_type, seid);
  13154. break;
  13155. }
  13156. }
  13157. /**
  13158. * i40e_fetch_switch_configuration - Get switch config from firmware
  13159. * @pf: board private structure
  13160. * @printconfig: should we print the contents
  13161. *
  13162. * Get the current switch configuration from the device and
  13163. * extract a few useful SEID values.
  13164. **/
  13165. int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
  13166. {
  13167. struct i40e_aqc_get_switch_config_resp *sw_config;
  13168. u16 next_seid = 0;
  13169. int ret = 0;
  13170. u8 *aq_buf;
  13171. int i;
  13172. aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
  13173. if (!aq_buf)
  13174. return -ENOMEM;
  13175. sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
  13176. do {
  13177. u16 num_reported, num_total;
  13178. ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
  13179. I40E_AQ_LARGE_BUF,
  13180. &next_seid, NULL);
  13181. if (ret) {
  13182. dev_info(&pf->pdev->dev,
  13183. "get switch config failed err %d aq_err %s\n",
  13184. ret,
  13185. i40e_aq_str(&pf->hw,
  13186. pf->hw.aq.asq_last_status));
  13187. kfree(aq_buf);
  13188. return -ENOENT;
  13189. }
  13190. num_reported = le16_to_cpu(sw_config->header.num_reported);
  13191. num_total = le16_to_cpu(sw_config->header.num_total);
  13192. if (printconfig)
  13193. dev_info(&pf->pdev->dev,
  13194. "header: %d reported %d total\n",
  13195. num_reported, num_total);
  13196. for (i = 0; i < num_reported; i++) {
  13197. struct i40e_aqc_switch_config_element_resp *ele =
  13198. &sw_config->element[i];
  13199. i40e_setup_pf_switch_element(pf, ele, num_reported,
  13200. printconfig);
  13201. }
  13202. } while (next_seid != 0);
  13203. kfree(aq_buf);
  13204. return ret;
  13205. }
  13206. /**
  13207. * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
  13208. * @pf: board private structure
  13209. * @reinit: if the Main VSI needs to re-initialized.
  13210. * @lock_acquired: indicates whether or not the lock has been acquired
  13211. *
  13212. * Returns 0 on success, negative value on failure
  13213. **/
  13214. static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired)
  13215. {
  13216. struct i40e_vsi *main_vsi;
  13217. u16 flags = 0;
  13218. int ret;
  13219. /* find out what's out there already */
  13220. ret = i40e_fetch_switch_configuration(pf, false);
  13221. if (ret) {
  13222. dev_info(&pf->pdev->dev,
  13223. "couldn't fetch switch config, err %pe aq_err %s\n",
  13224. ERR_PTR(ret),
  13225. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  13226. return ret;
  13227. }
  13228. i40e_pf_reset_stats(pf);
  13229. /* set the switch config bit for the whole device to
  13230. * support limited promisc or true promisc
  13231. * when user requests promisc. The default is limited
  13232. * promisc.
  13233. */
  13234. if ((pf->hw.pf_id == 0) &&
  13235. !test_bit(I40E_FLAG_TRUE_PROMISC_ENA, pf->flags)) {
  13236. flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
  13237. pf->last_sw_conf_flags = flags;
  13238. }
  13239. if (pf->hw.pf_id == 0) {
  13240. u16 valid_flags;
  13241. valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
  13242. ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags, 0,
  13243. NULL);
  13244. if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
  13245. dev_info(&pf->pdev->dev,
  13246. "couldn't set switch config bits, err %pe aq_err %s\n",
  13247. ERR_PTR(ret),
  13248. i40e_aq_str(&pf->hw,
  13249. pf->hw.aq.asq_last_status));
  13250. /* not a fatal problem, just keep going */
  13251. }
  13252. pf->last_sw_conf_valid_flags = valid_flags;
  13253. }
  13254. /* first time setup */
  13255. main_vsi = i40e_pf_get_main_vsi(pf);
  13256. if (!main_vsi || reinit) {
  13257. struct i40e_veb *veb;
  13258. u16 uplink_seid;
  13259. /* Set up the PF VSI associated with the PF's main VSI
  13260. * that is already in the HW switch
  13261. */
  13262. veb = i40e_pf_get_main_veb(pf);
  13263. if (veb)
  13264. uplink_seid = veb->seid;
  13265. else
  13266. uplink_seid = pf->mac_seid;
  13267. if (!main_vsi)
  13268. main_vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN,
  13269. uplink_seid, 0);
  13270. else if (reinit)
  13271. main_vsi = i40e_vsi_reinit_setup(main_vsi);
  13272. if (!main_vsi) {
  13273. dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
  13274. i40e_cloud_filter_exit(pf);
  13275. i40e_fdir_teardown(pf);
  13276. return -EAGAIN;
  13277. }
  13278. } else {
  13279. /* force a reset of TC and queue layout configurations */
  13280. main_vsi->seid = pf->main_vsi_seid;
  13281. i40e_vsi_reconfig_tc(main_vsi);
  13282. }
  13283. i40e_vlan_stripping_disable(main_vsi);
  13284. i40e_fdir_sb_setup(pf);
  13285. /* Setup static PF queue filter control settings */
  13286. ret = i40e_setup_pf_filter_control(pf);
  13287. if (ret) {
  13288. dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
  13289. ret);
  13290. /* Failure here should not stop continuing other steps */
  13291. }
  13292. /* enable RSS in the HW, even for only one queue, as the stack can use
  13293. * the hash
  13294. */
  13295. if (test_bit(I40E_FLAG_RSS_ENA, pf->flags))
  13296. i40e_pf_config_rss(pf);
  13297. /* fill in link information and enable LSE reporting */
  13298. i40e_link_event(pf);
  13299. i40e_ptp_init(pf);
  13300. if (!lock_acquired)
  13301. rtnl_lock();
  13302. /* repopulate tunnel port filters */
  13303. udp_tunnel_nic_reset_ntf(main_vsi->netdev);
  13304. if (!lock_acquired)
  13305. rtnl_unlock();
  13306. return ret;
  13307. }
  13308. /**
  13309. * i40e_determine_queue_usage - Work out queue distribution
  13310. * @pf: board private structure
  13311. **/
  13312. static void i40e_determine_queue_usage(struct i40e_pf *pf)
  13313. {
  13314. int queues_left;
  13315. int q_max;
  13316. pf->num_lan_qps = 0;
  13317. /* Find the max queues to be put into basic use. We'll always be
  13318. * using TC0, whether or not DCB is running, and TC0 will get the
  13319. * big RSS set.
  13320. */
  13321. queues_left = pf->hw.func_caps.num_tx_qp;
  13322. if ((queues_left == 1) ||
  13323. !test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
  13324. /* one qp for PF, no queues for anything else */
  13325. queues_left = 0;
  13326. pf->alloc_rss_size = pf->num_lan_qps = 1;
  13327. /* make sure all the fancies are disabled */
  13328. clear_bit(I40E_FLAG_RSS_ENA, pf->flags);
  13329. clear_bit(I40E_FLAG_IWARP_ENA, pf->flags);
  13330. clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
  13331. clear_bit(I40E_FLAG_FD_ATR_ENA, pf->flags);
  13332. clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
  13333. clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
  13334. clear_bit(I40E_FLAG_SRIOV_ENA, pf->flags);
  13335. clear_bit(I40E_FLAG_VMDQ_ENA, pf->flags);
  13336. set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
  13337. } else if (!test_bit(I40E_FLAG_RSS_ENA, pf->flags) &&
  13338. !test_bit(I40E_FLAG_FD_SB_ENA, pf->flags) &&
  13339. !test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags) &&
  13340. !test_bit(I40E_FLAG_DCB_CAPABLE, pf->flags)) {
  13341. /* one qp for PF */
  13342. pf->alloc_rss_size = pf->num_lan_qps = 1;
  13343. queues_left -= pf->num_lan_qps;
  13344. clear_bit(I40E_FLAG_RSS_ENA, pf->flags);
  13345. clear_bit(I40E_FLAG_IWARP_ENA, pf->flags);
  13346. clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
  13347. clear_bit(I40E_FLAG_FD_ATR_ENA, pf->flags);
  13348. clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
  13349. clear_bit(I40E_FLAG_VMDQ_ENA, pf->flags);
  13350. set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
  13351. } else {
  13352. /* Not enough queues for all TCs */
  13353. if (test_bit(I40E_FLAG_DCB_CAPABLE, pf->flags) &&
  13354. queues_left < I40E_MAX_TRAFFIC_CLASS) {
  13355. clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
  13356. clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
  13357. dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
  13358. }
  13359. /* limit lan qps to the smaller of qps, cpus or msix */
  13360. q_max = max_t(int, pf->rss_size_max, num_online_cpus());
  13361. q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
  13362. q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
  13363. pf->num_lan_qps = q_max;
  13364. queues_left -= pf->num_lan_qps;
  13365. }
  13366. if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags)) {
  13367. if (queues_left > 1) {
  13368. queues_left -= 1; /* save 1 queue for FD */
  13369. } else {
  13370. clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
  13371. set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
  13372. dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
  13373. }
  13374. }
  13375. if (test_bit(I40E_FLAG_SRIOV_ENA, pf->flags) &&
  13376. pf->num_vf_qps && pf->num_req_vfs && queues_left) {
  13377. pf->num_req_vfs = min_t(int, pf->num_req_vfs,
  13378. (queues_left / pf->num_vf_qps));
  13379. queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
  13380. }
  13381. if (test_bit(I40E_FLAG_VMDQ_ENA, pf->flags) &&
  13382. pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
  13383. pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
  13384. (queues_left / pf->num_vmdq_qps));
  13385. queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
  13386. }
  13387. pf->queues_left = queues_left;
  13388. dev_dbg(&pf->pdev->dev,
  13389. "qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
  13390. pf->hw.func_caps.num_tx_qp,
  13391. !!test_bit(I40E_FLAG_FD_SB_ENA, pf->flags),
  13392. pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
  13393. pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
  13394. queues_left);
  13395. }
  13396. /**
  13397. * i40e_setup_pf_filter_control - Setup PF static filter control
  13398. * @pf: PF to be setup
  13399. *
  13400. * i40e_setup_pf_filter_control sets up a PF's initial filter control
  13401. * settings. If PE/FCoE are enabled then it will also set the per PF
  13402. * based filter sizes required for them. It also enables Flow director,
  13403. * ethertype and macvlan type filter settings for the pf.
  13404. *
  13405. * Returns 0 on success, negative on failure
  13406. **/
  13407. static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
  13408. {
  13409. struct i40e_filter_control_settings *settings = &pf->filter_settings;
  13410. settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
  13411. /* Flow Director is enabled */
  13412. if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags) ||
  13413. test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags))
  13414. settings->enable_fdir = true;
  13415. /* Ethtype and MACVLAN filters enabled for PF */
  13416. settings->enable_ethtype = true;
  13417. settings->enable_macvlan = true;
  13418. if (i40e_set_filter_control(&pf->hw, settings))
  13419. return -ENOENT;
  13420. return 0;
  13421. }
  13422. #define INFO_STRING_LEN 255
  13423. #define REMAIN(__x) (INFO_STRING_LEN - (__x))
  13424. static void i40e_print_features(struct i40e_pf *pf)
  13425. {
  13426. struct i40e_vsi *main_vsi = i40e_pf_get_main_vsi(pf);
  13427. struct i40e_hw *hw = &pf->hw;
  13428. char *buf;
  13429. int i;
  13430. buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
  13431. if (!buf)
  13432. return;
  13433. i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
  13434. #ifdef CONFIG_PCI_IOV
  13435. i += scnprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
  13436. #endif
  13437. i += scnprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
  13438. pf->hw.func_caps.num_vsis, main_vsi->num_queue_pairs);
  13439. if (test_bit(I40E_FLAG_RSS_ENA, pf->flags))
  13440. i += scnprintf(&buf[i], REMAIN(i), " RSS");
  13441. if (test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags))
  13442. i += scnprintf(&buf[i], REMAIN(i), " FD_ATR");
  13443. if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags)) {
  13444. i += scnprintf(&buf[i], REMAIN(i), " FD_SB");
  13445. i += scnprintf(&buf[i], REMAIN(i), " NTUPLE");
  13446. }
  13447. if (test_bit(I40E_FLAG_DCB_CAPABLE, pf->flags))
  13448. i += scnprintf(&buf[i], REMAIN(i), " DCB");
  13449. i += scnprintf(&buf[i], REMAIN(i), " VxLAN");
  13450. i += scnprintf(&buf[i], REMAIN(i), " Geneve");
  13451. if (test_bit(I40E_FLAG_PTP_ENA, pf->flags))
  13452. i += scnprintf(&buf[i], REMAIN(i), " PTP");
  13453. if (test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags))
  13454. i += scnprintf(&buf[i], REMAIN(i), " VEB");
  13455. else
  13456. i += scnprintf(&buf[i], REMAIN(i), " VEPA");
  13457. dev_info(&pf->pdev->dev, "%s\n", buf);
  13458. kfree(buf);
  13459. WARN_ON(i > INFO_STRING_LEN);
  13460. }
  13461. /**
  13462. * i40e_get_platform_mac_addr - get platform-specific MAC address
  13463. * @pdev: PCI device information struct
  13464. * @pf: board private structure
  13465. *
  13466. * Look up the MAC address for the device. First we'll try
  13467. * eth_platform_get_mac_address, which will check Open Firmware, or arch
  13468. * specific fallback. Otherwise, we'll default to the stored value in
  13469. * firmware.
  13470. **/
  13471. static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
  13472. {
  13473. if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
  13474. i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
  13475. }
  13476. /**
  13477. * i40e_set_fec_in_flags - helper function for setting FEC options in flags
  13478. * @fec_cfg: FEC option to set in flags
  13479. * @flags: ptr to flags in which we set FEC option
  13480. **/
  13481. void i40e_set_fec_in_flags(u8 fec_cfg, unsigned long *flags)
  13482. {
  13483. if (fec_cfg & I40E_AQ_SET_FEC_AUTO) {
  13484. set_bit(I40E_FLAG_RS_FEC, flags);
  13485. set_bit(I40E_FLAG_BASE_R_FEC, flags);
  13486. }
  13487. if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_RS) ||
  13488. (fec_cfg & I40E_AQ_SET_FEC_ABILITY_RS)) {
  13489. set_bit(I40E_FLAG_RS_FEC, flags);
  13490. clear_bit(I40E_FLAG_BASE_R_FEC, flags);
  13491. }
  13492. if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_KR) ||
  13493. (fec_cfg & I40E_AQ_SET_FEC_ABILITY_KR)) {
  13494. set_bit(I40E_FLAG_BASE_R_FEC, flags);
  13495. clear_bit(I40E_FLAG_RS_FEC, flags);
  13496. }
  13497. if (fec_cfg == 0) {
  13498. clear_bit(I40E_FLAG_RS_FEC, flags);
  13499. clear_bit(I40E_FLAG_BASE_R_FEC, flags);
  13500. }
  13501. }
  13502. /**
  13503. * i40e_check_recovery_mode - check if we are running transition firmware
  13504. * @pf: board private structure
  13505. *
  13506. * Check registers indicating the firmware runs in recovery mode. Sets the
  13507. * appropriate driver state.
  13508. *
  13509. * Returns true if the recovery mode was detected, false otherwise
  13510. **/
  13511. static bool i40e_check_recovery_mode(struct i40e_pf *pf)
  13512. {
  13513. u32 val = rd32(&pf->hw, I40E_GL_FWSTS);
  13514. if (val & I40E_GL_FWSTS_FWS1B_MASK) {
  13515. dev_crit(&pf->pdev->dev, "Firmware recovery mode detected. Limiting functionality.\n");
  13516. dev_crit(&pf->pdev->dev, "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for details on firmware recovery mode.\n");
  13517. set_bit(__I40E_RECOVERY_MODE, pf->state);
  13518. return true;
  13519. }
  13520. if (test_bit(__I40E_RECOVERY_MODE, pf->state))
  13521. dev_info(&pf->pdev->dev, "Please do Power-On Reset to initialize adapter in normal mode with full functionality.\n");
  13522. return false;
  13523. }
  13524. /**
  13525. * i40e_pf_loop_reset - perform reset in a loop.
  13526. * @pf: board private structure
  13527. *
  13528. * This function is useful when a NIC is about to enter recovery mode.
  13529. * When a NIC's internal data structures are corrupted the NIC's
  13530. * firmware is going to enter recovery mode.
  13531. * Right after a POR it takes about 7 minutes for firmware to enter
  13532. * recovery mode. Until that time a NIC is in some kind of intermediate
  13533. * state. After that time period the NIC almost surely enters
  13534. * recovery mode. The only way for a driver to detect intermediate
  13535. * state is to issue a series of pf-resets and check a return value.
  13536. * If a PF reset returns success then the firmware could be in recovery
  13537. * mode so the caller of this code needs to check for recovery mode
  13538. * if this function returns success. There is a little chance that
  13539. * firmware will hang in intermediate state forever.
  13540. * Since waiting 7 minutes is quite a lot of time this function waits
  13541. * 10 seconds and then gives up by returning an error.
  13542. *
  13543. * Return 0 on success, negative on failure.
  13544. **/
  13545. static int i40e_pf_loop_reset(struct i40e_pf *pf)
  13546. {
  13547. /* wait max 10 seconds for PF reset to succeed */
  13548. const unsigned long time_end = jiffies + 10 * HZ;
  13549. struct i40e_hw *hw = &pf->hw;
  13550. int ret;
  13551. ret = i40e_pf_reset(hw);
  13552. while (ret != 0 && time_before(jiffies, time_end)) {
  13553. usleep_range(10000, 20000);
  13554. ret = i40e_pf_reset(hw);
  13555. }
  13556. if (ret == 0)
  13557. pf->pfr_count++;
  13558. else
  13559. dev_info(&pf->pdev->dev, "PF reset failed: %d\n", ret);
  13560. return ret;
  13561. }
  13562. /**
  13563. * i40e_check_fw_empr - check if FW issued unexpected EMP Reset
  13564. * @pf: board private structure
  13565. *
  13566. * Check FW registers to determine if FW issued unexpected EMP Reset.
  13567. * Every time when unexpected EMP Reset occurs the FW increments
  13568. * a counter of unexpected EMP Resets. When the counter reaches 10
  13569. * the FW should enter the Recovery mode
  13570. *
  13571. * Returns true if FW issued unexpected EMP Reset
  13572. **/
  13573. static bool i40e_check_fw_empr(struct i40e_pf *pf)
  13574. {
  13575. const u32 fw_sts = rd32(&pf->hw, I40E_GL_FWSTS) &
  13576. I40E_GL_FWSTS_FWS1B_MASK;
  13577. return (fw_sts > I40E_GL_FWSTS_FWS1B_EMPR_0) &&
  13578. (fw_sts <= I40E_GL_FWSTS_FWS1B_EMPR_10);
  13579. }
  13580. /**
  13581. * i40e_handle_resets - handle EMP resets and PF resets
  13582. * @pf: board private structure
  13583. *
  13584. * Handle both EMP resets and PF resets and conclude whether there are
  13585. * any issues regarding these resets. If there are any issues then
  13586. * generate log entry.
  13587. *
  13588. * Return 0 if NIC is healthy or negative value when there are issues
  13589. * with resets
  13590. **/
  13591. static int i40e_handle_resets(struct i40e_pf *pf)
  13592. {
  13593. const int pfr = i40e_pf_loop_reset(pf);
  13594. const bool is_empr = i40e_check_fw_empr(pf);
  13595. if (is_empr || pfr != 0)
  13596. dev_crit(&pf->pdev->dev, "Entering recovery mode due to repeated FW resets. This may take several minutes. Refer to the Intel(R) Ethernet Adapters and Devices User Guide.\n");
  13597. return is_empr ? -EIO : pfr;
  13598. }
  13599. /**
  13600. * i40e_init_recovery_mode - initialize subsystems needed in recovery mode
  13601. * @pf: board private structure
  13602. * @hw: ptr to the hardware info
  13603. *
  13604. * This function does a minimal setup of all subsystems needed for running
  13605. * recovery mode.
  13606. *
  13607. * Returns 0 on success, negative on failure
  13608. **/
  13609. static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw)
  13610. {
  13611. struct i40e_vsi *vsi;
  13612. int err;
  13613. int v_idx;
  13614. pci_set_drvdata(pf->pdev, pf);
  13615. pci_save_state(pf->pdev);
  13616. /* set up periodic task facility */
  13617. timer_setup(&pf->service_timer, i40e_service_timer, 0);
  13618. pf->service_timer_period = HZ;
  13619. INIT_WORK(&pf->service_task, i40e_service_task);
  13620. clear_bit(__I40E_SERVICE_SCHED, pf->state);
  13621. err = i40e_init_interrupt_scheme(pf);
  13622. if (err)
  13623. goto err_switch_setup;
  13624. /* The number of VSIs reported by the FW is the minimum guaranteed
  13625. * to us; HW supports far more and we share the remaining pool with
  13626. * the other PFs. We allocate space for more than the guarantee with
  13627. * the understanding that we might not get them all later.
  13628. */
  13629. if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
  13630. pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
  13631. else
  13632. pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
  13633. /* Set up the vsi struct and our local tracking of the MAIN PF vsi. */
  13634. pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
  13635. GFP_KERNEL);
  13636. if (!pf->vsi) {
  13637. err = -ENOMEM;
  13638. goto err_switch_setup;
  13639. }
  13640. /* We allocate one VSI which is needed as absolute minimum
  13641. * in order to register the netdev
  13642. */
  13643. v_idx = i40e_vsi_mem_alloc(pf, I40E_VSI_MAIN);
  13644. if (v_idx < 0) {
  13645. err = v_idx;
  13646. goto err_switch_setup;
  13647. }
  13648. pf->lan_vsi = v_idx;
  13649. vsi = pf->vsi[v_idx];
  13650. if (!vsi) {
  13651. err = -EFAULT;
  13652. goto err_switch_setup;
  13653. }
  13654. vsi->alloc_queue_pairs = 1;
  13655. err = i40e_config_netdev(vsi);
  13656. if (err)
  13657. goto err_switch_setup;
  13658. err = register_netdev(vsi->netdev);
  13659. if (err)
  13660. goto err_switch_setup;
  13661. vsi->netdev_registered = true;
  13662. i40e_dbg_pf_init(pf);
  13663. err = i40e_setup_misc_vector_for_recovery_mode(pf);
  13664. if (err)
  13665. goto err_switch_setup;
  13666. /* tell the firmware that we're starting */
  13667. i40e_send_version(pf);
  13668. /* since everything's happy, start the service_task timer */
  13669. mod_timer(&pf->service_timer,
  13670. round_jiffies(jiffies + pf->service_timer_period));
  13671. return 0;
  13672. err_switch_setup:
  13673. i40e_reset_interrupt_capability(pf);
  13674. timer_shutdown_sync(&pf->service_timer);
  13675. i40e_shutdown_adminq(hw);
  13676. iounmap(hw->hw_addr);
  13677. pci_release_mem_regions(pf->pdev);
  13678. pci_disable_device(pf->pdev);
  13679. i40e_free_pf(pf);
  13680. return err;
  13681. }
  13682. /**
  13683. * i40e_set_subsystem_device_id - set subsystem device id
  13684. * @hw: pointer to the hardware info
  13685. *
  13686. * Set PCI subsystem device id either from a pci_dev structure or
  13687. * a specific FW register.
  13688. **/
  13689. static inline void i40e_set_subsystem_device_id(struct i40e_hw *hw)
  13690. {
  13691. struct i40e_pf *pf = i40e_hw_to_pf(hw);
  13692. hw->subsystem_device_id = pf->pdev->subsystem_device ?
  13693. pf->pdev->subsystem_device :
  13694. (ushort)(rd32(hw, I40E_PFPCI_SUBSYSID) & USHRT_MAX);
  13695. }
  13696. /**
  13697. * i40e_probe - Device initialization routine
  13698. * @pdev: PCI device information struct
  13699. * @ent: entry in i40e_pci_tbl
  13700. *
  13701. * i40e_probe initializes a PF identified by a pci_dev structure.
  13702. * The OS initialization, configuring of the PF private structure,
  13703. * and a hardware reset occur.
  13704. *
  13705. * Returns 0 on success, negative on failure
  13706. **/
  13707. static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  13708. {
  13709. struct i40e_aq_get_phy_abilities_resp abilities;
  13710. #ifdef CONFIG_I40E_DCB
  13711. enum i40e_get_fw_lldp_status_resp lldp_status;
  13712. #endif /* CONFIG_I40E_DCB */
  13713. struct i40e_vsi *vsi;
  13714. struct i40e_pf *pf;
  13715. struct i40e_hw *hw;
  13716. u16 wol_nvm_bits;
  13717. char nvm_ver[32];
  13718. u16 link_status;
  13719. #ifdef CONFIG_I40E_DCB
  13720. int status;
  13721. #endif /* CONFIG_I40E_DCB */
  13722. int err;
  13723. u32 val;
  13724. err = pci_enable_device_mem(pdev);
  13725. if (err)
  13726. return err;
  13727. /* set up for high or low dma */
  13728. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  13729. if (err) {
  13730. dev_err(&pdev->dev,
  13731. "DMA configuration failed: 0x%x\n", err);
  13732. goto err_dma;
  13733. }
  13734. /* set up pci connections */
  13735. err = pci_request_mem_regions(pdev, i40e_driver_name);
  13736. if (err) {
  13737. dev_info(&pdev->dev,
  13738. "pci_request_selected_regions failed %d\n", err);
  13739. goto err_pci_reg;
  13740. }
  13741. pci_set_master(pdev);
  13742. /* Now that we have a PCI connection, we need to do the
  13743. * low level device setup. This is primarily setting up
  13744. * the Admin Queue structures and then querying for the
  13745. * device's current profile information.
  13746. */
  13747. pf = i40e_alloc_pf(&pdev->dev);
  13748. if (!pf) {
  13749. err = -ENOMEM;
  13750. goto err_pf_alloc;
  13751. }
  13752. pf->next_vsi = 0;
  13753. pf->pdev = pdev;
  13754. set_bit(__I40E_DOWN, pf->state);
  13755. hw = &pf->hw;
  13756. pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
  13757. I40E_MAX_CSR_SPACE);
  13758. /* We believe that the highest register to read is
  13759. * I40E_GLGEN_STAT_CLEAR, so we check if the BAR size
  13760. * is not less than that before mapping to prevent a
  13761. * kernel panic.
  13762. */
  13763. if (pf->ioremap_len < I40E_GLGEN_STAT_CLEAR) {
  13764. dev_err(&pdev->dev, "Cannot map registers, bar size 0x%X too small, aborting\n",
  13765. pf->ioremap_len);
  13766. err = -ENOMEM;
  13767. goto err_ioremap;
  13768. }
  13769. hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
  13770. if (!hw->hw_addr) {
  13771. err = -EIO;
  13772. dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
  13773. (unsigned int)pci_resource_start(pdev, 0),
  13774. pf->ioremap_len, err);
  13775. goto err_ioremap;
  13776. }
  13777. hw->vendor_id = pdev->vendor;
  13778. hw->device_id = pdev->device;
  13779. pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
  13780. hw->subsystem_vendor_id = pdev->subsystem_vendor;
  13781. i40e_set_subsystem_device_id(hw);
  13782. hw->bus.device = PCI_SLOT(pdev->devfn);
  13783. hw->bus.func = PCI_FUNC(pdev->devfn);
  13784. hw->bus.bus_id = pdev->bus->number;
  13785. /* Select something other than the 802.1ad ethertype for the
  13786. * switch to use internally and drop on ingress.
  13787. */
  13788. hw->switch_tag = 0xffff;
  13789. hw->first_tag = ETH_P_8021AD;
  13790. hw->second_tag = ETH_P_8021Q;
  13791. INIT_LIST_HEAD(&pf->l3_flex_pit_list);
  13792. INIT_LIST_HEAD(&pf->l4_flex_pit_list);
  13793. INIT_LIST_HEAD(&pf->ddp_old_prof);
  13794. /* set up the locks for the AQ, do this only once in probe
  13795. * and destroy them only once in remove
  13796. */
  13797. mutex_init(&hw->aq.asq_mutex);
  13798. mutex_init(&hw->aq.arq_mutex);
  13799. pf->msg_enable = netif_msg_init(debug,
  13800. NETIF_MSG_DRV |
  13801. NETIF_MSG_PROBE |
  13802. NETIF_MSG_LINK);
  13803. if (debug < -1)
  13804. pf->hw.debug_mask = debug;
  13805. /* do a special CORER for clearing PXE mode once at init */
  13806. if (hw->revision_id == 0 &&
  13807. (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
  13808. wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
  13809. i40e_flush(hw);
  13810. msleep(200);
  13811. pf->corer_count++;
  13812. i40e_clear_pxe_mode(hw);
  13813. }
  13814. /* Reset here to make sure all is clean and to define PF 'n' */
  13815. i40e_clear_hw(hw);
  13816. err = i40e_set_mac_type(hw);
  13817. if (err) {
  13818. dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
  13819. err);
  13820. goto err_pf_reset;
  13821. }
  13822. err = i40e_handle_resets(pf);
  13823. if (err)
  13824. goto err_pf_reset;
  13825. i40e_check_recovery_mode(pf);
  13826. if (is_kdump_kernel()) {
  13827. hw->aq.num_arq_entries = I40E_MIN_ARQ_LEN;
  13828. hw->aq.num_asq_entries = I40E_MIN_ASQ_LEN;
  13829. } else {
  13830. hw->aq.num_arq_entries = I40E_AQ_LEN;
  13831. hw->aq.num_asq_entries = I40E_AQ_LEN;
  13832. }
  13833. hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
  13834. hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
  13835. snprintf(pf->int_name, sizeof(pf->int_name) - 1,
  13836. "%s-%s:misc",
  13837. dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
  13838. err = i40e_init_shared_code(hw);
  13839. if (err) {
  13840. dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
  13841. err);
  13842. goto err_pf_reset;
  13843. }
  13844. /* set up a default setting for link flow control */
  13845. pf->hw.fc.requested_mode = I40E_FC_NONE;
  13846. err = i40e_init_adminq(hw);
  13847. if (err) {
  13848. if (err == -EIO)
  13849. dev_info(&pdev->dev,
  13850. "The driver for the device stopped because the NVM image v%u.%u is newer than expected v%u.%u. You must install the most recent version of the network driver.\n",
  13851. hw->aq.api_maj_ver,
  13852. hw->aq.api_min_ver,
  13853. I40E_FW_API_VERSION_MAJOR,
  13854. I40E_FW_MINOR_VERSION(hw));
  13855. else
  13856. dev_info(&pdev->dev,
  13857. "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
  13858. goto err_pf_reset;
  13859. }
  13860. i40e_get_oem_version(hw);
  13861. i40e_get_pba_string(hw);
  13862. /* provide nvm, fw, api versions, vendor:device id, subsys vendor:device id */
  13863. i40e_nvm_version_str(hw, nvm_ver, sizeof(nvm_ver));
  13864. dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s [%04x:%04x] [%04x:%04x]\n",
  13865. hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
  13866. hw->aq.api_maj_ver, hw->aq.api_min_ver, nvm_ver,
  13867. hw->vendor_id, hw->device_id, hw->subsystem_vendor_id,
  13868. hw->subsystem_device_id);
  13869. if (i40e_is_aq_api_ver_ge(hw, I40E_FW_API_VERSION_MAJOR,
  13870. I40E_FW_MINOR_VERSION(hw) + 1))
  13871. dev_dbg(&pdev->dev,
  13872. "The driver for the device detected a newer version of the NVM image v%u.%u than v%u.%u.\n",
  13873. hw->aq.api_maj_ver,
  13874. hw->aq.api_min_ver,
  13875. I40E_FW_API_VERSION_MAJOR,
  13876. I40E_FW_MINOR_VERSION(hw));
  13877. else if (i40e_is_aq_api_ver_lt(hw, 1, 4))
  13878. dev_info(&pdev->dev,
  13879. "The driver for the device detected an older version of the NVM image v%u.%u than expected v%u.%u. Please update the NVM image.\n",
  13880. hw->aq.api_maj_ver,
  13881. hw->aq.api_min_ver,
  13882. I40E_FW_API_VERSION_MAJOR,
  13883. I40E_FW_MINOR_VERSION(hw));
  13884. i40e_verify_eeprom(pf);
  13885. /* Rev 0 hardware was never productized */
  13886. if (hw->revision_id < 1)
  13887. dev_warn(&pdev->dev, "This device is a pre-production adapter/LOM. Please be aware there may be issues with your hardware. If you are experiencing problems please contact your Intel or hardware representative who provided you with this hardware.\n");
  13888. i40e_clear_pxe_mode(hw);
  13889. err = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
  13890. if (err)
  13891. goto err_adminq_setup;
  13892. err = i40e_sw_init(pf);
  13893. if (err) {
  13894. dev_info(&pdev->dev, "sw_init failed: %d\n", err);
  13895. goto err_sw_init;
  13896. }
  13897. if (test_bit(__I40E_RECOVERY_MODE, pf->state))
  13898. return i40e_init_recovery_mode(pf, hw);
  13899. err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
  13900. hw->func_caps.num_rx_qp, 0, 0);
  13901. if (err) {
  13902. dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
  13903. goto err_init_lan_hmc;
  13904. }
  13905. err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
  13906. if (err) {
  13907. dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
  13908. err = -ENOENT;
  13909. goto err_configure_lan_hmc;
  13910. }
  13911. /* Disable LLDP for NICs that have firmware versions lower than v4.3.
  13912. * Ignore error return codes because if it was already disabled via
  13913. * hardware settings this will fail
  13914. */
  13915. if (test_bit(I40E_HW_CAP_STOP_FW_LLDP, pf->hw.caps)) {
  13916. dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
  13917. i40e_aq_stop_lldp(hw, true, false, NULL);
  13918. }
  13919. /* allow a platform config to override the HW addr */
  13920. i40e_get_platform_mac_addr(pdev, pf);
  13921. if (!is_valid_ether_addr(hw->mac.addr)) {
  13922. dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
  13923. err = -EIO;
  13924. goto err_mac_addr;
  13925. }
  13926. dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
  13927. ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
  13928. i40e_get_port_mac_addr(hw, hw->mac.port_addr);
  13929. if (is_valid_ether_addr(hw->mac.port_addr))
  13930. set_bit(I40E_HW_CAP_PORT_ID_VALID, pf->hw.caps);
  13931. i40e_ptp_alloc_pins(pf);
  13932. pci_set_drvdata(pdev, pf);
  13933. pci_save_state(pdev);
  13934. #ifdef CONFIG_I40E_DCB
  13935. status = i40e_get_fw_lldp_status(&pf->hw, &lldp_status);
  13936. (!status &&
  13937. lldp_status == I40E_GET_FW_LLDP_STATUS_ENABLED) ?
  13938. (clear_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags)) :
  13939. (set_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags));
  13940. dev_info(&pdev->dev,
  13941. test_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags) ?
  13942. "FW LLDP is disabled\n" :
  13943. "FW LLDP is enabled\n");
  13944. /* Enable FW to write default DCB config on link-up */
  13945. i40e_aq_set_dcb_parameters(hw, true, NULL);
  13946. err = i40e_init_pf_dcb(pf);
  13947. if (err) {
  13948. dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
  13949. clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
  13950. clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
  13951. /* Continue without DCB enabled */
  13952. }
  13953. #endif /* CONFIG_I40E_DCB */
  13954. /* set up periodic task facility */
  13955. timer_setup(&pf->service_timer, i40e_service_timer, 0);
  13956. pf->service_timer_period = HZ;
  13957. INIT_WORK(&pf->service_task, i40e_service_task);
  13958. clear_bit(__I40E_SERVICE_SCHED, pf->state);
  13959. /* NVM bit on means WoL disabled for the port */
  13960. i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
  13961. if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
  13962. pf->wol_en = false;
  13963. else
  13964. pf->wol_en = true;
  13965. device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
  13966. /* set up the main switch operations */
  13967. i40e_determine_queue_usage(pf);
  13968. err = i40e_init_interrupt_scheme(pf);
  13969. if (err)
  13970. goto err_switch_setup;
  13971. /* Reduce Tx and Rx pairs for kdump
  13972. * When MSI-X is enabled, it's not allowed to use more TC queue
  13973. * pairs than MSI-X vectors (pf->num_lan_msix) exist. Thus
  13974. * vsi->num_queue_pairs will be equal to pf->num_lan_msix, i.e., 1.
  13975. */
  13976. if (is_kdump_kernel())
  13977. pf->num_lan_msix = 1;
  13978. pf->udp_tunnel_nic.set_port = i40e_udp_tunnel_set_port;
  13979. pf->udp_tunnel_nic.unset_port = i40e_udp_tunnel_unset_port;
  13980. pf->udp_tunnel_nic.flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP;
  13981. pf->udp_tunnel_nic.shared = &pf->udp_tunnel_shared;
  13982. pf->udp_tunnel_nic.tables[0].n_entries = I40E_MAX_PF_UDP_OFFLOAD_PORTS;
  13983. pf->udp_tunnel_nic.tables[0].tunnel_types = UDP_TUNNEL_TYPE_VXLAN |
  13984. UDP_TUNNEL_TYPE_GENEVE;
  13985. /* The number of VSIs reported by the FW is the minimum guaranteed
  13986. * to us; HW supports far more and we share the remaining pool with
  13987. * the other PFs. We allocate space for more than the guarantee with
  13988. * the understanding that we might not get them all later.
  13989. */
  13990. if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
  13991. pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
  13992. else
  13993. pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
  13994. if (pf->num_alloc_vsi > UDP_TUNNEL_NIC_MAX_SHARING_DEVICES) {
  13995. dev_warn(&pf->pdev->dev,
  13996. "limiting the VSI count due to UDP tunnel limitation %d > %d\n",
  13997. pf->num_alloc_vsi, UDP_TUNNEL_NIC_MAX_SHARING_DEVICES);
  13998. pf->num_alloc_vsi = UDP_TUNNEL_NIC_MAX_SHARING_DEVICES;
  13999. }
  14000. /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
  14001. pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
  14002. GFP_KERNEL);
  14003. if (!pf->vsi) {
  14004. err = -ENOMEM;
  14005. goto err_switch_setup;
  14006. }
  14007. #ifdef CONFIG_PCI_IOV
  14008. /* prep for VF support */
  14009. if (test_bit(I40E_FLAG_SRIOV_ENA, pf->flags) &&
  14010. test_bit(I40E_FLAG_MSIX_ENA, pf->flags) &&
  14011. !test_bit(__I40E_BAD_EEPROM, pf->state)) {
  14012. if (pci_num_vf(pdev))
  14013. set_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags);
  14014. }
  14015. #endif
  14016. err = i40e_setup_pf_switch(pf, false, false);
  14017. if (err) {
  14018. dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
  14019. goto err_vsis;
  14020. }
  14021. vsi = i40e_pf_get_main_vsi(pf);
  14022. INIT_LIST_HEAD(&vsi->ch_list);
  14023. /* if FDIR VSI was set up, start it now */
  14024. vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
  14025. if (vsi)
  14026. i40e_vsi_open(vsi);
  14027. /* The driver only wants link up/down and module qualification
  14028. * reports from firmware. Note the negative logic.
  14029. */
  14030. err = i40e_aq_set_phy_int_mask(&pf->hw,
  14031. ~(I40E_AQ_EVENT_LINK_UPDOWN |
  14032. I40E_AQ_EVENT_MEDIA_NA |
  14033. I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
  14034. if (err)
  14035. dev_info(&pf->pdev->dev, "set phy mask fail, err %pe aq_err %s\n",
  14036. ERR_PTR(err),
  14037. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  14038. /* Reconfigure hardware for allowing smaller MSS in the case
  14039. * of TSO, so that we avoid the MDD being fired and causing
  14040. * a reset in the case of small MSS+TSO.
  14041. */
  14042. val = rd32(hw, I40E_REG_MSS);
  14043. if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
  14044. val &= ~I40E_REG_MSS_MIN_MASK;
  14045. val |= I40E_64BYTE_MSS;
  14046. wr32(hw, I40E_REG_MSS, val);
  14047. }
  14048. if (test_bit(I40E_HW_CAP_RESTART_AUTONEG, pf->hw.caps)) {
  14049. msleep(75);
  14050. err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
  14051. if (err)
  14052. dev_info(&pf->pdev->dev, "link restart failed, err %pe aq_err %s\n",
  14053. ERR_PTR(err),
  14054. i40e_aq_str(&pf->hw,
  14055. pf->hw.aq.asq_last_status));
  14056. }
  14057. /* The main driver is (mostly) up and happy. We need to set this state
  14058. * before setting up the misc vector or we get a race and the vector
  14059. * ends up disabled forever.
  14060. */
  14061. clear_bit(__I40E_DOWN, pf->state);
  14062. /* In case of MSIX we are going to setup the misc vector right here
  14063. * to handle admin queue events etc. In case of legacy and MSI
  14064. * the misc functionality and queue processing is combined in
  14065. * the same vector and that gets setup at open.
  14066. */
  14067. if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
  14068. err = i40e_setup_misc_vector(pf);
  14069. if (err) {
  14070. dev_info(&pdev->dev,
  14071. "setup of misc vector failed: %d\n", err);
  14072. i40e_cloud_filter_exit(pf);
  14073. i40e_fdir_teardown(pf);
  14074. goto err_vsis;
  14075. }
  14076. }
  14077. #ifdef CONFIG_PCI_IOV
  14078. /* prep for VF support */
  14079. if (test_bit(I40E_FLAG_SRIOV_ENA, pf->flags) &&
  14080. test_bit(I40E_FLAG_MSIX_ENA, pf->flags) &&
  14081. !test_bit(__I40E_BAD_EEPROM, pf->state)) {
  14082. /* disable link interrupts for VFs */
  14083. val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
  14084. val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
  14085. wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
  14086. i40e_flush(hw);
  14087. if (pci_num_vf(pdev)) {
  14088. dev_info(&pdev->dev,
  14089. "Active VFs found, allocating resources.\n");
  14090. err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
  14091. if (err)
  14092. dev_info(&pdev->dev,
  14093. "Error %d allocating resources for existing VFs\n",
  14094. err);
  14095. }
  14096. }
  14097. #endif /* CONFIG_PCI_IOV */
  14098. if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
  14099. pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
  14100. pf->num_iwarp_msix,
  14101. I40E_IWARP_IRQ_PILE_ID);
  14102. if (pf->iwarp_base_vector < 0) {
  14103. dev_info(&pdev->dev,
  14104. "failed to get tracking for %d vectors for IWARP err=%d\n",
  14105. pf->num_iwarp_msix, pf->iwarp_base_vector);
  14106. clear_bit(I40E_FLAG_IWARP_ENA, pf->flags);
  14107. }
  14108. }
  14109. i40e_dbg_pf_init(pf);
  14110. /* tell the firmware that we're starting */
  14111. i40e_send_version(pf);
  14112. /* since everything's happy, start the service_task timer */
  14113. mod_timer(&pf->service_timer,
  14114. round_jiffies(jiffies + pf->service_timer_period));
  14115. /* add this PF to client device list and launch a client service task */
  14116. if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
  14117. err = i40e_lan_add_device(pf);
  14118. if (err)
  14119. dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
  14120. err);
  14121. }
  14122. #define PCI_SPEED_SIZE 8
  14123. #define PCI_WIDTH_SIZE 8
  14124. /* Devices on the IOSF bus do not have this information
  14125. * and will report PCI Gen 1 x 1 by default so don't bother
  14126. * checking them.
  14127. */
  14128. if (!test_bit(I40E_HW_CAP_NO_PCI_LINK_CHECK, pf->hw.caps)) {
  14129. char speed[PCI_SPEED_SIZE] = "Unknown";
  14130. char width[PCI_WIDTH_SIZE] = "Unknown";
  14131. /* Get the negotiated link width and speed from PCI config
  14132. * space
  14133. */
  14134. pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
  14135. &link_status);
  14136. i40e_set_pci_config_data(hw, link_status);
  14137. switch (hw->bus.speed) {
  14138. case i40e_bus_speed_8000:
  14139. strscpy(speed, "8.0", PCI_SPEED_SIZE); break;
  14140. case i40e_bus_speed_5000:
  14141. strscpy(speed, "5.0", PCI_SPEED_SIZE); break;
  14142. case i40e_bus_speed_2500:
  14143. strscpy(speed, "2.5", PCI_SPEED_SIZE); break;
  14144. default:
  14145. break;
  14146. }
  14147. switch (hw->bus.width) {
  14148. case i40e_bus_width_pcie_x8:
  14149. strscpy(width, "8", PCI_WIDTH_SIZE); break;
  14150. case i40e_bus_width_pcie_x4:
  14151. strscpy(width, "4", PCI_WIDTH_SIZE); break;
  14152. case i40e_bus_width_pcie_x2:
  14153. strscpy(width, "2", PCI_WIDTH_SIZE); break;
  14154. case i40e_bus_width_pcie_x1:
  14155. strscpy(width, "1", PCI_WIDTH_SIZE); break;
  14156. default:
  14157. break;
  14158. }
  14159. dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
  14160. speed, width);
  14161. if (hw->bus.width < i40e_bus_width_pcie_x8 ||
  14162. hw->bus.speed < i40e_bus_speed_8000) {
  14163. dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
  14164. dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
  14165. }
  14166. }
  14167. /* get the requested speeds from the fw */
  14168. err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
  14169. if (err)
  14170. dev_dbg(&pf->pdev->dev, "get requested speeds ret = %pe last_status = %s\n",
  14171. ERR_PTR(err),
  14172. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  14173. pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
  14174. /* set the FEC config due to the board capabilities */
  14175. i40e_set_fec_in_flags(abilities.fec_cfg_curr_mod_ext_info, pf->flags);
  14176. /* get the supported phy types from the fw */
  14177. err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
  14178. if (err)
  14179. dev_dbg(&pf->pdev->dev, "get supported phy types ret = %pe last_status = %s\n",
  14180. ERR_PTR(err),
  14181. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  14182. /* make sure the MFS hasn't been set lower than the default */
  14183. #define MAX_FRAME_SIZE_DEFAULT 0x2600
  14184. val = FIELD_GET(I40E_PRTGL_SAH_MFS_MASK,
  14185. rd32(&pf->hw, I40E_PRTGL_SAH));
  14186. if (val < MAX_FRAME_SIZE_DEFAULT)
  14187. dev_warn(&pdev->dev, "MFS for port %x (%d) has been set below the default (%d)\n",
  14188. pf->hw.port, val, MAX_FRAME_SIZE_DEFAULT);
  14189. /* Add a filter to drop all Flow control frames from any VSI from being
  14190. * transmitted. By doing so we stop a malicious VF from sending out
  14191. * PAUSE or PFC frames and potentially controlling traffic for other
  14192. * PF/VF VSIs.
  14193. * The FW can still send Flow control frames if enabled.
  14194. */
  14195. i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
  14196. pf->main_vsi_seid);
  14197. if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
  14198. (pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
  14199. set_bit(I40E_HW_CAP_PHY_CONTROLS_LEDS, pf->hw.caps);
  14200. if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
  14201. set_bit(I40E_HW_CAP_CRT_RETIMER, pf->hw.caps);
  14202. /* print a string summarizing features */
  14203. i40e_print_features(pf);
  14204. i40e_devlink_register(pf);
  14205. return 0;
  14206. /* Unwind what we've done if something failed in the setup */
  14207. err_vsis:
  14208. set_bit(__I40E_DOWN, pf->state);
  14209. i40e_clear_interrupt_scheme(pf);
  14210. kfree(pf->vsi);
  14211. err_switch_setup:
  14212. i40e_reset_interrupt_capability(pf);
  14213. timer_shutdown_sync(&pf->service_timer);
  14214. err_mac_addr:
  14215. err_configure_lan_hmc:
  14216. (void)i40e_shutdown_lan_hmc(hw);
  14217. err_init_lan_hmc:
  14218. kfree(pf->qp_pile);
  14219. err_sw_init:
  14220. err_adminq_setup:
  14221. err_pf_reset:
  14222. iounmap(hw->hw_addr);
  14223. err_ioremap:
  14224. i40e_free_pf(pf);
  14225. err_pf_alloc:
  14226. pci_release_mem_regions(pdev);
  14227. err_pci_reg:
  14228. err_dma:
  14229. pci_disable_device(pdev);
  14230. return err;
  14231. }
  14232. /**
  14233. * i40e_remove - Device removal routine
  14234. * @pdev: PCI device information struct
  14235. *
  14236. * i40e_remove is called by the PCI subsystem to alert the driver
  14237. * that is should release a PCI device. This could be caused by a
  14238. * Hot-Plug event, or because the driver is going to be removed from
  14239. * memory.
  14240. **/
  14241. static void i40e_remove(struct pci_dev *pdev)
  14242. {
  14243. struct i40e_pf *pf = pci_get_drvdata(pdev);
  14244. struct i40e_hw *hw = &pf->hw;
  14245. struct i40e_vsi *vsi;
  14246. struct i40e_veb *veb;
  14247. int ret_code;
  14248. int i;
  14249. i40e_devlink_unregister(pf);
  14250. i40e_dbg_pf_exit(pf);
  14251. i40e_ptp_stop(pf);
  14252. /* Disable RSS in hw */
  14253. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
  14254. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
  14255. /* Grab __I40E_RESET_RECOVERY_PENDING and set __I40E_IN_REMOVE
  14256. * flags, once they are set, i40e_rebuild should not be called as
  14257. * i40e_prep_for_reset always returns early.
  14258. */
  14259. while (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
  14260. usleep_range(1000, 2000);
  14261. set_bit(__I40E_IN_REMOVE, pf->state);
  14262. if (test_bit(I40E_FLAG_SRIOV_ENA, pf->flags)) {
  14263. set_bit(__I40E_VF_RESETS_DISABLED, pf->state);
  14264. i40e_free_vfs(pf);
  14265. clear_bit(I40E_FLAG_SRIOV_ENA, pf->flags);
  14266. }
  14267. /* no more scheduling of any task */
  14268. set_bit(__I40E_SUSPENDED, pf->state);
  14269. set_bit(__I40E_DOWN, pf->state);
  14270. if (pf->service_timer.function)
  14271. timer_shutdown_sync(&pf->service_timer);
  14272. if (pf->service_task.func)
  14273. cancel_work_sync(&pf->service_task);
  14274. if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
  14275. struct i40e_vsi *vsi = pf->vsi[0];
  14276. /* We know that we have allocated only one vsi for this PF,
  14277. * it was just for registering netdevice, so the interface
  14278. * could be visible in the 'ifconfig' output
  14279. */
  14280. unregister_netdev(vsi->netdev);
  14281. free_netdev(vsi->netdev);
  14282. goto unmap;
  14283. }
  14284. /* Client close must be called explicitly here because the timer
  14285. * has been stopped.
  14286. */
  14287. i40e_notify_client_of_netdev_close(pf, false);
  14288. i40e_fdir_teardown(pf);
  14289. /* If there is a switch structure or any orphans, remove them.
  14290. * This will leave only the PF's VSI remaining.
  14291. */
  14292. i40e_pf_for_each_veb(pf, i, veb)
  14293. if (veb->uplink_seid == pf->mac_seid ||
  14294. veb->uplink_seid == 0)
  14295. i40e_switch_branch_release(veb);
  14296. /* Now we can shutdown the PF's VSIs, just before we kill
  14297. * adminq and hmc.
  14298. */
  14299. i40e_pf_for_each_vsi(pf, i, vsi) {
  14300. i40e_vsi_close(vsi);
  14301. i40e_vsi_release(vsi);
  14302. pf->vsi[i] = NULL;
  14303. }
  14304. i40e_cloud_filter_exit(pf);
  14305. /* remove attached clients */
  14306. if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
  14307. ret_code = i40e_lan_del_device(pf);
  14308. if (ret_code)
  14309. dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
  14310. ret_code);
  14311. }
  14312. /* shutdown and destroy the HMC */
  14313. if (hw->hmc.hmc_obj) {
  14314. ret_code = i40e_shutdown_lan_hmc(hw);
  14315. if (ret_code)
  14316. dev_warn(&pdev->dev,
  14317. "Failed to destroy the HMC resources: %d\n",
  14318. ret_code);
  14319. }
  14320. unmap:
  14321. /* Free MSI/legacy interrupt 0 when in recovery mode. */
  14322. if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
  14323. !test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
  14324. free_irq(pf->pdev->irq, pf);
  14325. /* shutdown the adminq */
  14326. i40e_shutdown_adminq(hw);
  14327. /* destroy the locks only once, here */
  14328. mutex_destroy(&hw->aq.arq_mutex);
  14329. mutex_destroy(&hw->aq.asq_mutex);
  14330. /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
  14331. rtnl_lock();
  14332. i40e_clear_interrupt_scheme(pf);
  14333. i40e_pf_for_each_vsi(pf, i, vsi) {
  14334. if (!test_bit(__I40E_RECOVERY_MODE, pf->state))
  14335. i40e_vsi_clear_rings(vsi);
  14336. i40e_vsi_clear(vsi);
  14337. pf->vsi[i] = NULL;
  14338. }
  14339. rtnl_unlock();
  14340. i40e_pf_for_each_veb(pf, i, veb) {
  14341. kfree(veb);
  14342. pf->veb[i] = NULL;
  14343. }
  14344. kfree(pf->qp_pile);
  14345. kfree(pf->vsi);
  14346. iounmap(hw->hw_addr);
  14347. i40e_free_pf(pf);
  14348. pci_release_mem_regions(pdev);
  14349. pci_disable_device(pdev);
  14350. }
  14351. /**
  14352. * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
  14353. * using the mac_address_write admin q function
  14354. * @pf: pointer to i40e_pf struct
  14355. **/
  14356. static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
  14357. {
  14358. struct i40e_vsi *main_vsi = i40e_pf_get_main_vsi(pf);
  14359. struct i40e_hw *hw = &pf->hw;
  14360. u8 mac_addr[6];
  14361. u16 flags = 0;
  14362. int ret;
  14363. /* Get current MAC address in case it's an LAA */
  14364. if (main_vsi && main_vsi->netdev) {
  14365. ether_addr_copy(mac_addr, main_vsi->netdev->dev_addr);
  14366. } else {
  14367. dev_err(&pf->pdev->dev,
  14368. "Failed to retrieve MAC address; using default\n");
  14369. ether_addr_copy(mac_addr, hw->mac.addr);
  14370. }
  14371. /* The FW expects the mac address write cmd to first be called with
  14372. * one of these flags before calling it again with the multicast
  14373. * enable flags.
  14374. */
  14375. flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
  14376. if (hw->func_caps.flex10_enable && hw->partition_id != 1)
  14377. flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
  14378. ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
  14379. if (ret) {
  14380. dev_err(&pf->pdev->dev,
  14381. "Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
  14382. return;
  14383. }
  14384. flags = I40E_AQC_MC_MAG_EN
  14385. | I40E_AQC_WOL_PRESERVE_ON_PFR
  14386. | I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
  14387. ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
  14388. if (ret)
  14389. dev_err(&pf->pdev->dev,
  14390. "Failed to enable Multicast Magic Packet wake up\n");
  14391. }
  14392. /**
  14393. * i40e_io_suspend - suspend all IO operations
  14394. * @pf: pointer to i40e_pf struct
  14395. *
  14396. **/
  14397. static int i40e_io_suspend(struct i40e_pf *pf)
  14398. {
  14399. struct i40e_hw *hw = &pf->hw;
  14400. set_bit(__I40E_DOWN, pf->state);
  14401. /* Ensure service task will not be running */
  14402. del_timer_sync(&pf->service_timer);
  14403. cancel_work_sync(&pf->service_task);
  14404. /* Client close must be called explicitly here because the timer
  14405. * has been stopped.
  14406. */
  14407. i40e_notify_client_of_netdev_close(pf, false);
  14408. if (test_bit(I40E_HW_CAP_WOL_MC_MAGIC_PKT_WAKE, pf->hw.caps) &&
  14409. pf->wol_en)
  14410. i40e_enable_mc_magic_wake(pf);
  14411. /* Since we're going to destroy queues during the
  14412. * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
  14413. * whole section
  14414. */
  14415. rtnl_lock();
  14416. i40e_prep_for_reset(pf);
  14417. wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
  14418. wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
  14419. /* Clear the interrupt scheme and release our IRQs so that the system
  14420. * can safely hibernate even when there are a large number of CPUs.
  14421. * Otherwise hibernation might fail when mapping all the vectors back
  14422. * to CPU0.
  14423. */
  14424. i40e_clear_interrupt_scheme(pf);
  14425. rtnl_unlock();
  14426. return 0;
  14427. }
  14428. /**
  14429. * i40e_io_resume - resume IO operations
  14430. * @pf: pointer to i40e_pf struct
  14431. *
  14432. **/
  14433. static int i40e_io_resume(struct i40e_pf *pf)
  14434. {
  14435. struct device *dev = &pf->pdev->dev;
  14436. int err;
  14437. /* We need to hold the RTNL lock prior to restoring interrupt schemes,
  14438. * since we're going to be restoring queues
  14439. */
  14440. rtnl_lock();
  14441. /* We cleared the interrupt scheme when we suspended, so we need to
  14442. * restore it now to resume device functionality.
  14443. */
  14444. err = i40e_restore_interrupt_scheme(pf);
  14445. if (err) {
  14446. dev_err(dev, "Cannot restore interrupt scheme: %d\n",
  14447. err);
  14448. }
  14449. clear_bit(__I40E_DOWN, pf->state);
  14450. i40e_reset_and_rebuild(pf, false, true);
  14451. rtnl_unlock();
  14452. /* Clear suspended state last after everything is recovered */
  14453. clear_bit(__I40E_SUSPENDED, pf->state);
  14454. /* Restart the service task */
  14455. mod_timer(&pf->service_timer,
  14456. round_jiffies(jiffies + pf->service_timer_period));
  14457. return 0;
  14458. }
  14459. /**
  14460. * i40e_pci_error_detected - warning that something funky happened in PCI land
  14461. * @pdev: PCI device information struct
  14462. * @error: the type of PCI error
  14463. *
  14464. * Called to warn that something happened and the error handling steps
  14465. * are in progress. Allows the driver to quiesce things, be ready for
  14466. * remediation.
  14467. **/
  14468. static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
  14469. pci_channel_state_t error)
  14470. {
  14471. struct i40e_pf *pf = pci_get_drvdata(pdev);
  14472. dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
  14473. if (!pf) {
  14474. dev_info(&pdev->dev,
  14475. "Cannot recover - error happened during device probe\n");
  14476. return PCI_ERS_RESULT_DISCONNECT;
  14477. }
  14478. /* shutdown all operations */
  14479. if (!test_bit(__I40E_SUSPENDED, pf->state))
  14480. i40e_io_suspend(pf);
  14481. /* Request a slot reset */
  14482. return PCI_ERS_RESULT_NEED_RESET;
  14483. }
  14484. /**
  14485. * i40e_pci_error_slot_reset - a PCI slot reset just happened
  14486. * @pdev: PCI device information struct
  14487. *
  14488. * Called to find if the driver can work with the device now that
  14489. * the pci slot has been reset. If a basic connection seems good
  14490. * (registers are readable and have sane content) then return a
  14491. * happy little PCI_ERS_RESULT_xxx.
  14492. **/
  14493. static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
  14494. {
  14495. struct i40e_pf *pf = pci_get_drvdata(pdev);
  14496. pci_ers_result_t result;
  14497. u32 reg;
  14498. dev_dbg(&pdev->dev, "%s\n", __func__);
  14499. /* enable I/O and memory of the device */
  14500. if (pci_enable_device(pdev)) {
  14501. dev_info(&pdev->dev,
  14502. "Cannot re-enable PCI device after reset.\n");
  14503. result = PCI_ERS_RESULT_DISCONNECT;
  14504. } else {
  14505. pci_set_master(pdev);
  14506. pci_restore_state(pdev);
  14507. pci_save_state(pdev);
  14508. pci_wake_from_d3(pdev, false);
  14509. reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  14510. if (reg == 0)
  14511. result = PCI_ERS_RESULT_RECOVERED;
  14512. else
  14513. result = PCI_ERS_RESULT_DISCONNECT;
  14514. }
  14515. return result;
  14516. }
  14517. /**
  14518. * i40e_pci_error_reset_prepare - prepare device driver for pci reset
  14519. * @pdev: PCI device information struct
  14520. */
  14521. static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
  14522. {
  14523. struct i40e_pf *pf = pci_get_drvdata(pdev);
  14524. i40e_prep_for_reset(pf);
  14525. }
  14526. /**
  14527. * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
  14528. * @pdev: PCI device information struct
  14529. */
  14530. static void i40e_pci_error_reset_done(struct pci_dev *pdev)
  14531. {
  14532. struct i40e_pf *pf = pci_get_drvdata(pdev);
  14533. if (test_bit(__I40E_IN_REMOVE, pf->state))
  14534. return;
  14535. i40e_reset_and_rebuild(pf, false, false);
  14536. #ifdef CONFIG_PCI_IOV
  14537. i40e_restore_all_vfs_msi_state(pdev);
  14538. #endif /* CONFIG_PCI_IOV */
  14539. }
  14540. /**
  14541. * i40e_pci_error_resume - restart operations after PCI error recovery
  14542. * @pdev: PCI device information struct
  14543. *
  14544. * Called to allow the driver to bring things back up after PCI error
  14545. * and/or reset recovery has finished.
  14546. **/
  14547. static void i40e_pci_error_resume(struct pci_dev *pdev)
  14548. {
  14549. struct i40e_pf *pf = pci_get_drvdata(pdev);
  14550. dev_dbg(&pdev->dev, "%s\n", __func__);
  14551. if (test_bit(__I40E_SUSPENDED, pf->state))
  14552. return;
  14553. i40e_io_resume(pf);
  14554. }
  14555. /**
  14556. * i40e_shutdown - PCI callback for shutting down
  14557. * @pdev: PCI device information struct
  14558. **/
  14559. static void i40e_shutdown(struct pci_dev *pdev)
  14560. {
  14561. struct i40e_pf *pf = pci_get_drvdata(pdev);
  14562. struct i40e_hw *hw = &pf->hw;
  14563. set_bit(__I40E_SUSPENDED, pf->state);
  14564. set_bit(__I40E_DOWN, pf->state);
  14565. del_timer_sync(&pf->service_timer);
  14566. cancel_work_sync(&pf->service_task);
  14567. i40e_cloud_filter_exit(pf);
  14568. i40e_fdir_teardown(pf);
  14569. /* Client close must be called explicitly here because the timer
  14570. * has been stopped.
  14571. */
  14572. i40e_notify_client_of_netdev_close(pf, false);
  14573. if (test_bit(I40E_HW_CAP_WOL_MC_MAGIC_PKT_WAKE, pf->hw.caps) &&
  14574. pf->wol_en)
  14575. i40e_enable_mc_magic_wake(pf);
  14576. i40e_prep_for_reset(pf);
  14577. wr32(hw, I40E_PFPM_APM,
  14578. (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
  14579. wr32(hw, I40E_PFPM_WUFC,
  14580. (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
  14581. /* Free MSI/legacy interrupt 0 when in recovery mode. */
  14582. if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
  14583. !test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
  14584. free_irq(pf->pdev->irq, pf);
  14585. /* Since we're going to destroy queues during the
  14586. * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
  14587. * whole section
  14588. */
  14589. rtnl_lock();
  14590. i40e_clear_interrupt_scheme(pf);
  14591. rtnl_unlock();
  14592. if (system_state == SYSTEM_POWER_OFF) {
  14593. pci_wake_from_d3(pdev, pf->wol_en);
  14594. pci_set_power_state(pdev, PCI_D3hot);
  14595. }
  14596. }
  14597. /**
  14598. * i40e_suspend - PM callback for moving to D3
  14599. * @dev: generic device information structure
  14600. **/
  14601. static int i40e_suspend(struct device *dev)
  14602. {
  14603. struct i40e_pf *pf = dev_get_drvdata(dev);
  14604. /* If we're already suspended, then there is nothing to do */
  14605. if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
  14606. return 0;
  14607. return i40e_io_suspend(pf);
  14608. }
  14609. /**
  14610. * i40e_resume - PM callback for waking up from D3
  14611. * @dev: generic device information structure
  14612. **/
  14613. static int i40e_resume(struct device *dev)
  14614. {
  14615. struct i40e_pf *pf = dev_get_drvdata(dev);
  14616. /* If we're not suspended, then there is nothing to do */
  14617. if (!test_bit(__I40E_SUSPENDED, pf->state))
  14618. return 0;
  14619. return i40e_io_resume(pf);
  14620. }
  14621. static const struct pci_error_handlers i40e_err_handler = {
  14622. .error_detected = i40e_pci_error_detected,
  14623. .slot_reset = i40e_pci_error_slot_reset,
  14624. .reset_prepare = i40e_pci_error_reset_prepare,
  14625. .reset_done = i40e_pci_error_reset_done,
  14626. .resume = i40e_pci_error_resume,
  14627. };
  14628. static DEFINE_SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
  14629. static struct pci_driver i40e_driver = {
  14630. .name = i40e_driver_name,
  14631. .id_table = i40e_pci_tbl,
  14632. .probe = i40e_probe,
  14633. .remove = i40e_remove,
  14634. .driver.pm = pm_sleep_ptr(&i40e_pm_ops),
  14635. .shutdown = i40e_shutdown,
  14636. .err_handler = &i40e_err_handler,
  14637. .sriov_configure = i40e_pci_sriov_configure,
  14638. };
  14639. /**
  14640. * i40e_init_module - Driver registration routine
  14641. *
  14642. * i40e_init_module is the first routine called when the driver is
  14643. * loaded. All it does is register with the PCI subsystem.
  14644. **/
  14645. static int __init i40e_init_module(void)
  14646. {
  14647. int err;
  14648. pr_info("%s: %s\n", i40e_driver_name, i40e_driver_string);
  14649. pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
  14650. /* There is no need to throttle the number of active tasks because
  14651. * each device limits its own task using a state bit for scheduling
  14652. * the service task, and the device tasks do not interfere with each
  14653. * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
  14654. * since we need to be able to guarantee forward progress even under
  14655. * memory pressure.
  14656. */
  14657. i40e_wq = alloc_workqueue("%s", 0, 0, i40e_driver_name);
  14658. if (!i40e_wq) {
  14659. pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
  14660. return -ENOMEM;
  14661. }
  14662. i40e_dbg_init();
  14663. err = pci_register_driver(&i40e_driver);
  14664. if (err) {
  14665. destroy_workqueue(i40e_wq);
  14666. i40e_dbg_exit();
  14667. return err;
  14668. }
  14669. return 0;
  14670. }
  14671. module_init(i40e_init_module);
  14672. /**
  14673. * i40e_exit_module - Driver exit cleanup routine
  14674. *
  14675. * i40e_exit_module is called just before the driver is removed
  14676. * from memory.
  14677. **/
  14678. static void __exit i40e_exit_module(void)
  14679. {
  14680. pci_unregister_driver(&i40e_driver);
  14681. destroy_workqueue(i40e_wq);
  14682. ida_destroy(&i40e_client_ida);
  14683. i40e_dbg_exit();
  14684. }
  14685. module_exit(i40e_exit_module);