i40e_main.c 337 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193
  1. /*******************************************************************************
  2. *
  3. * Intel Ethernet Controller XL710 Family Linux Driver
  4. * Copyright(c) 2013 - 2017 Intel Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * The full GNU General Public License is included in this distribution in
  19. * the file called "COPYING".
  20. *
  21. * Contact Information:
  22. * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  23. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  24. *
  25. ******************************************************************************/
  26. #include <linux/etherdevice.h>
  27. #include <linux/of_net.h>
  28. #include <linux/pci.h>
  29. #include <linux/bpf.h>
  30. /* Local includes */
  31. #include "i40e.h"
  32. #include "i40e_diag.h"
  33. #include <net/udp_tunnel.h>
  34. /* All i40e tracepoints are defined by the include below, which
  35. * must be included exactly once across the whole kernel with
  36. * CREATE_TRACE_POINTS defined
  37. */
  38. #define CREATE_TRACE_POINTS
  39. #include "i40e_trace.h"
  40. const char i40e_driver_name[] = "i40e";
  41. static const char i40e_driver_string[] =
  42. "Intel(R) Ethernet Connection XL710 Network Driver";
  43. #define DRV_KERN "-k"
  44. #define DRV_VERSION_MAJOR 2
  45. #define DRV_VERSION_MINOR 1
  46. #define DRV_VERSION_BUILD 14
  47. #define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
  48. __stringify(DRV_VERSION_MINOR) "." \
  49. __stringify(DRV_VERSION_BUILD) DRV_KERN
  50. const char i40e_driver_version_str[] = DRV_VERSION;
  51. static const char i40e_copyright[] = "Copyright (c) 2013 - 2014 Intel Corporation.";
  52. /* a bit of forward declarations */
  53. static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
  54. static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired);
  55. static int i40e_add_vsi(struct i40e_vsi *vsi);
  56. static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
  57. static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit);
  58. static int i40e_setup_misc_vector(struct i40e_pf *pf);
  59. static void i40e_determine_queue_usage(struct i40e_pf *pf);
  60. static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
  61. static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired);
  62. static int i40e_reset(struct i40e_pf *pf);
  63. static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired);
  64. static void i40e_fdir_sb_setup(struct i40e_pf *pf);
  65. static int i40e_veb_get_bw_info(struct i40e_veb *veb);
  66. /* i40e_pci_tbl - PCI Device ID Table
  67. *
  68. * Last entry must be all 0s
  69. *
  70. * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
  71. * Class, Class Mask, private data (not used) }
  72. */
  73. static const struct pci_device_id i40e_pci_tbl[] = {
  74. {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
  75. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
  76. {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
  77. {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
  78. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
  79. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
  80. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
  81. {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
  82. {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
  83. {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
  84. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
  85. {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
  86. {PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
  87. {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
  88. {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
  89. {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
  90. {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
  91. {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
  92. {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
  93. /* required last entry */
  94. {0, }
  95. };
  96. MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
  97. #define I40E_MAX_VF_COUNT 128
  98. static int debug = -1;
  99. module_param(debug, uint, 0);
  100. MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
  101. MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
  102. MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
  103. MODULE_LICENSE("GPL");
  104. MODULE_VERSION(DRV_VERSION);
  105. static struct workqueue_struct *i40e_wq;
  106. /**
  107. * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
  108. * @hw: pointer to the HW structure
  109. * @mem: ptr to mem struct to fill out
  110. * @size: size of memory requested
  111. * @alignment: what to align the allocation to
  112. **/
  113. int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
  114. u64 size, u32 alignment)
  115. {
  116. struct i40e_pf *pf = (struct i40e_pf *)hw->back;
  117. mem->size = ALIGN(size, alignment);
  118. mem->va = dma_zalloc_coherent(&pf->pdev->dev, mem->size,
  119. &mem->pa, GFP_KERNEL);
  120. if (!mem->va)
  121. return -ENOMEM;
  122. return 0;
  123. }
  124. /**
  125. * i40e_free_dma_mem_d - OS specific memory free for shared code
  126. * @hw: pointer to the HW structure
  127. * @mem: ptr to mem struct to free
  128. **/
  129. int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
  130. {
  131. struct i40e_pf *pf = (struct i40e_pf *)hw->back;
  132. dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
  133. mem->va = NULL;
  134. mem->pa = 0;
  135. mem->size = 0;
  136. return 0;
  137. }
  138. /**
  139. * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
  140. * @hw: pointer to the HW structure
  141. * @mem: ptr to mem struct to fill out
  142. * @size: size of memory requested
  143. **/
  144. int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
  145. u32 size)
  146. {
  147. mem->size = size;
  148. mem->va = kzalloc(size, GFP_KERNEL);
  149. if (!mem->va)
  150. return -ENOMEM;
  151. return 0;
  152. }
  153. /**
  154. * i40e_free_virt_mem_d - OS specific memory free for shared code
  155. * @hw: pointer to the HW structure
  156. * @mem: ptr to mem struct to free
  157. **/
  158. int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
  159. {
  160. /* it's ok to kfree a NULL pointer */
  161. kfree(mem->va);
  162. mem->va = NULL;
  163. mem->size = 0;
  164. return 0;
  165. }
  166. /**
  167. * i40e_get_lump - find a lump of free generic resource
  168. * @pf: board private structure
  169. * @pile: the pile of resource to search
  170. * @needed: the number of items needed
  171. * @id: an owner id to stick on the items assigned
  172. *
  173. * Returns the base item index of the lump, or negative for error
  174. *
  175. * The search_hint trick and lack of advanced fit-finding only work
  176. * because we're highly likely to have all the same size lump requests.
  177. * Linear search time and any fragmentation should be minimal.
  178. **/
  179. static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
  180. u16 needed, u16 id)
  181. {
  182. int ret = -ENOMEM;
  183. int i, j;
  184. if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
  185. dev_info(&pf->pdev->dev,
  186. "param err: pile=%p needed=%d id=0x%04x\n",
  187. pile, needed, id);
  188. return -EINVAL;
  189. }
  190. /* start the linear search with an imperfect hint */
  191. i = pile->search_hint;
  192. while (i < pile->num_entries) {
  193. /* skip already allocated entries */
  194. if (pile->list[i] & I40E_PILE_VALID_BIT) {
  195. i++;
  196. continue;
  197. }
  198. /* do we have enough in this lump? */
  199. for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
  200. if (pile->list[i+j] & I40E_PILE_VALID_BIT)
  201. break;
  202. }
  203. if (j == needed) {
  204. /* there was enough, so assign it to the requestor */
  205. for (j = 0; j < needed; j++)
  206. pile->list[i+j] = id | I40E_PILE_VALID_BIT;
  207. ret = i;
  208. pile->search_hint = i + j;
  209. break;
  210. }
  211. /* not enough, so skip over it and continue looking */
  212. i += j;
  213. }
  214. return ret;
  215. }
  216. /**
  217. * i40e_put_lump - return a lump of generic resource
  218. * @pile: the pile of resource to search
  219. * @index: the base item index
  220. * @id: the owner id of the items assigned
  221. *
  222. * Returns the count of items in the lump
  223. **/
  224. static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
  225. {
  226. int valid_id = (id | I40E_PILE_VALID_BIT);
  227. int count = 0;
  228. int i;
  229. if (!pile || index >= pile->num_entries)
  230. return -EINVAL;
  231. for (i = index;
  232. i < pile->num_entries && pile->list[i] == valid_id;
  233. i++) {
  234. pile->list[i] = 0;
  235. count++;
  236. }
  237. if (count && index < pile->search_hint)
  238. pile->search_hint = index;
  239. return count;
  240. }
  241. /**
  242. * i40e_find_vsi_from_id - searches for the vsi with the given id
  243. * @pf - the pf structure to search for the vsi
  244. * @id - id of the vsi it is searching for
  245. **/
  246. struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
  247. {
  248. int i;
  249. for (i = 0; i < pf->num_alloc_vsi; i++)
  250. if (pf->vsi[i] && (pf->vsi[i]->id == id))
  251. return pf->vsi[i];
  252. return NULL;
  253. }
  254. /**
  255. * i40e_service_event_schedule - Schedule the service task to wake up
  256. * @pf: board private structure
  257. *
  258. * If not already scheduled, this puts the task into the work queue
  259. **/
  260. void i40e_service_event_schedule(struct i40e_pf *pf)
  261. {
  262. if (!test_bit(__I40E_DOWN, pf->state) &&
  263. !test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
  264. queue_work(i40e_wq, &pf->service_task);
  265. }
  266. /**
  267. * i40e_tx_timeout - Respond to a Tx Hang
  268. * @netdev: network interface device structure
  269. *
  270. * If any port has noticed a Tx timeout, it is likely that the whole
  271. * device is munged, not just the one netdev port, so go for the full
  272. * reset.
  273. **/
  274. static void i40e_tx_timeout(struct net_device *netdev)
  275. {
  276. struct i40e_netdev_priv *np = netdev_priv(netdev);
  277. struct i40e_vsi *vsi = np->vsi;
  278. struct i40e_pf *pf = vsi->back;
  279. struct i40e_ring *tx_ring = NULL;
  280. unsigned int i, hung_queue = 0;
  281. u32 head, val;
  282. pf->tx_timeout_count++;
  283. /* find the stopped queue the same way the stack does */
  284. for (i = 0; i < netdev->num_tx_queues; i++) {
  285. struct netdev_queue *q;
  286. unsigned long trans_start;
  287. q = netdev_get_tx_queue(netdev, i);
  288. trans_start = q->trans_start;
  289. if (netif_xmit_stopped(q) &&
  290. time_after(jiffies,
  291. (trans_start + netdev->watchdog_timeo))) {
  292. hung_queue = i;
  293. break;
  294. }
  295. }
  296. if (i == netdev->num_tx_queues) {
  297. netdev_info(netdev, "tx_timeout: no netdev hung queue found\n");
  298. } else {
  299. /* now that we have an index, find the tx_ring struct */
  300. for (i = 0; i < vsi->num_queue_pairs; i++) {
  301. if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
  302. if (hung_queue ==
  303. vsi->tx_rings[i]->queue_index) {
  304. tx_ring = vsi->tx_rings[i];
  305. break;
  306. }
  307. }
  308. }
  309. }
  310. if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
  311. pf->tx_timeout_recovery_level = 1; /* reset after some time */
  312. else if (time_before(jiffies,
  313. (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
  314. return; /* don't do any new action before the next timeout */
  315. if (tx_ring) {
  316. head = i40e_get_head(tx_ring);
  317. /* Read interrupt register */
  318. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  319. val = rd32(&pf->hw,
  320. I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
  321. tx_ring->vsi->base_vector - 1));
  322. else
  323. val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
  324. 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",
  325. vsi->seid, hung_queue, tx_ring->next_to_clean,
  326. head, tx_ring->next_to_use,
  327. readl(tx_ring->tail), val);
  328. }
  329. pf->tx_timeout_last_recovery = jiffies;
  330. netdev_info(netdev, "tx_timeout recovery level %d, hung_queue %d\n",
  331. pf->tx_timeout_recovery_level, hung_queue);
  332. switch (pf->tx_timeout_recovery_level) {
  333. case 1:
  334. set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
  335. break;
  336. case 2:
  337. set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
  338. break;
  339. case 3:
  340. set_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
  341. break;
  342. default:
  343. netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
  344. break;
  345. }
  346. i40e_service_event_schedule(pf);
  347. pf->tx_timeout_recovery_level++;
  348. }
  349. /**
  350. * i40e_get_vsi_stats_struct - Get System Network Statistics
  351. * @vsi: the VSI we care about
  352. *
  353. * Returns the address of the device statistics structure.
  354. * The statistics are actually updated from the service task.
  355. **/
  356. struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
  357. {
  358. return &vsi->net_stats;
  359. }
  360. /**
  361. * i40e_get_netdev_stats_struct_tx - populate stats from a Tx ring
  362. * @ring: Tx ring to get statistics from
  363. * @stats: statistics entry to be updated
  364. **/
  365. static void i40e_get_netdev_stats_struct_tx(struct i40e_ring *ring,
  366. struct rtnl_link_stats64 *stats)
  367. {
  368. u64 bytes, packets;
  369. unsigned int start;
  370. do {
  371. start = u64_stats_fetch_begin_irq(&ring->syncp);
  372. packets = ring->stats.packets;
  373. bytes = ring->stats.bytes;
  374. } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
  375. stats->tx_packets += packets;
  376. stats->tx_bytes += bytes;
  377. }
  378. /**
  379. * i40e_get_netdev_stats_struct - Get statistics for netdev interface
  380. * @netdev: network interface device structure
  381. *
  382. * Returns the address of the device statistics structure.
  383. * The statistics are actually updated from the service task.
  384. **/
  385. static void i40e_get_netdev_stats_struct(struct net_device *netdev,
  386. struct rtnl_link_stats64 *stats)
  387. {
  388. struct i40e_netdev_priv *np = netdev_priv(netdev);
  389. struct i40e_ring *tx_ring, *rx_ring;
  390. struct i40e_vsi *vsi = np->vsi;
  391. struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
  392. int i;
  393. if (test_bit(__I40E_VSI_DOWN, vsi->state))
  394. return;
  395. if (!vsi->tx_rings)
  396. return;
  397. rcu_read_lock();
  398. for (i = 0; i < vsi->num_queue_pairs; i++) {
  399. u64 bytes, packets;
  400. unsigned int start;
  401. tx_ring = ACCESS_ONCE(vsi->tx_rings[i]);
  402. if (!tx_ring)
  403. continue;
  404. i40e_get_netdev_stats_struct_tx(tx_ring, stats);
  405. rx_ring = &tx_ring[1];
  406. do {
  407. start = u64_stats_fetch_begin_irq(&rx_ring->syncp);
  408. packets = rx_ring->stats.packets;
  409. bytes = rx_ring->stats.bytes;
  410. } while (u64_stats_fetch_retry_irq(&rx_ring->syncp, start));
  411. stats->rx_packets += packets;
  412. stats->rx_bytes += bytes;
  413. if (i40e_enabled_xdp_vsi(vsi))
  414. i40e_get_netdev_stats_struct_tx(&rx_ring[1], stats);
  415. }
  416. rcu_read_unlock();
  417. /* following stats updated by i40e_watchdog_subtask() */
  418. stats->multicast = vsi_stats->multicast;
  419. stats->tx_errors = vsi_stats->tx_errors;
  420. stats->tx_dropped = vsi_stats->tx_dropped;
  421. stats->rx_errors = vsi_stats->rx_errors;
  422. stats->rx_dropped = vsi_stats->rx_dropped;
  423. stats->rx_crc_errors = vsi_stats->rx_crc_errors;
  424. stats->rx_length_errors = vsi_stats->rx_length_errors;
  425. }
  426. /**
  427. * i40e_vsi_reset_stats - Resets all stats of the given vsi
  428. * @vsi: the VSI to have its stats reset
  429. **/
  430. void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
  431. {
  432. struct rtnl_link_stats64 *ns;
  433. int i;
  434. if (!vsi)
  435. return;
  436. ns = i40e_get_vsi_stats_struct(vsi);
  437. memset(ns, 0, sizeof(*ns));
  438. memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
  439. memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
  440. memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
  441. if (vsi->rx_rings && vsi->rx_rings[0]) {
  442. for (i = 0; i < vsi->num_queue_pairs; i++) {
  443. memset(&vsi->rx_rings[i]->stats, 0,
  444. sizeof(vsi->rx_rings[i]->stats));
  445. memset(&vsi->rx_rings[i]->rx_stats, 0,
  446. sizeof(vsi->rx_rings[i]->rx_stats));
  447. memset(&vsi->tx_rings[i]->stats, 0,
  448. sizeof(vsi->tx_rings[i]->stats));
  449. memset(&vsi->tx_rings[i]->tx_stats, 0,
  450. sizeof(vsi->tx_rings[i]->tx_stats));
  451. }
  452. }
  453. vsi->stat_offsets_loaded = false;
  454. }
  455. /**
  456. * i40e_pf_reset_stats - Reset all of the stats for the given PF
  457. * @pf: the PF to be reset
  458. **/
  459. void i40e_pf_reset_stats(struct i40e_pf *pf)
  460. {
  461. int i;
  462. memset(&pf->stats, 0, sizeof(pf->stats));
  463. memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
  464. pf->stat_offsets_loaded = false;
  465. for (i = 0; i < I40E_MAX_VEB; i++) {
  466. if (pf->veb[i]) {
  467. memset(&pf->veb[i]->stats, 0,
  468. sizeof(pf->veb[i]->stats));
  469. memset(&pf->veb[i]->stats_offsets, 0,
  470. sizeof(pf->veb[i]->stats_offsets));
  471. pf->veb[i]->stat_offsets_loaded = false;
  472. }
  473. }
  474. pf->hw_csum_rx_error = 0;
  475. }
  476. /**
  477. * i40e_stat_update48 - read and update a 48 bit stat from the chip
  478. * @hw: ptr to the hardware info
  479. * @hireg: the high 32 bit reg to read
  480. * @loreg: the low 32 bit reg to read
  481. * @offset_loaded: has the initial offset been loaded yet
  482. * @offset: ptr to current offset value
  483. * @stat: ptr to the stat
  484. *
  485. * Since the device stats are not reset at PFReset, they likely will not
  486. * be zeroed when the driver starts. We'll save the first values read
  487. * and use them as offsets to be subtracted from the raw values in order
  488. * to report stats that count from zero. In the process, we also manage
  489. * the potential roll-over.
  490. **/
  491. static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
  492. bool offset_loaded, u64 *offset, u64 *stat)
  493. {
  494. u64 new_data;
  495. if (hw->device_id == I40E_DEV_ID_QEMU) {
  496. new_data = rd32(hw, loreg);
  497. new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
  498. } else {
  499. new_data = rd64(hw, loreg);
  500. }
  501. if (!offset_loaded)
  502. *offset = new_data;
  503. if (likely(new_data >= *offset))
  504. *stat = new_data - *offset;
  505. else
  506. *stat = (new_data + BIT_ULL(48)) - *offset;
  507. *stat &= 0xFFFFFFFFFFFFULL;
  508. }
  509. /**
  510. * i40e_stat_update32 - read and update a 32 bit stat from the chip
  511. * @hw: ptr to the hardware info
  512. * @reg: the hw reg to read
  513. * @offset_loaded: has the initial offset been loaded yet
  514. * @offset: ptr to current offset value
  515. * @stat: ptr to the stat
  516. **/
  517. static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
  518. bool offset_loaded, u64 *offset, u64 *stat)
  519. {
  520. u32 new_data;
  521. new_data = rd32(hw, reg);
  522. if (!offset_loaded)
  523. *offset = new_data;
  524. if (likely(new_data >= *offset))
  525. *stat = (u32)(new_data - *offset);
  526. else
  527. *stat = (u32)((new_data + BIT_ULL(32)) - *offset);
  528. }
  529. /**
  530. * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
  531. * @vsi: the VSI to be updated
  532. **/
  533. void i40e_update_eth_stats(struct i40e_vsi *vsi)
  534. {
  535. int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
  536. struct i40e_pf *pf = vsi->back;
  537. struct i40e_hw *hw = &pf->hw;
  538. struct i40e_eth_stats *oes;
  539. struct i40e_eth_stats *es; /* device's eth stats */
  540. es = &vsi->eth_stats;
  541. oes = &vsi->eth_stats_offsets;
  542. /* Gather up the stats that the hw collects */
  543. i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
  544. vsi->stat_offsets_loaded,
  545. &oes->tx_errors, &es->tx_errors);
  546. i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
  547. vsi->stat_offsets_loaded,
  548. &oes->rx_discards, &es->rx_discards);
  549. i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
  550. vsi->stat_offsets_loaded,
  551. &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
  552. i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
  553. vsi->stat_offsets_loaded,
  554. &oes->tx_errors, &es->tx_errors);
  555. i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
  556. I40E_GLV_GORCL(stat_idx),
  557. vsi->stat_offsets_loaded,
  558. &oes->rx_bytes, &es->rx_bytes);
  559. i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
  560. I40E_GLV_UPRCL(stat_idx),
  561. vsi->stat_offsets_loaded,
  562. &oes->rx_unicast, &es->rx_unicast);
  563. i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
  564. I40E_GLV_MPRCL(stat_idx),
  565. vsi->stat_offsets_loaded,
  566. &oes->rx_multicast, &es->rx_multicast);
  567. i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
  568. I40E_GLV_BPRCL(stat_idx),
  569. vsi->stat_offsets_loaded,
  570. &oes->rx_broadcast, &es->rx_broadcast);
  571. i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
  572. I40E_GLV_GOTCL(stat_idx),
  573. vsi->stat_offsets_loaded,
  574. &oes->tx_bytes, &es->tx_bytes);
  575. i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
  576. I40E_GLV_UPTCL(stat_idx),
  577. vsi->stat_offsets_loaded,
  578. &oes->tx_unicast, &es->tx_unicast);
  579. i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
  580. I40E_GLV_MPTCL(stat_idx),
  581. vsi->stat_offsets_loaded,
  582. &oes->tx_multicast, &es->tx_multicast);
  583. i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
  584. I40E_GLV_BPTCL(stat_idx),
  585. vsi->stat_offsets_loaded,
  586. &oes->tx_broadcast, &es->tx_broadcast);
  587. vsi->stat_offsets_loaded = true;
  588. }
  589. /**
  590. * i40e_update_veb_stats - Update Switch component statistics
  591. * @veb: the VEB being updated
  592. **/
  593. static void i40e_update_veb_stats(struct i40e_veb *veb)
  594. {
  595. struct i40e_pf *pf = veb->pf;
  596. struct i40e_hw *hw = &pf->hw;
  597. struct i40e_eth_stats *oes;
  598. struct i40e_eth_stats *es; /* device's eth stats */
  599. struct i40e_veb_tc_stats *veb_oes;
  600. struct i40e_veb_tc_stats *veb_es;
  601. int i, idx = 0;
  602. idx = veb->stats_idx;
  603. es = &veb->stats;
  604. oes = &veb->stats_offsets;
  605. veb_es = &veb->tc_stats;
  606. veb_oes = &veb->tc_stats_offsets;
  607. /* Gather up the stats that the hw collects */
  608. i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
  609. veb->stat_offsets_loaded,
  610. &oes->tx_discards, &es->tx_discards);
  611. if (hw->revision_id > 0)
  612. i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
  613. veb->stat_offsets_loaded,
  614. &oes->rx_unknown_protocol,
  615. &es->rx_unknown_protocol);
  616. i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
  617. veb->stat_offsets_loaded,
  618. &oes->rx_bytes, &es->rx_bytes);
  619. i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
  620. veb->stat_offsets_loaded,
  621. &oes->rx_unicast, &es->rx_unicast);
  622. i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
  623. veb->stat_offsets_loaded,
  624. &oes->rx_multicast, &es->rx_multicast);
  625. i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
  626. veb->stat_offsets_loaded,
  627. &oes->rx_broadcast, &es->rx_broadcast);
  628. i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
  629. veb->stat_offsets_loaded,
  630. &oes->tx_bytes, &es->tx_bytes);
  631. i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
  632. veb->stat_offsets_loaded,
  633. &oes->tx_unicast, &es->tx_unicast);
  634. i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
  635. veb->stat_offsets_loaded,
  636. &oes->tx_multicast, &es->tx_multicast);
  637. i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
  638. veb->stat_offsets_loaded,
  639. &oes->tx_broadcast, &es->tx_broadcast);
  640. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  641. i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
  642. I40E_GLVEBTC_RPCL(i, idx),
  643. veb->stat_offsets_loaded,
  644. &veb_oes->tc_rx_packets[i],
  645. &veb_es->tc_rx_packets[i]);
  646. i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
  647. I40E_GLVEBTC_RBCL(i, idx),
  648. veb->stat_offsets_loaded,
  649. &veb_oes->tc_rx_bytes[i],
  650. &veb_es->tc_rx_bytes[i]);
  651. i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
  652. I40E_GLVEBTC_TPCL(i, idx),
  653. veb->stat_offsets_loaded,
  654. &veb_oes->tc_tx_packets[i],
  655. &veb_es->tc_tx_packets[i]);
  656. i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
  657. I40E_GLVEBTC_TBCL(i, idx),
  658. veb->stat_offsets_loaded,
  659. &veb_oes->tc_tx_bytes[i],
  660. &veb_es->tc_tx_bytes[i]);
  661. }
  662. veb->stat_offsets_loaded = true;
  663. }
  664. /**
  665. * i40e_update_vsi_stats - Update the vsi statistics counters.
  666. * @vsi: the VSI to be updated
  667. *
  668. * There are a few instances where we store the same stat in a
  669. * couple of different structs. This is partly because we have
  670. * the netdev stats that need to be filled out, which is slightly
  671. * different from the "eth_stats" defined by the chip and used in
  672. * VF communications. We sort it out here.
  673. **/
  674. static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
  675. {
  676. struct i40e_pf *pf = vsi->back;
  677. struct rtnl_link_stats64 *ons;
  678. struct rtnl_link_stats64 *ns; /* netdev stats */
  679. struct i40e_eth_stats *oes;
  680. struct i40e_eth_stats *es; /* device's eth stats */
  681. u32 tx_restart, tx_busy;
  682. struct i40e_ring *p;
  683. u32 rx_page, rx_buf;
  684. u64 bytes, packets;
  685. unsigned int start;
  686. u64 tx_linearize;
  687. u64 tx_force_wb;
  688. u64 rx_p, rx_b;
  689. u64 tx_p, tx_b;
  690. u16 q;
  691. if (test_bit(__I40E_VSI_DOWN, vsi->state) ||
  692. test_bit(__I40E_CONFIG_BUSY, pf->state))
  693. return;
  694. ns = i40e_get_vsi_stats_struct(vsi);
  695. ons = &vsi->net_stats_offsets;
  696. es = &vsi->eth_stats;
  697. oes = &vsi->eth_stats_offsets;
  698. /* Gather up the netdev and vsi stats that the driver collects
  699. * on the fly during packet processing
  700. */
  701. rx_b = rx_p = 0;
  702. tx_b = tx_p = 0;
  703. tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
  704. rx_page = 0;
  705. rx_buf = 0;
  706. rcu_read_lock();
  707. for (q = 0; q < vsi->num_queue_pairs; q++) {
  708. /* locate Tx ring */
  709. p = ACCESS_ONCE(vsi->tx_rings[q]);
  710. do {
  711. start = u64_stats_fetch_begin_irq(&p->syncp);
  712. packets = p->stats.packets;
  713. bytes = p->stats.bytes;
  714. } while (u64_stats_fetch_retry_irq(&p->syncp, start));
  715. tx_b += bytes;
  716. tx_p += packets;
  717. tx_restart += p->tx_stats.restart_queue;
  718. tx_busy += p->tx_stats.tx_busy;
  719. tx_linearize += p->tx_stats.tx_linearize;
  720. tx_force_wb += p->tx_stats.tx_force_wb;
  721. /* Rx queue is part of the same block as Tx queue */
  722. p = &p[1];
  723. do {
  724. start = u64_stats_fetch_begin_irq(&p->syncp);
  725. packets = p->stats.packets;
  726. bytes = p->stats.bytes;
  727. } while (u64_stats_fetch_retry_irq(&p->syncp, start));
  728. rx_b += bytes;
  729. rx_p += packets;
  730. rx_buf += p->rx_stats.alloc_buff_failed;
  731. rx_page += p->rx_stats.alloc_page_failed;
  732. }
  733. rcu_read_unlock();
  734. vsi->tx_restart = tx_restart;
  735. vsi->tx_busy = tx_busy;
  736. vsi->tx_linearize = tx_linearize;
  737. vsi->tx_force_wb = tx_force_wb;
  738. vsi->rx_page_failed = rx_page;
  739. vsi->rx_buf_failed = rx_buf;
  740. ns->rx_packets = rx_p;
  741. ns->rx_bytes = rx_b;
  742. ns->tx_packets = tx_p;
  743. ns->tx_bytes = tx_b;
  744. /* update netdev stats from eth stats */
  745. i40e_update_eth_stats(vsi);
  746. ons->tx_errors = oes->tx_errors;
  747. ns->tx_errors = es->tx_errors;
  748. ons->multicast = oes->rx_multicast;
  749. ns->multicast = es->rx_multicast;
  750. ons->rx_dropped = oes->rx_discards;
  751. ns->rx_dropped = es->rx_discards;
  752. ons->tx_dropped = oes->tx_discards;
  753. ns->tx_dropped = es->tx_discards;
  754. /* pull in a couple PF stats if this is the main vsi */
  755. if (vsi == pf->vsi[pf->lan_vsi]) {
  756. ns->rx_crc_errors = pf->stats.crc_errors;
  757. ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
  758. ns->rx_length_errors = pf->stats.rx_length_errors;
  759. }
  760. }
  761. /**
  762. * i40e_update_pf_stats - Update the PF statistics counters.
  763. * @pf: the PF to be updated
  764. **/
  765. static void i40e_update_pf_stats(struct i40e_pf *pf)
  766. {
  767. struct i40e_hw_port_stats *osd = &pf->stats_offsets;
  768. struct i40e_hw_port_stats *nsd = &pf->stats;
  769. struct i40e_hw *hw = &pf->hw;
  770. u32 val;
  771. int i;
  772. i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
  773. I40E_GLPRT_GORCL(hw->port),
  774. pf->stat_offsets_loaded,
  775. &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
  776. i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
  777. I40E_GLPRT_GOTCL(hw->port),
  778. pf->stat_offsets_loaded,
  779. &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
  780. i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
  781. pf->stat_offsets_loaded,
  782. &osd->eth.rx_discards,
  783. &nsd->eth.rx_discards);
  784. i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
  785. I40E_GLPRT_UPRCL(hw->port),
  786. pf->stat_offsets_loaded,
  787. &osd->eth.rx_unicast,
  788. &nsd->eth.rx_unicast);
  789. i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
  790. I40E_GLPRT_MPRCL(hw->port),
  791. pf->stat_offsets_loaded,
  792. &osd->eth.rx_multicast,
  793. &nsd->eth.rx_multicast);
  794. i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
  795. I40E_GLPRT_BPRCL(hw->port),
  796. pf->stat_offsets_loaded,
  797. &osd->eth.rx_broadcast,
  798. &nsd->eth.rx_broadcast);
  799. i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
  800. I40E_GLPRT_UPTCL(hw->port),
  801. pf->stat_offsets_loaded,
  802. &osd->eth.tx_unicast,
  803. &nsd->eth.tx_unicast);
  804. i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
  805. I40E_GLPRT_MPTCL(hw->port),
  806. pf->stat_offsets_loaded,
  807. &osd->eth.tx_multicast,
  808. &nsd->eth.tx_multicast);
  809. i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
  810. I40E_GLPRT_BPTCL(hw->port),
  811. pf->stat_offsets_loaded,
  812. &osd->eth.tx_broadcast,
  813. &nsd->eth.tx_broadcast);
  814. i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
  815. pf->stat_offsets_loaded,
  816. &osd->tx_dropped_link_down,
  817. &nsd->tx_dropped_link_down);
  818. i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
  819. pf->stat_offsets_loaded,
  820. &osd->crc_errors, &nsd->crc_errors);
  821. i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
  822. pf->stat_offsets_loaded,
  823. &osd->illegal_bytes, &nsd->illegal_bytes);
  824. i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
  825. pf->stat_offsets_loaded,
  826. &osd->mac_local_faults,
  827. &nsd->mac_local_faults);
  828. i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
  829. pf->stat_offsets_loaded,
  830. &osd->mac_remote_faults,
  831. &nsd->mac_remote_faults);
  832. i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
  833. pf->stat_offsets_loaded,
  834. &osd->rx_length_errors,
  835. &nsd->rx_length_errors);
  836. i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
  837. pf->stat_offsets_loaded,
  838. &osd->link_xon_rx, &nsd->link_xon_rx);
  839. i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
  840. pf->stat_offsets_loaded,
  841. &osd->link_xon_tx, &nsd->link_xon_tx);
  842. i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
  843. pf->stat_offsets_loaded,
  844. &osd->link_xoff_rx, &nsd->link_xoff_rx);
  845. i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
  846. pf->stat_offsets_loaded,
  847. &osd->link_xoff_tx, &nsd->link_xoff_tx);
  848. for (i = 0; i < 8; i++) {
  849. i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
  850. pf->stat_offsets_loaded,
  851. &osd->priority_xoff_rx[i],
  852. &nsd->priority_xoff_rx[i]);
  853. i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
  854. pf->stat_offsets_loaded,
  855. &osd->priority_xon_rx[i],
  856. &nsd->priority_xon_rx[i]);
  857. i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
  858. pf->stat_offsets_loaded,
  859. &osd->priority_xon_tx[i],
  860. &nsd->priority_xon_tx[i]);
  861. i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
  862. pf->stat_offsets_loaded,
  863. &osd->priority_xoff_tx[i],
  864. &nsd->priority_xoff_tx[i]);
  865. i40e_stat_update32(hw,
  866. I40E_GLPRT_RXON2OFFCNT(hw->port, i),
  867. pf->stat_offsets_loaded,
  868. &osd->priority_xon_2_xoff[i],
  869. &nsd->priority_xon_2_xoff[i]);
  870. }
  871. i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
  872. I40E_GLPRT_PRC64L(hw->port),
  873. pf->stat_offsets_loaded,
  874. &osd->rx_size_64, &nsd->rx_size_64);
  875. i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
  876. I40E_GLPRT_PRC127L(hw->port),
  877. pf->stat_offsets_loaded,
  878. &osd->rx_size_127, &nsd->rx_size_127);
  879. i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
  880. I40E_GLPRT_PRC255L(hw->port),
  881. pf->stat_offsets_loaded,
  882. &osd->rx_size_255, &nsd->rx_size_255);
  883. i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
  884. I40E_GLPRT_PRC511L(hw->port),
  885. pf->stat_offsets_loaded,
  886. &osd->rx_size_511, &nsd->rx_size_511);
  887. i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
  888. I40E_GLPRT_PRC1023L(hw->port),
  889. pf->stat_offsets_loaded,
  890. &osd->rx_size_1023, &nsd->rx_size_1023);
  891. i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
  892. I40E_GLPRT_PRC1522L(hw->port),
  893. pf->stat_offsets_loaded,
  894. &osd->rx_size_1522, &nsd->rx_size_1522);
  895. i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
  896. I40E_GLPRT_PRC9522L(hw->port),
  897. pf->stat_offsets_loaded,
  898. &osd->rx_size_big, &nsd->rx_size_big);
  899. i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
  900. I40E_GLPRT_PTC64L(hw->port),
  901. pf->stat_offsets_loaded,
  902. &osd->tx_size_64, &nsd->tx_size_64);
  903. i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
  904. I40E_GLPRT_PTC127L(hw->port),
  905. pf->stat_offsets_loaded,
  906. &osd->tx_size_127, &nsd->tx_size_127);
  907. i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
  908. I40E_GLPRT_PTC255L(hw->port),
  909. pf->stat_offsets_loaded,
  910. &osd->tx_size_255, &nsd->tx_size_255);
  911. i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
  912. I40E_GLPRT_PTC511L(hw->port),
  913. pf->stat_offsets_loaded,
  914. &osd->tx_size_511, &nsd->tx_size_511);
  915. i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
  916. I40E_GLPRT_PTC1023L(hw->port),
  917. pf->stat_offsets_loaded,
  918. &osd->tx_size_1023, &nsd->tx_size_1023);
  919. i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
  920. I40E_GLPRT_PTC1522L(hw->port),
  921. pf->stat_offsets_loaded,
  922. &osd->tx_size_1522, &nsd->tx_size_1522);
  923. i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
  924. I40E_GLPRT_PTC9522L(hw->port),
  925. pf->stat_offsets_loaded,
  926. &osd->tx_size_big, &nsd->tx_size_big);
  927. i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
  928. pf->stat_offsets_loaded,
  929. &osd->rx_undersize, &nsd->rx_undersize);
  930. i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
  931. pf->stat_offsets_loaded,
  932. &osd->rx_fragments, &nsd->rx_fragments);
  933. i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
  934. pf->stat_offsets_loaded,
  935. &osd->rx_oversize, &nsd->rx_oversize);
  936. i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
  937. pf->stat_offsets_loaded,
  938. &osd->rx_jabber, &nsd->rx_jabber);
  939. /* FDIR stats */
  940. i40e_stat_update32(hw,
  941. I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(pf->hw.pf_id)),
  942. pf->stat_offsets_loaded,
  943. &osd->fd_atr_match, &nsd->fd_atr_match);
  944. i40e_stat_update32(hw,
  945. I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(pf->hw.pf_id)),
  946. pf->stat_offsets_loaded,
  947. &osd->fd_sb_match, &nsd->fd_sb_match);
  948. i40e_stat_update32(hw,
  949. I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(pf->hw.pf_id)),
  950. pf->stat_offsets_loaded,
  951. &osd->fd_atr_tunnel_match, &nsd->fd_atr_tunnel_match);
  952. val = rd32(hw, I40E_PRTPM_EEE_STAT);
  953. nsd->tx_lpi_status =
  954. (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
  955. I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
  956. nsd->rx_lpi_status =
  957. (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
  958. I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
  959. i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
  960. pf->stat_offsets_loaded,
  961. &osd->tx_lpi_count, &nsd->tx_lpi_count);
  962. i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
  963. pf->stat_offsets_loaded,
  964. &osd->rx_lpi_count, &nsd->rx_lpi_count);
  965. if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
  966. !(pf->flags & I40E_FLAG_FD_SB_AUTO_DISABLED))
  967. nsd->fd_sb_status = true;
  968. else
  969. nsd->fd_sb_status = false;
  970. if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
  971. !(pf->flags & I40E_FLAG_FD_ATR_AUTO_DISABLED))
  972. nsd->fd_atr_status = true;
  973. else
  974. nsd->fd_atr_status = false;
  975. pf->stat_offsets_loaded = true;
  976. }
  977. /**
  978. * i40e_update_stats - Update the various statistics counters.
  979. * @vsi: the VSI to be updated
  980. *
  981. * Update the various stats for this VSI and its related entities.
  982. **/
  983. void i40e_update_stats(struct i40e_vsi *vsi)
  984. {
  985. struct i40e_pf *pf = vsi->back;
  986. if (vsi == pf->vsi[pf->lan_vsi])
  987. i40e_update_pf_stats(pf);
  988. i40e_update_vsi_stats(vsi);
  989. }
  990. /**
  991. * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
  992. * @vsi: the VSI to be searched
  993. * @macaddr: the MAC address
  994. * @vlan: the vlan
  995. *
  996. * Returns ptr to the filter object or NULL
  997. **/
  998. static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
  999. const u8 *macaddr, s16 vlan)
  1000. {
  1001. struct i40e_mac_filter *f;
  1002. u64 key;
  1003. if (!vsi || !macaddr)
  1004. return NULL;
  1005. key = i40e_addr_to_hkey(macaddr);
  1006. hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
  1007. if ((ether_addr_equal(macaddr, f->macaddr)) &&
  1008. (vlan == f->vlan))
  1009. return f;
  1010. }
  1011. return NULL;
  1012. }
  1013. /**
  1014. * i40e_find_mac - Find a mac addr in the macvlan filters list
  1015. * @vsi: the VSI to be searched
  1016. * @macaddr: the MAC address we are searching for
  1017. *
  1018. * Returns the first filter with the provided MAC address or NULL if
  1019. * MAC address was not found
  1020. **/
  1021. struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
  1022. {
  1023. struct i40e_mac_filter *f;
  1024. u64 key;
  1025. if (!vsi || !macaddr)
  1026. return NULL;
  1027. key = i40e_addr_to_hkey(macaddr);
  1028. hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
  1029. if ((ether_addr_equal(macaddr, f->macaddr)))
  1030. return f;
  1031. }
  1032. return NULL;
  1033. }
  1034. /**
  1035. * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
  1036. * @vsi: the VSI to be searched
  1037. *
  1038. * Returns true if VSI is in vlan mode or false otherwise
  1039. **/
  1040. bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
  1041. {
  1042. /* If we have a PVID, always operate in VLAN mode */
  1043. if (vsi->info.pvid)
  1044. return true;
  1045. /* We need to operate in VLAN mode whenever we have any filters with
  1046. * a VLAN other than I40E_VLAN_ALL. We could check the table each
  1047. * time, incurring search cost repeatedly. However, we can notice two
  1048. * things:
  1049. *
  1050. * 1) the only place where we can gain a VLAN filter is in
  1051. * i40e_add_filter.
  1052. *
  1053. * 2) the only place where filters are actually removed is in
  1054. * i40e_sync_filters_subtask.
  1055. *
  1056. * Thus, we can simply use a boolean value, has_vlan_filters which we
  1057. * will set to true when we add a VLAN filter in i40e_add_filter. Then
  1058. * we have to perform the full search after deleting filters in
  1059. * i40e_sync_filters_subtask, but we already have to search
  1060. * filters here and can perform the check at the same time. This
  1061. * results in avoiding embedding a loop for VLAN mode inside another
  1062. * loop over all the filters, and should maintain correctness as noted
  1063. * above.
  1064. */
  1065. return vsi->has_vlan_filter;
  1066. }
  1067. /**
  1068. * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
  1069. * @vsi: the VSI to configure
  1070. * @tmp_add_list: list of filters ready to be added
  1071. * @tmp_del_list: list of filters ready to be deleted
  1072. * @vlan_filters: the number of active VLAN filters
  1073. *
  1074. * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
  1075. * behave as expected. If we have any active VLAN filters remaining or about
  1076. * to be added then we need to update non-VLAN filters to be marked as VLAN=0
  1077. * so that they only match against untagged traffic. If we no longer have any
  1078. * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
  1079. * so that they match against both tagged and untagged traffic. In this way,
  1080. * we ensure that we correctly receive the desired traffic. This ensures that
  1081. * when we have an active VLAN we will receive only untagged traffic and
  1082. * traffic matching active VLANs. If we have no active VLANs then we will
  1083. * operate in non-VLAN mode and receive all traffic, tagged or untagged.
  1084. *
  1085. * Finally, in a similar fashion, this function also corrects filters when
  1086. * there is an active PVID assigned to this VSI.
  1087. *
  1088. * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
  1089. *
  1090. * This function is only expected to be called from within
  1091. * i40e_sync_vsi_filters.
  1092. *
  1093. * NOTE: This function expects to be called while under the
  1094. * mac_filter_hash_lock
  1095. */
  1096. static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
  1097. struct hlist_head *tmp_add_list,
  1098. struct hlist_head *tmp_del_list,
  1099. int vlan_filters)
  1100. {
  1101. s16 pvid = le16_to_cpu(vsi->info.pvid);
  1102. struct i40e_mac_filter *f, *add_head;
  1103. struct i40e_new_mac_filter *new;
  1104. struct hlist_node *h;
  1105. int bkt, new_vlan;
  1106. /* To determine if a particular filter needs to be replaced we
  1107. * have the three following conditions:
  1108. *
  1109. * a) if we have a PVID assigned, then all filters which are
  1110. * not marked as VLAN=PVID must be replaced with filters that
  1111. * are.
  1112. * b) otherwise, if we have any active VLANS, all filters
  1113. * which are marked as VLAN=-1 must be replaced with
  1114. * filters marked as VLAN=0
  1115. * c) finally, if we do not have any active VLANS, all filters
  1116. * which are marked as VLAN=0 must be replaced with filters
  1117. * marked as VLAN=-1
  1118. */
  1119. /* Update the filters about to be added in place */
  1120. hlist_for_each_entry(new, tmp_add_list, hlist) {
  1121. if (pvid && new->f->vlan != pvid)
  1122. new->f->vlan = pvid;
  1123. else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
  1124. new->f->vlan = 0;
  1125. else if (!vlan_filters && new->f->vlan == 0)
  1126. new->f->vlan = I40E_VLAN_ANY;
  1127. }
  1128. /* Update the remaining active filters */
  1129. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  1130. /* Combine the checks for whether a filter needs to be changed
  1131. * and then determine the new VLAN inside the if block, in
  1132. * order to avoid duplicating code for adding the new filter
  1133. * then deleting the old filter.
  1134. */
  1135. if ((pvid && f->vlan != pvid) ||
  1136. (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
  1137. (!vlan_filters && f->vlan == 0)) {
  1138. /* Determine the new vlan we will be adding */
  1139. if (pvid)
  1140. new_vlan = pvid;
  1141. else if (vlan_filters)
  1142. new_vlan = 0;
  1143. else
  1144. new_vlan = I40E_VLAN_ANY;
  1145. /* Create the new filter */
  1146. add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
  1147. if (!add_head)
  1148. return -ENOMEM;
  1149. /* Create a temporary i40e_new_mac_filter */
  1150. new = kzalloc(sizeof(*new), GFP_ATOMIC);
  1151. if (!new)
  1152. return -ENOMEM;
  1153. new->f = add_head;
  1154. new->state = add_head->state;
  1155. /* Add the new filter to the tmp list */
  1156. hlist_add_head(&new->hlist, tmp_add_list);
  1157. /* Put the original filter into the delete list */
  1158. f->state = I40E_FILTER_REMOVE;
  1159. hash_del(&f->hlist);
  1160. hlist_add_head(&f->hlist, tmp_del_list);
  1161. }
  1162. }
  1163. vsi->has_vlan_filter = !!vlan_filters;
  1164. return 0;
  1165. }
  1166. /**
  1167. * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
  1168. * @vsi: the PF Main VSI - inappropriate for any other VSI
  1169. * @macaddr: the MAC address
  1170. *
  1171. * Remove whatever filter the firmware set up so the driver can manage
  1172. * its own filtering intelligently.
  1173. **/
  1174. static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
  1175. {
  1176. struct i40e_aqc_remove_macvlan_element_data element;
  1177. struct i40e_pf *pf = vsi->back;
  1178. /* Only appropriate for the PF main VSI */
  1179. if (vsi->type != I40E_VSI_MAIN)
  1180. return;
  1181. memset(&element, 0, sizeof(element));
  1182. ether_addr_copy(element.mac_addr, macaddr);
  1183. element.vlan_tag = 0;
  1184. /* Ignore error returns, some firmware does it this way... */
  1185. element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
  1186. i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
  1187. memset(&element, 0, sizeof(element));
  1188. ether_addr_copy(element.mac_addr, macaddr);
  1189. element.vlan_tag = 0;
  1190. /* ...and some firmware does it this way. */
  1191. element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
  1192. I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
  1193. i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
  1194. }
  1195. /**
  1196. * i40e_add_filter - Add a mac/vlan filter to the VSI
  1197. * @vsi: the VSI to be searched
  1198. * @macaddr: the MAC address
  1199. * @vlan: the vlan
  1200. *
  1201. * Returns ptr to the filter object or NULL when no memory available.
  1202. *
  1203. * NOTE: This function is expected to be called with mac_filter_hash_lock
  1204. * being held.
  1205. **/
  1206. struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
  1207. const u8 *macaddr, s16 vlan)
  1208. {
  1209. struct i40e_mac_filter *f;
  1210. u64 key;
  1211. if (!vsi || !macaddr)
  1212. return NULL;
  1213. f = i40e_find_filter(vsi, macaddr, vlan);
  1214. if (!f) {
  1215. f = kzalloc(sizeof(*f), GFP_ATOMIC);
  1216. if (!f)
  1217. return NULL;
  1218. /* Update the boolean indicating if we need to function in
  1219. * VLAN mode.
  1220. */
  1221. if (vlan >= 0)
  1222. vsi->has_vlan_filter = true;
  1223. ether_addr_copy(f->macaddr, macaddr);
  1224. f->vlan = vlan;
  1225. /* If we're in overflow promisc mode, set the state directly
  1226. * to failed, so we don't bother to try sending the filter
  1227. * to the hardware.
  1228. */
  1229. if (test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state))
  1230. f->state = I40E_FILTER_FAILED;
  1231. else
  1232. f->state = I40E_FILTER_NEW;
  1233. INIT_HLIST_NODE(&f->hlist);
  1234. key = i40e_addr_to_hkey(macaddr);
  1235. hash_add(vsi->mac_filter_hash, &f->hlist, key);
  1236. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  1237. vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
  1238. }
  1239. /* If we're asked to add a filter that has been marked for removal, it
  1240. * is safe to simply restore it to active state. __i40e_del_filter
  1241. * will have simply deleted any filters which were previously marked
  1242. * NEW or FAILED, so if it is currently marked REMOVE it must have
  1243. * previously been ACTIVE. Since we haven't yet run the sync filters
  1244. * task, just restore this filter to the ACTIVE state so that the
  1245. * sync task leaves it in place
  1246. */
  1247. if (f->state == I40E_FILTER_REMOVE)
  1248. f->state = I40E_FILTER_ACTIVE;
  1249. return f;
  1250. }
  1251. /**
  1252. * __i40e_del_filter - Remove a specific filter from the VSI
  1253. * @vsi: VSI to remove from
  1254. * @f: the filter to remove from the list
  1255. *
  1256. * This function should be called instead of i40e_del_filter only if you know
  1257. * the exact filter you will remove already, such as via i40e_find_filter or
  1258. * i40e_find_mac.
  1259. *
  1260. * NOTE: This function is expected to be called with mac_filter_hash_lock
  1261. * being held.
  1262. * ANOTHER NOTE: This function MUST be called from within the context of
  1263. * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
  1264. * instead of list_for_each_entry().
  1265. **/
  1266. void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
  1267. {
  1268. if (!f)
  1269. return;
  1270. /* If the filter was never added to firmware then we can just delete it
  1271. * directly and we don't want to set the status to remove or else an
  1272. * admin queue command will unnecessarily fire.
  1273. */
  1274. if ((f->state == I40E_FILTER_FAILED) ||
  1275. (f->state == I40E_FILTER_NEW)) {
  1276. hash_del(&f->hlist);
  1277. kfree(f);
  1278. } else {
  1279. f->state = I40E_FILTER_REMOVE;
  1280. }
  1281. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  1282. vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
  1283. }
  1284. /**
  1285. * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
  1286. * @vsi: the VSI to be searched
  1287. * @macaddr: the MAC address
  1288. * @vlan: the VLAN
  1289. *
  1290. * NOTE: This function is expected to be called with mac_filter_hash_lock
  1291. * being held.
  1292. * ANOTHER NOTE: This function MUST be called from within the context of
  1293. * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
  1294. * instead of list_for_each_entry().
  1295. **/
  1296. void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
  1297. {
  1298. struct i40e_mac_filter *f;
  1299. if (!vsi || !macaddr)
  1300. return;
  1301. f = i40e_find_filter(vsi, macaddr, vlan);
  1302. __i40e_del_filter(vsi, f);
  1303. }
  1304. /**
  1305. * i40e_add_mac_filter - Add a MAC filter for all active VLANs
  1306. * @vsi: the VSI to be searched
  1307. * @macaddr: the mac address to be filtered
  1308. *
  1309. * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
  1310. * go through all the macvlan filters and add a macvlan filter for each
  1311. * unique vlan that already exists. If a PVID has been assigned, instead only
  1312. * add the macaddr to that VLAN.
  1313. *
  1314. * Returns last filter added on success, else NULL
  1315. **/
  1316. struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
  1317. const u8 *macaddr)
  1318. {
  1319. struct i40e_mac_filter *f, *add = NULL;
  1320. struct hlist_node *h;
  1321. int bkt;
  1322. if (vsi->info.pvid)
  1323. return i40e_add_filter(vsi, macaddr,
  1324. le16_to_cpu(vsi->info.pvid));
  1325. if (!i40e_is_vsi_in_vlan(vsi))
  1326. return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
  1327. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  1328. if (f->state == I40E_FILTER_REMOVE)
  1329. continue;
  1330. add = i40e_add_filter(vsi, macaddr, f->vlan);
  1331. if (!add)
  1332. return NULL;
  1333. }
  1334. return add;
  1335. }
  1336. /**
  1337. * i40e_del_mac_filter - Remove a MAC filter from all VLANs
  1338. * @vsi: the VSI to be searched
  1339. * @macaddr: the mac address to be removed
  1340. *
  1341. * Removes a given MAC address from a VSI regardless of what VLAN it has been
  1342. * associated with.
  1343. *
  1344. * Returns 0 for success, or error
  1345. **/
  1346. int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
  1347. {
  1348. struct i40e_mac_filter *f;
  1349. struct hlist_node *h;
  1350. bool found = false;
  1351. int bkt;
  1352. WARN(!spin_is_locked(&vsi->mac_filter_hash_lock),
  1353. "Missing mac_filter_hash_lock\n");
  1354. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  1355. if (ether_addr_equal(macaddr, f->macaddr)) {
  1356. __i40e_del_filter(vsi, f);
  1357. found = true;
  1358. }
  1359. }
  1360. if (found)
  1361. return 0;
  1362. else
  1363. return -ENOENT;
  1364. }
  1365. /**
  1366. * i40e_set_mac - NDO callback to set mac address
  1367. * @netdev: network interface device structure
  1368. * @p: pointer to an address structure
  1369. *
  1370. * Returns 0 on success, negative on failure
  1371. **/
  1372. static int i40e_set_mac(struct net_device *netdev, void *p)
  1373. {
  1374. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1375. struct i40e_vsi *vsi = np->vsi;
  1376. struct i40e_pf *pf = vsi->back;
  1377. struct i40e_hw *hw = &pf->hw;
  1378. struct sockaddr *addr = p;
  1379. if (!is_valid_ether_addr(addr->sa_data))
  1380. return -EADDRNOTAVAIL;
  1381. if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
  1382. netdev_info(netdev, "already using mac address %pM\n",
  1383. addr->sa_data);
  1384. return 0;
  1385. }
  1386. if (test_bit(__I40E_VSI_DOWN, vsi->back->state) ||
  1387. test_bit(__I40E_RESET_RECOVERY_PENDING, vsi->back->state))
  1388. return -EADDRNOTAVAIL;
  1389. if (ether_addr_equal(hw->mac.addr, addr->sa_data))
  1390. netdev_info(netdev, "returning to hw mac address %pM\n",
  1391. hw->mac.addr);
  1392. else
  1393. netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
  1394. spin_lock_bh(&vsi->mac_filter_hash_lock);
  1395. i40e_del_mac_filter(vsi, netdev->dev_addr);
  1396. i40e_add_mac_filter(vsi, addr->sa_data);
  1397. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  1398. ether_addr_copy(netdev->dev_addr, addr->sa_data);
  1399. if (vsi->type == I40E_VSI_MAIN) {
  1400. i40e_status ret;
  1401. ret = i40e_aq_mac_address_write(&vsi->back->hw,
  1402. I40E_AQC_WRITE_TYPE_LAA_WOL,
  1403. addr->sa_data, NULL);
  1404. if (ret)
  1405. netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
  1406. i40e_stat_str(hw, ret),
  1407. i40e_aq_str(hw, hw->aq.asq_last_status));
  1408. }
  1409. /* schedule our worker thread which will take care of
  1410. * applying the new filter changes
  1411. */
  1412. i40e_service_event_schedule(vsi->back);
  1413. return 0;
  1414. }
  1415. /**
  1416. * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
  1417. * @vsi: the VSI being setup
  1418. * @ctxt: VSI context structure
  1419. * @enabled_tc: Enabled TCs bitmap
  1420. * @is_add: True if called before Add VSI
  1421. *
  1422. * Setup VSI queue mapping for enabled traffic classes.
  1423. **/
  1424. static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
  1425. struct i40e_vsi_context *ctxt,
  1426. u8 enabled_tc,
  1427. bool is_add)
  1428. {
  1429. struct i40e_pf *pf = vsi->back;
  1430. u16 sections = 0;
  1431. u8 netdev_tc = 0;
  1432. u16 numtc = 0;
  1433. u16 qcount;
  1434. u8 offset;
  1435. u16 qmap;
  1436. int i;
  1437. u16 num_tc_qps = 0;
  1438. sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
  1439. offset = 0;
  1440. if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
  1441. /* Find numtc from enabled TC bitmap */
  1442. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  1443. if (enabled_tc & BIT(i)) /* TC is enabled */
  1444. numtc++;
  1445. }
  1446. if (!numtc) {
  1447. dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
  1448. numtc = 1;
  1449. }
  1450. } else {
  1451. /* At least TC0 is enabled in case of non-DCB case */
  1452. numtc = 1;
  1453. }
  1454. vsi->tc_config.numtc = numtc;
  1455. vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
  1456. /* Number of queues per enabled TC */
  1457. qcount = vsi->alloc_queue_pairs;
  1458. num_tc_qps = qcount / numtc;
  1459. num_tc_qps = min_t(int, num_tc_qps, i40e_pf_get_max_q_per_tc(pf));
  1460. /* Setup queue offset/count for all TCs for given VSI */
  1461. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  1462. /* See if the given TC is enabled for the given VSI */
  1463. if (vsi->tc_config.enabled_tc & BIT(i)) {
  1464. /* TC is enabled */
  1465. int pow, num_qps;
  1466. switch (vsi->type) {
  1467. case I40E_VSI_MAIN:
  1468. qcount = min_t(int, pf->alloc_rss_size,
  1469. num_tc_qps);
  1470. break;
  1471. case I40E_VSI_FDIR:
  1472. case I40E_VSI_SRIOV:
  1473. case I40E_VSI_VMDQ2:
  1474. default:
  1475. qcount = num_tc_qps;
  1476. WARN_ON(i != 0);
  1477. break;
  1478. }
  1479. vsi->tc_config.tc_info[i].qoffset = offset;
  1480. vsi->tc_config.tc_info[i].qcount = qcount;
  1481. /* find the next higher power-of-2 of num queue pairs */
  1482. num_qps = qcount;
  1483. pow = 0;
  1484. while (num_qps && (BIT_ULL(pow) < qcount)) {
  1485. pow++;
  1486. num_qps >>= 1;
  1487. }
  1488. vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
  1489. qmap =
  1490. (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
  1491. (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
  1492. offset += qcount;
  1493. } else {
  1494. /* TC is not enabled so set the offset to
  1495. * default queue and allocate one queue
  1496. * for the given TC.
  1497. */
  1498. vsi->tc_config.tc_info[i].qoffset = 0;
  1499. vsi->tc_config.tc_info[i].qcount = 1;
  1500. vsi->tc_config.tc_info[i].netdev_tc = 0;
  1501. qmap = 0;
  1502. }
  1503. ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
  1504. }
  1505. /* Set actual Tx/Rx queue pairs */
  1506. vsi->num_queue_pairs = offset;
  1507. if ((vsi->type == I40E_VSI_MAIN) && (numtc == 1)) {
  1508. if (vsi->req_queue_pairs > 0)
  1509. vsi->num_queue_pairs = vsi->req_queue_pairs;
  1510. else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  1511. vsi->num_queue_pairs = pf->num_lan_msix;
  1512. }
  1513. /* Scheduler section valid can only be set for ADD VSI */
  1514. if (is_add) {
  1515. sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
  1516. ctxt->info.up_enable_bits = enabled_tc;
  1517. }
  1518. if (vsi->type == I40E_VSI_SRIOV) {
  1519. ctxt->info.mapping_flags |=
  1520. cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
  1521. for (i = 0; i < vsi->num_queue_pairs; i++)
  1522. ctxt->info.queue_mapping[i] =
  1523. cpu_to_le16(vsi->base_queue + i);
  1524. } else {
  1525. ctxt->info.mapping_flags |=
  1526. cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
  1527. ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
  1528. }
  1529. ctxt->info.valid_sections |= cpu_to_le16(sections);
  1530. }
  1531. /**
  1532. * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
  1533. * @netdev: the netdevice
  1534. * @addr: address to add
  1535. *
  1536. * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
  1537. * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
  1538. */
  1539. static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
  1540. {
  1541. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1542. struct i40e_vsi *vsi = np->vsi;
  1543. if (i40e_add_mac_filter(vsi, addr))
  1544. return 0;
  1545. else
  1546. return -ENOMEM;
  1547. }
  1548. /**
  1549. * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
  1550. * @netdev: the netdevice
  1551. * @addr: address to add
  1552. *
  1553. * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
  1554. * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
  1555. */
  1556. static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
  1557. {
  1558. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1559. struct i40e_vsi *vsi = np->vsi;
  1560. i40e_del_mac_filter(vsi, addr);
  1561. return 0;
  1562. }
  1563. /**
  1564. * i40e_set_rx_mode - NDO callback to set the netdev filters
  1565. * @netdev: network interface device structure
  1566. **/
  1567. static void i40e_set_rx_mode(struct net_device *netdev)
  1568. {
  1569. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1570. struct i40e_vsi *vsi = np->vsi;
  1571. spin_lock_bh(&vsi->mac_filter_hash_lock);
  1572. __dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
  1573. __dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
  1574. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  1575. /* check for other flag changes */
  1576. if (vsi->current_netdev_flags != vsi->netdev->flags) {
  1577. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  1578. vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
  1579. }
  1580. /* schedule our worker thread which will take care of
  1581. * applying the new filter changes
  1582. */
  1583. i40e_service_event_schedule(vsi->back);
  1584. }
  1585. /**
  1586. * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
  1587. * @vsi: Pointer to VSI struct
  1588. * @from: Pointer to list which contains MAC filter entries - changes to
  1589. * those entries needs to be undone.
  1590. *
  1591. * MAC filter entries from this list were slated for deletion.
  1592. **/
  1593. static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
  1594. struct hlist_head *from)
  1595. {
  1596. struct i40e_mac_filter *f;
  1597. struct hlist_node *h;
  1598. hlist_for_each_entry_safe(f, h, from, hlist) {
  1599. u64 key = i40e_addr_to_hkey(f->macaddr);
  1600. /* Move the element back into MAC filter list*/
  1601. hlist_del(&f->hlist);
  1602. hash_add(vsi->mac_filter_hash, &f->hlist, key);
  1603. }
  1604. }
  1605. /**
  1606. * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
  1607. * @vsi: Pointer to vsi struct
  1608. * @from: Pointer to list which contains MAC filter entries - changes to
  1609. * those entries needs to be undone.
  1610. *
  1611. * MAC filter entries from this list were slated for addition.
  1612. **/
  1613. static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
  1614. struct hlist_head *from)
  1615. {
  1616. struct i40e_new_mac_filter *new;
  1617. struct hlist_node *h;
  1618. hlist_for_each_entry_safe(new, h, from, hlist) {
  1619. /* We can simply free the wrapper structure */
  1620. hlist_del(&new->hlist);
  1621. kfree(new);
  1622. }
  1623. }
  1624. /**
  1625. * i40e_next_entry - Get the next non-broadcast filter from a list
  1626. * @next: pointer to filter in list
  1627. *
  1628. * Returns the next non-broadcast filter in the list. Required so that we
  1629. * ignore broadcast filters within the list, since these are not handled via
  1630. * the normal firmware update path.
  1631. */
  1632. static
  1633. struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
  1634. {
  1635. hlist_for_each_entry_continue(next, hlist) {
  1636. if (!is_broadcast_ether_addr(next->f->macaddr))
  1637. return next;
  1638. }
  1639. return NULL;
  1640. }
  1641. /**
  1642. * i40e_update_filter_state - Update filter state based on return data
  1643. * from firmware
  1644. * @count: Number of filters added
  1645. * @add_list: return data from fw
  1646. * @head: pointer to first filter in current batch
  1647. *
  1648. * MAC filter entries from list were slated to be added to device. Returns
  1649. * number of successful filters. Note that 0 does NOT mean success!
  1650. **/
  1651. static int
  1652. i40e_update_filter_state(int count,
  1653. struct i40e_aqc_add_macvlan_element_data *add_list,
  1654. struct i40e_new_mac_filter *add_head)
  1655. {
  1656. int retval = 0;
  1657. int i;
  1658. for (i = 0; i < count; i++) {
  1659. /* Always check status of each filter. We don't need to check
  1660. * the firmware return status because we pre-set the filter
  1661. * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
  1662. * request to the adminq. Thus, if it no longer matches then
  1663. * we know the filter is active.
  1664. */
  1665. if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
  1666. add_head->state = I40E_FILTER_FAILED;
  1667. } else {
  1668. add_head->state = I40E_FILTER_ACTIVE;
  1669. retval++;
  1670. }
  1671. add_head = i40e_next_filter(add_head);
  1672. if (!add_head)
  1673. break;
  1674. }
  1675. return retval;
  1676. }
  1677. /**
  1678. * i40e_aqc_del_filters - Request firmware to delete a set of filters
  1679. * @vsi: ptr to the VSI
  1680. * @vsi_name: name to display in messages
  1681. * @list: the list of filters to send to firmware
  1682. * @num_del: the number of filters to delete
  1683. * @retval: Set to -EIO on failure to delete
  1684. *
  1685. * Send a request to firmware via AdminQ to delete a set of filters. Uses
  1686. * *retval instead of a return value so that success does not force ret_val to
  1687. * be set to 0. This ensures that a sequence of calls to this function
  1688. * preserve the previous value of *retval on successful delete.
  1689. */
  1690. static
  1691. void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
  1692. struct i40e_aqc_remove_macvlan_element_data *list,
  1693. int num_del, int *retval)
  1694. {
  1695. struct i40e_hw *hw = &vsi->back->hw;
  1696. i40e_status aq_ret;
  1697. int aq_err;
  1698. aq_ret = i40e_aq_remove_macvlan(hw, vsi->seid, list, num_del, NULL);
  1699. aq_err = hw->aq.asq_last_status;
  1700. /* Explicitly ignore and do not report when firmware returns ENOENT */
  1701. if (aq_ret && !(aq_err == I40E_AQ_RC_ENOENT)) {
  1702. *retval = -EIO;
  1703. dev_info(&vsi->back->pdev->dev,
  1704. "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
  1705. vsi_name, i40e_stat_str(hw, aq_ret),
  1706. i40e_aq_str(hw, aq_err));
  1707. }
  1708. }
  1709. /**
  1710. * i40e_aqc_add_filters - Request firmware to add a set of filters
  1711. * @vsi: ptr to the VSI
  1712. * @vsi_name: name to display in messages
  1713. * @list: the list of filters to send to firmware
  1714. * @add_head: Position in the add hlist
  1715. * @num_add: the number of filters to add
  1716. * @promisc_change: set to true on exit if promiscuous mode was forced on
  1717. *
  1718. * Send a request to firmware via AdminQ to add a chunk of filters. Will set
  1719. * promisc_changed to true if the firmware has run out of space for more
  1720. * filters.
  1721. */
  1722. static
  1723. void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
  1724. struct i40e_aqc_add_macvlan_element_data *list,
  1725. struct i40e_new_mac_filter *add_head,
  1726. int num_add, bool *promisc_changed)
  1727. {
  1728. struct i40e_hw *hw = &vsi->back->hw;
  1729. int aq_err, fcnt;
  1730. i40e_aq_add_macvlan(hw, vsi->seid, list, num_add, NULL);
  1731. aq_err = hw->aq.asq_last_status;
  1732. fcnt = i40e_update_filter_state(num_add, list, add_head);
  1733. if (fcnt != num_add) {
  1734. *promisc_changed = true;
  1735. set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  1736. dev_warn(&vsi->back->pdev->dev,
  1737. "Error %s adding RX filters on %s, promiscuous mode forced on\n",
  1738. i40e_aq_str(hw, aq_err),
  1739. vsi_name);
  1740. }
  1741. }
  1742. /**
  1743. * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
  1744. * @vsi: pointer to the VSI
  1745. * @f: filter data
  1746. *
  1747. * This function sets or clears the promiscuous broadcast flags for VLAN
  1748. * filters in order to properly receive broadcast frames. Assumes that only
  1749. * broadcast filters are passed.
  1750. *
  1751. * Returns status indicating success or failure;
  1752. **/
  1753. static i40e_status
  1754. i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
  1755. struct i40e_mac_filter *f)
  1756. {
  1757. bool enable = f->state == I40E_FILTER_NEW;
  1758. struct i40e_hw *hw = &vsi->back->hw;
  1759. i40e_status aq_ret;
  1760. if (f->vlan == I40E_VLAN_ANY) {
  1761. aq_ret = i40e_aq_set_vsi_broadcast(hw,
  1762. vsi->seid,
  1763. enable,
  1764. NULL);
  1765. } else {
  1766. aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
  1767. vsi->seid,
  1768. enable,
  1769. f->vlan,
  1770. NULL);
  1771. }
  1772. if (aq_ret)
  1773. dev_warn(&vsi->back->pdev->dev,
  1774. "Error %s setting broadcast promiscuous mode on %s\n",
  1775. i40e_aq_str(hw, hw->aq.asq_last_status),
  1776. vsi_name);
  1777. return aq_ret;
  1778. }
  1779. /**
  1780. * i40e_sync_vsi_filters - Update the VSI filter list to the HW
  1781. * @vsi: ptr to the VSI
  1782. *
  1783. * Push any outstanding VSI filter changes through the AdminQ.
  1784. *
  1785. * Returns 0 or error value
  1786. **/
  1787. int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
  1788. {
  1789. struct hlist_head tmp_add_list, tmp_del_list;
  1790. struct i40e_mac_filter *f;
  1791. struct i40e_new_mac_filter *new, *add_head = NULL;
  1792. struct i40e_hw *hw = &vsi->back->hw;
  1793. unsigned int failed_filters = 0;
  1794. unsigned int vlan_filters = 0;
  1795. bool promisc_changed = false;
  1796. char vsi_name[16] = "PF";
  1797. int filter_list_len = 0;
  1798. i40e_status aq_ret = 0;
  1799. u32 changed_flags = 0;
  1800. struct hlist_node *h;
  1801. struct i40e_pf *pf;
  1802. int num_add = 0;
  1803. int num_del = 0;
  1804. int retval = 0;
  1805. u16 cmd_flags;
  1806. int list_size;
  1807. int bkt;
  1808. /* empty array typed pointers, kcalloc later */
  1809. struct i40e_aqc_add_macvlan_element_data *add_list;
  1810. struct i40e_aqc_remove_macvlan_element_data *del_list;
  1811. while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state))
  1812. usleep_range(1000, 2000);
  1813. pf = vsi->back;
  1814. if (vsi->netdev) {
  1815. changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
  1816. vsi->current_netdev_flags = vsi->netdev->flags;
  1817. }
  1818. INIT_HLIST_HEAD(&tmp_add_list);
  1819. INIT_HLIST_HEAD(&tmp_del_list);
  1820. if (vsi->type == I40E_VSI_SRIOV)
  1821. snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
  1822. else if (vsi->type != I40E_VSI_MAIN)
  1823. snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
  1824. if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
  1825. vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
  1826. spin_lock_bh(&vsi->mac_filter_hash_lock);
  1827. /* Create a list of filters to delete. */
  1828. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  1829. if (f->state == I40E_FILTER_REMOVE) {
  1830. /* Move the element into temporary del_list */
  1831. hash_del(&f->hlist);
  1832. hlist_add_head(&f->hlist, &tmp_del_list);
  1833. /* Avoid counting removed filters */
  1834. continue;
  1835. }
  1836. if (f->state == I40E_FILTER_NEW) {
  1837. /* Create a temporary i40e_new_mac_filter */
  1838. new = kzalloc(sizeof(*new), GFP_ATOMIC);
  1839. if (!new)
  1840. goto err_no_memory_locked;
  1841. /* Store pointer to the real filter */
  1842. new->f = f;
  1843. new->state = f->state;
  1844. /* Add it to the hash list */
  1845. hlist_add_head(&new->hlist, &tmp_add_list);
  1846. }
  1847. /* Count the number of active (current and new) VLAN
  1848. * filters we have now. Does not count filters which
  1849. * are marked for deletion.
  1850. */
  1851. if (f->vlan > 0)
  1852. vlan_filters++;
  1853. }
  1854. retval = i40e_correct_mac_vlan_filters(vsi,
  1855. &tmp_add_list,
  1856. &tmp_del_list,
  1857. vlan_filters);
  1858. if (retval)
  1859. goto err_no_memory_locked;
  1860. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  1861. }
  1862. /* Now process 'del_list' outside the lock */
  1863. if (!hlist_empty(&tmp_del_list)) {
  1864. filter_list_len = hw->aq.asq_buf_size /
  1865. sizeof(struct i40e_aqc_remove_macvlan_element_data);
  1866. list_size = filter_list_len *
  1867. sizeof(struct i40e_aqc_remove_macvlan_element_data);
  1868. del_list = kzalloc(list_size, GFP_ATOMIC);
  1869. if (!del_list)
  1870. goto err_no_memory;
  1871. hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
  1872. cmd_flags = 0;
  1873. /* handle broadcast filters by updating the broadcast
  1874. * promiscuous flag and release filter list.
  1875. */
  1876. if (is_broadcast_ether_addr(f->macaddr)) {
  1877. i40e_aqc_broadcast_filter(vsi, vsi_name, f);
  1878. hlist_del(&f->hlist);
  1879. kfree(f);
  1880. continue;
  1881. }
  1882. /* add to delete list */
  1883. ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
  1884. if (f->vlan == I40E_VLAN_ANY) {
  1885. del_list[num_del].vlan_tag = 0;
  1886. cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
  1887. } else {
  1888. del_list[num_del].vlan_tag =
  1889. cpu_to_le16((u16)(f->vlan));
  1890. }
  1891. cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
  1892. del_list[num_del].flags = cmd_flags;
  1893. num_del++;
  1894. /* flush a full buffer */
  1895. if (num_del == filter_list_len) {
  1896. i40e_aqc_del_filters(vsi, vsi_name, del_list,
  1897. num_del, &retval);
  1898. memset(del_list, 0, list_size);
  1899. num_del = 0;
  1900. }
  1901. /* Release memory for MAC filter entries which were
  1902. * synced up with HW.
  1903. */
  1904. hlist_del(&f->hlist);
  1905. kfree(f);
  1906. }
  1907. if (num_del) {
  1908. i40e_aqc_del_filters(vsi, vsi_name, del_list,
  1909. num_del, &retval);
  1910. }
  1911. kfree(del_list);
  1912. del_list = NULL;
  1913. }
  1914. if (!hlist_empty(&tmp_add_list)) {
  1915. /* Do all the adds now. */
  1916. filter_list_len = hw->aq.asq_buf_size /
  1917. sizeof(struct i40e_aqc_add_macvlan_element_data);
  1918. list_size = filter_list_len *
  1919. sizeof(struct i40e_aqc_add_macvlan_element_data);
  1920. add_list = kzalloc(list_size, GFP_ATOMIC);
  1921. if (!add_list)
  1922. goto err_no_memory;
  1923. num_add = 0;
  1924. hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
  1925. if (test_bit(__I40E_VSI_OVERFLOW_PROMISC,
  1926. vsi->state)) {
  1927. new->state = I40E_FILTER_FAILED;
  1928. continue;
  1929. }
  1930. /* handle broadcast filters by updating the broadcast
  1931. * promiscuous flag instead of adding a MAC filter.
  1932. */
  1933. if (is_broadcast_ether_addr(new->f->macaddr)) {
  1934. if (i40e_aqc_broadcast_filter(vsi, vsi_name,
  1935. new->f))
  1936. new->state = I40E_FILTER_FAILED;
  1937. else
  1938. new->state = I40E_FILTER_ACTIVE;
  1939. continue;
  1940. }
  1941. /* add to add array */
  1942. if (num_add == 0)
  1943. add_head = new;
  1944. cmd_flags = 0;
  1945. ether_addr_copy(add_list[num_add].mac_addr,
  1946. new->f->macaddr);
  1947. if (new->f->vlan == I40E_VLAN_ANY) {
  1948. add_list[num_add].vlan_tag = 0;
  1949. cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
  1950. } else {
  1951. add_list[num_add].vlan_tag =
  1952. cpu_to_le16((u16)(new->f->vlan));
  1953. }
  1954. add_list[num_add].queue_number = 0;
  1955. /* set invalid match method for later detection */
  1956. add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
  1957. cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
  1958. add_list[num_add].flags = cpu_to_le16(cmd_flags);
  1959. num_add++;
  1960. /* flush a full buffer */
  1961. if (num_add == filter_list_len) {
  1962. i40e_aqc_add_filters(vsi, vsi_name, add_list,
  1963. add_head, num_add,
  1964. &promisc_changed);
  1965. memset(add_list, 0, list_size);
  1966. num_add = 0;
  1967. }
  1968. }
  1969. if (num_add) {
  1970. i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
  1971. num_add, &promisc_changed);
  1972. }
  1973. /* Now move all of the filters from the temp add list back to
  1974. * the VSI's list.
  1975. */
  1976. spin_lock_bh(&vsi->mac_filter_hash_lock);
  1977. hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
  1978. /* Only update the state if we're still NEW */
  1979. if (new->f->state == I40E_FILTER_NEW)
  1980. new->f->state = new->state;
  1981. hlist_del(&new->hlist);
  1982. kfree(new);
  1983. }
  1984. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  1985. kfree(add_list);
  1986. add_list = NULL;
  1987. }
  1988. /* Determine the number of active and failed filters. */
  1989. spin_lock_bh(&vsi->mac_filter_hash_lock);
  1990. vsi->active_filters = 0;
  1991. hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
  1992. if (f->state == I40E_FILTER_ACTIVE)
  1993. vsi->active_filters++;
  1994. else if (f->state == I40E_FILTER_FAILED)
  1995. failed_filters++;
  1996. }
  1997. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  1998. /* If promiscuous mode has changed, we need to calculate a new
  1999. * threshold for when we are safe to exit
  2000. */
  2001. if (promisc_changed)
  2002. vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
  2003. /* Check if we are able to exit overflow promiscuous mode. We can
  2004. * safely exit if we didn't just enter, we no longer have any failed
  2005. * filters, and we have reduced filters below the threshold value.
  2006. */
  2007. if (test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state) &&
  2008. !promisc_changed && !failed_filters &&
  2009. (vsi->active_filters < vsi->promisc_threshold)) {
  2010. dev_info(&pf->pdev->dev,
  2011. "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
  2012. vsi_name);
  2013. clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  2014. promisc_changed = true;
  2015. vsi->promisc_threshold = 0;
  2016. }
  2017. /* if the VF is not trusted do not do promisc */
  2018. if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
  2019. clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  2020. goto out;
  2021. }
  2022. /* check for changes in promiscuous modes */
  2023. if (changed_flags & IFF_ALLMULTI) {
  2024. bool cur_multipromisc;
  2025. cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
  2026. aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
  2027. vsi->seid,
  2028. cur_multipromisc,
  2029. NULL);
  2030. if (aq_ret) {
  2031. retval = i40e_aq_rc_to_posix(aq_ret,
  2032. hw->aq.asq_last_status);
  2033. dev_info(&pf->pdev->dev,
  2034. "set multi promisc failed on %s, err %s aq_err %s\n",
  2035. vsi_name,
  2036. i40e_stat_str(hw, aq_ret),
  2037. i40e_aq_str(hw, hw->aq.asq_last_status));
  2038. }
  2039. }
  2040. if ((changed_flags & IFF_PROMISC) || promisc_changed) {
  2041. bool cur_promisc;
  2042. cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
  2043. test_bit(__I40E_VSI_OVERFLOW_PROMISC,
  2044. vsi->state));
  2045. if ((vsi->type == I40E_VSI_MAIN) &&
  2046. (pf->lan_veb != I40E_NO_VEB) &&
  2047. !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
  2048. /* set defport ON for Main VSI instead of true promisc
  2049. * this way we will get all unicast/multicast and VLAN
  2050. * promisc behavior but will not get VF or VMDq traffic
  2051. * replicated on the Main VSI.
  2052. */
  2053. if (pf->cur_promisc != cur_promisc) {
  2054. pf->cur_promisc = cur_promisc;
  2055. if (cur_promisc)
  2056. aq_ret =
  2057. i40e_aq_set_default_vsi(hw,
  2058. vsi->seid,
  2059. NULL);
  2060. else
  2061. aq_ret =
  2062. i40e_aq_clear_default_vsi(hw,
  2063. vsi->seid,
  2064. NULL);
  2065. if (aq_ret) {
  2066. retval = i40e_aq_rc_to_posix(aq_ret,
  2067. hw->aq.asq_last_status);
  2068. dev_info(&pf->pdev->dev,
  2069. "Set default VSI failed on %s, err %s, aq_err %s\n",
  2070. vsi_name,
  2071. i40e_stat_str(hw, aq_ret),
  2072. i40e_aq_str(hw,
  2073. hw->aq.asq_last_status));
  2074. }
  2075. }
  2076. } else {
  2077. aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
  2078. hw,
  2079. vsi->seid,
  2080. cur_promisc, NULL,
  2081. true);
  2082. if (aq_ret) {
  2083. retval =
  2084. i40e_aq_rc_to_posix(aq_ret,
  2085. hw->aq.asq_last_status);
  2086. dev_info(&pf->pdev->dev,
  2087. "set unicast promisc failed on %s, err %s, aq_err %s\n",
  2088. vsi_name,
  2089. i40e_stat_str(hw, aq_ret),
  2090. i40e_aq_str(hw,
  2091. hw->aq.asq_last_status));
  2092. }
  2093. aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
  2094. hw,
  2095. vsi->seid,
  2096. cur_promisc, NULL);
  2097. if (aq_ret) {
  2098. retval =
  2099. i40e_aq_rc_to_posix(aq_ret,
  2100. hw->aq.asq_last_status);
  2101. dev_info(&pf->pdev->dev,
  2102. "set multicast promisc failed on %s, err %s, aq_err %s\n",
  2103. vsi_name,
  2104. i40e_stat_str(hw, aq_ret),
  2105. i40e_aq_str(hw,
  2106. hw->aq.asq_last_status));
  2107. }
  2108. }
  2109. aq_ret = i40e_aq_set_vsi_broadcast(&vsi->back->hw,
  2110. vsi->seid,
  2111. cur_promisc, NULL);
  2112. if (aq_ret) {
  2113. retval = i40e_aq_rc_to_posix(aq_ret,
  2114. pf->hw.aq.asq_last_status);
  2115. dev_info(&pf->pdev->dev,
  2116. "set brdcast promisc failed, err %s, aq_err %s\n",
  2117. i40e_stat_str(hw, aq_ret),
  2118. i40e_aq_str(hw,
  2119. hw->aq.asq_last_status));
  2120. }
  2121. }
  2122. out:
  2123. /* if something went wrong then set the changed flag so we try again */
  2124. if (retval)
  2125. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  2126. clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
  2127. return retval;
  2128. err_no_memory:
  2129. /* Restore elements on the temporary add and delete lists */
  2130. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2131. err_no_memory_locked:
  2132. i40e_undo_del_filter_entries(vsi, &tmp_del_list);
  2133. i40e_undo_add_filter_entries(vsi, &tmp_add_list);
  2134. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2135. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  2136. clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
  2137. return -ENOMEM;
  2138. }
  2139. /**
  2140. * i40e_sync_filters_subtask - Sync the VSI filter list with HW
  2141. * @pf: board private structure
  2142. **/
  2143. static void i40e_sync_filters_subtask(struct i40e_pf *pf)
  2144. {
  2145. int v;
  2146. if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC))
  2147. return;
  2148. pf->flags &= ~I40E_FLAG_FILTER_SYNC;
  2149. for (v = 0; v < pf->num_alloc_vsi; v++) {
  2150. if (pf->vsi[v] &&
  2151. (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED)) {
  2152. int ret = i40e_sync_vsi_filters(pf->vsi[v]);
  2153. if (ret) {
  2154. /* come back and try again later */
  2155. pf->flags |= I40E_FLAG_FILTER_SYNC;
  2156. break;
  2157. }
  2158. }
  2159. }
  2160. }
  2161. /**
  2162. * i40e_max_xdp_frame_size - returns the maximum allowed frame size for XDP
  2163. * @vsi: the vsi
  2164. **/
  2165. static int i40e_max_xdp_frame_size(struct i40e_vsi *vsi)
  2166. {
  2167. if (PAGE_SIZE >= 8192 || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
  2168. return I40E_RXBUFFER_2048;
  2169. else
  2170. return I40E_RXBUFFER_3072;
  2171. }
  2172. /**
  2173. * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
  2174. * @netdev: network interface device structure
  2175. * @new_mtu: new value for maximum frame size
  2176. *
  2177. * Returns 0 on success, negative on failure
  2178. **/
  2179. static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
  2180. {
  2181. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2182. struct i40e_vsi *vsi = np->vsi;
  2183. struct i40e_pf *pf = vsi->back;
  2184. if (i40e_enabled_xdp_vsi(vsi)) {
  2185. int frame_size = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
  2186. if (frame_size > i40e_max_xdp_frame_size(vsi))
  2187. return -EINVAL;
  2188. }
  2189. netdev_info(netdev, "changing MTU from %d to %d\n",
  2190. netdev->mtu, new_mtu);
  2191. netdev->mtu = new_mtu;
  2192. if (netif_running(netdev))
  2193. i40e_vsi_reinit_locked(vsi);
  2194. pf->flags |= (I40E_FLAG_SERVICE_CLIENT_REQUESTED |
  2195. I40E_FLAG_CLIENT_L2_CHANGE);
  2196. return 0;
  2197. }
  2198. /**
  2199. * i40e_ioctl - Access the hwtstamp interface
  2200. * @netdev: network interface device structure
  2201. * @ifr: interface request data
  2202. * @cmd: ioctl command
  2203. **/
  2204. int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  2205. {
  2206. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2207. struct i40e_pf *pf = np->vsi->back;
  2208. switch (cmd) {
  2209. case SIOCGHWTSTAMP:
  2210. return i40e_ptp_get_ts_config(pf, ifr);
  2211. case SIOCSHWTSTAMP:
  2212. return i40e_ptp_set_ts_config(pf, ifr);
  2213. default:
  2214. return -EOPNOTSUPP;
  2215. }
  2216. }
  2217. /**
  2218. * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
  2219. * @vsi: the vsi being adjusted
  2220. **/
  2221. void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
  2222. {
  2223. struct i40e_vsi_context ctxt;
  2224. i40e_status ret;
  2225. if ((vsi->info.valid_sections &
  2226. cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
  2227. ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
  2228. return; /* already enabled */
  2229. vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  2230. vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
  2231. I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
  2232. ctxt.seid = vsi->seid;
  2233. ctxt.info = vsi->info;
  2234. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  2235. if (ret) {
  2236. dev_info(&vsi->back->pdev->dev,
  2237. "update vlan stripping failed, err %s aq_err %s\n",
  2238. i40e_stat_str(&vsi->back->hw, ret),
  2239. i40e_aq_str(&vsi->back->hw,
  2240. vsi->back->hw.aq.asq_last_status));
  2241. }
  2242. }
  2243. /**
  2244. * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
  2245. * @vsi: the vsi being adjusted
  2246. **/
  2247. void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
  2248. {
  2249. struct i40e_vsi_context ctxt;
  2250. i40e_status ret;
  2251. if ((vsi->info.valid_sections &
  2252. cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
  2253. ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
  2254. I40E_AQ_VSI_PVLAN_EMOD_MASK))
  2255. return; /* already disabled */
  2256. vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  2257. vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
  2258. I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
  2259. ctxt.seid = vsi->seid;
  2260. ctxt.info = vsi->info;
  2261. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  2262. if (ret) {
  2263. dev_info(&vsi->back->pdev->dev,
  2264. "update vlan stripping failed, err %s aq_err %s\n",
  2265. i40e_stat_str(&vsi->back->hw, ret),
  2266. i40e_aq_str(&vsi->back->hw,
  2267. vsi->back->hw.aq.asq_last_status));
  2268. }
  2269. }
  2270. /**
  2271. * i40e_vlan_rx_register - Setup or shutdown vlan offload
  2272. * @netdev: network interface to be adjusted
  2273. * @features: netdev features to test if VLAN offload is enabled or not
  2274. **/
  2275. static void i40e_vlan_rx_register(struct net_device *netdev, u32 features)
  2276. {
  2277. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2278. struct i40e_vsi *vsi = np->vsi;
  2279. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  2280. i40e_vlan_stripping_enable(vsi);
  2281. else
  2282. i40e_vlan_stripping_disable(vsi);
  2283. }
  2284. /**
  2285. * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
  2286. * @vsi: the vsi being configured
  2287. * @vid: vlan id to be added (0 = untagged only , -1 = any)
  2288. *
  2289. * This is a helper function for adding a new MAC/VLAN filter with the
  2290. * specified VLAN for each existing MAC address already in the hash table.
  2291. * This function does *not* perform any accounting to update filters based on
  2292. * VLAN mode.
  2293. *
  2294. * NOTE: this function expects to be called while under the
  2295. * mac_filter_hash_lock
  2296. **/
  2297. int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
  2298. {
  2299. struct i40e_mac_filter *f, *add_f;
  2300. struct hlist_node *h;
  2301. int bkt;
  2302. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  2303. if (f->state == I40E_FILTER_REMOVE)
  2304. continue;
  2305. add_f = i40e_add_filter(vsi, f->macaddr, vid);
  2306. if (!add_f) {
  2307. dev_info(&vsi->back->pdev->dev,
  2308. "Could not add vlan filter %d for %pM\n",
  2309. vid, f->macaddr);
  2310. return -ENOMEM;
  2311. }
  2312. }
  2313. return 0;
  2314. }
  2315. /**
  2316. * i40e_vsi_add_vlan - Add VSI membership for given VLAN
  2317. * @vsi: the VSI being configured
  2318. * @vid: VLAN id to be added
  2319. **/
  2320. int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
  2321. {
  2322. int err;
  2323. if (vsi->info.pvid)
  2324. return -EINVAL;
  2325. /* The network stack will attempt to add VID=0, with the intention to
  2326. * receive priority tagged packets with a VLAN of 0. Our HW receives
  2327. * these packets by default when configured to receive untagged
  2328. * packets, so we don't need to add a filter for this case.
  2329. * Additionally, HW interprets adding a VID=0 filter as meaning to
  2330. * receive *only* tagged traffic and stops receiving untagged traffic.
  2331. * Thus, we do not want to actually add a filter for VID=0
  2332. */
  2333. if (!vid)
  2334. return 0;
  2335. /* Locked once because all functions invoked below iterates list*/
  2336. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2337. err = i40e_add_vlan_all_mac(vsi, vid);
  2338. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2339. if (err)
  2340. return err;
  2341. /* schedule our worker thread which will take care of
  2342. * applying the new filter changes
  2343. */
  2344. i40e_service_event_schedule(vsi->back);
  2345. return 0;
  2346. }
  2347. /**
  2348. * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
  2349. * @vsi: the vsi being configured
  2350. * @vid: vlan id to be removed (0 = untagged only , -1 = any)
  2351. *
  2352. * This function should be used to remove all VLAN filters which match the
  2353. * given VID. It does not schedule the service event and does not take the
  2354. * mac_filter_hash_lock so it may be combined with other operations under
  2355. * a single invocation of the mac_filter_hash_lock.
  2356. *
  2357. * NOTE: this function expects to be called while under the
  2358. * mac_filter_hash_lock
  2359. */
  2360. void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
  2361. {
  2362. struct i40e_mac_filter *f;
  2363. struct hlist_node *h;
  2364. int bkt;
  2365. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  2366. if (f->vlan == vid)
  2367. __i40e_del_filter(vsi, f);
  2368. }
  2369. }
  2370. /**
  2371. * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
  2372. * @vsi: the VSI being configured
  2373. * @vid: VLAN id to be removed
  2374. **/
  2375. void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
  2376. {
  2377. if (!vid || vsi->info.pvid)
  2378. return;
  2379. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2380. i40e_rm_vlan_all_mac(vsi, vid);
  2381. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2382. /* schedule our worker thread which will take care of
  2383. * applying the new filter changes
  2384. */
  2385. i40e_service_event_schedule(vsi->back);
  2386. }
  2387. /**
  2388. * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
  2389. * @netdev: network interface to be adjusted
  2390. * @vid: vlan id to be added
  2391. *
  2392. * net_device_ops implementation for adding vlan ids
  2393. **/
  2394. static int i40e_vlan_rx_add_vid(struct net_device *netdev,
  2395. __always_unused __be16 proto, u16 vid)
  2396. {
  2397. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2398. struct i40e_vsi *vsi = np->vsi;
  2399. int ret = 0;
  2400. if (vid >= VLAN_N_VID)
  2401. return -EINVAL;
  2402. ret = i40e_vsi_add_vlan(vsi, vid);
  2403. if (!ret)
  2404. set_bit(vid, vsi->active_vlans);
  2405. return ret;
  2406. }
  2407. /**
  2408. * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
  2409. * @netdev: network interface to be adjusted
  2410. * @vid: vlan id to be removed
  2411. *
  2412. * net_device_ops implementation for removing vlan ids
  2413. **/
  2414. static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
  2415. __always_unused __be16 proto, u16 vid)
  2416. {
  2417. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2418. struct i40e_vsi *vsi = np->vsi;
  2419. /* return code is ignored as there is nothing a user
  2420. * can do about failure to remove and a log message was
  2421. * already printed from the other function
  2422. */
  2423. i40e_vsi_kill_vlan(vsi, vid);
  2424. clear_bit(vid, vsi->active_vlans);
  2425. return 0;
  2426. }
  2427. /**
  2428. * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
  2429. * @vsi: the vsi being brought back up
  2430. **/
  2431. static void i40e_restore_vlan(struct i40e_vsi *vsi)
  2432. {
  2433. u16 vid;
  2434. if (!vsi->netdev)
  2435. return;
  2436. i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
  2437. for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
  2438. i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
  2439. vid);
  2440. }
  2441. /**
  2442. * i40e_vsi_add_pvid - Add pvid for the VSI
  2443. * @vsi: the vsi being adjusted
  2444. * @vid: the vlan id to set as a PVID
  2445. **/
  2446. int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
  2447. {
  2448. struct i40e_vsi_context ctxt;
  2449. i40e_status ret;
  2450. vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  2451. vsi->info.pvid = cpu_to_le16(vid);
  2452. vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
  2453. I40E_AQ_VSI_PVLAN_INSERT_PVID |
  2454. I40E_AQ_VSI_PVLAN_EMOD_STR;
  2455. ctxt.seid = vsi->seid;
  2456. ctxt.info = vsi->info;
  2457. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  2458. if (ret) {
  2459. dev_info(&vsi->back->pdev->dev,
  2460. "add pvid failed, err %s aq_err %s\n",
  2461. i40e_stat_str(&vsi->back->hw, ret),
  2462. i40e_aq_str(&vsi->back->hw,
  2463. vsi->back->hw.aq.asq_last_status));
  2464. return -ENOENT;
  2465. }
  2466. return 0;
  2467. }
  2468. /**
  2469. * i40e_vsi_remove_pvid - Remove the pvid from the VSI
  2470. * @vsi: the vsi being adjusted
  2471. *
  2472. * Just use the vlan_rx_register() service to put it back to normal
  2473. **/
  2474. void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
  2475. {
  2476. i40e_vlan_stripping_disable(vsi);
  2477. vsi->info.pvid = 0;
  2478. }
  2479. /**
  2480. * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
  2481. * @vsi: ptr to the VSI
  2482. *
  2483. * If this function returns with an error, then it's possible one or
  2484. * more of the rings is populated (while the rest are not). It is the
  2485. * callers duty to clean those orphaned rings.
  2486. *
  2487. * Return 0 on success, negative on failure
  2488. **/
  2489. static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
  2490. {
  2491. int i, err = 0;
  2492. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2493. err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
  2494. if (!i40e_enabled_xdp_vsi(vsi))
  2495. return err;
  2496. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2497. err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
  2498. return err;
  2499. }
  2500. /**
  2501. * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
  2502. * @vsi: ptr to the VSI
  2503. *
  2504. * Free VSI's transmit software resources
  2505. **/
  2506. static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
  2507. {
  2508. int i;
  2509. if (vsi->tx_rings) {
  2510. for (i = 0; i < vsi->num_queue_pairs; i++)
  2511. if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
  2512. i40e_free_tx_resources(vsi->tx_rings[i]);
  2513. }
  2514. if (vsi->xdp_rings) {
  2515. for (i = 0; i < vsi->num_queue_pairs; i++)
  2516. if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
  2517. i40e_free_tx_resources(vsi->xdp_rings[i]);
  2518. }
  2519. }
  2520. /**
  2521. * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
  2522. * @vsi: ptr to the VSI
  2523. *
  2524. * If this function returns with an error, then it's possible one or
  2525. * more of the rings is populated (while the rest are not). It is the
  2526. * callers duty to clean those orphaned rings.
  2527. *
  2528. * Return 0 on success, negative on failure
  2529. **/
  2530. static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
  2531. {
  2532. int i, err = 0;
  2533. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2534. err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
  2535. return err;
  2536. }
  2537. /**
  2538. * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
  2539. * @vsi: ptr to the VSI
  2540. *
  2541. * Free all receive software resources
  2542. **/
  2543. static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
  2544. {
  2545. int i;
  2546. if (!vsi->rx_rings)
  2547. return;
  2548. for (i = 0; i < vsi->num_queue_pairs; i++)
  2549. if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
  2550. i40e_free_rx_resources(vsi->rx_rings[i]);
  2551. }
  2552. /**
  2553. * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
  2554. * @ring: The Tx ring to configure
  2555. *
  2556. * This enables/disables XPS for a given Tx descriptor ring
  2557. * based on the TCs enabled for the VSI that ring belongs to.
  2558. **/
  2559. static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
  2560. {
  2561. struct i40e_vsi *vsi = ring->vsi;
  2562. if (!ring->q_vector || !ring->netdev)
  2563. return;
  2564. if ((vsi->tc_config.numtc <= 1) &&
  2565. !test_and_set_bit(__I40E_TX_XPS_INIT_DONE, &ring->state)) {
  2566. netif_set_xps_queue(ring->netdev,
  2567. get_cpu_mask(ring->q_vector->v_idx),
  2568. ring->queue_index);
  2569. }
  2570. /* schedule our worker thread which will take care of
  2571. * applying the new filter changes
  2572. */
  2573. i40e_service_event_schedule(vsi->back);
  2574. }
  2575. /**
  2576. * i40e_configure_tx_ring - Configure a transmit ring context and rest
  2577. * @ring: The Tx ring to configure
  2578. *
  2579. * Configure the Tx descriptor ring in the HMC context.
  2580. **/
  2581. static int i40e_configure_tx_ring(struct i40e_ring *ring)
  2582. {
  2583. struct i40e_vsi *vsi = ring->vsi;
  2584. u16 pf_q = vsi->base_queue + ring->queue_index;
  2585. struct i40e_hw *hw = &vsi->back->hw;
  2586. struct i40e_hmc_obj_txq tx_ctx;
  2587. i40e_status err = 0;
  2588. u32 qtx_ctl = 0;
  2589. /* some ATR related tx ring init */
  2590. if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
  2591. ring->atr_sample_rate = vsi->back->atr_sample_rate;
  2592. ring->atr_count = 0;
  2593. } else {
  2594. ring->atr_sample_rate = 0;
  2595. }
  2596. /* configure XPS */
  2597. i40e_config_xps_tx_ring(ring);
  2598. /* clear the context structure first */
  2599. memset(&tx_ctx, 0, sizeof(tx_ctx));
  2600. tx_ctx.new_context = 1;
  2601. tx_ctx.base = (ring->dma / 128);
  2602. tx_ctx.qlen = ring->count;
  2603. tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
  2604. I40E_FLAG_FD_ATR_ENABLED));
  2605. tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
  2606. /* FDIR VSI tx ring can still use RS bit and writebacks */
  2607. if (vsi->type != I40E_VSI_FDIR)
  2608. tx_ctx.head_wb_ena = 1;
  2609. tx_ctx.head_wb_addr = ring->dma +
  2610. (ring->count * sizeof(struct i40e_tx_desc));
  2611. /* As part of VSI creation/update, FW allocates certain
  2612. * Tx arbitration queue sets for each TC enabled for
  2613. * the VSI. The FW returns the handles to these queue
  2614. * sets as part of the response buffer to Add VSI,
  2615. * Update VSI, etc. AQ commands. It is expected that
  2616. * these queue set handles be associated with the Tx
  2617. * queues by the driver as part of the TX queue context
  2618. * initialization. This has to be done regardless of
  2619. * DCB as by default everything is mapped to TC0.
  2620. */
  2621. tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
  2622. tx_ctx.rdylist_act = 0;
  2623. /* clear the context in the HMC */
  2624. err = i40e_clear_lan_tx_queue_context(hw, pf_q);
  2625. if (err) {
  2626. dev_info(&vsi->back->pdev->dev,
  2627. "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
  2628. ring->queue_index, pf_q, err);
  2629. return -ENOMEM;
  2630. }
  2631. /* set the context in the HMC */
  2632. err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
  2633. if (err) {
  2634. dev_info(&vsi->back->pdev->dev,
  2635. "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
  2636. ring->queue_index, pf_q, err);
  2637. return -ENOMEM;
  2638. }
  2639. /* Now associate this queue with this PCI function */
  2640. if (vsi->type == I40E_VSI_VMDQ2) {
  2641. qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
  2642. qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
  2643. I40E_QTX_CTL_VFVM_INDX_MASK;
  2644. } else {
  2645. qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
  2646. }
  2647. qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
  2648. I40E_QTX_CTL_PF_INDX_MASK);
  2649. wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
  2650. i40e_flush(hw);
  2651. /* cache tail off for easier writes later */
  2652. ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
  2653. return 0;
  2654. }
  2655. /**
  2656. * i40e_configure_rx_ring - Configure a receive ring context
  2657. * @ring: The Rx ring to configure
  2658. *
  2659. * Configure the Rx descriptor ring in the HMC context.
  2660. **/
  2661. static int i40e_configure_rx_ring(struct i40e_ring *ring)
  2662. {
  2663. struct i40e_vsi *vsi = ring->vsi;
  2664. u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
  2665. u16 pf_q = vsi->base_queue + ring->queue_index;
  2666. struct i40e_hw *hw = &vsi->back->hw;
  2667. struct i40e_hmc_obj_rxq rx_ctx;
  2668. i40e_status err = 0;
  2669. ring->state = 0;
  2670. /* clear the context structure first */
  2671. memset(&rx_ctx, 0, sizeof(rx_ctx));
  2672. ring->rx_buf_len = vsi->rx_buf_len;
  2673. rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
  2674. BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
  2675. rx_ctx.base = (ring->dma / 128);
  2676. rx_ctx.qlen = ring->count;
  2677. /* use 32 byte descriptors */
  2678. rx_ctx.dsize = 1;
  2679. /* descriptor type is always zero
  2680. * rx_ctx.dtype = 0;
  2681. */
  2682. rx_ctx.hsplit_0 = 0;
  2683. rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
  2684. if (hw->revision_id == 0)
  2685. rx_ctx.lrxqthresh = 0;
  2686. else
  2687. rx_ctx.lrxqthresh = 2;
  2688. rx_ctx.crcstrip = 1;
  2689. rx_ctx.l2tsel = 1;
  2690. /* this controls whether VLAN is stripped from inner headers */
  2691. rx_ctx.showiv = 0;
  2692. /* set the prefena field to 1 because the manual says to */
  2693. rx_ctx.prefena = 1;
  2694. /* clear the context in the HMC */
  2695. err = i40e_clear_lan_rx_queue_context(hw, pf_q);
  2696. if (err) {
  2697. dev_info(&vsi->back->pdev->dev,
  2698. "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
  2699. ring->queue_index, pf_q, err);
  2700. return -ENOMEM;
  2701. }
  2702. /* set the context in the HMC */
  2703. err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
  2704. if (err) {
  2705. dev_info(&vsi->back->pdev->dev,
  2706. "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
  2707. ring->queue_index, pf_q, err);
  2708. return -ENOMEM;
  2709. }
  2710. /* configure Rx buffer alignment */
  2711. if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
  2712. clear_ring_build_skb_enabled(ring);
  2713. else
  2714. set_ring_build_skb_enabled(ring);
  2715. /* cache tail for quicker writes, and clear the reg before use */
  2716. ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
  2717. writel(0, ring->tail);
  2718. i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
  2719. return 0;
  2720. }
  2721. /**
  2722. * i40e_vsi_configure_tx - Configure the VSI for Tx
  2723. * @vsi: VSI structure describing this set of rings and resources
  2724. *
  2725. * Configure the Tx VSI for operation.
  2726. **/
  2727. static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
  2728. {
  2729. int err = 0;
  2730. u16 i;
  2731. for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
  2732. err = i40e_configure_tx_ring(vsi->tx_rings[i]);
  2733. if (!i40e_enabled_xdp_vsi(vsi))
  2734. return err;
  2735. for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
  2736. err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
  2737. return err;
  2738. }
  2739. /**
  2740. * i40e_vsi_configure_rx - Configure the VSI for Rx
  2741. * @vsi: the VSI being configured
  2742. *
  2743. * Configure the Rx VSI for operation.
  2744. **/
  2745. static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
  2746. {
  2747. int err = 0;
  2748. u16 i;
  2749. if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) {
  2750. vsi->max_frame = I40E_MAX_RXBUFFER;
  2751. vsi->rx_buf_len = I40E_RXBUFFER_2048;
  2752. #if (PAGE_SIZE < 8192)
  2753. } else if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
  2754. (vsi->netdev->mtu <= ETH_DATA_LEN)) {
  2755. vsi->max_frame = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
  2756. vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
  2757. #endif
  2758. } else {
  2759. vsi->max_frame = I40E_MAX_RXBUFFER;
  2760. vsi->rx_buf_len = (PAGE_SIZE < 8192) ? I40E_RXBUFFER_3072 :
  2761. I40E_RXBUFFER_2048;
  2762. }
  2763. /* set up individual rings */
  2764. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2765. err = i40e_configure_rx_ring(vsi->rx_rings[i]);
  2766. return err;
  2767. }
  2768. /**
  2769. * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
  2770. * @vsi: ptr to the VSI
  2771. **/
  2772. static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
  2773. {
  2774. struct i40e_ring *tx_ring, *rx_ring;
  2775. u16 qoffset, qcount;
  2776. int i, n;
  2777. if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
  2778. /* Reset the TC information */
  2779. for (i = 0; i < vsi->num_queue_pairs; i++) {
  2780. rx_ring = vsi->rx_rings[i];
  2781. tx_ring = vsi->tx_rings[i];
  2782. rx_ring->dcb_tc = 0;
  2783. tx_ring->dcb_tc = 0;
  2784. }
  2785. }
  2786. for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
  2787. if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
  2788. continue;
  2789. qoffset = vsi->tc_config.tc_info[n].qoffset;
  2790. qcount = vsi->tc_config.tc_info[n].qcount;
  2791. for (i = qoffset; i < (qoffset + qcount); i++) {
  2792. rx_ring = vsi->rx_rings[i];
  2793. tx_ring = vsi->tx_rings[i];
  2794. rx_ring->dcb_tc = n;
  2795. tx_ring->dcb_tc = n;
  2796. }
  2797. }
  2798. }
  2799. /**
  2800. * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
  2801. * @vsi: ptr to the VSI
  2802. **/
  2803. static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
  2804. {
  2805. if (vsi->netdev)
  2806. i40e_set_rx_mode(vsi->netdev);
  2807. }
  2808. /**
  2809. * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
  2810. * @vsi: Pointer to the targeted VSI
  2811. *
  2812. * This function replays the hlist on the hw where all the SB Flow Director
  2813. * filters were saved.
  2814. **/
  2815. static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
  2816. {
  2817. struct i40e_fdir_filter *filter;
  2818. struct i40e_pf *pf = vsi->back;
  2819. struct hlist_node *node;
  2820. if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
  2821. return;
  2822. /* Reset FDir counters as we're replaying all existing filters */
  2823. pf->fd_tcp4_filter_cnt = 0;
  2824. pf->fd_udp4_filter_cnt = 0;
  2825. pf->fd_sctp4_filter_cnt = 0;
  2826. pf->fd_ip4_filter_cnt = 0;
  2827. hlist_for_each_entry_safe(filter, node,
  2828. &pf->fdir_filter_list, fdir_node) {
  2829. i40e_add_del_fdir(vsi, filter, true);
  2830. }
  2831. }
  2832. /**
  2833. * i40e_vsi_configure - Set up the VSI for action
  2834. * @vsi: the VSI being configured
  2835. **/
  2836. static int i40e_vsi_configure(struct i40e_vsi *vsi)
  2837. {
  2838. int err;
  2839. i40e_set_vsi_rx_mode(vsi);
  2840. i40e_restore_vlan(vsi);
  2841. i40e_vsi_config_dcb_rings(vsi);
  2842. err = i40e_vsi_configure_tx(vsi);
  2843. if (!err)
  2844. err = i40e_vsi_configure_rx(vsi);
  2845. return err;
  2846. }
  2847. /**
  2848. * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
  2849. * @vsi: the VSI being configured
  2850. **/
  2851. static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
  2852. {
  2853. bool has_xdp = i40e_enabled_xdp_vsi(vsi);
  2854. struct i40e_pf *pf = vsi->back;
  2855. struct i40e_hw *hw = &pf->hw;
  2856. u16 vector;
  2857. int i, q;
  2858. u32 qp;
  2859. /* The interrupt indexing is offset by 1 in the PFINT_ITRn
  2860. * and PFINT_LNKLSTn registers, e.g.:
  2861. * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
  2862. */
  2863. qp = vsi->base_queue;
  2864. vector = vsi->base_vector;
  2865. for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
  2866. struct i40e_q_vector *q_vector = vsi->q_vectors[i];
  2867. q_vector->itr_countdown = ITR_COUNTDOWN_START;
  2868. q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[i]->rx_itr_setting);
  2869. q_vector->rx.latency_range = I40E_LOW_LATENCY;
  2870. wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
  2871. q_vector->rx.itr);
  2872. q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[i]->tx_itr_setting);
  2873. q_vector->tx.latency_range = I40E_LOW_LATENCY;
  2874. wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
  2875. q_vector->tx.itr);
  2876. wr32(hw, I40E_PFINT_RATEN(vector - 1),
  2877. i40e_intrl_usec_to_reg(vsi->int_rate_limit));
  2878. /* Linked list for the queuepairs assigned to this vector */
  2879. wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
  2880. for (q = 0; q < q_vector->num_ringpairs; q++) {
  2881. u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
  2882. u32 val;
  2883. val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  2884. (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
  2885. (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
  2886. (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
  2887. (I40E_QUEUE_TYPE_TX <<
  2888. I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
  2889. wr32(hw, I40E_QINT_RQCTL(qp), val);
  2890. if (has_xdp) {
  2891. val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  2892. (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
  2893. (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
  2894. (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
  2895. (I40E_QUEUE_TYPE_TX <<
  2896. I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
  2897. wr32(hw, I40E_QINT_TQCTL(nextqp), val);
  2898. }
  2899. val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  2900. (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
  2901. (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
  2902. ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
  2903. (I40E_QUEUE_TYPE_RX <<
  2904. I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
  2905. /* Terminate the linked list */
  2906. if (q == (q_vector->num_ringpairs - 1))
  2907. val |= (I40E_QUEUE_END_OF_LIST <<
  2908. I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
  2909. wr32(hw, I40E_QINT_TQCTL(qp), val);
  2910. qp++;
  2911. }
  2912. }
  2913. i40e_flush(hw);
  2914. }
  2915. /**
  2916. * i40e_enable_misc_int_causes - enable the non-queue interrupts
  2917. * @hw: ptr to the hardware info
  2918. **/
  2919. static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
  2920. {
  2921. struct i40e_hw *hw = &pf->hw;
  2922. u32 val;
  2923. /* clear things first */
  2924. wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
  2925. rd32(hw, I40E_PFINT_ICR0); /* read to clear */
  2926. val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
  2927. I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
  2928. I40E_PFINT_ICR0_ENA_GRST_MASK |
  2929. I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
  2930. I40E_PFINT_ICR0_ENA_GPIO_MASK |
  2931. I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
  2932. I40E_PFINT_ICR0_ENA_VFLR_MASK |
  2933. I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
  2934. if (pf->flags & I40E_FLAG_IWARP_ENABLED)
  2935. val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
  2936. if (pf->flags & I40E_FLAG_PTP)
  2937. val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
  2938. wr32(hw, I40E_PFINT_ICR0_ENA, val);
  2939. /* SW_ITR_IDX = 0, but don't change INTENA */
  2940. wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
  2941. I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
  2942. /* OTHER_ITR_IDX = 0 */
  2943. wr32(hw, I40E_PFINT_STAT_CTL0, 0);
  2944. }
  2945. /**
  2946. * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
  2947. * @vsi: the VSI being configured
  2948. **/
  2949. static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
  2950. {
  2951. u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
  2952. struct i40e_q_vector *q_vector = vsi->q_vectors[0];
  2953. struct i40e_pf *pf = vsi->back;
  2954. struct i40e_hw *hw = &pf->hw;
  2955. u32 val;
  2956. /* set the ITR configuration */
  2957. q_vector->itr_countdown = ITR_COUNTDOWN_START;
  2958. q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[0]->rx_itr_setting);
  2959. q_vector->rx.latency_range = I40E_LOW_LATENCY;
  2960. wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
  2961. q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[0]->tx_itr_setting);
  2962. q_vector->tx.latency_range = I40E_LOW_LATENCY;
  2963. wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
  2964. i40e_enable_misc_int_causes(pf);
  2965. /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
  2966. wr32(hw, I40E_PFINT_LNKLST0, 0);
  2967. /* Associate the queue pair to the vector and enable the queue int */
  2968. val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  2969. (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
  2970. (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
  2971. (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
  2972. wr32(hw, I40E_QINT_RQCTL(0), val);
  2973. if (i40e_enabled_xdp_vsi(vsi)) {
  2974. val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  2975. (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT)|
  2976. (I40E_QUEUE_TYPE_TX
  2977. << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
  2978. wr32(hw, I40E_QINT_TQCTL(nextqp), val);
  2979. }
  2980. val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  2981. (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
  2982. (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
  2983. wr32(hw, I40E_QINT_TQCTL(0), val);
  2984. i40e_flush(hw);
  2985. }
  2986. /**
  2987. * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
  2988. * @pf: board private structure
  2989. **/
  2990. void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
  2991. {
  2992. struct i40e_hw *hw = &pf->hw;
  2993. wr32(hw, I40E_PFINT_DYN_CTL0,
  2994. I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
  2995. i40e_flush(hw);
  2996. }
  2997. /**
  2998. * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
  2999. * @pf: board private structure
  3000. * @clearpba: true when all pending interrupt events should be cleared
  3001. **/
  3002. void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf, bool clearpba)
  3003. {
  3004. struct i40e_hw *hw = &pf->hw;
  3005. u32 val;
  3006. val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
  3007. (clearpba ? I40E_PFINT_DYN_CTL0_CLEARPBA_MASK : 0) |
  3008. (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
  3009. wr32(hw, I40E_PFINT_DYN_CTL0, val);
  3010. i40e_flush(hw);
  3011. }
  3012. /**
  3013. * i40e_msix_clean_rings - MSIX mode Interrupt Handler
  3014. * @irq: interrupt number
  3015. * @data: pointer to a q_vector
  3016. **/
  3017. static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
  3018. {
  3019. struct i40e_q_vector *q_vector = data;
  3020. if (!q_vector->tx.ring && !q_vector->rx.ring)
  3021. return IRQ_HANDLED;
  3022. napi_schedule_irqoff(&q_vector->napi);
  3023. return IRQ_HANDLED;
  3024. }
  3025. /**
  3026. * i40e_irq_affinity_notify - Callback for affinity changes
  3027. * @notify: context as to what irq was changed
  3028. * @mask: the new affinity mask
  3029. *
  3030. * This is a callback function used by the irq_set_affinity_notifier function
  3031. * so that we may register to receive changes to the irq affinity masks.
  3032. **/
  3033. static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
  3034. const cpumask_t *mask)
  3035. {
  3036. struct i40e_q_vector *q_vector =
  3037. container_of(notify, struct i40e_q_vector, affinity_notify);
  3038. cpumask_copy(&q_vector->affinity_mask, mask);
  3039. }
  3040. /**
  3041. * i40e_irq_affinity_release - Callback for affinity notifier release
  3042. * @ref: internal core kernel usage
  3043. *
  3044. * This is a callback function used by the irq_set_affinity_notifier function
  3045. * to inform the current notification subscriber that they will no longer
  3046. * receive notifications.
  3047. **/
  3048. static void i40e_irq_affinity_release(struct kref *ref) {}
  3049. /**
  3050. * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
  3051. * @vsi: the VSI being configured
  3052. * @basename: name for the vector
  3053. *
  3054. * Allocates MSI-X vectors and requests interrupts from the kernel.
  3055. **/
  3056. static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
  3057. {
  3058. int q_vectors = vsi->num_q_vectors;
  3059. struct i40e_pf *pf = vsi->back;
  3060. int base = vsi->base_vector;
  3061. int rx_int_idx = 0;
  3062. int tx_int_idx = 0;
  3063. int vector, err;
  3064. int irq_num;
  3065. for (vector = 0; vector < q_vectors; vector++) {
  3066. struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
  3067. irq_num = pf->msix_entries[base + vector].vector;
  3068. if (q_vector->tx.ring && q_vector->rx.ring) {
  3069. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  3070. "%s-%s-%d", basename, "TxRx", rx_int_idx++);
  3071. tx_int_idx++;
  3072. } else if (q_vector->rx.ring) {
  3073. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  3074. "%s-%s-%d", basename, "rx", rx_int_idx++);
  3075. } else if (q_vector->tx.ring) {
  3076. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  3077. "%s-%s-%d", basename, "tx", tx_int_idx++);
  3078. } else {
  3079. /* skip this unused q_vector */
  3080. continue;
  3081. }
  3082. err = request_irq(irq_num,
  3083. vsi->irq_handler,
  3084. 0,
  3085. q_vector->name,
  3086. q_vector);
  3087. if (err) {
  3088. dev_info(&pf->pdev->dev,
  3089. "MSIX request_irq failed, error: %d\n", err);
  3090. goto free_queue_irqs;
  3091. }
  3092. /* register for affinity change notifications */
  3093. q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
  3094. q_vector->affinity_notify.release = i40e_irq_affinity_release;
  3095. irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
  3096. /* get_cpu_mask returns a static constant mask with
  3097. * a permanent lifetime so it's ok to use here.
  3098. */
  3099. irq_set_affinity_hint(irq_num, get_cpu_mask(q_vector->v_idx));
  3100. }
  3101. vsi->irqs_ready = true;
  3102. return 0;
  3103. free_queue_irqs:
  3104. while (vector) {
  3105. vector--;
  3106. irq_num = pf->msix_entries[base + vector].vector;
  3107. irq_set_affinity_notifier(irq_num, NULL);
  3108. irq_set_affinity_hint(irq_num, NULL);
  3109. free_irq(irq_num, &vsi->q_vectors[vector]);
  3110. }
  3111. return err;
  3112. }
  3113. /**
  3114. * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
  3115. * @vsi: the VSI being un-configured
  3116. **/
  3117. static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
  3118. {
  3119. struct i40e_pf *pf = vsi->back;
  3120. struct i40e_hw *hw = &pf->hw;
  3121. int base = vsi->base_vector;
  3122. int i;
  3123. /* disable interrupt causation from each queue */
  3124. for (i = 0; i < vsi->num_queue_pairs; i++) {
  3125. u32 val;
  3126. val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
  3127. val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
  3128. wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
  3129. val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
  3130. val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
  3131. wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
  3132. if (!i40e_enabled_xdp_vsi(vsi))
  3133. continue;
  3134. wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
  3135. }
  3136. /* disable each interrupt */
  3137. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3138. for (i = vsi->base_vector;
  3139. i < (vsi->num_q_vectors + vsi->base_vector); i++)
  3140. wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
  3141. i40e_flush(hw);
  3142. for (i = 0; i < vsi->num_q_vectors; i++)
  3143. synchronize_irq(pf->msix_entries[i + base].vector);
  3144. } else {
  3145. /* Legacy and MSI mode - this stops all interrupt handling */
  3146. wr32(hw, I40E_PFINT_ICR0_ENA, 0);
  3147. wr32(hw, I40E_PFINT_DYN_CTL0, 0);
  3148. i40e_flush(hw);
  3149. synchronize_irq(pf->pdev->irq);
  3150. }
  3151. }
  3152. /**
  3153. * i40e_vsi_enable_irq - Enable IRQ for the given VSI
  3154. * @vsi: the VSI being configured
  3155. **/
  3156. static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
  3157. {
  3158. struct i40e_pf *pf = vsi->back;
  3159. int i;
  3160. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3161. for (i = 0; i < vsi->num_q_vectors; i++)
  3162. i40e_irq_dynamic_enable(vsi, i);
  3163. } else {
  3164. i40e_irq_dynamic_enable_icr0(pf, true);
  3165. }
  3166. i40e_flush(&pf->hw);
  3167. return 0;
  3168. }
  3169. /**
  3170. * i40e_stop_misc_vector - Stop the vector that handles non-queue events
  3171. * @pf: board private structure
  3172. **/
  3173. static void i40e_stop_misc_vector(struct i40e_pf *pf)
  3174. {
  3175. /* Disable ICR 0 */
  3176. wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
  3177. i40e_flush(&pf->hw);
  3178. }
  3179. /**
  3180. * i40e_intr - MSI/Legacy and non-queue interrupt handler
  3181. * @irq: interrupt number
  3182. * @data: pointer to a q_vector
  3183. *
  3184. * This is the handler used for all MSI/Legacy interrupts, and deals
  3185. * with both queue and non-queue interrupts. This is also used in
  3186. * MSIX mode to handle the non-queue interrupts.
  3187. **/
  3188. static irqreturn_t i40e_intr(int irq, void *data)
  3189. {
  3190. struct i40e_pf *pf = (struct i40e_pf *)data;
  3191. struct i40e_hw *hw = &pf->hw;
  3192. irqreturn_t ret = IRQ_NONE;
  3193. u32 icr0, icr0_remaining;
  3194. u32 val, ena_mask;
  3195. icr0 = rd32(hw, I40E_PFINT_ICR0);
  3196. ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
  3197. /* if sharing a legacy IRQ, we might get called w/o an intr pending */
  3198. if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
  3199. goto enable_intr;
  3200. /* if interrupt but no bits showing, must be SWINT */
  3201. if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
  3202. (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
  3203. pf->sw_int_count++;
  3204. if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
  3205. (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
  3206. ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
  3207. dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
  3208. set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
  3209. }
  3210. /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
  3211. if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
  3212. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  3213. struct i40e_q_vector *q_vector = vsi->q_vectors[0];
  3214. /* We do not have a way to disarm Queue causes while leaving
  3215. * interrupt enabled for all other causes, ideally
  3216. * interrupt should be disabled while we are in NAPI but
  3217. * this is not a performance path and napi_schedule()
  3218. * can deal with rescheduling.
  3219. */
  3220. if (!test_bit(__I40E_DOWN, pf->state))
  3221. napi_schedule_irqoff(&q_vector->napi);
  3222. }
  3223. if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
  3224. ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
  3225. set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
  3226. i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
  3227. }
  3228. if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
  3229. ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
  3230. set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
  3231. }
  3232. if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
  3233. ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
  3234. set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
  3235. }
  3236. if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
  3237. if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
  3238. set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
  3239. ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
  3240. val = rd32(hw, I40E_GLGEN_RSTAT);
  3241. val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
  3242. >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
  3243. if (val == I40E_RESET_CORER) {
  3244. pf->corer_count++;
  3245. } else if (val == I40E_RESET_GLOBR) {
  3246. pf->globr_count++;
  3247. } else if (val == I40E_RESET_EMPR) {
  3248. pf->empr_count++;
  3249. set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
  3250. }
  3251. }
  3252. if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
  3253. icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
  3254. dev_info(&pf->pdev->dev, "HMC error interrupt\n");
  3255. dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
  3256. rd32(hw, I40E_PFHMC_ERRORINFO),
  3257. rd32(hw, I40E_PFHMC_ERRORDATA));
  3258. }
  3259. if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
  3260. u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
  3261. if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
  3262. icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
  3263. i40e_ptp_tx_hwtstamp(pf);
  3264. }
  3265. }
  3266. /* If a critical error is pending we have no choice but to reset the
  3267. * device.
  3268. * Report and mask out any remaining unexpected interrupts.
  3269. */
  3270. icr0_remaining = icr0 & ena_mask;
  3271. if (icr0_remaining) {
  3272. dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
  3273. icr0_remaining);
  3274. if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
  3275. (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
  3276. (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
  3277. dev_info(&pf->pdev->dev, "device will be reset\n");
  3278. set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
  3279. i40e_service_event_schedule(pf);
  3280. }
  3281. ena_mask &= ~icr0_remaining;
  3282. }
  3283. ret = IRQ_HANDLED;
  3284. enable_intr:
  3285. /* re-enable interrupt causes */
  3286. wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
  3287. if (!test_bit(__I40E_DOWN, pf->state)) {
  3288. i40e_service_event_schedule(pf);
  3289. i40e_irq_dynamic_enable_icr0(pf, false);
  3290. }
  3291. return ret;
  3292. }
  3293. /**
  3294. * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
  3295. * @tx_ring: tx ring to clean
  3296. * @budget: how many cleans we're allowed
  3297. *
  3298. * Returns true if there's any budget left (e.g. the clean is finished)
  3299. **/
  3300. static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
  3301. {
  3302. struct i40e_vsi *vsi = tx_ring->vsi;
  3303. u16 i = tx_ring->next_to_clean;
  3304. struct i40e_tx_buffer *tx_buf;
  3305. struct i40e_tx_desc *tx_desc;
  3306. tx_buf = &tx_ring->tx_bi[i];
  3307. tx_desc = I40E_TX_DESC(tx_ring, i);
  3308. i -= tx_ring->count;
  3309. do {
  3310. struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
  3311. /* if next_to_watch is not set then there is no work pending */
  3312. if (!eop_desc)
  3313. break;
  3314. /* prevent any other reads prior to eop_desc */
  3315. read_barrier_depends();
  3316. /* if the descriptor isn't done, no work yet to do */
  3317. if (!(eop_desc->cmd_type_offset_bsz &
  3318. cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
  3319. break;
  3320. /* clear next_to_watch to prevent false hangs */
  3321. tx_buf->next_to_watch = NULL;
  3322. tx_desc->buffer_addr = 0;
  3323. tx_desc->cmd_type_offset_bsz = 0;
  3324. /* move past filter desc */
  3325. tx_buf++;
  3326. tx_desc++;
  3327. i++;
  3328. if (unlikely(!i)) {
  3329. i -= tx_ring->count;
  3330. tx_buf = tx_ring->tx_bi;
  3331. tx_desc = I40E_TX_DESC(tx_ring, 0);
  3332. }
  3333. /* unmap skb header data */
  3334. dma_unmap_single(tx_ring->dev,
  3335. dma_unmap_addr(tx_buf, dma),
  3336. dma_unmap_len(tx_buf, len),
  3337. DMA_TO_DEVICE);
  3338. if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
  3339. kfree(tx_buf->raw_buf);
  3340. tx_buf->raw_buf = NULL;
  3341. tx_buf->tx_flags = 0;
  3342. tx_buf->next_to_watch = NULL;
  3343. dma_unmap_len_set(tx_buf, len, 0);
  3344. tx_desc->buffer_addr = 0;
  3345. tx_desc->cmd_type_offset_bsz = 0;
  3346. /* move us past the eop_desc for start of next FD desc */
  3347. tx_buf++;
  3348. tx_desc++;
  3349. i++;
  3350. if (unlikely(!i)) {
  3351. i -= tx_ring->count;
  3352. tx_buf = tx_ring->tx_bi;
  3353. tx_desc = I40E_TX_DESC(tx_ring, 0);
  3354. }
  3355. /* update budget accounting */
  3356. budget--;
  3357. } while (likely(budget));
  3358. i += tx_ring->count;
  3359. tx_ring->next_to_clean = i;
  3360. if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
  3361. i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
  3362. return budget > 0;
  3363. }
  3364. /**
  3365. * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
  3366. * @irq: interrupt number
  3367. * @data: pointer to a q_vector
  3368. **/
  3369. static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
  3370. {
  3371. struct i40e_q_vector *q_vector = data;
  3372. struct i40e_vsi *vsi;
  3373. if (!q_vector->tx.ring)
  3374. return IRQ_HANDLED;
  3375. vsi = q_vector->tx.ring->vsi;
  3376. i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
  3377. return IRQ_HANDLED;
  3378. }
  3379. /**
  3380. * i40e_map_vector_to_qp - Assigns the queue pair to the vector
  3381. * @vsi: the VSI being configured
  3382. * @v_idx: vector index
  3383. * @qp_idx: queue pair index
  3384. **/
  3385. static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
  3386. {
  3387. struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
  3388. struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
  3389. struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
  3390. tx_ring->q_vector = q_vector;
  3391. tx_ring->next = q_vector->tx.ring;
  3392. q_vector->tx.ring = tx_ring;
  3393. q_vector->tx.count++;
  3394. /* Place XDP Tx ring in the same q_vector ring list as regular Tx */
  3395. if (i40e_enabled_xdp_vsi(vsi)) {
  3396. struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
  3397. xdp_ring->q_vector = q_vector;
  3398. xdp_ring->next = q_vector->tx.ring;
  3399. q_vector->tx.ring = xdp_ring;
  3400. q_vector->tx.count++;
  3401. }
  3402. rx_ring->q_vector = q_vector;
  3403. rx_ring->next = q_vector->rx.ring;
  3404. q_vector->rx.ring = rx_ring;
  3405. q_vector->rx.count++;
  3406. }
  3407. /**
  3408. * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
  3409. * @vsi: the VSI being configured
  3410. *
  3411. * This function maps descriptor rings to the queue-specific vectors
  3412. * we were allotted through the MSI-X enabling code. Ideally, we'd have
  3413. * one vector per queue pair, but on a constrained vector budget, we
  3414. * group the queue pairs as "efficiently" as possible.
  3415. **/
  3416. static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
  3417. {
  3418. int qp_remaining = vsi->num_queue_pairs;
  3419. int q_vectors = vsi->num_q_vectors;
  3420. int num_ringpairs;
  3421. int v_start = 0;
  3422. int qp_idx = 0;
  3423. /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
  3424. * group them so there are multiple queues per vector.
  3425. * It is also important to go through all the vectors available to be
  3426. * sure that if we don't use all the vectors, that the remaining vectors
  3427. * are cleared. This is especially important when decreasing the
  3428. * number of queues in use.
  3429. */
  3430. for (; v_start < q_vectors; v_start++) {
  3431. struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
  3432. num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
  3433. q_vector->num_ringpairs = num_ringpairs;
  3434. q_vector->rx.count = 0;
  3435. q_vector->tx.count = 0;
  3436. q_vector->rx.ring = NULL;
  3437. q_vector->tx.ring = NULL;
  3438. while (num_ringpairs--) {
  3439. i40e_map_vector_to_qp(vsi, v_start, qp_idx);
  3440. qp_idx++;
  3441. qp_remaining--;
  3442. }
  3443. }
  3444. }
  3445. /**
  3446. * i40e_vsi_request_irq - Request IRQ from the OS
  3447. * @vsi: the VSI being configured
  3448. * @basename: name for the vector
  3449. **/
  3450. static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
  3451. {
  3452. struct i40e_pf *pf = vsi->back;
  3453. int err;
  3454. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  3455. err = i40e_vsi_request_irq_msix(vsi, basename);
  3456. else if (pf->flags & I40E_FLAG_MSI_ENABLED)
  3457. err = request_irq(pf->pdev->irq, i40e_intr, 0,
  3458. pf->int_name, pf);
  3459. else
  3460. err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
  3461. pf->int_name, pf);
  3462. if (err)
  3463. dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
  3464. return err;
  3465. }
  3466. #ifdef CONFIG_NET_POLL_CONTROLLER
  3467. /**
  3468. * i40e_netpoll - A Polling 'interrupt' handler
  3469. * @netdev: network interface device structure
  3470. *
  3471. * This is used by netconsole to send skbs without having to re-enable
  3472. * interrupts. It's not called while the normal interrupt routine is executing.
  3473. **/
  3474. static void i40e_netpoll(struct net_device *netdev)
  3475. {
  3476. struct i40e_netdev_priv *np = netdev_priv(netdev);
  3477. struct i40e_vsi *vsi = np->vsi;
  3478. struct i40e_pf *pf = vsi->back;
  3479. int i;
  3480. /* if interface is down do nothing */
  3481. if (test_bit(__I40E_VSI_DOWN, vsi->state))
  3482. return;
  3483. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3484. for (i = 0; i < vsi->num_q_vectors; i++)
  3485. i40e_msix_clean_rings(0, vsi->q_vectors[i]);
  3486. } else {
  3487. i40e_intr(pf->pdev->irq, netdev);
  3488. }
  3489. }
  3490. #endif
  3491. #define I40E_QTX_ENA_WAIT_COUNT 50
  3492. /**
  3493. * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
  3494. * @pf: the PF being configured
  3495. * @pf_q: the PF queue
  3496. * @enable: enable or disable state of the queue
  3497. *
  3498. * This routine will wait for the given Tx queue of the PF to reach the
  3499. * enabled or disabled state.
  3500. * Returns -ETIMEDOUT in case of failing to reach the requested state after
  3501. * multiple retries; else will return 0 in case of success.
  3502. **/
  3503. static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
  3504. {
  3505. int i;
  3506. u32 tx_reg;
  3507. for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
  3508. tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
  3509. if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
  3510. break;
  3511. usleep_range(10, 20);
  3512. }
  3513. if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
  3514. return -ETIMEDOUT;
  3515. return 0;
  3516. }
  3517. /**
  3518. * i40e_control_tx_q - Start or stop a particular Tx queue
  3519. * @pf: the PF structure
  3520. * @pf_q: the PF queue to configure
  3521. * @enable: start or stop the queue
  3522. *
  3523. * This function enables or disables a single queue. Note that any delay
  3524. * required after the operation is expected to be handled by the caller of
  3525. * this function.
  3526. **/
  3527. static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
  3528. {
  3529. struct i40e_hw *hw = &pf->hw;
  3530. u32 tx_reg;
  3531. int i;
  3532. /* warn the TX unit of coming changes */
  3533. i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
  3534. if (!enable)
  3535. usleep_range(10, 20);
  3536. for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
  3537. tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
  3538. if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
  3539. ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
  3540. break;
  3541. usleep_range(1000, 2000);
  3542. }
  3543. /* Skip if the queue is already in the requested state */
  3544. if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
  3545. return;
  3546. /* turn on/off the queue */
  3547. if (enable) {
  3548. wr32(hw, I40E_QTX_HEAD(pf_q), 0);
  3549. tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
  3550. } else {
  3551. tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
  3552. }
  3553. wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
  3554. }
  3555. /**
  3556. * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
  3557. * @seid: VSI SEID
  3558. * @pf: the PF structure
  3559. * @pf_q: the PF queue to configure
  3560. * @is_xdp: true if the queue is used for XDP
  3561. * @enable: start or stop the queue
  3562. **/
  3563. static int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
  3564. bool is_xdp, bool enable)
  3565. {
  3566. int ret;
  3567. i40e_control_tx_q(pf, pf_q, enable);
  3568. /* wait for the change to finish */
  3569. ret = i40e_pf_txq_wait(pf, pf_q, enable);
  3570. if (ret) {
  3571. dev_info(&pf->pdev->dev,
  3572. "VSI seid %d %sTx ring %d %sable timeout\n",
  3573. seid, (is_xdp ? "XDP " : ""), pf_q,
  3574. (enable ? "en" : "dis"));
  3575. }
  3576. return ret;
  3577. }
  3578. /**
  3579. * i40e_vsi_control_tx - Start or stop a VSI's rings
  3580. * @vsi: the VSI being configured
  3581. * @enable: start or stop the rings
  3582. **/
  3583. static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
  3584. {
  3585. struct i40e_pf *pf = vsi->back;
  3586. int i, pf_q, ret = 0;
  3587. pf_q = vsi->base_queue;
  3588. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  3589. ret = i40e_control_wait_tx_q(vsi->seid, pf,
  3590. pf_q,
  3591. false /*is xdp*/, enable);
  3592. if (ret)
  3593. break;
  3594. if (!i40e_enabled_xdp_vsi(vsi))
  3595. continue;
  3596. ret = i40e_control_wait_tx_q(vsi->seid, pf,
  3597. pf_q + vsi->alloc_queue_pairs,
  3598. true /*is xdp*/, enable);
  3599. if (ret)
  3600. break;
  3601. }
  3602. return ret;
  3603. }
  3604. /**
  3605. * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
  3606. * @pf: the PF being configured
  3607. * @pf_q: the PF queue
  3608. * @enable: enable or disable state of the queue
  3609. *
  3610. * This routine will wait for the given Rx queue of the PF to reach the
  3611. * enabled or disabled state.
  3612. * Returns -ETIMEDOUT in case of failing to reach the requested state after
  3613. * multiple retries; else will return 0 in case of success.
  3614. **/
  3615. static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
  3616. {
  3617. int i;
  3618. u32 rx_reg;
  3619. for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
  3620. rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
  3621. if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
  3622. break;
  3623. usleep_range(10, 20);
  3624. }
  3625. if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
  3626. return -ETIMEDOUT;
  3627. return 0;
  3628. }
  3629. /**
  3630. * i40e_control_rx_q - Start or stop a particular Rx queue
  3631. * @pf: the PF structure
  3632. * @pf_q: the PF queue to configure
  3633. * @enable: start or stop the queue
  3634. *
  3635. * This function enables or disables a single queue. Note that any delay
  3636. * required after the operation is expected to be handled by the caller of
  3637. * this function.
  3638. **/
  3639. static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
  3640. {
  3641. struct i40e_hw *hw = &pf->hw;
  3642. u32 rx_reg;
  3643. int i;
  3644. for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
  3645. rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
  3646. if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
  3647. ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
  3648. break;
  3649. usleep_range(1000, 2000);
  3650. }
  3651. /* Skip if the queue is already in the requested state */
  3652. if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
  3653. return;
  3654. /* turn on/off the queue */
  3655. if (enable)
  3656. rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
  3657. else
  3658. rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
  3659. wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
  3660. }
  3661. /**
  3662. * i40e_vsi_control_rx - Start or stop a VSI's rings
  3663. * @vsi: the VSI being configured
  3664. * @enable: start or stop the rings
  3665. **/
  3666. static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
  3667. {
  3668. struct i40e_pf *pf = vsi->back;
  3669. int i, pf_q, ret = 0;
  3670. pf_q = vsi->base_queue;
  3671. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  3672. i40e_control_rx_q(pf, pf_q, enable);
  3673. /* wait for the change to finish */
  3674. ret = i40e_pf_rxq_wait(pf, pf_q, enable);
  3675. if (ret) {
  3676. dev_info(&pf->pdev->dev,
  3677. "VSI seid %d Rx ring %d %sable timeout\n",
  3678. vsi->seid, pf_q, (enable ? "en" : "dis"));
  3679. break;
  3680. }
  3681. }
  3682. /* Due to HW errata, on Rx disable only, the register can indicate done
  3683. * before it really is. Needs 50ms to be sure
  3684. */
  3685. if (!enable)
  3686. mdelay(50);
  3687. return ret;
  3688. }
  3689. /**
  3690. * i40e_vsi_start_rings - Start a VSI's rings
  3691. * @vsi: the VSI being configured
  3692. **/
  3693. int i40e_vsi_start_rings(struct i40e_vsi *vsi)
  3694. {
  3695. int ret = 0;
  3696. /* do rx first for enable and last for disable */
  3697. ret = i40e_vsi_control_rx(vsi, true);
  3698. if (ret)
  3699. return ret;
  3700. ret = i40e_vsi_control_tx(vsi, true);
  3701. return ret;
  3702. }
  3703. /**
  3704. * i40e_vsi_stop_rings - Stop a VSI's rings
  3705. * @vsi: the VSI being configured
  3706. **/
  3707. void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
  3708. {
  3709. /* When port TX is suspended, don't wait */
  3710. if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
  3711. return i40e_vsi_stop_rings_no_wait(vsi);
  3712. /* do rx first for enable and last for disable
  3713. * Ignore return value, we need to shutdown whatever we can
  3714. */
  3715. i40e_vsi_control_tx(vsi, false);
  3716. i40e_vsi_control_rx(vsi, false);
  3717. }
  3718. /**
  3719. * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
  3720. * @vsi: the VSI being shutdown
  3721. *
  3722. * This function stops all the rings for a VSI but does not delay to verify
  3723. * that rings have been disabled. It is expected that the caller is shutting
  3724. * down multiple VSIs at once and will delay together for all the VSIs after
  3725. * initiating the shutdown. This is particularly useful for shutting down lots
  3726. * of VFs together. Otherwise, a large delay can be incurred while configuring
  3727. * each VSI in serial.
  3728. **/
  3729. void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
  3730. {
  3731. struct i40e_pf *pf = vsi->back;
  3732. int i, pf_q;
  3733. pf_q = vsi->base_queue;
  3734. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  3735. i40e_control_tx_q(pf, pf_q, false);
  3736. i40e_control_rx_q(pf, pf_q, false);
  3737. }
  3738. }
  3739. /**
  3740. * i40e_vsi_free_irq - Free the irq association with the OS
  3741. * @vsi: the VSI being configured
  3742. **/
  3743. static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
  3744. {
  3745. struct i40e_pf *pf = vsi->back;
  3746. struct i40e_hw *hw = &pf->hw;
  3747. int base = vsi->base_vector;
  3748. u32 val, qp;
  3749. int i;
  3750. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3751. if (!vsi->q_vectors)
  3752. return;
  3753. if (!vsi->irqs_ready)
  3754. return;
  3755. vsi->irqs_ready = false;
  3756. for (i = 0; i < vsi->num_q_vectors; i++) {
  3757. int irq_num;
  3758. u16 vector;
  3759. vector = i + base;
  3760. irq_num = pf->msix_entries[vector].vector;
  3761. /* free only the irqs that were actually requested */
  3762. if (!vsi->q_vectors[i] ||
  3763. !vsi->q_vectors[i]->num_ringpairs)
  3764. continue;
  3765. /* clear the affinity notifier in the IRQ descriptor */
  3766. irq_set_affinity_notifier(irq_num, NULL);
  3767. /* remove our suggested affinity mask for this IRQ */
  3768. irq_set_affinity_hint(irq_num, NULL);
  3769. synchronize_irq(irq_num);
  3770. free_irq(irq_num, vsi->q_vectors[i]);
  3771. /* Tear down the interrupt queue link list
  3772. *
  3773. * We know that they come in pairs and always
  3774. * the Rx first, then the Tx. To clear the
  3775. * link list, stick the EOL value into the
  3776. * next_q field of the registers.
  3777. */
  3778. val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
  3779. qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
  3780. >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
  3781. val |= I40E_QUEUE_END_OF_LIST
  3782. << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
  3783. wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
  3784. while (qp != I40E_QUEUE_END_OF_LIST) {
  3785. u32 next;
  3786. val = rd32(hw, I40E_QINT_RQCTL(qp));
  3787. val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
  3788. I40E_QINT_RQCTL_MSIX0_INDX_MASK |
  3789. I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  3790. I40E_QINT_RQCTL_INTEVENT_MASK);
  3791. val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
  3792. I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
  3793. wr32(hw, I40E_QINT_RQCTL(qp), val);
  3794. val = rd32(hw, I40E_QINT_TQCTL(qp));
  3795. next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
  3796. >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
  3797. val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
  3798. I40E_QINT_TQCTL_MSIX0_INDX_MASK |
  3799. I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  3800. I40E_QINT_TQCTL_INTEVENT_MASK);
  3801. val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
  3802. I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
  3803. wr32(hw, I40E_QINT_TQCTL(qp), val);
  3804. qp = next;
  3805. }
  3806. }
  3807. } else {
  3808. free_irq(pf->pdev->irq, pf);
  3809. val = rd32(hw, I40E_PFINT_LNKLST0);
  3810. qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
  3811. >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
  3812. val |= I40E_QUEUE_END_OF_LIST
  3813. << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
  3814. wr32(hw, I40E_PFINT_LNKLST0, val);
  3815. val = rd32(hw, I40E_QINT_RQCTL(qp));
  3816. val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
  3817. I40E_QINT_RQCTL_MSIX0_INDX_MASK |
  3818. I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  3819. I40E_QINT_RQCTL_INTEVENT_MASK);
  3820. val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
  3821. I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
  3822. wr32(hw, I40E_QINT_RQCTL(qp), val);
  3823. val = rd32(hw, I40E_QINT_TQCTL(qp));
  3824. val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
  3825. I40E_QINT_TQCTL_MSIX0_INDX_MASK |
  3826. I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  3827. I40E_QINT_TQCTL_INTEVENT_MASK);
  3828. val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
  3829. I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
  3830. wr32(hw, I40E_QINT_TQCTL(qp), val);
  3831. }
  3832. }
  3833. /**
  3834. * i40e_free_q_vector - Free memory allocated for specific interrupt vector
  3835. * @vsi: the VSI being configured
  3836. * @v_idx: Index of vector to be freed
  3837. *
  3838. * This function frees the memory allocated to the q_vector. In addition if
  3839. * NAPI is enabled it will delete any references to the NAPI struct prior
  3840. * to freeing the q_vector.
  3841. **/
  3842. static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
  3843. {
  3844. struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
  3845. struct i40e_ring *ring;
  3846. if (!q_vector)
  3847. return;
  3848. /* disassociate q_vector from rings */
  3849. i40e_for_each_ring(ring, q_vector->tx)
  3850. ring->q_vector = NULL;
  3851. i40e_for_each_ring(ring, q_vector->rx)
  3852. ring->q_vector = NULL;
  3853. /* only VSI w/ an associated netdev is set up w/ NAPI */
  3854. if (vsi->netdev)
  3855. netif_napi_del(&q_vector->napi);
  3856. vsi->q_vectors[v_idx] = NULL;
  3857. kfree_rcu(q_vector, rcu);
  3858. }
  3859. /**
  3860. * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
  3861. * @vsi: the VSI being un-configured
  3862. *
  3863. * This frees the memory allocated to the q_vectors and
  3864. * deletes references to the NAPI struct.
  3865. **/
  3866. static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
  3867. {
  3868. int v_idx;
  3869. for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
  3870. i40e_free_q_vector(vsi, v_idx);
  3871. }
  3872. /**
  3873. * i40e_reset_interrupt_capability - Disable interrupt setup in OS
  3874. * @pf: board private structure
  3875. **/
  3876. static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
  3877. {
  3878. /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
  3879. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3880. pci_disable_msix(pf->pdev);
  3881. kfree(pf->msix_entries);
  3882. pf->msix_entries = NULL;
  3883. kfree(pf->irq_pile);
  3884. pf->irq_pile = NULL;
  3885. } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
  3886. pci_disable_msi(pf->pdev);
  3887. }
  3888. pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
  3889. }
  3890. /**
  3891. * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
  3892. * @pf: board private structure
  3893. *
  3894. * We go through and clear interrupt specific resources and reset the structure
  3895. * to pre-load conditions
  3896. **/
  3897. static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
  3898. {
  3899. int i;
  3900. i40e_stop_misc_vector(pf);
  3901. if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
  3902. synchronize_irq(pf->msix_entries[0].vector);
  3903. free_irq(pf->msix_entries[0].vector, pf);
  3904. }
  3905. i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
  3906. I40E_IWARP_IRQ_PILE_ID);
  3907. i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
  3908. for (i = 0; i < pf->num_alloc_vsi; i++)
  3909. if (pf->vsi[i])
  3910. i40e_vsi_free_q_vectors(pf->vsi[i]);
  3911. i40e_reset_interrupt_capability(pf);
  3912. }
  3913. /**
  3914. * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
  3915. * @vsi: the VSI being configured
  3916. **/
  3917. static void i40e_napi_enable_all(struct i40e_vsi *vsi)
  3918. {
  3919. int q_idx;
  3920. if (!vsi->netdev)
  3921. return;
  3922. for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
  3923. struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
  3924. if (q_vector->rx.ring || q_vector->tx.ring)
  3925. napi_enable(&q_vector->napi);
  3926. }
  3927. }
  3928. /**
  3929. * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
  3930. * @vsi: the VSI being configured
  3931. **/
  3932. static void i40e_napi_disable_all(struct i40e_vsi *vsi)
  3933. {
  3934. int q_idx;
  3935. if (!vsi->netdev)
  3936. return;
  3937. for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
  3938. struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
  3939. if (q_vector->rx.ring || q_vector->tx.ring)
  3940. napi_disable(&q_vector->napi);
  3941. }
  3942. }
  3943. /**
  3944. * i40e_vsi_close - Shut down a VSI
  3945. * @vsi: the vsi to be quelled
  3946. **/
  3947. static void i40e_vsi_close(struct i40e_vsi *vsi)
  3948. {
  3949. struct i40e_pf *pf = vsi->back;
  3950. if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
  3951. i40e_down(vsi);
  3952. i40e_vsi_free_irq(vsi);
  3953. i40e_vsi_free_tx_resources(vsi);
  3954. i40e_vsi_free_rx_resources(vsi);
  3955. vsi->current_netdev_flags = 0;
  3956. pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
  3957. if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
  3958. pf->flags |= I40E_FLAG_CLIENT_RESET;
  3959. }
  3960. /**
  3961. * i40e_quiesce_vsi - Pause a given VSI
  3962. * @vsi: the VSI being paused
  3963. **/
  3964. static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
  3965. {
  3966. if (test_bit(__I40E_VSI_DOWN, vsi->state))
  3967. return;
  3968. set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
  3969. if (vsi->netdev && netif_running(vsi->netdev))
  3970. vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
  3971. else
  3972. i40e_vsi_close(vsi);
  3973. }
  3974. /**
  3975. * i40e_unquiesce_vsi - Resume a given VSI
  3976. * @vsi: the VSI being resumed
  3977. **/
  3978. static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
  3979. {
  3980. if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
  3981. return;
  3982. if (vsi->netdev && netif_running(vsi->netdev))
  3983. vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
  3984. else
  3985. i40e_vsi_open(vsi); /* this clears the DOWN bit */
  3986. }
  3987. /**
  3988. * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
  3989. * @pf: the PF
  3990. **/
  3991. static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
  3992. {
  3993. int v;
  3994. for (v = 0; v < pf->num_alloc_vsi; v++) {
  3995. if (pf->vsi[v])
  3996. i40e_quiesce_vsi(pf->vsi[v]);
  3997. }
  3998. }
  3999. /**
  4000. * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
  4001. * @pf: the PF
  4002. **/
  4003. static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
  4004. {
  4005. int v;
  4006. for (v = 0; v < pf->num_alloc_vsi; v++) {
  4007. if (pf->vsi[v])
  4008. i40e_unquiesce_vsi(pf->vsi[v]);
  4009. }
  4010. }
  4011. /**
  4012. * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
  4013. * @vsi: the VSI being configured
  4014. *
  4015. * Wait until all queues on a given VSI have been disabled.
  4016. **/
  4017. int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
  4018. {
  4019. struct i40e_pf *pf = vsi->back;
  4020. int i, pf_q, ret;
  4021. pf_q = vsi->base_queue;
  4022. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  4023. /* Check and wait for the Tx queue */
  4024. ret = i40e_pf_txq_wait(pf, pf_q, false);
  4025. if (ret) {
  4026. dev_info(&pf->pdev->dev,
  4027. "VSI seid %d Tx ring %d disable timeout\n",
  4028. vsi->seid, pf_q);
  4029. return ret;
  4030. }
  4031. if (!i40e_enabled_xdp_vsi(vsi))
  4032. goto wait_rx;
  4033. /* Check and wait for the XDP Tx queue */
  4034. ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
  4035. false);
  4036. if (ret) {
  4037. dev_info(&pf->pdev->dev,
  4038. "VSI seid %d XDP Tx ring %d disable timeout\n",
  4039. vsi->seid, pf_q);
  4040. return ret;
  4041. }
  4042. wait_rx:
  4043. /* Check and wait for the Rx queue */
  4044. ret = i40e_pf_rxq_wait(pf, pf_q, false);
  4045. if (ret) {
  4046. dev_info(&pf->pdev->dev,
  4047. "VSI seid %d Rx ring %d disable timeout\n",
  4048. vsi->seid, pf_q);
  4049. return ret;
  4050. }
  4051. }
  4052. return 0;
  4053. }
  4054. #ifdef CONFIG_I40E_DCB
  4055. /**
  4056. * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
  4057. * @pf: the PF
  4058. *
  4059. * This function waits for the queues to be in disabled state for all the
  4060. * VSIs that are managed by this PF.
  4061. **/
  4062. static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
  4063. {
  4064. int v, ret = 0;
  4065. for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
  4066. if (pf->vsi[v]) {
  4067. ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
  4068. if (ret)
  4069. break;
  4070. }
  4071. }
  4072. return ret;
  4073. }
  4074. #endif
  4075. /**
  4076. * i40e_detect_recover_hung_queue - Function to detect and recover hung_queue
  4077. * @q_idx: TX queue number
  4078. * @vsi: Pointer to VSI struct
  4079. *
  4080. * This function checks specified queue for given VSI. Detects hung condition.
  4081. * We proactively detect hung TX queues by checking if interrupts are disabled
  4082. * but there are pending descriptors. If it appears hung, attempt to recover
  4083. * by triggering a SW interrupt.
  4084. **/
  4085. static void i40e_detect_recover_hung_queue(int q_idx, struct i40e_vsi *vsi)
  4086. {
  4087. struct i40e_ring *tx_ring = NULL;
  4088. struct i40e_pf *pf;
  4089. u32 val, tx_pending;
  4090. int i;
  4091. pf = vsi->back;
  4092. /* now that we have an index, find the tx_ring struct */
  4093. for (i = 0; i < vsi->num_queue_pairs; i++) {
  4094. if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
  4095. if (q_idx == vsi->tx_rings[i]->queue_index) {
  4096. tx_ring = vsi->tx_rings[i];
  4097. break;
  4098. }
  4099. }
  4100. }
  4101. if (!tx_ring)
  4102. return;
  4103. /* Read interrupt register */
  4104. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  4105. val = rd32(&pf->hw,
  4106. I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
  4107. tx_ring->vsi->base_vector - 1));
  4108. else
  4109. val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
  4110. tx_pending = i40e_get_tx_pending(tx_ring);
  4111. /* Interrupts are disabled and TX pending is non-zero,
  4112. * trigger the SW interrupt (don't wait). Worst case
  4113. * there will be one extra interrupt which may result
  4114. * into not cleaning any queues because queues are cleaned.
  4115. */
  4116. if (tx_pending && (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK)))
  4117. i40e_force_wb(vsi, tx_ring->q_vector);
  4118. }
  4119. /**
  4120. * i40e_detect_recover_hung - Function to detect and recover hung_queues
  4121. * @pf: pointer to PF struct
  4122. *
  4123. * LAN VSI has netdev and netdev has TX queues. This function is to check
  4124. * each of those TX queues if they are hung, trigger recovery by issuing
  4125. * SW interrupt.
  4126. **/
  4127. static void i40e_detect_recover_hung(struct i40e_pf *pf)
  4128. {
  4129. struct net_device *netdev;
  4130. struct i40e_vsi *vsi;
  4131. unsigned int i;
  4132. /* Only for LAN VSI */
  4133. vsi = pf->vsi[pf->lan_vsi];
  4134. if (!vsi)
  4135. return;
  4136. /* Make sure, VSI state is not DOWN/RECOVERY_PENDING */
  4137. if (test_bit(__I40E_VSI_DOWN, vsi->back->state) ||
  4138. test_bit(__I40E_RESET_RECOVERY_PENDING, vsi->back->state))
  4139. return;
  4140. /* Make sure type is MAIN VSI */
  4141. if (vsi->type != I40E_VSI_MAIN)
  4142. return;
  4143. netdev = vsi->netdev;
  4144. if (!netdev)
  4145. return;
  4146. /* Bail out if netif_carrier is not OK */
  4147. if (!netif_carrier_ok(netdev))
  4148. return;
  4149. /* Go thru' TX queues for netdev */
  4150. for (i = 0; i < netdev->num_tx_queues; i++) {
  4151. struct netdev_queue *q;
  4152. q = netdev_get_tx_queue(netdev, i);
  4153. if (q)
  4154. i40e_detect_recover_hung_queue(i, vsi);
  4155. }
  4156. }
  4157. /**
  4158. * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
  4159. * @pf: pointer to PF
  4160. *
  4161. * Get TC map for ISCSI PF type that will include iSCSI TC
  4162. * and LAN TC.
  4163. **/
  4164. static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
  4165. {
  4166. struct i40e_dcb_app_priority_table app;
  4167. struct i40e_hw *hw = &pf->hw;
  4168. u8 enabled_tc = 1; /* TC0 is always enabled */
  4169. u8 tc, i;
  4170. /* Get the iSCSI APP TLV */
  4171. struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
  4172. for (i = 0; i < dcbcfg->numapps; i++) {
  4173. app = dcbcfg->app[i];
  4174. if (app.selector == I40E_APP_SEL_TCPIP &&
  4175. app.protocolid == I40E_APP_PROTOID_ISCSI) {
  4176. tc = dcbcfg->etscfg.prioritytable[app.priority];
  4177. enabled_tc |= BIT(tc);
  4178. break;
  4179. }
  4180. }
  4181. return enabled_tc;
  4182. }
  4183. /**
  4184. * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
  4185. * @dcbcfg: the corresponding DCBx configuration structure
  4186. *
  4187. * Return the number of TCs from given DCBx configuration
  4188. **/
  4189. static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
  4190. {
  4191. int i, tc_unused = 0;
  4192. u8 num_tc = 0;
  4193. u8 ret = 0;
  4194. /* Scan the ETS Config Priority Table to find
  4195. * traffic class enabled for a given priority
  4196. * and create a bitmask of enabled TCs
  4197. */
  4198. for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
  4199. num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
  4200. /* Now scan the bitmask to check for
  4201. * contiguous TCs starting with TC0
  4202. */
  4203. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4204. if (num_tc & BIT(i)) {
  4205. if (!tc_unused) {
  4206. ret++;
  4207. } else {
  4208. pr_err("Non-contiguous TC - Disabling DCB\n");
  4209. return 1;
  4210. }
  4211. } else {
  4212. tc_unused = 1;
  4213. }
  4214. }
  4215. /* There is always at least TC0 */
  4216. if (!ret)
  4217. ret = 1;
  4218. return ret;
  4219. }
  4220. /**
  4221. * i40e_dcb_get_enabled_tc - Get enabled traffic classes
  4222. * @dcbcfg: the corresponding DCBx configuration structure
  4223. *
  4224. * Query the current DCB configuration and return the number of
  4225. * traffic classes enabled from the given DCBX config
  4226. **/
  4227. static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
  4228. {
  4229. u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
  4230. u8 enabled_tc = 1;
  4231. u8 i;
  4232. for (i = 0; i < num_tc; i++)
  4233. enabled_tc |= BIT(i);
  4234. return enabled_tc;
  4235. }
  4236. /**
  4237. * i40e_pf_get_num_tc - Get enabled traffic classes for PF
  4238. * @pf: PF being queried
  4239. *
  4240. * Return number of traffic classes enabled for the given PF
  4241. **/
  4242. static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
  4243. {
  4244. struct i40e_hw *hw = &pf->hw;
  4245. u8 i, enabled_tc = 1;
  4246. u8 num_tc = 0;
  4247. struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
  4248. /* If DCB is not enabled then always in single TC */
  4249. if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
  4250. return 1;
  4251. /* SFP mode will be enabled for all TCs on port */
  4252. if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
  4253. return i40e_dcb_get_num_tc(dcbcfg);
  4254. /* MFP mode return count of enabled TCs for this PF */
  4255. if (pf->hw.func_caps.iscsi)
  4256. enabled_tc = i40e_get_iscsi_tc_map(pf);
  4257. else
  4258. return 1; /* Only TC0 */
  4259. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4260. if (enabled_tc & BIT(i))
  4261. num_tc++;
  4262. }
  4263. return num_tc;
  4264. }
  4265. /**
  4266. * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
  4267. * @pf: PF being queried
  4268. *
  4269. * Return a bitmap for enabled traffic classes for this PF.
  4270. **/
  4271. static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
  4272. {
  4273. /* If DCB is not enabled for this PF then just return default TC */
  4274. if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
  4275. return I40E_DEFAULT_TRAFFIC_CLASS;
  4276. /* SFP mode we want PF to be enabled for all TCs */
  4277. if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
  4278. return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
  4279. /* MFP enabled and iSCSI PF type */
  4280. if (pf->hw.func_caps.iscsi)
  4281. return i40e_get_iscsi_tc_map(pf);
  4282. else
  4283. return I40E_DEFAULT_TRAFFIC_CLASS;
  4284. }
  4285. /**
  4286. * i40e_vsi_get_bw_info - Query VSI BW Information
  4287. * @vsi: the VSI being queried
  4288. *
  4289. * Returns 0 on success, negative value on failure
  4290. **/
  4291. static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
  4292. {
  4293. struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
  4294. struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
  4295. struct i40e_pf *pf = vsi->back;
  4296. struct i40e_hw *hw = &pf->hw;
  4297. i40e_status ret;
  4298. u32 tc_bw_max;
  4299. int i;
  4300. /* Get the VSI level BW configuration */
  4301. ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
  4302. if (ret) {
  4303. dev_info(&pf->pdev->dev,
  4304. "couldn't get PF vsi bw config, err %s aq_err %s\n",
  4305. i40e_stat_str(&pf->hw, ret),
  4306. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4307. return -EINVAL;
  4308. }
  4309. /* Get the VSI level BW configuration per TC */
  4310. ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
  4311. NULL);
  4312. if (ret) {
  4313. dev_info(&pf->pdev->dev,
  4314. "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
  4315. i40e_stat_str(&pf->hw, ret),
  4316. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4317. return -EINVAL;
  4318. }
  4319. if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
  4320. dev_info(&pf->pdev->dev,
  4321. "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
  4322. bw_config.tc_valid_bits,
  4323. bw_ets_config.tc_valid_bits);
  4324. /* Still continuing */
  4325. }
  4326. vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
  4327. vsi->bw_max_quanta = bw_config.max_bw;
  4328. tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
  4329. (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
  4330. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4331. vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
  4332. vsi->bw_ets_limit_credits[i] =
  4333. le16_to_cpu(bw_ets_config.credits[i]);
  4334. /* 3 bits out of 4 for each TC */
  4335. vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
  4336. }
  4337. return 0;
  4338. }
  4339. /**
  4340. * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
  4341. * @vsi: the VSI being configured
  4342. * @enabled_tc: TC bitmap
  4343. * @bw_credits: BW shared credits per TC
  4344. *
  4345. * Returns 0 on success, negative value on failure
  4346. **/
  4347. static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
  4348. u8 *bw_share)
  4349. {
  4350. struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
  4351. i40e_status ret;
  4352. int i;
  4353. bw_data.tc_valid_bits = enabled_tc;
  4354. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
  4355. bw_data.tc_bw_credits[i] = bw_share[i];
  4356. ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
  4357. NULL);
  4358. if (ret) {
  4359. dev_info(&vsi->back->pdev->dev,
  4360. "AQ command Config VSI BW allocation per TC failed = %d\n",
  4361. vsi->back->hw.aq.asq_last_status);
  4362. return -EINVAL;
  4363. }
  4364. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
  4365. vsi->info.qs_handle[i] = bw_data.qs_handles[i];
  4366. return 0;
  4367. }
  4368. /**
  4369. * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
  4370. * @vsi: the VSI being configured
  4371. * @enabled_tc: TC map to be enabled
  4372. *
  4373. **/
  4374. static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
  4375. {
  4376. struct net_device *netdev = vsi->netdev;
  4377. struct i40e_pf *pf = vsi->back;
  4378. struct i40e_hw *hw = &pf->hw;
  4379. u8 netdev_tc = 0;
  4380. int i;
  4381. struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
  4382. if (!netdev)
  4383. return;
  4384. if (!enabled_tc) {
  4385. netdev_reset_tc(netdev);
  4386. return;
  4387. }
  4388. /* Set up actual enabled TCs on the VSI */
  4389. if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
  4390. return;
  4391. /* set per TC queues for the VSI */
  4392. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4393. /* Only set TC queues for enabled tcs
  4394. *
  4395. * e.g. For a VSI that has TC0 and TC3 enabled the
  4396. * enabled_tc bitmap would be 0x00001001; the driver
  4397. * will set the numtc for netdev as 2 that will be
  4398. * referenced by the netdev layer as TC 0 and 1.
  4399. */
  4400. if (vsi->tc_config.enabled_tc & BIT(i))
  4401. netdev_set_tc_queue(netdev,
  4402. vsi->tc_config.tc_info[i].netdev_tc,
  4403. vsi->tc_config.tc_info[i].qcount,
  4404. vsi->tc_config.tc_info[i].qoffset);
  4405. }
  4406. /* Assign UP2TC map for the VSI */
  4407. for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
  4408. /* Get the actual TC# for the UP */
  4409. u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
  4410. /* Get the mapped netdev TC# for the UP */
  4411. netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
  4412. netdev_set_prio_tc_map(netdev, i, netdev_tc);
  4413. }
  4414. }
  4415. /**
  4416. * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
  4417. * @vsi: the VSI being configured
  4418. * @ctxt: the ctxt buffer returned from AQ VSI update param command
  4419. **/
  4420. static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
  4421. struct i40e_vsi_context *ctxt)
  4422. {
  4423. /* copy just the sections touched not the entire info
  4424. * since not all sections are valid as returned by
  4425. * update vsi params
  4426. */
  4427. vsi->info.mapping_flags = ctxt->info.mapping_flags;
  4428. memcpy(&vsi->info.queue_mapping,
  4429. &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
  4430. memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
  4431. sizeof(vsi->info.tc_mapping));
  4432. }
  4433. /**
  4434. * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
  4435. * @vsi: VSI to be configured
  4436. * @enabled_tc: TC bitmap
  4437. *
  4438. * This configures a particular VSI for TCs that are mapped to the
  4439. * given TC bitmap. It uses default bandwidth share for TCs across
  4440. * VSIs to configure TC for a particular VSI.
  4441. *
  4442. * NOTE:
  4443. * It is expected that the VSI queues have been quisced before calling
  4444. * this function.
  4445. **/
  4446. static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
  4447. {
  4448. u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
  4449. struct i40e_vsi_context ctxt;
  4450. int ret = 0;
  4451. int i;
  4452. /* Check if enabled_tc is same as existing or new TCs */
  4453. if (vsi->tc_config.enabled_tc == enabled_tc)
  4454. return ret;
  4455. /* Enable ETS TCs with equal BW Share for now across all VSIs */
  4456. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4457. if (enabled_tc & BIT(i))
  4458. bw_share[i] = 1;
  4459. }
  4460. ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
  4461. if (ret) {
  4462. dev_info(&vsi->back->pdev->dev,
  4463. "Failed configuring TC map %d for VSI %d\n",
  4464. enabled_tc, vsi->seid);
  4465. goto out;
  4466. }
  4467. /* Update Queue Pairs Mapping for currently enabled UPs */
  4468. ctxt.seid = vsi->seid;
  4469. ctxt.pf_num = vsi->back->hw.pf_id;
  4470. ctxt.vf_num = 0;
  4471. ctxt.uplink_seid = vsi->uplink_seid;
  4472. ctxt.info = vsi->info;
  4473. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
  4474. if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
  4475. ctxt.info.valid_sections |=
  4476. cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
  4477. ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
  4478. }
  4479. /* Update the VSI after updating the VSI queue-mapping information */
  4480. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  4481. if (ret) {
  4482. dev_info(&vsi->back->pdev->dev,
  4483. "Update vsi tc config failed, err %s aq_err %s\n",
  4484. i40e_stat_str(&vsi->back->hw, ret),
  4485. i40e_aq_str(&vsi->back->hw,
  4486. vsi->back->hw.aq.asq_last_status));
  4487. goto out;
  4488. }
  4489. /* update the local VSI info with updated queue map */
  4490. i40e_vsi_update_queue_map(vsi, &ctxt);
  4491. vsi->info.valid_sections = 0;
  4492. /* Update current VSI BW information */
  4493. ret = i40e_vsi_get_bw_info(vsi);
  4494. if (ret) {
  4495. dev_info(&vsi->back->pdev->dev,
  4496. "Failed updating vsi bw info, err %s aq_err %s\n",
  4497. i40e_stat_str(&vsi->back->hw, ret),
  4498. i40e_aq_str(&vsi->back->hw,
  4499. vsi->back->hw.aq.asq_last_status));
  4500. goto out;
  4501. }
  4502. /* Update the netdev TC setup */
  4503. i40e_vsi_config_netdev_tc(vsi, enabled_tc);
  4504. out:
  4505. return ret;
  4506. }
  4507. /**
  4508. * i40e_veb_config_tc - Configure TCs for given VEB
  4509. * @veb: given VEB
  4510. * @enabled_tc: TC bitmap
  4511. *
  4512. * Configures given TC bitmap for VEB (switching) element
  4513. **/
  4514. int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
  4515. {
  4516. struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
  4517. struct i40e_pf *pf = veb->pf;
  4518. int ret = 0;
  4519. int i;
  4520. /* No TCs or already enabled TCs just return */
  4521. if (!enabled_tc || veb->enabled_tc == enabled_tc)
  4522. return ret;
  4523. bw_data.tc_valid_bits = enabled_tc;
  4524. /* bw_data.absolute_credits is not set (relative) */
  4525. /* Enable ETS TCs with equal BW Share for now */
  4526. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4527. if (enabled_tc & BIT(i))
  4528. bw_data.tc_bw_share_credits[i] = 1;
  4529. }
  4530. ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
  4531. &bw_data, NULL);
  4532. if (ret) {
  4533. dev_info(&pf->pdev->dev,
  4534. "VEB bw config failed, err %s aq_err %s\n",
  4535. i40e_stat_str(&pf->hw, ret),
  4536. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4537. goto out;
  4538. }
  4539. /* Update the BW information */
  4540. ret = i40e_veb_get_bw_info(veb);
  4541. if (ret) {
  4542. dev_info(&pf->pdev->dev,
  4543. "Failed getting veb bw config, err %s aq_err %s\n",
  4544. i40e_stat_str(&pf->hw, ret),
  4545. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4546. }
  4547. out:
  4548. return ret;
  4549. }
  4550. #ifdef CONFIG_I40E_DCB
  4551. /**
  4552. * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
  4553. * @pf: PF struct
  4554. *
  4555. * Reconfigure VEB/VSIs on a given PF; it is assumed that
  4556. * the caller would've quiesce all the VSIs before calling
  4557. * this function
  4558. **/
  4559. static void i40e_dcb_reconfigure(struct i40e_pf *pf)
  4560. {
  4561. u8 tc_map = 0;
  4562. int ret;
  4563. u8 v;
  4564. /* Enable the TCs available on PF to all VEBs */
  4565. tc_map = i40e_pf_get_tc_map(pf);
  4566. for (v = 0; v < I40E_MAX_VEB; v++) {
  4567. if (!pf->veb[v])
  4568. continue;
  4569. ret = i40e_veb_config_tc(pf->veb[v], tc_map);
  4570. if (ret) {
  4571. dev_info(&pf->pdev->dev,
  4572. "Failed configuring TC for VEB seid=%d\n",
  4573. pf->veb[v]->seid);
  4574. /* Will try to configure as many components */
  4575. }
  4576. }
  4577. /* Update each VSI */
  4578. for (v = 0; v < pf->num_alloc_vsi; v++) {
  4579. if (!pf->vsi[v])
  4580. continue;
  4581. /* - Enable all TCs for the LAN VSI
  4582. * - For all others keep them at TC0 for now
  4583. */
  4584. if (v == pf->lan_vsi)
  4585. tc_map = i40e_pf_get_tc_map(pf);
  4586. else
  4587. tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
  4588. ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
  4589. if (ret) {
  4590. dev_info(&pf->pdev->dev,
  4591. "Failed configuring TC for VSI seid=%d\n",
  4592. pf->vsi[v]->seid);
  4593. /* Will try to configure as many components */
  4594. } else {
  4595. /* Re-configure VSI vectors based on updated TC map */
  4596. i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
  4597. if (pf->vsi[v]->netdev)
  4598. i40e_dcbnl_set_all(pf->vsi[v]);
  4599. }
  4600. }
  4601. }
  4602. /**
  4603. * i40e_resume_port_tx - Resume port Tx
  4604. * @pf: PF struct
  4605. *
  4606. * Resume a port's Tx and issue a PF reset in case of failure to
  4607. * resume.
  4608. **/
  4609. static int i40e_resume_port_tx(struct i40e_pf *pf)
  4610. {
  4611. struct i40e_hw *hw = &pf->hw;
  4612. int ret;
  4613. ret = i40e_aq_resume_port_tx(hw, NULL);
  4614. if (ret) {
  4615. dev_info(&pf->pdev->dev,
  4616. "Resume Port Tx failed, err %s aq_err %s\n",
  4617. i40e_stat_str(&pf->hw, ret),
  4618. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4619. /* Schedule PF reset to recover */
  4620. set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
  4621. i40e_service_event_schedule(pf);
  4622. }
  4623. return ret;
  4624. }
  4625. /**
  4626. * i40e_init_pf_dcb - Initialize DCB configuration
  4627. * @pf: PF being configured
  4628. *
  4629. * Query the current DCB configuration and cache it
  4630. * in the hardware structure
  4631. **/
  4632. static int i40e_init_pf_dcb(struct i40e_pf *pf)
  4633. {
  4634. struct i40e_hw *hw = &pf->hw;
  4635. int err = 0;
  4636. /* Do not enable DCB for SW1 and SW2 images even if the FW is capable */
  4637. if (pf->hw_features & I40E_HW_NO_DCB_SUPPORT)
  4638. goto out;
  4639. /* Get the initial DCB configuration */
  4640. err = i40e_init_dcb(hw);
  4641. if (!err) {
  4642. /* Device/Function is not DCBX capable */
  4643. if ((!hw->func_caps.dcb) ||
  4644. (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
  4645. dev_info(&pf->pdev->dev,
  4646. "DCBX offload is not supported or is disabled for this PF.\n");
  4647. } else {
  4648. /* When status is not DISABLED then DCBX in FW */
  4649. pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
  4650. DCB_CAP_DCBX_VER_IEEE;
  4651. pf->flags |= I40E_FLAG_DCB_CAPABLE;
  4652. /* Enable DCB tagging only when more than one TC
  4653. * or explicitly disable if only one TC
  4654. */
  4655. if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
  4656. pf->flags |= I40E_FLAG_DCB_ENABLED;
  4657. else
  4658. pf->flags &= ~I40E_FLAG_DCB_ENABLED;
  4659. dev_dbg(&pf->pdev->dev,
  4660. "DCBX offload is supported for this PF.\n");
  4661. }
  4662. } else {
  4663. dev_info(&pf->pdev->dev,
  4664. "Query for DCB configuration failed, err %s aq_err %s\n",
  4665. i40e_stat_str(&pf->hw, err),
  4666. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4667. }
  4668. out:
  4669. return err;
  4670. }
  4671. #endif /* CONFIG_I40E_DCB */
  4672. #define SPEED_SIZE 14
  4673. #define FC_SIZE 8
  4674. /**
  4675. * i40e_print_link_message - print link up or down
  4676. * @vsi: the VSI for which link needs a message
  4677. */
  4678. void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
  4679. {
  4680. enum i40e_aq_link_speed new_speed;
  4681. char *speed = "Unknown";
  4682. char *fc = "Unknown";
  4683. char *fec = "";
  4684. char *req_fec = "";
  4685. char *an = "";
  4686. new_speed = vsi->back->hw.phy.link_info.link_speed;
  4687. if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
  4688. return;
  4689. vsi->current_isup = isup;
  4690. vsi->current_speed = new_speed;
  4691. if (!isup) {
  4692. netdev_info(vsi->netdev, "NIC Link is Down\n");
  4693. return;
  4694. }
  4695. /* Warn user if link speed on NPAR enabled partition is not at
  4696. * least 10GB
  4697. */
  4698. if (vsi->back->hw.func_caps.npar_enable &&
  4699. (vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
  4700. vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
  4701. netdev_warn(vsi->netdev,
  4702. "The partition detected link speed that is less than 10Gbps\n");
  4703. switch (vsi->back->hw.phy.link_info.link_speed) {
  4704. case I40E_LINK_SPEED_40GB:
  4705. speed = "40 G";
  4706. break;
  4707. case I40E_LINK_SPEED_20GB:
  4708. speed = "20 G";
  4709. break;
  4710. case I40E_LINK_SPEED_25GB:
  4711. speed = "25 G";
  4712. break;
  4713. case I40E_LINK_SPEED_10GB:
  4714. speed = "10 G";
  4715. break;
  4716. case I40E_LINK_SPEED_1GB:
  4717. speed = "1000 M";
  4718. break;
  4719. case I40E_LINK_SPEED_100MB:
  4720. speed = "100 M";
  4721. break;
  4722. default:
  4723. break;
  4724. }
  4725. switch (vsi->back->hw.fc.current_mode) {
  4726. case I40E_FC_FULL:
  4727. fc = "RX/TX";
  4728. break;
  4729. case I40E_FC_TX_PAUSE:
  4730. fc = "TX";
  4731. break;
  4732. case I40E_FC_RX_PAUSE:
  4733. fc = "RX";
  4734. break;
  4735. default:
  4736. fc = "None";
  4737. break;
  4738. }
  4739. if (vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
  4740. req_fec = ", Requested FEC: None";
  4741. fec = ", FEC: None";
  4742. an = ", Autoneg: False";
  4743. if (vsi->back->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
  4744. an = ", Autoneg: True";
  4745. if (vsi->back->hw.phy.link_info.fec_info &
  4746. I40E_AQ_CONFIG_FEC_KR_ENA)
  4747. fec = ", FEC: CL74 FC-FEC/BASE-R";
  4748. else if (vsi->back->hw.phy.link_info.fec_info &
  4749. I40E_AQ_CONFIG_FEC_RS_ENA)
  4750. fec = ", FEC: CL108 RS-FEC";
  4751. /* 'CL108 RS-FEC' should be displayed when RS is requested, or
  4752. * both RS and FC are requested
  4753. */
  4754. if (vsi->back->hw.phy.link_info.req_fec_info &
  4755. (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
  4756. if (vsi->back->hw.phy.link_info.req_fec_info &
  4757. I40E_AQ_REQUEST_FEC_RS)
  4758. req_fec = ", Requested FEC: CL108 RS-FEC";
  4759. else
  4760. req_fec = ", Requested FEC: CL74 FC-FEC/BASE-R";
  4761. }
  4762. }
  4763. netdev_info(vsi->netdev, "NIC Link is Up, %sbps Full Duplex%s%s%s, Flow Control: %s\n",
  4764. speed, req_fec, fec, an, fc);
  4765. }
  4766. /**
  4767. * i40e_up_complete - Finish the last steps of bringing up a connection
  4768. * @vsi: the VSI being configured
  4769. **/
  4770. static int i40e_up_complete(struct i40e_vsi *vsi)
  4771. {
  4772. struct i40e_pf *pf = vsi->back;
  4773. int err;
  4774. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  4775. i40e_vsi_configure_msix(vsi);
  4776. else
  4777. i40e_configure_msi_and_legacy(vsi);
  4778. /* start rings */
  4779. err = i40e_vsi_start_rings(vsi);
  4780. if (err)
  4781. return err;
  4782. clear_bit(__I40E_VSI_DOWN, vsi->state);
  4783. i40e_napi_enable_all(vsi);
  4784. i40e_vsi_enable_irq(vsi);
  4785. if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
  4786. (vsi->netdev)) {
  4787. i40e_print_link_message(vsi, true);
  4788. netif_tx_start_all_queues(vsi->netdev);
  4789. netif_carrier_on(vsi->netdev);
  4790. } else if (vsi->netdev) {
  4791. i40e_print_link_message(vsi, false);
  4792. /* need to check for qualified module here*/
  4793. if ((pf->hw.phy.link_info.link_info &
  4794. I40E_AQ_MEDIA_AVAILABLE) &&
  4795. (!(pf->hw.phy.link_info.an_info &
  4796. I40E_AQ_QUALIFIED_MODULE)))
  4797. netdev_err(vsi->netdev,
  4798. "the driver failed to link because an unqualified module was detected.");
  4799. }
  4800. /* replay FDIR SB filters */
  4801. if (vsi->type == I40E_VSI_FDIR) {
  4802. /* reset fd counters */
  4803. pf->fd_add_err = 0;
  4804. pf->fd_atr_cnt = 0;
  4805. i40e_fdir_filter_restore(vsi);
  4806. }
  4807. /* On the next run of the service_task, notify any clients of the new
  4808. * opened netdev
  4809. */
  4810. pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
  4811. i40e_service_event_schedule(pf);
  4812. return 0;
  4813. }
  4814. /**
  4815. * i40e_vsi_reinit_locked - Reset the VSI
  4816. * @vsi: the VSI being configured
  4817. *
  4818. * Rebuild the ring structs after some configuration
  4819. * has changed, e.g. MTU size.
  4820. **/
  4821. static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
  4822. {
  4823. struct i40e_pf *pf = vsi->back;
  4824. WARN_ON(in_interrupt());
  4825. while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
  4826. usleep_range(1000, 2000);
  4827. i40e_down(vsi);
  4828. i40e_up(vsi);
  4829. clear_bit(__I40E_CONFIG_BUSY, pf->state);
  4830. }
  4831. /**
  4832. * i40e_up - Bring the connection back up after being down
  4833. * @vsi: the VSI being configured
  4834. **/
  4835. int i40e_up(struct i40e_vsi *vsi)
  4836. {
  4837. int err;
  4838. err = i40e_vsi_configure(vsi);
  4839. if (!err)
  4840. err = i40e_up_complete(vsi);
  4841. return err;
  4842. }
  4843. /**
  4844. * i40e_down - Shutdown the connection processing
  4845. * @vsi: the VSI being stopped
  4846. **/
  4847. void i40e_down(struct i40e_vsi *vsi)
  4848. {
  4849. int i;
  4850. /* It is assumed that the caller of this function
  4851. * sets the vsi->state __I40E_VSI_DOWN bit.
  4852. */
  4853. if (vsi->netdev) {
  4854. netif_carrier_off(vsi->netdev);
  4855. netif_tx_disable(vsi->netdev);
  4856. }
  4857. i40e_vsi_disable_irq(vsi);
  4858. i40e_vsi_stop_rings(vsi);
  4859. i40e_napi_disable_all(vsi);
  4860. for (i = 0; i < vsi->num_queue_pairs; i++) {
  4861. i40e_clean_tx_ring(vsi->tx_rings[i]);
  4862. if (i40e_enabled_xdp_vsi(vsi))
  4863. i40e_clean_tx_ring(vsi->xdp_rings[i]);
  4864. i40e_clean_rx_ring(vsi->rx_rings[i]);
  4865. }
  4866. }
  4867. /**
  4868. * i40e_setup_tc - configure multiple traffic classes
  4869. * @netdev: net device to configure
  4870. * @tc: number of traffic classes to enable
  4871. **/
  4872. static int i40e_setup_tc(struct net_device *netdev, u8 tc)
  4873. {
  4874. struct i40e_netdev_priv *np = netdev_priv(netdev);
  4875. struct i40e_vsi *vsi = np->vsi;
  4876. struct i40e_pf *pf = vsi->back;
  4877. u8 enabled_tc = 0;
  4878. int ret = -EINVAL;
  4879. int i;
  4880. /* Check if DCB enabled to continue */
  4881. if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
  4882. netdev_info(netdev, "DCB is not enabled for adapter\n");
  4883. goto exit;
  4884. }
  4885. /* Check if MFP enabled */
  4886. if (pf->flags & I40E_FLAG_MFP_ENABLED) {
  4887. netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
  4888. goto exit;
  4889. }
  4890. /* Check whether tc count is within enabled limit */
  4891. if (tc > i40e_pf_get_num_tc(pf)) {
  4892. netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
  4893. goto exit;
  4894. }
  4895. /* Generate TC map for number of tc requested */
  4896. for (i = 0; i < tc; i++)
  4897. enabled_tc |= BIT(i);
  4898. /* Requesting same TC configuration as already enabled */
  4899. if (enabled_tc == vsi->tc_config.enabled_tc)
  4900. return 0;
  4901. /* Quiesce VSI queues */
  4902. i40e_quiesce_vsi(vsi);
  4903. /* Configure VSI for enabled TCs */
  4904. ret = i40e_vsi_config_tc(vsi, enabled_tc);
  4905. if (ret) {
  4906. netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
  4907. vsi->seid);
  4908. goto exit;
  4909. }
  4910. /* Unquiesce VSI */
  4911. i40e_unquiesce_vsi(vsi);
  4912. exit:
  4913. return ret;
  4914. }
  4915. static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
  4916. void *type_data)
  4917. {
  4918. struct tc_mqprio_qopt *mqprio = type_data;
  4919. if (type != TC_SETUP_MQPRIO)
  4920. return -EOPNOTSUPP;
  4921. mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
  4922. return i40e_setup_tc(netdev, mqprio->num_tc);
  4923. }
  4924. /**
  4925. * i40e_open - Called when a network interface is made active
  4926. * @netdev: network interface device structure
  4927. *
  4928. * The open entry point is called when a network interface is made
  4929. * active by the system (IFF_UP). At this point all resources needed
  4930. * for transmit and receive operations are allocated, the interrupt
  4931. * handler is registered with the OS, the netdev watchdog subtask is
  4932. * enabled, and the stack is notified that the interface is ready.
  4933. *
  4934. * Returns 0 on success, negative value on failure
  4935. **/
  4936. int i40e_open(struct net_device *netdev)
  4937. {
  4938. struct i40e_netdev_priv *np = netdev_priv(netdev);
  4939. struct i40e_vsi *vsi = np->vsi;
  4940. struct i40e_pf *pf = vsi->back;
  4941. int err;
  4942. /* disallow open during test or if eeprom is broken */
  4943. if (test_bit(__I40E_TESTING, pf->state) ||
  4944. test_bit(__I40E_BAD_EEPROM, pf->state))
  4945. return -EBUSY;
  4946. netif_carrier_off(netdev);
  4947. err = i40e_vsi_open(vsi);
  4948. if (err)
  4949. return err;
  4950. /* configure global TSO hardware offload settings */
  4951. wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
  4952. TCP_FLAG_FIN) >> 16);
  4953. wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
  4954. TCP_FLAG_FIN |
  4955. TCP_FLAG_CWR) >> 16);
  4956. wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
  4957. udp_tunnel_get_rx_info(netdev);
  4958. return 0;
  4959. }
  4960. /**
  4961. * i40e_vsi_open -
  4962. * @vsi: the VSI to open
  4963. *
  4964. * Finish initialization of the VSI.
  4965. *
  4966. * Returns 0 on success, negative value on failure
  4967. *
  4968. * Note: expects to be called while under rtnl_lock()
  4969. **/
  4970. int i40e_vsi_open(struct i40e_vsi *vsi)
  4971. {
  4972. struct i40e_pf *pf = vsi->back;
  4973. char int_name[I40E_INT_NAME_STR_LEN];
  4974. int err;
  4975. /* allocate descriptors */
  4976. err = i40e_vsi_setup_tx_resources(vsi);
  4977. if (err)
  4978. goto err_setup_tx;
  4979. err = i40e_vsi_setup_rx_resources(vsi);
  4980. if (err)
  4981. goto err_setup_rx;
  4982. err = i40e_vsi_configure(vsi);
  4983. if (err)
  4984. goto err_setup_rx;
  4985. if (vsi->netdev) {
  4986. snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
  4987. dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
  4988. err = i40e_vsi_request_irq(vsi, int_name);
  4989. if (err)
  4990. goto err_setup_rx;
  4991. /* Notify the stack of the actual queue counts. */
  4992. err = netif_set_real_num_tx_queues(vsi->netdev,
  4993. vsi->num_queue_pairs);
  4994. if (err)
  4995. goto err_set_queues;
  4996. err = netif_set_real_num_rx_queues(vsi->netdev,
  4997. vsi->num_queue_pairs);
  4998. if (err)
  4999. goto err_set_queues;
  5000. } else if (vsi->type == I40E_VSI_FDIR) {
  5001. snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
  5002. dev_driver_string(&pf->pdev->dev),
  5003. dev_name(&pf->pdev->dev));
  5004. err = i40e_vsi_request_irq(vsi, int_name);
  5005. } else {
  5006. err = -EINVAL;
  5007. goto err_setup_rx;
  5008. }
  5009. err = i40e_up_complete(vsi);
  5010. if (err)
  5011. goto err_up_complete;
  5012. return 0;
  5013. err_up_complete:
  5014. i40e_down(vsi);
  5015. err_set_queues:
  5016. i40e_vsi_free_irq(vsi);
  5017. err_setup_rx:
  5018. i40e_vsi_free_rx_resources(vsi);
  5019. err_setup_tx:
  5020. i40e_vsi_free_tx_resources(vsi);
  5021. if (vsi == pf->vsi[pf->lan_vsi])
  5022. i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED), true);
  5023. return err;
  5024. }
  5025. /**
  5026. * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
  5027. * @pf: Pointer to PF
  5028. *
  5029. * This function destroys the hlist where all the Flow Director
  5030. * filters were saved.
  5031. **/
  5032. static void i40e_fdir_filter_exit(struct i40e_pf *pf)
  5033. {
  5034. struct i40e_fdir_filter *filter;
  5035. struct i40e_flex_pit *pit_entry, *tmp;
  5036. struct hlist_node *node2;
  5037. hlist_for_each_entry_safe(filter, node2,
  5038. &pf->fdir_filter_list, fdir_node) {
  5039. hlist_del(&filter->fdir_node);
  5040. kfree(filter);
  5041. }
  5042. list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
  5043. list_del(&pit_entry->list);
  5044. kfree(pit_entry);
  5045. }
  5046. INIT_LIST_HEAD(&pf->l3_flex_pit_list);
  5047. list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
  5048. list_del(&pit_entry->list);
  5049. kfree(pit_entry);
  5050. }
  5051. INIT_LIST_HEAD(&pf->l4_flex_pit_list);
  5052. pf->fdir_pf_active_filters = 0;
  5053. pf->fd_tcp4_filter_cnt = 0;
  5054. pf->fd_udp4_filter_cnt = 0;
  5055. pf->fd_sctp4_filter_cnt = 0;
  5056. pf->fd_ip4_filter_cnt = 0;
  5057. /* Reprogram the default input set for TCP/IPv4 */
  5058. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
  5059. I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
  5060. I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
  5061. /* Reprogram the default input set for UDP/IPv4 */
  5062. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
  5063. I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
  5064. I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
  5065. /* Reprogram the default input set for SCTP/IPv4 */
  5066. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
  5067. I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
  5068. I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
  5069. /* Reprogram the default input set for Other/IPv4 */
  5070. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
  5071. I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
  5072. }
  5073. /**
  5074. * i40e_close - Disables a network interface
  5075. * @netdev: network interface device structure
  5076. *
  5077. * The close entry point is called when an interface is de-activated
  5078. * by the OS. The hardware is still under the driver's control, but
  5079. * this netdev interface is disabled.
  5080. *
  5081. * Returns 0, this is not allowed to fail
  5082. **/
  5083. int i40e_close(struct net_device *netdev)
  5084. {
  5085. struct i40e_netdev_priv *np = netdev_priv(netdev);
  5086. struct i40e_vsi *vsi = np->vsi;
  5087. i40e_vsi_close(vsi);
  5088. return 0;
  5089. }
  5090. /**
  5091. * i40e_do_reset - Start a PF or Core Reset sequence
  5092. * @pf: board private structure
  5093. * @reset_flags: which reset is requested
  5094. * @lock_acquired: indicates whether or not the lock has been acquired
  5095. * before this function was called.
  5096. *
  5097. * The essential difference in resets is that the PF Reset
  5098. * doesn't clear the packet buffers, doesn't reset the PE
  5099. * firmware, and doesn't bother the other PFs on the chip.
  5100. **/
  5101. void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
  5102. {
  5103. u32 val;
  5104. WARN_ON(in_interrupt());
  5105. /* do the biggest reset indicated */
  5106. if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
  5107. /* Request a Global Reset
  5108. *
  5109. * This will start the chip's countdown to the actual full
  5110. * chip reset event, and a warning interrupt to be sent
  5111. * to all PFs, including the requestor. Our handler
  5112. * for the warning interrupt will deal with the shutdown
  5113. * and recovery of the switch setup.
  5114. */
  5115. dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
  5116. val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  5117. val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
  5118. wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
  5119. } else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
  5120. /* Request a Core Reset
  5121. *
  5122. * Same as Global Reset, except does *not* include the MAC/PHY
  5123. */
  5124. dev_dbg(&pf->pdev->dev, "CoreR requested\n");
  5125. val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  5126. val |= I40E_GLGEN_RTRIG_CORER_MASK;
  5127. wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
  5128. i40e_flush(&pf->hw);
  5129. } else if (reset_flags & BIT_ULL(__I40E_PF_RESET_REQUESTED)) {
  5130. /* Request a PF Reset
  5131. *
  5132. * Resets only the PF-specific registers
  5133. *
  5134. * This goes directly to the tear-down and rebuild of
  5135. * the switch, since we need to do all the recovery as
  5136. * for the Core Reset.
  5137. */
  5138. dev_dbg(&pf->pdev->dev, "PFR requested\n");
  5139. i40e_handle_reset_warning(pf, lock_acquired);
  5140. } else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
  5141. int v;
  5142. /* Find the VSI(s) that requested a re-init */
  5143. dev_info(&pf->pdev->dev,
  5144. "VSI reinit requested\n");
  5145. for (v = 0; v < pf->num_alloc_vsi; v++) {
  5146. struct i40e_vsi *vsi = pf->vsi[v];
  5147. if (vsi != NULL &&
  5148. test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
  5149. vsi->state))
  5150. i40e_vsi_reinit_locked(pf->vsi[v]);
  5151. }
  5152. } else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
  5153. int v;
  5154. /* Find the VSI(s) that needs to be brought down */
  5155. dev_info(&pf->pdev->dev, "VSI down requested\n");
  5156. for (v = 0; v < pf->num_alloc_vsi; v++) {
  5157. struct i40e_vsi *vsi = pf->vsi[v];
  5158. if (vsi != NULL &&
  5159. test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
  5160. vsi->state)) {
  5161. set_bit(__I40E_VSI_DOWN, vsi->state);
  5162. i40e_down(vsi);
  5163. }
  5164. }
  5165. } else {
  5166. dev_info(&pf->pdev->dev,
  5167. "bad reset request 0x%08x\n", reset_flags);
  5168. }
  5169. }
  5170. #ifdef CONFIG_I40E_DCB
  5171. /**
  5172. * i40e_dcb_need_reconfig - Check if DCB needs reconfig
  5173. * @pf: board private structure
  5174. * @old_cfg: current DCB config
  5175. * @new_cfg: new DCB config
  5176. **/
  5177. bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
  5178. struct i40e_dcbx_config *old_cfg,
  5179. struct i40e_dcbx_config *new_cfg)
  5180. {
  5181. bool need_reconfig = false;
  5182. /* Check if ETS configuration has changed */
  5183. if (memcmp(&new_cfg->etscfg,
  5184. &old_cfg->etscfg,
  5185. sizeof(new_cfg->etscfg))) {
  5186. /* If Priority Table has changed reconfig is needed */
  5187. if (memcmp(&new_cfg->etscfg.prioritytable,
  5188. &old_cfg->etscfg.prioritytable,
  5189. sizeof(new_cfg->etscfg.prioritytable))) {
  5190. need_reconfig = true;
  5191. dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
  5192. }
  5193. if (memcmp(&new_cfg->etscfg.tcbwtable,
  5194. &old_cfg->etscfg.tcbwtable,
  5195. sizeof(new_cfg->etscfg.tcbwtable)))
  5196. dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
  5197. if (memcmp(&new_cfg->etscfg.tsatable,
  5198. &old_cfg->etscfg.tsatable,
  5199. sizeof(new_cfg->etscfg.tsatable)))
  5200. dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
  5201. }
  5202. /* Check if PFC configuration has changed */
  5203. if (memcmp(&new_cfg->pfc,
  5204. &old_cfg->pfc,
  5205. sizeof(new_cfg->pfc))) {
  5206. need_reconfig = true;
  5207. dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
  5208. }
  5209. /* Check if APP Table has changed */
  5210. if (memcmp(&new_cfg->app,
  5211. &old_cfg->app,
  5212. sizeof(new_cfg->app))) {
  5213. need_reconfig = true;
  5214. dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
  5215. }
  5216. dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
  5217. return need_reconfig;
  5218. }
  5219. /**
  5220. * i40e_handle_lldp_event - Handle LLDP Change MIB event
  5221. * @pf: board private structure
  5222. * @e: event info posted on ARQ
  5223. **/
  5224. static int i40e_handle_lldp_event(struct i40e_pf *pf,
  5225. struct i40e_arq_event_info *e)
  5226. {
  5227. struct i40e_aqc_lldp_get_mib *mib =
  5228. (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
  5229. struct i40e_hw *hw = &pf->hw;
  5230. struct i40e_dcbx_config tmp_dcbx_cfg;
  5231. bool need_reconfig = false;
  5232. int ret = 0;
  5233. u8 type;
  5234. /* Not DCB capable or capability disabled */
  5235. if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
  5236. return ret;
  5237. /* Ignore if event is not for Nearest Bridge */
  5238. type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
  5239. & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
  5240. dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
  5241. if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
  5242. return ret;
  5243. /* Check MIB Type and return if event for Remote MIB update */
  5244. type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
  5245. dev_dbg(&pf->pdev->dev,
  5246. "LLDP event mib type %s\n", type ? "remote" : "local");
  5247. if (type == I40E_AQ_LLDP_MIB_REMOTE) {
  5248. /* Update the remote cached instance and return */
  5249. ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
  5250. I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
  5251. &hw->remote_dcbx_config);
  5252. goto exit;
  5253. }
  5254. /* Store the old configuration */
  5255. tmp_dcbx_cfg = hw->local_dcbx_config;
  5256. /* Reset the old DCBx configuration data */
  5257. memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
  5258. /* Get updated DCBX data from firmware */
  5259. ret = i40e_get_dcb_config(&pf->hw);
  5260. if (ret) {
  5261. dev_info(&pf->pdev->dev,
  5262. "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
  5263. i40e_stat_str(&pf->hw, ret),
  5264. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5265. goto exit;
  5266. }
  5267. /* No change detected in DCBX configs */
  5268. if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
  5269. sizeof(tmp_dcbx_cfg))) {
  5270. dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
  5271. goto exit;
  5272. }
  5273. need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
  5274. &hw->local_dcbx_config);
  5275. i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
  5276. if (!need_reconfig)
  5277. goto exit;
  5278. /* Enable DCB tagging only when more than one TC */
  5279. if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
  5280. pf->flags |= I40E_FLAG_DCB_ENABLED;
  5281. else
  5282. pf->flags &= ~I40E_FLAG_DCB_ENABLED;
  5283. set_bit(__I40E_PORT_SUSPENDED, pf->state);
  5284. /* Reconfiguration needed quiesce all VSIs */
  5285. i40e_pf_quiesce_all_vsi(pf);
  5286. /* Changes in configuration update VEB/VSI */
  5287. i40e_dcb_reconfigure(pf);
  5288. ret = i40e_resume_port_tx(pf);
  5289. clear_bit(__I40E_PORT_SUSPENDED, pf->state);
  5290. /* In case of error no point in resuming VSIs */
  5291. if (ret)
  5292. goto exit;
  5293. /* Wait for the PF's queues to be disabled */
  5294. ret = i40e_pf_wait_queues_disabled(pf);
  5295. if (ret) {
  5296. /* Schedule PF reset to recover */
  5297. set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
  5298. i40e_service_event_schedule(pf);
  5299. } else {
  5300. i40e_pf_unquiesce_all_vsi(pf);
  5301. pf->flags |= (I40E_FLAG_SERVICE_CLIENT_REQUESTED |
  5302. I40E_FLAG_CLIENT_L2_CHANGE);
  5303. }
  5304. exit:
  5305. return ret;
  5306. }
  5307. #endif /* CONFIG_I40E_DCB */
  5308. /**
  5309. * i40e_do_reset_safe - Protected reset path for userland calls.
  5310. * @pf: board private structure
  5311. * @reset_flags: which reset is requested
  5312. *
  5313. **/
  5314. void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
  5315. {
  5316. rtnl_lock();
  5317. i40e_do_reset(pf, reset_flags, true);
  5318. rtnl_unlock();
  5319. }
  5320. /**
  5321. * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
  5322. * @pf: board private structure
  5323. * @e: event info posted on ARQ
  5324. *
  5325. * Handler for LAN Queue Overflow Event generated by the firmware for PF
  5326. * and VF queues
  5327. **/
  5328. static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
  5329. struct i40e_arq_event_info *e)
  5330. {
  5331. struct i40e_aqc_lan_overflow *data =
  5332. (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
  5333. u32 queue = le32_to_cpu(data->prtdcb_rupto);
  5334. u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
  5335. struct i40e_hw *hw = &pf->hw;
  5336. struct i40e_vf *vf;
  5337. u16 vf_id;
  5338. dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
  5339. queue, qtx_ctl);
  5340. /* Queue belongs to VF, find the VF and issue VF reset */
  5341. if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
  5342. >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
  5343. vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
  5344. >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
  5345. vf_id -= hw->func_caps.vf_base_id;
  5346. vf = &pf->vf[vf_id];
  5347. i40e_vc_notify_vf_reset(vf);
  5348. /* Allow VF to process pending reset notification */
  5349. msleep(20);
  5350. i40e_reset_vf(vf, false);
  5351. }
  5352. }
  5353. /**
  5354. * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
  5355. * @pf: board private structure
  5356. **/
  5357. u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
  5358. {
  5359. u32 val, fcnt_prog;
  5360. val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
  5361. fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
  5362. return fcnt_prog;
  5363. }
  5364. /**
  5365. * i40e_get_current_fd_count - Get total FD filters programmed for this PF
  5366. * @pf: board private structure
  5367. **/
  5368. u32 i40e_get_current_fd_count(struct i40e_pf *pf)
  5369. {
  5370. u32 val, fcnt_prog;
  5371. val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
  5372. fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
  5373. ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
  5374. I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
  5375. return fcnt_prog;
  5376. }
  5377. /**
  5378. * i40e_get_global_fd_count - Get total FD filters programmed on device
  5379. * @pf: board private structure
  5380. **/
  5381. u32 i40e_get_global_fd_count(struct i40e_pf *pf)
  5382. {
  5383. u32 val, fcnt_prog;
  5384. val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
  5385. fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
  5386. ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
  5387. I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
  5388. return fcnt_prog;
  5389. }
  5390. /**
  5391. * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
  5392. * @pf: board private structure
  5393. **/
  5394. void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
  5395. {
  5396. struct i40e_fdir_filter *filter;
  5397. u32 fcnt_prog, fcnt_avail;
  5398. struct hlist_node *node;
  5399. if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
  5400. return;
  5401. /* Check if we have enough room to re-enable FDir SB capability. */
  5402. fcnt_prog = i40e_get_global_fd_count(pf);
  5403. fcnt_avail = pf->fdir_pf_filter_count;
  5404. if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
  5405. (pf->fd_add_err == 0) ||
  5406. (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt)) {
  5407. if (pf->flags & I40E_FLAG_FD_SB_AUTO_DISABLED) {
  5408. pf->flags &= ~I40E_FLAG_FD_SB_AUTO_DISABLED;
  5409. if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
  5410. (I40E_DEBUG_FD & pf->hw.debug_mask))
  5411. dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
  5412. }
  5413. }
  5414. /* We should wait for even more space before re-enabling ATR.
  5415. * Additionally, we cannot enable ATR as long as we still have TCP SB
  5416. * rules active.
  5417. */
  5418. if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
  5419. (pf->fd_tcp4_filter_cnt == 0)) {
  5420. if (pf->flags & I40E_FLAG_FD_ATR_AUTO_DISABLED) {
  5421. pf->flags &= ~I40E_FLAG_FD_ATR_AUTO_DISABLED;
  5422. if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
  5423. (I40E_DEBUG_FD & pf->hw.debug_mask))
  5424. dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
  5425. }
  5426. }
  5427. /* if hw had a problem adding a filter, delete it */
  5428. if (pf->fd_inv > 0) {
  5429. hlist_for_each_entry_safe(filter, node,
  5430. &pf->fdir_filter_list, fdir_node) {
  5431. if (filter->fd_id == pf->fd_inv) {
  5432. hlist_del(&filter->fdir_node);
  5433. kfree(filter);
  5434. pf->fdir_pf_active_filters--;
  5435. }
  5436. }
  5437. }
  5438. }
  5439. #define I40E_MIN_FD_FLUSH_INTERVAL 10
  5440. #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
  5441. /**
  5442. * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
  5443. * @pf: board private structure
  5444. **/
  5445. static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
  5446. {
  5447. unsigned long min_flush_time;
  5448. int flush_wait_retry = 50;
  5449. bool disable_atr = false;
  5450. int fd_room;
  5451. int reg;
  5452. if (!time_after(jiffies, pf->fd_flush_timestamp +
  5453. (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
  5454. return;
  5455. /* If the flush is happening too quick and we have mostly SB rules we
  5456. * should not re-enable ATR for some time.
  5457. */
  5458. min_flush_time = pf->fd_flush_timestamp +
  5459. (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
  5460. fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
  5461. if (!(time_after(jiffies, min_flush_time)) &&
  5462. (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
  5463. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  5464. dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
  5465. disable_atr = true;
  5466. }
  5467. pf->fd_flush_timestamp = jiffies;
  5468. pf->flags |= I40E_FLAG_FD_ATR_AUTO_DISABLED;
  5469. /* flush all filters */
  5470. wr32(&pf->hw, I40E_PFQF_CTL_1,
  5471. I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
  5472. i40e_flush(&pf->hw);
  5473. pf->fd_flush_cnt++;
  5474. pf->fd_add_err = 0;
  5475. do {
  5476. /* Check FD flush status every 5-6msec */
  5477. usleep_range(5000, 6000);
  5478. reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
  5479. if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
  5480. break;
  5481. } while (flush_wait_retry--);
  5482. if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
  5483. dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
  5484. } else {
  5485. /* replay sideband filters */
  5486. i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
  5487. if (!disable_atr && !pf->fd_tcp4_filter_cnt)
  5488. pf->flags &= ~I40E_FLAG_FD_ATR_AUTO_DISABLED;
  5489. clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
  5490. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  5491. dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
  5492. }
  5493. }
  5494. /**
  5495. * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
  5496. * @pf: board private structure
  5497. **/
  5498. u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
  5499. {
  5500. return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
  5501. }
  5502. /* We can see up to 256 filter programming desc in transit if the filters are
  5503. * being applied really fast; before we see the first
  5504. * filter miss error on Rx queue 0. Accumulating enough error messages before
  5505. * reacting will make sure we don't cause flush too often.
  5506. */
  5507. #define I40E_MAX_FD_PROGRAM_ERROR 256
  5508. /**
  5509. * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
  5510. * @pf: board private structure
  5511. **/
  5512. static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
  5513. {
  5514. /* if interface is down do nothing */
  5515. if (test_bit(__I40E_DOWN, pf->state))
  5516. return;
  5517. if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
  5518. i40e_fdir_flush_and_replay(pf);
  5519. i40e_fdir_check_and_reenable(pf);
  5520. }
  5521. /**
  5522. * i40e_vsi_link_event - notify VSI of a link event
  5523. * @vsi: vsi to be notified
  5524. * @link_up: link up or down
  5525. **/
  5526. static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
  5527. {
  5528. if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
  5529. return;
  5530. switch (vsi->type) {
  5531. case I40E_VSI_MAIN:
  5532. if (!vsi->netdev || !vsi->netdev_registered)
  5533. break;
  5534. if (link_up) {
  5535. netif_carrier_on(vsi->netdev);
  5536. netif_tx_wake_all_queues(vsi->netdev);
  5537. } else {
  5538. netif_carrier_off(vsi->netdev);
  5539. netif_tx_stop_all_queues(vsi->netdev);
  5540. }
  5541. break;
  5542. case I40E_VSI_SRIOV:
  5543. case I40E_VSI_VMDQ2:
  5544. case I40E_VSI_CTRL:
  5545. case I40E_VSI_IWARP:
  5546. case I40E_VSI_MIRROR:
  5547. default:
  5548. /* there is no notification for other VSIs */
  5549. break;
  5550. }
  5551. }
  5552. /**
  5553. * i40e_veb_link_event - notify elements on the veb of a link event
  5554. * @veb: veb to be notified
  5555. * @link_up: link up or down
  5556. **/
  5557. static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
  5558. {
  5559. struct i40e_pf *pf;
  5560. int i;
  5561. if (!veb || !veb->pf)
  5562. return;
  5563. pf = veb->pf;
  5564. /* depth first... */
  5565. for (i = 0; i < I40E_MAX_VEB; i++)
  5566. if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
  5567. i40e_veb_link_event(pf->veb[i], link_up);
  5568. /* ... now the local VSIs */
  5569. for (i = 0; i < pf->num_alloc_vsi; i++)
  5570. if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
  5571. i40e_vsi_link_event(pf->vsi[i], link_up);
  5572. }
  5573. /**
  5574. * i40e_link_event - Update netif_carrier status
  5575. * @pf: board private structure
  5576. **/
  5577. static void i40e_link_event(struct i40e_pf *pf)
  5578. {
  5579. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  5580. u8 new_link_speed, old_link_speed;
  5581. i40e_status status;
  5582. bool new_link, old_link;
  5583. /* save off old link status information */
  5584. pf->hw.phy.link_info_old = pf->hw.phy.link_info;
  5585. /* set this to force the get_link_status call to refresh state */
  5586. pf->hw.phy.get_link_info = true;
  5587. old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
  5588. status = i40e_get_link_status(&pf->hw, &new_link);
  5589. /* On success, disable temp link polling */
  5590. if (status == I40E_SUCCESS) {
  5591. if (pf->flags & I40E_FLAG_TEMP_LINK_POLLING)
  5592. pf->flags &= ~I40E_FLAG_TEMP_LINK_POLLING;
  5593. } else {
  5594. /* Enable link polling temporarily until i40e_get_link_status
  5595. * returns I40E_SUCCESS
  5596. */
  5597. pf->flags |= I40E_FLAG_TEMP_LINK_POLLING;
  5598. dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
  5599. status);
  5600. return;
  5601. }
  5602. old_link_speed = pf->hw.phy.link_info_old.link_speed;
  5603. new_link_speed = pf->hw.phy.link_info.link_speed;
  5604. if (new_link == old_link &&
  5605. new_link_speed == old_link_speed &&
  5606. (test_bit(__I40E_VSI_DOWN, vsi->state) ||
  5607. new_link == netif_carrier_ok(vsi->netdev)))
  5608. return;
  5609. if (!test_bit(__I40E_VSI_DOWN, vsi->state))
  5610. i40e_print_link_message(vsi, new_link);
  5611. /* Notify the base of the switch tree connected to
  5612. * the link. Floating VEBs are not notified.
  5613. */
  5614. if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
  5615. i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
  5616. else
  5617. i40e_vsi_link_event(vsi, new_link);
  5618. if (pf->vf)
  5619. i40e_vc_notify_link_state(pf);
  5620. if (pf->flags & I40E_FLAG_PTP)
  5621. i40e_ptp_set_increment(pf);
  5622. }
  5623. /**
  5624. * i40e_watchdog_subtask - periodic checks not using event driven response
  5625. * @pf: board private structure
  5626. **/
  5627. static void i40e_watchdog_subtask(struct i40e_pf *pf)
  5628. {
  5629. int i;
  5630. /* if interface is down do nothing */
  5631. if (test_bit(__I40E_DOWN, pf->state) ||
  5632. test_bit(__I40E_CONFIG_BUSY, pf->state))
  5633. return;
  5634. /* make sure we don't do these things too often */
  5635. if (time_before(jiffies, (pf->service_timer_previous +
  5636. pf->service_timer_period)))
  5637. return;
  5638. pf->service_timer_previous = jiffies;
  5639. if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
  5640. (pf->flags & I40E_FLAG_TEMP_LINK_POLLING))
  5641. i40e_link_event(pf);
  5642. /* Update the stats for active netdevs so the network stack
  5643. * can look at updated numbers whenever it cares to
  5644. */
  5645. for (i = 0; i < pf->num_alloc_vsi; i++)
  5646. if (pf->vsi[i] && pf->vsi[i]->netdev)
  5647. i40e_update_stats(pf->vsi[i]);
  5648. if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
  5649. /* Update the stats for the active switching components */
  5650. for (i = 0; i < I40E_MAX_VEB; i++)
  5651. if (pf->veb[i])
  5652. i40e_update_veb_stats(pf->veb[i]);
  5653. }
  5654. i40e_ptp_rx_hang(pf);
  5655. i40e_ptp_tx_hang(pf);
  5656. }
  5657. /**
  5658. * i40e_reset_subtask - Set up for resetting the device and driver
  5659. * @pf: board private structure
  5660. **/
  5661. static void i40e_reset_subtask(struct i40e_pf *pf)
  5662. {
  5663. u32 reset_flags = 0;
  5664. if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
  5665. reset_flags |= BIT(__I40E_REINIT_REQUESTED);
  5666. clear_bit(__I40E_REINIT_REQUESTED, pf->state);
  5667. }
  5668. if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
  5669. reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
  5670. clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
  5671. }
  5672. if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
  5673. reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
  5674. clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
  5675. }
  5676. if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
  5677. reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
  5678. clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
  5679. }
  5680. if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
  5681. reset_flags |= BIT(__I40E_DOWN_REQUESTED);
  5682. clear_bit(__I40E_DOWN_REQUESTED, pf->state);
  5683. }
  5684. /* If there's a recovery already waiting, it takes
  5685. * precedence before starting a new reset sequence.
  5686. */
  5687. if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
  5688. i40e_prep_for_reset(pf, false);
  5689. i40e_reset(pf);
  5690. i40e_rebuild(pf, false, false);
  5691. }
  5692. /* If we're already down or resetting, just bail */
  5693. if (reset_flags &&
  5694. !test_bit(__I40E_DOWN, pf->state) &&
  5695. !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
  5696. i40e_do_reset(pf, reset_flags, false);
  5697. }
  5698. }
  5699. /**
  5700. * i40e_handle_link_event - Handle link event
  5701. * @pf: board private structure
  5702. * @e: event info posted on ARQ
  5703. **/
  5704. static void i40e_handle_link_event(struct i40e_pf *pf,
  5705. struct i40e_arq_event_info *e)
  5706. {
  5707. struct i40e_aqc_get_link_status *status =
  5708. (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
  5709. /* Do a new status request to re-enable LSE reporting
  5710. * and load new status information into the hw struct
  5711. * This completely ignores any state information
  5712. * in the ARQ event info, instead choosing to always
  5713. * issue the AQ update link status command.
  5714. */
  5715. i40e_link_event(pf);
  5716. /* check for unqualified module, if link is down */
  5717. if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
  5718. (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
  5719. (!(status->link_info & I40E_AQ_LINK_UP)))
  5720. dev_err(&pf->pdev->dev,
  5721. "The driver failed to link because an unqualified module was detected.\n");
  5722. }
  5723. /**
  5724. * i40e_clean_adminq_subtask - Clean the AdminQ rings
  5725. * @pf: board private structure
  5726. **/
  5727. static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
  5728. {
  5729. struct i40e_arq_event_info event;
  5730. struct i40e_hw *hw = &pf->hw;
  5731. u16 pending, i = 0;
  5732. i40e_status ret;
  5733. u16 opcode;
  5734. u32 oldval;
  5735. u32 val;
  5736. /* Do not run clean AQ when PF reset fails */
  5737. if (test_bit(__I40E_RESET_FAILED, pf->state))
  5738. return;
  5739. /* check for error indications */
  5740. val = rd32(&pf->hw, pf->hw.aq.arq.len);
  5741. oldval = val;
  5742. if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
  5743. if (hw->debug_mask & I40E_DEBUG_AQ)
  5744. dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
  5745. val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
  5746. }
  5747. if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
  5748. if (hw->debug_mask & I40E_DEBUG_AQ)
  5749. dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
  5750. val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
  5751. pf->arq_overflows++;
  5752. }
  5753. if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
  5754. if (hw->debug_mask & I40E_DEBUG_AQ)
  5755. dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
  5756. val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
  5757. }
  5758. if (oldval != val)
  5759. wr32(&pf->hw, pf->hw.aq.arq.len, val);
  5760. val = rd32(&pf->hw, pf->hw.aq.asq.len);
  5761. oldval = val;
  5762. if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
  5763. if (pf->hw.debug_mask & I40E_DEBUG_AQ)
  5764. dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
  5765. val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
  5766. }
  5767. if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
  5768. if (pf->hw.debug_mask & I40E_DEBUG_AQ)
  5769. dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
  5770. val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
  5771. }
  5772. if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
  5773. if (pf->hw.debug_mask & I40E_DEBUG_AQ)
  5774. dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
  5775. val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
  5776. }
  5777. if (oldval != val)
  5778. wr32(&pf->hw, pf->hw.aq.asq.len, val);
  5779. event.buf_len = I40E_MAX_AQ_BUF_SIZE;
  5780. event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
  5781. if (!event.msg_buf)
  5782. return;
  5783. do {
  5784. ret = i40e_clean_arq_element(hw, &event, &pending);
  5785. if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
  5786. break;
  5787. else if (ret) {
  5788. dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
  5789. break;
  5790. }
  5791. opcode = le16_to_cpu(event.desc.opcode);
  5792. switch (opcode) {
  5793. case i40e_aqc_opc_get_link_status:
  5794. i40e_handle_link_event(pf, &event);
  5795. break;
  5796. case i40e_aqc_opc_send_msg_to_pf:
  5797. ret = i40e_vc_process_vf_msg(pf,
  5798. le16_to_cpu(event.desc.retval),
  5799. le32_to_cpu(event.desc.cookie_high),
  5800. le32_to_cpu(event.desc.cookie_low),
  5801. event.msg_buf,
  5802. event.msg_len);
  5803. break;
  5804. case i40e_aqc_opc_lldp_update_mib:
  5805. dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
  5806. #ifdef CONFIG_I40E_DCB
  5807. rtnl_lock();
  5808. ret = i40e_handle_lldp_event(pf, &event);
  5809. rtnl_unlock();
  5810. #endif /* CONFIG_I40E_DCB */
  5811. break;
  5812. case i40e_aqc_opc_event_lan_overflow:
  5813. dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
  5814. i40e_handle_lan_overflow_event(pf, &event);
  5815. break;
  5816. case i40e_aqc_opc_send_msg_to_peer:
  5817. dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
  5818. break;
  5819. case i40e_aqc_opc_nvm_erase:
  5820. case i40e_aqc_opc_nvm_update:
  5821. case i40e_aqc_opc_oem_post_update:
  5822. i40e_debug(&pf->hw, I40E_DEBUG_NVM,
  5823. "ARQ NVM operation 0x%04x completed\n",
  5824. opcode);
  5825. break;
  5826. default:
  5827. dev_info(&pf->pdev->dev,
  5828. "ARQ: Unknown event 0x%04x ignored\n",
  5829. opcode);
  5830. break;
  5831. }
  5832. } while (i++ < pf->adminq_work_limit);
  5833. if (i < pf->adminq_work_limit)
  5834. clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
  5835. /* re-enable Admin queue interrupt cause */
  5836. val = rd32(hw, I40E_PFINT_ICR0_ENA);
  5837. val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
  5838. wr32(hw, I40E_PFINT_ICR0_ENA, val);
  5839. i40e_flush(hw);
  5840. kfree(event.msg_buf);
  5841. }
  5842. /**
  5843. * i40e_verify_eeprom - make sure eeprom is good to use
  5844. * @pf: board private structure
  5845. **/
  5846. static void i40e_verify_eeprom(struct i40e_pf *pf)
  5847. {
  5848. int err;
  5849. err = i40e_diag_eeprom_test(&pf->hw);
  5850. if (err) {
  5851. /* retry in case of garbage read */
  5852. err = i40e_diag_eeprom_test(&pf->hw);
  5853. if (err) {
  5854. dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
  5855. err);
  5856. set_bit(__I40E_BAD_EEPROM, pf->state);
  5857. }
  5858. }
  5859. if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
  5860. dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
  5861. clear_bit(__I40E_BAD_EEPROM, pf->state);
  5862. }
  5863. }
  5864. /**
  5865. * i40e_enable_pf_switch_lb
  5866. * @pf: pointer to the PF structure
  5867. *
  5868. * enable switch loop back or die - no point in a return value
  5869. **/
  5870. static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
  5871. {
  5872. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  5873. struct i40e_vsi_context ctxt;
  5874. int ret;
  5875. ctxt.seid = pf->main_vsi_seid;
  5876. ctxt.pf_num = pf->hw.pf_id;
  5877. ctxt.vf_num = 0;
  5878. ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
  5879. if (ret) {
  5880. dev_info(&pf->pdev->dev,
  5881. "couldn't get PF vsi config, err %s aq_err %s\n",
  5882. i40e_stat_str(&pf->hw, ret),
  5883. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5884. return;
  5885. }
  5886. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  5887. ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  5888. ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  5889. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  5890. if (ret) {
  5891. dev_info(&pf->pdev->dev,
  5892. "update vsi switch failed, err %s aq_err %s\n",
  5893. i40e_stat_str(&pf->hw, ret),
  5894. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5895. }
  5896. }
  5897. /**
  5898. * i40e_disable_pf_switch_lb
  5899. * @pf: pointer to the PF structure
  5900. *
  5901. * disable switch loop back or die - no point in a return value
  5902. **/
  5903. static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
  5904. {
  5905. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  5906. struct i40e_vsi_context ctxt;
  5907. int ret;
  5908. ctxt.seid = pf->main_vsi_seid;
  5909. ctxt.pf_num = pf->hw.pf_id;
  5910. ctxt.vf_num = 0;
  5911. ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
  5912. if (ret) {
  5913. dev_info(&pf->pdev->dev,
  5914. "couldn't get PF vsi config, err %s aq_err %s\n",
  5915. i40e_stat_str(&pf->hw, ret),
  5916. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5917. return;
  5918. }
  5919. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  5920. ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  5921. ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  5922. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  5923. if (ret) {
  5924. dev_info(&pf->pdev->dev,
  5925. "update vsi switch failed, err %s aq_err %s\n",
  5926. i40e_stat_str(&pf->hw, ret),
  5927. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5928. }
  5929. }
  5930. /**
  5931. * i40e_config_bridge_mode - Configure the HW bridge mode
  5932. * @veb: pointer to the bridge instance
  5933. *
  5934. * Configure the loop back mode for the LAN VSI that is downlink to the
  5935. * specified HW bridge instance. It is expected this function is called
  5936. * when a new HW bridge is instantiated.
  5937. **/
  5938. static void i40e_config_bridge_mode(struct i40e_veb *veb)
  5939. {
  5940. struct i40e_pf *pf = veb->pf;
  5941. if (pf->hw.debug_mask & I40E_DEBUG_LAN)
  5942. dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
  5943. veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
  5944. if (veb->bridge_mode & BRIDGE_MODE_VEPA)
  5945. i40e_disable_pf_switch_lb(pf);
  5946. else
  5947. i40e_enable_pf_switch_lb(pf);
  5948. }
  5949. /**
  5950. * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
  5951. * @veb: pointer to the VEB instance
  5952. *
  5953. * This is a recursive function that first builds the attached VSIs then
  5954. * recurses in to build the next layer of VEB. We track the connections
  5955. * through our own index numbers because the seid's from the HW could
  5956. * change across the reset.
  5957. **/
  5958. static int i40e_reconstitute_veb(struct i40e_veb *veb)
  5959. {
  5960. struct i40e_vsi *ctl_vsi = NULL;
  5961. struct i40e_pf *pf = veb->pf;
  5962. int v, veb_idx;
  5963. int ret;
  5964. /* build VSI that owns this VEB, temporarily attached to base VEB */
  5965. for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
  5966. if (pf->vsi[v] &&
  5967. pf->vsi[v]->veb_idx == veb->idx &&
  5968. pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
  5969. ctl_vsi = pf->vsi[v];
  5970. break;
  5971. }
  5972. }
  5973. if (!ctl_vsi) {
  5974. dev_info(&pf->pdev->dev,
  5975. "missing owner VSI for veb_idx %d\n", veb->idx);
  5976. ret = -ENOENT;
  5977. goto end_reconstitute;
  5978. }
  5979. if (ctl_vsi != pf->vsi[pf->lan_vsi])
  5980. ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
  5981. ret = i40e_add_vsi(ctl_vsi);
  5982. if (ret) {
  5983. dev_info(&pf->pdev->dev,
  5984. "rebuild of veb_idx %d owner VSI failed: %d\n",
  5985. veb->idx, ret);
  5986. goto end_reconstitute;
  5987. }
  5988. i40e_vsi_reset_stats(ctl_vsi);
  5989. /* create the VEB in the switch and move the VSI onto the VEB */
  5990. ret = i40e_add_veb(veb, ctl_vsi);
  5991. if (ret)
  5992. goto end_reconstitute;
  5993. if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
  5994. veb->bridge_mode = BRIDGE_MODE_VEB;
  5995. else
  5996. veb->bridge_mode = BRIDGE_MODE_VEPA;
  5997. i40e_config_bridge_mode(veb);
  5998. /* create the remaining VSIs attached to this VEB */
  5999. for (v = 0; v < pf->num_alloc_vsi; v++) {
  6000. if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
  6001. continue;
  6002. if (pf->vsi[v]->veb_idx == veb->idx) {
  6003. struct i40e_vsi *vsi = pf->vsi[v];
  6004. vsi->uplink_seid = veb->seid;
  6005. ret = i40e_add_vsi(vsi);
  6006. if (ret) {
  6007. dev_info(&pf->pdev->dev,
  6008. "rebuild of vsi_idx %d failed: %d\n",
  6009. v, ret);
  6010. goto end_reconstitute;
  6011. }
  6012. i40e_vsi_reset_stats(vsi);
  6013. }
  6014. }
  6015. /* create any VEBs attached to this VEB - RECURSION */
  6016. for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
  6017. if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
  6018. pf->veb[veb_idx]->uplink_seid = veb->seid;
  6019. ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
  6020. if (ret)
  6021. break;
  6022. }
  6023. }
  6024. end_reconstitute:
  6025. return ret;
  6026. }
  6027. /**
  6028. * i40e_get_capabilities - get info about the HW
  6029. * @pf: the PF struct
  6030. **/
  6031. static int i40e_get_capabilities(struct i40e_pf *pf)
  6032. {
  6033. struct i40e_aqc_list_capabilities_element_resp *cap_buf;
  6034. u16 data_size;
  6035. int buf_len;
  6036. int err;
  6037. buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
  6038. do {
  6039. cap_buf = kzalloc(buf_len, GFP_KERNEL);
  6040. if (!cap_buf)
  6041. return -ENOMEM;
  6042. /* this loads the data into the hw struct for us */
  6043. err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
  6044. &data_size,
  6045. i40e_aqc_opc_list_func_capabilities,
  6046. NULL);
  6047. /* data loaded, buffer no longer needed */
  6048. kfree(cap_buf);
  6049. if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
  6050. /* retry with a larger buffer */
  6051. buf_len = data_size;
  6052. } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
  6053. dev_info(&pf->pdev->dev,
  6054. "capability discovery failed, err %s aq_err %s\n",
  6055. i40e_stat_str(&pf->hw, err),
  6056. i40e_aq_str(&pf->hw,
  6057. pf->hw.aq.asq_last_status));
  6058. return -ENODEV;
  6059. }
  6060. } while (err);
  6061. if (pf->hw.debug_mask & I40E_DEBUG_USER)
  6062. dev_info(&pf->pdev->dev,
  6063. "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",
  6064. pf->hw.pf_id, pf->hw.func_caps.num_vfs,
  6065. pf->hw.func_caps.num_msix_vectors,
  6066. pf->hw.func_caps.num_msix_vectors_vf,
  6067. pf->hw.func_caps.fd_filters_guaranteed,
  6068. pf->hw.func_caps.fd_filters_best_effort,
  6069. pf->hw.func_caps.num_tx_qp,
  6070. pf->hw.func_caps.num_vsis);
  6071. #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
  6072. + pf->hw.func_caps.num_vfs)
  6073. if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
  6074. dev_info(&pf->pdev->dev,
  6075. "got num_vsis %d, setting num_vsis to %d\n",
  6076. pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
  6077. pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
  6078. }
  6079. return 0;
  6080. }
  6081. static int i40e_vsi_clear(struct i40e_vsi *vsi);
  6082. /**
  6083. * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
  6084. * @pf: board private structure
  6085. **/
  6086. static void i40e_fdir_sb_setup(struct i40e_pf *pf)
  6087. {
  6088. struct i40e_vsi *vsi;
  6089. /* quick workaround for an NVM issue that leaves a critical register
  6090. * uninitialized
  6091. */
  6092. if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
  6093. static const u32 hkey[] = {
  6094. 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
  6095. 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
  6096. 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
  6097. 0x95b3a76d};
  6098. int i;
  6099. for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
  6100. wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
  6101. }
  6102. if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
  6103. return;
  6104. /* find existing VSI and see if it needs configuring */
  6105. vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
  6106. /* create a new VSI if none exists */
  6107. if (!vsi) {
  6108. vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
  6109. pf->vsi[pf->lan_vsi]->seid, 0);
  6110. if (!vsi) {
  6111. dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
  6112. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  6113. return;
  6114. }
  6115. }
  6116. i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
  6117. }
  6118. /**
  6119. * i40e_fdir_teardown - release the Flow Director resources
  6120. * @pf: board private structure
  6121. **/
  6122. static void i40e_fdir_teardown(struct i40e_pf *pf)
  6123. {
  6124. struct i40e_vsi *vsi;
  6125. i40e_fdir_filter_exit(pf);
  6126. vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
  6127. if (vsi)
  6128. i40e_vsi_release(vsi);
  6129. }
  6130. /**
  6131. * i40e_prep_for_reset - prep for the core to reset
  6132. * @pf: board private structure
  6133. * @lock_acquired: indicates whether or not the lock has been acquired
  6134. * before this function was called.
  6135. *
  6136. * Close up the VFs and other things in prep for PF Reset.
  6137. **/
  6138. static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired)
  6139. {
  6140. struct i40e_hw *hw = &pf->hw;
  6141. i40e_status ret = 0;
  6142. u32 v;
  6143. clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
  6144. if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
  6145. return;
  6146. if (i40e_check_asq_alive(&pf->hw))
  6147. i40e_vc_notify_reset(pf);
  6148. dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
  6149. /* quiesce the VSIs and their queues that are not already DOWN */
  6150. /* pf_quiesce_all_vsi modifies netdev structures -rtnl_lock needed */
  6151. if (!lock_acquired)
  6152. rtnl_lock();
  6153. i40e_pf_quiesce_all_vsi(pf);
  6154. if (!lock_acquired)
  6155. rtnl_unlock();
  6156. for (v = 0; v < pf->num_alloc_vsi; v++) {
  6157. if (pf->vsi[v])
  6158. pf->vsi[v]->seid = 0;
  6159. }
  6160. i40e_shutdown_adminq(&pf->hw);
  6161. /* call shutdown HMC */
  6162. if (hw->hmc.hmc_obj) {
  6163. ret = i40e_shutdown_lan_hmc(hw);
  6164. if (ret)
  6165. dev_warn(&pf->pdev->dev,
  6166. "shutdown_lan_hmc failed: %d\n", ret);
  6167. }
  6168. }
  6169. /**
  6170. * i40e_send_version - update firmware with driver version
  6171. * @pf: PF struct
  6172. */
  6173. static void i40e_send_version(struct i40e_pf *pf)
  6174. {
  6175. struct i40e_driver_version dv;
  6176. dv.major_version = DRV_VERSION_MAJOR;
  6177. dv.minor_version = DRV_VERSION_MINOR;
  6178. dv.build_version = DRV_VERSION_BUILD;
  6179. dv.subbuild_version = 0;
  6180. strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
  6181. i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
  6182. }
  6183. /**
  6184. * i40e_get_oem_version - get OEM specific version information
  6185. * @hw: pointer to the hardware structure
  6186. **/
  6187. static void i40e_get_oem_version(struct i40e_hw *hw)
  6188. {
  6189. u16 block_offset = 0xffff;
  6190. u16 block_length = 0;
  6191. u16 capabilities = 0;
  6192. u16 gen_snap = 0;
  6193. u16 release = 0;
  6194. #define I40E_SR_NVM_OEM_VERSION_PTR 0x1B
  6195. #define I40E_NVM_OEM_LENGTH_OFFSET 0x00
  6196. #define I40E_NVM_OEM_CAPABILITIES_OFFSET 0x01
  6197. #define I40E_NVM_OEM_GEN_OFFSET 0x02
  6198. #define I40E_NVM_OEM_RELEASE_OFFSET 0x03
  6199. #define I40E_NVM_OEM_CAPABILITIES_MASK 0x000F
  6200. #define I40E_NVM_OEM_LENGTH 3
  6201. /* Check if pointer to OEM version block is valid. */
  6202. i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
  6203. if (block_offset == 0xffff)
  6204. return;
  6205. /* Check if OEM version block has correct length. */
  6206. i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
  6207. &block_length);
  6208. if (block_length < I40E_NVM_OEM_LENGTH)
  6209. return;
  6210. /* Check if OEM version format is as expected. */
  6211. i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
  6212. &capabilities);
  6213. if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
  6214. return;
  6215. i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
  6216. &gen_snap);
  6217. i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
  6218. &release);
  6219. hw->nvm.oem_ver = (gen_snap << I40E_OEM_SNAP_SHIFT) | release;
  6220. hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
  6221. }
  6222. /**
  6223. * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
  6224. * @pf: board private structure
  6225. **/
  6226. static int i40e_reset(struct i40e_pf *pf)
  6227. {
  6228. struct i40e_hw *hw = &pf->hw;
  6229. i40e_status ret;
  6230. ret = i40e_pf_reset(hw);
  6231. if (ret) {
  6232. dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
  6233. set_bit(__I40E_RESET_FAILED, pf->state);
  6234. clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
  6235. } else {
  6236. pf->pfr_count++;
  6237. }
  6238. return ret;
  6239. }
  6240. /**
  6241. * i40e_rebuild - rebuild using a saved config
  6242. * @pf: board private structure
  6243. * @reinit: if the Main VSI needs to re-initialized.
  6244. * @lock_acquired: indicates whether or not the lock has been acquired
  6245. * before this function was called.
  6246. **/
  6247. static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
  6248. {
  6249. struct i40e_hw *hw = &pf->hw;
  6250. u8 set_fc_aq_fail = 0;
  6251. i40e_status ret;
  6252. u32 val;
  6253. int v;
  6254. if (test_bit(__I40E_DOWN, pf->state))
  6255. goto clear_recovery;
  6256. dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
  6257. /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
  6258. ret = i40e_init_adminq(&pf->hw);
  6259. if (ret) {
  6260. dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
  6261. i40e_stat_str(&pf->hw, ret),
  6262. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  6263. goto clear_recovery;
  6264. }
  6265. i40e_get_oem_version(&pf->hw);
  6266. /* re-verify the eeprom if we just had an EMP reset */
  6267. if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
  6268. i40e_verify_eeprom(pf);
  6269. i40e_clear_pxe_mode(hw);
  6270. ret = i40e_get_capabilities(pf);
  6271. if (ret)
  6272. goto end_core_reset;
  6273. ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
  6274. hw->func_caps.num_rx_qp, 0, 0);
  6275. if (ret) {
  6276. dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
  6277. goto end_core_reset;
  6278. }
  6279. ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
  6280. if (ret) {
  6281. dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
  6282. goto end_core_reset;
  6283. }
  6284. #ifdef CONFIG_I40E_DCB
  6285. ret = i40e_init_pf_dcb(pf);
  6286. if (ret) {
  6287. dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
  6288. pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
  6289. /* Continue without DCB enabled */
  6290. }
  6291. #endif /* CONFIG_I40E_DCB */
  6292. /* do basic switch setup */
  6293. if (!lock_acquired)
  6294. rtnl_lock();
  6295. ret = i40e_setup_pf_switch(pf, reinit);
  6296. if (ret)
  6297. goto end_unlock;
  6298. /* The driver only wants link up/down and module qualification
  6299. * reports from firmware. Note the negative logic.
  6300. */
  6301. ret = i40e_aq_set_phy_int_mask(&pf->hw,
  6302. ~(I40E_AQ_EVENT_LINK_UPDOWN |
  6303. I40E_AQ_EVENT_MEDIA_NA |
  6304. I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
  6305. if (ret)
  6306. dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
  6307. i40e_stat_str(&pf->hw, ret),
  6308. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  6309. /* make sure our flow control settings are restored */
  6310. ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
  6311. if (ret)
  6312. dev_dbg(&pf->pdev->dev, "setting flow control: ret = %s last_status = %s\n",
  6313. i40e_stat_str(&pf->hw, ret),
  6314. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  6315. /* Rebuild the VSIs and VEBs that existed before reset.
  6316. * They are still in our local switch element arrays, so only
  6317. * need to rebuild the switch model in the HW.
  6318. *
  6319. * If there were VEBs but the reconstitution failed, we'll try
  6320. * try to recover minimal use by getting the basic PF VSI working.
  6321. */
  6322. if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
  6323. dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
  6324. /* find the one VEB connected to the MAC, and find orphans */
  6325. for (v = 0; v < I40E_MAX_VEB; v++) {
  6326. if (!pf->veb[v])
  6327. continue;
  6328. if (pf->veb[v]->uplink_seid == pf->mac_seid ||
  6329. pf->veb[v]->uplink_seid == 0) {
  6330. ret = i40e_reconstitute_veb(pf->veb[v]);
  6331. if (!ret)
  6332. continue;
  6333. /* If Main VEB failed, we're in deep doodoo,
  6334. * so give up rebuilding the switch and set up
  6335. * for minimal rebuild of PF VSI.
  6336. * If orphan failed, we'll report the error
  6337. * but try to keep going.
  6338. */
  6339. if (pf->veb[v]->uplink_seid == pf->mac_seid) {
  6340. dev_info(&pf->pdev->dev,
  6341. "rebuild of switch failed: %d, will try to set up simple PF connection\n",
  6342. ret);
  6343. pf->vsi[pf->lan_vsi]->uplink_seid
  6344. = pf->mac_seid;
  6345. break;
  6346. } else if (pf->veb[v]->uplink_seid == 0) {
  6347. dev_info(&pf->pdev->dev,
  6348. "rebuild of orphan VEB failed: %d\n",
  6349. ret);
  6350. }
  6351. }
  6352. }
  6353. }
  6354. if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
  6355. dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
  6356. /* no VEB, so rebuild only the Main VSI */
  6357. ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
  6358. if (ret) {
  6359. dev_info(&pf->pdev->dev,
  6360. "rebuild of Main VSI failed: %d\n", ret);
  6361. goto end_unlock;
  6362. }
  6363. }
  6364. /* Reconfigure hardware for allowing smaller MSS in the case
  6365. * of TSO, so that we avoid the MDD being fired and causing
  6366. * a reset in the case of small MSS+TSO.
  6367. */
  6368. #define I40E_REG_MSS 0x000E64DC
  6369. #define I40E_REG_MSS_MIN_MASK 0x3FF0000
  6370. #define I40E_64BYTE_MSS 0x400000
  6371. val = rd32(hw, I40E_REG_MSS);
  6372. if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
  6373. val &= ~I40E_REG_MSS_MIN_MASK;
  6374. val |= I40E_64BYTE_MSS;
  6375. wr32(hw, I40E_REG_MSS, val);
  6376. }
  6377. if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
  6378. msleep(75);
  6379. ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
  6380. if (ret)
  6381. dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
  6382. i40e_stat_str(&pf->hw, ret),
  6383. i40e_aq_str(&pf->hw,
  6384. pf->hw.aq.asq_last_status));
  6385. }
  6386. /* reinit the misc interrupt */
  6387. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  6388. ret = i40e_setup_misc_vector(pf);
  6389. /* Add a filter to drop all Flow control frames from any VSI from being
  6390. * transmitted. By doing so we stop a malicious VF from sending out
  6391. * PAUSE or PFC frames and potentially controlling traffic for other
  6392. * PF/VF VSIs.
  6393. * The FW can still send Flow control frames if enabled.
  6394. */
  6395. i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
  6396. pf->main_vsi_seid);
  6397. /* restart the VSIs that were rebuilt and running before the reset */
  6398. i40e_pf_unquiesce_all_vsi(pf);
  6399. /* Release the RTNL lock before we start resetting VFs */
  6400. if (!lock_acquired)
  6401. rtnl_unlock();
  6402. i40e_reset_all_vfs(pf, true);
  6403. /* tell the firmware that we're starting */
  6404. i40e_send_version(pf);
  6405. /* We've already released the lock, so don't do it again */
  6406. goto end_core_reset;
  6407. end_unlock:
  6408. if (!lock_acquired)
  6409. rtnl_unlock();
  6410. end_core_reset:
  6411. clear_bit(__I40E_RESET_FAILED, pf->state);
  6412. clear_recovery:
  6413. clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
  6414. }
  6415. /**
  6416. * i40e_reset_and_rebuild - reset and rebuild using a saved config
  6417. * @pf: board private structure
  6418. * @reinit: if the Main VSI needs to re-initialized.
  6419. * @lock_acquired: indicates whether or not the lock has been acquired
  6420. * before this function was called.
  6421. **/
  6422. static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
  6423. bool lock_acquired)
  6424. {
  6425. int ret;
  6426. /* Now we wait for GRST to settle out.
  6427. * We don't have to delete the VEBs or VSIs from the hw switch
  6428. * because the reset will make them disappear.
  6429. */
  6430. ret = i40e_reset(pf);
  6431. if (!ret)
  6432. i40e_rebuild(pf, reinit, lock_acquired);
  6433. }
  6434. /**
  6435. * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
  6436. * @pf: board private structure
  6437. *
  6438. * Close up the VFs and other things in prep for a Core Reset,
  6439. * then get ready to rebuild the world.
  6440. * @lock_acquired: indicates whether or not the lock has been acquired
  6441. * before this function was called.
  6442. **/
  6443. static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
  6444. {
  6445. i40e_prep_for_reset(pf, lock_acquired);
  6446. i40e_reset_and_rebuild(pf, false, lock_acquired);
  6447. }
  6448. /**
  6449. * i40e_handle_mdd_event
  6450. * @pf: pointer to the PF structure
  6451. *
  6452. * Called from the MDD irq handler to identify possibly malicious vfs
  6453. **/
  6454. static void i40e_handle_mdd_event(struct i40e_pf *pf)
  6455. {
  6456. struct i40e_hw *hw = &pf->hw;
  6457. bool mdd_detected = false;
  6458. bool pf_mdd_detected = false;
  6459. struct i40e_vf *vf;
  6460. u32 reg;
  6461. int i;
  6462. if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
  6463. return;
  6464. /* find what triggered the MDD event */
  6465. reg = rd32(hw, I40E_GL_MDET_TX);
  6466. if (reg & I40E_GL_MDET_TX_VALID_MASK) {
  6467. u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
  6468. I40E_GL_MDET_TX_PF_NUM_SHIFT;
  6469. u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
  6470. I40E_GL_MDET_TX_VF_NUM_SHIFT;
  6471. u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
  6472. I40E_GL_MDET_TX_EVENT_SHIFT;
  6473. u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
  6474. I40E_GL_MDET_TX_QUEUE_SHIFT) -
  6475. pf->hw.func_caps.base_queue;
  6476. if (netif_msg_tx_err(pf))
  6477. dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
  6478. event, queue, pf_num, vf_num);
  6479. wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
  6480. mdd_detected = true;
  6481. }
  6482. reg = rd32(hw, I40E_GL_MDET_RX);
  6483. if (reg & I40E_GL_MDET_RX_VALID_MASK) {
  6484. u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
  6485. I40E_GL_MDET_RX_FUNCTION_SHIFT;
  6486. u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
  6487. I40E_GL_MDET_RX_EVENT_SHIFT;
  6488. u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
  6489. I40E_GL_MDET_RX_QUEUE_SHIFT) -
  6490. pf->hw.func_caps.base_queue;
  6491. if (netif_msg_rx_err(pf))
  6492. dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
  6493. event, queue, func);
  6494. wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
  6495. mdd_detected = true;
  6496. }
  6497. if (mdd_detected) {
  6498. reg = rd32(hw, I40E_PF_MDET_TX);
  6499. if (reg & I40E_PF_MDET_TX_VALID_MASK) {
  6500. wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
  6501. dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
  6502. pf_mdd_detected = true;
  6503. }
  6504. reg = rd32(hw, I40E_PF_MDET_RX);
  6505. if (reg & I40E_PF_MDET_RX_VALID_MASK) {
  6506. wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
  6507. dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
  6508. pf_mdd_detected = true;
  6509. }
  6510. /* Queue belongs to the PF, initiate a reset */
  6511. if (pf_mdd_detected) {
  6512. set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
  6513. i40e_service_event_schedule(pf);
  6514. }
  6515. }
  6516. /* see if one of the VFs needs its hand slapped */
  6517. for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
  6518. vf = &(pf->vf[i]);
  6519. reg = rd32(hw, I40E_VP_MDET_TX(i));
  6520. if (reg & I40E_VP_MDET_TX_VALID_MASK) {
  6521. wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
  6522. vf->num_mdd_events++;
  6523. dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
  6524. i);
  6525. }
  6526. reg = rd32(hw, I40E_VP_MDET_RX(i));
  6527. if (reg & I40E_VP_MDET_RX_VALID_MASK) {
  6528. wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
  6529. vf->num_mdd_events++;
  6530. dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
  6531. i);
  6532. }
  6533. if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
  6534. dev_info(&pf->pdev->dev,
  6535. "Too many MDD events on VF %d, disabled\n", i);
  6536. dev_info(&pf->pdev->dev,
  6537. "Use PF Control I/F to re-enable the VF\n");
  6538. set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
  6539. }
  6540. }
  6541. /* re-enable mdd interrupt cause */
  6542. clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
  6543. reg = rd32(hw, I40E_PFINT_ICR0_ENA);
  6544. reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
  6545. wr32(hw, I40E_PFINT_ICR0_ENA, reg);
  6546. i40e_flush(hw);
  6547. }
  6548. static const char *i40e_tunnel_name(struct i40e_udp_port_config *port)
  6549. {
  6550. switch (port->type) {
  6551. case UDP_TUNNEL_TYPE_VXLAN:
  6552. return "vxlan";
  6553. case UDP_TUNNEL_TYPE_GENEVE:
  6554. return "geneve";
  6555. default:
  6556. return "unknown";
  6557. }
  6558. }
  6559. /**
  6560. * i40e_sync_udp_filters - Trigger a sync event for existing UDP filters
  6561. * @pf: board private structure
  6562. **/
  6563. static void i40e_sync_udp_filters(struct i40e_pf *pf)
  6564. {
  6565. int i;
  6566. /* loop through and set pending bit for all active UDP filters */
  6567. for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
  6568. if (pf->udp_ports[i].port)
  6569. pf->pending_udp_bitmap |= BIT_ULL(i);
  6570. }
  6571. pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
  6572. }
  6573. /**
  6574. * i40e_sync_udp_filters_subtask - Sync the VSI filter list with HW
  6575. * @pf: board private structure
  6576. **/
  6577. static void i40e_sync_udp_filters_subtask(struct i40e_pf *pf)
  6578. {
  6579. struct i40e_hw *hw = &pf->hw;
  6580. i40e_status ret;
  6581. u16 port;
  6582. int i;
  6583. if (!(pf->flags & I40E_FLAG_UDP_FILTER_SYNC))
  6584. return;
  6585. pf->flags &= ~I40E_FLAG_UDP_FILTER_SYNC;
  6586. for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
  6587. if (pf->pending_udp_bitmap & BIT_ULL(i)) {
  6588. pf->pending_udp_bitmap &= ~BIT_ULL(i);
  6589. port = pf->udp_ports[i].port;
  6590. if (port)
  6591. ret = i40e_aq_add_udp_tunnel(hw, port,
  6592. pf->udp_ports[i].type,
  6593. NULL, NULL);
  6594. else
  6595. ret = i40e_aq_del_udp_tunnel(hw, i, NULL);
  6596. if (ret) {
  6597. dev_info(&pf->pdev->dev,
  6598. "%s %s port %d, index %d failed, err %s aq_err %s\n",
  6599. i40e_tunnel_name(&pf->udp_ports[i]),
  6600. port ? "add" : "delete",
  6601. port, i,
  6602. i40e_stat_str(&pf->hw, ret),
  6603. i40e_aq_str(&pf->hw,
  6604. pf->hw.aq.asq_last_status));
  6605. pf->udp_ports[i].port = 0;
  6606. }
  6607. }
  6608. }
  6609. }
  6610. /**
  6611. * i40e_service_task - Run the driver's async subtasks
  6612. * @work: pointer to work_struct containing our data
  6613. **/
  6614. static void i40e_service_task(struct work_struct *work)
  6615. {
  6616. struct i40e_pf *pf = container_of(work,
  6617. struct i40e_pf,
  6618. service_task);
  6619. unsigned long start_time = jiffies;
  6620. /* don't bother with service tasks if a reset is in progress */
  6621. if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
  6622. return;
  6623. if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
  6624. return;
  6625. i40e_detect_recover_hung(pf);
  6626. i40e_sync_filters_subtask(pf);
  6627. i40e_reset_subtask(pf);
  6628. i40e_handle_mdd_event(pf);
  6629. i40e_vc_process_vflr_event(pf);
  6630. i40e_watchdog_subtask(pf);
  6631. i40e_fdir_reinit_subtask(pf);
  6632. if (pf->flags & I40E_FLAG_CLIENT_RESET) {
  6633. /* Client subtask will reopen next time through. */
  6634. i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], true);
  6635. pf->flags &= ~I40E_FLAG_CLIENT_RESET;
  6636. } else {
  6637. i40e_client_subtask(pf);
  6638. if (pf->flags & I40E_FLAG_CLIENT_L2_CHANGE) {
  6639. i40e_notify_client_of_l2_param_changes(
  6640. pf->vsi[pf->lan_vsi]);
  6641. pf->flags &= ~I40E_FLAG_CLIENT_L2_CHANGE;
  6642. }
  6643. }
  6644. i40e_sync_filters_subtask(pf);
  6645. i40e_sync_udp_filters_subtask(pf);
  6646. i40e_clean_adminq_subtask(pf);
  6647. /* flush memory to make sure state is correct before next watchdog */
  6648. smp_mb__before_atomic();
  6649. clear_bit(__I40E_SERVICE_SCHED, pf->state);
  6650. /* If the tasks have taken longer than one timer cycle or there
  6651. * is more work to be done, reschedule the service task now
  6652. * rather than wait for the timer to tick again.
  6653. */
  6654. if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
  6655. test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state) ||
  6656. test_bit(__I40E_MDD_EVENT_PENDING, pf->state) ||
  6657. test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
  6658. i40e_service_event_schedule(pf);
  6659. }
  6660. /**
  6661. * i40e_service_timer - timer callback
  6662. * @data: pointer to PF struct
  6663. **/
  6664. static void i40e_service_timer(unsigned long data)
  6665. {
  6666. struct i40e_pf *pf = (struct i40e_pf *)data;
  6667. mod_timer(&pf->service_timer,
  6668. round_jiffies(jiffies + pf->service_timer_period));
  6669. i40e_service_event_schedule(pf);
  6670. }
  6671. /**
  6672. * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
  6673. * @vsi: the VSI being configured
  6674. **/
  6675. static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
  6676. {
  6677. struct i40e_pf *pf = vsi->back;
  6678. switch (vsi->type) {
  6679. case I40E_VSI_MAIN:
  6680. vsi->alloc_queue_pairs = pf->num_lan_qps;
  6681. vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  6682. I40E_REQ_DESCRIPTOR_MULTIPLE);
  6683. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  6684. vsi->num_q_vectors = pf->num_lan_msix;
  6685. else
  6686. vsi->num_q_vectors = 1;
  6687. break;
  6688. case I40E_VSI_FDIR:
  6689. vsi->alloc_queue_pairs = 1;
  6690. vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
  6691. I40E_REQ_DESCRIPTOR_MULTIPLE);
  6692. vsi->num_q_vectors = pf->num_fdsb_msix;
  6693. break;
  6694. case I40E_VSI_VMDQ2:
  6695. vsi->alloc_queue_pairs = pf->num_vmdq_qps;
  6696. vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  6697. I40E_REQ_DESCRIPTOR_MULTIPLE);
  6698. vsi->num_q_vectors = pf->num_vmdq_msix;
  6699. break;
  6700. case I40E_VSI_SRIOV:
  6701. vsi->alloc_queue_pairs = pf->num_vf_qps;
  6702. vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  6703. I40E_REQ_DESCRIPTOR_MULTIPLE);
  6704. break;
  6705. default:
  6706. WARN_ON(1);
  6707. return -ENODATA;
  6708. }
  6709. return 0;
  6710. }
  6711. /**
  6712. * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
  6713. * @type: VSI pointer
  6714. * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
  6715. *
  6716. * On error: returns error code (negative)
  6717. * On success: returns 0
  6718. **/
  6719. static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
  6720. {
  6721. struct i40e_ring **next_rings;
  6722. int size;
  6723. int ret = 0;
  6724. /* allocate memory for both Tx, XDP Tx and Rx ring pointers */
  6725. size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
  6726. (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
  6727. vsi->tx_rings = kzalloc(size, GFP_KERNEL);
  6728. if (!vsi->tx_rings)
  6729. return -ENOMEM;
  6730. next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
  6731. if (i40e_enabled_xdp_vsi(vsi)) {
  6732. vsi->xdp_rings = next_rings;
  6733. next_rings += vsi->alloc_queue_pairs;
  6734. }
  6735. vsi->rx_rings = next_rings;
  6736. if (alloc_qvectors) {
  6737. /* allocate memory for q_vector pointers */
  6738. size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
  6739. vsi->q_vectors = kzalloc(size, GFP_KERNEL);
  6740. if (!vsi->q_vectors) {
  6741. ret = -ENOMEM;
  6742. goto err_vectors;
  6743. }
  6744. }
  6745. return ret;
  6746. err_vectors:
  6747. kfree(vsi->tx_rings);
  6748. return ret;
  6749. }
  6750. /**
  6751. * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
  6752. * @pf: board private structure
  6753. * @type: type of VSI
  6754. *
  6755. * On error: returns error code (negative)
  6756. * On success: returns vsi index in PF (positive)
  6757. **/
  6758. static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
  6759. {
  6760. int ret = -ENODEV;
  6761. struct i40e_vsi *vsi;
  6762. int vsi_idx;
  6763. int i;
  6764. /* Need to protect the allocation of the VSIs at the PF level */
  6765. mutex_lock(&pf->switch_mutex);
  6766. /* VSI list may be fragmented if VSI creation/destruction has
  6767. * been happening. We can afford to do a quick scan to look
  6768. * for any free VSIs in the list.
  6769. *
  6770. * find next empty vsi slot, looping back around if necessary
  6771. */
  6772. i = pf->next_vsi;
  6773. while (i < pf->num_alloc_vsi && pf->vsi[i])
  6774. i++;
  6775. if (i >= pf->num_alloc_vsi) {
  6776. i = 0;
  6777. while (i < pf->next_vsi && pf->vsi[i])
  6778. i++;
  6779. }
  6780. if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
  6781. vsi_idx = i; /* Found one! */
  6782. } else {
  6783. ret = -ENODEV;
  6784. goto unlock_pf; /* out of VSI slots! */
  6785. }
  6786. pf->next_vsi = ++i;
  6787. vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
  6788. if (!vsi) {
  6789. ret = -ENOMEM;
  6790. goto unlock_pf;
  6791. }
  6792. vsi->type = type;
  6793. vsi->back = pf;
  6794. set_bit(__I40E_VSI_DOWN, vsi->state);
  6795. vsi->flags = 0;
  6796. vsi->idx = vsi_idx;
  6797. vsi->int_rate_limit = 0;
  6798. vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
  6799. pf->rss_table_size : 64;
  6800. vsi->netdev_registered = false;
  6801. vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
  6802. hash_init(vsi->mac_filter_hash);
  6803. vsi->irqs_ready = false;
  6804. ret = i40e_set_num_rings_in_vsi(vsi);
  6805. if (ret)
  6806. goto err_rings;
  6807. ret = i40e_vsi_alloc_arrays(vsi, true);
  6808. if (ret)
  6809. goto err_rings;
  6810. /* Setup default MSIX irq handler for VSI */
  6811. i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
  6812. /* Initialize VSI lock */
  6813. spin_lock_init(&vsi->mac_filter_hash_lock);
  6814. pf->vsi[vsi_idx] = vsi;
  6815. ret = vsi_idx;
  6816. goto unlock_pf;
  6817. err_rings:
  6818. pf->next_vsi = i - 1;
  6819. kfree(vsi);
  6820. unlock_pf:
  6821. mutex_unlock(&pf->switch_mutex);
  6822. return ret;
  6823. }
  6824. /**
  6825. * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
  6826. * @type: VSI pointer
  6827. * @free_qvectors: a bool to specify if q_vectors need to be freed.
  6828. *
  6829. * On error: returns error code (negative)
  6830. * On success: returns 0
  6831. **/
  6832. static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
  6833. {
  6834. /* free the ring and vector containers */
  6835. if (free_qvectors) {
  6836. kfree(vsi->q_vectors);
  6837. vsi->q_vectors = NULL;
  6838. }
  6839. kfree(vsi->tx_rings);
  6840. vsi->tx_rings = NULL;
  6841. vsi->rx_rings = NULL;
  6842. vsi->xdp_rings = NULL;
  6843. }
  6844. /**
  6845. * i40e_clear_rss_config_user - clear the user configured RSS hash keys
  6846. * and lookup table
  6847. * @vsi: Pointer to VSI structure
  6848. */
  6849. static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
  6850. {
  6851. if (!vsi)
  6852. return;
  6853. kfree(vsi->rss_hkey_user);
  6854. vsi->rss_hkey_user = NULL;
  6855. kfree(vsi->rss_lut_user);
  6856. vsi->rss_lut_user = NULL;
  6857. }
  6858. /**
  6859. * i40e_vsi_clear - Deallocate the VSI provided
  6860. * @vsi: the VSI being un-configured
  6861. **/
  6862. static int i40e_vsi_clear(struct i40e_vsi *vsi)
  6863. {
  6864. struct i40e_pf *pf;
  6865. if (!vsi)
  6866. return 0;
  6867. if (!vsi->back)
  6868. goto free_vsi;
  6869. pf = vsi->back;
  6870. mutex_lock(&pf->switch_mutex);
  6871. if (!pf->vsi[vsi->idx]) {
  6872. dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
  6873. vsi->idx, vsi->idx, vsi, vsi->type);
  6874. goto unlock_vsi;
  6875. }
  6876. if (pf->vsi[vsi->idx] != vsi) {
  6877. dev_err(&pf->pdev->dev,
  6878. "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
  6879. pf->vsi[vsi->idx]->idx,
  6880. pf->vsi[vsi->idx],
  6881. pf->vsi[vsi->idx]->type,
  6882. vsi->idx, vsi, vsi->type);
  6883. goto unlock_vsi;
  6884. }
  6885. /* updates the PF for this cleared vsi */
  6886. i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
  6887. i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
  6888. i40e_vsi_free_arrays(vsi, true);
  6889. i40e_clear_rss_config_user(vsi);
  6890. pf->vsi[vsi->idx] = NULL;
  6891. if (vsi->idx < pf->next_vsi)
  6892. pf->next_vsi = vsi->idx;
  6893. unlock_vsi:
  6894. mutex_unlock(&pf->switch_mutex);
  6895. free_vsi:
  6896. kfree(vsi);
  6897. return 0;
  6898. }
  6899. /**
  6900. * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
  6901. * @vsi: the VSI being cleaned
  6902. **/
  6903. static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
  6904. {
  6905. int i;
  6906. if (vsi->tx_rings && vsi->tx_rings[0]) {
  6907. for (i = 0; i < vsi->alloc_queue_pairs; i++) {
  6908. kfree_rcu(vsi->tx_rings[i], rcu);
  6909. vsi->tx_rings[i] = NULL;
  6910. vsi->rx_rings[i] = NULL;
  6911. if (vsi->xdp_rings)
  6912. vsi->xdp_rings[i] = NULL;
  6913. }
  6914. }
  6915. }
  6916. /**
  6917. * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
  6918. * @vsi: the VSI being configured
  6919. **/
  6920. static int i40e_alloc_rings(struct i40e_vsi *vsi)
  6921. {
  6922. int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
  6923. struct i40e_pf *pf = vsi->back;
  6924. struct i40e_ring *ring;
  6925. /* Set basic values in the rings to be used later during open() */
  6926. for (i = 0; i < vsi->alloc_queue_pairs; i++) {
  6927. /* allocate space for both Tx and Rx in one shot */
  6928. ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
  6929. if (!ring)
  6930. goto err_out;
  6931. ring->queue_index = i;
  6932. ring->reg_idx = vsi->base_queue + i;
  6933. ring->ring_active = false;
  6934. ring->vsi = vsi;
  6935. ring->netdev = vsi->netdev;
  6936. ring->dev = &pf->pdev->dev;
  6937. ring->count = vsi->num_desc;
  6938. ring->size = 0;
  6939. ring->dcb_tc = 0;
  6940. if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
  6941. ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
  6942. ring->tx_itr_setting = pf->tx_itr_default;
  6943. vsi->tx_rings[i] = ring++;
  6944. if (!i40e_enabled_xdp_vsi(vsi))
  6945. goto setup_rx;
  6946. ring->queue_index = vsi->alloc_queue_pairs + i;
  6947. ring->reg_idx = vsi->base_queue + ring->queue_index;
  6948. ring->ring_active = false;
  6949. ring->vsi = vsi;
  6950. ring->netdev = NULL;
  6951. ring->dev = &pf->pdev->dev;
  6952. ring->count = vsi->num_desc;
  6953. ring->size = 0;
  6954. ring->dcb_tc = 0;
  6955. if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
  6956. ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
  6957. set_ring_xdp(ring);
  6958. ring->tx_itr_setting = pf->tx_itr_default;
  6959. vsi->xdp_rings[i] = ring++;
  6960. setup_rx:
  6961. ring->queue_index = i;
  6962. ring->reg_idx = vsi->base_queue + i;
  6963. ring->ring_active = false;
  6964. ring->vsi = vsi;
  6965. ring->netdev = vsi->netdev;
  6966. ring->dev = &pf->pdev->dev;
  6967. ring->count = vsi->num_desc;
  6968. ring->size = 0;
  6969. ring->dcb_tc = 0;
  6970. ring->rx_itr_setting = pf->rx_itr_default;
  6971. vsi->rx_rings[i] = ring;
  6972. }
  6973. return 0;
  6974. err_out:
  6975. i40e_vsi_clear_rings(vsi);
  6976. return -ENOMEM;
  6977. }
  6978. /**
  6979. * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
  6980. * @pf: board private structure
  6981. * @vectors: the number of MSI-X vectors to request
  6982. *
  6983. * Returns the number of vectors reserved, or error
  6984. **/
  6985. static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
  6986. {
  6987. vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
  6988. I40E_MIN_MSIX, vectors);
  6989. if (vectors < 0) {
  6990. dev_info(&pf->pdev->dev,
  6991. "MSI-X vector reservation failed: %d\n", vectors);
  6992. vectors = 0;
  6993. }
  6994. return vectors;
  6995. }
  6996. /**
  6997. * i40e_init_msix - Setup the MSIX capability
  6998. * @pf: board private structure
  6999. *
  7000. * Work with the OS to set up the MSIX vectors needed.
  7001. *
  7002. * Returns the number of vectors reserved or negative on failure
  7003. **/
  7004. static int i40e_init_msix(struct i40e_pf *pf)
  7005. {
  7006. struct i40e_hw *hw = &pf->hw;
  7007. int cpus, extra_vectors;
  7008. int vectors_left;
  7009. int v_budget, i;
  7010. int v_actual;
  7011. int iwarp_requested = 0;
  7012. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
  7013. return -ENODEV;
  7014. /* The number of vectors we'll request will be comprised of:
  7015. * - Add 1 for "other" cause for Admin Queue events, etc.
  7016. * - The number of LAN queue pairs
  7017. * - Queues being used for RSS.
  7018. * We don't need as many as max_rss_size vectors.
  7019. * use rss_size instead in the calculation since that
  7020. * is governed by number of cpus in the system.
  7021. * - assumes symmetric Tx/Rx pairing
  7022. * - The number of VMDq pairs
  7023. * - The CPU count within the NUMA node if iWARP is enabled
  7024. * Once we count this up, try the request.
  7025. *
  7026. * If we can't get what we want, we'll simplify to nearly nothing
  7027. * and try again. If that still fails, we punt.
  7028. */
  7029. vectors_left = hw->func_caps.num_msix_vectors;
  7030. v_budget = 0;
  7031. /* reserve one vector for miscellaneous handler */
  7032. if (vectors_left) {
  7033. v_budget++;
  7034. vectors_left--;
  7035. }
  7036. /* reserve some vectors for the main PF traffic queues. Initially we
  7037. * only reserve at most 50% of the available vectors, in the case that
  7038. * the number of online CPUs is large. This ensures that we can enable
  7039. * extra features as well. Once we've enabled the other features, we
  7040. * will use any remaining vectors to reach as close as we can to the
  7041. * number of online CPUs.
  7042. */
  7043. cpus = num_online_cpus();
  7044. pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
  7045. vectors_left -= pf->num_lan_msix;
  7046. /* reserve one vector for sideband flow director */
  7047. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  7048. if (vectors_left) {
  7049. pf->num_fdsb_msix = 1;
  7050. v_budget++;
  7051. vectors_left--;
  7052. } else {
  7053. pf->num_fdsb_msix = 0;
  7054. }
  7055. }
  7056. /* can we reserve enough for iWARP? */
  7057. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  7058. iwarp_requested = pf->num_iwarp_msix;
  7059. if (!vectors_left)
  7060. pf->num_iwarp_msix = 0;
  7061. else if (vectors_left < pf->num_iwarp_msix)
  7062. pf->num_iwarp_msix = 1;
  7063. v_budget += pf->num_iwarp_msix;
  7064. vectors_left -= pf->num_iwarp_msix;
  7065. }
  7066. /* any vectors left over go for VMDq support */
  7067. if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
  7068. int vmdq_vecs_wanted = pf->num_vmdq_vsis * pf->num_vmdq_qps;
  7069. int vmdq_vecs = min_t(int, vectors_left, vmdq_vecs_wanted);
  7070. if (!vectors_left) {
  7071. pf->num_vmdq_msix = 0;
  7072. pf->num_vmdq_qps = 0;
  7073. } else {
  7074. /* if we're short on vectors for what's desired, we limit
  7075. * the queues per vmdq. If this is still more than are
  7076. * available, the user will need to change the number of
  7077. * queues/vectors used by the PF later with the ethtool
  7078. * channels command
  7079. */
  7080. if (vmdq_vecs < vmdq_vecs_wanted)
  7081. pf->num_vmdq_qps = 1;
  7082. pf->num_vmdq_msix = pf->num_vmdq_qps;
  7083. v_budget += vmdq_vecs;
  7084. vectors_left -= vmdq_vecs;
  7085. }
  7086. }
  7087. /* On systems with a large number of SMP cores, we previously limited
  7088. * the number of vectors for num_lan_msix to be at most 50% of the
  7089. * available vectors, to allow for other features. Now, we add back
  7090. * the remaining vectors. However, we ensure that the total
  7091. * num_lan_msix will not exceed num_online_cpus(). To do this, we
  7092. * calculate the number of vectors we can add without going over the
  7093. * cap of CPUs. For systems with a small number of CPUs this will be
  7094. * zero.
  7095. */
  7096. extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
  7097. pf->num_lan_msix += extra_vectors;
  7098. vectors_left -= extra_vectors;
  7099. WARN(vectors_left < 0,
  7100. "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
  7101. v_budget += pf->num_lan_msix;
  7102. pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
  7103. GFP_KERNEL);
  7104. if (!pf->msix_entries)
  7105. return -ENOMEM;
  7106. for (i = 0; i < v_budget; i++)
  7107. pf->msix_entries[i].entry = i;
  7108. v_actual = i40e_reserve_msix_vectors(pf, v_budget);
  7109. if (v_actual < I40E_MIN_MSIX) {
  7110. pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
  7111. kfree(pf->msix_entries);
  7112. pf->msix_entries = NULL;
  7113. pci_disable_msix(pf->pdev);
  7114. return -ENODEV;
  7115. } else if (v_actual == I40E_MIN_MSIX) {
  7116. /* Adjust for minimal MSIX use */
  7117. pf->num_vmdq_vsis = 0;
  7118. pf->num_vmdq_qps = 0;
  7119. pf->num_lan_qps = 1;
  7120. pf->num_lan_msix = 1;
  7121. } else if (!vectors_left) {
  7122. /* If we have limited resources, we will start with no vectors
  7123. * for the special features and then allocate vectors to some
  7124. * of these features based on the policy and at the end disable
  7125. * the features that did not get any vectors.
  7126. */
  7127. int vec;
  7128. dev_info(&pf->pdev->dev,
  7129. "MSI-X vector limit reached, attempting to redistribute vectors\n");
  7130. /* reserve the misc vector */
  7131. vec = v_actual - 1;
  7132. /* Scale vector usage down */
  7133. pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
  7134. pf->num_vmdq_vsis = 1;
  7135. pf->num_vmdq_qps = 1;
  7136. /* partition out the remaining vectors */
  7137. switch (vec) {
  7138. case 2:
  7139. pf->num_lan_msix = 1;
  7140. break;
  7141. case 3:
  7142. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  7143. pf->num_lan_msix = 1;
  7144. pf->num_iwarp_msix = 1;
  7145. } else {
  7146. pf->num_lan_msix = 2;
  7147. }
  7148. break;
  7149. default:
  7150. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  7151. pf->num_iwarp_msix = min_t(int, (vec / 3),
  7152. iwarp_requested);
  7153. pf->num_vmdq_vsis = min_t(int, (vec / 3),
  7154. I40E_DEFAULT_NUM_VMDQ_VSI);
  7155. } else {
  7156. pf->num_vmdq_vsis = min_t(int, (vec / 2),
  7157. I40E_DEFAULT_NUM_VMDQ_VSI);
  7158. }
  7159. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  7160. pf->num_fdsb_msix = 1;
  7161. vec--;
  7162. }
  7163. pf->num_lan_msix = min_t(int,
  7164. (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
  7165. pf->num_lan_msix);
  7166. pf->num_lan_qps = pf->num_lan_msix;
  7167. break;
  7168. }
  7169. }
  7170. if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
  7171. (pf->num_fdsb_msix == 0)) {
  7172. dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
  7173. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  7174. }
  7175. if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
  7176. (pf->num_vmdq_msix == 0)) {
  7177. dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
  7178. pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
  7179. }
  7180. if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
  7181. (pf->num_iwarp_msix == 0)) {
  7182. dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
  7183. pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
  7184. }
  7185. i40e_debug(&pf->hw, I40E_DEBUG_INIT,
  7186. "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
  7187. pf->num_lan_msix,
  7188. pf->num_vmdq_msix * pf->num_vmdq_vsis,
  7189. pf->num_fdsb_msix,
  7190. pf->num_iwarp_msix);
  7191. return v_actual;
  7192. }
  7193. /**
  7194. * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
  7195. * @vsi: the VSI being configured
  7196. * @v_idx: index of the vector in the vsi struct
  7197. * @cpu: cpu to be used on affinity_mask
  7198. *
  7199. * We allocate one q_vector. If allocation fails we return -ENOMEM.
  7200. **/
  7201. static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx, int cpu)
  7202. {
  7203. struct i40e_q_vector *q_vector;
  7204. /* allocate q_vector */
  7205. q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
  7206. if (!q_vector)
  7207. return -ENOMEM;
  7208. q_vector->vsi = vsi;
  7209. q_vector->v_idx = v_idx;
  7210. cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
  7211. if (vsi->netdev)
  7212. netif_napi_add(vsi->netdev, &q_vector->napi,
  7213. i40e_napi_poll, NAPI_POLL_WEIGHT);
  7214. q_vector->rx.latency_range = I40E_LOW_LATENCY;
  7215. q_vector->tx.latency_range = I40E_LOW_LATENCY;
  7216. /* tie q_vector and vsi together */
  7217. vsi->q_vectors[v_idx] = q_vector;
  7218. return 0;
  7219. }
  7220. /**
  7221. * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
  7222. * @vsi: the VSI being configured
  7223. *
  7224. * We allocate one q_vector per queue interrupt. If allocation fails we
  7225. * return -ENOMEM.
  7226. **/
  7227. static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
  7228. {
  7229. struct i40e_pf *pf = vsi->back;
  7230. int err, v_idx, num_q_vectors, current_cpu;
  7231. /* if not MSIX, give the one vector only to the LAN VSI */
  7232. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  7233. num_q_vectors = vsi->num_q_vectors;
  7234. else if (vsi == pf->vsi[pf->lan_vsi])
  7235. num_q_vectors = 1;
  7236. else
  7237. return -EINVAL;
  7238. current_cpu = cpumask_first(cpu_online_mask);
  7239. for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
  7240. err = i40e_vsi_alloc_q_vector(vsi, v_idx, current_cpu);
  7241. if (err)
  7242. goto err_out;
  7243. current_cpu = cpumask_next(current_cpu, cpu_online_mask);
  7244. if (unlikely(current_cpu >= nr_cpu_ids))
  7245. current_cpu = cpumask_first(cpu_online_mask);
  7246. }
  7247. return 0;
  7248. err_out:
  7249. while (v_idx--)
  7250. i40e_free_q_vector(vsi, v_idx);
  7251. return err;
  7252. }
  7253. /**
  7254. * i40e_init_interrupt_scheme - Determine proper interrupt scheme
  7255. * @pf: board private structure to initialize
  7256. **/
  7257. static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
  7258. {
  7259. int vectors = 0;
  7260. ssize_t size;
  7261. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  7262. vectors = i40e_init_msix(pf);
  7263. if (vectors < 0) {
  7264. pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
  7265. I40E_FLAG_IWARP_ENABLED |
  7266. I40E_FLAG_RSS_ENABLED |
  7267. I40E_FLAG_DCB_CAPABLE |
  7268. I40E_FLAG_DCB_ENABLED |
  7269. I40E_FLAG_SRIOV_ENABLED |
  7270. I40E_FLAG_FD_SB_ENABLED |
  7271. I40E_FLAG_FD_ATR_ENABLED |
  7272. I40E_FLAG_VMDQ_ENABLED);
  7273. /* rework the queue expectations without MSIX */
  7274. i40e_determine_queue_usage(pf);
  7275. }
  7276. }
  7277. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
  7278. (pf->flags & I40E_FLAG_MSI_ENABLED)) {
  7279. dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
  7280. vectors = pci_enable_msi(pf->pdev);
  7281. if (vectors < 0) {
  7282. dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
  7283. vectors);
  7284. pf->flags &= ~I40E_FLAG_MSI_ENABLED;
  7285. }
  7286. vectors = 1; /* one MSI or Legacy vector */
  7287. }
  7288. if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
  7289. dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
  7290. /* set up vector assignment tracking */
  7291. size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
  7292. pf->irq_pile = kzalloc(size, GFP_KERNEL);
  7293. if (!pf->irq_pile) {
  7294. dev_err(&pf->pdev->dev, "error allocating irq_pile memory\n");
  7295. return -ENOMEM;
  7296. }
  7297. pf->irq_pile->num_entries = vectors;
  7298. pf->irq_pile->search_hint = 0;
  7299. /* track first vector for misc interrupts, ignore return */
  7300. (void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
  7301. return 0;
  7302. }
  7303. /**
  7304. * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
  7305. * @pf: board private structure
  7306. *
  7307. * This sets up the handler for MSIX 0, which is used to manage the
  7308. * non-queue interrupts, e.g. AdminQ and errors. This is not used
  7309. * when in MSI or Legacy interrupt mode.
  7310. **/
  7311. static int i40e_setup_misc_vector(struct i40e_pf *pf)
  7312. {
  7313. struct i40e_hw *hw = &pf->hw;
  7314. int err = 0;
  7315. /* Only request the irq if this is the first time through, and
  7316. * not when we're rebuilding after a Reset
  7317. */
  7318. if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) {
  7319. err = request_irq(pf->msix_entries[0].vector,
  7320. i40e_intr, 0, pf->int_name, pf);
  7321. if (err) {
  7322. dev_info(&pf->pdev->dev,
  7323. "request_irq for %s failed: %d\n",
  7324. pf->int_name, err);
  7325. return -EFAULT;
  7326. }
  7327. }
  7328. i40e_enable_misc_int_causes(pf);
  7329. /* associate no queues to the misc vector */
  7330. wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
  7331. wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
  7332. i40e_flush(hw);
  7333. i40e_irq_dynamic_enable_icr0(pf, true);
  7334. return err;
  7335. }
  7336. /**
  7337. * i40e_config_rss_aq - Prepare for RSS using AQ commands
  7338. * @vsi: vsi structure
  7339. * @seed: RSS hash seed
  7340. **/
  7341. static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
  7342. u8 *lut, u16 lut_size)
  7343. {
  7344. struct i40e_pf *pf = vsi->back;
  7345. struct i40e_hw *hw = &pf->hw;
  7346. int ret = 0;
  7347. if (seed) {
  7348. struct i40e_aqc_get_set_rss_key_data *seed_dw =
  7349. (struct i40e_aqc_get_set_rss_key_data *)seed;
  7350. ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
  7351. if (ret) {
  7352. dev_info(&pf->pdev->dev,
  7353. "Cannot set RSS key, err %s aq_err %s\n",
  7354. i40e_stat_str(hw, ret),
  7355. i40e_aq_str(hw, hw->aq.asq_last_status));
  7356. return ret;
  7357. }
  7358. }
  7359. if (lut) {
  7360. bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
  7361. ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
  7362. if (ret) {
  7363. dev_info(&pf->pdev->dev,
  7364. "Cannot set RSS lut, err %s aq_err %s\n",
  7365. i40e_stat_str(hw, ret),
  7366. i40e_aq_str(hw, hw->aq.asq_last_status));
  7367. return ret;
  7368. }
  7369. }
  7370. return ret;
  7371. }
  7372. /**
  7373. * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
  7374. * @vsi: Pointer to vsi structure
  7375. * @seed: Buffter to store the hash keys
  7376. * @lut: Buffer to store the lookup table entries
  7377. * @lut_size: Size of buffer to store the lookup table entries
  7378. *
  7379. * Return 0 on success, negative on failure
  7380. */
  7381. static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
  7382. u8 *lut, u16 lut_size)
  7383. {
  7384. struct i40e_pf *pf = vsi->back;
  7385. struct i40e_hw *hw = &pf->hw;
  7386. int ret = 0;
  7387. if (seed) {
  7388. ret = i40e_aq_get_rss_key(hw, vsi->id,
  7389. (struct i40e_aqc_get_set_rss_key_data *)seed);
  7390. if (ret) {
  7391. dev_info(&pf->pdev->dev,
  7392. "Cannot get RSS key, err %s aq_err %s\n",
  7393. i40e_stat_str(&pf->hw, ret),
  7394. i40e_aq_str(&pf->hw,
  7395. pf->hw.aq.asq_last_status));
  7396. return ret;
  7397. }
  7398. }
  7399. if (lut) {
  7400. bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
  7401. ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
  7402. if (ret) {
  7403. dev_info(&pf->pdev->dev,
  7404. "Cannot get RSS lut, err %s aq_err %s\n",
  7405. i40e_stat_str(&pf->hw, ret),
  7406. i40e_aq_str(&pf->hw,
  7407. pf->hw.aq.asq_last_status));
  7408. return ret;
  7409. }
  7410. }
  7411. return ret;
  7412. }
  7413. /**
  7414. * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
  7415. * @vsi: VSI structure
  7416. **/
  7417. static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
  7418. {
  7419. u8 seed[I40E_HKEY_ARRAY_SIZE];
  7420. struct i40e_pf *pf = vsi->back;
  7421. u8 *lut;
  7422. int ret;
  7423. if (!(pf->hw_features & I40E_HW_RSS_AQ_CAPABLE))
  7424. return 0;
  7425. if (!vsi->rss_size)
  7426. vsi->rss_size = min_t(int, pf->alloc_rss_size,
  7427. vsi->num_queue_pairs);
  7428. if (!vsi->rss_size)
  7429. return -EINVAL;
  7430. lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
  7431. if (!lut)
  7432. return -ENOMEM;
  7433. /* Use the user configured hash keys and lookup table if there is one,
  7434. * otherwise use default
  7435. */
  7436. if (vsi->rss_lut_user)
  7437. memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
  7438. else
  7439. i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
  7440. if (vsi->rss_hkey_user)
  7441. memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
  7442. else
  7443. netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
  7444. ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
  7445. kfree(lut);
  7446. return ret;
  7447. }
  7448. /**
  7449. * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
  7450. * @vsi: Pointer to vsi structure
  7451. * @seed: RSS hash seed
  7452. * @lut: Lookup table
  7453. * @lut_size: Lookup table size
  7454. *
  7455. * Returns 0 on success, negative on failure
  7456. **/
  7457. static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
  7458. const u8 *lut, u16 lut_size)
  7459. {
  7460. struct i40e_pf *pf = vsi->back;
  7461. struct i40e_hw *hw = &pf->hw;
  7462. u16 vf_id = vsi->vf_id;
  7463. u8 i;
  7464. /* Fill out hash function seed */
  7465. if (seed) {
  7466. u32 *seed_dw = (u32 *)seed;
  7467. if (vsi->type == I40E_VSI_MAIN) {
  7468. for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
  7469. wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
  7470. } else if (vsi->type == I40E_VSI_SRIOV) {
  7471. for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
  7472. wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
  7473. } else {
  7474. dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
  7475. }
  7476. }
  7477. if (lut) {
  7478. u32 *lut_dw = (u32 *)lut;
  7479. if (vsi->type == I40E_VSI_MAIN) {
  7480. if (lut_size != I40E_HLUT_ARRAY_SIZE)
  7481. return -EINVAL;
  7482. for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
  7483. wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
  7484. } else if (vsi->type == I40E_VSI_SRIOV) {
  7485. if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
  7486. return -EINVAL;
  7487. for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
  7488. wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
  7489. } else {
  7490. dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
  7491. }
  7492. }
  7493. i40e_flush(hw);
  7494. return 0;
  7495. }
  7496. /**
  7497. * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
  7498. * @vsi: Pointer to VSI structure
  7499. * @seed: Buffer to store the keys
  7500. * @lut: Buffer to store the lookup table entries
  7501. * @lut_size: Size of buffer to store the lookup table entries
  7502. *
  7503. * Returns 0 on success, negative on failure
  7504. */
  7505. static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
  7506. u8 *lut, u16 lut_size)
  7507. {
  7508. struct i40e_pf *pf = vsi->back;
  7509. struct i40e_hw *hw = &pf->hw;
  7510. u16 i;
  7511. if (seed) {
  7512. u32 *seed_dw = (u32 *)seed;
  7513. for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
  7514. seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
  7515. }
  7516. if (lut) {
  7517. u32 *lut_dw = (u32 *)lut;
  7518. if (lut_size != I40E_HLUT_ARRAY_SIZE)
  7519. return -EINVAL;
  7520. for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
  7521. lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
  7522. }
  7523. return 0;
  7524. }
  7525. /**
  7526. * i40e_config_rss - Configure RSS keys and lut
  7527. * @vsi: Pointer to VSI structure
  7528. * @seed: RSS hash seed
  7529. * @lut: Lookup table
  7530. * @lut_size: Lookup table size
  7531. *
  7532. * Returns 0 on success, negative on failure
  7533. */
  7534. int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
  7535. {
  7536. struct i40e_pf *pf = vsi->back;
  7537. if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
  7538. return i40e_config_rss_aq(vsi, seed, lut, lut_size);
  7539. else
  7540. return i40e_config_rss_reg(vsi, seed, lut, lut_size);
  7541. }
  7542. /**
  7543. * i40e_get_rss - Get RSS keys and lut
  7544. * @vsi: Pointer to VSI structure
  7545. * @seed: Buffer to store the keys
  7546. * @lut: Buffer to store the lookup table entries
  7547. * lut_size: Size of buffer to store the lookup table entries
  7548. *
  7549. * Returns 0 on success, negative on failure
  7550. */
  7551. int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
  7552. {
  7553. struct i40e_pf *pf = vsi->back;
  7554. if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
  7555. return i40e_get_rss_aq(vsi, seed, lut, lut_size);
  7556. else
  7557. return i40e_get_rss_reg(vsi, seed, lut, lut_size);
  7558. }
  7559. /**
  7560. * i40e_fill_rss_lut - Fill the RSS lookup table with default values
  7561. * @pf: Pointer to board private structure
  7562. * @lut: Lookup table
  7563. * @rss_table_size: Lookup table size
  7564. * @rss_size: Range of queue number for hashing
  7565. */
  7566. void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
  7567. u16 rss_table_size, u16 rss_size)
  7568. {
  7569. u16 i;
  7570. for (i = 0; i < rss_table_size; i++)
  7571. lut[i] = i % rss_size;
  7572. }
  7573. /**
  7574. * i40e_pf_config_rss - Prepare for RSS if used
  7575. * @pf: board private structure
  7576. **/
  7577. static int i40e_pf_config_rss(struct i40e_pf *pf)
  7578. {
  7579. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  7580. u8 seed[I40E_HKEY_ARRAY_SIZE];
  7581. u8 *lut;
  7582. struct i40e_hw *hw = &pf->hw;
  7583. u32 reg_val;
  7584. u64 hena;
  7585. int ret;
  7586. /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
  7587. hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
  7588. ((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
  7589. hena |= i40e_pf_get_default_rss_hena(pf);
  7590. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
  7591. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
  7592. /* Determine the RSS table size based on the hardware capabilities */
  7593. reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
  7594. reg_val = (pf->rss_table_size == 512) ?
  7595. (reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
  7596. (reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
  7597. i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
  7598. /* Determine the RSS size of the VSI */
  7599. if (!vsi->rss_size) {
  7600. u16 qcount;
  7601. qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
  7602. vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
  7603. }
  7604. if (!vsi->rss_size)
  7605. return -EINVAL;
  7606. lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
  7607. if (!lut)
  7608. return -ENOMEM;
  7609. /* Use user configured lut if there is one, otherwise use default */
  7610. if (vsi->rss_lut_user)
  7611. memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
  7612. else
  7613. i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
  7614. /* Use user configured hash key if there is one, otherwise
  7615. * use default.
  7616. */
  7617. if (vsi->rss_hkey_user)
  7618. memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
  7619. else
  7620. netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
  7621. ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
  7622. kfree(lut);
  7623. return ret;
  7624. }
  7625. /**
  7626. * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
  7627. * @pf: board private structure
  7628. * @queue_count: the requested queue count for rss.
  7629. *
  7630. * returns 0 if rss is not enabled, if enabled returns the final rss queue
  7631. * count which may be different from the requested queue count.
  7632. * Note: expects to be called while under rtnl_lock()
  7633. **/
  7634. int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
  7635. {
  7636. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  7637. int new_rss_size;
  7638. if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
  7639. return 0;
  7640. new_rss_size = min_t(int, queue_count, pf->rss_size_max);
  7641. if (queue_count != vsi->num_queue_pairs) {
  7642. u16 qcount;
  7643. vsi->req_queue_pairs = queue_count;
  7644. i40e_prep_for_reset(pf, true);
  7645. pf->alloc_rss_size = new_rss_size;
  7646. i40e_reset_and_rebuild(pf, true, true);
  7647. /* Discard the user configured hash keys and lut, if less
  7648. * queues are enabled.
  7649. */
  7650. if (queue_count < vsi->rss_size) {
  7651. i40e_clear_rss_config_user(vsi);
  7652. dev_dbg(&pf->pdev->dev,
  7653. "discard user configured hash keys and lut\n");
  7654. }
  7655. /* Reset vsi->rss_size, as number of enabled queues changed */
  7656. qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
  7657. vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
  7658. i40e_pf_config_rss(pf);
  7659. }
  7660. dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count: %d/%d\n",
  7661. vsi->req_queue_pairs, pf->rss_size_max);
  7662. return pf->alloc_rss_size;
  7663. }
  7664. /**
  7665. * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
  7666. * @pf: board private structure
  7667. **/
  7668. i40e_status i40e_get_partition_bw_setting(struct i40e_pf *pf)
  7669. {
  7670. i40e_status status;
  7671. bool min_valid, max_valid;
  7672. u32 max_bw, min_bw;
  7673. status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
  7674. &min_valid, &max_valid);
  7675. if (!status) {
  7676. if (min_valid)
  7677. pf->min_bw = min_bw;
  7678. if (max_valid)
  7679. pf->max_bw = max_bw;
  7680. }
  7681. return status;
  7682. }
  7683. /**
  7684. * i40e_set_partition_bw_setting - Set BW settings for this PF partition
  7685. * @pf: board private structure
  7686. **/
  7687. i40e_status i40e_set_partition_bw_setting(struct i40e_pf *pf)
  7688. {
  7689. struct i40e_aqc_configure_partition_bw_data bw_data;
  7690. i40e_status status;
  7691. /* Set the valid bit for this PF */
  7692. bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
  7693. bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
  7694. bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
  7695. /* Set the new bandwidths */
  7696. status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
  7697. return status;
  7698. }
  7699. /**
  7700. * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
  7701. * @pf: board private structure
  7702. **/
  7703. i40e_status i40e_commit_partition_bw_setting(struct i40e_pf *pf)
  7704. {
  7705. /* Commit temporary BW setting to permanent NVM image */
  7706. enum i40e_admin_queue_err last_aq_status;
  7707. i40e_status ret;
  7708. u16 nvm_word;
  7709. if (pf->hw.partition_id != 1) {
  7710. dev_info(&pf->pdev->dev,
  7711. "Commit BW only works on partition 1! This is partition %d",
  7712. pf->hw.partition_id);
  7713. ret = I40E_NOT_SUPPORTED;
  7714. goto bw_commit_out;
  7715. }
  7716. /* Acquire NVM for read access */
  7717. ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
  7718. last_aq_status = pf->hw.aq.asq_last_status;
  7719. if (ret) {
  7720. dev_info(&pf->pdev->dev,
  7721. "Cannot acquire NVM for read access, err %s aq_err %s\n",
  7722. i40e_stat_str(&pf->hw, ret),
  7723. i40e_aq_str(&pf->hw, last_aq_status));
  7724. goto bw_commit_out;
  7725. }
  7726. /* Read word 0x10 of NVM - SW compatibility word 1 */
  7727. ret = i40e_aq_read_nvm(&pf->hw,
  7728. I40E_SR_NVM_CONTROL_WORD,
  7729. 0x10, sizeof(nvm_word), &nvm_word,
  7730. false, NULL);
  7731. /* Save off last admin queue command status before releasing
  7732. * the NVM
  7733. */
  7734. last_aq_status = pf->hw.aq.asq_last_status;
  7735. i40e_release_nvm(&pf->hw);
  7736. if (ret) {
  7737. dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
  7738. i40e_stat_str(&pf->hw, ret),
  7739. i40e_aq_str(&pf->hw, last_aq_status));
  7740. goto bw_commit_out;
  7741. }
  7742. /* Wait a bit for NVM release to complete */
  7743. msleep(50);
  7744. /* Acquire NVM for write access */
  7745. ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
  7746. last_aq_status = pf->hw.aq.asq_last_status;
  7747. if (ret) {
  7748. dev_info(&pf->pdev->dev,
  7749. "Cannot acquire NVM for write access, err %s aq_err %s\n",
  7750. i40e_stat_str(&pf->hw, ret),
  7751. i40e_aq_str(&pf->hw, last_aq_status));
  7752. goto bw_commit_out;
  7753. }
  7754. /* Write it back out unchanged to initiate update NVM,
  7755. * which will force a write of the shadow (alt) RAM to
  7756. * the NVM - thus storing the bandwidth values permanently.
  7757. */
  7758. ret = i40e_aq_update_nvm(&pf->hw,
  7759. I40E_SR_NVM_CONTROL_WORD,
  7760. 0x10, sizeof(nvm_word),
  7761. &nvm_word, true, NULL);
  7762. /* Save off last admin queue command status before releasing
  7763. * the NVM
  7764. */
  7765. last_aq_status = pf->hw.aq.asq_last_status;
  7766. i40e_release_nvm(&pf->hw);
  7767. if (ret)
  7768. dev_info(&pf->pdev->dev,
  7769. "BW settings NOT SAVED, err %s aq_err %s\n",
  7770. i40e_stat_str(&pf->hw, ret),
  7771. i40e_aq_str(&pf->hw, last_aq_status));
  7772. bw_commit_out:
  7773. return ret;
  7774. }
  7775. /**
  7776. * i40e_sw_init - Initialize general software structures (struct i40e_pf)
  7777. * @pf: board private structure to initialize
  7778. *
  7779. * i40e_sw_init initializes the Adapter private data structure.
  7780. * Fields are initialized based on PCI device information and
  7781. * OS network device settings (MTU size).
  7782. **/
  7783. static int i40e_sw_init(struct i40e_pf *pf)
  7784. {
  7785. int err = 0;
  7786. int size;
  7787. /* Set default capability flags */
  7788. pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
  7789. I40E_FLAG_MSI_ENABLED |
  7790. I40E_FLAG_MSIX_ENABLED;
  7791. /* Set default ITR */
  7792. pf->rx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF;
  7793. pf->tx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF;
  7794. /* Depending on PF configurations, it is possible that the RSS
  7795. * maximum might end up larger than the available queues
  7796. */
  7797. pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
  7798. pf->alloc_rss_size = 1;
  7799. pf->rss_table_size = pf->hw.func_caps.rss_table_size;
  7800. pf->rss_size_max = min_t(int, pf->rss_size_max,
  7801. pf->hw.func_caps.num_tx_qp);
  7802. if (pf->hw.func_caps.rss) {
  7803. pf->flags |= I40E_FLAG_RSS_ENABLED;
  7804. pf->alloc_rss_size = min_t(int, pf->rss_size_max,
  7805. num_online_cpus());
  7806. }
  7807. /* MFP mode enabled */
  7808. if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
  7809. pf->flags |= I40E_FLAG_MFP_ENABLED;
  7810. dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
  7811. if (i40e_get_partition_bw_setting(pf)) {
  7812. dev_warn(&pf->pdev->dev,
  7813. "Could not get partition bw settings\n");
  7814. } else {
  7815. dev_info(&pf->pdev->dev,
  7816. "Partition BW Min = %8.8x, Max = %8.8x\n",
  7817. pf->min_bw, pf->max_bw);
  7818. /* nudge the Tx scheduler */
  7819. i40e_set_partition_bw_setting(pf);
  7820. }
  7821. }
  7822. if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
  7823. (pf->hw.func_caps.fd_filters_best_effort > 0)) {
  7824. pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
  7825. pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
  7826. if (pf->flags & I40E_FLAG_MFP_ENABLED &&
  7827. pf->hw.num_partitions > 1)
  7828. dev_info(&pf->pdev->dev,
  7829. "Flow Director Sideband mode Disabled in MFP mode\n");
  7830. else
  7831. pf->flags |= I40E_FLAG_FD_SB_ENABLED;
  7832. pf->fdir_pf_filter_count =
  7833. pf->hw.func_caps.fd_filters_guaranteed;
  7834. pf->hw.fdir_shared_filter_count =
  7835. pf->hw.func_caps.fd_filters_best_effort;
  7836. }
  7837. if (pf->hw.mac.type == I40E_MAC_X722) {
  7838. pf->hw_features |= (I40E_HW_RSS_AQ_CAPABLE |
  7839. I40E_HW_128_QP_RSS_CAPABLE |
  7840. I40E_HW_ATR_EVICT_CAPABLE |
  7841. I40E_HW_WB_ON_ITR_CAPABLE |
  7842. I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE |
  7843. I40E_HW_NO_PCI_LINK_CHECK |
  7844. I40E_HW_USE_SET_LLDP_MIB |
  7845. I40E_HW_GENEVE_OFFLOAD_CAPABLE |
  7846. I40E_HW_PTP_L4_CAPABLE |
  7847. I40E_HW_WOL_MC_MAGIC_PKT_WAKE |
  7848. I40E_HW_OUTER_UDP_CSUM_CAPABLE);
  7849. #define I40E_FDEVICT_PCTYPE_DEFAULT 0xc03
  7850. if (rd32(&pf->hw, I40E_GLQF_FDEVICTENA(1)) !=
  7851. I40E_FDEVICT_PCTYPE_DEFAULT) {
  7852. dev_warn(&pf->pdev->dev,
  7853. "FD EVICT PCTYPES are not right, disable FD HW EVICT\n");
  7854. pf->hw_features &= ~I40E_HW_ATR_EVICT_CAPABLE;
  7855. }
  7856. } else if ((pf->hw.aq.api_maj_ver > 1) ||
  7857. ((pf->hw.aq.api_maj_ver == 1) &&
  7858. (pf->hw.aq.api_min_ver > 4))) {
  7859. /* Supported in FW API version higher than 1.4 */
  7860. pf->hw_features |= I40E_HW_GENEVE_OFFLOAD_CAPABLE;
  7861. }
  7862. /* Enable HW ATR eviction if possible */
  7863. if (pf->hw_features & I40E_HW_ATR_EVICT_CAPABLE)
  7864. pf->flags |= I40E_FLAG_HW_ATR_EVICT_ENABLED;
  7865. if ((pf->hw.mac.type == I40E_MAC_XL710) &&
  7866. (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
  7867. (pf->hw.aq.fw_maj_ver < 4))) {
  7868. pf->hw_features |= I40E_HW_RESTART_AUTONEG;
  7869. /* No DCB support for FW < v4.33 */
  7870. pf->hw_features |= I40E_HW_NO_DCB_SUPPORT;
  7871. }
  7872. /* Disable FW LLDP if FW < v4.3 */
  7873. if ((pf->hw.mac.type == I40E_MAC_XL710) &&
  7874. (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
  7875. (pf->hw.aq.fw_maj_ver < 4)))
  7876. pf->hw_features |= I40E_HW_STOP_FW_LLDP;
  7877. /* Use the FW Set LLDP MIB API if FW > v4.40 */
  7878. if ((pf->hw.mac.type == I40E_MAC_XL710) &&
  7879. (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
  7880. (pf->hw.aq.fw_maj_ver >= 5)))
  7881. pf->hw_features |= I40E_HW_USE_SET_LLDP_MIB;
  7882. if (pf->hw.func_caps.vmdq) {
  7883. pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
  7884. pf->flags |= I40E_FLAG_VMDQ_ENABLED;
  7885. pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
  7886. }
  7887. if (pf->hw.func_caps.iwarp) {
  7888. pf->flags |= I40E_FLAG_IWARP_ENABLED;
  7889. /* IWARP needs one extra vector for CQP just like MISC.*/
  7890. pf->num_iwarp_msix = (int)num_online_cpus() + 1;
  7891. }
  7892. #ifdef CONFIG_PCI_IOV
  7893. if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
  7894. pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
  7895. pf->flags |= I40E_FLAG_SRIOV_ENABLED;
  7896. pf->num_req_vfs = min_t(int,
  7897. pf->hw.func_caps.num_vfs,
  7898. I40E_MAX_VF_COUNT);
  7899. }
  7900. #endif /* CONFIG_PCI_IOV */
  7901. pf->eeprom_version = 0xDEAD;
  7902. pf->lan_veb = I40E_NO_VEB;
  7903. pf->lan_vsi = I40E_NO_VSI;
  7904. /* By default FW has this off for performance reasons */
  7905. pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
  7906. /* set up queue assignment tracking */
  7907. size = sizeof(struct i40e_lump_tracking)
  7908. + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
  7909. pf->qp_pile = kzalloc(size, GFP_KERNEL);
  7910. if (!pf->qp_pile) {
  7911. err = -ENOMEM;
  7912. goto sw_init_done;
  7913. }
  7914. pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
  7915. pf->qp_pile->search_hint = 0;
  7916. pf->tx_timeout_recovery_level = 1;
  7917. mutex_init(&pf->switch_mutex);
  7918. sw_init_done:
  7919. return err;
  7920. }
  7921. /**
  7922. * i40e_set_ntuple - set the ntuple feature flag and take action
  7923. * @pf: board private structure to initialize
  7924. * @features: the feature set that the stack is suggesting
  7925. *
  7926. * returns a bool to indicate if reset needs to happen
  7927. **/
  7928. bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
  7929. {
  7930. bool need_reset = false;
  7931. /* Check if Flow Director n-tuple support was enabled or disabled. If
  7932. * the state changed, we need to reset.
  7933. */
  7934. if (features & NETIF_F_NTUPLE) {
  7935. /* Enable filters and mark for reset */
  7936. if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
  7937. need_reset = true;
  7938. /* enable FD_SB only if there is MSI-X vector */
  7939. if (pf->num_fdsb_msix > 0)
  7940. pf->flags |= I40E_FLAG_FD_SB_ENABLED;
  7941. } else {
  7942. /* turn off filters, mark for reset and clear SW filter list */
  7943. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  7944. need_reset = true;
  7945. i40e_fdir_filter_exit(pf);
  7946. }
  7947. pf->flags &= ~(I40E_FLAG_FD_SB_ENABLED |
  7948. I40E_FLAG_FD_SB_AUTO_DISABLED);
  7949. /* reset fd counters */
  7950. pf->fd_add_err = 0;
  7951. pf->fd_atr_cnt = 0;
  7952. /* if ATR was auto disabled it can be re-enabled. */
  7953. if (pf->flags & I40E_FLAG_FD_ATR_AUTO_DISABLED) {
  7954. pf->flags &= ~I40E_FLAG_FD_ATR_AUTO_DISABLED;
  7955. if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
  7956. (I40E_DEBUG_FD & pf->hw.debug_mask))
  7957. dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
  7958. }
  7959. }
  7960. return need_reset;
  7961. }
  7962. /**
  7963. * i40e_clear_rss_lut - clear the rx hash lookup table
  7964. * @vsi: the VSI being configured
  7965. **/
  7966. static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
  7967. {
  7968. struct i40e_pf *pf = vsi->back;
  7969. struct i40e_hw *hw = &pf->hw;
  7970. u16 vf_id = vsi->vf_id;
  7971. u8 i;
  7972. if (vsi->type == I40E_VSI_MAIN) {
  7973. for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
  7974. wr32(hw, I40E_PFQF_HLUT(i), 0);
  7975. } else if (vsi->type == I40E_VSI_SRIOV) {
  7976. for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
  7977. i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
  7978. } else {
  7979. dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
  7980. }
  7981. }
  7982. /**
  7983. * i40e_set_features - set the netdev feature flags
  7984. * @netdev: ptr to the netdev being adjusted
  7985. * @features: the feature set that the stack is suggesting
  7986. * Note: expects to be called while under rtnl_lock()
  7987. **/
  7988. static int i40e_set_features(struct net_device *netdev,
  7989. netdev_features_t features)
  7990. {
  7991. struct i40e_netdev_priv *np = netdev_priv(netdev);
  7992. struct i40e_vsi *vsi = np->vsi;
  7993. struct i40e_pf *pf = vsi->back;
  7994. bool need_reset;
  7995. if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
  7996. i40e_pf_config_rss(pf);
  7997. else if (!(features & NETIF_F_RXHASH) &&
  7998. netdev->features & NETIF_F_RXHASH)
  7999. i40e_clear_rss_lut(vsi);
  8000. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  8001. i40e_vlan_stripping_enable(vsi);
  8002. else
  8003. i40e_vlan_stripping_disable(vsi);
  8004. need_reset = i40e_set_ntuple(pf, features);
  8005. if (need_reset)
  8006. i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED), true);
  8007. return 0;
  8008. }
  8009. /**
  8010. * i40e_get_udp_port_idx - Lookup a possibly offloaded for Rx UDP port
  8011. * @pf: board private structure
  8012. * @port: The UDP port to look up
  8013. *
  8014. * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
  8015. **/
  8016. static u8 i40e_get_udp_port_idx(struct i40e_pf *pf, u16 port)
  8017. {
  8018. u8 i;
  8019. for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
  8020. if (pf->udp_ports[i].port == port)
  8021. return i;
  8022. }
  8023. return i;
  8024. }
  8025. /**
  8026. * i40e_udp_tunnel_add - Get notifications about UDP tunnel ports that come up
  8027. * @netdev: This physical port's netdev
  8028. * @ti: Tunnel endpoint information
  8029. **/
  8030. static void i40e_udp_tunnel_add(struct net_device *netdev,
  8031. struct udp_tunnel_info *ti)
  8032. {
  8033. struct i40e_netdev_priv *np = netdev_priv(netdev);
  8034. struct i40e_vsi *vsi = np->vsi;
  8035. struct i40e_pf *pf = vsi->back;
  8036. u16 port = ntohs(ti->port);
  8037. u8 next_idx;
  8038. u8 idx;
  8039. idx = i40e_get_udp_port_idx(pf, port);
  8040. /* Check if port already exists */
  8041. if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
  8042. netdev_info(netdev, "port %d already offloaded\n", port);
  8043. return;
  8044. }
  8045. /* Now check if there is space to add the new port */
  8046. next_idx = i40e_get_udp_port_idx(pf, 0);
  8047. if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
  8048. netdev_info(netdev, "maximum number of offloaded UDP ports reached, not adding port %d\n",
  8049. port);
  8050. return;
  8051. }
  8052. switch (ti->type) {
  8053. case UDP_TUNNEL_TYPE_VXLAN:
  8054. pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_VXLAN;
  8055. break;
  8056. case UDP_TUNNEL_TYPE_GENEVE:
  8057. if (!(pf->hw_features & I40E_HW_GENEVE_OFFLOAD_CAPABLE))
  8058. return;
  8059. pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_NGE;
  8060. break;
  8061. default:
  8062. return;
  8063. }
  8064. /* New port: add it and mark its index in the bitmap */
  8065. pf->udp_ports[next_idx].port = port;
  8066. pf->pending_udp_bitmap |= BIT_ULL(next_idx);
  8067. pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
  8068. }
  8069. /**
  8070. * i40e_udp_tunnel_del - Get notifications about UDP tunnel ports that go away
  8071. * @netdev: This physical port's netdev
  8072. * @ti: Tunnel endpoint information
  8073. **/
  8074. static void i40e_udp_tunnel_del(struct net_device *netdev,
  8075. struct udp_tunnel_info *ti)
  8076. {
  8077. struct i40e_netdev_priv *np = netdev_priv(netdev);
  8078. struct i40e_vsi *vsi = np->vsi;
  8079. struct i40e_pf *pf = vsi->back;
  8080. u16 port = ntohs(ti->port);
  8081. u8 idx;
  8082. idx = i40e_get_udp_port_idx(pf, port);
  8083. /* Check if port already exists */
  8084. if (idx >= I40E_MAX_PF_UDP_OFFLOAD_PORTS)
  8085. goto not_found;
  8086. switch (ti->type) {
  8087. case UDP_TUNNEL_TYPE_VXLAN:
  8088. if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_VXLAN)
  8089. goto not_found;
  8090. break;
  8091. case UDP_TUNNEL_TYPE_GENEVE:
  8092. if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_NGE)
  8093. goto not_found;
  8094. break;
  8095. default:
  8096. goto not_found;
  8097. }
  8098. /* if port exists, set it to 0 (mark for deletion)
  8099. * and make it pending
  8100. */
  8101. pf->udp_ports[idx].port = 0;
  8102. pf->pending_udp_bitmap |= BIT_ULL(idx);
  8103. pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
  8104. return;
  8105. not_found:
  8106. netdev_warn(netdev, "UDP port %d was not found, not deleting\n",
  8107. port);
  8108. }
  8109. static int i40e_get_phys_port_id(struct net_device *netdev,
  8110. struct netdev_phys_item_id *ppid)
  8111. {
  8112. struct i40e_netdev_priv *np = netdev_priv(netdev);
  8113. struct i40e_pf *pf = np->vsi->back;
  8114. struct i40e_hw *hw = &pf->hw;
  8115. if (!(pf->hw_features & I40E_HW_PORT_ID_VALID))
  8116. return -EOPNOTSUPP;
  8117. ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
  8118. memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
  8119. return 0;
  8120. }
  8121. /**
  8122. * i40e_ndo_fdb_add - add an entry to the hardware database
  8123. * @ndm: the input from the stack
  8124. * @tb: pointer to array of nladdr (unused)
  8125. * @dev: the net device pointer
  8126. * @addr: the MAC address entry being added
  8127. * @flags: instructions from stack about fdb operation
  8128. */
  8129. static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
  8130. struct net_device *dev,
  8131. const unsigned char *addr, u16 vid,
  8132. u16 flags)
  8133. {
  8134. struct i40e_netdev_priv *np = netdev_priv(dev);
  8135. struct i40e_pf *pf = np->vsi->back;
  8136. int err = 0;
  8137. if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
  8138. return -EOPNOTSUPP;
  8139. if (vid) {
  8140. pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
  8141. return -EINVAL;
  8142. }
  8143. /* Hardware does not support aging addresses so if a
  8144. * ndm_state is given only allow permanent addresses
  8145. */
  8146. if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
  8147. netdev_info(dev, "FDB only supports static addresses\n");
  8148. return -EINVAL;
  8149. }
  8150. if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
  8151. err = dev_uc_add_excl(dev, addr);
  8152. else if (is_multicast_ether_addr(addr))
  8153. err = dev_mc_add_excl(dev, addr);
  8154. else
  8155. err = -EINVAL;
  8156. /* Only return duplicate errors if NLM_F_EXCL is set */
  8157. if (err == -EEXIST && !(flags & NLM_F_EXCL))
  8158. err = 0;
  8159. return err;
  8160. }
  8161. /**
  8162. * i40e_ndo_bridge_setlink - Set the hardware bridge mode
  8163. * @dev: the netdev being configured
  8164. * @nlh: RTNL message
  8165. *
  8166. * Inserts a new hardware bridge if not already created and
  8167. * enables the bridging mode requested (VEB or VEPA). If the
  8168. * hardware bridge has already been inserted and the request
  8169. * is to change the mode then that requires a PF reset to
  8170. * allow rebuild of the components with required hardware
  8171. * bridge mode enabled.
  8172. *
  8173. * Note: expects to be called while under rtnl_lock()
  8174. **/
  8175. static int i40e_ndo_bridge_setlink(struct net_device *dev,
  8176. struct nlmsghdr *nlh,
  8177. u16 flags)
  8178. {
  8179. struct i40e_netdev_priv *np = netdev_priv(dev);
  8180. struct i40e_vsi *vsi = np->vsi;
  8181. struct i40e_pf *pf = vsi->back;
  8182. struct i40e_veb *veb = NULL;
  8183. struct nlattr *attr, *br_spec;
  8184. int i, rem;
  8185. /* Only for PF VSI for now */
  8186. if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
  8187. return -EOPNOTSUPP;
  8188. /* Find the HW bridge for PF VSI */
  8189. for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
  8190. if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
  8191. veb = pf->veb[i];
  8192. }
  8193. br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
  8194. nla_for_each_nested(attr, br_spec, rem) {
  8195. __u16 mode;
  8196. if (nla_type(attr) != IFLA_BRIDGE_MODE)
  8197. continue;
  8198. mode = nla_get_u16(attr);
  8199. if ((mode != BRIDGE_MODE_VEPA) &&
  8200. (mode != BRIDGE_MODE_VEB))
  8201. return -EINVAL;
  8202. /* Insert a new HW bridge */
  8203. if (!veb) {
  8204. veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
  8205. vsi->tc_config.enabled_tc);
  8206. if (veb) {
  8207. veb->bridge_mode = mode;
  8208. i40e_config_bridge_mode(veb);
  8209. } else {
  8210. /* No Bridge HW offload available */
  8211. return -ENOENT;
  8212. }
  8213. break;
  8214. } else if (mode != veb->bridge_mode) {
  8215. /* Existing HW bridge but different mode needs reset */
  8216. veb->bridge_mode = mode;
  8217. /* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
  8218. if (mode == BRIDGE_MODE_VEB)
  8219. pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
  8220. else
  8221. pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
  8222. i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED),
  8223. true);
  8224. break;
  8225. }
  8226. }
  8227. return 0;
  8228. }
  8229. /**
  8230. * i40e_ndo_bridge_getlink - Get the hardware bridge mode
  8231. * @skb: skb buff
  8232. * @pid: process id
  8233. * @seq: RTNL message seq #
  8234. * @dev: the netdev being configured
  8235. * @filter_mask: unused
  8236. * @nlflags: netlink flags passed in
  8237. *
  8238. * Return the mode in which the hardware bridge is operating in
  8239. * i.e VEB or VEPA.
  8240. **/
  8241. static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
  8242. struct net_device *dev,
  8243. u32 __always_unused filter_mask,
  8244. int nlflags)
  8245. {
  8246. struct i40e_netdev_priv *np = netdev_priv(dev);
  8247. struct i40e_vsi *vsi = np->vsi;
  8248. struct i40e_pf *pf = vsi->back;
  8249. struct i40e_veb *veb = NULL;
  8250. int i;
  8251. /* Only for PF VSI for now */
  8252. if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
  8253. return -EOPNOTSUPP;
  8254. /* Find the HW bridge for the PF VSI */
  8255. for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
  8256. if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
  8257. veb = pf->veb[i];
  8258. }
  8259. if (!veb)
  8260. return 0;
  8261. return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
  8262. 0, 0, nlflags, filter_mask, NULL);
  8263. }
  8264. /**
  8265. * i40e_features_check - Validate encapsulated packet conforms to limits
  8266. * @skb: skb buff
  8267. * @dev: This physical port's netdev
  8268. * @features: Offload features that the stack believes apply
  8269. **/
  8270. static netdev_features_t i40e_features_check(struct sk_buff *skb,
  8271. struct net_device *dev,
  8272. netdev_features_t features)
  8273. {
  8274. size_t len;
  8275. /* No point in doing any of this if neither checksum nor GSO are
  8276. * being requested for this frame. We can rule out both by just
  8277. * checking for CHECKSUM_PARTIAL
  8278. */
  8279. if (skb->ip_summed != CHECKSUM_PARTIAL)
  8280. return features;
  8281. /* We cannot support GSO if the MSS is going to be less than
  8282. * 64 bytes. If it is then we need to drop support for GSO.
  8283. */
  8284. if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
  8285. features &= ~NETIF_F_GSO_MASK;
  8286. /* MACLEN can support at most 63 words */
  8287. len = skb_network_header(skb) - skb->data;
  8288. if (len & ~(63 * 2))
  8289. goto out_err;
  8290. /* IPLEN and EIPLEN can support at most 127 dwords */
  8291. len = skb_transport_header(skb) - skb_network_header(skb);
  8292. if (len & ~(127 * 4))
  8293. goto out_err;
  8294. if (skb->encapsulation) {
  8295. /* L4TUNLEN can support 127 words */
  8296. len = skb_inner_network_header(skb) - skb_transport_header(skb);
  8297. if (len & ~(127 * 2))
  8298. goto out_err;
  8299. /* IPLEN can support at most 127 dwords */
  8300. len = skb_inner_transport_header(skb) -
  8301. skb_inner_network_header(skb);
  8302. if (len & ~(127 * 4))
  8303. goto out_err;
  8304. }
  8305. /* No need to validate L4LEN as TCP is the only protocol with a
  8306. * a flexible value and we support all possible values supported
  8307. * by TCP, which is at most 15 dwords
  8308. */
  8309. return features;
  8310. out_err:
  8311. return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
  8312. }
  8313. /**
  8314. * i40e_xdp_setup - add/remove an XDP program
  8315. * @vsi: VSI to changed
  8316. * @prog: XDP program
  8317. **/
  8318. static int i40e_xdp_setup(struct i40e_vsi *vsi,
  8319. struct bpf_prog *prog)
  8320. {
  8321. int frame_size = vsi->netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
  8322. struct i40e_pf *pf = vsi->back;
  8323. struct bpf_prog *old_prog;
  8324. bool need_reset;
  8325. int i;
  8326. /* Don't allow frames that span over multiple buffers */
  8327. if (frame_size > vsi->rx_buf_len)
  8328. return -EINVAL;
  8329. if (!i40e_enabled_xdp_vsi(vsi) && !prog)
  8330. return 0;
  8331. /* When turning XDP on->off/off->on we reset and rebuild the rings. */
  8332. need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
  8333. if (need_reset)
  8334. i40e_prep_for_reset(pf, true);
  8335. old_prog = xchg(&vsi->xdp_prog, prog);
  8336. if (need_reset)
  8337. i40e_reset_and_rebuild(pf, true, true);
  8338. for (i = 0; i < vsi->num_queue_pairs; i++)
  8339. WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
  8340. if (old_prog)
  8341. bpf_prog_put(old_prog);
  8342. return 0;
  8343. }
  8344. /**
  8345. * i40e_xdp - implements ndo_xdp for i40e
  8346. * @dev: netdevice
  8347. * @xdp: XDP command
  8348. **/
  8349. static int i40e_xdp(struct net_device *dev,
  8350. struct netdev_xdp *xdp)
  8351. {
  8352. struct i40e_netdev_priv *np = netdev_priv(dev);
  8353. struct i40e_vsi *vsi = np->vsi;
  8354. if (vsi->type != I40E_VSI_MAIN)
  8355. return -EINVAL;
  8356. switch (xdp->command) {
  8357. case XDP_SETUP_PROG:
  8358. return i40e_xdp_setup(vsi, xdp->prog);
  8359. case XDP_QUERY_PROG:
  8360. xdp->prog_attached = i40e_enabled_xdp_vsi(vsi);
  8361. xdp->prog_id = vsi->xdp_prog ? vsi->xdp_prog->aux->id : 0;
  8362. return 0;
  8363. default:
  8364. return -EINVAL;
  8365. }
  8366. }
  8367. static const struct net_device_ops i40e_netdev_ops = {
  8368. .ndo_open = i40e_open,
  8369. .ndo_stop = i40e_close,
  8370. .ndo_start_xmit = i40e_lan_xmit_frame,
  8371. .ndo_get_stats64 = i40e_get_netdev_stats_struct,
  8372. .ndo_set_rx_mode = i40e_set_rx_mode,
  8373. .ndo_validate_addr = eth_validate_addr,
  8374. .ndo_set_mac_address = i40e_set_mac,
  8375. .ndo_change_mtu = i40e_change_mtu,
  8376. .ndo_do_ioctl = i40e_ioctl,
  8377. .ndo_tx_timeout = i40e_tx_timeout,
  8378. .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
  8379. .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
  8380. #ifdef CONFIG_NET_POLL_CONTROLLER
  8381. .ndo_poll_controller = i40e_netpoll,
  8382. #endif
  8383. .ndo_setup_tc = __i40e_setup_tc,
  8384. .ndo_set_features = i40e_set_features,
  8385. .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
  8386. .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
  8387. .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
  8388. .ndo_get_vf_config = i40e_ndo_get_vf_config,
  8389. .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
  8390. .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofchk,
  8391. .ndo_set_vf_trust = i40e_ndo_set_vf_trust,
  8392. .ndo_udp_tunnel_add = i40e_udp_tunnel_add,
  8393. .ndo_udp_tunnel_del = i40e_udp_tunnel_del,
  8394. .ndo_get_phys_port_id = i40e_get_phys_port_id,
  8395. .ndo_fdb_add = i40e_ndo_fdb_add,
  8396. .ndo_features_check = i40e_features_check,
  8397. .ndo_bridge_getlink = i40e_ndo_bridge_getlink,
  8398. .ndo_bridge_setlink = i40e_ndo_bridge_setlink,
  8399. .ndo_xdp = i40e_xdp,
  8400. };
  8401. /**
  8402. * i40e_config_netdev - Setup the netdev flags
  8403. * @vsi: the VSI being configured
  8404. *
  8405. * Returns 0 on success, negative value on failure
  8406. **/
  8407. static int i40e_config_netdev(struct i40e_vsi *vsi)
  8408. {
  8409. struct i40e_pf *pf = vsi->back;
  8410. struct i40e_hw *hw = &pf->hw;
  8411. struct i40e_netdev_priv *np;
  8412. struct net_device *netdev;
  8413. u8 broadcast[ETH_ALEN];
  8414. u8 mac_addr[ETH_ALEN];
  8415. int etherdev_size;
  8416. netdev_features_t hw_enc_features;
  8417. netdev_features_t hw_features;
  8418. etherdev_size = sizeof(struct i40e_netdev_priv);
  8419. netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
  8420. if (!netdev)
  8421. return -ENOMEM;
  8422. vsi->netdev = netdev;
  8423. np = netdev_priv(netdev);
  8424. np->vsi = vsi;
  8425. hw_enc_features = NETIF_F_SG |
  8426. NETIF_F_IP_CSUM |
  8427. NETIF_F_IPV6_CSUM |
  8428. NETIF_F_HIGHDMA |
  8429. NETIF_F_SOFT_FEATURES |
  8430. NETIF_F_TSO |
  8431. NETIF_F_TSO_ECN |
  8432. NETIF_F_TSO6 |
  8433. NETIF_F_GSO_GRE |
  8434. NETIF_F_GSO_GRE_CSUM |
  8435. NETIF_F_GSO_PARTIAL |
  8436. NETIF_F_GSO_UDP_TUNNEL |
  8437. NETIF_F_GSO_UDP_TUNNEL_CSUM |
  8438. NETIF_F_SCTP_CRC |
  8439. NETIF_F_RXHASH |
  8440. NETIF_F_RXCSUM |
  8441. 0;
  8442. if (!(pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE))
  8443. netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
  8444. netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
  8445. netdev->hw_enc_features |= hw_enc_features;
  8446. /* record features VLANs can make use of */
  8447. netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
  8448. if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
  8449. netdev->hw_features |= NETIF_F_NTUPLE;
  8450. hw_features = hw_enc_features |
  8451. NETIF_F_HW_VLAN_CTAG_TX |
  8452. NETIF_F_HW_VLAN_CTAG_RX;
  8453. netdev->hw_features |= hw_features;
  8454. netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
  8455. netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
  8456. if (vsi->type == I40E_VSI_MAIN) {
  8457. SET_NETDEV_DEV(netdev, &pf->pdev->dev);
  8458. ether_addr_copy(mac_addr, hw->mac.perm_addr);
  8459. /* The following steps are necessary for two reasons. First,
  8460. * some older NVM configurations load a default MAC-VLAN
  8461. * filter that will accept any tagged packet, and we want to
  8462. * replace this with a normal filter. Additionally, it is
  8463. * possible our MAC address was provided by the platform using
  8464. * Open Firmware or similar.
  8465. *
  8466. * Thus, we need to remove the default filter and install one
  8467. * specific to the MAC address.
  8468. */
  8469. i40e_rm_default_mac_filter(vsi, mac_addr);
  8470. spin_lock_bh(&vsi->mac_filter_hash_lock);
  8471. i40e_add_mac_filter(vsi, mac_addr);
  8472. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  8473. } else {
  8474. /* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
  8475. * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
  8476. * the end, which is 4 bytes long, so force truncation of the
  8477. * original name by IFNAMSIZ - 4
  8478. */
  8479. snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d",
  8480. IFNAMSIZ - 4,
  8481. pf->vsi[pf->lan_vsi]->netdev->name);
  8482. random_ether_addr(mac_addr);
  8483. spin_lock_bh(&vsi->mac_filter_hash_lock);
  8484. i40e_add_mac_filter(vsi, mac_addr);
  8485. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  8486. }
  8487. /* Add the broadcast filter so that we initially will receive
  8488. * broadcast packets. Note that when a new VLAN is first added the
  8489. * driver will convert all filters marked I40E_VLAN_ANY into VLAN
  8490. * specific filters as part of transitioning into "vlan" operation.
  8491. * When more VLANs are added, the driver will copy each existing MAC
  8492. * filter and add it for the new VLAN.
  8493. *
  8494. * Broadcast filters are handled specially by
  8495. * i40e_sync_filters_subtask, as the driver must to set the broadcast
  8496. * promiscuous bit instead of adding this directly as a MAC/VLAN
  8497. * filter. The subtask will update the correct broadcast promiscuous
  8498. * bits as VLANs become active or inactive.
  8499. */
  8500. eth_broadcast_addr(broadcast);
  8501. spin_lock_bh(&vsi->mac_filter_hash_lock);
  8502. i40e_add_mac_filter(vsi, broadcast);
  8503. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  8504. ether_addr_copy(netdev->dev_addr, mac_addr);
  8505. ether_addr_copy(netdev->perm_addr, mac_addr);
  8506. netdev->priv_flags |= IFF_UNICAST_FLT;
  8507. netdev->priv_flags |= IFF_SUPP_NOFCS;
  8508. /* Setup netdev TC information */
  8509. i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
  8510. netdev->netdev_ops = &i40e_netdev_ops;
  8511. netdev->watchdog_timeo = 5 * HZ;
  8512. i40e_set_ethtool_ops(netdev);
  8513. /* MTU range: 68 - 9706 */
  8514. netdev->min_mtu = ETH_MIN_MTU;
  8515. netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
  8516. return 0;
  8517. }
  8518. /**
  8519. * i40e_vsi_delete - Delete a VSI from the switch
  8520. * @vsi: the VSI being removed
  8521. *
  8522. * Returns 0 on success, negative value on failure
  8523. **/
  8524. static void i40e_vsi_delete(struct i40e_vsi *vsi)
  8525. {
  8526. /* remove default VSI is not allowed */
  8527. if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
  8528. return;
  8529. i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
  8530. }
  8531. /**
  8532. * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
  8533. * @vsi: the VSI being queried
  8534. *
  8535. * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
  8536. **/
  8537. int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
  8538. {
  8539. struct i40e_veb *veb;
  8540. struct i40e_pf *pf = vsi->back;
  8541. /* Uplink is not a bridge so default to VEB */
  8542. if (vsi->veb_idx == I40E_NO_VEB)
  8543. return 1;
  8544. veb = pf->veb[vsi->veb_idx];
  8545. if (!veb) {
  8546. dev_info(&pf->pdev->dev,
  8547. "There is no veb associated with the bridge\n");
  8548. return -ENOENT;
  8549. }
  8550. /* Uplink is a bridge in VEPA mode */
  8551. if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
  8552. return 0;
  8553. } else {
  8554. /* Uplink is a bridge in VEB mode */
  8555. return 1;
  8556. }
  8557. /* VEPA is now default bridge, so return 0 */
  8558. return 0;
  8559. }
  8560. /**
  8561. * i40e_add_vsi - Add a VSI to the switch
  8562. * @vsi: the VSI being configured
  8563. *
  8564. * This initializes a VSI context depending on the VSI type to be added and
  8565. * passes it down to the add_vsi aq command.
  8566. **/
  8567. static int i40e_add_vsi(struct i40e_vsi *vsi)
  8568. {
  8569. int ret = -ENODEV;
  8570. struct i40e_pf *pf = vsi->back;
  8571. struct i40e_hw *hw = &pf->hw;
  8572. struct i40e_vsi_context ctxt;
  8573. struct i40e_mac_filter *f;
  8574. struct hlist_node *h;
  8575. int bkt;
  8576. u8 enabled_tc = 0x1; /* TC0 enabled */
  8577. int f_count = 0;
  8578. memset(&ctxt, 0, sizeof(ctxt));
  8579. switch (vsi->type) {
  8580. case I40E_VSI_MAIN:
  8581. /* The PF's main VSI is already setup as part of the
  8582. * device initialization, so we'll not bother with
  8583. * the add_vsi call, but we will retrieve the current
  8584. * VSI context.
  8585. */
  8586. ctxt.seid = pf->main_vsi_seid;
  8587. ctxt.pf_num = pf->hw.pf_id;
  8588. ctxt.vf_num = 0;
  8589. ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
  8590. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  8591. if (ret) {
  8592. dev_info(&pf->pdev->dev,
  8593. "couldn't get PF vsi config, err %s aq_err %s\n",
  8594. i40e_stat_str(&pf->hw, ret),
  8595. i40e_aq_str(&pf->hw,
  8596. pf->hw.aq.asq_last_status));
  8597. return -ENOENT;
  8598. }
  8599. vsi->info = ctxt.info;
  8600. vsi->info.valid_sections = 0;
  8601. vsi->seid = ctxt.seid;
  8602. vsi->id = ctxt.vsi_number;
  8603. enabled_tc = i40e_pf_get_tc_map(pf);
  8604. /* MFP mode setup queue map and update VSI */
  8605. if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
  8606. !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
  8607. memset(&ctxt, 0, sizeof(ctxt));
  8608. ctxt.seid = pf->main_vsi_seid;
  8609. ctxt.pf_num = pf->hw.pf_id;
  8610. ctxt.vf_num = 0;
  8611. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
  8612. ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
  8613. if (ret) {
  8614. dev_info(&pf->pdev->dev,
  8615. "update vsi failed, err %s aq_err %s\n",
  8616. i40e_stat_str(&pf->hw, ret),
  8617. i40e_aq_str(&pf->hw,
  8618. pf->hw.aq.asq_last_status));
  8619. ret = -ENOENT;
  8620. goto err;
  8621. }
  8622. /* update the local VSI info queue map */
  8623. i40e_vsi_update_queue_map(vsi, &ctxt);
  8624. vsi->info.valid_sections = 0;
  8625. } else {
  8626. /* Default/Main VSI is only enabled for TC0
  8627. * reconfigure it to enable all TCs that are
  8628. * available on the port in SFP mode.
  8629. * For MFP case the iSCSI PF would use this
  8630. * flow to enable LAN+iSCSI TC.
  8631. */
  8632. ret = i40e_vsi_config_tc(vsi, enabled_tc);
  8633. if (ret) {
  8634. /* Single TC condition is not fatal,
  8635. * message and continue
  8636. */
  8637. dev_info(&pf->pdev->dev,
  8638. "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
  8639. enabled_tc,
  8640. i40e_stat_str(&pf->hw, ret),
  8641. i40e_aq_str(&pf->hw,
  8642. pf->hw.aq.asq_last_status));
  8643. }
  8644. }
  8645. break;
  8646. case I40E_VSI_FDIR:
  8647. ctxt.pf_num = hw->pf_id;
  8648. ctxt.vf_num = 0;
  8649. ctxt.uplink_seid = vsi->uplink_seid;
  8650. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  8651. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  8652. if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
  8653. (i40e_is_vsi_uplink_mode_veb(vsi))) {
  8654. ctxt.info.valid_sections |=
  8655. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  8656. ctxt.info.switch_id =
  8657. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  8658. }
  8659. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
  8660. break;
  8661. case I40E_VSI_VMDQ2:
  8662. ctxt.pf_num = hw->pf_id;
  8663. ctxt.vf_num = 0;
  8664. ctxt.uplink_seid = vsi->uplink_seid;
  8665. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  8666. ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
  8667. /* This VSI is connected to VEB so the switch_id
  8668. * should be set to zero by default.
  8669. */
  8670. if (i40e_is_vsi_uplink_mode_veb(vsi)) {
  8671. ctxt.info.valid_sections |=
  8672. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  8673. ctxt.info.switch_id =
  8674. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  8675. }
  8676. /* Setup the VSI tx/rx queue map for TC0 only for now */
  8677. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
  8678. break;
  8679. case I40E_VSI_SRIOV:
  8680. ctxt.pf_num = hw->pf_id;
  8681. ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
  8682. ctxt.uplink_seid = vsi->uplink_seid;
  8683. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  8684. ctxt.flags = I40E_AQ_VSI_TYPE_VF;
  8685. /* This VSI is connected to VEB so the switch_id
  8686. * should be set to zero by default.
  8687. */
  8688. if (i40e_is_vsi_uplink_mode_veb(vsi)) {
  8689. ctxt.info.valid_sections |=
  8690. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  8691. ctxt.info.switch_id =
  8692. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  8693. }
  8694. if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
  8695. ctxt.info.valid_sections |=
  8696. cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
  8697. ctxt.info.queueing_opt_flags |=
  8698. (I40E_AQ_VSI_QUE_OPT_TCP_ENA |
  8699. I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
  8700. }
  8701. ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  8702. ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
  8703. if (pf->vf[vsi->vf_id].spoofchk) {
  8704. ctxt.info.valid_sections |=
  8705. cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
  8706. ctxt.info.sec_flags |=
  8707. (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
  8708. I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
  8709. }
  8710. /* Setup the VSI tx/rx queue map for TC0 only for now */
  8711. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
  8712. break;
  8713. case I40E_VSI_IWARP:
  8714. /* send down message to iWARP */
  8715. break;
  8716. default:
  8717. return -ENODEV;
  8718. }
  8719. if (vsi->type != I40E_VSI_MAIN) {
  8720. ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
  8721. if (ret) {
  8722. dev_info(&vsi->back->pdev->dev,
  8723. "add vsi failed, err %s aq_err %s\n",
  8724. i40e_stat_str(&pf->hw, ret),
  8725. i40e_aq_str(&pf->hw,
  8726. pf->hw.aq.asq_last_status));
  8727. ret = -ENOENT;
  8728. goto err;
  8729. }
  8730. vsi->info = ctxt.info;
  8731. vsi->info.valid_sections = 0;
  8732. vsi->seid = ctxt.seid;
  8733. vsi->id = ctxt.vsi_number;
  8734. }
  8735. vsi->active_filters = 0;
  8736. clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  8737. spin_lock_bh(&vsi->mac_filter_hash_lock);
  8738. /* If macvlan filters already exist, force them to get loaded */
  8739. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  8740. f->state = I40E_FILTER_NEW;
  8741. f_count++;
  8742. }
  8743. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  8744. if (f_count) {
  8745. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  8746. pf->flags |= I40E_FLAG_FILTER_SYNC;
  8747. }
  8748. /* Update VSI BW information */
  8749. ret = i40e_vsi_get_bw_info(vsi);
  8750. if (ret) {
  8751. dev_info(&pf->pdev->dev,
  8752. "couldn't get vsi bw info, err %s aq_err %s\n",
  8753. i40e_stat_str(&pf->hw, ret),
  8754. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  8755. /* VSI is already added so not tearing that up */
  8756. ret = 0;
  8757. }
  8758. err:
  8759. return ret;
  8760. }
  8761. /**
  8762. * i40e_vsi_release - Delete a VSI and free its resources
  8763. * @vsi: the VSI being removed
  8764. *
  8765. * Returns 0 on success or < 0 on error
  8766. **/
  8767. int i40e_vsi_release(struct i40e_vsi *vsi)
  8768. {
  8769. struct i40e_mac_filter *f;
  8770. struct hlist_node *h;
  8771. struct i40e_veb *veb = NULL;
  8772. struct i40e_pf *pf;
  8773. u16 uplink_seid;
  8774. int i, n, bkt;
  8775. pf = vsi->back;
  8776. /* release of a VEB-owner or last VSI is not allowed */
  8777. if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
  8778. dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
  8779. vsi->seid, vsi->uplink_seid);
  8780. return -ENODEV;
  8781. }
  8782. if (vsi == pf->vsi[pf->lan_vsi] &&
  8783. !test_bit(__I40E_DOWN, pf->state)) {
  8784. dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
  8785. return -ENODEV;
  8786. }
  8787. uplink_seid = vsi->uplink_seid;
  8788. if (vsi->type != I40E_VSI_SRIOV) {
  8789. if (vsi->netdev_registered) {
  8790. vsi->netdev_registered = false;
  8791. if (vsi->netdev) {
  8792. /* results in a call to i40e_close() */
  8793. unregister_netdev(vsi->netdev);
  8794. }
  8795. } else {
  8796. i40e_vsi_close(vsi);
  8797. }
  8798. i40e_vsi_disable_irq(vsi);
  8799. }
  8800. spin_lock_bh(&vsi->mac_filter_hash_lock);
  8801. /* clear the sync flag on all filters */
  8802. if (vsi->netdev) {
  8803. __dev_uc_unsync(vsi->netdev, NULL);
  8804. __dev_mc_unsync(vsi->netdev, NULL);
  8805. }
  8806. /* make sure any remaining filters are marked for deletion */
  8807. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
  8808. __i40e_del_filter(vsi, f);
  8809. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  8810. i40e_sync_vsi_filters(vsi);
  8811. i40e_vsi_delete(vsi);
  8812. i40e_vsi_free_q_vectors(vsi);
  8813. if (vsi->netdev) {
  8814. free_netdev(vsi->netdev);
  8815. vsi->netdev = NULL;
  8816. }
  8817. i40e_vsi_clear_rings(vsi);
  8818. i40e_vsi_clear(vsi);
  8819. /* If this was the last thing on the VEB, except for the
  8820. * controlling VSI, remove the VEB, which puts the controlling
  8821. * VSI onto the next level down in the switch.
  8822. *
  8823. * Well, okay, there's one more exception here: don't remove
  8824. * the orphan VEBs yet. We'll wait for an explicit remove request
  8825. * from up the network stack.
  8826. */
  8827. for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
  8828. if (pf->vsi[i] &&
  8829. pf->vsi[i]->uplink_seid == uplink_seid &&
  8830. (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
  8831. n++; /* count the VSIs */
  8832. }
  8833. }
  8834. for (i = 0; i < I40E_MAX_VEB; i++) {
  8835. if (!pf->veb[i])
  8836. continue;
  8837. if (pf->veb[i]->uplink_seid == uplink_seid)
  8838. n++; /* count the VEBs */
  8839. if (pf->veb[i]->seid == uplink_seid)
  8840. veb = pf->veb[i];
  8841. }
  8842. if (n == 0 && veb && veb->uplink_seid != 0)
  8843. i40e_veb_release(veb);
  8844. return 0;
  8845. }
  8846. /**
  8847. * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
  8848. * @vsi: ptr to the VSI
  8849. *
  8850. * This should only be called after i40e_vsi_mem_alloc() which allocates the
  8851. * corresponding SW VSI structure and initializes num_queue_pairs for the
  8852. * newly allocated VSI.
  8853. *
  8854. * Returns 0 on success or negative on failure
  8855. **/
  8856. static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
  8857. {
  8858. int ret = -ENOENT;
  8859. struct i40e_pf *pf = vsi->back;
  8860. if (vsi->q_vectors[0]) {
  8861. dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
  8862. vsi->seid);
  8863. return -EEXIST;
  8864. }
  8865. if (vsi->base_vector) {
  8866. dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
  8867. vsi->seid, vsi->base_vector);
  8868. return -EEXIST;
  8869. }
  8870. ret = i40e_vsi_alloc_q_vectors(vsi);
  8871. if (ret) {
  8872. dev_info(&pf->pdev->dev,
  8873. "failed to allocate %d q_vector for VSI %d, ret=%d\n",
  8874. vsi->num_q_vectors, vsi->seid, ret);
  8875. vsi->num_q_vectors = 0;
  8876. goto vector_setup_out;
  8877. }
  8878. /* In Legacy mode, we do not have to get any other vector since we
  8879. * piggyback on the misc/ICR0 for queue interrupts.
  8880. */
  8881. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
  8882. return ret;
  8883. if (vsi->num_q_vectors)
  8884. vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
  8885. vsi->num_q_vectors, vsi->idx);
  8886. if (vsi->base_vector < 0) {
  8887. dev_info(&pf->pdev->dev,
  8888. "failed to get tracking for %d vectors for VSI %d, err=%d\n",
  8889. vsi->num_q_vectors, vsi->seid, vsi->base_vector);
  8890. i40e_vsi_free_q_vectors(vsi);
  8891. ret = -ENOENT;
  8892. goto vector_setup_out;
  8893. }
  8894. vector_setup_out:
  8895. return ret;
  8896. }
  8897. /**
  8898. * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
  8899. * @vsi: pointer to the vsi.
  8900. *
  8901. * This re-allocates a vsi's queue resources.
  8902. *
  8903. * Returns pointer to the successfully allocated and configured VSI sw struct
  8904. * on success, otherwise returns NULL on failure.
  8905. **/
  8906. static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
  8907. {
  8908. u16 alloc_queue_pairs;
  8909. struct i40e_pf *pf;
  8910. u8 enabled_tc;
  8911. int ret;
  8912. if (!vsi)
  8913. return NULL;
  8914. pf = vsi->back;
  8915. i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
  8916. i40e_vsi_clear_rings(vsi);
  8917. i40e_vsi_free_arrays(vsi, false);
  8918. i40e_set_num_rings_in_vsi(vsi);
  8919. ret = i40e_vsi_alloc_arrays(vsi, false);
  8920. if (ret)
  8921. goto err_vsi;
  8922. alloc_queue_pairs = vsi->alloc_queue_pairs *
  8923. (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
  8924. ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
  8925. if (ret < 0) {
  8926. dev_info(&pf->pdev->dev,
  8927. "failed to get tracking for %d queues for VSI %d err %d\n",
  8928. alloc_queue_pairs, vsi->seid, ret);
  8929. goto err_vsi;
  8930. }
  8931. vsi->base_queue = ret;
  8932. /* Update the FW view of the VSI. Force a reset of TC and queue
  8933. * layout configurations.
  8934. */
  8935. enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
  8936. pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
  8937. pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
  8938. i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
  8939. if (vsi->type == I40E_VSI_MAIN)
  8940. i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
  8941. /* assign it some queues */
  8942. ret = i40e_alloc_rings(vsi);
  8943. if (ret)
  8944. goto err_rings;
  8945. /* map all of the rings to the q_vectors */
  8946. i40e_vsi_map_rings_to_vectors(vsi);
  8947. return vsi;
  8948. err_rings:
  8949. i40e_vsi_free_q_vectors(vsi);
  8950. if (vsi->netdev_registered) {
  8951. vsi->netdev_registered = false;
  8952. unregister_netdev(vsi->netdev);
  8953. free_netdev(vsi->netdev);
  8954. vsi->netdev = NULL;
  8955. }
  8956. i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
  8957. err_vsi:
  8958. i40e_vsi_clear(vsi);
  8959. return NULL;
  8960. }
  8961. /**
  8962. * i40e_vsi_setup - Set up a VSI by a given type
  8963. * @pf: board private structure
  8964. * @type: VSI type
  8965. * @uplink_seid: the switch element to link to
  8966. * @param1: usage depends upon VSI type. For VF types, indicates VF id
  8967. *
  8968. * This allocates the sw VSI structure and its queue resources, then add a VSI
  8969. * to the identified VEB.
  8970. *
  8971. * Returns pointer to the successfully allocated and configure VSI sw struct on
  8972. * success, otherwise returns NULL on failure.
  8973. **/
  8974. struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
  8975. u16 uplink_seid, u32 param1)
  8976. {
  8977. struct i40e_vsi *vsi = NULL;
  8978. struct i40e_veb *veb = NULL;
  8979. u16 alloc_queue_pairs;
  8980. int ret, i;
  8981. int v_idx;
  8982. /* The requested uplink_seid must be either
  8983. * - the PF's port seid
  8984. * no VEB is needed because this is the PF
  8985. * or this is a Flow Director special case VSI
  8986. * - seid of an existing VEB
  8987. * - seid of a VSI that owns an existing VEB
  8988. * - seid of a VSI that doesn't own a VEB
  8989. * a new VEB is created and the VSI becomes the owner
  8990. * - seid of the PF VSI, which is what creates the first VEB
  8991. * this is a special case of the previous
  8992. *
  8993. * Find which uplink_seid we were given and create a new VEB if needed
  8994. */
  8995. for (i = 0; i < I40E_MAX_VEB; i++) {
  8996. if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
  8997. veb = pf->veb[i];
  8998. break;
  8999. }
  9000. }
  9001. if (!veb && uplink_seid != pf->mac_seid) {
  9002. for (i = 0; i < pf->num_alloc_vsi; i++) {
  9003. if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
  9004. vsi = pf->vsi[i];
  9005. break;
  9006. }
  9007. }
  9008. if (!vsi) {
  9009. dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
  9010. uplink_seid);
  9011. return NULL;
  9012. }
  9013. if (vsi->uplink_seid == pf->mac_seid)
  9014. veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
  9015. vsi->tc_config.enabled_tc);
  9016. else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
  9017. veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
  9018. vsi->tc_config.enabled_tc);
  9019. if (veb) {
  9020. if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
  9021. dev_info(&vsi->back->pdev->dev,
  9022. "New VSI creation error, uplink seid of LAN VSI expected.\n");
  9023. return NULL;
  9024. }
  9025. /* We come up by default in VEPA mode if SRIOV is not
  9026. * already enabled, in which case we can't force VEPA
  9027. * mode.
  9028. */
  9029. if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
  9030. veb->bridge_mode = BRIDGE_MODE_VEPA;
  9031. pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
  9032. }
  9033. i40e_config_bridge_mode(veb);
  9034. }
  9035. for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
  9036. if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
  9037. veb = pf->veb[i];
  9038. }
  9039. if (!veb) {
  9040. dev_info(&pf->pdev->dev, "couldn't add VEB\n");
  9041. return NULL;
  9042. }
  9043. vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
  9044. uplink_seid = veb->seid;
  9045. }
  9046. /* get vsi sw struct */
  9047. v_idx = i40e_vsi_mem_alloc(pf, type);
  9048. if (v_idx < 0)
  9049. goto err_alloc;
  9050. vsi = pf->vsi[v_idx];
  9051. if (!vsi)
  9052. goto err_alloc;
  9053. vsi->type = type;
  9054. vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
  9055. if (type == I40E_VSI_MAIN)
  9056. pf->lan_vsi = v_idx;
  9057. else if (type == I40E_VSI_SRIOV)
  9058. vsi->vf_id = param1;
  9059. /* assign it some queues */
  9060. alloc_queue_pairs = vsi->alloc_queue_pairs *
  9061. (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
  9062. ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
  9063. if (ret < 0) {
  9064. dev_info(&pf->pdev->dev,
  9065. "failed to get tracking for %d queues for VSI %d err=%d\n",
  9066. alloc_queue_pairs, vsi->seid, ret);
  9067. goto err_vsi;
  9068. }
  9069. vsi->base_queue = ret;
  9070. /* get a VSI from the hardware */
  9071. vsi->uplink_seid = uplink_seid;
  9072. ret = i40e_add_vsi(vsi);
  9073. if (ret)
  9074. goto err_vsi;
  9075. switch (vsi->type) {
  9076. /* setup the netdev if needed */
  9077. case I40E_VSI_MAIN:
  9078. case I40E_VSI_VMDQ2:
  9079. ret = i40e_config_netdev(vsi);
  9080. if (ret)
  9081. goto err_netdev;
  9082. ret = register_netdev(vsi->netdev);
  9083. if (ret)
  9084. goto err_netdev;
  9085. vsi->netdev_registered = true;
  9086. netif_carrier_off(vsi->netdev);
  9087. #ifdef CONFIG_I40E_DCB
  9088. /* Setup DCB netlink interface */
  9089. i40e_dcbnl_setup(vsi);
  9090. #endif /* CONFIG_I40E_DCB */
  9091. /* fall through */
  9092. case I40E_VSI_FDIR:
  9093. /* set up vectors and rings if needed */
  9094. ret = i40e_vsi_setup_vectors(vsi);
  9095. if (ret)
  9096. goto err_msix;
  9097. ret = i40e_alloc_rings(vsi);
  9098. if (ret)
  9099. goto err_rings;
  9100. /* map all of the rings to the q_vectors */
  9101. i40e_vsi_map_rings_to_vectors(vsi);
  9102. i40e_vsi_reset_stats(vsi);
  9103. break;
  9104. default:
  9105. /* no netdev or rings for the other VSI types */
  9106. break;
  9107. }
  9108. if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
  9109. (vsi->type == I40E_VSI_VMDQ2)) {
  9110. ret = i40e_vsi_config_rss(vsi);
  9111. }
  9112. return vsi;
  9113. err_rings:
  9114. i40e_vsi_free_q_vectors(vsi);
  9115. err_msix:
  9116. if (vsi->netdev_registered) {
  9117. vsi->netdev_registered = false;
  9118. unregister_netdev(vsi->netdev);
  9119. free_netdev(vsi->netdev);
  9120. vsi->netdev = NULL;
  9121. }
  9122. err_netdev:
  9123. i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
  9124. err_vsi:
  9125. i40e_vsi_clear(vsi);
  9126. err_alloc:
  9127. return NULL;
  9128. }
  9129. /**
  9130. * i40e_veb_get_bw_info - Query VEB BW information
  9131. * @veb: the veb to query
  9132. *
  9133. * Query the Tx scheduler BW configuration data for given VEB
  9134. **/
  9135. static int i40e_veb_get_bw_info(struct i40e_veb *veb)
  9136. {
  9137. struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
  9138. struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
  9139. struct i40e_pf *pf = veb->pf;
  9140. struct i40e_hw *hw = &pf->hw;
  9141. u32 tc_bw_max;
  9142. int ret = 0;
  9143. int i;
  9144. ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
  9145. &bw_data, NULL);
  9146. if (ret) {
  9147. dev_info(&pf->pdev->dev,
  9148. "query veb bw config failed, err %s aq_err %s\n",
  9149. i40e_stat_str(&pf->hw, ret),
  9150. i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
  9151. goto out;
  9152. }
  9153. ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
  9154. &ets_data, NULL);
  9155. if (ret) {
  9156. dev_info(&pf->pdev->dev,
  9157. "query veb bw ets config failed, err %s aq_err %s\n",
  9158. i40e_stat_str(&pf->hw, ret),
  9159. i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
  9160. goto out;
  9161. }
  9162. veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
  9163. veb->bw_max_quanta = ets_data.tc_bw_max;
  9164. veb->is_abs_credits = bw_data.absolute_credits_enable;
  9165. veb->enabled_tc = ets_data.tc_valid_bits;
  9166. tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
  9167. (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
  9168. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  9169. veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
  9170. veb->bw_tc_limit_credits[i] =
  9171. le16_to_cpu(bw_data.tc_bw_limits[i]);
  9172. veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
  9173. }
  9174. out:
  9175. return ret;
  9176. }
  9177. /**
  9178. * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
  9179. * @pf: board private structure
  9180. *
  9181. * On error: returns error code (negative)
  9182. * On success: returns vsi index in PF (positive)
  9183. **/
  9184. static int i40e_veb_mem_alloc(struct i40e_pf *pf)
  9185. {
  9186. int ret = -ENOENT;
  9187. struct i40e_veb *veb;
  9188. int i;
  9189. /* Need to protect the allocation of switch elements at the PF level */
  9190. mutex_lock(&pf->switch_mutex);
  9191. /* VEB list may be fragmented if VEB creation/destruction has
  9192. * been happening. We can afford to do a quick scan to look
  9193. * for any free slots in the list.
  9194. *
  9195. * find next empty veb slot, looping back around if necessary
  9196. */
  9197. i = 0;
  9198. while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
  9199. i++;
  9200. if (i >= I40E_MAX_VEB) {
  9201. ret = -ENOMEM;
  9202. goto err_alloc_veb; /* out of VEB slots! */
  9203. }
  9204. veb = kzalloc(sizeof(*veb), GFP_KERNEL);
  9205. if (!veb) {
  9206. ret = -ENOMEM;
  9207. goto err_alloc_veb;
  9208. }
  9209. veb->pf = pf;
  9210. veb->idx = i;
  9211. veb->enabled_tc = 1;
  9212. pf->veb[i] = veb;
  9213. ret = i;
  9214. err_alloc_veb:
  9215. mutex_unlock(&pf->switch_mutex);
  9216. return ret;
  9217. }
  9218. /**
  9219. * i40e_switch_branch_release - Delete a branch of the switch tree
  9220. * @branch: where to start deleting
  9221. *
  9222. * This uses recursion to find the tips of the branch to be
  9223. * removed, deleting until we get back to and can delete this VEB.
  9224. **/
  9225. static void i40e_switch_branch_release(struct i40e_veb *branch)
  9226. {
  9227. struct i40e_pf *pf = branch->pf;
  9228. u16 branch_seid = branch->seid;
  9229. u16 veb_idx = branch->idx;
  9230. int i;
  9231. /* release any VEBs on this VEB - RECURSION */
  9232. for (i = 0; i < I40E_MAX_VEB; i++) {
  9233. if (!pf->veb[i])
  9234. continue;
  9235. if (pf->veb[i]->uplink_seid == branch->seid)
  9236. i40e_switch_branch_release(pf->veb[i]);
  9237. }
  9238. /* Release the VSIs on this VEB, but not the owner VSI.
  9239. *
  9240. * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
  9241. * the VEB itself, so don't use (*branch) after this loop.
  9242. */
  9243. for (i = 0; i < pf->num_alloc_vsi; i++) {
  9244. if (!pf->vsi[i])
  9245. continue;
  9246. if (pf->vsi[i]->uplink_seid == branch_seid &&
  9247. (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
  9248. i40e_vsi_release(pf->vsi[i]);
  9249. }
  9250. }
  9251. /* There's one corner case where the VEB might not have been
  9252. * removed, so double check it here and remove it if needed.
  9253. * This case happens if the veb was created from the debugfs
  9254. * commands and no VSIs were added to it.
  9255. */
  9256. if (pf->veb[veb_idx])
  9257. i40e_veb_release(pf->veb[veb_idx]);
  9258. }
  9259. /**
  9260. * i40e_veb_clear - remove veb struct
  9261. * @veb: the veb to remove
  9262. **/
  9263. static void i40e_veb_clear(struct i40e_veb *veb)
  9264. {
  9265. if (!veb)
  9266. return;
  9267. if (veb->pf) {
  9268. struct i40e_pf *pf = veb->pf;
  9269. mutex_lock(&pf->switch_mutex);
  9270. if (pf->veb[veb->idx] == veb)
  9271. pf->veb[veb->idx] = NULL;
  9272. mutex_unlock(&pf->switch_mutex);
  9273. }
  9274. kfree(veb);
  9275. }
  9276. /**
  9277. * i40e_veb_release - Delete a VEB and free its resources
  9278. * @veb: the VEB being removed
  9279. **/
  9280. void i40e_veb_release(struct i40e_veb *veb)
  9281. {
  9282. struct i40e_vsi *vsi = NULL;
  9283. struct i40e_pf *pf;
  9284. int i, n = 0;
  9285. pf = veb->pf;
  9286. /* find the remaining VSI and check for extras */
  9287. for (i = 0; i < pf->num_alloc_vsi; i++) {
  9288. if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
  9289. n++;
  9290. vsi = pf->vsi[i];
  9291. }
  9292. }
  9293. if (n != 1) {
  9294. dev_info(&pf->pdev->dev,
  9295. "can't remove VEB %d with %d VSIs left\n",
  9296. veb->seid, n);
  9297. return;
  9298. }
  9299. /* move the remaining VSI to uplink veb */
  9300. vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
  9301. if (veb->uplink_seid) {
  9302. vsi->uplink_seid = veb->uplink_seid;
  9303. if (veb->uplink_seid == pf->mac_seid)
  9304. vsi->veb_idx = I40E_NO_VEB;
  9305. else
  9306. vsi->veb_idx = veb->veb_idx;
  9307. } else {
  9308. /* floating VEB */
  9309. vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
  9310. vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
  9311. }
  9312. i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
  9313. i40e_veb_clear(veb);
  9314. }
  9315. /**
  9316. * i40e_add_veb - create the VEB in the switch
  9317. * @veb: the VEB to be instantiated
  9318. * @vsi: the controlling VSI
  9319. **/
  9320. static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
  9321. {
  9322. struct i40e_pf *pf = veb->pf;
  9323. bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
  9324. int ret;
  9325. ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
  9326. veb->enabled_tc, false,
  9327. &veb->seid, enable_stats, NULL);
  9328. /* get a VEB from the hardware */
  9329. if (ret) {
  9330. dev_info(&pf->pdev->dev,
  9331. "couldn't add VEB, err %s aq_err %s\n",
  9332. i40e_stat_str(&pf->hw, ret),
  9333. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  9334. return -EPERM;
  9335. }
  9336. /* get statistics counter */
  9337. ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
  9338. &veb->stats_idx, NULL, NULL, NULL);
  9339. if (ret) {
  9340. dev_info(&pf->pdev->dev,
  9341. "couldn't get VEB statistics idx, err %s aq_err %s\n",
  9342. i40e_stat_str(&pf->hw, ret),
  9343. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  9344. return -EPERM;
  9345. }
  9346. ret = i40e_veb_get_bw_info(veb);
  9347. if (ret) {
  9348. dev_info(&pf->pdev->dev,
  9349. "couldn't get VEB bw info, err %s aq_err %s\n",
  9350. i40e_stat_str(&pf->hw, ret),
  9351. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  9352. i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
  9353. return -ENOENT;
  9354. }
  9355. vsi->uplink_seid = veb->seid;
  9356. vsi->veb_idx = veb->idx;
  9357. vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
  9358. return 0;
  9359. }
  9360. /**
  9361. * i40e_veb_setup - Set up a VEB
  9362. * @pf: board private structure
  9363. * @flags: VEB setup flags
  9364. * @uplink_seid: the switch element to link to
  9365. * @vsi_seid: the initial VSI seid
  9366. * @enabled_tc: Enabled TC bit-map
  9367. *
  9368. * This allocates the sw VEB structure and links it into the switch
  9369. * It is possible and legal for this to be a duplicate of an already
  9370. * existing VEB. It is also possible for both uplink and vsi seids
  9371. * to be zero, in order to create a floating VEB.
  9372. *
  9373. * Returns pointer to the successfully allocated VEB sw struct on
  9374. * success, otherwise returns NULL on failure.
  9375. **/
  9376. struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
  9377. u16 uplink_seid, u16 vsi_seid,
  9378. u8 enabled_tc)
  9379. {
  9380. struct i40e_veb *veb, *uplink_veb = NULL;
  9381. int vsi_idx, veb_idx;
  9382. int ret;
  9383. /* if one seid is 0, the other must be 0 to create a floating relay */
  9384. if ((uplink_seid == 0 || vsi_seid == 0) &&
  9385. (uplink_seid + vsi_seid != 0)) {
  9386. dev_info(&pf->pdev->dev,
  9387. "one, not both seid's are 0: uplink=%d vsi=%d\n",
  9388. uplink_seid, vsi_seid);
  9389. return NULL;
  9390. }
  9391. /* make sure there is such a vsi and uplink */
  9392. for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
  9393. if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
  9394. break;
  9395. if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) {
  9396. dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
  9397. vsi_seid);
  9398. return NULL;
  9399. }
  9400. if (uplink_seid && uplink_seid != pf->mac_seid) {
  9401. for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
  9402. if (pf->veb[veb_idx] &&
  9403. pf->veb[veb_idx]->seid == uplink_seid) {
  9404. uplink_veb = pf->veb[veb_idx];
  9405. break;
  9406. }
  9407. }
  9408. if (!uplink_veb) {
  9409. dev_info(&pf->pdev->dev,
  9410. "uplink seid %d not found\n", uplink_seid);
  9411. return NULL;
  9412. }
  9413. }
  9414. /* get veb sw struct */
  9415. veb_idx = i40e_veb_mem_alloc(pf);
  9416. if (veb_idx < 0)
  9417. goto err_alloc;
  9418. veb = pf->veb[veb_idx];
  9419. veb->flags = flags;
  9420. veb->uplink_seid = uplink_seid;
  9421. veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
  9422. veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
  9423. /* create the VEB in the switch */
  9424. ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
  9425. if (ret)
  9426. goto err_veb;
  9427. if (vsi_idx == pf->lan_vsi)
  9428. pf->lan_veb = veb->idx;
  9429. return veb;
  9430. err_veb:
  9431. i40e_veb_clear(veb);
  9432. err_alloc:
  9433. return NULL;
  9434. }
  9435. /**
  9436. * i40e_setup_pf_switch_element - set PF vars based on switch type
  9437. * @pf: board private structure
  9438. * @ele: element we are building info from
  9439. * @num_reported: total number of elements
  9440. * @printconfig: should we print the contents
  9441. *
  9442. * helper function to assist in extracting a few useful SEID values.
  9443. **/
  9444. static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
  9445. struct i40e_aqc_switch_config_element_resp *ele,
  9446. u16 num_reported, bool printconfig)
  9447. {
  9448. u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
  9449. u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
  9450. u8 element_type = ele->element_type;
  9451. u16 seid = le16_to_cpu(ele->seid);
  9452. if (printconfig)
  9453. dev_info(&pf->pdev->dev,
  9454. "type=%d seid=%d uplink=%d downlink=%d\n",
  9455. element_type, seid, uplink_seid, downlink_seid);
  9456. switch (element_type) {
  9457. case I40E_SWITCH_ELEMENT_TYPE_MAC:
  9458. pf->mac_seid = seid;
  9459. break;
  9460. case I40E_SWITCH_ELEMENT_TYPE_VEB:
  9461. /* Main VEB? */
  9462. if (uplink_seid != pf->mac_seid)
  9463. break;
  9464. if (pf->lan_veb == I40E_NO_VEB) {
  9465. int v;
  9466. /* find existing or else empty VEB */
  9467. for (v = 0; v < I40E_MAX_VEB; v++) {
  9468. if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
  9469. pf->lan_veb = v;
  9470. break;
  9471. }
  9472. }
  9473. if (pf->lan_veb == I40E_NO_VEB) {
  9474. v = i40e_veb_mem_alloc(pf);
  9475. if (v < 0)
  9476. break;
  9477. pf->lan_veb = v;
  9478. }
  9479. }
  9480. pf->veb[pf->lan_veb]->seid = seid;
  9481. pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
  9482. pf->veb[pf->lan_veb]->pf = pf;
  9483. pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
  9484. break;
  9485. case I40E_SWITCH_ELEMENT_TYPE_VSI:
  9486. if (num_reported != 1)
  9487. break;
  9488. /* This is immediately after a reset so we can assume this is
  9489. * the PF's VSI
  9490. */
  9491. pf->mac_seid = uplink_seid;
  9492. pf->pf_seid = downlink_seid;
  9493. pf->main_vsi_seid = seid;
  9494. if (printconfig)
  9495. dev_info(&pf->pdev->dev,
  9496. "pf_seid=%d main_vsi_seid=%d\n",
  9497. pf->pf_seid, pf->main_vsi_seid);
  9498. break;
  9499. case I40E_SWITCH_ELEMENT_TYPE_PF:
  9500. case I40E_SWITCH_ELEMENT_TYPE_VF:
  9501. case I40E_SWITCH_ELEMENT_TYPE_EMP:
  9502. case I40E_SWITCH_ELEMENT_TYPE_BMC:
  9503. case I40E_SWITCH_ELEMENT_TYPE_PE:
  9504. case I40E_SWITCH_ELEMENT_TYPE_PA:
  9505. /* ignore these for now */
  9506. break;
  9507. default:
  9508. dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
  9509. element_type, seid);
  9510. break;
  9511. }
  9512. }
  9513. /**
  9514. * i40e_fetch_switch_configuration - Get switch config from firmware
  9515. * @pf: board private structure
  9516. * @printconfig: should we print the contents
  9517. *
  9518. * Get the current switch configuration from the device and
  9519. * extract a few useful SEID values.
  9520. **/
  9521. int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
  9522. {
  9523. struct i40e_aqc_get_switch_config_resp *sw_config;
  9524. u16 next_seid = 0;
  9525. int ret = 0;
  9526. u8 *aq_buf;
  9527. int i;
  9528. aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
  9529. if (!aq_buf)
  9530. return -ENOMEM;
  9531. sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
  9532. do {
  9533. u16 num_reported, num_total;
  9534. ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
  9535. I40E_AQ_LARGE_BUF,
  9536. &next_seid, NULL);
  9537. if (ret) {
  9538. dev_info(&pf->pdev->dev,
  9539. "get switch config failed err %s aq_err %s\n",
  9540. i40e_stat_str(&pf->hw, ret),
  9541. i40e_aq_str(&pf->hw,
  9542. pf->hw.aq.asq_last_status));
  9543. kfree(aq_buf);
  9544. return -ENOENT;
  9545. }
  9546. num_reported = le16_to_cpu(sw_config->header.num_reported);
  9547. num_total = le16_to_cpu(sw_config->header.num_total);
  9548. if (printconfig)
  9549. dev_info(&pf->pdev->dev,
  9550. "header: %d reported %d total\n",
  9551. num_reported, num_total);
  9552. for (i = 0; i < num_reported; i++) {
  9553. struct i40e_aqc_switch_config_element_resp *ele =
  9554. &sw_config->element[i];
  9555. i40e_setup_pf_switch_element(pf, ele, num_reported,
  9556. printconfig);
  9557. }
  9558. } while (next_seid != 0);
  9559. kfree(aq_buf);
  9560. return ret;
  9561. }
  9562. /**
  9563. * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
  9564. * @pf: board private structure
  9565. * @reinit: if the Main VSI needs to re-initialized.
  9566. *
  9567. * Returns 0 on success, negative value on failure
  9568. **/
  9569. static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
  9570. {
  9571. u16 flags = 0;
  9572. int ret;
  9573. /* find out what's out there already */
  9574. ret = i40e_fetch_switch_configuration(pf, false);
  9575. if (ret) {
  9576. dev_info(&pf->pdev->dev,
  9577. "couldn't fetch switch config, err %s aq_err %s\n",
  9578. i40e_stat_str(&pf->hw, ret),
  9579. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  9580. return ret;
  9581. }
  9582. i40e_pf_reset_stats(pf);
  9583. /* set the switch config bit for the whole device to
  9584. * support limited promisc or true promisc
  9585. * when user requests promisc. The default is limited
  9586. * promisc.
  9587. */
  9588. if ((pf->hw.pf_id == 0) &&
  9589. !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT))
  9590. flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
  9591. if (pf->hw.pf_id == 0) {
  9592. u16 valid_flags;
  9593. valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
  9594. ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags,
  9595. NULL);
  9596. if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
  9597. dev_info(&pf->pdev->dev,
  9598. "couldn't set switch config bits, err %s aq_err %s\n",
  9599. i40e_stat_str(&pf->hw, ret),
  9600. i40e_aq_str(&pf->hw,
  9601. pf->hw.aq.asq_last_status));
  9602. /* not a fatal problem, just keep going */
  9603. }
  9604. }
  9605. /* first time setup */
  9606. if (pf->lan_vsi == I40E_NO_VSI || reinit) {
  9607. struct i40e_vsi *vsi = NULL;
  9608. u16 uplink_seid;
  9609. /* Set up the PF VSI associated with the PF's main VSI
  9610. * that is already in the HW switch
  9611. */
  9612. if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
  9613. uplink_seid = pf->veb[pf->lan_veb]->seid;
  9614. else
  9615. uplink_seid = pf->mac_seid;
  9616. if (pf->lan_vsi == I40E_NO_VSI)
  9617. vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
  9618. else if (reinit)
  9619. vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
  9620. if (!vsi) {
  9621. dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
  9622. i40e_fdir_teardown(pf);
  9623. return -EAGAIN;
  9624. }
  9625. } else {
  9626. /* force a reset of TC and queue layout configurations */
  9627. u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
  9628. pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
  9629. pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
  9630. i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
  9631. }
  9632. i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
  9633. i40e_fdir_sb_setup(pf);
  9634. /* Setup static PF queue filter control settings */
  9635. ret = i40e_setup_pf_filter_control(pf);
  9636. if (ret) {
  9637. dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
  9638. ret);
  9639. /* Failure here should not stop continuing other steps */
  9640. }
  9641. /* enable RSS in the HW, even for only one queue, as the stack can use
  9642. * the hash
  9643. */
  9644. if ((pf->flags & I40E_FLAG_RSS_ENABLED))
  9645. i40e_pf_config_rss(pf);
  9646. /* fill in link information and enable LSE reporting */
  9647. i40e_link_event(pf);
  9648. /* Initialize user-specific link properties */
  9649. pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
  9650. I40E_AQ_AN_COMPLETED) ? true : false);
  9651. i40e_ptp_init(pf);
  9652. /* repopulate tunnel port filters */
  9653. i40e_sync_udp_filters(pf);
  9654. return ret;
  9655. }
  9656. /**
  9657. * i40e_determine_queue_usage - Work out queue distribution
  9658. * @pf: board private structure
  9659. **/
  9660. static void i40e_determine_queue_usage(struct i40e_pf *pf)
  9661. {
  9662. int queues_left;
  9663. pf->num_lan_qps = 0;
  9664. /* Find the max queues to be put into basic use. We'll always be
  9665. * using TC0, whether or not DCB is running, and TC0 will get the
  9666. * big RSS set.
  9667. */
  9668. queues_left = pf->hw.func_caps.num_tx_qp;
  9669. if ((queues_left == 1) ||
  9670. !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
  9671. /* one qp for PF, no queues for anything else */
  9672. queues_left = 0;
  9673. pf->alloc_rss_size = pf->num_lan_qps = 1;
  9674. /* make sure all the fancies are disabled */
  9675. pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
  9676. I40E_FLAG_IWARP_ENABLED |
  9677. I40E_FLAG_FD_SB_ENABLED |
  9678. I40E_FLAG_FD_ATR_ENABLED |
  9679. I40E_FLAG_DCB_CAPABLE |
  9680. I40E_FLAG_DCB_ENABLED |
  9681. I40E_FLAG_SRIOV_ENABLED |
  9682. I40E_FLAG_VMDQ_ENABLED);
  9683. } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
  9684. I40E_FLAG_FD_SB_ENABLED |
  9685. I40E_FLAG_FD_ATR_ENABLED |
  9686. I40E_FLAG_DCB_CAPABLE))) {
  9687. /* one qp for PF */
  9688. pf->alloc_rss_size = pf->num_lan_qps = 1;
  9689. queues_left -= pf->num_lan_qps;
  9690. pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
  9691. I40E_FLAG_IWARP_ENABLED |
  9692. I40E_FLAG_FD_SB_ENABLED |
  9693. I40E_FLAG_FD_ATR_ENABLED |
  9694. I40E_FLAG_DCB_ENABLED |
  9695. I40E_FLAG_VMDQ_ENABLED);
  9696. } else {
  9697. /* Not enough queues for all TCs */
  9698. if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
  9699. (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
  9700. pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
  9701. I40E_FLAG_DCB_ENABLED);
  9702. dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
  9703. }
  9704. pf->num_lan_qps = max_t(int, pf->rss_size_max,
  9705. num_online_cpus());
  9706. pf->num_lan_qps = min_t(int, pf->num_lan_qps,
  9707. pf->hw.func_caps.num_tx_qp);
  9708. queues_left -= pf->num_lan_qps;
  9709. }
  9710. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  9711. if (queues_left > 1) {
  9712. queues_left -= 1; /* save 1 queue for FD */
  9713. } else {
  9714. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  9715. dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
  9716. }
  9717. }
  9718. if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
  9719. pf->num_vf_qps && pf->num_req_vfs && queues_left) {
  9720. pf->num_req_vfs = min_t(int, pf->num_req_vfs,
  9721. (queues_left / pf->num_vf_qps));
  9722. queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
  9723. }
  9724. if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
  9725. pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
  9726. pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
  9727. (queues_left / pf->num_vmdq_qps));
  9728. queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
  9729. }
  9730. pf->queues_left = queues_left;
  9731. dev_dbg(&pf->pdev->dev,
  9732. "qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
  9733. pf->hw.func_caps.num_tx_qp,
  9734. !!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
  9735. pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
  9736. pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
  9737. queues_left);
  9738. }
  9739. /**
  9740. * i40e_setup_pf_filter_control - Setup PF static filter control
  9741. * @pf: PF to be setup
  9742. *
  9743. * i40e_setup_pf_filter_control sets up a PF's initial filter control
  9744. * settings. If PE/FCoE are enabled then it will also set the per PF
  9745. * based filter sizes required for them. It also enables Flow director,
  9746. * ethertype and macvlan type filter settings for the pf.
  9747. *
  9748. * Returns 0 on success, negative on failure
  9749. **/
  9750. static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
  9751. {
  9752. struct i40e_filter_control_settings *settings = &pf->filter_settings;
  9753. settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
  9754. /* Flow Director is enabled */
  9755. if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
  9756. settings->enable_fdir = true;
  9757. /* Ethtype and MACVLAN filters enabled for PF */
  9758. settings->enable_ethtype = true;
  9759. settings->enable_macvlan = true;
  9760. if (i40e_set_filter_control(&pf->hw, settings))
  9761. return -ENOENT;
  9762. return 0;
  9763. }
  9764. #define INFO_STRING_LEN 255
  9765. #define REMAIN(__x) (INFO_STRING_LEN - (__x))
  9766. static void i40e_print_features(struct i40e_pf *pf)
  9767. {
  9768. struct i40e_hw *hw = &pf->hw;
  9769. char *buf;
  9770. int i;
  9771. buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
  9772. if (!buf)
  9773. return;
  9774. i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
  9775. #ifdef CONFIG_PCI_IOV
  9776. i += snprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
  9777. #endif
  9778. i += snprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
  9779. pf->hw.func_caps.num_vsis,
  9780. pf->vsi[pf->lan_vsi]->num_queue_pairs);
  9781. if (pf->flags & I40E_FLAG_RSS_ENABLED)
  9782. i += snprintf(&buf[i], REMAIN(i), " RSS");
  9783. if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
  9784. i += snprintf(&buf[i], REMAIN(i), " FD_ATR");
  9785. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  9786. i += snprintf(&buf[i], REMAIN(i), " FD_SB");
  9787. i += snprintf(&buf[i], REMAIN(i), " NTUPLE");
  9788. }
  9789. if (pf->flags & I40E_FLAG_DCB_CAPABLE)
  9790. i += snprintf(&buf[i], REMAIN(i), " DCB");
  9791. i += snprintf(&buf[i], REMAIN(i), " VxLAN");
  9792. i += snprintf(&buf[i], REMAIN(i), " Geneve");
  9793. if (pf->flags & I40E_FLAG_PTP)
  9794. i += snprintf(&buf[i], REMAIN(i), " PTP");
  9795. if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
  9796. i += snprintf(&buf[i], REMAIN(i), " VEB");
  9797. else
  9798. i += snprintf(&buf[i], REMAIN(i), " VEPA");
  9799. dev_info(&pf->pdev->dev, "%s\n", buf);
  9800. kfree(buf);
  9801. WARN_ON(i > INFO_STRING_LEN);
  9802. }
  9803. /**
  9804. * i40e_get_platform_mac_addr - get platform-specific MAC address
  9805. * @pdev: PCI device information struct
  9806. * @pf: board private structure
  9807. *
  9808. * Look up the MAC address for the device. First we'll try
  9809. * eth_platform_get_mac_address, which will check Open Firmware, or arch
  9810. * specific fallback. Otherwise, we'll default to the stored value in
  9811. * firmware.
  9812. **/
  9813. static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
  9814. {
  9815. if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
  9816. i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
  9817. }
  9818. /**
  9819. * i40e_probe - Device initialization routine
  9820. * @pdev: PCI device information struct
  9821. * @ent: entry in i40e_pci_tbl
  9822. *
  9823. * i40e_probe initializes a PF identified by a pci_dev structure.
  9824. * The OS initialization, configuring of the PF private structure,
  9825. * and a hardware reset occur.
  9826. *
  9827. * Returns 0 on success, negative on failure
  9828. **/
  9829. static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  9830. {
  9831. struct i40e_aq_get_phy_abilities_resp abilities;
  9832. struct i40e_pf *pf;
  9833. struct i40e_hw *hw;
  9834. static u16 pfs_found;
  9835. u16 wol_nvm_bits;
  9836. u16 link_status;
  9837. int err;
  9838. u32 val;
  9839. u32 i;
  9840. u8 set_fc_aq_fail;
  9841. err = pci_enable_device_mem(pdev);
  9842. if (err)
  9843. return err;
  9844. /* set up for high or low dma */
  9845. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  9846. if (err) {
  9847. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  9848. if (err) {
  9849. dev_err(&pdev->dev,
  9850. "DMA configuration failed: 0x%x\n", err);
  9851. goto err_dma;
  9852. }
  9853. }
  9854. /* set up pci connections */
  9855. err = pci_request_mem_regions(pdev, i40e_driver_name);
  9856. if (err) {
  9857. dev_info(&pdev->dev,
  9858. "pci_request_selected_regions failed %d\n", err);
  9859. goto err_pci_reg;
  9860. }
  9861. pci_enable_pcie_error_reporting(pdev);
  9862. pci_set_master(pdev);
  9863. /* Now that we have a PCI connection, we need to do the
  9864. * low level device setup. This is primarily setting up
  9865. * the Admin Queue structures and then querying for the
  9866. * device's current profile information.
  9867. */
  9868. pf = kzalloc(sizeof(*pf), GFP_KERNEL);
  9869. if (!pf) {
  9870. err = -ENOMEM;
  9871. goto err_pf_alloc;
  9872. }
  9873. pf->next_vsi = 0;
  9874. pf->pdev = pdev;
  9875. set_bit(__I40E_DOWN, pf->state);
  9876. hw = &pf->hw;
  9877. hw->back = pf;
  9878. pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
  9879. I40E_MAX_CSR_SPACE);
  9880. hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
  9881. if (!hw->hw_addr) {
  9882. err = -EIO;
  9883. dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
  9884. (unsigned int)pci_resource_start(pdev, 0),
  9885. pf->ioremap_len, err);
  9886. goto err_ioremap;
  9887. }
  9888. hw->vendor_id = pdev->vendor;
  9889. hw->device_id = pdev->device;
  9890. pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
  9891. hw->subsystem_vendor_id = pdev->subsystem_vendor;
  9892. hw->subsystem_device_id = pdev->subsystem_device;
  9893. hw->bus.device = PCI_SLOT(pdev->devfn);
  9894. hw->bus.func = PCI_FUNC(pdev->devfn);
  9895. hw->bus.bus_id = pdev->bus->number;
  9896. pf->instance = pfs_found;
  9897. INIT_LIST_HEAD(&pf->l3_flex_pit_list);
  9898. INIT_LIST_HEAD(&pf->l4_flex_pit_list);
  9899. /* set up the locks for the AQ, do this only once in probe
  9900. * and destroy them only once in remove
  9901. */
  9902. mutex_init(&hw->aq.asq_mutex);
  9903. mutex_init(&hw->aq.arq_mutex);
  9904. pf->msg_enable = netif_msg_init(debug,
  9905. NETIF_MSG_DRV |
  9906. NETIF_MSG_PROBE |
  9907. NETIF_MSG_LINK);
  9908. if (debug < -1)
  9909. pf->hw.debug_mask = debug;
  9910. /* do a special CORER for clearing PXE mode once at init */
  9911. if (hw->revision_id == 0 &&
  9912. (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
  9913. wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
  9914. i40e_flush(hw);
  9915. msleep(200);
  9916. pf->corer_count++;
  9917. i40e_clear_pxe_mode(hw);
  9918. }
  9919. /* Reset here to make sure all is clean and to define PF 'n' */
  9920. i40e_clear_hw(hw);
  9921. err = i40e_pf_reset(hw);
  9922. if (err) {
  9923. dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
  9924. goto err_pf_reset;
  9925. }
  9926. pf->pfr_count++;
  9927. hw->aq.num_arq_entries = I40E_AQ_LEN;
  9928. hw->aq.num_asq_entries = I40E_AQ_LEN;
  9929. hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
  9930. hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
  9931. pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
  9932. snprintf(pf->int_name, sizeof(pf->int_name) - 1,
  9933. "%s-%s:misc",
  9934. dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
  9935. err = i40e_init_shared_code(hw);
  9936. if (err) {
  9937. dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
  9938. err);
  9939. goto err_pf_reset;
  9940. }
  9941. /* set up a default setting for link flow control */
  9942. pf->hw.fc.requested_mode = I40E_FC_NONE;
  9943. err = i40e_init_adminq(hw);
  9944. if (err) {
  9945. if (err == I40E_ERR_FIRMWARE_API_VERSION)
  9946. dev_info(&pdev->dev,
  9947. "The driver for the device stopped because the NVM image is newer than expected. You must install the most recent version of the network driver.\n");
  9948. else
  9949. dev_info(&pdev->dev,
  9950. "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
  9951. goto err_pf_reset;
  9952. }
  9953. i40e_get_oem_version(hw);
  9954. /* provide nvm, fw, api versions */
  9955. dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s\n",
  9956. hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
  9957. hw->aq.api_maj_ver, hw->aq.api_min_ver,
  9958. i40e_nvm_version_str(hw));
  9959. if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
  9960. hw->aq.api_min_ver > I40E_FW_API_VERSION_MINOR)
  9961. dev_info(&pdev->dev,
  9962. "The driver for the device detected a newer version of the NVM image than expected. Please install the most recent version of the network driver.\n");
  9963. else if (hw->aq.api_maj_ver < I40E_FW_API_VERSION_MAJOR ||
  9964. hw->aq.api_min_ver < (I40E_FW_API_VERSION_MINOR - 1))
  9965. dev_info(&pdev->dev,
  9966. "The driver for the device detected an older version of the NVM image than expected. Please update the NVM image.\n");
  9967. i40e_verify_eeprom(pf);
  9968. /* Rev 0 hardware was never productized */
  9969. if (hw->revision_id < 1)
  9970. 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");
  9971. i40e_clear_pxe_mode(hw);
  9972. err = i40e_get_capabilities(pf);
  9973. if (err)
  9974. goto err_adminq_setup;
  9975. err = i40e_sw_init(pf);
  9976. if (err) {
  9977. dev_info(&pdev->dev, "sw_init failed: %d\n", err);
  9978. goto err_sw_init;
  9979. }
  9980. err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
  9981. hw->func_caps.num_rx_qp, 0, 0);
  9982. if (err) {
  9983. dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
  9984. goto err_init_lan_hmc;
  9985. }
  9986. err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
  9987. if (err) {
  9988. dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
  9989. err = -ENOENT;
  9990. goto err_configure_lan_hmc;
  9991. }
  9992. /* Disable LLDP for NICs that have firmware versions lower than v4.3.
  9993. * Ignore error return codes because if it was already disabled via
  9994. * hardware settings this will fail
  9995. */
  9996. if (pf->hw_features & I40E_HW_STOP_FW_LLDP) {
  9997. dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
  9998. i40e_aq_stop_lldp(hw, true, NULL);
  9999. }
  10000. /* allow a platform config to override the HW addr */
  10001. i40e_get_platform_mac_addr(pdev, pf);
  10002. if (!is_valid_ether_addr(hw->mac.addr)) {
  10003. dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
  10004. err = -EIO;
  10005. goto err_mac_addr;
  10006. }
  10007. dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
  10008. ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
  10009. i40e_get_port_mac_addr(hw, hw->mac.port_addr);
  10010. if (is_valid_ether_addr(hw->mac.port_addr))
  10011. pf->hw_features |= I40E_HW_PORT_ID_VALID;
  10012. pci_set_drvdata(pdev, pf);
  10013. pci_save_state(pdev);
  10014. #ifdef CONFIG_I40E_DCB
  10015. err = i40e_init_pf_dcb(pf);
  10016. if (err) {
  10017. dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
  10018. pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
  10019. /* Continue without DCB enabled */
  10020. }
  10021. #endif /* CONFIG_I40E_DCB */
  10022. /* set up periodic task facility */
  10023. setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
  10024. pf->service_timer_period = HZ;
  10025. INIT_WORK(&pf->service_task, i40e_service_task);
  10026. clear_bit(__I40E_SERVICE_SCHED, pf->state);
  10027. /* NVM bit on means WoL disabled for the port */
  10028. i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
  10029. if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
  10030. pf->wol_en = false;
  10031. else
  10032. pf->wol_en = true;
  10033. device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
  10034. /* set up the main switch operations */
  10035. i40e_determine_queue_usage(pf);
  10036. err = i40e_init_interrupt_scheme(pf);
  10037. if (err)
  10038. goto err_switch_setup;
  10039. /* The number of VSIs reported by the FW is the minimum guaranteed
  10040. * to us; HW supports far more and we share the remaining pool with
  10041. * the other PFs. We allocate space for more than the guarantee with
  10042. * the understanding that we might not get them all later.
  10043. */
  10044. if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
  10045. pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
  10046. else
  10047. pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
  10048. /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
  10049. pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
  10050. GFP_KERNEL);
  10051. if (!pf->vsi) {
  10052. err = -ENOMEM;
  10053. goto err_switch_setup;
  10054. }
  10055. #ifdef CONFIG_PCI_IOV
  10056. /* prep for VF support */
  10057. if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
  10058. (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
  10059. !test_bit(__I40E_BAD_EEPROM, pf->state)) {
  10060. if (pci_num_vf(pdev))
  10061. pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
  10062. }
  10063. #endif
  10064. err = i40e_setup_pf_switch(pf, false);
  10065. if (err) {
  10066. dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
  10067. goto err_vsis;
  10068. }
  10069. /* Make sure flow control is set according to current settings */
  10070. err = i40e_set_fc(hw, &set_fc_aq_fail, true);
  10071. if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_GET)
  10072. dev_dbg(&pf->pdev->dev,
  10073. "Set fc with err %s aq_err %s on get_phy_cap\n",
  10074. i40e_stat_str(hw, err),
  10075. i40e_aq_str(hw, hw->aq.asq_last_status));
  10076. if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_SET)
  10077. dev_dbg(&pf->pdev->dev,
  10078. "Set fc with err %s aq_err %s on set_phy_config\n",
  10079. i40e_stat_str(hw, err),
  10080. i40e_aq_str(hw, hw->aq.asq_last_status));
  10081. if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_UPDATE)
  10082. dev_dbg(&pf->pdev->dev,
  10083. "Set fc with err %s aq_err %s on get_link_info\n",
  10084. i40e_stat_str(hw, err),
  10085. i40e_aq_str(hw, hw->aq.asq_last_status));
  10086. /* if FDIR VSI was set up, start it now */
  10087. for (i = 0; i < pf->num_alloc_vsi; i++) {
  10088. if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
  10089. i40e_vsi_open(pf->vsi[i]);
  10090. break;
  10091. }
  10092. }
  10093. /* The driver only wants link up/down and module qualification
  10094. * reports from firmware. Note the negative logic.
  10095. */
  10096. err = i40e_aq_set_phy_int_mask(&pf->hw,
  10097. ~(I40E_AQ_EVENT_LINK_UPDOWN |
  10098. I40E_AQ_EVENT_MEDIA_NA |
  10099. I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
  10100. if (err)
  10101. dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
  10102. i40e_stat_str(&pf->hw, err),
  10103. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  10104. /* Reconfigure hardware for allowing smaller MSS in the case
  10105. * of TSO, so that we avoid the MDD being fired and causing
  10106. * a reset in the case of small MSS+TSO.
  10107. */
  10108. val = rd32(hw, I40E_REG_MSS);
  10109. if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
  10110. val &= ~I40E_REG_MSS_MIN_MASK;
  10111. val |= I40E_64BYTE_MSS;
  10112. wr32(hw, I40E_REG_MSS, val);
  10113. }
  10114. if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
  10115. msleep(75);
  10116. err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
  10117. if (err)
  10118. dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
  10119. i40e_stat_str(&pf->hw, err),
  10120. i40e_aq_str(&pf->hw,
  10121. pf->hw.aq.asq_last_status));
  10122. }
  10123. /* The main driver is (mostly) up and happy. We need to set this state
  10124. * before setting up the misc vector or we get a race and the vector
  10125. * ends up disabled forever.
  10126. */
  10127. clear_bit(__I40E_DOWN, pf->state);
  10128. /* In case of MSIX we are going to setup the misc vector right here
  10129. * to handle admin queue events etc. In case of legacy and MSI
  10130. * the misc functionality and queue processing is combined in
  10131. * the same vector and that gets setup at open.
  10132. */
  10133. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  10134. err = i40e_setup_misc_vector(pf);
  10135. if (err) {
  10136. dev_info(&pdev->dev,
  10137. "setup of misc vector failed: %d\n", err);
  10138. goto err_vsis;
  10139. }
  10140. }
  10141. #ifdef CONFIG_PCI_IOV
  10142. /* prep for VF support */
  10143. if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
  10144. (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
  10145. !test_bit(__I40E_BAD_EEPROM, pf->state)) {
  10146. /* disable link interrupts for VFs */
  10147. val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
  10148. val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
  10149. wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
  10150. i40e_flush(hw);
  10151. if (pci_num_vf(pdev)) {
  10152. dev_info(&pdev->dev,
  10153. "Active VFs found, allocating resources.\n");
  10154. err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
  10155. if (err)
  10156. dev_info(&pdev->dev,
  10157. "Error %d allocating resources for existing VFs\n",
  10158. err);
  10159. }
  10160. }
  10161. #endif /* CONFIG_PCI_IOV */
  10162. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  10163. pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
  10164. pf->num_iwarp_msix,
  10165. I40E_IWARP_IRQ_PILE_ID);
  10166. if (pf->iwarp_base_vector < 0) {
  10167. dev_info(&pdev->dev,
  10168. "failed to get tracking for %d vectors for IWARP err=%d\n",
  10169. pf->num_iwarp_msix, pf->iwarp_base_vector);
  10170. pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
  10171. }
  10172. }
  10173. i40e_dbg_pf_init(pf);
  10174. /* tell the firmware that we're starting */
  10175. i40e_send_version(pf);
  10176. /* since everything's happy, start the service_task timer */
  10177. mod_timer(&pf->service_timer,
  10178. round_jiffies(jiffies + pf->service_timer_period));
  10179. /* add this PF to client device list and launch a client service task */
  10180. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  10181. err = i40e_lan_add_device(pf);
  10182. if (err)
  10183. dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
  10184. err);
  10185. }
  10186. #define PCI_SPEED_SIZE 8
  10187. #define PCI_WIDTH_SIZE 8
  10188. /* Devices on the IOSF bus do not have this information
  10189. * and will report PCI Gen 1 x 1 by default so don't bother
  10190. * checking them.
  10191. */
  10192. if (!(pf->hw_features & I40E_HW_NO_PCI_LINK_CHECK)) {
  10193. char speed[PCI_SPEED_SIZE] = "Unknown";
  10194. char width[PCI_WIDTH_SIZE] = "Unknown";
  10195. /* Get the negotiated link width and speed from PCI config
  10196. * space
  10197. */
  10198. pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
  10199. &link_status);
  10200. i40e_set_pci_config_data(hw, link_status);
  10201. switch (hw->bus.speed) {
  10202. case i40e_bus_speed_8000:
  10203. strncpy(speed, "8.0", PCI_SPEED_SIZE); break;
  10204. case i40e_bus_speed_5000:
  10205. strncpy(speed, "5.0", PCI_SPEED_SIZE); break;
  10206. case i40e_bus_speed_2500:
  10207. strncpy(speed, "2.5", PCI_SPEED_SIZE); break;
  10208. default:
  10209. break;
  10210. }
  10211. switch (hw->bus.width) {
  10212. case i40e_bus_width_pcie_x8:
  10213. strncpy(width, "8", PCI_WIDTH_SIZE); break;
  10214. case i40e_bus_width_pcie_x4:
  10215. strncpy(width, "4", PCI_WIDTH_SIZE); break;
  10216. case i40e_bus_width_pcie_x2:
  10217. strncpy(width, "2", PCI_WIDTH_SIZE); break;
  10218. case i40e_bus_width_pcie_x1:
  10219. strncpy(width, "1", PCI_WIDTH_SIZE); break;
  10220. default:
  10221. break;
  10222. }
  10223. dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
  10224. speed, width);
  10225. if (hw->bus.width < i40e_bus_width_pcie_x8 ||
  10226. hw->bus.speed < i40e_bus_speed_8000) {
  10227. dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
  10228. dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
  10229. }
  10230. }
  10231. /* get the requested speeds from the fw */
  10232. err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
  10233. if (err)
  10234. dev_dbg(&pf->pdev->dev, "get requested speeds ret = %s last_status = %s\n",
  10235. i40e_stat_str(&pf->hw, err),
  10236. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  10237. pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
  10238. /* get the supported phy types from the fw */
  10239. err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
  10240. if (err)
  10241. dev_dbg(&pf->pdev->dev, "get supported phy types ret = %s last_status = %s\n",
  10242. i40e_stat_str(&pf->hw, err),
  10243. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  10244. /* Add a filter to drop all Flow control frames from any VSI from being
  10245. * transmitted. By doing so we stop a malicious VF from sending out
  10246. * PAUSE or PFC frames and potentially controlling traffic for other
  10247. * PF/VF VSIs.
  10248. * The FW can still send Flow control frames if enabled.
  10249. */
  10250. i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
  10251. pf->main_vsi_seid);
  10252. if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
  10253. (pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
  10254. pf->hw_features |= I40E_HW_PHY_CONTROLS_LEDS;
  10255. if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
  10256. pf->hw_features |= I40E_HW_HAVE_CRT_RETIMER;
  10257. /* print a string summarizing features */
  10258. i40e_print_features(pf);
  10259. return 0;
  10260. /* Unwind what we've done if something failed in the setup */
  10261. err_vsis:
  10262. set_bit(__I40E_DOWN, pf->state);
  10263. i40e_clear_interrupt_scheme(pf);
  10264. kfree(pf->vsi);
  10265. err_switch_setup:
  10266. i40e_reset_interrupt_capability(pf);
  10267. del_timer_sync(&pf->service_timer);
  10268. err_mac_addr:
  10269. err_configure_lan_hmc:
  10270. (void)i40e_shutdown_lan_hmc(hw);
  10271. err_init_lan_hmc:
  10272. kfree(pf->qp_pile);
  10273. err_sw_init:
  10274. err_adminq_setup:
  10275. err_pf_reset:
  10276. iounmap(hw->hw_addr);
  10277. err_ioremap:
  10278. kfree(pf);
  10279. err_pf_alloc:
  10280. pci_disable_pcie_error_reporting(pdev);
  10281. pci_release_mem_regions(pdev);
  10282. err_pci_reg:
  10283. err_dma:
  10284. pci_disable_device(pdev);
  10285. return err;
  10286. }
  10287. /**
  10288. * i40e_remove - Device removal routine
  10289. * @pdev: PCI device information struct
  10290. *
  10291. * i40e_remove is called by the PCI subsystem to alert the driver
  10292. * that is should release a PCI device. This could be caused by a
  10293. * Hot-Plug event, or because the driver is going to be removed from
  10294. * memory.
  10295. **/
  10296. static void i40e_remove(struct pci_dev *pdev)
  10297. {
  10298. struct i40e_pf *pf = pci_get_drvdata(pdev);
  10299. struct i40e_hw *hw = &pf->hw;
  10300. i40e_status ret_code;
  10301. int i;
  10302. i40e_dbg_pf_exit(pf);
  10303. i40e_ptp_stop(pf);
  10304. /* Disable RSS in hw */
  10305. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
  10306. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
  10307. /* no more scheduling of any task */
  10308. set_bit(__I40E_SUSPENDED, pf->state);
  10309. set_bit(__I40E_DOWN, pf->state);
  10310. if (pf->service_timer.data)
  10311. del_timer_sync(&pf->service_timer);
  10312. if (pf->service_task.func)
  10313. cancel_work_sync(&pf->service_task);
  10314. /* Client close must be called explicitly here because the timer
  10315. * has been stopped.
  10316. */
  10317. i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
  10318. if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
  10319. i40e_free_vfs(pf);
  10320. pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
  10321. }
  10322. i40e_fdir_teardown(pf);
  10323. /* If there is a switch structure or any orphans, remove them.
  10324. * This will leave only the PF's VSI remaining.
  10325. */
  10326. for (i = 0; i < I40E_MAX_VEB; i++) {
  10327. if (!pf->veb[i])
  10328. continue;
  10329. if (pf->veb[i]->uplink_seid == pf->mac_seid ||
  10330. pf->veb[i]->uplink_seid == 0)
  10331. i40e_switch_branch_release(pf->veb[i]);
  10332. }
  10333. /* Now we can shutdown the PF's VSI, just before we kill
  10334. * adminq and hmc.
  10335. */
  10336. if (pf->vsi[pf->lan_vsi])
  10337. i40e_vsi_release(pf->vsi[pf->lan_vsi]);
  10338. /* remove attached clients */
  10339. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  10340. ret_code = i40e_lan_del_device(pf);
  10341. if (ret_code)
  10342. dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
  10343. ret_code);
  10344. }
  10345. /* shutdown and destroy the HMC */
  10346. if (hw->hmc.hmc_obj) {
  10347. ret_code = i40e_shutdown_lan_hmc(hw);
  10348. if (ret_code)
  10349. dev_warn(&pdev->dev,
  10350. "Failed to destroy the HMC resources: %d\n",
  10351. ret_code);
  10352. }
  10353. /* shutdown the adminq */
  10354. i40e_shutdown_adminq(hw);
  10355. /* destroy the locks only once, here */
  10356. mutex_destroy(&hw->aq.arq_mutex);
  10357. mutex_destroy(&hw->aq.asq_mutex);
  10358. /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
  10359. i40e_clear_interrupt_scheme(pf);
  10360. for (i = 0; i < pf->num_alloc_vsi; i++) {
  10361. if (pf->vsi[i]) {
  10362. i40e_vsi_clear_rings(pf->vsi[i]);
  10363. i40e_vsi_clear(pf->vsi[i]);
  10364. pf->vsi[i] = NULL;
  10365. }
  10366. }
  10367. for (i = 0; i < I40E_MAX_VEB; i++) {
  10368. kfree(pf->veb[i]);
  10369. pf->veb[i] = NULL;
  10370. }
  10371. kfree(pf->qp_pile);
  10372. kfree(pf->vsi);
  10373. iounmap(hw->hw_addr);
  10374. kfree(pf);
  10375. pci_release_mem_regions(pdev);
  10376. pci_disable_pcie_error_reporting(pdev);
  10377. pci_disable_device(pdev);
  10378. }
  10379. /**
  10380. * i40e_pci_error_detected - warning that something funky happened in PCI land
  10381. * @pdev: PCI device information struct
  10382. *
  10383. * Called to warn that something happened and the error handling steps
  10384. * are in progress. Allows the driver to quiesce things, be ready for
  10385. * remediation.
  10386. **/
  10387. static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
  10388. enum pci_channel_state error)
  10389. {
  10390. struct i40e_pf *pf = pci_get_drvdata(pdev);
  10391. dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
  10392. if (!pf) {
  10393. dev_info(&pdev->dev,
  10394. "Cannot recover - error happened during device probe\n");
  10395. return PCI_ERS_RESULT_DISCONNECT;
  10396. }
  10397. /* shutdown all operations */
  10398. if (!test_bit(__I40E_SUSPENDED, pf->state))
  10399. i40e_prep_for_reset(pf, false);
  10400. /* Request a slot reset */
  10401. return PCI_ERS_RESULT_NEED_RESET;
  10402. }
  10403. /**
  10404. * i40e_pci_error_slot_reset - a PCI slot reset just happened
  10405. * @pdev: PCI device information struct
  10406. *
  10407. * Called to find if the driver can work with the device now that
  10408. * the pci slot has been reset. If a basic connection seems good
  10409. * (registers are readable and have sane content) then return a
  10410. * happy little PCI_ERS_RESULT_xxx.
  10411. **/
  10412. static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
  10413. {
  10414. struct i40e_pf *pf = pci_get_drvdata(pdev);
  10415. pci_ers_result_t result;
  10416. int err;
  10417. u32 reg;
  10418. dev_dbg(&pdev->dev, "%s\n", __func__);
  10419. if (pci_enable_device_mem(pdev)) {
  10420. dev_info(&pdev->dev,
  10421. "Cannot re-enable PCI device after reset.\n");
  10422. result = PCI_ERS_RESULT_DISCONNECT;
  10423. } else {
  10424. pci_set_master(pdev);
  10425. pci_restore_state(pdev);
  10426. pci_save_state(pdev);
  10427. pci_wake_from_d3(pdev, false);
  10428. reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  10429. if (reg == 0)
  10430. result = PCI_ERS_RESULT_RECOVERED;
  10431. else
  10432. result = PCI_ERS_RESULT_DISCONNECT;
  10433. }
  10434. err = pci_cleanup_aer_uncorrect_error_status(pdev);
  10435. if (err) {
  10436. dev_info(&pdev->dev,
  10437. "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
  10438. err);
  10439. /* non-fatal, continue */
  10440. }
  10441. return result;
  10442. }
  10443. /**
  10444. * i40e_pci_error_resume - restart operations after PCI error recovery
  10445. * @pdev: PCI device information struct
  10446. *
  10447. * Called to allow the driver to bring things back up after PCI error
  10448. * and/or reset recovery has finished.
  10449. **/
  10450. static void i40e_pci_error_resume(struct pci_dev *pdev)
  10451. {
  10452. struct i40e_pf *pf = pci_get_drvdata(pdev);
  10453. dev_dbg(&pdev->dev, "%s\n", __func__);
  10454. if (test_bit(__I40E_SUSPENDED, pf->state))
  10455. return;
  10456. i40e_handle_reset_warning(pf, false);
  10457. }
  10458. /**
  10459. * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
  10460. * using the mac_address_write admin q function
  10461. * @pf: pointer to i40e_pf struct
  10462. **/
  10463. static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
  10464. {
  10465. struct i40e_hw *hw = &pf->hw;
  10466. i40e_status ret;
  10467. u8 mac_addr[6];
  10468. u16 flags = 0;
  10469. /* Get current MAC address in case it's an LAA */
  10470. if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
  10471. ether_addr_copy(mac_addr,
  10472. pf->vsi[pf->lan_vsi]->netdev->dev_addr);
  10473. } else {
  10474. dev_err(&pf->pdev->dev,
  10475. "Failed to retrieve MAC address; using default\n");
  10476. ether_addr_copy(mac_addr, hw->mac.addr);
  10477. }
  10478. /* The FW expects the mac address write cmd to first be called with
  10479. * one of these flags before calling it again with the multicast
  10480. * enable flags.
  10481. */
  10482. flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
  10483. if (hw->func_caps.flex10_enable && hw->partition_id != 1)
  10484. flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
  10485. ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
  10486. if (ret) {
  10487. dev_err(&pf->pdev->dev,
  10488. "Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
  10489. return;
  10490. }
  10491. flags = I40E_AQC_MC_MAG_EN
  10492. | I40E_AQC_WOL_PRESERVE_ON_PFR
  10493. | I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
  10494. ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
  10495. if (ret)
  10496. dev_err(&pf->pdev->dev,
  10497. "Failed to enable Multicast Magic Packet wake up\n");
  10498. }
  10499. /**
  10500. * i40e_shutdown - PCI callback for shutting down
  10501. * @pdev: PCI device information struct
  10502. **/
  10503. static void i40e_shutdown(struct pci_dev *pdev)
  10504. {
  10505. struct i40e_pf *pf = pci_get_drvdata(pdev);
  10506. struct i40e_hw *hw = &pf->hw;
  10507. set_bit(__I40E_SUSPENDED, pf->state);
  10508. set_bit(__I40E_DOWN, pf->state);
  10509. rtnl_lock();
  10510. i40e_prep_for_reset(pf, true);
  10511. rtnl_unlock();
  10512. wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
  10513. wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
  10514. del_timer_sync(&pf->service_timer);
  10515. cancel_work_sync(&pf->service_task);
  10516. i40e_fdir_teardown(pf);
  10517. /* Client close must be called explicitly here because the timer
  10518. * has been stopped.
  10519. */
  10520. i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
  10521. if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
  10522. i40e_enable_mc_magic_wake(pf);
  10523. i40e_prep_for_reset(pf, false);
  10524. wr32(hw, I40E_PFPM_APM,
  10525. (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
  10526. wr32(hw, I40E_PFPM_WUFC,
  10527. (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
  10528. i40e_clear_interrupt_scheme(pf);
  10529. if (system_state == SYSTEM_POWER_OFF) {
  10530. pci_wake_from_d3(pdev, pf->wol_en);
  10531. pci_set_power_state(pdev, PCI_D3hot);
  10532. }
  10533. }
  10534. #ifdef CONFIG_PM
  10535. /**
  10536. * i40e_suspend - PCI callback for moving to D3
  10537. * @pdev: PCI device information struct
  10538. **/
  10539. static int i40e_suspend(struct pci_dev *pdev, pm_message_t state)
  10540. {
  10541. struct i40e_pf *pf = pci_get_drvdata(pdev);
  10542. struct i40e_hw *hw = &pf->hw;
  10543. int retval = 0;
  10544. set_bit(__I40E_SUSPENDED, pf->state);
  10545. set_bit(__I40E_DOWN, pf->state);
  10546. if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
  10547. i40e_enable_mc_magic_wake(pf);
  10548. i40e_prep_for_reset(pf, false);
  10549. wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
  10550. wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
  10551. i40e_stop_misc_vector(pf);
  10552. if (pf->msix_entries) {
  10553. synchronize_irq(pf->msix_entries[0].vector);
  10554. free_irq(pf->msix_entries[0].vector, pf);
  10555. }
  10556. retval = pci_save_state(pdev);
  10557. if (retval)
  10558. return retval;
  10559. pci_wake_from_d3(pdev, pf->wol_en);
  10560. pci_set_power_state(pdev, PCI_D3hot);
  10561. return retval;
  10562. }
  10563. /**
  10564. * i40e_resume - PCI callback for waking up from D3
  10565. * @pdev: PCI device information struct
  10566. **/
  10567. static int i40e_resume(struct pci_dev *pdev)
  10568. {
  10569. struct i40e_pf *pf = pci_get_drvdata(pdev);
  10570. u32 err;
  10571. pci_set_power_state(pdev, PCI_D0);
  10572. pci_restore_state(pdev);
  10573. /* pci_restore_state() clears dev->state_saves, so
  10574. * call pci_save_state() again to restore it.
  10575. */
  10576. pci_save_state(pdev);
  10577. err = pci_enable_device_mem(pdev);
  10578. if (err) {
  10579. dev_err(&pdev->dev, "Cannot enable PCI device from suspend\n");
  10580. return err;
  10581. }
  10582. pci_set_master(pdev);
  10583. /* no wakeup events while running */
  10584. pci_wake_from_d3(pdev, false);
  10585. /* handling the reset will rebuild the device state */
  10586. if (test_and_clear_bit(__I40E_SUSPENDED, pf->state)) {
  10587. clear_bit(__I40E_DOWN, pf->state);
  10588. if (pf->msix_entries) {
  10589. err = request_irq(pf->msix_entries[0].vector,
  10590. i40e_intr, 0, pf->int_name, pf);
  10591. if (err) {
  10592. dev_err(&pf->pdev->dev,
  10593. "request_irq for %s failed: %d\n",
  10594. pf->int_name, err);
  10595. }
  10596. }
  10597. i40e_reset_and_rebuild(pf, false, false);
  10598. }
  10599. return 0;
  10600. }
  10601. #endif
  10602. static const struct pci_error_handlers i40e_err_handler = {
  10603. .error_detected = i40e_pci_error_detected,
  10604. .slot_reset = i40e_pci_error_slot_reset,
  10605. .resume = i40e_pci_error_resume,
  10606. };
  10607. static struct pci_driver i40e_driver = {
  10608. .name = i40e_driver_name,
  10609. .id_table = i40e_pci_tbl,
  10610. .probe = i40e_probe,
  10611. .remove = i40e_remove,
  10612. #ifdef CONFIG_PM
  10613. .suspend = i40e_suspend,
  10614. .resume = i40e_resume,
  10615. #endif
  10616. .shutdown = i40e_shutdown,
  10617. .err_handler = &i40e_err_handler,
  10618. .sriov_configure = i40e_pci_sriov_configure,
  10619. };
  10620. /**
  10621. * i40e_init_module - Driver registration routine
  10622. *
  10623. * i40e_init_module is the first routine called when the driver is
  10624. * loaded. All it does is register with the PCI subsystem.
  10625. **/
  10626. static int __init i40e_init_module(void)
  10627. {
  10628. pr_info("%s: %s - version %s\n", i40e_driver_name,
  10629. i40e_driver_string, i40e_driver_version_str);
  10630. pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
  10631. /* There is no need to throttle the number of active tasks because
  10632. * each device limits its own task using a state bit for scheduling
  10633. * the service task, and the device tasks do not interfere with each
  10634. * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
  10635. * since we need to be able to guarantee forward progress even under
  10636. * memory pressure.
  10637. */
  10638. i40e_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, i40e_driver_name);
  10639. if (!i40e_wq) {
  10640. pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
  10641. return -ENOMEM;
  10642. }
  10643. i40e_dbg_init();
  10644. return pci_register_driver(&i40e_driver);
  10645. }
  10646. module_init(i40e_init_module);
  10647. /**
  10648. * i40e_exit_module - Driver exit cleanup routine
  10649. *
  10650. * i40e_exit_module is called just before the driver is removed
  10651. * from memory.
  10652. **/
  10653. static void __exit i40e_exit_module(void)
  10654. {
  10655. pci_unregister_driver(&i40e_driver);
  10656. destroy_workqueue(i40e_wq);
  10657. i40e_dbg_exit();
  10658. }
  10659. module_exit(i40e_exit_module);