ice_main.c 258 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2018-2023, Intel Corporation. */
  3. /* Intel(R) Ethernet Connection E800 Series Linux Driver */
  4. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  5. #include <generated/utsrelease.h>
  6. #include <linux/crash_dump.h>
  7. #include "ice.h"
  8. #include "ice_base.h"
  9. #include "ice_lib.h"
  10. #include "ice_fltr.h"
  11. #include "ice_dcb_lib.h"
  12. #include "ice_dcb_nl.h"
  13. #include "devlink/devlink.h"
  14. #include "devlink/devlink_port.h"
  15. #include "ice_sf_eth.h"
  16. #include "ice_hwmon.h"
  17. /* Including ice_trace.h with CREATE_TRACE_POINTS defined will generate the
  18. * ice tracepoint functions. This must be done exactly once across the
  19. * ice driver.
  20. */
  21. #define CREATE_TRACE_POINTS
  22. #include "ice_trace.h"
  23. #include "ice_eswitch.h"
  24. #include "ice_tc_lib.h"
  25. #include "ice_vsi_vlan_ops.h"
  26. #include <net/xdp_sock_drv.h>
  27. #define DRV_SUMMARY "Intel(R) Ethernet Connection E800 Series Linux Driver"
  28. static const char ice_driver_string[] = DRV_SUMMARY;
  29. static const char ice_copyright[] = "Copyright (c) 2018, Intel Corporation.";
  30. /* DDP Package file located in firmware search paths (e.g. /lib/firmware/) */
  31. #define ICE_DDP_PKG_PATH "intel/ice/ddp/"
  32. #define ICE_DDP_PKG_FILE ICE_DDP_PKG_PATH "ice.pkg"
  33. MODULE_DESCRIPTION(DRV_SUMMARY);
  34. MODULE_IMPORT_NS(LIBIE);
  35. MODULE_LICENSE("GPL v2");
  36. MODULE_FIRMWARE(ICE_DDP_PKG_FILE);
  37. static int debug = -1;
  38. module_param(debug, int, 0644);
  39. #ifndef CONFIG_DYNAMIC_DEBUG
  40. MODULE_PARM_DESC(debug, "netif level (0=none,...,16=all), hw debug_mask (0x8XXXXXXX)");
  41. #else
  42. MODULE_PARM_DESC(debug, "netif level (0=none,...,16=all)");
  43. #endif /* !CONFIG_DYNAMIC_DEBUG */
  44. DEFINE_STATIC_KEY_FALSE(ice_xdp_locking_key);
  45. EXPORT_SYMBOL(ice_xdp_locking_key);
  46. /**
  47. * ice_hw_to_dev - Get device pointer from the hardware structure
  48. * @hw: pointer to the device HW structure
  49. *
  50. * Used to access the device pointer from compilation units which can't easily
  51. * include the definition of struct ice_pf without leading to circular header
  52. * dependencies.
  53. */
  54. struct device *ice_hw_to_dev(struct ice_hw *hw)
  55. {
  56. struct ice_pf *pf = container_of(hw, struct ice_pf, hw);
  57. return &pf->pdev->dev;
  58. }
  59. static struct workqueue_struct *ice_wq;
  60. struct workqueue_struct *ice_lag_wq;
  61. static const struct net_device_ops ice_netdev_safe_mode_ops;
  62. static const struct net_device_ops ice_netdev_ops;
  63. static void ice_rebuild(struct ice_pf *pf, enum ice_reset_req reset_type);
  64. static void ice_vsi_release_all(struct ice_pf *pf);
  65. static int ice_rebuild_channels(struct ice_pf *pf);
  66. static void ice_remove_q_channels(struct ice_vsi *vsi, bool rem_adv_fltr);
  67. static int
  68. ice_indr_setup_tc_cb(struct net_device *netdev, struct Qdisc *sch,
  69. void *cb_priv, enum tc_setup_type type, void *type_data,
  70. void *data,
  71. void (*cleanup)(struct flow_block_cb *block_cb));
  72. bool netif_is_ice(const struct net_device *dev)
  73. {
  74. return dev && (dev->netdev_ops == &ice_netdev_ops ||
  75. dev->netdev_ops == &ice_netdev_safe_mode_ops);
  76. }
  77. /**
  78. * ice_get_tx_pending - returns number of Tx descriptors not processed
  79. * @ring: the ring of descriptors
  80. */
  81. static u16 ice_get_tx_pending(struct ice_tx_ring *ring)
  82. {
  83. u16 head, tail;
  84. head = ring->next_to_clean;
  85. tail = ring->next_to_use;
  86. if (head != tail)
  87. return (head < tail) ?
  88. tail - head : (tail + ring->count - head);
  89. return 0;
  90. }
  91. /**
  92. * ice_check_for_hang_subtask - check for and recover hung queues
  93. * @pf: pointer to PF struct
  94. */
  95. static void ice_check_for_hang_subtask(struct ice_pf *pf)
  96. {
  97. struct ice_vsi *vsi = NULL;
  98. struct ice_hw *hw;
  99. unsigned int i;
  100. int packets;
  101. u32 v;
  102. ice_for_each_vsi(pf, v)
  103. if (pf->vsi[v] && pf->vsi[v]->type == ICE_VSI_PF) {
  104. vsi = pf->vsi[v];
  105. break;
  106. }
  107. if (!vsi || test_bit(ICE_VSI_DOWN, vsi->state))
  108. return;
  109. if (!(vsi->netdev && netif_carrier_ok(vsi->netdev)))
  110. return;
  111. hw = &vsi->back->hw;
  112. ice_for_each_txq(vsi, i) {
  113. struct ice_tx_ring *tx_ring = vsi->tx_rings[i];
  114. struct ice_ring_stats *ring_stats;
  115. if (!tx_ring)
  116. continue;
  117. if (ice_ring_ch_enabled(tx_ring))
  118. continue;
  119. ring_stats = tx_ring->ring_stats;
  120. if (!ring_stats)
  121. continue;
  122. if (tx_ring->desc) {
  123. /* If packet counter has not changed the queue is
  124. * likely stalled, so force an interrupt for this
  125. * queue.
  126. *
  127. * prev_pkt would be negative if there was no
  128. * pending work.
  129. */
  130. packets = ring_stats->stats.pkts & INT_MAX;
  131. if (ring_stats->tx_stats.prev_pkt == packets) {
  132. /* Trigger sw interrupt to revive the queue */
  133. ice_trigger_sw_intr(hw, tx_ring->q_vector);
  134. continue;
  135. }
  136. /* Memory barrier between read of packet count and call
  137. * to ice_get_tx_pending()
  138. */
  139. smp_rmb();
  140. ring_stats->tx_stats.prev_pkt =
  141. ice_get_tx_pending(tx_ring) ? packets : -1;
  142. }
  143. }
  144. }
  145. /**
  146. * ice_init_mac_fltr - Set initial MAC filters
  147. * @pf: board private structure
  148. *
  149. * Set initial set of MAC filters for PF VSI; configure filters for permanent
  150. * address and broadcast address. If an error is encountered, netdevice will be
  151. * unregistered.
  152. */
  153. static int ice_init_mac_fltr(struct ice_pf *pf)
  154. {
  155. struct ice_vsi *vsi;
  156. u8 *perm_addr;
  157. vsi = ice_get_main_vsi(pf);
  158. if (!vsi)
  159. return -EINVAL;
  160. perm_addr = vsi->port_info->mac.perm_addr;
  161. return ice_fltr_add_mac_and_broadcast(vsi, perm_addr, ICE_FWD_TO_VSI);
  162. }
  163. /**
  164. * ice_add_mac_to_sync_list - creates list of MAC addresses to be synced
  165. * @netdev: the net device on which the sync is happening
  166. * @addr: MAC address to sync
  167. *
  168. * This is a callback function which is called by the in kernel device sync
  169. * functions (like __dev_uc_sync, __dev_mc_sync, etc). This function only
  170. * populates the tmp_sync_list, which is later used by ice_add_mac to add the
  171. * MAC filters from the hardware.
  172. */
  173. static int ice_add_mac_to_sync_list(struct net_device *netdev, const u8 *addr)
  174. {
  175. struct ice_netdev_priv *np = netdev_priv(netdev);
  176. struct ice_vsi *vsi = np->vsi;
  177. if (ice_fltr_add_mac_to_list(vsi, &vsi->tmp_sync_list, addr,
  178. ICE_FWD_TO_VSI))
  179. return -EINVAL;
  180. return 0;
  181. }
  182. /**
  183. * ice_add_mac_to_unsync_list - creates list of MAC addresses to be unsynced
  184. * @netdev: the net device on which the unsync is happening
  185. * @addr: MAC address to unsync
  186. *
  187. * This is a callback function which is called by the in kernel device unsync
  188. * functions (like __dev_uc_unsync, __dev_mc_unsync, etc). This function only
  189. * populates the tmp_unsync_list, which is later used by ice_remove_mac to
  190. * delete the MAC filters from the hardware.
  191. */
  192. static int ice_add_mac_to_unsync_list(struct net_device *netdev, const u8 *addr)
  193. {
  194. struct ice_netdev_priv *np = netdev_priv(netdev);
  195. struct ice_vsi *vsi = np->vsi;
  196. /* Under some circumstances, we might receive a request to delete our
  197. * own device address from our uc list. Because we store the device
  198. * address in the VSI's MAC filter list, we need to ignore such
  199. * requests and not delete our device address from this list.
  200. */
  201. if (ether_addr_equal(addr, netdev->dev_addr))
  202. return 0;
  203. if (ice_fltr_add_mac_to_list(vsi, &vsi->tmp_unsync_list, addr,
  204. ICE_FWD_TO_VSI))
  205. return -EINVAL;
  206. return 0;
  207. }
  208. /**
  209. * ice_vsi_fltr_changed - check if filter state changed
  210. * @vsi: VSI to be checked
  211. *
  212. * returns true if filter state has changed, false otherwise.
  213. */
  214. static bool ice_vsi_fltr_changed(struct ice_vsi *vsi)
  215. {
  216. return test_bit(ICE_VSI_UMAC_FLTR_CHANGED, vsi->state) ||
  217. test_bit(ICE_VSI_MMAC_FLTR_CHANGED, vsi->state);
  218. }
  219. /**
  220. * ice_set_promisc - Enable promiscuous mode for a given PF
  221. * @vsi: the VSI being configured
  222. * @promisc_m: mask of promiscuous config bits
  223. *
  224. */
  225. static int ice_set_promisc(struct ice_vsi *vsi, u8 promisc_m)
  226. {
  227. int status;
  228. if (vsi->type != ICE_VSI_PF)
  229. return 0;
  230. if (ice_vsi_has_non_zero_vlans(vsi)) {
  231. promisc_m |= (ICE_PROMISC_VLAN_RX | ICE_PROMISC_VLAN_TX);
  232. status = ice_fltr_set_vlan_vsi_promisc(&vsi->back->hw, vsi,
  233. promisc_m);
  234. } else {
  235. status = ice_fltr_set_vsi_promisc(&vsi->back->hw, vsi->idx,
  236. promisc_m, 0);
  237. }
  238. if (status && status != -EEXIST)
  239. return status;
  240. netdev_dbg(vsi->netdev, "set promisc filter bits for VSI %i: 0x%x\n",
  241. vsi->vsi_num, promisc_m);
  242. return 0;
  243. }
  244. /**
  245. * ice_clear_promisc - Disable promiscuous mode for a given PF
  246. * @vsi: the VSI being configured
  247. * @promisc_m: mask of promiscuous config bits
  248. *
  249. */
  250. static int ice_clear_promisc(struct ice_vsi *vsi, u8 promisc_m)
  251. {
  252. int status;
  253. if (vsi->type != ICE_VSI_PF)
  254. return 0;
  255. if (ice_vsi_has_non_zero_vlans(vsi)) {
  256. promisc_m |= (ICE_PROMISC_VLAN_RX | ICE_PROMISC_VLAN_TX);
  257. status = ice_fltr_clear_vlan_vsi_promisc(&vsi->back->hw, vsi,
  258. promisc_m);
  259. } else {
  260. status = ice_fltr_clear_vsi_promisc(&vsi->back->hw, vsi->idx,
  261. promisc_m, 0);
  262. }
  263. netdev_dbg(vsi->netdev, "clear promisc filter bits for VSI %i: 0x%x\n",
  264. vsi->vsi_num, promisc_m);
  265. return status;
  266. }
  267. /**
  268. * ice_vsi_sync_fltr - Update the VSI filter list to the HW
  269. * @vsi: ptr to the VSI
  270. *
  271. * Push any outstanding VSI filter changes through the AdminQ.
  272. */
  273. static int ice_vsi_sync_fltr(struct ice_vsi *vsi)
  274. {
  275. struct ice_vsi_vlan_ops *vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
  276. struct device *dev = ice_pf_to_dev(vsi->back);
  277. struct net_device *netdev = vsi->netdev;
  278. bool promisc_forced_on = false;
  279. struct ice_pf *pf = vsi->back;
  280. struct ice_hw *hw = &pf->hw;
  281. u32 changed_flags = 0;
  282. int err;
  283. if (!vsi->netdev)
  284. return -EINVAL;
  285. while (test_and_set_bit(ICE_CFG_BUSY, vsi->state))
  286. usleep_range(1000, 2000);
  287. changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
  288. vsi->current_netdev_flags = vsi->netdev->flags;
  289. INIT_LIST_HEAD(&vsi->tmp_sync_list);
  290. INIT_LIST_HEAD(&vsi->tmp_unsync_list);
  291. if (ice_vsi_fltr_changed(vsi)) {
  292. clear_bit(ICE_VSI_UMAC_FLTR_CHANGED, vsi->state);
  293. clear_bit(ICE_VSI_MMAC_FLTR_CHANGED, vsi->state);
  294. /* grab the netdev's addr_list_lock */
  295. netif_addr_lock_bh(netdev);
  296. __dev_uc_sync(netdev, ice_add_mac_to_sync_list,
  297. ice_add_mac_to_unsync_list);
  298. __dev_mc_sync(netdev, ice_add_mac_to_sync_list,
  299. ice_add_mac_to_unsync_list);
  300. /* our temp lists are populated. release lock */
  301. netif_addr_unlock_bh(netdev);
  302. }
  303. /* Remove MAC addresses in the unsync list */
  304. err = ice_fltr_remove_mac_list(vsi, &vsi->tmp_unsync_list);
  305. ice_fltr_free_list(dev, &vsi->tmp_unsync_list);
  306. if (err) {
  307. netdev_err(netdev, "Failed to delete MAC filters\n");
  308. /* if we failed because of alloc failures, just bail */
  309. if (err == -ENOMEM)
  310. goto out;
  311. }
  312. /* Add MAC addresses in the sync list */
  313. err = ice_fltr_add_mac_list(vsi, &vsi->tmp_sync_list);
  314. ice_fltr_free_list(dev, &vsi->tmp_sync_list);
  315. /* If filter is added successfully or already exists, do not go into
  316. * 'if' condition and report it as error. Instead continue processing
  317. * rest of the function.
  318. */
  319. if (err && err != -EEXIST) {
  320. netdev_err(netdev, "Failed to add MAC filters\n");
  321. /* If there is no more space for new umac filters, VSI
  322. * should go into promiscuous mode. There should be some
  323. * space reserved for promiscuous filters.
  324. */
  325. if (hw->adminq.sq_last_status == ICE_AQ_RC_ENOSPC &&
  326. !test_and_set_bit(ICE_FLTR_OVERFLOW_PROMISC,
  327. vsi->state)) {
  328. promisc_forced_on = true;
  329. netdev_warn(netdev, "Reached MAC filter limit, forcing promisc mode on VSI %d\n",
  330. vsi->vsi_num);
  331. } else {
  332. goto out;
  333. }
  334. }
  335. err = 0;
  336. /* check for changes in promiscuous modes */
  337. if (changed_flags & IFF_ALLMULTI) {
  338. if (vsi->current_netdev_flags & IFF_ALLMULTI) {
  339. err = ice_set_promisc(vsi, ICE_MCAST_PROMISC_BITS);
  340. if (err) {
  341. vsi->current_netdev_flags &= ~IFF_ALLMULTI;
  342. goto out_promisc;
  343. }
  344. } else {
  345. /* !(vsi->current_netdev_flags & IFF_ALLMULTI) */
  346. err = ice_clear_promisc(vsi, ICE_MCAST_PROMISC_BITS);
  347. if (err) {
  348. vsi->current_netdev_flags |= IFF_ALLMULTI;
  349. goto out_promisc;
  350. }
  351. }
  352. }
  353. if (((changed_flags & IFF_PROMISC) || promisc_forced_on) ||
  354. test_bit(ICE_VSI_PROMISC_CHANGED, vsi->state)) {
  355. clear_bit(ICE_VSI_PROMISC_CHANGED, vsi->state);
  356. if (vsi->current_netdev_flags & IFF_PROMISC) {
  357. /* Apply Rx filter rule to get traffic from wire */
  358. if (!ice_is_dflt_vsi_in_use(vsi->port_info)) {
  359. err = ice_set_dflt_vsi(vsi);
  360. if (err && err != -EEXIST) {
  361. netdev_err(netdev, "Error %d setting default VSI %i Rx rule\n",
  362. err, vsi->vsi_num);
  363. vsi->current_netdev_flags &=
  364. ~IFF_PROMISC;
  365. goto out_promisc;
  366. }
  367. err = 0;
  368. vlan_ops->dis_rx_filtering(vsi);
  369. /* promiscuous mode implies allmulticast so
  370. * that VSIs that are in promiscuous mode are
  371. * subscribed to multicast packets coming to
  372. * the port
  373. */
  374. err = ice_set_promisc(vsi,
  375. ICE_MCAST_PROMISC_BITS);
  376. if (err)
  377. goto out_promisc;
  378. }
  379. } else {
  380. /* Clear Rx filter to remove traffic from wire */
  381. if (ice_is_vsi_dflt_vsi(vsi)) {
  382. err = ice_clear_dflt_vsi(vsi);
  383. if (err) {
  384. netdev_err(netdev, "Error %d clearing default VSI %i Rx rule\n",
  385. err, vsi->vsi_num);
  386. vsi->current_netdev_flags |=
  387. IFF_PROMISC;
  388. goto out_promisc;
  389. }
  390. if (vsi->netdev->features &
  391. NETIF_F_HW_VLAN_CTAG_FILTER)
  392. vlan_ops->ena_rx_filtering(vsi);
  393. }
  394. /* disable allmulti here, but only if allmulti is not
  395. * still enabled for the netdev
  396. */
  397. if (!(vsi->current_netdev_flags & IFF_ALLMULTI)) {
  398. err = ice_clear_promisc(vsi,
  399. ICE_MCAST_PROMISC_BITS);
  400. if (err) {
  401. netdev_err(netdev, "Error %d clearing multicast promiscuous on VSI %i\n",
  402. err, vsi->vsi_num);
  403. }
  404. }
  405. }
  406. }
  407. goto exit;
  408. out_promisc:
  409. set_bit(ICE_VSI_PROMISC_CHANGED, vsi->state);
  410. goto exit;
  411. out:
  412. /* if something went wrong then set the changed flag so we try again */
  413. set_bit(ICE_VSI_UMAC_FLTR_CHANGED, vsi->state);
  414. set_bit(ICE_VSI_MMAC_FLTR_CHANGED, vsi->state);
  415. exit:
  416. clear_bit(ICE_CFG_BUSY, vsi->state);
  417. return err;
  418. }
  419. /**
  420. * ice_sync_fltr_subtask - Sync the VSI filter list with HW
  421. * @pf: board private structure
  422. */
  423. static void ice_sync_fltr_subtask(struct ice_pf *pf)
  424. {
  425. int v;
  426. if (!pf || !(test_bit(ICE_FLAG_FLTR_SYNC, pf->flags)))
  427. return;
  428. clear_bit(ICE_FLAG_FLTR_SYNC, pf->flags);
  429. ice_for_each_vsi(pf, v)
  430. if (pf->vsi[v] && ice_vsi_fltr_changed(pf->vsi[v]) &&
  431. ice_vsi_sync_fltr(pf->vsi[v])) {
  432. /* come back and try again later */
  433. set_bit(ICE_FLAG_FLTR_SYNC, pf->flags);
  434. break;
  435. }
  436. }
  437. /**
  438. * ice_pf_dis_all_vsi - Pause all VSIs on a PF
  439. * @pf: the PF
  440. * @locked: is the rtnl_lock already held
  441. */
  442. static void ice_pf_dis_all_vsi(struct ice_pf *pf, bool locked)
  443. {
  444. int node;
  445. int v;
  446. ice_for_each_vsi(pf, v)
  447. if (pf->vsi[v])
  448. ice_dis_vsi(pf->vsi[v], locked);
  449. for (node = 0; node < ICE_MAX_PF_AGG_NODES; node++)
  450. pf->pf_agg_node[node].num_vsis = 0;
  451. for (node = 0; node < ICE_MAX_VF_AGG_NODES; node++)
  452. pf->vf_agg_node[node].num_vsis = 0;
  453. }
  454. /**
  455. * ice_prepare_for_reset - prep for reset
  456. * @pf: board private structure
  457. * @reset_type: reset type requested
  458. *
  459. * Inform or close all dependent features in prep for reset.
  460. */
  461. static void
  462. ice_prepare_for_reset(struct ice_pf *pf, enum ice_reset_req reset_type)
  463. {
  464. struct ice_hw *hw = &pf->hw;
  465. struct ice_vsi *vsi;
  466. struct ice_vf *vf;
  467. unsigned int bkt;
  468. dev_dbg(ice_pf_to_dev(pf), "reset_type=%d\n", reset_type);
  469. /* already prepared for reset */
  470. if (test_bit(ICE_PREPARED_FOR_RESET, pf->state))
  471. return;
  472. synchronize_irq(pf->oicr_irq.virq);
  473. ice_unplug_aux_dev(pf);
  474. /* Notify VFs of impending reset */
  475. if (ice_check_sq_alive(hw, &hw->mailboxq))
  476. ice_vc_notify_reset(pf);
  477. /* Disable VFs until reset is completed */
  478. mutex_lock(&pf->vfs.table_lock);
  479. ice_for_each_vf(pf, bkt, vf)
  480. ice_set_vf_state_dis(vf);
  481. mutex_unlock(&pf->vfs.table_lock);
  482. if (ice_is_eswitch_mode_switchdev(pf)) {
  483. rtnl_lock();
  484. ice_eswitch_br_fdb_flush(pf->eswitch.br_offloads->bridge);
  485. rtnl_unlock();
  486. }
  487. /* release ADQ specific HW and SW resources */
  488. vsi = ice_get_main_vsi(pf);
  489. if (!vsi)
  490. goto skip;
  491. /* to be on safe side, reset orig_rss_size so that normal flow
  492. * of deciding rss_size can take precedence
  493. */
  494. vsi->orig_rss_size = 0;
  495. if (test_bit(ICE_FLAG_TC_MQPRIO, pf->flags)) {
  496. if (reset_type == ICE_RESET_PFR) {
  497. vsi->old_ena_tc = vsi->all_enatc;
  498. vsi->old_numtc = vsi->all_numtc;
  499. } else {
  500. ice_remove_q_channels(vsi, true);
  501. /* for other reset type, do not support channel rebuild
  502. * hence reset needed info
  503. */
  504. vsi->old_ena_tc = 0;
  505. vsi->all_enatc = 0;
  506. vsi->old_numtc = 0;
  507. vsi->all_numtc = 0;
  508. vsi->req_txq = 0;
  509. vsi->req_rxq = 0;
  510. clear_bit(ICE_FLAG_TC_MQPRIO, pf->flags);
  511. memset(&vsi->mqprio_qopt, 0, sizeof(vsi->mqprio_qopt));
  512. }
  513. }
  514. if (vsi->netdev)
  515. netif_device_detach(vsi->netdev);
  516. skip:
  517. /* clear SW filtering DB */
  518. ice_clear_hw_tbls(hw);
  519. /* disable the VSIs and their queues that are not already DOWN */
  520. set_bit(ICE_VSI_REBUILD_PENDING, ice_get_main_vsi(pf)->state);
  521. ice_pf_dis_all_vsi(pf, false);
  522. if (test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags))
  523. ice_ptp_prepare_for_reset(pf, reset_type);
  524. if (ice_is_feature_supported(pf, ICE_F_GNSS))
  525. ice_gnss_exit(pf);
  526. if (hw->port_info)
  527. ice_sched_clear_port(hw->port_info);
  528. ice_shutdown_all_ctrlq(hw, false);
  529. set_bit(ICE_PREPARED_FOR_RESET, pf->state);
  530. }
  531. /**
  532. * ice_do_reset - Initiate one of many types of resets
  533. * @pf: board private structure
  534. * @reset_type: reset type requested before this function was called.
  535. */
  536. static void ice_do_reset(struct ice_pf *pf, enum ice_reset_req reset_type)
  537. {
  538. struct device *dev = ice_pf_to_dev(pf);
  539. struct ice_hw *hw = &pf->hw;
  540. dev_dbg(dev, "reset_type 0x%x requested\n", reset_type);
  541. if (pf->lag && pf->lag->bonded && reset_type == ICE_RESET_PFR) {
  542. dev_dbg(dev, "PFR on a bonded interface, promoting to CORER\n");
  543. reset_type = ICE_RESET_CORER;
  544. }
  545. ice_prepare_for_reset(pf, reset_type);
  546. /* trigger the reset */
  547. if (ice_reset(hw, reset_type)) {
  548. dev_err(dev, "reset %d failed\n", reset_type);
  549. set_bit(ICE_RESET_FAILED, pf->state);
  550. clear_bit(ICE_RESET_OICR_RECV, pf->state);
  551. clear_bit(ICE_PREPARED_FOR_RESET, pf->state);
  552. clear_bit(ICE_PFR_REQ, pf->state);
  553. clear_bit(ICE_CORER_REQ, pf->state);
  554. clear_bit(ICE_GLOBR_REQ, pf->state);
  555. wake_up(&pf->reset_wait_queue);
  556. return;
  557. }
  558. /* PFR is a bit of a special case because it doesn't result in an OICR
  559. * interrupt. So for PFR, rebuild after the reset and clear the reset-
  560. * associated state bits.
  561. */
  562. if (reset_type == ICE_RESET_PFR) {
  563. pf->pfr_count++;
  564. ice_rebuild(pf, reset_type);
  565. clear_bit(ICE_PREPARED_FOR_RESET, pf->state);
  566. clear_bit(ICE_PFR_REQ, pf->state);
  567. wake_up(&pf->reset_wait_queue);
  568. ice_reset_all_vfs(pf);
  569. }
  570. }
  571. /**
  572. * ice_reset_subtask - Set up for resetting the device and driver
  573. * @pf: board private structure
  574. */
  575. static void ice_reset_subtask(struct ice_pf *pf)
  576. {
  577. enum ice_reset_req reset_type = ICE_RESET_INVAL;
  578. /* When a CORER/GLOBR/EMPR is about to happen, the hardware triggers an
  579. * OICR interrupt. The OICR handler (ice_misc_intr) determines what type
  580. * of reset is pending and sets bits in pf->state indicating the reset
  581. * type and ICE_RESET_OICR_RECV. So, if the latter bit is set
  582. * prepare for pending reset if not already (for PF software-initiated
  583. * global resets the software should already be prepared for it as
  584. * indicated by ICE_PREPARED_FOR_RESET; for global resets initiated
  585. * by firmware or software on other PFs, that bit is not set so prepare
  586. * for the reset now), poll for reset done, rebuild and return.
  587. */
  588. if (test_bit(ICE_RESET_OICR_RECV, pf->state)) {
  589. /* Perform the largest reset requested */
  590. if (test_and_clear_bit(ICE_CORER_RECV, pf->state))
  591. reset_type = ICE_RESET_CORER;
  592. if (test_and_clear_bit(ICE_GLOBR_RECV, pf->state))
  593. reset_type = ICE_RESET_GLOBR;
  594. if (test_and_clear_bit(ICE_EMPR_RECV, pf->state))
  595. reset_type = ICE_RESET_EMPR;
  596. /* return if no valid reset type requested */
  597. if (reset_type == ICE_RESET_INVAL)
  598. return;
  599. ice_prepare_for_reset(pf, reset_type);
  600. /* make sure we are ready to rebuild */
  601. if (ice_check_reset(&pf->hw)) {
  602. set_bit(ICE_RESET_FAILED, pf->state);
  603. } else {
  604. /* done with reset. start rebuild */
  605. pf->hw.reset_ongoing = false;
  606. ice_rebuild(pf, reset_type);
  607. /* clear bit to resume normal operations, but
  608. * ICE_NEEDS_RESTART bit is set in case rebuild failed
  609. */
  610. clear_bit(ICE_RESET_OICR_RECV, pf->state);
  611. clear_bit(ICE_PREPARED_FOR_RESET, pf->state);
  612. clear_bit(ICE_PFR_REQ, pf->state);
  613. clear_bit(ICE_CORER_REQ, pf->state);
  614. clear_bit(ICE_GLOBR_REQ, pf->state);
  615. wake_up(&pf->reset_wait_queue);
  616. ice_reset_all_vfs(pf);
  617. }
  618. return;
  619. }
  620. /* No pending resets to finish processing. Check for new resets */
  621. if (test_bit(ICE_PFR_REQ, pf->state)) {
  622. reset_type = ICE_RESET_PFR;
  623. if (pf->lag && pf->lag->bonded) {
  624. dev_dbg(ice_pf_to_dev(pf), "PFR on a bonded interface, promoting to CORER\n");
  625. reset_type = ICE_RESET_CORER;
  626. }
  627. }
  628. if (test_bit(ICE_CORER_REQ, pf->state))
  629. reset_type = ICE_RESET_CORER;
  630. if (test_bit(ICE_GLOBR_REQ, pf->state))
  631. reset_type = ICE_RESET_GLOBR;
  632. /* If no valid reset type requested just return */
  633. if (reset_type == ICE_RESET_INVAL)
  634. return;
  635. /* reset if not already down or busy */
  636. if (!test_bit(ICE_DOWN, pf->state) &&
  637. !test_bit(ICE_CFG_BUSY, pf->state)) {
  638. ice_do_reset(pf, reset_type);
  639. }
  640. }
  641. /**
  642. * ice_print_topo_conflict - print topology conflict message
  643. * @vsi: the VSI whose topology status is being checked
  644. */
  645. static void ice_print_topo_conflict(struct ice_vsi *vsi)
  646. {
  647. switch (vsi->port_info->phy.link_info.topo_media_conflict) {
  648. case ICE_AQ_LINK_TOPO_CONFLICT:
  649. case ICE_AQ_LINK_MEDIA_CONFLICT:
  650. case ICE_AQ_LINK_TOPO_UNREACH_PRT:
  651. case ICE_AQ_LINK_TOPO_UNDRUTIL_PRT:
  652. case ICE_AQ_LINK_TOPO_UNDRUTIL_MEDIA:
  653. netdev_info(vsi->netdev, "Potential misconfiguration of the Ethernet port detected. If it was not intended, please use the Intel (R) Ethernet Port Configuration Tool to address the issue.\n");
  654. break;
  655. case ICE_AQ_LINK_TOPO_UNSUPP_MEDIA:
  656. if (test_bit(ICE_FLAG_LINK_LENIENT_MODE_ENA, vsi->back->flags))
  657. netdev_warn(vsi->netdev, "An unsupported module type was detected. Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules\n");
  658. else
  659. netdev_err(vsi->netdev, "Rx/Tx is disabled on this device because an unsupported module type was detected. Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
  660. break;
  661. default:
  662. break;
  663. }
  664. }
  665. /**
  666. * ice_print_link_msg - print link up or down message
  667. * @vsi: the VSI whose link status is being queried
  668. * @isup: boolean for if the link is now up or down
  669. */
  670. void ice_print_link_msg(struct ice_vsi *vsi, bool isup)
  671. {
  672. struct ice_aqc_get_phy_caps_data *caps;
  673. const char *an_advertised;
  674. const char *fec_req;
  675. const char *speed;
  676. const char *fec;
  677. const char *fc;
  678. const char *an;
  679. int status;
  680. if (!vsi)
  681. return;
  682. if (vsi->current_isup == isup)
  683. return;
  684. vsi->current_isup = isup;
  685. if (!isup) {
  686. netdev_info(vsi->netdev, "NIC Link is Down\n");
  687. return;
  688. }
  689. switch (vsi->port_info->phy.link_info.link_speed) {
  690. case ICE_AQ_LINK_SPEED_200GB:
  691. speed = "200 G";
  692. break;
  693. case ICE_AQ_LINK_SPEED_100GB:
  694. speed = "100 G";
  695. break;
  696. case ICE_AQ_LINK_SPEED_50GB:
  697. speed = "50 G";
  698. break;
  699. case ICE_AQ_LINK_SPEED_40GB:
  700. speed = "40 G";
  701. break;
  702. case ICE_AQ_LINK_SPEED_25GB:
  703. speed = "25 G";
  704. break;
  705. case ICE_AQ_LINK_SPEED_20GB:
  706. speed = "20 G";
  707. break;
  708. case ICE_AQ_LINK_SPEED_10GB:
  709. speed = "10 G";
  710. break;
  711. case ICE_AQ_LINK_SPEED_5GB:
  712. speed = "5 G";
  713. break;
  714. case ICE_AQ_LINK_SPEED_2500MB:
  715. speed = "2.5 G";
  716. break;
  717. case ICE_AQ_LINK_SPEED_1000MB:
  718. speed = "1 G";
  719. break;
  720. case ICE_AQ_LINK_SPEED_100MB:
  721. speed = "100 M";
  722. break;
  723. default:
  724. speed = "Unknown ";
  725. break;
  726. }
  727. switch (vsi->port_info->fc.current_mode) {
  728. case ICE_FC_FULL:
  729. fc = "Rx/Tx";
  730. break;
  731. case ICE_FC_TX_PAUSE:
  732. fc = "Tx";
  733. break;
  734. case ICE_FC_RX_PAUSE:
  735. fc = "Rx";
  736. break;
  737. case ICE_FC_NONE:
  738. fc = "None";
  739. break;
  740. default:
  741. fc = "Unknown";
  742. break;
  743. }
  744. /* Get FEC mode based on negotiated link info */
  745. switch (vsi->port_info->phy.link_info.fec_info) {
  746. case ICE_AQ_LINK_25G_RS_528_FEC_EN:
  747. case ICE_AQ_LINK_25G_RS_544_FEC_EN:
  748. fec = "RS-FEC";
  749. break;
  750. case ICE_AQ_LINK_25G_KR_FEC_EN:
  751. fec = "FC-FEC/BASE-R";
  752. break;
  753. default:
  754. fec = "NONE";
  755. break;
  756. }
  757. /* check if autoneg completed, might be false due to not supported */
  758. if (vsi->port_info->phy.link_info.an_info & ICE_AQ_AN_COMPLETED)
  759. an = "True";
  760. else
  761. an = "False";
  762. /* Get FEC mode requested based on PHY caps last SW configuration */
  763. caps = kzalloc(sizeof(*caps), GFP_KERNEL);
  764. if (!caps) {
  765. fec_req = "Unknown";
  766. an_advertised = "Unknown";
  767. goto done;
  768. }
  769. status = ice_aq_get_phy_caps(vsi->port_info, false,
  770. ICE_AQC_REPORT_ACTIVE_CFG, caps, NULL);
  771. if (status)
  772. netdev_info(vsi->netdev, "Get phy capability failed.\n");
  773. an_advertised = ice_is_phy_caps_an_enabled(caps) ? "On" : "Off";
  774. if (caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_528_REQ ||
  775. caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_544_REQ)
  776. fec_req = "RS-FEC";
  777. else if (caps->link_fec_options & ICE_AQC_PHY_FEC_10G_KR_40G_KR4_REQ ||
  778. caps->link_fec_options & ICE_AQC_PHY_FEC_25G_KR_REQ)
  779. fec_req = "FC-FEC/BASE-R";
  780. else
  781. fec_req = "NONE";
  782. kfree(caps);
  783. done:
  784. netdev_info(vsi->netdev, "NIC Link is up %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg Advertised: %s, Autoneg Negotiated: %s, Flow Control: %s\n",
  785. speed, fec_req, fec, an_advertised, an, fc);
  786. ice_print_topo_conflict(vsi);
  787. }
  788. /**
  789. * ice_vsi_link_event - update the VSI's netdev
  790. * @vsi: the VSI on which the link event occurred
  791. * @link_up: whether or not the VSI needs to be set up or down
  792. */
  793. static void ice_vsi_link_event(struct ice_vsi *vsi, bool link_up)
  794. {
  795. if (!vsi)
  796. return;
  797. if (test_bit(ICE_VSI_DOWN, vsi->state) || !vsi->netdev)
  798. return;
  799. if (vsi->type == ICE_VSI_PF) {
  800. if (link_up == netif_carrier_ok(vsi->netdev))
  801. return;
  802. if (link_up) {
  803. netif_carrier_on(vsi->netdev);
  804. netif_tx_wake_all_queues(vsi->netdev);
  805. } else {
  806. netif_carrier_off(vsi->netdev);
  807. netif_tx_stop_all_queues(vsi->netdev);
  808. }
  809. }
  810. }
  811. /**
  812. * ice_set_dflt_mib - send a default config MIB to the FW
  813. * @pf: private PF struct
  814. *
  815. * This function sends a default configuration MIB to the FW.
  816. *
  817. * If this function errors out at any point, the driver is still able to
  818. * function. The main impact is that LFC may not operate as expected.
  819. * Therefore an error state in this function should be treated with a DBG
  820. * message and continue on with driver rebuild/reenable.
  821. */
  822. static void ice_set_dflt_mib(struct ice_pf *pf)
  823. {
  824. struct device *dev = ice_pf_to_dev(pf);
  825. u8 mib_type, *buf, *lldpmib = NULL;
  826. u16 len, typelen, offset = 0;
  827. struct ice_lldp_org_tlv *tlv;
  828. struct ice_hw *hw = &pf->hw;
  829. u32 ouisubtype;
  830. mib_type = SET_LOCAL_MIB_TYPE_LOCAL_MIB;
  831. lldpmib = kzalloc(ICE_LLDPDU_SIZE, GFP_KERNEL);
  832. if (!lldpmib) {
  833. dev_dbg(dev, "%s Failed to allocate MIB memory\n",
  834. __func__);
  835. return;
  836. }
  837. /* Add ETS CFG TLV */
  838. tlv = (struct ice_lldp_org_tlv *)lldpmib;
  839. typelen = ((ICE_TLV_TYPE_ORG << ICE_LLDP_TLV_TYPE_S) |
  840. ICE_IEEE_ETS_TLV_LEN);
  841. tlv->typelen = htons(typelen);
  842. ouisubtype = ((ICE_IEEE_8021QAZ_OUI << ICE_LLDP_TLV_OUI_S) |
  843. ICE_IEEE_SUBTYPE_ETS_CFG);
  844. tlv->ouisubtype = htonl(ouisubtype);
  845. buf = tlv->tlvinfo;
  846. buf[0] = 0;
  847. /* ETS CFG all UPs map to TC 0. Next 4 (1 - 4) Octets = 0.
  848. * Octets 5 - 12 are BW values, set octet 5 to 100% BW.
  849. * Octets 13 - 20 are TSA values - leave as zeros
  850. */
  851. buf[5] = 0x64;
  852. len = FIELD_GET(ICE_LLDP_TLV_LEN_M, typelen);
  853. offset += len + 2;
  854. tlv = (struct ice_lldp_org_tlv *)
  855. ((char *)tlv + sizeof(tlv->typelen) + len);
  856. /* Add ETS REC TLV */
  857. buf = tlv->tlvinfo;
  858. tlv->typelen = htons(typelen);
  859. ouisubtype = ((ICE_IEEE_8021QAZ_OUI << ICE_LLDP_TLV_OUI_S) |
  860. ICE_IEEE_SUBTYPE_ETS_REC);
  861. tlv->ouisubtype = htonl(ouisubtype);
  862. /* First octet of buf is reserved
  863. * Octets 1 - 4 map UP to TC - all UPs map to zero
  864. * Octets 5 - 12 are BW values - set TC 0 to 100%.
  865. * Octets 13 - 20 are TSA value - leave as zeros
  866. */
  867. buf[5] = 0x64;
  868. offset += len + 2;
  869. tlv = (struct ice_lldp_org_tlv *)
  870. ((char *)tlv + sizeof(tlv->typelen) + len);
  871. /* Add PFC CFG TLV */
  872. typelen = ((ICE_TLV_TYPE_ORG << ICE_LLDP_TLV_TYPE_S) |
  873. ICE_IEEE_PFC_TLV_LEN);
  874. tlv->typelen = htons(typelen);
  875. ouisubtype = ((ICE_IEEE_8021QAZ_OUI << ICE_LLDP_TLV_OUI_S) |
  876. ICE_IEEE_SUBTYPE_PFC_CFG);
  877. tlv->ouisubtype = htonl(ouisubtype);
  878. /* Octet 1 left as all zeros - PFC disabled */
  879. buf[0] = 0x08;
  880. len = FIELD_GET(ICE_LLDP_TLV_LEN_M, typelen);
  881. offset += len + 2;
  882. if (ice_aq_set_lldp_mib(hw, mib_type, (void *)lldpmib, offset, NULL))
  883. dev_dbg(dev, "%s Failed to set default LLDP MIB\n", __func__);
  884. kfree(lldpmib);
  885. }
  886. /**
  887. * ice_check_phy_fw_load - check if PHY FW load failed
  888. * @pf: pointer to PF struct
  889. * @link_cfg_err: bitmap from the link info structure
  890. *
  891. * check if external PHY FW load failed and print an error message if it did
  892. */
  893. static void ice_check_phy_fw_load(struct ice_pf *pf, u8 link_cfg_err)
  894. {
  895. if (!(link_cfg_err & ICE_AQ_LINK_EXTERNAL_PHY_LOAD_FAILURE)) {
  896. clear_bit(ICE_FLAG_PHY_FW_LOAD_FAILED, pf->flags);
  897. return;
  898. }
  899. if (test_bit(ICE_FLAG_PHY_FW_LOAD_FAILED, pf->flags))
  900. return;
  901. if (link_cfg_err & ICE_AQ_LINK_EXTERNAL_PHY_LOAD_FAILURE) {
  902. dev_err(ice_pf_to_dev(pf), "Device failed to load the FW for the external PHY. Please download and install the latest NVM for your device and try again\n");
  903. set_bit(ICE_FLAG_PHY_FW_LOAD_FAILED, pf->flags);
  904. }
  905. }
  906. /**
  907. * ice_check_module_power
  908. * @pf: pointer to PF struct
  909. * @link_cfg_err: bitmap from the link info structure
  910. *
  911. * check module power level returned by a previous call to aq_get_link_info
  912. * and print error messages if module power level is not supported
  913. */
  914. static void ice_check_module_power(struct ice_pf *pf, u8 link_cfg_err)
  915. {
  916. /* if module power level is supported, clear the flag */
  917. if (!(link_cfg_err & (ICE_AQ_LINK_INVAL_MAX_POWER_LIMIT |
  918. ICE_AQ_LINK_MODULE_POWER_UNSUPPORTED))) {
  919. clear_bit(ICE_FLAG_MOD_POWER_UNSUPPORTED, pf->flags);
  920. return;
  921. }
  922. /* if ICE_FLAG_MOD_POWER_UNSUPPORTED was previously set and the
  923. * above block didn't clear this bit, there's nothing to do
  924. */
  925. if (test_bit(ICE_FLAG_MOD_POWER_UNSUPPORTED, pf->flags))
  926. return;
  927. if (link_cfg_err & ICE_AQ_LINK_INVAL_MAX_POWER_LIMIT) {
  928. dev_err(ice_pf_to_dev(pf), "The installed module is incompatible with the device's NVM image. Cannot start link\n");
  929. set_bit(ICE_FLAG_MOD_POWER_UNSUPPORTED, pf->flags);
  930. } else if (link_cfg_err & ICE_AQ_LINK_MODULE_POWER_UNSUPPORTED) {
  931. dev_err(ice_pf_to_dev(pf), "The module's power requirements exceed the device's power supply. Cannot start link\n");
  932. set_bit(ICE_FLAG_MOD_POWER_UNSUPPORTED, pf->flags);
  933. }
  934. }
  935. /**
  936. * ice_check_link_cfg_err - check if link configuration failed
  937. * @pf: pointer to the PF struct
  938. * @link_cfg_err: bitmap from the link info structure
  939. *
  940. * print if any link configuration failure happens due to the value in the
  941. * link_cfg_err parameter in the link info structure
  942. */
  943. static void ice_check_link_cfg_err(struct ice_pf *pf, u8 link_cfg_err)
  944. {
  945. ice_check_module_power(pf, link_cfg_err);
  946. ice_check_phy_fw_load(pf, link_cfg_err);
  947. }
  948. /**
  949. * ice_link_event - process the link event
  950. * @pf: PF that the link event is associated with
  951. * @pi: port_info for the port that the link event is associated with
  952. * @link_up: true if the physical link is up and false if it is down
  953. * @link_speed: current link speed received from the link event
  954. *
  955. * Returns 0 on success and negative on failure
  956. */
  957. static int
  958. ice_link_event(struct ice_pf *pf, struct ice_port_info *pi, bool link_up,
  959. u16 link_speed)
  960. {
  961. struct device *dev = ice_pf_to_dev(pf);
  962. struct ice_phy_info *phy_info;
  963. struct ice_vsi *vsi;
  964. u16 old_link_speed;
  965. bool old_link;
  966. int status;
  967. phy_info = &pi->phy;
  968. phy_info->link_info_old = phy_info->link_info;
  969. old_link = !!(phy_info->link_info_old.link_info & ICE_AQ_LINK_UP);
  970. old_link_speed = phy_info->link_info_old.link_speed;
  971. /* update the link info structures and re-enable link events,
  972. * don't bail on failure due to other book keeping needed
  973. */
  974. status = ice_update_link_info(pi);
  975. if (status)
  976. dev_dbg(dev, "Failed to update link status on port %d, err %d aq_err %s\n",
  977. pi->lport, status,
  978. ice_aq_str(pi->hw->adminq.sq_last_status));
  979. ice_check_link_cfg_err(pf, pi->phy.link_info.link_cfg_err);
  980. /* Check if the link state is up after updating link info, and treat
  981. * this event as an UP event since the link is actually UP now.
  982. */
  983. if (phy_info->link_info.link_info & ICE_AQ_LINK_UP)
  984. link_up = true;
  985. vsi = ice_get_main_vsi(pf);
  986. if (!vsi || !vsi->port_info)
  987. return -EINVAL;
  988. /* turn off PHY if media was removed */
  989. if (!test_bit(ICE_FLAG_NO_MEDIA, pf->flags) &&
  990. !(pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE)) {
  991. set_bit(ICE_FLAG_NO_MEDIA, pf->flags);
  992. ice_set_link(vsi, false);
  993. }
  994. /* if the old link up/down and speed is the same as the new */
  995. if (link_up == old_link && link_speed == old_link_speed)
  996. return 0;
  997. ice_ptp_link_change(pf, link_up);
  998. if (ice_is_dcb_active(pf)) {
  999. if (test_bit(ICE_FLAG_DCB_ENA, pf->flags))
  1000. ice_dcb_rebuild(pf);
  1001. } else {
  1002. if (link_up)
  1003. ice_set_dflt_mib(pf);
  1004. }
  1005. ice_vsi_link_event(vsi, link_up);
  1006. ice_print_link_msg(vsi, link_up);
  1007. ice_vc_notify_link_state(pf);
  1008. return 0;
  1009. }
  1010. /**
  1011. * ice_watchdog_subtask - periodic tasks not using event driven scheduling
  1012. * @pf: board private structure
  1013. */
  1014. static void ice_watchdog_subtask(struct ice_pf *pf)
  1015. {
  1016. int i;
  1017. /* if interface is down do nothing */
  1018. if (test_bit(ICE_DOWN, pf->state) ||
  1019. test_bit(ICE_CFG_BUSY, pf->state))
  1020. return;
  1021. /* make sure we don't do these things too often */
  1022. if (time_before(jiffies,
  1023. pf->serv_tmr_prev + pf->serv_tmr_period))
  1024. return;
  1025. pf->serv_tmr_prev = jiffies;
  1026. /* Update the stats for active netdevs so the network stack
  1027. * can look at updated numbers whenever it cares to
  1028. */
  1029. ice_update_pf_stats(pf);
  1030. ice_for_each_vsi(pf, i)
  1031. if (pf->vsi[i] && pf->vsi[i]->netdev)
  1032. ice_update_vsi_stats(pf->vsi[i]);
  1033. }
  1034. /**
  1035. * ice_init_link_events - enable/initialize link events
  1036. * @pi: pointer to the port_info instance
  1037. *
  1038. * Returns -EIO on failure, 0 on success
  1039. */
  1040. static int ice_init_link_events(struct ice_port_info *pi)
  1041. {
  1042. u16 mask;
  1043. mask = ~((u16)(ICE_AQ_LINK_EVENT_UPDOWN | ICE_AQ_LINK_EVENT_MEDIA_NA |
  1044. ICE_AQ_LINK_EVENT_MODULE_QUAL_FAIL |
  1045. ICE_AQ_LINK_EVENT_PHY_FW_LOAD_FAIL));
  1046. if (ice_aq_set_event_mask(pi->hw, pi->lport, mask, NULL)) {
  1047. dev_dbg(ice_hw_to_dev(pi->hw), "Failed to set link event mask for port %d\n",
  1048. pi->lport);
  1049. return -EIO;
  1050. }
  1051. if (ice_aq_get_link_info(pi, true, NULL, NULL)) {
  1052. dev_dbg(ice_hw_to_dev(pi->hw), "Failed to enable link events for port %d\n",
  1053. pi->lport);
  1054. return -EIO;
  1055. }
  1056. return 0;
  1057. }
  1058. /**
  1059. * ice_handle_link_event - handle link event via ARQ
  1060. * @pf: PF that the link event is associated with
  1061. * @event: event structure containing link status info
  1062. */
  1063. static int
  1064. ice_handle_link_event(struct ice_pf *pf, struct ice_rq_event_info *event)
  1065. {
  1066. struct ice_aqc_get_link_status_data *link_data;
  1067. struct ice_port_info *port_info;
  1068. int status;
  1069. link_data = (struct ice_aqc_get_link_status_data *)event->msg_buf;
  1070. port_info = pf->hw.port_info;
  1071. if (!port_info)
  1072. return -EINVAL;
  1073. status = ice_link_event(pf, port_info,
  1074. !!(link_data->link_info & ICE_AQ_LINK_UP),
  1075. le16_to_cpu(link_data->link_speed));
  1076. if (status)
  1077. dev_dbg(ice_pf_to_dev(pf), "Could not process link event, error %d\n",
  1078. status);
  1079. return status;
  1080. }
  1081. /**
  1082. * ice_get_fwlog_data - copy the FW log data from ARQ event
  1083. * @pf: PF that the FW log event is associated with
  1084. * @event: event structure containing FW log data
  1085. */
  1086. static void
  1087. ice_get_fwlog_data(struct ice_pf *pf, struct ice_rq_event_info *event)
  1088. {
  1089. struct ice_fwlog_data *fwlog;
  1090. struct ice_hw *hw = &pf->hw;
  1091. fwlog = &hw->fwlog_ring.rings[hw->fwlog_ring.tail];
  1092. memset(fwlog->data, 0, PAGE_SIZE);
  1093. fwlog->data_size = le16_to_cpu(event->desc.datalen);
  1094. memcpy(fwlog->data, event->msg_buf, fwlog->data_size);
  1095. ice_fwlog_ring_increment(&hw->fwlog_ring.tail, hw->fwlog_ring.size);
  1096. if (ice_fwlog_ring_full(&hw->fwlog_ring)) {
  1097. /* the rings are full so bump the head to create room */
  1098. ice_fwlog_ring_increment(&hw->fwlog_ring.head,
  1099. hw->fwlog_ring.size);
  1100. }
  1101. }
  1102. /**
  1103. * ice_aq_prep_for_event - Prepare to wait for an AdminQ event from firmware
  1104. * @pf: pointer to the PF private structure
  1105. * @task: intermediate helper storage and identifier for waiting
  1106. * @opcode: the opcode to wait for
  1107. *
  1108. * Prepares to wait for a specific AdminQ completion event on the ARQ for
  1109. * a given PF. Actual wait would be done by a call to ice_aq_wait_for_event().
  1110. *
  1111. * Calls are separated to allow caller registering for event before sending
  1112. * the command, which mitigates a race between registering and FW responding.
  1113. *
  1114. * To obtain only the descriptor contents, pass an task->event with null
  1115. * msg_buf. If the complete data buffer is desired, allocate the
  1116. * task->event.msg_buf with enough space ahead of time.
  1117. */
  1118. void ice_aq_prep_for_event(struct ice_pf *pf, struct ice_aq_task *task,
  1119. u16 opcode)
  1120. {
  1121. INIT_HLIST_NODE(&task->entry);
  1122. task->opcode = opcode;
  1123. task->state = ICE_AQ_TASK_WAITING;
  1124. spin_lock_bh(&pf->aq_wait_lock);
  1125. hlist_add_head(&task->entry, &pf->aq_wait_list);
  1126. spin_unlock_bh(&pf->aq_wait_lock);
  1127. }
  1128. /**
  1129. * ice_aq_wait_for_event - Wait for an AdminQ event from firmware
  1130. * @pf: pointer to the PF private structure
  1131. * @task: ptr prepared by ice_aq_prep_for_event()
  1132. * @timeout: how long to wait, in jiffies
  1133. *
  1134. * Waits for a specific AdminQ completion event on the ARQ for a given PF. The
  1135. * current thread will be put to sleep until the specified event occurs or
  1136. * until the given timeout is reached.
  1137. *
  1138. * Returns: zero on success, or a negative error code on failure.
  1139. */
  1140. int ice_aq_wait_for_event(struct ice_pf *pf, struct ice_aq_task *task,
  1141. unsigned long timeout)
  1142. {
  1143. enum ice_aq_task_state *state = &task->state;
  1144. struct device *dev = ice_pf_to_dev(pf);
  1145. unsigned long start = jiffies;
  1146. long ret;
  1147. int err;
  1148. ret = wait_event_interruptible_timeout(pf->aq_wait_queue,
  1149. *state != ICE_AQ_TASK_WAITING,
  1150. timeout);
  1151. switch (*state) {
  1152. case ICE_AQ_TASK_NOT_PREPARED:
  1153. WARN(1, "call to %s without ice_aq_prep_for_event()", __func__);
  1154. err = -EINVAL;
  1155. break;
  1156. case ICE_AQ_TASK_WAITING:
  1157. err = ret < 0 ? ret : -ETIMEDOUT;
  1158. break;
  1159. case ICE_AQ_TASK_CANCELED:
  1160. err = ret < 0 ? ret : -ECANCELED;
  1161. break;
  1162. case ICE_AQ_TASK_COMPLETE:
  1163. err = ret < 0 ? ret : 0;
  1164. break;
  1165. default:
  1166. WARN(1, "Unexpected AdminQ wait task state %u", *state);
  1167. err = -EINVAL;
  1168. break;
  1169. }
  1170. dev_dbg(dev, "Waited %u msecs (max %u msecs) for firmware response to op 0x%04x\n",
  1171. jiffies_to_msecs(jiffies - start),
  1172. jiffies_to_msecs(timeout),
  1173. task->opcode);
  1174. spin_lock_bh(&pf->aq_wait_lock);
  1175. hlist_del(&task->entry);
  1176. spin_unlock_bh(&pf->aq_wait_lock);
  1177. return err;
  1178. }
  1179. /**
  1180. * ice_aq_check_events - Check if any thread is waiting for an AdminQ event
  1181. * @pf: pointer to the PF private structure
  1182. * @opcode: the opcode of the event
  1183. * @event: the event to check
  1184. *
  1185. * Loops over the current list of pending threads waiting for an AdminQ event.
  1186. * For each matching task, copy the contents of the event into the task
  1187. * structure and wake up the thread.
  1188. *
  1189. * If multiple threads wait for the same opcode, they will all be woken up.
  1190. *
  1191. * Note that event->msg_buf will only be duplicated if the event has a buffer
  1192. * with enough space already allocated. Otherwise, only the descriptor and
  1193. * message length will be copied.
  1194. *
  1195. * Returns: true if an event was found, false otherwise
  1196. */
  1197. static void ice_aq_check_events(struct ice_pf *pf, u16 opcode,
  1198. struct ice_rq_event_info *event)
  1199. {
  1200. struct ice_rq_event_info *task_ev;
  1201. struct ice_aq_task *task;
  1202. bool found = false;
  1203. spin_lock_bh(&pf->aq_wait_lock);
  1204. hlist_for_each_entry(task, &pf->aq_wait_list, entry) {
  1205. if (task->state != ICE_AQ_TASK_WAITING)
  1206. continue;
  1207. if (task->opcode != opcode)
  1208. continue;
  1209. task_ev = &task->event;
  1210. memcpy(&task_ev->desc, &event->desc, sizeof(event->desc));
  1211. task_ev->msg_len = event->msg_len;
  1212. /* Only copy the data buffer if a destination was set */
  1213. if (task_ev->msg_buf && task_ev->buf_len >= event->buf_len) {
  1214. memcpy(task_ev->msg_buf, event->msg_buf,
  1215. event->buf_len);
  1216. task_ev->buf_len = event->buf_len;
  1217. }
  1218. task->state = ICE_AQ_TASK_COMPLETE;
  1219. found = true;
  1220. }
  1221. spin_unlock_bh(&pf->aq_wait_lock);
  1222. if (found)
  1223. wake_up(&pf->aq_wait_queue);
  1224. }
  1225. /**
  1226. * ice_aq_cancel_waiting_tasks - Immediately cancel all waiting tasks
  1227. * @pf: the PF private structure
  1228. *
  1229. * Set all waiting tasks to ICE_AQ_TASK_CANCELED, and wake up their threads.
  1230. * This will then cause ice_aq_wait_for_event to exit with -ECANCELED.
  1231. */
  1232. static void ice_aq_cancel_waiting_tasks(struct ice_pf *pf)
  1233. {
  1234. struct ice_aq_task *task;
  1235. spin_lock_bh(&pf->aq_wait_lock);
  1236. hlist_for_each_entry(task, &pf->aq_wait_list, entry)
  1237. task->state = ICE_AQ_TASK_CANCELED;
  1238. spin_unlock_bh(&pf->aq_wait_lock);
  1239. wake_up(&pf->aq_wait_queue);
  1240. }
  1241. #define ICE_MBX_OVERFLOW_WATERMARK 64
  1242. /**
  1243. * __ice_clean_ctrlq - helper function to clean controlq rings
  1244. * @pf: ptr to struct ice_pf
  1245. * @q_type: specific Control queue type
  1246. */
  1247. static int __ice_clean_ctrlq(struct ice_pf *pf, enum ice_ctl_q q_type)
  1248. {
  1249. struct device *dev = ice_pf_to_dev(pf);
  1250. struct ice_rq_event_info event;
  1251. struct ice_hw *hw = &pf->hw;
  1252. struct ice_ctl_q_info *cq;
  1253. u16 pending, i = 0;
  1254. const char *qtype;
  1255. u32 oldval, val;
  1256. /* Do not clean control queue if/when PF reset fails */
  1257. if (test_bit(ICE_RESET_FAILED, pf->state))
  1258. return 0;
  1259. switch (q_type) {
  1260. case ICE_CTL_Q_ADMIN:
  1261. cq = &hw->adminq;
  1262. qtype = "Admin";
  1263. break;
  1264. case ICE_CTL_Q_SB:
  1265. cq = &hw->sbq;
  1266. qtype = "Sideband";
  1267. break;
  1268. case ICE_CTL_Q_MAILBOX:
  1269. cq = &hw->mailboxq;
  1270. qtype = "Mailbox";
  1271. /* we are going to try to detect a malicious VF, so set the
  1272. * state to begin detection
  1273. */
  1274. hw->mbx_snapshot.mbx_buf.state = ICE_MAL_VF_DETECT_STATE_NEW_SNAPSHOT;
  1275. break;
  1276. default:
  1277. dev_warn(dev, "Unknown control queue type 0x%x\n", q_type);
  1278. return 0;
  1279. }
  1280. /* check for error indications - PF_xx_AxQLEN register layout for
  1281. * FW/MBX/SB are identical so just use defines for PF_FW_AxQLEN.
  1282. */
  1283. val = rd32(hw, cq->rq.len);
  1284. if (val & (PF_FW_ARQLEN_ARQVFE_M | PF_FW_ARQLEN_ARQOVFL_M |
  1285. PF_FW_ARQLEN_ARQCRIT_M)) {
  1286. oldval = val;
  1287. if (val & PF_FW_ARQLEN_ARQVFE_M)
  1288. dev_dbg(dev, "%s Receive Queue VF Error detected\n",
  1289. qtype);
  1290. if (val & PF_FW_ARQLEN_ARQOVFL_M) {
  1291. dev_dbg(dev, "%s Receive Queue Overflow Error detected\n",
  1292. qtype);
  1293. }
  1294. if (val & PF_FW_ARQLEN_ARQCRIT_M)
  1295. dev_dbg(dev, "%s Receive Queue Critical Error detected\n",
  1296. qtype);
  1297. val &= ~(PF_FW_ARQLEN_ARQVFE_M | PF_FW_ARQLEN_ARQOVFL_M |
  1298. PF_FW_ARQLEN_ARQCRIT_M);
  1299. if (oldval != val)
  1300. wr32(hw, cq->rq.len, val);
  1301. }
  1302. val = rd32(hw, cq->sq.len);
  1303. if (val & (PF_FW_ATQLEN_ATQVFE_M | PF_FW_ATQLEN_ATQOVFL_M |
  1304. PF_FW_ATQLEN_ATQCRIT_M)) {
  1305. oldval = val;
  1306. if (val & PF_FW_ATQLEN_ATQVFE_M)
  1307. dev_dbg(dev, "%s Send Queue VF Error detected\n",
  1308. qtype);
  1309. if (val & PF_FW_ATQLEN_ATQOVFL_M) {
  1310. dev_dbg(dev, "%s Send Queue Overflow Error detected\n",
  1311. qtype);
  1312. }
  1313. if (val & PF_FW_ATQLEN_ATQCRIT_M)
  1314. dev_dbg(dev, "%s Send Queue Critical Error detected\n",
  1315. qtype);
  1316. val &= ~(PF_FW_ATQLEN_ATQVFE_M | PF_FW_ATQLEN_ATQOVFL_M |
  1317. PF_FW_ATQLEN_ATQCRIT_M);
  1318. if (oldval != val)
  1319. wr32(hw, cq->sq.len, val);
  1320. }
  1321. event.buf_len = cq->rq_buf_size;
  1322. event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
  1323. if (!event.msg_buf)
  1324. return 0;
  1325. do {
  1326. struct ice_mbx_data data = {};
  1327. u16 opcode;
  1328. int ret;
  1329. ret = ice_clean_rq_elem(hw, cq, &event, &pending);
  1330. if (ret == -EALREADY)
  1331. break;
  1332. if (ret) {
  1333. dev_err(dev, "%s Receive Queue event error %d\n", qtype,
  1334. ret);
  1335. break;
  1336. }
  1337. opcode = le16_to_cpu(event.desc.opcode);
  1338. /* Notify any thread that might be waiting for this event */
  1339. ice_aq_check_events(pf, opcode, &event);
  1340. switch (opcode) {
  1341. case ice_aqc_opc_get_link_status:
  1342. if (ice_handle_link_event(pf, &event))
  1343. dev_err(dev, "Could not handle link event\n");
  1344. break;
  1345. case ice_aqc_opc_event_lan_overflow:
  1346. ice_vf_lan_overflow_event(pf, &event);
  1347. break;
  1348. case ice_mbx_opc_send_msg_to_pf:
  1349. if (ice_is_feature_supported(pf, ICE_F_MBX_LIMIT)) {
  1350. ice_vc_process_vf_msg(pf, &event, NULL);
  1351. ice_mbx_vf_dec_trig_e830(hw, &event);
  1352. } else {
  1353. u16 val = hw->mailboxq.num_rq_entries;
  1354. data.max_num_msgs_mbx = val;
  1355. val = ICE_MBX_OVERFLOW_WATERMARK;
  1356. data.async_watermark_val = val;
  1357. data.num_msg_proc = i;
  1358. data.num_pending_arq = pending;
  1359. ice_vc_process_vf_msg(pf, &event, &data);
  1360. }
  1361. break;
  1362. case ice_aqc_opc_fw_logs_event:
  1363. ice_get_fwlog_data(pf, &event);
  1364. break;
  1365. case ice_aqc_opc_lldp_set_mib_change:
  1366. ice_dcb_process_lldp_set_mib_change(pf, &event);
  1367. break;
  1368. default:
  1369. dev_dbg(dev, "%s Receive Queue unknown event 0x%04x ignored\n",
  1370. qtype, opcode);
  1371. break;
  1372. }
  1373. } while (pending && (i++ < ICE_DFLT_IRQ_WORK));
  1374. kfree(event.msg_buf);
  1375. return pending && (i == ICE_DFLT_IRQ_WORK);
  1376. }
  1377. /**
  1378. * ice_ctrlq_pending - check if there is a difference between ntc and ntu
  1379. * @hw: pointer to hardware info
  1380. * @cq: control queue information
  1381. *
  1382. * returns true if there are pending messages in a queue, false if there aren't
  1383. */
  1384. static bool ice_ctrlq_pending(struct ice_hw *hw, struct ice_ctl_q_info *cq)
  1385. {
  1386. u16 ntu;
  1387. ntu = (u16)(rd32(hw, cq->rq.head) & cq->rq.head_mask);
  1388. return cq->rq.next_to_clean != ntu;
  1389. }
  1390. /**
  1391. * ice_clean_adminq_subtask - clean the AdminQ rings
  1392. * @pf: board private structure
  1393. */
  1394. static void ice_clean_adminq_subtask(struct ice_pf *pf)
  1395. {
  1396. struct ice_hw *hw = &pf->hw;
  1397. if (!test_bit(ICE_ADMINQ_EVENT_PENDING, pf->state))
  1398. return;
  1399. if (__ice_clean_ctrlq(pf, ICE_CTL_Q_ADMIN))
  1400. return;
  1401. clear_bit(ICE_ADMINQ_EVENT_PENDING, pf->state);
  1402. /* There might be a situation where new messages arrive to a control
  1403. * queue between processing the last message and clearing the
  1404. * EVENT_PENDING bit. So before exiting, check queue head again (using
  1405. * ice_ctrlq_pending) and process new messages if any.
  1406. */
  1407. if (ice_ctrlq_pending(hw, &hw->adminq))
  1408. __ice_clean_ctrlq(pf, ICE_CTL_Q_ADMIN);
  1409. ice_flush(hw);
  1410. }
  1411. /**
  1412. * ice_clean_mailboxq_subtask - clean the MailboxQ rings
  1413. * @pf: board private structure
  1414. */
  1415. static void ice_clean_mailboxq_subtask(struct ice_pf *pf)
  1416. {
  1417. struct ice_hw *hw = &pf->hw;
  1418. if (!test_bit(ICE_MAILBOXQ_EVENT_PENDING, pf->state))
  1419. return;
  1420. if (__ice_clean_ctrlq(pf, ICE_CTL_Q_MAILBOX))
  1421. return;
  1422. clear_bit(ICE_MAILBOXQ_EVENT_PENDING, pf->state);
  1423. if (ice_ctrlq_pending(hw, &hw->mailboxq))
  1424. __ice_clean_ctrlq(pf, ICE_CTL_Q_MAILBOX);
  1425. ice_flush(hw);
  1426. }
  1427. /**
  1428. * ice_clean_sbq_subtask - clean the Sideband Queue rings
  1429. * @pf: board private structure
  1430. */
  1431. static void ice_clean_sbq_subtask(struct ice_pf *pf)
  1432. {
  1433. struct ice_hw *hw = &pf->hw;
  1434. /* if mac_type is not generic, sideband is not supported
  1435. * and there's nothing to do here
  1436. */
  1437. if (!ice_is_generic_mac(hw)) {
  1438. clear_bit(ICE_SIDEBANDQ_EVENT_PENDING, pf->state);
  1439. return;
  1440. }
  1441. if (!test_bit(ICE_SIDEBANDQ_EVENT_PENDING, pf->state))
  1442. return;
  1443. if (__ice_clean_ctrlq(pf, ICE_CTL_Q_SB))
  1444. return;
  1445. clear_bit(ICE_SIDEBANDQ_EVENT_PENDING, pf->state);
  1446. if (ice_ctrlq_pending(hw, &hw->sbq))
  1447. __ice_clean_ctrlq(pf, ICE_CTL_Q_SB);
  1448. ice_flush(hw);
  1449. }
  1450. /**
  1451. * ice_service_task_schedule - schedule the service task to wake up
  1452. * @pf: board private structure
  1453. *
  1454. * If not already scheduled, this puts the task into the work queue.
  1455. */
  1456. void ice_service_task_schedule(struct ice_pf *pf)
  1457. {
  1458. if (!test_bit(ICE_SERVICE_DIS, pf->state) &&
  1459. !test_and_set_bit(ICE_SERVICE_SCHED, pf->state) &&
  1460. !test_bit(ICE_NEEDS_RESTART, pf->state))
  1461. queue_work(ice_wq, &pf->serv_task);
  1462. }
  1463. /**
  1464. * ice_service_task_complete - finish up the service task
  1465. * @pf: board private structure
  1466. */
  1467. static void ice_service_task_complete(struct ice_pf *pf)
  1468. {
  1469. WARN_ON(!test_bit(ICE_SERVICE_SCHED, pf->state));
  1470. /* force memory (pf->state) to sync before next service task */
  1471. smp_mb__before_atomic();
  1472. clear_bit(ICE_SERVICE_SCHED, pf->state);
  1473. }
  1474. /**
  1475. * ice_service_task_stop - stop service task and cancel works
  1476. * @pf: board private structure
  1477. *
  1478. * Return 0 if the ICE_SERVICE_DIS bit was not already set,
  1479. * 1 otherwise.
  1480. */
  1481. static int ice_service_task_stop(struct ice_pf *pf)
  1482. {
  1483. int ret;
  1484. ret = test_and_set_bit(ICE_SERVICE_DIS, pf->state);
  1485. if (pf->serv_tmr.function)
  1486. del_timer_sync(&pf->serv_tmr);
  1487. if (pf->serv_task.func)
  1488. cancel_work_sync(&pf->serv_task);
  1489. clear_bit(ICE_SERVICE_SCHED, pf->state);
  1490. return ret;
  1491. }
  1492. /**
  1493. * ice_service_task_restart - restart service task and schedule works
  1494. * @pf: board private structure
  1495. *
  1496. * This function is needed for suspend and resume works (e.g WoL scenario)
  1497. */
  1498. static void ice_service_task_restart(struct ice_pf *pf)
  1499. {
  1500. clear_bit(ICE_SERVICE_DIS, pf->state);
  1501. ice_service_task_schedule(pf);
  1502. }
  1503. /**
  1504. * ice_service_timer - timer callback to schedule service task
  1505. * @t: pointer to timer_list
  1506. */
  1507. static void ice_service_timer(struct timer_list *t)
  1508. {
  1509. struct ice_pf *pf = from_timer(pf, t, serv_tmr);
  1510. mod_timer(&pf->serv_tmr, round_jiffies(pf->serv_tmr_period + jiffies));
  1511. ice_service_task_schedule(pf);
  1512. }
  1513. /**
  1514. * ice_mdd_maybe_reset_vf - reset VF after MDD event
  1515. * @pf: pointer to the PF structure
  1516. * @vf: pointer to the VF structure
  1517. * @reset_vf_tx: whether Tx MDD has occurred
  1518. * @reset_vf_rx: whether Rx MDD has occurred
  1519. *
  1520. * Since the queue can get stuck on VF MDD events, the PF can be configured to
  1521. * automatically reset the VF by enabling the private ethtool flag
  1522. * mdd-auto-reset-vf.
  1523. */
  1524. static void ice_mdd_maybe_reset_vf(struct ice_pf *pf, struct ice_vf *vf,
  1525. bool reset_vf_tx, bool reset_vf_rx)
  1526. {
  1527. struct device *dev = ice_pf_to_dev(pf);
  1528. if (!test_bit(ICE_FLAG_MDD_AUTO_RESET_VF, pf->flags))
  1529. return;
  1530. /* VF MDD event counters will be cleared by reset, so print the event
  1531. * prior to reset.
  1532. */
  1533. if (reset_vf_tx)
  1534. ice_print_vf_tx_mdd_event(vf);
  1535. if (reset_vf_rx)
  1536. ice_print_vf_rx_mdd_event(vf);
  1537. dev_info(dev, "PF-to-VF reset on PF %d VF %d due to MDD event\n",
  1538. pf->hw.pf_id, vf->vf_id);
  1539. ice_reset_vf(vf, ICE_VF_RESET_NOTIFY | ICE_VF_RESET_LOCK);
  1540. }
  1541. /**
  1542. * ice_handle_mdd_event - handle malicious driver detect event
  1543. * @pf: pointer to the PF structure
  1544. *
  1545. * Called from service task. OICR interrupt handler indicates MDD event.
  1546. * VF MDD logging is guarded by net_ratelimit. Additional PF and VF log
  1547. * messages are wrapped by netif_msg_[rx|tx]_err. Since VF Rx MDD events
  1548. * disable the queue, the PF can be configured to reset the VF using ethtool
  1549. * private flag mdd-auto-reset-vf.
  1550. */
  1551. static void ice_handle_mdd_event(struct ice_pf *pf)
  1552. {
  1553. struct device *dev = ice_pf_to_dev(pf);
  1554. struct ice_hw *hw = &pf->hw;
  1555. struct ice_vf *vf;
  1556. unsigned int bkt;
  1557. u32 reg;
  1558. if (!test_and_clear_bit(ICE_MDD_EVENT_PENDING, pf->state)) {
  1559. /* Since the VF MDD event logging is rate limited, check if
  1560. * there are pending MDD events.
  1561. */
  1562. ice_print_vfs_mdd_events(pf);
  1563. return;
  1564. }
  1565. /* find what triggered an MDD event */
  1566. reg = rd32(hw, GL_MDET_TX_PQM);
  1567. if (reg & GL_MDET_TX_PQM_VALID_M) {
  1568. u8 pf_num = FIELD_GET(GL_MDET_TX_PQM_PF_NUM_M, reg);
  1569. u16 vf_num = FIELD_GET(GL_MDET_TX_PQM_VF_NUM_M, reg);
  1570. u8 event = FIELD_GET(GL_MDET_TX_PQM_MAL_TYPE_M, reg);
  1571. u16 queue = FIELD_GET(GL_MDET_TX_PQM_QNUM_M, reg);
  1572. if (netif_msg_tx_err(pf))
  1573. dev_info(dev, "Malicious Driver Detection event %d on TX queue %d PF# %d VF# %d\n",
  1574. event, queue, pf_num, vf_num);
  1575. wr32(hw, GL_MDET_TX_PQM, 0xffffffff);
  1576. }
  1577. reg = rd32(hw, GL_MDET_TX_TCLAN_BY_MAC(hw));
  1578. if (reg & GL_MDET_TX_TCLAN_VALID_M) {
  1579. u8 pf_num = FIELD_GET(GL_MDET_TX_TCLAN_PF_NUM_M, reg);
  1580. u16 vf_num = FIELD_GET(GL_MDET_TX_TCLAN_VF_NUM_M, reg);
  1581. u8 event = FIELD_GET(GL_MDET_TX_TCLAN_MAL_TYPE_M, reg);
  1582. u16 queue = FIELD_GET(GL_MDET_TX_TCLAN_QNUM_M, reg);
  1583. if (netif_msg_tx_err(pf))
  1584. dev_info(dev, "Malicious Driver Detection event %d on TX queue %d PF# %d VF# %d\n",
  1585. event, queue, pf_num, vf_num);
  1586. wr32(hw, GL_MDET_TX_TCLAN_BY_MAC(hw), U32_MAX);
  1587. }
  1588. reg = rd32(hw, GL_MDET_RX);
  1589. if (reg & GL_MDET_RX_VALID_M) {
  1590. u8 pf_num = FIELD_GET(GL_MDET_RX_PF_NUM_M, reg);
  1591. u16 vf_num = FIELD_GET(GL_MDET_RX_VF_NUM_M, reg);
  1592. u8 event = FIELD_GET(GL_MDET_RX_MAL_TYPE_M, reg);
  1593. u16 queue = FIELD_GET(GL_MDET_RX_QNUM_M, reg);
  1594. if (netif_msg_rx_err(pf))
  1595. dev_info(dev, "Malicious Driver Detection event %d on RX queue %d PF# %d VF# %d\n",
  1596. event, queue, pf_num, vf_num);
  1597. wr32(hw, GL_MDET_RX, 0xffffffff);
  1598. }
  1599. /* check to see if this PF caused an MDD event */
  1600. reg = rd32(hw, PF_MDET_TX_PQM);
  1601. if (reg & PF_MDET_TX_PQM_VALID_M) {
  1602. wr32(hw, PF_MDET_TX_PQM, 0xFFFF);
  1603. if (netif_msg_tx_err(pf))
  1604. dev_info(dev, "Malicious Driver Detection event TX_PQM detected on PF\n");
  1605. }
  1606. reg = rd32(hw, PF_MDET_TX_TCLAN_BY_MAC(hw));
  1607. if (reg & PF_MDET_TX_TCLAN_VALID_M) {
  1608. wr32(hw, PF_MDET_TX_TCLAN_BY_MAC(hw), 0xffff);
  1609. if (netif_msg_tx_err(pf))
  1610. dev_info(dev, "Malicious Driver Detection event TX_TCLAN detected on PF\n");
  1611. }
  1612. reg = rd32(hw, PF_MDET_RX);
  1613. if (reg & PF_MDET_RX_VALID_M) {
  1614. wr32(hw, PF_MDET_RX, 0xFFFF);
  1615. if (netif_msg_rx_err(pf))
  1616. dev_info(dev, "Malicious Driver Detection event RX detected on PF\n");
  1617. }
  1618. /* Check to see if one of the VFs caused an MDD event, and then
  1619. * increment counters and set print pending
  1620. */
  1621. mutex_lock(&pf->vfs.table_lock);
  1622. ice_for_each_vf(pf, bkt, vf) {
  1623. bool reset_vf_tx = false, reset_vf_rx = false;
  1624. reg = rd32(hw, VP_MDET_TX_PQM(vf->vf_id));
  1625. if (reg & VP_MDET_TX_PQM_VALID_M) {
  1626. wr32(hw, VP_MDET_TX_PQM(vf->vf_id), 0xFFFF);
  1627. vf->mdd_tx_events.count++;
  1628. set_bit(ICE_MDD_VF_PRINT_PENDING, pf->state);
  1629. if (netif_msg_tx_err(pf))
  1630. dev_info(dev, "Malicious Driver Detection event TX_PQM detected on VF %d\n",
  1631. vf->vf_id);
  1632. reset_vf_tx = true;
  1633. }
  1634. reg = rd32(hw, VP_MDET_TX_TCLAN(vf->vf_id));
  1635. if (reg & VP_MDET_TX_TCLAN_VALID_M) {
  1636. wr32(hw, VP_MDET_TX_TCLAN(vf->vf_id), 0xFFFF);
  1637. vf->mdd_tx_events.count++;
  1638. set_bit(ICE_MDD_VF_PRINT_PENDING, pf->state);
  1639. if (netif_msg_tx_err(pf))
  1640. dev_info(dev, "Malicious Driver Detection event TX_TCLAN detected on VF %d\n",
  1641. vf->vf_id);
  1642. reset_vf_tx = true;
  1643. }
  1644. reg = rd32(hw, VP_MDET_TX_TDPU(vf->vf_id));
  1645. if (reg & VP_MDET_TX_TDPU_VALID_M) {
  1646. wr32(hw, VP_MDET_TX_TDPU(vf->vf_id), 0xFFFF);
  1647. vf->mdd_tx_events.count++;
  1648. set_bit(ICE_MDD_VF_PRINT_PENDING, pf->state);
  1649. if (netif_msg_tx_err(pf))
  1650. dev_info(dev, "Malicious Driver Detection event TX_TDPU detected on VF %d\n",
  1651. vf->vf_id);
  1652. reset_vf_tx = true;
  1653. }
  1654. reg = rd32(hw, VP_MDET_RX(vf->vf_id));
  1655. if (reg & VP_MDET_RX_VALID_M) {
  1656. wr32(hw, VP_MDET_RX(vf->vf_id), 0xFFFF);
  1657. vf->mdd_rx_events.count++;
  1658. set_bit(ICE_MDD_VF_PRINT_PENDING, pf->state);
  1659. if (netif_msg_rx_err(pf))
  1660. dev_info(dev, "Malicious Driver Detection event RX detected on VF %d\n",
  1661. vf->vf_id);
  1662. reset_vf_rx = true;
  1663. }
  1664. if (reset_vf_tx || reset_vf_rx)
  1665. ice_mdd_maybe_reset_vf(pf, vf, reset_vf_tx,
  1666. reset_vf_rx);
  1667. }
  1668. mutex_unlock(&pf->vfs.table_lock);
  1669. ice_print_vfs_mdd_events(pf);
  1670. }
  1671. /**
  1672. * ice_force_phys_link_state - Force the physical link state
  1673. * @vsi: VSI to force the physical link state to up/down
  1674. * @link_up: true/false indicates to set the physical link to up/down
  1675. *
  1676. * Force the physical link state by getting the current PHY capabilities from
  1677. * hardware and setting the PHY config based on the determined capabilities. If
  1678. * link changes a link event will be triggered because both the Enable Automatic
  1679. * Link Update and LESM Enable bits are set when setting the PHY capabilities.
  1680. *
  1681. * Returns 0 on success, negative on failure
  1682. */
  1683. static int ice_force_phys_link_state(struct ice_vsi *vsi, bool link_up)
  1684. {
  1685. struct ice_aqc_get_phy_caps_data *pcaps;
  1686. struct ice_aqc_set_phy_cfg_data *cfg;
  1687. struct ice_port_info *pi;
  1688. struct device *dev;
  1689. int retcode;
  1690. if (!vsi || !vsi->port_info || !vsi->back)
  1691. return -EINVAL;
  1692. if (vsi->type != ICE_VSI_PF)
  1693. return 0;
  1694. dev = ice_pf_to_dev(vsi->back);
  1695. pi = vsi->port_info;
  1696. pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
  1697. if (!pcaps)
  1698. return -ENOMEM;
  1699. retcode = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_ACTIVE_CFG, pcaps,
  1700. NULL);
  1701. if (retcode) {
  1702. dev_err(dev, "Failed to get phy capabilities, VSI %d error %d\n",
  1703. vsi->vsi_num, retcode);
  1704. retcode = -EIO;
  1705. goto out;
  1706. }
  1707. /* No change in link */
  1708. if (link_up == !!(pcaps->caps & ICE_AQC_PHY_EN_LINK) &&
  1709. link_up == !!(pi->phy.link_info.link_info & ICE_AQ_LINK_UP))
  1710. goto out;
  1711. /* Use the current user PHY configuration. The current user PHY
  1712. * configuration is initialized during probe from PHY capabilities
  1713. * software mode, and updated on set PHY configuration.
  1714. */
  1715. cfg = kmemdup(&pi->phy.curr_user_phy_cfg, sizeof(*cfg), GFP_KERNEL);
  1716. if (!cfg) {
  1717. retcode = -ENOMEM;
  1718. goto out;
  1719. }
  1720. cfg->caps |= ICE_AQ_PHY_ENA_AUTO_LINK_UPDT;
  1721. if (link_up)
  1722. cfg->caps |= ICE_AQ_PHY_ENA_LINK;
  1723. else
  1724. cfg->caps &= ~ICE_AQ_PHY_ENA_LINK;
  1725. retcode = ice_aq_set_phy_cfg(&vsi->back->hw, pi, cfg, NULL);
  1726. if (retcode) {
  1727. dev_err(dev, "Failed to set phy config, VSI %d error %d\n",
  1728. vsi->vsi_num, retcode);
  1729. retcode = -EIO;
  1730. }
  1731. kfree(cfg);
  1732. out:
  1733. kfree(pcaps);
  1734. return retcode;
  1735. }
  1736. /**
  1737. * ice_init_nvm_phy_type - Initialize the NVM PHY type
  1738. * @pi: port info structure
  1739. *
  1740. * Initialize nvm_phy_type_[low|high] for link lenient mode support
  1741. */
  1742. static int ice_init_nvm_phy_type(struct ice_port_info *pi)
  1743. {
  1744. struct ice_aqc_get_phy_caps_data *pcaps;
  1745. struct ice_pf *pf = pi->hw->back;
  1746. int err;
  1747. pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
  1748. if (!pcaps)
  1749. return -ENOMEM;
  1750. err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_TOPO_CAP_NO_MEDIA,
  1751. pcaps, NULL);
  1752. if (err) {
  1753. dev_err(ice_pf_to_dev(pf), "Get PHY capability failed.\n");
  1754. goto out;
  1755. }
  1756. pf->nvm_phy_type_hi = pcaps->phy_type_high;
  1757. pf->nvm_phy_type_lo = pcaps->phy_type_low;
  1758. out:
  1759. kfree(pcaps);
  1760. return err;
  1761. }
  1762. /**
  1763. * ice_init_link_dflt_override - Initialize link default override
  1764. * @pi: port info structure
  1765. *
  1766. * Initialize link default override and PHY total port shutdown during probe
  1767. */
  1768. static void ice_init_link_dflt_override(struct ice_port_info *pi)
  1769. {
  1770. struct ice_link_default_override_tlv *ldo;
  1771. struct ice_pf *pf = pi->hw->back;
  1772. ldo = &pf->link_dflt_override;
  1773. if (ice_get_link_default_override(ldo, pi))
  1774. return;
  1775. if (!(ldo->options & ICE_LINK_OVERRIDE_PORT_DIS))
  1776. return;
  1777. /* Enable Total Port Shutdown (override/replace link-down-on-close
  1778. * ethtool private flag) for ports with Port Disable bit set.
  1779. */
  1780. set_bit(ICE_FLAG_TOTAL_PORT_SHUTDOWN_ENA, pf->flags);
  1781. set_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags);
  1782. }
  1783. /**
  1784. * ice_init_phy_cfg_dflt_override - Initialize PHY cfg default override settings
  1785. * @pi: port info structure
  1786. *
  1787. * If default override is enabled, initialize the user PHY cfg speed and FEC
  1788. * settings using the default override mask from the NVM.
  1789. *
  1790. * The PHY should only be configured with the default override settings the
  1791. * first time media is available. The ICE_LINK_DEFAULT_OVERRIDE_PENDING state
  1792. * is used to indicate that the user PHY cfg default override is initialized
  1793. * and the PHY has not been configured with the default override settings. The
  1794. * state is set here, and cleared in ice_configure_phy the first time the PHY is
  1795. * configured.
  1796. *
  1797. * This function should be called only if the FW doesn't support default
  1798. * configuration mode, as reported by ice_fw_supports_report_dflt_cfg.
  1799. */
  1800. static void ice_init_phy_cfg_dflt_override(struct ice_port_info *pi)
  1801. {
  1802. struct ice_link_default_override_tlv *ldo;
  1803. struct ice_aqc_set_phy_cfg_data *cfg;
  1804. struct ice_phy_info *phy = &pi->phy;
  1805. struct ice_pf *pf = pi->hw->back;
  1806. ldo = &pf->link_dflt_override;
  1807. /* If link default override is enabled, use to mask NVM PHY capabilities
  1808. * for speed and FEC default configuration.
  1809. */
  1810. cfg = &phy->curr_user_phy_cfg;
  1811. if (ldo->phy_type_low || ldo->phy_type_high) {
  1812. cfg->phy_type_low = pf->nvm_phy_type_lo &
  1813. cpu_to_le64(ldo->phy_type_low);
  1814. cfg->phy_type_high = pf->nvm_phy_type_hi &
  1815. cpu_to_le64(ldo->phy_type_high);
  1816. }
  1817. cfg->link_fec_opt = ldo->fec_options;
  1818. phy->curr_user_fec_req = ICE_FEC_AUTO;
  1819. set_bit(ICE_LINK_DEFAULT_OVERRIDE_PENDING, pf->state);
  1820. }
  1821. /**
  1822. * ice_init_phy_user_cfg - Initialize the PHY user configuration
  1823. * @pi: port info structure
  1824. *
  1825. * Initialize the current user PHY configuration, speed, FEC, and FC requested
  1826. * mode to default. The PHY defaults are from get PHY capabilities topology
  1827. * with media so call when media is first available. An error is returned if
  1828. * called when media is not available. The PHY initialization completed state is
  1829. * set here.
  1830. *
  1831. * These configurations are used when setting PHY
  1832. * configuration. The user PHY configuration is updated on set PHY
  1833. * configuration. Returns 0 on success, negative on failure
  1834. */
  1835. static int ice_init_phy_user_cfg(struct ice_port_info *pi)
  1836. {
  1837. struct ice_aqc_get_phy_caps_data *pcaps;
  1838. struct ice_phy_info *phy = &pi->phy;
  1839. struct ice_pf *pf = pi->hw->back;
  1840. int err;
  1841. if (!(phy->link_info.link_info & ICE_AQ_MEDIA_AVAILABLE))
  1842. return -EIO;
  1843. pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
  1844. if (!pcaps)
  1845. return -ENOMEM;
  1846. if (ice_fw_supports_report_dflt_cfg(pi->hw))
  1847. err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_DFLT_CFG,
  1848. pcaps, NULL);
  1849. else
  1850. err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_TOPO_CAP_MEDIA,
  1851. pcaps, NULL);
  1852. if (err) {
  1853. dev_err(ice_pf_to_dev(pf), "Get PHY capability failed.\n");
  1854. goto err_out;
  1855. }
  1856. ice_copy_phy_caps_to_cfg(pi, pcaps, &pi->phy.curr_user_phy_cfg);
  1857. /* check if lenient mode is supported and enabled */
  1858. if (ice_fw_supports_link_override(pi->hw) &&
  1859. !(pcaps->module_compliance_enforcement &
  1860. ICE_AQC_MOD_ENFORCE_STRICT_MODE)) {
  1861. set_bit(ICE_FLAG_LINK_LENIENT_MODE_ENA, pf->flags);
  1862. /* if the FW supports default PHY configuration mode, then the driver
  1863. * does not have to apply link override settings. If not,
  1864. * initialize user PHY configuration with link override values
  1865. */
  1866. if (!ice_fw_supports_report_dflt_cfg(pi->hw) &&
  1867. (pf->link_dflt_override.options & ICE_LINK_OVERRIDE_EN)) {
  1868. ice_init_phy_cfg_dflt_override(pi);
  1869. goto out;
  1870. }
  1871. }
  1872. /* if link default override is not enabled, set user flow control and
  1873. * FEC settings based on what get_phy_caps returned
  1874. */
  1875. phy->curr_user_fec_req = ice_caps_to_fec_mode(pcaps->caps,
  1876. pcaps->link_fec_options);
  1877. phy->curr_user_fc_req = ice_caps_to_fc_mode(pcaps->caps);
  1878. out:
  1879. phy->curr_user_speed_req = ICE_AQ_LINK_SPEED_M;
  1880. set_bit(ICE_PHY_INIT_COMPLETE, pf->state);
  1881. err_out:
  1882. kfree(pcaps);
  1883. return err;
  1884. }
  1885. /**
  1886. * ice_configure_phy - configure PHY
  1887. * @vsi: VSI of PHY
  1888. *
  1889. * Set the PHY configuration. If the current PHY configuration is the same as
  1890. * the curr_user_phy_cfg, then do nothing to avoid link flap. Otherwise
  1891. * configure the based get PHY capabilities for topology with media.
  1892. */
  1893. static int ice_configure_phy(struct ice_vsi *vsi)
  1894. {
  1895. struct device *dev = ice_pf_to_dev(vsi->back);
  1896. struct ice_port_info *pi = vsi->port_info;
  1897. struct ice_aqc_get_phy_caps_data *pcaps;
  1898. struct ice_aqc_set_phy_cfg_data *cfg;
  1899. struct ice_phy_info *phy = &pi->phy;
  1900. struct ice_pf *pf = vsi->back;
  1901. int err;
  1902. /* Ensure we have media as we cannot configure a medialess port */
  1903. if (!(phy->link_info.link_info & ICE_AQ_MEDIA_AVAILABLE))
  1904. return -ENOMEDIUM;
  1905. ice_print_topo_conflict(vsi);
  1906. if (!test_bit(ICE_FLAG_LINK_LENIENT_MODE_ENA, pf->flags) &&
  1907. phy->link_info.topo_media_conflict == ICE_AQ_LINK_TOPO_UNSUPP_MEDIA)
  1908. return -EPERM;
  1909. if (test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags))
  1910. return ice_force_phys_link_state(vsi, true);
  1911. pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
  1912. if (!pcaps)
  1913. return -ENOMEM;
  1914. /* Get current PHY config */
  1915. err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_ACTIVE_CFG, pcaps,
  1916. NULL);
  1917. if (err) {
  1918. dev_err(dev, "Failed to get PHY configuration, VSI %d error %d\n",
  1919. vsi->vsi_num, err);
  1920. goto done;
  1921. }
  1922. /* If PHY enable link is configured and configuration has not changed,
  1923. * there's nothing to do
  1924. */
  1925. if (pcaps->caps & ICE_AQC_PHY_EN_LINK &&
  1926. ice_phy_caps_equals_cfg(pcaps, &phy->curr_user_phy_cfg))
  1927. goto done;
  1928. /* Use PHY topology as baseline for configuration */
  1929. memset(pcaps, 0, sizeof(*pcaps));
  1930. if (ice_fw_supports_report_dflt_cfg(pi->hw))
  1931. err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_DFLT_CFG,
  1932. pcaps, NULL);
  1933. else
  1934. err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_TOPO_CAP_MEDIA,
  1935. pcaps, NULL);
  1936. if (err) {
  1937. dev_err(dev, "Failed to get PHY caps, VSI %d error %d\n",
  1938. vsi->vsi_num, err);
  1939. goto done;
  1940. }
  1941. cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
  1942. if (!cfg) {
  1943. err = -ENOMEM;
  1944. goto done;
  1945. }
  1946. ice_copy_phy_caps_to_cfg(pi, pcaps, cfg);
  1947. /* Speed - If default override pending, use curr_user_phy_cfg set in
  1948. * ice_init_phy_user_cfg_ldo.
  1949. */
  1950. if (test_and_clear_bit(ICE_LINK_DEFAULT_OVERRIDE_PENDING,
  1951. vsi->back->state)) {
  1952. cfg->phy_type_low = phy->curr_user_phy_cfg.phy_type_low;
  1953. cfg->phy_type_high = phy->curr_user_phy_cfg.phy_type_high;
  1954. } else {
  1955. u64 phy_low = 0, phy_high = 0;
  1956. ice_update_phy_type(&phy_low, &phy_high,
  1957. pi->phy.curr_user_speed_req);
  1958. cfg->phy_type_low = pcaps->phy_type_low & cpu_to_le64(phy_low);
  1959. cfg->phy_type_high = pcaps->phy_type_high &
  1960. cpu_to_le64(phy_high);
  1961. }
  1962. /* Can't provide what was requested; use PHY capabilities */
  1963. if (!cfg->phy_type_low && !cfg->phy_type_high) {
  1964. cfg->phy_type_low = pcaps->phy_type_low;
  1965. cfg->phy_type_high = pcaps->phy_type_high;
  1966. }
  1967. /* FEC */
  1968. ice_cfg_phy_fec(pi, cfg, phy->curr_user_fec_req);
  1969. /* Can't provide what was requested; use PHY capabilities */
  1970. if (cfg->link_fec_opt !=
  1971. (cfg->link_fec_opt & pcaps->link_fec_options)) {
  1972. cfg->caps |= pcaps->caps & ICE_AQC_PHY_EN_AUTO_FEC;
  1973. cfg->link_fec_opt = pcaps->link_fec_options;
  1974. }
  1975. /* Flow Control - always supported; no need to check against
  1976. * capabilities
  1977. */
  1978. ice_cfg_phy_fc(pi, cfg, phy->curr_user_fc_req);
  1979. /* Enable link and link update */
  1980. cfg->caps |= ICE_AQ_PHY_ENA_AUTO_LINK_UPDT | ICE_AQ_PHY_ENA_LINK;
  1981. err = ice_aq_set_phy_cfg(&pf->hw, pi, cfg, NULL);
  1982. if (err)
  1983. dev_err(dev, "Failed to set phy config, VSI %d error %d\n",
  1984. vsi->vsi_num, err);
  1985. kfree(cfg);
  1986. done:
  1987. kfree(pcaps);
  1988. return err;
  1989. }
  1990. /**
  1991. * ice_check_media_subtask - Check for media
  1992. * @pf: pointer to PF struct
  1993. *
  1994. * If media is available, then initialize PHY user configuration if it is not
  1995. * been, and configure the PHY if the interface is up.
  1996. */
  1997. static void ice_check_media_subtask(struct ice_pf *pf)
  1998. {
  1999. struct ice_port_info *pi;
  2000. struct ice_vsi *vsi;
  2001. int err;
  2002. /* No need to check for media if it's already present */
  2003. if (!test_bit(ICE_FLAG_NO_MEDIA, pf->flags))
  2004. return;
  2005. vsi = ice_get_main_vsi(pf);
  2006. if (!vsi)
  2007. return;
  2008. /* Refresh link info and check if media is present */
  2009. pi = vsi->port_info;
  2010. err = ice_update_link_info(pi);
  2011. if (err)
  2012. return;
  2013. ice_check_link_cfg_err(pf, pi->phy.link_info.link_cfg_err);
  2014. if (pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE) {
  2015. if (!test_bit(ICE_PHY_INIT_COMPLETE, pf->state))
  2016. ice_init_phy_user_cfg(pi);
  2017. /* PHY settings are reset on media insertion, reconfigure
  2018. * PHY to preserve settings.
  2019. */
  2020. if (test_bit(ICE_VSI_DOWN, vsi->state) &&
  2021. test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, vsi->back->flags))
  2022. return;
  2023. err = ice_configure_phy(vsi);
  2024. if (!err)
  2025. clear_bit(ICE_FLAG_NO_MEDIA, pf->flags);
  2026. /* A Link Status Event will be generated; the event handler
  2027. * will complete bringing the interface up
  2028. */
  2029. }
  2030. }
  2031. /**
  2032. * ice_service_task - manage and run subtasks
  2033. * @work: pointer to work_struct contained by the PF struct
  2034. */
  2035. static void ice_service_task(struct work_struct *work)
  2036. {
  2037. struct ice_pf *pf = container_of(work, struct ice_pf, serv_task);
  2038. unsigned long start_time = jiffies;
  2039. /* subtasks */
  2040. /* process reset requests first */
  2041. ice_reset_subtask(pf);
  2042. /* bail if a reset/recovery cycle is pending or rebuild failed */
  2043. if (ice_is_reset_in_progress(pf->state) ||
  2044. test_bit(ICE_SUSPENDED, pf->state) ||
  2045. test_bit(ICE_NEEDS_RESTART, pf->state)) {
  2046. ice_service_task_complete(pf);
  2047. return;
  2048. }
  2049. if (test_and_clear_bit(ICE_AUX_ERR_PENDING, pf->state)) {
  2050. struct iidc_event *event;
  2051. event = kzalloc(sizeof(*event), GFP_KERNEL);
  2052. if (event) {
  2053. set_bit(IIDC_EVENT_CRIT_ERR, event->type);
  2054. /* report the entire OICR value to AUX driver */
  2055. swap(event->reg, pf->oicr_err_reg);
  2056. ice_send_event_to_aux(pf, event);
  2057. kfree(event);
  2058. }
  2059. }
  2060. /* unplug aux dev per request, if an unplug request came in
  2061. * while processing a plug request, this will handle it
  2062. */
  2063. if (test_and_clear_bit(ICE_FLAG_UNPLUG_AUX_DEV, pf->flags))
  2064. ice_unplug_aux_dev(pf);
  2065. /* Plug aux device per request */
  2066. if (test_and_clear_bit(ICE_FLAG_PLUG_AUX_DEV, pf->flags))
  2067. ice_plug_aux_dev(pf);
  2068. if (test_and_clear_bit(ICE_FLAG_MTU_CHANGED, pf->flags)) {
  2069. struct iidc_event *event;
  2070. event = kzalloc(sizeof(*event), GFP_KERNEL);
  2071. if (event) {
  2072. set_bit(IIDC_EVENT_AFTER_MTU_CHANGE, event->type);
  2073. ice_send_event_to_aux(pf, event);
  2074. kfree(event);
  2075. }
  2076. }
  2077. ice_clean_adminq_subtask(pf);
  2078. ice_check_media_subtask(pf);
  2079. ice_check_for_hang_subtask(pf);
  2080. ice_sync_fltr_subtask(pf);
  2081. ice_handle_mdd_event(pf);
  2082. ice_watchdog_subtask(pf);
  2083. if (ice_is_safe_mode(pf)) {
  2084. ice_service_task_complete(pf);
  2085. return;
  2086. }
  2087. ice_process_vflr_event(pf);
  2088. ice_clean_mailboxq_subtask(pf);
  2089. ice_clean_sbq_subtask(pf);
  2090. ice_sync_arfs_fltrs(pf);
  2091. ice_flush_fdir_ctx(pf);
  2092. /* Clear ICE_SERVICE_SCHED flag to allow scheduling next event */
  2093. ice_service_task_complete(pf);
  2094. /* If the tasks have taken longer than one service timer period
  2095. * or there is more work to be done, reset the service timer to
  2096. * schedule the service task now.
  2097. */
  2098. if (time_after(jiffies, (start_time + pf->serv_tmr_period)) ||
  2099. test_bit(ICE_MDD_EVENT_PENDING, pf->state) ||
  2100. test_bit(ICE_VFLR_EVENT_PENDING, pf->state) ||
  2101. test_bit(ICE_MAILBOXQ_EVENT_PENDING, pf->state) ||
  2102. test_bit(ICE_FD_VF_FLUSH_CTX, pf->state) ||
  2103. test_bit(ICE_SIDEBANDQ_EVENT_PENDING, pf->state) ||
  2104. test_bit(ICE_ADMINQ_EVENT_PENDING, pf->state))
  2105. mod_timer(&pf->serv_tmr, jiffies);
  2106. }
  2107. /**
  2108. * ice_set_ctrlq_len - helper function to set controlq length
  2109. * @hw: pointer to the HW instance
  2110. */
  2111. static void ice_set_ctrlq_len(struct ice_hw *hw)
  2112. {
  2113. hw->adminq.num_rq_entries = ICE_AQ_LEN;
  2114. hw->adminq.num_sq_entries = ICE_AQ_LEN;
  2115. hw->adminq.rq_buf_size = ICE_AQ_MAX_BUF_LEN;
  2116. hw->adminq.sq_buf_size = ICE_AQ_MAX_BUF_LEN;
  2117. hw->mailboxq.num_rq_entries = PF_MBX_ARQLEN_ARQLEN_M;
  2118. hw->mailboxq.num_sq_entries = ICE_MBXSQ_LEN;
  2119. hw->mailboxq.rq_buf_size = ICE_MBXQ_MAX_BUF_LEN;
  2120. hw->mailboxq.sq_buf_size = ICE_MBXQ_MAX_BUF_LEN;
  2121. hw->sbq.num_rq_entries = ICE_SBQ_LEN;
  2122. hw->sbq.num_sq_entries = ICE_SBQ_LEN;
  2123. hw->sbq.rq_buf_size = ICE_SBQ_MAX_BUF_LEN;
  2124. hw->sbq.sq_buf_size = ICE_SBQ_MAX_BUF_LEN;
  2125. }
  2126. /**
  2127. * ice_schedule_reset - schedule a reset
  2128. * @pf: board private structure
  2129. * @reset: reset being requested
  2130. */
  2131. int ice_schedule_reset(struct ice_pf *pf, enum ice_reset_req reset)
  2132. {
  2133. struct device *dev = ice_pf_to_dev(pf);
  2134. /* bail out if earlier reset has failed */
  2135. if (test_bit(ICE_RESET_FAILED, pf->state)) {
  2136. dev_dbg(dev, "earlier reset has failed\n");
  2137. return -EIO;
  2138. }
  2139. /* bail if reset/recovery already in progress */
  2140. if (ice_is_reset_in_progress(pf->state)) {
  2141. dev_dbg(dev, "Reset already in progress\n");
  2142. return -EBUSY;
  2143. }
  2144. switch (reset) {
  2145. case ICE_RESET_PFR:
  2146. set_bit(ICE_PFR_REQ, pf->state);
  2147. break;
  2148. case ICE_RESET_CORER:
  2149. set_bit(ICE_CORER_REQ, pf->state);
  2150. break;
  2151. case ICE_RESET_GLOBR:
  2152. set_bit(ICE_GLOBR_REQ, pf->state);
  2153. break;
  2154. default:
  2155. return -EINVAL;
  2156. }
  2157. ice_service_task_schedule(pf);
  2158. return 0;
  2159. }
  2160. /**
  2161. * ice_irq_affinity_notify - Callback for affinity changes
  2162. * @notify: context as to what irq was changed
  2163. * @mask: the new affinity mask
  2164. *
  2165. * This is a callback function used by the irq_set_affinity_notifier function
  2166. * so that we may register to receive changes to the irq affinity masks.
  2167. */
  2168. static void
  2169. ice_irq_affinity_notify(struct irq_affinity_notify *notify,
  2170. const cpumask_t *mask)
  2171. {
  2172. struct ice_q_vector *q_vector =
  2173. container_of(notify, struct ice_q_vector, affinity_notify);
  2174. cpumask_copy(&q_vector->affinity_mask, mask);
  2175. }
  2176. /**
  2177. * ice_irq_affinity_release - Callback for affinity notifier release
  2178. * @ref: internal core kernel usage
  2179. *
  2180. * This is a callback function used by the irq_set_affinity_notifier function
  2181. * to inform the current notification subscriber that they will no longer
  2182. * receive notifications.
  2183. */
  2184. static void ice_irq_affinity_release(struct kref __always_unused *ref) {}
  2185. /**
  2186. * ice_vsi_ena_irq - Enable IRQ for the given VSI
  2187. * @vsi: the VSI being configured
  2188. */
  2189. static int ice_vsi_ena_irq(struct ice_vsi *vsi)
  2190. {
  2191. struct ice_hw *hw = &vsi->back->hw;
  2192. int i;
  2193. ice_for_each_q_vector(vsi, i)
  2194. ice_irq_dynamic_ena(hw, vsi, vsi->q_vectors[i]);
  2195. ice_flush(hw);
  2196. return 0;
  2197. }
  2198. /**
  2199. * ice_vsi_req_irq_msix - get MSI-X vectors from the OS for the VSI
  2200. * @vsi: the VSI being configured
  2201. * @basename: name for the vector
  2202. */
  2203. static int ice_vsi_req_irq_msix(struct ice_vsi *vsi, char *basename)
  2204. {
  2205. int q_vectors = vsi->num_q_vectors;
  2206. struct ice_pf *pf = vsi->back;
  2207. struct device *dev;
  2208. int rx_int_idx = 0;
  2209. int tx_int_idx = 0;
  2210. int vector, err;
  2211. int irq_num;
  2212. dev = ice_pf_to_dev(pf);
  2213. for (vector = 0; vector < q_vectors; vector++) {
  2214. struct ice_q_vector *q_vector = vsi->q_vectors[vector];
  2215. irq_num = q_vector->irq.virq;
  2216. if (q_vector->tx.tx_ring && q_vector->rx.rx_ring) {
  2217. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  2218. "%s-%s-%d", basename, "TxRx", rx_int_idx++);
  2219. tx_int_idx++;
  2220. } else if (q_vector->rx.rx_ring) {
  2221. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  2222. "%s-%s-%d", basename, "rx", rx_int_idx++);
  2223. } else if (q_vector->tx.tx_ring) {
  2224. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  2225. "%s-%s-%d", basename, "tx", tx_int_idx++);
  2226. } else {
  2227. /* skip this unused q_vector */
  2228. continue;
  2229. }
  2230. if (vsi->type == ICE_VSI_CTRL && vsi->vf)
  2231. err = devm_request_irq(dev, irq_num, vsi->irq_handler,
  2232. IRQF_SHARED, q_vector->name,
  2233. q_vector);
  2234. else
  2235. err = devm_request_irq(dev, irq_num, vsi->irq_handler,
  2236. 0, q_vector->name, q_vector);
  2237. if (err) {
  2238. netdev_err(vsi->netdev, "MSIX request_irq failed, error: %d\n",
  2239. err);
  2240. goto free_q_irqs;
  2241. }
  2242. /* register for affinity change notifications */
  2243. if (!IS_ENABLED(CONFIG_RFS_ACCEL)) {
  2244. struct irq_affinity_notify *affinity_notify;
  2245. affinity_notify = &q_vector->affinity_notify;
  2246. affinity_notify->notify = ice_irq_affinity_notify;
  2247. affinity_notify->release = ice_irq_affinity_release;
  2248. irq_set_affinity_notifier(irq_num, affinity_notify);
  2249. }
  2250. /* assign the mask for this irq */
  2251. irq_update_affinity_hint(irq_num, &q_vector->affinity_mask);
  2252. }
  2253. err = ice_set_cpu_rx_rmap(vsi);
  2254. if (err) {
  2255. netdev_err(vsi->netdev, "Failed to setup CPU RMAP on VSI %u: %pe\n",
  2256. vsi->vsi_num, ERR_PTR(err));
  2257. goto free_q_irqs;
  2258. }
  2259. vsi->irqs_ready = true;
  2260. return 0;
  2261. free_q_irqs:
  2262. while (vector--) {
  2263. irq_num = vsi->q_vectors[vector]->irq.virq;
  2264. if (!IS_ENABLED(CONFIG_RFS_ACCEL))
  2265. irq_set_affinity_notifier(irq_num, NULL);
  2266. irq_update_affinity_hint(irq_num, NULL);
  2267. devm_free_irq(dev, irq_num, &vsi->q_vectors[vector]);
  2268. }
  2269. return err;
  2270. }
  2271. /**
  2272. * ice_xdp_alloc_setup_rings - Allocate and setup Tx rings for XDP
  2273. * @vsi: VSI to setup Tx rings used by XDP
  2274. *
  2275. * Return 0 on success and negative value on error
  2276. */
  2277. static int ice_xdp_alloc_setup_rings(struct ice_vsi *vsi)
  2278. {
  2279. struct device *dev = ice_pf_to_dev(vsi->back);
  2280. struct ice_tx_desc *tx_desc;
  2281. int i, j;
  2282. ice_for_each_xdp_txq(vsi, i) {
  2283. u16 xdp_q_idx = vsi->alloc_txq + i;
  2284. struct ice_ring_stats *ring_stats;
  2285. struct ice_tx_ring *xdp_ring;
  2286. xdp_ring = kzalloc(sizeof(*xdp_ring), GFP_KERNEL);
  2287. if (!xdp_ring)
  2288. goto free_xdp_rings;
  2289. ring_stats = kzalloc(sizeof(*ring_stats), GFP_KERNEL);
  2290. if (!ring_stats) {
  2291. ice_free_tx_ring(xdp_ring);
  2292. goto free_xdp_rings;
  2293. }
  2294. xdp_ring->ring_stats = ring_stats;
  2295. xdp_ring->q_index = xdp_q_idx;
  2296. xdp_ring->reg_idx = vsi->txq_map[xdp_q_idx];
  2297. xdp_ring->vsi = vsi;
  2298. xdp_ring->netdev = NULL;
  2299. xdp_ring->dev = dev;
  2300. xdp_ring->count = vsi->num_tx_desc;
  2301. WRITE_ONCE(vsi->xdp_rings[i], xdp_ring);
  2302. if (ice_setup_tx_ring(xdp_ring))
  2303. goto free_xdp_rings;
  2304. ice_set_ring_xdp(xdp_ring);
  2305. spin_lock_init(&xdp_ring->tx_lock);
  2306. for (j = 0; j < xdp_ring->count; j++) {
  2307. tx_desc = ICE_TX_DESC(xdp_ring, j);
  2308. tx_desc->cmd_type_offset_bsz = 0;
  2309. }
  2310. }
  2311. return 0;
  2312. free_xdp_rings:
  2313. for (; i >= 0; i--) {
  2314. if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc) {
  2315. kfree_rcu(vsi->xdp_rings[i]->ring_stats, rcu);
  2316. vsi->xdp_rings[i]->ring_stats = NULL;
  2317. ice_free_tx_ring(vsi->xdp_rings[i]);
  2318. }
  2319. }
  2320. return -ENOMEM;
  2321. }
  2322. /**
  2323. * ice_vsi_assign_bpf_prog - set or clear bpf prog pointer on VSI
  2324. * @vsi: VSI to set the bpf prog on
  2325. * @prog: the bpf prog pointer
  2326. */
  2327. static void ice_vsi_assign_bpf_prog(struct ice_vsi *vsi, struct bpf_prog *prog)
  2328. {
  2329. struct bpf_prog *old_prog;
  2330. int i;
  2331. old_prog = xchg(&vsi->xdp_prog, prog);
  2332. ice_for_each_rxq(vsi, i)
  2333. WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
  2334. if (old_prog)
  2335. bpf_prog_put(old_prog);
  2336. }
  2337. static struct ice_tx_ring *ice_xdp_ring_from_qid(struct ice_vsi *vsi, int qid)
  2338. {
  2339. struct ice_q_vector *q_vector;
  2340. struct ice_tx_ring *ring;
  2341. if (static_key_enabled(&ice_xdp_locking_key))
  2342. return vsi->xdp_rings[qid % vsi->num_xdp_txq];
  2343. q_vector = vsi->rx_rings[qid]->q_vector;
  2344. ice_for_each_tx_ring(ring, q_vector->tx)
  2345. if (ice_ring_is_xdp(ring))
  2346. return ring;
  2347. return NULL;
  2348. }
  2349. /**
  2350. * ice_map_xdp_rings - Map XDP rings to interrupt vectors
  2351. * @vsi: the VSI with XDP rings being configured
  2352. *
  2353. * Map XDP rings to interrupt vectors and perform the configuration steps
  2354. * dependent on the mapping.
  2355. */
  2356. void ice_map_xdp_rings(struct ice_vsi *vsi)
  2357. {
  2358. int xdp_rings_rem = vsi->num_xdp_txq;
  2359. int v_idx, q_idx;
  2360. /* follow the logic from ice_vsi_map_rings_to_vectors */
  2361. ice_for_each_q_vector(vsi, v_idx) {
  2362. struct ice_q_vector *q_vector = vsi->q_vectors[v_idx];
  2363. int xdp_rings_per_v, q_id, q_base;
  2364. xdp_rings_per_v = DIV_ROUND_UP(xdp_rings_rem,
  2365. vsi->num_q_vectors - v_idx);
  2366. q_base = vsi->num_xdp_txq - xdp_rings_rem;
  2367. for (q_id = q_base; q_id < (q_base + xdp_rings_per_v); q_id++) {
  2368. struct ice_tx_ring *xdp_ring = vsi->xdp_rings[q_id];
  2369. xdp_ring->q_vector = q_vector;
  2370. xdp_ring->next = q_vector->tx.tx_ring;
  2371. q_vector->tx.tx_ring = xdp_ring;
  2372. }
  2373. xdp_rings_rem -= xdp_rings_per_v;
  2374. }
  2375. ice_for_each_rxq(vsi, q_idx) {
  2376. vsi->rx_rings[q_idx]->xdp_ring = ice_xdp_ring_from_qid(vsi,
  2377. q_idx);
  2378. ice_tx_xsk_pool(vsi, q_idx);
  2379. }
  2380. }
  2381. /**
  2382. * ice_unmap_xdp_rings - Unmap XDP rings from interrupt vectors
  2383. * @vsi: the VSI with XDP rings being unmapped
  2384. */
  2385. static void ice_unmap_xdp_rings(struct ice_vsi *vsi)
  2386. {
  2387. int v_idx;
  2388. ice_for_each_q_vector(vsi, v_idx) {
  2389. struct ice_q_vector *q_vector = vsi->q_vectors[v_idx];
  2390. struct ice_tx_ring *ring;
  2391. ice_for_each_tx_ring(ring, q_vector->tx)
  2392. if (!ring->tx_buf || !ice_ring_is_xdp(ring))
  2393. break;
  2394. /* restore the value of last node prior to XDP setup */
  2395. q_vector->tx.tx_ring = ring;
  2396. }
  2397. }
  2398. /**
  2399. * ice_prepare_xdp_rings - Allocate, configure and setup Tx rings for XDP
  2400. * @vsi: VSI to bring up Tx rings used by XDP
  2401. * @prog: bpf program that will be assigned to VSI
  2402. * @cfg_type: create from scratch or restore the existing configuration
  2403. *
  2404. * Return 0 on success and negative value on error
  2405. */
  2406. int ice_prepare_xdp_rings(struct ice_vsi *vsi, struct bpf_prog *prog,
  2407. enum ice_xdp_cfg cfg_type)
  2408. {
  2409. u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
  2410. struct ice_pf *pf = vsi->back;
  2411. struct ice_qs_cfg xdp_qs_cfg = {
  2412. .qs_mutex = &pf->avail_q_mutex,
  2413. .pf_map = pf->avail_txqs,
  2414. .pf_map_size = pf->max_pf_txqs,
  2415. .q_count = vsi->num_xdp_txq,
  2416. .scatter_count = ICE_MAX_SCATTER_TXQS,
  2417. .vsi_map = vsi->txq_map,
  2418. .vsi_map_offset = vsi->alloc_txq,
  2419. .mapping_mode = ICE_VSI_MAP_CONTIG
  2420. };
  2421. struct device *dev;
  2422. int status, i;
  2423. dev = ice_pf_to_dev(pf);
  2424. vsi->xdp_rings = devm_kcalloc(dev, vsi->num_xdp_txq,
  2425. sizeof(*vsi->xdp_rings), GFP_KERNEL);
  2426. if (!vsi->xdp_rings)
  2427. return -ENOMEM;
  2428. vsi->xdp_mapping_mode = xdp_qs_cfg.mapping_mode;
  2429. if (__ice_vsi_get_qs(&xdp_qs_cfg))
  2430. goto err_map_xdp;
  2431. if (static_key_enabled(&ice_xdp_locking_key))
  2432. netdev_warn(vsi->netdev,
  2433. "Could not allocate one XDP Tx ring per CPU, XDP_TX/XDP_REDIRECT actions will be slower\n");
  2434. if (ice_xdp_alloc_setup_rings(vsi))
  2435. goto clear_xdp_rings;
  2436. /* omit the scheduler update if in reset path; XDP queues will be
  2437. * taken into account at the end of ice_vsi_rebuild, where
  2438. * ice_cfg_vsi_lan is being called
  2439. */
  2440. if (cfg_type == ICE_XDP_CFG_PART)
  2441. return 0;
  2442. ice_map_xdp_rings(vsi);
  2443. /* tell the Tx scheduler that right now we have
  2444. * additional queues
  2445. */
  2446. for (i = 0; i < vsi->tc_cfg.numtc; i++)
  2447. max_txqs[i] = vsi->num_txq + vsi->num_xdp_txq;
  2448. status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
  2449. max_txqs);
  2450. if (status) {
  2451. dev_err(dev, "Failed VSI LAN queue config for XDP, error: %d\n",
  2452. status);
  2453. goto unmap_xdp_rings;
  2454. }
  2455. /* assign the prog only when it's not already present on VSI;
  2456. * this flow is a subject of both ethtool -L and ndo_bpf flows;
  2457. * VSI rebuild that happens under ethtool -L can expose us to
  2458. * the bpf_prog refcount issues as we would be swapping same
  2459. * bpf_prog pointers from vsi->xdp_prog and calling bpf_prog_put
  2460. * on it as it would be treated as an 'old_prog'; for ndo_bpf
  2461. * this is not harmful as dev_xdp_install bumps the refcount
  2462. * before calling the op exposed by the driver;
  2463. */
  2464. if (!ice_is_xdp_ena_vsi(vsi))
  2465. ice_vsi_assign_bpf_prog(vsi, prog);
  2466. return 0;
  2467. unmap_xdp_rings:
  2468. ice_unmap_xdp_rings(vsi);
  2469. clear_xdp_rings:
  2470. ice_for_each_xdp_txq(vsi, i)
  2471. if (vsi->xdp_rings[i]) {
  2472. kfree_rcu(vsi->xdp_rings[i], rcu);
  2473. vsi->xdp_rings[i] = NULL;
  2474. }
  2475. err_map_xdp:
  2476. mutex_lock(&pf->avail_q_mutex);
  2477. ice_for_each_xdp_txq(vsi, i) {
  2478. clear_bit(vsi->txq_map[i + vsi->alloc_txq], pf->avail_txqs);
  2479. vsi->txq_map[i + vsi->alloc_txq] = ICE_INVAL_Q_INDEX;
  2480. }
  2481. mutex_unlock(&pf->avail_q_mutex);
  2482. devm_kfree(dev, vsi->xdp_rings);
  2483. vsi->xdp_rings = NULL;
  2484. return -ENOMEM;
  2485. }
  2486. /**
  2487. * ice_destroy_xdp_rings - undo the configuration made by ice_prepare_xdp_rings
  2488. * @vsi: VSI to remove XDP rings
  2489. * @cfg_type: disable XDP permanently or allow it to be restored later
  2490. *
  2491. * Detach XDP rings from irq vectors, clean up the PF bitmap and free
  2492. * resources
  2493. */
  2494. int ice_destroy_xdp_rings(struct ice_vsi *vsi, enum ice_xdp_cfg cfg_type)
  2495. {
  2496. u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
  2497. struct ice_pf *pf = vsi->back;
  2498. int i;
  2499. /* q_vectors are freed in reset path so there's no point in detaching
  2500. * rings
  2501. */
  2502. if (cfg_type == ICE_XDP_CFG_PART)
  2503. goto free_qmap;
  2504. ice_unmap_xdp_rings(vsi);
  2505. free_qmap:
  2506. mutex_lock(&pf->avail_q_mutex);
  2507. ice_for_each_xdp_txq(vsi, i) {
  2508. clear_bit(vsi->txq_map[i + vsi->alloc_txq], pf->avail_txqs);
  2509. vsi->txq_map[i + vsi->alloc_txq] = ICE_INVAL_Q_INDEX;
  2510. }
  2511. mutex_unlock(&pf->avail_q_mutex);
  2512. ice_for_each_xdp_txq(vsi, i)
  2513. if (vsi->xdp_rings[i]) {
  2514. if (vsi->xdp_rings[i]->desc) {
  2515. synchronize_rcu();
  2516. ice_free_tx_ring(vsi->xdp_rings[i]);
  2517. }
  2518. kfree_rcu(vsi->xdp_rings[i]->ring_stats, rcu);
  2519. vsi->xdp_rings[i]->ring_stats = NULL;
  2520. kfree_rcu(vsi->xdp_rings[i], rcu);
  2521. vsi->xdp_rings[i] = NULL;
  2522. }
  2523. devm_kfree(ice_pf_to_dev(pf), vsi->xdp_rings);
  2524. vsi->xdp_rings = NULL;
  2525. if (static_key_enabled(&ice_xdp_locking_key))
  2526. static_branch_dec(&ice_xdp_locking_key);
  2527. if (cfg_type == ICE_XDP_CFG_PART)
  2528. return 0;
  2529. ice_vsi_assign_bpf_prog(vsi, NULL);
  2530. /* notify Tx scheduler that we destroyed XDP queues and bring
  2531. * back the old number of child nodes
  2532. */
  2533. for (i = 0; i < vsi->tc_cfg.numtc; i++)
  2534. max_txqs[i] = vsi->num_txq;
  2535. /* change number of XDP Tx queues to 0 */
  2536. vsi->num_xdp_txq = 0;
  2537. return ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
  2538. max_txqs);
  2539. }
  2540. /**
  2541. * ice_vsi_rx_napi_schedule - Schedule napi on RX queues from VSI
  2542. * @vsi: VSI to schedule napi on
  2543. */
  2544. static void ice_vsi_rx_napi_schedule(struct ice_vsi *vsi)
  2545. {
  2546. int i;
  2547. ice_for_each_rxq(vsi, i) {
  2548. struct ice_rx_ring *rx_ring = vsi->rx_rings[i];
  2549. if (READ_ONCE(rx_ring->xsk_pool))
  2550. napi_schedule(&rx_ring->q_vector->napi);
  2551. }
  2552. }
  2553. /**
  2554. * ice_vsi_determine_xdp_res - figure out how many Tx qs can XDP have
  2555. * @vsi: VSI to determine the count of XDP Tx qs
  2556. *
  2557. * returns 0 if Tx qs count is higher than at least half of CPU count,
  2558. * -ENOMEM otherwise
  2559. */
  2560. int ice_vsi_determine_xdp_res(struct ice_vsi *vsi)
  2561. {
  2562. u16 avail = ice_get_avail_txq_count(vsi->back);
  2563. u16 cpus = num_possible_cpus();
  2564. if (avail < cpus / 2)
  2565. return -ENOMEM;
  2566. if (vsi->type == ICE_VSI_SF)
  2567. avail = vsi->alloc_txq;
  2568. vsi->num_xdp_txq = min_t(u16, avail, cpus);
  2569. if (vsi->num_xdp_txq < cpus)
  2570. static_branch_inc(&ice_xdp_locking_key);
  2571. return 0;
  2572. }
  2573. /**
  2574. * ice_max_xdp_frame_size - returns the maximum allowed frame size for XDP
  2575. * @vsi: Pointer to VSI structure
  2576. */
  2577. static int ice_max_xdp_frame_size(struct ice_vsi *vsi)
  2578. {
  2579. if (test_bit(ICE_FLAG_LEGACY_RX, vsi->back->flags))
  2580. return ICE_RXBUF_1664;
  2581. else
  2582. return ICE_RXBUF_3072;
  2583. }
  2584. /**
  2585. * ice_xdp_setup_prog - Add or remove XDP eBPF program
  2586. * @vsi: VSI to setup XDP for
  2587. * @prog: XDP program
  2588. * @extack: netlink extended ack
  2589. */
  2590. static int
  2591. ice_xdp_setup_prog(struct ice_vsi *vsi, struct bpf_prog *prog,
  2592. struct netlink_ext_ack *extack)
  2593. {
  2594. unsigned int frame_size = vsi->netdev->mtu + ICE_ETH_PKT_HDR_PAD;
  2595. int ret = 0, xdp_ring_err = 0;
  2596. bool if_running;
  2597. if (prog && !prog->aux->xdp_has_frags) {
  2598. if (frame_size > ice_max_xdp_frame_size(vsi)) {
  2599. NL_SET_ERR_MSG_MOD(extack,
  2600. "MTU is too large for linear frames and XDP prog does not support frags");
  2601. return -EOPNOTSUPP;
  2602. }
  2603. }
  2604. /* hot swap progs and avoid toggling link */
  2605. if (ice_is_xdp_ena_vsi(vsi) == !!prog ||
  2606. test_bit(ICE_VSI_REBUILD_PENDING, vsi->state)) {
  2607. ice_vsi_assign_bpf_prog(vsi, prog);
  2608. return 0;
  2609. }
  2610. if_running = netif_running(vsi->netdev) &&
  2611. !test_and_set_bit(ICE_VSI_DOWN, vsi->state);
  2612. /* need to stop netdev while setting up the program for Rx rings */
  2613. if (if_running) {
  2614. ret = ice_down(vsi);
  2615. if (ret) {
  2616. NL_SET_ERR_MSG_MOD(extack, "Preparing device for XDP attach failed");
  2617. return ret;
  2618. }
  2619. }
  2620. if (!ice_is_xdp_ena_vsi(vsi) && prog) {
  2621. xdp_ring_err = ice_vsi_determine_xdp_res(vsi);
  2622. if (xdp_ring_err) {
  2623. NL_SET_ERR_MSG_MOD(extack, "Not enough Tx resources for XDP");
  2624. goto resume_if;
  2625. } else {
  2626. xdp_ring_err = ice_prepare_xdp_rings(vsi, prog,
  2627. ICE_XDP_CFG_FULL);
  2628. if (xdp_ring_err) {
  2629. NL_SET_ERR_MSG_MOD(extack, "Setting up XDP Tx resources failed");
  2630. goto resume_if;
  2631. }
  2632. }
  2633. xdp_features_set_redirect_target(vsi->netdev, true);
  2634. /* reallocate Rx queues that are used for zero-copy */
  2635. xdp_ring_err = ice_realloc_zc_buf(vsi, true);
  2636. if (xdp_ring_err)
  2637. NL_SET_ERR_MSG_MOD(extack, "Setting up XDP Rx resources failed");
  2638. } else if (ice_is_xdp_ena_vsi(vsi) && !prog) {
  2639. xdp_features_clear_redirect_target(vsi->netdev);
  2640. xdp_ring_err = ice_destroy_xdp_rings(vsi, ICE_XDP_CFG_FULL);
  2641. if (xdp_ring_err)
  2642. NL_SET_ERR_MSG_MOD(extack, "Freeing XDP Tx resources failed");
  2643. /* reallocate Rx queues that were used for zero-copy */
  2644. xdp_ring_err = ice_realloc_zc_buf(vsi, false);
  2645. if (xdp_ring_err)
  2646. NL_SET_ERR_MSG_MOD(extack, "Freeing XDP Rx resources failed");
  2647. }
  2648. resume_if:
  2649. if (if_running)
  2650. ret = ice_up(vsi);
  2651. if (!ret && prog)
  2652. ice_vsi_rx_napi_schedule(vsi);
  2653. return (ret || xdp_ring_err) ? -ENOMEM : 0;
  2654. }
  2655. /**
  2656. * ice_xdp_safe_mode - XDP handler for safe mode
  2657. * @dev: netdevice
  2658. * @xdp: XDP command
  2659. */
  2660. static int ice_xdp_safe_mode(struct net_device __always_unused *dev,
  2661. struct netdev_bpf *xdp)
  2662. {
  2663. NL_SET_ERR_MSG_MOD(xdp->extack,
  2664. "Please provide working DDP firmware package in order to use XDP\n"
  2665. "Refer to Documentation/networking/device_drivers/ethernet/intel/ice.rst");
  2666. return -EOPNOTSUPP;
  2667. }
  2668. /**
  2669. * ice_xdp - implements XDP handler
  2670. * @dev: netdevice
  2671. * @xdp: XDP command
  2672. */
  2673. int ice_xdp(struct net_device *dev, struct netdev_bpf *xdp)
  2674. {
  2675. struct ice_netdev_priv *np = netdev_priv(dev);
  2676. struct ice_vsi *vsi = np->vsi;
  2677. int ret;
  2678. if (vsi->type != ICE_VSI_PF && vsi->type != ICE_VSI_SF) {
  2679. NL_SET_ERR_MSG_MOD(xdp->extack, "XDP can be loaded only on PF or SF VSI");
  2680. return -EINVAL;
  2681. }
  2682. mutex_lock(&vsi->xdp_state_lock);
  2683. switch (xdp->command) {
  2684. case XDP_SETUP_PROG:
  2685. ret = ice_xdp_setup_prog(vsi, xdp->prog, xdp->extack);
  2686. break;
  2687. case XDP_SETUP_XSK_POOL:
  2688. ret = ice_xsk_pool_setup(vsi, xdp->xsk.pool, xdp->xsk.queue_id);
  2689. break;
  2690. default:
  2691. ret = -EINVAL;
  2692. }
  2693. mutex_unlock(&vsi->xdp_state_lock);
  2694. return ret;
  2695. }
  2696. /**
  2697. * ice_ena_misc_vector - enable the non-queue interrupts
  2698. * @pf: board private structure
  2699. */
  2700. static void ice_ena_misc_vector(struct ice_pf *pf)
  2701. {
  2702. struct ice_hw *hw = &pf->hw;
  2703. u32 pf_intr_start_offset;
  2704. u32 val;
  2705. /* Disable anti-spoof detection interrupt to prevent spurious event
  2706. * interrupts during a function reset. Anti-spoof functionally is
  2707. * still supported.
  2708. */
  2709. val = rd32(hw, GL_MDCK_TX_TDPU);
  2710. val |= GL_MDCK_TX_TDPU_RCU_ANTISPOOF_ITR_DIS_M;
  2711. wr32(hw, GL_MDCK_TX_TDPU, val);
  2712. /* clear things first */
  2713. wr32(hw, PFINT_OICR_ENA, 0); /* disable all */
  2714. rd32(hw, PFINT_OICR); /* read to clear */
  2715. val = (PFINT_OICR_ECC_ERR_M |
  2716. PFINT_OICR_MAL_DETECT_M |
  2717. PFINT_OICR_GRST_M |
  2718. PFINT_OICR_PCI_EXCEPTION_M |
  2719. PFINT_OICR_VFLR_M |
  2720. PFINT_OICR_HMC_ERR_M |
  2721. PFINT_OICR_PE_PUSH_M |
  2722. PFINT_OICR_PE_CRITERR_M);
  2723. wr32(hw, PFINT_OICR_ENA, val);
  2724. /* SW_ITR_IDX = 0, but don't change INTENA */
  2725. wr32(hw, GLINT_DYN_CTL(pf->oicr_irq.index),
  2726. GLINT_DYN_CTL_SW_ITR_INDX_M | GLINT_DYN_CTL_INTENA_MSK_M);
  2727. if (!pf->hw.dev_caps.ts_dev_info.ts_ll_int_read)
  2728. return;
  2729. pf_intr_start_offset = rd32(hw, PFINT_ALLOC) & PFINT_ALLOC_FIRST;
  2730. wr32(hw, GLINT_DYN_CTL(pf->ll_ts_irq.index + pf_intr_start_offset),
  2731. GLINT_DYN_CTL_SW_ITR_INDX_M | GLINT_DYN_CTL_INTENA_MSK_M);
  2732. }
  2733. /**
  2734. * ice_ll_ts_intr - ll_ts interrupt handler
  2735. * @irq: interrupt number
  2736. * @data: pointer to a q_vector
  2737. */
  2738. static irqreturn_t ice_ll_ts_intr(int __always_unused irq, void *data)
  2739. {
  2740. struct ice_pf *pf = data;
  2741. u32 pf_intr_start_offset;
  2742. struct ice_ptp_tx *tx;
  2743. unsigned long flags;
  2744. struct ice_hw *hw;
  2745. u32 val;
  2746. u8 idx;
  2747. hw = &pf->hw;
  2748. tx = &pf->ptp.port.tx;
  2749. spin_lock_irqsave(&tx->lock, flags);
  2750. if (tx->init) {
  2751. ice_ptp_complete_tx_single_tstamp(tx);
  2752. idx = find_next_bit_wrap(tx->in_use, tx->len,
  2753. tx->last_ll_ts_idx_read + 1);
  2754. if (idx != tx->len)
  2755. ice_ptp_req_tx_single_tstamp(tx, idx);
  2756. }
  2757. spin_unlock_irqrestore(&tx->lock, flags);
  2758. val = GLINT_DYN_CTL_INTENA_M | GLINT_DYN_CTL_CLEARPBA_M |
  2759. (ICE_ITR_NONE << GLINT_DYN_CTL_ITR_INDX_S);
  2760. pf_intr_start_offset = rd32(hw, PFINT_ALLOC) & PFINT_ALLOC_FIRST;
  2761. wr32(hw, GLINT_DYN_CTL(pf->ll_ts_irq.index + pf_intr_start_offset),
  2762. val);
  2763. return IRQ_HANDLED;
  2764. }
  2765. /**
  2766. * ice_misc_intr - misc interrupt handler
  2767. * @irq: interrupt number
  2768. * @data: pointer to a q_vector
  2769. */
  2770. static irqreturn_t ice_misc_intr(int __always_unused irq, void *data)
  2771. {
  2772. struct ice_pf *pf = (struct ice_pf *)data;
  2773. irqreturn_t ret = IRQ_HANDLED;
  2774. struct ice_hw *hw = &pf->hw;
  2775. struct device *dev;
  2776. u32 oicr, ena_mask;
  2777. dev = ice_pf_to_dev(pf);
  2778. set_bit(ICE_ADMINQ_EVENT_PENDING, pf->state);
  2779. set_bit(ICE_MAILBOXQ_EVENT_PENDING, pf->state);
  2780. set_bit(ICE_SIDEBANDQ_EVENT_PENDING, pf->state);
  2781. oicr = rd32(hw, PFINT_OICR);
  2782. ena_mask = rd32(hw, PFINT_OICR_ENA);
  2783. if (oicr & PFINT_OICR_SWINT_M) {
  2784. ena_mask &= ~PFINT_OICR_SWINT_M;
  2785. pf->sw_int_count++;
  2786. }
  2787. if (oicr & PFINT_OICR_MAL_DETECT_M) {
  2788. ena_mask &= ~PFINT_OICR_MAL_DETECT_M;
  2789. set_bit(ICE_MDD_EVENT_PENDING, pf->state);
  2790. }
  2791. if (oicr & PFINT_OICR_VFLR_M) {
  2792. /* disable any further VFLR event notifications */
  2793. if (test_bit(ICE_VF_RESETS_DISABLED, pf->state)) {
  2794. u32 reg = rd32(hw, PFINT_OICR_ENA);
  2795. reg &= ~PFINT_OICR_VFLR_M;
  2796. wr32(hw, PFINT_OICR_ENA, reg);
  2797. } else {
  2798. ena_mask &= ~PFINT_OICR_VFLR_M;
  2799. set_bit(ICE_VFLR_EVENT_PENDING, pf->state);
  2800. }
  2801. }
  2802. if (oicr & PFINT_OICR_GRST_M) {
  2803. u32 reset;
  2804. /* we have a reset warning */
  2805. ena_mask &= ~PFINT_OICR_GRST_M;
  2806. reset = FIELD_GET(GLGEN_RSTAT_RESET_TYPE_M,
  2807. rd32(hw, GLGEN_RSTAT));
  2808. if (reset == ICE_RESET_CORER)
  2809. pf->corer_count++;
  2810. else if (reset == ICE_RESET_GLOBR)
  2811. pf->globr_count++;
  2812. else if (reset == ICE_RESET_EMPR)
  2813. pf->empr_count++;
  2814. else
  2815. dev_dbg(dev, "Invalid reset type %d\n", reset);
  2816. /* If a reset cycle isn't already in progress, we set a bit in
  2817. * pf->state so that the service task can start a reset/rebuild.
  2818. */
  2819. if (!test_and_set_bit(ICE_RESET_OICR_RECV, pf->state)) {
  2820. if (reset == ICE_RESET_CORER)
  2821. set_bit(ICE_CORER_RECV, pf->state);
  2822. else if (reset == ICE_RESET_GLOBR)
  2823. set_bit(ICE_GLOBR_RECV, pf->state);
  2824. else
  2825. set_bit(ICE_EMPR_RECV, pf->state);
  2826. /* There are couple of different bits at play here.
  2827. * hw->reset_ongoing indicates whether the hardware is
  2828. * in reset. This is set to true when a reset interrupt
  2829. * is received and set back to false after the driver
  2830. * has determined that the hardware is out of reset.
  2831. *
  2832. * ICE_RESET_OICR_RECV in pf->state indicates
  2833. * that a post reset rebuild is required before the
  2834. * driver is operational again. This is set above.
  2835. *
  2836. * As this is the start of the reset/rebuild cycle, set
  2837. * both to indicate that.
  2838. */
  2839. hw->reset_ongoing = true;
  2840. }
  2841. }
  2842. if (oicr & PFINT_OICR_TSYN_TX_M) {
  2843. ena_mask &= ~PFINT_OICR_TSYN_TX_M;
  2844. if (ice_pf_state_is_nominal(pf) &&
  2845. pf->hw.dev_caps.ts_dev_info.ts_ll_int_read) {
  2846. struct ice_ptp_tx *tx = &pf->ptp.port.tx;
  2847. unsigned long flags;
  2848. u8 idx;
  2849. spin_lock_irqsave(&tx->lock, flags);
  2850. idx = find_next_bit_wrap(tx->in_use, tx->len,
  2851. tx->last_ll_ts_idx_read + 1);
  2852. if (idx != tx->len)
  2853. ice_ptp_req_tx_single_tstamp(tx, idx);
  2854. spin_unlock_irqrestore(&tx->lock, flags);
  2855. } else if (ice_ptp_pf_handles_tx_interrupt(pf)) {
  2856. set_bit(ICE_MISC_THREAD_TX_TSTAMP, pf->misc_thread);
  2857. ret = IRQ_WAKE_THREAD;
  2858. }
  2859. }
  2860. if (oicr & PFINT_OICR_TSYN_EVNT_M) {
  2861. u8 tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned;
  2862. u32 gltsyn_stat = rd32(hw, GLTSYN_STAT(tmr_idx));
  2863. ena_mask &= ~PFINT_OICR_TSYN_EVNT_M;
  2864. if (ice_pf_src_tmr_owned(pf)) {
  2865. /* Save EVENTs from GLTSYN register */
  2866. pf->ptp.ext_ts_irq |= gltsyn_stat &
  2867. (GLTSYN_STAT_EVENT0_M |
  2868. GLTSYN_STAT_EVENT1_M |
  2869. GLTSYN_STAT_EVENT2_M);
  2870. ice_ptp_extts_event(pf);
  2871. }
  2872. }
  2873. #define ICE_AUX_CRIT_ERR (PFINT_OICR_PE_CRITERR_M | PFINT_OICR_HMC_ERR_M | PFINT_OICR_PE_PUSH_M)
  2874. if (oicr & ICE_AUX_CRIT_ERR) {
  2875. pf->oicr_err_reg |= oicr;
  2876. set_bit(ICE_AUX_ERR_PENDING, pf->state);
  2877. ena_mask &= ~ICE_AUX_CRIT_ERR;
  2878. }
  2879. /* Report any remaining unexpected interrupts */
  2880. oicr &= ena_mask;
  2881. if (oicr) {
  2882. dev_dbg(dev, "unhandled interrupt oicr=0x%08x\n", oicr);
  2883. /* If a critical error is pending there is no choice but to
  2884. * reset the device.
  2885. */
  2886. if (oicr & (PFINT_OICR_PCI_EXCEPTION_M |
  2887. PFINT_OICR_ECC_ERR_M)) {
  2888. set_bit(ICE_PFR_REQ, pf->state);
  2889. }
  2890. }
  2891. ice_service_task_schedule(pf);
  2892. if (ret == IRQ_HANDLED)
  2893. ice_irq_dynamic_ena(hw, NULL, NULL);
  2894. return ret;
  2895. }
  2896. /**
  2897. * ice_misc_intr_thread_fn - misc interrupt thread function
  2898. * @irq: interrupt number
  2899. * @data: pointer to a q_vector
  2900. */
  2901. static irqreturn_t ice_misc_intr_thread_fn(int __always_unused irq, void *data)
  2902. {
  2903. struct ice_pf *pf = data;
  2904. struct ice_hw *hw;
  2905. hw = &pf->hw;
  2906. if (ice_is_reset_in_progress(pf->state))
  2907. goto skip_irq;
  2908. if (test_and_clear_bit(ICE_MISC_THREAD_TX_TSTAMP, pf->misc_thread)) {
  2909. /* Process outstanding Tx timestamps. If there is more work,
  2910. * re-arm the interrupt to trigger again.
  2911. */
  2912. if (ice_ptp_process_ts(pf) == ICE_TX_TSTAMP_WORK_PENDING) {
  2913. wr32(hw, PFINT_OICR, PFINT_OICR_TSYN_TX_M);
  2914. ice_flush(hw);
  2915. }
  2916. }
  2917. skip_irq:
  2918. ice_irq_dynamic_ena(hw, NULL, NULL);
  2919. return IRQ_HANDLED;
  2920. }
  2921. /**
  2922. * ice_dis_ctrlq_interrupts - disable control queue interrupts
  2923. * @hw: pointer to HW structure
  2924. */
  2925. static void ice_dis_ctrlq_interrupts(struct ice_hw *hw)
  2926. {
  2927. /* disable Admin queue Interrupt causes */
  2928. wr32(hw, PFINT_FW_CTL,
  2929. rd32(hw, PFINT_FW_CTL) & ~PFINT_FW_CTL_CAUSE_ENA_M);
  2930. /* disable Mailbox queue Interrupt causes */
  2931. wr32(hw, PFINT_MBX_CTL,
  2932. rd32(hw, PFINT_MBX_CTL) & ~PFINT_MBX_CTL_CAUSE_ENA_M);
  2933. wr32(hw, PFINT_SB_CTL,
  2934. rd32(hw, PFINT_SB_CTL) & ~PFINT_SB_CTL_CAUSE_ENA_M);
  2935. /* disable Control queue Interrupt causes */
  2936. wr32(hw, PFINT_OICR_CTL,
  2937. rd32(hw, PFINT_OICR_CTL) & ~PFINT_OICR_CTL_CAUSE_ENA_M);
  2938. ice_flush(hw);
  2939. }
  2940. /**
  2941. * ice_free_irq_msix_ll_ts- Unroll ll_ts vector setup
  2942. * @pf: board private structure
  2943. */
  2944. static void ice_free_irq_msix_ll_ts(struct ice_pf *pf)
  2945. {
  2946. int irq_num = pf->ll_ts_irq.virq;
  2947. synchronize_irq(irq_num);
  2948. devm_free_irq(ice_pf_to_dev(pf), irq_num, pf);
  2949. ice_free_irq(pf, pf->ll_ts_irq);
  2950. }
  2951. /**
  2952. * ice_free_irq_msix_misc - Unroll misc vector setup
  2953. * @pf: board private structure
  2954. */
  2955. static void ice_free_irq_msix_misc(struct ice_pf *pf)
  2956. {
  2957. int misc_irq_num = pf->oicr_irq.virq;
  2958. struct ice_hw *hw = &pf->hw;
  2959. ice_dis_ctrlq_interrupts(hw);
  2960. /* disable OICR interrupt */
  2961. wr32(hw, PFINT_OICR_ENA, 0);
  2962. ice_flush(hw);
  2963. synchronize_irq(misc_irq_num);
  2964. devm_free_irq(ice_pf_to_dev(pf), misc_irq_num, pf);
  2965. ice_free_irq(pf, pf->oicr_irq);
  2966. if (pf->hw.dev_caps.ts_dev_info.ts_ll_int_read)
  2967. ice_free_irq_msix_ll_ts(pf);
  2968. }
  2969. /**
  2970. * ice_ena_ctrlq_interrupts - enable control queue interrupts
  2971. * @hw: pointer to HW structure
  2972. * @reg_idx: HW vector index to associate the control queue interrupts with
  2973. */
  2974. static void ice_ena_ctrlq_interrupts(struct ice_hw *hw, u16 reg_idx)
  2975. {
  2976. u32 val;
  2977. val = ((reg_idx & PFINT_OICR_CTL_MSIX_INDX_M) |
  2978. PFINT_OICR_CTL_CAUSE_ENA_M);
  2979. wr32(hw, PFINT_OICR_CTL, val);
  2980. /* enable Admin queue Interrupt causes */
  2981. val = ((reg_idx & PFINT_FW_CTL_MSIX_INDX_M) |
  2982. PFINT_FW_CTL_CAUSE_ENA_M);
  2983. wr32(hw, PFINT_FW_CTL, val);
  2984. /* enable Mailbox queue Interrupt causes */
  2985. val = ((reg_idx & PFINT_MBX_CTL_MSIX_INDX_M) |
  2986. PFINT_MBX_CTL_CAUSE_ENA_M);
  2987. wr32(hw, PFINT_MBX_CTL, val);
  2988. if (!hw->dev_caps.ts_dev_info.ts_ll_int_read) {
  2989. /* enable Sideband queue Interrupt causes */
  2990. val = ((reg_idx & PFINT_SB_CTL_MSIX_INDX_M) |
  2991. PFINT_SB_CTL_CAUSE_ENA_M);
  2992. wr32(hw, PFINT_SB_CTL, val);
  2993. }
  2994. ice_flush(hw);
  2995. }
  2996. /**
  2997. * ice_req_irq_msix_misc - Setup the misc vector to handle non queue events
  2998. * @pf: board private structure
  2999. *
  3000. * This sets up the handler for MSIX 0, which is used to manage the
  3001. * non-queue interrupts, e.g. AdminQ and errors. This is not used
  3002. * when in MSI or Legacy interrupt mode.
  3003. */
  3004. static int ice_req_irq_msix_misc(struct ice_pf *pf)
  3005. {
  3006. struct device *dev = ice_pf_to_dev(pf);
  3007. struct ice_hw *hw = &pf->hw;
  3008. u32 pf_intr_start_offset;
  3009. struct msi_map irq;
  3010. int err = 0;
  3011. if (!pf->int_name[0])
  3012. snprintf(pf->int_name, sizeof(pf->int_name) - 1, "%s-%s:misc",
  3013. dev_driver_string(dev), dev_name(dev));
  3014. if (!pf->int_name_ll_ts[0])
  3015. snprintf(pf->int_name_ll_ts, sizeof(pf->int_name_ll_ts) - 1,
  3016. "%s-%s:ll_ts", dev_driver_string(dev), dev_name(dev));
  3017. /* Do not request IRQ but do enable OICR interrupt since settings are
  3018. * lost during reset. Note that this function is called only during
  3019. * rebuild path and not while reset is in progress.
  3020. */
  3021. if (ice_is_reset_in_progress(pf->state))
  3022. goto skip_req_irq;
  3023. /* reserve one vector in irq_tracker for misc interrupts */
  3024. irq = ice_alloc_irq(pf, false);
  3025. if (irq.index < 0)
  3026. return irq.index;
  3027. pf->oicr_irq = irq;
  3028. err = devm_request_threaded_irq(dev, pf->oicr_irq.virq, ice_misc_intr,
  3029. ice_misc_intr_thread_fn, 0,
  3030. pf->int_name, pf);
  3031. if (err) {
  3032. dev_err(dev, "devm_request_threaded_irq for %s failed: %d\n",
  3033. pf->int_name, err);
  3034. ice_free_irq(pf, pf->oicr_irq);
  3035. return err;
  3036. }
  3037. /* reserve one vector in irq_tracker for ll_ts interrupt */
  3038. if (!pf->hw.dev_caps.ts_dev_info.ts_ll_int_read)
  3039. goto skip_req_irq;
  3040. irq = ice_alloc_irq(pf, false);
  3041. if (irq.index < 0)
  3042. return irq.index;
  3043. pf->ll_ts_irq = irq;
  3044. err = devm_request_irq(dev, pf->ll_ts_irq.virq, ice_ll_ts_intr, 0,
  3045. pf->int_name_ll_ts, pf);
  3046. if (err) {
  3047. dev_err(dev, "devm_request_irq for %s failed: %d\n",
  3048. pf->int_name_ll_ts, err);
  3049. ice_free_irq(pf, pf->ll_ts_irq);
  3050. return err;
  3051. }
  3052. skip_req_irq:
  3053. ice_ena_misc_vector(pf);
  3054. ice_ena_ctrlq_interrupts(hw, pf->oicr_irq.index);
  3055. /* This enables LL TS interrupt */
  3056. pf_intr_start_offset = rd32(hw, PFINT_ALLOC) & PFINT_ALLOC_FIRST;
  3057. if (pf->hw.dev_caps.ts_dev_info.ts_ll_int_read)
  3058. wr32(hw, PFINT_SB_CTL,
  3059. ((pf->ll_ts_irq.index + pf_intr_start_offset) &
  3060. PFINT_SB_CTL_MSIX_INDX_M) | PFINT_SB_CTL_CAUSE_ENA_M);
  3061. wr32(hw, GLINT_ITR(ICE_RX_ITR, pf->oicr_irq.index),
  3062. ITR_REG_ALIGN(ICE_ITR_8K) >> ICE_ITR_GRAN_S);
  3063. ice_flush(hw);
  3064. ice_irq_dynamic_ena(hw, NULL, NULL);
  3065. return 0;
  3066. }
  3067. /**
  3068. * ice_set_ops - set netdev and ethtools ops for the given netdev
  3069. * @vsi: the VSI associated with the new netdev
  3070. */
  3071. static void ice_set_ops(struct ice_vsi *vsi)
  3072. {
  3073. struct net_device *netdev = vsi->netdev;
  3074. struct ice_pf *pf = ice_netdev_to_pf(netdev);
  3075. if (ice_is_safe_mode(pf)) {
  3076. netdev->netdev_ops = &ice_netdev_safe_mode_ops;
  3077. ice_set_ethtool_safe_mode_ops(netdev);
  3078. return;
  3079. }
  3080. netdev->netdev_ops = &ice_netdev_ops;
  3081. netdev->udp_tunnel_nic_info = &pf->hw.udp_tunnel_nic;
  3082. netdev->xdp_metadata_ops = &ice_xdp_md_ops;
  3083. ice_set_ethtool_ops(netdev);
  3084. if (vsi->type != ICE_VSI_PF)
  3085. return;
  3086. netdev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
  3087. NETDEV_XDP_ACT_XSK_ZEROCOPY |
  3088. NETDEV_XDP_ACT_RX_SG;
  3089. netdev->xdp_zc_max_segs = ICE_MAX_BUF_TXD;
  3090. }
  3091. /**
  3092. * ice_set_netdev_features - set features for the given netdev
  3093. * @netdev: netdev instance
  3094. */
  3095. void ice_set_netdev_features(struct net_device *netdev)
  3096. {
  3097. struct ice_pf *pf = ice_netdev_to_pf(netdev);
  3098. bool is_dvm_ena = ice_is_dvm_ena(&pf->hw);
  3099. netdev_features_t csumo_features;
  3100. netdev_features_t vlano_features;
  3101. netdev_features_t dflt_features;
  3102. netdev_features_t tso_features;
  3103. if (ice_is_safe_mode(pf)) {
  3104. /* safe mode */
  3105. netdev->features = NETIF_F_SG | NETIF_F_HIGHDMA;
  3106. netdev->hw_features = netdev->features;
  3107. return;
  3108. }
  3109. dflt_features = NETIF_F_SG |
  3110. NETIF_F_HIGHDMA |
  3111. NETIF_F_NTUPLE |
  3112. NETIF_F_RXHASH;
  3113. csumo_features = NETIF_F_RXCSUM |
  3114. NETIF_F_IP_CSUM |
  3115. NETIF_F_SCTP_CRC |
  3116. NETIF_F_IPV6_CSUM;
  3117. vlano_features = NETIF_F_HW_VLAN_CTAG_FILTER |
  3118. NETIF_F_HW_VLAN_CTAG_TX |
  3119. NETIF_F_HW_VLAN_CTAG_RX;
  3120. /* Enable CTAG/STAG filtering by default in Double VLAN Mode (DVM) */
  3121. if (is_dvm_ena)
  3122. vlano_features |= NETIF_F_HW_VLAN_STAG_FILTER;
  3123. tso_features = NETIF_F_TSO |
  3124. NETIF_F_TSO_ECN |
  3125. NETIF_F_TSO6 |
  3126. NETIF_F_GSO_GRE |
  3127. NETIF_F_GSO_UDP_TUNNEL |
  3128. NETIF_F_GSO_GRE_CSUM |
  3129. NETIF_F_GSO_UDP_TUNNEL_CSUM |
  3130. NETIF_F_GSO_PARTIAL |
  3131. NETIF_F_GSO_IPXIP4 |
  3132. NETIF_F_GSO_IPXIP6 |
  3133. NETIF_F_GSO_UDP_L4;
  3134. netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM |
  3135. NETIF_F_GSO_GRE_CSUM;
  3136. /* set features that user can change */
  3137. netdev->hw_features = dflt_features | csumo_features |
  3138. vlano_features | tso_features;
  3139. /* add support for HW_CSUM on packets with MPLS header */
  3140. netdev->mpls_features = NETIF_F_HW_CSUM |
  3141. NETIF_F_TSO |
  3142. NETIF_F_TSO6;
  3143. /* enable features */
  3144. netdev->features |= netdev->hw_features;
  3145. netdev->hw_features |= NETIF_F_HW_TC;
  3146. netdev->hw_features |= NETIF_F_LOOPBACK;
  3147. /* encap and VLAN devices inherit default, csumo and tso features */
  3148. netdev->hw_enc_features |= dflt_features | csumo_features |
  3149. tso_features;
  3150. netdev->vlan_features |= dflt_features | csumo_features |
  3151. tso_features;
  3152. /* advertise support but don't enable by default since only one type of
  3153. * VLAN offload can be enabled at a time (i.e. CTAG or STAG). When one
  3154. * type turns on the other has to be turned off. This is enforced by the
  3155. * ice_fix_features() ndo callback.
  3156. */
  3157. if (is_dvm_ena)
  3158. netdev->hw_features |= NETIF_F_HW_VLAN_STAG_RX |
  3159. NETIF_F_HW_VLAN_STAG_TX;
  3160. /* Leave CRC / FCS stripping enabled by default, but allow the value to
  3161. * be changed at runtime
  3162. */
  3163. netdev->hw_features |= NETIF_F_RXFCS;
  3164. netif_set_tso_max_size(netdev, ICE_MAX_TSO_SIZE);
  3165. }
  3166. /**
  3167. * ice_fill_rss_lut - Fill the RSS lookup table with default values
  3168. * @lut: Lookup table
  3169. * @rss_table_size: Lookup table size
  3170. * @rss_size: Range of queue number for hashing
  3171. */
  3172. void ice_fill_rss_lut(u8 *lut, u16 rss_table_size, u16 rss_size)
  3173. {
  3174. u16 i;
  3175. for (i = 0; i < rss_table_size; i++)
  3176. lut[i] = i % rss_size;
  3177. }
  3178. /**
  3179. * ice_pf_vsi_setup - Set up a PF VSI
  3180. * @pf: board private structure
  3181. * @pi: pointer to the port_info instance
  3182. *
  3183. * Returns pointer to the successfully allocated VSI software struct
  3184. * on success, otherwise returns NULL on failure.
  3185. */
  3186. static struct ice_vsi *
  3187. ice_pf_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
  3188. {
  3189. struct ice_vsi_cfg_params params = {};
  3190. params.type = ICE_VSI_PF;
  3191. params.port_info = pi;
  3192. params.flags = ICE_VSI_FLAG_INIT;
  3193. return ice_vsi_setup(pf, &params);
  3194. }
  3195. static struct ice_vsi *
  3196. ice_chnl_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi,
  3197. struct ice_channel *ch)
  3198. {
  3199. struct ice_vsi_cfg_params params = {};
  3200. params.type = ICE_VSI_CHNL;
  3201. params.port_info = pi;
  3202. params.ch = ch;
  3203. params.flags = ICE_VSI_FLAG_INIT;
  3204. return ice_vsi_setup(pf, &params);
  3205. }
  3206. /**
  3207. * ice_ctrl_vsi_setup - Set up a control VSI
  3208. * @pf: board private structure
  3209. * @pi: pointer to the port_info instance
  3210. *
  3211. * Returns pointer to the successfully allocated VSI software struct
  3212. * on success, otherwise returns NULL on failure.
  3213. */
  3214. static struct ice_vsi *
  3215. ice_ctrl_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
  3216. {
  3217. struct ice_vsi_cfg_params params = {};
  3218. params.type = ICE_VSI_CTRL;
  3219. params.port_info = pi;
  3220. params.flags = ICE_VSI_FLAG_INIT;
  3221. return ice_vsi_setup(pf, &params);
  3222. }
  3223. /**
  3224. * ice_lb_vsi_setup - Set up a loopback VSI
  3225. * @pf: board private structure
  3226. * @pi: pointer to the port_info instance
  3227. *
  3228. * Returns pointer to the successfully allocated VSI software struct
  3229. * on success, otherwise returns NULL on failure.
  3230. */
  3231. struct ice_vsi *
  3232. ice_lb_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
  3233. {
  3234. struct ice_vsi_cfg_params params = {};
  3235. params.type = ICE_VSI_LB;
  3236. params.port_info = pi;
  3237. params.flags = ICE_VSI_FLAG_INIT;
  3238. return ice_vsi_setup(pf, &params);
  3239. }
  3240. /**
  3241. * ice_vlan_rx_add_vid - Add a VLAN ID filter to HW offload
  3242. * @netdev: network interface to be adjusted
  3243. * @proto: VLAN TPID
  3244. * @vid: VLAN ID to be added
  3245. *
  3246. * net_device_ops implementation for adding VLAN IDs
  3247. */
  3248. int ice_vlan_rx_add_vid(struct net_device *netdev, __be16 proto, u16 vid)
  3249. {
  3250. struct ice_netdev_priv *np = netdev_priv(netdev);
  3251. struct ice_vsi_vlan_ops *vlan_ops;
  3252. struct ice_vsi *vsi = np->vsi;
  3253. struct ice_vlan vlan;
  3254. int ret;
  3255. /* VLAN 0 is added by default during load/reset */
  3256. if (!vid)
  3257. return 0;
  3258. while (test_and_set_bit(ICE_CFG_BUSY, vsi->state))
  3259. usleep_range(1000, 2000);
  3260. /* Add multicast promisc rule for the VLAN ID to be added if
  3261. * all-multicast is currently enabled.
  3262. */
  3263. if (vsi->current_netdev_flags & IFF_ALLMULTI) {
  3264. ret = ice_fltr_set_vsi_promisc(&vsi->back->hw, vsi->idx,
  3265. ICE_MCAST_VLAN_PROMISC_BITS,
  3266. vid);
  3267. if (ret)
  3268. goto finish;
  3269. }
  3270. vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
  3271. /* Add a switch rule for this VLAN ID so its corresponding VLAN tagged
  3272. * packets aren't pruned by the device's internal switch on Rx
  3273. */
  3274. vlan = ICE_VLAN(be16_to_cpu(proto), vid, 0);
  3275. ret = vlan_ops->add_vlan(vsi, &vlan);
  3276. if (ret)
  3277. goto finish;
  3278. /* If all-multicast is currently enabled and this VLAN ID is only one
  3279. * besides VLAN-0 we have to update look-up type of multicast promisc
  3280. * rule for VLAN-0 from ICE_SW_LKUP_PROMISC to ICE_SW_LKUP_PROMISC_VLAN.
  3281. */
  3282. if ((vsi->current_netdev_flags & IFF_ALLMULTI) &&
  3283. ice_vsi_num_non_zero_vlans(vsi) == 1) {
  3284. ice_fltr_clear_vsi_promisc(&vsi->back->hw, vsi->idx,
  3285. ICE_MCAST_PROMISC_BITS, 0);
  3286. ice_fltr_set_vsi_promisc(&vsi->back->hw, vsi->idx,
  3287. ICE_MCAST_VLAN_PROMISC_BITS, 0);
  3288. }
  3289. finish:
  3290. clear_bit(ICE_CFG_BUSY, vsi->state);
  3291. return ret;
  3292. }
  3293. /**
  3294. * ice_vlan_rx_kill_vid - Remove a VLAN ID filter from HW offload
  3295. * @netdev: network interface to be adjusted
  3296. * @proto: VLAN TPID
  3297. * @vid: VLAN ID to be removed
  3298. *
  3299. * net_device_ops implementation for removing VLAN IDs
  3300. */
  3301. int ice_vlan_rx_kill_vid(struct net_device *netdev, __be16 proto, u16 vid)
  3302. {
  3303. struct ice_netdev_priv *np = netdev_priv(netdev);
  3304. struct ice_vsi_vlan_ops *vlan_ops;
  3305. struct ice_vsi *vsi = np->vsi;
  3306. struct ice_vlan vlan;
  3307. int ret;
  3308. /* don't allow removal of VLAN 0 */
  3309. if (!vid)
  3310. return 0;
  3311. while (test_and_set_bit(ICE_CFG_BUSY, vsi->state))
  3312. usleep_range(1000, 2000);
  3313. ret = ice_clear_vsi_promisc(&vsi->back->hw, vsi->idx,
  3314. ICE_MCAST_VLAN_PROMISC_BITS, vid);
  3315. if (ret) {
  3316. netdev_err(netdev, "Error clearing multicast promiscuous mode on VSI %i\n",
  3317. vsi->vsi_num);
  3318. vsi->current_netdev_flags |= IFF_ALLMULTI;
  3319. }
  3320. vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
  3321. /* Make sure VLAN delete is successful before updating VLAN
  3322. * information
  3323. */
  3324. vlan = ICE_VLAN(be16_to_cpu(proto), vid, 0);
  3325. ret = vlan_ops->del_vlan(vsi, &vlan);
  3326. if (ret)
  3327. goto finish;
  3328. /* Remove multicast promisc rule for the removed VLAN ID if
  3329. * all-multicast is enabled.
  3330. */
  3331. if (vsi->current_netdev_flags & IFF_ALLMULTI)
  3332. ice_fltr_clear_vsi_promisc(&vsi->back->hw, vsi->idx,
  3333. ICE_MCAST_VLAN_PROMISC_BITS, vid);
  3334. if (!ice_vsi_has_non_zero_vlans(vsi)) {
  3335. /* Update look-up type of multicast promisc rule for VLAN 0
  3336. * from ICE_SW_LKUP_PROMISC_VLAN to ICE_SW_LKUP_PROMISC when
  3337. * all-multicast is enabled and VLAN 0 is the only VLAN rule.
  3338. */
  3339. if (vsi->current_netdev_flags & IFF_ALLMULTI) {
  3340. ice_fltr_clear_vsi_promisc(&vsi->back->hw, vsi->idx,
  3341. ICE_MCAST_VLAN_PROMISC_BITS,
  3342. 0);
  3343. ice_fltr_set_vsi_promisc(&vsi->back->hw, vsi->idx,
  3344. ICE_MCAST_PROMISC_BITS, 0);
  3345. }
  3346. }
  3347. finish:
  3348. clear_bit(ICE_CFG_BUSY, vsi->state);
  3349. return ret;
  3350. }
  3351. /**
  3352. * ice_rep_indr_tc_block_unbind
  3353. * @cb_priv: indirection block private data
  3354. */
  3355. static void ice_rep_indr_tc_block_unbind(void *cb_priv)
  3356. {
  3357. struct ice_indr_block_priv *indr_priv = cb_priv;
  3358. list_del(&indr_priv->list);
  3359. kfree(indr_priv);
  3360. }
  3361. /**
  3362. * ice_tc_indir_block_unregister - Unregister TC indirect block notifications
  3363. * @vsi: VSI struct which has the netdev
  3364. */
  3365. static void ice_tc_indir_block_unregister(struct ice_vsi *vsi)
  3366. {
  3367. struct ice_netdev_priv *np = netdev_priv(vsi->netdev);
  3368. flow_indr_dev_unregister(ice_indr_setup_tc_cb, np,
  3369. ice_rep_indr_tc_block_unbind);
  3370. }
  3371. /**
  3372. * ice_tc_indir_block_register - Register TC indirect block notifications
  3373. * @vsi: VSI struct which has the netdev
  3374. *
  3375. * Returns 0 on success, negative value on failure
  3376. */
  3377. static int ice_tc_indir_block_register(struct ice_vsi *vsi)
  3378. {
  3379. struct ice_netdev_priv *np;
  3380. if (!vsi || !vsi->netdev)
  3381. return -EINVAL;
  3382. np = netdev_priv(vsi->netdev);
  3383. INIT_LIST_HEAD(&np->tc_indr_block_priv_list);
  3384. return flow_indr_dev_register(ice_indr_setup_tc_cb, np);
  3385. }
  3386. /**
  3387. * ice_get_avail_q_count - Get count of queues in use
  3388. * @pf_qmap: bitmap to get queue use count from
  3389. * @lock: pointer to a mutex that protects access to pf_qmap
  3390. * @size: size of the bitmap
  3391. */
  3392. static u16
  3393. ice_get_avail_q_count(unsigned long *pf_qmap, struct mutex *lock, u16 size)
  3394. {
  3395. unsigned long bit;
  3396. u16 count = 0;
  3397. mutex_lock(lock);
  3398. for_each_clear_bit(bit, pf_qmap, size)
  3399. count++;
  3400. mutex_unlock(lock);
  3401. return count;
  3402. }
  3403. /**
  3404. * ice_get_avail_txq_count - Get count of Tx queues in use
  3405. * @pf: pointer to an ice_pf instance
  3406. */
  3407. u16 ice_get_avail_txq_count(struct ice_pf *pf)
  3408. {
  3409. return ice_get_avail_q_count(pf->avail_txqs, &pf->avail_q_mutex,
  3410. pf->max_pf_txqs);
  3411. }
  3412. /**
  3413. * ice_get_avail_rxq_count - Get count of Rx queues in use
  3414. * @pf: pointer to an ice_pf instance
  3415. */
  3416. u16 ice_get_avail_rxq_count(struct ice_pf *pf)
  3417. {
  3418. return ice_get_avail_q_count(pf->avail_rxqs, &pf->avail_q_mutex,
  3419. pf->max_pf_rxqs);
  3420. }
  3421. /**
  3422. * ice_deinit_pf - Unrolls initialziations done by ice_init_pf
  3423. * @pf: board private structure to initialize
  3424. */
  3425. static void ice_deinit_pf(struct ice_pf *pf)
  3426. {
  3427. ice_service_task_stop(pf);
  3428. mutex_destroy(&pf->lag_mutex);
  3429. mutex_destroy(&pf->adev_mutex);
  3430. mutex_destroy(&pf->sw_mutex);
  3431. mutex_destroy(&pf->tc_mutex);
  3432. mutex_destroy(&pf->avail_q_mutex);
  3433. mutex_destroy(&pf->vfs.table_lock);
  3434. if (pf->avail_txqs) {
  3435. bitmap_free(pf->avail_txqs);
  3436. pf->avail_txqs = NULL;
  3437. }
  3438. if (pf->avail_rxqs) {
  3439. bitmap_free(pf->avail_rxqs);
  3440. pf->avail_rxqs = NULL;
  3441. }
  3442. if (pf->ptp.clock)
  3443. ptp_clock_unregister(pf->ptp.clock);
  3444. xa_destroy(&pf->dyn_ports);
  3445. xa_destroy(&pf->sf_nums);
  3446. }
  3447. /**
  3448. * ice_set_pf_caps - set PFs capability flags
  3449. * @pf: pointer to the PF instance
  3450. */
  3451. static void ice_set_pf_caps(struct ice_pf *pf)
  3452. {
  3453. struct ice_hw_func_caps *func_caps = &pf->hw.func_caps;
  3454. clear_bit(ICE_FLAG_RDMA_ENA, pf->flags);
  3455. if (func_caps->common_cap.rdma)
  3456. set_bit(ICE_FLAG_RDMA_ENA, pf->flags);
  3457. clear_bit(ICE_FLAG_DCB_CAPABLE, pf->flags);
  3458. if (func_caps->common_cap.dcb)
  3459. set_bit(ICE_FLAG_DCB_CAPABLE, pf->flags);
  3460. clear_bit(ICE_FLAG_SRIOV_CAPABLE, pf->flags);
  3461. if (func_caps->common_cap.sr_iov_1_1) {
  3462. set_bit(ICE_FLAG_SRIOV_CAPABLE, pf->flags);
  3463. pf->vfs.num_supported = min_t(int, func_caps->num_allocd_vfs,
  3464. ICE_MAX_SRIOV_VFS);
  3465. }
  3466. clear_bit(ICE_FLAG_RSS_ENA, pf->flags);
  3467. if (func_caps->common_cap.rss_table_size)
  3468. set_bit(ICE_FLAG_RSS_ENA, pf->flags);
  3469. clear_bit(ICE_FLAG_FD_ENA, pf->flags);
  3470. if (func_caps->fd_fltr_guar > 0 || func_caps->fd_fltr_best_effort > 0) {
  3471. u16 unused;
  3472. /* ctrl_vsi_idx will be set to a valid value when flow director
  3473. * is setup by ice_init_fdir
  3474. */
  3475. pf->ctrl_vsi_idx = ICE_NO_VSI;
  3476. set_bit(ICE_FLAG_FD_ENA, pf->flags);
  3477. /* force guaranteed filter pool for PF */
  3478. ice_alloc_fd_guar_item(&pf->hw, &unused,
  3479. func_caps->fd_fltr_guar);
  3480. /* force shared filter pool for PF */
  3481. ice_alloc_fd_shrd_item(&pf->hw, &unused,
  3482. func_caps->fd_fltr_best_effort);
  3483. }
  3484. clear_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags);
  3485. if (func_caps->common_cap.ieee_1588 &&
  3486. !(pf->hw.mac_type == ICE_MAC_E830))
  3487. set_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags);
  3488. pf->max_pf_txqs = func_caps->common_cap.num_txq;
  3489. pf->max_pf_rxqs = func_caps->common_cap.num_rxq;
  3490. }
  3491. /**
  3492. * ice_init_pf - Initialize general software structures (struct ice_pf)
  3493. * @pf: board private structure to initialize
  3494. */
  3495. static int ice_init_pf(struct ice_pf *pf)
  3496. {
  3497. ice_set_pf_caps(pf);
  3498. mutex_init(&pf->sw_mutex);
  3499. mutex_init(&pf->tc_mutex);
  3500. mutex_init(&pf->adev_mutex);
  3501. mutex_init(&pf->lag_mutex);
  3502. INIT_HLIST_HEAD(&pf->aq_wait_list);
  3503. spin_lock_init(&pf->aq_wait_lock);
  3504. init_waitqueue_head(&pf->aq_wait_queue);
  3505. init_waitqueue_head(&pf->reset_wait_queue);
  3506. /* setup service timer and periodic service task */
  3507. timer_setup(&pf->serv_tmr, ice_service_timer, 0);
  3508. pf->serv_tmr_period = HZ;
  3509. INIT_WORK(&pf->serv_task, ice_service_task);
  3510. clear_bit(ICE_SERVICE_SCHED, pf->state);
  3511. mutex_init(&pf->avail_q_mutex);
  3512. pf->avail_txqs = bitmap_zalloc(pf->max_pf_txqs, GFP_KERNEL);
  3513. if (!pf->avail_txqs)
  3514. return -ENOMEM;
  3515. pf->avail_rxqs = bitmap_zalloc(pf->max_pf_rxqs, GFP_KERNEL);
  3516. if (!pf->avail_rxqs) {
  3517. bitmap_free(pf->avail_txqs);
  3518. pf->avail_txqs = NULL;
  3519. return -ENOMEM;
  3520. }
  3521. mutex_init(&pf->vfs.table_lock);
  3522. hash_init(pf->vfs.table);
  3523. if (ice_is_feature_supported(pf, ICE_F_MBX_LIMIT))
  3524. wr32(&pf->hw, E830_MBX_PF_IN_FLIGHT_VF_MSGS_THRESH,
  3525. ICE_MBX_OVERFLOW_WATERMARK);
  3526. else
  3527. ice_mbx_init_snapshot(&pf->hw);
  3528. xa_init(&pf->dyn_ports);
  3529. xa_init(&pf->sf_nums);
  3530. return 0;
  3531. }
  3532. /**
  3533. * ice_is_wol_supported - check if WoL is supported
  3534. * @hw: pointer to hardware info
  3535. *
  3536. * Check if WoL is supported based on the HW configuration.
  3537. * Returns true if NVM supports and enables WoL for this port, false otherwise
  3538. */
  3539. bool ice_is_wol_supported(struct ice_hw *hw)
  3540. {
  3541. u16 wol_ctrl;
  3542. /* A bit set to 1 in the NVM Software Reserved Word 2 (WoL control
  3543. * word) indicates WoL is not supported on the corresponding PF ID.
  3544. */
  3545. if (ice_read_sr_word(hw, ICE_SR_NVM_WOL_CFG, &wol_ctrl))
  3546. return false;
  3547. return !(BIT(hw->port_info->lport) & wol_ctrl);
  3548. }
  3549. /**
  3550. * ice_vsi_recfg_qs - Change the number of queues on a VSI
  3551. * @vsi: VSI being changed
  3552. * @new_rx: new number of Rx queues
  3553. * @new_tx: new number of Tx queues
  3554. * @locked: is adev device_lock held
  3555. *
  3556. * Only change the number of queues if new_tx, or new_rx is non-0.
  3557. *
  3558. * Returns 0 on success.
  3559. */
  3560. int ice_vsi_recfg_qs(struct ice_vsi *vsi, int new_rx, int new_tx, bool locked)
  3561. {
  3562. struct ice_pf *pf = vsi->back;
  3563. int i, err = 0, timeout = 50;
  3564. if (!new_rx && !new_tx)
  3565. return -EINVAL;
  3566. while (test_and_set_bit(ICE_CFG_BUSY, pf->state)) {
  3567. timeout--;
  3568. if (!timeout)
  3569. return -EBUSY;
  3570. usleep_range(1000, 2000);
  3571. }
  3572. if (new_tx)
  3573. vsi->req_txq = (u16)new_tx;
  3574. if (new_rx)
  3575. vsi->req_rxq = (u16)new_rx;
  3576. /* set for the next time the netdev is started */
  3577. if (!netif_running(vsi->netdev)) {
  3578. err = ice_vsi_rebuild(vsi, ICE_VSI_FLAG_NO_INIT);
  3579. if (err)
  3580. goto rebuild_err;
  3581. dev_dbg(ice_pf_to_dev(pf), "Link is down, queue count change happens when link is brought up\n");
  3582. goto done;
  3583. }
  3584. ice_vsi_close(vsi);
  3585. err = ice_vsi_rebuild(vsi, ICE_VSI_FLAG_NO_INIT);
  3586. if (err)
  3587. goto rebuild_err;
  3588. ice_for_each_traffic_class(i) {
  3589. if (vsi->tc_cfg.ena_tc & BIT(i))
  3590. netdev_set_tc_queue(vsi->netdev,
  3591. vsi->tc_cfg.tc_info[i].netdev_tc,
  3592. vsi->tc_cfg.tc_info[i].qcount_tx,
  3593. vsi->tc_cfg.tc_info[i].qoffset);
  3594. }
  3595. ice_pf_dcb_recfg(pf, locked);
  3596. ice_vsi_open(vsi);
  3597. goto done;
  3598. rebuild_err:
  3599. dev_err(ice_pf_to_dev(pf), "Error during VSI rebuild: %d. Unload and reload the driver.\n",
  3600. err);
  3601. done:
  3602. clear_bit(ICE_CFG_BUSY, pf->state);
  3603. return err;
  3604. }
  3605. /**
  3606. * ice_set_safe_mode_vlan_cfg - configure PF VSI to allow all VLANs in safe mode
  3607. * @pf: PF to configure
  3608. *
  3609. * No VLAN offloads/filtering are advertised in safe mode so make sure the PF
  3610. * VSI can still Tx/Rx VLAN tagged packets.
  3611. */
  3612. static void ice_set_safe_mode_vlan_cfg(struct ice_pf *pf)
  3613. {
  3614. struct ice_vsi *vsi = ice_get_main_vsi(pf);
  3615. struct ice_vsi_ctx *ctxt;
  3616. struct ice_hw *hw;
  3617. int status;
  3618. if (!vsi)
  3619. return;
  3620. ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
  3621. if (!ctxt)
  3622. return;
  3623. hw = &pf->hw;
  3624. ctxt->info = vsi->info;
  3625. ctxt->info.valid_sections =
  3626. cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID |
  3627. ICE_AQ_VSI_PROP_SECURITY_VALID |
  3628. ICE_AQ_VSI_PROP_SW_VALID);
  3629. /* disable VLAN anti-spoof */
  3630. ctxt->info.sec_flags &= ~(ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
  3631. ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
  3632. /* disable VLAN pruning and keep all other settings */
  3633. ctxt->info.sw_flags2 &= ~ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
  3634. /* allow all VLANs on Tx and don't strip on Rx */
  3635. ctxt->info.inner_vlan_flags = ICE_AQ_VSI_INNER_VLAN_TX_MODE_ALL |
  3636. ICE_AQ_VSI_INNER_VLAN_EMODE_NOTHING;
  3637. status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
  3638. if (status) {
  3639. dev_err(ice_pf_to_dev(vsi->back), "Failed to update VSI for safe mode VLANs, err %d aq_err %s\n",
  3640. status, ice_aq_str(hw->adminq.sq_last_status));
  3641. } else {
  3642. vsi->info.sec_flags = ctxt->info.sec_flags;
  3643. vsi->info.sw_flags2 = ctxt->info.sw_flags2;
  3644. vsi->info.inner_vlan_flags = ctxt->info.inner_vlan_flags;
  3645. }
  3646. kfree(ctxt);
  3647. }
  3648. /**
  3649. * ice_log_pkg_init - log result of DDP package load
  3650. * @hw: pointer to hardware info
  3651. * @state: state of package load
  3652. */
  3653. static void ice_log_pkg_init(struct ice_hw *hw, enum ice_ddp_state state)
  3654. {
  3655. struct ice_pf *pf = hw->back;
  3656. struct device *dev;
  3657. dev = ice_pf_to_dev(pf);
  3658. switch (state) {
  3659. case ICE_DDP_PKG_SUCCESS:
  3660. dev_info(dev, "The DDP package was successfully loaded: %s version %d.%d.%d.%d\n",
  3661. hw->active_pkg_name,
  3662. hw->active_pkg_ver.major,
  3663. hw->active_pkg_ver.minor,
  3664. hw->active_pkg_ver.update,
  3665. hw->active_pkg_ver.draft);
  3666. break;
  3667. case ICE_DDP_PKG_SAME_VERSION_ALREADY_LOADED:
  3668. dev_info(dev, "DDP package already present on device: %s version %d.%d.%d.%d\n",
  3669. hw->active_pkg_name,
  3670. hw->active_pkg_ver.major,
  3671. hw->active_pkg_ver.minor,
  3672. hw->active_pkg_ver.update,
  3673. hw->active_pkg_ver.draft);
  3674. break;
  3675. case ICE_DDP_PKG_ALREADY_LOADED_NOT_SUPPORTED:
  3676. dev_err(dev, "The device has a DDP package that is not supported by the driver. The device has package '%s' version %d.%d.x.x. The driver requires version %d.%d.x.x. Entering Safe Mode.\n",
  3677. hw->active_pkg_name,
  3678. hw->active_pkg_ver.major,
  3679. hw->active_pkg_ver.minor,
  3680. ICE_PKG_SUPP_VER_MAJ, ICE_PKG_SUPP_VER_MNR);
  3681. break;
  3682. case ICE_DDP_PKG_COMPATIBLE_ALREADY_LOADED:
  3683. dev_info(dev, "The driver could not load the DDP package file because a compatible DDP package is already present on the device. The device has package '%s' version %d.%d.%d.%d. The package file found by the driver: '%s' version %d.%d.%d.%d.\n",
  3684. hw->active_pkg_name,
  3685. hw->active_pkg_ver.major,
  3686. hw->active_pkg_ver.minor,
  3687. hw->active_pkg_ver.update,
  3688. hw->active_pkg_ver.draft,
  3689. hw->pkg_name,
  3690. hw->pkg_ver.major,
  3691. hw->pkg_ver.minor,
  3692. hw->pkg_ver.update,
  3693. hw->pkg_ver.draft);
  3694. break;
  3695. case ICE_DDP_PKG_FW_MISMATCH:
  3696. dev_err(dev, "The firmware loaded on the device is not compatible with the DDP package. Please update the device's NVM. Entering safe mode.\n");
  3697. break;
  3698. case ICE_DDP_PKG_INVALID_FILE:
  3699. dev_err(dev, "The DDP package file is invalid. Entering Safe Mode.\n");
  3700. break;
  3701. case ICE_DDP_PKG_FILE_VERSION_TOO_HIGH:
  3702. dev_err(dev, "The DDP package file version is higher than the driver supports. Please use an updated driver. Entering Safe Mode.\n");
  3703. break;
  3704. case ICE_DDP_PKG_FILE_VERSION_TOO_LOW:
  3705. dev_err(dev, "The DDP package file version is lower than the driver supports. The driver requires version %d.%d.x.x. Please use an updated DDP Package file. Entering Safe Mode.\n",
  3706. ICE_PKG_SUPP_VER_MAJ, ICE_PKG_SUPP_VER_MNR);
  3707. break;
  3708. case ICE_DDP_PKG_FILE_SIGNATURE_INVALID:
  3709. dev_err(dev, "The DDP package could not be loaded because its signature is not valid. Please use a valid DDP Package. Entering Safe Mode.\n");
  3710. break;
  3711. case ICE_DDP_PKG_FILE_REVISION_TOO_LOW:
  3712. dev_err(dev, "The DDP Package could not be loaded because its security revision is too low. Please use an updated DDP Package. Entering Safe Mode.\n");
  3713. break;
  3714. case ICE_DDP_PKG_LOAD_ERROR:
  3715. dev_err(dev, "An error occurred on the device while loading the DDP package. The device will be reset.\n");
  3716. /* poll for reset to complete */
  3717. if (ice_check_reset(hw))
  3718. dev_err(dev, "Error resetting device. Please reload the driver\n");
  3719. break;
  3720. case ICE_DDP_PKG_ERR:
  3721. default:
  3722. dev_err(dev, "An unknown error occurred when loading the DDP package. Entering Safe Mode.\n");
  3723. break;
  3724. }
  3725. }
  3726. /**
  3727. * ice_load_pkg - load/reload the DDP Package file
  3728. * @firmware: firmware structure when firmware requested or NULL for reload
  3729. * @pf: pointer to the PF instance
  3730. *
  3731. * Called on probe and post CORER/GLOBR rebuild to load DDP Package and
  3732. * initialize HW tables.
  3733. */
  3734. static void
  3735. ice_load_pkg(const struct firmware *firmware, struct ice_pf *pf)
  3736. {
  3737. enum ice_ddp_state state = ICE_DDP_PKG_ERR;
  3738. struct device *dev = ice_pf_to_dev(pf);
  3739. struct ice_hw *hw = &pf->hw;
  3740. /* Load DDP Package */
  3741. if (firmware && !hw->pkg_copy) {
  3742. state = ice_copy_and_init_pkg(hw, firmware->data,
  3743. firmware->size);
  3744. ice_log_pkg_init(hw, state);
  3745. } else if (!firmware && hw->pkg_copy) {
  3746. /* Reload package during rebuild after CORER/GLOBR reset */
  3747. state = ice_init_pkg(hw, hw->pkg_copy, hw->pkg_size);
  3748. ice_log_pkg_init(hw, state);
  3749. } else {
  3750. dev_err(dev, "The DDP package file failed to load. Entering Safe Mode.\n");
  3751. }
  3752. if (!ice_is_init_pkg_successful(state)) {
  3753. /* Safe Mode */
  3754. clear_bit(ICE_FLAG_ADV_FEATURES, pf->flags);
  3755. return;
  3756. }
  3757. /* Successful download package is the precondition for advanced
  3758. * features, hence setting the ICE_FLAG_ADV_FEATURES flag
  3759. */
  3760. set_bit(ICE_FLAG_ADV_FEATURES, pf->flags);
  3761. }
  3762. /**
  3763. * ice_verify_cacheline_size - verify driver's assumption of 64 Byte cache lines
  3764. * @pf: pointer to the PF structure
  3765. *
  3766. * There is no error returned here because the driver should be able to handle
  3767. * 128 Byte cache lines, so we only print a warning in case issues are seen,
  3768. * specifically with Tx.
  3769. */
  3770. static void ice_verify_cacheline_size(struct ice_pf *pf)
  3771. {
  3772. if (rd32(&pf->hw, GLPCI_CNF2) & GLPCI_CNF2_CACHELINE_SIZE_M)
  3773. dev_warn(ice_pf_to_dev(pf), "%d Byte cache line assumption is invalid, driver may have Tx timeouts!\n",
  3774. ICE_CACHE_LINE_BYTES);
  3775. }
  3776. /**
  3777. * ice_send_version - update firmware with driver version
  3778. * @pf: PF struct
  3779. *
  3780. * Returns 0 on success, else error code
  3781. */
  3782. static int ice_send_version(struct ice_pf *pf)
  3783. {
  3784. struct ice_driver_ver dv;
  3785. dv.major_ver = 0xff;
  3786. dv.minor_ver = 0xff;
  3787. dv.build_ver = 0xff;
  3788. dv.subbuild_ver = 0;
  3789. strscpy((char *)dv.driver_string, UTS_RELEASE,
  3790. sizeof(dv.driver_string));
  3791. return ice_aq_send_driver_ver(&pf->hw, &dv, NULL);
  3792. }
  3793. /**
  3794. * ice_init_fdir - Initialize flow director VSI and configuration
  3795. * @pf: pointer to the PF instance
  3796. *
  3797. * returns 0 on success, negative on error
  3798. */
  3799. static int ice_init_fdir(struct ice_pf *pf)
  3800. {
  3801. struct device *dev = ice_pf_to_dev(pf);
  3802. struct ice_vsi *ctrl_vsi;
  3803. int err;
  3804. /* Side Band Flow Director needs to have a control VSI.
  3805. * Allocate it and store it in the PF.
  3806. */
  3807. ctrl_vsi = ice_ctrl_vsi_setup(pf, pf->hw.port_info);
  3808. if (!ctrl_vsi) {
  3809. dev_dbg(dev, "could not create control VSI\n");
  3810. return -ENOMEM;
  3811. }
  3812. err = ice_vsi_open_ctrl(ctrl_vsi);
  3813. if (err) {
  3814. dev_dbg(dev, "could not open control VSI\n");
  3815. goto err_vsi_open;
  3816. }
  3817. mutex_init(&pf->hw.fdir_fltr_lock);
  3818. err = ice_fdir_create_dflt_rules(pf);
  3819. if (err)
  3820. goto err_fdir_rule;
  3821. return 0;
  3822. err_fdir_rule:
  3823. ice_fdir_release_flows(&pf->hw);
  3824. ice_vsi_close(ctrl_vsi);
  3825. err_vsi_open:
  3826. ice_vsi_release(ctrl_vsi);
  3827. if (pf->ctrl_vsi_idx != ICE_NO_VSI) {
  3828. pf->vsi[pf->ctrl_vsi_idx] = NULL;
  3829. pf->ctrl_vsi_idx = ICE_NO_VSI;
  3830. }
  3831. return err;
  3832. }
  3833. static void ice_deinit_fdir(struct ice_pf *pf)
  3834. {
  3835. struct ice_vsi *vsi = ice_get_ctrl_vsi(pf);
  3836. if (!vsi)
  3837. return;
  3838. ice_vsi_manage_fdir(vsi, false);
  3839. ice_vsi_release(vsi);
  3840. if (pf->ctrl_vsi_idx != ICE_NO_VSI) {
  3841. pf->vsi[pf->ctrl_vsi_idx] = NULL;
  3842. pf->ctrl_vsi_idx = ICE_NO_VSI;
  3843. }
  3844. mutex_destroy(&(&pf->hw)->fdir_fltr_lock);
  3845. }
  3846. /**
  3847. * ice_get_opt_fw_name - return optional firmware file name or NULL
  3848. * @pf: pointer to the PF instance
  3849. */
  3850. static char *ice_get_opt_fw_name(struct ice_pf *pf)
  3851. {
  3852. /* Optional firmware name same as default with additional dash
  3853. * followed by a EUI-64 identifier (PCIe Device Serial Number)
  3854. */
  3855. struct pci_dev *pdev = pf->pdev;
  3856. char *opt_fw_filename;
  3857. u64 dsn;
  3858. /* Determine the name of the optional file using the DSN (two
  3859. * dwords following the start of the DSN Capability).
  3860. */
  3861. dsn = pci_get_dsn(pdev);
  3862. if (!dsn)
  3863. return NULL;
  3864. opt_fw_filename = kzalloc(NAME_MAX, GFP_KERNEL);
  3865. if (!opt_fw_filename)
  3866. return NULL;
  3867. snprintf(opt_fw_filename, NAME_MAX, "%sice-%016llx.pkg",
  3868. ICE_DDP_PKG_PATH, dsn);
  3869. return opt_fw_filename;
  3870. }
  3871. /**
  3872. * ice_request_fw - Device initialization routine
  3873. * @pf: pointer to the PF instance
  3874. * @firmware: double pointer to firmware struct
  3875. *
  3876. * Return: zero when successful, negative values otherwise.
  3877. */
  3878. static int ice_request_fw(struct ice_pf *pf, const struct firmware **firmware)
  3879. {
  3880. char *opt_fw_filename = ice_get_opt_fw_name(pf);
  3881. struct device *dev = ice_pf_to_dev(pf);
  3882. int err = 0;
  3883. /* optional device-specific DDP (if present) overrides the default DDP
  3884. * package file. kernel logs a debug message if the file doesn't exist,
  3885. * and warning messages for other errors.
  3886. */
  3887. if (opt_fw_filename) {
  3888. err = firmware_request_nowarn(firmware, opt_fw_filename, dev);
  3889. kfree(opt_fw_filename);
  3890. if (!err)
  3891. return err;
  3892. }
  3893. err = request_firmware(firmware, ICE_DDP_PKG_FILE, dev);
  3894. if (err)
  3895. dev_err(dev, "The DDP package file was not found or could not be read. Entering Safe Mode\n");
  3896. return err;
  3897. }
  3898. /**
  3899. * ice_init_tx_topology - performs Tx topology initialization
  3900. * @hw: pointer to the hardware structure
  3901. * @firmware: pointer to firmware structure
  3902. *
  3903. * Return: zero when init was successful, negative values otherwise.
  3904. */
  3905. static int
  3906. ice_init_tx_topology(struct ice_hw *hw, const struct firmware *firmware)
  3907. {
  3908. u8 num_tx_sched_layers = hw->num_tx_sched_layers;
  3909. struct ice_pf *pf = hw->back;
  3910. struct device *dev;
  3911. int err;
  3912. dev = ice_pf_to_dev(pf);
  3913. err = ice_cfg_tx_topo(hw, firmware->data, firmware->size);
  3914. if (!err) {
  3915. if (hw->num_tx_sched_layers > num_tx_sched_layers)
  3916. dev_info(dev, "Tx scheduling layers switching feature disabled\n");
  3917. else
  3918. dev_info(dev, "Tx scheduling layers switching feature enabled\n");
  3919. return 0;
  3920. } else if (err == -ENODEV) {
  3921. /* If we failed to re-initialize the device, we can no longer
  3922. * continue loading.
  3923. */
  3924. dev_warn(dev, "Failed to initialize hardware after applying Tx scheduling configuration.\n");
  3925. return err;
  3926. } else if (err == -EIO) {
  3927. dev_info(dev, "DDP package does not support Tx scheduling layers switching feature - please update to the latest DDP package and try again\n");
  3928. return 0;
  3929. } else if (err == -EEXIST) {
  3930. return 0;
  3931. }
  3932. /* Do not treat this as a fatal error. */
  3933. dev_info(dev, "Failed to apply Tx scheduling configuration, err %pe\n",
  3934. ERR_PTR(err));
  3935. return 0;
  3936. }
  3937. /**
  3938. * ice_init_ddp_config - DDP related configuration
  3939. * @hw: pointer to the hardware structure
  3940. * @pf: pointer to pf structure
  3941. *
  3942. * This function loads DDP file from the disk, then initializes Tx
  3943. * topology. At the end DDP package is loaded on the card.
  3944. *
  3945. * Return: zero when init was successful, negative values otherwise.
  3946. */
  3947. static int ice_init_ddp_config(struct ice_hw *hw, struct ice_pf *pf)
  3948. {
  3949. struct device *dev = ice_pf_to_dev(pf);
  3950. const struct firmware *firmware = NULL;
  3951. int err;
  3952. err = ice_request_fw(pf, &firmware);
  3953. if (err) {
  3954. dev_err(dev, "Fail during requesting FW: %d\n", err);
  3955. return err;
  3956. }
  3957. err = ice_init_tx_topology(hw, firmware);
  3958. if (err) {
  3959. dev_err(dev, "Fail during initialization of Tx topology: %d\n",
  3960. err);
  3961. release_firmware(firmware);
  3962. return err;
  3963. }
  3964. /* Download firmware to device */
  3965. ice_load_pkg(firmware, pf);
  3966. release_firmware(firmware);
  3967. return 0;
  3968. }
  3969. /**
  3970. * ice_print_wake_reason - show the wake up cause in the log
  3971. * @pf: pointer to the PF struct
  3972. */
  3973. static void ice_print_wake_reason(struct ice_pf *pf)
  3974. {
  3975. u32 wus = pf->wakeup_reason;
  3976. const char *wake_str;
  3977. /* if no wake event, nothing to print */
  3978. if (!wus)
  3979. return;
  3980. if (wus & PFPM_WUS_LNKC_M)
  3981. wake_str = "Link\n";
  3982. else if (wus & PFPM_WUS_MAG_M)
  3983. wake_str = "Magic Packet\n";
  3984. else if (wus & PFPM_WUS_MNG_M)
  3985. wake_str = "Management\n";
  3986. else if (wus & PFPM_WUS_FW_RST_WK_M)
  3987. wake_str = "Firmware Reset\n";
  3988. else
  3989. wake_str = "Unknown\n";
  3990. dev_info(ice_pf_to_dev(pf), "Wake reason: %s", wake_str);
  3991. }
  3992. /**
  3993. * ice_pf_fwlog_update_module - update 1 module
  3994. * @pf: pointer to the PF struct
  3995. * @log_level: log_level to use for the @module
  3996. * @module: module to update
  3997. */
  3998. void ice_pf_fwlog_update_module(struct ice_pf *pf, int log_level, int module)
  3999. {
  4000. struct ice_hw *hw = &pf->hw;
  4001. hw->fwlog_cfg.module_entries[module].log_level = log_level;
  4002. }
  4003. /**
  4004. * ice_register_netdev - register netdev
  4005. * @vsi: pointer to the VSI struct
  4006. */
  4007. static int ice_register_netdev(struct ice_vsi *vsi)
  4008. {
  4009. int err;
  4010. if (!vsi || !vsi->netdev)
  4011. return -EIO;
  4012. err = register_netdev(vsi->netdev);
  4013. if (err)
  4014. return err;
  4015. set_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state);
  4016. netif_carrier_off(vsi->netdev);
  4017. netif_tx_stop_all_queues(vsi->netdev);
  4018. return 0;
  4019. }
  4020. static void ice_unregister_netdev(struct ice_vsi *vsi)
  4021. {
  4022. if (!vsi || !vsi->netdev)
  4023. return;
  4024. unregister_netdev(vsi->netdev);
  4025. clear_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state);
  4026. }
  4027. /**
  4028. * ice_cfg_netdev - Allocate, configure and register a netdev
  4029. * @vsi: the VSI associated with the new netdev
  4030. *
  4031. * Returns 0 on success, negative value on failure
  4032. */
  4033. static int ice_cfg_netdev(struct ice_vsi *vsi)
  4034. {
  4035. struct ice_netdev_priv *np;
  4036. struct net_device *netdev;
  4037. u8 mac_addr[ETH_ALEN];
  4038. netdev = alloc_etherdev_mqs(sizeof(*np), vsi->alloc_txq,
  4039. vsi->alloc_rxq);
  4040. if (!netdev)
  4041. return -ENOMEM;
  4042. set_bit(ICE_VSI_NETDEV_ALLOCD, vsi->state);
  4043. vsi->netdev = netdev;
  4044. np = netdev_priv(netdev);
  4045. np->vsi = vsi;
  4046. ice_set_netdev_features(netdev);
  4047. ice_set_ops(vsi);
  4048. if (vsi->type == ICE_VSI_PF) {
  4049. SET_NETDEV_DEV(netdev, ice_pf_to_dev(vsi->back));
  4050. ether_addr_copy(mac_addr, vsi->port_info->mac.perm_addr);
  4051. eth_hw_addr_set(netdev, mac_addr);
  4052. }
  4053. netdev->priv_flags |= IFF_UNICAST_FLT;
  4054. /* Setup netdev TC information */
  4055. ice_vsi_cfg_netdev_tc(vsi, vsi->tc_cfg.ena_tc);
  4056. netdev->max_mtu = ICE_MAX_MTU;
  4057. return 0;
  4058. }
  4059. static void ice_decfg_netdev(struct ice_vsi *vsi)
  4060. {
  4061. clear_bit(ICE_VSI_NETDEV_ALLOCD, vsi->state);
  4062. free_netdev(vsi->netdev);
  4063. vsi->netdev = NULL;
  4064. }
  4065. /**
  4066. * ice_wait_for_fw - wait for full FW readiness
  4067. * @hw: pointer to the hardware structure
  4068. * @timeout: milliseconds that can elapse before timing out
  4069. */
  4070. static int ice_wait_for_fw(struct ice_hw *hw, u32 timeout)
  4071. {
  4072. int fw_loading;
  4073. u32 elapsed = 0;
  4074. while (elapsed <= timeout) {
  4075. fw_loading = rd32(hw, GL_MNG_FWSM) & GL_MNG_FWSM_FW_LOADING_M;
  4076. /* firmware was not yet loaded, we have to wait more */
  4077. if (fw_loading) {
  4078. elapsed += 100;
  4079. msleep(100);
  4080. continue;
  4081. }
  4082. return 0;
  4083. }
  4084. return -ETIMEDOUT;
  4085. }
  4086. int ice_init_dev(struct ice_pf *pf)
  4087. {
  4088. struct device *dev = ice_pf_to_dev(pf);
  4089. struct ice_hw *hw = &pf->hw;
  4090. int err;
  4091. err = ice_init_hw(hw);
  4092. if (err) {
  4093. dev_err(dev, "ice_init_hw failed: %d\n", err);
  4094. return err;
  4095. }
  4096. /* Some cards require longer initialization times
  4097. * due to necessity of loading FW from an external source.
  4098. * This can take even half a minute.
  4099. */
  4100. if (ice_is_pf_c827(hw)) {
  4101. err = ice_wait_for_fw(hw, 30000);
  4102. if (err) {
  4103. dev_err(dev, "ice_wait_for_fw timed out");
  4104. return err;
  4105. }
  4106. }
  4107. ice_init_feature_support(pf);
  4108. err = ice_init_ddp_config(hw, pf);
  4109. /* if ice_init_ddp_config fails, ICE_FLAG_ADV_FEATURES bit won't be
  4110. * set in pf->state, which will cause ice_is_safe_mode to return
  4111. * true
  4112. */
  4113. if (err || ice_is_safe_mode(pf)) {
  4114. /* we already got function/device capabilities but these don't
  4115. * reflect what the driver needs to do in safe mode. Instead of
  4116. * adding conditional logic everywhere to ignore these
  4117. * device/function capabilities, override them.
  4118. */
  4119. ice_set_safe_mode_caps(hw);
  4120. }
  4121. err = ice_init_pf(pf);
  4122. if (err) {
  4123. dev_err(dev, "ice_init_pf failed: %d\n", err);
  4124. goto err_init_pf;
  4125. }
  4126. pf->hw.udp_tunnel_nic.set_port = ice_udp_tunnel_set_port;
  4127. pf->hw.udp_tunnel_nic.unset_port = ice_udp_tunnel_unset_port;
  4128. pf->hw.udp_tunnel_nic.flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP;
  4129. pf->hw.udp_tunnel_nic.shared = &pf->hw.udp_tunnel_shared;
  4130. if (pf->hw.tnl.valid_count[TNL_VXLAN]) {
  4131. pf->hw.udp_tunnel_nic.tables[0].n_entries =
  4132. pf->hw.tnl.valid_count[TNL_VXLAN];
  4133. pf->hw.udp_tunnel_nic.tables[0].tunnel_types =
  4134. UDP_TUNNEL_TYPE_VXLAN;
  4135. }
  4136. if (pf->hw.tnl.valid_count[TNL_GENEVE]) {
  4137. pf->hw.udp_tunnel_nic.tables[1].n_entries =
  4138. pf->hw.tnl.valid_count[TNL_GENEVE];
  4139. pf->hw.udp_tunnel_nic.tables[1].tunnel_types =
  4140. UDP_TUNNEL_TYPE_GENEVE;
  4141. }
  4142. err = ice_init_interrupt_scheme(pf);
  4143. if (err) {
  4144. dev_err(dev, "ice_init_interrupt_scheme failed: %d\n", err);
  4145. err = -EIO;
  4146. goto err_init_interrupt_scheme;
  4147. }
  4148. /* In case of MSIX we are going to setup the misc vector right here
  4149. * to handle admin queue events etc. In case of legacy and MSI
  4150. * the misc functionality and queue processing is combined in
  4151. * the same vector and that gets setup at open.
  4152. */
  4153. err = ice_req_irq_msix_misc(pf);
  4154. if (err) {
  4155. dev_err(dev, "setup of misc vector failed: %d\n", err);
  4156. goto err_req_irq_msix_misc;
  4157. }
  4158. return 0;
  4159. err_req_irq_msix_misc:
  4160. ice_clear_interrupt_scheme(pf);
  4161. err_init_interrupt_scheme:
  4162. ice_deinit_pf(pf);
  4163. err_init_pf:
  4164. ice_deinit_hw(hw);
  4165. return err;
  4166. }
  4167. void ice_deinit_dev(struct ice_pf *pf)
  4168. {
  4169. ice_free_irq_msix_misc(pf);
  4170. ice_deinit_pf(pf);
  4171. ice_deinit_hw(&pf->hw);
  4172. /* Service task is already stopped, so call reset directly. */
  4173. ice_reset(&pf->hw, ICE_RESET_PFR);
  4174. pci_wait_for_pending_transaction(pf->pdev);
  4175. ice_clear_interrupt_scheme(pf);
  4176. }
  4177. static void ice_init_features(struct ice_pf *pf)
  4178. {
  4179. struct device *dev = ice_pf_to_dev(pf);
  4180. if (ice_is_safe_mode(pf))
  4181. return;
  4182. /* initialize DDP driven features */
  4183. if (test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags))
  4184. ice_ptp_init(pf);
  4185. if (ice_is_feature_supported(pf, ICE_F_GNSS))
  4186. ice_gnss_init(pf);
  4187. if (ice_is_feature_supported(pf, ICE_F_CGU) ||
  4188. ice_is_feature_supported(pf, ICE_F_PHY_RCLK))
  4189. ice_dpll_init(pf);
  4190. /* Note: Flow director init failure is non-fatal to load */
  4191. if (ice_init_fdir(pf))
  4192. dev_err(dev, "could not initialize flow director\n");
  4193. /* Note: DCB init failure is non-fatal to load */
  4194. if (ice_init_pf_dcb(pf, false)) {
  4195. clear_bit(ICE_FLAG_DCB_CAPABLE, pf->flags);
  4196. clear_bit(ICE_FLAG_DCB_ENA, pf->flags);
  4197. } else {
  4198. ice_cfg_lldp_mib_change(&pf->hw, true);
  4199. }
  4200. if (ice_init_lag(pf))
  4201. dev_warn(dev, "Failed to init link aggregation support\n");
  4202. ice_hwmon_init(pf);
  4203. }
  4204. static void ice_deinit_features(struct ice_pf *pf)
  4205. {
  4206. if (ice_is_safe_mode(pf))
  4207. return;
  4208. ice_deinit_lag(pf);
  4209. if (test_bit(ICE_FLAG_DCB_CAPABLE, pf->flags))
  4210. ice_cfg_lldp_mib_change(&pf->hw, false);
  4211. ice_deinit_fdir(pf);
  4212. if (ice_is_feature_supported(pf, ICE_F_GNSS))
  4213. ice_gnss_exit(pf);
  4214. if (test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags))
  4215. ice_ptp_release(pf);
  4216. if (test_bit(ICE_FLAG_DPLL, pf->flags))
  4217. ice_dpll_deinit(pf);
  4218. if (pf->eswitch_mode == DEVLINK_ESWITCH_MODE_SWITCHDEV)
  4219. xa_destroy(&pf->eswitch.reprs);
  4220. }
  4221. static void ice_init_wakeup(struct ice_pf *pf)
  4222. {
  4223. /* Save wakeup reason register for later use */
  4224. pf->wakeup_reason = rd32(&pf->hw, PFPM_WUS);
  4225. /* check for a power management event */
  4226. ice_print_wake_reason(pf);
  4227. /* clear wake status, all bits */
  4228. wr32(&pf->hw, PFPM_WUS, U32_MAX);
  4229. /* Disable WoL at init, wait for user to enable */
  4230. device_set_wakeup_enable(ice_pf_to_dev(pf), false);
  4231. }
  4232. static int ice_init_link(struct ice_pf *pf)
  4233. {
  4234. struct device *dev = ice_pf_to_dev(pf);
  4235. int err;
  4236. err = ice_init_link_events(pf->hw.port_info);
  4237. if (err) {
  4238. dev_err(dev, "ice_init_link_events failed: %d\n", err);
  4239. return err;
  4240. }
  4241. /* not a fatal error if this fails */
  4242. err = ice_init_nvm_phy_type(pf->hw.port_info);
  4243. if (err)
  4244. dev_err(dev, "ice_init_nvm_phy_type failed: %d\n", err);
  4245. /* not a fatal error if this fails */
  4246. err = ice_update_link_info(pf->hw.port_info);
  4247. if (err)
  4248. dev_err(dev, "ice_update_link_info failed: %d\n", err);
  4249. ice_init_link_dflt_override(pf->hw.port_info);
  4250. ice_check_link_cfg_err(pf,
  4251. pf->hw.port_info->phy.link_info.link_cfg_err);
  4252. /* if media available, initialize PHY settings */
  4253. if (pf->hw.port_info->phy.link_info.link_info &
  4254. ICE_AQ_MEDIA_AVAILABLE) {
  4255. /* not a fatal error if this fails */
  4256. err = ice_init_phy_user_cfg(pf->hw.port_info);
  4257. if (err)
  4258. dev_err(dev, "ice_init_phy_user_cfg failed: %d\n", err);
  4259. if (!test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags)) {
  4260. struct ice_vsi *vsi = ice_get_main_vsi(pf);
  4261. if (vsi)
  4262. ice_configure_phy(vsi);
  4263. }
  4264. } else {
  4265. set_bit(ICE_FLAG_NO_MEDIA, pf->flags);
  4266. }
  4267. return err;
  4268. }
  4269. static int ice_init_pf_sw(struct ice_pf *pf)
  4270. {
  4271. bool dvm = ice_is_dvm_ena(&pf->hw);
  4272. struct ice_vsi *vsi;
  4273. int err;
  4274. /* create switch struct for the switch element created by FW on boot */
  4275. pf->first_sw = kzalloc(sizeof(*pf->first_sw), GFP_KERNEL);
  4276. if (!pf->first_sw)
  4277. return -ENOMEM;
  4278. if (pf->hw.evb_veb)
  4279. pf->first_sw->bridge_mode = BRIDGE_MODE_VEB;
  4280. else
  4281. pf->first_sw->bridge_mode = BRIDGE_MODE_VEPA;
  4282. pf->first_sw->pf = pf;
  4283. /* record the sw_id available for later use */
  4284. pf->first_sw->sw_id = pf->hw.port_info->sw_id;
  4285. err = ice_aq_set_port_params(pf->hw.port_info, dvm, NULL);
  4286. if (err)
  4287. goto err_aq_set_port_params;
  4288. vsi = ice_pf_vsi_setup(pf, pf->hw.port_info);
  4289. if (!vsi) {
  4290. err = -ENOMEM;
  4291. goto err_pf_vsi_setup;
  4292. }
  4293. return 0;
  4294. err_pf_vsi_setup:
  4295. err_aq_set_port_params:
  4296. kfree(pf->first_sw);
  4297. return err;
  4298. }
  4299. static void ice_deinit_pf_sw(struct ice_pf *pf)
  4300. {
  4301. struct ice_vsi *vsi = ice_get_main_vsi(pf);
  4302. if (!vsi)
  4303. return;
  4304. ice_vsi_release(vsi);
  4305. kfree(pf->first_sw);
  4306. }
  4307. static int ice_alloc_vsis(struct ice_pf *pf)
  4308. {
  4309. struct device *dev = ice_pf_to_dev(pf);
  4310. pf->num_alloc_vsi = pf->hw.func_caps.guar_num_vsi;
  4311. if (!pf->num_alloc_vsi)
  4312. return -EIO;
  4313. if (pf->num_alloc_vsi > UDP_TUNNEL_NIC_MAX_SHARING_DEVICES) {
  4314. dev_warn(dev,
  4315. "limiting the VSI count due to UDP tunnel limitation %d > %d\n",
  4316. pf->num_alloc_vsi, UDP_TUNNEL_NIC_MAX_SHARING_DEVICES);
  4317. pf->num_alloc_vsi = UDP_TUNNEL_NIC_MAX_SHARING_DEVICES;
  4318. }
  4319. pf->vsi = devm_kcalloc(dev, pf->num_alloc_vsi, sizeof(*pf->vsi),
  4320. GFP_KERNEL);
  4321. if (!pf->vsi)
  4322. return -ENOMEM;
  4323. pf->vsi_stats = devm_kcalloc(dev, pf->num_alloc_vsi,
  4324. sizeof(*pf->vsi_stats), GFP_KERNEL);
  4325. if (!pf->vsi_stats) {
  4326. devm_kfree(dev, pf->vsi);
  4327. return -ENOMEM;
  4328. }
  4329. return 0;
  4330. }
  4331. static void ice_dealloc_vsis(struct ice_pf *pf)
  4332. {
  4333. devm_kfree(ice_pf_to_dev(pf), pf->vsi_stats);
  4334. pf->vsi_stats = NULL;
  4335. pf->num_alloc_vsi = 0;
  4336. devm_kfree(ice_pf_to_dev(pf), pf->vsi);
  4337. pf->vsi = NULL;
  4338. }
  4339. static int ice_init_devlink(struct ice_pf *pf)
  4340. {
  4341. int err;
  4342. err = ice_devlink_register_params(pf);
  4343. if (err)
  4344. return err;
  4345. ice_devlink_init_regions(pf);
  4346. ice_devlink_register(pf);
  4347. return 0;
  4348. }
  4349. static void ice_deinit_devlink(struct ice_pf *pf)
  4350. {
  4351. ice_devlink_unregister(pf);
  4352. ice_devlink_destroy_regions(pf);
  4353. ice_devlink_unregister_params(pf);
  4354. }
  4355. static int ice_init(struct ice_pf *pf)
  4356. {
  4357. int err;
  4358. err = ice_init_dev(pf);
  4359. if (err)
  4360. return err;
  4361. err = ice_alloc_vsis(pf);
  4362. if (err)
  4363. goto err_alloc_vsis;
  4364. err = ice_init_pf_sw(pf);
  4365. if (err)
  4366. goto err_init_pf_sw;
  4367. ice_init_wakeup(pf);
  4368. err = ice_init_link(pf);
  4369. if (err)
  4370. goto err_init_link;
  4371. err = ice_send_version(pf);
  4372. if (err)
  4373. goto err_init_link;
  4374. ice_verify_cacheline_size(pf);
  4375. if (ice_is_safe_mode(pf))
  4376. ice_set_safe_mode_vlan_cfg(pf);
  4377. else
  4378. /* print PCI link speed and width */
  4379. pcie_print_link_status(pf->pdev);
  4380. /* ready to go, so clear down state bit */
  4381. clear_bit(ICE_DOWN, pf->state);
  4382. clear_bit(ICE_SERVICE_DIS, pf->state);
  4383. /* since everything is good, start the service timer */
  4384. mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
  4385. return 0;
  4386. err_init_link:
  4387. ice_deinit_pf_sw(pf);
  4388. err_init_pf_sw:
  4389. ice_dealloc_vsis(pf);
  4390. err_alloc_vsis:
  4391. ice_deinit_dev(pf);
  4392. return err;
  4393. }
  4394. static void ice_deinit(struct ice_pf *pf)
  4395. {
  4396. set_bit(ICE_SERVICE_DIS, pf->state);
  4397. set_bit(ICE_DOWN, pf->state);
  4398. ice_deinit_pf_sw(pf);
  4399. ice_dealloc_vsis(pf);
  4400. ice_deinit_dev(pf);
  4401. }
  4402. /**
  4403. * ice_load - load pf by init hw and starting VSI
  4404. * @pf: pointer to the pf instance
  4405. *
  4406. * This function has to be called under devl_lock.
  4407. */
  4408. int ice_load(struct ice_pf *pf)
  4409. {
  4410. struct ice_vsi *vsi;
  4411. int err;
  4412. devl_assert_locked(priv_to_devlink(pf));
  4413. vsi = ice_get_main_vsi(pf);
  4414. /* init channel list */
  4415. INIT_LIST_HEAD(&vsi->ch_list);
  4416. err = ice_cfg_netdev(vsi);
  4417. if (err)
  4418. return err;
  4419. /* Setup DCB netlink interface */
  4420. ice_dcbnl_setup(vsi);
  4421. err = ice_init_mac_fltr(pf);
  4422. if (err)
  4423. goto err_init_mac_fltr;
  4424. err = ice_devlink_create_pf_port(pf);
  4425. if (err)
  4426. goto err_devlink_create_pf_port;
  4427. SET_NETDEV_DEVLINK_PORT(vsi->netdev, &pf->devlink_port);
  4428. err = ice_register_netdev(vsi);
  4429. if (err)
  4430. goto err_register_netdev;
  4431. err = ice_tc_indir_block_register(vsi);
  4432. if (err)
  4433. goto err_tc_indir_block_register;
  4434. ice_napi_add(vsi);
  4435. ice_init_features(pf);
  4436. err = ice_init_rdma(pf);
  4437. if (err)
  4438. goto err_init_rdma;
  4439. ice_service_task_restart(pf);
  4440. clear_bit(ICE_DOWN, pf->state);
  4441. return 0;
  4442. err_init_rdma:
  4443. ice_deinit_features(pf);
  4444. ice_tc_indir_block_unregister(vsi);
  4445. err_tc_indir_block_register:
  4446. ice_unregister_netdev(vsi);
  4447. err_register_netdev:
  4448. ice_devlink_destroy_pf_port(pf);
  4449. err_devlink_create_pf_port:
  4450. err_init_mac_fltr:
  4451. ice_decfg_netdev(vsi);
  4452. return err;
  4453. }
  4454. /**
  4455. * ice_unload - unload pf by stopping VSI and deinit hw
  4456. * @pf: pointer to the pf instance
  4457. *
  4458. * This function has to be called under devl_lock.
  4459. */
  4460. void ice_unload(struct ice_pf *pf)
  4461. {
  4462. struct ice_vsi *vsi = ice_get_main_vsi(pf);
  4463. devl_assert_locked(priv_to_devlink(pf));
  4464. ice_deinit_rdma(pf);
  4465. ice_deinit_features(pf);
  4466. ice_tc_indir_block_unregister(vsi);
  4467. ice_unregister_netdev(vsi);
  4468. ice_devlink_destroy_pf_port(pf);
  4469. ice_decfg_netdev(vsi);
  4470. }
  4471. /**
  4472. * ice_probe - Device initialization routine
  4473. * @pdev: PCI device information struct
  4474. * @ent: entry in ice_pci_tbl
  4475. *
  4476. * Returns 0 on success, negative on failure
  4477. */
  4478. static int
  4479. ice_probe(struct pci_dev *pdev, const struct pci_device_id __always_unused *ent)
  4480. {
  4481. struct device *dev = &pdev->dev;
  4482. struct ice_adapter *adapter;
  4483. struct ice_pf *pf;
  4484. struct ice_hw *hw;
  4485. int err;
  4486. if (pdev->is_virtfn) {
  4487. dev_err(dev, "can't probe a virtual function\n");
  4488. return -EINVAL;
  4489. }
  4490. /* when under a kdump kernel initiate a reset before enabling the
  4491. * device in order to clear out any pending DMA transactions. These
  4492. * transactions can cause some systems to machine check when doing
  4493. * the pcim_enable_device() below.
  4494. */
  4495. if (is_kdump_kernel()) {
  4496. pci_save_state(pdev);
  4497. pci_clear_master(pdev);
  4498. err = pcie_flr(pdev);
  4499. if (err)
  4500. return err;
  4501. pci_restore_state(pdev);
  4502. }
  4503. /* this driver uses devres, see
  4504. * Documentation/driver-api/driver-model/devres.rst
  4505. */
  4506. err = pcim_enable_device(pdev);
  4507. if (err)
  4508. return err;
  4509. err = pcim_iomap_regions(pdev, BIT(ICE_BAR0), dev_driver_string(dev));
  4510. if (err) {
  4511. dev_err(dev, "BAR0 I/O map error %d\n", err);
  4512. return err;
  4513. }
  4514. pf = ice_allocate_pf(dev);
  4515. if (!pf)
  4516. return -ENOMEM;
  4517. /* initialize Auxiliary index to invalid value */
  4518. pf->aux_idx = -1;
  4519. /* set up for high or low DMA */
  4520. err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
  4521. if (err) {
  4522. dev_err(dev, "DMA configuration failed: 0x%x\n", err);
  4523. return err;
  4524. }
  4525. pci_set_master(pdev);
  4526. adapter = ice_adapter_get(pdev);
  4527. if (IS_ERR(adapter))
  4528. return PTR_ERR(adapter);
  4529. pf->pdev = pdev;
  4530. pf->adapter = adapter;
  4531. pci_set_drvdata(pdev, pf);
  4532. set_bit(ICE_DOWN, pf->state);
  4533. /* Disable service task until DOWN bit is cleared */
  4534. set_bit(ICE_SERVICE_DIS, pf->state);
  4535. hw = &pf->hw;
  4536. hw->hw_addr = pcim_iomap_table(pdev)[ICE_BAR0];
  4537. pci_save_state(pdev);
  4538. hw->back = pf;
  4539. hw->port_info = NULL;
  4540. hw->vendor_id = pdev->vendor;
  4541. hw->device_id = pdev->device;
  4542. pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
  4543. hw->subsystem_vendor_id = pdev->subsystem_vendor;
  4544. hw->subsystem_device_id = pdev->subsystem_device;
  4545. hw->bus.device = PCI_SLOT(pdev->devfn);
  4546. hw->bus.func = PCI_FUNC(pdev->devfn);
  4547. ice_set_ctrlq_len(hw);
  4548. pf->msg_enable = netif_msg_init(debug, ICE_DFLT_NETIF_M);
  4549. #ifndef CONFIG_DYNAMIC_DEBUG
  4550. if (debug < -1)
  4551. hw->debug_mask = debug;
  4552. #endif
  4553. err = ice_init(pf);
  4554. if (err)
  4555. goto err_init;
  4556. devl_lock(priv_to_devlink(pf));
  4557. err = ice_load(pf);
  4558. if (err)
  4559. goto err_load;
  4560. err = ice_init_devlink(pf);
  4561. if (err)
  4562. goto err_init_devlink;
  4563. devl_unlock(priv_to_devlink(pf));
  4564. return 0;
  4565. err_init_devlink:
  4566. ice_unload(pf);
  4567. err_load:
  4568. devl_unlock(priv_to_devlink(pf));
  4569. ice_deinit(pf);
  4570. err_init:
  4571. ice_adapter_put(pdev);
  4572. return err;
  4573. }
  4574. /**
  4575. * ice_set_wake - enable or disable Wake on LAN
  4576. * @pf: pointer to the PF struct
  4577. *
  4578. * Simple helper for WoL control
  4579. */
  4580. static void ice_set_wake(struct ice_pf *pf)
  4581. {
  4582. struct ice_hw *hw = &pf->hw;
  4583. bool wol = pf->wol_ena;
  4584. /* clear wake state, otherwise new wake events won't fire */
  4585. wr32(hw, PFPM_WUS, U32_MAX);
  4586. /* enable / disable APM wake up, no RMW needed */
  4587. wr32(hw, PFPM_APM, wol ? PFPM_APM_APME_M : 0);
  4588. /* set magic packet filter enabled */
  4589. wr32(hw, PFPM_WUFC, wol ? PFPM_WUFC_MAG_M : 0);
  4590. }
  4591. /**
  4592. * ice_setup_mc_magic_wake - setup device to wake on multicast magic packet
  4593. * @pf: pointer to the PF struct
  4594. *
  4595. * Issue firmware command to enable multicast magic wake, making
  4596. * sure that any locally administered address (LAA) is used for
  4597. * wake, and that PF reset doesn't undo the LAA.
  4598. */
  4599. static void ice_setup_mc_magic_wake(struct ice_pf *pf)
  4600. {
  4601. struct device *dev = ice_pf_to_dev(pf);
  4602. struct ice_hw *hw = &pf->hw;
  4603. u8 mac_addr[ETH_ALEN];
  4604. struct ice_vsi *vsi;
  4605. int status;
  4606. u8 flags;
  4607. if (!pf->wol_ena)
  4608. return;
  4609. vsi = ice_get_main_vsi(pf);
  4610. if (!vsi)
  4611. return;
  4612. /* Get current MAC address in case it's an LAA */
  4613. if (vsi->netdev)
  4614. ether_addr_copy(mac_addr, vsi->netdev->dev_addr);
  4615. else
  4616. ether_addr_copy(mac_addr, vsi->port_info->mac.perm_addr);
  4617. flags = ICE_AQC_MAN_MAC_WR_MC_MAG_EN |
  4618. ICE_AQC_MAN_MAC_UPDATE_LAA_WOL |
  4619. ICE_AQC_MAN_MAC_WR_WOL_LAA_PFR_KEEP;
  4620. status = ice_aq_manage_mac_write(hw, mac_addr, flags, NULL);
  4621. if (status)
  4622. dev_err(dev, "Failed to enable Multicast Magic Packet wake, err %d aq_err %s\n",
  4623. status, ice_aq_str(hw->adminq.sq_last_status));
  4624. }
  4625. /**
  4626. * ice_remove - Device removal routine
  4627. * @pdev: PCI device information struct
  4628. */
  4629. static void ice_remove(struct pci_dev *pdev)
  4630. {
  4631. struct ice_pf *pf = pci_get_drvdata(pdev);
  4632. int i;
  4633. for (i = 0; i < ICE_MAX_RESET_WAIT; i++) {
  4634. if (!ice_is_reset_in_progress(pf->state))
  4635. break;
  4636. msleep(100);
  4637. }
  4638. if (test_bit(ICE_FLAG_SRIOV_ENA, pf->flags)) {
  4639. set_bit(ICE_VF_RESETS_DISABLED, pf->state);
  4640. ice_free_vfs(pf);
  4641. }
  4642. ice_hwmon_exit(pf);
  4643. ice_service_task_stop(pf);
  4644. ice_aq_cancel_waiting_tasks(pf);
  4645. set_bit(ICE_DOWN, pf->state);
  4646. if (!ice_is_safe_mode(pf))
  4647. ice_remove_arfs(pf);
  4648. devl_lock(priv_to_devlink(pf));
  4649. ice_dealloc_all_dynamic_ports(pf);
  4650. ice_deinit_devlink(pf);
  4651. ice_unload(pf);
  4652. devl_unlock(priv_to_devlink(pf));
  4653. ice_deinit(pf);
  4654. ice_vsi_release_all(pf);
  4655. ice_setup_mc_magic_wake(pf);
  4656. ice_set_wake(pf);
  4657. ice_adapter_put(pdev);
  4658. }
  4659. /**
  4660. * ice_shutdown - PCI callback for shutting down device
  4661. * @pdev: PCI device information struct
  4662. */
  4663. static void ice_shutdown(struct pci_dev *pdev)
  4664. {
  4665. struct ice_pf *pf = pci_get_drvdata(pdev);
  4666. ice_remove(pdev);
  4667. if (system_state == SYSTEM_POWER_OFF) {
  4668. pci_wake_from_d3(pdev, pf->wol_ena);
  4669. pci_set_power_state(pdev, PCI_D3hot);
  4670. }
  4671. }
  4672. /**
  4673. * ice_prepare_for_shutdown - prep for PCI shutdown
  4674. * @pf: board private structure
  4675. *
  4676. * Inform or close all dependent features in prep for PCI device shutdown
  4677. */
  4678. static void ice_prepare_for_shutdown(struct ice_pf *pf)
  4679. {
  4680. struct ice_hw *hw = &pf->hw;
  4681. u32 v;
  4682. /* Notify VFs of impending reset */
  4683. if (ice_check_sq_alive(hw, &hw->mailboxq))
  4684. ice_vc_notify_reset(pf);
  4685. dev_dbg(ice_pf_to_dev(pf), "Tearing down internal switch for shutdown\n");
  4686. /* disable the VSIs and their queues that are not already DOWN */
  4687. ice_pf_dis_all_vsi(pf, false);
  4688. ice_for_each_vsi(pf, v)
  4689. if (pf->vsi[v])
  4690. pf->vsi[v]->vsi_num = 0;
  4691. ice_shutdown_all_ctrlq(hw, true);
  4692. }
  4693. /**
  4694. * ice_reinit_interrupt_scheme - Reinitialize interrupt scheme
  4695. * @pf: board private structure to reinitialize
  4696. *
  4697. * This routine reinitialize interrupt scheme that was cleared during
  4698. * power management suspend callback.
  4699. *
  4700. * This should be called during resume routine to re-allocate the q_vectors
  4701. * and reacquire interrupts.
  4702. */
  4703. static int ice_reinit_interrupt_scheme(struct ice_pf *pf)
  4704. {
  4705. struct device *dev = ice_pf_to_dev(pf);
  4706. int ret, v;
  4707. /* Since we clear MSIX flag during suspend, we need to
  4708. * set it back during resume...
  4709. */
  4710. ret = ice_init_interrupt_scheme(pf);
  4711. if (ret) {
  4712. dev_err(dev, "Failed to re-initialize interrupt %d\n", ret);
  4713. return ret;
  4714. }
  4715. /* Remap vectors and rings, after successful re-init interrupts */
  4716. ice_for_each_vsi(pf, v) {
  4717. if (!pf->vsi[v])
  4718. continue;
  4719. ret = ice_vsi_alloc_q_vectors(pf->vsi[v]);
  4720. if (ret)
  4721. goto err_reinit;
  4722. ice_vsi_map_rings_to_vectors(pf->vsi[v]);
  4723. rtnl_lock();
  4724. ice_vsi_set_napi_queues(pf->vsi[v]);
  4725. rtnl_unlock();
  4726. }
  4727. ret = ice_req_irq_msix_misc(pf);
  4728. if (ret) {
  4729. dev_err(dev, "Setting up misc vector failed after device suspend %d\n",
  4730. ret);
  4731. goto err_reinit;
  4732. }
  4733. return 0;
  4734. err_reinit:
  4735. while (v--)
  4736. if (pf->vsi[v]) {
  4737. rtnl_lock();
  4738. ice_vsi_clear_napi_queues(pf->vsi[v]);
  4739. rtnl_unlock();
  4740. ice_vsi_free_q_vectors(pf->vsi[v]);
  4741. }
  4742. return ret;
  4743. }
  4744. /**
  4745. * ice_suspend
  4746. * @dev: generic device information structure
  4747. *
  4748. * Power Management callback to quiesce the device and prepare
  4749. * for D3 transition.
  4750. */
  4751. static int ice_suspend(struct device *dev)
  4752. {
  4753. struct pci_dev *pdev = to_pci_dev(dev);
  4754. struct ice_pf *pf;
  4755. int disabled, v;
  4756. pf = pci_get_drvdata(pdev);
  4757. if (!ice_pf_state_is_nominal(pf)) {
  4758. dev_err(dev, "Device is not ready, no need to suspend it\n");
  4759. return -EBUSY;
  4760. }
  4761. /* Stop watchdog tasks until resume completion.
  4762. * Even though it is most likely that the service task is
  4763. * disabled if the device is suspended or down, the service task's
  4764. * state is controlled by a different state bit, and we should
  4765. * store and honor whatever state that bit is in at this point.
  4766. */
  4767. disabled = ice_service_task_stop(pf);
  4768. ice_deinit_rdma(pf);
  4769. /* Already suspended?, then there is nothing to do */
  4770. if (test_and_set_bit(ICE_SUSPENDED, pf->state)) {
  4771. if (!disabled)
  4772. ice_service_task_restart(pf);
  4773. return 0;
  4774. }
  4775. if (test_bit(ICE_DOWN, pf->state) ||
  4776. ice_is_reset_in_progress(pf->state)) {
  4777. dev_err(dev, "can't suspend device in reset or already down\n");
  4778. if (!disabled)
  4779. ice_service_task_restart(pf);
  4780. return 0;
  4781. }
  4782. ice_setup_mc_magic_wake(pf);
  4783. ice_prepare_for_shutdown(pf);
  4784. ice_set_wake(pf);
  4785. /* Free vectors, clear the interrupt scheme and release IRQs
  4786. * for proper hibernation, especially with large number of CPUs.
  4787. * Otherwise hibernation might fail when mapping all the vectors back
  4788. * to CPU0.
  4789. */
  4790. ice_free_irq_msix_misc(pf);
  4791. ice_for_each_vsi(pf, v) {
  4792. if (!pf->vsi[v])
  4793. continue;
  4794. rtnl_lock();
  4795. ice_vsi_clear_napi_queues(pf->vsi[v]);
  4796. rtnl_unlock();
  4797. ice_vsi_free_q_vectors(pf->vsi[v]);
  4798. }
  4799. ice_clear_interrupt_scheme(pf);
  4800. pci_save_state(pdev);
  4801. pci_wake_from_d3(pdev, pf->wol_ena);
  4802. pci_set_power_state(pdev, PCI_D3hot);
  4803. return 0;
  4804. }
  4805. /**
  4806. * ice_resume - PM callback for waking up from D3
  4807. * @dev: generic device information structure
  4808. */
  4809. static int ice_resume(struct device *dev)
  4810. {
  4811. struct pci_dev *pdev = to_pci_dev(dev);
  4812. enum ice_reset_req reset_type;
  4813. struct ice_pf *pf;
  4814. struct ice_hw *hw;
  4815. int ret;
  4816. pci_set_power_state(pdev, PCI_D0);
  4817. pci_restore_state(pdev);
  4818. pci_save_state(pdev);
  4819. if (!pci_device_is_present(pdev))
  4820. return -ENODEV;
  4821. ret = pci_enable_device_mem(pdev);
  4822. if (ret) {
  4823. dev_err(dev, "Cannot enable device after suspend\n");
  4824. return ret;
  4825. }
  4826. pf = pci_get_drvdata(pdev);
  4827. hw = &pf->hw;
  4828. pf->wakeup_reason = rd32(hw, PFPM_WUS);
  4829. ice_print_wake_reason(pf);
  4830. /* We cleared the interrupt scheme when we suspended, so we need to
  4831. * restore it now to resume device functionality.
  4832. */
  4833. ret = ice_reinit_interrupt_scheme(pf);
  4834. if (ret)
  4835. dev_err(dev, "Cannot restore interrupt scheme: %d\n", ret);
  4836. ret = ice_init_rdma(pf);
  4837. if (ret)
  4838. dev_err(dev, "Reinitialize RDMA during resume failed: %d\n",
  4839. ret);
  4840. clear_bit(ICE_DOWN, pf->state);
  4841. /* Now perform PF reset and rebuild */
  4842. reset_type = ICE_RESET_PFR;
  4843. /* re-enable service task for reset, but allow reset to schedule it */
  4844. clear_bit(ICE_SERVICE_DIS, pf->state);
  4845. if (ice_schedule_reset(pf, reset_type))
  4846. dev_err(dev, "Reset during resume failed.\n");
  4847. clear_bit(ICE_SUSPENDED, pf->state);
  4848. ice_service_task_restart(pf);
  4849. /* Restart the service task */
  4850. mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
  4851. return 0;
  4852. }
  4853. /**
  4854. * ice_pci_err_detected - warning that PCI error has been detected
  4855. * @pdev: PCI device information struct
  4856. * @err: the type of PCI error
  4857. *
  4858. * Called to warn that something happened on the PCI bus and the error handling
  4859. * is in progress. Allows the driver to gracefully prepare/handle PCI errors.
  4860. */
  4861. static pci_ers_result_t
  4862. ice_pci_err_detected(struct pci_dev *pdev, pci_channel_state_t err)
  4863. {
  4864. struct ice_pf *pf = pci_get_drvdata(pdev);
  4865. if (!pf) {
  4866. dev_err(&pdev->dev, "%s: unrecoverable device error %d\n",
  4867. __func__, err);
  4868. return PCI_ERS_RESULT_DISCONNECT;
  4869. }
  4870. if (!test_bit(ICE_SUSPENDED, pf->state)) {
  4871. ice_service_task_stop(pf);
  4872. if (!test_bit(ICE_PREPARED_FOR_RESET, pf->state)) {
  4873. set_bit(ICE_PFR_REQ, pf->state);
  4874. ice_prepare_for_reset(pf, ICE_RESET_PFR);
  4875. }
  4876. }
  4877. return PCI_ERS_RESULT_NEED_RESET;
  4878. }
  4879. /**
  4880. * ice_pci_err_slot_reset - a PCI slot reset has just happened
  4881. * @pdev: PCI device information struct
  4882. *
  4883. * Called to determine if the driver can recover from the PCI slot reset by
  4884. * using a register read to determine if the device is recoverable.
  4885. */
  4886. static pci_ers_result_t ice_pci_err_slot_reset(struct pci_dev *pdev)
  4887. {
  4888. struct ice_pf *pf = pci_get_drvdata(pdev);
  4889. pci_ers_result_t result;
  4890. int err;
  4891. u32 reg;
  4892. err = pci_enable_device_mem(pdev);
  4893. if (err) {
  4894. dev_err(&pdev->dev, "Cannot re-enable PCI device after reset, error %d\n",
  4895. err);
  4896. result = PCI_ERS_RESULT_DISCONNECT;
  4897. } else {
  4898. pci_set_master(pdev);
  4899. pci_restore_state(pdev);
  4900. pci_save_state(pdev);
  4901. pci_wake_from_d3(pdev, false);
  4902. /* Check for life */
  4903. reg = rd32(&pf->hw, GLGEN_RTRIG);
  4904. if (!reg)
  4905. result = PCI_ERS_RESULT_RECOVERED;
  4906. else
  4907. result = PCI_ERS_RESULT_DISCONNECT;
  4908. }
  4909. return result;
  4910. }
  4911. /**
  4912. * ice_pci_err_resume - restart operations after PCI error recovery
  4913. * @pdev: PCI device information struct
  4914. *
  4915. * Called to allow the driver to bring things back up after PCI error and/or
  4916. * reset recovery have finished
  4917. */
  4918. static void ice_pci_err_resume(struct pci_dev *pdev)
  4919. {
  4920. struct ice_pf *pf = pci_get_drvdata(pdev);
  4921. if (!pf) {
  4922. dev_err(&pdev->dev, "%s failed, device is unrecoverable\n",
  4923. __func__);
  4924. return;
  4925. }
  4926. if (test_bit(ICE_SUSPENDED, pf->state)) {
  4927. dev_dbg(&pdev->dev, "%s failed to resume normal operations!\n",
  4928. __func__);
  4929. return;
  4930. }
  4931. ice_restore_all_vfs_msi_state(pf);
  4932. ice_do_reset(pf, ICE_RESET_PFR);
  4933. ice_service_task_restart(pf);
  4934. mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
  4935. }
  4936. /**
  4937. * ice_pci_err_reset_prepare - prepare device driver for PCI reset
  4938. * @pdev: PCI device information struct
  4939. */
  4940. static void ice_pci_err_reset_prepare(struct pci_dev *pdev)
  4941. {
  4942. struct ice_pf *pf = pci_get_drvdata(pdev);
  4943. if (!test_bit(ICE_SUSPENDED, pf->state)) {
  4944. ice_service_task_stop(pf);
  4945. if (!test_bit(ICE_PREPARED_FOR_RESET, pf->state)) {
  4946. set_bit(ICE_PFR_REQ, pf->state);
  4947. ice_prepare_for_reset(pf, ICE_RESET_PFR);
  4948. }
  4949. }
  4950. }
  4951. /**
  4952. * ice_pci_err_reset_done - PCI reset done, device driver reset can begin
  4953. * @pdev: PCI device information struct
  4954. */
  4955. static void ice_pci_err_reset_done(struct pci_dev *pdev)
  4956. {
  4957. ice_pci_err_resume(pdev);
  4958. }
  4959. /* ice_pci_tbl - PCI Device ID Table
  4960. *
  4961. * Wildcard entries (PCI_ANY_ID) should come last
  4962. * Last entry must be all 0s
  4963. *
  4964. * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
  4965. * Class, Class Mask, private data (not used) }
  4966. */
  4967. static const struct pci_device_id ice_pci_tbl[] = {
  4968. { PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_BACKPLANE) },
  4969. { PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_QSFP) },
  4970. { PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_SFP) },
  4971. { PCI_VDEVICE(INTEL, ICE_DEV_ID_E810_XXV_BACKPLANE) },
  4972. { PCI_VDEVICE(INTEL, ICE_DEV_ID_E810_XXV_QSFP) },
  4973. { PCI_VDEVICE(INTEL, ICE_DEV_ID_E810_XXV_SFP) },
  4974. { PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_BACKPLANE) },
  4975. { PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_QSFP) },
  4976. { PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_SFP) },
  4977. { PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_10G_BASE_T) },
  4978. { PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_SGMII) },
  4979. { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_BACKPLANE) },
  4980. { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_QSFP) },
  4981. { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_SFP) },
  4982. { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_10G_BASE_T) },
  4983. { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_SGMII) },
  4984. { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_BACKPLANE) },
  4985. { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_SFP) },
  4986. { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_10G_BASE_T) },
  4987. { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_SGMII) },
  4988. { PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_BACKPLANE) },
  4989. { PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_SFP) },
  4990. { PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_10G_BASE_T) },
  4991. { PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_1GBE) },
  4992. { PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_QSFP) },
  4993. { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822_SI_DFLT) },
  4994. { PCI_VDEVICE(INTEL, ICE_DEV_ID_E825C_BACKPLANE), },
  4995. { PCI_VDEVICE(INTEL, ICE_DEV_ID_E825C_QSFP), },
  4996. { PCI_VDEVICE(INTEL, ICE_DEV_ID_E825C_SFP), },
  4997. { PCI_VDEVICE(INTEL, ICE_DEV_ID_E825C_SGMII), },
  4998. { PCI_VDEVICE(INTEL, ICE_DEV_ID_E830CC_BACKPLANE) },
  4999. { PCI_VDEVICE(INTEL, ICE_DEV_ID_E830CC_QSFP56) },
  5000. { PCI_VDEVICE(INTEL, ICE_DEV_ID_E830CC_SFP) },
  5001. { PCI_VDEVICE(INTEL, ICE_DEV_ID_E830CC_SFP_DD) },
  5002. { PCI_VDEVICE(INTEL, ICE_DEV_ID_E830C_BACKPLANE), },
  5003. { PCI_VDEVICE(INTEL, ICE_DEV_ID_E830_XXV_BACKPLANE), },
  5004. { PCI_VDEVICE(INTEL, ICE_DEV_ID_E830C_QSFP), },
  5005. { PCI_VDEVICE(INTEL, ICE_DEV_ID_E830_XXV_QSFP), },
  5006. { PCI_VDEVICE(INTEL, ICE_DEV_ID_E830C_SFP), },
  5007. { PCI_VDEVICE(INTEL, ICE_DEV_ID_E830_XXV_SFP), },
  5008. /* required last entry */
  5009. {}
  5010. };
  5011. MODULE_DEVICE_TABLE(pci, ice_pci_tbl);
  5012. static DEFINE_SIMPLE_DEV_PM_OPS(ice_pm_ops, ice_suspend, ice_resume);
  5013. static const struct pci_error_handlers ice_pci_err_handler = {
  5014. .error_detected = ice_pci_err_detected,
  5015. .slot_reset = ice_pci_err_slot_reset,
  5016. .reset_prepare = ice_pci_err_reset_prepare,
  5017. .reset_done = ice_pci_err_reset_done,
  5018. .resume = ice_pci_err_resume
  5019. };
  5020. static struct pci_driver ice_driver = {
  5021. .name = KBUILD_MODNAME,
  5022. .id_table = ice_pci_tbl,
  5023. .probe = ice_probe,
  5024. .remove = ice_remove,
  5025. .driver.pm = pm_sleep_ptr(&ice_pm_ops),
  5026. .shutdown = ice_shutdown,
  5027. .sriov_configure = ice_sriov_configure,
  5028. .sriov_get_vf_total_msix = ice_sriov_get_vf_total_msix,
  5029. .sriov_set_msix_vec_count = ice_sriov_set_msix_vec_count,
  5030. .err_handler = &ice_pci_err_handler
  5031. };
  5032. /**
  5033. * ice_module_init - Driver registration routine
  5034. *
  5035. * ice_module_init is the first routine called when the driver is
  5036. * loaded. All it does is register with the PCI subsystem.
  5037. */
  5038. static int __init ice_module_init(void)
  5039. {
  5040. int status = -ENOMEM;
  5041. pr_info("%s\n", ice_driver_string);
  5042. pr_info("%s\n", ice_copyright);
  5043. ice_adv_lnk_speed_maps_init();
  5044. ice_wq = alloc_workqueue("%s", 0, 0, KBUILD_MODNAME);
  5045. if (!ice_wq) {
  5046. pr_err("Failed to create workqueue\n");
  5047. return status;
  5048. }
  5049. ice_lag_wq = alloc_ordered_workqueue("ice_lag_wq", 0);
  5050. if (!ice_lag_wq) {
  5051. pr_err("Failed to create LAG workqueue\n");
  5052. goto err_dest_wq;
  5053. }
  5054. ice_debugfs_init();
  5055. status = pci_register_driver(&ice_driver);
  5056. if (status) {
  5057. pr_err("failed to register PCI driver, err %d\n", status);
  5058. goto err_dest_lag_wq;
  5059. }
  5060. status = ice_sf_driver_register();
  5061. if (status) {
  5062. pr_err("Failed to register SF driver, err %d\n", status);
  5063. goto err_sf_driver;
  5064. }
  5065. return 0;
  5066. err_sf_driver:
  5067. pci_unregister_driver(&ice_driver);
  5068. err_dest_lag_wq:
  5069. destroy_workqueue(ice_lag_wq);
  5070. ice_debugfs_exit();
  5071. err_dest_wq:
  5072. destroy_workqueue(ice_wq);
  5073. return status;
  5074. }
  5075. module_init(ice_module_init);
  5076. /**
  5077. * ice_module_exit - Driver exit cleanup routine
  5078. *
  5079. * ice_module_exit is called just before the driver is removed
  5080. * from memory.
  5081. */
  5082. static void __exit ice_module_exit(void)
  5083. {
  5084. ice_sf_driver_unregister();
  5085. pci_unregister_driver(&ice_driver);
  5086. ice_debugfs_exit();
  5087. destroy_workqueue(ice_wq);
  5088. destroy_workqueue(ice_lag_wq);
  5089. pr_info("module unloaded\n");
  5090. }
  5091. module_exit(ice_module_exit);
  5092. /**
  5093. * ice_set_mac_address - NDO callback to set MAC address
  5094. * @netdev: network interface device structure
  5095. * @pi: pointer to an address structure
  5096. *
  5097. * Returns 0 on success, negative on failure
  5098. */
  5099. static int ice_set_mac_address(struct net_device *netdev, void *pi)
  5100. {
  5101. struct ice_netdev_priv *np = netdev_priv(netdev);
  5102. struct ice_vsi *vsi = np->vsi;
  5103. struct ice_pf *pf = vsi->back;
  5104. struct ice_hw *hw = &pf->hw;
  5105. struct sockaddr *addr = pi;
  5106. u8 old_mac[ETH_ALEN];
  5107. u8 flags = 0;
  5108. u8 *mac;
  5109. int err;
  5110. mac = (u8 *)addr->sa_data;
  5111. if (!is_valid_ether_addr(mac))
  5112. return -EADDRNOTAVAIL;
  5113. if (test_bit(ICE_DOWN, pf->state) ||
  5114. ice_is_reset_in_progress(pf->state)) {
  5115. netdev_err(netdev, "can't set mac %pM. device not ready\n",
  5116. mac);
  5117. return -EBUSY;
  5118. }
  5119. if (ice_chnl_dmac_fltr_cnt(pf)) {
  5120. netdev_err(netdev, "can't set mac %pM. Device has tc-flower filters, delete all of them and try again\n",
  5121. mac);
  5122. return -EAGAIN;
  5123. }
  5124. netif_addr_lock_bh(netdev);
  5125. ether_addr_copy(old_mac, netdev->dev_addr);
  5126. /* change the netdev's MAC address */
  5127. eth_hw_addr_set(netdev, mac);
  5128. netif_addr_unlock_bh(netdev);
  5129. /* Clean up old MAC filter. Not an error if old filter doesn't exist */
  5130. err = ice_fltr_remove_mac(vsi, old_mac, ICE_FWD_TO_VSI);
  5131. if (err && err != -ENOENT) {
  5132. err = -EADDRNOTAVAIL;
  5133. goto err_update_filters;
  5134. }
  5135. /* Add filter for new MAC. If filter exists, return success */
  5136. err = ice_fltr_add_mac(vsi, mac, ICE_FWD_TO_VSI);
  5137. if (err == -EEXIST) {
  5138. /* Although this MAC filter is already present in hardware it's
  5139. * possible in some cases (e.g. bonding) that dev_addr was
  5140. * modified outside of the driver and needs to be restored back
  5141. * to this value.
  5142. */
  5143. netdev_dbg(netdev, "filter for MAC %pM already exists\n", mac);
  5144. return 0;
  5145. } else if (err) {
  5146. /* error if the new filter addition failed */
  5147. err = -EADDRNOTAVAIL;
  5148. }
  5149. err_update_filters:
  5150. if (err) {
  5151. netdev_err(netdev, "can't set MAC %pM. filter update failed\n",
  5152. mac);
  5153. netif_addr_lock_bh(netdev);
  5154. eth_hw_addr_set(netdev, old_mac);
  5155. netif_addr_unlock_bh(netdev);
  5156. return err;
  5157. }
  5158. netdev_dbg(vsi->netdev, "updated MAC address to %pM\n",
  5159. netdev->dev_addr);
  5160. /* write new MAC address to the firmware */
  5161. flags = ICE_AQC_MAN_MAC_UPDATE_LAA_WOL;
  5162. err = ice_aq_manage_mac_write(hw, mac, flags, NULL);
  5163. if (err) {
  5164. netdev_err(netdev, "can't set MAC %pM. write to firmware failed error %d\n",
  5165. mac, err);
  5166. }
  5167. return 0;
  5168. }
  5169. /**
  5170. * ice_set_rx_mode - NDO callback to set the netdev filters
  5171. * @netdev: network interface device structure
  5172. */
  5173. static void ice_set_rx_mode(struct net_device *netdev)
  5174. {
  5175. struct ice_netdev_priv *np = netdev_priv(netdev);
  5176. struct ice_vsi *vsi = np->vsi;
  5177. if (!vsi || ice_is_switchdev_running(vsi->back))
  5178. return;
  5179. /* Set the flags to synchronize filters
  5180. * ndo_set_rx_mode may be triggered even without a change in netdev
  5181. * flags
  5182. */
  5183. set_bit(ICE_VSI_UMAC_FLTR_CHANGED, vsi->state);
  5184. set_bit(ICE_VSI_MMAC_FLTR_CHANGED, vsi->state);
  5185. set_bit(ICE_FLAG_FLTR_SYNC, vsi->back->flags);
  5186. /* schedule our worker thread which will take care of
  5187. * applying the new filter changes
  5188. */
  5189. ice_service_task_schedule(vsi->back);
  5190. }
  5191. /**
  5192. * ice_set_tx_maxrate - NDO callback to set the maximum per-queue bitrate
  5193. * @netdev: network interface device structure
  5194. * @queue_index: Queue ID
  5195. * @maxrate: maximum bandwidth in Mbps
  5196. */
  5197. static int
  5198. ice_set_tx_maxrate(struct net_device *netdev, int queue_index, u32 maxrate)
  5199. {
  5200. struct ice_netdev_priv *np = netdev_priv(netdev);
  5201. struct ice_vsi *vsi = np->vsi;
  5202. u16 q_handle;
  5203. int status;
  5204. u8 tc;
  5205. /* Validate maxrate requested is within permitted range */
  5206. if (maxrate && (maxrate > (ICE_SCHED_MAX_BW / 1000))) {
  5207. netdev_err(netdev, "Invalid max rate %d specified for the queue %d\n",
  5208. maxrate, queue_index);
  5209. return -EINVAL;
  5210. }
  5211. q_handle = vsi->tx_rings[queue_index]->q_handle;
  5212. tc = ice_dcb_get_tc(vsi, queue_index);
  5213. vsi = ice_locate_vsi_using_queue(vsi, queue_index);
  5214. if (!vsi) {
  5215. netdev_err(netdev, "Invalid VSI for given queue %d\n",
  5216. queue_index);
  5217. return -EINVAL;
  5218. }
  5219. /* Set BW back to default, when user set maxrate to 0 */
  5220. if (!maxrate)
  5221. status = ice_cfg_q_bw_dflt_lmt(vsi->port_info, vsi->idx, tc,
  5222. q_handle, ICE_MAX_BW);
  5223. else
  5224. status = ice_cfg_q_bw_lmt(vsi->port_info, vsi->idx, tc,
  5225. q_handle, ICE_MAX_BW, maxrate * 1000);
  5226. if (status)
  5227. netdev_err(netdev, "Unable to set Tx max rate, error %d\n",
  5228. status);
  5229. return status;
  5230. }
  5231. /**
  5232. * ice_fdb_add - add an entry to the hardware database
  5233. * @ndm: the input from the stack
  5234. * @tb: pointer to array of nladdr (unused)
  5235. * @dev: the net device pointer
  5236. * @addr: the MAC address entry being added
  5237. * @vid: VLAN ID
  5238. * @flags: instructions from stack about fdb operation
  5239. * @extack: netlink extended ack
  5240. */
  5241. static int
  5242. ice_fdb_add(struct ndmsg *ndm, struct nlattr __always_unused *tb[],
  5243. struct net_device *dev, const unsigned char *addr, u16 vid,
  5244. u16 flags, struct netlink_ext_ack __always_unused *extack)
  5245. {
  5246. int err;
  5247. if (vid) {
  5248. netdev_err(dev, "VLANs aren't supported yet for dev_uc|mc_add()\n");
  5249. return -EINVAL;
  5250. }
  5251. if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
  5252. netdev_err(dev, "FDB only supports static addresses\n");
  5253. return -EINVAL;
  5254. }
  5255. if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
  5256. err = dev_uc_add_excl(dev, addr);
  5257. else if (is_multicast_ether_addr(addr))
  5258. err = dev_mc_add_excl(dev, addr);
  5259. else
  5260. err = -EINVAL;
  5261. /* Only return duplicate errors if NLM_F_EXCL is set */
  5262. if (err == -EEXIST && !(flags & NLM_F_EXCL))
  5263. err = 0;
  5264. return err;
  5265. }
  5266. /**
  5267. * ice_fdb_del - delete an entry from the hardware database
  5268. * @ndm: the input from the stack
  5269. * @tb: pointer to array of nladdr (unused)
  5270. * @dev: the net device pointer
  5271. * @addr: the MAC address entry being added
  5272. * @vid: VLAN ID
  5273. * @extack: netlink extended ack
  5274. */
  5275. static int
  5276. ice_fdb_del(struct ndmsg *ndm, __always_unused struct nlattr *tb[],
  5277. struct net_device *dev, const unsigned char *addr,
  5278. __always_unused u16 vid, struct netlink_ext_ack *extack)
  5279. {
  5280. int err;
  5281. if (ndm->ndm_state & NUD_PERMANENT) {
  5282. netdev_err(dev, "FDB only supports static addresses\n");
  5283. return -EINVAL;
  5284. }
  5285. if (is_unicast_ether_addr(addr))
  5286. err = dev_uc_del(dev, addr);
  5287. else if (is_multicast_ether_addr(addr))
  5288. err = dev_mc_del(dev, addr);
  5289. else
  5290. err = -EINVAL;
  5291. return err;
  5292. }
  5293. #define NETIF_VLAN_OFFLOAD_FEATURES (NETIF_F_HW_VLAN_CTAG_RX | \
  5294. NETIF_F_HW_VLAN_CTAG_TX | \
  5295. NETIF_F_HW_VLAN_STAG_RX | \
  5296. NETIF_F_HW_VLAN_STAG_TX)
  5297. #define NETIF_VLAN_STRIPPING_FEATURES (NETIF_F_HW_VLAN_CTAG_RX | \
  5298. NETIF_F_HW_VLAN_STAG_RX)
  5299. #define NETIF_VLAN_FILTERING_FEATURES (NETIF_F_HW_VLAN_CTAG_FILTER | \
  5300. NETIF_F_HW_VLAN_STAG_FILTER)
  5301. /**
  5302. * ice_fix_features - fix the netdev features flags based on device limitations
  5303. * @netdev: ptr to the netdev that flags are being fixed on
  5304. * @features: features that need to be checked and possibly fixed
  5305. *
  5306. * Make sure any fixups are made to features in this callback. This enables the
  5307. * driver to not have to check unsupported configurations throughout the driver
  5308. * because that's the responsiblity of this callback.
  5309. *
  5310. * Single VLAN Mode (SVM) Supported Features:
  5311. * NETIF_F_HW_VLAN_CTAG_FILTER
  5312. * NETIF_F_HW_VLAN_CTAG_RX
  5313. * NETIF_F_HW_VLAN_CTAG_TX
  5314. *
  5315. * Double VLAN Mode (DVM) Supported Features:
  5316. * NETIF_F_HW_VLAN_CTAG_FILTER
  5317. * NETIF_F_HW_VLAN_CTAG_RX
  5318. * NETIF_F_HW_VLAN_CTAG_TX
  5319. *
  5320. * NETIF_F_HW_VLAN_STAG_FILTER
  5321. * NETIF_HW_VLAN_STAG_RX
  5322. * NETIF_HW_VLAN_STAG_TX
  5323. *
  5324. * Features that need fixing:
  5325. * Cannot simultaneously enable CTAG and STAG stripping and/or insertion.
  5326. * These are mutually exlusive as the VSI context cannot support multiple
  5327. * VLAN ethertypes simultaneously for stripping and/or insertion. If this
  5328. * is not done, then default to clearing the requested STAG offload
  5329. * settings.
  5330. *
  5331. * All supported filtering has to be enabled or disabled together. For
  5332. * example, in DVM, CTAG and STAG filtering have to be enabled and disabled
  5333. * together. If this is not done, then default to VLAN filtering disabled.
  5334. * These are mutually exclusive as there is currently no way to
  5335. * enable/disable VLAN filtering based on VLAN ethertype when using VLAN
  5336. * prune rules.
  5337. */
  5338. static netdev_features_t
  5339. ice_fix_features(struct net_device *netdev, netdev_features_t features)
  5340. {
  5341. struct ice_netdev_priv *np = netdev_priv(netdev);
  5342. netdev_features_t req_vlan_fltr, cur_vlan_fltr;
  5343. bool cur_ctag, cur_stag, req_ctag, req_stag;
  5344. cur_vlan_fltr = netdev->features & NETIF_VLAN_FILTERING_FEATURES;
  5345. cur_ctag = cur_vlan_fltr & NETIF_F_HW_VLAN_CTAG_FILTER;
  5346. cur_stag = cur_vlan_fltr & NETIF_F_HW_VLAN_STAG_FILTER;
  5347. req_vlan_fltr = features & NETIF_VLAN_FILTERING_FEATURES;
  5348. req_ctag = req_vlan_fltr & NETIF_F_HW_VLAN_CTAG_FILTER;
  5349. req_stag = req_vlan_fltr & NETIF_F_HW_VLAN_STAG_FILTER;
  5350. if (req_vlan_fltr != cur_vlan_fltr) {
  5351. if (ice_is_dvm_ena(&np->vsi->back->hw)) {
  5352. if (req_ctag && req_stag) {
  5353. features |= NETIF_VLAN_FILTERING_FEATURES;
  5354. } else if (!req_ctag && !req_stag) {
  5355. features &= ~NETIF_VLAN_FILTERING_FEATURES;
  5356. } else if ((!cur_ctag && req_ctag && !cur_stag) ||
  5357. (!cur_stag && req_stag && !cur_ctag)) {
  5358. features |= NETIF_VLAN_FILTERING_FEATURES;
  5359. netdev_warn(netdev, "802.1Q and 802.1ad VLAN filtering must be either both on or both off. VLAN filtering has been enabled for both types.\n");
  5360. } else if ((cur_ctag && !req_ctag && cur_stag) ||
  5361. (cur_stag && !req_stag && cur_ctag)) {
  5362. features &= ~NETIF_VLAN_FILTERING_FEATURES;
  5363. netdev_warn(netdev, "802.1Q and 802.1ad VLAN filtering must be either both on or both off. VLAN filtering has been disabled for both types.\n");
  5364. }
  5365. } else {
  5366. if (req_vlan_fltr & NETIF_F_HW_VLAN_STAG_FILTER)
  5367. netdev_warn(netdev, "cannot support requested 802.1ad filtering setting in SVM mode\n");
  5368. if (req_vlan_fltr & NETIF_F_HW_VLAN_CTAG_FILTER)
  5369. features |= NETIF_F_HW_VLAN_CTAG_FILTER;
  5370. }
  5371. }
  5372. if ((features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX)) &&
  5373. (features & (NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX))) {
  5374. netdev_warn(netdev, "cannot support CTAG and STAG VLAN stripping and/or insertion simultaneously since CTAG and STAG offloads are mutually exclusive, clearing STAG offload settings\n");
  5375. features &= ~(NETIF_F_HW_VLAN_STAG_RX |
  5376. NETIF_F_HW_VLAN_STAG_TX);
  5377. }
  5378. if (!(netdev->features & NETIF_F_RXFCS) &&
  5379. (features & NETIF_F_RXFCS) &&
  5380. (features & NETIF_VLAN_STRIPPING_FEATURES) &&
  5381. !ice_vsi_has_non_zero_vlans(np->vsi)) {
  5382. netdev_warn(netdev, "Disabling VLAN stripping as FCS/CRC stripping is also disabled and there is no VLAN configured\n");
  5383. features &= ~NETIF_VLAN_STRIPPING_FEATURES;
  5384. }
  5385. return features;
  5386. }
  5387. /**
  5388. * ice_set_rx_rings_vlan_proto - update rings with new stripped VLAN proto
  5389. * @vsi: PF's VSI
  5390. * @vlan_ethertype: VLAN ethertype (802.1Q or 802.1ad) in network byte order
  5391. *
  5392. * Store current stripped VLAN proto in ring packet context,
  5393. * so it can be accessed more efficiently by packet processing code.
  5394. */
  5395. static void
  5396. ice_set_rx_rings_vlan_proto(struct ice_vsi *vsi, __be16 vlan_ethertype)
  5397. {
  5398. u16 i;
  5399. ice_for_each_alloc_rxq(vsi, i)
  5400. vsi->rx_rings[i]->pkt_ctx.vlan_proto = vlan_ethertype;
  5401. }
  5402. /**
  5403. * ice_set_vlan_offload_features - set VLAN offload features for the PF VSI
  5404. * @vsi: PF's VSI
  5405. * @features: features used to determine VLAN offload settings
  5406. *
  5407. * First, determine the vlan_ethertype based on the VLAN offload bits in
  5408. * features. Then determine if stripping and insertion should be enabled or
  5409. * disabled. Finally enable or disable VLAN stripping and insertion.
  5410. */
  5411. static int
  5412. ice_set_vlan_offload_features(struct ice_vsi *vsi, netdev_features_t features)
  5413. {
  5414. bool enable_stripping = true, enable_insertion = true;
  5415. struct ice_vsi_vlan_ops *vlan_ops;
  5416. int strip_err = 0, insert_err = 0;
  5417. u16 vlan_ethertype = 0;
  5418. vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
  5419. if (features & (NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX))
  5420. vlan_ethertype = ETH_P_8021AD;
  5421. else if (features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX))
  5422. vlan_ethertype = ETH_P_8021Q;
  5423. if (!(features & (NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_CTAG_RX)))
  5424. enable_stripping = false;
  5425. if (!(features & (NETIF_F_HW_VLAN_STAG_TX | NETIF_F_HW_VLAN_CTAG_TX)))
  5426. enable_insertion = false;
  5427. if (enable_stripping)
  5428. strip_err = vlan_ops->ena_stripping(vsi, vlan_ethertype);
  5429. else
  5430. strip_err = vlan_ops->dis_stripping(vsi);
  5431. if (enable_insertion)
  5432. insert_err = vlan_ops->ena_insertion(vsi, vlan_ethertype);
  5433. else
  5434. insert_err = vlan_ops->dis_insertion(vsi);
  5435. if (strip_err || insert_err)
  5436. return -EIO;
  5437. ice_set_rx_rings_vlan_proto(vsi, enable_stripping ?
  5438. htons(vlan_ethertype) : 0);
  5439. return 0;
  5440. }
  5441. /**
  5442. * ice_set_vlan_filtering_features - set VLAN filtering features for the PF VSI
  5443. * @vsi: PF's VSI
  5444. * @features: features used to determine VLAN filtering settings
  5445. *
  5446. * Enable or disable Rx VLAN filtering based on the VLAN filtering bits in the
  5447. * features.
  5448. */
  5449. static int
  5450. ice_set_vlan_filtering_features(struct ice_vsi *vsi, netdev_features_t features)
  5451. {
  5452. struct ice_vsi_vlan_ops *vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
  5453. int err = 0;
  5454. /* support Single VLAN Mode (SVM) and Double VLAN Mode (DVM) by checking
  5455. * if either bit is set. In switchdev mode Rx filtering should never be
  5456. * enabled.
  5457. */
  5458. if ((features &
  5459. (NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_STAG_FILTER)) &&
  5460. !ice_is_eswitch_mode_switchdev(vsi->back))
  5461. err = vlan_ops->ena_rx_filtering(vsi);
  5462. else
  5463. err = vlan_ops->dis_rx_filtering(vsi);
  5464. return err;
  5465. }
  5466. /**
  5467. * ice_set_vlan_features - set VLAN settings based on suggested feature set
  5468. * @netdev: ptr to the netdev being adjusted
  5469. * @features: the feature set that the stack is suggesting
  5470. *
  5471. * Only update VLAN settings if the requested_vlan_features are different than
  5472. * the current_vlan_features.
  5473. */
  5474. static int
  5475. ice_set_vlan_features(struct net_device *netdev, netdev_features_t features)
  5476. {
  5477. netdev_features_t current_vlan_features, requested_vlan_features;
  5478. struct ice_netdev_priv *np = netdev_priv(netdev);
  5479. struct ice_vsi *vsi = np->vsi;
  5480. int err;
  5481. current_vlan_features = netdev->features & NETIF_VLAN_OFFLOAD_FEATURES;
  5482. requested_vlan_features = features & NETIF_VLAN_OFFLOAD_FEATURES;
  5483. if (current_vlan_features ^ requested_vlan_features) {
  5484. if ((features & NETIF_F_RXFCS) &&
  5485. (features & NETIF_VLAN_STRIPPING_FEATURES)) {
  5486. dev_err(ice_pf_to_dev(vsi->back),
  5487. "To enable VLAN stripping, you must first enable FCS/CRC stripping\n");
  5488. return -EIO;
  5489. }
  5490. err = ice_set_vlan_offload_features(vsi, features);
  5491. if (err)
  5492. return err;
  5493. }
  5494. current_vlan_features = netdev->features &
  5495. NETIF_VLAN_FILTERING_FEATURES;
  5496. requested_vlan_features = features & NETIF_VLAN_FILTERING_FEATURES;
  5497. if (current_vlan_features ^ requested_vlan_features) {
  5498. err = ice_set_vlan_filtering_features(vsi, features);
  5499. if (err)
  5500. return err;
  5501. }
  5502. return 0;
  5503. }
  5504. /**
  5505. * ice_set_loopback - turn on/off loopback mode on underlying PF
  5506. * @vsi: ptr to VSI
  5507. * @ena: flag to indicate the on/off setting
  5508. */
  5509. static int ice_set_loopback(struct ice_vsi *vsi, bool ena)
  5510. {
  5511. bool if_running = netif_running(vsi->netdev);
  5512. int ret;
  5513. if (if_running && !test_and_set_bit(ICE_VSI_DOWN, vsi->state)) {
  5514. ret = ice_down(vsi);
  5515. if (ret) {
  5516. netdev_err(vsi->netdev, "Preparing device to toggle loopback failed\n");
  5517. return ret;
  5518. }
  5519. }
  5520. ret = ice_aq_set_mac_loopback(&vsi->back->hw, ena, NULL);
  5521. if (ret)
  5522. netdev_err(vsi->netdev, "Failed to toggle loopback state\n");
  5523. if (if_running)
  5524. ret = ice_up(vsi);
  5525. return ret;
  5526. }
  5527. /**
  5528. * ice_set_features - set the netdev feature flags
  5529. * @netdev: ptr to the netdev being adjusted
  5530. * @features: the feature set that the stack is suggesting
  5531. */
  5532. static int
  5533. ice_set_features(struct net_device *netdev, netdev_features_t features)
  5534. {
  5535. netdev_features_t changed = netdev->features ^ features;
  5536. struct ice_netdev_priv *np = netdev_priv(netdev);
  5537. struct ice_vsi *vsi = np->vsi;
  5538. struct ice_pf *pf = vsi->back;
  5539. int ret = 0;
  5540. /* Don't set any netdev advanced features with device in Safe Mode */
  5541. if (ice_is_safe_mode(pf)) {
  5542. dev_err(ice_pf_to_dev(pf),
  5543. "Device is in Safe Mode - not enabling advanced netdev features\n");
  5544. return ret;
  5545. }
  5546. /* Do not change setting during reset */
  5547. if (ice_is_reset_in_progress(pf->state)) {
  5548. dev_err(ice_pf_to_dev(pf),
  5549. "Device is resetting, changing advanced netdev features temporarily unavailable.\n");
  5550. return -EBUSY;
  5551. }
  5552. /* Multiple features can be changed in one call so keep features in
  5553. * separate if/else statements to guarantee each feature is checked
  5554. */
  5555. if (changed & NETIF_F_RXHASH)
  5556. ice_vsi_manage_rss_lut(vsi, !!(features & NETIF_F_RXHASH));
  5557. ret = ice_set_vlan_features(netdev, features);
  5558. if (ret)
  5559. return ret;
  5560. /* Turn on receive of FCS aka CRC, and after setting this
  5561. * flag the packet data will have the 4 byte CRC appended
  5562. */
  5563. if (changed & NETIF_F_RXFCS) {
  5564. if ((features & NETIF_F_RXFCS) &&
  5565. (features & NETIF_VLAN_STRIPPING_FEATURES)) {
  5566. dev_err(ice_pf_to_dev(vsi->back),
  5567. "To disable FCS/CRC stripping, you must first disable VLAN stripping\n");
  5568. return -EIO;
  5569. }
  5570. ice_vsi_cfg_crc_strip(vsi, !!(features & NETIF_F_RXFCS));
  5571. ret = ice_down_up(vsi);
  5572. if (ret)
  5573. return ret;
  5574. }
  5575. if (changed & NETIF_F_NTUPLE) {
  5576. bool ena = !!(features & NETIF_F_NTUPLE);
  5577. ice_vsi_manage_fdir(vsi, ena);
  5578. ena ? ice_init_arfs(vsi) : ice_clear_arfs(vsi);
  5579. }
  5580. /* don't turn off hw_tc_offload when ADQ is already enabled */
  5581. if (!(features & NETIF_F_HW_TC) && ice_is_adq_active(pf)) {
  5582. dev_err(ice_pf_to_dev(pf), "ADQ is active, can't turn hw_tc_offload off\n");
  5583. return -EACCES;
  5584. }
  5585. if (changed & NETIF_F_HW_TC) {
  5586. bool ena = !!(features & NETIF_F_HW_TC);
  5587. ena ? set_bit(ICE_FLAG_CLS_FLOWER, pf->flags) :
  5588. clear_bit(ICE_FLAG_CLS_FLOWER, pf->flags);
  5589. }
  5590. if (changed & NETIF_F_LOOPBACK)
  5591. ret = ice_set_loopback(vsi, !!(features & NETIF_F_LOOPBACK));
  5592. return ret;
  5593. }
  5594. /**
  5595. * ice_vsi_vlan_setup - Setup VLAN offload properties on a PF VSI
  5596. * @vsi: VSI to setup VLAN properties for
  5597. */
  5598. static int ice_vsi_vlan_setup(struct ice_vsi *vsi)
  5599. {
  5600. int err;
  5601. err = ice_set_vlan_offload_features(vsi, vsi->netdev->features);
  5602. if (err)
  5603. return err;
  5604. err = ice_set_vlan_filtering_features(vsi, vsi->netdev->features);
  5605. if (err)
  5606. return err;
  5607. return ice_vsi_add_vlan_zero(vsi);
  5608. }
  5609. /**
  5610. * ice_vsi_cfg_lan - Setup the VSI lan related config
  5611. * @vsi: the VSI being configured
  5612. *
  5613. * Return 0 on success and negative value on error
  5614. */
  5615. int ice_vsi_cfg_lan(struct ice_vsi *vsi)
  5616. {
  5617. int err;
  5618. if (vsi->netdev && vsi->type == ICE_VSI_PF) {
  5619. ice_set_rx_mode(vsi->netdev);
  5620. err = ice_vsi_vlan_setup(vsi);
  5621. if (err)
  5622. return err;
  5623. }
  5624. ice_vsi_cfg_dcb_rings(vsi);
  5625. err = ice_vsi_cfg_lan_txqs(vsi);
  5626. if (!err && ice_is_xdp_ena_vsi(vsi))
  5627. err = ice_vsi_cfg_xdp_txqs(vsi);
  5628. if (!err)
  5629. err = ice_vsi_cfg_rxqs(vsi);
  5630. return err;
  5631. }
  5632. /* THEORY OF MODERATION:
  5633. * The ice driver hardware works differently than the hardware that DIMLIB was
  5634. * originally made for. ice hardware doesn't have packet count limits that
  5635. * can trigger an interrupt, but it *does* have interrupt rate limit support,
  5636. * which is hard-coded to a limit of 250,000 ints/second.
  5637. * If not using dynamic moderation, the INTRL value can be modified
  5638. * by ethtool rx-usecs-high.
  5639. */
  5640. struct ice_dim {
  5641. /* the throttle rate for interrupts, basically worst case delay before
  5642. * an initial interrupt fires, value is stored in microseconds.
  5643. */
  5644. u16 itr;
  5645. };
  5646. /* Make a different profile for Rx that doesn't allow quite so aggressive
  5647. * moderation at the high end (it maxes out at 126us or about 8k interrupts a
  5648. * second.
  5649. */
  5650. static const struct ice_dim rx_profile[] = {
  5651. {2}, /* 500,000 ints/s, capped at 250K by INTRL */
  5652. {8}, /* 125,000 ints/s */
  5653. {16}, /* 62,500 ints/s */
  5654. {62}, /* 16,129 ints/s */
  5655. {126} /* 7,936 ints/s */
  5656. };
  5657. /* The transmit profile, which has the same sorts of values
  5658. * as the previous struct
  5659. */
  5660. static const struct ice_dim tx_profile[] = {
  5661. {2}, /* 500,000 ints/s, capped at 250K by INTRL */
  5662. {8}, /* 125,000 ints/s */
  5663. {40}, /* 16,125 ints/s */
  5664. {128}, /* 7,812 ints/s */
  5665. {256} /* 3,906 ints/s */
  5666. };
  5667. static void ice_tx_dim_work(struct work_struct *work)
  5668. {
  5669. struct ice_ring_container *rc;
  5670. struct dim *dim;
  5671. u16 itr;
  5672. dim = container_of(work, struct dim, work);
  5673. rc = dim->priv;
  5674. WARN_ON(dim->profile_ix >= ARRAY_SIZE(tx_profile));
  5675. /* look up the values in our local table */
  5676. itr = tx_profile[dim->profile_ix].itr;
  5677. ice_trace(tx_dim_work, container_of(rc, struct ice_q_vector, tx), dim);
  5678. ice_write_itr(rc, itr);
  5679. dim->state = DIM_START_MEASURE;
  5680. }
  5681. static void ice_rx_dim_work(struct work_struct *work)
  5682. {
  5683. struct ice_ring_container *rc;
  5684. struct dim *dim;
  5685. u16 itr;
  5686. dim = container_of(work, struct dim, work);
  5687. rc = dim->priv;
  5688. WARN_ON(dim->profile_ix >= ARRAY_SIZE(rx_profile));
  5689. /* look up the values in our local table */
  5690. itr = rx_profile[dim->profile_ix].itr;
  5691. ice_trace(rx_dim_work, container_of(rc, struct ice_q_vector, rx), dim);
  5692. ice_write_itr(rc, itr);
  5693. dim->state = DIM_START_MEASURE;
  5694. }
  5695. #define ICE_DIM_DEFAULT_PROFILE_IX 1
  5696. /**
  5697. * ice_init_moderation - set up interrupt moderation
  5698. * @q_vector: the vector containing rings to be configured
  5699. *
  5700. * Set up interrupt moderation registers, with the intent to do the right thing
  5701. * when called from reset or from probe, and whether or not dynamic moderation
  5702. * is enabled or not. Take special care to write all the registers in both
  5703. * dynamic moderation mode or not in order to make sure hardware is in a known
  5704. * state.
  5705. */
  5706. static void ice_init_moderation(struct ice_q_vector *q_vector)
  5707. {
  5708. struct ice_ring_container *rc;
  5709. bool tx_dynamic, rx_dynamic;
  5710. rc = &q_vector->tx;
  5711. INIT_WORK(&rc->dim.work, ice_tx_dim_work);
  5712. rc->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
  5713. rc->dim.profile_ix = ICE_DIM_DEFAULT_PROFILE_IX;
  5714. rc->dim.priv = rc;
  5715. tx_dynamic = ITR_IS_DYNAMIC(rc);
  5716. /* set the initial TX ITR to match the above */
  5717. ice_write_itr(rc, tx_dynamic ?
  5718. tx_profile[rc->dim.profile_ix].itr : rc->itr_setting);
  5719. rc = &q_vector->rx;
  5720. INIT_WORK(&rc->dim.work, ice_rx_dim_work);
  5721. rc->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
  5722. rc->dim.profile_ix = ICE_DIM_DEFAULT_PROFILE_IX;
  5723. rc->dim.priv = rc;
  5724. rx_dynamic = ITR_IS_DYNAMIC(rc);
  5725. /* set the initial RX ITR to match the above */
  5726. ice_write_itr(rc, rx_dynamic ? rx_profile[rc->dim.profile_ix].itr :
  5727. rc->itr_setting);
  5728. ice_set_q_vector_intrl(q_vector);
  5729. }
  5730. /**
  5731. * ice_napi_enable_all - Enable NAPI for all q_vectors in the VSI
  5732. * @vsi: the VSI being configured
  5733. */
  5734. static void ice_napi_enable_all(struct ice_vsi *vsi)
  5735. {
  5736. int q_idx;
  5737. if (!vsi->netdev)
  5738. return;
  5739. ice_for_each_q_vector(vsi, q_idx) {
  5740. struct ice_q_vector *q_vector = vsi->q_vectors[q_idx];
  5741. ice_init_moderation(q_vector);
  5742. if (q_vector->rx.rx_ring || q_vector->tx.tx_ring)
  5743. napi_enable(&q_vector->napi);
  5744. }
  5745. }
  5746. /**
  5747. * ice_up_complete - Finish the last steps of bringing up a connection
  5748. * @vsi: The VSI being configured
  5749. *
  5750. * Return 0 on success and negative value on error
  5751. */
  5752. static int ice_up_complete(struct ice_vsi *vsi)
  5753. {
  5754. struct ice_pf *pf = vsi->back;
  5755. int err;
  5756. ice_vsi_cfg_msix(vsi);
  5757. /* Enable only Rx rings, Tx rings were enabled by the FW when the
  5758. * Tx queue group list was configured and the context bits were
  5759. * programmed using ice_vsi_cfg_txqs
  5760. */
  5761. err = ice_vsi_start_all_rx_rings(vsi);
  5762. if (err)
  5763. return err;
  5764. clear_bit(ICE_VSI_DOWN, vsi->state);
  5765. ice_napi_enable_all(vsi);
  5766. ice_vsi_ena_irq(vsi);
  5767. if (vsi->port_info &&
  5768. (vsi->port_info->phy.link_info.link_info & ICE_AQ_LINK_UP) &&
  5769. ((vsi->netdev && (vsi->type == ICE_VSI_PF ||
  5770. vsi->type == ICE_VSI_SF)))) {
  5771. ice_print_link_msg(vsi, true);
  5772. netif_tx_start_all_queues(vsi->netdev);
  5773. netif_carrier_on(vsi->netdev);
  5774. ice_ptp_link_change(pf, true);
  5775. }
  5776. /* Perform an initial read of the statistics registers now to
  5777. * set the baseline so counters are ready when interface is up
  5778. */
  5779. ice_update_eth_stats(vsi);
  5780. if (vsi->type == ICE_VSI_PF)
  5781. ice_service_task_schedule(pf);
  5782. return 0;
  5783. }
  5784. /**
  5785. * ice_up - Bring the connection back up after being down
  5786. * @vsi: VSI being configured
  5787. */
  5788. int ice_up(struct ice_vsi *vsi)
  5789. {
  5790. int err;
  5791. err = ice_vsi_cfg_lan(vsi);
  5792. if (!err)
  5793. err = ice_up_complete(vsi);
  5794. return err;
  5795. }
  5796. /**
  5797. * ice_fetch_u64_stats_per_ring - get packets and bytes stats per ring
  5798. * @syncp: pointer to u64_stats_sync
  5799. * @stats: stats that pkts and bytes count will be taken from
  5800. * @pkts: packets stats counter
  5801. * @bytes: bytes stats counter
  5802. *
  5803. * This function fetches stats from the ring considering the atomic operations
  5804. * that needs to be performed to read u64 values in 32 bit machine.
  5805. */
  5806. void
  5807. ice_fetch_u64_stats_per_ring(struct u64_stats_sync *syncp,
  5808. struct ice_q_stats stats, u64 *pkts, u64 *bytes)
  5809. {
  5810. unsigned int start;
  5811. do {
  5812. start = u64_stats_fetch_begin(syncp);
  5813. *pkts = stats.pkts;
  5814. *bytes = stats.bytes;
  5815. } while (u64_stats_fetch_retry(syncp, start));
  5816. }
  5817. /**
  5818. * ice_update_vsi_tx_ring_stats - Update VSI Tx ring stats counters
  5819. * @vsi: the VSI to be updated
  5820. * @vsi_stats: the stats struct to be updated
  5821. * @rings: rings to work on
  5822. * @count: number of rings
  5823. */
  5824. static void
  5825. ice_update_vsi_tx_ring_stats(struct ice_vsi *vsi,
  5826. struct rtnl_link_stats64 *vsi_stats,
  5827. struct ice_tx_ring **rings, u16 count)
  5828. {
  5829. u16 i;
  5830. for (i = 0; i < count; i++) {
  5831. struct ice_tx_ring *ring;
  5832. u64 pkts = 0, bytes = 0;
  5833. ring = READ_ONCE(rings[i]);
  5834. if (!ring || !ring->ring_stats)
  5835. continue;
  5836. ice_fetch_u64_stats_per_ring(&ring->ring_stats->syncp,
  5837. ring->ring_stats->stats, &pkts,
  5838. &bytes);
  5839. vsi_stats->tx_packets += pkts;
  5840. vsi_stats->tx_bytes += bytes;
  5841. vsi->tx_restart += ring->ring_stats->tx_stats.restart_q;
  5842. vsi->tx_busy += ring->ring_stats->tx_stats.tx_busy;
  5843. vsi->tx_linearize += ring->ring_stats->tx_stats.tx_linearize;
  5844. }
  5845. }
  5846. /**
  5847. * ice_update_vsi_ring_stats - Update VSI stats counters
  5848. * @vsi: the VSI to be updated
  5849. */
  5850. static void ice_update_vsi_ring_stats(struct ice_vsi *vsi)
  5851. {
  5852. struct rtnl_link_stats64 *net_stats, *stats_prev;
  5853. struct rtnl_link_stats64 *vsi_stats;
  5854. struct ice_pf *pf = vsi->back;
  5855. u64 pkts, bytes;
  5856. int i;
  5857. vsi_stats = kzalloc(sizeof(*vsi_stats), GFP_ATOMIC);
  5858. if (!vsi_stats)
  5859. return;
  5860. /* reset non-netdev (extended) stats */
  5861. vsi->tx_restart = 0;
  5862. vsi->tx_busy = 0;
  5863. vsi->tx_linearize = 0;
  5864. vsi->rx_buf_failed = 0;
  5865. vsi->rx_page_failed = 0;
  5866. rcu_read_lock();
  5867. /* update Tx rings counters */
  5868. ice_update_vsi_tx_ring_stats(vsi, vsi_stats, vsi->tx_rings,
  5869. vsi->num_txq);
  5870. /* update Rx rings counters */
  5871. ice_for_each_rxq(vsi, i) {
  5872. struct ice_rx_ring *ring = READ_ONCE(vsi->rx_rings[i]);
  5873. struct ice_ring_stats *ring_stats;
  5874. ring_stats = ring->ring_stats;
  5875. ice_fetch_u64_stats_per_ring(&ring_stats->syncp,
  5876. ring_stats->stats, &pkts,
  5877. &bytes);
  5878. vsi_stats->rx_packets += pkts;
  5879. vsi_stats->rx_bytes += bytes;
  5880. vsi->rx_buf_failed += ring_stats->rx_stats.alloc_buf_failed;
  5881. vsi->rx_page_failed += ring_stats->rx_stats.alloc_page_failed;
  5882. }
  5883. /* update XDP Tx rings counters */
  5884. if (ice_is_xdp_ena_vsi(vsi))
  5885. ice_update_vsi_tx_ring_stats(vsi, vsi_stats, vsi->xdp_rings,
  5886. vsi->num_xdp_txq);
  5887. rcu_read_unlock();
  5888. net_stats = &vsi->net_stats;
  5889. stats_prev = &vsi->net_stats_prev;
  5890. /* Update netdev counters, but keep in mind that values could start at
  5891. * random value after PF reset. And as we increase the reported stat by
  5892. * diff of Prev-Cur, we need to be sure that Prev is valid. If it's not,
  5893. * let's skip this round.
  5894. */
  5895. if (likely(pf->stat_prev_loaded)) {
  5896. net_stats->tx_packets += vsi_stats->tx_packets - stats_prev->tx_packets;
  5897. net_stats->tx_bytes += vsi_stats->tx_bytes - stats_prev->tx_bytes;
  5898. net_stats->rx_packets += vsi_stats->rx_packets - stats_prev->rx_packets;
  5899. net_stats->rx_bytes += vsi_stats->rx_bytes - stats_prev->rx_bytes;
  5900. }
  5901. stats_prev->tx_packets = vsi_stats->tx_packets;
  5902. stats_prev->tx_bytes = vsi_stats->tx_bytes;
  5903. stats_prev->rx_packets = vsi_stats->rx_packets;
  5904. stats_prev->rx_bytes = vsi_stats->rx_bytes;
  5905. kfree(vsi_stats);
  5906. }
  5907. /**
  5908. * ice_update_vsi_stats - Update VSI stats counters
  5909. * @vsi: the VSI to be updated
  5910. */
  5911. void ice_update_vsi_stats(struct ice_vsi *vsi)
  5912. {
  5913. struct rtnl_link_stats64 *cur_ns = &vsi->net_stats;
  5914. struct ice_eth_stats *cur_es = &vsi->eth_stats;
  5915. struct ice_pf *pf = vsi->back;
  5916. if (test_bit(ICE_VSI_DOWN, vsi->state) ||
  5917. test_bit(ICE_CFG_BUSY, pf->state))
  5918. return;
  5919. /* get stats as recorded by Tx/Rx rings */
  5920. ice_update_vsi_ring_stats(vsi);
  5921. /* get VSI stats as recorded by the hardware */
  5922. ice_update_eth_stats(vsi);
  5923. cur_ns->tx_errors = cur_es->tx_errors;
  5924. cur_ns->rx_dropped = cur_es->rx_discards;
  5925. cur_ns->tx_dropped = cur_es->tx_discards;
  5926. cur_ns->multicast = cur_es->rx_multicast;
  5927. /* update some more netdev stats if this is main VSI */
  5928. if (vsi->type == ICE_VSI_PF) {
  5929. cur_ns->rx_crc_errors = pf->stats.crc_errors;
  5930. cur_ns->rx_errors = pf->stats.crc_errors +
  5931. pf->stats.illegal_bytes +
  5932. pf->stats.rx_undersize +
  5933. pf->hw_csum_rx_error +
  5934. pf->stats.rx_jabber +
  5935. pf->stats.rx_fragments +
  5936. pf->stats.rx_oversize;
  5937. /* record drops from the port level */
  5938. cur_ns->rx_missed_errors = pf->stats.eth.rx_discards;
  5939. }
  5940. }
  5941. /**
  5942. * ice_update_pf_stats - Update PF port stats counters
  5943. * @pf: PF whose stats needs to be updated
  5944. */
  5945. void ice_update_pf_stats(struct ice_pf *pf)
  5946. {
  5947. struct ice_hw_port_stats *prev_ps, *cur_ps;
  5948. struct ice_hw *hw = &pf->hw;
  5949. u16 fd_ctr_base;
  5950. u8 port;
  5951. port = hw->port_info->lport;
  5952. prev_ps = &pf->stats_prev;
  5953. cur_ps = &pf->stats;
  5954. if (ice_is_reset_in_progress(pf->state))
  5955. pf->stat_prev_loaded = false;
  5956. ice_stat_update40(hw, GLPRT_GORCL(port), pf->stat_prev_loaded,
  5957. &prev_ps->eth.rx_bytes,
  5958. &cur_ps->eth.rx_bytes);
  5959. ice_stat_update40(hw, GLPRT_UPRCL(port), pf->stat_prev_loaded,
  5960. &prev_ps->eth.rx_unicast,
  5961. &cur_ps->eth.rx_unicast);
  5962. ice_stat_update40(hw, GLPRT_MPRCL(port), pf->stat_prev_loaded,
  5963. &prev_ps->eth.rx_multicast,
  5964. &cur_ps->eth.rx_multicast);
  5965. ice_stat_update40(hw, GLPRT_BPRCL(port), pf->stat_prev_loaded,
  5966. &prev_ps->eth.rx_broadcast,
  5967. &cur_ps->eth.rx_broadcast);
  5968. ice_stat_update32(hw, PRTRPB_RDPC, pf->stat_prev_loaded,
  5969. &prev_ps->eth.rx_discards,
  5970. &cur_ps->eth.rx_discards);
  5971. ice_stat_update40(hw, GLPRT_GOTCL(port), pf->stat_prev_loaded,
  5972. &prev_ps->eth.tx_bytes,
  5973. &cur_ps->eth.tx_bytes);
  5974. ice_stat_update40(hw, GLPRT_UPTCL(port), pf->stat_prev_loaded,
  5975. &prev_ps->eth.tx_unicast,
  5976. &cur_ps->eth.tx_unicast);
  5977. ice_stat_update40(hw, GLPRT_MPTCL(port), pf->stat_prev_loaded,
  5978. &prev_ps->eth.tx_multicast,
  5979. &cur_ps->eth.tx_multicast);
  5980. ice_stat_update40(hw, GLPRT_BPTCL(port), pf->stat_prev_loaded,
  5981. &prev_ps->eth.tx_broadcast,
  5982. &cur_ps->eth.tx_broadcast);
  5983. ice_stat_update32(hw, GLPRT_TDOLD(port), pf->stat_prev_loaded,
  5984. &prev_ps->tx_dropped_link_down,
  5985. &cur_ps->tx_dropped_link_down);
  5986. ice_stat_update40(hw, GLPRT_PRC64L(port), pf->stat_prev_loaded,
  5987. &prev_ps->rx_size_64, &cur_ps->rx_size_64);
  5988. ice_stat_update40(hw, GLPRT_PRC127L(port), pf->stat_prev_loaded,
  5989. &prev_ps->rx_size_127, &cur_ps->rx_size_127);
  5990. ice_stat_update40(hw, GLPRT_PRC255L(port), pf->stat_prev_loaded,
  5991. &prev_ps->rx_size_255, &cur_ps->rx_size_255);
  5992. ice_stat_update40(hw, GLPRT_PRC511L(port), pf->stat_prev_loaded,
  5993. &prev_ps->rx_size_511, &cur_ps->rx_size_511);
  5994. ice_stat_update40(hw, GLPRT_PRC1023L(port), pf->stat_prev_loaded,
  5995. &prev_ps->rx_size_1023, &cur_ps->rx_size_1023);
  5996. ice_stat_update40(hw, GLPRT_PRC1522L(port), pf->stat_prev_loaded,
  5997. &prev_ps->rx_size_1522, &cur_ps->rx_size_1522);
  5998. ice_stat_update40(hw, GLPRT_PRC9522L(port), pf->stat_prev_loaded,
  5999. &prev_ps->rx_size_big, &cur_ps->rx_size_big);
  6000. ice_stat_update40(hw, GLPRT_PTC64L(port), pf->stat_prev_loaded,
  6001. &prev_ps->tx_size_64, &cur_ps->tx_size_64);
  6002. ice_stat_update40(hw, GLPRT_PTC127L(port), pf->stat_prev_loaded,
  6003. &prev_ps->tx_size_127, &cur_ps->tx_size_127);
  6004. ice_stat_update40(hw, GLPRT_PTC255L(port), pf->stat_prev_loaded,
  6005. &prev_ps->tx_size_255, &cur_ps->tx_size_255);
  6006. ice_stat_update40(hw, GLPRT_PTC511L(port), pf->stat_prev_loaded,
  6007. &prev_ps->tx_size_511, &cur_ps->tx_size_511);
  6008. ice_stat_update40(hw, GLPRT_PTC1023L(port), pf->stat_prev_loaded,
  6009. &prev_ps->tx_size_1023, &cur_ps->tx_size_1023);
  6010. ice_stat_update40(hw, GLPRT_PTC1522L(port), pf->stat_prev_loaded,
  6011. &prev_ps->tx_size_1522, &cur_ps->tx_size_1522);
  6012. ice_stat_update40(hw, GLPRT_PTC9522L(port), pf->stat_prev_loaded,
  6013. &prev_ps->tx_size_big, &cur_ps->tx_size_big);
  6014. fd_ctr_base = hw->fd_ctr_base;
  6015. ice_stat_update40(hw,
  6016. GLSTAT_FD_CNT0L(ICE_FD_SB_STAT_IDX(fd_ctr_base)),
  6017. pf->stat_prev_loaded, &prev_ps->fd_sb_match,
  6018. &cur_ps->fd_sb_match);
  6019. ice_stat_update32(hw, GLPRT_LXONRXC(port), pf->stat_prev_loaded,
  6020. &prev_ps->link_xon_rx, &cur_ps->link_xon_rx);
  6021. ice_stat_update32(hw, GLPRT_LXOFFRXC(port), pf->stat_prev_loaded,
  6022. &prev_ps->link_xoff_rx, &cur_ps->link_xoff_rx);
  6023. ice_stat_update32(hw, GLPRT_LXONTXC(port), pf->stat_prev_loaded,
  6024. &prev_ps->link_xon_tx, &cur_ps->link_xon_tx);
  6025. ice_stat_update32(hw, GLPRT_LXOFFTXC(port), pf->stat_prev_loaded,
  6026. &prev_ps->link_xoff_tx, &cur_ps->link_xoff_tx);
  6027. ice_update_dcb_stats(pf);
  6028. ice_stat_update32(hw, GLPRT_CRCERRS(port), pf->stat_prev_loaded,
  6029. &prev_ps->crc_errors, &cur_ps->crc_errors);
  6030. ice_stat_update32(hw, GLPRT_ILLERRC(port), pf->stat_prev_loaded,
  6031. &prev_ps->illegal_bytes, &cur_ps->illegal_bytes);
  6032. ice_stat_update32(hw, GLPRT_MLFC(port), pf->stat_prev_loaded,
  6033. &prev_ps->mac_local_faults,
  6034. &cur_ps->mac_local_faults);
  6035. ice_stat_update32(hw, GLPRT_MRFC(port), pf->stat_prev_loaded,
  6036. &prev_ps->mac_remote_faults,
  6037. &cur_ps->mac_remote_faults);
  6038. ice_stat_update32(hw, GLPRT_RUC(port), pf->stat_prev_loaded,
  6039. &prev_ps->rx_undersize, &cur_ps->rx_undersize);
  6040. ice_stat_update32(hw, GLPRT_RFC(port), pf->stat_prev_loaded,
  6041. &prev_ps->rx_fragments, &cur_ps->rx_fragments);
  6042. ice_stat_update32(hw, GLPRT_ROC(port), pf->stat_prev_loaded,
  6043. &prev_ps->rx_oversize, &cur_ps->rx_oversize);
  6044. ice_stat_update32(hw, GLPRT_RJC(port), pf->stat_prev_loaded,
  6045. &prev_ps->rx_jabber, &cur_ps->rx_jabber);
  6046. cur_ps->fd_sb_status = test_bit(ICE_FLAG_FD_ENA, pf->flags) ? 1 : 0;
  6047. pf->stat_prev_loaded = true;
  6048. }
  6049. /**
  6050. * ice_get_stats64 - get statistics for network device structure
  6051. * @netdev: network interface device structure
  6052. * @stats: main device statistics structure
  6053. */
  6054. void ice_get_stats64(struct net_device *netdev, struct rtnl_link_stats64 *stats)
  6055. {
  6056. struct ice_netdev_priv *np = netdev_priv(netdev);
  6057. struct rtnl_link_stats64 *vsi_stats;
  6058. struct ice_vsi *vsi = np->vsi;
  6059. vsi_stats = &vsi->net_stats;
  6060. if (!vsi->num_txq || !vsi->num_rxq)
  6061. return;
  6062. /* netdev packet/byte stats come from ring counter. These are obtained
  6063. * by summing up ring counters (done by ice_update_vsi_ring_stats).
  6064. * But, only call the update routine and read the registers if VSI is
  6065. * not down.
  6066. */
  6067. if (!test_bit(ICE_VSI_DOWN, vsi->state))
  6068. ice_update_vsi_ring_stats(vsi);
  6069. stats->tx_packets = vsi_stats->tx_packets;
  6070. stats->tx_bytes = vsi_stats->tx_bytes;
  6071. stats->rx_packets = vsi_stats->rx_packets;
  6072. stats->rx_bytes = vsi_stats->rx_bytes;
  6073. /* The rest of the stats can be read from the hardware but instead we
  6074. * just return values that the watchdog task has already obtained from
  6075. * the hardware.
  6076. */
  6077. stats->multicast = vsi_stats->multicast;
  6078. stats->tx_errors = vsi_stats->tx_errors;
  6079. stats->tx_dropped = vsi_stats->tx_dropped;
  6080. stats->rx_errors = vsi_stats->rx_errors;
  6081. stats->rx_dropped = vsi_stats->rx_dropped;
  6082. stats->rx_crc_errors = vsi_stats->rx_crc_errors;
  6083. stats->rx_length_errors = vsi_stats->rx_length_errors;
  6084. }
  6085. /**
  6086. * ice_napi_disable_all - Disable NAPI for all q_vectors in the VSI
  6087. * @vsi: VSI having NAPI disabled
  6088. */
  6089. static void ice_napi_disable_all(struct ice_vsi *vsi)
  6090. {
  6091. int q_idx;
  6092. if (!vsi->netdev)
  6093. return;
  6094. ice_for_each_q_vector(vsi, q_idx) {
  6095. struct ice_q_vector *q_vector = vsi->q_vectors[q_idx];
  6096. if (q_vector->rx.rx_ring || q_vector->tx.tx_ring)
  6097. napi_disable(&q_vector->napi);
  6098. cancel_work_sync(&q_vector->tx.dim.work);
  6099. cancel_work_sync(&q_vector->rx.dim.work);
  6100. }
  6101. }
  6102. /**
  6103. * ice_vsi_dis_irq - Mask off queue interrupt generation on the VSI
  6104. * @vsi: the VSI being un-configured
  6105. */
  6106. static void ice_vsi_dis_irq(struct ice_vsi *vsi)
  6107. {
  6108. struct ice_pf *pf = vsi->back;
  6109. struct ice_hw *hw = &pf->hw;
  6110. u32 val;
  6111. int i;
  6112. /* disable interrupt causation from each Rx queue; Tx queues are
  6113. * handled in ice_vsi_stop_tx_ring()
  6114. */
  6115. if (vsi->rx_rings) {
  6116. ice_for_each_rxq(vsi, i) {
  6117. if (vsi->rx_rings[i]) {
  6118. u16 reg;
  6119. reg = vsi->rx_rings[i]->reg_idx;
  6120. val = rd32(hw, QINT_RQCTL(reg));
  6121. val &= ~QINT_RQCTL_CAUSE_ENA_M;
  6122. wr32(hw, QINT_RQCTL(reg), val);
  6123. }
  6124. }
  6125. }
  6126. /* disable each interrupt */
  6127. ice_for_each_q_vector(vsi, i) {
  6128. if (!vsi->q_vectors[i])
  6129. continue;
  6130. wr32(hw, GLINT_DYN_CTL(vsi->q_vectors[i]->reg_idx), 0);
  6131. }
  6132. ice_flush(hw);
  6133. /* don't call synchronize_irq() for VF's from the host */
  6134. if (vsi->type == ICE_VSI_VF)
  6135. return;
  6136. ice_for_each_q_vector(vsi, i)
  6137. synchronize_irq(vsi->q_vectors[i]->irq.virq);
  6138. }
  6139. /**
  6140. * ice_down - Shutdown the connection
  6141. * @vsi: The VSI being stopped
  6142. *
  6143. * Caller of this function is expected to set the vsi->state ICE_DOWN bit
  6144. */
  6145. int ice_down(struct ice_vsi *vsi)
  6146. {
  6147. int i, tx_err, rx_err, vlan_err = 0;
  6148. WARN_ON(!test_bit(ICE_VSI_DOWN, vsi->state));
  6149. if (vsi->netdev) {
  6150. vlan_err = ice_vsi_del_vlan_zero(vsi);
  6151. ice_ptp_link_change(vsi->back, false);
  6152. netif_carrier_off(vsi->netdev);
  6153. netif_tx_disable(vsi->netdev);
  6154. }
  6155. ice_vsi_dis_irq(vsi);
  6156. tx_err = ice_vsi_stop_lan_tx_rings(vsi, ICE_NO_RESET, 0);
  6157. if (tx_err)
  6158. netdev_err(vsi->netdev, "Failed stop Tx rings, VSI %d error %d\n",
  6159. vsi->vsi_num, tx_err);
  6160. if (!tx_err && vsi->xdp_rings) {
  6161. tx_err = ice_vsi_stop_xdp_tx_rings(vsi);
  6162. if (tx_err)
  6163. netdev_err(vsi->netdev, "Failed stop XDP rings, VSI %d error %d\n",
  6164. vsi->vsi_num, tx_err);
  6165. }
  6166. rx_err = ice_vsi_stop_all_rx_rings(vsi);
  6167. if (rx_err)
  6168. netdev_err(vsi->netdev, "Failed stop Rx rings, VSI %d error %d\n",
  6169. vsi->vsi_num, rx_err);
  6170. ice_napi_disable_all(vsi);
  6171. ice_for_each_txq(vsi, i)
  6172. ice_clean_tx_ring(vsi->tx_rings[i]);
  6173. if (vsi->xdp_rings)
  6174. ice_for_each_xdp_txq(vsi, i)
  6175. ice_clean_tx_ring(vsi->xdp_rings[i]);
  6176. ice_for_each_rxq(vsi, i)
  6177. ice_clean_rx_ring(vsi->rx_rings[i]);
  6178. if (tx_err || rx_err || vlan_err) {
  6179. netdev_err(vsi->netdev, "Failed to close VSI 0x%04X on switch 0x%04X\n",
  6180. vsi->vsi_num, vsi->vsw->sw_id);
  6181. return -EIO;
  6182. }
  6183. return 0;
  6184. }
  6185. /**
  6186. * ice_down_up - shutdown the VSI connection and bring it up
  6187. * @vsi: the VSI to be reconnected
  6188. */
  6189. int ice_down_up(struct ice_vsi *vsi)
  6190. {
  6191. int ret;
  6192. /* if DOWN already set, nothing to do */
  6193. if (test_and_set_bit(ICE_VSI_DOWN, vsi->state))
  6194. return 0;
  6195. ret = ice_down(vsi);
  6196. if (ret)
  6197. return ret;
  6198. ret = ice_up(vsi);
  6199. if (ret) {
  6200. netdev_err(vsi->netdev, "reallocating resources failed during netdev features change, may need to reload driver\n");
  6201. return ret;
  6202. }
  6203. return 0;
  6204. }
  6205. /**
  6206. * ice_vsi_setup_tx_rings - Allocate VSI Tx queue resources
  6207. * @vsi: VSI having resources allocated
  6208. *
  6209. * Return 0 on success, negative on failure
  6210. */
  6211. int ice_vsi_setup_tx_rings(struct ice_vsi *vsi)
  6212. {
  6213. int i, err = 0;
  6214. if (!vsi->num_txq) {
  6215. dev_err(ice_pf_to_dev(vsi->back), "VSI %d has 0 Tx queues\n",
  6216. vsi->vsi_num);
  6217. return -EINVAL;
  6218. }
  6219. ice_for_each_txq(vsi, i) {
  6220. struct ice_tx_ring *ring = vsi->tx_rings[i];
  6221. if (!ring)
  6222. return -EINVAL;
  6223. if (vsi->netdev)
  6224. ring->netdev = vsi->netdev;
  6225. err = ice_setup_tx_ring(ring);
  6226. if (err)
  6227. break;
  6228. }
  6229. return err;
  6230. }
  6231. /**
  6232. * ice_vsi_setup_rx_rings - Allocate VSI Rx queue resources
  6233. * @vsi: VSI having resources allocated
  6234. *
  6235. * Return 0 on success, negative on failure
  6236. */
  6237. int ice_vsi_setup_rx_rings(struct ice_vsi *vsi)
  6238. {
  6239. int i, err = 0;
  6240. if (!vsi->num_rxq) {
  6241. dev_err(ice_pf_to_dev(vsi->back), "VSI %d has 0 Rx queues\n",
  6242. vsi->vsi_num);
  6243. return -EINVAL;
  6244. }
  6245. ice_for_each_rxq(vsi, i) {
  6246. struct ice_rx_ring *ring = vsi->rx_rings[i];
  6247. if (!ring)
  6248. return -EINVAL;
  6249. if (vsi->netdev)
  6250. ring->netdev = vsi->netdev;
  6251. err = ice_setup_rx_ring(ring);
  6252. if (err)
  6253. break;
  6254. }
  6255. return err;
  6256. }
  6257. /**
  6258. * ice_vsi_open_ctrl - open control VSI for use
  6259. * @vsi: the VSI to open
  6260. *
  6261. * Initialization of the Control VSI
  6262. *
  6263. * Returns 0 on success, negative value on error
  6264. */
  6265. int ice_vsi_open_ctrl(struct ice_vsi *vsi)
  6266. {
  6267. char int_name[ICE_INT_NAME_STR_LEN];
  6268. struct ice_pf *pf = vsi->back;
  6269. struct device *dev;
  6270. int err;
  6271. dev = ice_pf_to_dev(pf);
  6272. /* allocate descriptors */
  6273. err = ice_vsi_setup_tx_rings(vsi);
  6274. if (err)
  6275. goto err_setup_tx;
  6276. err = ice_vsi_setup_rx_rings(vsi);
  6277. if (err)
  6278. goto err_setup_rx;
  6279. err = ice_vsi_cfg_lan(vsi);
  6280. if (err)
  6281. goto err_setup_rx;
  6282. snprintf(int_name, sizeof(int_name) - 1, "%s-%s:ctrl",
  6283. dev_driver_string(dev), dev_name(dev));
  6284. err = ice_vsi_req_irq_msix(vsi, int_name);
  6285. if (err)
  6286. goto err_setup_rx;
  6287. ice_vsi_cfg_msix(vsi);
  6288. err = ice_vsi_start_all_rx_rings(vsi);
  6289. if (err)
  6290. goto err_up_complete;
  6291. clear_bit(ICE_VSI_DOWN, vsi->state);
  6292. ice_vsi_ena_irq(vsi);
  6293. return 0;
  6294. err_up_complete:
  6295. ice_down(vsi);
  6296. err_setup_rx:
  6297. ice_vsi_free_rx_rings(vsi);
  6298. err_setup_tx:
  6299. ice_vsi_free_tx_rings(vsi);
  6300. return err;
  6301. }
  6302. /**
  6303. * ice_vsi_open - Called when a network interface is made active
  6304. * @vsi: the VSI to open
  6305. *
  6306. * Initialization of the VSI
  6307. *
  6308. * Returns 0 on success, negative value on error
  6309. */
  6310. int ice_vsi_open(struct ice_vsi *vsi)
  6311. {
  6312. char int_name[ICE_INT_NAME_STR_LEN];
  6313. struct ice_pf *pf = vsi->back;
  6314. int err;
  6315. /* allocate descriptors */
  6316. err = ice_vsi_setup_tx_rings(vsi);
  6317. if (err)
  6318. goto err_setup_tx;
  6319. err = ice_vsi_setup_rx_rings(vsi);
  6320. if (err)
  6321. goto err_setup_rx;
  6322. err = ice_vsi_cfg_lan(vsi);
  6323. if (err)
  6324. goto err_setup_rx;
  6325. snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
  6326. dev_driver_string(ice_pf_to_dev(pf)), vsi->netdev->name);
  6327. err = ice_vsi_req_irq_msix(vsi, int_name);
  6328. if (err)
  6329. goto err_setup_rx;
  6330. ice_vsi_cfg_netdev_tc(vsi, vsi->tc_cfg.ena_tc);
  6331. if (vsi->type == ICE_VSI_PF || vsi->type == ICE_VSI_SF) {
  6332. /* Notify the stack of the actual queue counts. */
  6333. err = netif_set_real_num_tx_queues(vsi->netdev, vsi->num_txq);
  6334. if (err)
  6335. goto err_set_qs;
  6336. err = netif_set_real_num_rx_queues(vsi->netdev, vsi->num_rxq);
  6337. if (err)
  6338. goto err_set_qs;
  6339. ice_vsi_set_napi_queues(vsi);
  6340. }
  6341. err = ice_up_complete(vsi);
  6342. if (err)
  6343. goto err_up_complete;
  6344. return 0;
  6345. err_up_complete:
  6346. ice_down(vsi);
  6347. err_set_qs:
  6348. ice_vsi_free_irq(vsi);
  6349. err_setup_rx:
  6350. ice_vsi_free_rx_rings(vsi);
  6351. err_setup_tx:
  6352. ice_vsi_free_tx_rings(vsi);
  6353. return err;
  6354. }
  6355. /**
  6356. * ice_vsi_release_all - Delete all VSIs
  6357. * @pf: PF from which all VSIs are being removed
  6358. */
  6359. static void ice_vsi_release_all(struct ice_pf *pf)
  6360. {
  6361. int err, i;
  6362. if (!pf->vsi)
  6363. return;
  6364. ice_for_each_vsi(pf, i) {
  6365. if (!pf->vsi[i])
  6366. continue;
  6367. if (pf->vsi[i]->type == ICE_VSI_CHNL)
  6368. continue;
  6369. err = ice_vsi_release(pf->vsi[i]);
  6370. if (err)
  6371. dev_dbg(ice_pf_to_dev(pf), "Failed to release pf->vsi[%d], err %d, vsi_num = %d\n",
  6372. i, err, pf->vsi[i]->vsi_num);
  6373. }
  6374. }
  6375. /**
  6376. * ice_vsi_rebuild_by_type - Rebuild VSI of a given type
  6377. * @pf: pointer to the PF instance
  6378. * @type: VSI type to rebuild
  6379. *
  6380. * Iterates through the pf->vsi array and rebuilds VSIs of the requested type
  6381. */
  6382. static int ice_vsi_rebuild_by_type(struct ice_pf *pf, enum ice_vsi_type type)
  6383. {
  6384. struct device *dev = ice_pf_to_dev(pf);
  6385. int i, err;
  6386. ice_for_each_vsi(pf, i) {
  6387. struct ice_vsi *vsi = pf->vsi[i];
  6388. if (!vsi || vsi->type != type)
  6389. continue;
  6390. /* rebuild the VSI */
  6391. err = ice_vsi_rebuild(vsi, ICE_VSI_FLAG_INIT);
  6392. if (err) {
  6393. dev_err(dev, "rebuild VSI failed, err %d, VSI index %d, type %s\n",
  6394. err, vsi->idx, ice_vsi_type_str(type));
  6395. return err;
  6396. }
  6397. /* replay filters for the VSI */
  6398. err = ice_replay_vsi(&pf->hw, vsi->idx);
  6399. if (err) {
  6400. dev_err(dev, "replay VSI failed, error %d, VSI index %d, type %s\n",
  6401. err, vsi->idx, ice_vsi_type_str(type));
  6402. return err;
  6403. }
  6404. /* Re-map HW VSI number, using VSI handle that has been
  6405. * previously validated in ice_replay_vsi() call above
  6406. */
  6407. vsi->vsi_num = ice_get_hw_vsi_num(&pf->hw, vsi->idx);
  6408. /* enable the VSI */
  6409. err = ice_ena_vsi(vsi, false);
  6410. if (err) {
  6411. dev_err(dev, "enable VSI failed, err %d, VSI index %d, type %s\n",
  6412. err, vsi->idx, ice_vsi_type_str(type));
  6413. return err;
  6414. }
  6415. dev_info(dev, "VSI rebuilt. VSI index %d, type %s\n", vsi->idx,
  6416. ice_vsi_type_str(type));
  6417. }
  6418. return 0;
  6419. }
  6420. /**
  6421. * ice_update_pf_netdev_link - Update PF netdev link status
  6422. * @pf: pointer to the PF instance
  6423. */
  6424. static void ice_update_pf_netdev_link(struct ice_pf *pf)
  6425. {
  6426. bool link_up;
  6427. int i;
  6428. ice_for_each_vsi(pf, i) {
  6429. struct ice_vsi *vsi = pf->vsi[i];
  6430. if (!vsi || vsi->type != ICE_VSI_PF)
  6431. return;
  6432. ice_get_link_status(pf->vsi[i]->port_info, &link_up);
  6433. if (link_up) {
  6434. netif_carrier_on(pf->vsi[i]->netdev);
  6435. netif_tx_wake_all_queues(pf->vsi[i]->netdev);
  6436. } else {
  6437. netif_carrier_off(pf->vsi[i]->netdev);
  6438. netif_tx_stop_all_queues(pf->vsi[i]->netdev);
  6439. }
  6440. }
  6441. }
  6442. /**
  6443. * ice_rebuild - rebuild after reset
  6444. * @pf: PF to rebuild
  6445. * @reset_type: type of reset
  6446. *
  6447. * Do not rebuild VF VSI in this flow because that is already handled via
  6448. * ice_reset_all_vfs(). This is because requirements for resetting a VF after a
  6449. * PFR/CORER/GLOBER/etc. are different than the normal flow. Also, we don't want
  6450. * to reset/rebuild all the VF VSI twice.
  6451. */
  6452. static void ice_rebuild(struct ice_pf *pf, enum ice_reset_req reset_type)
  6453. {
  6454. struct ice_vsi *vsi = ice_get_main_vsi(pf);
  6455. struct device *dev = ice_pf_to_dev(pf);
  6456. struct ice_hw *hw = &pf->hw;
  6457. bool dvm;
  6458. int err;
  6459. if (test_bit(ICE_DOWN, pf->state))
  6460. goto clear_recovery;
  6461. dev_dbg(dev, "rebuilding PF after reset_type=%d\n", reset_type);
  6462. #define ICE_EMP_RESET_SLEEP_MS 5000
  6463. if (reset_type == ICE_RESET_EMPR) {
  6464. /* If an EMP reset has occurred, any previously pending flash
  6465. * update will have completed. We no longer know whether or
  6466. * not the NVM update EMP reset is restricted.
  6467. */
  6468. pf->fw_emp_reset_disabled = false;
  6469. msleep(ICE_EMP_RESET_SLEEP_MS);
  6470. }
  6471. err = ice_init_all_ctrlq(hw);
  6472. if (err) {
  6473. dev_err(dev, "control queues init failed %d\n", err);
  6474. goto err_init_ctrlq;
  6475. }
  6476. /* if DDP was previously loaded successfully */
  6477. if (!ice_is_safe_mode(pf)) {
  6478. /* reload the SW DB of filter tables */
  6479. if (reset_type == ICE_RESET_PFR)
  6480. ice_fill_blk_tbls(hw);
  6481. else
  6482. /* Reload DDP Package after CORER/GLOBR reset */
  6483. ice_load_pkg(NULL, pf);
  6484. }
  6485. err = ice_clear_pf_cfg(hw);
  6486. if (err) {
  6487. dev_err(dev, "clear PF configuration failed %d\n", err);
  6488. goto err_init_ctrlq;
  6489. }
  6490. ice_clear_pxe_mode(hw);
  6491. err = ice_init_nvm(hw);
  6492. if (err) {
  6493. dev_err(dev, "ice_init_nvm failed %d\n", err);
  6494. goto err_init_ctrlq;
  6495. }
  6496. err = ice_get_caps(hw);
  6497. if (err) {
  6498. dev_err(dev, "ice_get_caps failed %d\n", err);
  6499. goto err_init_ctrlq;
  6500. }
  6501. err = ice_aq_set_mac_cfg(hw, ICE_AQ_SET_MAC_FRAME_SIZE_MAX, NULL);
  6502. if (err) {
  6503. dev_err(dev, "set_mac_cfg failed %d\n", err);
  6504. goto err_init_ctrlq;
  6505. }
  6506. dvm = ice_is_dvm_ena(hw);
  6507. err = ice_aq_set_port_params(pf->hw.port_info, dvm, NULL);
  6508. if (err)
  6509. goto err_init_ctrlq;
  6510. err = ice_sched_init_port(hw->port_info);
  6511. if (err)
  6512. goto err_sched_init_port;
  6513. /* start misc vector */
  6514. err = ice_req_irq_msix_misc(pf);
  6515. if (err) {
  6516. dev_err(dev, "misc vector setup failed: %d\n", err);
  6517. goto err_sched_init_port;
  6518. }
  6519. if (test_bit(ICE_FLAG_FD_ENA, pf->flags)) {
  6520. wr32(hw, PFQF_FD_ENA, PFQF_FD_ENA_FD_ENA_M);
  6521. if (!rd32(hw, PFQF_FD_SIZE)) {
  6522. u16 unused, guar, b_effort;
  6523. guar = hw->func_caps.fd_fltr_guar;
  6524. b_effort = hw->func_caps.fd_fltr_best_effort;
  6525. /* force guaranteed filter pool for PF */
  6526. ice_alloc_fd_guar_item(hw, &unused, guar);
  6527. /* force shared filter pool for PF */
  6528. ice_alloc_fd_shrd_item(hw, &unused, b_effort);
  6529. }
  6530. }
  6531. if (test_bit(ICE_FLAG_DCB_ENA, pf->flags))
  6532. ice_dcb_rebuild(pf);
  6533. /* If the PF previously had enabled PTP, PTP init needs to happen before
  6534. * the VSI rebuild. If not, this causes the PTP link status events to
  6535. * fail.
  6536. */
  6537. if (test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags))
  6538. ice_ptp_rebuild(pf, reset_type);
  6539. if (ice_is_feature_supported(pf, ICE_F_GNSS))
  6540. ice_gnss_init(pf);
  6541. /* rebuild PF VSI */
  6542. err = ice_vsi_rebuild_by_type(pf, ICE_VSI_PF);
  6543. if (err) {
  6544. dev_err(dev, "PF VSI rebuild failed: %d\n", err);
  6545. goto err_vsi_rebuild;
  6546. }
  6547. if (reset_type == ICE_RESET_PFR) {
  6548. err = ice_rebuild_channels(pf);
  6549. if (err) {
  6550. dev_err(dev, "failed to rebuild and replay ADQ VSIs, err %d\n",
  6551. err);
  6552. goto err_vsi_rebuild;
  6553. }
  6554. }
  6555. /* If Flow Director is active */
  6556. if (test_bit(ICE_FLAG_FD_ENA, pf->flags)) {
  6557. err = ice_vsi_rebuild_by_type(pf, ICE_VSI_CTRL);
  6558. if (err) {
  6559. dev_err(dev, "control VSI rebuild failed: %d\n", err);
  6560. goto err_vsi_rebuild;
  6561. }
  6562. /* replay HW Flow Director recipes */
  6563. if (hw->fdir_prof)
  6564. ice_fdir_replay_flows(hw);
  6565. /* replay Flow Director filters */
  6566. ice_fdir_replay_fltrs(pf);
  6567. ice_rebuild_arfs(pf);
  6568. }
  6569. if (vsi && vsi->netdev)
  6570. netif_device_attach(vsi->netdev);
  6571. ice_update_pf_netdev_link(pf);
  6572. /* tell the firmware we are up */
  6573. err = ice_send_version(pf);
  6574. if (err) {
  6575. dev_err(dev, "Rebuild failed due to error sending driver version: %d\n",
  6576. err);
  6577. goto err_vsi_rebuild;
  6578. }
  6579. ice_replay_post(hw);
  6580. /* if we get here, reset flow is successful */
  6581. clear_bit(ICE_RESET_FAILED, pf->state);
  6582. ice_plug_aux_dev(pf);
  6583. if (ice_is_feature_supported(pf, ICE_F_SRIOV_LAG))
  6584. ice_lag_rebuild(pf);
  6585. /* Restore timestamp mode settings after VSI rebuild */
  6586. ice_ptp_restore_timestamp_mode(pf);
  6587. return;
  6588. err_vsi_rebuild:
  6589. err_sched_init_port:
  6590. ice_sched_cleanup_all(hw);
  6591. err_init_ctrlq:
  6592. ice_shutdown_all_ctrlq(hw, false);
  6593. set_bit(ICE_RESET_FAILED, pf->state);
  6594. clear_recovery:
  6595. /* set this bit in PF state to control service task scheduling */
  6596. set_bit(ICE_NEEDS_RESTART, pf->state);
  6597. dev_err(dev, "Rebuild failed, unload and reload driver\n");
  6598. }
  6599. /**
  6600. * ice_change_mtu - NDO callback to change the MTU
  6601. * @netdev: network interface device structure
  6602. * @new_mtu: new value for maximum frame size
  6603. *
  6604. * Returns 0 on success, negative on failure
  6605. */
  6606. int ice_change_mtu(struct net_device *netdev, int new_mtu)
  6607. {
  6608. struct ice_netdev_priv *np = netdev_priv(netdev);
  6609. struct ice_vsi *vsi = np->vsi;
  6610. struct ice_pf *pf = vsi->back;
  6611. struct bpf_prog *prog;
  6612. u8 count = 0;
  6613. int err = 0;
  6614. if (new_mtu == (int)netdev->mtu) {
  6615. netdev_warn(netdev, "MTU is already %u\n", netdev->mtu);
  6616. return 0;
  6617. }
  6618. prog = vsi->xdp_prog;
  6619. if (prog && !prog->aux->xdp_has_frags) {
  6620. int frame_size = ice_max_xdp_frame_size(vsi);
  6621. if (new_mtu + ICE_ETH_PKT_HDR_PAD > frame_size) {
  6622. netdev_err(netdev, "max MTU for XDP usage is %d\n",
  6623. frame_size - ICE_ETH_PKT_HDR_PAD);
  6624. return -EINVAL;
  6625. }
  6626. } else if (test_bit(ICE_FLAG_LEGACY_RX, pf->flags)) {
  6627. if (new_mtu + ICE_ETH_PKT_HDR_PAD > ICE_MAX_FRAME_LEGACY_RX) {
  6628. netdev_err(netdev, "Too big MTU for legacy-rx; Max is %d\n",
  6629. ICE_MAX_FRAME_LEGACY_RX - ICE_ETH_PKT_HDR_PAD);
  6630. return -EINVAL;
  6631. }
  6632. }
  6633. /* if a reset is in progress, wait for some time for it to complete */
  6634. do {
  6635. if (ice_is_reset_in_progress(pf->state)) {
  6636. count++;
  6637. usleep_range(1000, 2000);
  6638. } else {
  6639. break;
  6640. }
  6641. } while (count < 100);
  6642. if (count == 100) {
  6643. netdev_err(netdev, "can't change MTU. Device is busy\n");
  6644. return -EBUSY;
  6645. }
  6646. WRITE_ONCE(netdev->mtu, (unsigned int)new_mtu);
  6647. err = ice_down_up(vsi);
  6648. if (err)
  6649. return err;
  6650. netdev_dbg(netdev, "changed MTU to %d\n", new_mtu);
  6651. set_bit(ICE_FLAG_MTU_CHANGED, pf->flags);
  6652. return err;
  6653. }
  6654. /**
  6655. * ice_eth_ioctl - Access the hwtstamp interface
  6656. * @netdev: network interface device structure
  6657. * @ifr: interface request data
  6658. * @cmd: ioctl command
  6659. */
  6660. static int ice_eth_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  6661. {
  6662. struct ice_netdev_priv *np = netdev_priv(netdev);
  6663. struct ice_pf *pf = np->vsi->back;
  6664. switch (cmd) {
  6665. case SIOCGHWTSTAMP:
  6666. return ice_ptp_get_ts_config(pf, ifr);
  6667. case SIOCSHWTSTAMP:
  6668. return ice_ptp_set_ts_config(pf, ifr);
  6669. default:
  6670. return -EOPNOTSUPP;
  6671. }
  6672. }
  6673. /**
  6674. * ice_aq_str - convert AQ err code to a string
  6675. * @aq_err: the AQ error code to convert
  6676. */
  6677. const char *ice_aq_str(enum ice_aq_err aq_err)
  6678. {
  6679. switch (aq_err) {
  6680. case ICE_AQ_RC_OK:
  6681. return "OK";
  6682. case ICE_AQ_RC_EPERM:
  6683. return "ICE_AQ_RC_EPERM";
  6684. case ICE_AQ_RC_ENOENT:
  6685. return "ICE_AQ_RC_ENOENT";
  6686. case ICE_AQ_RC_ENOMEM:
  6687. return "ICE_AQ_RC_ENOMEM";
  6688. case ICE_AQ_RC_EBUSY:
  6689. return "ICE_AQ_RC_EBUSY";
  6690. case ICE_AQ_RC_EEXIST:
  6691. return "ICE_AQ_RC_EEXIST";
  6692. case ICE_AQ_RC_EINVAL:
  6693. return "ICE_AQ_RC_EINVAL";
  6694. case ICE_AQ_RC_ENOSPC:
  6695. return "ICE_AQ_RC_ENOSPC";
  6696. case ICE_AQ_RC_ENOSYS:
  6697. return "ICE_AQ_RC_ENOSYS";
  6698. case ICE_AQ_RC_EMODE:
  6699. return "ICE_AQ_RC_EMODE";
  6700. case ICE_AQ_RC_ENOSEC:
  6701. return "ICE_AQ_RC_ENOSEC";
  6702. case ICE_AQ_RC_EBADSIG:
  6703. return "ICE_AQ_RC_EBADSIG";
  6704. case ICE_AQ_RC_ESVN:
  6705. return "ICE_AQ_RC_ESVN";
  6706. case ICE_AQ_RC_EBADMAN:
  6707. return "ICE_AQ_RC_EBADMAN";
  6708. case ICE_AQ_RC_EBADBUF:
  6709. return "ICE_AQ_RC_EBADBUF";
  6710. }
  6711. return "ICE_AQ_RC_UNKNOWN";
  6712. }
  6713. /**
  6714. * ice_set_rss_lut - Set RSS LUT
  6715. * @vsi: Pointer to VSI structure
  6716. * @lut: Lookup table
  6717. * @lut_size: Lookup table size
  6718. *
  6719. * Returns 0 on success, negative on failure
  6720. */
  6721. int ice_set_rss_lut(struct ice_vsi *vsi, u8 *lut, u16 lut_size)
  6722. {
  6723. struct ice_aq_get_set_rss_lut_params params = {};
  6724. struct ice_hw *hw = &vsi->back->hw;
  6725. int status;
  6726. if (!lut)
  6727. return -EINVAL;
  6728. params.vsi_handle = vsi->idx;
  6729. params.lut_size = lut_size;
  6730. params.lut_type = vsi->rss_lut_type;
  6731. params.lut = lut;
  6732. status = ice_aq_set_rss_lut(hw, &params);
  6733. if (status)
  6734. dev_err(ice_pf_to_dev(vsi->back), "Cannot set RSS lut, err %d aq_err %s\n",
  6735. status, ice_aq_str(hw->adminq.sq_last_status));
  6736. return status;
  6737. }
  6738. /**
  6739. * ice_set_rss_key - Set RSS key
  6740. * @vsi: Pointer to the VSI structure
  6741. * @seed: RSS hash seed
  6742. *
  6743. * Returns 0 on success, negative on failure
  6744. */
  6745. int ice_set_rss_key(struct ice_vsi *vsi, u8 *seed)
  6746. {
  6747. struct ice_hw *hw = &vsi->back->hw;
  6748. int status;
  6749. if (!seed)
  6750. return -EINVAL;
  6751. status = ice_aq_set_rss_key(hw, vsi->idx, (struct ice_aqc_get_set_rss_keys *)seed);
  6752. if (status)
  6753. dev_err(ice_pf_to_dev(vsi->back), "Cannot set RSS key, err %d aq_err %s\n",
  6754. status, ice_aq_str(hw->adminq.sq_last_status));
  6755. return status;
  6756. }
  6757. /**
  6758. * ice_get_rss_lut - Get RSS LUT
  6759. * @vsi: Pointer to VSI structure
  6760. * @lut: Buffer to store the lookup table entries
  6761. * @lut_size: Size of buffer to store the lookup table entries
  6762. *
  6763. * Returns 0 on success, negative on failure
  6764. */
  6765. int ice_get_rss_lut(struct ice_vsi *vsi, u8 *lut, u16 lut_size)
  6766. {
  6767. struct ice_aq_get_set_rss_lut_params params = {};
  6768. struct ice_hw *hw = &vsi->back->hw;
  6769. int status;
  6770. if (!lut)
  6771. return -EINVAL;
  6772. params.vsi_handle = vsi->idx;
  6773. params.lut_size = lut_size;
  6774. params.lut_type = vsi->rss_lut_type;
  6775. params.lut = lut;
  6776. status = ice_aq_get_rss_lut(hw, &params);
  6777. if (status)
  6778. dev_err(ice_pf_to_dev(vsi->back), "Cannot get RSS lut, err %d aq_err %s\n",
  6779. status, ice_aq_str(hw->adminq.sq_last_status));
  6780. return status;
  6781. }
  6782. /**
  6783. * ice_get_rss_key - Get RSS key
  6784. * @vsi: Pointer to VSI structure
  6785. * @seed: Buffer to store the key in
  6786. *
  6787. * Returns 0 on success, negative on failure
  6788. */
  6789. int ice_get_rss_key(struct ice_vsi *vsi, u8 *seed)
  6790. {
  6791. struct ice_hw *hw = &vsi->back->hw;
  6792. int status;
  6793. if (!seed)
  6794. return -EINVAL;
  6795. status = ice_aq_get_rss_key(hw, vsi->idx, (struct ice_aqc_get_set_rss_keys *)seed);
  6796. if (status)
  6797. dev_err(ice_pf_to_dev(vsi->back), "Cannot get RSS key, err %d aq_err %s\n",
  6798. status, ice_aq_str(hw->adminq.sq_last_status));
  6799. return status;
  6800. }
  6801. /**
  6802. * ice_set_rss_hfunc - Set RSS HASH function
  6803. * @vsi: Pointer to VSI structure
  6804. * @hfunc: hash function (ICE_AQ_VSI_Q_OPT_RSS_*)
  6805. *
  6806. * Returns 0 on success, negative on failure
  6807. */
  6808. int ice_set_rss_hfunc(struct ice_vsi *vsi, u8 hfunc)
  6809. {
  6810. struct ice_hw *hw = &vsi->back->hw;
  6811. struct ice_vsi_ctx *ctx;
  6812. bool symm;
  6813. int err;
  6814. if (hfunc == vsi->rss_hfunc)
  6815. return 0;
  6816. if (hfunc != ICE_AQ_VSI_Q_OPT_RSS_HASH_TPLZ &&
  6817. hfunc != ICE_AQ_VSI_Q_OPT_RSS_HASH_SYM_TPLZ)
  6818. return -EOPNOTSUPP;
  6819. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  6820. if (!ctx)
  6821. return -ENOMEM;
  6822. ctx->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_Q_OPT_VALID);
  6823. ctx->info.q_opt_rss = vsi->info.q_opt_rss;
  6824. ctx->info.q_opt_rss &= ~ICE_AQ_VSI_Q_OPT_RSS_HASH_M;
  6825. ctx->info.q_opt_rss |=
  6826. FIELD_PREP(ICE_AQ_VSI_Q_OPT_RSS_HASH_M, hfunc);
  6827. ctx->info.q_opt_tc = vsi->info.q_opt_tc;
  6828. ctx->info.q_opt_flags = vsi->info.q_opt_rss;
  6829. err = ice_update_vsi(hw, vsi->idx, ctx, NULL);
  6830. if (err) {
  6831. dev_err(ice_pf_to_dev(vsi->back), "Failed to configure RSS hash for VSI %d, error %d\n",
  6832. vsi->vsi_num, err);
  6833. } else {
  6834. vsi->info.q_opt_rss = ctx->info.q_opt_rss;
  6835. vsi->rss_hfunc = hfunc;
  6836. netdev_info(vsi->netdev, "Hash function set to: %sToeplitz\n",
  6837. hfunc == ICE_AQ_VSI_Q_OPT_RSS_HASH_SYM_TPLZ ?
  6838. "Symmetric " : "");
  6839. }
  6840. kfree(ctx);
  6841. if (err)
  6842. return err;
  6843. /* Fix the symmetry setting for all existing RSS configurations */
  6844. symm = !!(hfunc == ICE_AQ_VSI_Q_OPT_RSS_HASH_SYM_TPLZ);
  6845. return ice_set_rss_cfg_symm(hw, vsi, symm);
  6846. }
  6847. /**
  6848. * ice_bridge_getlink - Get the hardware bridge mode
  6849. * @skb: skb buff
  6850. * @pid: process ID
  6851. * @seq: RTNL message seq
  6852. * @dev: the netdev being configured
  6853. * @filter_mask: filter mask passed in
  6854. * @nlflags: netlink flags passed in
  6855. *
  6856. * Return the bridge mode (VEB/VEPA)
  6857. */
  6858. static int
  6859. ice_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
  6860. struct net_device *dev, u32 filter_mask, int nlflags)
  6861. {
  6862. struct ice_netdev_priv *np = netdev_priv(dev);
  6863. struct ice_vsi *vsi = np->vsi;
  6864. struct ice_pf *pf = vsi->back;
  6865. u16 bmode;
  6866. bmode = pf->first_sw->bridge_mode;
  6867. return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bmode, 0, 0, nlflags,
  6868. filter_mask, NULL);
  6869. }
  6870. /**
  6871. * ice_vsi_update_bridge_mode - Update VSI for switching bridge mode (VEB/VEPA)
  6872. * @vsi: Pointer to VSI structure
  6873. * @bmode: Hardware bridge mode (VEB/VEPA)
  6874. *
  6875. * Returns 0 on success, negative on failure
  6876. */
  6877. static int ice_vsi_update_bridge_mode(struct ice_vsi *vsi, u16 bmode)
  6878. {
  6879. struct ice_aqc_vsi_props *vsi_props;
  6880. struct ice_hw *hw = &vsi->back->hw;
  6881. struct ice_vsi_ctx *ctxt;
  6882. int ret;
  6883. vsi_props = &vsi->info;
  6884. ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
  6885. if (!ctxt)
  6886. return -ENOMEM;
  6887. ctxt->info = vsi->info;
  6888. if (bmode == BRIDGE_MODE_VEB)
  6889. /* change from VEPA to VEB mode */
  6890. ctxt->info.sw_flags |= ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
  6891. else
  6892. /* change from VEB to VEPA mode */
  6893. ctxt->info.sw_flags &= ~ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
  6894. ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SW_VALID);
  6895. ret = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
  6896. if (ret) {
  6897. dev_err(ice_pf_to_dev(vsi->back), "update VSI for bridge mode failed, bmode = %d err %d aq_err %s\n",
  6898. bmode, ret, ice_aq_str(hw->adminq.sq_last_status));
  6899. goto out;
  6900. }
  6901. /* Update sw flags for book keeping */
  6902. vsi_props->sw_flags = ctxt->info.sw_flags;
  6903. out:
  6904. kfree(ctxt);
  6905. return ret;
  6906. }
  6907. /**
  6908. * ice_bridge_setlink - Set the hardware bridge mode
  6909. * @dev: the netdev being configured
  6910. * @nlh: RTNL message
  6911. * @flags: bridge setlink flags
  6912. * @extack: netlink extended ack
  6913. *
  6914. * Sets the bridge mode (VEB/VEPA) of the switch to which the netdev (VSI) is
  6915. * hooked up to. Iterates through the PF VSI list and sets the loopback mode (if
  6916. * not already set for all VSIs connected to this switch. And also update the
  6917. * unicast switch filter rules for the corresponding switch of the netdev.
  6918. */
  6919. static int
  6920. ice_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
  6921. u16 __always_unused flags,
  6922. struct netlink_ext_ack __always_unused *extack)
  6923. {
  6924. struct ice_netdev_priv *np = netdev_priv(dev);
  6925. struct ice_pf *pf = np->vsi->back;
  6926. struct nlattr *attr, *br_spec;
  6927. struct ice_hw *hw = &pf->hw;
  6928. struct ice_sw *pf_sw;
  6929. int rem, v, err = 0;
  6930. pf_sw = pf->first_sw;
  6931. /* find the attribute in the netlink message */
  6932. br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
  6933. if (!br_spec)
  6934. return -EINVAL;
  6935. nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) {
  6936. __u16 mode = nla_get_u16(attr);
  6937. if (mode != BRIDGE_MODE_VEPA && mode != BRIDGE_MODE_VEB)
  6938. return -EINVAL;
  6939. /* Continue if bridge mode is not being flipped */
  6940. if (mode == pf_sw->bridge_mode)
  6941. continue;
  6942. /* Iterates through the PF VSI list and update the loopback
  6943. * mode of the VSI
  6944. */
  6945. ice_for_each_vsi(pf, v) {
  6946. if (!pf->vsi[v])
  6947. continue;
  6948. err = ice_vsi_update_bridge_mode(pf->vsi[v], mode);
  6949. if (err)
  6950. return err;
  6951. }
  6952. hw->evb_veb = (mode == BRIDGE_MODE_VEB);
  6953. /* Update the unicast switch filter rules for the corresponding
  6954. * switch of the netdev
  6955. */
  6956. err = ice_update_sw_rule_bridge_mode(hw);
  6957. if (err) {
  6958. netdev_err(dev, "switch rule update failed, mode = %d err %d aq_err %s\n",
  6959. mode, err,
  6960. ice_aq_str(hw->adminq.sq_last_status));
  6961. /* revert hw->evb_veb */
  6962. hw->evb_veb = (pf_sw->bridge_mode == BRIDGE_MODE_VEB);
  6963. return err;
  6964. }
  6965. pf_sw->bridge_mode = mode;
  6966. }
  6967. return 0;
  6968. }
  6969. /**
  6970. * ice_tx_timeout - Respond to a Tx Hang
  6971. * @netdev: network interface device structure
  6972. * @txqueue: Tx queue
  6973. */
  6974. void ice_tx_timeout(struct net_device *netdev, unsigned int txqueue)
  6975. {
  6976. struct ice_netdev_priv *np = netdev_priv(netdev);
  6977. struct ice_tx_ring *tx_ring = NULL;
  6978. struct ice_vsi *vsi = np->vsi;
  6979. struct ice_pf *pf = vsi->back;
  6980. u32 i;
  6981. pf->tx_timeout_count++;
  6982. /* Check if PFC is enabled for the TC to which the queue belongs
  6983. * to. If yes then Tx timeout is not caused by a hung queue, no
  6984. * need to reset and rebuild
  6985. */
  6986. if (ice_is_pfc_causing_hung_q(pf, txqueue)) {
  6987. dev_info(ice_pf_to_dev(pf), "Fake Tx hang detected on queue %u, timeout caused by PFC storm\n",
  6988. txqueue);
  6989. return;
  6990. }
  6991. /* now that we have an index, find the tx_ring struct */
  6992. ice_for_each_txq(vsi, i)
  6993. if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
  6994. if (txqueue == vsi->tx_rings[i]->q_index) {
  6995. tx_ring = vsi->tx_rings[i];
  6996. break;
  6997. }
  6998. /* Reset recovery level if enough time has elapsed after last timeout.
  6999. * Also ensure no new reset action happens before next timeout period.
  7000. */
  7001. if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ * 20)))
  7002. pf->tx_timeout_recovery_level = 1;
  7003. else if (time_before(jiffies, (pf->tx_timeout_last_recovery +
  7004. netdev->watchdog_timeo)))
  7005. return;
  7006. if (tx_ring) {
  7007. struct ice_hw *hw = &pf->hw;
  7008. u32 head, val = 0;
  7009. head = FIELD_GET(QTX_COMM_HEAD_HEAD_M,
  7010. rd32(hw, QTX_COMM_HEAD(vsi->txq_map[txqueue])));
  7011. /* Read interrupt register */
  7012. val = rd32(hw, GLINT_DYN_CTL(tx_ring->q_vector->reg_idx));
  7013. netdev_info(netdev, "tx_timeout: VSI_num: %d, Q %u, NTC: 0x%x, HW_HEAD: 0x%x, NTU: 0x%x, INT: 0x%x\n",
  7014. vsi->vsi_num, txqueue, tx_ring->next_to_clean,
  7015. head, tx_ring->next_to_use, val);
  7016. }
  7017. pf->tx_timeout_last_recovery = jiffies;
  7018. netdev_info(netdev, "tx_timeout recovery level %d, txqueue %u\n",
  7019. pf->tx_timeout_recovery_level, txqueue);
  7020. switch (pf->tx_timeout_recovery_level) {
  7021. case 1:
  7022. set_bit(ICE_PFR_REQ, pf->state);
  7023. break;
  7024. case 2:
  7025. set_bit(ICE_CORER_REQ, pf->state);
  7026. break;
  7027. case 3:
  7028. set_bit(ICE_GLOBR_REQ, pf->state);
  7029. break;
  7030. default:
  7031. netdev_err(netdev, "tx_timeout recovery unsuccessful, device is in unrecoverable state.\n");
  7032. set_bit(ICE_DOWN, pf->state);
  7033. set_bit(ICE_VSI_NEEDS_RESTART, vsi->state);
  7034. set_bit(ICE_SERVICE_DIS, pf->state);
  7035. break;
  7036. }
  7037. ice_service_task_schedule(pf);
  7038. pf->tx_timeout_recovery_level++;
  7039. }
  7040. /**
  7041. * ice_setup_tc_cls_flower - flower classifier offloads
  7042. * @np: net device to configure
  7043. * @filter_dev: device on which filter is added
  7044. * @cls_flower: offload data
  7045. */
  7046. static int
  7047. ice_setup_tc_cls_flower(struct ice_netdev_priv *np,
  7048. struct net_device *filter_dev,
  7049. struct flow_cls_offload *cls_flower)
  7050. {
  7051. struct ice_vsi *vsi = np->vsi;
  7052. if (cls_flower->common.chain_index)
  7053. return -EOPNOTSUPP;
  7054. switch (cls_flower->command) {
  7055. case FLOW_CLS_REPLACE:
  7056. return ice_add_cls_flower(filter_dev, vsi, cls_flower);
  7057. case FLOW_CLS_DESTROY:
  7058. return ice_del_cls_flower(vsi, cls_flower);
  7059. default:
  7060. return -EINVAL;
  7061. }
  7062. }
  7063. /**
  7064. * ice_setup_tc_block_cb - callback handler registered for TC block
  7065. * @type: TC SETUP type
  7066. * @type_data: TC flower offload data that contains user input
  7067. * @cb_priv: netdev private data
  7068. */
  7069. static int
  7070. ice_setup_tc_block_cb(enum tc_setup_type type, void *type_data, void *cb_priv)
  7071. {
  7072. struct ice_netdev_priv *np = cb_priv;
  7073. switch (type) {
  7074. case TC_SETUP_CLSFLOWER:
  7075. return ice_setup_tc_cls_flower(np, np->vsi->netdev,
  7076. type_data);
  7077. default:
  7078. return -EOPNOTSUPP;
  7079. }
  7080. }
  7081. /**
  7082. * ice_validate_mqprio_qopt - Validate TCF input parameters
  7083. * @vsi: Pointer to VSI
  7084. * @mqprio_qopt: input parameters for mqprio queue configuration
  7085. *
  7086. * This function validates MQPRIO params, such as qcount (power of 2 wherever
  7087. * needed), and make sure user doesn't specify qcount and BW rate limit
  7088. * for TCs, which are more than "num_tc"
  7089. */
  7090. static int
  7091. ice_validate_mqprio_qopt(struct ice_vsi *vsi,
  7092. struct tc_mqprio_qopt_offload *mqprio_qopt)
  7093. {
  7094. int non_power_of_2_qcount = 0;
  7095. struct ice_pf *pf = vsi->back;
  7096. int max_rss_q_cnt = 0;
  7097. u64 sum_min_rate = 0;
  7098. struct device *dev;
  7099. int i, speed;
  7100. u8 num_tc;
  7101. if (vsi->type != ICE_VSI_PF)
  7102. return -EINVAL;
  7103. if (mqprio_qopt->qopt.offset[0] != 0 ||
  7104. mqprio_qopt->qopt.num_tc < 1 ||
  7105. mqprio_qopt->qopt.num_tc > ICE_CHNL_MAX_TC)
  7106. return -EINVAL;
  7107. dev = ice_pf_to_dev(pf);
  7108. vsi->ch_rss_size = 0;
  7109. num_tc = mqprio_qopt->qopt.num_tc;
  7110. speed = ice_get_link_speed_kbps(vsi);
  7111. for (i = 0; num_tc; i++) {
  7112. int qcount = mqprio_qopt->qopt.count[i];
  7113. u64 max_rate, min_rate, rem;
  7114. if (!qcount)
  7115. return -EINVAL;
  7116. if (is_power_of_2(qcount)) {
  7117. if (non_power_of_2_qcount &&
  7118. qcount > non_power_of_2_qcount) {
  7119. dev_err(dev, "qcount[%d] cannot be greater than non power of 2 qcount[%d]\n",
  7120. qcount, non_power_of_2_qcount);
  7121. return -EINVAL;
  7122. }
  7123. if (qcount > max_rss_q_cnt)
  7124. max_rss_q_cnt = qcount;
  7125. } else {
  7126. if (non_power_of_2_qcount &&
  7127. qcount != non_power_of_2_qcount) {
  7128. dev_err(dev, "Only one non power of 2 qcount allowed[%d,%d]\n",
  7129. qcount, non_power_of_2_qcount);
  7130. return -EINVAL;
  7131. }
  7132. if (qcount < max_rss_q_cnt) {
  7133. dev_err(dev, "non power of 2 qcount[%d] cannot be less than other qcount[%d]\n",
  7134. qcount, max_rss_q_cnt);
  7135. return -EINVAL;
  7136. }
  7137. max_rss_q_cnt = qcount;
  7138. non_power_of_2_qcount = qcount;
  7139. }
  7140. /* TC command takes input in K/N/Gbps or K/M/Gbit etc but
  7141. * converts the bandwidth rate limit into Bytes/s when
  7142. * passing it down to the driver. So convert input bandwidth
  7143. * from Bytes/s to Kbps
  7144. */
  7145. max_rate = mqprio_qopt->max_rate[i];
  7146. max_rate = div_u64(max_rate, ICE_BW_KBPS_DIVISOR);
  7147. /* min_rate is minimum guaranteed rate and it can't be zero */
  7148. min_rate = mqprio_qopt->min_rate[i];
  7149. min_rate = div_u64(min_rate, ICE_BW_KBPS_DIVISOR);
  7150. sum_min_rate += min_rate;
  7151. if (min_rate && min_rate < ICE_MIN_BW_LIMIT) {
  7152. dev_err(dev, "TC%d: min_rate(%llu Kbps) < %u Kbps\n", i,
  7153. min_rate, ICE_MIN_BW_LIMIT);
  7154. return -EINVAL;
  7155. }
  7156. if (max_rate && max_rate > speed) {
  7157. dev_err(dev, "TC%d: max_rate(%llu Kbps) > link speed of %u Kbps\n",
  7158. i, max_rate, speed);
  7159. return -EINVAL;
  7160. }
  7161. iter_div_u64_rem(min_rate, ICE_MIN_BW_LIMIT, &rem);
  7162. if (rem) {
  7163. dev_err(dev, "TC%d: Min Rate not multiple of %u Kbps",
  7164. i, ICE_MIN_BW_LIMIT);
  7165. return -EINVAL;
  7166. }
  7167. iter_div_u64_rem(max_rate, ICE_MIN_BW_LIMIT, &rem);
  7168. if (rem) {
  7169. dev_err(dev, "TC%d: Max Rate not multiple of %u Kbps",
  7170. i, ICE_MIN_BW_LIMIT);
  7171. return -EINVAL;
  7172. }
  7173. /* min_rate can't be more than max_rate, except when max_rate
  7174. * is zero (implies max_rate sought is max line rate). In such
  7175. * a case min_rate can be more than max.
  7176. */
  7177. if (max_rate && min_rate > max_rate) {
  7178. dev_err(dev, "min_rate %llu Kbps can't be more than max_rate %llu Kbps\n",
  7179. min_rate, max_rate);
  7180. return -EINVAL;
  7181. }
  7182. if (i >= mqprio_qopt->qopt.num_tc - 1)
  7183. break;
  7184. if (mqprio_qopt->qopt.offset[i + 1] !=
  7185. (mqprio_qopt->qopt.offset[i] + qcount))
  7186. return -EINVAL;
  7187. }
  7188. if (vsi->num_rxq <
  7189. (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
  7190. return -EINVAL;
  7191. if (vsi->num_txq <
  7192. (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
  7193. return -EINVAL;
  7194. if (sum_min_rate && sum_min_rate > (u64)speed) {
  7195. dev_err(dev, "Invalid min Tx rate(%llu) Kbps > speed (%u) Kbps specified\n",
  7196. sum_min_rate, speed);
  7197. return -EINVAL;
  7198. }
  7199. /* make sure vsi->ch_rss_size is set correctly based on TC's qcount */
  7200. vsi->ch_rss_size = max_rss_q_cnt;
  7201. return 0;
  7202. }
  7203. /**
  7204. * ice_add_vsi_to_fdir - add a VSI to the flow director group for PF
  7205. * @pf: ptr to PF device
  7206. * @vsi: ptr to VSI
  7207. */
  7208. static int ice_add_vsi_to_fdir(struct ice_pf *pf, struct ice_vsi *vsi)
  7209. {
  7210. struct device *dev = ice_pf_to_dev(pf);
  7211. bool added = false;
  7212. struct ice_hw *hw;
  7213. int flow;
  7214. if (!(vsi->num_gfltr || vsi->num_bfltr))
  7215. return -EINVAL;
  7216. hw = &pf->hw;
  7217. for (flow = 0; flow < ICE_FLTR_PTYPE_MAX; flow++) {
  7218. struct ice_fd_hw_prof *prof;
  7219. int tun, status;
  7220. u64 entry_h;
  7221. if (!(hw->fdir_prof && hw->fdir_prof[flow] &&
  7222. hw->fdir_prof[flow]->cnt))
  7223. continue;
  7224. for (tun = 0; tun < ICE_FD_HW_SEG_MAX; tun++) {
  7225. enum ice_flow_priority prio;
  7226. /* add this VSI to FDir profile for this flow */
  7227. prio = ICE_FLOW_PRIO_NORMAL;
  7228. prof = hw->fdir_prof[flow];
  7229. status = ice_flow_add_entry(hw, ICE_BLK_FD,
  7230. prof->prof_id[tun],
  7231. prof->vsi_h[0], vsi->idx,
  7232. prio, prof->fdir_seg[tun],
  7233. &entry_h);
  7234. if (status) {
  7235. dev_err(dev, "channel VSI idx %d, not able to add to group %d\n",
  7236. vsi->idx, flow);
  7237. continue;
  7238. }
  7239. prof->entry_h[prof->cnt][tun] = entry_h;
  7240. }
  7241. /* store VSI for filter replay and delete */
  7242. prof->vsi_h[prof->cnt] = vsi->idx;
  7243. prof->cnt++;
  7244. added = true;
  7245. dev_dbg(dev, "VSI idx %d added to fdir group %d\n", vsi->idx,
  7246. flow);
  7247. }
  7248. if (!added)
  7249. dev_dbg(dev, "VSI idx %d not added to fdir groups\n", vsi->idx);
  7250. return 0;
  7251. }
  7252. /**
  7253. * ice_add_channel - add a channel by adding VSI
  7254. * @pf: ptr to PF device
  7255. * @sw_id: underlying HW switching element ID
  7256. * @ch: ptr to channel structure
  7257. *
  7258. * Add a channel (VSI) using add_vsi and queue_map
  7259. */
  7260. static int ice_add_channel(struct ice_pf *pf, u16 sw_id, struct ice_channel *ch)
  7261. {
  7262. struct device *dev = ice_pf_to_dev(pf);
  7263. struct ice_vsi *vsi;
  7264. if (ch->type != ICE_VSI_CHNL) {
  7265. dev_err(dev, "add new VSI failed, ch->type %d\n", ch->type);
  7266. return -EINVAL;
  7267. }
  7268. vsi = ice_chnl_vsi_setup(pf, pf->hw.port_info, ch);
  7269. if (!vsi || vsi->type != ICE_VSI_CHNL) {
  7270. dev_err(dev, "create chnl VSI failure\n");
  7271. return -EINVAL;
  7272. }
  7273. ice_add_vsi_to_fdir(pf, vsi);
  7274. ch->sw_id = sw_id;
  7275. ch->vsi_num = vsi->vsi_num;
  7276. ch->info.mapping_flags = vsi->info.mapping_flags;
  7277. ch->ch_vsi = vsi;
  7278. /* set the back pointer of channel for newly created VSI */
  7279. vsi->ch = ch;
  7280. memcpy(&ch->info.q_mapping, &vsi->info.q_mapping,
  7281. sizeof(vsi->info.q_mapping));
  7282. memcpy(&ch->info.tc_mapping, vsi->info.tc_mapping,
  7283. sizeof(vsi->info.tc_mapping));
  7284. return 0;
  7285. }
  7286. /**
  7287. * ice_chnl_cfg_res
  7288. * @vsi: the VSI being setup
  7289. * @ch: ptr to channel structure
  7290. *
  7291. * Configure channel specific resources such as rings, vector.
  7292. */
  7293. static void ice_chnl_cfg_res(struct ice_vsi *vsi, struct ice_channel *ch)
  7294. {
  7295. int i;
  7296. for (i = 0; i < ch->num_txq; i++) {
  7297. struct ice_q_vector *tx_q_vector, *rx_q_vector;
  7298. struct ice_ring_container *rc;
  7299. struct ice_tx_ring *tx_ring;
  7300. struct ice_rx_ring *rx_ring;
  7301. tx_ring = vsi->tx_rings[ch->base_q + i];
  7302. rx_ring = vsi->rx_rings[ch->base_q + i];
  7303. if (!tx_ring || !rx_ring)
  7304. continue;
  7305. /* setup ring being channel enabled */
  7306. tx_ring->ch = ch;
  7307. rx_ring->ch = ch;
  7308. /* following code block sets up vector specific attributes */
  7309. tx_q_vector = tx_ring->q_vector;
  7310. rx_q_vector = rx_ring->q_vector;
  7311. if (!tx_q_vector && !rx_q_vector)
  7312. continue;
  7313. if (tx_q_vector) {
  7314. tx_q_vector->ch = ch;
  7315. /* setup Tx and Rx ITR setting if DIM is off */
  7316. rc = &tx_q_vector->tx;
  7317. if (!ITR_IS_DYNAMIC(rc))
  7318. ice_write_itr(rc, rc->itr_setting);
  7319. }
  7320. if (rx_q_vector) {
  7321. rx_q_vector->ch = ch;
  7322. /* setup Tx and Rx ITR setting if DIM is off */
  7323. rc = &rx_q_vector->rx;
  7324. if (!ITR_IS_DYNAMIC(rc))
  7325. ice_write_itr(rc, rc->itr_setting);
  7326. }
  7327. }
  7328. /* it is safe to assume that, if channel has non-zero num_t[r]xq, then
  7329. * GLINT_ITR register would have written to perform in-context
  7330. * update, hence perform flush
  7331. */
  7332. if (ch->num_txq || ch->num_rxq)
  7333. ice_flush(&vsi->back->hw);
  7334. }
  7335. /**
  7336. * ice_cfg_chnl_all_res - configure channel resources
  7337. * @vsi: pte to main_vsi
  7338. * @ch: ptr to channel structure
  7339. *
  7340. * This function configures channel specific resources such as flow-director
  7341. * counter index, and other resources such as queues, vectors, ITR settings
  7342. */
  7343. static void
  7344. ice_cfg_chnl_all_res(struct ice_vsi *vsi, struct ice_channel *ch)
  7345. {
  7346. /* configure channel (aka ADQ) resources such as queues, vectors,
  7347. * ITR settings for channel specific vectors and anything else
  7348. */
  7349. ice_chnl_cfg_res(vsi, ch);
  7350. }
  7351. /**
  7352. * ice_setup_hw_channel - setup new channel
  7353. * @pf: ptr to PF device
  7354. * @vsi: the VSI being setup
  7355. * @ch: ptr to channel structure
  7356. * @sw_id: underlying HW switching element ID
  7357. * @type: type of channel to be created (VMDq2/VF)
  7358. *
  7359. * Setup new channel (VSI) based on specified type (VMDq2/VF)
  7360. * and configures Tx rings accordingly
  7361. */
  7362. static int
  7363. ice_setup_hw_channel(struct ice_pf *pf, struct ice_vsi *vsi,
  7364. struct ice_channel *ch, u16 sw_id, u8 type)
  7365. {
  7366. struct device *dev = ice_pf_to_dev(pf);
  7367. int ret;
  7368. ch->base_q = vsi->next_base_q;
  7369. ch->type = type;
  7370. ret = ice_add_channel(pf, sw_id, ch);
  7371. if (ret) {
  7372. dev_err(dev, "failed to add_channel using sw_id %u\n", sw_id);
  7373. return ret;
  7374. }
  7375. /* configure/setup ADQ specific resources */
  7376. ice_cfg_chnl_all_res(vsi, ch);
  7377. /* make sure to update the next_base_q so that subsequent channel's
  7378. * (aka ADQ) VSI queue map is correct
  7379. */
  7380. vsi->next_base_q = vsi->next_base_q + ch->num_rxq;
  7381. dev_dbg(dev, "added channel: vsi_num %u, num_rxq %u\n", ch->vsi_num,
  7382. ch->num_rxq);
  7383. return 0;
  7384. }
  7385. /**
  7386. * ice_setup_channel - setup new channel using uplink element
  7387. * @pf: ptr to PF device
  7388. * @vsi: the VSI being setup
  7389. * @ch: ptr to channel structure
  7390. *
  7391. * Setup new channel (VSI) based on specified type (VMDq2/VF)
  7392. * and uplink switching element
  7393. */
  7394. static bool
  7395. ice_setup_channel(struct ice_pf *pf, struct ice_vsi *vsi,
  7396. struct ice_channel *ch)
  7397. {
  7398. struct device *dev = ice_pf_to_dev(pf);
  7399. u16 sw_id;
  7400. int ret;
  7401. if (vsi->type != ICE_VSI_PF) {
  7402. dev_err(dev, "unsupported parent VSI type(%d)\n", vsi->type);
  7403. return false;
  7404. }
  7405. sw_id = pf->first_sw->sw_id;
  7406. /* create channel (VSI) */
  7407. ret = ice_setup_hw_channel(pf, vsi, ch, sw_id, ICE_VSI_CHNL);
  7408. if (ret) {
  7409. dev_err(dev, "failed to setup hw_channel\n");
  7410. return false;
  7411. }
  7412. dev_dbg(dev, "successfully created channel()\n");
  7413. return ch->ch_vsi ? true : false;
  7414. }
  7415. /**
  7416. * ice_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
  7417. * @vsi: VSI to be configured
  7418. * @max_tx_rate: max Tx rate in Kbps to be configured as maximum BW limit
  7419. * @min_tx_rate: min Tx rate in Kbps to be configured as minimum BW limit
  7420. */
  7421. static int
  7422. ice_set_bw_limit(struct ice_vsi *vsi, u64 max_tx_rate, u64 min_tx_rate)
  7423. {
  7424. int err;
  7425. err = ice_set_min_bw_limit(vsi, min_tx_rate);
  7426. if (err)
  7427. return err;
  7428. return ice_set_max_bw_limit(vsi, max_tx_rate);
  7429. }
  7430. /**
  7431. * ice_create_q_channel - function to create channel
  7432. * @vsi: VSI to be configured
  7433. * @ch: ptr to channel (it contains channel specific params)
  7434. *
  7435. * This function creates channel (VSI) using num_queues specified by user,
  7436. * reconfigs RSS if needed.
  7437. */
  7438. static int ice_create_q_channel(struct ice_vsi *vsi, struct ice_channel *ch)
  7439. {
  7440. struct ice_pf *pf = vsi->back;
  7441. struct device *dev;
  7442. if (!ch)
  7443. return -EINVAL;
  7444. dev = ice_pf_to_dev(pf);
  7445. if (!ch->num_txq || !ch->num_rxq) {
  7446. dev_err(dev, "Invalid num_queues requested: %d\n", ch->num_rxq);
  7447. return -EINVAL;
  7448. }
  7449. if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_txq) {
  7450. dev_err(dev, "cnt_q_avail (%u) less than num_queues %d\n",
  7451. vsi->cnt_q_avail, ch->num_txq);
  7452. return -EINVAL;
  7453. }
  7454. if (!ice_setup_channel(pf, vsi, ch)) {
  7455. dev_info(dev, "Failed to setup channel\n");
  7456. return -EINVAL;
  7457. }
  7458. /* configure BW rate limit */
  7459. if (ch->ch_vsi && (ch->max_tx_rate || ch->min_tx_rate)) {
  7460. int ret;
  7461. ret = ice_set_bw_limit(ch->ch_vsi, ch->max_tx_rate,
  7462. ch->min_tx_rate);
  7463. if (ret)
  7464. dev_err(dev, "failed to set Tx rate of %llu Kbps for VSI(%u)\n",
  7465. ch->max_tx_rate, ch->ch_vsi->vsi_num);
  7466. else
  7467. dev_dbg(dev, "set Tx rate of %llu Kbps for VSI(%u)\n",
  7468. ch->max_tx_rate, ch->ch_vsi->vsi_num);
  7469. }
  7470. vsi->cnt_q_avail -= ch->num_txq;
  7471. return 0;
  7472. }
  7473. /**
  7474. * ice_rem_all_chnl_fltrs - removes all channel filters
  7475. * @pf: ptr to PF, TC-flower based filter are tracked at PF level
  7476. *
  7477. * Remove all advanced switch filters only if they are channel specific
  7478. * tc-flower based filter
  7479. */
  7480. static void ice_rem_all_chnl_fltrs(struct ice_pf *pf)
  7481. {
  7482. struct ice_tc_flower_fltr *fltr;
  7483. struct hlist_node *node;
  7484. /* to remove all channel filters, iterate an ordered list of filters */
  7485. hlist_for_each_entry_safe(fltr, node,
  7486. &pf->tc_flower_fltr_list,
  7487. tc_flower_node) {
  7488. struct ice_rule_query_data rule;
  7489. int status;
  7490. /* for now process only channel specific filters */
  7491. if (!ice_is_chnl_fltr(fltr))
  7492. continue;
  7493. rule.rid = fltr->rid;
  7494. rule.rule_id = fltr->rule_id;
  7495. rule.vsi_handle = fltr->dest_vsi_handle;
  7496. status = ice_rem_adv_rule_by_id(&pf->hw, &rule);
  7497. if (status) {
  7498. if (status == -ENOENT)
  7499. dev_dbg(ice_pf_to_dev(pf), "TC flower filter (rule_id %u) does not exist\n",
  7500. rule.rule_id);
  7501. else
  7502. dev_err(ice_pf_to_dev(pf), "failed to delete TC flower filter, status %d\n",
  7503. status);
  7504. } else if (fltr->dest_vsi) {
  7505. /* update advanced switch filter count */
  7506. if (fltr->dest_vsi->type == ICE_VSI_CHNL) {
  7507. u32 flags = fltr->flags;
  7508. fltr->dest_vsi->num_chnl_fltr--;
  7509. if (flags & (ICE_TC_FLWR_FIELD_DST_MAC |
  7510. ICE_TC_FLWR_FIELD_ENC_DST_MAC))
  7511. pf->num_dmac_chnl_fltrs--;
  7512. }
  7513. }
  7514. hlist_del(&fltr->tc_flower_node);
  7515. kfree(fltr);
  7516. }
  7517. }
  7518. /**
  7519. * ice_remove_q_channels - Remove queue channels for the TCs
  7520. * @vsi: VSI to be configured
  7521. * @rem_fltr: delete advanced switch filter or not
  7522. *
  7523. * Remove queue channels for the TCs
  7524. */
  7525. static void ice_remove_q_channels(struct ice_vsi *vsi, bool rem_fltr)
  7526. {
  7527. struct ice_channel *ch, *ch_tmp;
  7528. struct ice_pf *pf = vsi->back;
  7529. int i;
  7530. /* remove all tc-flower based filter if they are channel filters only */
  7531. if (rem_fltr)
  7532. ice_rem_all_chnl_fltrs(pf);
  7533. /* remove ntuple filters since queue configuration is being changed */
  7534. if (vsi->netdev->features & NETIF_F_NTUPLE) {
  7535. struct ice_hw *hw = &pf->hw;
  7536. mutex_lock(&hw->fdir_fltr_lock);
  7537. ice_fdir_del_all_fltrs(vsi);
  7538. mutex_unlock(&hw->fdir_fltr_lock);
  7539. }
  7540. /* perform cleanup for channels if they exist */
  7541. list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
  7542. struct ice_vsi *ch_vsi;
  7543. list_del(&ch->list);
  7544. ch_vsi = ch->ch_vsi;
  7545. if (!ch_vsi) {
  7546. kfree(ch);
  7547. continue;
  7548. }
  7549. /* Reset queue contexts */
  7550. for (i = 0; i < ch->num_rxq; i++) {
  7551. struct ice_tx_ring *tx_ring;
  7552. struct ice_rx_ring *rx_ring;
  7553. tx_ring = vsi->tx_rings[ch->base_q + i];
  7554. rx_ring = vsi->rx_rings[ch->base_q + i];
  7555. if (tx_ring) {
  7556. tx_ring->ch = NULL;
  7557. if (tx_ring->q_vector)
  7558. tx_ring->q_vector->ch = NULL;
  7559. }
  7560. if (rx_ring) {
  7561. rx_ring->ch = NULL;
  7562. if (rx_ring->q_vector)
  7563. rx_ring->q_vector->ch = NULL;
  7564. }
  7565. }
  7566. /* Release FD resources for the channel VSI */
  7567. ice_fdir_rem_adq_chnl(&pf->hw, ch->ch_vsi->idx);
  7568. /* clear the VSI from scheduler tree */
  7569. ice_rm_vsi_lan_cfg(ch->ch_vsi->port_info, ch->ch_vsi->idx);
  7570. /* Delete VSI from FW, PF and HW VSI arrays */
  7571. ice_vsi_delete(ch->ch_vsi);
  7572. /* free the channel */
  7573. kfree(ch);
  7574. }
  7575. /* clear the channel VSI map which is stored in main VSI */
  7576. ice_for_each_chnl_tc(i)
  7577. vsi->tc_map_vsi[i] = NULL;
  7578. /* reset main VSI's all TC information */
  7579. vsi->all_enatc = 0;
  7580. vsi->all_numtc = 0;
  7581. }
  7582. /**
  7583. * ice_rebuild_channels - rebuild channel
  7584. * @pf: ptr to PF
  7585. *
  7586. * Recreate channel VSIs and replay filters
  7587. */
  7588. static int ice_rebuild_channels(struct ice_pf *pf)
  7589. {
  7590. struct device *dev = ice_pf_to_dev(pf);
  7591. struct ice_vsi *main_vsi;
  7592. bool rem_adv_fltr = true;
  7593. struct ice_channel *ch;
  7594. struct ice_vsi *vsi;
  7595. int tc_idx = 1;
  7596. int i, err;
  7597. main_vsi = ice_get_main_vsi(pf);
  7598. if (!main_vsi)
  7599. return 0;
  7600. if (!test_bit(ICE_FLAG_TC_MQPRIO, pf->flags) ||
  7601. main_vsi->old_numtc == 1)
  7602. return 0; /* nothing to be done */
  7603. /* reconfigure main VSI based on old value of TC and cached values
  7604. * for MQPRIO opts
  7605. */
  7606. err = ice_vsi_cfg_tc(main_vsi, main_vsi->old_ena_tc);
  7607. if (err) {
  7608. dev_err(dev, "failed configuring TC(ena_tc:0x%02x) for HW VSI=%u\n",
  7609. main_vsi->old_ena_tc, main_vsi->vsi_num);
  7610. return err;
  7611. }
  7612. /* rebuild ADQ VSIs */
  7613. ice_for_each_vsi(pf, i) {
  7614. enum ice_vsi_type type;
  7615. vsi = pf->vsi[i];
  7616. if (!vsi || vsi->type != ICE_VSI_CHNL)
  7617. continue;
  7618. type = vsi->type;
  7619. /* rebuild ADQ VSI */
  7620. err = ice_vsi_rebuild(vsi, ICE_VSI_FLAG_INIT);
  7621. if (err) {
  7622. dev_err(dev, "VSI (type:%s) at index %d rebuild failed, err %d\n",
  7623. ice_vsi_type_str(type), vsi->idx, err);
  7624. goto cleanup;
  7625. }
  7626. /* Re-map HW VSI number, using VSI handle that has been
  7627. * previously validated in ice_replay_vsi() call above
  7628. */
  7629. vsi->vsi_num = ice_get_hw_vsi_num(&pf->hw, vsi->idx);
  7630. /* replay filters for the VSI */
  7631. err = ice_replay_vsi(&pf->hw, vsi->idx);
  7632. if (err) {
  7633. dev_err(dev, "VSI (type:%s) replay failed, err %d, VSI index %d\n",
  7634. ice_vsi_type_str(type), err, vsi->idx);
  7635. rem_adv_fltr = false;
  7636. goto cleanup;
  7637. }
  7638. dev_info(dev, "VSI (type:%s) at index %d rebuilt successfully\n",
  7639. ice_vsi_type_str(type), vsi->idx);
  7640. /* store ADQ VSI at correct TC index in main VSI's
  7641. * map of TC to VSI
  7642. */
  7643. main_vsi->tc_map_vsi[tc_idx++] = vsi;
  7644. }
  7645. /* ADQ VSI(s) has been rebuilt successfully, so setup
  7646. * channel for main VSI's Tx and Rx rings
  7647. */
  7648. list_for_each_entry(ch, &main_vsi->ch_list, list) {
  7649. struct ice_vsi *ch_vsi;
  7650. ch_vsi = ch->ch_vsi;
  7651. if (!ch_vsi)
  7652. continue;
  7653. /* reconfig channel resources */
  7654. ice_cfg_chnl_all_res(main_vsi, ch);
  7655. /* replay BW rate limit if it is non-zero */
  7656. if (!ch->max_tx_rate && !ch->min_tx_rate)
  7657. continue;
  7658. err = ice_set_bw_limit(ch_vsi, ch->max_tx_rate,
  7659. ch->min_tx_rate);
  7660. if (err)
  7661. dev_err(dev, "failed (err:%d) to rebuild BW rate limit, max_tx_rate: %llu Kbps, min_tx_rate: %llu Kbps for VSI(%u)\n",
  7662. err, ch->max_tx_rate, ch->min_tx_rate,
  7663. ch_vsi->vsi_num);
  7664. else
  7665. dev_dbg(dev, "successfully rebuild BW rate limit, max_tx_rate: %llu Kbps, min_tx_rate: %llu Kbps for VSI(%u)\n",
  7666. ch->max_tx_rate, ch->min_tx_rate,
  7667. ch_vsi->vsi_num);
  7668. }
  7669. /* reconfig RSS for main VSI */
  7670. if (main_vsi->ch_rss_size)
  7671. ice_vsi_cfg_rss_lut_key(main_vsi);
  7672. return 0;
  7673. cleanup:
  7674. ice_remove_q_channels(main_vsi, rem_adv_fltr);
  7675. return err;
  7676. }
  7677. /**
  7678. * ice_create_q_channels - Add queue channel for the given TCs
  7679. * @vsi: VSI to be configured
  7680. *
  7681. * Configures queue channel mapping to the given TCs
  7682. */
  7683. static int ice_create_q_channels(struct ice_vsi *vsi)
  7684. {
  7685. struct ice_pf *pf = vsi->back;
  7686. struct ice_channel *ch;
  7687. int ret = 0, i;
  7688. ice_for_each_chnl_tc(i) {
  7689. if (!(vsi->all_enatc & BIT(i)))
  7690. continue;
  7691. ch = kzalloc(sizeof(*ch), GFP_KERNEL);
  7692. if (!ch) {
  7693. ret = -ENOMEM;
  7694. goto err_free;
  7695. }
  7696. INIT_LIST_HEAD(&ch->list);
  7697. ch->num_rxq = vsi->mqprio_qopt.qopt.count[i];
  7698. ch->num_txq = vsi->mqprio_qopt.qopt.count[i];
  7699. ch->base_q = vsi->mqprio_qopt.qopt.offset[i];
  7700. ch->max_tx_rate = vsi->mqprio_qopt.max_rate[i];
  7701. ch->min_tx_rate = vsi->mqprio_qopt.min_rate[i];
  7702. /* convert to Kbits/s */
  7703. if (ch->max_tx_rate)
  7704. ch->max_tx_rate = div_u64(ch->max_tx_rate,
  7705. ICE_BW_KBPS_DIVISOR);
  7706. if (ch->min_tx_rate)
  7707. ch->min_tx_rate = div_u64(ch->min_tx_rate,
  7708. ICE_BW_KBPS_DIVISOR);
  7709. ret = ice_create_q_channel(vsi, ch);
  7710. if (ret) {
  7711. dev_err(ice_pf_to_dev(pf),
  7712. "failed creating channel TC:%d\n", i);
  7713. kfree(ch);
  7714. goto err_free;
  7715. }
  7716. list_add_tail(&ch->list, &vsi->ch_list);
  7717. vsi->tc_map_vsi[i] = ch->ch_vsi;
  7718. dev_dbg(ice_pf_to_dev(pf),
  7719. "successfully created channel: VSI %pK\n", ch->ch_vsi);
  7720. }
  7721. return 0;
  7722. err_free:
  7723. ice_remove_q_channels(vsi, false);
  7724. return ret;
  7725. }
  7726. /**
  7727. * ice_setup_tc_mqprio_qdisc - configure multiple traffic classes
  7728. * @netdev: net device to configure
  7729. * @type_data: TC offload data
  7730. */
  7731. static int ice_setup_tc_mqprio_qdisc(struct net_device *netdev, void *type_data)
  7732. {
  7733. struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
  7734. struct ice_netdev_priv *np = netdev_priv(netdev);
  7735. struct ice_vsi *vsi = np->vsi;
  7736. struct ice_pf *pf = vsi->back;
  7737. u16 mode, ena_tc_qdisc = 0;
  7738. int cur_txq, cur_rxq;
  7739. u8 hw = 0, num_tcf;
  7740. struct device *dev;
  7741. int ret, i;
  7742. dev = ice_pf_to_dev(pf);
  7743. num_tcf = mqprio_qopt->qopt.num_tc;
  7744. hw = mqprio_qopt->qopt.hw;
  7745. mode = mqprio_qopt->mode;
  7746. if (!hw) {
  7747. clear_bit(ICE_FLAG_TC_MQPRIO, pf->flags);
  7748. vsi->ch_rss_size = 0;
  7749. memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
  7750. goto config_tcf;
  7751. }
  7752. /* Generate queue region map for number of TCF requested */
  7753. for (i = 0; i < num_tcf; i++)
  7754. ena_tc_qdisc |= BIT(i);
  7755. switch (mode) {
  7756. case TC_MQPRIO_MODE_CHANNEL:
  7757. if (pf->hw.port_info->is_custom_tx_enabled) {
  7758. dev_err(dev, "Custom Tx scheduler feature enabled, can't configure ADQ\n");
  7759. return -EBUSY;
  7760. }
  7761. ice_tear_down_devlink_rate_tree(pf);
  7762. ret = ice_validate_mqprio_qopt(vsi, mqprio_qopt);
  7763. if (ret) {
  7764. netdev_err(netdev, "failed to validate_mqprio_qopt(), ret %d\n",
  7765. ret);
  7766. return ret;
  7767. }
  7768. memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
  7769. set_bit(ICE_FLAG_TC_MQPRIO, pf->flags);
  7770. /* don't assume state of hw_tc_offload during driver load
  7771. * and set the flag for TC flower filter if hw_tc_offload
  7772. * already ON
  7773. */
  7774. if (vsi->netdev->features & NETIF_F_HW_TC)
  7775. set_bit(ICE_FLAG_CLS_FLOWER, pf->flags);
  7776. break;
  7777. default:
  7778. return -EINVAL;
  7779. }
  7780. config_tcf:
  7781. /* Requesting same TCF configuration as already enabled */
  7782. if (ena_tc_qdisc == vsi->tc_cfg.ena_tc &&
  7783. mode != TC_MQPRIO_MODE_CHANNEL)
  7784. return 0;
  7785. /* Pause VSI queues */
  7786. ice_dis_vsi(vsi, true);
  7787. if (!hw && !test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))
  7788. ice_remove_q_channels(vsi, true);
  7789. if (!hw && !test_bit(ICE_FLAG_TC_MQPRIO, pf->flags)) {
  7790. vsi->req_txq = min_t(int, ice_get_avail_txq_count(pf),
  7791. num_online_cpus());
  7792. vsi->req_rxq = min_t(int, ice_get_avail_rxq_count(pf),
  7793. num_online_cpus());
  7794. } else {
  7795. /* logic to rebuild VSI, same like ethtool -L */
  7796. u16 offset = 0, qcount_tx = 0, qcount_rx = 0;
  7797. for (i = 0; i < num_tcf; i++) {
  7798. if (!(ena_tc_qdisc & BIT(i)))
  7799. continue;
  7800. offset = vsi->mqprio_qopt.qopt.offset[i];
  7801. qcount_rx = vsi->mqprio_qopt.qopt.count[i];
  7802. qcount_tx = vsi->mqprio_qopt.qopt.count[i];
  7803. }
  7804. vsi->req_txq = offset + qcount_tx;
  7805. vsi->req_rxq = offset + qcount_rx;
  7806. /* store away original rss_size info, so that it gets reused
  7807. * form ice_vsi_rebuild during tc-qdisc delete stage - to
  7808. * determine, what should be the rss_sizefor main VSI
  7809. */
  7810. vsi->orig_rss_size = vsi->rss_size;
  7811. }
  7812. /* save current values of Tx and Rx queues before calling VSI rebuild
  7813. * for fallback option
  7814. */
  7815. cur_txq = vsi->num_txq;
  7816. cur_rxq = vsi->num_rxq;
  7817. /* proceed with rebuild main VSI using correct number of queues */
  7818. ret = ice_vsi_rebuild(vsi, ICE_VSI_FLAG_NO_INIT);
  7819. if (ret) {
  7820. /* fallback to current number of queues */
  7821. dev_info(dev, "Rebuild failed with new queues, try with current number of queues\n");
  7822. vsi->req_txq = cur_txq;
  7823. vsi->req_rxq = cur_rxq;
  7824. clear_bit(ICE_RESET_FAILED, pf->state);
  7825. if (ice_vsi_rebuild(vsi, ICE_VSI_FLAG_NO_INIT)) {
  7826. dev_err(dev, "Rebuild of main VSI failed again\n");
  7827. return ret;
  7828. }
  7829. }
  7830. vsi->all_numtc = num_tcf;
  7831. vsi->all_enatc = ena_tc_qdisc;
  7832. ret = ice_vsi_cfg_tc(vsi, ena_tc_qdisc);
  7833. if (ret) {
  7834. netdev_err(netdev, "failed configuring TC for VSI id=%d\n",
  7835. vsi->vsi_num);
  7836. goto exit;
  7837. }
  7838. if (test_bit(ICE_FLAG_TC_MQPRIO, pf->flags)) {
  7839. u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
  7840. u64 min_tx_rate = vsi->mqprio_qopt.min_rate[0];
  7841. /* set TC0 rate limit if specified */
  7842. if (max_tx_rate || min_tx_rate) {
  7843. /* convert to Kbits/s */
  7844. if (max_tx_rate)
  7845. max_tx_rate = div_u64(max_tx_rate, ICE_BW_KBPS_DIVISOR);
  7846. if (min_tx_rate)
  7847. min_tx_rate = div_u64(min_tx_rate, ICE_BW_KBPS_DIVISOR);
  7848. ret = ice_set_bw_limit(vsi, max_tx_rate, min_tx_rate);
  7849. if (!ret) {
  7850. dev_dbg(dev, "set Tx rate max %llu min %llu for VSI(%u)\n",
  7851. max_tx_rate, min_tx_rate, vsi->vsi_num);
  7852. } else {
  7853. dev_err(dev, "failed to set Tx rate max %llu min %llu for VSI(%u)\n",
  7854. max_tx_rate, min_tx_rate, vsi->vsi_num);
  7855. goto exit;
  7856. }
  7857. }
  7858. ret = ice_create_q_channels(vsi);
  7859. if (ret) {
  7860. netdev_err(netdev, "failed configuring queue channels\n");
  7861. goto exit;
  7862. } else {
  7863. netdev_dbg(netdev, "successfully configured channels\n");
  7864. }
  7865. }
  7866. if (vsi->ch_rss_size)
  7867. ice_vsi_cfg_rss_lut_key(vsi);
  7868. exit:
  7869. /* if error, reset the all_numtc and all_enatc */
  7870. if (ret) {
  7871. vsi->all_numtc = 0;
  7872. vsi->all_enatc = 0;
  7873. }
  7874. /* resume VSI */
  7875. ice_ena_vsi(vsi, true);
  7876. return ret;
  7877. }
  7878. static LIST_HEAD(ice_block_cb_list);
  7879. static int
  7880. ice_setup_tc(struct net_device *netdev, enum tc_setup_type type,
  7881. void *type_data)
  7882. {
  7883. struct ice_netdev_priv *np = netdev_priv(netdev);
  7884. struct ice_pf *pf = np->vsi->back;
  7885. bool locked = false;
  7886. int err;
  7887. switch (type) {
  7888. case TC_SETUP_BLOCK:
  7889. return flow_block_cb_setup_simple(type_data,
  7890. &ice_block_cb_list,
  7891. ice_setup_tc_block_cb,
  7892. np, np, true);
  7893. case TC_SETUP_QDISC_MQPRIO:
  7894. if (ice_is_eswitch_mode_switchdev(pf)) {
  7895. netdev_err(netdev, "TC MQPRIO offload not supported, switchdev is enabled\n");
  7896. return -EOPNOTSUPP;
  7897. }
  7898. if (pf->adev) {
  7899. mutex_lock(&pf->adev_mutex);
  7900. device_lock(&pf->adev->dev);
  7901. locked = true;
  7902. if (pf->adev->dev.driver) {
  7903. netdev_err(netdev, "Cannot change qdisc when RDMA is active\n");
  7904. err = -EBUSY;
  7905. goto adev_unlock;
  7906. }
  7907. }
  7908. /* setup traffic classifier for receive side */
  7909. mutex_lock(&pf->tc_mutex);
  7910. err = ice_setup_tc_mqprio_qdisc(netdev, type_data);
  7911. mutex_unlock(&pf->tc_mutex);
  7912. adev_unlock:
  7913. if (locked) {
  7914. device_unlock(&pf->adev->dev);
  7915. mutex_unlock(&pf->adev_mutex);
  7916. }
  7917. return err;
  7918. default:
  7919. return -EOPNOTSUPP;
  7920. }
  7921. return -EOPNOTSUPP;
  7922. }
  7923. static struct ice_indr_block_priv *
  7924. ice_indr_block_priv_lookup(struct ice_netdev_priv *np,
  7925. struct net_device *netdev)
  7926. {
  7927. struct ice_indr_block_priv *cb_priv;
  7928. list_for_each_entry(cb_priv, &np->tc_indr_block_priv_list, list) {
  7929. if (!cb_priv->netdev)
  7930. return NULL;
  7931. if (cb_priv->netdev == netdev)
  7932. return cb_priv;
  7933. }
  7934. return NULL;
  7935. }
  7936. static int
  7937. ice_indr_setup_block_cb(enum tc_setup_type type, void *type_data,
  7938. void *indr_priv)
  7939. {
  7940. struct ice_indr_block_priv *priv = indr_priv;
  7941. struct ice_netdev_priv *np = priv->np;
  7942. switch (type) {
  7943. case TC_SETUP_CLSFLOWER:
  7944. return ice_setup_tc_cls_flower(np, priv->netdev,
  7945. (struct flow_cls_offload *)
  7946. type_data);
  7947. default:
  7948. return -EOPNOTSUPP;
  7949. }
  7950. }
  7951. static int
  7952. ice_indr_setup_tc_block(struct net_device *netdev, struct Qdisc *sch,
  7953. struct ice_netdev_priv *np,
  7954. struct flow_block_offload *f, void *data,
  7955. void (*cleanup)(struct flow_block_cb *block_cb))
  7956. {
  7957. struct ice_indr_block_priv *indr_priv;
  7958. struct flow_block_cb *block_cb;
  7959. if (!ice_is_tunnel_supported(netdev) &&
  7960. !(is_vlan_dev(netdev) &&
  7961. vlan_dev_real_dev(netdev) == np->vsi->netdev))
  7962. return -EOPNOTSUPP;
  7963. if (f->binder_type != FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
  7964. return -EOPNOTSUPP;
  7965. switch (f->command) {
  7966. case FLOW_BLOCK_BIND:
  7967. indr_priv = ice_indr_block_priv_lookup(np, netdev);
  7968. if (indr_priv)
  7969. return -EEXIST;
  7970. indr_priv = kzalloc(sizeof(*indr_priv), GFP_KERNEL);
  7971. if (!indr_priv)
  7972. return -ENOMEM;
  7973. indr_priv->netdev = netdev;
  7974. indr_priv->np = np;
  7975. list_add(&indr_priv->list, &np->tc_indr_block_priv_list);
  7976. block_cb =
  7977. flow_indr_block_cb_alloc(ice_indr_setup_block_cb,
  7978. indr_priv, indr_priv,
  7979. ice_rep_indr_tc_block_unbind,
  7980. f, netdev, sch, data, np,
  7981. cleanup);
  7982. if (IS_ERR(block_cb)) {
  7983. list_del(&indr_priv->list);
  7984. kfree(indr_priv);
  7985. return PTR_ERR(block_cb);
  7986. }
  7987. flow_block_cb_add(block_cb, f);
  7988. list_add_tail(&block_cb->driver_list, &ice_block_cb_list);
  7989. break;
  7990. case FLOW_BLOCK_UNBIND:
  7991. indr_priv = ice_indr_block_priv_lookup(np, netdev);
  7992. if (!indr_priv)
  7993. return -ENOENT;
  7994. block_cb = flow_block_cb_lookup(f->block,
  7995. ice_indr_setup_block_cb,
  7996. indr_priv);
  7997. if (!block_cb)
  7998. return -ENOENT;
  7999. flow_indr_block_cb_remove(block_cb, f);
  8000. list_del(&block_cb->driver_list);
  8001. break;
  8002. default:
  8003. return -EOPNOTSUPP;
  8004. }
  8005. return 0;
  8006. }
  8007. static int
  8008. ice_indr_setup_tc_cb(struct net_device *netdev, struct Qdisc *sch,
  8009. void *cb_priv, enum tc_setup_type type, void *type_data,
  8010. void *data,
  8011. void (*cleanup)(struct flow_block_cb *block_cb))
  8012. {
  8013. switch (type) {
  8014. case TC_SETUP_BLOCK:
  8015. return ice_indr_setup_tc_block(netdev, sch, cb_priv, type_data,
  8016. data, cleanup);
  8017. default:
  8018. return -EOPNOTSUPP;
  8019. }
  8020. }
  8021. /**
  8022. * ice_open - Called when a network interface becomes active
  8023. * @netdev: network interface device structure
  8024. *
  8025. * The open entry point is called when a network interface is made
  8026. * active by the system (IFF_UP). At this point all resources needed
  8027. * for transmit and receive operations are allocated, the interrupt
  8028. * handler is registered with the OS, the netdev watchdog is enabled,
  8029. * and the stack is notified that the interface is ready.
  8030. *
  8031. * Returns 0 on success, negative value on failure
  8032. */
  8033. int ice_open(struct net_device *netdev)
  8034. {
  8035. struct ice_netdev_priv *np = netdev_priv(netdev);
  8036. struct ice_pf *pf = np->vsi->back;
  8037. if (ice_is_reset_in_progress(pf->state)) {
  8038. netdev_err(netdev, "can't open net device while reset is in progress");
  8039. return -EBUSY;
  8040. }
  8041. return ice_open_internal(netdev);
  8042. }
  8043. /**
  8044. * ice_open_internal - Called when a network interface becomes active
  8045. * @netdev: network interface device structure
  8046. *
  8047. * Internal ice_open implementation. Should not be used directly except for ice_open and reset
  8048. * handling routine
  8049. *
  8050. * Returns 0 on success, negative value on failure
  8051. */
  8052. int ice_open_internal(struct net_device *netdev)
  8053. {
  8054. struct ice_netdev_priv *np = netdev_priv(netdev);
  8055. struct ice_vsi *vsi = np->vsi;
  8056. struct ice_pf *pf = vsi->back;
  8057. struct ice_port_info *pi;
  8058. int err;
  8059. if (test_bit(ICE_NEEDS_RESTART, pf->state)) {
  8060. netdev_err(netdev, "driver needs to be unloaded and reloaded\n");
  8061. return -EIO;
  8062. }
  8063. netif_carrier_off(netdev);
  8064. pi = vsi->port_info;
  8065. err = ice_update_link_info(pi);
  8066. if (err) {
  8067. netdev_err(netdev, "Failed to get link info, error %d\n", err);
  8068. return err;
  8069. }
  8070. ice_check_link_cfg_err(pf, pi->phy.link_info.link_cfg_err);
  8071. /* Set PHY if there is media, otherwise, turn off PHY */
  8072. if (pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE) {
  8073. clear_bit(ICE_FLAG_NO_MEDIA, pf->flags);
  8074. if (!test_bit(ICE_PHY_INIT_COMPLETE, pf->state)) {
  8075. err = ice_init_phy_user_cfg(pi);
  8076. if (err) {
  8077. netdev_err(netdev, "Failed to initialize PHY settings, error %d\n",
  8078. err);
  8079. return err;
  8080. }
  8081. }
  8082. err = ice_configure_phy(vsi);
  8083. if (err) {
  8084. netdev_err(netdev, "Failed to set physical link up, error %d\n",
  8085. err);
  8086. return err;
  8087. }
  8088. } else {
  8089. set_bit(ICE_FLAG_NO_MEDIA, pf->flags);
  8090. ice_set_link(vsi, false);
  8091. }
  8092. err = ice_vsi_open(vsi);
  8093. if (err)
  8094. netdev_err(netdev, "Failed to open VSI 0x%04X on switch 0x%04X\n",
  8095. vsi->vsi_num, vsi->vsw->sw_id);
  8096. /* Update existing tunnels information */
  8097. udp_tunnel_get_rx_info(netdev);
  8098. return err;
  8099. }
  8100. /**
  8101. * ice_stop - Disables a network interface
  8102. * @netdev: network interface device structure
  8103. *
  8104. * The stop entry point is called when an interface is de-activated by the OS,
  8105. * and the netdevice enters the DOWN state. The hardware is still under the
  8106. * driver's control, but the netdev interface is disabled.
  8107. *
  8108. * Returns success only - not allowed to fail
  8109. */
  8110. int ice_stop(struct net_device *netdev)
  8111. {
  8112. struct ice_netdev_priv *np = netdev_priv(netdev);
  8113. struct ice_vsi *vsi = np->vsi;
  8114. struct ice_pf *pf = vsi->back;
  8115. if (ice_is_reset_in_progress(pf->state)) {
  8116. netdev_err(netdev, "can't stop net device while reset is in progress");
  8117. return -EBUSY;
  8118. }
  8119. if (test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, vsi->back->flags)) {
  8120. int link_err = ice_force_phys_link_state(vsi, false);
  8121. if (link_err) {
  8122. if (link_err == -ENOMEDIUM)
  8123. netdev_info(vsi->netdev, "Skipping link reconfig - no media attached, VSI %d\n",
  8124. vsi->vsi_num);
  8125. else
  8126. netdev_err(vsi->netdev, "Failed to set physical link down, VSI %d error %d\n",
  8127. vsi->vsi_num, link_err);
  8128. ice_vsi_close(vsi);
  8129. return -EIO;
  8130. }
  8131. }
  8132. ice_vsi_close(vsi);
  8133. return 0;
  8134. }
  8135. /**
  8136. * ice_features_check - Validate encapsulated packet conforms to limits
  8137. * @skb: skb buffer
  8138. * @netdev: This port's netdev
  8139. * @features: Offload features that the stack believes apply
  8140. */
  8141. static netdev_features_t
  8142. ice_features_check(struct sk_buff *skb,
  8143. struct net_device __always_unused *netdev,
  8144. netdev_features_t features)
  8145. {
  8146. bool gso = skb_is_gso(skb);
  8147. size_t len;
  8148. /* No point in doing any of this if neither checksum nor GSO are
  8149. * being requested for this frame. We can rule out both by just
  8150. * checking for CHECKSUM_PARTIAL
  8151. */
  8152. if (skb->ip_summed != CHECKSUM_PARTIAL)
  8153. return features;
  8154. /* We cannot support GSO if the MSS is going to be less than
  8155. * 64 bytes. If it is then we need to drop support for GSO.
  8156. */
  8157. if (gso && (skb_shinfo(skb)->gso_size < ICE_TXD_CTX_MIN_MSS))
  8158. features &= ~NETIF_F_GSO_MASK;
  8159. len = skb_network_offset(skb);
  8160. if (len > ICE_TXD_MACLEN_MAX || len & 0x1)
  8161. goto out_rm_features;
  8162. len = skb_network_header_len(skb);
  8163. if (len > ICE_TXD_IPLEN_MAX || len & 0x1)
  8164. goto out_rm_features;
  8165. if (skb->encapsulation) {
  8166. /* this must work for VXLAN frames AND IPIP/SIT frames, and in
  8167. * the case of IPIP frames, the transport header pointer is
  8168. * after the inner header! So check to make sure that this
  8169. * is a GRE or UDP_TUNNEL frame before doing that math.
  8170. */
  8171. if (gso && (skb_shinfo(skb)->gso_type &
  8172. (SKB_GSO_GRE | SKB_GSO_UDP_TUNNEL))) {
  8173. len = skb_inner_network_header(skb) -
  8174. skb_transport_header(skb);
  8175. if (len > ICE_TXD_L4LEN_MAX || len & 0x1)
  8176. goto out_rm_features;
  8177. }
  8178. len = skb_inner_network_header_len(skb);
  8179. if (len > ICE_TXD_IPLEN_MAX || len & 0x1)
  8180. goto out_rm_features;
  8181. }
  8182. return features;
  8183. out_rm_features:
  8184. return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
  8185. }
  8186. static const struct net_device_ops ice_netdev_safe_mode_ops = {
  8187. .ndo_open = ice_open,
  8188. .ndo_stop = ice_stop,
  8189. .ndo_start_xmit = ice_start_xmit,
  8190. .ndo_set_mac_address = ice_set_mac_address,
  8191. .ndo_validate_addr = eth_validate_addr,
  8192. .ndo_change_mtu = ice_change_mtu,
  8193. .ndo_get_stats64 = ice_get_stats64,
  8194. .ndo_tx_timeout = ice_tx_timeout,
  8195. .ndo_bpf = ice_xdp_safe_mode,
  8196. };
  8197. static const struct net_device_ops ice_netdev_ops = {
  8198. .ndo_open = ice_open,
  8199. .ndo_stop = ice_stop,
  8200. .ndo_start_xmit = ice_start_xmit,
  8201. .ndo_select_queue = ice_select_queue,
  8202. .ndo_features_check = ice_features_check,
  8203. .ndo_fix_features = ice_fix_features,
  8204. .ndo_set_rx_mode = ice_set_rx_mode,
  8205. .ndo_set_mac_address = ice_set_mac_address,
  8206. .ndo_validate_addr = eth_validate_addr,
  8207. .ndo_change_mtu = ice_change_mtu,
  8208. .ndo_get_stats64 = ice_get_stats64,
  8209. .ndo_set_tx_maxrate = ice_set_tx_maxrate,
  8210. .ndo_eth_ioctl = ice_eth_ioctl,
  8211. .ndo_set_vf_spoofchk = ice_set_vf_spoofchk,
  8212. .ndo_set_vf_mac = ice_set_vf_mac,
  8213. .ndo_get_vf_config = ice_get_vf_cfg,
  8214. .ndo_set_vf_trust = ice_set_vf_trust,
  8215. .ndo_set_vf_vlan = ice_set_vf_port_vlan,
  8216. .ndo_set_vf_link_state = ice_set_vf_link_state,
  8217. .ndo_get_vf_stats = ice_get_vf_stats,
  8218. .ndo_set_vf_rate = ice_set_vf_bw,
  8219. .ndo_vlan_rx_add_vid = ice_vlan_rx_add_vid,
  8220. .ndo_vlan_rx_kill_vid = ice_vlan_rx_kill_vid,
  8221. .ndo_setup_tc = ice_setup_tc,
  8222. .ndo_set_features = ice_set_features,
  8223. .ndo_bridge_getlink = ice_bridge_getlink,
  8224. .ndo_bridge_setlink = ice_bridge_setlink,
  8225. .ndo_fdb_add = ice_fdb_add,
  8226. .ndo_fdb_del = ice_fdb_del,
  8227. #ifdef CONFIG_RFS_ACCEL
  8228. .ndo_rx_flow_steer = ice_rx_flow_steer,
  8229. #endif
  8230. .ndo_tx_timeout = ice_tx_timeout,
  8231. .ndo_bpf = ice_xdp,
  8232. .ndo_xdp_xmit = ice_xdp_xmit,
  8233. .ndo_xsk_wakeup = ice_xsk_wakeup,
  8234. };