i40e_main.c 325 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782
  1. /*******************************************************************************
  2. *
  3. * Intel Ethernet Controller XL710 Family Linux Driver
  4. * Copyright(c) 2013 - 2016 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. /* Local includes */
  30. #include "i40e.h"
  31. #include "i40e_diag.h"
  32. #include <net/udp_tunnel.h>
  33. const char i40e_driver_name[] = "i40e";
  34. static const char i40e_driver_string[] =
  35. "Intel(R) Ethernet Connection XL710 Network Driver";
  36. #define DRV_KERN "-k"
  37. #define DRV_VERSION_MAJOR 2
  38. #define DRV_VERSION_MINOR 1
  39. #define DRV_VERSION_BUILD 7
  40. #define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
  41. __stringify(DRV_VERSION_MINOR) "." \
  42. __stringify(DRV_VERSION_BUILD) DRV_KERN
  43. const char i40e_driver_version_str[] = DRV_VERSION;
  44. static const char i40e_copyright[] = "Copyright (c) 2013 - 2014 Intel Corporation.";
  45. /* a bit of forward declarations */
  46. static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
  47. static void i40e_handle_reset_warning(struct i40e_pf *pf);
  48. static int i40e_add_vsi(struct i40e_vsi *vsi);
  49. static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
  50. static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit);
  51. static int i40e_setup_misc_vector(struct i40e_pf *pf);
  52. static void i40e_determine_queue_usage(struct i40e_pf *pf);
  53. static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
  54. static void i40e_fdir_sb_setup(struct i40e_pf *pf);
  55. static int i40e_veb_get_bw_info(struct i40e_veb *veb);
  56. /* i40e_pci_tbl - PCI Device ID Table
  57. *
  58. * Last entry must be all 0s
  59. *
  60. * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
  61. * Class, Class Mask, private data (not used) }
  62. */
  63. static const struct pci_device_id i40e_pci_tbl[] = {
  64. {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
  65. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
  66. {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
  67. {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
  68. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
  69. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
  70. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
  71. {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
  72. {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
  73. {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
  74. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
  75. {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
  76. {PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
  77. {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
  78. {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
  79. {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
  80. {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
  81. {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
  82. {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
  83. /* required last entry */
  84. {0, }
  85. };
  86. MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
  87. #define I40E_MAX_VF_COUNT 128
  88. static int debug = -1;
  89. module_param(debug, uint, 0);
  90. MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
  91. MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
  92. MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
  93. MODULE_LICENSE("GPL");
  94. MODULE_VERSION(DRV_VERSION);
  95. static struct workqueue_struct *i40e_wq;
  96. /**
  97. * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
  98. * @hw: pointer to the HW structure
  99. * @mem: ptr to mem struct to fill out
  100. * @size: size of memory requested
  101. * @alignment: what to align the allocation to
  102. **/
  103. int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
  104. u64 size, u32 alignment)
  105. {
  106. struct i40e_pf *pf = (struct i40e_pf *)hw->back;
  107. mem->size = ALIGN(size, alignment);
  108. mem->va = dma_zalloc_coherent(&pf->pdev->dev, mem->size,
  109. &mem->pa, GFP_KERNEL);
  110. if (!mem->va)
  111. return -ENOMEM;
  112. return 0;
  113. }
  114. /**
  115. * i40e_free_dma_mem_d - OS specific memory free for shared code
  116. * @hw: pointer to the HW structure
  117. * @mem: ptr to mem struct to free
  118. **/
  119. int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
  120. {
  121. struct i40e_pf *pf = (struct i40e_pf *)hw->back;
  122. dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
  123. mem->va = NULL;
  124. mem->pa = 0;
  125. mem->size = 0;
  126. return 0;
  127. }
  128. /**
  129. * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
  130. * @hw: pointer to the HW structure
  131. * @mem: ptr to mem struct to fill out
  132. * @size: size of memory requested
  133. **/
  134. int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
  135. u32 size)
  136. {
  137. mem->size = size;
  138. mem->va = kzalloc(size, GFP_KERNEL);
  139. if (!mem->va)
  140. return -ENOMEM;
  141. return 0;
  142. }
  143. /**
  144. * i40e_free_virt_mem_d - OS specific memory free for shared code
  145. * @hw: pointer to the HW structure
  146. * @mem: ptr to mem struct to free
  147. **/
  148. int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
  149. {
  150. /* it's ok to kfree a NULL pointer */
  151. kfree(mem->va);
  152. mem->va = NULL;
  153. mem->size = 0;
  154. return 0;
  155. }
  156. /**
  157. * i40e_get_lump - find a lump of free generic resource
  158. * @pf: board private structure
  159. * @pile: the pile of resource to search
  160. * @needed: the number of items needed
  161. * @id: an owner id to stick on the items assigned
  162. *
  163. * Returns the base item index of the lump, or negative for error
  164. *
  165. * The search_hint trick and lack of advanced fit-finding only work
  166. * because we're highly likely to have all the same size lump requests.
  167. * Linear search time and any fragmentation should be minimal.
  168. **/
  169. static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
  170. u16 needed, u16 id)
  171. {
  172. int ret = -ENOMEM;
  173. int i, j;
  174. if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
  175. dev_info(&pf->pdev->dev,
  176. "param err: pile=%p needed=%d id=0x%04x\n",
  177. pile, needed, id);
  178. return -EINVAL;
  179. }
  180. /* start the linear search with an imperfect hint */
  181. i = pile->search_hint;
  182. while (i < pile->num_entries) {
  183. /* skip already allocated entries */
  184. if (pile->list[i] & I40E_PILE_VALID_BIT) {
  185. i++;
  186. continue;
  187. }
  188. /* do we have enough in this lump? */
  189. for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
  190. if (pile->list[i+j] & I40E_PILE_VALID_BIT)
  191. break;
  192. }
  193. if (j == needed) {
  194. /* there was enough, so assign it to the requestor */
  195. for (j = 0; j < needed; j++)
  196. pile->list[i+j] = id | I40E_PILE_VALID_BIT;
  197. ret = i;
  198. pile->search_hint = i + j;
  199. break;
  200. }
  201. /* not enough, so skip over it and continue looking */
  202. i += j;
  203. }
  204. return ret;
  205. }
  206. /**
  207. * i40e_put_lump - return a lump of generic resource
  208. * @pile: the pile of resource to search
  209. * @index: the base item index
  210. * @id: the owner id of the items assigned
  211. *
  212. * Returns the count of items in the lump
  213. **/
  214. static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
  215. {
  216. int valid_id = (id | I40E_PILE_VALID_BIT);
  217. int count = 0;
  218. int i;
  219. if (!pile || index >= pile->num_entries)
  220. return -EINVAL;
  221. for (i = index;
  222. i < pile->num_entries && pile->list[i] == valid_id;
  223. i++) {
  224. pile->list[i] = 0;
  225. count++;
  226. }
  227. if (count && index < pile->search_hint)
  228. pile->search_hint = index;
  229. return count;
  230. }
  231. /**
  232. * i40e_find_vsi_from_id - searches for the vsi with the given id
  233. * @pf - the pf structure to search for the vsi
  234. * @id - id of the vsi it is searching for
  235. **/
  236. struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
  237. {
  238. int i;
  239. for (i = 0; i < pf->num_alloc_vsi; i++)
  240. if (pf->vsi[i] && (pf->vsi[i]->id == id))
  241. return pf->vsi[i];
  242. return NULL;
  243. }
  244. /**
  245. * i40e_service_event_schedule - Schedule the service task to wake up
  246. * @pf: board private structure
  247. *
  248. * If not already scheduled, this puts the task into the work queue
  249. **/
  250. void i40e_service_event_schedule(struct i40e_pf *pf)
  251. {
  252. if (!test_bit(__I40E_DOWN, &pf->state) &&
  253. !test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
  254. queue_work(i40e_wq, &pf->service_task);
  255. }
  256. /**
  257. * i40e_tx_timeout - Respond to a Tx Hang
  258. * @netdev: network interface device structure
  259. *
  260. * If any port has noticed a Tx timeout, it is likely that the whole
  261. * device is munged, not just the one netdev port, so go for the full
  262. * reset.
  263. **/
  264. static void i40e_tx_timeout(struct net_device *netdev)
  265. {
  266. struct i40e_netdev_priv *np = netdev_priv(netdev);
  267. struct i40e_vsi *vsi = np->vsi;
  268. struct i40e_pf *pf = vsi->back;
  269. struct i40e_ring *tx_ring = NULL;
  270. unsigned int i, hung_queue = 0;
  271. u32 head, val;
  272. pf->tx_timeout_count++;
  273. /* find the stopped queue the same way the stack does */
  274. for (i = 0; i < netdev->num_tx_queues; i++) {
  275. struct netdev_queue *q;
  276. unsigned long trans_start;
  277. q = netdev_get_tx_queue(netdev, i);
  278. trans_start = q->trans_start;
  279. if (netif_xmit_stopped(q) &&
  280. time_after(jiffies,
  281. (trans_start + netdev->watchdog_timeo))) {
  282. hung_queue = i;
  283. break;
  284. }
  285. }
  286. if (i == netdev->num_tx_queues) {
  287. netdev_info(netdev, "tx_timeout: no netdev hung queue found\n");
  288. } else {
  289. /* now that we have an index, find the tx_ring struct */
  290. for (i = 0; i < vsi->num_queue_pairs; i++) {
  291. if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
  292. if (hung_queue ==
  293. vsi->tx_rings[i]->queue_index) {
  294. tx_ring = vsi->tx_rings[i];
  295. break;
  296. }
  297. }
  298. }
  299. }
  300. if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
  301. pf->tx_timeout_recovery_level = 1; /* reset after some time */
  302. else if (time_before(jiffies,
  303. (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
  304. return; /* don't do any new action before the next timeout */
  305. if (tx_ring) {
  306. head = i40e_get_head(tx_ring);
  307. /* Read interrupt register */
  308. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  309. val = rd32(&pf->hw,
  310. I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
  311. tx_ring->vsi->base_vector - 1));
  312. else
  313. val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
  314. 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",
  315. vsi->seid, hung_queue, tx_ring->next_to_clean,
  316. head, tx_ring->next_to_use,
  317. readl(tx_ring->tail), val);
  318. }
  319. pf->tx_timeout_last_recovery = jiffies;
  320. netdev_info(netdev, "tx_timeout recovery level %d, hung_queue %d\n",
  321. pf->tx_timeout_recovery_level, hung_queue);
  322. switch (pf->tx_timeout_recovery_level) {
  323. case 1:
  324. set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
  325. break;
  326. case 2:
  327. set_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
  328. break;
  329. case 3:
  330. set_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
  331. break;
  332. default:
  333. netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
  334. break;
  335. }
  336. i40e_service_event_schedule(pf);
  337. pf->tx_timeout_recovery_level++;
  338. }
  339. /**
  340. * i40e_get_vsi_stats_struct - Get System Network Statistics
  341. * @vsi: the VSI we care about
  342. *
  343. * Returns the address of the device statistics structure.
  344. * The statistics are actually updated from the service task.
  345. **/
  346. struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
  347. {
  348. return &vsi->net_stats;
  349. }
  350. /**
  351. * i40e_get_netdev_stats_struct - Get statistics for netdev interface
  352. * @netdev: network interface device structure
  353. *
  354. * Returns the address of the device statistics structure.
  355. * The statistics are actually updated from the service task.
  356. **/
  357. static void i40e_get_netdev_stats_struct(struct net_device *netdev,
  358. struct rtnl_link_stats64 *stats)
  359. {
  360. struct i40e_netdev_priv *np = netdev_priv(netdev);
  361. struct i40e_ring *tx_ring, *rx_ring;
  362. struct i40e_vsi *vsi = np->vsi;
  363. struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
  364. int i;
  365. if (test_bit(__I40E_DOWN, &vsi->state))
  366. return;
  367. if (!vsi->tx_rings)
  368. return;
  369. rcu_read_lock();
  370. for (i = 0; i < vsi->num_queue_pairs; i++) {
  371. u64 bytes, packets;
  372. unsigned int start;
  373. tx_ring = ACCESS_ONCE(vsi->tx_rings[i]);
  374. if (!tx_ring)
  375. continue;
  376. do {
  377. start = u64_stats_fetch_begin_irq(&tx_ring->syncp);
  378. packets = tx_ring->stats.packets;
  379. bytes = tx_ring->stats.bytes;
  380. } while (u64_stats_fetch_retry_irq(&tx_ring->syncp, start));
  381. stats->tx_packets += packets;
  382. stats->tx_bytes += bytes;
  383. rx_ring = &tx_ring[1];
  384. do {
  385. start = u64_stats_fetch_begin_irq(&rx_ring->syncp);
  386. packets = rx_ring->stats.packets;
  387. bytes = rx_ring->stats.bytes;
  388. } while (u64_stats_fetch_retry_irq(&rx_ring->syncp, start));
  389. stats->rx_packets += packets;
  390. stats->rx_bytes += bytes;
  391. }
  392. rcu_read_unlock();
  393. /* following stats updated by i40e_watchdog_subtask() */
  394. stats->multicast = vsi_stats->multicast;
  395. stats->tx_errors = vsi_stats->tx_errors;
  396. stats->tx_dropped = vsi_stats->tx_dropped;
  397. stats->rx_errors = vsi_stats->rx_errors;
  398. stats->rx_dropped = vsi_stats->rx_dropped;
  399. stats->rx_crc_errors = vsi_stats->rx_crc_errors;
  400. stats->rx_length_errors = vsi_stats->rx_length_errors;
  401. }
  402. /**
  403. * i40e_vsi_reset_stats - Resets all stats of the given vsi
  404. * @vsi: the VSI to have its stats reset
  405. **/
  406. void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
  407. {
  408. struct rtnl_link_stats64 *ns;
  409. int i;
  410. if (!vsi)
  411. return;
  412. ns = i40e_get_vsi_stats_struct(vsi);
  413. memset(ns, 0, sizeof(*ns));
  414. memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
  415. memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
  416. memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
  417. if (vsi->rx_rings && vsi->rx_rings[0]) {
  418. for (i = 0; i < vsi->num_queue_pairs; i++) {
  419. memset(&vsi->rx_rings[i]->stats, 0,
  420. sizeof(vsi->rx_rings[i]->stats));
  421. memset(&vsi->rx_rings[i]->rx_stats, 0,
  422. sizeof(vsi->rx_rings[i]->rx_stats));
  423. memset(&vsi->tx_rings[i]->stats, 0,
  424. sizeof(vsi->tx_rings[i]->stats));
  425. memset(&vsi->tx_rings[i]->tx_stats, 0,
  426. sizeof(vsi->tx_rings[i]->tx_stats));
  427. }
  428. }
  429. vsi->stat_offsets_loaded = false;
  430. }
  431. /**
  432. * i40e_pf_reset_stats - Reset all of the stats for the given PF
  433. * @pf: the PF to be reset
  434. **/
  435. void i40e_pf_reset_stats(struct i40e_pf *pf)
  436. {
  437. int i;
  438. memset(&pf->stats, 0, sizeof(pf->stats));
  439. memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
  440. pf->stat_offsets_loaded = false;
  441. for (i = 0; i < I40E_MAX_VEB; i++) {
  442. if (pf->veb[i]) {
  443. memset(&pf->veb[i]->stats, 0,
  444. sizeof(pf->veb[i]->stats));
  445. memset(&pf->veb[i]->stats_offsets, 0,
  446. sizeof(pf->veb[i]->stats_offsets));
  447. pf->veb[i]->stat_offsets_loaded = false;
  448. }
  449. }
  450. pf->hw_csum_rx_error = 0;
  451. }
  452. /**
  453. * i40e_stat_update48 - read and update a 48 bit stat from the chip
  454. * @hw: ptr to the hardware info
  455. * @hireg: the high 32 bit reg to read
  456. * @loreg: the low 32 bit reg to read
  457. * @offset_loaded: has the initial offset been loaded yet
  458. * @offset: ptr to current offset value
  459. * @stat: ptr to the stat
  460. *
  461. * Since the device stats are not reset at PFReset, they likely will not
  462. * be zeroed when the driver starts. We'll save the first values read
  463. * and use them as offsets to be subtracted from the raw values in order
  464. * to report stats that count from zero. In the process, we also manage
  465. * the potential roll-over.
  466. **/
  467. static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
  468. bool offset_loaded, u64 *offset, u64 *stat)
  469. {
  470. u64 new_data;
  471. if (hw->device_id == I40E_DEV_ID_QEMU) {
  472. new_data = rd32(hw, loreg);
  473. new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
  474. } else {
  475. new_data = rd64(hw, loreg);
  476. }
  477. if (!offset_loaded)
  478. *offset = new_data;
  479. if (likely(new_data >= *offset))
  480. *stat = new_data - *offset;
  481. else
  482. *stat = (new_data + BIT_ULL(48)) - *offset;
  483. *stat &= 0xFFFFFFFFFFFFULL;
  484. }
  485. /**
  486. * i40e_stat_update32 - read and update a 32 bit stat from the chip
  487. * @hw: ptr to the hardware info
  488. * @reg: the hw reg to read
  489. * @offset_loaded: has the initial offset been loaded yet
  490. * @offset: ptr to current offset value
  491. * @stat: ptr to the stat
  492. **/
  493. static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
  494. bool offset_loaded, u64 *offset, u64 *stat)
  495. {
  496. u32 new_data;
  497. new_data = rd32(hw, reg);
  498. if (!offset_loaded)
  499. *offset = new_data;
  500. if (likely(new_data >= *offset))
  501. *stat = (u32)(new_data - *offset);
  502. else
  503. *stat = (u32)((new_data + BIT_ULL(32)) - *offset);
  504. }
  505. /**
  506. * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
  507. * @vsi: the VSI to be updated
  508. **/
  509. void i40e_update_eth_stats(struct i40e_vsi *vsi)
  510. {
  511. int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
  512. struct i40e_pf *pf = vsi->back;
  513. struct i40e_hw *hw = &pf->hw;
  514. struct i40e_eth_stats *oes;
  515. struct i40e_eth_stats *es; /* device's eth stats */
  516. es = &vsi->eth_stats;
  517. oes = &vsi->eth_stats_offsets;
  518. /* Gather up the stats that the hw collects */
  519. i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
  520. vsi->stat_offsets_loaded,
  521. &oes->tx_errors, &es->tx_errors);
  522. i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
  523. vsi->stat_offsets_loaded,
  524. &oes->rx_discards, &es->rx_discards);
  525. i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
  526. vsi->stat_offsets_loaded,
  527. &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
  528. i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
  529. vsi->stat_offsets_loaded,
  530. &oes->tx_errors, &es->tx_errors);
  531. i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
  532. I40E_GLV_GORCL(stat_idx),
  533. vsi->stat_offsets_loaded,
  534. &oes->rx_bytes, &es->rx_bytes);
  535. i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
  536. I40E_GLV_UPRCL(stat_idx),
  537. vsi->stat_offsets_loaded,
  538. &oes->rx_unicast, &es->rx_unicast);
  539. i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
  540. I40E_GLV_MPRCL(stat_idx),
  541. vsi->stat_offsets_loaded,
  542. &oes->rx_multicast, &es->rx_multicast);
  543. i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
  544. I40E_GLV_BPRCL(stat_idx),
  545. vsi->stat_offsets_loaded,
  546. &oes->rx_broadcast, &es->rx_broadcast);
  547. i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
  548. I40E_GLV_GOTCL(stat_idx),
  549. vsi->stat_offsets_loaded,
  550. &oes->tx_bytes, &es->tx_bytes);
  551. i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
  552. I40E_GLV_UPTCL(stat_idx),
  553. vsi->stat_offsets_loaded,
  554. &oes->tx_unicast, &es->tx_unicast);
  555. i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
  556. I40E_GLV_MPTCL(stat_idx),
  557. vsi->stat_offsets_loaded,
  558. &oes->tx_multicast, &es->tx_multicast);
  559. i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
  560. I40E_GLV_BPTCL(stat_idx),
  561. vsi->stat_offsets_loaded,
  562. &oes->tx_broadcast, &es->tx_broadcast);
  563. vsi->stat_offsets_loaded = true;
  564. }
  565. /**
  566. * i40e_update_veb_stats - Update Switch component statistics
  567. * @veb: the VEB being updated
  568. **/
  569. static void i40e_update_veb_stats(struct i40e_veb *veb)
  570. {
  571. struct i40e_pf *pf = veb->pf;
  572. struct i40e_hw *hw = &pf->hw;
  573. struct i40e_eth_stats *oes;
  574. struct i40e_eth_stats *es; /* device's eth stats */
  575. struct i40e_veb_tc_stats *veb_oes;
  576. struct i40e_veb_tc_stats *veb_es;
  577. int i, idx = 0;
  578. idx = veb->stats_idx;
  579. es = &veb->stats;
  580. oes = &veb->stats_offsets;
  581. veb_es = &veb->tc_stats;
  582. veb_oes = &veb->tc_stats_offsets;
  583. /* Gather up the stats that the hw collects */
  584. i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
  585. veb->stat_offsets_loaded,
  586. &oes->tx_discards, &es->tx_discards);
  587. if (hw->revision_id > 0)
  588. i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
  589. veb->stat_offsets_loaded,
  590. &oes->rx_unknown_protocol,
  591. &es->rx_unknown_protocol);
  592. i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
  593. veb->stat_offsets_loaded,
  594. &oes->rx_bytes, &es->rx_bytes);
  595. i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
  596. veb->stat_offsets_loaded,
  597. &oes->rx_unicast, &es->rx_unicast);
  598. i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
  599. veb->stat_offsets_loaded,
  600. &oes->rx_multicast, &es->rx_multicast);
  601. i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
  602. veb->stat_offsets_loaded,
  603. &oes->rx_broadcast, &es->rx_broadcast);
  604. i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
  605. veb->stat_offsets_loaded,
  606. &oes->tx_bytes, &es->tx_bytes);
  607. i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
  608. veb->stat_offsets_loaded,
  609. &oes->tx_unicast, &es->tx_unicast);
  610. i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
  611. veb->stat_offsets_loaded,
  612. &oes->tx_multicast, &es->tx_multicast);
  613. i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
  614. veb->stat_offsets_loaded,
  615. &oes->tx_broadcast, &es->tx_broadcast);
  616. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  617. i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
  618. I40E_GLVEBTC_RPCL(i, idx),
  619. veb->stat_offsets_loaded,
  620. &veb_oes->tc_rx_packets[i],
  621. &veb_es->tc_rx_packets[i]);
  622. i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
  623. I40E_GLVEBTC_RBCL(i, idx),
  624. veb->stat_offsets_loaded,
  625. &veb_oes->tc_rx_bytes[i],
  626. &veb_es->tc_rx_bytes[i]);
  627. i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
  628. I40E_GLVEBTC_TPCL(i, idx),
  629. veb->stat_offsets_loaded,
  630. &veb_oes->tc_tx_packets[i],
  631. &veb_es->tc_tx_packets[i]);
  632. i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
  633. I40E_GLVEBTC_TBCL(i, idx),
  634. veb->stat_offsets_loaded,
  635. &veb_oes->tc_tx_bytes[i],
  636. &veb_es->tc_tx_bytes[i]);
  637. }
  638. veb->stat_offsets_loaded = true;
  639. }
  640. /**
  641. * i40e_update_vsi_stats - Update the vsi statistics counters.
  642. * @vsi: the VSI to be updated
  643. *
  644. * There are a few instances where we store the same stat in a
  645. * couple of different structs. This is partly because we have
  646. * the netdev stats that need to be filled out, which is slightly
  647. * different from the "eth_stats" defined by the chip and used in
  648. * VF communications. We sort it out here.
  649. **/
  650. static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
  651. {
  652. struct i40e_pf *pf = vsi->back;
  653. struct rtnl_link_stats64 *ons;
  654. struct rtnl_link_stats64 *ns; /* netdev stats */
  655. struct i40e_eth_stats *oes;
  656. struct i40e_eth_stats *es; /* device's eth stats */
  657. u32 tx_restart, tx_busy;
  658. u64 tx_lost_interrupt;
  659. struct i40e_ring *p;
  660. u32 rx_page, rx_buf;
  661. u64 bytes, packets;
  662. unsigned int start;
  663. u64 tx_linearize;
  664. u64 tx_force_wb;
  665. u64 rx_p, rx_b;
  666. u64 tx_p, tx_b;
  667. u16 q;
  668. if (test_bit(__I40E_DOWN, &vsi->state) ||
  669. test_bit(__I40E_CONFIG_BUSY, &pf->state))
  670. return;
  671. ns = i40e_get_vsi_stats_struct(vsi);
  672. ons = &vsi->net_stats_offsets;
  673. es = &vsi->eth_stats;
  674. oes = &vsi->eth_stats_offsets;
  675. /* Gather up the netdev and vsi stats that the driver collects
  676. * on the fly during packet processing
  677. */
  678. rx_b = rx_p = 0;
  679. tx_b = tx_p = 0;
  680. tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
  681. tx_lost_interrupt = 0;
  682. rx_page = 0;
  683. rx_buf = 0;
  684. rcu_read_lock();
  685. for (q = 0; q < vsi->num_queue_pairs; q++) {
  686. /* locate Tx ring */
  687. p = ACCESS_ONCE(vsi->tx_rings[q]);
  688. do {
  689. start = u64_stats_fetch_begin_irq(&p->syncp);
  690. packets = p->stats.packets;
  691. bytes = p->stats.bytes;
  692. } while (u64_stats_fetch_retry_irq(&p->syncp, start));
  693. tx_b += bytes;
  694. tx_p += packets;
  695. tx_restart += p->tx_stats.restart_queue;
  696. tx_busy += p->tx_stats.tx_busy;
  697. tx_linearize += p->tx_stats.tx_linearize;
  698. tx_force_wb += p->tx_stats.tx_force_wb;
  699. tx_lost_interrupt += p->tx_stats.tx_lost_interrupt;
  700. /* Rx queue is part of the same block as Tx queue */
  701. p = &p[1];
  702. do {
  703. start = u64_stats_fetch_begin_irq(&p->syncp);
  704. packets = p->stats.packets;
  705. bytes = p->stats.bytes;
  706. } while (u64_stats_fetch_retry_irq(&p->syncp, start));
  707. rx_b += bytes;
  708. rx_p += packets;
  709. rx_buf += p->rx_stats.alloc_buff_failed;
  710. rx_page += p->rx_stats.alloc_page_failed;
  711. }
  712. rcu_read_unlock();
  713. vsi->tx_restart = tx_restart;
  714. vsi->tx_busy = tx_busy;
  715. vsi->tx_linearize = tx_linearize;
  716. vsi->tx_force_wb = tx_force_wb;
  717. vsi->tx_lost_interrupt = tx_lost_interrupt;
  718. vsi->rx_page_failed = rx_page;
  719. vsi->rx_buf_failed = rx_buf;
  720. ns->rx_packets = rx_p;
  721. ns->rx_bytes = rx_b;
  722. ns->tx_packets = tx_p;
  723. ns->tx_bytes = tx_b;
  724. /* update netdev stats from eth stats */
  725. i40e_update_eth_stats(vsi);
  726. ons->tx_errors = oes->tx_errors;
  727. ns->tx_errors = es->tx_errors;
  728. ons->multicast = oes->rx_multicast;
  729. ns->multicast = es->rx_multicast;
  730. ons->rx_dropped = oes->rx_discards;
  731. ns->rx_dropped = es->rx_discards;
  732. ons->tx_dropped = oes->tx_discards;
  733. ns->tx_dropped = es->tx_discards;
  734. /* pull in a couple PF stats if this is the main vsi */
  735. if (vsi == pf->vsi[pf->lan_vsi]) {
  736. ns->rx_crc_errors = pf->stats.crc_errors;
  737. ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
  738. ns->rx_length_errors = pf->stats.rx_length_errors;
  739. }
  740. }
  741. /**
  742. * i40e_update_pf_stats - Update the PF statistics counters.
  743. * @pf: the PF to be updated
  744. **/
  745. static void i40e_update_pf_stats(struct i40e_pf *pf)
  746. {
  747. struct i40e_hw_port_stats *osd = &pf->stats_offsets;
  748. struct i40e_hw_port_stats *nsd = &pf->stats;
  749. struct i40e_hw *hw = &pf->hw;
  750. u32 val;
  751. int i;
  752. i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
  753. I40E_GLPRT_GORCL(hw->port),
  754. pf->stat_offsets_loaded,
  755. &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
  756. i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
  757. I40E_GLPRT_GOTCL(hw->port),
  758. pf->stat_offsets_loaded,
  759. &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
  760. i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
  761. pf->stat_offsets_loaded,
  762. &osd->eth.rx_discards,
  763. &nsd->eth.rx_discards);
  764. i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
  765. I40E_GLPRT_UPRCL(hw->port),
  766. pf->stat_offsets_loaded,
  767. &osd->eth.rx_unicast,
  768. &nsd->eth.rx_unicast);
  769. i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
  770. I40E_GLPRT_MPRCL(hw->port),
  771. pf->stat_offsets_loaded,
  772. &osd->eth.rx_multicast,
  773. &nsd->eth.rx_multicast);
  774. i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
  775. I40E_GLPRT_BPRCL(hw->port),
  776. pf->stat_offsets_loaded,
  777. &osd->eth.rx_broadcast,
  778. &nsd->eth.rx_broadcast);
  779. i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
  780. I40E_GLPRT_UPTCL(hw->port),
  781. pf->stat_offsets_loaded,
  782. &osd->eth.tx_unicast,
  783. &nsd->eth.tx_unicast);
  784. i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
  785. I40E_GLPRT_MPTCL(hw->port),
  786. pf->stat_offsets_loaded,
  787. &osd->eth.tx_multicast,
  788. &nsd->eth.tx_multicast);
  789. i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
  790. I40E_GLPRT_BPTCL(hw->port),
  791. pf->stat_offsets_loaded,
  792. &osd->eth.tx_broadcast,
  793. &nsd->eth.tx_broadcast);
  794. i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
  795. pf->stat_offsets_loaded,
  796. &osd->tx_dropped_link_down,
  797. &nsd->tx_dropped_link_down);
  798. i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
  799. pf->stat_offsets_loaded,
  800. &osd->crc_errors, &nsd->crc_errors);
  801. i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
  802. pf->stat_offsets_loaded,
  803. &osd->illegal_bytes, &nsd->illegal_bytes);
  804. i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
  805. pf->stat_offsets_loaded,
  806. &osd->mac_local_faults,
  807. &nsd->mac_local_faults);
  808. i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
  809. pf->stat_offsets_loaded,
  810. &osd->mac_remote_faults,
  811. &nsd->mac_remote_faults);
  812. i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
  813. pf->stat_offsets_loaded,
  814. &osd->rx_length_errors,
  815. &nsd->rx_length_errors);
  816. i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
  817. pf->stat_offsets_loaded,
  818. &osd->link_xon_rx, &nsd->link_xon_rx);
  819. i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
  820. pf->stat_offsets_loaded,
  821. &osd->link_xon_tx, &nsd->link_xon_tx);
  822. i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
  823. pf->stat_offsets_loaded,
  824. &osd->link_xoff_rx, &nsd->link_xoff_rx);
  825. i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
  826. pf->stat_offsets_loaded,
  827. &osd->link_xoff_tx, &nsd->link_xoff_tx);
  828. for (i = 0; i < 8; i++) {
  829. i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
  830. pf->stat_offsets_loaded,
  831. &osd->priority_xoff_rx[i],
  832. &nsd->priority_xoff_rx[i]);
  833. i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
  834. pf->stat_offsets_loaded,
  835. &osd->priority_xon_rx[i],
  836. &nsd->priority_xon_rx[i]);
  837. i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
  838. pf->stat_offsets_loaded,
  839. &osd->priority_xon_tx[i],
  840. &nsd->priority_xon_tx[i]);
  841. i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
  842. pf->stat_offsets_loaded,
  843. &osd->priority_xoff_tx[i],
  844. &nsd->priority_xoff_tx[i]);
  845. i40e_stat_update32(hw,
  846. I40E_GLPRT_RXON2OFFCNT(hw->port, i),
  847. pf->stat_offsets_loaded,
  848. &osd->priority_xon_2_xoff[i],
  849. &nsd->priority_xon_2_xoff[i]);
  850. }
  851. i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
  852. I40E_GLPRT_PRC64L(hw->port),
  853. pf->stat_offsets_loaded,
  854. &osd->rx_size_64, &nsd->rx_size_64);
  855. i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
  856. I40E_GLPRT_PRC127L(hw->port),
  857. pf->stat_offsets_loaded,
  858. &osd->rx_size_127, &nsd->rx_size_127);
  859. i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
  860. I40E_GLPRT_PRC255L(hw->port),
  861. pf->stat_offsets_loaded,
  862. &osd->rx_size_255, &nsd->rx_size_255);
  863. i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
  864. I40E_GLPRT_PRC511L(hw->port),
  865. pf->stat_offsets_loaded,
  866. &osd->rx_size_511, &nsd->rx_size_511);
  867. i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
  868. I40E_GLPRT_PRC1023L(hw->port),
  869. pf->stat_offsets_loaded,
  870. &osd->rx_size_1023, &nsd->rx_size_1023);
  871. i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
  872. I40E_GLPRT_PRC1522L(hw->port),
  873. pf->stat_offsets_loaded,
  874. &osd->rx_size_1522, &nsd->rx_size_1522);
  875. i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
  876. I40E_GLPRT_PRC9522L(hw->port),
  877. pf->stat_offsets_loaded,
  878. &osd->rx_size_big, &nsd->rx_size_big);
  879. i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
  880. I40E_GLPRT_PTC64L(hw->port),
  881. pf->stat_offsets_loaded,
  882. &osd->tx_size_64, &nsd->tx_size_64);
  883. i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
  884. I40E_GLPRT_PTC127L(hw->port),
  885. pf->stat_offsets_loaded,
  886. &osd->tx_size_127, &nsd->tx_size_127);
  887. i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
  888. I40E_GLPRT_PTC255L(hw->port),
  889. pf->stat_offsets_loaded,
  890. &osd->tx_size_255, &nsd->tx_size_255);
  891. i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
  892. I40E_GLPRT_PTC511L(hw->port),
  893. pf->stat_offsets_loaded,
  894. &osd->tx_size_511, &nsd->tx_size_511);
  895. i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
  896. I40E_GLPRT_PTC1023L(hw->port),
  897. pf->stat_offsets_loaded,
  898. &osd->tx_size_1023, &nsd->tx_size_1023);
  899. i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
  900. I40E_GLPRT_PTC1522L(hw->port),
  901. pf->stat_offsets_loaded,
  902. &osd->tx_size_1522, &nsd->tx_size_1522);
  903. i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
  904. I40E_GLPRT_PTC9522L(hw->port),
  905. pf->stat_offsets_loaded,
  906. &osd->tx_size_big, &nsd->tx_size_big);
  907. i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
  908. pf->stat_offsets_loaded,
  909. &osd->rx_undersize, &nsd->rx_undersize);
  910. i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
  911. pf->stat_offsets_loaded,
  912. &osd->rx_fragments, &nsd->rx_fragments);
  913. i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
  914. pf->stat_offsets_loaded,
  915. &osd->rx_oversize, &nsd->rx_oversize);
  916. i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
  917. pf->stat_offsets_loaded,
  918. &osd->rx_jabber, &nsd->rx_jabber);
  919. /* FDIR stats */
  920. i40e_stat_update32(hw,
  921. I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(pf->hw.pf_id)),
  922. pf->stat_offsets_loaded,
  923. &osd->fd_atr_match, &nsd->fd_atr_match);
  924. i40e_stat_update32(hw,
  925. I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(pf->hw.pf_id)),
  926. pf->stat_offsets_loaded,
  927. &osd->fd_sb_match, &nsd->fd_sb_match);
  928. i40e_stat_update32(hw,
  929. I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(pf->hw.pf_id)),
  930. pf->stat_offsets_loaded,
  931. &osd->fd_atr_tunnel_match, &nsd->fd_atr_tunnel_match);
  932. val = rd32(hw, I40E_PRTPM_EEE_STAT);
  933. nsd->tx_lpi_status =
  934. (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
  935. I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
  936. nsd->rx_lpi_status =
  937. (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
  938. I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
  939. i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
  940. pf->stat_offsets_loaded,
  941. &osd->tx_lpi_count, &nsd->tx_lpi_count);
  942. i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
  943. pf->stat_offsets_loaded,
  944. &osd->rx_lpi_count, &nsd->rx_lpi_count);
  945. if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
  946. !(pf->hw_disabled_flags & I40E_FLAG_FD_SB_ENABLED))
  947. nsd->fd_sb_status = true;
  948. else
  949. nsd->fd_sb_status = false;
  950. if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
  951. !(pf->hw_disabled_flags & I40E_FLAG_FD_ATR_ENABLED))
  952. nsd->fd_atr_status = true;
  953. else
  954. nsd->fd_atr_status = false;
  955. pf->stat_offsets_loaded = true;
  956. }
  957. /**
  958. * i40e_update_stats - Update the various statistics counters.
  959. * @vsi: the VSI to be updated
  960. *
  961. * Update the various stats for this VSI and its related entities.
  962. **/
  963. void i40e_update_stats(struct i40e_vsi *vsi)
  964. {
  965. struct i40e_pf *pf = vsi->back;
  966. if (vsi == pf->vsi[pf->lan_vsi])
  967. i40e_update_pf_stats(pf);
  968. i40e_update_vsi_stats(vsi);
  969. }
  970. /**
  971. * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
  972. * @vsi: the VSI to be searched
  973. * @macaddr: the MAC address
  974. * @vlan: the vlan
  975. *
  976. * Returns ptr to the filter object or NULL
  977. **/
  978. static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
  979. const u8 *macaddr, s16 vlan)
  980. {
  981. struct i40e_mac_filter *f;
  982. u64 key;
  983. if (!vsi || !macaddr)
  984. return NULL;
  985. key = i40e_addr_to_hkey(macaddr);
  986. hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
  987. if ((ether_addr_equal(macaddr, f->macaddr)) &&
  988. (vlan == f->vlan))
  989. return f;
  990. }
  991. return NULL;
  992. }
  993. /**
  994. * i40e_find_mac - Find a mac addr in the macvlan filters list
  995. * @vsi: the VSI to be searched
  996. * @macaddr: the MAC address we are searching for
  997. *
  998. * Returns the first filter with the provided MAC address or NULL if
  999. * MAC address was not found
  1000. **/
  1001. struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
  1002. {
  1003. struct i40e_mac_filter *f;
  1004. u64 key;
  1005. if (!vsi || !macaddr)
  1006. return NULL;
  1007. key = i40e_addr_to_hkey(macaddr);
  1008. hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
  1009. if ((ether_addr_equal(macaddr, f->macaddr)))
  1010. return f;
  1011. }
  1012. return NULL;
  1013. }
  1014. /**
  1015. * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
  1016. * @vsi: the VSI to be searched
  1017. *
  1018. * Returns true if VSI is in vlan mode or false otherwise
  1019. **/
  1020. bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
  1021. {
  1022. /* If we have a PVID, always operate in VLAN mode */
  1023. if (vsi->info.pvid)
  1024. return true;
  1025. /* We need to operate in VLAN mode whenever we have any filters with
  1026. * a VLAN other than I40E_VLAN_ALL. We could check the table each
  1027. * time, incurring search cost repeatedly. However, we can notice two
  1028. * things:
  1029. *
  1030. * 1) the only place where we can gain a VLAN filter is in
  1031. * i40e_add_filter.
  1032. *
  1033. * 2) the only place where filters are actually removed is in
  1034. * i40e_sync_filters_subtask.
  1035. *
  1036. * Thus, we can simply use a boolean value, has_vlan_filters which we
  1037. * will set to true when we add a VLAN filter in i40e_add_filter. Then
  1038. * we have to perform the full search after deleting filters in
  1039. * i40e_sync_filters_subtask, but we already have to search
  1040. * filters here and can perform the check at the same time. This
  1041. * results in avoiding embedding a loop for VLAN mode inside another
  1042. * loop over all the filters, and should maintain correctness as noted
  1043. * above.
  1044. */
  1045. return vsi->has_vlan_filter;
  1046. }
  1047. /**
  1048. * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
  1049. * @vsi: the VSI to configure
  1050. * @tmp_add_list: list of filters ready to be added
  1051. * @tmp_del_list: list of filters ready to be deleted
  1052. * @vlan_filters: the number of active VLAN filters
  1053. *
  1054. * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
  1055. * behave as expected. If we have any active VLAN filters remaining or about
  1056. * to be added then we need to update non-VLAN filters to be marked as VLAN=0
  1057. * so that they only match against untagged traffic. If we no longer have any
  1058. * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
  1059. * so that they match against both tagged and untagged traffic. In this way,
  1060. * we ensure that we correctly receive the desired traffic. This ensures that
  1061. * when we have an active VLAN we will receive only untagged traffic and
  1062. * traffic matching active VLANs. If we have no active VLANs then we will
  1063. * operate in non-VLAN mode and receive all traffic, tagged or untagged.
  1064. *
  1065. * Finally, in a similar fashion, this function also corrects filters when
  1066. * there is an active PVID assigned to this VSI.
  1067. *
  1068. * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
  1069. *
  1070. * This function is only expected to be called from within
  1071. * i40e_sync_vsi_filters.
  1072. *
  1073. * NOTE: This function expects to be called while under the
  1074. * mac_filter_hash_lock
  1075. */
  1076. static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
  1077. struct hlist_head *tmp_add_list,
  1078. struct hlist_head *tmp_del_list,
  1079. int vlan_filters)
  1080. {
  1081. s16 pvid = le16_to_cpu(vsi->info.pvid);
  1082. struct i40e_mac_filter *f, *add_head;
  1083. struct i40e_new_mac_filter *new;
  1084. struct hlist_node *h;
  1085. int bkt, new_vlan;
  1086. /* To determine if a particular filter needs to be replaced we
  1087. * have the three following conditions:
  1088. *
  1089. * a) if we have a PVID assigned, then all filters which are
  1090. * not marked as VLAN=PVID must be replaced with filters that
  1091. * are.
  1092. * b) otherwise, if we have any active VLANS, all filters
  1093. * which are marked as VLAN=-1 must be replaced with
  1094. * filters marked as VLAN=0
  1095. * c) finally, if we do not have any active VLANS, all filters
  1096. * which are marked as VLAN=0 must be replaced with filters
  1097. * marked as VLAN=-1
  1098. */
  1099. /* Update the filters about to be added in place */
  1100. hlist_for_each_entry(new, tmp_add_list, hlist) {
  1101. if (pvid && new->f->vlan != pvid)
  1102. new->f->vlan = pvid;
  1103. else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
  1104. new->f->vlan = 0;
  1105. else if (!vlan_filters && new->f->vlan == 0)
  1106. new->f->vlan = I40E_VLAN_ANY;
  1107. }
  1108. /* Update the remaining active filters */
  1109. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  1110. /* Combine the checks for whether a filter needs to be changed
  1111. * and then determine the new VLAN inside the if block, in
  1112. * order to avoid duplicating code for adding the new filter
  1113. * then deleting the old filter.
  1114. */
  1115. if ((pvid && f->vlan != pvid) ||
  1116. (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
  1117. (!vlan_filters && f->vlan == 0)) {
  1118. /* Determine the new vlan we will be adding */
  1119. if (pvid)
  1120. new_vlan = pvid;
  1121. else if (vlan_filters)
  1122. new_vlan = 0;
  1123. else
  1124. new_vlan = I40E_VLAN_ANY;
  1125. /* Create the new filter */
  1126. add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
  1127. if (!add_head)
  1128. return -ENOMEM;
  1129. /* Create a temporary i40e_new_mac_filter */
  1130. new = kzalloc(sizeof(*new), GFP_ATOMIC);
  1131. if (!new)
  1132. return -ENOMEM;
  1133. new->f = add_head;
  1134. new->state = add_head->state;
  1135. /* Add the new filter to the tmp list */
  1136. hlist_add_head(&new->hlist, tmp_add_list);
  1137. /* Put the original filter into the delete list */
  1138. f->state = I40E_FILTER_REMOVE;
  1139. hash_del(&f->hlist);
  1140. hlist_add_head(&f->hlist, tmp_del_list);
  1141. }
  1142. }
  1143. vsi->has_vlan_filter = !!vlan_filters;
  1144. return 0;
  1145. }
  1146. /**
  1147. * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
  1148. * @vsi: the PF Main VSI - inappropriate for any other VSI
  1149. * @macaddr: the MAC address
  1150. *
  1151. * Remove whatever filter the firmware set up so the driver can manage
  1152. * its own filtering intelligently.
  1153. **/
  1154. static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
  1155. {
  1156. struct i40e_aqc_remove_macvlan_element_data element;
  1157. struct i40e_pf *pf = vsi->back;
  1158. /* Only appropriate for the PF main VSI */
  1159. if (vsi->type != I40E_VSI_MAIN)
  1160. return;
  1161. memset(&element, 0, sizeof(element));
  1162. ether_addr_copy(element.mac_addr, macaddr);
  1163. element.vlan_tag = 0;
  1164. /* Ignore error returns, some firmware does it this way... */
  1165. element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
  1166. i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
  1167. memset(&element, 0, sizeof(element));
  1168. ether_addr_copy(element.mac_addr, macaddr);
  1169. element.vlan_tag = 0;
  1170. /* ...and some firmware does it this way. */
  1171. element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
  1172. I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
  1173. i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
  1174. }
  1175. /**
  1176. * i40e_add_filter - Add a mac/vlan filter to the VSI
  1177. * @vsi: the VSI to be searched
  1178. * @macaddr: the MAC address
  1179. * @vlan: the vlan
  1180. *
  1181. * Returns ptr to the filter object or NULL when no memory available.
  1182. *
  1183. * NOTE: This function is expected to be called with mac_filter_hash_lock
  1184. * being held.
  1185. **/
  1186. struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
  1187. const u8 *macaddr, s16 vlan)
  1188. {
  1189. struct i40e_mac_filter *f;
  1190. u64 key;
  1191. if (!vsi || !macaddr)
  1192. return NULL;
  1193. f = i40e_find_filter(vsi, macaddr, vlan);
  1194. if (!f) {
  1195. f = kzalloc(sizeof(*f), GFP_ATOMIC);
  1196. if (!f)
  1197. return NULL;
  1198. /* Update the boolean indicating if we need to function in
  1199. * VLAN mode.
  1200. */
  1201. if (vlan >= 0)
  1202. vsi->has_vlan_filter = true;
  1203. ether_addr_copy(f->macaddr, macaddr);
  1204. f->vlan = vlan;
  1205. /* If we're in overflow promisc mode, set the state directly
  1206. * to failed, so we don't bother to try sending the filter
  1207. * to the hardware.
  1208. */
  1209. if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state))
  1210. f->state = I40E_FILTER_FAILED;
  1211. else
  1212. f->state = I40E_FILTER_NEW;
  1213. INIT_HLIST_NODE(&f->hlist);
  1214. key = i40e_addr_to_hkey(macaddr);
  1215. hash_add(vsi->mac_filter_hash, &f->hlist, key);
  1216. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  1217. vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
  1218. }
  1219. /* If we're asked to add a filter that has been marked for removal, it
  1220. * is safe to simply restore it to active state. __i40e_del_filter
  1221. * will have simply deleted any filters which were previously marked
  1222. * NEW or FAILED, so if it is currently marked REMOVE it must have
  1223. * previously been ACTIVE. Since we haven't yet run the sync filters
  1224. * task, just restore this filter to the ACTIVE state so that the
  1225. * sync task leaves it in place
  1226. */
  1227. if (f->state == I40E_FILTER_REMOVE)
  1228. f->state = I40E_FILTER_ACTIVE;
  1229. return f;
  1230. }
  1231. /**
  1232. * __i40e_del_filter - Remove a specific filter from the VSI
  1233. * @vsi: VSI to remove from
  1234. * @f: the filter to remove from the list
  1235. *
  1236. * This function should be called instead of i40e_del_filter only if you know
  1237. * the exact filter you will remove already, such as via i40e_find_filter or
  1238. * i40e_find_mac.
  1239. *
  1240. * NOTE: This function is expected to be called with mac_filter_hash_lock
  1241. * being held.
  1242. * ANOTHER NOTE: This function MUST be called from within the context of
  1243. * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
  1244. * instead of list_for_each_entry().
  1245. **/
  1246. void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
  1247. {
  1248. if (!f)
  1249. return;
  1250. /* If the filter was never added to firmware then we can just delete it
  1251. * directly and we don't want to set the status to remove or else an
  1252. * admin queue command will unnecessarily fire.
  1253. */
  1254. if ((f->state == I40E_FILTER_FAILED) ||
  1255. (f->state == I40E_FILTER_NEW)) {
  1256. hash_del(&f->hlist);
  1257. kfree(f);
  1258. } else {
  1259. f->state = I40E_FILTER_REMOVE;
  1260. }
  1261. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  1262. vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
  1263. }
  1264. /**
  1265. * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
  1266. * @vsi: the VSI to be searched
  1267. * @macaddr: the MAC address
  1268. * @vlan: the VLAN
  1269. *
  1270. * NOTE: This function is expected to be called with mac_filter_hash_lock
  1271. * being held.
  1272. * ANOTHER NOTE: This function MUST be called from within the context of
  1273. * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
  1274. * instead of list_for_each_entry().
  1275. **/
  1276. void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
  1277. {
  1278. struct i40e_mac_filter *f;
  1279. if (!vsi || !macaddr)
  1280. return;
  1281. f = i40e_find_filter(vsi, macaddr, vlan);
  1282. __i40e_del_filter(vsi, f);
  1283. }
  1284. /**
  1285. * i40e_add_mac_filter - Add a MAC filter for all active VLANs
  1286. * @vsi: the VSI to be searched
  1287. * @macaddr: the mac address to be filtered
  1288. *
  1289. * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
  1290. * go through all the macvlan filters and add a macvlan filter for each
  1291. * unique vlan that already exists. If a PVID has been assigned, instead only
  1292. * add the macaddr to that VLAN.
  1293. *
  1294. * Returns last filter added on success, else NULL
  1295. **/
  1296. struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
  1297. const u8 *macaddr)
  1298. {
  1299. struct i40e_mac_filter *f, *add = NULL;
  1300. struct hlist_node *h;
  1301. int bkt;
  1302. if (vsi->info.pvid)
  1303. return i40e_add_filter(vsi, macaddr,
  1304. le16_to_cpu(vsi->info.pvid));
  1305. if (!i40e_is_vsi_in_vlan(vsi))
  1306. return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
  1307. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  1308. if (f->state == I40E_FILTER_REMOVE)
  1309. continue;
  1310. add = i40e_add_filter(vsi, macaddr, f->vlan);
  1311. if (!add)
  1312. return NULL;
  1313. }
  1314. return add;
  1315. }
  1316. /**
  1317. * i40e_del_mac_filter - Remove a MAC filter from all VLANs
  1318. * @vsi: the VSI to be searched
  1319. * @macaddr: the mac address to be removed
  1320. *
  1321. * Removes a given MAC address from a VSI regardless of what VLAN it has been
  1322. * associated with.
  1323. *
  1324. * Returns 0 for success, or error
  1325. **/
  1326. int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
  1327. {
  1328. struct i40e_mac_filter *f;
  1329. struct hlist_node *h;
  1330. bool found = false;
  1331. int bkt;
  1332. WARN(!spin_is_locked(&vsi->mac_filter_hash_lock),
  1333. "Missing mac_filter_hash_lock\n");
  1334. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  1335. if (ether_addr_equal(macaddr, f->macaddr)) {
  1336. __i40e_del_filter(vsi, f);
  1337. found = true;
  1338. }
  1339. }
  1340. if (found)
  1341. return 0;
  1342. else
  1343. return -ENOENT;
  1344. }
  1345. /**
  1346. * i40e_set_mac - NDO callback to set mac address
  1347. * @netdev: network interface device structure
  1348. * @p: pointer to an address structure
  1349. *
  1350. * Returns 0 on success, negative on failure
  1351. **/
  1352. static int i40e_set_mac(struct net_device *netdev, void *p)
  1353. {
  1354. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1355. struct i40e_vsi *vsi = np->vsi;
  1356. struct i40e_pf *pf = vsi->back;
  1357. struct i40e_hw *hw = &pf->hw;
  1358. struct sockaddr *addr = p;
  1359. if (!is_valid_ether_addr(addr->sa_data))
  1360. return -EADDRNOTAVAIL;
  1361. if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
  1362. netdev_info(netdev, "already using mac address %pM\n",
  1363. addr->sa_data);
  1364. return 0;
  1365. }
  1366. if (test_bit(__I40E_DOWN, &vsi->back->state) ||
  1367. test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
  1368. return -EADDRNOTAVAIL;
  1369. if (ether_addr_equal(hw->mac.addr, addr->sa_data))
  1370. netdev_info(netdev, "returning to hw mac address %pM\n",
  1371. hw->mac.addr);
  1372. else
  1373. netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
  1374. spin_lock_bh(&vsi->mac_filter_hash_lock);
  1375. i40e_del_mac_filter(vsi, netdev->dev_addr);
  1376. i40e_add_mac_filter(vsi, addr->sa_data);
  1377. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  1378. ether_addr_copy(netdev->dev_addr, addr->sa_data);
  1379. if (vsi->type == I40E_VSI_MAIN) {
  1380. i40e_status ret;
  1381. ret = i40e_aq_mac_address_write(&vsi->back->hw,
  1382. I40E_AQC_WRITE_TYPE_LAA_WOL,
  1383. addr->sa_data, NULL);
  1384. if (ret)
  1385. netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
  1386. i40e_stat_str(hw, ret),
  1387. i40e_aq_str(hw, hw->aq.asq_last_status));
  1388. }
  1389. /* schedule our worker thread which will take care of
  1390. * applying the new filter changes
  1391. */
  1392. i40e_service_event_schedule(vsi->back);
  1393. return 0;
  1394. }
  1395. /**
  1396. * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
  1397. * @vsi: the VSI being setup
  1398. * @ctxt: VSI context structure
  1399. * @enabled_tc: Enabled TCs bitmap
  1400. * @is_add: True if called before Add VSI
  1401. *
  1402. * Setup VSI queue mapping for enabled traffic classes.
  1403. **/
  1404. static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
  1405. struct i40e_vsi_context *ctxt,
  1406. u8 enabled_tc,
  1407. bool is_add)
  1408. {
  1409. struct i40e_pf *pf = vsi->back;
  1410. u16 sections = 0;
  1411. u8 netdev_tc = 0;
  1412. u16 numtc = 0;
  1413. u16 qcount;
  1414. u8 offset;
  1415. u16 qmap;
  1416. int i;
  1417. u16 num_tc_qps = 0;
  1418. sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
  1419. offset = 0;
  1420. if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
  1421. /* Find numtc from enabled TC bitmap */
  1422. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  1423. if (enabled_tc & BIT(i)) /* TC is enabled */
  1424. numtc++;
  1425. }
  1426. if (!numtc) {
  1427. dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
  1428. numtc = 1;
  1429. }
  1430. } else {
  1431. /* At least TC0 is enabled in case of non-DCB case */
  1432. numtc = 1;
  1433. }
  1434. vsi->tc_config.numtc = numtc;
  1435. vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
  1436. /* Number of queues per enabled TC */
  1437. qcount = vsi->alloc_queue_pairs;
  1438. num_tc_qps = qcount / numtc;
  1439. num_tc_qps = min_t(int, num_tc_qps, i40e_pf_get_max_q_per_tc(pf));
  1440. /* Setup queue offset/count for all TCs for given VSI */
  1441. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  1442. /* See if the given TC is enabled for the given VSI */
  1443. if (vsi->tc_config.enabled_tc & BIT(i)) {
  1444. /* TC is enabled */
  1445. int pow, num_qps;
  1446. switch (vsi->type) {
  1447. case I40E_VSI_MAIN:
  1448. qcount = min_t(int, pf->alloc_rss_size,
  1449. num_tc_qps);
  1450. break;
  1451. case I40E_VSI_FDIR:
  1452. case I40E_VSI_SRIOV:
  1453. case I40E_VSI_VMDQ2:
  1454. default:
  1455. qcount = num_tc_qps;
  1456. WARN_ON(i != 0);
  1457. break;
  1458. }
  1459. vsi->tc_config.tc_info[i].qoffset = offset;
  1460. vsi->tc_config.tc_info[i].qcount = qcount;
  1461. /* find the next higher power-of-2 of num queue pairs */
  1462. num_qps = qcount;
  1463. pow = 0;
  1464. while (num_qps && (BIT_ULL(pow) < qcount)) {
  1465. pow++;
  1466. num_qps >>= 1;
  1467. }
  1468. vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
  1469. qmap =
  1470. (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
  1471. (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
  1472. offset += qcount;
  1473. } else {
  1474. /* TC is not enabled so set the offset to
  1475. * default queue and allocate one queue
  1476. * for the given TC.
  1477. */
  1478. vsi->tc_config.tc_info[i].qoffset = 0;
  1479. vsi->tc_config.tc_info[i].qcount = 1;
  1480. vsi->tc_config.tc_info[i].netdev_tc = 0;
  1481. qmap = 0;
  1482. }
  1483. ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
  1484. }
  1485. /* Set actual Tx/Rx queue pairs */
  1486. vsi->num_queue_pairs = offset;
  1487. if ((vsi->type == I40E_VSI_MAIN) && (numtc == 1)) {
  1488. if (vsi->req_queue_pairs > 0)
  1489. vsi->num_queue_pairs = vsi->req_queue_pairs;
  1490. else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  1491. vsi->num_queue_pairs = pf->num_lan_msix;
  1492. }
  1493. /* Scheduler section valid can only be set for ADD VSI */
  1494. if (is_add) {
  1495. sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
  1496. ctxt->info.up_enable_bits = enabled_tc;
  1497. }
  1498. if (vsi->type == I40E_VSI_SRIOV) {
  1499. ctxt->info.mapping_flags |=
  1500. cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
  1501. for (i = 0; i < vsi->num_queue_pairs; i++)
  1502. ctxt->info.queue_mapping[i] =
  1503. cpu_to_le16(vsi->base_queue + i);
  1504. } else {
  1505. ctxt->info.mapping_flags |=
  1506. cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
  1507. ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
  1508. }
  1509. ctxt->info.valid_sections |= cpu_to_le16(sections);
  1510. }
  1511. /**
  1512. * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
  1513. * @netdev: the netdevice
  1514. * @addr: address to add
  1515. *
  1516. * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
  1517. * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
  1518. */
  1519. static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
  1520. {
  1521. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1522. struct i40e_vsi *vsi = np->vsi;
  1523. if (i40e_add_mac_filter(vsi, addr))
  1524. return 0;
  1525. else
  1526. return -ENOMEM;
  1527. }
  1528. /**
  1529. * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
  1530. * @netdev: the netdevice
  1531. * @addr: address to add
  1532. *
  1533. * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
  1534. * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
  1535. */
  1536. static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
  1537. {
  1538. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1539. struct i40e_vsi *vsi = np->vsi;
  1540. i40e_del_mac_filter(vsi, addr);
  1541. return 0;
  1542. }
  1543. /**
  1544. * i40e_set_rx_mode - NDO callback to set the netdev filters
  1545. * @netdev: network interface device structure
  1546. **/
  1547. static void i40e_set_rx_mode(struct net_device *netdev)
  1548. {
  1549. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1550. struct i40e_vsi *vsi = np->vsi;
  1551. spin_lock_bh(&vsi->mac_filter_hash_lock);
  1552. __dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
  1553. __dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
  1554. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  1555. /* check for other flag changes */
  1556. if (vsi->current_netdev_flags != vsi->netdev->flags) {
  1557. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  1558. vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
  1559. }
  1560. /* schedule our worker thread which will take care of
  1561. * applying the new filter changes
  1562. */
  1563. i40e_service_event_schedule(vsi->back);
  1564. }
  1565. /**
  1566. * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
  1567. * @vsi: Pointer to VSI struct
  1568. * @from: Pointer to list which contains MAC filter entries - changes to
  1569. * those entries needs to be undone.
  1570. *
  1571. * MAC filter entries from this list were slated for deletion.
  1572. **/
  1573. static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
  1574. struct hlist_head *from)
  1575. {
  1576. struct i40e_mac_filter *f;
  1577. struct hlist_node *h;
  1578. hlist_for_each_entry_safe(f, h, from, hlist) {
  1579. u64 key = i40e_addr_to_hkey(f->macaddr);
  1580. /* Move the element back into MAC filter list*/
  1581. hlist_del(&f->hlist);
  1582. hash_add(vsi->mac_filter_hash, &f->hlist, key);
  1583. }
  1584. }
  1585. /**
  1586. * i40e_undo_add_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 addition.
  1592. **/
  1593. static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
  1594. struct hlist_head *from)
  1595. {
  1596. struct i40e_new_mac_filter *new;
  1597. struct hlist_node *h;
  1598. hlist_for_each_entry_safe(new, h, from, hlist) {
  1599. /* We can simply free the wrapper structure */
  1600. hlist_del(&new->hlist);
  1601. kfree(new);
  1602. }
  1603. }
  1604. /**
  1605. * i40e_next_entry - Get the next non-broadcast filter from a list
  1606. * @next: pointer to filter in list
  1607. *
  1608. * Returns the next non-broadcast filter in the list. Required so that we
  1609. * ignore broadcast filters within the list, since these are not handled via
  1610. * the normal firmware update path.
  1611. */
  1612. static
  1613. struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
  1614. {
  1615. hlist_for_each_entry_continue(next, hlist) {
  1616. if (!is_broadcast_ether_addr(next->f->macaddr))
  1617. return next;
  1618. }
  1619. return NULL;
  1620. }
  1621. /**
  1622. * i40e_update_filter_state - Update filter state based on return data
  1623. * from firmware
  1624. * @count: Number of filters added
  1625. * @add_list: return data from fw
  1626. * @head: pointer to first filter in current batch
  1627. *
  1628. * MAC filter entries from list were slated to be added to device. Returns
  1629. * number of successful filters. Note that 0 does NOT mean success!
  1630. **/
  1631. static int
  1632. i40e_update_filter_state(int count,
  1633. struct i40e_aqc_add_macvlan_element_data *add_list,
  1634. struct i40e_new_mac_filter *add_head)
  1635. {
  1636. int retval = 0;
  1637. int i;
  1638. for (i = 0; i < count; i++) {
  1639. /* Always check status of each filter. We don't need to check
  1640. * the firmware return status because we pre-set the filter
  1641. * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
  1642. * request to the adminq. Thus, if it no longer matches then
  1643. * we know the filter is active.
  1644. */
  1645. if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
  1646. add_head->state = I40E_FILTER_FAILED;
  1647. } else {
  1648. add_head->state = I40E_FILTER_ACTIVE;
  1649. retval++;
  1650. }
  1651. add_head = i40e_next_filter(add_head);
  1652. if (!add_head)
  1653. break;
  1654. }
  1655. return retval;
  1656. }
  1657. /**
  1658. * i40e_aqc_del_filters - Request firmware to delete a set of filters
  1659. * @vsi: ptr to the VSI
  1660. * @vsi_name: name to display in messages
  1661. * @list: the list of filters to send to firmware
  1662. * @num_del: the number of filters to delete
  1663. * @retval: Set to -EIO on failure to delete
  1664. *
  1665. * Send a request to firmware via AdminQ to delete a set of filters. Uses
  1666. * *retval instead of a return value so that success does not force ret_val to
  1667. * be set to 0. This ensures that a sequence of calls to this function
  1668. * preserve the previous value of *retval on successful delete.
  1669. */
  1670. static
  1671. void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
  1672. struct i40e_aqc_remove_macvlan_element_data *list,
  1673. int num_del, int *retval)
  1674. {
  1675. struct i40e_hw *hw = &vsi->back->hw;
  1676. i40e_status aq_ret;
  1677. int aq_err;
  1678. aq_ret = i40e_aq_remove_macvlan(hw, vsi->seid, list, num_del, NULL);
  1679. aq_err = hw->aq.asq_last_status;
  1680. /* Explicitly ignore and do not report when firmware returns ENOENT */
  1681. if (aq_ret && !(aq_err == I40E_AQ_RC_ENOENT)) {
  1682. *retval = -EIO;
  1683. dev_info(&vsi->back->pdev->dev,
  1684. "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
  1685. vsi_name, i40e_stat_str(hw, aq_ret),
  1686. i40e_aq_str(hw, aq_err));
  1687. }
  1688. }
  1689. /**
  1690. * i40e_aqc_add_filters - Request firmware to add a set of filters
  1691. * @vsi: ptr to the VSI
  1692. * @vsi_name: name to display in messages
  1693. * @list: the list of filters to send to firmware
  1694. * @add_head: Position in the add hlist
  1695. * @num_add: the number of filters to add
  1696. * @promisc_change: set to true on exit if promiscuous mode was forced on
  1697. *
  1698. * Send a request to firmware via AdminQ to add a chunk of filters. Will set
  1699. * promisc_changed to true if the firmware has run out of space for more
  1700. * filters.
  1701. */
  1702. static
  1703. void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
  1704. struct i40e_aqc_add_macvlan_element_data *list,
  1705. struct i40e_new_mac_filter *add_head,
  1706. int num_add, bool *promisc_changed)
  1707. {
  1708. struct i40e_hw *hw = &vsi->back->hw;
  1709. int aq_err, fcnt;
  1710. i40e_aq_add_macvlan(hw, vsi->seid, list, num_add, NULL);
  1711. aq_err = hw->aq.asq_last_status;
  1712. fcnt = i40e_update_filter_state(num_add, list, add_head);
  1713. if (fcnt != num_add) {
  1714. *promisc_changed = true;
  1715. set_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
  1716. dev_warn(&vsi->back->pdev->dev,
  1717. "Error %s adding RX filters on %s, promiscuous mode forced on\n",
  1718. i40e_aq_str(hw, aq_err),
  1719. vsi_name);
  1720. }
  1721. }
  1722. /**
  1723. * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
  1724. * @vsi: pointer to the VSI
  1725. * @f: filter data
  1726. *
  1727. * This function sets or clears the promiscuous broadcast flags for VLAN
  1728. * filters in order to properly receive broadcast frames. Assumes that only
  1729. * broadcast filters are passed.
  1730. *
  1731. * Returns status indicating success or failure;
  1732. **/
  1733. static i40e_status
  1734. i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
  1735. struct i40e_mac_filter *f)
  1736. {
  1737. bool enable = f->state == I40E_FILTER_NEW;
  1738. struct i40e_hw *hw = &vsi->back->hw;
  1739. i40e_status aq_ret;
  1740. if (f->vlan == I40E_VLAN_ANY) {
  1741. aq_ret = i40e_aq_set_vsi_broadcast(hw,
  1742. vsi->seid,
  1743. enable,
  1744. NULL);
  1745. } else {
  1746. aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
  1747. vsi->seid,
  1748. enable,
  1749. f->vlan,
  1750. NULL);
  1751. }
  1752. if (aq_ret)
  1753. dev_warn(&vsi->back->pdev->dev,
  1754. "Error %s setting broadcast promiscuous mode on %s\n",
  1755. i40e_aq_str(hw, hw->aq.asq_last_status),
  1756. vsi_name);
  1757. return aq_ret;
  1758. }
  1759. /**
  1760. * i40e_sync_vsi_filters - Update the VSI filter list to the HW
  1761. * @vsi: ptr to the VSI
  1762. *
  1763. * Push any outstanding VSI filter changes through the AdminQ.
  1764. *
  1765. * Returns 0 or error value
  1766. **/
  1767. int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
  1768. {
  1769. struct hlist_head tmp_add_list, tmp_del_list;
  1770. struct i40e_mac_filter *f;
  1771. struct i40e_new_mac_filter *new, *add_head = NULL;
  1772. struct i40e_hw *hw = &vsi->back->hw;
  1773. unsigned int failed_filters = 0;
  1774. unsigned int vlan_filters = 0;
  1775. bool promisc_changed = false;
  1776. char vsi_name[16] = "PF";
  1777. int filter_list_len = 0;
  1778. i40e_status aq_ret = 0;
  1779. u32 changed_flags = 0;
  1780. struct hlist_node *h;
  1781. struct i40e_pf *pf;
  1782. int num_add = 0;
  1783. int num_del = 0;
  1784. int retval = 0;
  1785. u16 cmd_flags;
  1786. int list_size;
  1787. int bkt;
  1788. /* empty array typed pointers, kcalloc later */
  1789. struct i40e_aqc_add_macvlan_element_data *add_list;
  1790. struct i40e_aqc_remove_macvlan_element_data *del_list;
  1791. while (test_and_set_bit(__I40E_CONFIG_BUSY, &vsi->state))
  1792. usleep_range(1000, 2000);
  1793. pf = vsi->back;
  1794. if (vsi->netdev) {
  1795. changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
  1796. vsi->current_netdev_flags = vsi->netdev->flags;
  1797. }
  1798. INIT_HLIST_HEAD(&tmp_add_list);
  1799. INIT_HLIST_HEAD(&tmp_del_list);
  1800. if (vsi->type == I40E_VSI_SRIOV)
  1801. snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
  1802. else if (vsi->type != I40E_VSI_MAIN)
  1803. snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
  1804. if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
  1805. vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
  1806. spin_lock_bh(&vsi->mac_filter_hash_lock);
  1807. /* Create a list of filters to delete. */
  1808. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  1809. if (f->state == I40E_FILTER_REMOVE) {
  1810. /* Move the element into temporary del_list */
  1811. hash_del(&f->hlist);
  1812. hlist_add_head(&f->hlist, &tmp_del_list);
  1813. /* Avoid counting removed filters */
  1814. continue;
  1815. }
  1816. if (f->state == I40E_FILTER_NEW) {
  1817. /* Create a temporary i40e_new_mac_filter */
  1818. new = kzalloc(sizeof(*new), GFP_ATOMIC);
  1819. if (!new)
  1820. goto err_no_memory_locked;
  1821. /* Store pointer to the real filter */
  1822. new->f = f;
  1823. new->state = f->state;
  1824. /* Add it to the hash list */
  1825. hlist_add_head(&new->hlist, &tmp_add_list);
  1826. }
  1827. /* Count the number of active (current and new) VLAN
  1828. * filters we have now. Does not count filters which
  1829. * are marked for deletion.
  1830. */
  1831. if (f->vlan > 0)
  1832. vlan_filters++;
  1833. }
  1834. retval = i40e_correct_mac_vlan_filters(vsi,
  1835. &tmp_add_list,
  1836. &tmp_del_list,
  1837. vlan_filters);
  1838. if (retval)
  1839. goto err_no_memory_locked;
  1840. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  1841. }
  1842. /* Now process 'del_list' outside the lock */
  1843. if (!hlist_empty(&tmp_del_list)) {
  1844. filter_list_len = hw->aq.asq_buf_size /
  1845. sizeof(struct i40e_aqc_remove_macvlan_element_data);
  1846. list_size = filter_list_len *
  1847. sizeof(struct i40e_aqc_remove_macvlan_element_data);
  1848. del_list = kzalloc(list_size, GFP_ATOMIC);
  1849. if (!del_list)
  1850. goto err_no_memory;
  1851. hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
  1852. cmd_flags = 0;
  1853. /* handle broadcast filters by updating the broadcast
  1854. * promiscuous flag and release filter list.
  1855. */
  1856. if (is_broadcast_ether_addr(f->macaddr)) {
  1857. i40e_aqc_broadcast_filter(vsi, vsi_name, f);
  1858. hlist_del(&f->hlist);
  1859. kfree(f);
  1860. continue;
  1861. }
  1862. /* add to delete list */
  1863. ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
  1864. if (f->vlan == I40E_VLAN_ANY) {
  1865. del_list[num_del].vlan_tag = 0;
  1866. cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
  1867. } else {
  1868. del_list[num_del].vlan_tag =
  1869. cpu_to_le16((u16)(f->vlan));
  1870. }
  1871. cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
  1872. del_list[num_del].flags = cmd_flags;
  1873. num_del++;
  1874. /* flush a full buffer */
  1875. if (num_del == filter_list_len) {
  1876. i40e_aqc_del_filters(vsi, vsi_name, del_list,
  1877. num_del, &retval);
  1878. memset(del_list, 0, list_size);
  1879. num_del = 0;
  1880. }
  1881. /* Release memory for MAC filter entries which were
  1882. * synced up with HW.
  1883. */
  1884. hlist_del(&f->hlist);
  1885. kfree(f);
  1886. }
  1887. if (num_del) {
  1888. i40e_aqc_del_filters(vsi, vsi_name, del_list,
  1889. num_del, &retval);
  1890. }
  1891. kfree(del_list);
  1892. del_list = NULL;
  1893. }
  1894. if (!hlist_empty(&tmp_add_list)) {
  1895. /* Do all the adds now. */
  1896. filter_list_len = hw->aq.asq_buf_size /
  1897. sizeof(struct i40e_aqc_add_macvlan_element_data);
  1898. list_size = filter_list_len *
  1899. sizeof(struct i40e_aqc_add_macvlan_element_data);
  1900. add_list = kzalloc(list_size, GFP_ATOMIC);
  1901. if (!add_list)
  1902. goto err_no_memory;
  1903. num_add = 0;
  1904. hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
  1905. if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
  1906. &vsi->state)) {
  1907. new->state = I40E_FILTER_FAILED;
  1908. continue;
  1909. }
  1910. /* handle broadcast filters by updating the broadcast
  1911. * promiscuous flag instead of adding a MAC filter.
  1912. */
  1913. if (is_broadcast_ether_addr(new->f->macaddr)) {
  1914. if (i40e_aqc_broadcast_filter(vsi, vsi_name,
  1915. new->f))
  1916. new->state = I40E_FILTER_FAILED;
  1917. else
  1918. new->state = I40E_FILTER_ACTIVE;
  1919. continue;
  1920. }
  1921. /* add to add array */
  1922. if (num_add == 0)
  1923. add_head = new;
  1924. cmd_flags = 0;
  1925. ether_addr_copy(add_list[num_add].mac_addr,
  1926. new->f->macaddr);
  1927. if (new->f->vlan == I40E_VLAN_ANY) {
  1928. add_list[num_add].vlan_tag = 0;
  1929. cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
  1930. } else {
  1931. add_list[num_add].vlan_tag =
  1932. cpu_to_le16((u16)(new->f->vlan));
  1933. }
  1934. add_list[num_add].queue_number = 0;
  1935. /* set invalid match method for later detection */
  1936. add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
  1937. cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
  1938. add_list[num_add].flags = cpu_to_le16(cmd_flags);
  1939. num_add++;
  1940. /* flush a full buffer */
  1941. if (num_add == filter_list_len) {
  1942. i40e_aqc_add_filters(vsi, vsi_name, add_list,
  1943. add_head, num_add,
  1944. &promisc_changed);
  1945. memset(add_list, 0, list_size);
  1946. num_add = 0;
  1947. }
  1948. }
  1949. if (num_add) {
  1950. i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
  1951. num_add, &promisc_changed);
  1952. }
  1953. /* Now move all of the filters from the temp add list back to
  1954. * the VSI's list.
  1955. */
  1956. spin_lock_bh(&vsi->mac_filter_hash_lock);
  1957. hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
  1958. /* Only update the state if we're still NEW */
  1959. if (new->f->state == I40E_FILTER_NEW)
  1960. new->f->state = new->state;
  1961. hlist_del(&new->hlist);
  1962. kfree(new);
  1963. }
  1964. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  1965. kfree(add_list);
  1966. add_list = NULL;
  1967. }
  1968. /* Determine the number of active and failed filters. */
  1969. spin_lock_bh(&vsi->mac_filter_hash_lock);
  1970. vsi->active_filters = 0;
  1971. hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
  1972. if (f->state == I40E_FILTER_ACTIVE)
  1973. vsi->active_filters++;
  1974. else if (f->state == I40E_FILTER_FAILED)
  1975. failed_filters++;
  1976. }
  1977. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  1978. /* If promiscuous mode has changed, we need to calculate a new
  1979. * threshold for when we are safe to exit
  1980. */
  1981. if (promisc_changed)
  1982. vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
  1983. /* Check if we are able to exit overflow promiscuous mode. We can
  1984. * safely exit if we didn't just enter, we no longer have any failed
  1985. * filters, and we have reduced filters below the threshold value.
  1986. */
  1987. if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state) &&
  1988. !promisc_changed && !failed_filters &&
  1989. (vsi->active_filters < vsi->promisc_threshold)) {
  1990. dev_info(&pf->pdev->dev,
  1991. "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
  1992. vsi_name);
  1993. clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
  1994. promisc_changed = true;
  1995. vsi->promisc_threshold = 0;
  1996. }
  1997. /* if the VF is not trusted do not do promisc */
  1998. if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
  1999. clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
  2000. goto out;
  2001. }
  2002. /* check for changes in promiscuous modes */
  2003. if (changed_flags & IFF_ALLMULTI) {
  2004. bool cur_multipromisc;
  2005. cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
  2006. aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
  2007. vsi->seid,
  2008. cur_multipromisc,
  2009. NULL);
  2010. if (aq_ret) {
  2011. retval = i40e_aq_rc_to_posix(aq_ret,
  2012. hw->aq.asq_last_status);
  2013. dev_info(&pf->pdev->dev,
  2014. "set multi promisc failed on %s, err %s aq_err %s\n",
  2015. vsi_name,
  2016. i40e_stat_str(hw, aq_ret),
  2017. i40e_aq_str(hw, hw->aq.asq_last_status));
  2018. }
  2019. }
  2020. if ((changed_flags & IFF_PROMISC) ||
  2021. (promisc_changed &&
  2022. test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state))) {
  2023. bool cur_promisc;
  2024. cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
  2025. test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
  2026. &vsi->state));
  2027. if ((vsi->type == I40E_VSI_MAIN) &&
  2028. (pf->lan_veb != I40E_NO_VEB) &&
  2029. !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
  2030. /* set defport ON for Main VSI instead of true promisc
  2031. * this way we will get all unicast/multicast and VLAN
  2032. * promisc behavior but will not get VF or VMDq traffic
  2033. * replicated on the Main VSI.
  2034. */
  2035. if (pf->cur_promisc != cur_promisc) {
  2036. pf->cur_promisc = cur_promisc;
  2037. if (cur_promisc)
  2038. aq_ret =
  2039. i40e_aq_set_default_vsi(hw,
  2040. vsi->seid,
  2041. NULL);
  2042. else
  2043. aq_ret =
  2044. i40e_aq_clear_default_vsi(hw,
  2045. vsi->seid,
  2046. NULL);
  2047. if (aq_ret) {
  2048. retval = i40e_aq_rc_to_posix(aq_ret,
  2049. hw->aq.asq_last_status);
  2050. dev_info(&pf->pdev->dev,
  2051. "Set default VSI failed on %s, err %s, aq_err %s\n",
  2052. vsi_name,
  2053. i40e_stat_str(hw, aq_ret),
  2054. i40e_aq_str(hw,
  2055. hw->aq.asq_last_status));
  2056. }
  2057. }
  2058. } else {
  2059. aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
  2060. hw,
  2061. vsi->seid,
  2062. cur_promisc, NULL,
  2063. true);
  2064. if (aq_ret) {
  2065. retval =
  2066. i40e_aq_rc_to_posix(aq_ret,
  2067. hw->aq.asq_last_status);
  2068. dev_info(&pf->pdev->dev,
  2069. "set unicast promisc 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. aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
  2076. hw,
  2077. vsi->seid,
  2078. cur_promisc, NULL);
  2079. if (aq_ret) {
  2080. retval =
  2081. i40e_aq_rc_to_posix(aq_ret,
  2082. hw->aq.asq_last_status);
  2083. dev_info(&pf->pdev->dev,
  2084. "set multicast promisc failed on %s, err %s, aq_err %s\n",
  2085. vsi_name,
  2086. i40e_stat_str(hw, aq_ret),
  2087. i40e_aq_str(hw,
  2088. hw->aq.asq_last_status));
  2089. }
  2090. }
  2091. aq_ret = i40e_aq_set_vsi_broadcast(&vsi->back->hw,
  2092. vsi->seid,
  2093. cur_promisc, NULL);
  2094. if (aq_ret) {
  2095. retval = i40e_aq_rc_to_posix(aq_ret,
  2096. pf->hw.aq.asq_last_status);
  2097. dev_info(&pf->pdev->dev,
  2098. "set brdcast promisc failed, err %s, aq_err %s\n",
  2099. i40e_stat_str(hw, aq_ret),
  2100. i40e_aq_str(hw,
  2101. hw->aq.asq_last_status));
  2102. }
  2103. }
  2104. out:
  2105. /* if something went wrong then set the changed flag so we try again */
  2106. if (retval)
  2107. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  2108. clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
  2109. return retval;
  2110. err_no_memory:
  2111. /* Restore elements on the temporary add and delete lists */
  2112. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2113. err_no_memory_locked:
  2114. i40e_undo_del_filter_entries(vsi, &tmp_del_list);
  2115. i40e_undo_add_filter_entries(vsi, &tmp_add_list);
  2116. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2117. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  2118. clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
  2119. return -ENOMEM;
  2120. }
  2121. /**
  2122. * i40e_sync_filters_subtask - Sync the VSI filter list with HW
  2123. * @pf: board private structure
  2124. **/
  2125. static void i40e_sync_filters_subtask(struct i40e_pf *pf)
  2126. {
  2127. int v;
  2128. if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC))
  2129. return;
  2130. pf->flags &= ~I40E_FLAG_FILTER_SYNC;
  2131. for (v = 0; v < pf->num_alloc_vsi; v++) {
  2132. if (pf->vsi[v] &&
  2133. (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED)) {
  2134. int ret = i40e_sync_vsi_filters(pf->vsi[v]);
  2135. if (ret) {
  2136. /* come back and try again later */
  2137. pf->flags |= I40E_FLAG_FILTER_SYNC;
  2138. break;
  2139. }
  2140. }
  2141. }
  2142. }
  2143. /**
  2144. * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
  2145. * @netdev: network interface device structure
  2146. * @new_mtu: new value for maximum frame size
  2147. *
  2148. * Returns 0 on success, negative on failure
  2149. **/
  2150. static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
  2151. {
  2152. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2153. struct i40e_vsi *vsi = np->vsi;
  2154. struct i40e_pf *pf = vsi->back;
  2155. netdev_info(netdev, "changing MTU from %d to %d\n",
  2156. netdev->mtu, new_mtu);
  2157. netdev->mtu = new_mtu;
  2158. if (netif_running(netdev))
  2159. i40e_vsi_reinit_locked(vsi);
  2160. pf->flags |= (I40E_FLAG_SERVICE_CLIENT_REQUESTED |
  2161. I40E_FLAG_CLIENT_L2_CHANGE);
  2162. return 0;
  2163. }
  2164. /**
  2165. * i40e_ioctl - Access the hwtstamp interface
  2166. * @netdev: network interface device structure
  2167. * @ifr: interface request data
  2168. * @cmd: ioctl command
  2169. **/
  2170. int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  2171. {
  2172. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2173. struct i40e_pf *pf = np->vsi->back;
  2174. switch (cmd) {
  2175. case SIOCGHWTSTAMP:
  2176. return i40e_ptp_get_ts_config(pf, ifr);
  2177. case SIOCSHWTSTAMP:
  2178. return i40e_ptp_set_ts_config(pf, ifr);
  2179. default:
  2180. return -EOPNOTSUPP;
  2181. }
  2182. }
  2183. /**
  2184. * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
  2185. * @vsi: the vsi being adjusted
  2186. **/
  2187. void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
  2188. {
  2189. struct i40e_vsi_context ctxt;
  2190. i40e_status ret;
  2191. if ((vsi->info.valid_sections &
  2192. cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
  2193. ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
  2194. return; /* already enabled */
  2195. vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  2196. vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
  2197. I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
  2198. ctxt.seid = vsi->seid;
  2199. ctxt.info = vsi->info;
  2200. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  2201. if (ret) {
  2202. dev_info(&vsi->back->pdev->dev,
  2203. "update vlan stripping failed, err %s aq_err %s\n",
  2204. i40e_stat_str(&vsi->back->hw, ret),
  2205. i40e_aq_str(&vsi->back->hw,
  2206. vsi->back->hw.aq.asq_last_status));
  2207. }
  2208. }
  2209. /**
  2210. * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
  2211. * @vsi: the vsi being adjusted
  2212. **/
  2213. void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
  2214. {
  2215. struct i40e_vsi_context ctxt;
  2216. i40e_status ret;
  2217. if ((vsi->info.valid_sections &
  2218. cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
  2219. ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
  2220. I40E_AQ_VSI_PVLAN_EMOD_MASK))
  2221. return; /* already disabled */
  2222. vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  2223. vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
  2224. I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
  2225. ctxt.seid = vsi->seid;
  2226. ctxt.info = vsi->info;
  2227. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  2228. if (ret) {
  2229. dev_info(&vsi->back->pdev->dev,
  2230. "update vlan stripping failed, err %s aq_err %s\n",
  2231. i40e_stat_str(&vsi->back->hw, ret),
  2232. i40e_aq_str(&vsi->back->hw,
  2233. vsi->back->hw.aq.asq_last_status));
  2234. }
  2235. }
  2236. /**
  2237. * i40e_vlan_rx_register - Setup or shutdown vlan offload
  2238. * @netdev: network interface to be adjusted
  2239. * @features: netdev features to test if VLAN offload is enabled or not
  2240. **/
  2241. static void i40e_vlan_rx_register(struct net_device *netdev, u32 features)
  2242. {
  2243. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2244. struct i40e_vsi *vsi = np->vsi;
  2245. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  2246. i40e_vlan_stripping_enable(vsi);
  2247. else
  2248. i40e_vlan_stripping_disable(vsi);
  2249. }
  2250. /**
  2251. * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
  2252. * @vsi: the vsi being configured
  2253. * @vid: vlan id to be added (0 = untagged only , -1 = any)
  2254. *
  2255. * This is a helper function for adding a new MAC/VLAN filter with the
  2256. * specified VLAN for each existing MAC address already in the hash table.
  2257. * This function does *not* perform any accounting to update filters based on
  2258. * VLAN mode.
  2259. *
  2260. * NOTE: this function expects to be called while under the
  2261. * mac_filter_hash_lock
  2262. **/
  2263. int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
  2264. {
  2265. struct i40e_mac_filter *f, *add_f;
  2266. struct hlist_node *h;
  2267. int bkt;
  2268. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  2269. if (f->state == I40E_FILTER_REMOVE)
  2270. continue;
  2271. add_f = i40e_add_filter(vsi, f->macaddr, vid);
  2272. if (!add_f) {
  2273. dev_info(&vsi->back->pdev->dev,
  2274. "Could not add vlan filter %d for %pM\n",
  2275. vid, f->macaddr);
  2276. return -ENOMEM;
  2277. }
  2278. }
  2279. return 0;
  2280. }
  2281. /**
  2282. * i40e_vsi_add_vlan - Add VSI membership for given VLAN
  2283. * @vsi: the VSI being configured
  2284. * @vid: VLAN id to be added
  2285. **/
  2286. int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
  2287. {
  2288. int err;
  2289. if (!vid || vsi->info.pvid)
  2290. return -EINVAL;
  2291. /* Locked once because all functions invoked below iterates list*/
  2292. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2293. err = i40e_add_vlan_all_mac(vsi, vid);
  2294. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2295. if (err)
  2296. return err;
  2297. /* schedule our worker thread which will take care of
  2298. * applying the new filter changes
  2299. */
  2300. i40e_service_event_schedule(vsi->back);
  2301. return 0;
  2302. }
  2303. /**
  2304. * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
  2305. * @vsi: the vsi being configured
  2306. * @vid: vlan id to be removed (0 = untagged only , -1 = any)
  2307. *
  2308. * This function should be used to remove all VLAN filters which match the
  2309. * given VID. It does not schedule the service event and does not take the
  2310. * mac_filter_hash_lock so it may be combined with other operations under
  2311. * a single invocation of the mac_filter_hash_lock.
  2312. *
  2313. * NOTE: this function expects to be called while under the
  2314. * mac_filter_hash_lock
  2315. */
  2316. void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
  2317. {
  2318. struct i40e_mac_filter *f;
  2319. struct hlist_node *h;
  2320. int bkt;
  2321. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  2322. if (f->vlan == vid)
  2323. __i40e_del_filter(vsi, f);
  2324. }
  2325. }
  2326. /**
  2327. * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
  2328. * @vsi: the VSI being configured
  2329. * @vid: VLAN id to be removed
  2330. **/
  2331. void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
  2332. {
  2333. if (!vid || vsi->info.pvid)
  2334. return;
  2335. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2336. i40e_rm_vlan_all_mac(vsi, vid);
  2337. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2338. /* schedule our worker thread which will take care of
  2339. * applying the new filter changes
  2340. */
  2341. i40e_service_event_schedule(vsi->back);
  2342. }
  2343. /**
  2344. * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
  2345. * @netdev: network interface to be adjusted
  2346. * @vid: vlan id to be added
  2347. *
  2348. * net_device_ops implementation for adding vlan ids
  2349. **/
  2350. static int i40e_vlan_rx_add_vid(struct net_device *netdev,
  2351. __always_unused __be16 proto, u16 vid)
  2352. {
  2353. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2354. struct i40e_vsi *vsi = np->vsi;
  2355. int ret = 0;
  2356. if (vid >= VLAN_N_VID)
  2357. return -EINVAL;
  2358. /* If the network stack called us with vid = 0 then
  2359. * it is asking to receive priority tagged packets with
  2360. * vlan id 0. Our HW receives them by default when configured
  2361. * to receive untagged packets so there is no need to add an
  2362. * extra filter for vlan 0 tagged packets.
  2363. */
  2364. if (vid)
  2365. ret = i40e_vsi_add_vlan(vsi, vid);
  2366. if (!ret)
  2367. set_bit(vid, vsi->active_vlans);
  2368. return ret;
  2369. }
  2370. /**
  2371. * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
  2372. * @netdev: network interface to be adjusted
  2373. * @vid: vlan id to be removed
  2374. *
  2375. * net_device_ops implementation for removing vlan ids
  2376. **/
  2377. static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
  2378. __always_unused __be16 proto, u16 vid)
  2379. {
  2380. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2381. struct i40e_vsi *vsi = np->vsi;
  2382. /* return code is ignored as there is nothing a user
  2383. * can do about failure to remove and a log message was
  2384. * already printed from the other function
  2385. */
  2386. i40e_vsi_kill_vlan(vsi, vid);
  2387. clear_bit(vid, vsi->active_vlans);
  2388. return 0;
  2389. }
  2390. /**
  2391. * i40e_macaddr_init - explicitly write the mac address filters
  2392. *
  2393. * @vsi: pointer to the vsi
  2394. * @macaddr: the MAC address
  2395. *
  2396. * This is needed when the macaddr has been obtained by other
  2397. * means than the default, e.g., from Open Firmware or IDPROM.
  2398. * Returns 0 on success, negative on failure
  2399. **/
  2400. static int i40e_macaddr_init(struct i40e_vsi *vsi, u8 *macaddr)
  2401. {
  2402. int ret;
  2403. struct i40e_aqc_add_macvlan_element_data element;
  2404. ret = i40e_aq_mac_address_write(&vsi->back->hw,
  2405. I40E_AQC_WRITE_TYPE_LAA_WOL,
  2406. macaddr, NULL);
  2407. if (ret) {
  2408. dev_info(&vsi->back->pdev->dev,
  2409. "Addr change for VSI failed: %d\n", ret);
  2410. return -EADDRNOTAVAIL;
  2411. }
  2412. memset(&element, 0, sizeof(element));
  2413. ether_addr_copy(element.mac_addr, macaddr);
  2414. element.flags = cpu_to_le16(I40E_AQC_MACVLAN_ADD_PERFECT_MATCH);
  2415. ret = i40e_aq_add_macvlan(&vsi->back->hw, vsi->seid, &element, 1, NULL);
  2416. if (ret) {
  2417. dev_info(&vsi->back->pdev->dev,
  2418. "add filter failed err %s aq_err %s\n",
  2419. i40e_stat_str(&vsi->back->hw, ret),
  2420. i40e_aq_str(&vsi->back->hw,
  2421. vsi->back->hw.aq.asq_last_status));
  2422. }
  2423. return ret;
  2424. }
  2425. /**
  2426. * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
  2427. * @vsi: the vsi being brought back up
  2428. **/
  2429. static void i40e_restore_vlan(struct i40e_vsi *vsi)
  2430. {
  2431. u16 vid;
  2432. if (!vsi->netdev)
  2433. return;
  2434. i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
  2435. for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
  2436. i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
  2437. vid);
  2438. }
  2439. /**
  2440. * i40e_vsi_add_pvid - Add pvid for the VSI
  2441. * @vsi: the vsi being adjusted
  2442. * @vid: the vlan id to set as a PVID
  2443. **/
  2444. int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
  2445. {
  2446. struct i40e_vsi_context ctxt;
  2447. i40e_status ret;
  2448. vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  2449. vsi->info.pvid = cpu_to_le16(vid);
  2450. vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
  2451. I40E_AQ_VSI_PVLAN_INSERT_PVID |
  2452. I40E_AQ_VSI_PVLAN_EMOD_STR;
  2453. ctxt.seid = vsi->seid;
  2454. ctxt.info = vsi->info;
  2455. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  2456. if (ret) {
  2457. dev_info(&vsi->back->pdev->dev,
  2458. "add pvid failed, err %s aq_err %s\n",
  2459. i40e_stat_str(&vsi->back->hw, ret),
  2460. i40e_aq_str(&vsi->back->hw,
  2461. vsi->back->hw.aq.asq_last_status));
  2462. return -ENOENT;
  2463. }
  2464. return 0;
  2465. }
  2466. /**
  2467. * i40e_vsi_remove_pvid - Remove the pvid from the VSI
  2468. * @vsi: the vsi being adjusted
  2469. *
  2470. * Just use the vlan_rx_register() service to put it back to normal
  2471. **/
  2472. void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
  2473. {
  2474. i40e_vlan_stripping_disable(vsi);
  2475. vsi->info.pvid = 0;
  2476. }
  2477. /**
  2478. * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
  2479. * @vsi: ptr to the VSI
  2480. *
  2481. * If this function returns with an error, then it's possible one or
  2482. * more of the rings is populated (while the rest are not). It is the
  2483. * callers duty to clean those orphaned rings.
  2484. *
  2485. * Return 0 on success, negative on failure
  2486. **/
  2487. static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
  2488. {
  2489. int i, err = 0;
  2490. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2491. err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
  2492. return err;
  2493. }
  2494. /**
  2495. * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
  2496. * @vsi: ptr to the VSI
  2497. *
  2498. * Free VSI's transmit software resources
  2499. **/
  2500. static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
  2501. {
  2502. int i;
  2503. if (!vsi->tx_rings)
  2504. return;
  2505. for (i = 0; i < vsi->num_queue_pairs; i++)
  2506. if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
  2507. i40e_free_tx_resources(vsi->tx_rings[i]);
  2508. }
  2509. /**
  2510. * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
  2511. * @vsi: ptr to the VSI
  2512. *
  2513. * If this function returns with an error, then it's possible one or
  2514. * more of the rings is populated (while the rest are not). It is the
  2515. * callers duty to clean those orphaned rings.
  2516. *
  2517. * Return 0 on success, negative on failure
  2518. **/
  2519. static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
  2520. {
  2521. int i, err = 0;
  2522. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2523. err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
  2524. return err;
  2525. }
  2526. /**
  2527. * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
  2528. * @vsi: ptr to the VSI
  2529. *
  2530. * Free all receive software resources
  2531. **/
  2532. static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
  2533. {
  2534. int i;
  2535. if (!vsi->rx_rings)
  2536. return;
  2537. for (i = 0; i < vsi->num_queue_pairs; i++)
  2538. if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
  2539. i40e_free_rx_resources(vsi->rx_rings[i]);
  2540. }
  2541. /**
  2542. * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
  2543. * @ring: The Tx ring to configure
  2544. *
  2545. * This enables/disables XPS for a given Tx descriptor ring
  2546. * based on the TCs enabled for the VSI that ring belongs to.
  2547. **/
  2548. static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
  2549. {
  2550. struct i40e_vsi *vsi = ring->vsi;
  2551. cpumask_var_t mask;
  2552. if (!ring->q_vector || !ring->netdev)
  2553. return;
  2554. /* Single TC mode enable XPS */
  2555. if (vsi->tc_config.numtc <= 1) {
  2556. if (!test_and_set_bit(__I40E_TX_XPS_INIT_DONE, &ring->state))
  2557. netif_set_xps_queue(ring->netdev,
  2558. &ring->q_vector->affinity_mask,
  2559. ring->queue_index);
  2560. } else if (alloc_cpumask_var(&mask, GFP_KERNEL)) {
  2561. /* Disable XPS to allow selection based on TC */
  2562. bitmap_zero(cpumask_bits(mask), nr_cpumask_bits);
  2563. netif_set_xps_queue(ring->netdev, mask, ring->queue_index);
  2564. free_cpumask_var(mask);
  2565. }
  2566. /* schedule our worker thread which will take care of
  2567. * applying the new filter changes
  2568. */
  2569. i40e_service_event_schedule(vsi->back);
  2570. }
  2571. /**
  2572. * i40e_configure_tx_ring - Configure a transmit ring context and rest
  2573. * @ring: The Tx ring to configure
  2574. *
  2575. * Configure the Tx descriptor ring in the HMC context.
  2576. **/
  2577. static int i40e_configure_tx_ring(struct i40e_ring *ring)
  2578. {
  2579. struct i40e_vsi *vsi = ring->vsi;
  2580. u16 pf_q = vsi->base_queue + ring->queue_index;
  2581. struct i40e_hw *hw = &vsi->back->hw;
  2582. struct i40e_hmc_obj_txq tx_ctx;
  2583. i40e_status err = 0;
  2584. u32 qtx_ctl = 0;
  2585. /* some ATR related tx ring init */
  2586. if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
  2587. ring->atr_sample_rate = vsi->back->atr_sample_rate;
  2588. ring->atr_count = 0;
  2589. } else {
  2590. ring->atr_sample_rate = 0;
  2591. }
  2592. /* configure XPS */
  2593. i40e_config_xps_tx_ring(ring);
  2594. /* clear the context structure first */
  2595. memset(&tx_ctx, 0, sizeof(tx_ctx));
  2596. tx_ctx.new_context = 1;
  2597. tx_ctx.base = (ring->dma / 128);
  2598. tx_ctx.qlen = ring->count;
  2599. tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
  2600. I40E_FLAG_FD_ATR_ENABLED));
  2601. tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
  2602. /* FDIR VSI tx ring can still use RS bit and writebacks */
  2603. if (vsi->type != I40E_VSI_FDIR)
  2604. tx_ctx.head_wb_ena = 1;
  2605. tx_ctx.head_wb_addr = ring->dma +
  2606. (ring->count * sizeof(struct i40e_tx_desc));
  2607. /* As part of VSI creation/update, FW allocates certain
  2608. * Tx arbitration queue sets for each TC enabled for
  2609. * the VSI. The FW returns the handles to these queue
  2610. * sets as part of the response buffer to Add VSI,
  2611. * Update VSI, etc. AQ commands. It is expected that
  2612. * these queue set handles be associated with the Tx
  2613. * queues by the driver as part of the TX queue context
  2614. * initialization. This has to be done regardless of
  2615. * DCB as by default everything is mapped to TC0.
  2616. */
  2617. tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
  2618. tx_ctx.rdylist_act = 0;
  2619. /* clear the context in the HMC */
  2620. err = i40e_clear_lan_tx_queue_context(hw, pf_q);
  2621. if (err) {
  2622. dev_info(&vsi->back->pdev->dev,
  2623. "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
  2624. ring->queue_index, pf_q, err);
  2625. return -ENOMEM;
  2626. }
  2627. /* set the context in the HMC */
  2628. err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
  2629. if (err) {
  2630. dev_info(&vsi->back->pdev->dev,
  2631. "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
  2632. ring->queue_index, pf_q, err);
  2633. return -ENOMEM;
  2634. }
  2635. /* Now associate this queue with this PCI function */
  2636. if (vsi->type == I40E_VSI_VMDQ2) {
  2637. qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
  2638. qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
  2639. I40E_QTX_CTL_VFVM_INDX_MASK;
  2640. } else {
  2641. qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
  2642. }
  2643. qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
  2644. I40E_QTX_CTL_PF_INDX_MASK);
  2645. wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
  2646. i40e_flush(hw);
  2647. /* cache tail off for easier writes later */
  2648. ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
  2649. return 0;
  2650. }
  2651. /**
  2652. * i40e_configure_rx_ring - Configure a receive ring context
  2653. * @ring: The Rx ring to configure
  2654. *
  2655. * Configure the Rx descriptor ring in the HMC context.
  2656. **/
  2657. static int i40e_configure_rx_ring(struct i40e_ring *ring)
  2658. {
  2659. struct i40e_vsi *vsi = ring->vsi;
  2660. u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
  2661. u16 pf_q = vsi->base_queue + ring->queue_index;
  2662. struct i40e_hw *hw = &vsi->back->hw;
  2663. struct i40e_hmc_obj_rxq rx_ctx;
  2664. i40e_status err = 0;
  2665. ring->state = 0;
  2666. /* clear the context structure first */
  2667. memset(&rx_ctx, 0, sizeof(rx_ctx));
  2668. ring->rx_buf_len = vsi->rx_buf_len;
  2669. rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
  2670. BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
  2671. rx_ctx.base = (ring->dma / 128);
  2672. rx_ctx.qlen = ring->count;
  2673. /* use 32 byte descriptors */
  2674. rx_ctx.dsize = 1;
  2675. /* descriptor type is always zero
  2676. * rx_ctx.dtype = 0;
  2677. */
  2678. rx_ctx.hsplit_0 = 0;
  2679. rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
  2680. if (hw->revision_id == 0)
  2681. rx_ctx.lrxqthresh = 0;
  2682. else
  2683. rx_ctx.lrxqthresh = 2;
  2684. rx_ctx.crcstrip = 1;
  2685. rx_ctx.l2tsel = 1;
  2686. /* this controls whether VLAN is stripped from inner headers */
  2687. rx_ctx.showiv = 0;
  2688. /* set the prefena field to 1 because the manual says to */
  2689. rx_ctx.prefena = 1;
  2690. /* clear the context in the HMC */
  2691. err = i40e_clear_lan_rx_queue_context(hw, pf_q);
  2692. if (err) {
  2693. dev_info(&vsi->back->pdev->dev,
  2694. "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
  2695. ring->queue_index, pf_q, err);
  2696. return -ENOMEM;
  2697. }
  2698. /* set the context in the HMC */
  2699. err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
  2700. if (err) {
  2701. dev_info(&vsi->back->pdev->dev,
  2702. "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
  2703. ring->queue_index, pf_q, err);
  2704. return -ENOMEM;
  2705. }
  2706. /* cache tail for quicker writes, and clear the reg before use */
  2707. ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
  2708. writel(0, ring->tail);
  2709. i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
  2710. return 0;
  2711. }
  2712. /**
  2713. * i40e_vsi_configure_tx - Configure the VSI for Tx
  2714. * @vsi: VSI structure describing this set of rings and resources
  2715. *
  2716. * Configure the Tx VSI for operation.
  2717. **/
  2718. static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
  2719. {
  2720. int err = 0;
  2721. u16 i;
  2722. for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
  2723. err = i40e_configure_tx_ring(vsi->tx_rings[i]);
  2724. return err;
  2725. }
  2726. /**
  2727. * i40e_vsi_configure_rx - Configure the VSI for Rx
  2728. * @vsi: the VSI being configured
  2729. *
  2730. * Configure the Rx VSI for operation.
  2731. **/
  2732. static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
  2733. {
  2734. int err = 0;
  2735. u16 i;
  2736. if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) {
  2737. vsi->max_frame = I40E_MAX_RXBUFFER;
  2738. vsi->rx_buf_len = I40E_RXBUFFER_2048;
  2739. #if (PAGE_SIZE < 8192)
  2740. } else if (vsi->netdev->mtu <= ETH_DATA_LEN) {
  2741. vsi->max_frame = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
  2742. vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
  2743. #endif
  2744. } else {
  2745. vsi->max_frame = I40E_MAX_RXBUFFER;
  2746. vsi->rx_buf_len = I40E_RXBUFFER_2048;
  2747. }
  2748. /* set up individual rings */
  2749. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2750. err = i40e_configure_rx_ring(vsi->rx_rings[i]);
  2751. return err;
  2752. }
  2753. /**
  2754. * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
  2755. * @vsi: ptr to the VSI
  2756. **/
  2757. static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
  2758. {
  2759. struct i40e_ring *tx_ring, *rx_ring;
  2760. u16 qoffset, qcount;
  2761. int i, n;
  2762. if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
  2763. /* Reset the TC information */
  2764. for (i = 0; i < vsi->num_queue_pairs; i++) {
  2765. rx_ring = vsi->rx_rings[i];
  2766. tx_ring = vsi->tx_rings[i];
  2767. rx_ring->dcb_tc = 0;
  2768. tx_ring->dcb_tc = 0;
  2769. }
  2770. }
  2771. for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
  2772. if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
  2773. continue;
  2774. qoffset = vsi->tc_config.tc_info[n].qoffset;
  2775. qcount = vsi->tc_config.tc_info[n].qcount;
  2776. for (i = qoffset; i < (qoffset + qcount); i++) {
  2777. rx_ring = vsi->rx_rings[i];
  2778. tx_ring = vsi->tx_rings[i];
  2779. rx_ring->dcb_tc = n;
  2780. tx_ring->dcb_tc = n;
  2781. }
  2782. }
  2783. }
  2784. /**
  2785. * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
  2786. * @vsi: ptr to the VSI
  2787. **/
  2788. static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
  2789. {
  2790. struct i40e_pf *pf = vsi->back;
  2791. int err;
  2792. if (vsi->netdev)
  2793. i40e_set_rx_mode(vsi->netdev);
  2794. if (!!(pf->flags & I40E_FLAG_PF_MAC)) {
  2795. err = i40e_macaddr_init(vsi, pf->hw.mac.addr);
  2796. if (err) {
  2797. dev_warn(&pf->pdev->dev,
  2798. "could not set up macaddr; err %d\n", err);
  2799. }
  2800. }
  2801. }
  2802. /**
  2803. * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
  2804. * @vsi: Pointer to the targeted VSI
  2805. *
  2806. * This function replays the hlist on the hw where all the SB Flow Director
  2807. * filters were saved.
  2808. **/
  2809. static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
  2810. {
  2811. struct i40e_fdir_filter *filter;
  2812. struct i40e_pf *pf = vsi->back;
  2813. struct hlist_node *node;
  2814. if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
  2815. return;
  2816. /* Reset FDir counters as we're replaying all existing filters */
  2817. pf->fd_tcp4_filter_cnt = 0;
  2818. pf->fd_udp4_filter_cnt = 0;
  2819. pf->fd_sctp4_filter_cnt = 0;
  2820. pf->fd_ip4_filter_cnt = 0;
  2821. hlist_for_each_entry_safe(filter, node,
  2822. &pf->fdir_filter_list, fdir_node) {
  2823. i40e_add_del_fdir(vsi, filter, true);
  2824. }
  2825. }
  2826. /**
  2827. * i40e_vsi_configure - Set up the VSI for action
  2828. * @vsi: the VSI being configured
  2829. **/
  2830. static int i40e_vsi_configure(struct i40e_vsi *vsi)
  2831. {
  2832. int err;
  2833. i40e_set_vsi_rx_mode(vsi);
  2834. i40e_restore_vlan(vsi);
  2835. i40e_vsi_config_dcb_rings(vsi);
  2836. err = i40e_vsi_configure_tx(vsi);
  2837. if (!err)
  2838. err = i40e_vsi_configure_rx(vsi);
  2839. return err;
  2840. }
  2841. /**
  2842. * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
  2843. * @vsi: the VSI being configured
  2844. **/
  2845. static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
  2846. {
  2847. struct i40e_pf *pf = vsi->back;
  2848. struct i40e_hw *hw = &pf->hw;
  2849. u16 vector;
  2850. int i, q;
  2851. u32 qp;
  2852. /* The interrupt indexing is offset by 1 in the PFINT_ITRn
  2853. * and PFINT_LNKLSTn registers, e.g.:
  2854. * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
  2855. */
  2856. qp = vsi->base_queue;
  2857. vector = vsi->base_vector;
  2858. for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
  2859. struct i40e_q_vector *q_vector = vsi->q_vectors[i];
  2860. q_vector->itr_countdown = ITR_COUNTDOWN_START;
  2861. q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[i]->rx_itr_setting);
  2862. q_vector->rx.latency_range = I40E_LOW_LATENCY;
  2863. wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
  2864. q_vector->rx.itr);
  2865. q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[i]->tx_itr_setting);
  2866. q_vector->tx.latency_range = I40E_LOW_LATENCY;
  2867. wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
  2868. q_vector->tx.itr);
  2869. wr32(hw, I40E_PFINT_RATEN(vector - 1),
  2870. i40e_intrl_usec_to_reg(vsi->int_rate_limit));
  2871. /* Linked list for the queuepairs assigned to this vector */
  2872. wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
  2873. for (q = 0; q < q_vector->num_ringpairs; q++) {
  2874. u32 val;
  2875. val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  2876. (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
  2877. (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
  2878. (qp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
  2879. (I40E_QUEUE_TYPE_TX
  2880. << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
  2881. wr32(hw, I40E_QINT_RQCTL(qp), val);
  2882. val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  2883. (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
  2884. (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
  2885. ((qp+1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT)|
  2886. (I40E_QUEUE_TYPE_RX
  2887. << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
  2888. /* Terminate the linked list */
  2889. if (q == (q_vector->num_ringpairs - 1))
  2890. val |= (I40E_QUEUE_END_OF_LIST
  2891. << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
  2892. wr32(hw, I40E_QINT_TQCTL(qp), val);
  2893. qp++;
  2894. }
  2895. }
  2896. i40e_flush(hw);
  2897. }
  2898. /**
  2899. * i40e_enable_misc_int_causes - enable the non-queue interrupts
  2900. * @hw: ptr to the hardware info
  2901. **/
  2902. static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
  2903. {
  2904. struct i40e_hw *hw = &pf->hw;
  2905. u32 val;
  2906. /* clear things first */
  2907. wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
  2908. rd32(hw, I40E_PFINT_ICR0); /* read to clear */
  2909. val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
  2910. I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
  2911. I40E_PFINT_ICR0_ENA_GRST_MASK |
  2912. I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
  2913. I40E_PFINT_ICR0_ENA_GPIO_MASK |
  2914. I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
  2915. I40E_PFINT_ICR0_ENA_VFLR_MASK |
  2916. I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
  2917. if (pf->flags & I40E_FLAG_IWARP_ENABLED)
  2918. val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
  2919. if (pf->flags & I40E_FLAG_PTP)
  2920. val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
  2921. wr32(hw, I40E_PFINT_ICR0_ENA, val);
  2922. /* SW_ITR_IDX = 0, but don't change INTENA */
  2923. wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
  2924. I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
  2925. /* OTHER_ITR_IDX = 0 */
  2926. wr32(hw, I40E_PFINT_STAT_CTL0, 0);
  2927. }
  2928. /**
  2929. * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
  2930. * @vsi: the VSI being configured
  2931. **/
  2932. static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
  2933. {
  2934. struct i40e_q_vector *q_vector = vsi->q_vectors[0];
  2935. struct i40e_pf *pf = vsi->back;
  2936. struct i40e_hw *hw = &pf->hw;
  2937. u32 val;
  2938. /* set the ITR configuration */
  2939. q_vector->itr_countdown = ITR_COUNTDOWN_START;
  2940. q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[0]->rx_itr_setting);
  2941. q_vector->rx.latency_range = I40E_LOW_LATENCY;
  2942. wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
  2943. q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[0]->tx_itr_setting);
  2944. q_vector->tx.latency_range = I40E_LOW_LATENCY;
  2945. wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
  2946. i40e_enable_misc_int_causes(pf);
  2947. /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
  2948. wr32(hw, I40E_PFINT_LNKLST0, 0);
  2949. /* Associate the queue pair to the vector and enable the queue int */
  2950. val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  2951. (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
  2952. (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
  2953. wr32(hw, I40E_QINT_RQCTL(0), val);
  2954. val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  2955. (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
  2956. (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
  2957. wr32(hw, I40E_QINT_TQCTL(0), val);
  2958. i40e_flush(hw);
  2959. }
  2960. /**
  2961. * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
  2962. * @pf: board private structure
  2963. **/
  2964. void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
  2965. {
  2966. struct i40e_hw *hw = &pf->hw;
  2967. wr32(hw, I40E_PFINT_DYN_CTL0,
  2968. I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
  2969. i40e_flush(hw);
  2970. }
  2971. /**
  2972. * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
  2973. * @pf: board private structure
  2974. * @clearpba: true when all pending interrupt events should be cleared
  2975. **/
  2976. void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf, bool clearpba)
  2977. {
  2978. struct i40e_hw *hw = &pf->hw;
  2979. u32 val;
  2980. val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
  2981. (clearpba ? I40E_PFINT_DYN_CTL0_CLEARPBA_MASK : 0) |
  2982. (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
  2983. wr32(hw, I40E_PFINT_DYN_CTL0, val);
  2984. i40e_flush(hw);
  2985. }
  2986. /**
  2987. * i40e_msix_clean_rings - MSIX mode Interrupt Handler
  2988. * @irq: interrupt number
  2989. * @data: pointer to a q_vector
  2990. **/
  2991. static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
  2992. {
  2993. struct i40e_q_vector *q_vector = data;
  2994. if (!q_vector->tx.ring && !q_vector->rx.ring)
  2995. return IRQ_HANDLED;
  2996. napi_schedule_irqoff(&q_vector->napi);
  2997. return IRQ_HANDLED;
  2998. }
  2999. /**
  3000. * i40e_irq_affinity_notify - Callback for affinity changes
  3001. * @notify: context as to what irq was changed
  3002. * @mask: the new affinity mask
  3003. *
  3004. * This is a callback function used by the irq_set_affinity_notifier function
  3005. * so that we may register to receive changes to the irq affinity masks.
  3006. **/
  3007. static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
  3008. const cpumask_t *mask)
  3009. {
  3010. struct i40e_q_vector *q_vector =
  3011. container_of(notify, struct i40e_q_vector, affinity_notify);
  3012. q_vector->affinity_mask = *mask;
  3013. }
  3014. /**
  3015. * i40e_irq_affinity_release - Callback for affinity notifier release
  3016. * @ref: internal core kernel usage
  3017. *
  3018. * This is a callback function used by the irq_set_affinity_notifier function
  3019. * to inform the current notification subscriber that they will no longer
  3020. * receive notifications.
  3021. **/
  3022. static void i40e_irq_affinity_release(struct kref *ref) {}
  3023. /**
  3024. * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
  3025. * @vsi: the VSI being configured
  3026. * @basename: name for the vector
  3027. *
  3028. * Allocates MSI-X vectors and requests interrupts from the kernel.
  3029. **/
  3030. static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
  3031. {
  3032. int q_vectors = vsi->num_q_vectors;
  3033. struct i40e_pf *pf = vsi->back;
  3034. int base = vsi->base_vector;
  3035. int rx_int_idx = 0;
  3036. int tx_int_idx = 0;
  3037. int vector, err;
  3038. int irq_num;
  3039. for (vector = 0; vector < q_vectors; vector++) {
  3040. struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
  3041. irq_num = pf->msix_entries[base + vector].vector;
  3042. if (q_vector->tx.ring && q_vector->rx.ring) {
  3043. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  3044. "%s-%s-%d", basename, "TxRx", rx_int_idx++);
  3045. tx_int_idx++;
  3046. } else if (q_vector->rx.ring) {
  3047. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  3048. "%s-%s-%d", basename, "rx", rx_int_idx++);
  3049. } else if (q_vector->tx.ring) {
  3050. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  3051. "%s-%s-%d", basename, "tx", tx_int_idx++);
  3052. } else {
  3053. /* skip this unused q_vector */
  3054. continue;
  3055. }
  3056. err = request_irq(irq_num,
  3057. vsi->irq_handler,
  3058. 0,
  3059. q_vector->name,
  3060. q_vector);
  3061. if (err) {
  3062. dev_info(&pf->pdev->dev,
  3063. "MSIX request_irq failed, error: %d\n", err);
  3064. goto free_queue_irqs;
  3065. }
  3066. /* register for affinity change notifications */
  3067. q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
  3068. q_vector->affinity_notify.release = i40e_irq_affinity_release;
  3069. irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
  3070. /* assign the mask for this irq */
  3071. irq_set_affinity_hint(irq_num, &q_vector->affinity_mask);
  3072. }
  3073. vsi->irqs_ready = true;
  3074. return 0;
  3075. free_queue_irqs:
  3076. while (vector) {
  3077. vector--;
  3078. irq_num = pf->msix_entries[base + vector].vector;
  3079. irq_set_affinity_notifier(irq_num, NULL);
  3080. irq_set_affinity_hint(irq_num, NULL);
  3081. free_irq(irq_num, &vsi->q_vectors[vector]);
  3082. }
  3083. return err;
  3084. }
  3085. /**
  3086. * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
  3087. * @vsi: the VSI being un-configured
  3088. **/
  3089. static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
  3090. {
  3091. struct i40e_pf *pf = vsi->back;
  3092. struct i40e_hw *hw = &pf->hw;
  3093. int base = vsi->base_vector;
  3094. int i;
  3095. for (i = 0; i < vsi->num_queue_pairs; i++) {
  3096. wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), 0);
  3097. wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), 0);
  3098. }
  3099. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3100. for (i = vsi->base_vector;
  3101. i < (vsi->num_q_vectors + vsi->base_vector); i++)
  3102. wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
  3103. i40e_flush(hw);
  3104. for (i = 0; i < vsi->num_q_vectors; i++)
  3105. synchronize_irq(pf->msix_entries[i + base].vector);
  3106. } else {
  3107. /* Legacy and MSI mode - this stops all interrupt handling */
  3108. wr32(hw, I40E_PFINT_ICR0_ENA, 0);
  3109. wr32(hw, I40E_PFINT_DYN_CTL0, 0);
  3110. i40e_flush(hw);
  3111. synchronize_irq(pf->pdev->irq);
  3112. }
  3113. }
  3114. /**
  3115. * i40e_vsi_enable_irq - Enable IRQ for the given VSI
  3116. * @vsi: the VSI being configured
  3117. **/
  3118. static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
  3119. {
  3120. struct i40e_pf *pf = vsi->back;
  3121. int i;
  3122. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3123. for (i = 0; i < vsi->num_q_vectors; i++)
  3124. i40e_irq_dynamic_enable(vsi, i);
  3125. } else {
  3126. i40e_irq_dynamic_enable_icr0(pf, true);
  3127. }
  3128. i40e_flush(&pf->hw);
  3129. return 0;
  3130. }
  3131. /**
  3132. * i40e_stop_misc_vector - Stop the vector that handles non-queue events
  3133. * @pf: board private structure
  3134. **/
  3135. static void i40e_stop_misc_vector(struct i40e_pf *pf)
  3136. {
  3137. /* Disable ICR 0 */
  3138. wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
  3139. i40e_flush(&pf->hw);
  3140. }
  3141. /**
  3142. * i40e_intr - MSI/Legacy and non-queue interrupt handler
  3143. * @irq: interrupt number
  3144. * @data: pointer to a q_vector
  3145. *
  3146. * This is the handler used for all MSI/Legacy interrupts, and deals
  3147. * with both queue and non-queue interrupts. This is also used in
  3148. * MSIX mode to handle the non-queue interrupts.
  3149. **/
  3150. static irqreturn_t i40e_intr(int irq, void *data)
  3151. {
  3152. struct i40e_pf *pf = (struct i40e_pf *)data;
  3153. struct i40e_hw *hw = &pf->hw;
  3154. irqreturn_t ret = IRQ_NONE;
  3155. u32 icr0, icr0_remaining;
  3156. u32 val, ena_mask;
  3157. icr0 = rd32(hw, I40E_PFINT_ICR0);
  3158. ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
  3159. /* if sharing a legacy IRQ, we might get called w/o an intr pending */
  3160. if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
  3161. goto enable_intr;
  3162. /* if interrupt but no bits showing, must be SWINT */
  3163. if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
  3164. (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
  3165. pf->sw_int_count++;
  3166. if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
  3167. (ena_mask & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
  3168. ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
  3169. icr0 &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
  3170. dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
  3171. }
  3172. /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
  3173. if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
  3174. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  3175. struct i40e_q_vector *q_vector = vsi->q_vectors[0];
  3176. /* We do not have a way to disarm Queue causes while leaving
  3177. * interrupt enabled for all other causes, ideally
  3178. * interrupt should be disabled while we are in NAPI but
  3179. * this is not a performance path and napi_schedule()
  3180. * can deal with rescheduling.
  3181. */
  3182. if (!test_bit(__I40E_DOWN, &pf->state))
  3183. napi_schedule_irqoff(&q_vector->napi);
  3184. }
  3185. if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
  3186. ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
  3187. set_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
  3188. i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
  3189. }
  3190. if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
  3191. ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
  3192. set_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
  3193. }
  3194. if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
  3195. ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
  3196. set_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
  3197. }
  3198. if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
  3199. if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
  3200. set_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
  3201. ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
  3202. val = rd32(hw, I40E_GLGEN_RSTAT);
  3203. val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
  3204. >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
  3205. if (val == I40E_RESET_CORER) {
  3206. pf->corer_count++;
  3207. } else if (val == I40E_RESET_GLOBR) {
  3208. pf->globr_count++;
  3209. } else if (val == I40E_RESET_EMPR) {
  3210. pf->empr_count++;
  3211. set_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state);
  3212. }
  3213. }
  3214. if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
  3215. icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
  3216. dev_info(&pf->pdev->dev, "HMC error interrupt\n");
  3217. dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
  3218. rd32(hw, I40E_PFHMC_ERRORINFO),
  3219. rd32(hw, I40E_PFHMC_ERRORDATA));
  3220. }
  3221. if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
  3222. u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
  3223. if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
  3224. icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
  3225. i40e_ptp_tx_hwtstamp(pf);
  3226. }
  3227. }
  3228. /* If a critical error is pending we have no choice but to reset the
  3229. * device.
  3230. * Report and mask out any remaining unexpected interrupts.
  3231. */
  3232. icr0_remaining = icr0 & ena_mask;
  3233. if (icr0_remaining) {
  3234. dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
  3235. icr0_remaining);
  3236. if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
  3237. (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
  3238. (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
  3239. dev_info(&pf->pdev->dev, "device will be reset\n");
  3240. set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
  3241. i40e_service_event_schedule(pf);
  3242. }
  3243. ena_mask &= ~icr0_remaining;
  3244. }
  3245. ret = IRQ_HANDLED;
  3246. enable_intr:
  3247. /* re-enable interrupt causes */
  3248. wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
  3249. if (!test_bit(__I40E_DOWN, &pf->state)) {
  3250. i40e_service_event_schedule(pf);
  3251. i40e_irq_dynamic_enable_icr0(pf, false);
  3252. }
  3253. return ret;
  3254. }
  3255. /**
  3256. * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
  3257. * @tx_ring: tx ring to clean
  3258. * @budget: how many cleans we're allowed
  3259. *
  3260. * Returns true if there's any budget left (e.g. the clean is finished)
  3261. **/
  3262. static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
  3263. {
  3264. struct i40e_vsi *vsi = tx_ring->vsi;
  3265. u16 i = tx_ring->next_to_clean;
  3266. struct i40e_tx_buffer *tx_buf;
  3267. struct i40e_tx_desc *tx_desc;
  3268. tx_buf = &tx_ring->tx_bi[i];
  3269. tx_desc = I40E_TX_DESC(tx_ring, i);
  3270. i -= tx_ring->count;
  3271. do {
  3272. struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
  3273. /* if next_to_watch is not set then there is no work pending */
  3274. if (!eop_desc)
  3275. break;
  3276. /* prevent any other reads prior to eop_desc */
  3277. read_barrier_depends();
  3278. /* if the descriptor isn't done, no work yet to do */
  3279. if (!(eop_desc->cmd_type_offset_bsz &
  3280. cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
  3281. break;
  3282. /* clear next_to_watch to prevent false hangs */
  3283. tx_buf->next_to_watch = NULL;
  3284. tx_desc->buffer_addr = 0;
  3285. tx_desc->cmd_type_offset_bsz = 0;
  3286. /* move past filter desc */
  3287. tx_buf++;
  3288. tx_desc++;
  3289. i++;
  3290. if (unlikely(!i)) {
  3291. i -= tx_ring->count;
  3292. tx_buf = tx_ring->tx_bi;
  3293. tx_desc = I40E_TX_DESC(tx_ring, 0);
  3294. }
  3295. /* unmap skb header data */
  3296. dma_unmap_single(tx_ring->dev,
  3297. dma_unmap_addr(tx_buf, dma),
  3298. dma_unmap_len(tx_buf, len),
  3299. DMA_TO_DEVICE);
  3300. if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
  3301. kfree(tx_buf->raw_buf);
  3302. tx_buf->raw_buf = NULL;
  3303. tx_buf->tx_flags = 0;
  3304. tx_buf->next_to_watch = NULL;
  3305. dma_unmap_len_set(tx_buf, len, 0);
  3306. tx_desc->buffer_addr = 0;
  3307. tx_desc->cmd_type_offset_bsz = 0;
  3308. /* move us past the eop_desc for start of next FD desc */
  3309. tx_buf++;
  3310. tx_desc++;
  3311. i++;
  3312. if (unlikely(!i)) {
  3313. i -= tx_ring->count;
  3314. tx_buf = tx_ring->tx_bi;
  3315. tx_desc = I40E_TX_DESC(tx_ring, 0);
  3316. }
  3317. /* update budget accounting */
  3318. budget--;
  3319. } while (likely(budget));
  3320. i += tx_ring->count;
  3321. tx_ring->next_to_clean = i;
  3322. if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
  3323. i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
  3324. return budget > 0;
  3325. }
  3326. /**
  3327. * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
  3328. * @irq: interrupt number
  3329. * @data: pointer to a q_vector
  3330. **/
  3331. static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
  3332. {
  3333. struct i40e_q_vector *q_vector = data;
  3334. struct i40e_vsi *vsi;
  3335. if (!q_vector->tx.ring)
  3336. return IRQ_HANDLED;
  3337. vsi = q_vector->tx.ring->vsi;
  3338. i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
  3339. return IRQ_HANDLED;
  3340. }
  3341. /**
  3342. * i40e_map_vector_to_qp - Assigns the queue pair to the vector
  3343. * @vsi: the VSI being configured
  3344. * @v_idx: vector index
  3345. * @qp_idx: queue pair index
  3346. **/
  3347. static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
  3348. {
  3349. struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
  3350. struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
  3351. struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
  3352. tx_ring->q_vector = q_vector;
  3353. tx_ring->next = q_vector->tx.ring;
  3354. q_vector->tx.ring = tx_ring;
  3355. q_vector->tx.count++;
  3356. rx_ring->q_vector = q_vector;
  3357. rx_ring->next = q_vector->rx.ring;
  3358. q_vector->rx.ring = rx_ring;
  3359. q_vector->rx.count++;
  3360. }
  3361. /**
  3362. * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
  3363. * @vsi: the VSI being configured
  3364. *
  3365. * This function maps descriptor rings to the queue-specific vectors
  3366. * we were allotted through the MSI-X enabling code. Ideally, we'd have
  3367. * one vector per queue pair, but on a constrained vector budget, we
  3368. * group the queue pairs as "efficiently" as possible.
  3369. **/
  3370. static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
  3371. {
  3372. int qp_remaining = vsi->num_queue_pairs;
  3373. int q_vectors = vsi->num_q_vectors;
  3374. int num_ringpairs;
  3375. int v_start = 0;
  3376. int qp_idx = 0;
  3377. /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
  3378. * group them so there are multiple queues per vector.
  3379. * It is also important to go through all the vectors available to be
  3380. * sure that if we don't use all the vectors, that the remaining vectors
  3381. * are cleared. This is especially important when decreasing the
  3382. * number of queues in use.
  3383. */
  3384. for (; v_start < q_vectors; v_start++) {
  3385. struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
  3386. num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
  3387. q_vector->num_ringpairs = num_ringpairs;
  3388. q_vector->rx.count = 0;
  3389. q_vector->tx.count = 0;
  3390. q_vector->rx.ring = NULL;
  3391. q_vector->tx.ring = NULL;
  3392. while (num_ringpairs--) {
  3393. i40e_map_vector_to_qp(vsi, v_start, qp_idx);
  3394. qp_idx++;
  3395. qp_remaining--;
  3396. }
  3397. }
  3398. }
  3399. /**
  3400. * i40e_vsi_request_irq - Request IRQ from the OS
  3401. * @vsi: the VSI being configured
  3402. * @basename: name for the vector
  3403. **/
  3404. static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
  3405. {
  3406. struct i40e_pf *pf = vsi->back;
  3407. int err;
  3408. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  3409. err = i40e_vsi_request_irq_msix(vsi, basename);
  3410. else if (pf->flags & I40E_FLAG_MSI_ENABLED)
  3411. err = request_irq(pf->pdev->irq, i40e_intr, 0,
  3412. pf->int_name, pf);
  3413. else
  3414. err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
  3415. pf->int_name, pf);
  3416. if (err)
  3417. dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
  3418. return err;
  3419. }
  3420. #ifdef CONFIG_NET_POLL_CONTROLLER
  3421. /**
  3422. * i40e_netpoll - A Polling 'interrupt' handler
  3423. * @netdev: network interface device structure
  3424. *
  3425. * This is used by netconsole to send skbs without having to re-enable
  3426. * interrupts. It's not called while the normal interrupt routine is executing.
  3427. **/
  3428. static void i40e_netpoll(struct net_device *netdev)
  3429. {
  3430. struct i40e_netdev_priv *np = netdev_priv(netdev);
  3431. struct i40e_vsi *vsi = np->vsi;
  3432. struct i40e_pf *pf = vsi->back;
  3433. int i;
  3434. /* if interface is down do nothing */
  3435. if (test_bit(__I40E_DOWN, &vsi->state))
  3436. return;
  3437. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3438. for (i = 0; i < vsi->num_q_vectors; i++)
  3439. i40e_msix_clean_rings(0, vsi->q_vectors[i]);
  3440. } else {
  3441. i40e_intr(pf->pdev->irq, netdev);
  3442. }
  3443. }
  3444. #endif
  3445. /**
  3446. * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
  3447. * @pf: the PF being configured
  3448. * @pf_q: the PF queue
  3449. * @enable: enable or disable state of the queue
  3450. *
  3451. * This routine will wait for the given Tx queue of the PF to reach the
  3452. * enabled or disabled state.
  3453. * Returns -ETIMEDOUT in case of failing to reach the requested state after
  3454. * multiple retries; else will return 0 in case of success.
  3455. **/
  3456. static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
  3457. {
  3458. int i;
  3459. u32 tx_reg;
  3460. for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
  3461. tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
  3462. if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
  3463. break;
  3464. usleep_range(10, 20);
  3465. }
  3466. if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
  3467. return -ETIMEDOUT;
  3468. return 0;
  3469. }
  3470. /**
  3471. * i40e_vsi_control_tx - Start or stop a VSI's rings
  3472. * @vsi: the VSI being configured
  3473. * @enable: start or stop the rings
  3474. **/
  3475. static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
  3476. {
  3477. struct i40e_pf *pf = vsi->back;
  3478. struct i40e_hw *hw = &pf->hw;
  3479. int i, j, pf_q, ret = 0;
  3480. u32 tx_reg;
  3481. pf_q = vsi->base_queue;
  3482. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  3483. /* warn the TX unit of coming changes */
  3484. i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
  3485. if (!enable)
  3486. usleep_range(10, 20);
  3487. for (j = 0; j < 50; j++) {
  3488. tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
  3489. if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
  3490. ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
  3491. break;
  3492. usleep_range(1000, 2000);
  3493. }
  3494. /* Skip if the queue is already in the requested state */
  3495. if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
  3496. continue;
  3497. /* turn on/off the queue */
  3498. if (enable) {
  3499. wr32(hw, I40E_QTX_HEAD(pf_q), 0);
  3500. tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
  3501. } else {
  3502. tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
  3503. }
  3504. wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
  3505. /* No waiting for the Tx queue to disable */
  3506. if (!enable && test_bit(__I40E_PORT_TX_SUSPENDED, &pf->state))
  3507. continue;
  3508. /* wait for the change to finish */
  3509. ret = i40e_pf_txq_wait(pf, pf_q, enable);
  3510. if (ret) {
  3511. dev_info(&pf->pdev->dev,
  3512. "VSI seid %d Tx ring %d %sable timeout\n",
  3513. vsi->seid, pf_q, (enable ? "en" : "dis"));
  3514. break;
  3515. }
  3516. }
  3517. return ret;
  3518. }
  3519. /**
  3520. * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
  3521. * @pf: the PF being configured
  3522. * @pf_q: the PF queue
  3523. * @enable: enable or disable state of the queue
  3524. *
  3525. * This routine will wait for the given Rx queue of the PF to reach the
  3526. * enabled or disabled state.
  3527. * Returns -ETIMEDOUT in case of failing to reach the requested state after
  3528. * multiple retries; else will return 0 in case of success.
  3529. **/
  3530. static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
  3531. {
  3532. int i;
  3533. u32 rx_reg;
  3534. for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
  3535. rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
  3536. if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
  3537. break;
  3538. usleep_range(10, 20);
  3539. }
  3540. if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
  3541. return -ETIMEDOUT;
  3542. return 0;
  3543. }
  3544. /**
  3545. * i40e_vsi_control_rx - Start or stop a VSI's rings
  3546. * @vsi: the VSI being configured
  3547. * @enable: start or stop the rings
  3548. **/
  3549. static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
  3550. {
  3551. struct i40e_pf *pf = vsi->back;
  3552. struct i40e_hw *hw = &pf->hw;
  3553. int i, j, pf_q, ret = 0;
  3554. u32 rx_reg;
  3555. pf_q = vsi->base_queue;
  3556. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  3557. for (j = 0; j < 50; j++) {
  3558. rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
  3559. if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
  3560. ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
  3561. break;
  3562. usleep_range(1000, 2000);
  3563. }
  3564. /* Skip if the queue is already in the requested state */
  3565. if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
  3566. continue;
  3567. /* turn on/off the queue */
  3568. if (enable)
  3569. rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
  3570. else
  3571. rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
  3572. wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
  3573. /* No waiting for the Tx queue to disable */
  3574. if (!enable && test_bit(__I40E_PORT_TX_SUSPENDED, &pf->state))
  3575. continue;
  3576. /* wait for the change to finish */
  3577. ret = i40e_pf_rxq_wait(pf, pf_q, enable);
  3578. if (ret) {
  3579. dev_info(&pf->pdev->dev,
  3580. "VSI seid %d Rx ring %d %sable timeout\n",
  3581. vsi->seid, pf_q, (enable ? "en" : "dis"));
  3582. break;
  3583. }
  3584. }
  3585. /* Due to HW errata, on Rx disable only, the register can indicate done
  3586. * before it really is. Needs 50ms to be sure
  3587. */
  3588. if (!enable)
  3589. mdelay(50);
  3590. return ret;
  3591. }
  3592. /**
  3593. * i40e_vsi_start_rings - Start a VSI's rings
  3594. * @vsi: the VSI being configured
  3595. **/
  3596. int i40e_vsi_start_rings(struct i40e_vsi *vsi)
  3597. {
  3598. int ret = 0;
  3599. /* do rx first for enable and last for disable */
  3600. ret = i40e_vsi_control_rx(vsi, true);
  3601. if (ret)
  3602. return ret;
  3603. ret = i40e_vsi_control_tx(vsi, true);
  3604. return ret;
  3605. }
  3606. /**
  3607. * i40e_vsi_stop_rings - Stop a VSI's rings
  3608. * @vsi: the VSI being configured
  3609. **/
  3610. void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
  3611. {
  3612. /* do rx first for enable and last for disable
  3613. * Ignore return value, we need to shutdown whatever we can
  3614. */
  3615. i40e_vsi_control_tx(vsi, false);
  3616. i40e_vsi_control_rx(vsi, false);
  3617. }
  3618. /**
  3619. * i40e_vsi_free_irq - Free the irq association with the OS
  3620. * @vsi: the VSI being configured
  3621. **/
  3622. static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
  3623. {
  3624. struct i40e_pf *pf = vsi->back;
  3625. struct i40e_hw *hw = &pf->hw;
  3626. int base = vsi->base_vector;
  3627. u32 val, qp;
  3628. int i;
  3629. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3630. if (!vsi->q_vectors)
  3631. return;
  3632. if (!vsi->irqs_ready)
  3633. return;
  3634. vsi->irqs_ready = false;
  3635. for (i = 0; i < vsi->num_q_vectors; i++) {
  3636. int irq_num;
  3637. u16 vector;
  3638. vector = i + base;
  3639. irq_num = pf->msix_entries[vector].vector;
  3640. /* free only the irqs that were actually requested */
  3641. if (!vsi->q_vectors[i] ||
  3642. !vsi->q_vectors[i]->num_ringpairs)
  3643. continue;
  3644. /* clear the affinity notifier in the IRQ descriptor */
  3645. irq_set_affinity_notifier(irq_num, NULL);
  3646. /* clear the affinity_mask in the IRQ descriptor */
  3647. irq_set_affinity_hint(irq_num, NULL);
  3648. synchronize_irq(irq_num);
  3649. free_irq(irq_num, vsi->q_vectors[i]);
  3650. /* Tear down the interrupt queue link list
  3651. *
  3652. * We know that they come in pairs and always
  3653. * the Rx first, then the Tx. To clear the
  3654. * link list, stick the EOL value into the
  3655. * next_q field of the registers.
  3656. */
  3657. val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
  3658. qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
  3659. >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
  3660. val |= I40E_QUEUE_END_OF_LIST
  3661. << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
  3662. wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
  3663. while (qp != I40E_QUEUE_END_OF_LIST) {
  3664. u32 next;
  3665. val = rd32(hw, I40E_QINT_RQCTL(qp));
  3666. val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
  3667. I40E_QINT_RQCTL_MSIX0_INDX_MASK |
  3668. I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  3669. I40E_QINT_RQCTL_INTEVENT_MASK);
  3670. val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
  3671. I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
  3672. wr32(hw, I40E_QINT_RQCTL(qp), val);
  3673. val = rd32(hw, I40E_QINT_TQCTL(qp));
  3674. next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
  3675. >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
  3676. val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
  3677. I40E_QINT_TQCTL_MSIX0_INDX_MASK |
  3678. I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  3679. I40E_QINT_TQCTL_INTEVENT_MASK);
  3680. val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
  3681. I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
  3682. wr32(hw, I40E_QINT_TQCTL(qp), val);
  3683. qp = next;
  3684. }
  3685. }
  3686. } else {
  3687. free_irq(pf->pdev->irq, pf);
  3688. val = rd32(hw, I40E_PFINT_LNKLST0);
  3689. qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
  3690. >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
  3691. val |= I40E_QUEUE_END_OF_LIST
  3692. << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
  3693. wr32(hw, I40E_PFINT_LNKLST0, val);
  3694. val = rd32(hw, I40E_QINT_RQCTL(qp));
  3695. val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
  3696. I40E_QINT_RQCTL_MSIX0_INDX_MASK |
  3697. I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  3698. I40E_QINT_RQCTL_INTEVENT_MASK);
  3699. val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
  3700. I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
  3701. wr32(hw, I40E_QINT_RQCTL(qp), val);
  3702. val = rd32(hw, I40E_QINT_TQCTL(qp));
  3703. val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
  3704. I40E_QINT_TQCTL_MSIX0_INDX_MASK |
  3705. I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  3706. I40E_QINT_TQCTL_INTEVENT_MASK);
  3707. val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
  3708. I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
  3709. wr32(hw, I40E_QINT_TQCTL(qp), val);
  3710. }
  3711. }
  3712. /**
  3713. * i40e_free_q_vector - Free memory allocated for specific interrupt vector
  3714. * @vsi: the VSI being configured
  3715. * @v_idx: Index of vector to be freed
  3716. *
  3717. * This function frees the memory allocated to the q_vector. In addition if
  3718. * NAPI is enabled it will delete any references to the NAPI struct prior
  3719. * to freeing the q_vector.
  3720. **/
  3721. static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
  3722. {
  3723. struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
  3724. struct i40e_ring *ring;
  3725. if (!q_vector)
  3726. return;
  3727. /* disassociate q_vector from rings */
  3728. i40e_for_each_ring(ring, q_vector->tx)
  3729. ring->q_vector = NULL;
  3730. i40e_for_each_ring(ring, q_vector->rx)
  3731. ring->q_vector = NULL;
  3732. /* only VSI w/ an associated netdev is set up w/ NAPI */
  3733. if (vsi->netdev)
  3734. netif_napi_del(&q_vector->napi);
  3735. vsi->q_vectors[v_idx] = NULL;
  3736. kfree_rcu(q_vector, rcu);
  3737. }
  3738. /**
  3739. * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
  3740. * @vsi: the VSI being un-configured
  3741. *
  3742. * This frees the memory allocated to the q_vectors and
  3743. * deletes references to the NAPI struct.
  3744. **/
  3745. static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
  3746. {
  3747. int v_idx;
  3748. for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
  3749. i40e_free_q_vector(vsi, v_idx);
  3750. }
  3751. /**
  3752. * i40e_reset_interrupt_capability - Disable interrupt setup in OS
  3753. * @pf: board private structure
  3754. **/
  3755. static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
  3756. {
  3757. /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
  3758. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3759. pci_disable_msix(pf->pdev);
  3760. kfree(pf->msix_entries);
  3761. pf->msix_entries = NULL;
  3762. kfree(pf->irq_pile);
  3763. pf->irq_pile = NULL;
  3764. } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
  3765. pci_disable_msi(pf->pdev);
  3766. }
  3767. pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
  3768. }
  3769. /**
  3770. * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
  3771. * @pf: board private structure
  3772. *
  3773. * We go through and clear interrupt specific resources and reset the structure
  3774. * to pre-load conditions
  3775. **/
  3776. static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
  3777. {
  3778. int i;
  3779. i40e_stop_misc_vector(pf);
  3780. if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
  3781. synchronize_irq(pf->msix_entries[0].vector);
  3782. free_irq(pf->msix_entries[0].vector, pf);
  3783. }
  3784. i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
  3785. I40E_IWARP_IRQ_PILE_ID);
  3786. i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
  3787. for (i = 0; i < pf->num_alloc_vsi; i++)
  3788. if (pf->vsi[i])
  3789. i40e_vsi_free_q_vectors(pf->vsi[i]);
  3790. i40e_reset_interrupt_capability(pf);
  3791. }
  3792. /**
  3793. * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
  3794. * @vsi: the VSI being configured
  3795. **/
  3796. static void i40e_napi_enable_all(struct i40e_vsi *vsi)
  3797. {
  3798. int q_idx;
  3799. if (!vsi->netdev)
  3800. return;
  3801. for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
  3802. napi_enable(&vsi->q_vectors[q_idx]->napi);
  3803. }
  3804. /**
  3805. * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
  3806. * @vsi: the VSI being configured
  3807. **/
  3808. static void i40e_napi_disable_all(struct i40e_vsi *vsi)
  3809. {
  3810. int q_idx;
  3811. if (!vsi->netdev)
  3812. return;
  3813. for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
  3814. napi_disable(&vsi->q_vectors[q_idx]->napi);
  3815. }
  3816. /**
  3817. * i40e_vsi_close - Shut down a VSI
  3818. * @vsi: the vsi to be quelled
  3819. **/
  3820. static void i40e_vsi_close(struct i40e_vsi *vsi)
  3821. {
  3822. struct i40e_pf *pf = vsi->back;
  3823. if (!test_and_set_bit(__I40E_DOWN, &vsi->state))
  3824. i40e_down(vsi);
  3825. i40e_vsi_free_irq(vsi);
  3826. i40e_vsi_free_tx_resources(vsi);
  3827. i40e_vsi_free_rx_resources(vsi);
  3828. vsi->current_netdev_flags = 0;
  3829. pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
  3830. if (test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
  3831. pf->flags |= I40E_FLAG_CLIENT_RESET;
  3832. }
  3833. /**
  3834. * i40e_quiesce_vsi - Pause a given VSI
  3835. * @vsi: the VSI being paused
  3836. **/
  3837. static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
  3838. {
  3839. if (test_bit(__I40E_DOWN, &vsi->state))
  3840. return;
  3841. set_bit(__I40E_NEEDS_RESTART, &vsi->state);
  3842. if (vsi->netdev && netif_running(vsi->netdev))
  3843. vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
  3844. else
  3845. i40e_vsi_close(vsi);
  3846. }
  3847. /**
  3848. * i40e_unquiesce_vsi - Resume a given VSI
  3849. * @vsi: the VSI being resumed
  3850. **/
  3851. static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
  3852. {
  3853. if (!test_bit(__I40E_NEEDS_RESTART, &vsi->state))
  3854. return;
  3855. clear_bit(__I40E_NEEDS_RESTART, &vsi->state);
  3856. if (vsi->netdev && netif_running(vsi->netdev))
  3857. vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
  3858. else
  3859. i40e_vsi_open(vsi); /* this clears the DOWN bit */
  3860. }
  3861. /**
  3862. * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
  3863. * @pf: the PF
  3864. **/
  3865. static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
  3866. {
  3867. int v;
  3868. for (v = 0; v < pf->num_alloc_vsi; v++) {
  3869. if (pf->vsi[v])
  3870. i40e_quiesce_vsi(pf->vsi[v]);
  3871. }
  3872. }
  3873. /**
  3874. * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
  3875. * @pf: the PF
  3876. **/
  3877. static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
  3878. {
  3879. int v;
  3880. for (v = 0; v < pf->num_alloc_vsi; v++) {
  3881. if (pf->vsi[v])
  3882. i40e_unquiesce_vsi(pf->vsi[v]);
  3883. }
  3884. }
  3885. #ifdef CONFIG_I40E_DCB
  3886. /**
  3887. * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
  3888. * @vsi: the VSI being configured
  3889. *
  3890. * This function waits for the given VSI's queues to be disabled.
  3891. **/
  3892. static int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
  3893. {
  3894. struct i40e_pf *pf = vsi->back;
  3895. int i, pf_q, ret;
  3896. pf_q = vsi->base_queue;
  3897. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  3898. /* Check and wait for the disable status of the queue */
  3899. ret = i40e_pf_txq_wait(pf, pf_q, false);
  3900. if (ret) {
  3901. dev_info(&pf->pdev->dev,
  3902. "VSI seid %d Tx ring %d disable timeout\n",
  3903. vsi->seid, pf_q);
  3904. return ret;
  3905. }
  3906. }
  3907. pf_q = vsi->base_queue;
  3908. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  3909. /* Check and wait for the disable status of the queue */
  3910. ret = i40e_pf_rxq_wait(pf, pf_q, false);
  3911. if (ret) {
  3912. dev_info(&pf->pdev->dev,
  3913. "VSI seid %d Rx ring %d disable timeout\n",
  3914. vsi->seid, pf_q);
  3915. return ret;
  3916. }
  3917. }
  3918. return 0;
  3919. }
  3920. /**
  3921. * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
  3922. * @pf: the PF
  3923. *
  3924. * This function waits for the queues to be in disabled state for all the
  3925. * VSIs that are managed by this PF.
  3926. **/
  3927. static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
  3928. {
  3929. int v, ret = 0;
  3930. for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
  3931. if (pf->vsi[v]) {
  3932. ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
  3933. if (ret)
  3934. break;
  3935. }
  3936. }
  3937. return ret;
  3938. }
  3939. #endif
  3940. /**
  3941. * i40e_detect_recover_hung_queue - Function to detect and recover hung_queue
  3942. * @q_idx: TX queue number
  3943. * @vsi: Pointer to VSI struct
  3944. *
  3945. * This function checks specified queue for given VSI. Detects hung condition.
  3946. * Sets hung bit since it is two step process. Before next run of service task
  3947. * if napi_poll runs, it reset 'hung' bit for respective q_vector. If not,
  3948. * hung condition remain unchanged and during subsequent run, this function
  3949. * issues SW interrupt to recover from hung condition.
  3950. **/
  3951. static void i40e_detect_recover_hung_queue(int q_idx, struct i40e_vsi *vsi)
  3952. {
  3953. struct i40e_ring *tx_ring = NULL;
  3954. struct i40e_pf *pf;
  3955. u32 head, val, tx_pending_hw;
  3956. int i;
  3957. pf = vsi->back;
  3958. /* now that we have an index, find the tx_ring struct */
  3959. for (i = 0; i < vsi->num_queue_pairs; i++) {
  3960. if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
  3961. if (q_idx == vsi->tx_rings[i]->queue_index) {
  3962. tx_ring = vsi->tx_rings[i];
  3963. break;
  3964. }
  3965. }
  3966. }
  3967. if (!tx_ring)
  3968. return;
  3969. /* Read interrupt register */
  3970. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  3971. val = rd32(&pf->hw,
  3972. I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
  3973. tx_ring->vsi->base_vector - 1));
  3974. else
  3975. val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
  3976. head = i40e_get_head(tx_ring);
  3977. tx_pending_hw = i40e_get_tx_pending(tx_ring, false);
  3978. /* HW is done executing descriptors, updated HEAD write back,
  3979. * but SW hasn't processed those descriptors. If interrupt is
  3980. * not generated from this point ON, it could result into
  3981. * dev_watchdog detecting timeout on those netdev_queue,
  3982. * hence proactively trigger SW interrupt.
  3983. */
  3984. if (tx_pending_hw && (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK))) {
  3985. /* NAPI Poll didn't run and clear since it was set */
  3986. if (test_and_clear_bit(I40E_Q_VECTOR_HUNG_DETECT,
  3987. &tx_ring->q_vector->hung_detected)) {
  3988. netdev_info(vsi->netdev, "VSI_seid %d, Hung TX queue %d, tx_pending_hw: %d, NTC:0x%x, HWB: 0x%x, NTU: 0x%x, TAIL: 0x%x\n",
  3989. vsi->seid, q_idx, tx_pending_hw,
  3990. tx_ring->next_to_clean, head,
  3991. tx_ring->next_to_use,
  3992. readl(tx_ring->tail));
  3993. netdev_info(vsi->netdev, "VSI_seid %d, Issuing force_wb for TX queue %d, Interrupt Reg: 0x%x\n",
  3994. vsi->seid, q_idx, val);
  3995. i40e_force_wb(vsi, tx_ring->q_vector);
  3996. } else {
  3997. /* First Chance - detected possible hung */
  3998. set_bit(I40E_Q_VECTOR_HUNG_DETECT,
  3999. &tx_ring->q_vector->hung_detected);
  4000. }
  4001. }
  4002. /* This is the case where we have interrupts missing,
  4003. * so the tx_pending in HW will most likely be 0, but we
  4004. * will have tx_pending in SW since the WB happened but the
  4005. * interrupt got lost.
  4006. */
  4007. if ((!tx_pending_hw) && i40e_get_tx_pending(tx_ring, true) &&
  4008. (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK))) {
  4009. local_bh_disable();
  4010. if (napi_reschedule(&tx_ring->q_vector->napi))
  4011. tx_ring->tx_stats.tx_lost_interrupt++;
  4012. local_bh_enable();
  4013. }
  4014. }
  4015. /**
  4016. * i40e_detect_recover_hung - Function to detect and recover hung_queues
  4017. * @pf: pointer to PF struct
  4018. *
  4019. * LAN VSI has netdev and netdev has TX queues. This function is to check
  4020. * each of those TX queues if they are hung, trigger recovery by issuing
  4021. * SW interrupt.
  4022. **/
  4023. static void i40e_detect_recover_hung(struct i40e_pf *pf)
  4024. {
  4025. struct net_device *netdev;
  4026. struct i40e_vsi *vsi;
  4027. int i;
  4028. /* Only for LAN VSI */
  4029. vsi = pf->vsi[pf->lan_vsi];
  4030. if (!vsi)
  4031. return;
  4032. /* Make sure, VSI state is not DOWN/RECOVERY_PENDING */
  4033. if (test_bit(__I40E_DOWN, &vsi->back->state) ||
  4034. test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
  4035. return;
  4036. /* Make sure type is MAIN VSI */
  4037. if (vsi->type != I40E_VSI_MAIN)
  4038. return;
  4039. netdev = vsi->netdev;
  4040. if (!netdev)
  4041. return;
  4042. /* Bail out if netif_carrier is not OK */
  4043. if (!netif_carrier_ok(netdev))
  4044. return;
  4045. /* Go thru' TX queues for netdev */
  4046. for (i = 0; i < netdev->num_tx_queues; i++) {
  4047. struct netdev_queue *q;
  4048. q = netdev_get_tx_queue(netdev, i);
  4049. if (q)
  4050. i40e_detect_recover_hung_queue(i, vsi);
  4051. }
  4052. }
  4053. /**
  4054. * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
  4055. * @pf: pointer to PF
  4056. *
  4057. * Get TC map for ISCSI PF type that will include iSCSI TC
  4058. * and LAN TC.
  4059. **/
  4060. static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
  4061. {
  4062. struct i40e_dcb_app_priority_table app;
  4063. struct i40e_hw *hw = &pf->hw;
  4064. u8 enabled_tc = 1; /* TC0 is always enabled */
  4065. u8 tc, i;
  4066. /* Get the iSCSI APP TLV */
  4067. struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
  4068. for (i = 0; i < dcbcfg->numapps; i++) {
  4069. app = dcbcfg->app[i];
  4070. if (app.selector == I40E_APP_SEL_TCPIP &&
  4071. app.protocolid == I40E_APP_PROTOID_ISCSI) {
  4072. tc = dcbcfg->etscfg.prioritytable[app.priority];
  4073. enabled_tc |= BIT(tc);
  4074. break;
  4075. }
  4076. }
  4077. return enabled_tc;
  4078. }
  4079. /**
  4080. * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
  4081. * @dcbcfg: the corresponding DCBx configuration structure
  4082. *
  4083. * Return the number of TCs from given DCBx configuration
  4084. **/
  4085. static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
  4086. {
  4087. int i, tc_unused = 0;
  4088. u8 num_tc = 0;
  4089. u8 ret = 0;
  4090. /* Scan the ETS Config Priority Table to find
  4091. * traffic class enabled for a given priority
  4092. * and create a bitmask of enabled TCs
  4093. */
  4094. for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
  4095. num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
  4096. /* Now scan the bitmask to check for
  4097. * contiguous TCs starting with TC0
  4098. */
  4099. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4100. if (num_tc & BIT(i)) {
  4101. if (!tc_unused) {
  4102. ret++;
  4103. } else {
  4104. pr_err("Non-contiguous TC - Disabling DCB\n");
  4105. return 1;
  4106. }
  4107. } else {
  4108. tc_unused = 1;
  4109. }
  4110. }
  4111. /* There is always at least TC0 */
  4112. if (!ret)
  4113. ret = 1;
  4114. return ret;
  4115. }
  4116. /**
  4117. * i40e_dcb_get_enabled_tc - Get enabled traffic classes
  4118. * @dcbcfg: the corresponding DCBx configuration structure
  4119. *
  4120. * Query the current DCB configuration and return the number of
  4121. * traffic classes enabled from the given DCBX config
  4122. **/
  4123. static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
  4124. {
  4125. u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
  4126. u8 enabled_tc = 1;
  4127. u8 i;
  4128. for (i = 0; i < num_tc; i++)
  4129. enabled_tc |= BIT(i);
  4130. return enabled_tc;
  4131. }
  4132. /**
  4133. * i40e_pf_get_num_tc - Get enabled traffic classes for PF
  4134. * @pf: PF being queried
  4135. *
  4136. * Return number of traffic classes enabled for the given PF
  4137. **/
  4138. static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
  4139. {
  4140. struct i40e_hw *hw = &pf->hw;
  4141. u8 i, enabled_tc = 1;
  4142. u8 num_tc = 0;
  4143. struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
  4144. /* If DCB is not enabled then always in single TC */
  4145. if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
  4146. return 1;
  4147. /* SFP mode will be enabled for all TCs on port */
  4148. if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
  4149. return i40e_dcb_get_num_tc(dcbcfg);
  4150. /* MFP mode return count of enabled TCs for this PF */
  4151. if (pf->hw.func_caps.iscsi)
  4152. enabled_tc = i40e_get_iscsi_tc_map(pf);
  4153. else
  4154. return 1; /* Only TC0 */
  4155. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4156. if (enabled_tc & BIT(i))
  4157. num_tc++;
  4158. }
  4159. return num_tc;
  4160. }
  4161. /**
  4162. * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
  4163. * @pf: PF being queried
  4164. *
  4165. * Return a bitmap for enabled traffic classes for this PF.
  4166. **/
  4167. static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
  4168. {
  4169. /* If DCB is not enabled for this PF then just return default TC */
  4170. if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
  4171. return I40E_DEFAULT_TRAFFIC_CLASS;
  4172. /* SFP mode we want PF to be enabled for all TCs */
  4173. if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
  4174. return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
  4175. /* MFP enabled and iSCSI PF type */
  4176. if (pf->hw.func_caps.iscsi)
  4177. return i40e_get_iscsi_tc_map(pf);
  4178. else
  4179. return I40E_DEFAULT_TRAFFIC_CLASS;
  4180. }
  4181. /**
  4182. * i40e_vsi_get_bw_info - Query VSI BW Information
  4183. * @vsi: the VSI being queried
  4184. *
  4185. * Returns 0 on success, negative value on failure
  4186. **/
  4187. static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
  4188. {
  4189. struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
  4190. struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
  4191. struct i40e_pf *pf = vsi->back;
  4192. struct i40e_hw *hw = &pf->hw;
  4193. i40e_status ret;
  4194. u32 tc_bw_max;
  4195. int i;
  4196. /* Get the VSI level BW configuration */
  4197. ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
  4198. if (ret) {
  4199. dev_info(&pf->pdev->dev,
  4200. "couldn't get PF vsi bw config, err %s aq_err %s\n",
  4201. i40e_stat_str(&pf->hw, ret),
  4202. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4203. return -EINVAL;
  4204. }
  4205. /* Get the VSI level BW configuration per TC */
  4206. ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
  4207. NULL);
  4208. if (ret) {
  4209. dev_info(&pf->pdev->dev,
  4210. "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
  4211. i40e_stat_str(&pf->hw, ret),
  4212. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4213. return -EINVAL;
  4214. }
  4215. if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
  4216. dev_info(&pf->pdev->dev,
  4217. "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
  4218. bw_config.tc_valid_bits,
  4219. bw_ets_config.tc_valid_bits);
  4220. /* Still continuing */
  4221. }
  4222. vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
  4223. vsi->bw_max_quanta = bw_config.max_bw;
  4224. tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
  4225. (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
  4226. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4227. vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
  4228. vsi->bw_ets_limit_credits[i] =
  4229. le16_to_cpu(bw_ets_config.credits[i]);
  4230. /* 3 bits out of 4 for each TC */
  4231. vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
  4232. }
  4233. return 0;
  4234. }
  4235. /**
  4236. * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
  4237. * @vsi: the VSI being configured
  4238. * @enabled_tc: TC bitmap
  4239. * @bw_credits: BW shared credits per TC
  4240. *
  4241. * Returns 0 on success, negative value on failure
  4242. **/
  4243. static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
  4244. u8 *bw_share)
  4245. {
  4246. struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
  4247. i40e_status ret;
  4248. int i;
  4249. bw_data.tc_valid_bits = enabled_tc;
  4250. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
  4251. bw_data.tc_bw_credits[i] = bw_share[i];
  4252. ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
  4253. NULL);
  4254. if (ret) {
  4255. dev_info(&vsi->back->pdev->dev,
  4256. "AQ command Config VSI BW allocation per TC failed = %d\n",
  4257. vsi->back->hw.aq.asq_last_status);
  4258. return -EINVAL;
  4259. }
  4260. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
  4261. vsi->info.qs_handle[i] = bw_data.qs_handles[i];
  4262. return 0;
  4263. }
  4264. /**
  4265. * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
  4266. * @vsi: the VSI being configured
  4267. * @enabled_tc: TC map to be enabled
  4268. *
  4269. **/
  4270. static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
  4271. {
  4272. struct net_device *netdev = vsi->netdev;
  4273. struct i40e_pf *pf = vsi->back;
  4274. struct i40e_hw *hw = &pf->hw;
  4275. u8 netdev_tc = 0;
  4276. int i;
  4277. struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
  4278. if (!netdev)
  4279. return;
  4280. if (!enabled_tc) {
  4281. netdev_reset_tc(netdev);
  4282. return;
  4283. }
  4284. /* Set up actual enabled TCs on the VSI */
  4285. if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
  4286. return;
  4287. /* set per TC queues for the VSI */
  4288. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4289. /* Only set TC queues for enabled tcs
  4290. *
  4291. * e.g. For a VSI that has TC0 and TC3 enabled the
  4292. * enabled_tc bitmap would be 0x00001001; the driver
  4293. * will set the numtc for netdev as 2 that will be
  4294. * referenced by the netdev layer as TC 0 and 1.
  4295. */
  4296. if (vsi->tc_config.enabled_tc & BIT(i))
  4297. netdev_set_tc_queue(netdev,
  4298. vsi->tc_config.tc_info[i].netdev_tc,
  4299. vsi->tc_config.tc_info[i].qcount,
  4300. vsi->tc_config.tc_info[i].qoffset);
  4301. }
  4302. /* Assign UP2TC map for the VSI */
  4303. for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
  4304. /* Get the actual TC# for the UP */
  4305. u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
  4306. /* Get the mapped netdev TC# for the UP */
  4307. netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
  4308. netdev_set_prio_tc_map(netdev, i, netdev_tc);
  4309. }
  4310. }
  4311. /**
  4312. * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
  4313. * @vsi: the VSI being configured
  4314. * @ctxt: the ctxt buffer returned from AQ VSI update param command
  4315. **/
  4316. static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
  4317. struct i40e_vsi_context *ctxt)
  4318. {
  4319. /* copy just the sections touched not the entire info
  4320. * since not all sections are valid as returned by
  4321. * update vsi params
  4322. */
  4323. vsi->info.mapping_flags = ctxt->info.mapping_flags;
  4324. memcpy(&vsi->info.queue_mapping,
  4325. &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
  4326. memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
  4327. sizeof(vsi->info.tc_mapping));
  4328. }
  4329. /**
  4330. * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
  4331. * @vsi: VSI to be configured
  4332. * @enabled_tc: TC bitmap
  4333. *
  4334. * This configures a particular VSI for TCs that are mapped to the
  4335. * given TC bitmap. It uses default bandwidth share for TCs across
  4336. * VSIs to configure TC for a particular VSI.
  4337. *
  4338. * NOTE:
  4339. * It is expected that the VSI queues have been quisced before calling
  4340. * this function.
  4341. **/
  4342. static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
  4343. {
  4344. u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
  4345. struct i40e_vsi_context ctxt;
  4346. int ret = 0;
  4347. int i;
  4348. /* Check if enabled_tc is same as existing or new TCs */
  4349. if (vsi->tc_config.enabled_tc == enabled_tc)
  4350. return ret;
  4351. /* Enable ETS TCs with equal BW Share for now across all VSIs */
  4352. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4353. if (enabled_tc & BIT(i))
  4354. bw_share[i] = 1;
  4355. }
  4356. ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
  4357. if (ret) {
  4358. dev_info(&vsi->back->pdev->dev,
  4359. "Failed configuring TC map %d for VSI %d\n",
  4360. enabled_tc, vsi->seid);
  4361. goto out;
  4362. }
  4363. /* Update Queue Pairs Mapping for currently enabled UPs */
  4364. ctxt.seid = vsi->seid;
  4365. ctxt.pf_num = vsi->back->hw.pf_id;
  4366. ctxt.vf_num = 0;
  4367. ctxt.uplink_seid = vsi->uplink_seid;
  4368. ctxt.info = vsi->info;
  4369. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
  4370. if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
  4371. ctxt.info.valid_sections |=
  4372. cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
  4373. ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
  4374. }
  4375. /* Update the VSI after updating the VSI queue-mapping information */
  4376. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  4377. if (ret) {
  4378. dev_info(&vsi->back->pdev->dev,
  4379. "Update vsi tc config failed, err %s aq_err %s\n",
  4380. i40e_stat_str(&vsi->back->hw, ret),
  4381. i40e_aq_str(&vsi->back->hw,
  4382. vsi->back->hw.aq.asq_last_status));
  4383. goto out;
  4384. }
  4385. /* update the local VSI info with updated queue map */
  4386. i40e_vsi_update_queue_map(vsi, &ctxt);
  4387. vsi->info.valid_sections = 0;
  4388. /* Update current VSI BW information */
  4389. ret = i40e_vsi_get_bw_info(vsi);
  4390. if (ret) {
  4391. dev_info(&vsi->back->pdev->dev,
  4392. "Failed updating vsi bw info, err %s aq_err %s\n",
  4393. i40e_stat_str(&vsi->back->hw, ret),
  4394. i40e_aq_str(&vsi->back->hw,
  4395. vsi->back->hw.aq.asq_last_status));
  4396. goto out;
  4397. }
  4398. /* Update the netdev TC setup */
  4399. i40e_vsi_config_netdev_tc(vsi, enabled_tc);
  4400. out:
  4401. return ret;
  4402. }
  4403. /**
  4404. * i40e_veb_config_tc - Configure TCs for given VEB
  4405. * @veb: given VEB
  4406. * @enabled_tc: TC bitmap
  4407. *
  4408. * Configures given TC bitmap for VEB (switching) element
  4409. **/
  4410. int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
  4411. {
  4412. struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
  4413. struct i40e_pf *pf = veb->pf;
  4414. int ret = 0;
  4415. int i;
  4416. /* No TCs or already enabled TCs just return */
  4417. if (!enabled_tc || veb->enabled_tc == enabled_tc)
  4418. return ret;
  4419. bw_data.tc_valid_bits = enabled_tc;
  4420. /* bw_data.absolute_credits is not set (relative) */
  4421. /* Enable ETS TCs with equal BW Share for now */
  4422. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4423. if (enabled_tc & BIT(i))
  4424. bw_data.tc_bw_share_credits[i] = 1;
  4425. }
  4426. ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
  4427. &bw_data, NULL);
  4428. if (ret) {
  4429. dev_info(&pf->pdev->dev,
  4430. "VEB bw config failed, err %s aq_err %s\n",
  4431. i40e_stat_str(&pf->hw, ret),
  4432. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4433. goto out;
  4434. }
  4435. /* Update the BW information */
  4436. ret = i40e_veb_get_bw_info(veb);
  4437. if (ret) {
  4438. dev_info(&pf->pdev->dev,
  4439. "Failed getting veb bw config, err %s aq_err %s\n",
  4440. i40e_stat_str(&pf->hw, ret),
  4441. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4442. }
  4443. out:
  4444. return ret;
  4445. }
  4446. #ifdef CONFIG_I40E_DCB
  4447. /**
  4448. * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
  4449. * @pf: PF struct
  4450. *
  4451. * Reconfigure VEB/VSIs on a given PF; it is assumed that
  4452. * the caller would've quiesce all the VSIs before calling
  4453. * this function
  4454. **/
  4455. static void i40e_dcb_reconfigure(struct i40e_pf *pf)
  4456. {
  4457. u8 tc_map = 0;
  4458. int ret;
  4459. u8 v;
  4460. /* Enable the TCs available on PF to all VEBs */
  4461. tc_map = i40e_pf_get_tc_map(pf);
  4462. for (v = 0; v < I40E_MAX_VEB; v++) {
  4463. if (!pf->veb[v])
  4464. continue;
  4465. ret = i40e_veb_config_tc(pf->veb[v], tc_map);
  4466. if (ret) {
  4467. dev_info(&pf->pdev->dev,
  4468. "Failed configuring TC for VEB seid=%d\n",
  4469. pf->veb[v]->seid);
  4470. /* Will try to configure as many components */
  4471. }
  4472. }
  4473. /* Update each VSI */
  4474. for (v = 0; v < pf->num_alloc_vsi; v++) {
  4475. if (!pf->vsi[v])
  4476. continue;
  4477. /* - Enable all TCs for the LAN VSI
  4478. * - For all others keep them at TC0 for now
  4479. */
  4480. if (v == pf->lan_vsi)
  4481. tc_map = i40e_pf_get_tc_map(pf);
  4482. else
  4483. tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
  4484. ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
  4485. if (ret) {
  4486. dev_info(&pf->pdev->dev,
  4487. "Failed configuring TC for VSI seid=%d\n",
  4488. pf->vsi[v]->seid);
  4489. /* Will try to configure as many components */
  4490. } else {
  4491. /* Re-configure VSI vectors based on updated TC map */
  4492. i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
  4493. if (pf->vsi[v]->netdev)
  4494. i40e_dcbnl_set_all(pf->vsi[v]);
  4495. }
  4496. }
  4497. }
  4498. /**
  4499. * i40e_resume_port_tx - Resume port Tx
  4500. * @pf: PF struct
  4501. *
  4502. * Resume a port's Tx and issue a PF reset in case of failure to
  4503. * resume.
  4504. **/
  4505. static int i40e_resume_port_tx(struct i40e_pf *pf)
  4506. {
  4507. struct i40e_hw *hw = &pf->hw;
  4508. int ret;
  4509. ret = i40e_aq_resume_port_tx(hw, NULL);
  4510. if (ret) {
  4511. dev_info(&pf->pdev->dev,
  4512. "Resume Port Tx failed, err %s aq_err %s\n",
  4513. i40e_stat_str(&pf->hw, ret),
  4514. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4515. /* Schedule PF reset to recover */
  4516. set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
  4517. i40e_service_event_schedule(pf);
  4518. }
  4519. return ret;
  4520. }
  4521. /**
  4522. * i40e_init_pf_dcb - Initialize DCB configuration
  4523. * @pf: PF being configured
  4524. *
  4525. * Query the current DCB configuration and cache it
  4526. * in the hardware structure
  4527. **/
  4528. static int i40e_init_pf_dcb(struct i40e_pf *pf)
  4529. {
  4530. struct i40e_hw *hw = &pf->hw;
  4531. int err = 0;
  4532. /* Do not enable DCB for SW1 and SW2 images even if the FW is capable */
  4533. if (pf->flags & I40E_FLAG_NO_DCB_SUPPORT)
  4534. goto out;
  4535. /* Get the initial DCB configuration */
  4536. err = i40e_init_dcb(hw);
  4537. if (!err) {
  4538. /* Device/Function is not DCBX capable */
  4539. if ((!hw->func_caps.dcb) ||
  4540. (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
  4541. dev_info(&pf->pdev->dev,
  4542. "DCBX offload is not supported or is disabled for this PF.\n");
  4543. } else {
  4544. /* When status is not DISABLED then DCBX in FW */
  4545. pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
  4546. DCB_CAP_DCBX_VER_IEEE;
  4547. pf->flags |= I40E_FLAG_DCB_CAPABLE;
  4548. /* Enable DCB tagging only when more than one TC
  4549. * or explicitly disable if only one TC
  4550. */
  4551. if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
  4552. pf->flags |= I40E_FLAG_DCB_ENABLED;
  4553. else
  4554. pf->flags &= ~I40E_FLAG_DCB_ENABLED;
  4555. dev_dbg(&pf->pdev->dev,
  4556. "DCBX offload is supported for this PF.\n");
  4557. }
  4558. } else {
  4559. dev_info(&pf->pdev->dev,
  4560. "Query for DCB configuration failed, err %s aq_err %s\n",
  4561. i40e_stat_str(&pf->hw, err),
  4562. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4563. }
  4564. out:
  4565. return err;
  4566. }
  4567. #endif /* CONFIG_I40E_DCB */
  4568. #define SPEED_SIZE 14
  4569. #define FC_SIZE 8
  4570. /**
  4571. * i40e_print_link_message - print link up or down
  4572. * @vsi: the VSI for which link needs a message
  4573. */
  4574. void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
  4575. {
  4576. enum i40e_aq_link_speed new_speed;
  4577. char *speed = "Unknown";
  4578. char *fc = "Unknown";
  4579. char *fec = "";
  4580. char *an = "";
  4581. new_speed = vsi->back->hw.phy.link_info.link_speed;
  4582. if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
  4583. return;
  4584. vsi->current_isup = isup;
  4585. vsi->current_speed = new_speed;
  4586. if (!isup) {
  4587. netdev_info(vsi->netdev, "NIC Link is Down\n");
  4588. return;
  4589. }
  4590. /* Warn user if link speed on NPAR enabled partition is not at
  4591. * least 10GB
  4592. */
  4593. if (vsi->back->hw.func_caps.npar_enable &&
  4594. (vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
  4595. vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
  4596. netdev_warn(vsi->netdev,
  4597. "The partition detected link speed that is less than 10Gbps\n");
  4598. switch (vsi->back->hw.phy.link_info.link_speed) {
  4599. case I40E_LINK_SPEED_40GB:
  4600. speed = "40 G";
  4601. break;
  4602. case I40E_LINK_SPEED_20GB:
  4603. speed = "20 G";
  4604. break;
  4605. case I40E_LINK_SPEED_25GB:
  4606. speed = "25 G";
  4607. break;
  4608. case I40E_LINK_SPEED_10GB:
  4609. speed = "10 G";
  4610. break;
  4611. case I40E_LINK_SPEED_1GB:
  4612. speed = "1000 M";
  4613. break;
  4614. case I40E_LINK_SPEED_100MB:
  4615. speed = "100 M";
  4616. break;
  4617. default:
  4618. break;
  4619. }
  4620. switch (vsi->back->hw.fc.current_mode) {
  4621. case I40E_FC_FULL:
  4622. fc = "RX/TX";
  4623. break;
  4624. case I40E_FC_TX_PAUSE:
  4625. fc = "TX";
  4626. break;
  4627. case I40E_FC_RX_PAUSE:
  4628. fc = "RX";
  4629. break;
  4630. default:
  4631. fc = "None";
  4632. break;
  4633. }
  4634. if (vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
  4635. fec = ", FEC: None";
  4636. an = ", Autoneg: False";
  4637. if (vsi->back->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
  4638. an = ", Autoneg: True";
  4639. if (vsi->back->hw.phy.link_info.fec_info &
  4640. I40E_AQ_CONFIG_FEC_KR_ENA)
  4641. fec = ", FEC: CL74 FC-FEC/BASE-R";
  4642. else if (vsi->back->hw.phy.link_info.fec_info &
  4643. I40E_AQ_CONFIG_FEC_RS_ENA)
  4644. fec = ", FEC: CL108 RS-FEC";
  4645. }
  4646. netdev_info(vsi->netdev, "NIC Link is Up, %sbps Full Duplex%s%s, Flow Control: %s\n",
  4647. speed, fec, an, fc);
  4648. }
  4649. /**
  4650. * i40e_up_complete - Finish the last steps of bringing up a connection
  4651. * @vsi: the VSI being configured
  4652. **/
  4653. static int i40e_up_complete(struct i40e_vsi *vsi)
  4654. {
  4655. struct i40e_pf *pf = vsi->back;
  4656. int err;
  4657. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  4658. i40e_vsi_configure_msix(vsi);
  4659. else
  4660. i40e_configure_msi_and_legacy(vsi);
  4661. /* start rings */
  4662. err = i40e_vsi_start_rings(vsi);
  4663. if (err)
  4664. return err;
  4665. clear_bit(__I40E_DOWN, &vsi->state);
  4666. i40e_napi_enable_all(vsi);
  4667. i40e_vsi_enable_irq(vsi);
  4668. if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
  4669. (vsi->netdev)) {
  4670. i40e_print_link_message(vsi, true);
  4671. netif_tx_start_all_queues(vsi->netdev);
  4672. netif_carrier_on(vsi->netdev);
  4673. } else if (vsi->netdev) {
  4674. i40e_print_link_message(vsi, false);
  4675. /* need to check for qualified module here*/
  4676. if ((pf->hw.phy.link_info.link_info &
  4677. I40E_AQ_MEDIA_AVAILABLE) &&
  4678. (!(pf->hw.phy.link_info.an_info &
  4679. I40E_AQ_QUALIFIED_MODULE)))
  4680. netdev_err(vsi->netdev,
  4681. "the driver failed to link because an unqualified module was detected.");
  4682. }
  4683. /* replay FDIR SB filters */
  4684. if (vsi->type == I40E_VSI_FDIR) {
  4685. /* reset fd counters */
  4686. pf->fd_add_err = 0;
  4687. pf->fd_atr_cnt = 0;
  4688. i40e_fdir_filter_restore(vsi);
  4689. }
  4690. /* On the next run of the service_task, notify any clients of the new
  4691. * opened netdev
  4692. */
  4693. pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
  4694. i40e_service_event_schedule(pf);
  4695. return 0;
  4696. }
  4697. /**
  4698. * i40e_vsi_reinit_locked - Reset the VSI
  4699. * @vsi: the VSI being configured
  4700. *
  4701. * Rebuild the ring structs after some configuration
  4702. * has changed, e.g. MTU size.
  4703. **/
  4704. static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
  4705. {
  4706. struct i40e_pf *pf = vsi->back;
  4707. WARN_ON(in_interrupt());
  4708. while (test_and_set_bit(__I40E_CONFIG_BUSY, &pf->state))
  4709. usleep_range(1000, 2000);
  4710. i40e_down(vsi);
  4711. i40e_up(vsi);
  4712. clear_bit(__I40E_CONFIG_BUSY, &pf->state);
  4713. }
  4714. /**
  4715. * i40e_up - Bring the connection back up after being down
  4716. * @vsi: the VSI being configured
  4717. **/
  4718. int i40e_up(struct i40e_vsi *vsi)
  4719. {
  4720. int err;
  4721. err = i40e_vsi_configure(vsi);
  4722. if (!err)
  4723. err = i40e_up_complete(vsi);
  4724. return err;
  4725. }
  4726. /**
  4727. * i40e_down - Shutdown the connection processing
  4728. * @vsi: the VSI being stopped
  4729. **/
  4730. void i40e_down(struct i40e_vsi *vsi)
  4731. {
  4732. int i;
  4733. /* It is assumed that the caller of this function
  4734. * sets the vsi->state __I40E_DOWN bit.
  4735. */
  4736. if (vsi->netdev) {
  4737. netif_carrier_off(vsi->netdev);
  4738. netif_tx_disable(vsi->netdev);
  4739. }
  4740. i40e_vsi_disable_irq(vsi);
  4741. i40e_vsi_stop_rings(vsi);
  4742. i40e_napi_disable_all(vsi);
  4743. for (i = 0; i < vsi->num_queue_pairs; i++) {
  4744. i40e_clean_tx_ring(vsi->tx_rings[i]);
  4745. i40e_clean_rx_ring(vsi->rx_rings[i]);
  4746. }
  4747. }
  4748. /**
  4749. * i40e_setup_tc - configure multiple traffic classes
  4750. * @netdev: net device to configure
  4751. * @tc: number of traffic classes to enable
  4752. **/
  4753. static int i40e_setup_tc(struct net_device *netdev, u8 tc)
  4754. {
  4755. struct i40e_netdev_priv *np = netdev_priv(netdev);
  4756. struct i40e_vsi *vsi = np->vsi;
  4757. struct i40e_pf *pf = vsi->back;
  4758. u8 enabled_tc = 0;
  4759. int ret = -EINVAL;
  4760. int i;
  4761. /* Check if DCB enabled to continue */
  4762. if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
  4763. netdev_info(netdev, "DCB is not enabled for adapter\n");
  4764. goto exit;
  4765. }
  4766. /* Check if MFP enabled */
  4767. if (pf->flags & I40E_FLAG_MFP_ENABLED) {
  4768. netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
  4769. goto exit;
  4770. }
  4771. /* Check whether tc count is within enabled limit */
  4772. if (tc > i40e_pf_get_num_tc(pf)) {
  4773. netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
  4774. goto exit;
  4775. }
  4776. /* Generate TC map for number of tc requested */
  4777. for (i = 0; i < tc; i++)
  4778. enabled_tc |= BIT(i);
  4779. /* Requesting same TC configuration as already enabled */
  4780. if (enabled_tc == vsi->tc_config.enabled_tc)
  4781. return 0;
  4782. /* Quiesce VSI queues */
  4783. i40e_quiesce_vsi(vsi);
  4784. /* Configure VSI for enabled TCs */
  4785. ret = i40e_vsi_config_tc(vsi, enabled_tc);
  4786. if (ret) {
  4787. netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
  4788. vsi->seid);
  4789. goto exit;
  4790. }
  4791. /* Unquiesce VSI */
  4792. i40e_unquiesce_vsi(vsi);
  4793. exit:
  4794. return ret;
  4795. }
  4796. static int __i40e_setup_tc(struct net_device *netdev, u32 handle, __be16 proto,
  4797. struct tc_to_netdev *tc)
  4798. {
  4799. if (tc->type != TC_SETUP_MQPRIO)
  4800. return -EINVAL;
  4801. tc->mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
  4802. return i40e_setup_tc(netdev, tc->mqprio->num_tc);
  4803. }
  4804. /**
  4805. * i40e_open - Called when a network interface is made active
  4806. * @netdev: network interface device structure
  4807. *
  4808. * The open entry point is called when a network interface is made
  4809. * active by the system (IFF_UP). At this point all resources needed
  4810. * for transmit and receive operations are allocated, the interrupt
  4811. * handler is registered with the OS, the netdev watchdog subtask is
  4812. * enabled, and the stack is notified that the interface is ready.
  4813. *
  4814. * Returns 0 on success, negative value on failure
  4815. **/
  4816. int i40e_open(struct net_device *netdev)
  4817. {
  4818. struct i40e_netdev_priv *np = netdev_priv(netdev);
  4819. struct i40e_vsi *vsi = np->vsi;
  4820. struct i40e_pf *pf = vsi->back;
  4821. int err;
  4822. /* disallow open during test or if eeprom is broken */
  4823. if (test_bit(__I40E_TESTING, &pf->state) ||
  4824. test_bit(__I40E_BAD_EEPROM, &pf->state))
  4825. return -EBUSY;
  4826. netif_carrier_off(netdev);
  4827. err = i40e_vsi_open(vsi);
  4828. if (err)
  4829. return err;
  4830. /* configure global TSO hardware offload settings */
  4831. wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
  4832. TCP_FLAG_FIN) >> 16);
  4833. wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
  4834. TCP_FLAG_FIN |
  4835. TCP_FLAG_CWR) >> 16);
  4836. wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
  4837. udp_tunnel_get_rx_info(netdev);
  4838. return 0;
  4839. }
  4840. /**
  4841. * i40e_vsi_open -
  4842. * @vsi: the VSI to open
  4843. *
  4844. * Finish initialization of the VSI.
  4845. *
  4846. * Returns 0 on success, negative value on failure
  4847. **/
  4848. int i40e_vsi_open(struct i40e_vsi *vsi)
  4849. {
  4850. struct i40e_pf *pf = vsi->back;
  4851. char int_name[I40E_INT_NAME_STR_LEN];
  4852. int err;
  4853. /* allocate descriptors */
  4854. err = i40e_vsi_setup_tx_resources(vsi);
  4855. if (err)
  4856. goto err_setup_tx;
  4857. err = i40e_vsi_setup_rx_resources(vsi);
  4858. if (err)
  4859. goto err_setup_rx;
  4860. err = i40e_vsi_configure(vsi);
  4861. if (err)
  4862. goto err_setup_rx;
  4863. if (vsi->netdev) {
  4864. snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
  4865. dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
  4866. err = i40e_vsi_request_irq(vsi, int_name);
  4867. if (err)
  4868. goto err_setup_rx;
  4869. /* Notify the stack of the actual queue counts. */
  4870. err = netif_set_real_num_tx_queues(vsi->netdev,
  4871. vsi->num_queue_pairs);
  4872. if (err)
  4873. goto err_set_queues;
  4874. err = netif_set_real_num_rx_queues(vsi->netdev,
  4875. vsi->num_queue_pairs);
  4876. if (err)
  4877. goto err_set_queues;
  4878. } else if (vsi->type == I40E_VSI_FDIR) {
  4879. snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
  4880. dev_driver_string(&pf->pdev->dev),
  4881. dev_name(&pf->pdev->dev));
  4882. err = i40e_vsi_request_irq(vsi, int_name);
  4883. } else {
  4884. err = -EINVAL;
  4885. goto err_setup_rx;
  4886. }
  4887. err = i40e_up_complete(vsi);
  4888. if (err)
  4889. goto err_up_complete;
  4890. return 0;
  4891. err_up_complete:
  4892. i40e_down(vsi);
  4893. err_set_queues:
  4894. i40e_vsi_free_irq(vsi);
  4895. err_setup_rx:
  4896. i40e_vsi_free_rx_resources(vsi);
  4897. err_setup_tx:
  4898. i40e_vsi_free_tx_resources(vsi);
  4899. if (vsi == pf->vsi[pf->lan_vsi])
  4900. i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
  4901. return err;
  4902. }
  4903. /**
  4904. * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
  4905. * @pf: Pointer to PF
  4906. *
  4907. * This function destroys the hlist where all the Flow Director
  4908. * filters were saved.
  4909. **/
  4910. static void i40e_fdir_filter_exit(struct i40e_pf *pf)
  4911. {
  4912. struct i40e_fdir_filter *filter;
  4913. struct i40e_flex_pit *pit_entry, *tmp;
  4914. struct hlist_node *node2;
  4915. hlist_for_each_entry_safe(filter, node2,
  4916. &pf->fdir_filter_list, fdir_node) {
  4917. hlist_del(&filter->fdir_node);
  4918. kfree(filter);
  4919. }
  4920. list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
  4921. list_del(&pit_entry->list);
  4922. kfree(pit_entry);
  4923. }
  4924. INIT_LIST_HEAD(&pf->l3_flex_pit_list);
  4925. list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
  4926. list_del(&pit_entry->list);
  4927. kfree(pit_entry);
  4928. }
  4929. INIT_LIST_HEAD(&pf->l4_flex_pit_list);
  4930. pf->fdir_pf_active_filters = 0;
  4931. pf->fd_tcp4_filter_cnt = 0;
  4932. pf->fd_udp4_filter_cnt = 0;
  4933. pf->fd_sctp4_filter_cnt = 0;
  4934. pf->fd_ip4_filter_cnt = 0;
  4935. /* Reprogram the default input set for TCP/IPv4 */
  4936. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
  4937. I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
  4938. I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
  4939. /* Reprogram the default input set for UDP/IPv4 */
  4940. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
  4941. I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
  4942. I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
  4943. /* Reprogram the default input set for SCTP/IPv4 */
  4944. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
  4945. I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
  4946. I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
  4947. /* Reprogram the default input set for Other/IPv4 */
  4948. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
  4949. I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
  4950. }
  4951. /**
  4952. * i40e_close - Disables a network interface
  4953. * @netdev: network interface device structure
  4954. *
  4955. * The close entry point is called when an interface is de-activated
  4956. * by the OS. The hardware is still under the driver's control, but
  4957. * this netdev interface is disabled.
  4958. *
  4959. * Returns 0, this is not allowed to fail
  4960. **/
  4961. int i40e_close(struct net_device *netdev)
  4962. {
  4963. struct i40e_netdev_priv *np = netdev_priv(netdev);
  4964. struct i40e_vsi *vsi = np->vsi;
  4965. i40e_vsi_close(vsi);
  4966. return 0;
  4967. }
  4968. /**
  4969. * i40e_do_reset - Start a PF or Core Reset sequence
  4970. * @pf: board private structure
  4971. * @reset_flags: which reset is requested
  4972. *
  4973. * The essential difference in resets is that the PF Reset
  4974. * doesn't clear the packet buffers, doesn't reset the PE
  4975. * firmware, and doesn't bother the other PFs on the chip.
  4976. **/
  4977. void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags)
  4978. {
  4979. u32 val;
  4980. WARN_ON(in_interrupt());
  4981. /* do the biggest reset indicated */
  4982. if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
  4983. /* Request a Global Reset
  4984. *
  4985. * This will start the chip's countdown to the actual full
  4986. * chip reset event, and a warning interrupt to be sent
  4987. * to all PFs, including the requestor. Our handler
  4988. * for the warning interrupt will deal with the shutdown
  4989. * and recovery of the switch setup.
  4990. */
  4991. dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
  4992. val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  4993. val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
  4994. wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
  4995. } else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
  4996. /* Request a Core Reset
  4997. *
  4998. * Same as Global Reset, except does *not* include the MAC/PHY
  4999. */
  5000. dev_dbg(&pf->pdev->dev, "CoreR requested\n");
  5001. val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  5002. val |= I40E_GLGEN_RTRIG_CORER_MASK;
  5003. wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
  5004. i40e_flush(&pf->hw);
  5005. } else if (reset_flags & BIT_ULL(__I40E_PF_RESET_REQUESTED)) {
  5006. /* Request a PF Reset
  5007. *
  5008. * Resets only the PF-specific registers
  5009. *
  5010. * This goes directly to the tear-down and rebuild of
  5011. * the switch, since we need to do all the recovery as
  5012. * for the Core Reset.
  5013. */
  5014. dev_dbg(&pf->pdev->dev, "PFR requested\n");
  5015. i40e_handle_reset_warning(pf);
  5016. } else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
  5017. int v;
  5018. /* Find the VSI(s) that requested a re-init */
  5019. dev_info(&pf->pdev->dev,
  5020. "VSI reinit requested\n");
  5021. for (v = 0; v < pf->num_alloc_vsi; v++) {
  5022. struct i40e_vsi *vsi = pf->vsi[v];
  5023. if (vsi != NULL &&
  5024. test_bit(__I40E_REINIT_REQUESTED, &vsi->state)) {
  5025. i40e_vsi_reinit_locked(pf->vsi[v]);
  5026. clear_bit(__I40E_REINIT_REQUESTED, &vsi->state);
  5027. }
  5028. }
  5029. } else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
  5030. int v;
  5031. /* Find the VSI(s) that needs to be brought down */
  5032. dev_info(&pf->pdev->dev, "VSI down requested\n");
  5033. for (v = 0; v < pf->num_alloc_vsi; v++) {
  5034. struct i40e_vsi *vsi = pf->vsi[v];
  5035. if (vsi != NULL &&
  5036. test_bit(__I40E_DOWN_REQUESTED, &vsi->state)) {
  5037. set_bit(__I40E_DOWN, &vsi->state);
  5038. i40e_down(vsi);
  5039. clear_bit(__I40E_DOWN_REQUESTED, &vsi->state);
  5040. }
  5041. }
  5042. } else {
  5043. dev_info(&pf->pdev->dev,
  5044. "bad reset request 0x%08x\n", reset_flags);
  5045. }
  5046. }
  5047. #ifdef CONFIG_I40E_DCB
  5048. /**
  5049. * i40e_dcb_need_reconfig - Check if DCB needs reconfig
  5050. * @pf: board private structure
  5051. * @old_cfg: current DCB config
  5052. * @new_cfg: new DCB config
  5053. **/
  5054. bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
  5055. struct i40e_dcbx_config *old_cfg,
  5056. struct i40e_dcbx_config *new_cfg)
  5057. {
  5058. bool need_reconfig = false;
  5059. /* Check if ETS configuration has changed */
  5060. if (memcmp(&new_cfg->etscfg,
  5061. &old_cfg->etscfg,
  5062. sizeof(new_cfg->etscfg))) {
  5063. /* If Priority Table has changed reconfig is needed */
  5064. if (memcmp(&new_cfg->etscfg.prioritytable,
  5065. &old_cfg->etscfg.prioritytable,
  5066. sizeof(new_cfg->etscfg.prioritytable))) {
  5067. need_reconfig = true;
  5068. dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
  5069. }
  5070. if (memcmp(&new_cfg->etscfg.tcbwtable,
  5071. &old_cfg->etscfg.tcbwtable,
  5072. sizeof(new_cfg->etscfg.tcbwtable)))
  5073. dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
  5074. if (memcmp(&new_cfg->etscfg.tsatable,
  5075. &old_cfg->etscfg.tsatable,
  5076. sizeof(new_cfg->etscfg.tsatable)))
  5077. dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
  5078. }
  5079. /* Check if PFC configuration has changed */
  5080. if (memcmp(&new_cfg->pfc,
  5081. &old_cfg->pfc,
  5082. sizeof(new_cfg->pfc))) {
  5083. need_reconfig = true;
  5084. dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
  5085. }
  5086. /* Check if APP Table has changed */
  5087. if (memcmp(&new_cfg->app,
  5088. &old_cfg->app,
  5089. sizeof(new_cfg->app))) {
  5090. need_reconfig = true;
  5091. dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
  5092. }
  5093. dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
  5094. return need_reconfig;
  5095. }
  5096. /**
  5097. * i40e_handle_lldp_event - Handle LLDP Change MIB event
  5098. * @pf: board private structure
  5099. * @e: event info posted on ARQ
  5100. **/
  5101. static int i40e_handle_lldp_event(struct i40e_pf *pf,
  5102. struct i40e_arq_event_info *e)
  5103. {
  5104. struct i40e_aqc_lldp_get_mib *mib =
  5105. (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
  5106. struct i40e_hw *hw = &pf->hw;
  5107. struct i40e_dcbx_config tmp_dcbx_cfg;
  5108. bool need_reconfig = false;
  5109. int ret = 0;
  5110. u8 type;
  5111. /* Not DCB capable or capability disabled */
  5112. if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
  5113. return ret;
  5114. /* Ignore if event is not for Nearest Bridge */
  5115. type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
  5116. & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
  5117. dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
  5118. if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
  5119. return ret;
  5120. /* Check MIB Type and return if event for Remote MIB update */
  5121. type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
  5122. dev_dbg(&pf->pdev->dev,
  5123. "LLDP event mib type %s\n", type ? "remote" : "local");
  5124. if (type == I40E_AQ_LLDP_MIB_REMOTE) {
  5125. /* Update the remote cached instance and return */
  5126. ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
  5127. I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
  5128. &hw->remote_dcbx_config);
  5129. goto exit;
  5130. }
  5131. /* Store the old configuration */
  5132. tmp_dcbx_cfg = hw->local_dcbx_config;
  5133. /* Reset the old DCBx configuration data */
  5134. memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
  5135. /* Get updated DCBX data from firmware */
  5136. ret = i40e_get_dcb_config(&pf->hw);
  5137. if (ret) {
  5138. dev_info(&pf->pdev->dev,
  5139. "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
  5140. i40e_stat_str(&pf->hw, ret),
  5141. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5142. goto exit;
  5143. }
  5144. /* No change detected in DCBX configs */
  5145. if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
  5146. sizeof(tmp_dcbx_cfg))) {
  5147. dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
  5148. goto exit;
  5149. }
  5150. need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
  5151. &hw->local_dcbx_config);
  5152. i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
  5153. if (!need_reconfig)
  5154. goto exit;
  5155. /* Enable DCB tagging only when more than one TC */
  5156. if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
  5157. pf->flags |= I40E_FLAG_DCB_ENABLED;
  5158. else
  5159. pf->flags &= ~I40E_FLAG_DCB_ENABLED;
  5160. set_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
  5161. /* Reconfiguration needed quiesce all VSIs */
  5162. i40e_pf_quiesce_all_vsi(pf);
  5163. /* Changes in configuration update VEB/VSI */
  5164. i40e_dcb_reconfigure(pf);
  5165. ret = i40e_resume_port_tx(pf);
  5166. clear_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
  5167. /* In case of error no point in resuming VSIs */
  5168. if (ret)
  5169. goto exit;
  5170. /* Wait for the PF's queues to be disabled */
  5171. ret = i40e_pf_wait_queues_disabled(pf);
  5172. if (ret) {
  5173. /* Schedule PF reset to recover */
  5174. set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
  5175. i40e_service_event_schedule(pf);
  5176. } else {
  5177. i40e_pf_unquiesce_all_vsi(pf);
  5178. pf->flags |= (I40E_FLAG_SERVICE_CLIENT_REQUESTED |
  5179. I40E_FLAG_CLIENT_L2_CHANGE);
  5180. }
  5181. exit:
  5182. return ret;
  5183. }
  5184. #endif /* CONFIG_I40E_DCB */
  5185. /**
  5186. * i40e_do_reset_safe - Protected reset path for userland calls.
  5187. * @pf: board private structure
  5188. * @reset_flags: which reset is requested
  5189. *
  5190. **/
  5191. void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
  5192. {
  5193. rtnl_lock();
  5194. i40e_do_reset(pf, reset_flags);
  5195. rtnl_unlock();
  5196. }
  5197. /**
  5198. * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
  5199. * @pf: board private structure
  5200. * @e: event info posted on ARQ
  5201. *
  5202. * Handler for LAN Queue Overflow Event generated by the firmware for PF
  5203. * and VF queues
  5204. **/
  5205. static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
  5206. struct i40e_arq_event_info *e)
  5207. {
  5208. struct i40e_aqc_lan_overflow *data =
  5209. (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
  5210. u32 queue = le32_to_cpu(data->prtdcb_rupto);
  5211. u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
  5212. struct i40e_hw *hw = &pf->hw;
  5213. struct i40e_vf *vf;
  5214. u16 vf_id;
  5215. dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
  5216. queue, qtx_ctl);
  5217. /* Queue belongs to VF, find the VF and issue VF reset */
  5218. if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
  5219. >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
  5220. vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
  5221. >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
  5222. vf_id -= hw->func_caps.vf_base_id;
  5223. vf = &pf->vf[vf_id];
  5224. i40e_vc_notify_vf_reset(vf);
  5225. /* Allow VF to process pending reset notification */
  5226. msleep(20);
  5227. i40e_reset_vf(vf, false);
  5228. }
  5229. }
  5230. /**
  5231. * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
  5232. * @pf: board private structure
  5233. **/
  5234. u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
  5235. {
  5236. u32 val, fcnt_prog;
  5237. val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
  5238. fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
  5239. return fcnt_prog;
  5240. }
  5241. /**
  5242. * i40e_get_current_fd_count - Get total FD filters programmed for this PF
  5243. * @pf: board private structure
  5244. **/
  5245. u32 i40e_get_current_fd_count(struct i40e_pf *pf)
  5246. {
  5247. u32 val, fcnt_prog;
  5248. val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
  5249. fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
  5250. ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
  5251. I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
  5252. return fcnt_prog;
  5253. }
  5254. /**
  5255. * i40e_get_global_fd_count - Get total FD filters programmed on device
  5256. * @pf: board private structure
  5257. **/
  5258. u32 i40e_get_global_fd_count(struct i40e_pf *pf)
  5259. {
  5260. u32 val, fcnt_prog;
  5261. val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
  5262. fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
  5263. ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
  5264. I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
  5265. return fcnt_prog;
  5266. }
  5267. /**
  5268. * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
  5269. * @pf: board private structure
  5270. **/
  5271. void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
  5272. {
  5273. struct i40e_fdir_filter *filter;
  5274. u32 fcnt_prog, fcnt_avail;
  5275. struct hlist_node *node;
  5276. if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
  5277. return;
  5278. /* Check if, FD SB or ATR was auto disabled and if there is enough room
  5279. * to re-enable
  5280. */
  5281. fcnt_prog = i40e_get_global_fd_count(pf);
  5282. fcnt_avail = pf->fdir_pf_filter_count;
  5283. if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
  5284. (pf->fd_add_err == 0) ||
  5285. (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt)) {
  5286. if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
  5287. (pf->hw_disabled_flags & I40E_FLAG_FD_SB_ENABLED)) {
  5288. pf->hw_disabled_flags &= ~I40E_FLAG_FD_SB_ENABLED;
  5289. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  5290. dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
  5291. }
  5292. }
  5293. /* Wait for some more space to be available to turn on ATR. We also
  5294. * must check that no existing ntuple rules for TCP are in effect
  5295. */
  5296. if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM * 2)) {
  5297. if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
  5298. (pf->hw_disabled_flags & I40E_FLAG_FD_ATR_ENABLED) &&
  5299. (pf->fd_tcp4_filter_cnt == 0)) {
  5300. pf->hw_disabled_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
  5301. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  5302. dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
  5303. }
  5304. }
  5305. /* if hw had a problem adding a filter, delete it */
  5306. if (pf->fd_inv > 0) {
  5307. hlist_for_each_entry_safe(filter, node,
  5308. &pf->fdir_filter_list, fdir_node) {
  5309. if (filter->fd_id == pf->fd_inv) {
  5310. hlist_del(&filter->fdir_node);
  5311. kfree(filter);
  5312. pf->fdir_pf_active_filters--;
  5313. }
  5314. }
  5315. }
  5316. }
  5317. #define I40E_MIN_FD_FLUSH_INTERVAL 10
  5318. #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
  5319. /**
  5320. * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
  5321. * @pf: board private structure
  5322. **/
  5323. static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
  5324. {
  5325. unsigned long min_flush_time;
  5326. int flush_wait_retry = 50;
  5327. bool disable_atr = false;
  5328. int fd_room;
  5329. int reg;
  5330. if (!time_after(jiffies, pf->fd_flush_timestamp +
  5331. (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
  5332. return;
  5333. /* If the flush is happening too quick and we have mostly SB rules we
  5334. * should not re-enable ATR for some time.
  5335. */
  5336. min_flush_time = pf->fd_flush_timestamp +
  5337. (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
  5338. fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
  5339. if (!(time_after(jiffies, min_flush_time)) &&
  5340. (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
  5341. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  5342. dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
  5343. disable_atr = true;
  5344. }
  5345. pf->fd_flush_timestamp = jiffies;
  5346. pf->hw_disabled_flags |= I40E_FLAG_FD_ATR_ENABLED;
  5347. /* flush all filters */
  5348. wr32(&pf->hw, I40E_PFQF_CTL_1,
  5349. I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
  5350. i40e_flush(&pf->hw);
  5351. pf->fd_flush_cnt++;
  5352. pf->fd_add_err = 0;
  5353. do {
  5354. /* Check FD flush status every 5-6msec */
  5355. usleep_range(5000, 6000);
  5356. reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
  5357. if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
  5358. break;
  5359. } while (flush_wait_retry--);
  5360. if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
  5361. dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
  5362. } else {
  5363. /* replay sideband filters */
  5364. i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
  5365. if (!disable_atr && !pf->fd_tcp4_filter_cnt)
  5366. pf->hw_disabled_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
  5367. clear_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state);
  5368. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  5369. dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
  5370. }
  5371. }
  5372. /**
  5373. * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
  5374. * @pf: board private structure
  5375. **/
  5376. u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
  5377. {
  5378. return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
  5379. }
  5380. /* We can see up to 256 filter programming desc in transit if the filters are
  5381. * being applied really fast; before we see the first
  5382. * filter miss error on Rx queue 0. Accumulating enough error messages before
  5383. * reacting will make sure we don't cause flush too often.
  5384. */
  5385. #define I40E_MAX_FD_PROGRAM_ERROR 256
  5386. /**
  5387. * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
  5388. * @pf: board private structure
  5389. **/
  5390. static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
  5391. {
  5392. /* if interface is down do nothing */
  5393. if (test_bit(__I40E_DOWN, &pf->state))
  5394. return;
  5395. if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
  5396. i40e_fdir_flush_and_replay(pf);
  5397. i40e_fdir_check_and_reenable(pf);
  5398. }
  5399. /**
  5400. * i40e_vsi_link_event - notify VSI of a link event
  5401. * @vsi: vsi to be notified
  5402. * @link_up: link up or down
  5403. **/
  5404. static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
  5405. {
  5406. if (!vsi || test_bit(__I40E_DOWN, &vsi->state))
  5407. return;
  5408. switch (vsi->type) {
  5409. case I40E_VSI_MAIN:
  5410. if (!vsi->netdev || !vsi->netdev_registered)
  5411. break;
  5412. if (link_up) {
  5413. netif_carrier_on(vsi->netdev);
  5414. netif_tx_wake_all_queues(vsi->netdev);
  5415. } else {
  5416. netif_carrier_off(vsi->netdev);
  5417. netif_tx_stop_all_queues(vsi->netdev);
  5418. }
  5419. break;
  5420. case I40E_VSI_SRIOV:
  5421. case I40E_VSI_VMDQ2:
  5422. case I40E_VSI_CTRL:
  5423. case I40E_VSI_IWARP:
  5424. case I40E_VSI_MIRROR:
  5425. default:
  5426. /* there is no notification for other VSIs */
  5427. break;
  5428. }
  5429. }
  5430. /**
  5431. * i40e_veb_link_event - notify elements on the veb of a link event
  5432. * @veb: veb to be notified
  5433. * @link_up: link up or down
  5434. **/
  5435. static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
  5436. {
  5437. struct i40e_pf *pf;
  5438. int i;
  5439. if (!veb || !veb->pf)
  5440. return;
  5441. pf = veb->pf;
  5442. /* depth first... */
  5443. for (i = 0; i < I40E_MAX_VEB; i++)
  5444. if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
  5445. i40e_veb_link_event(pf->veb[i], link_up);
  5446. /* ... now the local VSIs */
  5447. for (i = 0; i < pf->num_alloc_vsi; i++)
  5448. if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
  5449. i40e_vsi_link_event(pf->vsi[i], link_up);
  5450. }
  5451. /**
  5452. * i40e_link_event - Update netif_carrier status
  5453. * @pf: board private structure
  5454. **/
  5455. static void i40e_link_event(struct i40e_pf *pf)
  5456. {
  5457. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  5458. u8 new_link_speed, old_link_speed;
  5459. i40e_status status;
  5460. bool new_link, old_link;
  5461. /* save off old link status information */
  5462. pf->hw.phy.link_info_old = pf->hw.phy.link_info;
  5463. /* set this to force the get_link_status call to refresh state */
  5464. pf->hw.phy.get_link_info = true;
  5465. old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
  5466. status = i40e_get_link_status(&pf->hw, &new_link);
  5467. /* On success, disable temp link polling */
  5468. if (status == I40E_SUCCESS) {
  5469. if (pf->flags & I40E_FLAG_TEMP_LINK_POLLING)
  5470. pf->flags &= ~I40E_FLAG_TEMP_LINK_POLLING;
  5471. } else {
  5472. /* Enable link polling temporarily until i40e_get_link_status
  5473. * returns I40E_SUCCESS
  5474. */
  5475. pf->flags |= I40E_FLAG_TEMP_LINK_POLLING;
  5476. dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
  5477. status);
  5478. return;
  5479. }
  5480. old_link_speed = pf->hw.phy.link_info_old.link_speed;
  5481. new_link_speed = pf->hw.phy.link_info.link_speed;
  5482. if (new_link == old_link &&
  5483. new_link_speed == old_link_speed &&
  5484. (test_bit(__I40E_DOWN, &vsi->state) ||
  5485. new_link == netif_carrier_ok(vsi->netdev)))
  5486. return;
  5487. if (!test_bit(__I40E_DOWN, &vsi->state))
  5488. i40e_print_link_message(vsi, new_link);
  5489. /* Notify the base of the switch tree connected to
  5490. * the link. Floating VEBs are not notified.
  5491. */
  5492. if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
  5493. i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
  5494. else
  5495. i40e_vsi_link_event(vsi, new_link);
  5496. if (pf->vf)
  5497. i40e_vc_notify_link_state(pf);
  5498. if (pf->flags & I40E_FLAG_PTP)
  5499. i40e_ptp_set_increment(pf);
  5500. }
  5501. /**
  5502. * i40e_watchdog_subtask - periodic checks not using event driven response
  5503. * @pf: board private structure
  5504. **/
  5505. static void i40e_watchdog_subtask(struct i40e_pf *pf)
  5506. {
  5507. int i;
  5508. /* if interface is down do nothing */
  5509. if (test_bit(__I40E_DOWN, &pf->state) ||
  5510. test_bit(__I40E_CONFIG_BUSY, &pf->state))
  5511. return;
  5512. /* make sure we don't do these things too often */
  5513. if (time_before(jiffies, (pf->service_timer_previous +
  5514. pf->service_timer_period)))
  5515. return;
  5516. pf->service_timer_previous = jiffies;
  5517. if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
  5518. (pf->flags & I40E_FLAG_TEMP_LINK_POLLING))
  5519. i40e_link_event(pf);
  5520. /* Update the stats for active netdevs so the network stack
  5521. * can look at updated numbers whenever it cares to
  5522. */
  5523. for (i = 0; i < pf->num_alloc_vsi; i++)
  5524. if (pf->vsi[i] && pf->vsi[i]->netdev)
  5525. i40e_update_stats(pf->vsi[i]);
  5526. if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
  5527. /* Update the stats for the active switching components */
  5528. for (i = 0; i < I40E_MAX_VEB; i++)
  5529. if (pf->veb[i])
  5530. i40e_update_veb_stats(pf->veb[i]);
  5531. }
  5532. i40e_ptp_rx_hang(pf->vsi[pf->lan_vsi]);
  5533. }
  5534. /**
  5535. * i40e_reset_subtask - Set up for resetting the device and driver
  5536. * @pf: board private structure
  5537. **/
  5538. static void i40e_reset_subtask(struct i40e_pf *pf)
  5539. {
  5540. u32 reset_flags = 0;
  5541. rtnl_lock();
  5542. if (test_bit(__I40E_REINIT_REQUESTED, &pf->state)) {
  5543. reset_flags |= BIT(__I40E_REINIT_REQUESTED);
  5544. clear_bit(__I40E_REINIT_REQUESTED, &pf->state);
  5545. }
  5546. if (test_bit(__I40E_PF_RESET_REQUESTED, &pf->state)) {
  5547. reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
  5548. clear_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
  5549. }
  5550. if (test_bit(__I40E_CORE_RESET_REQUESTED, &pf->state)) {
  5551. reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
  5552. clear_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
  5553. }
  5554. if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state)) {
  5555. reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
  5556. clear_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
  5557. }
  5558. if (test_bit(__I40E_DOWN_REQUESTED, &pf->state)) {
  5559. reset_flags |= BIT(__I40E_DOWN_REQUESTED);
  5560. clear_bit(__I40E_DOWN_REQUESTED, &pf->state);
  5561. }
  5562. /* If there's a recovery already waiting, it takes
  5563. * precedence before starting a new reset sequence.
  5564. */
  5565. if (test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state)) {
  5566. i40e_handle_reset_warning(pf);
  5567. goto unlock;
  5568. }
  5569. /* If we're already down or resetting, just bail */
  5570. if (reset_flags &&
  5571. !test_bit(__I40E_DOWN, &pf->state) &&
  5572. !test_bit(__I40E_CONFIG_BUSY, &pf->state))
  5573. i40e_do_reset(pf, reset_flags);
  5574. unlock:
  5575. rtnl_unlock();
  5576. }
  5577. /**
  5578. * i40e_handle_link_event - Handle link event
  5579. * @pf: board private structure
  5580. * @e: event info posted on ARQ
  5581. **/
  5582. static void i40e_handle_link_event(struct i40e_pf *pf,
  5583. struct i40e_arq_event_info *e)
  5584. {
  5585. struct i40e_aqc_get_link_status *status =
  5586. (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
  5587. /* Do a new status request to re-enable LSE reporting
  5588. * and load new status information into the hw struct
  5589. * This completely ignores any state information
  5590. * in the ARQ event info, instead choosing to always
  5591. * issue the AQ update link status command.
  5592. */
  5593. i40e_link_event(pf);
  5594. /* check for unqualified module, if link is down */
  5595. if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
  5596. (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
  5597. (!(status->link_info & I40E_AQ_LINK_UP)))
  5598. dev_err(&pf->pdev->dev,
  5599. "The driver failed to link because an unqualified module was detected.\n");
  5600. }
  5601. /**
  5602. * i40e_clean_adminq_subtask - Clean the AdminQ rings
  5603. * @pf: board private structure
  5604. **/
  5605. static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
  5606. {
  5607. struct i40e_arq_event_info event;
  5608. struct i40e_hw *hw = &pf->hw;
  5609. u16 pending, i = 0;
  5610. i40e_status ret;
  5611. u16 opcode;
  5612. u32 oldval;
  5613. u32 val;
  5614. /* Do not run clean AQ when PF reset fails */
  5615. if (test_bit(__I40E_RESET_FAILED, &pf->state))
  5616. return;
  5617. /* check for error indications */
  5618. val = rd32(&pf->hw, pf->hw.aq.arq.len);
  5619. oldval = val;
  5620. if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
  5621. if (hw->debug_mask & I40E_DEBUG_AQ)
  5622. dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
  5623. val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
  5624. }
  5625. if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
  5626. if (hw->debug_mask & I40E_DEBUG_AQ)
  5627. dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
  5628. val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
  5629. pf->arq_overflows++;
  5630. }
  5631. if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
  5632. if (hw->debug_mask & I40E_DEBUG_AQ)
  5633. dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
  5634. val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
  5635. }
  5636. if (oldval != val)
  5637. wr32(&pf->hw, pf->hw.aq.arq.len, val);
  5638. val = rd32(&pf->hw, pf->hw.aq.asq.len);
  5639. oldval = val;
  5640. if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
  5641. if (pf->hw.debug_mask & I40E_DEBUG_AQ)
  5642. dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
  5643. val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
  5644. }
  5645. if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
  5646. if (pf->hw.debug_mask & I40E_DEBUG_AQ)
  5647. dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
  5648. val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
  5649. }
  5650. if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
  5651. if (pf->hw.debug_mask & I40E_DEBUG_AQ)
  5652. dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
  5653. val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
  5654. }
  5655. if (oldval != val)
  5656. wr32(&pf->hw, pf->hw.aq.asq.len, val);
  5657. event.buf_len = I40E_MAX_AQ_BUF_SIZE;
  5658. event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
  5659. if (!event.msg_buf)
  5660. return;
  5661. do {
  5662. ret = i40e_clean_arq_element(hw, &event, &pending);
  5663. if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
  5664. break;
  5665. else if (ret) {
  5666. dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
  5667. break;
  5668. }
  5669. opcode = le16_to_cpu(event.desc.opcode);
  5670. switch (opcode) {
  5671. case i40e_aqc_opc_get_link_status:
  5672. i40e_handle_link_event(pf, &event);
  5673. break;
  5674. case i40e_aqc_opc_send_msg_to_pf:
  5675. ret = i40e_vc_process_vf_msg(pf,
  5676. le16_to_cpu(event.desc.retval),
  5677. le32_to_cpu(event.desc.cookie_high),
  5678. le32_to_cpu(event.desc.cookie_low),
  5679. event.msg_buf,
  5680. event.msg_len);
  5681. break;
  5682. case i40e_aqc_opc_lldp_update_mib:
  5683. dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
  5684. #ifdef CONFIG_I40E_DCB
  5685. rtnl_lock();
  5686. ret = i40e_handle_lldp_event(pf, &event);
  5687. rtnl_unlock();
  5688. #endif /* CONFIG_I40E_DCB */
  5689. break;
  5690. case i40e_aqc_opc_event_lan_overflow:
  5691. dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
  5692. i40e_handle_lan_overflow_event(pf, &event);
  5693. break;
  5694. case i40e_aqc_opc_send_msg_to_peer:
  5695. dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
  5696. break;
  5697. case i40e_aqc_opc_nvm_erase:
  5698. case i40e_aqc_opc_nvm_update:
  5699. case i40e_aqc_opc_oem_post_update:
  5700. i40e_debug(&pf->hw, I40E_DEBUG_NVM,
  5701. "ARQ NVM operation 0x%04x completed\n",
  5702. opcode);
  5703. break;
  5704. default:
  5705. dev_info(&pf->pdev->dev,
  5706. "ARQ: Unknown event 0x%04x ignored\n",
  5707. opcode);
  5708. break;
  5709. }
  5710. } while (i++ < pf->adminq_work_limit);
  5711. if (i < pf->adminq_work_limit)
  5712. clear_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
  5713. /* re-enable Admin queue interrupt cause */
  5714. val = rd32(hw, I40E_PFINT_ICR0_ENA);
  5715. val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
  5716. wr32(hw, I40E_PFINT_ICR0_ENA, val);
  5717. i40e_flush(hw);
  5718. kfree(event.msg_buf);
  5719. }
  5720. /**
  5721. * i40e_verify_eeprom - make sure eeprom is good to use
  5722. * @pf: board private structure
  5723. **/
  5724. static void i40e_verify_eeprom(struct i40e_pf *pf)
  5725. {
  5726. int err;
  5727. err = i40e_diag_eeprom_test(&pf->hw);
  5728. if (err) {
  5729. /* retry in case of garbage read */
  5730. err = i40e_diag_eeprom_test(&pf->hw);
  5731. if (err) {
  5732. dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
  5733. err);
  5734. set_bit(__I40E_BAD_EEPROM, &pf->state);
  5735. }
  5736. }
  5737. if (!err && test_bit(__I40E_BAD_EEPROM, &pf->state)) {
  5738. dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
  5739. clear_bit(__I40E_BAD_EEPROM, &pf->state);
  5740. }
  5741. }
  5742. /**
  5743. * i40e_enable_pf_switch_lb
  5744. * @pf: pointer to the PF structure
  5745. *
  5746. * enable switch loop back or die - no point in a return value
  5747. **/
  5748. static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
  5749. {
  5750. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  5751. struct i40e_vsi_context ctxt;
  5752. int ret;
  5753. ctxt.seid = pf->main_vsi_seid;
  5754. ctxt.pf_num = pf->hw.pf_id;
  5755. ctxt.vf_num = 0;
  5756. ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
  5757. if (ret) {
  5758. dev_info(&pf->pdev->dev,
  5759. "couldn't get PF vsi config, err %s aq_err %s\n",
  5760. i40e_stat_str(&pf->hw, ret),
  5761. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5762. return;
  5763. }
  5764. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  5765. ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  5766. ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  5767. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  5768. if (ret) {
  5769. dev_info(&pf->pdev->dev,
  5770. "update vsi switch failed, err %s aq_err %s\n",
  5771. i40e_stat_str(&pf->hw, ret),
  5772. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5773. }
  5774. }
  5775. /**
  5776. * i40e_disable_pf_switch_lb
  5777. * @pf: pointer to the PF structure
  5778. *
  5779. * disable switch loop back or die - no point in a return value
  5780. **/
  5781. static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
  5782. {
  5783. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  5784. struct i40e_vsi_context ctxt;
  5785. int ret;
  5786. ctxt.seid = pf->main_vsi_seid;
  5787. ctxt.pf_num = pf->hw.pf_id;
  5788. ctxt.vf_num = 0;
  5789. ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
  5790. if (ret) {
  5791. dev_info(&pf->pdev->dev,
  5792. "couldn't get PF vsi config, err %s aq_err %s\n",
  5793. i40e_stat_str(&pf->hw, ret),
  5794. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5795. return;
  5796. }
  5797. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  5798. ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  5799. ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  5800. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  5801. if (ret) {
  5802. dev_info(&pf->pdev->dev,
  5803. "update vsi switch failed, err %s aq_err %s\n",
  5804. i40e_stat_str(&pf->hw, ret),
  5805. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5806. }
  5807. }
  5808. /**
  5809. * i40e_config_bridge_mode - Configure the HW bridge mode
  5810. * @veb: pointer to the bridge instance
  5811. *
  5812. * Configure the loop back mode for the LAN VSI that is downlink to the
  5813. * specified HW bridge instance. It is expected this function is called
  5814. * when a new HW bridge is instantiated.
  5815. **/
  5816. static void i40e_config_bridge_mode(struct i40e_veb *veb)
  5817. {
  5818. struct i40e_pf *pf = veb->pf;
  5819. if (pf->hw.debug_mask & I40E_DEBUG_LAN)
  5820. dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
  5821. veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
  5822. if (veb->bridge_mode & BRIDGE_MODE_VEPA)
  5823. i40e_disable_pf_switch_lb(pf);
  5824. else
  5825. i40e_enable_pf_switch_lb(pf);
  5826. }
  5827. /**
  5828. * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
  5829. * @veb: pointer to the VEB instance
  5830. *
  5831. * This is a recursive function that first builds the attached VSIs then
  5832. * recurses in to build the next layer of VEB. We track the connections
  5833. * through our own index numbers because the seid's from the HW could
  5834. * change across the reset.
  5835. **/
  5836. static int i40e_reconstitute_veb(struct i40e_veb *veb)
  5837. {
  5838. struct i40e_vsi *ctl_vsi = NULL;
  5839. struct i40e_pf *pf = veb->pf;
  5840. int v, veb_idx;
  5841. int ret;
  5842. /* build VSI that owns this VEB, temporarily attached to base VEB */
  5843. for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
  5844. if (pf->vsi[v] &&
  5845. pf->vsi[v]->veb_idx == veb->idx &&
  5846. pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
  5847. ctl_vsi = pf->vsi[v];
  5848. break;
  5849. }
  5850. }
  5851. if (!ctl_vsi) {
  5852. dev_info(&pf->pdev->dev,
  5853. "missing owner VSI for veb_idx %d\n", veb->idx);
  5854. ret = -ENOENT;
  5855. goto end_reconstitute;
  5856. }
  5857. if (ctl_vsi != pf->vsi[pf->lan_vsi])
  5858. ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
  5859. ret = i40e_add_vsi(ctl_vsi);
  5860. if (ret) {
  5861. dev_info(&pf->pdev->dev,
  5862. "rebuild of veb_idx %d owner VSI failed: %d\n",
  5863. veb->idx, ret);
  5864. goto end_reconstitute;
  5865. }
  5866. i40e_vsi_reset_stats(ctl_vsi);
  5867. /* create the VEB in the switch and move the VSI onto the VEB */
  5868. ret = i40e_add_veb(veb, ctl_vsi);
  5869. if (ret)
  5870. goto end_reconstitute;
  5871. if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
  5872. veb->bridge_mode = BRIDGE_MODE_VEB;
  5873. else
  5874. veb->bridge_mode = BRIDGE_MODE_VEPA;
  5875. i40e_config_bridge_mode(veb);
  5876. /* create the remaining VSIs attached to this VEB */
  5877. for (v = 0; v < pf->num_alloc_vsi; v++) {
  5878. if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
  5879. continue;
  5880. if (pf->vsi[v]->veb_idx == veb->idx) {
  5881. struct i40e_vsi *vsi = pf->vsi[v];
  5882. vsi->uplink_seid = veb->seid;
  5883. ret = i40e_add_vsi(vsi);
  5884. if (ret) {
  5885. dev_info(&pf->pdev->dev,
  5886. "rebuild of vsi_idx %d failed: %d\n",
  5887. v, ret);
  5888. goto end_reconstitute;
  5889. }
  5890. i40e_vsi_reset_stats(vsi);
  5891. }
  5892. }
  5893. /* create any VEBs attached to this VEB - RECURSION */
  5894. for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
  5895. if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
  5896. pf->veb[veb_idx]->uplink_seid = veb->seid;
  5897. ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
  5898. if (ret)
  5899. break;
  5900. }
  5901. }
  5902. end_reconstitute:
  5903. return ret;
  5904. }
  5905. /**
  5906. * i40e_get_capabilities - get info about the HW
  5907. * @pf: the PF struct
  5908. **/
  5909. static int i40e_get_capabilities(struct i40e_pf *pf)
  5910. {
  5911. struct i40e_aqc_list_capabilities_element_resp *cap_buf;
  5912. u16 data_size;
  5913. int buf_len;
  5914. int err;
  5915. buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
  5916. do {
  5917. cap_buf = kzalloc(buf_len, GFP_KERNEL);
  5918. if (!cap_buf)
  5919. return -ENOMEM;
  5920. /* this loads the data into the hw struct for us */
  5921. err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
  5922. &data_size,
  5923. i40e_aqc_opc_list_func_capabilities,
  5924. NULL);
  5925. /* data loaded, buffer no longer needed */
  5926. kfree(cap_buf);
  5927. if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
  5928. /* retry with a larger buffer */
  5929. buf_len = data_size;
  5930. } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
  5931. dev_info(&pf->pdev->dev,
  5932. "capability discovery failed, err %s aq_err %s\n",
  5933. i40e_stat_str(&pf->hw, err),
  5934. i40e_aq_str(&pf->hw,
  5935. pf->hw.aq.asq_last_status));
  5936. return -ENODEV;
  5937. }
  5938. } while (err);
  5939. if (pf->hw.debug_mask & I40E_DEBUG_USER)
  5940. dev_info(&pf->pdev->dev,
  5941. "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",
  5942. pf->hw.pf_id, pf->hw.func_caps.num_vfs,
  5943. pf->hw.func_caps.num_msix_vectors,
  5944. pf->hw.func_caps.num_msix_vectors_vf,
  5945. pf->hw.func_caps.fd_filters_guaranteed,
  5946. pf->hw.func_caps.fd_filters_best_effort,
  5947. pf->hw.func_caps.num_tx_qp,
  5948. pf->hw.func_caps.num_vsis);
  5949. #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
  5950. + pf->hw.func_caps.num_vfs)
  5951. if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
  5952. dev_info(&pf->pdev->dev,
  5953. "got num_vsis %d, setting num_vsis to %d\n",
  5954. pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
  5955. pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
  5956. }
  5957. return 0;
  5958. }
  5959. static int i40e_vsi_clear(struct i40e_vsi *vsi);
  5960. /**
  5961. * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
  5962. * @pf: board private structure
  5963. **/
  5964. static void i40e_fdir_sb_setup(struct i40e_pf *pf)
  5965. {
  5966. struct i40e_vsi *vsi;
  5967. /* quick workaround for an NVM issue that leaves a critical register
  5968. * uninitialized
  5969. */
  5970. if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
  5971. static const u32 hkey[] = {
  5972. 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
  5973. 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
  5974. 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
  5975. 0x95b3a76d};
  5976. int i;
  5977. for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
  5978. wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
  5979. }
  5980. if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
  5981. return;
  5982. /* find existing VSI and see if it needs configuring */
  5983. vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
  5984. /* create a new VSI if none exists */
  5985. if (!vsi) {
  5986. vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
  5987. pf->vsi[pf->lan_vsi]->seid, 0);
  5988. if (!vsi) {
  5989. dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
  5990. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  5991. return;
  5992. }
  5993. }
  5994. i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
  5995. }
  5996. /**
  5997. * i40e_fdir_teardown - release the Flow Director resources
  5998. * @pf: board private structure
  5999. **/
  6000. static void i40e_fdir_teardown(struct i40e_pf *pf)
  6001. {
  6002. struct i40e_vsi *vsi;
  6003. i40e_fdir_filter_exit(pf);
  6004. vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
  6005. if (vsi)
  6006. i40e_vsi_release(vsi);
  6007. }
  6008. /**
  6009. * i40e_prep_for_reset - prep for the core to reset
  6010. * @pf: board private structure
  6011. *
  6012. * Close up the VFs and other things in prep for PF Reset.
  6013. **/
  6014. static void i40e_prep_for_reset(struct i40e_pf *pf)
  6015. {
  6016. struct i40e_hw *hw = &pf->hw;
  6017. i40e_status ret = 0;
  6018. u32 v;
  6019. clear_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
  6020. if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
  6021. return;
  6022. if (i40e_check_asq_alive(&pf->hw))
  6023. i40e_vc_notify_reset(pf);
  6024. dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
  6025. /* quiesce the VSIs and their queues that are not already DOWN */
  6026. i40e_pf_quiesce_all_vsi(pf);
  6027. for (v = 0; v < pf->num_alloc_vsi; v++) {
  6028. if (pf->vsi[v])
  6029. pf->vsi[v]->seid = 0;
  6030. }
  6031. i40e_shutdown_adminq(&pf->hw);
  6032. /* call shutdown HMC */
  6033. if (hw->hmc.hmc_obj) {
  6034. ret = i40e_shutdown_lan_hmc(hw);
  6035. if (ret)
  6036. dev_warn(&pf->pdev->dev,
  6037. "shutdown_lan_hmc failed: %d\n", ret);
  6038. }
  6039. }
  6040. /**
  6041. * i40e_send_version - update firmware with driver version
  6042. * @pf: PF struct
  6043. */
  6044. static void i40e_send_version(struct i40e_pf *pf)
  6045. {
  6046. struct i40e_driver_version dv;
  6047. dv.major_version = DRV_VERSION_MAJOR;
  6048. dv.minor_version = DRV_VERSION_MINOR;
  6049. dv.build_version = DRV_VERSION_BUILD;
  6050. dv.subbuild_version = 0;
  6051. strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
  6052. i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
  6053. }
  6054. /**
  6055. * i40e_reset_and_rebuild - reset and rebuild using a saved config
  6056. * @pf: board private structure
  6057. * @reinit: if the Main VSI needs to re-initialized.
  6058. **/
  6059. static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit)
  6060. {
  6061. struct i40e_hw *hw = &pf->hw;
  6062. u8 set_fc_aq_fail = 0;
  6063. i40e_status ret;
  6064. u32 val;
  6065. u32 v;
  6066. /* Now we wait for GRST to settle out.
  6067. * We don't have to delete the VEBs or VSIs from the hw switch
  6068. * because the reset will make them disappear.
  6069. */
  6070. ret = i40e_pf_reset(hw);
  6071. if (ret) {
  6072. dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
  6073. set_bit(__I40E_RESET_FAILED, &pf->state);
  6074. goto clear_recovery;
  6075. }
  6076. pf->pfr_count++;
  6077. if (test_bit(__I40E_DOWN, &pf->state))
  6078. goto clear_recovery;
  6079. dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
  6080. /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
  6081. ret = i40e_init_adminq(&pf->hw);
  6082. if (ret) {
  6083. dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
  6084. i40e_stat_str(&pf->hw, ret),
  6085. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  6086. goto clear_recovery;
  6087. }
  6088. /* re-verify the eeprom if we just had an EMP reset */
  6089. if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state))
  6090. i40e_verify_eeprom(pf);
  6091. i40e_clear_pxe_mode(hw);
  6092. ret = i40e_get_capabilities(pf);
  6093. if (ret)
  6094. goto end_core_reset;
  6095. ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
  6096. hw->func_caps.num_rx_qp, 0, 0);
  6097. if (ret) {
  6098. dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
  6099. goto end_core_reset;
  6100. }
  6101. ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
  6102. if (ret) {
  6103. dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
  6104. goto end_core_reset;
  6105. }
  6106. #ifdef CONFIG_I40E_DCB
  6107. ret = i40e_init_pf_dcb(pf);
  6108. if (ret) {
  6109. dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
  6110. pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
  6111. /* Continue without DCB enabled */
  6112. }
  6113. #endif /* CONFIG_I40E_DCB */
  6114. /* do basic switch setup */
  6115. ret = i40e_setup_pf_switch(pf, reinit);
  6116. if (ret)
  6117. goto end_core_reset;
  6118. /* The driver only wants link up/down and module qualification
  6119. * reports from firmware. Note the negative logic.
  6120. */
  6121. ret = i40e_aq_set_phy_int_mask(&pf->hw,
  6122. ~(I40E_AQ_EVENT_LINK_UPDOWN |
  6123. I40E_AQ_EVENT_MEDIA_NA |
  6124. I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
  6125. if (ret)
  6126. dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
  6127. i40e_stat_str(&pf->hw, ret),
  6128. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  6129. /* make sure our flow control settings are restored */
  6130. ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
  6131. if (ret)
  6132. dev_dbg(&pf->pdev->dev, "setting flow control: ret = %s last_status = %s\n",
  6133. i40e_stat_str(&pf->hw, ret),
  6134. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  6135. /* Rebuild the VSIs and VEBs that existed before reset.
  6136. * They are still in our local switch element arrays, so only
  6137. * need to rebuild the switch model in the HW.
  6138. *
  6139. * If there were VEBs but the reconstitution failed, we'll try
  6140. * try to recover minimal use by getting the basic PF VSI working.
  6141. */
  6142. if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
  6143. dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
  6144. /* find the one VEB connected to the MAC, and find orphans */
  6145. for (v = 0; v < I40E_MAX_VEB; v++) {
  6146. if (!pf->veb[v])
  6147. continue;
  6148. if (pf->veb[v]->uplink_seid == pf->mac_seid ||
  6149. pf->veb[v]->uplink_seid == 0) {
  6150. ret = i40e_reconstitute_veb(pf->veb[v]);
  6151. if (!ret)
  6152. continue;
  6153. /* If Main VEB failed, we're in deep doodoo,
  6154. * so give up rebuilding the switch and set up
  6155. * for minimal rebuild of PF VSI.
  6156. * If orphan failed, we'll report the error
  6157. * but try to keep going.
  6158. */
  6159. if (pf->veb[v]->uplink_seid == pf->mac_seid) {
  6160. dev_info(&pf->pdev->dev,
  6161. "rebuild of switch failed: %d, will try to set up simple PF connection\n",
  6162. ret);
  6163. pf->vsi[pf->lan_vsi]->uplink_seid
  6164. = pf->mac_seid;
  6165. break;
  6166. } else if (pf->veb[v]->uplink_seid == 0) {
  6167. dev_info(&pf->pdev->dev,
  6168. "rebuild of orphan VEB failed: %d\n",
  6169. ret);
  6170. }
  6171. }
  6172. }
  6173. }
  6174. if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
  6175. dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
  6176. /* no VEB, so rebuild only the Main VSI */
  6177. ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
  6178. if (ret) {
  6179. dev_info(&pf->pdev->dev,
  6180. "rebuild of Main VSI failed: %d\n", ret);
  6181. goto end_core_reset;
  6182. }
  6183. }
  6184. /* Reconfigure hardware for allowing smaller MSS in the case
  6185. * of TSO, so that we avoid the MDD being fired and causing
  6186. * a reset in the case of small MSS+TSO.
  6187. */
  6188. #define I40E_REG_MSS 0x000E64DC
  6189. #define I40E_REG_MSS_MIN_MASK 0x3FF0000
  6190. #define I40E_64BYTE_MSS 0x400000
  6191. val = rd32(hw, I40E_REG_MSS);
  6192. if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
  6193. val &= ~I40E_REG_MSS_MIN_MASK;
  6194. val |= I40E_64BYTE_MSS;
  6195. wr32(hw, I40E_REG_MSS, val);
  6196. }
  6197. if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
  6198. msleep(75);
  6199. ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
  6200. if (ret)
  6201. dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
  6202. i40e_stat_str(&pf->hw, ret),
  6203. i40e_aq_str(&pf->hw,
  6204. pf->hw.aq.asq_last_status));
  6205. }
  6206. /* reinit the misc interrupt */
  6207. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  6208. ret = i40e_setup_misc_vector(pf);
  6209. /* Add a filter to drop all Flow control frames from any VSI from being
  6210. * transmitted. By doing so we stop a malicious VF from sending out
  6211. * PAUSE or PFC frames and potentially controlling traffic for other
  6212. * PF/VF VSIs.
  6213. * The FW can still send Flow control frames if enabled.
  6214. */
  6215. i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
  6216. pf->main_vsi_seid);
  6217. /* restart the VSIs that were rebuilt and running before the reset */
  6218. i40e_pf_unquiesce_all_vsi(pf);
  6219. if (pf->num_alloc_vfs) {
  6220. for (v = 0; v < pf->num_alloc_vfs; v++)
  6221. i40e_reset_vf(&pf->vf[v], true);
  6222. }
  6223. /* tell the firmware that we're starting */
  6224. i40e_send_version(pf);
  6225. end_core_reset:
  6226. clear_bit(__I40E_RESET_FAILED, &pf->state);
  6227. clear_recovery:
  6228. clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
  6229. }
  6230. /**
  6231. * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
  6232. * @pf: board private structure
  6233. *
  6234. * Close up the VFs and other things in prep for a Core Reset,
  6235. * then get ready to rebuild the world.
  6236. **/
  6237. static void i40e_handle_reset_warning(struct i40e_pf *pf)
  6238. {
  6239. i40e_prep_for_reset(pf);
  6240. i40e_reset_and_rebuild(pf, false);
  6241. }
  6242. /**
  6243. * i40e_handle_mdd_event
  6244. * @pf: pointer to the PF structure
  6245. *
  6246. * Called from the MDD irq handler to identify possibly malicious vfs
  6247. **/
  6248. static void i40e_handle_mdd_event(struct i40e_pf *pf)
  6249. {
  6250. struct i40e_hw *hw = &pf->hw;
  6251. bool mdd_detected = false;
  6252. bool pf_mdd_detected = false;
  6253. struct i40e_vf *vf;
  6254. u32 reg;
  6255. int i;
  6256. if (!test_bit(__I40E_MDD_EVENT_PENDING, &pf->state))
  6257. return;
  6258. /* find what triggered the MDD event */
  6259. reg = rd32(hw, I40E_GL_MDET_TX);
  6260. if (reg & I40E_GL_MDET_TX_VALID_MASK) {
  6261. u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
  6262. I40E_GL_MDET_TX_PF_NUM_SHIFT;
  6263. u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
  6264. I40E_GL_MDET_TX_VF_NUM_SHIFT;
  6265. u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
  6266. I40E_GL_MDET_TX_EVENT_SHIFT;
  6267. u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
  6268. I40E_GL_MDET_TX_QUEUE_SHIFT) -
  6269. pf->hw.func_caps.base_queue;
  6270. if (netif_msg_tx_err(pf))
  6271. dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
  6272. event, queue, pf_num, vf_num);
  6273. wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
  6274. mdd_detected = true;
  6275. }
  6276. reg = rd32(hw, I40E_GL_MDET_RX);
  6277. if (reg & I40E_GL_MDET_RX_VALID_MASK) {
  6278. u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
  6279. I40E_GL_MDET_RX_FUNCTION_SHIFT;
  6280. u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
  6281. I40E_GL_MDET_RX_EVENT_SHIFT;
  6282. u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
  6283. I40E_GL_MDET_RX_QUEUE_SHIFT) -
  6284. pf->hw.func_caps.base_queue;
  6285. if (netif_msg_rx_err(pf))
  6286. dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
  6287. event, queue, func);
  6288. wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
  6289. mdd_detected = true;
  6290. }
  6291. if (mdd_detected) {
  6292. reg = rd32(hw, I40E_PF_MDET_TX);
  6293. if (reg & I40E_PF_MDET_TX_VALID_MASK) {
  6294. wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
  6295. dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
  6296. pf_mdd_detected = true;
  6297. }
  6298. reg = rd32(hw, I40E_PF_MDET_RX);
  6299. if (reg & I40E_PF_MDET_RX_VALID_MASK) {
  6300. wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
  6301. dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
  6302. pf_mdd_detected = true;
  6303. }
  6304. /* Queue belongs to the PF, initiate a reset */
  6305. if (pf_mdd_detected) {
  6306. set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
  6307. i40e_service_event_schedule(pf);
  6308. }
  6309. }
  6310. /* see if one of the VFs needs its hand slapped */
  6311. for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
  6312. vf = &(pf->vf[i]);
  6313. reg = rd32(hw, I40E_VP_MDET_TX(i));
  6314. if (reg & I40E_VP_MDET_TX_VALID_MASK) {
  6315. wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
  6316. vf->num_mdd_events++;
  6317. dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
  6318. i);
  6319. }
  6320. reg = rd32(hw, I40E_VP_MDET_RX(i));
  6321. if (reg & I40E_VP_MDET_RX_VALID_MASK) {
  6322. wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
  6323. vf->num_mdd_events++;
  6324. dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
  6325. i);
  6326. }
  6327. if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
  6328. dev_info(&pf->pdev->dev,
  6329. "Too many MDD events on VF %d, disabled\n", i);
  6330. dev_info(&pf->pdev->dev,
  6331. "Use PF Control I/F to re-enable the VF\n");
  6332. set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
  6333. }
  6334. }
  6335. /* re-enable mdd interrupt cause */
  6336. clear_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
  6337. reg = rd32(hw, I40E_PFINT_ICR0_ENA);
  6338. reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
  6339. wr32(hw, I40E_PFINT_ICR0_ENA, reg);
  6340. i40e_flush(hw);
  6341. }
  6342. /**
  6343. * i40e_sync_udp_filters_subtask - Sync the VSI filter list with HW
  6344. * @pf: board private structure
  6345. **/
  6346. static void i40e_sync_udp_filters_subtask(struct i40e_pf *pf)
  6347. {
  6348. struct i40e_hw *hw = &pf->hw;
  6349. i40e_status ret;
  6350. u16 port;
  6351. int i;
  6352. if (!(pf->flags & I40E_FLAG_UDP_FILTER_SYNC))
  6353. return;
  6354. pf->flags &= ~I40E_FLAG_UDP_FILTER_SYNC;
  6355. for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
  6356. if (pf->pending_udp_bitmap & BIT_ULL(i)) {
  6357. pf->pending_udp_bitmap &= ~BIT_ULL(i);
  6358. port = pf->udp_ports[i].index;
  6359. if (port)
  6360. ret = i40e_aq_add_udp_tunnel(hw, port,
  6361. pf->udp_ports[i].type,
  6362. NULL, NULL);
  6363. else
  6364. ret = i40e_aq_del_udp_tunnel(hw, i, NULL);
  6365. if (ret) {
  6366. dev_dbg(&pf->pdev->dev,
  6367. "%s %s port %d, index %d failed, err %s aq_err %s\n",
  6368. pf->udp_ports[i].type ? "vxlan" : "geneve",
  6369. port ? "add" : "delete",
  6370. port, i,
  6371. i40e_stat_str(&pf->hw, ret),
  6372. i40e_aq_str(&pf->hw,
  6373. pf->hw.aq.asq_last_status));
  6374. pf->udp_ports[i].index = 0;
  6375. }
  6376. }
  6377. }
  6378. }
  6379. /**
  6380. * i40e_service_task - Run the driver's async subtasks
  6381. * @work: pointer to work_struct containing our data
  6382. **/
  6383. static void i40e_service_task(struct work_struct *work)
  6384. {
  6385. struct i40e_pf *pf = container_of(work,
  6386. struct i40e_pf,
  6387. service_task);
  6388. unsigned long start_time = jiffies;
  6389. /* don't bother with service tasks if a reset is in progress */
  6390. if (test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
  6391. return;
  6392. }
  6393. if (test_and_set_bit(__I40E_SERVICE_SCHED, &pf->state))
  6394. return;
  6395. i40e_detect_recover_hung(pf);
  6396. i40e_sync_filters_subtask(pf);
  6397. i40e_reset_subtask(pf);
  6398. i40e_handle_mdd_event(pf);
  6399. i40e_vc_process_vflr_event(pf);
  6400. i40e_watchdog_subtask(pf);
  6401. i40e_fdir_reinit_subtask(pf);
  6402. if (pf->flags & I40E_FLAG_CLIENT_RESET) {
  6403. /* Client subtask will reopen next time through. */
  6404. i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], true);
  6405. pf->flags &= ~I40E_FLAG_CLIENT_RESET;
  6406. } else {
  6407. i40e_client_subtask(pf);
  6408. if (pf->flags & I40E_FLAG_CLIENT_L2_CHANGE) {
  6409. i40e_notify_client_of_l2_param_changes(
  6410. pf->vsi[pf->lan_vsi]);
  6411. pf->flags &= ~I40E_FLAG_CLIENT_L2_CHANGE;
  6412. }
  6413. }
  6414. i40e_sync_filters_subtask(pf);
  6415. i40e_sync_udp_filters_subtask(pf);
  6416. i40e_clean_adminq_subtask(pf);
  6417. /* flush memory to make sure state is correct before next watchdog */
  6418. smp_mb__before_atomic();
  6419. clear_bit(__I40E_SERVICE_SCHED, &pf->state);
  6420. /* If the tasks have taken longer than one timer cycle or there
  6421. * is more work to be done, reschedule the service task now
  6422. * rather than wait for the timer to tick again.
  6423. */
  6424. if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
  6425. test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state) ||
  6426. test_bit(__I40E_MDD_EVENT_PENDING, &pf->state) ||
  6427. test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
  6428. i40e_service_event_schedule(pf);
  6429. }
  6430. /**
  6431. * i40e_service_timer - timer callback
  6432. * @data: pointer to PF struct
  6433. **/
  6434. static void i40e_service_timer(unsigned long data)
  6435. {
  6436. struct i40e_pf *pf = (struct i40e_pf *)data;
  6437. mod_timer(&pf->service_timer,
  6438. round_jiffies(jiffies + pf->service_timer_period));
  6439. i40e_service_event_schedule(pf);
  6440. }
  6441. /**
  6442. * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
  6443. * @vsi: the VSI being configured
  6444. **/
  6445. static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
  6446. {
  6447. struct i40e_pf *pf = vsi->back;
  6448. switch (vsi->type) {
  6449. case I40E_VSI_MAIN:
  6450. vsi->alloc_queue_pairs = pf->num_lan_qps;
  6451. vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  6452. I40E_REQ_DESCRIPTOR_MULTIPLE);
  6453. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  6454. vsi->num_q_vectors = pf->num_lan_msix;
  6455. else
  6456. vsi->num_q_vectors = 1;
  6457. break;
  6458. case I40E_VSI_FDIR:
  6459. vsi->alloc_queue_pairs = 1;
  6460. vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
  6461. I40E_REQ_DESCRIPTOR_MULTIPLE);
  6462. vsi->num_q_vectors = pf->num_fdsb_msix;
  6463. break;
  6464. case I40E_VSI_VMDQ2:
  6465. vsi->alloc_queue_pairs = pf->num_vmdq_qps;
  6466. vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  6467. I40E_REQ_DESCRIPTOR_MULTIPLE);
  6468. vsi->num_q_vectors = pf->num_vmdq_msix;
  6469. break;
  6470. case I40E_VSI_SRIOV:
  6471. vsi->alloc_queue_pairs = pf->num_vf_qps;
  6472. vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  6473. I40E_REQ_DESCRIPTOR_MULTIPLE);
  6474. break;
  6475. default:
  6476. WARN_ON(1);
  6477. return -ENODATA;
  6478. }
  6479. return 0;
  6480. }
  6481. /**
  6482. * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
  6483. * @type: VSI pointer
  6484. * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
  6485. *
  6486. * On error: returns error code (negative)
  6487. * On success: returns 0
  6488. **/
  6489. static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
  6490. {
  6491. int size;
  6492. int ret = 0;
  6493. /* allocate memory for both Tx and Rx ring pointers */
  6494. size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs * 2;
  6495. vsi->tx_rings = kzalloc(size, GFP_KERNEL);
  6496. if (!vsi->tx_rings)
  6497. return -ENOMEM;
  6498. vsi->rx_rings = &vsi->tx_rings[vsi->alloc_queue_pairs];
  6499. if (alloc_qvectors) {
  6500. /* allocate memory for q_vector pointers */
  6501. size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
  6502. vsi->q_vectors = kzalloc(size, GFP_KERNEL);
  6503. if (!vsi->q_vectors) {
  6504. ret = -ENOMEM;
  6505. goto err_vectors;
  6506. }
  6507. }
  6508. return ret;
  6509. err_vectors:
  6510. kfree(vsi->tx_rings);
  6511. return ret;
  6512. }
  6513. /**
  6514. * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
  6515. * @pf: board private structure
  6516. * @type: type of VSI
  6517. *
  6518. * On error: returns error code (negative)
  6519. * On success: returns vsi index in PF (positive)
  6520. **/
  6521. static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
  6522. {
  6523. int ret = -ENODEV;
  6524. struct i40e_vsi *vsi;
  6525. int vsi_idx;
  6526. int i;
  6527. /* Need to protect the allocation of the VSIs at the PF level */
  6528. mutex_lock(&pf->switch_mutex);
  6529. /* VSI list may be fragmented if VSI creation/destruction has
  6530. * been happening. We can afford to do a quick scan to look
  6531. * for any free VSIs in the list.
  6532. *
  6533. * find next empty vsi slot, looping back around if necessary
  6534. */
  6535. i = pf->next_vsi;
  6536. while (i < pf->num_alloc_vsi && pf->vsi[i])
  6537. i++;
  6538. if (i >= pf->num_alloc_vsi) {
  6539. i = 0;
  6540. while (i < pf->next_vsi && pf->vsi[i])
  6541. i++;
  6542. }
  6543. if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
  6544. vsi_idx = i; /* Found one! */
  6545. } else {
  6546. ret = -ENODEV;
  6547. goto unlock_pf; /* out of VSI slots! */
  6548. }
  6549. pf->next_vsi = ++i;
  6550. vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
  6551. if (!vsi) {
  6552. ret = -ENOMEM;
  6553. goto unlock_pf;
  6554. }
  6555. vsi->type = type;
  6556. vsi->back = pf;
  6557. set_bit(__I40E_DOWN, &vsi->state);
  6558. vsi->flags = 0;
  6559. vsi->idx = vsi_idx;
  6560. vsi->int_rate_limit = 0;
  6561. vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
  6562. pf->rss_table_size : 64;
  6563. vsi->netdev_registered = false;
  6564. vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
  6565. hash_init(vsi->mac_filter_hash);
  6566. vsi->irqs_ready = false;
  6567. ret = i40e_set_num_rings_in_vsi(vsi);
  6568. if (ret)
  6569. goto err_rings;
  6570. ret = i40e_vsi_alloc_arrays(vsi, true);
  6571. if (ret)
  6572. goto err_rings;
  6573. /* Setup default MSIX irq handler for VSI */
  6574. i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
  6575. /* Initialize VSI lock */
  6576. spin_lock_init(&vsi->mac_filter_hash_lock);
  6577. pf->vsi[vsi_idx] = vsi;
  6578. ret = vsi_idx;
  6579. goto unlock_pf;
  6580. err_rings:
  6581. pf->next_vsi = i - 1;
  6582. kfree(vsi);
  6583. unlock_pf:
  6584. mutex_unlock(&pf->switch_mutex);
  6585. return ret;
  6586. }
  6587. /**
  6588. * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
  6589. * @type: VSI pointer
  6590. * @free_qvectors: a bool to specify if q_vectors need to be freed.
  6591. *
  6592. * On error: returns error code (negative)
  6593. * On success: returns 0
  6594. **/
  6595. static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
  6596. {
  6597. /* free the ring and vector containers */
  6598. if (free_qvectors) {
  6599. kfree(vsi->q_vectors);
  6600. vsi->q_vectors = NULL;
  6601. }
  6602. kfree(vsi->tx_rings);
  6603. vsi->tx_rings = NULL;
  6604. vsi->rx_rings = NULL;
  6605. }
  6606. /**
  6607. * i40e_clear_rss_config_user - clear the user configured RSS hash keys
  6608. * and lookup table
  6609. * @vsi: Pointer to VSI structure
  6610. */
  6611. static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
  6612. {
  6613. if (!vsi)
  6614. return;
  6615. kfree(vsi->rss_hkey_user);
  6616. vsi->rss_hkey_user = NULL;
  6617. kfree(vsi->rss_lut_user);
  6618. vsi->rss_lut_user = NULL;
  6619. }
  6620. /**
  6621. * i40e_vsi_clear - Deallocate the VSI provided
  6622. * @vsi: the VSI being un-configured
  6623. **/
  6624. static int i40e_vsi_clear(struct i40e_vsi *vsi)
  6625. {
  6626. struct i40e_pf *pf;
  6627. if (!vsi)
  6628. return 0;
  6629. if (!vsi->back)
  6630. goto free_vsi;
  6631. pf = vsi->back;
  6632. mutex_lock(&pf->switch_mutex);
  6633. if (!pf->vsi[vsi->idx]) {
  6634. dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
  6635. vsi->idx, vsi->idx, vsi, vsi->type);
  6636. goto unlock_vsi;
  6637. }
  6638. if (pf->vsi[vsi->idx] != vsi) {
  6639. dev_err(&pf->pdev->dev,
  6640. "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
  6641. pf->vsi[vsi->idx]->idx,
  6642. pf->vsi[vsi->idx],
  6643. pf->vsi[vsi->idx]->type,
  6644. vsi->idx, vsi, vsi->type);
  6645. goto unlock_vsi;
  6646. }
  6647. /* updates the PF for this cleared vsi */
  6648. i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
  6649. i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
  6650. i40e_vsi_free_arrays(vsi, true);
  6651. i40e_clear_rss_config_user(vsi);
  6652. pf->vsi[vsi->idx] = NULL;
  6653. if (vsi->idx < pf->next_vsi)
  6654. pf->next_vsi = vsi->idx;
  6655. unlock_vsi:
  6656. mutex_unlock(&pf->switch_mutex);
  6657. free_vsi:
  6658. kfree(vsi);
  6659. return 0;
  6660. }
  6661. /**
  6662. * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
  6663. * @vsi: the VSI being cleaned
  6664. **/
  6665. static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
  6666. {
  6667. int i;
  6668. if (vsi->tx_rings && vsi->tx_rings[0]) {
  6669. for (i = 0; i < vsi->alloc_queue_pairs; i++) {
  6670. kfree_rcu(vsi->tx_rings[i], rcu);
  6671. vsi->tx_rings[i] = NULL;
  6672. vsi->rx_rings[i] = NULL;
  6673. }
  6674. }
  6675. }
  6676. /**
  6677. * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
  6678. * @vsi: the VSI being configured
  6679. **/
  6680. static int i40e_alloc_rings(struct i40e_vsi *vsi)
  6681. {
  6682. struct i40e_ring *tx_ring, *rx_ring;
  6683. struct i40e_pf *pf = vsi->back;
  6684. int i;
  6685. /* Set basic values in the rings to be used later during open() */
  6686. for (i = 0; i < vsi->alloc_queue_pairs; i++) {
  6687. /* allocate space for both Tx and Rx in one shot */
  6688. tx_ring = kzalloc(sizeof(struct i40e_ring) * 2, GFP_KERNEL);
  6689. if (!tx_ring)
  6690. goto err_out;
  6691. tx_ring->queue_index = i;
  6692. tx_ring->reg_idx = vsi->base_queue + i;
  6693. tx_ring->ring_active = false;
  6694. tx_ring->vsi = vsi;
  6695. tx_ring->netdev = vsi->netdev;
  6696. tx_ring->dev = &pf->pdev->dev;
  6697. tx_ring->count = vsi->num_desc;
  6698. tx_ring->size = 0;
  6699. tx_ring->dcb_tc = 0;
  6700. if (vsi->back->flags & I40E_FLAG_WB_ON_ITR_CAPABLE)
  6701. tx_ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
  6702. tx_ring->tx_itr_setting = pf->tx_itr_default;
  6703. vsi->tx_rings[i] = tx_ring;
  6704. rx_ring = &tx_ring[1];
  6705. rx_ring->queue_index = i;
  6706. rx_ring->reg_idx = vsi->base_queue + i;
  6707. rx_ring->ring_active = false;
  6708. rx_ring->vsi = vsi;
  6709. rx_ring->netdev = vsi->netdev;
  6710. rx_ring->dev = &pf->pdev->dev;
  6711. rx_ring->count = vsi->num_desc;
  6712. rx_ring->size = 0;
  6713. rx_ring->dcb_tc = 0;
  6714. rx_ring->rx_itr_setting = pf->rx_itr_default;
  6715. vsi->rx_rings[i] = rx_ring;
  6716. }
  6717. return 0;
  6718. err_out:
  6719. i40e_vsi_clear_rings(vsi);
  6720. return -ENOMEM;
  6721. }
  6722. /**
  6723. * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
  6724. * @pf: board private structure
  6725. * @vectors: the number of MSI-X vectors to request
  6726. *
  6727. * Returns the number of vectors reserved, or error
  6728. **/
  6729. static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
  6730. {
  6731. vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
  6732. I40E_MIN_MSIX, vectors);
  6733. if (vectors < 0) {
  6734. dev_info(&pf->pdev->dev,
  6735. "MSI-X vector reservation failed: %d\n", vectors);
  6736. vectors = 0;
  6737. }
  6738. return vectors;
  6739. }
  6740. /**
  6741. * i40e_init_msix - Setup the MSIX capability
  6742. * @pf: board private structure
  6743. *
  6744. * Work with the OS to set up the MSIX vectors needed.
  6745. *
  6746. * Returns the number of vectors reserved or negative on failure
  6747. **/
  6748. static int i40e_init_msix(struct i40e_pf *pf)
  6749. {
  6750. struct i40e_hw *hw = &pf->hw;
  6751. int cpus, extra_vectors;
  6752. int vectors_left;
  6753. int v_budget, i;
  6754. int v_actual;
  6755. int iwarp_requested = 0;
  6756. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
  6757. return -ENODEV;
  6758. /* The number of vectors we'll request will be comprised of:
  6759. * - Add 1 for "other" cause for Admin Queue events, etc.
  6760. * - The number of LAN queue pairs
  6761. * - Queues being used for RSS.
  6762. * We don't need as many as max_rss_size vectors.
  6763. * use rss_size instead in the calculation since that
  6764. * is governed by number of cpus in the system.
  6765. * - assumes symmetric Tx/Rx pairing
  6766. * - The number of VMDq pairs
  6767. * - The CPU count within the NUMA node if iWARP is enabled
  6768. * Once we count this up, try the request.
  6769. *
  6770. * If we can't get what we want, we'll simplify to nearly nothing
  6771. * and try again. If that still fails, we punt.
  6772. */
  6773. vectors_left = hw->func_caps.num_msix_vectors;
  6774. v_budget = 0;
  6775. /* reserve one vector for miscellaneous handler */
  6776. if (vectors_left) {
  6777. v_budget++;
  6778. vectors_left--;
  6779. }
  6780. /* reserve some vectors for the main PF traffic queues. Initially we
  6781. * only reserve at most 50% of the available vectors, in the case that
  6782. * the number of online CPUs is large. This ensures that we can enable
  6783. * extra features as well. Once we've enabled the other features, we
  6784. * will use any remaining vectors to reach as close as we can to the
  6785. * number of online CPUs.
  6786. */
  6787. cpus = num_online_cpus();
  6788. pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
  6789. vectors_left -= pf->num_lan_msix;
  6790. /* reserve one vector for sideband flow director */
  6791. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  6792. if (vectors_left) {
  6793. pf->num_fdsb_msix = 1;
  6794. v_budget++;
  6795. vectors_left--;
  6796. } else {
  6797. pf->num_fdsb_msix = 0;
  6798. }
  6799. }
  6800. /* can we reserve enough for iWARP? */
  6801. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  6802. iwarp_requested = pf->num_iwarp_msix;
  6803. if (!vectors_left)
  6804. pf->num_iwarp_msix = 0;
  6805. else if (vectors_left < pf->num_iwarp_msix)
  6806. pf->num_iwarp_msix = 1;
  6807. v_budget += pf->num_iwarp_msix;
  6808. vectors_left -= pf->num_iwarp_msix;
  6809. }
  6810. /* any vectors left over go for VMDq support */
  6811. if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
  6812. int vmdq_vecs_wanted = pf->num_vmdq_vsis * pf->num_vmdq_qps;
  6813. int vmdq_vecs = min_t(int, vectors_left, vmdq_vecs_wanted);
  6814. if (!vectors_left) {
  6815. pf->num_vmdq_msix = 0;
  6816. pf->num_vmdq_qps = 0;
  6817. } else {
  6818. /* if we're short on vectors for what's desired, we limit
  6819. * the queues per vmdq. If this is still more than are
  6820. * available, the user will need to change the number of
  6821. * queues/vectors used by the PF later with the ethtool
  6822. * channels command
  6823. */
  6824. if (vmdq_vecs < vmdq_vecs_wanted)
  6825. pf->num_vmdq_qps = 1;
  6826. pf->num_vmdq_msix = pf->num_vmdq_qps;
  6827. v_budget += vmdq_vecs;
  6828. vectors_left -= vmdq_vecs;
  6829. }
  6830. }
  6831. /* On systems with a large number of SMP cores, we previously limited
  6832. * the number of vectors for num_lan_msix to be at most 50% of the
  6833. * available vectors, to allow for other features. Now, we add back
  6834. * the remaining vectors. However, we ensure that the total
  6835. * num_lan_msix will not exceed num_online_cpus(). To do this, we
  6836. * calculate the number of vectors we can add without going over the
  6837. * cap of CPUs. For systems with a small number of CPUs this will be
  6838. * zero.
  6839. */
  6840. extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
  6841. pf->num_lan_msix += extra_vectors;
  6842. vectors_left -= extra_vectors;
  6843. WARN(vectors_left < 0,
  6844. "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
  6845. v_budget += pf->num_lan_msix;
  6846. pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
  6847. GFP_KERNEL);
  6848. if (!pf->msix_entries)
  6849. return -ENOMEM;
  6850. for (i = 0; i < v_budget; i++)
  6851. pf->msix_entries[i].entry = i;
  6852. v_actual = i40e_reserve_msix_vectors(pf, v_budget);
  6853. if (v_actual < I40E_MIN_MSIX) {
  6854. pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
  6855. kfree(pf->msix_entries);
  6856. pf->msix_entries = NULL;
  6857. pci_disable_msix(pf->pdev);
  6858. return -ENODEV;
  6859. } else if (v_actual == I40E_MIN_MSIX) {
  6860. /* Adjust for minimal MSIX use */
  6861. pf->num_vmdq_vsis = 0;
  6862. pf->num_vmdq_qps = 0;
  6863. pf->num_lan_qps = 1;
  6864. pf->num_lan_msix = 1;
  6865. } else if (!vectors_left) {
  6866. /* If we have limited resources, we will start with no vectors
  6867. * for the special features and then allocate vectors to some
  6868. * of these features based on the policy and at the end disable
  6869. * the features that did not get any vectors.
  6870. */
  6871. int vec;
  6872. dev_info(&pf->pdev->dev,
  6873. "MSI-X vector limit reached, attempting to redistribute vectors\n");
  6874. /* reserve the misc vector */
  6875. vec = v_actual - 1;
  6876. /* Scale vector usage down */
  6877. pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
  6878. pf->num_vmdq_vsis = 1;
  6879. pf->num_vmdq_qps = 1;
  6880. /* partition out the remaining vectors */
  6881. switch (vec) {
  6882. case 2:
  6883. pf->num_lan_msix = 1;
  6884. break;
  6885. case 3:
  6886. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  6887. pf->num_lan_msix = 1;
  6888. pf->num_iwarp_msix = 1;
  6889. } else {
  6890. pf->num_lan_msix = 2;
  6891. }
  6892. break;
  6893. default:
  6894. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  6895. pf->num_iwarp_msix = min_t(int, (vec / 3),
  6896. iwarp_requested);
  6897. pf->num_vmdq_vsis = min_t(int, (vec / 3),
  6898. I40E_DEFAULT_NUM_VMDQ_VSI);
  6899. } else {
  6900. pf->num_vmdq_vsis = min_t(int, (vec / 2),
  6901. I40E_DEFAULT_NUM_VMDQ_VSI);
  6902. }
  6903. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  6904. pf->num_fdsb_msix = 1;
  6905. vec--;
  6906. }
  6907. pf->num_lan_msix = min_t(int,
  6908. (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
  6909. pf->num_lan_msix);
  6910. pf->num_lan_qps = pf->num_lan_msix;
  6911. break;
  6912. }
  6913. }
  6914. if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
  6915. (pf->num_fdsb_msix == 0)) {
  6916. dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
  6917. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  6918. }
  6919. if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
  6920. (pf->num_vmdq_msix == 0)) {
  6921. dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
  6922. pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
  6923. }
  6924. if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
  6925. (pf->num_iwarp_msix == 0)) {
  6926. dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
  6927. pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
  6928. }
  6929. i40e_debug(&pf->hw, I40E_DEBUG_INIT,
  6930. "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
  6931. pf->num_lan_msix,
  6932. pf->num_vmdq_msix * pf->num_vmdq_vsis,
  6933. pf->num_fdsb_msix,
  6934. pf->num_iwarp_msix);
  6935. return v_actual;
  6936. }
  6937. /**
  6938. * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
  6939. * @vsi: the VSI being configured
  6940. * @v_idx: index of the vector in the vsi struct
  6941. * @cpu: cpu to be used on affinity_mask
  6942. *
  6943. * We allocate one q_vector. If allocation fails we return -ENOMEM.
  6944. **/
  6945. static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx, int cpu)
  6946. {
  6947. struct i40e_q_vector *q_vector;
  6948. /* allocate q_vector */
  6949. q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
  6950. if (!q_vector)
  6951. return -ENOMEM;
  6952. q_vector->vsi = vsi;
  6953. q_vector->v_idx = v_idx;
  6954. cpumask_set_cpu(cpu, &q_vector->affinity_mask);
  6955. if (vsi->netdev)
  6956. netif_napi_add(vsi->netdev, &q_vector->napi,
  6957. i40e_napi_poll, NAPI_POLL_WEIGHT);
  6958. q_vector->rx.latency_range = I40E_LOW_LATENCY;
  6959. q_vector->tx.latency_range = I40E_LOW_LATENCY;
  6960. /* tie q_vector and vsi together */
  6961. vsi->q_vectors[v_idx] = q_vector;
  6962. return 0;
  6963. }
  6964. /**
  6965. * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
  6966. * @vsi: the VSI being configured
  6967. *
  6968. * We allocate one q_vector per queue interrupt. If allocation fails we
  6969. * return -ENOMEM.
  6970. **/
  6971. static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
  6972. {
  6973. struct i40e_pf *pf = vsi->back;
  6974. int err, v_idx, num_q_vectors, current_cpu;
  6975. /* if not MSIX, give the one vector only to the LAN VSI */
  6976. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  6977. num_q_vectors = vsi->num_q_vectors;
  6978. else if (vsi == pf->vsi[pf->lan_vsi])
  6979. num_q_vectors = 1;
  6980. else
  6981. return -EINVAL;
  6982. current_cpu = cpumask_first(cpu_online_mask);
  6983. for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
  6984. err = i40e_vsi_alloc_q_vector(vsi, v_idx, current_cpu);
  6985. if (err)
  6986. goto err_out;
  6987. current_cpu = cpumask_next(current_cpu, cpu_online_mask);
  6988. if (unlikely(current_cpu >= nr_cpu_ids))
  6989. current_cpu = cpumask_first(cpu_online_mask);
  6990. }
  6991. return 0;
  6992. err_out:
  6993. while (v_idx--)
  6994. i40e_free_q_vector(vsi, v_idx);
  6995. return err;
  6996. }
  6997. /**
  6998. * i40e_init_interrupt_scheme - Determine proper interrupt scheme
  6999. * @pf: board private structure to initialize
  7000. **/
  7001. static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
  7002. {
  7003. int vectors = 0;
  7004. ssize_t size;
  7005. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  7006. vectors = i40e_init_msix(pf);
  7007. if (vectors < 0) {
  7008. pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
  7009. I40E_FLAG_IWARP_ENABLED |
  7010. I40E_FLAG_RSS_ENABLED |
  7011. I40E_FLAG_DCB_CAPABLE |
  7012. I40E_FLAG_DCB_ENABLED |
  7013. I40E_FLAG_SRIOV_ENABLED |
  7014. I40E_FLAG_FD_SB_ENABLED |
  7015. I40E_FLAG_FD_ATR_ENABLED |
  7016. I40E_FLAG_VMDQ_ENABLED);
  7017. /* rework the queue expectations without MSIX */
  7018. i40e_determine_queue_usage(pf);
  7019. }
  7020. }
  7021. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
  7022. (pf->flags & I40E_FLAG_MSI_ENABLED)) {
  7023. dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
  7024. vectors = pci_enable_msi(pf->pdev);
  7025. if (vectors < 0) {
  7026. dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
  7027. vectors);
  7028. pf->flags &= ~I40E_FLAG_MSI_ENABLED;
  7029. }
  7030. vectors = 1; /* one MSI or Legacy vector */
  7031. }
  7032. if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
  7033. dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
  7034. /* set up vector assignment tracking */
  7035. size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
  7036. pf->irq_pile = kzalloc(size, GFP_KERNEL);
  7037. if (!pf->irq_pile) {
  7038. dev_err(&pf->pdev->dev, "error allocating irq_pile memory\n");
  7039. return -ENOMEM;
  7040. }
  7041. pf->irq_pile->num_entries = vectors;
  7042. pf->irq_pile->search_hint = 0;
  7043. /* track first vector for misc interrupts, ignore return */
  7044. (void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
  7045. return 0;
  7046. }
  7047. /**
  7048. * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
  7049. * @pf: board private structure
  7050. *
  7051. * This sets up the handler for MSIX 0, which is used to manage the
  7052. * non-queue interrupts, e.g. AdminQ and errors. This is not used
  7053. * when in MSI or Legacy interrupt mode.
  7054. **/
  7055. static int i40e_setup_misc_vector(struct i40e_pf *pf)
  7056. {
  7057. struct i40e_hw *hw = &pf->hw;
  7058. int err = 0;
  7059. /* Only request the irq if this is the first time through, and
  7060. * not when we're rebuilding after a Reset
  7061. */
  7062. if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
  7063. err = request_irq(pf->msix_entries[0].vector,
  7064. i40e_intr, 0, pf->int_name, pf);
  7065. if (err) {
  7066. dev_info(&pf->pdev->dev,
  7067. "request_irq for %s failed: %d\n",
  7068. pf->int_name, err);
  7069. return -EFAULT;
  7070. }
  7071. }
  7072. i40e_enable_misc_int_causes(pf);
  7073. /* associate no queues to the misc vector */
  7074. wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
  7075. wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
  7076. i40e_flush(hw);
  7077. i40e_irq_dynamic_enable_icr0(pf, true);
  7078. return err;
  7079. }
  7080. /**
  7081. * i40e_config_rss_aq - Prepare for RSS using AQ commands
  7082. * @vsi: vsi structure
  7083. * @seed: RSS hash seed
  7084. **/
  7085. static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
  7086. u8 *lut, u16 lut_size)
  7087. {
  7088. struct i40e_pf *pf = vsi->back;
  7089. struct i40e_hw *hw = &pf->hw;
  7090. int ret = 0;
  7091. if (seed) {
  7092. struct i40e_aqc_get_set_rss_key_data *seed_dw =
  7093. (struct i40e_aqc_get_set_rss_key_data *)seed;
  7094. ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
  7095. if (ret) {
  7096. dev_info(&pf->pdev->dev,
  7097. "Cannot set RSS key, err %s aq_err %s\n",
  7098. i40e_stat_str(hw, ret),
  7099. i40e_aq_str(hw, hw->aq.asq_last_status));
  7100. return ret;
  7101. }
  7102. }
  7103. if (lut) {
  7104. bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
  7105. ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
  7106. if (ret) {
  7107. dev_info(&pf->pdev->dev,
  7108. "Cannot set RSS lut, err %s aq_err %s\n",
  7109. i40e_stat_str(hw, ret),
  7110. i40e_aq_str(hw, hw->aq.asq_last_status));
  7111. return ret;
  7112. }
  7113. }
  7114. return ret;
  7115. }
  7116. /**
  7117. * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
  7118. * @vsi: Pointer to vsi structure
  7119. * @seed: Buffter to store the hash keys
  7120. * @lut: Buffer to store the lookup table entries
  7121. * @lut_size: Size of buffer to store the lookup table entries
  7122. *
  7123. * Return 0 on success, negative on failure
  7124. */
  7125. static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
  7126. u8 *lut, u16 lut_size)
  7127. {
  7128. struct i40e_pf *pf = vsi->back;
  7129. struct i40e_hw *hw = &pf->hw;
  7130. int ret = 0;
  7131. if (seed) {
  7132. ret = i40e_aq_get_rss_key(hw, vsi->id,
  7133. (struct i40e_aqc_get_set_rss_key_data *)seed);
  7134. if (ret) {
  7135. dev_info(&pf->pdev->dev,
  7136. "Cannot get RSS key, err %s aq_err %s\n",
  7137. i40e_stat_str(&pf->hw, ret),
  7138. i40e_aq_str(&pf->hw,
  7139. pf->hw.aq.asq_last_status));
  7140. return ret;
  7141. }
  7142. }
  7143. if (lut) {
  7144. bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
  7145. ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
  7146. if (ret) {
  7147. dev_info(&pf->pdev->dev,
  7148. "Cannot get RSS lut, err %s aq_err %s\n",
  7149. i40e_stat_str(&pf->hw, ret),
  7150. i40e_aq_str(&pf->hw,
  7151. pf->hw.aq.asq_last_status));
  7152. return ret;
  7153. }
  7154. }
  7155. return ret;
  7156. }
  7157. /**
  7158. * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
  7159. * @vsi: VSI structure
  7160. **/
  7161. static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
  7162. {
  7163. u8 seed[I40E_HKEY_ARRAY_SIZE];
  7164. struct i40e_pf *pf = vsi->back;
  7165. u8 *lut;
  7166. int ret;
  7167. if (!(pf->flags & I40E_FLAG_RSS_AQ_CAPABLE))
  7168. return 0;
  7169. if (!vsi->rss_size)
  7170. vsi->rss_size = min_t(int, pf->alloc_rss_size,
  7171. vsi->num_queue_pairs);
  7172. if (!vsi->rss_size)
  7173. return -EINVAL;
  7174. lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
  7175. if (!lut)
  7176. return -ENOMEM;
  7177. /* Use the user configured hash keys and lookup table if there is one,
  7178. * otherwise use default
  7179. */
  7180. if (vsi->rss_lut_user)
  7181. memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
  7182. else
  7183. i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
  7184. if (vsi->rss_hkey_user)
  7185. memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
  7186. else
  7187. netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
  7188. ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
  7189. kfree(lut);
  7190. return ret;
  7191. }
  7192. /**
  7193. * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
  7194. * @vsi: Pointer to vsi structure
  7195. * @seed: RSS hash seed
  7196. * @lut: Lookup table
  7197. * @lut_size: Lookup table size
  7198. *
  7199. * Returns 0 on success, negative on failure
  7200. **/
  7201. static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
  7202. const u8 *lut, u16 lut_size)
  7203. {
  7204. struct i40e_pf *pf = vsi->back;
  7205. struct i40e_hw *hw = &pf->hw;
  7206. u16 vf_id = vsi->vf_id;
  7207. u8 i;
  7208. /* Fill out hash function seed */
  7209. if (seed) {
  7210. u32 *seed_dw = (u32 *)seed;
  7211. if (vsi->type == I40E_VSI_MAIN) {
  7212. for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
  7213. wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
  7214. } else if (vsi->type == I40E_VSI_SRIOV) {
  7215. for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
  7216. wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
  7217. } else {
  7218. dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
  7219. }
  7220. }
  7221. if (lut) {
  7222. u32 *lut_dw = (u32 *)lut;
  7223. if (vsi->type == I40E_VSI_MAIN) {
  7224. if (lut_size != I40E_HLUT_ARRAY_SIZE)
  7225. return -EINVAL;
  7226. for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
  7227. wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
  7228. } else if (vsi->type == I40E_VSI_SRIOV) {
  7229. if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
  7230. return -EINVAL;
  7231. for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
  7232. wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
  7233. } else {
  7234. dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
  7235. }
  7236. }
  7237. i40e_flush(hw);
  7238. return 0;
  7239. }
  7240. /**
  7241. * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
  7242. * @vsi: Pointer to VSI structure
  7243. * @seed: Buffer to store the keys
  7244. * @lut: Buffer to store the lookup table entries
  7245. * @lut_size: Size of buffer to store the lookup table entries
  7246. *
  7247. * Returns 0 on success, negative on failure
  7248. */
  7249. static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
  7250. u8 *lut, u16 lut_size)
  7251. {
  7252. struct i40e_pf *pf = vsi->back;
  7253. struct i40e_hw *hw = &pf->hw;
  7254. u16 i;
  7255. if (seed) {
  7256. u32 *seed_dw = (u32 *)seed;
  7257. for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
  7258. seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
  7259. }
  7260. if (lut) {
  7261. u32 *lut_dw = (u32 *)lut;
  7262. if (lut_size != I40E_HLUT_ARRAY_SIZE)
  7263. return -EINVAL;
  7264. for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
  7265. lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
  7266. }
  7267. return 0;
  7268. }
  7269. /**
  7270. * i40e_config_rss - Configure RSS keys and lut
  7271. * @vsi: Pointer to VSI structure
  7272. * @seed: RSS hash seed
  7273. * @lut: Lookup table
  7274. * @lut_size: Lookup table size
  7275. *
  7276. * Returns 0 on success, negative on failure
  7277. */
  7278. int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
  7279. {
  7280. struct i40e_pf *pf = vsi->back;
  7281. if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
  7282. return i40e_config_rss_aq(vsi, seed, lut, lut_size);
  7283. else
  7284. return i40e_config_rss_reg(vsi, seed, lut, lut_size);
  7285. }
  7286. /**
  7287. * i40e_get_rss - Get RSS keys and lut
  7288. * @vsi: Pointer to VSI structure
  7289. * @seed: Buffer to store the keys
  7290. * @lut: Buffer to store the lookup table entries
  7291. * lut_size: Size of buffer to store the lookup table entries
  7292. *
  7293. * Returns 0 on success, negative on failure
  7294. */
  7295. int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
  7296. {
  7297. struct i40e_pf *pf = vsi->back;
  7298. if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
  7299. return i40e_get_rss_aq(vsi, seed, lut, lut_size);
  7300. else
  7301. return i40e_get_rss_reg(vsi, seed, lut, lut_size);
  7302. }
  7303. /**
  7304. * i40e_fill_rss_lut - Fill the RSS lookup table with default values
  7305. * @pf: Pointer to board private structure
  7306. * @lut: Lookup table
  7307. * @rss_table_size: Lookup table size
  7308. * @rss_size: Range of queue number for hashing
  7309. */
  7310. void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
  7311. u16 rss_table_size, u16 rss_size)
  7312. {
  7313. u16 i;
  7314. for (i = 0; i < rss_table_size; i++)
  7315. lut[i] = i % rss_size;
  7316. }
  7317. /**
  7318. * i40e_pf_config_rss - Prepare for RSS if used
  7319. * @pf: board private structure
  7320. **/
  7321. static int i40e_pf_config_rss(struct i40e_pf *pf)
  7322. {
  7323. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  7324. u8 seed[I40E_HKEY_ARRAY_SIZE];
  7325. u8 *lut;
  7326. struct i40e_hw *hw = &pf->hw;
  7327. u32 reg_val;
  7328. u64 hena;
  7329. int ret;
  7330. /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
  7331. hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
  7332. ((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
  7333. hena |= i40e_pf_get_default_rss_hena(pf);
  7334. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
  7335. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
  7336. /* Determine the RSS table size based on the hardware capabilities */
  7337. reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
  7338. reg_val = (pf->rss_table_size == 512) ?
  7339. (reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
  7340. (reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
  7341. i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
  7342. /* Determine the RSS size of the VSI */
  7343. if (!vsi->rss_size) {
  7344. u16 qcount;
  7345. qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
  7346. vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
  7347. }
  7348. if (!vsi->rss_size)
  7349. return -EINVAL;
  7350. lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
  7351. if (!lut)
  7352. return -ENOMEM;
  7353. /* Use user configured lut if there is one, otherwise use default */
  7354. if (vsi->rss_lut_user)
  7355. memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
  7356. else
  7357. i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
  7358. /* Use user configured hash key if there is one, otherwise
  7359. * use default.
  7360. */
  7361. if (vsi->rss_hkey_user)
  7362. memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
  7363. else
  7364. netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
  7365. ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
  7366. kfree(lut);
  7367. return ret;
  7368. }
  7369. /**
  7370. * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
  7371. * @pf: board private structure
  7372. * @queue_count: the requested queue count for rss.
  7373. *
  7374. * returns 0 if rss is not enabled, if enabled returns the final rss queue
  7375. * count which may be different from the requested queue count.
  7376. **/
  7377. int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
  7378. {
  7379. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  7380. int new_rss_size;
  7381. if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
  7382. return 0;
  7383. new_rss_size = min_t(int, queue_count, pf->rss_size_max);
  7384. if (queue_count != vsi->num_queue_pairs) {
  7385. u16 qcount;
  7386. vsi->req_queue_pairs = queue_count;
  7387. i40e_prep_for_reset(pf);
  7388. pf->alloc_rss_size = new_rss_size;
  7389. i40e_reset_and_rebuild(pf, true);
  7390. /* Discard the user configured hash keys and lut, if less
  7391. * queues are enabled.
  7392. */
  7393. if (queue_count < vsi->rss_size) {
  7394. i40e_clear_rss_config_user(vsi);
  7395. dev_dbg(&pf->pdev->dev,
  7396. "discard user configured hash keys and lut\n");
  7397. }
  7398. /* Reset vsi->rss_size, as number of enabled queues changed */
  7399. qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
  7400. vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
  7401. i40e_pf_config_rss(pf);
  7402. }
  7403. dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count: %d/%d\n",
  7404. vsi->req_queue_pairs, pf->rss_size_max);
  7405. return pf->alloc_rss_size;
  7406. }
  7407. /**
  7408. * i40e_get_npar_bw_setting - Retrieve BW settings for this PF partition
  7409. * @pf: board private structure
  7410. **/
  7411. i40e_status i40e_get_npar_bw_setting(struct i40e_pf *pf)
  7412. {
  7413. i40e_status status;
  7414. bool min_valid, max_valid;
  7415. u32 max_bw, min_bw;
  7416. status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
  7417. &min_valid, &max_valid);
  7418. if (!status) {
  7419. if (min_valid)
  7420. pf->npar_min_bw = min_bw;
  7421. if (max_valid)
  7422. pf->npar_max_bw = max_bw;
  7423. }
  7424. return status;
  7425. }
  7426. /**
  7427. * i40e_set_npar_bw_setting - Set BW settings for this PF partition
  7428. * @pf: board private structure
  7429. **/
  7430. i40e_status i40e_set_npar_bw_setting(struct i40e_pf *pf)
  7431. {
  7432. struct i40e_aqc_configure_partition_bw_data bw_data;
  7433. i40e_status status;
  7434. /* Set the valid bit for this PF */
  7435. bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
  7436. bw_data.max_bw[pf->hw.pf_id] = pf->npar_max_bw & I40E_ALT_BW_VALUE_MASK;
  7437. bw_data.min_bw[pf->hw.pf_id] = pf->npar_min_bw & I40E_ALT_BW_VALUE_MASK;
  7438. /* Set the new bandwidths */
  7439. status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
  7440. return status;
  7441. }
  7442. /**
  7443. * i40e_commit_npar_bw_setting - Commit BW settings for this PF partition
  7444. * @pf: board private structure
  7445. **/
  7446. i40e_status i40e_commit_npar_bw_setting(struct i40e_pf *pf)
  7447. {
  7448. /* Commit temporary BW setting to permanent NVM image */
  7449. enum i40e_admin_queue_err last_aq_status;
  7450. i40e_status ret;
  7451. u16 nvm_word;
  7452. if (pf->hw.partition_id != 1) {
  7453. dev_info(&pf->pdev->dev,
  7454. "Commit BW only works on partition 1! This is partition %d",
  7455. pf->hw.partition_id);
  7456. ret = I40E_NOT_SUPPORTED;
  7457. goto bw_commit_out;
  7458. }
  7459. /* Acquire NVM for read access */
  7460. ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
  7461. last_aq_status = pf->hw.aq.asq_last_status;
  7462. if (ret) {
  7463. dev_info(&pf->pdev->dev,
  7464. "Cannot acquire NVM for read access, err %s aq_err %s\n",
  7465. i40e_stat_str(&pf->hw, ret),
  7466. i40e_aq_str(&pf->hw, last_aq_status));
  7467. goto bw_commit_out;
  7468. }
  7469. /* Read word 0x10 of NVM - SW compatibility word 1 */
  7470. ret = i40e_aq_read_nvm(&pf->hw,
  7471. I40E_SR_NVM_CONTROL_WORD,
  7472. 0x10, sizeof(nvm_word), &nvm_word,
  7473. false, NULL);
  7474. /* Save off last admin queue command status before releasing
  7475. * the NVM
  7476. */
  7477. last_aq_status = pf->hw.aq.asq_last_status;
  7478. i40e_release_nvm(&pf->hw);
  7479. if (ret) {
  7480. dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
  7481. i40e_stat_str(&pf->hw, ret),
  7482. i40e_aq_str(&pf->hw, last_aq_status));
  7483. goto bw_commit_out;
  7484. }
  7485. /* Wait a bit for NVM release to complete */
  7486. msleep(50);
  7487. /* Acquire NVM for write access */
  7488. ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
  7489. last_aq_status = pf->hw.aq.asq_last_status;
  7490. if (ret) {
  7491. dev_info(&pf->pdev->dev,
  7492. "Cannot acquire NVM for write access, err %s aq_err %s\n",
  7493. i40e_stat_str(&pf->hw, ret),
  7494. i40e_aq_str(&pf->hw, last_aq_status));
  7495. goto bw_commit_out;
  7496. }
  7497. /* Write it back out unchanged to initiate update NVM,
  7498. * which will force a write of the shadow (alt) RAM to
  7499. * the NVM - thus storing the bandwidth values permanently.
  7500. */
  7501. ret = i40e_aq_update_nvm(&pf->hw,
  7502. I40E_SR_NVM_CONTROL_WORD,
  7503. 0x10, sizeof(nvm_word),
  7504. &nvm_word, true, NULL);
  7505. /* Save off last admin queue command status before releasing
  7506. * the NVM
  7507. */
  7508. last_aq_status = pf->hw.aq.asq_last_status;
  7509. i40e_release_nvm(&pf->hw);
  7510. if (ret)
  7511. dev_info(&pf->pdev->dev,
  7512. "BW settings NOT SAVED, err %s aq_err %s\n",
  7513. i40e_stat_str(&pf->hw, ret),
  7514. i40e_aq_str(&pf->hw, last_aq_status));
  7515. bw_commit_out:
  7516. return ret;
  7517. }
  7518. /**
  7519. * i40e_sw_init - Initialize general software structures (struct i40e_pf)
  7520. * @pf: board private structure to initialize
  7521. *
  7522. * i40e_sw_init initializes the Adapter private data structure.
  7523. * Fields are initialized based on PCI device information and
  7524. * OS network device settings (MTU size).
  7525. **/
  7526. static int i40e_sw_init(struct i40e_pf *pf)
  7527. {
  7528. int err = 0;
  7529. int size;
  7530. /* Set default capability flags */
  7531. pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
  7532. I40E_FLAG_MSI_ENABLED |
  7533. I40E_FLAG_MSIX_ENABLED;
  7534. /* Set default ITR */
  7535. pf->rx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF;
  7536. pf->tx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF;
  7537. /* Depending on PF configurations, it is possible that the RSS
  7538. * maximum might end up larger than the available queues
  7539. */
  7540. pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
  7541. pf->alloc_rss_size = 1;
  7542. pf->rss_table_size = pf->hw.func_caps.rss_table_size;
  7543. pf->rss_size_max = min_t(int, pf->rss_size_max,
  7544. pf->hw.func_caps.num_tx_qp);
  7545. if (pf->hw.func_caps.rss) {
  7546. pf->flags |= I40E_FLAG_RSS_ENABLED;
  7547. pf->alloc_rss_size = min_t(int, pf->rss_size_max,
  7548. num_online_cpus());
  7549. }
  7550. /* MFP mode enabled */
  7551. if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
  7552. pf->flags |= I40E_FLAG_MFP_ENABLED;
  7553. dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
  7554. if (i40e_get_npar_bw_setting(pf))
  7555. dev_warn(&pf->pdev->dev,
  7556. "Could not get NPAR bw settings\n");
  7557. else
  7558. dev_info(&pf->pdev->dev,
  7559. "Min BW = %8.8x, Max BW = %8.8x\n",
  7560. pf->npar_min_bw, pf->npar_max_bw);
  7561. }
  7562. /* FW/NVM is not yet fixed in this regard */
  7563. if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
  7564. (pf->hw.func_caps.fd_filters_best_effort > 0)) {
  7565. pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
  7566. pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
  7567. if (pf->flags & I40E_FLAG_MFP_ENABLED &&
  7568. pf->hw.num_partitions > 1)
  7569. dev_info(&pf->pdev->dev,
  7570. "Flow Director Sideband mode Disabled in MFP mode\n");
  7571. else
  7572. pf->flags |= I40E_FLAG_FD_SB_ENABLED;
  7573. pf->fdir_pf_filter_count =
  7574. pf->hw.func_caps.fd_filters_guaranteed;
  7575. pf->hw.fdir_shared_filter_count =
  7576. pf->hw.func_caps.fd_filters_best_effort;
  7577. }
  7578. if ((pf->hw.mac.type == I40E_MAC_XL710) &&
  7579. (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
  7580. (pf->hw.aq.fw_maj_ver < 4))) {
  7581. pf->flags |= I40E_FLAG_RESTART_AUTONEG;
  7582. /* No DCB support for FW < v4.33 */
  7583. pf->flags |= I40E_FLAG_NO_DCB_SUPPORT;
  7584. }
  7585. /* Disable FW LLDP if FW < v4.3 */
  7586. if ((pf->hw.mac.type == I40E_MAC_XL710) &&
  7587. (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
  7588. (pf->hw.aq.fw_maj_ver < 4)))
  7589. pf->flags |= I40E_FLAG_STOP_FW_LLDP;
  7590. /* Use the FW Set LLDP MIB API if FW > v4.40 */
  7591. if ((pf->hw.mac.type == I40E_MAC_XL710) &&
  7592. (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
  7593. (pf->hw.aq.fw_maj_ver >= 5)))
  7594. pf->flags |= I40E_FLAG_USE_SET_LLDP_MIB;
  7595. if (pf->hw.func_caps.vmdq) {
  7596. pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
  7597. pf->flags |= I40E_FLAG_VMDQ_ENABLED;
  7598. pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
  7599. }
  7600. if (pf->hw.func_caps.iwarp) {
  7601. pf->flags |= I40E_FLAG_IWARP_ENABLED;
  7602. /* IWARP needs one extra vector for CQP just like MISC.*/
  7603. pf->num_iwarp_msix = (int)num_online_cpus() + 1;
  7604. }
  7605. #ifdef CONFIG_PCI_IOV
  7606. if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
  7607. pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
  7608. pf->flags |= I40E_FLAG_SRIOV_ENABLED;
  7609. pf->num_req_vfs = min_t(int,
  7610. pf->hw.func_caps.num_vfs,
  7611. I40E_MAX_VF_COUNT);
  7612. }
  7613. #endif /* CONFIG_PCI_IOV */
  7614. if (pf->hw.mac.type == I40E_MAC_X722) {
  7615. pf->flags |= I40E_FLAG_RSS_AQ_CAPABLE
  7616. | I40E_FLAG_128_QP_RSS_CAPABLE
  7617. | I40E_FLAG_HW_ATR_EVICT_CAPABLE
  7618. | I40E_FLAG_OUTER_UDP_CSUM_CAPABLE
  7619. | I40E_FLAG_WB_ON_ITR_CAPABLE
  7620. | I40E_FLAG_MULTIPLE_TCP_UDP_RSS_PCTYPE
  7621. | I40E_FLAG_NO_PCI_LINK_CHECK
  7622. | I40E_FLAG_USE_SET_LLDP_MIB
  7623. | I40E_FLAG_GENEVE_OFFLOAD_CAPABLE
  7624. | I40E_FLAG_PTP_L4_CAPABLE
  7625. | I40E_FLAG_WOL_MC_MAGIC_PKT_WAKE;
  7626. } else if ((pf->hw.aq.api_maj_ver > 1) ||
  7627. ((pf->hw.aq.api_maj_ver == 1) &&
  7628. (pf->hw.aq.api_min_ver > 4))) {
  7629. /* Supported in FW API version higher than 1.4 */
  7630. pf->flags |= I40E_FLAG_GENEVE_OFFLOAD_CAPABLE;
  7631. pf->hw_disabled_flags = I40E_FLAG_HW_ATR_EVICT_CAPABLE;
  7632. } else {
  7633. pf->hw_disabled_flags = I40E_FLAG_HW_ATR_EVICT_CAPABLE;
  7634. }
  7635. pf->eeprom_version = 0xDEAD;
  7636. pf->lan_veb = I40E_NO_VEB;
  7637. pf->lan_vsi = I40E_NO_VSI;
  7638. /* By default FW has this off for performance reasons */
  7639. pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
  7640. /* set up queue assignment tracking */
  7641. size = sizeof(struct i40e_lump_tracking)
  7642. + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
  7643. pf->qp_pile = kzalloc(size, GFP_KERNEL);
  7644. if (!pf->qp_pile) {
  7645. err = -ENOMEM;
  7646. goto sw_init_done;
  7647. }
  7648. pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
  7649. pf->qp_pile->search_hint = 0;
  7650. pf->tx_timeout_recovery_level = 1;
  7651. mutex_init(&pf->switch_mutex);
  7652. /* If NPAR is enabled nudge the Tx scheduler */
  7653. if (pf->hw.func_caps.npar_enable && (!i40e_get_npar_bw_setting(pf)))
  7654. i40e_set_npar_bw_setting(pf);
  7655. sw_init_done:
  7656. return err;
  7657. }
  7658. /**
  7659. * i40e_set_ntuple - set the ntuple feature flag and take action
  7660. * @pf: board private structure to initialize
  7661. * @features: the feature set that the stack is suggesting
  7662. *
  7663. * returns a bool to indicate if reset needs to happen
  7664. **/
  7665. bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
  7666. {
  7667. bool need_reset = false;
  7668. /* Check if Flow Director n-tuple support was enabled or disabled. If
  7669. * the state changed, we need to reset.
  7670. */
  7671. if (features & NETIF_F_NTUPLE) {
  7672. /* Enable filters and mark for reset */
  7673. if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
  7674. need_reset = true;
  7675. /* enable FD_SB only if there is MSI-X vector */
  7676. if (pf->num_fdsb_msix > 0)
  7677. pf->flags |= I40E_FLAG_FD_SB_ENABLED;
  7678. } else {
  7679. /* turn off filters, mark for reset and clear SW filter list */
  7680. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  7681. need_reset = true;
  7682. i40e_fdir_filter_exit(pf);
  7683. }
  7684. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  7685. pf->hw_disabled_flags &= ~I40E_FLAG_FD_SB_ENABLED;
  7686. /* reset fd counters */
  7687. pf->fd_add_err = 0;
  7688. pf->fd_atr_cnt = 0;
  7689. /* if ATR was auto disabled it can be re-enabled. */
  7690. if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
  7691. (pf->hw_disabled_flags & I40E_FLAG_FD_ATR_ENABLED)) {
  7692. pf->hw_disabled_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
  7693. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  7694. dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
  7695. }
  7696. }
  7697. return need_reset;
  7698. }
  7699. /**
  7700. * i40e_clear_rss_lut - clear the rx hash lookup table
  7701. * @vsi: the VSI being configured
  7702. **/
  7703. static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
  7704. {
  7705. struct i40e_pf *pf = vsi->back;
  7706. struct i40e_hw *hw = &pf->hw;
  7707. u16 vf_id = vsi->vf_id;
  7708. u8 i;
  7709. if (vsi->type == I40E_VSI_MAIN) {
  7710. for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
  7711. wr32(hw, I40E_PFQF_HLUT(i), 0);
  7712. } else if (vsi->type == I40E_VSI_SRIOV) {
  7713. for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
  7714. i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
  7715. } else {
  7716. dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
  7717. }
  7718. }
  7719. /**
  7720. * i40e_set_features - set the netdev feature flags
  7721. * @netdev: ptr to the netdev being adjusted
  7722. * @features: the feature set that the stack is suggesting
  7723. **/
  7724. static int i40e_set_features(struct net_device *netdev,
  7725. netdev_features_t features)
  7726. {
  7727. struct i40e_netdev_priv *np = netdev_priv(netdev);
  7728. struct i40e_vsi *vsi = np->vsi;
  7729. struct i40e_pf *pf = vsi->back;
  7730. bool need_reset;
  7731. if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
  7732. i40e_pf_config_rss(pf);
  7733. else if (!(features & NETIF_F_RXHASH) &&
  7734. netdev->features & NETIF_F_RXHASH)
  7735. i40e_clear_rss_lut(vsi);
  7736. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  7737. i40e_vlan_stripping_enable(vsi);
  7738. else
  7739. i40e_vlan_stripping_disable(vsi);
  7740. need_reset = i40e_set_ntuple(pf, features);
  7741. if (need_reset)
  7742. i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
  7743. return 0;
  7744. }
  7745. /**
  7746. * i40e_get_udp_port_idx - Lookup a possibly offloaded for Rx UDP port
  7747. * @pf: board private structure
  7748. * @port: The UDP port to look up
  7749. *
  7750. * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
  7751. **/
  7752. static u8 i40e_get_udp_port_idx(struct i40e_pf *pf, u16 port)
  7753. {
  7754. u8 i;
  7755. for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
  7756. if (pf->udp_ports[i].index == port)
  7757. return i;
  7758. }
  7759. return i;
  7760. }
  7761. /**
  7762. * i40e_udp_tunnel_add - Get notifications about UDP tunnel ports that come up
  7763. * @netdev: This physical port's netdev
  7764. * @ti: Tunnel endpoint information
  7765. **/
  7766. static void i40e_udp_tunnel_add(struct net_device *netdev,
  7767. struct udp_tunnel_info *ti)
  7768. {
  7769. struct i40e_netdev_priv *np = netdev_priv(netdev);
  7770. struct i40e_vsi *vsi = np->vsi;
  7771. struct i40e_pf *pf = vsi->back;
  7772. u16 port = ntohs(ti->port);
  7773. u8 next_idx;
  7774. u8 idx;
  7775. idx = i40e_get_udp_port_idx(pf, port);
  7776. /* Check if port already exists */
  7777. if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
  7778. netdev_info(netdev, "port %d already offloaded\n", port);
  7779. return;
  7780. }
  7781. /* Now check if there is space to add the new port */
  7782. next_idx = i40e_get_udp_port_idx(pf, 0);
  7783. if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
  7784. netdev_info(netdev, "maximum number of offloaded UDP ports reached, not adding port %d\n",
  7785. port);
  7786. return;
  7787. }
  7788. switch (ti->type) {
  7789. case UDP_TUNNEL_TYPE_VXLAN:
  7790. pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_VXLAN;
  7791. break;
  7792. case UDP_TUNNEL_TYPE_GENEVE:
  7793. if (!(pf->flags & I40E_FLAG_GENEVE_OFFLOAD_CAPABLE))
  7794. return;
  7795. pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_NGE;
  7796. break;
  7797. default:
  7798. return;
  7799. }
  7800. /* New port: add it and mark its index in the bitmap */
  7801. pf->udp_ports[next_idx].index = port;
  7802. pf->pending_udp_bitmap |= BIT_ULL(next_idx);
  7803. pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
  7804. }
  7805. /**
  7806. * i40e_udp_tunnel_del - Get notifications about UDP tunnel ports that go away
  7807. * @netdev: This physical port's netdev
  7808. * @ti: Tunnel endpoint information
  7809. **/
  7810. static void i40e_udp_tunnel_del(struct net_device *netdev,
  7811. struct udp_tunnel_info *ti)
  7812. {
  7813. struct i40e_netdev_priv *np = netdev_priv(netdev);
  7814. struct i40e_vsi *vsi = np->vsi;
  7815. struct i40e_pf *pf = vsi->back;
  7816. u16 port = ntohs(ti->port);
  7817. u8 idx;
  7818. idx = i40e_get_udp_port_idx(pf, port);
  7819. /* Check if port already exists */
  7820. if (idx >= I40E_MAX_PF_UDP_OFFLOAD_PORTS)
  7821. goto not_found;
  7822. switch (ti->type) {
  7823. case UDP_TUNNEL_TYPE_VXLAN:
  7824. if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_VXLAN)
  7825. goto not_found;
  7826. break;
  7827. case UDP_TUNNEL_TYPE_GENEVE:
  7828. if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_NGE)
  7829. goto not_found;
  7830. break;
  7831. default:
  7832. goto not_found;
  7833. }
  7834. /* if port exists, set it to 0 (mark for deletion)
  7835. * and make it pending
  7836. */
  7837. pf->udp_ports[idx].index = 0;
  7838. pf->pending_udp_bitmap |= BIT_ULL(idx);
  7839. pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
  7840. return;
  7841. not_found:
  7842. netdev_warn(netdev, "UDP port %d was not found, not deleting\n",
  7843. port);
  7844. }
  7845. static int i40e_get_phys_port_id(struct net_device *netdev,
  7846. struct netdev_phys_item_id *ppid)
  7847. {
  7848. struct i40e_netdev_priv *np = netdev_priv(netdev);
  7849. struct i40e_pf *pf = np->vsi->back;
  7850. struct i40e_hw *hw = &pf->hw;
  7851. if (!(pf->flags & I40E_FLAG_PORT_ID_VALID))
  7852. return -EOPNOTSUPP;
  7853. ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
  7854. memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
  7855. return 0;
  7856. }
  7857. /**
  7858. * i40e_ndo_fdb_add - add an entry to the hardware database
  7859. * @ndm: the input from the stack
  7860. * @tb: pointer to array of nladdr (unused)
  7861. * @dev: the net device pointer
  7862. * @addr: the MAC address entry being added
  7863. * @flags: instructions from stack about fdb operation
  7864. */
  7865. static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
  7866. struct net_device *dev,
  7867. const unsigned char *addr, u16 vid,
  7868. u16 flags)
  7869. {
  7870. struct i40e_netdev_priv *np = netdev_priv(dev);
  7871. struct i40e_pf *pf = np->vsi->back;
  7872. int err = 0;
  7873. if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
  7874. return -EOPNOTSUPP;
  7875. if (vid) {
  7876. pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
  7877. return -EINVAL;
  7878. }
  7879. /* Hardware does not support aging addresses so if a
  7880. * ndm_state is given only allow permanent addresses
  7881. */
  7882. if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
  7883. netdev_info(dev, "FDB only supports static addresses\n");
  7884. return -EINVAL;
  7885. }
  7886. if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
  7887. err = dev_uc_add_excl(dev, addr);
  7888. else if (is_multicast_ether_addr(addr))
  7889. err = dev_mc_add_excl(dev, addr);
  7890. else
  7891. err = -EINVAL;
  7892. /* Only return duplicate errors if NLM_F_EXCL is set */
  7893. if (err == -EEXIST && !(flags & NLM_F_EXCL))
  7894. err = 0;
  7895. return err;
  7896. }
  7897. /**
  7898. * i40e_ndo_bridge_setlink - Set the hardware bridge mode
  7899. * @dev: the netdev being configured
  7900. * @nlh: RTNL message
  7901. *
  7902. * Inserts a new hardware bridge if not already created and
  7903. * enables the bridging mode requested (VEB or VEPA). If the
  7904. * hardware bridge has already been inserted and the request
  7905. * is to change the mode then that requires a PF reset to
  7906. * allow rebuild of the components with required hardware
  7907. * bridge mode enabled.
  7908. **/
  7909. static int i40e_ndo_bridge_setlink(struct net_device *dev,
  7910. struct nlmsghdr *nlh,
  7911. u16 flags)
  7912. {
  7913. struct i40e_netdev_priv *np = netdev_priv(dev);
  7914. struct i40e_vsi *vsi = np->vsi;
  7915. struct i40e_pf *pf = vsi->back;
  7916. struct i40e_veb *veb = NULL;
  7917. struct nlattr *attr, *br_spec;
  7918. int i, rem;
  7919. /* Only for PF VSI for now */
  7920. if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
  7921. return -EOPNOTSUPP;
  7922. /* Find the HW bridge for PF VSI */
  7923. for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
  7924. if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
  7925. veb = pf->veb[i];
  7926. }
  7927. br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
  7928. nla_for_each_nested(attr, br_spec, rem) {
  7929. __u16 mode;
  7930. if (nla_type(attr) != IFLA_BRIDGE_MODE)
  7931. continue;
  7932. mode = nla_get_u16(attr);
  7933. if ((mode != BRIDGE_MODE_VEPA) &&
  7934. (mode != BRIDGE_MODE_VEB))
  7935. return -EINVAL;
  7936. /* Insert a new HW bridge */
  7937. if (!veb) {
  7938. veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
  7939. vsi->tc_config.enabled_tc);
  7940. if (veb) {
  7941. veb->bridge_mode = mode;
  7942. i40e_config_bridge_mode(veb);
  7943. } else {
  7944. /* No Bridge HW offload available */
  7945. return -ENOENT;
  7946. }
  7947. break;
  7948. } else if (mode != veb->bridge_mode) {
  7949. /* Existing HW bridge but different mode needs reset */
  7950. veb->bridge_mode = mode;
  7951. /* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
  7952. if (mode == BRIDGE_MODE_VEB)
  7953. pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
  7954. else
  7955. pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
  7956. i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
  7957. break;
  7958. }
  7959. }
  7960. return 0;
  7961. }
  7962. /**
  7963. * i40e_ndo_bridge_getlink - Get the hardware bridge mode
  7964. * @skb: skb buff
  7965. * @pid: process id
  7966. * @seq: RTNL message seq #
  7967. * @dev: the netdev being configured
  7968. * @filter_mask: unused
  7969. * @nlflags: netlink flags passed in
  7970. *
  7971. * Return the mode in which the hardware bridge is operating in
  7972. * i.e VEB or VEPA.
  7973. **/
  7974. static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
  7975. struct net_device *dev,
  7976. u32 __always_unused filter_mask,
  7977. int nlflags)
  7978. {
  7979. struct i40e_netdev_priv *np = netdev_priv(dev);
  7980. struct i40e_vsi *vsi = np->vsi;
  7981. struct i40e_pf *pf = vsi->back;
  7982. struct i40e_veb *veb = NULL;
  7983. int i;
  7984. /* Only for PF VSI for now */
  7985. if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
  7986. return -EOPNOTSUPP;
  7987. /* Find the HW bridge for the PF VSI */
  7988. for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
  7989. if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
  7990. veb = pf->veb[i];
  7991. }
  7992. if (!veb)
  7993. return 0;
  7994. return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
  7995. 0, 0, nlflags, filter_mask, NULL);
  7996. }
  7997. /**
  7998. * i40e_features_check - Validate encapsulated packet conforms to limits
  7999. * @skb: skb buff
  8000. * @dev: This physical port's netdev
  8001. * @features: Offload features that the stack believes apply
  8002. **/
  8003. static netdev_features_t i40e_features_check(struct sk_buff *skb,
  8004. struct net_device *dev,
  8005. netdev_features_t features)
  8006. {
  8007. size_t len;
  8008. /* No point in doing any of this if neither checksum nor GSO are
  8009. * being requested for this frame. We can rule out both by just
  8010. * checking for CHECKSUM_PARTIAL
  8011. */
  8012. if (skb->ip_summed != CHECKSUM_PARTIAL)
  8013. return features;
  8014. /* We cannot support GSO if the MSS is going to be less than
  8015. * 64 bytes. If it is then we need to drop support for GSO.
  8016. */
  8017. if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
  8018. features &= ~NETIF_F_GSO_MASK;
  8019. /* MACLEN can support at most 63 words */
  8020. len = skb_network_header(skb) - skb->data;
  8021. if (len & ~(63 * 2))
  8022. goto out_err;
  8023. /* IPLEN and EIPLEN can support at most 127 dwords */
  8024. len = skb_transport_header(skb) - skb_network_header(skb);
  8025. if (len & ~(127 * 4))
  8026. goto out_err;
  8027. if (skb->encapsulation) {
  8028. /* L4TUNLEN can support 127 words */
  8029. len = skb_inner_network_header(skb) - skb_transport_header(skb);
  8030. if (len & ~(127 * 2))
  8031. goto out_err;
  8032. /* IPLEN can support at most 127 dwords */
  8033. len = skb_inner_transport_header(skb) -
  8034. skb_inner_network_header(skb);
  8035. if (len & ~(127 * 4))
  8036. goto out_err;
  8037. }
  8038. /* No need to validate L4LEN as TCP is the only protocol with a
  8039. * a flexible value and we support all possible values supported
  8040. * by TCP, which is at most 15 dwords
  8041. */
  8042. return features;
  8043. out_err:
  8044. return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
  8045. }
  8046. static const struct net_device_ops i40e_netdev_ops = {
  8047. .ndo_open = i40e_open,
  8048. .ndo_stop = i40e_close,
  8049. .ndo_start_xmit = i40e_lan_xmit_frame,
  8050. .ndo_get_stats64 = i40e_get_netdev_stats_struct,
  8051. .ndo_set_rx_mode = i40e_set_rx_mode,
  8052. .ndo_validate_addr = eth_validate_addr,
  8053. .ndo_set_mac_address = i40e_set_mac,
  8054. .ndo_change_mtu = i40e_change_mtu,
  8055. .ndo_do_ioctl = i40e_ioctl,
  8056. .ndo_tx_timeout = i40e_tx_timeout,
  8057. .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
  8058. .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
  8059. #ifdef CONFIG_NET_POLL_CONTROLLER
  8060. .ndo_poll_controller = i40e_netpoll,
  8061. #endif
  8062. .ndo_setup_tc = __i40e_setup_tc,
  8063. .ndo_set_features = i40e_set_features,
  8064. .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
  8065. .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
  8066. .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
  8067. .ndo_get_vf_config = i40e_ndo_get_vf_config,
  8068. .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
  8069. .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofchk,
  8070. .ndo_set_vf_trust = i40e_ndo_set_vf_trust,
  8071. .ndo_udp_tunnel_add = i40e_udp_tunnel_add,
  8072. .ndo_udp_tunnel_del = i40e_udp_tunnel_del,
  8073. .ndo_get_phys_port_id = i40e_get_phys_port_id,
  8074. .ndo_fdb_add = i40e_ndo_fdb_add,
  8075. .ndo_features_check = i40e_features_check,
  8076. .ndo_bridge_getlink = i40e_ndo_bridge_getlink,
  8077. .ndo_bridge_setlink = i40e_ndo_bridge_setlink,
  8078. };
  8079. /**
  8080. * i40e_config_netdev - Setup the netdev flags
  8081. * @vsi: the VSI being configured
  8082. *
  8083. * Returns 0 on success, negative value on failure
  8084. **/
  8085. static int i40e_config_netdev(struct i40e_vsi *vsi)
  8086. {
  8087. struct i40e_pf *pf = vsi->back;
  8088. struct i40e_hw *hw = &pf->hw;
  8089. struct i40e_netdev_priv *np;
  8090. struct net_device *netdev;
  8091. u8 broadcast[ETH_ALEN];
  8092. u8 mac_addr[ETH_ALEN];
  8093. int etherdev_size;
  8094. etherdev_size = sizeof(struct i40e_netdev_priv);
  8095. netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
  8096. if (!netdev)
  8097. return -ENOMEM;
  8098. vsi->netdev = netdev;
  8099. np = netdev_priv(netdev);
  8100. np->vsi = vsi;
  8101. netdev->hw_enc_features |= NETIF_F_SG |
  8102. NETIF_F_IP_CSUM |
  8103. NETIF_F_IPV6_CSUM |
  8104. NETIF_F_HIGHDMA |
  8105. NETIF_F_SOFT_FEATURES |
  8106. NETIF_F_TSO |
  8107. NETIF_F_TSO_ECN |
  8108. NETIF_F_TSO6 |
  8109. NETIF_F_GSO_GRE |
  8110. NETIF_F_GSO_GRE_CSUM |
  8111. NETIF_F_GSO_IPXIP4 |
  8112. NETIF_F_GSO_IPXIP6 |
  8113. NETIF_F_GSO_UDP_TUNNEL |
  8114. NETIF_F_GSO_UDP_TUNNEL_CSUM |
  8115. NETIF_F_GSO_PARTIAL |
  8116. NETIF_F_SCTP_CRC |
  8117. NETIF_F_RXHASH |
  8118. NETIF_F_RXCSUM |
  8119. 0;
  8120. if (!(pf->flags & I40E_FLAG_OUTER_UDP_CSUM_CAPABLE))
  8121. netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
  8122. netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
  8123. /* record features VLANs can make use of */
  8124. netdev->vlan_features |= netdev->hw_enc_features |
  8125. NETIF_F_TSO_MANGLEID;
  8126. if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
  8127. netdev->hw_features |= NETIF_F_NTUPLE;
  8128. netdev->hw_features |= netdev->hw_enc_features |
  8129. NETIF_F_HW_VLAN_CTAG_TX |
  8130. NETIF_F_HW_VLAN_CTAG_RX;
  8131. netdev->features |= netdev->hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
  8132. netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
  8133. if (vsi->type == I40E_VSI_MAIN) {
  8134. SET_NETDEV_DEV(netdev, &pf->pdev->dev);
  8135. ether_addr_copy(mac_addr, hw->mac.perm_addr);
  8136. /* The following steps are necessary to properly keep track of
  8137. * MAC-VLAN filters loaded into firmware - first we remove
  8138. * filter that is automatically generated by firmware and then
  8139. * add new filter both to the driver hash table and firmware.
  8140. */
  8141. i40e_rm_default_mac_filter(vsi, mac_addr);
  8142. spin_lock_bh(&vsi->mac_filter_hash_lock);
  8143. i40e_add_mac_filter(vsi, mac_addr);
  8144. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  8145. } else {
  8146. /* relate the VSI_VMDQ name to the VSI_MAIN name */
  8147. snprintf(netdev->name, IFNAMSIZ, "%sv%%d",
  8148. pf->vsi[pf->lan_vsi]->netdev->name);
  8149. random_ether_addr(mac_addr);
  8150. spin_lock_bh(&vsi->mac_filter_hash_lock);
  8151. i40e_add_mac_filter(vsi, mac_addr);
  8152. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  8153. }
  8154. /* Add the broadcast filter so that we initially will receive
  8155. * broadcast packets. Note that when a new VLAN is first added the
  8156. * driver will convert all filters marked I40E_VLAN_ANY into VLAN
  8157. * specific filters as part of transitioning into "vlan" operation.
  8158. * When more VLANs are added, the driver will copy each existing MAC
  8159. * filter and add it for the new VLAN.
  8160. *
  8161. * Broadcast filters are handled specially by
  8162. * i40e_sync_filters_subtask, as the driver must to set the broadcast
  8163. * promiscuous bit instead of adding this directly as a MAC/VLAN
  8164. * filter. The subtask will update the correct broadcast promiscuous
  8165. * bits as VLANs become active or inactive.
  8166. */
  8167. eth_broadcast_addr(broadcast);
  8168. spin_lock_bh(&vsi->mac_filter_hash_lock);
  8169. i40e_add_mac_filter(vsi, broadcast);
  8170. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  8171. ether_addr_copy(netdev->dev_addr, mac_addr);
  8172. ether_addr_copy(netdev->perm_addr, mac_addr);
  8173. netdev->priv_flags |= IFF_UNICAST_FLT;
  8174. netdev->priv_flags |= IFF_SUPP_NOFCS;
  8175. /* Setup netdev TC information */
  8176. i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
  8177. netdev->netdev_ops = &i40e_netdev_ops;
  8178. netdev->watchdog_timeo = 5 * HZ;
  8179. i40e_set_ethtool_ops(netdev);
  8180. /* MTU range: 68 - 9706 */
  8181. netdev->min_mtu = ETH_MIN_MTU;
  8182. netdev->max_mtu = I40E_MAX_RXBUFFER -
  8183. (ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
  8184. return 0;
  8185. }
  8186. /**
  8187. * i40e_vsi_delete - Delete a VSI from the switch
  8188. * @vsi: the VSI being removed
  8189. *
  8190. * Returns 0 on success, negative value on failure
  8191. **/
  8192. static void i40e_vsi_delete(struct i40e_vsi *vsi)
  8193. {
  8194. /* remove default VSI is not allowed */
  8195. if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
  8196. return;
  8197. i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
  8198. }
  8199. /**
  8200. * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
  8201. * @vsi: the VSI being queried
  8202. *
  8203. * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
  8204. **/
  8205. int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
  8206. {
  8207. struct i40e_veb *veb;
  8208. struct i40e_pf *pf = vsi->back;
  8209. /* Uplink is not a bridge so default to VEB */
  8210. if (vsi->veb_idx == I40E_NO_VEB)
  8211. return 1;
  8212. veb = pf->veb[vsi->veb_idx];
  8213. if (!veb) {
  8214. dev_info(&pf->pdev->dev,
  8215. "There is no veb associated with the bridge\n");
  8216. return -ENOENT;
  8217. }
  8218. /* Uplink is a bridge in VEPA mode */
  8219. if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
  8220. return 0;
  8221. } else {
  8222. /* Uplink is a bridge in VEB mode */
  8223. return 1;
  8224. }
  8225. /* VEPA is now default bridge, so return 0 */
  8226. return 0;
  8227. }
  8228. /**
  8229. * i40e_add_vsi - Add a VSI to the switch
  8230. * @vsi: the VSI being configured
  8231. *
  8232. * This initializes a VSI context depending on the VSI type to be added and
  8233. * passes it down to the add_vsi aq command.
  8234. **/
  8235. static int i40e_add_vsi(struct i40e_vsi *vsi)
  8236. {
  8237. int ret = -ENODEV;
  8238. struct i40e_pf *pf = vsi->back;
  8239. struct i40e_hw *hw = &pf->hw;
  8240. struct i40e_vsi_context ctxt;
  8241. struct i40e_mac_filter *f;
  8242. struct hlist_node *h;
  8243. int bkt;
  8244. u8 enabled_tc = 0x1; /* TC0 enabled */
  8245. int f_count = 0;
  8246. memset(&ctxt, 0, sizeof(ctxt));
  8247. switch (vsi->type) {
  8248. case I40E_VSI_MAIN:
  8249. /* The PF's main VSI is already setup as part of the
  8250. * device initialization, so we'll not bother with
  8251. * the add_vsi call, but we will retrieve the current
  8252. * VSI context.
  8253. */
  8254. ctxt.seid = pf->main_vsi_seid;
  8255. ctxt.pf_num = pf->hw.pf_id;
  8256. ctxt.vf_num = 0;
  8257. ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
  8258. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  8259. if (ret) {
  8260. dev_info(&pf->pdev->dev,
  8261. "couldn't get PF vsi config, err %s aq_err %s\n",
  8262. i40e_stat_str(&pf->hw, ret),
  8263. i40e_aq_str(&pf->hw,
  8264. pf->hw.aq.asq_last_status));
  8265. return -ENOENT;
  8266. }
  8267. vsi->info = ctxt.info;
  8268. vsi->info.valid_sections = 0;
  8269. vsi->seid = ctxt.seid;
  8270. vsi->id = ctxt.vsi_number;
  8271. enabled_tc = i40e_pf_get_tc_map(pf);
  8272. /* MFP mode setup queue map and update VSI */
  8273. if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
  8274. !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
  8275. memset(&ctxt, 0, sizeof(ctxt));
  8276. ctxt.seid = pf->main_vsi_seid;
  8277. ctxt.pf_num = pf->hw.pf_id;
  8278. ctxt.vf_num = 0;
  8279. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
  8280. ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
  8281. if (ret) {
  8282. dev_info(&pf->pdev->dev,
  8283. "update vsi failed, err %s aq_err %s\n",
  8284. i40e_stat_str(&pf->hw, ret),
  8285. i40e_aq_str(&pf->hw,
  8286. pf->hw.aq.asq_last_status));
  8287. ret = -ENOENT;
  8288. goto err;
  8289. }
  8290. /* update the local VSI info queue map */
  8291. i40e_vsi_update_queue_map(vsi, &ctxt);
  8292. vsi->info.valid_sections = 0;
  8293. } else {
  8294. /* Default/Main VSI is only enabled for TC0
  8295. * reconfigure it to enable all TCs that are
  8296. * available on the port in SFP mode.
  8297. * For MFP case the iSCSI PF would use this
  8298. * flow to enable LAN+iSCSI TC.
  8299. */
  8300. ret = i40e_vsi_config_tc(vsi, enabled_tc);
  8301. if (ret) {
  8302. dev_info(&pf->pdev->dev,
  8303. "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
  8304. enabled_tc,
  8305. i40e_stat_str(&pf->hw, ret),
  8306. i40e_aq_str(&pf->hw,
  8307. pf->hw.aq.asq_last_status));
  8308. ret = -ENOENT;
  8309. }
  8310. }
  8311. break;
  8312. case I40E_VSI_FDIR:
  8313. ctxt.pf_num = hw->pf_id;
  8314. ctxt.vf_num = 0;
  8315. ctxt.uplink_seid = vsi->uplink_seid;
  8316. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  8317. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  8318. if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
  8319. (i40e_is_vsi_uplink_mode_veb(vsi))) {
  8320. ctxt.info.valid_sections |=
  8321. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  8322. ctxt.info.switch_id =
  8323. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  8324. }
  8325. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
  8326. break;
  8327. case I40E_VSI_VMDQ2:
  8328. ctxt.pf_num = hw->pf_id;
  8329. ctxt.vf_num = 0;
  8330. ctxt.uplink_seid = vsi->uplink_seid;
  8331. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  8332. ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
  8333. /* This VSI is connected to VEB so the switch_id
  8334. * should be set to zero by default.
  8335. */
  8336. if (i40e_is_vsi_uplink_mode_veb(vsi)) {
  8337. ctxt.info.valid_sections |=
  8338. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  8339. ctxt.info.switch_id =
  8340. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  8341. }
  8342. /* Setup the VSI tx/rx queue map for TC0 only for now */
  8343. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
  8344. break;
  8345. case I40E_VSI_SRIOV:
  8346. ctxt.pf_num = hw->pf_id;
  8347. ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
  8348. ctxt.uplink_seid = vsi->uplink_seid;
  8349. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  8350. ctxt.flags = I40E_AQ_VSI_TYPE_VF;
  8351. /* This VSI is connected to VEB so the switch_id
  8352. * should be set to zero by default.
  8353. */
  8354. if (i40e_is_vsi_uplink_mode_veb(vsi)) {
  8355. ctxt.info.valid_sections |=
  8356. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  8357. ctxt.info.switch_id =
  8358. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  8359. }
  8360. if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
  8361. ctxt.info.valid_sections |=
  8362. cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
  8363. ctxt.info.queueing_opt_flags |=
  8364. (I40E_AQ_VSI_QUE_OPT_TCP_ENA |
  8365. I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
  8366. }
  8367. ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  8368. ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
  8369. if (pf->vf[vsi->vf_id].spoofchk) {
  8370. ctxt.info.valid_sections |=
  8371. cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
  8372. ctxt.info.sec_flags |=
  8373. (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
  8374. I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
  8375. }
  8376. /* Setup the VSI tx/rx queue map for TC0 only for now */
  8377. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
  8378. break;
  8379. case I40E_VSI_IWARP:
  8380. /* send down message to iWARP */
  8381. break;
  8382. default:
  8383. return -ENODEV;
  8384. }
  8385. if (vsi->type != I40E_VSI_MAIN) {
  8386. ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
  8387. if (ret) {
  8388. dev_info(&vsi->back->pdev->dev,
  8389. "add vsi failed, err %s aq_err %s\n",
  8390. i40e_stat_str(&pf->hw, ret),
  8391. i40e_aq_str(&pf->hw,
  8392. pf->hw.aq.asq_last_status));
  8393. ret = -ENOENT;
  8394. goto err;
  8395. }
  8396. vsi->info = ctxt.info;
  8397. vsi->info.valid_sections = 0;
  8398. vsi->seid = ctxt.seid;
  8399. vsi->id = ctxt.vsi_number;
  8400. }
  8401. vsi->active_filters = 0;
  8402. clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
  8403. spin_lock_bh(&vsi->mac_filter_hash_lock);
  8404. /* If macvlan filters already exist, force them to get loaded */
  8405. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  8406. f->state = I40E_FILTER_NEW;
  8407. f_count++;
  8408. }
  8409. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  8410. if (f_count) {
  8411. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  8412. pf->flags |= I40E_FLAG_FILTER_SYNC;
  8413. }
  8414. /* Update VSI BW information */
  8415. ret = i40e_vsi_get_bw_info(vsi);
  8416. if (ret) {
  8417. dev_info(&pf->pdev->dev,
  8418. "couldn't get vsi bw info, err %s aq_err %s\n",
  8419. i40e_stat_str(&pf->hw, ret),
  8420. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  8421. /* VSI is already added so not tearing that up */
  8422. ret = 0;
  8423. }
  8424. err:
  8425. return ret;
  8426. }
  8427. /**
  8428. * i40e_vsi_release - Delete a VSI and free its resources
  8429. * @vsi: the VSI being removed
  8430. *
  8431. * Returns 0 on success or < 0 on error
  8432. **/
  8433. int i40e_vsi_release(struct i40e_vsi *vsi)
  8434. {
  8435. struct i40e_mac_filter *f;
  8436. struct hlist_node *h;
  8437. struct i40e_veb *veb = NULL;
  8438. struct i40e_pf *pf;
  8439. u16 uplink_seid;
  8440. int i, n, bkt;
  8441. pf = vsi->back;
  8442. /* release of a VEB-owner or last VSI is not allowed */
  8443. if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
  8444. dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
  8445. vsi->seid, vsi->uplink_seid);
  8446. return -ENODEV;
  8447. }
  8448. if (vsi == pf->vsi[pf->lan_vsi] &&
  8449. !test_bit(__I40E_DOWN, &pf->state)) {
  8450. dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
  8451. return -ENODEV;
  8452. }
  8453. uplink_seid = vsi->uplink_seid;
  8454. if (vsi->type != I40E_VSI_SRIOV) {
  8455. if (vsi->netdev_registered) {
  8456. vsi->netdev_registered = false;
  8457. if (vsi->netdev) {
  8458. /* results in a call to i40e_close() */
  8459. unregister_netdev(vsi->netdev);
  8460. }
  8461. } else {
  8462. i40e_vsi_close(vsi);
  8463. }
  8464. i40e_vsi_disable_irq(vsi);
  8465. }
  8466. spin_lock_bh(&vsi->mac_filter_hash_lock);
  8467. /* clear the sync flag on all filters */
  8468. if (vsi->netdev) {
  8469. __dev_uc_unsync(vsi->netdev, NULL);
  8470. __dev_mc_unsync(vsi->netdev, NULL);
  8471. }
  8472. /* make sure any remaining filters are marked for deletion */
  8473. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
  8474. __i40e_del_filter(vsi, f);
  8475. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  8476. i40e_sync_vsi_filters(vsi);
  8477. i40e_vsi_delete(vsi);
  8478. i40e_vsi_free_q_vectors(vsi);
  8479. if (vsi->netdev) {
  8480. free_netdev(vsi->netdev);
  8481. vsi->netdev = NULL;
  8482. }
  8483. i40e_vsi_clear_rings(vsi);
  8484. i40e_vsi_clear(vsi);
  8485. /* If this was the last thing on the VEB, except for the
  8486. * controlling VSI, remove the VEB, which puts the controlling
  8487. * VSI onto the next level down in the switch.
  8488. *
  8489. * Well, okay, there's one more exception here: don't remove
  8490. * the orphan VEBs yet. We'll wait for an explicit remove request
  8491. * from up the network stack.
  8492. */
  8493. for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
  8494. if (pf->vsi[i] &&
  8495. pf->vsi[i]->uplink_seid == uplink_seid &&
  8496. (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
  8497. n++; /* count the VSIs */
  8498. }
  8499. }
  8500. for (i = 0; i < I40E_MAX_VEB; i++) {
  8501. if (!pf->veb[i])
  8502. continue;
  8503. if (pf->veb[i]->uplink_seid == uplink_seid)
  8504. n++; /* count the VEBs */
  8505. if (pf->veb[i]->seid == uplink_seid)
  8506. veb = pf->veb[i];
  8507. }
  8508. if (n == 0 && veb && veb->uplink_seid != 0)
  8509. i40e_veb_release(veb);
  8510. return 0;
  8511. }
  8512. /**
  8513. * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
  8514. * @vsi: ptr to the VSI
  8515. *
  8516. * This should only be called after i40e_vsi_mem_alloc() which allocates the
  8517. * corresponding SW VSI structure and initializes num_queue_pairs for the
  8518. * newly allocated VSI.
  8519. *
  8520. * Returns 0 on success or negative on failure
  8521. **/
  8522. static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
  8523. {
  8524. int ret = -ENOENT;
  8525. struct i40e_pf *pf = vsi->back;
  8526. if (vsi->q_vectors[0]) {
  8527. dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
  8528. vsi->seid);
  8529. return -EEXIST;
  8530. }
  8531. if (vsi->base_vector) {
  8532. dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
  8533. vsi->seid, vsi->base_vector);
  8534. return -EEXIST;
  8535. }
  8536. ret = i40e_vsi_alloc_q_vectors(vsi);
  8537. if (ret) {
  8538. dev_info(&pf->pdev->dev,
  8539. "failed to allocate %d q_vector for VSI %d, ret=%d\n",
  8540. vsi->num_q_vectors, vsi->seid, ret);
  8541. vsi->num_q_vectors = 0;
  8542. goto vector_setup_out;
  8543. }
  8544. /* In Legacy mode, we do not have to get any other vector since we
  8545. * piggyback on the misc/ICR0 for queue interrupts.
  8546. */
  8547. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
  8548. return ret;
  8549. if (vsi->num_q_vectors)
  8550. vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
  8551. vsi->num_q_vectors, vsi->idx);
  8552. if (vsi->base_vector < 0) {
  8553. dev_info(&pf->pdev->dev,
  8554. "failed to get tracking for %d vectors for VSI %d, err=%d\n",
  8555. vsi->num_q_vectors, vsi->seid, vsi->base_vector);
  8556. i40e_vsi_free_q_vectors(vsi);
  8557. ret = -ENOENT;
  8558. goto vector_setup_out;
  8559. }
  8560. vector_setup_out:
  8561. return ret;
  8562. }
  8563. /**
  8564. * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
  8565. * @vsi: pointer to the vsi.
  8566. *
  8567. * This re-allocates a vsi's queue resources.
  8568. *
  8569. * Returns pointer to the successfully allocated and configured VSI sw struct
  8570. * on success, otherwise returns NULL on failure.
  8571. **/
  8572. static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
  8573. {
  8574. struct i40e_pf *pf;
  8575. u8 enabled_tc;
  8576. int ret;
  8577. if (!vsi)
  8578. return NULL;
  8579. pf = vsi->back;
  8580. i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
  8581. i40e_vsi_clear_rings(vsi);
  8582. i40e_vsi_free_arrays(vsi, false);
  8583. i40e_set_num_rings_in_vsi(vsi);
  8584. ret = i40e_vsi_alloc_arrays(vsi, false);
  8585. if (ret)
  8586. goto err_vsi;
  8587. ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs, vsi->idx);
  8588. if (ret < 0) {
  8589. dev_info(&pf->pdev->dev,
  8590. "failed to get tracking for %d queues for VSI %d err %d\n",
  8591. vsi->alloc_queue_pairs, vsi->seid, ret);
  8592. goto err_vsi;
  8593. }
  8594. vsi->base_queue = ret;
  8595. /* Update the FW view of the VSI. Force a reset of TC and queue
  8596. * layout configurations.
  8597. */
  8598. enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
  8599. pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
  8600. pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
  8601. i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
  8602. if (vsi->type == I40E_VSI_MAIN)
  8603. i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
  8604. /* assign it some queues */
  8605. ret = i40e_alloc_rings(vsi);
  8606. if (ret)
  8607. goto err_rings;
  8608. /* map all of the rings to the q_vectors */
  8609. i40e_vsi_map_rings_to_vectors(vsi);
  8610. return vsi;
  8611. err_rings:
  8612. i40e_vsi_free_q_vectors(vsi);
  8613. if (vsi->netdev_registered) {
  8614. vsi->netdev_registered = false;
  8615. unregister_netdev(vsi->netdev);
  8616. free_netdev(vsi->netdev);
  8617. vsi->netdev = NULL;
  8618. }
  8619. i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
  8620. err_vsi:
  8621. i40e_vsi_clear(vsi);
  8622. return NULL;
  8623. }
  8624. /**
  8625. * i40e_vsi_setup - Set up a VSI by a given type
  8626. * @pf: board private structure
  8627. * @type: VSI type
  8628. * @uplink_seid: the switch element to link to
  8629. * @param1: usage depends upon VSI type. For VF types, indicates VF id
  8630. *
  8631. * This allocates the sw VSI structure and its queue resources, then add a VSI
  8632. * to the identified VEB.
  8633. *
  8634. * Returns pointer to the successfully allocated and configure VSI sw struct on
  8635. * success, otherwise returns NULL on failure.
  8636. **/
  8637. struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
  8638. u16 uplink_seid, u32 param1)
  8639. {
  8640. struct i40e_vsi *vsi = NULL;
  8641. struct i40e_veb *veb = NULL;
  8642. int ret, i;
  8643. int v_idx;
  8644. /* The requested uplink_seid must be either
  8645. * - the PF's port seid
  8646. * no VEB is needed because this is the PF
  8647. * or this is a Flow Director special case VSI
  8648. * - seid of an existing VEB
  8649. * - seid of a VSI that owns an existing VEB
  8650. * - seid of a VSI that doesn't own a VEB
  8651. * a new VEB is created and the VSI becomes the owner
  8652. * - seid of the PF VSI, which is what creates the first VEB
  8653. * this is a special case of the previous
  8654. *
  8655. * Find which uplink_seid we were given and create a new VEB if needed
  8656. */
  8657. for (i = 0; i < I40E_MAX_VEB; i++) {
  8658. if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
  8659. veb = pf->veb[i];
  8660. break;
  8661. }
  8662. }
  8663. if (!veb && uplink_seid != pf->mac_seid) {
  8664. for (i = 0; i < pf->num_alloc_vsi; i++) {
  8665. if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
  8666. vsi = pf->vsi[i];
  8667. break;
  8668. }
  8669. }
  8670. if (!vsi) {
  8671. dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
  8672. uplink_seid);
  8673. return NULL;
  8674. }
  8675. if (vsi->uplink_seid == pf->mac_seid)
  8676. veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
  8677. vsi->tc_config.enabled_tc);
  8678. else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
  8679. veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
  8680. vsi->tc_config.enabled_tc);
  8681. if (veb) {
  8682. if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
  8683. dev_info(&vsi->back->pdev->dev,
  8684. "New VSI creation error, uplink seid of LAN VSI expected.\n");
  8685. return NULL;
  8686. }
  8687. /* We come up by default in VEPA mode if SRIOV is not
  8688. * already enabled, in which case we can't force VEPA
  8689. * mode.
  8690. */
  8691. if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
  8692. veb->bridge_mode = BRIDGE_MODE_VEPA;
  8693. pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
  8694. }
  8695. i40e_config_bridge_mode(veb);
  8696. }
  8697. for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
  8698. if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
  8699. veb = pf->veb[i];
  8700. }
  8701. if (!veb) {
  8702. dev_info(&pf->pdev->dev, "couldn't add VEB\n");
  8703. return NULL;
  8704. }
  8705. vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
  8706. uplink_seid = veb->seid;
  8707. }
  8708. /* get vsi sw struct */
  8709. v_idx = i40e_vsi_mem_alloc(pf, type);
  8710. if (v_idx < 0)
  8711. goto err_alloc;
  8712. vsi = pf->vsi[v_idx];
  8713. if (!vsi)
  8714. goto err_alloc;
  8715. vsi->type = type;
  8716. vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
  8717. if (type == I40E_VSI_MAIN)
  8718. pf->lan_vsi = v_idx;
  8719. else if (type == I40E_VSI_SRIOV)
  8720. vsi->vf_id = param1;
  8721. /* assign it some queues */
  8722. ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs,
  8723. vsi->idx);
  8724. if (ret < 0) {
  8725. dev_info(&pf->pdev->dev,
  8726. "failed to get tracking for %d queues for VSI %d err=%d\n",
  8727. vsi->alloc_queue_pairs, vsi->seid, ret);
  8728. goto err_vsi;
  8729. }
  8730. vsi->base_queue = ret;
  8731. /* get a VSI from the hardware */
  8732. vsi->uplink_seid = uplink_seid;
  8733. ret = i40e_add_vsi(vsi);
  8734. if (ret)
  8735. goto err_vsi;
  8736. switch (vsi->type) {
  8737. /* setup the netdev if needed */
  8738. case I40E_VSI_MAIN:
  8739. /* Apply relevant filters if a platform-specific mac
  8740. * address was selected.
  8741. */
  8742. if (!!(pf->flags & I40E_FLAG_PF_MAC)) {
  8743. ret = i40e_macaddr_init(vsi, pf->hw.mac.addr);
  8744. if (ret) {
  8745. dev_warn(&pf->pdev->dev,
  8746. "could not set up macaddr; err %d\n",
  8747. ret);
  8748. }
  8749. }
  8750. case I40E_VSI_VMDQ2:
  8751. ret = i40e_config_netdev(vsi);
  8752. if (ret)
  8753. goto err_netdev;
  8754. ret = register_netdev(vsi->netdev);
  8755. if (ret)
  8756. goto err_netdev;
  8757. vsi->netdev_registered = true;
  8758. netif_carrier_off(vsi->netdev);
  8759. #ifdef CONFIG_I40E_DCB
  8760. /* Setup DCB netlink interface */
  8761. i40e_dcbnl_setup(vsi);
  8762. #endif /* CONFIG_I40E_DCB */
  8763. /* fall through */
  8764. case I40E_VSI_FDIR:
  8765. /* set up vectors and rings if needed */
  8766. ret = i40e_vsi_setup_vectors(vsi);
  8767. if (ret)
  8768. goto err_msix;
  8769. ret = i40e_alloc_rings(vsi);
  8770. if (ret)
  8771. goto err_rings;
  8772. /* map all of the rings to the q_vectors */
  8773. i40e_vsi_map_rings_to_vectors(vsi);
  8774. i40e_vsi_reset_stats(vsi);
  8775. break;
  8776. default:
  8777. /* no netdev or rings for the other VSI types */
  8778. break;
  8779. }
  8780. if ((pf->flags & I40E_FLAG_RSS_AQ_CAPABLE) &&
  8781. (vsi->type == I40E_VSI_VMDQ2)) {
  8782. ret = i40e_vsi_config_rss(vsi);
  8783. }
  8784. return vsi;
  8785. err_rings:
  8786. i40e_vsi_free_q_vectors(vsi);
  8787. err_msix:
  8788. if (vsi->netdev_registered) {
  8789. vsi->netdev_registered = false;
  8790. unregister_netdev(vsi->netdev);
  8791. free_netdev(vsi->netdev);
  8792. vsi->netdev = NULL;
  8793. }
  8794. err_netdev:
  8795. i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
  8796. err_vsi:
  8797. i40e_vsi_clear(vsi);
  8798. err_alloc:
  8799. return NULL;
  8800. }
  8801. /**
  8802. * i40e_veb_get_bw_info - Query VEB BW information
  8803. * @veb: the veb to query
  8804. *
  8805. * Query the Tx scheduler BW configuration data for given VEB
  8806. **/
  8807. static int i40e_veb_get_bw_info(struct i40e_veb *veb)
  8808. {
  8809. struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
  8810. struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
  8811. struct i40e_pf *pf = veb->pf;
  8812. struct i40e_hw *hw = &pf->hw;
  8813. u32 tc_bw_max;
  8814. int ret = 0;
  8815. int i;
  8816. ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
  8817. &bw_data, NULL);
  8818. if (ret) {
  8819. dev_info(&pf->pdev->dev,
  8820. "query veb bw config failed, err %s aq_err %s\n",
  8821. i40e_stat_str(&pf->hw, ret),
  8822. i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
  8823. goto out;
  8824. }
  8825. ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
  8826. &ets_data, NULL);
  8827. if (ret) {
  8828. dev_info(&pf->pdev->dev,
  8829. "query veb bw ets config failed, err %s aq_err %s\n",
  8830. i40e_stat_str(&pf->hw, ret),
  8831. i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
  8832. goto out;
  8833. }
  8834. veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
  8835. veb->bw_max_quanta = ets_data.tc_bw_max;
  8836. veb->is_abs_credits = bw_data.absolute_credits_enable;
  8837. veb->enabled_tc = ets_data.tc_valid_bits;
  8838. tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
  8839. (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
  8840. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  8841. veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
  8842. veb->bw_tc_limit_credits[i] =
  8843. le16_to_cpu(bw_data.tc_bw_limits[i]);
  8844. veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
  8845. }
  8846. out:
  8847. return ret;
  8848. }
  8849. /**
  8850. * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
  8851. * @pf: board private structure
  8852. *
  8853. * On error: returns error code (negative)
  8854. * On success: returns vsi index in PF (positive)
  8855. **/
  8856. static int i40e_veb_mem_alloc(struct i40e_pf *pf)
  8857. {
  8858. int ret = -ENOENT;
  8859. struct i40e_veb *veb;
  8860. int i;
  8861. /* Need to protect the allocation of switch elements at the PF level */
  8862. mutex_lock(&pf->switch_mutex);
  8863. /* VEB list may be fragmented if VEB creation/destruction has
  8864. * been happening. We can afford to do a quick scan to look
  8865. * for any free slots in the list.
  8866. *
  8867. * find next empty veb slot, looping back around if necessary
  8868. */
  8869. i = 0;
  8870. while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
  8871. i++;
  8872. if (i >= I40E_MAX_VEB) {
  8873. ret = -ENOMEM;
  8874. goto err_alloc_veb; /* out of VEB slots! */
  8875. }
  8876. veb = kzalloc(sizeof(*veb), GFP_KERNEL);
  8877. if (!veb) {
  8878. ret = -ENOMEM;
  8879. goto err_alloc_veb;
  8880. }
  8881. veb->pf = pf;
  8882. veb->idx = i;
  8883. veb->enabled_tc = 1;
  8884. pf->veb[i] = veb;
  8885. ret = i;
  8886. err_alloc_veb:
  8887. mutex_unlock(&pf->switch_mutex);
  8888. return ret;
  8889. }
  8890. /**
  8891. * i40e_switch_branch_release - Delete a branch of the switch tree
  8892. * @branch: where to start deleting
  8893. *
  8894. * This uses recursion to find the tips of the branch to be
  8895. * removed, deleting until we get back to and can delete this VEB.
  8896. **/
  8897. static void i40e_switch_branch_release(struct i40e_veb *branch)
  8898. {
  8899. struct i40e_pf *pf = branch->pf;
  8900. u16 branch_seid = branch->seid;
  8901. u16 veb_idx = branch->idx;
  8902. int i;
  8903. /* release any VEBs on this VEB - RECURSION */
  8904. for (i = 0; i < I40E_MAX_VEB; i++) {
  8905. if (!pf->veb[i])
  8906. continue;
  8907. if (pf->veb[i]->uplink_seid == branch->seid)
  8908. i40e_switch_branch_release(pf->veb[i]);
  8909. }
  8910. /* Release the VSIs on this VEB, but not the owner VSI.
  8911. *
  8912. * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
  8913. * the VEB itself, so don't use (*branch) after this loop.
  8914. */
  8915. for (i = 0; i < pf->num_alloc_vsi; i++) {
  8916. if (!pf->vsi[i])
  8917. continue;
  8918. if (pf->vsi[i]->uplink_seid == branch_seid &&
  8919. (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
  8920. i40e_vsi_release(pf->vsi[i]);
  8921. }
  8922. }
  8923. /* There's one corner case where the VEB might not have been
  8924. * removed, so double check it here and remove it if needed.
  8925. * This case happens if the veb was created from the debugfs
  8926. * commands and no VSIs were added to it.
  8927. */
  8928. if (pf->veb[veb_idx])
  8929. i40e_veb_release(pf->veb[veb_idx]);
  8930. }
  8931. /**
  8932. * i40e_veb_clear - remove veb struct
  8933. * @veb: the veb to remove
  8934. **/
  8935. static void i40e_veb_clear(struct i40e_veb *veb)
  8936. {
  8937. if (!veb)
  8938. return;
  8939. if (veb->pf) {
  8940. struct i40e_pf *pf = veb->pf;
  8941. mutex_lock(&pf->switch_mutex);
  8942. if (pf->veb[veb->idx] == veb)
  8943. pf->veb[veb->idx] = NULL;
  8944. mutex_unlock(&pf->switch_mutex);
  8945. }
  8946. kfree(veb);
  8947. }
  8948. /**
  8949. * i40e_veb_release - Delete a VEB and free its resources
  8950. * @veb: the VEB being removed
  8951. **/
  8952. void i40e_veb_release(struct i40e_veb *veb)
  8953. {
  8954. struct i40e_vsi *vsi = NULL;
  8955. struct i40e_pf *pf;
  8956. int i, n = 0;
  8957. pf = veb->pf;
  8958. /* find the remaining VSI and check for extras */
  8959. for (i = 0; i < pf->num_alloc_vsi; i++) {
  8960. if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
  8961. n++;
  8962. vsi = pf->vsi[i];
  8963. }
  8964. }
  8965. if (n != 1) {
  8966. dev_info(&pf->pdev->dev,
  8967. "can't remove VEB %d with %d VSIs left\n",
  8968. veb->seid, n);
  8969. return;
  8970. }
  8971. /* move the remaining VSI to uplink veb */
  8972. vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
  8973. if (veb->uplink_seid) {
  8974. vsi->uplink_seid = veb->uplink_seid;
  8975. if (veb->uplink_seid == pf->mac_seid)
  8976. vsi->veb_idx = I40E_NO_VEB;
  8977. else
  8978. vsi->veb_idx = veb->veb_idx;
  8979. } else {
  8980. /* floating VEB */
  8981. vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
  8982. vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
  8983. }
  8984. i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
  8985. i40e_veb_clear(veb);
  8986. }
  8987. /**
  8988. * i40e_add_veb - create the VEB in the switch
  8989. * @veb: the VEB to be instantiated
  8990. * @vsi: the controlling VSI
  8991. **/
  8992. static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
  8993. {
  8994. struct i40e_pf *pf = veb->pf;
  8995. bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
  8996. int ret;
  8997. ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
  8998. veb->enabled_tc, false,
  8999. &veb->seid, enable_stats, NULL);
  9000. /* get a VEB from the hardware */
  9001. if (ret) {
  9002. dev_info(&pf->pdev->dev,
  9003. "couldn't add VEB, err %s aq_err %s\n",
  9004. i40e_stat_str(&pf->hw, ret),
  9005. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  9006. return -EPERM;
  9007. }
  9008. /* get statistics counter */
  9009. ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
  9010. &veb->stats_idx, NULL, NULL, NULL);
  9011. if (ret) {
  9012. dev_info(&pf->pdev->dev,
  9013. "couldn't get VEB statistics idx, err %s aq_err %s\n",
  9014. i40e_stat_str(&pf->hw, ret),
  9015. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  9016. return -EPERM;
  9017. }
  9018. ret = i40e_veb_get_bw_info(veb);
  9019. if (ret) {
  9020. dev_info(&pf->pdev->dev,
  9021. "couldn't get VEB bw info, err %s aq_err %s\n",
  9022. i40e_stat_str(&pf->hw, ret),
  9023. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  9024. i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
  9025. return -ENOENT;
  9026. }
  9027. vsi->uplink_seid = veb->seid;
  9028. vsi->veb_idx = veb->idx;
  9029. vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
  9030. return 0;
  9031. }
  9032. /**
  9033. * i40e_veb_setup - Set up a VEB
  9034. * @pf: board private structure
  9035. * @flags: VEB setup flags
  9036. * @uplink_seid: the switch element to link to
  9037. * @vsi_seid: the initial VSI seid
  9038. * @enabled_tc: Enabled TC bit-map
  9039. *
  9040. * This allocates the sw VEB structure and links it into the switch
  9041. * It is possible and legal for this to be a duplicate of an already
  9042. * existing VEB. It is also possible for both uplink and vsi seids
  9043. * to be zero, in order to create a floating VEB.
  9044. *
  9045. * Returns pointer to the successfully allocated VEB sw struct on
  9046. * success, otherwise returns NULL on failure.
  9047. **/
  9048. struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
  9049. u16 uplink_seid, u16 vsi_seid,
  9050. u8 enabled_tc)
  9051. {
  9052. struct i40e_veb *veb, *uplink_veb = NULL;
  9053. int vsi_idx, veb_idx;
  9054. int ret;
  9055. /* if one seid is 0, the other must be 0 to create a floating relay */
  9056. if ((uplink_seid == 0 || vsi_seid == 0) &&
  9057. (uplink_seid + vsi_seid != 0)) {
  9058. dev_info(&pf->pdev->dev,
  9059. "one, not both seid's are 0: uplink=%d vsi=%d\n",
  9060. uplink_seid, vsi_seid);
  9061. return NULL;
  9062. }
  9063. /* make sure there is such a vsi and uplink */
  9064. for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
  9065. if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
  9066. break;
  9067. if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) {
  9068. dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
  9069. vsi_seid);
  9070. return NULL;
  9071. }
  9072. if (uplink_seid && uplink_seid != pf->mac_seid) {
  9073. for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
  9074. if (pf->veb[veb_idx] &&
  9075. pf->veb[veb_idx]->seid == uplink_seid) {
  9076. uplink_veb = pf->veb[veb_idx];
  9077. break;
  9078. }
  9079. }
  9080. if (!uplink_veb) {
  9081. dev_info(&pf->pdev->dev,
  9082. "uplink seid %d not found\n", uplink_seid);
  9083. return NULL;
  9084. }
  9085. }
  9086. /* get veb sw struct */
  9087. veb_idx = i40e_veb_mem_alloc(pf);
  9088. if (veb_idx < 0)
  9089. goto err_alloc;
  9090. veb = pf->veb[veb_idx];
  9091. veb->flags = flags;
  9092. veb->uplink_seid = uplink_seid;
  9093. veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
  9094. veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
  9095. /* create the VEB in the switch */
  9096. ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
  9097. if (ret)
  9098. goto err_veb;
  9099. if (vsi_idx == pf->lan_vsi)
  9100. pf->lan_veb = veb->idx;
  9101. return veb;
  9102. err_veb:
  9103. i40e_veb_clear(veb);
  9104. err_alloc:
  9105. return NULL;
  9106. }
  9107. /**
  9108. * i40e_setup_pf_switch_element - set PF vars based on switch type
  9109. * @pf: board private structure
  9110. * @ele: element we are building info from
  9111. * @num_reported: total number of elements
  9112. * @printconfig: should we print the contents
  9113. *
  9114. * helper function to assist in extracting a few useful SEID values.
  9115. **/
  9116. static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
  9117. struct i40e_aqc_switch_config_element_resp *ele,
  9118. u16 num_reported, bool printconfig)
  9119. {
  9120. u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
  9121. u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
  9122. u8 element_type = ele->element_type;
  9123. u16 seid = le16_to_cpu(ele->seid);
  9124. if (printconfig)
  9125. dev_info(&pf->pdev->dev,
  9126. "type=%d seid=%d uplink=%d downlink=%d\n",
  9127. element_type, seid, uplink_seid, downlink_seid);
  9128. switch (element_type) {
  9129. case I40E_SWITCH_ELEMENT_TYPE_MAC:
  9130. pf->mac_seid = seid;
  9131. break;
  9132. case I40E_SWITCH_ELEMENT_TYPE_VEB:
  9133. /* Main VEB? */
  9134. if (uplink_seid != pf->mac_seid)
  9135. break;
  9136. if (pf->lan_veb == I40E_NO_VEB) {
  9137. int v;
  9138. /* find existing or else empty VEB */
  9139. for (v = 0; v < I40E_MAX_VEB; v++) {
  9140. if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
  9141. pf->lan_veb = v;
  9142. break;
  9143. }
  9144. }
  9145. if (pf->lan_veb == I40E_NO_VEB) {
  9146. v = i40e_veb_mem_alloc(pf);
  9147. if (v < 0)
  9148. break;
  9149. pf->lan_veb = v;
  9150. }
  9151. }
  9152. pf->veb[pf->lan_veb]->seid = seid;
  9153. pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
  9154. pf->veb[pf->lan_veb]->pf = pf;
  9155. pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
  9156. break;
  9157. case I40E_SWITCH_ELEMENT_TYPE_VSI:
  9158. if (num_reported != 1)
  9159. break;
  9160. /* This is immediately after a reset so we can assume this is
  9161. * the PF's VSI
  9162. */
  9163. pf->mac_seid = uplink_seid;
  9164. pf->pf_seid = downlink_seid;
  9165. pf->main_vsi_seid = seid;
  9166. if (printconfig)
  9167. dev_info(&pf->pdev->dev,
  9168. "pf_seid=%d main_vsi_seid=%d\n",
  9169. pf->pf_seid, pf->main_vsi_seid);
  9170. break;
  9171. case I40E_SWITCH_ELEMENT_TYPE_PF:
  9172. case I40E_SWITCH_ELEMENT_TYPE_VF:
  9173. case I40E_SWITCH_ELEMENT_TYPE_EMP:
  9174. case I40E_SWITCH_ELEMENT_TYPE_BMC:
  9175. case I40E_SWITCH_ELEMENT_TYPE_PE:
  9176. case I40E_SWITCH_ELEMENT_TYPE_PA:
  9177. /* ignore these for now */
  9178. break;
  9179. default:
  9180. dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
  9181. element_type, seid);
  9182. break;
  9183. }
  9184. }
  9185. /**
  9186. * i40e_fetch_switch_configuration - Get switch config from firmware
  9187. * @pf: board private structure
  9188. * @printconfig: should we print the contents
  9189. *
  9190. * Get the current switch configuration from the device and
  9191. * extract a few useful SEID values.
  9192. **/
  9193. int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
  9194. {
  9195. struct i40e_aqc_get_switch_config_resp *sw_config;
  9196. u16 next_seid = 0;
  9197. int ret = 0;
  9198. u8 *aq_buf;
  9199. int i;
  9200. aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
  9201. if (!aq_buf)
  9202. return -ENOMEM;
  9203. sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
  9204. do {
  9205. u16 num_reported, num_total;
  9206. ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
  9207. I40E_AQ_LARGE_BUF,
  9208. &next_seid, NULL);
  9209. if (ret) {
  9210. dev_info(&pf->pdev->dev,
  9211. "get switch config failed err %s aq_err %s\n",
  9212. i40e_stat_str(&pf->hw, ret),
  9213. i40e_aq_str(&pf->hw,
  9214. pf->hw.aq.asq_last_status));
  9215. kfree(aq_buf);
  9216. return -ENOENT;
  9217. }
  9218. num_reported = le16_to_cpu(sw_config->header.num_reported);
  9219. num_total = le16_to_cpu(sw_config->header.num_total);
  9220. if (printconfig)
  9221. dev_info(&pf->pdev->dev,
  9222. "header: %d reported %d total\n",
  9223. num_reported, num_total);
  9224. for (i = 0; i < num_reported; i++) {
  9225. struct i40e_aqc_switch_config_element_resp *ele =
  9226. &sw_config->element[i];
  9227. i40e_setup_pf_switch_element(pf, ele, num_reported,
  9228. printconfig);
  9229. }
  9230. } while (next_seid != 0);
  9231. kfree(aq_buf);
  9232. return ret;
  9233. }
  9234. /**
  9235. * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
  9236. * @pf: board private structure
  9237. * @reinit: if the Main VSI needs to re-initialized.
  9238. *
  9239. * Returns 0 on success, negative value on failure
  9240. **/
  9241. static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
  9242. {
  9243. u16 flags = 0;
  9244. int ret;
  9245. /* find out what's out there already */
  9246. ret = i40e_fetch_switch_configuration(pf, false);
  9247. if (ret) {
  9248. dev_info(&pf->pdev->dev,
  9249. "couldn't fetch switch config, err %s aq_err %s\n",
  9250. i40e_stat_str(&pf->hw, ret),
  9251. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  9252. return ret;
  9253. }
  9254. i40e_pf_reset_stats(pf);
  9255. /* set the switch config bit for the whole device to
  9256. * support limited promisc or true promisc
  9257. * when user requests promisc. The default is limited
  9258. * promisc.
  9259. */
  9260. if ((pf->hw.pf_id == 0) &&
  9261. !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT))
  9262. flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
  9263. if (pf->hw.pf_id == 0) {
  9264. u16 valid_flags;
  9265. valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
  9266. ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags,
  9267. NULL);
  9268. if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
  9269. dev_info(&pf->pdev->dev,
  9270. "couldn't set switch config bits, err %s aq_err %s\n",
  9271. i40e_stat_str(&pf->hw, ret),
  9272. i40e_aq_str(&pf->hw,
  9273. pf->hw.aq.asq_last_status));
  9274. /* not a fatal problem, just keep going */
  9275. }
  9276. }
  9277. /* first time setup */
  9278. if (pf->lan_vsi == I40E_NO_VSI || reinit) {
  9279. struct i40e_vsi *vsi = NULL;
  9280. u16 uplink_seid;
  9281. /* Set up the PF VSI associated with the PF's main VSI
  9282. * that is already in the HW switch
  9283. */
  9284. if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
  9285. uplink_seid = pf->veb[pf->lan_veb]->seid;
  9286. else
  9287. uplink_seid = pf->mac_seid;
  9288. if (pf->lan_vsi == I40E_NO_VSI)
  9289. vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
  9290. else if (reinit)
  9291. vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
  9292. if (!vsi) {
  9293. dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
  9294. i40e_fdir_teardown(pf);
  9295. return -EAGAIN;
  9296. }
  9297. } else {
  9298. /* force a reset of TC and queue layout configurations */
  9299. u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
  9300. pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
  9301. pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
  9302. i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
  9303. }
  9304. i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
  9305. i40e_fdir_sb_setup(pf);
  9306. /* Setup static PF queue filter control settings */
  9307. ret = i40e_setup_pf_filter_control(pf);
  9308. if (ret) {
  9309. dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
  9310. ret);
  9311. /* Failure here should not stop continuing other steps */
  9312. }
  9313. /* enable RSS in the HW, even for only one queue, as the stack can use
  9314. * the hash
  9315. */
  9316. if ((pf->flags & I40E_FLAG_RSS_ENABLED))
  9317. i40e_pf_config_rss(pf);
  9318. /* fill in link information and enable LSE reporting */
  9319. i40e_link_event(pf);
  9320. /* Initialize user-specific link properties */
  9321. pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
  9322. I40E_AQ_AN_COMPLETED) ? true : false);
  9323. i40e_ptp_init(pf);
  9324. return ret;
  9325. }
  9326. /**
  9327. * i40e_determine_queue_usage - Work out queue distribution
  9328. * @pf: board private structure
  9329. **/
  9330. static void i40e_determine_queue_usage(struct i40e_pf *pf)
  9331. {
  9332. int queues_left;
  9333. pf->num_lan_qps = 0;
  9334. /* Find the max queues to be put into basic use. We'll always be
  9335. * using TC0, whether or not DCB is running, and TC0 will get the
  9336. * big RSS set.
  9337. */
  9338. queues_left = pf->hw.func_caps.num_tx_qp;
  9339. if ((queues_left == 1) ||
  9340. !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
  9341. /* one qp for PF, no queues for anything else */
  9342. queues_left = 0;
  9343. pf->alloc_rss_size = pf->num_lan_qps = 1;
  9344. /* make sure all the fancies are disabled */
  9345. pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
  9346. I40E_FLAG_IWARP_ENABLED |
  9347. I40E_FLAG_FD_SB_ENABLED |
  9348. I40E_FLAG_FD_ATR_ENABLED |
  9349. I40E_FLAG_DCB_CAPABLE |
  9350. I40E_FLAG_DCB_ENABLED |
  9351. I40E_FLAG_SRIOV_ENABLED |
  9352. I40E_FLAG_VMDQ_ENABLED);
  9353. } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
  9354. I40E_FLAG_FD_SB_ENABLED |
  9355. I40E_FLAG_FD_ATR_ENABLED |
  9356. I40E_FLAG_DCB_CAPABLE))) {
  9357. /* one qp for PF */
  9358. pf->alloc_rss_size = pf->num_lan_qps = 1;
  9359. queues_left -= pf->num_lan_qps;
  9360. pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
  9361. I40E_FLAG_IWARP_ENABLED |
  9362. I40E_FLAG_FD_SB_ENABLED |
  9363. I40E_FLAG_FD_ATR_ENABLED |
  9364. I40E_FLAG_DCB_ENABLED |
  9365. I40E_FLAG_VMDQ_ENABLED);
  9366. } else {
  9367. /* Not enough queues for all TCs */
  9368. if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
  9369. (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
  9370. pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
  9371. I40E_FLAG_DCB_ENABLED);
  9372. dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
  9373. }
  9374. pf->num_lan_qps = max_t(int, pf->rss_size_max,
  9375. num_online_cpus());
  9376. pf->num_lan_qps = min_t(int, pf->num_lan_qps,
  9377. pf->hw.func_caps.num_tx_qp);
  9378. queues_left -= pf->num_lan_qps;
  9379. }
  9380. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  9381. if (queues_left > 1) {
  9382. queues_left -= 1; /* save 1 queue for FD */
  9383. } else {
  9384. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  9385. dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
  9386. }
  9387. }
  9388. if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
  9389. pf->num_vf_qps && pf->num_req_vfs && queues_left) {
  9390. pf->num_req_vfs = min_t(int, pf->num_req_vfs,
  9391. (queues_left / pf->num_vf_qps));
  9392. queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
  9393. }
  9394. if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
  9395. pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
  9396. pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
  9397. (queues_left / pf->num_vmdq_qps));
  9398. queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
  9399. }
  9400. pf->queues_left = queues_left;
  9401. dev_dbg(&pf->pdev->dev,
  9402. "qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
  9403. pf->hw.func_caps.num_tx_qp,
  9404. !!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
  9405. pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
  9406. pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
  9407. queues_left);
  9408. }
  9409. /**
  9410. * i40e_setup_pf_filter_control - Setup PF static filter control
  9411. * @pf: PF to be setup
  9412. *
  9413. * i40e_setup_pf_filter_control sets up a PF's initial filter control
  9414. * settings. If PE/FCoE are enabled then it will also set the per PF
  9415. * based filter sizes required for them. It also enables Flow director,
  9416. * ethertype and macvlan type filter settings for the pf.
  9417. *
  9418. * Returns 0 on success, negative on failure
  9419. **/
  9420. static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
  9421. {
  9422. struct i40e_filter_control_settings *settings = &pf->filter_settings;
  9423. settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
  9424. /* Flow Director is enabled */
  9425. if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
  9426. settings->enable_fdir = true;
  9427. /* Ethtype and MACVLAN filters enabled for PF */
  9428. settings->enable_ethtype = true;
  9429. settings->enable_macvlan = true;
  9430. if (i40e_set_filter_control(&pf->hw, settings))
  9431. return -ENOENT;
  9432. return 0;
  9433. }
  9434. #define INFO_STRING_LEN 255
  9435. #define REMAIN(__x) (INFO_STRING_LEN - (__x))
  9436. static void i40e_print_features(struct i40e_pf *pf)
  9437. {
  9438. struct i40e_hw *hw = &pf->hw;
  9439. char *buf;
  9440. int i;
  9441. buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
  9442. if (!buf)
  9443. return;
  9444. i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
  9445. #ifdef CONFIG_PCI_IOV
  9446. i += snprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
  9447. #endif
  9448. i += snprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
  9449. pf->hw.func_caps.num_vsis,
  9450. pf->vsi[pf->lan_vsi]->num_queue_pairs);
  9451. if (pf->flags & I40E_FLAG_RSS_ENABLED)
  9452. i += snprintf(&buf[i], REMAIN(i), " RSS");
  9453. if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
  9454. i += snprintf(&buf[i], REMAIN(i), " FD_ATR");
  9455. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  9456. i += snprintf(&buf[i], REMAIN(i), " FD_SB");
  9457. i += snprintf(&buf[i], REMAIN(i), " NTUPLE");
  9458. }
  9459. if (pf->flags & I40E_FLAG_DCB_CAPABLE)
  9460. i += snprintf(&buf[i], REMAIN(i), " DCB");
  9461. i += snprintf(&buf[i], REMAIN(i), " VxLAN");
  9462. i += snprintf(&buf[i], REMAIN(i), " Geneve");
  9463. if (pf->flags & I40E_FLAG_PTP)
  9464. i += snprintf(&buf[i], REMAIN(i), " PTP");
  9465. if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
  9466. i += snprintf(&buf[i], REMAIN(i), " VEB");
  9467. else
  9468. i += snprintf(&buf[i], REMAIN(i), " VEPA");
  9469. dev_info(&pf->pdev->dev, "%s\n", buf);
  9470. kfree(buf);
  9471. WARN_ON(i > INFO_STRING_LEN);
  9472. }
  9473. /**
  9474. * i40e_get_platform_mac_addr - get platform-specific MAC address
  9475. *
  9476. * @pdev: PCI device information struct
  9477. * @pf: board private structure
  9478. *
  9479. * Look up the MAC address in Open Firmware on systems that support it,
  9480. * and use IDPROM on SPARC if no OF address is found. On return, the
  9481. * I40E_FLAG_PF_MAC will be wset in pf->flags if a platform-specific value
  9482. * has been selected.
  9483. **/
  9484. static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
  9485. {
  9486. pf->flags &= ~I40E_FLAG_PF_MAC;
  9487. if (!eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
  9488. pf->flags |= I40E_FLAG_PF_MAC;
  9489. }
  9490. /**
  9491. * i40e_probe - Device initialization routine
  9492. * @pdev: PCI device information struct
  9493. * @ent: entry in i40e_pci_tbl
  9494. *
  9495. * i40e_probe initializes a PF identified by a pci_dev structure.
  9496. * The OS initialization, configuring of the PF private structure,
  9497. * and a hardware reset occur.
  9498. *
  9499. * Returns 0 on success, negative on failure
  9500. **/
  9501. static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  9502. {
  9503. struct i40e_aq_get_phy_abilities_resp abilities;
  9504. struct i40e_pf *pf;
  9505. struct i40e_hw *hw;
  9506. static u16 pfs_found;
  9507. u16 wol_nvm_bits;
  9508. u16 link_status;
  9509. int err;
  9510. u32 val;
  9511. u32 i;
  9512. u8 set_fc_aq_fail;
  9513. err = pci_enable_device_mem(pdev);
  9514. if (err)
  9515. return err;
  9516. /* set up for high or low dma */
  9517. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  9518. if (err) {
  9519. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  9520. if (err) {
  9521. dev_err(&pdev->dev,
  9522. "DMA configuration failed: 0x%x\n", err);
  9523. goto err_dma;
  9524. }
  9525. }
  9526. /* set up pci connections */
  9527. err = pci_request_mem_regions(pdev, i40e_driver_name);
  9528. if (err) {
  9529. dev_info(&pdev->dev,
  9530. "pci_request_selected_regions failed %d\n", err);
  9531. goto err_pci_reg;
  9532. }
  9533. pci_enable_pcie_error_reporting(pdev);
  9534. pci_set_master(pdev);
  9535. /* Now that we have a PCI connection, we need to do the
  9536. * low level device setup. This is primarily setting up
  9537. * the Admin Queue structures and then querying for the
  9538. * device's current profile information.
  9539. */
  9540. pf = kzalloc(sizeof(*pf), GFP_KERNEL);
  9541. if (!pf) {
  9542. err = -ENOMEM;
  9543. goto err_pf_alloc;
  9544. }
  9545. pf->next_vsi = 0;
  9546. pf->pdev = pdev;
  9547. set_bit(__I40E_DOWN, &pf->state);
  9548. hw = &pf->hw;
  9549. hw->back = pf;
  9550. pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
  9551. I40E_MAX_CSR_SPACE);
  9552. hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
  9553. if (!hw->hw_addr) {
  9554. err = -EIO;
  9555. dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
  9556. (unsigned int)pci_resource_start(pdev, 0),
  9557. pf->ioremap_len, err);
  9558. goto err_ioremap;
  9559. }
  9560. hw->vendor_id = pdev->vendor;
  9561. hw->device_id = pdev->device;
  9562. pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
  9563. hw->subsystem_vendor_id = pdev->subsystem_vendor;
  9564. hw->subsystem_device_id = pdev->subsystem_device;
  9565. hw->bus.device = PCI_SLOT(pdev->devfn);
  9566. hw->bus.func = PCI_FUNC(pdev->devfn);
  9567. hw->bus.bus_id = pdev->bus->number;
  9568. pf->instance = pfs_found;
  9569. INIT_LIST_HEAD(&pf->l3_flex_pit_list);
  9570. INIT_LIST_HEAD(&pf->l4_flex_pit_list);
  9571. /* set up the locks for the AQ, do this only once in probe
  9572. * and destroy them only once in remove
  9573. */
  9574. mutex_init(&hw->aq.asq_mutex);
  9575. mutex_init(&hw->aq.arq_mutex);
  9576. pf->msg_enable = netif_msg_init(debug,
  9577. NETIF_MSG_DRV |
  9578. NETIF_MSG_PROBE |
  9579. NETIF_MSG_LINK);
  9580. if (debug < -1)
  9581. pf->hw.debug_mask = debug;
  9582. /* do a special CORER for clearing PXE mode once at init */
  9583. if (hw->revision_id == 0 &&
  9584. (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
  9585. wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
  9586. i40e_flush(hw);
  9587. msleep(200);
  9588. pf->corer_count++;
  9589. i40e_clear_pxe_mode(hw);
  9590. }
  9591. /* Reset here to make sure all is clean and to define PF 'n' */
  9592. i40e_clear_hw(hw);
  9593. err = i40e_pf_reset(hw);
  9594. if (err) {
  9595. dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
  9596. goto err_pf_reset;
  9597. }
  9598. pf->pfr_count++;
  9599. hw->aq.num_arq_entries = I40E_AQ_LEN;
  9600. hw->aq.num_asq_entries = I40E_AQ_LEN;
  9601. hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
  9602. hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
  9603. pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
  9604. snprintf(pf->int_name, sizeof(pf->int_name) - 1,
  9605. "%s-%s:misc",
  9606. dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
  9607. err = i40e_init_shared_code(hw);
  9608. if (err) {
  9609. dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
  9610. err);
  9611. goto err_pf_reset;
  9612. }
  9613. /* set up a default setting for link flow control */
  9614. pf->hw.fc.requested_mode = I40E_FC_NONE;
  9615. err = i40e_init_adminq(hw);
  9616. if (err) {
  9617. if (err == I40E_ERR_FIRMWARE_API_VERSION)
  9618. dev_info(&pdev->dev,
  9619. "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");
  9620. else
  9621. dev_info(&pdev->dev,
  9622. "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
  9623. goto err_pf_reset;
  9624. }
  9625. /* provide nvm, fw, api versions */
  9626. dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s\n",
  9627. hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
  9628. hw->aq.api_maj_ver, hw->aq.api_min_ver,
  9629. i40e_nvm_version_str(hw));
  9630. if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
  9631. hw->aq.api_min_ver > I40E_FW_API_VERSION_MINOR)
  9632. dev_info(&pdev->dev,
  9633. "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");
  9634. else if (hw->aq.api_maj_ver < I40E_FW_API_VERSION_MAJOR ||
  9635. hw->aq.api_min_ver < (I40E_FW_API_VERSION_MINOR - 1))
  9636. dev_info(&pdev->dev,
  9637. "The driver for the device detected an older version of the NVM image than expected. Please update the NVM image.\n");
  9638. i40e_verify_eeprom(pf);
  9639. /* Rev 0 hardware was never productized */
  9640. if (hw->revision_id < 1)
  9641. 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");
  9642. i40e_clear_pxe_mode(hw);
  9643. err = i40e_get_capabilities(pf);
  9644. if (err)
  9645. goto err_adminq_setup;
  9646. err = i40e_sw_init(pf);
  9647. if (err) {
  9648. dev_info(&pdev->dev, "sw_init failed: %d\n", err);
  9649. goto err_sw_init;
  9650. }
  9651. err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
  9652. hw->func_caps.num_rx_qp, 0, 0);
  9653. if (err) {
  9654. dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
  9655. goto err_init_lan_hmc;
  9656. }
  9657. err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
  9658. if (err) {
  9659. dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
  9660. err = -ENOENT;
  9661. goto err_configure_lan_hmc;
  9662. }
  9663. /* Disable LLDP for NICs that have firmware versions lower than v4.3.
  9664. * Ignore error return codes because if it was already disabled via
  9665. * hardware settings this will fail
  9666. */
  9667. if (pf->flags & I40E_FLAG_STOP_FW_LLDP) {
  9668. dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
  9669. i40e_aq_stop_lldp(hw, true, NULL);
  9670. }
  9671. i40e_get_mac_addr(hw, hw->mac.addr);
  9672. /* allow a platform config to override the HW addr */
  9673. i40e_get_platform_mac_addr(pdev, pf);
  9674. if (!is_valid_ether_addr(hw->mac.addr)) {
  9675. dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
  9676. err = -EIO;
  9677. goto err_mac_addr;
  9678. }
  9679. dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
  9680. ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
  9681. i40e_get_port_mac_addr(hw, hw->mac.port_addr);
  9682. if (is_valid_ether_addr(hw->mac.port_addr))
  9683. pf->flags |= I40E_FLAG_PORT_ID_VALID;
  9684. pci_set_drvdata(pdev, pf);
  9685. pci_save_state(pdev);
  9686. #ifdef CONFIG_I40E_DCB
  9687. err = i40e_init_pf_dcb(pf);
  9688. if (err) {
  9689. dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
  9690. pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
  9691. /* Continue without DCB enabled */
  9692. }
  9693. #endif /* CONFIG_I40E_DCB */
  9694. /* set up periodic task facility */
  9695. setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
  9696. pf->service_timer_period = HZ;
  9697. INIT_WORK(&pf->service_task, i40e_service_task);
  9698. clear_bit(__I40E_SERVICE_SCHED, &pf->state);
  9699. pf->flags |= I40E_FLAG_NEED_LINK_UPDATE;
  9700. /* NVM bit on means WoL disabled for the port */
  9701. i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
  9702. if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
  9703. pf->wol_en = false;
  9704. else
  9705. pf->wol_en = true;
  9706. device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
  9707. /* set up the main switch operations */
  9708. i40e_determine_queue_usage(pf);
  9709. err = i40e_init_interrupt_scheme(pf);
  9710. if (err)
  9711. goto err_switch_setup;
  9712. /* The number of VSIs reported by the FW is the minimum guaranteed
  9713. * to us; HW supports far more and we share the remaining pool with
  9714. * the other PFs. We allocate space for more than the guarantee with
  9715. * the understanding that we might not get them all later.
  9716. */
  9717. if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
  9718. pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
  9719. else
  9720. pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
  9721. /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
  9722. pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
  9723. GFP_KERNEL);
  9724. if (!pf->vsi) {
  9725. err = -ENOMEM;
  9726. goto err_switch_setup;
  9727. }
  9728. #ifdef CONFIG_PCI_IOV
  9729. /* prep for VF support */
  9730. if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
  9731. (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
  9732. !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
  9733. if (pci_num_vf(pdev))
  9734. pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
  9735. }
  9736. #endif
  9737. err = i40e_setup_pf_switch(pf, false);
  9738. if (err) {
  9739. dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
  9740. goto err_vsis;
  9741. }
  9742. /* Make sure flow control is set according to current settings */
  9743. err = i40e_set_fc(hw, &set_fc_aq_fail, true);
  9744. if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_GET)
  9745. dev_dbg(&pf->pdev->dev,
  9746. "Set fc with err %s aq_err %s on get_phy_cap\n",
  9747. i40e_stat_str(hw, err),
  9748. i40e_aq_str(hw, hw->aq.asq_last_status));
  9749. if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_SET)
  9750. dev_dbg(&pf->pdev->dev,
  9751. "Set fc with err %s aq_err %s on set_phy_config\n",
  9752. i40e_stat_str(hw, err),
  9753. i40e_aq_str(hw, hw->aq.asq_last_status));
  9754. if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_UPDATE)
  9755. dev_dbg(&pf->pdev->dev,
  9756. "Set fc with err %s aq_err %s on get_link_info\n",
  9757. i40e_stat_str(hw, err),
  9758. i40e_aq_str(hw, hw->aq.asq_last_status));
  9759. /* if FDIR VSI was set up, start it now */
  9760. for (i = 0; i < pf->num_alloc_vsi; i++) {
  9761. if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
  9762. i40e_vsi_open(pf->vsi[i]);
  9763. break;
  9764. }
  9765. }
  9766. /* The driver only wants link up/down and module qualification
  9767. * reports from firmware. Note the negative logic.
  9768. */
  9769. err = i40e_aq_set_phy_int_mask(&pf->hw,
  9770. ~(I40E_AQ_EVENT_LINK_UPDOWN |
  9771. I40E_AQ_EVENT_MEDIA_NA |
  9772. I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
  9773. if (err)
  9774. dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
  9775. i40e_stat_str(&pf->hw, err),
  9776. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  9777. /* Reconfigure hardware for allowing smaller MSS in the case
  9778. * of TSO, so that we avoid the MDD being fired and causing
  9779. * a reset in the case of small MSS+TSO.
  9780. */
  9781. val = rd32(hw, I40E_REG_MSS);
  9782. if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
  9783. val &= ~I40E_REG_MSS_MIN_MASK;
  9784. val |= I40E_64BYTE_MSS;
  9785. wr32(hw, I40E_REG_MSS, val);
  9786. }
  9787. if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
  9788. msleep(75);
  9789. err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
  9790. if (err)
  9791. dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
  9792. i40e_stat_str(&pf->hw, err),
  9793. i40e_aq_str(&pf->hw,
  9794. pf->hw.aq.asq_last_status));
  9795. }
  9796. /* The main driver is (mostly) up and happy. We need to set this state
  9797. * before setting up the misc vector or we get a race and the vector
  9798. * ends up disabled forever.
  9799. */
  9800. clear_bit(__I40E_DOWN, &pf->state);
  9801. /* In case of MSIX we are going to setup the misc vector right here
  9802. * to handle admin queue events etc. In case of legacy and MSI
  9803. * the misc functionality and queue processing is combined in
  9804. * the same vector and that gets setup at open.
  9805. */
  9806. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  9807. err = i40e_setup_misc_vector(pf);
  9808. if (err) {
  9809. dev_info(&pdev->dev,
  9810. "setup of misc vector failed: %d\n", err);
  9811. goto err_vsis;
  9812. }
  9813. }
  9814. #ifdef CONFIG_PCI_IOV
  9815. /* prep for VF support */
  9816. if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
  9817. (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
  9818. !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
  9819. /* disable link interrupts for VFs */
  9820. val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
  9821. val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
  9822. wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
  9823. i40e_flush(hw);
  9824. if (pci_num_vf(pdev)) {
  9825. dev_info(&pdev->dev,
  9826. "Active VFs found, allocating resources.\n");
  9827. err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
  9828. if (err)
  9829. dev_info(&pdev->dev,
  9830. "Error %d allocating resources for existing VFs\n",
  9831. err);
  9832. }
  9833. }
  9834. #endif /* CONFIG_PCI_IOV */
  9835. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  9836. pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
  9837. pf->num_iwarp_msix,
  9838. I40E_IWARP_IRQ_PILE_ID);
  9839. if (pf->iwarp_base_vector < 0) {
  9840. dev_info(&pdev->dev,
  9841. "failed to get tracking for %d vectors for IWARP err=%d\n",
  9842. pf->num_iwarp_msix, pf->iwarp_base_vector);
  9843. pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
  9844. }
  9845. }
  9846. i40e_dbg_pf_init(pf);
  9847. /* tell the firmware that we're starting */
  9848. i40e_send_version(pf);
  9849. /* since everything's happy, start the service_task timer */
  9850. mod_timer(&pf->service_timer,
  9851. round_jiffies(jiffies + pf->service_timer_period));
  9852. /* add this PF to client device list and launch a client service task */
  9853. err = i40e_lan_add_device(pf);
  9854. if (err)
  9855. dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
  9856. err);
  9857. #define PCI_SPEED_SIZE 8
  9858. #define PCI_WIDTH_SIZE 8
  9859. /* Devices on the IOSF bus do not have this information
  9860. * and will report PCI Gen 1 x 1 by default so don't bother
  9861. * checking them.
  9862. */
  9863. if (!(pf->flags & I40E_FLAG_NO_PCI_LINK_CHECK)) {
  9864. char speed[PCI_SPEED_SIZE] = "Unknown";
  9865. char width[PCI_WIDTH_SIZE] = "Unknown";
  9866. /* Get the negotiated link width and speed from PCI config
  9867. * space
  9868. */
  9869. pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
  9870. &link_status);
  9871. i40e_set_pci_config_data(hw, link_status);
  9872. switch (hw->bus.speed) {
  9873. case i40e_bus_speed_8000:
  9874. strncpy(speed, "8.0", PCI_SPEED_SIZE); break;
  9875. case i40e_bus_speed_5000:
  9876. strncpy(speed, "5.0", PCI_SPEED_SIZE); break;
  9877. case i40e_bus_speed_2500:
  9878. strncpy(speed, "2.5", PCI_SPEED_SIZE); break;
  9879. default:
  9880. break;
  9881. }
  9882. switch (hw->bus.width) {
  9883. case i40e_bus_width_pcie_x8:
  9884. strncpy(width, "8", PCI_WIDTH_SIZE); break;
  9885. case i40e_bus_width_pcie_x4:
  9886. strncpy(width, "4", PCI_WIDTH_SIZE); break;
  9887. case i40e_bus_width_pcie_x2:
  9888. strncpy(width, "2", PCI_WIDTH_SIZE); break;
  9889. case i40e_bus_width_pcie_x1:
  9890. strncpy(width, "1", PCI_WIDTH_SIZE); break;
  9891. default:
  9892. break;
  9893. }
  9894. dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
  9895. speed, width);
  9896. if (hw->bus.width < i40e_bus_width_pcie_x8 ||
  9897. hw->bus.speed < i40e_bus_speed_8000) {
  9898. dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
  9899. dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
  9900. }
  9901. }
  9902. /* get the requested speeds from the fw */
  9903. err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
  9904. if (err)
  9905. dev_dbg(&pf->pdev->dev, "get requested speeds ret = %s last_status = %s\n",
  9906. i40e_stat_str(&pf->hw, err),
  9907. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  9908. pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
  9909. /* get the supported phy types from the fw */
  9910. err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
  9911. if (err)
  9912. dev_dbg(&pf->pdev->dev, "get supported phy types ret = %s last_status = %s\n",
  9913. i40e_stat_str(&pf->hw, err),
  9914. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  9915. /* Add a filter to drop all Flow control frames from any VSI from being
  9916. * transmitted. By doing so we stop a malicious VF from sending out
  9917. * PAUSE or PFC frames and potentially controlling traffic for other
  9918. * PF/VF VSIs.
  9919. * The FW can still send Flow control frames if enabled.
  9920. */
  9921. i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
  9922. pf->main_vsi_seid);
  9923. if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
  9924. (pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
  9925. pf->flags |= I40E_FLAG_PHY_CONTROLS_LEDS;
  9926. if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
  9927. pf->flags |= I40E_FLAG_HAVE_CRT_RETIMER;
  9928. /* print a string summarizing features */
  9929. i40e_print_features(pf);
  9930. return 0;
  9931. /* Unwind what we've done if something failed in the setup */
  9932. err_vsis:
  9933. set_bit(__I40E_DOWN, &pf->state);
  9934. i40e_clear_interrupt_scheme(pf);
  9935. kfree(pf->vsi);
  9936. err_switch_setup:
  9937. i40e_reset_interrupt_capability(pf);
  9938. del_timer_sync(&pf->service_timer);
  9939. err_mac_addr:
  9940. err_configure_lan_hmc:
  9941. (void)i40e_shutdown_lan_hmc(hw);
  9942. err_init_lan_hmc:
  9943. kfree(pf->qp_pile);
  9944. err_sw_init:
  9945. err_adminq_setup:
  9946. err_pf_reset:
  9947. iounmap(hw->hw_addr);
  9948. err_ioremap:
  9949. kfree(pf);
  9950. err_pf_alloc:
  9951. pci_disable_pcie_error_reporting(pdev);
  9952. pci_release_mem_regions(pdev);
  9953. err_pci_reg:
  9954. err_dma:
  9955. pci_disable_device(pdev);
  9956. return err;
  9957. }
  9958. /**
  9959. * i40e_remove - Device removal routine
  9960. * @pdev: PCI device information struct
  9961. *
  9962. * i40e_remove is called by the PCI subsystem to alert the driver
  9963. * that is should release a PCI device. This could be caused by a
  9964. * Hot-Plug event, or because the driver is going to be removed from
  9965. * memory.
  9966. **/
  9967. static void i40e_remove(struct pci_dev *pdev)
  9968. {
  9969. struct i40e_pf *pf = pci_get_drvdata(pdev);
  9970. struct i40e_hw *hw = &pf->hw;
  9971. i40e_status ret_code;
  9972. int i;
  9973. i40e_dbg_pf_exit(pf);
  9974. i40e_ptp_stop(pf);
  9975. /* Disable RSS in hw */
  9976. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
  9977. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
  9978. /* no more scheduling of any task */
  9979. set_bit(__I40E_SUSPENDED, &pf->state);
  9980. set_bit(__I40E_DOWN, &pf->state);
  9981. if (pf->service_timer.data)
  9982. del_timer_sync(&pf->service_timer);
  9983. if (pf->service_task.func)
  9984. cancel_work_sync(&pf->service_task);
  9985. if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
  9986. i40e_free_vfs(pf);
  9987. pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
  9988. }
  9989. i40e_fdir_teardown(pf);
  9990. /* If there is a switch structure or any orphans, remove them.
  9991. * This will leave only the PF's VSI remaining.
  9992. */
  9993. for (i = 0; i < I40E_MAX_VEB; i++) {
  9994. if (!pf->veb[i])
  9995. continue;
  9996. if (pf->veb[i]->uplink_seid == pf->mac_seid ||
  9997. pf->veb[i]->uplink_seid == 0)
  9998. i40e_switch_branch_release(pf->veb[i]);
  9999. }
  10000. /* Now we can shutdown the PF's VSI, just before we kill
  10001. * adminq and hmc.
  10002. */
  10003. if (pf->vsi[pf->lan_vsi])
  10004. i40e_vsi_release(pf->vsi[pf->lan_vsi]);
  10005. /* remove attached clients */
  10006. ret_code = i40e_lan_del_device(pf);
  10007. if (ret_code) {
  10008. dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
  10009. ret_code);
  10010. }
  10011. /* shutdown and destroy the HMC */
  10012. if (hw->hmc.hmc_obj) {
  10013. ret_code = i40e_shutdown_lan_hmc(hw);
  10014. if (ret_code)
  10015. dev_warn(&pdev->dev,
  10016. "Failed to destroy the HMC resources: %d\n",
  10017. ret_code);
  10018. }
  10019. /* shutdown the adminq */
  10020. i40e_shutdown_adminq(hw);
  10021. /* destroy the locks only once, here */
  10022. mutex_destroy(&hw->aq.arq_mutex);
  10023. mutex_destroy(&hw->aq.asq_mutex);
  10024. /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
  10025. i40e_clear_interrupt_scheme(pf);
  10026. for (i = 0; i < pf->num_alloc_vsi; i++) {
  10027. if (pf->vsi[i]) {
  10028. i40e_vsi_clear_rings(pf->vsi[i]);
  10029. i40e_vsi_clear(pf->vsi[i]);
  10030. pf->vsi[i] = NULL;
  10031. }
  10032. }
  10033. for (i = 0; i < I40E_MAX_VEB; i++) {
  10034. kfree(pf->veb[i]);
  10035. pf->veb[i] = NULL;
  10036. }
  10037. kfree(pf->qp_pile);
  10038. kfree(pf->vsi);
  10039. iounmap(hw->hw_addr);
  10040. kfree(pf);
  10041. pci_release_mem_regions(pdev);
  10042. pci_disable_pcie_error_reporting(pdev);
  10043. pci_disable_device(pdev);
  10044. }
  10045. /**
  10046. * i40e_pci_error_detected - warning that something funky happened in PCI land
  10047. * @pdev: PCI device information struct
  10048. *
  10049. * Called to warn that something happened and the error handling steps
  10050. * are in progress. Allows the driver to quiesce things, be ready for
  10051. * remediation.
  10052. **/
  10053. static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
  10054. enum pci_channel_state error)
  10055. {
  10056. struct i40e_pf *pf = pci_get_drvdata(pdev);
  10057. dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
  10058. if (!pf) {
  10059. dev_info(&pdev->dev,
  10060. "Cannot recover - error happened during device probe\n");
  10061. return PCI_ERS_RESULT_DISCONNECT;
  10062. }
  10063. /* shutdown all operations */
  10064. if (!test_bit(__I40E_SUSPENDED, &pf->state)) {
  10065. rtnl_lock();
  10066. i40e_prep_for_reset(pf);
  10067. rtnl_unlock();
  10068. }
  10069. /* Request a slot reset */
  10070. return PCI_ERS_RESULT_NEED_RESET;
  10071. }
  10072. /**
  10073. * i40e_pci_error_slot_reset - a PCI slot reset just happened
  10074. * @pdev: PCI device information struct
  10075. *
  10076. * Called to find if the driver can work with the device now that
  10077. * the pci slot has been reset. If a basic connection seems good
  10078. * (registers are readable and have sane content) then return a
  10079. * happy little PCI_ERS_RESULT_xxx.
  10080. **/
  10081. static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
  10082. {
  10083. struct i40e_pf *pf = pci_get_drvdata(pdev);
  10084. pci_ers_result_t result;
  10085. int err;
  10086. u32 reg;
  10087. dev_dbg(&pdev->dev, "%s\n", __func__);
  10088. if (pci_enable_device_mem(pdev)) {
  10089. dev_info(&pdev->dev,
  10090. "Cannot re-enable PCI device after reset.\n");
  10091. result = PCI_ERS_RESULT_DISCONNECT;
  10092. } else {
  10093. pci_set_master(pdev);
  10094. pci_restore_state(pdev);
  10095. pci_save_state(pdev);
  10096. pci_wake_from_d3(pdev, false);
  10097. reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  10098. if (reg == 0)
  10099. result = PCI_ERS_RESULT_RECOVERED;
  10100. else
  10101. result = PCI_ERS_RESULT_DISCONNECT;
  10102. }
  10103. err = pci_cleanup_aer_uncorrect_error_status(pdev);
  10104. if (err) {
  10105. dev_info(&pdev->dev,
  10106. "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
  10107. err);
  10108. /* non-fatal, continue */
  10109. }
  10110. return result;
  10111. }
  10112. /**
  10113. * i40e_pci_error_resume - restart operations after PCI error recovery
  10114. * @pdev: PCI device information struct
  10115. *
  10116. * Called to allow the driver to bring things back up after PCI error
  10117. * and/or reset recovery has finished.
  10118. **/
  10119. static void i40e_pci_error_resume(struct pci_dev *pdev)
  10120. {
  10121. struct i40e_pf *pf = pci_get_drvdata(pdev);
  10122. dev_dbg(&pdev->dev, "%s\n", __func__);
  10123. if (test_bit(__I40E_SUSPENDED, &pf->state))
  10124. return;
  10125. rtnl_lock();
  10126. i40e_handle_reset_warning(pf);
  10127. rtnl_unlock();
  10128. }
  10129. /**
  10130. * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
  10131. * using the mac_address_write admin q function
  10132. * @pf: pointer to i40e_pf struct
  10133. **/
  10134. static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
  10135. {
  10136. struct i40e_hw *hw = &pf->hw;
  10137. i40e_status ret;
  10138. u8 mac_addr[6];
  10139. u16 flags = 0;
  10140. /* Get current MAC address in case it's an LAA */
  10141. if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
  10142. ether_addr_copy(mac_addr,
  10143. pf->vsi[pf->lan_vsi]->netdev->dev_addr);
  10144. } else {
  10145. dev_err(&pf->pdev->dev,
  10146. "Failed to retrieve MAC address; using default\n");
  10147. ether_addr_copy(mac_addr, hw->mac.addr);
  10148. }
  10149. /* The FW expects the mac address write cmd to first be called with
  10150. * one of these flags before calling it again with the multicast
  10151. * enable flags.
  10152. */
  10153. flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
  10154. if (hw->func_caps.flex10_enable && hw->partition_id != 1)
  10155. flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
  10156. ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
  10157. if (ret) {
  10158. dev_err(&pf->pdev->dev,
  10159. "Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
  10160. return;
  10161. }
  10162. flags = I40E_AQC_MC_MAG_EN
  10163. | I40E_AQC_WOL_PRESERVE_ON_PFR
  10164. | I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
  10165. ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
  10166. if (ret)
  10167. dev_err(&pf->pdev->dev,
  10168. "Failed to enable Multicast Magic Packet wake up\n");
  10169. }
  10170. /**
  10171. * i40e_shutdown - PCI callback for shutting down
  10172. * @pdev: PCI device information struct
  10173. **/
  10174. static void i40e_shutdown(struct pci_dev *pdev)
  10175. {
  10176. struct i40e_pf *pf = pci_get_drvdata(pdev);
  10177. struct i40e_hw *hw = &pf->hw;
  10178. set_bit(__I40E_SUSPENDED, &pf->state);
  10179. set_bit(__I40E_DOWN, &pf->state);
  10180. rtnl_lock();
  10181. i40e_prep_for_reset(pf);
  10182. rtnl_unlock();
  10183. wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
  10184. wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
  10185. del_timer_sync(&pf->service_timer);
  10186. cancel_work_sync(&pf->service_task);
  10187. i40e_fdir_teardown(pf);
  10188. if (pf->wol_en && (pf->flags & I40E_FLAG_WOL_MC_MAGIC_PKT_WAKE))
  10189. i40e_enable_mc_magic_wake(pf);
  10190. rtnl_lock();
  10191. i40e_prep_for_reset(pf);
  10192. rtnl_unlock();
  10193. wr32(hw, I40E_PFPM_APM,
  10194. (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
  10195. wr32(hw, I40E_PFPM_WUFC,
  10196. (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
  10197. i40e_clear_interrupt_scheme(pf);
  10198. if (system_state == SYSTEM_POWER_OFF) {
  10199. pci_wake_from_d3(pdev, pf->wol_en);
  10200. pci_set_power_state(pdev, PCI_D3hot);
  10201. }
  10202. }
  10203. #ifdef CONFIG_PM
  10204. /**
  10205. * i40e_suspend - PCI callback for moving to D3
  10206. * @pdev: PCI device information struct
  10207. **/
  10208. static int i40e_suspend(struct pci_dev *pdev, pm_message_t state)
  10209. {
  10210. struct i40e_pf *pf = pci_get_drvdata(pdev);
  10211. struct i40e_hw *hw = &pf->hw;
  10212. int retval = 0;
  10213. set_bit(__I40E_SUSPENDED, &pf->state);
  10214. set_bit(__I40E_DOWN, &pf->state);
  10215. if (pf->wol_en && (pf->flags & I40E_FLAG_WOL_MC_MAGIC_PKT_WAKE))
  10216. i40e_enable_mc_magic_wake(pf);
  10217. rtnl_lock();
  10218. i40e_prep_for_reset(pf);
  10219. rtnl_unlock();
  10220. wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
  10221. wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
  10222. i40e_stop_misc_vector(pf);
  10223. retval = pci_save_state(pdev);
  10224. if (retval)
  10225. return retval;
  10226. pci_wake_from_d3(pdev, pf->wol_en);
  10227. pci_set_power_state(pdev, PCI_D3hot);
  10228. return retval;
  10229. }
  10230. /**
  10231. * i40e_resume - PCI callback for waking up from D3
  10232. * @pdev: PCI device information struct
  10233. **/
  10234. static int i40e_resume(struct pci_dev *pdev)
  10235. {
  10236. struct i40e_pf *pf = pci_get_drvdata(pdev);
  10237. u32 err;
  10238. pci_set_power_state(pdev, PCI_D0);
  10239. pci_restore_state(pdev);
  10240. /* pci_restore_state() clears dev->state_saves, so
  10241. * call pci_save_state() again to restore it.
  10242. */
  10243. pci_save_state(pdev);
  10244. err = pci_enable_device_mem(pdev);
  10245. if (err) {
  10246. dev_err(&pdev->dev, "Cannot enable PCI device from suspend\n");
  10247. return err;
  10248. }
  10249. pci_set_master(pdev);
  10250. /* no wakeup events while running */
  10251. pci_wake_from_d3(pdev, false);
  10252. /* handling the reset will rebuild the device state */
  10253. if (test_and_clear_bit(__I40E_SUSPENDED, &pf->state)) {
  10254. clear_bit(__I40E_DOWN, &pf->state);
  10255. rtnl_lock();
  10256. i40e_reset_and_rebuild(pf, false);
  10257. rtnl_unlock();
  10258. }
  10259. return 0;
  10260. }
  10261. #endif
  10262. static const struct pci_error_handlers i40e_err_handler = {
  10263. .error_detected = i40e_pci_error_detected,
  10264. .slot_reset = i40e_pci_error_slot_reset,
  10265. .resume = i40e_pci_error_resume,
  10266. };
  10267. static struct pci_driver i40e_driver = {
  10268. .name = i40e_driver_name,
  10269. .id_table = i40e_pci_tbl,
  10270. .probe = i40e_probe,
  10271. .remove = i40e_remove,
  10272. #ifdef CONFIG_PM
  10273. .suspend = i40e_suspend,
  10274. .resume = i40e_resume,
  10275. #endif
  10276. .shutdown = i40e_shutdown,
  10277. .err_handler = &i40e_err_handler,
  10278. .sriov_configure = i40e_pci_sriov_configure,
  10279. };
  10280. /**
  10281. * i40e_init_module - Driver registration routine
  10282. *
  10283. * i40e_init_module is the first routine called when the driver is
  10284. * loaded. All it does is register with the PCI subsystem.
  10285. **/
  10286. static int __init i40e_init_module(void)
  10287. {
  10288. pr_info("%s: %s - version %s\n", i40e_driver_name,
  10289. i40e_driver_string, i40e_driver_version_str);
  10290. pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
  10291. /* we will see if single thread per module is enough for now,
  10292. * it can't be any worse than using the system workqueue which
  10293. * was already single threaded
  10294. */
  10295. i40e_wq = alloc_workqueue("%s", WQ_UNBOUND | WQ_MEM_RECLAIM, 1,
  10296. i40e_driver_name);
  10297. if (!i40e_wq) {
  10298. pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
  10299. return -ENOMEM;
  10300. }
  10301. i40e_dbg_init();
  10302. return pci_register_driver(&i40e_driver);
  10303. }
  10304. module_init(i40e_init_module);
  10305. /**
  10306. * i40e_exit_module - Driver exit cleanup routine
  10307. *
  10308. * i40e_exit_module is called just before the driver is removed
  10309. * from memory.
  10310. **/
  10311. static void __exit i40e_exit_module(void)
  10312. {
  10313. pci_unregister_driver(&i40e_driver);
  10314. destroy_workqueue(i40e_wq);
  10315. i40e_dbg_exit();
  10316. }
  10317. module_exit(i40e_exit_module);